]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/rtc/rtc-tps65910.c
rtc: tps65910: Add RTC calibration support
[karo-tx-linux.git] / drivers / rtc / rtc-tps65910.c
1 /*
2  * rtc-tps65910.c -- TPS65910 Real Time Clock interface
3  *
4  * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
5  * Author: Venu Byravarasu <vbyravarasu@nvidia.com>
6  *
7  * Based on original TI driver rtc-twl.c
8  *   Copyright (C) 2007 MontaVista Software, Inc
9  *   Author: Alexandre Rusev <source@mvista.com>
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version
14  * 2 of the License, or (at your option) any later version.
15  */
16
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
19 #include <linux/init.h>
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/rtc.h>
23 #include <linux/bcd.h>
24 #include <linux/math64.h>
25 #include <linux/platform_device.h>
26 #include <linux/interrupt.h>
27 #include <linux/mfd/tps65910.h>
28
29 struct tps65910_rtc {
30         struct rtc_device       *rtc;
31         int irq;
32 };
33
34 /* Total number of RTC registers needed to set time*/
35 #define NUM_TIME_REGS   (TPS65910_YEARS - TPS65910_SECONDS + 1)
36
37 /* Total number of RTC registers needed to set compensation registers */
38 #define NUM_COMP_REGS   (TPS65910_RTC_COMP_MSB - TPS65910_RTC_COMP_LSB + 1)
39
40 /* Min and max values supported with 'offset' interface (swapped sign) */
41 #define MIN_OFFSET      (-277761)
42 #define MAX_OFFSET      (277778)
43
44 /* Number of ticks per hour */
45 #define TICKS_PER_HOUR  (32768 * 3600)
46
47 /* Multiplier for ppb conversions */
48 #define PPB_MULT        (1000000000LL)
49
50 static int tps65910_rtc_alarm_irq_enable(struct device *dev, unsigned enabled)
51 {
52         struct tps65910 *tps = dev_get_drvdata(dev->parent);
53         u8 val = 0;
54
55         if (enabled)
56                 val = TPS65910_RTC_INTERRUPTS_IT_ALARM;
57
58         return regmap_write(tps->regmap, TPS65910_RTC_INTERRUPTS, val);
59 }
60
61 /*
62  * Gets current tps65910 RTC time and date parameters.
63  *
64  * The RTC's time/alarm representation is not what gmtime(3) requires
65  * Linux to use:
66  *
67  *  - Months are 1..12 vs Linux 0-11
68  *  - Years are 0..99 vs Linux 1900..N (we assume 21st century)
69  */
70 static int tps65910_rtc_read_time(struct device *dev, struct rtc_time *tm)
71 {
72         unsigned char rtc_data[NUM_TIME_REGS];
73         struct tps65910 *tps = dev_get_drvdata(dev->parent);
74         int ret;
75
76         /* Copy RTC counting registers to static registers or latches */
77         ret = regmap_update_bits(tps->regmap, TPS65910_RTC_CTRL,
78                 TPS65910_RTC_CTRL_GET_TIME, TPS65910_RTC_CTRL_GET_TIME);
79         if (ret < 0) {
80                 dev_err(dev, "RTC CTRL reg update failed with err:%d\n", ret);
81                 return ret;
82         }
83
84         ret = regmap_bulk_read(tps->regmap, TPS65910_SECONDS, rtc_data,
85                 NUM_TIME_REGS);
86         if (ret < 0) {
87                 dev_err(dev, "reading from RTC failed with err:%d\n", ret);
88                 return ret;
89         }
90
91         tm->tm_sec = bcd2bin(rtc_data[0]);
92         tm->tm_min = bcd2bin(rtc_data[1]);
93         tm->tm_hour = bcd2bin(rtc_data[2]);
94         tm->tm_mday = bcd2bin(rtc_data[3]);
95         tm->tm_mon = bcd2bin(rtc_data[4]) - 1;
96         tm->tm_year = bcd2bin(rtc_data[5]) + 100;
97
98         return ret;
99 }
100
101 static int tps65910_rtc_set_time(struct device *dev, struct rtc_time *tm)
102 {
103         unsigned char rtc_data[NUM_TIME_REGS];
104         struct tps65910 *tps = dev_get_drvdata(dev->parent);
105         int ret;
106
107         rtc_data[0] = bin2bcd(tm->tm_sec);
108         rtc_data[1] = bin2bcd(tm->tm_min);
109         rtc_data[2] = bin2bcd(tm->tm_hour);
110         rtc_data[3] = bin2bcd(tm->tm_mday);
111         rtc_data[4] = bin2bcd(tm->tm_mon + 1);
112         rtc_data[5] = bin2bcd(tm->tm_year - 100);
113
114         /* Stop RTC while updating the RTC time registers */
115         ret = regmap_update_bits(tps->regmap, TPS65910_RTC_CTRL,
116                 TPS65910_RTC_CTRL_STOP_RTC, 0);
117         if (ret < 0) {
118                 dev_err(dev, "RTC stop failed with err:%d\n", ret);
119                 return ret;
120         }
121
122         /* update all the time registers in one shot */
123         ret = regmap_bulk_write(tps->regmap, TPS65910_SECONDS, rtc_data,
124                 NUM_TIME_REGS);
125         if (ret < 0) {
126                 dev_err(dev, "rtc_set_time error %d\n", ret);
127                 return ret;
128         }
129
130         /* Start back RTC */
131         ret = regmap_update_bits(tps->regmap, TPS65910_RTC_CTRL,
132                 TPS65910_RTC_CTRL_STOP_RTC, 1);
133         if (ret < 0)
134                 dev_err(dev, "RTC start failed with err:%d\n", ret);
135
136         return ret;
137 }
138
139 /*
140  * Gets current tps65910 RTC alarm time.
141  */
142 static int tps65910_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
143 {
144         unsigned char alarm_data[NUM_TIME_REGS];
145         u32 int_val;
146         struct tps65910 *tps = dev_get_drvdata(dev->parent);
147         int ret;
148
149         ret = regmap_bulk_read(tps->regmap, TPS65910_SECONDS, alarm_data,
150                 NUM_TIME_REGS);
151         if (ret < 0) {
152                 dev_err(dev, "rtc_read_alarm error %d\n", ret);
153                 return ret;
154         }
155
156         alm->time.tm_sec = bcd2bin(alarm_data[0]);
157         alm->time.tm_min = bcd2bin(alarm_data[1]);
158         alm->time.tm_hour = bcd2bin(alarm_data[2]);
159         alm->time.tm_mday = bcd2bin(alarm_data[3]);
160         alm->time.tm_mon = bcd2bin(alarm_data[4]) - 1;
161         alm->time.tm_year = bcd2bin(alarm_data[5]) + 100;
162
163         ret = regmap_read(tps->regmap, TPS65910_RTC_INTERRUPTS, &int_val);
164         if (ret < 0)
165                 return ret;
166
167         if (int_val & TPS65910_RTC_INTERRUPTS_IT_ALARM)
168                 alm->enabled = 1;
169
170         return ret;
171 }
172
173 static int tps65910_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
174 {
175         unsigned char alarm_data[NUM_TIME_REGS];
176         struct tps65910 *tps = dev_get_drvdata(dev->parent);
177         int ret;
178
179         ret = tps65910_rtc_alarm_irq_enable(dev, 0);
180         if (ret)
181                 return ret;
182
183         alarm_data[0] = bin2bcd(alm->time.tm_sec);
184         alarm_data[1] = bin2bcd(alm->time.tm_min);
185         alarm_data[2] = bin2bcd(alm->time.tm_hour);
186         alarm_data[3] = bin2bcd(alm->time.tm_mday);
187         alarm_data[4] = bin2bcd(alm->time.tm_mon + 1);
188         alarm_data[5] = bin2bcd(alm->time.tm_year - 100);
189
190         /* update all the alarm registers in one shot */
191         ret = regmap_bulk_write(tps->regmap, TPS65910_ALARM_SECONDS,
192                 alarm_data, NUM_TIME_REGS);
193         if (ret) {
194                 dev_err(dev, "rtc_set_alarm error %d\n", ret);
195                 return ret;
196         }
197
198         if (alm->enabled)
199                 ret = tps65910_rtc_alarm_irq_enable(dev, 1);
200
201         return ret;
202 }
203
204 static int tps65910_rtc_set_calibration(struct device *dev, int calibration)
205 {
206         unsigned char comp_data[NUM_COMP_REGS];
207         struct tps65910 *tps = dev_get_drvdata(dev->parent);
208         s16 value;
209         int ret;
210
211         /*
212          * TPS65910 uses two's complement 16 bit value for compensation for RTC
213          * crystal inaccuracies. One time every hour when seconds counter
214          * increments from 0 to 1 compensation value will be added to internal
215          * RTC counter value.
216          *
217          * Compensation value 0x7FFF is prohibited value.
218          *
219          * Valid range for compensation value: [-32768 .. 32766]
220          */
221         if ((calibration < -32768) || (calibration > 32766)) {
222                 dev_err(dev, "RTC calibration value out of range: %d\n",
223                         calibration);
224                 return -EINVAL;
225         }
226
227         value = (s16)calibration;
228
229         comp_data[0] = (u16)value & 0xFF;
230         comp_data[1] = ((u16)value >> 8) & 0xFF;
231
232         /* Update all the compensation registers in one shot */
233         ret = regmap_bulk_write(tps->regmap, TPS65910_RTC_COMP_LSB,
234                 comp_data, NUM_COMP_REGS);
235         if (ret < 0) {
236                 dev_err(dev, "rtc_set_calibration error: %d\n", ret);
237                 return ret;
238         }
239
240         /* Enable automatic compensation */
241         ret = regmap_update_bits(tps->regmap, TPS65910_RTC_CTRL,
242                 TPS65910_RTC_CTRL_AUTO_COMP, TPS65910_RTC_CTRL_AUTO_COMP);
243         if (ret < 0)
244                 dev_err(dev, "auto_comp enable failed with error: %d\n", ret);
245
246         return ret;
247 }
248
249 static int tps65910_rtc_get_calibration(struct device *dev, int *calibration)
250 {
251         unsigned char comp_data[NUM_COMP_REGS];
252         struct tps65910 *tps = dev_get_drvdata(dev->parent);
253         unsigned int ctrl;
254         u16 value;
255         int ret;
256
257         ret = regmap_read(tps->regmap, TPS65910_RTC_CTRL, &ctrl);
258         if (ret < 0)
259                 return ret;
260
261         /* If automatic compensation is not enabled report back zero */
262         if (!(ctrl & TPS65910_RTC_CTRL_AUTO_COMP)) {
263                 *calibration = 0;
264                 return 0;
265         }
266
267         ret = regmap_bulk_read(tps->regmap, TPS65910_RTC_COMP_LSB, comp_data,
268                 NUM_COMP_REGS);
269         if (ret < 0) {
270                 dev_err(dev, "rtc_get_calibration error: %d\n", ret);
271                 return ret;
272         }
273
274         value = (u16)comp_data[0] | ((u16)comp_data[1] << 8);
275
276         *calibration = (s16)value;
277
278         return 0;
279 }
280
281 static int tps65910_read_offset(struct device *dev, long *offset)
282 {
283         int calibration;
284         s64 tmp;
285         int ret;
286
287         ret = tps65910_rtc_get_calibration(dev, &calibration);
288         if (ret < 0)
289                 return ret;
290
291         /* Convert from RTC calibration register format to ppb format */
292         tmp = calibration * (s64)PPB_MULT;
293         if (tmp < 0)
294                 tmp -= TICKS_PER_HOUR / 2LL;
295         else
296                 tmp += TICKS_PER_HOUR / 2LL;
297         tmp = div_s64(tmp, TICKS_PER_HOUR);
298
299         /* Offset value operates in negative way, so swap sign */
300         *offset = (long)-tmp;
301
302         return 0;
303 }
304
305 static int tps65910_set_offset(struct device *dev, long offset)
306 {
307         int calibration;
308         s64 tmp;
309         int ret;
310
311         /* Make sure offset value is within supported range */
312         if (offset < MIN_OFFSET || offset > MAX_OFFSET)
313                 return -ERANGE;
314
315         /* Convert from ppb format to RTC calibration register format */
316         tmp = offset * (s64)TICKS_PER_HOUR;
317         if (tmp < 0)
318                 tmp -= PPB_MULT / 2LL;
319         else
320                 tmp += PPB_MULT / 2LL;
321         tmp = div_s64(tmp, PPB_MULT);
322
323         /* Offset value operates in negative way, so swap sign */
324         calibration = (int)-tmp;
325
326         ret = tps65910_rtc_set_calibration(dev, calibration);
327
328         return ret;
329 }
330
331 static irqreturn_t tps65910_rtc_interrupt(int irq, void *rtc)
332 {
333         struct device *dev = rtc;
334         unsigned long events = 0;
335         struct tps65910 *tps = dev_get_drvdata(dev->parent);
336         struct tps65910_rtc *tps_rtc = dev_get_drvdata(dev);
337         int ret;
338         u32 rtc_reg;
339
340         ret = regmap_read(tps->regmap, TPS65910_RTC_STATUS, &rtc_reg);
341         if (ret)
342                 return IRQ_NONE;
343
344         if (rtc_reg & TPS65910_RTC_STATUS_ALARM)
345                 events = RTC_IRQF | RTC_AF;
346
347         ret = regmap_write(tps->regmap, TPS65910_RTC_STATUS, rtc_reg);
348         if (ret)
349                 return IRQ_NONE;
350
351         /* Notify RTC core on event */
352         rtc_update_irq(tps_rtc->rtc, 1, events);
353
354         return IRQ_HANDLED;
355 }
356
357 static const struct rtc_class_ops tps65910_rtc_ops = {
358         .read_time      = tps65910_rtc_read_time,
359         .set_time       = tps65910_rtc_set_time,
360         .read_alarm     = tps65910_rtc_read_alarm,
361         .set_alarm      = tps65910_rtc_set_alarm,
362         .alarm_irq_enable = tps65910_rtc_alarm_irq_enable,
363         .read_offset    = tps65910_read_offset,
364         .set_offset     = tps65910_set_offset,
365 };
366
367 static int tps65910_rtc_probe(struct platform_device *pdev)
368 {
369         struct tps65910 *tps65910 = NULL;
370         struct tps65910_rtc *tps_rtc = NULL;
371         int ret;
372         int irq;
373         u32 rtc_reg;
374
375         tps65910 = dev_get_drvdata(pdev->dev.parent);
376
377         tps_rtc = devm_kzalloc(&pdev->dev, sizeof(struct tps65910_rtc),
378                         GFP_KERNEL);
379         if (!tps_rtc)
380                 return -ENOMEM;
381
382         /* Clear pending interrupts */
383         ret = regmap_read(tps65910->regmap, TPS65910_RTC_STATUS, &rtc_reg);
384         if (ret < 0)
385                 return ret;
386
387         ret = regmap_write(tps65910->regmap, TPS65910_RTC_STATUS, rtc_reg);
388         if (ret < 0)
389                 return ret;
390
391         dev_dbg(&pdev->dev, "Enabling rtc-tps65910.\n");
392
393         /* Enable RTC digital power domain */
394         ret = regmap_update_bits(tps65910->regmap, TPS65910_DEVCTRL,
395                 DEVCTRL_RTC_PWDN_MASK, 0 << DEVCTRL_RTC_PWDN_SHIFT);
396         if (ret < 0)
397                 return ret;
398
399         rtc_reg = TPS65910_RTC_CTRL_STOP_RTC;
400         ret = regmap_write(tps65910->regmap, TPS65910_RTC_CTRL, rtc_reg);
401         if (ret < 0)
402                 return ret;
403
404         platform_set_drvdata(pdev, tps_rtc);
405
406         irq  = platform_get_irq(pdev, 0);
407         if (irq <= 0) {
408                 dev_warn(&pdev->dev, "Wake up is not possible as irq = %d\n",
409                         irq);
410                 return -ENXIO;
411         }
412
413         ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
414                 tps65910_rtc_interrupt, IRQF_TRIGGER_LOW,
415                 dev_name(&pdev->dev), &pdev->dev);
416         if (ret < 0) {
417                 dev_err(&pdev->dev, "IRQ is not free.\n");
418                 return ret;
419         }
420         tps_rtc->irq = irq;
421         device_set_wakeup_capable(&pdev->dev, 1);
422
423         tps_rtc->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
424                 &tps65910_rtc_ops, THIS_MODULE);
425         if (IS_ERR(tps_rtc->rtc)) {
426                 ret = PTR_ERR(tps_rtc->rtc);
427                 dev_err(&pdev->dev, "RTC device register: err %d\n", ret);
428                 return ret;
429         }
430
431         return 0;
432 }
433
434 /*
435  * Disable tps65910 RTC interrupts.
436  * Sets status flag to free.
437  */
438 static int tps65910_rtc_remove(struct platform_device *pdev)
439 {
440         tps65910_rtc_alarm_irq_enable(&pdev->dev, 0);
441
442         return 0;
443 }
444
445 #ifdef CONFIG_PM_SLEEP
446 static int tps65910_rtc_suspend(struct device *dev)
447 {
448         struct tps65910_rtc *tps_rtc = dev_get_drvdata(dev);
449
450         if (device_may_wakeup(dev))
451                 enable_irq_wake(tps_rtc->irq);
452         return 0;
453 }
454
455 static int tps65910_rtc_resume(struct device *dev)
456 {
457         struct tps65910_rtc *tps_rtc = dev_get_drvdata(dev);
458
459         if (device_may_wakeup(dev))
460                 disable_irq_wake(tps_rtc->irq);
461         return 0;
462 }
463 #endif
464
465 static SIMPLE_DEV_PM_OPS(tps65910_rtc_pm_ops, tps65910_rtc_suspend,
466                         tps65910_rtc_resume);
467
468 static struct platform_driver tps65910_rtc_driver = {
469         .probe          = tps65910_rtc_probe,
470         .remove         = tps65910_rtc_remove,
471         .driver         = {
472                 .name   = "tps65910-rtc",
473                 .pm     = &tps65910_rtc_pm_ops,
474         },
475 };
476
477 module_platform_driver(tps65910_rtc_driver);
478 MODULE_ALIAS("platform:rtc-tps65910");
479 MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
480 MODULE_LICENSE("GPL");