| 1 | |
| 2 | |
| 3 | * Driver for TI BQ32000 RTC.
|
| 4 | *
|
| 5 | * Copyright (C) 2009 Semihalf.
|
| 6 | * Copyright (C) 2014 Pavel Machek <[email protected]>
|
| 7 | *
|
| 8 | * You can get hardware description at
|
| 9 | * https://www.ti.com/lit/ds/symlink/bq32000.pdf
|
| 10 |
|
| 11 |
|
| 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>
|
| 19 | #include <linux/delay.h>
|
| 20 |
|
| 21 | #define BQ32K_SECONDS 0x00
|
| 22 | #define BQ32K_SECONDS_MASK 0x7F
|
| 23 | #define BQ32K_STOP 0x80
|
| 24 |
|
| 25 | #define BQ32K_MINUTES 0x01
|
| 26 | #define BQ32K_MINUTES_MASK 0x7F
|
| 27 | #define BQ32K_OF 0x80
|
| 28 |
|
| 29 | #define BQ32K_HOURS_MASK 0x3F
|
| 30 | #define BQ32K_CENT 0x40
|
| 31 | #define BQ32K_CENT_EN 0x80
|
| 32 |
|
| 33 | #define BQ32K_CALIBRATION 0x07
|
| 34 | #define BQ32K_TCH2 0x08
|
| 35 | #define BQ32K_CFG2 0x09
|
| 36 | #define BQ32K_TCFE BIT(6)
|
| 37 |
|
| 38 | #define MAX_LEN 10 |
| 39 | * register for this particular RTC.
|
| 40 |
|
| 41 |
|
| 42 | struct bq32k_regs {
|
| 43 | uint8_t seconds;
|
| 44 | uint8_t minutes;
|
| 45 | uint8_t cent_hours;
|
| 46 | uint8_t day;
|
| 47 | uint8_t date;
|
| 48 | uint8_t month;
|
| 49 | uint8_t years;
|
| 50 | };
|
| 51 |
|
| 52 | static struct i2c_driver bq32k_driver;
|
| 53 |
|
| 54 | static int bq32k_read(struct device *dev, void *data, uint8_t off, uint8_t len)
|
| 55 | {
|
| 56 | struct i2c_client *client = to_i2c_client(dev);
|
| 57 | struct i2c_msg msgs[] = {
|
| 58 | {
|
| 59 | .addr = client->addr,
|
| 60 | .flags = 0,
|
| 61 | .len = 1,
|
| 62 | .buf = &off,
|
| 63 | }, {
|
| 64 | .addr = client->addr,
|
| 65 | .flags = I2C_M_RD,
|
| 66 | .len = len,
|
| 67 | .buf = data,
|
| 68 | }
|
| 69 | };
|
| 70 |
|
| 71 | if (i2c_transfer(client->adapter, msgs, 2) == 2)
|
| 72 | return 0;
|
| 73 |
|
| 74 | return -EIO;
|
| 75 | }
|
| 76 |
|
| 77 | static int bq32k_write(struct device *dev, void *data, uint8_t off, uint8_t len)
|
| 78 | {
|
| 79 | struct i2c_client *client = to_i2c_client(dev);
|
| 80 | uint8_t buffer[MAX_LEN + 1];
|
| 81 |
|
| 82 | buffer[0] = off;
|
| 83 | memcpy(&buffer[1], data, len);
|
| 84 |
|
| 85 | if (i2c_master_send(client, buffer, len + 1) == len + 1)
|
| 86 | return 0;
|
| 87 |
|
| 88 | return -EIO;
|
| 89 | }
|
| 90 |
|
| 91 | static int bq32k_rtc_read_time(struct device *dev, struct rtc_time *tm)
|
| 92 | {
|
| 93 | struct i2c_client *client = to_i2c_client(dev);
|
| 94 | struct bq32k_regs regs;
|
| 95 | int error;
|
| 96 |
|
| 97 | |
| 98 | * When the device doesn't have the interrupt connected, prevent
|
| 99 | * userpace from polling the RTC registers too frequently.
|
| 100 |
|
| 101 | if (client->irq <= 0)
|
| 102 | usleep_range(2000, 2500);
|
| 103 |
|
| 104 | error = bq32k_read(dev, ®s, 0, sizeof(regs));
|
| 105 | if (error)
|
| 106 | return error;
|
| 107 |
|
| 108 | |
| 109 | * In case of oscillator failure, the register contents should be
|
| 110 | * considered invalid. The flag is cleared the next time the RTC is set.
|
| 111 |
|
| 112 | if (regs.minutes & BQ32K_OF)
|
| 113 | return -EINVAL;
|
| 114 |
|
| 115 | tm->tm_sec = bcd2bin(regs.seconds & BQ32K_SECONDS_MASK);
|
| 116 | tm->tm_min = bcd2bin(regs.minutes & BQ32K_MINUTES_MASK);
|
| 117 | tm->tm_hour = bcd2bin(regs.cent_hours & BQ32K_HOURS_MASK);
|
| 118 | tm->tm_mday = bcd2bin(regs.date);
|
| 119 | tm->tm_wday = bcd2bin(regs.day) - 1;
|
| 120 | tm->tm_mon = bcd2bin(regs.month) - 1;
|
| 121 | tm->tm_year = bcd2bin(regs.years) +
|
| 122 | ((regs.cent_hours & BQ32K_CENT) ? 100 : 0);
|
| 123 |
|
| 124 | return 0;
|
| 125 | }
|
| 126 |
|
| 127 | static int bq32k_rtc_set_time(struct device *dev, struct rtc_time *tm)
|
| 128 | {
|
| 129 | struct bq32k_regs regs;
|
| 130 |
|
| 131 | regs.seconds = bin2bcd(tm->tm_sec);
|
| 132 | regs.minutes = bin2bcd(tm->tm_min);
|
| 133 | regs.cent_hours = bin2bcd(tm->tm_hour) | BQ32K_CENT_EN;
|
| 134 | regs.day = bin2bcd(tm->tm_wday + 1);
|
| 135 | regs.date = bin2bcd(tm->tm_mday);
|
| 136 | regs.month = bin2bcd(tm->tm_mon + 1);
|
| 137 |
|
| 138 | if (tm->tm_year >= 100) {
|
| 139 | regs.cent_hours |= BQ32K_CENT;
|
| 140 | regs.years = bin2bcd(tm->tm_year - 100);
|
| 141 | } else
|
| 142 | regs.years = bin2bcd(tm->tm_year);
|
| 143 |
|
| 144 | return bq32k_write(dev, ®s, 0, sizeof(regs));
|
| 145 | }
|
| 146 |
|
| 147 | static const struct rtc_class_ops bq32k_rtc_ops = {
|
| 148 | .read_time = bq32k_rtc_read_time,
|
| 149 | .set_time = bq32k_rtc_set_time,
|
| 150 | };
|
| 151 |
|
| 152 | static int trickle_charger_of_init(struct device *dev, struct device_node *node)
|
| 153 | {
|
| 154 | unsigned char reg;
|
| 155 | int error;
|
| 156 | u32 ohms = 0;
|
| 157 |
|
| 158 | if (of_property_read_u32(node, "trickle-resistor-ohms" , &ohms))
|
| 159 | return 0;
|
| 160 |
|
| 161 | switch (ohms) {
|
| 162 | case 180+940:
|
| 163 | |
| 164 | * TCHE[3:0] == 0x05, TCH2 == 1, TCFE == 0 (charging
|
| 165 | * over diode and 940ohm resistor)
|
| 166 |
|
| 167 |
|
| 168 | if (of_property_read_bool(node, "trickle-diode-disable")) {
|
| 169 | dev_err(dev, "diode and resistor mismatch\n");
|
| 170 | return -EINVAL;
|
| 171 | }
|
| 172 | reg = 0x05;
|
| 173 | break;
|
| 174 |
|
| 175 | case 180+20000:
|
| 176 |
|
| 177 |
|
| 178 | if (!of_property_read_bool(node, "trickle-diode-disable")) {
|
| 179 | dev_err(dev, "bq32k: diode and resistor mismatch\n");
|
| 180 | return -EINVAL;
|
| 181 | }
|
| 182 | reg = 0x45;
|
| 183 | break;
|
| 184 |
|
| 185 | default:
|
| 186 | dev_err(dev, "invalid resistor value (%d)\n", ohms);
|
| 187 | return -EINVAL;
|
| 188 | }
|
| 189 |
|
| 190 | error = bq32k_write(dev, ®, BQ32K_CFG2, 1);
|
| 191 | if (error)
|
| 192 | return error;
|
| 193 |
|
| 194 | reg = 0x20;
|
| 195 | error = bq32k_write(dev, ®, BQ32K_TCH2, 1);
|
| 196 | if (error)
|
| 197 | return error;
|
| 198 |
|
| 199 | dev_info(dev, "Enabled trickle RTC battery charge.\n");
|
| 200 | return 0;
|
| 201 | }
|
| 202 |
|
| 203 | static ssize_t bq32k_sysfs_show_tricklecharge_bypass(struct device *dev,
|
| 204 | struct device_attribute *attr,
|
| 205 | char *buf)
|
| 206 | {
|
| 207 | int reg, error;
|
| 208 |
|
| 209 | error = bq32k_read(dev, ®, BQ32K_CFG2, 1);
|
| 210 | if (error)
|
| 211 | return error;
|
| 212 |
|
| 213 | return sprintf(buf, "%d\n", (reg & BQ32K_TCFE) ? 1 : 0);
|
| 214 | }
|
| 215 |
|
| 216 | static ssize_t bq32k_sysfs_store_tricklecharge_bypass(struct device *dev,
|
| 217 | struct device_attribute *attr,
|
| 218 | const char *buf, size_t count)
|
| 219 | {
|
| 220 | int reg, enable, error;
|
| 221 |
|
| 222 | if (kstrtoint(buf, 0, &enable))
|
| 223 | return -EINVAL;
|
| 224 |
|
| 225 | error = bq32k_read(dev, ®, BQ32K_CFG2, 1);
|
| 226 | if (error)
|
| 227 | return error;
|
| 228 |
|
| 229 | if (enable) {
|
| 230 | reg |= BQ32K_TCFE;
|
| 231 | error = bq32k_write(dev, ®, BQ32K_CFG2, 1);
|
| 232 | if (error)
|
| 233 | return error;
|
| 234 |
|
| 235 | dev_info(dev, "Enabled trickle charge FET bypass.\n");
|
| 236 | } else {
|
| 237 | reg &= ~BQ32K_TCFE;
|
| 238 | error = bq32k_write(dev, ®, BQ32K_CFG2, 1);
|
| 239 | if (error)
|
| 240 | return error;
|
| 241 |
|
| 242 | dev_info(dev, "Disabled trickle charge FET bypass.\n");
|
| 243 | }
|
| 244 |
|
| 245 | return count;
|
| 246 | }
|
| 247 |
|
| 248 | static DEVICE_ATTR(trickle_charge_bypass, 0644,
|
| 249 | bq32k_sysfs_show_tricklecharge_bypass,
|
| 250 | bq32k_sysfs_store_tricklecharge_bypass);
|
| 251 |
|
| 252 | static int bq32k_sysfs_register(struct device *dev)
|
| 253 | {
|
| 254 | return device_create_file(dev, &dev_attr_trickle_charge_bypass);
|
| 255 | }
|
| 256 |
|
| 257 | static void bq32k_sysfs_unregister(struct device *dev)
|
| 258 | {
|
| 259 | device_remove_file(dev, &dev_attr_trickle_charge_bypass);
|
| 260 | }
|
| 261 |
|
| 262 | static int bq32k_probe(struct i2c_client *client)
|
| 263 | {
|
| 264 | struct device *dev = &client->dev;
|
| 265 | struct rtc_device *rtc;
|
| 266 | uint8_t reg;
|
| 267 | int error;
|
| 268 |
|
| 269 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
|
| 270 | return -ENODEV;
|
| 271 |
|
| 272 |
|
| 273 | error = bq32k_read(dev, ®, BQ32K_SECONDS, 1);
|
| 274 | if (!error && (reg & BQ32K_STOP)) {
|
| 275 | dev_warn(dev, "Oscillator was halted. Restarting...\n");
|
| 276 | reg &= ~BQ32K_STOP;
|
| 277 | error = bq32k_write(dev, ®, BQ32K_SECONDS, 1);
|
| 278 | }
|
| 279 | if (error)
|
| 280 | return error;
|
| 281 |
|
| 282 |
|
| 283 | error = bq32k_read(dev, ®, BQ32K_MINUTES, 1);
|
| 284 | if (error)
|
| 285 | return error;
|
| 286 | if (reg & BQ32K_OF)
|
| 287 | dev_warn(dev, "Oscillator Failure. Check RTC battery.\n");
|
| 288 |
|
| 289 | if (client->dev.of_node)
|
| 290 | trickle_charger_of_init(dev, client->dev.of_node);
|
| 291 |
|
| 292 | rtc = devm_rtc_device_register(&client->dev, bq32k_driver.driver.name,
|
| 293 | &bq32k_rtc_ops, THIS_MODULE);
|
| 294 | if (IS_ERR(rtc))
|
| 295 | return PTR_ERR(rtc);
|
| 296 |
|
| 297 | error = bq32k_sysfs_register(&client->dev);
|
| 298 | if (error) {
|
| 299 | dev_err(&client->dev,
|
| 300 | "Unable to create sysfs entries for rtc bq32000\n");
|
| 301 | return error;
|
| 302 | }
|
| 303 |
|
| 304 |
|
| 305 | i2c_set_clientdata(client, rtc);
|
| 306 |
|
| 307 | return 0;
|
| 308 | }
|
| 309 |
|
| 310 | static void bq32k_remove(struct i2c_client *client)
|
| 311 | {
|
| 312 | bq32k_sysfs_unregister(&client->dev);
|
| 313 | }
|
| 314 |
|
| 315 | static const struct i2c_device_id bq32k_id[] = {
|
| 316 | { .name = "bq32000" },
|
| 317 | { }
|
| 318 | };
|
| 319 | MODULE_DEVICE_TABLE(i2c, bq32k_id);
|
| 320 |
|
| 321 | static const __maybe_unused struct of_device_id bq32k_of_match[] = {
|
| 322 | { .compatible = "ti,bq32000" },
|
| 323 | { }
|
| 324 | };
|
| 325 | MODULE_DEVICE_TABLE(of, bq32k_of_match);
|
| 326 |
|
| 327 | static struct i2c_driver bq32k_driver = {
|
| 328 | .driver = {
|
| 329 | .name = "bq32k",
|
| 330 | .of_match_table = of_match_ptr(bq32k_of_match),
|
| 331 | },
|
| 332 | .probe = bq32k_probe,
|
| 333 | .remove = bq32k_remove,
|
| 334 | .id_table = bq32k_id,
|
| 335 | };
|
| 336 |
|
| 337 | module_i2c_driver(bq32k_driver);
|
| 338 |
|
| 339 | MODULE_AUTHOR("Semihalf, Piotr Ziecik <[email protected]>");
|
| 340 | MODULE_DESCRIPTION("TI BQ32000 I2C RTC driver");
|
| 341 | MODULE_LICENSE("GPL");
|