2 drivers/rtc/rtc-isl1208.c | 357 +++++++++++++++++++++-------------------------
3 1 file changed, 170 insertions(+), 187 deletions(-)
5 --- a/drivers/rtc/rtc-isl1208.c
6 +++ b/drivers/rtc/rtc-isl1208.c
11 -#define DRV_NAME "isl1208"
12 -#define DRV_VERSION "0.2"
13 +#define DRV_VERSION "0.3"
17 #define ISL1208_REG_SC 0x00
18 #define ISL1208_REG_MN 0x01
19 #define ISL1208_REG_HR 0x02
20 -#define ISL1208_REG_HR_MIL (1<<7) /* 24h/12h mode */
21 -#define ISL1208_REG_HR_PM (1<<5) /* PM/AM bit in 12h mode */
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_DT 0x03
25 #define ISL1208_REG_MO 0x04
26 #define ISL1208_REG_YR 0x05
29 /* control/status section */
30 #define ISL1208_REG_SR 0x07
31 -#define ISL1208_REG_SR_ARST (1<<7) /* auto reset */
32 -#define ISL1208_REG_SR_XTOSCB (1<<6) /* crystal oscillator */
33 -#define ISL1208_REG_SR_WRTC (1<<4) /* write rtc */
34 -#define ISL1208_REG_SR_ALM (1<<2) /* alarm */
35 -#define ISL1208_REG_SR_BAT (1<<1) /* battery */
36 -#define ISL1208_REG_SR_RTCF (1<<0) /* rtc fail */
37 +#define ISL1208_REG_SR_ARST (1<<7) /* auto reset */
38 +#define ISL1208_REG_SR_XTOSCB (1<<6) /* crystal oscillator */
39 +#define ISL1208_REG_SR_WRTC (1<<4) /* write rtc */
40 +#define ISL1208_REG_SR_ALM (1<<2) /* alarm */
41 +#define ISL1208_REG_SR_BAT (1<<1) /* battery */
42 +#define ISL1208_REG_SR_RTCF (1<<0) /* rtc fail */
43 #define ISL1208_REG_INT 0x08
44 -#define ISL1208_REG_09 0x09 /* reserved */
45 +#define ISL1208_REG_09 0x09 /* reserved */
46 #define ISL1208_REG_ATR 0x0a
47 #define ISL1208_REG_DTR 0x0b
50 #define ISL1208_REG_USR2 0x13
51 #define ISL1208_USR_SECTION_LEN 2
53 -/* i2c configuration */
54 -#define ISL1208_I2C_ADDR 0xde
56 -static const unsigned short normal_i2c[] = {
57 - ISL1208_I2C_ADDR>>1, I2C_CLIENT_END
59 -I2C_CLIENT_INSMOD; /* defines addr_data */
61 -static int isl1208_attach_adapter(struct i2c_adapter *adapter);
62 -static int isl1208_detach_client(struct i2c_client *client);
64 -static struct i2c_driver isl1208_driver = {
68 - .id = I2C_DRIVERID_ISL1208,
69 - .attach_adapter = &isl1208_attach_adapter,
70 - .detach_client = &isl1208_detach_client,
72 +static struct i2c_driver isl1208_driver;
76 isl1208_i2c_read_regs(struct i2c_client *client, u8 reg, u8 buf[],
80 u8 reg_addr[1] = { reg };
81 struct i2c_msg msgs[2] = {
82 - { client->addr, client->flags, sizeof(reg_addr), reg_addr },
83 - { client->addr, client->flags | I2C_M_RD, len, buf }
84 + {client->addr, client->flags, sizeof(reg_addr), reg_addr}
86 + {client->addr, client->flags | I2C_M_RD, len, buf}
91 BUG_ON(reg > ISL1208_REG_USR2);
92 BUG_ON(reg + len > ISL1208_REG_USR2 + 1);
97 isl1208_i2c_set_regs(struct i2c_client *client, u8 reg, u8 const buf[],
101 u8 i2c_buf[ISL1208_REG_USR2 + 2];
102 struct i2c_msg msgs[1] = {
103 - { client->addr, client->flags, len + 1, i2c_buf }
104 + {client->addr, client->flags, len + 1, i2c_buf}
109 BUG_ON(reg > ISL1208_REG_USR2);
110 BUG_ON(reg + len > ISL1208_REG_USR2 + 1);
115 /* simple check to see wether we have a isl1208 */
116 -static int isl1208_i2c_validate_client(struct i2c_client *client)
118 +isl1208_i2c_validate_client(struct i2c_client *client)
120 u8 regs[ISL1208_RTC_SECTION_LEN] = { 0, };
121 u8 zero_mask[ISL1208_RTC_SECTION_LEN] = {
122 @@ -139,24 +120,29 @@
125 for (i = 0; i < ISL1208_RTC_SECTION_LEN; ++i) {
126 - if (regs[i] & zero_mask[i]) /* check if bits are cleared */
127 + if (regs[i] & zero_mask[i]) /* check if bits are cleared */
134 -static int isl1208_i2c_get_sr(struct i2c_client *client)
136 +isl1208_i2c_get_sr(struct i2c_client *client)
138 - return i2c_smbus_read_byte_data(client, ISL1208_REG_SR) == -1 ? -EIO:0;
139 + int sr = i2c_smbus_read_byte_data(client, ISL1208_REG_SR);
146 -static int isl1208_i2c_get_atr(struct i2c_client *client)
148 +isl1208_i2c_get_atr(struct i2c_client *client)
150 int atr = i2c_smbus_read_byte_data(client, ISL1208_REG_ATR);
156 /* The 6bit value in the ATR register controls the load
157 * capacitance C_load * in steps of 0.25pF
158 @@ -169,51 +155,54 @@
162 - atr &= 0x3f; /* mask out lsb */
163 - atr ^= 1<<5; /* invert 6th bit */
164 - atr += 2*9; /* add offset of 4.5pF; unit[atr] = 0.25pF */
165 + atr &= 0x3f; /* mask out lsb */
166 + atr ^= 1 << 5; /* invert 6th bit */
167 + atr += 2 * 9; /* add offset of 4.5pF; unit[atr] = 0.25pF */
172 -static int isl1208_i2c_get_dtr(struct i2c_client *client)
174 +isl1208_i2c_get_dtr(struct i2c_client *client)
176 int dtr = i2c_smbus_read_byte_data(client, ISL1208_REG_DTR);
181 /* dtr encodes adjustments of {-60,-40,-20,0,20,40,60} ppm */
182 - dtr = ((dtr & 0x3) * 20) * (dtr & (1<<2) ? -1 : 1);
183 + dtr = ((dtr & 0x3) * 20) * (dtr & (1 << 2) ? -1 : 1);
188 -static int isl1208_i2c_get_usr(struct i2c_client *client)
190 +isl1208_i2c_get_usr(struct i2c_client *client)
192 u8 buf[ISL1208_USR_SECTION_LEN] = { 0, };
195 - ret = isl1208_i2c_read_regs (client, ISL1208_REG_USR1, buf,
196 - ISL1208_USR_SECTION_LEN);
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 +isl1208_i2c_set_usr(struct i2c_client *client, u16 usr)
209 u8 buf[ISL1208_USR_SECTION_LEN];
212 buf[1] = (usr >> 8) & 0xff;
214 - return isl1208_i2c_set_regs (client, ISL1208_REG_USR1, buf,
215 - ISL1208_USR_SECTION_LEN);
216 + return isl1208_i2c_set_regs(client, ISL1208_REG_USR1, buf,
217 + ISL1208_USR_SECTION_LEN);
220 -static int isl1208_rtc_proc(struct device *dev, struct seq_file *seq)
222 +isl1208_rtc_proc(struct device *dev, struct seq_file *seq)
224 struct i2c_client *const client = to_i2c_client(dev);
225 int sr, dtr, atr, usr;
226 @@ -230,20 +219,19 @@
227 (sr & ISL1208_REG_SR_ALM) ? " ALM" : "",
228 (sr & ISL1208_REG_SR_WRTC) ? " WRTC" : "",
229 (sr & ISL1208_REG_SR_XTOSCB) ? " XTOSCB" : "",
230 - (sr & ISL1208_REG_SR_ARST) ? " ARST" : "",
232 + (sr & ISL1208_REG_SR_ARST) ? " ARST" : "", sr);
234 seq_printf(seq, "batt_status\t: %s\n",
235 (sr & ISL1208_REG_SR_RTCF) ? "bad" : "okay");
237 dtr = isl1208_i2c_get_dtr(client);
240 seq_printf(seq, "digital_trim\t: %d ppm\n", dtr);
242 atr = isl1208_i2c_get_atr(client);
244 seq_printf(seq, "analog_trim\t: %d.%.2d pF\n",
245 - atr>>2, (atr&0x3)*25);
246 + atr >> 2, (atr & 0x3) * 25);
248 usr = isl1208_i2c_get_usr(client);
255 -static int isl1208_i2c_read_time(struct i2c_client *client,
256 - struct rtc_time *tm)
258 +isl1208_i2c_read_time(struct i2c_client *client, struct rtc_time *tm)
261 u8 regs[ISL1208_RTC_SECTION_LEN] = { 0, };
262 @@ -274,27 +261,30 @@
264 tm->tm_sec = BCD2BIN(regs[ISL1208_REG_SC]);
265 tm->tm_min = BCD2BIN(regs[ISL1208_REG_MN]);
266 - { /* HR field has a more complex interpretation */
268 + /* HR field has a more complex interpretation */
270 const u8 _hr = regs[ISL1208_REG_HR];
271 - if (_hr & ISL1208_REG_HR_MIL) /* 24h format */
272 + if (_hr & ISL1208_REG_HR_MIL) /* 24h format */
273 tm->tm_hour = BCD2BIN(_hr & 0x3f);
274 - else { // 12h format
277 tm->tm_hour = BCD2BIN(_hr & 0x1f);
278 - if (_hr & ISL1208_REG_HR_PM) /* PM flag set */
279 + if (_hr & ISL1208_REG_HR_PM) /* PM flag set */
284 tm->tm_mday = BCD2BIN(regs[ISL1208_REG_DT]);
285 - tm->tm_mon = BCD2BIN(regs[ISL1208_REG_MO]) - 1; /* rtc starts at 1 */
286 + tm->tm_mon = BCD2BIN(regs[ISL1208_REG_MO]) - 1; /* rtc starts at 1 */
287 tm->tm_year = BCD2BIN(regs[ISL1208_REG_YR]) + 100;
288 tm->tm_wday = BCD2BIN(regs[ISL1208_REG_DW]);
293 -static int isl1208_i2c_read_alarm(struct i2c_client *client,
294 - struct rtc_wkalrm *alarm)
296 +isl1208_i2c_read_alarm(struct i2c_client *client, struct rtc_wkalrm *alarm)
298 struct rtc_time *const tm = &alarm->time;
299 u8 regs[ISL1208_ALARM_SECTION_LEN] = { 0, };
303 sr = isl1208_i2c_read_regs(client, ISL1208_REG_SCA, regs,
304 - ISL1208_ALARM_SECTION_LEN);
305 + ISL1208_ALARM_SECTION_LEN);
307 dev_err(&client->dev, "%s: reading alarm section failed\n",
309 @@ -315,23 +305,25 @@
312 /* MSB of each alarm register is an enable bit */
313 - tm->tm_sec = BCD2BIN(regs[ISL1208_REG_SCA-ISL1208_REG_SCA] & 0x7f);
314 - tm->tm_min = BCD2BIN(regs[ISL1208_REG_MNA-ISL1208_REG_SCA] & 0x7f);
315 - tm->tm_hour = BCD2BIN(regs[ISL1208_REG_HRA-ISL1208_REG_SCA] & 0x3f);
316 - tm->tm_mday = BCD2BIN(regs[ISL1208_REG_DTA-ISL1208_REG_SCA] & 0x3f);
317 - tm->tm_mon = BCD2BIN(regs[ISL1208_REG_MOA-ISL1208_REG_SCA] & 0x1f)-1;
318 - tm->tm_wday = BCD2BIN(regs[ISL1208_REG_DWA-ISL1208_REG_SCA] & 0x03);
319 + tm->tm_sec = BCD2BIN(regs[ISL1208_REG_SCA - ISL1208_REG_SCA] & 0x7f);
320 + tm->tm_min = BCD2BIN(regs[ISL1208_REG_MNA - ISL1208_REG_SCA] & 0x7f);
321 + tm->tm_hour = BCD2BIN(regs[ISL1208_REG_HRA - ISL1208_REG_SCA] & 0x3f);
322 + tm->tm_mday = BCD2BIN(regs[ISL1208_REG_DTA - ISL1208_REG_SCA] & 0x3f);
324 + BCD2BIN(regs[ISL1208_REG_MOA - ISL1208_REG_SCA] & 0x1f) - 1;
325 + tm->tm_wday = BCD2BIN(regs[ISL1208_REG_DWA - ISL1208_REG_SCA] & 0x03);
330 -static int isl1208_rtc_read_time(struct device *dev, struct rtc_time *tm)
332 +isl1208_rtc_read_time(struct device *dev, struct rtc_time *tm)
334 return isl1208_i2c_read_time(to_i2c_client(dev), tm);
337 -static int isl1208_i2c_set_time(struct i2c_client *client,
338 - struct rtc_time const *tm)
340 +isl1208_i2c_set_time(struct i2c_client *client, struct rtc_time const *tm)
343 u8 regs[ISL1208_RTC_SECTION_LEN] = { 0, };
348 - sr = i2c_smbus_write_byte_data (client, ISL1208_REG_SR,
349 + sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR,
350 sr | ISL1208_REG_SR_WRTC);
352 dev_err(&client->dev, "%s: writing SR failed\n", __func__);
356 /* clear WRTC again */
357 - sr = i2c_smbus_write_byte_data (client, ISL1208_REG_SR,
358 + sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR,
359 sr & ~ISL1208_REG_SR_WRTC);
361 dev_err(&client->dev, "%s: writing SR failed\n", __func__);
362 @@ -380,70 +372,69 @@
366 -static int isl1208_rtc_set_time(struct device *dev, struct rtc_time *tm)
368 +isl1208_rtc_set_time(struct device *dev, struct rtc_time *tm)
370 return isl1208_i2c_set_time(to_i2c_client(dev), tm);
373 -static int isl1208_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
375 +isl1208_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
377 return isl1208_i2c_read_alarm(to_i2c_client(dev), alarm);
380 static const struct rtc_class_ops isl1208_rtc_ops = {
381 - .proc = isl1208_rtc_proc,
382 - .read_time = isl1208_rtc_read_time,
383 - .set_time = isl1208_rtc_set_time,
384 - .read_alarm = isl1208_rtc_read_alarm,
385 - //.set_alarm = isl1208_rtc_set_alarm,
386 + .proc = isl1208_rtc_proc,
387 + .read_time = isl1208_rtc_read_time,
388 + .set_time = isl1208_rtc_set_time,
389 + .read_alarm = isl1208_rtc_read_alarm,
390 + /*.set_alarm = isl1208_rtc_set_alarm, */
393 /* sysfs interface */
395 -static ssize_t isl1208_sysfs_show_atrim(struct device *dev,
396 - struct device_attribute *attr,
399 +isl1208_sysfs_show_atrim(struct device *dev,
400 + struct device_attribute *attr, char *buf)
404 - atr = isl1208_i2c_get_atr(to_i2c_client(dev));
405 + int atr = isl1208_i2c_get_atr(to_i2c_client(dev));
409 - return sprintf(buf, "%d.%.2d pF\n", atr>>2, (atr&0x3)*25);
410 + return sprintf(buf, "%d.%.2d pF\n", atr >> 2, (atr & 0x3) * 25);
413 static DEVICE_ATTR(atrim, S_IRUGO, isl1208_sysfs_show_atrim, NULL);
415 -static ssize_t isl1208_sysfs_show_dtrim(struct device *dev,
416 - struct device_attribute *attr,
419 +isl1208_sysfs_show_dtrim(struct device *dev,
420 + struct device_attribute *attr, char *buf)
424 - dtr = isl1208_i2c_get_dtr(to_i2c_client(dev));
425 + int dtr = isl1208_i2c_get_dtr(to_i2c_client(dev));
429 return sprintf(buf, "%d ppm\n", dtr);
432 static DEVICE_ATTR(dtrim, S_IRUGO, isl1208_sysfs_show_dtrim, NULL);
434 -static ssize_t isl1208_sysfs_show_usr(struct device *dev,
435 - struct device_attribute *attr,
438 +isl1208_sysfs_show_usr(struct device *dev,
439 + struct device_attribute *attr, char *buf)
443 - usr = isl1208_i2c_get_usr(to_i2c_client(dev));
444 + int usr = isl1208_i2c_get_usr(to_i2c_client(dev));
448 return sprintf(buf, "0x%.4x\n", usr);
451 -static ssize_t isl1208_sysfs_store_usr(struct device *dev,
452 - struct device_attribute *attr,
453 - const char *buf, size_t count)
455 +isl1208_sysfs_store_usr(struct device *dev,
456 + struct device_attribute *attr,
457 + const char *buf, size_t count)
461 @@ -460,124 +451,116 @@
463 return isl1208_i2c_set_usr(to_i2c_client(dev), usr) ? -EIO : count;
466 static DEVICE_ATTR(usr, S_IRUGO | S_IWUSR, isl1208_sysfs_show_usr,
467 isl1208_sysfs_store_usr);
470 -isl1208_probe(struct i2c_adapter *adapter, int addr, int kind)
471 +isl1208_sysfs_register(struct device *dev)
474 - struct i2c_client *new_client = NULL;
475 - struct rtc_device *rtc = NULL;
478 + err = device_create_file(dev, &dev_attr_atrim);
482 - if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
485 + err = device_create_file(dev, &dev_attr_dtrim);
487 + device_remove_file(dev, &dev_attr_atrim);
491 - new_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
492 - if (new_client == NULL) {
495 + err = device_create_file(dev, &dev_attr_usr);
497 + device_remove_file(dev, &dev_attr_atrim);
498 + device_remove_file(dev, &dev_attr_dtrim);
501 - new_client->addr = addr;
502 - new_client->adapter = adapter;
503 - new_client->driver = &isl1208_driver;
504 - new_client->flags = 0;
505 - strcpy(new_client->name, DRV_NAME);
510 - rc = isl1208_i2c_validate_client(new_client);
515 +isl1208_sysfs_unregister(struct device *dev)
517 + device_remove_file(dev, &dev_attr_atrim);
518 + device_remove_file(dev, &dev_attr_atrim);
519 + device_remove_file(dev, &dev_attr_usr);
525 +isl1208_probe(struct i2c_client *client)
528 + struct rtc_device *rtc;
530 - rc = i2c_attach_client(new_client);
533 + if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
536 - dev_info(&new_client->dev,
537 + if (isl1208_i2c_validate_client(client) < 0)
540 + dev_info(&client->dev,
541 "chip found, driver version " DRV_VERSION "\n");
543 rtc = rtc_device_register(isl1208_driver.driver.name,
545 - &isl1208_rtc_ops, THIS_MODULE);
549 - goto failout_detach;
551 + &client->dev, &isl1208_rtc_ops,
554 + return PTR_ERR(rtc);
556 - i2c_set_clientdata(new_client, rtc);
557 + i2c_set_clientdata(client, rtc);
559 - rc = isl1208_i2c_get_sr(new_client);
560 + rc = isl1208_i2c_get_sr(client);
562 - dev_err(&new_client->dev, "reading status failed\n");
563 - goto failout_unregister;
564 + dev_err(&client->dev, "reading status failed\n");
565 + goto exit_unregister;
568 if (rc & ISL1208_REG_SR_RTCF)
569 - dev_warn(&new_client->dev, "rtc power failure detected, "
570 + dev_warn(&client->dev, "rtc power failure detected, "
571 "please set clock.\n");
573 - rc = device_create_file(&new_client->dev, &dev_attr_atrim);
575 - goto failout_unregister;
576 - rc = device_create_file(&new_client->dev, &dev_attr_dtrim);
578 - goto failout_atrim;
579 - rc = device_create_file(&new_client->dev, &dev_attr_usr);
581 - goto failout_dtrim;
582 + rc = isl1208_sysfs_register(&client->dev);
584 + goto exit_unregister;
589 - device_remove_file(&new_client->dev, &dev_attr_dtrim);
591 - device_remove_file(&new_client->dev, &dev_attr_atrim);
592 - failout_unregister:
594 rtc_device_unregister(rtc);
596 - i2c_detach_client(new_client);
603 -isl1208_attach_adapter (struct i2c_adapter *adapter)
605 - return i2c_probe(adapter, &addr_data, isl1208_probe);
610 -isl1208_detach_client(struct i2c_client *client)
611 +isl1208_remove(struct i2c_client *client)
614 - struct rtc_device *const rtc = i2c_get_clientdata(client);
617 - rtc_device_unregister(rtc); /* do we need to kfree? */
619 - rc = i2c_detach_client(client);
622 + struct rtc_device *rtc = i2c_get_clientdata(client);
625 + isl1208_sysfs_unregister(&client->dev);
626 + rtc_device_unregister(rtc);
631 -/* module management */
632 +static struct i2c_driver isl1208_driver = {
634 + .name = "rtc-isl1208",
636 + .probe = isl1208_probe,
637 + .remove = isl1208_remove,
640 -static int __init isl1208_init(void)
644 return i2c_add_driver(&isl1208_driver);
647 -static void __exit isl1208_exit(void)
651 i2c_del_driver(&isl1208_driver);