drivers/rtc/rtc-tps65910.c: enable/disable wake in suspend/resume
[cascardo/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/platform_device.h>
25 #include <linux/interrupt.h>
26 #include <linux/mfd/tps65910.h>
27
28 struct tps65910_rtc {
29         struct rtc_device       *rtc;
30         int irq;
31         /* To store the list of enabled interrupts */
32         u32 irqstat;
33 };
34
35 /* Total number of RTC registers needed to set time*/
36 #define NUM_TIME_REGS   (TPS65910_YEARS - TPS65910_SECONDS + 1)
37
38 static int tps65910_rtc_alarm_irq_enable(struct device *dev, unsigned enabled)
39 {
40         struct tps65910 *tps = dev_get_drvdata(dev->parent);
41         u8 val = 0;
42
43         if (enabled)
44                 val = TPS65910_RTC_INTERRUPTS_IT_ALARM;
45
46         return regmap_write(tps->regmap, TPS65910_RTC_INTERRUPTS, val);
47 }
48
49 /*
50  * Gets current tps65910 RTC time and date parameters.
51  *
52  * The RTC's time/alarm representation is not what gmtime(3) requires
53  * Linux to use:
54  *
55  *  - Months are 1..12 vs Linux 0-11
56  *  - Years are 0..99 vs Linux 1900..N (we assume 21st century)
57  */
58 static int tps65910_rtc_read_time(struct device *dev, struct rtc_time *tm)
59 {
60         unsigned char rtc_data[NUM_TIME_REGS];
61         struct tps65910 *tps = dev_get_drvdata(dev->parent);
62         int ret;
63
64         /* Copy RTC counting registers to static registers or latches */
65         ret = regmap_update_bits(tps->regmap, TPS65910_RTC_CTRL,
66                 TPS65910_RTC_CTRL_GET_TIME, TPS65910_RTC_CTRL_GET_TIME);
67         if (ret < 0) {
68                 dev_err(dev, "RTC CTRL reg update failed with err:%d\n", ret);
69                 return ret;
70         }
71
72         ret = regmap_bulk_read(tps->regmap, TPS65910_SECONDS, rtc_data,
73                 NUM_TIME_REGS);
74         if (ret < 0) {
75                 dev_err(dev, "reading from RTC failed with err:%d\n", ret);
76                 return ret;
77         }
78
79         tm->tm_sec = bcd2bin(rtc_data[0]);
80         tm->tm_min = bcd2bin(rtc_data[1]);
81         tm->tm_hour = bcd2bin(rtc_data[2]);
82         tm->tm_mday = bcd2bin(rtc_data[3]);
83         tm->tm_mon = bcd2bin(rtc_data[4]) - 1;
84         tm->tm_year = bcd2bin(rtc_data[5]) + 100;
85
86         return ret;
87 }
88
89 static int tps65910_rtc_set_time(struct device *dev, struct rtc_time *tm)
90 {
91         unsigned char rtc_data[NUM_TIME_REGS];
92         struct tps65910 *tps = dev_get_drvdata(dev->parent);
93         int ret;
94
95         rtc_data[0] = bin2bcd(tm->tm_sec);
96         rtc_data[1] = bin2bcd(tm->tm_min);
97         rtc_data[2] = bin2bcd(tm->tm_hour);
98         rtc_data[3] = bin2bcd(tm->tm_mday);
99         rtc_data[4] = bin2bcd(tm->tm_mon + 1);
100         rtc_data[5] = bin2bcd(tm->tm_year - 100);
101
102         /* Stop RTC while updating the RTC time registers */
103         ret = regmap_update_bits(tps->regmap, TPS65910_RTC_CTRL,
104                 TPS65910_RTC_CTRL_STOP_RTC, 0);
105         if (ret < 0) {
106                 dev_err(dev, "RTC stop failed with err:%d\n", ret);
107                 return ret;
108         }
109
110         /* update all the time registers in one shot */
111         ret = regmap_bulk_write(tps->regmap, TPS65910_SECONDS, rtc_data,
112                 NUM_TIME_REGS);
113         if (ret < 0) {
114                 dev_err(dev, "rtc_set_time error %d\n", ret);
115                 return ret;
116         }
117
118         /* Start back RTC */
119         ret = regmap_update_bits(tps->regmap, TPS65910_RTC_CTRL,
120                 TPS65910_RTC_CTRL_STOP_RTC, 1);
121         if (ret < 0)
122                 dev_err(dev, "RTC start failed with err:%d\n", ret);
123
124         return ret;
125 }
126
127 /*
128  * Gets current tps65910 RTC alarm time.
129  */
130 static int tps65910_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
131 {
132         unsigned char alarm_data[NUM_TIME_REGS];
133         u32 int_val;
134         struct tps65910 *tps = dev_get_drvdata(dev->parent);
135         int ret;
136
137         ret = regmap_bulk_read(tps->regmap, TPS65910_SECONDS, alarm_data,
138                 NUM_TIME_REGS);
139         if (ret < 0) {
140                 dev_err(dev, "rtc_read_alarm error %d\n", ret);
141                 return ret;
142         }
143
144         alm->time.tm_sec = bcd2bin(alarm_data[0]);
145         alm->time.tm_min = bcd2bin(alarm_data[1]);
146         alm->time.tm_hour = bcd2bin(alarm_data[2]);
147         alm->time.tm_mday = bcd2bin(alarm_data[3]);
148         alm->time.tm_mon = bcd2bin(alarm_data[4]) - 1;
149         alm->time.tm_year = bcd2bin(alarm_data[5]) + 100;
150
151         ret = regmap_read(tps->regmap, TPS65910_RTC_INTERRUPTS, &int_val);
152         if (ret < 0)
153                 return ret;
154
155         if (int_val & TPS65910_RTC_INTERRUPTS_IT_ALARM)
156                 alm->enabled = 1;
157
158         return ret;
159 }
160
161 static int tps65910_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
162 {
163         unsigned char alarm_data[NUM_TIME_REGS];
164         struct tps65910 *tps = dev_get_drvdata(dev->parent);
165         int ret;
166
167         ret = tps65910_rtc_alarm_irq_enable(dev, 0);
168         if (ret)
169                 return ret;
170
171         alarm_data[0] = bin2bcd(alm->time.tm_sec);
172         alarm_data[1] = bin2bcd(alm->time.tm_min);
173         alarm_data[2] = bin2bcd(alm->time.tm_hour);
174         alarm_data[3] = bin2bcd(alm->time.tm_mday);
175         alarm_data[4] = bin2bcd(alm->time.tm_mon + 1);
176         alarm_data[5] = bin2bcd(alm->time.tm_year - 100);
177
178         /* update all the alarm registers in one shot */
179         ret = regmap_bulk_write(tps->regmap, TPS65910_ALARM_SECONDS,
180                 alarm_data, NUM_TIME_REGS);
181         if (ret) {
182                 dev_err(dev, "rtc_set_alarm error %d\n", ret);
183                 return ret;
184         }
185
186         if (alm->enabled)
187                 ret = tps65910_rtc_alarm_irq_enable(dev, 1);
188
189         return ret;
190 }
191
192 static irqreturn_t tps65910_rtc_interrupt(int irq, void *rtc)
193 {
194         struct device *dev = rtc;
195         unsigned long events = 0;
196         struct tps65910 *tps = dev_get_drvdata(dev->parent);
197         struct tps65910_rtc *tps_rtc = dev_get_drvdata(dev);
198         int ret;
199         u32 rtc_reg;
200
201         ret = regmap_read(tps->regmap, TPS65910_RTC_STATUS, &rtc_reg);
202         if (ret)
203                 return IRQ_NONE;
204
205         if (rtc_reg & TPS65910_RTC_STATUS_ALARM)
206                 events = RTC_IRQF | RTC_AF;
207
208         ret = regmap_write(tps->regmap, TPS65910_RTC_STATUS, rtc_reg);
209         if (ret)
210                 return IRQ_NONE;
211
212         /* Notify RTC core on event */
213         rtc_update_irq(tps_rtc->rtc, 1, events);
214
215         return IRQ_HANDLED;
216 }
217
218 static const struct rtc_class_ops tps65910_rtc_ops = {
219         .read_time      = tps65910_rtc_read_time,
220         .set_time       = tps65910_rtc_set_time,
221         .read_alarm     = tps65910_rtc_read_alarm,
222         .set_alarm      = tps65910_rtc_set_alarm,
223         .alarm_irq_enable = tps65910_rtc_alarm_irq_enable,
224 };
225
226 static int tps65910_rtc_probe(struct platform_device *pdev)
227 {
228         struct tps65910 *tps65910 = NULL;
229         struct tps65910_rtc *tps_rtc = NULL;
230         int ret;
231         int irq;
232         u32 rtc_reg;
233
234         tps65910 = dev_get_drvdata(pdev->dev.parent);
235
236         tps_rtc = devm_kzalloc(&pdev->dev, sizeof(struct tps65910_rtc),
237                         GFP_KERNEL);
238         if (!tps_rtc)
239                 return -ENOMEM;
240
241         /* Clear pending interrupts */
242         ret = regmap_read(tps65910->regmap, TPS65910_RTC_STATUS, &rtc_reg);
243         if (ret < 0)
244                 return ret;
245
246         ret = regmap_write(tps65910->regmap, TPS65910_RTC_STATUS, rtc_reg);
247         if (ret < 0)
248                 return ret;
249
250         dev_dbg(&pdev->dev, "Enabling rtc-tps65910.\n");
251
252         /* Enable RTC digital power domain */
253         ret = regmap_update_bits(tps65910->regmap, TPS65910_DEVCTRL,
254                 DEVCTRL_RTC_PWDN_MASK, 0 << DEVCTRL_RTC_PWDN_SHIFT);
255         if (ret < 0)
256                 return ret;
257
258         rtc_reg = TPS65910_RTC_CTRL_STOP_RTC;
259         ret = regmap_write(tps65910->regmap, TPS65910_RTC_CTRL, rtc_reg);
260         if (ret < 0)
261                 return ret;
262
263         irq  = platform_get_irq(pdev, 0);
264         if (irq <= 0) {
265                 dev_warn(&pdev->dev, "Wake up is not possible as irq = %d\n",
266                         irq);
267                 return ret;
268         }
269
270         ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
271                 tps65910_rtc_interrupt, IRQF_TRIGGER_LOW,
272                 dev_name(&pdev->dev), &pdev->dev);
273         if (ret < 0) {
274                 dev_err(&pdev->dev, "IRQ is not free.\n");
275                 return ret;
276         }
277         tps_rtc->irq = irq;
278         device_set_wakeup_capable(&pdev->dev, 1);
279
280         tps_rtc->rtc = rtc_device_register(pdev->name, &pdev->dev,
281                 &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
288         platform_set_drvdata(pdev, tps_rtc);
289
290         return 0;
291 }
292
293 /*
294  * Disable tps65910 RTC interrupts.
295  * Sets status flag to free.
296  */
297 static int tps65910_rtc_remove(struct platform_device *pdev)
298 {
299         /* leave rtc running, but disable irqs */
300         struct tps65910_rtc *tps_rtc = platform_get_drvdata(pdev);
301
302         tps65910_rtc_alarm_irq_enable(&pdev->dev, 0);
303
304         rtc_device_unregister(tps_rtc->rtc);
305         return 0;
306 }
307
308 #ifdef CONFIG_PM_SLEEP
309
310 static int tps65910_rtc_suspend(struct device *dev)
311 {
312         struct tps65910 *tps = dev_get_drvdata(dev->parent);
313         struct tps65910_rtc *tps_rtc = dev_get_drvdata(dev);
314         u8 alarm = TPS65910_RTC_INTERRUPTS_IT_ALARM;
315         int ret;
316
317         if (device_may_wakeup(dev))
318                 enable_irq_wake(tps_rtc->irq);
319
320         /* Store current list of enabled interrupts*/
321         ret = regmap_read(tps->regmap, TPS65910_RTC_INTERRUPTS,
322                 &tps->rtc->irqstat);
323         if (ret < 0)
324                 return ret;
325
326         /* Enable RTC ALARM interrupt only */
327         return regmap_write(tps->regmap, TPS65910_RTC_INTERRUPTS, alarm);
328 }
329
330 static int tps65910_rtc_resume(struct device *dev)
331 {
332         struct tps65910 *tps = dev_get_drvdata(dev->parent);
333         struct tps65910_rtc *tps_rtc = dev_get_drvdata(dev);
334
335         if (device_may_wakeup(dev))
336                 disable_irq_wake(tps_rtc->irq);
337
338         /* Restore list of enabled interrupts before suspend */
339         return regmap_write(tps->regmap, TPS65910_RTC_INTERRUPTS,
340                 tps->rtc->irqstat);
341 }
342
343 static const struct dev_pm_ops tps65910_rtc_pm_ops = {
344         .suspend        = tps65910_rtc_suspend,
345         .resume         = tps65910_rtc_resume,
346 };
347
348 #define DEV_PM_OPS     (&tps65910_rtc_pm_ops)
349 #else
350 #define DEV_PM_OPS     NULL
351 #endif
352
353 static struct platform_driver tps65910_rtc_driver = {
354         .probe          = tps65910_rtc_probe,
355         .remove         = tps65910_rtc_remove,
356         .driver         = {
357                 .owner  = THIS_MODULE,
358                 .name   = "tps65910-rtc",
359                 .pm     = DEV_PM_OPS,
360         },
361 };
362
363 module_platform_driver(tps65910_rtc_driver);
364 MODULE_ALIAS("platform:rtc-tps65910");
365 MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
366 MODULE_LICENSE("GPL");