1 // SPDX-License-Identifier: GPL-2.0
3 * RTC driver for tps6594 PMIC
5 * Copyright (C) 2023 BayLibre Incorporated - https://www.baylibre.com/
9 #include <linux/errno.h>
10 #include <linux/init.h>
11 #include <linux/interrupt.h>
12 #include <linux/kernel.h>
13 #include <linux/limits.h>
14 #include <linux/math64.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/mod_devicetable.h>
18 #include <linux/property.h>
19 #include <linux/rtc.h>
20 #include <linux/types.h>
21 #include <linux/units.h>
23 #include <linux/mfd/tps6594.h>
25 // Total number of RTC registers needed to set time
26 #define NUM_TIME_REGS (TPS6594_REG_RTC_WEEKS - TPS6594_REG_RTC_SECONDS + 1)
28 // Total number of RTC alarm registers
29 #define NUM_TIME_ALARM_REGS (NUM_TIME_REGS - 1)
32 * Min and max values supported by 'offset' interface (swapped sign).
33 * After conversion, the values do not exceed the range [-32767, 33767]
34 * which COMP_REG must conform to.
36 #define MIN_OFFSET (-277774)
37 #define MAX_OFFSET (277774)
39 // Number of ticks per hour
40 #define TICKS_PER_HOUR (32768 * 3600)
42 // Multiplier for ppb conversions
45 static int tps6594_rtc_alarm_irq_enable(struct device *dev,
48 struct tps6594 *tps = dev_get_drvdata(dev->parent);
51 val = enabled ? TPS6594_BIT_IT_ALARM : 0;
53 return regmap_update_bits(tps->regmap, TPS6594_REG_RTC_INTERRUPTS,
54 TPS6594_BIT_IT_ALARM, val);
57 /* Pulse GET_TIME field of RTC_CTRL_1 to store a timestamp in shadow registers. */
58 static int tps6594_rtc_shadow_timestamp(struct device *dev, struct tps6594 *tps)
63 * Set GET_TIME to 0. Next time we set GET_TIME to 1 we will be sure to store
64 * an up-to-date timestamp.
66 ret = regmap_clear_bits(tps->regmap, TPS6594_REG_RTC_CTRL_1,
67 TPS6594_BIT_GET_TIME);
72 * Copy content of RTC registers to shadow registers or latches to read
73 * a coherent timestamp.
75 return regmap_set_bits(tps->regmap, TPS6594_REG_RTC_CTRL_1,
76 TPS6594_BIT_GET_TIME);
79 static int tps6594_rtc_read_time(struct device *dev, struct rtc_time *tm)
81 unsigned char rtc_data[NUM_TIME_REGS];
82 struct tps6594 *tps = dev_get_drvdata(dev->parent);
85 // Check if RTC is running.
86 ret = regmap_test_bits(tps->regmap, TPS6594_REG_RTC_STATUS,
93 ret = tps6594_rtc_shadow_timestamp(dev, tps);
97 // Read shadowed RTC registers.
98 ret = regmap_bulk_read(tps->regmap, TPS6594_REG_RTC_SECONDS, rtc_data,
103 tm->tm_sec = bcd2bin(rtc_data[0]);
104 tm->tm_min = bcd2bin(rtc_data[1]);
105 tm->tm_hour = bcd2bin(rtc_data[2]);
106 tm->tm_mday = bcd2bin(rtc_data[3]);
107 tm->tm_mon = bcd2bin(rtc_data[4]) - 1;
108 tm->tm_year = bcd2bin(rtc_data[5]) + 100;
109 tm->tm_wday = bcd2bin(rtc_data[6]);
114 static int tps6594_rtc_set_time(struct device *dev, struct rtc_time *tm)
116 unsigned char rtc_data[NUM_TIME_REGS];
117 struct tps6594 *tps = dev_get_drvdata(dev->parent);
120 rtc_data[0] = bin2bcd(tm->tm_sec);
121 rtc_data[1] = bin2bcd(tm->tm_min);
122 rtc_data[2] = bin2bcd(tm->tm_hour);
123 rtc_data[3] = bin2bcd(tm->tm_mday);
124 rtc_data[4] = bin2bcd(tm->tm_mon + 1);
125 rtc_data[5] = bin2bcd(tm->tm_year - 100);
126 rtc_data[6] = bin2bcd(tm->tm_wday);
128 // Stop RTC while updating the RTC time registers.
129 ret = regmap_clear_bits(tps->regmap, TPS6594_REG_RTC_CTRL_1,
130 TPS6594_BIT_STOP_RTC);
134 // Update all the time registers in one shot.
135 ret = regmap_bulk_write(tps->regmap, TPS6594_REG_RTC_SECONDS, rtc_data,
141 return regmap_set_bits(tps->regmap, TPS6594_REG_RTC_CTRL_1,
142 TPS6594_BIT_STOP_RTC);
145 static int tps6594_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
147 unsigned char alarm_data[NUM_TIME_ALARM_REGS];
149 struct tps6594 *tps = dev_get_drvdata(dev->parent);
152 ret = regmap_bulk_read(tps->regmap, TPS6594_REG_ALARM_SECONDS,
153 alarm_data, NUM_TIME_ALARM_REGS);
157 alm->time.tm_sec = bcd2bin(alarm_data[0]);
158 alm->time.tm_min = bcd2bin(alarm_data[1]);
159 alm->time.tm_hour = bcd2bin(alarm_data[2]);
160 alm->time.tm_mday = bcd2bin(alarm_data[3]);
161 alm->time.tm_mon = bcd2bin(alarm_data[4]) - 1;
162 alm->time.tm_year = bcd2bin(alarm_data[5]) + 100;
164 ret = regmap_read(tps->regmap, TPS6594_REG_RTC_INTERRUPTS, &int_val);
168 alm->enabled = int_val & TPS6594_BIT_IT_ALARM;
173 static int tps6594_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
175 unsigned char alarm_data[NUM_TIME_ALARM_REGS];
176 struct tps6594 *tps = dev_get_drvdata(dev->parent);
179 // Disable alarm irq before changing the alarm timestamp.
180 ret = tps6594_rtc_alarm_irq_enable(dev, 0);
184 alarm_data[0] = bin2bcd(alm->time.tm_sec);
185 alarm_data[1] = bin2bcd(alm->time.tm_min);
186 alarm_data[2] = bin2bcd(alm->time.tm_hour);
187 alarm_data[3] = bin2bcd(alm->time.tm_mday);
188 alarm_data[4] = bin2bcd(alm->time.tm_mon + 1);
189 alarm_data[5] = bin2bcd(alm->time.tm_year - 100);
191 // Update all the alarm registers in one shot.
192 ret = regmap_bulk_write(tps->regmap, TPS6594_REG_ALARM_SECONDS,
193 alarm_data, NUM_TIME_ALARM_REGS);
198 ret = tps6594_rtc_alarm_irq_enable(dev, 1);
203 static int tps6594_rtc_set_calibration(struct device *dev, int calibration)
205 struct tps6594 *tps = dev_get_drvdata(dev->parent);
210 * TPS6594 uses two's complement 16 bit value for compensation of RTC
211 * crystal inaccuracies. One time every hour when seconds counter
212 * increments from 0 to 1 compensation value will be added to internal
215 * Valid range for compensation value: [-32767 .. 32767].
217 if (calibration < S16_MIN + 1 || calibration > S16_MAX)
220 value = cpu_to_le16(calibration);
222 // Update all the compensation registers in one shot.
223 ret = regmap_bulk_write(tps->regmap, TPS6594_REG_RTC_COMP_LSB, &value,
228 // Enable automatic compensation.
229 return regmap_set_bits(tps->regmap, TPS6594_REG_RTC_CTRL_1,
230 TPS6594_BIT_AUTO_COMP);
233 static int tps6594_rtc_get_calibration(struct device *dev, int *calibration)
235 struct tps6594 *tps = dev_get_drvdata(dev->parent);
240 ret = regmap_read(tps->regmap, TPS6594_REG_RTC_CTRL_1, &ctrl);
244 // If automatic compensation is not enabled report back zero.
245 if (!(ctrl & TPS6594_BIT_AUTO_COMP)) {
250 ret = regmap_bulk_read(tps->regmap, TPS6594_REG_RTC_COMP_LSB, &value,
255 *calibration = le16_to_cpu(value);
260 static int tps6594_rtc_read_offset(struct device *dev, long *offset)
266 ret = tps6594_rtc_get_calibration(dev, &calibration);
270 // Convert from RTC calibration register format to ppb format.
271 tmp = calibration * PPB_MULT;
274 tmp -= TICKS_PER_HOUR / 2LL;
276 tmp += TICKS_PER_HOUR / 2LL;
277 tmp = div_s64(tmp, TICKS_PER_HOUR);
281 * Computatiion is the reverse operation of the one done in
282 * `tps6594_rtc_set_offset`. The safety remarks applie here too.
286 * Offset value operates in negative way, so swap sign.
287 * See 8.3.10.5, (32768 - COMP_REG).
289 *offset = (long)-tmp;
294 static int tps6594_rtc_set_offset(struct device *dev, long offset)
299 // Make sure offset value is within supported range.
300 if (offset < MIN_OFFSET || offset > MAX_OFFSET)
303 // Convert from ppb format to RTC calibration register format.
305 tmp = offset * TICKS_PER_HOUR;
307 tmp -= PPB_MULT / 2LL;
309 tmp += PPB_MULT / 2LL;
310 tmp = div_s64(tmp, PPB_MULT);
314 * - tmp = offset * TICK_PER_HOUR :
315 * `offset` can't be more than 277774, so `tmp` can't exceed 277774000000000
316 * which is lower than the maximum value in an `s64` (2^63-1). No overflow here.
318 * - tmp += TICK_PER_HOUR / 2LL :
319 * tmp will have a maximum value of 277774117964800 which is still inferior to 2^63-1.
322 // Offset value operates in negative way, so swap sign.
323 calibration = (int)-tmp;
325 return tps6594_rtc_set_calibration(dev, calibration);
328 static irqreturn_t tps6594_rtc_interrupt(int irq, void *rtc)
330 struct device *dev = rtc;
331 struct tps6594 *tps = dev_get_drvdata(dev->parent);
332 struct rtc_device *rtc_dev = dev_get_drvdata(dev);
336 ret = regmap_read(tps->regmap, TPS6594_REG_RTC_STATUS, &rtc_reg);
340 rtc_update_irq(rtc_dev, 1, RTC_IRQF | RTC_AF);
345 static const struct rtc_class_ops tps6594_rtc_ops = {
346 .read_time = tps6594_rtc_read_time,
347 .set_time = tps6594_rtc_set_time,
348 .read_alarm = tps6594_rtc_read_alarm,
349 .set_alarm = tps6594_rtc_set_alarm,
350 .alarm_irq_enable = tps6594_rtc_alarm_irq_enable,
351 .read_offset = tps6594_rtc_read_offset,
352 .set_offset = tps6594_rtc_set_offset,
355 static int tps6594_rtc_probe(struct platform_device *pdev)
357 struct tps6594 *tps = dev_get_drvdata(pdev->dev.parent);
358 struct device *dev = &pdev->dev;
359 struct rtc_device *rtc;
363 rtc = devm_kzalloc(dev, sizeof(*rtc), GFP_KERNEL);
367 rtc = devm_rtc_allocate_device(dev);
371 // Enable crystal oscillator.
372 ret = regmap_set_bits(tps->regmap, TPS6594_REG_RTC_CTRL_2,
373 TPS6594_BIT_XTAL_EN);
377 ret = regmap_test_bits(tps->regmap, TPS6594_REG_RTC_STATUS,
383 ret = regmap_set_bits(tps->regmap, TPS6594_REG_RTC_CTRL_1,
384 TPS6594_BIT_STOP_RTC);
389 * On some boards, a 40 ms delay is needed before BIT_RUN is set.
390 * 80 ms should provide sufficient margin.
395 * RTC should be running now. Check if this is the case.
396 * If not it might be a missing oscillator.
398 ret = regmap_test_bits(tps->regmap, TPS6594_REG_RTC_STATUS,
405 // Stop RTC until first call to `tps6594_rtc_set_time`.
406 ret = regmap_clear_bits(tps->regmap, TPS6594_REG_RTC_CTRL_1,
407 TPS6594_BIT_STOP_RTC);
412 platform_set_drvdata(pdev, rtc);
414 irq = platform_get_irq_byname(pdev, TPS6594_IRQ_NAME_ALARM);
416 return dev_err_probe(dev, irq, "Failed to get irq\n");
418 ret = devm_request_threaded_irq(dev, irq, NULL, tps6594_rtc_interrupt,
419 IRQF_ONESHOT, TPS6594_IRQ_NAME_ALARM,
422 return dev_err_probe(dev, ret,
423 "Failed to request_threaded_irq\n");
425 ret = device_init_wakeup(dev, true);
427 return dev_err_probe(dev, ret,
428 "Failed to init rtc as wakeup source\n");
430 rtc->ops = &tps6594_rtc_ops;
431 rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
432 rtc->range_max = RTC_TIMESTAMP_END_2099;
434 return devm_rtc_register_device(rtc);
437 static const struct platform_device_id tps6594_rtc_id_table[] = {
441 MODULE_DEVICE_TABLE(platform, tps6594_rtc_id_table);
443 static struct platform_driver tps6594_rtc_driver = {
444 .probe = tps6594_rtc_probe,
446 .name = "tps6594-rtc",
448 .id_table = tps6594_rtc_id_table,
451 module_platform_driver(tps6594_rtc_driver);
453 MODULE_DESCRIPTION("TPS6594 RTC driver");
454 MODULE_LICENSE("GPL");