wm97xx_battery.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Battery measurement code for WM97xx
  3. *
  4. * based on tosa_battery.c
  5. *
  6. * Copyright (C) 2008 Marek Vasut <marek.vasut@gmail.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. */
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/power_supply.h>
  18. #include <linux/wm97xx.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/gpio.h>
  22. #include <linux/irq.h>
  23. #include <linux/slab.h>
  24. static struct work_struct bat_work;
  25. static DEFINE_MUTEX(work_lock);
  26. static int bat_status = POWER_SUPPLY_STATUS_UNKNOWN;
  27. static enum power_supply_property *prop;
  28. static unsigned long wm97xx_read_bat(struct power_supply *bat_ps)
  29. {
  30. struct wm97xx_pdata *wmdata = bat_ps->dev.parent->platform_data;
  31. struct wm97xx_batt_pdata *pdata = wmdata->batt_pdata;
  32. return wm97xx_read_aux_adc(dev_get_drvdata(bat_ps->dev.parent),
  33. pdata->batt_aux) * pdata->batt_mult /
  34. pdata->batt_div;
  35. }
  36. static unsigned long wm97xx_read_temp(struct power_supply *bat_ps)
  37. {
  38. struct wm97xx_pdata *wmdata = bat_ps->dev.parent->platform_data;
  39. struct wm97xx_batt_pdata *pdata = wmdata->batt_pdata;
  40. return wm97xx_read_aux_adc(dev_get_drvdata(bat_ps->dev.parent),
  41. pdata->temp_aux) * pdata->temp_mult /
  42. pdata->temp_div;
  43. }
  44. static int wm97xx_bat_get_property(struct power_supply *bat_ps,
  45. enum power_supply_property psp,
  46. union power_supply_propval *val)
  47. {
  48. struct wm97xx_pdata *wmdata = bat_ps->dev.parent->platform_data;
  49. struct wm97xx_batt_pdata *pdata = wmdata->batt_pdata;
  50. switch (psp) {
  51. case POWER_SUPPLY_PROP_STATUS:
  52. val->intval = bat_status;
  53. break;
  54. case POWER_SUPPLY_PROP_TECHNOLOGY:
  55. val->intval = pdata->batt_tech;
  56. break;
  57. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  58. if (pdata->batt_aux >= 0)
  59. val->intval = wm97xx_read_bat(bat_ps);
  60. else
  61. return -EINVAL;
  62. break;
  63. case POWER_SUPPLY_PROP_TEMP:
  64. if (pdata->temp_aux >= 0)
  65. val->intval = wm97xx_read_temp(bat_ps);
  66. else
  67. return -EINVAL;
  68. break;
  69. case POWER_SUPPLY_PROP_VOLTAGE_MAX:
  70. if (pdata->max_voltage >= 0)
  71. val->intval = pdata->max_voltage;
  72. else
  73. return -EINVAL;
  74. break;
  75. case POWER_SUPPLY_PROP_VOLTAGE_MIN:
  76. if (pdata->min_voltage >= 0)
  77. val->intval = pdata->min_voltage;
  78. else
  79. return -EINVAL;
  80. break;
  81. case POWER_SUPPLY_PROP_PRESENT:
  82. val->intval = 1;
  83. break;
  84. default:
  85. return -EINVAL;
  86. }
  87. return 0;
  88. }
  89. static void wm97xx_bat_external_power_changed(struct power_supply *bat_ps)
  90. {
  91. schedule_work(&bat_work);
  92. }
  93. static void wm97xx_bat_update(struct power_supply *bat_ps)
  94. {
  95. int old_status = bat_status;
  96. struct wm97xx_pdata *wmdata = bat_ps->dev.parent->platform_data;
  97. struct wm97xx_batt_pdata *pdata = wmdata->batt_pdata;
  98. mutex_lock(&work_lock);
  99. bat_status = (pdata->charge_gpio >= 0) ?
  100. (gpio_get_value(pdata->charge_gpio) ?
  101. POWER_SUPPLY_STATUS_DISCHARGING :
  102. POWER_SUPPLY_STATUS_CHARGING) :
  103. POWER_SUPPLY_STATUS_UNKNOWN;
  104. if (old_status != bat_status) {
  105. pr_debug("%s: %i -> %i\n", bat_ps->desc->name, old_status,
  106. bat_status);
  107. power_supply_changed(bat_ps);
  108. }
  109. mutex_unlock(&work_lock);
  110. }
  111. static struct power_supply *bat_psy;
  112. static struct power_supply_desc bat_psy_desc = {
  113. .type = POWER_SUPPLY_TYPE_BATTERY,
  114. .get_property = wm97xx_bat_get_property,
  115. .external_power_changed = wm97xx_bat_external_power_changed,
  116. .use_for_apm = 1,
  117. };
  118. static void wm97xx_bat_work(struct work_struct *work)
  119. {
  120. wm97xx_bat_update(bat_psy);
  121. }
  122. static irqreturn_t wm97xx_chrg_irq(int irq, void *data)
  123. {
  124. schedule_work(&bat_work);
  125. return IRQ_HANDLED;
  126. }
  127. #ifdef CONFIG_PM
  128. static int wm97xx_bat_suspend(struct device *dev)
  129. {
  130. flush_work(&bat_work);
  131. return 0;
  132. }
  133. static int wm97xx_bat_resume(struct device *dev)
  134. {
  135. schedule_work(&bat_work);
  136. return 0;
  137. }
  138. static const struct dev_pm_ops wm97xx_bat_pm_ops = {
  139. .suspend = wm97xx_bat_suspend,
  140. .resume = wm97xx_bat_resume,
  141. };
  142. #endif
  143. static int wm97xx_bat_probe(struct platform_device *dev)
  144. {
  145. int ret = 0;
  146. int props = 1; /* POWER_SUPPLY_PROP_PRESENT */
  147. int i = 0;
  148. struct wm97xx_pdata *wmdata = dev->dev.platform_data;
  149. struct wm97xx_batt_pdata *pdata;
  150. if (!wmdata) {
  151. dev_err(&dev->dev, "No platform data supplied\n");
  152. return -EINVAL;
  153. }
  154. pdata = wmdata->batt_pdata;
  155. if (dev->id != -1)
  156. return -EINVAL;
  157. if (!pdata) {
  158. dev_err(&dev->dev, "No platform_data supplied\n");
  159. return -EINVAL;
  160. }
  161. if (gpio_is_valid(pdata->charge_gpio)) {
  162. ret = gpio_request(pdata->charge_gpio, "BATT CHRG");
  163. if (ret)
  164. goto err;
  165. ret = gpio_direction_input(pdata->charge_gpio);
  166. if (ret)
  167. goto err2;
  168. ret = request_irq(gpio_to_irq(pdata->charge_gpio),
  169. wm97xx_chrg_irq, 0,
  170. "AC Detect", dev);
  171. if (ret)
  172. goto err2;
  173. props++; /* POWER_SUPPLY_PROP_STATUS */
  174. }
  175. if (pdata->batt_tech >= 0)
  176. props++; /* POWER_SUPPLY_PROP_TECHNOLOGY */
  177. if (pdata->temp_aux >= 0)
  178. props++; /* POWER_SUPPLY_PROP_TEMP */
  179. if (pdata->batt_aux >= 0)
  180. props++; /* POWER_SUPPLY_PROP_VOLTAGE_NOW */
  181. if (pdata->max_voltage >= 0)
  182. props++; /* POWER_SUPPLY_PROP_VOLTAGE_MAX */
  183. if (pdata->min_voltage >= 0)
  184. props++; /* POWER_SUPPLY_PROP_VOLTAGE_MIN */
  185. prop = kzalloc(props * sizeof(*prop), GFP_KERNEL);
  186. if (!prop) {
  187. ret = -ENOMEM;
  188. goto err3;
  189. }
  190. prop[i++] = POWER_SUPPLY_PROP_PRESENT;
  191. if (pdata->charge_gpio >= 0)
  192. prop[i++] = POWER_SUPPLY_PROP_STATUS;
  193. if (pdata->batt_tech >= 0)
  194. prop[i++] = POWER_SUPPLY_PROP_TECHNOLOGY;
  195. if (pdata->temp_aux >= 0)
  196. prop[i++] = POWER_SUPPLY_PROP_TEMP;
  197. if (pdata->batt_aux >= 0)
  198. prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_NOW;
  199. if (pdata->max_voltage >= 0)
  200. prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_MAX;
  201. if (pdata->min_voltage >= 0)
  202. prop[i++] = POWER_SUPPLY_PROP_VOLTAGE_MIN;
  203. INIT_WORK(&bat_work, wm97xx_bat_work);
  204. if (!pdata->batt_name) {
  205. dev_info(&dev->dev, "Please consider setting proper battery "
  206. "name in platform definition file, falling "
  207. "back to name \"wm97xx-batt\"\n");
  208. bat_psy_desc.name = "wm97xx-batt";
  209. } else
  210. bat_psy_desc.name = pdata->batt_name;
  211. bat_psy_desc.properties = prop;
  212. bat_psy_desc.num_properties = props;
  213. bat_psy = power_supply_register(&dev->dev, &bat_psy_desc, NULL);
  214. if (!IS_ERR(bat_psy)) {
  215. schedule_work(&bat_work);
  216. } else {
  217. ret = PTR_ERR(bat_psy);
  218. goto err4;
  219. }
  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 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. cancel_work_sync(&bat_work);
  241. power_supply_unregister(bat_psy);
  242. kfree(prop);
  243. return 0;
  244. }
  245. static struct platform_driver wm97xx_bat_driver = {
  246. .driver = {
  247. .name = "wm97xx-battery",
  248. #ifdef CONFIG_PM
  249. .pm = &wm97xx_bat_pm_ops,
  250. #endif
  251. },
  252. .probe = wm97xx_bat_probe,
  253. .remove = wm97xx_bat_remove,
  254. };
  255. module_platform_driver(wm97xx_bat_driver);
  256. MODULE_LICENSE("GPL");
  257. MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
  258. MODULE_DESCRIPTION("WM97xx battery driver");