1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Real time clock driver for DA9052
5 * Copyright(c) 2012 Dialog Semiconductor Ltd.
7 * Author: Dajun Dajun Chen <dajun.chen@diasemi.com>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/rtc.h>
13 #include <linux/err.h>
14 #include <linux/delay.h>
16 #include <linux/mfd/da9052/da9052.h>
17 #include <linux/mfd/da9052/reg.h>
19 #define rtc_err(rtc, fmt, ...) \
20 dev_err(rtc->da9052->dev, "%s: " fmt, __func__, ##__VA_ARGS__)
22 #define DA9052_GET_TIME_RETRIES 5
25 struct rtc_device
*rtc
;
26 struct da9052
*da9052
;
29 static int da9052_rtc_enable_alarm(struct da9052_rtc
*rtc
, bool enable
)
33 ret
= da9052_reg_update(rtc
->da9052
, DA9052_ALARM_Y_REG
,
34 DA9052_ALARM_Y_ALARM_ON
|DA9052_ALARM_Y_TICK_ON
,
35 DA9052_ALARM_Y_ALARM_ON
);
37 rtc_err(rtc
, "Failed to enable ALM: %d\n", ret
);
39 ret
= da9052_reg_update(rtc
->da9052
, DA9052_ALARM_Y_REG
,
40 DA9052_ALARM_Y_ALARM_ON
|DA9052_ALARM_Y_TICK_ON
, 0);
42 rtc_err(rtc
, "Write error: %d\n", ret
);
47 static irqreturn_t
da9052_rtc_irq(int irq
, void *data
)
49 struct da9052_rtc
*rtc
= data
;
51 rtc_update_irq(rtc
->rtc
, 1, RTC_IRQF
| RTC_AF
);
56 static int da9052_read_alarm(struct da9052_rtc
*rtc
, struct rtc_time
*rtc_tm
)
61 int timeout
= DA9052_GET_TIME_RETRIES
;
63 ret
= da9052_group_read(rtc
->da9052
, DA9052_ALARM_MI_REG
, 5, &v
[0][0]);
65 rtc_err(rtc
, "Failed to group read ALM: %d\n", ret
);
70 ret
= da9052_group_read(rtc
->da9052
,
71 DA9052_ALARM_MI_REG
, 5, &v
[idx
][0]);
73 rtc_err(rtc
, "Failed to group read ALM: %d\n", ret
);
77 if (memcmp(&v
[0][0], &v
[1][0], 5) == 0) {
78 rtc_tm
->tm_year
= (v
[0][4] & DA9052_RTC_YEAR
) + 100;
79 rtc_tm
->tm_mon
= (v
[0][3] & DA9052_RTC_MONTH
) - 1;
80 rtc_tm
->tm_mday
= v
[0][2] & DA9052_RTC_DAY
;
81 rtc_tm
->tm_hour
= v
[0][1] & DA9052_RTC_HOUR
;
82 rtc_tm
->tm_min
= v
[0][0] & DA9052_RTC_MIN
;
85 ret
= rtc_valid_tm(rtc_tm
);
94 rtc_err(rtc
, "Timed out reading alarm time\n");
99 static int da9052_set_alarm(struct da9052_rtc
*rtc
, struct rtc_time
*rtc_tm
)
101 struct da9052
*da9052
= rtc
->da9052
;
102 unsigned long alm_time
;
106 alm_time
= rtc_tm_to_time64(rtc_tm
);
108 if (rtc_tm
->tm_sec
> 0) {
109 alm_time
+= 60 - rtc_tm
->tm_sec
;
110 rtc_time64_to_tm(alm_time
, rtc_tm
);
112 BUG_ON(rtc_tm
->tm_sec
); /* it will cause repeated irqs if not zero */
114 rtc_tm
->tm_year
-= 100;
117 ret
= da9052_reg_update(da9052
, DA9052_ALARM_MI_REG
,
118 DA9052_RTC_MIN
, rtc_tm
->tm_min
);
120 rtc_err(rtc
, "Failed to write ALRM MIN: %d\n", ret
);
124 v
[0] = rtc_tm
->tm_hour
;
125 v
[1] = rtc_tm
->tm_mday
;
126 v
[2] = rtc_tm
->tm_mon
;
128 ret
= da9052_group_write(da9052
, DA9052_ALARM_H_REG
, 3, v
);
132 ret
= da9052_reg_update(da9052
, DA9052_ALARM_Y_REG
,
133 DA9052_RTC_YEAR
, rtc_tm
->tm_year
);
135 rtc_err(rtc
, "Failed to write ALRM YEAR: %d\n", ret
);
140 static int da9052_rtc_get_alarm_status(struct da9052_rtc
*rtc
)
144 ret
= da9052_reg_read(rtc
->da9052
, DA9052_ALARM_Y_REG
);
146 rtc_err(rtc
, "Failed to read ALM: %d\n", ret
);
150 return !!(ret
&DA9052_ALARM_Y_ALARM_ON
);
153 static int da9052_rtc_read_time(struct device
*dev
, struct rtc_time
*rtc_tm
)
155 struct da9052_rtc
*rtc
= dev_get_drvdata(dev
);
159 int timeout
= DA9052_GET_TIME_RETRIES
;
161 ret
= da9052_group_read(rtc
->da9052
, DA9052_COUNT_S_REG
, 6, &v
[0][0]);
163 rtc_err(rtc
, "Failed to read RTC time : %d\n", ret
);
168 ret
= da9052_group_read(rtc
->da9052
,
169 DA9052_COUNT_S_REG
, 6, &v
[idx
][0]);
171 rtc_err(rtc
, "Failed to read RTC time : %d\n", ret
);
175 if (memcmp(&v
[0][0], &v
[1][0], 6) == 0) {
176 rtc_tm
->tm_year
= (v
[0][5] & DA9052_RTC_YEAR
) + 100;
177 rtc_tm
->tm_mon
= (v
[0][4] & DA9052_RTC_MONTH
) - 1;
178 rtc_tm
->tm_mday
= v
[0][3] & DA9052_RTC_DAY
;
179 rtc_tm
->tm_hour
= v
[0][2] & DA9052_RTC_HOUR
;
180 rtc_tm
->tm_min
= v
[0][1] & DA9052_RTC_MIN
;
181 rtc_tm
->tm_sec
= v
[0][0] & DA9052_RTC_SEC
;
191 rtc_err(rtc
, "Timed out reading time\n");
196 static int da9052_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
198 struct da9052_rtc
*rtc
;
202 /* DA9052 only has 6 bits for year - to represent 2000-2063 */
203 if ((tm
->tm_year
< 100) || (tm
->tm_year
> 163))
206 rtc
= dev_get_drvdata(dev
);
212 v
[4] = tm
->tm_mon
+ 1;
213 v
[5] = tm
->tm_year
- 100;
215 ret
= da9052_group_write(rtc
->da9052
, DA9052_COUNT_S_REG
, 6, v
);
217 rtc_err(rtc
, "failed to set RTC time: %d\n", ret
);
221 static int da9052_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
224 struct rtc_time
*tm
= &alrm
->time
;
225 struct da9052_rtc
*rtc
= dev_get_drvdata(dev
);
227 ret
= da9052_read_alarm(rtc
, tm
);
229 rtc_err(rtc
, "failed to read RTC alarm: %d\n", ret
);
233 alrm
->enabled
= da9052_rtc_get_alarm_status(rtc
);
237 static int da9052_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
240 struct rtc_time
*tm
= &alrm
->time
;
241 struct da9052_rtc
*rtc
= dev_get_drvdata(dev
);
243 /* DA9052 only has 6 bits for year - to represent 2000-2063 */
244 if ((tm
->tm_year
< 100) || (tm
->tm_year
> 163))
247 ret
= da9052_rtc_enable_alarm(rtc
, 0);
251 ret
= da9052_set_alarm(rtc
, tm
);
255 ret
= da9052_rtc_enable_alarm(rtc
, 1);
259 static int da9052_rtc_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
261 struct da9052_rtc
*rtc
= dev_get_drvdata(dev
);
263 return da9052_rtc_enable_alarm(rtc
, enabled
);
266 static const struct rtc_class_ops da9052_rtc_ops
= {
267 .read_time
= da9052_rtc_read_time
,
268 .set_time
= da9052_rtc_set_time
,
269 .read_alarm
= da9052_rtc_read_alarm
,
270 .set_alarm
= da9052_rtc_set_alarm
,
271 .alarm_irq_enable
= da9052_rtc_alarm_irq_enable
,
274 static int da9052_rtc_probe(struct platform_device
*pdev
)
276 struct da9052_rtc
*rtc
;
279 rtc
= devm_kzalloc(&pdev
->dev
, sizeof(struct da9052_rtc
), GFP_KERNEL
);
283 rtc
->da9052
= dev_get_drvdata(pdev
->dev
.parent
);
284 platform_set_drvdata(pdev
, rtc
);
286 ret
= da9052_reg_write(rtc
->da9052
, DA9052_BBAT_CONT_REG
, 0xFE);
289 "Failed to setup RTC battery charging: %d\n", ret
);
293 ret
= da9052_reg_update(rtc
->da9052
, DA9052_ALARM_Y_REG
,
294 DA9052_ALARM_Y_TICK_ON
, 0);
296 rtc_err(rtc
, "Failed to disable TICKS: %d\n", ret
);
298 device_init_wakeup(&pdev
->dev
, true);
299 rtc
->rtc
= devm_rtc_allocate_device(&pdev
->dev
);
300 if (IS_ERR(rtc
->rtc
))
301 return PTR_ERR(rtc
->rtc
);
303 rtc
->rtc
->ops
= &da9052_rtc_ops
;
304 rtc
->rtc
->range_min
= RTC_TIMESTAMP_BEGIN_2000
;
305 rtc
->rtc
->range_max
= RTC_TIMESTAMP_END_2063
;
307 ret
= rtc_register_device(rtc
->rtc
);
311 ret
= da9052_request_irq(rtc
->da9052
, DA9052_IRQ_ALARM
, "ALM",
312 da9052_rtc_irq
, rtc
);
314 rtc_err(rtc
, "irq registration failed: %d\n", ret
);
321 static struct platform_driver da9052_rtc_driver
= {
322 .probe
= da9052_rtc_probe
,
324 .name
= "da9052-rtc",
328 module_platform_driver(da9052_rtc_driver
);
330 MODULE_AUTHOR("Anthony Olech <Anthony.Olech@diasemi.com>");
331 MODULE_DESCRIPTION("RTC driver for Dialog DA9052 PMIC");
332 MODULE_LICENSE("GPL");
333 MODULE_ALIAS("platform:da9052-rtc");