1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
5 #include <linux/module.h>
6 #include <linux/init.h>
8 #include <linux/platform_device.h>
10 #include <linux/pm_wakeirq.h>
11 #include <linux/regmap.h>
12 #include <linux/slab.h>
13 #include <linux/spinlock.h>
15 /* RTC Register offsets from RTC CTRL REG */
16 #define PM8XXX_ALARM_CTRL_OFFSET 0x01
17 #define PM8XXX_RTC_WRITE_OFFSET 0x02
18 #define PM8XXX_RTC_READ_OFFSET 0x06
19 #define PM8XXX_ALARM_RW_OFFSET 0x0A
21 /* RTC_CTRL register bit fields */
22 #define PM8xxx_RTC_ENABLE BIT(7)
23 #define PM8xxx_RTC_ALARM_CLEAR BIT(0)
24 #define PM8xxx_RTC_ALARM_ENABLE BIT(7)
26 #define NUM_8_BIT_RTC_REGS 0x4
29 * struct pm8xxx_rtc_regs - describe RTC registers per PMIC versions
30 * @ctrl: base address of control register
31 * @write: base address of write register
32 * @read: base address of read register
33 * @alarm_ctrl: base address of alarm control register
34 * @alarm_ctrl2: base address of alarm control2 register
35 * @alarm_rw: base address of alarm read-write register
36 * @alarm_en: alarm enable mask
38 struct pm8xxx_rtc_regs {
42 unsigned int alarm_ctrl;
43 unsigned int alarm_ctrl2;
44 unsigned int alarm_rw;
45 unsigned int alarm_en;
49 * struct pm8xxx_rtc - rtc driver internal structure
50 * @rtc: rtc device for this driver.
51 * @regmap: regmap used to access RTC registers
52 * @allow_set_time: indicates whether writing to the RTC is allowed
53 * @rtc_alarm_irq: rtc alarm irq number.
54 * @regs: rtc registers description.
55 * @rtc_dev: device structure.
56 * @ctrl_reg_lock: spinlock protecting access to ctrl_reg.
59 struct rtc_device *rtc;
60 struct regmap *regmap;
63 const struct pm8xxx_rtc_regs *regs;
64 struct device *rtc_dev;
65 spinlock_t ctrl_reg_lock;
69 * Steps to write the RTC registers.
70 * 1. Disable alarm if enabled.
71 * 2. Disable rtc if enabled.
72 * 3. Write 0x00 to LSB.
73 * 4. Write Byte[1], Byte[2], Byte[3] then Byte[0].
74 * 5. Enable rtc if disabled in step 2.
75 * 6. Enable alarm if disabled in step 1.
77 static int pm8xxx_rtc_set_time(struct device *dev, struct rtc_time *tm)
80 unsigned long secs, irq_flags;
81 u8 value[NUM_8_BIT_RTC_REGS], alarm_enabled = 0, rtc_disabled = 0;
82 unsigned int ctrl_reg, rtc_ctrl_reg;
83 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
84 const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
86 if (!rtc_dd->allow_set_time)
89 secs = rtc_tm_to_time64(tm);
91 dev_dbg(dev, "Seconds value to be written to RTC = %lu\n", secs);
93 for (i = 0; i < NUM_8_BIT_RTC_REGS; i++) {
94 value[i] = secs & 0xFF;
98 spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags);
100 rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl, &ctrl_reg);
104 if (ctrl_reg & regs->alarm_en) {
106 ctrl_reg &= ~regs->alarm_en;
107 rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl, ctrl_reg);
109 dev_err(dev, "Write to RTC Alarm control register failed\n");
114 /* Disable RTC H/w before writing on RTC register */
115 rc = regmap_read(rtc_dd->regmap, regs->ctrl, &rtc_ctrl_reg);
119 if (rtc_ctrl_reg & PM8xxx_RTC_ENABLE) {
121 rtc_ctrl_reg &= ~PM8xxx_RTC_ENABLE;
122 rc = regmap_write(rtc_dd->regmap, regs->ctrl, rtc_ctrl_reg);
124 dev_err(dev, "Write to RTC control register failed\n");
129 /* Write 0 to Byte[0] */
130 rc = regmap_write(rtc_dd->regmap, regs->write, 0);
132 dev_err(dev, "Write to RTC write data register failed\n");
136 /* Write Byte[1], Byte[2], Byte[3] */
137 rc = regmap_bulk_write(rtc_dd->regmap, regs->write + 1,
138 &value[1], sizeof(value) - 1);
140 dev_err(dev, "Write to RTC write data register failed\n");
145 rc = regmap_write(rtc_dd->regmap, regs->write, value[0]);
147 dev_err(dev, "Write to RTC write data register failed\n");
151 /* Enable RTC H/w after writing on RTC register */
153 rtc_ctrl_reg |= PM8xxx_RTC_ENABLE;
154 rc = regmap_write(rtc_dd->regmap, regs->ctrl, rtc_ctrl_reg);
156 dev_err(dev, "Write to RTC control register failed\n");
162 ctrl_reg |= regs->alarm_en;
163 rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl, ctrl_reg);
165 dev_err(dev, "Write to RTC Alarm control register failed\n");
171 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
176 static int pm8xxx_rtc_read_time(struct device *dev, struct rtc_time *tm)
179 u8 value[NUM_8_BIT_RTC_REGS];
182 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
183 const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
185 rc = regmap_bulk_read(rtc_dd->regmap, regs->read, value, sizeof(value));
187 dev_err(dev, "RTC read data register failed\n");
192 * Read the LSB again and check if there has been a carry over.
193 * If there is, redo the read operation.
195 rc = regmap_read(rtc_dd->regmap, regs->read, ®);
197 dev_err(dev, "RTC read data register failed\n");
201 if (unlikely(reg < value[0])) {
202 rc = regmap_bulk_read(rtc_dd->regmap, regs->read,
203 value, sizeof(value));
205 dev_err(dev, "RTC read data register failed\n");
210 secs = value[0] | (value[1] << 8) | (value[2] << 16) |
211 ((unsigned long)value[3] << 24);
213 rtc_time64_to_tm(secs, tm);
215 dev_dbg(dev, "secs = %lu, h:m:s == %ptRt, y-m-d = %ptRdr\n", secs, tm, tm);
220 static int pm8xxx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
223 u8 value[NUM_8_BIT_RTC_REGS];
224 unsigned int ctrl_reg;
225 unsigned long secs, irq_flags;
226 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
227 const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
229 secs = rtc_tm_to_time64(&alarm->time);
231 for (i = 0; i < NUM_8_BIT_RTC_REGS; i++) {
232 value[i] = secs & 0xFF;
236 spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags);
238 rc = regmap_bulk_write(rtc_dd->regmap, regs->alarm_rw, value,
241 dev_err(dev, "Write to RTC ALARM register failed\n");
245 rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl, &ctrl_reg);
250 ctrl_reg |= regs->alarm_en;
252 ctrl_reg &= ~regs->alarm_en;
254 rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl, ctrl_reg);
256 dev_err(dev, "Write to RTC alarm control register failed\n");
260 dev_dbg(dev, "Alarm Set for h:m:s=%ptRt, y-m-d=%ptRdr\n",
261 &alarm->time, &alarm->time);
263 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
267 static int pm8xxx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
270 unsigned int ctrl_reg;
271 u8 value[NUM_8_BIT_RTC_REGS];
273 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
274 const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
276 rc = regmap_bulk_read(rtc_dd->regmap, regs->alarm_rw, value,
279 dev_err(dev, "RTC alarm time read failed\n");
283 secs = value[0] | (value[1] << 8) | (value[2] << 16) |
284 ((unsigned long)value[3] << 24);
286 rtc_time64_to_tm(secs, &alarm->time);
288 rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl, &ctrl_reg);
290 dev_err(dev, "Read from RTC alarm control register failed\n");
293 alarm->enabled = !!(ctrl_reg & PM8xxx_RTC_ALARM_ENABLE);
295 dev_dbg(dev, "Alarm set for - h:m:s=%ptRt, y-m-d=%ptRdr\n",
296 &alarm->time, &alarm->time);
301 static int pm8xxx_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
304 unsigned long irq_flags;
305 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
306 const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
307 unsigned int ctrl_reg;
308 u8 value[NUM_8_BIT_RTC_REGS] = {0};
310 spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags);
312 rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl, &ctrl_reg);
317 ctrl_reg |= regs->alarm_en;
319 ctrl_reg &= ~regs->alarm_en;
321 rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl, ctrl_reg);
323 dev_err(dev, "Write to RTC control register failed\n");
327 /* Clear Alarm register */
329 rc = regmap_bulk_write(rtc_dd->regmap, regs->alarm_rw, value,
332 dev_err(dev, "Clear RTC ALARM register failed\n");
338 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
342 static const struct rtc_class_ops pm8xxx_rtc_ops = {
343 .read_time = pm8xxx_rtc_read_time,
344 .set_time = pm8xxx_rtc_set_time,
345 .set_alarm = pm8xxx_rtc_set_alarm,
346 .read_alarm = pm8xxx_rtc_read_alarm,
347 .alarm_irq_enable = pm8xxx_rtc_alarm_irq_enable,
350 static irqreturn_t pm8xxx_alarm_trigger(int irq, void *dev_id)
352 struct pm8xxx_rtc *rtc_dd = dev_id;
353 const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
354 unsigned int ctrl_reg;
357 rtc_update_irq(rtc_dd->rtc, 1, RTC_IRQF | RTC_AF);
359 spin_lock(&rtc_dd->ctrl_reg_lock);
361 /* Clear the alarm enable bit */
362 rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl, &ctrl_reg);
364 spin_unlock(&rtc_dd->ctrl_reg_lock);
365 goto rtc_alarm_handled;
368 ctrl_reg &= ~regs->alarm_en;
370 rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl, ctrl_reg);
372 spin_unlock(&rtc_dd->ctrl_reg_lock);
373 dev_err(rtc_dd->rtc_dev,
374 "Write to alarm control register failed\n");
375 goto rtc_alarm_handled;
378 spin_unlock(&rtc_dd->ctrl_reg_lock);
380 /* Clear RTC alarm register */
381 rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl2, &ctrl_reg);
383 dev_err(rtc_dd->rtc_dev,
384 "RTC Alarm control2 register read failed\n");
385 goto rtc_alarm_handled;
388 ctrl_reg |= PM8xxx_RTC_ALARM_CLEAR;
389 rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl2, ctrl_reg);
391 dev_err(rtc_dd->rtc_dev,
392 "Write to RTC Alarm control2 register failed\n");
398 static int pm8xxx_rtc_enable(struct pm8xxx_rtc *rtc_dd)
400 const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
401 unsigned int ctrl_reg;
404 /* Check if the RTC is on, else turn it on */
405 rc = regmap_read(rtc_dd->regmap, regs->ctrl, &ctrl_reg);
409 if (!(ctrl_reg & PM8xxx_RTC_ENABLE)) {
410 ctrl_reg |= PM8xxx_RTC_ENABLE;
411 rc = regmap_write(rtc_dd->regmap, regs->ctrl, ctrl_reg);
419 static const struct pm8xxx_rtc_regs pm8921_regs = {
425 .alarm_ctrl2 = 0x11e,
429 static const struct pm8xxx_rtc_regs pm8058_regs = {
435 .alarm_ctrl2 = 0x1e9,
439 static const struct pm8xxx_rtc_regs pm8941_regs = {
444 .alarm_ctrl = 0x6146,
445 .alarm_ctrl2 = 0x6148,
449 static const struct pm8xxx_rtc_regs pmk8350_regs = {
454 .alarm_ctrl = 0x6246,
455 .alarm_ctrl2 = 0x6248,
460 * Hardcoded RTC bases until IORESOURCE_REG mapping is figured out
462 static const struct of_device_id pm8xxx_id_table[] = {
463 { .compatible = "qcom,pm8921-rtc", .data = &pm8921_regs },
464 { .compatible = "qcom,pm8018-rtc", .data = &pm8921_regs },
465 { .compatible = "qcom,pm8058-rtc", .data = &pm8058_regs },
466 { .compatible = "qcom,pm8941-rtc", .data = &pm8941_regs },
467 { .compatible = "qcom,pmk8350-rtc", .data = &pmk8350_regs },
470 MODULE_DEVICE_TABLE(of, pm8xxx_id_table);
472 static int pm8xxx_rtc_probe(struct platform_device *pdev)
475 struct pm8xxx_rtc *rtc_dd;
476 const struct of_device_id *match;
478 match = of_match_node(pm8xxx_id_table, pdev->dev.of_node);
482 rtc_dd = devm_kzalloc(&pdev->dev, sizeof(*rtc_dd), GFP_KERNEL);
486 /* Initialise spinlock to protect RTC control register */
487 spin_lock_init(&rtc_dd->ctrl_reg_lock);
489 rtc_dd->regmap = dev_get_regmap(pdev->dev.parent, NULL);
490 if (!rtc_dd->regmap) {
491 dev_err(&pdev->dev, "Parent regmap unavailable.\n");
495 rtc_dd->rtc_alarm_irq = platform_get_irq(pdev, 0);
496 if (rtc_dd->rtc_alarm_irq < 0)
499 rtc_dd->allow_set_time = of_property_read_bool(pdev->dev.of_node,
502 rtc_dd->regs = match->data;
503 rtc_dd->rtc_dev = &pdev->dev;
505 rc = pm8xxx_rtc_enable(rtc_dd);
509 platform_set_drvdata(pdev, rtc_dd);
511 device_init_wakeup(&pdev->dev, 1);
513 /* Register the RTC device */
514 rtc_dd->rtc = devm_rtc_allocate_device(&pdev->dev);
515 if (IS_ERR(rtc_dd->rtc))
516 return PTR_ERR(rtc_dd->rtc);
518 rtc_dd->rtc->ops = &pm8xxx_rtc_ops;
519 rtc_dd->rtc->range_max = U32_MAX;
521 /* Request the alarm IRQ */
522 rc = devm_request_any_context_irq(&pdev->dev, rtc_dd->rtc_alarm_irq,
523 pm8xxx_alarm_trigger,
525 "pm8xxx_rtc_alarm", rtc_dd);
527 dev_err(&pdev->dev, "Request IRQ failed (%d)\n", rc);
531 rc = devm_rtc_register_device(rtc_dd->rtc);
535 rc = dev_pm_set_wake_irq(&pdev->dev, rtc_dd->rtc_alarm_irq);
542 static int pm8xxx_remove(struct platform_device *pdev)
544 dev_pm_clear_wake_irq(&pdev->dev);
548 static struct platform_driver pm8xxx_rtc_driver = {
549 .probe = pm8xxx_rtc_probe,
550 .remove = pm8xxx_remove,
552 .name = "rtc-pm8xxx",
553 .of_match_table = pm8xxx_id_table,
557 module_platform_driver(pm8xxx_rtc_driver);
559 MODULE_ALIAS("platform:rtc-pm8xxx");
560 MODULE_DESCRIPTION("PMIC8xxx RTC driver");
561 MODULE_LICENSE("GPL v2");
562 MODULE_AUTHOR("Anirudh Ghayal <aghayal@codeaurora.org>");