olpc_battery.c 17 KB

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