wm97xx_battery.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. static DEFINE_MUTEX(bat_lock);
  26. static struct work_struct bat_work;
  27. struct mutex work_lock;
  28. static int bat_status = POWER_SUPPLY_STATUS_UNKNOWN;
  29. static struct wm97xx_batt_info *gpdata;
  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_scheduled_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 (gpdata) {
  153. dev_err(&dev->dev, "Do not pass platform_data through "
  154. "wm97xx_bat_set_pdata!\n");
  155. return -EINVAL;
  156. } else
  157. pdata = wmdata->batt_pdata;
  158. if (dev->id != -1)
  159. return -EINVAL;
  160. mutex_init(&work_lock);
  161. if (!pdata) {
  162. dev_err(&dev->dev, "No platform_data supplied\n");
  163. return -EINVAL;
  164. }
  165. if (gpio_is_valid(pdata->charge_gpio)) {
  166. ret = gpio_request(pdata->charge_gpio, "BATT CHRG");
  167. if (ret)
  168. goto err;
  169. ret = gpio_direction_input(pdata->charge_gpio);
  170. if (ret)
  171. goto err2;
  172. ret = request_irq(gpio_to_irq(pdata->charge_gpio),
  173. wm97xx_chrg_irq, IRQF_DISABLED,
  174. "AC Detect", 0);
  175. if (ret)
  176. goto err2;
  177. props++; /* POWER_SUPPLY_PROP_STATUS */
  178. }
  179. if (pdata->batt_tech >= 0)
  180. props++; /* POWER_SUPPLY_PROP_TECHNOLOGY */
  181. if (pdata->temp_aux >= 0)
  182. props++; /* POWER_SUPPLY_PROP_TEMP */
  183. if (pdata->batt_aux >= 0)
  184. props++; /* POWER_SUPPLY_PROP_VOLTAGE_NOW */
  185. if (pdata->max_voltage >= 0)
  186. props++; /* POWER_SUPPLY_PROP_VOLTAGE_MAX */
  187. if (pdata->min_voltage >= 0)
  188. props++; /* POWER_SUPPLY_PROP_VOLTAGE_MIN */
  189. prop = kzalloc(props * sizeof(*prop), GFP_KERNEL);
  190. if (!prop)
  191. goto err3;
  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_ps.name = "wm97xx-batt";
  211. } else
  212. bat_ps.name = pdata->batt_name;
  213. bat_ps.properties = prop;
  214. bat_ps.num_properties = props;
  215. ret = power_supply_register(&dev->dev, &bat_ps);
  216. if (!ret)
  217. schedule_work(&bat_work);
  218. else
  219. goto err4;
  220. return 0;
  221. err4:
  222. kfree(prop);
  223. err3:
  224. if (gpio_is_valid(pdata->charge_gpio))
  225. free_irq(gpio_to_irq(pdata->charge_gpio), dev);
  226. err2:
  227. if (gpio_is_valid(pdata->charge_gpio))
  228. gpio_free(pdata->charge_gpio);
  229. err:
  230. return ret;
  231. }
  232. static int __devexit wm97xx_bat_remove(struct platform_device *dev)
  233. {
  234. struct wm97xx_pdata *wmdata = dev->dev.platform_data;
  235. struct wm97xx_batt_pdata *pdata = wmdata->batt_pdata;
  236. if (pdata && gpio_is_valid(pdata->charge_gpio)) {
  237. free_irq(gpio_to_irq(pdata->charge_gpio), dev);
  238. gpio_free(pdata->charge_gpio);
  239. }
  240. flush_scheduled_work();
  241. power_supply_unregister(&bat_ps);
  242. kfree(prop);
  243. return 0;
  244. }
  245. static struct platform_driver wm97xx_bat_driver = {
  246. .driver = {
  247. .name = "wm97xx-battery",
  248. .owner = THIS_MODULE,
  249. #ifdef CONFIG_PM
  250. .pm = &wm97xx_bat_pm_ops,
  251. #endif
  252. },
  253. .probe = wm97xx_bat_probe,
  254. .remove = __devexit_p(wm97xx_bat_remove),
  255. };
  256. static int __init wm97xx_bat_init(void)
  257. {
  258. return platform_driver_register(&wm97xx_bat_driver);
  259. }
  260. static void __exit wm97xx_bat_exit(void)
  261. {
  262. platform_driver_unregister(&wm97xx_bat_driver);
  263. }
  264. void wm97xx_bat_set_pdata(struct wm97xx_batt_info *data)
  265. {
  266. gpdata = data;
  267. }
  268. EXPORT_SYMBOL_GPL(wm97xx_bat_set_pdata);
  269. module_init(wm97xx_bat_init);
  270. module_exit(wm97xx_bat_exit);
  271. MODULE_LICENSE("GPL");
  272. MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
  273. MODULE_DESCRIPTION("WM97xx battery driver");