wm97xx_battery.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. static DEFINE_MUTEX(bat_lock);
  25. static struct work_struct bat_work;
  26. struct mutex work_lock;
  27. static int bat_status = POWER_SUPPLY_STATUS_UNKNOWN;
  28. static struct wm97xx_batt_info *gpdata;
  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(bat_ps->dev->parent->driver_data,
  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(bat_ps->dev->parent->driver_data,
  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. #ifdef CONFIG_PM
  124. static int wm97xx_bat_suspend(struct platform_device *dev, pm_message_t state)
  125. {
  126. flush_scheduled_work();
  127. return 0;
  128. }
  129. static int wm97xx_bat_resume(struct platform_device *dev)
  130. {
  131. schedule_work(&bat_work);
  132. return 0;
  133. }
  134. #else
  135. #define wm97xx_bat_suspend NULL
  136. #define wm97xx_bat_resume NULL
  137. #endif
  138. static int __devinit wm97xx_bat_probe(struct platform_device *dev)
  139. {
  140. int ret = 0;
  141. int props = 1; /* POWER_SUPPLY_PROP_PRESENT */
  142. int i = 0;
  143. struct wm97xx_pdata *wmdata = dev->dev.platform_data;
  144. struct wm97xx_batt_pdata *pdata;
  145. if (gpdata) {
  146. dev_err(&dev->dev, "Do not pass platform_data through "
  147. "wm97xx_bat_set_pdata!\n");
  148. return -EINVAL;
  149. } else
  150. pdata = wmdata->batt_pdata;
  151. if (dev->id != -1)
  152. return -EINVAL;
  153. mutex_init(&work_lock);
  154. if (!pdata) {
  155. dev_err(&dev->dev, "No platform_data supplied\n");
  156. return -EINVAL;
  157. }
  158. if (pdata->charge_gpio >= 0 && gpio_is_valid(pdata->charge_gpio)) {
  159. ret = gpio_request(pdata->charge_gpio, "BATT CHRG");
  160. if (ret)
  161. goto err;
  162. ret = gpio_direction_input(pdata->charge_gpio);
  163. if (ret)
  164. goto err2;
  165. props++; /* POWER_SUPPLY_PROP_STATUS */
  166. }
  167. if (pdata->batt_tech >= 0)
  168. props++; /* POWER_SUPPLY_PROP_TECHNOLOGY */
  169. if (pdata->temp_aux >= 0)
  170. props++; /* POWER_SUPPLY_PROP_TEMP */
  171. if (pdata->batt_aux >= 0)
  172. props++; /* POWER_SUPPLY_PROP_VOLTAGE_NOW */
  173. if (pdata->max_voltage >= 0)
  174. props++; /* POWER_SUPPLY_PROP_VOLTAGE_MAX */
  175. if (pdata->min_voltage >= 0)
  176. props++; /* POWER_SUPPLY_PROP_VOLTAGE_MIN */
  177. prop = kzalloc(props * sizeof(*prop), GFP_KERNEL);
  178. if (!prop)
  179. goto err2;
  180. prop[i++] = POWER_SUPPLY_PROP_PRESENT;
  181. if (pdata->charge_gpio >= 0)
  182. prop[i++] = POWER_SUPPLY_PROP_STATUS;
  183. if (pdata->batt_tech >= 0)
  184. prop[i++] = POWER_SUPPLY_PROP_TECHNOLOGY;
  185. if (pdata->temp_aux >= 0)
  186. prop[i++] = POWER_SUPPLY_PROP_TEMP;
  187. if (pdata->batt_aux >= 0)
  188. prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_NOW;
  189. if (pdata->max_voltage >= 0)
  190. prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_MAX;
  191. if (pdata->min_voltage >= 0)
  192. prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_MIN;
  193. INIT_WORK(&bat_work, wm97xx_bat_work);
  194. if (!pdata->batt_name) {
  195. dev_info(&dev->dev, "Please consider setting proper battery "
  196. "name in platform definition file, falling "
  197. "back to name \"wm97xx-batt\"\n");
  198. bat_ps.name = "wm97xx-batt";
  199. } else
  200. bat_ps.name = pdata->batt_name;
  201. bat_ps.properties = prop;
  202. bat_ps.num_properties = props;
  203. ret = power_supply_register(&dev->dev, &bat_ps);
  204. if (!ret)
  205. schedule_work(&bat_work);
  206. else
  207. goto err3;
  208. return 0;
  209. err3:
  210. kfree(prop);
  211. err2:
  212. gpio_free(pdata->charge_gpio);
  213. err:
  214. return ret;
  215. }
  216. static int __devexit wm97xx_bat_remove(struct platform_device *dev)
  217. {
  218. struct wm97xx_pdata *wmdata = dev->dev.platform_data;
  219. struct wm97xx_batt_pdata *pdata = wmdata->batt_pdata;
  220. if (pdata && pdata->charge_gpio && pdata->charge_gpio >= 0)
  221. gpio_free(pdata->charge_gpio);
  222. flush_scheduled_work();
  223. power_supply_unregister(&bat_ps);
  224. kfree(prop);
  225. return 0;
  226. }
  227. static struct platform_driver wm97xx_bat_driver = {
  228. .driver = {
  229. .name = "wm97xx-battery",
  230. .owner = THIS_MODULE,
  231. },
  232. .probe = wm97xx_bat_probe,
  233. .remove = __devexit_p(wm97xx_bat_remove),
  234. .suspend = wm97xx_bat_suspend,
  235. .resume = wm97xx_bat_resume,
  236. };
  237. static int __init wm97xx_bat_init(void)
  238. {
  239. return platform_driver_register(&wm97xx_bat_driver);
  240. }
  241. static void __exit wm97xx_bat_exit(void)
  242. {
  243. platform_driver_unregister(&wm97xx_bat_driver);
  244. }
  245. void wm97xx_bat_set_pdata(struct wm97xx_batt_info *data)
  246. {
  247. gpdata = data;
  248. }
  249. EXPORT_SYMBOL_GPL(wm97xx_bat_set_pdata);
  250. module_init(wm97xx_bat_init);
  251. module_exit(wm97xx_bat_exit);
  252. MODULE_LICENSE("GPL");
  253. MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
  254. MODULE_DESCRIPTION("WM97xx battery driver");