olpc_battery.c 11 KB

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