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