Merge branches 'acpi-pm' and 'pm-pci'
[linux-2.6-microblaze.git] / drivers / rtc / rtc-da9052.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Real time clock driver for DA9052
4  *
5  * Copyright(c) 2012 Dialog Semiconductor Ltd.
6  *
7  * Author: Dajun Dajun Chen <dajun.chen@diasemi.com>
8  */
9
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>
15
16 #include <linux/mfd/da9052/da9052.h>
17 #include <linux/mfd/da9052/reg.h>
18
19 #define rtc_err(rtc, fmt, ...) \
20                 dev_err(rtc->da9052->dev, "%s: " fmt, __func__, ##__VA_ARGS__)
21
22 #define DA9052_GET_TIME_RETRIES 5
23
24 struct da9052_rtc {
25         struct rtc_device *rtc;
26         struct da9052 *da9052;
27 };
28
29 static int da9052_rtc_enable_alarm(struct da9052_rtc *rtc, bool enable)
30 {
31         int ret;
32         if (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);
36                 if (ret != 0)
37                         rtc_err(rtc, "Failed to enable ALM: %d\n", ret);
38         } else {
39                 ret = da9052_reg_update(rtc->da9052, DA9052_ALARM_Y_REG,
40                         DA9052_ALARM_Y_ALARM_ON|DA9052_ALARM_Y_TICK_ON, 0);
41                 if (ret != 0)
42                         rtc_err(rtc, "Write error: %d\n", ret);
43         }
44         return ret;
45 }
46
47 static irqreturn_t da9052_rtc_irq(int irq, void *data)
48 {
49         struct da9052_rtc *rtc = data;
50
51         rtc_update_irq(rtc->rtc, 1, RTC_IRQF | RTC_AF);
52
53         return IRQ_HANDLED;
54 }
55
56 static int da9052_read_alarm(struct da9052_rtc *rtc, struct rtc_time *rtc_tm)
57 {
58         int ret;
59         uint8_t v[2][5];
60         int idx = 1;
61         int timeout = DA9052_GET_TIME_RETRIES;
62
63         ret = da9052_group_read(rtc->da9052, DA9052_ALARM_MI_REG, 5, &v[0][0]);
64         if (ret) {
65                 rtc_err(rtc, "Failed to group read ALM: %d\n", ret);
66                 return ret;
67         }
68
69         do {
70                 ret = da9052_group_read(rtc->da9052,
71                                         DA9052_ALARM_MI_REG, 5, &v[idx][0]);
72                 if (ret) {
73                         rtc_err(rtc, "Failed to group read ALM: %d\n", ret);
74                         return ret;
75                 }
76
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;
83                         rtc_tm->tm_sec = 0;
84
85                         ret = rtc_valid_tm(rtc_tm);
86                         return ret;
87                 }
88
89                 idx = (1-idx);
90                 msleep(20);
91
92         } while (timeout--);
93
94         rtc_err(rtc, "Timed out reading alarm time\n");
95
96         return -EIO;
97 }
98
99 static int da9052_set_alarm(struct da9052_rtc *rtc, struct rtc_time *rtc_tm)
100 {
101         struct da9052 *da9052 = rtc->da9052;
102         unsigned long alm_time;
103         int ret;
104         uint8_t v[3];
105
106         ret = rtc_tm_to_time(rtc_tm, &alm_time);
107         if (ret != 0)
108                 return ret;
109
110         if (rtc_tm->tm_sec > 0) {
111                 alm_time += 60 - rtc_tm->tm_sec;
112                 rtc_time_to_tm(alm_time, rtc_tm);
113         }
114         BUG_ON(rtc_tm->tm_sec); /* it will cause repeated irqs if not zero */
115
116         rtc_tm->tm_year -= 100;
117         rtc_tm->tm_mon += 1;
118
119         ret = da9052_reg_update(da9052, DA9052_ALARM_MI_REG,
120                                 DA9052_RTC_MIN, rtc_tm->tm_min);
121         if (ret != 0) {
122                 rtc_err(rtc, "Failed to write ALRM MIN: %d\n", ret);
123                 return ret;
124         }
125
126         v[0] = rtc_tm->tm_hour;
127         v[1] = rtc_tm->tm_mday;
128         v[2] = rtc_tm->tm_mon;
129
130         ret = da9052_group_write(da9052, DA9052_ALARM_H_REG, 3, v);
131         if (ret < 0)
132                 return ret;
133
134         ret = da9052_reg_update(da9052, DA9052_ALARM_Y_REG,
135                                 DA9052_RTC_YEAR, rtc_tm->tm_year);
136         if (ret != 0)
137                 rtc_err(rtc, "Failed to write ALRM YEAR: %d\n", ret);
138
139         return ret;
140 }
141
142 static int da9052_rtc_get_alarm_status(struct da9052_rtc *rtc)
143 {
144         int ret;
145
146         ret = da9052_reg_read(rtc->da9052, DA9052_ALARM_Y_REG);
147         if (ret < 0) {
148                 rtc_err(rtc, "Failed to read ALM: %d\n", ret);
149                 return ret;
150         }
151
152         return !!(ret&DA9052_ALARM_Y_ALARM_ON);
153 }
154
155 static int da9052_rtc_read_time(struct device *dev, struct rtc_time *rtc_tm)
156 {
157         struct da9052_rtc *rtc = dev_get_drvdata(dev);
158         int ret;
159         uint8_t v[2][6];
160         int idx = 1;
161         int timeout = DA9052_GET_TIME_RETRIES;
162
163         ret = da9052_group_read(rtc->da9052, DA9052_COUNT_S_REG, 6, &v[0][0]);
164         if (ret) {
165                 rtc_err(rtc, "Failed to read RTC time : %d\n", ret);
166                 return ret;
167         }
168
169         do {
170                 ret = da9052_group_read(rtc->da9052,
171                                         DA9052_COUNT_S_REG, 6, &v[idx][0]);
172                 if (ret) {
173                         rtc_err(rtc, "Failed to read RTC time : %d\n", ret);
174                         return ret;
175                 }
176
177                 if (memcmp(&v[0][0], &v[1][0], 6) == 0) {
178                         rtc_tm->tm_year = (v[0][5] & DA9052_RTC_YEAR) + 100;
179                         rtc_tm->tm_mon  = (v[0][4] & DA9052_RTC_MONTH) - 1;
180                         rtc_tm->tm_mday = v[0][3] & DA9052_RTC_DAY;
181                         rtc_tm->tm_hour = v[0][2] & DA9052_RTC_HOUR;
182                         rtc_tm->tm_min  = v[0][1] & DA9052_RTC_MIN;
183                         rtc_tm->tm_sec  = v[0][0] & DA9052_RTC_SEC;
184
185                         return 0;
186                 }
187
188                 idx = (1-idx);
189                 msleep(20);
190
191         } while (timeout--);
192
193         rtc_err(rtc, "Timed out reading time\n");
194
195         return -EIO;
196 }
197
198 static int da9052_rtc_set_time(struct device *dev, struct rtc_time *tm)
199 {
200         struct da9052_rtc *rtc;
201         uint8_t v[6];
202         int ret;
203
204         /* DA9052 only has 6 bits for year - to represent 2000-2063 */
205         if ((tm->tm_year < 100) || (tm->tm_year > 163))
206                 return -EINVAL;
207
208         rtc = dev_get_drvdata(dev);
209
210         v[0] = tm->tm_sec;
211         v[1] = tm->tm_min;
212         v[2] = tm->tm_hour;
213         v[3] = tm->tm_mday;
214         v[4] = tm->tm_mon + 1;
215         v[5] = tm->tm_year - 100;
216
217         ret = da9052_group_write(rtc->da9052, DA9052_COUNT_S_REG, 6, v);
218         if (ret < 0)
219                 rtc_err(rtc, "failed to set RTC time: %d\n", ret);
220         return ret;
221 }
222
223 static int da9052_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
224 {
225         int ret;
226         struct rtc_time *tm = &alrm->time;
227         struct da9052_rtc *rtc = dev_get_drvdata(dev);
228
229         ret = da9052_read_alarm(rtc, tm);
230         if (ret < 0) {
231                 rtc_err(rtc, "failed to read RTC alarm: %d\n", ret);
232                 return ret;
233         }
234
235         alrm->enabled = da9052_rtc_get_alarm_status(rtc);
236         return 0;
237 }
238
239 static int da9052_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
240 {
241         int ret;
242         struct rtc_time *tm = &alrm->time;
243         struct da9052_rtc *rtc = dev_get_drvdata(dev);
244
245         /* DA9052 only has 6 bits for year - to represent 2000-2063 */
246         if ((tm->tm_year < 100) || (tm->tm_year > 163))
247                 return -EINVAL;
248
249         ret = da9052_rtc_enable_alarm(rtc, 0);
250         if (ret < 0)
251                 return ret;
252
253         ret = da9052_set_alarm(rtc, tm);
254         if (ret < 0)
255                 return ret;
256
257         ret = da9052_rtc_enable_alarm(rtc, 1);
258         return ret;
259 }
260
261 static int da9052_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
262 {
263         struct da9052_rtc *rtc = dev_get_drvdata(dev);
264
265         return da9052_rtc_enable_alarm(rtc, enabled);
266 }
267
268 static const struct rtc_class_ops da9052_rtc_ops = {
269         .read_time      = da9052_rtc_read_time,
270         .set_time       = da9052_rtc_set_time,
271         .read_alarm     = da9052_rtc_read_alarm,
272         .set_alarm      = da9052_rtc_set_alarm,
273         .alarm_irq_enable = da9052_rtc_alarm_irq_enable,
274 };
275
276 static int da9052_rtc_probe(struct platform_device *pdev)
277 {
278         struct da9052_rtc *rtc;
279         int ret;
280
281         rtc = devm_kzalloc(&pdev->dev, sizeof(struct da9052_rtc), GFP_KERNEL);
282         if (!rtc)
283                 return -ENOMEM;
284
285         rtc->da9052 = dev_get_drvdata(pdev->dev.parent);
286         platform_set_drvdata(pdev, rtc);
287
288         ret = da9052_reg_write(rtc->da9052, DA9052_BBAT_CONT_REG, 0xFE);
289         if (ret < 0) {
290                 rtc_err(rtc,
291                         "Failed to setup RTC battery charging: %d\n", ret);
292                 return ret;
293         }
294
295         ret = da9052_reg_update(rtc->da9052, DA9052_ALARM_Y_REG,
296                                 DA9052_ALARM_Y_TICK_ON, 0);
297         if (ret != 0)
298                 rtc_err(rtc, "Failed to disable TICKS: %d\n", ret);
299
300         device_init_wakeup(&pdev->dev, true);
301         rtc->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
302                                        &da9052_rtc_ops, THIS_MODULE);
303
304         if (IS_ERR(rtc->rtc))
305                 return PTR_ERR(rtc->rtc);
306
307         ret = da9052_request_irq(rtc->da9052, DA9052_IRQ_ALARM, "ALM",
308                                 da9052_rtc_irq, rtc);
309         if (ret != 0) {
310                 rtc_err(rtc, "irq registration failed: %d\n", ret);
311                 return ret;
312         }
313
314         return 0;
315 }
316
317 static struct platform_driver da9052_rtc_driver = {
318         .probe  = da9052_rtc_probe,
319         .driver = {
320                 .name   = "da9052-rtc",
321         },
322 };
323
324 module_platform_driver(da9052_rtc_driver);
325
326 MODULE_AUTHOR("Anthony Olech <Anthony.Olech@diasemi.com>");
327 MODULE_DESCRIPTION("RTC driver for Dialog DA9052 PMIC");
328 MODULE_LICENSE("GPL");
329 MODULE_ALIAS("platform:da9052-rtc");