power_supply: Introduce power supply charging driver
Subsystems (2)
v11 cover power_supply: Introduce power supply charging driver
v1: introduced feature as a framework within power supply class driver with
separate files for battid framework and charging framework
v2: fixed review comments, moved macros and inline functions to power_supply.h
v3: moved the feature as a separate driver, combined battid framework and
charging framework inside the power supply charging driver. Moved charger specific properties to power_supply_charger.h and plugged the driver with power supply subsystem using power_supply_notifier introduced in my previous patch. Also a sample charger chip driver (bq24261) patch added to give more idea on the psy charging driver usage
v4: Fixed review comments, no major design changes.
v5: Fixed makefile inconsistencies, removed unused pdata callbacks
v6: Fixed nested loops, commenting style
v7: added kerneldocs for structs and minor fixes
v8: used msecs_to_jiffies instead of HZ directly, modified Kconfig help text
for POWER_SUPPLY_CHARGING_ALGO_PSE
v9: Removed string lookups, static cable initialization
v10: Fixed bug in algorithm lookup
v11: Few variable name changes for better readability
Jenny TC (4):
power_supply: Add inlmt,iterm, min/max temp props
power_supply: Introduce generic psy charging driver
power_supply: Introduce PSE compliant algorithm
power_supply: bq24261 charger driver
Documentation/power/power_supply_charger.txt | 350 +++++++ Documentation/power/power_supply_class.txt | 6 + drivers/power/Kconfig | 33 + drivers/power/Makefile | 3 + drivers/power/bq24261_charger.c | 1348 ++++++++++++++++++++++++++ drivers/power/charging_algo_pse.c | 202 ++++ drivers/power/power_supply_charger.c | 1016 +++++++++++++++++++ drivers/power/power_supply_charger.h | 226 +++++ drivers/power/power_supply_core.c | 3 + drivers/power/power_supply_sysfs.c | 4 + include/linux/power/bq24261-charger.h | 25 + include/linux/power/power_supply_charger.h | 372 +++++++ include/linux/power_supply.h | 165 ++++ 13 files changed, 3753 insertions(+) create mode 100644 Documentation/power/power_supply_charger.txt create mode 100644 drivers/power/bq24261_charger.c create mode 100644 drivers/power/charging_algo_pse.c create mode 100644 drivers/power/power_supply_charger.c create mode 100644 drivers/power/power_supply_charger.h create mode 100644 include/linux/power/bq24261-charger.h create mode 100644 include/linux/power/power_supply_charger.h
Signature · 1 line
1.7.9.5
PATCH v11 1/4 power_supply: Add inlmt,iterm, min/max temp props
Add new power supply properties for input current, charge termination current, min and max temperature
POWER_SUPPLY_PROP_TEMP_MIN - minimum operatable temperature POWER_SUPPLY_PROP_TEMP_MAX - maximum operatable temperature
POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT - input current limit programmed by charger. Indicates the input current for a charging source.
POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT - Charge termination current used to detect the end of charge condition
--- Documentation/power/power_supply_class.txt | 6 ++++++ drivers/power/power_supply_sysfs.c | 4 ++++ include/linux/power_supply.h | 4 ++++ 3 files changed, 14 insertions(+)
Documentation/power/power_supply_class.txt +6 -0
@@ -118,6 +118,10 @@ relative, time-based measurements. CONSTANT_CHARGE_CURRENT - constant charge current programmed by charger. CONSTANT_CHARGE_CURRENT_MAX - maximum charge current supported by the power supply object. +INPUT_CURRENT_LIMIT - input current limit programmed by charger. Indicates +the current drawn from a charging source. +CHARGE_TERM_CURRENT - Charge termination current used to detect the end of charge +condition. CONSTANT_CHARGE_VOLTAGE - constant charge voltage programmed by charger. CONSTANT_CHARGE_VOLTAGE_MAX - maximum charge voltage supported by the
@@ -140,6 +144,8 @@ TEMP_ALERT_MAX - maximum battery temperature alert. TEMP_AMBIENT - ambient temperature. TEMP_AMBIENT_ALERT_MIN - minimum ambient temperature alert. TEMP_AMBIENT_ALERT_MAX - maximum ambient temperature alert. +TEMP_MIN - minimum operatable temperature +TEMP_MAX - maximum operatable temperature TIME_TO_EMPTY - seconds left for battery to be considered empty (i.e. while battery powers a load)
drivers/power/power_supply_sysfs.c +4 -0
@@ -167,6 +167,7 @@ static struct device_attribute power_supply_attrs[] = {POWER_SUPPLY_ATTR (constant_charge_voltage_max),POWER_SUPPLY_ATTR (charge_control_limit),POWER_SUPPLY_ATTR (charge_control_limit_max), +POWER_SUPPLY_ATTR (input_current_limit),POWER_SUPPLY_ATTR (energy_full_design),POWER_SUPPLY_ATTR (energy_empty_design), POWER_SUPPLY_ATTR(energy_full),
@@ -178,6 +179,8 @@ static struct device_attribute power_supply_attrs[] = {POWER_SUPPLY_ATTR (capacity_alert_max),POWER_SUPPLY_ATTR (capacity_level),POWER_SUPPLY_ATTR (temp), +POWER_SUPPLY_ATTR (temp_max), +POWER_SUPPLY_ATTR (temp_min),POWER_SUPPLY_ATTR (temp_alert_min),POWER_SUPPLY_ATTR (temp_alert_max), POWER_SUPPLY_ATTR(temp_ambient),
@@ -189,6 +192,7 @@ static struct device_attribute power_supply_attrs[] = {POWER_SUPPLY_ATTR (time_to_full_avg),POWER_SUPPLY_ATTR (type),POWER_SUPPLY_ATTR (scope), +POWER_SUPPLY_ATTR (charge_term_current),/* Properties of type `const char *' */ POWER_SUPPLY_ATTR (model_name), POWER_SUPPLY_ATTR(manufacturer),
include/linux/power_supply.h +4 -0
@@ -120,6 +120,7 @@ enum power_supply_property {POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX ,POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT ,POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX , +POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT ,POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN ,POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN , POWER_SUPPLY_PROP_ENERGY_FULL,
@@ -131,6 +132,8 @@ enum power_supply_property {POWER_SUPPLY_PROP_CAPACITY_ALERT_MAX ,/* in percents! */ POWER_SUPPLY_PROP_CAPACITY_LEVEL ,POWER_SUPPLY_PROP_TEMP , +POWER_SUPPLY_PROP_TEMP_MAX , +POWER_SUPPLY_PROP_TEMP_MIN ,POWER_SUPPLY_PROP_TEMP_ALERT_MIN ,POWER_SUPPLY_PROP_TEMP_ALERT_MAX , POWER_SUPPLY_PROP_TEMP_AMBIENT,
@@ -142,6 +145,7 @@ enum power_supply_property {POWER_SUPPLY_PROP_TIME_TO_FULL_AVG ,POWER_SUPPLY_PROP_TYPE ,/* use power_supply.type instead */ POWER_SUPPLY_PROP_SCOPE , +POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT ,/* Properties of type `const char *' */ POWER_SUPPLY_PROP_MODEL_NAME , POWER_SUPPLY_PROP_MANUFACTURER,
Signature · 1 line
1.7.9.5
Re: [PATCH 1/4] power_supply: Add inlmt,iterm, min/max temp props
Quoted 6 lines · Jenny TC
Add new power supply properties for input current, charge termination current, min and max temperature
POWER_SUPPLY_PROP_TEMP_MIN- minimum operatable temperaturePOWER_SUPPLY_PROP_TEMP_MAX- maximum operatable temperature
Quoted 7 lines
POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT- input current limit programmed by charger. Indicates the input current for a charging source.
POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT- Charge termination current used to detect the end of charge conditionSigned-off-by Jenny TC <[email protected]>
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Sebastian Reichel
maintainerRe: [PATCH 1/4] power_supply: Add inlmt,iterm, min/max temp props
In reply to Jenny TCHi Jenny,
Quoted 6 lines · Jenny TC
Add new power supply properties for input current, charge termination current, min and max temperature
POWER_SUPPLY_PROP_TEMP_MIN- minimum operatable temperaturePOWER_SUPPLY_PROP_TEMP_MAX- maximum operatable temperature
Quoted 7 lines
POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT- input current limit programmed by charger. Indicates the input current for a charging source.
POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT- Charge termination current used to detect the end of charge conditionSigned-off-by Jenny TC <[email protected]>
I've added this to my queue, which will go into battery.git's for-next, once I have access to it:
You can skip this patch in further iterations of this patchset.
-- Sebastian
PATCH v11 2/4 power_supply: Introduce generic psy charging driver
In reply to Jenny TCThe Power Supply charging driver connects multiple subsystems
to do charging in a generic way. The subsystems involves power_supply, thermal and battery communication subsystems (1wire). With this the charging is handled in a generic way.
The driver makes use of different new features - Battery Identification interfaces, pluggable charging algorithms, charger cable arbitrations etc. The patch also introduces generic interface for charger cable notifications. Charger cable events and capabilities can be notified using the generic power_supply_notifier chain.
Overall this driver removes the charging logic out of the charger chip driver and the charger chip driver can just listen to the request from the power supply charging driver to set the charger properties. This can be implemented by exposing get_property and set property callbacks.
--- Documentation/power/power_supply_charger.txt | 350 +++++++++ drivers/power/Kconfig | 8 + drivers/power/Makefile | 1 + drivers/power/power_supply_charger.c | 1016 ++++++++++++++++++++++++++ drivers/power/power_supply_charger.h | 226 ++++++ drivers/power/power_supply_core.c | 3 + include/linux/power/power_supply_charger.h | 307 ++++++++ include/linux/power_supply.h | 161 ++++ 8 files changed, 2072 insertions(+) create mode 100644 Documentation/power/power_supply_charger.txt create mode 100644 drivers/power/power_supply_charger.c create mode 100644 drivers/power/power_supply_charger.h create mode 100644 include/linux/power/power_supply_charger.h
Documentation/power/power_supply_charger.txt new file +350 -0
@@ -0,0 +1,350 @@ +1. Introduction +=============== + +The Power Supply charging driver connects multiple subsystems +to do charging in a generic way. The subsystems involves power_supply, +thermal and battery communication subsystems (1wire). With this the charging is +handled in a generic way by plugging the driver to power supply subsystem. + +The driver introduces different new features - Battery Identification +interfaces, pluggable charging algorithms, charger cable arbitrations etc. + +In existing driver implementations the charging is done based on the static +battery characteristics. This is done at the boot time by passing the battery +properties (max_voltage, capacity) etc. as a platform data to the +charger/battery driver. But new generation high volt batteries needs to be +identified dynamically to do charging in a safe manner. The batteries are +coming with different communication protocols. It become necessary to +communicate with battery and identify it's charging profiles before setup +charging. + +Also the charging algorithms can vary based on the battery characteristics +and the platform characteristics. To handle charging in a generic way it's +necessary to support pluggable charging algorithms. Power Supply Charging +driver selects algorithms based on the type of battery charging profile. +This is a simple binding and can be improved later. This may be improved to +select the algorithms based on the platform requirements. Also we can extend +this driver to plug algorithms from the user space. + +The driver also introduces the charger cable arbitration. A charger may +supports multiple cables, but it may not be able to charge with multiple +cables at a time (USB/AC/Wireless etc.). The arbitration logic inside the +driver selects the cable based on it's capabilities and the maximum +charge current the platform can support. + +Also the driver exposes features to control charging on different platform +states. One such feature is thermal. The driver handles the thermal +throttling requests for charging and control charging based on the thermal +subsystem requirements. + +Overall this driver removes the charging logic out of the charger chip driver +and the charger chip driver can just listen to the request from the power +supply charging driver to set the charger properties. This can be implemented +by exposing get_property and set property callbacks. + +2. Reading Battery charging profile +=================================== + +Power Supply charging driver expose APIs to retrieve battery profile of a +battery. The battery profile can be read by battery identification driver which +may be 1wire/I2C/SFI driver. Battery identification driver can register the +battery profile with the power supply charging driver using the API +psy_battery_prop_changed(). The driver also exposes API +psy_get_battery_prop() to retrieve the battery profile which can be +used by power supply drivers to setup the charging. Also drivers +can register for battery removal/insertion notifications using +power_supply_reg_notifier() + +3. Use Charging Driver to setup charging +=========================================== + +* Register the driver with the power_supply class +* Register the driver with the power supply charging driver +* Expose set_property and get_property functions so that charging + framework can control the charging + +3.1 Registering charger chip driver with power supply charging driver +================================================================ + +struct power_supply_class psy_usb; +struct power_supply_charger psyc_usb; + +/* register with power supply subsystem */ +psy_usb.name = DEV_NAME; +psy_usb.type = POWER_SUPPLY_TYPE_USB; + +/* pointer to power supply property structure */ +psy_usb.properties = &psy_usb_props; +psy_usb.num_properties = ARRAY_SIZE(psy_usb_props); + +/* pointer to power supply get property function */ +psy_usb.get_property = psy_usb_get_property; + +/* pointer to power supply set property function */ +psy_usb.set_property = psy_usb_set_property; + +/* pointer to the supplied_to argument to indicate the batteries + to which this charger is supplying power */ +psy_usb.supplied_to = supplied_to; +psy_usb.num_supplicants = num_supplicants; + +/* register with power_supply subsystem */ +power_supply_register(device, &psy_usb); + +/* Register with power supply charging driver */ + +/* define supported cable types for this driver */ +psyc_usb.supported_cables = POWER_SUPPLY_CHARGER_TYPE_USB; + +/* pointer to power supply charger get property function */ +psyc_usb.get_property = bq24261_usb_psyc_get_property; + +/* pointer to power supply charger set property function */ +psyc_usb.set_property = bq24261_usb_psyc_set_property; + +/* pointer to throttle states */ +psyc_usb.throttle_states = chip->pdata->throttle_states; + +/* Number of throttle states */ +psyc_usb.num_throttle_states = chip->pdata->num_throttle_states; + +/*register with power supply charging */ +power_supply_register_charger(&chip->psy_usb, &chip->psyc_usb); + +3.2 Properties exposed to power supply class driver +================================================== +* POWER_SUPPLY_PROP_ONLINE + * Read access using get_property_function + * Returns the online property set using the set_property + function + * Write access using set_property function + * Expose the value through get_property_function. +* POWER_SUPPLY_PROP_PRESENT + * Read access using get_property_function + * Returns the present property set using the set_property + function + * Write access using set_property function + * Expose the value through get_property_function. +* POWER_SUPPLY_PROP_HEALTH + * Read access using get_property_function + * Returns charger health +* POWER_SUPPLY_PROP_TYPE + * Read access using power_supply structure + * Returns charger type +* POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT + * Read access using get property + * Returns the present charge control limit + * Write access using set property + * Set charge control limit. + * Action: Driver is not expected to take any actions on this. + Instead need to expose this on the read interface. + Charging framework process this and notifies the + charging parameters accordingly. +* POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX + * Read access using get property + * Returns the maximum charge control limit +* POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT + * Read access using get property + * Returns the input current limit passed by set_property + function + * Default value : 0 + * Write access using set property + * Set input current limit + * Action : Write h/w register to set the input current limit + based on the value +* POWER_SUPPLY_PROP_CHARGE_CURRENT + * Read access using get property + * Returns charge current passed by set_property function + * Default value : 0 + * Write access using set property + * Set charge current + * Action : Write h/w register to set the charge current based + on the value +* POWER_SUPPLY_PROP_CHARGE_VOLTAGE + * Read access using get property + * Returns charge voltage passed by set_property function + * Default value: 0 + * Write access using set property + * Set charge voltage + * Action : Write h/w register to set the charge voltage based + on the value +* POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT + * Read access using get property + * Returns charge termination current passed by set_property + function + * Default value : 0 + * Write access using set property + * Set charge termination current + * Action : Write h/w register to set the charge termination + current + based on the value +* POWER_SUPPLY_PROP_TEMP_MIN + * Read access using get property + * Returns minimum charging temperature passed by set_property + * Default value : Platform dependant + * Write access using set_property + * Set Minimum charging temperature + * Action : Write h/w register to set the minimum charging + temperature based on the value +* POWER_SUPPLY_PROP_TEMP_MAX + * Write access using set_property + * Set Maximum charging temperature + * Action : Write h/w register to set the maximum charging + temperature based on the value + * Read access using get property + * Returns maximum charging temperature passed by set_property + * Default value : Platform dependent +* POWER_SUPPLY_PROP_MAX_CHARGE_CURRENT + * Read access using get property + * Returns maximum charging current passed by set_property + * Default value : Platform dependent + * Write access using set_property + * Set Maximum charging current + * Action: Configure safety charging registers if any. If no h/w + actions expected, report the value on the + get_property interface. +* POWER_SUPPLY_PROP_MAX_CHARGE_VOLTAGE + * Read access using get property + * Returns maximum charging current passed by set_property + * Default value : Platform dependent + * Write access using set_property + * Set Maximum charging voltage + * Action: Configure safety charging registers if any. If not, + no actions expected for this. + +3.3 Properties exposed to power supply charging driver +===================================================== + +* POWER_SUPPLY_CHARGER_PROP_ENABLE_CHARGING + * Write access using set_property + * Enable/Disable charging. Charger supplies power to platform, + but charging is disabled + * Action: Configure charger chip registers to enable/disable + charging. Writing 0, disables charging, writing 1 enables + charging +* POWER_SUPPLY_CHARGER_PROP_ENABLE_CHARGER + * Write access using set_property + * Enable/Disable charger. Charger doesn't supply power to + platform or battery and battery start to discharge. + * Action: Configure charger chip registers to enable/disable + charger. Writing 0 disables charger, writing 1 enables + charger +* POWER_SUPPLY_CHARGER_PROP_CABLE_TYPE + * Write access using set_property + * Set charger cable type. + * Action: Report power supply type based on cable type +* POWER_SUPPLY_CHARGER_PROP_RESET_WDT + * Write access using set_property + * Reset charger Watch Dog Timer + * Action: Reset charger Watch Dog Timer. +* POWER_SUPPLY_CHARGER_PROP_PRIORITY + * Read access using get property + * Expose charger driver/chips priority if platform has multiple + charger chip/drivers. + +3.4 Throttling data configuration +============================= +Power supply charging driver can take actions for Thermal throttling requests. +Power supply core sends event PSY_EVENT_THROTTLE on power_supply_notifier +chain. Power supply charging driver handle this event and takes throttling +actions as in the throttling configuration structure. The throttling +configuration structure has the following format: + +struct psy_throttle_state { + enum psy_throttle_action throttle_action; + unsigned throttle_val; +}; + +enum psy_throttle_action { + /* Disable charger, charger doesn't supply VSYS */ + PSY_THROTTLE_DISABLE_CHARGER = 0, + + /* Disable charging, charger would supply VSYS */ + PSY_THROTTLE_DISABLE_CHARGING, + + /* Throttle charge current */ + PSY_THROTTLE_CC_LIMIT, + + /* Throttle input current limit */ + PSY_THROTTLE_INPUT_LIMIT, +}; + +Throttling configuration example: + +struct psy_throttle_state my_throttle_states[] = { + + /* Level 0: Limit charge current to 1500mA. Normal Level */ + { + .throttle_action = PSY_THROTTLE_CC_LIMIT, + .throttle_val = 1500, + }, + + /* Level 1: Limit charge current to 500mA */ + { + .throttle_action = PSY_THROTTLE_CC_LIMIT, + .throttle_val = 500, + }, + + /* + * Level 2: Disable charging: Stop charging, charger supply power to + * platform. + */ + { + .throttle_action = PSY_THROTTLE_DISABLE_CHARGING, + }, + + /* Level 3: Disable charger: Battery start discharging */ + { + .throttle_action = PSY_THROTTLE_DISABLE_CHARGER, + }, + +}; + +4. Charger Cable notifications +============================== + +Charger cables can have different charging capabilities (current) depending on +platform and cable type. The cable provider drivers detect and identify the +cable types and notifies the power supply subsystem by posting an event +PSY_EVENT_CABLE on the power_supply_notifier chain. The event notification +should have a property structure pointer which indicates the cable type, event +and capability. + +struct psy_cable_props cable; +cable.chrg_evt = PSY_CHARGER_CABLE_EVENT_CONNECT; +cable.chrg_type = PSY_CHARGER_CABLE_TYPE_USB_DCP; +cable.ma = 1500; + +atomic_notifier_call_chain(&power_supply_notifier, + PSY_EVENT_CABLE, &cable); + +Power supply charging driver process this event and takes actions to setup +charging. + +5. Registering new charging algorithm +=================================== +Power supply charging driver supports pluggable charging algorithms. Charging +algorithms processes charging profile and/or applies different logic (pulse +charging/fast charging/relaxed charging) to decide the charging parameters (CC +and CV). + +* Populate algorithm structure + struct psy_charging_algo new_algo; + /* populate the type charging profile the algorithm handles */ + pse_algo.chrg_prof_type = PSY_CHRG_PROF_PSE; + pse_algo.name = "my_algo"; + /* callback function to retrieve CC and CV */ + pse_algo.get_next_cc_cv = my_algo_get_next_cc_cv; + /* callback function to retreive battery thresholds */ + pse_algo.get_batt_thresholds = my_algo_get_bat_thresholds; + /* register charging algorithm */ + power_supply_register_charging_algo(&pse_algo); + +When the type of charging profile reported by battery and algorithm matches, +the algorithm will be invoked to get the charging parameters. + +6. TODO +======= +* Replace static cable array with dynamic list +* Implement safety timer and watch dog timer features with more monitoring +* Move charge full detection logic to psy charging driver from algorithm driver
drivers/power/Kconfig +8 -0
@@ -14,6 +14,14 @@ config POWER_SUPPLY_DEBUG Say Y here to enable debugging messages for power supply class and drivers. +config POWER_SUPPLY_CHARGER + bool "Power Supply Charger" + help + Say Y here to enable the power supply charging control driver. Charging + control supports charging in a generic way. This allows the charger + drivers to keep the charging logic outside and the charger driver + just need to abstract the charger hardware. + config PDA_POWER tristate "Generic PDA/phone power driver" depends on !S390
drivers/power/Makefile +1 -0
@@ -7,6 +7,7 @@ power_supply-$(CONFIG_LEDS_TRIGGERS) += power_supply_leds.o obj-$(CONFIG_POWER_SUPPLY) += power_supply.o obj-$(CONFIG_GENERIC_ADC_BATTERY) += generic-adc-battery.o +obj-$(CONFIG_POWER_SUPPLY_CHARGER) += power_supply_charger.o obj-$(CONFIG_PDA_POWER) += pda_power.o obj-$(CONFIG_APM_POWER) += apm_power.o obj-$(CONFIG_MAX8925_POWER) += max8925_power.o
drivers/power/power_supply_charger.c new file +1016 -0
1,016 lines not on this page · raw message
drivers/power/power_supply_charger.h new file +226 -0
226 lines not on this page · raw message
drivers/power/power_supply_core.c +3 -0
@@ -483,6 +483,9 @@ static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd, ret = psy->set_property (psy,POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT , &val); +atomic_notifier_call_chain (&power_supply_notifier, +PSY_EVENT_THROTTLE , psy); +return ret; }
include/linux/power/power_supply_charger.h new file +307 -0
307 lines not on this page · raw message
include/linux/power_supply.h +161 -0
177 lines not on this page · raw message
Signature · 1 line
1.7.9.5
Re: [PATCH 2/4] power_supply: Introduce generic psy charging driver
Hi!
Quoted 11 lines
The Power Supply charging driver connects multiple subsystems
to do charging in a generic way. The subsystems involvespower_supply, thermal and battery communication subsystems (1wire). With this the charging is handled in a generic way.The driver makes use of different new features - Battery Identification interfaces, pluggable charging algorithms, charger cable arbitrations etc. The patch also introduces generic interface for charger cable notifications. Charger cable events and capabilities can be notified using the generic
power_supply_notifierchain.
Quoted 6 lines
Overall this driver removes the charging logic out of the charger chip driver and the charger chip driver can just listen to the request from the power supply charging driver to set the charger properties. This can be implemented by exposing
get_propertyand set property callbacks.Signed-off-by Jenny TC <[email protected]>
No, sorry, this still does not look like kernel code.
Quoted 2 lines
+3. Use Charging Driver to setup charging
+===========================================
I think intention here is to match the lengths?
drivers/power/power_supply_charger.c line 87
Quoted 5 lines
drivers/power/power_supply_charger.c new file +5
-0+static inline bool psy_is_battery_prop_changed (struct psy_batt_props *bat_prop, +struct psy_batt_props *bat_cache) +{ +if (!bat_cache) +return true;
Are all these !null checks neccessary? I find them excessive ... especially for debug functions below.
drivers/power/power_supply_charger.c line 107
Quoted 4 lines
drivers/power/power_supply_charger.c new file +4
-0+static inline bool psy_is_charger_prop_changed (struct psy_charger_props *prop, +struct psy_charger_props *cache_prop) +{ +/* if online/prsent/health/is_charging is changed, then return true */
typo "present".
Quoted 1 line
+static int
__update_supplied_psy(struct device *dev, void *data)
Too many spaces before __.
Quoted 4 lines
drivers/power/power_supply_charger.c new file +4
-0+for (i =0 ; i < psy->num_supplicants; i++) { + psb =power_supply_get_by_name (psy->supplied_to[i]); +if (!psb) +continue ;
drivers/power/power_supply_charger.c line 178
Quoted 10 lines
drivers/power/power_supply_charger.c new file +10
-0+ +if (!psb->data) + psb->data =devm_kzalloc (psb->dev, +sizeof (struct psy_batt_context ),GFP_KERNEL ); + + batt_context = psb->data; + batt_context->supplied_by_psy + [batt_context->num_supplied_by_psy++] = psy; + charger_context->supplied_to_psy + [charger_context->num_supplied_to_psy++] = psb;
Really strange indent.
What ensures you don't write beyond end of array.
And why have arrays at all? Could you simply link the structures together?
drivers/power/power_supply_charger.c line 226
Quoted 9 lines
drivers/power/power_supply_charger.c new file +9
-0+static void cache_successive_samples (long *sample_array,long new_sample) +{ +int i; + +for (i =0 ; i <MAX_CUR_VOLT_SAMPLES -1 ; ++i) + *(sample_array + i) = *(sample_array + i +1 ); + + *(sample_array + i) = new_sample; +}
But this is why I write. Would a ring buffer be faster and more elegant?
Also we do sample_array[i], not *(sample_array + i).
Oh and we also do i++, not ++i.
drivers/power/power_supply_charger.c line 317
Quoted 4 lines
drivers/power/power_supply_charger.c new file +4
-0+static int process_cable_props (struct psy_cable_props *cap) +{ +struct charger_cable *cable =NULL ; +unsigned off;
unsigned int.
drivers/power/power_supply_charger.c line 335
Quoted 2 lines
drivers/power/power_supply_charger.c new file +2
-0+memcpy ((void *)&cable->cable_props, (void *)cap, +sizeof (cable->cable_props));
Are the casts actually neccessary?
Would cable->cable_props = caps work as well?
Quoted 8 lines
drivers/power/power_supply_charger.c new file +8
-0+spin_lock (&psy_chrgr.event_queue_lock); +list_for_each_entry_safe (evt, tmp, &psy_chrgr.event_queue, node) { +list_del (&evt->node); +spin_unlock (&psy_chrgr.event_queue_lock); + +mutex_lock (&psy_chrgr.event_lock); + +switch (evt->event) {
...
drivers/power/power_supply_charger.c line 381
Quoted 10 lines
drivers/power/power_supply_charger.c new file +10
-0+ } + +mutex_unlock (&psy_chrgr.event_lock); + +spin_lock (&psy_chrgr.event_queue_lock); +kfree (evt); + } + +spin_unlock (&psy_chrgr.event_queue_lock); +}
Are you sure about locking here? Care to drop some comments to help us understand it?
drivers/power/power_supply_charger.c line 429
Quoted 10 lines
drivers/power/power_supply_charger.c new file +10
-0+static int register_usb_notifier (void ) +{ +int retval =0 ; + + otg_xceiver =usb_get_phy (USB_PHY_TYPE_USB2 ); +if (!otg_xceiver) { +pr_err ("failure to get otg transceiver\n" ); + retval = -EIO ; + goto notifier_reg_failed; + }
just do return -EIO here.
drivers/power/power_supply_charger.c line 438
Quoted 9 lines
drivers/power/power_supply_charger.c new file +9
-0+ retval =usb_register_notifier (otg_xceiver, &nb); +if (retval) { +pr_err ("failure to register otg notifier\n" ); + goto notifier_reg_failed; + } + +notifier_reg_failed: +return retval; +}
Delete goto and label.
Quoted 9 lines
drivers/power/power_supply_charger.c new file +9
-0+static inline bool is_trigger_charging_algo (struct power_supply *psy) +{ +/* +* trigger charging algorithm if battery or +* charger properties are changed. Also no need to +* invoke algorithm for power_supply_changed from +* charger, if all supplied_to has the ext_port_changed defined. +* On invoking the ext_port_changed the supplied to can send +* power_supplied_changed event.
drivers/power/power_supply_charger.c line 458
Quoted 10 lines
drivers/power/power_supply_charger.c new file +10
-0+*/ + +if ((psy_is_charger (psy) && !is_supplied_to_has_ext_pwr_changed (psy)) && +is_chrgr_prop_changed (psy)) +return true; + +if ((psy_is_battery (psy)) && (is_batt_prop_changed (psy) || +is_supplied_by_changed (psy))) +return true; +
Begin function with if (!psy_is_charger...) to simplify the rest?
Quoted 25 lines
drivers/power/power_supply_charger.c new file +24
-0+static int get_battery_status (struct power_supply *psy) +{ +int status; +struct power_supply *psc; +struct psy_batt_context *batt_context; +int cnt; + +if (!psy_is_battery (psy) || (!psy->data)) +return POWER_SUPPLY_STATUS_UNKNOWN ; + + batt_context = psy->data; + status =POWER_SUPPLY_STATUS_DISCHARGING ; + cnt = batt_context->num_supplied_by_psy; + +while (cnt--) { + psc = batt_context->supplied_by_psy[cnt]; + +if (psy_is_present (psc)) + status =POWER_SUPPLY_STATUS_NOT_CHARGING ; + +if (!(psy_is_charging_can_be_enabled (psc)) || + (!psy_is_health_good (psy)) || + (!psy_is_health_good (psc))) +continue ;
drivers/power/power_supply_charger.c line 496
Quoted 10 lines
drivers/power/power_supply_charger.c new file +10
-0+if ((batt_context->algo_stat ==PSY_ALGO_STAT_FULL ) || + (batt_context->algo_stat ==PSY_ALGO_STAT_MAINT )) + status =POWER_SUPPLY_STATUS_FULL ; +else if (psy_is_charging_enabled (psc)) + status =POWER_SUPPLY_STATUS_CHARGING ; + } + +pr_debug ("%s: Set status=%d for %s\n" , __func__, status, psy->name); + +return status;
So if you have two batteries, return value is basically random?
drivers/power/power_supply_charger.c line 504
Quoted 6 lines
drivers/power/power_supply_charger.c new file +6
-0+static void update_charger_online (struct power_supply *psy) +{ +int online; +struct psy_charger_context *charger_context; + + online =psy_is_charger_enabled (psy) ?1 :0 ;
!!(..)
drivers/power/power_supply_charger.c line 508
Quoted 4 lines
drivers/power/power_supply_charger.c new file +4
-0+psy_set_charger_online (psy, online); + charger_context = psy->data; + charger_context->charger_props.online = online; +}
Or perhaps online should work with bools, as you use them elsewhere?
drivers/power/power_supply_charger.c line 619
Quoted 5 lines
drivers/power/power_supply_charger.c new file +5
-0+static inline void wait_for_charging_enabled (struct power_supply *psy) +{ +wait_event_timeout (psy_chrgr.wait_chrg_enable, + (psy_is_charging_enabled (psy)),HZ ); +}
Is one second timeout enough?
Quoted 4 lines
drivers/power/power_supply_charger.c new file +4
-0+static inline void enable_supplied_by_charging (struct power_supply *psy, +bool is_enable) +{ +struct power_supply *psc;
drivers/power/power_supply_charger.c line 634
Quoted 10 lines
drivers/power/power_supply_charger.c new file +10
-0+struct psy_batt_context *batt_context; +int cnt; + +if (!psy_is_battery (psy)) +return ; +/* +* Get list of chargers supplying power to this battery and +* disable charging for all chargers +*/ + batt_context = psy->data;
two spaces.
drivers/power/power_supply_charger.c line 644
Quoted 10 lines
drivers/power/power_supply_charger.c new file +10
-0+ cnt = batt_context->num_supplied_by_psy; + +while (cnt--) { + psc = batt_context->supplied_by_psy[cnt]; +if (!psy_is_present (psc)) +continue ; +if (is_enable &&psy_is_charging_can_be_enabled (psc)) { +psy_enable_charging (psc); +wait_for_charging_enabled (psc); +
extra empty line
drivers/power/power_supply_charger.c line 647
Quoted 3 lines
drivers/power/power_supply_charger.c new file +3
-0+ }else { +psy_disable_charging (psc); + }
Do continue and get rid of else?
drivers/power/power_supply_charger.c line 665
Quoted 9 lines
drivers/power/power_supply_charger.c new file +9
-0+if (!is_trigger_charging_algo (psy)) +return ; + +if (psy_is_battery (psy)) { + +enable_supplied_by_charging (psy, !trigger_algo (psy)); + goto update_sysfs; + + }
More extra lines.
Best regards,
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Sebastian Reichel
maintainerRe: [PATCH 2/4] power_supply: Introduce generic psy charging driver
In reply to Jenny TCHi Jenny,
Quoted 11 lines · Jenny TC
The Power Supply charging driver connects multiple subsystems
to do charging in a generic way. The subsystems involvespower_supply, thermal and battery communication subsystems (1wire). With this the charging is handled in a generic way.The driver makes use of different new features - Battery Identification interfaces, pluggable charging algorithms, charger cable arbitrations etc. The patch also introduces generic interface for charger cable notifications. Charger cable events and capabilities can be notified using the generic
power_supply_notifierchain.
Quoted 4 lines
Overall this driver removes the charging logic out of the charger chip driver and the charger chip driver can just listen to the request from the power supply charging driver to set the charger properties. This can be implemented by exposing
get_propertyand set property callbacks.
this seems to be a superset of charger-manager, which is already in the kernel. I would prefer not to have two different charger mangement frameworks in the kernel.
I suggest to add features supported by charger-manager to power supply charging driver and convert users of charger-manager to the improved driver.
I CC'd MyungJoo Ham, who wrote the charger-manager, so that he can also give feedback.
Quoted 22 lines
Signed-off-by Jenny TC <[email protected]>--- Documentation/power/power_supply_charger.txt | 350 +++++++++ drivers/power/Kconfig | 8 + drivers/power/Makefile | 1 + drivers/power/power_supply_charger.c | 1016 ++++++++++++++++++++++++++ drivers/power/power_supply_charger.h | 226 ++++++ drivers/power/power_supply_core.c | 3 + include/linux/power/power_supply_charger.h | 307 ++++++++ include/linux/power_supply.h | 161 ++++ 8 files changed, 2072 insertions(+) create mode 100644 Documentation/power/power_supply_charger.txt create mode 100644 drivers/power/power_supply_charger.c create mode 100644 drivers/power/power_supply_charger.h create mode 100644 include/linux/power/power_supply_charger.hDocumentation/power/power_supply_charger.txt new file +0
-0
Documentation/power/power_supply_charger.txt line 6
Quoted 6 lines
Documentation/power/power_supply_charger.txt new file +6
-0@@ -0,0 +1,350 @@ +1. Introduction +=============== + +The Power Supply charging driver connects multiple subsystems +to do charging in a generic way. The subsystems involves power_supply, +thermal and battery communication subsystems (1wire). With this the charging is
(e.g. 1wire)?
Quoted 27 lines
Documentation/power/power_supply_charger.txt new file +27
-0+handled in a generic way by plugging the driver to power supply subsystem. + +The driver introduces different new features - Battery Identification +interfaces, pluggable charging algorithms, charger cable arbitrations etc. + +In existing driver implementations the charging is done based on the static +battery characteristics. This is done at the boot time by passing the battery +properties (max_voltage, capacity) etc. as a platform data to the +charger/battery driver. But new generation high volt batteries needs to be +identified dynamically to do charging in a safe manner. The batteries are +coming with different communication protocols. It become necessary to +communicate with battery and identify it's charging profiles before setup +charging. + +Also the charging algorithms can vary based on the battery characteristics +and the platform characteristics. To handle charging in a generic way it's +necessary to support pluggable charging algorithms. Power Supply Charging +driver selects algorithms based on the type of battery charging profile. +This is a simple binding and can be improved later. This may be improved to +select the algorithms based on the platform requirements. Also we can extend +this driver to plug algorithms from the user space. + +The driver also introduces the charger cable arbitration. A charger may +supports multiple cables, but it may not be able to charge with multiple +cables at a time (USB/AC/Wireless etc.). The arbitration logic inside the +driver selects the cable based on it's capabilities and the maximum +charge current the platform can support.
Documentation/power/power_supply_charger.txt line 43
Quoted 10 lines
Documentation/power/power_supply_charger.txt new file +10
-0+ +Also the driver exposes features to control charging on different platform +states. One such feature is thermal. The driver handles the thermal +throttling requests for charging and control charging based on the thermal +subsystem requirements. + +Overall this driver removes the charging logic out of the charger chip driver +and the charger chip driver can just listen to the request from the power +supply charging driver to set the charger properties. This can be implemented +by exposing get_property and set property callbacks.
It can be, or it is supposed to be?
Documentation/power/power_supply_charger.txt line 56
Quoted 12 lines
Documentation/power/power_supply_charger.txt new file +12
-0+2. Reading Battery charging profile +=================================== + +Power Supply charging driver expose APIs to retrieve battery profile of a +battery. The battery profile can be read by battery identification driver which +may be 1wire/I2C/SFI driver. Battery identification driver can register the +battery profile with the power supply charging driver using the API +psy_battery_prop_changed(). The driver also exposes API +psy_get_battery_prop() to retrieve the battery profile which can be +used by power supply drivers to setup the charging. Also drivers +can register for battery removal/insertion notifications using +power_supply_reg_notifier()
I think _prop() should be either _profile() or _props().
Documentation/power/power_supply_charger.txt line 59
Quoted 2 lines
Documentation/power/power_supply_charger.txt new file +2
-0+3. Use Charging Driver to setup charging +===========================================
^^^
wrong length?
Quoted 3 lines
[...]
+3.1 Registering charger chip driver with power supply charging driver
+================================================================
^^^^^^
wrong length?
Quoted 3 lines
[...]
+3.2 Properties exposed to power supply class driver
+==================================================
^
wrong length?
Quoted 3 lines
[...]
+3.3 Properties exposed to power supply charging driver
+=====================================================
^
wrong length?
Quoted 3 lines
[...]
+3.4 Throttling data configuration
+=============================
^^^^
wrong length?
Quoted 3 lines
[...]
+5. Registering new charging algorithm
+===================================
^^
wrong length?
Documentation/power/power_supply_charger.txt line 350
Quoted 6 lines
[...]
+6. TODO
+=======Documentation/power/power_supply_charger.txt new file +3
-0+* Replace static cable array with dynamic list +* Implement safety timer and watch dog timer features with more monitoring +* Move charge full detection logic to psy charging driver from algorithm driver
Just curious: What are your plans regarding the TODO list?
drivers/power/power_supply_charger.c line 2
Quoted 9 lines
[...]
drivers/power/power_supply_charger.c new file +2
-0@@ -0,0 +1,1016 @@ +/* +* Copyright (C) 2012 Intel Corporation
should be updated probably :)
Quoted 6 lines
[...]
drivers/power/power_supply_charger.c new file +5
-0+#include <linux/device.h> +#include <linux/err.h> +#include <linux/init.h> +#include <linux/module.h> +#include <linux/notifier.h>
drivers/power/power_supply_charger.c line 33
Quoted 10 lines
drivers/power/power_supply_charger.c new file +10
-0+#include <linux/power_supply.h> +#include <linux/power/power_supply_charger.h> +#include <linux/slab.h> +#include <linux/thermal.h> +#include <linux/types.h> +#include <linux/usb/otg.h> +#include "power_supply_charger.h" + + +#define MAX_CHARGER_COUNT 5
^^^^^^^^^^^^^^^^^
unused?
Quoted 90 lines
drivers/power/power_supply_charger.c new file +90
-0+#define MAX_CABLE_COUNT 15 +static LIST_HEAD (algo_list); + +struct psy_event_node { +struct list_head node; +unsigned long event; +struct psy_cable_props cap; +struct power_supply *psy; +struct psy_batt_chrg_prof batt_property; +}; + +struct charger_cable { +struct psy_cable_props cable_props; +unsigned long psy_cable_type; +}; + +struct psy_charger_drv_context { +bool is_usb_cable_evt_reg; +int psyc_cnt; +int batt_status; +/* cache battery and charger properties */ +struct mutex event_lock; +struct list_head event_queue; +struct psy_batt_chrg_prof batt_property; +struct work_struct event_work; +struct charger_cable cable_list[MAX_CABLE_COUNT ]; +wait_queue_head_t wait_chrg_enable; +spinlock_t event_queue_lock; +}; + +DEFINE_MUTEX (battid_lock); + +static struct psy_charger_drv_context psy_chrgr; +static struct usb_phy *otg_xceiver; +static int handle_event_notification (struct notifier_block *nb, +unsigned long event,void *data); +static struct notifier_block nb = { + .notifier_call = handle_event_notification, + }; +static void configure_chrgr_source (struct charger_cable *cable_lst); +static struct psy_charging_algo *power_supply_get_charging_algo + (struct power_supply *,struct psy_batt_chrg_prof *); +static void __power_supply_trigger_charging_handler (struct power_supply *psy); +static void power_supply_trigger_charging_handler (struct power_supply *psy); +static void trigger_algo_psy_class (void ); +static int psy_charger_throttle_charger (struct power_supply *psy, +unsigned long state); + +static inline bool psy_is_battery_prop_changed (struct psy_batt_props *bat_prop, +struct psy_batt_props *bat_cache) +{ +if (!bat_cache) +return true; + +/* return true if temperature, health or throttling state changed */ +if ((bat_cache->temperature != bat_prop->temperature) || + (bat_cache->health != bat_prop->health) || + (bat_cache->throttle_state != bat_prop->throttle_state)) +return true; + +/* return true if voltage or current changed not within TTL limit */ +if (time_after64 (bat_prop->tstamp, bat_cache->tstamp +PROP_TTL ) && + (bat_cache->current_now != bat_prop->current_now || + bat_cache->voltage_now != bat_prop->voltage_now)) +return true; + +return false; +} + +static inline bool psy_is_charger_prop_changed (struct psy_charger_props *prop, +struct psy_charger_props *cache_prop) +{ +/* if online/prsent/health/is_charging is changed, then return true */ +if (!cache_prop) +return true; + +if (cache_prop->online != prop->online || + cache_prop->present != prop->present || + cache_prop->is_charging != prop->is_charging || + cache_prop->health != prop->health) +return true; +else +return false; + +} + +static inline void get_cur_chrgr_prop (struct power_supply *psy, +struct psy_charger_props *chrgr_prop) +{ + chrgr_prop->is_charging =psy_is_charging_enabled (psy);
drivers/power/power_supply_charger.c line 134
Quoted 10 lines
drivers/power/power_supply_charger.c new file +10
-0+ chrgr_prop->online =psy_is_online (psy); + chrgr_prop->present =psy_is_present (psy); + chrgr_prop->cable =psy_cable_type (psy); + chrgr_prop->health =PSY_HEALTH (psy); + chrgr_prop->tstamp =get_jiffies_64 (); +} + +static void dump_charger_props (struct psy_charger_props *props) +{ +if (props)
You can drop the NULL pointer check. This should be checked already by the parent function.
drivers/power/power_supply_charger.c line 143
Quoted 9 lines
drivers/power/power_supply_charger.c new file +9
-0+pr_debug ("%s: present=%d is_charging=%d health=%d online=%d cable=%ld tstamp=%ld\n" , + __func__, props->present, props->is_charging, + props->health, props->online, props->cable, + props->tstamp); +} + +static void dump_battery_props (struct psy_batt_props *props) +{ +if (props)
You can drop the NULL pointer check. This should be checked already by the parent function.
Quoted 15 lines
drivers/power/power_supply_charger.c new file +15
-0+pr_debug ("%s voltage_now=%ld current_now=%ld temperature=%d status=%ld health=%d tstamp=%lld" , + __func__, props->voltage_now, props->current_now, + props->temperature, props->status, props->health, + props->tstamp); +} + +static int __update_supplied_psy (struct device *dev,void *data) +{ +struct psy_charger_context *charger_context; +struct psy_batt_context *batt_context; +struct power_supply *psb, *psy; +int i; + + psy =dev_get_drvdata (dev); +
drivers/power/power_supply_charger.c line 168
Quoted 10 lines
drivers/power/power_supply_charger.c new file +10
-0+if (!psy || !psy_is_charger (psy) || !psy->data) +return 0 ; + + charger_context = psy->data; + charger_context->num_supplied_to_psy =0 ; + +for (i =0 ; i < psy->num_supplicants; i++) { + psb =power_supply_get_by_name (psy->supplied_to[i]); +if (!psb) +continue ;
no warning?
drivers/power/power_supply_charger.c line 172
Quoted 3 lines
drivers/power/power_supply_charger.c new file +3
-0+if (!psb->data) + psb->data =devm_kzalloc (psb->dev, +sizeof (struct psy_batt_context ),GFP_KERNEL );
devm_kzalloc can fail (recheck psb->data == NULL).
drivers/power/power_supply_charger.c line 176
Quoted 3 lines
drivers/power/power_supply_charger.c new file +3
-0+ batt_context = psb->data; + batt_context->supplied_by_psy + [batt_context->num_supplied_by_psy++] = psy;
this can go out of bound!
drivers/power/power_supply_charger.c line 178
Quoted 2 lines
drivers/power/power_supply_charger.c new file +2
-0+ charger_context->supplied_to_psy + [charger_context->num_supplied_to_psy++] = psb;
this can go out of bound!
Quoted 6 lines
drivers/power/power_supply_charger.c new file +5
-0+ } + +return 0 ; +} +
drivers/power/power_supply_charger.c line 226
Quoted 10 lines
drivers/power/power_supply_charger.c new file +10
-0+ +static void cache_successive_samples (long *sample_array,long new_sample) +{ +int i; + +for (i =0 ; i <MAX_CUR_VOLT_SAMPLES -1 ; ++i) + *(sample_array + i) = *(sample_array + i +1 ); + + *(sample_array + i) = new_sample; +}
please use array syntax, e.g.
sample_array[i] = new_sample[i+1];
not using a ring buffer is ok with MAX_CUR_VOLT_SAMPLES being 3. If this is ever increased a ring buffer should be used, though.
drivers/power/power_supply_charger.c line 264
Quoted 10 lines
[...]
drivers/power/power_supply_charger.c new file +9
-0+static inline bool is_batt_prop_changed (struct power_supply *psy) +{ +struct psy_batt_context *batt_context; +struct psy_batt_props new_batt_props; + +if (!psy->data) +update_supplied_psy (); + + batt_context = psy->data;
^^
remove one space
Quoted 13 lines
[...]
drivers/power/power_supply_charger.c new file +12
-0+static int process_cable_props (struct psy_cable_props *cap) +{ +struct charger_cable *cable =NULL ; +unsigned off; + +pr_info ("%s: event:%d, type:%lu, current_mA:%d\n" , + __func__, cap->chrg_evt, cap->chrg_type, cap->current_mA); + + off =ffs (cap->chrg_type); + +if (!off || off >=ARRAY_SIZE (psy_chrgr.cable_list)) { +pr_err ("%s:%d Invalid cable type\n" , __FILE__, __LINE__);
drivers/power/power_supply_charger.c line 335
Quoted 10 lines
drivers/power/power_supply_charger.c new file +10
-0+return -EINVAL ; + } + + cable = &psy_chrgr.cable_list[off -1 ]; + +if (cable->psy_cable_type ==PSY_CHARGER_CABLE_TYPE_NONE ) + cable->psy_cable_type = cap->chrg_type; + +memcpy ((void *)&cable->cable_props, (void *)cap, +sizeof (cable->cable_props));
Use struct assignment instead of memcpy:
*cable->cable_props = *cap;
Quoted 17 lines
drivers/power/power_supply_charger.c new file +16
-0+ +configure_chrgr_source (psy_chrgr.cable_list); + +return 0 ; +} + + +static int register_usb_notifier (void ) +{ +int retval =0 ; + + otg_xceiver =usb_get_phy (USB_PHY_TYPE_USB2 ); +if (!otg_xceiver) { +pr_err ("failure to get otg transceiver\n" ); + retval = -EIO ; + goto notifier_reg_failed;
drivers/power/power_supply_charger.c line 438
Quoted 10 lines
drivers/power/power_supply_charger.c new file +10
-0+ } + retval =usb_register_notifier (otg_xceiver, &nb); +if (retval) { +pr_err ("failure to register otg notifier\n" ); + goto notifier_reg_failed; + } + +notifier_reg_failed: +return retval; +}
remove useless goto by returning directly.
Quoted 27 lines
[...]
drivers/power/power_supply_charger.c new file +26
-0+static int get_battery_status (struct power_supply *psy) +{ +int status; +struct power_supply *psc; +struct psy_batt_context *batt_context; +int cnt; + +if (!psy_is_battery (psy) || (!psy->data)) +return POWER_SUPPLY_STATUS_UNKNOWN ; + + batt_context = psy->data; + status =POWER_SUPPLY_STATUS_DISCHARGING ; + cnt = batt_context->num_supplied_by_psy; + +while (cnt--) { + psc = batt_context->supplied_by_psy[cnt]; + +if (psy_is_present (psc)) + status =POWER_SUPPLY_STATUS_NOT_CHARGING ; + +if (!(psy_is_charging_can_be_enabled (psc)) || + (!psy_is_health_good (psy)) || + (!psy_is_health_good (psc))) +continue ; + + if ((batt_context->algo_stat == PSY_ALGO_STAT_FULL) ||
drivers/power/power_supply_charger.c line 497
Quoted 10 lines
drivers/power/power_supply_charger.c new file +10
-0+ (batt_context->algo_stat == PSY_ALGO_STAT_MAINT)) + status =POWER_SUPPLY_STATUS_FULL ; + else if (psy_is_charging_enabled(psc)) + status =POWER_SUPPLY_STATUS_CHARGING ; + } + +pr_debug ("%s: Set status=%d for %s\n" , __func__, status, psy->name); + +return status; +}
mh. So basically if there's more than one charger for a battery we will return the status of the one, which was initialized at last? IMHO the status from the other chargers should be used to validate the other ones.
drivers/power/power_supply_charger.c line 504
Quoted 7 lines
[...]
drivers/power/power_supply_charger.c new file +6
-0+static void update_charger_online (struct power_supply *psy) +{ +int online; +struct psy_charger_context *charger_context; + + online =psy_is_charger_enabled (psy) ?1 :0 ;
online = !!psy_is_charger_enabled(psy)
drivers/power/power_supply_charger.c line 508
Quoted 5 lines
drivers/power/power_supply_charger.c new file +4
-0+psy_set_charger_online (psy, online); + charger_context = psy->data; + charger_context->charger_props.online = online; +}
TODO TODO TODO
drivers/power/power_supply_charger.h line 2
Quoted 8 lines
drivers/power/power_supply_charger.h new file +2
-0@@ -0,0 +1,226 @@ +/* +* Copyright (C) 2012 Intel Corporation
^^^^
update?
drivers/power/power_supply_charger.h line 79
Quoted 10 lines
[...]
drivers/power/power_supply_charger.h new file +9
-0+static inline int psy_throttle_action + (struct power_supply *psy,unsigned int state) +{ +struct power_supply_charger *psyc; + + psyc =psy_to_psyc (psy); + +if (psyc) +return ((psyc->throttle_states)+state)->throttle_action;
please use array syntax!
Quoted 7 lines
drivers/power/power_supply_charger.h new file +6
-0+ +/* If undetermined state, better disable charger for safety reasons */ + +return PSY_THROTTLE_DISABLE_CHARGER ; +} +
drivers/power/power_supply_charger.h line 118
Quoted 10 lines
drivers/power/power_supply_charger.h new file +10
-0+ +static inline int psy_throttle_cc_value (struct power_supply *psy, +unsigned int state) +{ +struct power_supply_charger *psyc; + + psyc =psy_to_psyc (psy); + +if (psyc) +return ((psyc->throttle_states)+state)->throttle_val;
please use array syntax!
drivers/power/power_supply_charger.h line 123
Quoted 6 lines
drivers/power/power_supply_charger.h new file +5
-0+ +/* If undetermined state, better set CC as 0 */ +return 0 ; +} +
-- Sebastian
RE: [PATCH 2/4] power_supply: Introduce generic psy charging driver
Quoted 28 lines
From: Sebastian Reichel [mailto:[email protected]]
Sent: Friday, July 18, 2014 7:49 AM
To: Tc, Jenny
Cc: [email protected]; Dmitry Eremin-Solenikov; Pavel Machek; Anton Vorontsov; David Woodhouse; David Cohen; Pallala, Ramakrishna; [email protected]
Subject: Re: [PATCH 2/4]power_supply: Introduce generic psy charging driverHi Jenny,
Quoted 7 lines · Jenny TC
The Power Supply charging driver connects multiple subsystems to do charging in a generic way. The subsystems involves
power_supply, thermal and battery communication subsystems (1wire). With this the charging is handled in a generic way.The driver makes use of different new features - Battery Identification interfaces, pluggable charging algorithms, charger cable arbitrations
etc.
Quoted 8 lines
The patch also introduces generic interface for charger cable notifications. Charger cable events and capabilities can be notified using the generic
power_supply_notifierchain.Overall this driver removes the charging logic out of the charger chip driver and the charger chip driver can just listen to the request from the power supply charging driver to set the charger properties. This can be implemented by exposing
get_propertyand set property callbacks.
Quoted 8 lines
this seems to be a superset of charger-manager, which is already in the kernel. I would prefer not to have two different charger mangement frameworks in the kernel.
I suggest to add features supported by charger-manager to power supply charging driver and convert users of charger-manager to the improved driver.
I CC'd MyungJoo Ham, who wrote the charger-manager, so that he can also give feedback.
We are back to the initial discussions we had in the list. The initial proposal was for the charger manager. The charger manager is more aligned to regulator framework, use private notification mechanisms(cm_notify_event,fullbatt_vchk etc) and relies more on platform data (struct charger_desc). This doesn't seems to be good to support plug in charger drivers, charging algorithms, battery identification drivers at runtime. Power supply charger driver is introduced to meet all these requirements by extending the existing power_supply subsystem features like power_supply_changed event, power_supply_register, power supply thermal throttling mechanism so that plugging new driver would be
easy. Also the user interfaces would remain the same as power_supply subsystem.
Able to locate link on the discussion.
http://www.gossamer-threads.com/lists/linux/kernel/1562180.
Re: [PATCH 2/4] power_supply: Introduce generic psy charging driver
Hi!
Quoted 14 lines
Quoted 5 lines
I suggest to add features supported by charger-manager to power supply charging driver and convert users of charger-manager to the improved driver.
I CC'd MyungJoo Ham, who wrote the charger-manager, so that he can also give feedback.
We are back to the initial discussions we had in the list. The initial proposal was for the charger manager. The charger manager is more aligned to regulator framework, use private notification
mechanisms(cm_notify_event,fullbatt_vchk etc)and relies more on platform data (structcharger_desc). This doesn't seems to be good to support plug in charger drivers, charging algorithms, battery identification drivers
at runtime.
Quoted 1 line
From what I seen, the new "plug in friendly" approach is unsuitable
for the kernel.
I really tried hard to review it, and it went through how many, 10?, iterations, and it is not getting better.
So please just extend existing code.
If you insist that is not adequate, please talk to some experienced hacker at Intel (does Rafael still work there? Ingo?) to design acceptable solution.
Seeing same patches for 10 iterations with no real improvements is not fun.
Thanks,
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
PATCH v11 3/4 power_supply: Introduce PSE compliant algorithm
In reply to Jenny TCAs per Product Safety Engineering (PSE) specification for battery charging, the battery characteristics and thereby the charging rates can vary on different temperature zones. This patch introduces a PSE compliant charging algorithm with maintenance charging support. The algorithm can be selected by the power supply charging driver based on the type of the battery charging profile.
--- drivers/power/Kconfig | 15 ++ drivers/power/Makefile | 1 + drivers/power/charging_algo_pse.c | 203 ++++++++++++++++++++++++++++ include/linux/power/power_supply_charger.h | 66 +++++++++ 4 files changed, 285 insertions(+) create mode 100644 drivers/power/charging_algo_pse.c
drivers/power/Kconfig +15 -0
@@ -22,6 +22,21 @@ config POWER_SUPPLY_CHARGER drivers to keep the charging logic outside and the charger driver just need to abstract the charger hardware. +config POWER_SUPPLY_CHARGING_ALGO_PSE + bool "PSE compliant charging algorithm" + depends on POWER_SUPPLY_CHARGER + help + Say Y here to select Product Safety Engineering (PSE) compliant + charging algorithm. As per PSE standard the battery characteristics + and thereby the charging rates can vary on different temperature + zones. Select this if your charging algorithm need to change the + charging parameters based on the battery temperature and the battery + charging profile follows the struct psy_pse_chrg_prof definition. + This config will enable PSE compliant charging algorithm with + maintenance charging support. At runtime the algorithm will be + selected by the psy charger driver based on the type of the battery + charging profile. + config PDA_POWER tristate "Generic PDA/phone power driver" depends on !S390
drivers/power/Makefile +1 -0
@@ -8,6 +8,7 @@ obj-$(CONFIG_POWER_SUPPLY) += power_supply.o obj-$(CONFIG_GENERIC_ADC_BATTERY) += generic-adc-battery.o obj-$(CONFIG_POWER_SUPPLY_CHARGER) += power_supply_charger.o +obj-$(CONFIG_POWER_SUPPLY_CHARGING_ALGO_PSE) += charging_algo_pse.o obj-$(CONFIG_PDA_POWER) += pda_power.o obj-$(CONFIG_APM_POWER) += apm_power.o obj-$(CONFIG_MAX8925_POWER) += max8925_power.o
drivers/power/charging_algo_pse.c new file +203 -0
203 lines not on this page · raw message
include/linux/power/power_supply_charger.h +66 -0
74 lines not on this page · raw message
Signature · 1 line
1.7.9.5
PATCH v11 4/4 power_supply: bq24261 charger driver
In reply to Jenny TCThis patch introduces BQ24261 charger driver. The driver makes use of power supply charging driver to setup charging. So the driver does hardware abstraction and handles h/w specific corner cases. The charging logic resides with power supply charging driver
--- drivers/power/Kconfig | 10 + drivers/power/Makefile | 1 + drivers/power/bq24261_charger.c | 1348 +++++++++++++++++++++++++++++++++ include/linux/power/bq24261-charger.h | 25 + 4 files changed, 1384 insertions(+) create mode 100644 drivers/power/bq24261_charger.c create mode 100644 include/linux/power/bq24261-charger.h
drivers/power/Kconfig +10 -0
@@ -411,6 +411,16 @@ config BATTERY_GOLDFISH Say Y to enable support for the battery and AC power in the Goldfish emulator. +config CHARGER_BQ24261 + tristate "BQ24261 charger driver" + select POWER_SUPPLY_CHARGER + depends on I2C + help + Say Y to include support for BQ24261 Charger driver. This driver + makes use of power supply charging driver. So the driver gives + the charger hardware abstraction only. Charging logic is abstracted + in the power supply charging driver. + source "drivers/power/reset/Kconfig" endif # POWER_SUPPLY
drivers/power/Makefile +1 -0
@@ -59,4 +59,5 @@ obj-$(CONFIG_CHARGER_BQ24735) += bq24735-charger.o obj-$(CONFIG_POWER_AVS) += avs/ obj-$(CONFIG_CHARGER_SMB347) += smb347-charger.o obj-$(CONFIG_CHARGER_TPS65090) += tps65090-charger.o +obj-$(CONFIG_CHARGER_BQ24261) += bq24261_charger.o obj-$(CONFIG_POWER_RESET) += reset/
drivers/power/bq24261_charger.c new file +1348 -0
1,348 lines not on this page · raw message
include/linux/power/bq24261-charger.h new file +25 -0
@@ -0,0 +1,25 @@ +/* +* bq24261_charger.h: platform data structure for bq24261 driver +* +* (C) Copyright 2012 Intel Corporation +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; version 2 +* of the License. +*/ + +#ifndef __BQ24261_CHARGER_H__ +#define __BQ24261_CHARGER_H__ + +struct bq24261_plat_data { +char **supplied_to; +size_t num_supplicants; +struct psy_throttle_state *throttle_states; +size_t num_throttle_states; +int safety_timer; +bool is_ts_enabled; + +}; + +#endif
Signature · 1 line
1.7.9.5