1 /* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
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.
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.
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/rtc.h>
16 #include <linux/platform_device.h>
18 #include <linux/regmap.h>
19 #include <linux/slab.h>
20 #include <linux/spinlock.h>
22 /* RTC Register offsets from RTC CTRL REG */
23 #define PM8XXX_ALARM_CTRL_OFFSET 0x01
24 #define PM8XXX_RTC_WRITE_OFFSET 0x02
25 #define PM8XXX_RTC_READ_OFFSET 0x06
26 #define PM8XXX_ALARM_RW_OFFSET 0x0A
28 /* RTC_CTRL register bit fields */
29 #define PM8xxx_RTC_ENABLE BIT(7)
30 #define PM8xxx_RTC_ALARM_ENABLE BIT(1)
31 #define PM8xxx_RTC_ALARM_CLEAR BIT(0)
33 #define NUM_8_BIT_RTC_REGS 0x4
36 * struct pm8xxx_rtc - rtc driver internal structure
37 * @rtc: rtc device for this driver.
38 * @regmap: regmap used to access RTC registers
39 * @allow_set_time: indicates whether writing to the RTC is allowed
40 * @rtc_alarm_irq: rtc alarm irq number.
41 * @rtc_base: address of rtc control register.
42 * @rtc_read_base: base address of read registers.
43 * @rtc_write_base: base address of write registers.
44 * @alarm_rw_base: base address of alarm registers.
45 * @ctrl_reg: rtc control register.
46 * @rtc_dev: device structure.
47 * @ctrl_reg_lock: spinlock protecting access to ctrl_reg.
50 struct rtc_device
*rtc
;
51 struct regmap
*regmap
;
59 struct device
*rtc_dev
;
60 spinlock_t ctrl_reg_lock
;
64 * Steps to write the RTC registers.
65 * 1. Disable alarm if enabled.
66 * 2. Write 0x00 to LSB.
67 * 3. Write Byte[1], Byte[2], Byte[3] then Byte[0].
68 * 4. Enable alarm if disabled in step 1.
70 static int pm8xxx_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
73 unsigned long secs
, irq_flags
;
74 u8 value
[NUM_8_BIT_RTC_REGS
], alarm_enabled
= 0, ctrl_reg
;
75 struct pm8xxx_rtc
*rtc_dd
= dev_get_drvdata(dev
);
77 if (!rtc_dd
->allow_set_time
)
80 rtc_tm_to_time(tm
, &secs
);
82 for (i
= 0; i
< NUM_8_BIT_RTC_REGS
; i
++) {
83 value
[i
] = secs
& 0xFF;
87 dev_dbg(dev
, "Seconds value to be written to RTC = %lu\n", secs
);
89 spin_lock_irqsave(&rtc_dd
->ctrl_reg_lock
, irq_flags
);
90 ctrl_reg
= rtc_dd
->ctrl_reg
;
92 if (ctrl_reg
& PM8xxx_RTC_ALARM_ENABLE
) {
94 ctrl_reg
&= ~PM8xxx_RTC_ALARM_ENABLE
;
95 rc
= regmap_write(rtc_dd
->regmap
, rtc_dd
->rtc_base
, ctrl_reg
);
97 dev_err(dev
, "Write to RTC control register failed\n");
100 rtc_dd
->ctrl_reg
= ctrl_reg
;
102 spin_unlock_irqrestore(&rtc_dd
->ctrl_reg_lock
, irq_flags
);
105 /* Write 0 to Byte[0] */
106 rc
= regmap_write(rtc_dd
->regmap
, rtc_dd
->rtc_write_base
, 0);
108 dev_err(dev
, "Write to RTC write data register failed\n");
112 /* Write Byte[1], Byte[2], Byte[3] */
113 rc
= regmap_bulk_write(rtc_dd
->regmap
, rtc_dd
->rtc_write_base
+ 1,
114 &value
[1], sizeof(value
) - 1);
116 dev_err(dev
, "Write to RTC write data register failed\n");
121 rc
= regmap_write(rtc_dd
->regmap
, rtc_dd
->rtc_write_base
, value
[0]);
123 dev_err(dev
, "Write to RTC write data register failed\n");
128 ctrl_reg
|= PM8xxx_RTC_ALARM_ENABLE
;
129 rc
= regmap_write(rtc_dd
->regmap
, rtc_dd
->rtc_base
, ctrl_reg
);
131 dev_err(dev
, "Write to RTC control register failed\n");
134 rtc_dd
->ctrl_reg
= ctrl_reg
;
139 spin_unlock_irqrestore(&rtc_dd
->ctrl_reg_lock
, irq_flags
);
144 static int pm8xxx_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
147 u8 value
[NUM_8_BIT_RTC_REGS
];
150 struct pm8xxx_rtc
*rtc_dd
= dev_get_drvdata(dev
);
152 rc
= regmap_bulk_read(rtc_dd
->regmap
, rtc_dd
->rtc_read_base
,
153 value
, sizeof(value
));
155 dev_err(dev
, "RTC read data register failed\n");
160 * Read the LSB again and check if there has been a carry over.
161 * If there is, redo the read operation.
163 rc
= regmap_read(rtc_dd
->regmap
, rtc_dd
->rtc_read_base
, ®
);
165 dev_err(dev
, "RTC read data register failed\n");
169 if (unlikely(reg
< value
[0])) {
170 rc
= regmap_bulk_read(rtc_dd
->regmap
, rtc_dd
->rtc_read_base
,
171 value
, sizeof(value
));
173 dev_err(dev
, "RTC read data register failed\n");
178 secs
= value
[0] | (value
[1] << 8) | (value
[2] << 16) | (value
[3] << 24);
180 rtc_time_to_tm(secs
, tm
);
182 rc
= rtc_valid_tm(tm
);
184 dev_err(dev
, "Invalid time read from RTC\n");
188 dev_dbg(dev
, "secs = %lu, h:m:s == %d:%d:%d, d/m/y = %d/%d/%d\n",
189 secs
, tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
190 tm
->tm_mday
, tm
->tm_mon
, tm
->tm_year
);
195 static int pm8xxx_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
198 u8 value
[NUM_8_BIT_RTC_REGS
], ctrl_reg
;
199 unsigned long secs
, irq_flags
;
200 struct pm8xxx_rtc
*rtc_dd
= dev_get_drvdata(dev
);
202 rtc_tm_to_time(&alarm
->time
, &secs
);
204 for (i
= 0; i
< NUM_8_BIT_RTC_REGS
; i
++) {
205 value
[i
] = secs
& 0xFF;
209 spin_lock_irqsave(&rtc_dd
->ctrl_reg_lock
, irq_flags
);
211 rc
= regmap_bulk_write(rtc_dd
->regmap
, rtc_dd
->alarm_rw_base
, value
,
214 dev_err(dev
, "Write to RTC ALARM register failed\n");
218 ctrl_reg
= rtc_dd
->ctrl_reg
;
221 ctrl_reg
|= PM8xxx_RTC_ALARM_ENABLE
;
223 ctrl_reg
&= ~PM8xxx_RTC_ALARM_ENABLE
;
225 rc
= regmap_write(rtc_dd
->regmap
, rtc_dd
->rtc_base
, ctrl_reg
);
227 dev_err(dev
, "Write to RTC control register failed\n");
231 rtc_dd
->ctrl_reg
= ctrl_reg
;
233 dev_dbg(dev
, "Alarm Set for h:r:s=%d:%d:%d, d/m/y=%d/%d/%d\n",
234 alarm
->time
.tm_hour
, alarm
->time
.tm_min
,
235 alarm
->time
.tm_sec
, alarm
->time
.tm_mday
,
236 alarm
->time
.tm_mon
, alarm
->time
.tm_year
);
238 spin_unlock_irqrestore(&rtc_dd
->ctrl_reg_lock
, irq_flags
);
242 static int pm8xxx_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
245 u8 value
[NUM_8_BIT_RTC_REGS
];
247 struct pm8xxx_rtc
*rtc_dd
= dev_get_drvdata(dev
);
249 rc
= regmap_bulk_read(rtc_dd
->regmap
, rtc_dd
->alarm_rw_base
, value
,
252 dev_err(dev
, "RTC alarm time read failed\n");
256 secs
= value
[0] | (value
[1] << 8) | (value
[2] << 16) | (value
[3] << 24);
258 rtc_time_to_tm(secs
, &alarm
->time
);
260 rc
= rtc_valid_tm(&alarm
->time
);
262 dev_err(dev
, "Invalid alarm time read from RTC\n");
266 dev_dbg(dev
, "Alarm set for - h:r:s=%d:%d:%d, d/m/y=%d/%d/%d\n",
267 alarm
->time
.tm_hour
, alarm
->time
.tm_min
,
268 alarm
->time
.tm_sec
, alarm
->time
.tm_mday
,
269 alarm
->time
.tm_mon
, alarm
->time
.tm_year
);
274 static int pm8xxx_rtc_alarm_irq_enable(struct device
*dev
, unsigned int enable
)
277 unsigned long irq_flags
;
278 struct pm8xxx_rtc
*rtc_dd
= dev_get_drvdata(dev
);
281 spin_lock_irqsave(&rtc_dd
->ctrl_reg_lock
, irq_flags
);
283 ctrl_reg
= rtc_dd
->ctrl_reg
;
286 ctrl_reg
|= PM8xxx_RTC_ALARM_ENABLE
;
288 ctrl_reg
&= ~PM8xxx_RTC_ALARM_ENABLE
;
290 rc
= regmap_write(rtc_dd
->regmap
, rtc_dd
->rtc_base
, ctrl_reg
);
292 dev_err(dev
, "Write to RTC control register failed\n");
296 rtc_dd
->ctrl_reg
= ctrl_reg
;
299 spin_unlock_irqrestore(&rtc_dd
->ctrl_reg_lock
, irq_flags
);
303 static const struct rtc_class_ops pm8xxx_rtc_ops
= {
304 .read_time
= pm8xxx_rtc_read_time
,
305 .set_time
= pm8xxx_rtc_set_time
,
306 .set_alarm
= pm8xxx_rtc_set_alarm
,
307 .read_alarm
= pm8xxx_rtc_read_alarm
,
308 .alarm_irq_enable
= pm8xxx_rtc_alarm_irq_enable
,
311 static irqreturn_t
pm8xxx_alarm_trigger(int irq
, void *dev_id
)
313 struct pm8xxx_rtc
*rtc_dd
= dev_id
;
314 unsigned int ctrl_reg
;
316 unsigned long irq_flags
;
318 rtc_update_irq(rtc_dd
->rtc
, 1, RTC_IRQF
| RTC_AF
);
320 spin_lock_irqsave(&rtc_dd
->ctrl_reg_lock
, irq_flags
);
322 /* Clear the alarm enable bit */
323 ctrl_reg
= rtc_dd
->ctrl_reg
;
324 ctrl_reg
&= ~PM8xxx_RTC_ALARM_ENABLE
;
326 rc
= regmap_write(rtc_dd
->regmap
, rtc_dd
->rtc_base
, ctrl_reg
);
328 spin_unlock_irqrestore(&rtc_dd
->ctrl_reg_lock
, irq_flags
);
329 dev_err(rtc_dd
->rtc_dev
,
330 "Write to RTC control register failed\n");
331 goto rtc_alarm_handled
;
334 rtc_dd
->ctrl_reg
= ctrl_reg
;
335 spin_unlock_irqrestore(&rtc_dd
->ctrl_reg_lock
, irq_flags
);
337 /* Clear RTC alarm register */
338 rc
= regmap_read(rtc_dd
->regmap
,
339 rtc_dd
->rtc_base
+ PM8XXX_ALARM_CTRL_OFFSET
,
342 dev_err(rtc_dd
->rtc_dev
,
343 "RTC Alarm control register read failed\n");
344 goto rtc_alarm_handled
;
347 ctrl_reg
&= ~PM8xxx_RTC_ALARM_CLEAR
;
348 rc
= regmap_write(rtc_dd
->regmap
,
349 rtc_dd
->rtc_base
+ PM8XXX_ALARM_CTRL_OFFSET
,
352 dev_err(rtc_dd
->rtc_dev
,
353 "Write to RTC Alarm control register failed\n");
360 * Hardcoded RTC bases until IORESOURCE_REG mapping is figured out
362 static const struct of_device_id pm8xxx_id_table
[] = {
363 { .compatible
= "qcom,pm8921-rtc", .data
= (void *) 0x11D },
364 { .compatible
= "qcom,pm8058-rtc", .data
= (void *) 0x1E8 },
367 MODULE_DEVICE_TABLE(of
, pm8xxx_id_table
);
369 static int pm8xxx_rtc_probe(struct platform_device
*pdev
)
372 unsigned int ctrl_reg
;
373 struct pm8xxx_rtc
*rtc_dd
;
374 const struct of_device_id
*match
;
376 match
= of_match_node(pm8xxx_id_table
, pdev
->dev
.of_node
);
380 rtc_dd
= devm_kzalloc(&pdev
->dev
, sizeof(*rtc_dd
), GFP_KERNEL
);
384 /* Initialise spinlock to protect RTC control register */
385 spin_lock_init(&rtc_dd
->ctrl_reg_lock
);
387 rtc_dd
->regmap
= dev_get_regmap(pdev
->dev
.parent
, NULL
);
388 if (!rtc_dd
->regmap
) {
389 dev_err(&pdev
->dev
, "Parent regmap unavailable.\n");
393 rtc_dd
->rtc_alarm_irq
= platform_get_irq(pdev
, 0);
394 if (rtc_dd
->rtc_alarm_irq
< 0) {
395 dev_err(&pdev
->dev
, "Alarm IRQ resource absent!\n");
399 rtc_dd
->allow_set_time
= of_property_read_bool(pdev
->dev
.of_node
,
402 rtc_dd
->rtc_base
= (long) match
->data
;
404 /* Setup RTC register addresses */
405 rtc_dd
->rtc_write_base
= rtc_dd
->rtc_base
+ PM8XXX_RTC_WRITE_OFFSET
;
406 rtc_dd
->rtc_read_base
= rtc_dd
->rtc_base
+ PM8XXX_RTC_READ_OFFSET
;
407 rtc_dd
->alarm_rw_base
= rtc_dd
->rtc_base
+ PM8XXX_ALARM_RW_OFFSET
;
409 rtc_dd
->rtc_dev
= &pdev
->dev
;
411 /* Check if the RTC is on, else turn it on */
412 rc
= regmap_read(rtc_dd
->regmap
, rtc_dd
->rtc_base
, &ctrl_reg
);
414 dev_err(&pdev
->dev
, "RTC control register read failed!\n");
418 if (!(ctrl_reg
& PM8xxx_RTC_ENABLE
)) {
419 ctrl_reg
|= PM8xxx_RTC_ENABLE
;
420 rc
= regmap_write(rtc_dd
->regmap
, rtc_dd
->rtc_base
, ctrl_reg
);
423 "Write to RTC control register failed\n");
428 rtc_dd
->ctrl_reg
= ctrl_reg
;
430 platform_set_drvdata(pdev
, rtc_dd
);
432 device_init_wakeup(&pdev
->dev
, 1);
434 /* Register the RTC device */
435 rtc_dd
->rtc
= devm_rtc_device_register(&pdev
->dev
, "pm8xxx_rtc",
436 &pm8xxx_rtc_ops
, THIS_MODULE
);
437 if (IS_ERR(rtc_dd
->rtc
)) {
438 dev_err(&pdev
->dev
, "%s: RTC registration failed (%ld)\n",
439 __func__
, PTR_ERR(rtc_dd
->rtc
));
440 return PTR_ERR(rtc_dd
->rtc
);
443 /* Request the alarm IRQ */
444 rc
= devm_request_any_context_irq(&pdev
->dev
, rtc_dd
->rtc_alarm_irq
,
445 pm8xxx_alarm_trigger
,
447 "pm8xxx_rtc_alarm", rtc_dd
);
449 dev_err(&pdev
->dev
, "Request IRQ failed (%d)\n", rc
);
453 dev_dbg(&pdev
->dev
, "Probe success !!\n");
458 #ifdef CONFIG_PM_SLEEP
459 static int pm8xxx_rtc_resume(struct device
*dev
)
461 struct pm8xxx_rtc
*rtc_dd
= dev_get_drvdata(dev
);
463 if (device_may_wakeup(dev
))
464 disable_irq_wake(rtc_dd
->rtc_alarm_irq
);
469 static int pm8xxx_rtc_suspend(struct device
*dev
)
471 struct pm8xxx_rtc
*rtc_dd
= dev_get_drvdata(dev
);
473 if (device_may_wakeup(dev
))
474 enable_irq_wake(rtc_dd
->rtc_alarm_irq
);
480 static SIMPLE_DEV_PM_OPS(pm8xxx_rtc_pm_ops
,
484 static struct platform_driver pm8xxx_rtc_driver
= {
485 .probe
= pm8xxx_rtc_probe
,
487 .name
= "rtc-pm8xxx",
488 .owner
= THIS_MODULE
,
489 .pm
= &pm8xxx_rtc_pm_ops
,
490 .of_match_table
= pm8xxx_id_table
,
494 module_platform_driver(pm8xxx_rtc_driver
);
496 MODULE_ALIAS("platform:rtc-pm8xxx");
497 MODULE_DESCRIPTION("PMIC8xxx RTC driver");
498 MODULE_LICENSE("GPL v2");
499 MODULE_AUTHOR("Anirudh Ghayal <aghayal@codeaurora.org>");