wm97xx_battery.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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->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_ps = {
  114. .type = POWER_SUPPLY_TYPE_BATTERY,
  115. .get_property = wm97xx_bat_get_property,
  116. .external_power_changed = wm97xx_bat_external_power_changed,
  117. .use_for_apm = 1,
  118. };
  119. static void wm97xx_bat_work(struct work_struct *work)
  120. {
  121. wm97xx_bat_update(&bat_ps);
  122. }
  123. static irqreturn_t wm97xx_chrg_irq(int irq, void *data)
  124. {
  125. schedule_work(&bat_work);
  126. return IRQ_HANDLED;
  127. }
  128. #ifdef CONFIG_PM
  129. static int wm97xx_bat_suspend(struct device *dev)
  130. {
  131. flush_work_sync(&bat_work);
  132. return 0;
  133. }
  134. static int wm97xx_bat_resume(struct device *dev)
  135. {
  136. schedule_work(&bat_work);
  137. return 0;
  138. }
  139. static const struct dev_pm_ops wm97xx_bat_pm_ops = {
  140. .suspend = wm97xx_bat_suspend,
  141. .resume = wm97xx_bat_resume,
  142. };
  143. #endif
  144. static int __devinit wm97xx_bat_probe(struct platform_device *dev)
  145. {
  146. int ret = 0;
  147. int props = 1; /* POWER_SUPPLY_PROP_PRESENT */
  148. int i = 0;
  149. struct wm97xx_pdata *wmdata = dev->dev.platform_data;
  150. struct wm97xx_batt_pdata *pdata;
  151. if (!wmdata) {
  152. dev_err(&dev->dev, "No platform data supplied\n");
  153. return -EINVAL;
  154. }
  155. pdata = wmdata->batt_pdata;
  156. if (dev->id != -1)
  157. return -EINVAL;
  158. if (!pdata) {
  159. dev_err(&dev->dev, "No platform_data supplied\n");
  160. return -EINVAL;
  161. }
  162. if (gpio_is_valid(pdata->charge_gpio)) {
  163. ret = gpio_request(pdata->charge_gpio, "BATT CHRG");
  164. if (ret)
  165. goto err;
  166. ret = gpio_direction_input(pdata->charge_gpio);
  167. if (ret)
  168. goto err2;
  169. ret = request_irq(gpio_to_irq(pdata->charge_gpio),
  170. wm97xx_chrg_irq, 0,
  171. "AC Detect", dev);
  172. if (ret)
  173. goto err2;
  174. props++; /* POWER_SUPPLY_PROP_STATUS */
  175. }
  176. if (pdata->batt_tech >= 0)
  177. props++; /* POWER_SUPPLY_PROP_TECHNOLOGY */
  178. if (pdata->temp_aux >= 0)
  179. props++; /* POWER_SUPPLY_PROP_TEMP */
  180. if (pdata->batt_aux >= 0)
  181. props++; /* POWER_SUPPLY_PROP_VOLTAGE_NOW */
  182. if (pdata->max_voltage >= 0)
  183. props++; /* POWER_SUPPLY_PROP_VOLTAGE_MAX */
  184. if (pdata->min_voltage >= 0)
  185. props++; /* POWER_SUPPLY_PROP_VOLTAGE_MIN */
  186. prop = kzalloc(props * sizeof(*prop), GFP_KERNEL);
  187. if (!prop)
  188. goto err3;
  189. prop[i++] = POWER_SUPPLY_PROP_PRESENT;
  190. if (pdata->charge_gpio >= 0)
  191. prop[i++] = POWER_SUPPLY_PROP_STATUS;
  192. if (pdata->batt_tech >= 0)
  193. prop[i++] = POWER_SUPPLY_PROP_TECHNOLOGY;
  194. if (pdata->temp_aux >= 0)
  195. prop[i++] = POWER_SUPPLY_PROP_TEMP;
  196. if (pdata->batt_aux >= 0)
  197. prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_NOW;
  198. if (pdata->max_voltage >= 0)
  199. prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_MAX;
  200. if (pdata->min_voltage >= 0)
  201. prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_MIN;
  202. INIT_WORK(&bat_work, wm97xx_bat_work);
  203. if (!pdata->batt_name) {
  204. dev_info(&dev->dev, "Please consider setting proper battery "
  205. "name in platform definition file, falling "
  206. "back to name \"wm97xx-batt\"\n");
  207. bat_ps.name = "wm97xx-batt";
  208. } else
  209. bat_ps.name = pdata->batt_name;
  210. bat_ps.properties = prop;
  211. bat_ps.num_properties = props;
  212. ret = power_supply_register(&dev->dev, &bat_ps);
  213. if (!ret)
  214. schedule_work(&bat_work);
  215. else
  216. goto err4;
  217. return 0;
  218. err4:
  219. kfree(prop);
  220. err3:
  221. if (gpio_is_valid(pdata->charge_gpio))
  222. free_irq(gpio_to_irq(pdata->charge_gpio), dev);
  223. err2:
  224. if (gpio_is_valid(pdata->charge_gpio))
  225. gpio_free(pdata->charge_gpio);
  226. err:
  227. return ret;
  228. }
  229. static int __devexit wm97xx_bat_remove(struct platform_device *dev)
  230. {
  231. struct wm97xx_pdata *wmdata = dev->dev.platform_data;
  232. struct wm97xx_batt_pdata *pdata = wmdata->batt_pdata;
  233. if (pdata && gpio_is_valid(pdata->charge_gpio)) {
  234. free_irq(gpio_to_irq(pdata->charge_gpio), dev);
  235. gpio_free(pdata->charge_gpio);
  236. }
  237. cancel_work_sync(&bat_work);
  238. power_supply_unregister(&bat_ps);
  239. kfree(prop);
  240. return 0;
  241. }
  242. static struct platform_driver wm97xx_bat_driver = {
  243. .driver = {
  244. .name = "wm97xx-battery",
  245. .owner = THIS_MODULE,
  246. #ifdef CONFIG_PM
  247. .pm = &wm97xx_bat_pm_ops,
  248. #endif
  249. },
  250. .probe = wm97xx_bat_probe,
  251. .remove = __devexit_p(wm97xx_bat_remove),
  252. };
  253. module_platform_driver(wm97xx_bat_driver);
  254. MODULE_LICENSE("GPL");
  255. MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
  256. MODULE_DESCRIPTION("WM97xx battery driver");