wm97xx_battery.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Battery measurement code for WM97xx
  4. *
  5. * based on tosa_battery.c
  6. *
  7. * Copyright (C) 2008 Marek Vasut <[email protected]>
  8. */
  9. #include <linux/init.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/power_supply.h>
  14. #include <linux/wm97xx.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/gpio/consumer.h>
  18. #include <linux/irq.h>
  19. #include <linux/slab.h>
  20. static struct work_struct bat_work;
  21. static struct gpio_desc *charge_gpiod;
  22. static DEFINE_MUTEX(work_lock);
  23. static int bat_status = POWER_SUPPLY_STATUS_UNKNOWN;
  24. static enum power_supply_property *prop;
  25. static unsigned long wm97xx_read_bat(struct power_supply *bat_ps)
  26. {
  27. struct wm97xx_batt_pdata *pdata = power_supply_get_drvdata(bat_ps);
  28. return wm97xx_read_aux_adc(dev_get_drvdata(bat_ps->dev.parent),
  29. pdata->batt_aux) * pdata->batt_mult /
  30. pdata->batt_div;
  31. }
  32. static unsigned long wm97xx_read_temp(struct power_supply *bat_ps)
  33. {
  34. struct wm97xx_batt_pdata *pdata = power_supply_get_drvdata(bat_ps);
  35. return wm97xx_read_aux_adc(dev_get_drvdata(bat_ps->dev.parent),
  36. pdata->temp_aux) * pdata->temp_mult /
  37. pdata->temp_div;
  38. }
  39. static int wm97xx_bat_get_property(struct power_supply *bat_ps,
  40. enum power_supply_property psp,
  41. union power_supply_propval *val)
  42. {
  43. struct wm97xx_batt_pdata *pdata = power_supply_get_drvdata(bat_ps);
  44. switch (psp) {
  45. case POWER_SUPPLY_PROP_STATUS:
  46. val->intval = bat_status;
  47. break;
  48. case POWER_SUPPLY_PROP_TECHNOLOGY:
  49. val->intval = pdata->batt_tech;
  50. break;
  51. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  52. if (pdata->batt_aux >= 0)
  53. val->intval = wm97xx_read_bat(bat_ps);
  54. else
  55. return -EINVAL;
  56. break;
  57. case POWER_SUPPLY_PROP_TEMP:
  58. if (pdata->temp_aux >= 0)
  59. val->intval = wm97xx_read_temp(bat_ps);
  60. else
  61. return -EINVAL;
  62. break;
  63. case POWER_SUPPLY_PROP_VOLTAGE_MAX:
  64. if (pdata->max_voltage >= 0)
  65. val->intval = pdata->max_voltage;
  66. else
  67. return -EINVAL;
  68. break;
  69. case POWER_SUPPLY_PROP_VOLTAGE_MIN:
  70. if (pdata->min_voltage >= 0)
  71. val->intval = pdata->min_voltage;
  72. else
  73. return -EINVAL;
  74. break;
  75. case POWER_SUPPLY_PROP_PRESENT:
  76. val->intval = 1;
  77. break;
  78. default:
  79. return -EINVAL;
  80. }
  81. return 0;
  82. }
  83. static void wm97xx_bat_external_power_changed(struct power_supply *bat_ps)
  84. {
  85. schedule_work(&bat_work);
  86. }
  87. static void wm97xx_bat_update(struct power_supply *bat_ps)
  88. {
  89. int old_status = bat_status;
  90. mutex_lock(&work_lock);
  91. bat_status = (charge_gpiod) ?
  92. (gpiod_get_value(charge_gpiod) ?
  93. POWER_SUPPLY_STATUS_DISCHARGING :
  94. POWER_SUPPLY_STATUS_CHARGING) :
  95. POWER_SUPPLY_STATUS_UNKNOWN;
  96. if (old_status != bat_status) {
  97. pr_debug("%s: %i -> %i\n", bat_ps->desc->name, old_status,
  98. bat_status);
  99. power_supply_changed(bat_ps);
  100. }
  101. mutex_unlock(&work_lock);
  102. }
  103. static struct power_supply *bat_psy;
  104. static struct power_supply_desc bat_psy_desc = {
  105. .type = POWER_SUPPLY_TYPE_BATTERY,
  106. .get_property = wm97xx_bat_get_property,
  107. .external_power_changed = wm97xx_bat_external_power_changed,
  108. .use_for_apm = 1,
  109. };
  110. static void wm97xx_bat_work(struct work_struct *work)
  111. {
  112. wm97xx_bat_update(bat_psy);
  113. }
  114. static irqreturn_t wm97xx_chrg_irq(int irq, void *data)
  115. {
  116. schedule_work(&bat_work);
  117. return IRQ_HANDLED;
  118. }
  119. #ifdef CONFIG_PM
  120. static int wm97xx_bat_suspend(struct device *dev)
  121. {
  122. flush_work(&bat_work);
  123. return 0;
  124. }
  125. static int wm97xx_bat_resume(struct device *dev)
  126. {
  127. schedule_work(&bat_work);
  128. return 0;
  129. }
  130. static const struct dev_pm_ops wm97xx_bat_pm_ops = {
  131. .suspend = wm97xx_bat_suspend,
  132. .resume = wm97xx_bat_resume,
  133. };
  134. #endif
  135. static int wm97xx_bat_probe(struct platform_device *dev)
  136. {
  137. int ret = 0;
  138. int props = 1; /* POWER_SUPPLY_PROP_PRESENT */
  139. int i = 0;
  140. struct wm97xx_batt_pdata *pdata = dev->dev.platform_data;
  141. struct power_supply_config cfg = {};
  142. if (!pdata) {
  143. dev_err(&dev->dev, "No platform data supplied\n");
  144. return -EINVAL;
  145. }
  146. cfg.drv_data = pdata;
  147. if (dev->id != -1)
  148. return -EINVAL;
  149. charge_gpiod = devm_gpiod_get_optional(&dev->dev, NULL, GPIOD_IN);
  150. if (IS_ERR(charge_gpiod))
  151. return dev_err_probe(&dev->dev,
  152. PTR_ERR(charge_gpiod),
  153. "failed to get charge GPIO\n");
  154. if (charge_gpiod) {
  155. gpiod_set_consumer_name(charge_gpiod, "BATT CHRG");
  156. ret = request_irq(gpiod_to_irq(charge_gpiod),
  157. wm97xx_chrg_irq, 0,
  158. "AC Detect", dev);
  159. if (ret)
  160. return dev_err_probe(&dev->dev, ret,
  161. "failed to request GPIO irq\n");
  162. props++; /* POWER_SUPPLY_PROP_STATUS */
  163. }
  164. if (pdata->batt_tech >= 0)
  165. props++; /* POWER_SUPPLY_PROP_TECHNOLOGY */
  166. if (pdata->temp_aux >= 0)
  167. props++; /* POWER_SUPPLY_PROP_TEMP */
  168. if (pdata->batt_aux >= 0)
  169. props++; /* POWER_SUPPLY_PROP_VOLTAGE_NOW */
  170. if (pdata->max_voltage >= 0)
  171. props++; /* POWER_SUPPLY_PROP_VOLTAGE_MAX */
  172. if (pdata->min_voltage >= 0)
  173. props++; /* POWER_SUPPLY_PROP_VOLTAGE_MIN */
  174. prop = kcalloc(props, sizeof(*prop), GFP_KERNEL);
  175. if (!prop) {
  176. ret = -ENOMEM;
  177. goto err3;
  178. }
  179. prop[i++] = POWER_SUPPLY_PROP_PRESENT;
  180. if (charge_gpiod)
  181. prop[i++] = POWER_SUPPLY_PROP_STATUS;
  182. if (pdata->batt_tech >= 0)
  183. prop[i++] = POWER_SUPPLY_PROP_TECHNOLOGY;
  184. if (pdata->temp_aux >= 0)
  185. prop[i++] = POWER_SUPPLY_PROP_TEMP;
  186. if (pdata->batt_aux >= 0)
  187. prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_NOW;
  188. if (pdata->max_voltage >= 0)
  189. prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_MAX;
  190. if (pdata->min_voltage >= 0)
  191. prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_MIN;
  192. INIT_WORK(&bat_work, wm97xx_bat_work);
  193. if (!pdata->batt_name) {
  194. dev_info(&dev->dev, "Please consider setting proper battery "
  195. "name in platform definition file, falling "
  196. "back to name \"wm97xx-batt\"\n");
  197. bat_psy_desc.name = "wm97xx-batt";
  198. } else
  199. bat_psy_desc.name = pdata->batt_name;
  200. bat_psy_desc.properties = prop;
  201. bat_psy_desc.num_properties = props;
  202. bat_psy = power_supply_register(&dev->dev, &bat_psy_desc, &cfg);
  203. if (!IS_ERR(bat_psy)) {
  204. schedule_work(&bat_work);
  205. } else {
  206. ret = PTR_ERR(bat_psy);
  207. goto err4;
  208. }
  209. return 0;
  210. err4:
  211. kfree(prop);
  212. err3:
  213. if (charge_gpiod)
  214. free_irq(gpiod_to_irq(charge_gpiod), dev);
  215. return ret;
  216. }
  217. static int wm97xx_bat_remove(struct platform_device *dev)
  218. {
  219. if (charge_gpiod)
  220. free_irq(gpiod_to_irq(charge_gpiod), dev);
  221. cancel_work_sync(&bat_work);
  222. power_supply_unregister(bat_psy);
  223. kfree(prop);
  224. return 0;
  225. }
  226. static struct platform_driver wm97xx_bat_driver = {
  227. .driver = {
  228. .name = "wm97xx-battery",
  229. #ifdef CONFIG_PM
  230. .pm = &wm97xx_bat_pm_ops,
  231. #endif
  232. },
  233. .probe = wm97xx_bat_probe,
  234. .remove = wm97xx_bat_remove,
  235. };
  236. module_platform_driver(wm97xx_bat_driver);
  237. MODULE_LICENSE("GPL");
  238. MODULE_AUTHOR("Marek Vasut <[email protected]>");
  239. MODULE_DESCRIPTION("WM97xx battery driver");