rx51_battery.c 6.6 KB

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