2 * drivers/rtc/rtc-ricoh619.c
4 * Real time clock driver for RICOH RC5T619 power management chip.
6 * Copyright (C) 2012-2013 RICOH COMPANY,LTD
9 * Copyright (C) 2011 NVIDIA Corporation
11 * this program is free software; you can redistribute it and/or modify
12 * it under the terms of the gnu general public license as published by
13 * the free software foundation; either version 2 of the license, or
14 * (at your option) any later version.
16 * this program is distributed in the hope that it will be useful, but without
17 * any warranty; without even the implied warranty of merchantability or
18 * fitness for a particular purpose. see the gnu general public license for
21 * you should have received a copy of the gnu general public license
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 /* #define verbose_debug 1 */
29 #include <linux/device.h>
30 #include <linux/err.h>
31 #include <linux/init.h>
32 #include <linux/kernel.h>
33 #include <linux/mfd/ricoh619.h>
34 #include <linux/rtc/rtc-ricoh619.h>
35 #include <linux/platform_device.h>
36 #include <linux/rtc.h>
37 #include <linux/slab.h>
40 unsigned long epoch_start;
42 struct rtc_device *rtc;
46 static int ricoh619_read_regs(struct device *dev, int reg, int len,
51 ret = ricoh619_bulk_reads(dev->parent, reg, len, val);
53 dev_err(dev->parent, "\n %s failed reading from 0x%02x\n",
60 static int ricoh619_write_regs(struct device *dev, int reg, int len,
64 ret = ricoh619_bulk_writes(dev->parent, reg, len, val);
66 dev_err(dev->parent, "\n %s failed writing\n", __func__);
73 static int ricoh619_rtc_valid_tm(struct device *dev, struct rtc_time *tm)
75 if (tm->tm_year >= (rtc_year_offset + 99)
78 || tm->tm_mday > rtc_month_days(tm->tm_mon,
79 tm->tm_year + os_ref_year)
82 || tm->tm_sec >= 60) {
83 dev_err(dev->parent, "\n returning error due to time"
84 "%d/%d/%d %d:%d:%d", tm->tm_mon, tm->tm_mday,
85 tm->tm_year, tm->tm_hour, tm->tm_min, tm->tm_sec);
91 static u8 dec2bcd(u8 dec)
93 return ((dec/10)<<4)+(dec%10);
96 static u8 bcd2dec(u8 bcd)
98 return (bcd >> 4)*10+(bcd & 0xf);
101 static void convert_bcd_to_decimal(u8 *buf, u8 len)
104 for (i = 0; i < len; i++)
105 buf[i] = bcd2dec(buf[i]);
108 static void convert_decimal_to_bcd(u8 *buf, u8 len)
111 for (i = 0; i < len; i++)
112 buf[i] = dec2bcd(buf[i]);
115 static void print_time(struct device *dev, struct rtc_time *tm)
117 dev_info(dev, "rtc-time : %d/%d/%d %d:%d\n",
118 (tm->tm_mon + 1), tm->tm_mday, (tm->tm_year + os_ref_year),
119 tm->tm_hour, tm->tm_min);
122 static int ricoh619_rtc_read_time(struct device *dev, struct rtc_time *tm)
126 err = ricoh619_read_regs(dev, rtc_seconds_reg, sizeof(buff), buff);
128 dev_err(dev, "\n %s :: failed to read time\n", __FILE__);
131 convert_bcd_to_decimal(buff, sizeof(buff));
132 tm->tm_sec = buff[0];
133 tm->tm_min = buff[1];
134 tm->tm_hour = buff[2];
135 tm->tm_wday = buff[3];
136 tm->tm_mday = buff[4];
137 tm->tm_mon = buff[5] - 1;
138 tm->tm_year = buff[6] + rtc_year_offset;
139 // print_time(dev, tm);
140 return ricoh619_rtc_valid_tm(dev, tm);
143 static int ricoh619_rtc_set_time(struct device *dev, struct rtc_time *tm)
148 // print_time(dev, tm);
149 buff[0] = tm->tm_sec;
150 buff[1] = tm->tm_min;
151 buff[2] = tm->tm_hour;
152 buff[3] = tm->tm_wday;
153 buff[4] = tm->tm_mday;
154 buff[5] = tm->tm_mon + 1;
155 buff[6] = tm->tm_year - rtc_year_offset;
157 convert_decimal_to_bcd(buff, sizeof(buff));
158 err = ricoh619_write_regs(dev, rtc_seconds_reg, sizeof(buff), buff);
160 dev_err(dev->parent, "\n failed to program new time\n");
166 static int ricoh619_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm);
168 static int ricoh619_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
170 struct ricoh619_rtc *rtc = dev_get_drvdata(dev);
171 unsigned long seconds;
179 rtc_tm_to_time(&alrm->time, &seconds);
180 err = ricoh619_rtc_read_time(dev, &tm);
182 dev_err(dev, "\n failed to read time\n");
185 rtc_tm_to_time(&tm, &rtc->epoch_start);
187 dev_info(dev->parent, "\n setting alarm to requested time::\n");
188 // print_time(dev->parent, &alrm->time);
190 if (WARN_ON(alrm->enabled && (seconds < rtc->epoch_start))) {
191 dev_err(dev->parent, "\n can't set alarm to requested time\n");
195 if (alrm->enabled && !rtc->irq_en)
197 else if (!alrm->enabled && rtc->irq_en)
200 buff[0] = alrm->time.tm_sec;
201 buff[1] = alrm->time.tm_min;
202 buff[2] = alrm->time.tm_hour;
203 buff[3] = alrm->time.tm_mday;
204 buff[4] = alrm->time.tm_mon + 1;
205 buff[5] = alrm->time.tm_year - rtc_year_offset;
206 convert_decimal_to_bcd(buff, sizeof(buff));
207 buff[3] |= 0x80; /* set DAL_EXT */
208 err = ricoh619_write_regs(dev, rtc_alarm_y_sec, sizeof(buff), buff);
210 dev_err(dev->parent, "\n unable to set alarm\n");
214 err = ricoh619_read_regs(dev, rtc_ctrl2, 1, buff);
216 dev_err(dev->parent, "unable to read rtc_ctrl2 reg\n");
220 buff[1] = buff[0] & ~0x81; /* to clear alarm-D flag, and set adjustment parameter */
221 buff[0] = 0x60; /* to enable alarm_d and 24-hour format */
222 err = ricoh619_write_regs(dev, rtc_ctrl1, 2, buff);
224 dev_err(dev, "failed programming rtc ctrl regs\n");
230 static int ricoh619_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
235 err = ricoh619_read_regs(dev, rtc_alarm_y_sec, sizeof(buff), buff);
238 buff[3] &= ~0x80; /* clear DAL_EXT */
239 convert_bcd_to_decimal(buff, sizeof(buff));
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;
248 // dev_info(dev->parent, "\n getting alarm time::\n");
249 // print_time(dev, &alrm->time);
254 static const struct rtc_class_ops ricoh619_rtc_ops = {
255 .read_time = ricoh619_rtc_read_time,
256 .set_time = ricoh619_rtc_set_time,
257 .set_alarm = ricoh619_rtc_set_alarm,
258 .read_alarm = ricoh619_rtc_read_alarm,
261 static irqreturn_t ricoh619_rtc_irq(int irq, void *data)
263 struct device *dev = data;
264 struct ricoh619_rtc *rtc = dev_get_drvdata(dev);
268 /* clear alarm-D status bits.*/
269 err = ricoh619_read_regs(dev, rtc_ctrl2, 1, ®);
271 dev_err(dev->parent, "unable to read rtc_ctrl2 reg\n");
273 /* to clear alarm-D flag, and set adjustment parameter */
275 err = ricoh619_write_regs(dev, rtc_ctrl2, 1, ®);
277 dev_err(dev->parent, "unable to program rtc_status reg\n");
279 rtc_update_irq(rtc->rtc, 1, RTC_IRQF | RTC_AF);
283 static int __devinit ricoh619_rtc_probe(struct platform_device *pdev)
285 struct ricoh619_rtc_platform_data *pdata = pdev->dev.platform_data;
286 struct ricoh619_rtc *rtc;
290 rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
291 // printk("%s,line=%d\n", __func__,__LINE__);
299 dev_err(&pdev->dev, "no platform_data specified\n");
304 dev_err(&pdev->dev, "\n no irq specified, wakeup is disabled\n");
306 dev_set_drvdata(&pdev->dev, rtc);
307 device_init_wakeup(&pdev->dev, 1);
308 rtc->rtc = rtc_device_register(pdev->name, &pdev->dev,
309 &ricoh619_rtc_ops, THIS_MODULE);
311 if (IS_ERR(rtc->rtc)) {
312 err = PTR_ERR(rtc->rtc);
316 reg = 0x60; /* to enable alarm_d and 24-hour format */
317 err = ricoh619_write_regs(&pdev->dev, rtc_ctrl1, 1, ®);
319 dev_err(&pdev->dev, "failed rtc setup\n");
323 reg = 0; /* clearing RTC Adjust register */
324 err = ricoh619_write_regs(&pdev->dev, rtc_adjust, 1, ®);
326 dev_err(&pdev->dev, "unable to program rtc_adjust reg\n");
329 /* Set default time-1970.1.1-0h:0m:0s if PON is on */
330 err = ricoh619_read_regs(&pdev->dev, rtc_ctrl2, 1, ®);
332 dev_err(&pdev->dev, "\n failed to read rtc ctl2 reg\n");
336 printk("%s,PON=1 -- CTRL2=%x\n", __func__, reg);
344 /* VDET & PON = 0, others are not changed */
346 err = ricoh619_write_regs(&pdev->dev, rtc_ctrl2, 1, ®);
348 dev_err(&pdev->dev, "\n failed to write rtc ctl2 reg\n");
353 err = ricoh619_rtc_read_time(&pdev->dev, &tm);
355 dev_err(&pdev->dev, "\n failed to read time\n");
359 if (ricoh619_rtc_valid_tm(&pdev->dev, &tm)) {
360 if (pdata->time.tm_year < 2000 || pdata->time.tm_year > 2100) {
361 memset(&pdata->time, 0, sizeof(pdata->time));
362 pdata->time.tm_year = rtc_year_offset;
363 pdata->time.tm_mday = 1;
365 pdata->time.tm_year -= os_ref_year;
366 err = ricoh619_rtc_set_time(&pdev->dev, &pdata->time);
368 dev_err(&pdev->dev, "\n failed to set time\n");
372 if (pdata && (pdata->irq >= 0)) {
373 rtc->irq = pdata->irq + RICOH619_IRQ_DALE;
374 err = request_threaded_irq(rtc->irq, NULL, ricoh619_rtc_irq,
375 IRQF_ONESHOT, "rtc_ricoh619",
378 dev_err(&pdev->dev, "request IRQ:%d fail\n", rtc->irq);
381 device_init_wakeup(&pdev->dev, 1);
382 enable_irq_wake(rtc->irq);
388 if (!IS_ERR_OR_NULL(rtc->rtc))
389 rtc_device_unregister(rtc->rtc);
394 static int __devexit ricoh619_rtc_remove(struct platform_device *pdev)
396 struct ricoh619_rtc *rtc = dev_get_drvdata(&pdev->dev);
399 free_irq(rtc->irq, rtc);
400 rtc_device_unregister(rtc->rtc);
405 static struct platform_driver ricoh619_rtc_driver = {
407 .name = "rtc_ricoh619",
408 .owner = THIS_MODULE,
410 .probe = ricoh619_rtc_probe,
411 .remove = __devexit_p(ricoh619_rtc_remove),
414 static int __init ricoh619_rtc_init(void)
416 return platform_driver_register(&ricoh619_rtc_driver);
418 subsys_initcall_sync(ricoh619_rtc_init);
420 static void __exit ricoh619_rtc_exit(void)
422 platform_driver_unregister(&ricoh619_rtc_driver);
424 module_exit(ricoh619_rtc_exit);
426 MODULE_DESCRIPTION("RICOH RICOH619 RTC driver");
427 MODULE_ALIAS("platform:rtc_ricoh619");
428 MODULE_AUTHOR("zhangqing <zhangqing@rock-chips.com>");
429 MODULE_LICENSE("GPL");