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 MAX77714_I2C_ADDR_RTC 0x48
23 #define MAX77686_INVALID_I2C_ADDR (-1)
25 /* Define non existing register */
26 #define MAX77686_INVALID_REG (-1)
28 /* RTC Control Register */
29 #define BCD_EN_SHIFT 0
30 #define BCD_EN_MASK BIT(BCD_EN_SHIFT)
31 #define MODEL24_SHIFT 1
32 #define MODEL24_MASK BIT(MODEL24_SHIFT)
33 /* RTC Update Register1 */
34 #define RTC_UDR_SHIFT 0
35 #define RTC_UDR_MASK BIT(RTC_UDR_SHIFT)
36 #define RTC_RBUDR_SHIFT 4
37 #define RTC_RBUDR_MASK BIT(RTC_RBUDR_SHIFT)
38 /* RTC Alarm Enable */
39 #define ALARM_ENABLE_SHIFT 7
40 #define ALARM_ENABLE_MASK BIT(ALARM_ENABLE_SHIFT)
42 #define REG_RTC_NONE 0xdeadbeef
45 * MAX77802 has separate register (RTCAE1) for alarm enable instead
46 * using 1 bit from registers RTC{SEC,MIN,HOUR,DAY,MONTH,YEAR,DATE}
47 * as in done in MAX77686.
49 #define MAX77802_ALARM_ENABLE_VALUE 0x77
63 * struct max77686_rtc_driver_data - model-specific configuration
64 * @delay: Minimum usecs needed for a RTC update
65 * @mask: Mask used to read RTC registers value
66 * @map: Registers offset to I2C addresses map
67 * @alarm_enable_reg: Has a separate alarm enable register?
68 * @rtc_i2c_addr: I2C address for RTC block
69 * @rtc_irq_from_platform: RTC interrupt via platform resource
70 * @alarm_pending_status_reg: Pending alarm status register
71 * @rtc_irq_chip: RTC IRQ CHIP for regmap
72 * @regmap_config: regmap configuration for the chip
74 struct max77686_rtc_driver_data
{
77 const unsigned int *map
;
78 bool alarm_enable_reg
;
80 bool rtc_irq_from_platform
;
81 int alarm_pending_status_reg
;
82 const struct regmap_irq_chip
*rtc_irq_chip
;
83 const struct regmap_config
*regmap_config
;
86 struct max77686_rtc_info
{
88 struct i2c_client
*rtc
;
89 struct rtc_device
*rtc_dev
;
92 struct regmap
*regmap
;
93 struct regmap
*rtc_regmap
;
95 const struct max77686_rtc_driver_data
*drv_data
;
96 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_MONTHDAY
] = MAX77686_RTC_MONTHDAY
,
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_irq_chip max77714_rtc_irq_chip
= {
205 .name
= "max77714-rtc",
206 .status_base
= MAX77686_RTC_INT
,
207 .mask_base
= MAX77686_RTC_INTM
,
209 .irqs
= max77686_rtc_irqs
,
210 .num_irqs
= ARRAY_SIZE(max77686_rtc_irqs
) - 1, /* no WTSR on 77714 */
213 static const struct max77686_rtc_driver_data max77714_drv_data
= {
217 .alarm_enable_reg
= false,
218 .rtc_irq_from_platform
= false,
219 /* On MAX77714 RTCA1 is BIT 1 of RTCINT (0x00). Not supported by this driver. */
220 .alarm_pending_status_reg
= MAX77686_INVALID_REG
,
221 .rtc_i2c_addr
= MAX77714_I2C_ADDR_RTC
,
222 .rtc_irq_chip
= &max77714_rtc_irq_chip
,
223 .regmap_config
= &max77686_rtc_regmap_config
,
226 static const struct regmap_config max77620_rtc_regmap_config
= {
229 .use_single_write
= true,
232 static const struct max77686_rtc_driver_data max77620_drv_data
= {
236 .alarm_enable_reg
= false,
237 .rtc_irq_from_platform
= true,
238 .alarm_pending_status_reg
= MAX77686_INVALID_REG
,
239 .rtc_i2c_addr
= MAX77620_I2C_ADDR_RTC
,
240 .rtc_irq_chip
= &max77686_rtc_irq_chip
,
241 .regmap_config
= &max77620_rtc_regmap_config
,
244 static const unsigned int max77802_map
[REG_RTC_END
] = {
245 [REG_RTC_CONTROLM
] = MAX77802_RTC_CONTROLM
,
246 [REG_RTC_CONTROL
] = MAX77802_RTC_CONTROL
,
247 [REG_RTC_UPDATE0
] = MAX77802_RTC_UPDATE0
,
248 [REG_WTSR_SMPL_CNTL
] = MAX77802_WTSR_SMPL_CNTL
,
249 [REG_RTC_SEC
] = MAX77802_RTC_SEC
,
250 [REG_RTC_MIN
] = MAX77802_RTC_MIN
,
251 [REG_RTC_HOUR
] = MAX77802_RTC_HOUR
,
252 [REG_RTC_WEEKDAY
] = MAX77802_RTC_WEEKDAY
,
253 [REG_RTC_MONTH
] = MAX77802_RTC_MONTH
,
254 [REG_RTC_YEAR
] = MAX77802_RTC_YEAR
,
255 [REG_RTC_MONTHDAY
] = MAX77802_RTC_MONTHDAY
,
256 [REG_ALARM1_SEC
] = MAX77802_ALARM1_SEC
,
257 [REG_ALARM1_MIN
] = MAX77802_ALARM1_MIN
,
258 [REG_ALARM1_HOUR
] = MAX77802_ALARM1_HOUR
,
259 [REG_ALARM1_WEEKDAY
] = MAX77802_ALARM1_WEEKDAY
,
260 [REG_ALARM1_MONTH
] = MAX77802_ALARM1_MONTH
,
261 [REG_ALARM1_YEAR
] = MAX77802_ALARM1_YEAR
,
262 [REG_ALARM1_DATE
] = MAX77802_ALARM1_DATE
,
263 [REG_ALARM2_SEC
] = MAX77802_ALARM2_SEC
,
264 [REG_ALARM2_MIN
] = MAX77802_ALARM2_MIN
,
265 [REG_ALARM2_HOUR
] = MAX77802_ALARM2_HOUR
,
266 [REG_ALARM2_WEEKDAY
] = MAX77802_ALARM2_WEEKDAY
,
267 [REG_ALARM2_MONTH
] = MAX77802_ALARM2_MONTH
,
268 [REG_ALARM2_YEAR
] = MAX77802_ALARM2_YEAR
,
269 [REG_ALARM2_DATE
] = MAX77802_ALARM2_DATE
,
270 [REG_RTC_AE1
] = MAX77802_RTC_AE1
,
273 static const struct regmap_irq_chip max77802_rtc_irq_chip
= {
274 .name
= "max77802-rtc",
275 .status_base
= MAX77802_RTC_INT
,
276 .mask_base
= MAX77802_RTC_INTM
,
278 .irqs
= max77686_rtc_irqs
, /* same masks as 77686 */
279 .num_irqs
= ARRAY_SIZE(max77686_rtc_irqs
),
282 static const struct max77686_rtc_driver_data max77802_drv_data
= {
286 .alarm_enable_reg
= true,
287 .rtc_irq_from_platform
= false,
288 .alarm_pending_status_reg
= MAX77686_REG_STATUS2
,
289 .rtc_i2c_addr
= MAX77686_INVALID_I2C_ADDR
,
290 .rtc_irq_chip
= &max77802_rtc_irq_chip
,
293 static void max77686_rtc_data_to_tm(u8
*data
, struct rtc_time
*tm
,
294 struct max77686_rtc_info
*info
)
296 u8 mask
= info
->drv_data
->mask
;
298 tm
->tm_sec
= data
[RTC_SEC
] & mask
;
299 tm
->tm_min
= data
[RTC_MIN
] & mask
;
300 tm
->tm_hour
= data
[RTC_HOUR
] & 0x1f;
302 /* Only a single bit is set in data[], so fls() would be equivalent */
303 tm
->tm_wday
= ffs(data
[RTC_WEEKDAY
] & mask
) - 1;
304 tm
->tm_mday
= data
[RTC_MONTHDAY
] & 0x1f;
305 tm
->tm_mon
= (data
[RTC_MONTH
] & 0x0f) - 1;
306 tm
->tm_year
= data
[RTC_YEAR
] & mask
;
311 * MAX77686 uses 1 bit from sec/min/hour/etc RTC registers and the
312 * year values are just 0..99 so add 100 to support up to 2099.
314 if (!info
->drv_data
->alarm_enable_reg
)
318 static int max77686_rtc_tm_to_data(struct rtc_time
*tm
, u8
*data
,
319 struct max77686_rtc_info
*info
)
321 data
[RTC_SEC
] = tm
->tm_sec
;
322 data
[RTC_MIN
] = tm
->tm_min
;
323 data
[RTC_HOUR
] = tm
->tm_hour
;
324 data
[RTC_WEEKDAY
] = 1 << tm
->tm_wday
;
325 data
[RTC_MONTHDAY
] = tm
->tm_mday
;
326 data
[RTC_MONTH
] = tm
->tm_mon
+ 1;
328 if (info
->drv_data
->alarm_enable_reg
) {
329 data
[RTC_YEAR
] = tm
->tm_year
;
333 data
[RTC_YEAR
] = tm
->tm_year
> 100 ? (tm
->tm_year
- 100) : 0;
335 if (tm
->tm_year
< 100) {
336 dev_err(info
->dev
, "RTC cannot handle the year %d.\n",
344 static int max77686_rtc_update(struct max77686_rtc_info
*info
,
345 enum MAX77686_RTC_OP op
)
349 unsigned long delay
= info
->drv_data
->delay
;
351 if (op
== MAX77686_RTC_WRITE
)
352 data
= 1 << RTC_UDR_SHIFT
;
354 data
= 1 << RTC_RBUDR_SHIFT
;
356 ret
= regmap_update_bits(info
->rtc_regmap
,
357 info
->drv_data
->map
[REG_RTC_UPDATE0
],
360 dev_err(info
->dev
, "Fail to write update reg(ret=%d, data=0x%x)\n",
363 /* Minimum delay required before RTC update. */
364 usleep_range(delay
, delay
* 2);
370 static int max77686_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
372 struct max77686_rtc_info
*info
= dev_get_drvdata(dev
);
373 u8 data
[RTC_NR_TIME
];
376 mutex_lock(&info
->lock
);
378 ret
= max77686_rtc_update(info
, MAX77686_RTC_READ
);
382 ret
= regmap_bulk_read(info
->rtc_regmap
,
383 info
->drv_data
->map
[REG_RTC_SEC
],
384 data
, ARRAY_SIZE(data
));
386 dev_err(info
->dev
, "Fail to read time reg(%d)\n", ret
);
390 max77686_rtc_data_to_tm(data
, tm
, info
);
393 mutex_unlock(&info
->lock
);
397 static int max77686_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
399 struct max77686_rtc_info
*info
= dev_get_drvdata(dev
);
400 u8 data
[RTC_NR_TIME
];
403 ret
= max77686_rtc_tm_to_data(tm
, data
, info
);
407 mutex_lock(&info
->lock
);
409 ret
= regmap_bulk_write(info
->rtc_regmap
,
410 info
->drv_data
->map
[REG_RTC_SEC
],
411 data
, ARRAY_SIZE(data
));
413 dev_err(info
->dev
, "Fail to write time reg(%d)\n", ret
);
417 ret
= max77686_rtc_update(info
, MAX77686_RTC_WRITE
);
420 mutex_unlock(&info
->lock
);
424 static int max77686_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
426 struct max77686_rtc_info
*info
= dev_get_drvdata(dev
);
427 u8 data
[RTC_NR_TIME
];
429 const unsigned int *map
= info
->drv_data
->map
;
432 mutex_lock(&info
->lock
);
434 ret
= max77686_rtc_update(info
, MAX77686_RTC_READ
);
438 ret
= regmap_bulk_read(info
->rtc_regmap
, map
[REG_ALARM1_SEC
],
439 data
, ARRAY_SIZE(data
));
441 dev_err(info
->dev
, "Fail to read alarm reg(%d)\n", ret
);
445 max77686_rtc_data_to_tm(data
, &alrm
->time
, info
);
449 if (info
->drv_data
->alarm_enable_reg
) {
450 if (map
[REG_RTC_AE1
] == REG_RTC_NONE
) {
453 "alarm enable register not set(%d)\n", ret
);
457 ret
= regmap_read(info
->rtc_regmap
, map
[REG_RTC_AE1
], &val
);
460 "fail to read alarm enable(%d)\n", ret
);
467 for (i
= 0; i
< ARRAY_SIZE(data
); i
++) {
468 if (data
[i
] & ALARM_ENABLE_MASK
) {
477 if (info
->drv_data
->alarm_pending_status_reg
== MAX77686_INVALID_REG
)
480 ret
= regmap_read(info
->regmap
,
481 info
->drv_data
->alarm_pending_status_reg
, &val
);
484 "Fail to read alarm pending status reg(%d)\n", ret
);
488 if (val
& (1 << 4)) /* RTCA1 */
492 mutex_unlock(&info
->lock
);
496 static int max77686_rtc_stop_alarm(struct max77686_rtc_info
*info
)
498 u8 data
[RTC_NR_TIME
];
501 const unsigned int *map
= info
->drv_data
->map
;
503 if (!mutex_is_locked(&info
->lock
))
504 dev_warn(info
->dev
, "%s: should have mutex locked\n", __func__
);
506 ret
= max77686_rtc_update(info
, MAX77686_RTC_READ
);
510 if (info
->drv_data
->alarm_enable_reg
) {
511 if (map
[REG_RTC_AE1
] == REG_RTC_NONE
) {
514 "alarm enable register not set(%d)\n", ret
);
518 ret
= regmap_write(info
->rtc_regmap
, map
[REG_RTC_AE1
], 0);
520 ret
= regmap_bulk_read(info
->rtc_regmap
, map
[REG_ALARM1_SEC
],
521 data
, ARRAY_SIZE(data
));
523 dev_err(info
->dev
, "Fail to read alarm reg(%d)\n", ret
);
527 max77686_rtc_data_to_tm(data
, &tm
, info
);
529 for (i
= 0; i
< ARRAY_SIZE(data
); i
++)
530 data
[i
] &= ~ALARM_ENABLE_MASK
;
532 ret
= regmap_bulk_write(info
->rtc_regmap
, map
[REG_ALARM1_SEC
],
533 data
, ARRAY_SIZE(data
));
537 dev_err(info
->dev
, "Fail to write alarm reg(%d)\n", ret
);
541 ret
= max77686_rtc_update(info
, MAX77686_RTC_WRITE
);
546 static int max77686_rtc_start_alarm(struct max77686_rtc_info
*info
)
548 u8 data
[RTC_NR_TIME
];
551 const unsigned int *map
= info
->drv_data
->map
;
553 if (!mutex_is_locked(&info
->lock
))
554 dev_warn(info
->dev
, "%s: should have mutex locked\n", __func__
);
556 ret
= max77686_rtc_update(info
, MAX77686_RTC_READ
);
560 if (info
->drv_data
->alarm_enable_reg
) {
561 ret
= regmap_write(info
->rtc_regmap
, map
[REG_RTC_AE1
],
562 MAX77802_ALARM_ENABLE_VALUE
);
564 ret
= regmap_bulk_read(info
->rtc_regmap
, map
[REG_ALARM1_SEC
],
565 data
, ARRAY_SIZE(data
));
567 dev_err(info
->dev
, "Fail to read alarm reg(%d)\n", ret
);
571 max77686_rtc_data_to_tm(data
, &tm
, info
);
573 data
[RTC_SEC
] |= (1 << ALARM_ENABLE_SHIFT
);
574 data
[RTC_MIN
] |= (1 << ALARM_ENABLE_SHIFT
);
575 data
[RTC_HOUR
] |= (1 << ALARM_ENABLE_SHIFT
);
576 data
[RTC_WEEKDAY
] &= ~ALARM_ENABLE_MASK
;
577 if (data
[RTC_MONTH
] & 0xf)
578 data
[RTC_MONTH
] |= (1 << ALARM_ENABLE_SHIFT
);
579 if (data
[RTC_YEAR
] & info
->drv_data
->mask
)
580 data
[RTC_YEAR
] |= (1 << ALARM_ENABLE_SHIFT
);
581 if (data
[RTC_MONTHDAY
] & 0x1f)
582 data
[RTC_MONTHDAY
] |= (1 << ALARM_ENABLE_SHIFT
);
584 ret
= regmap_bulk_write(info
->rtc_regmap
, map
[REG_ALARM1_SEC
],
585 data
, ARRAY_SIZE(data
));
589 dev_err(info
->dev
, "Fail to write alarm reg(%d)\n", ret
);
593 ret
= max77686_rtc_update(info
, MAX77686_RTC_WRITE
);
598 static int max77686_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
600 struct max77686_rtc_info
*info
= dev_get_drvdata(dev
);
601 u8 data
[RTC_NR_TIME
];
604 ret
= max77686_rtc_tm_to_data(&alrm
->time
, data
, info
);
608 mutex_lock(&info
->lock
);
610 ret
= max77686_rtc_stop_alarm(info
);
614 ret
= regmap_bulk_write(info
->rtc_regmap
,
615 info
->drv_data
->map
[REG_ALARM1_SEC
],
616 data
, ARRAY_SIZE(data
));
619 dev_err(info
->dev
, "Fail to write alarm reg(%d)\n", ret
);
623 ret
= max77686_rtc_update(info
, MAX77686_RTC_WRITE
);
628 ret
= max77686_rtc_start_alarm(info
);
630 mutex_unlock(&info
->lock
);
634 static int max77686_rtc_alarm_irq_enable(struct device
*dev
,
635 unsigned int enabled
)
637 struct max77686_rtc_info
*info
= dev_get_drvdata(dev
);
640 mutex_lock(&info
->lock
);
642 ret
= max77686_rtc_start_alarm(info
);
644 ret
= max77686_rtc_stop_alarm(info
);
645 mutex_unlock(&info
->lock
);
650 static irqreturn_t
max77686_rtc_alarm_irq(int irq
, void *data
)
652 struct max77686_rtc_info
*info
= data
;
654 dev_dbg(info
->dev
, "RTC alarm IRQ: %d\n", irq
);
656 rtc_update_irq(info
->rtc_dev
, 1, RTC_IRQF
| RTC_AF
);
661 static const struct rtc_class_ops max77686_rtc_ops
= {
662 .read_time
= max77686_rtc_read_time
,
663 .set_time
= max77686_rtc_set_time
,
664 .read_alarm
= max77686_rtc_read_alarm
,
665 .set_alarm
= max77686_rtc_set_alarm
,
666 .alarm_irq_enable
= max77686_rtc_alarm_irq_enable
,
669 static int max77686_rtc_init_reg(struct max77686_rtc_info
*info
)
674 /* Set RTC control register : Binary mode, 24hour mdoe */
675 data
[0] = (1 << BCD_EN_SHIFT
) | (1 << MODEL24_SHIFT
);
676 data
[1] = (0 << BCD_EN_SHIFT
) | (1 << MODEL24_SHIFT
);
678 ret
= regmap_bulk_write(info
->rtc_regmap
,
679 info
->drv_data
->map
[REG_RTC_CONTROLM
],
680 data
, ARRAY_SIZE(data
));
682 dev_err(info
->dev
, "Fail to write controlm reg(%d)\n", ret
);
686 ret
= max77686_rtc_update(info
, MAX77686_RTC_WRITE
);
690 static int max77686_init_rtc_regmap(struct max77686_rtc_info
*info
)
692 struct device
*parent
= info
->dev
->parent
;
693 struct i2c_client
*parent_i2c
= to_i2c_client(parent
);
696 if (info
->drv_data
->rtc_irq_from_platform
) {
697 struct platform_device
*pdev
= to_platform_device(info
->dev
);
699 info
->rtc_irq
= platform_get_irq(pdev
, 0);
700 if (info
->rtc_irq
< 0)
701 return info
->rtc_irq
;
703 info
->rtc_irq
= parent_i2c
->irq
;
706 info
->regmap
= dev_get_regmap(parent
, NULL
);
708 dev_err(info
->dev
, "Failed to get rtc regmap\n");
712 if (info
->drv_data
->rtc_i2c_addr
== MAX77686_INVALID_I2C_ADDR
) {
713 info
->rtc_regmap
= info
->regmap
;
717 info
->rtc
= devm_i2c_new_dummy_device(info
->dev
, parent_i2c
->adapter
,
718 info
->drv_data
->rtc_i2c_addr
);
719 if (IS_ERR(info
->rtc
)) {
720 dev_err(info
->dev
, "Failed to allocate I2C device for RTC\n");
721 return PTR_ERR(info
->rtc
);
724 info
->rtc_regmap
= devm_regmap_init_i2c(info
->rtc
,
725 info
->drv_data
->regmap_config
);
726 if (IS_ERR(info
->rtc_regmap
)) {
727 ret
= PTR_ERR(info
->rtc_regmap
);
728 dev_err(info
->dev
, "Failed to allocate RTC regmap: %d\n", ret
);
733 ret
= regmap_add_irq_chip(info
->rtc_regmap
, info
->rtc_irq
,
734 IRQF_ONESHOT
| IRQF_SHARED
,
735 0, info
->drv_data
->rtc_irq_chip
,
736 &info
->rtc_irq_data
);
738 dev_err(info
->dev
, "Failed to add RTC irq chip: %d\n", ret
);
745 static int max77686_rtc_probe(struct platform_device
*pdev
)
747 struct max77686_rtc_info
*info
;
748 const struct platform_device_id
*id
= platform_get_device_id(pdev
);
751 info
= devm_kzalloc(&pdev
->dev
, sizeof(struct max77686_rtc_info
),
756 mutex_init(&info
->lock
);
757 info
->dev
= &pdev
->dev
;
758 info
->drv_data
= (const struct max77686_rtc_driver_data
*)
761 ret
= max77686_init_rtc_regmap(info
);
765 platform_set_drvdata(pdev
, info
);
767 ret
= max77686_rtc_init_reg(info
);
769 dev_err(&pdev
->dev
, "Failed to initialize RTC reg:%d\n", ret
);
773 device_init_wakeup(&pdev
->dev
, 1);
775 info
->rtc_dev
= devm_rtc_device_register(&pdev
->dev
, id
->name
,
776 &max77686_rtc_ops
, THIS_MODULE
);
778 if (IS_ERR(info
->rtc_dev
)) {
779 ret
= PTR_ERR(info
->rtc_dev
);
780 dev_err(&pdev
->dev
, "Failed to register RTC device: %d\n", ret
);
786 info
->virq
= regmap_irq_get_virq(info
->rtc_irq_data
,
787 MAX77686_RTCIRQ_RTCA1
);
788 if (info
->virq
<= 0) {
793 ret
= request_threaded_irq(info
->virq
, NULL
, max77686_rtc_alarm_irq
, 0,
796 dev_err(&pdev
->dev
, "Failed to request alarm IRQ: %d: %d\n",
804 regmap_del_irq_chip(info
->rtc_irq
, info
->rtc_irq_data
);
809 static void max77686_rtc_remove(struct platform_device
*pdev
)
811 struct max77686_rtc_info
*info
= platform_get_drvdata(pdev
);
813 free_irq(info
->virq
, info
);
814 regmap_del_irq_chip(info
->rtc_irq
, info
->rtc_irq_data
);
817 #ifdef CONFIG_PM_SLEEP
818 static int max77686_rtc_suspend(struct device
*dev
)
820 struct max77686_rtc_info
*info
= dev_get_drvdata(dev
);
823 if (device_may_wakeup(dev
)) {
824 struct max77686_rtc_info
*info
= dev_get_drvdata(dev
);
826 ret
= enable_irq_wake(info
->virq
);
830 * If the main IRQ (not virtual) is the parent IRQ, then it must be
831 * disabled during suspend because if it happens while suspended it
832 * will be handled before resuming I2C.
834 * Since Main IRQ is shared, all its users should disable it to be sure
835 * it won't fire while one of them is still suspended.
837 if (!info
->drv_data
->rtc_irq_from_platform
)
838 disable_irq(info
->rtc_irq
);
843 static int max77686_rtc_resume(struct device
*dev
)
845 struct max77686_rtc_info
*info
= dev_get_drvdata(dev
);
847 if (!info
->drv_data
->rtc_irq_from_platform
)
848 enable_irq(info
->rtc_irq
);
850 if (device_may_wakeup(dev
)) {
851 struct max77686_rtc_info
*info
= dev_get_drvdata(dev
);
853 return disable_irq_wake(info
->virq
);
860 static SIMPLE_DEV_PM_OPS(max77686_rtc_pm_ops
,
861 max77686_rtc_suspend
, max77686_rtc_resume
);
863 static const struct platform_device_id rtc_id
[] = {
864 { "max77686-rtc", .driver_data
= (kernel_ulong_t
)&max77686_drv_data
, },
865 { "max77802-rtc", .driver_data
= (kernel_ulong_t
)&max77802_drv_data
, },
866 { "max77620-rtc", .driver_data
= (kernel_ulong_t
)&max77620_drv_data
, },
867 { "max77714-rtc", .driver_data
= (kernel_ulong_t
)&max77714_drv_data
, },
870 MODULE_DEVICE_TABLE(platform
, rtc_id
);
872 static struct platform_driver max77686_rtc_driver
= {
874 .name
= "max77686-rtc",
875 .pm
= &max77686_rtc_pm_ops
,
877 .probe
= max77686_rtc_probe
,
878 .remove
= max77686_rtc_remove
,
882 module_platform_driver(max77686_rtc_driver
);
884 MODULE_DESCRIPTION("Maxim MAX77686 RTC driver");
885 MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>");
886 MODULE_LICENSE("GPL");