summaryrefslogtreecommitdiff
path: root/target/linux/generic/patches-3.8/840-rtc7301.patch
blob: 89b5d46fc9016771e4873f39c042950bef040bc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
--- a/drivers/rtc/Kconfig
+++ b/drivers/rtc/Kconfig
@@ -781,6 +781,15 @@ config RTC_DRV_NUC900
 	  If you say yes here you get support for the RTC subsystem of the
 	  NUC910/NUC920 used in embedded systems.
 
+config RTC_DRV_RTC7301
+	tristate "Epson RTC-7301 SF/DG"
+	help
+	  If you say Y here you will get support for the
+	  Epson RTC-7301 SF/DG RTC chips.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called rtc-7301.
+
 comment "on-CPU RTC drivers"
 
 config RTC_DRV_DAVINCI
--- a/drivers/rtc/Makefile
+++ b/drivers/rtc/Makefile
@@ -94,6 +94,7 @@ obj-$(CONFIG_RTC_DRV_RP5C01)	+= rtc-rp5c
 obj-$(CONFIG_RTC_DRV_RS5C313)	+= rtc-rs5c313.o
 obj-$(CONFIG_RTC_DRV_RS5C348)	+= rtc-rs5c348.o
 obj-$(CONFIG_RTC_DRV_RS5C372)	+= rtc-rs5c372.o
+obj-$(CONFIG_RTC_DRV_RTC7301)	+= rtc-rtc7301.o
 obj-$(CONFIG_RTC_DRV_RV3029C2)	+= rtc-rv3029c2.o
 obj-$(CONFIG_RTC_DRV_RX8025)	+= rtc-rx8025.o
 obj-$(CONFIG_RTC_DRV_RX8581)	+= rtc-rx8581.o
