wm97xx_battery.c 6.3 KB

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