sbs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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 power_supply_desc bat_desc;
  67. struct acpi_sbs *sbs;
  68. unsigned long update_time;
  69. char name[8];
  70. char manufacturer_name[ACPI_SBS_BLOCK_MAX];
  71. char device_name[ACPI_SBS_BLOCK_MAX];
  72. char device_chemistry[ACPI_SBS_BLOCK_MAX];
  73. u16 alarm_capacity;
  74. u16 full_charge_capacity;
  75. u16 design_capacity;
  76. u16 design_voltage;
  77. u16 serial_number;
  78. u16 cycle_count;
  79. u16 temp_now;
  80. u16 voltage_now;
  81. s16 rate_now;
  82. s16 rate_avg;
  83. u16 capacity_now;
  84. u16 state_of_charge;
  85. u16 state;
  86. u16 mode;
  87. u16 spec;
  88. u8 id;
  89. u8 present:1;
  90. u8 have_sysfs_alarm:1;
  91. };
  92. #define to_acpi_battery(x) power_supply_get_drvdata(x)
  93. struct acpi_sbs {
  94. struct power_supply *charger;
  95. struct acpi_device *device;
  96. struct acpi_smb_hc *hc;
  97. struct mutex lock;
  98. struct acpi_battery battery[MAX_SBS_BAT];
  99. u8 batteries_supported:4;
  100. u8 manager_present:1;
  101. u8 charger_present:1;
  102. u8 charger_exists:1;
  103. };
  104. #define to_acpi_sbs(x) power_supply_get_drvdata(x)
  105. static int acpi_sbs_remove(struct acpi_device *device);
  106. static int acpi_battery_get_state(struct acpi_battery *battery);
  107. static inline int battery_scale(int log)
  108. {
  109. int scale = 1;
  110. while (log--)
  111. scale *= 10;
  112. return scale;
  113. }
  114. static inline int acpi_battery_vscale(struct acpi_battery *battery)
  115. {
  116. return battery_scale((battery->spec & 0x0f00) >> 8);
  117. }
  118. static inline int acpi_battery_ipscale(struct acpi_battery *battery)
  119. {
  120. return battery_scale((battery->spec & 0xf000) >> 12);
  121. }
  122. static inline int acpi_battery_mode(struct acpi_battery *battery)
  123. {
  124. return (battery->mode & 0x8000);
  125. }
  126. static inline int acpi_battery_scale(struct acpi_battery *battery)
  127. {
  128. return (acpi_battery_mode(battery) ? 10 : 1) *
  129. acpi_battery_ipscale(battery);
  130. }
  131. static int sbs_get_ac_property(struct power_supply *psy,
  132. enum power_supply_property psp,
  133. union power_supply_propval *val)
  134. {
  135. struct acpi_sbs *sbs = to_acpi_sbs(psy);
  136. switch (psp) {
  137. case POWER_SUPPLY_PROP_ONLINE:
  138. val->intval = sbs->charger_present;
  139. break;
  140. default:
  141. return -EINVAL;
  142. }
  143. return 0;
  144. }
  145. static int acpi_battery_technology(struct acpi_battery *battery)
  146. {
  147. if (!strcasecmp("NiCd", battery->device_chemistry))
  148. return POWER_SUPPLY_TECHNOLOGY_NiCd;
  149. if (!strcasecmp("NiMH", battery->device_chemistry))
  150. return POWER_SUPPLY_TECHNOLOGY_NiMH;
  151. if (!strcasecmp("LION", battery->device_chemistry))
  152. return POWER_SUPPLY_TECHNOLOGY_LION;
  153. if (!strcasecmp("LiP", battery->device_chemistry))
  154. return POWER_SUPPLY_TECHNOLOGY_LIPO;
  155. return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
  156. }
  157. static int acpi_sbs_battery_get_property(struct power_supply *psy,
  158. enum power_supply_property psp,
  159. union power_supply_propval *val)
  160. {
  161. struct acpi_battery *battery = to_acpi_battery(psy);
  162. if ((!battery->present) && psp != POWER_SUPPLY_PROP_PRESENT)
  163. return -ENODEV;
  164. acpi_battery_get_state(battery);
  165. switch (psp) {
  166. case POWER_SUPPLY_PROP_STATUS:
  167. if (battery->rate_now < 0)
  168. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  169. else if (battery->rate_now > 0)
  170. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  171. else
  172. val->intval = POWER_SUPPLY_STATUS_FULL;
  173. break;
  174. case POWER_SUPPLY_PROP_PRESENT:
  175. val->intval = battery->present;
  176. break;
  177. case POWER_SUPPLY_PROP_TECHNOLOGY:
  178. val->intval = acpi_battery_technology(battery);
  179. break;
  180. case POWER_SUPPLY_PROP_CYCLE_COUNT:
  181. val->intval = battery->cycle_count;
  182. break;
  183. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  184. val->intval = battery->design_voltage *
  185. acpi_battery_vscale(battery) * 1000;
  186. break;
  187. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  188. val->intval = battery->voltage_now *
  189. acpi_battery_vscale(battery) * 1000;
  190. break;
  191. case POWER_SUPPLY_PROP_CURRENT_NOW:
  192. case POWER_SUPPLY_PROP_POWER_NOW:
  193. val->intval = abs(battery->rate_now) *
  194. acpi_battery_ipscale(battery) * 1000;
  195. val->intval *= (acpi_battery_mode(battery)) ?
  196. (battery->voltage_now *
  197. acpi_battery_vscale(battery) / 1000) : 1;
  198. break;
  199. case POWER_SUPPLY_PROP_CURRENT_AVG:
  200. case POWER_SUPPLY_PROP_POWER_AVG:
  201. val->intval = abs(battery->rate_avg) *
  202. acpi_battery_ipscale(battery) * 1000;
  203. val->intval *= (acpi_battery_mode(battery)) ?
  204. (battery->voltage_now *
  205. acpi_battery_vscale(battery) / 1000) : 1;
  206. break;
  207. case POWER_SUPPLY_PROP_CAPACITY:
  208. val->intval = battery->state_of_charge;
  209. break;
  210. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  211. case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
  212. val->intval = battery->design_capacity *
  213. acpi_battery_scale(battery) * 1000;
  214. break;
  215. case POWER_SUPPLY_PROP_CHARGE_FULL:
  216. case POWER_SUPPLY_PROP_ENERGY_FULL:
  217. val->intval = battery->full_charge_capacity *
  218. acpi_battery_scale(battery) * 1000;
  219. break;
  220. case POWER_SUPPLY_PROP_CHARGE_NOW:
  221. case POWER_SUPPLY_PROP_ENERGY_NOW:
  222. val->intval = battery->capacity_now *
  223. acpi_battery_scale(battery) * 1000;
  224. break;
  225. case POWER_SUPPLY_PROP_TEMP:
  226. val->intval = battery->temp_now - 2730; // dK -> dC
  227. break;
  228. case POWER_SUPPLY_PROP_MODEL_NAME:
  229. val->strval = battery->device_name;
  230. break;
  231. case POWER_SUPPLY_PROP_MANUFACTURER:
  232. val->strval = battery->manufacturer_name;
  233. break;
  234. default:
  235. return -EINVAL;
  236. }
  237. return 0;
  238. }
  239. static enum power_supply_property sbs_ac_props[] = {
  240. POWER_SUPPLY_PROP_ONLINE,
  241. };
  242. static enum power_supply_property sbs_charge_battery_props[] = {
  243. POWER_SUPPLY_PROP_STATUS,
  244. POWER_SUPPLY_PROP_PRESENT,
  245. POWER_SUPPLY_PROP_TECHNOLOGY,
  246. POWER_SUPPLY_PROP_CYCLE_COUNT,
  247. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  248. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  249. POWER_SUPPLY_PROP_CURRENT_NOW,
  250. POWER_SUPPLY_PROP_CURRENT_AVG,
  251. POWER_SUPPLY_PROP_CAPACITY,
  252. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  253. POWER_SUPPLY_PROP_CHARGE_FULL,
  254. POWER_SUPPLY_PROP_CHARGE_NOW,
  255. POWER_SUPPLY_PROP_TEMP,
  256. POWER_SUPPLY_PROP_MODEL_NAME,
  257. POWER_SUPPLY_PROP_MANUFACTURER,
  258. };
  259. static enum power_supply_property sbs_energy_battery_props[] = {
  260. POWER_SUPPLY_PROP_STATUS,
  261. POWER_SUPPLY_PROP_PRESENT,
  262. POWER_SUPPLY_PROP_TECHNOLOGY,
  263. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  264. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  265. POWER_SUPPLY_PROP_CURRENT_NOW,
  266. POWER_SUPPLY_PROP_CURRENT_AVG,
  267. POWER_SUPPLY_PROP_POWER_NOW,
  268. POWER_SUPPLY_PROP_POWER_AVG,
  269. POWER_SUPPLY_PROP_CAPACITY,
  270. POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
  271. POWER_SUPPLY_PROP_ENERGY_FULL,
  272. POWER_SUPPLY_PROP_ENERGY_NOW,
  273. POWER_SUPPLY_PROP_TEMP,
  274. POWER_SUPPLY_PROP_MODEL_NAME,
  275. POWER_SUPPLY_PROP_MANUFACTURER,
  276. };
  277. static const struct power_supply_desc acpi_sbs_charger_desc = {
  278. .name = "sbs-charger",
  279. .type = POWER_SUPPLY_TYPE_MAINS,
  280. .properties = sbs_ac_props,
  281. .num_properties = ARRAY_SIZE(sbs_ac_props),
  282. .get_property = sbs_get_ac_property,
  283. };
  284. /* --------------------------------------------------------------------------
  285. Smart Battery System Management
  286. -------------------------------------------------------------------------- */
  287. struct acpi_battery_reader {
  288. u8 command; /* command for battery */
  289. u8 mode; /* word or block? */
  290. size_t offset; /* offset inside struct acpi_sbs_battery */
  291. };
  292. static struct acpi_battery_reader info_readers[] = {
  293. {0x01, SMBUS_READ_WORD, offsetof(struct acpi_battery, alarm_capacity)},
  294. {0x03, SMBUS_READ_WORD, offsetof(struct acpi_battery, mode)},
  295. {0x10, SMBUS_READ_WORD, offsetof(struct acpi_battery, full_charge_capacity)},
  296. {0x17, SMBUS_READ_WORD, offsetof(struct acpi_battery, cycle_count)},
  297. {0x18, SMBUS_READ_WORD, offsetof(struct acpi_battery, design_capacity)},
  298. {0x19, SMBUS_READ_WORD, offsetof(struct acpi_battery, design_voltage)},
  299. {0x1a, SMBUS_READ_WORD, offsetof(struct acpi_battery, spec)},
  300. {0x1c, SMBUS_READ_WORD, offsetof(struct acpi_battery, serial_number)},
  301. {0x20, SMBUS_READ_BLOCK, offsetof(struct acpi_battery, manufacturer_name)},
  302. {0x21, SMBUS_READ_BLOCK, offsetof(struct acpi_battery, device_name)},
  303. {0x22, SMBUS_READ_BLOCK, offsetof(struct acpi_battery, device_chemistry)},
  304. };
  305. static struct acpi_battery_reader state_readers[] = {
  306. {0x08, SMBUS_READ_WORD, offsetof(struct acpi_battery, temp_now)},
  307. {0x09, SMBUS_READ_WORD, offsetof(struct acpi_battery, voltage_now)},
  308. {0x0a, SMBUS_READ_WORD, offsetof(struct acpi_battery, rate_now)},
  309. {0x0b, SMBUS_READ_WORD, offsetof(struct acpi_battery, rate_avg)},
  310. {0x0f, SMBUS_READ_WORD, offsetof(struct acpi_battery, capacity_now)},
  311. {0x0e, SMBUS_READ_WORD, offsetof(struct acpi_battery, state_of_charge)},
  312. {0x16, SMBUS_READ_WORD, offsetof(struct acpi_battery, state)},
  313. };
  314. static int acpi_manager_get_info(struct acpi_sbs *sbs)
  315. {
  316. int result = 0;
  317. u16 battery_system_info;
  318. result = acpi_smbus_read(sbs->hc, SMBUS_READ_WORD, ACPI_SBS_MANAGER,
  319. 0x04, (u8 *)&battery_system_info);
  320. if (!result)
  321. sbs->batteries_supported = battery_system_info & 0x000f;
  322. return result;
  323. }
  324. static int acpi_battery_get_info(struct acpi_battery *battery)
  325. {
  326. int i, result = 0;
  327. for (i = 0; i < ARRAY_SIZE(info_readers); ++i) {
  328. result = acpi_smbus_read(battery->sbs->hc,
  329. info_readers[i].mode,
  330. ACPI_SBS_BATTERY,
  331. info_readers[i].command,
  332. (u8 *) battery +
  333. info_readers[i].offset);
  334. if (result)
  335. break;
  336. }
  337. return result;
  338. }
  339. static int acpi_battery_get_state(struct acpi_battery *battery)
  340. {
  341. int i, result = 0;
  342. if (battery->update_time &&
  343. time_before(jiffies, battery->update_time +
  344. msecs_to_jiffies(cache_time)))
  345. return 0;
  346. for (i = 0; i < ARRAY_SIZE(state_readers); ++i) {
  347. result = acpi_smbus_read(battery->sbs->hc,
  348. state_readers[i].mode,
  349. ACPI_SBS_BATTERY,
  350. state_readers[i].command,
  351. (u8 *)battery +
  352. state_readers[i].offset);
  353. if (result)
  354. goto end;
  355. }
  356. end:
  357. battery->update_time = jiffies;
  358. return result;
  359. }
  360. static int acpi_battery_get_alarm(struct acpi_battery *battery)
  361. {
  362. return acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD,
  363. ACPI_SBS_BATTERY, 0x01,
  364. (u8 *)&battery->alarm_capacity);
  365. }
  366. static int acpi_battery_set_alarm(struct acpi_battery *battery)
  367. {
  368. struct acpi_sbs *sbs = battery->sbs;
  369. u16 value, sel = 1 << (battery->id + 12);
  370. int ret;
  371. if (sbs->manager_present) {
  372. ret = acpi_smbus_read(sbs->hc, SMBUS_READ_WORD, ACPI_SBS_MANAGER,
  373. 0x01, (u8 *)&value);
  374. if (ret)
  375. goto end;
  376. if ((value & 0xf000) != sel) {
  377. value &= 0x0fff;
  378. value |= sel;
  379. ret = acpi_smbus_write(sbs->hc, SMBUS_WRITE_WORD,
  380. ACPI_SBS_MANAGER,
  381. 0x01, (u8 *)&value, 2);
  382. if (ret)
  383. goto end;
  384. }
  385. }
  386. ret = acpi_smbus_write(sbs->hc, SMBUS_WRITE_WORD, ACPI_SBS_BATTERY,
  387. 0x01, (u8 *)&battery->alarm_capacity, 2);
  388. end:
  389. return ret;
  390. }
  391. static int acpi_ac_get_present(struct acpi_sbs *sbs)
  392. {
  393. int result;
  394. u16 status;
  395. result = acpi_smbus_read(sbs->hc, SMBUS_READ_WORD, ACPI_SBS_CHARGER,
  396. 0x13, (u8 *) & status);
  397. if (result)
  398. return result;
  399. /*
  400. * The spec requires that bit 4 always be 1. If it's not set, assume
  401. * that the implementation doesn't support an SBS charger
  402. */
  403. if (!((status >> 4) & 0x1))
  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);