2 * x1205.c - An i2c driver for the Xicor X1205 RTC
3 * Copyright 2004 Karen Spearel
4 * Copyright 2005 Alessandro Zummo
6 * please send all reports to:
7 * kas11 at tampabay dot rr dot com
8 * a dot zummo at towertech dot it
10 * based on the other drivers in this same directory.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/slab.h>
21 #include <linux/i2c.h>
22 #include <linux/string.h>
23 #include <linux/bcd.h>
24 #include <linux/rtc.h>
25 #include <linux/list.h>
27 #include <linux/x1205.h>
29 #define DRV_VERSION "0.9.9"
31 /* Addresses to scan: none. This chip is located at
32 * 0x6f and uses a two bytes register addressing.
33 * Two bytes need to be written to read a single register,
34 * while most other chips just require one and take the second
35 * one as the data to be written. To prevent corrupting
36 * unknown chips, the user must explicitely set the probe parameter.
39 static unsigned short normal_i2c
[] = { I2C_CLIENT_END
};
41 /* Insmod parameters */
43 I2C_CLIENT_MODULE_PARM(hctosys
,
44 "Set the system time from the hardware clock upon initialization");
46 /* offsets into CCR area */
57 #define X1205_REG_SR 0x3F /* status register */
58 #define X1205_REG_Y2K 0x37
59 #define X1205_REG_DW 0x36
60 #define X1205_REG_YR 0x35
61 #define X1205_REG_MO 0x34
62 #define X1205_REG_DT 0x33
63 #define X1205_REG_HR 0x32
64 #define X1205_REG_MN 0x31
65 #define X1205_REG_SC 0x30
66 #define X1205_REG_DTR 0x13
67 #define X1205_REG_ATR 0x12
68 #define X1205_REG_INT 0x11
69 #define X1205_REG_0 0x10
70 #define X1205_REG_Y2K1 0x0F
71 #define X1205_REG_DWA1 0x0E
72 #define X1205_REG_YRA1 0x0D
73 #define X1205_REG_MOA1 0x0C
74 #define X1205_REG_DTA1 0x0B
75 #define X1205_REG_HRA1 0x0A
76 #define X1205_REG_MNA1 0x09
77 #define X1205_REG_SCA1 0x08
78 #define X1205_REG_Y2K0 0x07
79 #define X1205_REG_DWA0 0x06
80 #define X1205_REG_YRA0 0x05
81 #define X1205_REG_MOA0 0x04
82 #define X1205_REG_DTA0 0x03
83 #define X1205_REG_HRA0 0x02
84 #define X1205_REG_MNA0 0x01
85 #define X1205_REG_SCA0 0x00
87 #define X1205_CCR_BASE 0x30 /* Base address of CCR */
88 #define X1205_ALM0_BASE 0x00 /* Base address of ALARM0 */
90 #define X1205_SR_RTCF 0x01 /* Clock failure */
91 #define X1205_SR_WEL 0x02 /* Write Enable Latch */
92 #define X1205_SR_RWEL 0x04 /* Register Write Enable */
94 #define X1205_DTR_DTR0 0x01
95 #define X1205_DTR_DTR1 0x02
96 #define X1205_DTR_DTR2 0x04
98 #define X1205_HR_MIL 0x80 /* Set in ccr.hour for 24 hr mode */
101 static int x1205_attach(struct i2c_adapter
*adapter
);
102 static int x1205_detach(struct i2c_client
*client
);
103 static int x1205_probe(struct i2c_adapter
*adapter
, int address
, int kind
);
104 static int x1205_command(struct i2c_client
*client
, unsigned int cmd
,
107 static struct i2c_driver x1205_driver
= {
111 .attach_adapter
= &x1205_attach
,
112 .detach_client
= &x1205_detach
,
116 struct i2c_client client
;
117 struct list_head list
;
121 static const unsigned char days_in_mo
[] =
122 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
124 static LIST_HEAD(x1205_clients
);
126 /* Workaround until the I2C subsytem will allow to send
127 * commands to a specific client. This function will send the command
128 * to the first client.
130 int x1205_do_command(unsigned int cmd
, void *arg
)
132 struct list_head
*walk
;
133 struct list_head
*tmp
;
134 struct x1205_data
*data
;
136 list_for_each_safe(walk
, tmp
, &x1205_clients
) {
137 data
= list_entry(walk
, struct x1205_data
, list
);
138 return x1205_command(&data
->client
, cmd
, arg
);
144 #define is_leap(year) \
145 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
147 /* make sure the rtc_time values are in bounds */
148 static int x1205_validate_tm(struct rtc_time
*tm
)
150 int year
= tm
->tm_year
+ 1900;
152 if ((tm
->tm_year
< 70) || (tm
->tm_year
> 255))
155 if ((tm
->tm_mon
> 11) || (tm
->tm_mday
== 0))
158 if (tm
->tm_mday
> days_in_mo
[tm
->tm_mon
]
159 + ((tm
->tm_mon
== 1) && is_leap(year
)))
162 if ((tm
->tm_hour
>= 24) || (tm
->tm_min
>= 60) || (tm
->tm_sec
>= 60))
169 * In the routines that deal directly with the x1205 hardware, we use
170 * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch
171 * Epoch is initialized as 2000. Time is set to UTC.
173 static int x1205_get_datetime(struct i2c_client
*client
, struct rtc_time
*tm
,
176 unsigned char dt_addr
[2] = { 0, reg_base
};
177 static unsigned char sr_addr
[2] = { 0, X1205_REG_SR
};
179 unsigned char buf
[8], sr
;
181 struct i2c_msg msgs
[] = {
182 { client
->addr
, 0, 2, sr_addr
}, /* setup read ptr */
183 { client
->addr
, I2C_M_RD
, 1, &sr
}, /* read status */
184 { client
->addr
, 0, 2, dt_addr
}, /* setup read ptr */
185 { client
->addr
, I2C_M_RD
, 8, buf
}, /* read date */
188 struct x1205_data
*data
= i2c_get_clientdata(client
);
190 /* read status register */
191 if ((i2c_transfer(client
->adapter
, &msgs
[0], 2)) != 2) {
192 dev_err(&client
->dev
, "%s: read error\n", __FUNCTION__
);
196 /* check for battery failure */
197 if (sr
& X1205_SR_RTCF
) {
198 dev_warn(&client
->dev
,
199 "Clock had a power failure, you must set the date.\n");
203 /* read date registers */
204 if ((i2c_transfer(client
->adapter
, &msgs
[2], 2)) != 2) {
205 dev_err(&client
->dev
, "%s: read error\n", __FUNCTION__
);
209 dev_dbg(&client
->dev
,
210 "%s: raw read data - sec=%02x, min=%02x, hr=%02x, "
211 "mday=%02x, mon=%02x, year=%02x, wday=%02x, y2k=%02x\n",
213 buf
[0], buf
[1], buf
[2], buf
[3],
214 buf
[4], buf
[5], buf
[6], buf
[7]);
216 tm
->tm_sec
= BCD2BIN(buf
[CCR_SEC
]);
217 tm
->tm_min
= BCD2BIN(buf
[CCR_MIN
]);
218 tm
->tm_hour
= BCD2BIN(buf
[CCR_HOUR
] & 0x3F); /* hr is 0-23 */
219 tm
->tm_mday
= BCD2BIN(buf
[CCR_MDAY
]);
220 tm
->tm_mon
= BCD2BIN(buf
[CCR_MONTH
]);
221 data
->epoch
= BCD2BIN(buf
[CCR_Y2K
]) * 100;
222 tm
->tm_year
= BCD2BIN(buf
[CCR_YEAR
]) + data
->epoch
- 1900;
223 tm
->tm_wday
= buf
[CCR_WDAY
];
225 dev_dbg(&client
->dev
, "%s: tm is secs=%d, mins=%d, hours=%d, "
226 "mday=%d, mon=%d, year=%d, wday=%d\n",
228 tm
->tm_sec
, tm
->tm_min
, tm
->tm_hour
,
229 tm
->tm_mday
, tm
->tm_mon
, tm
->tm_year
, tm
->tm_wday
);
234 static int x1205_set_datetime(struct i2c_client
*client
, struct rtc_time
*tm
,
235 int datetoo
, u8 reg_base
)
239 unsigned char buf
[8];
241 static const unsigned char wel
[3] = { 0, X1205_REG_SR
,
244 static const unsigned char rwel
[3] = { 0, X1205_REG_SR
,
245 X1205_SR_WEL
| X1205_SR_RWEL
};
247 static const unsigned char diswe
[3] = { 0, X1205_REG_SR
, 0 };
249 struct x1205_data
*data
= i2c_get_clientdata(client
);
251 /* check if all values in the tm struct are correct */
252 if ((err
= x1205_validate_tm(tm
)) < 0)
255 dev_dbg(&client
->dev
, "%s: secs=%d, mins=%d, hours=%d, "
256 "mday=%d, mon=%d, year=%d, wday=%d\n",
258 tm
->tm_sec
, tm
->tm_min
, tm
->tm_hour
,
259 tm
->tm_mday
, tm
->tm_mon
, tm
->tm_year
, tm
->tm_wday
);
261 buf
[CCR_SEC
] = BIN2BCD(tm
->tm_sec
);
262 buf
[CCR_MIN
] = BIN2BCD(tm
->tm_min
);
264 /* set hour and 24hr bit */
265 buf
[CCR_HOUR
] = BIN2BCD(tm
->tm_hour
) | X1205_HR_MIL
;
267 /* should we also set the date? */
269 buf
[CCR_MDAY
] = BIN2BCD(tm
->tm_mday
);
272 buf
[CCR_MONTH
] = BIN2BCD(tm
->tm_mon
);
274 /* year, since 1900 */
275 buf
[CCR_YEAR
] = BIN2BCD(tm
->tm_year
+ 1900 - data
->epoch
);
276 buf
[CCR_WDAY
] = tm
->tm_wday
& 0x07;
277 buf
[CCR_Y2K
] = BIN2BCD(data
->epoch
/ 100);
280 /* this sequence is required to unlock the chip */
281 xfer
= i2c_master_send(client
, wel
, 3);
283 dev_err(&client
->dev
, "%s: wel - %d\n", __FUNCTION__
, xfer
);
287 xfer
= i2c_master_send(client
, rwel
, 3);
289 dev_err(&client
->dev
, "%s: rwel - %d\n", __FUNCTION__
, xfer
);
293 /* write register's data */
294 for (i
= 0; i
< (datetoo
? 8 : 3); i
++) {
295 unsigned char rdata
[3] = { 0, reg_base
+ i
, buf
[i
] };
297 xfer
= i2c_master_send(client
, rdata
, 3);
299 dev_err(&client
->dev
,
300 "%s: xfer=%d addr=%02x, data=%02x\n",
302 xfer
, rdata
[1], rdata
[2]);
307 /* disable further writes */
308 xfer
= i2c_master_send(client
, diswe
, 3);
310 dev_err(&client
->dev
, "%s: diswe - %d\n", __FUNCTION__
, xfer
);
317 static int x1205_get_dtrim(struct i2c_client
*client
, int *trim
)
320 static unsigned char dtr_addr
[2] = { 0, X1205_REG_DTR
};
322 struct i2c_msg msgs
[] = {
323 { client
->addr
, 0, 2, dtr_addr
}, /* setup read ptr */
324 { client
->addr
, I2C_M_RD
, 1, &dtr
}, /* read dtr */
327 /* read dtr register */
328 if ((i2c_transfer(client
->adapter
, &msgs
[0], 2)) != 2) {
329 dev_err(&client
->dev
, "%s: read error\n", __FUNCTION__
);
333 dev_dbg(&client
->dev
, "%s: raw dtr=%x\n", __FUNCTION__
, dtr
);
337 if (dtr
& X1205_DTR_DTR0
)
340 if (dtr
& X1205_DTR_DTR1
)
343 if (dtr
& X1205_DTR_DTR2
)
349 static int x1205_get_atrim(struct i2c_client
*client
, int *trim
)
352 static unsigned char atr_addr
[2] = { 0, X1205_REG_ATR
};
354 struct i2c_msg msgs
[] = {
355 { client
->addr
, 0, 2, atr_addr
}, /* setup read ptr */
356 { client
->addr
, I2C_M_RD
, 1, &atr
}, /* read atr */
359 /* read atr register */
360 if ((i2c_transfer(client
->adapter
, &msgs
[0], 2)) != 2) {
361 dev_err(&client
->dev
, "%s: read error\n", __FUNCTION__
);
365 dev_dbg(&client
->dev
, "%s: raw atr=%x\n", __FUNCTION__
, atr
);
367 /* atr is a two's complement value on 6 bits,
368 * perform sign extension. The formula is
369 * Catr = (atr * 0.25pF) + 11.00pF.
374 dev_dbg(&client
->dev
, "%s: raw atr=%x (%d)\n", __FUNCTION__
, atr
, atr
);
376 *trim
= (atr
* 250) + 11000;
378 dev_dbg(&client
->dev
, "%s: real=%d\n", __FUNCTION__
, *trim
);
383 static int x1205_hctosys(struct i2c_client
*client
)
390 err
= x1205_command(client
, X1205_CMD_GETDATETIME
, &tm
);
393 dev_err(&client
->dev
,
394 "Unable to set the system clock\n");
398 /* IMPORTANT: the RTC only stores whole seconds. It is arbitrary
399 * whether it stores the most close value or the value with partial
400 * seconds truncated. However, it is important that we use it to store
401 * the truncated value. This is because otherwise it is necessary,
402 * in an rtc sync function, to read both xtime.tv_sec and
403 * xtime.tv_nsec. On some processors (i.e. ARM), an atomic read
404 * of >32bits is not possible. So storing the most close value would
405 * slow down the sync API. So here we have the truncated value and
406 * the best guess is to add 0.5s.
409 tv
.tv_nsec
= NSEC_PER_SEC
>> 1;
411 /* WARNING: this is not the C library 'mktime' call, it is a built in
412 * inline function from include/linux/time.h. It expects (requires)
413 * the month to be in the range 1-12
416 tv
.tv_sec
= mktime(tm
.tm_year
+ 1900, tm
.tm_mon
+ 1,
417 tm
.tm_mday
, tm
.tm_hour
,
418 tm
.tm_min
, tm
.tm_sec
);
420 do_settimeofday(&tv
);
422 dev_info(&client
->dev
,
423 "setting the system clock to %d-%d-%d %d:%d:%d\n",
424 tm
.tm_year
+ 1900, tm
.tm_mon
+ 1,
425 tm
.tm_mday
, tm
.tm_hour
, tm
.tm_min
,
439 static int x1205_validate_client(struct i2c_client
*client
)
443 /* Probe array. We will read the register at the specified
444 * address and check if the given bits are zero.
446 static const unsigned char probe_zero_pattern
[] = {
455 static const struct x1205_limit probe_limits_pattern
[] = {
456 /* register, mask, min, max */
457 { X1205_REG_Y2K
, 0xFF, 19, 20 },
458 { X1205_REG_DW
, 0xFF, 0, 6 },
459 { X1205_REG_YR
, 0xFF, 0, 99 },
460 { X1205_REG_MO
, 0xFF, 0, 12 },
461 { X1205_REG_DT
, 0xFF, 0, 31 },
462 { X1205_REG_HR
, 0x7F, 0, 23 },
463 { X1205_REG_MN
, 0xFF, 0, 59 },
464 { X1205_REG_SC
, 0xFF, 0, 59 },
465 { X1205_REG_Y2K1
, 0xFF, 19, 20 },
466 { X1205_REG_Y2K0
, 0xFF, 19, 20 },
469 /* check that registers have bits a 0 where expected */
470 for (i
= 0; i
< ARRAY_SIZE(probe_zero_pattern
); i
+= 2) {
473 unsigned char addr
[2] = { 0, probe_zero_pattern
[i
] };
475 struct i2c_msg msgs
[2] = {
476 { client
->addr
, 0, 2, addr
},
477 { client
->addr
, I2C_M_RD
, 1, &buf
},
480 xfer
= i2c_transfer(client
->adapter
, msgs
, 2);
482 dev_err(&client
->adapter
->dev
,
483 "%s: could not read register %x\n",
484 __FUNCTION__
, addr
[1]);
489 if ((buf
& probe_zero_pattern
[i
+1]) != 0) {
490 dev_err(&client
->adapter
->dev
,
491 "%s: register=%02x, zero pattern=%d, value=%x\n",
492 __FUNCTION__
, addr
[1], i
, buf
);
498 /* check limits (only registers with bcd values) */
499 for (i
= 0; i
< ARRAY_SIZE(probe_limits_pattern
); i
++) {
500 unsigned char reg
, value
;
502 unsigned char addr
[2] = { 0, probe_limits_pattern
[i
].reg
};
504 struct i2c_msg msgs
[2] = {
505 { client
->addr
, 0, 2, addr
},
506 { client
->addr
, I2C_M_RD
, 1, ®
},
509 xfer
= i2c_transfer(client
->adapter
, msgs
, 2);
512 dev_err(&client
->adapter
->dev
,
513 "%s: could not read register %x\n",
514 __FUNCTION__
, addr
[1]);
519 value
= BCD2BIN(reg
& probe_limits_pattern
[i
].mask
);
521 if (value
> probe_limits_pattern
[i
].max
||
522 value
< probe_limits_pattern
[i
].min
) {
523 dev_dbg(&client
->adapter
->dev
,
524 "%s: register=%x, lim pattern=%d, value=%d\n",
525 __FUNCTION__
, addr
[1], i
, value
);
534 static int x1205_attach(struct i2c_adapter
*adapter
)
536 dev_dbg(&adapter
->dev
, "%s\n", __FUNCTION__
);
538 return i2c_probe(adapter
, &addr_data
, x1205_probe
);
541 int x1205_direct_attach(int adapter_id
,
542 struct i2c_client_address_data
*address_data
)
545 struct i2c_adapter
*adapter
= i2c_get_adapter(adapter_id
);
548 err
= i2c_probe(adapter
,
549 address_data
, x1205_probe
);
551 i2c_put_adapter(adapter
);
559 static int x1205_probe(struct i2c_adapter
*adapter
, int address
, int kind
)
561 struct i2c_client
*client
;
562 struct x1205_data
*data
;
566 dev_dbg(&adapter
->dev
, "%s\n", __FUNCTION__
);
568 if (!i2c_check_functionality(adapter
, I2C_FUNC_I2C
)) {
573 if (!(data
= kzalloc(sizeof(struct x1205_data
), GFP_KERNEL
))) {
578 /* Initialize our structures */
581 client
= &data
->client
;
582 client
->addr
= address
;
583 client
->driver
= &x1205_driver
;
584 client
->adapter
= adapter
;
586 strlcpy(client
->name
, "x1205", I2C_NAME_SIZE
);
588 i2c_set_clientdata(client
, data
);
590 /* Verify the chip is really an X1205 */
592 if (x1205_validate_client(client
) < 0) {
598 /* Inform the i2c layer */
599 if ((err
= i2c_attach_client(client
)))
602 list_add(&data
->list
, &x1205_clients
);
604 dev_info(&client
->dev
, "chip found, driver version " DRV_VERSION
"\n");
606 /* If requested, set the system time */
608 x1205_hctosys(client
);
619 static int x1205_detach(struct i2c_client
*client
)
622 struct x1205_data
*data
= i2c_get_clientdata(client
);
624 dev_dbg(&client
->dev
, "%s\n", __FUNCTION__
);
626 if ((err
= i2c_detach_client(client
)))
629 list_del(&data
->list
);
636 static int x1205_command(struct i2c_client
*client
, unsigned int cmd
,
642 if (!capable(CAP_SYS_TIME
))
645 dev_dbg(&client
->dev
, "%s: cmd=%d\n", __FUNCTION__
, cmd
);
648 case X1205_CMD_GETDATETIME
:
649 return x1205_get_datetime(client
, param
, X1205_CCR_BASE
);
651 case X1205_CMD_SETTIME
:
652 return x1205_set_datetime(client
, param
, 0,
655 case X1205_CMD_SETDATETIME
:
656 return x1205_set_datetime(client
, param
, 1,
659 case X1205_CMD_GETALARM
:
660 return x1205_get_datetime(client
, param
, X1205_ALM0_BASE
);
662 case X1205_CMD_SETALARM
:
663 return x1205_set_datetime(client
, param
, 1,
666 case X1205_CMD_GETDTRIM
:
667 return x1205_get_dtrim(client
, param
);
669 case X1205_CMD_GETATRIM
:
670 return x1205_get_atrim(client
, param
);
677 static int __init
x1205_init(void)
679 return i2c_add_driver(&x1205_driver
);
682 static void __exit
x1205_exit(void)
684 i2c_del_driver(&x1205_driver
);
688 "Karen Spearel <kas11@tampabay.rr.com>, "
689 "Alessandro Zummo <a.zummo@towertech.it>");
690 MODULE_DESCRIPTION("Xicor X1205 RTC driver");
691 MODULE_LICENSE("GPL");
692 MODULE_VERSION(DRV_VERSION
);
694 EXPORT_SYMBOL_GPL(x1205_do_command
);
695 EXPORT_SYMBOL_GPL(x1205_direct_attach
);
697 module_init(x1205_init
);
698 module_exit(x1205_exit
);