1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
6 * Copyright (c) 2023, Linaro Limited
9 #include <linux/module.h>
10 #include <linux/nvmem-consumer.h>
11 #include <linux/init.h>
12 #include <linux/rtc.h>
13 #include <linux/platform_device.h>
15 #include <linux/pm_wakeirq.h>
16 #include <linux/regmap.h>
17 #include <linux/slab.h>
18 #include <linux/spinlock.h>
20 #include <linux/unaligned.h>
22 /* RTC_CTRL register bit fields */
23 #define PM8xxx_RTC_ENABLE BIT(7)
24 #define PM8xxx_RTC_ALARM_CLEAR BIT(0)
25 #define PM8xxx_RTC_ALARM_ENABLE BIT(7)
27 #define NUM_8_BIT_RTC_REGS 0x4
30 * struct pm8xxx_rtc_regs - describe RTC registers per PMIC versions
31 * @ctrl: address of control register
32 * @write: base address of write registers
33 * @read: base address of read registers
34 * @alarm_ctrl: address of alarm control register
35 * @alarm_ctrl2: address of alarm control2 register
36 * @alarm_rw: base address of alarm read-write registers
37 * @alarm_en: alarm enable mask
39 struct pm8xxx_rtc_regs
{
43 unsigned int alarm_ctrl
;
44 unsigned int alarm_ctrl2
;
45 unsigned int alarm_rw
;
46 unsigned int alarm_en
;
50 * struct pm8xxx_rtc - RTC driver internal structure
52 * @regmap: regmap used to access registers
53 * @allow_set_time: whether the time can be set
54 * @alarm_irq: alarm irq number
55 * @regs: register description
56 * @dev: device structure
57 * @nvmem_cell: nvmem cell for offset
58 * @offset: offset from epoch in seconds
61 struct rtc_device
*rtc
;
62 struct regmap
*regmap
;
65 const struct pm8xxx_rtc_regs
*regs
;
67 struct nvmem_cell
*nvmem_cell
;
71 static int pm8xxx_rtc_read_nvmem_offset(struct pm8xxx_rtc
*rtc_dd
)
77 buf
= nvmem_cell_read(rtc_dd
->nvmem_cell
, &len
);
80 dev_dbg(rtc_dd
->dev
, "failed to read nvmem offset: %d\n", rc
);
84 if (len
!= sizeof(u32
)) {
85 dev_dbg(rtc_dd
->dev
, "unexpected nvmem cell size %zu\n", len
);
90 rtc_dd
->offset
= get_unaligned_le32(buf
);
97 static int pm8xxx_rtc_write_nvmem_offset(struct pm8xxx_rtc
*rtc_dd
, u32 offset
)
102 put_unaligned_le32(offset
, buf
);
104 rc
= nvmem_cell_write(rtc_dd
->nvmem_cell
, buf
, sizeof(buf
));
106 dev_dbg(rtc_dd
->dev
, "failed to write nvmem offset: %d\n", rc
);
113 static int pm8xxx_rtc_read_offset(struct pm8xxx_rtc
*rtc_dd
)
115 if (!rtc_dd
->nvmem_cell
)
118 return pm8xxx_rtc_read_nvmem_offset(rtc_dd
);
121 static int pm8xxx_rtc_read_raw(struct pm8xxx_rtc
*rtc_dd
, u32
*secs
)
123 const struct pm8xxx_rtc_regs
*regs
= rtc_dd
->regs
;
124 u8 value
[NUM_8_BIT_RTC_REGS
];
128 rc
= regmap_bulk_read(rtc_dd
->regmap
, regs
->read
, value
, sizeof(value
));
133 * Read the LSB again and check if there has been a carry over.
134 * If there has, redo the read operation.
136 rc
= regmap_read(rtc_dd
->regmap
, regs
->read
, ®
);
140 if (reg
< value
[0]) {
141 rc
= regmap_bulk_read(rtc_dd
->regmap
, regs
->read
, value
,
147 *secs
= get_unaligned_le32(value
);
152 static int pm8xxx_rtc_update_offset(struct pm8xxx_rtc
*rtc_dd
, u32 secs
)
158 if (!rtc_dd
->nvmem_cell
)
161 rc
= pm8xxx_rtc_read_raw(rtc_dd
, &raw_secs
);
165 offset
= secs
- raw_secs
;
167 if (offset
== rtc_dd
->offset
)
170 rc
= pm8xxx_rtc_write_nvmem_offset(rtc_dd
, offset
);
174 rtc_dd
->offset
= offset
;
180 * Steps to write the RTC registers.
181 * 1. Disable alarm if enabled.
182 * 2. Disable rtc if enabled.
183 * 3. Write 0x00 to LSB.
184 * 4. Write Byte[1], Byte[2], Byte[3] then Byte[0].
185 * 5. Enable rtc if disabled in step 2.
186 * 6. Enable alarm if disabled in step 1.
188 static int __pm8xxx_rtc_set_time(struct pm8xxx_rtc
*rtc_dd
, u32 secs
)
190 const struct pm8xxx_rtc_regs
*regs
= rtc_dd
->regs
;
191 u8 value
[NUM_8_BIT_RTC_REGS
];
195 put_unaligned_le32(secs
, value
);
197 rc
= regmap_update_bits_check(rtc_dd
->regmap
, regs
->alarm_ctrl
,
198 regs
->alarm_en
, 0, &alarm_enabled
);
203 rc
= regmap_update_bits(rtc_dd
->regmap
, regs
->ctrl
, PM8xxx_RTC_ENABLE
, 0);
207 /* Write 0 to Byte[0] */
208 rc
= regmap_write(rtc_dd
->regmap
, regs
->write
, 0);
212 /* Write Byte[1], Byte[2], Byte[3] */
213 rc
= regmap_bulk_write(rtc_dd
->regmap
, regs
->write
+ 1,
214 &value
[1], sizeof(value
) - 1);
219 rc
= regmap_write(rtc_dd
->regmap
, regs
->write
, value
[0]);
224 rc
= regmap_update_bits(rtc_dd
->regmap
, regs
->ctrl
, PM8xxx_RTC_ENABLE
,
230 rc
= regmap_update_bits(rtc_dd
->regmap
, regs
->alarm_ctrl
,
231 regs
->alarm_en
, regs
->alarm_en
);
239 static int pm8xxx_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
241 struct pm8xxx_rtc
*rtc_dd
= dev_get_drvdata(dev
);
245 secs
= rtc_tm_to_time64(tm
);
247 if (rtc_dd
->allow_set_time
)
248 rc
= __pm8xxx_rtc_set_time(rtc_dd
, secs
);
250 rc
= pm8xxx_rtc_update_offset(rtc_dd
, secs
);
255 dev_dbg(dev
, "set time: %ptRd %ptRt (%u + %u)\n", tm
, tm
,
256 secs
- rtc_dd
->offset
, rtc_dd
->offset
);
260 static int pm8xxx_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
262 struct pm8xxx_rtc
*rtc_dd
= dev_get_drvdata(dev
);
266 rc
= pm8xxx_rtc_read_raw(rtc_dd
, &secs
);
270 secs
+= rtc_dd
->offset
;
271 rtc_time64_to_tm(secs
, tm
);
273 dev_dbg(dev
, "read time: %ptRd %ptRt (%u + %u)\n", tm
, tm
,
274 secs
- rtc_dd
->offset
, rtc_dd
->offset
);
278 static int pm8xxx_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
280 struct pm8xxx_rtc
*rtc_dd
= dev_get_drvdata(dev
);
281 const struct pm8xxx_rtc_regs
*regs
= rtc_dd
->regs
;
282 u8 value
[NUM_8_BIT_RTC_REGS
];
286 secs
= rtc_tm_to_time64(&alarm
->time
);
287 secs
-= rtc_dd
->offset
;
288 put_unaligned_le32(secs
, value
);
290 rc
= regmap_update_bits(rtc_dd
->regmap
, regs
->alarm_ctrl
,
295 rc
= regmap_bulk_write(rtc_dd
->regmap
, regs
->alarm_rw
, value
,
300 if (alarm
->enabled
) {
301 rc
= regmap_update_bits(rtc_dd
->regmap
, regs
->alarm_ctrl
,
302 regs
->alarm_en
, regs
->alarm_en
);
307 dev_dbg(dev
, "set alarm: %ptRd %ptRt\n", &alarm
->time
, &alarm
->time
);
312 static int pm8xxx_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
314 struct pm8xxx_rtc
*rtc_dd
= dev_get_drvdata(dev
);
315 const struct pm8xxx_rtc_regs
*regs
= rtc_dd
->regs
;
316 u8 value
[NUM_8_BIT_RTC_REGS
];
317 unsigned int ctrl_reg
;
321 rc
= regmap_bulk_read(rtc_dd
->regmap
, regs
->alarm_rw
, value
,
326 secs
= get_unaligned_le32(value
);
327 secs
+= rtc_dd
->offset
;
328 rtc_time64_to_tm(secs
, &alarm
->time
);
330 rc
= regmap_read(rtc_dd
->regmap
, regs
->alarm_ctrl
, &ctrl_reg
);
334 alarm
->enabled
= !!(ctrl_reg
& PM8xxx_RTC_ALARM_ENABLE
);
336 dev_dbg(dev
, "read alarm: %ptRd %ptRt\n", &alarm
->time
, &alarm
->time
);
341 static int pm8xxx_rtc_alarm_irq_enable(struct device
*dev
, unsigned int enable
)
343 struct pm8xxx_rtc
*rtc_dd
= dev_get_drvdata(dev
);
344 const struct pm8xxx_rtc_regs
*regs
= rtc_dd
->regs
;
345 u8 value
[NUM_8_BIT_RTC_REGS
] = {0};
350 val
= regs
->alarm_en
;
354 rc
= regmap_update_bits(rtc_dd
->regmap
, regs
->alarm_ctrl
,
355 regs
->alarm_en
, val
);
359 /* Clear alarm register */
361 rc
= regmap_bulk_write(rtc_dd
->regmap
, regs
->alarm_rw
, value
,
370 static const struct rtc_class_ops pm8xxx_rtc_ops
= {
371 .read_time
= pm8xxx_rtc_read_time
,
372 .set_time
= pm8xxx_rtc_set_time
,
373 .set_alarm
= pm8xxx_rtc_set_alarm
,
374 .read_alarm
= pm8xxx_rtc_read_alarm
,
375 .alarm_irq_enable
= pm8xxx_rtc_alarm_irq_enable
,
378 static irqreturn_t
pm8xxx_alarm_trigger(int irq
, void *dev_id
)
380 struct pm8xxx_rtc
*rtc_dd
= dev_id
;
381 const struct pm8xxx_rtc_regs
*regs
= rtc_dd
->regs
;
384 rtc_update_irq(rtc_dd
->rtc
, 1, RTC_IRQF
| RTC_AF
);
387 rc
= regmap_update_bits(rtc_dd
->regmap
, regs
->alarm_ctrl
,
392 /* Clear alarm status */
393 rc
= regmap_update_bits(rtc_dd
->regmap
, regs
->alarm_ctrl2
,
394 PM8xxx_RTC_ALARM_CLEAR
, 0);
401 static int pm8xxx_rtc_enable(struct pm8xxx_rtc
*rtc_dd
)
403 const struct pm8xxx_rtc_regs
*regs
= rtc_dd
->regs
;
405 return regmap_update_bits(rtc_dd
->regmap
, regs
->ctrl
, PM8xxx_RTC_ENABLE
,
409 static const struct pm8xxx_rtc_regs pm8921_regs
= {
415 .alarm_ctrl2
= 0x11e,
419 static const struct pm8xxx_rtc_regs pm8058_regs
= {
425 .alarm_ctrl2
= 0x1e9,
429 static const struct pm8xxx_rtc_regs pm8941_regs
= {
434 .alarm_ctrl
= 0x6146,
435 .alarm_ctrl2
= 0x6148,
439 static const struct pm8xxx_rtc_regs pmk8350_regs
= {
444 .alarm_ctrl
= 0x6246,
445 .alarm_ctrl2
= 0x6248,
449 static const struct of_device_id pm8xxx_id_table
[] = {
450 { .compatible
= "qcom,pm8921-rtc", .data
= &pm8921_regs
},
451 { .compatible
= "qcom,pm8058-rtc", .data
= &pm8058_regs
},
452 { .compatible
= "qcom,pm8941-rtc", .data
= &pm8941_regs
},
453 { .compatible
= "qcom,pmk8350-rtc", .data
= &pmk8350_regs
},
456 MODULE_DEVICE_TABLE(of
, pm8xxx_id_table
);
458 static int pm8xxx_rtc_probe(struct platform_device
*pdev
)
460 const struct of_device_id
*match
;
461 struct pm8xxx_rtc
*rtc_dd
;
464 match
= of_match_node(pm8xxx_id_table
, pdev
->dev
.of_node
);
468 rtc_dd
= devm_kzalloc(&pdev
->dev
, sizeof(*rtc_dd
), GFP_KERNEL
);
472 rtc_dd
->regmap
= dev_get_regmap(pdev
->dev
.parent
, NULL
);
476 rtc_dd
->alarm_irq
= platform_get_irq(pdev
, 0);
477 if (rtc_dd
->alarm_irq
< 0)
480 rtc_dd
->allow_set_time
= of_property_read_bool(pdev
->dev
.of_node
,
483 rtc_dd
->nvmem_cell
= devm_nvmem_cell_get(&pdev
->dev
, "offset");
484 if (IS_ERR(rtc_dd
->nvmem_cell
)) {
485 rc
= PTR_ERR(rtc_dd
->nvmem_cell
);
488 rtc_dd
->nvmem_cell
= NULL
;
491 rtc_dd
->regs
= match
->data
;
492 rtc_dd
->dev
= &pdev
->dev
;
494 if (!rtc_dd
->allow_set_time
) {
495 rc
= pm8xxx_rtc_read_offset(rtc_dd
);
500 rc
= pm8xxx_rtc_enable(rtc_dd
);
504 platform_set_drvdata(pdev
, rtc_dd
);
506 device_init_wakeup(&pdev
->dev
, 1);
508 rtc_dd
->rtc
= devm_rtc_allocate_device(&pdev
->dev
);
509 if (IS_ERR(rtc_dd
->rtc
))
510 return PTR_ERR(rtc_dd
->rtc
);
512 rtc_dd
->rtc
->ops
= &pm8xxx_rtc_ops
;
513 rtc_dd
->rtc
->range_max
= U32_MAX
;
515 rc
= devm_request_any_context_irq(&pdev
->dev
, rtc_dd
->alarm_irq
,
516 pm8xxx_alarm_trigger
,
518 "pm8xxx_rtc_alarm", rtc_dd
);
522 rc
= devm_rtc_register_device(rtc_dd
->rtc
);
526 rc
= dev_pm_set_wake_irq(&pdev
->dev
, rtc_dd
->alarm_irq
);
533 static void pm8xxx_remove(struct platform_device
*pdev
)
535 dev_pm_clear_wake_irq(&pdev
->dev
);
538 static struct platform_driver pm8xxx_rtc_driver
= {
539 .probe
= pm8xxx_rtc_probe
,
540 .remove
= pm8xxx_remove
,
542 .name
= "rtc-pm8xxx",
543 .of_match_table
= pm8xxx_id_table
,
547 module_platform_driver(pm8xxx_rtc_driver
);
549 MODULE_ALIAS("platform:rtc-pm8xxx");
550 MODULE_DESCRIPTION("PMIC8xxx RTC driver");
551 MODULE_LICENSE("GPL v2");
552 MODULE_AUTHOR("Anirudh Ghayal <aghayal@codeaurora.org>");
553 MODULE_AUTHOR("Johan Hovold <johan@kernel.org>");