blob: d8b350bc0bb89eab71911a9014a3d2f35d17b1a4 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/rtc.h>
16#include <linux/mfd/pmic8058.h>
17#include <linux/pm.h>
18#include <linux/slab.h>
19#include <linux/rtc/rtc-pm8058.h>
20#include <linux/pm_runtime.h>
21
22#define PM8058_RTC_CTRL 0x1E8
23 #define PM8058_RTC_ENABLE BIT(7)
24 #define PM8058_RTC_ALARM_ENABLE BIT(1)
Ashay Jaiswal4d1ab552011-07-15 11:30:49 +053025 #define PM8058_RTC_ABORT_ENABLE BIT(0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070026#define PM8058_RTC_ALARM_CTRL 0x1E9
27 #define PM8058_RTC_ALARM_CLEAR BIT(0)
28#define PM8058_RTC_TEST 0x1F6
29#define PM8058_RTC_READ_BASE 0x1EE
30#define PM8058_RTC_WRITE_BASE 0x1EA
31#define PM8058_RTC_ALARM_BASE 0x1F2
32
33struct pm8058_rtc {
34 struct rtc_device *rtc0;
35 u8 rtc_ctrl_reg;
36 int rtc_irq;
37 int rtc_alarm_irq;
38 struct pm8058_chip *pm_chip;
39};
40
41static int
42pm8058_rtc_read_bytes(struct pm8058_rtc *rtc_dd, u8 *rtc_val, int base)
43{
44 int i, rc;
45
46 /*
47 * Read the 32-bit RTC/Alarm Value.
48 * These values have to be read 8-bit at a time.
49 */
50 for (i = 0; i < 4; i++) {
51 rc = pm8058_read(rtc_dd->pm_chip, base + i, &rtc_val[i], 1);
52 if (rc < 0) {
53 pr_err("%s: PM8058 read failed\n", __func__);
54 return rc;
55 }
56 }
57
58 return 0;
59}
60
61static int
62pm8058_rtc_write_bytes(struct pm8058_rtc *rtc_dd, u8 *rtc_val, int base)
63{
64 int i, rc;
65
66 /*
67 * Write the 32-bit Value.
68 * These values have to be written 8-bit at a time.
69 */
70 for (i = 0; i < 4; i++) {
71 rc = pm8058_write(rtc_dd->pm_chip, base + i, &rtc_val[i], 1);
72 if (rc < 0) {
73 pr_err("%s: PM8058 read failed\n", __func__);
74 return rc;
75 }
76 }
77
78 return 0;
79}
80
81/*
82 * Steps to write the RTC registers.
83 * 1. Disable alarm if enabled.
84 * 2. Write 0x00 to LSB.
85 * 3. Write Byte[1], Byte[2], Byte[3] then Byte[0].
86 * 4. Enable alarm if disabled earlier.
87 */
88#ifdef CONFIG_RTC_PM8058_WRITE_ENABLE
89static int
90pm8058_rtc0_set_time(struct device *dev, struct rtc_time *tm)
91{
92 int rc;
93 unsigned long secs = 0;
94 u8 value[4], reg = 0, alarm_enabled = 0, ctrl_reg = 0, i;
95 struct pm8058_rtc *rtc_dd = dev_get_drvdata(dev);
96
97 ctrl_reg = rtc_dd->rtc_ctrl_reg;
98
99 rtc_tm_to_time(tm, &secs);
100
101 value[0] = secs & 0xFF;
102 value[1] = (secs >> 8) & 0xFF;
103 value[2] = (secs >> 16) & 0xFF;
104 value[3] = (secs >> 24) & 0xFF;
105
106 pr_debug("%s: Seconds value to be written to RTC = %lu\n", __func__,
107 secs);
108 /* Disable alarm before updating RTC */
109 if (ctrl_reg & PM8058_RTC_ALARM_ENABLE) {
110 alarm_enabled = 1;
111 ctrl_reg &= ~PM8058_RTC_ALARM_ENABLE;
112 rc = pm8058_write(rtc_dd->pm_chip, PM8058_RTC_CTRL,
113 &ctrl_reg, 1);
114 if (rc < 0) {
115 pr_err("%s: PM8058 write failed\n", __func__);
116 return rc;
117 }
118 }
119
120 /* Write Byte[1], Byte[2], Byte[3], Byte[0] */
121 reg = 0;
122 rc = pm8058_write(rtc_dd->pm_chip, PM8058_RTC_WRITE_BASE, &reg, 1);
123 if (rc < 0) {
124 pr_err("%s: PM8058 write failed\n", __func__);
125 return rc;
126 }
127
128 for (i = 1; i < 4; i++) {
129 rc = pm8058_write(rtc_dd->pm_chip, PM8058_RTC_WRITE_BASE + i,
130 &value[i], 1);
131 if (rc < 0) {
132 pr_err("%s:Write to RTC registers failed\n", __func__);
133 return rc;
134 }
135 }
136
137 rc = pm8058_write(rtc_dd->pm_chip, PM8058_RTC_WRITE_BASE,
138 &value[0], 1);
139 if (rc < 0) {
140 pr_err("%s: PM8058 write failed\n", __func__);
141 return rc;
142 }
143
144 if (alarm_enabled) {
145 ctrl_reg |= PM8058_RTC_ALARM_ENABLE;
146 rc = pm8058_write(rtc_dd->pm_chip, PM8058_RTC_CTRL,
147 &ctrl_reg, 1);
148 if (rc < 0) {
149 pr_err("%s: PM8058 write failed\n", __func__);
150 return rc;
151 }
152 }
153
154 rtc_dd->rtc_ctrl_reg = ctrl_reg;
155
156 return 0;
157}
158#endif
159
160static int
161pm8058_rtc0_read_time(struct device *dev, struct rtc_time *tm)
162{
163 int rc;
164 u8 value[4], reg;
165 unsigned long secs = 0;
166 struct pm8058_rtc *rtc_dd = dev_get_drvdata(dev);
167
168 rc = pm8058_rtc_read_bytes(rtc_dd, value, PM8058_RTC_READ_BASE);
169 if (rc < 0) {
170 pr_err("%s: RTC time read failed\n", __func__);
171 return rc;
172 }
173
174 /*
175 * Read the LSB again and check if there has been a carry over.
176 * If there is, redo the read operation.
177 */
178 rc = pm8058_read(rtc_dd->pm_chip, PM8058_RTC_READ_BASE, &reg, 1);
179 if (rc < 0) {
180 pr_err("%s: PM8058 read failed\n", __func__);
181 return rc;
182 }
183
184 if (unlikely(reg < value[0])) {
185 rc = pm8058_rtc_read_bytes(rtc_dd, value,
186 PM8058_RTC_READ_BASE);
187 if (rc < 0) {
188 pr_err("%s: RTC time read failed\n", __func__);
189 return rc;
190 }
191 }
192
193 secs = value[0] | (value[1] << 8) | (value[2] << 16) | (value[3] << 24);
194
195 rtc_time_to_tm(secs, tm);
196
197 rc = rtc_valid_tm(tm);
198 if (rc < 0) {
199 pr_err("%s: Invalid time read from PMIC8058\n", __func__);
200 return rc;
201 }
202
203 pr_debug("%s: secs = %lu, h::m:s == %d::%d::%d, d/m/y = %d/%d/%d\n",
204 __func__, secs, tm->tm_hour, tm->tm_min, tm->tm_sec,
205 tm->tm_mday, tm->tm_mon, tm->tm_year);
206
207 return 0;
208}
209
210static int
211pm8058_rtc0_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
212{
213 int rc;
214 u8 value[4], reg;
215 struct rtc_time rtc_tm;
216 unsigned long secs_alarm, secs_rtc;
217 struct pm8058_rtc *rtc_dd = dev_get_drvdata(dev);
218
219 reg = rtc_dd->rtc_ctrl_reg;
220
221 /* Check if the alarm is valid */
222 rc = rtc_valid_tm(&alarm->time);
223 if (rc < 0) {
224 pr_err("%s: Alarm time invalid\n", __func__);
225 return -EINVAL;
226 }
227
228 rtc_tm_to_time(&alarm->time, &secs_alarm);
229
230 /*
231 * Read the current RTC time and verify if the alarm time is in the
232 * past. If yes, return invalid.
233 */
234 rc = pm8058_rtc0_read_time(dev, &rtc_tm);
235 if (rc) {
236 pr_err("%s: Unable to read RTC time\n", __func__);
237 return -EINVAL;
238 }
239 rtc_tm_to_time(&rtc_tm, &secs_rtc);
240
241 if (secs_alarm < secs_rtc) {
242 pr_err("%s: Trying to set alarm in the past\n", __func__);
243 return -EINVAL;
244 }
245
246 value[0] = secs_alarm & 0xFF;
247 value[1] = (secs_alarm >> 8) & 0xFF;
248 value[2] = (secs_alarm >> 16) & 0xFF;
249 value[3] = (secs_alarm >> 24) & 0xFF;
250
251 rc = pm8058_rtc_write_bytes(rtc_dd, value, PM8058_RTC_ALARM_BASE);
252 if (rc < 0) {
253 pr_err("%s: Alarm could not be set\n", __func__);
254 return rc;
255 }
256
257 reg = (alarm->enabled) ? (reg | PM8058_RTC_ALARM_ENABLE) :
258 (reg & ~PM8058_RTC_ALARM_ENABLE);
259
260 rc = pm8058_write(rtc_dd->pm_chip, PM8058_RTC_CTRL, &reg, 1);
261 if (rc < 0) {
262 pr_err("%s: PM8058 write failed\n", __func__);
263 return rc;
264 }
265
266 rtc_dd->rtc_ctrl_reg = reg;
267
268 pr_debug("%s: Alarm Set for h:r:s=%d:%d:%d, d/m/y=%d/%d/%d\n",
269 __func__, alarm->time.tm_hour, alarm->time.tm_min,
270 alarm->time.tm_sec, alarm->time.tm_mday,
271 alarm->time.tm_mon, alarm->time.tm_year);
272
273 return 0;
274}
275
276static int
277pm8058_rtc0_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
278{
279 int rc;
280 u8 value[4], reg;
281 unsigned long secs = 0;
282 struct pm8058_rtc *rtc_dd = dev_get_drvdata(dev);
283
284 reg = rtc_dd->rtc_ctrl_reg;
285
286 alarm->enabled = !!(reg & PM8058_RTC_ALARM_ENABLE);
287
288 rc = pm8058_rtc_read_bytes(rtc_dd, value,
289 PM8058_RTC_ALARM_BASE);
290 if (rc < 0) {
291 pr_err("%s: RTC alarm time read failed\n", __func__);
292 return rc;
293 }
294
295 secs = value[0] | (value[1] << 8) | (value[2] << 16) | (value[3] << 24);
296
297 rtc_time_to_tm(secs, &alarm->time);
298
299 rc = rtc_valid_tm(&alarm->time);
300 if (rc < 0) {
301 pr_err("%s: Invalid time read from PMIC8058\n", __func__);
302 return rc;
303 }
304
305 pr_debug("%s: Alarm set for - h:r:s=%d:%d:%d, d/m/y=%d/%d/%d\n",
306 __func__, alarm->time.tm_hour, alarm->time.tm_min,
307 alarm->time.tm_sec, alarm->time.tm_mday,
308 alarm->time.tm_mon, alarm->time.tm_year);
309
310 return 0;
311}
312
313
314static int
315pm8058_rtc0_alarm_irq_enable(struct device *dev, unsigned int enable)
316{
317 int rc;
318 struct pm8058_rtc *rtc_dd = dev_get_drvdata(dev);
319 u8 reg;
320
321 reg = rtc_dd->rtc_ctrl_reg;
322 reg = (enable) ? (reg | PM8058_RTC_ALARM_ENABLE) :
323 (reg & ~PM8058_RTC_ALARM_ENABLE);
324
325 rc = pm8058_write(rtc_dd->pm_chip, PM8058_RTC_CTRL, &reg, 1);
326 if (rc < 0) {
327 pr_err("%s: PM8058 write failed\n", __func__);
328 return rc;
329 }
330
331 rtc_dd->rtc_ctrl_reg = reg;
332
333 return rc;
334}
335
336static struct rtc_class_ops pm8058_rtc0_ops = {
337 .read_time = pm8058_rtc0_read_time,
338 .set_alarm = pm8058_rtc0_set_alarm,
339 .read_alarm = pm8058_rtc0_read_alarm,
340 .alarm_irq_enable = pm8058_rtc0_alarm_irq_enable,
341};
342
343static irqreturn_t pm8058_alarm_trigger(int irq, void *dev_id)
344{
345 u8 reg;
346 int rc;
347 unsigned long events = 0;
348 struct pm8058_rtc *rtc_dd = dev_id;
349
350 events = RTC_IRQF | RTC_AF;
351 rtc_update_irq(rtc_dd->rtc0, 1, events);
352
353 pr_debug("%s: Alarm Triggered !!\n", __func__);
354
355 /* Clear the alarm enable bit */
356 reg = rtc_dd->rtc_ctrl_reg;
357
358 reg &= ~PM8058_RTC_ALARM_ENABLE;
359 rc = pm8058_write(rtc_dd->pm_chip, PM8058_RTC_CTRL,
360 &reg, 1);
361 if (rc < 0) {
362 pr_err("%s: PM8058 write failed\n", __func__);
363 goto rtc_alarm_handled;
364 }
365
366 rtc_dd->rtc_ctrl_reg = reg;
367
368 /* Clear RTC alarm register */
369 rc = pm8058_read(rtc_dd->pm_chip, PM8058_RTC_ALARM_CTRL, &reg, 1);
370 if (rc < 0) {
371 pr_err("%s: PM8058 read failed\n", __func__);
372 goto rtc_alarm_handled;
373 }
374
375 reg &= ~PM8058_RTC_ALARM_CLEAR;
376 rc = pm8058_write(rtc_dd->pm_chip, PM8058_RTC_ALARM_CTRL, &reg, 1);
377 if (rc < 0) {
378 pr_err("%s: PM8058 write failed\n", __func__);
379 goto rtc_alarm_handled;
380 }
381
382rtc_alarm_handled:
383 return IRQ_HANDLED;
384}
385
386static int __devinit pm8058_rtc_probe(struct platform_device *pdev)
387{
388 int rc;
389 u8 reg, reg_alarm;
390 struct pm8058_rtc *rtc_dd;
391 struct pm8058_chip *pm_chip;
392
393 pm_chip = platform_get_drvdata(pdev);
394 if (pm_chip == NULL) {
395 pr_err("%s: Invalid driver information\n", __func__);
396 return -ENXIO;
397 }
398
399 rtc_dd = kzalloc(sizeof(*rtc_dd), GFP_KERNEL);
400 if (rtc_dd == NULL) {
401 pr_err("%s: Unable to allocate memory\n", __func__);
402 return -ENOMEM;
403 }
404
405 /* Enable runtime PM ops, start in ACTIVE mode */
406 rc = pm_runtime_set_active(&pdev->dev);
407 if (rc < 0)
408 dev_dbg(&pdev->dev, "unable to set runtime pm state\n");
409 pm_runtime_enable(&pdev->dev);
410
411 rtc_dd->rtc_irq = platform_get_irq(pdev, 0);
412 rtc_dd->rtc_alarm_irq = platform_get_irq(pdev, 1);
413 if (!rtc_dd->rtc_alarm_irq || !rtc_dd->rtc_irq) {
414 pr_err("%s: RTC Alarm IRQ absent\n", __func__);
415 rc = -ENXIO;
416 goto fail_rtc_enable;
417 }
418
419 rtc_dd->pm_chip = pm_chip;
420
421 rc = pm8058_read(pm_chip, PM8058_RTC_CTRL, &reg, 1);
422 if (rc < 0) {
423 pr_err("%s: PM8058 read failed\n", __func__);
424 goto fail_rtc_enable;
425 }
426
Ashay Jaiswal4d1ab552011-07-15 11:30:49 +0530427 /* Enable RTC, ABORT enable and disable alarm */
428 reg |= ((PM8058_RTC_ENABLE | PM8058_RTC_ABORT_ENABLE) &
429 ~PM8058_RTC_ALARM_ENABLE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700430
Ashay Jaiswal4d1ab552011-07-15 11:30:49 +0530431 rc = pm8058_write(pm_chip, PM8058_RTC_CTRL, &reg, 1);
432 if (rc < 0) {
433 pr_err("%s: PM8058 write failed\n", __func__);
434 goto fail_rtc_enable;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700435 }
Ashay Jaiswal4d1ab552011-07-15 11:30:49 +0530436
437 /* Clear RTC alarm control register */
438 rc = pm8058_read(rtc_dd->pm_chip, PM8058_RTC_ALARM_CTRL,
439 &reg_alarm, 1);
440 if (rc < 0) {
441 pr_err("%s: PM8058 read failed\n", __func__);
442 goto fail_rtc_enable;
443 }
444
445 reg_alarm &= ~PM8058_RTC_ALARM_CLEAR;
446 rc = pm8058_write(rtc_dd->pm_chip, PM8058_RTC_ALARM_CTRL,
447 &reg_alarm, 1);
448 if (rc < 0) {
449 pr_err("%s: PM8058 write failed\n", __func__);
450 goto fail_rtc_enable;
451 }
452
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700453 rtc_dd->rtc_ctrl_reg = reg;
454
455#ifdef CONFIG_RTC_PM8058_WRITE_ENABLE
Ashay Jaiswal4d1ab552011-07-15 11:30:49 +0530456 pm8058_rtc0_ops.set_time = pm8058_rtc0_set_time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700457#endif
458
459 /* Register the RTC device */
460 rtc_dd->rtc0 = rtc_device_register("pm8058_rtc0", &pdev->dev,
461 &pm8058_rtc0_ops, THIS_MODULE);
462 if (IS_ERR(rtc_dd->rtc0)) {
463 pr_err("%s: RTC device registration failed (%ld)\n",
464 __func__, PTR_ERR(rtc_dd->rtc0));
465 rc = PTR_ERR(rtc_dd->rtc0);
466 goto fail_rtc_enable;
467 }
468
469 platform_set_drvdata(pdev, rtc_dd);
470
471 /* Request the alarm IRQ */
472 rc = request_threaded_irq(rtc_dd->rtc_alarm_irq, NULL,
473 pm8058_alarm_trigger, IRQF_TRIGGER_RISING,
474 "pm8058_rtc_alarm", rtc_dd);
475 if (rc < 0) {
476 pr_err("%s: Request IRQ failed (%d)\n", __func__, rc);
477 goto fail_req_irq;
478 }
479
480 device_init_wakeup(&pdev->dev, 1);
481
482 pr_debug("%s: Probe success !!\n", __func__);
483
484 return 0;
485
486fail_req_irq:
487 rtc_device_unregister(rtc_dd->rtc0);
488fail_rtc_enable:
489 pm_runtime_set_suspended(&pdev->dev);
490 pm_runtime_disable(&pdev->dev);
491 kfree(rtc_dd);
492 return rc;
493}
494
495#ifdef CONFIG_PM
496static int pm8058_rtc_resume(struct device *dev)
497{
498 struct pm8058_rtc *rtc_dd = dev_get_drvdata(dev);
499
500 if (device_may_wakeup(dev))
501 disable_irq_wake(rtc_dd->rtc_alarm_irq);
502
503 return 0;
504}
505
506static int pm8058_rtc_suspend(struct device *dev)
507{
508 struct pm8058_rtc *rtc_dd = dev_get_drvdata(dev);
509
510 if (device_may_wakeup(dev))
511 enable_irq_wake(rtc_dd->rtc_alarm_irq);
512
513 return 0;
514}
515
516static struct dev_pm_ops pm8058_rtc_pm_ops = {
517 .suspend = pm8058_rtc_suspend,
518 .resume = pm8058_rtc_resume,
519};
520#endif
521
522static int __devexit pm8058_rtc_remove(struct platform_device *pdev)
523{
524 struct pm8058_rtc *rtc_dd = platform_get_drvdata(pdev);
525
526 pm_runtime_set_suspended(&pdev->dev);
527 pm_runtime_disable(&pdev->dev);
528
529 device_init_wakeup(&pdev->dev, 0);
530 free_irq(rtc_dd->rtc_alarm_irq, rtc_dd);
531 rtc_device_unregister(rtc_dd->rtc0);
532 kfree(rtc_dd);
533
534 return 0;
535}
536
Ashay Jaiswal4d1ab552011-07-15 11:30:49 +0530537static void pm8058_rtc_shutdown(struct platform_device *pdev)
538{
539 u8 reg;
540 int rc, i;
541 bool rtc_alarm_powerup = false;
542 struct pm8058_rtc *rtc_dd = platform_get_drvdata(pdev);
543 struct pm8058_rtc_platform_data *pdata = pdev->dev.platform_data;
544
545 if (pdata != NULL)
546 rtc_alarm_powerup = pdata->rtc_alarm_powerup;
547
548 if (!rtc_alarm_powerup) {
549
550 dev_dbg(&pdev->dev, "Disabling alarm interrupts\n");
551
552 /* Disable RTC alarms */
553 reg = rtc_dd->rtc_ctrl_reg;
554 reg &= ~PM8058_RTC_ALARM_ENABLE;
555 rc = pm8058_write(rtc_dd->pm_chip, PM8058_RTC_CTRL, &reg, 1);
556 if (rc < 0) {
557 pr_err("%s: PM8058 write failed\n", __func__);
558 return;
559 }
560
561 /* Clear Alarm register */
562 reg = 0x0;
563 for (i = 0; i < 4; i++) {
564 rc = pm8058_write(rtc_dd->pm_chip,
565 PM8058_RTC_ALARM_BASE + i, &reg, 1);
566 if (rc < 0) {
567 pr_err("%s: PM8058 write failed\n", __func__);
568 return;
569 }
570 }
571
572 }
573}
574
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700575static struct platform_driver pm8058_rtc_driver = {
576 .probe = pm8058_rtc_probe,
577 .remove = __devexit_p(pm8058_rtc_remove),
Ashay Jaiswal4d1ab552011-07-15 11:30:49 +0530578 .shutdown = pm8058_rtc_shutdown,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700579 .driver = {
580 .name = "pm8058-rtc",
581 .owner = THIS_MODULE,
582#ifdef CONFIG_PM
583 .pm = &pm8058_rtc_pm_ops,
584#endif
585 },
586};
587
588static int __init pm8058_rtc_init(void)
589{
590 return platform_driver_register(&pm8058_rtc_driver);
591}
592
593static void __exit pm8058_rtc_exit(void)
594{
595 platform_driver_unregister(&pm8058_rtc_driver);
596}
597
598module_init(pm8058_rtc_init);
599module_exit(pm8058_rtc_exit);
600
601MODULE_ALIAS("platform:pm8058-rtc");
602MODULE_DESCRIPTION("PMIC8058 RTC driver");
603MODULE_LICENSE("GPL v2");