2 * drivers/rtc/rtc-pcf8583.c
4 * Copyright (C) 2000 Russell King
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Driver for PCF8583 RTC & RAM chip
12 * Converted to the generic RTC susbsystem by G. Liakhovetski (2006)
14 #include <linux/module.h>
15 #include <linux/i2c.h>
16 #include <linux/slab.h>
17 #include <linux/string.h>
18 #include <linux/mc146818rtc.h>
19 #include <linux/init.h>
20 #include <linux/errno.h>
21 #include <linux/bcd.h>
30 struct i2c_client client
;
31 struct rtc_device
*rtc
;
35 #define CTRL_STOP 0x80
36 #define CTRL_HOLD 0x40
37 #define CTRL_32KHZ 0x00
38 #define CTRL_MASK 0x08
39 #define CTRL_ALARMEN 0x04
40 #define CTRL_ALARM 0x02
41 #define CTRL_TIMER 0x01
43 static unsigned short normal_i2c
[] = { I2C_CLIENT_END
};
45 /* Module parameters */
48 static struct i2c_driver pcf8583_driver
;
50 #define get_ctrl(x) ((struct pcf8583 *)i2c_get_clientdata(x))->ctrl
51 #define set_ctrl(x, v) get_ctrl(x) = v
53 #define CMOS_YEAR (64 + 128)
54 #define CMOS_CHECKSUM (63)
56 static int pcf8583_get_datetime(struct i2c_client
*client
, struct rtc_time
*dt
)
58 unsigned char buf
[8], addr
[1] = { 1 };
59 struct i2c_msg msgs
[2] = {
74 memset(buf
, 0, sizeof(buf
));
76 ret
= i2c_transfer(client
->adapter
, msgs
, 2);
78 dt
->tm_year
= buf
[4] >> 6;
79 dt
->tm_wday
= buf
[5] >> 5;
84 dt
->tm_sec
= BCD_TO_BIN(buf
[1]);
85 dt
->tm_min
= BCD_TO_BIN(buf
[2]);
86 dt
->tm_hour
= BCD_TO_BIN(buf
[3]);
87 dt
->tm_mday
= BCD_TO_BIN(buf
[4]);
88 dt
->tm_mon
= BCD_TO_BIN(buf
[5]);
91 return ret
== 2 ? 0 : -EIO
;
94 static int pcf8583_set_datetime(struct i2c_client
*client
, struct rtc_time
*dt
, int datetoo
)
100 buf
[1] = get_ctrl(client
) | 0x80;
102 buf
[3] = BIN_TO_BCD(dt
->tm_sec
);
103 buf
[4] = BIN_TO_BCD(dt
->tm_min
);
104 buf
[5] = BIN_TO_BCD(dt
->tm_hour
);
108 buf
[6] = BIN_TO_BCD(dt
->tm_mday
) | (dt
->tm_year
<< 6);
109 buf
[7] = BIN_TO_BCD(dt
->tm_mon
) | (dt
->tm_wday
<< 5);
112 ret
= i2c_master_send(client
, (char *)buf
, len
);
116 buf
[1] = get_ctrl(client
);
117 ret
= i2c_master_send(client
, (char *)buf
, 2);
119 return ret
== 2 ? 0 : -EIO
;
122 static int pcf8583_get_ctrl(struct i2c_client
*client
, unsigned char *ctrl
)
124 *ctrl
= get_ctrl(client
);
128 static int pcf8583_set_ctrl(struct i2c_client
*client
, unsigned char *ctrl
)
130 unsigned char buf
[2];
134 set_ctrl(client
, *ctrl
);
136 return i2c_master_send(client
, (char *)buf
, 2);
139 static int pcf8583_read_mem(struct i2c_client
*client
, struct rtc_mem
*mem
)
141 unsigned char addr
[1];
142 struct i2c_msg msgs
[2] = {
144 .addr
= client
->addr
,
149 .addr
= client
->addr
,
161 return i2c_transfer(client
->adapter
, msgs
, 2) == 2 ? 0 : -EIO
;
164 static int pcf8583_write_mem(struct i2c_client
*client
, struct rtc_mem
*mem
)
166 unsigned char addr
[1];
167 struct i2c_msg msgs
[2] = {
169 .addr
= client
->addr
,
174 .addr
= client
->addr
,
175 .flags
= I2C_M_NOSTART
,
186 return i2c_transfer(client
->adapter
, msgs
, 2) == 2 ? 0 : -EIO
;
189 static int pcf8583_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
191 struct i2c_client
*client
= to_i2c_client(dev
);
192 unsigned char ctrl
, year
[2];
193 struct rtc_mem mem
= { CMOS_YEAR
, sizeof(year
), year
};
194 int real_year
, year_offset
, err
;
197 * Ensure that the RTC is running.
199 pcf8583_get_ctrl(client
, &ctrl
);
200 if (ctrl
& (CTRL_STOP
| CTRL_HOLD
)) {
201 unsigned char new_ctrl
= ctrl
& ~(CTRL_STOP
| CTRL_HOLD
);
203 printk(KERN_WARNING
"RTC: resetting control %02x -> %02x\n",
206 if ((err
= pcf8583_set_ctrl(client
, &new_ctrl
)) < 0)
210 if (pcf8583_get_datetime(client
, tm
) ||
211 pcf8583_read_mem(client
, &mem
))
217 * The RTC year holds the LSB two bits of the current
218 * year, which should reflect the LSB two bits of the
219 * CMOS copy of the year. Any difference indicates
220 * that we have to correct the CMOS version.
222 year_offset
= tm
->tm_year
- (real_year
& 3);
225 * RTC year wrapped. Adjust it appropriately.
229 tm
->tm_year
= real_year
+ year_offset
+ year
[1] * 100;
234 static int pcf8583_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
236 struct i2c_client
*client
= to_i2c_client(dev
);
237 unsigned char year
[2], chk
;
238 struct rtc_mem cmos_year
= { CMOS_YEAR
, sizeof(year
), year
};
239 struct rtc_mem cmos_check
= { CMOS_CHECKSUM
, 1, &chk
};
243 * The RTC's own 2-bit year must reflect the least
244 * significant two bits of the CMOS year.
247 ret
= pcf8583_set_datetime(client
, tm
, 1);
251 ret
= pcf8583_read_mem(client
, &cmos_check
);
255 ret
= pcf8583_read_mem(client
, &cmos_year
);
259 chk
-= year
[1] + year
[0];
261 year
[1] = tm
->tm_year
/ 100;
262 year
[0] = tm
->tm_year
% 100;
264 chk
+= year
[1] + year
[0];
266 ret
= pcf8583_write_mem(client
, &cmos_year
);
271 ret
= pcf8583_write_mem(client
, &cmos_check
);
276 static struct rtc_class_ops pcf8583_rtc_ops
= {
277 .read_time
= pcf8583_rtc_read_time
,
278 .set_time
= pcf8583_rtc_set_time
,
281 static int pcf8583_probe(struct i2c_adapter
*adap
, int addr
, int kind
);
283 static int pcf8583_attach(struct i2c_adapter
*adap
)
285 return i2c_probe(adap
, &addr_data
, pcf8583_probe
);
288 static int pcf8583_detach(struct i2c_client
*client
)
291 struct pcf8583
*pcf
= i2c_get_clientdata(client
);
292 struct rtc_device
*rtc
= pcf
->rtc
;
295 rtc_device_unregister(rtc
);
297 if ((err
= i2c_detach_client(client
)))
304 static struct i2c_driver pcf8583_driver
= {
308 .id
= I2C_DRIVERID_PCF8583
,
309 .attach_adapter
= pcf8583_attach
,
310 .detach_client
= pcf8583_detach
,
313 static int pcf8583_probe(struct i2c_adapter
*adap
, int addr
, int kind
)
316 struct i2c_client
*client
;
317 struct rtc_device
*rtc
;
318 unsigned char buf
[1], ad
[1] = { 0 };
320 struct i2c_msg msgs
[2] = {
334 pcf
= kzalloc(sizeof(*pcf
), GFP_KERNEL
);
338 client
= &pcf
->client
;
341 client
->adapter
= adap
;
342 client
->driver
= &pcf8583_driver
;
344 strlcpy(client
->name
, pcf8583_driver
.driver
.name
, I2C_NAME_SIZE
);
346 if (i2c_transfer(client
->adapter
, msgs
, 2) != 2) {
351 err
= i2c_attach_client(client
);
356 rtc
= rtc_device_register(pcf8583_driver
.driver
.name
, &client
->dev
,
357 &pcf8583_rtc_ops
, THIS_MODULE
);
365 i2c_set_clientdata(client
, pcf
);
366 set_ctrl(client
, buf
[0]);
371 i2c_detach_client(client
);
379 static __init
int pcf8583_init(void)
381 return i2c_add_driver(&pcf8583_driver
);
384 static __exit
void pcf8583_exit(void)
386 i2c_del_driver(&pcf8583_driver
);
389 module_init(pcf8583_init
);
390 module_exit(pcf8583_exit
);
392 MODULE_AUTHOR("Russell King");
393 MODULE_DESCRIPTION("PCF8583 I2C RTC driver");
394 MODULE_LICENSE("GPL");