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