wm97xx_battery.c 7.7 KB

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