Linux-2.6.12-rc2
[linux-2.6/next.git] / drivers / i2c / chips / rtc8564.c
blob5a9deddb626b318bfd9a9dfd46708961fff480fa
1 /*
2 * linux/drivers/i2c/chips/rtc8564.c
4 * Copyright (C) 2002-2004 Stefan Eletzhofer
6 * based on linux/drivers/acron/char/pcf8583.c
7 * Copyright (C) 2000 Russell King
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.
13 * Driver for system3's EPSON RTC 8564 chip
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>
24 #include "rtc8564.h"
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
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);
37 struct rtc8564_data {
38 struct i2c_client client;
39 u16 ctrl;
42 static inline u8 _rtc8564_ctrl1(struct i2c_client *client)
44 struct rtc8564_data *data = i2c_get_clientdata(client);
45 return data->ctrl & 0xff;
47 static inline u8 _rtc8564_ctrl2(struct i2c_client *client)
49 struct rtc8564_data *data = i2c_get_clientdata(client);
50 return (data->ctrl & 0xff00) >> 8;
53 #define CTRL1(c) _rtc8564_ctrl1(c)
54 #define CTRL2(c) _rtc8564_ctrl2(c)
56 #define BCD_TO_BIN(val) (((val)&15) + ((val)>>4)*10)
57 #define BIN_TO_BCD(val) ((((val)/10)<<4) + (val)%10)
59 static int debug;;
60 module_param(debug, int, S_IRUGO | S_IWUSR);
62 static struct i2c_driver rtc8564_driver;
64 static unsigned short ignore[] = { I2C_CLIENT_END };
65 static unsigned short normal_addr[] = { 0x51, I2C_CLIENT_END };
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,
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);
80 static int rtc8564_read(struct i2c_client *client, unsigned char adr,
81 unsigned char *buf, unsigned char len)
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}
90 _DBG(1, "client=%p, adr=%d, buf=%p, len=%d", client, adr, buf, len);
92 if (!buf) {
93 ret = -EINVAL;
94 goto done;
97 ret = i2c_transfer(client->adapter, msgs, 2);
98 if (ret == 2) {
99 ret = 0;
102 done:
103 return ret;
106 static int rtc8564_write(struct i2c_client *client, unsigned char adr,
107 unsigned char *data, unsigned char len)
109 int ret = 0;
110 unsigned char _data[16];
111 struct i2c_msg wr;
112 int i;
114 if (!data || len > 15) {
115 ret = -EINVAL;
116 goto done;
119 _DBG(1, "client=%p, adr=%d, buf=%p, len=%d", client, adr, data, len);
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]);
127 wr.addr = client->addr;
128 wr.flags = 0;
129 wr.len = len + 1;
130 wr.buf = _data;
132 ret = i2c_transfer(client->adapter, &wr, 1);
133 if (ret == 1) {
134 ret = 0;
137 done:
138 return ret;
141 static int rtc8564_attach(struct i2c_adapter *adap, int addr, int kind)
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}
151 struct i2c_msg ctrl_rd[2] = {
152 {addr, 0, 1, ad},
153 {addr, I2C_M_RD, 2, data}
156 d = kmalloc(sizeof(struct rtc8564_data), GFP_KERNEL);
157 if (!d) {
158 ret = -ENOMEM;
159 goto done;
161 memset(d, 0, sizeof(struct rtc8564_data));
162 new_client = &d->client;
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;
171 _DBG(1, "client=%p", new_client);
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;
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;
191 d->ctrl = data[0] | (data[1] << 8);
193 _DBG(1, "RTC8564_REG_CTRL1=%02x, RTC8564_REG_CTRL2=%02x",
194 data[0], data[1]);
196 ret = i2c_attach_client(new_client);
197 done:
198 if (ret) {
199 kfree(d);
201 return ret;
204 static int rtc8564_probe(struct i2c_adapter *adap)
206 return i2c_probe(adap, &addr_data, rtc8564_attach);
209 static int rtc8564_detach(struct i2c_client *client)
211 i2c_detach_client(client);
212 kfree(i2c_get_clientdata(client));
213 return 0;
216 static int rtc8564_get_datetime(struct i2c_client *client, struct rtc_tm *dt)
218 int ret = -EIO;
219 unsigned char buf[15];
221 _DBG(1, "client=%p, dt=%p", client, dt);
223 if (!dt)
224 return -EINVAL;
226 memset(buf, 0, sizeof(buf));
228 ret = rtc8564_read(client, 0, buf, 15);
229 if (ret)
230 return ret;
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);
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);
244 _DBGRTCTM(2, *dt);
246 return 0;
249 static int
250 rtc8564_set_datetime(struct i2c_client *client, struct rtc_tm *dt, int datetoo)
252 int ret, len = 5;
253 unsigned char buf[15];
255 _DBG(1, "client=%p, dt=%p", client, dt);
257 if (!dt)
258 return -EINVAL;
260 _DBGRTCTM(2, *dt);
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);
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);
278 ret = rtc8564_write(client, 0, buf, len);
279 if (ret) {
280 _DBG(1, "error writing data! %d", ret);
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);
289 return ret;
292 static int rtc8564_get_ctrl(struct i2c_client *client, unsigned int *ctrl)
294 struct rtc8564_data *data = i2c_get_clientdata(client);
296 if (!ctrl)
297 return -1;
299 *ctrl = data->ctrl;
300 return 0;
303 static int rtc8564_set_ctrl(struct i2c_client *client, unsigned int *ctrl)
305 struct rtc8564_data *data = i2c_get_clientdata(client);
306 unsigned char buf[2];
308 if (!ctrl)
309 return -1;
311 buf[0] = *ctrl & 0xff;
312 buf[1] = (*ctrl & 0xff00) >> 8;
313 data->ctrl = *ctrl;
315 return rtc8564_write(client, 0, buf, 2);
318 static int rtc8564_read_mem(struct i2c_client *client, struct mem *mem)
321 if (!mem)
322 return -EINVAL;
324 return rtc8564_read(client, mem->loc, mem->data, mem->nr);
327 static int rtc8564_write_mem(struct i2c_client *client, struct mem *mem)
330 if (!mem)
331 return -EINVAL;
333 return rtc8564_write(client, mem->loc, mem->data, mem->nr);
336 static int
337 rtc8564_command(struct i2c_client *client, unsigned int cmd, void *arg)
340 _DBG(1, "cmd=%d", cmd);
342 switch (cmd) {
343 case RTC_GETDATETIME:
344 return rtc8564_get_datetime(client, arg);
346 case RTC_SETTIME:
347 return rtc8564_set_datetime(client, arg, 0);
349 case RTC_SETDATETIME:
350 return rtc8564_set_datetime(client, arg, 1);
352 case RTC_GETCTRL:
353 return rtc8564_get_ctrl(client, arg);
355 case RTC_SETCTRL:
356 return rtc8564_set_ctrl(client, arg);
358 case MEM_READ:
359 return rtc8564_read_mem(client, arg);
361 case MEM_WRITE:
362 return rtc8564_write_mem(client, arg);
364 default:
365 return -EINVAL;
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
379 static __init int rtc8564_init(void)
381 return i2c_add_driver(&rtc8564_driver);
384 static __exit void rtc8564_exit(void)
386 i2c_del_driver(&rtc8564_driver);
389 MODULE_AUTHOR("Stefan Eletzhofer <Stefan.Eletzhofer@eletztrick.de>");
390 MODULE_DESCRIPTION("EPSON RTC8564 Driver");
391 MODULE_LICENSE("GPL");
393 module_init(rtc8564_init);
394 module_exit(rtc8564_exit);