rx51_battery.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Nokia RX-51 battery driver
  3. *
  4. * Copyright (C) 2012 Pali Rohár <pali.rohar@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/param.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/power_supply.h>
  24. #include <linux/slab.h>
  25. #include <linux/i2c/twl4030-madc.h>
  26. #include <linux/iio/consumer.h>
  27. #include <linux/of.h>
  28. struct rx51_device_info {
  29. struct device *dev;
  30. struct power_supply *bat;
  31. struct power_supply_desc bat_desc;
  32. struct iio_channel *channel_temp;
  33. struct iio_channel *channel_bsi;
  34. struct iio_channel *channel_vbat;
  35. };
  36. /*
  37. * Read ADCIN channel value, code copied from maemo kernel
  38. */
  39. static int rx51_battery_read_adc(struct iio_channel *channel)
  40. {
  41. int val, err;
  42. err = iio_read_channel_average_raw(channel, &val);
  43. if (err < 0)
  44. return err;
  45. return val;
  46. }
  47. /*
  48. * Read ADCIN channel 12 (voltage) and convert RAW value to micro voltage
  49. * This conversion formula was extracted from maemo program bsi-read
  50. */
  51. static int rx51_battery_read_voltage(struct rx51_device_info *di)
  52. {
  53. int voltage = rx51_battery_read_adc(di->channel_vbat);
  54. if (voltage < 0) {
  55. dev_err(di->dev, "Could not read ADC: %d\n", voltage);
  56. return voltage;
  57. }
  58. return 1000 * (10000 * voltage / 1705);
  59. }
  60. /*
  61. * Temperature look-up tables
  62. * TEMP = (1/(t1 + 1/298) - 273.15)
  63. * Where t1 = (1/B) * ln((RAW_ADC_U * 2.5)/(R * I * 255))
  64. * Formula is based on experimental data, RX-51 CAL data, maemo program bme
  65. * and formula from da9052 driver with values R = 100, B = 3380, I = 0.00671
  66. */
  67. /*
  68. * Table1 (temperature for first 25 RAW values)
  69. * Usage: TEMP = rx51_temp_table1[RAW]
  70. * RAW is between 1 and 24
  71. * TEMP is between 201 C and 55 C
  72. */
  73. static u8 rx51_temp_table1[] = {
  74. 255, 201, 159, 138, 124, 114, 106, 99, 94, 89, 85, 82, 78, 75,
  75. 73, 70, 68, 66, 64, 62, 61, 59, 57, 56, 55
  76. };
  77. /*
  78. * Table2 (lowest RAW value for temperature)
  79. * Usage: RAW = rx51_temp_table2[TEMP-rx51_temp_table2_first]
  80. * TEMP is between 53 C and -32 C
  81. * RAW is between 25 and 993
  82. */
  83. #define rx51_temp_table2_first 53
  84. static u16 rx51_temp_table2[] = {
  85. 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 39,
  86. 40, 41, 43, 44, 46, 48, 49, 51, 53, 55, 57, 59, 61, 64,
  87. 66, 69, 71, 74, 77, 80, 83, 86, 90, 94, 97, 101, 106, 110,
  88. 115, 119, 125, 130, 136, 141, 148, 154, 161, 168, 176, 184, 202, 211,
  89. 221, 231, 242, 254, 266, 279, 293, 308, 323, 340, 357, 375, 395, 415,
  90. 437, 460, 485, 511, 539, 568, 600, 633, 669, 706, 747, 790, 836, 885,
  91. 937, 993, 1024
  92. };
  93. /*
  94. * Read ADCIN channel 0 (battery temp) and convert value to tenths of Celsius
  95. * Use Temperature look-up tables for conversation
  96. */
  97. static int rx51_battery_read_temperature(struct rx51_device_info *di)
  98. {
  99. int min = 0;
  100. int max = ARRAY_SIZE(rx51_temp_table2) - 1;
  101. int raw = rx51_battery_read_adc(di->channel_temp);
  102. if (raw < 0)
  103. dev_err(di->dev, "Could not read ADC: %d\n", raw);
  104. /* Zero and negative values are undefined */
  105. if (raw <= 0)
  106. return INT_MAX;
  107. /* ADC channels are 10 bit, higher value are undefined */
  108. if (raw >= (1 << 10))
  109. return INT_MIN;
  110. /* First check for temperature in first direct table */
  111. if (raw < ARRAY_SIZE(rx51_temp_table1))
  112. return rx51_temp_table1[raw] * 10;
  113. /* Binary search RAW value in second inverse table */
  114. while (max - min > 1) {
  115. int mid = (max + min) / 2;
  116. if (rx51_temp_table2[mid] <= raw)
  117. min = mid;
  118. else if (rx51_temp_table2[mid] > raw)
  119. max = mid;
  120. if (rx51_temp_table2[mid] == raw)
  121. break;
  122. }
  123. return (rx51_temp_table2_first - min) * 10;
  124. }
  125. /*
  126. * Read ADCIN channel 4 (BSI) and convert RAW value to micro Ah
  127. * This conversion formula was extracted from maemo program bsi-read
  128. */
  129. static int rx51_battery_read_capacity(struct rx51_device_info *di)
  130. {
  131. int capacity = rx51_battery_read_adc(di->channel_bsi);
  132. if (capacity < 0) {
  133. dev_err(di->dev, "Could not read ADC: %d\n", capacity);
  134. return capacity;
  135. }
  136. return 1280 * (1200 * capacity)/(1024 - capacity);
  137. }
  138. /*
  139. * Return power_supply property
  140. */
  141. static int rx51_battery_get_property(struct power_supply *psy,
  142. enum power_supply_property psp,
  143. union power_supply_propval *val)
  144. {
  145. struct rx51_device_info *di = power_supply_get_drvdata(psy);
  146. switch (psp) {
  147. case POWER_SUPPLY_PROP_TECHNOLOGY:
  148. val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
  149. break;
  150. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  151. val->intval = 4200000;
  152. break;
  153. case POWER_SUPPLY_PROP_PRESENT:
  154. val->intval = rx51_battery_read_voltage(di) ? 1 : 0;
  155. break;
  156. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  157. val->intval = rx51_battery_read_voltage(di);
  158. break;
  159. case POWER_SUPPLY_PROP_TEMP:
  160. val->intval = rx51_battery_read_temperature(di);
  161. break;
  162. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  163. val->intval = rx51_battery_read_capacity(di);
  164. break;
  165. default:
  166. return -EINVAL;
  167. }
  168. if (val->intval == INT_MAX || val->intval == INT_MIN)
  169. return -EINVAL;
  170. return 0;
  171. }
  172. static enum power_supply_property rx51_battery_props[] = {
  173. POWER_SUPPLY_PROP_TECHNOLOGY,
  174. POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
  175. POWER_SUPPLY_PROP_PRESENT,
  176. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  177. POWER_SUPPLY_PROP_TEMP,
  178. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  179. };
  180. static int rx51_battery_probe(struct platform_device *pdev)
  181. {
  182. struct power_supply_config psy_cfg = {};
  183. struct rx51_device_info *di;
  184. int ret;
  185. di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
  186. if (!di)
  187. return -ENOMEM;
  188. platform_set_drvdata(pdev, di);
  189. di->dev = &pdev->dev;
  190. di->bat_desc.name = "rx51-battery";
  191. di->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY;
  192. di->bat_desc.properties = rx51_battery_props;
  193. di->bat_desc.num_properties = ARRAY_SIZE(rx51_battery_props);
  194. di->bat_desc.get_property = rx51_battery_get_property;
  195. psy_cfg.drv_data = di;
  196. di->channel_temp = iio_channel_get(di->dev, "temp");
  197. if (IS_ERR(di->channel_temp)) {
  198. ret = PTR_ERR(di->channel_temp);
  199. goto error;
  200. }
  201. di->channel_bsi = iio_channel_get(di->dev, "bsi");
  202. if (IS_ERR(di->channel_bsi)) {
  203. ret = PTR_ERR(di->channel_bsi);
  204. goto error_channel_temp;
  205. }
  206. di->channel_vbat = iio_channel_get(di->dev, "vbat");
  207. if (IS_ERR(di->channel_vbat)) {
  208. ret = PTR_ERR(di->channel_vbat);
  209. goto error_channel_bsi;
  210. }
  211. di->bat = power_supply_register(di->dev, &di->bat_desc, &psy_cfg);
  212. if (IS_ERR(di->bat)) {
  213. ret = PTR_ERR(di->bat);
  214. goto error_channel_vbat;
  215. }
  216. return 0;
  217. error_channel_vbat:
  218. iio_channel_release(di->channel_vbat);
  219. error_channel_bsi:
  220. iio_channel_release(di->channel_bsi);
  221. error_channel_temp:
  222. iio_channel_release(di->channel_temp);
  223. error:
  224. return ret;
  225. }
  226. static int rx51_battery_remove(struct platform_device *pdev)
  227. {
  228. struct rx51_device_info *di = platform_get_drvdata(pdev);
  229. power_supply_unregister(di->bat);
  230. iio_channel_release(di->channel_vbat);
  231. iio_channel_release(di->channel_bsi);
  232. iio_channel_release(di->channel_temp);
  233. return 0;
  234. }
  235. #ifdef CONFIG_OF
  236. static const struct of_device_id n900_battery_of_match[] = {
  237. {.compatible = "nokia,n900-battery", },
  238. { },
  239. };
  240. MODULE_DEVICE_TABLE(of, n900_battery_of_match);
  241. #endif
  242. static struct platform_driver rx51_battery_driver = {
  243. .probe = rx51_battery_probe,
  244. .remove = rx51_battery_remove,
  245. .driver = {
  246. .name = "rx51-battery",
  247. .of_match_table = of_match_ptr(n900_battery_of_match),
  248. },
  249. };
  250. module_platform_driver(rx51_battery_driver);
  251. MODULE_ALIAS("platform:rx51-battery");
  252. MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
  253. MODULE_DESCRIPTION("Nokia RX-51 battery driver");
  254. MODULE_LICENSE("GPL");