Skip to contentSkip to conversation

power_supply: Introduce power supply charging driver

PATCH In review Jenny TC 2 participants 7 messages Aug 13, 2014 – Nov 12, 2014

Subsystems (2)
lore
Share & tools
lore

Jenny TC

author
#0 lore raw

v12 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
v12: Enabled polling and RTC wakeup which is supported in charger-manager as
suggested by Sebastian. Fixed review comments from Sebastian and Pavel

Jenny TC (3):
power_supply: Introduce generic psy charging driver
power_supply: Introduce PSE compliant algorithm
power_supply: bq24261 charger driver

 Documentation/power/power_supply_charger.txt |  349 +++++++
 drivers/power/Kconfig                        |   33 +
 drivers/power/Makefile                       |    3 +
 drivers/power/bq24261_charger.c              | 1348 ++++++++++++++++++++++++++
 drivers/power/charging_algo_pse.c            |  217 +++++
 drivers/power/power_supply_charger.c         | 1186 ++++++++++++++++++++++
 drivers/power/power_supply_charger.h         |  225 +++++
 drivers/power/power_supply_core.c            |    3 +
 include/linux/power/bq24261-charger.h        |   25 +
 include/linux/power/power_supply_charger.h   |  374 +++++++
 include/linux/power_supply.h                 |  161 +++
 11 files changed, 3924 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
author
#1 lore raw

PATCH v12 1/3 power_supply: Introduce generic psy charging driver

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. 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 |  349 ++++++++
 drivers/power/Kconfig                        |    8 +
 drivers/power/Makefile                       |    1 +
 drivers/power/power_supply_charger.c         | 1206 ++++++++++++++++++++++++++
 drivers/power/power_supply_charger.h         |  224 +++++
 drivers/power/power_supply_core.c            |    3 +
 include/linux/power/power_supply_charger.h   |  308 +++++++
 include/linux/power_supply.h                 |  161 ++++
 8 files changed, 2260 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 +349 -0
