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