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/platform_device.h>
22 #include <linux/interrupt.h>
23 #include <linux/mfd/tps65910.h>
26 struct rtc_device
*rtc
;
30 /* Total number of RTC registers needed to set time*/
31 #define NUM_TIME_REGS (TPS65910_YEARS - TPS65910_SECONDS + 1)
33 /* Total number of RTC registers needed to set compensation registers */
34 #define NUM_COMP_REGS (TPS65910_RTC_COMP_MSB - TPS65910_RTC_COMP_LSB + 1)
36 /* Min and max values supported with 'offset' interface (swapped sign) */
37 #define MIN_OFFSET (-277761)
38 #define MAX_OFFSET (277778)
40 /* Number of ticks per hour */
41 #define TICKS_PER_HOUR (32768 * 3600)
43 /* Multiplier for ppb conversions */
44 #define PPB_MULT (1000000000LL)
46 static int tps65910_rtc_alarm_irq_enable(struct device
*dev
,
49 struct tps65910
*tps
= dev_get_drvdata(dev
->parent
);
53 val
= TPS65910_RTC_INTERRUPTS_IT_ALARM
;
55 return regmap_write(tps
->regmap
, TPS65910_RTC_INTERRUPTS
, val
);
59 * Gets current tps65910 RTC time and date parameters.
61 * The RTC's time/alarm representation is not what gmtime(3) requires
64 * - Months are 1..12 vs Linux 0-11
65 * - Years are 0..99 vs Linux 1900..N (we assume 21st century)
67 static int tps65910_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
69 unsigned char rtc_data
[NUM_TIME_REGS
];
70 struct tps65910
*tps
= dev_get_drvdata(dev
->parent
);
73 /* Copy RTC counting registers to static registers or latches */
74 ret
= regmap_update_bits(tps
->regmap
, TPS65910_RTC_CTRL
,
75 TPS65910_RTC_CTRL_GET_TIME
, TPS65910_RTC_CTRL_GET_TIME
);
77 dev_err(dev
, "RTC CTRL reg update failed with err:%d\n", ret
);
81 ret
= regmap_bulk_read(tps
->regmap
, TPS65910_SECONDS
, rtc_data
,
84 dev_err(dev
, "reading from RTC failed with err:%d\n", ret
);
88 tm
->tm_sec
= bcd2bin(rtc_data
[0]);
89 tm
->tm_min
= bcd2bin(rtc_data
[1]);
90 tm
->tm_hour
= bcd2bin(rtc_data
[2]);
91 tm
->tm_mday
= bcd2bin(rtc_data
[3]);
92 tm
->tm_mon
= bcd2bin(rtc_data
[4]) - 1;
93 tm
->tm_year
= bcd2bin(rtc_data
[5]) + 100;
98 static int tps65910_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
100 unsigned char rtc_data
[NUM_TIME_REGS
];
101 struct tps65910
*tps
= dev_get_drvdata(dev
->parent
);
104 rtc_data
[0] = bin2bcd(tm
->tm_sec
);
105 rtc_data
[1] = bin2bcd(tm
->tm_min
);
106 rtc_data
[2] = bin2bcd(tm
->tm_hour
);
107 rtc_data
[3] = bin2bcd(tm
->tm_mday
);
108 rtc_data
[4] = bin2bcd(tm
->tm_mon
+ 1);
109 rtc_data
[5] = bin2bcd(tm
->tm_year
- 100);
111 /* Stop RTC while updating the RTC time registers */
112 ret
= regmap_update_bits(tps
->regmap
, TPS65910_RTC_CTRL
,
113 TPS65910_RTC_CTRL_STOP_RTC
, 0);
115 dev_err(dev
, "RTC stop failed with err:%d\n", ret
);
119 /* update all the time registers in one shot */
120 ret
= regmap_bulk_write(tps
->regmap
, TPS65910_SECONDS
, rtc_data
,
123 dev_err(dev
, "rtc_set_time error %d\n", ret
);
128 ret
= regmap_update_bits(tps
->regmap
, TPS65910_RTC_CTRL
,
129 TPS65910_RTC_CTRL_STOP_RTC
, 1);
131 dev_err(dev
, "RTC start failed with err:%d\n", ret
);
137 * Gets current tps65910 RTC alarm time.
139 static int tps65910_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alm
)
141 unsigned char alarm_data
[NUM_TIME_REGS
];
143 struct tps65910
*tps
= dev_get_drvdata(dev
->parent
);
146 ret
= regmap_bulk_read(tps
->regmap
, TPS65910_ALARM_SECONDS
, alarm_data
,
149 dev_err(dev
, "rtc_read_alarm error %d\n", ret
);
153 alm
->time
.tm_sec
= bcd2bin(alarm_data
[0]);
154 alm
->time
.tm_min
= bcd2bin(alarm_data
[1]);
155 alm
->time
.tm_hour
= bcd2bin(alarm_data
[2]);
156 alm
->time
.tm_mday
= bcd2bin(alarm_data
[3]);
157 alm
->time
.tm_mon
= bcd2bin(alarm_data
[4]) - 1;
158 alm
->time
.tm_year
= bcd2bin(alarm_data
[5]) + 100;
160 ret
= regmap_read(tps
->regmap
, TPS65910_RTC_INTERRUPTS
, &int_val
);
164 if (int_val
& TPS65910_RTC_INTERRUPTS_IT_ALARM
)
170 static int tps65910_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alm
)
172 unsigned char alarm_data
[NUM_TIME_REGS
];
173 struct tps65910
*tps
= dev_get_drvdata(dev
->parent
);
176 ret
= tps65910_rtc_alarm_irq_enable(dev
, 0);
180 alarm_data
[0] = bin2bcd(alm
->time
.tm_sec
);
181 alarm_data
[1] = bin2bcd(alm
->time
.tm_min
);
182 alarm_data
[2] = bin2bcd(alm
->time
.tm_hour
);
183 alarm_data
[3] = bin2bcd(alm
->time
.tm_mday
);
184 alarm_data
[4] = bin2bcd(alm
->time
.tm_mon
+ 1);
185 alarm_data
[5] = bin2bcd(alm
->time
.tm_year
- 100);
187 /* update all the alarm registers in one shot */
188 ret
= regmap_bulk_write(tps
->regmap
, TPS65910_ALARM_SECONDS
,
189 alarm_data
, NUM_TIME_REGS
);
191 dev_err(dev
, "rtc_set_alarm error %d\n", ret
);
196 ret
= tps65910_rtc_alarm_irq_enable(dev
, 1);
201 static int tps65910_rtc_set_calibration(struct device
*dev
, int calibration
)
203 unsigned char comp_data
[NUM_COMP_REGS
];
204 struct tps65910
*tps
= dev_get_drvdata(dev
->parent
);
209 * TPS65910 uses two's complement 16 bit value for compensation for RTC
210 * crystal inaccuracies. One time every hour when seconds counter
211 * increments from 0 to 1 compensation value will be added to internal
214 * Compensation value 0x7FFF is prohibited value.
216 * Valid range for compensation value: [-32768 .. 32766]
218 if ((calibration
< -32768) || (calibration
> 32766)) {
219 dev_err(dev
, "RTC calibration value out of range: %d\n",
224 value
= (s16
)calibration
;
226 comp_data
[0] = (u16
)value
& 0xFF;
227 comp_data
[1] = ((u16
)value
>> 8) & 0xFF;
229 /* Update all the compensation registers in one shot */
230 ret
= regmap_bulk_write(tps
->regmap
, TPS65910_RTC_COMP_LSB
,
231 comp_data
, NUM_COMP_REGS
);
233 dev_err(dev
, "rtc_set_calibration error: %d\n", ret
);
237 /* Enable automatic compensation */
238 ret
= regmap_update_bits(tps
->regmap
, TPS65910_RTC_CTRL
,
239 TPS65910_RTC_CTRL_AUTO_COMP
, TPS65910_RTC_CTRL_AUTO_COMP
);
241 dev_err(dev
, "auto_comp enable failed with error: %d\n", ret
);
246 static int tps65910_rtc_get_calibration(struct device
*dev
, int *calibration
)
248 unsigned char comp_data
[NUM_COMP_REGS
];
249 struct tps65910
*tps
= dev_get_drvdata(dev
->parent
);
254 ret
= regmap_read(tps
->regmap
, TPS65910_RTC_CTRL
, &ctrl
);
258 /* If automatic compensation is not enabled report back zero */
259 if (!(ctrl
& TPS65910_RTC_CTRL_AUTO_COMP
)) {
264 ret
= regmap_bulk_read(tps
->regmap
, TPS65910_RTC_COMP_LSB
, comp_data
,
267 dev_err(dev
, "rtc_get_calibration error: %d\n", ret
);
271 value
= (u16
)comp_data
[0] | ((u16
)comp_data
[1] << 8);
273 *calibration
= (s16
)value
;
278 static int tps65910_read_offset(struct device
*dev
, long *offset
)
284 ret
= tps65910_rtc_get_calibration(dev
, &calibration
);
288 /* Convert from RTC calibration register format to ppb format */
289 tmp
= calibration
* (s64
)PPB_MULT
;
291 tmp
-= TICKS_PER_HOUR
/ 2LL;
293 tmp
+= TICKS_PER_HOUR
/ 2LL;
294 tmp
= div_s64(tmp
, TICKS_PER_HOUR
);
296 /* Offset value operates in negative way, so swap sign */
297 *offset
= (long)-tmp
;
302 static int tps65910_set_offset(struct device
*dev
, long offset
)
308 /* Make sure offset value is within supported range */
309 if (offset
< MIN_OFFSET
|| offset
> MAX_OFFSET
)
312 /* Convert from ppb format to RTC calibration register format */
313 tmp
= offset
* (s64
)TICKS_PER_HOUR
;
315 tmp
-= PPB_MULT
/ 2LL;
317 tmp
+= PPB_MULT
/ 2LL;
318 tmp
= div_s64(tmp
, PPB_MULT
);
320 /* Offset value operates in negative way, so swap sign */
321 calibration
= (int)-tmp
;
323 ret
= tps65910_rtc_set_calibration(dev
, calibration
);
328 static irqreturn_t
tps65910_rtc_interrupt(int irq
, void *rtc
)
330 struct device
*dev
= rtc
;
331 unsigned long events
= 0;
332 struct tps65910
*tps
= dev_get_drvdata(dev
->parent
);
333 struct tps65910_rtc
*tps_rtc
= dev_get_drvdata(dev
);
337 ret
= regmap_read(tps
->regmap
, TPS65910_RTC_STATUS
, &rtc_reg
);
341 if (rtc_reg
& TPS65910_RTC_STATUS_ALARM
)
342 events
= RTC_IRQF
| RTC_AF
;
344 ret
= regmap_write(tps
->regmap
, TPS65910_RTC_STATUS
, rtc_reg
);
348 /* Notify RTC core on event */
349 rtc_update_irq(tps_rtc
->rtc
, 1, events
);
354 static const struct rtc_class_ops tps65910_rtc_ops
= {
355 .read_time
= tps65910_rtc_read_time
,
356 .set_time
= tps65910_rtc_set_time
,
357 .read_alarm
= tps65910_rtc_read_alarm
,
358 .set_alarm
= tps65910_rtc_set_alarm
,
359 .alarm_irq_enable
= tps65910_rtc_alarm_irq_enable
,
360 .read_offset
= tps65910_read_offset
,
361 .set_offset
= tps65910_set_offset
,
364 static const struct rtc_class_ops tps65910_rtc_ops_noirq
= {
365 .read_time
= tps65910_rtc_read_time
,
366 .set_time
= tps65910_rtc_set_time
,
367 .read_offset
= tps65910_read_offset
,
368 .set_offset
= tps65910_set_offset
,
371 static int tps65910_rtc_probe(struct platform_device
*pdev
)
373 struct tps65910
*tps65910
= NULL
;
374 struct tps65910_rtc
*tps_rtc
= NULL
;
379 tps65910
= dev_get_drvdata(pdev
->dev
.parent
);
381 tps_rtc
= devm_kzalloc(&pdev
->dev
, sizeof(struct tps65910_rtc
),
386 tps_rtc
->rtc
= devm_rtc_allocate_device(&pdev
->dev
);
387 if (IS_ERR(tps_rtc
->rtc
))
388 return PTR_ERR(tps_rtc
->rtc
);
390 /* Clear pending interrupts */
391 ret
= regmap_read(tps65910
->regmap
, TPS65910_RTC_STATUS
, &rtc_reg
);
395 ret
= regmap_write(tps65910
->regmap
, TPS65910_RTC_STATUS
, rtc_reg
);
399 dev_dbg(&pdev
->dev
, "Enabling rtc-tps65910.\n");
401 /* Enable RTC digital power domain */
402 ret
= regmap_update_bits(tps65910
->regmap
, TPS65910_DEVCTRL
,
403 DEVCTRL_RTC_PWDN_MASK
, 0 << DEVCTRL_RTC_PWDN_SHIFT
);
407 rtc_reg
= TPS65910_RTC_CTRL_STOP_RTC
;
408 ret
= regmap_write(tps65910
->regmap
, TPS65910_RTC_CTRL
, rtc_reg
);
412 platform_set_drvdata(pdev
, tps_rtc
);
414 irq
= platform_get_irq(pdev
, 0);
416 dev_warn(&pdev
->dev
, "Wake up is not possible as irq = %d\n",
421 ret
= devm_request_threaded_irq(&pdev
->dev
, irq
, NULL
,
422 tps65910_rtc_interrupt
, IRQF_TRIGGER_LOW
,
423 dev_name(&pdev
->dev
), &pdev
->dev
);
429 device_set_wakeup_capable(&pdev
->dev
, 1);
430 tps_rtc
->rtc
->ops
= &tps65910_rtc_ops
;
432 tps_rtc
->rtc
->ops
= &tps65910_rtc_ops_noirq
;
434 tps_rtc
->rtc
->range_min
= RTC_TIMESTAMP_BEGIN_2000
;
435 tps_rtc
->rtc
->range_max
= RTC_TIMESTAMP_END_2099
;
437 return devm_rtc_register_device(tps_rtc
->rtc
);
440 #ifdef CONFIG_PM_SLEEP
441 static int tps65910_rtc_suspend(struct device
*dev
)
443 struct tps65910_rtc
*tps_rtc
= dev_get_drvdata(dev
);
445 if (device_may_wakeup(dev
))
446 enable_irq_wake(tps_rtc
->irq
);
450 static int tps65910_rtc_resume(struct device
*dev
)
452 struct tps65910_rtc
*tps_rtc
= dev_get_drvdata(dev
);
454 if (device_may_wakeup(dev
))
455 disable_irq_wake(tps_rtc
->irq
);
460 static SIMPLE_DEV_PM_OPS(tps65910_rtc_pm_ops
, tps65910_rtc_suspend
,
461 tps65910_rtc_resume
);
463 static struct platform_driver tps65910_rtc_driver
= {
464 .probe
= tps65910_rtc_probe
,
466 .name
= "tps65910-rtc",
467 .pm
= &tps65910_rtc_pm_ops
,
471 module_platform_driver(tps65910_rtc_driver
);
472 MODULE_ALIAS("platform:rtc-tps65910");
473 MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
474 MODULE_LICENSE("GPL");