ug3105_battery.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 <[email protected]>
  35. */
  36. #include <linux/devm-helpers.h>
  37. #include <linux/module.h>
  38. #include <linux/mutex.h>
  39. #include <linux/slab.h>
  40. #include <linux/i2c.h>
  41. #include <linux/mod_devicetable.h>
  42. #include <linux/power_supply.h>
  43. #include <linux/workqueue.h>
  44. #define UG3105_MOV_AVG_WINDOW 8
  45. #define UG3105_INIT_POLL_TIME (5 * HZ)
  46. #define UG3105_POLL_TIME (30 * HZ)
  47. #define UG3105_SETTLE_TIME (1 * HZ)
  48. #define UG3105_INIT_POLL_COUNT 30
  49. #define UG3105_REG_MODE 0x00
  50. #define UG3105_REG_CTRL1 0x01
  51. #define UG3105_REG_COULOMB_CNT 0x02
  52. #define UG3105_REG_BAT_VOLT 0x08
  53. #define UG3105_REG_BAT_CURR 0x0c
  54. #define UG3105_MODE_STANDBY 0x00
  55. #define UG3105_MODE_RUN 0x10
  56. #define UG3105_CTRL1_RESET_COULOMB_CNT 0x03
  57. #define UG3105_CURR_HYST_UA 65000
  58. #define UG3105_LOW_BAT_UV 3700000
  59. #define UG3105_FULL_BAT_HYST_UV 38000
  60. struct ug3105_chip {
  61. struct i2c_client *client;
  62. struct power_supply *psy;
  63. struct power_supply_battery_info *info;
  64. struct delayed_work work;
  65. struct mutex lock;
  66. int ocv[UG3105_MOV_AVG_WINDOW]; /* micro-volt */
  67. int intern_res[UG3105_MOV_AVG_WINDOW]; /* milli-ohm */
  68. int poll_count;
  69. int ocv_avg_index;
  70. int ocv_avg; /* micro-volt */
  71. int intern_res_poll_count;
  72. int intern_res_avg_index;
  73. int intern_res_avg; /* milli-ohm */
  74. int volt; /* micro-volt */
  75. int curr; /* micro-ampere */
  76. int total_coulomb_count;
  77. int uv_per_unit;
  78. int ua_per_unit;
  79. int status;
  80. int capacity;
  81. bool supplied;
  82. };
  83. static int ug3105_read_word(struct i2c_client *client, u8 reg)
  84. {
  85. int val;
  86. val = i2c_smbus_read_word_data(client, reg);
  87. if (val < 0)
  88. dev_err(&client->dev, "Error reading reg 0x%02x\n", reg);
  89. return val;
  90. }
  91. static int ug3105_get_status(struct ug3105_chip *chip)
  92. {
  93. int full = chip->info->constant_charge_voltage_max_uv - UG3105_FULL_BAT_HYST_UV;
  94. if (chip->curr > UG3105_CURR_HYST_UA)
  95. return POWER_SUPPLY_STATUS_CHARGING;
  96. if (chip->curr < -UG3105_CURR_HYST_UA)
  97. return POWER_SUPPLY_STATUS_DISCHARGING;
  98. if (chip->supplied && chip->ocv_avg > full)
  99. return POWER_SUPPLY_STATUS_FULL;
  100. return POWER_SUPPLY_STATUS_NOT_CHARGING;
  101. }
  102. static int ug3105_get_capacity(struct ug3105_chip *chip)
  103. {
  104. /*
  105. * OCV voltages in uV for 0-110% in 5% increments, the 100-110% is
  106. * for LiPo HV (High-Voltage) bateries which can go up to 4.35V
  107. * instead of the usual 4.2V.
  108. */
  109. static const int ocv_capacity_tbl[23] = {
  110. 3350000,
  111. 3610000,
  112. 3690000,
  113. 3710000,
  114. 3730000,
  115. 3750000,
  116. 3770000,
  117. 3786667,
  118. 3803333,
  119. 3820000,
  120. 3836667,
  121. 3853333,
  122. 3870000,
  123. 3907500,
  124. 3945000,
  125. 3982500,
  126. 4020000,
  127. 4075000,
  128. 4110000,
  129. 4150000,
  130. 4200000,
  131. 4250000,
  132. 4300000,
  133. };
  134. int i, ocv_diff, ocv_step;
  135. if (chip->ocv_avg < ocv_capacity_tbl[0])
  136. return 0;
  137. if (chip->status == POWER_SUPPLY_STATUS_FULL)
  138. return 100;
  139. for (i = 1; i < ARRAY_SIZE(ocv_capacity_tbl); i++) {
  140. if (chip->ocv_avg > ocv_capacity_tbl[i])
  141. continue;
  142. ocv_diff = ocv_capacity_tbl[i] - chip->ocv_avg;
  143. ocv_step = ocv_capacity_tbl[i] - ocv_capacity_tbl[i - 1];
  144. /* scale 0-110% down to 0-100% for LiPo HV */
  145. if (chip->info->constant_charge_voltage_max_uv >= 4300000)
  146. return (i * 500 - ocv_diff * 500 / ocv_step) / 110;
  147. else
  148. return i * 5 - ocv_diff * 5 / ocv_step;
  149. }
  150. return 100;
  151. }
  152. static void ug3105_work(struct work_struct *work)
  153. {
  154. struct ug3105_chip *chip = container_of(work, struct ug3105_chip,
  155. work.work);
  156. int i, val, curr_diff, volt_diff, res, win_size;
  157. bool prev_supplied = chip->supplied;
  158. int prev_status = chip->status;
  159. int prev_volt = chip->volt;
  160. int prev_curr = chip->curr;
  161. struct power_supply *psy;
  162. mutex_lock(&chip->lock);
  163. psy = chip->psy;
  164. if (!psy)
  165. goto out;
  166. val = ug3105_read_word(chip->client, UG3105_REG_BAT_VOLT);
  167. if (val < 0)
  168. goto out;
  169. chip->volt = val * chip->uv_per_unit;
  170. val = ug3105_read_word(chip->client, UG3105_REG_BAT_CURR);
  171. if (val < 0)
  172. goto out;
  173. chip->curr = (s16)val * chip->ua_per_unit;
  174. chip->ocv[chip->ocv_avg_index] =
  175. chip->volt - chip->curr * chip->intern_res_avg / 1000;
  176. chip->ocv_avg_index = (chip->ocv_avg_index + 1) % UG3105_MOV_AVG_WINDOW;
  177. chip->poll_count++;
  178. /*
  179. * See possible improvements comment above.
  180. *
  181. * Read + reset coulomb counter every 10 polls (every 300 seconds)
  182. * if ((chip->poll_count % 10) == 0) {
  183. * val = ug3105_read_word(chip->client, UG3105_REG_COULOMB_CNT);
  184. * if (val < 0)
  185. * goto out;
  186. *
  187. * i2c_smbus_write_byte_data(chip->client, UG3105_REG_CTRL1,
  188. * UG3105_CTRL1_RESET_COULOMB_CNT);
  189. *
  190. * chip->total_coulomb_count += (s16)val;
  191. * dev_dbg(&chip->client->dev, "coulomb count %d total %d\n",
  192. * (s16)val, chip->total_coulomb_count);
  193. * }
  194. */
  195. chip->ocv_avg = 0;
  196. win_size = min(chip->poll_count, UG3105_MOV_AVG_WINDOW);
  197. for (i = 0; i < win_size; i++)
  198. chip->ocv_avg += chip->ocv[i];
  199. chip->ocv_avg /= win_size;
  200. chip->supplied = power_supply_am_i_supplied(psy);
  201. chip->status = ug3105_get_status(chip);
  202. chip->capacity = ug3105_get_capacity(chip);
  203. /*
  204. * Skip internal resistance calc on charger [un]plug and
  205. * when the battery is almost empty (voltage low).
  206. */
  207. if (chip->supplied != prev_supplied ||
  208. chip->volt < UG3105_LOW_BAT_UV ||
  209. chip->poll_count < 2)
  210. goto out;
  211. /*
  212. * Assuming that the OCV voltage does not change significantly
  213. * between 2 polls, then we can calculate the internal resistance
  214. * on a significant current change by attributing all voltage
  215. * change between the 2 readings to the internal resistance.
  216. */
  217. curr_diff = abs(chip->curr - prev_curr);
  218. if (curr_diff < UG3105_CURR_HYST_UA)
  219. goto out;
  220. volt_diff = abs(chip->volt - prev_volt);
  221. res = volt_diff * 1000 / curr_diff;
  222. if ((res < (chip->intern_res_avg * 2 / 3)) ||
  223. (res > (chip->intern_res_avg * 4 / 3))) {
  224. dev_dbg(&chip->client->dev, "Ignoring outlier internal resistance %d mOhm\n", res);
  225. goto out;
  226. }
  227. dev_dbg(&chip->client->dev, "Internal resistance %d mOhm\n", res);
  228. chip->intern_res[chip->intern_res_avg_index] = res;
  229. chip->intern_res_avg_index = (chip->intern_res_avg_index + 1) % UG3105_MOV_AVG_WINDOW;
  230. chip->intern_res_poll_count++;
  231. chip->intern_res_avg = 0;
  232. win_size = min(chip->intern_res_poll_count, UG3105_MOV_AVG_WINDOW);
  233. for (i = 0; i < win_size; i++)
  234. chip->intern_res_avg += chip->intern_res[i];
  235. chip->intern_res_avg /= win_size;
  236. out:
  237. mutex_unlock(&chip->lock);
  238. queue_delayed_work(system_wq, &chip->work,
  239. (chip->poll_count <= UG3105_INIT_POLL_COUNT) ?
  240. UG3105_INIT_POLL_TIME : UG3105_POLL_TIME);
  241. if (chip->status != prev_status && psy)
  242. power_supply_changed(psy);
  243. }
  244. static enum power_supply_property ug3105_battery_props[] = {
  245. POWER_SUPPLY_PROP_STATUS,
  246. POWER_SUPPLY_PROP_PRESENT,
  247. POWER_SUPPLY_PROP_TECHNOLOGY,
  248. POWER_SUPPLY_PROP_SCOPE,
  249. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  250. POWER_SUPPLY_PROP_VOLTAGE_OCV,
  251. POWER_SUPPLY_PROP_CURRENT_NOW,
  252. POWER_SUPPLY_PROP_CAPACITY,
  253. };
  254. static int ug3105_get_property(struct power_supply *psy,
  255. enum power_supply_property psp,
  256. union power_supply_propval *val)
  257. {
  258. struct ug3105_chip *chip = power_supply_get_drvdata(psy);
  259. int ret = 0;
  260. mutex_lock(&chip->lock);
  261. if (!chip->psy) {
  262. ret = -EAGAIN;
  263. goto out;
  264. }
  265. switch (psp) {
  266. case POWER_SUPPLY_PROP_STATUS:
  267. val->intval = chip->status;
  268. break;
  269. case POWER_SUPPLY_PROP_PRESENT:
  270. val->intval = 1;
  271. break;
  272. case POWER_SUPPLY_PROP_TECHNOLOGY:
  273. val->intval = chip->info->technology;
  274. break;
  275. case POWER_SUPPLY_PROP_SCOPE:
  276. val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
  277. break;
  278. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  279. ret = ug3105_read_word(chip->client, UG3105_REG_BAT_VOLT);
  280. if (ret < 0)
  281. break;
  282. val->intval = ret * chip->uv_per_unit;
  283. ret = 0;
  284. break;
  285. case POWER_SUPPLY_PROP_VOLTAGE_OCV:
  286. val->intval = chip->ocv_avg;
  287. break;
  288. case POWER_SUPPLY_PROP_CURRENT_NOW:
  289. ret = ug3105_read_word(chip->client, UG3105_REG_BAT_CURR);
  290. if (ret < 0)
  291. break;
  292. val->intval = (s16)ret * chip->ua_per_unit;
  293. ret = 0;
  294. break;
  295. case POWER_SUPPLY_PROP_CAPACITY:
  296. val->intval = chip->capacity;
  297. break;
  298. default:
  299. ret = -EINVAL;
  300. }
  301. out:
  302. mutex_unlock(&chip->lock);
  303. return ret;
  304. }
  305. static void ug3105_external_power_changed(struct power_supply *psy)
  306. {
  307. struct ug3105_chip *chip = power_supply_get_drvdata(psy);
  308. dev_dbg(&chip->client->dev, "external power changed\n");
  309. mod_delayed_work(system_wq, &chip->work, UG3105_SETTLE_TIME);
  310. }
  311. static const struct power_supply_desc ug3105_psy_desc = {
  312. .name = "ug3105_battery",
  313. .type = POWER_SUPPLY_TYPE_BATTERY,
  314. .get_property = ug3105_get_property,
  315. .external_power_changed = ug3105_external_power_changed,
  316. .properties = ug3105_battery_props,
  317. .num_properties = ARRAY_SIZE(ug3105_battery_props),
  318. };
  319. static void ug3105_init(struct ug3105_chip *chip)
  320. {
  321. chip->poll_count = 0;
  322. chip->ocv_avg_index = 0;
  323. chip->total_coulomb_count = 0;
  324. i2c_smbus_write_byte_data(chip->client, UG3105_REG_MODE,
  325. UG3105_MODE_RUN);
  326. i2c_smbus_write_byte_data(chip->client, UG3105_REG_CTRL1,
  327. UG3105_CTRL1_RESET_COULOMB_CNT);
  328. queue_delayed_work(system_wq, &chip->work, 0);
  329. flush_delayed_work(&chip->work);
  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. chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
  340. if (!chip)
  341. return -ENOMEM;
  342. chip->client = client;
  343. mutex_init(&chip->lock);
  344. ret = devm_delayed_work_autocancel(dev, &chip->work, ug3105_work);
  345. if (ret)
  346. return ret;
  347. psy_cfg.drv_data = chip;
  348. psy = devm_power_supply_register(dev, &ug3105_psy_desc, &psy_cfg);
  349. if (IS_ERR(psy))
  350. return PTR_ERR(psy);
  351. ret = power_supply_get_battery_info(psy, &chip->info);
  352. if (ret)
  353. return ret;
  354. if (chip->info->factory_internal_resistance_uohm == -EINVAL ||
  355. chip->info->constant_charge_voltage_max_uv == -EINVAL) {
  356. dev_err(dev, "error required properties are missing\n");
  357. return -ENODEV;
  358. }
  359. device_property_read_u32(dev, "upisemi,rsns-microohm", &curr_sense_res_uohm);
  360. /*
  361. * DAC maximum is 4.5V divided by 65536 steps + an unknown factor of 10
  362. * coming from somewhere for some reason (verified with a volt-meter).
  363. */
  364. chip->uv_per_unit = 45000000/65536;
  365. /* Datasheet says 8.1 uV per unit for the current ADC */
  366. chip->ua_per_unit = 8100000 / curr_sense_res_uohm;
  367. /* Use provided internal resistance as start point (in milli-ohm) */
  368. chip->intern_res_avg = chip->info->factory_internal_resistance_uohm / 1000;
  369. /* Also add it to the internal resistance moving average window */
  370. chip->intern_res[0] = chip->intern_res_avg;
  371. chip->intern_res_avg_index = 1;
  372. chip->intern_res_poll_count = 1;
  373. mutex_lock(&chip->lock);
  374. chip->psy = psy;
  375. mutex_unlock(&chip->lock);
  376. ug3105_init(chip);
  377. i2c_set_clientdata(client, chip);
  378. return 0;
  379. }
  380. static int __maybe_unused ug3105_suspend(struct device *dev)
  381. {
  382. struct ug3105_chip *chip = dev_get_drvdata(dev);
  383. cancel_delayed_work_sync(&chip->work);
  384. i2c_smbus_write_byte_data(chip->client, UG3105_REG_MODE,
  385. UG3105_MODE_STANDBY);
  386. return 0;
  387. }
  388. static int __maybe_unused ug3105_resume(struct device *dev)
  389. {
  390. struct ug3105_chip *chip = dev_get_drvdata(dev);
  391. ug3105_init(chip);
  392. return 0;
  393. }
  394. static SIMPLE_DEV_PM_OPS(ug3105_pm_ops, ug3105_suspend,
  395. ug3105_resume);
  396. static const struct i2c_device_id ug3105_id[] = {
  397. { "ug3105" },
  398. { }
  399. };
  400. MODULE_DEVICE_TABLE(i2c, ug3105_id);
  401. static struct i2c_driver ug3105_i2c_driver = {
  402. .driver = {
  403. .name = "ug3105",
  404. .pm = &ug3105_pm_ops,
  405. },
  406. .probe_new = ug3105_probe,
  407. .id_table = ug3105_id,
  408. };
  409. module_i2c_driver(ug3105_i2c_driver);
  410. MODULE_AUTHOR("Hans de Goede <[email protected]");
  411. MODULE_DESCRIPTION("uPI uG3105 battery monitor driver");
  412. MODULE_LICENSE("GPL");