Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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