rtc-rv8803.c (15438B)
1// SPDX-License-Identifier: GPL-2.0 2/* 3 * RTC driver for the Micro Crystal RV8803 4 * 5 * Copyright (C) 2015 Micro Crystal SA 6 * Alexandre Belloni <alexandre.belloni@bootlin.com> 7 * 8 */ 9 10#include <linux/bcd.h> 11#include <linux/bitops.h> 12#include <linux/log2.h> 13#include <linux/i2c.h> 14#include <linux/interrupt.h> 15#include <linux/kernel.h> 16#include <linux/module.h> 17#include <linux/of_device.h> 18#include <linux/rtc.h> 19 20#define RV8803_I2C_TRY_COUNT 4 21 22#define RV8803_SEC 0x00 23#define RV8803_MIN 0x01 24#define RV8803_HOUR 0x02 25#define RV8803_WEEK 0x03 26#define RV8803_DAY 0x04 27#define RV8803_MONTH 0x05 28#define RV8803_YEAR 0x06 29#define RV8803_RAM 0x07 30#define RV8803_ALARM_MIN 0x08 31#define RV8803_ALARM_HOUR 0x09 32#define RV8803_ALARM_WEEK_OR_DAY 0x0A 33#define RV8803_EXT 0x0D 34#define RV8803_FLAG 0x0E 35#define RV8803_CTRL 0x0F 36 37#define RV8803_EXT_WADA BIT(6) 38 39#define RV8803_FLAG_V1F BIT(0) 40#define RV8803_FLAG_V2F BIT(1) 41#define RV8803_FLAG_AF BIT(3) 42#define RV8803_FLAG_TF BIT(4) 43#define RV8803_FLAG_UF BIT(5) 44 45#define RV8803_CTRL_RESET BIT(0) 46 47#define RV8803_CTRL_EIE BIT(2) 48#define RV8803_CTRL_AIE BIT(3) 49#define RV8803_CTRL_TIE BIT(4) 50#define RV8803_CTRL_UIE BIT(5) 51 52#define RX8900_BACKUP_CTRL 0x18 53#define RX8900_FLAG_SWOFF BIT(2) 54#define RX8900_FLAG_VDETOFF BIT(3) 55 56enum rv8803_type { 57 rv_8803, 58 rx_8804, 59 rx_8900 60}; 61 62struct rv8803_data { 63 struct i2c_client *client; 64 struct rtc_device *rtc; 65 struct mutex flags_lock; 66 u8 ctrl; 67 enum rv8803_type type; 68}; 69 70static int rv8803_read_reg(const struct i2c_client *client, u8 reg) 71{ 72 int try = RV8803_I2C_TRY_COUNT; 73 s32 ret; 74 75 /* 76 * There is a 61µs window during which the RTC does not acknowledge I2C 77 * transfers. In that case, ensure that there are multiple attempts. 78 */ 79 do 80 ret = i2c_smbus_read_byte_data(client, reg); 81 while ((ret == -ENXIO || ret == -EIO) && --try); 82 if (ret < 0) 83 dev_err(&client->dev, "Unable to read register 0x%02x\n", reg); 84 85 return ret; 86} 87 88static int rv8803_read_regs(const struct i2c_client *client, 89 u8 reg, u8 count, u8 *values) 90{ 91 int try = RV8803_I2C_TRY_COUNT; 92 s32 ret; 93 94 do 95 ret = i2c_smbus_read_i2c_block_data(client, reg, count, values); 96 while ((ret == -ENXIO || ret == -EIO) && --try); 97 if (ret != count) { 98 dev_err(&client->dev, 99 "Unable to read registers 0x%02x..0x%02x\n", 100 reg, reg + count - 1); 101 return ret < 0 ? ret : -EIO; 102 } 103 104 return 0; 105} 106 107static int rv8803_write_reg(const struct i2c_client *client, u8 reg, u8 value) 108{ 109 int try = RV8803_I2C_TRY_COUNT; 110 s32 ret; 111 112 do 113 ret = i2c_smbus_write_byte_data(client, reg, value); 114 while ((ret == -ENXIO || ret == -EIO) && --try); 115 if (ret) 116 dev_err(&client->dev, "Unable to write register 0x%02x\n", reg); 117 118 return ret; 119} 120 121static int rv8803_write_regs(const struct i2c_client *client, 122 u8 reg, u8 count, const u8 *values) 123{ 124 int try = RV8803_I2C_TRY_COUNT; 125 s32 ret; 126 127 do 128 ret = i2c_smbus_write_i2c_block_data(client, reg, count, 129 values); 130 while ((ret == -ENXIO || ret == -EIO) && --try); 131 if (ret) 132 dev_err(&client->dev, 133 "Unable to write registers 0x%02x..0x%02x\n", 134 reg, reg + count - 1); 135 136 return ret; 137} 138 139static irqreturn_t rv8803_handle_irq(int irq, void *dev_id) 140{ 141 struct i2c_client *client = dev_id; 142 struct rv8803_data *rv8803 = i2c_get_clientdata(client); 143 unsigned long events = 0; 144 int flags; 145 146 mutex_lock(&rv8803->flags_lock); 147 148 flags = rv8803_read_reg(client, RV8803_FLAG); 149 if (flags <= 0) { 150 mutex_unlock(&rv8803->flags_lock); 151 return IRQ_NONE; 152 } 153 154 if (flags & RV8803_FLAG_V1F) 155 dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n"); 156 157 if (flags & RV8803_FLAG_V2F) 158 dev_warn(&client->dev, "Voltage low, data loss detected.\n"); 159 160 if (flags & RV8803_FLAG_TF) { 161 flags &= ~RV8803_FLAG_TF; 162 rv8803->ctrl &= ~RV8803_CTRL_TIE; 163 events |= RTC_PF; 164 } 165 166 if (flags & RV8803_FLAG_AF) { 167 flags &= ~RV8803_FLAG_AF; 168 rv8803->ctrl &= ~RV8803_CTRL_AIE; 169 events |= RTC_AF; 170 } 171 172 if (flags & RV8803_FLAG_UF) { 173 flags &= ~RV8803_FLAG_UF; 174 rv8803->ctrl &= ~RV8803_CTRL_UIE; 175 events |= RTC_UF; 176 } 177 178 if (events) { 179 rtc_update_irq(rv8803->rtc, 1, events); 180 rv8803_write_reg(client, RV8803_FLAG, flags); 181 rv8803_write_reg(rv8803->client, RV8803_CTRL, rv8803->ctrl); 182 } 183 184 mutex_unlock(&rv8803->flags_lock); 185 186 return IRQ_HANDLED; 187} 188 189static int rv8803_get_time(struct device *dev, struct rtc_time *tm) 190{ 191 struct rv8803_data *rv8803 = dev_get_drvdata(dev); 192 u8 date1[7]; 193 u8 date2[7]; 194 u8 *date = date1; 195 int ret, flags; 196 197 flags = rv8803_read_reg(rv8803->client, RV8803_FLAG); 198 if (flags < 0) 199 return flags; 200 201 if (flags & RV8803_FLAG_V2F) { 202 dev_warn(dev, "Voltage low, data is invalid.\n"); 203 return -EINVAL; 204 } 205 206 ret = rv8803_read_regs(rv8803->client, RV8803_SEC, 7, date); 207 if (ret) 208 return ret; 209 210 if ((date1[RV8803_SEC] & 0x7f) == bin2bcd(59)) { 211 ret = rv8803_read_regs(rv8803->client, RV8803_SEC, 7, date2); 212 if (ret) 213 return ret; 214 215 if ((date2[RV8803_SEC] & 0x7f) != bin2bcd(59)) 216 date = date2; 217 } 218 219 tm->tm_sec = bcd2bin(date[RV8803_SEC] & 0x7f); 220 tm->tm_min = bcd2bin(date[RV8803_MIN] & 0x7f); 221 tm->tm_hour = bcd2bin(date[RV8803_HOUR] & 0x3f); 222 tm->tm_wday = ilog2(date[RV8803_WEEK] & 0x7f); 223 tm->tm_mday = bcd2bin(date[RV8803_DAY] & 0x3f); 224 tm->tm_mon = bcd2bin(date[RV8803_MONTH] & 0x1f) - 1; 225 tm->tm_year = bcd2bin(date[RV8803_YEAR]) + 100; 226 227 return 0; 228} 229 230static int rv8803_set_time(struct device *dev, struct rtc_time *tm) 231{ 232 struct rv8803_data *rv8803 = dev_get_drvdata(dev); 233 u8 date[7]; 234 int ctrl, flags, ret; 235 236 ctrl = rv8803_read_reg(rv8803->client, RV8803_CTRL); 237 if (ctrl < 0) 238 return ctrl; 239 240 /* Stop the clock */ 241 ret = rv8803_write_reg(rv8803->client, RV8803_CTRL, 242 ctrl | RV8803_CTRL_RESET); 243 if (ret) 244 return ret; 245 246 date[RV8803_SEC] = bin2bcd(tm->tm_sec); 247 date[RV8803_MIN] = bin2bcd(tm->tm_min); 248 date[RV8803_HOUR] = bin2bcd(tm->tm_hour); 249 date[RV8803_WEEK] = 1 << (tm->tm_wday); 250 date[RV8803_DAY] = bin2bcd(tm->tm_mday); 251 date[RV8803_MONTH] = bin2bcd(tm->tm_mon + 1); 252 date[RV8803_YEAR] = bin2bcd(tm->tm_year - 100); 253 254 ret = rv8803_write_regs(rv8803->client, RV8803_SEC, 7, date); 255 if (ret) 256 return ret; 257 258 /* Restart the clock */ 259 ret = rv8803_write_reg(rv8803->client, RV8803_CTRL, 260 ctrl & ~RV8803_CTRL_RESET); 261 if (ret) 262 return ret; 263 264 mutex_lock(&rv8803->flags_lock); 265 266 flags = rv8803_read_reg(rv8803->client, RV8803_FLAG); 267 if (flags < 0) { 268 mutex_unlock(&rv8803->flags_lock); 269 return flags; 270 } 271 272 ret = rv8803_write_reg(rv8803->client, RV8803_FLAG, 273 flags & ~(RV8803_FLAG_V1F | RV8803_FLAG_V2F)); 274 275 mutex_unlock(&rv8803->flags_lock); 276 277 return ret; 278} 279 280static int rv8803_get_alarm(struct device *dev, struct rtc_wkalrm *alrm) 281{ 282 struct rv8803_data *rv8803 = dev_get_drvdata(dev); 283 struct i2c_client *client = rv8803->client; 284 u8 alarmvals[3]; 285 int flags, ret; 286 287 ret = rv8803_read_regs(client, RV8803_ALARM_MIN, 3, alarmvals); 288 if (ret) 289 return ret; 290 291 flags = rv8803_read_reg(client, RV8803_FLAG); 292 if (flags < 0) 293 return flags; 294 295 alrm->time.tm_sec = 0; 296 alrm->time.tm_min = bcd2bin(alarmvals[0] & 0x7f); 297 alrm->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f); 298 alrm->time.tm_mday = bcd2bin(alarmvals[2] & 0x3f); 299 300 alrm->enabled = !!(rv8803->ctrl & RV8803_CTRL_AIE); 301 alrm->pending = (flags & RV8803_FLAG_AF) && alrm->enabled; 302 303 return 0; 304} 305 306static int rv8803_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) 307{ 308 struct i2c_client *client = to_i2c_client(dev); 309 struct rv8803_data *rv8803 = dev_get_drvdata(dev); 310 u8 alarmvals[3]; 311 u8 ctrl[2]; 312 int ret, err; 313 314 /* The alarm has no seconds, round up to nearest minute */ 315 if (alrm->time.tm_sec) { 316 time64_t alarm_time = rtc_tm_to_time64(&alrm->time); 317 318 alarm_time += 60 - alrm->time.tm_sec; 319 rtc_time64_to_tm(alarm_time, &alrm->time); 320 } 321 322 mutex_lock(&rv8803->flags_lock); 323 324 ret = rv8803_read_regs(client, RV8803_FLAG, 2, ctrl); 325 if (ret) { 326 mutex_unlock(&rv8803->flags_lock); 327 return ret; 328 } 329 330 alarmvals[0] = bin2bcd(alrm->time.tm_min); 331 alarmvals[1] = bin2bcd(alrm->time.tm_hour); 332 alarmvals[2] = bin2bcd(alrm->time.tm_mday); 333 334 if (rv8803->ctrl & (RV8803_CTRL_AIE | RV8803_CTRL_UIE)) { 335 rv8803->ctrl &= ~(RV8803_CTRL_AIE | RV8803_CTRL_UIE); 336 err = rv8803_write_reg(rv8803->client, RV8803_CTRL, 337 rv8803->ctrl); 338 if (err) { 339 mutex_unlock(&rv8803->flags_lock); 340 return err; 341 } 342 } 343 344 ctrl[0] &= ~RV8803_FLAG_AF; 345 err = rv8803_write_reg(rv8803->client, RV8803_FLAG, ctrl[0]); 346 mutex_unlock(&rv8803->flags_lock); 347 if (err) 348 return err; 349 350 err = rv8803_write_regs(rv8803->client, RV8803_ALARM_MIN, 3, alarmvals); 351 if (err) 352 return err; 353 354 if (alrm->enabled) { 355 if (rv8803->rtc->uie_rtctimer.enabled) 356 rv8803->ctrl |= RV8803_CTRL_UIE; 357 if (rv8803->rtc->aie_timer.enabled) 358 rv8803->ctrl |= RV8803_CTRL_AIE; 359 360 err = rv8803_write_reg(rv8803->client, RV8803_CTRL, 361 rv8803->ctrl); 362 if (err) 363 return err; 364 } 365 366 return 0; 367} 368 369static int rv8803_alarm_irq_enable(struct device *dev, unsigned int enabled) 370{ 371 struct i2c_client *client = to_i2c_client(dev); 372 struct rv8803_data *rv8803 = dev_get_drvdata(dev); 373 int ctrl, flags, err; 374 375 ctrl = rv8803->ctrl; 376 377 if (enabled) { 378 if (rv8803->rtc->uie_rtctimer.enabled) 379 ctrl |= RV8803_CTRL_UIE; 380 if (rv8803->rtc->aie_timer.enabled) 381 ctrl |= RV8803_CTRL_AIE; 382 } else { 383 if (!rv8803->rtc->uie_rtctimer.enabled) 384 ctrl &= ~RV8803_CTRL_UIE; 385 if (!rv8803->rtc->aie_timer.enabled) 386 ctrl &= ~RV8803_CTRL_AIE; 387 } 388 389 mutex_lock(&rv8803->flags_lock); 390 flags = rv8803_read_reg(client, RV8803_FLAG); 391 if (flags < 0) { 392 mutex_unlock(&rv8803->flags_lock); 393 return flags; 394 } 395 flags &= ~(RV8803_FLAG_AF | RV8803_FLAG_UF); 396 err = rv8803_write_reg(client, RV8803_FLAG, flags); 397 mutex_unlock(&rv8803->flags_lock); 398 if (err) 399 return err; 400 401 if (ctrl != rv8803->ctrl) { 402 rv8803->ctrl = ctrl; 403 err = rv8803_write_reg(client, RV8803_CTRL, rv8803->ctrl); 404 if (err) 405 return err; 406 } 407 408 return 0; 409} 410 411static int rv8803_ioctl(struct device *dev, unsigned int cmd, unsigned long arg) 412{ 413 struct i2c_client *client = to_i2c_client(dev); 414 struct rv8803_data *rv8803 = dev_get_drvdata(dev); 415 unsigned int vl = 0; 416 int flags, ret = 0; 417 418 switch (cmd) { 419 case RTC_VL_READ: 420 flags = rv8803_read_reg(client, RV8803_FLAG); 421 if (flags < 0) 422 return flags; 423 424 if (flags & RV8803_FLAG_V1F) { 425 dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n"); 426 vl = RTC_VL_ACCURACY_LOW; 427 } 428 429 if (flags & RV8803_FLAG_V2F) 430 vl |= RTC_VL_DATA_INVALID; 431 432 return put_user(vl, (unsigned int __user *)arg); 433 434 case RTC_VL_CLR: 435 mutex_lock(&rv8803->flags_lock); 436 flags = rv8803_read_reg(client, RV8803_FLAG); 437 if (flags < 0) { 438 mutex_unlock(&rv8803->flags_lock); 439 return flags; 440 } 441 442 flags &= ~RV8803_FLAG_V1F; 443 ret = rv8803_write_reg(client, RV8803_FLAG, flags); 444 mutex_unlock(&rv8803->flags_lock); 445 if (ret) 446 return ret; 447 448 return 0; 449 450 default: 451 return -ENOIOCTLCMD; 452 } 453} 454 455static int rv8803_nvram_write(void *priv, unsigned int offset, void *val, 456 size_t bytes) 457{ 458 return rv8803_write_reg(priv, RV8803_RAM, *(u8 *)val); 459} 460 461static int rv8803_nvram_read(void *priv, unsigned int offset, 462 void *val, size_t bytes) 463{ 464 int ret; 465 466 ret = rv8803_read_reg(priv, RV8803_RAM); 467 if (ret < 0) 468 return ret; 469 470 *(u8 *)val = ret; 471 472 return 0; 473} 474 475static const struct rtc_class_ops rv8803_rtc_ops = { 476 .read_time = rv8803_get_time, 477 .set_time = rv8803_set_time, 478 .ioctl = rv8803_ioctl, 479 .read_alarm = rv8803_get_alarm, 480 .set_alarm = rv8803_set_alarm, 481 .alarm_irq_enable = rv8803_alarm_irq_enable, 482}; 483 484static int rx8900_trickle_charger_init(struct rv8803_data *rv8803) 485{ 486 struct i2c_client *client = rv8803->client; 487 struct device_node *node = client->dev.of_node; 488 int err; 489 u8 flags; 490 491 if (!node) 492 return 0; 493 494 if (rv8803->type != rx_8900) 495 return 0; 496 497 err = i2c_smbus_read_byte_data(rv8803->client, RX8900_BACKUP_CTRL); 498 if (err < 0) 499 return err; 500 501 flags = ~(RX8900_FLAG_VDETOFF | RX8900_FLAG_SWOFF) & (u8)err; 502 503 if (of_property_read_bool(node, "epson,vdet-disable")) 504 flags |= RX8900_FLAG_VDETOFF; 505 506 if (of_property_read_bool(node, "trickle-diode-disable")) 507 flags |= RX8900_FLAG_SWOFF; 508 509 return i2c_smbus_write_byte_data(rv8803->client, RX8900_BACKUP_CTRL, 510 flags); 511} 512 513static int rv8803_probe(struct i2c_client *client, 514 const struct i2c_device_id *id) 515{ 516 struct i2c_adapter *adapter = client->adapter; 517 struct rv8803_data *rv8803; 518 int err, flags; 519 struct nvmem_config nvmem_cfg = { 520 .name = "rv8803_nvram", 521 .word_size = 1, 522 .stride = 1, 523 .size = 1, 524 .reg_read = rv8803_nvram_read, 525 .reg_write = rv8803_nvram_write, 526 .priv = client, 527 }; 528 529 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | 530 I2C_FUNC_SMBUS_I2C_BLOCK)) { 531 dev_err(&adapter->dev, "doesn't support I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_I2C_BLOCK\n"); 532 return -EIO; 533 } 534 535 rv8803 = devm_kzalloc(&client->dev, sizeof(struct rv8803_data), 536 GFP_KERNEL); 537 if (!rv8803) 538 return -ENOMEM; 539 540 mutex_init(&rv8803->flags_lock); 541 rv8803->client = client; 542 if (client->dev.of_node) 543 rv8803->type = (enum rv8803_type) 544 of_device_get_match_data(&client->dev); 545 else 546 rv8803->type = id->driver_data; 547 i2c_set_clientdata(client, rv8803); 548 549 flags = rv8803_read_reg(client, RV8803_FLAG); 550 if (flags < 0) 551 return flags; 552 553 if (flags & RV8803_FLAG_V1F) 554 dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n"); 555 556 if (flags & RV8803_FLAG_V2F) 557 dev_warn(&client->dev, "Voltage low, data loss detected.\n"); 558 559 if (flags & RV8803_FLAG_AF) 560 dev_warn(&client->dev, "An alarm maybe have been missed.\n"); 561 562 rv8803->rtc = devm_rtc_allocate_device(&client->dev); 563 if (IS_ERR(rv8803->rtc)) 564 return PTR_ERR(rv8803->rtc); 565 566 if (client->irq > 0) { 567 err = devm_request_threaded_irq(&client->dev, client->irq, 568 NULL, rv8803_handle_irq, 569 IRQF_TRIGGER_LOW | IRQF_ONESHOT, 570 "rv8803", client); 571 if (err) { 572 dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n"); 573 client->irq = 0; 574 } 575 } 576 if (!client->irq) 577 clear_bit(RTC_FEATURE_ALARM, rv8803->rtc->features); 578 579 err = rv8803_write_reg(rv8803->client, RV8803_EXT, RV8803_EXT_WADA); 580 if (err) 581 return err; 582 583 err = rx8900_trickle_charger_init(rv8803); 584 if (err) { 585 dev_err(&client->dev, "failed to init charger\n"); 586 return err; 587 } 588 589 rv8803->rtc->ops = &rv8803_rtc_ops; 590 rv8803->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000; 591 rv8803->rtc->range_max = RTC_TIMESTAMP_END_2099; 592 err = devm_rtc_register_device(rv8803->rtc); 593 if (err) 594 return err; 595 596 devm_rtc_nvmem_register(rv8803->rtc, &nvmem_cfg); 597 598 rv8803->rtc->max_user_freq = 1; 599 600 return 0; 601} 602 603static const struct i2c_device_id rv8803_id[] = { 604 { "rv8803", rv_8803 }, 605 { "rv8804", rx_8804 }, 606 { "rx8803", rv_8803 }, 607 { "rx8900", rx_8900 }, 608 { } 609}; 610MODULE_DEVICE_TABLE(i2c, rv8803_id); 611 612static const __maybe_unused struct of_device_id rv8803_of_match[] = { 613 { 614 .compatible = "microcrystal,rv8803", 615 .data = (void *)rv_8803 616 }, 617 { 618 .compatible = "epson,rx8803", 619 .data = (void *)rv_8803 620 }, 621 { 622 .compatible = "epson,rx8804", 623 .data = (void *)rx_8804 624 }, 625 { 626 .compatible = "epson,rx8900", 627 .data = (void *)rx_8900 628 }, 629 { } 630}; 631MODULE_DEVICE_TABLE(of, rv8803_of_match); 632 633static struct i2c_driver rv8803_driver = { 634 .driver = { 635 .name = "rtc-rv8803", 636 .of_match_table = of_match_ptr(rv8803_of_match), 637 }, 638 .probe = rv8803_probe, 639 .id_table = rv8803_id, 640}; 641module_i2c_driver(rv8803_driver); 642 643MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>"); 644MODULE_DESCRIPTION("Micro Crystal RV8803 RTC driver"); 645MODULE_LICENSE("GPL v2");