aboutsummaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
authorDale Farnsworth <dale@farnsworth.org>2007-05-08 03:26:39 -0400
committerLinus Torvalds <torvalds@woody.linux-foundation.org>2007-05-08 14:15:03 -0400
commitaa5bd7e929325dbb48be43c3dccf7d1da433e38e (patch)
tree6014547bca7f692020afe89e2abd51e62d351ab7 /drivers
parente9f2bd8191a416fbd17e8bfc002dba5411937997 (diff)
rtc: add RTC class driver for the Maxim MAX6900
Signed-off-by: Dale Farnsworth.org <dale@farnsworth.org> Cc: Alessandro Zummo <a.zummo@towertech.it> Cc: David Brownell <david-b@pacbell.net> Cc: Jean Delvare <khali@linux-fr.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'drivers')
-rw-r--r--drivers/rtc/Kconfig10
-rw-r--r--drivers/rtc/Makefile1
-rw-r--r--drivers/rtc/rtc-max6900.c311
3 files changed, 322 insertions, 0 deletions
diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
index 372d74963dd4..1b853c4ff86d 100644
--- a/drivers/rtc/Kconfig
+++ b/drivers/rtc/Kconfig
@@ -334,6 +334,16 @@ config RTC_DRV_TEST
334 This driver can also be built as a module. If so, the module 334 This driver can also be built as a module. If so, the module
335 will be called rtc-test. 335 will be called rtc-test.
336 336
337config RTC_DRV_MAX6900
338 tristate "Maxim 6900"
339 depends on RTC_CLASS && I2C
340 help
341 If you say yes here you will get support for the
342 Maxim MAX6900 I2C RTC chip.
343
344 This driver can also be built as a module. If so, the module
345 will be called rtc-max6900.
346
337config RTC_DRV_MAX6902 347config RTC_DRV_MAX6902
338 tristate "Maxim 6902" 348 tristate "Maxim 6902"
339 depends on RTC_CLASS && SPI 349 depends on RTC_CLASS && SPI
diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
index cad027d36447..f9a3e0130f4e 100644
--- a/drivers/rtc/Makefile
+++ b/drivers/rtc/Makefile
@@ -35,6 +35,7 @@ obj-$(CONFIG_RTC_DRV_EP93XX) += rtc-ep93xx.o
35obj-$(CONFIG_RTC_DRV_SA1100) += rtc-sa1100.o 35obj-$(CONFIG_RTC_DRV_SA1100) += rtc-sa1100.o
36obj-$(CONFIG_RTC_DRV_VR41XX) += rtc-vr41xx.o 36obj-$(CONFIG_RTC_DRV_VR41XX) += rtc-vr41xx.o
37obj-$(CONFIG_RTC_DRV_PL031) += rtc-pl031.o 37obj-$(CONFIG_RTC_DRV_PL031) += rtc-pl031.o
38obj-$(CONFIG_RTC_DRV_MAX6900) += rtc-max6900.o
38obj-$(CONFIG_RTC_DRV_MAX6902) += rtc-max6902.o 39obj-$(CONFIG_RTC_DRV_MAX6902) += rtc-max6902.o
39obj-$(CONFIG_RTC_DRV_V3020) += rtc-v3020.o 40obj-$(CONFIG_RTC_DRV_V3020) += rtc-v3020.o
40obj-$(CONFIG_RTC_DRV_AT91RM9200)+= rtc-at91rm9200.o 41obj-$(CONFIG_RTC_DRV_AT91RM9200)+= rtc-at91rm9200.o
diff --git a/drivers/rtc/rtc-max6900.c b/drivers/rtc/rtc-max6900.c
new file mode 100644
index 000000000000..eee4ee5bb75a
--- /dev/null
+++ b/drivers/rtc/rtc-max6900.c
@@ -0,0 +1,311 @@
1/*
2 * rtc class driver for the Maxim MAX6900 chip
3 *
4 * Author: Dale Farnsworth <dale@farnsworth.org>
5 *
6 * based on previously existing rtc class drivers
7 *
8 * 2007 (c) MontaVista, Software, Inc. This file is licensed under
9 * the terms of the GNU General Public License version 2. This program
10 * is licensed "as is" without any warranty of any kind, whether express
11 * or implied.
12 */
13
14#include <linux/module.h>
15#include <linux/i2c.h>
16#include <linux/bcd.h>
17#include <linux/rtc.h>
18#include <linux/delay.h>
19
20#define DRV_NAME "max6900"
21#define DRV_VERSION "0.1"
22
23/*
24 * register indices
25 */
26#define MAX6900_REG_SC 0 /* seconds 00-59 */
27#define MAX6900_REG_MN 1 /* minutes 00-59 */
28#define MAX6900_REG_HR 2 /* hours 00-23 */
29#define MAX6900_REG_DT 3 /* day of month 00-31 */
30#define MAX6900_REG_MO 4 /* month 01-12 */
31#define MAX6900_REG_DW 5 /* day of week 1-7 */
32#define MAX6900_REG_YR 6 /* year 00-99 */
33#define MAX6900_REG_CT 7 /* control */
34#define MAX6900_REG_LEN 8
35
36#define MAX6900_REG_CT_WP (1 << 7) /* Write Protect */
37
38/*
39 * register read/write commands
40 */
41#define MAX6900_REG_CONTROL_WRITE 0x8e
42#define MAX6900_REG_BURST_READ 0xbf
43#define MAX6900_REG_BURST_WRITE 0xbe
44#define MAX6900_REG_RESERVED_READ 0x96
45
46#define MAX6900_IDLE_TIME_AFTER_WRITE 3 /* specification says 2.5 mS */
47
48#define MAX6900_I2C_ADDR 0xa0
49
50static unsigned short normal_i2c[] = {
51 MAX6900_I2C_ADDR >> 1,
52 I2C_CLIENT_END
53};
54
55I2C_CLIENT_INSMOD; /* defines addr_data */
56
57static int max6900_probe(struct i2c_adapter *adapter, int addr, int kind);
58
59static int max6900_i2c_read_regs(struct i2c_client *client, u8 *buf)
60{
61 u8 reg_addr[1] = { MAX6900_REG_BURST_READ };
62 struct i2c_msg msgs[2] = {
63 {
64 .addr = client->addr,
65 .flags = 0, /* write */
66 .len = sizeof(reg_addr),
67 .buf = reg_addr
68 },
69 {
70 .addr = client->addr,
71 .flags = I2C_M_RD,
72 .len = MAX6900_REG_LEN,
73 .buf = buf
74 }
75 };
76 int rc;
77
78 rc = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
79 if (rc != ARRAY_SIZE(msgs)) {
80 dev_err(&client->dev, "%s: register read failed\n",
81 __FUNCTION__);
82 return -EIO;
83 }
84 return 0;
85}
86
87static int max6900_i2c_write_regs(struct i2c_client *client, u8 const *buf)
88{
89 u8 i2c_buf[MAX6900_REG_LEN + 1] = { MAX6900_REG_BURST_WRITE };
90 struct i2c_msg msgs[1] = {
91 {
92 .addr = client->addr,
93 .flags = 0, /* write */
94 .len = MAX6900_REG_LEN + 1,
95 .buf = i2c_buf
96 }
97 };
98 int rc;
99
100 memcpy(&i2c_buf[1], buf, MAX6900_REG_LEN);
101
102 rc = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
103 if (rc != ARRAY_SIZE(msgs)) {
104 dev_err(&client->dev, "%s: register write failed\n",
105 __FUNCTION__);
106 return -EIO;
107 }
108 msleep(MAX6900_IDLE_TIME_AFTER_WRITE);
109 return 0;
110}
111
112static int max6900_i2c_validate_client(struct i2c_client *client)
113{
114 u8 regs[MAX6900_REG_LEN];
115 u8 zero_mask[MAX6900_REG_LEN] = {
116 0x80, /* seconds */
117 0x80, /* minutes */
118 0x40, /* hours */
119 0xc0, /* day of month */
120 0xe0, /* month */
121 0xf8, /* day of week */
122 0x00, /* year */
123 0x7f, /* control */
124 };
125 int i;
126 int rc;
127 int reserved;
128
129 reserved = i2c_smbus_read_byte_data(client, MAX6900_REG_RESERVED_READ);
130 if (reserved != 0x07)
131 return -ENODEV;
132
133 rc = max6900_i2c_read_regs(client, regs);
134 if (rc < 0)
135 return rc;
136
137 for (i = 0; i < MAX6900_REG_LEN; ++i) {
138 if (regs[i] & zero_mask[i])
139 return -ENODEV;
140 }
141
142 return 0;
143}
144
145static int max6900_i2c_read_time(struct i2c_client *client, struct rtc_time *tm)
146{
147 int rc;
148 u8 regs[MAX6900_REG_LEN];
149
150 rc = max6900_i2c_read_regs(client, regs);
151 if (rc < 0)
152 return rc;
153
154 tm->tm_sec = BCD2BIN(regs[MAX6900_REG_SC]);
155 tm->tm_min = BCD2BIN(regs[MAX6900_REG_MN]);
156 tm->tm_hour = BCD2BIN(regs[MAX6900_REG_HR] & 0x3f);
157 tm->tm_mday = BCD2BIN(regs[MAX6900_REG_DT]);
158 tm->tm_mon = BCD2BIN(regs[MAX6900_REG_MO]) - 1;
159 tm->tm_year = BCD2BIN(regs[MAX6900_REG_YR]) + 100;
160 tm->tm_wday = BCD2BIN(regs[MAX6900_REG_DW]);
161
162 return 0;
163}
164
165static int max6900_i2c_clear_write_protect(struct i2c_client *client)
166{
167 int rc;
168 rc = i2c_smbus_write_byte_data (client, MAX6900_REG_CONTROL_WRITE, 0);
169 if (rc < 0) {
170 dev_err(&client->dev, "%s: control register write failed\n",
171 __FUNCTION__);
172 return -EIO;
173 }
174 return 0;
175}
176
177static int max6900_i2c_set_time(struct i2c_client *client,
178 struct rtc_time const *tm)
179{
180 u8 regs[MAX6900_REG_LEN];
181 int rc;
182
183 rc = max6900_i2c_clear_write_protect(client);
184 if (rc < 0)
185 return rc;
186
187 regs[MAX6900_REG_SC] = BIN2BCD(tm->tm_sec);
188 regs[MAX6900_REG_MN] = BIN2BCD(tm->tm_min);
189 regs[MAX6900_REG_HR] = BIN2BCD(tm->tm_hour);
190 regs[MAX6900_REG_DT] = BIN2BCD(tm->tm_mday);
191 regs[MAX6900_REG_MO] = BIN2BCD(tm->tm_mon + 1);
192 regs[MAX6900_REG_YR] = BIN2BCD(tm->tm_year - 100);
193 regs[MAX6900_REG_DW] = BIN2BCD(tm->tm_wday);
194 regs[MAX6900_REG_CT] = MAX6900_REG_CT_WP; /* set write protect */
195
196 rc = max6900_i2c_write_regs(client, regs);
197 if (rc < 0)
198 return rc;
199
200 return 0;
201}
202
203static int max6900_rtc_read_time(struct device *dev, struct rtc_time *tm)
204{
205 return max6900_i2c_read_time(to_i2c_client(dev), tm);
206}
207
208static int max6900_rtc_set_time(struct device *dev, struct rtc_time *tm)
209{
210 return max6900_i2c_set_time(to_i2c_client(dev), tm);
211}
212
213static int max6900_attach_adapter(struct i2c_adapter *adapter)
214{
215 return i2c_probe(adapter, &addr_data, max6900_probe);
216}
217
218static int max6900_detach_client(struct i2c_client *client)
219{
220 struct rtc_device *const rtc = i2c_get_clientdata(client);
221
222 if (rtc)
223 rtc_device_unregister(rtc);
224
225 return i2c_detach_client(client);
226}
227
228static struct i2c_driver max6900_driver = {
229 .driver = {
230 .name = DRV_NAME,
231 },
232 .id = I2C_DRIVERID_MAX6900,
233 .attach_adapter = max6900_attach_adapter,
234 .detach_client = max6900_detach_client,
235};
236
237static const struct rtc_class_ops max6900_rtc_ops = {
238 .read_time = max6900_rtc_read_time,
239 .set_time = max6900_rtc_set_time,
240};
241
242static int max6900_probe(struct i2c_adapter *adapter, int addr, int kind)
243{
244 int rc = 0;
245 struct i2c_client *client = NULL;
246 struct rtc_device *rtc = NULL;
247
248 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
249 rc = -ENODEV;
250 goto failout;
251 }
252
253 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
254 if (client == NULL) {
255 rc = -ENOMEM;
256 goto failout;
257 }
258
259 client->addr = addr;
260 client->adapter = adapter;
261 client->driver = &max6900_driver;
262 strlcpy(client->name, DRV_NAME, I2C_NAME_SIZE);
263
264 if (kind < 0) {
265 rc = max6900_i2c_validate_client(client);
266 if (rc < 0)
267 goto failout;
268 }
269
270 rc = i2c_attach_client(client);
271 if (rc < 0)
272 goto failout;
273
274 dev_info(&client->dev,
275 "chip found, driver version " DRV_VERSION "\n");
276
277 rtc = rtc_device_register(max6900_driver.driver.name,
278 &client->dev,
279 &max6900_rtc_ops, THIS_MODULE);
280 if (IS_ERR(rtc)) {
281 rc = PTR_ERR(rtc);
282 goto failout_detach;
283 }
284
285 i2c_set_clientdata(client, rtc);
286
287 return 0;
288
289failout_detach:
290 i2c_detach_client(client);
291failout:
292 kfree(client);
293 return rc;
294}
295
296static int __init max6900_init(void)
297{
298 return i2c_add_driver(&max6900_driver);
299}
300
301static void __exit max6900_exit(void)
302{
303 i2c_del_driver(&max6900_driver);
304}
305
306MODULE_DESCRIPTION("Maxim MAX6900 RTC driver");
307MODULE_LICENSE("GPL");
308MODULE_VERSION(DRV_VERSION);
309
310module_init(max6900_init);
311module_exit(max6900_exit);