Skip to contentSkip to conversation

power_supply: Introduce charging Framework

PATCH In review Jenny TC 2 participants 12 messages Oct 18, 2012 – Nov 19, 2012 Power supply

lore
Share & tools
lore

Jenny TC

author
#0 lore raw

cover power_supply: Introduce charging Framework

The Power Supply charging framework connects multiple subsystems to do charging in a generic way. The subsystems involves power_supply, extcon, and thermal. With this the charging is handled in a generic way.

The framework makes use of different new features - Battery Identification framework, pluggable charging algorithms, charger cable arbitrations. PSE compliant charging algorithm also enabled as part of this patchset.

With this framework the charging logic can be kept outside the charger driver. The charger driver just need to expose th get_property and set_property functions to get and set a specific set of power supply properties. The driver can convert these values to the hardware configurations to setup charging.

Jenny TC (7):
power_supply : Introduce battery identification framework
power_supply: Add charger control properties power_supply : add supported charger cable feature
power_supply: add throttle state
power_supply: Introduce Power Supply charging framework
power_supply: enable charger framework callbacks
power_supply: Introduce PSE compliant algorithm

 drivers/power/Kconfig                |   28 ++
 drivers/power/Makefile               |    3 +
 drivers/power/battery_id.c           |   94 +++++
 drivers/power/charging_algo_pse.c    |   79 +++++
 drivers/power/power_supply.h         |   20 ++
 drivers/power/power_supply_charger.c |  634 ++++++++++++++++++++++++++++++++++
 drivers/power/power_supply_charger.h |  165 +++++++++
 drivers/power/power_supply_core.c    |   12 +
 drivers/power/power_supply_sysfs.c   |   16 +-
 include/linux/power/battery_id.h     |   97 ++++++
 include/linux/power_supply.h         |   53 ++-
 11 files changed, 1189 insertions(+), 12 deletions(-)
 create mode 100644 drivers/power/battery_id.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/battery_id.h
Signature · 1 line
1.7.9.5
author
#1 lore raw

PATCH 1/7 power_supply : Introduce battery identification framework

This patch introduces generic battid framework. Different battid drivers sitting on different linux kernel subsystem (1wire, I2C, SFI etc) can interface with the power supply susbsytem using the APIs exposed in the power supply usbsystem. The consumers (charger driver/battery driver/power management drivers) can register for notification from the battery id drivers using the APIs from this framework

---
 drivers/power/Kconfig            |    8 ++++
 drivers/power/Makefile           |    1 +
 drivers/power/battery_id.c       |   94 ++++++++++++++++++++++++++++++++++++++
 include/linux/power/battery_id.h |   50 ++++++++++++++++++++
 4 files changed, 153 insertions(+)
 create mode 100644 drivers/power/battery_id.c
 create mode 100644 include/linux/power/battery_id.h
drivers/power/Kconfig +8 -0
index 49a8939..74b297d 100644
@@ -8,6 +8,14 @@ menuconfig POWER_SUPPLY
 
 if POWER_SUPPLY
 
+config POWER_SUPPLY_BATTID
+	bool "Power Supply Battery Identification Framework"
+	help
+	 Say Y here to enable the power supply battery idnetification
+	 framework. The framework would allow different battery identification
+	 drivers to interface with power supply subsystem. Also it allows consumer
+	 drivers to register for notification from the power_supply subsystem.
+
 config POWER_SUPPLY_DEBUG
 	bool "Power supply debug"
 	help
drivers/power/Makefile +1 -0
index b949cf8..6a63f45 100644
@@ -3,6 +3,7 @@ ccflags-$(CONFIG_POWER_SUPPLY_DEBUG) := -DDEBUG
 power_supply-y				:= power_supply_core.o
 power_supply-$(CONFIG_SYSFS)		+= power_supply_sysfs.o
 power_supply-$(CONFIG_LEDS_TRIGGERS)	+= power_supply_leds.o
+power_supply-$(CONFIG_POWER_SUPPLY_BATTID) += battery_id.o
 
 obj-$(CONFIG_POWER_SUPPLY)	+= power_supply.o
 obj-$(CONFIG_GENERIC_ADC_BATTERY)	+= generic-adc-battery.o
drivers/power/battery_id.c new file +94 -0

94 lines not on this page · raw message

include/linux/power/battery_id.h new file +50 -0

50 lines not on this page · raw message

Signature · 1 line
1.7.9.5
author
#2 lore raw

PATCH 2/7 power_supply: Add charger control properties

In reply to Jenny TC

The battery charger needs to have control path along
with the reporting charger properties. In existing solutions this is implemented using regulator framework. A regulator framework doesn't fit a charger driver requirement because of the following reason
Charger needs support two paths - charger path (charger to platform) and charging (charger to battery).Disabling the charging path alone (eg over battery temperature) will allow the platform to work with power from charger without discharging the battery. And the charger may need to be disabled completely based on the charger temperature or the platform temperature.
Charger has more than one pair of voltage/current to control (CC,CV,INLMT) These features will not directly fit in the regulator framework

