olpc_battery.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. /*
  2. * Battery driver for One Laptop Per Child board.
  3. *
  4. * Copyright © 2006-2010 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/types.h>
  13. #include <linux/err.h>
  14. #include <linux/device.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/power_supply.h>
  17. #include <linux/jiffies.h>
  18. #include <linux/sched.h>
  19. #include <asm/olpc.h>
  20. #define EC_BAT_VOLTAGE 0x10 /* uint16_t, *9.76/32, mV */
  21. #define EC_BAT_CURRENT 0x11 /* int16_t, *15.625/120, mA */
  22. #define EC_BAT_ACR 0x12 /* int16_t, *6250/15, µAh */
  23. #define EC_BAT_TEMP 0x13 /* uint16_t, *100/256, °C */
  24. #define EC_AMB_TEMP 0x14 /* uint16_t, *100/256, °C */
  25. #define EC_BAT_STATUS 0x15 /* uint8_t, bitmask */
  26. #define EC_BAT_SOC 0x16 /* uint8_t, percentage */
  27. #define EC_BAT_SERIAL 0x17 /* uint8_t[6] */
  28. #define EC_BAT_EEPROM 0x18 /* uint8_t adr as input, uint8_t output */
  29. #define EC_BAT_ERRCODE 0x1f /* uint8_t, bitmask */
  30. #define BAT_STAT_PRESENT 0x01
  31. #define BAT_STAT_FULL 0x02
  32. #define BAT_STAT_LOW 0x04
  33. #define BAT_STAT_DESTROY 0x08
  34. #define BAT_STAT_AC 0x10
  35. #define BAT_STAT_CHARGING 0x20
  36. #define BAT_STAT_DISCHARGING 0x40
  37. #define BAT_STAT_TRICKLE 0x80
  38. #define BAT_ERR_INFOFAIL 0x02
  39. #define BAT_ERR_OVERVOLTAGE 0x04
  40. #define BAT_ERR_OVERTEMP 0x05
  41. #define BAT_ERR_GAUGESTOP 0x06
  42. #define BAT_ERR_OUT_OF_CONTROL 0x07
  43. #define BAT_ERR_ID_FAIL 0x09
  44. #define BAT_ERR_ACR_FAIL 0x10
  45. #define BAT_ADDR_MFR_TYPE 0x5F
  46. /*********************************************************************
  47. * Power
  48. *********************************************************************/
  49. static int olpc_ac_get_prop(struct power_supply *psy,
  50. enum power_supply_property psp,
  51. union power_supply_propval *val)
  52. {
  53. int ret = 0;
  54. uint8_t status;
  55. switch (psp) {
  56. case POWER_SUPPLY_PROP_ONLINE:
  57. ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &status, 1);
  58. if (ret)
  59. return ret;
  60. val->intval = !!(status & BAT_STAT_AC);
  61. break;
  62. default:
  63. ret = -EINVAL;
  64. break;
  65. }
  66. return ret;
  67. }
  68. static enum power_supply_property olpc_ac_props[] = {
  69. POWER_SUPPLY_PROP_ONLINE,
  70. };
  71. static struct power_supply olpc_ac = {
  72. .name = "olpc-ac",
  73. .type = POWER_SUPPLY_TYPE_MAINS,
  74. .properties = olpc_ac_props,
  75. .num_properties = ARRAY_SIZE(olpc_ac_props),
  76. .get_property = olpc_ac_get_prop,
  77. };
  78. static char bat_serial[17]; /* Ick */
  79. static int olpc_bat_get_status(union power_supply_propval *val, uint8_t ec_byte)
  80. {
  81. if (olpc_platform_info.ecver > 0x44) {
  82. if (ec_byte & (BAT_STAT_CHARGING | BAT_STAT_TRICKLE))
  83. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  84. else if (ec_byte & BAT_STAT_DISCHARGING)
  85. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  86. else if (ec_byte & BAT_STAT_FULL)
  87. val->intval = POWER_SUPPLY_STATUS_FULL;
  88. else /* er,... */
  89. val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
  90. } else {
  91. /* Older EC didn't report charge/discharge bits */
  92. if (!(ec_byte & BAT_STAT_AC)) /* No AC means discharging */
  93. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  94. else if (ec_byte & BAT_STAT_FULL)
  95. val->intval = POWER_SUPPLY_STATUS_FULL;
  96. else /* Not _necessarily_ true but EC doesn't tell all yet */
  97. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  98. }
  99. return 0;
  100. }
  101. static int olpc_bat_get_health(union power_supply_propval *val)
  102. {
  103. uint8_t ec_byte;
  104. int ret;
  105. ret = olpc_ec_cmd(EC_BAT_ERRCODE, NULL, 0, &ec_byte, 1);
  106. if (ret)
  107. return ret;
  108. switch (ec_byte) {
  109. case 0:
  110. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  111. break;
  112. case BAT_ERR_OVERTEMP:
  113. val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
  114. break;
  115. case BAT_ERR_OVERVOLTAGE:
  116. val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  117. break;
  118. case BAT_ERR_INFOFAIL:
  119. case BAT_ERR_OUT_OF_CONTROL:
  120. case BAT_ERR_ID_FAIL:
  121. case BAT_ERR_ACR_FAIL:
  122. val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  123. break;
  124. default:
  125. /* Eep. We don't know this failure code */
  126. ret = -EIO;
  127. }
  128. return ret;
  129. }
  130. static int olpc_bat_get_mfr(union power_supply_propval *val)
  131. {
  132. uint8_t ec_byte;
  133. int ret;
  134. ec_byte = BAT_ADDR_MFR_TYPE;
  135. ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1);
  136. if (ret)
  137. return ret;
  138. switch (ec_byte >> 4) {
  139. case 1:
  140. val->strval = "Gold Peak";
  141. break;
  142. case 2:
  143. val->strval = "BYD";
  144. break;
  145. default:
  146. val->strval = "Unknown";
  147. break;
  148. }
  149. return ret;
  150. }
  151. static int olpc_bat_get_tech(union power_supply_propval *val)
  152. {
  153. uint8_t ec_byte;
  154. int ret;
  155. ec_byte = BAT_ADDR_MFR_TYPE;
  156. ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1);
  157. if (ret)
  158. return ret;
  159. switch (ec_byte & 0xf) {
  160. case 1:
  161. val->intval = POWER_SUPPLY_TECHNOLOGY_NiMH;
  162. break;
  163. case 2:
  164. val->intval = POWER_SUPPLY_TECHNOLOGY_LiFe;
  165. break;
  166. default:
  167. val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
  168. break;
  169. }
  170. return ret;
  171. }
  172. static int olpc_bat_get_charge_full_design(union power_supply_propval *val)
  173. {
  174. uint8_t ec_byte;
  175. union power_supply_propval tech;
  176. int ret, mfr;
  177. ret = olpc_bat_get_tech(&tech);
  178. if (ret)
  179. return ret;
  180. ec_byte = BAT_ADDR_MFR_TYPE;
  181. ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1);
  182. if (ret)
  183. return ret;
  184. mfr = ec_byte >> 4;
  185. switch (tech.intval) {
  186. case POWER_SUPPLY_TECHNOLOGY_NiMH:
  187. switch (mfr) {
  188. case 1: /* Gold Peak */
  189. val->intval = 3000000*.8;
  190. break;
  191. default:
  192. return -EIO;
  193. }
  194. break;
  195. case POWER_SUPPLY_TECHNOLOGY_LiFe:
  196. switch (mfr) {
  197. case 1: /* Gold Peak, fall through */
  198. case 2: /* BYD */
  199. val->intval = 2800000;
  200. break;
  201. default:
  202. return -EIO;
  203. }
  204. break;
  205. default:
  206. return -EIO;
  207. }
  208. return ret;
  209. }
  210. static int olpc_bat_get_charge_now(union power_supply_propval *val)
  211. {
  212. uint8_t soc;
  213. union power_supply_propval full;
  214. int ret;
  215. ret = olpc_ec_cmd(EC_BAT_SOC, NULL, 0, &soc, 1);
  216. if (ret)
  217. return ret;
  218. ret = olpc_bat_get_charge_full_design(&full);
  219. if (ret)
  220. return ret;
  221. val->intval = soc * (full.intval / 100);
  222. return 0;
  223. }
  224. static int olpc_bat_get_voltage_max_design(union power_supply_propval *val)
  225. {
  226. uint8_t ec_byte;
  227. union power_supply_propval tech;
  228. int mfr;
  229. int ret;
  230. ret = olpc_bat_get_tech(&tech);
  231. if (ret)
  232. return ret;
  233. ec_byte = BAT_ADDR_MFR_TYPE;
  234. ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1);
  235. if (ret)
  236. return ret;
  237. mfr = ec_byte >> 4;
  238. switch (tech.intval) {
  239. case POWER_SUPPLY_TECHNOLOGY_NiMH:
  240. switch (mfr) {
  241. case 1: /* Gold Peak */
  242. val->intval = 6000000;
  243. break;
  244. default:
  245. return -EIO;
  246. }
  247. break;
  248. case POWER_SUPPLY_TECHNOLOGY_LiFe:
  249. switch (mfr) {
  250. case 1: /* Gold Peak */
  251. val->intval = 6400000;
  252. break;
  253. case 2: /* BYD */
  254. val->intval = 6500000;
  255. break;
  256. default:
  257. return -EIO;
  258. }
  259. break;
  260. default:
  261. return -EIO;
  262. }
  263. return ret;
  264. }
  265. /*********************************************************************
  266. * Battery properties
  267. *********************************************************************/
  268. static int olpc_bat_get_property(struct power_supply *psy,
  269. enum power_supply_property psp,
  270. union power_supply_propval *val)
  271. {
  272. int ret = 0;
  273. __be16 ec_word;
  274. uint8_t ec_byte;
  275. __be64 ser_buf;
  276. ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &ec_byte, 1);
  277. if (ret)
  278. return ret;
  279. /* Theoretically there's a race here -- the battery could be
  280. removed immediately after we check whether it's present, and
  281. then we query for some other property of the now-absent battery.
  282. It doesn't matter though -- the EC will return the last-known
  283. information, and it's as if we just ran that _little_ bit faster
  284. and managed to read it out before the battery went away. */
  285. if (!(ec_byte & (BAT_STAT_PRESENT | BAT_STAT_TRICKLE)) &&
  286. psp != POWER_SUPPLY_PROP_PRESENT)
  287. return -ENODEV;
  288. switch (psp) {
  289. case POWER_SUPPLY_PROP_STATUS:
  290. ret = olpc_bat_get_status(val, ec_byte);
  291. if (ret)
  292. return ret;
  293. break;
  294. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  295. if (ec_byte & BAT_STAT_TRICKLE)
  296. val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
  297. else if (ec_byte & BAT_STAT_CHARGING)
  298. val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
  299. else
  300. val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
  301. break;
  302. case POWER_SUPPLY_PROP_PRESENT:
  303. val->intval = !!(ec_byte & (BAT_STAT_PRESENT |
  304. BAT_STAT_TRICKLE));
  305. break;
  306. case POWER_SUPPLY_PROP_HEALTH:
  307. if (ec_byte & BAT_STAT_DESTROY)
  308. val->intval = POWER_SUPPLY_HEALTH_DEAD;
  309. else {
  310. ret = olpc_bat_get_health(val);
  311. if (ret)
  312. return ret;
  313. }
  314. break;
  315. case POWER_SUPPLY_PROP_MANUFACTURER:
  316. ret = olpc_bat_get_mfr(val);
  317. if (ret)
  318. return ret;
  319. break;
  320. case POWER_SUPPLY_PROP_TECHNOLOGY:
  321. ret = olpc_bat_get_tech(val);
  322. if (ret)
  323. return ret;
  324. break;
  325. case POWER_SUPPLY_PROP_VOLTAGE_AVG:
  326. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  327. ret = olpc_ec_cmd(EC_BAT_VOLTAGE, NULL, 0, (void *)&ec_word, 2);
  328. if (ret)
  329. return ret;
  330. val->intval = (s16)be16_to_cpu(ec_word) * 9760L / 32;
  331. break;
  332. case POWER_SUPPLY_PROP_CURRENT_AVG:
  333. case POWER_SUPPLY_PROP_CURRENT_NOW:
  334. ret = olpc_ec_cmd(EC_BAT_CURRENT, NULL, 0, (void *)&ec_word, 2);
  335. if (ret)
  336. return ret;
  337. val->intval = (s16)be16_to_cpu(ec_word) * 15625L / 120;
  338. break;
  339. case POWER_SUPPLY_PROP_CAPACITY:
  340. ret = olpc_ec_cmd(EC_BAT_SOC, NULL, 0, &ec_byte, 1);
  341. if (ret)
  342. return ret;
  343. val->intval = ec_byte;
  344. break;
  345. case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
  346. if (ec_byte & BAT_STAT_FULL)
  347. val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
  348. else if (ec_byte & BAT_STAT_LOW)
  349. val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
  350. else
  351. val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
  352. break;
  353. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  354. ret = olpc_bat_get_charge_full_design(val);
  355. if (ret)
  356. return ret;
  357. break;
  358. case POWER_SUPPLY_PROP_CHARGE_NOW:
  359. ret = olpc_bat_get_charge_now(val);
  360. if (ret)
  361. return ret;
  362. break;
  363. case POWER_SUPPLY_PROP_TEMP:
  364. ret = olpc_ec_cmd(EC_BAT_TEMP, NULL, 0, (void *)&ec_word, 2);
  365. if (ret)
  366. return ret;
  367. val->intval = (s16)be16_to_cpu(ec_word) * 100 / 256;
  368. break;
  369. case POWER_SUPPLY_PROP_TEMP_AMBIENT:
  370. ret = olpc_ec_cmd(EC_AMB_TEMP, NULL, 0, (void *)&ec_word, 2);
  371. if (ret)
  372. return ret;
  373. val->intval = (int)be16_to_cpu(ec_word) * 100 / 256;
  374. break;
  375. case POWER_SUPPLY_PROP_CHARGE_COUNTER:
  376. ret = olpc_ec_cmd(EC_BAT_ACR, NULL, 0, (void *)&ec_word, 2);
  377. if (ret)
  378. return ret;
  379. val->intval = (s16)be16_to_cpu(ec_word) * 6250 / 15;
  380. break;
  381. case POWER_SUPPLY_PROP_SERIAL_NUMBER:
  382. ret = olpc_ec_cmd(EC_BAT_SERIAL, NULL, 0, (void *)&ser_buf, 8);
  383. if (ret)
  384. return ret;
  385. sprintf(bat_serial, "%016llx", (long long)be64_to_cpu(ser_buf));
  386. val->strval = bat_serial;
  387. break;
  388. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  389. ret = olpc_bat_get_voltage_max_design(val);
  390. if (ret)
  391. return ret;
  392. break;
  393. default:
  394. ret = -EINVAL;
  395. break;
  396. }
  397. return ret;
  398. }
  399. static enum power_supply_property olpc_xo1_bat_props[] = {
  400. POWER_SUPPLY_PROP_STATUS,
  401. POWER_SUPPLY_PROP_CHARGE_TYPE,
  402. POWER_SUPPLY_PROP_PRESENT,
  403. POWER_SUPPLY_PROP_HEALTH,
  404. POWER_SUPPLY_PROP_TECHNOLOGY,
  405. POWER_SUPPLY_PROP_VOLTAGE_AVG,
  406. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  407. POWER_SUPPLY_PROP_CURRENT_AVG,
  408. POWER_SUPPLY_PROP_CURRENT_NOW,
  409. POWER_SUPPLY_PROP_CAPACITY,
  410. POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  411. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  412. POWER_SUPPLY_PROP_CHARGE_NOW,
  413. POWER_SUPPLY_PROP_TEMP,
  414. POWER_SUPPLY_PROP_TEMP_AMBIENT,
  415. POWER_SUPPLY_PROP_MANUFACTURER,
  416. POWER_SUPPLY_PROP_SERIAL_NUMBER,
  417. POWER_SUPPLY_PROP_CHARGE_COUNTER,
  418. POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
  419. };
  420. /* XO-1.5 does not have ambient temperature property */
  421. static enum power_supply_property olpc_xo15_bat_props[] = {
  422. POWER_SUPPLY_PROP_STATUS,
  423. POWER_SUPPLY_PROP_CHARGE_TYPE,
  424. POWER_SUPPLY_PROP_PRESENT,
  425. POWER_SUPPLY_PROP_HEALTH,
  426. POWER_SUPPLY_PROP_TECHNOLOGY,
  427. POWER_SUPPLY_PROP_VOLTAGE_AVG,
  428. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  429. POWER_SUPPLY_PROP_CURRENT_AVG,
  430. POWER_SUPPLY_PROP_CURRENT_NOW,
  431. POWER_SUPPLY_PROP_CAPACITY,
  432. POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  433. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  434. POWER_SUPPLY_PROP_CHARGE_NOW,
  435. POWER_SUPPLY_PROP_TEMP,
  436. POWER_SUPPLY_PROP_MANUFACTURER,
  437. POWER_SUPPLY_PROP_SERIAL_NUMBER,
  438. POWER_SUPPLY_PROP_CHARGE_COUNTER,
  439. POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
  440. };
  441. /* EEPROM reading goes completely around the power_supply API, sadly */
  442. #define EEPROM_START 0x20
  443. #define EEPROM_END 0x80
  444. #define EEPROM_SIZE (EEPROM_END - EEPROM_START)
  445. static ssize_t olpc_bat_eeprom_read(struct file *filp, struct kobject *kobj,
  446. struct bin_attribute *attr, char *buf, loff_t off, size_t count)
  447. {
  448. uint8_t ec_byte;
  449. int ret;
  450. int i;
  451. if (off >= EEPROM_SIZE)
  452. return 0;
  453. if (off + count > EEPROM_SIZE)
  454. count = EEPROM_SIZE - off;
  455. for (i = 0; i < count; i++) {
  456. ec_byte = EEPROM_START + off + i;
  457. ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &buf[i], 1);
  458. if (ret) {
  459. pr_err("olpc-battery: "
  460. "EC_BAT_EEPROM cmd @ 0x%x failed - %d!\n",
  461. ec_byte, ret);
  462. return -EIO;
  463. }
  464. }
  465. return count;
  466. }
  467. static struct bin_attribute olpc_bat_eeprom = {
  468. .attr = {
  469. .name = "eeprom",
  470. .mode = S_IRUGO,
  471. },
  472. .size = 0,
  473. .read = olpc_bat_eeprom_read,
  474. };
  475. /* Allow userspace to see the specific error value pulled from the EC */
  476. static ssize_t olpc_bat_error_read(struct device *dev,
  477. struct device_attribute *attr, char *buf)
  478. {
  479. uint8_t ec_byte;
  480. ssize_t ret;
  481. ret = olpc_ec_cmd(EC_BAT_ERRCODE, NULL, 0, &ec_byte, 1);
  482. if (ret < 0)
  483. return ret;
  484. return sprintf(buf, "%d\n", ec_byte);
  485. }
  486. static struct device_attribute olpc_bat_error = {
  487. .attr = {
  488. .name = "error",
  489. .mode = S_IRUGO,
  490. },
  491. .show = olpc_bat_error_read,
  492. };
  493. /*********************************************************************
  494. * Initialisation
  495. *********************************************************************/
  496. static struct power_supply olpc_bat = {
  497. .name = "olpc-battery",
  498. .get_property = olpc_bat_get_property,
  499. .use_for_apm = 1,
  500. };
  501. static int olpc_battery_suspend(struct platform_device *pdev,
  502. pm_message_t state)
  503. {
  504. if (device_may_wakeup(olpc_ac.dev))
  505. olpc_ec_wakeup_set(EC_SCI_SRC_ACPWR);
  506. else
  507. olpc_ec_wakeup_clear(EC_SCI_SRC_ACPWR);
  508. if (device_may_wakeup(olpc_bat.dev))
  509. olpc_ec_wakeup_set(EC_SCI_SRC_BATTERY | EC_SCI_SRC_BATSOC
  510. | EC_SCI_SRC_BATERR);
  511. else
  512. olpc_ec_wakeup_clear(EC_SCI_SRC_BATTERY | EC_SCI_SRC_BATSOC
  513. | EC_SCI_SRC_BATERR);
  514. return 0;
  515. }
  516. static int __devinit olpc_battery_probe(struct platform_device *pdev)
  517. {
  518. int ret;
  519. uint8_t status;
  520. /*
  521. * We've seen a number of EC protocol changes; this driver requires
  522. * the latest EC protocol, supported by 0x44 and above.
  523. */
  524. if (olpc_platform_info.ecver < 0x44) {
  525. printk(KERN_NOTICE "OLPC EC version 0x%02x too old for "
  526. "battery driver.\n", olpc_platform_info.ecver);
  527. return -ENXIO;
  528. }
  529. ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &status, 1);
  530. if (ret)
  531. return ret;
  532. /* Ignore the status. It doesn't actually matter */
  533. ret = power_supply_register(&pdev->dev, &olpc_ac);
  534. if (ret)
  535. return ret;
  536. if (olpc_board_at_least(olpc_board_pre(0xd0))) { /* XO-1.5 */
  537. olpc_bat.properties = olpc_xo15_bat_props;
  538. olpc_bat.num_properties = ARRAY_SIZE(olpc_xo15_bat_props);
  539. } else { /* XO-1 */
  540. olpc_bat.properties = olpc_xo1_bat_props;
  541. olpc_bat.num_properties = ARRAY_SIZE(olpc_xo1_bat_props);
  542. }
  543. ret = power_supply_register(&pdev->dev, &olpc_bat);
  544. if (ret)
  545. goto battery_failed;
  546. ret = device_create_bin_file(olpc_bat.dev, &olpc_bat_eeprom);
  547. if (ret)
  548. goto eeprom_failed;
  549. ret = device_create_file(olpc_bat.dev, &olpc_bat_error);
  550. if (ret)
  551. goto error_failed;
  552. if (olpc_ec_wakeup_available()) {
  553. device_set_wakeup_capable(olpc_ac.dev, true);
  554. device_set_wakeup_capable(olpc_bat.dev, true);
  555. }
  556. return 0;
  557. error_failed:
  558. device_remove_bin_file(olpc_bat.dev, &olpc_bat_eeprom);
  559. eeprom_failed:
  560. power_supply_unregister(&olpc_bat);
  561. battery_failed:
  562. power_supply_unregister(&olpc_ac);
  563. return ret;
  564. }
  565. static int __devexit olpc_battery_remove(struct platform_device *pdev)
  566. {
  567. device_remove_file(olpc_bat.dev, &olpc_bat_error);
  568. device_remove_bin_file(olpc_bat.dev, &olpc_bat_eeprom);
  569. power_supply_unregister(&olpc_bat);
  570. power_supply_unregister(&olpc_ac);
  571. return 0;
  572. }
  573. static const struct of_device_id olpc_battery_ids[] __devinitconst = {
  574. { .compatible = "olpc,xo1-battery" },
  575. {}
  576. };
  577. MODULE_DEVICE_TABLE(of, olpc_battery_ids);
  578. static struct platform_driver olpc_battery_driver = {
  579. .driver = {
  580. .name = "olpc-battery",
  581. .owner = THIS_MODULE,
  582. .of_match_table = olpc_battery_ids,
  583. },
  584. .probe = olpc_battery_probe,
  585. .remove = __devexit_p(olpc_battery_remove),
  586. .suspend = olpc_battery_suspend,
  587. };
  588. module_platform_driver(olpc_battery_driver);
  589. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  590. MODULE_LICENSE("GPL");
  591. MODULE_DESCRIPTION("Battery driver for One Laptop Per Child 'XO' machine");