blob: 731ed1a97f593bd20966cbf45a646a4d5f2aca51 [file] [log] [blame]
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -07001/* rtc-da9063.c - Real time clock device driver for DA9063
2 * Copyright (C) 2013-14 Dialog Semiconductor Ltd.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 */
14
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/init.h>
18#include <linux/platform_device.h>
19#include <linux/interrupt.h>
20#include <linux/rtc.h>
21#include <linux/slab.h>
22#include <linux/delay.h>
23#include <linux/regmap.h>
24#include <linux/mfd/da9063/registers.h>
25#include <linux/mfd/da9063/core.h>
26
27#define YEARS_TO_DA9063(year) ((year) - 100)
28#define MONTHS_TO_DA9063(month) ((month) + 1)
29#define YEARS_FROM_DA9063(year) ((year) + 100)
30#define MONTHS_FROM_DA9063(month) ((month) - 1)
31
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +010032#define RTC_ALARM_DATA_LEN (DA9063_AD_REG_ALARM_Y - DA9063_AD_REG_ALARM_MI + 1)
33
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -070034#define RTC_DATA_LEN (DA9063_REG_COUNT_Y - DA9063_REG_COUNT_S + 1)
35#define RTC_SEC 0
36#define RTC_MIN 1
37#define RTC_HOUR 2
38#define RTC_DAY 3
39#define RTC_MONTH 4
40#define RTC_YEAR 5
41
42struct da9063_rtc {
43 struct rtc_device *rtc_dev;
44 struct da9063 *hw;
45 struct rtc_time alarm_time;
46 bool rtc_sync;
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +010047 int alarm_year;
48 int alarm_start;
49 int alarm_len;
50 int data_start;
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -070051};
52
53static void da9063_data_to_tm(u8 *data, struct rtc_time *tm)
54{
55 tm->tm_sec = data[RTC_SEC] & DA9063_COUNT_SEC_MASK;
56 tm->tm_min = data[RTC_MIN] & DA9063_COUNT_MIN_MASK;
57 tm->tm_hour = data[RTC_HOUR] & DA9063_COUNT_HOUR_MASK;
58 tm->tm_mday = data[RTC_DAY] & DA9063_COUNT_DAY_MASK;
59 tm->tm_mon = MONTHS_FROM_DA9063(data[RTC_MONTH] &
60 DA9063_COUNT_MONTH_MASK);
61 tm->tm_year = YEARS_FROM_DA9063(data[RTC_YEAR] &
62 DA9063_COUNT_YEAR_MASK);
63}
64
65static void da9063_tm_to_data(struct rtc_time *tm, u8 *data)
66{
67 data[RTC_SEC] &= ~DA9063_COUNT_SEC_MASK;
68 data[RTC_SEC] |= tm->tm_sec & DA9063_COUNT_SEC_MASK;
69
70 data[RTC_MIN] &= ~DA9063_COUNT_MIN_MASK;
71 data[RTC_MIN] |= tm->tm_min & DA9063_COUNT_MIN_MASK;
72
73 data[RTC_HOUR] &= ~DA9063_COUNT_HOUR_MASK;
74 data[RTC_HOUR] |= tm->tm_hour & DA9063_COUNT_HOUR_MASK;
75
76 data[RTC_DAY] &= ~DA9063_COUNT_DAY_MASK;
77 data[RTC_DAY] |= tm->tm_mday & DA9063_COUNT_DAY_MASK;
78
79 data[RTC_MONTH] &= ~DA9063_COUNT_MONTH_MASK;
80 data[RTC_MONTH] |= MONTHS_TO_DA9063(tm->tm_mon) &
81 DA9063_COUNT_MONTH_MASK;
82
83 data[RTC_YEAR] &= ~DA9063_COUNT_YEAR_MASK;
84 data[RTC_YEAR] |= YEARS_TO_DA9063(tm->tm_year) &
85 DA9063_COUNT_YEAR_MASK;
86}
87
88static int da9063_rtc_stop_alarm(struct device *dev)
89{
90 struct da9063_rtc *rtc = dev_get_drvdata(dev);
91
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +010092 return regmap_update_bits(rtc->hw->regmap, rtc->alarm_year,
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -070093 DA9063_ALARM_ON, 0);
94}
95
96static int da9063_rtc_start_alarm(struct device *dev)
97{
98 struct da9063_rtc *rtc = dev_get_drvdata(dev);
99
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +0100100 return regmap_update_bits(rtc->hw->regmap, rtc->alarm_year,
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700101 DA9063_ALARM_ON, DA9063_ALARM_ON);
102}
103
104static int da9063_rtc_read_time(struct device *dev, struct rtc_time *tm)
105{
106 struct da9063_rtc *rtc = dev_get_drvdata(dev);
107 unsigned long tm_secs;
108 unsigned long al_secs;
109 u8 data[RTC_DATA_LEN];
110 int ret;
111
112 ret = regmap_bulk_read(rtc->hw->regmap, DA9063_REG_COUNT_S,
113 data, RTC_DATA_LEN);
114 if (ret < 0) {
115 dev_err(dev, "Failed to read RTC time data: %d\n", ret);
116 return ret;
117 }
118
119 if (!(data[RTC_SEC] & DA9063_RTC_READ)) {
120 dev_dbg(dev, "RTC not yet ready to be read by the host\n");
121 return -EINVAL;
122 }
123
124 da9063_data_to_tm(data, tm);
125
126 rtc_tm_to_time(tm, &tm_secs);
127 rtc_tm_to_time(&rtc->alarm_time, &al_secs);
128
129 /* handle the rtc synchronisation delay */
130 if (rtc->rtc_sync == true && al_secs - tm_secs == 1)
131 memcpy(tm, &rtc->alarm_time, sizeof(struct rtc_time));
132 else
133 rtc->rtc_sync = false;
134
135 return rtc_valid_tm(tm);
136}
137
138static int da9063_rtc_set_time(struct device *dev, struct rtc_time *tm)
139{
140 struct da9063_rtc *rtc = dev_get_drvdata(dev);
141 u8 data[RTC_DATA_LEN];
142 int ret;
143
144 da9063_tm_to_data(tm, data);
145 ret = regmap_bulk_write(rtc->hw->regmap, DA9063_REG_COUNT_S,
146 data, RTC_DATA_LEN);
147 if (ret < 0)
148 dev_err(dev, "Failed to set RTC time data: %d\n", ret);
149
150 return ret;
151}
152
153static int da9063_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
154{
155 struct da9063_rtc *rtc = dev_get_drvdata(dev);
156 u8 data[RTC_DATA_LEN];
157 int ret;
158 unsigned int val;
159
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +0100160 data[RTC_SEC] = 0;
161 ret = regmap_bulk_read(rtc->hw->regmap, rtc->alarm_start,
162 &data[rtc->data_start], rtc->alarm_len);
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700163 if (ret < 0)
164 return ret;
165
166 da9063_data_to_tm(data, &alrm->time);
167
168 alrm->enabled = !!(data[RTC_YEAR] & DA9063_ALARM_ON);
169
170 ret = regmap_read(rtc->hw->regmap, DA9063_REG_EVENT_A, &val);
171 if (ret < 0)
172 return ret;
173
174 if (val & (DA9063_E_ALARM))
175 alrm->pending = 1;
176 else
177 alrm->pending = 0;
178
179 return 0;
180}
181
182static int da9063_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
183{
184 struct da9063_rtc *rtc = dev_get_drvdata(dev);
185 u8 data[RTC_DATA_LEN];
186 int ret;
187
188 da9063_tm_to_data(&alrm->time, data);
189
190 ret = da9063_rtc_stop_alarm(dev);
191 if (ret < 0) {
192 dev_err(dev, "Failed to stop alarm: %d\n", ret);
193 return ret;
194 }
195
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +0100196 ret = regmap_bulk_write(rtc->hw->regmap, rtc->alarm_start,
197 &data[rtc->data_start], rtc->alarm_len);
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700198 if (ret < 0) {
199 dev_err(dev, "Failed to write alarm: %d\n", ret);
200 return ret;
201 }
202
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +0100203 da9063_data_to_tm(data, &rtc->alarm_time);
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700204
205 if (alrm->enabled) {
206 ret = da9063_rtc_start_alarm(dev);
207 if (ret < 0) {
208 dev_err(dev, "Failed to start alarm: %d\n", ret);
209 return ret;
210 }
211 }
212
213 return ret;
214}
215
216static int da9063_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
217{
218 if (enabled)
219 return da9063_rtc_start_alarm(dev);
220 else
221 return da9063_rtc_stop_alarm(dev);
222}
223
224static irqreturn_t da9063_alarm_event(int irq, void *data)
225{
226 struct da9063_rtc *rtc = data;
227
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +0100228 regmap_update_bits(rtc->hw->regmap, rtc->alarm_year,
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700229 DA9063_ALARM_ON, 0);
230
231 rtc->rtc_sync = true;
232 rtc_update_irq(rtc->rtc_dev, 1, RTC_IRQF | RTC_AF);
233
234 return IRQ_HANDLED;
235}
236
237static const struct rtc_class_ops da9063_rtc_ops = {
238 .read_time = da9063_rtc_read_time,
239 .set_time = da9063_rtc_set_time,
240 .read_alarm = da9063_rtc_read_alarm,
241 .set_alarm = da9063_rtc_set_alarm,
242 .alarm_irq_enable = da9063_rtc_alarm_irq_enable,
243};
244
245static int da9063_rtc_probe(struct platform_device *pdev)
246{
247 struct da9063 *da9063 = dev_get_drvdata(pdev->dev.parent);
248 struct da9063_rtc *rtc;
249 int irq_alarm;
250 u8 data[RTC_DATA_LEN];
251 int ret;
252
253 ret = regmap_update_bits(da9063->regmap, DA9063_REG_CONTROL_E,
254 DA9063_RTC_EN, DA9063_RTC_EN);
255 if (ret < 0) {
256 dev_err(&pdev->dev, "Failed to enable RTC\n");
257 goto err;
258 }
259
260 ret = regmap_update_bits(da9063->regmap, DA9063_REG_EN_32K,
261 DA9063_CRYSTAL, DA9063_CRYSTAL);
262 if (ret < 0) {
263 dev_err(&pdev->dev, "Failed to run 32kHz oscillator\n");
264 goto err;
265 }
266
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +0100267 rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
268 if (!rtc)
269 return -ENOMEM;
270
271 if (da9063->variant_code == PMIC_DA9063_AD) {
272 rtc->alarm_year = DA9063_AD_REG_ALARM_Y;
273 rtc->alarm_start = DA9063_AD_REG_ALARM_MI;
274 rtc->alarm_len = RTC_ALARM_DATA_LEN;
275 rtc->data_start = RTC_MIN;
276 } else {
277 rtc->alarm_year = DA9063_BB_REG_ALARM_Y;
278 rtc->alarm_start = DA9063_BB_REG_ALARM_S;
279 rtc->alarm_len = RTC_DATA_LEN;
280 rtc->data_start = RTC_SEC;
281 }
282
283 ret = regmap_update_bits(da9063->regmap, rtc->alarm_start,
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700284 DA9063_ALARM_STATUS_TICK | DA9063_ALARM_STATUS_ALARM,
285 0);
286 if (ret < 0) {
287 dev_err(&pdev->dev, "Failed to access RTC alarm register\n");
288 goto err;
289 }
290
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +0100291 ret = regmap_update_bits(da9063->regmap, rtc->alarm_start,
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700292 DA9063_ALARM_STATUS_ALARM,
293 DA9063_ALARM_STATUS_ALARM);
294 if (ret < 0) {
295 dev_err(&pdev->dev, "Failed to access RTC alarm register\n");
296 goto err;
297 }
298
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +0100299 ret = regmap_update_bits(da9063->regmap, rtc->alarm_year,
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700300 DA9063_TICK_ON, 0);
301 if (ret < 0) {
302 dev_err(&pdev->dev, "Failed to disable TICKs\n");
303 goto err;
304 }
305
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +0100306 data[RTC_SEC] = 0;
307 ret = regmap_bulk_read(da9063->regmap, rtc->alarm_start,
308 &data[rtc->data_start], rtc->alarm_len);
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700309 if (ret < 0) {
310 dev_err(&pdev->dev, "Failed to read initial alarm data: %d\n",
311 ret);
312 goto err;
313 }
314
Opensource [Steve Twiss]c2a57552014-06-06 14:36:03 -0700315 platform_set_drvdata(pdev, rtc);
316
317 irq_alarm = platform_get_irq_byname(pdev, "ALARM");
318 ret = devm_request_threaded_irq(&pdev->dev, irq_alarm, NULL,
319 da9063_alarm_event,
320 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
321 "ALARM", rtc);
322 if (ret) {
323 dev_err(&pdev->dev, "Failed to request ALARM IRQ %d: %d\n",
324 irq_alarm, ret);
325 goto err;
326 }
327
328 rtc->hw = da9063;
329 rtc->rtc_dev = devm_rtc_device_register(&pdev->dev, DA9063_DRVNAME_RTC,
330 &da9063_rtc_ops, THIS_MODULE);
331 if (IS_ERR(rtc->rtc_dev))
332 return PTR_ERR(rtc->rtc_dev);
333
334 da9063_data_to_tm(data, &rtc->alarm_time);
335 rtc->rtc_sync = false;
336err:
337 return ret;
338}
339
340static struct platform_driver da9063_rtc_driver = {
341 .probe = da9063_rtc_probe,
342 .driver = {
343 .name = DA9063_DRVNAME_RTC,
344 .owner = THIS_MODULE,
345 },
346};
347
348module_platform_driver(da9063_rtc_driver);
349
350MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>");
351MODULE_DESCRIPTION("Real time clock device driver for Dialog DA9063");
352MODULE_LICENSE("GPL v2");
353MODULE_ALIAS("platform:" DA9063_DRVNAME_RTC);