b2b9d04171c584b68ce8f06c61a284b4a51063bc
[linux-2.6.git] / drivers / rtc / rtc-tps80031.c
1 /*
2  * drivers/rtc/rtc_tps80031.c
3  *
4  * RTC driver for TI TPS80031
5  *
6  * Copyright (c) 2011, 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 /* #define DEBUG                1 */
24 /* #define VERBOSE_DEBUG        1 */
25
26 #include <linux/device.h>
27 #include <linux/err.h>
28 #include <linux/init.h>
29 #include <linux/kernel.h>
30 #include <linux/mfd/tps80031.h>
31 #include <linux/platform_device.h>
32 #include <linux/rtc.h>
33 #include <linux/slab.h>
34 #include <linux/gpio.h>
35
36 #define RTC_CTRL                0x10
37 #define RTC_STATUS              0x11
38 #define RTC_SECONDS_REG         0x0
39 #define RTC_ALARM               0x8
40 #define RTC_INT                 0x12
41 #define RTC_RESET_STATUS        0x16
42
43 #define ENABLE_ALARM_INT 0x8
44 #define ALARM_INT_STATUS 0x40
45 #define STOP_RTC 1
46
47 /* Power on reset Values of RTC registers */
48 #define RTC_POR_YEAR 0
49 #define RTC_POR_MONTH 1
50 #define RTC_POR_DAY 1
51
52 /*
53 Linux RTC driver refers 19th centaury as base year in many
54 calculations. (e.g. refer drivers/rtc/rtc-lib.c)
55 */
56 #define OS_REF_YEAR 1900
57
58 /*
59         PMU RTC have only 2 nibbles to store year information, so using an
60         offset of 100 to set the base year as 2000 for our driver.
61 */
62 #define RTC_YEAR_OFFSET 100
63
64 struct tps80031_rtc {
65         unsigned long           epoch_start;
66         int                     irq;
67         struct rtc_device       *rtc;
68         u8                      alarm_irq_enabled;
69         int                     msecure_gpio;
70 };
71
72 static inline void tps80031_enable_rtc_write(struct device *dev)
73 {
74         struct tps80031_rtc *rtc = dev_get_drvdata(dev);
75
76         if (rtc->msecure_gpio >= 0)
77                 gpio_set_value(rtc->msecure_gpio, 1);
78 }
79
80 static inline void tps80031_disable_rtc_write(struct device *dev)
81 {
82         struct tps80031_rtc *rtc = dev_get_drvdata(dev);
83
84         if (rtc->msecure_gpio >= 0)
85                 gpio_set_value(rtc->msecure_gpio, 0);
86 }
87
88 static int tps80031_read_regs(struct device *dev, int reg, int len,
89         uint8_t *val)
90 {
91         int ret;
92
93         /* dummy read of STATUS_REG as per data sheet */
94         ret = tps80031_reads(dev->parent, 1, RTC_STATUS, 1, val);
95         if (ret < 0) {
96                 dev_err(dev->parent, "failed reading RTC_STATUS\n");
97                 WARN_ON(1);
98                 return ret;
99         }
100
101         ret = tps80031_reads(dev->parent, 1, reg, len, val);
102         if (ret < 0) {
103                 dev_err(dev->parent, "failed reading from reg %d\n", reg);
104                 WARN_ON(1);
105                 return ret;
106         }
107         return 0;
108 }
109
110 static int tps80031_write_regs(struct device *dev, int reg, int len,
111         uint8_t *val)
112 {
113         int ret;
114
115         tps80031_enable_rtc_write(dev);
116         ret = tps80031_writes(dev->parent, 1, reg, len, val);
117         if (ret < 0) {
118                 tps80031_disable_rtc_write(dev);
119                 dev_err(dev->parent, "failed writing reg: %d\n", reg);
120                 WARN_ON(1);
121                 return ret;
122         }
123         tps80031_disable_rtc_write(dev);
124         return 0;
125 }
126
127 static u8 dec2bcd(u8 dec)
128 {
129         return ((dec/10)<<4)+(dec%10);
130 }
131
132 static u8 bcd2dec(u8 bcd)
133 {
134         return (bcd >> 4)*10+(bcd & 0xF);
135 }
136
137 static void convert_bcd_to_decimal(u8 *buf, u8 len)
138 {
139         int i = 0;
140         for (i = 0; i < len; i++)
141                 buf[i] = bcd2dec(buf[i]);
142 }
143
144 static void convert_decimal_to_bcd(u8 *buf, u8 len)
145 {
146         int i = 0;
147         for (i = 0; i < len; i++)
148                 buf[i] = dec2bcd(buf[i]);
149 }
150
151 static int tps80031_rtc_read_time(struct device *dev, struct rtc_time *tm)
152 {
153         u8 buff[7];
154         int err;
155         err = tps80031_read_regs(dev, RTC_SECONDS_REG, sizeof(buff), buff);
156         if (err < 0) {
157                 dev_err(dev->parent, "failed reading time\n");
158                 return err;
159         }
160         convert_bcd_to_decimal(buff, sizeof(buff));
161         tm->tm_sec = buff[0];
162         tm->tm_min = buff[1];
163         tm->tm_hour = buff[2];
164         tm->tm_mday = buff[3];
165         tm->tm_mon = buff[4] - 1;
166         tm->tm_year = buff[5] + RTC_YEAR_OFFSET;
167         tm->tm_wday = buff[6];
168         return 0;
169 }
170
171 static int tps80031_rtc_stop(struct device *dev)
172 {
173         int err;
174
175         tps80031_enable_rtc_write(dev);
176         err = tps80031_clr_bits(dev->parent, 1, RTC_CTRL, STOP_RTC);
177         if (err < 0)
178                 dev_err(dev->parent, "failed to stop RTC. err: %d\n", err);
179         tps80031_disable_rtc_write(dev);
180         return err;
181 }
182
183 static int tps80031_rtc_start(struct device *dev)
184 {
185         int err;
186
187         tps80031_enable_rtc_write(dev);
188         err = tps80031_set_bits(dev->parent, 1, RTC_CTRL, STOP_RTC);
189         if (err < 0)
190                 dev_err(dev->parent, "failed to start RTC. err: %d\n", err);
191         tps80031_disable_rtc_write(dev);
192         return err;
193 }
194
195
196 static int tps80031_rtc_set_time(struct device *dev, struct rtc_time *tm)
197 {
198         u8 buff[7];
199         int err;
200
201         buff[0] = tm->tm_sec;
202         buff[1] = tm->tm_min;
203         buff[2] = tm->tm_hour;
204         buff[3] = tm->tm_mday;
205         buff[4] = tm->tm_mon + 1;
206         buff[5] = tm->tm_year % RTC_YEAR_OFFSET;
207         buff[6] = tm->tm_wday;
208
209         convert_decimal_to_bcd(buff, sizeof(buff));
210         err = tps80031_rtc_stop(dev);
211         if (err < 0)
212                 return err;
213
214         err = tps80031_write_regs(dev, RTC_SECONDS_REG, sizeof(buff), buff);
215         if (err < 0) {
216                 dev_err(dev->parent, "failed to program new time\n");
217                 return err;
218         }
219
220         err = tps80031_rtc_start(dev);
221         return err;
222 }
223
224 static int tps80031_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
225 {
226         struct tps80031_rtc *rtc = dev_get_drvdata(dev);
227         unsigned long seconds;
228         u8 buff[6];
229         int err;
230         struct rtc_time tm;
231
232         if (rtc->irq == -1)
233                 return -EIO;
234
235         rtc_tm_to_time(&alrm->time, &seconds);
236         tps80031_rtc_read_time(dev, &tm);
237         rtc_tm_to_time(&tm, &rtc->epoch_start);
238
239         if (WARN_ON(alrm->enabled && (seconds < rtc->epoch_start))) {
240                 dev_err(dev->parent, "can't set alarm to requested time\n");
241                 return -EINVAL;
242         }
243
244         buff[0] = alrm->time.tm_sec;
245         buff[1] = alrm->time.tm_min;
246         buff[2] = alrm->time.tm_hour;
247         buff[3] = alrm->time.tm_mday;
248         buff[4] = alrm->time.tm_mon + 1;
249         buff[5] = alrm->time.tm_year % RTC_YEAR_OFFSET;
250         convert_decimal_to_bcd(buff, sizeof(buff));
251         err = tps80031_write_regs(dev, RTC_ALARM, sizeof(buff), buff);
252         if (err)
253                 dev_err(dev->parent, "unable to program alarm\n");
254
255         return err;
256 }
257
258 static int tps80031_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
259 {
260         u8 buff[6];
261         int err;
262
263         err = tps80031_read_regs(dev, RTC_ALARM, sizeof(buff), buff);
264         if (err)
265                 return err;
266         convert_bcd_to_decimal(buff, sizeof(buff));
267
268         alrm->time.tm_sec = buff[0];
269         alrm->time.tm_min = buff[1];
270         alrm->time.tm_hour = buff[2];
271         alrm->time.tm_mday = buff[3];
272         alrm->time.tm_mon = buff[4] - 1;
273         alrm->time.tm_year = buff[5] + RTC_YEAR_OFFSET;
274
275         return 0;
276 }
277
278 static int tps80031_rtc_alarm_irq_enable(struct device *dev,
279                                          unsigned int enable)
280 {
281         struct tps80031_rtc *rtc = dev_get_drvdata(dev);
282         int err;
283         struct device *p = dev->parent;
284
285         if (rtc->irq == -1)
286                 return -EIO;
287
288         if (enable) {
289                 if (rtc->alarm_irq_enabled)
290                         return 0;
291
292                 tps80031_enable_rtc_write(dev);
293                 err = tps80031_set_bits(p, 1, RTC_INT, ENABLE_ALARM_INT);
294                 tps80031_disable_rtc_write(dev);
295                 if (err < 0) {
296                         dev_err(p, "failed to set ALRM int. err: %d\n", err);
297                         return err;
298                 } else
299                         rtc->alarm_irq_enabled = 1;
300         } else {
301                 if(!rtc->alarm_irq_enabled)
302                         return 0;
303                 tps80031_enable_rtc_write(dev);
304                 err = tps80031_clr_bits(p, 1, RTC_INT, ENABLE_ALARM_INT);
305                 tps80031_disable_rtc_write(dev);
306                 if (err < 0) {
307                         dev_err(p, "failed to clear ALRM int. err: %d\n", err);
308                         return err;
309                 } else
310                         rtc->alarm_irq_enabled = 0;
311         }
312         return 0;
313 }
314
315 static const struct rtc_class_ops tps80031_rtc_ops = {
316         .read_time = tps80031_rtc_read_time,
317         .set_time = tps80031_rtc_set_time,
318         .set_alarm = tps80031_rtc_set_alarm,
319         .read_alarm = tps80031_rtc_read_alarm,
320         .alarm_irq_enable = tps80031_rtc_alarm_irq_enable,
321 };
322
323 static irqreturn_t tps80031_rtc_irq(int irq, void *data)
324 {
325         struct device *dev = data;
326         struct tps80031_rtc *rtc = dev_get_drvdata(dev);
327         u8 reg;
328         int err;
329
330         /* clear Alarm status bits.*/
331         err = tps80031_read_regs(dev, RTC_STATUS, 1, &reg);
332         if (err) {
333                 dev_err(dev->parent, "unable to read RTC_STATUS reg\n");
334                 return -EBUSY;
335         }
336
337         tps80031_enable_rtc_write(dev);
338         err = tps80031_force_update(dev->parent, 1, RTC_STATUS,
339                 ALARM_INT_STATUS, ALARM_INT_STATUS);
340         tps80031_disable_rtc_write(dev);
341         if (err) {
342                 dev_err(dev->parent, "unable to set Alarm INT\n");
343                 return -EBUSY;
344         }
345
346         rtc_update_irq(rtc->rtc, 1, RTC_IRQF | RTC_AF);
347         return IRQ_HANDLED;
348 }
349
350 static int __devinit tps80031_rtc_probe(struct platform_device *pdev)
351 {
352         struct tps80031_rtc_platform_data *pdata = pdev->dev.platform_data;
353         struct tps80031_rtc *rtc;
354         struct rtc_time tm;
355         int err;
356         u8 reg;
357
358         if (!pdata) {
359                 dev_err(&pdev->dev, "no platform_data specified\n");
360                 return -EINVAL;
361         }
362
363         rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
364         if (!rtc)
365                 return -ENOMEM;
366
367         rtc->irq = -1;
368         if (pdata->irq < 0)
369                 dev_err(&pdev->dev, "no IRQ specified, wakeup is disabled\n");
370
371         rtc->msecure_gpio = -1;
372         if (gpio_is_valid(pdata->msecure_gpio)) {
373                 err = gpio_request(pdata->msecure_gpio, "tps80031 msecure");
374                 if (err == 0) {
375                         rtc->msecure_gpio = pdata->msecure_gpio;
376                         gpio_direction_output(rtc->msecure_gpio, 0);
377                 } else
378                         dev_warn(&pdev->dev, "could not get msecure GPIO\n");
379         }
380
381         rtc->rtc = rtc_device_register(pdev->name, &pdev->dev,
382                                        &tps80031_rtc_ops, THIS_MODULE);
383         dev_set_drvdata(&pdev->dev, rtc);
384
385         if (IS_ERR(rtc->rtc)) {
386                 err = PTR_ERR(rtc->rtc);
387                 goto fail;
388         }
389
390         if ((int)pdev && (int)&pdev->dev)
391                 err = tps80031_read_regs(&pdev->dev, RTC_STATUS, 1, &reg);
392         else {
393                 dev_err(&pdev->dev, "%s Input params incorrect\n", __func__);
394                 err = -EBUSY;
395                 goto fail;
396         }
397
398         if (err) {
399                 dev_err(&pdev->dev, "%s unable to read status\n", __func__);
400                 err = -EBUSY;
401                 goto fail;
402         }
403
404         /* If RTC have POR values, set time using platform data*/
405         tps80031_rtc_read_time(&pdev->dev, &tm);
406         if ((tm.tm_year == RTC_YEAR_OFFSET + RTC_POR_YEAR) &&
407                 (tm.tm_mon == (RTC_POR_MONTH - 1)) &&
408                 (tm.tm_mday == RTC_POR_DAY)) {
409                 if (pdata->time.tm_year < 2000 ||
410                         pdata->time.tm_year > 2100) {
411                         dev_err(&pdev->dev, "Invalid platform data\n");
412                         memset(&pdata->time, 0, sizeof(pdata->time));
413                         pdata->time.tm_year = 2011;
414                         pdata->time.tm_mday = 1;
415                 }
416                 tps80031_rtc_set_time(&pdev->dev, &pdata->time);
417         }
418
419         reg = ALARM_INT_STATUS;
420         err = tps80031_write_regs(&pdev->dev, RTC_STATUS, 1, &reg);
421         if (err) {
422                 dev_err(&pdev->dev, "unable to program RTC_STATUS reg\n");
423                 return -EBUSY;
424         }
425
426         tps80031_enable_rtc_write(&pdev->dev);
427         err = tps80031_set_bits(pdev->dev.parent, 1, RTC_INT, ENABLE_ALARM_INT);
428         tps80031_disable_rtc_write(&pdev->dev);
429         if (err) {
430                 dev_err(&pdev->dev, "unable to program Interrupt Mask reg\n");
431                 err = -EBUSY;
432                 rtc->alarm_irq_enabled = 0;
433                 goto fail;
434         } else
435                 rtc->alarm_irq_enabled = 1;
436
437         if (pdata && (pdata->irq >= 0)) {
438                 rtc->irq = pdata->irq;
439                 err = request_threaded_irq(pdata->irq, NULL, tps80031_rtc_irq,
440                                         IRQF_ONESHOT, "rtc_tps80031",
441                                         &pdev->dev);
442                 if (err) {
443                         dev_err(&pdev->dev, "request IRQ:%d fail\n", rtc->irq);
444                         rtc->irq = -1;
445                 } else {
446                         device_init_wakeup(&pdev->dev, 1);
447                         enable_irq_wake(rtc->irq);
448                 }
449         }
450         return 0;
451
452 fail:
453         if (!IS_ERR_OR_NULL(rtc->rtc))
454                 rtc_device_unregister(rtc->rtc);
455         kfree(rtc);
456         return err;
457 }
458
459 static int __devexit tps80031_rtc_remove(struct platform_device *pdev)
460 {
461         struct tps80031_rtc *rtc = dev_get_drvdata(&pdev->dev);
462
463         if (rtc->irq != -1)
464                 free_irq(rtc->irq, rtc);
465         rtc_device_unregister(rtc->rtc);
466         kfree(rtc);
467         return 0;
468 }
469
470 static struct platform_driver tps80031_rtc_driver = {
471         .driver = {
472                 .name   = "rtc_tps80031",
473                 .owner  = THIS_MODULE,
474         },
475         .probe  = tps80031_rtc_probe,
476         .remove = __devexit_p(tps80031_rtc_remove),
477 };
478
479 static int __init tps80031_rtc_init(void)
480 {
481         return platform_driver_register(&tps80031_rtc_driver);
482 }
483 module_init(tps80031_rtc_init);
484
485 static void __exit tps80031_rtc_exit(void)
486 {
487         platform_driver_unregister(&tps80031_rtc_driver);
488 }
489 module_exit(tps80031_rtc_exit);
490
491 MODULE_DESCRIPTION("TI TPS80031 RTC driver");
492 MODULE_AUTHOR("NVIDIA Corporation");
493 MODULE_LICENSE("GPL");
494 MODULE_ALIAS("platform:rtc_tps80031");