diff options
Diffstat (limited to 'drivers/rtc/rtc-tps80031.c')
-rw-r--r-- | drivers/rtc/rtc-tps80031.c | 449 |
1 files changed, 449 insertions, 0 deletions
diff --git a/drivers/rtc/rtc-tps80031.c b/drivers/rtc/rtc-tps80031.c new file mode 100644 index 00000000000..70d6734a570 --- /dev/null +++ b/drivers/rtc/rtc-tps80031.c | |||
@@ -0,0 +1,449 @@ | |||
1 | /* | ||
2 | * drivers/rtc/rtc_tps80031.c | ||
3 | * | ||
4 | * RTC driver for TI TPS80031 | ||
5 | * | ||
6 | * Copyright (c) 2011, NVIDIA Corporation. | ||
7 | * | ||
8 | * This program is free software; you can redistribute it and/or modify | ||
9 | * it under the terms of the GNU General Public License as published by | ||
10 | * the Free Software Foundation; either version 2 of the License, or | ||
11 | * (at your option) any later version. | ||
12 | * | ||
13 | * This program is distributed in the hope that it will be useful, but WITHOUT | ||
14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
15 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
16 | * more details. | ||
17 | * | ||
18 | * You should have received a copy of the GNU General Public License along | ||
19 | * with this program; if not, write to the Free Software Foundation, Inc., | ||
20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
21 | */ | ||
22 | |||
23 | /* #define DEBUG 1 */ | ||
24 | /* #define VERBOSE_DEBUG 1 */ | ||
25 | |||
26 | #include <linux/device.h> | ||
27 | #include <linux/err.h> | ||
28 | #include <linux/init.h> | ||
29 | #include <linux/kernel.h> | ||
30 | #include <linux/mfd/tps80031.h> | ||
31 | #include <linux/platform_device.h> | ||
32 | #include <linux/rtc.h> | ||
33 | #include <linux/slab.h> | ||
34 | |||
35 | #define RTC_CTRL 0x10 | ||
36 | #define RTC_STATUS 0x11 | ||
37 | #define RTC_SECONDS_REG 0x0 | ||
38 | #define RTC_ALARM 0x8 | ||
39 | #define RTC_INT 0x12 | ||
40 | #define RTC_RESET_STATUS 0x16 | ||
41 | |||
42 | #define ENABLE_ALARM_INT 0x8 | ||
43 | #define ALARM_INT_STATUS 0x40 | ||
44 | #define STOP_RTC 1 | ||
45 | |||
46 | /* Power on reset Values of RTC registers */ | ||
47 | #define RTC_POR_YEAR 0 | ||
48 | #define RTC_POR_MONTH 1 | ||
49 | #define RTC_POR_DAY 1 | ||
50 | |||
51 | /* | ||
52 | Linux RTC driver refers 19th centaury as base year in many | ||
53 | calculations. (e.g. refer drivers/rtc/rtc-lib.c) | ||
54 | */ | ||
55 | #define OS_REF_YEAR 1900 | ||
56 | |||
57 | /* | ||
58 | PMU RTC have only 2 nibbles to store year information, so using an | ||
59 | offset of 100 to set the base year as 2000 for our driver. | ||
60 | */ | ||
61 | #define RTC_YEAR_OFFSET 100 | ||
62 | |||
63 | struct tps80031_rtc { | ||
64 | unsigned long epoch_start; | ||
65 | int irq; | ||
66 | struct rtc_device *rtc; | ||
67 | u8 alarm_irq_enabled; | ||
68 | }; | ||
69 | |||
70 | static int tps80031_read_regs(struct device *dev, int reg, int len, | ||
71 | uint8_t *val) | ||
72 | { | ||
73 | int ret; | ||
74 | |||
75 | /* dummy read of STATUS_REG as per data sheet */ | ||
76 | ret = tps80031_reads(dev->parent, 1, RTC_STATUS, 1, val); | ||
77 | if (ret < 0) { | ||
78 | dev_err(dev->parent, "failed reading RTC_STATUS\n"); | ||
79 | WARN_ON(1); | ||
80 | return ret; | ||
81 | } | ||
82 | |||
83 | ret = tps80031_reads(dev->parent, 1, reg, len, val); | ||
84 | if (ret < 0) { | ||
85 | dev_err(dev->parent, "failed reading from reg %d\n", reg); | ||
86 | WARN_ON(1); | ||
87 | return ret; | ||
88 | } | ||
89 | return 0; | ||
90 | } | ||
91 | |||
92 | static int tps80031_write_regs(struct device *dev, int reg, int len, | ||
93 | uint8_t *val) | ||
94 | { | ||
95 | int ret; | ||
96 | ret = tps80031_writes(dev->parent, 1, reg, len, val); | ||
97 | if (ret < 0) { | ||
98 | dev_err(dev->parent, "failed writing reg: %d\n", reg); | ||
99 | WARN_ON(1); | ||
100 | return ret; | ||
101 | } | ||
102 | |||
103 | return 0; | ||
104 | } | ||
105 | |||
106 | static u8 dec2bcd(u8 dec) | ||
107 | { | ||
108 | return ((dec/10)<<4)+(dec%10); | ||
109 | } | ||
110 | |||
111 | static u8 bcd2dec(u8 bcd) | ||
112 | { | ||
113 | return (bcd >> 4)*10+(bcd & 0xF); | ||
114 | } | ||
115 | |||
116 | static void convert_bcd_to_decimal(u8 *buf, u8 len) | ||
117 | { | ||
118 | int i = 0; | ||
119 | for (i = 0; i < len; i++) | ||
120 | buf[i] = bcd2dec(buf[i]); | ||
121 | } | ||
122 | |||
123 | static void convert_decimal_to_bcd(u8 *buf, u8 len) | ||
124 | { | ||
125 | int i = 0; | ||
126 | for (i = 0; i < len; i++) | ||
127 | buf[i] = dec2bcd(buf[i]); | ||
128 | } | ||
129 | |||
130 | static int tps80031_rtc_read_time(struct device *dev, struct rtc_time *tm) | ||
131 | { | ||
132 | u8 buff[7]; | ||
133 | int err; | ||
134 | err = tps80031_read_regs(dev, RTC_SECONDS_REG, sizeof(buff), buff); | ||
135 | if (err < 0) { | ||
136 | dev_err(dev->parent, "failed reading time\n"); | ||
137 | return err; | ||
138 | } | ||
139 | convert_bcd_to_decimal(buff, sizeof(buff)); | ||
140 | tm->tm_sec = buff[0]; | ||
141 | tm->tm_min = buff[1]; | ||
142 | tm->tm_hour = buff[2]; | ||
143 | tm->tm_mday = buff[3]; | ||
144 | tm->tm_mon = buff[4] - 1; | ||
145 | tm->tm_year = buff[5] + RTC_YEAR_OFFSET; | ||
146 | tm->tm_wday = buff[6]; | ||
147 | return 0; | ||
148 | } | ||
149 | |||
150 | static int tps80031_rtc_stop(struct device *dev) | ||
151 | { | ||
152 | int err; | ||
153 | err = tps80031_clr_bits(dev->parent, 1, RTC_CTRL, STOP_RTC); | ||
154 | if (err < 0) | ||
155 | dev_err(dev->parent, "failed to stop RTC. err: %d\n", err); | ||
156 | return err; | ||
157 | } | ||
158 | |||
159 | static int tps80031_rtc_start(struct device *dev) | ||
160 | { | ||
161 | int err; | ||
162 | err = tps80031_set_bits(dev->parent, 1, RTC_CTRL, STOP_RTC); | ||
163 | if (err < 0) | ||
164 | dev_err(dev->parent, "failed to start RTC. err: %d\n", err); | ||
165 | return err; | ||
166 | } | ||
167 | |||
168 | |||
169 | static int tps80031_rtc_set_time(struct device *dev, struct rtc_time *tm) | ||
170 | { | ||
171 | u8 buff[7]; | ||
172 | int err; | ||
173 | |||
174 | buff[0] = tm->tm_sec; | ||
175 | buff[1] = tm->tm_min; | ||
176 | buff[2] = tm->tm_hour; | ||
177 | buff[3] = tm->tm_mday; | ||
178 | buff[4] = tm->tm_mon + 1; | ||
179 | buff[5] = tm->tm_year % RTC_YEAR_OFFSET; | ||
180 | buff[6] = tm->tm_wday; | ||
181 | |||
182 | convert_decimal_to_bcd(buff, sizeof(buff)); | ||
183 | err = tps80031_rtc_stop(dev); | ||
184 | if (err < 0) | ||
185 | return err; | ||
186 | |||
187 | err = tps80031_write_regs(dev, RTC_SECONDS_REG, sizeof(buff), buff); | ||
188 | if (err < 0) { | ||
189 | dev_err(dev->parent, "failed to program new time\n"); | ||
190 | return err; | ||
191 | } | ||
192 | |||
193 | err = tps80031_rtc_start(dev); | ||
194 | return err; | ||
195 | } | ||
196 | |||
197 | static int tps80031_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) | ||
198 | { | ||
199 | struct tps80031_rtc *rtc = dev_get_drvdata(dev); | ||
200 | unsigned long seconds; | ||
201 | u8 buff[6]; | ||
202 | int err; | ||
203 | struct rtc_time tm; | ||
204 | |||
205 | if (rtc->irq == -1) | ||
206 | return -EIO; | ||
207 | |||
208 | rtc_tm_to_time(&alrm->time, &seconds); | ||
209 | tps80031_rtc_read_time(dev, &tm); | ||
210 | rtc_tm_to_time(&tm, &rtc->epoch_start); | ||
211 | |||
212 | if (WARN_ON(alrm->enabled && (seconds < rtc->epoch_start))) { | ||
213 | dev_err(dev->parent, "can't set alarm to requested time\n"); | ||
214 | return -EINVAL; | ||
215 | } | ||
216 | |||
217 | buff[0] = alrm->time.tm_sec; | ||
218 | buff[1] = alrm->time.tm_min; | ||
219 | buff[2] = alrm->time.tm_hour; | ||
220 | buff[3] = alrm->time.tm_mday; | ||
221 | buff[4] = alrm->time.tm_mon + 1; | ||
222 | buff[5] = alrm->time.tm_year % RTC_YEAR_OFFSET; | ||
223 | convert_decimal_to_bcd(buff, sizeof(buff)); | ||
224 | err = tps80031_write_regs(dev, RTC_ALARM, sizeof(buff), buff); | ||
225 | if (err) | ||
226 | dev_err(dev->parent, "unable to program alarm\n"); | ||
227 | |||
228 | return err; | ||
229 | } | ||
230 | |||
231 | static int tps80031_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) | ||
232 | { | ||
233 | u8 buff[6]; | ||
234 | int err; | ||
235 | |||
236 | err = tps80031_read_regs(dev, RTC_ALARM, sizeof(buff), buff); | ||
237 | if (err) | ||
238 | return err; | ||
239 | convert_bcd_to_decimal(buff, sizeof(buff)); | ||
240 | |||
241 | alrm->time.tm_sec = buff[0]; | ||
242 | alrm->time.tm_min = buff[1]; | ||
243 | alrm->time.tm_hour = buff[2]; | ||
244 | alrm->time.tm_mday = buff[3]; | ||
245 | alrm->time.tm_mon = buff[4] - 1; | ||
246 | alrm->time.tm_year = buff[5] + RTC_YEAR_OFFSET; | ||
247 | |||
248 | return 0; | ||
249 | } | ||
250 | |||
251 | static int tps80031_rtc_alarm_irq_enable(struct device *dev, | ||
252 | unsigned int enable) | ||
253 | { | ||
254 | struct tps80031_rtc *rtc = dev_get_drvdata(dev); | ||
255 | int err; | ||
256 | struct device *p = dev->parent; | ||
257 | |||
258 | if (rtc->irq == -1) | ||
259 | return -EIO; | ||
260 | |||
261 | if (enable) { | ||
262 | if (rtc->alarm_irq_enabled) | ||
263 | return 0; | ||
264 | |||
265 | err = tps80031_set_bits(p, 1, RTC_INT, ENABLE_ALARM_INT); | ||
266 | if (err < 0) { | ||
267 | dev_err(p, "failed to set ALRM int. err: %d\n", err); | ||
268 | return err; | ||
269 | } else | ||
270 | rtc->alarm_irq_enabled = 1; | ||
271 | } else { | ||
272 | if(!rtc->alarm_irq_enabled) | ||
273 | return 0; | ||
274 | err = tps80031_clr_bits(p, 1, RTC_INT, ENABLE_ALARM_INT); | ||
275 | if (err < 0) { | ||
276 | dev_err(p, "failed to clear ALRM int. err: %d\n", err); | ||
277 | return err; | ||
278 | } else | ||
279 | rtc->alarm_irq_enabled = 0; | ||
280 | } | ||
281 | return 0; | ||
282 | } | ||
283 | |||
284 | static const struct rtc_class_ops tps80031_rtc_ops = { | ||
285 | .read_time = tps80031_rtc_read_time, | ||
286 | .set_time = tps80031_rtc_set_time, | ||
287 | .set_alarm = tps80031_rtc_set_alarm, | ||
288 | .read_alarm = tps80031_rtc_read_alarm, | ||
289 | .alarm_irq_enable = tps80031_rtc_alarm_irq_enable, | ||
290 | }; | ||
291 | |||
292 | static irqreturn_t tps80031_rtc_irq(int irq, void *data) | ||
293 | { | ||
294 | struct device *dev = data; | ||
295 | struct tps80031_rtc *rtc = dev_get_drvdata(dev); | ||
296 | u8 reg; | ||
297 | int err; | ||
298 | |||
299 | /* clear Alarm status bits.*/ | ||
300 | err = tps80031_read_regs(dev, RTC_STATUS, 1, ®); | ||
301 | if (err) { | ||
302 | dev_err(dev->parent, "unable to read RTC_STATUS reg\n"); | ||
303 | return -EBUSY; | ||
304 | } | ||
305 | |||
306 | err = tps80031_force_update(dev->parent, 1, RTC_STATUS, | ||
307 | ALARM_INT_STATUS, ALARM_INT_STATUS); | ||
308 | if (err) { | ||
309 | dev_err(dev->parent, "unable to set Alarm INT\n"); | ||
310 | return -EBUSY; | ||
311 | } | ||
312 | |||
313 | rtc_update_irq(rtc->rtc, 1, RTC_IRQF | RTC_AF); | ||
314 | return IRQ_HANDLED; | ||
315 | } | ||
316 | |||
317 | static int __devinit tps80031_rtc_probe(struct platform_device *pdev) | ||
318 | { | ||
319 | struct tps80031_rtc_platform_data *pdata = pdev->dev.platform_data; | ||
320 | struct tps80031_rtc *rtc; | ||
321 | struct rtc_time tm; | ||
322 | int err; | ||
323 | u8 reg; | ||
324 | |||
325 | if (!pdata) { | ||
326 | dev_err(&pdev->dev, "no platform_data specified\n"); | ||
327 | return -EINVAL; | ||
328 | } | ||
329 | |||
330 | rtc = kzalloc(sizeof(*rtc), GFP_KERNEL); | ||
331 | if (!rtc) | ||
332 | return -ENOMEM; | ||
333 | |||
334 | rtc->irq = -1; | ||
335 | if (pdata->irq < 0) | ||
336 | dev_err(&pdev->dev, "no IRQ specified, wakeup is disabled\n"); | ||
337 | |||
338 | rtc->rtc = rtc_device_register(pdev->name, &pdev->dev, | ||
339 | &tps80031_rtc_ops, THIS_MODULE); | ||
340 | |||
341 | if (IS_ERR(rtc->rtc)) { | ||
342 | err = PTR_ERR(rtc->rtc); | ||
343 | goto fail; | ||
344 | } | ||
345 | |||
346 | if ((int)pdev && (int)&pdev->dev) | ||
347 | err = tps80031_read_regs(&pdev->dev, RTC_STATUS, 1, ®); | ||
348 | else { | ||
349 | dev_err(&pdev->dev, "%s Input params incorrect\n", __func__); | ||
350 | err = -EBUSY; | ||
351 | goto fail; | ||
352 | } | ||
353 | |||
354 | if (err) { | ||
355 | dev_err(&pdev->dev, "%s unable to read status\n", __func__); | ||
356 | err = -EBUSY; | ||
357 | goto fail; | ||
358 | } | ||
359 | |||
360 | /* If RTC have POR values, set time using platform data*/ | ||
361 | tps80031_rtc_read_time(&pdev->dev, &tm); | ||
362 | if ((tm.tm_year == RTC_YEAR_OFFSET + RTC_POR_YEAR) && | ||
363 | (tm.tm_mon == (RTC_POR_MONTH - 1)) && | ||
364 | (tm.tm_mday == RTC_POR_DAY)) { | ||
365 | if (pdata->time.tm_year < 2000 || | ||
366 | pdata->time.tm_year > 2100) { | ||
367 | dev_err(&pdev->dev, "Invalid platform data\n"); | ||
368 | memset(&pdata->time, 0, sizeof(pdata->time)); | ||
369 | pdata->time.tm_year = 2011; | ||
370 | pdata->time.tm_mday = 1; | ||
371 | } | ||
372 | tps80031_rtc_set_time(&pdev->dev, &pdata->time); | ||
373 | } | ||
374 | |||
375 | reg = ALARM_INT_STATUS; | ||
376 | err = tps80031_write_regs(&pdev->dev, RTC_STATUS, 1, ®); | ||
377 | if (err) { | ||
378 | dev_err(&pdev->dev, "unable to program RTC_STATUS reg\n"); | ||
379 | return -EBUSY; | ||
380 | } | ||
381 | |||
382 | err = tps80031_set_bits(pdev->dev.parent, 1, RTC_INT, ENABLE_ALARM_INT); | ||
383 | if (err) { | ||
384 | dev_err(&pdev->dev, "unable to program Interrupt Mask reg\n"); | ||
385 | err = -EBUSY; | ||
386 | rtc->alarm_irq_enabled = 0; | ||
387 | goto fail; | ||
388 | } else | ||
389 | rtc->alarm_irq_enabled = 1; | ||
390 | |||
391 | dev_set_drvdata(&pdev->dev, rtc); | ||
392 | if (pdata && (pdata->irq >= 0)) { | ||
393 | rtc->irq = pdata->irq; | ||
394 | err = request_threaded_irq(pdata->irq, NULL, tps80031_rtc_irq, | ||
395 | IRQF_ONESHOT, "rtc_tps80031", | ||
396 | &pdev->dev); | ||
397 | if (err) { | ||
398 | dev_err(&pdev->dev, "request IRQ:%d fail\n", rtc->irq); | ||
399 | rtc->irq = -1; | ||
400 | } else { | ||
401 | device_init_wakeup(&pdev->dev, 1); | ||
402 | enable_irq_wake(rtc->irq); | ||
403 | } | ||
404 | } | ||
405 | return 0; | ||
406 | |||
407 | fail: | ||
408 | if (!IS_ERR_OR_NULL(rtc->rtc)) | ||
409 | rtc_device_unregister(rtc->rtc); | ||
410 | kfree(rtc); | ||
411 | return err; | ||
412 | } | ||
413 | |||
414 | static int __devexit tps80031_rtc_remove(struct platform_device *pdev) | ||
415 | { | ||
416 | struct tps80031_rtc *rtc = dev_get_drvdata(&pdev->dev); | ||
417 | |||
418 | if (rtc->irq != -1) | ||
419 | free_irq(rtc->irq, rtc); | ||
420 | rtc_device_unregister(rtc->rtc); | ||
421 | kfree(rtc); | ||
422 | return 0; | ||
423 | } | ||
424 | |||
425 | static struct platform_driver tps80031_rtc_driver = { | ||
426 | .driver = { | ||
427 | .name = "rtc_tps80031", | ||
428 | .owner = THIS_MODULE, | ||
429 | }, | ||
430 | .probe = tps80031_rtc_probe, | ||
431 | .remove = __devexit_p(tps80031_rtc_remove), | ||
432 | }; | ||
433 | |||
434 | static int __init tps80031_rtc_init(void) | ||
435 | { | ||
436 | return platform_driver_register(&tps80031_rtc_driver); | ||
437 | } | ||
438 | module_init(tps80031_rtc_init); | ||
439 | |||
440 | static void __exit tps80031_rtc_exit(void) | ||
441 | { | ||
442 | platform_driver_unregister(&tps80031_rtc_driver); | ||
443 | } | ||
444 | module_exit(tps80031_rtc_exit); | ||
445 | |||
446 | MODULE_DESCRIPTION("TI TPS80031 RTC driver"); | ||
447 | MODULE_AUTHOR("NVIDIA Corporation"); | ||
448 | MODULE_LICENSE("GPL"); | ||
449 | MODULE_ALIAS("platform:rtc_tps80031"); | ||