rx51_battery.c 6.7 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. /* RX51 specific channels */
  27. #define TWL4030_MADC_BTEMP_RX51 TWL4030_MADC_ADCIN0
  28. #define TWL4030_MADC_BCI_RX51 TWL4030_MADC_ADCIN4
  29. struct rx51_device_info {
  30. struct device *dev;
  31. struct power_supply bat;
  32. };
  33. /*
  34. * Read ADCIN channel value, code copied from maemo kernel
  35. */
  36. static int rx51_battery_read_adc(int channel)
  37. {
  38. struct twl4030_madc_request req;
  39. req.channels = channel;
  40. req.do_avg = 1;
  41. req.method = TWL4030_MADC_SW1;
  42. req.func_cb = NULL;
  43. req.type = TWL4030_MADC_WAIT;
  44. req.raw = true;
  45. if (twl4030_madc_conversion(&req) <= 0)
  46. return -ENODATA;
  47. return req.rbuf[ffs(channel) - 1];
  48. }
  49. /*
  50. * Read ADCIN channel 12 (voltage) and convert RAW value to micro voltage
  51. * This conversion formula was extracted from maemo program bsi-read
  52. */
  53. static int rx51_battery_read_voltage(struct rx51_device_info *di)
  54. {
  55. int voltage = rx51_battery_read_adc(TWL4030_MADC_VBAT);
  56. if (voltage < 0)
  57. return voltage;
  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(TWL4030_MADC_BTEMP_RX51);
  102. /* Zero and negative values are undefined */
  103. if (raw <= 0)
  104. return INT_MAX;
  105. /* ADC channels are 10 bit, higher value are undefined */
  106. if (raw >= (1 << 10))
  107. return INT_MIN;
  108. /* First check for temperature in first direct table */
  109. if (raw < ARRAY_SIZE(rx51_temp_table1))
  110. return rx51_temp_table1[raw] * 10;
  111. /* Binary search RAW value in second inverse table */
  112. while (max - min > 1) {
  113. int mid = (max + min) / 2;
  114. if (rx51_temp_table2[mid] <= raw)
  115. min = mid;
  116. else if (rx51_temp_table2[mid] > raw)
  117. max = mid;
  118. if (rx51_temp_table2[mid] == raw)
  119. break;
  120. }
  121. return (rx51_temp_table2_first - min) * 10;
  122. }
  123. /*
  124. * Read ADCIN channel 4 (BSI) and convert RAW value to micro Ah
  125. * This conversion formula was extracted from maemo program bsi-read
  126. */
  127. static int rx51_battery_read_capacity(struct rx51_device_info *di)
  128. {
  129. int capacity = rx51_battery_read_adc(TWL4030_MADC_BCI_RX51);
  130. if (capacity < 0)
  131. return capacity;
  132. return 1280 * (1200 * capacity)/(1024 - capacity);
  133. }
  134. /*
  135. * Return power_supply property
  136. */
  137. static int rx51_battery_get_property(struct power_supply *psy,
  138. enum power_supply_property psp,
  139. union power_supply_propval *val)
  140. {
  141. struct rx51_device_info *di = container_of((psy),
  142. struct rx51_device_info, bat);
  143. switch (psp) {
  144. case POWER_SUPPLY_PROP_TECHNOLOGY:
  145. val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
  146. break;
  147. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  148. val->intval = 4200000;
  149. break;
  150. case POWER_SUPPLY_PROP_PRESENT:
  151. val->intval = rx51_battery_read_voltage(di) ? 1 : 0;
  152. break;
  153. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  154. val->intval = rx51_battery_read_voltage(di);
  155. break;
  156. case POWER_SUPPLY_PROP_TEMP:
  157. val->intval = rx51_battery_read_temperature(di);
  158. break;
  159. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  160. val->intval = rx51_battery_read_capacity(di);
  161. break;
  162. default:
  163. return -EINVAL;
  164. }
  165. if (val->intval == INT_MAX || val->intval == INT_MIN)
  166. return -EINVAL;
  167. return 0;
  168. }
  169. static enum power_supply_property rx51_battery_props[] = {
  170. POWER_SUPPLY_PROP_TECHNOLOGY,
  171. POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
  172. POWER_SUPPLY_PROP_PRESENT,
  173. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  174. POWER_SUPPLY_PROP_TEMP,
  175. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  176. };
  177. static int rx51_battery_probe(struct platform_device *pdev)
  178. {
  179. struct rx51_device_info *di;
  180. int ret;
  181. di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
  182. if (!di)
  183. return -ENOMEM;
  184. platform_set_drvdata(pdev, di);
  185. di->bat.name = dev_name(&pdev->dev);
  186. di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
  187. di->bat.properties = rx51_battery_props;
  188. di->bat.num_properties = ARRAY_SIZE(rx51_battery_props);
  189. di->bat.get_property = rx51_battery_get_property;
  190. ret = power_supply_register(di->dev, &di->bat);
  191. if (ret)
  192. return ret;
  193. return 0;
  194. }
  195. static int rx51_battery_remove(struct platform_device *pdev)
  196. {
  197. struct rx51_device_info *di = platform_get_drvdata(pdev);
  198. power_supply_unregister(&di->bat);
  199. return 0;
  200. }
  201. static struct platform_driver rx51_battery_driver = {
  202. .probe = rx51_battery_probe,
  203. .remove = rx51_battery_remove,
  204. .driver = {
  205. .name = "rx51-battery",
  206. .owner = THIS_MODULE,
  207. },
  208. };
  209. module_platform_driver(rx51_battery_driver);
  210. MODULE_ALIAS("platform:rx51-battery");
  211. MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
  212. MODULE_DESCRIPTION("Nokia RX-51 battery driver");
  213. MODULE_LICENSE("GPL");