olpc_battery.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Battery driver for One Laptop Per Child board.
  3. *
  4. * Copyright © 2006 David Woodhouse <dwmw2@infradead.org>
  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 version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/err.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/power_supply.h>
  14. #include <linux/jiffies.h>
  15. #include <linux/sched.h>
  16. #include <asm/olpc.h>
  17. #define EC_BAT_VOLTAGE 0x10 /* uint16_t, *9.76/32, mV */
  18. #define EC_BAT_CURRENT 0x11 /* int16_t, *15.625/120, mA */
  19. #define EC_BAT_ACR 0x12
  20. #define EC_BAT_TEMP 0x13 /* uint16_t, *100/256, °C */
  21. #define EC_AMB_TEMP 0x14 /* uint16_t, *100/256, °C */
  22. #define EC_BAT_STATUS 0x15 /* uint8_t, bitmask */
  23. #define EC_BAT_SOC 0x16 /* uint8_t, percentage */
  24. #define EC_BAT_SERIAL 0x17 /* uint8_t[6] */
  25. #define EC_BAT_EEPROM 0x18 /* uint8_t adr as input, uint8_t output */
  26. #define EC_BAT_ERRCODE 0x1f /* uint8_t, bitmask */
  27. #define BAT_STAT_PRESENT 0x01
  28. #define BAT_STAT_FULL 0x02
  29. #define BAT_STAT_LOW 0x04
  30. #define BAT_STAT_DESTROY 0x08
  31. #define BAT_STAT_AC 0x10
  32. #define BAT_STAT_CHARGING 0x20
  33. #define BAT_STAT_DISCHARGING 0x40
  34. #define BAT_ERR_INFOFAIL 0x02
  35. #define BAT_ERR_OVERVOLTAGE 0x04
  36. #define BAT_ERR_OVERTEMP 0x05
  37. #define BAT_ERR_GAUGESTOP 0x06
  38. #define BAT_ERR_OUT_OF_CONTROL 0x07
  39. #define BAT_ERR_ID_FAIL 0x09
  40. #define BAT_ERR_ACR_FAIL 0x10
  41. #define BAT_ADDR_MFR_TYPE 0x5F
  42. /*********************************************************************
  43. * Power
  44. *********************************************************************/
  45. static int olpc_ac_get_prop(struct power_supply *psy,
  46. enum power_supply_property psp,
  47. union power_supply_propval *val)
  48. {
  49. int ret = 0;
  50. uint8_t status;
  51. switch (psp) {
  52. case POWER_SUPPLY_PROP_ONLINE:
  53. ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &status, 1);
  54. if (ret)
  55. return ret;
  56. val->intval = !!(status & BAT_STAT_AC);
  57. break;
  58. default:
  59. ret = -EINVAL;
  60. break;
  61. }
  62. return ret;
  63. }
  64. static enum power_supply_property olpc_ac_props[] = {
  65. POWER_SUPPLY_PROP_ONLINE,
  66. };
  67. static struct power_supply olpc_ac = {
  68. .name = "olpc-ac",
  69. .type = POWER_SUPPLY_TYPE_MAINS,
  70. .properties = olpc_ac_props,
  71. .num_properties = ARRAY_SIZE(olpc_ac_props),
  72. .get_property = olpc_ac_get_prop,
  73. };
  74. static char bat_serial[17]; /* Ick */
  75. /*********************************************************************
  76. * Battery properties
  77. *********************************************************************/
  78. static int olpc_bat_get_property(struct power_supply *psy,
  79. enum power_supply_property psp,
  80. union power_supply_propval *val)
  81. {
  82. int ret = 0;
  83. int16_t ec_word;
  84. uint8_t ec_byte;
  85. uint64_t ser_buf;
  86. ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &ec_byte, 1);
  87. if (ret)
  88. return ret;
  89. /* Theoretically there's a race here -- the battery could be
  90. removed immediately after we check whether it's present, and
  91. then we query for some other property of the now-absent battery.
  92. It doesn't matter though -- the EC will return the last-known
  93. information, and it's as if we just ran that _little_ bit faster
  94. and managed to read it out before the battery went away. */
  95. if (!(ec_byte & BAT_STAT_PRESENT) && psp != POWER_SUPPLY_PROP_PRESENT)
  96. return -ENODEV;
  97. switch (psp) {
  98. case POWER_SUPPLY_PROP_STATUS:
  99. if (olpc_platform_info.ecver > 0x44) {
  100. if (ec_byte & BAT_STAT_CHARGING)
  101. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  102. else if (ec_byte & BAT_STAT_DISCHARGING)
  103. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  104. else if (ec_byte & BAT_STAT_FULL)
  105. val->intval = POWER_SUPPLY_STATUS_FULL;
  106. else /* er,... */
  107. val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
  108. } else {
  109. /* Older EC didn't report charge/discharge bits */
  110. if (!(ec_byte & BAT_STAT_AC)) /* No AC means discharging */
  111. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  112. else if (ec_byte & BAT_STAT_FULL)
  113. val->intval = POWER_SUPPLY_STATUS_FULL;
  114. else /* Not _necessarily_ true but EC doesn't tell all yet */
  115. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  116. break;
  117. }
  118. case POWER_SUPPLY_PROP_PRESENT:
  119. val->intval = !!(ec_byte & BAT_STAT_PRESENT);
  120. break;
  121. case POWER_SUPPLY_PROP_HEALTH:
  122. if (ec_byte & BAT_STAT_DESTROY)
  123. val->intval = POWER_SUPPLY_HEALTH_DEAD;
  124. else {
  125. ret = olpc_ec_cmd(EC_BAT_ERRCODE, NULL, 0, &ec_byte, 1);
  126. if (ret)
  127. return ret;
  128. switch (ec_byte) {
  129. case 0:
  130. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  131. break;
  132. case BAT_ERR_OVERTEMP:
  133. val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
  134. break;
  135. case BAT_ERR_OVERVOLTAGE:
  136. val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  137. break;
  138. case BAT_ERR_INFOFAIL:
  139. case BAT_ERR_OUT_OF_CONTROL:
  140. case BAT_ERR_ID_FAIL:
  141. case BAT_ERR_ACR_FAIL:
  142. val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  143. break;
  144. default:
  145. /* Eep. We don't know this failure code */
  146. return -EIO;
  147. }
  148. }
  149. break;
  150. case POWER_SUPPLY_PROP_MANUFACTURER:
  151. ec_byte = BAT_ADDR_MFR_TYPE;
  152. ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1);
  153. if (ret)
  154. return ret;
  155. switch (ec_byte >> 4) {
  156. case 1:
  157. val->strval = "Gold Peak";
  158. break;
  159. case 2:
  160. val->strval = "BYD";
  161. break;
  162. default:
  163. val->strval = "Unknown";
  164. break;
  165. }
  166. break;
  167. case POWER_SUPPLY_PROP_TECHNOLOGY:
  168. ec_byte = BAT_ADDR_MFR_TYPE;
  169. ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1);
  170. if (ret)
  171. return ret;
  172. switch (ec_byte & 0xf) {
  173. case 1:
  174. val->intval = POWER_SUPPLY_TECHNOLOGY_NiMH;
  175. break;
  176. case 2:
  177. val->intval = POWER_SUPPLY_TECHNOLOGY_LiFe;
  178. break;
  179. default:
  180. val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
  181. break;
  182. }
  183. break;
  184. case POWER_SUPPLY_PROP_VOLTAGE_AVG:
  185. ret = olpc_ec_cmd(EC_BAT_VOLTAGE, NULL, 0, (void *)&ec_word, 2);
  186. if (ret)
  187. return ret;
  188. ec_word = be16_to_cpu(ec_word);
  189. val->intval = ec_word * 9760L / 32;
  190. break;
  191. case POWER_SUPPLY_PROP_CURRENT_AVG:
  192. ret = olpc_ec_cmd(EC_BAT_CURRENT, NULL, 0, (void *)&ec_word, 2);
  193. if (ret)
  194. return ret;
  195. ec_word = be16_to_cpu(ec_word);
  196. val->intval = ec_word * 15625L / 120;
  197. break;
  198. case POWER_SUPPLY_PROP_CAPACITY:
  199. ret = olpc_ec_cmd(EC_BAT_SOC, NULL, 0, &ec_byte, 1);
  200. if (ret)
  201. return ret;
  202. val->intval = ec_byte;
  203. break;
  204. case POWER_SUPPLY_PROP_TEMP:
  205. ret = olpc_ec_cmd(EC_BAT_TEMP, NULL, 0, (void *)&ec_word, 2);
  206. if (ret)
  207. return ret;
  208. ec_word = be16_to_cpu(ec_word);
  209. val->intval = ec_word * 100 / 256;
  210. break;
  211. case POWER_SUPPLY_PROP_TEMP_AMBIENT:
  212. ret = olpc_ec_cmd(EC_AMB_TEMP, NULL, 0, (void *)&ec_word, 2);
  213. if (ret)
  214. return ret;
  215. ec_word = be16_to_cpu(ec_word);
  216. val->intval = ec_word * 100 / 256;
  217. break;
  218. case POWER_SUPPLY_PROP_SERIAL_NUMBER:
  219. ret = olpc_ec_cmd(EC_BAT_SERIAL, NULL, 0, (void *)&ser_buf, 8);
  220. if (ret)
  221. return ret;
  222. sprintf(bat_serial, "%016llx", (long long)be64_to_cpu(ser_buf));
  223. val->strval = bat_serial;
  224. break;
  225. default:
  226. ret = -EINVAL;
  227. break;
  228. }
  229. return ret;
  230. }
  231. static enum power_supply_property olpc_bat_props[] = {
  232. POWER_SUPPLY_PROP_STATUS,
  233. POWER_SUPPLY_PROP_PRESENT,
  234. POWER_SUPPLY_PROP_HEALTH,
  235. POWER_SUPPLY_PROP_TECHNOLOGY,
  236. POWER_SUPPLY_PROP_VOLTAGE_AVG,
  237. POWER_SUPPLY_PROP_CURRENT_AVG,
  238. POWER_SUPPLY_PROP_CAPACITY,
  239. POWER_SUPPLY_PROP_TEMP,
  240. POWER_SUPPLY_PROP_TEMP_AMBIENT,
  241. POWER_SUPPLY_PROP_MANUFACTURER,
  242. POWER_SUPPLY_PROP_SERIAL_NUMBER,
  243. };
  244. /*********************************************************************
  245. * Initialisation
  246. *********************************************************************/
  247. static struct platform_device *bat_pdev;
  248. static struct power_supply olpc_bat = {
  249. .properties = olpc_bat_props,
  250. .num_properties = ARRAY_SIZE(olpc_bat_props),
  251. .get_property = olpc_bat_get_property,
  252. .use_for_apm = 1,
  253. };
  254. void olpc_battery_trigger_uevent(unsigned long cause)
  255. {
  256. if (cause & EC_SCI_SRC_ACPWR)
  257. kobject_uevent(&olpc_ac.dev->kobj, KOBJ_CHANGE);
  258. if (cause & (EC_SCI_SRC_BATERR|EC_SCI_SRC_BATSOC|EC_SCI_SRC_BATTERY))
  259. kobject_uevent(&olpc_bat.dev->kobj, KOBJ_CHANGE);
  260. }
  261. static int __init olpc_bat_init(void)
  262. {
  263. int ret = 0;
  264. uint8_t status;
  265. if (!olpc_platform_info.ecver)
  266. return -ENXIO;
  267. if (olpc_platform_info.ecver < 0x43) {
  268. printk(KERN_NOTICE "OLPC EC version 0x%02x too old for battery driver.\n", olpc_platform_info.ecver);
  269. return -ENXIO;
  270. }
  271. ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &status, 1);
  272. if (ret)
  273. return ret;
  274. /* Ignore the status. It doesn't actually matter */
  275. bat_pdev = platform_device_register_simple("olpc-battery", 0, NULL, 0);
  276. if (IS_ERR(bat_pdev))
  277. return PTR_ERR(bat_pdev);
  278. ret = power_supply_register(&bat_pdev->dev, &olpc_ac);
  279. if (ret)
  280. goto ac_failed;
  281. olpc_bat.name = bat_pdev->name;
  282. ret = power_supply_register(&bat_pdev->dev, &olpc_bat);
  283. if (ret)
  284. goto battery_failed;
  285. goto success;
  286. battery_failed:
  287. power_supply_unregister(&olpc_ac);
  288. ac_failed:
  289. platform_device_unregister(bat_pdev);
  290. success:
  291. return ret;
  292. }
  293. static void __exit olpc_bat_exit(void)
  294. {
  295. power_supply_unregister(&olpc_bat);
  296. power_supply_unregister(&olpc_ac);
  297. platform_device_unregister(bat_pdev);
  298. }
  299. module_init(olpc_bat_init);
  300. module_exit(olpc_bat_exit);
  301. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  302. MODULE_LICENSE("GPL");
  303. MODULE_DESCRIPTION("Battery driver for One Laptop Per Child 'XO' machine");