15 #include <linux/module.h>
16 #include <linux/i2c.h>
17 #include <linux/slab.h>
18 #include <linux/rtc.h>
20 #include <linux/errno.h>
34 #define CTRL_STOP 0x80
35 #define CTRL_HOLD 0x40
36 #define CTRL_32KHZ 0x00
37 #define CTRL_MASK 0x08
38 #define CTRL_ALARMEN 0x04
39 #define CTRL_ALARM 0x02
40 #define CTRL_TIMER 0x01
45 #define get_ctrl(x) ((struct pcf8583 *)i2c_get_clientdata(x))->ctrl
46 #define set_ctrl(x, v) get_ctrl(x) = v
48 #define CMOS_YEAR (64 + 128)
49 #define CMOS_CHECKSUM (63)
53 unsigned char buf[8],
addr[1] = { 1 };
69 memset(buf, 0,
sizeof(buf));
86 return ret == 2 ? 0 : -
EIO;
114 return ret == 2 ? 0 : -
EIO;
117 static int pcf8583_get_ctrl(
struct i2c_client *client,
unsigned char *
ctrl)
123 static int pcf8583_set_ctrl(
struct i2c_client *client,
unsigned char *
ctrl)
125 unsigned char buf[2];
136 unsigned char addr[1];
161 unsigned char buf[9];
164 if (mem->
loc < 8 || mem->
nr > 8)
171 return ret == mem->
nr + 1 ? 0 : -
EIO;
179 int real_year, year_offset,
err;
184 pcf8583_get_ctrl(client, &ctrl);
191 if ((err = pcf8583_set_ctrl(client, &new_ctrl)) < 0)
195 if (pcf8583_get_datetime(client, tm) ||
196 pcf8583_read_mem(client, &mem))
207 year_offset = tm->
tm_year - (real_year & 3);
214 tm->
tm_year = (real_year + year_offset + year[1] * 100) - 1900;
219 static int pcf8583_rtc_set_time(
struct device *dev,
struct rtc_time *tm)
222 unsigned char year[2], chk;
225 unsigned int proper_year = tm->
tm_year + 1900;
233 ret = pcf8583_set_datetime(client, tm, 1);
237 ret = pcf8583_read_mem(client, &cmos_check);
241 ret = pcf8583_read_mem(client, &cmos_year);
245 chk -= year[1] + year[0];
247 year[1] = proper_year / 100;
248 year[0] = proper_year % 100;
250 chk += year[1] + year[0];
252 ret = pcf8583_write_mem(client, &cmos_year);
257 ret = pcf8583_write_mem(client, &cmos_check);
263 .read_time = pcf8583_rtc_read_time,
264 .set_time = pcf8583_rtc_set_time,
267 static int pcf8583_probe(
struct i2c_client *client,
276 pcf8583 = kzalloc(
sizeof(
struct pcf8583),
GFP_KERNEL);
280 i2c_set_clientdata(client, pcf8583);
285 if (IS_ERR(pcf8583->
rtc)) {
286 err = PTR_ERR(pcf8583->
rtc);
299 struct pcf8583 *pcf8583 = i2c_get_clientdata(client);
318 .probe = pcf8583_probe,
320 .id_table = pcf8583_id,