--- /dev/null
+++ b/drivers/rtc/rtc-rtc7301.c
@@ -0,0 +1,219 @@
+/*
+ * Driver for Epson RTC-7301SF/DG
+ *
+ * Copyright (C) 2009 Jose Vasconcellos
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <linux/module.h>
+#include <linux/rtc.h>
+#include <linux/platform_device.h>
+#include <linux/io.h>
+#include <linux/delay.h>
+#include <linux/bcd.h>
+
+#define RTC_NAME "rtc7301"
+#define RTC_VERSION "0.1"
+
+/* Epson RTC-7301 register addresses */
+#define RTC7301_SEC		0x00
+#define RTC7301_SEC10		0x01
+#define RTC7301_MIN		0x02
+#define RTC7301_MIN10		0x03
+#define RTC7301_HOUR		0x04
+#define RTC7301_HOUR10		0x05
+#define RTC7301_WEEKDAY		0x06
+#define RTC7301_DAY		0x07
+#define RTC7301_DAY10		0x08
+#define RTC7301_MON		0x09
+#define RTC7301_MON10		0x0A
+#define RTC7301_YEAR		0x0B
+#define RTC7301_YEAR10		0x0C
+#define RTC7301_YEAR100		0x0D
+#define RTC7301_YEAR1000	0x0E
+#define RTC7301_CTRLREG		0x0F
+
+static uint8_t __iomem *rtc7301_base;
+
+#define read_reg(offset) (readb(rtc7301_base + offset) & 0xf)
+#define write_reg(offset, data) writeb(data, rtc7301_base + (offset))
+
+#define rtc7301_isbusy() (read_reg(RTC7301_CTRLREG) & 1)
+
+static void rtc7301_init_settings(void)
+{
+	int i;
+
+	write_reg(RTC7301_CTRLREG, 2);
+	write_reg(RTC7301_YEAR1000, 2);
+	udelay(122);
+
+	/* bank 1 */
+	write_reg(RTC7301_CTRLREG, 6);
+	for (i=0; i<15; i++)
+		write_reg(i, 0);
+
+	/* bank 2 */
+	write_reg(RTC7301_CTRLREG, 14);
+	for (i=0; i<15; i++)
+		write_reg(i, 0);
+	write_reg(RTC7301_CTRLREG, 0);
+}
+
+static int rtc7301_get_datetime(struct device *dev, struct rtc_time *dt)
+{
+	int cnt;
+	uint8_t buf[16];
+
+	cnt = 0;
+	while (rtc7301_isbusy()) {
+		udelay(244);
+		if (cnt++ > 100) {
+			dev_err(dev, "%s: timeout error %x\n", __func__, rtc7301_base[RTC7301_CTRLREG]);
+			return -EIO;
+		}
+	}
+
+	for (cnt=0; cnt<16; cnt++)
+		buf[cnt] = read_reg(cnt);
+
+	if (buf[RTC7301_SEC10] & 8) {
+		dev_err(dev, "%s: RTC not set\n", __func__);
+		return -EINVAL;
+	}
+
+	memset(dt, 0, sizeof(*dt));
+
+	dt->tm_sec =  buf[RTC7301_SEC] + buf[RTC7301_SEC10]*10;
+	dt->tm_min =  buf[RTC7301_MIN] + buf[RTC7301_MIN10]*10;
+	dt->tm_hour = buf[RTC7301_HOUR] + buf[RTC7301_HOUR10]*10;
+
+	dt->tm_mday = buf[RTC7301_DAY] + buf[RTC7301_DAY10]*10;
+	dt->tm_mon =  buf[RTC7301_MON] + buf[RTC7301_MON10]*10 - 1;
+	dt->tm_year = buf[RTC7301_YEAR] + buf[RTC7301_YEAR10]*10 +
+		      buf[RTC7301_YEAR100]*100 +
+		      ((buf[RTC7301_YEAR1000] & 3)*1000) - 1900;
+
+	/* the rtc device may contain illegal values on power up
+	 * according to the data sheet. make sure they are valid.
+	 */
+
+	return rtc_valid_tm(dt);
+}
+
+static int rtc7301_set_datetime(struct device *dev, struct rtc_time *dt)
+{
+	int data;
+
+	data = dt->tm_year + 1900;
+	if (data >= 2100 || data < 1900)
+		return -EINVAL;
+
+	write_reg(RTC7301_CTRLREG, 2);
+       	udelay(122);
+
+	data = bin2bcd(dt->tm_sec);
+	write_reg(RTC7301_SEC, data);
+	write_reg(RTC7301_SEC10, (data >> 4));
+
+	data = bin2bcd(dt->tm_min);
+	write_reg(RTC7301_MIN, data );
+	write_reg(RTC7301_MIN10, (data >> 4));
+
+	data = bin2bcd(dt->tm_hour);
+	write_reg(RTC7301_HOUR, data);
+	write_reg(RTC7301_HOUR10, (data >> 4));
+
+	data = bin2bcd(dt->tm_mday);
+	write_reg(RTC7301_DAY, data);
+	write_reg(RTC7301_DAY10, (data>> 4));
+
+	data = bin2bcd(dt->tm_mon + 1);
+	write_reg(RTC7301_MON, data);
+	write_reg(RTC7301_MON10, (data >> 4));
+
+	data = bin2bcd(dt->tm_year % 100);
+	write_reg(RTC7301_YEAR, data);
+	write_reg(RTC7301_YEAR10, (data >> 4));
+	data = bin2bcd((1900 + dt->tm_year) / 100);
+	write_reg(RTC7301_YEAR100, data);
+
+	data = bin2bcd(dt->tm_wday);
+	write_reg(RTC7301_WEEKDAY, data);
+
+	write_reg(RTC7301_CTRLREG, 0);
+
+	return 0;
+}
+
+static const struct rtc_class_ops rtc7301_rtc_ops = {
+	.read_time	= rtc7301_get_datetime,
+	.set_time	= rtc7301_set_datetime,
+};
+
+static int __devinit rtc7301_probe(struct platform_device *pdev)
+{
+	struct rtc_device *rtc;
+	struct resource *res;
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	if (!res)
+		return -ENOENT;
+
+	rtc7301_base = ioremap_nocache(res->start, 0x1000 /*res->end - res->start + 1*/);
+	if (!rtc7301_base)
+		return -EINVAL;
+
+	rtc = rtc_device_register(RTC_NAME, &pdev->dev,
+				&rtc7301_rtc_ops, THIS_MODULE);
+	if (IS_ERR(rtc)) {
+		iounmap(rtc7301_base);
+		return PTR_ERR(rtc);
+	}
+
+	platform_set_drvdata(pdev, rtc);
+
+	rtc7301_init_settings();
+	return 0;
+}
+
+static int __devexit rtc7301_remove(struct platform_device *pdev)
+{
+	struct rtc_device *rtc = platform_get_drvdata(pdev);
+
+	if (rtc)
+		rtc_device_unregister(rtc);
+	if (rtc7301_base)
+		iounmap(rtc7301_base);
+	return 0;
+}
+
+static struct platform_driver rtc7301_driver = {
+	.driver = {
+		.name	= RTC_NAME,
+		.owner	= THIS_MODULE,
+	},
+	.probe	= rtc7301_probe,
+	.remove = __devexit_p(rtc7301_remove),
+};
+
+static __init int rtc7301_init(void)
+{
+	return platform_driver_register(&rtc7301_driver);
+}
+module_init(rtc7301_init);
+
+static __exit void rtc7301_exit(void)
+{
+	platform_driver_unregister(&rtc7301_driver);
+}
+module_exit(rtc7301_exit);
+
+MODULE_DESCRIPTION("Epson 7301 RTC driver");
+MODULE_AUTHOR("Jose Vasconcellos <jvasco@verizon.net>");
+MODULE_LICENSE("GPL");
+MODULE_ALIAS("platform:" RTC_NAME);
+MODULE_VERSION(RTC_VERSION);