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>
26 # define _DBG(x, fmt, args...) do{ if (debug>=x) printk(KERN_DEBUG"%s: " fmt "\n", __FUNCTION__, ##args); } while(0);
28 # define _DBG(x, fmt, args...) do { } while(0);
31 #define _DBGRTCTM(x, rtctm) if (debug>=x) printk("%s: secs=%d, mins=%d, hours=%d, mday=%d, " \
32 "mon=%d, year=%d, wday=%d VL=%d\n", __FUNCTION__, \
33 (rtctm).secs, (rtctm).mins, (rtctm).hours, (rtctm).mday, \
34 (rtctm).mon, (rtctm).year, (rtctm).wday, (rtctm).vl);
37 struct i2c_client client;
41 static inline u8 _rtc8564_ctrl1(struct i2c_client *client)
43 struct rtc8564_data *data = i2c_get_clientdata(client);
44 return data->ctrl & 0xff;
46 static inline u8 _rtc8564_ctrl2(struct i2c_client *client)
48 struct rtc8564_data *data = i2c_get_clientdata(client);
49 return (data->ctrl & 0xff00) >> 8;
52 #define CTRL1(c) _rtc8564_ctrl1(c)
53 #define CTRL2(c) _rtc8564_ctrl2(c)
55 #define BCD_TO_BIN(val) (((val)&15) + ((val)>>4)*10)
56 #define BIN_TO_BCD(val) ((((val)/10)<<4) + (val)%10)
59 module_param(debug, int, S_IRUGO | S_IWUSR);
61 static struct i2c_driver rtc8564_driver;
63 static unsigned short ignore[] = { I2C_CLIENT_END };
64 static unsigned short normal_addr[] = { 0x51, I2C_CLIENT_END };
66 static struct i2c_client_address_data addr_data = {
67 .normal_i2c = normal_addr,
72 static int rtc8564_read_mem(struct i2c_client *client, struct mem *mem);
73 static int rtc8564_write_mem(struct i2c_client *client, struct mem *mem);
75 static int rtc8564_read(struct i2c_client *client, unsigned char adr,
76 unsigned char *buf, unsigned char len)
79 unsigned char addr[1] = { adr };
80 struct i2c_msg msgs[2] = {
81 {client->addr, 0, 1, addr},
82 {client->addr, I2C_M_RD, len, buf}
85 _DBG(1, "client=%p, adr=%d, buf=%p, len=%d", client, adr, buf, len);
92 ret = i2c_transfer(client->adapter, msgs, 2);
101 static int rtc8564_write(struct i2c_client *client, unsigned char adr,
102 unsigned char *data, unsigned char len)
105 unsigned char _data[16];
109 if (!data || len > 15) {
114 _DBG(1, "client=%p, adr=%d, buf=%p, len=%d", client, adr, data, len);
117 for (i = 0; i < len; i++) {
118 _data[i + 1] = data[i];
119 _DBG(5, "data[%d] = 0x%02x (%d)", i, data[i], data[i]);
122 wr.addr = client->addr;
127 ret = i2c_transfer(client->adapter, &wr, 1);
136 static int rtc8564_attach(struct i2c_adapter *adap, int addr, int kind)
139 struct i2c_client *new_client;
140 struct rtc8564_data *d;
141 unsigned char data[10];
142 unsigned char ad[1] = { 0 };
143 struct i2c_msg ctrl_wr[1] = {
146 struct i2c_msg ctrl_rd[2] = {
148 {addr, I2C_M_RD, 2, data}
151 d = kmalloc(sizeof(struct rtc8564_data), GFP_KERNEL);
156 memset(d, 0, sizeof(struct rtc8564_data));
157 new_client = &d->client;
159 strlcpy(new_client->name, "RTC8564", I2C_NAME_SIZE);
160 i2c_set_clientdata(new_client, d);
161 new_client->flags = I2C_CLIENT_ALLOW_USE | I2C_DF_NOTIFY;
162 new_client->addr = addr;
163 new_client->adapter = adap;
164 new_client->driver = &rtc8564_driver;
166 _DBG(1, "client=%p", new_client);
171 ret = i2c_transfer(new_client->adapter, ctrl_wr, 1);
173 printk(KERN_INFO "rtc8564: cant init ctrl1\n");
178 /* read back ctrl1 and ctrl2 */
179 ret = i2c_transfer(new_client->adapter, ctrl_rd, 2);
181 printk(KERN_INFO "rtc8564: cant read ctrl\n");
186 d->ctrl = data[0] | (data[1] << 8);
188 _DBG(1, "RTC8564_REG_CTRL1=%02x, RTC8564_REG_CTRL2=%02x",
191 ret = i2c_attach_client(new_client);
199 static int rtc8564_probe(struct i2c_adapter *adap)
201 return i2c_probe(adap, &addr_data, rtc8564_attach);
204 static int rtc8564_detach(struct i2c_client *client)
206 i2c_detach_client(client);
207 kfree(i2c_get_clientdata(client));
211 static int rtc8564_get_datetime(struct i2c_client *client, struct rtc_tm *dt)
214 unsigned char buf[15];
216 _DBG(1, "client=%p, dt=%p", client, dt);
221 memset(buf, 0, sizeof(buf));
223 ret = rtc8564_read(client, 0, buf, 15);
227 /* century stored in minute alarm reg */
228 dt->year = BCD_TO_BIN(buf[RTC8564_REG_YEAR]);
229 dt->year += 100 * BCD_TO_BIN(buf[RTC8564_REG_AL_MIN] & 0x3f);
230 dt->mday = BCD_TO_BIN(buf[RTC8564_REG_DAY] & 0x3f);
231 dt->wday = BCD_TO_BIN(buf[RTC8564_REG_WDAY] & 7);
232 dt->mon = BCD_TO_BIN(buf[RTC8564_REG_MON_CENT] & 0x1f);
234 dt->secs = BCD_TO_BIN(buf[RTC8564_REG_SEC] & 0x7f);
235 dt->vl = (buf[RTC8564_REG_SEC] & 0x80) == 0x80;
236 dt->mins = BCD_TO_BIN(buf[RTC8564_REG_MIN] & 0x7f);
237 dt->hours = BCD_TO_BIN(buf[RTC8564_REG_HR] & 0x3f);
245 rtc8564_set_datetime(struct i2c_client *client, struct rtc_tm *dt, int datetoo)
248 unsigned char buf[15];
250 _DBG(1, "client=%p, dt=%p", client, dt);
257 buf[RTC8564_REG_CTRL1] = CTRL1(client) | RTC8564_CTRL1_STOP;
258 buf[RTC8564_REG_CTRL2] = CTRL2(client);
259 buf[RTC8564_REG_SEC] = BIN_TO_BCD(dt->secs);
260 buf[RTC8564_REG_MIN] = BIN_TO_BCD(dt->mins);
261 buf[RTC8564_REG_HR] = BIN_TO_BCD(dt->hours);
265 buf[RTC8564_REG_DAY] = BIN_TO_BCD(dt->mday);
266 buf[RTC8564_REG_WDAY] = BIN_TO_BCD(dt->wday);
267 buf[RTC8564_REG_MON_CENT] = BIN_TO_BCD(dt->mon) & 0x1f;
268 /* century stored in minute alarm reg */
269 buf[RTC8564_REG_YEAR] = BIN_TO_BCD(dt->year % 100);
270 buf[RTC8564_REG_AL_MIN] = BIN_TO_BCD(dt->year / 100);
273 ret = rtc8564_write(client, 0, buf, len);
275 _DBG(1, "error writing data! %d", ret);
278 buf[RTC8564_REG_CTRL1] = CTRL1(client);
279 ret = rtc8564_write(client, 0, buf, 1);
281 _DBG(1, "error writing data! %d", ret);
287 static int rtc8564_get_ctrl(struct i2c_client *client, unsigned int *ctrl)
289 struct rtc8564_data *data = i2c_get_clientdata(client);
298 static int rtc8564_set_ctrl(struct i2c_client *client, unsigned int *ctrl)
300 struct rtc8564_data *data = i2c_get_clientdata(client);
301 unsigned char buf[2];
306 buf[0] = *ctrl & 0xff;
307 buf[1] = (*ctrl & 0xff00) >> 8;
310 return rtc8564_write(client, 0, buf, 2);
313 static int rtc8564_read_mem(struct i2c_client *client, struct mem *mem)
319 return rtc8564_read(client, mem->loc, mem->data, mem->nr);
322 static int rtc8564_write_mem(struct i2c_client *client, struct mem *mem)
328 return rtc8564_write(client, mem->loc, mem->data, mem->nr);
332 rtc8564_command(struct i2c_client *client, unsigned int cmd, void *arg)
335 _DBG(1, "cmd=%d", cmd);
338 case RTC_GETDATETIME:
339 return rtc8564_get_datetime(client, arg);
342 return rtc8564_set_datetime(client, arg, 0);
344 case RTC_SETDATETIME:
345 return rtc8564_set_datetime(client, arg, 1);
348 return rtc8564_get_ctrl(client, arg);
351 return rtc8564_set_ctrl(client, arg);
354 return rtc8564_read_mem(client, arg);
357 return rtc8564_write_mem(client, arg);
364 static struct i2c_driver rtc8564_driver = {
365 .owner = THIS_MODULE,
367 .id = I2C_DRIVERID_RTC8564,
368 .flags = I2C_DF_NOTIFY,
369 .attach_adapter = rtc8564_probe,
370 .detach_client = rtc8564_detach,
371 .command = rtc8564_command
374 static __init int rtc8564_init(void)
376 return i2c_add_driver(&rtc8564_driver);
379 static __exit void rtc8564_exit(void)
381 i2c_del_driver(&rtc8564_driver);
384 MODULE_AUTHOR("Stefan Eletzhofer <Stefan.Eletzhofer@eletztrick.de>");
385 MODULE_DESCRIPTION("EPSON RTC8564 Driver");
386 MODULE_LICENSE("GPL");
388 module_init(rtc8564_init);
389 module_exit(rtc8564_exit);