acer_a500_battery.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Battery driver for Acer Iconia Tab A500.
  4. *
  5. * Copyright 2020 GRATE-driver project.
  6. *
  7. * Based on downstream driver from Acer Inc.
  8. * Based on NVIDIA Gas Gauge driver for SBS Compliant Batteries.
  9. *
  10. * Copyright (c) 2010, NVIDIA Corporation.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/power_supply.h>
  15. #include <linux/regmap.h>
  16. #include <linux/sched.h>
  17. #include <linux/slab.h>
  18. #include <linux/workqueue.h>
  19. enum {
  20. REG_CAPACITY,
  21. REG_VOLTAGE,
  22. REG_CURRENT,
  23. REG_DESIGN_CAPACITY,
  24. REG_TEMPERATURE,
  25. };
  26. #define EC_DATA(_reg, _psp) { \
  27. .psp = POWER_SUPPLY_PROP_ ## _psp, \
  28. .reg = _reg, \
  29. }
  30. static const struct battery_register {
  31. enum power_supply_property psp;
  32. unsigned int reg;
  33. } ec_data[] = {
  34. [REG_CAPACITY] = EC_DATA(0x00, CAPACITY),
  35. [REG_VOLTAGE] = EC_DATA(0x01, VOLTAGE_NOW),
  36. [REG_CURRENT] = EC_DATA(0x03, CURRENT_NOW),
  37. [REG_DESIGN_CAPACITY] = EC_DATA(0x08, CHARGE_FULL_DESIGN),
  38. [REG_TEMPERATURE] = EC_DATA(0x0a, TEMP),
  39. };
  40. static const enum power_supply_property a500_battery_properties[] = {
  41. POWER_SUPPLY_PROP_CAPACITY,
  42. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  43. POWER_SUPPLY_PROP_CURRENT_NOW,
  44. POWER_SUPPLY_PROP_PRESENT,
  45. POWER_SUPPLY_PROP_STATUS,
  46. POWER_SUPPLY_PROP_TECHNOLOGY,
  47. POWER_SUPPLY_PROP_TEMP,
  48. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  49. };
  50. struct a500_battery {
  51. struct delayed_work poll_work;
  52. struct power_supply *psy;
  53. struct regmap *regmap;
  54. unsigned int capacity;
  55. };
  56. static bool a500_battery_update_capacity(struct a500_battery *bat)
  57. {
  58. unsigned int capacity;
  59. int err;
  60. err = regmap_read(bat->regmap, ec_data[REG_CAPACITY].reg, &capacity);
  61. if (err)
  62. return false;
  63. /* capacity can be >100% even if max value is 100% */
  64. capacity = min(capacity, 100u);
  65. if (bat->capacity != capacity) {
  66. bat->capacity = capacity;
  67. return true;
  68. }
  69. return false;
  70. }
  71. static int a500_battery_get_status(struct a500_battery *bat)
  72. {
  73. if (bat->capacity < 100) {
  74. if (power_supply_am_i_supplied(bat->psy))
  75. return POWER_SUPPLY_STATUS_CHARGING;
  76. else
  77. return POWER_SUPPLY_STATUS_DISCHARGING;
  78. }
  79. return POWER_SUPPLY_STATUS_FULL;
  80. }
  81. static void a500_battery_unit_adjustment(struct device *dev,
  82. enum power_supply_property psp,
  83. union power_supply_propval *val)
  84. {
  85. const unsigned int base_unit_conversion = 1000;
  86. const unsigned int temp_kelvin_to_celsius = 2731;
  87. switch (psp) {
  88. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  89. case POWER_SUPPLY_PROP_CURRENT_NOW:
  90. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  91. val->intval *= base_unit_conversion;
  92. break;
  93. case POWER_SUPPLY_PROP_TEMP:
  94. val->intval -= temp_kelvin_to_celsius;
  95. break;
  96. case POWER_SUPPLY_PROP_PRESENT:
  97. val->intval = !!val->intval;
  98. break;
  99. default:
  100. dev_dbg(dev,
  101. "%s: no need for unit conversion %d\n", __func__, psp);
  102. }
  103. }
  104. static int a500_battery_get_ec_data_index(struct device *dev,
  105. enum power_supply_property psp)
  106. {
  107. unsigned int i;
  108. /*
  109. * DESIGN_CAPACITY register always returns a non-zero value if
  110. * battery is connected and zero if disconnected, hence we'll use
  111. * it for judging the battery presence.
  112. */
  113. if (psp == POWER_SUPPLY_PROP_PRESENT)
  114. psp = POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN;
  115. for (i = 0; i < ARRAY_SIZE(ec_data); i++)
  116. if (psp == ec_data[i].psp)
  117. return i;
  118. dev_dbg(dev, "%s: invalid property %u\n", __func__, psp);
  119. return -EINVAL;
  120. }
  121. static int a500_battery_get_property(struct power_supply *psy,
  122. enum power_supply_property psp,
  123. union power_supply_propval *val)
  124. {
  125. struct a500_battery *bat = power_supply_get_drvdata(psy);
  126. struct device *dev = psy->dev.parent;
  127. int ret = 0;
  128. switch (psp) {
  129. case POWER_SUPPLY_PROP_STATUS:
  130. val->intval = a500_battery_get_status(bat);
  131. break;
  132. case POWER_SUPPLY_PROP_TECHNOLOGY:
  133. val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
  134. break;
  135. case POWER_SUPPLY_PROP_CAPACITY:
  136. a500_battery_update_capacity(bat);
  137. val->intval = bat->capacity;
  138. break;
  139. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  140. case POWER_SUPPLY_PROP_CURRENT_NOW:
  141. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  142. case POWER_SUPPLY_PROP_PRESENT:
  143. case POWER_SUPPLY_PROP_TEMP:
  144. ret = a500_battery_get_ec_data_index(dev, psp);
  145. if (ret < 0)
  146. break;
  147. ret = regmap_read(bat->regmap, ec_data[ret].reg, &val->intval);
  148. break;
  149. default:
  150. dev_err(dev, "%s: invalid property %u\n", __func__, psp);
  151. return -EINVAL;
  152. }
  153. if (!ret) {
  154. /* convert units to match requirements of power supply class */
  155. a500_battery_unit_adjustment(dev, psp, val);
  156. }
  157. dev_dbg(dev, "%s: property = %d, value = %x\n",
  158. __func__, psp, val->intval);
  159. /* return NODATA for properties if battery not presents */
  160. if (ret)
  161. return -ENODATA;
  162. return 0;
  163. }
  164. static void a500_battery_poll_work(struct work_struct *work)
  165. {
  166. struct a500_battery *bat;
  167. bool capacity_changed;
  168. bat = container_of(work, struct a500_battery, poll_work.work);
  169. capacity_changed = a500_battery_update_capacity(bat);
  170. if (capacity_changed)
  171. power_supply_changed(bat->psy);
  172. /* continuously send uevent notification */
  173. schedule_delayed_work(&bat->poll_work, 30 * HZ);
  174. }
  175. static const struct power_supply_desc a500_battery_desc = {
  176. .name = "ec-battery",
  177. .type = POWER_SUPPLY_TYPE_BATTERY,
  178. .properties = a500_battery_properties,
  179. .get_property = a500_battery_get_property,
  180. .num_properties = ARRAY_SIZE(a500_battery_properties),
  181. .external_power_changed = power_supply_changed,
  182. };
  183. static int a500_battery_probe(struct platform_device *pdev)
  184. {
  185. struct power_supply_config psy_cfg = {};
  186. struct a500_battery *bat;
  187. bat = devm_kzalloc(&pdev->dev, sizeof(*bat), GFP_KERNEL);
  188. if (!bat)
  189. return -ENOMEM;
  190. platform_set_drvdata(pdev, bat);
  191. psy_cfg.of_node = pdev->dev.parent->of_node;
  192. psy_cfg.drv_data = bat;
  193. bat->regmap = dev_get_regmap(pdev->dev.parent, "KB930");
  194. if (!bat->regmap)
  195. return -EINVAL;
  196. bat->psy = devm_power_supply_register_no_ws(&pdev->dev,
  197. &a500_battery_desc,
  198. &psy_cfg);
  199. if (IS_ERR(bat->psy))
  200. return dev_err_probe(&pdev->dev, PTR_ERR(bat->psy),
  201. "failed to register battery\n");
  202. INIT_DELAYED_WORK(&bat->poll_work, a500_battery_poll_work);
  203. schedule_delayed_work(&bat->poll_work, HZ);
  204. return 0;
  205. }
  206. static int a500_battery_remove(struct platform_device *pdev)
  207. {
  208. struct a500_battery *bat = dev_get_drvdata(&pdev->dev);
  209. cancel_delayed_work_sync(&bat->poll_work);
  210. return 0;
  211. }
  212. static int __maybe_unused a500_battery_suspend(struct device *dev)
  213. {
  214. struct a500_battery *bat = dev_get_drvdata(dev);
  215. cancel_delayed_work_sync(&bat->poll_work);
  216. return 0;
  217. }
  218. static int __maybe_unused a500_battery_resume(struct device *dev)
  219. {
  220. struct a500_battery *bat = dev_get_drvdata(dev);
  221. schedule_delayed_work(&bat->poll_work, HZ);
  222. return 0;
  223. }
  224. static SIMPLE_DEV_PM_OPS(a500_battery_pm_ops,
  225. a500_battery_suspend, a500_battery_resume);
  226. static struct platform_driver a500_battery_driver = {
  227. .driver = {
  228. .name = "acer-a500-iconia-battery",
  229. .pm = &a500_battery_pm_ops,
  230. },
  231. .probe = a500_battery_probe,
  232. .remove = a500_battery_remove,
  233. };
  234. module_platform_driver(a500_battery_driver);
  235. MODULE_DESCRIPTION("Battery gauge driver for Acer Iconia Tab A500");
  236. MODULE_AUTHOR("Dmitry Osipenko <[email protected]>");
  237. MODULE_ALIAS("platform:acer-a500-iconia-battery");
  238. MODULE_LICENSE("GPL");