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