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/platform_device.h>
25 #include <linux/pm_runtime.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 static int tps65910_rtc_alarm_irq_enable(struct device *dev, unsigned enabled)
39 struct tps65910 *tps = dev_get_drvdata(dev->parent);
43 val = TPS65910_RTC_INTERRUPTS_IT_ALARM;
45 return regmap_write(tps->regmap, TPS65910_RTC_INTERRUPTS, val);
49 * Gets current tps65910 RTC time and date parameters.
51 * The RTC's time/alarm representation is not what gmtime(3) requires
54 * - Months are 1..12 vs Linux 0-11
55 * - Years are 0..99 vs Linux 1900..N (we assume 21st century)
57 static int tps65910_rtc_read_time(struct device *dev, struct rtc_time *tm)
59 unsigned char rtc_data[NUM_TIME_REGS];
60 struct tps65910 *tps = dev_get_drvdata(dev->parent);
63 /* Copy RTC counting registers to static registers or latches */
64 ret = regmap_update_bits(tps->regmap, TPS65910_RTC_CTRL,
65 TPS65910_RTC_CTRL_GET_TIME, TPS65910_RTC_CTRL_GET_TIME);
67 dev_err(dev, "RTC CTRL reg update failed with err:%d\n", ret);
71 ret = regmap_bulk_read(tps->regmap, TPS65910_SECONDS, rtc_data,
74 dev_err(dev, "reading from RTC failed with err:%d\n", ret);
78 tm->tm_sec = bcd2bin(rtc_data[0]);
79 tm->tm_min = bcd2bin(rtc_data[1]);
80 tm->tm_hour = bcd2bin(rtc_data[2]);
81 tm->tm_mday = bcd2bin(rtc_data[3]);
82 tm->tm_mon = bcd2bin(rtc_data[4]) - 1;
83 tm->tm_year = bcd2bin(rtc_data[5]) + 100;
88 static int tps65910_rtc_set_time(struct device *dev, struct rtc_time *tm)
90 unsigned char rtc_data[NUM_TIME_REGS];
91 struct tps65910 *tps = dev_get_drvdata(dev->parent);
94 rtc_data[0] = bin2bcd(tm->tm_sec);
95 rtc_data[1] = bin2bcd(tm->tm_min);
96 rtc_data[2] = bin2bcd(tm->tm_hour);
97 rtc_data[3] = bin2bcd(tm->tm_mday);
98 rtc_data[4] = bin2bcd(tm->tm_mon + 1);
99 rtc_data[5] = bin2bcd(tm->tm_year - 100);
101 /* Stop RTC while updating the RTC time registers */
102 ret = regmap_update_bits(tps->regmap, TPS65910_RTC_CTRL,
103 TPS65910_RTC_CTRL_STOP_RTC, 0);
105 dev_err(dev, "RTC stop failed with err:%d\n", ret);
109 /* update all the time registers in one shot */
110 ret = regmap_bulk_write(tps->regmap, TPS65910_SECONDS, rtc_data,
113 dev_err(dev, "rtc_set_time error %d\n", ret);
118 ret = regmap_update_bits(tps->regmap, TPS65910_RTC_CTRL,
119 TPS65910_RTC_CTRL_STOP_RTC, 1);
121 dev_err(dev, "RTC start failed with err:%d\n", ret);
127 * Gets current tps65910 RTC alarm time.
129 static int tps65910_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
131 unsigned char alarm_data[NUM_TIME_REGS];
133 struct tps65910 *tps = dev_get_drvdata(dev->parent);
136 ret = regmap_bulk_read(tps->regmap, TPS65910_SECONDS, alarm_data,
139 dev_err(dev, "rtc_read_alarm error %d\n", ret);
143 alm->time.tm_sec = bcd2bin(alarm_data[0]);
144 alm->time.tm_min = bcd2bin(alarm_data[1]);
145 alm->time.tm_hour = bcd2bin(alarm_data[2]);
146 alm->time.tm_mday = bcd2bin(alarm_data[3]);
147 alm->time.tm_mon = bcd2bin(alarm_data[4]) - 1;
148 alm->time.tm_year = bcd2bin(alarm_data[5]) + 100;
150 ret = regmap_read(tps->regmap, TPS65910_RTC_INTERRUPTS, &int_val);
154 if (int_val & TPS65910_RTC_INTERRUPTS_IT_ALARM)
160 static int tps65910_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
162 unsigned char alarm_data[NUM_TIME_REGS];
163 struct tps65910 *tps = dev_get_drvdata(dev->parent);
166 ret = tps65910_rtc_alarm_irq_enable(dev, 0);
170 alarm_data[0] = bin2bcd(alm->time.tm_sec);
171 alarm_data[1] = bin2bcd(alm->time.tm_min);
172 alarm_data[2] = bin2bcd(alm->time.tm_hour);
173 alarm_data[3] = bin2bcd(alm->time.tm_mday);
174 alarm_data[4] = bin2bcd(alm->time.tm_mon + 1);
175 alarm_data[5] = bin2bcd(alm->time.tm_year - 100);
177 /* update all the alarm registers in one shot */
178 ret = regmap_bulk_write(tps->regmap, TPS65910_ALARM_SECONDS,
179 alarm_data, NUM_TIME_REGS);
181 dev_err(dev, "rtc_set_alarm error %d\n", ret);
186 ret = tps65910_rtc_alarm_irq_enable(dev, 1);
191 static irqreturn_t tps65910_rtc_interrupt(int irq, void *rtc)
193 struct device *dev = rtc;
194 unsigned long events = 0;
195 struct tps65910 *tps = dev_get_drvdata(dev->parent);
196 struct tps65910_rtc *tps_rtc = dev_get_drvdata(dev);
200 ret = regmap_read(tps->regmap, TPS65910_RTC_STATUS, &rtc_reg);
204 if (rtc_reg & TPS65910_RTC_STATUS_ALARM)
205 events = RTC_IRQF | RTC_AF;
207 ret = regmap_write(tps->regmap, TPS65910_RTC_STATUS, rtc_reg);
211 /* Notify RTC core on event */
212 rtc_update_irq(tps_rtc->rtc, 1, events);
217 static const struct rtc_class_ops tps65910_rtc_ops = {
218 .read_time = tps65910_rtc_read_time,
219 .set_time = tps65910_rtc_set_time,
220 .read_alarm = tps65910_rtc_read_alarm,
221 .set_alarm = tps65910_rtc_set_alarm,
222 .alarm_irq_enable = tps65910_rtc_alarm_irq_enable,
225 static int tps65910_rtc_probe(struct platform_device *pdev)
227 struct tps65910 *tps65910 = NULL;
228 struct tps65910_rtc *tps_rtc = NULL;
233 tps65910 = dev_get_drvdata(pdev->dev.parent);
235 tps_rtc = devm_kzalloc(&pdev->dev, sizeof(struct tps65910_rtc),
240 /* Clear pending interrupts */
241 ret = regmap_read(tps65910->regmap, TPS65910_RTC_STATUS, &rtc_reg);
245 ret = regmap_write(tps65910->regmap, TPS65910_RTC_STATUS, rtc_reg);
249 dev_dbg(&pdev->dev, "Enabling rtc-tps65910.\n");
251 /* Enable RTC digital power domain */
252 ret = regmap_update_bits(tps65910->regmap, TPS65910_DEVCTRL,
253 DEVCTRL_RTC_PWDN_MASK, 0 << DEVCTRL_RTC_PWDN_SHIFT);
257 rtc_reg = TPS65910_RTC_CTRL_STOP_RTC;
258 ret = regmap_write(tps65910->regmap, TPS65910_RTC_CTRL, rtc_reg);
262 irq = platform_get_irq(pdev, 0);
264 dev_warn(&pdev->dev, "Wake up is not possible as irq = %d\n",
269 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
270 tps65910_rtc_interrupt, IRQF_TRIGGER_LOW | IRQF_EARLY_RESUME,
271 dev_name(&pdev->dev), &pdev->dev);
273 dev_err(&pdev->dev, "IRQ is not free.\n");
277 device_set_wakeup_capable(&pdev->dev, 1);
279 tps_rtc->rtc = rtc_device_register(pdev->name, &pdev->dev,
280 &tps65910_rtc_ops, THIS_MODULE);
281 if (IS_ERR(tps_rtc->rtc)) {
282 ret = PTR_ERR(tps_rtc->rtc);
283 dev_err(&pdev->dev, "RTC device register: err %d\n", ret);
287 platform_set_drvdata(pdev, tps_rtc);
293 * Disable tps65910 RTC interrupts.
294 * Sets status flag to free.
296 static int tps65910_rtc_remove(struct platform_device *pdev)
298 /* leave rtc running, but disable irqs */
299 struct tps65910_rtc *tps_rtc = platform_get_drvdata(pdev);
301 tps65910_rtc_alarm_irq_enable(&pdev->dev, 0);
303 rtc_device_unregister(tps_rtc->rtc);
307 #ifdef CONFIG_PM_SLEEP
308 static int tps65910_rtc_suspend(struct device *dev)
310 struct tps65910_rtc *tps_rtc = dev_get_drvdata(dev);
312 if (device_may_wakeup(dev))
313 enable_irq_wake(tps_rtc->irq);
317 static int tps65910_rtc_resume(struct device *dev)
319 struct tps65910_rtc *tps_rtc = dev_get_drvdata(dev);
321 if (device_may_wakeup(dev))
322 disable_irq_wake(tps_rtc->irq);
327 static const struct dev_pm_ops tps65910_rtc_pm_ops = {
328 SET_SYSTEM_SLEEP_PM_OPS(tps65910_rtc_suspend, tps65910_rtc_resume)
331 static struct platform_driver tps65910_rtc_driver = {
332 .probe = tps65910_rtc_probe,
333 .remove = tps65910_rtc_remove,
335 .owner = THIS_MODULE,
336 .name = "tps65910-rtc",
337 .pm = &tps65910_rtc_pm_ops,
341 module_platform_driver(tps65910_rtc_driver);
342 MODULE_ALIAS("platform:rtc-tps65910");
343 MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
344 MODULE_LICENSE("GPL");