interconnect: qcom: Fix Kconfig indentation
[linux/fpc-iii.git] / drivers / rtc / rtc-bd70528.c
blob627037aa66a8071e6ecc8fde41a6d5a9590e348e
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 //
3 // Copyright (C) 2018 ROHM Semiconductors
4 //
5 // RTC driver for ROHM BD70528 PMIC
7 #include <linux/bcd.h>
8 #include <linux/mfd/rohm-bd70528.h>
9 #include <linux/module.h>
10 #include <linux/of.h>
11 #include <linux/platform_device.h>
12 #include <linux/regmap.h>
13 #include <linux/rtc.h>
16 * We read regs RTC_SEC => RTC_YEAR
17 * this struct is ordered according to chip registers.
18 * Keep it u8 only to avoid padding issues.
20 struct bd70528_rtc_day {
21 u8 sec;
22 u8 min;
23 u8 hour;
24 } __packed;
26 struct bd70528_rtc_data {
27 struct bd70528_rtc_day time;
28 u8 week;
29 u8 day;
30 u8 month;
31 u8 year;
32 } __packed;
34 struct bd70528_rtc_wake {
35 struct bd70528_rtc_day time;
36 u8 ctrl;
37 } __packed;
39 struct bd70528_rtc_alm {
40 struct bd70528_rtc_data data;
41 u8 alm_mask;
42 u8 alm_repeat;
43 } __packed;
45 struct bd70528_rtc {
46 struct rohm_regmap_dev *mfd;
47 struct device *dev;
50 static int bd70528_set_wake(struct rohm_regmap_dev *bd70528,
51 int enable, int *old_state)
53 int ret;
54 unsigned int ctrl_reg;
56 ret = regmap_read(bd70528->regmap, BD70528_REG_WAKE_EN, &ctrl_reg);
57 if (ret)
58 return ret;
60 if (old_state) {
61 if (ctrl_reg & BD70528_MASK_WAKE_EN)
62 *old_state |= BD70528_WAKE_STATE_BIT;
63 else
64 *old_state &= ~BD70528_WAKE_STATE_BIT;
66 if (!enable == !(*old_state & BD70528_WAKE_STATE_BIT))
67 return 0;
70 if (enable)
71 ctrl_reg |= BD70528_MASK_WAKE_EN;
72 else
73 ctrl_reg &= ~BD70528_MASK_WAKE_EN;
75 return regmap_write(bd70528->regmap, BD70528_REG_WAKE_EN,
76 ctrl_reg);
79 static int bd70528_set_elapsed_tmr(struct rohm_regmap_dev *bd70528,
80 int enable, int *old_state)
82 int ret;
83 unsigned int ctrl_reg;
86 * TBD
87 * What is the purpose of elapsed timer ?
88 * Is the timeout registers counting down, or is the disable - re-enable
89 * going to restart the elapsed-time counting? If counting is restarted
90 * the timeout should be decreased by the amount of time that has
91 * elapsed since starting the timer. Maybe we should store the monotonic
92 * clock value when timer is started so that if RTC is set while timer
93 * is armed we could do the compensation. This is a hack if RTC/system
94 * clk are drifting. OTOH, RTC controlled via I2C is in any case
95 * inaccurate...
97 ret = regmap_read(bd70528->regmap, BD70528_REG_ELAPSED_TIMER_EN,
98 &ctrl_reg);
99 if (ret)
100 return ret;
102 if (old_state) {
103 if (ctrl_reg & BD70528_MASK_ELAPSED_TIMER_EN)
104 *old_state |= BD70528_ELAPSED_STATE_BIT;
105 else
106 *old_state &= ~BD70528_ELAPSED_STATE_BIT;
108 if ((!enable) == (!(*old_state & BD70528_ELAPSED_STATE_BIT)))
109 return 0;
112 if (enable)
113 ctrl_reg |= BD70528_MASK_ELAPSED_TIMER_EN;
114 else
115 ctrl_reg &= ~BD70528_MASK_ELAPSED_TIMER_EN;
117 return regmap_write(bd70528->regmap, BD70528_REG_ELAPSED_TIMER_EN,
118 ctrl_reg);
121 static int bd70528_set_rtc_based_timers(struct bd70528_rtc *r, int new_state,
122 int *old_state)
124 int ret;
126 ret = bd70528_wdt_set(r->mfd, new_state & BD70528_WDT_STATE_BIT,
127 old_state);
128 if (ret) {
129 dev_err(r->dev,
130 "Failed to disable WDG for RTC setting (%d)\n", ret);
131 return ret;
133 ret = bd70528_set_elapsed_tmr(r->mfd,
134 new_state & BD70528_ELAPSED_STATE_BIT,
135 old_state);
136 if (ret) {
137 dev_err(r->dev,
138 "Failed to disable 'elapsed timer' for RTC setting\n");
139 return ret;
141 ret = bd70528_set_wake(r->mfd, new_state & BD70528_WAKE_STATE_BIT,
142 old_state);
143 if (ret) {
144 dev_err(r->dev,
145 "Failed to disable 'wake timer' for RTC setting\n");
146 return ret;
149 return ret;
152 static int bd70528_re_enable_rtc_based_timers(struct bd70528_rtc *r,
153 int old_state)
155 return bd70528_set_rtc_based_timers(r, old_state, NULL);
158 static int bd70528_disable_rtc_based_timers(struct bd70528_rtc *r,
159 int *old_state)
161 return bd70528_set_rtc_based_timers(r, 0, old_state);
164 static inline void tmday2rtc(struct rtc_time *t, struct bd70528_rtc_day *d)
166 d->sec &= ~BD70528_MASK_RTC_SEC;
167 d->min &= ~BD70528_MASK_RTC_MINUTE;
168 d->hour &= ~BD70528_MASK_RTC_HOUR;
169 d->sec |= bin2bcd(t->tm_sec);
170 d->min |= bin2bcd(t->tm_min);
171 d->hour |= bin2bcd(t->tm_hour);
174 static inline void tm2rtc(struct rtc_time *t, struct bd70528_rtc_data *r)
176 r->day &= ~BD70528_MASK_RTC_DAY;
177 r->week &= ~BD70528_MASK_RTC_WEEK;
178 r->month &= ~BD70528_MASK_RTC_MONTH;
180 * PM and 24H bits are not used by Wake - thus we clear them
181 * here and not in tmday2rtc() which is also used by wake.
183 r->time.hour &= ~(BD70528_MASK_RTC_HOUR_PM | BD70528_MASK_RTC_HOUR_24H);
185 tmday2rtc(t, &r->time);
187 * We do always set time in 24H mode.
189 r->time.hour |= BD70528_MASK_RTC_HOUR_24H;
190 r->day |= bin2bcd(t->tm_mday);
191 r->week |= bin2bcd(t->tm_wday);
192 r->month |= bin2bcd(t->tm_mon + 1);
193 r->year = bin2bcd(t->tm_year - 100);
196 static inline void rtc2tm(struct bd70528_rtc_data *r, struct rtc_time *t)
198 t->tm_sec = bcd2bin(r->time.sec & BD70528_MASK_RTC_SEC);
199 t->tm_min = bcd2bin(r->time.min & BD70528_MASK_RTC_MINUTE);
200 t->tm_hour = bcd2bin(r->time.hour & BD70528_MASK_RTC_HOUR);
202 * If RTC is in 12H mode, then bit BD70528_MASK_RTC_HOUR_PM
203 * is not BCD value but tells whether it is AM or PM
205 if (!(r->time.hour & BD70528_MASK_RTC_HOUR_24H)) {
206 t->tm_hour %= 12;
207 if (r->time.hour & BD70528_MASK_RTC_HOUR_PM)
208 t->tm_hour += 12;
210 t->tm_mday = bcd2bin(r->day & BD70528_MASK_RTC_DAY);
211 t->tm_mon = bcd2bin(r->month & BD70528_MASK_RTC_MONTH) - 1;
212 t->tm_year = 100 + bcd2bin(r->year & BD70528_MASK_RTC_YEAR);
213 t->tm_wday = bcd2bin(r->week & BD70528_MASK_RTC_WEEK);
216 static int bd70528_set_alarm(struct device *dev, struct rtc_wkalrm *a)
218 struct bd70528_rtc_wake wake;
219 struct bd70528_rtc_alm alm;
220 int ret;
221 struct bd70528_rtc *r = dev_get_drvdata(dev);
222 struct rohm_regmap_dev *bd70528 = r->mfd;
224 ret = regmap_bulk_read(bd70528->regmap, BD70528_REG_RTC_WAKE_START,
225 &wake, sizeof(wake));
226 if (ret) {
227 dev_err(dev, "Failed to read wake regs\n");
228 return ret;
231 ret = regmap_bulk_read(bd70528->regmap, BD70528_REG_RTC_ALM_START,
232 &alm, sizeof(alm));
233 if (ret) {
234 dev_err(dev, "Failed to read alarm regs\n");
235 return ret;
238 tm2rtc(&a->time, &alm.data);
239 tmday2rtc(&a->time, &wake.time);
241 if (a->enabled) {
242 alm.alm_mask &= ~BD70528_MASK_ALM_EN;
243 wake.ctrl |= BD70528_MASK_WAKE_EN;
244 } else {
245 alm.alm_mask |= BD70528_MASK_ALM_EN;
246 wake.ctrl &= ~BD70528_MASK_WAKE_EN;
249 ret = regmap_bulk_write(bd70528->regmap,
250 BD70528_REG_RTC_WAKE_START, &wake,
251 sizeof(wake));
252 if (ret) {
253 dev_err(dev, "Failed to set wake time\n");
254 return ret;
256 ret = regmap_bulk_write(bd70528->regmap, BD70528_REG_RTC_ALM_START,
257 &alm, sizeof(alm));
258 if (ret)
259 dev_err(dev, "Failed to set alarm time\n");
261 return ret;
264 static int bd70528_read_alarm(struct device *dev, struct rtc_wkalrm *a)
266 struct bd70528_rtc_alm alm;
267 int ret;
268 struct bd70528_rtc *r = dev_get_drvdata(dev);
269 struct rohm_regmap_dev *bd70528 = r->mfd;
271 ret = regmap_bulk_read(bd70528->regmap, BD70528_REG_RTC_ALM_START,
272 &alm, sizeof(alm));
273 if (ret) {
274 dev_err(dev, "Failed to read alarm regs\n");
275 return ret;
278 rtc2tm(&alm.data, &a->time);
279 a->time.tm_mday = -1;
280 a->time.tm_mon = -1;
281 a->time.tm_year = -1;
282 a->enabled = !(alm.alm_mask & BD70528_MASK_ALM_EN);
283 a->pending = 0;
285 return 0;
288 static int bd70528_set_time_locked(struct device *dev, struct rtc_time *t)
290 int ret, tmpret, old_states;
291 struct bd70528_rtc_data rtc_data;
292 struct bd70528_rtc *r = dev_get_drvdata(dev);
293 struct rohm_regmap_dev *bd70528 = r->mfd;
295 ret = bd70528_disable_rtc_based_timers(r, &old_states);
296 if (ret)
297 return ret;
299 tmpret = regmap_bulk_read(bd70528->regmap,
300 BD70528_REG_RTC_START, &rtc_data,
301 sizeof(rtc_data));
302 if (tmpret) {
303 dev_err(dev, "Failed to read RTC time registers\n");
304 goto renable_out;
306 tm2rtc(t, &rtc_data);
308 tmpret = regmap_bulk_write(bd70528->regmap,
309 BD70528_REG_RTC_START, &rtc_data,
310 sizeof(rtc_data));
311 if (tmpret) {
312 dev_err(dev, "Failed to set RTC time\n");
313 goto renable_out;
316 renable_out:
317 ret = bd70528_re_enable_rtc_based_timers(r, old_states);
318 if (tmpret)
319 ret = tmpret;
321 return ret;
324 static int bd70528_set_time(struct device *dev, struct rtc_time *t)
326 int ret;
327 struct bd70528_rtc *r = dev_get_drvdata(dev);
329 bd70528_wdt_lock(r->mfd);
330 ret = bd70528_set_time_locked(dev, t);
331 bd70528_wdt_unlock(r->mfd);
332 return ret;
335 static int bd70528_get_time(struct device *dev, struct rtc_time *t)
337 struct bd70528_rtc *r = dev_get_drvdata(dev);
338 struct rohm_regmap_dev *bd70528 = r->mfd;
339 struct bd70528_rtc_data rtc_data;
340 int ret;
342 /* read the RTC date and time registers all at once */
343 ret = regmap_bulk_read(bd70528->regmap,
344 BD70528_REG_RTC_START, &rtc_data,
345 sizeof(rtc_data));
346 if (ret) {
347 dev_err(dev, "Failed to read RTC time (err %d)\n", ret);
348 return ret;
351 rtc2tm(&rtc_data, t);
353 return 0;
356 static int bd70528_alm_enable(struct device *dev, unsigned int enabled)
358 int ret;
359 unsigned int enableval = BD70528_MASK_ALM_EN;
360 struct bd70528_rtc *r = dev_get_drvdata(dev);
362 if (enabled)
363 enableval = 0;
365 bd70528_wdt_lock(r->mfd);
366 ret = bd70528_set_wake(r->mfd, enabled, NULL);
367 if (ret) {
368 dev_err(dev, "Failed to change wake state\n");
369 goto out_unlock;
371 ret = regmap_update_bits(r->mfd->regmap, BD70528_REG_RTC_ALM_MASK,
372 BD70528_MASK_ALM_EN, enableval);
373 if (ret)
374 dev_err(dev, "Failed to change alarm state\n");
376 out_unlock:
377 bd70528_wdt_unlock(r->mfd);
378 return ret;
381 static const struct rtc_class_ops bd70528_rtc_ops = {
382 .read_time = bd70528_get_time,
383 .set_time = bd70528_set_time,
384 .read_alarm = bd70528_read_alarm,
385 .set_alarm = bd70528_set_alarm,
386 .alarm_irq_enable = bd70528_alm_enable,
389 static irqreturn_t alm_hndlr(int irq, void *data)
391 struct rtc_device *rtc = data;
393 rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF | RTC_PF);
394 return IRQ_HANDLED;
397 static int bd70528_probe(struct platform_device *pdev)
399 struct bd70528_rtc *bd_rtc;
400 struct rohm_regmap_dev *mfd;
401 int ret;
402 struct rtc_device *rtc;
403 int irq;
404 unsigned int hr;
406 mfd = dev_get_drvdata(pdev->dev.parent);
407 if (!mfd) {
408 dev_err(&pdev->dev, "No MFD driver data\n");
409 return -EINVAL;
411 bd_rtc = devm_kzalloc(&pdev->dev, sizeof(*bd_rtc), GFP_KERNEL);
412 if (!bd_rtc)
413 return -ENOMEM;
415 bd_rtc->mfd = mfd;
416 bd_rtc->dev = &pdev->dev;
418 irq = platform_get_irq_byname(pdev, "bd70528-rtc-alm");
419 if (irq < 0)
420 return irq;
422 platform_set_drvdata(pdev, bd_rtc);
424 ret = regmap_read(mfd->regmap, BD70528_REG_RTC_HOUR, &hr);
426 if (ret) {
427 dev_err(&pdev->dev, "Failed to reag RTC clock\n");
428 return ret;
431 if (!(hr & BD70528_MASK_RTC_HOUR_24H)) {
432 struct rtc_time t;
434 ret = bd70528_get_time(&pdev->dev, &t);
436 if (!ret)
437 ret = bd70528_set_time(&pdev->dev, &t);
439 if (ret) {
440 dev_err(&pdev->dev,
441 "Setting 24H clock for RTC failed\n");
442 return ret;
446 device_set_wakeup_capable(&pdev->dev, true);
447 device_wakeup_enable(&pdev->dev);
449 rtc = devm_rtc_allocate_device(&pdev->dev);
450 if (IS_ERR(rtc)) {
451 dev_err(&pdev->dev, "RTC device creation failed\n");
452 return PTR_ERR(rtc);
455 rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
456 rtc->range_max = RTC_TIMESTAMP_END_2099;
457 rtc->ops = &bd70528_rtc_ops;
459 /* Request alarm IRQ prior to registerig the RTC */
460 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, &alm_hndlr,
461 IRQF_ONESHOT, "bd70528-rtc", rtc);
462 if (ret)
463 return ret;
466 * BD70528 irq controller is not touching the main mask register.
467 * So enable the RTC block interrupts at main level. We can just
468 * leave them enabled as irq-controller should disable irqs
469 * from sub-registers when IRQ is disabled or freed.
471 ret = regmap_update_bits(mfd->regmap,
472 BD70528_REG_INT_MAIN_MASK,
473 BD70528_INT_RTC_MASK, 0);
474 if (ret) {
475 dev_err(&pdev->dev, "Failed to enable RTC interrupts\n");
476 return ret;
479 return rtc_register_device(rtc);
482 static struct platform_driver bd70528_rtc = {
483 .driver = {
484 .name = "bd70528-rtc"
486 .probe = bd70528_probe,
489 module_platform_driver(bd70528_rtc);
491 MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
492 MODULE_DESCRIPTION("BD70528 RTC driver");
493 MODULE_LICENSE("GPL");
494 MODULE_ALIAS("platform:bd70528-rtc");