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/regmap.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
14 /* RTC Register offsets from RTC CTRL REG */
15 #define PM8XXX_ALARM_CTRL_OFFSET 0x01
16 #define PM8XXX_RTC_WRITE_OFFSET 0x02
17 #define PM8XXX_RTC_READ_OFFSET 0x06
18 #define PM8XXX_ALARM_RW_OFFSET 0x0A
20 /* RTC_CTRL register bit fields */
21 #define PM8xxx_RTC_ENABLE BIT(7)
22 #define PM8xxx_RTC_ALARM_CLEAR BIT(0)
23 #define PM8xxx_RTC_ALARM_ENABLE BIT(7)
25 #define NUM_8_BIT_RTC_REGS 0x4
28 * struct pm8xxx_rtc_regs - describe RTC registers per PMIC versions
29 * @ctrl: base address of control register
30 * @write: base address of write register
31 * @read: base address of read register
32 * @alarm_ctrl: base address of alarm control register
33 * @alarm_ctrl2: base address of alarm control2 register
34 * @alarm_rw: base address of alarm read-write register
35 * @alarm_en: alarm enable mask
37 struct pm8xxx_rtc_regs {
41 unsigned int alarm_ctrl;
42 unsigned int alarm_ctrl2;
43 unsigned int alarm_rw;
44 unsigned int alarm_en;
48 * struct pm8xxx_rtc - rtc driver internal structure
49 * @rtc: rtc device for this driver.
50 * @regmap: regmap used to access RTC registers
51 * @allow_set_time: indicates whether writing to the RTC is allowed
52 * @rtc_alarm_irq: rtc alarm irq number.
53 * @regs: rtc registers description.
54 * @rtc_dev: device structure.
55 * @ctrl_reg_lock: spinlock protecting access to ctrl_reg.
58 struct rtc_device *rtc;
59 struct regmap *regmap;
62 const struct pm8xxx_rtc_regs *regs;
63 struct device *rtc_dev;
64 spinlock_t ctrl_reg_lock;
68 * Steps to write the RTC registers.
69 * 1. Disable alarm if enabled.
70 * 2. Disable rtc if enabled.
71 * 3. Write 0x00 to LSB.
72 * 4. Write Byte[1], Byte[2], Byte[3] then Byte[0].
73 * 5. Enable rtc if disabled in step 2.
74 * 6. Enable alarm if disabled in step 1.
76 static int pm8xxx_rtc_set_time(struct device *dev, struct rtc_time *tm)
79 unsigned long secs, irq_flags;
80 u8 value[NUM_8_BIT_RTC_REGS], alarm_enabled = 0, rtc_disabled = 0;
81 unsigned int ctrl_reg, rtc_ctrl_reg;
82 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
83 const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
85 if (!rtc_dd->allow_set_time)
88 secs = rtc_tm_to_time64(tm);
90 dev_dbg(dev, "Seconds value to be written to RTC = %lu\n", secs);
92 for (i = 0; i < NUM_8_BIT_RTC_REGS; i++) {
93 value[i] = secs & 0xFF;
97 spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags);
99 rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl, &ctrl_reg);
103 if (ctrl_reg & regs->alarm_en) {
105 ctrl_reg &= ~regs->alarm_en;
106 rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl, ctrl_reg);
108 dev_err(dev, "Write to RTC Alarm control register failed\n");
113 /* Disable RTC H/w before writing on RTC register */
114 rc = regmap_read(rtc_dd->regmap, regs->ctrl, &rtc_ctrl_reg);
118 if (rtc_ctrl_reg & PM8xxx_RTC_ENABLE) {
120 rtc_ctrl_reg &= ~PM8xxx_RTC_ENABLE;
121 rc = regmap_write(rtc_dd->regmap, regs->ctrl, rtc_ctrl_reg);
123 dev_err(dev, "Write to RTC control register failed\n");
128 /* Write 0 to Byte[0] */
129 rc = regmap_write(rtc_dd->regmap, regs->write, 0);
131 dev_err(dev, "Write to RTC write data register failed\n");
135 /* Write Byte[1], Byte[2], Byte[3] */
136 rc = regmap_bulk_write(rtc_dd->regmap, regs->write + 1,
137 &value[1], sizeof(value) - 1);
139 dev_err(dev, "Write to RTC write data register failed\n");
144 rc = regmap_write(rtc_dd->regmap, regs->write, value[0]);
146 dev_err(dev, "Write to RTC write data register failed\n");
150 /* Enable RTC H/w after writing on RTC register */
152 rtc_ctrl_reg |= PM8xxx_RTC_ENABLE;
153 rc = regmap_write(rtc_dd->regmap, regs->ctrl, rtc_ctrl_reg);
155 dev_err(dev, "Write to RTC control register failed\n");
161 ctrl_reg |= regs->alarm_en;
162 rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl, ctrl_reg);
164 dev_err(dev, "Write to RTC Alarm control register failed\n");
170 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
175 static int pm8xxx_rtc_read_time(struct device *dev, struct rtc_time *tm)
178 u8 value[NUM_8_BIT_RTC_REGS];
181 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
182 const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
184 rc = regmap_bulk_read(rtc_dd->regmap, regs->read, value, sizeof(value));
186 dev_err(dev, "RTC read data register failed\n");
191 * Read the LSB again and check if there has been a carry over.
192 * If there is, redo the read operation.
194 rc = regmap_read(rtc_dd->regmap, regs->read, ®);
196 dev_err(dev, "RTC read data register failed\n");
200 if (unlikely(reg < value[0])) {
201 rc = regmap_bulk_read(rtc_dd->regmap, regs->read,
202 value, sizeof(value));
204 dev_err(dev, "RTC read data register failed\n");
209 secs = value[0] | (value[1] << 8) | (value[2] << 16) |
210 ((unsigned long)value[3] << 24);
212 rtc_time64_to_tm(secs, tm);
214 dev_dbg(dev, "secs = %lu, h:m:s == %ptRt, y-m-d = %ptRdr\n", secs, tm, tm);
219 static int pm8xxx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
222 u8 value[NUM_8_BIT_RTC_REGS];
223 unsigned int ctrl_reg;
224 unsigned long secs, irq_flags;
225 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
226 const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
228 secs = rtc_tm_to_time64(&alarm->time);
230 for (i = 0; i < NUM_8_BIT_RTC_REGS; i++) {
231 value[i] = secs & 0xFF;
235 spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags);
237 rc = regmap_bulk_write(rtc_dd->regmap, regs->alarm_rw, value,
240 dev_err(dev, "Write to RTC ALARM register failed\n");
244 rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl, &ctrl_reg);
249 ctrl_reg |= regs->alarm_en;
251 ctrl_reg &= ~regs->alarm_en;
253 rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl, ctrl_reg);
255 dev_err(dev, "Write to RTC alarm control register failed\n");
259 dev_dbg(dev, "Alarm Set for h:m:s=%ptRt, y-m-d=%ptRdr\n",
260 &alarm->time, &alarm->time);
262 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
266 static int pm8xxx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
269 unsigned int ctrl_reg;
270 u8 value[NUM_8_BIT_RTC_REGS];
272 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
273 const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
275 rc = regmap_bulk_read(rtc_dd->regmap, regs->alarm_rw, value,
278 dev_err(dev, "RTC alarm time read failed\n");
282 secs = value[0] | (value[1] << 8) | (value[2] << 16) |
283 ((unsigned long)value[3] << 24);
285 rtc_time64_to_tm(secs, &alarm->time);
287 rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl, &ctrl_reg);
289 dev_err(dev, "Read from RTC alarm control register failed\n");
292 alarm->enabled = !!(ctrl_reg & PM8xxx_RTC_ALARM_ENABLE);
294 dev_dbg(dev, "Alarm set for - h:m:s=%ptRt, y-m-d=%ptRdr\n",
295 &alarm->time, &alarm->time);
300 static int pm8xxx_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
303 unsigned long irq_flags;
304 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
305 const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
306 unsigned int ctrl_reg;
307 u8 value[NUM_8_BIT_RTC_REGS] = {0};
309 spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags);
311 rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl, &ctrl_reg);
316 ctrl_reg |= regs->alarm_en;
318 ctrl_reg &= ~regs->alarm_en;
320 rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl, ctrl_reg);
322 dev_err(dev, "Write to RTC control register failed\n");
326 /* Clear Alarm register */
328 rc = regmap_bulk_write(rtc_dd->regmap, regs->alarm_rw, value,
331 dev_err(dev, "Clear RTC ALARM register failed\n");
337 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
341 static const struct rtc_class_ops pm8xxx_rtc_ops = {
342 .read_time = pm8xxx_rtc_read_time,
343 .set_time = pm8xxx_rtc_set_time,
344 .set_alarm = pm8xxx_rtc_set_alarm,
345 .read_alarm = pm8xxx_rtc_read_alarm,
346 .alarm_irq_enable = pm8xxx_rtc_alarm_irq_enable,
349 static irqreturn_t pm8xxx_alarm_trigger(int irq, void *dev_id)
351 struct pm8xxx_rtc *rtc_dd = dev_id;
352 const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
353 unsigned int ctrl_reg;
356 rtc_update_irq(rtc_dd->rtc, 1, RTC_IRQF | RTC_AF);
358 spin_lock(&rtc_dd->ctrl_reg_lock);
360 /* Clear the alarm enable bit */
361 rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl, &ctrl_reg);
363 spin_unlock(&rtc_dd->ctrl_reg_lock);
364 goto rtc_alarm_handled;
367 ctrl_reg &= ~regs->alarm_en;
369 rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl, ctrl_reg);
371 spin_unlock(&rtc_dd->ctrl_reg_lock);
372 dev_err(rtc_dd->rtc_dev,
373 "Write to alarm control register failed\n");
374 goto rtc_alarm_handled;
377 spin_unlock(&rtc_dd->ctrl_reg_lock);
379 /* Clear RTC alarm register */
380 rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl2, &ctrl_reg);
382 dev_err(rtc_dd->rtc_dev,
383 "RTC Alarm control2 register read failed\n");
384 goto rtc_alarm_handled;
387 ctrl_reg |= PM8xxx_RTC_ALARM_CLEAR;
388 rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl2, ctrl_reg);
390 dev_err(rtc_dd->rtc_dev,
391 "Write to RTC Alarm control2 register failed\n");
397 static int pm8xxx_rtc_enable(struct pm8xxx_rtc *rtc_dd)
399 const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
400 unsigned int ctrl_reg;
403 /* Check if the RTC is on, else turn it on */
404 rc = regmap_read(rtc_dd->regmap, regs->ctrl, &ctrl_reg);
408 if (!(ctrl_reg & PM8xxx_RTC_ENABLE)) {
409 ctrl_reg |= PM8xxx_RTC_ENABLE;
410 rc = regmap_write(rtc_dd->regmap, regs->ctrl, ctrl_reg);
418 static const struct pm8xxx_rtc_regs pm8921_regs = {
424 .alarm_ctrl2 = 0x11e,
428 static const struct pm8xxx_rtc_regs pm8058_regs = {
434 .alarm_ctrl2 = 0x1e9,
438 static const struct pm8xxx_rtc_regs pm8941_regs = {
443 .alarm_ctrl = 0x6146,
444 .alarm_ctrl2 = 0x6148,
448 static const struct pm8xxx_rtc_regs pmk8350_regs = {
453 .alarm_ctrl = 0x6246,
454 .alarm_ctrl2 = 0x6248,
459 * Hardcoded RTC bases until IORESOURCE_REG mapping is figured out
461 static const struct of_device_id pm8xxx_id_table[] = {
462 { .compatible = "qcom,pm8921-rtc", .data = &pm8921_regs },
463 { .compatible = "qcom,pm8018-rtc", .data = &pm8921_regs },
464 { .compatible = "qcom,pm8058-rtc", .data = &pm8058_regs },
465 { .compatible = "qcom,pm8941-rtc", .data = &pm8941_regs },
466 { .compatible = "qcom,pmk8350-rtc", .data = &pmk8350_regs },
469 MODULE_DEVICE_TABLE(of, pm8xxx_id_table);
471 static int pm8xxx_rtc_probe(struct platform_device *pdev)
474 struct pm8xxx_rtc *rtc_dd;
475 const struct of_device_id *match;
477 match = of_match_node(pm8xxx_id_table, pdev->dev.of_node);
481 rtc_dd = devm_kzalloc(&pdev->dev, sizeof(*rtc_dd), GFP_KERNEL);
485 /* Initialise spinlock to protect RTC control register */
486 spin_lock_init(&rtc_dd->ctrl_reg_lock);
488 rtc_dd->regmap = dev_get_regmap(pdev->dev.parent, NULL);
489 if (!rtc_dd->regmap) {
490 dev_err(&pdev->dev, "Parent regmap unavailable.\n");
494 rtc_dd->rtc_alarm_irq = platform_get_irq(pdev, 0);
495 if (rtc_dd->rtc_alarm_irq < 0)
498 rtc_dd->allow_set_time = of_property_read_bool(pdev->dev.of_node,
501 rtc_dd->regs = match->data;
502 rtc_dd->rtc_dev = &pdev->dev;
504 rc = pm8xxx_rtc_enable(rtc_dd);
508 platform_set_drvdata(pdev, rtc_dd);
510 device_init_wakeup(&pdev->dev, 1);
512 /* Register the RTC device */
513 rtc_dd->rtc = devm_rtc_allocate_device(&pdev->dev);
514 if (IS_ERR(rtc_dd->rtc))
515 return PTR_ERR(rtc_dd->rtc);
517 rtc_dd->rtc->ops = &pm8xxx_rtc_ops;
518 rtc_dd->rtc->range_max = U32_MAX;
520 /* Request the alarm IRQ */
521 rc = devm_request_any_context_irq(&pdev->dev, rtc_dd->rtc_alarm_irq,
522 pm8xxx_alarm_trigger,
524 "pm8xxx_rtc_alarm", rtc_dd);
526 dev_err(&pdev->dev, "Request IRQ failed (%d)\n", rc);
530 return devm_rtc_register_device(rtc_dd->rtc);
533 #ifdef CONFIG_PM_SLEEP
534 static int pm8xxx_rtc_resume(struct device *dev)
536 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
538 if (device_may_wakeup(dev))
539 disable_irq_wake(rtc_dd->rtc_alarm_irq);
544 static int pm8xxx_rtc_suspend(struct device *dev)
546 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
548 if (device_may_wakeup(dev))
549 enable_irq_wake(rtc_dd->rtc_alarm_irq);
555 static SIMPLE_DEV_PM_OPS(pm8xxx_rtc_pm_ops,
559 static struct platform_driver pm8xxx_rtc_driver = {
560 .probe = pm8xxx_rtc_probe,
562 .name = "rtc-pm8xxx",
563 .pm = &pm8xxx_rtc_pm_ops,
564 .of_match_table = pm8xxx_id_table,
568 module_platform_driver(pm8xxx_rtc_driver);
570 MODULE_ALIAS("platform:rtc-pm8xxx");
571 MODULE_DESCRIPTION("PMIC8xxx RTC driver");
572 MODULE_LICENSE("GPL v2");
573 MODULE_AUTHOR("Anirudh Ghayal <aghayal@codeaurora.org>");