wm97xx_battery.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 <marek.vasut@gmail.com>
  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.h>
  18. #include <linux/irq.h>
  19. #include <linux/slab.h>
  20. static struct work_struct bat_work;
  21. static DEFINE_MUTEX(work_lock);
  22. static int bat_status = POWER_SUPPLY_STATUS_UNKNOWN;
  23. static enum power_supply_property *prop;
  24. static unsigned long wm97xx_read_bat(struct power_supply *bat_ps)
  25. {
  26. struct wm97xx_batt_pdata *pdata = power_supply_get_drvdata(bat_ps);
  27. return wm97xx_read_aux_adc(dev_get_drvdata(bat_ps->dev.parent),
  28. pdata->batt_aux) * pdata->batt_mult /
  29. pdata->batt_div;
  30. }
  31. static unsigned long wm97xx_read_temp(struct power_supply *bat_ps)
  32. {
  33. struct wm97xx_batt_pdata *pdata = power_supply_get_drvdata(bat_ps);
  34. return wm97xx_read_aux_adc(dev_get_drvdata(bat_ps->dev.parent),
  35. pdata->temp_aux) * pdata->temp_mult /
  36. pdata->temp_div;
  37. }
  38. static int wm97xx_bat_get_property(struct power_supply *bat_ps,
  39. enum power_supply_property psp,
  40. union power_supply_propval *val)
  41. {
  42. struct wm97xx_batt_pdata *pdata = power_supply_get_drvdata(bat_ps);
  43. switch (psp) {
  44. case POWER_SUPPLY_PROP_STATUS:
  45. val->intval = bat_status;
  46. break;
  47. case POWER_SUPPLY_PROP_TECHNOLOGY:
  48. val->intval = pdata->batt_tech;
  49. break;
  50. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  51. if (pdata->batt_aux >= 0)
  52. val->intval = wm97xx_read_bat(bat_ps);
  53. else
  54. return -EINVAL;
  55. break;
  56. case POWER_SUPPLY_PROP_TEMP:
  57. if (pdata->temp_aux >= 0)
  58. val->intval = wm97xx_read_temp(bat_ps);
  59. else
  60. return -EINVAL;
  61. break;
  62. case POWER_SUPPLY_PROP_VOLTAGE_MAX:
  63. if (pdata->max_voltage >= 0)
  64. val->intval = pdata->max_voltage;
  65. else
  66. return -EINVAL;
  67. break;
  68. case POWER_SUPPLY_PROP_VOLTAGE_MIN:
  69. if (pdata->min_voltage >= 0)
  70. val->intval = pdata->min_voltage;
  71. else
  72. return -EINVAL;
  73. break;
  74. case POWER_SUPPLY_PROP_PRESENT:
  75. val->intval = 1;
  76. break;
  77. default:
  78. return -EINVAL;
  79. }
  80. return 0;
  81. }
  82. static void wm97xx_bat_external_power_changed(struct power_supply *bat_ps)
  83. {
  84. schedule_work(&bat_work);
  85. }
  86. static void wm97xx_bat_update(struct power_supply *bat_ps)
  87. {
  88. int old_status = bat_status;
  89. struct wm97xx_batt_pdata *pdata = power_supply_get_drvdata(bat_ps);
  90. mutex_lock(&work_lock);
  91. bat_status = (pdata->charge_gpio >= 0) ?
  92. (gpio_get_value(pdata->charge_gpio) ?
  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. if (gpio_is_valid(pdata->charge_gpio)) {
  150. ret = gpio_request(pdata->charge_gpio, "BATT CHRG");
  151. if (ret)
  152. goto err;
  153. ret = gpio_direction_input(pdata->charge_gpio);
  154. if (ret)
  155. goto err2;
  156. ret = request_irq(gpio_to_irq(pdata->charge_gpio),
  157. wm97xx_chrg_irq, 0,
  158. "AC Detect", dev);
  159. if (ret)
  160. goto err2;
  161. props++; /* POWER_SUPPLY_PROP_STATUS */
  162. }
  163. if (pdata->batt_tech >= 0)
  164. props++; /* POWER_SUPPLY_PROP_TECHNOLOGY */
  165. if (pdata->temp_aux >= 0)
  166. props++; /* POWER_SUPPLY_PROP_TEMP */
  167. if (pdata->batt_aux >= 0)
  168. props++; /* POWER_SUPPLY_PROP_VOLTAGE_NOW */
  169. if (pdata->max_voltage >= 0)
  170. props++; /* POWER_SUPPLY_PROP_VOLTAGE_MAX */
  171. if (pdata->min_voltage >= 0)
  172. props++; /* POWER_SUPPLY_PROP_VOLTAGE_MIN */
  173. prop = kcalloc(props, sizeof(*prop), GFP_KERNEL);
  174. if (!prop) {
  175. ret = -ENOMEM;
  176. goto err3;
  177. }
  178. prop[i++] = POWER_SUPPLY_PROP_PRESENT;
  179. if (pdata->charge_gpio >= 0)
  180. prop[i++] = POWER_SUPPLY_PROP_STATUS;
  181. if (pdata->batt_tech >= 0)
  182. prop[i++] = POWER_SUPPLY_PROP_TECHNOLOGY;
  183. if (pdata->temp_aux >= 0)
  184. prop[i++] = POWER_SUPPLY_PROP_TEMP;
  185. if (pdata->batt_aux >= 0)
  186. prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_NOW;
  187. if (pdata->max_voltage >= 0)
  188. prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_MAX;
  189. if (pdata->min_voltage >= 0)
  190. prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_MIN;
  191. INIT_WORK(&bat_work, wm97xx_bat_work);
  192. if (!pdata->batt_name) {
  193. dev_info(&dev->dev, "Please consider setting proper battery "
  194. "name in platform definition file, falling "
  195. "back to name \"wm97xx-batt\"\n");
  196. bat_psy_desc.name = "wm97xx-batt";
  197. } else
  198. bat_psy_desc.name = pdata->batt_name;
  199. bat_psy_desc.properties = prop;
  200. bat_psy_desc.num_properties = props;
  201. bat_psy = power_supply_register(&dev->dev, &bat_psy_desc, &cfg);
  202. if (!IS_ERR(bat_psy)) {
  203. schedule_work(&bat_work);
  204. } else {
  205. ret = PTR_ERR(bat_psy);
  206. goto err4;
  207. }
  208. return 0;
  209. err4:
  210. kfree(prop);
  211. err3:
  212. if (gpio_is_valid(pdata->charge_gpio))
  213. free_irq(gpio_to_irq(pdata->charge_gpio), dev);
  214. err2:
  215. if (gpio_is_valid(pdata->charge_gpio))
  216. gpio_free(pdata->charge_gpio);
  217. err:
  218. return ret;
  219. }
  220. static int wm97xx_bat_remove(struct platform_device *dev)
  221. {
  222. struct wm97xx_batt_pdata *pdata = dev->dev.platform_data;
  223. if (pdata && gpio_is_valid(pdata->charge_gpio)) {
  224. free_irq(gpio_to_irq(pdata->charge_gpio), dev);
  225. gpio_free(pdata->charge_gpio);
  226. }
  227. cancel_work_sync(&bat_work);
  228. power_supply_unregister(bat_psy);
  229. kfree(prop);
  230. return 0;
  231. }
  232. static struct platform_driver wm97xx_bat_driver = {
  233. .driver = {
  234. .name = "wm97xx-battery",
  235. #ifdef CONFIG_PM
  236. .pm = &wm97xx_bat_pm_ops,
  237. #endif
  238. },
  239. .probe = wm97xx_bat_probe,
  240. .remove = wm97xx_bat_remove,
  241. };
  242. module_platform_driver(wm97xx_bat_driver);
  243. MODULE_LICENSE("GPL");
  244. MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
  245. MODULE_DESCRIPTION("WM97xx battery driver");