blob: 292c51d052975e2cfea59f451b44350a2493bb52 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/*
2 * Base driver for ASR SCS RTC
3 *
4 * Copyright 2020 ASR Microelectronics (Shanghai) Co., Ltd.
5 *
6 * This file is subject to the terms and conditions of the GNU General
7 * Public License. See the file "COPYING" in the main directory of this
8 * archive for more details.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/slab.h>
23#include <linux/regmap.h>
24#include <linux/mfd/core.h>
25#include <linux/mfd/88pm80x.h>
26#include <linux/mfd/pm803.h>
27#include <linux/mfd/pm802.h>
28#include <linux/mfd/pm813.h>
29#include <linux/rtc.h>
30#include <linux/reboot.h>
31#include <linux/sysfs.h>
32#include <soc/asr/addr-map.h>
33#include <linux/cputype.h>
34
35#ifdef CONFIG_CPU_ASR1903
36#define SCS_RTC_FREQ (32765)
37#else
38#define SCS_RTC_FREQ (32787)
39#endif
40
41#define SCS_RTC_COUNTR (0x0)
42#define SCS_RTC_ALARM (0x4)
43#define SCS_RTC_TRIM (0x8)
44#define SCS_RTC_ALARM_EN (0x1 << 0)
45#define SCS_RTC_ALARM_MASK (0x1 << 0)
46#define SCS_RTC_ALARM_CLR (0x1 << 1)
47#define SCS_RTC_CNTL (0xc)
48#define SCS_IDAC_CODE (0x10)
49#define SCS_RTC_SYNC_CFG (0x14)
50#define SCS_DCS_MODE (0x1c)
51#define SCS_CLEAR_SYNC_DONE (0x1 << 0 | 0x1 << 6)
52#define SCS_SYNC_IDAC_CODE (0x1 << 0 | 0x1 << 5)
53#define SCS_SYNC_RTC_CNRL (0x1 << 0 | 0x1 << 4)
54#define SCS_SYNC_RTC_TRIM (0x1 << 0 | 0x1 << 3)
55#define SCS_SYNC_RTC_ALARM (0x1 << 0 | 0x1 << 2)
56#define SCS_SYNC_RTC_ALARM_CNTL (0x1 << 0 | 0x1 << 2 | 0x1 << 4)
57#define SCS_SYNC_RTC_COUNTER (0x1 << 0 | 0x1 << 1)
58#define SCS_RTC_VIRT_BASE (APB_VIRT_BASE + 0x03E000)
59#define ASR1903_SCS_RTC_VIRT_BASE (APB_VIRT_BASE + 0x0C0000)
60
61#define SCR_RTC_SYNC_UDELAY (130)
62
63struct scs_rtc_info {
64 spinlock_t lock;
65 void __iomem *base;
66 struct pm80x_chip *chip;
67 struct regmap *map;
68 struct rtc_device *rtc_dev;
69 struct device *dev;
70 int irq;
71 int vrtc;
72 int (*sync) (s64 ticks);
73
74#ifdef CONFIG_RTC_DRV_SA1100
75 struct delayed_work sa1100_sync_work;
76#endif
77};
78
79static s64 base_ticks;
80#ifdef CONFIG_RTC_DRV_SA1100
81extern int sync_time_to_soc(s64 ticks);
82#endif
83static int scs_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled);
84
85#ifdef CONFIG_RTC_DRV_SA1100
86static int scs_rtc_read_time(struct device *dev, struct rtc_time *tm);
87static void sa1100_sync_fn(struct work_struct *work)
88{
89 struct scs_rtc_info *info =
90 container_of(work, struct scs_rtc_info, sa1100_sync_work.work);
91 s64 ticks;
92 struct rtc_time tm;
93 int ret;
94
95 ret = scs_rtc_read_time(info->dev, &tm);
96 if (ret < 0) {
97 dev_err(info->dev, "Failed to read time.\n");
98 return;
99 }
100
101 ticks = rtc_tm_to_time64(&tm);
102 if (info->sync)
103 info->sync(ticks);
104}
105#endif
106
107static int pm80x_write_base_ticks(struct scs_rtc_info *info, s64 base_ticks)
108{
109 u8 buf[5];
110 u32 reg = PM802_RTC_EXPIRE2_1;
111
112 BUG_ON(CHIP_PM801 == info->chip->type);
113 buf[0] = base_ticks & 0xFF;
114 buf[1] = (base_ticks >> 8) & 0xFF;
115 buf[2] = (base_ticks >> 16) & 0xFF;
116 buf[3] = (base_ticks >> 24) & 0xFF;
117 buf[4] = (base_ticks >> 32) & 0xFF;
118 dev_dbg(info->dev, "set base ticks:0x%llx\n", base_ticks);
119
120 if (CHIP_PM803 != info->chip->type) {
121 regmap_raw_write(info->map, reg, buf, 4);
122 }
123
124 if (CHIP_PM802 == info->chip->type) {
125 if (CHIP_PM802_ID_B1 == info->chip->chip_id ||
126 CHIP_PM802_ID_B0 == info->chip->chip_id) {
127 regmap_raw_read(info->map, PM802_RTC_MISC14, buf, 1);
128 buf[0] &= 0x0f;
129 buf[0] |= ((buf[4] & 0xf) << 4);
130 /* F1[7:4] */
131 regmap_raw_write(info->map, PM802_RTC_MISC14, &buf[0], 1);
132 } else {
133 /* CD[7:0] */
134 regmap_raw_write(info->map, PM802S_RTC_SPARED, &buf[4], 1);
135 }
136 } else if (CHIP_PM803 == info->chip->type) {
137 regmap_raw_write(info->map, PM803_RTC_MISC18, &buf[0], 3);
138 regmap_raw_write(info->map, PM803_RTC_SPARE5, &buf[3], 2);
139 } else if (CHIP_PM813 == info->chip->type) {
140 if (CHIP_PM813_ID == info->chip->chip_id) {
141 /* F6[7:0] */
142 regmap_raw_write(info->map, PM813_RTC_MISC19, &buf[4], 1);
143 } else {
144 /* CD[7:0] */
145 regmap_raw_write(info->map, PM813S_RTC_SPARED, &buf[4], 1);
146 }
147 } else {
148 BUG();
149 }
150
151 return 0;
152}
153
154static int pm80x_read_base_ticks(struct scs_rtc_info *info, s64 *base_ticks)
155{
156 u8 buf[5];
157 u32 reg = PM802_RTC_EXPIRE2_1;
158
159 BUG_ON(CHIP_PM801 == info->chip->type);
160
161 if (CHIP_PM803 != info->chip->type) {
162 regmap_raw_read(info->map, reg, buf, 4);
163 }
164
165 if (CHIP_PM802 == info->chip->type) {
166 if (CHIP_PM802_ID_B1 == info->chip->chip_id ||
167 CHIP_PM802_ID_B0 == info->chip->chip_id) {
168 /* F1[7:4] */
169 regmap_raw_read(info->map, PM802_RTC_MISC14, &buf[4], 1);
170 buf[4] = (buf[4] >> 4) & 0xf;
171 } else {
172 /* CD[7:0] */
173 regmap_raw_read(info->map, PM802S_RTC_SPARED, &buf[4], 1);
174 }
175 } else if (CHIP_PM803 == info->chip->type) {
176 regmap_raw_read(info->map, PM803_RTC_MISC18, &buf[0], 3);
177 regmap_raw_read(info->map, PM803_RTC_SPARE5, &buf[3], 2);
178 } else if (CHIP_PM813 == info->chip->type) {
179 if (CHIP_PM813_ID == info->chip->chip_id) {
180 /* F6[7:0] */
181 regmap_raw_read(info->map, PM813_RTC_MISC19, &buf[4], 1);
182 } else {
183 /* CD[7:0] */
184 regmap_raw_read(info->map, PM813S_RTC_SPARED, &buf[4], 1);
185 }
186 }
187
188 *base_ticks = (((s64)buf[4]) << 60) >> 28;
189 *base_ticks |= (((u32)buf[3]) << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
190
191 dev_dbg(info->dev, "%x-%x-%x-%x-%x, base_ticks: %llx\n",
192 buf[0], buf[1], buf[2], buf[3], buf[4], *base_ticks);
193
194 return 0;
195}
196
197static int scs_rtc_alarm_clear(struct device *dev)
198{
199 struct scs_rtc_info *info = dev_get_drvdata(dev);
200 int ret = 0;
201 unsigned long flags;
202
203 spin_lock_irqsave(&info->lock, flags);
204 writel(readl(info->base + SCS_RTC_CNTL) | SCS_RTC_ALARM_CLR,
205 info->base + SCS_RTC_CNTL);
206 writel(SCS_SYNC_RTC_CNRL, info->base + SCS_RTC_SYNC_CFG);
207 udelay(SCR_RTC_SYNC_UDELAY);
208 writel(SCS_CLEAR_SYNC_DONE, info->base + SCS_RTC_SYNC_CFG);
209 spin_unlock_irqrestore(&info->lock, flags);
210
211 return ret;
212}
213
214static int scs_rtc_update_trim(struct device *dev)
215{
216 struct scs_rtc_info *info = dev_get_drvdata(dev);
217 int ret = 0;
218 unsigned long flags;
219
220 spin_lock_irqsave(&info->lock, flags);
221 if (cpu_is_asr1803_a0())
222 writel(0xffff, info->base + SCS_RTC_TRIM);
223 else
224 writel((SCS_RTC_FREQ - 1), info->base + SCS_RTC_TRIM);
225 writel(SCS_SYNC_RTC_TRIM, info->base + SCS_RTC_SYNC_CFG);
226 udelay(SCR_RTC_SYNC_UDELAY);
227 writel(SCS_CLEAR_SYNC_DONE, info->base + SCS_RTC_SYNC_CFG);
228
229 if (cpu_is_asr1903()) {
230 writel(0x14, info->base + SCS_IDAC_CODE);
231 writel(SCS_SYNC_IDAC_CODE, info->base + SCS_RTC_SYNC_CFG);
232 udelay(SCR_RTC_SYNC_UDELAY);
233 writel(SCS_CLEAR_SYNC_DONE, info->base + SCS_RTC_SYNC_CFG);
234 }
235 spin_unlock_irqrestore(&info->lock, flags);
236
237 return ret;
238}
239
240static irqreturn_t rtc_update_handler(int irq, void *data)
241{
242 struct scs_rtc_info *info = (struct scs_rtc_info *)data;
243
244 scs_rtc_alarm_clear(info->dev);
245 rtc_update_irq(info->rtc_dev, 1, RTC_AF);
246 return IRQ_HANDLED;
247}
248
249static int scs_rtc_read_time(struct device *dev, struct rtc_time *tm)
250{
251 unsigned long time_sec;
252 u64 tmp_sec;
253 u32 val1, val2;
254 s64 ticks;
255 struct scs_rtc_info *info = dev_get_drvdata(dev);
256
257 do {
258 val1 = readl(info->base + SCS_RTC_COUNTR);
259 val2 = readl(info->base + SCS_RTC_COUNTR);
260 } while (val2 != val1);
261
262 if (cpu_is_asr1803_a0()) {
263 tmp_sec = ((u64)val1) * 261993;
264 do_div(tmp_sec, (3 * 128 * 1024));
265 time_sec = (u32)tmp_sec;
266 } else {
267 time_sec = val1;
268 }
269
270 pm80x_read_base_ticks(info, &base_ticks);
271 ticks = time_sec + base_ticks;
272 rtc_time64_to_tm(ticks, tm);
273
274 return 0;
275}
276
277static int scs_rtc_set_time(struct device *dev, struct rtc_time *tm)
278{
279 unsigned long time;
280 u64 tmp_sec;
281 s64 ticks;
282 u32 val1, val2;
283 struct scs_rtc_info *info = dev_get_drvdata(dev);
284 int ret = 0;
285 unsigned long flags;
b.liub17525e2025-05-14 17:22:29 +0800286 // Add by mbtk
n.nie5f3f0c42025-05-15 16:37:45 +0800287 struct timespec64 ts;
b.liue9582032025-04-17 19:18:16 +0800288
289 if ((tm->tm_year < 70) || (tm->tm_year > 300)) {
290 dev_err(info->dev,
291 "SCSRTC: Set time %d out of range. Please set time between 1970 to 2200.\n",
292 1900 + tm->tm_year);
293 return -EINVAL;
294 }
295
296 spin_lock_irqsave(&info->lock, flags);
b.liub17525e2025-05-14 17:22:29 +0800297 // Add by mbtk
n.nie5f3f0c42025-05-15 16:37:45 +0800298 ktime_get_real_ts64(&ts);
b.liub17525e2025-05-14 17:22:29 +0800299 rtc_time_to_tm(ts.tv_sec - sys_tz.tz_minuteswest * 60, tm);
b.liue9582032025-04-17 19:18:16 +0800300 ticks = rtc_tm_to_time64(tm);
301 do {
302 val1 = readl(info->base + SCS_RTC_COUNTR);
303 val2 = readl(info->base + SCS_RTC_COUNTR);
304 } while (val2 != val1);
305
306 if (cpu_is_asr1803_a0()) {
307 tmp_sec = ((u64)val1) * 261993;
308 do_div(tmp_sec, (3 * 128 * 1024));
309 time = (u32)tmp_sec;
310 } else {
311 time = val1;
312 }
313
314 base_ticks = ticks - time;
315 spin_unlock_irqrestore(&info->lock, flags);
316 pm80x_write_base_ticks(info, base_ticks);
317 spin_lock_irqsave(&info->lock, flags);
318#if 0
319 if (cpu_is_asr1803_a0()) {
320 tmp_sec = ((u64)time) * (3 * 128 * 1024);
321 do_div(tmp_sec, 261993);
322 time = (unsigned long)tmp_sec;
323 }
324 writel(time, info->base + SCS_RTC_COUNTR);
325
326 writel(SCS_SYNC_RTC_COUNTER, info->base + SCS_RTC_SYNC_CFG);
327 udelay(SCR_RTC_SYNC_UDELAY);
328 writel(SCS_CLEAR_SYNC_DONE, info->base + SCS_RTC_SYNC_CFG);
329#endif
330 if (info->sync)
331 info->sync(ticks);
332 spin_unlock_irqrestore(&info->lock, flags);
333
334 return ret;
335}
336
337static int scs_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
338{
339 unsigned long ticks;
340 u64 tmp_sec;
341 s64 time;
342 struct scs_rtc_info *info = dev_get_drvdata(dev);
343
344 ticks = readl(info->base + SCS_RTC_ALARM);
345 if (cpu_is_asr1803_a0()) {
346 tmp_sec = ((u64)ticks) * 261993;
347 do_div(tmp_sec, (3 * 128 * 1024));
348 ticks = (u32)tmp_sec;
349 }
350
351 pm80x_read_base_ticks(info, &base_ticks);
352 time = ticks + base_ticks;
353
354 rtc_time64_to_tm(time, &alrm->time);
355 ticks = readl(info->base + SCS_RTC_CNTL);
356 alrm->enabled = ((ticks & SCS_RTC_ALARM_MASK) == SCS_RTC_ALARM_EN) ? 1 : 0;
357 alrm->pending = 0;
358 return 0;
359}
360
361static int scs_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
362{
363 struct scs_rtc_info *info = dev_get_drvdata(dev);
364 unsigned long flags;
365 u64 tmp_sec;
366 s64 ticks;
367 u32 time;
368
369 ticks = rtc_tm_to_time64(&alrm->time);
370 pm80x_read_base_ticks(info, &base_ticks);
371 ticks = ticks - base_ticks;
372 if (cpu_is_asr1803_a0()) {
373 tmp_sec = ((u64)ticks) * (3 * 128 * 1024);
374 //time = tmp_sec / 261993;
375 do_div(tmp_sec, 261993);
376 time = (u32)tmp_sec;
377 } else {
378 time = (u32)ticks;
379 }
380
381 spin_lock_irqsave(&info->lock, flags);
382 writel(time, info->base + SCS_RTC_ALARM);
383 writel(SCS_SYNC_RTC_ALARM_CNTL, info->base + SCS_RTC_SYNC_CFG);
384 udelay(SCR_RTC_SYNC_UDELAY);
385 writel(SCS_CLEAR_SYNC_DONE, info->base + SCS_RTC_SYNC_CFG);
386 spin_unlock_irqrestore(&info->lock, flags);
387 scs_rtc_alarm_irq_enable(dev, alrm->enabled);
388 return 0;
389}
390
391static int scs_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
392{
393 struct scs_rtc_info *info = dev_get_drvdata(dev);
394 int ret = 0;
395 unsigned long flags;
396
397 spin_lock_irqsave(&info->lock, flags);
398 if (enabled)
399 writel(readl(info->base + SCS_RTC_CNTL) | SCS_RTC_ALARM_MASK,
400 info->base + SCS_RTC_CNTL);
401 else
402 writel(readl(info->base + SCS_RTC_CNTL) & (~SCS_RTC_ALARM_MASK),
403 info->base + SCS_RTC_CNTL);
404 writel(SCS_SYNC_RTC_CNRL, info->base + SCS_RTC_SYNC_CFG);
405 udelay(SCR_RTC_SYNC_UDELAY);
406 writel(SCS_CLEAR_SYNC_DONE, info->base + SCS_RTC_SYNC_CFG);
407 spin_unlock_irqrestore(&info->lock, flags);
408
409 return ret;
410}
411
412static const struct rtc_class_ops scs_rtc_ops = {
413 .read_time = scs_rtc_read_time,
414 .set_time = scs_rtc_set_time,
415 .read_alarm = scs_rtc_read_alarm,
416 .set_alarm = scs_rtc_set_alarm,
417 .alarm_irq_enable = scs_rtc_alarm_irq_enable,
418};
419
420#ifdef CONFIG_PM_SLEEP
421static int scs_rtc_suspend(struct device *dev)
422{
423 return pm80x_dev_suspend(dev);
424}
425
426static int scs_rtc_resume(struct device *dev)
427{
428 return pm80x_dev_resume(dev);
429}
430#endif
431
432static SIMPLE_DEV_PM_OPS(scs_rtc_pm_ops, scs_rtc_suspend, scs_rtc_resume);
433
434static struct delayed_work sync_work;
435static unsigned int print_tstamp_delay = 30;
436static void sync_timestamp(struct work_struct *work)
437{
438 struct timespec64 ts;
439 struct rtc_time tm;
440
441 ktime_get_real_ts64(&ts);
442 rtc_time64_to_tm(ts.tv_sec - sys_tz.tz_minuteswest * 60, &tm);
443 pr_info("Timestamp is: %02d-%02d %02d:%02d:%02d.%03lu UTC\n",
444 tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min,
445 tm.tm_sec, ts.tv_nsec);
446 /* print timestamp every 30 minutes */
447 schedule_delayed_work(&sync_work, print_tstamp_delay*60*HZ);
448}
449
450#ifdef CONFIG_SYSFS
451static ssize_t tstamp_show_delay(struct device *dev,
452 struct device_attribute *attr, char *buf)
453{
454 return snprintf(buf, PAGE_SIZE, "%u\n", print_tstamp_delay);
455}
456static ssize_t tstamp_store_delay(struct device *dev,
457 struct device_attribute *attr,
458 const char *buf, size_t size)
459{
460 char *end;
461 unsigned long new = simple_strtoul(buf, &end, 0);
462 if (end == buf)
463 return -EINVAL;
464 print_tstamp_delay = new;
465 cancel_delayed_work(&sync_work);
466 schedule_delayed_work(&sync_work, print_tstamp_delay*60*HZ);
467 return size;
468}
469static DEVICE_ATTR(tstamp_delay, 0664, tstamp_show_delay, tstamp_store_delay);
470
471static int add_tstamp_delay(struct device *dev)
472{
473 return device_create_file(dev, &dev_attr_tstamp_delay);
474}
475
476static void remove_tstamp_delay(struct device *dev)
477{
478 device_remove_file(dev, &dev_attr_tstamp_delay);
479}
480
481#else
482#define add_tstamp_delay(dev) 0
483#define remove_tstamp_delay(dev) do {} while (0)
484#endif /* CONFIG_SYSFS */
485
486static int scs_rtc_probe(struct platform_device *pdev)
487{
488 struct pm80x_chip *chip = dev_get_drvdata(pdev->dev.parent);
489 struct pm80x_rtc_pdata *pdata = NULL;
490 struct scs_rtc_info *info;
491 struct rtc_time tm;
492 int ret;
493
494 if (cpu_is_asr1803_z1())
495 return -ENODEV;
496
497 INIT_DEFERRABLE_WORK(&sync_work, sync_timestamp);
498 schedule_delayed_work(&sync_work, 1*60*HZ);
499 add_tstamp_delay(&pdev->dev);
500
501 pdata = pdev->dev.platform_data;
502 if (IS_ENABLED(CONFIG_OF)) {
503 if (!pdata) {
504 pdata = devm_kzalloc(&pdev->dev,
505 sizeof(*pdata), GFP_KERNEL);
506 if (!pdata)
507 return -ENOMEM;
508 }
509 } else if (!pdata) {
510 return -EINVAL;
511 }
512
513 info =
514 devm_kzalloc(&pdev->dev, sizeof(struct scs_rtc_info), GFP_KERNEL);
515 if (!info)
516 return -ENOMEM;
517 info->irq = platform_get_irq(pdev, 0);
518 if (info->irq < 0) {
519 dev_err(&pdev->dev, "No IRQ resource!\n");
520 ret = -EINVAL;
521 goto out;
522 }
523
524 info->chip = chip;
525 info->map = chip->regmap;
526 if (!info->map) {
527 dev_err(&pdev->dev, "no regmap!\n");
528 ret = -EINVAL;
529 goto out;
530 }
531 if (cpu_is_asr1903())
532 info->base = ASR1903_SCS_RTC_VIRT_BASE;
533 else
534 info->base = SCS_RTC_VIRT_BASE;
535 spin_lock_init(&info->lock);
536
537 info->dev = &pdev->dev;
538 dev_set_drvdata(&pdev->dev, info);
539
540 scs_rtc_alarm_clear(&pdev->dev);
541 scs_rtc_update_trim(&pdev->dev);
542 scs_rtc_alarm_irq_enable(&pdev->dev, 0);
543 ret = scs_rtc_read_time(&pdev->dev, &tm);
544 if (ret < 0) {
545 dev_err(&pdev->dev, "Failed to read initial time.\n");
546 goto out;
547 }
548
549 if ((tm.tm_year < 70) || (tm.tm_year > 1157)) {
550 tm.tm_year = 70;
551 tm.tm_mon = 0;
552 tm.tm_mday = 1;
553 tm.tm_hour = 0;
554 tm.tm_min = 0;
555 tm.tm_sec = 0;
556 ret = scs_rtc_set_time(&pdev->dev, &tm);
557 if (ret < 0) {
558 dev_err(&pdev->dev, "Failed to set initial time.\n");
559 goto out;
560 }
561 }
562
563 info->sync = NULL; /*initialize*/
564#ifdef CONFIG_RTC_DRV_SA1100
565 info->sync = sync_time_to_soc;
566 INIT_DELAYED_WORK(&info->sa1100_sync_work, sa1100_sync_fn);
567 schedule_delayed_work(&info->sa1100_sync_work, 2 * HZ);
568#endif
569
570 dev_info(&pdev->dev, "%d-%d-%d-->%d: %d: %d\n",
571 tm.tm_year, tm.tm_mon, tm.tm_mday,
572 tm.tm_hour, tm.tm_min, tm.tm_sec);
573
574 info->rtc_dev = devm_rtc_device_register(&pdev->dev, "scs-rtc",
575 &scs_rtc_ops, THIS_MODULE);
576 if (IS_ERR(info->rtc_dev)) {
577 ret = PTR_ERR(info->rtc_dev);
578 dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
579 goto out;
580 }
581
582 /* remeber whether this power up is caused by PMIC RTC or not. */
583 info->rtc_dev->dev.platform_data = &pdata->rtc_wakeup;
584 ret = pm80x_request_irq(chip, info->irq, rtc_update_handler,
585 IRQF_ONESHOT | IRQF_NO_SUSPEND , "rtc", info);
586 if (ret < 0) {
587 dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
588 info->irq, ret);
589 goto out;
590 }
591
592 BUG_ON((CHIP_PM801 == info->chip->type) || (CHIP_PM800 == info->chip->type));
593
594 device_init_wakeup(&pdev->dev, 1);
595
596 return 0;
597out:
598 return ret;
599}
600
601static int scs_rtc_remove(struct platform_device *pdev)
602{
603 struct scs_rtc_info *info = platform_get_drvdata(pdev);
604
605 platform_set_drvdata(pdev, NULL);
606 pm80x_free_irq(info->chip, info->irq, info);
607 remove_tstamp_delay(&pdev->dev);
608 return 0;
609}
610
611static void scs_rtc_shutdown(struct platform_device *pdev)
612{
613 struct timespec64 now;
614 struct rtc_time tm;
615 struct scs_rtc_info *info = platform_get_drvdata(pdev);
616
617 /* sync timekeeping time to pmic rtc */
618 ktime_get_real_ts64(&now);
619 if (now.tv_nsec < (NSEC_PER_SEC >> 1))
620 rtc_time64_to_tm(now.tv_sec, &tm);
621 else
622 rtc_time64_to_tm(now.tv_sec + 1, &tm);
623 scs_rtc_set_time(info->dev, &tm);
624 pm80x_free_irq(info->chip, info->irq, info);
625}
626
627static struct platform_driver scs_rtc_driver = {
628 .driver = {
629 .name = "scs-rtc",
630 .owner = THIS_MODULE,
631 .pm = &scs_rtc_pm_ops,
632 },
633 .probe = scs_rtc_probe,
634 .remove = scs_rtc_remove,
635 .shutdown = scs_rtc_shutdown,
636};
637
638static int __init scs_rtc_init(void)
639{
640 return platform_driver_register(&scs_rtc_driver);
641}
642module_init(scs_rtc_init);
643
644static void __exit scs_rtc_exit(void)
645{
646 platform_driver_unregister(&scs_rtc_driver);
647}
648module_exit(scs_rtc_exit);
649
650MODULE_LICENSE("GPL");
651MODULE_DESCRIPTION("ASR PM803 and SCS RTC driver");