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
;
83 struct max77686_rtc_info
{
85 struct i2c_client
*rtc
;
86 struct rtc_device
*rtc_dev
;
89 struct regmap
*regmap
;
90 struct regmap
*rtc_regmap
;
92 const struct max77686_rtc_driver_data
*drv_data
;
93 struct regmap_irq_chip_data
*rtc_irq_data
;
100 enum MAX77686_RTC_OP
{
105 /* These are not registers but just offsets that are mapped to addresses */
106 enum max77686_rtc_reg_offset
{
107 REG_RTC_CONTROLM
= 0,
136 /* Maps RTC registers offset to the MAX77686 register addresses */
137 static const unsigned int max77686_map
[REG_RTC_END
] = {
138 [REG_RTC_CONTROLM
] = MAX77686_RTC_CONTROLM
,
139 [REG_RTC_CONTROL
] = MAX77686_RTC_CONTROL
,
140 [REG_RTC_UPDATE0
] = MAX77686_RTC_UPDATE0
,
141 [REG_WTSR_SMPL_CNTL
] = MAX77686_WTSR_SMPL_CNTL
,
142 [REG_RTC_SEC
] = MAX77686_RTC_SEC
,
143 [REG_RTC_MIN
] = MAX77686_RTC_MIN
,
144 [REG_RTC_HOUR
] = MAX77686_RTC_HOUR
,
145 [REG_RTC_WEEKDAY
] = MAX77686_RTC_WEEKDAY
,
146 [REG_RTC_MONTH
] = MAX77686_RTC_MONTH
,
147 [REG_RTC_YEAR
] = MAX77686_RTC_YEAR
,
148 [REG_RTC_DATE
] = MAX77686_RTC_DATE
,
149 [REG_ALARM1_SEC
] = MAX77686_ALARM1_SEC
,
150 [REG_ALARM1_MIN
] = MAX77686_ALARM1_MIN
,
151 [REG_ALARM1_HOUR
] = MAX77686_ALARM1_HOUR
,
152 [REG_ALARM1_WEEKDAY
] = MAX77686_ALARM1_WEEKDAY
,
153 [REG_ALARM1_MONTH
] = MAX77686_ALARM1_MONTH
,
154 [REG_ALARM1_YEAR
] = MAX77686_ALARM1_YEAR
,
155 [REG_ALARM1_DATE
] = MAX77686_ALARM1_DATE
,
156 [REG_ALARM2_SEC
] = MAX77686_ALARM2_SEC
,
157 [REG_ALARM2_MIN
] = MAX77686_ALARM2_MIN
,
158 [REG_ALARM2_HOUR
] = MAX77686_ALARM2_HOUR
,
159 [REG_ALARM2_WEEKDAY
] = MAX77686_ALARM2_WEEKDAY
,
160 [REG_ALARM2_MONTH
] = MAX77686_ALARM2_MONTH
,
161 [REG_ALARM2_YEAR
] = MAX77686_ALARM2_YEAR
,
162 [REG_ALARM2_DATE
] = MAX77686_ALARM2_DATE
,
163 [REG_RTC_AE1
] = REG_RTC_NONE
,
166 static const struct regmap_irq max77686_rtc_irqs
[] = {
168 REGMAP_IRQ_REG(0, 0, MAX77686_RTCINT_RTC60S_MSK
),
169 REGMAP_IRQ_REG(1, 0, MAX77686_RTCINT_RTCA1_MSK
),
170 REGMAP_IRQ_REG(2, 0, MAX77686_RTCINT_RTCA2_MSK
),
171 REGMAP_IRQ_REG(3, 0, MAX77686_RTCINT_SMPL_MSK
),
172 REGMAP_IRQ_REG(4, 0, MAX77686_RTCINT_RTC1S_MSK
),
173 REGMAP_IRQ_REG(5, 0, MAX77686_RTCINT_WTSR_MSK
),
176 static const struct regmap_irq_chip max77686_rtc_irq_chip
= {
177 .name
= "max77686-rtc",
178 .status_base
= MAX77686_RTC_INT
,
179 .mask_base
= MAX77686_RTC_INTM
,
181 .irqs
= max77686_rtc_irqs
,
182 .num_irqs
= ARRAY_SIZE(max77686_rtc_irqs
),
185 static const struct max77686_rtc_driver_data max77686_drv_data
= {
189 .alarm_enable_reg
= false,
190 .rtc_irq_from_platform
= false,
191 .alarm_pending_status_reg
= MAX77686_REG_STATUS2
,
192 .rtc_i2c_addr
= MAX77686_I2C_ADDR_RTC
,
193 .rtc_irq_chip
= &max77686_rtc_irq_chip
,
196 static const struct max77686_rtc_driver_data max77620_drv_data
= {
200 .alarm_enable_reg
= false,
201 .rtc_irq_from_platform
= true,
202 .alarm_pending_status_reg
= MAX77686_INVALID_REG
,
203 .rtc_i2c_addr
= MAX77620_I2C_ADDR_RTC
,
204 .rtc_irq_chip
= &max77686_rtc_irq_chip
,
207 static const unsigned int max77802_map
[REG_RTC_END
] = {
208 [REG_RTC_CONTROLM
] = MAX77802_RTC_CONTROLM
,
209 [REG_RTC_CONTROL
] = MAX77802_RTC_CONTROL
,
210 [REG_RTC_UPDATE0
] = MAX77802_RTC_UPDATE0
,
211 [REG_WTSR_SMPL_CNTL
] = MAX77802_WTSR_SMPL_CNTL
,
212 [REG_RTC_SEC
] = MAX77802_RTC_SEC
,
213 [REG_RTC_MIN
] = MAX77802_RTC_MIN
,
214 [REG_RTC_HOUR
] = MAX77802_RTC_HOUR
,
215 [REG_RTC_WEEKDAY
] = MAX77802_RTC_WEEKDAY
,
216 [REG_RTC_MONTH
] = MAX77802_RTC_MONTH
,
217 [REG_RTC_YEAR
] = MAX77802_RTC_YEAR
,
218 [REG_RTC_DATE
] = MAX77802_RTC_DATE
,
219 [REG_ALARM1_SEC
] = MAX77802_ALARM1_SEC
,
220 [REG_ALARM1_MIN
] = MAX77802_ALARM1_MIN
,
221 [REG_ALARM1_HOUR
] = MAX77802_ALARM1_HOUR
,
222 [REG_ALARM1_WEEKDAY
] = MAX77802_ALARM1_WEEKDAY
,
223 [REG_ALARM1_MONTH
] = MAX77802_ALARM1_MONTH
,
224 [REG_ALARM1_YEAR
] = MAX77802_ALARM1_YEAR
,
225 [REG_ALARM1_DATE
] = MAX77802_ALARM1_DATE
,
226 [REG_ALARM2_SEC
] = MAX77802_ALARM2_SEC
,
227 [REG_ALARM2_MIN
] = MAX77802_ALARM2_MIN
,
228 [REG_ALARM2_HOUR
] = MAX77802_ALARM2_HOUR
,
229 [REG_ALARM2_WEEKDAY
] = MAX77802_ALARM2_WEEKDAY
,
230 [REG_ALARM2_MONTH
] = MAX77802_ALARM2_MONTH
,
231 [REG_ALARM2_YEAR
] = MAX77802_ALARM2_YEAR
,
232 [REG_ALARM2_DATE
] = MAX77802_ALARM2_DATE
,
233 [REG_RTC_AE1
] = MAX77802_RTC_AE1
,
236 static const struct regmap_irq_chip max77802_rtc_irq_chip
= {
237 .name
= "max77802-rtc",
238 .status_base
= MAX77802_RTC_INT
,
239 .mask_base
= MAX77802_RTC_INTM
,
241 .irqs
= max77686_rtc_irqs
, /* same masks as 77686 */
242 .num_irqs
= ARRAY_SIZE(max77686_rtc_irqs
),
245 static const struct max77686_rtc_driver_data max77802_drv_data
= {
249 .alarm_enable_reg
= true,
250 .rtc_irq_from_platform
= false,
251 .alarm_pending_status_reg
= MAX77686_REG_STATUS2
,
252 .rtc_i2c_addr
= MAX77686_INVALID_I2C_ADDR
,
253 .rtc_irq_chip
= &max77802_rtc_irq_chip
,
256 static void max77686_rtc_data_to_tm(u8
*data
, struct rtc_time
*tm
,
257 struct max77686_rtc_info
*info
)
259 u8 mask
= info
->drv_data
->mask
;
261 tm
->tm_sec
= data
[RTC_SEC
] & mask
;
262 tm
->tm_min
= data
[RTC_MIN
] & mask
;
263 if (info
->rtc_24hr_mode
) {
264 tm
->tm_hour
= data
[RTC_HOUR
] & 0x1f;
266 tm
->tm_hour
= data
[RTC_HOUR
] & 0x0f;
267 if (data
[RTC_HOUR
] & HOUR_PM_MASK
)
271 /* Only a single bit is set in data[], so fls() would be equivalent */
272 tm
->tm_wday
= ffs(data
[RTC_WEEKDAY
] & mask
) - 1;
273 tm
->tm_mday
= data
[RTC_DATE
] & 0x1f;
274 tm
->tm_mon
= (data
[RTC_MONTH
] & 0x0f) - 1;
275 tm
->tm_year
= data
[RTC_YEAR
] & mask
;
280 * MAX77686 uses 1 bit from sec/min/hour/etc RTC registers and the
281 * year values are just 0..99 so add 100 to support up to 2099.
283 if (!info
->drv_data
->alarm_enable_reg
)
287 static int max77686_rtc_tm_to_data(struct rtc_time
*tm
, u8
*data
,
288 struct max77686_rtc_info
*info
)
290 data
[RTC_SEC
] = tm
->tm_sec
;
291 data
[RTC_MIN
] = tm
->tm_min
;
292 data
[RTC_HOUR
] = tm
->tm_hour
;
293 data
[RTC_WEEKDAY
] = 1 << tm
->tm_wday
;
294 data
[RTC_DATE
] = tm
->tm_mday
;
295 data
[RTC_MONTH
] = tm
->tm_mon
+ 1;
297 if (info
->drv_data
->alarm_enable_reg
) {
298 data
[RTC_YEAR
] = tm
->tm_year
;
302 data
[RTC_YEAR
] = tm
->tm_year
> 100 ? (tm
->tm_year
- 100) : 0;
304 if (tm
->tm_year
< 100) {
305 dev_err(info
->dev
, "RTC cannot handle the year %d.\n",
313 static int max77686_rtc_update(struct max77686_rtc_info
*info
,
314 enum MAX77686_RTC_OP op
)
318 unsigned long delay
= info
->drv_data
->delay
;
320 if (op
== MAX77686_RTC_WRITE
)
321 data
= 1 << RTC_UDR_SHIFT
;
323 data
= 1 << RTC_RBUDR_SHIFT
;
325 ret
= regmap_update_bits(info
->rtc_regmap
,
326 info
->drv_data
->map
[REG_RTC_UPDATE0
],
329 dev_err(info
->dev
, "Fail to write update reg(ret=%d, data=0x%x)\n",
332 /* Minimum delay required before RTC update. */
333 usleep_range(delay
, delay
* 2);
339 static int max77686_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
341 struct max77686_rtc_info
*info
= dev_get_drvdata(dev
);
342 u8 data
[RTC_NR_TIME
];
345 mutex_lock(&info
->lock
);
347 ret
= max77686_rtc_update(info
, MAX77686_RTC_READ
);
351 ret
= regmap_bulk_read(info
->rtc_regmap
,
352 info
->drv_data
->map
[REG_RTC_SEC
],
353 data
, ARRAY_SIZE(data
));
355 dev_err(info
->dev
, "Fail to read time reg(%d)\n", ret
);
359 max77686_rtc_data_to_tm(data
, tm
, info
);
362 mutex_unlock(&info
->lock
);
366 static int max77686_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
368 struct max77686_rtc_info
*info
= dev_get_drvdata(dev
);
369 u8 data
[RTC_NR_TIME
];
372 ret
= max77686_rtc_tm_to_data(tm
, data
, info
);
376 mutex_lock(&info
->lock
);
378 ret
= regmap_bulk_write(info
->rtc_regmap
,
379 info
->drv_data
->map
[REG_RTC_SEC
],
380 data
, ARRAY_SIZE(data
));
382 dev_err(info
->dev
, "Fail to write time reg(%d)\n", ret
);
386 ret
= max77686_rtc_update(info
, MAX77686_RTC_WRITE
);
389 mutex_unlock(&info
->lock
);
393 static int max77686_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
395 struct max77686_rtc_info
*info
= dev_get_drvdata(dev
);
396 u8 data
[RTC_NR_TIME
];
398 const unsigned int *map
= info
->drv_data
->map
;
401 mutex_lock(&info
->lock
);
403 ret
= max77686_rtc_update(info
, MAX77686_RTC_READ
);
407 ret
= regmap_bulk_read(info
->rtc_regmap
, map
[REG_ALARM1_SEC
],
408 data
, ARRAY_SIZE(data
));
410 dev_err(info
->dev
, "Fail to read alarm reg(%d)\n", ret
);
414 max77686_rtc_data_to_tm(data
, &alrm
->time
, info
);
418 if (info
->drv_data
->alarm_enable_reg
) {
419 if (map
[REG_RTC_AE1
] == REG_RTC_NONE
) {
422 "alarm enable register not set(%d)\n", ret
);
426 ret
= regmap_read(info
->rtc_regmap
, map
[REG_RTC_AE1
], &val
);
429 "fail to read alarm enable(%d)\n", ret
);
436 for (i
= 0; i
< ARRAY_SIZE(data
); i
++) {
437 if (data
[i
] & ALARM_ENABLE_MASK
) {
446 if (info
->drv_data
->alarm_pending_status_reg
== MAX77686_INVALID_REG
)
449 ret
= regmap_read(info
->regmap
,
450 info
->drv_data
->alarm_pending_status_reg
, &val
);
453 "Fail to read alarm pending status reg(%d)\n", ret
);
457 if (val
& (1 << 4)) /* RTCA1 */
461 mutex_unlock(&info
->lock
);
465 static int max77686_rtc_stop_alarm(struct max77686_rtc_info
*info
)
467 u8 data
[RTC_NR_TIME
];
470 const unsigned int *map
= info
->drv_data
->map
;
472 if (!mutex_is_locked(&info
->lock
))
473 dev_warn(info
->dev
, "%s: should have mutex locked\n", __func__
);
475 ret
= max77686_rtc_update(info
, MAX77686_RTC_READ
);
479 if (info
->drv_data
->alarm_enable_reg
) {
480 if (map
[REG_RTC_AE1
] == REG_RTC_NONE
) {
483 "alarm enable register not set(%d)\n", ret
);
487 ret
= regmap_write(info
->rtc_regmap
, map
[REG_RTC_AE1
], 0);
489 ret
= regmap_bulk_read(info
->rtc_regmap
, map
[REG_ALARM1_SEC
],
490 data
, ARRAY_SIZE(data
));
492 dev_err(info
->dev
, "Fail to read alarm reg(%d)\n", ret
);
496 max77686_rtc_data_to_tm(data
, &tm
, info
);
498 for (i
= 0; i
< ARRAY_SIZE(data
); i
++)
499 data
[i
] &= ~ALARM_ENABLE_MASK
;
501 ret
= regmap_bulk_write(info
->rtc_regmap
, map
[REG_ALARM1_SEC
],
502 data
, ARRAY_SIZE(data
));
506 dev_err(info
->dev
, "Fail to write alarm reg(%d)\n", ret
);
510 ret
= max77686_rtc_update(info
, MAX77686_RTC_WRITE
);
515 static int max77686_rtc_start_alarm(struct max77686_rtc_info
*info
)
517 u8 data
[RTC_NR_TIME
];
520 const unsigned int *map
= info
->drv_data
->map
;
522 if (!mutex_is_locked(&info
->lock
))
523 dev_warn(info
->dev
, "%s: should have mutex locked\n", __func__
);
525 ret
= max77686_rtc_update(info
, MAX77686_RTC_READ
);
529 if (info
->drv_data
->alarm_enable_reg
) {
530 ret
= regmap_write(info
->rtc_regmap
, map
[REG_RTC_AE1
],
531 MAX77802_ALARM_ENABLE_VALUE
);
533 ret
= regmap_bulk_read(info
->rtc_regmap
, map
[REG_ALARM1_SEC
],
534 data
, ARRAY_SIZE(data
));
536 dev_err(info
->dev
, "Fail to read alarm reg(%d)\n", ret
);
540 max77686_rtc_data_to_tm(data
, &tm
, info
);
542 data
[RTC_SEC
] |= (1 << ALARM_ENABLE_SHIFT
);
543 data
[RTC_MIN
] |= (1 << ALARM_ENABLE_SHIFT
);
544 data
[RTC_HOUR
] |= (1 << ALARM_ENABLE_SHIFT
);
545 data
[RTC_WEEKDAY
] &= ~ALARM_ENABLE_MASK
;
546 if (data
[RTC_MONTH
] & 0xf)
547 data
[RTC_MONTH
] |= (1 << ALARM_ENABLE_SHIFT
);
548 if (data
[RTC_YEAR
] & info
->drv_data
->mask
)
549 data
[RTC_YEAR
] |= (1 << ALARM_ENABLE_SHIFT
);
550 if (data
[RTC_DATE
] & 0x1f)
551 data
[RTC_DATE
] |= (1 << ALARM_ENABLE_SHIFT
);
553 ret
= regmap_bulk_write(info
->rtc_regmap
, map
[REG_ALARM1_SEC
],
554 data
, ARRAY_SIZE(data
));
558 dev_err(info
->dev
, "Fail to write alarm reg(%d)\n", ret
);
562 ret
= max77686_rtc_update(info
, MAX77686_RTC_WRITE
);
567 static int max77686_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
569 struct max77686_rtc_info
*info
= dev_get_drvdata(dev
);
570 u8 data
[RTC_NR_TIME
];
573 ret
= max77686_rtc_tm_to_data(&alrm
->time
, data
, info
);
577 mutex_lock(&info
->lock
);
579 ret
= max77686_rtc_stop_alarm(info
);
583 ret
= regmap_bulk_write(info
->rtc_regmap
,
584 info
->drv_data
->map
[REG_ALARM1_SEC
],
585 data
, ARRAY_SIZE(data
));
588 dev_err(info
->dev
, "Fail to write alarm reg(%d)\n", ret
);
592 ret
= max77686_rtc_update(info
, MAX77686_RTC_WRITE
);
597 ret
= max77686_rtc_start_alarm(info
);
599 mutex_unlock(&info
->lock
);
603 static int max77686_rtc_alarm_irq_enable(struct device
*dev
,
604 unsigned int enabled
)
606 struct max77686_rtc_info
*info
= dev_get_drvdata(dev
);
609 mutex_lock(&info
->lock
);
611 ret
= max77686_rtc_start_alarm(info
);
613 ret
= max77686_rtc_stop_alarm(info
);
614 mutex_unlock(&info
->lock
);
619 static irqreturn_t
max77686_rtc_alarm_irq(int irq
, void *data
)
621 struct max77686_rtc_info
*info
= data
;
623 dev_dbg(info
->dev
, "RTC alarm IRQ: %d\n", irq
);
625 rtc_update_irq(info
->rtc_dev
, 1, RTC_IRQF
| RTC_AF
);
630 static const struct rtc_class_ops max77686_rtc_ops
= {
631 .read_time
= max77686_rtc_read_time
,
632 .set_time
= max77686_rtc_set_time
,
633 .read_alarm
= max77686_rtc_read_alarm
,
634 .set_alarm
= max77686_rtc_set_alarm
,
635 .alarm_irq_enable
= max77686_rtc_alarm_irq_enable
,
638 static int max77686_rtc_init_reg(struct max77686_rtc_info
*info
)
643 /* Set RTC control register : Binary mode, 24hour mdoe */
644 data
[0] = (1 << BCD_EN_SHIFT
) | (1 << MODEL24_SHIFT
);
645 data
[1] = (0 << BCD_EN_SHIFT
) | (1 << MODEL24_SHIFT
);
647 info
->rtc_24hr_mode
= 1;
649 ret
= regmap_bulk_write(info
->rtc_regmap
,
650 info
->drv_data
->map
[REG_RTC_CONTROLM
],
651 data
, ARRAY_SIZE(data
));
653 dev_err(info
->dev
, "Fail to write controlm reg(%d)\n", ret
);
657 ret
= max77686_rtc_update(info
, MAX77686_RTC_WRITE
);
661 static const struct regmap_config max77686_rtc_regmap_config
= {
666 static int max77686_init_rtc_regmap(struct max77686_rtc_info
*info
)
668 struct device
*parent
= info
->dev
->parent
;
669 struct i2c_client
*parent_i2c
= to_i2c_client(parent
);
672 if (info
->drv_data
->rtc_irq_from_platform
) {
673 struct platform_device
*pdev
= to_platform_device(info
->dev
);
675 info
->rtc_irq
= platform_get_irq(pdev
, 0);
676 if (info
->rtc_irq
< 0) {
677 dev_err(info
->dev
, "Failed to get rtc interrupts: %d\n",
679 return info
->rtc_irq
;
682 info
->rtc_irq
= parent_i2c
->irq
;
685 info
->regmap
= dev_get_regmap(parent
, NULL
);
687 dev_err(info
->dev
, "Failed to get rtc regmap\n");
691 if (info
->drv_data
->rtc_i2c_addr
== MAX77686_INVALID_I2C_ADDR
) {
692 info
->rtc_regmap
= info
->regmap
;
696 info
->rtc
= i2c_new_dummy(parent_i2c
->adapter
,
697 info
->drv_data
->rtc_i2c_addr
);
699 dev_err(info
->dev
, "Failed to allocate I2C device for RTC\n");
703 info
->rtc_regmap
= devm_regmap_init_i2c(info
->rtc
,
704 &max77686_rtc_regmap_config
);
705 if (IS_ERR(info
->rtc_regmap
)) {
706 ret
= PTR_ERR(info
->rtc_regmap
);
707 dev_err(info
->dev
, "Failed to allocate RTC regmap: %d\n", ret
);
708 goto err_unregister_i2c
;
712 ret
= regmap_add_irq_chip(info
->rtc_regmap
, info
->rtc_irq
,
713 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
|
714 IRQF_SHARED
, 0, info
->drv_data
->rtc_irq_chip
,
715 &info
->rtc_irq_data
);
717 dev_err(info
->dev
, "Failed to add RTC irq chip: %d\n", ret
);
718 goto err_unregister_i2c
;
725 i2c_unregister_device(info
->rtc
);
729 static int max77686_rtc_probe(struct platform_device
*pdev
)
731 struct max77686_rtc_info
*info
;
732 const struct platform_device_id
*id
= platform_get_device_id(pdev
);
735 info
= devm_kzalloc(&pdev
->dev
, sizeof(struct max77686_rtc_info
),
740 mutex_init(&info
->lock
);
741 info
->dev
= &pdev
->dev
;
742 info
->drv_data
= (const struct max77686_rtc_driver_data
*)
745 ret
= max77686_init_rtc_regmap(info
);
749 platform_set_drvdata(pdev
, info
);
751 ret
= max77686_rtc_init_reg(info
);
753 dev_err(&pdev
->dev
, "Failed to initialize RTC reg:%d\n", ret
);
757 device_init_wakeup(&pdev
->dev
, 1);
759 info
->rtc_dev
= devm_rtc_device_register(&pdev
->dev
, id
->name
,
760 &max77686_rtc_ops
, THIS_MODULE
);
762 if (IS_ERR(info
->rtc_dev
)) {
763 ret
= PTR_ERR(info
->rtc_dev
);
764 dev_err(&pdev
->dev
, "Failed to register RTC device: %d\n", ret
);
770 info
->virq
= regmap_irq_get_virq(info
->rtc_irq_data
,
771 MAX77686_RTCIRQ_RTCA1
);
772 if (info
->virq
<= 0) {
777 ret
= request_threaded_irq(info
->virq
, NULL
, max77686_rtc_alarm_irq
, 0,
780 dev_err(&pdev
->dev
, "Failed to request alarm IRQ: %d: %d\n",
788 regmap_del_irq_chip(info
->rtc_irq
, info
->rtc_irq_data
);
790 i2c_unregister_device(info
->rtc
);
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
);
802 i2c_unregister_device(info
->rtc
);
807 #ifdef CONFIG_PM_SLEEP
808 static int max77686_rtc_suspend(struct device
*dev
)
810 if (device_may_wakeup(dev
)) {
811 struct max77686_rtc_info
*info
= dev_get_drvdata(dev
);
813 return enable_irq_wake(info
->virq
);
819 static int max77686_rtc_resume(struct device
*dev
)
821 if (device_may_wakeup(dev
)) {
822 struct max77686_rtc_info
*info
= dev_get_drvdata(dev
);
824 return disable_irq_wake(info
->virq
);
831 static SIMPLE_DEV_PM_OPS(max77686_rtc_pm_ops
,
832 max77686_rtc_suspend
, max77686_rtc_resume
);
834 static const struct platform_device_id rtc_id
[] = {
835 { "max77686-rtc", .driver_data
= (kernel_ulong_t
)&max77686_drv_data
, },
836 { "max77802-rtc", .driver_data
= (kernel_ulong_t
)&max77802_drv_data
, },
837 { "max77620-rtc", .driver_data
= (kernel_ulong_t
)&max77620_drv_data
, },
840 MODULE_DEVICE_TABLE(platform
, rtc_id
);
842 static struct platform_driver max77686_rtc_driver
= {
844 .name
= "max77686-rtc",
845 .pm
= &max77686_rtc_pm_ops
,
847 .probe
= max77686_rtc_probe
,
848 .remove
= max77686_rtc_remove
,
852 module_platform_driver(max77686_rtc_driver
);
854 MODULE_DESCRIPTION("Maxim MAX77686 RTC driver");
855 MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>");
856 MODULE_LICENSE("GPL");