Commit b2bd8a3b authored by Andres Salomon's avatar Andres Salomon Committed by Anton Vorontsov

power_supply: cleanup of the OLPC battery driver

Move portions of the massive switch statement into functions.  The layout of
this thing has already caused one bug (a break in the wrong place), it needed
to shrink.
Signed-off-by: default avatarAndres Salomon <dilinger@debian.org>
Cc: David Woodhouse <dwmw2@infradead.org>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarAnton Vorontsov <cbouatmailru@gmail.com>
parent d7eb9e36
......@@ -86,33 +86,8 @@ static struct power_supply olpc_ac = {
static char bat_serial[17]; /* Ick */
/*********************************************************************
* Battery properties
*********************************************************************/
static int olpc_bat_get_property(struct power_supply *psy,
enum power_supply_property psp,
union power_supply_propval *val)
static int olpc_bat_get_status(union power_supply_propval *val, uint8_t ec_byte)
{
int ret = 0;
int16_t ec_word;
uint8_t ec_byte;
uint64_t ser_buf;
ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &ec_byte, 1);
if (ret)
return ret;
/* Theoretically there's a race here -- the battery could be
removed immediately after we check whether it's present, and
then we query for some other property of the now-absent battery.
It doesn't matter though -- the EC will return the last-known
information, and it's as if we just ran that _little_ bit faster
and managed to read it out before the battery went away. */
if (!(ec_byte & BAT_STAT_PRESENT) && psp != POWER_SUPPLY_PROP_PRESENT)
return -ENODEV;
switch (psp) {
case POWER_SUPPLY_PROP_STATUS:
if (olpc_platform_info.ecver > 0x44) {
if (ec_byte & BAT_STAT_CHARGING)
val->intval = POWER_SUPPLY_STATUS_CHARGING;
......@@ -130,16 +105,16 @@ static int olpc_bat_get_property(struct power_supply *psy,
val->intval = POWER_SUPPLY_STATUS_FULL;
else /* Not _necessarily_ true but EC doesn't tell all yet */
val->intval = POWER_SUPPLY_STATUS_CHARGING;
break;
}
case POWER_SUPPLY_PROP_PRESENT:
val->intval = !!(ec_byte & BAT_STAT_PRESENT);
break;
case POWER_SUPPLY_PROP_HEALTH:
if (ec_byte & BAT_STAT_DESTROY)
val->intval = POWER_SUPPLY_HEALTH_DEAD;
else {
return 0;
}
static int olpc_bat_get_health(union power_supply_propval *val)
{
uint8_t ec_byte;
int ret;
ret = olpc_ec_cmd(EC_BAT_ERRCODE, NULL, 0, &ec_byte, 1);
if (ret)
return ret;
......@@ -166,12 +141,17 @@ static int olpc_bat_get_property(struct power_supply *psy,
default:
/* Eep. We don't know this failure code */
return -EIO;
}
ret = -EIO;
}
break;
case POWER_SUPPLY_PROP_MANUFACTURER:
return ret;
}
static int olpc_bat_get_mfr(union power_supply_propval *val)
{
uint8_t ec_byte;
int ret;
ec_byte = BAT_ADDR_MFR_TYPE;
ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1);
if (ret)
......@@ -188,8 +168,15 @@ static int olpc_bat_get_property(struct power_supply *psy,
val->strval = "Unknown";
break;
}
break;
case POWER_SUPPLY_PROP_TECHNOLOGY:
return ret;
}
static int olpc_bat_get_tech(union power_supply_propval *val)
{
uint8_t ec_byte;
int ret;
ec_byte = BAT_ADDR_MFR_TYPE;
ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1);
if (ret)
......@@ -206,6 +193,64 @@ static int olpc_bat_get_property(struct power_supply *psy,
val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
break;
}
return ret;
}
/*********************************************************************
* Battery properties
*********************************************************************/
static int olpc_bat_get_property(struct power_supply *psy,
enum power_supply_property psp,
union power_supply_propval *val)
{
int ret = 0;
int16_t ec_word;
uint8_t ec_byte;
uint64_t ser_buf;
ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &ec_byte, 1);
if (ret)
return ret;
/* Theoretically there's a race here -- the battery could be
removed immediately after we check whether it's present, and
then we query for some other property of the now-absent battery.
It doesn't matter though -- the EC will return the last-known
information, and it's as if we just ran that _little_ bit faster
and managed to read it out before the battery went away. */
if (!(ec_byte & BAT_STAT_PRESENT) && psp != POWER_SUPPLY_PROP_PRESENT)
return -ENODEV;
switch (psp) {
case POWER_SUPPLY_PROP_STATUS:
ret = olpc_bat_get_status(val, ec_byte);
if (ret)
return ret;
break;
case POWER_SUPPLY_PROP_PRESENT:
val->intval = !!(ec_byte & BAT_STAT_PRESENT);
break;
case POWER_SUPPLY_PROP_HEALTH:
if (ec_byte & BAT_STAT_DESTROY)
val->intval = POWER_SUPPLY_HEALTH_DEAD;
else {
ret = olpc_bat_get_health(val);
if (ret)
return ret;
}
break;
case POWER_SUPPLY_PROP_MANUFACTURER:
ret = olpc_bat_get_mfr(val);
if (ret)
return ret;
break;
case POWER_SUPPLY_PROP_TECHNOLOGY:
ret = olpc_bat_get_tech(val);
if (ret)
return ret;
break;
case POWER_SUPPLY_PROP_VOLTAGE_AVG:
ret = olpc_ec_cmd(EC_BAT_VOLTAGE, NULL, 0, (void *)&ec_word, 2);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment