aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/rtc/rtc-ricoh583.c
diff options
context:
space:
mode:
authorJonathan Herman <hermanjl@cs.unc.edu>2013-01-22 10:38:37 -0500
committerJonathan Herman <hermanjl@cs.unc.edu>2013-01-22 10:38:37 -0500
commitfcc9d2e5a6c89d22b8b773a64fb4ad21ac318446 (patch)
treea57612d1888735a2ec7972891b68c1ac5ec8faea /drivers/rtc/rtc-ricoh583.c
parent8dea78da5cee153b8af9c07a2745f6c55057fe12 (diff)
Added missing tegra files.HEADmaster
Diffstat (limited to 'drivers/rtc/rtc-ricoh583.c')
-rw-r--r--drivers/rtc/rtc-ricoh583.c403
1 files changed, 403 insertions, 0 deletions
diff --git a/drivers/rtc/rtc-ricoh583.c b/drivers/rtc/rtc-ricoh583.c
new file mode 100644
index 00000000000..8bc17d9a101
--- /dev/null
+++ b/drivers/rtc/rtc-ricoh583.c
@@ -0,0 +1,403 @@
1/*
2 * drivers/rtc/rtc_ricoh583.c
3 *
4 * rtc driver for ricoh rc5t583 pmu
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/ricoh583.h>
31#include <linux/platform_device.h>
32#include <linux/rtc.h>
33#include <linux/slab.h>
34
35#define rtc_ctrl1 0xED
36#define rtc_ctrl2 0xEE
37#define rtc_seconds_reg 0xE0
38#define rtc_alarm_y 0xF0
39#define rtc_adjust 0xE7
40
41/*
42linux rtc driver refers 1900 as base year in many calculations.
43(e.g. refer drivers/rtc/rtc-lib.c)
44*/
45#define os_ref_year 1900
46
47/*
48 pmu rtc have only 2 nibbles to store year information, so using an
49 offset of 100 to set the base year as 2000 for our driver.
50*/
51#define rtc_year_offset 100
52
53struct ricoh583_rtc {
54 unsigned long epoch_start;
55 int irq;
56 struct rtc_device *rtc;
57 bool irq_en;
58};
59
60static int ricoh583_read_regs(struct device *dev, int reg, int len,
61 uint8_t *val)
62{
63 int ret;
64
65 ret = ricoh583_bulk_reads(dev->parent, reg, len, val);
66 if (ret < 0) {
67 dev_err(dev->parent, "\n %s failed reading from 0x%02x\n",
68 __func__, reg);
69 WARN_ON(1);
70 }
71 return ret;
72}
73
74static int ricoh583_write_regs(struct device *dev, int reg, int len,
75 uint8_t *val)
76{
77 int ret;
78 ret = ricoh583_bulk_writes(dev->parent, reg, len, val);
79 if (ret < 0) {
80 dev_err(dev->parent, "\n %s failed writing\n", __func__);
81 WARN_ON(1);
82 }
83
84 return ret;
85}
86
87static int ricoh583_rtc_valid_tm(struct device *dev, struct rtc_time *tm)
88{
89 if (tm->tm_year >= (rtc_year_offset + 99)
90 || tm->tm_mon > 12
91 || tm->tm_mday < 1
92 || tm->tm_mday > rtc_month_days(tm->tm_mon,
93 tm->tm_year + os_ref_year)
94 || tm->tm_hour >= 24
95 || tm->tm_min >= 60
96 || tm->tm_sec >= 60) {
97 dev_err(dev->parent, "\n returning error due to time"
98 "%d/%d/%d %d:%d:%d", tm->tm_mon, tm->tm_mday,
99 tm->tm_year, tm->tm_hour, tm->tm_min, tm->tm_sec);
100 return -EINVAL;
101 }
102 return 0;
103}
104
105static u8 dec2bcd(u8 dec)
106{
107 return ((dec/10)<<4)+(dec%10);
108}
109
110static u8 bcd2dec(u8 bcd)
111{
112 return (bcd >> 4)*10+(bcd & 0xf);
113}
114
115static void convert_bcd_to_decimal(u8 *buf, u8 len)
116{
117 int i = 0;
118 for (i = 0; i < len; i++)
119 buf[i] = bcd2dec(buf[i]);
120}
121
122static void convert_decimal_to_bcd(u8 *buf, u8 len)
123{
124 int i = 0;
125 for (i = 0; i < len; i++)
126 buf[i] = dec2bcd(buf[i]);
127}
128
129static void print_time(struct device *dev, struct rtc_time *tm)
130{
131 dev_info(dev, "rtc-time : %d/%d/%d %d:%d\n",
132 (tm->tm_mon + 1), tm->tm_mday, (tm->tm_year + os_ref_year),
133 tm->tm_hour, tm->tm_min);
134}
135
136static int ricoh583_rtc_read_time(struct device *dev, struct rtc_time *tm)
137{
138 u8 buff[7];
139 int err;
140 err = ricoh583_read_regs(dev, rtc_seconds_reg, sizeof(buff), buff);
141 if (err < 0) {
142 dev_err(dev, "\n %s :: failed to read time\n", __FILE__);
143 return err;
144 }
145 convert_bcd_to_decimal(buff, sizeof(buff));
146 tm->tm_sec = buff[0];
147 tm->tm_min = buff[1];
148 tm->tm_hour = buff[2];
149 tm->tm_wday = buff[3];
150 tm->tm_mday = buff[4];
151 tm->tm_mon = buff[5] - 1;
152 tm->tm_year = buff[6] + rtc_year_offset;
153 print_time(dev, tm);
154 return ricoh583_rtc_valid_tm(dev, tm);
155}
156
157static int ricoh583_rtc_set_time(struct device *dev, struct rtc_time *tm)
158{
159 u8 buff[7];
160 int err;
161
162 print_time(dev, tm);
163 buff[0] = tm->tm_sec;
164 buff[1] = tm->tm_min;
165 buff[2] = tm->tm_hour;
166 buff[3] = tm->tm_wday;
167 buff[4] = tm->tm_mday;
168 buff[5] = tm->tm_mon + 1;
169 buff[6] = tm->tm_year - rtc_year_offset;
170
171 convert_decimal_to_bcd(buff, sizeof(buff));
172 err = ricoh583_write_regs(dev, rtc_seconds_reg, sizeof(buff), buff);
173 if (err < 0) {
174 dev_err(dev->parent, "\n failed to program new time\n");
175 return err;
176 }
177
178 return 0;
179}
180static int ricoh583_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm);
181
182static int ricoh583_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
183{
184 struct ricoh583_rtc *rtc = dev_get_drvdata(dev);
185 unsigned long seconds;
186 u8 buff[5];
187 int err;
188 struct rtc_time tm;
189
190 if (rtc->irq == -1)
191 return -EIO;
192
193 rtc_tm_to_time(&alrm->time, &seconds);
194 ricoh583_rtc_read_time(dev, &tm);
195 rtc_tm_to_time(&tm, &rtc->epoch_start);
196 /*
197 work around: As YAL does not provide the seconds register,
198 program minute register to next minute, in cases when alarm
199 is requested within a minute from the current time.
200 */
201 if (seconds - rtc->epoch_start < 60)
202 alrm->time.tm_min += 1;
203 dev_info(dev->parent, "\n setting alarm to requested time::\n");
204 print_time(dev->parent, &alrm->time);
205
206 if (WARN_ON(alrm->enabled && (seconds < rtc->epoch_start))) {
207 dev_err(dev->parent, "\n can't set alarm to requested time\n");
208 return -EINVAL;
209 }
210
211 if (alrm->enabled && !rtc->irq_en)
212 rtc->irq_en = true;
213 else if (!alrm->enabled && rtc->irq_en)
214 rtc->irq_en = false;
215
216 buff[0] = alrm->time.tm_min;
217 buff[1] = alrm->time.tm_hour;
218 buff[2] = alrm->time.tm_mday;
219 buff[3] = alrm->time.tm_mon + 1;
220 buff[4] = alrm->time.tm_year - rtc_year_offset;
221 convert_decimal_to_bcd(buff, sizeof(buff));
222 err = ricoh583_write_regs(dev, rtc_alarm_y, sizeof(buff), buff);
223 if (err) {
224 dev_err(dev->parent, "\n unable to set alarm\n");
225 return -EBUSY;
226 }
227 buff[0] = 0x20; /* to enable alarm_y */
228 buff[1] = 0x20; /* to enable 24-hour format */
229 err = ricoh583_write_regs(dev, rtc_ctrl1, 2, buff);
230 if (err) {
231 dev_err(dev, "failed programming rtc ctrl regs\n");
232 return -EBUSY;
233 }
234return err;
235}
236
237static int ricoh583_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
238{
239 u8 buff[5];
240 int err;
241
242 err = ricoh583_read_regs(dev, rtc_alarm_y, sizeof(buff), buff);
243 if (err)
244 return err;
245 convert_bcd_to_decimal(buff, sizeof(buff));
246
247 alrm->time.tm_min = buff[0];
248 alrm->time.tm_hour = buff[1];
249 alrm->time.tm_mday = buff[2];
250 alrm->time.tm_mon = buff[3] - 1;
251 alrm->time.tm_year = buff[4] + rtc_year_offset;
252
253 dev_info(dev->parent, "\n getting alarm time::\n");
254 print_time(dev, &alrm->time);
255
256 return 0;
257}
258
259static const struct rtc_class_ops ricoh583_rtc_ops = {
260 .read_time = ricoh583_rtc_read_time,
261 .set_time = ricoh583_rtc_set_time,
262 .set_alarm = ricoh583_rtc_set_alarm,
263 .read_alarm = ricoh583_rtc_read_alarm,
264};
265
266static irqreturn_t ricoh583_rtc_irq(int irq, void *data)
267{
268 struct device *dev = data;
269 struct ricoh583_rtc *rtc = dev_get_drvdata(dev);
270 u8 reg;
271 int err;
272
273 /* clear alarm-Y status bits.*/
274 err = ricoh583_read_regs(dev, rtc_ctrl2, 1, &reg);
275 if (err) {
276 dev_err(dev->parent, "unable to read rtc_ctrl2 reg\n");
277 return -EBUSY;
278 }
279 reg &= ~0x8;
280 err = ricoh583_write_regs(dev, rtc_ctrl2, 1, &reg);
281 if (err) {
282 dev_err(dev->parent, "unable to program rtc_status reg\n");
283 return -EBUSY;
284 }
285
286 rtc_update_irq(rtc->rtc, 1, RTC_IRQF | RTC_AF);
287 return IRQ_HANDLED;
288}
289
290static int __devinit ricoh583_rtc_probe(struct platform_device *pdev)
291{
292 struct ricoh583_rtc_platform_data *pdata = pdev->dev.platform_data;
293 struct ricoh583_rtc *rtc;
294 struct rtc_time tm;
295 int err;
296 u8 reg[2];
297 rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
298
299 if (!rtc)
300 return -ENOMEM;
301
302 rtc->irq = -1;
303
304 if (!pdata) {
305 dev_err(&pdev->dev, "no platform_data specified\n");
306 return -EINVAL;
307 }
308
309 if (pdata->irq < 0)
310 dev_err(&pdev->dev, "\n no irq specified, wakeup is disabled\n");
311
312 dev_set_drvdata(&pdev->dev, rtc);
313 device_init_wakeup(&pdev->dev, 1);
314 rtc->rtc = rtc_device_register(pdev->name, &pdev->dev,
315 &ricoh583_rtc_ops, THIS_MODULE);
316
317 if (IS_ERR(rtc->rtc)) {
318 err = PTR_ERR(rtc->rtc);
319 goto fail;
320 }
321 reg[0] = 0; /* clearing RTC Adjust register */
322 err = ricoh583_write_regs(&pdev->dev, rtc_adjust, 1, reg);
323 if (err) {
324 dev_err(&pdev->dev, "unable to program rtc_adjust reg\n");
325 return -EBUSY;
326 }
327
328 reg[0] = 0x20; /* to enable alarm_y */
329 reg[1] = 0x20; /* to enable 24-hour format */
330 err = ricoh583_write_regs(&pdev->dev, rtc_ctrl1, 2, reg);
331 if (err) {
332 dev_err(&pdev->dev, "failed rtc setup\n");
333 return -EBUSY;
334 }
335
336 ricoh583_rtc_read_time(&pdev->dev, &tm);
337 if (ricoh583_rtc_valid_tm(&pdev->dev, &tm)) {
338 if (pdata->time.tm_year < 2000 || pdata->time.tm_year > 2100) {
339 memset(&pdata->time, 0, sizeof(pdata->time));
340 pdata->time.tm_year = rtc_year_offset;
341 pdata->time.tm_mday = 1;
342 } else
343 pdata->time.tm_year -= os_ref_year;
344 ricoh583_rtc_set_time(&pdev->dev, &pdata->time);
345 }
346 if (pdata && (pdata->irq >= 0)) {
347 rtc->irq = pdata->irq;
348 err = request_threaded_irq(pdata->irq, NULL, ricoh583_rtc_irq,
349 IRQF_ONESHOT, "rtc_ricoh583",
350 &pdev->dev);
351 if (err) {
352 dev_err(&pdev->dev, "request IRQ:%d fail\n", rtc->irq);
353 rtc->irq = -1;
354 } else {
355 device_init_wakeup(&pdev->dev, 1);
356 enable_irq_wake(rtc->irq);
357 }
358 }
359 return 0;
360
361fail:
362 if (!IS_ERR_OR_NULL(rtc->rtc))
363 rtc_device_unregister(rtc->rtc);
364 kfree(rtc);
365 return err;
366}
367
368static int __devexit ricoh583_rtc_remove(struct platform_device *pdev)
369{
370 struct ricoh583_rtc *rtc = dev_get_drvdata(&pdev->dev);
371
372 if (rtc->irq != -1)
373 free_irq(rtc->irq, rtc);
374 rtc_device_unregister(rtc->rtc);
375 kfree(rtc);
376 return 0;
377}
378
379static struct platform_driver ricoh583_rtc_driver = {
380 .driver = {
381 .name = "rtc_ricoh583",
382 .owner = THIS_MODULE,
383 },
384 .probe = ricoh583_rtc_probe,
385 .remove = __devexit_p(ricoh583_rtc_remove),
386};
387
388static int __init ricoh583_rtc_init(void)
389{
390 return platform_driver_register(&ricoh583_rtc_driver);
391}
392module_init(ricoh583_rtc_init);
393
394static void __exit ricoh583_rtc_exit(void)
395{
396 platform_driver_unregister(&ricoh583_rtc_driver);
397}
398module_exit(ricoh583_rtc_exit);
399
400MODULE_DESCRIPTION("RICOH PMU ricoh583 RTC driver");
401MODULE_AUTHOR("NVIDIA Corporation");
402MODULE_LICENSE("GPL");
403MODULE_ALIAS("platform:rtc_ricoh583");