Since the charger driver sits in the power supply subsystem it make sense to add the properties to control the charger.

Also this patch adds/modify the power supply properties to meet the requirements for charger control

---
 drivers/power/power_supply_sysfs.c |   16 ++++++++++++----
 include/linux/power_supply.h       |   24 ++++++++++++++++--------
 2 files changed, 28 insertions(+), 12 deletions(-)
drivers/power/power_supply_sysfs.c +12 -4
index 40fa3b7..3166d00 100644
@@ -160,12 +160,13 @@ static struct device_attribute power_supply_attrs[] = {
 	POWER_SUPPLY_ATTR(charge_now),
 	POWER_SUPPLY_ATTR(charge_avg),
 	POWER_SUPPLY_ATTR(charge_counter),
-	POWER_SUPPLY_ATTR(constant_charge_current),
-	POWER_SUPPLY_ATTR(constant_charge_current_max),
-	POWER_SUPPLY_ATTR(constant_charge_voltage),
-	POWER_SUPPLY_ATTR(constant_charge_voltage_max),
+	POWER_SUPPLY_ATTR(charge_current),
+	POWER_SUPPLY_ATTR(max_charge_current),
+	POWER_SUPPLY_ATTR(charge_voltage),
+	POWER_SUPPLY_ATTR(max_charge_voltage),
 	POWER_SUPPLY_ATTR(charge_control_limit),
 	POWER_SUPPLY_ATTR(charge_control_limit_max),
+	POWER_SUPPLY_ATTR(input_cur_limit),
 	POWER_SUPPLY_ATTR(energy_full_design),
 	POWER_SUPPLY_ATTR(energy_empty_design),
 	POWER_SUPPLY_ATTR(energy_full),
@@ -177,6 +178,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(max_temp),
+	POWER_SUPPLY_ATTR(min_temp),
 	POWER_SUPPLY_ATTR(temp_alert_min),
 	POWER_SUPPLY_ATTR(temp_alert_max),
 	POWER_SUPPLY_ATTR(temp_ambient),
@@ -188,6 +191,11 @@ 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_cur),
+	POWER_SUPPLY_ATTR(enable_charging),
+	POWER_SUPPLY_ATTR(enable_charger),
+	POWER_SUPPLY_ATTR(cable_type),
+	POWER_SUPPLY_ATTR(priority),
 	/* Properties of type `const char *' */
 	POWER_SUPPLY_ATTR(model_name),
 	POWER_SUPPLY_ATTR(manufacturer),
