olpc_battery.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. /*********************************************************************
  75. * Battery properties
  76. *********************************************************************/
  77. static int olpc_bat_get_property(struct power_supply *psy,
  78. enum power_supply_property psp,
  79. union power_supply_propval *val)
  80. {
  81. int ret = 0;
  82. int16_t ec_word;
  83. uint8_t ec_byte;
  84. ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &ec_byte, 1);
  85. if (ret)
  86. return ret;
  87. /* Theoretically there's a race here -- the battery could be
  88. removed immediately after we check whether it's present, and
  89. then we query for some other property of the now-absent battery.
  90. It doesn't matter though -- the EC will return the last-known
  91. information, and it's as if we just ran that _little_ bit faster
  92. and managed to read it out before the battery went away. */
  93. if (!(ec_byte & BAT_STAT_PRESENT) && psp != POWER_SUPPLY_PROP_PRESENT)
  94. return -ENODEV;
  95. switch (psp) {
  96. case POWER_SUPPLY_PROP_STATUS:
  97. if (olpc_platform_info.ecver > 0x44) {
  98. if (ec_byte & BAT_STAT_CHARGING)
  99. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  100. else if (ec_byte & BAT_STAT_DISCHARGING)
  101. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  102. else if (ec_byte & BAT_STAT_FULL)
  103. val->intval = POWER_SUPPLY_STATUS_FULL;
  104. else /* er,... */
  105. val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
  106. } else {
  107. /* Older EC didn't report charge/discharge bits */
  108. if (!(ec_byte & BAT_STAT_AC)) /* No AC means discharging */
  109. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  110. else if (ec_byte & BAT_STAT_FULL)
  111. val->intval = POWER_SUPPLY_STATUS_FULL;
  112. else /* Not _necessarily_ true but EC doesn't tell all yet */
  113. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  114. break;
  115. }
  116. case POWER_SUPPLY_PROP_PRESENT:
  117. val->intval = !!(ec_byte & BAT_STAT_PRESENT);
  118. break;
  119. case POWER_SUPPLY_PROP_HEALTH:
  120. if (ec_byte & BAT_STAT_DESTROY)
  121. val->intval = POWER_SUPPLY_HEALTH_DEAD;
  122. else {
  123. ret = olpc_ec_cmd(EC_BAT_ERRCODE, NULL, 0, &ec_byte, 1);
  124. if (ret)
  125. return ret;
  126. switch (ec_byte) {
  127. case 0:
  128. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  129. break;
  130. case BAT_ERR_OVERTEMP:
  131. val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
  132. break;
  133. case BAT_ERR_OVERVOLTAGE:
  134. val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  135. break;
  136. case BAT_ERR_INFOFAIL:
  137. case BAT_ERR_OUT_OF_CONTROL:
  138. case BAT_ERR_ID_FAIL:
  139. case BAT_ERR_ACR_FAIL:
  140. val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  141. break;
  142. default:
  143. /* Eep. We don't know this failure code */
  144. return -EIO;
  145. }
  146. }
  147. break;
  148. case POWER_SUPPLY_PROP_MANUFACTURER:
  149. ec_byte = BAT_ADDR_MFR_TYPE;
  150. ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1);
  151. if (ret)
  152. return ret;
  153. switch (ec_byte >> 4) {
  154. case 1:
  155. val->strval = "Gold Peak";
  156. break;
  157. case 2:
  158. val->strval = "BYD";
  159. break;
  160. default:
  161. val->strval = "Unknown";
  162. break;
  163. }
  164. break;
  165. case POWER_SUPPLY_PROP_TECHNOLOGY:
  166. ec_byte = BAT_ADDR_MFR_TYPE;
  167. ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1);
  168. if (ret)
  169. return ret;
  170. switch (ec_byte & 0xf) {
  171. case 1:
  172. val->intval = POWER_SUPPLY_TECHNOLOGY_NiMH;
  173. break;
  174. case 2:
  175. val->intval = POWER_SUPPLY_TECHNOLOGY_LiFe;
  176. break;
  177. default:
  178. val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
  179. break;
  180. }
  181. break;
  182. case POWER_SUPPLY_PROP_VOLTAGE_AVG:
  183. ret = olpc_ec_cmd(EC_BAT_VOLTAGE, NULL, 0, (void *)&ec_word, 2);
  184. if (ret)
  185. return ret;
  186. ec_word = be16_to_cpu(ec_word);
  187. val->intval = ec_word * 9760L / 32;
  188. break;
  189. case POWER_SUPPLY_PROP_CURRENT_AVG:
  190. ret = olpc_ec_cmd(EC_BAT_CURRENT, NULL, 0, (void *)&ec_word, 2);
  191. if (ret)
  192. return ret;
  193. ec_word = be16_to_cpu(ec_word);
  194. val->intval = ec_word * 15625L / 120;
  195. break;
  196. case POWER_SUPPLY_PROP_CAPACITY:
  197. ret = olpc_ec_cmd(EC_BAT_SOC, NULL, 0, &ec_byte, 1);
  198. if (ret)
  199. return ret;
  200. val->intval = ec_byte;
  201. break;
  202. case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
  203. if (ec_byte & BAT_STAT_FULL)
  204. val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
  205. else if (ec_byte & BAT_STAT_LOW)
  206. val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
  207. else
  208. val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
  209. break;
  210. case POWER_SUPPLY_PROP_TEMP:
  211. ret = olpc_ec_cmd(EC_BAT_TEMP, NULL, 0, (void *)&ec_word, 2);
  212. if (ret)
  213. return ret;
  214. ec_word = be16_to_cpu(ec_word);
  215. val->intval = ec_word * 100 / 256;
  216. break;
  217. case POWER_SUPPLY_PROP_TEMP_AMBIENT:
  218. ret = olpc_ec_cmd(EC_AMB_TEMP, NULL, 0, (void *)&ec_word, 2);
  219. if (ret)
  220. return ret;
  221. ec_word = be16_to_cpu(ec_word);
  222. val->intval = ec_word * 100 / 256;
  223. break;
  224. default:
  225. ret = -EINVAL;
  226. break;
  227. }
  228. return ret;
  229. }
  230. static enum power_supply_property olpc_bat_props[] = {
  231. POWER_SUPPLY_PROP_STATUS,
  232. POWER_SUPPLY_PROP_PRESENT,
  233. POWER_SUPPLY_PROP_HEALTH,
  234. POWER_SUPPLY_PROP_TECHNOLOGY,
  235. POWER_SUPPLY_PROP_VOLTAGE_AVG,
  236. POWER_SUPPLY_PROP_CURRENT_AVG,
  237. POWER_SUPPLY_PROP_CAPACITY,
  238. POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  239. POWER_SUPPLY_PROP_TEMP,
  240. POWER_SUPPLY_PROP_TEMP_AMBIENT,
  241. POWER_SUPPLY_PROP_MANUFACTURER,
  242. };
  243. /*********************************************************************
  244. * Initialisation
  245. *********************************************************************/
  246. static struct platform_device *bat_pdev;
  247. static struct power_supply olpc_bat = {
  248. .properties = olpc_bat_props,
  249. .num_properties = ARRAY_SIZE(olpc_bat_props),
  250. .get_property = olpc_bat_get_property,
  251. .use_for_apm = 1,
  252. };
  253. void olpc_battery_trigger_uevent(unsigned long cause)
  254. {
  255. if (cause & EC_SCI_SRC_ACPWR)
  256. kobject_uevent(&olpc_ac.dev->kobj, KOBJ_CHANGE);
  257. if (cause & (EC_SCI_SRC_BATERR|EC_SCI_SRC_BATSOC|EC_SCI_SRC_BATTERY))
  258. kobject_uevent(&olpc_bat.dev->kobj, KOBJ_CHANGE);
  259. }
  260. static int __init olpc_bat_init(void)
  261. {
  262. int ret = 0;
  263. uint8_t status;
  264. if (!olpc_platform_info.ecver)
  265. return -ENXIO;
  266. if (olpc_platform_info.ecver < 0x43) {
  267. printk(KERN_NOTICE "OLPC EC version 0x%02x too old for battery driver.\n", olpc_platform_info.ecver);
  268. return -ENXIO;
  269. }
  270. ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &status, 1);
  271. if (ret)
  272. return ret;
  273. /* Ignore the status. It doesn't actually matter */
  274. bat_pdev = platform_device_register_simple("olpc-battery", 0, NULL, 0);
  275. if (IS_ERR(bat_pdev))
  276. return PTR_ERR(bat_pdev);
  277. ret = power_supply_register(&bat_pdev->dev, &olpc_ac);
  278. if (ret)
  279. goto ac_failed;
  280. olpc_bat.name = bat_pdev->name;
  281. ret = power_supply_register(&bat_pdev->dev, &olpc_bat);
  282. if (ret)
  283. goto battery_failed;
  284. olpc_register_battery_callback(&olpc_battery_trigger_uevent);
  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. olpc_deregister_battery_callback();
  296. power_supply_unregister(&olpc_bat);
  297. power_supply_unregister(&olpc_ac);
  298. platform_device_unregister(bat_pdev);
  299. }
  300. module_init(olpc_bat_init);
  301. module_exit(olpc_bat_exit);
  302. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  303. MODULE_LICENSE("GPL");
  304. MODULE_DESCRIPTION("Battery driver for One Laptop Per Child 'XO' machine");