1 // SPDX-License-Identifier: GPL-2.0+
3 * Real time clock device driver for DA9063
4 * Copyright (C) 2013-2015 Dialog Semiconductor Ltd.
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>
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)
38 struct da9063_compatible_rtc_regmap {
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;
50 int rtc_event_alarm_mask;
51 int rtc_alarm_on_mask;
52 int rtc_alarm_status_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;
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;
74 static const struct da9063_compatible_rtc_regmap da9063_ad_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,
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,
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 = {
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,
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,
128 .rtc_data_start = RTC_SEC,
129 .rtc_alarm_len = RTC_DATA_LEN,
132 static const struct da9063_compatible_rtc_regmap da9062_aa_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,
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,
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 },
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];
228 ret = regmap_bulk_read(rtc->regmap,
229 config->rtc_count_secs_reg,
232 dev_err(dev, "Failed to read RTC time data: %d\n", 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");
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));
250 rtc->rtc_sync = false;
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];
262 da9063_tm_to_data(tm, data, rtc);
263 ret = regmap_bulk_write(rtc->regmap,
264 config->rtc_count_secs_reg,
267 dev_err(dev, "Failed to set RTC time data: %d\n", 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];
281 ret = regmap_bulk_read(rtc->regmap,
282 config->rtc_alarm_secs_reg,
283 &data[config->rtc_data_start],
284 config->rtc_alarm_len);
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,
298 if (val & config->rtc_event_alarm_mask)
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];
313 da9063_tm_to_data(&alrm->time, data, rtc);
315 ret = da9063_rtc_stop_alarm(dev);
317 dev_err(dev, "Failed to stop alarm: %d\n", 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);
326 dev_err(dev, "Failed to write alarm: %d\n", ret);
330 da9063_data_to_tm(data, &rtc->alarm_time, rtc);
333 ret = da9063_rtc_start_alarm(dev);
335 dev_err(dev, "Failed to start alarm: %d\n", ret);
343 static int da9063_rtc_alarm_irq_enable(struct device *dev,
344 unsigned int enabled)
347 return da9063_rtc_start_alarm(dev);
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);
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 const struct of_device_id *match;
382 u8 data[RTC_DATA_LEN];
385 if (!pdev->dev.of_node)
388 match = of_match_node(da9063_compatible_reg_id_table,
391 rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
395 rtc->config = match->data;
396 if (of_device_is_compatible(pdev->dev.of_node, "dlg,da9063-rtc")) {
397 struct da9063 *chip = dev_get_drvdata(pdev->dev.parent);
399 if (chip->variant_code == PMIC_DA9063_AD)
400 rtc->config = &da9063_ad_regs;
403 rtc->regmap = dev_get_regmap(pdev->dev.parent, NULL);
405 dev_warn(&pdev->dev, "Parent regmap unavailable.\n");
409 config = rtc->config;
410 ret = regmap_update_bits(rtc->regmap,
411 config->rtc_enable_reg,
412 config->rtc_enable_mask,
413 config->rtc_enable_mask);
415 dev_err(&pdev->dev, "Failed to enable RTC\n");
419 ret = regmap_update_bits(rtc->regmap,
420 config->rtc_enable_32k_crystal_reg,
421 config->rtc_crystal_mask,
422 config->rtc_crystal_mask);
424 dev_err(&pdev->dev, "Failed to run 32kHz oscillator\n");
428 ret = regmap_update_bits(rtc->regmap,
429 config->rtc_alarm_secs_reg,
430 config->rtc_alarm_status_mask,
433 dev_err(&pdev->dev, "Failed to access RTC alarm register\n");
437 ret = regmap_update_bits(rtc->regmap,
438 config->rtc_alarm_secs_reg,
439 DA9063_ALARM_STATUS_ALARM,
440 DA9063_ALARM_STATUS_ALARM);
442 dev_err(&pdev->dev, "Failed to access RTC alarm register\n");
446 ret = regmap_update_bits(rtc->regmap,
447 config->rtc_alarm_year_reg,
448 config->rtc_tick_on_mask,
451 dev_err(&pdev->dev, "Failed to disable TICKs\n");
456 ret = regmap_bulk_read(rtc->regmap,
457 config->rtc_alarm_secs_reg,
458 &data[config->rtc_data_start],
459 config->rtc_alarm_len);
461 dev_err(&pdev->dev, "Failed to read initial alarm data: %d\n",
466 platform_set_drvdata(pdev, rtc);
468 rtc->rtc_dev = devm_rtc_allocate_device(&pdev->dev);
469 if (IS_ERR(rtc->rtc_dev))
470 return PTR_ERR(rtc->rtc_dev);
472 rtc->rtc_dev->ops = &da9063_rtc_ops;
473 rtc->rtc_dev->range_min = RTC_TIMESTAMP_BEGIN_2000;
474 rtc->rtc_dev->range_max = RTC_TIMESTAMP_END_2063;
476 da9063_data_to_tm(data, &rtc->alarm_time, rtc);
477 rtc->rtc_sync = false;
479 if (config->rtc_data_start != RTC_SEC) {
480 set_bit(RTC_FEATURE_ALARM_RES_MINUTE, rtc->rtc_dev->features);
482 * TODO: some models have alarms on a minute boundary but still
483 * support real hardware interrupts.
485 clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, rtc->rtc_dev->features);
488 irq_alarm = platform_get_irq_byname(pdev, "ALARM");
492 ret = devm_request_threaded_irq(&pdev->dev, irq_alarm, NULL,
494 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
497 dev_err(&pdev->dev, "Failed to request ALARM IRQ %d: %d\n",
500 ret = dev_pm_set_wake_irq(&pdev->dev, irq_alarm);
503 "Failed to set IRQ %d as a wake IRQ: %d\n",
506 device_init_wakeup(&pdev->dev, true);
508 return devm_rtc_register_device(rtc->rtc_dev);
511 static struct platform_driver da9063_rtc_driver = {
512 .probe = da9063_rtc_probe,
514 .name = DA9063_DRVNAME_RTC,
515 .of_match_table = da9063_compatible_reg_id_table,
519 module_platform_driver(da9063_rtc_driver);
521 MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>");
522 MODULE_DESCRIPTION("Real time clock device driver for Dialog DA9063");
523 MODULE_LICENSE("GPL");
524 MODULE_ALIAS("platform:" DA9063_DRVNAME_RTC);