staticint olpc_bat_get_status(struct olpc_battery_data *data, union power_supply_propval *val, uint8_t ec_byte)
{ if (data->new_proto) { if (ec_byte & (BAT_STAT_CHARGING | BAT_STAT_TRICKLE))
val->intval = POWER_SUPPLY_STATUS_CHARGING; elseif (ec_byte & BAT_STAT_DISCHARGING)
val->intval = POWER_SUPPLY_STATUS_DISCHARGING; elseif (ec_byte & BAT_STAT_FULL)
val->intval = POWER_SUPPLY_STATUS_FULL; else/* er,... */
val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
} else { /* Older EC didn't report charge/discharge bits */ if (!(ec_byte & BAT_STAT_AC)) /* No AC means discharging */
val->intval = POWER_SUPPLY_STATUS_DISCHARGING; elseif (ec_byte & BAT_STAT_FULL)
val->intval = POWER_SUPPLY_STATUS_FULL; else/* Not _necessarily_ true but EC doesn't tell all yet */
val->intval = POWER_SUPPLY_STATUS_CHARGING;
}
return 0;
}
staticint 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;
switch (ec_byte) { case 0:
val->intval = POWER_SUPPLY_HEALTH_GOOD; break;
case BAT_ERR_OVERTEMP:
val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; break;
case BAT_ERR_OVERVOLTAGE:
val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE; break;
case BAT_ERR_INFOFAIL: case BAT_ERR_OUT_OF_CONTROL: case BAT_ERR_ID_FAIL: case BAT_ERR_ACR_FAIL:
val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; break;
default: /* Eep. We don't know this failure code */
ret = -EIO;
}
return ret;
}
staticint 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) return ret;
case POWER_SUPPLY_TECHNOLOGY_LiFe: switch (mfr) { case 1: /* Gold Peak, fall through */ case 2: /* BYD */
val->intval = 2800000; break; default: return -EIO;
} break;
default: return -EIO;
}
return ret;
}
staticint olpc_bat_get_charge_now(union power_supply_propval *val)
{
uint8_t soc; union power_supply_propval full; int ret;
ret = olpc_ec_cmd(EC_BAT_SOC, NULL, 0, &soc, 1); if (ret) return ret;
ret = olpc_bat_get_charge_full_design(&full); if (ret) return ret;
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 | BAT_STAT_TRICKLE)) &&
psp != POWER_SUPPLY_PROP_PRESENT) return -ENODEV;
switch (psp) { case POWER_SUPPLY_PROP_STATUS:
ret = olpc_bat_get_status(data, val, ec_byte); if (ret) return ret; break; case POWER_SUPPLY_PROP_CHARGE_TYPE: if (ec_byte & BAT_STAT_TRICKLE)
val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE; elseif (ec_byte & BAT_STAT_CHARGING)
val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST; else
val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE; break; case POWER_SUPPLY_PROP_PRESENT:
val->intval = !!(ec_byte & (BAT_STAT_PRESENT |
BAT_STAT_TRICKLE)); 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: case POWER_SUPPLY_PROP_VOLTAGE_NOW:
ret = olpc_ec_cmd(EC_BAT_VOLTAGE, NULL, 0, (void *)&ec_word, 2); if (ret) return ret;
val->intval = ecword_to_cpu(data, ec_word) * 9760L / 32; break; case POWER_SUPPLY_PROP_CURRENT_AVG: case POWER_SUPPLY_PROP_CURRENT_NOW:
ret = olpc_ec_cmd(EC_BAT_CURRENT, NULL, 0, (void *)&ec_word, 2); if (ret) return ret;
val->intval = ecword_to_cpu(data, ec_word) * 15625L / 120; break; case POWER_SUPPLY_PROP_CAPACITY:
ret = olpc_ec_cmd(EC_BAT_SOC, NULL, 0, &ec_byte, 1); if (ret) return ret;
val->intval = ec_byte; break; case POWER_SUPPLY_PROP_CAPACITY_LEVEL: if (ec_byte & BAT_STAT_FULL)
val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL; elseif (ec_byte & BAT_STAT_LOW)
val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW; else
val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; break; case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
ret = olpc_bat_get_charge_full_design(val); if (ret) return ret; break; case POWER_SUPPLY_PROP_CHARGE_NOW:
ret = olpc_bat_get_charge_now(val); if (ret) return ret; break; case POWER_SUPPLY_PROP_TEMP:
ret = olpc_ec_cmd(EC_BAT_TEMP, NULL, 0, (void *)&ec_word, 2); if (ret) return ret;
val->intval = ecword_to_cpu(data, ec_word) * 10 / 256; break; case POWER_SUPPLY_PROP_TEMP_AMBIENT:
ret = olpc_ec_cmd(EC_AMB_TEMP, NULL, 0, (void *)&ec_word, 2); if (ret) return ret;
val->intval = (int)ecword_to_cpu(data, ec_word) * 10 / 256; break; case POWER_SUPPLY_PROP_CHARGE_COUNTER:
ret = olpc_ec_cmd(EC_BAT_ACR, NULL, 0, (void *)&ec_word, 2); if (ret) return ret;
val->intval = ecword_to_cpu(data, ec_word) * 6250 / 15; break; case POWER_SUPPLY_PROP_SERIAL_NUMBER:
ret = olpc_ec_cmd(EC_BAT_SERIAL, NULL, 0, (void *)&ser_buf, 8); if (ret) return ret;
sprintf(data->bat_serial, "%016llx", (longlong)be64_to_cpu(ser_buf));
val->strval = data->bat_serial; break; case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
ret = olpc_bat_get_voltage_max_design(val); if (ret) return ret; break; default:
ret = -EINVAL; break;
}
data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); if (!data) return -ENOMEM;
platform_set_drvdata(pdev, data);
/* See if the EC is already there and get the EC revision */
ret = olpc_ec_cmd(EC_FIRMWARE_REV, NULL, 0, &ecver, 1); if (ret) return ret;
np = of_find_compatible_node(NULL, NULL, "olpc,xo1.75-ec"); if (np) {
of_node_put(np); /* XO 1.75 */
data->new_proto = true;
data->little_endian = true;
} elseif (ecver > 0x44) { /* XO 1 or 1.5 with a new EC firmware. */
data->new_proto = true;
} elseif (ecver < 0x44) { /* * We've seen a number of EC protocol changes; this driver * requires the latest EC protocol, supported by 0x44 and above.
*/
printk(KERN_NOTICE "OLPC EC version 0x%02x too old for " "battery driver.\n", ecver); return -ENXIO;
}
ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &status, 1); if (ret) return ret;
/* Ignore the status. It doesn't actually matter */
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.