olpc_battery.c 16 KB

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