Lines Matching +full:sbs +full:- +full:battery
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * sbs.c - ACPI Smart Battery System Driver ($Revision: 2.0 $)
6 * Copyright (c) 2005-2007 Vladimir Lebedev <vladimir.p.lebedev@intel.com>
22 #include <acpi/battery.h>
28 #define ACPI_SBS_CLASS "sbs"
30 #define ACPI_SBS_DEVICE_NAME "Smart Battery System"
38 MODULE_DESCRIPTION("Smart Battery System ACPI interface driver");
57 struct acpi_sbs *sbs; member
90 struct acpi_battery battery[MAX_SBS_BAT]; member
100 static int acpi_battery_get_state(struct acpi_battery *battery);
105 while (log--) in battery_scale()
110 static inline int acpi_battery_vscale(struct acpi_battery *battery) in acpi_battery_vscale() argument
112 return battery_scale((battery->spec & 0x0f00) >> 8); in acpi_battery_vscale()
115 static inline int acpi_battery_ipscale(struct acpi_battery *battery) in acpi_battery_ipscale() argument
117 return battery_scale((battery->spec & 0xf000) >> 12); in acpi_battery_ipscale()
120 static inline int acpi_battery_mode(struct acpi_battery *battery) in acpi_battery_mode() argument
122 return (battery->mode & 0x8000); in acpi_battery_mode()
125 static inline int acpi_battery_scale(struct acpi_battery *battery) in acpi_battery_scale() argument
127 return (acpi_battery_mode(battery) ? 10 : 1) * in acpi_battery_scale()
128 acpi_battery_ipscale(battery); in acpi_battery_scale()
135 struct acpi_sbs *sbs = to_acpi_sbs(psy); in sbs_get_ac_property() local
138 val->intval = sbs->charger_present; in sbs_get_ac_property()
141 return -EINVAL; in sbs_get_ac_property()
146 static int acpi_battery_technology(struct acpi_battery *battery) in acpi_battery_technology() argument
148 if (!strcasecmp("NiCd", battery->device_chemistry)) in acpi_battery_technology()
150 if (!strcasecmp("NiMH", battery->device_chemistry)) in acpi_battery_technology()
152 if (!strcasecmp("LION", battery->device_chemistry)) in acpi_battery_technology()
154 if (!strcasecmp("LiP", battery->device_chemistry)) in acpi_battery_technology()
163 struct acpi_battery *battery = to_acpi_battery(psy); in acpi_sbs_battery_get_property() local
165 if ((!battery->present) && psp != POWER_SUPPLY_PROP_PRESENT) in acpi_sbs_battery_get_property()
166 return -ENODEV; in acpi_sbs_battery_get_property()
168 acpi_battery_get_state(battery); in acpi_sbs_battery_get_property()
171 if (battery->rate_now < 0) in acpi_sbs_battery_get_property()
172 val->intval = POWER_SUPPLY_STATUS_DISCHARGING; in acpi_sbs_battery_get_property()
173 else if (battery->rate_now > 0) in acpi_sbs_battery_get_property()
174 val->intval = POWER_SUPPLY_STATUS_CHARGING; in acpi_sbs_battery_get_property()
176 val->intval = POWER_SUPPLY_STATUS_FULL; in acpi_sbs_battery_get_property()
179 val->intval = battery->present; in acpi_sbs_battery_get_property()
182 val->intval = acpi_battery_technology(battery); in acpi_sbs_battery_get_property()
185 val->intval = battery->cycle_count; in acpi_sbs_battery_get_property()
188 val->intval = battery->design_voltage * in acpi_sbs_battery_get_property()
189 acpi_battery_vscale(battery) * 1000; in acpi_sbs_battery_get_property()
192 val->intval = battery->voltage_now * in acpi_sbs_battery_get_property()
193 acpi_battery_vscale(battery) * 1000; in acpi_sbs_battery_get_property()
197 val->intval = abs(battery->rate_now) * in acpi_sbs_battery_get_property()
198 acpi_battery_ipscale(battery) * 1000; in acpi_sbs_battery_get_property()
199 val->intval *= (acpi_battery_mode(battery)) ? in acpi_sbs_battery_get_property()
200 (battery->voltage_now * in acpi_sbs_battery_get_property()
201 acpi_battery_vscale(battery) / 1000) : 1; in acpi_sbs_battery_get_property()
205 val->intval = abs(battery->rate_avg) * in acpi_sbs_battery_get_property()
206 acpi_battery_ipscale(battery) * 1000; in acpi_sbs_battery_get_property()
207 val->intval *= (acpi_battery_mode(battery)) ? in acpi_sbs_battery_get_property()
208 (battery->voltage_now * in acpi_sbs_battery_get_property()
209 acpi_battery_vscale(battery) / 1000) : 1; in acpi_sbs_battery_get_property()
212 val->intval = battery->state_of_charge; in acpi_sbs_battery_get_property()
216 val->intval = battery->design_capacity * in acpi_sbs_battery_get_property()
217 acpi_battery_scale(battery) * 1000; in acpi_sbs_battery_get_property()
221 val->intval = battery->full_charge_capacity * in acpi_sbs_battery_get_property()
222 acpi_battery_scale(battery) * 1000; in acpi_sbs_battery_get_property()
226 val->intval = battery->capacity_now * in acpi_sbs_battery_get_property()
227 acpi_battery_scale(battery) * 1000; in acpi_sbs_battery_get_property()
230 val->intval = battery->temp_now - 2730; // dK -> dC in acpi_sbs_battery_get_property()
233 val->strval = battery->device_name; in acpi_sbs_battery_get_property()
236 val->strval = battery->manufacturer_name; in acpi_sbs_battery_get_property()
239 return -EINVAL; in acpi_sbs_battery_get_property()
286 .name = "sbs-charger",
293 /* --------------------------------------------------------------------------
294 Smart Battery System Management
295 -------------------------------------------------------------------------- */
298 u8 command; /* command for battery */
327 static int acpi_manager_get_info(struct acpi_sbs *sbs) in acpi_manager_get_info() argument
332 result = acpi_smbus_read(sbs->hc, SMBUS_READ_WORD, ACPI_SBS_MANAGER, in acpi_manager_get_info()
335 sbs->batteries_supported = battery_system_info & 0x000f; in acpi_manager_get_info()
339 static int acpi_battery_get_info(struct acpi_battery *battery) in acpi_battery_get_info() argument
344 result = acpi_smbus_read(battery->sbs->hc, in acpi_battery_get_info()
348 (u8 *) battery + in acpi_battery_get_info()
356 static int acpi_battery_get_state(struct acpi_battery *battery) in acpi_battery_get_state() argument
360 if (battery->update_time && in acpi_battery_get_state()
361 time_before(jiffies, battery->update_time + in acpi_battery_get_state()
365 result = acpi_smbus_read(battery->sbs->hc, in acpi_battery_get_state()
369 (u8 *)battery + in acpi_battery_get_state()
375 battery->update_time = jiffies; in acpi_battery_get_state()
379 static int acpi_battery_get_alarm(struct acpi_battery *battery) in acpi_battery_get_alarm() argument
381 return acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD, in acpi_battery_get_alarm()
383 (u8 *)&battery->alarm_capacity); in acpi_battery_get_alarm()
386 static int acpi_battery_set_alarm(struct acpi_battery *battery) in acpi_battery_set_alarm() argument
388 struct acpi_sbs *sbs = battery->sbs; in acpi_battery_set_alarm() local
389 u16 value, sel = 1 << (battery->id + 12); in acpi_battery_set_alarm()
394 if (sbs->manager_present) { in acpi_battery_set_alarm()
395 ret = acpi_smbus_read(sbs->hc, SMBUS_READ_WORD, ACPI_SBS_MANAGER, in acpi_battery_set_alarm()
402 ret = acpi_smbus_write(sbs->hc, SMBUS_WRITE_WORD, in acpi_battery_set_alarm()
409 ret = acpi_smbus_write(sbs->hc, SMBUS_WRITE_WORD, ACPI_SBS_BATTERY, in acpi_battery_set_alarm()
410 0x01, (u8 *)&battery->alarm_capacity, 2); in acpi_battery_set_alarm()
415 static int acpi_ac_get_present(struct acpi_sbs *sbs) in acpi_ac_get_present() argument
420 result = acpi_smbus_read(sbs->hc, SMBUS_READ_WORD, ACPI_SBS_CHARGER, in acpi_ac_get_present()
428 * that the implementation doesn't support an SBS charger. in acpi_ac_get_present()
432 * wrong, so ignore the SBS charger for those too. in acpi_ac_get_present()
435 return -ENODEV; in acpi_ac_get_present()
437 sbs->charger_present = (status >> 15) & 0x1; in acpi_ac_get_present()
445 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev)); in acpi_battery_alarm_show() local
446 acpi_battery_get_alarm(battery); in acpi_battery_alarm_show()
447 return sprintf(buf, "%d\n", battery->alarm_capacity * in acpi_battery_alarm_show()
448 acpi_battery_scale(battery) * 1000); in acpi_battery_alarm_show()
456 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev)); in acpi_battery_alarm_store() local
458 battery->alarm_capacity = x / in acpi_battery_alarm_store()
459 (1000 * acpi_battery_scale(battery)); in acpi_battery_alarm_store()
460 if (battery->present) in acpi_battery_alarm_store()
461 acpi_battery_set_alarm(battery); in acpi_battery_alarm_store()
471 /* --------------------------------------------------------------------------
473 -------------------------------------------------------------------------- */
474 static int acpi_battery_read(struct acpi_battery *battery) in acpi_battery_read() argument
476 int result = 0, saved_present = battery->present; in acpi_battery_read()
479 if (battery->sbs->manager_present) { in acpi_battery_read()
480 result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD, in acpi_battery_read()
483 battery->present = state & (1 << battery->id); in acpi_battery_read()
485 state |= 1 << (battery->id + 12); in acpi_battery_read()
486 acpi_smbus_write(battery->sbs->hc, SMBUS_WRITE_WORD, in acpi_battery_read()
488 } else if (battery->id == 0) in acpi_battery_read()
489 battery->present = 1; in acpi_battery_read()
491 if (result || !battery->present) in acpi_battery_read()
494 if (saved_present != battery->present) { in acpi_battery_read()
495 battery->update_time = 0; in acpi_battery_read()
496 result = acpi_battery_get_info(battery); in acpi_battery_read()
498 battery->present = 0; in acpi_battery_read()
502 result = acpi_battery_get_state(battery); in acpi_battery_read()
504 battery->present = 0; in acpi_battery_read()
508 /* Smart Battery */
509 static int acpi_battery_add(struct acpi_sbs *sbs, int id) in acpi_battery_add() argument
511 struct acpi_battery *battery = &sbs->battery[id]; in acpi_battery_add() local
512 struct power_supply_config psy_cfg = { .drv_data = battery, }; in acpi_battery_add()
515 battery->id = id; in acpi_battery_add()
516 battery->sbs = sbs; in acpi_battery_add()
517 result = acpi_battery_read(battery); in acpi_battery_add()
521 sprintf(battery->name, ACPI_BATTERY_DIR_NAME, id); in acpi_battery_add()
522 battery->bat_desc.name = battery->name; in acpi_battery_add()
523 battery->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY; in acpi_battery_add()
524 if (!acpi_battery_mode(battery)) { in acpi_battery_add()
525 battery->bat_desc.properties = sbs_charge_battery_props; in acpi_battery_add()
526 battery->bat_desc.num_properties = in acpi_battery_add()
529 battery->bat_desc.properties = sbs_energy_battery_props; in acpi_battery_add()
530 battery->bat_desc.num_properties = in acpi_battery_add()
533 battery->bat_desc.get_property = acpi_sbs_battery_get_property; in acpi_battery_add()
534 battery->bat = power_supply_register(&sbs->device->dev, in acpi_battery_add()
535 &battery->bat_desc, &psy_cfg); in acpi_battery_add()
536 if (IS_ERR(battery->bat)) { in acpi_battery_add()
537 result = PTR_ERR(battery->bat); in acpi_battery_add()
538 battery->bat = NULL; in acpi_battery_add()
542 result = device_create_file(&battery->bat->dev, &alarm_attr); in acpi_battery_add()
545 battery->have_sysfs_alarm = 1; in acpi_battery_add()
547 printk(KERN_INFO PREFIX "%s [%s]: Battery Slot [%s] (battery %s)\n", in acpi_battery_add()
548 ACPI_SBS_DEVICE_NAME, acpi_device_bid(sbs->device), in acpi_battery_add()
549 battery->name, battery->present ? "present" : "absent"); in acpi_battery_add()
553 static void acpi_battery_remove(struct acpi_sbs *sbs, int id) in acpi_battery_remove() argument
555 struct acpi_battery *battery = &sbs->battery[id]; in acpi_battery_remove() local
557 if (battery->bat) { in acpi_battery_remove()
558 if (battery->have_sysfs_alarm) in acpi_battery_remove()
559 device_remove_file(&battery->bat->dev, &alarm_attr); in acpi_battery_remove()
560 power_supply_unregister(battery->bat); in acpi_battery_remove()
564 static int acpi_charger_add(struct acpi_sbs *sbs) in acpi_charger_add() argument
567 struct power_supply_config psy_cfg = { .drv_data = sbs, }; in acpi_charger_add()
569 result = acpi_ac_get_present(sbs); in acpi_charger_add()
573 sbs->charger_exists = 1; in acpi_charger_add()
574 sbs->charger = power_supply_register(&sbs->device->dev, in acpi_charger_add()
576 if (IS_ERR(sbs->charger)) { in acpi_charger_add()
577 result = PTR_ERR(sbs->charger); in acpi_charger_add()
578 sbs->charger = NULL; in acpi_charger_add()
581 ACPI_SBS_DEVICE_NAME, acpi_device_bid(sbs->device), in acpi_charger_add()
582 ACPI_AC_DIR_NAME, sbs->charger_present ? "on-line" : "off-line"); in acpi_charger_add()
587 static void acpi_charger_remove(struct acpi_sbs *sbs) in acpi_charger_remove() argument
589 if (sbs->charger) in acpi_charger_remove()
590 power_supply_unregister(sbs->charger); in acpi_charger_remove()
596 struct acpi_sbs *sbs = context; in acpi_sbs_callback() local
598 u8 saved_charger_state = sbs->charger_present; in acpi_sbs_callback()
601 if (sbs->charger_exists) { in acpi_sbs_callback()
602 acpi_ac_get_present(sbs); in acpi_sbs_callback()
603 if (sbs->charger_present != saved_charger_state) in acpi_sbs_callback()
604 kobject_uevent(&sbs->charger->dev.kobj, KOBJ_CHANGE); in acpi_sbs_callback()
607 if (sbs->manager_present) { in acpi_sbs_callback()
609 if (!(sbs->batteries_supported & (1 << id))) in acpi_sbs_callback()
611 bat = &sbs->battery[id]; in acpi_sbs_callback()
612 saved_battery_state = bat->present; in acpi_sbs_callback()
614 if (saved_battery_state == bat->present) in acpi_sbs_callback()
616 kobject_uevent(&bat->bat->dev.kobj, KOBJ_CHANGE); in acpi_sbs_callback()
623 struct acpi_sbs *sbs; in acpi_sbs_add() local
627 sbs = kzalloc(sizeof(struct acpi_sbs), GFP_KERNEL); in acpi_sbs_add()
628 if (!sbs) { in acpi_sbs_add()
629 result = -ENOMEM; in acpi_sbs_add()
633 mutex_init(&sbs->lock); in acpi_sbs_add()
635 sbs->hc = acpi_driver_data(device->parent); in acpi_sbs_add()
636 sbs->device = device; in acpi_sbs_add()
639 device->driver_data = sbs; in acpi_sbs_add()
641 result = acpi_charger_add(sbs); in acpi_sbs_add()
642 if (result && result != -ENODEV) in acpi_sbs_add()
648 result = acpi_manager_get_info(sbs); in acpi_sbs_add()
650 sbs->manager_present = 1; in acpi_sbs_add()
652 if ((sbs->batteries_supported & (1 << id))) in acpi_sbs_add()
653 acpi_battery_add(sbs, id); in acpi_sbs_add()
657 if (!sbs->manager_present) in acpi_sbs_add()
658 acpi_battery_add(sbs, 0); in acpi_sbs_add()
660 acpi_smbus_register_callback(sbs->hc, acpi_sbs_callback, sbs); in acpi_sbs_add()
669 struct acpi_sbs *sbs; in acpi_sbs_remove() local
673 return -EINVAL; in acpi_sbs_remove()
674 sbs = acpi_driver_data(device); in acpi_sbs_remove()
675 if (!sbs) in acpi_sbs_remove()
676 return -EINVAL; in acpi_sbs_remove()
677 mutex_lock(&sbs->lock); in acpi_sbs_remove()
678 acpi_smbus_unregister_callback(sbs->hc); in acpi_sbs_remove()
680 acpi_battery_remove(sbs, id); in acpi_sbs_remove()
681 acpi_charger_remove(sbs); in acpi_sbs_remove()
682 mutex_unlock(&sbs->lock); in acpi_sbs_remove()
683 mutex_destroy(&sbs->lock); in acpi_sbs_remove()
684 kfree(sbs); in acpi_sbs_remove()
691 struct acpi_sbs *sbs; in acpi_sbs_resume() local
693 return -EINVAL; in acpi_sbs_resume()
694 sbs = to_acpi_device(dev)->driver_data; in acpi_sbs_resume()
695 acpi_sbs_callback(sbs); in acpi_sbs_resume()
705 .name = "sbs",
720 return -ENODEV; in acpi_sbs_init()
724 return -ENODEV; in acpi_sbs_init()