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