2 * rtc-tps65910.c -- TPS65910 Real Time Clock interface
4 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
5 * Author: Venu Byravarasu <vbyravarasu@nvidia.com>
7 * Based on original TI driver rtc-twl.c
8 * Copyright (C) 2007 MontaVista Software, Inc
9 * Author: Alexandre Rusev <source@mvista.com>
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.
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>
30 struct rtc_device *rtc;
34 /* Total number of RTC registers needed to set time*/
35 #define NUM_TIME_REGS (TPS65910_YEARS - TPS65910_SECONDS + 1)
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)
40 /* Min and max values supported with 'offset' interface (swapped sign) */
41 #define MIN_OFFSET (-277761)
42 #define MAX_OFFSET (277778)
44 /* Number of ticks per hour */
45 #define TICKS_PER_HOUR (32768 * 3600)
47 /* Multiplier for ppb conversions */
48 #define PPB_MULT (1000000000LL)
50 static int tps65910_rtc_alarm_irq_enable(struct device *dev, unsigned enabled)
52 struct tps65910 *tps = dev_get_drvdata(dev->parent);
56 val = TPS65910_RTC_INTERRUPTS_IT_ALARM;
58 return regmap_write(tps->regmap, TPS65910_RTC_INTERRUPTS, val);
62 * Gets current tps65910 RTC time and date parameters.
64 * The RTC's time/alarm representation is not what gmtime(3) requires
67 * - Months are 1..12 vs Linux 0-11
68 * - Years are 0..99 vs Linux 1900..N (we assume 21st century)
70 static int tps65910_rtc_read_time(struct device *dev, struct rtc_time *tm)
72 unsigned char rtc_data[NUM_TIME_REGS];
73 struct tps65910 *tps = dev_get_drvdata(dev->parent);
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);
80 dev_err(dev, "RTC CTRL reg update failed with err:%d\n", ret);
84 ret = regmap_bulk_read(tps->regmap, TPS65910_SECONDS, rtc_data,
87 dev_err(dev, "reading from RTC failed with err:%d\n", ret);
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;
101 static int tps65910_rtc_set_time(struct device *dev, struct rtc_time *tm)
103 unsigned char rtc_data[NUM_TIME_REGS];
104 struct tps65910 *tps = dev_get_drvdata(dev->parent);
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);
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);
118 dev_err(dev, "RTC stop failed with err:%d\n", ret);
122 /* update all the time registers in one shot */
123 ret = regmap_bulk_write(tps->regmap, TPS65910_SECONDS, rtc_data,
126 dev_err(dev, "rtc_set_time error %d\n", ret);
131 ret = regmap_update_bits(tps->regmap, TPS65910_RTC_CTRL,
132 TPS65910_RTC_CTRL_STOP_RTC, 1);
134 dev_err(dev, "RTC start failed with err:%d\n", ret);
140 * Gets current tps65910 RTC alarm time.
142 static int tps65910_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
144 unsigned char alarm_data[NUM_TIME_REGS];
146 struct tps65910 *tps = dev_get_drvdata(dev->parent);
149 ret = regmap_bulk_read(tps->regmap, TPS65910_SECONDS, alarm_data,
152 dev_err(dev, "rtc_read_alarm error %d\n", ret);
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;
163 ret = regmap_read(tps->regmap, TPS65910_RTC_INTERRUPTS, &int_val);
167 if (int_val & TPS65910_RTC_INTERRUPTS_IT_ALARM)
173 static int tps65910_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
175 unsigned char alarm_data[NUM_TIME_REGS];
176 struct tps65910 *tps = dev_get_drvdata(dev->parent);
179 ret = tps65910_rtc_alarm_irq_enable(dev, 0);
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);
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);
194 dev_err(dev, "rtc_set_alarm error %d\n", ret);
199 ret = tps65910_rtc_alarm_irq_enable(dev, 1);
204 static int tps65910_rtc_set_calibration(struct device *dev, int calibration)
206 unsigned char comp_data[NUM_COMP_REGS];
207 struct tps65910 *tps = dev_get_drvdata(dev->parent);
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
217 * Compensation value 0x7FFF is prohibited value.
219 * Valid range for compensation value: [-32768 .. 32766]
221 if ((calibration < -32768) || (calibration > 32766)) {
222 dev_err(dev, "RTC calibration value out of range: %d\n",
227 value = (s16)calibration;
229 comp_data[0] = (u16)value & 0xFF;
230 comp_data[1] = ((u16)value >> 8) & 0xFF;
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);
236 dev_err(dev, "rtc_set_calibration error: %d\n", ret);
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);
244 dev_err(dev, "auto_comp enable failed with error: %d\n", ret);
249 static int tps65910_rtc_get_calibration(struct device *dev, int *calibration)
251 unsigned char comp_data[NUM_COMP_REGS];
252 struct tps65910 *tps = dev_get_drvdata(dev->parent);
257 ret = regmap_read(tps->regmap, TPS65910_RTC_CTRL, &ctrl);
261 /* If automatic compensation is not enabled report back zero */
262 if (!(ctrl & TPS65910_RTC_CTRL_AUTO_COMP)) {
267 ret = regmap_bulk_read(tps->regmap, TPS65910_RTC_COMP_LSB, comp_data,
270 dev_err(dev, "rtc_get_calibration error: %d\n", ret);
274 value = (u16)comp_data[0] | ((u16)comp_data[1] << 8);
276 *calibration = (s16)value;
281 static int tps65910_read_offset(struct device *dev, long *offset)
287 ret = tps65910_rtc_get_calibration(dev, &calibration);
291 /* Convert from RTC calibration register format to ppb format */
292 tmp = calibration * (s64)PPB_MULT;
294 tmp -= TICKS_PER_HOUR / 2LL;
296 tmp += TICKS_PER_HOUR / 2LL;
297 tmp = div_s64(tmp, TICKS_PER_HOUR);
299 /* Offset value operates in negative way, so swap sign */
300 *offset = (long)-tmp;
305 static int tps65910_set_offset(struct device *dev, long offset)
311 /* Make sure offset value is within supported range */
312 if (offset < MIN_OFFSET || offset > MAX_OFFSET)
315 /* Convert from ppb format to RTC calibration register format */
316 tmp = offset * (s64)TICKS_PER_HOUR;
318 tmp -= PPB_MULT / 2LL;
320 tmp += PPB_MULT / 2LL;
321 tmp = div_s64(tmp, PPB_MULT);
323 /* Offset value operates in negative way, so swap sign */
324 calibration = (int)-tmp;
326 ret = tps65910_rtc_set_calibration(dev, calibration);
331 static irqreturn_t tps65910_rtc_interrupt(int irq, void *rtc)
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);
340 ret = regmap_read(tps->regmap, TPS65910_RTC_STATUS, &rtc_reg);
344 if (rtc_reg & TPS65910_RTC_STATUS_ALARM)
345 events = RTC_IRQF | RTC_AF;
347 ret = regmap_write(tps->regmap, TPS65910_RTC_STATUS, rtc_reg);
351 /* Notify RTC core on event */
352 rtc_update_irq(tps_rtc->rtc, 1, events);
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,
367 static int tps65910_rtc_probe(struct platform_device *pdev)
369 struct tps65910 *tps65910 = NULL;
370 struct tps65910_rtc *tps_rtc = NULL;
375 tps65910 = dev_get_drvdata(pdev->dev.parent);
377 tps_rtc = devm_kzalloc(&pdev->dev, sizeof(struct tps65910_rtc),
382 /* Clear pending interrupts */
383 ret = regmap_read(tps65910->regmap, TPS65910_RTC_STATUS, &rtc_reg);
387 ret = regmap_write(tps65910->regmap, TPS65910_RTC_STATUS, rtc_reg);
391 dev_dbg(&pdev->dev, "Enabling rtc-tps65910.\n");
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);
399 rtc_reg = TPS65910_RTC_CTRL_STOP_RTC;
400 ret = regmap_write(tps65910->regmap, TPS65910_RTC_CTRL, rtc_reg);
404 platform_set_drvdata(pdev, tps_rtc);
406 irq = platform_get_irq(pdev, 0);
408 dev_warn(&pdev->dev, "Wake up is not possible as irq = %d\n",
413 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
414 tps65910_rtc_interrupt, IRQF_TRIGGER_LOW,
415 dev_name(&pdev->dev), &pdev->dev);
417 dev_err(&pdev->dev, "IRQ is not free.\n");
421 device_set_wakeup_capable(&pdev->dev, 1);
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);
435 * Disable tps65910 RTC interrupts.
436 * Sets status flag to free.
438 static int tps65910_rtc_remove(struct platform_device *pdev)
440 tps65910_rtc_alarm_irq_enable(&pdev->dev, 0);
445 #ifdef CONFIG_PM_SLEEP
446 static int tps65910_rtc_suspend(struct device *dev)
448 struct tps65910_rtc *tps_rtc = dev_get_drvdata(dev);
450 if (device_may_wakeup(dev))
451 enable_irq_wake(tps_rtc->irq);
455 static int tps65910_rtc_resume(struct device *dev)
457 struct tps65910_rtc *tps_rtc = dev_get_drvdata(dev);
459 if (device_may_wakeup(dev))
460 disable_irq_wake(tps_rtc->irq);
465 static SIMPLE_DEV_PM_OPS(tps65910_rtc_pm_ops, tps65910_rtc_suspend,
466 tps65910_rtc_resume);
468 static struct platform_driver tps65910_rtc_driver = {
469 .probe = tps65910_rtc_probe,
470 .remove = tps65910_rtc_remove,
472 .name = "tps65910-rtc",
473 .pm = &tps65910_rtc_pm_ops,
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");