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-bd71815.h>
10 #include <linux/mfd/rohm-bd71828.h>
11 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/regmap.h>
15 #include <linux/rtc.h>
18 * On BD71828 and BD71815 the ALM0 MASK is 14 bytes after the ALM0
21 #define BD718XX_ALM_EN_OFFSET 14
24 * We read regs RTC_SEC => RTC_YEAR
25 * this struct is ordered according to chip registers.
26 * Keep it u8 only (or packed) to avoid padding issues.
28 struct bd70528_rtc_day {
34 struct bd70528_rtc_data {
35 struct bd70528_rtc_day time;
42 struct bd70528_rtc_wake {
43 struct bd70528_rtc_day time;
47 struct bd71828_rtc_alm {
48 struct bd70528_rtc_data alm0;
49 struct bd70528_rtc_data alm1;
54 struct bd70528_rtc_alm {
55 struct bd70528_rtc_data data;
61 struct rohm_regmap_dev *parent;
62 struct regmap *regmap;
65 u8 bd718xx_alm_block_start;
69 static int bd70528_set_wake(struct rohm_regmap_dev *bd70528,
70 int enable, int *old_state)
73 unsigned int ctrl_reg;
75 ret = regmap_read(bd70528->regmap, BD70528_REG_WAKE_EN, &ctrl_reg);
80 if (ctrl_reg & BD70528_MASK_WAKE_EN)
81 *old_state |= BD70528_WAKE_STATE_BIT;
83 *old_state &= ~BD70528_WAKE_STATE_BIT;
85 if (!enable == !(*old_state & BD70528_WAKE_STATE_BIT))
90 ctrl_reg |= BD70528_MASK_WAKE_EN;
92 ctrl_reg &= ~BD70528_MASK_WAKE_EN;
94 return regmap_write(bd70528->regmap, BD70528_REG_WAKE_EN,
98 static int bd70528_set_elapsed_tmr(struct rohm_regmap_dev *bd70528,
99 int enable, int *old_state)
102 unsigned int ctrl_reg;
106 * What is the purpose of elapsed timer ?
107 * Is the timeout registers counting down, or is the disable - re-enable
108 * going to restart the elapsed-time counting? If counting is restarted
109 * the timeout should be decreased by the amount of time that has
110 * elapsed since starting the timer. Maybe we should store the monotonic
111 * clock value when timer is started so that if RTC is set while timer
112 * is armed we could do the compensation. This is a hack if RTC/system
113 * clk are drifting. OTOH, RTC controlled via I2C is in any case
116 ret = regmap_read(bd70528->regmap, BD70528_REG_ELAPSED_TIMER_EN,
122 if (ctrl_reg & BD70528_MASK_ELAPSED_TIMER_EN)
123 *old_state |= BD70528_ELAPSED_STATE_BIT;
125 *old_state &= ~BD70528_ELAPSED_STATE_BIT;
127 if ((!enable) == (!(*old_state & BD70528_ELAPSED_STATE_BIT)))
132 ctrl_reg |= BD70528_MASK_ELAPSED_TIMER_EN;
134 ctrl_reg &= ~BD70528_MASK_ELAPSED_TIMER_EN;
136 return regmap_write(bd70528->regmap, BD70528_REG_ELAPSED_TIMER_EN,
140 static int bd70528_set_rtc_based_timers(struct bd70528_rtc *r, int new_state,
145 ret = bd70528_wdt_set(r->parent, new_state & BD70528_WDT_STATE_BIT,
149 "Failed to disable WDG for RTC setting (%d)\n", ret);
152 ret = bd70528_set_elapsed_tmr(r->parent,
153 new_state & BD70528_ELAPSED_STATE_BIT,
157 "Failed to disable 'elapsed timer' for RTC setting\n");
160 ret = bd70528_set_wake(r->parent, new_state & BD70528_WAKE_STATE_BIT,
164 "Failed to disable 'wake timer' for RTC setting\n");
171 static int bd70528_re_enable_rtc_based_timers(struct bd70528_rtc *r,
174 if (!r->has_rtc_timers)
177 return bd70528_set_rtc_based_timers(r, old_state, NULL);
180 static int bd70528_disable_rtc_based_timers(struct bd70528_rtc *r,
183 if (!r->has_rtc_timers)
186 return bd70528_set_rtc_based_timers(r, 0, old_state);
189 static inline void tmday2rtc(struct rtc_time *t, struct bd70528_rtc_day *d)
191 d->sec &= ~BD70528_MASK_RTC_SEC;
192 d->min &= ~BD70528_MASK_RTC_MINUTE;
193 d->hour &= ~BD70528_MASK_RTC_HOUR;
194 d->sec |= bin2bcd(t->tm_sec);
195 d->min |= bin2bcd(t->tm_min);
196 d->hour |= bin2bcd(t->tm_hour);
199 static inline void tm2rtc(struct rtc_time *t, struct bd70528_rtc_data *r)
201 r->day &= ~BD70528_MASK_RTC_DAY;
202 r->week &= ~BD70528_MASK_RTC_WEEK;
203 r->month &= ~BD70528_MASK_RTC_MONTH;
205 * PM and 24H bits are not used by Wake - thus we clear them
206 * here and not in tmday2rtc() which is also used by wake.
208 r->time.hour &= ~(BD70528_MASK_RTC_HOUR_PM | BD70528_MASK_RTC_HOUR_24H);
210 tmday2rtc(t, &r->time);
212 * We do always set time in 24H mode.
214 r->time.hour |= BD70528_MASK_RTC_HOUR_24H;
215 r->day |= bin2bcd(t->tm_mday);
216 r->week |= bin2bcd(t->tm_wday);
217 r->month |= bin2bcd(t->tm_mon + 1);
218 r->year = bin2bcd(t->tm_year - 100);
221 static inline void rtc2tm(struct bd70528_rtc_data *r, struct rtc_time *t)
223 t->tm_sec = bcd2bin(r->time.sec & BD70528_MASK_RTC_SEC);
224 t->tm_min = bcd2bin(r->time.min & BD70528_MASK_RTC_MINUTE);
225 t->tm_hour = bcd2bin(r->time.hour & BD70528_MASK_RTC_HOUR);
227 * If RTC is in 12H mode, then bit BD70528_MASK_RTC_HOUR_PM
228 * is not BCD value but tells whether it is AM or PM
230 if (!(r->time.hour & BD70528_MASK_RTC_HOUR_24H)) {
232 if (r->time.hour & BD70528_MASK_RTC_HOUR_PM)
235 t->tm_mday = bcd2bin(r->day & BD70528_MASK_RTC_DAY);
236 t->tm_mon = bcd2bin(r->month & BD70528_MASK_RTC_MONTH) - 1;
237 t->tm_year = 100 + bcd2bin(r->year & BD70528_MASK_RTC_YEAR);
238 t->tm_wday = bcd2bin(r->week & BD70528_MASK_RTC_WEEK);
241 static int bd71828_set_alarm(struct device *dev, struct rtc_wkalrm *a)
244 struct bd71828_rtc_alm alm;
245 struct bd70528_rtc *r = dev_get_drvdata(dev);
247 ret = regmap_bulk_read(r->regmap, r->bd718xx_alm_block_start, &alm,
250 dev_err(dev, "Failed to read alarm regs\n");
254 tm2rtc(&a->time, &alm.alm0);
257 alm.alm_mask &= ~BD70528_MASK_ALM_EN;
259 alm.alm_mask |= BD70528_MASK_ALM_EN;
261 ret = regmap_bulk_write(r->regmap, r->bd718xx_alm_block_start, &alm,
264 dev_err(dev, "Failed to set alarm time\n");
270 static int bd70528_set_alarm(struct device *dev, struct rtc_wkalrm *a)
272 struct bd70528_rtc_wake wake;
273 struct bd70528_rtc_alm alm;
275 struct bd70528_rtc *r = dev_get_drvdata(dev);
277 ret = regmap_bulk_read(r->regmap, BD70528_REG_RTC_WAKE_START, &wake,
280 dev_err(dev, "Failed to read wake regs\n");
284 ret = regmap_bulk_read(r->regmap, BD70528_REG_RTC_ALM_START, &alm,
287 dev_err(dev, "Failed to read alarm regs\n");
291 tm2rtc(&a->time, &alm.data);
292 tmday2rtc(&a->time, &wake.time);
295 alm.alm_mask &= ~BD70528_MASK_ALM_EN;
296 wake.ctrl |= BD70528_MASK_WAKE_EN;
298 alm.alm_mask |= BD70528_MASK_ALM_EN;
299 wake.ctrl &= ~BD70528_MASK_WAKE_EN;
302 ret = regmap_bulk_write(r->regmap, BD70528_REG_RTC_WAKE_START, &wake,
305 dev_err(dev, "Failed to set wake time\n");
308 ret = regmap_bulk_write(r->regmap, BD70528_REG_RTC_ALM_START, &alm,
311 dev_err(dev, "Failed to set alarm time\n");
316 static int bd71828_read_alarm(struct device *dev, struct rtc_wkalrm *a)
319 struct bd71828_rtc_alm alm;
320 struct bd70528_rtc *r = dev_get_drvdata(dev);
322 ret = regmap_bulk_read(r->regmap, r->bd718xx_alm_block_start, &alm,
325 dev_err(dev, "Failed to read alarm regs\n");
329 rtc2tm(&alm.alm0, &a->time);
330 a->time.tm_mday = -1;
332 a->time.tm_year = -1;
333 a->enabled = !!(alm.alm_mask & BD70528_MASK_ALM_EN);
339 static int bd70528_read_alarm(struct device *dev, struct rtc_wkalrm *a)
341 struct bd70528_rtc_alm alm;
343 struct bd70528_rtc *r = dev_get_drvdata(dev);
345 ret = regmap_bulk_read(r->regmap, BD70528_REG_RTC_ALM_START, &alm,
348 dev_err(dev, "Failed to read alarm regs\n");
352 rtc2tm(&alm.data, &a->time);
353 a->time.tm_mday = -1;
355 a->time.tm_year = -1;
356 a->enabled = !(alm.alm_mask & BD70528_MASK_ALM_EN);
362 static int bd70528_set_time_locked(struct device *dev, struct rtc_time *t)
364 int ret, tmpret, old_states;
365 struct bd70528_rtc_data rtc_data;
366 struct bd70528_rtc *r = dev_get_drvdata(dev);
368 ret = bd70528_disable_rtc_based_timers(r, &old_states);
372 tmpret = regmap_bulk_read(r->regmap, r->reg_time_start, &rtc_data,
375 dev_err(dev, "Failed to read RTC time registers\n");
378 tm2rtc(t, &rtc_data);
380 tmpret = regmap_bulk_write(r->regmap, r->reg_time_start, &rtc_data,
383 dev_err(dev, "Failed to set RTC time\n");
388 ret = bd70528_re_enable_rtc_based_timers(r, old_states);
395 static int bd71828_set_time(struct device *dev, struct rtc_time *t)
397 return bd70528_set_time_locked(dev, t);
400 static int bd70528_set_time(struct device *dev, struct rtc_time *t)
403 struct bd70528_rtc *r = dev_get_drvdata(dev);
405 bd70528_wdt_lock(r->parent);
406 ret = bd70528_set_time_locked(dev, t);
407 bd70528_wdt_unlock(r->parent);
411 static int bd70528_get_time(struct device *dev, struct rtc_time *t)
413 struct bd70528_rtc *r = dev_get_drvdata(dev);
414 struct bd70528_rtc_data rtc_data;
417 /* read the RTC date and time registers all at once */
418 ret = regmap_bulk_read(r->regmap, r->reg_time_start, &rtc_data,
421 dev_err(dev, "Failed to read RTC time (err %d)\n", ret);
425 rtc2tm(&rtc_data, t);
430 static int bd70528_alm_enable(struct device *dev, unsigned int enabled)
433 unsigned int enableval = BD70528_MASK_ALM_EN;
434 struct bd70528_rtc *r = dev_get_drvdata(dev);
439 bd70528_wdt_lock(r->parent);
440 ret = bd70528_set_wake(r->parent, enabled, NULL);
442 dev_err(dev, "Failed to change wake state\n");
445 ret = regmap_update_bits(r->regmap, BD70528_REG_RTC_ALM_MASK,
446 BD70528_MASK_ALM_EN, enableval);
448 dev_err(dev, "Failed to change alarm state\n");
451 bd70528_wdt_unlock(r->parent);
455 static int bd71828_alm_enable(struct device *dev, unsigned int enabled)
458 struct bd70528_rtc *r = dev_get_drvdata(dev);
459 unsigned int enableval = BD70528_MASK_ALM_EN;
464 ret = regmap_update_bits(r->regmap, r->bd718xx_alm_block_start +
465 BD718XX_ALM_EN_OFFSET, BD70528_MASK_ALM_EN,
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 const char *irq_name;
503 struct rtc_device *rtc;
506 bool enable_main_irq = false;
508 enum rohm_chip_type chip = platform_get_device_id(pdev)->driver_data;
510 bd_rtc = devm_kzalloc(&pdev->dev, sizeof(*bd_rtc), GFP_KERNEL);
514 bd_rtc->regmap = dev_get_regmap(pdev->dev.parent, NULL);
515 if (!bd_rtc->regmap) {
516 dev_err(&pdev->dev, "No regmap\n");
520 bd_rtc->dev = &pdev->dev;
523 case ROHM_CHIP_TYPE_BD70528:
524 bd_rtc->parent = dev_get_drvdata(pdev->dev.parent);
525 if (!bd_rtc->parent) {
526 dev_err(&pdev->dev, "No MFD data\n");
529 irq_name = "bd70528-rtc-alm";
530 bd_rtc->has_rtc_timers = true;
531 bd_rtc->reg_time_start = BD70528_REG_RTC_START;
532 hour_reg = BD70528_REG_RTC_HOUR;
533 enable_main_irq = true;
534 rtc_ops = &bd70528_rtc_ops;
536 case ROHM_CHIP_TYPE_BD71815:
537 irq_name = "bd71815-rtc-alm-0";
538 bd_rtc->reg_time_start = BD71815_REG_RTC_START;
541 * See also BD718XX_ALM_EN_OFFSET:
542 * This works for BD71828 and BD71815 as they have same offset
543 * between ALM0 start and ALM0_MASK. If new ICs are to be
544 * added this requires proper check as ALM0_MASK is not located
545 * at the end of ALM0 block - but after all ALM blocks so if
546 * amount of ALMs differ the offset to enable/disable is likely
547 * to be incorrect and enable/disable must be given as own
550 bd_rtc->bd718xx_alm_block_start = BD71815_REG_RTC_ALM_START;
551 hour_reg = BD71815_REG_HOUR;
552 rtc_ops = &bd71828_rtc_ops;
554 case ROHM_CHIP_TYPE_BD71828:
555 irq_name = "bd71828-rtc-alm-0";
556 bd_rtc->reg_time_start = BD71828_REG_RTC_START;
557 bd_rtc->bd718xx_alm_block_start = BD71828_REG_RTC_ALM_START;
558 hour_reg = BD71828_REG_RTC_HOUR;
559 rtc_ops = &bd71828_rtc_ops;
562 dev_err(&pdev->dev, "Unknown chip\n");
566 irq = platform_get_irq_byname(pdev, irq_name);
571 platform_set_drvdata(pdev, bd_rtc);
573 ret = regmap_read(bd_rtc->regmap, hour_reg, &hr);
576 dev_err(&pdev->dev, "Failed to reag RTC clock\n");
580 if (!(hr & BD70528_MASK_RTC_HOUR_24H)) {
583 ret = rtc_ops->read_time(&pdev->dev, &t);
586 ret = rtc_ops->set_time(&pdev->dev, &t);
590 "Setting 24H clock for RTC failed\n");
595 device_set_wakeup_capable(&pdev->dev, true);
596 device_wakeup_enable(&pdev->dev);
598 rtc = devm_rtc_allocate_device(&pdev->dev);
600 dev_err(&pdev->dev, "RTC device creation failed\n");
604 rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
605 rtc->range_max = RTC_TIMESTAMP_END_2099;
608 /* Request alarm IRQ prior to registerig the RTC */
609 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, &alm_hndlr,
610 IRQF_ONESHOT, "bd70528-rtc", rtc);
615 * BD70528 irq controller is not touching the main mask register.
616 * So enable the RTC block interrupts at main level. We can just
617 * leave them enabled as irq-controller should disable irqs
618 * from sub-registers when IRQ is disabled or freed.
620 if (enable_main_irq) {
621 ret = regmap_update_bits(bd_rtc->regmap,
622 BD70528_REG_INT_MAIN_MASK,
623 BD70528_INT_RTC_MASK, 0);
625 dev_err(&pdev->dev, "Failed to enable RTC interrupts\n");
630 return devm_rtc_register_device(rtc);
633 static const struct platform_device_id bd718x7_rtc_id[] = {
634 { "bd70528-rtc", ROHM_CHIP_TYPE_BD70528 },
635 { "bd71828-rtc", ROHM_CHIP_TYPE_BD71828 },
636 { "bd71815-rtc", ROHM_CHIP_TYPE_BD71815 },
639 MODULE_DEVICE_TABLE(platform, bd718x7_rtc_id);
641 static struct platform_driver bd70528_rtc = {
643 .name = "bd70528-rtc"
645 .probe = bd70528_probe,
646 .id_table = bd718x7_rtc_id,
649 module_platform_driver(bd70528_rtc);
651 MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
652 MODULE_DESCRIPTION("ROHM BD70528 and BD71828 PMIC RTC driver");
653 MODULE_LICENSE("GPL");
654 MODULE_ALIAS("platform:bd70528-rtc");