1 // SPDX-License-Identifier: GPL-2.0-only
3 * rtc-palmas.c -- Palmas Real Time Clock driver.
5 * RTC driver for TI Palma series devices like TPS65913,
6 * TPS65914 power management IC.
8 * Copyright (c) 2012, NVIDIA Corporation.
10 * Author: Laxman Dewangan <ldewangan@nvidia.com>
13 #include <linux/bcd.h>
14 #include <linux/errno.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
17 #include <linux/kernel.h>
18 #include <linux/mfd/palmas.h>
19 #include <linux/module.h>
21 #include <linux/rtc.h>
22 #include <linux/types.h>
23 #include <linux/platform_device.h>
27 struct rtc_device
*rtc
;
32 /* Total number of RTC registers needed to set time*/
33 #define PALMAS_NUM_TIME_REGS (PALMAS_YEARS_REG - PALMAS_SECONDS_REG + 1)
35 static int palmas_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
37 unsigned char rtc_data
[PALMAS_NUM_TIME_REGS
];
38 struct palmas
*palmas
= dev_get_drvdata(dev
->parent
);
41 /* Copy RTC counting registers to static registers or latches */
42 ret
= palmas_update_bits(palmas
, PALMAS_RTC_BASE
, PALMAS_RTC_CTRL_REG
,
43 PALMAS_RTC_CTRL_REG_GET_TIME
, PALMAS_RTC_CTRL_REG_GET_TIME
);
45 dev_err(dev
, "RTC CTRL reg update failed, err: %d\n", ret
);
49 ret
= palmas_bulk_read(palmas
, PALMAS_RTC_BASE
, PALMAS_SECONDS_REG
,
50 rtc_data
, PALMAS_NUM_TIME_REGS
);
52 dev_err(dev
, "RTC_SECONDS reg read failed, err = %d\n", ret
);
56 tm
->tm_sec
= bcd2bin(rtc_data
[0]);
57 tm
->tm_min
= bcd2bin(rtc_data
[1]);
58 tm
->tm_hour
= bcd2bin(rtc_data
[2]);
59 tm
->tm_mday
= bcd2bin(rtc_data
[3]);
60 tm
->tm_mon
= bcd2bin(rtc_data
[4]) - 1;
61 tm
->tm_year
= bcd2bin(rtc_data
[5]) + 100;
66 static int palmas_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
68 unsigned char rtc_data
[PALMAS_NUM_TIME_REGS
];
69 struct palmas
*palmas
= dev_get_drvdata(dev
->parent
);
72 rtc_data
[0] = bin2bcd(tm
->tm_sec
);
73 rtc_data
[1] = bin2bcd(tm
->tm_min
);
74 rtc_data
[2] = bin2bcd(tm
->tm_hour
);
75 rtc_data
[3] = bin2bcd(tm
->tm_mday
);
76 rtc_data
[4] = bin2bcd(tm
->tm_mon
+ 1);
77 rtc_data
[5] = bin2bcd(tm
->tm_year
- 100);
79 /* Stop RTC while updating the RTC time registers */
80 ret
= palmas_update_bits(palmas
, PALMAS_RTC_BASE
, PALMAS_RTC_CTRL_REG
,
81 PALMAS_RTC_CTRL_REG_STOP_RTC
, 0);
83 dev_err(dev
, "RTC stop failed, err = %d\n", ret
);
87 ret
= palmas_bulk_write(palmas
, PALMAS_RTC_BASE
, PALMAS_SECONDS_REG
,
88 rtc_data
, PALMAS_NUM_TIME_REGS
);
90 dev_err(dev
, "RTC_SECONDS reg write failed, err = %d\n", ret
);
95 ret
= palmas_update_bits(palmas
, PALMAS_RTC_BASE
, PALMAS_RTC_CTRL_REG
,
96 PALMAS_RTC_CTRL_REG_STOP_RTC
, PALMAS_RTC_CTRL_REG_STOP_RTC
);
98 dev_err(dev
, "RTC start failed, err = %d\n", ret
);
102 static int palmas_rtc_alarm_irq_enable(struct device
*dev
, unsigned enabled
)
104 struct palmas
*palmas
= dev_get_drvdata(dev
->parent
);
107 val
= enabled
? PALMAS_RTC_INTERRUPTS_REG_IT_ALARM
: 0;
108 return palmas_write(palmas
, PALMAS_RTC_BASE
,
109 PALMAS_RTC_INTERRUPTS_REG
, val
);
112 static int palmas_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alm
)
114 unsigned char alarm_data
[PALMAS_NUM_TIME_REGS
];
116 struct palmas
*palmas
= dev_get_drvdata(dev
->parent
);
119 ret
= palmas_bulk_read(palmas
, PALMAS_RTC_BASE
,
120 PALMAS_ALARM_SECONDS_REG
,
121 alarm_data
, PALMAS_NUM_TIME_REGS
);
123 dev_err(dev
, "RTC_ALARM_SECONDS read failed, err = %d\n", ret
);
127 alm
->time
.tm_sec
= bcd2bin(alarm_data
[0]);
128 alm
->time
.tm_min
= bcd2bin(alarm_data
[1]);
129 alm
->time
.tm_hour
= bcd2bin(alarm_data
[2]);
130 alm
->time
.tm_mday
= bcd2bin(alarm_data
[3]);
131 alm
->time
.tm_mon
= bcd2bin(alarm_data
[4]) - 1;
132 alm
->time
.tm_year
= bcd2bin(alarm_data
[5]) + 100;
134 ret
= palmas_read(palmas
, PALMAS_RTC_BASE
, PALMAS_RTC_INTERRUPTS_REG
,
137 dev_err(dev
, "RTC_INTERRUPTS reg read failed, err = %d\n", ret
);
141 if (int_val
& PALMAS_RTC_INTERRUPTS_REG_IT_ALARM
)
146 static int palmas_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alm
)
148 unsigned char alarm_data
[PALMAS_NUM_TIME_REGS
];
149 struct palmas
*palmas
= dev_get_drvdata(dev
->parent
);
152 ret
= palmas_rtc_alarm_irq_enable(dev
, 0);
154 dev_err(dev
, "Disable RTC alarm failed\n");
158 alarm_data
[0] = bin2bcd(alm
->time
.tm_sec
);
159 alarm_data
[1] = bin2bcd(alm
->time
.tm_min
);
160 alarm_data
[2] = bin2bcd(alm
->time
.tm_hour
);
161 alarm_data
[3] = bin2bcd(alm
->time
.tm_mday
);
162 alarm_data
[4] = bin2bcd(alm
->time
.tm_mon
+ 1);
163 alarm_data
[5] = bin2bcd(alm
->time
.tm_year
- 100);
165 ret
= palmas_bulk_write(palmas
, PALMAS_RTC_BASE
,
166 PALMAS_ALARM_SECONDS_REG
, alarm_data
, PALMAS_NUM_TIME_REGS
);
168 dev_err(dev
, "ALARM_SECONDS_REG write failed, err = %d\n", ret
);
173 ret
= palmas_rtc_alarm_irq_enable(dev
, 1);
177 static int palmas_clear_interrupts(struct device
*dev
)
179 struct palmas
*palmas
= dev_get_drvdata(dev
->parent
);
180 unsigned int rtc_reg
;
183 ret
= palmas_read(palmas
, PALMAS_RTC_BASE
, PALMAS_RTC_STATUS_REG
,
186 dev_err(dev
, "RTC_STATUS read failed, err = %d\n", ret
);
190 ret
= palmas_write(palmas
, PALMAS_RTC_BASE
, PALMAS_RTC_STATUS_REG
,
193 dev_err(dev
, "RTC_STATUS write failed, err = %d\n", ret
);
199 static irqreturn_t
palmas_rtc_interrupt(int irq
, void *context
)
201 struct palmas_rtc
*palmas_rtc
= context
;
202 struct device
*dev
= palmas_rtc
->dev
;
205 ret
= palmas_clear_interrupts(dev
);
207 dev_err(dev
, "RTC interrupt clear failed, err = %d\n", ret
);
211 rtc_update_irq(palmas_rtc
->rtc
, 1, RTC_IRQF
| RTC_AF
);
215 static const struct rtc_class_ops palmas_rtc_ops
= {
216 .read_time
= palmas_rtc_read_time
,
217 .set_time
= palmas_rtc_set_time
,
218 .read_alarm
= palmas_rtc_read_alarm
,
219 .set_alarm
= palmas_rtc_set_alarm
,
220 .alarm_irq_enable
= palmas_rtc_alarm_irq_enable
,
223 static int palmas_rtc_probe(struct platform_device
*pdev
)
225 struct palmas
*palmas
= dev_get_drvdata(pdev
->dev
.parent
);
226 struct palmas_rtc
*palmas_rtc
= NULL
;
228 bool enable_bb_charging
= false;
229 bool high_bb_charging
= false;
231 if (pdev
->dev
.of_node
) {
232 enable_bb_charging
= of_property_read_bool(pdev
->dev
.of_node
,
233 "ti,backup-battery-chargeable");
234 high_bb_charging
= of_property_read_bool(pdev
->dev
.of_node
,
235 "ti,backup-battery-charge-high-current");
238 palmas_rtc
= devm_kzalloc(&pdev
->dev
, sizeof(struct palmas_rtc
),
243 /* Clear pending interrupts */
244 ret
= palmas_clear_interrupts(&pdev
->dev
);
246 dev_err(&pdev
->dev
, "clear RTC int failed, err = %d\n", ret
);
250 palmas_rtc
->dev
= &pdev
->dev
;
251 platform_set_drvdata(pdev
, palmas_rtc
);
253 if (enable_bb_charging
) {
254 unsigned reg
= PALMAS_BACKUP_BATTERY_CTRL_BBS_BBC_LOW_ICHRG
;
256 if (high_bb_charging
)
259 ret
= palmas_update_bits(palmas
, PALMAS_PMU_CONTROL_BASE
,
260 PALMAS_BACKUP_BATTERY_CTRL
,
261 PALMAS_BACKUP_BATTERY_CTRL_BBS_BBC_LOW_ICHRG
, reg
);
264 "BACKUP_BATTERY_CTRL update failed, %d\n", ret
);
268 ret
= palmas_update_bits(palmas
, PALMAS_PMU_CONTROL_BASE
,
269 PALMAS_BACKUP_BATTERY_CTRL
,
270 PALMAS_BACKUP_BATTERY_CTRL_BB_CHG_EN
,
271 PALMAS_BACKUP_BATTERY_CTRL_BB_CHG_EN
);
274 "BACKUP_BATTERY_CTRL update failed, %d\n", ret
);
280 ret
= palmas_update_bits(palmas
, PALMAS_RTC_BASE
, PALMAS_RTC_CTRL_REG
,
281 PALMAS_RTC_CTRL_REG_STOP_RTC
,
282 PALMAS_RTC_CTRL_REG_STOP_RTC
);
284 dev_err(&pdev
->dev
, "RTC_CTRL write failed, err = %d\n", ret
);
288 palmas_rtc
->irq
= platform_get_irq(pdev
, 0);
290 device_init_wakeup(&pdev
->dev
, 1);
291 palmas_rtc
->rtc
= devm_rtc_device_register(&pdev
->dev
, pdev
->name
,
292 &palmas_rtc_ops
, THIS_MODULE
);
293 if (IS_ERR(palmas_rtc
->rtc
)) {
294 ret
= PTR_ERR(palmas_rtc
->rtc
);
295 dev_err(&pdev
->dev
, "RTC register failed, err = %d\n", ret
);
299 ret
= devm_request_threaded_irq(&pdev
->dev
, palmas_rtc
->irq
, NULL
,
300 palmas_rtc_interrupt
,
301 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
302 dev_name(&pdev
->dev
), palmas_rtc
);
304 dev_err(&pdev
->dev
, "IRQ request failed, err = %d\n", ret
);
311 static void palmas_rtc_remove(struct platform_device
*pdev
)
313 palmas_rtc_alarm_irq_enable(&pdev
->dev
, 0);
316 #ifdef CONFIG_PM_SLEEP
317 static int palmas_rtc_suspend(struct device
*dev
)
319 struct palmas_rtc
*palmas_rtc
= dev_get_drvdata(dev
);
321 if (device_may_wakeup(dev
))
322 enable_irq_wake(palmas_rtc
->irq
);
326 static int palmas_rtc_resume(struct device
*dev
)
328 struct palmas_rtc
*palmas_rtc
= dev_get_drvdata(dev
);
330 if (device_may_wakeup(dev
))
331 disable_irq_wake(palmas_rtc
->irq
);
336 static SIMPLE_DEV_PM_OPS(palmas_rtc_pm_ops
, palmas_rtc_suspend
,
340 static const struct of_device_id of_palmas_rtc_match
[] = {
341 { .compatible
= "ti,palmas-rtc"},
344 MODULE_DEVICE_TABLE(of
, of_palmas_rtc_match
);
347 static struct platform_driver palmas_rtc_driver
= {
348 .probe
= palmas_rtc_probe
,
349 .remove
= palmas_rtc_remove
,
351 .name
= "palmas-rtc",
352 .pm
= &palmas_rtc_pm_ops
,
353 .of_match_table
= of_match_ptr(of_palmas_rtc_match
),
357 module_platform_driver(palmas_rtc_driver
);
359 MODULE_ALIAS("platform:palmas_rtc");
360 MODULE_DESCRIPTION("TI PALMAS series RTC driver");
361 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
362 MODULE_LICENSE("GPL v2");