remove <linux/i2c/twl4030-pwrirq.h>
[pandora-kernel.git] / drivers / rtc / rtc-twl4030.c
1 /*
2  * rtc-twl4030.c -- TWL4030 Real Time Clock interface
3  *
4  * Copyright (C) 2007 MontaVista Software, Inc
5  * Author: Alexandre Rusev <source@mvista.com>
6  *
7  * Based on original TI driver twl4030-rtc.c
8  *   Copyright (C) 2006 Texas Instruments, Inc.
9  *
10  * Based on rtc-omap.c
11  *   Copyright (C) 2003 MontaVista Software, Inc.
12  *   Author: George G. Davis <gdavis@mvista.com> or <source@mvista.com>
13  *   Copyright (C) 2006 David Brownell
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version
18  * 2 of the License, or (at your option) any later version.
19  */
20
21 #include <linux/kernel.h>
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/types.h>
25 #include <linux/rtc.h>
26 #include <linux/bcd.h>
27 #include <linux/platform_device.h>
28 #include <linux/interrupt.h>
29
30 #include <linux/i2c/twl4030.h>
31
32
33 /*
34  * RTC block register offsets (use TWL_MODULE_RTC)
35  */
36 #define REG_SECONDS_REG                          0x00
37 #define REG_MINUTES_REG                          0x01
38 #define REG_HOURS_REG                            0x02
39 #define REG_DAYS_REG                             0x03
40 #define REG_MONTHS_REG                           0x04
41 #define REG_YEARS_REG                            0x05
42 #define REG_WEEKS_REG                            0x06
43
44 #define REG_ALARM_SECONDS_REG                    0x07
45 #define REG_ALARM_MINUTES_REG                    0x08
46 #define REG_ALARM_HOURS_REG                      0x09
47 #define REG_ALARM_DAYS_REG                       0x0A
48 #define REG_ALARM_MONTHS_REG                     0x0B
49 #define REG_ALARM_YEARS_REG                      0x0C
50
51 #define REG_RTC_CTRL_REG                         0x0D
52 #define REG_RTC_STATUS_REG                       0x0E
53 #define REG_RTC_INTERRUPTS_REG                   0x0F
54
55 #define REG_RTC_COMP_LSB_REG                     0x10
56 #define REG_RTC_COMP_MSB_REG                     0x11
57
58 /* RTC_CTRL_REG bitfields */
59 #define BIT_RTC_CTRL_REG_STOP_RTC_M              0x01
60 #define BIT_RTC_CTRL_REG_ROUND_30S_M             0x02
61 #define BIT_RTC_CTRL_REG_AUTO_COMP_M             0x04
62 #define BIT_RTC_CTRL_REG_MODE_12_24_M            0x08
63 #define BIT_RTC_CTRL_REG_TEST_MODE_M             0x10
64 #define BIT_RTC_CTRL_REG_SET_32_COUNTER_M        0x20
65 #define BIT_RTC_CTRL_REG_GET_TIME_M              0x40
66
67 /* RTC_STATUS_REG bitfields */
68 #define BIT_RTC_STATUS_REG_RUN_M                 0x02
69 #define BIT_RTC_STATUS_REG_1S_EVENT_M            0x04
70 #define BIT_RTC_STATUS_REG_1M_EVENT_M            0x08
71 #define BIT_RTC_STATUS_REG_1H_EVENT_M            0x10
72 #define BIT_RTC_STATUS_REG_1D_EVENT_M            0x20
73 #define BIT_RTC_STATUS_REG_ALARM_M               0x40
74 #define BIT_RTC_STATUS_REG_POWER_UP_M            0x80
75
76 /* RTC_INTERRUPTS_REG bitfields */
77 #define BIT_RTC_INTERRUPTS_REG_EVERY_M           0x03
78 #define BIT_RTC_INTERRUPTS_REG_IT_TIMER_M        0x04
79 #define BIT_RTC_INTERRUPTS_REG_IT_ALARM_M        0x08
80
81
82 /* REG_SECONDS_REG through REG_YEARS_REG is how many registers? */
83 #define ALL_TIME_REGS           6
84
85 /*----------------------------------------------------------------------*/
86
87 /*
88  * Supports 1 byte read from TWL4030 RTC register.
89  */
90 static int twl4030_rtc_read_u8(u8 *data, u8 reg)
91 {
92         int ret;
93
94         ret = twl4030_i2c_read_u8(TWL4030_MODULE_RTC, data, reg);
95         if (ret < 0)
96                 pr_err("twl4030_rtc: Could not read TWL4030"
97                        "register %X - error %d\n", reg, ret);
98         return ret;
99 }
100
101 /*
102  * Supports 1 byte write to TWL4030 RTC registers.
103  */
104 static int twl4030_rtc_write_u8(u8 data, u8 reg)
105 {
106         int ret;
107
108         ret = twl4030_i2c_write_u8(TWL4030_MODULE_RTC, data, reg);
109         if (ret < 0)
110                 pr_err("twl4030_rtc: Could not write TWL4030"
111                        "register %X - error %d\n", reg, ret);
112         return ret;
113 }
114
115 /*
116  * Cache the value for timer/alarm interrupts register; this is
117  * only changed by callers holding rtc ops lock (or resume).
118  */
119 static unsigned char rtc_irq_bits;
120
121 /*
122  * Enable timer and/or alarm interrupts.
123  */
124 static int set_rtc_irq_bit(unsigned char bit)
125 {
126         unsigned char val;
127         int ret;
128
129         val = rtc_irq_bits | bit;
130         ret = twl4030_rtc_write_u8(val, REG_RTC_INTERRUPTS_REG);
131         if (ret == 0)
132                 rtc_irq_bits = val;
133
134         return ret;
135 }
136
137 /*
138  * Disable timer and/or alarm interrupts.
139  */
140 static int mask_rtc_irq_bit(unsigned char bit)
141 {
142         unsigned char val;
143         int ret;
144
145         val = rtc_irq_bits & ~bit;
146         ret = twl4030_rtc_write_u8(val, REG_RTC_INTERRUPTS_REG);
147         if (ret == 0)
148                 rtc_irq_bits = val;
149
150         return ret;
151 }
152
153 static inline int twl4030_rtc_alarm_irq_set_state(int enabled)
154 {
155         int ret;
156
157         if (enabled)
158                 ret = set_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_ALARM_M);
159         else
160                 ret = mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_ALARM_M);
161
162         return ret;
163 }
164
165 static inline int twl4030_rtc_irq_set_state(int enabled)
166 {
167         int ret;
168
169         if (enabled)
170                 ret = set_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M);
171         else
172                 ret = mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M);
173
174         return ret;
175 }
176
177 /*
178  * Gets current TWL4030 RTC time and date parameters.
179  *
180  * The RTC's time/alarm representation is not what gmtime(3) requires
181  * Linux to use:
182  *
183  *  - Months are 1..12 vs Linux 0-11
184  *  - Years are 0..99 vs Linux 1900..N (we assume 21st century)
185  */
186 static int twl4030_rtc_read_time(struct device *dev, struct rtc_time *tm)
187 {
188         unsigned char rtc_data[ALL_TIME_REGS + 1];
189         int ret;
190         u8 save_control;
191
192         ret = twl4030_rtc_read_u8(&save_control, REG_RTC_CTRL_REG);
193         if (ret < 0)
194                 return ret;
195
196         save_control |= BIT_RTC_CTRL_REG_GET_TIME_M;
197
198         ret = twl4030_rtc_write_u8(save_control, REG_RTC_CTRL_REG);
199         if (ret < 0)
200                 return ret;
201
202         ret = twl4030_i2c_read(TWL4030_MODULE_RTC, rtc_data,
203                                REG_SECONDS_REG, ALL_TIME_REGS);
204
205         if (ret < 0) {
206                 dev_err(dev, "rtc_read_time error %d\n", ret);
207                 return ret;
208         }
209
210         tm->tm_sec = bcd2bin(rtc_data[0]);
211         tm->tm_min = bcd2bin(rtc_data[1]);
212         tm->tm_hour = bcd2bin(rtc_data[2]);
213         tm->tm_mday = bcd2bin(rtc_data[3]);
214         tm->tm_mon = bcd2bin(rtc_data[4]) - 1;
215         tm->tm_year = bcd2bin(rtc_data[5]) + 100;
216
217         return ret;
218 }
219
220 static int twl4030_rtc_set_time(struct device *dev, struct rtc_time *tm)
221 {
222         unsigned char save_control;
223         unsigned char rtc_data[ALL_TIME_REGS + 1];
224         int ret;
225
226         rtc_data[1] = bin2bcd(tm->tm_sec);
227         rtc_data[2] = bin2bcd(tm->tm_min);
228         rtc_data[3] = bin2bcd(tm->tm_hour);
229         rtc_data[4] = bin2bcd(tm->tm_mday);
230         rtc_data[5] = bin2bcd(tm->tm_mon + 1);
231         rtc_data[6] = bin2bcd(tm->tm_year - 100);
232
233         /* Stop RTC while updating the TC registers */
234         ret = twl4030_rtc_read_u8(&save_control, REG_RTC_CTRL_REG);
235         if (ret < 0)
236                 goto out;
237
238         save_control &= ~BIT_RTC_CTRL_REG_STOP_RTC_M;
239         twl4030_rtc_write_u8(save_control, REG_RTC_CTRL_REG);
240         if (ret < 0)
241                 goto out;
242
243         /* update all the time registers in one shot */
244         ret = twl4030_i2c_write(TWL4030_MODULE_RTC, rtc_data,
245                         REG_SECONDS_REG, ALL_TIME_REGS);
246         if (ret < 0) {
247                 dev_err(dev, "rtc_set_time error %d\n", ret);
248                 goto out;
249         }
250
251         /* Start back RTC */
252         save_control |= BIT_RTC_CTRL_REG_STOP_RTC_M;
253         ret = twl4030_rtc_write_u8(save_control, REG_RTC_CTRL_REG);
254
255 out:
256         return ret;
257 }
258
259 /*
260  * Gets current TWL4030 RTC alarm time.
261  */
262 static int twl4030_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
263 {
264         unsigned char rtc_data[ALL_TIME_REGS + 1];
265         int ret;
266
267         ret = twl4030_i2c_read(TWL4030_MODULE_RTC, rtc_data,
268                                REG_ALARM_SECONDS_REG, ALL_TIME_REGS);
269         if (ret < 0) {
270                 dev_err(dev, "rtc_read_alarm error %d\n", ret);
271                 return ret;
272         }
273
274         /* some of these fields may be wildcard/"match all" */
275         alm->time.tm_sec = bcd2bin(rtc_data[0]);
276         alm->time.tm_min = bcd2bin(rtc_data[1]);
277         alm->time.tm_hour = bcd2bin(rtc_data[2]);
278         alm->time.tm_mday = bcd2bin(rtc_data[3]);
279         alm->time.tm_mon = bcd2bin(rtc_data[4]) - 1;
280         alm->time.tm_year = bcd2bin(rtc_data[5]) + 100;
281
282         /* report cached alarm enable state */
283         if (rtc_irq_bits & BIT_RTC_INTERRUPTS_REG_IT_ALARM_M)
284                 alm->enabled = 1;
285
286         return ret;
287 }
288
289 static int twl4030_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
290 {
291         unsigned char alarm_data[ALL_TIME_REGS + 1];
292         int ret;
293
294         ret = twl4030_rtc_alarm_irq_set_state(0);
295         if (ret)
296                 goto out;
297
298         alarm_data[1] = bin2bcd(alm->time.tm_sec);
299         alarm_data[2] = bin2bcd(alm->time.tm_min);
300         alarm_data[3] = bin2bcd(alm->time.tm_hour);
301         alarm_data[4] = bin2bcd(alm->time.tm_mday);
302         alarm_data[5] = bin2bcd(alm->time.tm_mon + 1);
303         alarm_data[6] = bin2bcd(alm->time.tm_year - 100);
304
305         /* update all the alarm registers in one shot */
306         ret = twl4030_i2c_write(TWL4030_MODULE_RTC, alarm_data,
307                         REG_ALARM_SECONDS_REG, ALL_TIME_REGS);
308         if (ret) {
309                 dev_err(dev, "rtc_set_alarm error %d\n", ret);
310                 goto out;
311         }
312
313         if (alm->enabled)
314                 ret = twl4030_rtc_alarm_irq_set_state(1);
315 out:
316         return ret;
317 }
318
319 #ifdef  CONFIG_RTC_INTF_DEV
320
321 static int twl4030_rtc_ioctl(struct device *dev, unsigned int cmd,
322                              unsigned long arg)
323 {
324         switch (cmd) {
325         case RTC_AIE_OFF:
326                 return twl4030_rtc_alarm_irq_set_state(0);
327         case RTC_AIE_ON:
328                 return twl4030_rtc_alarm_irq_set_state(1);
329         case RTC_UIE_OFF:
330                 return twl4030_rtc_irq_set_state(0);
331         case RTC_UIE_ON:
332                 return twl4030_rtc_irq_set_state(1);
333
334         default:
335                 return -ENOIOCTLCMD;
336         }
337 }
338
339 #else
340 #define omap_rtc_ioctl  NULL
341 #endif
342
343 static irqreturn_t twl4030_rtc_interrupt(int irq, void *rtc)
344 {
345         unsigned long events = 0;
346         int ret = IRQ_NONE;
347         int res;
348         u8 rd_reg;
349
350         res = twl4030_rtc_read_u8(&rd_reg, REG_RTC_STATUS_REG);
351         if (res)
352                 goto out;
353         /*
354          * Figure out source of interrupt: ALARM or TIMER in RTC_STATUS_REG.
355          * only one (ALARM or RTC) interrupt source may be enabled
356          * at time, we also could check our results
357          * by reading RTS_INTERRUPTS_REGISTER[IT_TIMER,IT_ALARM]
358          */
359         if (rd_reg & BIT_RTC_STATUS_REG_ALARM_M)
360                 events |= RTC_IRQF | RTC_AF;
361         else
362                 events |= RTC_IRQF | RTC_UF;
363
364         res = twl4030_rtc_write_u8(rd_reg | BIT_RTC_STATUS_REG_ALARM_M,
365                                    REG_RTC_STATUS_REG);
366         if (res)
367                 goto out;
368
369         /* Clear on Read enabled. RTC_IT bit of TWL4030_INT_PWR_ISR1
370          * needs 2 reads to clear the interrupt. One read is done in
371          * do_twl4030_pwrirq(). Doing the second read, to clear
372          * the bit.
373          *
374          * FIXME the reason PWR_ISR1 needs an extra read is that
375          * RTC_IF retriggered until we cleared REG_ALARM_M above.
376          * But re-reading like this is a bad hack; by doing so we
377          * risk wrongly clearing status for some other IRQ (losing
378          * the interrupt).  Be smarter about handling RTC_UF ...
379          */
380         res = twl4030_i2c_read_u8(TWL4030_MODULE_INT,
381                         &rd_reg, TWL4030_INT_PWR_ISR1);
382         if (res)
383                 goto out;
384
385         /* Notify RTC core on event */
386         rtc_update_irq(rtc, 1, events);
387
388         ret = IRQ_HANDLED;
389 out:
390         return ret;
391 }
392
393 static struct rtc_class_ops twl4030_rtc_ops = {
394         .ioctl          = twl4030_rtc_ioctl,
395         .read_time      = twl4030_rtc_read_time,
396         .set_time       = twl4030_rtc_set_time,
397         .read_alarm     = twl4030_rtc_read_alarm,
398         .set_alarm      = twl4030_rtc_set_alarm,
399 };
400
401 /*----------------------------------------------------------------------*/
402
403 static int __devinit twl4030_rtc_probe(struct platform_device *pdev)
404 {
405         struct rtc_device *rtc;
406         int ret = 0;
407         int irq = platform_get_irq(pdev, 0);
408         u8 rd_reg;
409
410         if (irq < 0)
411                 return irq;
412
413         rtc = rtc_device_register(pdev->name,
414                                   &pdev->dev, &twl4030_rtc_ops, THIS_MODULE);
415         if (IS_ERR(rtc)) {
416                 ret = -EINVAL;
417                 dev_err(&pdev->dev, "can't register RTC device, err %ld\n",
418                         PTR_ERR(rtc));
419                 goto out0;
420
421         }
422
423         platform_set_drvdata(pdev, rtc);
424
425         ret = twl4030_rtc_read_u8(&rd_reg, REG_RTC_STATUS_REG);
426
427         if (ret < 0)
428                 goto out1;
429
430         if (rd_reg & BIT_RTC_STATUS_REG_POWER_UP_M)
431                 dev_warn(&pdev->dev, "Power up reset detected.\n");
432
433         if (rd_reg & BIT_RTC_STATUS_REG_ALARM_M)
434                 dev_warn(&pdev->dev, "Pending Alarm interrupt detected.\n");
435
436         /* Clear RTC Power up reset and pending alarm interrupts */
437         ret = twl4030_rtc_write_u8(rd_reg, REG_RTC_STATUS_REG);
438         if (ret < 0)
439                 goto out1;
440
441         ret = request_irq(irq, twl4030_rtc_interrupt,
442                                 0, rtc->dev.bus_id, rtc);
443         if (ret < 0) {
444                 dev_err(&pdev->dev, "IRQ is not free.\n");
445                 goto out1;
446         }
447
448         /* Check RTC module status, Enable if it is off */
449         ret = twl4030_rtc_read_u8(&rd_reg, REG_RTC_CTRL_REG);
450         if (ret < 0)
451                 goto out2;
452
453         if (!(rd_reg & BIT_RTC_CTRL_REG_STOP_RTC_M)) {
454                 dev_info(&pdev->dev, "Enabling TWL4030-RTC.\n");
455                 rd_reg = BIT_RTC_CTRL_REG_STOP_RTC_M;
456                 ret = twl4030_rtc_write_u8(rd_reg, REG_RTC_CTRL_REG);
457                 if (ret < 0)
458                         goto out2;
459         }
460
461         /* FIXME stop touching MODULE_INT registers; there's already
462          * driver code responsible for them.
463          */
464
465         /* use rising edge detection for RTC alarm */
466         ret = twl4030_i2c_read_u8(TWL4030_MODULE_INT,
467                         &rd_reg, TWL4030_INT_PWR_EDR1);
468         if (ret < 0)
469                 goto out2;
470
471         rd_reg |= BIT(3);
472         ret = twl4030_i2c_write_u8(TWL4030_MODULE_INT,
473                         rd_reg, TWL4030_INT_PWR_EDR1);
474         if (ret < 0)
475                 goto out2;
476
477         /* init cached IRQ enable bits */
478         ret = twl4030_rtc_read_u8(&rtc_irq_bits, REG_RTC_INTERRUPTS_REG);
479         if (ret < 0)
480                 goto out2;
481
482         return ret;
483
484
485 out2:
486         free_irq(irq, rtc);
487 out1:
488         rtc_device_unregister(rtc);
489 out0:
490         return ret;
491 }
492
493 /*
494  * Disable all TWL4030 RTC module interrupts.
495  * Sets status flag to free.
496  */
497 static int __devexit twl4030_rtc_remove(struct platform_device *pdev)
498 {
499         /* leave rtc running, but disable irqs */
500         struct rtc_device *rtc = platform_get_drvdata(pdev);
501         int irq = platform_get_irq(pdev, 0);
502
503         mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_ALARM_M);
504         mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M);
505
506         free_irq(irq, rtc);
507
508         rtc_device_unregister(rtc);
509         platform_set_drvdata(pdev, NULL);
510         return 0;
511 }
512
513 static void twl4030_rtc_shutdown(struct platform_device *pdev)
514 {
515         mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M |
516                          BIT_RTC_INTERRUPTS_REG_IT_ALARM_M);
517 }
518
519 #ifdef CONFIG_PM
520
521 static unsigned char irqstat;
522
523 static int twl4030_rtc_suspend(struct platform_device *pdev, pm_message_t state)
524 {
525         irqstat = rtc_irq_bits;
526
527         /* REVISIT alarm may need to wake us from sleep */
528         mask_rtc_irq_bit(BIT_RTC_INTERRUPTS_REG_IT_TIMER_M |
529                          BIT_RTC_INTERRUPTS_REG_IT_ALARM_M);
530         return 0;
531 }
532
533 static int twl4030_rtc_resume(struct platform_device *pdev)
534 {
535         set_rtc_irq_bit(irqstat);
536         return 0;
537 }
538
539 #else
540 #define twl4030_rtc_suspend NULL
541 #define twl4030_rtc_resume  NULL
542 #endif
543
544 MODULE_ALIAS("platform:twl4030_rtc");
545
546 static struct platform_driver twl4030rtc_driver = {
547         .probe          = twl4030_rtc_probe,
548         .remove         = __devexit_p(twl4030_rtc_remove),
549         .shutdown       = twl4030_rtc_shutdown,
550         .suspend        = twl4030_rtc_suspend,
551         .resume         = twl4030_rtc_resume,
552         .driver         = {
553                 .owner  = THIS_MODULE,
554                 .name   = "twl4030_rtc",
555         },
556 };
557
558 static int __init twl4030_rtc_init(void)
559 {
560         return platform_driver_register(&twl4030rtc_driver);
561 }
562 module_init(twl4030_rtc_init);
563
564 static void __exit twl4030_rtc_exit(void)
565 {
566         platform_driver_unregister(&twl4030rtc_driver);
567 }
568 module_exit(twl4030_rtc_exit);
569
570 MODULE_AUTHOR("Texas Instruments, MontaVista Software");
571 MODULE_LICENSE("GPL");