blob: d4b72a9fa2ba85bf14432fb899575809b9f202ab [file] [log] [blame]
Wolfram Sang737842e2019-03-18 16:57:27 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Real time clock device driver for DA9063
Steve Twissce006ca2015-10-02 11:21:11 +01004 * Copyright (C) 2013-2015 Dialog Semiconductor Ltd.
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -07005 */
6
S Twiss80ca3272015-07-21 11:29:07 +01007#include <linux/delay.h>
8#include <linux/init.h>
9#include <linux/interrupt.h>
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -070010#include <linux/kernel.h>
11#include <linux/module.h>
S Twiss80ca3272015-07-21 11:29:07 +010012#include <linux/of.h>
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -070013#include <linux/platform_device.h>
S Twiss80ca3272015-07-21 11:29:07 +010014#include <linux/regmap.h>
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -070015#include <linux/rtc.h>
16#include <linux/slab.h>
S Twiss80ca3272015-07-21 11:29:07 +010017
18#include <linux/mfd/da9062/registers.h>
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -070019#include <linux/mfd/da9063/registers.h>
20#include <linux/mfd/da9063/core.h>
21
22#define YEARS_TO_DA9063(year) ((year) - 100)
23#define MONTHS_TO_DA9063(month) ((month) + 1)
24#define YEARS_FROM_DA9063(year) ((year) + 100)
25#define MONTHS_FROM_DA9063(month) ((month) - 1)
26
S Twiss80ca3272015-07-21 11:29:07 +010027enum {
28 RTC_SEC = 0,
29 RTC_MIN = 1,
30 RTC_HOUR = 2,
31 RTC_DAY = 3,
32 RTC_MONTH = 4,
33 RTC_YEAR = 5,
34 RTC_DATA_LEN
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -070035};
36
S Twiss80ca3272015-07-21 11:29:07 +010037struct da9063_compatible_rtc_regmap {
38 /* REGS */
39 int rtc_enable_reg;
40 int rtc_enable_32k_crystal_reg;
41 int rtc_alarm_secs_reg;
42 int rtc_alarm_year_reg;
43 int rtc_count_secs_reg;
44 int rtc_count_year_reg;
45 int rtc_event_reg;
46 /* MASKS */
47 int rtc_enable_mask;
48 int rtc_crystal_mask;
49 int rtc_event_alarm_mask;
50 int rtc_alarm_on_mask;
51 int rtc_alarm_status_mask;
52 int rtc_tick_on_mask;
53 int rtc_ready_to_read_mask;
54 int rtc_count_sec_mask;
55 int rtc_count_min_mask;
56 int rtc_count_hour_mask;
57 int rtc_count_day_mask;
58 int rtc_count_month_mask;
59 int rtc_count_year_mask;
60 /* ALARM CONFIG */
61 int rtc_data_start;
62 int rtc_alarm_len;
63};
64
65struct da9063_compatible_rtc {
66 struct rtc_device *rtc_dev;
67 struct rtc_time alarm_time;
68 struct regmap *regmap;
69 const struct da9063_compatible_rtc_regmap *config;
70 bool rtc_sync;
71};
72
73static const struct da9063_compatible_rtc_regmap da9063_ad_regs = {
74 /* REGS */
75 .rtc_enable_reg = DA9063_REG_CONTROL_E,
76 .rtc_alarm_secs_reg = DA9063_AD_REG_ALARM_MI,
77 .rtc_alarm_year_reg = DA9063_AD_REG_ALARM_Y,
78 .rtc_count_secs_reg = DA9063_REG_COUNT_S,
79 .rtc_count_year_reg = DA9063_REG_COUNT_Y,
80 .rtc_event_reg = DA9063_REG_EVENT_A,
81 /* MASKS */
82 .rtc_enable_mask = DA9063_RTC_EN,
83 .rtc_crystal_mask = DA9063_CRYSTAL,
84 .rtc_enable_32k_crystal_reg = DA9063_REG_EN_32K,
85 .rtc_event_alarm_mask = DA9063_E_ALARM,
86 .rtc_alarm_on_mask = DA9063_ALARM_ON,
87 .rtc_alarm_status_mask = DA9063_ALARM_STATUS_ALARM |
88 DA9063_ALARM_STATUS_TICK,
89 .rtc_tick_on_mask = DA9063_TICK_ON,
90 .rtc_ready_to_read_mask = DA9063_RTC_READ,
91 .rtc_count_sec_mask = DA9063_COUNT_SEC_MASK,
92 .rtc_count_min_mask = DA9063_COUNT_MIN_MASK,
93 .rtc_count_hour_mask = DA9063_COUNT_HOUR_MASK,
94 .rtc_count_day_mask = DA9063_COUNT_DAY_MASK,
95 .rtc_count_month_mask = DA9063_COUNT_MONTH_MASK,
96 .rtc_count_year_mask = DA9063_COUNT_YEAR_MASK,
97 /* ALARM CONFIG */
98 .rtc_data_start = RTC_MIN,
99 .rtc_alarm_len = RTC_DATA_LEN - 1,
100};
101
102static const struct da9063_compatible_rtc_regmap da9063_bb_regs = {
103 /* REGS */
104 .rtc_enable_reg = DA9063_REG_CONTROL_E,
105 .rtc_alarm_secs_reg = DA9063_BB_REG_ALARM_S,
106 .rtc_alarm_year_reg = DA9063_BB_REG_ALARM_Y,
107 .rtc_count_secs_reg = DA9063_REG_COUNT_S,
108 .rtc_count_year_reg = DA9063_REG_COUNT_Y,
109 .rtc_event_reg = DA9063_REG_EVENT_A,
110 /* MASKS */
111 .rtc_enable_mask = DA9063_RTC_EN,
112 .rtc_crystal_mask = DA9063_CRYSTAL,
113 .rtc_enable_32k_crystal_reg = DA9063_REG_EN_32K,
114 .rtc_event_alarm_mask = DA9063_E_ALARM,
115 .rtc_alarm_on_mask = DA9063_ALARM_ON,
116 .rtc_alarm_status_mask = DA9063_ALARM_STATUS_ALARM |
117 DA9063_ALARM_STATUS_TICK,
118 .rtc_tick_on_mask = DA9063_TICK_ON,
119 .rtc_ready_to_read_mask = DA9063_RTC_READ,
120 .rtc_count_sec_mask = DA9063_COUNT_SEC_MASK,
121 .rtc_count_min_mask = DA9063_COUNT_MIN_MASK,
122 .rtc_count_hour_mask = DA9063_COUNT_HOUR_MASK,
123 .rtc_count_day_mask = DA9063_COUNT_DAY_MASK,
124 .rtc_count_month_mask = DA9063_COUNT_MONTH_MASK,
125 .rtc_count_year_mask = DA9063_COUNT_YEAR_MASK,
126 /* ALARM CONFIG */
127 .rtc_data_start = RTC_SEC,
128 .rtc_alarm_len = RTC_DATA_LEN,
129};
130
131static const struct da9063_compatible_rtc_regmap da9062_aa_regs = {
132 /* REGS */
133 .rtc_enable_reg = DA9062AA_CONTROL_E,
134 .rtc_alarm_secs_reg = DA9062AA_ALARM_S,
135 .rtc_alarm_year_reg = DA9062AA_ALARM_Y,
136 .rtc_count_secs_reg = DA9062AA_COUNT_S,
137 .rtc_count_year_reg = DA9062AA_COUNT_Y,
138 .rtc_event_reg = DA9062AA_EVENT_A,
139 /* MASKS */
140 .rtc_enable_mask = DA9062AA_RTC_EN_MASK,
141 .rtc_crystal_mask = DA9062AA_CRYSTAL_MASK,
142 .rtc_enable_32k_crystal_reg = DA9062AA_EN_32K,
143 .rtc_event_alarm_mask = DA9062AA_M_ALARM_MASK,
144 .rtc_alarm_on_mask = DA9062AA_ALARM_ON_MASK,
145 .rtc_alarm_status_mask = (0x02 << 6),
146 .rtc_tick_on_mask = DA9062AA_TICK_ON_MASK,
147 .rtc_ready_to_read_mask = DA9062AA_RTC_READ_MASK,
148 .rtc_count_sec_mask = DA9062AA_COUNT_SEC_MASK,
149 .rtc_count_min_mask = DA9062AA_COUNT_MIN_MASK,
150 .rtc_count_hour_mask = DA9062AA_COUNT_HOUR_MASK,
151 .rtc_count_day_mask = DA9062AA_COUNT_DAY_MASK,
152 .rtc_count_month_mask = DA9062AA_COUNT_MONTH_MASK,
153 .rtc_count_year_mask = DA9062AA_COUNT_YEAR_MASK,
154 /* ALARM CONFIG */
155 .rtc_data_start = RTC_SEC,
156 .rtc_alarm_len = RTC_DATA_LEN,
157};
158
159static const struct of_device_id da9063_compatible_reg_id_table[] = {
160 { .compatible = "dlg,da9063-rtc", .data = &da9063_bb_regs },
161 { .compatible = "dlg,da9062-rtc", .data = &da9062_aa_regs },
162 { },
163};
Javier Martinez Canillas73798d52015-08-27 13:52:02 +0200164MODULE_DEVICE_TABLE(of, da9063_compatible_reg_id_table);
S Twiss80ca3272015-07-21 11:29:07 +0100165
166static void da9063_data_to_tm(u8 *data, struct rtc_time *tm,
167 struct da9063_compatible_rtc *rtc)
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700168{
S Twiss80ca3272015-07-21 11:29:07 +0100169 const struct da9063_compatible_rtc_regmap *config = rtc->config;
170
171 tm->tm_sec = data[RTC_SEC] & config->rtc_count_sec_mask;
172 tm->tm_min = data[RTC_MIN] & config->rtc_count_min_mask;
173 tm->tm_hour = data[RTC_HOUR] & config->rtc_count_hour_mask;
174 tm->tm_mday = data[RTC_DAY] & config->rtc_count_day_mask;
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700175 tm->tm_mon = MONTHS_FROM_DA9063(data[RTC_MONTH] &
S Twiss80ca3272015-07-21 11:29:07 +0100176 config->rtc_count_month_mask);
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700177 tm->tm_year = YEARS_FROM_DA9063(data[RTC_YEAR] &
S Twiss80ca3272015-07-21 11:29:07 +0100178 config->rtc_count_year_mask);
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700179}
180
S Twiss80ca3272015-07-21 11:29:07 +0100181static void da9063_tm_to_data(struct rtc_time *tm, u8 *data,
182 struct da9063_compatible_rtc *rtc)
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700183{
S Twiss80ca3272015-07-21 11:29:07 +0100184 const struct da9063_compatible_rtc_regmap *config = rtc->config;
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700185
Enrico Scholz2ad2c172015-11-27 13:02:55 +0100186 data[RTC_SEC] = tm->tm_sec & config->rtc_count_sec_mask;
187 data[RTC_MIN] = tm->tm_min & config->rtc_count_min_mask;
188 data[RTC_HOUR] = tm->tm_hour & config->rtc_count_hour_mask;
189 data[RTC_DAY] = tm->tm_mday & config->rtc_count_day_mask;
190 data[RTC_MONTH] = MONTHS_TO_DA9063(tm->tm_mon) &
S Twiss80ca3272015-07-21 11:29:07 +0100191 config->rtc_count_month_mask;
Enrico Scholz2ad2c172015-11-27 13:02:55 +0100192 data[RTC_YEAR] = YEARS_TO_DA9063(tm->tm_year) &
S Twiss80ca3272015-07-21 11:29:07 +0100193 config->rtc_count_year_mask;
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700194}
195
196static int da9063_rtc_stop_alarm(struct device *dev)
197{
S Twiss80ca3272015-07-21 11:29:07 +0100198 struct da9063_compatible_rtc *rtc = dev_get_drvdata(dev);
199 const struct da9063_compatible_rtc_regmap *config = rtc->config;
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700200
S Twiss80ca3272015-07-21 11:29:07 +0100201 return regmap_update_bits(rtc->regmap,
202 config->rtc_alarm_year_reg,
203 config->rtc_alarm_on_mask,
204 0);
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700205}
206
207static int da9063_rtc_start_alarm(struct device *dev)
208{
S Twiss80ca3272015-07-21 11:29:07 +0100209 struct da9063_compatible_rtc *rtc = dev_get_drvdata(dev);
210 const struct da9063_compatible_rtc_regmap *config = rtc->config;
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700211
S Twiss80ca3272015-07-21 11:29:07 +0100212 return regmap_update_bits(rtc->regmap,
213 config->rtc_alarm_year_reg,
214 config->rtc_alarm_on_mask,
215 config->rtc_alarm_on_mask);
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700216}
217
218static int da9063_rtc_read_time(struct device *dev, struct rtc_time *tm)
219{
S Twiss80ca3272015-07-21 11:29:07 +0100220 struct da9063_compatible_rtc *rtc = dev_get_drvdata(dev);
221 const struct da9063_compatible_rtc_regmap *config = rtc->config;
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700222 unsigned long tm_secs;
223 unsigned long al_secs;
224 u8 data[RTC_DATA_LEN];
225 int ret;
226
S Twiss80ca3272015-07-21 11:29:07 +0100227 ret = regmap_bulk_read(rtc->regmap,
228 config->rtc_count_secs_reg,
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700229 data, RTC_DATA_LEN);
230 if (ret < 0) {
231 dev_err(dev, "Failed to read RTC time data: %d\n", ret);
232 return ret;
233 }
234
S Twiss80ca3272015-07-21 11:29:07 +0100235 if (!(data[RTC_SEC] & config->rtc_ready_to_read_mask)) {
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700236 dev_dbg(dev, "RTC not yet ready to be read by the host\n");
237 return -EINVAL;
238 }
239
S Twiss80ca3272015-07-21 11:29:07 +0100240 da9063_data_to_tm(data, tm, rtc);
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700241
Alexandre Bellonib599db32019-03-21 11:15:57 +0100242 tm_secs = rtc_tm_to_time64(tm);
243 al_secs = rtc_tm_to_time64(&rtc->alarm_time);
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700244
245 /* handle the rtc synchronisation delay */
Kaixu Xiaa48c6222020-11-06 16:00:37 +0800246 if (rtc->rtc_sync && al_secs - tm_secs == 1)
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700247 memcpy(tm, &rtc->alarm_time, sizeof(struct rtc_time));
248 else
249 rtc->rtc_sync = false;
250
Alexandre Bellonibb54be12018-03-02 10:08:47 +0100251 return 0;
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700252}
253
254static int da9063_rtc_set_time(struct device *dev, struct rtc_time *tm)
255{
S Twiss80ca3272015-07-21 11:29:07 +0100256 struct da9063_compatible_rtc *rtc = dev_get_drvdata(dev);
257 const struct da9063_compatible_rtc_regmap *config = rtc->config;
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700258 u8 data[RTC_DATA_LEN];
259 int ret;
260
S Twiss80ca3272015-07-21 11:29:07 +0100261 da9063_tm_to_data(tm, data, rtc);
262 ret = regmap_bulk_write(rtc->regmap,
263 config->rtc_count_secs_reg,
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700264 data, RTC_DATA_LEN);
265 if (ret < 0)
266 dev_err(dev, "Failed to set RTC time data: %d\n", ret);
267
268 return ret;
269}
270
271static int da9063_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
272{
S Twiss80ca3272015-07-21 11:29:07 +0100273 struct da9063_compatible_rtc *rtc = dev_get_drvdata(dev);
274 const struct da9063_compatible_rtc_regmap *config = rtc->config;
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700275 u8 data[RTC_DATA_LEN];
276 int ret;
277 unsigned int val;
278
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +0100279 data[RTC_SEC] = 0;
S Twiss80ca3272015-07-21 11:29:07 +0100280 ret = regmap_bulk_read(rtc->regmap,
281 config->rtc_alarm_secs_reg,
282 &data[config->rtc_data_start],
283 config->rtc_alarm_len);
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700284 if (ret < 0)
285 return ret;
286
S Twiss80ca3272015-07-21 11:29:07 +0100287 da9063_data_to_tm(data, &alrm->time, rtc);
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700288
S Twiss80ca3272015-07-21 11:29:07 +0100289 alrm->enabled = !!(data[RTC_YEAR] & config->rtc_alarm_on_mask);
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700290
S Twiss80ca3272015-07-21 11:29:07 +0100291 ret = regmap_read(rtc->regmap,
292 config->rtc_event_reg,
293 &val);
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700294 if (ret < 0)
295 return ret;
296
S Twiss80ca3272015-07-21 11:29:07 +0100297 if (val & config->rtc_event_alarm_mask)
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700298 alrm->pending = 1;
299 else
300 alrm->pending = 0;
301
302 return 0;
303}
304
305static int da9063_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
306{
S Twiss80ca3272015-07-21 11:29:07 +0100307 struct da9063_compatible_rtc *rtc = dev_get_drvdata(dev);
308 const struct da9063_compatible_rtc_regmap *config = rtc->config;
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700309 u8 data[RTC_DATA_LEN];
310 int ret;
311
S Twiss80ca3272015-07-21 11:29:07 +0100312 da9063_tm_to_data(&alrm->time, data, rtc);
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700313
314 ret = da9063_rtc_stop_alarm(dev);
315 if (ret < 0) {
316 dev_err(dev, "Failed to stop alarm: %d\n", ret);
317 return ret;
318 }
319
S Twiss80ca3272015-07-21 11:29:07 +0100320 ret = regmap_bulk_write(rtc->regmap,
321 config->rtc_alarm_secs_reg,
322 &data[config->rtc_data_start],
323 config->rtc_alarm_len);
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700324 if (ret < 0) {
325 dev_err(dev, "Failed to write alarm: %d\n", ret);
326 return ret;
327 }
328
S Twiss80ca3272015-07-21 11:29:07 +0100329 da9063_data_to_tm(data, &rtc->alarm_time, rtc);
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700330
331 if (alrm->enabled) {
332 ret = da9063_rtc_start_alarm(dev);
333 if (ret < 0) {
334 dev_err(dev, "Failed to start alarm: %d\n", ret);
335 return ret;
336 }
337 }
338
339 return ret;
340}
341
S Twiss80ca3272015-07-21 11:29:07 +0100342static int da9063_rtc_alarm_irq_enable(struct device *dev,
343 unsigned int enabled)
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700344{
345 if (enabled)
346 return da9063_rtc_start_alarm(dev);
347 else
348 return da9063_rtc_stop_alarm(dev);
349}
350
351static irqreturn_t da9063_alarm_event(int irq, void *data)
352{
S Twiss80ca3272015-07-21 11:29:07 +0100353 struct da9063_compatible_rtc *rtc = data;
354 const struct da9063_compatible_rtc_regmap *config = rtc->config;
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700355
S Twiss80ca3272015-07-21 11:29:07 +0100356 regmap_update_bits(rtc->regmap,
357 config->rtc_alarm_year_reg,
358 config->rtc_alarm_on_mask,
359 0);
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700360
361 rtc->rtc_sync = true;
362 rtc_update_irq(rtc->rtc_dev, 1, RTC_IRQF | RTC_AF);
363
364 return IRQ_HANDLED;
365}
366
367static const struct rtc_class_ops da9063_rtc_ops = {
368 .read_time = da9063_rtc_read_time,
369 .set_time = da9063_rtc_set_time,
370 .read_alarm = da9063_rtc_read_alarm,
371 .set_alarm = da9063_rtc_set_alarm,
372 .alarm_irq_enable = da9063_rtc_alarm_irq_enable,
373};
374
375static int da9063_rtc_probe(struct platform_device *pdev)
376{
S Twiss80ca3272015-07-21 11:29:07 +0100377 struct da9063_compatible_rtc *rtc;
378 const struct da9063_compatible_rtc_regmap *config;
379 const struct of_device_id *match;
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700380 int irq_alarm;
381 u8 data[RTC_DATA_LEN];
382 int ret;
383
S Twiss80ca3272015-07-21 11:29:07 +0100384 if (!pdev->dev.of_node)
385 return -ENXIO;
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700386
S Twiss80ca3272015-07-21 11:29:07 +0100387 match = of_match_node(da9063_compatible_reg_id_table,
388 pdev->dev.of_node);
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700389
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +0100390 rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
391 if (!rtc)
392 return -ENOMEM;
393
S Twiss80ca3272015-07-21 11:29:07 +0100394 rtc->config = match->data;
395 if (of_device_is_compatible(pdev->dev.of_node, "dlg,da9063-rtc")) {
396 struct da9063 *chip = dev_get_drvdata(pdev->dev.parent);
397
398 if (chip->variant_code == PMIC_DA9063_AD)
399 rtc->config = &da9063_ad_regs;
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +0100400 }
401
S Twiss80ca3272015-07-21 11:29:07 +0100402 rtc->regmap = dev_get_regmap(pdev->dev.parent, NULL);
403 if (!rtc->regmap) {
404 dev_warn(&pdev->dev, "Parent regmap unavailable.\n");
405 return -ENXIO;
406 }
407
408 config = rtc->config;
409 ret = regmap_update_bits(rtc->regmap,
410 config->rtc_enable_reg,
411 config->rtc_enable_mask,
412 config->rtc_enable_mask);
413 if (ret < 0) {
414 dev_err(&pdev->dev, "Failed to enable RTC\n");
415 return ret;
416 }
417
418 ret = regmap_update_bits(rtc->regmap,
419 config->rtc_enable_32k_crystal_reg,
420 config->rtc_crystal_mask,
421 config->rtc_crystal_mask);
422 if (ret < 0) {
423 dev_err(&pdev->dev, "Failed to run 32kHz oscillator\n");
424 return ret;
425 }
426
427 ret = regmap_update_bits(rtc->regmap,
428 config->rtc_alarm_secs_reg,
429 config->rtc_alarm_status_mask,
430 0);
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700431 if (ret < 0) {
432 dev_err(&pdev->dev, "Failed to access RTC alarm register\n");
S Twiss80ca3272015-07-21 11:29:07 +0100433 return ret;
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700434 }
435
S Twiss80ca3272015-07-21 11:29:07 +0100436 ret = regmap_update_bits(rtc->regmap,
437 config->rtc_alarm_secs_reg,
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700438 DA9063_ALARM_STATUS_ALARM,
439 DA9063_ALARM_STATUS_ALARM);
440 if (ret < 0) {
441 dev_err(&pdev->dev, "Failed to access RTC alarm register\n");
S Twiss80ca3272015-07-21 11:29:07 +0100442 return ret;
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700443 }
444
S Twiss80ca3272015-07-21 11:29:07 +0100445 ret = regmap_update_bits(rtc->regmap,
446 config->rtc_alarm_year_reg,
447 config->rtc_tick_on_mask,
448 0);
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700449 if (ret < 0) {
450 dev_err(&pdev->dev, "Failed to disable TICKs\n");
S Twiss80ca3272015-07-21 11:29:07 +0100451 return ret;
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700452 }
453
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +0100454 data[RTC_SEC] = 0;
S Twiss80ca3272015-07-21 11:29:07 +0100455 ret = regmap_bulk_read(rtc->regmap,
456 config->rtc_alarm_secs_reg,
457 &data[config->rtc_data_start],
458 config->rtc_alarm_len);
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700459 if (ret < 0) {
460 dev_err(&pdev->dev, "Failed to read initial alarm data: %d\n",
461 ret);
S Twiss80ca3272015-07-21 11:29:07 +0100462 return ret;
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700463 }
464
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700465 platform_set_drvdata(pdev, rtc);
466
Alexandre Belloni5ff404d2019-03-21 11:15:56 +0100467 rtc->rtc_dev = devm_rtc_allocate_device(&pdev->dev);
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700468 if (IS_ERR(rtc->rtc_dev))
469 return PTR_ERR(rtc->rtc_dev);
470
Alexandre Belloni5ff404d2019-03-21 11:15:56 +0100471 rtc->rtc_dev->ops = &da9063_rtc_ops;
472 rtc->rtc_dev->range_min = RTC_TIMESTAMP_BEGIN_2000;
473 rtc->rtc_dev->range_max = RTC_TIMESTAMP_END_2063;
474
S Twiss80ca3272015-07-21 11:29:07 +0100475 da9063_data_to_tm(data, &rtc->alarm_time, rtc);
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700476 rtc->rtc_sync = false;
Steve Twiss77535ac2015-12-08 16:28:39 +0000477
Alexandre Belloni882c5e52019-04-02 12:26:36 +0200478 /*
479 * TODO: some models have alarms on a minute boundary but still support
480 * real hardware interrupts. Add this once the core supports it.
481 */
482 if (config->rtc_data_start != RTC_SEC)
483 rtc->rtc_dev->uie_unsupported = 1;
484
Steve Twiss77535ac2015-12-08 16:28:39 +0000485 irq_alarm = platform_get_irq_byname(pdev, "ALARM");
Krzysztof Kozlowski7da83f12019-10-04 17:05:10 +0200486 if (irq_alarm < 0)
487 return irq_alarm;
488
Steve Twiss77535ac2015-12-08 16:28:39 +0000489 ret = devm_request_threaded_irq(&pdev->dev, irq_alarm, NULL,
490 da9063_alarm_event,
491 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
492 "ALARM", rtc);
493 if (ret)
494 dev_err(&pdev->dev, "Failed to request ALARM IRQ %d: %d\n",
495 irq_alarm, ret);
496
Bartosz Golaszewskifdcfd852020-11-09 17:34:08 +0100497 return devm_rtc_register_device(rtc->rtc_dev);
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700498}
499
500static struct platform_driver da9063_rtc_driver = {
501 .probe = da9063_rtc_probe,
502 .driver = {
503 .name = DA9063_DRVNAME_RTC,
S Twiss80ca3272015-07-21 11:29:07 +0100504 .of_match_table = da9063_compatible_reg_id_table,
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700505 },
506};
507
508module_platform_driver(da9063_rtc_driver);
509
510MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>");
511MODULE_DESCRIPTION("Real time clock device driver for Dialog DA9063");
Steve Twissce006ca2015-10-02 11:21:11 +0100512MODULE_LICENSE("GPL");
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700513MODULE_ALIAS("platform:" DA9063_DRVNAME_RTC);