Linux 6.13-rc4
[linux.git] / drivers / rtc / rtc-da9063.c
blob859397541f2983fb32163b780245554e0e84084a
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Real time clock device driver for DA9063
4 * Copyright (C) 2013-2015 Dialog Semiconductor Ltd.
5 */
7 #include <linux/delay.h>
8 #include <linux/init.h>
9 #include <linux/interrupt.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/of.h>
13 #include <linux/platform_device.h>
14 #include <linux/pm_wakeirq.h>
15 #include <linux/regmap.h>
16 #include <linux/rtc.h>
17 #include <linux/slab.h>
19 #include <linux/mfd/da9062/registers.h>
20 #include <linux/mfd/da9063/registers.h>
21 #include <linux/mfd/da9063/core.h>
23 #define YEARS_TO_DA9063(year) ((year) - 100)
24 #define MONTHS_TO_DA9063(month) ((month) + 1)
25 #define YEARS_FROM_DA9063(year) ((year) + 100)
26 #define MONTHS_FROM_DA9063(month) ((month) - 1)
28 enum {
29 RTC_SEC = 0,
30 RTC_MIN = 1,
31 RTC_HOUR = 2,
32 RTC_DAY = 3,
33 RTC_MONTH = 4,
34 RTC_YEAR = 5,
35 RTC_DATA_LEN
38 struct da9063_compatible_rtc_regmap {
39 /* REGS */
40 int rtc_enable_reg;
41 int rtc_enable_32k_crystal_reg;
42 int rtc_alarm_secs_reg;
43 int rtc_alarm_year_reg;
44 int rtc_count_secs_reg;
45 int rtc_count_year_reg;
46 int rtc_event_reg;
47 /* MASKS */
48 int rtc_enable_mask;
49 int rtc_crystal_mask;
50 int rtc_event_alarm_mask;
51 int rtc_alarm_on_mask;
52 int rtc_alarm_status_mask;
53 int rtc_tick_on_mask;
54 int rtc_ready_to_read_mask;
55 int rtc_count_sec_mask;
56 int rtc_count_min_mask;
57 int rtc_count_hour_mask;
58 int rtc_count_day_mask;
59 int rtc_count_month_mask;
60 int rtc_count_year_mask;
61 /* ALARM CONFIG */
62 int rtc_data_start;
63 int rtc_alarm_len;
66 struct da9063_compatible_rtc {
67 struct rtc_device *rtc_dev;
68 struct rtc_time alarm_time;
69 struct regmap *regmap;
70 const struct da9063_compatible_rtc_regmap *config;
71 bool rtc_sync;
74 static const struct da9063_compatible_rtc_regmap da9063_ad_regs = {
75 /* REGS */
76 .rtc_enable_reg = DA9063_REG_CONTROL_E,
77 .rtc_alarm_secs_reg = DA9063_AD_REG_ALARM_MI,
78 .rtc_alarm_year_reg = DA9063_AD_REG_ALARM_Y,
79 .rtc_count_secs_reg = DA9063_REG_COUNT_S,
80 .rtc_count_year_reg = DA9063_REG_COUNT_Y,
81 .rtc_event_reg = DA9063_REG_EVENT_A,
82 /* MASKS */
83 .rtc_enable_mask = DA9063_RTC_EN,
84 .rtc_crystal_mask = DA9063_CRYSTAL,
85 .rtc_enable_32k_crystal_reg = DA9063_REG_EN_32K,
86 .rtc_event_alarm_mask = DA9063_E_ALARM,
87 .rtc_alarm_on_mask = DA9063_ALARM_ON,
88 .rtc_alarm_status_mask = DA9063_ALARM_STATUS_ALARM |
89 DA9063_ALARM_STATUS_TICK,
90 .rtc_tick_on_mask = DA9063_TICK_ON,
91 .rtc_ready_to_read_mask = DA9063_RTC_READ,
92 .rtc_count_sec_mask = DA9063_COUNT_SEC_MASK,
93 .rtc_count_min_mask = DA9063_COUNT_MIN_MASK,
94 .rtc_count_hour_mask = DA9063_COUNT_HOUR_MASK,
95 .rtc_count_day_mask = DA9063_COUNT_DAY_MASK,
96 .rtc_count_month_mask = DA9063_COUNT_MONTH_MASK,
97 .rtc_count_year_mask = DA9063_COUNT_YEAR_MASK,
98 /* ALARM CONFIG */
99 .rtc_data_start = RTC_MIN,
100 .rtc_alarm_len = RTC_DATA_LEN - 1,
103 static const struct da9063_compatible_rtc_regmap da9063_bb_regs = {
104 /* REGS */
105 .rtc_enable_reg = DA9063_REG_CONTROL_E,
106 .rtc_alarm_secs_reg = DA9063_BB_REG_ALARM_S,
107 .rtc_alarm_year_reg = DA9063_BB_REG_ALARM_Y,
108 .rtc_count_secs_reg = DA9063_REG_COUNT_S,
109 .rtc_count_year_reg = DA9063_REG_COUNT_Y,
110 .rtc_event_reg = DA9063_REG_EVENT_A,
111 /* MASKS */
112 .rtc_enable_mask = DA9063_RTC_EN,
113 .rtc_crystal_mask = DA9063_CRYSTAL,
114 .rtc_enable_32k_crystal_reg = DA9063_REG_EN_32K,
115 .rtc_event_alarm_mask = DA9063_E_ALARM,
116 .rtc_alarm_on_mask = DA9063_ALARM_ON,
117 .rtc_alarm_status_mask = DA9063_ALARM_STATUS_ALARM |
118 DA9063_ALARM_STATUS_TICK,
119 .rtc_tick_on_mask = DA9063_TICK_ON,
120 .rtc_ready_to_read_mask = DA9063_RTC_READ,
121 .rtc_count_sec_mask = DA9063_COUNT_SEC_MASK,
122 .rtc_count_min_mask = DA9063_COUNT_MIN_MASK,
123 .rtc_count_hour_mask = DA9063_COUNT_HOUR_MASK,
124 .rtc_count_day_mask = DA9063_COUNT_DAY_MASK,
125 .rtc_count_month_mask = DA9063_COUNT_MONTH_MASK,
126 .rtc_count_year_mask = DA9063_COUNT_YEAR_MASK,
127 /* ALARM CONFIG */
128 .rtc_data_start = RTC_SEC,
129 .rtc_alarm_len = RTC_DATA_LEN,
132 static const struct da9063_compatible_rtc_regmap da9062_aa_regs = {
133 /* REGS */
134 .rtc_enable_reg = DA9062AA_CONTROL_E,
135 .rtc_alarm_secs_reg = DA9062AA_ALARM_S,
136 .rtc_alarm_year_reg = DA9062AA_ALARM_Y,
137 .rtc_count_secs_reg = DA9062AA_COUNT_S,
138 .rtc_count_year_reg = DA9062AA_COUNT_Y,
139 .rtc_event_reg = DA9062AA_EVENT_A,
140 /* MASKS */
141 .rtc_enable_mask = DA9062AA_RTC_EN_MASK,
142 .rtc_crystal_mask = DA9062AA_CRYSTAL_MASK,
143 .rtc_enable_32k_crystal_reg = DA9062AA_EN_32K,
144 .rtc_event_alarm_mask = DA9062AA_M_ALARM_MASK,
145 .rtc_alarm_on_mask = DA9062AA_ALARM_ON_MASK,
146 .rtc_alarm_status_mask = (0x02 << 6),
147 .rtc_tick_on_mask = DA9062AA_TICK_ON_MASK,
148 .rtc_ready_to_read_mask = DA9062AA_RTC_READ_MASK,
149 .rtc_count_sec_mask = DA9062AA_COUNT_SEC_MASK,
150 .rtc_count_min_mask = DA9062AA_COUNT_MIN_MASK,
151 .rtc_count_hour_mask = DA9062AA_COUNT_HOUR_MASK,
152 .rtc_count_day_mask = DA9062AA_COUNT_DAY_MASK,
153 .rtc_count_month_mask = DA9062AA_COUNT_MONTH_MASK,
154 .rtc_count_year_mask = DA9062AA_COUNT_YEAR_MASK,
155 /* ALARM CONFIG */
156 .rtc_data_start = RTC_SEC,
157 .rtc_alarm_len = RTC_DATA_LEN,
160 static const struct of_device_id da9063_compatible_reg_id_table[] = {
161 { .compatible = "dlg,da9063-rtc", .data = &da9063_bb_regs },
162 { .compatible = "dlg,da9062-rtc", .data = &da9062_aa_regs },
163 { },
165 MODULE_DEVICE_TABLE(of, da9063_compatible_reg_id_table);
167 static void da9063_data_to_tm(u8 *data, struct rtc_time *tm,
168 struct da9063_compatible_rtc *rtc)
170 const struct da9063_compatible_rtc_regmap *config = rtc->config;
172 tm->tm_sec = data[RTC_SEC] & config->rtc_count_sec_mask;
173 tm->tm_min = data[RTC_MIN] & config->rtc_count_min_mask;
174 tm->tm_hour = data[RTC_HOUR] & config->rtc_count_hour_mask;
175 tm->tm_mday = data[RTC_DAY] & config->rtc_count_day_mask;
176 tm->tm_mon = MONTHS_FROM_DA9063(data[RTC_MONTH] &
177 config->rtc_count_month_mask);
178 tm->tm_year = YEARS_FROM_DA9063(data[RTC_YEAR] &
179 config->rtc_count_year_mask);
182 static void da9063_tm_to_data(struct rtc_time *tm, u8 *data,
183 struct da9063_compatible_rtc *rtc)
185 const struct da9063_compatible_rtc_regmap *config = rtc->config;
187 data[RTC_SEC] = tm->tm_sec & config->rtc_count_sec_mask;
188 data[RTC_MIN] = tm->tm_min & config->rtc_count_min_mask;
189 data[RTC_HOUR] = tm->tm_hour & config->rtc_count_hour_mask;
190 data[RTC_DAY] = tm->tm_mday & config->rtc_count_day_mask;
191 data[RTC_MONTH] = MONTHS_TO_DA9063(tm->tm_mon) &
192 config->rtc_count_month_mask;
193 data[RTC_YEAR] = YEARS_TO_DA9063(tm->tm_year) &
194 config->rtc_count_year_mask;
197 static int da9063_rtc_stop_alarm(struct device *dev)
199 struct da9063_compatible_rtc *rtc = dev_get_drvdata(dev);
200 const struct da9063_compatible_rtc_regmap *config = rtc->config;
202 return regmap_update_bits(rtc->regmap,
203 config->rtc_alarm_year_reg,
204 config->rtc_alarm_on_mask,
208 static int da9063_rtc_start_alarm(struct device *dev)
210 struct da9063_compatible_rtc *rtc = dev_get_drvdata(dev);
211 const struct da9063_compatible_rtc_regmap *config = rtc->config;
213 return regmap_update_bits(rtc->regmap,
214 config->rtc_alarm_year_reg,
215 config->rtc_alarm_on_mask,
216 config->rtc_alarm_on_mask);
219 static int da9063_rtc_read_time(struct device *dev, struct rtc_time *tm)
221 struct da9063_compatible_rtc *rtc = dev_get_drvdata(dev);
222 const struct da9063_compatible_rtc_regmap *config = rtc->config;
223 unsigned long tm_secs;
224 unsigned long al_secs;
225 u8 data[RTC_DATA_LEN];
226 int ret;
228 ret = regmap_bulk_read(rtc->regmap,
229 config->rtc_count_secs_reg,
230 data, RTC_DATA_LEN);
231 if (ret < 0) {
232 dev_err(dev, "Failed to read RTC time data: %d\n", ret);
233 return ret;
236 if (!(data[RTC_SEC] & config->rtc_ready_to_read_mask)) {
237 dev_dbg(dev, "RTC not yet ready to be read by the host\n");
238 return -EINVAL;
241 da9063_data_to_tm(data, tm, rtc);
243 tm_secs = rtc_tm_to_time64(tm);
244 al_secs = rtc_tm_to_time64(&rtc->alarm_time);
246 /* handle the rtc synchronisation delay */
247 if (rtc->rtc_sync && al_secs - tm_secs == 1)
248 memcpy(tm, &rtc->alarm_time, sizeof(struct rtc_time));
249 else
250 rtc->rtc_sync = false;
252 return 0;
255 static int da9063_rtc_set_time(struct device *dev, struct rtc_time *tm)
257 struct da9063_compatible_rtc *rtc = dev_get_drvdata(dev);
258 const struct da9063_compatible_rtc_regmap *config = rtc->config;
259 u8 data[RTC_DATA_LEN];
260 int ret;
262 da9063_tm_to_data(tm, data, rtc);
263 ret = regmap_bulk_write(rtc->regmap,
264 config->rtc_count_secs_reg,
265 data, RTC_DATA_LEN);
266 if (ret < 0)
267 dev_err(dev, "Failed to set RTC time data: %d\n", ret);
269 return ret;
272 static int da9063_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
274 struct da9063_compatible_rtc *rtc = dev_get_drvdata(dev);
275 const struct da9063_compatible_rtc_regmap *config = rtc->config;
276 u8 data[RTC_DATA_LEN];
277 int ret;
278 unsigned int val;
280 data[RTC_SEC] = 0;
281 ret = regmap_bulk_read(rtc->regmap,
282 config->rtc_alarm_secs_reg,
283 &data[config->rtc_data_start],
284 config->rtc_alarm_len);
285 if (ret < 0)
286 return ret;
288 da9063_data_to_tm(data, &alrm->time, rtc);
290 alrm->enabled = !!(data[RTC_YEAR] & config->rtc_alarm_on_mask);
292 ret = regmap_read(rtc->regmap,
293 config->rtc_event_reg,
294 &val);
295 if (ret < 0)
296 return ret;
298 if (val & config->rtc_event_alarm_mask)
299 alrm->pending = 1;
300 else
301 alrm->pending = 0;
303 return 0;
306 static int da9063_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
308 struct da9063_compatible_rtc *rtc = dev_get_drvdata(dev);
309 const struct da9063_compatible_rtc_regmap *config = rtc->config;
310 u8 data[RTC_DATA_LEN];
311 int ret;
313 da9063_tm_to_data(&alrm->time, data, rtc);
315 ret = da9063_rtc_stop_alarm(dev);
316 if (ret < 0) {
317 dev_err(dev, "Failed to stop alarm: %d\n", ret);
318 return ret;
321 ret = regmap_bulk_write(rtc->regmap,
322 config->rtc_alarm_secs_reg,
323 &data[config->rtc_data_start],
324 config->rtc_alarm_len);
325 if (ret < 0) {
326 dev_err(dev, "Failed to write alarm: %d\n", ret);
327 return ret;
330 da9063_data_to_tm(data, &rtc->alarm_time, rtc);
332 if (alrm->enabled) {
333 ret = da9063_rtc_start_alarm(dev);
334 if (ret < 0) {
335 dev_err(dev, "Failed to start alarm: %d\n", ret);
336 return ret;
340 return ret;
343 static int da9063_rtc_alarm_irq_enable(struct device *dev,
344 unsigned int enabled)
346 if (enabled)
347 return da9063_rtc_start_alarm(dev);
348 else
349 return da9063_rtc_stop_alarm(dev);
352 static irqreturn_t da9063_alarm_event(int irq, void *data)
354 struct da9063_compatible_rtc *rtc = data;
355 const struct da9063_compatible_rtc_regmap *config = rtc->config;
357 regmap_update_bits(rtc->regmap,
358 config->rtc_alarm_year_reg,
359 config->rtc_alarm_on_mask,
362 rtc->rtc_sync = true;
363 rtc_update_irq(rtc->rtc_dev, 1, RTC_IRQF | RTC_AF);
365 return IRQ_HANDLED;
368 static const struct rtc_class_ops da9063_rtc_ops = {
369 .read_time = da9063_rtc_read_time,
370 .set_time = da9063_rtc_set_time,
371 .read_alarm = da9063_rtc_read_alarm,
372 .set_alarm = da9063_rtc_set_alarm,
373 .alarm_irq_enable = da9063_rtc_alarm_irq_enable,
376 static int da9063_rtc_probe(struct platform_device *pdev)
378 struct da9063_compatible_rtc *rtc;
379 const struct da9063_compatible_rtc_regmap *config;
380 int irq_alarm;
381 u8 data[RTC_DATA_LEN];
382 int ret;
384 if (!pdev->dev.of_node)
385 return -ENXIO;
387 rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
388 if (!rtc)
389 return -ENOMEM;
391 rtc->config = device_get_match_data(&pdev->dev);
392 if (of_device_is_compatible(pdev->dev.of_node, "dlg,da9063-rtc")) {
393 struct da9063 *chip = dev_get_drvdata(pdev->dev.parent);
395 if (chip->variant_code == PMIC_DA9063_AD)
396 rtc->config = &da9063_ad_regs;
399 rtc->regmap = dev_get_regmap(pdev->dev.parent, NULL);
400 if (!rtc->regmap) {
401 dev_warn(&pdev->dev, "Parent regmap unavailable.\n");
402 return -ENXIO;
405 config = rtc->config;
406 ret = regmap_update_bits(rtc->regmap,
407 config->rtc_enable_reg,
408 config->rtc_enable_mask,
409 config->rtc_enable_mask);
410 if (ret < 0)
411 return dev_err_probe(&pdev->dev, ret, "Failed to enable RTC\n");
413 ret = regmap_update_bits(rtc->regmap,
414 config->rtc_enable_32k_crystal_reg,
415 config->rtc_crystal_mask,
416 config->rtc_crystal_mask);
417 if (ret < 0)
418 return dev_err_probe(&pdev->dev, ret,
419 "Failed to run 32kHz oscillator\n");
421 ret = regmap_update_bits(rtc->regmap,
422 config->rtc_alarm_secs_reg,
423 config->rtc_alarm_status_mask,
425 if (ret < 0)
426 return dev_err_probe(&pdev->dev, ret,
427 "Failed to access RTC alarm register\n");
429 ret = regmap_update_bits(rtc->regmap,
430 config->rtc_alarm_secs_reg,
431 DA9063_ALARM_STATUS_ALARM,
432 DA9063_ALARM_STATUS_ALARM);
433 if (ret < 0)
434 return dev_err_probe(&pdev->dev, ret,
435 "Failed to access RTC alarm register\n");
437 ret = regmap_update_bits(rtc->regmap,
438 config->rtc_alarm_year_reg,
439 config->rtc_tick_on_mask,
441 if (ret < 0)
442 return dev_err_probe(&pdev->dev, ret,
443 "Failed to disable TICKs\n");
445 data[RTC_SEC] = 0;
446 ret = regmap_bulk_read(rtc->regmap,
447 config->rtc_alarm_secs_reg,
448 &data[config->rtc_data_start],
449 config->rtc_alarm_len);
450 if (ret < 0)
451 return dev_err_probe(&pdev->dev, ret,
452 "Failed to read initial alarm data\n");
454 platform_set_drvdata(pdev, rtc);
456 rtc->rtc_dev = devm_rtc_allocate_device(&pdev->dev);
457 if (IS_ERR(rtc->rtc_dev))
458 return PTR_ERR(rtc->rtc_dev);
460 rtc->rtc_dev->ops = &da9063_rtc_ops;
461 rtc->rtc_dev->range_min = RTC_TIMESTAMP_BEGIN_2000;
462 rtc->rtc_dev->range_max = RTC_TIMESTAMP_END_2063;
464 da9063_data_to_tm(data, &rtc->alarm_time, rtc);
465 rtc->rtc_sync = false;
467 if (config->rtc_data_start != RTC_SEC) {
468 set_bit(RTC_FEATURE_ALARM_RES_MINUTE, rtc->rtc_dev->features);
470 * TODO: some models have alarms on a minute boundary but still
471 * support real hardware interrupts.
473 clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, rtc->rtc_dev->features);
476 irq_alarm = platform_get_irq_byname_optional(pdev, "ALARM");
477 if (irq_alarm >= 0) {
478 ret = devm_request_threaded_irq(&pdev->dev, irq_alarm, NULL,
479 da9063_alarm_event,
480 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
481 "ALARM", rtc);
482 if (ret)
483 dev_err(&pdev->dev,
484 "Failed to request ALARM IRQ %d: %d\n",
485 irq_alarm, ret);
487 ret = dev_pm_set_wake_irq(&pdev->dev, irq_alarm);
488 if (ret)
489 dev_warn(&pdev->dev,
490 "Failed to set IRQ %d as a wake IRQ: %d\n",
491 irq_alarm, ret);
493 device_init_wakeup(&pdev->dev, true);
494 } else if (irq_alarm != -ENXIO) {
495 return irq_alarm;
496 } else {
497 clear_bit(RTC_FEATURE_ALARM, rtc->rtc_dev->features);
500 return devm_rtc_register_device(rtc->rtc_dev);
503 static struct platform_driver da9063_rtc_driver = {
504 .probe = da9063_rtc_probe,
505 .driver = {
506 .name = DA9063_DRVNAME_RTC,
507 .of_match_table = da9063_compatible_reg_id_table,
511 module_platform_driver(da9063_rtc_driver);
513 MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>");
514 MODULE_DESCRIPTION("Real time clock device driver for Dialog DA9063");
515 MODULE_LICENSE("GPL");
516 MODULE_ALIAS("platform:" DA9063_DRVNAME_RTC);