2 * rtc-tps6586x.c: RTC driver for TI PMIC TPS6586X
4 * Copyright (c) 2012, NVIDIA Corporation.
6 * Author: Laxman Dewangan <ldewangan@nvidia.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation version 2.
12 * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
13 * whether express or implied; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 #include <linux/device.h>
24 #include <linux/err.h>
25 #include <linux/init.h>
26 #include <linux/kernel.h>
27 #include <linux/mfd/tps6586x.h>
28 #include <linux/module.h>
29 #include <linux/platform_device.h>
30 #include <linux/pm_runtime.h>
31 #include <linux/rtc.h>
32 #include <linux/slab.h>
35 #define POR_RESET_N BIT(7)
36 #define OSC_SRC_SEL BIT(6)
37 #define RTC_ENABLE BIT(5) /* enables alarm */
38 #define RTC_BUF_ENABLE BIT(4) /* 32 KHz buffer enable */
39 #define PRE_BYPASS BIT(3) /* 0=1KHz or 1=32KHz updates */
40 #define CL_SEL_MASK (BIT(2)|BIT(1))
42 #define RTC_ALARM1_HI 0xc1
43 #define RTC_COUNT4 0xc6
45 /* start a PMU RTC access by reading the register prior to the RTC_COUNT4 */
46 #define RTC_COUNT4_DUMMYREAD 0xc5
48 /*only 14-bits width in second*/
49 #define ALM1_VALID_RANGE_IN_SEC 0x3FFF
51 #define TPS6586X_RTC_CL_SEL_1_5PF 0x0
52 #define TPS6586X_RTC_CL_SEL_6_5PF 0x1
53 #define TPS6586X_RTC_CL_SEL_7_5PF 0x2
54 #define TPS6586X_RTC_CL_SEL_12_5PF 0x3
58 struct rtc_device
*rtc
;
63 static inline struct device
*to_tps6586x_dev(struct device
*dev
)
68 static int tps6586x_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
70 struct device
*tps_dev
= to_tps6586x_dev(dev
);
71 unsigned long long ticks
= 0;
77 ret
= tps6586x_reads(tps_dev
, RTC_COUNT4_DUMMYREAD
, sizeof(buff
), buff
);
79 dev_err(dev
, "read counter failed with err %d\n", ret
);
83 for (i
= 1; i
< sizeof(buff
); i
++) {
88 seconds
= ticks
>> 10;
89 rtc_time64_to_tm(seconds
, tm
);
94 static int tps6586x_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
96 struct device
*tps_dev
= to_tps6586x_dev(dev
);
97 unsigned long long ticks
;
102 seconds
= rtc_tm_to_time64(tm
);
104 ticks
= (unsigned long long)seconds
<< 10;
105 buff
[0] = (ticks
>> 32) & 0xff;
106 buff
[1] = (ticks
>> 24) & 0xff;
107 buff
[2] = (ticks
>> 16) & 0xff;
108 buff
[3] = (ticks
>> 8) & 0xff;
109 buff
[4] = ticks
& 0xff;
111 /* Disable RTC before changing time */
112 ret
= tps6586x_clr_bits(tps_dev
, RTC_CTRL
, RTC_ENABLE
);
114 dev_err(dev
, "failed to clear RTC_ENABLE\n");
118 ret
= tps6586x_writes(tps_dev
, RTC_COUNT4
, sizeof(buff
), buff
);
120 dev_err(dev
, "failed to program new time\n");
125 ret
= tps6586x_set_bits(tps_dev
, RTC_CTRL
, RTC_ENABLE
);
127 dev_err(dev
, "failed to set RTC_ENABLE\n");
133 static int tps6586x_rtc_alarm_irq_enable(struct device
*dev
,
134 unsigned int enabled
)
136 struct tps6586x_rtc
*rtc
= dev_get_drvdata(dev
);
138 if (enabled
&& !rtc
->irq_en
) {
139 enable_irq(rtc
->irq
);
141 } else if (!enabled
&& rtc
->irq_en
) {
142 disable_irq(rtc
->irq
);
148 static int tps6586x_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
150 struct device
*tps_dev
= to_tps6586x_dev(dev
);
153 unsigned long rtc_current_time
;
154 unsigned long long rticks
= 0;
160 seconds
= rtc_tm_to_time64(&alrm
->time
);
162 ret
= tps6586x_rtc_alarm_irq_enable(dev
, alrm
->enabled
);
164 dev_err(dev
, "can't set alarm irq, err %d\n", ret
);
168 ret
= tps6586x_reads(tps_dev
, RTC_COUNT4_DUMMYREAD
,
169 sizeof(rbuff
), rbuff
);
171 dev_err(dev
, "read counter failed with err %d\n", ret
);
175 for (i
= 1; i
< sizeof(rbuff
); i
++) {
180 rtc_current_time
= rticks
>> 10;
181 if ((seconds
- rtc_current_time
) > ALM1_VALID_RANGE_IN_SEC
)
182 seconds
= rtc_current_time
- 1;
184 ticks
= (unsigned long long)seconds
<< 10;
185 buff
[0] = (ticks
>> 16) & 0xff;
186 buff
[1] = (ticks
>> 8) & 0xff;
187 buff
[2] = ticks
& 0xff;
189 ret
= tps6586x_writes(tps_dev
, RTC_ALARM1_HI
, sizeof(buff
), buff
);
191 dev_err(dev
, "programming alarm failed with err %d\n", ret
);
196 static int tps6586x_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
198 struct device
*tps_dev
= to_tps6586x_dev(dev
);
204 ret
= tps6586x_reads(tps_dev
, RTC_ALARM1_HI
, sizeof(buff
), buff
);
206 dev_err(dev
, "read RTC_ALARM1_HI failed with err %d\n", ret
);
210 ticks
= (buff
[0] << 16) | (buff
[1] << 8) | buff
[2];
211 seconds
= ticks
>> 10;
213 rtc_time64_to_tm(seconds
, &alrm
->time
);
217 static const struct rtc_class_ops tps6586x_rtc_ops
= {
218 .read_time
= tps6586x_rtc_read_time
,
219 .set_time
= tps6586x_rtc_set_time
,
220 .set_alarm
= tps6586x_rtc_set_alarm
,
221 .read_alarm
= tps6586x_rtc_read_alarm
,
222 .alarm_irq_enable
= tps6586x_rtc_alarm_irq_enable
,
225 static irqreturn_t
tps6586x_rtc_irq(int irq
, void *data
)
227 struct tps6586x_rtc
*rtc
= data
;
229 rtc_update_irq(rtc
->rtc
, 1, RTC_IRQF
| RTC_AF
);
233 static int tps6586x_rtc_probe(struct platform_device
*pdev
)
235 struct device
*tps_dev
= to_tps6586x_dev(&pdev
->dev
);
236 struct tps6586x_rtc
*rtc
;
239 rtc
= devm_kzalloc(&pdev
->dev
, sizeof(*rtc
), GFP_KERNEL
);
243 rtc
->dev
= &pdev
->dev
;
244 rtc
->irq
= platform_get_irq(pdev
, 0);
246 /* 1 kHz tick mode, enable tick counting */
247 ret
= tps6586x_update(tps_dev
, RTC_CTRL
,
248 RTC_ENABLE
| OSC_SRC_SEL
|
249 ((TPS6586X_RTC_CL_SEL_1_5PF
<< CL_SEL_POS
) & CL_SEL_MASK
),
250 RTC_ENABLE
| OSC_SRC_SEL
| PRE_BYPASS
| CL_SEL_MASK
);
252 dev_err(&pdev
->dev
, "unable to start counter\n");
256 device_init_wakeup(&pdev
->dev
, 1);
258 platform_set_drvdata(pdev
, rtc
);
259 rtc
->rtc
= devm_rtc_allocate_device(&pdev
->dev
);
260 if (IS_ERR(rtc
->rtc
)) {
261 ret
= PTR_ERR(rtc
->rtc
);
262 goto fail_rtc_register
;
265 rtc
->rtc
->ops
= &tps6586x_rtc_ops
;
266 rtc
->rtc
->range_max
= (1ULL << 30) - 1; /* 30-bit seconds */
267 rtc
->rtc
->start_secs
= mktime64(2009, 1, 1, 0, 0, 0);
268 rtc
->rtc
->set_start_time
= true;
270 ret
= devm_request_threaded_irq(&pdev
->dev
, rtc
->irq
, NULL
,
273 dev_name(&pdev
->dev
), rtc
);
275 dev_err(&pdev
->dev
, "request IRQ(%d) failed with ret %d\n",
277 goto fail_rtc_register
;
279 disable_irq(rtc
->irq
);
281 ret
= rtc_register_device(rtc
->rtc
);
283 goto fail_rtc_register
;
288 tps6586x_update(tps_dev
, RTC_CTRL
, 0,
289 RTC_ENABLE
| OSC_SRC_SEL
| PRE_BYPASS
| CL_SEL_MASK
);
293 static int tps6586x_rtc_remove(struct platform_device
*pdev
)
295 struct device
*tps_dev
= to_tps6586x_dev(&pdev
->dev
);
297 tps6586x_update(tps_dev
, RTC_CTRL
, 0,
298 RTC_ENABLE
| OSC_SRC_SEL
| PRE_BYPASS
| CL_SEL_MASK
);
302 #ifdef CONFIG_PM_SLEEP
303 static int tps6586x_rtc_suspend(struct device
*dev
)
305 struct tps6586x_rtc
*rtc
= dev_get_drvdata(dev
);
307 if (device_may_wakeup(dev
))
308 enable_irq_wake(rtc
->irq
);
312 static int tps6586x_rtc_resume(struct device
*dev
)
314 struct tps6586x_rtc
*rtc
= dev_get_drvdata(dev
);
316 if (device_may_wakeup(dev
))
317 disable_irq_wake(rtc
->irq
);
322 static SIMPLE_DEV_PM_OPS(tps6586x_pm_ops
, tps6586x_rtc_suspend
,
323 tps6586x_rtc_resume
);
325 static struct platform_driver tps6586x_rtc_driver
= {
327 .name
= "tps6586x-rtc",
328 .pm
= &tps6586x_pm_ops
,
330 .probe
= tps6586x_rtc_probe
,
331 .remove
= tps6586x_rtc_remove
,
333 module_platform_driver(tps6586x_rtc_driver
);
335 MODULE_ALIAS("platform:tps6586x-rtc");
336 MODULE_DESCRIPTION("TI TPS6586x RTC driver");
337 MODULE_AUTHOR("Laxman dewangan <ldewangan@nvidia.com>");
338 MODULE_LICENSE("GPL v2");