2 drivers/rtc/rtc-isl1208.c | 357 +++++++++++++++++++++-------------------------
3 1 file changed, 170 insertions(+), 187 deletions(-)
5 Index: linux-2.6.25.1/drivers/rtc/rtc-isl1208.c
6 ===================================================================
7 --- linux-2.6.25.1.orig/drivers/rtc/rtc-isl1208.c
8 +++ linux-2.6.25.1/drivers/rtc/rtc-isl1208.c
10 #include <linux/bcd.h>
11 #include <linux/rtc.h>
13 -#define DRV_NAME "isl1208"
14 -#define DRV_VERSION "0.2"
15 +#define DRV_VERSION "0.3"
19 #define ISL1208_REG_SC 0x00
20 #define ISL1208_REG_MN 0x01
21 #define ISL1208_REG_HR 0x02
22 -#define ISL1208_REG_HR_MIL (1<<7) /* 24h/12h mode */
23 -#define ISL1208_REG_HR_PM (1<<5) /* PM/AM bit in 12h mode */
24 +#define ISL1208_REG_HR_MIL (1<<7) /* 24h/12h mode */
25 +#define ISL1208_REG_HR_PM (1<<5) /* PM/AM bit in 12h mode */
26 #define ISL1208_REG_DT 0x03
27 #define ISL1208_REG_MO 0x04
28 #define ISL1208_REG_YR 0x05
31 /* control/status section */
32 #define ISL1208_REG_SR 0x07
33 -#define ISL1208_REG_SR_ARST (1<<7) /* auto reset */
34 -#define ISL1208_REG_SR_XTOSCB (1<<6) /* crystal oscillator */
35 -#define ISL1208_REG_SR_WRTC (1<<4) /* write rtc */
36 -#define ISL1208_REG_SR_ALM (1<<2) /* alarm */
37 -#define ISL1208_REG_SR_BAT (1<<1) /* battery */
38 -#define ISL1208_REG_SR_RTCF (1<<0) /* rtc fail */
39 +#define ISL1208_REG_SR_ARST (1<<7) /* auto reset */
40 +#define ISL1208_REG_SR_XTOSCB (1<<6) /* crystal oscillator */
41 +#define ISL1208_REG_SR_WRTC (1<<4) /* write rtc */
42 +#define ISL1208_REG_SR_ALM (1<<2) /* alarm */
43 +#define ISL1208_REG_SR_BAT (1<<1) /* battery */
44 +#define ISL1208_REG_SR_RTCF (1<<0) /* rtc fail */
45 #define ISL1208_REG_INT 0x08
46 -#define ISL1208_REG_09 0x09 /* reserved */
47 +#define ISL1208_REG_09 0x09 /* reserved */
48 #define ISL1208_REG_ATR 0x0a
49 #define ISL1208_REG_DTR 0x0b
52 #define ISL1208_REG_USR2 0x13
53 #define ISL1208_USR_SECTION_LEN 2
55 -/* i2c configuration */
56 -#define ISL1208_I2C_ADDR 0xde
58 -static const unsigned short normal_i2c[] = {
59 - ISL1208_I2C_ADDR>>1, I2C_CLIENT_END
61 -I2C_CLIENT_INSMOD; /* defines addr_data */
63 -static int isl1208_attach_adapter(struct i2c_adapter *adapter);
64 -static int isl1208_detach_client(struct i2c_client *client);
66 -static struct i2c_driver isl1208_driver = {
70 - .id = I2C_DRIVERID_ISL1208,
71 - .attach_adapter = &isl1208_attach_adapter,
72 - .detach_client = &isl1208_detach_client,
74 +static struct i2c_driver isl1208_driver;
78 isl1208_i2c_read_regs(struct i2c_client *client, u8 reg, u8 buf[],
82 u8 reg_addr[1] = { reg };
83 struct i2c_msg msgs[2] = {
84 - { client->addr, client->flags, sizeof(reg_addr), reg_addr },
85 - { client->addr, client->flags | I2C_M_RD, len, buf }
86 + {client->addr, client->flags, sizeof(reg_addr), reg_addr}
88 + {client->addr, client->flags | I2C_M_RD, len, buf}
93 BUG_ON(reg > ISL1208_REG_USR2);
94 BUG_ON(reg + len > ISL1208_REG_USR2 + 1);
96 @@ -103,15 +84,14 @@ isl1208_i2c_read_regs(struct i2c_client
99 isl1208_i2c_set_regs(struct i2c_client *client, u8 reg, u8 const buf[],
103 u8 i2c_buf[ISL1208_REG_USR2 + 2];
104 struct i2c_msg msgs[1] = {
105 - { client->addr, client->flags, len + 1, i2c_buf }
106 + {client->addr, client->flags, len + 1, i2c_buf}
111 BUG_ON(reg > ISL1208_REG_USR2);
112 BUG_ON(reg + len > ISL1208_REG_USR2 + 1);
114 @@ -125,7 +105,8 @@ isl1208_i2c_set_regs(struct i2c_client *
117 /* simple check to see wether we have a isl1208 */
118 -static int isl1208_i2c_validate_client(struct i2c_client *client)
120 +isl1208_i2c_validate_client(struct i2c_client *client)
122 u8 regs[ISL1208_RTC_SECTION_LEN] = { 0, };
123 u8 zero_mask[ISL1208_RTC_SECTION_LEN] = {
124 @@ -139,24 +120,29 @@ static int isl1208_i2c_validate_client(s
127 for (i = 0; i < ISL1208_RTC_SECTION_LEN; ++i) {
128 - if (regs[i] & zero_mask[i]) /* check if bits are cleared */
129 + if (regs[i] & zero_mask[i]) /* check if bits are cleared */
136 -static int isl1208_i2c_get_sr(struct i2c_client *client)
138 +isl1208_i2c_get_sr(struct i2c_client *client)
140 - return i2c_smbus_read_byte_data(client, ISL1208_REG_SR) == -1 ? -EIO:0;
141 + int sr = i2c_smbus_read_byte_data(client, ISL1208_REG_SR);
148 -static int isl1208_i2c_get_atr(struct i2c_client *client)
150 +isl1208_i2c_get_atr(struct i2c_client *client)
152 int atr = i2c_smbus_read_byte_data(client, ISL1208_REG_ATR);
158 /* The 6bit value in the ATR register controls the load
159 * capacitance C_load * in steps of 0.25pF
160 @@ -169,51 +155,54 @@ static int isl1208_i2c_get_atr(struct i2
164 - atr &= 0x3f; /* mask out lsb */
165 - atr ^= 1<<5; /* invert 6th bit */
166 - atr += 2*9; /* add offset of 4.5pF; unit[atr] = 0.25pF */
167 + atr &= 0x3f; /* mask out lsb */
168 + atr ^= 1 << 5; /* invert 6th bit */
169 + atr += 2 * 9; /* add offset of 4.5pF; unit[atr] = 0.25pF */
174 -static int isl1208_i2c_get_dtr(struct i2c_client *client)
176 +isl1208_i2c_get_dtr(struct i2c_client *client)
178 int dtr = i2c_smbus_read_byte_data(client, ISL1208_REG_DTR);
183 /* dtr encodes adjustments of {-60,-40,-20,0,20,40,60} ppm */
184 - dtr = ((dtr & 0x3) * 20) * (dtr & (1<<2) ? -1 : 1);
185 + dtr = ((dtr & 0x3) * 20) * (dtr & (1 << 2) ? -1 : 1);
190 -static int isl1208_i2c_get_usr(struct i2c_client *client)
192 +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);
199 + ret = isl1208_i2c_read_regs(client, ISL1208_REG_USR1, buf,
200 + ISL1208_USR_SECTION_LEN);
204 return (buf[1] << 8) | buf[0];
207 -static int isl1208_i2c_set_usr(struct i2c_client *client, u16 usr)
209 +isl1208_i2c_set_usr(struct i2c_client *client, u16 usr)
211 u8 buf[ISL1208_USR_SECTION_LEN];
214 buf[1] = (usr >> 8) & 0xff;
216 - return isl1208_i2c_set_regs (client, ISL1208_REG_USR1, buf,
217 - ISL1208_USR_SECTION_LEN);
218 + return isl1208_i2c_set_regs(client, ISL1208_REG_USR1, buf,
219 + ISL1208_USR_SECTION_LEN);
222 -static int isl1208_rtc_proc(struct device *dev, struct seq_file *seq)
224 +isl1208_rtc_proc(struct device *dev, struct seq_file *seq)
226 struct i2c_client *const client = to_i2c_client(dev);
227 int sr, dtr, atr, usr;
228 @@ -230,20 +219,19 @@ static int isl1208_rtc_proc(struct devic
229 (sr & ISL1208_REG_SR_ALM) ? " ALM" : "",
230 (sr & ISL1208_REG_SR_WRTC) ? " WRTC" : "",
231 (sr & ISL1208_REG_SR_XTOSCB) ? " XTOSCB" : "",
232 - (sr & ISL1208_REG_SR_ARST) ? " ARST" : "",
234 + (sr & ISL1208_REG_SR_ARST) ? " ARST" : "", sr);
236 seq_printf(seq, "batt_status\t: %s\n",
237 (sr & ISL1208_REG_SR_RTCF) ? "bad" : "okay");
239 dtr = isl1208_i2c_get_dtr(client);
242 seq_printf(seq, "digital_trim\t: %d ppm\n", dtr);
244 atr = isl1208_i2c_get_atr(client);
246 seq_printf(seq, "analog_trim\t: %d.%.2d pF\n",
247 - atr>>2, (atr&0x3)*25);
248 + atr >> 2, (atr & 0x3) * 25);
250 usr = isl1208_i2c_get_usr(client);
252 @@ -252,9 +240,8 @@ static int isl1208_rtc_proc(struct devic
257 -static int isl1208_i2c_read_time(struct i2c_client *client,
258 - struct rtc_time *tm)
260 +isl1208_i2c_read_time(struct i2c_client *client, struct rtc_time *tm)
263 u8 regs[ISL1208_RTC_SECTION_LEN] = { 0, };
264 @@ -274,27 +261,30 @@ static int isl1208_i2c_read_time(struct
266 tm->tm_sec = BCD2BIN(regs[ISL1208_REG_SC]);
267 tm->tm_min = BCD2BIN(regs[ISL1208_REG_MN]);
268 - { /* HR field has a more complex interpretation */
270 + /* HR field has a more complex interpretation */
272 const u8 _hr = regs[ISL1208_REG_HR];
273 - if (_hr & ISL1208_REG_HR_MIL) /* 24h format */
274 + if (_hr & ISL1208_REG_HR_MIL) /* 24h format */
275 tm->tm_hour = BCD2BIN(_hr & 0x3f);
276 - else { // 12h format
279 tm->tm_hour = BCD2BIN(_hr & 0x1f);
280 - if (_hr & ISL1208_REG_HR_PM) /* PM flag set */
281 + if (_hr & ISL1208_REG_HR_PM) /* PM flag set */
286 tm->tm_mday = BCD2BIN(regs[ISL1208_REG_DT]);
287 - tm->tm_mon = BCD2BIN(regs[ISL1208_REG_MO]) - 1; /* rtc starts at 1 */
288 + tm->tm_mon = BCD2BIN(regs[ISL1208_REG_MO]) - 1; /* rtc starts at 1 */
289 tm->tm_year = BCD2BIN(regs[ISL1208_REG_YR]) + 100;
290 tm->tm_wday = BCD2BIN(regs[ISL1208_REG_DW]);
295 -static int isl1208_i2c_read_alarm(struct i2c_client *client,
296 - struct rtc_wkalrm *alarm)
298 +isl1208_i2c_read_alarm(struct i2c_client *client, struct rtc_wkalrm *alarm)
300 struct rtc_time *const tm = &alarm->time;
301 u8 regs[ISL1208_ALARM_SECTION_LEN] = { 0, };
302 @@ -307,7 +297,7 @@ static int isl1208_i2c_read_alarm(struct
305 sr = isl1208_i2c_read_regs(client, ISL1208_REG_SCA, regs,
306 - ISL1208_ALARM_SECTION_LEN);
307 + ISL1208_ALARM_SECTION_LEN);
309 dev_err(&client->dev, "%s: reading alarm section failed\n",
311 @@ -315,23 +305,25 @@ static int isl1208_i2c_read_alarm(struct
314 /* MSB of each alarm register is an enable bit */
315 - tm->tm_sec = BCD2BIN(regs[ISL1208_REG_SCA-ISL1208_REG_SCA] & 0x7f);
316 - tm->tm_min = BCD2BIN(regs[ISL1208_REG_MNA-ISL1208_REG_SCA] & 0x7f);
317 - tm->tm_hour = BCD2BIN(regs[ISL1208_REG_HRA-ISL1208_REG_SCA] & 0x3f);
318 - tm->tm_mday = BCD2BIN(regs[ISL1208_REG_DTA-ISL1208_REG_SCA] & 0x3f);
319 - tm->tm_mon = BCD2BIN(regs[ISL1208_REG_MOA-ISL1208_REG_SCA] & 0x1f)-1;
320 - tm->tm_wday = BCD2BIN(regs[ISL1208_REG_DWA-ISL1208_REG_SCA] & 0x03);
321 + tm->tm_sec = BCD2BIN(regs[ISL1208_REG_SCA - ISL1208_REG_SCA] & 0x7f);
322 + tm->tm_min = BCD2BIN(regs[ISL1208_REG_MNA - ISL1208_REG_SCA] & 0x7f);
323 + tm->tm_hour = BCD2BIN(regs[ISL1208_REG_HRA - ISL1208_REG_SCA] & 0x3f);
324 + tm->tm_mday = BCD2BIN(regs[ISL1208_REG_DTA - ISL1208_REG_SCA] & 0x3f);
326 + BCD2BIN(regs[ISL1208_REG_MOA - ISL1208_REG_SCA] & 0x1f) - 1;
327 + tm->tm_wday = BCD2BIN(regs[ISL1208_REG_DWA - ISL1208_REG_SCA] & 0x03);
332 -static int isl1208_rtc_read_time(struct device *dev, struct rtc_time *tm)
334 +isl1208_rtc_read_time(struct device *dev, struct rtc_time *tm)
336 return isl1208_i2c_read_time(to_i2c_client(dev), tm);
339 -static int isl1208_i2c_set_time(struct i2c_client *client,
340 - struct rtc_time const *tm)
342 +isl1208_i2c_set_time(struct i2c_client *client, struct rtc_time const *tm)
345 u8 regs[ISL1208_RTC_SECTION_LEN] = { 0, };
346 @@ -353,7 +345,7 @@ static int isl1208_i2c_set_time(struct i
350 - sr = i2c_smbus_write_byte_data (client, ISL1208_REG_SR,
351 + sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR,
352 sr | ISL1208_REG_SR_WRTC);
354 dev_err(&client->dev, "%s: writing SR failed\n", __func__);
355 @@ -369,7 +361,7 @@ static int isl1208_i2c_set_time(struct i
358 /* clear WRTC again */
359 - sr = i2c_smbus_write_byte_data (client, ISL1208_REG_SR,
360 + sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR,
361 sr & ~ISL1208_REG_SR_WRTC);
363 dev_err(&client->dev, "%s: writing SR failed\n", __func__);
364 @@ -380,70 +372,69 @@ static int isl1208_i2c_set_time(struct i
368 -static int isl1208_rtc_set_time(struct device *dev, struct rtc_time *tm)
370 +isl1208_rtc_set_time(struct device *dev, struct rtc_time *tm)
372 return isl1208_i2c_set_time(to_i2c_client(dev), tm);
375 -static int isl1208_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
377 +isl1208_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
379 return isl1208_i2c_read_alarm(to_i2c_client(dev), alarm);
382 static const struct rtc_class_ops isl1208_rtc_ops = {
383 - .proc = isl1208_rtc_proc,
384 - .read_time = isl1208_rtc_read_time,
385 - .set_time = isl1208_rtc_set_time,
386 - .read_alarm = isl1208_rtc_read_alarm,
387 - //.set_alarm = isl1208_rtc_set_alarm,
388 + .proc = isl1208_rtc_proc,
389 + .read_time = isl1208_rtc_read_time,
390 + .set_time = isl1208_rtc_set_time,
391 + .read_alarm = isl1208_rtc_read_alarm,
392 + /*.set_alarm = isl1208_rtc_set_alarm, */
395 /* sysfs interface */
397 -static ssize_t isl1208_sysfs_show_atrim(struct device *dev,
398 - struct device_attribute *attr,
401 +isl1208_sysfs_show_atrim(struct device *dev,
402 + struct device_attribute *attr, char *buf)
406 - atr = isl1208_i2c_get_atr(to_i2c_client(dev));
407 + int atr = isl1208_i2c_get_atr(to_i2c_client(dev));
411 - return sprintf(buf, "%d.%.2d pF\n", atr>>2, (atr&0x3)*25);
412 + 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,
421 +isl1208_sysfs_show_dtrim(struct device *dev,
422 + struct device_attribute *attr, char *buf)
426 - dtr = isl1208_i2c_get_dtr(to_i2c_client(dev));
427 + int dtr = isl1208_i2c_get_dtr(to_i2c_client(dev));
431 return sprintf(buf, "%d ppm\n", dtr);
434 static DEVICE_ATTR(dtrim, S_IRUGO, isl1208_sysfs_show_dtrim, NULL);
436 -static ssize_t isl1208_sysfs_show_usr(struct device *dev,
437 - struct device_attribute *attr,
440 +isl1208_sysfs_show_usr(struct device *dev,
441 + struct device_attribute *attr, char *buf)
445 - usr = isl1208_i2c_get_usr(to_i2c_client(dev));
446 + int usr = isl1208_i2c_get_usr(to_i2c_client(dev));
450 return sprintf(buf, "0x%.4x\n", usr);
453 -static ssize_t isl1208_sysfs_store_usr(struct device *dev,
454 - struct device_attribute *attr,
455 - const char *buf, size_t count)
457 +isl1208_sysfs_store_usr(struct device *dev,
458 + struct device_attribute *attr,
459 + const char *buf, size_t count)
463 @@ -460,124 +451,116 @@ static ssize_t isl1208_sysfs_store_usr(s
465 return isl1208_i2c_set_usr(to_i2c_client(dev), usr) ? -EIO : count;
468 static DEVICE_ATTR(usr, S_IRUGO | S_IWUSR, isl1208_sysfs_show_usr,
469 isl1208_sysfs_store_usr);
472 -isl1208_probe(struct i2c_adapter *adapter, int addr, int kind)
473 +isl1208_sysfs_register(struct device *dev)
476 - struct i2c_client *new_client = NULL;
477 - struct rtc_device *rtc = NULL;
480 + err = device_create_file(dev, &dev_attr_atrim);
484 - if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
487 + err = device_create_file(dev, &dev_attr_dtrim);
489 + device_remove_file(dev, &dev_attr_atrim);
493 - new_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
494 - if (new_client == NULL) {
497 + err = device_create_file(dev, &dev_attr_usr);
499 + device_remove_file(dev, &dev_attr_atrim);
500 + device_remove_file(dev, &dev_attr_dtrim);
503 - new_client->addr = addr;
504 - new_client->adapter = adapter;
505 - new_client->driver = &isl1208_driver;
506 - new_client->flags = 0;
507 - strcpy(new_client->name, DRV_NAME);
512 - rc = isl1208_i2c_validate_client(new_client);
517 +isl1208_sysfs_unregister(struct device *dev)
519 + device_remove_file(dev, &dev_attr_atrim);
520 + device_remove_file(dev, &dev_attr_atrim);
521 + device_remove_file(dev, &dev_attr_usr);
527 +isl1208_probe(struct i2c_client *client)
530 + struct rtc_device *rtc;
532 - rc = i2c_attach_client(new_client);
535 + if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
538 - dev_info(&new_client->dev,
539 + if (isl1208_i2c_validate_client(client) < 0)
542 + dev_info(&client->dev,
543 "chip found, driver version " DRV_VERSION "\n");
545 rtc = rtc_device_register(isl1208_driver.driver.name,
547 - &isl1208_rtc_ops, THIS_MODULE);
551 - goto failout_detach;
553 + &client->dev, &isl1208_rtc_ops,
556 + return PTR_ERR(rtc);
558 - i2c_set_clientdata(new_client, rtc);
559 + i2c_set_clientdata(client, rtc);
561 - rc = isl1208_i2c_get_sr(new_client);
562 + rc = isl1208_i2c_get_sr(client);
564 - dev_err(&new_client->dev, "reading status failed\n");
565 - goto failout_unregister;
566 + dev_err(&client->dev, "reading status failed\n");
567 + goto exit_unregister;
570 if (rc & ISL1208_REG_SR_RTCF)
571 - dev_warn(&new_client->dev, "rtc power failure detected, "
572 + dev_warn(&client->dev, "rtc power failure detected, "
573 "please set clock.\n");
575 - rc = device_create_file(&new_client->dev, &dev_attr_atrim);
577 - goto failout_unregister;
578 - rc = device_create_file(&new_client->dev, &dev_attr_dtrim);
580 - goto failout_atrim;
581 - rc = device_create_file(&new_client->dev, &dev_attr_usr);
583 - goto failout_dtrim;
584 + rc = isl1208_sysfs_register(&client->dev);
586 + goto exit_unregister;
591 - device_remove_file(&new_client->dev, &dev_attr_dtrim);
593 - device_remove_file(&new_client->dev, &dev_attr_atrim);
594 - failout_unregister:
596 rtc_device_unregister(rtc);
598 - i2c_detach_client(new_client);
605 -isl1208_attach_adapter (struct i2c_adapter *adapter)
607 - return i2c_probe(adapter, &addr_data, isl1208_probe);
612 -isl1208_detach_client(struct i2c_client *client)
613 +isl1208_remove(struct i2c_client *client)
616 - struct rtc_device *const rtc = i2c_get_clientdata(client);
619 - rtc_device_unregister(rtc); /* do we need to kfree? */
621 - rc = i2c_detach_client(client);
624 + struct rtc_device *rtc = i2c_get_clientdata(client);
627 + isl1208_sysfs_unregister(&client->dev);
628 + rtc_device_unregister(rtc);
633 -/* module management */
634 +static struct i2c_driver isl1208_driver = {
636 + .name = "rtc-isl1208",
638 + .probe = isl1208_probe,
639 + .remove = isl1208_remove,
642 -static int __init isl1208_init(void)
646 return i2c_add_driver(&isl1208_driver);
649 -static void __exit isl1208_exit(void)
653 i2c_del_driver(&isl1208_driver);