1 /* 2 * Micro Crystal RV-3029 rtc class driver 3 * 4 * Author: Gregory Hermant <gregory.hermant@calao-systems.com> 5 * Michael Buesch <m@bues.ch> 6 * 7 * based on previously existing rtc class drivers 8 * 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License version 2 as 11 * published by the Free Software Foundation. 12 * 13 */ 14 15 #include <linux/module.h> 16 #include <linux/i2c.h> 17 #include <linux/bcd.h> 18 #include <linux/rtc.h> 19 #include <linux/delay.h> 20 #include <linux/of.h> 21 #include <linux/hwmon.h> 22 #include <linux/hwmon-sysfs.h> 23 24 25 /* Register map */ 26 /* control section */ 27 #define RV3029_ONOFF_CTRL 0x00 28 #define RV3029_ONOFF_CTRL_WE BIT(0) 29 #define RV3029_ONOFF_CTRL_TE BIT(1) 30 #define RV3029_ONOFF_CTRL_TAR BIT(2) 31 #define RV3029_ONOFF_CTRL_EERE BIT(3) 32 #define RV3029_ONOFF_CTRL_SRON BIT(4) 33 #define RV3029_ONOFF_CTRL_TD0 BIT(5) 34 #define RV3029_ONOFF_CTRL_TD1 BIT(6) 35 #define RV3029_ONOFF_CTRL_CLKINT BIT(7) 36 #define RV3029_IRQ_CTRL 0x01 37 #define RV3029_IRQ_CTRL_AIE BIT(0) 38 #define RV3029_IRQ_CTRL_TIE BIT(1) 39 #define RV3029_IRQ_CTRL_V1IE BIT(2) 40 #define RV3029_IRQ_CTRL_V2IE BIT(3) 41 #define RV3029_IRQ_CTRL_SRIE BIT(4) 42 #define RV3029_IRQ_FLAGS 0x02 43 #define RV3029_IRQ_FLAGS_AF BIT(0) 44 #define RV3029_IRQ_FLAGS_TF BIT(1) 45 #define RV3029_IRQ_FLAGS_V1IF BIT(2) 46 #define RV3029_IRQ_FLAGS_V2IF BIT(3) 47 #define RV3029_IRQ_FLAGS_SRF BIT(4) 48 #define RV3029_STATUS 0x03 49 #define RV3029_STATUS_VLOW1 BIT(2) 50 #define RV3029_STATUS_VLOW2 BIT(3) 51 #define RV3029_STATUS_SR BIT(4) 52 #define RV3029_STATUS_PON BIT(5) 53 #define RV3029_STATUS_EEBUSY BIT(7) 54 #define RV3029_RST_CTRL 0x04 55 #define RV3029_RST_CTRL_SYSR BIT(4) 56 #define RV3029_CONTROL_SECTION_LEN 0x05 57 58 /* watch section */ 59 #define RV3029_W_SEC 0x08 60 #define RV3029_W_MINUTES 0x09 61 #define RV3029_W_HOURS 0x0A 62 #define RV3029_REG_HR_12_24 BIT(6) /* 24h/12h mode */ 63 #define RV3029_REG_HR_PM BIT(5) /* PM/AM bit in 12h mode */ 64 #define RV3029_W_DATE 0x0B 65 #define RV3029_W_DAYS 0x0C 66 #define RV3029_W_MONTHS 0x0D 67 #define RV3029_W_YEARS 0x0E 68 #define RV3029_WATCH_SECTION_LEN 0x07 69 70 /* alarm section */ 71 #define RV3029_A_SC 0x10 72 #define RV3029_A_MN 0x11 73 #define RV3029_A_HR 0x12 74 #define RV3029_A_DT 0x13 75 #define RV3029_A_DW 0x14 76 #define RV3029_A_MO 0x15 77 #define RV3029_A_YR 0x16 78 #define RV3029_ALARM_SECTION_LEN 0x07 79 80 /* timer section */ 81 #define RV3029_TIMER_LOW 0x18 82 #define RV3029_TIMER_HIGH 0x19 83 84 /* temperature section */ 85 #define RV3029_TEMP_PAGE 0x20 86 87 /* eeprom data section */ 88 #define RV3029_E2P_EEDATA1 0x28 89 #define RV3029_E2P_EEDATA2 0x29 90 #define RV3029_E2PDATA_SECTION_LEN 0x02 91 92 /* eeprom control section */ 93 #define RV3029_CONTROL_E2P_EECTRL 0x30 94 #define RV3029_EECTRL_THP BIT(0) /* temp scan interval */ 95 #define RV3029_EECTRL_THE BIT(1) /* thermometer enable */ 96 #define RV3029_EECTRL_FD0 BIT(2) /* CLKOUT */ 97 #define RV3029_EECTRL_FD1 BIT(3) /* CLKOUT */ 98 #define RV3029_TRICKLE_1K BIT(4) /* 1.5K resistance */ 99 #define RV3029_TRICKLE_5K BIT(5) /* 5K resistance */ 100 #define RV3029_TRICKLE_20K BIT(6) /* 20K resistance */ 101 #define RV3029_TRICKLE_80K BIT(7) /* 80K resistance */ 102 #define RV3029_TRICKLE_MASK (RV3029_TRICKLE_1K |\ 103 RV3029_TRICKLE_5K |\ 104 RV3029_TRICKLE_20K |\ 105 RV3029_TRICKLE_80K) 106 #define RV3029_TRICKLE_SHIFT 4 107 #define RV3029_CONTROL_E2P_XOFFS 0x31 /* XTAL offset */ 108 #define RV3029_CONTROL_E2P_XOFFS_SIGN BIT(7) /* Sign: 1->pos, 0->neg */ 109 #define RV3029_CONTROL_E2P_QCOEF 0x32 /* XTAL temp drift coef */ 110 #define RV3029_CONTROL_E2P_TURNOVER 0x33 /* XTAL turnover temp (in *C) */ 111 #define RV3029_CONTROL_E2P_TOV_MASK 0x3F /* XTAL turnover temp mask */ 112 113 /* user ram section */ 114 #define RV3029_USR1_RAM_PAGE 0x38 115 #define RV3029_USR1_SECTION_LEN 0x04 116 #define RV3029_USR2_RAM_PAGE 0x3C 117 #define RV3029_USR2_SECTION_LEN 0x04 118 119 static int 120 rv3029_i2c_read_regs(struct i2c_client *client, u8 reg, u8 *buf, 121 unsigned len) 122 { 123 int ret; 124 125 if ((reg > RV3029_USR1_RAM_PAGE + 7) || 126 (reg + len > RV3029_USR1_RAM_PAGE + 8)) 127 return -EINVAL; 128 129 ret = i2c_smbus_read_i2c_block_data(client, reg, len, buf); 130 if (ret < 0) 131 return ret; 132 if (ret < len) 133 return -EIO; 134 return 0; 135 } 136 137 static int 138 rv3029_i2c_write_regs(struct i2c_client *client, u8 reg, u8 const buf[], 139 unsigned len) 140 { 141 if ((reg > RV3029_USR1_RAM_PAGE + 7) || 142 (reg + len > RV3029_USR1_RAM_PAGE + 8)) 143 return -EINVAL; 144 145 return i2c_smbus_write_i2c_block_data(client, reg, len, buf); 146 } 147 148 static int 149 rv3029_i2c_update_bits(struct i2c_client *client, u8 reg, u8 mask, u8 set) 150 { 151 u8 buf; 152 int ret; 153 154 ret = rv3029_i2c_read_regs(client, reg, &buf, 1); 155 if (ret < 0) 156 return ret; 157 buf &= ~mask; 158 buf |= set & mask; 159 ret = rv3029_i2c_write_regs(client, reg, &buf, 1); 160 if (ret < 0) 161 return ret; 162 163 return 0; 164 } 165 166 static int 167 rv3029_i2c_get_sr(struct i2c_client *client, u8 *buf) 168 { 169 int ret = rv3029_i2c_read_regs(client, RV3029_STATUS, buf, 1); 170 171 if (ret < 0) 172 return -EIO; 173 dev_dbg(&client->dev, "status = 0x%.2x (%d)\n", buf[0], buf[0]); 174 return 0; 175 } 176 177 static int 178 rv3029_i2c_set_sr(struct i2c_client *client, u8 val) 179 { 180 u8 buf[1]; 181 int sr; 182 183 buf[0] = val; 184 sr = rv3029_i2c_write_regs(client, RV3029_STATUS, buf, 1); 185 dev_dbg(&client->dev, "status = 0x%.2x (%d)\n", buf[0], buf[0]); 186 if (sr < 0) 187 return -EIO; 188 return 0; 189 } 190 191 static int rv3029_eeprom_busywait(struct i2c_client *client) 192 { 193 int i, ret; 194 u8 sr; 195 196 for (i = 100; i > 0; i--) { 197 ret = rv3029_i2c_get_sr(client, &sr); 198 if (ret < 0) 199 break; 200 if (!(sr & RV3029_STATUS_EEBUSY)) 201 break; 202 usleep_range(1000, 10000); 203 } 204 if (i <= 0) { 205 dev_err(&client->dev, "EEPROM busy wait timeout.\n"); 206 return -ETIMEDOUT; 207 } 208 209 return ret; 210 } 211 212 static int rv3029_eeprom_exit(struct i2c_client *client) 213 { 214 /* Re-enable eeprom refresh */ 215 return rv3029_i2c_update_bits(client, RV3029_ONOFF_CTRL, 216 RV3029_ONOFF_CTRL_EERE, 217 RV3029_ONOFF_CTRL_EERE); 218 } 219 220 static int rv3029_eeprom_enter(struct i2c_client *client) 221 { 222 int ret; 223 u8 sr; 224 225 /* Check whether we are in the allowed voltage range. */ 226 ret = rv3029_i2c_get_sr(client, &sr); 227 if (ret < 0) 228 return ret; 229 if (sr & (RV3029_STATUS_VLOW1 | RV3029_STATUS_VLOW2)) { 230 /* We clear the bits and retry once just in case 231 * we had a brown out in early startup. 232 */ 233 sr &= ~RV3029_STATUS_VLOW1; 234 sr &= ~RV3029_STATUS_VLOW2; 235 ret = rv3029_i2c_set_sr(client, sr); 236 if (ret < 0) 237 return ret; 238 usleep_range(1000, 10000); 239 ret = rv3029_i2c_get_sr(client, &sr); 240 if (ret < 0) 241 return ret; 242 if (sr & (RV3029_STATUS_VLOW1 | RV3029_STATUS_VLOW2)) { 243 dev_err(&client->dev, 244 "Supply voltage is too low to safely access the EEPROM.\n"); 245 return -ENODEV; 246 } 247 } 248 249 /* Disable eeprom refresh. */ 250 ret = rv3029_i2c_update_bits(client, RV3029_ONOFF_CTRL, 251 RV3029_ONOFF_CTRL_EERE, 0); 252 if (ret < 0) 253 return ret; 254 255 /* Wait for any previous eeprom accesses to finish. */ 256 ret = rv3029_eeprom_busywait(client); 257 if (ret < 0) 258 rv3029_eeprom_exit(client); 259 260 return ret; 261 } 262 263 static int rv3029_eeprom_read(struct i2c_client *client, u8 reg, 264 u8 buf[], size_t len) 265 { 266 int ret, err; 267 268 err = rv3029_eeprom_enter(client); 269 if (err < 0) 270 return err; 271 272 ret = rv3029_i2c_read_regs(client, reg, buf, len); 273 274 err = rv3029_eeprom_exit(client); 275 if (err < 0) 276 return err; 277 278 return ret; 279 } 280 281 static int rv3029_eeprom_write(struct i2c_client *client, u8 reg, 282 u8 const buf[], size_t len) 283 { 284 int ret, err; 285 size_t i; 286 u8 tmp; 287 288 err = rv3029_eeprom_enter(client); 289 if (err < 0) 290 return err; 291 292 for (i = 0; i < len; i++, reg++) { 293 ret = rv3029_i2c_read_regs(client, reg, &tmp, 1); 294 if (ret < 0) 295 break; 296 if (tmp != buf[i]) { 297 ret = rv3029_i2c_write_regs(client, reg, &buf[i], 1); 298 if (ret < 0) 299 break; 300 } 301 ret = rv3029_eeprom_busywait(client); 302 if (ret < 0) 303 break; 304 } 305 306 err = rv3029_eeprom_exit(client); 307 if (err < 0) 308 return err; 309 310 return ret; 311 } 312 313 static int rv3029_eeprom_update_bits(struct i2c_client *client, 314 u8 reg, u8 mask, u8 set) 315 { 316 u8 buf; 317 int ret; 318 319 ret = rv3029_eeprom_read(client, reg, &buf, 1); 320 if (ret < 0) 321 return ret; 322 buf &= ~mask; 323 buf |= set & mask; 324 ret = rv3029_eeprom_write(client, reg, &buf, 1); 325 if (ret < 0) 326 return ret; 327 328 return 0; 329 } 330 331 static int 332 rv3029_i2c_read_time(struct i2c_client *client, struct rtc_time *tm) 333 { 334 u8 buf[1]; 335 int ret; 336 u8 regs[RV3029_WATCH_SECTION_LEN] = { 0, }; 337 338 ret = rv3029_i2c_get_sr(client, buf); 339 if (ret < 0) { 340 dev_err(&client->dev, "%s: reading SR failed\n", __func__); 341 return -EIO; 342 } 343 344 ret = rv3029_i2c_read_regs(client, RV3029_W_SEC, regs, 345 RV3029_WATCH_SECTION_LEN); 346 if (ret < 0) { 347 dev_err(&client->dev, "%s: reading RTC section failed\n", 348 __func__); 349 return ret; 350 } 351 352 tm->tm_sec = bcd2bin(regs[RV3029_W_SEC-RV3029_W_SEC]); 353 tm->tm_min = bcd2bin(regs[RV3029_W_MINUTES-RV3029_W_SEC]); 354 355 /* HR field has a more complex interpretation */ 356 { 357 const u8 _hr = regs[RV3029_W_HOURS-RV3029_W_SEC]; 358 359 if (_hr & RV3029_REG_HR_12_24) { 360 /* 12h format */ 361 tm->tm_hour = bcd2bin(_hr & 0x1f); 362 if (_hr & RV3029_REG_HR_PM) /* PM flag set */ 363 tm->tm_hour += 12; 364 } else /* 24h format */ 365 tm->tm_hour = bcd2bin(_hr & 0x3f); 366 } 367 368 tm->tm_mday = bcd2bin(regs[RV3029_W_DATE-RV3029_W_SEC]); 369 tm->tm_mon = bcd2bin(regs[RV3029_W_MONTHS-RV3029_W_SEC]) - 1; 370 tm->tm_year = bcd2bin(regs[RV3029_W_YEARS-RV3029_W_SEC]) + 100; 371 tm->tm_wday = bcd2bin(regs[RV3029_W_DAYS-RV3029_W_SEC]) - 1; 372 373 return 0; 374 } 375 376 static int rv3029_rtc_read_time(struct device *dev, struct rtc_time *tm) 377 { 378 return rv3029_i2c_read_time(to_i2c_client(dev), tm); 379 } 380 381 static int 382 rv3029_i2c_read_alarm(struct i2c_client *client, struct rtc_wkalrm *alarm) 383 { 384 struct rtc_time *const tm = &alarm->time; 385 int ret; 386 u8 regs[8]; 387 388 ret = rv3029_i2c_get_sr(client, regs); 389 if (ret < 0) { 390 dev_err(&client->dev, "%s: reading SR failed\n", __func__); 391 return -EIO; 392 } 393 394 ret = rv3029_i2c_read_regs(client, RV3029_A_SC, regs, 395 RV3029_ALARM_SECTION_LEN); 396 397 if (ret < 0) { 398 dev_err(&client->dev, "%s: reading alarm section failed\n", 399 __func__); 400 return ret; 401 } 402 403 tm->tm_sec = bcd2bin(regs[RV3029_A_SC-RV3029_A_SC] & 0x7f); 404 tm->tm_min = bcd2bin(regs[RV3029_A_MN-RV3029_A_SC] & 0x7f); 405 tm->tm_hour = bcd2bin(regs[RV3029_A_HR-RV3029_A_SC] & 0x3f); 406 tm->tm_mday = bcd2bin(regs[RV3029_A_DT-RV3029_A_SC] & 0x3f); 407 tm->tm_mon = bcd2bin(regs[RV3029_A_MO-RV3029_A_SC] & 0x1f) - 1; 408 tm->tm_year = bcd2bin(regs[RV3029_A_YR-RV3029_A_SC] & 0x7f) + 100; 409 tm->tm_wday = bcd2bin(regs[RV3029_A_DW-RV3029_A_SC] & 0x07) - 1; 410 411 return 0; 412 } 413 414 static int 415 rv3029_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm) 416 { 417 return rv3029_i2c_read_alarm(to_i2c_client(dev), alarm); 418 } 419 420 static int rv3029_rtc_i2c_alarm_set_irq(struct i2c_client *client, 421 int enable) 422 { 423 int ret; 424 425 /* enable/disable AIE irq */ 426 ret = rv3029_i2c_update_bits(client, RV3029_IRQ_CTRL, 427 RV3029_IRQ_CTRL_AIE, 428 (enable ? RV3029_IRQ_CTRL_AIE : 0)); 429 if (ret < 0) { 430 dev_err(&client->dev, "can't update INT reg\n"); 431 return ret; 432 } 433 434 return 0; 435 } 436 437 static int rv3029_rtc_i2c_set_alarm(struct i2c_client *client, 438 struct rtc_wkalrm *alarm) 439 { 440 struct rtc_time *const tm = &alarm->time; 441 int ret; 442 u8 regs[8]; 443 444 /* 445 * The clock has an 8 bit wide bcd-coded register (they never learn) 446 * for the year. tm_year is an offset from 1900 and we are interested 447 * in the 2000-2099 range, so any value less than 100 is invalid. 448 */ 449 if (tm->tm_year < 100) 450 return -EINVAL; 451 452 ret = rv3029_i2c_get_sr(client, regs); 453 if (ret < 0) { 454 dev_err(&client->dev, "%s: reading SR failed\n", __func__); 455 return -EIO; 456 } 457 regs[RV3029_A_SC-RV3029_A_SC] = bin2bcd(tm->tm_sec & 0x7f); 458 regs[RV3029_A_MN-RV3029_A_SC] = bin2bcd(tm->tm_min & 0x7f); 459 regs[RV3029_A_HR-RV3029_A_SC] = bin2bcd(tm->tm_hour & 0x3f); 460 regs[RV3029_A_DT-RV3029_A_SC] = bin2bcd(tm->tm_mday & 0x3f); 461 regs[RV3029_A_MO-RV3029_A_SC] = bin2bcd((tm->tm_mon & 0x1f) - 1); 462 regs[RV3029_A_DW-RV3029_A_SC] = bin2bcd((tm->tm_wday & 7) - 1); 463 regs[RV3029_A_YR-RV3029_A_SC] = bin2bcd((tm->tm_year & 0x7f) - 100); 464 465 ret = rv3029_i2c_write_regs(client, RV3029_A_SC, regs, 466 RV3029_ALARM_SECTION_LEN); 467 if (ret < 0) 468 return ret; 469 470 if (alarm->enabled) { 471 /* clear AF flag */ 472 ret = rv3029_i2c_update_bits(client, RV3029_IRQ_FLAGS, 473 RV3029_IRQ_FLAGS_AF, 0); 474 if (ret < 0) { 475 dev_err(&client->dev, "can't clear alarm flag\n"); 476 return ret; 477 } 478 /* enable AIE irq */ 479 ret = rv3029_rtc_i2c_alarm_set_irq(client, 1); 480 if (ret) 481 return ret; 482 483 dev_dbg(&client->dev, "alarm IRQ armed\n"); 484 } else { 485 /* disable AIE irq */ 486 ret = rv3029_rtc_i2c_alarm_set_irq(client, 0); 487 if (ret) 488 return ret; 489 490 dev_dbg(&client->dev, "alarm IRQ disabled\n"); 491 } 492 493 return 0; 494 } 495 496 static int rv3029_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm) 497 { 498 return rv3029_rtc_i2c_set_alarm(to_i2c_client(dev), alarm); 499 } 500 501 static int 502 rv3029_i2c_set_time(struct i2c_client *client, struct rtc_time const *tm) 503 { 504 u8 regs[8]; 505 int ret; 506 507 /* 508 * The clock has an 8 bit wide bcd-coded register (they never learn) 509 * for the year. tm_year is an offset from 1900 and we are interested 510 * in the 2000-2099 range, so any value less than 100 is invalid. 511 */ 512 if (tm->tm_year < 100) 513 return -EINVAL; 514 515 regs[RV3029_W_SEC-RV3029_W_SEC] = bin2bcd(tm->tm_sec); 516 regs[RV3029_W_MINUTES-RV3029_W_SEC] = bin2bcd(tm->tm_min); 517 regs[RV3029_W_HOURS-RV3029_W_SEC] = bin2bcd(tm->tm_hour); 518 regs[RV3029_W_DATE-RV3029_W_SEC] = bin2bcd(tm->tm_mday); 519 regs[RV3029_W_MONTHS-RV3029_W_SEC] = bin2bcd(tm->tm_mon+1); 520 regs[RV3029_W_DAYS-RV3029_W_SEC] = bin2bcd((tm->tm_wday & 7)+1); 521 regs[RV3029_W_YEARS-RV3029_W_SEC] = bin2bcd(tm->tm_year - 100); 522 523 ret = rv3029_i2c_write_regs(client, RV3029_W_SEC, regs, 524 RV3029_WATCH_SECTION_LEN); 525 if (ret < 0) 526 return ret; 527 528 ret = rv3029_i2c_get_sr(client, regs); 529 if (ret < 0) { 530 dev_err(&client->dev, "%s: reading SR failed\n", __func__); 531 return ret; 532 } 533 /* clear PON bit */ 534 ret = rv3029_i2c_set_sr(client, (regs[0] & ~RV3029_STATUS_PON)); 535 if (ret < 0) { 536 dev_err(&client->dev, "%s: reading SR failed\n", __func__); 537 return ret; 538 } 539 540 return 0; 541 } 542 543 static int rv3029_rtc_set_time(struct device *dev, struct rtc_time *tm) 544 { 545 return rv3029_i2c_set_time(to_i2c_client(dev), tm); 546 } 547 548 static const struct rv3029_trickle_tab_elem { 549 u32 r; /* resistance in ohms */ 550 u8 conf; /* trickle config bits */ 551 } rv3029_trickle_tab[] = { 552 { 553 .r = 1076, 554 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K | 555 RV3029_TRICKLE_20K | RV3029_TRICKLE_80K, 556 }, { 557 .r = 1091, 558 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K | 559 RV3029_TRICKLE_20K, 560 }, { 561 .r = 1137, 562 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K | 563 RV3029_TRICKLE_80K, 564 }, { 565 .r = 1154, 566 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K, 567 }, { 568 .r = 1371, 569 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_20K | 570 RV3029_TRICKLE_80K, 571 }, { 572 .r = 1395, 573 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_20K, 574 }, { 575 .r = 1472, 576 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_80K, 577 }, { 578 .r = 1500, 579 .conf = RV3029_TRICKLE_1K, 580 }, { 581 .r = 3810, 582 .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_20K | 583 RV3029_TRICKLE_80K, 584 }, { 585 .r = 4000, 586 .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_20K, 587 }, { 588 .r = 4706, 589 .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_80K, 590 }, { 591 .r = 5000, 592 .conf = RV3029_TRICKLE_5K, 593 }, { 594 .r = 16000, 595 .conf = RV3029_TRICKLE_20K | RV3029_TRICKLE_80K, 596 }, { 597 .r = 20000, 598 .conf = RV3029_TRICKLE_20K, 599 }, { 600 .r = 80000, 601 .conf = RV3029_TRICKLE_80K, 602 }, 603 }; 604 605 static void rv3029_trickle_config(struct i2c_client *client) 606 { 607 struct device_node *of_node = client->dev.of_node; 608 const struct rv3029_trickle_tab_elem *elem; 609 int i, err; 610 u32 ohms; 611 u8 trickle_set_bits; 612 613 if (!of_node) 614 return; 615 616 /* Configure the trickle charger. */ 617 err = of_property_read_u32(of_node, "trickle-resistor-ohms", &ohms); 618 if (err) { 619 /* Disable trickle charger. */ 620 trickle_set_bits = 0; 621 } else { 622 /* Enable trickle charger. */ 623 for (i = 0; i < ARRAY_SIZE(rv3029_trickle_tab); i++) { 624 elem = &rv3029_trickle_tab[i]; 625 if (elem->r >= ohms) 626 break; 627 } 628 trickle_set_bits = elem->conf; 629 dev_info(&client->dev, 630 "Trickle charger enabled at %d ohms resistance.\n", 631 elem->r); 632 } 633 err = rv3029_eeprom_update_bits(client, RV3029_CONTROL_E2P_EECTRL, 634 RV3029_TRICKLE_MASK, 635 trickle_set_bits); 636 if (err < 0) { 637 dev_err(&client->dev, 638 "Failed to update trickle charger config\n"); 639 } 640 } 641 642 #ifdef CONFIG_RTC_DRV_RV3029_HWMON 643 644 static int rv3029_read_temp(struct i2c_client *client, int *temp_mC) 645 { 646 int ret; 647 u8 temp; 648 649 ret = rv3029_i2c_read_regs(client, RV3029_TEMP_PAGE, &temp, 1); 650 if (ret < 0) 651 return ret; 652 653 *temp_mC = ((int)temp - 60) * 1000; 654 655 return 0; 656 } 657 658 static ssize_t rv3029_hwmon_show_temp(struct device *dev, 659 struct device_attribute *attr, 660 char *buf) 661 { 662 struct i2c_client *client = dev_get_drvdata(dev); 663 int ret, temp_mC; 664 665 ret = rv3029_read_temp(client, &temp_mC); 666 if (ret < 0) 667 return ret; 668 669 return sprintf(buf, "%d\n", temp_mC); 670 } 671 672 static ssize_t rv3029_hwmon_set_update_interval(struct device *dev, 673 struct device_attribute *attr, 674 const char *buf, 675 size_t count) 676 { 677 struct i2c_client *client = dev_get_drvdata(dev); 678 unsigned long interval_ms; 679 int ret; 680 u8 th_set_bits = 0; 681 682 ret = kstrtoul(buf, 10, &interval_ms); 683 if (ret < 0) 684 return ret; 685 686 if (interval_ms != 0) { 687 th_set_bits |= RV3029_EECTRL_THE; 688 if (interval_ms >= 16000) 689 th_set_bits |= RV3029_EECTRL_THP; 690 } 691 ret = rv3029_eeprom_update_bits(client, RV3029_CONTROL_E2P_EECTRL, 692 RV3029_EECTRL_THE | RV3029_EECTRL_THP, 693 th_set_bits); 694 if (ret < 0) 695 return ret; 696 697 return count; 698 } 699 700 static ssize_t rv3029_hwmon_show_update_interval(struct device *dev, 701 struct device_attribute *attr, 702 char *buf) 703 { 704 struct i2c_client *client = dev_get_drvdata(dev); 705 int ret, interval_ms; 706 u8 eectrl; 707 708 ret = rv3029_eeprom_read(client, RV3029_CONTROL_E2P_EECTRL, 709 &eectrl, 1); 710 if (ret < 0) 711 return ret; 712 713 if (eectrl & RV3029_EECTRL_THE) { 714 if (eectrl & RV3029_EECTRL_THP) 715 interval_ms = 16000; 716 else 717 interval_ms = 1000; 718 } else { 719 interval_ms = 0; 720 } 721 722 return sprintf(buf, "%d\n", interval_ms); 723 } 724 725 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, rv3029_hwmon_show_temp, 726 NULL, 0); 727 static SENSOR_DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO, 728 rv3029_hwmon_show_update_interval, 729 rv3029_hwmon_set_update_interval, 0); 730 731 static struct attribute *rv3029_hwmon_attrs[] = { 732 &sensor_dev_attr_temp1_input.dev_attr.attr, 733 &sensor_dev_attr_update_interval.dev_attr.attr, 734 NULL, 735 }; 736 ATTRIBUTE_GROUPS(rv3029_hwmon); 737 738 static void rv3029_hwmon_register(struct i2c_client *client) 739 { 740 struct device *hwmon_dev; 741 742 hwmon_dev = devm_hwmon_device_register_with_groups( 743 &client->dev, client->name, client, rv3029_hwmon_groups); 744 if (IS_ERR(hwmon_dev)) { 745 dev_warn(&client->dev, 746 "unable to register hwmon device %ld\n", 747 PTR_ERR(hwmon_dev)); 748 } 749 } 750 751 #else /* CONFIG_RTC_DRV_RV3029_HWMON */ 752 753 static void rv3029_hwmon_register(struct i2c_client *client) 754 { 755 } 756 757 #endif /* CONFIG_RTC_DRV_RV3029_HWMON */ 758 759 static const struct rtc_class_ops rv3029_rtc_ops = { 760 .read_time = rv3029_rtc_read_time, 761 .set_time = rv3029_rtc_set_time, 762 .read_alarm = rv3029_rtc_read_alarm, 763 .set_alarm = rv3029_rtc_set_alarm, 764 }; 765 766 static struct i2c_device_id rv3029_id[] = { 767 { "rv3029", 0 }, 768 { "rv3029c2", 0 }, 769 { } 770 }; 771 MODULE_DEVICE_TABLE(i2c, rv3029_id); 772 773 static int rv3029_probe(struct i2c_client *client, 774 const struct i2c_device_id *id) 775 { 776 struct rtc_device *rtc; 777 int rc = 0; 778 u8 buf[1]; 779 780 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_EMUL)) 781 return -ENODEV; 782 783 rc = rv3029_i2c_get_sr(client, buf); 784 if (rc < 0) { 785 dev_err(&client->dev, "reading status failed\n"); 786 return rc; 787 } 788 789 rv3029_trickle_config(client); 790 rv3029_hwmon_register(client); 791 792 rtc = devm_rtc_device_register(&client->dev, client->name, 793 &rv3029_rtc_ops, THIS_MODULE); 794 795 if (IS_ERR(rtc)) 796 return PTR_ERR(rtc); 797 798 i2c_set_clientdata(client, rtc); 799 800 return 0; 801 } 802 803 static struct i2c_driver rv3029_driver = { 804 .driver = { 805 .name = "rtc-rv3029c2", 806 }, 807 .probe = rv3029_probe, 808 .id_table = rv3029_id, 809 }; 810 811 module_i2c_driver(rv3029_driver); 812 813 MODULE_AUTHOR("Gregory Hermant <gregory.hermant@calao-systems.com>"); 814 MODULE_AUTHOR("Michael Buesch <m@bues.ch>"); 815 MODULE_DESCRIPTION("Micro Crystal RV3029 RTC driver"); 816 MODULE_LICENSE("GPL"); 817