olpc_battery.c 16 KB

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