2 * RTC driver for Maxim MAX77686 and MAX77802
4 * Copyright (C) 2012 Samsung Electronics Co.Ltd
6 * based on rtc-max8997.c
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
15 #include <linux/slab.h>
16 #include <linux/rtc.h>
17 #include <linux/delay.h>
18 #include <linux/mutex.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/mfd/max77686-private.h>
22 #include <linux/irqdomain.h>
23 #include <linux/regmap.h>
25 /* RTC Control Register */
26 #define BCD_EN_SHIFT 0
27 #define BCD_EN_MASK (1 << BCD_EN_SHIFT)
28 #define MODEL24_SHIFT 1
29 #define MODEL24_MASK (1 << MODEL24_SHIFT)
30 /* RTC Update Register1 */
31 #define RTC_UDR_SHIFT 0
32 #define RTC_UDR_MASK (1 << RTC_UDR_SHIFT)
33 #define RTC_RBUDR_SHIFT 4
34 #define RTC_RBUDR_MASK (1 << RTC_RBUDR_SHIFT)
35 /* RTC Hour register */
36 #define HOUR_PM_SHIFT 6
37 #define HOUR_PM_MASK (1 << HOUR_PM_SHIFT)
38 /* RTC Alarm Enable */
39 #define ALARM_ENABLE_SHIFT 7
40 #define ALARM_ENABLE_MASK (1 << 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
62 struct max77686_rtc_driver_data {
63 /* Minimum usecs needed for a RTC update */
65 /* Mask used to read RTC registers value */
67 /* Registers offset to I2C addresses map */
68 const unsigned int *map;
69 /* Has a separate alarm enable register? */
70 bool alarm_enable_reg;
71 /* Has a separate I2C regmap for the RTC? */
72 bool separate_i2c_addr;
75 struct max77686_rtc_info {
77 struct max77686_dev *max77686;
78 struct i2c_client *rtc;
79 struct rtc_device *rtc_dev;
82 struct regmap *regmap;
84 const struct max77686_rtc_driver_data *drv_data;
90 enum MAX77686_RTC_OP {
95 /* These are not registers but just offsets that are mapped to addresses */
96 enum max77686_rtc_reg_offset {
126 /* Maps RTC registers offset to the MAX77686 register addresses */
127 static const unsigned int max77686_map[REG_RTC_END] = {
128 [REG_RTC_CONTROLM] = MAX77686_RTC_CONTROLM,
129 [REG_RTC_CONTROL] = MAX77686_RTC_CONTROL,
130 [REG_RTC_UPDATE0] = MAX77686_RTC_UPDATE0,
131 [REG_WTSR_SMPL_CNTL] = MAX77686_WTSR_SMPL_CNTL,
132 [REG_RTC_SEC] = MAX77686_RTC_SEC,
133 [REG_RTC_MIN] = MAX77686_RTC_MIN,
134 [REG_RTC_HOUR] = MAX77686_RTC_HOUR,
135 [REG_RTC_WEEKDAY] = MAX77686_RTC_WEEKDAY,
136 [REG_RTC_MONTH] = MAX77686_RTC_MONTH,
137 [REG_RTC_YEAR] = MAX77686_RTC_YEAR,
138 [REG_RTC_DATE] = MAX77686_RTC_DATE,
139 [REG_ALARM1_SEC] = MAX77686_ALARM1_SEC,
140 [REG_ALARM1_MIN] = MAX77686_ALARM1_MIN,
141 [REG_ALARM1_HOUR] = MAX77686_ALARM1_HOUR,
142 [REG_ALARM1_WEEKDAY] = MAX77686_ALARM1_WEEKDAY,
143 [REG_ALARM1_MONTH] = MAX77686_ALARM1_MONTH,
144 [REG_ALARM1_YEAR] = MAX77686_ALARM1_YEAR,
145 [REG_ALARM1_DATE] = MAX77686_ALARM1_DATE,
146 [REG_ALARM2_SEC] = MAX77686_ALARM2_SEC,
147 [REG_ALARM2_MIN] = MAX77686_ALARM2_MIN,
148 [REG_ALARM2_HOUR] = MAX77686_ALARM2_HOUR,
149 [REG_ALARM2_WEEKDAY] = MAX77686_ALARM2_WEEKDAY,
150 [REG_ALARM2_MONTH] = MAX77686_ALARM2_MONTH,
151 [REG_ALARM2_YEAR] = MAX77686_ALARM2_YEAR,
152 [REG_ALARM2_DATE] = MAX77686_ALARM2_DATE,
153 [REG_RTC_AE1] = REG_RTC_NONE,
156 static const struct max77686_rtc_driver_data max77686_drv_data = {
160 .alarm_enable_reg = false,
161 .separate_i2c_addr = true,
164 static const unsigned int max77802_map[REG_RTC_END] = {
165 [REG_RTC_CONTROLM] = MAX77802_RTC_CONTROLM,
166 [REG_RTC_CONTROL] = MAX77802_RTC_CONTROL,
167 [REG_RTC_UPDATE0] = MAX77802_RTC_UPDATE0,
168 [REG_WTSR_SMPL_CNTL] = MAX77802_WTSR_SMPL_CNTL,
169 [REG_RTC_SEC] = MAX77802_RTC_SEC,
170 [REG_RTC_MIN] = MAX77802_RTC_MIN,
171 [REG_RTC_HOUR] = MAX77802_RTC_HOUR,
172 [REG_RTC_WEEKDAY] = MAX77802_RTC_WEEKDAY,
173 [REG_RTC_MONTH] = MAX77802_RTC_MONTH,
174 [REG_RTC_YEAR] = MAX77802_RTC_YEAR,
175 [REG_RTC_DATE] = MAX77802_RTC_DATE,
176 [REG_ALARM1_SEC] = MAX77802_ALARM1_SEC,
177 [REG_ALARM1_MIN] = MAX77802_ALARM1_MIN,
178 [REG_ALARM1_HOUR] = MAX77802_ALARM1_HOUR,
179 [REG_ALARM1_WEEKDAY] = MAX77802_ALARM1_WEEKDAY,
180 [REG_ALARM1_MONTH] = MAX77802_ALARM1_MONTH,
181 [REG_ALARM1_YEAR] = MAX77802_ALARM1_YEAR,
182 [REG_ALARM1_DATE] = MAX77802_ALARM1_DATE,
183 [REG_ALARM2_SEC] = MAX77802_ALARM2_SEC,
184 [REG_ALARM2_MIN] = MAX77802_ALARM2_MIN,
185 [REG_ALARM2_HOUR] = MAX77802_ALARM2_HOUR,
186 [REG_ALARM2_WEEKDAY] = MAX77802_ALARM2_WEEKDAY,
187 [REG_ALARM2_MONTH] = MAX77802_ALARM2_MONTH,
188 [REG_ALARM2_YEAR] = MAX77802_ALARM2_YEAR,
189 [REG_ALARM2_DATE] = MAX77802_ALARM2_DATE,
190 [REG_RTC_AE1] = MAX77802_RTC_AE1,
193 static const struct max77686_rtc_driver_data max77802_drv_data = {
197 .alarm_enable_reg = true,
198 .separate_i2c_addr = false,
201 static void max77686_rtc_data_to_tm(u8 *data, struct rtc_time *tm,
202 struct max77686_rtc_info *info)
204 u8 mask = info->drv_data->mask;
206 tm->tm_sec = data[RTC_SEC] & mask;
207 tm->tm_min = data[RTC_MIN] & mask;
208 if (info->rtc_24hr_mode)
209 tm->tm_hour = data[RTC_HOUR] & 0x1f;
211 tm->tm_hour = data[RTC_HOUR] & 0x0f;
212 if (data[RTC_HOUR] & HOUR_PM_MASK)
216 /* Only a single bit is set in data[], so fls() would be equivalent */
217 tm->tm_wday = ffs(data[RTC_WEEKDAY] & mask) - 1;
218 tm->tm_mday = data[RTC_DATE] & 0x1f;
219 tm->tm_mon = (data[RTC_MONTH] & 0x0f) - 1;
220 tm->tm_year = data[RTC_YEAR] & mask;
225 * MAX77686 uses 1 bit from sec/min/hour/etc RTC registers and the
226 * year values are just 0..99 so add 100 to support up to 2099.
228 if (!info->drv_data->alarm_enable_reg)
232 static int max77686_rtc_tm_to_data(struct rtc_time *tm, u8 *data,
233 struct max77686_rtc_info *info)
235 data[RTC_SEC] = tm->tm_sec;
236 data[RTC_MIN] = tm->tm_min;
237 data[RTC_HOUR] = tm->tm_hour;
238 data[RTC_WEEKDAY] = 1 << tm->tm_wday;
239 data[RTC_DATE] = tm->tm_mday;
240 data[RTC_MONTH] = tm->tm_mon + 1;
242 if (info->drv_data->alarm_enable_reg) {
243 data[RTC_YEAR] = tm->tm_year;
247 data[RTC_YEAR] = tm->tm_year > 100 ? (tm->tm_year - 100) : 0;
249 if (tm->tm_year < 100) {
250 dev_err(info->dev, "RTC cannot handle the year %d.\n",
258 static int max77686_rtc_update(struct max77686_rtc_info *info,
259 enum MAX77686_RTC_OP op)
263 unsigned long delay = info->drv_data->delay;
265 if (op == MAX77686_RTC_WRITE)
266 data = 1 << RTC_UDR_SHIFT;
268 data = 1 << RTC_RBUDR_SHIFT;
270 ret = regmap_update_bits(info->max77686->rtc_regmap,
271 info->drv_data->map[REG_RTC_UPDATE0],
274 dev_err(info->dev, "Fail to write update reg(ret=%d, data=0x%x)\n",
277 /* Minimum delay required before RTC update. */
278 usleep_range(delay, delay * 2);
284 static int max77686_rtc_read_time(struct device *dev, struct rtc_time *tm)
286 struct max77686_rtc_info *info = dev_get_drvdata(dev);
287 u8 data[RTC_NR_TIME];
290 mutex_lock(&info->lock);
292 ret = max77686_rtc_update(info, MAX77686_RTC_READ);
296 ret = regmap_bulk_read(info->max77686->rtc_regmap,
297 info->drv_data->map[REG_RTC_SEC],
298 data, ARRAY_SIZE(data));
300 dev_err(info->dev, "Fail to read time reg(%d)\n", ret);
304 max77686_rtc_data_to_tm(data, tm, info);
306 ret = rtc_valid_tm(tm);
309 mutex_unlock(&info->lock);
313 static int max77686_rtc_set_time(struct device *dev, struct rtc_time *tm)
315 struct max77686_rtc_info *info = dev_get_drvdata(dev);
316 u8 data[RTC_NR_TIME];
319 ret = max77686_rtc_tm_to_data(tm, data, info);
323 mutex_lock(&info->lock);
325 ret = regmap_bulk_write(info->max77686->rtc_regmap,
326 info->drv_data->map[REG_RTC_SEC],
327 data, ARRAY_SIZE(data));
329 dev_err(info->dev, "Fail to write time reg(%d)\n", ret);
333 ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
336 mutex_unlock(&info->lock);
340 static int max77686_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
342 struct max77686_rtc_info *info = dev_get_drvdata(dev);
343 u8 data[RTC_NR_TIME];
345 const unsigned int *map = info->drv_data->map;
348 mutex_lock(&info->lock);
350 ret = max77686_rtc_update(info, MAX77686_RTC_READ);
354 ret = regmap_bulk_read(info->max77686->rtc_regmap,
355 map[REG_ALARM1_SEC], data, ARRAY_SIZE(data));
357 dev_err(info->dev, "Fail to read alarm reg(%d)\n", ret);
361 max77686_rtc_data_to_tm(data, &alrm->time, info);
365 if (info->drv_data->alarm_enable_reg) {
366 if (map[REG_RTC_AE1] == REG_RTC_NONE) {
369 "alarm enable register not set(%d)\n", ret);
373 ret = regmap_read(info->max77686->regmap,
374 map[REG_RTC_AE1], &val);
377 "fail to read alarm enable(%d)\n", ret);
384 for (i = 0; i < ARRAY_SIZE(data); i++) {
385 if (data[i] & ALARM_ENABLE_MASK) {
393 ret = regmap_read(info->max77686->regmap, MAX77686_REG_STATUS2, &val);
395 dev_err(info->dev, "Fail to read status2 reg(%d)\n", ret);
399 if (val & (1 << 4)) /* RTCA1 */
403 mutex_unlock(&info->lock);
407 static int max77686_rtc_stop_alarm(struct max77686_rtc_info *info)
409 u8 data[RTC_NR_TIME];
412 const unsigned int *map = info->drv_data->map;
414 if (!mutex_is_locked(&info->lock))
415 dev_warn(info->dev, "%s: should have mutex locked\n", __func__);
417 ret = max77686_rtc_update(info, MAX77686_RTC_READ);
421 if (info->drv_data->alarm_enable_reg) {
422 if (map[REG_RTC_AE1] == REG_RTC_NONE) {
425 "alarm enable register not set(%d)\n", ret);
429 ret = regmap_write(info->max77686->regmap, map[REG_RTC_AE1], 0);
431 ret = regmap_bulk_read(info->max77686->rtc_regmap,
432 map[REG_ALARM1_SEC], data,
435 dev_err(info->dev, "Fail to read alarm reg(%d)\n", ret);
439 max77686_rtc_data_to_tm(data, &tm, info);
441 for (i = 0; i < ARRAY_SIZE(data); i++)
442 data[i] &= ~ALARM_ENABLE_MASK;
444 ret = regmap_bulk_write(info->max77686->rtc_regmap,
445 map[REG_ALARM1_SEC], data,
450 dev_err(info->dev, "Fail to write alarm reg(%d)\n", ret);
454 ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
459 static int max77686_rtc_start_alarm(struct max77686_rtc_info *info)
461 u8 data[RTC_NR_TIME];
464 const unsigned int *map = info->drv_data->map;
466 if (!mutex_is_locked(&info->lock))
467 dev_warn(info->dev, "%s: should have mutex locked\n", __func__);
469 ret = max77686_rtc_update(info, MAX77686_RTC_READ);
473 if (info->drv_data->alarm_enable_reg) {
474 ret = regmap_write(info->max77686->regmap, map[REG_RTC_AE1],
475 MAX77802_ALARM_ENABLE_VALUE);
477 ret = regmap_bulk_read(info->max77686->rtc_regmap,
478 map[REG_ALARM1_SEC], data,
481 dev_err(info->dev, "Fail to read alarm reg(%d)\n", ret);
485 max77686_rtc_data_to_tm(data, &tm, info);
487 data[RTC_SEC] |= (1 << ALARM_ENABLE_SHIFT);
488 data[RTC_MIN] |= (1 << ALARM_ENABLE_SHIFT);
489 data[RTC_HOUR] |= (1 << ALARM_ENABLE_SHIFT);
490 data[RTC_WEEKDAY] &= ~ALARM_ENABLE_MASK;
491 if (data[RTC_MONTH] & 0xf)
492 data[RTC_MONTH] |= (1 << ALARM_ENABLE_SHIFT);
493 if (data[RTC_YEAR] & info->drv_data->mask)
494 data[RTC_YEAR] |= (1 << ALARM_ENABLE_SHIFT);
495 if (data[RTC_DATE] & 0x1f)
496 data[RTC_DATE] |= (1 << ALARM_ENABLE_SHIFT);
498 ret = regmap_bulk_write(info->max77686->rtc_regmap,
499 map[REG_ALARM1_SEC], data,
504 dev_err(info->dev, "Fail to write alarm reg(%d)\n", ret);
508 ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
513 static int max77686_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
515 struct max77686_rtc_info *info = dev_get_drvdata(dev);
516 u8 data[RTC_NR_TIME];
519 ret = max77686_rtc_tm_to_data(&alrm->time, data, info);
523 mutex_lock(&info->lock);
525 ret = max77686_rtc_stop_alarm(info);
529 ret = regmap_bulk_write(info->max77686->rtc_regmap,
530 info->drv_data->map[REG_ALARM1_SEC],
531 data, ARRAY_SIZE(data));
534 dev_err(info->dev, "Fail to write alarm reg(%d)\n", ret);
538 ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
543 ret = max77686_rtc_start_alarm(info);
545 mutex_unlock(&info->lock);
549 static int max77686_rtc_alarm_irq_enable(struct device *dev,
550 unsigned int enabled)
552 struct max77686_rtc_info *info = dev_get_drvdata(dev);
555 mutex_lock(&info->lock);
557 ret = max77686_rtc_start_alarm(info);
559 ret = max77686_rtc_stop_alarm(info);
560 mutex_unlock(&info->lock);
565 static irqreturn_t max77686_rtc_alarm_irq(int irq, void *data)
567 struct max77686_rtc_info *info = data;
569 dev_dbg(info->dev, "RTC alarm IRQ: %d\n", irq);
571 rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF);
576 static const struct rtc_class_ops max77686_rtc_ops = {
577 .read_time = max77686_rtc_read_time,
578 .set_time = max77686_rtc_set_time,
579 .read_alarm = max77686_rtc_read_alarm,
580 .set_alarm = max77686_rtc_set_alarm,
581 .alarm_irq_enable = max77686_rtc_alarm_irq_enable,
584 static int max77686_rtc_init_reg(struct max77686_rtc_info *info)
589 /* Set RTC control register : Binary mode, 24hour mdoe */
590 data[0] = (1 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
591 data[1] = (0 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
593 info->rtc_24hr_mode = 1;
595 ret = regmap_bulk_write(info->max77686->rtc_regmap,
596 info->drv_data->map[REG_RTC_CONTROLM],
597 data, ARRAY_SIZE(data));
599 dev_err(info->dev, "Fail to write controlm reg(%d)\n", ret);
603 ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
607 static int max77686_rtc_probe(struct platform_device *pdev)
609 struct max77686_dev *max77686 = dev_get_drvdata(pdev->dev.parent);
610 struct max77686_rtc_info *info;
611 const struct platform_device_id *id = platform_get_device_id(pdev);
614 info = devm_kzalloc(&pdev->dev, sizeof(struct max77686_rtc_info),
619 mutex_init(&info->lock);
620 info->dev = &pdev->dev;
621 info->max77686 = max77686;
622 info->rtc = max77686->rtc;
623 info->drv_data = (const struct max77686_rtc_driver_data *)
626 if (!info->drv_data->separate_i2c_addr)
627 info->max77686->rtc_regmap = info->max77686->regmap;
629 platform_set_drvdata(pdev, info);
631 ret = max77686_rtc_init_reg(info);
634 dev_err(&pdev->dev, "Failed to initialize RTC reg:%d\n", ret);
638 device_init_wakeup(&pdev->dev, 1);
640 info->rtc_dev = devm_rtc_device_register(&pdev->dev, id->name,
641 &max77686_rtc_ops, THIS_MODULE);
643 if (IS_ERR(info->rtc_dev)) {
644 ret = PTR_ERR(info->rtc_dev);
645 dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
651 if (!max77686->rtc_irq_data) {
653 dev_err(&pdev->dev, "No RTC regmap IRQ chip\n");
657 info->virq = regmap_irq_get_virq(max77686->rtc_irq_data,
658 MAX77686_RTCIRQ_RTCA1);
659 if (info->virq <= 0) {
664 ret = devm_request_threaded_irq(&pdev->dev, info->virq, NULL,
665 max77686_rtc_alarm_irq, 0, "rtc-alarm1", info);
667 dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n",
674 #ifdef CONFIG_PM_SLEEP
675 static int max77686_rtc_suspend(struct device *dev)
677 if (device_may_wakeup(dev)) {
678 struct max77686_rtc_info *info = dev_get_drvdata(dev);
680 return enable_irq_wake(info->virq);
686 static int max77686_rtc_resume(struct device *dev)
688 if (device_may_wakeup(dev)) {
689 struct max77686_rtc_info *info = dev_get_drvdata(dev);
691 return disable_irq_wake(info->virq);
698 static SIMPLE_DEV_PM_OPS(max77686_rtc_pm_ops,
699 max77686_rtc_suspend, max77686_rtc_resume);
701 static const struct platform_device_id rtc_id[] = {
702 { "max77686-rtc", .driver_data = (kernel_ulong_t)&max77686_drv_data, },
703 { "max77802-rtc", .driver_data = (kernel_ulong_t)&max77802_drv_data, },
706 MODULE_DEVICE_TABLE(platform, rtc_id);
708 static struct platform_driver max77686_rtc_driver = {
710 .name = "max77686-rtc",
711 .pm = &max77686_rtc_pm_ops,
713 .probe = max77686_rtc_probe,
717 module_platform_driver(max77686_rtc_driver);
719 MODULE_DESCRIPTION("Maxim MAX77686 RTC driver");
720 MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>");
721 MODULE_LICENSE("GPL");