1 // SPDX-License-Identifier: GPL-2.0+
3 // RTC driver for Maxim MAX77686 and MAX77802
5 // Copyright (C) 2012 Samsung Electronics Co.Ltd
7 // based on rtc-max8997.c
10 #include <linux/slab.h>
11 #include <linux/rtc.h>
12 #include <linux/delay.h>
13 #include <linux/mutex.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/mfd/max77686-private.h>
17 #include <linux/irqdomain.h>
18 #include <linux/regmap.h>
20 #define MAX77686_I2C_ADDR_RTC (0x0C >> 1)
21 #define MAX77620_I2C_ADDR_RTC 0x68
22 #define MAX77686_INVALID_I2C_ADDR (-1)
24 /* Define non existing register */
25 #define MAX77686_INVALID_REG (-1)
27 /* RTC Control Register */
28 #define BCD_EN_SHIFT 0
29 #define BCD_EN_MASK BIT(BCD_EN_SHIFT)
30 #define MODEL24_SHIFT 1
31 #define MODEL24_MASK BIT(MODEL24_SHIFT)
32 /* RTC Update Register1 */
33 #define RTC_UDR_SHIFT 0
34 #define RTC_UDR_MASK BIT(RTC_UDR_SHIFT)
35 #define RTC_RBUDR_SHIFT 4
36 #define RTC_RBUDR_MASK BIT(RTC_RBUDR_SHIFT)
37 /* RTC Hour register */
38 #define HOUR_PM_SHIFT 6
39 #define HOUR_PM_MASK BIT(HOUR_PM_SHIFT)
40 /* RTC Alarm Enable */
41 #define ALARM_ENABLE_SHIFT 7
42 #define ALARM_ENABLE_MASK BIT(ALARM_ENABLE_SHIFT)
44 #define REG_RTC_NONE 0xdeadbeef
47 * MAX77802 has separate register (RTCAE1) for alarm enable instead
48 * using 1 bit from registers RTC{SEC,MIN,HOUR,DAY,MONTH,YEAR,DATE}
49 * as in done in MAX77686.
51 #define MAX77802_ALARM_ENABLE_VALUE 0x77
64 struct max77686_rtc_driver_data {
65 /* Minimum usecs needed for a RTC update */
67 /* Mask used to read RTC registers value */
69 /* Registers offset to I2C addresses map */
70 const unsigned int *map;
71 /* Has a separate alarm enable register? */
72 bool alarm_enable_reg;
73 /* I2C address for RTC block */
75 /* RTC interrupt via platform resource */
76 bool rtc_irq_from_platform;
77 /* Pending alarm status register */
78 int alarm_pending_status_reg;
79 /* RTC IRQ CHIP for regmap */
80 const struct regmap_irq_chip *rtc_irq_chip;
81 /* regmap configuration for the chip */
82 const struct regmap_config *regmap_config;
85 struct max77686_rtc_info {
87 struct i2c_client *rtc;
88 struct rtc_device *rtc_dev;
91 struct regmap *regmap;
92 struct regmap *rtc_regmap;
94 const struct max77686_rtc_driver_data *drv_data;
95 struct regmap_irq_chip_data *rtc_irq_data;
102 enum MAX77686_RTC_OP {
107 /* These are not registers but just offsets that are mapped to addresses */
108 enum max77686_rtc_reg_offset {
109 REG_RTC_CONTROLM = 0,
138 /* Maps RTC registers offset to the MAX77686 register addresses */
139 static const unsigned int max77686_map[REG_RTC_END] = {
140 [REG_RTC_CONTROLM] = MAX77686_RTC_CONTROLM,
141 [REG_RTC_CONTROL] = MAX77686_RTC_CONTROL,
142 [REG_RTC_UPDATE0] = MAX77686_RTC_UPDATE0,
143 [REG_WTSR_SMPL_CNTL] = MAX77686_WTSR_SMPL_CNTL,
144 [REG_RTC_SEC] = MAX77686_RTC_SEC,
145 [REG_RTC_MIN] = MAX77686_RTC_MIN,
146 [REG_RTC_HOUR] = MAX77686_RTC_HOUR,
147 [REG_RTC_WEEKDAY] = MAX77686_RTC_WEEKDAY,
148 [REG_RTC_MONTH] = MAX77686_RTC_MONTH,
149 [REG_RTC_YEAR] = MAX77686_RTC_YEAR,
150 [REG_RTC_DATE] = MAX77686_RTC_DATE,
151 [REG_ALARM1_SEC] = MAX77686_ALARM1_SEC,
152 [REG_ALARM1_MIN] = MAX77686_ALARM1_MIN,
153 [REG_ALARM1_HOUR] = MAX77686_ALARM1_HOUR,
154 [REG_ALARM1_WEEKDAY] = MAX77686_ALARM1_WEEKDAY,
155 [REG_ALARM1_MONTH] = MAX77686_ALARM1_MONTH,
156 [REG_ALARM1_YEAR] = MAX77686_ALARM1_YEAR,
157 [REG_ALARM1_DATE] = MAX77686_ALARM1_DATE,
158 [REG_ALARM2_SEC] = MAX77686_ALARM2_SEC,
159 [REG_ALARM2_MIN] = MAX77686_ALARM2_MIN,
160 [REG_ALARM2_HOUR] = MAX77686_ALARM2_HOUR,
161 [REG_ALARM2_WEEKDAY] = MAX77686_ALARM2_WEEKDAY,
162 [REG_ALARM2_MONTH] = MAX77686_ALARM2_MONTH,
163 [REG_ALARM2_YEAR] = MAX77686_ALARM2_YEAR,
164 [REG_ALARM2_DATE] = MAX77686_ALARM2_DATE,
165 [REG_RTC_AE1] = REG_RTC_NONE,
168 static const struct regmap_irq max77686_rtc_irqs[] = {
170 REGMAP_IRQ_REG(0, 0, MAX77686_RTCINT_RTC60S_MSK),
171 REGMAP_IRQ_REG(1, 0, MAX77686_RTCINT_RTCA1_MSK),
172 REGMAP_IRQ_REG(2, 0, MAX77686_RTCINT_RTCA2_MSK),
173 REGMAP_IRQ_REG(3, 0, MAX77686_RTCINT_SMPL_MSK),
174 REGMAP_IRQ_REG(4, 0, MAX77686_RTCINT_RTC1S_MSK),
175 REGMAP_IRQ_REG(5, 0, MAX77686_RTCINT_WTSR_MSK),
178 static const struct regmap_irq_chip max77686_rtc_irq_chip = {
179 .name = "max77686-rtc",
180 .status_base = MAX77686_RTC_INT,
181 .mask_base = MAX77686_RTC_INTM,
183 .irqs = max77686_rtc_irqs,
184 .num_irqs = ARRAY_SIZE(max77686_rtc_irqs),
187 static const struct regmap_config max77686_rtc_regmap_config = {
192 static const struct max77686_rtc_driver_data max77686_drv_data = {
196 .alarm_enable_reg = false,
197 .rtc_irq_from_platform = false,
198 .alarm_pending_status_reg = MAX77686_REG_STATUS2,
199 .rtc_i2c_addr = MAX77686_I2C_ADDR_RTC,
200 .rtc_irq_chip = &max77686_rtc_irq_chip,
201 .regmap_config = &max77686_rtc_regmap_config,
204 static const struct regmap_config max77620_rtc_regmap_config = {
207 .use_single_write = true,
210 static const struct max77686_rtc_driver_data max77620_drv_data = {
214 .alarm_enable_reg = false,
215 .rtc_irq_from_platform = true,
216 .alarm_pending_status_reg = MAX77686_INVALID_REG,
217 .rtc_i2c_addr = MAX77620_I2C_ADDR_RTC,
218 .rtc_irq_chip = &max77686_rtc_irq_chip,
219 .regmap_config = &max77620_rtc_regmap_config,
222 static const unsigned int max77802_map[REG_RTC_END] = {
223 [REG_RTC_CONTROLM] = MAX77802_RTC_CONTROLM,
224 [REG_RTC_CONTROL] = MAX77802_RTC_CONTROL,
225 [REG_RTC_UPDATE0] = MAX77802_RTC_UPDATE0,
226 [REG_WTSR_SMPL_CNTL] = MAX77802_WTSR_SMPL_CNTL,
227 [REG_RTC_SEC] = MAX77802_RTC_SEC,
228 [REG_RTC_MIN] = MAX77802_RTC_MIN,
229 [REG_RTC_HOUR] = MAX77802_RTC_HOUR,
230 [REG_RTC_WEEKDAY] = MAX77802_RTC_WEEKDAY,
231 [REG_RTC_MONTH] = MAX77802_RTC_MONTH,
232 [REG_RTC_YEAR] = MAX77802_RTC_YEAR,
233 [REG_RTC_DATE] = MAX77802_RTC_DATE,
234 [REG_ALARM1_SEC] = MAX77802_ALARM1_SEC,
235 [REG_ALARM1_MIN] = MAX77802_ALARM1_MIN,
236 [REG_ALARM1_HOUR] = MAX77802_ALARM1_HOUR,
237 [REG_ALARM1_WEEKDAY] = MAX77802_ALARM1_WEEKDAY,
238 [REG_ALARM1_MONTH] = MAX77802_ALARM1_MONTH,
239 [REG_ALARM1_YEAR] = MAX77802_ALARM1_YEAR,
240 [REG_ALARM1_DATE] = MAX77802_ALARM1_DATE,
241 [REG_ALARM2_SEC] = MAX77802_ALARM2_SEC,
242 [REG_ALARM2_MIN] = MAX77802_ALARM2_MIN,
243 [REG_ALARM2_HOUR] = MAX77802_ALARM2_HOUR,
244 [REG_ALARM2_WEEKDAY] = MAX77802_ALARM2_WEEKDAY,
245 [REG_ALARM2_MONTH] = MAX77802_ALARM2_MONTH,
246 [REG_ALARM2_YEAR] = MAX77802_ALARM2_YEAR,
247 [REG_ALARM2_DATE] = MAX77802_ALARM2_DATE,
248 [REG_RTC_AE1] = MAX77802_RTC_AE1,
251 static const struct regmap_irq_chip max77802_rtc_irq_chip = {
252 .name = "max77802-rtc",
253 .status_base = MAX77802_RTC_INT,
254 .mask_base = MAX77802_RTC_INTM,
256 .irqs = max77686_rtc_irqs, /* same masks as 77686 */
257 .num_irqs = ARRAY_SIZE(max77686_rtc_irqs),
260 static const struct max77686_rtc_driver_data max77802_drv_data = {
264 .alarm_enable_reg = true,
265 .rtc_irq_from_platform = false,
266 .alarm_pending_status_reg = MAX77686_REG_STATUS2,
267 .rtc_i2c_addr = MAX77686_INVALID_I2C_ADDR,
268 .rtc_irq_chip = &max77802_rtc_irq_chip,
271 static void max77686_rtc_data_to_tm(u8 *data, struct rtc_time *tm,
272 struct max77686_rtc_info *info)
274 u8 mask = info->drv_data->mask;
276 tm->tm_sec = data[RTC_SEC] & mask;
277 tm->tm_min = data[RTC_MIN] & mask;
278 if (info->rtc_24hr_mode) {
279 tm->tm_hour = data[RTC_HOUR] & 0x1f;
281 tm->tm_hour = data[RTC_HOUR] & 0x0f;
282 if (data[RTC_HOUR] & HOUR_PM_MASK)
286 /* Only a single bit is set in data[], so fls() would be equivalent */
287 tm->tm_wday = ffs(data[RTC_WEEKDAY] & mask) - 1;
288 tm->tm_mday = data[RTC_DATE] & 0x1f;
289 tm->tm_mon = (data[RTC_MONTH] & 0x0f) - 1;
290 tm->tm_year = data[RTC_YEAR] & mask;
295 * MAX77686 uses 1 bit from sec/min/hour/etc RTC registers and the
296 * year values are just 0..99 so add 100 to support up to 2099.
298 if (!info->drv_data->alarm_enable_reg)
302 static int max77686_rtc_tm_to_data(struct rtc_time *tm, u8 *data,
303 struct max77686_rtc_info *info)
305 data[RTC_SEC] = tm->tm_sec;
306 data[RTC_MIN] = tm->tm_min;
307 data[RTC_HOUR] = tm->tm_hour;
308 data[RTC_WEEKDAY] = 1 << tm->tm_wday;
309 data[RTC_DATE] = tm->tm_mday;
310 data[RTC_MONTH] = tm->tm_mon + 1;
312 if (info->drv_data->alarm_enable_reg) {
313 data[RTC_YEAR] = tm->tm_year;
317 data[RTC_YEAR] = tm->tm_year > 100 ? (tm->tm_year - 100) : 0;
319 if (tm->tm_year < 100) {
320 dev_err(info->dev, "RTC cannot handle the year %d.\n",
328 static int max77686_rtc_update(struct max77686_rtc_info *info,
329 enum MAX77686_RTC_OP op)
333 unsigned long delay = info->drv_data->delay;
335 if (op == MAX77686_RTC_WRITE)
336 data = 1 << RTC_UDR_SHIFT;
338 data = 1 << RTC_RBUDR_SHIFT;
340 ret = regmap_update_bits(info->rtc_regmap,
341 info->drv_data->map[REG_RTC_UPDATE0],
344 dev_err(info->dev, "Fail to write update reg(ret=%d, data=0x%x)\n",
347 /* Minimum delay required before RTC update. */
348 usleep_range(delay, delay * 2);
354 static int max77686_rtc_read_time(struct device *dev, struct rtc_time *tm)
356 struct max77686_rtc_info *info = dev_get_drvdata(dev);
357 u8 data[RTC_NR_TIME];
360 mutex_lock(&info->lock);
362 ret = max77686_rtc_update(info, MAX77686_RTC_READ);
366 ret = regmap_bulk_read(info->rtc_regmap,
367 info->drv_data->map[REG_RTC_SEC],
368 data, ARRAY_SIZE(data));
370 dev_err(info->dev, "Fail to read time reg(%d)\n", ret);
374 max77686_rtc_data_to_tm(data, tm, info);
377 mutex_unlock(&info->lock);
381 static int max77686_rtc_set_time(struct device *dev, struct rtc_time *tm)
383 struct max77686_rtc_info *info = dev_get_drvdata(dev);
384 u8 data[RTC_NR_TIME];
387 ret = max77686_rtc_tm_to_data(tm, data, info);
391 mutex_lock(&info->lock);
393 ret = regmap_bulk_write(info->rtc_regmap,
394 info->drv_data->map[REG_RTC_SEC],
395 data, ARRAY_SIZE(data));
397 dev_err(info->dev, "Fail to write time reg(%d)\n", ret);
401 ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
404 mutex_unlock(&info->lock);
408 static int max77686_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
410 struct max77686_rtc_info *info = dev_get_drvdata(dev);
411 u8 data[RTC_NR_TIME];
413 const unsigned int *map = info->drv_data->map;
416 mutex_lock(&info->lock);
418 ret = max77686_rtc_update(info, MAX77686_RTC_READ);
422 ret = regmap_bulk_read(info->rtc_regmap, map[REG_ALARM1_SEC],
423 data, ARRAY_SIZE(data));
425 dev_err(info->dev, "Fail to read alarm reg(%d)\n", ret);
429 max77686_rtc_data_to_tm(data, &alrm->time, info);
433 if (info->drv_data->alarm_enable_reg) {
434 if (map[REG_RTC_AE1] == REG_RTC_NONE) {
437 "alarm enable register not set(%d)\n", ret);
441 ret = regmap_read(info->rtc_regmap, map[REG_RTC_AE1], &val);
444 "fail to read alarm enable(%d)\n", ret);
451 for (i = 0; i < ARRAY_SIZE(data); i++) {
452 if (data[i] & ALARM_ENABLE_MASK) {
461 if (info->drv_data->alarm_pending_status_reg == MAX77686_INVALID_REG)
464 ret = regmap_read(info->regmap,
465 info->drv_data->alarm_pending_status_reg, &val);
468 "Fail to read alarm pending status reg(%d)\n", ret);
472 if (val & (1 << 4)) /* RTCA1 */
476 mutex_unlock(&info->lock);
480 static int max77686_rtc_stop_alarm(struct max77686_rtc_info *info)
482 u8 data[RTC_NR_TIME];
485 const unsigned int *map = info->drv_data->map;
487 if (!mutex_is_locked(&info->lock))
488 dev_warn(info->dev, "%s: should have mutex locked\n", __func__);
490 ret = max77686_rtc_update(info, MAX77686_RTC_READ);
494 if (info->drv_data->alarm_enable_reg) {
495 if (map[REG_RTC_AE1] == REG_RTC_NONE) {
498 "alarm enable register not set(%d)\n", ret);
502 ret = regmap_write(info->rtc_regmap, map[REG_RTC_AE1], 0);
504 ret = regmap_bulk_read(info->rtc_regmap, map[REG_ALARM1_SEC],
505 data, ARRAY_SIZE(data));
507 dev_err(info->dev, "Fail to read alarm reg(%d)\n", ret);
511 max77686_rtc_data_to_tm(data, &tm, info);
513 for (i = 0; i < ARRAY_SIZE(data); i++)
514 data[i] &= ~ALARM_ENABLE_MASK;
516 ret = regmap_bulk_write(info->rtc_regmap, map[REG_ALARM1_SEC],
517 data, ARRAY_SIZE(data));
521 dev_err(info->dev, "Fail to write alarm reg(%d)\n", ret);
525 ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
530 static int max77686_rtc_start_alarm(struct max77686_rtc_info *info)
532 u8 data[RTC_NR_TIME];
535 const unsigned int *map = info->drv_data->map;
537 if (!mutex_is_locked(&info->lock))
538 dev_warn(info->dev, "%s: should have mutex locked\n", __func__);
540 ret = max77686_rtc_update(info, MAX77686_RTC_READ);
544 if (info->drv_data->alarm_enable_reg) {
545 ret = regmap_write(info->rtc_regmap, map[REG_RTC_AE1],
546 MAX77802_ALARM_ENABLE_VALUE);
548 ret = regmap_bulk_read(info->rtc_regmap, map[REG_ALARM1_SEC],
549 data, ARRAY_SIZE(data));
551 dev_err(info->dev, "Fail to read alarm reg(%d)\n", ret);
555 max77686_rtc_data_to_tm(data, &tm, info);
557 data[RTC_SEC] |= (1 << ALARM_ENABLE_SHIFT);
558 data[RTC_MIN] |= (1 << ALARM_ENABLE_SHIFT);
559 data[RTC_HOUR] |= (1 << ALARM_ENABLE_SHIFT);
560 data[RTC_WEEKDAY] &= ~ALARM_ENABLE_MASK;
561 if (data[RTC_MONTH] & 0xf)
562 data[RTC_MONTH] |= (1 << ALARM_ENABLE_SHIFT);
563 if (data[RTC_YEAR] & info->drv_data->mask)
564 data[RTC_YEAR] |= (1 << ALARM_ENABLE_SHIFT);
565 if (data[RTC_DATE] & 0x1f)
566 data[RTC_DATE] |= (1 << ALARM_ENABLE_SHIFT);
568 ret = regmap_bulk_write(info->rtc_regmap, map[REG_ALARM1_SEC],
569 data, ARRAY_SIZE(data));
573 dev_err(info->dev, "Fail to write alarm reg(%d)\n", ret);
577 ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
582 static int max77686_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
584 struct max77686_rtc_info *info = dev_get_drvdata(dev);
585 u8 data[RTC_NR_TIME];
588 ret = max77686_rtc_tm_to_data(&alrm->time, data, info);
592 mutex_lock(&info->lock);
594 ret = max77686_rtc_stop_alarm(info);
598 ret = regmap_bulk_write(info->rtc_regmap,
599 info->drv_data->map[REG_ALARM1_SEC],
600 data, ARRAY_SIZE(data));
603 dev_err(info->dev, "Fail to write alarm reg(%d)\n", ret);
607 ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
612 ret = max77686_rtc_start_alarm(info);
614 mutex_unlock(&info->lock);
618 static int max77686_rtc_alarm_irq_enable(struct device *dev,
619 unsigned int enabled)
621 struct max77686_rtc_info *info = dev_get_drvdata(dev);
624 mutex_lock(&info->lock);
626 ret = max77686_rtc_start_alarm(info);
628 ret = max77686_rtc_stop_alarm(info);
629 mutex_unlock(&info->lock);
634 static irqreturn_t max77686_rtc_alarm_irq(int irq, void *data)
636 struct max77686_rtc_info *info = data;
638 dev_dbg(info->dev, "RTC alarm IRQ: %d\n", irq);
640 rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF);
645 static const struct rtc_class_ops max77686_rtc_ops = {
646 .read_time = max77686_rtc_read_time,
647 .set_time = max77686_rtc_set_time,
648 .read_alarm = max77686_rtc_read_alarm,
649 .set_alarm = max77686_rtc_set_alarm,
650 .alarm_irq_enable = max77686_rtc_alarm_irq_enable,
653 static int max77686_rtc_init_reg(struct max77686_rtc_info *info)
658 /* Set RTC control register : Binary mode, 24hour mdoe */
659 data[0] = (1 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
660 data[1] = (0 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
662 info->rtc_24hr_mode = 1;
664 ret = regmap_bulk_write(info->rtc_regmap,
665 info->drv_data->map[REG_RTC_CONTROLM],
666 data, ARRAY_SIZE(data));
668 dev_err(info->dev, "Fail to write controlm reg(%d)\n", ret);
672 ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
676 static int max77686_init_rtc_regmap(struct max77686_rtc_info *info)
678 struct device *parent = info->dev->parent;
679 struct i2c_client *parent_i2c = to_i2c_client(parent);
682 if (info->drv_data->rtc_irq_from_platform) {
683 struct platform_device *pdev = to_platform_device(info->dev);
685 info->rtc_irq = platform_get_irq(pdev, 0);
686 if (info->rtc_irq < 0)
687 return info->rtc_irq;
689 info->rtc_irq = parent_i2c->irq;
692 info->regmap = dev_get_regmap(parent, NULL);
694 dev_err(info->dev, "Failed to get rtc regmap\n");
698 if (info->drv_data->rtc_i2c_addr == MAX77686_INVALID_I2C_ADDR) {
699 info->rtc_regmap = info->regmap;
703 info->rtc = devm_i2c_new_dummy_device(info->dev, parent_i2c->adapter,
704 info->drv_data->rtc_i2c_addr);
705 if (IS_ERR(info->rtc)) {
706 dev_err(info->dev, "Failed to allocate I2C device for RTC\n");
707 return PTR_ERR(info->rtc);
710 info->rtc_regmap = devm_regmap_init_i2c(info->rtc,
711 info->drv_data->regmap_config);
712 if (IS_ERR(info->rtc_regmap)) {
713 ret = PTR_ERR(info->rtc_regmap);
714 dev_err(info->dev, "Failed to allocate RTC regmap: %d\n", ret);
719 ret = regmap_add_irq_chip(info->rtc_regmap, info->rtc_irq,
720 IRQF_ONESHOT | IRQF_SHARED,
721 0, info->drv_data->rtc_irq_chip,
722 &info->rtc_irq_data);
724 dev_err(info->dev, "Failed to add RTC irq chip: %d\n", ret);
731 static int max77686_rtc_probe(struct platform_device *pdev)
733 struct max77686_rtc_info *info;
734 const struct platform_device_id *id = platform_get_device_id(pdev);
737 info = devm_kzalloc(&pdev->dev, sizeof(struct max77686_rtc_info),
742 mutex_init(&info->lock);
743 info->dev = &pdev->dev;
744 info->drv_data = (const struct max77686_rtc_driver_data *)
747 ret = max77686_init_rtc_regmap(info);
751 platform_set_drvdata(pdev, info);
753 ret = max77686_rtc_init_reg(info);
755 dev_err(&pdev->dev, "Failed to initialize RTC reg:%d\n", ret);
759 device_init_wakeup(&pdev->dev, 1);
761 info->rtc_dev = devm_rtc_device_register(&pdev->dev, id->name,
762 &max77686_rtc_ops, THIS_MODULE);
764 if (IS_ERR(info->rtc_dev)) {
765 ret = PTR_ERR(info->rtc_dev);
766 dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
772 info->virq = regmap_irq_get_virq(info->rtc_irq_data,
773 MAX77686_RTCIRQ_RTCA1);
774 if (info->virq <= 0) {
779 ret = request_threaded_irq(info->virq, NULL, max77686_rtc_alarm_irq, 0,
782 dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n",
790 regmap_del_irq_chip(info->rtc_irq, info->rtc_irq_data);
795 static int max77686_rtc_remove(struct platform_device *pdev)
797 struct max77686_rtc_info *info = platform_get_drvdata(pdev);
799 free_irq(info->virq, info);
800 regmap_del_irq_chip(info->rtc_irq, info->rtc_irq_data);
805 #ifdef CONFIG_PM_SLEEP
806 static int max77686_rtc_suspend(struct device *dev)
808 struct max77686_rtc_info *info = dev_get_drvdata(dev);
811 if (device_may_wakeup(dev)) {
812 struct max77686_rtc_info *info = dev_get_drvdata(dev);
814 ret = enable_irq_wake(info->virq);
818 * If the main IRQ (not virtual) is the parent IRQ, then it must be
819 * disabled during suspend because if it happens while suspended it
820 * will be handled before resuming I2C.
822 * Since Main IRQ is shared, all its users should disable it to be sure
823 * it won't fire while one of them is still suspended.
825 if (!info->drv_data->rtc_irq_from_platform)
826 disable_irq(info->rtc_irq);
831 static int max77686_rtc_resume(struct device *dev)
833 struct max77686_rtc_info *info = dev_get_drvdata(dev);
835 if (!info->drv_data->rtc_irq_from_platform)
836 enable_irq(info->rtc_irq);
838 if (device_may_wakeup(dev)) {
839 struct max77686_rtc_info *info = dev_get_drvdata(dev);
841 return disable_irq_wake(info->virq);
848 static SIMPLE_DEV_PM_OPS(max77686_rtc_pm_ops,
849 max77686_rtc_suspend, max77686_rtc_resume);
851 static const struct platform_device_id rtc_id[] = {
852 { "max77686-rtc", .driver_data = (kernel_ulong_t)&max77686_drv_data, },
853 { "max77802-rtc", .driver_data = (kernel_ulong_t)&max77802_drv_data, },
854 { "max77620-rtc", .driver_data = (kernel_ulong_t)&max77620_drv_data, },
857 MODULE_DEVICE_TABLE(platform, rtc_id);
859 static struct platform_driver max77686_rtc_driver = {
861 .name = "max77686-rtc",
862 .pm = &max77686_rtc_pm_ops,
864 .probe = max77686_rtc_probe,
865 .remove = max77686_rtc_remove,
869 module_platform_driver(max77686_rtc_driver);
871 MODULE_DESCRIPTION("Maxim MAX77686 RTC driver");
872 MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>");
873 MODULE_LICENSE("GPL");