2 * An i2c driver for the Xicor/Intersil X1205 RTC
3 * Copyright 2004 Karen Spearel
4 * Copyright 2005 Alessandro Zummo
6 * please send all reports to:
7 * Karen Spearel <kas111 at gmail dot com>
8 * Alessandro Zummo <a.zummo@towertech.it>
10 * based on a lot of other RTC drivers.
12 * Information and datasheet:
13 * http://www.intersil.com/cda/deviceinfo/0,1477,X1205,00.html
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License version 2 as
17 * published by the Free Software Foundation.
20 #include <linux/i2c.h>
21 #include <linux/bcd.h>
22 #include <linux/rtc.h>
23 #include <linux/delay.h>
24 #include <linux/module.h>
25 #include <linux/bitops.h>
27 /* offsets into CCR area */
38 #define X1205_REG_SR 0x3F /* status register */
39 #define X1205_REG_Y2K 0x37
40 #define X1205_REG_DW 0x36
41 #define X1205_REG_YR 0x35
42 #define X1205_REG_MO 0x34
43 #define X1205_REG_DT 0x33
44 #define X1205_REG_HR 0x32
45 #define X1205_REG_MN 0x31
46 #define X1205_REG_SC 0x30
47 #define X1205_REG_DTR 0x13
48 #define X1205_REG_ATR 0x12
49 #define X1205_REG_INT 0x11
50 #define X1205_REG_0 0x10
51 #define X1205_REG_Y2K1 0x0F
52 #define X1205_REG_DWA1 0x0E
53 #define X1205_REG_YRA1 0x0D
54 #define X1205_REG_MOA1 0x0C
55 #define X1205_REG_DTA1 0x0B
56 #define X1205_REG_HRA1 0x0A
57 #define X1205_REG_MNA1 0x09
58 #define X1205_REG_SCA1 0x08
59 #define X1205_REG_Y2K0 0x07
60 #define X1205_REG_DWA0 0x06
61 #define X1205_REG_YRA0 0x05
62 #define X1205_REG_MOA0 0x04
63 #define X1205_REG_DTA0 0x03
64 #define X1205_REG_HRA0 0x02
65 #define X1205_REG_MNA0 0x01
66 #define X1205_REG_SCA0 0x00
68 #define X1205_CCR_BASE 0x30 /* Base address of CCR */
69 #define X1205_ALM0_BASE 0x00 /* Base address of ALARM0 */
71 #define X1205_SR_RTCF 0x01 /* Clock failure */
72 #define X1205_SR_WEL 0x02 /* Write Enable Latch */
73 #define X1205_SR_RWEL 0x04 /* Register Write Enable */
74 #define X1205_SR_AL0 0x20 /* Alarm 0 match */
76 #define X1205_DTR_DTR0 0x01
77 #define X1205_DTR_DTR1 0x02
78 #define X1205_DTR_DTR2 0x04
80 #define X1205_HR_MIL 0x80 /* Set in ccr.hour for 24 hr mode */
82 #define X1205_INT_AL0E 0x20 /* Alarm 0 enable */
84 static struct i2c_driver x1205_driver
;
87 * In the routines that deal directly with the x1205 hardware, we use
88 * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch
89 * Epoch is initialized as 2000. Time is set to UTC.
91 static int x1205_get_datetime(struct i2c_client
*client
, struct rtc_time
*tm
,
92 unsigned char reg_base
)
94 unsigned char dt_addr
[2] = { 0, reg_base
};
98 struct i2c_msg msgs
[] = {
100 .addr
= client
->addr
,
105 .addr
= client
->addr
,
112 /* read date registers */
113 if (i2c_transfer(client
->adapter
, &msgs
[0], 2) != 2) {
114 dev_err(&client
->dev
, "%s: read error\n", __func__
);
118 dev_dbg(&client
->dev
,
119 "%s: raw read data - sec=%02x, min=%02x, hr=%02x, "
120 "mday=%02x, mon=%02x, year=%02x, wday=%02x, y2k=%02x\n",
122 buf
[0], buf
[1], buf
[2], buf
[3],
123 buf
[4], buf
[5], buf
[6], buf
[7]);
125 /* Mask out the enable bits if these are alarm registers */
126 if (reg_base
< X1205_CCR_BASE
)
127 for (i
= 0; i
<= 4; i
++)
130 tm
->tm_sec
= bcd2bin(buf
[CCR_SEC
]);
131 tm
->tm_min
= bcd2bin(buf
[CCR_MIN
]);
132 tm
->tm_hour
= bcd2bin(buf
[CCR_HOUR
] & 0x3F); /* hr is 0-23 */
133 tm
->tm_mday
= bcd2bin(buf
[CCR_MDAY
]);
134 tm
->tm_mon
= bcd2bin(buf
[CCR_MONTH
]) - 1; /* mon is 0-11 */
135 tm
->tm_year
= bcd2bin(buf
[CCR_YEAR
])
136 + (bcd2bin(buf
[CCR_Y2K
]) * 100) - 1900;
137 tm
->tm_wday
= buf
[CCR_WDAY
];
139 dev_dbg(&client
->dev
, "%s: tm is secs=%d, mins=%d, hours=%d, "
140 "mday=%d, mon=%d, year=%d, wday=%d\n",
142 tm
->tm_sec
, tm
->tm_min
, tm
->tm_hour
,
143 tm
->tm_mday
, tm
->tm_mon
, tm
->tm_year
, tm
->tm_wday
);
148 static int x1205_get_status(struct i2c_client
*client
, unsigned char *sr
)
150 static unsigned char sr_addr
[2] = { 0, X1205_REG_SR
};
152 struct i2c_msg msgs
[] = {
153 { /* setup read ptr */
154 .addr
= client
->addr
,
159 .addr
= client
->addr
,
166 /* read status register */
167 if (i2c_transfer(client
->adapter
, &msgs
[0], 2) != 2) {
168 dev_err(&client
->dev
, "%s: read error\n", __func__
);
175 static int x1205_set_datetime(struct i2c_client
*client
, struct rtc_time
*tm
,
176 u8 reg_base
, unsigned char alm_enable
)
179 unsigned char rdata
[10] = { 0, reg_base
};
180 unsigned char *buf
= rdata
+ 2;
182 static const unsigned char wel
[3] = { 0, X1205_REG_SR
,
185 static const unsigned char rwel
[3] = { 0, X1205_REG_SR
,
186 X1205_SR_WEL
| X1205_SR_RWEL
};
188 static const unsigned char diswe
[3] = { 0, X1205_REG_SR
, 0 };
190 dev_dbg(&client
->dev
,
191 "%s: sec=%d min=%d hour=%d mday=%d mon=%d year=%d wday=%d\n",
192 __func__
, tm
->tm_sec
, tm
->tm_min
, tm
->tm_hour
, tm
->tm_mday
,
193 tm
->tm_mon
, tm
->tm_year
, tm
->tm_wday
);
195 buf
[CCR_SEC
] = bin2bcd(tm
->tm_sec
);
196 buf
[CCR_MIN
] = bin2bcd(tm
->tm_min
);
198 /* set hour and 24hr bit */
199 buf
[CCR_HOUR
] = bin2bcd(tm
->tm_hour
) | X1205_HR_MIL
;
201 buf
[CCR_MDAY
] = bin2bcd(tm
->tm_mday
);
204 buf
[CCR_MONTH
] = bin2bcd(tm
->tm_mon
+ 1);
206 /* year, since the rtc epoch*/
207 buf
[CCR_YEAR
] = bin2bcd(tm
->tm_year
% 100);
208 buf
[CCR_WDAY
] = tm
->tm_wday
& 0x07;
209 buf
[CCR_Y2K
] = bin2bcd((tm
->tm_year
+ 1900) / 100);
211 /* If writing alarm registers, set compare bits on registers 0-4 */
212 if (reg_base
< X1205_CCR_BASE
)
213 for (i
= 0; i
<= 4; i
++)
216 /* this sequence is required to unlock the chip */
217 xfer
= i2c_master_send(client
, wel
, 3);
219 dev_err(&client
->dev
, "%s: wel - %d\n", __func__
, xfer
);
223 xfer
= i2c_master_send(client
, rwel
, 3);
225 dev_err(&client
->dev
, "%s: rwel - %d\n", __func__
, xfer
);
229 xfer
= i2c_master_send(client
, rdata
, sizeof(rdata
));
230 if (xfer
!= sizeof(rdata
)) {
231 dev_err(&client
->dev
,
232 "%s: result=%d addr=%02x, data=%02x\n",
234 xfer
, rdata
[1], rdata
[2]);
238 /* If we wrote to the nonvolatile region, wait 10msec for write cycle*/
239 if (reg_base
< X1205_CCR_BASE
) {
240 unsigned char al0e
[3] = { 0, X1205_REG_INT
, 0 };
244 /* ...and set or clear the AL0E bit in the INT register */
246 /* Need to set RWEL again as the write has cleared it */
247 xfer
= i2c_master_send(client
, rwel
, 3);
249 dev_err(&client
->dev
,
250 "%s: aloe rwel - %d\n",
257 al0e
[2] = X1205_INT_AL0E
;
259 xfer
= i2c_master_send(client
, al0e
, 3);
261 dev_err(&client
->dev
,
268 /* and wait 10msec again for this write to complete */
272 /* disable further writes */
273 xfer
= i2c_master_send(client
, diswe
, 3);
275 dev_err(&client
->dev
, "%s: diswe - %d\n", __func__
, xfer
);
282 static int x1205_fix_osc(struct i2c_client
*client
)
287 memset(&tm
, 0, sizeof(tm
));
289 err
= x1205_set_datetime(client
, &tm
, X1205_CCR_BASE
, 0);
291 dev_err(&client
->dev
, "unable to restart the oscillator\n");
296 static int x1205_get_dtrim(struct i2c_client
*client
, int *trim
)
299 static unsigned char dtr_addr
[2] = { 0, X1205_REG_DTR
};
301 struct i2c_msg msgs
[] = {
302 { /* setup read ptr */
303 .addr
= client
->addr
,
308 .addr
= client
->addr
,
315 /* read dtr register */
316 if (i2c_transfer(client
->adapter
, &msgs
[0], 2) != 2) {
317 dev_err(&client
->dev
, "%s: read error\n", __func__
);
321 dev_dbg(&client
->dev
, "%s: raw dtr=%x\n", __func__
, dtr
);
325 if (dtr
& X1205_DTR_DTR0
)
328 if (dtr
& X1205_DTR_DTR1
)
331 if (dtr
& X1205_DTR_DTR2
)
337 static int x1205_get_atrim(struct i2c_client
*client
, int *trim
)
340 static unsigned char atr_addr
[2] = { 0, X1205_REG_ATR
};
342 struct i2c_msg msgs
[] = {
343 {/* setup read ptr */
344 .addr
= client
->addr
,
349 .addr
= client
->addr
,
356 /* read atr register */
357 if (i2c_transfer(client
->adapter
, &msgs
[0], 2) != 2) {
358 dev_err(&client
->dev
, "%s: read error\n", __func__
);
362 dev_dbg(&client
->dev
, "%s: raw atr=%x\n", __func__
, atr
);
364 /* atr is a two's complement value on 6 bits,
365 * perform sign extension. The formula is
366 * Catr = (atr * 0.25pF) + 11.00pF.
368 atr
= sign_extend32(atr
, 5);
370 dev_dbg(&client
->dev
, "%s: raw atr=%x (%d)\n", __func__
, atr
, atr
);
372 *trim
= (atr
* 250) + 11000;
374 dev_dbg(&client
->dev
, "%s: real=%d\n", __func__
, *trim
);
380 unsigned char reg
, mask
, min
, max
;
383 static int x1205_validate_client(struct i2c_client
*client
)
387 /* Probe array. We will read the register at the specified
388 * address and check if the given bits are zero.
390 static const unsigned char probe_zero_pattern
[] = {
399 static const struct x1205_limit probe_limits_pattern
[] = {
400 /* register, mask, min, max */
401 { X1205_REG_Y2K
, 0xFF, 19, 20 },
402 { X1205_REG_DW
, 0xFF, 0, 6 },
403 { X1205_REG_YR
, 0xFF, 0, 99 },
404 { X1205_REG_MO
, 0xFF, 0, 12 },
405 { X1205_REG_DT
, 0xFF, 0, 31 },
406 { X1205_REG_HR
, 0x7F, 0, 23 },
407 { X1205_REG_MN
, 0xFF, 0, 59 },
408 { X1205_REG_SC
, 0xFF, 0, 59 },
409 { X1205_REG_Y2K1
, 0xFF, 19, 20 },
410 { X1205_REG_Y2K0
, 0xFF, 19, 20 },
413 /* check that registers have bits a 0 where expected */
414 for (i
= 0; i
< ARRAY_SIZE(probe_zero_pattern
); i
+= 2) {
417 unsigned char addr
[2] = { 0, probe_zero_pattern
[i
] };
419 struct i2c_msg msgs
[2] = {
421 .addr
= client
->addr
,
426 .addr
= client
->addr
,
433 xfer
= i2c_transfer(client
->adapter
, msgs
, 2);
435 dev_err(&client
->dev
,
436 "%s: could not read register %x\n",
437 __func__
, probe_zero_pattern
[i
]);
442 if ((buf
& probe_zero_pattern
[i
+1]) != 0) {
443 dev_err(&client
->dev
,
444 "%s: register=%02x, zero pattern=%d, value=%x\n",
445 __func__
, probe_zero_pattern
[i
], i
, buf
);
451 /* check limits (only registers with bcd values) */
452 for (i
= 0; i
< ARRAY_SIZE(probe_limits_pattern
); i
++) {
453 unsigned char reg
, value
;
455 unsigned char addr
[2] = { 0, probe_limits_pattern
[i
].reg
};
457 struct i2c_msg msgs
[2] = {
459 .addr
= client
->addr
,
464 .addr
= client
->addr
,
471 xfer
= i2c_transfer(client
->adapter
, msgs
, 2);
473 dev_err(&client
->dev
,
474 "%s: could not read register %x\n",
475 __func__
, probe_limits_pattern
[i
].reg
);
480 value
= bcd2bin(reg
& probe_limits_pattern
[i
].mask
);
482 if (value
> probe_limits_pattern
[i
].max
||
483 value
< probe_limits_pattern
[i
].min
) {
484 dev_dbg(&client
->dev
,
485 "%s: register=%x, lim pattern=%d, value=%d\n",
486 __func__
, probe_limits_pattern
[i
].reg
,
496 static int x1205_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
499 unsigned char intreg
, status
;
500 static unsigned char int_addr
[2] = { 0, X1205_REG_INT
};
501 struct i2c_client
*client
= to_i2c_client(dev
);
502 struct i2c_msg msgs
[] = {
503 { /* setup read ptr */
504 .addr
= client
->addr
,
508 {/* read INT register */
510 .addr
= client
->addr
,
517 /* read interrupt register and status register */
518 if (i2c_transfer(client
->adapter
, &msgs
[0], 2) != 2) {
519 dev_err(&client
->dev
, "%s: read error\n", __func__
);
522 err
= x1205_get_status(client
, &status
);
524 alrm
->pending
= (status
& X1205_SR_AL0
) ? 1 : 0;
525 alrm
->enabled
= (intreg
& X1205_INT_AL0E
) ? 1 : 0;
526 err
= x1205_get_datetime(client
, &alrm
->time
, X1205_ALM0_BASE
);
531 static int x1205_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
533 return x1205_set_datetime(to_i2c_client(dev
),
534 &alrm
->time
, X1205_ALM0_BASE
, alrm
->enabled
);
537 static int x1205_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
539 return x1205_get_datetime(to_i2c_client(dev
),
543 static int x1205_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
545 return x1205_set_datetime(to_i2c_client(dev
),
546 tm
, X1205_CCR_BASE
, 0);
549 static int x1205_rtc_proc(struct device
*dev
, struct seq_file
*seq
)
551 int err
, dtrim
, atrim
;
553 err
= x1205_get_dtrim(to_i2c_client(dev
), &dtrim
);
555 seq_printf(seq
, "digital_trim\t: %d ppm\n", dtrim
);
557 err
= x1205_get_atrim(to_i2c_client(dev
), &atrim
);
559 seq_printf(seq
, "analog_trim\t: %d.%02d pF\n",
560 atrim
/ 1000, atrim
% 1000);
564 static const struct rtc_class_ops x1205_rtc_ops
= {
565 .proc
= x1205_rtc_proc
,
566 .read_time
= x1205_rtc_read_time
,
567 .set_time
= x1205_rtc_set_time
,
568 .read_alarm
= x1205_rtc_read_alarm
,
569 .set_alarm
= x1205_rtc_set_alarm
,
572 static ssize_t
x1205_sysfs_show_atrim(struct device
*dev
,
573 struct device_attribute
*attr
, char *buf
)
577 err
= x1205_get_atrim(to_i2c_client(dev
), &atrim
);
581 return sprintf(buf
, "%d.%02d pF\n", atrim
/ 1000, atrim
% 1000);
583 static DEVICE_ATTR(atrim
, S_IRUGO
, x1205_sysfs_show_atrim
, NULL
);
585 static ssize_t
x1205_sysfs_show_dtrim(struct device
*dev
,
586 struct device_attribute
*attr
, char *buf
)
590 err
= x1205_get_dtrim(to_i2c_client(dev
), &dtrim
);
594 return sprintf(buf
, "%d ppm\n", dtrim
);
596 static DEVICE_ATTR(dtrim
, S_IRUGO
, x1205_sysfs_show_dtrim
, NULL
);
598 static int x1205_sysfs_register(struct device
*dev
)
602 err
= device_create_file(dev
, &dev_attr_atrim
);
606 err
= device_create_file(dev
, &dev_attr_dtrim
);
608 device_remove_file(dev
, &dev_attr_atrim
);
613 static void x1205_sysfs_unregister(struct device
*dev
)
615 device_remove_file(dev
, &dev_attr_atrim
);
616 device_remove_file(dev
, &dev_attr_dtrim
);
620 static int x1205_probe(struct i2c_client
*client
,
621 const struct i2c_device_id
*id
)
625 struct rtc_device
*rtc
;
627 dev_dbg(&client
->dev
, "%s\n", __func__
);
629 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
))
632 if (x1205_validate_client(client
) < 0)
635 rtc
= devm_rtc_device_register(&client
->dev
, x1205_driver
.driver
.name
,
636 &x1205_rtc_ops
, THIS_MODULE
);
641 i2c_set_clientdata(client
, rtc
);
643 /* Check for power failures and eventually enable the osc */
644 err
= x1205_get_status(client
, &sr
);
646 if (sr
& X1205_SR_RTCF
) {
647 dev_err(&client
->dev
,
648 "power failure detected, "
649 "please set the clock\n");
651 x1205_fix_osc(client
);
654 dev_err(&client
->dev
, "couldn't read status\n");
657 err
= x1205_sysfs_register(&client
->dev
);
659 dev_err(&client
->dev
, "Unable to create sysfs entries\n");
664 static int x1205_remove(struct i2c_client
*client
)
666 x1205_sysfs_unregister(&client
->dev
);
670 static const struct i2c_device_id x1205_id
[] = {
674 MODULE_DEVICE_TABLE(i2c
, x1205_id
);
676 static struct i2c_driver x1205_driver
= {
680 .probe
= x1205_probe
,
681 .remove
= x1205_remove
,
682 .id_table
= x1205_id
,
685 module_i2c_driver(x1205_driver
);
688 "Karen Spearel <kas111 at gmail dot com>, "
689 "Alessandro Zummo <a.zummo@towertech.it>");
690 MODULE_DESCRIPTION("Xicor/Intersil X1205 RTC driver");
691 MODULE_LICENSE("GPL");