wm97xx_battery.c 7.2 KB

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