ARM: tegra30: pm: flush L1 data before exit coherency on secondary CPU
[linux-2.6.git] / drivers / rtc / rtc-tps6586x.c
1 /*
2  * drivers/rtc/rtc-tps6586x.c
3  *
4  * RTC driver for TI TPS6586x
5  *
6  * Copyright (c) 2010, NVIDIA Corporation.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  * more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21  */
22
23 #include <linux/device.h>
24 #include <linux/err.h>
25 #include <linux/init.h>
26 #include <linux/kernel.h>
27 #include <linux/mfd/tps6586x.h>
28 #include <linux/platform_device.h>
29 #include <linux/rtc.h>
30 #include <linux/slab.h>
31
32 #define RTC_CTRL        0xc0
33 #define POR_RESET_N     BIT(7)
34 #define OSC_SRC_SEL     BIT(6)
35 #define RTC_ENABLE      BIT(5)  /* enables alarm */
36 #define RTC_BUF_ENABLE  BIT(4)  /* 32 KHz buffer enable */
37 #define PRE_BYPASS      BIT(3)  /* 0=1KHz or 1=32KHz updates */
38 #define CL_SEL_MASK     (BIT(2)|BIT(1))
39 #define CL_SEL_POS      1
40 #define RTC_ALARM1_HI   0xc1
41 #define RTC_COUNT4      0xc6
42 #define RTC_COUNT4_DUMMYREAD 0xc5  /* start a PMU RTC access by reading the register prior to the RTC_COUNT4 */
43 #define ALM1_VALID_RANGE_IN_SEC 0x3FFF /*only 14-bits width in second*/
44
45 struct tps6586x_rtc {
46         unsigned long           epoch_start;
47         int                     irq;
48         struct rtc_device       *rtc;
49         bool                    irq_en;
50 };
51
52 static inline struct device *to_tps6586x_dev(struct device *dev)
53 {
54         return dev->parent;
55 }
56
57 static int tps6586x_rtc_read_time(struct device *dev, struct rtc_time *tm)
58 {
59         struct tps6586x_rtc *rtc = dev_get_drvdata(dev);
60         struct device *tps_dev = to_tps6586x_dev(dev);
61         unsigned long long ticks = 0;
62         unsigned long seconds;
63         u8 buff[6];
64         int err;
65         int i;
66
67         err = tps6586x_reads(tps_dev, RTC_COUNT4_DUMMYREAD, sizeof(buff), buff);
68         if (err < 0) {
69                 dev_err(dev, "failed to read counter\n");
70                 return err;
71         }
72
73         for (i = 1; i < sizeof(buff); i++) {
74                 ticks <<= 8;
75                 ticks |= buff[i];
76         }
77
78         seconds = ticks >> 10;
79
80         seconds += rtc->epoch_start;
81         rtc_time_to_tm(seconds, tm);
82         return rtc_valid_tm(tm);
83 }
84
85 static int tps6586x_rtc_set_time(struct device *dev, struct rtc_time *tm)
86 {
87         struct tps6586x_rtc *rtc = dev_get_drvdata(dev);
88         struct device *tps_dev = to_tps6586x_dev(dev);
89         unsigned long long ticks;
90         unsigned long seconds;
91         u8 buff[5];
92         int err;
93
94         rtc_tm_to_time(tm, &seconds);
95
96         if (WARN_ON(seconds < rtc->epoch_start)) {
97                 dev_err(dev, "requested time unsupported\n");
98                 return -EINVAL;
99         }
100
101         seconds -= rtc->epoch_start;
102
103         ticks = (unsigned long long)seconds << 10;
104         buff[0] = (ticks >> 32) & 0xff;
105         buff[1] = (ticks >> 24) & 0xff;
106         buff[2] = (ticks >> 16) & 0xff;
107         buff[3] = (ticks >> 8) & 0xff;
108         buff[4] = ticks & 0xff;
109
110         err = tps6586x_clr_bits(tps_dev, RTC_CTRL, RTC_ENABLE);
111         if (err < 0) {
112                 dev_err(dev, "failed to clear RTC_ENABLE\n");
113                 return err;
114         }
115
116         err = tps6586x_writes(tps_dev, RTC_COUNT4, sizeof(buff), buff);
117         if (err < 0) {
118                 dev_err(dev, "failed to program new time\n");
119                 return err;
120         }
121
122         err = tps6586x_set_bits(tps_dev, RTC_CTRL, RTC_ENABLE);
123         if (err < 0) {
124                 dev_err(dev, "failed to set RTC_ENABLE\n");
125                 return err;
126         }
127
128         return 0;
129 }
130
131 static int tps6586x_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
132 {
133         struct tps6586x_rtc *rtc = dev_get_drvdata(dev);
134         struct device *tps_dev = to_tps6586x_dev(dev);
135         unsigned long seconds;
136         unsigned long ticks;
137         unsigned long rtc_current_time;
138         unsigned long long rticks = 0;
139         u8 buff[3];
140         u8 rbuff[6];
141         int err;
142         int i;
143
144         if (rtc->irq == -1)
145                 return -EIO;
146
147         rtc_tm_to_time(&alrm->time, &seconds);
148
149         if (WARN_ON(alrm->enabled && (seconds < rtc->epoch_start))) {
150                 dev_err(dev, "can't set alarm to requested time\n");
151                 return -EINVAL;
152         }
153
154         if (alrm->enabled && !rtc->irq_en) {
155                 enable_irq(rtc->irq);
156                 rtc->irq_en = true;
157         } else if (!alrm->enabled && rtc->irq_en) {
158                 disable_irq(rtc->irq);
159                 rtc->irq_en = false;
160         }
161
162         seconds -= rtc->epoch_start;
163
164         err = tps6586x_reads(tps_dev, RTC_COUNT4_DUMMYREAD, sizeof(rbuff), rbuff);
165         if (err < 0) {
166                 dev_err(dev, "failed to read counter\n");
167                 return err;
168         }
169
170         for (i = 1; i < sizeof(rbuff); i++) {
171                 rticks <<= 8;
172                 rticks |= rbuff[i];
173         }
174
175         rtc_current_time = rticks >> 10;
176         if ((seconds - rtc_current_time) > ALM1_VALID_RANGE_IN_SEC)
177                 seconds = rtc_current_time - 1;
178
179         ticks = (unsigned long long)seconds << 10;
180
181         buff[0] = (ticks >> 16) & 0xff;
182         buff[1] = (ticks >> 8) & 0xff;
183         buff[2] = ticks & 0xff;
184
185         err = tps6586x_writes(tps_dev, RTC_ALARM1_HI, sizeof(buff), buff);
186         if (err)
187                 dev_err(tps_dev, "unable to program alarm\n");
188
189         return err;
190 }
191
192 static int tps6586x_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
193 {
194         struct tps6586x_rtc *rtc = dev_get_drvdata(dev);
195         struct device *tps_dev = to_tps6586x_dev(dev);
196         unsigned long ticks;
197         unsigned long seconds;
198         u8 buff[3];
199         int err;
200
201         err = tps6586x_reads(tps_dev, RTC_ALARM1_HI, sizeof(buff), buff);
202         if (err)
203                 return err;
204
205         ticks = (buff[0] << 16) | (buff[1] << 8) | buff[2];
206         seconds = ticks >> 10;
207         seconds += rtc->epoch_start;
208
209         rtc_time_to_tm(seconds, &alrm->time);
210
211         return 0;
212 }
213
214 static int tps6586x_rtc_alarm_irq_enable(struct device *dev,
215                                          unsigned int enabled)
216 {
217         struct tps6586x_rtc *rtc = dev_get_drvdata(dev);
218         struct device *tps_dev = to_tps6586x_dev(dev);
219         u8 buff;
220         int err;
221
222         if (rtc->irq == -1)
223                 return -EIO;
224
225         err = tps6586x_read(tps_dev, RTC_CTRL, &buff);
226         if (err < 0) {
227                 dev_err(dev, "failed to read RTC_CTRL\n");
228                 return err;
229         }
230
231         if ((enabled && (buff & RTC_ENABLE)) ||
232             (!enabled && !(buff & RTC_ENABLE)))
233                 return 0;
234
235         if (enabled) {
236                 err = tps6586x_set_bits(tps_dev, RTC_CTRL, RTC_ENABLE);
237                 if (err < 0) {
238                         dev_err(dev, "failed to set RTC_ENABLE\n");
239                         return err;
240                 }
241
242                 if (!rtc->irq_en) {
243                         enable_irq(rtc->irq);
244                         rtc->irq_en = true;
245                 }
246         } else {
247                 err = tps6586x_clr_bits(tps_dev, RTC_CTRL, RTC_ENABLE);
248                 if (err < 0) {
249                         dev_err(dev, "failed to clear RTC_ENABLE\n");
250                         return err;
251                 }
252
253                 if (rtc->irq_en) {
254                         disable_irq(rtc->irq);
255                         rtc->irq_en = false;
256                 }
257         }
258
259         return 0;
260 }
261
262 static const struct rtc_class_ops tps6586x_rtc_ops = {
263         .read_time      = tps6586x_rtc_read_time,
264         .set_time       = tps6586x_rtc_set_time,
265         .set_alarm      = tps6586x_rtc_set_alarm,
266         .read_alarm     = tps6586x_rtc_read_alarm,
267         .alarm_irq_enable = tps6586x_rtc_alarm_irq_enable,
268 };
269
270 static irqreturn_t tps6586x_rtc_irq(int irq, void *data)
271 {
272         struct device *dev = data;
273         struct tps6586x_rtc *rtc = dev_get_drvdata(dev);
274
275         rtc_update_irq(rtc->rtc, 1, RTC_IRQF | RTC_AF);
276         return IRQ_HANDLED;
277 }
278
279 static int __devinit tps6586x_rtc_probe(struct platform_device *pdev)
280 {
281         struct tps6586x_rtc_platform_data *pdata = pdev->dev.platform_data;
282         struct device *tps_dev = to_tps6586x_dev(&pdev->dev);
283         struct tps6586x_rtc *rtc;
284         int err;
285         struct tps6586x_epoch_start *epoch;
286
287         if (!pdata) {
288                 dev_err(&pdev->dev, "no platform_data specified\n");
289                 return -EINVAL;
290         }
291
292         rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
293
294         if (!rtc)
295                 return -ENOMEM;
296
297         rtc->irq = -1;
298
299         if (pdata->irq < 0)
300                 dev_warn(&pdev->dev, "no IRQ specified, wakeup is disabled\n");
301
302         epoch = &pdata->start;
303         rtc->epoch_start = mktime(epoch->year, epoch->month, epoch->day,
304                                   epoch->hour, epoch->min, epoch->sec);
305
306         dev_set_drvdata(&pdev->dev, rtc);
307
308         device_init_wakeup(&pdev->dev, 1);
309
310         rtc->rtc = rtc_device_register("tps6586x-rtc", &pdev->dev,
311                                        &tps6586x_rtc_ops, THIS_MODULE);
312
313         if (IS_ERR(rtc->rtc)) {
314                 err = PTR_ERR(rtc->rtc);
315                 goto fail;
316         }
317
318         /* 1 kHz tick mode, enable tick counting */
319         err = tps6586x_update(tps_dev, RTC_CTRL,
320                 RTC_ENABLE | OSC_SRC_SEL | ((pdata->cl_sel << CL_SEL_POS) &
321                                             CL_SEL_MASK),
322                 RTC_ENABLE | OSC_SRC_SEL | PRE_BYPASS | CL_SEL_MASK);
323         if (err < 0) {
324                 dev_err(&pdev->dev, "unable to start counter\n");
325                 goto fail;
326         }
327
328         if (pdata && (pdata->irq >= 0)) {
329                 rtc->irq = pdata->irq;
330                 err = request_threaded_irq(pdata->irq, NULL, tps6586x_rtc_irq,
331                                            IRQF_ONESHOT, "tps6586x-rtc",
332                                            &pdev->dev);
333                 if (err) {
334                         dev_warn(&pdev->dev, "unable to request IRQ(%d)\n", rtc->irq);
335                         rtc->irq = -1;
336                 } else {
337                         enable_irq_wake(rtc->irq);
338                         disable_irq(rtc->irq);
339                 }
340         }
341
342         return 0;
343
344 fail:
345         if (!IS_ERR_OR_NULL(rtc->rtc))
346                 rtc_device_unregister(rtc->rtc);
347         device_init_wakeup(&pdev->dev, 0);
348         kfree(rtc);
349         return err;
350 }
351
352 static int __devexit tps6586x_rtc_remove(struct platform_device *pdev)
353 {
354         struct tps6586x_rtc *rtc = dev_get_drvdata(&pdev->dev);
355
356         if (rtc->irq != -1)
357                 free_irq(rtc->irq, rtc);
358         rtc_device_unregister(rtc->rtc);
359         kfree(rtc);
360         return 0;
361 }
362
363 static struct platform_driver tps6586x_rtc_driver = {
364         .driver = {
365                 .name   = "tps6586x-rtc",
366                 .owner  = THIS_MODULE,
367         },
368         .probe  = tps6586x_rtc_probe,
369         .remove = __devexit_p(tps6586x_rtc_remove),
370 };
371
372 static int __init tps6586x_rtc_init(void)
373 {
374         return platform_driver_register(&tps6586x_rtc_driver);
375 }
376 module_init(tps6586x_rtc_init);
377
378 static void __exit tps6586x_rtc_exit(void)
379 {
380         platform_driver_unregister(&tps6586x_rtc_driver);
381 }
382 module_exit(tps6586x_rtc_exit);
383
384 MODULE_DESCRIPTION("TI TPS6586x RTC driver");
385 MODULE_AUTHOR("NVIDIA Corporation");
386 MODULE_LICENSE("GPL");
387 MODULE_ALIAS("platform:rtc-tps6586x");