Lines Matching +full:battery +full:- +full:backed
1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright 2013 Til-Technologies
6 * Author: Renaud Cerrato <r.cerrato@til-technologies.fr>
16 * Datasheets: https://www.nxp.com/docs/en/data-sheet/PCF2127.pdf
17 * https://www.nxp.com/docs/en/data-sheet/PCF2131DS.pdf
73 * PCF2127 has 512 bytes general-purpose static RAM (SRAM) that is
74 * battery backed and can survive a power outage.
214 * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
226 ret = regmap_bulk_read(pcf2127->regmap, pcf2127->cfg->reg_time_base, in pcf2127_rtc_read_time()
241 return -EINVAL; in pcf2127_rtc_read_time()
249 tm->tm_sec = bcd2bin(buf[0] & 0x7F); in pcf2127_rtc_read_time()
250 tm->tm_min = bcd2bin(buf[1] & 0x7F); in pcf2127_rtc_read_time()
251 tm->tm_hour = bcd2bin(buf[2] & 0x3F); in pcf2127_rtc_read_time()
252 tm->tm_mday = bcd2bin(buf[3] & 0x3F); in pcf2127_rtc_read_time()
253 tm->tm_wday = buf[4] & 0x07; in pcf2127_rtc_read_time()
254 tm->tm_mon = bcd2bin(buf[5] & 0x1F) - 1; in pcf2127_rtc_read_time()
255 tm->tm_year = bcd2bin(buf[6]); in pcf2127_rtc_read_time()
256 tm->tm_year += 100; in pcf2127_rtc_read_time()
261 tm->tm_sec, tm->tm_min, tm->tm_hour, in pcf2127_rtc_read_time()
262 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); in pcf2127_rtc_read_time()
276 tm->tm_sec, tm->tm_min, tm->tm_hour, in pcf2127_rtc_set_time()
277 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); in pcf2127_rtc_set_time()
280 buf[i++] = bin2bcd(tm->tm_sec); /* this will also clear OSF flag */ in pcf2127_rtc_set_time()
281 buf[i++] = bin2bcd(tm->tm_min); in pcf2127_rtc_set_time()
282 buf[i++] = bin2bcd(tm->tm_hour); in pcf2127_rtc_set_time()
283 buf[i++] = bin2bcd(tm->tm_mday); in pcf2127_rtc_set_time()
284 buf[i++] = tm->tm_wday & 0x07; in pcf2127_rtc_set_time()
286 /* month, 1 - 12 */ in pcf2127_rtc_set_time()
287 buf[i++] = bin2bcd(tm->tm_mon + 1); in pcf2127_rtc_set_time()
290 buf[i++] = bin2bcd(tm->tm_year - 100); in pcf2127_rtc_set_time()
297 if (pcf2127->cfg->type == PCF2131) { in pcf2127_rtc_set_time()
298 err = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL1, in pcf2127_rtc_set_time()
306 err = regmap_write(pcf2127->regmap, PCF2131_REG_SR_RESET, in pcf2127_rtc_set_time()
315 err = regmap_bulk_write(pcf2127->regmap, pcf2127->cfg->reg_time_base, buf, i); in pcf2127_rtc_set_time()
321 if (pcf2127->cfg->type == PCF2131) { in pcf2127_rtc_set_time()
323 err = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL1, in pcf2127_rtc_set_time()
343 ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &val); in pcf2127_rtc_ioctl()
356 return regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL3, in pcf2127_rtc_ioctl()
360 return -ENOIOCTLCMD; in pcf2127_rtc_ioctl()
371 ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_ADDR_MSB, in pcf2127_nvmem_read()
376 return regmap_bulk_read(pcf2127->regmap, PCF2127_REG_RAM_RD_CMD, in pcf2127_nvmem_read()
387 ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_ADDR_MSB, in pcf2127_nvmem_write()
392 return regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_WRT_CMD, in pcf2127_nvmem_write()
407 wd_val = ((wdd->timeout * pcf2127->cfg->wdd_clock_hz_x1000) / 1000) + 1; in pcf2127_wdt_ping()
409 return regmap_write(pcf2127->regmap, pcf2127->cfg->reg_wd_val, wd_val); in pcf2127_wdt_ping()
426 dev_err(wdd->parent, in pcf2127_wdt_active_ping()
443 return regmap_write(pcf2127->regmap, pcf2127->cfg->reg_wd_val, in pcf2127_wdt_stop()
450 dev_dbg(wdd->parent, "new watchdog timeout: %is (old: %is)\n", in pcf2127_wdt_set_timeout()
451 new_timeout, wdd->timeout); in pcf2127_wdt_set_timeout()
453 wdd->timeout = new_timeout; in pcf2127_wdt_set_timeout()
478 * t = (n - 1) / f
485 return (1000 * (n - 1)) / f1000; in pcf2127_watchdog_get_period()
493 !device_property_read_bool(dev, "reset-source")) in pcf2127_watchdog_init()
496 pcf2127->wdd.parent = dev; in pcf2127_watchdog_init()
497 pcf2127->wdd.info = &pcf2127_wdt_info; in pcf2127_watchdog_init()
498 pcf2127->wdd.ops = &pcf2127_watchdog_ops; in pcf2127_watchdog_init()
500 pcf2127->wdd.min_timeout = in pcf2127_watchdog_init()
502 2, pcf2127->cfg->wdd_clock_hz_x1000); in pcf2127_watchdog_init()
503 pcf2127->wdd.max_timeout = in pcf2127_watchdog_init()
505 255, pcf2127->cfg->wdd_clock_hz_x1000); in pcf2127_watchdog_init()
506 pcf2127->wdd.timeout = PCF2127_WD_DEFAULT_TIMEOUT_S; in pcf2127_watchdog_init()
509 pcf2127->cfg->wdd_clock_hz_x1000); in pcf2127_watchdog_init()
511 pcf2127->wdd.min_hw_heartbeat_ms = pcf2127->cfg->wdd_min_hw_heartbeat_ms; in pcf2127_watchdog_init()
512 pcf2127->wdd.status = WATCHDOG_NOWAYOUT_INIT_STATUS; in pcf2127_watchdog_init()
514 watchdog_set_drvdata(&pcf2127->wdd, pcf2127); in pcf2127_watchdog_init()
517 if (pcf2127->cfg->wd_val_reg_readable) { in pcf2127_watchdog_init()
520 ret = regmap_read(pcf2127->regmap, pcf2127->cfg->reg_wd_val, in pcf2127_watchdog_init()
526 set_bit(WDOG_HW_RUNNING, &pcf2127->wdd.status); in pcf2127_watchdog_init()
529 return devm_watchdog_register_device(dev, &pcf2127->wdd); in pcf2127_watchdog_init()
540 ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL2, &ctrl2); in pcf2127_rtc_read_alarm()
544 ret = pcf2127_wdt_active_ping(&pcf2127->wdd); in pcf2127_rtc_read_alarm()
548 ret = regmap_bulk_read(pcf2127->regmap, pcf2127->cfg->regs_alarm_base, in pcf2127_rtc_read_alarm()
553 alrm->enabled = ctrl2 & PCF2127_BIT_CTRL2_AIE; in pcf2127_rtc_read_alarm()
554 alrm->pending = ctrl2 & PCF2127_BIT_CTRL2_AF; in pcf2127_rtc_read_alarm()
556 alrm->time.tm_sec = bcd2bin(buf[0] & 0x7F); in pcf2127_rtc_read_alarm()
557 alrm->time.tm_min = bcd2bin(buf[1] & 0x7F); in pcf2127_rtc_read_alarm()
558 alrm->time.tm_hour = bcd2bin(buf[2] & 0x3F); in pcf2127_rtc_read_alarm()
559 alrm->time.tm_mday = bcd2bin(buf[3] & 0x3F); in pcf2127_rtc_read_alarm()
569 ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL2, in pcf2127_rtc_alarm_irq_enable()
575 return pcf2127_wdt_active_ping(&pcf2127->wdd); in pcf2127_rtc_alarm_irq_enable()
584 ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL2, in pcf2127_rtc_set_alarm()
589 ret = pcf2127_wdt_active_ping(&pcf2127->wdd); in pcf2127_rtc_set_alarm()
593 buf[0] = bin2bcd(alrm->time.tm_sec); in pcf2127_rtc_set_alarm()
594 buf[1] = bin2bcd(alrm->time.tm_min); in pcf2127_rtc_set_alarm()
595 buf[2] = bin2bcd(alrm->time.tm_hour); in pcf2127_rtc_set_alarm()
596 buf[3] = bin2bcd(alrm->time.tm_mday); in pcf2127_rtc_set_alarm()
599 ret = regmap_bulk_write(pcf2127->regmap, pcf2127->cfg->regs_alarm_base, in pcf2127_rtc_set_alarm()
604 return pcf2127_rtc_alarm_irq_enable(dev, alrm->enabled); in pcf2127_rtc_set_alarm()
619 ret = regmap_bulk_read(pcf2127->regmap, pcf2127->cfg->ts[ts_id].reg_base, in pcf2127_rtc_ts_read()
634 /* TS_MO register (month) value range: 1-12 */ in pcf2127_rtc_ts_read()
635 tm.tm_mon = bcd2bin(data[5] & 0x1F) - 1; in pcf2127_rtc_ts_read()
655 if (ts_id >= pcf2127->cfg->ts_count) in pcf2127_rtc_ts_snapshot()
659 if (pcf2127->ts_valid[ts_id]) in pcf2127_rtc_ts_snapshot()
662 ret = pcf2127_rtc_ts_read(dev, &pcf2127->ts[ts_id], ts_id); in pcf2127_rtc_ts_snapshot()
664 pcf2127->ts_valid[ts_id] = true; in pcf2127_rtc_ts_snapshot()
673 ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL2, &ctrl2); in pcf2127_rtc_irq()
677 if (pcf2127->cfg->ts_count == 1) { in pcf2127_rtc_irq()
681 ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL1, &ctrl1); in pcf2127_rtc_irq()
692 regmap_write(pcf2127->regmap, PCF2127_REG_CTRL1, in pcf2127_rtc_irq()
696 regmap_write(pcf2127->regmap, PCF2127_REG_CTRL2, in pcf2127_rtc_irq()
702 ret = regmap_read(pcf2127->regmap, PCF2131_REG_CTRL4, &ctrl4); in pcf2127_rtc_irq()
713 for (i = 0; i < pcf2127->cfg->ts_count; i++) { in pcf2127_rtc_irq()
720 regmap_write(pcf2127->regmap, PCF2131_REG_CTRL4, in pcf2127_rtc_irq()
725 regmap_write(pcf2127->regmap, PCF2127_REG_CTRL2, in pcf2127_rtc_irq()
730 rtc_update_irq(pcf2127->rtc, 1, RTC_IRQF | RTC_AF); in pcf2127_rtc_irq()
732 pcf2127_wdt_active_ping(&pcf2127->wdd); in pcf2127_rtc_irq()
752 struct pcf2127 *pcf2127 = dev_get_drvdata(dev->parent); in timestamp_store()
755 if (ts_id >= pcf2127->cfg->ts_count) in timestamp_store()
758 if (pcf2127->irq_enabled) { in timestamp_store()
759 pcf2127->ts_valid[ts_id] = false; in timestamp_store()
762 ret = regmap_update_bits(pcf2127->regmap, in timestamp_store()
763 pcf2127->cfg->ts[ts_id].gnd_detect_reg, in timestamp_store()
764 pcf2127->cfg->ts[ts_id].gnd_detect_bit, in timestamp_store()
772 if (pcf2127->cfg->ts[ts_id].inter_detect_bit) { in timestamp_store()
774 ret = regmap_update_bits(pcf2127->regmap, in timestamp_store()
775 pcf2127->cfg->ts[ts_id].inter_detect_reg, in timestamp_store()
776 pcf2127->cfg->ts[ts_id].inter_detect_bit, in timestamp_store()
785 ret = pcf2127_wdt_active_ping(&pcf2127->wdd); in timestamp_store()
825 struct pcf2127 *pcf2127 = dev_get_drvdata(dev->parent); in timestamp_show()
829 if (ts_id >= pcf2127->cfg->ts_count) in timestamp_show()
832 if (pcf2127->irq_enabled) { in timestamp_show()
833 if (!pcf2127->ts_valid[ts_id]) in timestamp_show()
835 ts = pcf2127->ts[ts_id]; in timestamp_show()
844 ret = regmap_read(pcf2127->regmap, in timestamp_show()
845 pcf2127->cfg->ts[ts_id].gnd_detect_reg, in timestamp_show()
850 valid_low = ctrl & pcf2127->cfg->ts[ts_id].gnd_detect_bit; in timestamp_show()
852 if (pcf2127->cfg->ts[ts_id].inter_detect_bit) { in timestamp_show()
856 ret = regmap_read(pcf2127->regmap, in timestamp_show()
857 pcf2127->cfg->ts[ts_id].inter_detect_reg, in timestamp_show()
862 valid_inter = ctrl & pcf2127->cfg->ts[ts_id].inter_detect_bit; in timestamp_show()
868 ret = pcf2127_rtc_ts_read(dev->parent, &ts, ts_id); in timestamp_show()
872 ret = pcf2127_wdt_active_ping(&pcf2127->wdd); in timestamp_show()
1039 if (ts_id >= pcf2127->cfg->ts_count) { in pcf2127_enable_ts()
1042 return -EINVAL; in pcf2127_enable_ts()
1046 ret = regmap_update_bits(pcf2127->regmap, in pcf2127_enable_ts()
1047 pcf2127->cfg->ts[ts_id].reg_base, in pcf2127_enable_ts()
1059 * Interrupt signals are open-drain outputs and can be left floating if in pcf2127_enable_ts()
1062 ret = regmap_update_bits(pcf2127->regmap, pcf2127->cfg->ts[ts_id].ie_reg, in pcf2127_enable_ts()
1063 pcf2127->cfg->ts[ts_id].ie_bit, in pcf2127_enable_ts()
1064 pcf2127->cfg->ts[ts_id].ie_bit); in pcf2127_enable_ts()
1083 ret = regmap_write(pcf2127->regmap, in pcf2127_configure_interrupt_pins()
1088 ret = regmap_write(pcf2127->regmap, in pcf2127_configure_interrupt_pins()
1107 return -ENOMEM; in pcf2127_probe()
1109 pcf2127->regmap = regmap; in pcf2127_probe()
1110 pcf2127->cfg = config; in pcf2127_probe()
1114 pcf2127->rtc = devm_rtc_allocate_device(dev); in pcf2127_probe()
1115 if (IS_ERR(pcf2127->rtc)) in pcf2127_probe()
1116 return PTR_ERR(pcf2127->rtc); in pcf2127_probe()
1118 pcf2127->rtc->ops = &pcf2127_rtc_ops; in pcf2127_probe()
1119 pcf2127->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000; in pcf2127_probe()
1120 pcf2127->rtc->range_max = RTC_TIMESTAMP_END_2099; in pcf2127_probe()
1121 pcf2127->rtc->set_start_time = true; /* Sets actual start to 1970 */ in pcf2127_probe()
1127 if (pcf2127->cfg->type == PCF2127 || pcf2127->cfg->type == PCF2129) { in pcf2127_probe()
1128 set_bit(RTC_FEATURE_ALARM_RES_2S, pcf2127->rtc->features); in pcf2127_probe()
1129 clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, pcf2127->rtc->features); in pcf2127_probe()
1132 clear_bit(RTC_FEATURE_ALARM, pcf2127->rtc->features); in pcf2127_probe()
1154 pcf2127->irq_enabled = true; in pcf2127_probe()
1157 if (alarm_irq > 0 || device_property_read_bool(dev, "wakeup-source")) { in pcf2127_probe()
1159 set_bit(RTC_FEATURE_ALARM, pcf2127->rtc->features); in pcf2127_probe()
1162 if (pcf2127->cfg->has_int_a_b) { in pcf2127_probe()
1171 if (pcf2127->cfg->has_nvmem) { in pcf2127_probe()
1179 ret = devm_rtc_nvmem_register(pcf2127->rtc, &nvmem_cfg); in pcf2127_probe()
1183 * The "Power-On Reset Override" facility prevents the RTC to do a reset in pcf2127_probe()
1186 ret = regmap_clear_bits(pcf2127->regmap, PCF2127_REG_CTRL1, in pcf2127_probe()
1191 ret = regmap_read(pcf2127->regmap, pcf2127->cfg->reg_clkout, &val); in pcf2127_probe()
1196 ret = regmap_set_bits(pcf2127->regmap, pcf2127->cfg->reg_clkout, in pcf2127_probe()
1213 ret = regmap_update_bits(pcf2127->regmap, pcf2127->cfg->reg_wd_ctl, in pcf2127_probe()
1219 (pcf2127->cfg->has_bit_wd_ctl_cd0 ? PCF2127_BIT_WD_CTL_CD0 : 0) | in pcf2127_probe()
1229 * Disable battery low/switch-over timestamp and interrupts. in pcf2127_probe()
1230 * Clear battery interrupt flags which can block new trigger events. in pcf2127_probe()
1234 ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL3, in pcf2127_probe()
1247 for (int i = 0; i < pcf2127->cfg->ts_count; i++) { in pcf2127_probe()
1253 ret = rtc_add_group(pcf2127->rtc, &pcf2127->cfg->attribute_group); in pcf2127_probe()
1260 return devm_rtc_register_device(pcf2127->rtc); in pcf2127_probe()
1284 return ret < 0 ? ret : -EIO; in pcf2127_i2c_write()
1299 return -EINVAL; in pcf2127_i2c_gather_write()
1303 return -ENOMEM; in pcf2127_i2c_gather_write()
1313 return ret < 0 ? ret : -EIO; in pcf2127_i2c_gather_write()
1326 return -EINVAL; in pcf2127_i2c_read()
1330 return ret < 0 ? ret : -EIO; in pcf2127_i2c_read()
1334 return ret < 0 ? ret : -EIO; in pcf2127_i2c_read()
1370 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) in pcf2127_i2c_probe()
1371 return -ENODEV; in pcf2127_i2c_probe()
1373 if (client->dev.of_node) { in pcf2127_i2c_probe()
1374 variant = of_device_get_match_data(&client->dev); in pcf2127_i2c_probe()
1376 return -ENODEV; in pcf2127_i2c_probe()
1379 i2c_match_id(pcf2127_i2c_id, client)->driver_data; in pcf2127_i2c_probe()
1382 return -ENODEV; in pcf2127_i2c_probe()
1386 config.max_register = variant->max_register, in pcf2127_i2c_probe()
1388 regmap = devm_regmap_init(&client->dev, &pcf2127_i2c_regmap, in pcf2127_i2c_probe()
1389 &client->dev, &config); in pcf2127_i2c_probe()
1391 dev_err(&client->dev, "%s: regmap allocation failed: %ld\n", in pcf2127_i2c_probe()
1396 return pcf2127_probe(&client->dev, regmap, client->irq, variant); in pcf2127_i2c_probe()
1401 .name = "rtc-pcf2127-i2c",
1447 if (spi->dev.of_node) { in pcf2127_spi_probe()
1448 variant = of_device_get_match_data(&spi->dev); in pcf2127_spi_probe()
1450 return -ENODEV; in pcf2127_spi_probe()
1452 enum pcf21xx_type type = spi_get_device_id(spi)->driver_data; in pcf2127_spi_probe()
1455 return -ENODEV; in pcf2127_spi_probe()
1459 config.max_register = variant->max_register, in pcf2127_spi_probe()
1463 dev_err(&spi->dev, "%s: regmap allocation failed: %ld\n", in pcf2127_spi_probe()
1468 return pcf2127_probe(&spi->dev, regmap, spi->irq, variant); in pcf2127_spi_probe()
1482 .name = "rtc-pcf2127-spi",
1539 MODULE_AUTHOR("Renaud Cerrato <r.cerrato@til-technologies.fr>");