kernel: generic: add kernel 4.3
[openwrt.git] / target / linux / generic / patches-4.3 / 840-rtc7301.patch
1 --- a/drivers/rtc/Kconfig
2 +++ b/drivers/rtc/Kconfig
3 @@ -1123,6 +1123,15 @@ config RTC_DRV_ZYNQMP
4           If you say yes here you get support for the RTC controller found on
5           Xilinx Zynq Ultrascale+ MPSoC.
6  
7 +config RTC_DRV_RTC7301
8 +       tristate "Epson RTC-7301 SF/DG"
9 +       help
10 +         If you say Y here you will get support for the
11 +         Epson RTC-7301 SF/DG RTC chips.
12 +
13 +         This driver can also be built as a module. If so, the module
14 +         will be called rtc-7301.
15 +
16  comment "on-CPU RTC drivers"
17  
18  config RTC_DRV_DAVINCI
19 --- a/drivers/rtc/Makefile
20 +++ b/drivers/rtc/Makefile
21 @@ -125,6 +125,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_RX4581)   += rtc-rx4581.o
28  obj-$(CONFIG_RTC_DRV_RX8025)   += rtc-rx8025.o
29 --- /dev/null
30 +++ b/drivers/rtc/rtc-rtc7301.c
31 @@ -0,0 +1,219 @@
32 +/*
33 + * Driver for Epson RTC-7301SF/DG
34 + *
35 + * Copyright (C) 2009 Jose Vasconcellos
36 + *
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.
40 + */
41 +
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>
48 +
49 +#define RTC_NAME "rtc7301"
50 +#define RTC_VERSION "0.1"
51 +
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
69 +
70 +static uint8_t __iomem *rtc7301_base;
71 +
72 +#define read_reg(offset) (readb(rtc7301_base + offset) & 0xf)
73 +#define write_reg(offset, data) writeb(data, rtc7301_base + (offset))
74 +
75 +#define rtc7301_isbusy() (read_reg(RTC7301_CTRLREG) & 1)
76 +
77 +static void rtc7301_init_settings(void)
78 +{
79 +       int i;
80 +
81 +       write_reg(RTC7301_CTRLREG, 2);
82 +       write_reg(RTC7301_YEAR1000, 2);
83 +       udelay(122);
84 +
85 +       /* bank 1 */
86 +       write_reg(RTC7301_CTRLREG, 6);
87 +       for (i=0; i<15; i++)
88 +               write_reg(i, 0);
89 +
90 +       /* bank 2 */
91 +       write_reg(RTC7301_CTRLREG, 14);
92 +       for (i=0; i<15; i++)
93 +               write_reg(i, 0);
94 +       write_reg(RTC7301_CTRLREG, 0);
95 +}
96 +
97 +static int rtc7301_get_datetime(struct device *dev, struct rtc_time *dt)
98 +{
99 +       int cnt;
100 +       uint8_t buf[16];
101 +
102 +       cnt = 0;
103 +       while (rtc7301_isbusy()) {
104 +               udelay(244);
105 +               if (cnt++ > 100) {
106 +                       dev_err(dev, "%s: timeout error %x\n", __func__, rtc7301_base[RTC7301_CTRLREG]);
107 +                       return -EIO;
108 +               }
109 +       }
110 +
111 +       for (cnt=0; cnt<16; cnt++)
112 +               buf[cnt] = read_reg(cnt);
113 +
114 +       if (buf[RTC7301_SEC10] & 8) {
115 +               dev_err(dev, "%s: RTC not set\n", __func__);
116 +               return -EINVAL;
117 +       }
118 +
119 +       memset(dt, 0, sizeof(*dt));
120 +
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;
124 +
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;
130 +
131 +       /* the rtc device may contain illegal values on power up
132 +        * according to the data sheet. make sure they are valid.
133 +        */
134 +
135 +       return rtc_valid_tm(dt);
136 +}
137 +
138 +static int rtc7301_set_datetime(struct device *dev, struct rtc_time *dt)
139 +{
140 +       int data;
141 +
142 +       data = dt->tm_year + 1900;
143 +       if (data >= 2100 || data < 1900)
144 +               return -EINVAL;
145 +
146 +       write_reg(RTC7301_CTRLREG, 2);
147 +               udelay(122);
148 +
149 +       data = bin2bcd(dt->tm_sec);
150 +       write_reg(RTC7301_SEC, data);
151 +       write_reg(RTC7301_SEC10, (data >> 4));
152 +
153 +       data = bin2bcd(dt->tm_min);
154 +       write_reg(RTC7301_MIN, data );
155 +       write_reg(RTC7301_MIN10, (data >> 4));
156 +
157 +       data = bin2bcd(dt->tm_hour);
158 +       write_reg(RTC7301_HOUR, data);
159 +       write_reg(RTC7301_HOUR10, (data >> 4));
160 +
161 +       data = bin2bcd(dt->tm_mday);
162 +       write_reg(RTC7301_DAY, data);
163 +       write_reg(RTC7301_DAY10, (data>> 4));
164 +
165 +       data = bin2bcd(dt->tm_mon + 1);
166 +       write_reg(RTC7301_MON, data);
167 +       write_reg(RTC7301_MON10, (data >> 4));
168 +
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);
174 +
175 +       data = bin2bcd(dt->tm_wday);
176 +       write_reg(RTC7301_WEEKDAY, data);
177 +
178 +       write_reg(RTC7301_CTRLREG, 0);
179 +
180 +       return 0;
181 +}
182 +
183 +static const struct rtc_class_ops rtc7301_rtc_ops = {
184 +       .read_time      = rtc7301_get_datetime,
185 +       .set_time       = rtc7301_set_datetime,
186 +};
187 +
188 +static int rtc7301_probe(struct platform_device *pdev)
189 +{
190 +       struct rtc_device *rtc;
191 +       struct resource *res;
192 +
193 +       res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
194 +       if (!res)
195 +               return -ENOENT;
196 +
197 +       rtc7301_base = ioremap_nocache(res->start, 0x1000 /*res->end - res->start + 1*/);
198 +       if (!rtc7301_base)
199 +               return -EINVAL;
200 +
201 +       rtc = rtc_device_register(RTC_NAME, &pdev->dev,
202 +                               &rtc7301_rtc_ops, THIS_MODULE);
203 +       if (IS_ERR(rtc)) {
204 +               iounmap(rtc7301_base);
205 +               return PTR_ERR(rtc);
206 +       }
207 +
208 +       platform_set_drvdata(pdev, rtc);
209 +
210 +       rtc7301_init_settings();
211 +       return 0;
212 +}
213 +
214 +static int rtc7301_remove(struct platform_device *pdev)
215 +{
216 +       struct rtc_device *rtc = platform_get_drvdata(pdev);
217 +
218 +       if (rtc)
219 +               rtc_device_unregister(rtc);
220 +       if (rtc7301_base)
221 +               iounmap(rtc7301_base);
222 +       return 0;
223 +}
224 +
225 +static struct platform_driver rtc7301_driver = {
226 +       .driver = {
227 +               .name   = RTC_NAME,
228 +               .owner  = THIS_MODULE,
229 +       },
230 +       .probe  = rtc7301_probe,
231 +       .remove = rtc7301_remove,
232 +};
233 +
234 +static __init int rtc7301_init(void)
235 +{
236 +       return platform_driver_register(&rtc7301_driver);
237 +}
238 +module_init(rtc7301_init);
239 +
240 +static __exit void rtc7301_exit(void)
241 +{
242 +       platform_driver_unregister(&rtc7301_driver);
243 +}
244 +module_exit(rtc7301_exit);
245 +
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);