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(da9052, fmt, ...) \
24 dev_err(da9052->dev, "%s: " fmt, __func__, ##__VA_ARGS__)
27 struct rtc_device
*rtc
;
28 struct da9052
*da9052
;
32 static int da9052_rtc_enable_alarm(struct da9052
*da9052
, bool enable
)
36 ret
= da9052_reg_update(da9052
, DA9052_ALARM_Y_REG
,
37 DA9052_ALARM_Y_ALARM_ON
,
38 DA9052_ALARM_Y_ALARM_ON
);
40 rtc_err(da9052
, "Failed to enable ALM: %d\n", ret
);
42 ret
= da9052_reg_update(da9052
, DA9052_ALARM_Y_REG
,
43 DA9052_ALARM_Y_ALARM_ON
, 0);
45 rtc_err(da9052
, "Write error: %d\n", ret
);
50 static irqreturn_t
da9052_rtc_irq(int irq
, void *data
)
52 struct da9052_rtc
*rtc
= data
;
55 ret
= da9052_reg_read(rtc
->da9052
, DA9052_ALARM_MI_REG
);
57 rtc_err(rtc
->da9052
, "Read error: %d\n", ret
);
61 if (ret
& DA9052_ALARMMI_ALARMTYPE
) {
62 da9052_rtc_enable_alarm(rtc
->da9052
, 0);
63 rtc_update_irq(rtc
->rtc
, 1, RTC_IRQF
| RTC_AF
);
65 rtc_update_irq(rtc
->rtc
, 1, RTC_IRQF
| RTC_PF
);
70 static int da9052_read_alarm(struct da9052
*da9052
, struct rtc_time
*rtc_tm
)
75 ret
= da9052_group_read(da9052
, DA9052_ALARM_MI_REG
, 5, v
);
77 rtc_err(da9052
, "Failed to group read ALM: %d\n", ret
);
81 rtc_tm
->tm_year
= (v
[4] & DA9052_RTC_YEAR
) + 100;
82 rtc_tm
->tm_mon
= (v
[3] & DA9052_RTC_MONTH
) - 1;
83 rtc_tm
->tm_mday
= v
[2] & DA9052_RTC_DAY
;
84 rtc_tm
->tm_hour
= v
[1] & DA9052_RTC_HOUR
;
85 rtc_tm
->tm_min
= v
[0] & DA9052_RTC_MIN
;
87 ret
= rtc_valid_tm(rtc_tm
);
93 static int da9052_set_alarm(struct da9052
*da9052
, struct rtc_time
*rtc_tm
)
98 rtc_tm
->tm_year
-= 100;
101 ret
= da9052_reg_update(da9052
, DA9052_ALARM_MI_REG
,
102 DA9052_RTC_MIN
, rtc_tm
->tm_min
);
104 rtc_err(da9052
, "Failed to write ALRM MIN: %d\n", ret
);
108 v
[0] = rtc_tm
->tm_hour
;
109 v
[1] = rtc_tm
->tm_mday
;
110 v
[2] = rtc_tm
->tm_mon
;
112 ret
= da9052_group_write(da9052
, DA9052_ALARM_H_REG
, 3, v
);
116 ret
= da9052_reg_update(da9052
, DA9052_ALARM_Y_REG
,
117 DA9052_RTC_YEAR
, rtc_tm
->tm_year
);
119 rtc_err(da9052
, "Failed to write ALRM YEAR: %d\n", ret
);
124 static int da9052_rtc_get_alarm_status(struct da9052
*da9052
)
128 ret
= da9052_reg_read(da9052
, DA9052_ALARM_Y_REG
);
130 rtc_err(da9052
, "Failed to read ALM: %d\n", ret
);
133 ret
&= DA9052_ALARM_Y_ALARM_ON
;
134 return (ret
> 0) ? 1 : 0;
137 static int da9052_rtc_read_time(struct device
*dev
, struct rtc_time
*rtc_tm
)
139 struct da9052_rtc
*rtc
= dev_get_drvdata(dev
);
143 ret
= da9052_group_read(rtc
->da9052
, DA9052_COUNT_S_REG
, 6, v
);
145 rtc_err(rtc
->da9052
, "Failed to read RTC time : %d\n", ret
);
149 rtc_tm
->tm_year
= (v
[5] & DA9052_RTC_YEAR
) + 100;
150 rtc_tm
->tm_mon
= (v
[4] & DA9052_RTC_MONTH
) - 1;
151 rtc_tm
->tm_mday
= v
[3] & DA9052_RTC_DAY
;
152 rtc_tm
->tm_hour
= v
[2] & DA9052_RTC_HOUR
;
153 rtc_tm
->tm_min
= v
[1] & DA9052_RTC_MIN
;
154 rtc_tm
->tm_sec
= v
[0] & DA9052_RTC_SEC
;
156 ret
= rtc_valid_tm(rtc_tm
);
158 rtc_err(rtc
->da9052
, "rtc_valid_tm failed: %d\n", ret
);
165 static int da9052_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
167 struct da9052_rtc
*rtc
;
170 rtc
= dev_get_drvdata(dev
);
176 v
[4] = tm
->tm_mon
+ 1;
177 v
[5] = tm
->tm_year
- 100;
179 return da9052_group_write(rtc
->da9052
, DA9052_COUNT_S_REG
, 6, v
);
182 static int da9052_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
185 struct rtc_time
*tm
= &alrm
->time
;
186 struct da9052_rtc
*rtc
= dev_get_drvdata(dev
);
188 ret
= da9052_read_alarm(rtc
->da9052
, tm
);
193 alrm
->enabled
= da9052_rtc_get_alarm_status(rtc
->da9052
);
198 static int da9052_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
201 struct rtc_time
*tm
= &alrm
->time
;
202 struct da9052_rtc
*rtc
= dev_get_drvdata(dev
);
204 ret
= da9052_rtc_enable_alarm(rtc
->da9052
, 0);
208 ret
= da9052_set_alarm(rtc
->da9052
, tm
);
212 ret
= da9052_rtc_enable_alarm(rtc
->da9052
, 1);
217 static int da9052_rtc_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
219 struct da9052_rtc
*rtc
= dev_get_drvdata(dev
);
221 return da9052_rtc_enable_alarm(rtc
->da9052
, enabled
);
224 static const struct rtc_class_ops da9052_rtc_ops
= {
225 .read_time
= da9052_rtc_read_time
,
226 .set_time
= da9052_rtc_set_time
,
227 .read_alarm
= da9052_rtc_read_alarm
,
228 .set_alarm
= da9052_rtc_set_alarm
,
229 .alarm_irq_enable
= da9052_rtc_alarm_irq_enable
,
232 static int da9052_rtc_probe(struct platform_device
*pdev
)
234 struct da9052_rtc
*rtc
;
237 rtc
= devm_kzalloc(&pdev
->dev
, sizeof(struct da9052_rtc
), GFP_KERNEL
);
241 rtc
->da9052
= dev_get_drvdata(pdev
->dev
.parent
);
242 platform_set_drvdata(pdev
, rtc
);
243 rtc
->irq
= DA9052_IRQ_ALARM
;
244 ret
= da9052_request_irq(rtc
->da9052
, rtc
->irq
, "ALM",
245 da9052_rtc_irq
, rtc
);
247 rtc_err(rtc
->da9052
, "irq registration failed: %d\n", ret
);
251 rtc
->rtc
= devm_rtc_device_register(&pdev
->dev
, pdev
->name
,
252 &da9052_rtc_ops
, THIS_MODULE
);
253 return PTR_ERR_OR_ZERO(rtc
->rtc
);
256 static struct platform_driver da9052_rtc_driver
= {
257 .probe
= da9052_rtc_probe
,
259 .name
= "da9052-rtc",
260 .owner
= THIS_MODULE
,
264 module_platform_driver(da9052_rtc_driver
);
266 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
267 MODULE_DESCRIPTION("RTC driver for Dialog DA9052 PMIC");
268 MODULE_LICENSE("GPL");
269 MODULE_ALIAS("platform:da9052-rtc");