1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * rtc-tps65910.c -- TPS65910 Real Time Clock interface
5 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
6 * Author: Venu Byravarasu <vbyravarasu@nvidia.com>
8 * Based on original TI driver rtc-twl.c
9 * Copyright (C) 2007 MontaVista Software, Inc
10 * Author: Alexandre Rusev <source@mvista.com>
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/types.h>
18 #include <linux/rtc.h>
19 #include <linux/bcd.h>
20 #include <linux/math64.h>
21 #include <linux/property.h>
22 #include <linux/platform_device.h>
23 #include <linux/interrupt.h>
24 #include <linux/mfd/tps65910.h>
27 struct rtc_device
*rtc
;
31 /* Total number of RTC registers needed to set time*/
32 #define NUM_TIME_REGS (TPS65910_YEARS - TPS65910_SECONDS + 1)
34 /* Total number of RTC registers needed to set compensation registers */
35 #define NUM_COMP_REGS (TPS65910_RTC_COMP_MSB - TPS65910_RTC_COMP_LSB + 1)
37 /* Min and max values supported with 'offset' interface (swapped sign) */
38 #define MIN_OFFSET (-277761)
39 #define MAX_OFFSET (277778)
41 /* Number of ticks per hour */
42 #define TICKS_PER_HOUR (32768 * 3600)
44 /* Multiplier for ppb conversions */
45 #define PPB_MULT (1000000000LL)
47 static int tps65910_rtc_alarm_irq_enable(struct device
*dev
,
50 struct tps65910
*tps
= dev_get_drvdata(dev
->parent
);
54 val
= TPS65910_RTC_INTERRUPTS_IT_ALARM
;
56 return regmap_write(tps
->regmap
, TPS65910_RTC_INTERRUPTS
, val
);
60 * Gets current tps65910 RTC time and date parameters.
62 * The RTC's time/alarm representation is not what gmtime(3) requires
65 * - Months are 1..12 vs Linux 0-11
66 * - Years are 0..99 vs Linux 1900..N (we assume 21st century)
68 static int tps65910_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
70 unsigned char rtc_data
[NUM_TIME_REGS
];
71 struct tps65910
*tps
= dev_get_drvdata(dev
->parent
);
74 /* Copy RTC counting registers to static registers or latches */
75 ret
= regmap_update_bits(tps
->regmap
, TPS65910_RTC_CTRL
,
76 TPS65910_RTC_CTRL_GET_TIME
, TPS65910_RTC_CTRL_GET_TIME
);
78 dev_err(dev
, "RTC CTRL reg update failed with err:%d\n", ret
);
82 ret
= regmap_bulk_read(tps
->regmap
, TPS65910_SECONDS
, rtc_data
,
85 dev_err(dev
, "reading from RTC failed with err:%d\n", ret
);
89 tm
->tm_sec
= bcd2bin(rtc_data
[0]);
90 tm
->tm_min
= bcd2bin(rtc_data
[1]);
91 tm
->tm_hour
= bcd2bin(rtc_data
[2]);
92 tm
->tm_mday
= bcd2bin(rtc_data
[3]);
93 tm
->tm_mon
= bcd2bin(rtc_data
[4]) - 1;
94 tm
->tm_year
= bcd2bin(rtc_data
[5]) + 100;
99 static int tps65910_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
101 unsigned char rtc_data
[NUM_TIME_REGS
];
102 struct tps65910
*tps
= dev_get_drvdata(dev
->parent
);
105 rtc_data
[0] = bin2bcd(tm
->tm_sec
);
106 rtc_data
[1] = bin2bcd(tm
->tm_min
);
107 rtc_data
[2] = bin2bcd(tm
->tm_hour
);
108 rtc_data
[3] = bin2bcd(tm
->tm_mday
);
109 rtc_data
[4] = bin2bcd(tm
->tm_mon
+ 1);
110 rtc_data
[5] = bin2bcd(tm
->tm_year
- 100);
112 /* Stop RTC while updating the RTC time registers */
113 ret
= regmap_update_bits(tps
->regmap
, TPS65910_RTC_CTRL
,
114 TPS65910_RTC_CTRL_STOP_RTC
, 0);
116 dev_err(dev
, "RTC stop failed with err:%d\n", ret
);
120 /* update all the time registers in one shot */
121 ret
= regmap_bulk_write(tps
->regmap
, TPS65910_SECONDS
, rtc_data
,
124 dev_err(dev
, "rtc_set_time error %d\n", ret
);
129 ret
= regmap_update_bits(tps
->regmap
, TPS65910_RTC_CTRL
,
130 TPS65910_RTC_CTRL_STOP_RTC
, 1);
132 dev_err(dev
, "RTC start failed with err:%d\n", ret
);
138 * Gets current tps65910 RTC alarm time.
140 static int tps65910_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alm
)
142 unsigned char alarm_data
[NUM_TIME_REGS
];
144 struct tps65910
*tps
= dev_get_drvdata(dev
->parent
);
147 ret
= regmap_bulk_read(tps
->regmap
, TPS65910_ALARM_SECONDS
, alarm_data
,
150 dev_err(dev
, "rtc_read_alarm error %d\n", ret
);
154 alm
->time
.tm_sec
= bcd2bin(alarm_data
[0]);
155 alm
->time
.tm_min
= bcd2bin(alarm_data
[1]);
156 alm
->time
.tm_hour
= bcd2bin(alarm_data
[2]);
157 alm
->time
.tm_mday
= bcd2bin(alarm_data
[3]);
158 alm
->time
.tm_mon
= bcd2bin(alarm_data
[4]) - 1;
159 alm
->time
.tm_year
= bcd2bin(alarm_data
[5]) + 100;
161 ret
= regmap_read(tps
->regmap
, TPS65910_RTC_INTERRUPTS
, &int_val
);
165 if (int_val
& TPS65910_RTC_INTERRUPTS_IT_ALARM
)
171 static int tps65910_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alm
)
173 unsigned char alarm_data
[NUM_TIME_REGS
];
174 struct tps65910
*tps
= dev_get_drvdata(dev
->parent
);
177 ret
= tps65910_rtc_alarm_irq_enable(dev
, 0);
181 alarm_data
[0] = bin2bcd(alm
->time
.tm_sec
);
182 alarm_data
[1] = bin2bcd(alm
->time
.tm_min
);
183 alarm_data
[2] = bin2bcd(alm
->time
.tm_hour
);
184 alarm_data
[3] = bin2bcd(alm
->time
.tm_mday
);
185 alarm_data
[4] = bin2bcd(alm
->time
.tm_mon
+ 1);
186 alarm_data
[5] = bin2bcd(alm
->time
.tm_year
- 100);
188 /* update all the alarm registers in one shot */
189 ret
= regmap_bulk_write(tps
->regmap
, TPS65910_ALARM_SECONDS
,
190 alarm_data
, NUM_TIME_REGS
);
192 dev_err(dev
, "rtc_set_alarm error %d\n", ret
);
197 ret
= tps65910_rtc_alarm_irq_enable(dev
, 1);
202 static int tps65910_rtc_set_calibration(struct device
*dev
, int calibration
)
204 unsigned char comp_data
[NUM_COMP_REGS
];
205 struct tps65910
*tps
= dev_get_drvdata(dev
->parent
);
210 * TPS65910 uses two's complement 16 bit value for compensation for 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 * Compensation value 0x7FFF is prohibited value.
217 * Valid range for compensation value: [-32768 .. 32766]
219 if ((calibration
< -32768) || (calibration
> 32766)) {
220 dev_err(dev
, "RTC calibration value out of range: %d\n",
225 value
= (s16
)calibration
;
227 comp_data
[0] = (u16
)value
& 0xFF;
228 comp_data
[1] = ((u16
)value
>> 8) & 0xFF;
230 /* Update all the compensation registers in one shot */
231 ret
= regmap_bulk_write(tps
->regmap
, TPS65910_RTC_COMP_LSB
,
232 comp_data
, NUM_COMP_REGS
);
234 dev_err(dev
, "rtc_set_calibration error: %d\n", ret
);
238 /* Enable automatic compensation */
239 ret
= regmap_update_bits(tps
->regmap
, TPS65910_RTC_CTRL
,
240 TPS65910_RTC_CTRL_AUTO_COMP
, TPS65910_RTC_CTRL_AUTO_COMP
);
242 dev_err(dev
, "auto_comp enable failed with error: %d\n", ret
);
247 static int tps65910_rtc_get_calibration(struct device
*dev
, int *calibration
)
249 unsigned char comp_data
[NUM_COMP_REGS
];
250 struct tps65910
*tps
= dev_get_drvdata(dev
->parent
);
255 ret
= regmap_read(tps
->regmap
, TPS65910_RTC_CTRL
, &ctrl
);
259 /* If automatic compensation is not enabled report back zero */
260 if (!(ctrl
& TPS65910_RTC_CTRL_AUTO_COMP
)) {
265 ret
= regmap_bulk_read(tps
->regmap
, TPS65910_RTC_COMP_LSB
, comp_data
,
268 dev_err(dev
, "rtc_get_calibration error: %d\n", ret
);
272 value
= (u16
)comp_data
[0] | ((u16
)comp_data
[1] << 8);
274 *calibration
= (s16
)value
;
279 static int tps65910_read_offset(struct device
*dev
, long *offset
)
285 ret
= tps65910_rtc_get_calibration(dev
, &calibration
);
289 /* Convert from RTC calibration register format to ppb format */
290 tmp
= calibration
* (s64
)PPB_MULT
;
292 tmp
-= TICKS_PER_HOUR
/ 2LL;
294 tmp
+= TICKS_PER_HOUR
/ 2LL;
295 tmp
= div_s64(tmp
, TICKS_PER_HOUR
);
297 /* Offset value operates in negative way, so swap sign */
298 *offset
= (long)-tmp
;
303 static int tps65910_set_offset(struct device
*dev
, long offset
)
309 /* Make sure offset value is within supported range */
310 if (offset
< MIN_OFFSET
|| offset
> MAX_OFFSET
)
313 /* Convert from ppb format to RTC calibration register format */
314 tmp
= offset
* (s64
)TICKS_PER_HOUR
;
316 tmp
-= PPB_MULT
/ 2LL;
318 tmp
+= PPB_MULT
/ 2LL;
319 tmp
= div_s64(tmp
, PPB_MULT
);
321 /* Offset value operates in negative way, so swap sign */
322 calibration
= (int)-tmp
;
324 ret
= tps65910_rtc_set_calibration(dev
, calibration
);
329 static irqreturn_t
tps65910_rtc_interrupt(int irq
, void *rtc
)
331 struct device
*dev
= rtc
;
332 unsigned long events
= 0;
333 struct tps65910
*tps
= dev_get_drvdata(dev
->parent
);
334 struct tps65910_rtc
*tps_rtc
= dev_get_drvdata(dev
);
338 ret
= regmap_read(tps
->regmap
, TPS65910_RTC_STATUS
, &rtc_reg
);
342 if (rtc_reg
& TPS65910_RTC_STATUS_ALARM
)
343 events
= RTC_IRQF
| RTC_AF
;
345 ret
= regmap_write(tps
->regmap
, TPS65910_RTC_STATUS
, rtc_reg
);
349 /* Notify RTC core on event */
350 rtc_update_irq(tps_rtc
->rtc
, 1, events
);
355 static const struct rtc_class_ops tps65910_rtc_ops
= {
356 .read_time
= tps65910_rtc_read_time
,
357 .set_time
= tps65910_rtc_set_time
,
358 .read_alarm
= tps65910_rtc_read_alarm
,
359 .set_alarm
= tps65910_rtc_set_alarm
,
360 .alarm_irq_enable
= tps65910_rtc_alarm_irq_enable
,
361 .read_offset
= tps65910_read_offset
,
362 .set_offset
= tps65910_set_offset
,
365 static int tps65910_rtc_probe(struct platform_device
*pdev
)
367 struct tps65910
*tps65910
= NULL
;
368 struct tps65910_rtc
*tps_rtc
= NULL
;
373 tps65910
= dev_get_drvdata(pdev
->dev
.parent
);
375 tps_rtc
= devm_kzalloc(&pdev
->dev
, sizeof(struct tps65910_rtc
),
380 tps_rtc
->rtc
= devm_rtc_allocate_device(&pdev
->dev
);
381 if (IS_ERR(tps_rtc
->rtc
))
382 return PTR_ERR(tps_rtc
->rtc
);
384 /* Clear pending interrupts */
385 ret
= regmap_read(tps65910
->regmap
, TPS65910_RTC_STATUS
, &rtc_reg
);
389 ret
= regmap_write(tps65910
->regmap
, TPS65910_RTC_STATUS
, rtc_reg
);
393 dev_dbg(&pdev
->dev
, "Enabling rtc-tps65910.\n");
395 /* Enable RTC digital power domain */
396 ret
= regmap_update_bits(tps65910
->regmap
, TPS65910_DEVCTRL
,
397 DEVCTRL_RTC_PWDN_MASK
, 0 << DEVCTRL_RTC_PWDN_SHIFT
);
401 rtc_reg
= TPS65910_RTC_CTRL_STOP_RTC
;
402 ret
= regmap_write(tps65910
->regmap
, TPS65910_RTC_CTRL
, rtc_reg
);
406 platform_set_drvdata(pdev
, tps_rtc
);
408 irq
= platform_get_irq(pdev
, 0);
412 ret
= devm_request_threaded_irq(&pdev
->dev
, irq
, NULL
,
413 tps65910_rtc_interrupt
, IRQF_TRIGGER_LOW
,
414 dev_name(&pdev
->dev
), &pdev
->dev
);
420 if (device_property_present(tps65910
->dev
, "wakeup-source"))
421 device_init_wakeup(&pdev
->dev
, 1);
423 device_set_wakeup_capable(&pdev
->dev
, 1);
425 clear_bit(RTC_FEATURE_ALARM
, tps_rtc
->rtc
->features
);
428 tps_rtc
->rtc
->ops
= &tps65910_rtc_ops
;
429 tps_rtc
->rtc
->range_min
= RTC_TIMESTAMP_BEGIN_2000
;
430 tps_rtc
->rtc
->range_max
= RTC_TIMESTAMP_END_2099
;
432 return devm_rtc_register_device(tps_rtc
->rtc
);
435 #ifdef CONFIG_PM_SLEEP
436 static int tps65910_rtc_suspend(struct device
*dev
)
438 struct tps65910_rtc
*tps_rtc
= dev_get_drvdata(dev
);
440 if (device_may_wakeup(dev
))
441 enable_irq_wake(tps_rtc
->irq
);
445 static int tps65910_rtc_resume(struct device
*dev
)
447 struct tps65910_rtc
*tps_rtc
= dev_get_drvdata(dev
);
449 if (device_may_wakeup(dev
))
450 disable_irq_wake(tps_rtc
->irq
);
455 static SIMPLE_DEV_PM_OPS(tps65910_rtc_pm_ops
, tps65910_rtc_suspend
,
456 tps65910_rtc_resume
);
458 static struct platform_driver tps65910_rtc_driver
= {
459 .probe
= tps65910_rtc_probe
,
461 .name
= "tps65910-rtc",
462 .pm
= &tps65910_rtc_pm_ops
,
466 module_platform_driver(tps65910_rtc_driver
);
467 MODULE_ALIAS("platform:tps65910-rtc");
468 MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
469 MODULE_DESCRIPTION("TI TPS65910 RTC driver");
470 MODULE_LICENSE("GPL");