wm97xx_battery.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 DEFINE_MUTEX(bat_lock);
  27. static struct work_struct bat_work;
  28. static struct mutex work_lock;
  29. static int bat_status = POWER_SUPPLY_STATUS_UNKNOWN;
  30. static enum power_supply_property *prop;
  31. static unsigned long wm97xx_read_bat(struct power_supply *bat_ps)
  32. {
  33. struct wm97xx_pdata *wmdata = bat_ps->dev->parent->platform_data;
  34. struct wm97xx_batt_pdata *pdata = wmdata->batt_pdata;
  35. return wm97xx_read_aux_adc(dev_get_drvdata(bat_ps->dev->parent),
  36. pdata->batt_aux) * pdata->batt_mult /
  37. pdata->batt_div;
  38. }
  39. static unsigned long wm97xx_read_temp(struct power_supply *bat_ps)
  40. {
  41. struct wm97xx_pdata *wmdata = bat_ps->dev->parent->platform_data;
  42. struct wm97xx_batt_pdata *pdata = wmdata->batt_pdata;
  43. return wm97xx_read_aux_adc(dev_get_drvdata(bat_ps->dev->parent),
  44. pdata->temp_aux) * pdata->temp_mult /
  45. pdata->temp_div;
  46. }
  47. static int wm97xx_bat_get_property(struct power_supply *bat_ps,
  48. enum power_supply_property psp,
  49. union power_supply_propval *val)
  50. {
  51. struct wm97xx_pdata *wmdata = bat_ps->dev->parent->platform_data;
  52. struct wm97xx_batt_pdata *pdata = wmdata->batt_pdata;
  53. switch (psp) {
  54. case POWER_SUPPLY_PROP_STATUS:
  55. val->intval = bat_status;
  56. break;
  57. case POWER_SUPPLY_PROP_TECHNOLOGY:
  58. val->intval = pdata->batt_tech;
  59. break;
  60. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  61. if (pdata->batt_aux >= 0)
  62. val->intval = wm97xx_read_bat(bat_ps);
  63. else
  64. return -EINVAL;
  65. break;
  66. case POWER_SUPPLY_PROP_TEMP:
  67. if (pdata->temp_aux >= 0)
  68. val->intval = wm97xx_read_temp(bat_ps);
  69. else
  70. return -EINVAL;
  71. break;
  72. case POWER_SUPPLY_PROP_VOLTAGE_MAX:
  73. if (pdata->max_voltage >= 0)
  74. val->intval = pdata->max_voltage;
  75. else
  76. return -EINVAL;
  77. break;
  78. case POWER_SUPPLY_PROP_VOLTAGE_MIN:
  79. if (pdata->min_voltage >= 0)
  80. val->intval = pdata->min_voltage;
  81. else
  82. return -EINVAL;
  83. break;
  84. case POWER_SUPPLY_PROP_PRESENT:
  85. val->intval = 1;
  86. break;
  87. default:
  88. return -EINVAL;
  89. }
  90. return 0;
  91. }
  92. static void wm97xx_bat_external_power_changed(struct power_supply *bat_ps)
  93. {
  94. schedule_work(&bat_work);
  95. }
  96. static void wm97xx_bat_update(struct power_supply *bat_ps)
  97. {
  98. int old_status = bat_status;
  99. struct wm97xx_pdata *wmdata = bat_ps->dev->parent->platform_data;
  100. struct wm97xx_batt_pdata *pdata = wmdata->batt_pdata;
  101. mutex_lock(&work_lock);
  102. bat_status = (pdata->charge_gpio >= 0) ?
  103. (gpio_get_value(pdata->charge_gpio) ?
  104. POWER_SUPPLY_STATUS_DISCHARGING :
  105. POWER_SUPPLY_STATUS_CHARGING) :
  106. POWER_SUPPLY_STATUS_UNKNOWN;
  107. if (old_status != bat_status) {
  108. pr_debug("%s: %i -> %i\n", bat_ps->name, old_status,
  109. bat_status);
  110. power_supply_changed(bat_ps);
  111. }
  112. mutex_unlock(&work_lock);
  113. }
  114. static struct power_supply bat_ps = {
  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_ps);
  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_sync(&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 __devinit 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. mutex_init(&work_lock);
  160. if (!pdata) {
  161. dev_err(&dev->dev, "No platform_data supplied\n");
  162. return -EINVAL;
  163. }
  164. if (gpio_is_valid(pdata->charge_gpio)) {
  165. ret = gpio_request(pdata->charge_gpio, "BATT CHRG");
  166. if (ret)
  167. goto err;
  168. ret = gpio_direction_input(pdata->charge_gpio);
  169. if (ret)
  170. goto err2;
  171. ret = request_irq(gpio_to_irq(pdata->charge_gpio),
  172. wm97xx_chrg_irq, IRQF_DISABLED,
  173. "AC Detect", dev);
  174. if (ret)
  175. goto err2;
  176. props++; /* POWER_SUPPLY_PROP_STATUS */
  177. }
  178. if (pdata->batt_tech >= 0)
  179. props++; /* POWER_SUPPLY_PROP_TECHNOLOGY */
  180. if (pdata->temp_aux >= 0)
  181. props++; /* POWER_SUPPLY_PROP_TEMP */
  182. if (pdata->batt_aux >= 0)
  183. props++; /* POWER_SUPPLY_PROP_VOLTAGE_NOW */
  184. if (pdata->max_voltage >= 0)
  185. props++; /* POWER_SUPPLY_PROP_VOLTAGE_MAX */
  186. if (pdata->min_voltage >= 0)
  187. props++; /* POWER_SUPPLY_PROP_VOLTAGE_MIN */
  188. prop = kzalloc(props * sizeof(*prop), GFP_KERNEL);
  189. if (!prop)
  190. goto err3;
  191. prop[i++] = POWER_SUPPLY_PROP_PRESENT;
  192. if (pdata->charge_gpio >= 0)
  193. prop[i++] = POWER_SUPPLY_PROP_STATUS;
  194. if (pdata->batt_tech >= 0)
  195. prop[i++] = POWER_SUPPLY_PROP_TECHNOLOGY;
  196. if (pdata->temp_aux >= 0)
  197. prop[i++] = POWER_SUPPLY_PROP_TEMP;
  198. if (pdata->batt_aux >= 0)
  199. prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_NOW;
  200. if (pdata->max_voltage >= 0)
  201. prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_MAX;
  202. if (pdata->min_voltage >= 0)
  203. prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_MIN;
  204. INIT_WORK(&bat_work, wm97xx_bat_work);
  205. if (!pdata->batt_name) {
  206. dev_info(&dev->dev, "Please consider setting proper battery "
  207. "name in platform definition file, falling "
  208. "back to name \"wm97xx-batt\"\n");
  209. bat_ps.name = "wm97xx-batt";
  210. } else
  211. bat_ps.name = pdata->batt_name;
  212. bat_ps.properties = prop;
  213. bat_ps.num_properties = props;
  214. ret = power_supply_register(&dev->dev, &bat_ps);
  215. if (!ret)
  216. schedule_work(&bat_work);
  217. else
  218. goto err4;
  219. return 0;
  220. err4:
  221. kfree(prop);
  222. err3:
  223. if (gpio_is_valid(pdata->charge_gpio))
  224. free_irq(gpio_to_irq(pdata->charge_gpio), dev);
  225. err2:
  226. if (gpio_is_valid(pdata->charge_gpio))
  227. gpio_free(pdata->charge_gpio);
  228. err:
  229. return ret;
  230. }
  231. static int __devexit wm97xx_bat_remove(struct platform_device *dev)
  232. {
  233. struct wm97xx_pdata *wmdata = dev->dev.platform_data;
  234. struct wm97xx_batt_pdata *pdata = wmdata->batt_pdata;
  235. if (pdata && gpio_is_valid(pdata->charge_gpio)) {
  236. free_irq(gpio_to_irq(pdata->charge_gpio), dev);
  237. gpio_free(pdata->charge_gpio);
  238. }
  239. cancel_work_sync(&bat_work);
  240. power_supply_unregister(&bat_ps);
  241. kfree(prop);
  242. return 0;
  243. }
  244. static struct platform_driver wm97xx_bat_driver = {
  245. .driver = {
  246. .name = "wm97xx-battery",
  247. .owner = THIS_MODULE,
  248. #ifdef CONFIG_PM
  249. .pm = &wm97xx_bat_pm_ops,
  250. #endif
  251. },
  252. .probe = wm97xx_bat_probe,
  253. .remove = __devexit_p(wm97xx_bat_remove),
  254. };
  255. static int __init wm97xx_bat_init(void)
  256. {
  257. return platform_driver_register(&wm97xx_bat_driver);
  258. }
  259. static void __exit wm97xx_bat_exit(void)
  260. {
  261. platform_driver_unregister(&wm97xx_bat_driver);
  262. }
  263. module_init(wm97xx_bat_init);
  264. module_exit(wm97xx_bat_exit);
  265. MODULE_LICENSE("GPL");
  266. MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
  267. MODULE_DESCRIPTION("WM97xx battery driver");