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