1 /* 2 * A driver for the I2C members of the Abracon AB x8xx RTC family, 3 * and compatible: AB 1805 and AB 0805 4 * 5 * Copyright 2014-2015 Macq S.A. 6 * 7 * Author: Philippe De Muyter <phdm@macqel.be> 8 * Author: Alexandre Belloni <alexandre.belloni@free-electrons.com> 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License version 2 as 12 * published by the Free Software Foundation. 13 * 14 */ 15 16 #include <linux/bcd.h> 17 #include <linux/i2c.h> 18 #include <linux/module.h> 19 #include <linux/rtc.h> 20 #include <linux/watchdog.h> 21 22 #define ABX8XX_REG_HTH 0x00 23 #define ABX8XX_REG_SC 0x01 24 #define ABX8XX_REG_MN 0x02 25 #define ABX8XX_REG_HR 0x03 26 #define ABX8XX_REG_DA 0x04 27 #define ABX8XX_REG_MO 0x05 28 #define ABX8XX_REG_YR 0x06 29 #define ABX8XX_REG_WD 0x07 30 31 #define ABX8XX_REG_AHTH 0x08 32 #define ABX8XX_REG_ASC 0x09 33 #define ABX8XX_REG_AMN 0x0a 34 #define ABX8XX_REG_AHR 0x0b 35 #define ABX8XX_REG_ADA 0x0c 36 #define ABX8XX_REG_AMO 0x0d 37 #define ABX8XX_REG_AWD 0x0e 38 39 #define ABX8XX_REG_STATUS 0x0f 40 #define ABX8XX_STATUS_AF BIT(2) 41 #define ABX8XX_STATUS_WDT BIT(6) 42 43 #define ABX8XX_REG_CTRL1 0x10 44 #define ABX8XX_CTRL_WRITE BIT(0) 45 #define ABX8XX_CTRL_ARST BIT(2) 46 #define ABX8XX_CTRL_12_24 BIT(6) 47 48 #define ABX8XX_REG_IRQ 0x12 49 #define ABX8XX_IRQ_AIE BIT(2) 50 #define ABX8XX_IRQ_IM_1_4 (0x3 << 5) 51 52 #define ABX8XX_REG_CD_TIMER_CTL 0x18 53 54 #define ABX8XX_REG_OSC 0x1c 55 #define ABX8XX_OSC_FOS BIT(3) 56 #define ABX8XX_OSC_BOS BIT(4) 57 #define ABX8XX_OSC_ACAL_512 BIT(5) 58 #define ABX8XX_OSC_ACAL_1024 BIT(6) 59 60 #define ABX8XX_OSC_OSEL BIT(7) 61 62 #define ABX8XX_REG_OSS 0x1d 63 #define ABX8XX_OSS_OF BIT(1) 64 #define ABX8XX_OSS_OMODE BIT(4) 65 66 #define ABX8XX_REG_WDT 0x1b 67 #define ABX8XX_WDT_WDS BIT(7) 68 #define ABX8XX_WDT_BMB_MASK 0x7c 69 #define ABX8XX_WDT_BMB_SHIFT 2 70 #define ABX8XX_WDT_MAX_TIME (ABX8XX_WDT_BMB_MASK >> ABX8XX_WDT_BMB_SHIFT) 71 #define ABX8XX_WDT_WRB_MASK 0x03 72 #define ABX8XX_WDT_WRB_1HZ 0x02 73 74 #define ABX8XX_REG_CFG_KEY 0x1f 75 #define ABX8XX_CFG_KEY_OSC 0xa1 76 #define ABX8XX_CFG_KEY_MISC 0x9d 77 78 #define ABX8XX_REG_ID0 0x28 79 80 #define ABX8XX_REG_TRICKLE 0x20 81 #define ABX8XX_TRICKLE_CHARGE_ENABLE 0xa0 82 #define ABX8XX_TRICKLE_STANDARD_DIODE 0x8 83 #define ABX8XX_TRICKLE_SCHOTTKY_DIODE 0x4 84 85 static u8 trickle_resistors[] = {0, 3, 6, 11}; 86 87 enum abx80x_chip {AB0801, AB0803, AB0804, AB0805, 88 AB1801, AB1803, AB1804, AB1805, ABX80X}; 89 90 struct abx80x_cap { 91 u16 pn; 92 bool has_tc; 93 bool has_wdog; 94 }; 95 96 static struct abx80x_cap abx80x_caps[] = { 97 [AB0801] = {.pn = 0x0801}, 98 [AB0803] = {.pn = 0x0803}, 99 [AB0804] = {.pn = 0x0804, .has_tc = true, .has_wdog = true}, 100 [AB0805] = {.pn = 0x0805, .has_tc = true, .has_wdog = true}, 101 [AB1801] = {.pn = 0x1801}, 102 [AB1803] = {.pn = 0x1803}, 103 [AB1804] = {.pn = 0x1804, .has_tc = true, .has_wdog = true}, 104 [AB1805] = {.pn = 0x1805, .has_tc = true, .has_wdog = true}, 105 [ABX80X] = {.pn = 0} 106 }; 107 108 struct abx80x_priv { 109 struct rtc_device *rtc; 110 struct i2c_client *client; 111 struct watchdog_device wdog; 112 }; 113 114 static int abx80x_is_rc_mode(struct i2c_client *client) 115 { 116 int flags = 0; 117 118 flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS); 119 if (flags < 0) { 120 dev_err(&client->dev, 121 "Failed to read autocalibration attribute\n"); 122 return flags; 123 } 124 125 return (flags & ABX8XX_OSS_OMODE) ? 1 : 0; 126 } 127 128 static int abx80x_enable_trickle_charger(struct i2c_client *client, 129 u8 trickle_cfg) 130 { 131 int err; 132 133 /* 134 * Write the configuration key register to enable access to the Trickle 135 * register 136 */ 137 err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY, 138 ABX8XX_CFG_KEY_MISC); 139 if (err < 0) { 140 dev_err(&client->dev, "Unable to write configuration key\n"); 141 return -EIO; 142 } 143 144 err = i2c_smbus_write_byte_data(client, ABX8XX_REG_TRICKLE, 145 ABX8XX_TRICKLE_CHARGE_ENABLE | 146 trickle_cfg); 147 if (err < 0) { 148 dev_err(&client->dev, "Unable to write trickle register\n"); 149 return -EIO; 150 } 151 152 return 0; 153 } 154 155 static int abx80x_rtc_read_time(struct device *dev, struct rtc_time *tm) 156 { 157 struct i2c_client *client = to_i2c_client(dev); 158 unsigned char buf[8]; 159 int err, flags, rc_mode = 0; 160 161 /* Read the Oscillator Failure only in XT mode */ 162 rc_mode = abx80x_is_rc_mode(client); 163 if (rc_mode < 0) 164 return rc_mode; 165 166 if (!rc_mode) { 167 flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS); 168 if (flags < 0) 169 return flags; 170 171 if (flags & ABX8XX_OSS_OF) { 172 dev_err(dev, "Oscillator failure, data is invalid.\n"); 173 return -EINVAL; 174 } 175 } 176 177 err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_HTH, 178 sizeof(buf), buf); 179 if (err < 0) { 180 dev_err(&client->dev, "Unable to read date\n"); 181 return -EIO; 182 } 183 184 tm->tm_sec = bcd2bin(buf[ABX8XX_REG_SC] & 0x7F); 185 tm->tm_min = bcd2bin(buf[ABX8XX_REG_MN] & 0x7F); 186 tm->tm_hour = bcd2bin(buf[ABX8XX_REG_HR] & 0x3F); 187 tm->tm_wday = buf[ABX8XX_REG_WD] & 0x7; 188 tm->tm_mday = bcd2bin(buf[ABX8XX_REG_DA] & 0x3F); 189 tm->tm_mon = bcd2bin(buf[ABX8XX_REG_MO] & 0x1F) - 1; 190 tm->tm_year = bcd2bin(buf[ABX8XX_REG_YR]) + 100; 191 192 return 0; 193 } 194 195 static int abx80x_rtc_set_time(struct device *dev, struct rtc_time *tm) 196 { 197 struct i2c_client *client = to_i2c_client(dev); 198 unsigned char buf[8]; 199 int err, flags; 200 201 if (tm->tm_year < 100) 202 return -EINVAL; 203 204 buf[ABX8XX_REG_HTH] = 0; 205 buf[ABX8XX_REG_SC] = bin2bcd(tm->tm_sec); 206 buf[ABX8XX_REG_MN] = bin2bcd(tm->tm_min); 207 buf[ABX8XX_REG_HR] = bin2bcd(tm->tm_hour); 208 buf[ABX8XX_REG_DA] = bin2bcd(tm->tm_mday); 209 buf[ABX8XX_REG_MO] = bin2bcd(tm->tm_mon + 1); 210 buf[ABX8XX_REG_YR] = bin2bcd(tm->tm_year - 100); 211 buf[ABX8XX_REG_WD] = tm->tm_wday; 212 213 err = i2c_smbus_write_i2c_block_data(client, ABX8XX_REG_HTH, 214 sizeof(buf), buf); 215 if (err < 0) { 216 dev_err(&client->dev, "Unable to write to date registers\n"); 217 return -EIO; 218 } 219 220 /* Clear the OF bit of Oscillator Status Register */ 221 flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS); 222 if (flags < 0) 223 return flags; 224 225 err = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSS, 226 flags & ~ABX8XX_OSS_OF); 227 if (err < 0) { 228 dev_err(&client->dev, "Unable to write oscillator status register\n"); 229 return err; 230 } 231 232 return 0; 233 } 234 235 static irqreturn_t abx80x_handle_irq(int irq, void *dev_id) 236 { 237 struct i2c_client *client = dev_id; 238 struct abx80x_priv *priv = i2c_get_clientdata(client); 239 struct rtc_device *rtc = priv->rtc; 240 int status; 241 242 status = i2c_smbus_read_byte_data(client, ABX8XX_REG_STATUS); 243 if (status < 0) 244 return IRQ_NONE; 245 246 if (status & ABX8XX_STATUS_AF) 247 rtc_update_irq(rtc, 1, RTC_AF | RTC_IRQF); 248 249 /* 250 * It is unclear if we'll get an interrupt before the external 251 * reset kicks in. 252 */ 253 if (status & ABX8XX_STATUS_WDT) 254 dev_alert(&client->dev, "watchdog timeout interrupt.\n"); 255 256 i2c_smbus_write_byte_data(client, ABX8XX_REG_STATUS, 0); 257 258 return IRQ_HANDLED; 259 } 260 261 static int abx80x_read_alarm(struct device *dev, struct rtc_wkalrm *t) 262 { 263 struct i2c_client *client = to_i2c_client(dev); 264 unsigned char buf[7]; 265 266 int irq_mask, err; 267 268 if (client->irq <= 0) 269 return -EINVAL; 270 271 err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_ASC, 272 sizeof(buf), buf); 273 if (err) 274 return err; 275 276 irq_mask = i2c_smbus_read_byte_data(client, ABX8XX_REG_IRQ); 277 if (irq_mask < 0) 278 return irq_mask; 279 280 t->time.tm_sec = bcd2bin(buf[0] & 0x7F); 281 t->time.tm_min = bcd2bin(buf[1] & 0x7F); 282 t->time.tm_hour = bcd2bin(buf[2] & 0x3F); 283 t->time.tm_mday = bcd2bin(buf[3] & 0x3F); 284 t->time.tm_mon = bcd2bin(buf[4] & 0x1F) - 1; 285 t->time.tm_wday = buf[5] & 0x7; 286 287 t->enabled = !!(irq_mask & ABX8XX_IRQ_AIE); 288 t->pending = (buf[6] & ABX8XX_STATUS_AF) && t->enabled; 289 290 return err; 291 } 292 293 static int abx80x_set_alarm(struct device *dev, struct rtc_wkalrm *t) 294 { 295 struct i2c_client *client = to_i2c_client(dev); 296 u8 alarm[6]; 297 int err; 298 299 if (client->irq <= 0) 300 return -EINVAL; 301 302 alarm[0] = 0x0; 303 alarm[1] = bin2bcd(t->time.tm_sec); 304 alarm[2] = bin2bcd(t->time.tm_min); 305 alarm[3] = bin2bcd(t->time.tm_hour); 306 alarm[4] = bin2bcd(t->time.tm_mday); 307 alarm[5] = bin2bcd(t->time.tm_mon + 1); 308 309 err = i2c_smbus_write_i2c_block_data(client, ABX8XX_REG_AHTH, 310 sizeof(alarm), alarm); 311 if (err < 0) { 312 dev_err(&client->dev, "Unable to write alarm registers\n"); 313 return -EIO; 314 } 315 316 if (t->enabled) { 317 err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ, 318 (ABX8XX_IRQ_IM_1_4 | 319 ABX8XX_IRQ_AIE)); 320 if (err) 321 return err; 322 } 323 324 return 0; 325 } 326 327 static int abx80x_rtc_set_autocalibration(struct device *dev, 328 int autocalibration) 329 { 330 struct i2c_client *client = to_i2c_client(dev); 331 int retval, flags = 0; 332 333 if ((autocalibration != 0) && (autocalibration != 1024) && 334 (autocalibration != 512)) { 335 dev_err(dev, "autocalibration value outside permitted range\n"); 336 return -EINVAL; 337 } 338 339 flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC); 340 if (flags < 0) 341 return flags; 342 343 if (autocalibration == 0) { 344 flags &= ~(ABX8XX_OSC_ACAL_512 | ABX8XX_OSC_ACAL_1024); 345 } else if (autocalibration == 1024) { 346 /* 1024 autocalibration is 0x10 */ 347 flags |= ABX8XX_OSC_ACAL_1024; 348 flags &= ~(ABX8XX_OSC_ACAL_512); 349 } else { 350 /* 512 autocalibration is 0x11 */ 351 flags |= (ABX8XX_OSC_ACAL_1024 | ABX8XX_OSC_ACAL_512); 352 } 353 354 /* Unlock write access to Oscillator Control Register */ 355 retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY, 356 ABX8XX_CFG_KEY_OSC); 357 if (retval < 0) { 358 dev_err(dev, "Failed to write CONFIG_KEY register\n"); 359 return retval; 360 } 361 362 retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSC, flags); 363 364 return retval; 365 } 366 367 static int abx80x_rtc_get_autocalibration(struct device *dev) 368 { 369 struct i2c_client *client = to_i2c_client(dev); 370 int flags = 0, autocalibration; 371 372 flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC); 373 if (flags < 0) 374 return flags; 375 376 if (flags & ABX8XX_OSC_ACAL_512) 377 autocalibration = 512; 378 else if (flags & ABX8XX_OSC_ACAL_1024) 379 autocalibration = 1024; 380 else 381 autocalibration = 0; 382 383 return autocalibration; 384 } 385 386 static ssize_t autocalibration_store(struct device *dev, 387 struct device_attribute *attr, 388 const char *buf, size_t count) 389 { 390 int retval; 391 unsigned long autocalibration = 0; 392 393 retval = kstrtoul(buf, 10, &autocalibration); 394 if (retval < 0) { 395 dev_err(dev, "Failed to store RTC autocalibration attribute\n"); 396 return -EINVAL; 397 } 398 399 retval = abx80x_rtc_set_autocalibration(dev, autocalibration); 400 401 return retval ? retval : count; 402 } 403 404 static ssize_t autocalibration_show(struct device *dev, 405 struct device_attribute *attr, char *buf) 406 { 407 int autocalibration = 0; 408 409 autocalibration = abx80x_rtc_get_autocalibration(dev); 410 if (autocalibration < 0) { 411 dev_err(dev, "Failed to read RTC autocalibration\n"); 412 sprintf(buf, "0\n"); 413 return autocalibration; 414 } 415 416 return sprintf(buf, "%d\n", autocalibration); 417 } 418 419 static DEVICE_ATTR_RW(autocalibration); 420 421 static ssize_t oscillator_store(struct device *dev, 422 struct device_attribute *attr, 423 const char *buf, size_t count) 424 { 425 struct i2c_client *client = to_i2c_client(dev); 426 int retval, flags, rc_mode = 0; 427 428 if (strncmp(buf, "rc", 2) == 0) { 429 rc_mode = 1; 430 } else if (strncmp(buf, "xtal", 4) == 0) { 431 rc_mode = 0; 432 } else { 433 dev_err(dev, "Oscillator selection value outside permitted ones\n"); 434 return -EINVAL; 435 } 436 437 flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC); 438 if (flags < 0) 439 return flags; 440 441 if (rc_mode == 0) 442 flags &= ~(ABX8XX_OSC_OSEL); 443 else 444 flags |= (ABX8XX_OSC_OSEL); 445 446 /* Unlock write access on Oscillator Control register */ 447 retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY, 448 ABX8XX_CFG_KEY_OSC); 449 if (retval < 0) { 450 dev_err(dev, "Failed to write CONFIG_KEY register\n"); 451 return retval; 452 } 453 454 retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSC, flags); 455 if (retval < 0) { 456 dev_err(dev, "Failed to write Oscillator Control register\n"); 457 return retval; 458 } 459 460 return retval ? retval : count; 461 } 462 463 static ssize_t oscillator_show(struct device *dev, 464 struct device_attribute *attr, char *buf) 465 { 466 int rc_mode = 0; 467 struct i2c_client *client = to_i2c_client(dev); 468 469 rc_mode = abx80x_is_rc_mode(client); 470 471 if (rc_mode < 0) { 472 dev_err(dev, "Failed to read RTC oscillator selection\n"); 473 sprintf(buf, "\n"); 474 return rc_mode; 475 } 476 477 if (rc_mode) 478 return sprintf(buf, "rc\n"); 479 else 480 return sprintf(buf, "xtal\n"); 481 } 482 483 static DEVICE_ATTR_RW(oscillator); 484 485 static struct attribute *rtc_calib_attrs[] = { 486 &dev_attr_autocalibration.attr, 487 &dev_attr_oscillator.attr, 488 NULL, 489 }; 490 491 static const struct attribute_group rtc_calib_attr_group = { 492 .attrs = rtc_calib_attrs, 493 }; 494 495 static int abx80x_alarm_irq_enable(struct device *dev, unsigned int enabled) 496 { 497 struct i2c_client *client = to_i2c_client(dev); 498 int err; 499 500 if (enabled) 501 err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ, 502 (ABX8XX_IRQ_IM_1_4 | 503 ABX8XX_IRQ_AIE)); 504 else 505 err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ, 506 ABX8XX_IRQ_IM_1_4); 507 return err; 508 } 509 510 static const struct rtc_class_ops abx80x_rtc_ops = { 511 .read_time = abx80x_rtc_read_time, 512 .set_time = abx80x_rtc_set_time, 513 .read_alarm = abx80x_read_alarm, 514 .set_alarm = abx80x_set_alarm, 515 .alarm_irq_enable = abx80x_alarm_irq_enable, 516 }; 517 518 static int abx80x_dt_trickle_cfg(struct device_node *np) 519 { 520 const char *diode; 521 int trickle_cfg = 0; 522 int i, ret; 523 u32 tmp; 524 525 ret = of_property_read_string(np, "abracon,tc-diode", &diode); 526 if (ret) 527 return ret; 528 529 if (!strcmp(diode, "standard")) 530 trickle_cfg |= ABX8XX_TRICKLE_STANDARD_DIODE; 531 else if (!strcmp(diode, "schottky")) 532 trickle_cfg |= ABX8XX_TRICKLE_SCHOTTKY_DIODE; 533 else 534 return -EINVAL; 535 536 ret = of_property_read_u32(np, "abracon,tc-resistor", &tmp); 537 if (ret) 538 return ret; 539 540 for (i = 0; i < sizeof(trickle_resistors); i++) 541 if (trickle_resistors[i] == tmp) 542 break; 543 544 if (i == sizeof(trickle_resistors)) 545 return -EINVAL; 546 547 return (trickle_cfg | i); 548 } 549 550 static void rtc_calib_remove_sysfs_group(void *_dev) 551 { 552 struct device *dev = _dev; 553 554 sysfs_remove_group(&dev->kobj, &rtc_calib_attr_group); 555 } 556 557 #ifdef CONFIG_WATCHDOG 558 559 static inline u8 timeout_bits(unsigned int timeout) 560 { 561 return ((timeout << ABX8XX_WDT_BMB_SHIFT) & ABX8XX_WDT_BMB_MASK) | 562 ABX8XX_WDT_WRB_1HZ; 563 } 564 565 static int __abx80x_wdog_set_timeout(struct watchdog_device *wdog, 566 unsigned int timeout) 567 { 568 struct abx80x_priv *priv = watchdog_get_drvdata(wdog); 569 u8 val = ABX8XX_WDT_WDS | timeout_bits(timeout); 570 571 /* 572 * Writing any timeout to the WDT register resets the watchdog timer. 573 * Writing 0 disables it. 574 */ 575 return i2c_smbus_write_byte_data(priv->client, ABX8XX_REG_WDT, val); 576 } 577 578 static int abx80x_wdog_set_timeout(struct watchdog_device *wdog, 579 unsigned int new_timeout) 580 { 581 int err = 0; 582 583 if (watchdog_hw_running(wdog)) 584 err = __abx80x_wdog_set_timeout(wdog, new_timeout); 585 586 if (err == 0) 587 wdog->timeout = new_timeout; 588 589 return err; 590 } 591 592 static int abx80x_wdog_ping(struct watchdog_device *wdog) 593 { 594 return __abx80x_wdog_set_timeout(wdog, wdog->timeout); 595 } 596 597 static int abx80x_wdog_start(struct watchdog_device *wdog) 598 { 599 return __abx80x_wdog_set_timeout(wdog, wdog->timeout); 600 } 601 602 static int abx80x_wdog_stop(struct watchdog_device *wdog) 603 { 604 return __abx80x_wdog_set_timeout(wdog, 0); 605 } 606 607 static const struct watchdog_info abx80x_wdog_info = { 608 .identity = "abx80x watchdog", 609 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE, 610 }; 611 612 static const struct watchdog_ops abx80x_wdog_ops = { 613 .owner = THIS_MODULE, 614 .start = abx80x_wdog_start, 615 .stop = abx80x_wdog_stop, 616 .ping = abx80x_wdog_ping, 617 .set_timeout = abx80x_wdog_set_timeout, 618 }; 619 620 static int abx80x_setup_watchdog(struct abx80x_priv *priv) 621 { 622 priv->wdog.parent = &priv->client->dev; 623 priv->wdog.ops = &abx80x_wdog_ops; 624 priv->wdog.info = &abx80x_wdog_info; 625 priv->wdog.min_timeout = 1; 626 priv->wdog.max_timeout = ABX8XX_WDT_MAX_TIME; 627 priv->wdog.timeout = ABX8XX_WDT_MAX_TIME; 628 629 watchdog_set_drvdata(&priv->wdog, priv); 630 631 return devm_watchdog_register_device(&priv->client->dev, &priv->wdog); 632 } 633 #else 634 static int abx80x_setup_watchdog(struct abx80x_priv *priv) 635 { 636 return 0; 637 } 638 #endif 639 640 static int abx80x_probe(struct i2c_client *client, 641 const struct i2c_device_id *id) 642 { 643 struct device_node *np = client->dev.of_node; 644 struct abx80x_priv *priv; 645 int i, data, err, trickle_cfg = -EINVAL; 646 char buf[7]; 647 unsigned int part = id->driver_data; 648 unsigned int partnumber; 649 unsigned int majrev, minrev; 650 unsigned int lot; 651 unsigned int wafer; 652 unsigned int uid; 653 654 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) 655 return -ENODEV; 656 657 err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_ID0, 658 sizeof(buf), buf); 659 if (err < 0) { 660 dev_err(&client->dev, "Unable to read partnumber\n"); 661 return -EIO; 662 } 663 664 partnumber = (buf[0] << 8) | buf[1]; 665 majrev = buf[2] >> 3; 666 minrev = buf[2] & 0x7; 667 lot = ((buf[4] & 0x80) << 2) | ((buf[6] & 0x80) << 1) | buf[3]; 668 uid = ((buf[4] & 0x7f) << 8) | buf[5]; 669 wafer = (buf[6] & 0x7c) >> 2; 670 dev_info(&client->dev, "model %04x, revision %u.%u, lot %x, wafer %x, uid %x\n", 671 partnumber, majrev, minrev, lot, wafer, uid); 672 673 data = i2c_smbus_read_byte_data(client, ABX8XX_REG_CTRL1); 674 if (data < 0) { 675 dev_err(&client->dev, "Unable to read control register\n"); 676 return -EIO; 677 } 678 679 err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CTRL1, 680 ((data & ~(ABX8XX_CTRL_12_24 | 681 ABX8XX_CTRL_ARST)) | 682 ABX8XX_CTRL_WRITE)); 683 if (err < 0) { 684 dev_err(&client->dev, "Unable to write control register\n"); 685 return -EIO; 686 } 687 688 /* part autodetection */ 689 if (part == ABX80X) { 690 for (i = 0; abx80x_caps[i].pn; i++) 691 if (partnumber == abx80x_caps[i].pn) 692 break; 693 if (abx80x_caps[i].pn == 0) { 694 dev_err(&client->dev, "Unknown part: %04x\n", 695 partnumber); 696 return -EINVAL; 697 } 698 part = i; 699 } 700 701 if (partnumber != abx80x_caps[part].pn) { 702 dev_err(&client->dev, "partnumber mismatch %04x != %04x\n", 703 partnumber, abx80x_caps[part].pn); 704 return -EINVAL; 705 } 706 707 if (np && abx80x_caps[part].has_tc) 708 trickle_cfg = abx80x_dt_trickle_cfg(np); 709 710 if (trickle_cfg > 0) { 711 dev_info(&client->dev, "Enabling trickle charger: %02x\n", 712 trickle_cfg); 713 abx80x_enable_trickle_charger(client, trickle_cfg); 714 } 715 716 err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CD_TIMER_CTL, 717 BIT(2)); 718 if (err) 719 return err; 720 721 priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL); 722 if (priv == NULL) 723 return -ENOMEM; 724 725 priv->rtc = devm_rtc_allocate_device(&client->dev); 726 if (IS_ERR(priv->rtc)) 727 return PTR_ERR(priv->rtc); 728 729 priv->rtc->ops = &abx80x_rtc_ops; 730 priv->client = client; 731 732 i2c_set_clientdata(client, priv); 733 734 if (abx80x_caps[part].has_wdog) { 735 err = abx80x_setup_watchdog(priv); 736 if (err) 737 return err; 738 } 739 740 if (client->irq > 0) { 741 dev_info(&client->dev, "IRQ %d supplied\n", client->irq); 742 err = devm_request_threaded_irq(&client->dev, client->irq, NULL, 743 abx80x_handle_irq, 744 IRQF_SHARED | IRQF_ONESHOT, 745 "abx8xx", 746 client); 747 if (err) { 748 dev_err(&client->dev, "unable to request IRQ, alarms disabled\n"); 749 client->irq = 0; 750 } 751 } 752 753 /* Export sysfs entries */ 754 err = sysfs_create_group(&(&client->dev)->kobj, &rtc_calib_attr_group); 755 if (err) { 756 dev_err(&client->dev, "Failed to create sysfs group: %d\n", 757 err); 758 return err; 759 } 760 761 err = devm_add_action_or_reset(&client->dev, 762 rtc_calib_remove_sysfs_group, 763 &client->dev); 764 if (err) { 765 dev_err(&client->dev, 766 "Failed to add sysfs cleanup action: %d\n", 767 err); 768 return err; 769 } 770 771 err = rtc_register_device(priv->rtc); 772 773 return err; 774 } 775 776 static int abx80x_remove(struct i2c_client *client) 777 { 778 return 0; 779 } 780 781 static const struct i2c_device_id abx80x_id[] = { 782 { "abx80x", ABX80X }, 783 { "ab0801", AB0801 }, 784 { "ab0803", AB0803 }, 785 { "ab0804", AB0804 }, 786 { "ab0805", AB0805 }, 787 { "ab1801", AB1801 }, 788 { "ab1803", AB1803 }, 789 { "ab1804", AB1804 }, 790 { "ab1805", AB1805 }, 791 { "rv1805", AB1805 }, 792 { } 793 }; 794 MODULE_DEVICE_TABLE(i2c, abx80x_id); 795 796 static struct i2c_driver abx80x_driver = { 797 .driver = { 798 .name = "rtc-abx80x", 799 }, 800 .probe = abx80x_probe, 801 .remove = abx80x_remove, 802 .id_table = abx80x_id, 803 }; 804 805 module_i2c_driver(abx80x_driver); 806 807 MODULE_AUTHOR("Philippe De Muyter <phdm@macqel.be>"); 808 MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>"); 809 MODULE_DESCRIPTION("Abracon ABX80X RTC driver"); 810 MODULE_LICENSE("GPL v2"); 811