new file mode 100644
index 0000000..6ae59ea
@@ -0,0 +1,349 @@
+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 (e.g. 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 becomes 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 just listen to the request from the power
+supply charging driver to set the charger properties. This is accomplished
+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_profile_changed(). The driver also exposes API
+psy_get_battery_profile() 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
+=======
+* Move charge full detection logic to psy charging driver from algorithm driver
drivers/power/Kconfig +8 -0
index 0196acf..f679f82 100644
@@ -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
index ee54a3e..405f0f4 100644
@@ -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 +1206 -0

1,206 lines not on this page · raw message

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

224 lines not on this page · raw message

drivers/power/power_supply_core.c +3 -0
index 2660664..1daa5c2 100644
@@ -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 +308 -0

308 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
author
#2 lore raw

PATCH v12 2/3 power_supply: Introduce PSE compliant algorithm

In reply to Jenny TC

As 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          |  216 ++++++++++++++++++++++++++++
 include/linux/power/power_supply_charger.h |   66 +++++++++
 4 files changed, 298 insertions(+)
 create mode 100644 drivers/power/charging_algo_pse.c
drivers/power/Kconfig +15 -0
index f679f82..54a0321 100644
@@ -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
index 405f0f4..77535fd 100644
@@ -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 +216 -0

216 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
#3 lore raw

Re: [PATCH 2/3] power_supply: Introduce PSE compliant algorithm

Hello Jenny,

Quoted 75 lines · Jenny TC

As 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          |  216 ++++++++++++++++++++++++++++
 include/linux/power/power_supply_charger.h |   66 +++++++++
 4 files changed, 298 insertions(+)
 create mode 100644 drivers/power/charging_algo_pse.c
drivers/power/Kconfig +15 -0
index f679f82..54a0321 100644
@@ -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
index 405f0f4..77535fd 100644
@@ -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 +16 -0
new file mode 100644
index 0000000..cbedebe
@@ -0,0 +1,216 @@
+/*
+ * Copyright (C) 2014 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.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.        See the GNU
+ * General Public License for more details.
+ *
+ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ * Author: Jenny TC <[email protected]>

drivers/power/charging_algo_pse.c line 26

Quoted 10 lines
drivers/power/charging_algo_pse.c new file +10 -0
+ */
+
+#include <linux/module.h>
+#include <linux/types.h>
+#include <linux/init.h>
+#include <linux/slab.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/power_supply.h>
+#include <linux/thermal.h>

Why do you need thermal.h here?

In theory, if you are creating thermal zones or cooling devices with your generic psi driver, then I agree, we need a link with thermal subsystem. But I cannot see how the code below is using the thermal code.

Quoted 277 lines
drivers/power/charging_algo_pse.c new file +190 -0
+#include "power_supply.h"
+#include "power_supply_charger.h"
+
+/* 98% of CV is considered as voltage to detect Full */
+#define FULL_CV_MIN 98
+
+/*
+ * Offset to exit from maintenance charging. In maintenance charging
+ * if the volatge is less than the (maintenance_lower_threshold -
+ * MAINT_EXIT_OFFSET) then system can switch to normal charging
+ */
+
+#define MAINT_EXIT_OFFSET 50  /* mV */
+
+static int get_tempzone(struct psy_pse_chrg_prof *pse_mod_bprof,
+               int temp)
+{
+       int i = 0, temp_range_cnt;
+
+       temp_range_cnt =  pse_mod_bprof->temp_mon_ranges;
+
+       if (!temp_range_cnt || temp_range_cnt > BATT_TEMP_NR_RNG)
+               return -EINVAL;
+
+       /* Basic validity check. Ensure zones are in decreasing order */
+
+       for (i = 0; i < temp_range_cnt - 1; i++) {
+               if (pse_mod_bprof->temp_mon_range[i].temp_max <=
+                               pse_mod_bprof->temp_mon_range[i + 1].temp_max)
+                       return -EINVAL;
+       }
+
+       if (pse_mod_bprof->temp_mon_range[i].temp_max <=
+                                       pse_mod_bprof->temp_min)
+               return -EINVAL;
+
+       if ((temp < pse_mod_bprof->temp_min) ||
+                       (temp > pse_mod_bprof->temp_mon_range[0].temp_max))
+               return -EINVAL;
+
+       for (i = 0; i < temp_range_cnt; i++)
+               if (temp > pse_mod_bprof->temp_mon_range[i].temp_max)
+                       break;
+       return i-1;
+}
+
+static inline bool is_charge_terminated(long volt, long cur,
+                               long iterm, unsigned long cv)
+{
+       return (cur > 0) && (cur <= iterm) &&
+                               ((volt * 100)  >= (FULL_CV_MIN * cv));
+}
+
+static inline bool is_battery_full(struct psy_batt_context *batt_cxt,
+               struct psy_pse_chrg_prof *pse_mod_bprof, unsigned long cv)
+{
+       int i;
+       struct psy_batt_props batt_props;
+
+       batt_props =  batt_cxt->batt_props;
+
+       /*
+        * Software full detection. Check the battery charge current to detect
+        * battery Full. The voltage also verified to avoid false charge
+        * full detection.
+        */
+       for (i = MAX_CUR_VOLT_SAMPLES - 1; i >= 0; --i) {
+
+               if (!(is_charge_terminated(batt_cxt->voltage_now_cache[i],
+                               batt_cxt->current_now_cache[i],
+                               pse_mod_bprof->chrg_term_mA, cv)))
+                       return false;
+       }
+
+       return true;
+}
+
+static int  pse_get_bat_thresholds(struct psy_batt_chrg_prof  bprof,
+                       struct psy_batt_thresholds *bat_thresh)
+{
+       struct psy_pse_chrg_prof *pse_mod_bprof =
+                       (struct psy_pse_chrg_prof *) bprof.batt_prof;
+
+       if ((bprof.chrg_prof_type != PSY_CHRG_PROF_PSE) || (!pse_mod_bprof))
+               return -EINVAL;
+
+       bat_thresh->iterm = pse_mod_bprof->chrg_term_mA;
+       bat_thresh->temp_min = pse_mod_bprof->temp_min;
+       bat_thresh->temp_max = pse_mod_bprof->temp_mon_range[0].temp_max;
+
+       return 0;
+}
+
+static enum psy_algo_stat pse_get_next_cc_cv(struct psy_batt_context *batt_cxt,
+       struct psy_batt_chrg_prof  bprof, unsigned long *cc, unsigned long *cv)
+{
+       int tzone;
+       struct psy_pse_chrg_prof *pse_mod_bprof;
+       struct psy_batt_props batt_props;
+       enum psy_algo_stat algo_stat;
+       int maint_exit_volt;
+
+       pse_mod_bprof = (struct psy_pse_chrg_prof *) bprof.batt_prof;
+       algo_stat = batt_cxt->algo_stat;
+
+       batt_props = batt_cxt->batt_props;
+
+       *cc = *cv = 0;
+
+       /*
+        * If STATUS is discharging, assume that charger is not connected.
+        * If charger is not connected, no need to take any action.
+        * If charge profile type is not PSY_CHRG_PROF_PSE or the charge profile
+        * is not present, no need to take any action.
+        */
+
+       if (!pse_mod_bprof)
+               return PSY_ALGO_STAT_NOT_CHARGE;
+
+       tzone = get_tempzone(pse_mod_bprof, batt_props.temperature);
+       if (tzone < 0)
+               return PSY_ALGO_STAT_NOT_CHARGE;
+
+       /*
+        * Change the algo status to not charging, if battery is
+        * not really charging or less than maintenance exit threshold.
+        * This way algorithm can switch to normal charging if current
+        * status is full/maintenance.
+        */
+       maint_exit_volt = pse_mod_bprof->
+                               temp_mon_range[tzone].maint_chrg_volt_min -
+                               MAINT_EXIT_OFFSET;
+
+       if ((batt_props.status == POWER_SUPPLY_STATUS_DISCHARGING) ||
+               (batt_props.status == POWER_SUPPLY_STATUS_NOT_CHARGING) ||
+                       batt_props.voltage_now < maint_exit_volt) {
+
+               algo_stat = PSY_ALGO_STAT_NOT_CHARGE;
+
+       }
+
+       /* read cc and cv based on temperature and algorithm status */
+       if (algo_stat == PSY_ALGO_STAT_FULL ||
+                       algo_stat == PSY_ALGO_STAT_MAINT) {
+
+               /*
+                * if status is full and voltage is lower than maintenance lower
+                * threshold change status to maintenance
+                */
+
+               if (algo_stat == PSY_ALGO_STAT_FULL &&
+                       (batt_props.voltage_now <=
+                       pse_mod_bprof->temp_mon_range[tzone]
+                                               .maint_chrg_volt_min))
+                               algo_stat = PSY_ALGO_STAT_MAINT;
+
+               /* Read maintenance CC and CV */
+               if (algo_stat == PSY_ALGO_STAT_MAINT) {
+                       *cv = pse_mod_bprof->temp_mon_range
+                                       [tzone].maint_chrg_volt_max;
+                       *cc = pse_mod_bprof->temp_mon_range
+                                       [tzone].maint_chrg_cur;
+               }
+       } else {
+               *cv = pse_mod_bprof->temp_mon_range[tzone].full_chrg_vol;
+               *cc = pse_mod_bprof->temp_mon_range[tzone].full_chrg_cur;
+               algo_stat = PSY_ALGO_STAT_CHARGE;
+       }
+
+       if (is_battery_full(batt_cxt, pse_mod_bprof, *cv)) {
+               *cc = *cv = 0;
+               algo_stat = PSY_ALGO_STAT_FULL;
+       }
+
+       return algo_stat;
+}
+
+struct psy_charging_algo pse_algo = {
+       .name = "pse-algo",
+       .chrg_prof_type = PSY_CHRG_PROF_PSE,
+       .get_next_cc_cv = pse_get_next_cc_cv,
+       .get_batt_thresholds = pse_get_bat_thresholds,
+};
+static int __init pse_algo_init(void)
+{
+       power_supply_register_charging_algo(&pse_algo);
+       return 0;
+}
+
+module_init(pse_algo_init);
include/linux/power/power_supply_charger.h +66 -1
index f1b9567..1718198 100644
@@ -94,8 +94,74 @@ enum battery_events {
 
 enum psy_batt_chrg_prof_type {
        PSY_CHRG_PROF_NONE = 0,
+       PSY_CHRG_PROF_PSE,
 };
 
+/* Product Safety Engineering (PSE) compliant charging profile */
+
+/**
+ * struct psy_ps_temp_chg_table - charging temperature zones definition
+ * @temp_max: upper temperature limit for each zone in Degree Celsius
+ * @full_chrg_vol: charge voltage till battery full in mV
+ * @full_chrg_cur: charge current till battery full in mA
+ * @maint_chrg_volt_min: voltage at which maintenance charging should
+ *             start in mV
+ * @maint_chrg_volt_max: voltage at which maintenance charging should
+ *     stop in mV. This is the charging voltage in maintenance charging mode
+ * @maint_chrg_cur: charge current in maintenance charging mode
+ *
+ * Charging temperature zone definition to decide the charging parameters on
+ * each zone. An array of the structure is used to define multiple temperature
+ * zones
+ */
+
+struct psy_ps_temp_chg_table {
+       short int temp_max;
+       short int full_chrg_vol;
+       short int full_chrg_cur;
+       short int maint_chrg_volt_min;
+       short int maint_chrg_volt_max;
+       short int maint_chrg_cur;
+} __packed;
+
+#define BATTID_STR_LEN         8
+#define BATT_TEMP_NR_RNG       6
+
+/**
+ * struct psy_pse_chrg_prof - PSE charging profile structure
+ * @batt_id: battery identifier
+ * @battery_type: as defined in POWER_SUPPLY_TECHNOLOGY_*
+ * @capacity: battery capacity in mAh
+ * @voltage_max: maximum battery volatge in mV
+ * @chrg_term_ma: charge termination current in mA
+ * @low_batt_mv: Low battery level voltage in mV
+ * @disch_temp_max: maximum operating temperature when battery is discharging
+ *             in degree Celsius
+ * @disch_temp_min: lowest operating temperature when battery is discharging
+ *     in degree Celsius
+ * @temp_mon_ranges: number of temperature zones
+ * @psy_ps_temp_chg_table: temperature zone table array
+ * @temp_min: minimum charging temperature
+ *
+ * PSE compliant charging profile which can be stored in battery EEPROM
+ * (if digital battery interface like MIPI BIF/SDQ supported) or in secondary
+ * storage to support analog battery (with BSI sensing support)
+ */
+
+struct psy_pse_chrg_prof {
+       char batt_id[BATTID_STR_LEN];
+       u16 battery_type;
+       u16 capacity;
+       u16 voltage_max;
+       u16 chrg_term_mA;
+       u16 low_batt_mV;
+       s8 disch_temp_max;
+       s8 disch_temp_min;
+       u16 temp_mon_ranges;
+       struct psy_ps_temp_chg_table temp_mon_range[BATT_TEMP_NR_RNG];
+       s8 temp_min;
+} __packed;
+
 /**
  * struct psy_batt_chrg_prof - power supply charging profile structure
  * @chrg_prof_type: charging profile type
--

1.7.9.5

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

Signature · 1 line
Eduardo Bezerra Valentin
author
#4 lore raw

PATCH v12 3/3 power_supply: bq24261 charger driver

In reply to Jenny TC

This 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
index 54a0321..4ff080c 100644
@@ -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
index 77535fd..a6e9897 100644
@@ -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
new file mode 100644
index 0000000..4ff02af
@@ -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
author
#5 lore raw
In reply to Jenny TC

Ping...

Quoted 58 lines

-----Original Message-----
From: Tc, Jenny
Sent: Wednesday, August 13, 2014 5:40 PM
To: [email protected]; Sebastian Reichel; Pavel Machek
Cc: Dmitry Eremin-Solenikov; Stephen Rothwell; Anton Vorontsov; David Woodhouse; David Cohen; Pallala, Ramakrishna; Tc, Jenny
Subject: [PATCHv12 0/3] 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
v12: Enabled polling and RTC wakeup which is supported in charger-manager as
suggested by Sebastian. Fixed review comments from Sebastian and Pavel

Jenny TC (3):
power_supply: Introduce generic psy charging driver
power_supply: Introduce PSE compliant algorithm
power_supply: bq24261 charger driver

 Documentation/power/power_supply_charger.txt |  349 +++++++
 drivers/power/Kconfig                        |   33 +
 drivers/power/Makefile                       |    3 +
 drivers/power/bq24261_charger.c              | 1348
++++++++++++++++++++++++++
 drivers/power/charging_algo_pse.c            |  217 +++++
 drivers/power/power_supply_charger.c         | 1186 ++++++++++++++++++++++
 drivers/power/power_supply_charger.h         |  225 +++++
 drivers/power/power_supply_core.c            |    3 +
 include/linux/power/bq24261-charger.h        |   25 +
 include/linux/power/power_supply_charger.h   |  374 +++++++
 include/linux/power_supply.h                 |  161 +++
 11 files changed, 3924 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

--
1.7.9.5

author
#6 lore raw
In reply to Jenny TC

Sebastian,

I would appreciate if you could share your plans on this patch?. As you agreed on the IRC session, can you push this patch to staging? Meanwhile we will work to close the TODOs captured in the IRC session (http://elektranox.org/battery.txt)

-Jenny

Quoted 49 lines

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
v12: Enabled polling and RTC wakeup which is supported in charger-manager as
suggested by Sebastian. Fixed review comments from Sebastian and Pavel

Jenny TC (3):
power_supply: Introduce generic psy charging driver
power_supply: Introduce PSE compliant algorithm
power_supply: bq24261 charger driver

 Documentation/power/power_supply_charger.txt |  349 +++++++
 drivers/power/Kconfig                        |   33 +
 drivers/power/Makefile                       |    3 +
 drivers/power/bq24261_charger.c              | 1348 ++++++++++++++++++++++++++
 drivers/power/charging_algo_pse.c            |  217 +++++
 drivers/power/power_supply_charger.c         | 1186 ++++++++++++++++++++++
 drivers/power/power_supply_charger.h         |  225 +++++
 drivers/power/power_supply_core.c            |    3 +
 include/linux/power/bq24261-charger.h        |   25 +
 include/linux/power/power_supply_charger.h   |  374 +++++++
 include/linux/power_supply.h                 |  161 +++
 11 files changed, 3924 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

--
1.7.9.5