sbs.c 20 KB

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