18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Driver for TI BQ32000 RTC. 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2009 Semihalf. 68c2ecf20Sopenharmony_ci * Copyright (C) 2014 Pavel Machek <pavel@denx.de> 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * You can get hardware description at 98c2ecf20Sopenharmony_ci * https://www.ti.com/lit/ds/symlink/bq32000.pdf 108c2ecf20Sopenharmony_ci */ 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#include <linux/module.h> 138c2ecf20Sopenharmony_ci#include <linux/i2c.h> 148c2ecf20Sopenharmony_ci#include <linux/rtc.h> 158c2ecf20Sopenharmony_ci#include <linux/init.h> 168c2ecf20Sopenharmony_ci#include <linux/errno.h> 178c2ecf20Sopenharmony_ci#include <linux/bcd.h> 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci#define BQ32K_SECONDS 0x00 /* Seconds register address */ 208c2ecf20Sopenharmony_ci#define BQ32K_SECONDS_MASK 0x7F /* Mask over seconds value */ 218c2ecf20Sopenharmony_ci#define BQ32K_STOP 0x80 /* Oscillator Stop flat */ 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci#define BQ32K_MINUTES 0x01 /* Minutes register address */ 248c2ecf20Sopenharmony_ci#define BQ32K_MINUTES_MASK 0x7F /* Mask over minutes value */ 258c2ecf20Sopenharmony_ci#define BQ32K_OF 0x80 /* Oscillator Failure flag */ 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci#define BQ32K_HOURS_MASK 0x3F /* Mask over hours value */ 288c2ecf20Sopenharmony_ci#define BQ32K_CENT 0x40 /* Century flag */ 298c2ecf20Sopenharmony_ci#define BQ32K_CENT_EN 0x80 /* Century flag enable bit */ 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci#define BQ32K_CALIBRATION 0x07 /* CAL_CFG1, calibration and control */ 328c2ecf20Sopenharmony_ci#define BQ32K_TCH2 0x08 /* Trickle charge enable */ 338c2ecf20Sopenharmony_ci#define BQ32K_CFG2 0x09 /* Trickle charger control */ 348c2ecf20Sopenharmony_ci#define BQ32K_TCFE BIT(6) /* Trickle charge FET bypass */ 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci#define MAX_LEN 10 /* Maximum number of consecutive 378c2ecf20Sopenharmony_ci * register for this particular RTC. 388c2ecf20Sopenharmony_ci */ 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_cistruct bq32k_regs { 418c2ecf20Sopenharmony_ci uint8_t seconds; 428c2ecf20Sopenharmony_ci uint8_t minutes; 438c2ecf20Sopenharmony_ci uint8_t cent_hours; 448c2ecf20Sopenharmony_ci uint8_t day; 458c2ecf20Sopenharmony_ci uint8_t date; 468c2ecf20Sopenharmony_ci uint8_t month; 478c2ecf20Sopenharmony_ci uint8_t years; 488c2ecf20Sopenharmony_ci}; 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_cistatic struct i2c_driver bq32k_driver; 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_cistatic int bq32k_read(struct device *dev, void *data, uint8_t off, uint8_t len) 538c2ecf20Sopenharmony_ci{ 548c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 558c2ecf20Sopenharmony_ci struct i2c_msg msgs[] = { 568c2ecf20Sopenharmony_ci { 578c2ecf20Sopenharmony_ci .addr = client->addr, 588c2ecf20Sopenharmony_ci .flags = 0, 598c2ecf20Sopenharmony_ci .len = 1, 608c2ecf20Sopenharmony_ci .buf = &off, 618c2ecf20Sopenharmony_ci }, { 628c2ecf20Sopenharmony_ci .addr = client->addr, 638c2ecf20Sopenharmony_ci .flags = I2C_M_RD, 648c2ecf20Sopenharmony_ci .len = len, 658c2ecf20Sopenharmony_ci .buf = data, 668c2ecf20Sopenharmony_ci } 678c2ecf20Sopenharmony_ci }; 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci if (i2c_transfer(client->adapter, msgs, 2) == 2) 708c2ecf20Sopenharmony_ci return 0; 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci return -EIO; 738c2ecf20Sopenharmony_ci} 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_cistatic int bq32k_write(struct device *dev, void *data, uint8_t off, uint8_t len) 768c2ecf20Sopenharmony_ci{ 778c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 788c2ecf20Sopenharmony_ci uint8_t buffer[MAX_LEN + 1]; 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci buffer[0] = off; 818c2ecf20Sopenharmony_ci memcpy(&buffer[1], data, len); 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci if (i2c_master_send(client, buffer, len + 1) == len + 1) 848c2ecf20Sopenharmony_ci return 0; 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci return -EIO; 878c2ecf20Sopenharmony_ci} 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_cistatic int bq32k_rtc_read_time(struct device *dev, struct rtc_time *tm) 908c2ecf20Sopenharmony_ci{ 918c2ecf20Sopenharmony_ci struct bq32k_regs regs; 928c2ecf20Sopenharmony_ci int error; 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci error = bq32k_read(dev, ®s, 0, sizeof(regs)); 958c2ecf20Sopenharmony_ci if (error) 968c2ecf20Sopenharmony_ci return error; 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci /* 998c2ecf20Sopenharmony_ci * In case of oscillator failure, the register contents should be 1008c2ecf20Sopenharmony_ci * considered invalid. The flag is cleared the next time the RTC is set. 1018c2ecf20Sopenharmony_ci */ 1028c2ecf20Sopenharmony_ci if (regs.minutes & BQ32K_OF) 1038c2ecf20Sopenharmony_ci return -EINVAL; 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci tm->tm_sec = bcd2bin(regs.seconds & BQ32K_SECONDS_MASK); 1068c2ecf20Sopenharmony_ci tm->tm_min = bcd2bin(regs.minutes & BQ32K_MINUTES_MASK); 1078c2ecf20Sopenharmony_ci tm->tm_hour = bcd2bin(regs.cent_hours & BQ32K_HOURS_MASK); 1088c2ecf20Sopenharmony_ci tm->tm_mday = bcd2bin(regs.date); 1098c2ecf20Sopenharmony_ci tm->tm_wday = bcd2bin(regs.day) - 1; 1108c2ecf20Sopenharmony_ci tm->tm_mon = bcd2bin(regs.month) - 1; 1118c2ecf20Sopenharmony_ci tm->tm_year = bcd2bin(regs.years) + 1128c2ecf20Sopenharmony_ci ((regs.cent_hours & BQ32K_CENT) ? 100 : 0); 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci return 0; 1158c2ecf20Sopenharmony_ci} 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_cistatic int bq32k_rtc_set_time(struct device *dev, struct rtc_time *tm) 1188c2ecf20Sopenharmony_ci{ 1198c2ecf20Sopenharmony_ci struct bq32k_regs regs; 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci regs.seconds = bin2bcd(tm->tm_sec); 1228c2ecf20Sopenharmony_ci regs.minutes = bin2bcd(tm->tm_min); 1238c2ecf20Sopenharmony_ci regs.cent_hours = bin2bcd(tm->tm_hour) | BQ32K_CENT_EN; 1248c2ecf20Sopenharmony_ci regs.day = bin2bcd(tm->tm_wday + 1); 1258c2ecf20Sopenharmony_ci regs.date = bin2bcd(tm->tm_mday); 1268c2ecf20Sopenharmony_ci regs.month = bin2bcd(tm->tm_mon + 1); 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci if (tm->tm_year >= 100) { 1298c2ecf20Sopenharmony_ci regs.cent_hours |= BQ32K_CENT; 1308c2ecf20Sopenharmony_ci regs.years = bin2bcd(tm->tm_year - 100); 1318c2ecf20Sopenharmony_ci } else 1328c2ecf20Sopenharmony_ci regs.years = bin2bcd(tm->tm_year); 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci return bq32k_write(dev, ®s, 0, sizeof(regs)); 1358c2ecf20Sopenharmony_ci} 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_cistatic const struct rtc_class_ops bq32k_rtc_ops = { 1388c2ecf20Sopenharmony_ci .read_time = bq32k_rtc_read_time, 1398c2ecf20Sopenharmony_ci .set_time = bq32k_rtc_set_time, 1408c2ecf20Sopenharmony_ci}; 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_cistatic int trickle_charger_of_init(struct device *dev, struct device_node *node) 1438c2ecf20Sopenharmony_ci{ 1448c2ecf20Sopenharmony_ci unsigned char reg; 1458c2ecf20Sopenharmony_ci int error; 1468c2ecf20Sopenharmony_ci u32 ohms = 0; 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci if (of_property_read_u32(node, "trickle-resistor-ohms" , &ohms)) 1498c2ecf20Sopenharmony_ci return 0; 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci switch (ohms) { 1528c2ecf20Sopenharmony_ci case 180+940: 1538c2ecf20Sopenharmony_ci /* 1548c2ecf20Sopenharmony_ci * TCHE[3:0] == 0x05, TCH2 == 1, TCFE == 0 (charging 1558c2ecf20Sopenharmony_ci * over diode and 940ohm resistor) 1568c2ecf20Sopenharmony_ci */ 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci if (of_property_read_bool(node, "trickle-diode-disable")) { 1598c2ecf20Sopenharmony_ci dev_err(dev, "diode and resistor mismatch\n"); 1608c2ecf20Sopenharmony_ci return -EINVAL; 1618c2ecf20Sopenharmony_ci } 1628c2ecf20Sopenharmony_ci reg = 0x05; 1638c2ecf20Sopenharmony_ci break; 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci case 180+20000: 1668c2ecf20Sopenharmony_ci /* diode disabled */ 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci if (!of_property_read_bool(node, "trickle-diode-disable")) { 1698c2ecf20Sopenharmony_ci dev_err(dev, "bq32k: diode and resistor mismatch\n"); 1708c2ecf20Sopenharmony_ci return -EINVAL; 1718c2ecf20Sopenharmony_ci } 1728c2ecf20Sopenharmony_ci reg = 0x45; 1738c2ecf20Sopenharmony_ci break; 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ci default: 1768c2ecf20Sopenharmony_ci dev_err(dev, "invalid resistor value (%d)\n", ohms); 1778c2ecf20Sopenharmony_ci return -EINVAL; 1788c2ecf20Sopenharmony_ci } 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci error = bq32k_write(dev, ®, BQ32K_CFG2, 1); 1818c2ecf20Sopenharmony_ci if (error) 1828c2ecf20Sopenharmony_ci return error; 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci reg = 0x20; 1858c2ecf20Sopenharmony_ci error = bq32k_write(dev, ®, BQ32K_TCH2, 1); 1868c2ecf20Sopenharmony_ci if (error) 1878c2ecf20Sopenharmony_ci return error; 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci dev_info(dev, "Enabled trickle RTC battery charge.\n"); 1908c2ecf20Sopenharmony_ci return 0; 1918c2ecf20Sopenharmony_ci} 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_cistatic ssize_t bq32k_sysfs_show_tricklecharge_bypass(struct device *dev, 1948c2ecf20Sopenharmony_ci struct device_attribute *attr, 1958c2ecf20Sopenharmony_ci char *buf) 1968c2ecf20Sopenharmony_ci{ 1978c2ecf20Sopenharmony_ci int reg, error; 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci error = bq32k_read(dev, ®, BQ32K_CFG2, 1); 2008c2ecf20Sopenharmony_ci if (error) 2018c2ecf20Sopenharmony_ci return error; 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", (reg & BQ32K_TCFE) ? 1 : 0); 2048c2ecf20Sopenharmony_ci} 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_cistatic ssize_t bq32k_sysfs_store_tricklecharge_bypass(struct device *dev, 2078c2ecf20Sopenharmony_ci struct device_attribute *attr, 2088c2ecf20Sopenharmony_ci const char *buf, size_t count) 2098c2ecf20Sopenharmony_ci{ 2108c2ecf20Sopenharmony_ci int reg, enable, error; 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci if (kstrtoint(buf, 0, &enable)) 2138c2ecf20Sopenharmony_ci return -EINVAL; 2148c2ecf20Sopenharmony_ci 2158c2ecf20Sopenharmony_ci error = bq32k_read(dev, ®, BQ32K_CFG2, 1); 2168c2ecf20Sopenharmony_ci if (error) 2178c2ecf20Sopenharmony_ci return error; 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ci if (enable) { 2208c2ecf20Sopenharmony_ci reg |= BQ32K_TCFE; 2218c2ecf20Sopenharmony_ci error = bq32k_write(dev, ®, BQ32K_CFG2, 1); 2228c2ecf20Sopenharmony_ci if (error) 2238c2ecf20Sopenharmony_ci return error; 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci dev_info(dev, "Enabled trickle charge FET bypass.\n"); 2268c2ecf20Sopenharmony_ci } else { 2278c2ecf20Sopenharmony_ci reg &= ~BQ32K_TCFE; 2288c2ecf20Sopenharmony_ci error = bq32k_write(dev, ®, BQ32K_CFG2, 1); 2298c2ecf20Sopenharmony_ci if (error) 2308c2ecf20Sopenharmony_ci return error; 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci dev_info(dev, "Disabled trickle charge FET bypass.\n"); 2338c2ecf20Sopenharmony_ci } 2348c2ecf20Sopenharmony_ci 2358c2ecf20Sopenharmony_ci return count; 2368c2ecf20Sopenharmony_ci} 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_cistatic DEVICE_ATTR(trickle_charge_bypass, 0644, 2398c2ecf20Sopenharmony_ci bq32k_sysfs_show_tricklecharge_bypass, 2408c2ecf20Sopenharmony_ci bq32k_sysfs_store_tricklecharge_bypass); 2418c2ecf20Sopenharmony_ci 2428c2ecf20Sopenharmony_cistatic int bq32k_sysfs_register(struct device *dev) 2438c2ecf20Sopenharmony_ci{ 2448c2ecf20Sopenharmony_ci return device_create_file(dev, &dev_attr_trickle_charge_bypass); 2458c2ecf20Sopenharmony_ci} 2468c2ecf20Sopenharmony_ci 2478c2ecf20Sopenharmony_cistatic void bq32k_sysfs_unregister(struct device *dev) 2488c2ecf20Sopenharmony_ci{ 2498c2ecf20Sopenharmony_ci device_remove_file(dev, &dev_attr_trickle_charge_bypass); 2508c2ecf20Sopenharmony_ci} 2518c2ecf20Sopenharmony_ci 2528c2ecf20Sopenharmony_cistatic int bq32k_probe(struct i2c_client *client, 2538c2ecf20Sopenharmony_ci const struct i2c_device_id *id) 2548c2ecf20Sopenharmony_ci{ 2558c2ecf20Sopenharmony_ci struct device *dev = &client->dev; 2568c2ecf20Sopenharmony_ci struct rtc_device *rtc; 2578c2ecf20Sopenharmony_ci uint8_t reg; 2588c2ecf20Sopenharmony_ci int error; 2598c2ecf20Sopenharmony_ci 2608c2ecf20Sopenharmony_ci if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) 2618c2ecf20Sopenharmony_ci return -ENODEV; 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_ci /* Check Oscillator Stop flag */ 2648c2ecf20Sopenharmony_ci error = bq32k_read(dev, ®, BQ32K_SECONDS, 1); 2658c2ecf20Sopenharmony_ci if (!error && (reg & BQ32K_STOP)) { 2668c2ecf20Sopenharmony_ci dev_warn(dev, "Oscillator was halted. Restarting...\n"); 2678c2ecf20Sopenharmony_ci reg &= ~BQ32K_STOP; 2688c2ecf20Sopenharmony_ci error = bq32k_write(dev, ®, BQ32K_SECONDS, 1); 2698c2ecf20Sopenharmony_ci } 2708c2ecf20Sopenharmony_ci if (error) 2718c2ecf20Sopenharmony_ci return error; 2728c2ecf20Sopenharmony_ci 2738c2ecf20Sopenharmony_ci /* Check Oscillator Failure flag */ 2748c2ecf20Sopenharmony_ci error = bq32k_read(dev, ®, BQ32K_MINUTES, 1); 2758c2ecf20Sopenharmony_ci if (error) 2768c2ecf20Sopenharmony_ci return error; 2778c2ecf20Sopenharmony_ci if (reg & BQ32K_OF) 2788c2ecf20Sopenharmony_ci dev_warn(dev, "Oscillator Failure. Check RTC battery.\n"); 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_ci if (client->dev.of_node) 2818c2ecf20Sopenharmony_ci trickle_charger_of_init(dev, client->dev.of_node); 2828c2ecf20Sopenharmony_ci 2838c2ecf20Sopenharmony_ci rtc = devm_rtc_device_register(&client->dev, bq32k_driver.driver.name, 2848c2ecf20Sopenharmony_ci &bq32k_rtc_ops, THIS_MODULE); 2858c2ecf20Sopenharmony_ci if (IS_ERR(rtc)) 2868c2ecf20Sopenharmony_ci return PTR_ERR(rtc); 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_ci error = bq32k_sysfs_register(&client->dev); 2898c2ecf20Sopenharmony_ci if (error) { 2908c2ecf20Sopenharmony_ci dev_err(&client->dev, 2918c2ecf20Sopenharmony_ci "Unable to create sysfs entries for rtc bq32000\n"); 2928c2ecf20Sopenharmony_ci return error; 2938c2ecf20Sopenharmony_ci } 2948c2ecf20Sopenharmony_ci 2958c2ecf20Sopenharmony_ci 2968c2ecf20Sopenharmony_ci i2c_set_clientdata(client, rtc); 2978c2ecf20Sopenharmony_ci 2988c2ecf20Sopenharmony_ci return 0; 2998c2ecf20Sopenharmony_ci} 3008c2ecf20Sopenharmony_ci 3018c2ecf20Sopenharmony_cistatic int bq32k_remove(struct i2c_client *client) 3028c2ecf20Sopenharmony_ci{ 3038c2ecf20Sopenharmony_ci bq32k_sysfs_unregister(&client->dev); 3048c2ecf20Sopenharmony_ci 3058c2ecf20Sopenharmony_ci return 0; 3068c2ecf20Sopenharmony_ci} 3078c2ecf20Sopenharmony_ci 3088c2ecf20Sopenharmony_cistatic const struct i2c_device_id bq32k_id[] = { 3098c2ecf20Sopenharmony_ci { "bq32000", 0 }, 3108c2ecf20Sopenharmony_ci { } 3118c2ecf20Sopenharmony_ci}; 3128c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, bq32k_id); 3138c2ecf20Sopenharmony_ci 3148c2ecf20Sopenharmony_cistatic const struct of_device_id bq32k_of_match[] = { 3158c2ecf20Sopenharmony_ci { .compatible = "ti,bq32000" }, 3168c2ecf20Sopenharmony_ci { } 3178c2ecf20Sopenharmony_ci}; 3188c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, bq32k_of_match); 3198c2ecf20Sopenharmony_ci 3208c2ecf20Sopenharmony_cistatic struct i2c_driver bq32k_driver = { 3218c2ecf20Sopenharmony_ci .driver = { 3228c2ecf20Sopenharmony_ci .name = "bq32k", 3238c2ecf20Sopenharmony_ci .of_match_table = of_match_ptr(bq32k_of_match), 3248c2ecf20Sopenharmony_ci }, 3258c2ecf20Sopenharmony_ci .probe = bq32k_probe, 3268c2ecf20Sopenharmony_ci .remove = bq32k_remove, 3278c2ecf20Sopenharmony_ci .id_table = bq32k_id, 3288c2ecf20Sopenharmony_ci}; 3298c2ecf20Sopenharmony_ci 3308c2ecf20Sopenharmony_cimodule_i2c_driver(bq32k_driver); 3318c2ecf20Sopenharmony_ci 3328c2ecf20Sopenharmony_ciMODULE_AUTHOR("Semihalf, Piotr Ziecik <kosmo@semihalf.com>"); 3338c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("TI BQ32000 I2C RTC driver"); 3348c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 335