2 * Intersil ISL1208 rtc class driver
4 * Copyright 2005,2006 Hebert Valerio Riedel <hvr@gnu.org>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
13 #include <linux/module.h>
14 #include <linux/i2c.h>
15 #include <linux/bcd.h>
16 #include <linux/rtc.h>
18 #define DRV_NAME "isl1208"
19 #define DRV_VERSION "0.2"
23 #define ISL1208_REG_SC 0x00
24 #define ISL1208_REG_MN 0x01
25 #define ISL1208_REG_HR 0x02
26 #define ISL1208_REG_HR_MIL (1<<7) /* 24h/12h mode */
27 #define ISL1208_REG_HR_PM (1<<5) /* PM/AM bit in 12h mode */
28 #define ISL1208_REG_DT 0x03
29 #define ISL1208_REG_MO 0x04
30 #define ISL1208_REG_YR 0x05
31 #define ISL1208_REG_DW 0x06
32 #define ISL1208_RTC_SECTION_LEN 7
34 /* control/status section */
35 #define ISL1208_REG_SR 0x07
36 #define ISL1208_REG_SR_ARST (1<<7) /* auto reset */
37 #define ISL1208_REG_SR_XTOSCB (1<<6) /* crystal oscillator */
38 #define ISL1208_REG_SR_WRTC (1<<4) /* write rtc */
39 #define ISL1208_REG_SR_ALM (1<<2) /* alarm */
40 #define ISL1208_REG_SR_BAT (1<<1) /* battery */
41 #define ISL1208_REG_SR_RTCF (1<<0) /* rtc fail */
42 #define ISL1208_REG_INT 0x08
43 #define ISL1208_REG_09 0x09 /* reserved */
44 #define ISL1208_REG_ATR 0x0a
45 #define ISL1208_REG_DTR 0x0b
48 #define ISL1208_REG_SCA 0x0c
49 #define ISL1208_REG_MNA 0x0d
50 #define ISL1208_REG_HRA 0x0e
51 #define ISL1208_REG_DTA 0x0f
52 #define ISL1208_REG_MOA 0x10
53 #define ISL1208_REG_DWA 0x11
54 #define ISL1208_ALARM_SECTION_LEN 6
57 #define ISL1208_REG_USR1 0x12
58 #define ISL1208_REG_USR2 0x13
59 #define ISL1208_USR_SECTION_LEN 2
61 /* i2c configuration */
62 #define ISL1208_I2C_ADDR 0xde
64 static const unsigned short normal_i2c
[] = {
65 ISL1208_I2C_ADDR
>>1, I2C_CLIENT_END
67 I2C_CLIENT_INSMOD
; /* defines addr_data */
69 static int isl1208_attach_adapter(struct i2c_adapter
*adapter
);
70 static int isl1208_detach_client(struct i2c_client
*client
);
72 static struct i2c_driver isl1208_driver
= {
76 .id
= I2C_DRIVERID_ISL1208
,
77 .attach_adapter
= &isl1208_attach_adapter
,
78 .detach_client
= &isl1208_detach_client
,
83 isl1208_i2c_read_regs(struct i2c_client
*client
, u8 reg
, u8 buf
[],
86 u8 reg_addr
[1] = { reg
};
87 struct i2c_msg msgs
[2] = {
88 { client
->addr
, client
->flags
, sizeof(reg_addr
), reg_addr
},
89 { client
->addr
, client
->flags
| I2C_M_RD
, len
, buf
}
94 BUG_ON(reg
> ISL1208_REG_USR2
);
95 BUG_ON(reg
+ len
> ISL1208_REG_USR2
+ 1);
97 ret
= i2c_transfer(client
->adapter
, msgs
, 2);
105 isl1208_i2c_set_regs(struct i2c_client
*client
, u8 reg
, u8
const buf
[],
108 u8 i2c_buf
[ISL1208_REG_USR2
+ 2];
109 struct i2c_msg msgs
[1] = {
110 { client
->addr
, client
->flags
, len
+ 1, i2c_buf
}
115 BUG_ON(reg
> ISL1208_REG_USR2
);
116 BUG_ON(reg
+ len
> ISL1208_REG_USR2
+ 1);
119 memcpy(&i2c_buf
[1], &buf
[0], len
);
121 ret
= i2c_transfer(client
->adapter
, msgs
, 1);
127 /* simple check to see wether we have a isl1208 */
128 static int isl1208_i2c_validate_client(struct i2c_client
*client
)
130 u8 regs
[ISL1208_RTC_SECTION_LEN
] = { 0, };
131 u8 zero_mask
[ISL1208_RTC_SECTION_LEN
] = {
132 0x80, 0x80, 0x40, 0xc0, 0xe0, 0x00, 0xf8
137 ret
= isl1208_i2c_read_regs(client
, 0, regs
, ISL1208_RTC_SECTION_LEN
);
141 for (i
= 0; i
< ISL1208_RTC_SECTION_LEN
; ++i
) {
142 if (regs
[i
] & zero_mask
[i
]) /* check if bits are cleared */
149 static int isl1208_i2c_get_sr(struct i2c_client
*client
)
151 return i2c_smbus_read_byte_data(client
, ISL1208_REG_SR
) == -1 ? -EIO
:0;
154 static int isl1208_i2c_get_atr(struct i2c_client
*client
)
156 int atr
= i2c_smbus_read_byte_data(client
, ISL1208_REG_ATR
);
161 /* The 6bit value in the ATR register controls the load
162 * capacitance C_load * in steps of 0.25pF
164 * bit (1<<5) of the ATR register is inverted
166 * C_load(ATR=0x20) = 4.50pF
167 * C_load(ATR=0x00) = 12.50pF
168 * C_load(ATR=0x1f) = 20.25pF
172 atr
&= 0x3f; /* mask out lsb */
173 atr
^= 1<<5; /* invert 6th bit */
174 atr
+= 2*9; /* add offset of 4.5pF; unit[atr] = 0.25pF */
179 static int isl1208_i2c_get_dtr(struct i2c_client
*client
)
181 int dtr
= i2c_smbus_read_byte_data(client
, ISL1208_REG_DTR
);
186 /* dtr encodes adjustments of {-60,-40,-20,0,20,40,60} ppm */
187 dtr
= ((dtr
& 0x3) * 20) * (dtr
& (1<<2) ? -1 : 1);
192 static int isl1208_i2c_get_usr(struct i2c_client
*client
)
194 u8 buf
[ISL1208_USR_SECTION_LEN
] = { 0, };
197 ret
= isl1208_i2c_read_regs (client
, ISL1208_REG_USR1
, buf
,
198 ISL1208_USR_SECTION_LEN
);
202 return (buf
[1] << 8) | buf
[0];
205 static int isl1208_i2c_set_usr(struct i2c_client
*client
, u16 usr
)
207 u8 buf
[ISL1208_USR_SECTION_LEN
];
210 buf
[1] = (usr
>> 8) & 0xff;
212 return isl1208_i2c_set_regs (client
, ISL1208_REG_USR1
, buf
,
213 ISL1208_USR_SECTION_LEN
);
216 static int isl1208_rtc_proc(struct device
*dev
, struct seq_file
*seq
)
218 struct i2c_client
*const client
= to_i2c_client(dev
);
219 int sr
, dtr
, atr
, usr
;
221 sr
= isl1208_i2c_get_sr(client
);
223 dev_err(&client
->dev
, "%s: reading SR failed\n", __func__
);
227 seq_printf(seq
, "status_reg\t:%s%s%s%s%s%s (0x%.2x)\n",
228 (sr
& ISL1208_REG_SR_RTCF
) ? " RTCF" : "",
229 (sr
& ISL1208_REG_SR_BAT
) ? " BAT" : "",
230 (sr
& ISL1208_REG_SR_ALM
) ? " ALM" : "",
231 (sr
& ISL1208_REG_SR_WRTC
) ? " WRTC" : "",
232 (sr
& ISL1208_REG_SR_XTOSCB
) ? " XTOSCB" : "",
233 (sr
& ISL1208_REG_SR_ARST
) ? " ARST" : "",
236 seq_printf(seq
, "batt_status\t: %s\n",
237 (sr
& ISL1208_REG_SR_RTCF
) ? "bad" : "okay");
239 dtr
= isl1208_i2c_get_dtr(client
);
241 seq_printf(seq
, "digital_trim\t: %d ppm\n", dtr
);
243 atr
= isl1208_i2c_get_atr(client
);
245 seq_printf(seq
, "analog_trim\t: %d.%.2d pF\n",
246 atr
>>2, (atr
&0x3)*25);
248 usr
= isl1208_i2c_get_usr(client
);
250 seq_printf(seq
, "user_data\t: 0x%.4x\n", usr
);
256 static int isl1208_i2c_read_time(struct i2c_client
*client
,
260 u8 regs
[ISL1208_RTC_SECTION_LEN
] = { 0, };
262 sr
= isl1208_i2c_get_sr(client
);
264 dev_err(&client
->dev
, "%s: reading SR failed\n", __func__
);
268 sr
= isl1208_i2c_read_regs(client
, 0, regs
, ISL1208_RTC_SECTION_LEN
);
270 dev_err(&client
->dev
, "%s: reading RTC section failed\n",
275 tm
->tm_sec
= BCD2BIN(regs
[ISL1208_REG_SC
]);
276 tm
->tm_min
= BCD2BIN(regs
[ISL1208_REG_MN
]);
277 { /* HR field has a more complex interpretation */
278 const u8 _hr
= regs
[ISL1208_REG_HR
];
279 if (_hr
& ISL1208_REG_HR_MIL
) /* 24h format */
280 tm
->tm_hour
= BCD2BIN(_hr
& 0x3f);
282 tm
->tm_hour
= BCD2BIN(_hr
& 0x1f);
283 if (_hr
& ISL1208_REG_HR_PM
) /* PM flag set */
288 tm
->tm_mday
= BCD2BIN(regs
[ISL1208_REG_DT
]);
289 tm
->tm_mon
= BCD2BIN(regs
[ISL1208_REG_MO
]) - 1; /* rtc starts at 1 */
290 tm
->tm_year
= BCD2BIN(regs
[ISL1208_REG_YR
]) + 100;
291 tm
->tm_wday
= BCD2BIN(regs
[ISL1208_REG_DW
]);
296 static int isl1208_i2c_read_alarm(struct i2c_client
*client
,
297 struct rtc_wkalrm
*alarm
)
299 struct rtc_time
*const tm
= &alarm
->time
;
300 u8 regs
[ISL1208_ALARM_SECTION_LEN
] = { 0, };
303 sr
= isl1208_i2c_get_sr(client
);
305 dev_err(&client
->dev
, "%s: reading SR failed\n", __func__
);
309 sr
= isl1208_i2c_read_regs(client
, ISL1208_REG_SCA
, regs
,
310 ISL1208_ALARM_SECTION_LEN
);
312 dev_err(&client
->dev
, "%s: reading alarm section failed\n",
317 /* MSB of each alarm register is an enable bit */
318 tm
->tm_sec
= BCD2BIN(regs
[ISL1208_REG_SCA
-ISL1208_REG_SCA
] & 0x7f);
319 tm
->tm_min
= BCD2BIN(regs
[ISL1208_REG_MNA
-ISL1208_REG_SCA
] & 0x7f);
320 tm
->tm_hour
= BCD2BIN(regs
[ISL1208_REG_HRA
-ISL1208_REG_SCA
] & 0x3f);
321 tm
->tm_mday
= BCD2BIN(regs
[ISL1208_REG_DTA
-ISL1208_REG_SCA
] & 0x3f);
322 tm
->tm_mon
= BCD2BIN(regs
[ISL1208_REG_MOA
-ISL1208_REG_SCA
] & 0x1f)-1;
323 tm
->tm_wday
= BCD2BIN(regs
[ISL1208_REG_DWA
-ISL1208_REG_SCA
] & 0x03);
328 static int isl1208_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
330 return isl1208_i2c_read_time(to_i2c_client(dev
), tm
);
333 static int isl1208_i2c_set_time(struct i2c_client
*client
,
334 struct rtc_time
const *tm
)
337 u8 regs
[ISL1208_RTC_SECTION_LEN
] = { 0, };
339 regs
[ISL1208_REG_SC
] = BIN2BCD(tm
->tm_sec
);
340 regs
[ISL1208_REG_MN
] = BIN2BCD(tm
->tm_min
);
341 regs
[ISL1208_REG_HR
] = BIN2BCD(tm
->tm_hour
) | ISL1208_REG_HR_MIL
;
343 regs
[ISL1208_REG_DT
] = BIN2BCD(tm
->tm_mday
);
344 regs
[ISL1208_REG_MO
] = BIN2BCD(tm
->tm_mon
+ 1);
345 regs
[ISL1208_REG_YR
] = BIN2BCD(tm
->tm_year
- 100);
347 regs
[ISL1208_REG_DW
] = BIN2BCD(tm
->tm_wday
& 7);
349 sr
= isl1208_i2c_get_sr(client
);
351 dev_err(&client
->dev
, "%s: reading SR failed\n", __func__
);
356 sr
= i2c_smbus_write_byte_data (client
, ISL1208_REG_SR
,
357 sr
| ISL1208_REG_SR_WRTC
);
359 dev_err(&client
->dev
, "%s: writing SR failed\n", __func__
);
363 /* write RTC registers */
364 sr
= isl1208_i2c_set_regs(client
, 0, regs
, ISL1208_RTC_SECTION_LEN
);
366 dev_err(&client
->dev
, "%s: writing RTC section failed\n",
371 /* clear WRTC again */
372 sr
= i2c_smbus_write_byte_data (client
, ISL1208_REG_SR
,
373 sr
& ~ISL1208_REG_SR_WRTC
);
375 dev_err(&client
->dev
, "%s: writing SR failed\n", __func__
);
383 static int isl1208_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
385 return isl1208_i2c_set_time(to_i2c_client(dev
), tm
);
388 static int isl1208_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
390 return isl1208_i2c_read_alarm(to_i2c_client(dev
), alarm
);
393 static const struct rtc_class_ops isl1208_rtc_ops
= {
394 .proc
= isl1208_rtc_proc
,
395 .read_time
= isl1208_rtc_read_time
,
396 .set_time
= isl1208_rtc_set_time
,
397 .read_alarm
= isl1208_rtc_read_alarm
,
398 //.set_alarm = isl1208_rtc_set_alarm,
401 /* sysfs interface */
403 static ssize_t
isl1208_sysfs_show_atrim(struct device
*dev
,
404 struct device_attribute
*attr
,
409 atr
= isl1208_i2c_get_atr(to_i2c_client(dev
));
413 return sprintf(buf
, "%d.%.2d pF\n", atr
>>2, (atr
&0x3)*25);
415 static DEVICE_ATTR(atrim
, S_IRUGO
, isl1208_sysfs_show_atrim
, NULL
);
417 static ssize_t
isl1208_sysfs_show_dtrim(struct device
*dev
,
418 struct device_attribute
*attr
,
423 dtr
= isl1208_i2c_get_dtr(to_i2c_client(dev
));
427 return sprintf(buf
, "%d ppm\n", dtr
);
429 static DEVICE_ATTR(dtrim
, S_IRUGO
, isl1208_sysfs_show_dtrim
, NULL
);
431 static ssize_t
isl1208_sysfs_show_usr(struct device
*dev
,
432 struct device_attribute
*attr
,
437 usr
= isl1208_i2c_get_usr(to_i2c_client(dev
));
441 return sprintf(buf
, "0x%.4x\n", usr
);
444 static ssize_t
isl1208_sysfs_store_usr(struct device
*dev
,
445 struct device_attribute
*attr
,
446 const char *buf
, size_t count
)
450 if (buf
[0] == '0' && (buf
[1] == 'x' || buf
[1] == 'X')) {
451 if (sscanf(buf
, "%x", &usr
) != 1)
454 if (sscanf(buf
, "%d", &usr
) != 1)
458 if (usr
< 0 || usr
> 0xffff)
461 return isl1208_i2c_set_usr(to_i2c_client(dev
), usr
) ? -EIO
: count
;
463 static DEVICE_ATTR(usr
, S_IRUGO
| S_IWUSR
, isl1208_sysfs_show_usr
,
464 isl1208_sysfs_store_usr
);
467 isl1208_probe(struct i2c_adapter
*adapter
, int addr
, int kind
)
470 struct i2c_client
*new_client
= NULL
;
471 struct rtc_device
*rtc
= NULL
;
473 if (!i2c_check_functionality(adapter
, I2C_FUNC_I2C
)) {
478 new_client
= kzalloc(sizeof(struct i2c_client
), GFP_KERNEL
);
479 if (new_client
== NULL
) {
484 new_client
->addr
= addr
;
485 new_client
->adapter
= adapter
;
486 new_client
->driver
= &isl1208_driver
;
487 new_client
->flags
= 0;
488 strcpy(new_client
->name
, DRV_NAME
);
491 rc
= isl1208_i2c_validate_client(new_client
);
496 rc
= i2c_attach_client(new_client
);
500 dev_info(&new_client
->dev
,
501 "chip found, driver version " DRV_VERSION
"\n");
503 rtc
= rtc_device_register(isl1208_driver
.driver
.name
,
505 &isl1208_rtc_ops
, THIS_MODULE
);
512 i2c_set_clientdata(new_client
, rtc
);
514 rc
= isl1208_i2c_get_sr(new_client
);
516 dev_err(&new_client
->dev
, "reading status failed\n");
517 goto failout_unregister
;
520 if (rc
& ISL1208_REG_SR_RTCF
)
521 dev_warn(&new_client
->dev
, "rtc power failure detected, "
522 "please set clock.\n");
524 rc
= device_create_file(&new_client
->dev
, &dev_attr_atrim
);
526 goto failout_unregister
;
527 rc
= device_create_file(&new_client
->dev
, &dev_attr_dtrim
);
530 rc
= device_create_file(&new_client
->dev
, &dev_attr_usr
);
537 device_remove_file(&new_client
->dev
, &dev_attr_dtrim
);
539 device_remove_file(&new_client
->dev
, &dev_attr_atrim
);
541 rtc_device_unregister(rtc
);
543 i2c_detach_client(new_client
);
550 isl1208_attach_adapter (struct i2c_adapter
*adapter
)
552 return i2c_probe(adapter
, &addr_data
, isl1208_probe
);
556 isl1208_detach_client(struct i2c_client
*client
)
559 struct rtc_device
*const rtc
= i2c_get_clientdata(client
);
562 rtc_device_unregister(rtc
); /* do we need to kfree? */
564 rc
= i2c_detach_client(client
);
573 /* module management */
575 static int __init
isl1208_init(void)
577 return i2c_add_driver(&isl1208_driver
);
580 static void __exit
isl1208_exit(void)
582 i2c_del_driver(&isl1208_driver
);
585 MODULE_AUTHOR("Herbert Valerio Riedel <hvr@gnu.org>");
586 MODULE_DESCRIPTION("Intersil ISL1208 RTC driver");
587 MODULE_LICENSE("GPL");
588 MODULE_VERSION(DRV_VERSION
);
590 module_init(isl1208_init
);
591 module_exit(isl1208_exit
);