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>
20 #include <linux/mfd/da9052/da9052.h>
21 #include <linux/mfd/da9052/reg.h>
23 #define rtc_err(rtc, fmt, ...) \
24 dev_err(rtc->da9052->dev, "%s: " fmt, __func__, ##__VA_ARGS__)
27 struct rtc_device
*rtc
;
28 struct da9052
*da9052
;
31 static int da9052_rtc_enable_alarm(struct da9052_rtc
*rtc
, bool enable
)
35 ret
= da9052_reg_update(rtc
->da9052
, DA9052_ALARM_Y_REG
,
36 DA9052_ALARM_Y_ALARM_ON
|DA9052_ALARM_Y_TICK_ON
,
37 DA9052_ALARM_Y_ALARM_ON
);
39 rtc_err(rtc
, "Failed to enable ALM: %d\n", ret
);
41 ret
= da9052_reg_update(rtc
->da9052
, DA9052_ALARM_Y_REG
,
42 DA9052_ALARM_Y_ALARM_ON
|DA9052_ALARM_Y_TICK_ON
, 0);
44 rtc_err(rtc
, "Write error: %d\n", ret
);
49 static irqreturn_t
da9052_rtc_irq(int irq
, void *data
)
51 struct da9052_rtc
*rtc
= data
;
53 rtc_update_irq(rtc
->rtc
, 1, RTC_IRQF
| RTC_AF
);
58 static int da9052_read_alarm(struct da9052_rtc
*rtc
, struct rtc_time
*rtc_tm
)
63 ret
= da9052_group_read(rtc
->da9052
, DA9052_ALARM_MI_REG
, 5, v
);
65 rtc_err(rtc
, "Failed to group read ALM: %d\n", ret
);
69 rtc_tm
->tm_year
= (v
[4] & DA9052_RTC_YEAR
) + 100;
70 rtc_tm
->tm_mon
= (v
[3] & DA9052_RTC_MONTH
) - 1;
71 rtc_tm
->tm_mday
= v
[2] & DA9052_RTC_DAY
;
72 rtc_tm
->tm_hour
= v
[1] & DA9052_RTC_HOUR
;
73 rtc_tm
->tm_min
= v
[0] & DA9052_RTC_MIN
;
75 ret
= rtc_valid_tm(rtc_tm
);
79 static int da9052_set_alarm(struct da9052_rtc
*rtc
, struct rtc_time
*rtc_tm
)
81 struct da9052
*da9052
= rtc
->da9052
;
82 unsigned long alm_time
;
86 ret
= rtc_tm_to_time(rtc_tm
, &alm_time
);
90 if (rtc_tm
->tm_sec
> 0) {
91 alm_time
+= 60 - rtc_tm
->tm_sec
;
92 rtc_time_to_tm(alm_time
, rtc_tm
);
94 BUG_ON(rtc_tm
->tm_sec
); /* it will cause repeated irqs if not zero */
96 rtc_tm
->tm_year
-= 100;
99 ret
= da9052_reg_update(da9052
, DA9052_ALARM_MI_REG
,
100 DA9052_RTC_MIN
, rtc_tm
->tm_min
);
102 rtc_err(rtc
, "Failed to write ALRM MIN: %d\n", ret
);
106 v
[0] = rtc_tm
->tm_hour
;
107 v
[1] = rtc_tm
->tm_mday
;
108 v
[2] = rtc_tm
->tm_mon
;
110 ret
= da9052_group_write(da9052
, DA9052_ALARM_H_REG
, 3, v
);
114 ret
= da9052_reg_update(da9052
, DA9052_ALARM_Y_REG
,
115 DA9052_RTC_YEAR
, rtc_tm
->tm_year
);
117 rtc_err(rtc
, "Failed to write ALRM YEAR: %d\n", ret
);
122 static int da9052_rtc_get_alarm_status(struct da9052_rtc
*rtc
)
126 ret
= da9052_reg_read(rtc
->da9052
, DA9052_ALARM_Y_REG
);
128 rtc_err(rtc
, "Failed to read ALM: %d\n", ret
);
132 return !!(ret
&DA9052_ALARM_Y_ALARM_ON
);
135 static int da9052_rtc_read_time(struct device
*dev
, struct rtc_time
*rtc_tm
)
137 struct da9052_rtc
*rtc
= dev_get_drvdata(dev
);
141 ret
= da9052_group_read(rtc
->da9052
, DA9052_COUNT_S_REG
, 6, v
);
143 rtc_err(rtc
, "Failed to read RTC time : %d\n", ret
);
147 rtc_tm
->tm_year
= (v
[5] & DA9052_RTC_YEAR
) + 100;
148 rtc_tm
->tm_mon
= (v
[4] & DA9052_RTC_MONTH
) - 1;
149 rtc_tm
->tm_mday
= v
[3] & DA9052_RTC_DAY
;
150 rtc_tm
->tm_hour
= v
[2] & DA9052_RTC_HOUR
;
151 rtc_tm
->tm_min
= v
[1] & DA9052_RTC_MIN
;
152 rtc_tm
->tm_sec
= v
[0] & DA9052_RTC_SEC
;
154 ret
= rtc_valid_tm(rtc_tm
);
158 static int da9052_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
160 struct da9052_rtc
*rtc
;
164 rtc
= dev_get_drvdata(dev
);
170 v
[4] = tm
->tm_mon
+ 1;
171 v
[5] = tm
->tm_year
- 100;
173 ret
= da9052_group_write(rtc
->da9052
, DA9052_COUNT_S_REG
, 6, v
);
175 rtc_err(rtc
, "failed to set RTC time: %d\n", ret
);
179 static int da9052_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
182 struct rtc_time
*tm
= &alrm
->time
;
183 struct da9052_rtc
*rtc
= dev_get_drvdata(dev
);
185 ret
= da9052_read_alarm(rtc
, tm
);
187 rtc_err(rtc
, "failed to read RTC alarm: %d\n", ret
);
191 alrm
->enabled
= da9052_rtc_get_alarm_status(rtc
);
195 static int da9052_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
198 struct rtc_time
*tm
= &alrm
->time
;
199 struct da9052_rtc
*rtc
= dev_get_drvdata(dev
);
201 ret
= da9052_rtc_enable_alarm(rtc
, 0);
205 ret
= da9052_set_alarm(rtc
, tm
);
209 ret
= da9052_rtc_enable_alarm(rtc
, 1);
213 static int da9052_rtc_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
215 struct da9052_rtc
*rtc
= dev_get_drvdata(dev
);
217 return da9052_rtc_enable_alarm(rtc
, enabled
);
220 static const struct rtc_class_ops da9052_rtc_ops
= {
221 .read_time
= da9052_rtc_read_time
,
222 .set_time
= da9052_rtc_set_time
,
223 .read_alarm
= da9052_rtc_read_alarm
,
224 .set_alarm
= da9052_rtc_set_alarm
,
225 .alarm_irq_enable
= da9052_rtc_alarm_irq_enable
,
228 static int da9052_rtc_probe(struct platform_device
*pdev
)
230 struct da9052_rtc
*rtc
;
233 rtc
= devm_kzalloc(&pdev
->dev
, sizeof(struct da9052_rtc
), GFP_KERNEL
);
237 rtc
->da9052
= dev_get_drvdata(pdev
->dev
.parent
);
238 platform_set_drvdata(pdev
, rtc
);
240 ret
= da9052_reg_write(rtc
->da9052
, DA9052_BBAT_CONT_REG
, 0xFE);
243 "Failed to setup RTC battery charging: %d\n", ret
);
247 ret
= da9052_reg_update(rtc
->da9052
, DA9052_ALARM_Y_REG
,
248 DA9052_ALARM_Y_TICK_ON
, 0);
250 rtc_err(rtc
, "Failed to disable TICKS: %d\n", ret
);
252 ret
= da9052_request_irq(rtc
->da9052
, DA9052_IRQ_ALARM
, "ALM",
253 da9052_rtc_irq
, rtc
);
255 rtc_err(rtc
, "irq registration failed: %d\n", ret
);
259 rtc
->rtc
= devm_rtc_device_register(&pdev
->dev
, pdev
->name
,
260 &da9052_rtc_ops
, THIS_MODULE
);
261 return PTR_ERR_OR_ZERO(rtc
->rtc
);
264 static struct platform_driver da9052_rtc_driver
= {
265 .probe
= da9052_rtc_probe
,
267 .name
= "da9052-rtc",
268 .owner
= THIS_MODULE
,
272 module_platform_driver(da9052_rtc_driver
);
274 MODULE_AUTHOR("Anthony Olech <Anthony.Olech@diasemi.com>");
275 MODULE_DESCRIPTION("RTC driver for Dialog DA9052 PMIC");
276 MODULE_LICENSE("GPL");
277 MODULE_ALIAS("platform:da9052-rtc");