xref: /openbmc/linux/drivers/rtc/rtc-da9063.c (revision ed17a2bc)
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  */
6 
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>
18 
19 #include <linux/mfd/da9062/registers.h>
20 #include <linux/mfd/da9063/registers.h>
21 #include <linux/mfd/da9063/core.h>
22 
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)
27 
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
36 };
37 
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;
64 };
65 
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;
72 };
73 
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,
101 };
102 
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,
130 };
131 
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,
158 };
159 
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 	{ },
164 };
165 MODULE_DEVICE_TABLE(of, da9063_compatible_reg_id_table);
166 
da9063_data_to_tm(u8 * data,struct rtc_time * tm,struct da9063_compatible_rtc * rtc)167 static void da9063_data_to_tm(u8 *data, struct rtc_time *tm,
168 			      struct da9063_compatible_rtc *rtc)
169 {
170 	const struct da9063_compatible_rtc_regmap *config = rtc->config;
171 
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);
180 }
181 
da9063_tm_to_data(struct rtc_time * tm,u8 * data,struct da9063_compatible_rtc * rtc)182 static void da9063_tm_to_data(struct rtc_time *tm, u8 *data,
183 			      struct da9063_compatible_rtc *rtc)
184 {
185 	const struct da9063_compatible_rtc_regmap *config = rtc->config;
186 
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;
195 }
196 
da9063_rtc_stop_alarm(struct device * dev)197 static int da9063_rtc_stop_alarm(struct device *dev)
198 {
199 	struct da9063_compatible_rtc *rtc = dev_get_drvdata(dev);
200 	const struct da9063_compatible_rtc_regmap *config = rtc->config;
201 
202 	return regmap_update_bits(rtc->regmap,
203 				  config->rtc_alarm_year_reg,
204 				  config->rtc_alarm_on_mask,
205 				  0);
206 }
207 
da9063_rtc_start_alarm(struct device * dev)208 static int da9063_rtc_start_alarm(struct device *dev)
209 {
210 	struct da9063_compatible_rtc *rtc = dev_get_drvdata(dev);
211 	const struct da9063_compatible_rtc_regmap *config = rtc->config;
212 
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);
217 }
218 
da9063_rtc_read_time(struct device * dev,struct rtc_time * tm)219 static int da9063_rtc_read_time(struct device *dev, struct rtc_time *tm)
220 {
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;
227 
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;
234 	}
235 
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;
239 	}
240 
241 	da9063_data_to_tm(data, tm, rtc);
242 
243 	tm_secs = rtc_tm_to_time64(tm);
244 	al_secs = rtc_tm_to_time64(&rtc->alarm_time);
245 
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;
251 
252 	return 0;
253 }
254 
da9063_rtc_set_time(struct device * dev,struct rtc_time * tm)255 static int da9063_rtc_set_time(struct device *dev, struct rtc_time *tm)
256 {
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;
261 
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);
268 
269 	return ret;
270 }
271 
da9063_rtc_read_alarm(struct device * dev,struct rtc_wkalrm * alrm)272 static int da9063_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
273 {
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;
279 
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;
287 
288 	da9063_data_to_tm(data, &alrm->time, rtc);
289 
290 	alrm->enabled = !!(data[RTC_YEAR] & config->rtc_alarm_on_mask);
291 
292 	ret = regmap_read(rtc->regmap,
293 			  config->rtc_event_reg,
294 			  &val);
295 	if (ret < 0)
296 		return ret;
297 
298 	if (val & config->rtc_event_alarm_mask)
299 		alrm->pending = 1;
300 	else
301 		alrm->pending = 0;
302 
303 	return 0;
304 }
305 
da9063_rtc_set_alarm(struct device * dev,struct rtc_wkalrm * alrm)306 static int da9063_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
307 {
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;
312 
313 	da9063_tm_to_data(&alrm->time, data, rtc);
314 
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;
319 	}
320 
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;
328 	}
329 
330 	da9063_data_to_tm(data, &rtc->alarm_time, rtc);
331 
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;
337 		}
338 	}
339 
340 	return ret;
341 }
342 
da9063_rtc_alarm_irq_enable(struct device * dev,unsigned int enabled)343 static int da9063_rtc_alarm_irq_enable(struct device *dev,
344 				       unsigned int enabled)
345 {
346 	if (enabled)
347 		return da9063_rtc_start_alarm(dev);
348 	else
349 		return da9063_rtc_stop_alarm(dev);
350 }
351 
da9063_alarm_event(int irq,void * data)352 static irqreturn_t da9063_alarm_event(int irq, void *data)
353 {
354 	struct da9063_compatible_rtc *rtc = data;
355 	const struct da9063_compatible_rtc_regmap *config = rtc->config;
356 
357 	regmap_update_bits(rtc->regmap,
358 			   config->rtc_alarm_year_reg,
359 			   config->rtc_alarm_on_mask,
360 			   0);
361 
362 	rtc->rtc_sync = true;
363 	rtc_update_irq(rtc->rtc_dev, 1, RTC_IRQF | RTC_AF);
364 
365 	return IRQ_HANDLED;
366 }
367 
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,
374 };
375 
da9063_rtc_probe(struct platform_device * pdev)376 static int da9063_rtc_probe(struct platform_device *pdev)
377 {
378 	struct da9063_compatible_rtc *rtc;
379 	const struct da9063_compatible_rtc_regmap *config;
380 	const struct of_device_id *match;
381 	int irq_alarm;
382 	u8 data[RTC_DATA_LEN];
383 	int ret;
384 
385 	if (!pdev->dev.of_node)
386 		return -ENXIO;
387 
388 	match = of_match_node(da9063_compatible_reg_id_table,
389 			      pdev->dev.of_node);
390 
391 	rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
392 	if (!rtc)
393 		return -ENOMEM;
394 
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);
398 
399 		if (chip->variant_code == PMIC_DA9063_AD)
400 			rtc->config = &da9063_ad_regs;
401 	}
402 
403 	rtc->regmap = dev_get_regmap(pdev->dev.parent, NULL);
404 	if (!rtc->regmap) {
405 		dev_warn(&pdev->dev, "Parent regmap unavailable.\n");
406 		return -ENXIO;
407 	}
408 
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);
414 	if (ret < 0) {
415 		dev_err(&pdev->dev, "Failed to enable RTC\n");
416 		return ret;
417 	}
418 
419 	ret = regmap_update_bits(rtc->regmap,
420 				 config->rtc_enable_32k_crystal_reg,
421 				 config->rtc_crystal_mask,
422 				 config->rtc_crystal_mask);
423 	if (ret < 0) {
424 		dev_err(&pdev->dev, "Failed to run 32kHz oscillator\n");
425 		return ret;
426 	}
427 
428 	ret = regmap_update_bits(rtc->regmap,
429 				 config->rtc_alarm_secs_reg,
430 				 config->rtc_alarm_status_mask,
431 				 0);
432 	if (ret < 0) {
433 		dev_err(&pdev->dev, "Failed to access RTC alarm register\n");
434 		return ret;
435 	}
436 
437 	ret = regmap_update_bits(rtc->regmap,
438 				 config->rtc_alarm_secs_reg,
439 				 DA9063_ALARM_STATUS_ALARM,
440 				 DA9063_ALARM_STATUS_ALARM);
441 	if (ret < 0) {
442 		dev_err(&pdev->dev, "Failed to access RTC alarm register\n");
443 		return ret;
444 	}
445 
446 	ret = regmap_update_bits(rtc->regmap,
447 				 config->rtc_alarm_year_reg,
448 				 config->rtc_tick_on_mask,
449 				 0);
450 	if (ret < 0) {
451 		dev_err(&pdev->dev, "Failed to disable TICKs\n");
452 		return ret;
453 	}
454 
455 	data[RTC_SEC] = 0;
456 	ret = regmap_bulk_read(rtc->regmap,
457 			       config->rtc_alarm_secs_reg,
458 			       &data[config->rtc_data_start],
459 			       config->rtc_alarm_len);
460 	if (ret < 0) {
461 		dev_err(&pdev->dev, "Failed to read initial alarm data: %d\n",
462 			ret);
463 		return ret;
464 	}
465 
466 	platform_set_drvdata(pdev, rtc);
467 
468 	rtc->rtc_dev = devm_rtc_allocate_device(&pdev->dev);
469 	if (IS_ERR(rtc->rtc_dev))
470 		return PTR_ERR(rtc->rtc_dev);
471 
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;
475 
476 	da9063_data_to_tm(data, &rtc->alarm_time, rtc);
477 	rtc->rtc_sync = false;
478 
479 	if (config->rtc_data_start != RTC_SEC) {
480 		set_bit(RTC_FEATURE_ALARM_RES_MINUTE, rtc->rtc_dev->features);
481 		/*
482 		 * TODO: some models have alarms on a minute boundary but still
483 		 * support real hardware interrupts.
484 		 */
485 		clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, rtc->rtc_dev->features);
486 	}
487 
488 	irq_alarm = platform_get_irq_byname(pdev, "ALARM");
489 	if (irq_alarm < 0)
490 		return irq_alarm;
491 
492 	ret = devm_request_threaded_irq(&pdev->dev, irq_alarm, NULL,
493 					da9063_alarm_event,
494 					IRQF_TRIGGER_LOW | IRQF_ONESHOT,
495 					"ALARM", rtc);
496 	if (ret)
497 		dev_err(&pdev->dev, "Failed to request ALARM IRQ %d: %d\n",
498 			irq_alarm, ret);
499 
500 	ret = dev_pm_set_wake_irq(&pdev->dev, irq_alarm);
501 	if (ret)
502 		dev_warn(&pdev->dev,
503 			 "Failed to set IRQ %d as a wake IRQ: %d\n",
504 			 irq_alarm, ret);
505 
506 	device_init_wakeup(&pdev->dev, true);
507 
508 	return devm_rtc_register_device(rtc->rtc_dev);
509 }
510 
511 static struct platform_driver da9063_rtc_driver = {
512 	.probe		= da9063_rtc_probe,
513 	.driver		= {
514 		.name	= DA9063_DRVNAME_RTC,
515 		.of_match_table = da9063_compatible_reg_id_table,
516 	},
517 };
518 
519 module_platform_driver(da9063_rtc_driver);
520 
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);
525