2 * linux/drivers/acorn/char/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 #include <linux/i2c.h>
13 #include <linux/slab.h>
14 #include <linux/string.h>
15 #include <linux/mc146818rtc.h>
16 #include <linux/init.h>
17 #include <linux/errno.h>
18 #include <linux/bcd.h>
22 static struct i2c_driver pcf8583_driver
;
24 static unsigned short ignore
[] = { I2C_CLIENT_END
};
25 static unsigned short normal_addr
[] = { 0x50, I2C_CLIENT_END
};
27 static struct i2c_client_address_data addr_data
= {
28 .normal_i2c
= normal_addr
,
34 #define DAT(x) ((unsigned int)(x->dev.driver_data))
37 pcf8583_attach(struct i2c_adapter
*adap
, int addr
, int kind
)
40 unsigned char buf
[1], ad
[1] = { 0 };
41 struct i2c_msg msgs
[2] = {
43 { addr
, I2C_M_RD
, 1, buf
}
46 c
= kmalloc(sizeof(*c
), GFP_KERNEL
);
50 memset(c
, 0, sizeof(*c
));
53 c
->driver
= &pcf8583_driver
;
55 if (i2c_transfer(c
->adapter
, msgs
, 2) == 2)
58 return i2c_attach_client(c
);
62 pcf8583_probe(struct i2c_adapter
*adap
)
64 return i2c_probe(adap
, &addr_data
, pcf8583_attach
);
68 pcf8583_detach(struct i2c_client
*client
)
70 i2c_detach_client(client
);
76 pcf8583_get_datetime(struct i2c_client
*client
, struct rtc_tm
*dt
)
78 unsigned char buf
[8], addr
[1] = { 1 };
79 struct i2c_msg msgs
[2] = {
80 { client
->addr
, 0, 1, addr
},
81 { client
->addr
, I2C_M_RD
, 6, buf
}
85 memset(buf
, 0, sizeof(buf
));
87 ret
= i2c_transfer(client
->adapter
, msgs
, 2);
89 dt
->year_off
= buf
[4] >> 6;
90 dt
->wday
= buf
[5] >> 5;
95 dt
->cs
= BCD_TO_BIN(buf
[0]);
96 dt
->secs
= BCD_TO_BIN(buf
[1]);
97 dt
->mins
= BCD_TO_BIN(buf
[2]);
98 dt
->hours
= BCD_TO_BIN(buf
[3]);
99 dt
->mday
= BCD_TO_BIN(buf
[4]);
100 dt
->mon
= BCD_TO_BIN(buf
[5]);
109 pcf8583_set_datetime(struct i2c_client
*client
, struct rtc_tm
*dt
, int datetoo
)
111 unsigned char buf
[8];
115 buf
[1] = DAT(client
) | 0x80;
116 buf
[2] = BIN_TO_BCD(dt
->cs
);
117 buf
[3] = BIN_TO_BCD(dt
->secs
);
118 buf
[4] = BIN_TO_BCD(dt
->mins
);
119 buf
[5] = BIN_TO_BCD(dt
->hours
);
123 buf
[6] = BIN_TO_BCD(dt
->mday
) | (dt
->year_off
<< 6);
124 buf
[7] = BIN_TO_BCD(dt
->mon
) | (dt
->wday
<< 5);
127 ret
= i2c_master_send(client
, (char *)buf
, len
);
131 buf
[1] = DAT(client
);
132 i2c_master_send(client
, (char *)buf
, 2);
138 pcf8583_get_ctrl(struct i2c_client
*client
, unsigned char *ctrl
)
145 pcf8583_set_ctrl(struct i2c_client
*client
, unsigned char *ctrl
)
147 unsigned char buf
[2];
153 return i2c_master_send(client
, (char *)buf
, 2);
157 pcf8583_read_mem(struct i2c_client
*client
, struct mem
*mem
)
159 unsigned char addr
[1];
160 struct i2c_msg msgs
[2] = {
161 { client
->addr
, 0, 1, addr
},
162 { client
->addr
, I2C_M_RD
, 0, mem
->data
}
169 msgs
[1].len
= mem
->nr
;
171 return i2c_transfer(client
->adapter
, msgs
, 2) == 2 ? 0 : -EIO
;
175 pcf8583_write_mem(struct i2c_client
*client
, struct mem
*mem
)
177 unsigned char addr
[1];
178 struct i2c_msg msgs
[2] = {
179 { client
->addr
, 0, 1, addr
},
180 { client
->addr
, 0, 0, mem
->data
}
187 msgs
[1].len
= mem
->nr
;
189 return i2c_transfer(client
->adapter
, msgs
, 2) == 2 ? 0 : -EIO
;
193 pcf8583_command(struct i2c_client
*client
, unsigned int cmd
, void *arg
)
196 case RTC_GETDATETIME
:
197 return pcf8583_get_datetime(client
, arg
);
200 return pcf8583_set_datetime(client
, arg
, 0);
202 case RTC_SETDATETIME
:
203 return pcf8583_set_datetime(client
, arg
, 1);
206 return pcf8583_get_ctrl(client
, arg
);
209 return pcf8583_set_ctrl(client
, arg
);
212 return pcf8583_read_mem(client
, arg
);
215 return pcf8583_write_mem(client
, arg
);
222 static struct i2c_driver pcf8583_driver
= {
224 .id
= I2C_DRIVERID_PCF8583
,
225 .flags
= I2C_DF_NOTIFY
,
226 .attach_adapter
= pcf8583_probe
,
227 .detach_client
= pcf8583_detach
,
228 .command
= pcf8583_command
231 static __init
int pcf8583_init(void)
233 return i2c_add_driver(&pcf8583_driver
);
236 __initcall(pcf8583_init
);