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
,
73 static int rtc8564_read_mem(struct i2c_client
*client
, struct mem
*mem
);
74 static int rtc8564_write_mem(struct i2c_client
*client
, struct mem
*mem
);
76 static int rtc8564_read(struct i2c_client
*client
, unsigned char adr
,
77 unsigned char *buf
, unsigned char len
)
80 unsigned char addr
[1] = { adr
};
81 struct i2c_msg msgs
[2] = {
82 {client
->addr
, 0, 1, addr
},
83 {client
->addr
, I2C_M_RD
, len
, buf
}
86 _DBG(1, "client=%p, adr=%d, buf=%p, len=%d", client
, adr
, buf
, len
);
93 ret
= i2c_transfer(client
->adapter
, msgs
, 2);
102 static int rtc8564_write(struct i2c_client
*client
, unsigned char adr
,
103 unsigned char *data
, unsigned char len
)
106 unsigned char _data
[16];
110 if (!data
|| len
> 15) {
115 _DBG(1, "client=%p, adr=%d, buf=%p, len=%d", client
, adr
, data
, len
);
118 for (i
= 0; i
< len
; i
++) {
119 _data
[i
+ 1] = data
[i
];
120 _DBG(5, "data[%d] = 0x%02x (%d)", i
, data
[i
], data
[i
]);
123 wr
.addr
= client
->addr
;
128 ret
= i2c_transfer(client
->adapter
, &wr
, 1);
137 static int rtc8564_attach(struct i2c_adapter
*adap
, int addr
, int kind
)
140 struct i2c_client
*new_client
;
141 struct rtc8564_data
*d
;
142 unsigned char data
[10];
143 unsigned char ad
[1] = { 0 };
144 struct i2c_msg ctrl_wr
[1] = {
147 struct i2c_msg ctrl_rd
[2] = {
149 {addr
, I2C_M_RD
, 2, data
}
152 d
= kmalloc(sizeof(struct rtc8564_data
), GFP_KERNEL
);
157 memset(d
, 0, sizeof(struct rtc8564_data
));
158 new_client
= &d
->client
;
160 strlcpy(new_client
->name
, "RTC8564", I2C_NAME_SIZE
);
161 i2c_set_clientdata(new_client
, d
);
162 new_client
->flags
= I2C_CLIENT_ALLOW_USE
| I2C_DF_NOTIFY
;
163 new_client
->addr
= addr
;
164 new_client
->adapter
= adap
;
165 new_client
->driver
= &rtc8564_driver
;
167 _DBG(1, "client=%p", new_client
);
172 ret
= i2c_transfer(new_client
->adapter
, ctrl_wr
, 1);
174 printk(KERN_INFO
"rtc8564: cant init ctrl1\n");
179 /* read back ctrl1 and ctrl2 */
180 ret
= i2c_transfer(new_client
->adapter
, ctrl_rd
, 2);
182 printk(KERN_INFO
"rtc8564: cant read ctrl\n");
187 d
->ctrl
= data
[0] | (data
[1] << 8);
189 _DBG(1, "RTC8564_REG_CTRL1=%02x, RTC8564_REG_CTRL2=%02x",
192 ret
= i2c_attach_client(new_client
);
200 static int rtc8564_probe(struct i2c_adapter
*adap
)
202 return i2c_probe(adap
, &addr_data
, rtc8564_attach
);
205 static int rtc8564_detach(struct i2c_client
*client
)
207 i2c_detach_client(client
);
208 kfree(i2c_get_clientdata(client
));
212 static int rtc8564_get_datetime(struct i2c_client
*client
, struct rtc_tm
*dt
)
215 unsigned char buf
[15];
217 _DBG(1, "client=%p, dt=%p", client
, dt
);
222 memset(buf
, 0, sizeof(buf
));
224 ret
= rtc8564_read(client
, 0, buf
, 15);
228 /* century stored in minute alarm reg */
229 dt
->year
= BCD_TO_BIN(buf
[RTC8564_REG_YEAR
]);
230 dt
->year
+= 100 * BCD_TO_BIN(buf
[RTC8564_REG_AL_MIN
] & 0x3f);
231 dt
->mday
= BCD_TO_BIN(buf
[RTC8564_REG_DAY
] & 0x3f);
232 dt
->wday
= BCD_TO_BIN(buf
[RTC8564_REG_WDAY
] & 7);
233 dt
->mon
= BCD_TO_BIN(buf
[RTC8564_REG_MON_CENT
] & 0x1f);
235 dt
->secs
= BCD_TO_BIN(buf
[RTC8564_REG_SEC
] & 0x7f);
236 dt
->vl
= (buf
[RTC8564_REG_SEC
] & 0x80) == 0x80;
237 dt
->mins
= BCD_TO_BIN(buf
[RTC8564_REG_MIN
] & 0x7f);
238 dt
->hours
= BCD_TO_BIN(buf
[RTC8564_REG_HR
] & 0x3f);
246 rtc8564_set_datetime(struct i2c_client
*client
, struct rtc_tm
*dt
, int datetoo
)
249 unsigned char buf
[15];
251 _DBG(1, "client=%p, dt=%p", client
, dt
);
258 buf
[RTC8564_REG_CTRL1
] = CTRL1(client
) | RTC8564_CTRL1_STOP
;
259 buf
[RTC8564_REG_CTRL2
] = CTRL2(client
);
260 buf
[RTC8564_REG_SEC
] = BIN_TO_BCD(dt
->secs
);
261 buf
[RTC8564_REG_MIN
] = BIN_TO_BCD(dt
->mins
);
262 buf
[RTC8564_REG_HR
] = BIN_TO_BCD(dt
->hours
);
266 buf
[RTC8564_REG_DAY
] = BIN_TO_BCD(dt
->mday
);
267 buf
[RTC8564_REG_WDAY
] = BIN_TO_BCD(dt
->wday
);
268 buf
[RTC8564_REG_MON_CENT
] = BIN_TO_BCD(dt
->mon
) & 0x1f;
269 /* century stored in minute alarm reg */
270 buf
[RTC8564_REG_YEAR
] = BIN_TO_BCD(dt
->year
% 100);
271 buf
[RTC8564_REG_AL_MIN
] = BIN_TO_BCD(dt
->year
/ 100);
274 ret
= rtc8564_write(client
, 0, buf
, len
);
276 _DBG(1, "error writing data! %d", ret
);
279 buf
[RTC8564_REG_CTRL1
] = CTRL1(client
);
280 ret
= rtc8564_write(client
, 0, buf
, 1);
282 _DBG(1, "error writing data! %d", ret
);
288 static int rtc8564_get_ctrl(struct i2c_client
*client
, unsigned int *ctrl
)
290 struct rtc8564_data
*data
= i2c_get_clientdata(client
);
299 static int rtc8564_set_ctrl(struct i2c_client
*client
, unsigned int *ctrl
)
301 struct rtc8564_data
*data
= i2c_get_clientdata(client
);
302 unsigned char buf
[2];
307 buf
[0] = *ctrl
& 0xff;
308 buf
[1] = (*ctrl
& 0xff00) >> 8;
311 return rtc8564_write(client
, 0, buf
, 2);
314 static int rtc8564_read_mem(struct i2c_client
*client
, struct mem
*mem
)
320 return rtc8564_read(client
, mem
->loc
, mem
->data
, mem
->nr
);
323 static int rtc8564_write_mem(struct i2c_client
*client
, struct mem
*mem
)
329 return rtc8564_write(client
, mem
->loc
, mem
->data
, mem
->nr
);
333 rtc8564_command(struct i2c_client
*client
, unsigned int cmd
, void *arg
)
336 _DBG(1, "cmd=%d", cmd
);
339 case RTC_GETDATETIME
:
340 return rtc8564_get_datetime(client
, arg
);
343 return rtc8564_set_datetime(client
, arg
, 0);
345 case RTC_SETDATETIME
:
346 return rtc8564_set_datetime(client
, arg
, 1);
349 return rtc8564_get_ctrl(client
, arg
);
352 return rtc8564_set_ctrl(client
, arg
);
355 return rtc8564_read_mem(client
, arg
);
358 return rtc8564_write_mem(client
, arg
);
365 static struct i2c_driver rtc8564_driver
= {
366 .owner
= THIS_MODULE
,
368 .id
= I2C_DRIVERID_RTC8564
,
369 .flags
= I2C_DF_NOTIFY
,
370 .attach_adapter
= rtc8564_probe
,
371 .detach_client
= rtc8564_detach
,
372 .command
= rtc8564_command
375 static __init
int rtc8564_init(void)
377 return i2c_add_driver(&rtc8564_driver
);
380 static __exit
void rtc8564_exit(void)
382 i2c_del_driver(&rtc8564_driver
);
385 MODULE_AUTHOR("Stefan Eletzhofer <Stefan.Eletzhofer@eletztrick.de>");
386 MODULE_DESCRIPTION("EPSON RTC8564 Driver");
387 MODULE_LICENSE("GPL");
389 module_init(rtc8564_init
);
390 module_exit(rtc8564_exit
);