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>
27 # define _DBG(x, fmt, args...) do{ if (debug>=x) printk(KERN_DEBUG"%s: " fmt "\n", __FUNCTION__, ##args); } while(0);
29 # define _DBG(x, fmt, args...) do { } while(0);
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);
38 struct i2c_client client
;
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)
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
,
71 .probe_range
= ignore
,
73 .ignore_range
= 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
)
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
);
97 ret
= i2c_transfer(client
->adapter
, msgs
, 2);
106 static int rtc8564_write(struct i2c_client
*client
, unsigned char adr
,
107 unsigned char *data
, unsigned char len
)
110 unsigned char _data
[16];
114 if (!data
|| len
> 15) {
119 _DBG(1, "client=%p, adr=%d, buf=%p, len=%d", client
, adr
, data
, len
);
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
;
132 ret
= i2c_transfer(client
->adapter
, &wr
, 1);
141 static int rtc8564_attach(struct i2c_adapter
*adap
, int addr
, int kind
)
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] = {
151 struct i2c_msg ctrl_rd
[2] = {
153 {addr
, I2C_M_RD
, 2, data
}
156 d
= kmalloc(sizeof(struct rtc8564_data
), GFP_KERNEL
);
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
);
176 ret
= i2c_transfer(new_client
->adapter
, ctrl_wr
, 1);
178 printk(KERN_INFO
"rtc8564: cant init ctrl1\n");
183 /* read back ctrl1 and ctrl2 */
184 ret
= i2c_transfer(new_client
->adapter
, ctrl_rd
, 2);
186 printk(KERN_INFO
"rtc8564: cant read ctrl\n");
191 d
->ctrl
= data
[0] | (data
[1] << 8);
193 _DBG(1, "RTC8564_REG_CTRL1=%02x, RTC8564_REG_CTRL2=%02x",
196 ret
= i2c_attach_client(new_client
);
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
));
216 static int rtc8564_get_datetime(struct i2c_client
*client
, struct rtc_tm
*dt
)
219 unsigned char buf
[15];
221 _DBG(1, "client=%p, dt=%p", client
, dt
);
226 memset(buf
, 0, sizeof(buf
));
228 ret
= rtc8564_read(client
, 0, buf
, 15);
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);
250 rtc8564_set_datetime(struct i2c_client
*client
, struct rtc_tm
*dt
, int datetoo
)
253 unsigned char buf
[15];
255 _DBG(1, "client=%p, dt=%p", client
, 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
);
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
);
280 _DBG(1, "error writing data! %d", ret
);
283 buf
[RTC8564_REG_CTRL1
] = CTRL1(client
);
284 ret
= rtc8564_write(client
, 0, buf
, 1);
286 _DBG(1, "error writing data! %d", ret
);
292 static int rtc8564_get_ctrl(struct i2c_client
*client
, unsigned int *ctrl
)
294 struct rtc8564_data
*data
= i2c_get_clientdata(client
);
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];
311 buf
[0] = *ctrl
& 0xff;
312 buf
[1] = (*ctrl
& 0xff00) >> 8;
315 return rtc8564_write(client
, 0, buf
, 2);
318 static int rtc8564_read_mem(struct i2c_client
*client
, struct mem
*mem
)
324 return rtc8564_read(client
, mem
->loc
, mem
->data
, mem
->nr
);
327 static int rtc8564_write_mem(struct i2c_client
*client
, struct mem
*mem
)
333 return rtc8564_write(client
, mem
->loc
, mem
->data
, mem
->nr
);
337 rtc8564_command(struct i2c_client
*client
, unsigned int cmd
, void *arg
)
340 _DBG(1, "cmd=%d", cmd
);
343 case RTC_GETDATETIME
:
344 return rtc8564_get_datetime(client
, arg
);
347 return rtc8564_set_datetime(client
, arg
, 0);
349 case RTC_SETDATETIME
:
350 return rtc8564_set_datetime(client
, arg
, 1);
353 return rtc8564_get_ctrl(client
, arg
);
356 return rtc8564_set_ctrl(client
, arg
);
359 return rtc8564_read_mem(client
, arg
);
362 return rtc8564_write_mem(client
, arg
);
369 static struct i2c_driver rtc8564_driver
= {
370 .owner
= THIS_MODULE
,
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
);