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