2 * Real time clock driver for DA9052
4 * Copyright(c) 2012 Dialog Semiconductor Ltd.
6 * Author: Dajun Dajun Chen <dajun.chen@diasemi.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/rtc.h>
18 #include <linux/err.h>
19 #include <linux/delay.h>
21 #include <linux/mfd/da9052/da9052.h>
22 #include <linux/mfd/da9052/reg.h>
24 #define rtc_err(rtc, fmt, ...) \
25 dev_err(rtc->da9052->dev, "%s: " fmt, __func__, ##__VA_ARGS__)
27 #define DA9052_GET_TIME_RETRIES 5
30 struct rtc_device
*rtc
;
31 struct da9052
*da9052
;
34 static int da9052_rtc_enable_alarm(struct da9052_rtc
*rtc
, bool enable
)
38 ret
= da9052_reg_update(rtc
->da9052
, DA9052_ALARM_Y_REG
,
39 DA9052_ALARM_Y_ALARM_ON
|DA9052_ALARM_Y_TICK_ON
,
40 DA9052_ALARM_Y_ALARM_ON
);
42 rtc_err(rtc
, "Failed to enable ALM: %d\n", ret
);
44 ret
= da9052_reg_update(rtc
->da9052
, DA9052_ALARM_Y_REG
,
45 DA9052_ALARM_Y_ALARM_ON
|DA9052_ALARM_Y_TICK_ON
, 0);
47 rtc_err(rtc
, "Write error: %d\n", ret
);
52 static irqreturn_t
da9052_rtc_irq(int irq
, void *data
)
54 struct da9052_rtc
*rtc
= data
;
56 rtc_update_irq(rtc
->rtc
, 1, RTC_IRQF
| RTC_AF
);
61 static int da9052_read_alarm(struct da9052_rtc
*rtc
, struct rtc_time
*rtc_tm
)
66 int timeout
= DA9052_GET_TIME_RETRIES
;
68 ret
= da9052_group_read(rtc
->da9052
, DA9052_ALARM_MI_REG
, 5, &v
[0][0]);
70 rtc_err(rtc
, "Failed to group read ALM: %d\n", ret
);
75 ret
= da9052_group_read(rtc
->da9052
,
76 DA9052_ALARM_MI_REG
, 5, &v
[idx
][0]);
78 rtc_err(rtc
, "Failed to group read ALM: %d\n", ret
);
82 if (memcmp(&v
[0][0], &v
[1][0], 5) == 0) {
83 rtc_tm
->tm_year
= (v
[0][4] & DA9052_RTC_YEAR
) + 100;
84 rtc_tm
->tm_mon
= (v
[0][3] & DA9052_RTC_MONTH
) - 1;
85 rtc_tm
->tm_mday
= v
[0][2] & DA9052_RTC_DAY
;
86 rtc_tm
->tm_hour
= v
[0][1] & DA9052_RTC_HOUR
;
87 rtc_tm
->tm_min
= v
[0][0] & DA9052_RTC_MIN
;
90 ret
= rtc_valid_tm(rtc_tm
);
99 rtc_err(rtc
, "Timed out reading alarm time\n");
104 static int da9052_set_alarm(struct da9052_rtc
*rtc
, struct rtc_time
*rtc_tm
)
106 struct da9052
*da9052
= rtc
->da9052
;
107 unsigned long alm_time
;
111 ret
= rtc_tm_to_time(rtc_tm
, &alm_time
);
115 if (rtc_tm
->tm_sec
> 0) {
116 alm_time
+= 60 - rtc_tm
->tm_sec
;
117 rtc_time_to_tm(alm_time
, rtc_tm
);
119 BUG_ON(rtc_tm
->tm_sec
); /* it will cause repeated irqs if not zero */
121 rtc_tm
->tm_year
-= 100;
124 ret
= da9052_reg_update(da9052
, DA9052_ALARM_MI_REG
,
125 DA9052_RTC_MIN
, rtc_tm
->tm_min
);
127 rtc_err(rtc
, "Failed to write ALRM MIN: %d\n", ret
);
131 v
[0] = rtc_tm
->tm_hour
;
132 v
[1] = rtc_tm
->tm_mday
;
133 v
[2] = rtc_tm
->tm_mon
;
135 ret
= da9052_group_write(da9052
, DA9052_ALARM_H_REG
, 3, v
);
139 ret
= da9052_reg_update(da9052
, DA9052_ALARM_Y_REG
,
140 DA9052_RTC_YEAR
, rtc_tm
->tm_year
);
142 rtc_err(rtc
, "Failed to write ALRM YEAR: %d\n", ret
);
147 static int da9052_rtc_get_alarm_status(struct da9052_rtc
*rtc
)
151 ret
= da9052_reg_read(rtc
->da9052
, DA9052_ALARM_Y_REG
);
153 rtc_err(rtc
, "Failed to read ALM: %d\n", ret
);
157 return !!(ret
&DA9052_ALARM_Y_ALARM_ON
);
160 static int da9052_rtc_read_time(struct device
*dev
, struct rtc_time
*rtc_tm
)
162 struct da9052_rtc
*rtc
= dev_get_drvdata(dev
);
166 int timeout
= DA9052_GET_TIME_RETRIES
;
168 ret
= da9052_group_read(rtc
->da9052
, DA9052_COUNT_S_REG
, 6, &v
[0][0]);
170 rtc_err(rtc
, "Failed to read RTC time : %d\n", ret
);
175 ret
= da9052_group_read(rtc
->da9052
,
176 DA9052_COUNT_S_REG
, 6, &v
[idx
][0]);
178 rtc_err(rtc
, "Failed to read RTC time : %d\n", ret
);
182 if (memcmp(&v
[0][0], &v
[1][0], 6) == 0) {
183 rtc_tm
->tm_year
= (v
[0][5] & DA9052_RTC_YEAR
) + 100;
184 rtc_tm
->tm_mon
= (v
[0][4] & DA9052_RTC_MONTH
) - 1;
185 rtc_tm
->tm_mday
= v
[0][3] & DA9052_RTC_DAY
;
186 rtc_tm
->tm_hour
= v
[0][2] & DA9052_RTC_HOUR
;
187 rtc_tm
->tm_min
= v
[0][1] & DA9052_RTC_MIN
;
188 rtc_tm
->tm_sec
= v
[0][0] & DA9052_RTC_SEC
;
190 ret
= rtc_valid_tm(rtc_tm
);
199 rtc_err(rtc
, "Timed out reading time\n");
204 static int da9052_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
206 struct da9052_rtc
*rtc
;
210 /* DA9052 only has 6 bits for year - to represent 2000-2063 */
211 if ((tm
->tm_year
< 100) || (tm
->tm_year
> 163))
214 rtc
= dev_get_drvdata(dev
);
220 v
[4] = tm
->tm_mon
+ 1;
221 v
[5] = tm
->tm_year
- 100;
223 ret
= da9052_group_write(rtc
->da9052
, DA9052_COUNT_S_REG
, 6, v
);
225 rtc_err(rtc
, "failed to set RTC time: %d\n", ret
);
229 static int da9052_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
232 struct rtc_time
*tm
= &alrm
->time
;
233 struct da9052_rtc
*rtc
= dev_get_drvdata(dev
);
235 ret
= da9052_read_alarm(rtc
, tm
);
237 rtc_err(rtc
, "failed to read RTC alarm: %d\n", ret
);
241 alrm
->enabled
= da9052_rtc_get_alarm_status(rtc
);
245 static int da9052_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
248 struct rtc_time
*tm
= &alrm
->time
;
249 struct da9052_rtc
*rtc
= dev_get_drvdata(dev
);
251 /* DA9052 only has 6 bits for year - to represent 2000-2063 */
252 if ((tm
->tm_year
< 100) || (tm
->tm_year
> 163))
255 ret
= da9052_rtc_enable_alarm(rtc
, 0);
259 ret
= da9052_set_alarm(rtc
, tm
);
263 ret
= da9052_rtc_enable_alarm(rtc
, 1);
267 static int da9052_rtc_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
269 struct da9052_rtc
*rtc
= dev_get_drvdata(dev
);
271 return da9052_rtc_enable_alarm(rtc
, enabled
);
274 static const struct rtc_class_ops da9052_rtc_ops
= {
275 .read_time
= da9052_rtc_read_time
,
276 .set_time
= da9052_rtc_set_time
,
277 .read_alarm
= da9052_rtc_read_alarm
,
278 .set_alarm
= da9052_rtc_set_alarm
,
279 .alarm_irq_enable
= da9052_rtc_alarm_irq_enable
,
282 static int da9052_rtc_probe(struct platform_device
*pdev
)
284 struct da9052_rtc
*rtc
;
287 rtc
= devm_kzalloc(&pdev
->dev
, sizeof(struct da9052_rtc
), GFP_KERNEL
);
291 rtc
->da9052
= dev_get_drvdata(pdev
->dev
.parent
);
292 platform_set_drvdata(pdev
, rtc
);
294 ret
= da9052_reg_write(rtc
->da9052
, DA9052_BBAT_CONT_REG
, 0xFE);
297 "Failed to setup RTC battery charging: %d\n", ret
);
301 ret
= da9052_reg_update(rtc
->da9052
, DA9052_ALARM_Y_REG
,
302 DA9052_ALARM_Y_TICK_ON
, 0);
304 rtc_err(rtc
, "Failed to disable TICKS: %d\n", ret
);
306 device_init_wakeup(&pdev
->dev
, true);
307 rtc
->rtc
= devm_rtc_device_register(&pdev
->dev
, pdev
->name
,
308 &da9052_rtc_ops
, THIS_MODULE
);
310 if (IS_ERR(rtc
->rtc
))
311 return PTR_ERR(rtc
->rtc
);
313 ret
= da9052_request_irq(rtc
->da9052
, DA9052_IRQ_ALARM
, "ALM",
314 da9052_rtc_irq
, rtc
);
316 rtc_err(rtc
, "irq registration failed: %d\n", ret
);
323 static struct platform_driver da9052_rtc_driver
= {
324 .probe
= da9052_rtc_probe
,
326 .name
= "da9052-rtc",
330 module_platform_driver(da9052_rtc_driver
);
332 MODULE_AUTHOR("Anthony Olech <Anthony.Olech@diasemi.com>");
333 MODULE_DESCRIPTION("RTC driver for Dialog DA9052 PMIC");
334 MODULE_LICENSE("GPL");
335 MODULE_ALIAS("platform:da9052-rtc");