diff options
Diffstat (limited to 'drivers/i2c/chips/rtc8564.c')
-rw-r--r-- | drivers/i2c/chips/rtc8564.c | 394 |
1 files changed, 394 insertions, 0 deletions
diff --git a/drivers/i2c/chips/rtc8564.c b/drivers/i2c/chips/rtc8564.c new file mode 100644 index 000000000000..5a9deddb626b --- /dev/null +++ b/drivers/i2c/chips/rtc8564.c | |||
@@ -0,0 +1,394 @@ | |||
1 | /* | ||
2 | * linux/drivers/i2c/chips/rtc8564.c | ||
3 | * | ||
4 | * Copyright (C) 2002-2004 Stefan Eletzhofer | ||
5 | * | ||
6 | * based on linux/drivers/acron/char/pcf8583.c | ||
7 | * Copyright (C) 2000 Russell King | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License version 2 as | ||
11 | * published by the Free Software Foundation. | ||
12 | * | ||
13 | * Driver for system3's EPSON RTC 8564 chip | ||
14 | */ | ||
15 | #include <linux/module.h> | ||
16 | #include <linux/kernel.h> | ||
17 | #include <linux/i2c.h> | ||
18 | #include <linux/slab.h> | ||
19 | #include <linux/string.h> | ||
20 | #include <linux/rtc.h> /* get the user-level API */ | ||
21 | #include <linux/init.h> | ||
22 | #include <linux/init.h> | ||
23 | |||
24 | #include "rtc8564.h" | ||
25 | |||
26 | #ifdef DEBUG | ||
27 | # define _DBG(x, fmt, args...) do{ if (debug>=x) printk(KERN_DEBUG"%s: " fmt "\n", __FUNCTION__, ##args); } while(0); | ||
28 | #else | ||
29 | # define _DBG(x, fmt, args...) do { } while(0); | ||
30 | #endif | ||
31 | |||
32 | #define _DBGRTCTM(x, rtctm) if (debug>=x) printk("%s: secs=%d, mins=%d, hours=%d, mday=%d, " \ | ||
33 | "mon=%d, year=%d, wday=%d VL=%d\n", __FUNCTION__, \ | ||
34 | (rtctm).secs, (rtctm).mins, (rtctm).hours, (rtctm).mday, \ | ||
35 | (rtctm).mon, (rtctm).year, (rtctm).wday, (rtctm).vl); | ||
36 | |||
37 | struct rtc8564_data { | ||
38 | struct i2c_client client; | ||
39 | u16 ctrl; | ||
40 | }; | ||
41 | |||
42 | static inline u8 _rtc8564_ctrl1(struct i2c_client *client) | ||
43 | { | ||
44 | struct rtc8564_data *data = i2c_get_clientdata(client); | ||
45 | return data->ctrl & 0xff; | ||
46 | } | ||
47 | static inline u8 _rtc8564_ctrl2(struct i2c_client *client) | ||
48 | { | ||
49 | struct rtc8564_data *data = i2c_get_clientdata(client); | ||
50 | return (data->ctrl & 0xff00) >> 8; | ||
51 | } | ||
52 | |||
53 | #define CTRL1(c) _rtc8564_ctrl1(c) | ||
54 | #define CTRL2(c) _rtc8564_ctrl2(c) | ||
55 | |||
56 | #define BCD_TO_BIN(val) (((val)&15) + ((val)>>4)*10) | ||
57 | #define BIN_TO_BCD(val) ((((val)/10)<<4) + (val)%10) | ||
58 | |||
59 | static int debug;; | ||
60 | module_param(debug, int, S_IRUGO | S_IWUSR); | ||
61 | |||
62 | static struct i2c_driver rtc8564_driver; | ||
63 | |||
64 | static unsigned short ignore[] = { I2C_CLIENT_END }; | ||
65 | static unsigned short normal_addr[] = { 0x51, I2C_CLIENT_END }; | ||
66 | |||
67 | static struct i2c_client_address_data addr_data = { | ||
68 | .normal_i2c = normal_addr, | ||
69 | .normal_i2c_range = ignore, | ||
70 | .probe = ignore, | ||
71 | .probe_range = ignore, | ||
72 | .ignore = ignore, | ||
73 | .ignore_range = ignore, | ||
74 | .force = ignore, | ||
75 | }; | ||
76 | |||
77 | static int rtc8564_read_mem(struct i2c_client *client, struct mem *mem); | ||
78 | static int rtc8564_write_mem(struct i2c_client *client, struct mem *mem); | ||
79 | |||
80 | static int rtc8564_read(struct i2c_client *client, unsigned char adr, | ||
81 | unsigned char *buf, unsigned char len) | ||
82 | { | ||
83 | int ret = -EIO; | ||
84 | unsigned char addr[1] = { adr }; | ||
85 | struct i2c_msg msgs[2] = { | ||
86 | {client->addr, 0, 1, addr}, | ||
87 | {client->addr, I2C_M_RD, len, buf} | ||
88 | }; | ||
89 | |||
90 | _DBG(1, "client=%p, adr=%d, buf=%p, len=%d", client, adr, buf, len); | ||
91 | |||
92 | if (!buf) { | ||
93 | ret = -EINVAL; | ||
94 | goto done; | ||
95 | } | ||
96 | |||
97 | ret = i2c_transfer(client->adapter, msgs, 2); | ||
98 | if (ret == 2) { | ||
99 | ret = 0; | ||
100 | } | ||
101 | |||
102 | done: | ||
103 | return ret; | ||
104 | } | ||
105 | |||
106 | static int rtc8564_write(struct i2c_client *client, unsigned char adr, | ||
107 | unsigned char *data, unsigned char len) | ||
108 | { | ||
109 | int ret = 0; | ||
110 | unsigned char _data[16]; | ||
111 | struct i2c_msg wr; | ||
112 | int i; | ||
113 | |||
114 | if (!data || len > 15) { | ||
115 | ret = -EINVAL; | ||
116 | goto done; | ||
117 | } | ||
118 | |||
119 | _DBG(1, "client=%p, adr=%d, buf=%p, len=%d", client, adr, data, len); | ||
120 | |||
121 | _data[0] = adr; | ||
122 | for (i = 0; i < len; i++) { | ||
123 | _data[i + 1] = data[i]; | ||
124 | _DBG(5, "data[%d] = 0x%02x (%d)", i, data[i], data[i]); | ||
125 | } | ||
126 | |||
127 | wr.addr = client->addr; | ||
128 | wr.flags = 0; | ||
129 | wr.len = len + 1; | ||
130 | wr.buf = _data; | ||
131 | |||
132 | ret = i2c_transfer(client->adapter, &wr, 1); | ||
133 | if (ret == 1) { | ||
134 | ret = 0; | ||
135 | } | ||
136 | |||
137 | done: | ||
138 | return ret; | ||
139 | } | ||
140 | |||
141 | static int rtc8564_attach(struct i2c_adapter *adap, int addr, int kind) | ||
142 | { | ||
143 | int ret; | ||
144 | struct i2c_client *new_client; | ||
145 | struct rtc8564_data *d; | ||
146 | unsigned char data[10]; | ||
147 | unsigned char ad[1] = { 0 }; | ||
148 | struct i2c_msg ctrl_wr[1] = { | ||
149 | {addr, 0, 2, data} | ||
150 | }; | ||
151 | struct i2c_msg ctrl_rd[2] = { | ||
152 | {addr, 0, 1, ad}, | ||
153 | {addr, I2C_M_RD, 2, data} | ||
154 | }; | ||
155 | |||
156 | d = kmalloc(sizeof(struct rtc8564_data), GFP_KERNEL); | ||
157 | if (!d) { | ||
158 | ret = -ENOMEM; | ||
159 | goto done; | ||
160 | } | ||
161 | memset(d, 0, sizeof(struct rtc8564_data)); | ||
162 | new_client = &d->client; | ||
163 | |||
164 | strlcpy(new_client->name, "RTC8564", I2C_NAME_SIZE); | ||
165 | i2c_set_clientdata(new_client, d); | ||
166 | new_client->flags = I2C_CLIENT_ALLOW_USE | I2C_DF_NOTIFY; | ||
167 | new_client->addr = addr; | ||
168 | new_client->adapter = adap; | ||
169 | new_client->driver = &rtc8564_driver; | ||
170 | |||
171 | _DBG(1, "client=%p", new_client); | ||
172 | |||
173 | /* init ctrl1 reg */ | ||
174 | data[0] = 0; | ||
175 | data[1] = 0; | ||
176 | ret = i2c_transfer(new_client->adapter, ctrl_wr, 1); | ||
177 | if (ret != 1) { | ||
178 | printk(KERN_INFO "rtc8564: cant init ctrl1\n"); | ||
179 | ret = -ENODEV; | ||
180 | goto done; | ||
181 | } | ||
182 | |||
183 | /* read back ctrl1 and ctrl2 */ | ||
184 | ret = i2c_transfer(new_client->adapter, ctrl_rd, 2); | ||
185 | if (ret != 2) { | ||
186 | printk(KERN_INFO "rtc8564: cant read ctrl\n"); | ||
187 | ret = -ENODEV; | ||
188 | goto done; | ||
189 | } | ||
190 | |||
191 | d->ctrl = data[0] | (data[1] << 8); | ||
192 | |||
193 | _DBG(1, "RTC8564_REG_CTRL1=%02x, RTC8564_REG_CTRL2=%02x", | ||
194 | data[0], data[1]); | ||
195 | |||
196 | ret = i2c_attach_client(new_client); | ||
197 | done: | ||
198 | if (ret) { | ||
199 | kfree(d); | ||
200 | } | ||
201 | return ret; | ||
202 | } | ||
203 | |||
204 | static int rtc8564_probe(struct i2c_adapter *adap) | ||
205 | { | ||
206 | return i2c_probe(adap, &addr_data, rtc8564_attach); | ||
207 | } | ||
208 | |||
209 | static int rtc8564_detach(struct i2c_client *client) | ||
210 | { | ||
211 | i2c_detach_client(client); | ||
212 | kfree(i2c_get_clientdata(client)); | ||
213 | return 0; | ||
214 | } | ||
215 | |||
216 | static int rtc8564_get_datetime(struct i2c_client *client, struct rtc_tm *dt) | ||
217 | { | ||
218 | int ret = -EIO; | ||
219 | unsigned char buf[15]; | ||
220 | |||
221 | _DBG(1, "client=%p, dt=%p", client, dt); | ||
222 | |||
223 | if (!dt) | ||
224 | return -EINVAL; | ||
225 | |||
226 | memset(buf, 0, sizeof(buf)); | ||
227 | |||
228 | ret = rtc8564_read(client, 0, buf, 15); | ||
229 | if (ret) | ||
230 | return ret; | ||
231 | |||
232 | /* century stored in minute alarm reg */ | ||
233 | dt->year = BCD_TO_BIN(buf[RTC8564_REG_YEAR]); | ||
234 | dt->year += 100 * BCD_TO_BIN(buf[RTC8564_REG_AL_MIN] & 0x3f); | ||
235 | dt->mday = BCD_TO_BIN(buf[RTC8564_REG_DAY] & 0x3f); | ||
236 | dt->wday = BCD_TO_BIN(buf[RTC8564_REG_WDAY] & 7); | ||
237 | dt->mon = BCD_TO_BIN(buf[RTC8564_REG_MON_CENT] & 0x1f); | ||
238 | |||
239 | dt->secs = BCD_TO_BIN(buf[RTC8564_REG_SEC] & 0x7f); | ||
240 | dt->vl = (buf[RTC8564_REG_SEC] & 0x80) == 0x80; | ||
241 | dt->mins = BCD_TO_BIN(buf[RTC8564_REG_MIN] & 0x7f); | ||
242 | dt->hours = BCD_TO_BIN(buf[RTC8564_REG_HR] & 0x3f); | ||
243 | |||
244 | _DBGRTCTM(2, *dt); | ||
245 | |||
246 | return 0; | ||
247 | } | ||
248 | |||
249 | static int | ||
250 | rtc8564_set_datetime(struct i2c_client *client, struct rtc_tm *dt, int datetoo) | ||
251 | { | ||
252 | int ret, len = 5; | ||
253 | unsigned char buf[15]; | ||
254 | |||
255 | _DBG(1, "client=%p, dt=%p", client, dt); | ||
256 | |||
257 | if (!dt) | ||
258 | return -EINVAL; | ||
259 | |||
260 | _DBGRTCTM(2, *dt); | ||
261 | |||
262 | buf[RTC8564_REG_CTRL1] = CTRL1(client) | RTC8564_CTRL1_STOP; | ||
263 | buf[RTC8564_REG_CTRL2] = CTRL2(client); | ||
264 | buf[RTC8564_REG_SEC] = BIN_TO_BCD(dt->secs); | ||
265 | buf[RTC8564_REG_MIN] = BIN_TO_BCD(dt->mins); | ||
266 | buf[RTC8564_REG_HR] = BIN_TO_BCD(dt->hours); | ||
267 | |||
268 | if (datetoo) { | ||
269 | len += 5; | ||
270 | buf[RTC8564_REG_DAY] = BIN_TO_BCD(dt->mday); | ||
271 | buf[RTC8564_REG_WDAY] = BIN_TO_BCD(dt->wday); | ||
272 | buf[RTC8564_REG_MON_CENT] = BIN_TO_BCD(dt->mon) & 0x1f; | ||
273 | /* century stored in minute alarm reg */ | ||
274 | buf[RTC8564_REG_YEAR] = BIN_TO_BCD(dt->year % 100); | ||
275 | buf[RTC8564_REG_AL_MIN] = BIN_TO_BCD(dt->year / 100); | ||
276 | } | ||
277 | |||
278 | ret = rtc8564_write(client, 0, buf, len); | ||
279 | if (ret) { | ||
280 | _DBG(1, "error writing data! %d", ret); | ||
281 | } | ||
282 | |||
283 | buf[RTC8564_REG_CTRL1] = CTRL1(client); | ||
284 | ret = rtc8564_write(client, 0, buf, 1); | ||
285 | if (ret) { | ||
286 | _DBG(1, "error writing data! %d", ret); | ||
287 | } | ||
288 | |||
289 | return ret; | ||
290 | } | ||
291 | |||
292 | static int rtc8564_get_ctrl(struct i2c_client *client, unsigned int *ctrl) | ||
293 | { | ||
294 | struct rtc8564_data *data = i2c_get_clientdata(client); | ||
295 | |||
296 | if (!ctrl) | ||
297 | return -1; | ||
298 | |||
299 | *ctrl = data->ctrl; | ||
300 | return 0; | ||
301 | } | ||
302 | |||
303 | static int rtc8564_set_ctrl(struct i2c_client *client, unsigned int *ctrl) | ||
304 | { | ||
305 | struct rtc8564_data *data = i2c_get_clientdata(client); | ||
306 | unsigned char buf[2]; | ||
307 | |||
308 | if (!ctrl) | ||
309 | return -1; | ||
310 | |||
311 | buf[0] = *ctrl & 0xff; | ||
312 | buf[1] = (*ctrl & 0xff00) >> 8; | ||
313 | data->ctrl = *ctrl; | ||
314 | |||
315 | return rtc8564_write(client, 0, buf, 2); | ||
316 | } | ||
317 | |||
318 | static int rtc8564_read_mem(struct i2c_client *client, struct mem *mem) | ||
319 | { | ||
320 | |||
321 | if (!mem) | ||
322 | return -EINVAL; | ||
323 | |||
324 | return rtc8564_read(client, mem->loc, mem->data, mem->nr); | ||
325 | } | ||
326 | |||
327 | static int rtc8564_write_mem(struct i2c_client *client, struct mem *mem) | ||
328 | { | ||
329 | |||
330 | if (!mem) | ||
331 | return -EINVAL; | ||
332 | |||
333 | return rtc8564_write(client, mem->loc, mem->data, mem->nr); | ||
334 | } | ||
335 | |||
336 | static int | ||
337 | rtc8564_command(struct i2c_client *client, unsigned int cmd, void *arg) | ||
338 | { | ||
339 | |||
340 | _DBG(1, "cmd=%d", cmd); | ||
341 | |||
342 | switch (cmd) { | ||
343 | case RTC_GETDATETIME: | ||
344 | return rtc8564_get_datetime(client, arg); | ||
345 | |||
346 | case RTC_SETTIME: | ||
347 | return rtc8564_set_datetime(client, arg, 0); | ||
348 | |||
349 | case RTC_SETDATETIME: | ||
350 | return rtc8564_set_datetime(client, arg, 1); | ||
351 | |||
352 | case RTC_GETCTRL: | ||
353 | return rtc8564_get_ctrl(client, arg); | ||
354 | |||
355 | case RTC_SETCTRL: | ||
356 | return rtc8564_set_ctrl(client, arg); | ||
357 | |||
358 | case MEM_READ: | ||
359 | return rtc8564_read_mem(client, arg); | ||
360 | |||
361 | case MEM_WRITE: | ||
362 | return rtc8564_write_mem(client, arg); | ||
363 | |||
364 | default: | ||
365 | return -EINVAL; | ||
366 | } | ||
367 | } | ||
368 | |||
369 | static struct i2c_driver rtc8564_driver = { | ||
370 | .owner = THIS_MODULE, | ||
371 | .name = "RTC8564", | ||
372 | .id = I2C_DRIVERID_RTC8564, | ||
373 | .flags = I2C_DF_NOTIFY, | ||
374 | .attach_adapter = rtc8564_probe, | ||
375 | .detach_client = rtc8564_detach, | ||
376 | .command = rtc8564_command | ||
377 | }; | ||
378 | |||
379 | static __init int rtc8564_init(void) | ||
380 | { | ||
381 | return i2c_add_driver(&rtc8564_driver); | ||
382 | } | ||
383 | |||
384 | static __exit void rtc8564_exit(void) | ||
385 | { | ||
386 | i2c_del_driver(&rtc8564_driver); | ||
387 | } | ||
388 | |||
389 | MODULE_AUTHOR("Stefan Eletzhofer <Stefan.Eletzhofer@eletztrick.de>"); | ||
390 | MODULE_DESCRIPTION("EPSON RTC8564 Driver"); | ||
391 | MODULE_LICENSE("GPL"); | ||
392 | |||
393 | module_init(rtc8564_init); | ||
394 | module_exit(rtc8564_exit); | ||