1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for TI BQ32000 RTC.
5 * Copyright (C) 2009 Semihalf.
6 * Copyright (C) 2014 Pavel Machek <pavel@denx.de>
8 * You can get hardware description at
9 * https://www.ti.com/lit/ds/symlink/bq32000.pdf
12 #include <linux/module.h>
13 #include <linux/i2c.h>
14 #include <linux/rtc.h>
15 #include <linux/init.h>
16 #include <linux/kstrtox.h>
17 #include <linux/errno.h>
18 #include <linux/bcd.h>
20 #define BQ32K_SECONDS 0x00 /* Seconds register address */
21 #define BQ32K_SECONDS_MASK 0x7F /* Mask over seconds value */
22 #define BQ32K_STOP 0x80 /* Oscillator Stop flat */
24 #define BQ32K_MINUTES 0x01 /* Minutes register address */
25 #define BQ32K_MINUTES_MASK 0x7F /* Mask over minutes value */
26 #define BQ32K_OF 0x80 /* Oscillator Failure flag */
28 #define BQ32K_HOURS_MASK 0x3F /* Mask over hours value */
29 #define BQ32K_CENT 0x40 /* Century flag */
30 #define BQ32K_CENT_EN 0x80 /* Century flag enable bit */
32 #define BQ32K_CALIBRATION 0x07 /* CAL_CFG1, calibration and control */
33 #define BQ32K_TCH2 0x08 /* Trickle charge enable */
34 #define BQ32K_CFG2 0x09 /* Trickle charger control */
35 #define BQ32K_TCFE BIT(6) /* Trickle charge FET bypass */
37 #define MAX_LEN 10 /* Maximum number of consecutive
38 * register for this particular RTC.
51 static struct i2c_driver bq32k_driver;
53 static int bq32k_read(struct device *dev, void *data, uint8_t off, uint8_t len)
55 struct i2c_client *client = to_i2c_client(dev);
56 struct i2c_msg msgs[] = {
70 if (i2c_transfer(client->adapter, msgs, 2) == 2)
76 static int bq32k_write(struct device *dev, void *data, uint8_t off, uint8_t len)
78 struct i2c_client *client = to_i2c_client(dev);
79 uint8_t buffer[MAX_LEN + 1];
82 memcpy(&buffer[1], data, len);
84 if (i2c_master_send(client, buffer, len + 1) == len + 1)
90 static int bq32k_rtc_read_time(struct device *dev, struct rtc_time *tm)
92 struct bq32k_regs regs;
95 error = bq32k_read(dev, ®s, 0, sizeof(regs));
100 * In case of oscillator failure, the register contents should be
101 * considered invalid. The flag is cleared the next time the RTC is set.
103 if (regs.minutes & BQ32K_OF)
106 tm->tm_sec = bcd2bin(regs.seconds & BQ32K_SECONDS_MASK);
107 tm->tm_min = bcd2bin(regs.minutes & BQ32K_MINUTES_MASK);
108 tm->tm_hour = bcd2bin(regs.cent_hours & BQ32K_HOURS_MASK);
109 tm->tm_mday = bcd2bin(regs.date);
110 tm->tm_wday = bcd2bin(regs.day) - 1;
111 tm->tm_mon = bcd2bin(regs.month) - 1;
112 tm->tm_year = bcd2bin(regs.years) +
113 ((regs.cent_hours & BQ32K_CENT) ? 100 : 0);
118 static int bq32k_rtc_set_time(struct device *dev, struct rtc_time *tm)
120 struct bq32k_regs regs;
122 regs.seconds = bin2bcd(tm->tm_sec);
123 regs.minutes = bin2bcd(tm->tm_min);
124 regs.cent_hours = bin2bcd(tm->tm_hour) | BQ32K_CENT_EN;
125 regs.day = bin2bcd(tm->tm_wday + 1);
126 regs.date = bin2bcd(tm->tm_mday);
127 regs.month = bin2bcd(tm->tm_mon + 1);
129 if (tm->tm_year >= 100) {
130 regs.cent_hours |= BQ32K_CENT;
131 regs.years = bin2bcd(tm->tm_year - 100);
133 regs.years = bin2bcd(tm->tm_year);
135 return bq32k_write(dev, ®s, 0, sizeof(regs));
138 static const struct rtc_class_ops bq32k_rtc_ops = {
139 .read_time = bq32k_rtc_read_time,
140 .set_time = bq32k_rtc_set_time,
143 static int trickle_charger_of_init(struct device *dev, struct device_node *node)
149 if (of_property_read_u32(node, "trickle-resistor-ohms" , &ohms))
155 * TCHE[3:0] == 0x05, TCH2 == 1, TCFE == 0 (charging
156 * over diode and 940ohm resistor)
159 if (of_property_read_bool(node, "trickle-diode-disable")) {
160 dev_err(dev, "diode and resistor mismatch\n");
169 if (!of_property_read_bool(node, "trickle-diode-disable")) {
170 dev_err(dev, "bq32k: diode and resistor mismatch\n");
177 dev_err(dev, "invalid resistor value (%d)\n", ohms);
181 error = bq32k_write(dev, ®, BQ32K_CFG2, 1);
186 error = bq32k_write(dev, ®, BQ32K_TCH2, 1);
190 dev_info(dev, "Enabled trickle RTC battery charge.\n");
194 static ssize_t bq32k_sysfs_show_tricklecharge_bypass(struct device *dev,
195 struct device_attribute *attr,
200 error = bq32k_read(dev, ®, BQ32K_CFG2, 1);
204 return sprintf(buf, "%d\n", (reg & BQ32K_TCFE) ? 1 : 0);
207 static ssize_t bq32k_sysfs_store_tricklecharge_bypass(struct device *dev,
208 struct device_attribute *attr,
209 const char *buf, size_t count)
211 int reg, enable, error;
213 if (kstrtoint(buf, 0, &enable))
216 error = bq32k_read(dev, ®, BQ32K_CFG2, 1);
222 error = bq32k_write(dev, ®, BQ32K_CFG2, 1);
226 dev_info(dev, "Enabled trickle charge FET bypass.\n");
229 error = bq32k_write(dev, ®, BQ32K_CFG2, 1);
233 dev_info(dev, "Disabled trickle charge FET bypass.\n");
239 static DEVICE_ATTR(trickle_charge_bypass, 0644,
240 bq32k_sysfs_show_tricklecharge_bypass,
241 bq32k_sysfs_store_tricklecharge_bypass);
243 static int bq32k_sysfs_register(struct device *dev)
245 return device_create_file(dev, &dev_attr_trickle_charge_bypass);
248 static void bq32k_sysfs_unregister(struct device *dev)
250 device_remove_file(dev, &dev_attr_trickle_charge_bypass);
253 static int bq32k_probe(struct i2c_client *client)
255 struct device *dev = &client->dev;
256 struct rtc_device *rtc;
260 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
263 /* Check Oscillator Stop flag */
264 error = bq32k_read(dev, ®, BQ32K_SECONDS, 1);
265 if (!error && (reg & BQ32K_STOP)) {
266 dev_warn(dev, "Oscillator was halted. Restarting...\n");
268 error = bq32k_write(dev, ®, BQ32K_SECONDS, 1);
273 /* Check Oscillator Failure flag */
274 error = bq32k_read(dev, ®, BQ32K_MINUTES, 1);
278 dev_warn(dev, "Oscillator Failure. Check RTC battery.\n");
280 if (client->dev.of_node)
281 trickle_charger_of_init(dev, client->dev.of_node);
283 rtc = devm_rtc_device_register(&client->dev, bq32k_driver.driver.name,
284 &bq32k_rtc_ops, THIS_MODULE);
288 error = bq32k_sysfs_register(&client->dev);
290 dev_err(&client->dev,
291 "Unable to create sysfs entries for rtc bq32000\n");
296 i2c_set_clientdata(client, rtc);
301 static void bq32k_remove(struct i2c_client *client)
303 bq32k_sysfs_unregister(&client->dev);
306 static const struct i2c_device_id bq32k_id[] = {
310 MODULE_DEVICE_TABLE(i2c, bq32k_id);
312 static const __maybe_unused struct of_device_id bq32k_of_match[] = {
313 { .compatible = "ti,bq32000" },
316 MODULE_DEVICE_TABLE(of, bq32k_of_match);
318 static struct i2c_driver bq32k_driver = {
321 .of_match_table = of_match_ptr(bq32k_of_match),
323 .probe = bq32k_probe,
324 .remove = bq32k_remove,
325 .id_table = bq32k_id,
328 module_i2c_driver(bq32k_driver);
330 MODULE_AUTHOR("Semihalf, Piotr Ziecik <kosmo@semihalf.com>");
331 MODULE_DESCRIPTION("TI BQ32000 I2C RTC driver");
332 MODULE_LICENSE("GPL");