1 --- a/drivers/rtc/Kconfig
2 +++ b/drivers/rtc/Kconfig
3 @@ -719,6 +719,15 @@ config RTC_DRV_NUC900
4 If you say yes here you get support for the RTC subsystem of the
5 NUC910/NUC920 used in embedded systems.
7 +config RTC_DRV_RTC7301
8 + tristate "Epson RTC-7301 SF/DG"
10 + If you say Y here you will get support for the
11 + Epson RTC-7301 SF/DG RTC chips.
13 + This driver can also be built as a module. If so, the module
14 + will be called rtc-7301.
16 comment "on-CPU RTC drivers"
18 config RTC_DRV_DAVINCI
19 --- a/drivers/rtc/Makefile
20 +++ b/drivers/rtc/Makefile
21 @@ -86,6 +86,7 @@ obj-$(CONFIG_RTC_DRV_RP5C01) += rtc-rp5c
22 obj-$(CONFIG_RTC_DRV_RS5C313) += rtc-rs5c313.o
23 obj-$(CONFIG_RTC_DRV_RS5C348) += rtc-rs5c348.o
24 obj-$(CONFIG_RTC_DRV_RS5C372) += rtc-rs5c372.o
25 +obj-$(CONFIG_RTC_DRV_RTC7301) += rtc-rtc7301.o
26 obj-$(CONFIG_RTC_DRV_RV3029C2) += rtc-rv3029c2.o
27 obj-$(CONFIG_RTC_DRV_RX8025) += rtc-rx8025.o
28 obj-$(CONFIG_RTC_DRV_RX8581) += rtc-rx8581.o
30 +++ b/drivers/rtc/rtc-rtc7301.c
33 + * Driver for Epson RTC-7301SF/DG
35 + * Copyright (C) 2009 Jose Vasconcellos
37 + * This program is free software; you can redistribute it and/or modify
38 + * it under the terms of the GNU General Public License version 2 as
39 + * published by the Free Software Foundation.
42 +#include <linux/module.h>
43 +#include <linux/rtc.h>
44 +#include <linux/platform_device.h>
45 +#include <linux/io.h>
46 +#include <linux/delay.h>
47 +#include <linux/bcd.h>
49 +#define RTC_NAME "rtc7301"
50 +#define RTC_VERSION "0.1"
52 +/* Epson RTC-7301 register addresses */
53 +#define RTC7301_SEC 0x00
54 +#define RTC7301_SEC10 0x01
55 +#define RTC7301_MIN 0x02
56 +#define RTC7301_MIN10 0x03
57 +#define RTC7301_HOUR 0x04
58 +#define RTC7301_HOUR10 0x05
59 +#define RTC7301_WEEKDAY 0x06
60 +#define RTC7301_DAY 0x07
61 +#define RTC7301_DAY10 0x08
62 +#define RTC7301_MON 0x09
63 +#define RTC7301_MON10 0x0A
64 +#define RTC7301_YEAR 0x0B
65 +#define RTC7301_YEAR10 0x0C
66 +#define RTC7301_YEAR100 0x0D
67 +#define RTC7301_YEAR1000 0x0E
68 +#define RTC7301_CTRLREG 0x0F
70 +static uint8_t __iomem *rtc7301_base;
72 +#define read_reg(offset) (readb(rtc7301_base + offset) & 0xf)
73 +#define write_reg(offset, data) writeb(data, rtc7301_base + (offset))
75 +#define rtc7301_isbusy() (read_reg(RTC7301_CTRLREG) & 1)
77 +static void rtc7301_init_settings(void)
81 + write_reg(RTC7301_CTRLREG, 2);
82 + write_reg(RTC7301_YEAR1000, 2);
86 + write_reg(RTC7301_CTRLREG, 6);
87 + for (i=0; i<15; i++)
91 + write_reg(RTC7301_CTRLREG, 14);
92 + for (i=0; i<15; i++)
94 + write_reg(RTC7301_CTRLREG, 0);
97 +static int rtc7301_get_datetime(struct device *dev, struct rtc_time *dt)
103 + while (rtc7301_isbusy()) {
106 + dev_err(dev, "%s: timeout error %x\n", __func__, rtc7301_base[RTC7301_CTRLREG]);
111 + for (cnt=0; cnt<16; cnt++)
112 + buf[cnt] = read_reg(cnt);
114 + if (buf[RTC7301_SEC10] & 8) {
115 + dev_err(dev, "%s: RTC not set\n", __func__);
119 + memset(dt, 0, sizeof(*dt));
121 + dt->tm_sec = buf[RTC7301_SEC] + buf[RTC7301_SEC10]*10;
122 + dt->tm_min = buf[RTC7301_MIN] + buf[RTC7301_MIN10]*10;
123 + dt->tm_hour = buf[RTC7301_HOUR] + buf[RTC7301_HOUR10]*10;
125 + dt->tm_mday = buf[RTC7301_DAY] + buf[RTC7301_DAY10]*10;
126 + dt->tm_mon = buf[RTC7301_MON] + buf[RTC7301_MON10]*10 - 1;
127 + dt->tm_year = buf[RTC7301_YEAR] + buf[RTC7301_YEAR10]*10 +
128 + buf[RTC7301_YEAR100]*100 +
129 + ((buf[RTC7301_YEAR1000] & 3)*1000) - 1900;
131 + /* the rtc device may contain illegal values on power up
132 + * according to the data sheet. make sure they are valid.
135 + return rtc_valid_tm(dt);
138 +static int rtc7301_set_datetime(struct device *dev, struct rtc_time *dt)
142 + data = dt->tm_year + 1900;
143 + if (data >= 2100 || data < 1900)
146 + write_reg(RTC7301_CTRLREG, 2);
149 + data = bin2bcd(dt->tm_sec);
150 + write_reg(RTC7301_SEC, data);
151 + write_reg(RTC7301_SEC10, (data >> 4));
153 + data = bin2bcd(dt->tm_min);
154 + write_reg(RTC7301_MIN, data );
155 + write_reg(RTC7301_MIN10, (data >> 4));
157 + data = bin2bcd(dt->tm_hour);
158 + write_reg(RTC7301_HOUR, data);
159 + write_reg(RTC7301_HOUR10, (data >> 4));
161 + data = bin2bcd(dt->tm_mday);
162 + write_reg(RTC7301_DAY, data);
163 + write_reg(RTC7301_DAY10, (data>> 4));
165 + data = bin2bcd(dt->tm_mon + 1);
166 + write_reg(RTC7301_MON, data);
167 + write_reg(RTC7301_MON10, (data >> 4));
169 + data = bin2bcd(dt->tm_year % 100);
170 + write_reg(RTC7301_YEAR, data);
171 + write_reg(RTC7301_YEAR10, (data >> 4));
172 + data = bin2bcd((1900 + dt->tm_year) / 100);
173 + write_reg(RTC7301_YEAR100, data);
175 + data = bin2bcd(dt->tm_wday);
176 + write_reg(RTC7301_WEEKDAY, data);
178 + write_reg(RTC7301_CTRLREG, 0);
183 +static const struct rtc_class_ops rtc7301_rtc_ops = {
184 + .read_time = rtc7301_get_datetime,
185 + .set_time = rtc7301_set_datetime,
188 +static int __devinit rtc7301_probe(struct platform_device *pdev)
190 + struct rtc_device *rtc;
191 + struct resource *res;
193 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
197 + rtc7301_base = ioremap_nocache(res->start, 0x1000 /*res->end - res->start + 1*/);
201 + rtc = rtc_device_register(RTC_NAME, &pdev->dev,
202 + &rtc7301_rtc_ops, THIS_MODULE);
204 + iounmap(rtc7301_base);
205 + return PTR_ERR(rtc);
208 + platform_set_drvdata(pdev, rtc);
210 + rtc7301_init_settings();
214 +static int __devexit rtc7301_remove(struct platform_device *pdev)
216 + struct rtc_device *rtc = platform_get_drvdata(pdev);
219 + rtc_device_unregister(rtc);
221 + iounmap(rtc7301_base);
225 +static struct platform_driver rtc7301_driver = {
228 + .owner = THIS_MODULE,
230 + .probe = rtc7301_probe,
231 + .remove = __devexit_p(rtc7301_remove),
234 +static __init int rtc7301_init(void)
236 + return platform_driver_register(&rtc7301_driver);
238 +module_init(rtc7301_init);
240 +static __exit void rtc7301_exit(void)
242 + platform_driver_unregister(&rtc7301_driver);
244 +module_exit(rtc7301_exit);
246 +MODULE_DESCRIPTION("Epson 7301 RTC driver");
247 +MODULE_AUTHOR("Jose Vasconcellos <jvasco@verizon.net>");
248 +MODULE_LICENSE("GPL");
249 +MODULE_ALIAS("platform:" RTC_NAME);
250 +MODULE_VERSION(RTC_VERSION);