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