media: tegra: add supporting for RAW8/RAW10 input and output
[linux-2.6.git] / drivers / rtc / rtc-tps6591x.c
1 /*
2  * drivers/rtc/rtc_tps6591x.c
3  *
4  * RTC driver for TI TPS6591x
5  *
6  * Copyright (c) 2011-2012, 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/tps6591x.h>
31 #include <linux/platform_device.h>
32 #include <linux/rtc.h>
33 #include <linux/slab.h>
34
35 #define RTC_CTRL                0x10
36 #define RTC_STATUS              0x11
37 #define RTC_SECONDS_REG         0x0
38 #define RTC_ALARM               0x8
39 #define RTC_INT                 0x12
40 #define RTC_RESET_STATUS        0x16
41 #define RTC_BBCH_REG            0x39
42
43 #define RTC_BBCH_SEL            0x02
44 #define RTC_BBCH_EN             0x01
45 #define ENABLE_ALARM_INT 0x8
46 #define RTC_RESET_VALUE 0x80
47 #define ALARM_INT_STATUS 0x40
48
49 /*
50 Linux RTC driver refers 1900 as base year in many calculations.
51 (e.g. refer drivers/rtc/rtc-lib.c)
52 */
53 #define OS_REF_YEAR 1900
54
55 /*
56         PMU RTC have only 2 nibbles to store year information, so using an offset
57         of 100 to set the base year as 2000 for our driver.
58 */
59 #define RTC_YEAR_OFFSET 100
60
61 struct tps6591x_rtc {
62         unsigned long           epoch_start;
63         int                     irq;
64         struct rtc_device       *rtc;
65         bool                    irq_en;
66 };
67
68 static int tps6591x_read_regs(struct device *dev, int reg, int len,
69         uint8_t *val)
70 {
71         int ret;
72
73         /* dummy read of STATUS_REG as per data sheet */
74         ret = tps6591x_reads(dev->parent, RTC_STATUS, 1, val);
75         if (ret < 0) {
76                 dev_err(dev->parent, "\n %s failed reading from RTC_STATUS\n",
77                         __func__);
78                 WARN_ON(1);
79                 return ret;
80         }
81
82         ret = tps6591x_reads(dev->parent, reg, len, val);
83         if (ret < 0) {
84                 dev_err(dev->parent, "\n %s failed reading from 0x%02x\n",
85                         __func__, reg);
86                 WARN_ON(1);
87                 return ret;
88         }
89         return 0;
90 }
91
92 static int tps6591x_write_regs(struct device *dev, int reg, int len,
93         uint8_t *val)
94 {
95         int ret;
96         ret = tps6591x_writes(dev->parent, reg, len, val);
97         if (ret < 0) {
98                 dev_err(dev->parent, "\n %s failed writing\n", __func__);
99                 WARN_ON(1);
100                 return ret;
101         }
102
103         return 0;
104 }
105
106 static int tps6591x_rtc_valid_tm(struct rtc_time *tm)
107 {
108         if (tm->tm_year >= (RTC_YEAR_OFFSET + 99)
109                 || tm->tm_year < (RTC_YEAR_OFFSET)
110                 || tm->tm_mon >= 12
111                 || tm->tm_mday < 1
112                 || tm->tm_mday > rtc_month_days(tm->tm_mon, tm->tm_year + OS_REF_YEAR)
113                 || tm->tm_hour >= 24
114                 || tm->tm_min >= 60
115                 || tm->tm_sec >= 60)
116                 return -EINVAL;
117         return 0;
118 }
119
120 static u8 dec2bcd(u8 dec)
121 {
122         return ((dec/10)<<4)+(dec%10);
123 }
124
125 static u8 bcd2dec(u8 bcd)
126 {
127         return (bcd >> 4)*10+(bcd & 0xF);
128 }
129
130 static void convert_bcd_to_decimal(u8 *buf, u8 len)
131 {
132         int i = 0;
133         for (i = 0; i < len; i++)
134                 buf[i] = bcd2dec(buf[i]);
135 }
136
137 static void convert_decimal_to_bcd(u8 *buf, u8 len)
138 {
139         int i = 0;
140         for (i = 0; i < len; i++)
141                 buf[i] = dec2bcd(buf[i]);
142 }
143
144 static void print_time(struct device *dev, struct rtc_time *tm)
145 {
146         dev_info(dev, "RTC Time : %d/%d/%d %d:%d:%d\n",
147                 (tm->tm_mon + 1), tm->tm_mday, (tm->tm_year + OS_REF_YEAR),
148                 tm->tm_hour, tm->tm_min , tm->tm_sec);
149 }
150
151 static int tps6591x_rtc_read_time(struct device *dev, struct rtc_time *tm)
152 {
153         u8 buff[7];
154         int err;
155         err = tps6591x_read_regs(dev, RTC_SECONDS_REG, sizeof(buff), buff);
156         if (err < 0) {
157                 dev_err(dev, "\n %s :: failed to read time\n", __FILE__);
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         print_time(dev, tm);
169         return tps6591x_rtc_valid_tm(tm);
170 }
171
172 static int tps6591x_rtc_stop(struct device *dev)
173 {
174         u8 reg = 0;
175         u8 retries = 0;
176         int err;
177         do {
178                 err = tps6591x_read_regs(dev, RTC_CTRL, 1, &reg);
179                 if (err < 0) {
180                         dev_err(dev->parent, "\n failed to read RTC_CTRL reg\n");
181                         return err;
182                 }
183
184                 /* clear STOP bit alone */
185                 reg &= ~0x1;
186
187                 err = tps6591x_write_regs(dev, RTC_CTRL, 1, &reg);
188                 if (err < 0) {
189                         dev_err(dev->parent, "\n failed to program RTC_CTRL reg\n");
190                         return err;
191                 }
192
193                 err = tps6591x_read_regs(dev, RTC_STATUS, 1, &reg);
194                 if (err < 0) {
195                         dev_err(dev->parent, "\n failed to read RTC_CTRL reg\n");
196                         return err;
197                 }
198                 /* FixMe: Is allowing up to 5 retries sufficient?? */
199                 if (retries++ == 5) {
200                         dev_err(dev->parent, "\n failed to stop RTC\n");
201                         return -EBUSY;
202                 }
203         }       while (reg & 2);
204         return 0;
205 }
206
207 static int tps6591x_rtc_start(struct device *dev)
208 {
209         u8 reg = 0;
210         u8 retries = 0;
211         int err;
212
213         do {
214                 err = tps6591x_read_regs(dev, RTC_CTRL, 1, &reg);
215                 if (err < 0) {
216                         dev_err(dev->parent, "\n failed to read RTC_CTRL reg\n");
217                         return err;
218                 }
219
220                 /* set STOP bit alone */
221                 reg |= 0x1;
222
223                 err = tps6591x_write_regs(dev, RTC_CTRL, 1, &reg);
224                 if (err < 0) {
225                         dev_err(dev->parent, "\n failed to program RTC_CTRL reg\n");
226                         return err;
227                 }
228
229                 err = tps6591x_read_regs(dev, RTC_STATUS, 1, &reg);
230                 if (err < 0) {
231                         dev_err(dev->parent, "\n failed to read RTC_CTRL reg\n");
232                         return err;
233                 }
234                 /* FixMe: Is allowing up to 5 retries sufficient?? */
235                 if (retries++ == 5) {
236                         dev_err(dev->parent, "\n failed to stop RTC\n");
237                         return -EBUSY;
238                 }
239         }       while (!(reg & 2));
240         return 0;
241 }
242
243
244 static int tps6591x_rtc_set_time(struct device *dev, struct rtc_time *tm)
245 {
246         u8 buff[7];
247         int err;
248
249         err = tps6591x_rtc_valid_tm(tm);
250         if (err < 0) {
251                 dev_err(dev->parent, "\n Invalid Time\n");
252                 return err;
253         }
254
255         buff[0] = tm->tm_sec;
256         buff[1] = tm->tm_min;
257         buff[2] = tm->tm_hour;
258         buff[3] = tm->tm_mday;
259         buff[4] = tm->tm_mon + 1;
260         buff[5] = tm->tm_year % RTC_YEAR_OFFSET;
261         buff[6] = tm->tm_wday;
262
263         print_time(dev, tm);
264         convert_decimal_to_bcd(buff, sizeof(buff));
265         err = tps6591x_rtc_stop(dev);
266         if (err < 0) {
267                 dev_err(dev->parent, "\n failed to clear RTC_ENABLE\n");
268                 return err;
269         }
270
271         err = tps6591x_write_regs(dev, RTC_SECONDS_REG, sizeof(buff), buff);
272         if (err < 0) {
273                 dev_err(dev->parent, "\n failed to program new time\n");
274                 return err;
275         }
276
277         err = tps6591x_rtc_start(dev);
278         if (err < 0) {
279                 dev_err(dev->parent, "\n failed to set RTC_ENABLE\n");
280                 return err;
281         }
282
283         return 0;
284 }
285
286 static int tps6591x_rtc_alarm_irq_enable(struct device *dev,
287                                          unsigned int enable)
288 {
289         struct tps6591x_rtc *rtc = dev_get_drvdata(dev);
290         u8 reg;
291         int err;
292
293         if (rtc->irq == -1)
294                 return -EIO;
295
296         if (enable) {
297                 if (rtc->irq_en == true)
298                         return 0;
299                 err = tps6591x_read_regs(dev, RTC_INT, 1, &reg);
300                 if (err)
301                         return err;
302                 reg |= 0x8;
303                 err = tps6591x_write_regs(dev, RTC_INT, 1, &reg);
304                 if (err)
305                         return err;
306                 rtc->irq_en = true;
307         } else {
308                 if (rtc->irq_en == false)
309                         return 0;
310                 err = tps6591x_read_regs(dev, RTC_INT, 1, &reg);
311                 if (err)
312                         return err;
313                 reg &= ~0x8;
314                 err = tps6591x_write_regs(dev, RTC_INT, 1, &reg);
315                 if (err)
316                         return err;
317                 rtc->irq_en = false;
318         }
319         return 0;
320 }
321
322 static int tps6591x_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
323 {
324         struct tps6591x_rtc *rtc = dev_get_drvdata(dev);
325         unsigned long seconds;
326         u8 buff[6];
327         int err;
328         struct rtc_time tm;
329
330         if (rtc->irq == -1)
331                 return -EIO;
332
333         err = tps6591x_rtc_valid_tm(&alrm->time);
334         if (err < 0) {
335                 dev_err(dev->parent, "\n Invalid alarm time\n");
336                 return err;
337         }
338
339         dev_info(dev->parent, "\n setting alarm to requested time::\n");
340         print_time(dev->parent, &alrm->time);
341         rtc_tm_to_time(&alrm->time, &seconds);
342         tps6591x_rtc_read_time(dev, &tm);
343         rtc_tm_to_time(&tm, &rtc->epoch_start);
344
345         if (WARN_ON(alrm->enabled && (seconds < rtc->epoch_start))) {
346                 dev_err(dev->parent, "\n can't set alarm to requested time\n");
347                 return -EINVAL;
348         }
349
350         err = tps6591x_rtc_alarm_irq_enable(dev, alrm->enabled);
351         if(err) {
352                 dev_err(dev->parent, "\n can't set alarm irq\n");
353                 return err;
354         }
355
356         buff[0] = alrm->time.tm_sec;
357         buff[1] = alrm->time.tm_min;
358         buff[2] = alrm->time.tm_hour;
359         buff[3] = alrm->time.tm_mday;
360         buff[4] = alrm->time.tm_mon + 1;
361         buff[5] = alrm->time.tm_year % RTC_YEAR_OFFSET;
362         convert_decimal_to_bcd(buff, sizeof(buff));
363         err = tps6591x_write_regs(dev, RTC_ALARM, sizeof(buff), buff);
364         if (err)
365                 dev_err(dev->parent, "\n unable to program alarm\n");
366
367         return err;
368 }
369
370 static int tps6591x_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
371 {
372         u8 buff[6];
373         int err;
374
375         err = tps6591x_read_regs(dev, RTC_ALARM, sizeof(buff), buff);
376         if (err)
377                 return err;
378         convert_bcd_to_decimal(buff, sizeof(buff));
379
380         alrm->time.tm_sec = buff[0];
381         alrm->time.tm_min = buff[1];
382         alrm->time.tm_hour = buff[2];
383         alrm->time.tm_mday = buff[3];
384         alrm->time.tm_mon = buff[4] - 1;
385         alrm->time.tm_year = buff[5] + RTC_YEAR_OFFSET;
386
387         dev_info(dev->parent, "\n getting alarm time::\n");
388         print_time(dev, &alrm->time);
389
390         return 0;
391 }
392
393 static const struct rtc_class_ops tps6591x_rtc_ops = {
394         .read_time      = tps6591x_rtc_read_time,
395         .set_time       = tps6591x_rtc_set_time,
396         .set_alarm      = tps6591x_rtc_set_alarm,
397         .read_alarm     = tps6591x_rtc_read_alarm,
398         .alarm_irq_enable = tps6591x_rtc_alarm_irq_enable,
399 };
400
401 static irqreturn_t tps6591x_rtc_irq(int irq, void *data)
402 {
403         struct device *dev = data;
404         struct tps6591x_rtc *rtc = dev_get_drvdata(dev);
405         u8 reg;
406         int err;
407
408         /* clear Alarm status bits.*/
409         err = tps6591x_read_regs(dev, RTC_STATUS, 1, &reg);
410         if (err) {
411                 dev_err(dev->parent, "unable to read RTC_STATUS reg\n");
412                 return -EBUSY;
413         }
414
415         reg = ALARM_INT_STATUS;
416         err = tps6591x_write_regs(dev, RTC_STATUS, 1, &reg);
417         if (err) {
418                 dev_err(dev->parent, "unable to program RTC_STATUS reg\n");
419                 return -EBUSY;
420         }
421
422         rtc_update_irq(rtc->rtc, 1, RTC_IRQF | RTC_AF);
423         return IRQ_HANDLED;
424 }
425
426 static int __devinit tps6591x_rtc_probe(struct platform_device *pdev)
427 {
428         struct tps6591x_rtc_platform_data *pdata = pdev->dev.platform_data;
429         struct tps6591x_rtc *rtc;
430         struct rtc_time tm;
431         int err;
432         u8 reg;
433
434         rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
435
436         if (!rtc)
437                 return -ENOMEM;
438
439         rtc->irq = -1;
440
441         if (!pdata) {
442                 dev_err(&pdev->dev, "no platform_data specified\n");
443                 return -EINVAL;
444         }
445
446         if (pdata->irq < 0)
447                 dev_err(&pdev->dev, "\n no IRQ specified, wakeup is disabled\n");
448
449         dev_set_drvdata(&pdev->dev, rtc);
450         device_init_wakeup(&pdev->dev, 1);
451         rtc->rtc = rtc_device_register(pdev->name, &pdev->dev,
452                                        &tps6591x_rtc_ops, THIS_MODULE);
453
454         if (IS_ERR(rtc->rtc)) {
455                 err = PTR_ERR(rtc->rtc);
456                 goto fail;
457         }
458
459         if ((int)pdev && (int)&pdev->dev)
460                 err = tps6591x_read_regs(&pdev->dev, RTC_STATUS, 1, &reg);
461         else {
462                 dev_err(&pdev->dev, "\n %s Input params incorrect\n", __func__);
463                 return -EBUSY;
464         }
465         if (err) {
466                 dev_err(&pdev->dev, "\n %s unable to read status\n", __func__);
467                 return -EBUSY;
468         }
469
470         reg = RTC_BBCH_SEL | RTC_BBCH_EN;
471         tps6591x_write_regs(&pdev->dev, RTC_BBCH_REG, 1, &reg);
472         if (err) {
473                 dev_err(&pdev->dev, "unable to program Charger reg\n");
474                 return -EBUSY;
475         }
476
477         err = tps6591x_rtc_start(&pdev->dev);
478         if (err) {
479                 dev_err(&pdev->dev, "unable to start RTC\n");
480                 return -EBUSY;
481         }
482
483         tps6591x_rtc_read_time(&pdev->dev, &tm);
484
485         if (tps6591x_rtc_valid_tm(&tm) < 0) {
486                 if (pdata->time.tm_year < 2000 || pdata->time.tm_year >= 2100) {
487                         memset(&pdata->time, 0, sizeof(pdata->time));
488                         pdata->time.tm_year = 2000;
489                         pdata->time.tm_mday = 1;
490                 }
491                 pdata->time.tm_year -= OS_REF_YEAR;
492                 tps6591x_rtc_set_time(&pdev->dev, &pdata->time);
493         }
494
495         reg = ALARM_INT_STATUS;
496         err = tps6591x_write_regs(&pdev->dev, RTC_STATUS, 1, &reg);
497         if (err) {
498                 dev_err(&pdev->dev, "unable to program RTC_STATUS reg\n");
499                 return -EBUSY;
500         }
501
502         reg = ENABLE_ALARM_INT;
503         tps6591x_write_regs(&pdev->dev, RTC_INT, 1, &reg);
504         if (err) {
505                 dev_err(&pdev->dev, "unable to program Interrupt Mask reg\n");
506                 return -EBUSY;
507         }
508
509         if (pdata && (pdata->irq >= 0)) {
510                 rtc->irq = pdata->irq;
511                 err = request_threaded_irq(pdata->irq, NULL, tps6591x_rtc_irq,
512                                         IRQF_ONESHOT, "rtc_tps6591x",
513                                         &pdev->dev);
514                 if (err) {
515                         dev_err(&pdev->dev, "request IRQ:%d fail\n", rtc->irq);
516                         rtc->irq = -1;
517                 } else {
518                         device_init_wakeup(&pdev->dev, 1);
519                         enable_irq_wake(rtc->irq);
520                 }
521         }
522         return 0;
523
524 fail:
525         if (!IS_ERR_OR_NULL(rtc->rtc))
526                 rtc_device_unregister(rtc->rtc);
527         kfree(rtc);
528         return err;
529 }
530
531 static int __devexit tps6591x_rtc_remove(struct platform_device *pdev)
532 {
533         struct tps6591x_rtc *rtc = dev_get_drvdata(&pdev->dev);
534
535         if (rtc->irq != -1)
536                 free_irq(rtc->irq, rtc);
537         rtc_device_unregister(rtc->rtc);
538         kfree(rtc);
539         return 0;
540 }
541
542 static struct platform_driver tps6591x_rtc_driver = {
543         .driver = {
544                 .name   = "rtc_tps6591x",
545                 .owner  = THIS_MODULE,
546         },
547         .probe  = tps6591x_rtc_probe,
548         .remove = __devexit_p(tps6591x_rtc_remove),
549 };
550
551 static int __init tps6591x_rtc_init(void)
552 {
553         return platform_driver_register(&tps6591x_rtc_driver);
554 }
555 module_init(tps6591x_rtc_init);
556
557 static void __exit tps6591x_rtc_exit(void)
558 {
559         platform_driver_unregister(&tps6591x_rtc_driver);
560 }
561 module_exit(tps6591x_rtc_exit);
562
563 MODULE_DESCRIPTION("TI TPS6591x RTC driver");
564 MODULE_AUTHOR("NVIDIA Corporation");
565 MODULE_LICENSE("GPL");
566 MODULE_ALIAS("platform:rtc_tps6591x");