aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/rtc/rtc-tps6591x.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/rtc/rtc-tps6591x.c')
-rw-r--r--drivers/rtc/rtc-tps6591x.c546
1 files changed, 546 insertions, 0 deletions
diff --git a/drivers/rtc/rtc-tps6591x.c b/drivers/rtc/rtc-tps6591x.c
new file mode 100644
index 00000000000..cab3e8874df
--- /dev/null
+++ b/drivers/rtc/rtc-tps6591x.c
@@ -0,0 +1,546 @@
1/*
2 * drivers/rtc/rtc_tps6591x.c
3 *
4 * RTC driver for TI TPS6591x
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/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/*
50Linux 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
61struct tps6591x_rtc {
62 unsigned long epoch_start;
63 int irq;
64 struct rtc_device *rtc;
65 bool irq_en;
66};
67
68static 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
92static 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
106static int tps6591x_rtc_valid_tm(struct rtc_time *tm)
107{
108 if (tm->tm_year >= (RTC_YEAR_OFFSET + 99)
109 || tm->tm_mon >= 12
110 || tm->tm_mday < 1
111 || tm->tm_mday > rtc_month_days(tm->tm_mon, tm->tm_year + OS_REF_YEAR)
112 || tm->tm_hour >= 24
113 || tm->tm_min >= 60
114 || tm->tm_sec >= 60)
115 return -EINVAL;
116 return 0;
117}
118
119static u8 dec2bcd(u8 dec)
120{
121 return ((dec/10)<<4)+(dec%10);
122}
123
124static u8 bcd2dec(u8 bcd)
125{
126 return (bcd >> 4)*10+(bcd & 0xF);
127}
128
129static void convert_bcd_to_decimal(u8 *buf, u8 len)
130{
131 int i = 0;
132 for (i = 0; i < len; i++)
133 buf[i] = bcd2dec(buf[i]);
134}
135
136static void convert_decimal_to_bcd(u8 *buf, u8 len)
137{
138 int i = 0;
139 for (i = 0; i < len; i++)
140 buf[i] = dec2bcd(buf[i]);
141}
142
143static void print_time(struct device *dev, struct rtc_time *tm)
144{
145 dev_info(dev, "RTC Time : %d/%d/%d %d:%d:%d\n",
146 (tm->tm_mon + 1), tm->tm_mday, (tm->tm_year + OS_REF_YEAR),
147 tm->tm_hour, tm->tm_min , tm->tm_sec);
148}
149
150static int tps6591x_rtc_read_time(struct device *dev, struct rtc_time *tm)
151{
152 u8 buff[7];
153 int err;
154 err = tps6591x_read_regs(dev, RTC_SECONDS_REG, sizeof(buff), buff);
155 if (err < 0) {
156 dev_err(dev, "\n %s :: failed to read time\n", __FILE__);
157 return err;
158 }
159 convert_bcd_to_decimal(buff, sizeof(buff));
160 tm->tm_sec = buff[0];
161 tm->tm_min = buff[1];
162 tm->tm_hour = buff[2];
163 tm->tm_mday = buff[3];
164 tm->tm_mon = buff[4] - 1;
165 tm->tm_year = buff[5];
166 tm->tm_wday = buff[6];
167 print_time(dev, tm);
168 return tps6591x_rtc_valid_tm(tm);
169}
170
171static int tps6591x_rtc_stop(struct device *dev)
172{
173 u8 reg = 0;
174 u8 retries = 0;
175 int err;
176 do {
177 err = tps6591x_read_regs(dev, RTC_CTRL, 1, &reg);
178 if (err < 0) {
179 dev_err(dev->parent, "\n failed to read RTC_CTRL reg\n");
180 return err;
181 }
182
183 /* clear STOP bit alone */
184 reg &= ~0x1;
185
186 err = tps6591x_write_regs(dev, RTC_CTRL, 1, &reg);
187 if (err < 0) {
188 dev_err(dev->parent, "\n failed to program RTC_CTRL reg\n");
189 return err;
190 }
191
192 err = tps6591x_read_regs(dev, RTC_STATUS, 1, &reg);
193 if (err < 0) {
194 dev_err(dev->parent, "\n failed to read RTC_CTRL reg\n");
195 return err;
196 }
197 /* FixMe: Is allowing up to 5 retries sufficient?? */
198 if (retries++ == 5) {
199 dev_err(dev->parent, "\n failed to stop RTC\n");
200 return -EBUSY;
201 }
202 } while (reg & 2);
203 return 0;
204}
205
206static int tps6591x_rtc_start(struct device *dev)
207{
208 u8 reg = 0;
209 u8 retries = 0;
210 int err;
211
212 do {
213 err = tps6591x_read_regs(dev, RTC_CTRL, 1, &reg);
214 if (err < 0) {
215 dev_err(dev->parent, "\n failed to read RTC_CTRL reg\n");
216 return err;
217 }
218
219 /* set STOP bit alone */
220 reg |= 0x1;
221
222 err = tps6591x_write_regs(dev, RTC_CTRL, 1, &reg);
223 if (err < 0) {
224 dev_err(dev->parent, "\n failed to program RTC_CTRL reg\n");
225 return err;
226 }
227
228 err = tps6591x_read_regs(dev, RTC_STATUS, 1, &reg);
229 if (err < 0) {
230 dev_err(dev->parent, "\n failed to read RTC_CTRL reg\n");
231 return err;
232 }
233 /* FixMe: Is allowing up to 5 retries sufficient?? */
234 if (retries++ == 5) {
235 dev_err(dev->parent, "\n failed to stop RTC\n");
236 return -EBUSY;
237 }
238 } while (!(reg & 2));
239 return 0;
240}
241
242
243static int tps6591x_rtc_set_time(struct device *dev, struct rtc_time *tm)
244{
245 u8 buff[7];
246 int err;
247
248 buff[0] = tm->tm_sec;
249 buff[1] = tm->tm_min;
250 buff[2] = tm->tm_hour;
251 buff[3] = tm->tm_mday;
252 buff[4] = tm->tm_mon + 1;
253 buff[5] = tm->tm_year;
254 buff[6] = tm->tm_wday;
255
256 print_time(dev, tm);
257 convert_decimal_to_bcd(buff, sizeof(buff));
258 err = tps6591x_rtc_stop(dev);
259 if (err < 0) {
260 dev_err(dev->parent, "\n failed to clear RTC_ENABLE\n");
261 return err;
262 }
263
264 err = tps6591x_write_regs(dev, RTC_SECONDS_REG, sizeof(buff), buff);
265 if (err < 0) {
266 dev_err(dev->parent, "\n failed to program new time\n");
267 return err;
268 }
269
270 err = tps6591x_rtc_start(dev);
271 if (err < 0) {
272 dev_err(dev->parent, "\n failed to set RTC_ENABLE\n");
273 return err;
274 }
275
276 return 0;
277}
278
279static int tps6591x_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
280{
281 struct tps6591x_rtc *rtc = dev_get_drvdata(dev);
282 unsigned long seconds;
283 u8 buff[6];
284 int err;
285 struct rtc_time tm;
286
287 if (rtc->irq == -1)
288 return -EIO;
289
290 dev_info(dev->parent, "\n setting alarm to requested time::\n");
291 print_time(dev->parent, &alrm->time);
292 rtc_tm_to_time(&alrm->time, &seconds);
293 tps6591x_rtc_read_time(dev, &tm);
294 rtc_tm_to_time(&tm, &rtc->epoch_start);
295
296 if (WARN_ON(alrm->enabled && (seconds < rtc->epoch_start))) {
297 dev_err(dev->parent, "\n can't set alarm to requested time\n");
298 return -EINVAL;
299 }
300
301 if (alrm->enabled && !rtc->irq_en) {
302 rtc->irq_en = true;
303 } else if (!alrm->enabled && rtc->irq_en) {
304 rtc->irq_en = false;
305 }
306
307 buff[0] = alrm->time.tm_sec;
308 buff[1] = alrm->time.tm_min;
309 buff[2] = alrm->time.tm_hour;
310 buff[3] = alrm->time.tm_mday;
311 buff[4] = alrm->time.tm_mon + 1;
312 buff[5] = alrm->time.tm_year;
313 convert_decimal_to_bcd(buff, sizeof(buff));
314 err = tps6591x_write_regs(dev, RTC_ALARM, sizeof(buff), buff);
315 if (err)
316 dev_err(dev->parent, "\n unable to program alarm\n");
317
318 return err;
319}
320
321static int tps6591x_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
322{
323 u8 buff[6];
324 int err;
325
326 err = tps6591x_read_regs(dev, RTC_ALARM, sizeof(buff), buff);
327 if (err)
328 return err;
329 convert_bcd_to_decimal(buff, sizeof(buff));
330
331 alrm->time.tm_sec = buff[0];
332 alrm->time.tm_min = buff[1];
333 alrm->time.tm_hour = buff[2];
334 alrm->time.tm_mday = buff[3];
335 alrm->time.tm_mon = buff[4] - 1;
336 alrm->time.tm_year = buff[5];
337
338 dev_info(dev->parent, "\n getting alarm time::\n");
339 print_time(dev, &alrm->time);
340
341 return 0;
342}
343
344static int tps6591x_rtc_alarm_irq_enable(struct device *dev,
345 unsigned int enable)
346{
347 struct tps6591x_rtc *rtc = dev_get_drvdata(dev);
348 u8 reg;
349 int err;
350
351 if (rtc->irq == -1)
352 return -EIO;
353
354 if (enable) {
355 if (rtc->irq_en == true)
356 return 0;
357 err = tps6591x_read_regs(dev, RTC_INT, 1, &reg);
358 if (err)
359 return err;
360 reg |= 0x8;
361 err = tps6591x_write_regs(dev, RTC_INT, 1, &reg);
362 if (err)
363 return err;
364 rtc->irq_en = true;
365 } else {
366 if (rtc->irq_en == false)
367 return 0;
368 err = tps6591x_read_regs(dev, RTC_INT, 1, &reg);
369 if (err)
370 return err;
371 reg &= ~0x8;
372 err = tps6591x_write_regs(dev, RTC_INT, 1, &reg);
373 if (err)
374 return err;
375 rtc->irq_en = false;
376 }
377 return 0;
378}
379
380static const struct rtc_class_ops tps6591x_rtc_ops = {
381 .read_time = tps6591x_rtc_read_time,
382 .set_time = tps6591x_rtc_set_time,
383 .set_alarm = tps6591x_rtc_set_alarm,
384 .read_alarm = tps6591x_rtc_read_alarm,
385 .alarm_irq_enable = tps6591x_rtc_alarm_irq_enable,
386};
387
388static irqreturn_t tps6591x_rtc_irq(int irq, void *data)
389{
390 struct device *dev = data;
391 struct tps6591x_rtc *rtc = dev_get_drvdata(dev);
392 u8 reg;
393 int err;
394
395 /* clear Alarm status bits.*/
396 err = tps6591x_read_regs(dev, RTC_STATUS, 1, &reg);
397 if (err) {
398 dev_err(dev->parent, "unable to read RTC_STATUS reg\n");
399 return -EBUSY;
400 }
401
402 reg = ALARM_INT_STATUS;
403 err = tps6591x_write_regs(dev, RTC_STATUS, 1, &reg);
404 if (err) {
405 dev_err(dev->parent, "unable to program RTC_STATUS reg\n");
406 return -EBUSY;
407 }
408
409 rtc_update_irq(rtc->rtc, 1, RTC_IRQF | RTC_AF);
410 return IRQ_HANDLED;
411}
412
413static int __devinit tps6591x_rtc_probe(struct platform_device *pdev)
414{
415 struct tps6591x_rtc_platform_data *pdata = pdev->dev.platform_data;
416 struct tps6591x_rtc *rtc;
417 struct rtc_time tm;
418 int err;
419 u8 reg;
420
421 rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
422
423 if (!rtc)
424 return -ENOMEM;
425
426 rtc->irq = -1;
427
428 if (!pdata) {
429 dev_err(&pdev->dev, "no platform_data specified\n");
430 return -EINVAL;
431 }
432
433 if (pdata->irq < 0)
434 dev_err(&pdev->dev, "\n no IRQ specified, wakeup is disabled\n");
435
436 dev_set_drvdata(&pdev->dev, rtc);
437 device_init_wakeup(&pdev->dev, 1);
438 rtc->rtc = rtc_device_register(pdev->name, &pdev->dev,
439 &tps6591x_rtc_ops, THIS_MODULE);
440
441 if (IS_ERR(rtc->rtc)) {
442 err = PTR_ERR(rtc->rtc);
443 goto fail;
444 }
445
446 if ((int)pdev && (int)&pdev->dev)
447 err = tps6591x_read_regs(&pdev->dev, RTC_STATUS, 1, &reg);
448 else {
449 dev_err(&pdev->dev, "\n %s Input params incorrect\n", __func__);
450 return -EBUSY;
451 }
452 if (err) {
453 dev_err(&pdev->dev, "\n %s unable to read status\n", __func__);
454 return -EBUSY;
455 }
456
457 reg = RTC_BBCH_SEL | RTC_BBCH_EN;
458 tps6591x_write_regs(&pdev->dev, RTC_BBCH_REG, 1, &reg);
459 if (err) {
460 dev_err(&pdev->dev, "unable to program Charger reg\n");
461 return -EBUSY;
462 }
463
464 tps6591x_rtc_read_time(&pdev->dev, &tm);
465 if ((tm.tm_year < RTC_YEAR_OFFSET || tm.tm_year > (RTC_YEAR_OFFSET + 99))){
466 if (pdata->time.tm_year < 2000 || pdata->time.tm_year > 2100) {
467 memset(&pdata->time, 0, sizeof(pdata->time));
468 pdata->time.tm_year = RTC_YEAR_OFFSET;
469 pdata->time.tm_mday = 1;
470 } else
471 pdata->time.tm_year -= OS_REF_YEAR;
472 tps6591x_rtc_set_time(&pdev->dev, &pdata->time);
473 }
474
475 reg = ALARM_INT_STATUS;
476 err = tps6591x_write_regs(&pdev->dev, RTC_STATUS, 1, &reg);
477 if (err) {
478 dev_err(&pdev->dev, "unable to program RTC_STATUS reg\n");
479 return -EBUSY;
480 }
481
482 reg = ENABLE_ALARM_INT;
483 tps6591x_write_regs(&pdev->dev, RTC_INT, 1, &reg);
484 if (err) {
485 dev_err(&pdev->dev, "unable to program Interrupt Mask reg\n");
486 return -EBUSY;
487 }
488
489 if (pdata && (pdata->irq >= 0)) {
490 rtc->irq = pdata->irq;
491 err = request_threaded_irq(pdata->irq, NULL, tps6591x_rtc_irq,
492 IRQF_ONESHOT, "rtc_tps6591x",
493 &pdev->dev);
494 if (err) {
495 dev_err(&pdev->dev, "request IRQ:%d fail\n", rtc->irq);
496 rtc->irq = -1;
497 } else {
498 device_init_wakeup(&pdev->dev, 1);
499 enable_irq_wake(rtc->irq);
500 }
501 }
502 return 0;
503
504fail:
505 if (!IS_ERR_OR_NULL(rtc->rtc))
506 rtc_device_unregister(rtc->rtc);
507 kfree(rtc);
508 return err;
509}
510
511static int __devexit tps6591x_rtc_remove(struct platform_device *pdev)
512{
513 struct tps6591x_rtc *rtc = dev_get_drvdata(&pdev->dev);
514
515 if (rtc->irq != -1)
516 free_irq(rtc->irq, rtc);
517 rtc_device_unregister(rtc->rtc);
518 kfree(rtc);
519 return 0;
520}
521
522static struct platform_driver tps6591x_rtc_driver = {
523 .driver = {
524 .name = "rtc_tps6591x",
525 .owner = THIS_MODULE,
526 },
527 .probe = tps6591x_rtc_probe,
528 .remove = __devexit_p(tps6591x_rtc_remove),
529};
530
531static int __init tps6591x_rtc_init(void)
532{
533 return platform_driver_register(&tps6591x_rtc_driver);
534}
535module_init(tps6591x_rtc_init);
536
537static void __exit tps6591x_rtc_exit(void)
538{
539 platform_driver_unregister(&tps6591x_rtc_driver);
540}
541module_exit(tps6591x_rtc_exit);
542
543MODULE_DESCRIPTION("TI TPS6591x RTC driver");
544MODULE_AUTHOR("NVIDIA Corporation");
545MODULE_LICENSE("GPL");
546MODULE_ALIAS("platform:rtc_tps6591x");