blob: f42aa2b2dcba5942cdbe1f6fa37d888d43504716 [file] [log] [blame]
Venu Byravarasu0e783982012-10-04 17:13:55 -07001/*
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/platform_device.h>
25#include <linux/interrupt.h>
26#include <linux/mfd/tps65910.h>
27
28struct tps65910_rtc {
29 struct rtc_device *rtc;
Laxman Dewanganeb5eba42013-02-21 16:44:35 -080030 int irq;
Venu Byravarasu0e783982012-10-04 17:13:55 -070031};
32
33/* Total number of RTC registers needed to set time*/
34#define NUM_TIME_REGS (TPS65910_YEARS - TPS65910_SECONDS + 1)
35
36static int tps65910_rtc_alarm_irq_enable(struct device *dev, unsigned enabled)
37{
38 struct tps65910 *tps = dev_get_drvdata(dev->parent);
39 u8 val = 0;
40
41 if (enabled)
42 val = TPS65910_RTC_INTERRUPTS_IT_ALARM;
43
44 return regmap_write(tps->regmap, TPS65910_RTC_INTERRUPTS, val);
45}
46
47/*
48 * Gets current tps65910 RTC time and date parameters.
49 *
50 * The RTC's time/alarm representation is not what gmtime(3) requires
51 * Linux to use:
52 *
53 * - Months are 1..12 vs Linux 0-11
54 * - Years are 0..99 vs Linux 1900..N (we assume 21st century)
55 */
56static int tps65910_rtc_read_time(struct device *dev, struct rtc_time *tm)
57{
58 unsigned char rtc_data[NUM_TIME_REGS];
59 struct tps65910 *tps = dev_get_drvdata(dev->parent);
60 int ret;
61
62 /* Copy RTC counting registers to static registers or latches */
63 ret = regmap_update_bits(tps->regmap, TPS65910_RTC_CTRL,
64 TPS65910_RTC_CTRL_GET_TIME, TPS65910_RTC_CTRL_GET_TIME);
65 if (ret < 0) {
66 dev_err(dev, "RTC CTRL reg update failed with err:%d\n", ret);
67 return ret;
68 }
69
70 ret = regmap_bulk_read(tps->regmap, TPS65910_SECONDS, rtc_data,
71 NUM_TIME_REGS);
72 if (ret < 0) {
73 dev_err(dev, "reading from RTC failed with err:%d\n", ret);
74 return ret;
75 }
76
77 tm->tm_sec = bcd2bin(rtc_data[0]);
78 tm->tm_min = bcd2bin(rtc_data[1]);
79 tm->tm_hour = bcd2bin(rtc_data[2]);
80 tm->tm_mday = bcd2bin(rtc_data[3]);
81 tm->tm_mon = bcd2bin(rtc_data[4]) - 1;
82 tm->tm_year = bcd2bin(rtc_data[5]) + 100;
83
84 return ret;
85}
86
87static int tps65910_rtc_set_time(struct device *dev, struct rtc_time *tm)
88{
89 unsigned char rtc_data[NUM_TIME_REGS];
90 struct tps65910 *tps = dev_get_drvdata(dev->parent);
91 int ret;
92
93 rtc_data[0] = bin2bcd(tm->tm_sec);
94 rtc_data[1] = bin2bcd(tm->tm_min);
95 rtc_data[2] = bin2bcd(tm->tm_hour);
96 rtc_data[3] = bin2bcd(tm->tm_mday);
97 rtc_data[4] = bin2bcd(tm->tm_mon + 1);
98 rtc_data[5] = bin2bcd(tm->tm_year - 100);
99
100 /* Stop RTC while updating the RTC time registers */
101 ret = regmap_update_bits(tps->regmap, TPS65910_RTC_CTRL,
102 TPS65910_RTC_CTRL_STOP_RTC, 0);
103 if (ret < 0) {
104 dev_err(dev, "RTC stop failed with err:%d\n", ret);
105 return ret;
106 }
107
108 /* update all the time registers in one shot */
109 ret = regmap_bulk_write(tps->regmap, TPS65910_SECONDS, rtc_data,
110 NUM_TIME_REGS);
111 if (ret < 0) {
112 dev_err(dev, "rtc_set_time error %d\n", ret);
113 return ret;
114 }
115
116 /* Start back RTC */
117 ret = regmap_update_bits(tps->regmap, TPS65910_RTC_CTRL,
118 TPS65910_RTC_CTRL_STOP_RTC, 1);
119 if (ret < 0)
120 dev_err(dev, "RTC start failed with err:%d\n", ret);
121
122 return ret;
123}
124
125/*
126 * Gets current tps65910 RTC alarm time.
127 */
128static int tps65910_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
129{
130 unsigned char alarm_data[NUM_TIME_REGS];
131 u32 int_val;
132 struct tps65910 *tps = dev_get_drvdata(dev->parent);
133 int ret;
134
135 ret = regmap_bulk_read(tps->regmap, TPS65910_SECONDS, alarm_data,
136 NUM_TIME_REGS);
137 if (ret < 0) {
138 dev_err(dev, "rtc_read_alarm error %d\n", ret);
139 return ret;
140 }
141
142 alm->time.tm_sec = bcd2bin(alarm_data[0]);
143 alm->time.tm_min = bcd2bin(alarm_data[1]);
144 alm->time.tm_hour = bcd2bin(alarm_data[2]);
145 alm->time.tm_mday = bcd2bin(alarm_data[3]);
146 alm->time.tm_mon = bcd2bin(alarm_data[4]) - 1;
147 alm->time.tm_year = bcd2bin(alarm_data[5]) + 100;
148
149 ret = regmap_read(tps->regmap, TPS65910_RTC_INTERRUPTS, &int_val);
150 if (ret < 0)
151 return ret;
152
153 if (int_val & TPS65910_RTC_INTERRUPTS_IT_ALARM)
154 alm->enabled = 1;
155
156 return ret;
157}
158
159static int tps65910_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
160{
161 unsigned char alarm_data[NUM_TIME_REGS];
162 struct tps65910 *tps = dev_get_drvdata(dev->parent);
163 int ret;
164
165 ret = tps65910_rtc_alarm_irq_enable(dev, 0);
166 if (ret)
167 return ret;
168
169 alarm_data[0] = bin2bcd(alm->time.tm_sec);
170 alarm_data[1] = bin2bcd(alm->time.tm_min);
171 alarm_data[2] = bin2bcd(alm->time.tm_hour);
172 alarm_data[3] = bin2bcd(alm->time.tm_mday);
173 alarm_data[4] = bin2bcd(alm->time.tm_mon + 1);
174 alarm_data[5] = bin2bcd(alm->time.tm_year - 100);
175
176 /* update all the alarm registers in one shot */
177 ret = regmap_bulk_write(tps->regmap, TPS65910_ALARM_SECONDS,
178 alarm_data, NUM_TIME_REGS);
179 if (ret) {
180 dev_err(dev, "rtc_set_alarm error %d\n", ret);
181 return ret;
182 }
183
184 if (alm->enabled)
185 ret = tps65910_rtc_alarm_irq_enable(dev, 1);
186
187 return ret;
188}
189
190static irqreturn_t tps65910_rtc_interrupt(int irq, void *rtc)
191{
192 struct device *dev = rtc;
193 unsigned long events = 0;
194 struct tps65910 *tps = dev_get_drvdata(dev->parent);
195 struct tps65910_rtc *tps_rtc = dev_get_drvdata(dev);
196 int ret;
197 u32 rtc_reg;
198
199 ret = regmap_read(tps->regmap, TPS65910_RTC_STATUS, &rtc_reg);
200 if (ret)
201 return IRQ_NONE;
202
203 if (rtc_reg & TPS65910_RTC_STATUS_ALARM)
204 events = RTC_IRQF | RTC_AF;
205
206 ret = regmap_write(tps->regmap, TPS65910_RTC_STATUS, rtc_reg);
207 if (ret)
208 return IRQ_NONE;
209
210 /* Notify RTC core on event */
211 rtc_update_irq(tps_rtc->rtc, 1, events);
212
213 return IRQ_HANDLED;
214}
215
216static const struct rtc_class_ops tps65910_rtc_ops = {
217 .read_time = tps65910_rtc_read_time,
218 .set_time = tps65910_rtc_set_time,
219 .read_alarm = tps65910_rtc_read_alarm,
220 .set_alarm = tps65910_rtc_set_alarm,
221 .alarm_irq_enable = tps65910_rtc_alarm_irq_enable,
222};
223
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800224static int tps65910_rtc_probe(struct platform_device *pdev)
Venu Byravarasu0e783982012-10-04 17:13:55 -0700225{
226 struct tps65910 *tps65910 = NULL;
227 struct tps65910_rtc *tps_rtc = NULL;
Venu Byravarasu0e783982012-10-04 17:13:55 -0700228 int ret;
229 int irq;
230 u32 rtc_reg;
231
232 tps65910 = dev_get_drvdata(pdev->dev.parent);
233
234 tps_rtc = devm_kzalloc(&pdev->dev, sizeof(struct tps65910_rtc),
235 GFP_KERNEL);
236 if (!tps_rtc)
237 return -ENOMEM;
238
239 /* Clear pending interrupts */
240 ret = regmap_read(tps65910->regmap, TPS65910_RTC_STATUS, &rtc_reg);
241 if (ret < 0)
242 return ret;
243
244 ret = regmap_write(tps65910->regmap, TPS65910_RTC_STATUS, rtc_reg);
245 if (ret < 0)
246 return ret;
247
248 dev_dbg(&pdev->dev, "Enabling rtc-tps65910.\n");
Kim, Milo18c701a2012-12-17 16:02:51 -0800249
250 /* Enable RTC digital power domain */
251 ret = regmap_update_bits(tps65910->regmap, TPS65910_DEVCTRL,
252 DEVCTRL_RTC_PWDN_MASK, 0 << DEVCTRL_RTC_PWDN_SHIFT);
253 if (ret < 0)
254 return ret;
255
Venu Byravarasu0e783982012-10-04 17:13:55 -0700256 rtc_reg = TPS65910_RTC_CTRL_STOP_RTC;
257 ret = regmap_write(tps65910->regmap, TPS65910_RTC_CTRL, rtc_reg);
258 if (ret < 0)
259 return ret;
260
Thierry Reding9f7d7a12014-08-08 14:20:26 -0700261 platform_set_drvdata(pdev, tps_rtc);
262
Venu Byravarasu06f77d12012-10-04 17:14:34 -0700263 irq = platform_get_irq(pdev, 0);
Venu Byravarasu0e783982012-10-04 17:13:55 -0700264 if (irq <= 0) {
265 dev_warn(&pdev->dev, "Wake up is not possible as irq = %d\n",
266 irq);
Sachin Kamatdbda1612013-04-29 16:20:10 -0700267 return -ENXIO;
Venu Byravarasu0e783982012-10-04 17:13:55 -0700268 }
269
Venu Byravarasu0e783982012-10-04 17:13:55 -0700270 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
Laxman Dewangan225ccc22013-02-21 16:44:38 -0800271 tps65910_rtc_interrupt, IRQF_TRIGGER_LOW | IRQF_EARLY_RESUME,
Sivaram Nair32c47462012-12-17 16:02:37 -0800272 dev_name(&pdev->dev), &pdev->dev);
Venu Byravarasu0e783982012-10-04 17:13:55 -0700273 if (ret < 0) {
274 dev_err(&pdev->dev, "IRQ is not free.\n");
275 return ret;
276 }
Laxman Dewanganeb5eba42013-02-21 16:44:35 -0800277 tps_rtc->irq = irq;
278 device_set_wakeup_capable(&pdev->dev, 1);
Venu Byravarasu0e783982012-10-04 17:13:55 -0700279
Jingoo Han97868b32013-04-29 16:19:16 -0700280 tps_rtc->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
Venu Byravarasu0e783982012-10-04 17:13:55 -0700281 &tps65910_rtc_ops, THIS_MODULE);
282 if (IS_ERR(tps_rtc->rtc)) {
283 ret = PTR_ERR(tps_rtc->rtc);
284 dev_err(&pdev->dev, "RTC device register: err %d\n", ret);
285 return ret;
286 }
287
Venu Byravarasu0e783982012-10-04 17:13:55 -0700288 return 0;
289}
290
291/*
292 * Disable tps65910 RTC interrupts.
293 * Sets status flag to free.
294 */
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800295static int tps65910_rtc_remove(struct platform_device *pdev)
Venu Byravarasu0e783982012-10-04 17:13:55 -0700296{
Kim, Milo1430e172012-11-29 13:54:36 -0800297 tps65910_rtc_alarm_irq_enable(&pdev->dev, 0);
Venu Byravarasu0e783982012-10-04 17:13:55 -0700298
Venu Byravarasu0e783982012-10-04 17:13:55 -0700299 return 0;
300}
301
302#ifdef CONFIG_PM_SLEEP
Venu Byravarasu0e783982012-10-04 17:13:55 -0700303static int tps65910_rtc_suspend(struct device *dev)
304{
Laxman Dewanganeb5eba42013-02-21 16:44:35 -0800305 struct tps65910_rtc *tps_rtc = dev_get_drvdata(dev);
Venu Byravarasu0e783982012-10-04 17:13:55 -0700306
Laxman Dewanganeb5eba42013-02-21 16:44:35 -0800307 if (device_may_wakeup(dev))
308 enable_irq_wake(tps_rtc->irq);
Laxman Dewangandfaf09a2013-02-21 16:44:36 -0800309 return 0;
Venu Byravarasu0e783982012-10-04 17:13:55 -0700310}
311
312static int tps65910_rtc_resume(struct device *dev)
313{
Laxman Dewanganeb5eba42013-02-21 16:44:35 -0800314 struct tps65910_rtc *tps_rtc = dev_get_drvdata(dev);
315
316 if (device_may_wakeup(dev))
317 disable_irq_wake(tps_rtc->irq);
Laxman Dewangandfaf09a2013-02-21 16:44:36 -0800318 return 0;
Venu Byravarasu0e783982012-10-04 17:13:55 -0700319}
Laxman Dewangan176a9f22013-02-21 16:44:37 -0800320#endif
Venu Byravarasu0e783982012-10-04 17:13:55 -0700321
Jingoo Hane4ae9092013-04-29 16:20:03 -0700322static SIMPLE_DEV_PM_OPS(tps65910_rtc_pm_ops, tps65910_rtc_suspend,
323 tps65910_rtc_resume);
Venu Byravarasu0e783982012-10-04 17:13:55 -0700324
Venu Byravarasu0e783982012-10-04 17:13:55 -0700325static struct platform_driver tps65910_rtc_driver = {
326 .probe = tps65910_rtc_probe,
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800327 .remove = tps65910_rtc_remove,
Venu Byravarasu0e783982012-10-04 17:13:55 -0700328 .driver = {
Venu Byravarasu0e783982012-10-04 17:13:55 -0700329 .name = "tps65910-rtc",
Laxman Dewangan176a9f22013-02-21 16:44:37 -0800330 .pm = &tps65910_rtc_pm_ops,
Venu Byravarasu0e783982012-10-04 17:13:55 -0700331 },
332};
333
334module_platform_driver(tps65910_rtc_driver);
335MODULE_ALIAS("platform:rtc-tps65910");
336MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
337MODULE_LICENSE("GPL");