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>
19 #include <linux/mfd/da9052/da9052.h>
20 #include <linux/mfd/da9052/reg.h>
22 #define rtc_err(da9052, fmt, ...) \
23 dev_err(da9052->dev, "%s: " fmt, __func__, ##__VA_ARGS__)
26 struct rtc_device
*rtc
;
27 struct da9052
*da9052
;
31 static int da9052_rtc_enable_alarm(struct da9052
*da9052
, bool enable
)
35 ret
= da9052_reg_update(da9052
, DA9052_ALARM_Y_REG
,
36 DA9052_ALARM_Y_ALARM_ON
,
37 DA9052_ALARM_Y_ALARM_ON
);
39 rtc_err(da9052
, "Failed to enable ALM: %d\n", ret
);
41 ret
= da9052_reg_update(da9052
, DA9052_ALARM_Y_REG
,
42 DA9052_ALARM_Y_ALARM_ON
, 0);
44 rtc_err(da9052
, "Write error: %d\n", ret
);
49 static irqreturn_t
da9052_rtc_irq(int irq
, void *data
)
51 struct da9052_rtc
*rtc
= data
;
54 ret
= da9052_reg_read(rtc
->da9052
, DA9052_ALARM_MI_REG
);
56 rtc_err(rtc
->da9052
, "Read error: %d\n", ret
);
60 if (ret
& DA9052_ALARMMI_ALARMTYPE
) {
61 da9052_rtc_enable_alarm(rtc
->da9052
, 0);
62 rtc_update_irq(rtc
->rtc
, 1, RTC_IRQF
| RTC_AF
);
64 rtc_update_irq(rtc
->rtc
, 1, RTC_IRQF
| RTC_PF
);
69 static int da9052_read_alarm(struct da9052
*da9052
, struct rtc_time
*rtc_tm
)
74 ret
= da9052_group_read(da9052
, DA9052_ALARM_MI_REG
, 5, v
);
76 rtc_err(da9052
, "Failed to group read ALM: %d\n", ret
);
80 rtc_tm
->tm_year
= (v
[4] & DA9052_RTC_YEAR
) + 100;
81 rtc_tm
->tm_mon
= (v
[3] & DA9052_RTC_MONTH
) - 1;
82 rtc_tm
->tm_mday
= v
[2] & DA9052_RTC_DAY
;
83 rtc_tm
->tm_hour
= v
[1] & DA9052_RTC_HOUR
;
84 rtc_tm
->tm_min
= v
[0] & DA9052_RTC_MIN
;
86 ret
= rtc_valid_tm(rtc_tm
);
92 static int da9052_set_alarm(struct da9052
*da9052
, struct rtc_time
*rtc_tm
)
97 rtc_tm
->tm_year
-= 100;
100 ret
= da9052_reg_update(da9052
, DA9052_ALARM_MI_REG
,
101 DA9052_RTC_MIN
, rtc_tm
->tm_min
);
103 rtc_err(da9052
, "Failed to write ALRM MIN: %d\n", ret
);
107 v
[0] = rtc_tm
->tm_hour
;
108 v
[1] = rtc_tm
->tm_mday
;
109 v
[2] = rtc_tm
->tm_mon
;
111 ret
= da9052_group_write(da9052
, DA9052_ALARM_H_REG
, 3, v
);
115 ret
= da9052_reg_update(da9052
, DA9052_ALARM_Y_REG
,
116 DA9052_RTC_YEAR
, rtc_tm
->tm_year
);
118 rtc_err(da9052
, "Failed to write ALRM YEAR: %d\n", ret
);
123 static int da9052_rtc_get_alarm_status(struct da9052
*da9052
)
127 ret
= da9052_reg_read(da9052
, DA9052_ALARM_Y_REG
);
129 rtc_err(da9052
, "Failed to read ALM: %d\n", ret
);
132 ret
&= DA9052_ALARM_Y_ALARM_ON
;
133 return (ret
> 0) ? 1 : 0;
136 static int da9052_rtc_read_time(struct device
*dev
, struct rtc_time
*rtc_tm
)
138 struct da9052_rtc
*rtc
= dev_get_drvdata(dev
);
142 ret
= da9052_group_read(rtc
->da9052
, DA9052_COUNT_S_REG
, 6, v
);
144 rtc_err(rtc
->da9052
, "Failed to read RTC time : %d\n", ret
);
148 rtc_tm
->tm_year
= (v
[5] & DA9052_RTC_YEAR
) + 100;
149 rtc_tm
->tm_mon
= (v
[4] & DA9052_RTC_MONTH
) - 1;
150 rtc_tm
->tm_mday
= v
[3] & DA9052_RTC_DAY
;
151 rtc_tm
->tm_hour
= v
[2] & DA9052_RTC_HOUR
;
152 rtc_tm
->tm_min
= v
[1] & DA9052_RTC_MIN
;
153 rtc_tm
->tm_sec
= v
[0] & DA9052_RTC_SEC
;
155 ret
= rtc_valid_tm(rtc_tm
);
157 rtc_err(rtc
->da9052
, "rtc_valid_tm failed: %d\n", ret
);
164 static int da9052_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
166 struct da9052_rtc
*rtc
;
169 rtc
= dev_get_drvdata(dev
);
175 v
[4] = tm
->tm_mon
+ 1;
176 v
[5] = tm
->tm_year
- 100;
178 return da9052_group_write(rtc
->da9052
, DA9052_COUNT_S_REG
, 6, v
);
181 static int da9052_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
184 struct rtc_time
*tm
= &alrm
->time
;
185 struct da9052_rtc
*rtc
= dev_get_drvdata(dev
);
187 ret
= da9052_read_alarm(rtc
->da9052
, tm
);
192 alrm
->enabled
= da9052_rtc_get_alarm_status(rtc
->da9052
);
197 static int da9052_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
200 struct rtc_time
*tm
= &alrm
->time
;
201 struct da9052_rtc
*rtc
= dev_get_drvdata(dev
);
203 ret
= da9052_rtc_enable_alarm(rtc
->da9052
, 0);
207 ret
= da9052_set_alarm(rtc
->da9052
, tm
);
211 ret
= da9052_rtc_enable_alarm(rtc
->da9052
, 1);
216 static int da9052_rtc_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
218 struct da9052_rtc
*rtc
= dev_get_drvdata(dev
);
220 return da9052_rtc_enable_alarm(rtc
->da9052
, enabled
);
223 static const struct rtc_class_ops da9052_rtc_ops
= {
224 .read_time
= da9052_rtc_read_time
,
225 .set_time
= da9052_rtc_set_time
,
226 .read_alarm
= da9052_rtc_read_alarm
,
227 .set_alarm
= da9052_rtc_set_alarm
,
228 .alarm_irq_enable
= da9052_rtc_alarm_irq_enable
,
231 static int da9052_rtc_probe(struct platform_device
*pdev
)
233 struct da9052_rtc
*rtc
;
236 rtc
= devm_kzalloc(&pdev
->dev
, sizeof(struct da9052_rtc
), GFP_KERNEL
);
240 rtc
->da9052
= dev_get_drvdata(pdev
->dev
.parent
);
241 platform_set_drvdata(pdev
, rtc
);
242 rtc
->irq
= DA9052_IRQ_ALARM
;
243 ret
= da9052_request_irq(rtc
->da9052
, rtc
->irq
, "ALM",
244 da9052_rtc_irq
, rtc
);
246 rtc_err(rtc
->da9052
, "irq registration failed: %d\n", ret
);
250 rtc
->rtc
= devm_rtc_device_register(&pdev
->dev
, pdev
->name
,
251 &da9052_rtc_ops
, THIS_MODULE
);
252 if (IS_ERR(rtc
->rtc
))
253 return PTR_ERR(rtc
->rtc
);
258 static int da9052_rtc_remove(struct platform_device
*pdev
)
260 platform_set_drvdata(pdev
, NULL
);
265 static struct platform_driver da9052_rtc_driver
= {
266 .probe
= da9052_rtc_probe
,
267 .remove
= da9052_rtc_remove
,
269 .name
= "da9052-rtc",
270 .owner
= THIS_MODULE
,
274 module_platform_driver(da9052_rtc_driver
);
276 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
277 MODULE_DESCRIPTION("RTC driver for Dialog DA9052 PMIC");
278 MODULE_LICENSE("GPL");
279 MODULE_ALIAS("platform:da9052-rtc");