rtc: tps80031: register as mfd sub device
[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_platform_data *tps80031_pdata;
353         struct tps80031_rtc_platform_data *pdata;
354         struct tps80031_rtc *rtc;
355         struct rtc_time tm;
356         int err;
357         u8 reg;
358
359         tps80031_pdata = dev_get_platdata(pdev->dev.parent);
360         if (!tps80031_pdata) {
361                 dev_err(&pdev->dev, "no tps80031 platform_data specified\n");
362                 return -EINVAL;
363         }
364
365         pdata = tps80031_pdata->rtc_pdata;
366         if (!pdata) {
367                 dev_err(&pdev->dev, "no platform_data specified\n");
368                 return -EINVAL;
369         }
370
371         rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
372         if (!rtc)
373                 return -ENOMEM;
374
375         rtc->irq = -1;
376         if (pdata->irq < 0)
377                 dev_err(&pdev->dev, "no IRQ specified, wakeup is disabled\n");
378
379         rtc->msecure_gpio = -1;
380         if (gpio_is_valid(pdata->msecure_gpio)) {
381                 err = gpio_request(pdata->msecure_gpio, "tps80031 msecure");
382                 if (err == 0) {
383                         rtc->msecure_gpio = pdata->msecure_gpio;
384                         gpio_direction_output(rtc->msecure_gpio, 0);
385                 } else
386                         dev_warn(&pdev->dev, "could not get msecure GPIO\n");
387         }
388
389         rtc->rtc = rtc_device_register(pdev->name, &pdev->dev,
390                                        &tps80031_rtc_ops, THIS_MODULE);
391         dev_set_drvdata(&pdev->dev, rtc);
392
393         if (IS_ERR(rtc->rtc)) {
394                 err = PTR_ERR(rtc->rtc);
395                 goto fail;
396         }
397
398         if ((int)pdev && (int)&pdev->dev)
399                 err = tps80031_read_regs(&pdev->dev, RTC_STATUS, 1, &reg);
400         else {
401                 dev_err(&pdev->dev, "%s Input params incorrect\n", __func__);
402                 err = -EBUSY;
403                 goto fail;
404         }
405
406         if (err) {
407                 dev_err(&pdev->dev, "%s unable to read status\n", __func__);
408                 err = -EBUSY;
409                 goto fail;
410         }
411
412         /* If RTC have POR values, set time using platform data*/
413         tps80031_rtc_read_time(&pdev->dev, &tm);
414         if ((tm.tm_year == RTC_YEAR_OFFSET + RTC_POR_YEAR) &&
415                 (tm.tm_mon == (RTC_POR_MONTH - 1)) &&
416                 (tm.tm_mday == RTC_POR_DAY)) {
417                 if (pdata->time.tm_year < 2000 ||
418                         pdata->time.tm_year > 2100) {
419                         dev_err(&pdev->dev, "Invalid platform data\n");
420                         memset(&pdata->time, 0, sizeof(pdata->time));
421                         pdata->time.tm_year = 2011;
422                         pdata->time.tm_mday = 1;
423                 }
424                 tps80031_rtc_set_time(&pdev->dev, &pdata->time);
425         }
426
427         reg = ALARM_INT_STATUS;
428         err = tps80031_write_regs(&pdev->dev, RTC_STATUS, 1, &reg);
429         if (err) {
430                 dev_err(&pdev->dev, "unable to program RTC_STATUS reg\n");
431                 return -EBUSY;
432         }
433
434         tps80031_enable_rtc_write(&pdev->dev);
435         err = tps80031_set_bits(pdev->dev.parent, 1, RTC_INT, ENABLE_ALARM_INT);
436         tps80031_disable_rtc_write(&pdev->dev);
437         if (err) {
438                 dev_err(&pdev->dev, "unable to program Interrupt Mask reg\n");
439                 err = -EBUSY;
440                 rtc->alarm_irq_enabled = 0;
441                 goto fail;
442         } else
443                 rtc->alarm_irq_enabled = 1;
444
445         if (pdata && (pdata->irq >= 0)) {
446                 rtc->irq = pdata->irq;
447                 err = request_threaded_irq(pdata->irq, NULL, tps80031_rtc_irq,
448                                         IRQF_ONESHOT, "rtc_tps80031",
449                                         &pdev->dev);
450                 if (err) {
451                         dev_err(&pdev->dev, "request IRQ:%d fail\n", rtc->irq);
452                         rtc->irq = -1;
453                 } else {
454                         device_init_wakeup(&pdev->dev, 1);
455                         enable_irq_wake(rtc->irq);
456                 }
457         }
458         return 0;
459
460 fail:
461         if (!IS_ERR_OR_NULL(rtc->rtc))
462                 rtc_device_unregister(rtc->rtc);
463         kfree(rtc);
464         return err;
465 }
466
467 static int __devexit tps80031_rtc_remove(struct platform_device *pdev)
468 {
469         struct tps80031_rtc *rtc = dev_get_drvdata(&pdev->dev);
470
471         if (rtc->irq != -1)
472                 free_irq(rtc->irq, rtc);
473         rtc_device_unregister(rtc->rtc);
474         kfree(rtc);
475         return 0;
476 }
477
478 static struct platform_driver tps80031_rtc_driver = {
479         .driver = {
480                 .name   = "tps80031-rtc",
481                 .owner  = THIS_MODULE,
482         },
483         .probe  = tps80031_rtc_probe,
484         .remove = __devexit_p(tps80031_rtc_remove),
485 };
486
487 static int __init tps80031_rtc_init(void)
488 {
489         return platform_driver_register(&tps80031_rtc_driver);
490 }
491 module_init(tps80031_rtc_init);
492
493 static void __exit tps80031_rtc_exit(void)
494 {
495         platform_driver_unregister(&tps80031_rtc_driver);
496 }
497 module_exit(tps80031_rtc_exit);
498
499 MODULE_DESCRIPTION("TI TPS80031 RTC driver");
500 MODULE_AUTHOR("NVIDIA Corporation");
501 MODULE_LICENSE("GPL");
502 MODULE_ALIAS("platform:rtc_tps80031");