sbs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. /*
  2. * sbs.c - ACPI Smart Battery System Driver ($Revision: 2.0 $)
  3. *
  4. * Copyright (c) 2007 Alexey Starikovskiy <astarikovskiy@suse.de>
  5. * Copyright (c) 2005-2007 Vladimir Lebedev <vladimir.p.lebedev@intel.com>
  6. * Copyright (c) 2005 Rich Townsend <rhdt@bartol.udel.edu>
  7. *
  8. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or (at
  13. * your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  21. */
  22. #include <linux/init.h>
  23. #include <linux/slab.h>
  24. #include <linux/module.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/kernel.h>
  27. #include <linux/acpi.h>
  28. #include <linux/timer.h>
  29. #include <linux/jiffies.h>
  30. #include <linux/delay.h>
  31. #include <linux/power_supply.h>
  32. #include <linux/dmi.h>
  33. #include "sbshc.h"
  34. #include "battery.h"
  35. #define PREFIX "ACPI: "
  36. #define ACPI_SBS_CLASS "sbs"
  37. #define ACPI_AC_CLASS "ac_adapter"
  38. #define ACPI_SBS_DEVICE_NAME "Smart Battery System"
  39. #define ACPI_SBS_FILE_INFO "info"
  40. #define ACPI_SBS_FILE_STATE "state"
  41. #define ACPI_SBS_FILE_ALARM "alarm"
  42. #define ACPI_BATTERY_DIR_NAME "BAT%i"
  43. #define ACPI_AC_DIR_NAME "AC0"
  44. #define ACPI_SBS_NOTIFY_STATUS 0x80
  45. #define ACPI_SBS_NOTIFY_INFO 0x81
  46. MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
  47. MODULE_DESCRIPTION("Smart Battery System ACPI interface driver");
  48. MODULE_LICENSE("GPL");
  49. static unsigned int cache_time = 1000;
  50. module_param(cache_time, uint, 0644);
  51. MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
  52. static bool sbs_manager_broken;
  53. #define MAX_SBS_BAT 4
  54. #define ACPI_SBS_BLOCK_MAX 32
  55. static const struct acpi_device_id sbs_device_ids[] = {
  56. {"ACPI0002", 0},
  57. {"", 0},
  58. };
  59. MODULE_DEVICE_TABLE(acpi, sbs_device_ids);
  60. struct acpi_battery {
  61. struct power_supply *bat;
  62. struct power_supply_desc bat_desc;
  63. struct acpi_sbs *sbs;
  64. unsigned long update_time;
  65. char name[8];
  66. char manufacturer_name[ACPI_SBS_BLOCK_MAX];
  67. char device_name[ACPI_SBS_BLOCK_MAX];
  68. char device_chemistry[ACPI_SBS_BLOCK_MAX];
  69. u16 alarm_capacity;
  70. u16 full_charge_capacity;
  71. u16 design_capacity;
  72. u16 design_voltage;
  73. u16 serial_number;
  74. u16 cycle_count;
  75. u16 temp_now;
  76. u16 voltage_now;
  77. s16 rate_now;
  78. s16 rate_avg;
  79. u16 capacity_now;
  80. u16 state_of_charge;
  81. u16 state;
  82. u16 mode;
  83. u16 spec;
  84. u8 id;
  85. u8 present:1;
  86. u8 have_sysfs_alarm:1;
  87. };
  88. #define to_acpi_battery(x) power_supply_get_drvdata(x)
  89. struct acpi_sbs {
  90. struct power_supply *charger;
  91. struct acpi_device *device;
  92. struct acpi_smb_hc *hc;
  93. struct mutex lock;
  94. struct acpi_battery battery[MAX_SBS_BAT];
  95. u8 batteries_supported:4;
  96. u8 manager_present:1;
  97. u8 charger_present:1;
  98. u8 charger_exists:1;
  99. };
  100. #define to_acpi_sbs(x) power_supply_get_drvdata(x)
  101. static int acpi_sbs_remove(struct acpi_device *device);
  102. static int acpi_battery_get_state(struct acpi_battery *battery);
  103. static inline int battery_scale(int log)
  104. {
  105. int scale = 1;
  106. while (log--)
  107. scale *= 10;
  108. return scale;
  109. }
  110. static inline int acpi_battery_vscale(struct acpi_battery *battery)
  111. {
  112. return battery_scale((battery->spec & 0x0f00) >> 8);
  113. }
  114. static inline int acpi_battery_ipscale(struct acpi_battery *battery)
  115. {
  116. return battery_scale((battery->spec & 0xf000) >> 12);
  117. }
  118. static inline int acpi_battery_mode(struct acpi_battery *battery)
  119. {
  120. return (battery->mode & 0x8000);
  121. }
  122. static inline int acpi_battery_scale(struct acpi_battery *battery)
  123. {
  124. return (acpi_battery_mode(battery) ? 10 : 1) *
  125. acpi_battery_ipscale(battery);
  126. }
  127. static int sbs_get_ac_property(struct power_supply *psy,
  128. enum power_supply_property psp,
  129. union power_supply_propval *val)
  130. {
  131. struct acpi_sbs *sbs = to_acpi_sbs(psy);
  132. switch (psp) {
  133. case POWER_SUPPLY_PROP_ONLINE:
  134. val->intval = sbs->charger_present;
  135. break;
  136. default:
  137. return -EINVAL;
  138. }
  139. return 0;
  140. }
  141. static int acpi_battery_technology(struct acpi_battery *battery)
  142. {
  143. if (!strcasecmp("NiCd", battery->device_chemistry))
  144. return POWER_SUPPLY_TECHNOLOGY_NiCd;
  145. if (!strcasecmp("NiMH", battery->device_chemistry))
  146. return POWER_SUPPLY_TECHNOLOGY_NiMH;
  147. if (!strcasecmp("LION", battery->device_chemistry))
  148. return POWER_SUPPLY_TECHNOLOGY_LION;
  149. if (!strcasecmp("LiP", battery->device_chemistry))
  150. return POWER_SUPPLY_TECHNOLOGY_LIPO;
  151. return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
  152. }
  153. static int acpi_sbs_battery_get_property(struct power_supply *psy,
  154. enum power_supply_property psp,
  155. union power_supply_propval *val)
  156. {
  157. struct acpi_battery *battery = to_acpi_battery(psy);
  158. if ((!battery->present) && psp != POWER_SUPPLY_PROP_PRESENT)
  159. return -ENODEV;
  160. acpi_battery_get_state(battery);
  161. switch (psp) {
  162. case POWER_SUPPLY_PROP_STATUS:
  163. if (battery->rate_now < 0)
  164. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  165. else if (battery->rate_now > 0)
  166. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  167. else
  168. val->intval = POWER_SUPPLY_STATUS_FULL;
  169. break;
  170. case POWER_SUPPLY_PROP_PRESENT:
  171. val->intval = battery->present;
  172. break;
  173. case POWER_SUPPLY_PROP_TECHNOLOGY:
  174. val->intval = acpi_battery_technology(battery);
  175. break;
  176. case POWER_SUPPLY_PROP_CYCLE_COUNT:
  177. val->intval = battery->cycle_count;
  178. break;
  179. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  180. val->intval = battery->design_voltage *
  181. acpi_battery_vscale(battery) * 1000;
  182. break;
  183. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  184. val->intval = battery->voltage_now *
  185. acpi_battery_vscale(battery) * 1000;
  186. break;
  187. case POWER_SUPPLY_PROP_CURRENT_NOW:
  188. case POWER_SUPPLY_PROP_POWER_NOW:
  189. val->intval = abs(battery->rate_now) *
  190. acpi_battery_ipscale(battery) * 1000;
  191. val->intval *= (acpi_battery_mode(battery)) ?
  192. (battery->voltage_now *
  193. acpi_battery_vscale(battery) / 1000) : 1;
  194. break;
  195. case POWER_SUPPLY_PROP_CURRENT_AVG:
  196. case POWER_SUPPLY_PROP_POWER_AVG:
  197. val->intval = abs(battery->rate_avg) *
  198. acpi_battery_ipscale(battery) * 1000;
  199. val->intval *= (acpi_battery_mode(battery)) ?
  200. (battery->voltage_now *
  201. acpi_battery_vscale(battery) / 1000) : 1;
  202. break;
  203. case POWER_SUPPLY_PROP_CAPACITY:
  204. val->intval = battery->state_of_charge;
  205. break;
  206. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  207. case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
  208. val->intval = battery->design_capacity *
  209. acpi_battery_scale(battery) * 1000;
  210. break;
  211. case POWER_SUPPLY_PROP_CHARGE_FULL:
  212. case POWER_SUPPLY_PROP_ENERGY_FULL:
  213. val->intval = battery->full_charge_capacity *
  214. acpi_battery_scale(battery) * 1000;
  215. break;
  216. case POWER_SUPPLY_PROP_CHARGE_NOW:
  217. case POWER_SUPPLY_PROP_ENERGY_NOW:
  218. val->intval = battery->capacity_now *
  219. acpi_battery_scale(battery) * 1000;
  220. break;
  221. case POWER_SUPPLY_PROP_TEMP:
  222. val->intval = battery->temp_now - 2730; // dK -> dC
  223. break;
  224. case POWER_SUPPLY_PROP_MODEL_NAME:
  225. val->strval = battery->device_name;
  226. break;
  227. case POWER_SUPPLY_PROP_MANUFACTURER:
  228. val->strval = battery->manufacturer_name;
  229. break;
  230. default:
  231. return -EINVAL;
  232. }
  233. return 0;
  234. }
  235. static enum power_supply_property sbs_ac_props[] = {
  236. POWER_SUPPLY_PROP_ONLINE,
  237. };
  238. static enum power_supply_property sbs_charge_battery_props[] = {
  239. POWER_SUPPLY_PROP_STATUS,
  240. POWER_SUPPLY_PROP_PRESENT,
  241. POWER_SUPPLY_PROP_TECHNOLOGY,
  242. POWER_SUPPLY_PROP_CYCLE_COUNT,
  243. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  244. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  245. POWER_SUPPLY_PROP_CURRENT_NOW,
  246. POWER_SUPPLY_PROP_CURRENT_AVG,
  247. POWER_SUPPLY_PROP_CAPACITY,
  248. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  249. POWER_SUPPLY_PROP_CHARGE_FULL,
  250. POWER_SUPPLY_PROP_CHARGE_NOW,
  251. POWER_SUPPLY_PROP_TEMP,
  252. POWER_SUPPLY_PROP_MODEL_NAME,
  253. POWER_SUPPLY_PROP_MANUFACTURER,
  254. };
  255. static enum power_supply_property sbs_energy_battery_props[] = {
  256. POWER_SUPPLY_PROP_STATUS,
  257. POWER_SUPPLY_PROP_PRESENT,
  258. POWER_SUPPLY_PROP_TECHNOLOGY,
  259. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  260. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  261. POWER_SUPPLY_PROP_CURRENT_NOW,
  262. POWER_SUPPLY_PROP_CURRENT_AVG,
  263. POWER_SUPPLY_PROP_POWER_NOW,
  264. POWER_SUPPLY_PROP_POWER_AVG,
  265. POWER_SUPPLY_PROP_CAPACITY,
  266. POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
  267. POWER_SUPPLY_PROP_ENERGY_FULL,
  268. POWER_SUPPLY_PROP_ENERGY_NOW,
  269. POWER_SUPPLY_PROP_TEMP,
  270. POWER_SUPPLY_PROP_MODEL_NAME,
  271. POWER_SUPPLY_PROP_MANUFACTURER,
  272. };
  273. static const struct power_supply_desc acpi_sbs_charger_desc = {
  274. .name = "sbs-charger",
  275. .type = POWER_SUPPLY_TYPE_MAINS,
  276. .properties = sbs_ac_props,
  277. .num_properties = ARRAY_SIZE(sbs_ac_props),
  278. .get_property = sbs_get_ac_property,
  279. };
  280. /* --------------------------------------------------------------------------
  281. Smart Battery System Management
  282. -------------------------------------------------------------------------- */
  283. struct acpi_battery_reader {
  284. u8 command; /* command for battery */
  285. u8 mode; /* word or block? */
  286. size_t offset; /* offset inside struct acpi_sbs_battery */
  287. };
  288. static struct acpi_battery_reader info_readers[] = {
  289. {0x01, SMBUS_READ_WORD, offsetof(struct acpi_battery, alarm_capacity)},
  290. {0x03, SMBUS_READ_WORD, offsetof(struct acpi_battery, mode)},
  291. {0x10, SMBUS_READ_WORD, offsetof(struct acpi_battery, full_charge_capacity)},
  292. {0x17, SMBUS_READ_WORD, offsetof(struct acpi_battery, cycle_count)},
  293. {0x18, SMBUS_READ_WORD, offsetof(struct acpi_battery, design_capacity)},
  294. {0x19, SMBUS_READ_WORD, offsetof(struct acpi_battery, design_voltage)},
  295. {0x1a, SMBUS_READ_WORD, offsetof(struct acpi_battery, spec)},
  296. {0x1c, SMBUS_READ_WORD, offsetof(struct acpi_battery, serial_number)},
  297. {0x20, SMBUS_READ_BLOCK, offsetof(struct acpi_battery, manufacturer_name)},
  298. {0x21, SMBUS_READ_BLOCK, offsetof(struct acpi_battery, device_name)},
  299. {0x22, SMBUS_READ_BLOCK, offsetof(struct acpi_battery, device_chemistry)},
  300. };
  301. static struct acpi_battery_reader state_readers[] = {
  302. {0x08, SMBUS_READ_WORD, offsetof(struct acpi_battery, temp_now)},
  303. {0x09, SMBUS_READ_WORD, offsetof(struct acpi_battery, voltage_now)},
  304. {0x0a, SMBUS_READ_WORD, offsetof(struct acpi_battery, rate_now)},
  305. {0x0b, SMBUS_READ_WORD, offsetof(struct acpi_battery, rate_avg)},
  306. {0x0f, SMBUS_READ_WORD, offsetof(struct acpi_battery, capacity_now)},
  307. {0x0e, SMBUS_READ_WORD, offsetof(struct acpi_battery, state_of_charge)},
  308. {0x16, SMBUS_READ_WORD, offsetof(struct acpi_battery, state)},
  309. };
  310. static int acpi_manager_get_info(struct acpi_sbs *sbs)
  311. {
  312. int result = 0;
  313. u16 battery_system_info;
  314. result = acpi_smbus_read(sbs->hc, SMBUS_READ_WORD, ACPI_SBS_MANAGER,
  315. 0x04, (u8 *)&battery_system_info);
  316. if (!result)
  317. sbs->batteries_supported = battery_system_info & 0x000f;
  318. return result;
  319. }
  320. static int acpi_battery_get_info(struct acpi_battery *battery)
  321. {
  322. int i, result = 0;
  323. for (i = 0; i < ARRAY_SIZE(info_readers); ++i) {
  324. result = acpi_smbus_read(battery->sbs->hc,
  325. info_readers[i].mode,
  326. ACPI_SBS_BATTERY,
  327. info_readers[i].command,
  328. (u8 *) battery +
  329. info_readers[i].offset);
  330. if (result)
  331. break;
  332. }
  333. return result;
  334. }
  335. static int acpi_battery_get_state(struct acpi_battery *battery)
  336. {
  337. int i, result = 0;
  338. if (battery->update_time &&
  339. time_before(jiffies, battery->update_time +
  340. msecs_to_jiffies(cache_time)))
  341. return 0;
  342. for (i = 0; i < ARRAY_SIZE(state_readers); ++i) {
  343. result = acpi_smbus_read(battery->sbs->hc,
  344. state_readers[i].mode,
  345. ACPI_SBS_BATTERY,
  346. state_readers[i].command,
  347. (u8 *)battery +
  348. state_readers[i].offset);
  349. if (result)
  350. goto end;
  351. }
  352. end:
  353. battery->update_time = jiffies;
  354. return result;
  355. }
  356. static int acpi_battery_get_alarm(struct acpi_battery *battery)
  357. {
  358. return acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD,
  359. ACPI_SBS_BATTERY, 0x01,
  360. (u8 *)&battery->alarm_capacity);
  361. }
  362. static int acpi_battery_set_alarm(struct acpi_battery *battery)
  363. {
  364. struct acpi_sbs *sbs = battery->sbs;
  365. u16 value, sel = 1 << (battery->id + 12);
  366. int ret;
  367. if (sbs->manager_present) {
  368. ret = acpi_smbus_read(sbs->hc, SMBUS_READ_WORD, ACPI_SBS_MANAGER,
  369. 0x01, (u8 *)&value);
  370. if (ret)
  371. goto end;
  372. if ((value & 0xf000) != sel) {
  373. value &= 0x0fff;
  374. value |= sel;
  375. ret = acpi_smbus_write(sbs->hc, SMBUS_WRITE_WORD,
  376. ACPI_SBS_MANAGER,
  377. 0x01, (u8 *)&value, 2);
  378. if (ret)
  379. goto end;
  380. }
  381. }
  382. ret = acpi_smbus_write(sbs->hc, SMBUS_WRITE_WORD, ACPI_SBS_BATTERY,
  383. 0x01, (u8 *)&battery->alarm_capacity, 2);
  384. end:
  385. return ret;
  386. }
  387. static int acpi_ac_get_present(struct acpi_sbs *sbs)
  388. {
  389. int result;
  390. u16 status;
  391. result = acpi_smbus_read(sbs->hc, SMBUS_READ_WORD, ACPI_SBS_CHARGER,
  392. 0x13, (u8 *) & status);
  393. if (result)
  394. return result;
  395. /*
  396. * The spec requires that bit 4 always be 1. If it's not set, assume
  397. * that the implementation doesn't support an SBS charger.
  398. *
  399. * And on some MacBooks a status of 0xffff is always returned, no
  400. * matter whether the charger is plugged in or not, which is also
  401. * wrong, so ignore the SBS charger for those too.
  402. */
  403. if (!((status >> 4) & 0x1) || status == 0xffff)
  404. return -ENODEV;
  405. sbs->charger_present = (status >> 15) & 0x1;
  406. return 0;
  407. }
  408. static ssize_t acpi_battery_alarm_show(struct device *dev,
  409. struct device_attribute *attr,
  410. char *buf)
  411. {
  412. struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
  413. acpi_battery_get_alarm(battery);
  414. return sprintf(buf, "%d\n", battery->alarm_capacity *
  415. acpi_battery_scale(battery) * 1000);
  416. }
  417. static ssize_t acpi_battery_alarm_store(struct device *dev,
  418. struct device_attribute *attr,
  419. const char *buf, size_t count)
  420. {
  421. unsigned long x;
  422. struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
  423. if (sscanf(buf, "%lu\n", &x) == 1)
  424. battery->alarm_capacity = x /
  425. (1000 * acpi_battery_scale(battery));
  426. if (battery->present)
  427. acpi_battery_set_alarm(battery);
  428. return count;
  429. }
  430. static struct device_attribute alarm_attr = {
  431. .attr = {.name = "alarm", .mode = 0644},
  432. .show = acpi_battery_alarm_show,
  433. .store = acpi_battery_alarm_store,
  434. };
  435. /* --------------------------------------------------------------------------
  436. Driver Interface
  437. -------------------------------------------------------------------------- */
  438. static int acpi_battery_read(struct acpi_battery *battery)
  439. {
  440. int result = 0, saved_present = battery->present;
  441. u16 state;
  442. if (battery->sbs->manager_present) {
  443. result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD,
  444. ACPI_SBS_MANAGER, 0x01, (u8 *)&state);
  445. if (!result)
  446. battery->present = state & (1 << battery->id);
  447. state &= 0x0fff;
  448. state |= 1 << (battery->id + 12);
  449. acpi_smbus_write(battery->sbs->hc, SMBUS_WRITE_WORD,
  450. ACPI_SBS_MANAGER, 0x01, (u8 *)&state, 2);
  451. } else if (battery->id == 0)
  452. battery->present = 1;
  453. if (result || !battery->present)
  454. return result;
  455. if (saved_present != battery->present) {
  456. battery->update_time = 0;
  457. result = acpi_battery_get_info(battery);
  458. if (result) {
  459. battery->present = 0;
  460. return result;
  461. }
  462. }
  463. result = acpi_battery_get_state(battery);
  464. if (result)
  465. battery->present = 0;
  466. return result;
  467. }
  468. /* Smart Battery */
  469. static int acpi_battery_add(struct acpi_sbs *sbs, int id)
  470. {
  471. struct acpi_battery *battery = &sbs->battery[id];
  472. struct power_supply_config psy_cfg = { .drv_data = battery, };
  473. int result;
  474. battery->id = id;
  475. battery->sbs = sbs;
  476. result = acpi_battery_read(battery);
  477. if (result)
  478. return result;
  479. sprintf(battery->name, ACPI_BATTERY_DIR_NAME, id);
  480. battery->bat_desc.name = battery->name;
  481. battery->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY;
  482. if (!acpi_battery_mode(battery)) {
  483. battery->bat_desc.properties = sbs_charge_battery_props;
  484. battery->bat_desc.num_properties =
  485. ARRAY_SIZE(sbs_charge_battery_props);
  486. } else {
  487. battery->bat_desc.properties = sbs_energy_battery_props;
  488. battery->bat_desc.num_properties =
  489. ARRAY_SIZE(sbs_energy_battery_props);
  490. }
  491. battery->bat_desc.get_property = acpi_sbs_battery_get_property;
  492. battery->bat = power_supply_register(&sbs->device->dev,
  493. &battery->bat_desc, &psy_cfg);
  494. if (IS_ERR(battery->bat)) {
  495. result = PTR_ERR(battery->bat);
  496. battery->bat = NULL;
  497. goto end;
  498. }
  499. result = device_create_file(&battery->bat->dev, &alarm_attr);
  500. if (result)
  501. goto end;
  502. battery->have_sysfs_alarm = 1;
  503. end:
  504. printk(KERN_INFO PREFIX "%s [%s]: Battery Slot [%s] (battery %s)\n",
  505. ACPI_SBS_DEVICE_NAME, acpi_device_bid(sbs->device),
  506. battery->name, battery->present ? "present" : "absent");
  507. return result;
  508. }
  509. static void acpi_battery_remove(struct acpi_sbs *sbs, int id)
  510. {
  511. struct acpi_battery *battery = &sbs->battery[id];
  512. if (battery->bat) {
  513. if (battery->have_sysfs_alarm)
  514. device_remove_file(&battery->bat->dev, &alarm_attr);
  515. power_supply_unregister(battery->bat);
  516. }
  517. }
  518. static int acpi_charger_add(struct acpi_sbs *sbs)
  519. {
  520. int result;
  521. struct power_supply_config psy_cfg = { .drv_data = sbs, };
  522. result = acpi_ac_get_present(sbs);
  523. if (result)
  524. goto end;
  525. sbs->charger_exists = 1;
  526. sbs->charger = power_supply_register(&sbs->device->dev,
  527. &acpi_sbs_charger_desc, &psy_cfg);
  528. if (IS_ERR(sbs->charger)) {
  529. result = PTR_ERR(sbs->charger);
  530. sbs->charger = NULL;
  531. }
  532. printk(KERN_INFO PREFIX "%s [%s]: AC Adapter [%s] (%s)\n",
  533. ACPI_SBS_DEVICE_NAME, acpi_device_bid(sbs->device),
  534. ACPI_AC_DIR_NAME, sbs->charger_present ? "on-line" : "off-line");
  535. end:
  536. return result;
  537. }
  538. static void acpi_charger_remove(struct acpi_sbs *sbs)
  539. {
  540. if (sbs->charger)
  541. power_supply_unregister(sbs->charger);
  542. }
  543. static void acpi_sbs_callback(void *context)
  544. {
  545. int id;
  546. struct acpi_sbs *sbs = context;
  547. struct acpi_battery *bat;
  548. u8 saved_charger_state = sbs->charger_present;
  549. u8 saved_battery_state;
  550. if (sbs->charger_exists) {
  551. acpi_ac_get_present(sbs);
  552. if (sbs->charger_present != saved_charger_state)
  553. kobject_uevent(&sbs->charger->dev.kobj, KOBJ_CHANGE);
  554. }
  555. if (sbs->manager_present) {
  556. for (id = 0; id < MAX_SBS_BAT; ++id) {
  557. if (!(sbs->batteries_supported & (1 << id)))
  558. continue;
  559. bat = &sbs->battery[id];
  560. saved_battery_state = bat->present;
  561. acpi_battery_read(bat);
  562. if (saved_battery_state == bat->present)
  563. continue;
  564. kobject_uevent(&bat->bat->dev.kobj, KOBJ_CHANGE);
  565. }
  566. }
  567. }
  568. static int disable_sbs_manager(const struct dmi_system_id *d)
  569. {
  570. sbs_manager_broken = true;
  571. return 0;
  572. }
  573. static struct dmi_system_id acpi_sbs_dmi_table[] = {
  574. {
  575. .callback = disable_sbs_manager,
  576. .ident = "Apple",
  577. .matches = {
  578. DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc.")
  579. },
  580. },
  581. { },
  582. };
  583. static int acpi_sbs_add(struct acpi_device *device)
  584. {
  585. struct acpi_sbs *sbs;
  586. int result = 0;
  587. int id;
  588. dmi_check_system(acpi_sbs_dmi_table);
  589. sbs = kzalloc(sizeof(struct acpi_sbs), GFP_KERNEL);
  590. if (!sbs) {
  591. result = -ENOMEM;
  592. goto end;
  593. }
  594. mutex_init(&sbs->lock);
  595. sbs->hc = acpi_driver_data(device->parent);
  596. sbs->device = device;
  597. strcpy(acpi_device_name(device), ACPI_SBS_DEVICE_NAME);
  598. strcpy(acpi_device_class(device), ACPI_SBS_CLASS);
  599. device->driver_data = sbs;
  600. result = acpi_charger_add(sbs);
  601. if (result && result != -ENODEV)
  602. goto end;
  603. result = 0;
  604. if (!sbs_manager_broken) {
  605. result = acpi_manager_get_info(sbs);
  606. if (!result) {
  607. sbs->manager_present = 1;
  608. for (id = 0; id < MAX_SBS_BAT; ++id)
  609. if ((sbs->batteries_supported & (1 << id)))
  610. acpi_battery_add(sbs, id);
  611. }
  612. }
  613. if (!sbs->manager_present)
  614. acpi_battery_add(sbs, 0);
  615. acpi_smbus_register_callback(sbs->hc, acpi_sbs_callback, sbs);
  616. end:
  617. if (result)
  618. acpi_sbs_remove(device);
  619. return result;
  620. }
  621. static int acpi_sbs_remove(struct acpi_device *device)
  622. {
  623. struct acpi_sbs *sbs;
  624. int id;
  625. if (!device)
  626. return -EINVAL;
  627. sbs = acpi_driver_data(device);
  628. if (!sbs)
  629. return -EINVAL;
  630. mutex_lock(&sbs->lock);
  631. acpi_smbus_unregister_callback(sbs->hc);
  632. for (id = 0; id < MAX_SBS_BAT; ++id)
  633. acpi_battery_remove(sbs, id);
  634. acpi_charger_remove(sbs);
  635. mutex_unlock(&sbs->lock);
  636. mutex_destroy(&sbs->lock);
  637. kfree(sbs);
  638. return 0;
  639. }
  640. #ifdef CONFIG_PM_SLEEP
  641. static int acpi_sbs_resume(struct device *dev)
  642. {
  643. struct acpi_sbs *sbs;
  644. if (!dev)
  645. return -EINVAL;
  646. sbs = to_acpi_device(dev)->driver_data;
  647. acpi_sbs_callback(sbs);
  648. return 0;
  649. }
  650. #else
  651. #define acpi_sbs_resume NULL
  652. #endif
  653. static SIMPLE_DEV_PM_OPS(acpi_sbs_pm, NULL, acpi_sbs_resume);
  654. static struct acpi_driver acpi_sbs_driver = {
  655. .name = "sbs",
  656. .class = ACPI_SBS_CLASS,
  657. .ids = sbs_device_ids,
  658. .ops = {
  659. .add = acpi_sbs_add,
  660. .remove = acpi_sbs_remove,
  661. },
  662. .drv.pm = &acpi_sbs_pm,
  663. };
  664. static int __init acpi_sbs_init(void)
  665. {
  666. int result = 0;
  667. if (acpi_disabled)
  668. return -ENODEV;
  669. result = acpi_bus_register_driver(&acpi_sbs_driver);
  670. if (result < 0)
  671. return -ENODEV;
  672. return 0;
  673. }
  674. static void __exit acpi_sbs_exit(void)
  675. {
  676. acpi_bus_unregister_driver(&acpi_sbs_driver);
  677. return;
  678. }
  679. module_init(acpi_sbs_init);
  680. module_exit(acpi_sbs_exit);