twl4030_madc_battery.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Dumb driver for LiIon batteries using TWL4030 madc.
  3. *
  4. * Copyright 2013 Golden Delicious Computers
  5. * Lukas Märdian <lukas@goldelico.com>
  6. *
  7. * Based on dumb driver for gta01 battery
  8. * Copyright 2009 Openmoko, Inc
  9. * Balaji Rao <balajirrao@openmoko.org>
  10. */
  11. #include <linux/module.h>
  12. #include <linux/param.h>
  13. #include <linux/delay.h>
  14. #include <linux/workqueue.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/power_supply.h>
  17. #include <linux/slab.h>
  18. #include <linux/sort.h>
  19. #include <linux/i2c/twl4030-madc.h>
  20. #include <linux/power/twl4030_madc_battery.h>
  21. #include <linux/iio/consumer.h>
  22. struct twl4030_madc_battery {
  23. struct power_supply *psy;
  24. struct twl4030_madc_bat_platform_data *pdata;
  25. struct iio_channel *channel_temp;
  26. struct iio_channel *channel_ichg;
  27. struct iio_channel *channel_vbat;
  28. };
  29. static enum power_supply_property twl4030_madc_bat_props[] = {
  30. POWER_SUPPLY_PROP_PRESENT,
  31. POWER_SUPPLY_PROP_STATUS,
  32. POWER_SUPPLY_PROP_TECHNOLOGY,
  33. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  34. POWER_SUPPLY_PROP_CURRENT_NOW,
  35. POWER_SUPPLY_PROP_CAPACITY,
  36. POWER_SUPPLY_PROP_CHARGE_FULL,
  37. POWER_SUPPLY_PROP_CHARGE_NOW,
  38. POWER_SUPPLY_PROP_TEMP,
  39. POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
  40. };
  41. static int madc_read(struct iio_channel *channel)
  42. {
  43. int val, err;
  44. err = iio_read_channel_processed(channel, &val);
  45. if (err < 0)
  46. return err;
  47. return val;
  48. }
  49. static int twl4030_madc_bat_get_charging_status(struct twl4030_madc_battery *bt)
  50. {
  51. return (madc_read(bt->channel_ichg) > 0) ? 1 : 0;
  52. }
  53. static int twl4030_madc_bat_get_voltage(struct twl4030_madc_battery *bt)
  54. {
  55. return madc_read(bt->channel_vbat);
  56. }
  57. static int twl4030_madc_bat_get_current(struct twl4030_madc_battery *bt)
  58. {
  59. return madc_read(bt->channel_ichg) * 1000;
  60. }
  61. static int twl4030_madc_bat_get_temp(struct twl4030_madc_battery *bt)
  62. {
  63. return madc_read(bt->channel_temp) * 10;
  64. }
  65. static int twl4030_madc_bat_voltscale(struct twl4030_madc_battery *bat,
  66. int volt)
  67. {
  68. struct twl4030_madc_bat_calibration *calibration;
  69. int i, res = 0;
  70. /* choose charging curve */
  71. if (twl4030_madc_bat_get_charging_status(bat))
  72. calibration = bat->pdata->charging;
  73. else
  74. calibration = bat->pdata->discharging;
  75. if (volt > calibration[0].voltage) {
  76. res = calibration[0].level;
  77. } else {
  78. for (i = 0; calibration[i+1].voltage >= 0; i++) {
  79. if (volt <= calibration[i].voltage &&
  80. volt >= calibration[i+1].voltage) {
  81. /* interval found - interpolate within range */
  82. res = calibration[i].level -
  83. ((calibration[i].voltage - volt) *
  84. (calibration[i].level -
  85. calibration[i+1].level)) /
  86. (calibration[i].voltage -
  87. calibration[i+1].voltage);
  88. break;
  89. }
  90. }
  91. }
  92. return res;
  93. }
  94. static int twl4030_madc_bat_get_property(struct power_supply *psy,
  95. enum power_supply_property psp,
  96. union power_supply_propval *val)
  97. {
  98. struct twl4030_madc_battery *bat = power_supply_get_drvdata(psy);
  99. switch (psp) {
  100. case POWER_SUPPLY_PROP_STATUS:
  101. if (twl4030_madc_bat_voltscale(bat,
  102. twl4030_madc_bat_get_voltage(bat)) > 95)
  103. val->intval = POWER_SUPPLY_STATUS_FULL;
  104. else {
  105. if (twl4030_madc_bat_get_charging_status(bat))
  106. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  107. else
  108. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  109. }
  110. break;
  111. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  112. val->intval = twl4030_madc_bat_get_voltage(bat) * 1000;
  113. break;
  114. case POWER_SUPPLY_PROP_TECHNOLOGY:
  115. val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
  116. break;
  117. case POWER_SUPPLY_PROP_CURRENT_NOW:
  118. val->intval = twl4030_madc_bat_get_current(bat);
  119. break;
  120. case POWER_SUPPLY_PROP_PRESENT:
  121. /* assume battery is always present */
  122. val->intval = 1;
  123. break;
  124. case POWER_SUPPLY_PROP_CHARGE_NOW: {
  125. int percent = twl4030_madc_bat_voltscale(bat,
  126. twl4030_madc_bat_get_voltage(bat));
  127. val->intval = (percent * bat->pdata->capacity) / 100;
  128. break;
  129. }
  130. case POWER_SUPPLY_PROP_CAPACITY:
  131. val->intval = twl4030_madc_bat_voltscale(bat,
  132. twl4030_madc_bat_get_voltage(bat));
  133. break;
  134. case POWER_SUPPLY_PROP_CHARGE_FULL:
  135. val->intval = bat->pdata->capacity;
  136. break;
  137. case POWER_SUPPLY_PROP_TEMP:
  138. val->intval = twl4030_madc_bat_get_temp(bat);
  139. break;
  140. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW: {
  141. int percent = twl4030_madc_bat_voltscale(bat,
  142. twl4030_madc_bat_get_voltage(bat));
  143. /* in mAh */
  144. int chg = (percent * (bat->pdata->capacity/1000))/100;
  145. /* assume discharge with 400 mA (ca. 1.5W) */
  146. val->intval = (3600l * chg) / 400;
  147. break;
  148. }
  149. default:
  150. return -EINVAL;
  151. }
  152. return 0;
  153. }
  154. static void twl4030_madc_bat_ext_changed(struct power_supply *psy)
  155. {
  156. power_supply_changed(psy);
  157. }
  158. static const struct power_supply_desc twl4030_madc_bat_desc = {
  159. .name = "twl4030_battery",
  160. .type = POWER_SUPPLY_TYPE_BATTERY,
  161. .properties = twl4030_madc_bat_props,
  162. .num_properties = ARRAY_SIZE(twl4030_madc_bat_props),
  163. .get_property = twl4030_madc_bat_get_property,
  164. .external_power_changed = twl4030_madc_bat_ext_changed,
  165. };
  166. static int twl4030_cmp(const void *a, const void *b)
  167. {
  168. return ((struct twl4030_madc_bat_calibration *)b)->voltage -
  169. ((struct twl4030_madc_bat_calibration *)a)->voltage;
  170. }
  171. static int twl4030_madc_battery_probe(struct platform_device *pdev)
  172. {
  173. struct twl4030_madc_battery *twl4030_madc_bat;
  174. struct twl4030_madc_bat_platform_data *pdata = pdev->dev.platform_data;
  175. struct power_supply_config psy_cfg = {};
  176. int ret = 0;
  177. twl4030_madc_bat = devm_kzalloc(&pdev->dev, sizeof(*twl4030_madc_bat),
  178. GFP_KERNEL);
  179. if (!twl4030_madc_bat)
  180. return -ENOMEM;
  181. twl4030_madc_bat->channel_temp = iio_channel_get(&pdev->dev, "temp");
  182. if (IS_ERR(twl4030_madc_bat->channel_temp)) {
  183. ret = PTR_ERR(twl4030_madc_bat->channel_temp);
  184. goto err;
  185. }
  186. twl4030_madc_bat->channel_ichg = iio_channel_get(&pdev->dev, "ichg");
  187. if (IS_ERR(twl4030_madc_bat->channel_ichg)) {
  188. ret = PTR_ERR(twl4030_madc_bat->channel_ichg);
  189. goto err_temp;
  190. }
  191. twl4030_madc_bat->channel_vbat = iio_channel_get(&pdev->dev, "vbat");
  192. if (IS_ERR(twl4030_madc_bat->channel_vbat)) {
  193. ret = PTR_ERR(twl4030_madc_bat->channel_vbat);
  194. goto err_ichg;
  195. }
  196. /* sort charging and discharging calibration data */
  197. sort(pdata->charging, pdata->charging_size,
  198. sizeof(struct twl4030_madc_bat_calibration),
  199. twl4030_cmp, NULL);
  200. sort(pdata->discharging, pdata->discharging_size,
  201. sizeof(struct twl4030_madc_bat_calibration),
  202. twl4030_cmp, NULL);
  203. twl4030_madc_bat->pdata = pdata;
  204. platform_set_drvdata(pdev, twl4030_madc_bat);
  205. psy_cfg.drv_data = twl4030_madc_bat;
  206. twl4030_madc_bat->psy = power_supply_register(&pdev->dev,
  207. &twl4030_madc_bat_desc,
  208. &psy_cfg);
  209. if (IS_ERR(twl4030_madc_bat->psy)) {
  210. ret = PTR_ERR(twl4030_madc_bat->psy);
  211. goto err_vbat;
  212. }
  213. return 0;
  214. err_vbat:
  215. iio_channel_release(twl4030_madc_bat->channel_vbat);
  216. err_ichg:
  217. iio_channel_release(twl4030_madc_bat->channel_ichg);
  218. err_temp:
  219. iio_channel_release(twl4030_madc_bat->channel_temp);
  220. err:
  221. return ret;
  222. }
  223. static int twl4030_madc_battery_remove(struct platform_device *pdev)
  224. {
  225. struct twl4030_madc_battery *bat = platform_get_drvdata(pdev);
  226. power_supply_unregister(bat->psy);
  227. iio_channel_release(bat->channel_vbat);
  228. iio_channel_release(bat->channel_ichg);
  229. iio_channel_release(bat->channel_temp);
  230. return 0;
  231. }
  232. static struct platform_driver twl4030_madc_battery_driver = {
  233. .driver = {
  234. .name = "twl4030_madc_battery",
  235. },
  236. .probe = twl4030_madc_battery_probe,
  237. .remove = twl4030_madc_battery_remove,
  238. };
  239. module_platform_driver(twl4030_madc_battery_driver);
  240. MODULE_LICENSE("GPL");
  241. MODULE_AUTHOR("Lukas Märdian <lukas@goldelico.com>");
  242. MODULE_DESCRIPTION("twl4030_madc battery driver");
  243. MODULE_ALIAS("platform:twl4030_madc_battery");