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