1 /* 2 * Intersil ISL1208 rtc class driver 3 * 4 * Copyright 2005,2006 Hebert Valerio Riedel <hvr@gnu.org> 5 * 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. 10 * 11 */ 12 13 #include <linux/module.h> 14 #include <linux/i2c.h> 15 #include <linux/bcd.h> 16 #include <linux/rtc.h> 17 #include "rtc-core.h" 18 #include <linux/of_irq.h> 19 20 /* Register map */ 21 /* rtc section */ 22 #define ISL1208_REG_SC 0x00 23 #define ISL1208_REG_MN 0x01 24 #define ISL1208_REG_HR 0x02 25 #define ISL1208_REG_HR_MIL (1<<7) /* 24h/12h mode */ 26 #define ISL1208_REG_HR_PM (1<<5) /* PM/AM bit in 12h mode */ 27 #define ISL1208_REG_DT 0x03 28 #define ISL1208_REG_MO 0x04 29 #define ISL1208_REG_YR 0x05 30 #define ISL1208_REG_DW 0x06 31 #define ISL1208_RTC_SECTION_LEN 7 32 33 /* control/status section */ 34 #define ISL1208_REG_SR 0x07 35 #define ISL1208_REG_SR_ARST (1<<7) /* auto reset */ 36 #define ISL1208_REG_SR_XTOSCB (1<<6) /* crystal oscillator */ 37 #define ISL1208_REG_SR_WRTC (1<<4) /* write rtc */ 38 #define ISL1208_REG_SR_EVT (1<<3) /* event */ 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_INT_ALME (1<<6) /* alarm enable */ 44 #define ISL1208_REG_INT_IM (1<<7) /* interrupt/alarm mode */ 45 #define ISL1219_REG_EV 0x09 46 #define ISL1219_REG_EV_EVEN (1<<4) /* event detection enable */ 47 #define ISL1219_REG_EV_EVIENB (1<<7) /* event in pull-up disable */ 48 #define ISL1208_REG_ATR 0x0a 49 #define ISL1208_REG_DTR 0x0b 50 51 /* alarm section */ 52 #define ISL1208_REG_SCA 0x0c 53 #define ISL1208_REG_MNA 0x0d 54 #define ISL1208_REG_HRA 0x0e 55 #define ISL1208_REG_DTA 0x0f 56 #define ISL1208_REG_MOA 0x10 57 #define ISL1208_REG_DWA 0x11 58 #define ISL1208_ALARM_SECTION_LEN 6 59 60 /* user section */ 61 #define ISL1208_REG_USR1 0x12 62 #define ISL1208_REG_USR2 0x13 63 #define ISL1208_USR_SECTION_LEN 2 64 65 /* event section */ 66 #define ISL1219_REG_SCT 0x14 67 #define ISL1219_REG_MNT 0x15 68 #define ISL1219_REG_HRT 0x16 69 #define ISL1219_REG_DTT 0x17 70 #define ISL1219_REG_MOT 0x18 71 #define ISL1219_REG_YRT 0x19 72 #define ISL1219_EVT_SECTION_LEN 6 73 74 static struct i2c_driver isl1208_driver; 75 76 /* ISL1208 various variants */ 77 enum { 78 TYPE_ISL1208 = 0, 79 TYPE_ISL1218, 80 TYPE_ISL1219, 81 }; 82 83 /* block read */ 84 static int 85 isl1208_i2c_read_regs(struct i2c_client *client, u8 reg, u8 buf[], 86 unsigned len) 87 { 88 u8 reg_addr[1] = { reg }; 89 struct i2c_msg msgs[2] = { 90 { 91 .addr = client->addr, 92 .len = sizeof(reg_addr), 93 .buf = reg_addr 94 }, 95 { 96 .addr = client->addr, 97 .flags = I2C_M_RD, 98 .len = len, 99 .buf = buf 100 } 101 }; 102 int ret; 103 104 WARN_ON(reg > ISL1219_REG_YRT); 105 WARN_ON(reg + len > ISL1219_REG_YRT + 1); 106 107 ret = i2c_transfer(client->adapter, msgs, 2); 108 if (ret > 0) 109 ret = 0; 110 return ret; 111 } 112 113 /* block write */ 114 static int 115 isl1208_i2c_set_regs(struct i2c_client *client, u8 reg, u8 const buf[], 116 unsigned len) 117 { 118 u8 i2c_buf[ISL1208_REG_USR2 + 2]; 119 struct i2c_msg msgs[1] = { 120 { 121 .addr = client->addr, 122 .len = len + 1, 123 .buf = i2c_buf 124 } 125 }; 126 int ret; 127 128 WARN_ON(reg > ISL1219_REG_YRT); 129 WARN_ON(reg + len > ISL1219_REG_YRT + 1); 130 131 i2c_buf[0] = reg; 132 memcpy(&i2c_buf[1], &buf[0], len); 133 134 ret = i2c_transfer(client->adapter, msgs, 1); 135 if (ret > 0) 136 ret = 0; 137 return ret; 138 } 139 140 /* simple check to see whether we have a isl1208 */ 141 static int 142 isl1208_i2c_validate_client(struct i2c_client *client) 143 { 144 u8 regs[ISL1208_RTC_SECTION_LEN] = { 0, }; 145 u8 zero_mask[ISL1208_RTC_SECTION_LEN] = { 146 0x80, 0x80, 0x40, 0xc0, 0xe0, 0x00, 0xf8 147 }; 148 int i; 149 int ret; 150 151 ret = isl1208_i2c_read_regs(client, 0, regs, ISL1208_RTC_SECTION_LEN); 152 if (ret < 0) 153 return ret; 154 155 for (i = 0; i < ISL1208_RTC_SECTION_LEN; ++i) { 156 if (regs[i] & zero_mask[i]) /* check if bits are cleared */ 157 return -ENODEV; 158 } 159 160 return 0; 161 } 162 163 static int 164 isl1208_i2c_get_sr(struct i2c_client *client) 165 { 166 return i2c_smbus_read_byte_data(client, ISL1208_REG_SR); 167 } 168 169 static int 170 isl1208_i2c_get_atr(struct i2c_client *client) 171 { 172 int atr = i2c_smbus_read_byte_data(client, ISL1208_REG_ATR); 173 if (atr < 0) 174 return atr; 175 176 /* The 6bit value in the ATR register controls the load 177 * capacitance C_load * in steps of 0.25pF 178 * 179 * bit (1<<5) of the ATR register is inverted 180 * 181 * C_load(ATR=0x20) = 4.50pF 182 * C_load(ATR=0x00) = 12.50pF 183 * C_load(ATR=0x1f) = 20.25pF 184 * 185 */ 186 187 atr &= 0x3f; /* mask out lsb */ 188 atr ^= 1 << 5; /* invert 6th bit */ 189 atr += 2 * 9; /* add offset of 4.5pF; unit[atr] = 0.25pF */ 190 191 return atr; 192 } 193 194 static int 195 isl1208_i2c_get_dtr(struct i2c_client *client) 196 { 197 int dtr = i2c_smbus_read_byte_data(client, ISL1208_REG_DTR); 198 if (dtr < 0) 199 return -EIO; 200 201 /* dtr encodes adjustments of {-60,-40,-20,0,20,40,60} ppm */ 202 dtr = ((dtr & 0x3) * 20) * (dtr & (1 << 2) ? -1 : 1); 203 204 return dtr; 205 } 206 207 static int 208 isl1208_i2c_get_usr(struct i2c_client *client) 209 { 210 u8 buf[ISL1208_USR_SECTION_LEN] = { 0, }; 211 int ret; 212 213 ret = isl1208_i2c_read_regs(client, ISL1208_REG_USR1, buf, 214 ISL1208_USR_SECTION_LEN); 215 if (ret < 0) 216 return ret; 217 218 return (buf[1] << 8) | buf[0]; 219 } 220 221 static int 222 isl1208_i2c_set_usr(struct i2c_client *client, u16 usr) 223 { 224 u8 buf[ISL1208_USR_SECTION_LEN]; 225 226 buf[0] = usr & 0xff; 227 buf[1] = (usr >> 8) & 0xff; 228 229 return isl1208_i2c_set_regs(client, ISL1208_REG_USR1, buf, 230 ISL1208_USR_SECTION_LEN); 231 } 232 233 static int 234 isl1208_rtc_toggle_alarm(struct i2c_client *client, int enable) 235 { 236 int icr = i2c_smbus_read_byte_data(client, ISL1208_REG_INT); 237 238 if (icr < 0) { 239 dev_err(&client->dev, "%s: reading INT failed\n", __func__); 240 return icr; 241 } 242 243 if (enable) 244 icr |= ISL1208_REG_INT_ALME | ISL1208_REG_INT_IM; 245 else 246 icr &= ~(ISL1208_REG_INT_ALME | ISL1208_REG_INT_IM); 247 248 icr = i2c_smbus_write_byte_data(client, ISL1208_REG_INT, icr); 249 if (icr < 0) { 250 dev_err(&client->dev, "%s: writing INT failed\n", __func__); 251 return icr; 252 } 253 254 return 0; 255 } 256 257 static int 258 isl1208_rtc_proc(struct device *dev, struct seq_file *seq) 259 { 260 struct i2c_client *const client = to_i2c_client(dev); 261 int sr, dtr, atr, usr; 262 263 sr = isl1208_i2c_get_sr(client); 264 if (sr < 0) { 265 dev_err(&client->dev, "%s: reading SR failed\n", __func__); 266 return sr; 267 } 268 269 seq_printf(seq, "status_reg\t:%s%s%s%s%s%s (0x%.2x)\n", 270 (sr & ISL1208_REG_SR_RTCF) ? " RTCF" : "", 271 (sr & ISL1208_REG_SR_BAT) ? " BAT" : "", 272 (sr & ISL1208_REG_SR_ALM) ? " ALM" : "", 273 (sr & ISL1208_REG_SR_WRTC) ? " WRTC" : "", 274 (sr & ISL1208_REG_SR_XTOSCB) ? " XTOSCB" : "", 275 (sr & ISL1208_REG_SR_ARST) ? " ARST" : "", sr); 276 277 seq_printf(seq, "batt_status\t: %s\n", 278 (sr & ISL1208_REG_SR_RTCF) ? "bad" : "okay"); 279 280 dtr = isl1208_i2c_get_dtr(client); 281 if (dtr >= 0 - 1) 282 seq_printf(seq, "digital_trim\t: %d ppm\n", dtr); 283 284 atr = isl1208_i2c_get_atr(client); 285 if (atr >= 0) 286 seq_printf(seq, "analog_trim\t: %d.%.2d pF\n", 287 atr >> 2, (atr & 0x3) * 25); 288 289 usr = isl1208_i2c_get_usr(client); 290 if (usr >= 0) 291 seq_printf(seq, "user_data\t: 0x%.4x\n", usr); 292 293 return 0; 294 } 295 296 static int 297 isl1208_i2c_read_time(struct i2c_client *client, struct rtc_time *tm) 298 { 299 int sr; 300 u8 regs[ISL1208_RTC_SECTION_LEN] = { 0, }; 301 302 sr = isl1208_i2c_get_sr(client); 303 if (sr < 0) { 304 dev_err(&client->dev, "%s: reading SR failed\n", __func__); 305 return -EIO; 306 } 307 308 sr = isl1208_i2c_read_regs(client, 0, regs, ISL1208_RTC_SECTION_LEN); 309 if (sr < 0) { 310 dev_err(&client->dev, "%s: reading RTC section failed\n", 311 __func__); 312 return sr; 313 } 314 315 tm->tm_sec = bcd2bin(regs[ISL1208_REG_SC]); 316 tm->tm_min = bcd2bin(regs[ISL1208_REG_MN]); 317 318 /* HR field has a more complex interpretation */ 319 { 320 const u8 _hr = regs[ISL1208_REG_HR]; 321 if (_hr & ISL1208_REG_HR_MIL) /* 24h format */ 322 tm->tm_hour = bcd2bin(_hr & 0x3f); 323 else { 324 /* 12h format */ 325 tm->tm_hour = bcd2bin(_hr & 0x1f); 326 if (_hr & ISL1208_REG_HR_PM) /* PM flag set */ 327 tm->tm_hour += 12; 328 } 329 } 330 331 tm->tm_mday = bcd2bin(regs[ISL1208_REG_DT]); 332 tm->tm_mon = bcd2bin(regs[ISL1208_REG_MO]) - 1; /* rtc starts at 1 */ 333 tm->tm_year = bcd2bin(regs[ISL1208_REG_YR]) + 100; 334 tm->tm_wday = bcd2bin(regs[ISL1208_REG_DW]); 335 336 return 0; 337 } 338 339 static int 340 isl1208_i2c_read_alarm(struct i2c_client *client, struct rtc_wkalrm *alarm) 341 { 342 struct rtc_time *const tm = &alarm->time; 343 u8 regs[ISL1208_ALARM_SECTION_LEN] = { 0, }; 344 int icr, yr, sr = isl1208_i2c_get_sr(client); 345 346 if (sr < 0) { 347 dev_err(&client->dev, "%s: reading SR failed\n", __func__); 348 return sr; 349 } 350 351 sr = isl1208_i2c_read_regs(client, ISL1208_REG_SCA, regs, 352 ISL1208_ALARM_SECTION_LEN); 353 if (sr < 0) { 354 dev_err(&client->dev, "%s: reading alarm section failed\n", 355 __func__); 356 return sr; 357 } 358 359 /* MSB of each alarm register is an enable bit */ 360 tm->tm_sec = bcd2bin(regs[ISL1208_REG_SCA - ISL1208_REG_SCA] & 0x7f); 361 tm->tm_min = bcd2bin(regs[ISL1208_REG_MNA - ISL1208_REG_SCA] & 0x7f); 362 tm->tm_hour = bcd2bin(regs[ISL1208_REG_HRA - ISL1208_REG_SCA] & 0x3f); 363 tm->tm_mday = bcd2bin(regs[ISL1208_REG_DTA - ISL1208_REG_SCA] & 0x3f); 364 tm->tm_mon = 365 bcd2bin(regs[ISL1208_REG_MOA - ISL1208_REG_SCA] & 0x1f) - 1; 366 tm->tm_wday = bcd2bin(regs[ISL1208_REG_DWA - ISL1208_REG_SCA] & 0x03); 367 368 /* The alarm doesn't store the year so get it from the rtc section */ 369 yr = i2c_smbus_read_byte_data(client, ISL1208_REG_YR); 370 if (yr < 0) { 371 dev_err(&client->dev, "%s: reading RTC YR failed\n", __func__); 372 return yr; 373 } 374 tm->tm_year = bcd2bin(yr) + 100; 375 376 icr = i2c_smbus_read_byte_data(client, ISL1208_REG_INT); 377 if (icr < 0) { 378 dev_err(&client->dev, "%s: reading INT failed\n", __func__); 379 return icr; 380 } 381 alarm->enabled = !!(icr & ISL1208_REG_INT_ALME); 382 383 return 0; 384 } 385 386 static int 387 isl1208_i2c_set_alarm(struct i2c_client *client, struct rtc_wkalrm *alarm) 388 { 389 struct rtc_time *alarm_tm = &alarm->time; 390 u8 regs[ISL1208_ALARM_SECTION_LEN] = { 0, }; 391 const int offs = ISL1208_REG_SCA; 392 struct rtc_time rtc_tm; 393 int err, enable; 394 395 err = isl1208_i2c_read_time(client, &rtc_tm); 396 if (err) 397 return err; 398 399 /* If the alarm time is before the current time disable the alarm */ 400 if (!alarm->enabled || rtc_tm_sub(alarm_tm, &rtc_tm) <= 0) 401 enable = 0x00; 402 else 403 enable = 0x80; 404 405 /* Program the alarm and enable it for each setting */ 406 regs[ISL1208_REG_SCA - offs] = bin2bcd(alarm_tm->tm_sec) | enable; 407 regs[ISL1208_REG_MNA - offs] = bin2bcd(alarm_tm->tm_min) | enable; 408 regs[ISL1208_REG_HRA - offs] = bin2bcd(alarm_tm->tm_hour) | 409 ISL1208_REG_HR_MIL | enable; 410 411 regs[ISL1208_REG_DTA - offs] = bin2bcd(alarm_tm->tm_mday) | enable; 412 regs[ISL1208_REG_MOA - offs] = bin2bcd(alarm_tm->tm_mon + 1) | enable; 413 regs[ISL1208_REG_DWA - offs] = bin2bcd(alarm_tm->tm_wday & 7) | enable; 414 415 /* write ALARM registers */ 416 err = isl1208_i2c_set_regs(client, offs, regs, 417 ISL1208_ALARM_SECTION_LEN); 418 if (err < 0) { 419 dev_err(&client->dev, "%s: writing ALARM section failed\n", 420 __func__); 421 return err; 422 } 423 424 err = isl1208_rtc_toggle_alarm(client, enable); 425 if (err) 426 return err; 427 428 return 0; 429 } 430 431 static int 432 isl1208_rtc_read_time(struct device *dev, struct rtc_time *tm) 433 { 434 return isl1208_i2c_read_time(to_i2c_client(dev), tm); 435 } 436 437 static int 438 isl1208_i2c_set_time(struct i2c_client *client, struct rtc_time const *tm) 439 { 440 int sr; 441 u8 regs[ISL1208_RTC_SECTION_LEN] = { 0, }; 442 443 /* The clock has an 8 bit wide bcd-coded register (they never learn) 444 * for the year. tm_year is an offset from 1900 and we are interested 445 * in the 2000-2099 range, so any value less than 100 is invalid. 446 */ 447 if (tm->tm_year < 100) 448 return -EINVAL; 449 450 regs[ISL1208_REG_SC] = bin2bcd(tm->tm_sec); 451 regs[ISL1208_REG_MN] = bin2bcd(tm->tm_min); 452 regs[ISL1208_REG_HR] = bin2bcd(tm->tm_hour) | ISL1208_REG_HR_MIL; 453 454 regs[ISL1208_REG_DT] = bin2bcd(tm->tm_mday); 455 regs[ISL1208_REG_MO] = bin2bcd(tm->tm_mon + 1); 456 regs[ISL1208_REG_YR] = bin2bcd(tm->tm_year - 100); 457 458 regs[ISL1208_REG_DW] = bin2bcd(tm->tm_wday & 7); 459 460 sr = isl1208_i2c_get_sr(client); 461 if (sr < 0) { 462 dev_err(&client->dev, "%s: reading SR failed\n", __func__); 463 return sr; 464 } 465 466 /* set WRTC */ 467 sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR, 468 sr | ISL1208_REG_SR_WRTC); 469 if (sr < 0) { 470 dev_err(&client->dev, "%s: writing SR failed\n", __func__); 471 return sr; 472 } 473 474 /* write RTC registers */ 475 sr = isl1208_i2c_set_regs(client, 0, regs, ISL1208_RTC_SECTION_LEN); 476 if (sr < 0) { 477 dev_err(&client->dev, "%s: writing RTC section failed\n", 478 __func__); 479 return sr; 480 } 481 482 /* clear WRTC again */ 483 sr = isl1208_i2c_get_sr(client); 484 if (sr < 0) { 485 dev_err(&client->dev, "%s: reading SR failed\n", __func__); 486 return sr; 487 } 488 sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR, 489 sr & ~ISL1208_REG_SR_WRTC); 490 if (sr < 0) { 491 dev_err(&client->dev, "%s: writing SR failed\n", __func__); 492 return sr; 493 } 494 495 return 0; 496 } 497 498 499 static int 500 isl1208_rtc_set_time(struct device *dev, struct rtc_time *tm) 501 { 502 return isl1208_i2c_set_time(to_i2c_client(dev), tm); 503 } 504 505 static int 506 isl1208_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm) 507 { 508 return isl1208_i2c_read_alarm(to_i2c_client(dev), alarm); 509 } 510 511 static int 512 isl1208_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm) 513 { 514 return isl1208_i2c_set_alarm(to_i2c_client(dev), alarm); 515 } 516 517 static ssize_t timestamp0_store(struct device *dev, 518 struct device_attribute *attr, 519 const char *buf, size_t count) 520 { 521 struct i2c_client *client = dev_get_drvdata(dev); 522 int sr; 523 524 sr = isl1208_i2c_get_sr(client); 525 if (sr < 0) { 526 dev_err(dev, "%s: reading SR failed\n", __func__); 527 return sr; 528 } 529 530 sr &= ~ISL1208_REG_SR_EVT; 531 532 sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR, sr); 533 if (sr < 0) 534 dev_err(dev, "%s: writing SR failed\n", 535 __func__); 536 537 return count; 538 }; 539 540 static ssize_t timestamp0_show(struct device *dev, 541 struct device_attribute *attr, char *buf) 542 { 543 struct i2c_client *client = dev_get_drvdata(dev); 544 u8 regs[ISL1219_EVT_SECTION_LEN] = { 0, }; 545 struct rtc_time tm; 546 int sr; 547 548 sr = isl1208_i2c_get_sr(client); 549 if (sr < 0) { 550 dev_err(dev, "%s: reading SR failed\n", __func__); 551 return sr; 552 } 553 554 if (!(sr & ISL1208_REG_SR_EVT)) 555 return 0; 556 557 sr = isl1208_i2c_read_regs(client, ISL1219_REG_SCT, regs, 558 ISL1219_EVT_SECTION_LEN); 559 if (sr < 0) { 560 dev_err(dev, "%s: reading event section failed\n", 561 __func__); 562 return 0; 563 } 564 565 /* MSB of each alarm register is an enable bit */ 566 tm.tm_sec = bcd2bin(regs[ISL1219_REG_SCT - ISL1219_REG_SCT] & 0x7f); 567 tm.tm_min = bcd2bin(regs[ISL1219_REG_MNT - ISL1219_REG_SCT] & 0x7f); 568 tm.tm_hour = bcd2bin(regs[ISL1219_REG_HRT - ISL1219_REG_SCT] & 0x3f); 569 tm.tm_mday = bcd2bin(regs[ISL1219_REG_DTT - ISL1219_REG_SCT] & 0x3f); 570 tm.tm_mon = 571 bcd2bin(regs[ISL1219_REG_MOT - ISL1219_REG_SCT] & 0x1f) - 1; 572 tm.tm_year = bcd2bin(regs[ISL1219_REG_YRT - ISL1219_REG_SCT]) + 100; 573 574 sr = rtc_valid_tm(&tm); 575 if (sr) 576 return sr; 577 578 return sprintf(buf, "%llu\n", 579 (unsigned long long)rtc_tm_to_time64(&tm)); 580 }; 581 582 static DEVICE_ATTR_RW(timestamp0); 583 584 static irqreturn_t 585 isl1208_rtc_interrupt(int irq, void *data) 586 { 587 unsigned long timeout = jiffies + msecs_to_jiffies(1000); 588 struct i2c_client *client = data; 589 struct rtc_device *rtc = i2c_get_clientdata(client); 590 int handled = 0, sr, err; 591 592 /* 593 * I2C reads get NAK'ed if we read straight away after an interrupt? 594 * Using a mdelay/msleep didn't seem to help either, so we work around 595 * this by continually trying to read the register for a short time. 596 */ 597 while (1) { 598 sr = isl1208_i2c_get_sr(client); 599 if (sr >= 0) 600 break; 601 602 if (time_after(jiffies, timeout)) { 603 dev_err(&client->dev, "%s: reading SR failed\n", 604 __func__); 605 return sr; 606 } 607 } 608 609 if (sr & ISL1208_REG_SR_ALM) { 610 dev_dbg(&client->dev, "alarm!\n"); 611 612 rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF); 613 614 /* Clear the alarm */ 615 sr &= ~ISL1208_REG_SR_ALM; 616 sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR, sr); 617 if (sr < 0) 618 dev_err(&client->dev, "%s: writing SR failed\n", 619 __func__); 620 else 621 handled = 1; 622 623 /* Disable the alarm */ 624 err = isl1208_rtc_toggle_alarm(client, 0); 625 if (err) 626 return err; 627 } 628 629 if (sr & ISL1208_REG_SR_EVT) { 630 sysfs_notify(&rtc->dev.kobj, NULL, 631 dev_attr_timestamp0.attr.name); 632 dev_warn(&client->dev, "event detected"); 633 handled = 1; 634 } 635 636 return handled ? IRQ_HANDLED : IRQ_NONE; 637 } 638 639 static const struct rtc_class_ops isl1208_rtc_ops = { 640 .proc = isl1208_rtc_proc, 641 .read_time = isl1208_rtc_read_time, 642 .set_time = isl1208_rtc_set_time, 643 .read_alarm = isl1208_rtc_read_alarm, 644 .set_alarm = isl1208_rtc_set_alarm, 645 }; 646 647 /* sysfs interface */ 648 649 static ssize_t 650 isl1208_sysfs_show_atrim(struct device *dev, 651 struct device_attribute *attr, char *buf) 652 { 653 int atr = isl1208_i2c_get_atr(to_i2c_client(dev)); 654 if (atr < 0) 655 return atr; 656 657 return sprintf(buf, "%d.%.2d pF\n", atr >> 2, (atr & 0x3) * 25); 658 } 659 660 static DEVICE_ATTR(atrim, S_IRUGO, isl1208_sysfs_show_atrim, NULL); 661 662 static ssize_t 663 isl1208_sysfs_show_dtrim(struct device *dev, 664 struct device_attribute *attr, char *buf) 665 { 666 int dtr = isl1208_i2c_get_dtr(to_i2c_client(dev)); 667 if (dtr < 0) 668 return dtr; 669 670 return sprintf(buf, "%d ppm\n", dtr); 671 } 672 673 static DEVICE_ATTR(dtrim, S_IRUGO, isl1208_sysfs_show_dtrim, NULL); 674 675 static ssize_t 676 isl1208_sysfs_show_usr(struct device *dev, 677 struct device_attribute *attr, char *buf) 678 { 679 int usr = isl1208_i2c_get_usr(to_i2c_client(dev)); 680 if (usr < 0) 681 return usr; 682 683 return sprintf(buf, "0x%.4x\n", usr); 684 } 685 686 static ssize_t 687 isl1208_sysfs_store_usr(struct device *dev, 688 struct device_attribute *attr, 689 const char *buf, size_t count) 690 { 691 int usr = -1; 692 693 if (buf[0] == '0' && (buf[1] == 'x' || buf[1] == 'X')) { 694 if (sscanf(buf, "%x", &usr) != 1) 695 return -EINVAL; 696 } else { 697 if (sscanf(buf, "%d", &usr) != 1) 698 return -EINVAL; 699 } 700 701 if (usr < 0 || usr > 0xffff) 702 return -EINVAL; 703 704 return isl1208_i2c_set_usr(to_i2c_client(dev), usr) ? -EIO : count; 705 } 706 707 static DEVICE_ATTR(usr, S_IRUGO | S_IWUSR, isl1208_sysfs_show_usr, 708 isl1208_sysfs_store_usr); 709 710 static struct attribute *isl1208_rtc_attrs[] = { 711 &dev_attr_atrim.attr, 712 &dev_attr_dtrim.attr, 713 &dev_attr_usr.attr, 714 NULL 715 }; 716 717 static const struct attribute_group isl1208_rtc_sysfs_files = { 718 .attrs = isl1208_rtc_attrs, 719 }; 720 721 static struct attribute *isl1219_rtc_attrs[] = { 722 &dev_attr_timestamp0.attr, 723 NULL 724 }; 725 726 static const struct attribute_group isl1219_rtc_sysfs_files = { 727 .attrs = isl1219_rtc_attrs, 728 }; 729 730 static int isl1208_setup_irq(struct i2c_client *client, int irq) 731 { 732 int rc = devm_request_threaded_irq(&client->dev, irq, NULL, 733 isl1208_rtc_interrupt, 734 IRQF_SHARED | IRQF_ONESHOT, 735 isl1208_driver.driver.name, 736 client); 737 if (!rc) { 738 device_init_wakeup(&client->dev, 1); 739 enable_irq_wake(irq); 740 } else { 741 dev_err(&client->dev, 742 "Unable to request irq %d, no alarm support\n", 743 irq); 744 } 745 return rc; 746 } 747 748 static int 749 isl1208_probe(struct i2c_client *client, const struct i2c_device_id *id) 750 { 751 int rc = 0; 752 struct rtc_device *rtc; 753 int evdet_irq = -1; 754 755 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) 756 return -ENODEV; 757 758 if (isl1208_i2c_validate_client(client) < 0) 759 return -ENODEV; 760 761 rtc = devm_rtc_allocate_device(&client->dev); 762 if (IS_ERR(rtc)) 763 return PTR_ERR(rtc); 764 765 rtc->ops = &isl1208_rtc_ops; 766 767 i2c_set_clientdata(client, rtc); 768 dev_set_drvdata(&rtc->dev, client); 769 770 rc = isl1208_i2c_get_sr(client); 771 if (rc < 0) { 772 dev_err(&client->dev, "reading status failed\n"); 773 return rc; 774 } 775 776 if (rc & ISL1208_REG_SR_RTCF) 777 dev_warn(&client->dev, "rtc power failure detected, " 778 "please set clock.\n"); 779 780 if (id->driver_data == TYPE_ISL1219) { 781 struct device_node *np = client->dev.of_node; 782 u32 evienb; 783 784 rc = i2c_smbus_read_byte_data(client, ISL1219_REG_EV); 785 if (rc < 0) { 786 dev_err(&client->dev, "failed to read EV reg\n"); 787 return rc; 788 } 789 rc |= ISL1219_REG_EV_EVEN; 790 if (!of_property_read_u32(np, "isil,ev-evienb", &evienb)) { 791 if (evienb) 792 rc |= ISL1219_REG_EV_EVIENB; 793 else 794 rc &= ~ISL1219_REG_EV_EVIENB; 795 } 796 rc = i2c_smbus_write_byte_data(client, ISL1219_REG_EV, rc); 797 if (rc < 0) { 798 dev_err(&client->dev, "could not enable tamper detection\n"); 799 return rc; 800 } 801 rc = rtc_add_group(rtc, &isl1219_rtc_sysfs_files); 802 if (rc) 803 return rc; 804 evdet_irq = of_irq_get_byname(np, "evdet"); 805 } 806 807 rc = sysfs_create_group(&client->dev.kobj, &isl1208_rtc_sysfs_files); 808 if (rc) 809 return rc; 810 811 if (client->irq > 0) 812 rc = isl1208_setup_irq(client, client->irq); 813 if (rc) 814 return rc; 815 816 if (evdet_irq > 0 && evdet_irq != client->irq) 817 rc = isl1208_setup_irq(client, evdet_irq); 818 if (rc) 819 return rc; 820 821 return rtc_register_device(rtc); 822 } 823 824 static int 825 isl1208_remove(struct i2c_client *client) 826 { 827 sysfs_remove_group(&client->dev.kobj, &isl1208_rtc_sysfs_files); 828 829 return 0; 830 } 831 832 static const struct i2c_device_id isl1208_id[] = { 833 { "isl1208", TYPE_ISL1208 }, 834 { "isl1218", TYPE_ISL1218 }, 835 { "isl1219", TYPE_ISL1219 }, 836 { } 837 }; 838 MODULE_DEVICE_TABLE(i2c, isl1208_id); 839 840 static const struct of_device_id isl1208_of_match[] = { 841 { .compatible = "isil,isl1208" }, 842 { .compatible = "isil,isl1218" }, 843 { .compatible = "isil,isl1219" }, 844 { } 845 }; 846 MODULE_DEVICE_TABLE(of, isl1208_of_match); 847 848 static struct i2c_driver isl1208_driver = { 849 .driver = { 850 .name = "rtc-isl1208", 851 .of_match_table = of_match_ptr(isl1208_of_match), 852 }, 853 .probe = isl1208_probe, 854 .remove = isl1208_remove, 855 .id_table = isl1208_id, 856 }; 857 858 module_i2c_driver(isl1208_driver); 859 860 MODULE_AUTHOR("Herbert Valerio Riedel <hvr@gnu.org>"); 861 MODULE_DESCRIPTION("Intersil ISL1208 RTC driver"); 862 MODULE_LICENSE("GPL"); 863