1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * RTC driver for the Micro Crystal RV3028 4 * 5 * Copyright (C) 2019 Micro Crystal SA 6 * 7 * Alexandre Belloni <alexandre.belloni@bootlin.com> 8 * 9 */ 10 11 #include <linux/clk-provider.h> 12 #include <linux/bcd.h> 13 #include <linux/bitfield.h> 14 #include <linux/bitops.h> 15 #include <linux/i2c.h> 16 #include <linux/interrupt.h> 17 #include <linux/kernel.h> 18 #include <linux/log2.h> 19 #include <linux/module.h> 20 #include <linux/of.h> 21 #include <linux/regmap.h> 22 #include <linux/rtc.h> 23 24 #define RV3028_SEC 0x00 25 #define RV3028_MIN 0x01 26 #define RV3028_HOUR 0x02 27 #define RV3028_WDAY 0x03 28 #define RV3028_DAY 0x04 29 #define RV3028_MONTH 0x05 30 #define RV3028_YEAR 0x06 31 #define RV3028_ALARM_MIN 0x07 32 #define RV3028_ALARM_HOUR 0x08 33 #define RV3028_ALARM_DAY 0x09 34 #define RV3028_STATUS 0x0E 35 #define RV3028_CTRL1 0x0F 36 #define RV3028_CTRL2 0x10 37 #define RV3028_EVT_CTRL 0x13 38 #define RV3028_TS_COUNT 0x14 39 #define RV3028_TS_SEC 0x15 40 #define RV3028_RAM1 0x1F 41 #define RV3028_EEPROM_ADDR 0x25 42 #define RV3028_EEPROM_DATA 0x26 43 #define RV3028_EEPROM_CMD 0x27 44 #define RV3028_CLKOUT 0x35 45 #define RV3028_OFFSET 0x36 46 #define RV3028_BACKUP 0x37 47 48 #define RV3028_STATUS_PORF BIT(0) 49 #define RV3028_STATUS_EVF BIT(1) 50 #define RV3028_STATUS_AF BIT(2) 51 #define RV3028_STATUS_TF BIT(3) 52 #define RV3028_STATUS_UF BIT(4) 53 #define RV3028_STATUS_BSF BIT(5) 54 #define RV3028_STATUS_CLKF BIT(6) 55 #define RV3028_STATUS_EEBUSY BIT(7) 56 57 #define RV3028_CLKOUT_FD_MASK GENMASK(2, 0) 58 #define RV3028_CLKOUT_PORIE BIT(3) 59 #define RV3028_CLKOUT_CLKSY BIT(6) 60 #define RV3028_CLKOUT_CLKOE BIT(7) 61 62 #define RV3028_CTRL1_EERD BIT(3) 63 #define RV3028_CTRL1_WADA BIT(5) 64 65 #define RV3028_CTRL2_RESET BIT(0) 66 #define RV3028_CTRL2_12_24 BIT(1) 67 #define RV3028_CTRL2_EIE BIT(2) 68 #define RV3028_CTRL2_AIE BIT(3) 69 #define RV3028_CTRL2_TIE BIT(4) 70 #define RV3028_CTRL2_UIE BIT(5) 71 #define RV3028_CTRL2_TSE BIT(7) 72 73 #define RV3028_EVT_CTRL_TSR BIT(2) 74 75 #define RV3028_EEPROM_CMD_UPDATE 0x11 76 #define RV3028_EEPROM_CMD_WRITE 0x21 77 #define RV3028_EEPROM_CMD_READ 0x22 78 79 #define RV3028_EEBUSY_POLL 10000 80 #define RV3028_EEBUSY_TIMEOUT 100000 81 82 #define RV3028_BACKUP_TCE BIT(5) 83 #define RV3028_BACKUP_TCR_MASK GENMASK(1,0) 84 #define RV3028_BACKUP_BSM GENMASK(3,2) 85 86 #define RV3028_BACKUP_BSM_DSM 0x1 87 #define RV3028_BACKUP_BSM_LSM 0x3 88 89 #define OFFSET_STEP_PPT 953674 90 91 enum rv3028_type { 92 rv_3028, 93 }; 94 95 struct rv3028_data { 96 struct regmap *regmap; 97 struct rtc_device *rtc; 98 enum rv3028_type type; 99 #ifdef CONFIG_COMMON_CLK 100 struct clk_hw clkout_hw; 101 #endif 102 }; 103 104 static u16 rv3028_trickle_resistors[] = {3000, 5000, 9000, 15000}; 105 106 static ssize_t timestamp0_store(struct device *dev, 107 struct device_attribute *attr, 108 const char *buf, size_t count) 109 { 110 struct rv3028_data *rv3028 = dev_get_drvdata(dev->parent); 111 112 regmap_update_bits(rv3028->regmap, RV3028_EVT_CTRL, RV3028_EVT_CTRL_TSR, 113 RV3028_EVT_CTRL_TSR); 114 115 return count; 116 }; 117 118 static ssize_t timestamp0_show(struct device *dev, 119 struct device_attribute *attr, char *buf) 120 { 121 struct rv3028_data *rv3028 = dev_get_drvdata(dev->parent); 122 struct rtc_time tm; 123 unsigned int count; 124 u8 date[6]; 125 int ret; 126 127 ret = regmap_read(rv3028->regmap, RV3028_TS_COUNT, &count); 128 if (ret) 129 return ret; 130 131 if (!count) 132 return 0; 133 134 ret = regmap_bulk_read(rv3028->regmap, RV3028_TS_SEC, date, 135 sizeof(date)); 136 if (ret) 137 return ret; 138 139 tm.tm_sec = bcd2bin(date[0]); 140 tm.tm_min = bcd2bin(date[1]); 141 tm.tm_hour = bcd2bin(date[2]); 142 tm.tm_mday = bcd2bin(date[3]); 143 tm.tm_mon = bcd2bin(date[4]) - 1; 144 tm.tm_year = bcd2bin(date[5]) + 100; 145 146 ret = rtc_valid_tm(&tm); 147 if (ret) 148 return ret; 149 150 return sprintf(buf, "%llu\n", 151 (unsigned long long)rtc_tm_to_time64(&tm)); 152 }; 153 154 static DEVICE_ATTR_RW(timestamp0); 155 156 static ssize_t timestamp0_count_show(struct device *dev, 157 struct device_attribute *attr, char *buf) 158 { 159 struct rv3028_data *rv3028 = dev_get_drvdata(dev->parent); 160 unsigned int count; 161 int ret; 162 163 ret = regmap_read(rv3028->regmap, RV3028_TS_COUNT, &count); 164 if (ret) 165 return ret; 166 167 return sprintf(buf, "%u\n", count); 168 }; 169 170 static DEVICE_ATTR_RO(timestamp0_count); 171 172 static struct attribute *rv3028_attrs[] = { 173 &dev_attr_timestamp0.attr, 174 &dev_attr_timestamp0_count.attr, 175 NULL 176 }; 177 178 static const struct attribute_group rv3028_attr_group = { 179 .attrs = rv3028_attrs, 180 }; 181 182 static int rv3028_exit_eerd(struct rv3028_data *rv3028, u32 eerd) 183 { 184 if (eerd) 185 return 0; 186 187 return regmap_update_bits(rv3028->regmap, RV3028_CTRL1, RV3028_CTRL1_EERD, 0); 188 } 189 190 static int rv3028_enter_eerd(struct rv3028_data *rv3028, u32 *eerd) 191 { 192 u32 ctrl1, status; 193 int ret; 194 195 ret = regmap_read(rv3028->regmap, RV3028_CTRL1, &ctrl1); 196 if (ret) 197 return ret; 198 199 *eerd = ctrl1 & RV3028_CTRL1_EERD; 200 if (*eerd) 201 return 0; 202 203 ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL1, 204 RV3028_CTRL1_EERD, RV3028_CTRL1_EERD); 205 if (ret) 206 return ret; 207 208 ret = regmap_read_poll_timeout(rv3028->regmap, RV3028_STATUS, status, 209 !(status & RV3028_STATUS_EEBUSY), 210 RV3028_EEBUSY_POLL, RV3028_EEBUSY_TIMEOUT); 211 if (ret) { 212 rv3028_exit_eerd(rv3028, *eerd); 213 214 return ret; 215 } 216 217 return 0; 218 } 219 220 static int rv3028_update_eeprom(struct rv3028_data *rv3028, u32 eerd) 221 { 222 u32 status; 223 int ret; 224 225 ret = regmap_write(rv3028->regmap, RV3028_EEPROM_CMD, 0x0); 226 if (ret) 227 goto exit_eerd; 228 229 ret = regmap_write(rv3028->regmap, RV3028_EEPROM_CMD, RV3028_EEPROM_CMD_UPDATE); 230 if (ret) 231 goto exit_eerd; 232 233 usleep_range(63000, RV3028_EEBUSY_TIMEOUT); 234 235 ret = regmap_read_poll_timeout(rv3028->regmap, RV3028_STATUS, status, 236 !(status & RV3028_STATUS_EEBUSY), 237 RV3028_EEBUSY_POLL, RV3028_EEBUSY_TIMEOUT); 238 239 exit_eerd: 240 rv3028_exit_eerd(rv3028, eerd); 241 242 return ret; 243 } 244 245 static int rv3028_update_cfg(struct rv3028_data *rv3028, unsigned int reg, 246 unsigned int mask, unsigned int val) 247 { 248 u32 eerd; 249 int ret; 250 251 ret = rv3028_enter_eerd(rv3028, &eerd); 252 if (ret) 253 return ret; 254 255 ret = regmap_update_bits(rv3028->regmap, reg, mask, val); 256 if (ret) { 257 rv3028_exit_eerd(rv3028, eerd); 258 return ret; 259 } 260 261 return rv3028_update_eeprom(rv3028, eerd); 262 } 263 264 static irqreturn_t rv3028_handle_irq(int irq, void *dev_id) 265 { 266 struct rv3028_data *rv3028 = dev_id; 267 unsigned long events = 0; 268 u32 status = 0, ctrl = 0; 269 270 if (regmap_read(rv3028->regmap, RV3028_STATUS, &status) < 0 || 271 status == 0) { 272 return IRQ_NONE; 273 } 274 275 status &= ~RV3028_STATUS_PORF; 276 277 if (status & RV3028_STATUS_TF) { 278 status |= RV3028_STATUS_TF; 279 ctrl |= RV3028_CTRL2_TIE; 280 events |= RTC_PF; 281 } 282 283 if (status & RV3028_STATUS_AF) { 284 status |= RV3028_STATUS_AF; 285 ctrl |= RV3028_CTRL2_AIE; 286 events |= RTC_AF; 287 } 288 289 if (status & RV3028_STATUS_UF) { 290 status |= RV3028_STATUS_UF; 291 ctrl |= RV3028_CTRL2_UIE; 292 events |= RTC_UF; 293 } 294 295 if (events) { 296 rtc_update_irq(rv3028->rtc, 1, events); 297 regmap_update_bits(rv3028->regmap, RV3028_STATUS, status, 0); 298 regmap_update_bits(rv3028->regmap, RV3028_CTRL2, ctrl, 0); 299 } 300 301 if (status & RV3028_STATUS_EVF) { 302 sysfs_notify(&rv3028->rtc->dev.kobj, NULL, 303 dev_attr_timestamp0.attr.name); 304 dev_warn(&rv3028->rtc->dev, "event detected"); 305 } 306 307 return IRQ_HANDLED; 308 } 309 310 static int rv3028_get_time(struct device *dev, struct rtc_time *tm) 311 { 312 struct rv3028_data *rv3028 = dev_get_drvdata(dev); 313 u8 date[7]; 314 int ret, status; 315 316 ret = regmap_read(rv3028->regmap, RV3028_STATUS, &status); 317 if (ret < 0) 318 return ret; 319 320 if (status & RV3028_STATUS_PORF) 321 return -EINVAL; 322 323 ret = regmap_bulk_read(rv3028->regmap, RV3028_SEC, date, sizeof(date)); 324 if (ret) 325 return ret; 326 327 tm->tm_sec = bcd2bin(date[RV3028_SEC] & 0x7f); 328 tm->tm_min = bcd2bin(date[RV3028_MIN] & 0x7f); 329 tm->tm_hour = bcd2bin(date[RV3028_HOUR] & 0x3f); 330 tm->tm_wday = date[RV3028_WDAY] & 0x7f; 331 tm->tm_mday = bcd2bin(date[RV3028_DAY] & 0x3f); 332 tm->tm_mon = bcd2bin(date[RV3028_MONTH] & 0x1f) - 1; 333 tm->tm_year = bcd2bin(date[RV3028_YEAR]) + 100; 334 335 return 0; 336 } 337 338 static int rv3028_set_time(struct device *dev, struct rtc_time *tm) 339 { 340 struct rv3028_data *rv3028 = dev_get_drvdata(dev); 341 u8 date[7]; 342 int ret; 343 344 date[RV3028_SEC] = bin2bcd(tm->tm_sec); 345 date[RV3028_MIN] = bin2bcd(tm->tm_min); 346 date[RV3028_HOUR] = bin2bcd(tm->tm_hour); 347 date[RV3028_WDAY] = tm->tm_wday; 348 date[RV3028_DAY] = bin2bcd(tm->tm_mday); 349 date[RV3028_MONTH] = bin2bcd(tm->tm_mon + 1); 350 date[RV3028_YEAR] = bin2bcd(tm->tm_year - 100); 351 352 /* 353 * Writing to the Seconds register has the same effect as setting RESET 354 * bit to 1 355 */ 356 ret = regmap_bulk_write(rv3028->regmap, RV3028_SEC, date, 357 sizeof(date)); 358 if (ret) 359 return ret; 360 361 ret = regmap_update_bits(rv3028->regmap, RV3028_STATUS, 362 RV3028_STATUS_PORF, 0); 363 364 return ret; 365 } 366 367 static int rv3028_get_alarm(struct device *dev, struct rtc_wkalrm *alrm) 368 { 369 struct rv3028_data *rv3028 = dev_get_drvdata(dev); 370 u8 alarmvals[3]; 371 int status, ctrl, ret; 372 373 ret = regmap_bulk_read(rv3028->regmap, RV3028_ALARM_MIN, alarmvals, 374 sizeof(alarmvals)); 375 if (ret) 376 return ret; 377 378 ret = regmap_read(rv3028->regmap, RV3028_STATUS, &status); 379 if (ret < 0) 380 return ret; 381 382 ret = regmap_read(rv3028->regmap, RV3028_CTRL2, &ctrl); 383 if (ret < 0) 384 return ret; 385 386 alrm->time.tm_sec = 0; 387 alrm->time.tm_min = bcd2bin(alarmvals[0] & 0x7f); 388 alrm->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f); 389 alrm->time.tm_mday = bcd2bin(alarmvals[2] & 0x3f); 390 391 alrm->enabled = !!(ctrl & RV3028_CTRL2_AIE); 392 alrm->pending = (status & RV3028_STATUS_AF) && alrm->enabled; 393 394 return 0; 395 } 396 397 static int rv3028_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) 398 { 399 struct rv3028_data *rv3028 = dev_get_drvdata(dev); 400 u8 alarmvals[3]; 401 u8 ctrl = 0; 402 int ret; 403 404 /* The alarm has no seconds, round up to nearest minute */ 405 if (alrm->time.tm_sec) { 406 time64_t alarm_time = rtc_tm_to_time64(&alrm->time); 407 408 alarm_time += 60 - alrm->time.tm_sec; 409 rtc_time64_to_tm(alarm_time, &alrm->time); 410 } 411 412 ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL2, 413 RV3028_CTRL2_AIE | RV3028_CTRL2_UIE, 0); 414 if (ret) 415 return ret; 416 417 alarmvals[0] = bin2bcd(alrm->time.tm_min); 418 alarmvals[1] = bin2bcd(alrm->time.tm_hour); 419 alarmvals[2] = bin2bcd(alrm->time.tm_mday); 420 421 ret = regmap_update_bits(rv3028->regmap, RV3028_STATUS, 422 RV3028_STATUS_AF, 0); 423 if (ret) 424 return ret; 425 426 ret = regmap_bulk_write(rv3028->regmap, RV3028_ALARM_MIN, alarmvals, 427 sizeof(alarmvals)); 428 if (ret) 429 return ret; 430 431 if (alrm->enabled) { 432 if (rv3028->rtc->uie_rtctimer.enabled) 433 ctrl |= RV3028_CTRL2_UIE; 434 if (rv3028->rtc->aie_timer.enabled) 435 ctrl |= RV3028_CTRL2_AIE; 436 } 437 438 ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL2, 439 RV3028_CTRL2_UIE | RV3028_CTRL2_AIE, ctrl); 440 441 return ret; 442 } 443 444 static int rv3028_alarm_irq_enable(struct device *dev, unsigned int enabled) 445 { 446 struct rv3028_data *rv3028 = dev_get_drvdata(dev); 447 int ctrl = 0, ret; 448 449 if (enabled) { 450 if (rv3028->rtc->uie_rtctimer.enabled) 451 ctrl |= RV3028_CTRL2_UIE; 452 if (rv3028->rtc->aie_timer.enabled) 453 ctrl |= RV3028_CTRL2_AIE; 454 } 455 456 ret = regmap_update_bits(rv3028->regmap, RV3028_STATUS, 457 RV3028_STATUS_AF | RV3028_STATUS_UF, 0); 458 if (ret) 459 return ret; 460 461 ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL2, 462 RV3028_CTRL2_UIE | RV3028_CTRL2_AIE, ctrl); 463 if (ret) 464 return ret; 465 466 return 0; 467 } 468 469 static int rv3028_read_offset(struct device *dev, long *offset) 470 { 471 struct rv3028_data *rv3028 = dev_get_drvdata(dev); 472 int ret, value, steps; 473 474 ret = regmap_read(rv3028->regmap, RV3028_OFFSET, &value); 475 if (ret < 0) 476 return ret; 477 478 steps = sign_extend32(value << 1, 8); 479 480 ret = regmap_read(rv3028->regmap, RV3028_BACKUP, &value); 481 if (ret < 0) 482 return ret; 483 484 steps += value >> 7; 485 486 *offset = DIV_ROUND_CLOSEST(steps * OFFSET_STEP_PPT, 1000); 487 488 return 0; 489 } 490 491 static int rv3028_set_offset(struct device *dev, long offset) 492 { 493 struct rv3028_data *rv3028 = dev_get_drvdata(dev); 494 u32 eerd; 495 int ret; 496 497 offset = clamp(offset, -244141L, 243187L) * 1000; 498 offset = DIV_ROUND_CLOSEST(offset, OFFSET_STEP_PPT); 499 500 ret = rv3028_enter_eerd(rv3028, &eerd); 501 if (ret) 502 return ret; 503 504 ret = regmap_write(rv3028->regmap, RV3028_OFFSET, offset >> 1); 505 if (ret < 0) 506 goto exit_eerd; 507 508 ret = regmap_update_bits(rv3028->regmap, RV3028_BACKUP, BIT(7), 509 offset << 7); 510 if (ret < 0) 511 goto exit_eerd; 512 513 return rv3028_update_eeprom(rv3028, eerd); 514 515 exit_eerd: 516 rv3028_exit_eerd(rv3028, eerd); 517 518 return ret; 519 520 } 521 522 static int rv3028_param_get(struct device *dev, struct rtc_param *param) 523 { 524 struct rv3028_data *rv3028 = dev_get_drvdata(dev); 525 int ret; 526 u32 value; 527 528 switch(param->param) { 529 case RTC_PARAM_BACKUP_SWITCH_MODE: 530 ret = regmap_read(rv3028->regmap, RV3028_BACKUP, &value); 531 if (ret < 0) 532 return ret; 533 534 value = FIELD_GET(RV3028_BACKUP_BSM, value); 535 536 switch(value) { 537 case RV3028_BACKUP_BSM_DSM: 538 param->uvalue = RTC_BSM_DIRECT; 539 break; 540 case RV3028_BACKUP_BSM_LSM: 541 param->uvalue = RTC_BSM_LEVEL; 542 break; 543 default: 544 param->uvalue = RTC_BSM_DISABLED; 545 } 546 break; 547 548 default: 549 return -EINVAL; 550 } 551 552 return 0; 553 } 554 555 static int rv3028_param_set(struct device *dev, struct rtc_param *param) 556 { 557 struct rv3028_data *rv3028 = dev_get_drvdata(dev); 558 u8 mode; 559 560 switch(param->param) { 561 case RTC_PARAM_BACKUP_SWITCH_MODE: 562 switch (param->uvalue) { 563 case RTC_BSM_DISABLED: 564 mode = 0; 565 break; 566 case RTC_BSM_DIRECT: 567 mode = RV3028_BACKUP_BSM_DSM; 568 break; 569 case RTC_BSM_LEVEL: 570 mode = RV3028_BACKUP_BSM_LSM; 571 break; 572 default: 573 return -EINVAL; 574 } 575 576 return rv3028_update_cfg(rv3028, RV3028_BACKUP, RV3028_BACKUP_BSM, 577 FIELD_PREP(RV3028_BACKUP_BSM, mode)); 578 579 default: 580 return -EINVAL; 581 } 582 583 return 0; 584 } 585 586 static int rv3028_ioctl(struct device *dev, unsigned int cmd, unsigned long arg) 587 { 588 struct rv3028_data *rv3028 = dev_get_drvdata(dev); 589 int status, ret = 0; 590 591 switch (cmd) { 592 case RTC_VL_READ: 593 ret = regmap_read(rv3028->regmap, RV3028_STATUS, &status); 594 if (ret < 0) 595 return ret; 596 597 status = status & RV3028_STATUS_PORF ? RTC_VL_DATA_INVALID : 0; 598 return put_user(status, (unsigned int __user *)arg); 599 600 default: 601 return -ENOIOCTLCMD; 602 } 603 } 604 605 static int rv3028_nvram_write(void *priv, unsigned int offset, void *val, 606 size_t bytes) 607 { 608 return regmap_bulk_write(priv, RV3028_RAM1 + offset, val, bytes); 609 } 610 611 static int rv3028_nvram_read(void *priv, unsigned int offset, void *val, 612 size_t bytes) 613 { 614 return regmap_bulk_read(priv, RV3028_RAM1 + offset, val, bytes); 615 } 616 617 static int rv3028_eeprom_write(void *priv, unsigned int offset, void *val, 618 size_t bytes) 619 { 620 struct rv3028_data *rv3028 = priv; 621 u32 status, eerd; 622 int i, ret; 623 u8 *buf = val; 624 625 ret = rv3028_enter_eerd(rv3028, &eerd); 626 if (ret) 627 return ret; 628 629 for (i = 0; i < bytes; i++) { 630 ret = regmap_write(rv3028->regmap, RV3028_EEPROM_ADDR, offset + i); 631 if (ret) 632 goto restore_eerd; 633 634 ret = regmap_write(rv3028->regmap, RV3028_EEPROM_DATA, buf[i]); 635 if (ret) 636 goto restore_eerd; 637 638 ret = regmap_write(rv3028->regmap, RV3028_EEPROM_CMD, 0x0); 639 if (ret) 640 goto restore_eerd; 641 642 ret = regmap_write(rv3028->regmap, RV3028_EEPROM_CMD, 643 RV3028_EEPROM_CMD_WRITE); 644 if (ret) 645 goto restore_eerd; 646 647 usleep_range(RV3028_EEBUSY_POLL, RV3028_EEBUSY_TIMEOUT); 648 649 ret = regmap_read_poll_timeout(rv3028->regmap, RV3028_STATUS, status, 650 !(status & RV3028_STATUS_EEBUSY), 651 RV3028_EEBUSY_POLL, 652 RV3028_EEBUSY_TIMEOUT); 653 if (ret) 654 goto restore_eerd; 655 } 656 657 restore_eerd: 658 rv3028_exit_eerd(rv3028, eerd); 659 660 return ret; 661 } 662 663 static int rv3028_eeprom_read(void *priv, unsigned int offset, void *val, 664 size_t bytes) 665 { 666 struct rv3028_data *rv3028 = priv; 667 u32 status, eerd, data; 668 int i, ret; 669 u8 *buf = val; 670 671 ret = rv3028_enter_eerd(rv3028, &eerd); 672 if (ret) 673 return ret; 674 675 for (i = 0; i < bytes; i++) { 676 ret = regmap_write(rv3028->regmap, RV3028_EEPROM_ADDR, offset + i); 677 if (ret) 678 goto restore_eerd; 679 680 ret = regmap_write(rv3028->regmap, RV3028_EEPROM_CMD, 0x0); 681 if (ret) 682 goto restore_eerd; 683 684 ret = regmap_write(rv3028->regmap, RV3028_EEPROM_CMD, 685 RV3028_EEPROM_CMD_READ); 686 if (ret) 687 goto restore_eerd; 688 689 ret = regmap_read_poll_timeout(rv3028->regmap, RV3028_STATUS, status, 690 !(status & RV3028_STATUS_EEBUSY), 691 RV3028_EEBUSY_POLL, 692 RV3028_EEBUSY_TIMEOUT); 693 if (ret) 694 goto restore_eerd; 695 696 ret = regmap_read(rv3028->regmap, RV3028_EEPROM_DATA, &data); 697 if (ret) 698 goto restore_eerd; 699 buf[i] = data; 700 } 701 702 restore_eerd: 703 rv3028_exit_eerd(rv3028, eerd); 704 705 return ret; 706 } 707 708 #ifdef CONFIG_COMMON_CLK 709 #define clkout_hw_to_rv3028(hw) container_of(hw, struct rv3028_data, clkout_hw) 710 711 static int clkout_rates[] = { 712 32768, 713 8192, 714 1024, 715 64, 716 32, 717 1, 718 }; 719 720 static unsigned long rv3028_clkout_recalc_rate(struct clk_hw *hw, 721 unsigned long parent_rate) 722 { 723 int clkout, ret; 724 struct rv3028_data *rv3028 = clkout_hw_to_rv3028(hw); 725 726 ret = regmap_read(rv3028->regmap, RV3028_CLKOUT, &clkout); 727 if (ret < 0) 728 return 0; 729 730 clkout &= RV3028_CLKOUT_FD_MASK; 731 return clkout_rates[clkout]; 732 } 733 734 static long rv3028_clkout_round_rate(struct clk_hw *hw, unsigned long rate, 735 unsigned long *prate) 736 { 737 int i; 738 739 for (i = 0; i < ARRAY_SIZE(clkout_rates); i++) 740 if (clkout_rates[i] <= rate) 741 return clkout_rates[i]; 742 743 return 0; 744 } 745 746 static int rv3028_clkout_set_rate(struct clk_hw *hw, unsigned long rate, 747 unsigned long parent_rate) 748 { 749 int i, ret; 750 u32 enabled; 751 struct rv3028_data *rv3028 = clkout_hw_to_rv3028(hw); 752 753 ret = regmap_read(rv3028->regmap, RV3028_CLKOUT, &enabled); 754 if (ret < 0) 755 return ret; 756 757 ret = regmap_write(rv3028->regmap, RV3028_CLKOUT, 0x0); 758 if (ret < 0) 759 return ret; 760 761 enabled &= RV3028_CLKOUT_CLKOE; 762 763 for (i = 0; i < ARRAY_SIZE(clkout_rates); i++) 764 if (clkout_rates[i] == rate) 765 return rv3028_update_cfg(rv3028, RV3028_CLKOUT, 0xff, 766 RV3028_CLKOUT_CLKSY | enabled | i); 767 768 return -EINVAL; 769 } 770 771 static int rv3028_clkout_prepare(struct clk_hw *hw) 772 { 773 struct rv3028_data *rv3028 = clkout_hw_to_rv3028(hw); 774 775 return regmap_write(rv3028->regmap, RV3028_CLKOUT, 776 RV3028_CLKOUT_CLKSY | RV3028_CLKOUT_CLKOE); 777 } 778 779 static void rv3028_clkout_unprepare(struct clk_hw *hw) 780 { 781 struct rv3028_data *rv3028 = clkout_hw_to_rv3028(hw); 782 783 regmap_write(rv3028->regmap, RV3028_CLKOUT, 0x0); 784 regmap_update_bits(rv3028->regmap, RV3028_STATUS, 785 RV3028_STATUS_CLKF, 0); 786 } 787 788 static int rv3028_clkout_is_prepared(struct clk_hw *hw) 789 { 790 int clkout, ret; 791 struct rv3028_data *rv3028 = clkout_hw_to_rv3028(hw); 792 793 ret = regmap_read(rv3028->regmap, RV3028_CLKOUT, &clkout); 794 if (ret < 0) 795 return ret; 796 797 return !!(clkout & RV3028_CLKOUT_CLKOE); 798 } 799 800 static const struct clk_ops rv3028_clkout_ops = { 801 .prepare = rv3028_clkout_prepare, 802 .unprepare = rv3028_clkout_unprepare, 803 .is_prepared = rv3028_clkout_is_prepared, 804 .recalc_rate = rv3028_clkout_recalc_rate, 805 .round_rate = rv3028_clkout_round_rate, 806 .set_rate = rv3028_clkout_set_rate, 807 }; 808 809 static int rv3028_clkout_register_clk(struct rv3028_data *rv3028, 810 struct i2c_client *client) 811 { 812 int ret; 813 struct clk *clk; 814 struct clk_init_data init; 815 struct device_node *node = client->dev.of_node; 816 817 ret = regmap_update_bits(rv3028->regmap, RV3028_STATUS, 818 RV3028_STATUS_CLKF, 0); 819 if (ret < 0) 820 return ret; 821 822 init.name = "rv3028-clkout"; 823 init.ops = &rv3028_clkout_ops; 824 init.flags = 0; 825 init.parent_names = NULL; 826 init.num_parents = 0; 827 rv3028->clkout_hw.init = &init; 828 829 /* optional override of the clockname */ 830 of_property_read_string(node, "clock-output-names", &init.name); 831 832 /* register the clock */ 833 clk = devm_clk_register(&client->dev, &rv3028->clkout_hw); 834 if (!IS_ERR(clk)) 835 of_clk_add_provider(node, of_clk_src_simple_get, clk); 836 837 return 0; 838 } 839 #endif 840 841 static const struct rtc_class_ops rv3028_rtc_ops = { 842 .read_time = rv3028_get_time, 843 .set_time = rv3028_set_time, 844 .read_alarm = rv3028_get_alarm, 845 .set_alarm = rv3028_set_alarm, 846 .alarm_irq_enable = rv3028_alarm_irq_enable, 847 .read_offset = rv3028_read_offset, 848 .set_offset = rv3028_set_offset, 849 .ioctl = rv3028_ioctl, 850 .param_get = rv3028_param_get, 851 .param_set = rv3028_param_set, 852 }; 853 854 static const struct regmap_config regmap_config = { 855 .reg_bits = 8, 856 .val_bits = 8, 857 .max_register = 0x37, 858 }; 859 860 static u8 rv3028_set_trickle_charger(struct rv3028_data *rv3028, 861 struct i2c_client *client) 862 { 863 int ret, val_old, val; 864 u32 ohms, chargeable; 865 866 ret = regmap_read(rv3028->regmap, RV3028_BACKUP, &val_old); 867 if (ret < 0) 868 return ret; 869 870 /* mask out only trickle charger bits */ 871 val_old = val_old & (RV3028_BACKUP_TCE | RV3028_BACKUP_TCR_MASK); 872 val = val_old; 873 874 /* setup trickle charger */ 875 if (!device_property_read_u32(&client->dev, "trickle-resistor-ohms", 876 &ohms)) { 877 int i; 878 879 for (i = 0; i < ARRAY_SIZE(rv3028_trickle_resistors); i++) 880 if (ohms == rv3028_trickle_resistors[i]) 881 break; 882 883 if (i < ARRAY_SIZE(rv3028_trickle_resistors)) { 884 /* enable trickle charger and its resistor */ 885 val = RV3028_BACKUP_TCE | i; 886 } else { 887 dev_warn(&client->dev, "invalid trickle resistor value\n"); 888 } 889 } 890 891 if (!device_property_read_u32(&client->dev, "aux-voltage-chargeable", 892 &chargeable)) { 893 switch (chargeable) { 894 case 0: 895 val &= ~RV3028_BACKUP_TCE; 896 break; 897 case 1: 898 val |= RV3028_BACKUP_TCE; 899 break; 900 default: 901 dev_warn(&client->dev, 902 "unsupported aux-voltage-chargeable value\n"); 903 break; 904 } 905 } 906 907 /* only update EEPROM if changes are necessary */ 908 if (val_old != val) { 909 ret = rv3028_update_cfg(rv3028, RV3028_BACKUP, RV3028_BACKUP_TCE | 910 RV3028_BACKUP_TCR_MASK, val); 911 if (ret) 912 return ret; 913 } 914 915 return ret; 916 } 917 918 static int rv3028_probe(struct i2c_client *client) 919 { 920 struct rv3028_data *rv3028; 921 int ret, status; 922 struct nvmem_config nvmem_cfg = { 923 .name = "rv3028_nvram", 924 .word_size = 1, 925 .stride = 1, 926 .size = 2, 927 .type = NVMEM_TYPE_BATTERY_BACKED, 928 .reg_read = rv3028_nvram_read, 929 .reg_write = rv3028_nvram_write, 930 }; 931 struct nvmem_config eeprom_cfg = { 932 .name = "rv3028_eeprom", 933 .word_size = 1, 934 .stride = 1, 935 .size = 43, 936 .type = NVMEM_TYPE_EEPROM, 937 .reg_read = rv3028_eeprom_read, 938 .reg_write = rv3028_eeprom_write, 939 }; 940 941 rv3028 = devm_kzalloc(&client->dev, sizeof(struct rv3028_data), 942 GFP_KERNEL); 943 if (!rv3028) 944 return -ENOMEM; 945 946 rv3028->regmap = devm_regmap_init_i2c(client, ®map_config); 947 if (IS_ERR(rv3028->regmap)) 948 return PTR_ERR(rv3028->regmap); 949 950 i2c_set_clientdata(client, rv3028); 951 952 ret = regmap_read(rv3028->regmap, RV3028_STATUS, &status); 953 if (ret < 0) 954 return ret; 955 956 if (status & RV3028_STATUS_AF) 957 dev_warn(&client->dev, "An alarm may have been missed.\n"); 958 959 rv3028->rtc = devm_rtc_allocate_device(&client->dev); 960 if (IS_ERR(rv3028->rtc)) 961 return PTR_ERR(rv3028->rtc); 962 963 if (client->irq > 0) { 964 unsigned long flags; 965 966 /* 967 * If flags = 0, devm_request_threaded_irq() will use IRQ flags 968 * obtained from device tree. 969 */ 970 if (dev_fwnode(&client->dev)) 971 flags = 0; 972 else 973 flags = IRQF_TRIGGER_LOW; 974 975 ret = devm_request_threaded_irq(&client->dev, client->irq, 976 NULL, rv3028_handle_irq, 977 flags | IRQF_ONESHOT, 978 "rv3028", rv3028); 979 if (ret) { 980 dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n"); 981 client->irq = 0; 982 } 983 } 984 if (!client->irq) 985 clear_bit(RTC_FEATURE_ALARM, rv3028->rtc->features); 986 987 ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL1, 988 RV3028_CTRL1_WADA, RV3028_CTRL1_WADA); 989 if (ret) 990 return ret; 991 992 /* setup timestamping */ 993 ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL2, 994 RV3028_CTRL2_EIE | RV3028_CTRL2_TSE, 995 RV3028_CTRL2_EIE | RV3028_CTRL2_TSE); 996 if (ret) 997 return ret; 998 999 ret = rv3028_set_trickle_charger(rv3028, client); 1000 if (ret) 1001 return ret; 1002 1003 ret = rtc_add_group(rv3028->rtc, &rv3028_attr_group); 1004 if (ret) 1005 return ret; 1006 1007 set_bit(RTC_FEATURE_BACKUP_SWITCH_MODE, rv3028->rtc->features); 1008 1009 rv3028->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000; 1010 rv3028->rtc->range_max = RTC_TIMESTAMP_END_2099; 1011 rv3028->rtc->ops = &rv3028_rtc_ops; 1012 ret = devm_rtc_register_device(rv3028->rtc); 1013 if (ret) 1014 return ret; 1015 1016 nvmem_cfg.priv = rv3028->regmap; 1017 devm_rtc_nvmem_register(rv3028->rtc, &nvmem_cfg); 1018 eeprom_cfg.priv = rv3028; 1019 devm_rtc_nvmem_register(rv3028->rtc, &eeprom_cfg); 1020 1021 rv3028->rtc->max_user_freq = 1; 1022 1023 #ifdef CONFIG_COMMON_CLK 1024 rv3028_clkout_register_clk(rv3028, client); 1025 #endif 1026 return 0; 1027 } 1028 1029 static const struct acpi_device_id rv3028_i2c_acpi_match[] = { 1030 { "MCRY3028" }, 1031 { } 1032 }; 1033 MODULE_DEVICE_TABLE(acpi, rv3028_i2c_acpi_match); 1034 1035 static const __maybe_unused struct of_device_id rv3028_of_match[] = { 1036 { .compatible = "microcrystal,rv3028", }, 1037 { } 1038 }; 1039 MODULE_DEVICE_TABLE(of, rv3028_of_match); 1040 1041 static const struct i2c_device_id rv3028_id_table[] = { 1042 { .name = "rv3028", }, 1043 { } 1044 }; 1045 MODULE_DEVICE_TABLE(i2c, rv3028_id_table); 1046 1047 static struct i2c_driver rv3028_driver = { 1048 .driver = { 1049 .name = "rtc-rv3028", 1050 .acpi_match_table = rv3028_i2c_acpi_match, 1051 .of_match_table = of_match_ptr(rv3028_of_match), 1052 }, 1053 .id_table = rv3028_id_table, 1054 .probe = rv3028_probe, 1055 }; 1056 module_i2c_driver(rv3028_driver); 1057 1058 MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>"); 1059 MODULE_DESCRIPTION("Micro Crystal RV3028 RTC driver"); 1060 MODULE_LICENSE("GPL v2"); 1061