blob: 0bdb89eb8b1aa4337ab6e3280ecc3c6a29b6cce2 [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/pm.h>
17#include <linux/slab.h>
18#include<linux/spinlock.h>
19
20#include <linux/mfd/pm8xxx/core.h>
21#include <linux/mfd/pm8xxx/rtc.h>
22
23
24/* RTC Register offsets from RTC CTRL REG */
25#define PM8XXX_ALARM_CTRL_OFFSET 0x01
26#define PM8XXX_RTC_WRITE_OFFSET 0x02
27#define PM8XXX_RTC_READ_OFFSET 0x06
28#define PM8XXX_ALARM_RW_OFFSET 0x0A
29
30/* RTC_CTRL register bit fields */
31#define PM8xxx_RTC_ENABLE BIT(7)
32#define PM8xxx_RTC_ALARM_ENABLE BIT(1)
33#define PM8xxx_RTC_ALARM_CLEAR BIT(0)
34
35#define NUM_8_BIT_RTC_REGS 0x4
36
37/**
38 * struct pm8xxx_rtc - rtc driver internal structure
39 * @rtc: rtc device for this driver
40 * @rtc_alarm_irq: rtc alarm irq number
41 */
42struct pm8xxx_rtc {
43 struct rtc_device *rtc;
44 int rtc_alarm_irq;
45 int rtc_base;
46 int rtc_read_base;
47 int rtc_write_base;
48 int alarm_rw_base;
49 u8 ctrl_reg;
50 struct device *rtc_dev;
51 spinlock_t ctrl_reg_lock;
52};
53
54/*
55 * The RTC registers need to be read/written one byte at a time. This is a
56 * hardware limitation.
57 */
58
59static int pm8xxx_read_wrapper(struct pm8xxx_rtc *rtc_dd, u8 *rtc_val,
60 int base, int count)
61{
62 int i, rc;
63 struct device *parent = rtc_dd->rtc_dev->parent;
64
65 for (i = 0; i < count; i++) {
66 rc = pm8xxx_readb(parent, base + i, &rtc_val[i]);
67 if (rc < 0) {
68 dev_err(rtc_dd->rtc_dev, "PM8xxx read failed\n");
69 return rc;
70 }
71 }
72
73 return 0;
74}
75
76static int pm8xxx_write_wrapper(struct pm8xxx_rtc *rtc_dd, u8 *rtc_val,
77 int base, int count)
78{
79 int i, rc;
80 struct device *parent = rtc_dd->rtc_dev->parent;
81
82 for (i = 0; i < count; i++) {
83 rc = pm8xxx_writeb(parent, base + i, rtc_val[i]);
84 if (rc < 0) {
85 dev_err(rtc_dd->rtc_dev, "PM8xxx write failed\n");
86 return rc;
87 }
88 }
89
90 return 0;
91}
92
93
94/*
95 * Steps to write the RTC registers.
96 * 1. Disable alarm if enabled.
97 * 2. Write 0x00 to LSB.
98 * 3. Write Byte[1], Byte[2], Byte[3] then Byte[0].
99 * 4. Enable alarm if disabled in step 1.
100 */
101static int
102pm8xxx_rtc_set_time(struct device *dev, struct rtc_time *tm)
103{
104 int rc;
105 unsigned long secs, irq_flags;
106 u8 value[4], reg = 0, alarm_enabled = 0, ctrl_reg;
107 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
108
109 rtc_tm_to_time(tm, &secs);
110
111 value[0] = secs & 0xFF;
112 value[1] = (secs >> 8) & 0xFF;
113 value[2] = (secs >> 16) & 0xFF;
114 value[3] = (secs >> 24) & 0xFF;
115
116 dev_dbg(dev, "Seconds value to be written to RTC = %lu\n", secs);
117
118 spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags);
119 ctrl_reg = rtc_dd->ctrl_reg;
120
121 if (ctrl_reg & PM8xxx_RTC_ALARM_ENABLE) {
122 alarm_enabled = 1;
123 ctrl_reg &= ~PM8xxx_RTC_ALARM_ENABLE;
124 rc = pm8xxx_write_wrapper(rtc_dd, &ctrl_reg, rtc_dd->rtc_base,
125 1);
126 if (rc < 0) {
127 dev_err(dev, "PM8xxx write failed\n");
128 goto rtc_rw_fail;
129 }
130 } else
131 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
132
133 /* Write Byte[1], Byte[2], Byte[3], Byte[0] */
134 /* Write 0 to Byte[0] */
135 reg = 0;
136 rc = pm8xxx_write_wrapper(rtc_dd, &reg, rtc_dd->rtc_write_base, 1);
137 if (rc < 0) {
138 dev_err(dev, "PM8xxx write failed\n");
139 goto rtc_rw_fail;
140 }
141
142 /* Write Byte[1], Byte[2], Byte[3] */
143 rc = pm8xxx_write_wrapper(rtc_dd, value + 1,
144 rtc_dd->rtc_write_base + 1, 3);
145 if (rc < 0) {
146 dev_err(dev, "Write to RTC registers failed\n");
147 goto rtc_rw_fail;
148 }
149
150 /* Write Byte[0] */
151 rc = pm8xxx_write_wrapper(rtc_dd, value, rtc_dd->rtc_write_base, 1);
152 if (rc < 0) {
153 dev_err(dev, "Write to RTC register failed\n");
154 goto rtc_rw_fail;
155 }
156
157 if (alarm_enabled) {
158 ctrl_reg |= PM8xxx_RTC_ALARM_ENABLE;
159 rc = pm8xxx_write_wrapper(rtc_dd, &ctrl_reg, rtc_dd->rtc_base,
160 1);
161 if (rc < 0) {
162 dev_err(dev, "PM8xxx write failed\n");
163 goto rtc_rw_fail;
164 }
165 }
166
167 rtc_dd->ctrl_reg = ctrl_reg;
168
169rtc_rw_fail:
170 if (alarm_enabled)
171 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
172
173 return rc;
174}
175
176static int
177pm8xxx_rtc_read_time(struct device *dev, struct rtc_time *tm)
178{
179 int rc;
180 u8 value[4], reg;
181 unsigned long secs;
182 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
183
184 rc = pm8xxx_read_wrapper(rtc_dd, value, rtc_dd->rtc_read_base,
185 NUM_8_BIT_RTC_REGS);
186 if (rc < 0) {
187 dev_err(dev, "RTC time read failed\n");
188 return rc;
189 }
190
191 /*
192 * Read the LSB again and check if there has been a carry over.
193 * If there is, redo the read operation.
194 */
195 rc = pm8xxx_read_wrapper(rtc_dd, &reg, rtc_dd->rtc_read_base, 1);
196 if (rc < 0) {
197 dev_err(dev, "PM8xxx read failed\n");
198 return rc;
199 }
200
201 if (unlikely(reg < value[0])) {
202 rc = pm8xxx_read_wrapper(rtc_dd, value,
203 rtc_dd->rtc_read_base, NUM_8_BIT_RTC_REGS);
204 if (rc < 0) {
205 dev_err(dev, "RTC time read failed\n");
206 return rc;
207 }
208 }
209
210 secs = value[0] | (value[1] << 8) | (value[2] << 16) \
211 | (value[3] << 24);
212
213 rtc_time_to_tm(secs, tm);
214
215 rc = rtc_valid_tm(tm);
216 if (rc < 0) {
217 dev_err(dev, "Invalid time read from PM8xxx\n");
218 return rc;
219 }
220
221 dev_dbg(dev, "secs = %lu, h:m:s == %d:%d:%d, d/m/y = %d/%d/%d\n",
222 secs, tm->tm_hour, tm->tm_min, tm->tm_sec,
223 tm->tm_mday, tm->tm_mon, tm->tm_year);
224
225 return 0;
226}
227
228static int
229pm8xxx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
230{
231 int rc;
232 u8 value[4], ctrl_reg;
233 unsigned long secs, secs_rtc, irq_flags;
234 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
235 struct rtc_time rtc_tm;
236
237 rtc_tm_to_time(&alarm->time, &secs);
238
239 /*
240 * Read the current RTC time and verify if the alarm time is in the
241 * past. If yes, return invalid.
242 */
243 rc = pm8xxx_rtc_read_time(dev, &rtc_tm);
244 if (rc < 0) {
245 dev_err(dev, "Unamble to read RTC time\n");
246 return -EINVAL;
247 }
248
249 rtc_tm_to_time(&rtc_tm, &secs_rtc);
250 if (secs < secs_rtc) {
251 dev_err(dev, "Trying to set alarm in the past\n");
252 return -EINVAL;
253 }
254
255 value[0] = secs & 0xFF;
256 value[1] = (secs >> 8) & 0xFF;
257 value[2] = (secs >> 16) & 0xFF;
258 value[3] = (secs >> 24) & 0xFF;
259
260 spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags);
261
262 rc = pm8xxx_write_wrapper(rtc_dd, value, rtc_dd->alarm_rw_base,
263 NUM_8_BIT_RTC_REGS);
264 if (rc < 0) {
265 dev_err(dev, "Write to RTC ALARM registers failed\n");
266 goto rtc_rw_fail;
267 }
268
269 ctrl_reg = rtc_dd->ctrl_reg;
270 ctrl_reg = (alarm->enabled) ? (ctrl_reg | PM8xxx_RTC_ALARM_ENABLE) :
271 (ctrl_reg & ~PM8xxx_RTC_ALARM_ENABLE);
272
273 rc = pm8xxx_write_wrapper(rtc_dd, &ctrl_reg, rtc_dd->rtc_base, 1);
274 if (rc < 0) {
275 dev_err(dev, "PM8xxx write failed\n");
276 goto rtc_rw_fail;
277 }
278
279 rtc_dd->ctrl_reg = ctrl_reg;
280
281 dev_dbg(dev, "Alarm Set for h:r:s=%d:%d:%d, d/m/y=%d/%d/%d\n",
282 alarm->time.tm_hour, alarm->time.tm_min,
283 alarm->time.tm_sec, alarm->time.tm_mday,
284 alarm->time.tm_mon, alarm->time.tm_year);
285rtc_rw_fail:
286 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
287 return rc;
288}
289
290static int
291pm8xxx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
292{
293 int rc;
294 u8 value[4];
295 unsigned long secs;
296 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
297
298 rc = pm8xxx_read_wrapper(rtc_dd, value, rtc_dd->alarm_rw_base,
299 NUM_8_BIT_RTC_REGS);
300 if (rc < 0) {
301 dev_err(dev, "RTC alarm time read failed\n");
302 return rc;
303 }
304
305 secs = value[0] | (value[1] << 8) | (value[2] << 16) | \
306 (value[3] << 24);
307
308 rtc_time_to_tm(secs, &alarm->time);
309
310 rc = rtc_valid_tm(&alarm->time);
311 if (rc < 0) {
312 dev_err(dev, "Invalid time read from PM8xxx\n");
313 return rc;
314 }
315
316 dev_dbg(dev, "Alarm set for - h:r:s=%d:%d:%d, d/m/y=%d/%d/%d\n",
317 alarm->time.tm_hour, alarm->time.tm_min,
318 alarm->time.tm_sec, alarm->time.tm_mday,
319 alarm->time.tm_mon, alarm->time.tm_year);
320
321 return 0;
322}
323
324
325static int
326pm8xxx_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
327{
328 int rc;
329 unsigned long irq_flags;
330 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
331 u8 ctrl_reg;
332
333 spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags);
334 ctrl_reg = rtc_dd->ctrl_reg;
335 ctrl_reg = (enabled) ? (ctrl_reg | PM8xxx_RTC_ALARM_ENABLE) :
336 (ctrl_reg & ~PM8xxx_RTC_ALARM_ENABLE);
337
338 rc = pm8xxx_write_wrapper(rtc_dd, &ctrl_reg, rtc_dd->rtc_base, 1);
339 if (rc < 0) {
340 dev_err(dev, "PM8xxx write failed\n");
341 goto rtc_rw_fail;
342 }
343
344 rtc_dd->ctrl_reg = ctrl_reg;
345
346rtc_rw_fail:
347 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
348 return rc;
349}
350
351static struct rtc_class_ops pm8xxx_rtc_ops = {
352 .read_time = pm8xxx_rtc_read_time,
353 .set_alarm = pm8xxx_rtc_set_alarm,
354 .read_alarm = pm8xxx_rtc_read_alarm,
355 .alarm_irq_enable = pm8xxx_rtc_alarm_irq_enable,
356};
357
358static irqreturn_t pm8xxx_alarm_trigger(int irq, void *dev_id)
359{
360 struct pm8xxx_rtc *rtc_dd = dev_id;
361 u8 ctrl_reg;
362 int rc;
363 unsigned long irq_flags;
364
365 rtc_update_irq(rtc_dd->rtc, 1, RTC_IRQF | RTC_AF);
366
367 spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags);
368
369 /* Clear the alarm enable bit */
370 ctrl_reg = rtc_dd->ctrl_reg;
371 ctrl_reg &= ~PM8xxx_RTC_ALARM_ENABLE;
372
373 rc = pm8xxx_write_wrapper(rtc_dd, &ctrl_reg, rtc_dd->rtc_base, 1);
374 if (rc < 0) {
375 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
376 dev_err(rtc_dd->rtc_dev, "PM8xxx write failed!\n");
377 goto rtc_alarm_handled;
378 }
379
380 rtc_dd->ctrl_reg = ctrl_reg;
381 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
382
383 /* Clear RTC alarm register */
384 rc = pm8xxx_read_wrapper(rtc_dd, &ctrl_reg, rtc_dd->rtc_base +
385 PM8XXX_ALARM_CTRL_OFFSET, 1);
386 if (rc < 0) {
387 dev_err(rtc_dd->rtc_dev, "PM8xxx write failed!\n");
388 goto rtc_alarm_handled;
389 }
390
391 ctrl_reg &= ~PM8xxx_RTC_ALARM_CLEAR;
392 rc = pm8xxx_write_wrapper(rtc_dd, &ctrl_reg, rtc_dd->rtc_base +
393 PM8XXX_ALARM_CTRL_OFFSET, 1);
394 if (rc < 0)
395 dev_err(rtc_dd->rtc_dev, "PM8xxx write failed!\n");
396
397rtc_alarm_handled:
398 return IRQ_HANDLED;
399}
400
401static int __devinit pm8xxx_rtc_probe(struct platform_device *pdev)
402{
403 int rc;
404 u8 ctrl_reg;
405 bool rtc_write_enable = false;
406 struct pm8xxx_rtc *rtc_dd;
407 struct resource *rtc_resource;
408 const struct pm8xxx_rtc_platform_data *pdata =
409 pdev->dev.platform_data;
410
411 if (pdata != NULL)
412 rtc_write_enable = pdata->rtc_write_enable;
413
414 rtc_dd = kzalloc(sizeof(*rtc_dd), GFP_KERNEL);
415 if (rtc_dd == NULL) {
416 dev_err(&pdev->dev, "Unable to allocate memory!\n");
417 return -ENOMEM;
418 }
419
420 /* Initialise spinlock to protect RTC cntrol register */
421 spin_lock_init(&rtc_dd->ctrl_reg_lock);
422
423 rtc_dd->rtc_alarm_irq = platform_get_irq(pdev, 0);
424 if (rtc_dd->rtc_alarm_irq < 0) {
425 dev_err(&pdev->dev, "Alarm IRQ resource absent!\n");
426 rc = -ENXIO;
427 goto fail_rtc_enable;
428 }
429
430 rtc_resource = platform_get_resource_byname(pdev, IORESOURCE_IO,
431 "pmic_rtc_base");
432 if (!(rtc_resource && rtc_resource->start)) {
433 dev_err(&pdev->dev, "RTC IO resource absent!\n");
434 rc = -ENXIO;
435 goto fail_rtc_enable;
436 }
437
438 rtc_dd->rtc_base = rtc_resource->start;
439
440 /* Setup RTC register addresses */
441 rtc_dd->rtc_write_base = rtc_dd->rtc_base + PM8XXX_RTC_WRITE_OFFSET;
442 rtc_dd->rtc_read_base = rtc_dd->rtc_base + PM8XXX_RTC_READ_OFFSET;
443 rtc_dd->alarm_rw_base = rtc_dd->rtc_base + PM8XXX_ALARM_RW_OFFSET;
444
445 rtc_dd->rtc_dev = &(pdev->dev);
446
447 /* Check if the RTC is on, else turn it on */
448 rc = pm8xxx_read_wrapper(rtc_dd, &ctrl_reg, rtc_dd->rtc_base, 1);
449 if (rc < 0) {
450 dev_err(&pdev->dev, "PM8xxx read failed!\n");
451 goto fail_rtc_enable;
452 }
453
454 if (!(ctrl_reg & PM8xxx_RTC_ENABLE)) {
455 ctrl_reg |= PM8xxx_RTC_ENABLE;
456 rc = pm8xxx_write_wrapper(rtc_dd, &ctrl_reg, rtc_dd->rtc_base,
457 1);
458 if (rc < 0) {
459 dev_err(&pdev->dev, "PM8xxx write failed!\n");
460 goto fail_rtc_enable;
461 }
462 }
463
464 rtc_dd->ctrl_reg = ctrl_reg;
465 if (rtc_write_enable == true)
466 pm8xxx_rtc_ops.set_time = pm8xxx_rtc_set_time;
467
468 platform_set_drvdata(pdev, rtc_dd);
469
470 /* Register the RTC device */
471 rtc_dd->rtc = rtc_device_register("pm8xxx_rtc", &pdev->dev,
472 &pm8xxx_rtc_ops, THIS_MODULE);
473 if (IS_ERR(rtc_dd->rtc)) {
474 dev_err(&pdev->dev, "%s: RTC registration failed (%ld)\n",
475 __func__, PTR_ERR(rtc_dd->rtc));
476 rc = PTR_ERR(rtc_dd->rtc);
477 goto fail_rtc_enable;
478 }
479
480 /* Request the alarm IRQ */
481 rc = request_any_context_irq(rtc_dd->rtc_alarm_irq,
482 pm8xxx_alarm_trigger, IRQF_TRIGGER_RISING,
483 "pm8xxx_rtc_alarm", rtc_dd);
484 if (rc < 0) {
485 dev_err(&pdev->dev, "Request IRQ failed (%d)\n", rc);
486 goto fail_req_irq;
487 }
488
489 device_init_wakeup(&pdev->dev, 1);
490
491 dev_dbg(&pdev->dev, "Probe success !!\n");
492
493 return 0;
494
495fail_req_irq:
496 rtc_device_unregister(rtc_dd->rtc);
497fail_rtc_enable:
498 platform_set_drvdata(pdev, NULL);
499 kfree(rtc_dd);
500 return rc;
501}
502
503#ifdef CONFIG_PM
504static int pm8xxx_rtc_resume(struct device *dev)
505{
506 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
507
508 if (device_may_wakeup(dev))
509 disable_irq_wake(rtc_dd->rtc_alarm_irq);
510
511 return 0;
512}
513
514static int pm8xxx_rtc_suspend(struct device *dev)
515{
516 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
517
518 if (device_may_wakeup(dev))
519 enable_irq_wake(rtc_dd->rtc_alarm_irq);
520
521 return 0;
522}
523
524static const struct dev_pm_ops pm8xxx_rtc_pm_ops = {
525 .suspend = pm8xxx_rtc_suspend,
526 .resume = pm8xxx_rtc_resume,
527};
528#endif
529static int __devexit pm8xxx_rtc_remove(struct platform_device *pdev)
530{
531 struct pm8xxx_rtc *rtc_dd = platform_get_drvdata(pdev);
532
533 device_init_wakeup(&pdev->dev, 0);
534 free_irq(rtc_dd->rtc_alarm_irq, rtc_dd);
535 rtc_device_unregister(rtc_dd->rtc);
536 platform_set_drvdata(pdev, NULL);
537 kfree(rtc_dd);
538
539 return 0;
540}
541
542static struct platform_driver pm8xxx_rtc_driver = {
543 .probe = pm8xxx_rtc_probe,
544 .remove = __devexit_p(pm8xxx_rtc_remove),
545 .driver = {
546 .name = PM8XXX_RTC_DEV_NAME,
547 .owner = THIS_MODULE,
548#ifdef CONFIG_PM
549 .pm = &pm8xxx_rtc_pm_ops,
550#endif
551 },
552};
553
554static int __init pm8xxx_rtc_init(void)
555{
556 return platform_driver_register(&pm8xxx_rtc_driver);
557}
558module_init(pm8xxx_rtc_init);
559
560static void __exit pm8xxx_rtc_exit(void)
561{
562 platform_driver_unregister(&pm8xxx_rtc_driver);
563}
564module_exit(pm8xxx_rtc_exit);
565
566MODULE_ALIAS("platform:rtc-pm8xxx");
567MODULE_DESCRIPTION("PMIC8xxx RTC driver");
568MODULE_LICENSE("GPL v2");
569MODULE_AUTHOR("Anirudh Ghayal <aghayal@codeaurora.org>");