olpc_battery.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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 /* int16_t, *6250/15, µAh */
  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. static int olpc_bat_get_status(union power_supply_propval *val, uint8_t ec_byte)
  76. {
  77. if (olpc_platform_info.ecver > 0x44) {
  78. if (ec_byte & BAT_STAT_CHARGING)
  79. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  80. else if (ec_byte & BAT_STAT_DISCHARGING)
  81. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  82. else if (ec_byte & BAT_STAT_FULL)
  83. val->intval = POWER_SUPPLY_STATUS_FULL;
  84. else /* er,... */
  85. val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
  86. } else {
  87. /* Older EC didn't report charge/discharge bits */
  88. if (!(ec_byte & BAT_STAT_AC)) /* No AC means discharging */
  89. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  90. else if (ec_byte & BAT_STAT_FULL)
  91. val->intval = POWER_SUPPLY_STATUS_FULL;
  92. else /* Not _necessarily_ true but EC doesn't tell all yet */
  93. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  94. }
  95. return 0;
  96. }
  97. static int olpc_bat_get_health(union power_supply_propval *val)
  98. {
  99. uint8_t ec_byte;
  100. int ret;
  101. ret = olpc_ec_cmd(EC_BAT_ERRCODE, NULL, 0, &ec_byte, 1);
  102. if (ret)
  103. return ret;
  104. switch (ec_byte) {
  105. case 0:
  106. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  107. break;
  108. case BAT_ERR_OVERTEMP:
  109. val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
  110. break;
  111. case BAT_ERR_OVERVOLTAGE:
  112. val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  113. break;
  114. case BAT_ERR_INFOFAIL:
  115. case BAT_ERR_OUT_OF_CONTROL:
  116. case BAT_ERR_ID_FAIL:
  117. case BAT_ERR_ACR_FAIL:
  118. val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  119. break;
  120. default:
  121. /* Eep. We don't know this failure code */
  122. ret = -EIO;
  123. }
  124. return ret;
  125. }
  126. static int olpc_bat_get_mfr(union power_supply_propval *val)
  127. {
  128. uint8_t ec_byte;
  129. int ret;
  130. ec_byte = BAT_ADDR_MFR_TYPE;
  131. ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1);
  132. if (ret)
  133. return ret;
  134. switch (ec_byte >> 4) {
  135. case 1:
  136. val->strval = "Gold Peak";
  137. break;
  138. case 2:
  139. val->strval = "BYD";
  140. break;
  141. default:
  142. val->strval = "Unknown";
  143. break;
  144. }
  145. return ret;
  146. }
  147. static int olpc_bat_get_tech(union power_supply_propval *val)
  148. {
  149. uint8_t ec_byte;
  150. int ret;
  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 & 0xf) {
  156. case 1:
  157. val->intval = POWER_SUPPLY_TECHNOLOGY_NiMH;
  158. break;
  159. case 2:
  160. val->intval = POWER_SUPPLY_TECHNOLOGY_LiFe;
  161. break;
  162. default:
  163. val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
  164. break;
  165. }
  166. return ret;
  167. }
  168. /*********************************************************************
  169. * Battery properties
  170. *********************************************************************/
  171. static int olpc_bat_get_property(struct power_supply *psy,
  172. enum power_supply_property psp,
  173. union power_supply_propval *val)
  174. {
  175. int ret = 0;
  176. int16_t ec_word;
  177. uint8_t ec_byte;
  178. uint64_t ser_buf;
  179. ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &ec_byte, 1);
  180. if (ret)
  181. return ret;
  182. /* Theoretically there's a race here -- the battery could be
  183. removed immediately after we check whether it's present, and
  184. then we query for some other property of the now-absent battery.
  185. It doesn't matter though -- the EC will return the last-known
  186. information, and it's as if we just ran that _little_ bit faster
  187. and managed to read it out before the battery went away. */
  188. if (!(ec_byte & BAT_STAT_PRESENT) && psp != POWER_SUPPLY_PROP_PRESENT)
  189. return -ENODEV;
  190. switch (psp) {
  191. case POWER_SUPPLY_PROP_STATUS:
  192. ret = olpc_bat_get_status(val, ec_byte);
  193. if (ret)
  194. return ret;
  195. break;
  196. case POWER_SUPPLY_PROP_PRESENT:
  197. val->intval = !!(ec_byte & BAT_STAT_PRESENT);
  198. break;
  199. case POWER_SUPPLY_PROP_HEALTH:
  200. if (ec_byte & BAT_STAT_DESTROY)
  201. val->intval = POWER_SUPPLY_HEALTH_DEAD;
  202. else {
  203. ret = olpc_bat_get_health(val);
  204. if (ret)
  205. return ret;
  206. }
  207. break;
  208. case POWER_SUPPLY_PROP_MANUFACTURER:
  209. ret = olpc_bat_get_mfr(val);
  210. if (ret)
  211. return ret;
  212. break;
  213. case POWER_SUPPLY_PROP_TECHNOLOGY:
  214. ret = olpc_bat_get_tech(val);
  215. if (ret)
  216. return ret;
  217. break;
  218. case POWER_SUPPLY_PROP_VOLTAGE_AVG:
  219. ret = olpc_ec_cmd(EC_BAT_VOLTAGE, NULL, 0, (void *)&ec_word, 2);
  220. if (ret)
  221. return ret;
  222. ec_word = be16_to_cpu(ec_word);
  223. val->intval = ec_word * 9760L / 32;
  224. break;
  225. case POWER_SUPPLY_PROP_CURRENT_AVG:
  226. ret = olpc_ec_cmd(EC_BAT_CURRENT, NULL, 0, (void *)&ec_word, 2);
  227. if (ret)
  228. return ret;
  229. ec_word = be16_to_cpu(ec_word);
  230. val->intval = ec_word * 15625L / 120;
  231. break;
  232. case POWER_SUPPLY_PROP_CAPACITY:
  233. ret = olpc_ec_cmd(EC_BAT_SOC, NULL, 0, &ec_byte, 1);
  234. if (ret)
  235. return ret;
  236. val->intval = ec_byte;
  237. break;
  238. case POWER_SUPPLY_PROP_TEMP:
  239. ret = olpc_ec_cmd(EC_BAT_TEMP, NULL, 0, (void *)&ec_word, 2);
  240. if (ret)
  241. return ret;
  242. ec_word = be16_to_cpu(ec_word);
  243. val->intval = ec_word * 100 / 256;
  244. break;
  245. case POWER_SUPPLY_PROP_TEMP_AMBIENT:
  246. ret = olpc_ec_cmd(EC_AMB_TEMP, NULL, 0, (void *)&ec_word, 2);
  247. if (ret)
  248. return ret;
  249. ec_word = be16_to_cpu(ec_word);
  250. val->intval = ec_word * 100 / 256;
  251. break;
  252. case POWER_SUPPLY_PROP_CHARGE_COUNTER:
  253. ret = olpc_ec_cmd(EC_BAT_ACR, NULL, 0, (void *)&ec_word, 2);
  254. if (ret)
  255. return ret;
  256. ec_word = be16_to_cpu(ec_word);
  257. val->intval = ec_word * 6250 / 15;
  258. break;
  259. case POWER_SUPPLY_PROP_SERIAL_NUMBER:
  260. ret = olpc_ec_cmd(EC_BAT_SERIAL, NULL, 0, (void *)&ser_buf, 8);
  261. if (ret)
  262. return ret;
  263. sprintf(bat_serial, "%016llx", (long long)be64_to_cpu(ser_buf));
  264. val->strval = bat_serial;
  265. break;
  266. default:
  267. ret = -EINVAL;
  268. break;
  269. }
  270. return ret;
  271. }
  272. static enum power_supply_property olpc_bat_props[] = {
  273. POWER_SUPPLY_PROP_STATUS,
  274. POWER_SUPPLY_PROP_PRESENT,
  275. POWER_SUPPLY_PROP_HEALTH,
  276. POWER_SUPPLY_PROP_TECHNOLOGY,
  277. POWER_SUPPLY_PROP_VOLTAGE_AVG,
  278. POWER_SUPPLY_PROP_CURRENT_AVG,
  279. POWER_SUPPLY_PROP_CAPACITY,
  280. POWER_SUPPLY_PROP_TEMP,
  281. POWER_SUPPLY_PROP_TEMP_AMBIENT,
  282. POWER_SUPPLY_PROP_MANUFACTURER,
  283. POWER_SUPPLY_PROP_SERIAL_NUMBER,
  284. POWER_SUPPLY_PROP_CHARGE_COUNTER,
  285. };
  286. /* EEPROM reading goes completely around the power_supply API, sadly */
  287. #define EEPROM_START 0x20
  288. #define EEPROM_END 0x80
  289. #define EEPROM_SIZE (EEPROM_END - EEPROM_START)
  290. static ssize_t olpc_bat_eeprom_read(struct kobject *kobj,
  291. struct bin_attribute *attr, char *buf, loff_t off, size_t count)
  292. {
  293. uint8_t ec_byte;
  294. int ret, end;
  295. if (off >= EEPROM_SIZE)
  296. return 0;
  297. if (off + count > EEPROM_SIZE)
  298. count = EEPROM_SIZE - off;
  299. end = EEPROM_START + off + count;
  300. for (ec_byte = EEPROM_START + off; ec_byte < end; ec_byte++) {
  301. ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1,
  302. &buf[ec_byte - EEPROM_START], 1);
  303. if (ret) {
  304. printk(KERN_ERR "olpc-battery: EC command "
  305. "EC_BAT_EEPROM @ 0x%x failed -"
  306. " %d!\n", ec_byte, ret);
  307. return -EIO;
  308. }
  309. }
  310. return count;
  311. }
  312. static struct bin_attribute olpc_bat_eeprom = {
  313. .attr = {
  314. .name = "eeprom",
  315. .mode = S_IRUGO,
  316. .owner = THIS_MODULE,
  317. },
  318. .size = 0,
  319. .read = olpc_bat_eeprom_read,
  320. };
  321. /*********************************************************************
  322. * Initialisation
  323. *********************************************************************/
  324. static struct platform_device *bat_pdev;
  325. static struct power_supply olpc_bat = {
  326. .properties = olpc_bat_props,
  327. .num_properties = ARRAY_SIZE(olpc_bat_props),
  328. .get_property = olpc_bat_get_property,
  329. .use_for_apm = 1,
  330. };
  331. void olpc_battery_trigger_uevent(unsigned long cause)
  332. {
  333. if (cause & EC_SCI_SRC_ACPWR)
  334. kobject_uevent(&olpc_ac.dev->kobj, KOBJ_CHANGE);
  335. if (cause & (EC_SCI_SRC_BATERR|EC_SCI_SRC_BATSOC|EC_SCI_SRC_BATTERY))
  336. kobject_uevent(&olpc_bat.dev->kobj, KOBJ_CHANGE);
  337. }
  338. static int __init olpc_bat_init(void)
  339. {
  340. int ret = 0;
  341. uint8_t status;
  342. if (!olpc_platform_info.ecver)
  343. return -ENXIO;
  344. /*
  345. * We've seen a number of EC protocol changes; this driver requires
  346. * the latest EC protocol, supported by 0x44 and above.
  347. */
  348. if (olpc_platform_info.ecver < 0x44) {
  349. printk(KERN_NOTICE "OLPC EC version 0x%02x too old for "
  350. "battery driver.\n", olpc_platform_info.ecver);
  351. return -ENXIO;
  352. }
  353. ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &status, 1);
  354. if (ret)
  355. return ret;
  356. /* Ignore the status. It doesn't actually matter */
  357. bat_pdev = platform_device_register_simple("olpc-battery", 0, NULL, 0);
  358. if (IS_ERR(bat_pdev))
  359. return PTR_ERR(bat_pdev);
  360. ret = power_supply_register(&bat_pdev->dev, &olpc_ac);
  361. if (ret)
  362. goto ac_failed;
  363. olpc_bat.name = bat_pdev->name;
  364. ret = power_supply_register(&bat_pdev->dev, &olpc_bat);
  365. if (ret)
  366. goto battery_failed;
  367. ret = device_create_bin_file(olpc_bat.dev, &olpc_bat_eeprom);
  368. if (ret)
  369. goto eeprom_failed;
  370. goto success;
  371. eeprom_failed:
  372. power_supply_unregister(&olpc_bat);
  373. battery_failed:
  374. power_supply_unregister(&olpc_ac);
  375. ac_failed:
  376. platform_device_unregister(bat_pdev);
  377. success:
  378. return ret;
  379. }
  380. static void __exit olpc_bat_exit(void)
  381. {
  382. device_remove_bin_file(olpc_bat.dev, &olpc_bat_eeprom);
  383. power_supply_unregister(&olpc_bat);
  384. power_supply_unregister(&olpc_ac);
  385. platform_device_unregister(bat_pdev);
  386. }
  387. module_init(olpc_bat_init);
  388. module_exit(olpc_bat_exit);
  389. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  390. MODULE_LICENSE("GPL");
  391. MODULE_DESCRIPTION("Battery driver for One Laptop Per Child 'XO' machine");