1 // SPDX-License-Identifier: GPL-2.0-or-later
3 // Copyright (C) 2018 ROHM Semiconductors
5 // RTC driver for ROHM BD70528 PMIC
8 #include <linux/mfd/rohm-bd70528.h>
9 #include <linux/mfd/rohm-bd71828.h>
10 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/regmap.h>
14 #include <linux/rtc.h>
17 * We read regs RTC_SEC => RTC_YEAR
18 * this struct is ordered according to chip registers.
19 * Keep it u8 only (or packed) to avoid padding issues.
21 struct bd70528_rtc_day {
27 struct bd70528_rtc_data {
28 struct bd70528_rtc_day time;
35 struct bd70528_rtc_wake {
36 struct bd70528_rtc_day time;
40 struct bd71828_rtc_alm {
41 struct bd70528_rtc_data alm0;
42 struct bd70528_rtc_data alm1;
47 struct bd70528_rtc_alm {
48 struct bd70528_rtc_data data;
54 struct rohm_regmap_dev *parent;
60 static int bd70528_set_wake(struct rohm_regmap_dev *bd70528,
61 int enable, int *old_state)
64 unsigned int ctrl_reg;
66 ret = regmap_read(bd70528->regmap, BD70528_REG_WAKE_EN, &ctrl_reg);
71 if (ctrl_reg & BD70528_MASK_WAKE_EN)
72 *old_state |= BD70528_WAKE_STATE_BIT;
74 *old_state &= ~BD70528_WAKE_STATE_BIT;
76 if (!enable == !(*old_state & BD70528_WAKE_STATE_BIT))
81 ctrl_reg |= BD70528_MASK_WAKE_EN;
83 ctrl_reg &= ~BD70528_MASK_WAKE_EN;
85 return regmap_write(bd70528->regmap, BD70528_REG_WAKE_EN,
89 static int bd70528_set_elapsed_tmr(struct rohm_regmap_dev *bd70528,
90 int enable, int *old_state)
93 unsigned int ctrl_reg;
97 * What is the purpose of elapsed timer ?
98 * Is the timeout registers counting down, or is the disable - re-enable
99 * going to restart the elapsed-time counting? If counting is restarted
100 * the timeout should be decreased by the amount of time that has
101 * elapsed since starting the timer. Maybe we should store the monotonic
102 * clock value when timer is started so that if RTC is set while timer
103 * is armed we could do the compensation. This is a hack if RTC/system
104 * clk are drifting. OTOH, RTC controlled via I2C is in any case
107 ret = regmap_read(bd70528->regmap, BD70528_REG_ELAPSED_TIMER_EN,
113 if (ctrl_reg & BD70528_MASK_ELAPSED_TIMER_EN)
114 *old_state |= BD70528_ELAPSED_STATE_BIT;
116 *old_state &= ~BD70528_ELAPSED_STATE_BIT;
118 if ((!enable) == (!(*old_state & BD70528_ELAPSED_STATE_BIT)))
123 ctrl_reg |= BD70528_MASK_ELAPSED_TIMER_EN;
125 ctrl_reg &= ~BD70528_MASK_ELAPSED_TIMER_EN;
127 return regmap_write(bd70528->regmap, BD70528_REG_ELAPSED_TIMER_EN,
131 static int bd70528_set_rtc_based_timers(struct bd70528_rtc *r, int new_state,
136 ret = bd70528_wdt_set(r->parent, new_state & BD70528_WDT_STATE_BIT,
140 "Failed to disable WDG for RTC setting (%d)\n", ret);
143 ret = bd70528_set_elapsed_tmr(r->parent,
144 new_state & BD70528_ELAPSED_STATE_BIT,
148 "Failed to disable 'elapsed timer' for RTC setting\n");
151 ret = bd70528_set_wake(r->parent, new_state & BD70528_WAKE_STATE_BIT,
155 "Failed to disable 'wake timer' for RTC setting\n");
162 static int bd70528_re_enable_rtc_based_timers(struct bd70528_rtc *r,
165 if (!r->has_rtc_timers)
168 return bd70528_set_rtc_based_timers(r, old_state, NULL);
171 static int bd70528_disable_rtc_based_timers(struct bd70528_rtc *r,
174 if (!r->has_rtc_timers)
177 return bd70528_set_rtc_based_timers(r, 0, old_state);
180 static inline void tmday2rtc(struct rtc_time *t, struct bd70528_rtc_day *d)
182 d->sec &= ~BD70528_MASK_RTC_SEC;
183 d->min &= ~BD70528_MASK_RTC_MINUTE;
184 d->hour &= ~BD70528_MASK_RTC_HOUR;
185 d->sec |= bin2bcd(t->tm_sec);
186 d->min |= bin2bcd(t->tm_min);
187 d->hour |= bin2bcd(t->tm_hour);
190 static inline void tm2rtc(struct rtc_time *t, struct bd70528_rtc_data *r)
192 r->day &= ~BD70528_MASK_RTC_DAY;
193 r->week &= ~BD70528_MASK_RTC_WEEK;
194 r->month &= ~BD70528_MASK_RTC_MONTH;
196 * PM and 24H bits are not used by Wake - thus we clear them
197 * here and not in tmday2rtc() which is also used by wake.
199 r->time.hour &= ~(BD70528_MASK_RTC_HOUR_PM | BD70528_MASK_RTC_HOUR_24H);
201 tmday2rtc(t, &r->time);
203 * We do always set time in 24H mode.
205 r->time.hour |= BD70528_MASK_RTC_HOUR_24H;
206 r->day |= bin2bcd(t->tm_mday);
207 r->week |= bin2bcd(t->tm_wday);
208 r->month |= bin2bcd(t->tm_mon + 1);
209 r->year = bin2bcd(t->tm_year - 100);
212 static inline void rtc2tm(struct bd70528_rtc_data *r, struct rtc_time *t)
214 t->tm_sec = bcd2bin(r->time.sec & BD70528_MASK_RTC_SEC);
215 t->tm_min = bcd2bin(r->time.min & BD70528_MASK_RTC_MINUTE);
216 t->tm_hour = bcd2bin(r->time.hour & BD70528_MASK_RTC_HOUR);
218 * If RTC is in 12H mode, then bit BD70528_MASK_RTC_HOUR_PM
219 * is not BCD value but tells whether it is AM or PM
221 if (!(r->time.hour & BD70528_MASK_RTC_HOUR_24H)) {
223 if (r->time.hour & BD70528_MASK_RTC_HOUR_PM)
226 t->tm_mday = bcd2bin(r->day & BD70528_MASK_RTC_DAY);
227 t->tm_mon = bcd2bin(r->month & BD70528_MASK_RTC_MONTH) - 1;
228 t->tm_year = 100 + bcd2bin(r->year & BD70528_MASK_RTC_YEAR);
229 t->tm_wday = bcd2bin(r->week & BD70528_MASK_RTC_WEEK);
232 static int bd71828_set_alarm(struct device *dev, struct rtc_wkalrm *a)
235 struct bd71828_rtc_alm alm;
236 struct bd70528_rtc *r = dev_get_drvdata(dev);
237 struct rohm_regmap_dev *parent = r->parent;
239 ret = regmap_bulk_read(parent->regmap, BD71828_REG_RTC_ALM_START,
242 dev_err(dev, "Failed to read alarm regs\n");
246 tm2rtc(&a->time, &alm.alm0);
249 alm.alm_mask &= ~BD70528_MASK_ALM_EN;
251 alm.alm_mask |= BD70528_MASK_ALM_EN;
253 ret = regmap_bulk_write(parent->regmap, BD71828_REG_RTC_ALM_START,
256 dev_err(dev, "Failed to set alarm time\n");
262 static int bd70528_set_alarm(struct device *dev, struct rtc_wkalrm *a)
264 struct bd70528_rtc_wake wake;
265 struct bd70528_rtc_alm alm;
267 struct bd70528_rtc *r = dev_get_drvdata(dev);
268 struct rohm_regmap_dev *parent = r->parent;
270 ret = regmap_bulk_read(parent->regmap, BD70528_REG_RTC_WAKE_START,
271 &wake, sizeof(wake));
273 dev_err(dev, "Failed to read wake regs\n");
277 ret = regmap_bulk_read(parent->regmap, BD70528_REG_RTC_ALM_START,
280 dev_err(dev, "Failed to read alarm regs\n");
284 tm2rtc(&a->time, &alm.data);
285 tmday2rtc(&a->time, &wake.time);
288 alm.alm_mask &= ~BD70528_MASK_ALM_EN;
289 wake.ctrl |= BD70528_MASK_WAKE_EN;
291 alm.alm_mask |= BD70528_MASK_ALM_EN;
292 wake.ctrl &= ~BD70528_MASK_WAKE_EN;
295 ret = regmap_bulk_write(parent->regmap,
296 BD70528_REG_RTC_WAKE_START, &wake,
299 dev_err(dev, "Failed to set wake time\n");
302 ret = regmap_bulk_write(parent->regmap, BD70528_REG_RTC_ALM_START,
305 dev_err(dev, "Failed to set alarm time\n");
310 static int bd71828_read_alarm(struct device *dev, struct rtc_wkalrm *a)
313 struct bd71828_rtc_alm alm;
314 struct bd70528_rtc *r = dev_get_drvdata(dev);
315 struct rohm_regmap_dev *parent = r->parent;
317 ret = regmap_bulk_read(parent->regmap, BD71828_REG_RTC_ALM_START,
320 dev_err(dev, "Failed to read alarm regs\n");
324 rtc2tm(&alm.alm0, &a->time);
325 a->time.tm_mday = -1;
327 a->time.tm_year = -1;
328 a->enabled = !!(alm.alm_mask & BD70528_MASK_ALM_EN);
334 static int bd70528_read_alarm(struct device *dev, struct rtc_wkalrm *a)
336 struct bd70528_rtc_alm alm;
338 struct bd70528_rtc *r = dev_get_drvdata(dev);
339 struct rohm_regmap_dev *parent = r->parent;
341 ret = regmap_bulk_read(parent->regmap, BD70528_REG_RTC_ALM_START,
344 dev_err(dev, "Failed to read alarm regs\n");
348 rtc2tm(&alm.data, &a->time);
349 a->time.tm_mday = -1;
351 a->time.tm_year = -1;
352 a->enabled = !(alm.alm_mask & BD70528_MASK_ALM_EN);
358 static int bd70528_set_time_locked(struct device *dev, struct rtc_time *t)
360 int ret, tmpret, old_states;
361 struct bd70528_rtc_data rtc_data;
362 struct bd70528_rtc *r = dev_get_drvdata(dev);
363 struct rohm_regmap_dev *parent = r->parent;
365 ret = bd70528_disable_rtc_based_timers(r, &old_states);
369 tmpret = regmap_bulk_read(parent->regmap,
370 r->reg_time_start, &rtc_data,
373 dev_err(dev, "Failed to read RTC time registers\n");
376 tm2rtc(t, &rtc_data);
378 tmpret = regmap_bulk_write(parent->regmap,
379 r->reg_time_start, &rtc_data,
382 dev_err(dev, "Failed to set RTC time\n");
387 ret = bd70528_re_enable_rtc_based_timers(r, old_states);
394 static int bd71828_set_time(struct device *dev, struct rtc_time *t)
396 return bd70528_set_time_locked(dev, t);
399 static int bd70528_set_time(struct device *dev, struct rtc_time *t)
402 struct bd70528_rtc *r = dev_get_drvdata(dev);
404 bd70528_wdt_lock(r->parent);
405 ret = bd70528_set_time_locked(dev, t);
406 bd70528_wdt_unlock(r->parent);
410 static int bd70528_get_time(struct device *dev, struct rtc_time *t)
412 struct bd70528_rtc *r = dev_get_drvdata(dev);
413 struct rohm_regmap_dev *parent = r->parent;
414 struct bd70528_rtc_data rtc_data;
417 /* read the RTC date and time registers all at once */
418 ret = regmap_bulk_read(parent->regmap,
419 r->reg_time_start, &rtc_data,
422 dev_err(dev, "Failed to read RTC time (err %d)\n", ret);
426 rtc2tm(&rtc_data, t);
431 static int bd70528_alm_enable(struct device *dev, unsigned int enabled)
434 unsigned int enableval = BD70528_MASK_ALM_EN;
435 struct bd70528_rtc *r = dev_get_drvdata(dev);
440 bd70528_wdt_lock(r->parent);
441 ret = bd70528_set_wake(r->parent, enabled, NULL);
443 dev_err(dev, "Failed to change wake state\n");
446 ret = regmap_update_bits(r->parent->regmap, BD70528_REG_RTC_ALM_MASK,
447 BD70528_MASK_ALM_EN, enableval);
449 dev_err(dev, "Failed to change alarm state\n");
452 bd70528_wdt_unlock(r->parent);
456 static int bd71828_alm_enable(struct device *dev, unsigned int enabled)
459 struct bd70528_rtc *r = dev_get_drvdata(dev);
460 unsigned int enableval = BD70528_MASK_ALM_EN;
465 ret = regmap_update_bits(r->parent->regmap, BD71828_REG_RTC_ALM0_MASK,
466 BD70528_MASK_ALM_EN, enableval);
468 dev_err(dev, "Failed to change alarm state\n");
473 static const struct rtc_class_ops bd70528_rtc_ops = {
474 .read_time = bd70528_get_time,
475 .set_time = bd70528_set_time,
476 .read_alarm = bd70528_read_alarm,
477 .set_alarm = bd70528_set_alarm,
478 .alarm_irq_enable = bd70528_alm_enable,
481 static const struct rtc_class_ops bd71828_rtc_ops = {
482 .read_time = bd70528_get_time,
483 .set_time = bd71828_set_time,
484 .read_alarm = bd71828_read_alarm,
485 .set_alarm = bd71828_set_alarm,
486 .alarm_irq_enable = bd71828_alm_enable,
489 static irqreturn_t alm_hndlr(int irq, void *data)
491 struct rtc_device *rtc = data;
493 rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF | RTC_PF);
497 static int bd70528_probe(struct platform_device *pdev)
499 struct bd70528_rtc *bd_rtc;
500 const struct rtc_class_ops *rtc_ops;
501 struct rohm_regmap_dev *parent;
502 const char *irq_name;
504 struct rtc_device *rtc;
507 bool enable_main_irq = false;
509 enum rohm_chip_type chip = platform_get_device_id(pdev)->driver_data;
511 parent = dev_get_drvdata(pdev->dev.parent);
513 dev_err(&pdev->dev, "No MFD driver data\n");
516 bd_rtc = devm_kzalloc(&pdev->dev, sizeof(*bd_rtc), GFP_KERNEL);
520 bd_rtc->parent = parent;
521 bd_rtc->dev = &pdev->dev;
524 case ROHM_CHIP_TYPE_BD70528:
525 irq_name = "bd70528-rtc-alm";
526 bd_rtc->has_rtc_timers = true;
527 bd_rtc->reg_time_start = BD70528_REG_RTC_START;
528 hour_reg = BD70528_REG_RTC_HOUR;
529 enable_main_irq = true;
530 rtc_ops = &bd70528_rtc_ops;
532 case ROHM_CHIP_TYPE_BD71828:
533 irq_name = "bd71828-rtc-alm-0";
534 bd_rtc->reg_time_start = BD71828_REG_RTC_START;
535 hour_reg = BD71828_REG_RTC_HOUR;
536 rtc_ops = &bd71828_rtc_ops;
539 dev_err(&pdev->dev, "Unknown chip\n");
543 irq = platform_get_irq_byname(pdev, irq_name);
548 platform_set_drvdata(pdev, bd_rtc);
550 ret = regmap_read(parent->regmap, hour_reg, &hr);
553 dev_err(&pdev->dev, "Failed to reag RTC clock\n");
557 if (!(hr & BD70528_MASK_RTC_HOUR_24H)) {
560 ret = rtc_ops->read_time(&pdev->dev, &t);
563 ret = rtc_ops->set_time(&pdev->dev, &t);
567 "Setting 24H clock for RTC failed\n");
572 device_set_wakeup_capable(&pdev->dev, true);
573 device_wakeup_enable(&pdev->dev);
575 rtc = devm_rtc_allocate_device(&pdev->dev);
577 dev_err(&pdev->dev, "RTC device creation failed\n");
581 rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
582 rtc->range_max = RTC_TIMESTAMP_END_2099;
585 /* Request alarm IRQ prior to registerig the RTC */
586 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, &alm_hndlr,
587 IRQF_ONESHOT, "bd70528-rtc", rtc);
592 * BD70528 irq controller is not touching the main mask register.
593 * So enable the RTC block interrupts at main level. We can just
594 * leave them enabled as irq-controller should disable irqs
595 * from sub-registers when IRQ is disabled or freed.
597 if (enable_main_irq) {
598 ret = regmap_update_bits(parent->regmap,
599 BD70528_REG_INT_MAIN_MASK,
600 BD70528_INT_RTC_MASK, 0);
602 dev_err(&pdev->dev, "Failed to enable RTC interrupts\n");
607 return rtc_register_device(rtc);
610 static const struct platform_device_id bd718x7_rtc_id[] = {
611 { "bd70528-rtc", ROHM_CHIP_TYPE_BD70528 },
612 { "bd71828-rtc", ROHM_CHIP_TYPE_BD71828 },
615 MODULE_DEVICE_TABLE(platform, bd718x7_rtc_id);
617 static struct platform_driver bd70528_rtc = {
619 .name = "bd70528-rtc"
621 .probe = bd70528_probe,
622 .id_table = bd718x7_rtc_id,
625 module_platform_driver(bd70528_rtc);
628 MODULE_DESCRIPTION("ROHM BD70528 and BD71828 PMIC RTC driver");
629 MODULE_LICENSE("GPL");
630 MODULE_ALIAS("platform:bd70528-rtc");