include/linux/power_supply.h +16 -8
index 1f0ab90..7c06956 100644
@@ -110,12 +110,13 @@ enum power_supply_property {
 	POWER_SUPPLY_PROP_CHARGE_NOW,
 	POWER_SUPPLY_PROP_CHARGE_AVG,
 	POWER_SUPPLY_PROP_CHARGE_COUNTER,
-	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
-	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
-	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
-	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
+	POWER_SUPPLY_PROP_CHARGE_CURRENT,
+	POWER_SUPPLY_PROP_MAX_CHARGE_CURRENT,
+	POWER_SUPPLY_PROP_CHARGE_VOLTAGE,
+	POWER_SUPPLY_PROP_MAX_CHARGE_VOLTAGE,
 	POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT,
 	POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX,
+	POWER_SUPPLY_PROP_INLMT,
 	POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
 	POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN,
 	POWER_SUPPLY_PROP_ENERGY_FULL,
@@ -127,6 +128,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_MAX_TEMP,
+	POWER_SUPPLY_PROP_MIN_TEMP,
 	POWER_SUPPLY_PROP_TEMP_ALERT_MIN,
 	POWER_SUPPLY_PROP_TEMP_ALERT_MAX,
 	POWER_SUPPLY_PROP_TEMP_AMBIENT,
@@ -138,6 +141,11 @@ 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_CUR,
+	POWER_SUPPLY_PROP_ENABLE_CHARGING,
+	POWER_SUPPLY_PROP_ENABLE_CHARGER,
+	POWER_SUPPLY_PROP_CABLE_TYPE,
+	POWER_SUPPLY_PROP_PRIORITY,
 	/* Properties of type `const char *' */
 	POWER_SUPPLY_PROP_MODEL_NAME,
 	POWER_SUPPLY_PROP_MANUFACTURER,
@@ -253,8 +261,8 @@ static inline bool power_supply_is_amp_property(enum power_supply_property psp)
 	case POWER_SUPPLY_PROP_CHARGE_NOW:
 	case POWER_SUPPLY_PROP_CHARGE_AVG:
 	case POWER_SUPPLY_PROP_CHARGE_COUNTER:
-	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
-	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
+	case POWER_SUPPLY_PROP_CHARGE_CURRENT:
+	case POWER_SUPPLY_PROP_MAX_CHARGE_CURRENT:
 	case POWER_SUPPLY_PROP_CURRENT_MAX:
 	case POWER_SUPPLY_PROP_CURRENT_NOW:
 	case POWER_SUPPLY_PROP_CURRENT_AVG:
@@ -281,9 +289,9 @@ static inline bool power_supply_is_watt_property(enum power_supply_property psp)
 	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
 	case POWER_SUPPLY_PROP_VOLTAGE_AVG:
+	case POWER_SUPPLY_PROP_CHARGE_VOLTAGE:
 	case POWER_SUPPLY_PROP_VOLTAGE_OCV:
-	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
-	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
+	case POWER_SUPPLY_PROP_MAX_CHARGE_VOLTAGE:
 	case POWER_SUPPLY_PROP_POWER_NOW:
 		return 1;
 	default:
Signature · 1 line
1.7.9.5
#3 lore raw

Re: [PATCH 2/7] power_supply: Add charger control properties

Quoted 31 lines · Jenny TC

The battery charger needs to have control path along
with the reporting charger properties. In existing solutions this is implemented using regulator framework. A regulator framework doesn't fit a charger driver requirement because of the following reason
Charger needs support two paths - charger path (charger to platform) and charging (charger to battery).Disabling the charging path alone (eg over battery temperature) will allow the platform to work with power from charger without discharging the battery. And the charger may need to be disabled completely based on the charger temperature or the platform temperature.
Charger has more than one pair of voltage/current to control (CC,CV,INLMT) These features will not directly fit in the regulator framework

Since the charger driver sits in the power supply subsystem it make sense to add the properties to control the charger.

Also this patch adds/modify the power supply properties to meet the requirements for charger control

---
 drivers/power/power_supply_sysfs.c |   16 ++++++++++++----
 include/linux/power_supply.h       |   24 ++++++++++++++++--------
 2 files changed, 28 insertions(+), 12 deletions(-)
drivers/power/power_supply_sysfs.c +0 -0
index 40fa3b7..3166d00 100644

drivers/power/power_supply_sysfs.c static struct device_attribute power_supply_attrs[] = { · line 166

Quoted 7 lines
drivers/power/power_supply_sysfs.c +0 -4
@@ -160,12 +160,13 @@ static struct device_attribute power_supply_attrs[] = {
 	POWER_SUPPLY_ATTR(charge_now),
 	POWER_SUPPLY_ATTR(charge_avg),
 	POWER_SUPPLY_ATTR(charge_counter),
-	POWER_SUPPLY_ATTR(constant_charge_current),
-	POWER_SUPPLY_ATTR(constant_charge_current_max),
-	POWER_SUPPLY_ATTR(constant_charge_voltage),
-	POWER_SUPPLY_ATTR(constant_charge_voltage_max),

Sorry, but you can't just remove properites like this. They're already used by a lot of drivers.

Thanks,
Anton.

author
#4 lore raw

PATCH 3/7 power_supply : add supported charger cable feature

In reply to Jenny TC

A charger can support different types of charger sources (cables). It make sense to define a field to inform the power supply subsystem what kind of cable a charger driver supports. Since a bitmask would be the easy way to do define, it's good to have a enum which has the bitmask definition for each cable types

---
 include/linux/power_supply.h |   16 ++++++++++++++++
 1 file changed, 16 insertions(+)
include/linux/power_supply.h +16 -0
index 7c06956..eea1709 100644
@@ -163,6 +163,21 @@ enum power_supply_type {
 	POWER_SUPPLY_TYPE_USB_ACA,	/* Accessory Charger Adapters */
 };
 
+enum power_supply_charger_cable_type {
+	POWER_SUPPLY_CHARGER_TYPE_NONE = 0,
+	POWER_SUPPLY_CHARGER_TYPE_USB_SDP = 1 << 0,
+	POWER_SUPPLY_CHARGER_TYPE_USB_DCP = 1 << 1,
+	POWER_SUPPLY_CHARGER_TYPE_USB_CDP = 1 << 2,
+	POWER_SUPPLY_CHARGER_TYPE_USB_ACA = 1 << 3,
+	POWER_SUPPLY_CHARGER_TYPE_AC = 1 << 4,
+};
+
+#define POWER_SUPPLY_CHARGER_TYPE_USB \
+	(POWER_SUPPLY_CHARGER_TYPE_USB_SDP | \
+	POWER_SUPPLY_CHARGER_TYPE_USB_DCP | \
+	POWER_SUPPLY_CHARGER_TYPE_USB_CDP | \
+	POWER_SUPPLY_CHARGER_TYPE_USB_ACA)
+
 union power_supply_propval {
 	int intval;
 	const char *strval;
@@ -175,6 +190,7 @@ struct power_supply {
 	size_t num_properties;
 
 	char **supplied_to;
+	unsigned long supported_cables;
 	size_t num_supplicants;
 
 	int (*get_property)(struct power_supply *psy,
Signature · 1 line
1.7.9.5
#5 lore raw

Re: [PATCH 3/7] power_supply : add supported charger cable feature

Quoted 38 lines · Jenny TC

A charger can support different types of charger sources (cables). It make sense to define a field to inform the power supply subsystem what kind of cable a charger driver supports. Since a bitmask would be the easy way to do define, it's good to have a enum which has the bitmask definition for each cable types

---
 include/linux/power_supply.h |   16 ++++++++++++++++
 1 file changed, 16 insertions(+)
include/linux/power_supply.h +15 -0
index 7c06956..eea1709 100644
@@ -163,6 +163,21 @@ enum power_supply_type {
 	POWER_SUPPLY_TYPE_USB_ACA,	/* Accessory Charger Adapters */
 };
 
+enum power_supply_charger_cable_type {
+	POWER_SUPPLY_CHARGER_TYPE_NONE = 0,
+	POWER_SUPPLY_CHARGER_TYPE_USB_SDP = 1 << 0,
+	POWER_SUPPLY_CHARGER_TYPE_USB_DCP = 1 << 1,
+	POWER_SUPPLY_CHARGER_TYPE_USB_CDP = 1 << 2,
+	POWER_SUPPLY_CHARGER_TYPE_USB_ACA = 1 << 3,
+	POWER_SUPPLY_CHARGER_TYPE_AC = 1 << 4,
+};
+
+#define POWER_SUPPLY_CHARGER_TYPE_USB \
+	(POWER_SUPPLY_CHARGER_TYPE_USB_SDP | \
+	POWER_SUPPLY_CHARGER_TYPE_USB_DCP | \
+	POWER_SUPPLY_CHARGER_TYPE_USB_CDP | \
+	POWER_SUPPLY_CHARGER_TYPE_USB_ACA)
+
 union power_supply_propval {
 	int intval;
 	const char *strval;

include/linux/power_supply.h struct power_supply { · line 193

Quoted 4 lines
include/linux/power_supply.h +1 -0
@@ -175,6 +190,7 @@ struct power_supply {
 	size_t num_properties;
 
 	char **supplied_to;
+	unsigned long supported_cables;

Enums have an integer resolution. Why do we need long type here?..

Thanks.

Quoted 5 lines
include/linux/power_supply.h +0 -0
 	size_t num_supplicants;
 
 	int (*get_property)(struct power_supply *psy,
Signature · 1 line
1.7.9.5
author
#6 lore raw

PATCH 4/7 power_supply: add throttle state

In reply to Jenny TC

The charger and battery temperature contribute to the platform thermal. The only way to control the temperature
is to control the charging. The charging can be controlled in different way. This could be disabling charger, disabling charging, adjusting CC, or by adjusting the INLMT. This patch adds a structure to define the charger throttle actions. Also this patch adds a throttle_states field to the struct power_supply which can be used by the charger driver to define it's throttle actions for different states

---
 include/linux/power_supply.h |   13 +++++++++++++
 1 file changed, 13 insertions(+)
include/linux/power_supply.h +13 -0
index eea1709..b4eb0af 100644
@@ -183,6 +183,18 @@ union power_supply_propval {
 	const char *strval;
 };
 
+enum psy_throttle_action {
+
+	PSY_THROTTLE_DISABLE_CHARGER = 0,
+	PSY_THROTTLE_DISABLE_CHARGING,
+	PSY_THROTTLE_CC_LIMIT,
+	PSY_THROTTLE_INPUT_LIMIT,
+};
+
+struct power_supply_throttle {
+	enum psy_throttle_action throttle_action;
+	unsigned throttle_val;
+};
 struct power_supply {
 	const char *name;
 	enum power_supply_type type;
@@ -192,6 +204,7 @@ struct power_supply {
 	char **supplied_to;
 	unsigned long supported_cables;
 	size_t num_supplicants;
+	struct power_supply_throttle *throttle_states;
 
 	int (*get_property)(struct power_supply *psy,
 			    enum power_supply_property psp,
Signature · 1 line
1.7.9.5
#7 lore raw

Re: [PATCH 4/7] power_supply: add throttle state

Quoted 19 lines · Jenny TC

The charger and battery temperature contribute to the platform thermal. The only way to control the temperature
is to control the charging. The charging can be controlled in different way. This could be disabling charger, disabling charging, adjusting CC, or by adjusting the INLMT. This patch adds a structure to define the charger throttle actions. Also this patch adds a throttle_states field to the struct power_supply which can be used by the charger driver to define it's throttle actions for different states

---
 include/linux/power_supply.h |   13 +++++++++++++
 1 file changed, 13 insertions(+)
include/linux/power_supply.h +0 -0
index eea1709..b4eb0af 100644

include/linux/power_supply.h union power_supply_propval { · line 187

Quoted 5 lines
include/linux/power_supply.h +2 -0
@@ -183,6 +183,18 @@ union power_supply_propval {
 	const char *strval;
 };
 
+enum psy_throttle_action {
+

No need for this empty line.

include/linux/power_supply.h union power_supply_propval { · line 196

Quoted 9 lines
include/linux/power_supply.h +9 -0
+	PSY_THROTTLE_DISABLE_CHARGER = 0,
+	PSY_THROTTLE_DISABLE_CHARGING,
+	PSY_THROTTLE_CC_LIMIT,
+	PSY_THROTTLE_INPUT_LIMIT,
+};
+
+struct power_supply_throttle {
+	enum psy_throttle_action throttle_action;
+	unsigned throttle_val;

Let's be consistent. unsigned int.

include/linux/power_supply.h union power_supply_propval { · line 197

Quoted 1 line
include/linux/power_supply.h +1 -0
+};

Empty line is needed here.

Quoted 13 lines
include/linux/power_supply.h +1 -0
 struct power_supply {
 	const char *name;
 	enum power_supply_type type;
@@ -192,6 +204,7 @@ struct power_supply {
 	char **supplied_to;
 	unsigned long supported_cables;
 	size_t num_supplicants;
+	struct power_supply_throttle *throttle_states;
 
 	int (*get_property)(struct power_supply *psy,
 			    enum power_supply_property psp,
Signature · 1 line
1.7.9.5
author
#8 lore raw

PATCH 5/7 power_supply: Introduce Power Supply charging framework

In reply to Jenny TC

The Power Supply charging framework connects multiple subsystems to do charging in a generic way. The subsystems involves power_supply, extcon, and thermal. With this the charging is handled in a generic way.

The framework makes use of different new features - Battery Identification framework, pluggable charging algorithms, charger cable arbitrations etc.

At present 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 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. Charging framework uses selected 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 framework to plug algorithms from the user space.

The framework 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. The arbitration logic inside the framework selects the cable based on it's capabilities and the maximum charge current the platform can support

Also this framework exposes features to control charging on different platform states. One such feature is thermal. The framework register with the thermal subsystem and control charging based on the thermal subsystem requirements.

Overall this framework removes the charging logic out of the charger driver and the charger driver can just listen to the request from the framework to set the charger properties. This can be implemented by exposing get_property and set property callbacks.

---
 drivers/power/Kconfig                |    8 +
 drivers/power/Makefile               |    1 +
 drivers/power/power_supply_charger.c |  634 ++++++++++++++++++++++++++++++++++
 drivers/power/power_supply_charger.h |  165 +++++++++
 4 files changed, 808 insertions(+)
 create mode 100644 drivers/power/power_supply_charger.c
 create mode 100644 drivers/power/power_supply_charger.h
drivers/power/Kconfig +8 -0
index 74b297d..34850e4 100644
@@ -8,6 +8,14 @@ menuconfig POWER_SUPPLY
 
 if POWER_SUPPLY
 
+config POWER_SUPPLY_CHARGER
+	bool "Power Supply Charger"
+	help
+	  Say Y here to enable the power supply charger framework. Charger
+	  framework 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 POWER_SUPPLY_BATTID
 	bool "Power Supply Battery Identification Framework"
 	help
drivers/power/Makefile +1 -0
index 6a63f45..ce8c230 100644
@@ -3,6 +3,7 @@ ccflags-$(CONFIG_POWER_SUPPLY_DEBUG) := -DDEBUG
 power_supply-y				:= power_supply_core.o
 power_supply-$(CONFIG_SYSFS)		+= power_supply_sysfs.o
 power_supply-$(CONFIG_LEDS_TRIGGERS)	+= power_supply_leds.o
+power_supply-$(CONFIG_POWER_SUPPLY_CHARGER) += power_supply_charger.o
 power_supply-$(CONFIG_POWER_SUPPLY_BATTID) += battery_id.o
 
 obj-$(CONFIG_POWER_SUPPLY)	+= power_supply.o
drivers/power/power_supply_charger.c new file +634 -0

634 lines not on this page · raw message

drivers/power/power_supply_charger.h new file +165 -0

165 lines not on this page · raw message

Signature · 1 line
1.7.9.5
#9 lore raw

Re: [PATCH 5/7] power_supply: Introduce Power Supply charging framework

Quoted 3 lines · Jenny TC

The Power Supply charging framework connects multiple subsystems to do charging in a generic way. The subsystems involves power_supply, extcon, and thermal. With this the charging is handled in a generic way.

[...]

drivers/power/power_supply_charger.c line 390

Quoted 9 lines
drivers/power/power_supply_charger.c new file +9 -0
+void power_supply_trigger_charging_handler(struct power_supply *psy)
+{
+	int i;
+	struct power_supply *psb = NULL;
+
+	if (!psy_chrgr.is_cable_evt_reg)
+		return;
+
+	if (is_trigger_charging_algo(psy)) {

if (!is_trigger_charging_algo(psy))

	return;
Quoted 26 lines
drivers/power/power_supply_charger.c new file +26 -0
+		if (IS_BATTERY(psy)) {
+			if (trigger_algo(psy))
+				enable_supplied_by_charging(psy, false);
+			else
+				enable_supplied_by_charging(psy, true);
+
+		} else if (IS_CHARGER(psy)) {
+			for (i = 0; i < psy->num_supplicants; i++) {
+				psb =
+				    power_supply_get_by_name(psy->
+							     supplied_to[i]);
+
+				if (psb && IS_BATTERY(psb) && IS_PRESENT(psb)) {
+					if (trigger_algo(psb)) {
+						disable_charging(psy);
+						break;
+					} else if (IS_CHARGING_CAN_BE_ENABLED
+								(psy)) {
+						enable_charging(psy);
+					}
+				}
+			}
+		}
+
+	}
+}

[...]

drivers/power/power_supply_charger.h line 4

Quoted 5 lines
drivers/power/power_supply_charger.h new file +4 -0
@@ -0,0 +1,165 @@
+#include <linux/power_supply.h>
+
+#ifndef __POWER_SUPPLY_CHARGER_H__
+

No need for this empty line.

Quoted 11 lines
drivers/power/power_supply_charger.h new file +11 -0
+#define __POWER_SUPPLY_CHARGER_H__
+
+struct batt_props {
+	struct list_head node;
+	const char *name;
+	unsigned long voltage_now;
+	long current_now;
+	int temperature;
+	long status;
+};
+

drivers/power/power_supply_charger.h line 25

Quoted 10 lines
drivers/power/power_supply_charger.h new file +10 -0
+struct charger_props {
+	struct list_head node;
+	const char *name;
+	bool present;
+	bool status;
+	bool online;
+	unsigned long cable;
+};
+
+struct charging_algo {

I guess it's better to prefix these w/ psy_ or power_.

Quoted 87 lines
drivers/power/power_supply_charger.h new file +87 -0
+	struct list_head node;
+	unsigned int chrg_prof_type;
+	char *name;
+	int (*get_next_cc_cv)(struct batt_props, struct ps_batt_chg_prof,
+				unsigned long *cc, unsigned long *cv);
+};
+
+extern int power_supply_register_charging_algo(struct charging_algo *);
+extern int power_supply_unregister_charging_algo(struct charging_algo *);
+
+static inline int set_ps_int_property(struct power_supply *psy,
+				      enum power_supply_property psp,
+				      int prop_val)
+{
+
+	union power_supply_propval val;
+
+	val.intval = prop_val;
+	return psy->set_property(psy, psp, &val);
+}
+
+static inline int get_ps_int_property(struct power_supply *psy,
+				      enum power_supply_property psp)
+{
+	union power_supply_propval val;
+
+	psy->get_property(psy, psp, &val);
+	return val.intval;
+}
+
+#define enable_charging(psy) \
+		({if ((CABLE_TYPE(psy) != POWER_SUPPLY_CHARGER_TYPE_NONE) &&\
+			!IS_CHARGING_ENABLED(psy)) \
+		set_ps_int_property(psy, POWER_SUPPLY_PROP_ENABLE_CHARGING,\
+					true);\
+		enable_charger(psy); })
+#define disable_charging(psy) \
+		({if ((CABLE_TYPE(psy) != POWER_SUPPLY_CHARGER_TYPE_NONE) &&\
+				IS_CHARGING_ENABLED(psy)) \
+		set_ps_int_property(psy,\
+				POWER_SUPPLY_PROP_ENABLE_CHARGING, false); })
+
+#define enable_charger(psy) \
+		set_ps_int_property(psy, POWER_SUPPLY_PROP_ENABLE_CHARGER, true)
+#define disable_charger(psy) \
+		set_ps_int_property(psy,\
+				POWER_SUPPLY_PROP_ENABLE_CHARGER, false)
+
+#define set_cc(psy, cc) \
+		set_ps_int_property(psy, POWER_SUPPLY_PROP_CHARGE_CURRENT, cc)
+
+#define set_cv(psy, cv) \
+		set_ps_int_property(psy, POWER_SUPPLY_PROP_CHARGE_VOLTAGE, cv)
+
+#define set_inlmt(psy, inlmt) \
+		set_ps_int_property(psy, POWER_SUPPLY_PROP_INLMT, inlmt)
+#define SET_MAX_CC(psy, max_cc) \
+		set_ps_int_property(psy,\
+				POWER_SUPPLY_PROP_MAX_CHARGE_CURRENT, max_cc)
+#define switch_cable(psy, new_cable) \
+		set_ps_int_property(psy,\
+				POWER_SUPPLY_PROP_CABLE_TYPE, new_cable)
+
+#define CV(psy) \
+		get_ps_int_property(psy, POWER_SUPPLY_PROP_CHARGE_VOLTAGE)
+#define CC(psy) \
+		get_ps_int_property(psy, POWER_SUPPLY_PROP_CHARGE_CURRENT)
+#define INLMT(psy) \
+		get_ps_int_property(psy, POWER_SUPPLY_PROP_INLMT)
+#define MAX_CC(psy) \
+		get_ps_int_property(psy, POWER_SUPPLY_PROP_MAX_CHARGE_CURRENT)
+#define MAX_CV(psy) \
+		get_ps_int_property(psy, POWER_SUPPLY_PROP_MAX_CHARGE_VOLTAGE)
+#define VOLTAGE_NOW(psy) \
+		get_ps_int_property(psy, POWER_SUPPLY_PROP_VOLTAGE_NOW)
+#define CURRENT_NOW(psy) \
+		get_ps_int_property(psy, POWER_SUPPLY_PROP_CURRENT_NOW)
+#define STATUS(psy) \
+		get_ps_int_property(psy, POWER_SUPPLY_PROP_STATUS)
+#define TEMPERATURE(psy) \
+		get_ps_int_property(psy, POWER_SUPPLY_PROP_TEMP)
+#define BATTERY_TYPE(psy) \
+		get_ps_int_property(psy, POWER_SUPPLY_PROP_TECHNOLOGY)
+#define PRIORITY(psy) \
+		get_ps_int_property(psy, POWER_SUPPLY_PROP_PRIORITY)
+#define CABLE_TYPE(psy) \
+		get_ps_int_property(psy, POWER_SUPPLY_PROP_CABLE_TYPE)

drivers/power/power_supply_charger.h line 122

Quoted 10 lines
drivers/power/power_supply_charger.h new file +10 -0
+
+#define IS_CHARGING_ENABLED(psy) \
+		get_ps_int_property(psy, POWER_SUPPLY_PROP_ENABLE_CHARGING)
+#define IS_CHARGER_ENABLED(psy) \
+		get_ps_int_property(psy, POWER_SUPPLY_PROP_ENABLE_CHARGER)
+#define IS_BATTERY(psy) (psy->type == POWER_SUPPLY_TYPE_BATTERY)
+#define IS_CHARGER(psy) (psy->type == POWER_SUPPLY_TYPE_USB ||\
+				psy->type == POWER_SUPPLY_TYPE_USB_CDP || \
+			psy->type == POWER_SUPPLY_TYPE_USB_DCP ||\
+			psy->type == POWER_SUPPLY_TYPE_USB_ACA)

I like these helpers. Don't hesitate to introduce them as much as you like, they are useful and make the code more readable.

But can we make them generic and static inlines? Otherwise the helpers themselves are not very readable. :)

static bool psy_is_charger(struct power_supply *psy)

{
	return ....
}

It's more lines of code, but the code is human-readable and maintainable.

And place them into include/linux/power_supply.h.

Quoted 45 lines
drivers/power/power_supply_charger.h new file +43 -0
+#define IS_ONLINE(psy) \
+		(get_ps_int_property(psy, POWER_SUPPLY_PROP_ONLINE) == 1)
+#define IS_PRESENT(psy) \
+		(get_ps_int_property(psy, POWER_SUPPLY_PROP_PRESENT) == 1)
+#define IS_SUPPORTED_CABLE(psy, cable_type) \
+		(psy->supported_cables & cable_type)
+#define IS_CABLE_ACTIVE(status) \
+	((status != EXTCON_CHRGR_CABLE_DISCONNECTED) ||\
+			(status != EXTCON_CHRGR_CABLE_SUSPENDED))
+
+#define IS_CHARGER_PROP_CHANGED(prop, cache_prop)\
+	((cache_prop.online != prop.online) || \
+	(cache_prop.present != prop.present))
+
+#define IS_BAT_PROP_CHANGED(bat_prop, bat_cache)\
+	((bat_cache.voltage_now != bat_prop.voltage_now) || \
+	(bat_cache.current_now != bat_prop.current_now) || \
+	(bat_cache.temperature != bat_prop.temperature))
+
+#define THROTTLE_ACTION(psy, state)\
+		(((psy->throttle_states)+state)->throttle_action)
+
+#define MAX_THROTTLE_STATE(psy)\
+		(get_ps_int_property(psy,\
+			POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX))
+
+#define CURRENT_THROTTLE_STATE(psy)\
+		(get_ps_int_property(psy,\
+			POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT))
+
+#define CURRENT_THROTTLE_ACTION(psy)\
+		THROTTLE_ACTION(psy, CURRENT_THROTTLE_STATE(psy))
+
+#define THROTTLE_CC_VALUE(psy, state)\
+		(((psy->throttle_states)+state)->throttle_val)
+
+#define IS_CHARGING_CAN_BE_ENABLED(psy) \
+	((CURRENT_THROTTLE_ACTION(psy) != PSY_THROTTLE_DISABLE_CHARGER)  &&\
+	(CURRENT_THROTTLE_ACTION(psy) != PSY_THROTTLE_DISABLE_CHARGING))
+#define IS_CHARGER_CAN_BE_ENABLED(psy) \
+	(CURRENT_THROTTLE_ACTION(psy) != PSY_THROTTLE_DISABLE_CHARGER)
+
+#endif
Signature · 1 line
1.7.9.5
author
#10 lore raw

PATCH 6/7 power_supply: enable charger framework callbacks

In reply to Jenny TC

The charger framework needs to be notified on registering a new charger, on power supply changed event or on a thermal throttle request. This patch enables support for the same

---
 drivers/power/power_supply.h      |   20 ++++++++++++++++++++
 drivers/power/power_supply_core.c |   12 ++++++++++++
 2 files changed, 32 insertions(+)
drivers/power/power_supply.h +20 -0
index cc439fd..ae2d66c 100644
@@ -40,3 +40,23 @@ static inline int power_supply_create_triggers(struct power_supply *psy)
 static inline void power_supply_remove_triggers(struct power_supply *psy) {}
 
 #endif /* CONFIG_LEDS_TRIGGERS */
+#ifdef CONFIG_POWER_SUPPLY_CHARGER
+
+extern void power_supply_trigger_charging_handler(struct power_supply *psy);
+extern int power_supply_register_charger(struct power_supply *psy);
+extern int power_supply_unregister_charger(struct power_supply *psy);
+extern int psy_charger_throttle_charger(struct power_supply *psy,
+					unsigned long state);
+
+#else
+
+static inline void power_supply_trigger_charging_handler(struct power_supply *psy) { }
+static inline int power_supply_register_charger(struct power_supply *psy) 
+{ return 0; }
+static inline int power_supply_unregister_charger(struct power_supply *psy) 
+{ return 0; }
+static inline int psy_charger_throttle_charger(struct power_supply *psy,
+					unsigned long state)
+{ return 0; }
+
+#endif
drivers/power/power_supply_core.c +12 -0
index 3338d49..73ac6e4 100644
@@ -19,6 +19,7 @@
 #include <linux/power_supply.h>
 #include <linux/thermal.h>
 #include "power_supply.h"
+#include "power_supply_charger.h"
 
 /* exported for the APM Power driver, APM emulation */
 struct class *power_supply_class;
@@ -49,6 +50,7 @@ static void power_supply_changed_work(struct work_struct *work)
 
 	class_for_each_device(power_supply_class, NULL, psy,
 			      __power_supply_changed_work);
+		power_supply_trigger_charging_handler(psy);
 
 	power_supply_update_leds(psy);
 
@@ -265,6 +267,8 @@ 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);
 
+	psy_charger_throttle_charger(psy,state);	
+
 	return ret;
 }
 
@@ -360,10 +364,16 @@ int power_supply_register(struct device *parent, struct power_supply *psy)
 	if (rc)
 		goto create_triggers_failed;
 
+	if (IS_CHARGER(psy))
+		rc = power_supply_register_charger(psy);
+	if (rc)
+		goto charger_register_failed;
+
 	power_supply_changed(psy);
 
 	goto success;
 
+charger_register_failed:
 create_triggers_failed:
 	psy_unregister_cooler(psy);
 register_cooler_failed:
@@ -382,6 +392,8 @@ void power_supply_unregister(struct power_supply *psy)
 {
 	cancel_work_sync(&psy->changed_work);
 	sysfs_remove_link(&psy->dev->kobj, "powers");
+	if (IS_CHARGER(psy))
+		 power_supply_unregister_charger(psy);
 	power_supply_remove_triggers(psy);
 	psy_unregister_cooler(psy);
 	psy_unregister_thermal(psy);
Signature · 1 line
1.7.9.5
author
#11 lore raw

PATCH 7/7 power_supply: Introduce PSE compliant algorithm

In reply to Jenny TC

As per PSE standard 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 charging framework based on the type of the battery charging profile.

---
 drivers/power/Kconfig             |   12 ++++++
 drivers/power/Makefile            |    1 +
 drivers/power/charging_algo_pse.c |   79 +++++++++++++++++++++++++++++++++++++
 include/linux/power/battery_id.h  |   47 ++++++++++++++++++++++
 4 files changed, 139 insertions(+)
 create mode 100644 drivers/power/charging_algo_pse.c
drivers/power/Kconfig +12 -0
index 34850e4..e09be69 100644
@@ -16,6 +16,18 @@ 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"
+	help
+	  Say Y here to select PSE compliant charging algorithm. As per PSE
+	  standard the battery characteristics and thereby the charging rates
+	  can vary on different temperature zones. This config will enable PSE
+	  compliant charging algorithm with maintenance charging support. The
+	  algorithm can be selected by the charging framework based on the type
+	  of the battery charging profile.
+
+	depends on POWER_SUPPLY_CHARGER
+
 config POWER_SUPPLY_BATTID
 	bool "Power Supply Battery Identification Framework"
 	help
drivers/power/Makefile +1 -0
index ce8c230..8b250b2 100644
@@ -8,6 +8,7 @@ power_supply-$(CONFIG_POWER_SUPPLY_BATTID) += battery_id.o
 
 obj-$(CONFIG_POWER_SUPPLY)	+= power_supply.o
 obj-$(CONFIG_GENERIC_ADC_BATTERY)	+= generic-adc-battery.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
drivers/power/charging_algo_pse.c new file +79 -0

79 lines not on this page · raw message

include/linux/power/battery_id.h +47 -0

59 lines not on this page · raw message

Signature · 1 line
1.7.9.5