1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Battery monitor driver for the uPI uG3105 battery monitor 4 * 5 * Note the uG3105 is not a full-featured autonomous fuel-gauge. Instead it is 6 * expected to be use in combination with some always on microcontroller reading 7 * its coulomb-counter before it can wrap (must be read every 400 seconds!). 8 * 9 * Since Linux does not monitor coulomb-counter changes while the device 10 * is off or suspended, the coulomb counter is not used atm. 11 * 12 * Possible improvements: 13 * 1. Activate commented out total_coulomb_count code 14 * 2. Reset total_coulomb_count val to 0 when the battery is as good as empty 15 * and remember that we did this (and clear the flag for this on susp/resume) 16 * 3. When the battery is full check if the flag that we set total_coulomb_count 17 * to when the battery was empty is set. If so we now know the capacity, 18 * not the design, but actual capacity, of the battery 19 * 4. Add some mechanism (needs userspace help, or maybe use efivar?) to remember 20 * the actual capacity of the battery over reboots 21 * 5. When we know the actual capacity at probe time, add energy_now and 22 * energy_full attributes. Guess boot + resume energy_now value based on ocv 23 * and then use total_coulomb_count to report energy_now over time, resetting 24 * things to adjust for drift when empty/full. This should give more accurate 25 * readings, esp. in the 30-70% range and allow userspace to estimate time 26 * remaining till empty/full 27 * 6. Maybe unregister + reregister the psy device when we learn the actual 28 * capacity during run-time ? 29 * 30 * The above will also require some sort of mwh_per_unit calculation. Testing 31 * has shown that an estimated 7404mWh increase of the battery's energy results 32 * in a total_coulomb_count increase of 3277 units with a 5 milli-ohm sense R. 33 * 34 * Copyright (C) 2021 Hans de Goede <hdegoede@redhat.com> 35 */ 36 37 #include <linux/devm-helpers.h> 38 #include <linux/module.h> 39 #include <linux/mutex.h> 40 #include <linux/slab.h> 41 #include <linux/i2c.h> 42 #include <linux/mod_devicetable.h> 43 #include <linux/power_supply.h> 44 #include <linux/workqueue.h> 45 46 #define UG3105_MOV_AVG_WINDOW 8 47 #define UG3105_INIT_POLL_TIME (5 * HZ) 48 #define UG3105_POLL_TIME (30 * HZ) 49 #define UG3105_SETTLE_TIME (1 * HZ) 50 51 #define UG3105_INIT_POLL_COUNT 30 52 53 #define UG3105_REG_MODE 0x00 54 #define UG3105_REG_CTRL1 0x01 55 #define UG3105_REG_COULOMB_CNT 0x02 56 #define UG3105_REG_BAT_VOLT 0x08 57 #define UG3105_REG_BAT_CURR 0x0c 58 59 #define UG3105_MODE_STANDBY 0x00 60 #define UG3105_MODE_RUN 0x10 61 62 #define UG3105_CTRL1_RESET_COULOMB_CNT 0x03 63 64 #define UG3105_CURR_HYST_UA 65000 65 66 #define UG3105_LOW_BAT_UV 3700000 67 #define UG3105_FULL_BAT_HYST_UV 38000 68 69 #define AMBIENT_TEMP_CELCIUS 25 70 71 struct ug3105_chip { 72 struct i2c_client *client; 73 struct power_supply *psy; 74 struct delayed_work work; 75 struct mutex lock; 76 int ocv[UG3105_MOV_AVG_WINDOW]; /* micro-volt */ 77 int intern_res[UG3105_MOV_AVG_WINDOW]; /* milli-ohm */ 78 int poll_count; 79 int ocv_avg_index; 80 int ocv_avg; /* micro-volt */ 81 int intern_res_poll_count; 82 int intern_res_avg_index; 83 int intern_res_avg; /* milli-ohm */ 84 int volt; /* micro-volt */ 85 int curr; /* micro-ampere */ 86 int total_coulomb_count; 87 int uv_per_unit; 88 int ua_per_unit; 89 int status; 90 int capacity; 91 bool supplied; 92 }; 93 94 static int ug3105_read_word(struct i2c_client *client, u8 reg) 95 { 96 int val; 97 98 val = i2c_smbus_read_word_data(client, reg); 99 if (val < 0) 100 dev_err(&client->dev, "Error reading reg 0x%02x\n", reg); 101 102 return val; 103 } 104 105 static int ug3105_get_status(struct ug3105_chip *chip) 106 { 107 int full = chip->psy->battery_info->constant_charge_voltage_max_uv - 108 UG3105_FULL_BAT_HYST_UV; 109 110 if (chip->curr > UG3105_CURR_HYST_UA) 111 return POWER_SUPPLY_STATUS_CHARGING; 112 113 if (chip->curr < -UG3105_CURR_HYST_UA) 114 return POWER_SUPPLY_STATUS_DISCHARGING; 115 116 if (chip->supplied && chip->ocv_avg > full) 117 return POWER_SUPPLY_STATUS_FULL; 118 119 return POWER_SUPPLY_STATUS_NOT_CHARGING; 120 } 121 122 static void ug3105_work(struct work_struct *work) 123 { 124 struct ug3105_chip *chip = container_of(work, struct ug3105_chip, 125 work.work); 126 int i, val, curr_diff, volt_diff, res, win_size; 127 bool prev_supplied = chip->supplied; 128 int prev_status = chip->status; 129 int prev_volt = chip->volt; 130 int prev_curr = chip->curr; 131 struct power_supply *psy; 132 133 mutex_lock(&chip->lock); 134 135 psy = chip->psy; 136 if (!psy) 137 goto out; 138 139 val = ug3105_read_word(chip->client, UG3105_REG_BAT_VOLT); 140 if (val < 0) 141 goto out; 142 chip->volt = val * chip->uv_per_unit; 143 144 val = ug3105_read_word(chip->client, UG3105_REG_BAT_CURR); 145 if (val < 0) 146 goto out; 147 chip->curr = (s16)val * chip->ua_per_unit; 148 149 chip->ocv[chip->ocv_avg_index] = 150 chip->volt - chip->curr * chip->intern_res_avg / 1000; 151 chip->ocv_avg_index = (chip->ocv_avg_index + 1) % UG3105_MOV_AVG_WINDOW; 152 chip->poll_count++; 153 154 /* 155 * See possible improvements comment above. 156 * 157 * Read + reset coulomb counter every 10 polls (every 300 seconds) 158 * if ((chip->poll_count % 10) == 0) { 159 * val = ug3105_read_word(chip->client, UG3105_REG_COULOMB_CNT); 160 * if (val < 0) 161 * goto out; 162 * 163 * i2c_smbus_write_byte_data(chip->client, UG3105_REG_CTRL1, 164 * UG3105_CTRL1_RESET_COULOMB_CNT); 165 * 166 * chip->total_coulomb_count += (s16)val; 167 * dev_dbg(&chip->client->dev, "coulomb count %d total %d\n", 168 * (s16)val, chip->total_coulomb_count); 169 * } 170 */ 171 172 chip->ocv_avg = 0; 173 win_size = min(chip->poll_count, UG3105_MOV_AVG_WINDOW); 174 for (i = 0; i < win_size; i++) 175 chip->ocv_avg += chip->ocv[i]; 176 chip->ocv_avg /= win_size; 177 178 chip->supplied = power_supply_am_i_supplied(psy); 179 chip->status = ug3105_get_status(chip); 180 if (chip->status == POWER_SUPPLY_STATUS_FULL) 181 chip->capacity = 100; 182 else 183 chip->capacity = power_supply_batinfo_ocv2cap(chip->psy->battery_info, 184 chip->ocv_avg, 185 AMBIENT_TEMP_CELCIUS); 186 187 /* 188 * Skip internal resistance calc on charger [un]plug and 189 * when the battery is almost empty (voltage low). 190 */ 191 if (chip->supplied != prev_supplied || 192 chip->volt < UG3105_LOW_BAT_UV || 193 chip->poll_count < 2) 194 goto out; 195 196 /* 197 * Assuming that the OCV voltage does not change significantly 198 * between 2 polls, then we can calculate the internal resistance 199 * on a significant current change by attributing all voltage 200 * change between the 2 readings to the internal resistance. 201 */ 202 curr_diff = abs(chip->curr - prev_curr); 203 if (curr_diff < UG3105_CURR_HYST_UA) 204 goto out; 205 206 volt_diff = abs(chip->volt - prev_volt); 207 res = volt_diff * 1000 / curr_diff; 208 209 if ((res < (chip->intern_res_avg * 2 / 3)) || 210 (res > (chip->intern_res_avg * 4 / 3))) { 211 dev_dbg(&chip->client->dev, "Ignoring outlier internal resistance %d mOhm\n", res); 212 goto out; 213 } 214 215 dev_dbg(&chip->client->dev, "Internal resistance %d mOhm\n", res); 216 217 chip->intern_res[chip->intern_res_avg_index] = res; 218 chip->intern_res_avg_index = (chip->intern_res_avg_index + 1) % UG3105_MOV_AVG_WINDOW; 219 chip->intern_res_poll_count++; 220 221 chip->intern_res_avg = 0; 222 win_size = min(chip->intern_res_poll_count, UG3105_MOV_AVG_WINDOW); 223 for (i = 0; i < win_size; i++) 224 chip->intern_res_avg += chip->intern_res[i]; 225 chip->intern_res_avg /= win_size; 226 227 out: 228 mutex_unlock(&chip->lock); 229 230 queue_delayed_work(system_wq, &chip->work, 231 (chip->poll_count <= UG3105_INIT_POLL_COUNT) ? 232 UG3105_INIT_POLL_TIME : UG3105_POLL_TIME); 233 234 if (chip->status != prev_status && psy) 235 power_supply_changed(psy); 236 } 237 238 static enum power_supply_property ug3105_battery_props[] = { 239 POWER_SUPPLY_PROP_STATUS, 240 POWER_SUPPLY_PROP_PRESENT, 241 POWER_SUPPLY_PROP_SCOPE, 242 POWER_SUPPLY_PROP_VOLTAGE_NOW, 243 POWER_SUPPLY_PROP_VOLTAGE_OCV, 244 POWER_SUPPLY_PROP_CURRENT_NOW, 245 POWER_SUPPLY_PROP_CAPACITY, 246 }; 247 248 static int ug3105_get_property(struct power_supply *psy, 249 enum power_supply_property psp, 250 union power_supply_propval *val) 251 { 252 struct ug3105_chip *chip = power_supply_get_drvdata(psy); 253 int ret = 0; 254 255 mutex_lock(&chip->lock); 256 257 if (!chip->psy) { 258 ret = -EAGAIN; 259 goto out; 260 } 261 262 switch (psp) { 263 case POWER_SUPPLY_PROP_STATUS: 264 val->intval = chip->status; 265 break; 266 case POWER_SUPPLY_PROP_PRESENT: 267 val->intval = 1; 268 break; 269 case POWER_SUPPLY_PROP_SCOPE: 270 val->intval = POWER_SUPPLY_SCOPE_SYSTEM; 271 break; 272 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 273 ret = ug3105_read_word(chip->client, UG3105_REG_BAT_VOLT); 274 if (ret < 0) 275 break; 276 val->intval = ret * chip->uv_per_unit; 277 ret = 0; 278 break; 279 case POWER_SUPPLY_PROP_VOLTAGE_OCV: 280 val->intval = chip->ocv_avg; 281 break; 282 case POWER_SUPPLY_PROP_CURRENT_NOW: 283 ret = ug3105_read_word(chip->client, UG3105_REG_BAT_CURR); 284 if (ret < 0) 285 break; 286 val->intval = (s16)ret * chip->ua_per_unit; 287 ret = 0; 288 break; 289 case POWER_SUPPLY_PROP_CAPACITY: 290 val->intval = chip->capacity; 291 break; 292 default: 293 ret = -EINVAL; 294 } 295 296 out: 297 mutex_unlock(&chip->lock); 298 return ret; 299 } 300 301 static void ug3105_external_power_changed(struct power_supply *psy) 302 { 303 struct ug3105_chip *chip = power_supply_get_drvdata(psy); 304 305 dev_dbg(&chip->client->dev, "external power changed\n"); 306 mod_delayed_work(system_wq, &chip->work, UG3105_SETTLE_TIME); 307 } 308 309 static const struct power_supply_desc ug3105_psy_desc = { 310 .name = "ug3105_battery", 311 .type = POWER_SUPPLY_TYPE_BATTERY, 312 .get_property = ug3105_get_property, 313 .external_power_changed = ug3105_external_power_changed, 314 .properties = ug3105_battery_props, 315 .num_properties = ARRAY_SIZE(ug3105_battery_props), 316 }; 317 318 static void ug3105_init(struct ug3105_chip *chip) 319 { 320 chip->poll_count = 0; 321 chip->ocv_avg_index = 0; 322 chip->total_coulomb_count = 0; 323 i2c_smbus_write_byte_data(chip->client, UG3105_REG_MODE, 324 UG3105_MODE_RUN); 325 i2c_smbus_write_byte_data(chip->client, UG3105_REG_CTRL1, 326 UG3105_CTRL1_RESET_COULOMB_CNT); 327 queue_delayed_work(system_wq, &chip->work, 0); 328 flush_delayed_work(&chip->work); 329 } 330 331 static int ug3105_probe(struct i2c_client *client) 332 { 333 struct power_supply_config psy_cfg = {}; 334 struct device *dev = &client->dev; 335 u32 curr_sense_res_uohm = 10000; 336 struct power_supply *psy; 337 struct ug3105_chip *chip; 338 int ret; 339 340 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL); 341 if (!chip) 342 return -ENOMEM; 343 344 chip->client = client; 345 mutex_init(&chip->lock); 346 ret = devm_delayed_work_autocancel(dev, &chip->work, ug3105_work); 347 if (ret) 348 return ret; 349 350 psy_cfg.drv_data = chip; 351 psy = devm_power_supply_register(dev, &ug3105_psy_desc, &psy_cfg); 352 if (IS_ERR(psy)) 353 return PTR_ERR(psy); 354 355 if (!psy->battery_info || 356 psy->battery_info->factory_internal_resistance_uohm == -EINVAL || 357 psy->battery_info->constant_charge_voltage_max_uv == -EINVAL || 358 !psy->battery_info->ocv_table[0]) { 359 dev_err(dev, "error required properties are missing\n"); 360 return -ENODEV; 361 } 362 363 device_property_read_u32(dev, "upisemi,rsns-microohm", &curr_sense_res_uohm); 364 365 /* 366 * DAC maximum is 4.5V divided by 65536 steps + an unknown factor of 10 367 * coming from somewhere for some reason (verified with a volt-meter). 368 */ 369 chip->uv_per_unit = 45000000/65536; 370 /* Datasheet says 8.1 uV per unit for the current ADC */ 371 chip->ua_per_unit = 8100000 / curr_sense_res_uohm; 372 373 /* Use provided internal resistance as start point (in milli-ohm) */ 374 chip->intern_res_avg = psy->battery_info->factory_internal_resistance_uohm / 1000; 375 /* Also add it to the internal resistance moving average window */ 376 chip->intern_res[0] = chip->intern_res_avg; 377 chip->intern_res_avg_index = 1; 378 chip->intern_res_poll_count = 1; 379 380 mutex_lock(&chip->lock); 381 chip->psy = psy; 382 mutex_unlock(&chip->lock); 383 384 ug3105_init(chip); 385 386 i2c_set_clientdata(client, chip); 387 return 0; 388 } 389 390 static int __maybe_unused ug3105_suspend(struct device *dev) 391 { 392 struct ug3105_chip *chip = dev_get_drvdata(dev); 393 394 cancel_delayed_work_sync(&chip->work); 395 i2c_smbus_write_byte_data(chip->client, UG3105_REG_MODE, 396 UG3105_MODE_STANDBY); 397 398 return 0; 399 } 400 401 static int __maybe_unused ug3105_resume(struct device *dev) 402 { 403 struct ug3105_chip *chip = dev_get_drvdata(dev); 404 405 ug3105_init(chip); 406 407 return 0; 408 } 409 410 static SIMPLE_DEV_PM_OPS(ug3105_pm_ops, ug3105_suspend, 411 ug3105_resume); 412 413 static const struct i2c_device_id ug3105_id[] = { 414 { "ug3105" }, 415 { } 416 }; 417 MODULE_DEVICE_TABLE(i2c, ug3105_id); 418 419 static struct i2c_driver ug3105_i2c_driver = { 420 .driver = { 421 .name = "ug3105", 422 .pm = &ug3105_pm_ops, 423 }, 424 .probe = ug3105_probe, 425 .id_table = ug3105_id, 426 }; 427 module_i2c_driver(ug3105_i2c_driver); 428 429 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com"); 430 MODULE_DESCRIPTION("uPI uG3105 battery monitor driver"); 431 MODULE_LICENSE("GPL"); 432