blob: 03044e1bc4974ff7e28957413dc4a8d99a0e95ef [file] [log] [blame]
Ashish Jangamfef931f2012-03-23 15:02:36 -07001/*
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>
Sachin Kamat11f54d02013-07-03 15:07:48 -070018#include <linux/err.h>
Adam Ward03cc1622015-04-16 12:45:26 -070019#include <linux/delay.h>
Ashish Jangamfef931f2012-03-23 15:02:36 -070020
21#include <linux/mfd/da9052/da9052.h>
22#include <linux/mfd/da9052/reg.h>
23
Anthony Olech7c994c082014-06-06 14:35:54 -070024#define rtc_err(rtc, fmt, ...) \
25 dev_err(rtc->da9052->dev, "%s: " fmt, __func__, ##__VA_ARGS__)
Ashish Jangamfef931f2012-03-23 15:02:36 -070026
Adam Ward03cc1622015-04-16 12:45:26 -070027#define DA9052_GET_TIME_RETRIES 5
28
Ashish Jangamfef931f2012-03-23 15:02:36 -070029struct da9052_rtc {
30 struct rtc_device *rtc;
31 struct da9052 *da9052;
Ashish Jangamfef931f2012-03-23 15:02:36 -070032};
33
Anthony Olech7c994c082014-06-06 14:35:54 -070034static int da9052_rtc_enable_alarm(struct da9052_rtc *rtc, bool enable)
Ashish Jangamfef931f2012-03-23 15:02:36 -070035{
36 int ret;
37 if (enable) {
Anthony Olech7c994c082014-06-06 14:35:54 -070038 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);
Ashish Jangamfef931f2012-03-23 15:02:36 -070041 if (ret != 0)
Anthony Olech7c994c082014-06-06 14:35:54 -070042 rtc_err(rtc, "Failed to enable ALM: %d\n", ret);
Ashish Jangamfef931f2012-03-23 15:02:36 -070043 } else {
Anthony Olech7c994c082014-06-06 14:35:54 -070044 ret = da9052_reg_update(rtc->da9052, DA9052_ALARM_Y_REG,
45 DA9052_ALARM_Y_ALARM_ON|DA9052_ALARM_Y_TICK_ON, 0);
Ashish Jangamfef931f2012-03-23 15:02:36 -070046 if (ret != 0)
Anthony Olech7c994c082014-06-06 14:35:54 -070047 rtc_err(rtc, "Write error: %d\n", ret);
Ashish Jangamfef931f2012-03-23 15:02:36 -070048 }
49 return ret;
50}
51
52static irqreturn_t da9052_rtc_irq(int irq, void *data)
53{
54 struct da9052_rtc *rtc = data;
Ashish Jangamfef931f2012-03-23 15:02:36 -070055
Anthony Olech7c994c082014-06-06 14:35:54 -070056 rtc_update_irq(rtc->rtc, 1, RTC_IRQF | RTC_AF);
Ashish Jangamfef931f2012-03-23 15:02:36 -070057
58 return IRQ_HANDLED;
59}
60
Anthony Olech7c994c082014-06-06 14:35:54 -070061static int da9052_read_alarm(struct da9052_rtc *rtc, struct rtc_time *rtc_tm)
Ashish Jangamfef931f2012-03-23 15:02:36 -070062{
63 int ret;
Adam Ward03cc1622015-04-16 12:45:26 -070064 uint8_t v[2][5];
65 int idx = 1;
66 int timeout = DA9052_GET_TIME_RETRIES;
Ashish Jangamfef931f2012-03-23 15:02:36 -070067
Adam Ward03cc1622015-04-16 12:45:26 -070068 ret = da9052_group_read(rtc->da9052, DA9052_ALARM_MI_REG, 5, &v[0][0]);
69 if (ret) {
Anthony Olech7c994c082014-06-06 14:35:54 -070070 rtc_err(rtc, "Failed to group read ALM: %d\n", ret);
Ashish Jangamfef931f2012-03-23 15:02:36 -070071 return ret;
72 }
73
Adam Ward03cc1622015-04-16 12:45:26 -070074 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 }
Ashish Jangamfef931f2012-03-23 15:02:36 -070081
Adam Ward03cc1622015-04-16 12:45:26 -070082 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;
Uwe Kleine-Königd2c92702016-07-11 10:05:27 +020088 rtc_tm->tm_sec = 0;
Adam Ward03cc1622015-04-16 12:45:26 -070089
90 ret = rtc_valid_tm(rtc_tm);
91 return ret;
92 }
93
94 idx = (1-idx);
95 msleep(20);
96
97 } while (timeout--);
98
99 rtc_err(rtc, "Timed out reading alarm time\n");
100
101 return -EIO;
Ashish Jangamfef931f2012-03-23 15:02:36 -0700102}
103
Anthony Olech7c994c082014-06-06 14:35:54 -0700104static int da9052_set_alarm(struct da9052_rtc *rtc, struct rtc_time *rtc_tm)
Ashish Jangamfef931f2012-03-23 15:02:36 -0700105{
Anthony Olech7c994c082014-06-06 14:35:54 -0700106 struct da9052 *da9052 = rtc->da9052;
107 unsigned long alm_time;
Ashish Jangamfef931f2012-03-23 15:02:36 -0700108 int ret;
109 uint8_t v[3];
110
Anthony Olech7c994c082014-06-06 14:35:54 -0700111 ret = rtc_tm_to_time(rtc_tm, &alm_time);
112 if (ret != 0)
113 return ret;
114
115 if (rtc_tm->tm_sec > 0) {
116 alm_time += 60 - rtc_tm->tm_sec;
117 rtc_time_to_tm(alm_time, rtc_tm);
118 }
119 BUG_ON(rtc_tm->tm_sec); /* it will cause repeated irqs if not zero */
120
Ashish Jangamfef931f2012-03-23 15:02:36 -0700121 rtc_tm->tm_year -= 100;
122 rtc_tm->tm_mon += 1;
123
124 ret = da9052_reg_update(da9052, DA9052_ALARM_MI_REG,
125 DA9052_RTC_MIN, rtc_tm->tm_min);
126 if (ret != 0) {
Anthony Olech7c994c082014-06-06 14:35:54 -0700127 rtc_err(rtc, "Failed to write ALRM MIN: %d\n", ret);
Ashish Jangamfef931f2012-03-23 15:02:36 -0700128 return ret;
129 }
130
131 v[0] = rtc_tm->tm_hour;
132 v[1] = rtc_tm->tm_mday;
133 v[2] = rtc_tm->tm_mon;
134
135 ret = da9052_group_write(da9052, DA9052_ALARM_H_REG, 3, v);
136 if (ret < 0)
137 return ret;
138
139 ret = da9052_reg_update(da9052, DA9052_ALARM_Y_REG,
140 DA9052_RTC_YEAR, rtc_tm->tm_year);
141 if (ret != 0)
Anthony Olech7c994c082014-06-06 14:35:54 -0700142 rtc_err(rtc, "Failed to write ALRM YEAR: %d\n", ret);
Ashish Jangamfef931f2012-03-23 15:02:36 -0700143
144 return ret;
145}
146
Anthony Olech7c994c082014-06-06 14:35:54 -0700147static int da9052_rtc_get_alarm_status(struct da9052_rtc *rtc)
Ashish Jangamfef931f2012-03-23 15:02:36 -0700148{
149 int ret;
150
Anthony Olech7c994c082014-06-06 14:35:54 -0700151 ret = da9052_reg_read(rtc->da9052, DA9052_ALARM_Y_REG);
Ashish Jangamfef931f2012-03-23 15:02:36 -0700152 if (ret < 0) {
Anthony Olech7c994c082014-06-06 14:35:54 -0700153 rtc_err(rtc, "Failed to read ALM: %d\n", ret);
Ashish Jangamfef931f2012-03-23 15:02:36 -0700154 return ret;
155 }
Anthony Olech7c994c082014-06-06 14:35:54 -0700156
157 return !!(ret&DA9052_ALARM_Y_ALARM_ON);
Ashish Jangamfef931f2012-03-23 15:02:36 -0700158}
159
160static int da9052_rtc_read_time(struct device *dev, struct rtc_time *rtc_tm)
161{
162 struct da9052_rtc *rtc = dev_get_drvdata(dev);
Ashish Jangamfef931f2012-03-23 15:02:36 -0700163 int ret;
Adam Ward03cc1622015-04-16 12:45:26 -0700164 uint8_t v[2][6];
165 int idx = 1;
166 int timeout = DA9052_GET_TIME_RETRIES;
Ashish Jangamfef931f2012-03-23 15:02:36 -0700167
Adam Ward03cc1622015-04-16 12:45:26 -0700168 ret = da9052_group_read(rtc->da9052, DA9052_COUNT_S_REG, 6, &v[0][0]);
169 if (ret) {
Anthony Olech7c994c082014-06-06 14:35:54 -0700170 rtc_err(rtc, "Failed to read RTC time : %d\n", ret);
Ashish Jangamfef931f2012-03-23 15:02:36 -0700171 return ret;
172 }
173
Adam Ward03cc1622015-04-16 12:45:26 -0700174 do {
175 ret = da9052_group_read(rtc->da9052,
176 DA9052_COUNT_S_REG, 6, &v[idx][0]);
177 if (ret) {
178 rtc_err(rtc, "Failed to read RTC time : %d\n", ret);
179 return ret;
180 }
Ashish Jangamfef931f2012-03-23 15:02:36 -0700181
Adam Ward03cc1622015-04-16 12:45:26 -0700182 if (memcmp(&v[0][0], &v[1][0], 6) == 0) {
183 rtc_tm->tm_year = (v[0][5] & DA9052_RTC_YEAR) + 100;
184 rtc_tm->tm_mon = (v[0][4] & DA9052_RTC_MONTH) - 1;
185 rtc_tm->tm_mday = v[0][3] & DA9052_RTC_DAY;
186 rtc_tm->tm_hour = v[0][2] & DA9052_RTC_HOUR;
187 rtc_tm->tm_min = v[0][1] & DA9052_RTC_MIN;
188 rtc_tm->tm_sec = v[0][0] & DA9052_RTC_SEC;
189
Alexandre Bellonibb54be12018-03-02 10:08:47 +0100190 return 0;
Adam Ward03cc1622015-04-16 12:45:26 -0700191 }
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;
Ashish Jangamfef931f2012-03-23 15:02:36 -0700201}
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];
Anthony Olech7c994c082014-06-06 14:35:54 -0700207 int ret;
Ashish Jangamfef931f2012-03-23 15:02:36 -0700208
Adam Wardae824c42015-04-16 12:45:29 -0700209 /* 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
Ashish Jangamfef931f2012-03-23 15:02:36 -0700213 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
Anthony Olech7c994c082014-06-06 14:35:54 -0700222 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;
Ashish Jangamfef931f2012-03-23 15:02:36 -0700226}
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
Anthony Olech7c994c082014-06-06 14:35:54 -0700234 ret = da9052_read_alarm(rtc, tm);
235 if (ret < 0) {
236 rtc_err(rtc, "failed to read RTC alarm: %d\n", ret);
Ashish Jangamfef931f2012-03-23 15:02:36 -0700237 return ret;
Anthony Olech7c994c082014-06-06 14:35:54 -0700238 }
Ashish Jangamfef931f2012-03-23 15:02:36 -0700239
Anthony Olech7c994c082014-06-06 14:35:54 -0700240 alrm->enabled = da9052_rtc_get_alarm_status(rtc);
Ashish Jangamfef931f2012-03-23 15:02:36 -0700241 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
Adam Wardae824c42015-04-16 12:45:29 -0700250 /* 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
Anthony Olech7c994c082014-06-06 14:35:54 -0700254 ret = da9052_rtc_enable_alarm(rtc, 0);
Ashish Jangamfef931f2012-03-23 15:02:36 -0700255 if (ret < 0)
256 return ret;
257
Anthony Olech7c994c082014-06-06 14:35:54 -0700258 ret = da9052_set_alarm(rtc, tm);
259 if (ret < 0)
Ashish Jangamfef931f2012-03-23 15:02:36 -0700260 return ret;
261
Anthony Olech7c994c082014-06-06 14:35:54 -0700262 ret = da9052_rtc_enable_alarm(rtc, 1);
Ashish Jangamfef931f2012-03-23 15:02:36 -0700263 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
Anthony Olech7c994c082014-06-06 14:35:54 -0700270 return da9052_rtc_enable_alarm(rtc, enabled);
Ashish Jangamfef931f2012-03-23 15:02:36 -0700271}
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
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800281static int da9052_rtc_probe(struct platform_device *pdev)
Ashish Jangamfef931f2012-03-23 15:02:36 -0700282{
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);
Anthony Olech7c994c082014-06-06 14:35:54 -0700292
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
Steve Twiss6406d96e2016-04-14 12:04:54 +0100305 device_init_wakeup(&pdev->dev, true);
306 rtc->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
307 &da9052_rtc_ops, THIS_MODULE);
308
309 if (IS_ERR(rtc->rtc))
310 return PTR_ERR(rtc->rtc);
311
Anthony Olechc2c0eed2014-04-03 14:49:52 -0700312 ret = da9052_request_irq(rtc->da9052, DA9052_IRQ_ALARM, "ALM",
Ashish Jangam925e8ea2013-03-22 15:04:44 -0700313 da9052_rtc_irq, rtc);
Ashish Jangamfef931f2012-03-23 15:02:36 -0700314 if (ret != 0) {
Anthony Olech7c994c082014-06-06 14:35:54 -0700315 rtc_err(rtc, "irq registration failed: %d\n", ret);
Devendra Naga007def02012-07-30 14:41:54 -0700316 return ret;
Ashish Jangamfef931f2012-03-23 15:02:36 -0700317 }
318
Steve Twiss6406d96e2016-04-14 12:04:54 +0100319 return 0;
Ashish Jangamfef931f2012-03-23 15:02:36 -0700320}
321
Ashish Jangamfef931f2012-03-23 15:02:36 -0700322static struct platform_driver da9052_rtc_driver = {
323 .probe = da9052_rtc_probe,
Ashish Jangamfef931f2012-03-23 15:02:36 -0700324 .driver = {
325 .name = "da9052-rtc",
Ashish Jangamfef931f2012-03-23 15:02:36 -0700326 },
327};
328
329module_platform_driver(da9052_rtc_driver);
330
Anthony Olech7c994c082014-06-06 14:35:54 -0700331MODULE_AUTHOR("Anthony Olech <Anthony.Olech@diasemi.com>");
Ashish Jangamfef931f2012-03-23 15:02:36 -0700332MODULE_DESCRIPTION("RTC driver for Dialog DA9052 PMIC");
333MODULE_LICENSE("GPL");
334MODULE_ALIAS("platform:da9052-rtc");