1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/module.h>
16#include <linux/kernel.h>
17#include <linux/time.h>
18#include <linux/rtc.h>
19#include <linux/slab.h>
20#include <linux/bcd.h>
21#include <linux/interrupt.h>
22#include <linux/ioctl.h>
23#include <linux/completion.h>
24#include <linux/mfd/wm831x/core.h>
25#include <linux/delay.h>
26#include <linux/platform_device.h>
27#include <linux/random.h>
28
29
30
31
32#define WM831X_RTC_WR_CNT_MASK 0xFFFF
33#define WM831X_RTC_WR_CNT_SHIFT 0
34#define WM831X_RTC_WR_CNT_WIDTH 16
35
36
37
38
39#define WM831X_RTC_TIME_MASK 0xFFFF
40#define WM831X_RTC_TIME_SHIFT 0
41#define WM831X_RTC_TIME_WIDTH 16
42
43
44
45
46#define WM831X_RTC_TIME_MASK 0xFFFF
47#define WM831X_RTC_TIME_SHIFT 0
48#define WM831X_RTC_TIME_WIDTH 16
49
50
51
52
53#define WM831X_RTC_ALM_MASK 0xFFFF
54#define WM831X_RTC_ALM_SHIFT 0
55#define WM831X_RTC_ALM_WIDTH 16
56
57
58
59
60#define WM831X_RTC_ALM_MASK 0xFFFF
61#define WM831X_RTC_ALM_SHIFT 0
62#define WM831X_RTC_ALM_WIDTH 16
63
64
65
66
67#define WM831X_RTC_VALID 0x8000
68#define WM831X_RTC_VALID_MASK 0x8000
69#define WM831X_RTC_VALID_SHIFT 15
70#define WM831X_RTC_VALID_WIDTH 1
71#define WM831X_RTC_SYNC_BUSY 0x4000
72#define WM831X_RTC_SYNC_BUSY_MASK 0x4000
73#define WM831X_RTC_SYNC_BUSY_SHIFT 14
74#define WM831X_RTC_SYNC_BUSY_WIDTH 1
75#define WM831X_RTC_ALM_ENA 0x0400
76#define WM831X_RTC_ALM_ENA_MASK 0x0400
77#define WM831X_RTC_ALM_ENA_SHIFT 10
78#define WM831X_RTC_ALM_ENA_WIDTH 1
79#define WM831X_RTC_PINT_FREQ_MASK 0x0070
80#define WM831X_RTC_PINT_FREQ_SHIFT 4
81#define WM831X_RTC_PINT_FREQ_WIDTH 3
82
83
84
85
86#define WM831X_RTC_TRIM_MASK 0x03FF
87#define WM831X_RTC_TRIM_SHIFT 0
88#define WM831X_RTC_TRIM_WIDTH 10
89
90#define WM831X_SET_TIME_RETRIES 5
91#define WM831X_GET_TIME_RETRIES 5
92
93struct wm831x_rtc {
94 struct wm831x *wm831x;
95 struct rtc_device *rtc;
96 unsigned int alarm_enabled:1;
97};
98
99static void wm831x_rtc_add_randomness(struct wm831x *wm831x)
100{
101 int ret;
102 u16 reg;
103
104
105
106
107
108
109 ret = wm831x_reg_read(wm831x, WM831X_RTC_WRITE_COUNTER);
110 if (ret >= 0) {
111 reg = ret;
112 add_device_randomness(®, sizeof(reg));
113 } else {
114 dev_warn(wm831x->dev, "Failed to read RTC write counter: %d\n",
115 ret);
116 }
117}
118
119
120
121
122static int wm831x_rtc_readtime(struct device *dev, struct rtc_time *tm)
123{
124 struct wm831x_rtc *wm831x_rtc = dev_get_drvdata(dev);
125 struct wm831x *wm831x = wm831x_rtc->wm831x;
126 u16 time1[2], time2[2];
127 int ret;
128 int count = 0;
129
130
131 ret = wm831x_reg_read(wm831x, WM831X_RTC_CONTROL);
132 if (ret < 0) {
133 dev_err(dev, "Failed to read RTC control: %d\n", ret);
134 return ret;
135 }
136 if (!(ret & WM831X_RTC_VALID)) {
137 dev_dbg(dev, "RTC not yet configured\n");
138 return -EINVAL;
139 }
140
141
142
143
144 do {
145 ret = wm831x_bulk_read(wm831x, WM831X_RTC_TIME_1,
146 2, time1);
147 if (ret != 0)
148 continue;
149
150 ret = wm831x_bulk_read(wm831x, WM831X_RTC_TIME_1,
151 2, time2);
152 if (ret != 0)
153 continue;
154
155 if (memcmp(time1, time2, sizeof(time1)) == 0) {
156 u32 time = (time1[0] << 16) | time1[1];
157
158 rtc_time_to_tm(time, tm);
159 return 0;
160 }
161
162 } while (++count < WM831X_GET_TIME_RETRIES);
163
164 dev_err(dev, "Timed out reading current time\n");
165
166 return -EIO;
167}
168
169
170
171
172static int wm831x_rtc_set_mmss(struct device *dev, unsigned long time)
173{
174 struct wm831x_rtc *wm831x_rtc = dev_get_drvdata(dev);
175 struct wm831x *wm831x = wm831x_rtc->wm831x;
176 struct rtc_time new_tm;
177 unsigned long new_time;
178 int ret;
179 int count = 0;
180
181 ret = wm831x_reg_write(wm831x, WM831X_RTC_TIME_1,
182 (time >> 16) & 0xffff);
183 if (ret < 0) {
184 dev_err(dev, "Failed to write TIME_1: %d\n", ret);
185 return ret;
186 }
187
188 ret = wm831x_reg_write(wm831x, WM831X_RTC_TIME_2, time & 0xffff);
189 if (ret < 0) {
190 dev_err(dev, "Failed to write TIME_2: %d\n", ret);
191 return ret;
192 }
193
194
195
196
197 do {
198 msleep(1);
199
200 ret = wm831x_reg_read(wm831x, WM831X_RTC_CONTROL);
201 if (ret < 0)
202 ret = WM831X_RTC_SYNC_BUSY;
203 } while (!(ret & WM831X_RTC_SYNC_BUSY) &&
204 ++count < WM831X_SET_TIME_RETRIES);
205
206 if (ret & WM831X_RTC_SYNC_BUSY) {
207 dev_err(dev, "Timed out writing RTC update\n");
208 return -EIO;
209 }
210
211
212
213
214 ret = wm831x_rtc_readtime(dev, &new_tm);
215 if (ret < 0)
216 return ret;
217
218 ret = rtc_tm_to_time(&new_tm, &new_time);
219 if (ret < 0) {
220 dev_err(dev, "Failed to convert time: %d\n", ret);
221 return ret;
222 }
223
224
225 if (new_time - time > 1) {
226 dev_err(dev, "RTC update not permitted by hardware\n");
227 return -EPERM;
228 }
229
230 return 0;
231}
232
233
234
235
236static int wm831x_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm)
237{
238 struct wm831x_rtc *wm831x_rtc = dev_get_drvdata(dev);
239 int ret;
240 u16 data[2];
241 u32 time;
242
243 ret = wm831x_bulk_read(wm831x_rtc->wm831x, WM831X_RTC_ALARM_1,
244 2, data);
245 if (ret != 0) {
246 dev_err(dev, "Failed to read alarm time: %d\n", ret);
247 return ret;
248 }
249
250 time = (data[0] << 16) | data[1];
251
252 rtc_time_to_tm(time, &alrm->time);
253
254 ret = wm831x_reg_read(wm831x_rtc->wm831x, WM831X_RTC_CONTROL);
255 if (ret < 0) {
256 dev_err(dev, "Failed to read RTC control: %d\n", ret);
257 return ret;
258 }
259
260 if (ret & WM831X_RTC_ALM_ENA)
261 alrm->enabled = 1;
262 else
263 alrm->enabled = 0;
264
265 return 0;
266}
267
268static int wm831x_rtc_stop_alarm(struct wm831x_rtc *wm831x_rtc)
269{
270 wm831x_rtc->alarm_enabled = 0;
271
272 return wm831x_set_bits(wm831x_rtc->wm831x, WM831X_RTC_CONTROL,
273 WM831X_RTC_ALM_ENA, 0);
274}
275
276static int wm831x_rtc_start_alarm(struct wm831x_rtc *wm831x_rtc)
277{
278 wm831x_rtc->alarm_enabled = 1;
279
280 return wm831x_set_bits(wm831x_rtc->wm831x, WM831X_RTC_CONTROL,
281 WM831X_RTC_ALM_ENA, WM831X_RTC_ALM_ENA);
282}
283
284static int wm831x_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
285{
286 struct wm831x_rtc *wm831x_rtc = dev_get_drvdata(dev);
287 struct wm831x *wm831x = wm831x_rtc->wm831x;
288 int ret;
289 unsigned long time;
290
291 ret = rtc_tm_to_time(&alrm->time, &time);
292 if (ret < 0) {
293 dev_err(dev, "Failed to convert time: %d\n", ret);
294 return ret;
295 }
296
297 ret = wm831x_rtc_stop_alarm(wm831x_rtc);
298 if (ret < 0) {
299 dev_err(dev, "Failed to stop alarm: %d\n", ret);
300 return ret;
301 }
302
303 ret = wm831x_reg_write(wm831x, WM831X_RTC_ALARM_1,
304 (time >> 16) & 0xffff);
305 if (ret < 0) {
306 dev_err(dev, "Failed to write ALARM_1: %d\n", ret);
307 return ret;
308 }
309
310 ret = wm831x_reg_write(wm831x, WM831X_RTC_ALARM_2, time & 0xffff);
311 if (ret < 0) {
312 dev_err(dev, "Failed to write ALARM_2: %d\n", ret);
313 return ret;
314 }
315
316 if (alrm->enabled) {
317 ret = wm831x_rtc_start_alarm(wm831x_rtc);
318 if (ret < 0) {
319 dev_err(dev, "Failed to start alarm: %d\n", ret);
320 return ret;
321 }
322 }
323
324 return 0;
325}
326
327static int wm831x_rtc_alarm_irq_enable(struct device *dev,
328 unsigned int enabled)
329{
330 struct wm831x_rtc *wm831x_rtc = dev_get_drvdata(dev);
331
332 if (enabled)
333 return wm831x_rtc_start_alarm(wm831x_rtc);
334 else
335 return wm831x_rtc_stop_alarm(wm831x_rtc);
336}
337
338static irqreturn_t wm831x_alm_irq(int irq, void *data)
339{
340 struct wm831x_rtc *wm831x_rtc = data;
341
342 rtc_update_irq(wm831x_rtc->rtc, 1, RTC_IRQF | RTC_AF);
343
344 return IRQ_HANDLED;
345}
346
347static const struct rtc_class_ops wm831x_rtc_ops = {
348 .read_time = wm831x_rtc_readtime,
349 .set_mmss = wm831x_rtc_set_mmss,
350 .read_alarm = wm831x_rtc_readalarm,
351 .set_alarm = wm831x_rtc_setalarm,
352 .alarm_irq_enable = wm831x_rtc_alarm_irq_enable,
353};
354
355#ifdef CONFIG_PM
356
357static int wm831x_rtc_suspend(struct device *dev)
358{
359 struct platform_device *pdev = to_platform_device(dev);
360 struct wm831x_rtc *wm831x_rtc = dev_get_drvdata(&pdev->dev);
361 int ret, enable;
362
363 if (wm831x_rtc->alarm_enabled && device_may_wakeup(&pdev->dev))
364 enable = WM831X_RTC_ALM_ENA;
365 else
366 enable = 0;
367
368 ret = wm831x_set_bits(wm831x_rtc->wm831x, WM831X_RTC_CONTROL,
369 WM831X_RTC_ALM_ENA, enable);
370 if (ret != 0)
371 dev_err(&pdev->dev, "Failed to update RTC alarm: %d\n", ret);
372
373 return 0;
374}
375
376
377
378
379static int wm831x_rtc_resume(struct device *dev)
380{
381 struct platform_device *pdev = to_platform_device(dev);
382 struct wm831x_rtc *wm831x_rtc = dev_get_drvdata(&pdev->dev);
383 int ret;
384
385 if (wm831x_rtc->alarm_enabled) {
386 ret = wm831x_rtc_start_alarm(wm831x_rtc);
387 if (ret != 0)
388 dev_err(&pdev->dev,
389 "Failed to restart RTC alarm: %d\n", ret);
390 }
391
392 return 0;
393}
394
395
396static int wm831x_rtc_freeze(struct device *dev)
397{
398 struct platform_device *pdev = to_platform_device(dev);
399 struct wm831x_rtc *wm831x_rtc = dev_get_drvdata(&pdev->dev);
400 int ret;
401
402 ret = wm831x_set_bits(wm831x_rtc->wm831x, WM831X_RTC_CONTROL,
403 WM831X_RTC_ALM_ENA, 0);
404 if (ret != 0)
405 dev_err(&pdev->dev, "Failed to stop RTC alarm: %d\n", ret);
406
407 return 0;
408}
409#else
410#define wm831x_rtc_suspend NULL
411#define wm831x_rtc_resume NULL
412#define wm831x_rtc_freeze NULL
413#endif
414
415static int wm831x_rtc_probe(struct platform_device *pdev)
416{
417 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
418 struct wm831x_rtc *wm831x_rtc;
419 int alm_irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "ALM"));
420 int ret = 0;
421
422 wm831x_rtc = devm_kzalloc(&pdev->dev, sizeof(*wm831x_rtc), GFP_KERNEL);
423 if (wm831x_rtc == NULL)
424 return -ENOMEM;
425
426 platform_set_drvdata(pdev, wm831x_rtc);
427 wm831x_rtc->wm831x = wm831x;
428
429 ret = wm831x_reg_read(wm831x, WM831X_RTC_CONTROL);
430 if (ret < 0) {
431 dev_err(&pdev->dev, "Failed to read RTC control: %d\n", ret);
432 goto err;
433 }
434 if (ret & WM831X_RTC_ALM_ENA)
435 wm831x_rtc->alarm_enabled = 1;
436
437 device_init_wakeup(&pdev->dev, 1);
438
439 wm831x_rtc->rtc = devm_rtc_device_register(&pdev->dev, "wm831x",
440 &wm831x_rtc_ops, THIS_MODULE);
441 if (IS_ERR(wm831x_rtc->rtc)) {
442 ret = PTR_ERR(wm831x_rtc->rtc);
443 goto err;
444 }
445
446 ret = devm_request_threaded_irq(&pdev->dev, alm_irq, NULL,
447 wm831x_alm_irq,
448 IRQF_TRIGGER_RISING, "RTC alarm",
449 wm831x_rtc);
450 if (ret != 0) {
451 dev_err(&pdev->dev, "Failed to request alarm IRQ %d: %d\n",
452 alm_irq, ret);
453 }
454
455 wm831x_rtc_add_randomness(wm831x);
456
457 return 0;
458
459err:
460 return ret;
461}
462
463static const struct dev_pm_ops wm831x_rtc_pm_ops = {
464 .suspend = wm831x_rtc_suspend,
465 .resume = wm831x_rtc_resume,
466
467 .freeze = wm831x_rtc_freeze,
468 .thaw = wm831x_rtc_resume,
469 .restore = wm831x_rtc_resume,
470
471 .poweroff = wm831x_rtc_suspend,
472};
473
474static struct platform_driver wm831x_rtc_driver = {
475 .probe = wm831x_rtc_probe,
476 .driver = {
477 .name = "wm831x-rtc",
478 .pm = &wm831x_rtc_pm_ops,
479 },
480};
481
482module_platform_driver(wm831x_rtc_driver);
483
484MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
485MODULE_DESCRIPTION("RTC driver for the WM831x series PMICs");
486MODULE_LICENSE("GPL");
487MODULE_ALIAS("platform:wm831x-rtc");
488