Commit 5f60cfd9 authored by Linus Torvalds's avatar Linus Torvalds
Browse files

Merge git://git.infradead.org/~dwmw2/battery-2.6

* git://git.infradead.org/~dwmw2/battery-2.6:
  [BATTERY] ds2760 W1 slave
  [BATTERY] One Laptop Per Child power/battery driver
  [BATTERY] Apple PMU driver
  [BATTERY] 1-Wire ds2760 chip battery driver
  [BATTERY] APM emulation driver for class batteries
  [BATTERY] pda_power platform driver
  [BATTERY] Universal power supply class (was: battery class)
parents 9f9d7632 d7ce6d1d
Linux power supply class
========================
Synopsis
~~~~~~~~
Power supply class used to represent battery, UPS, AC or DC power supply
properties to user-space.
It defines core set of attributes, which should be applicable to (almost)
every power supply out there. Attributes are available via sysfs and uevent
interfaces.
Each attribute has well defined meaning, up to unit of measure used. While
the attributes provided are believed to be universally applicable to any
power supply, specific monitoring hardware may not be able to provide them
all, so any of them may be skipped.
Power supply class is extensible, and allows to define drivers own attributes.
The core attribute set is subject to the standard Linux evolution (i.e.
if it will be found that some attribute is applicable to many power supply
types or their drivers, it can be added to the core set).
It also integrates with LED framework, for the purpose of providing
typically expected feedback of battery charging/fully charged status and
AC/USB power supply online status. (Note that specific details of the
indication (including whether to use it at all) are fully controllable by
user and/or specific machine defaults, per design principles of LED
framework).
Attributes/properties
~~~~~~~~~~~~~~~~~~~~~
Power supply class has predefined set of attributes, this eliminates code
duplication across drivers. Power supply class insist on reusing its
predefined attributes *and* their units.
So, userspace gets predictable set of attributes and their units for any
kind of power supply, and can process/present them to a user in consistent
manner. Results for different power supplies and machines are also directly
comparable.
See drivers/power/ds2760_battery.c and drivers/power/pda_power.c for the
example how to declare and handle attributes.
Units
~~~~~
Quoting include/linux/power_supply.h:
All voltages, currents, charges, energies, time and temperatures in µV,
µA, µAh, µWh, seconds and tenths of degree Celsius unless otherwise
stated. It's driver's job to convert its raw values to units in which
this class operates.
Attributes/properties detailed
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ ~ ~ ~ ~ ~ ~ Charge/Energy/Capacity - how to not confuse ~ ~ ~ ~ ~ ~ ~
~ ~
~ Because both "charge" (µAh) and "energy" (µWh) represents "capacity" ~
~ of battery, this class distinguish these terms. Don't mix them! ~
~ ~
~ CHARGE_* attributes represents capacity in µAh only. ~
~ ENERGY_* attributes represents capacity in µWh only. ~
~ CAPACITY attribute represents capacity in *percents*, from 0 to 100. ~
~ ~
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
Postfixes:
_AVG - *hardware* averaged value, use it if your hardware is really able to
report averaged values.
_NOW - momentary/instantaneous values.
STATUS - this attribute represents operating status (charging, full,
discharging (i.e. powering a load), etc.). This corresponds to
BATTERY_STATUS_* values, as defined in battery.h.
HEALTH - represents health of the battery, values corresponds to
POWER_SUPPLY_HEALTH_*, defined in battery.h.
VOLTAGE_MAX_DESIGN, VOLTAGE_MIN_DESIGN - design values for maximal and
minimal power supply voltages. Maximal/minimal means values of voltages
when battery considered "full"/"empty" at normal conditions. Yes, there is
no direct relation between voltage and battery capacity, but some dumb
batteries use voltage for very approximated calculation of capacity.
Battery driver also can use this attribute just to inform userspace
about maximal and minimal voltage thresholds of a given battery.
CHARGE_FULL_DESIGN, CHARGE_EMPTY_DESIGN - design charge values, when
battery considered full/empty.
ENERGY_FULL_DESIGN, ENERGY_EMPTY_DESIGN - same as above but for energy.
CHARGE_FULL, CHARGE_EMPTY - These attributes means "last remembered value
of charge when battery became full/empty". It also could mean "value of
charge when battery considered full/empty at given conditions (temperature,
age)". I.e. these attributes represents real thresholds, not design values.
ENERGY_FULL, ENERGY_EMPTY - same as above but for energy.
CAPACITY - capacity in percents.
CAPACITY_LEVEL - capacity level. This corresponds to
POWER_SUPPLY_CAPACITY_LEVEL_*.
TEMP - temperature of the power supply.
TEMP_AMBIENT - ambient temperature.
TIME_TO_EMPTY - seconds left for battery to be considered empty (i.e.
while battery powers a load)
TIME_TO_FULL - seconds left for battery to be considered full (i.e.
while battery is charging)
Battery <-> external power supply interaction
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Often power supplies are acting as supplies and supplicants at the same
time. Batteries are good example. So, batteries usually care if they're
externally powered or not.
For that case, power supply class implements notification mechanism for
batteries.
External power supply (AC) lists supplicants (batteries) names in
"supplied_to" struct member, and each power_supply_changed() call
issued by external power supply will notify supplicants via
external_power_changed callback.
QA
~~
Q: Where is POWER_SUPPLY_PROP_XYZ attribute?
A: If you cannot find attribute suitable for your driver needs, feel free
to add it and send patch along with your driver.
The attributes available currently are the ones currently provided by the
drivers written.
Good candidates to add in future: model/part#, cycle_time, manufacturer,
etc.
Q: I have some very specific attribute (e.g. battery color), should I add
this attribute to standard ones?
A: Most likely, no. Such attribute can be placed in the driver itself, if
it is useful. Of course, if the attribute in question applicable to
large set of batteries, provided by many drivers, and/or comes from
some general battery specification/standard, it may be a candidate to
be added to the core attribute set.
Q: Suppose, my battery monitoring chip/firmware does not provides capacity
in percents, but provides charge_{now,full,empty}. Should I calculate
percentage capacity manually, inside the driver, and register CAPACITY
attribute? The same question about time_to_empty/time_to_full.
A: Most likely, no. This class is designed to export properties which are
directly measurable by the specific hardware available.
Inferring not available properties using some heuristics or mathematical
model is not subject of work for a battery driver. Such functionality
should be factored out, and in fact, apm_power, the driver to serve
legacy APM API on top of power supply class, uses a simple heuristic of
approximating remaining battery capacity based on its charge, current,
voltage and so on. But full-fledged battery model is likely not subject
for kernel at all, as it would require floating point calculation to deal
with things like differential equations and Kalman filters. This is
better be handled by batteryd/libbattery, yet to be written.
......@@ -52,6 +52,8 @@ source "drivers/spi/Kconfig"
source "drivers/w1/Kconfig"
source "drivers/power/Kconfig"
source "drivers/hwmon/Kconfig"
source "drivers/mfd/Kconfig"
......
......@@ -61,6 +61,7 @@ obj-$(CONFIG_I2O) += message/
obj-$(CONFIG_RTC_LIB) += rtc/
obj-y += i2c/
obj-$(CONFIG_W1) += w1/
obj-$(CONFIG_POWER_SUPPLY) += power/
obj-$(CONFIG_HWMON) += hwmon/
obj-$(CONFIG_PHONE) += telephony/
obj-$(CONFIG_MD) += md/
......
menuconfig POWER_SUPPLY
tristate "Power supply class support"
help
Say Y here to enable power supply class support. This allows
power supply (batteries, AC, USB) monitoring by userspace
via sysfs and uevent (if available) and/or APM kernel interface
(if selected below).
if POWER_SUPPLY
config POWER_SUPPLY_DEBUG
bool "Power supply debug"
help
Say Y here to enable debugging messages for power supply class
and drivers.
config PDA_POWER
tristate "Generic PDA/phone power driver"
help
Say Y here to enable generic power driver for PDAs and phones with
one or two external power supplies (AC/USB) connected to main and
backup batteries, and optional builtin charger.
config APM_POWER
tristate "APM emulation for class batteries"
depends on APM_EMULATION
help
Say Y here to enable support APM status emulation using
battery class devices.
config BATTERY_DS2760
tristate "DS2760 battery driver (HP iPAQ & others)"
select W1
select W1_SLAVE_DS2760
help
Say Y here to enable support for batteries with ds2760 chip.
config BATTERY_PMU
tristate "Apple PMU battery"
depends on ADB_PMU
help
Say Y here to expose battery information on Apple machines
through the generic battery class.
config BATTERY_OLPC
tristate "One Laptop Per Child battery"
depends on X86_32 && OLPC
help
Say Y to enable support for the battery on the OLPC laptop.
endif # POWER_SUPPLY
power_supply-objs := power_supply_core.o
ifeq ($(CONFIG_SYSFS),y)
power_supply-objs += power_supply_sysfs.o
endif
ifeq ($(CONFIG_LEDS_TRIGGERS),y)
power_supply-objs += power_supply_leds.o
endif
ifeq ($(CONFIG_POWER_SUPPLY_DEBUG),y)
EXTRA_CFLAGS += -DDEBUG
endif
obj-$(CONFIG_POWER_SUPPLY) += power_supply.o
obj-$(CONFIG_PDA_POWER) += pda_power.o
obj-$(CONFIG_APM_POWER) += apm_power.o
obj-$(CONFIG_BATTERY_DS2760) += ds2760_battery.o
obj-$(CONFIG_BATTERY_PMU) += pmu_battery.o
obj-$(CONFIG_BATTERY_OLPC) += olpc_battery.o
/*
* Copyright © 2007 Anton Vorontsov <cbou@mail.ru>
* Copyright © 2007 Eugeny Boger <eugenyboger@dgap.mipt.ru>
*
* Author: Eugeny Boger <eugenyboger@dgap.mipt.ru>
*
* Use consistent with the GNU GPL is permitted,
* provided that this copyright notice is
* preserved in its entirety in all copies and derived works.
*/
#include <linux/module.h>
#include <linux/power_supply.h>
#include <linux/apm-emulation.h>
#define PSY_PROP(psy, prop, val) psy->get_property(psy, \
POWER_SUPPLY_PROP_##prop, val)
#define _MPSY_PROP(prop, val) main_battery->get_property(main_battery, \
prop, val)
#define MPSY_PROP(prop, val) _MPSY_PROP(POWER_SUPPLY_PROP_##prop, val)
static struct power_supply *main_battery;
static void find_main_battery(void)
{
struct device *dev;
struct power_supply *bat, *batm;
union power_supply_propval full;
int max_charge = 0;
main_battery = NULL;
batm = NULL;
list_for_each_entry(dev, &power_supply_class->devices, node) {
bat = dev_get_drvdata(dev);
/* If none of battery devices cantains 'use_for_apm' flag,
choice one with maximum design charge */
if (!PSY_PROP(bat, CHARGE_FULL_DESIGN, &full)) {
if (full.intval > max_charge) {
batm = bat;
max_charge = full.intval;
}
}
if (bat->use_for_apm)
main_battery = bat;
}
if (!main_battery)
main_battery = batm;
return;
}
static int calculate_time(int status)
{
union power_supply_propval charge_full, charge_empty;
union power_supply_propval charge, I;
if (MPSY_PROP(CHARGE_FULL, &charge_full)) {
/* if battery can't report this property, use design value */
if (MPSY_PROP(CHARGE_FULL_DESIGN, &charge_full))
return -1;
}
if (MPSY_PROP(CHARGE_EMPTY, &charge_empty)) {
/* if battery can't report this property, use design value */
if (MPSY_PROP(CHARGE_EMPTY_DESIGN, &charge_empty))
charge_empty.intval = 0;
}
if (MPSY_PROP(CHARGE_AVG, &charge)) {
/* if battery can't report average value, use momentary */
if (MPSY_PROP(CHARGE_NOW, &charge))
return -1;
}
if (MPSY_PROP(CURRENT_AVG, &I)) {
/* if battery can't report average value, use momentary */
if (MPSY_PROP(CURRENT_NOW, &I))
return -1;
}
if (status == POWER_SUPPLY_STATUS_CHARGING)
return ((charge.intval - charge_full.intval) * 60L) /
I.intval;
else
return -((charge.intval - charge_empty.intval) * 60L) /
I.intval;
}
static int calculate_capacity(int using_charge)
{
enum power_supply_property full_prop, empty_prop;
enum power_supply_property full_design_prop, empty_design_prop;
enum power_supply_property now_prop, avg_prop;
union power_supply_propval empty, full, cur;
int ret;
if (using_charge) {
full_prop = POWER_SUPPLY_PROP_CHARGE_FULL;
empty_prop = POWER_SUPPLY_PROP_CHARGE_EMPTY;
full_design_prop = POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN;
empty_design_prop = POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN;
now_prop = POWER_SUPPLY_PROP_CHARGE_NOW;
avg_prop = POWER_SUPPLY_PROP_CHARGE_AVG;
} else {
full_prop = POWER_SUPPLY_PROP_ENERGY_FULL;
empty_prop = POWER_SUPPLY_PROP_ENERGY_EMPTY;
full_design_prop = POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN;
empty_design_prop = POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN;
now_prop = POWER_SUPPLY_PROP_ENERGY_NOW;
avg_prop = POWER_SUPPLY_PROP_ENERGY_AVG;
}
if (_MPSY_PROP(full_prop, &full)) {
/* if battery can't report this property, use design value */
if (_MPSY_PROP(full_design_prop, &full))
return -1;
}
if (_MPSY_PROP(avg_prop, &cur)) {
/* if battery can't report average value, use momentary */
if (_MPSY_PROP(now_prop, &cur))
return -1;
}
if (_MPSY_PROP(empty_prop, &empty)) {
/* if battery can't report this property, use design value */
if (_MPSY_PROP(empty_design_prop, &empty))
empty.intval = 0;
}
if (full.intval - empty.intval)
ret = ((cur.intval - empty.intval) * 100L) /
(full.intval - empty.intval);
else
return -1;
if (ret > 100)
return 100;
else if (ret < 0)
return 0;
return ret;
}
static void apm_battery_apm_get_power_status(struct apm_power_info *info)
{
union power_supply_propval status;
union power_supply_propval capacity, time_to_full, time_to_empty;
down(&power_supply_class->sem);
find_main_battery();
if (!main_battery) {
up(&power_supply_class->sem);
return;
}
/* status */
if (MPSY_PROP(STATUS, &status))
status.intval = POWER_SUPPLY_STATUS_UNKNOWN;
/* ac line status */
if ((status.intval == POWER_SUPPLY_STATUS_CHARGING) ||
(status.intval == POWER_SUPPLY_STATUS_NOT_CHARGING) ||
(status.intval == POWER_SUPPLY_STATUS_FULL))
info->ac_line_status = APM_AC_ONLINE;
else
info->ac_line_status = APM_AC_OFFLINE;
/* battery life (i.e. capacity, in percents) */
if (MPSY_PROP(CAPACITY, &capacity) == 0) {
info->battery_life = capacity.intval;
} else {
/* try calculate using energy */
info->battery_life = calculate_capacity(0);
/* if failed try calculate using charge instead */
if (info->battery_life == -1)
info->battery_life = calculate_capacity(1);
}
/* charging status */
if (status.intval == POWER_SUPPLY_STATUS_CHARGING) {
info->battery_status = APM_BATTERY_STATUS_CHARGING;
} else {
if (info->battery_life > 50)
info->battery_status = APM_BATTERY_STATUS_HIGH;
else if (info->battery_life > 5)
info->battery_status = APM_BATTERY_STATUS_LOW;
else
info->battery_status = APM_BATTERY_STATUS_CRITICAL;
}
info->battery_flag = info->battery_status;
/* time */
info->units = APM_UNITS_MINS;
if (status.intval == POWER_SUPPLY_STATUS_CHARGING) {
if (MPSY_PROP(TIME_TO_FULL_AVG, &time_to_full)) {
if (MPSY_PROP(TIME_TO_FULL_NOW, &time_to_full))
info->time = calculate_time(status.intval);
else
info->time = time_to_full.intval / 60;
}
} else {
if (MPSY_PROP(TIME_TO_EMPTY_AVG, &time_to_empty)) {
if (MPSY_PROP(TIME_TO_EMPTY_NOW, &time_to_empty))
info->time = calculate_time(status.intval);
else
info->time = time_to_empty.intval / 60;
}
}
up(&power_supply_class->sem);
return;
}
static int __init apm_battery_init(void)
{
printk(KERN_INFO "APM Battery Driver\n");
apm_get_power_status = apm_battery_apm_get_power_status;
return 0;
}
static void __exit apm_battery_exit(void)
{
apm_get_power_status = NULL;
return;
}
module_init(apm_battery_init);
module_exit(apm_battery_exit);
MODULE_AUTHOR("Eugeny Boger <eugenyboger@dgap.mipt.ru>");
MODULE_DESCRIPTION("APM emulation driver for battery monitoring class");
MODULE_LICENSE("GPL");
/*
* Driver for batteries with DS2760 chips inside.
*
* Copyright © 2007 Anton Vorontsov
* 2004-2007 Matt Reimer
* 2004 Szabolcs Gyurko
*
* Use consistent with the GNU GPL is permitted,
* provided that this copyright notice is
* preserved in its entirety in all copies and derived works.
*
* Author: Anton Vorontsov <cbou@mail.ru>
* February 2007
*
* Matt Reimer <mreimer@vpop.net>
* April 2004, 2005, 2007
*
* Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>
* September 2004
*/
#include <linux/module.h>
#include <linux/param.h>
#include <linux/jiffies.h>
#include <linux/workqueue.h>
#include <linux/pm.h>
#include <linux/platform_device.h>
#include <linux/power_supply.h>
#include "../w1/w1.h"
#include "../w1/slaves/w1_ds2760.h"
struct ds2760_device_info {
struct device *dev;
/* DS2760 data, valid after calling ds2760_battery_read_status() */
unsigned long update_time; /* jiffies when data read */
char raw[DS2760_DATA_SIZE]; /* raw DS2760 data */
int voltage_raw; /* units of 4.88 mV */
int voltage_uV; /* units of µV */
int current_raw; /* units of 0.625 mA */
int current_uA; /* units of µA */
int accum_current_raw; /* units of 0.25 mAh */
int accum_current_uAh; /* units of µAh */
int temp_raw; /* units of 0.125 °C */
int temp_C; /* units of 0.1 °C */
int rated_capacity; /* units of µAh */
int rem_capacity; /* percentage */
int full_active_uAh; /* units of µAh */
int empty_uAh; /* units of µAh */
int life_sec; /* units of seconds */
int charge_status; /* POWER_SUPPLY_STATUS_* */
int full_counter;
struct power_supply bat;
struct device *w1_dev;
struct workqueue_struct *monitor_wqueue;
struct delayed_work monitor_work;
};
static unsigned int cache_time = 1000;
module_param(cache_time, uint, 0644);
MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
/* Some batteries have their rated capacity stored a N * 10 mAh, while
* others use an index into this table. */
static int rated_capacities[] = {
0,
920, /* Samsung */
920, /* BYD */
920, /* Lishen */
920, /* NEC */
1440, /* Samsung */
1440, /* BYD */
1440, /* Lishen */
1440, /* NEC */
2880, /* Samsung */
2880, /* BYD */
2880, /* Lishen */
2880 /* NEC */
};
/* array is level at temps 0°C, 10°C, 20°C, 30°C, 40°C
* temp is in Celsius */
static int battery_interpolate(int array[], int temp)
{
int index, dt;
if (temp <= 0)
return array[0];
if (temp >= 40)
return array[4];
index = temp / 10;
dt = temp % 10;
return array[index] + (((array[index + 1] - array[index]) * dt) / 10);
}
static int ds2760_battery_read_status(struct ds2760_device_info *di)
{
int ret, i, start, count, scale[5];
if (di->update_time && time_before(jiffies, di->update_time +
msecs_to_jiffies(cache_time)))
return 0;
/* The first time we read the entire contents of SRAM/EEPROM,
* but after that we just read the interesting bits that change. */
if (di->update_time == 0) {
start = 0;
count = DS2760_DATA_SIZE;
} else {
start = DS2760_VOLTAGE_MSB;
count = DS2760_TEMP_LSB - start + 1;
}
ret = w1_ds2760_read(di->w1_dev, di->raw + start, start, count);
if (ret != count) {
dev_warn(di->dev, "call to w1_ds2760_read failed (0x%p)\n",