All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 00/18] add support for AXP20X and AXP22X power supply drivers
@ 2017-02-14  9:40 ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:40 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: Quentin Schulz, linux-iio, devicetree, linux-kernel, linux-pm,
	linux-arm-kernel, thomas.petazzoni, icenowy, bonbons

The X-Powers AXP20X and AXP22X PMICs have multiple ADCs. They expose
information and data of the various power supplies they support such as
ACIN, battery and VBUS. For example, they expose the current battery
voltage, charge or discharge, as well as ACIN and VBUS current voltages
and currents, internal PMIC temperature and ADC on 2 different GPIOs
when in the right mode (for the AXP209 only).

The ACIN power supply driver is added by this patch. The AXP20X and
AXP22X can both read the status and the "usability" of the power supply
but only the AXP209 will be able to tell the current current and voltage
of the power supply by reading ADC channels. It is simply not supported
by the AXP22X PMICs.

The battery power supply driver is also added by this patch. The AXP20X
and AXP22X share most of their behaviour but have slight variations. The
allowed target voltages for battery charging are not the same, the
AXP22X PMIC are able to tell if the battery percentage computed by the
PMIC is trustworthy and they have different formulas for computing max
current for battery power supply. The driver is able to give the current
voltage and current of the battery (be it charging or discharging), the
maximal and minimal voltage and maximal current allowed for the battery,
whether the battery is present and usable and its capacity. It will get
the battery current current and voltage by reading the ADC channels. The
PMIC allows maximal voltages (4.36V for AXP20X and 4.22V and 4.24V for
AXP22X) that should not be used with Lithium-based batteries and since
this PMIC is supposed to be used with Lithium-based batteries, they have
been disabled. The values returned by the ADC driver are multipled by
1000 to scale from the mV returned by the ADC to the uV expected by the
power supply framework.

This series of patch adds DT bindings for ACIN power supply, ADC and
battery power supply drivers for AXP20X and AXP22X PMICs and their
documentation. It also enables the supported power supplies for the
Nextthing Co. CHIP and Sinlinx SinA33 boards.

The different drivers are also added to the MFD cells of the AXP20X and
AXP22X cells and the writeable and volatile regs updated to work with
the newly added drivers.

This series of patch is based on a previous upstreaming attempt done by
Bruno Prémont few months ago. It differs in three points: the ADC
driver does not tell the battery temperature (TS_IN) as I do not have a
board to test it with, it does not tell the instantaneous battery power
as it returns crazy values for me and finally no support for OCV curves
for the battery.

You can test these patches from this repo and branch:
https://github.com/QSchulz/linux/tree/axp2xx_adc_batt_ac_v3

v3:
 - Removed DT property for constant charge current in favor of the WIP
 battery framework as requested by Sebastian Reichel,
 - Using a simple if condition instead of a switch in the ADC driver,
 - Fixed error handling in ADC driver's probe,
 - Fixed missing call to iio_map_array_unregister in the ADC driver's
 remove,
 - Removed ADC driver's DT node and documentation,
 - Merged IIO channel mapping patches into the original ADC driver
 patch,
 - Removed `adding V-OFF to writeable reg' patch as it's already in
 writeable reg range,

v2:
 - Some registers' name have been changed to better reflect their
 purpose,
 - Make VBUS power supply driver use IIO channels when AXP ADC driver is
 enabled, but fall back on previous behavior when disabled. This is made
 to avoid the ADC driver overwritting registers for VBUS power supply
 ADC when removed,
 - Removed useless adding of data registers to volatile registers,
 - Reordered IIO channels, now grouped by same part of the PMIC (e.g.
 voltage and current of the battery have the same index in different
 IIO types),
 - Added structures for specific data instead of matching on IDs,
 - Switched from DT IIO channels mapping to iio_map structures IIO
 channels mapping,

Quentin

Quentin Schulz (18):
  dt-bindings: power: battery: add constant-charge-current property
  power: supply: power_supply_core: add constant-current-charge optional
    property
  mfd: axp20x: correct name of temperature data ADC registers
  iio: adc: add support for X-Powers AXP20X and AXP22X PMICs ADCs
  mfd: axp20x: add ADC cells for AXP20X and AXP22X PMICs
  mfd: axp20x: add AC power supply cells for AXP22X PMICs
  ARM: dtsi: axp209: add AC power supply subnode
  ARM: dtsi: axp22x: add AC power supply subnode
  ARM: dts: sun8i: sina33: enable ACIN power supply subnode
  ARM: sun5i: chip: enable ACIN power supply subnode
  dt-bindings: power: supply: add AXP20X/AXP22X battery DT binding
  mfd: axp20x: add CHRG_CTRL1/2/3 to writeable regs for AXP20X/AXP22X
  power: supply: add battery driver for AXP20X and AXP22X PMICs
  mfd: axp20x: add MFD cells for AXP20X and AXP22X battery driver
  ARM: dtsi: axp209: add battery power supply subnode
  ARM: dtsi: axp22x: add battery power supply subnode
  ARM: dts: sun8i: sina33: enable battery power supply subnode
  ARM: sun5i: chip: enable battery power supply subnode

 .../bindings/power/supply/axp20x_battery.txt       |  20 +
 .../devicetree/bindings/power/supply/battery.txt   |   2 +
 arch/arm/boot/dts/axp209.dtsi                      |  10 +
 arch/arm/boot/dts/axp22x.dtsi                      |  10 +
 arch/arm/boot/dts/sun5i-r8-chip.dts                |   8 +
 arch/arm/boot/dts/sun8i-a33-sinlinx-sina33.dts     |   8 +
 drivers/iio/adc/Kconfig                            |  10 +
 drivers/iio/adc/Makefile                           |   1 +
 drivers/iio/adc/axp20x_adc.c                       | 606 +++++++++++++++++++++
 drivers/mfd/axp20x.c                               |  29 +-
 drivers/power/supply/Kconfig                       |  12 +
 drivers/power/supply/Makefile                      |   1 +
 drivers/power/supply/axp20x_battery.c              | 492 +++++++++++++++++
 drivers/power/supply/power_supply_core.c           |   3 +
 include/linux/mfd/axp20x.h                         |   4 +-
 include/linux/power_supply.h                       |   1 +
 16 files changed, 1214 insertions(+), 3 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/power/supply/axp20x_battery.txt
 create mode 100644 drivers/iio/adc/axp20x_adc.c
 create mode 100644 drivers/power/supply/axp20x_battery.c

-- 
2.9.3

^ permalink raw reply	[flat|nested] 126+ messages in thread

* [PATCH v3 00/18] add support for AXP20X and AXP22X power supply drivers
@ 2017-02-14  9:40 ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:40 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: thomas.petazzoni, devicetree, linux-pm, linux-iio, linux-kernel,
	Quentin Schulz, bonbons, icenowy, linux-arm-kernel

The X-Powers AXP20X and AXP22X PMICs have multiple ADCs. They expose
information and data of the various power supplies they support such as
ACIN, battery and VBUS. For example, they expose the current battery
voltage, charge or discharge, as well as ACIN and VBUS current voltages
and currents, internal PMIC temperature and ADC on 2 different GPIOs
when in the right mode (for the AXP209 only).

The ACIN power supply driver is added by this patch. The AXP20X and
AXP22X can both read the status and the "usability" of the power supply
but only the AXP209 will be able to tell the current current and voltage
of the power supply by reading ADC channels. It is simply not supported
by the AXP22X PMICs.

The battery power supply driver is also added by this patch. The AXP20X
and AXP22X share most of their behaviour but have slight variations. The
allowed target voltages for battery charging are not the same, the
AXP22X PMIC are able to tell if the battery percentage computed by the
PMIC is trustworthy and they have different formulas for computing max
current for battery power supply. The driver is able to give the current
voltage and current of the battery (be it charging or discharging), the
maximal and minimal voltage and maximal current allowed for the battery,
whether the battery is present and usable and its capacity. It will get
the battery current current and voltage by reading the ADC channels. The
PMIC allows maximal voltages (4.36V for AXP20X and 4.22V and 4.24V for
AXP22X) that should not be used with Lithium-based batteries and since
this PMIC is supposed to be used with Lithium-based batteries, they have
been disabled. The values returned by the ADC driver are multipled by
1000 to scale from the mV returned by the ADC to the uV expected by the
power supply framework.

This series of patch adds DT bindings for ACIN power supply, ADC and
battery power supply drivers for AXP20X and AXP22X PMICs and their
documentation. It also enables the supported power supplies for the
Nextthing Co. CHIP and Sinlinx SinA33 boards.

The different drivers are also added to the MFD cells of the AXP20X and
AXP22X cells and the writeable and volatile regs updated to work with
the newly added drivers.

This series of patch is based on a previous upstreaming attempt done by
Bruno Prémont few months ago. It differs in three points: the ADC
driver does not tell the battery temperature (TS_IN) as I do not have a
board to test it with, it does not tell the instantaneous battery power
as it returns crazy values for me and finally no support for OCV curves
for the battery.

You can test these patches from this repo and branch:
https://github.com/QSchulz/linux/tree/axp2xx_adc_batt_ac_v3

v3:
 - Removed DT property for constant charge current in favor of the WIP
 battery framework as requested by Sebastian Reichel,
 - Using a simple if condition instead of a switch in the ADC driver,
 - Fixed error handling in ADC driver's probe,
 - Fixed missing call to iio_map_array_unregister in the ADC driver's
 remove,
 - Removed ADC driver's DT node and documentation,
 - Merged IIO channel mapping patches into the original ADC driver
 patch,
 - Removed `adding V-OFF to writeable reg' patch as it's already in
 writeable reg range,

v2:
 - Some registers' name have been changed to better reflect their
 purpose,
 - Make VBUS power supply driver use IIO channels when AXP ADC driver is
 enabled, but fall back on previous behavior when disabled. This is made
 to avoid the ADC driver overwritting registers for VBUS power supply
 ADC when removed,
 - Removed useless adding of data registers to volatile registers,
 - Reordered IIO channels, now grouped by same part of the PMIC (e.g.
 voltage and current of the battery have the same index in different
 IIO types),
 - Added structures for specific data instead of matching on IDs,
 - Switched from DT IIO channels mapping to iio_map structures IIO
 channels mapping,

Quentin

Quentin Schulz (18):
  dt-bindings: power: battery: add constant-charge-current property
  power: supply: power_supply_core: add constant-current-charge optional
    property
  mfd: axp20x: correct name of temperature data ADC registers
  iio: adc: add support for X-Powers AXP20X and AXP22X PMICs ADCs
  mfd: axp20x: add ADC cells for AXP20X and AXP22X PMICs
  mfd: axp20x: add AC power supply cells for AXP22X PMICs
  ARM: dtsi: axp209: add AC power supply subnode
  ARM: dtsi: axp22x: add AC power supply subnode
  ARM: dts: sun8i: sina33: enable ACIN power supply subnode
  ARM: sun5i: chip: enable ACIN power supply subnode
  dt-bindings: power: supply: add AXP20X/AXP22X battery DT binding
  mfd: axp20x: add CHRG_CTRL1/2/3 to writeable regs for AXP20X/AXP22X
  power: supply: add battery driver for AXP20X and AXP22X PMICs
  mfd: axp20x: add MFD cells for AXP20X and AXP22X battery driver
  ARM: dtsi: axp209: add battery power supply subnode
  ARM: dtsi: axp22x: add battery power supply subnode
  ARM: dts: sun8i: sina33: enable battery power supply subnode
  ARM: sun5i: chip: enable battery power supply subnode

 .../bindings/power/supply/axp20x_battery.txt       |  20 +
 .../devicetree/bindings/power/supply/battery.txt   |   2 +
 arch/arm/boot/dts/axp209.dtsi                      |  10 +
 arch/arm/boot/dts/axp22x.dtsi                      |  10 +
 arch/arm/boot/dts/sun5i-r8-chip.dts                |   8 +
 arch/arm/boot/dts/sun8i-a33-sinlinx-sina33.dts     |   8 +
 drivers/iio/adc/Kconfig                            |  10 +
 drivers/iio/adc/Makefile                           |   1 +
 drivers/iio/adc/axp20x_adc.c                       | 606 +++++++++++++++++++++
 drivers/mfd/axp20x.c                               |  29 +-
 drivers/power/supply/Kconfig                       |  12 +
 drivers/power/supply/Makefile                      |   1 +
 drivers/power/supply/axp20x_battery.c              | 492 +++++++++++++++++
 drivers/power/supply/power_supply_core.c           |   3 +
 include/linux/mfd/axp20x.h                         |   4 +-
 include/linux/power_supply.h                       |   1 +
 16 files changed, 1214 insertions(+), 3 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/power/supply/axp20x_battery.txt
 create mode 100644 drivers/iio/adc/axp20x_adc.c
 create mode 100644 drivers/power/supply/axp20x_battery.c

-- 
2.9.3


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 126+ messages in thread

* [PATCH v3 00/18] add support for AXP20X and AXP22X power supply drivers
@ 2017-02-14  9:40 ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:40 UTC (permalink / raw)
  To: linux-arm-kernel

The X-Powers AXP20X and AXP22X PMICs have multiple ADCs. They expose
information and data of the various power supplies they support such as
ACIN, battery and VBUS. For example, they expose the current battery
voltage, charge or discharge, as well as ACIN and VBUS current voltages
and currents, internal PMIC temperature and ADC on 2 different GPIOs
when in the right mode (for the AXP209 only).

The ACIN power supply driver is added by this patch. The AXP20X and
AXP22X can both read the status and the "usability" of the power supply
but only the AXP209 will be able to tell the current current and voltage
of the power supply by reading ADC channels. It is simply not supported
by the AXP22X PMICs.

The battery power supply driver is also added by this patch. The AXP20X
and AXP22X share most of their behaviour but have slight variations. The
allowed target voltages for battery charging are not the same, the
AXP22X PMIC are able to tell if the battery percentage computed by the
PMIC is trustworthy and they have different formulas for computing max
current for battery power supply. The driver is able to give the current
voltage and current of the battery (be it charging or discharging), the
maximal and minimal voltage and maximal current allowed for the battery,
whether the battery is present and usable and its capacity. It will get
the battery current current and voltage by reading the ADC channels. The
PMIC allows maximal voltages (4.36V for AXP20X and 4.22V and 4.24V for
AXP22X) that should not be used with Lithium-based batteries and since
this PMIC is supposed to be used with Lithium-based batteries, they have
been disabled. The values returned by the ADC driver are multipled by
1000 to scale from the mV returned by the ADC to the uV expected by the
power supply framework.

This series of patch adds DT bindings for ACIN power supply, ADC and
battery power supply drivers for AXP20X and AXP22X PMICs and their
documentation. It also enables the supported power supplies for the
Nextthing Co. CHIP and Sinlinx SinA33 boards.

The different drivers are also added to the MFD cells of the AXP20X and
AXP22X cells and the writeable and volatile regs updated to work with
the newly added drivers.

This series of patch is based on a previous upstreaming attempt done by
Bruno Pr?mont few months ago. It differs in three points: the ADC
driver does not tell the battery temperature (TS_IN) as I do not have a
board to test it with, it does not tell the instantaneous battery power
as it returns crazy values for me and finally no support for OCV curves
for the battery.

You can test these patches from this repo and branch:
https://github.com/QSchulz/linux/tree/axp2xx_adc_batt_ac_v3

v3:
 - Removed DT property for constant charge current in favor of the WIP
 battery framework as requested by Sebastian Reichel,
 - Using a simple if condition instead of a switch in the ADC driver,
 - Fixed error handling in ADC driver's probe,
 - Fixed missing call to iio_map_array_unregister in the ADC driver's
 remove,
 - Removed ADC driver's DT node and documentation,
 - Merged IIO channel mapping patches into the original ADC driver
 patch,
 - Removed `adding V-OFF to writeable reg' patch as it's already in
 writeable reg range,

v2:
 - Some registers' name have been changed to better reflect their
 purpose,
 - Make VBUS power supply driver use IIO channels when AXP ADC driver is
 enabled, but fall back on previous behavior when disabled. This is made
 to avoid the ADC driver overwritting registers for VBUS power supply
 ADC when removed,
 - Removed useless adding of data registers to volatile registers,
 - Reordered IIO channels, now grouped by same part of the PMIC (e.g.
 voltage and current of the battery have the same index in different
 IIO types),
 - Added structures for specific data instead of matching on IDs,
 - Switched from DT IIO channels mapping to iio_map structures IIO
 channels mapping,

Quentin

Quentin Schulz (18):
  dt-bindings: power: battery: add constant-charge-current property
  power: supply: power_supply_core: add constant-current-charge optional
    property
  mfd: axp20x: correct name of temperature data ADC registers
  iio: adc: add support for X-Powers AXP20X and AXP22X PMICs ADCs
  mfd: axp20x: add ADC cells for AXP20X and AXP22X PMICs
  mfd: axp20x: add AC power supply cells for AXP22X PMICs
  ARM: dtsi: axp209: add AC power supply subnode
  ARM: dtsi: axp22x: add AC power supply subnode
  ARM: dts: sun8i: sina33: enable ACIN power supply subnode
  ARM: sun5i: chip: enable ACIN power supply subnode
  dt-bindings: power: supply: add AXP20X/AXP22X battery DT binding
  mfd: axp20x: add CHRG_CTRL1/2/3 to writeable regs for AXP20X/AXP22X
  power: supply: add battery driver for AXP20X and AXP22X PMICs
  mfd: axp20x: add MFD cells for AXP20X and AXP22X battery driver
  ARM: dtsi: axp209: add battery power supply subnode
  ARM: dtsi: axp22x: add battery power supply subnode
  ARM: dts: sun8i: sina33: enable battery power supply subnode
  ARM: sun5i: chip: enable battery power supply subnode

 .../bindings/power/supply/axp20x_battery.txt       |  20 +
 .../devicetree/bindings/power/supply/battery.txt   |   2 +
 arch/arm/boot/dts/axp209.dtsi                      |  10 +
 arch/arm/boot/dts/axp22x.dtsi                      |  10 +
 arch/arm/boot/dts/sun5i-r8-chip.dts                |   8 +
 arch/arm/boot/dts/sun8i-a33-sinlinx-sina33.dts     |   8 +
 drivers/iio/adc/Kconfig                            |  10 +
 drivers/iio/adc/Makefile                           |   1 +
 drivers/iio/adc/axp20x_adc.c                       | 606 +++++++++++++++++++++
 drivers/mfd/axp20x.c                               |  29 +-
 drivers/power/supply/Kconfig                       |  12 +
 drivers/power/supply/Makefile                      |   1 +
 drivers/power/supply/axp20x_battery.c              | 492 +++++++++++++++++
 drivers/power/supply/power_supply_core.c           |   3 +
 include/linux/mfd/axp20x.h                         |   4 +-
 include/linux/power_supply.h                       |   1 +
 16 files changed, 1214 insertions(+), 3 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/power/supply/axp20x_battery.txt
 create mode 100644 drivers/iio/adc/axp20x_adc.c
 create mode 100644 drivers/power/supply/axp20x_battery.c

-- 
2.9.3

^ permalink raw reply	[flat|nested] 126+ messages in thread

* [PATCH v3 01/18] dt-bindings: power: battery: add constant-charge-current property
  2017-02-14  9:40 ` Quentin Schulz
  (?)
@ 2017-02-14  9:40   ` Quentin Schulz
  -1 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:40 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: Quentin Schulz, linux-iio, devicetree, linux-kernel, linux-pm,
	linux-arm-kernel, thomas.petazzoni, icenowy, bonbons

This adds the constant-charge-current property to the list of optional
properties of the battery.

The constant charge current is critical for batteries as they can't
handle all charge currents.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
---

added in v3

 Documentation/devicetree/bindings/power/supply/battery.txt | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/Documentation/devicetree/bindings/power/supply/battery.txt b/Documentation/devicetree/bindings/power/supply/battery.txt
index d78a4aa..3e830fe 100644
--- a/Documentation/devicetree/bindings/power/supply/battery.txt
+++ b/Documentation/devicetree/bindings/power/supply/battery.txt
@@ -7,6 +7,7 @@ Optional Properties:
  - voltage-min-design-microvolt: drained battery voltage
  - energy-full-design-microwatt-hours: battery design energy
  - charge-full-design-microamp-hours: battery design capacity
+ - constant-charge-current-microamp: battery constant charge current
 
 Future Properties must be named for the corresponding elements in
 enum power_supply_property, defined in include/linux/power_supply.h.
@@ -22,6 +23,7 @@ Example:
 		voltage-min-design-microvolt = <3200000>;
 		energy-full-design-microwatt-hours = <5290000>;
 		charge-full-design-microamp-hours = <1430000>;
+		constant-charge-current-microamp = <300000>;
 	};
 
 	charger: charger@11 {
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 01/18] dt-bindings: power: battery: add constant-charge-current property
@ 2017-02-14  9:40   ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:40 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: thomas.petazzoni, devicetree, linux-pm, linux-iio, linux-kernel,
	Quentin Schulz, bonbons, icenowy, linux-arm-kernel

This adds the constant-charge-current property to the list of optional
properties of the battery.

The constant charge current is critical for batteries as they can't
handle all charge currents.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
---

added in v3

 Documentation/devicetree/bindings/power/supply/battery.txt | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/Documentation/devicetree/bindings/power/supply/battery.txt b/Documentation/devicetree/bindings/power/supply/battery.txt
index d78a4aa..3e830fe 100644
--- a/Documentation/devicetree/bindings/power/supply/battery.txt
+++ b/Documentation/devicetree/bindings/power/supply/battery.txt
@@ -7,6 +7,7 @@ Optional Properties:
  - voltage-min-design-microvolt: drained battery voltage
  - energy-full-design-microwatt-hours: battery design energy
  - charge-full-design-microamp-hours: battery design capacity
+ - constant-charge-current-microamp: battery constant charge current
 
 Future Properties must be named for the corresponding elements in
 enum power_supply_property, defined in include/linux/power_supply.h.
@@ -22,6 +23,7 @@ Example:
 		voltage-min-design-microvolt = <3200000>;
 		energy-full-design-microwatt-hours = <5290000>;
 		charge-full-design-microamp-hours = <1430000>;
+		constant-charge-current-microamp = <300000>;
 	};
 
 	charger: charger@11 {
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 01/18] dt-bindings: power: battery: add constant-charge-current property
@ 2017-02-14  9:40   ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:40 UTC (permalink / raw)
  To: linux-arm-kernel

This adds the constant-charge-current property to the list of optional
properties of the battery.

The constant charge current is critical for batteries as they can't
handle all charge currents.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
---

added in v3

 Documentation/devicetree/bindings/power/supply/battery.txt | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/Documentation/devicetree/bindings/power/supply/battery.txt b/Documentation/devicetree/bindings/power/supply/battery.txt
index d78a4aa..3e830fe 100644
--- a/Documentation/devicetree/bindings/power/supply/battery.txt
+++ b/Documentation/devicetree/bindings/power/supply/battery.txt
@@ -7,6 +7,7 @@ Optional Properties:
  - voltage-min-design-microvolt: drained battery voltage
  - energy-full-design-microwatt-hours: battery design energy
  - charge-full-design-microamp-hours: battery design capacity
+ - constant-charge-current-microamp: battery constant charge current
 
 Future Properties must be named for the corresponding elements in
 enum power_supply_property, defined in include/linux/power_supply.h.
@@ -22,6 +23,7 @@ Example:
 		voltage-min-design-microvolt = <3200000>;
 		energy-full-design-microwatt-hours = <5290000>;
 		charge-full-design-microamp-hours = <1430000>;
+		constant-charge-current-microamp = <300000>;
 	};
 
 	charger: charger at 11 {
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 02/18] power: supply: power_supply_core: add constant-current-charge optional property
  2017-02-14  9:40 ` Quentin Schulz
  (?)
@ 2017-02-14  9:40   ` Quentin Schulz
  -1 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:40 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: Quentin Schulz, linux-iio, devicetree, linux-kernel, linux-pm,
	linux-arm-kernel, thomas.petazzoni, icenowy, bonbons

This adds the constant-current-charge property to the list of optional
properties for the battery.

The constant charge current is critical for batteries as they can't
handle all charge currents.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
---

added in v3

 drivers/power/supply/power_supply_core.c | 3 +++
 include/linux/power_supply.h             | 1 +
 2 files changed, 4 insertions(+)

diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c
index ced8fef..c844def 100644
--- a/drivers/power/supply/power_supply_core.c
+++ b/drivers/power/supply/power_supply_core.c
@@ -498,6 +498,7 @@ int power_supply_get_battery_info(struct power_supply *psy,
 	info->energy_full_design_uwh = -EINVAL;
 	info->charge_full_design_uah = -EINVAL;
 	info->voltage_min_design_uv  = -EINVAL;
+	info->constant_charge_current_ua  = -EINVAL;
 
 	if (!psy->of_node) {
 		dev_warn(&psy->dev, "%s currently only supports devicetree\n",
@@ -522,6 +523,8 @@ int power_supply_get_battery_info(struct power_supply *psy,
 			     &info->charge_full_design_uah);
 	of_property_read_u32(battery_np, "voltage-min-design-microvolt",
 			     &info->voltage_min_design_uv);
+	of_property_read_u32(battery_np, "constant-charge-current-microamp",
+			     &info->constant_charge_current_ua);
 
 	return 0;
 }
diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h
index e84f1d3..f723a8e 100644
--- a/include/linux/power_supply.h
+++ b/include/linux/power_supply.h
@@ -301,6 +301,7 @@ struct power_supply_battery_info {
 	int energy_full_design_uwh;	/* microWatt-hours */
 	int charge_full_design_uah;	/* microAmp-hours */
 	int voltage_min_design_uv;	/* microVolts */
+	int constant_charge_current_ua; /* microAmps */
 };
 
 extern struct atomic_notifier_head power_supply_notifier;
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 02/18] power: supply: power_supply_core: add constant-current-charge optional property
@ 2017-02-14  9:40   ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:40 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: thomas.petazzoni, devicetree, linux-pm, linux-iio, linux-kernel,
	Quentin Schulz, bonbons, icenowy, linux-arm-kernel

This adds the constant-current-charge property to the list of optional
properties for the battery.

The constant charge current is critical for batteries as they can't
handle all charge currents.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
---

added in v3

 drivers/power/supply/power_supply_core.c | 3 +++
 include/linux/power_supply.h             | 1 +
 2 files changed, 4 insertions(+)

diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c
index ced8fef..c844def 100644
--- a/drivers/power/supply/power_supply_core.c
+++ b/drivers/power/supply/power_supply_core.c
@@ -498,6 +498,7 @@ int power_supply_get_battery_info(struct power_supply *psy,
 	info->energy_full_design_uwh = -EINVAL;
 	info->charge_full_design_uah = -EINVAL;
 	info->voltage_min_design_uv  = -EINVAL;
+	info->constant_charge_current_ua  = -EINVAL;
 
 	if (!psy->of_node) {
 		dev_warn(&psy->dev, "%s currently only supports devicetree\n",
@@ -522,6 +523,8 @@ int power_supply_get_battery_info(struct power_supply *psy,
 			     &info->charge_full_design_uah);
 	of_property_read_u32(battery_np, "voltage-min-design-microvolt",
 			     &info->voltage_min_design_uv);
+	of_property_read_u32(battery_np, "constant-charge-current-microamp",
+			     &info->constant_charge_current_ua);
 
 	return 0;
 }
diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h
index e84f1d3..f723a8e 100644
--- a/include/linux/power_supply.h
+++ b/include/linux/power_supply.h
@@ -301,6 +301,7 @@ struct power_supply_battery_info {
 	int energy_full_design_uwh;	/* microWatt-hours */
 	int charge_full_design_uah;	/* microAmp-hours */
 	int voltage_min_design_uv;	/* microVolts */
+	int constant_charge_current_ua; /* microAmps */
 };
 
 extern struct atomic_notifier_head power_supply_notifier;
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 02/18] power: supply: power_supply_core: add constant-current-charge optional property
@ 2017-02-14  9:40   ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:40 UTC (permalink / raw)
  To: linux-arm-kernel

This adds the constant-current-charge property to the list of optional
properties for the battery.

The constant charge current is critical for batteries as they can't
handle all charge currents.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
---

added in v3

 drivers/power/supply/power_supply_core.c | 3 +++
 include/linux/power_supply.h             | 1 +
 2 files changed, 4 insertions(+)

diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c
index ced8fef..c844def 100644
--- a/drivers/power/supply/power_supply_core.c
+++ b/drivers/power/supply/power_supply_core.c
@@ -498,6 +498,7 @@ int power_supply_get_battery_info(struct power_supply *psy,
 	info->energy_full_design_uwh = -EINVAL;
 	info->charge_full_design_uah = -EINVAL;
 	info->voltage_min_design_uv  = -EINVAL;
+	info->constant_charge_current_ua  = -EINVAL;
 
 	if (!psy->of_node) {
 		dev_warn(&psy->dev, "%s currently only supports devicetree\n",
@@ -522,6 +523,8 @@ int power_supply_get_battery_info(struct power_supply *psy,
 			     &info->charge_full_design_uah);
 	of_property_read_u32(battery_np, "voltage-min-design-microvolt",
 			     &info->voltage_min_design_uv);
+	of_property_read_u32(battery_np, "constant-charge-current-microamp",
+			     &info->constant_charge_current_ua);
 
 	return 0;
 }
diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h
index e84f1d3..f723a8e 100644
--- a/include/linux/power_supply.h
+++ b/include/linux/power_supply.h
@@ -301,6 +301,7 @@ struct power_supply_battery_info {
 	int energy_full_design_uwh;	/* microWatt-hours */
 	int charge_full_design_uah;	/* microAmp-hours */
 	int voltage_min_design_uv;	/* microVolts */
+	int constant_charge_current_ua; /* microAmps */
 };
 
 extern struct atomic_notifier_head power_supply_notifier;
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 03/18] mfd: axp20x: correct name of temperature data ADC registers
  2017-02-14  9:40 ` Quentin Schulz
  (?)
@ 2017-02-14  9:40   ` Quentin Schulz
  -1 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:40 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: Quentin Schulz, linux-iio, devicetree, linux-kernel, linux-pm,
	linux-arm-kernel, thomas.petazzoni, icenowy, bonbons

The registers 0x56 and 0x57 of AXP22X PMIC store the value of the
internal temperature of the PMIC.

This patch modifies the name of these registers from AXP22X_PMIC_ADC_H/L
to AXP22X_PMIC_TEMP_H/L so their purpose is clearer.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Chen-Yu Tsai <wens@csie.org>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
Acked-for-MFD-by: Lee Jones <lee.jones@linaro.org>
---

added in v2

 drivers/mfd/axp20x.c       | 2 +-
 include/linux/mfd/axp20x.h | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/mfd/axp20x.c b/drivers/mfd/axp20x.c
index 619a83e..9c2fd37 100644
--- a/drivers/mfd/axp20x.c
+++ b/drivers/mfd/axp20x.c
@@ -102,7 +102,7 @@ static const struct regmap_range axp22x_volatile_ranges[] = {
 	regmap_reg_range(AXP20X_VBUS_IPSOUT_MGMT, AXP20X_VBUS_IPSOUT_MGMT),
 	regmap_reg_range(AXP20X_IRQ1_EN, AXP20X_IRQ5_STATE),
 	regmap_reg_range(AXP22X_GPIO_STATE, AXP22X_GPIO_STATE),
-	regmap_reg_range(AXP22X_PMIC_ADC_H, AXP20X_IPSOUT_V_HIGH_L),
+	regmap_reg_range(AXP22X_PMIC_TEMP_H, AXP20X_IPSOUT_V_HIGH_L),
 	regmap_reg_range(AXP20X_FG_RES, AXP20X_FG_RES),
 };
 
diff --git a/include/linux/mfd/axp20x.h b/include/linux/mfd/axp20x.h
index a4860bc..5ecadbb 100644
--- a/include/linux/mfd/axp20x.h
+++ b/include/linux/mfd/axp20x.h
@@ -228,8 +228,8 @@ enum {
 #define AXP20X_OCV_MAX			0xf
 
 /* AXP22X specific registers */
-#define AXP22X_PMIC_ADC_H		0x56
-#define AXP22X_PMIC_ADC_L		0x57
+#define AXP22X_PMIC_TEMP_H		0x56
+#define AXP22X_PMIC_TEMP_L		0x57
 #define AXP22X_TS_ADC_H			0x58
 #define AXP22X_TS_ADC_L			0x59
 #define AXP22X_BATLOW_THRES1		0xe6
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 03/18] mfd: axp20x: correct name of temperature data ADC registers
@ 2017-02-14  9:40   ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:40 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: thomas.petazzoni, devicetree, linux-pm, linux-iio, linux-kernel,
	Quentin Schulz, bonbons, icenowy, linux-arm-kernel

The registers 0x56 and 0x57 of AXP22X PMIC store the value of the
internal temperature of the PMIC.

This patch modifies the name of these registers from AXP22X_PMIC_ADC_H/L
to AXP22X_PMIC_TEMP_H/L so their purpose is clearer.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Chen-Yu Tsai <wens@csie.org>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
Acked-for-MFD-by: Lee Jones <lee.jones@linaro.org>
---

added in v2

 drivers/mfd/axp20x.c       | 2 +-
 include/linux/mfd/axp20x.h | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/mfd/axp20x.c b/drivers/mfd/axp20x.c
index 619a83e..9c2fd37 100644
--- a/drivers/mfd/axp20x.c
+++ b/drivers/mfd/axp20x.c
@@ -102,7 +102,7 @@ static const struct regmap_range axp22x_volatile_ranges[] = {
 	regmap_reg_range(AXP20X_VBUS_IPSOUT_MGMT, AXP20X_VBUS_IPSOUT_MGMT),
 	regmap_reg_range(AXP20X_IRQ1_EN, AXP20X_IRQ5_STATE),
 	regmap_reg_range(AXP22X_GPIO_STATE, AXP22X_GPIO_STATE),
-	regmap_reg_range(AXP22X_PMIC_ADC_H, AXP20X_IPSOUT_V_HIGH_L),
+	regmap_reg_range(AXP22X_PMIC_TEMP_H, AXP20X_IPSOUT_V_HIGH_L),
 	regmap_reg_range(AXP20X_FG_RES, AXP20X_FG_RES),
 };
 
diff --git a/include/linux/mfd/axp20x.h b/include/linux/mfd/axp20x.h
index a4860bc..5ecadbb 100644
--- a/include/linux/mfd/axp20x.h
+++ b/include/linux/mfd/axp20x.h
@@ -228,8 +228,8 @@ enum {
 #define AXP20X_OCV_MAX			0xf
 
 /* AXP22X specific registers */
-#define AXP22X_PMIC_ADC_H		0x56
-#define AXP22X_PMIC_ADC_L		0x57
+#define AXP22X_PMIC_TEMP_H		0x56
+#define AXP22X_PMIC_TEMP_L		0x57
 #define AXP22X_TS_ADC_H			0x58
 #define AXP22X_TS_ADC_L			0x59
 #define AXP22X_BATLOW_THRES1		0xe6
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 03/18] mfd: axp20x: correct name of temperature data ADC registers
@ 2017-02-14  9:40   ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:40 UTC (permalink / raw)
  To: linux-arm-kernel

The registers 0x56 and 0x57 of AXP22X PMIC store the value of the
internal temperature of the PMIC.

This patch modifies the name of these registers from AXP22X_PMIC_ADC_H/L
to AXP22X_PMIC_TEMP_H/L so their purpose is clearer.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Chen-Yu Tsai <wens@csie.org>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
Acked-for-MFD-by: Lee Jones <lee.jones@linaro.org>
---

added in v2

 drivers/mfd/axp20x.c       | 2 +-
 include/linux/mfd/axp20x.h | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/mfd/axp20x.c b/drivers/mfd/axp20x.c
index 619a83e..9c2fd37 100644
--- a/drivers/mfd/axp20x.c
+++ b/drivers/mfd/axp20x.c
@@ -102,7 +102,7 @@ static const struct regmap_range axp22x_volatile_ranges[] = {
 	regmap_reg_range(AXP20X_VBUS_IPSOUT_MGMT, AXP20X_VBUS_IPSOUT_MGMT),
 	regmap_reg_range(AXP20X_IRQ1_EN, AXP20X_IRQ5_STATE),
 	regmap_reg_range(AXP22X_GPIO_STATE, AXP22X_GPIO_STATE),
-	regmap_reg_range(AXP22X_PMIC_ADC_H, AXP20X_IPSOUT_V_HIGH_L),
+	regmap_reg_range(AXP22X_PMIC_TEMP_H, AXP20X_IPSOUT_V_HIGH_L),
 	regmap_reg_range(AXP20X_FG_RES, AXP20X_FG_RES),
 };
 
diff --git a/include/linux/mfd/axp20x.h b/include/linux/mfd/axp20x.h
index a4860bc..5ecadbb 100644
--- a/include/linux/mfd/axp20x.h
+++ b/include/linux/mfd/axp20x.h
@@ -228,8 +228,8 @@ enum {
 #define AXP20X_OCV_MAX			0xf
 
 /* AXP22X specific registers */
-#define AXP22X_PMIC_ADC_H		0x56
-#define AXP22X_PMIC_ADC_L		0x57
+#define AXP22X_PMIC_TEMP_H		0x56
+#define AXP22X_PMIC_TEMP_L		0x57
 #define AXP22X_TS_ADC_H			0x58
 #define AXP22X_TS_ADC_L			0x59
 #define AXP22X_BATLOW_THRES1		0xe6
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 04/18] iio: adc: add support for X-Powers AXP20X and AXP22X PMICs ADCs
  2017-02-14  9:40 ` Quentin Schulz
  (?)
@ 2017-02-14  9:40   ` Quentin Schulz
  -1 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:40 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: Quentin Schulz, linux-iio, devicetree, linux-kernel, linux-pm,
	linux-arm-kernel, thomas.petazzoni, icenowy, bonbons

The X-Powers AXP20X and AXP22X PMICs have multiple ADCs. They expose the
battery voltage, battery charge and discharge currents, AC-in and VBUS
voltages and currents, 2 GPIOs muxable in ADC mode and PMIC temperature.

This adds support for most of AXP20X and AXP22X ADCs.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
---

v3:
 - moved from switch to if condition in axp20x_adc_raw and
 axp22x_adc_raw,
 - removed DT support as DT node has been dropped,
   - use of platform_device_id
 - correctly defined the name of the iio device (name used to probe the
 driver),
 - added goto for errors in probe,
 - added iio_map_array_unregister to the remove function,

v2:
 - removed unused defines,
 - changed BIT(x) to 1 << x when describing bits purpose for which 2 <<
 x or 3 << x exists, to be consistent,
 - changed ADC rate defines to macro formulas,
 - reordered IIO channels, now different measures (current/voltage) of
 the same part of the PMIC (e.g. battery), have the same IIO channel in
 their respective IIO type. When a part of the PMIC have only one
 measure, a number is jumped,
 - left IIO channel mapping in DT to use iio_map structure,
 - removed indexing of ADC internal temperature,
 - removed unused iio_dev structure in axp20x_adc_iio,
 - added a structure for data specific to AXP20X or AXP22X PMICs instead
 of using an ID and an if condition when needing to separate the
 behaviour of both,
 - added a comment on batt_chrg_i really being on 12bits rather than
 what the Chinese datasheets say (13 bits),
 - corrected the offset for AXP22X PMIC temperature,
 - set the ADC rate to a value (100Hz) shared by the AXP20X and AXP22X,
 - created macro formulas to compute the ADC rate for each,
 - added a condition on presence of ADC_EN2 reg before setting/resetting
 it,
 - switched from devm_iio_device_unregister to the non-devm function
 because of the need for a remove function,
 - removed some dead code,

 drivers/iio/adc/Kconfig      |  10 +
 drivers/iio/adc/Makefile     |   1 +
 drivers/iio/adc/axp20x_adc.c | 606 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 617 insertions(+)
 create mode 100644 drivers/iio/adc/axp20x_adc.c

diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
index 9c8b558..ed17fe1 100644
--- a/drivers/iio/adc/Kconfig
+++ b/drivers/iio/adc/Kconfig
@@ -154,6 +154,16 @@ config AT91_SAMA5D2_ADC
 	  To compile this driver as a module, choose M here: the module will be
 	  called at91-sama5d2_adc.
 
+config AXP20X_ADC
+	tristate "X-Powers AXP20X and AXP22X ADC driver"
+	depends on MFD_AXP20X
+	help
+	  Say yes here to have support for X-Powers power management IC (PMIC)
+	  AXP20X and AXP22X ADC devices.
+
+	  To compile this driver as a module, choose M here: the module will be
+	  called axp20x_adc.
+
 config AXP288_ADC
 	tristate "X-Powers AXP288 ADC driver"
 	depends on MFD_AXP20X
diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile
index d36c4be..f5c28a5 100644
--- a/drivers/iio/adc/Makefile
+++ b/drivers/iio/adc/Makefile
@@ -16,6 +16,7 @@ obj-$(CONFIG_AD7887) += ad7887.o
 obj-$(CONFIG_AD799X) += ad799x.o
 obj-$(CONFIG_AT91_ADC) += at91_adc.o
 obj-$(CONFIG_AT91_SAMA5D2_ADC) += at91-sama5d2_adc.o
+obj-$(CONFIG_AXP20X_ADC) += axp20x_adc.o
 obj-$(CONFIG_AXP288_ADC) += axp288_adc.o
 obj-$(CONFIG_BCM_IPROC_ADC) += bcm_iproc_adc.o
 obj-$(CONFIG_BERLIN2_ADC) += berlin2-adc.o
diff --git a/drivers/iio/adc/axp20x_adc.c b/drivers/iio/adc/axp20x_adc.c
new file mode 100644
index 0000000..5ef6af8
--- /dev/null
+++ b/drivers/iio/adc/axp20x_adc.c
@@ -0,0 +1,606 @@
+/* ADC driver for AXP20X and AXP22X PMICs
+ *
+ * Copyright (c) 2016 Free Electrons NextThing Co.
+ *	Quentin Schulz <quentin.schulz@free-electrons.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License version 2 as published by the
+ * Free Software Foundation.
+ */
+
+#include <linux/completion.h>
+#include <linux/interrupt.h>
+#include <linux/io.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_device.h>
+#include <linux/platform_device.h>
+#include <linux/pm_runtime.h>
+#include <linux/regmap.h>
+#include <linux/thermal.h>
+
+#include <linux/iio/iio.h>
+#include <linux/iio/driver.h>
+#include <linux/iio/machine.h>
+#include <linux/mfd/axp20x.h>
+
+#define AXP20X_ADC_EN1_MASK			GENMASK(7, 0)
+
+#define AXP20X_ADC_EN2_MASK			(GENMASK(3, 2) | BIT(7))
+#define AXP22X_ADC_EN1_MASK			(GENMASK(7, 5) | BIT(0))
+
+#define AXP20X_GPIO10_IN_RANGE_GPIO0		BIT(0)
+#define AXP20X_GPIO10_IN_RANGE_GPIO1		BIT(1)
+#define AXP20X_GPIO10_IN_RANGE_GPIO0_VAL(x)	((x) & BIT(0))
+#define AXP20X_GPIO10_IN_RANGE_GPIO1_VAL(x)	(((x) & BIT(0)) << 1)
+
+#define AXP20X_ADC_RATE_MASK			GENMASK(7, 6)
+#define AXP20X_ADC_RATE_HZ(x)			((ilog2((x) / 25) << 6) & AXP20X_ADC_RATE_MASK)
+#define AXP22X_ADC_RATE_HZ(x)			((ilog2((x) / 100) << 6) & AXP20X_ADC_RATE_MASK)
+
+#define AXP20X_ADC_CHANNEL(_channel, _name, _type, _reg)	\
+	{							\
+		.type = _type,					\
+		.indexed = 1,					\
+		.channel = _channel,				\
+		.address = _reg,				\
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |	\
+				      BIT(IIO_CHAN_INFO_SCALE),	\
+		.datasheet_name = _name,			\
+	}
+
+#define AXP20X_ADC_CHANNEL_OFFSET(_channel, _name, _type, _reg) \
+	{							\
+		.type = _type,					\
+		.indexed = 1,					\
+		.channel = _channel,				\
+		.address = _reg,				\
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |	\
+				      BIT(IIO_CHAN_INFO_SCALE) |\
+				      BIT(IIO_CHAN_INFO_OFFSET),\
+		.datasheet_name = _name,			\
+	}
+
+struct axp_data;
+
+struct axp20x_adc_iio {
+	struct regmap		*regmap;
+	struct axp_data		*data;
+};
+
+enum axp20x_adc_channel_v {
+	AXP20X_ACIN_V = 0,
+	AXP20X_VBUS_V,
+	AXP20X_TS_IN,
+	AXP20X_GPIO0_V,
+	AXP20X_GPIO1_V,
+	AXP20X_IPSOUT_V,
+	AXP20X_BATT_V,
+};
+
+enum axp20x_adc_channel_i {
+	AXP20X_ACIN_I = 0,
+	AXP20X_VBUS_I,
+	AXP20X_BATT_CHRG_I,
+	AXP20X_BATT_DISCHRG_I,
+};
+
+enum axp22x_adc_channel_v {
+	AXP22X_TS_IN = 0,
+	AXP22X_BATT_V,
+};
+
+enum axp22x_adc_channel_i {
+	AXP22X_BATT_CHRG_I = 1,
+	AXP22X_BATT_DISCHRG_I,
+};
+
+static struct iio_map axp20x_maps[] = {
+	{
+		.consumer_dev_name = "axp20x-usb-power-supply",
+		.consumer_channel = "vbus_v",
+		.adc_channel_label = "vbus_v",
+	}, {
+		.consumer_dev_name = "axp20x-usb-power-supply",
+		.consumer_channel = "vbus_i",
+		.adc_channel_label = "vbus_i",
+	}, {
+		.consumer_dev_name = "axp20x-ac-power-supply",
+		.consumer_channel = "acin_v",
+		.adc_channel_label = "acin_v",
+	}, {
+		.consumer_dev_name = "axp20x-ac-power-supply",
+		.consumer_channel = "acin_i",
+		.adc_channel_label = "acin_i",
+	}, {
+		.consumer_dev_name = "axp20x-battery-power-supply",
+		.consumer_channel = "batt_v",
+		.adc_channel_label = "batt_v",
+	}, {
+		.consumer_dev_name = "axp20x-battery-power-supply",
+		.consumer_channel = "batt_chrg_i",
+		.adc_channel_label = "batt_chrg_i",
+	}, {
+		.consumer_dev_name = "axp20x-battery-power-supply",
+		.consumer_channel = "batt_dischrg_i",
+		.adc_channel_label = "batt_dischrg_i",
+	}, { /* sentinel */ }
+};
+
+static struct iio_map axp22x_maps[] = {
+	{
+		.consumer_dev_name = "axp20x-battery-power-supply",
+		.consumer_channel = "batt_v",
+		.adc_channel_label = "batt_v",
+	}, {
+		.consumer_dev_name = "axp20x-battery-power-supply",
+		.consumer_channel = "batt_chrg_i",
+		.adc_channel_label = "batt_chrg_i",
+	}, {
+		.consumer_dev_name = "axp20x-battery-power-supply",
+		.consumer_channel = "batt_dischrg_i",
+		.adc_channel_label = "batt_dischrg_i",
+	}, { /* sentinel */ }
+};
+
+/*
+ * Channels are mapped by physical system. Their channels share the same index.
+ * i.e. acin_i is in_current0_raw and acin_v is in_voltage0_raw.
+ * The only exception is for the battery. batt_v will be in_voltage6_raw and
+ * charge current in_current6_raw and discharge current will be in_current7_raw.
+ */
+static const struct iio_chan_spec axp20x_adc_channels[] = {
+	AXP20X_ADC_CHANNEL(AXP20X_ACIN_V, "acin_v", IIO_VOLTAGE,
+			   AXP20X_ACIN_V_ADC_H),
+	AXP20X_ADC_CHANNEL(AXP20X_ACIN_I, "acin_i", IIO_CURRENT,
+			   AXP20X_ACIN_I_ADC_H),
+	AXP20X_ADC_CHANNEL(AXP20X_VBUS_V, "vbus_v", IIO_VOLTAGE,
+			   AXP20X_VBUS_V_ADC_H),
+	AXP20X_ADC_CHANNEL(AXP20X_VBUS_I, "vbus_i", IIO_CURRENT,
+			   AXP20X_VBUS_I_ADC_H),
+	{
+		.type = IIO_TEMP,
+		.address = AXP20X_TEMP_ADC_H,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+				      BIT(IIO_CHAN_INFO_SCALE) |
+				      BIT(IIO_CHAN_INFO_OFFSET),
+		.datasheet_name = "pmic_temp",
+	},
+	AXP20X_ADC_CHANNEL_OFFSET(AXP20X_GPIO0_V, "gpio0_v", IIO_VOLTAGE,
+				  AXP20X_GPIO0_V_ADC_H),
+	AXP20X_ADC_CHANNEL_OFFSET(AXP20X_GPIO1_V, "gpio1_v", IIO_VOLTAGE,
+				  AXP20X_GPIO1_V_ADC_H),
+	AXP20X_ADC_CHANNEL(AXP20X_IPSOUT_V, "ipsout_v", IIO_VOLTAGE,
+			   AXP20X_IPSOUT_V_HIGH_H),
+	AXP20X_ADC_CHANNEL(AXP20X_BATT_V, "batt_v", IIO_VOLTAGE,
+			   AXP20X_BATT_V_H),
+	AXP20X_ADC_CHANNEL(AXP20X_BATT_CHRG_I, "batt_chrg_i", IIO_CURRENT,
+			   AXP20X_BATT_CHRG_I_H),
+	AXP20X_ADC_CHANNEL(AXP20X_BATT_DISCHRG_I, "batt_dischrg_i", IIO_CURRENT,
+			   AXP20X_BATT_DISCHRG_I_H),
+};
+
+static const struct iio_chan_spec axp22x_adc_channels[] = {
+	{
+		.type = IIO_TEMP,
+		.address = AXP22X_PMIC_TEMP_H,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+				      BIT(IIO_CHAN_INFO_SCALE) |
+				      BIT(IIO_CHAN_INFO_OFFSET),
+		.datasheet_name = "pmic_temp",
+	},
+	AXP20X_ADC_CHANNEL(AXP22X_BATT_V, "batt_v", IIO_VOLTAGE,
+			   AXP20X_BATT_V_H),
+	AXP20X_ADC_CHANNEL(AXP22X_BATT_CHRG_I, "batt_chrg_i", IIO_CURRENT,
+			   AXP20X_BATT_CHRG_I_H),
+	AXP20X_ADC_CHANNEL(AXP22X_BATT_DISCHRG_I, "batt_dischrg_i", IIO_CURRENT,
+			   AXP20X_BATT_DISCHRG_I_H),
+};
+
+static int axp20x_adc_raw(struct iio_dev *indio_dev,
+			  struct iio_chan_spec const *chan, int *val)
+{
+	struct axp20x_adc_iio *info = iio_priv(indio_dev);
+	int size = 12;
+
+	/*
+	 *N.B.:  Unlike the Chinese datasheets tell, the charging current is
+	 * stored on 12 bits, not 13 bits. Only discharging current is on 13
+	 * bits.
+	 */
+	if (chan->type == IIO_CURRENT && chan->channel == AXP20X_BATT_DISCHRG_I)
+		size = 13;
+	else
+		size = 12;
+
+	*val = axp20x_read_variable_width(info->regmap, chan->address, size);
+	if (*val < 0)
+		return *val;
+
+	return IIO_VAL_INT;
+}
+
+static int axp22x_adc_raw(struct iio_dev *indio_dev,
+			  struct iio_chan_spec const *chan, int *val)
+{
+	struct axp20x_adc_iio *info = iio_priv(indio_dev);
+	int size;
+
+	/*
+	 * N.B.: Unlike the Chinese datasheets tell, the charging current is
+	 * stored on 12 bits, not 13 bits. Only discharging current is on 13
+	 * bits.
+	 */
+	if (chan->type == IIO_CURRENT && chan->channel == AXP22X_BATT_DISCHRG_I)
+		size = 13;
+	else
+		size = 12;
+
+	*val = axp20x_read_variable_width(info->regmap, chan->address, size);
+	if (*val < 0)
+		return *val;
+
+	return IIO_VAL_INT;
+}
+
+static int axp20x_adc_scale_voltage(int channel, int *val, int *val2)
+{
+	switch (channel) {
+	case AXP20X_ACIN_V:
+	case AXP20X_VBUS_V:
+		*val = 1;
+		*val2 = 700000;
+		return IIO_VAL_INT_PLUS_MICRO;
+
+	case AXP20X_GPIO0_V:
+	case AXP20X_GPIO1_V:
+		*val = 0;
+		*val2 = 500000;
+		return IIO_VAL_INT_PLUS_MICRO;
+
+	case AXP20X_BATT_V:
+		*val = 1;
+		*val2 = 100000;
+		return IIO_VAL_INT_PLUS_MICRO;
+
+	case AXP20X_IPSOUT_V:
+		*val = 1;
+		*val2 = 400000;
+		return IIO_VAL_INT_PLUS_MICRO;
+
+	default:
+		return -EINVAL;
+	}
+}
+
+static int axp20x_adc_scale_current(int channel, int *val, int *val2)
+{
+	switch (channel) {
+	case AXP20X_ACIN_I:
+		*val = 0;
+		*val2 = 625000;
+		return IIO_VAL_INT_PLUS_MICRO;
+
+	case AXP20X_VBUS_I:
+		*val = 0;
+		*val2 = 375000;
+		return IIO_VAL_INT_PLUS_MICRO;
+
+	case AXP20X_BATT_DISCHRG_I:
+	case AXP20X_BATT_CHRG_I:
+		*val = 0;
+		*val2 = 500000;
+		return IIO_VAL_INT_PLUS_MICRO;
+
+	default:
+		return -EINVAL;
+	}
+}
+
+static int axp20x_adc_scale(struct iio_chan_spec const *chan, int *val,
+			    int *val2)
+{
+	switch (chan->type) {
+	case IIO_VOLTAGE:
+		return axp20x_adc_scale_voltage(chan->channel, val, val2);
+
+	case IIO_CURRENT:
+		return axp20x_adc_scale_current(chan->channel, val, val2);
+
+	case IIO_TEMP:
+		*val = 100;
+		return IIO_VAL_INT;
+
+	default:
+		return -EINVAL;
+	}
+}
+
+static int axp22x_adc_scale(struct iio_chan_spec const *chan, int *val,
+			    int *val2)
+{
+	switch (chan->type) {
+	case IIO_VOLTAGE:
+		if (chan->channel != AXP22X_BATT_V)
+			return -EINVAL;
+
+		*val = 1;
+		*val2 = 100000;
+		return IIO_VAL_INT_PLUS_MICRO;
+
+	case IIO_CURRENT:
+		*val = 0;
+		*val2 = 500000;
+		return IIO_VAL_INT_PLUS_MICRO;
+
+	case IIO_TEMP:
+		*val = 100;
+		return IIO_VAL_INT;
+
+	default:
+		return -EINVAL;
+	}
+}
+
+static int axp20x_adc_offset_voltage(struct iio_dev *indio_dev, int channel,
+				     int *val)
+{
+	struct axp20x_adc_iio *info = iio_priv(indio_dev);
+	int ret, reg;
+
+	if (channel != AXP20X_GPIO0_V && channel != AXP20X_GPIO1_V)
+		return -EINVAL;
+
+	ret = regmap_read(info->regmap, AXP20X_GPIO10_IN_RANGE, &reg);
+	if (ret < 0)
+		return ret;
+
+	if (channel == AXP20X_GPIO0_V)
+		*val = reg & AXP20X_GPIO10_IN_RANGE_GPIO0;
+	else
+		*val = reg & AXP20X_GPIO10_IN_RANGE_GPIO1;
+
+	*val = !!(*val) * 700000;
+
+	return IIO_VAL_INT;
+}
+
+static int axp20x_adc_offset(struct iio_dev *indio_dev,
+			     struct iio_chan_spec const *chan, int *val)
+{
+	switch (chan->type) {
+	case IIO_VOLTAGE:
+		return axp20x_adc_offset_voltage(indio_dev, chan->channel, val);
+
+	case IIO_TEMP:
+		*val = -1447;
+		return IIO_VAL_INT;
+
+	default:
+		return -EINVAL;
+	}
+}
+
+static int axp20x_read_raw(struct iio_dev *indio_dev,
+			   struct iio_chan_spec const *chan, int *val,
+			   int *val2, long mask)
+{
+	switch (mask) {
+	case IIO_CHAN_INFO_OFFSET:
+		return axp20x_adc_offset(indio_dev, chan, val);
+
+	case IIO_CHAN_INFO_SCALE:
+		return axp20x_adc_scale(chan, val, val2);
+
+	case IIO_CHAN_INFO_RAW:
+		return axp20x_adc_raw(indio_dev, chan, val);
+
+	default:
+		return -EINVAL;
+	}
+}
+
+static int axp22x_read_raw(struct iio_dev *indio_dev,
+			   struct iio_chan_spec const *chan, int *val,
+			   int *val2, long mask)
+{
+	switch (mask) {
+	case IIO_CHAN_INFO_OFFSET:
+		*val = -2677;
+		return IIO_VAL_INT;
+
+	case IIO_CHAN_INFO_SCALE:
+		return axp22x_adc_scale(chan, val, val2);
+
+	case IIO_CHAN_INFO_RAW:
+		return axp22x_adc_raw(indio_dev, chan, val);
+
+	default:
+		return -EINVAL;
+	}
+}
+
+static int axp20x_write_raw(struct iio_dev *indio_dev,
+			    struct iio_chan_spec const *chan, int val, int val2,
+			    long mask)
+{
+	struct axp20x_adc_iio *info = iio_priv(indio_dev);
+
+	/*
+	 * The AXP20X PMIC allows the user to choose between 0V and 0.7V offsets
+	 * for (independently) GPIO0 and GPIO1 when in ADC mode.
+	 */
+	if (mask != IIO_CHAN_INFO_OFFSET)
+		return -EINVAL;
+
+	if (chan->channel != AXP20X_GPIO0_V && chan->channel != AXP20X_GPIO1_V)
+		return -EINVAL;
+
+	if (val != 0 && val != 700000)
+		return -EINVAL;
+
+	if (chan->channel == AXP20X_GPIO0_V)
+		return regmap_update_bits(info->regmap, AXP20X_GPIO10_IN_RANGE,
+					  AXP20X_GPIO10_IN_RANGE_GPIO0,
+					  AXP20X_GPIO10_IN_RANGE_GPIO0_VAL(!!val));
+
+	return regmap_update_bits(info->regmap, AXP20X_GPIO10_IN_RANGE,
+				  AXP20X_GPIO10_IN_RANGE_GPIO1,
+				  AXP20X_GPIO10_IN_RANGE_GPIO1_VAL(!!val));
+}
+
+static const struct iio_info axp20x_adc_iio_info = {
+	.read_raw = axp20x_read_raw,
+	.write_raw = axp20x_write_raw,
+	.driver_module = THIS_MODULE,
+};
+
+static const struct iio_info axp22x_adc_iio_info = {
+	.read_raw = axp22x_read_raw,
+	.driver_module = THIS_MODULE,
+};
+
+static int axp20x_adc_rate(int rate)
+{
+	return AXP20X_ADC_RATE_HZ(rate);
+}
+
+static int axp22x_adc_rate(int rate)
+{
+	return AXP22X_ADC_RATE_HZ(rate);
+}
+
+struct axp_data {
+	const struct iio_info		*iio_info;
+	int				num_channels;
+	struct iio_chan_spec const	*channels;
+	unsigned long			adc_en1_mask;
+	int				(*adc_rate)(int rate);
+	bool				adc_en2;
+	struct iio_map			*maps;
+};
+
+static const struct axp_data axp20x_data = {
+	.iio_info = &axp20x_adc_iio_info,
+	.num_channels = ARRAY_SIZE(axp20x_adc_channels),
+	.channels = axp20x_adc_channels,
+	.adc_en1_mask = AXP20X_ADC_EN1_MASK,
+	.adc_rate = axp20x_adc_rate,
+	.adc_en2 = true,
+	.maps = axp20x_maps,
+};
+
+static const struct axp_data axp22x_data = {
+	.iio_info = &axp22x_adc_iio_info,
+	.num_channels = ARRAY_SIZE(axp22x_adc_channels),
+	.channels = axp22x_adc_channels,
+	.adc_en1_mask = AXP22X_ADC_EN1_MASK,
+	.adc_rate = axp22x_adc_rate,
+	.adc_en2 = false,
+	.maps = axp22x_maps,
+};
+
+static const struct platform_device_id axp20x_adc_id_match[] = {
+	{ .name = "axp20x-adc", .driver_data = (kernel_ulong_t)&axp20x_data, },
+	{ .name = "axp22x-adc", .driver_data = (kernel_ulong_t)&axp22x_data, },
+	{ /* sentinel */ },
+};
+
+static int axp20x_probe(struct platform_device *pdev)
+{
+	struct axp20x_adc_iio *info;
+	struct iio_dev *indio_dev;
+	struct axp20x_dev *axp20x_dev;
+	int ret;
+
+	axp20x_dev = dev_get_drvdata(pdev->dev.parent);
+
+	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*info));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	info = iio_priv(indio_dev);
+	platform_set_drvdata(pdev, indio_dev);
+
+	info->regmap = axp20x_dev->regmap;
+	indio_dev->dev.parent = &pdev->dev;
+	indio_dev->dev.of_node = pdev->dev.of_node;
+	indio_dev->modes = INDIO_DIRECT_MODE;
+
+	info->data = (struct axp_data *)platform_get_device_id(pdev)->driver_data;
+
+	indio_dev->name = platform_get_device_id(pdev)->name;
+	indio_dev->info = info->data->iio_info;
+	indio_dev->num_channels = info->data->num_channels;
+	indio_dev->channels = info->data->channels;
+
+	/* Enable the ADCs on IP */
+	regmap_write(info->regmap, AXP20X_ADC_EN1, info->data->adc_en1_mask);
+
+	if (info->data->adc_en2)
+		/* Enable GPIO0/1 and internal temperature ADCs */
+		regmap_update_bits(info->regmap, AXP20X_ADC_EN2,
+				   AXP20X_ADC_EN2_MASK, AXP20X_ADC_EN2_MASK);
+
+	/* Configure ADCs rate */
+	regmap_update_bits(info->regmap, AXP20X_ADC_RATE, AXP20X_ADC_RATE_MASK,
+			   info->data->adc_rate(100));
+
+	ret = iio_map_array_register(indio_dev, info->data->maps);
+	if (ret < 0) {
+		dev_err(&pdev->dev, "failed to register IIO maps: %d\n", ret);
+		goto fail_map;
+	}
+
+	ret = iio_device_register(indio_dev);
+	if (ret < 0) {
+		dev_err(&pdev->dev, "could not register the device\n");
+		goto fail_register;
+	}
+
+	return 0;
+
+fail_register:
+	iio_map_array_unregister(indio_dev);
+
+fail_map:
+	regmap_write(info->regmap, AXP20X_ADC_EN1, 0);
+
+	if (info->data->adc_en2)
+		regmap_write(info->regmap, AXP20X_ADC_EN2, 0);
+
+	return ret;
+}
+
+static int axp20x_remove(struct platform_device *pdev)
+{
+	struct axp20x_adc_iio *info;
+	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
+
+	info = iio_priv(indio_dev);
+
+	iio_device_unregister(indio_dev);
+	iio_map_array_unregister(indio_dev);
+
+	regmap_write(info->regmap, AXP20X_ADC_EN1, 0);
+
+	if (info->data->adc_en2)
+		regmap_write(info->regmap, AXP20X_ADC_EN2, 0);
+
+	return 0;
+}
+
+static struct platform_driver axp20x_adc_driver = {
+	.driver = {
+		.name = "axp20x-adc",
+	},
+	.id_table = axp20x_adc_id_match,
+	.probe = axp20x_probe,
+	.remove = axp20x_remove,
+};
+
+module_platform_driver(axp20x_adc_driver);
+
+MODULE_DESCRIPTION("ADC driver for AXP20X and AXP22X PMICs");
+MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
+MODULE_LICENSE("GPL");
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 04/18] iio: adc: add support for X-Powers AXP20X and AXP22X PMICs ADCs
@ 2017-02-14  9:40   ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:40 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: thomas.petazzoni, devicetree, linux-pm, linux-iio, linux-kernel,
	Quentin Schulz, bonbons, icenowy, linux-arm-kernel

The X-Powers AXP20X and AXP22X PMICs have multiple ADCs. They expose the
battery voltage, battery charge and discharge currents, AC-in and VBUS
voltages and currents, 2 GPIOs muxable in ADC mode and PMIC temperature.

This adds support for most of AXP20X and AXP22X ADCs.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
---

v3:
 - moved from switch to if condition in axp20x_adc_raw and
 axp22x_adc_raw,
 - removed DT support as DT node has been dropped,
   - use of platform_device_id
 - correctly defined the name of the iio device (name used to probe the
 driver),
 - added goto for errors in probe,
 - added iio_map_array_unregister to the remove function,

v2:
 - removed unused defines,
 - changed BIT(x) to 1 << x when describing bits purpose for which 2 <<
 x or 3 << x exists, to be consistent,
 - changed ADC rate defines to macro formulas,
 - reordered IIO channels, now different measures (current/voltage) of
 the same part of the PMIC (e.g. battery), have the same IIO channel in
 their respective IIO type. When a part of the PMIC have only one
 measure, a number is jumped,
 - left IIO channel mapping in DT to use iio_map structure,
 - removed indexing of ADC internal temperature,
 - removed unused iio_dev structure in axp20x_adc_iio,
 - added a structure for data specific to AXP20X or AXP22X PMICs instead
 of using an ID and an if condition when needing to separate the
 behaviour of both,
 - added a comment on batt_chrg_i really being on 12bits rather than
 what the Chinese datasheets say (13 bits),
 - corrected the offset for AXP22X PMIC temperature,
 - set the ADC rate to a value (100Hz) shared by the AXP20X and AXP22X,
 - created macro formulas to compute the ADC rate for each,
 - added a condition on presence of ADC_EN2 reg before setting/resetting
 it,
 - switched from devm_iio_device_unregister to the non-devm function
 because of the need for a remove function,
 - removed some dead code,

 drivers/iio/adc/Kconfig      |  10 +
 drivers/iio/adc/Makefile     |   1 +
 drivers/iio/adc/axp20x_adc.c | 606 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 617 insertions(+)
 create mode 100644 drivers/iio/adc/axp20x_adc.c

diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
index 9c8b558..ed17fe1 100644
--- a/drivers/iio/adc/Kconfig
+++ b/drivers/iio/adc/Kconfig
@@ -154,6 +154,16 @@ config AT91_SAMA5D2_ADC
 	  To compile this driver as a module, choose M here: the module will be
 	  called at91-sama5d2_adc.
 
+config AXP20X_ADC
+	tristate "X-Powers AXP20X and AXP22X ADC driver"
+	depends on MFD_AXP20X
+	help
+	  Say yes here to have support for X-Powers power management IC (PMIC)
+	  AXP20X and AXP22X ADC devices.
+
+	  To compile this driver as a module, choose M here: the module will be
+	  called axp20x_adc.
+
 config AXP288_ADC
 	tristate "X-Powers AXP288 ADC driver"
 	depends on MFD_AXP20X
diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile
index d36c4be..f5c28a5 100644
--- a/drivers/iio/adc/Makefile
+++ b/drivers/iio/adc/Makefile
@@ -16,6 +16,7 @@ obj-$(CONFIG_AD7887) += ad7887.o
 obj-$(CONFIG_AD799X) += ad799x.o
 obj-$(CONFIG_AT91_ADC) += at91_adc.o
 obj-$(CONFIG_AT91_SAMA5D2_ADC) += at91-sama5d2_adc.o
+obj-$(CONFIG_AXP20X_ADC) += axp20x_adc.o
 obj-$(CONFIG_AXP288_ADC) += axp288_adc.o
 obj-$(CONFIG_BCM_IPROC_ADC) += bcm_iproc_adc.o
 obj-$(CONFIG_BERLIN2_ADC) += berlin2-adc.o
diff --git a/drivers/iio/adc/axp20x_adc.c b/drivers/iio/adc/axp20x_adc.c
new file mode 100644
index 0000000..5ef6af8
--- /dev/null
+++ b/drivers/iio/adc/axp20x_adc.c
@@ -0,0 +1,606 @@
+/* ADC driver for AXP20X and AXP22X PMICs
+ *
+ * Copyright (c) 2016 Free Electrons NextThing Co.
+ *	Quentin Schulz <quentin.schulz@free-electrons.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License version 2 as published by the
+ * Free Software Foundation.
+ */
+
+#include <linux/completion.h>
+#include <linux/interrupt.h>
+#include <linux/io.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_device.h>
+#include <linux/platform_device.h>
+#include <linux/pm_runtime.h>
+#include <linux/regmap.h>
+#include <linux/thermal.h>
+
+#include <linux/iio/iio.h>
+#include <linux/iio/driver.h>
+#include <linux/iio/machine.h>
+#include <linux/mfd/axp20x.h>
+
+#define AXP20X_ADC_EN1_MASK			GENMASK(7, 0)
+
+#define AXP20X_ADC_EN2_MASK			(GENMASK(3, 2) | BIT(7))
+#define AXP22X_ADC_EN1_MASK			(GENMASK(7, 5) | BIT(0))
+
+#define AXP20X_GPIO10_IN_RANGE_GPIO0		BIT(0)
+#define AXP20X_GPIO10_IN_RANGE_GPIO1		BIT(1)
+#define AXP20X_GPIO10_IN_RANGE_GPIO0_VAL(x)	((x) & BIT(0))
+#define AXP20X_GPIO10_IN_RANGE_GPIO1_VAL(x)	(((x) & BIT(0)) << 1)
+
+#define AXP20X_ADC_RATE_MASK			GENMASK(7, 6)
+#define AXP20X_ADC_RATE_HZ(x)			((ilog2((x) / 25) << 6) & AXP20X_ADC_RATE_MASK)
+#define AXP22X_ADC_RATE_HZ(x)			((ilog2((x) / 100) << 6) & AXP20X_ADC_RATE_MASK)
+
+#define AXP20X_ADC_CHANNEL(_channel, _name, _type, _reg)	\
+	{							\
+		.type = _type,					\
+		.indexed = 1,					\
+		.channel = _channel,				\
+		.address = _reg,				\
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |	\
+				      BIT(IIO_CHAN_INFO_SCALE),	\
+		.datasheet_name = _name,			\
+	}
+
+#define AXP20X_ADC_CHANNEL_OFFSET(_channel, _name, _type, _reg) \
+	{							\
+		.type = _type,					\
+		.indexed = 1,					\
+		.channel = _channel,				\
+		.address = _reg,				\
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |	\
+				      BIT(IIO_CHAN_INFO_SCALE) |\
+				      BIT(IIO_CHAN_INFO_OFFSET),\
+		.datasheet_name = _name,			\
+	}
+
+struct axp_data;
+
+struct axp20x_adc_iio {
+	struct regmap		*regmap;
+	struct axp_data		*data;
+};
+
+enum axp20x_adc_channel_v {
+	AXP20X_ACIN_V = 0,
+	AXP20X_VBUS_V,
+	AXP20X_TS_IN,
+	AXP20X_GPIO0_V,
+	AXP20X_GPIO1_V,
+	AXP20X_IPSOUT_V,
+	AXP20X_BATT_V,
+};
+
+enum axp20x_adc_channel_i {
+	AXP20X_ACIN_I = 0,
+	AXP20X_VBUS_I,
+	AXP20X_BATT_CHRG_I,
+	AXP20X_BATT_DISCHRG_I,
+};
+
+enum axp22x_adc_channel_v {
+	AXP22X_TS_IN = 0,
+	AXP22X_BATT_V,
+};
+
+enum axp22x_adc_channel_i {
+	AXP22X_BATT_CHRG_I = 1,
+	AXP22X_BATT_DISCHRG_I,
+};
+
+static struct iio_map axp20x_maps[] = {
+	{
+		.consumer_dev_name = "axp20x-usb-power-supply",
+		.consumer_channel = "vbus_v",
+		.adc_channel_label = "vbus_v",
+	}, {
+		.consumer_dev_name = "axp20x-usb-power-supply",
+		.consumer_channel = "vbus_i",
+		.adc_channel_label = "vbus_i",
+	}, {
+		.consumer_dev_name = "axp20x-ac-power-supply",
+		.consumer_channel = "acin_v",
+		.adc_channel_label = "acin_v",
+	}, {
+		.consumer_dev_name = "axp20x-ac-power-supply",
+		.consumer_channel = "acin_i",
+		.adc_channel_label = "acin_i",
+	}, {
+		.consumer_dev_name = "axp20x-battery-power-supply",
+		.consumer_channel = "batt_v",
+		.adc_channel_label = "batt_v",
+	}, {
+		.consumer_dev_name = "axp20x-battery-power-supply",
+		.consumer_channel = "batt_chrg_i",
+		.adc_channel_label = "batt_chrg_i",
+	}, {
+		.consumer_dev_name = "axp20x-battery-power-supply",
+		.consumer_channel = "batt_dischrg_i",
+		.adc_channel_label = "batt_dischrg_i",
+	}, { /* sentinel */ }
+};
+
+static struct iio_map axp22x_maps[] = {
+	{
+		.consumer_dev_name = "axp20x-battery-power-supply",
+		.consumer_channel = "batt_v",
+		.adc_channel_label = "batt_v",
+	}, {
+		.consumer_dev_name = "axp20x-battery-power-supply",
+		.consumer_channel = "batt_chrg_i",
+		.adc_channel_label = "batt_chrg_i",
+	}, {
+		.consumer_dev_name = "axp20x-battery-power-supply",
+		.consumer_channel = "batt_dischrg_i",
+		.adc_channel_label = "batt_dischrg_i",
+	}, { /* sentinel */ }
+};
+
+/*
+ * Channels are mapped by physical system. Their channels share the same index.
+ * i.e. acin_i is in_current0_raw and acin_v is in_voltage0_raw.
+ * The only exception is for the battery. batt_v will be in_voltage6_raw and
+ * charge current in_current6_raw and discharge current will be in_current7_raw.
+ */
+static const struct iio_chan_spec axp20x_adc_channels[] = {
+	AXP20X_ADC_CHANNEL(AXP20X_ACIN_V, "acin_v", IIO_VOLTAGE,
+			   AXP20X_ACIN_V_ADC_H),
+	AXP20X_ADC_CHANNEL(AXP20X_ACIN_I, "acin_i", IIO_CURRENT,
+			   AXP20X_ACIN_I_ADC_H),
+	AXP20X_ADC_CHANNEL(AXP20X_VBUS_V, "vbus_v", IIO_VOLTAGE,
+			   AXP20X_VBUS_V_ADC_H),
+	AXP20X_ADC_CHANNEL(AXP20X_VBUS_I, "vbus_i", IIO_CURRENT,
+			   AXP20X_VBUS_I_ADC_H),
+	{
+		.type = IIO_TEMP,
+		.address = AXP20X_TEMP_ADC_H,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+				      BIT(IIO_CHAN_INFO_SCALE) |
+				      BIT(IIO_CHAN_INFO_OFFSET),
+		.datasheet_name = "pmic_temp",
+	},
+	AXP20X_ADC_CHANNEL_OFFSET(AXP20X_GPIO0_V, "gpio0_v", IIO_VOLTAGE,
+				  AXP20X_GPIO0_V_ADC_H),
+	AXP20X_ADC_CHANNEL_OFFSET(AXP20X_GPIO1_V, "gpio1_v", IIO_VOLTAGE,
+				  AXP20X_GPIO1_V_ADC_H),
+	AXP20X_ADC_CHANNEL(AXP20X_IPSOUT_V, "ipsout_v", IIO_VOLTAGE,
+			   AXP20X_IPSOUT_V_HIGH_H),
+	AXP20X_ADC_CHANNEL(AXP20X_BATT_V, "batt_v", IIO_VOLTAGE,
+			   AXP20X_BATT_V_H),
+	AXP20X_ADC_CHANNEL(AXP20X_BATT_CHRG_I, "batt_chrg_i", IIO_CURRENT,
+			   AXP20X_BATT_CHRG_I_H),
+	AXP20X_ADC_CHANNEL(AXP20X_BATT_DISCHRG_I, "batt_dischrg_i", IIO_CURRENT,
+			   AXP20X_BATT_DISCHRG_I_H),
+};
+
+static const struct iio_chan_spec axp22x_adc_channels[] = {
+	{
+		.type = IIO_TEMP,
+		.address = AXP22X_PMIC_TEMP_H,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+				      BIT(IIO_CHAN_INFO_SCALE) |
+				      BIT(IIO_CHAN_INFO_OFFSET),
+		.datasheet_name = "pmic_temp",
+	},
+	AXP20X_ADC_CHANNEL(AXP22X_BATT_V, "batt_v", IIO_VOLTAGE,
+			   AXP20X_BATT_V_H),
+	AXP20X_ADC_CHANNEL(AXP22X_BATT_CHRG_I, "batt_chrg_i", IIO_CURRENT,
+			   AXP20X_BATT_CHRG_I_H),
+	AXP20X_ADC_CHANNEL(AXP22X_BATT_DISCHRG_I, "batt_dischrg_i", IIO_CURRENT,
+			   AXP20X_BATT_DISCHRG_I_H),
+};
+
+static int axp20x_adc_raw(struct iio_dev *indio_dev,
+			  struct iio_chan_spec const *chan, int *val)
+{
+	struct axp20x_adc_iio *info = iio_priv(indio_dev);
+	int size = 12;
+
+	/*
+	 *N.B.:  Unlike the Chinese datasheets tell, the charging current is
+	 * stored on 12 bits, not 13 bits. Only discharging current is on 13
+	 * bits.
+	 */
+	if (chan->type == IIO_CURRENT && chan->channel == AXP20X_BATT_DISCHRG_I)
+		size = 13;
+	else
+		size = 12;
+
+	*val = axp20x_read_variable_width(info->regmap, chan->address, size);
+	if (*val < 0)
+		return *val;
+
+	return IIO_VAL_INT;
+}
+
+static int axp22x_adc_raw(struct iio_dev *indio_dev,
+			  struct iio_chan_spec const *chan, int *val)
+{
+	struct axp20x_adc_iio *info = iio_priv(indio_dev);
+	int size;
+
+	/*
+	 * N.B.: Unlike the Chinese datasheets tell, the charging current is
+	 * stored on 12 bits, not 13 bits. Only discharging current is on 13
+	 * bits.
+	 */
+	if (chan->type == IIO_CURRENT && chan->channel == AXP22X_BATT_DISCHRG_I)
+		size = 13;
+	else
+		size = 12;
+
+	*val = axp20x_read_variable_width(info->regmap, chan->address, size);
+	if (*val < 0)
+		return *val;
+
+	return IIO_VAL_INT;
+}
+
+static int axp20x_adc_scale_voltage(int channel, int *val, int *val2)
+{
+	switch (channel) {
+	case AXP20X_ACIN_V:
+	case AXP20X_VBUS_V:
+		*val = 1;
+		*val2 = 700000;
+		return IIO_VAL_INT_PLUS_MICRO;
+
+	case AXP20X_GPIO0_V:
+	case AXP20X_GPIO1_V:
+		*val = 0;
+		*val2 = 500000;
+		return IIO_VAL_INT_PLUS_MICRO;
+
+	case AXP20X_BATT_V:
+		*val = 1;
+		*val2 = 100000;
+		return IIO_VAL_INT_PLUS_MICRO;
+
+	case AXP20X_IPSOUT_V:
+		*val = 1;
+		*val2 = 400000;
+		return IIO_VAL_INT_PLUS_MICRO;
+
+	default:
+		return -EINVAL;
+	}
+}
+
+static int axp20x_adc_scale_current(int channel, int *val, int *val2)
+{
+	switch (channel) {
+	case AXP20X_ACIN_I:
+		*val = 0;
+		*val2 = 625000;
+		return IIO_VAL_INT_PLUS_MICRO;
+
+	case AXP20X_VBUS_I:
+		*val = 0;
+		*val2 = 375000;
+		return IIO_VAL_INT_PLUS_MICRO;
+
+	case AXP20X_BATT_DISCHRG_I:
+	case AXP20X_BATT_CHRG_I:
+		*val = 0;
+		*val2 = 500000;
+		return IIO_VAL_INT_PLUS_MICRO;
+
+	default:
+		return -EINVAL;
+	}
+}
+
+static int axp20x_adc_scale(struct iio_chan_spec const *chan, int *val,
+			    int *val2)
+{
+	switch (chan->type) {
+	case IIO_VOLTAGE:
+		return axp20x_adc_scale_voltage(chan->channel, val, val2);
+
+	case IIO_CURRENT:
+		return axp20x_adc_scale_current(chan->channel, val, val2);
+
+	case IIO_TEMP:
+		*val = 100;
+		return IIO_VAL_INT;
+
+	default:
+		return -EINVAL;
+	}
+}
+
+static int axp22x_adc_scale(struct iio_chan_spec const *chan, int *val,
+			    int *val2)
+{
+	switch (chan->type) {
+	case IIO_VOLTAGE:
+		if (chan->channel != AXP22X_BATT_V)
+			return -EINVAL;
+
+		*val = 1;
+		*val2 = 100000;
+		return IIO_VAL_INT_PLUS_MICRO;
+
+	case IIO_CURRENT:
+		*val = 0;
+		*val2 = 500000;
+		return IIO_VAL_INT_PLUS_MICRO;
+
+	case IIO_TEMP:
+		*val = 100;
+		return IIO_VAL_INT;
+
+	default:
+		return -EINVAL;
+	}
+}
+
+static int axp20x_adc_offset_voltage(struct iio_dev *indio_dev, int channel,
+				     int *val)
+{
+	struct axp20x_adc_iio *info = iio_priv(indio_dev);
+	int ret, reg;
+
+	if (channel != AXP20X_GPIO0_V && channel != AXP20X_GPIO1_V)
+		return -EINVAL;
+
+	ret = regmap_read(info->regmap, AXP20X_GPIO10_IN_RANGE, &reg);
+	if (ret < 0)
+		return ret;
+
+	if (channel == AXP20X_GPIO0_V)
+		*val = reg & AXP20X_GPIO10_IN_RANGE_GPIO0;
+	else
+		*val = reg & AXP20X_GPIO10_IN_RANGE_GPIO1;
+
+	*val = !!(*val) * 700000;
+
+	return IIO_VAL_INT;
+}
+
+static int axp20x_adc_offset(struct iio_dev *indio_dev,
+			     struct iio_chan_spec const *chan, int *val)
+{
+	switch (chan->type) {
+	case IIO_VOLTAGE:
+		return axp20x_adc_offset_voltage(indio_dev, chan->channel, val);
+
+	case IIO_TEMP:
+		*val = -1447;
+		return IIO_VAL_INT;
+
+	default:
+		return -EINVAL;
+	}
+}
+
+static int axp20x_read_raw(struct iio_dev *indio_dev,
+			   struct iio_chan_spec const *chan, int *val,
+			   int *val2, long mask)
+{
+	switch (mask) {
+	case IIO_CHAN_INFO_OFFSET:
+		return axp20x_adc_offset(indio_dev, chan, val);
+
+	case IIO_CHAN_INFO_SCALE:
+		return axp20x_adc_scale(chan, val, val2);
+
+	case IIO_CHAN_INFO_RAW:
+		return axp20x_adc_raw(indio_dev, chan, val);
+
+	default:
+		return -EINVAL;
+	}
+}
+
+static int axp22x_read_raw(struct iio_dev *indio_dev,
+			   struct iio_chan_spec const *chan, int *val,
+			   int *val2, long mask)
+{
+	switch (mask) {
+	case IIO_CHAN_INFO_OFFSET:
+		*val = -2677;
+		return IIO_VAL_INT;
+
+	case IIO_CHAN_INFO_SCALE:
+		return axp22x_adc_scale(chan, val, val2);
+
+	case IIO_CHAN_INFO_RAW:
+		return axp22x_adc_raw(indio_dev, chan, val);
+
+	default:
+		return -EINVAL;
+	}
+}
+
+static int axp20x_write_raw(struct iio_dev *indio_dev,
+			    struct iio_chan_spec const *chan, int val, int val2,
+			    long mask)
+{
+	struct axp20x_adc_iio *info = iio_priv(indio_dev);
+
+	/*
+	 * The AXP20X PMIC allows the user to choose between 0V and 0.7V offsets
+	 * for (independently) GPIO0 and GPIO1 when in ADC mode.
+	 */
+	if (mask != IIO_CHAN_INFO_OFFSET)
+		return -EINVAL;
+
+	if (chan->channel != AXP20X_GPIO0_V && chan->channel != AXP20X_GPIO1_V)
+		return -EINVAL;
+
+	if (val != 0 && val != 700000)
+		return -EINVAL;
+
+	if (chan->channel == AXP20X_GPIO0_V)
+		return regmap_update_bits(info->regmap, AXP20X_GPIO10_IN_RANGE,
+					  AXP20X_GPIO10_IN_RANGE_GPIO0,
+					  AXP20X_GPIO10_IN_RANGE_GPIO0_VAL(!!val));
+
+	return regmap_update_bits(info->regmap, AXP20X_GPIO10_IN_RANGE,
+				  AXP20X_GPIO10_IN_RANGE_GPIO1,
+				  AXP20X_GPIO10_IN_RANGE_GPIO1_VAL(!!val));
+}
+
+static const struct iio_info axp20x_adc_iio_info = {
+	.read_raw = axp20x_read_raw,
+	.write_raw = axp20x_write_raw,
+	.driver_module = THIS_MODULE,
+};
+
+static const struct iio_info axp22x_adc_iio_info = {
+	.read_raw = axp22x_read_raw,
+	.driver_module = THIS_MODULE,
+};
+
+static int axp20x_adc_rate(int rate)
+{
+	return AXP20X_ADC_RATE_HZ(rate);
+}
+
+static int axp22x_adc_rate(int rate)
+{
+	return AXP22X_ADC_RATE_HZ(rate);
+}
+
+struct axp_data {
+	const struct iio_info		*iio_info;
+	int				num_channels;
+	struct iio_chan_spec const	*channels;
+	unsigned long			adc_en1_mask;
+	int				(*adc_rate)(int rate);
+	bool				adc_en2;
+	struct iio_map			*maps;
+};
+
+static const struct axp_data axp20x_data = {
+	.iio_info = &axp20x_adc_iio_info,
+	.num_channels = ARRAY_SIZE(axp20x_adc_channels),
+	.channels = axp20x_adc_channels,
+	.adc_en1_mask = AXP20X_ADC_EN1_MASK,
+	.adc_rate = axp20x_adc_rate,
+	.adc_en2 = true,
+	.maps = axp20x_maps,
+};
+
+static const struct axp_data axp22x_data = {
+	.iio_info = &axp22x_adc_iio_info,
+	.num_channels = ARRAY_SIZE(axp22x_adc_channels),
+	.channels = axp22x_adc_channels,
+	.adc_en1_mask = AXP22X_ADC_EN1_MASK,
+	.adc_rate = axp22x_adc_rate,
+	.adc_en2 = false,
+	.maps = axp22x_maps,
+};
+
+static const struct platform_device_id axp20x_adc_id_match[] = {
+	{ .name = "axp20x-adc", .driver_data = (kernel_ulong_t)&axp20x_data, },
+	{ .name = "axp22x-adc", .driver_data = (kernel_ulong_t)&axp22x_data, },
+	{ /* sentinel */ },
+};
+
+static int axp20x_probe(struct platform_device *pdev)
+{
+	struct axp20x_adc_iio *info;
+	struct iio_dev *indio_dev;
+	struct axp20x_dev *axp20x_dev;
+	int ret;
+
+	axp20x_dev = dev_get_drvdata(pdev->dev.parent);
+
+	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*info));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	info = iio_priv(indio_dev);
+	platform_set_drvdata(pdev, indio_dev);
+
+	info->regmap = axp20x_dev->regmap;
+	indio_dev->dev.parent = &pdev->dev;
+	indio_dev->dev.of_node = pdev->dev.of_node;
+	indio_dev->modes = INDIO_DIRECT_MODE;
+
+	info->data = (struct axp_data *)platform_get_device_id(pdev)->driver_data;
+
+	indio_dev->name = platform_get_device_id(pdev)->name;
+	indio_dev->info = info->data->iio_info;
+	indio_dev->num_channels = info->data->num_channels;
+	indio_dev->channels = info->data->channels;
+
+	/* Enable the ADCs on IP */
+	regmap_write(info->regmap, AXP20X_ADC_EN1, info->data->adc_en1_mask);
+
+	if (info->data->adc_en2)
+		/* Enable GPIO0/1 and internal temperature ADCs */
+		regmap_update_bits(info->regmap, AXP20X_ADC_EN2,
+				   AXP20X_ADC_EN2_MASK, AXP20X_ADC_EN2_MASK);
+
+	/* Configure ADCs rate */
+	regmap_update_bits(info->regmap, AXP20X_ADC_RATE, AXP20X_ADC_RATE_MASK,
+			   info->data->adc_rate(100));
+
+	ret = iio_map_array_register(indio_dev, info->data->maps);
+	if (ret < 0) {
+		dev_err(&pdev->dev, "failed to register IIO maps: %d\n", ret);
+		goto fail_map;
+	}
+
+	ret = iio_device_register(indio_dev);
+	if (ret < 0) {
+		dev_err(&pdev->dev, "could not register the device\n");
+		goto fail_register;
+	}
+
+	return 0;
+
+fail_register:
+	iio_map_array_unregister(indio_dev);
+
+fail_map:
+	regmap_write(info->regmap, AXP20X_ADC_EN1, 0);
+
+	if (info->data->adc_en2)
+		regmap_write(info->regmap, AXP20X_ADC_EN2, 0);
+
+	return ret;
+}
+
+static int axp20x_remove(struct platform_device *pdev)
+{
+	struct axp20x_adc_iio *info;
+	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
+
+	info = iio_priv(indio_dev);
+
+	iio_device_unregister(indio_dev);
+	iio_map_array_unregister(indio_dev);
+
+	regmap_write(info->regmap, AXP20X_ADC_EN1, 0);
+
+	if (info->data->adc_en2)
+		regmap_write(info->regmap, AXP20X_ADC_EN2, 0);
+
+	return 0;
+}
+
+static struct platform_driver axp20x_adc_driver = {
+	.driver = {
+		.name = "axp20x-adc",
+	},
+	.id_table = axp20x_adc_id_match,
+	.probe = axp20x_probe,
+	.remove = axp20x_remove,
+};
+
+module_platform_driver(axp20x_adc_driver);
+
+MODULE_DESCRIPTION("ADC driver for AXP20X and AXP22X PMICs");
+MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
+MODULE_LICENSE("GPL");
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 04/18] iio: adc: add support for X-Powers AXP20X and AXP22X PMICs ADCs
@ 2017-02-14  9:40   ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:40 UTC (permalink / raw)
  To: linux-arm-kernel

The X-Powers AXP20X and AXP22X PMICs have multiple ADCs. They expose the
battery voltage, battery charge and discharge currents, AC-in and VBUS
voltages and currents, 2 GPIOs muxable in ADC mode and PMIC temperature.

This adds support for most of AXP20X and AXP22X ADCs.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
---

v3:
 - moved from switch to if condition in axp20x_adc_raw and
 axp22x_adc_raw,
 - removed DT support as DT node has been dropped,
   - use of platform_device_id
 - correctly defined the name of the iio device (name used to probe the
 driver),
 - added goto for errors in probe,
 - added iio_map_array_unregister to the remove function,

v2:
 - removed unused defines,
 - changed BIT(x) to 1 << x when describing bits purpose for which 2 <<
 x or 3 << x exists, to be consistent,
 - changed ADC rate defines to macro formulas,
 - reordered IIO channels, now different measures (current/voltage) of
 the same part of the PMIC (e.g. battery), have the same IIO channel in
 their respective IIO type. When a part of the PMIC have only one
 measure, a number is jumped,
 - left IIO channel mapping in DT to use iio_map structure,
 - removed indexing of ADC internal temperature,
 - removed unused iio_dev structure in axp20x_adc_iio,
 - added a structure for data specific to AXP20X or AXP22X PMICs instead
 of using an ID and an if condition when needing to separate the
 behaviour of both,
 - added a comment on batt_chrg_i really being on 12bits rather than
 what the Chinese datasheets say (13 bits),
 - corrected the offset for AXP22X PMIC temperature,
 - set the ADC rate to a value (100Hz) shared by the AXP20X and AXP22X,
 - created macro formulas to compute the ADC rate for each,
 - added a condition on presence of ADC_EN2 reg before setting/resetting
 it,
 - switched from devm_iio_device_unregister to the non-devm function
 because of the need for a remove function,
 - removed some dead code,

 drivers/iio/adc/Kconfig      |  10 +
 drivers/iio/adc/Makefile     |   1 +
 drivers/iio/adc/axp20x_adc.c | 606 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 617 insertions(+)
 create mode 100644 drivers/iio/adc/axp20x_adc.c

diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
index 9c8b558..ed17fe1 100644
--- a/drivers/iio/adc/Kconfig
+++ b/drivers/iio/adc/Kconfig
@@ -154,6 +154,16 @@ config AT91_SAMA5D2_ADC
 	  To compile this driver as a module, choose M here: the module will be
 	  called at91-sama5d2_adc.
 
+config AXP20X_ADC
+	tristate "X-Powers AXP20X and AXP22X ADC driver"
+	depends on MFD_AXP20X
+	help
+	  Say yes here to have support for X-Powers power management IC (PMIC)
+	  AXP20X and AXP22X ADC devices.
+
+	  To compile this driver as a module, choose M here: the module will be
+	  called axp20x_adc.
+
 config AXP288_ADC
 	tristate "X-Powers AXP288 ADC driver"
 	depends on MFD_AXP20X
diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile
index d36c4be..f5c28a5 100644
--- a/drivers/iio/adc/Makefile
+++ b/drivers/iio/adc/Makefile
@@ -16,6 +16,7 @@ obj-$(CONFIG_AD7887) += ad7887.o
 obj-$(CONFIG_AD799X) += ad799x.o
 obj-$(CONFIG_AT91_ADC) += at91_adc.o
 obj-$(CONFIG_AT91_SAMA5D2_ADC) += at91-sama5d2_adc.o
+obj-$(CONFIG_AXP20X_ADC) += axp20x_adc.o
 obj-$(CONFIG_AXP288_ADC) += axp288_adc.o
 obj-$(CONFIG_BCM_IPROC_ADC) += bcm_iproc_adc.o
 obj-$(CONFIG_BERLIN2_ADC) += berlin2-adc.o
diff --git a/drivers/iio/adc/axp20x_adc.c b/drivers/iio/adc/axp20x_adc.c
new file mode 100644
index 0000000..5ef6af8
--- /dev/null
+++ b/drivers/iio/adc/axp20x_adc.c
@@ -0,0 +1,606 @@
+/* ADC driver for AXP20X and AXP22X PMICs
+ *
+ * Copyright (c) 2016 Free Electrons NextThing Co.
+ *	Quentin Schulz <quentin.schulz@free-electrons.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License version 2 as published by the
+ * Free Software Foundation.
+ */
+
+#include <linux/completion.h>
+#include <linux/interrupt.h>
+#include <linux/io.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_device.h>
+#include <linux/platform_device.h>
+#include <linux/pm_runtime.h>
+#include <linux/regmap.h>
+#include <linux/thermal.h>
+
+#include <linux/iio/iio.h>
+#include <linux/iio/driver.h>
+#include <linux/iio/machine.h>
+#include <linux/mfd/axp20x.h>
+
+#define AXP20X_ADC_EN1_MASK			GENMASK(7, 0)
+
+#define AXP20X_ADC_EN2_MASK			(GENMASK(3, 2) | BIT(7))
+#define AXP22X_ADC_EN1_MASK			(GENMASK(7, 5) | BIT(0))
+
+#define AXP20X_GPIO10_IN_RANGE_GPIO0		BIT(0)
+#define AXP20X_GPIO10_IN_RANGE_GPIO1		BIT(1)
+#define AXP20X_GPIO10_IN_RANGE_GPIO0_VAL(x)	((x) & BIT(0))
+#define AXP20X_GPIO10_IN_RANGE_GPIO1_VAL(x)	(((x) & BIT(0)) << 1)
+
+#define AXP20X_ADC_RATE_MASK			GENMASK(7, 6)
+#define AXP20X_ADC_RATE_HZ(x)			((ilog2((x) / 25) << 6) & AXP20X_ADC_RATE_MASK)
+#define AXP22X_ADC_RATE_HZ(x)			((ilog2((x) / 100) << 6) & AXP20X_ADC_RATE_MASK)
+
+#define AXP20X_ADC_CHANNEL(_channel, _name, _type, _reg)	\
+	{							\
+		.type = _type,					\
+		.indexed = 1,					\
+		.channel = _channel,				\
+		.address = _reg,				\
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |	\
+				      BIT(IIO_CHAN_INFO_SCALE),	\
+		.datasheet_name = _name,			\
+	}
+
+#define AXP20X_ADC_CHANNEL_OFFSET(_channel, _name, _type, _reg) \
+	{							\
+		.type = _type,					\
+		.indexed = 1,					\
+		.channel = _channel,				\
+		.address = _reg,				\
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |	\
+				      BIT(IIO_CHAN_INFO_SCALE) |\
+				      BIT(IIO_CHAN_INFO_OFFSET),\
+		.datasheet_name = _name,			\
+	}
+
+struct axp_data;
+
+struct axp20x_adc_iio {
+	struct regmap		*regmap;
+	struct axp_data		*data;
+};
+
+enum axp20x_adc_channel_v {
+	AXP20X_ACIN_V = 0,
+	AXP20X_VBUS_V,
+	AXP20X_TS_IN,
+	AXP20X_GPIO0_V,
+	AXP20X_GPIO1_V,
+	AXP20X_IPSOUT_V,
+	AXP20X_BATT_V,
+};
+
+enum axp20x_adc_channel_i {
+	AXP20X_ACIN_I = 0,
+	AXP20X_VBUS_I,
+	AXP20X_BATT_CHRG_I,
+	AXP20X_BATT_DISCHRG_I,
+};
+
+enum axp22x_adc_channel_v {
+	AXP22X_TS_IN = 0,
+	AXP22X_BATT_V,
+};
+
+enum axp22x_adc_channel_i {
+	AXP22X_BATT_CHRG_I = 1,
+	AXP22X_BATT_DISCHRG_I,
+};
+
+static struct iio_map axp20x_maps[] = {
+	{
+		.consumer_dev_name = "axp20x-usb-power-supply",
+		.consumer_channel = "vbus_v",
+		.adc_channel_label = "vbus_v",
+	}, {
+		.consumer_dev_name = "axp20x-usb-power-supply",
+		.consumer_channel = "vbus_i",
+		.adc_channel_label = "vbus_i",
+	}, {
+		.consumer_dev_name = "axp20x-ac-power-supply",
+		.consumer_channel = "acin_v",
+		.adc_channel_label = "acin_v",
+	}, {
+		.consumer_dev_name = "axp20x-ac-power-supply",
+		.consumer_channel = "acin_i",
+		.adc_channel_label = "acin_i",
+	}, {
+		.consumer_dev_name = "axp20x-battery-power-supply",
+		.consumer_channel = "batt_v",
+		.adc_channel_label = "batt_v",
+	}, {
+		.consumer_dev_name = "axp20x-battery-power-supply",
+		.consumer_channel = "batt_chrg_i",
+		.adc_channel_label = "batt_chrg_i",
+	}, {
+		.consumer_dev_name = "axp20x-battery-power-supply",
+		.consumer_channel = "batt_dischrg_i",
+		.adc_channel_label = "batt_dischrg_i",
+	}, { /* sentinel */ }
+};
+
+static struct iio_map axp22x_maps[] = {
+	{
+		.consumer_dev_name = "axp20x-battery-power-supply",
+		.consumer_channel = "batt_v",
+		.adc_channel_label = "batt_v",
+	}, {
+		.consumer_dev_name = "axp20x-battery-power-supply",
+		.consumer_channel = "batt_chrg_i",
+		.adc_channel_label = "batt_chrg_i",
+	}, {
+		.consumer_dev_name = "axp20x-battery-power-supply",
+		.consumer_channel = "batt_dischrg_i",
+		.adc_channel_label = "batt_dischrg_i",
+	}, { /* sentinel */ }
+};
+
+/*
+ * Channels are mapped by physical system. Their channels share the same index.
+ * i.e. acin_i is in_current0_raw and acin_v is in_voltage0_raw.
+ * The only exception is for the battery. batt_v will be in_voltage6_raw and
+ * charge current in_current6_raw and discharge current will be in_current7_raw.
+ */
+static const struct iio_chan_spec axp20x_adc_channels[] = {
+	AXP20X_ADC_CHANNEL(AXP20X_ACIN_V, "acin_v", IIO_VOLTAGE,
+			   AXP20X_ACIN_V_ADC_H),
+	AXP20X_ADC_CHANNEL(AXP20X_ACIN_I, "acin_i", IIO_CURRENT,
+			   AXP20X_ACIN_I_ADC_H),
+	AXP20X_ADC_CHANNEL(AXP20X_VBUS_V, "vbus_v", IIO_VOLTAGE,
+			   AXP20X_VBUS_V_ADC_H),
+	AXP20X_ADC_CHANNEL(AXP20X_VBUS_I, "vbus_i", IIO_CURRENT,
+			   AXP20X_VBUS_I_ADC_H),
+	{
+		.type = IIO_TEMP,
+		.address = AXP20X_TEMP_ADC_H,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+				      BIT(IIO_CHAN_INFO_SCALE) |
+				      BIT(IIO_CHAN_INFO_OFFSET),
+		.datasheet_name = "pmic_temp",
+	},
+	AXP20X_ADC_CHANNEL_OFFSET(AXP20X_GPIO0_V, "gpio0_v", IIO_VOLTAGE,
+				  AXP20X_GPIO0_V_ADC_H),
+	AXP20X_ADC_CHANNEL_OFFSET(AXP20X_GPIO1_V, "gpio1_v", IIO_VOLTAGE,
+				  AXP20X_GPIO1_V_ADC_H),
+	AXP20X_ADC_CHANNEL(AXP20X_IPSOUT_V, "ipsout_v", IIO_VOLTAGE,
+			   AXP20X_IPSOUT_V_HIGH_H),
+	AXP20X_ADC_CHANNEL(AXP20X_BATT_V, "batt_v", IIO_VOLTAGE,
+			   AXP20X_BATT_V_H),
+	AXP20X_ADC_CHANNEL(AXP20X_BATT_CHRG_I, "batt_chrg_i", IIO_CURRENT,
+			   AXP20X_BATT_CHRG_I_H),
+	AXP20X_ADC_CHANNEL(AXP20X_BATT_DISCHRG_I, "batt_dischrg_i", IIO_CURRENT,
+			   AXP20X_BATT_DISCHRG_I_H),
+};
+
+static const struct iio_chan_spec axp22x_adc_channels[] = {
+	{
+		.type = IIO_TEMP,
+		.address = AXP22X_PMIC_TEMP_H,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+				      BIT(IIO_CHAN_INFO_SCALE) |
+				      BIT(IIO_CHAN_INFO_OFFSET),
+		.datasheet_name = "pmic_temp",
+	},
+	AXP20X_ADC_CHANNEL(AXP22X_BATT_V, "batt_v", IIO_VOLTAGE,
+			   AXP20X_BATT_V_H),
+	AXP20X_ADC_CHANNEL(AXP22X_BATT_CHRG_I, "batt_chrg_i", IIO_CURRENT,
+			   AXP20X_BATT_CHRG_I_H),
+	AXP20X_ADC_CHANNEL(AXP22X_BATT_DISCHRG_I, "batt_dischrg_i", IIO_CURRENT,
+			   AXP20X_BATT_DISCHRG_I_H),
+};
+
+static int axp20x_adc_raw(struct iio_dev *indio_dev,
+			  struct iio_chan_spec const *chan, int *val)
+{
+	struct axp20x_adc_iio *info = iio_priv(indio_dev);
+	int size = 12;
+
+	/*
+	 *N.B.:  Unlike the Chinese datasheets tell, the charging current is
+	 * stored on 12 bits, not 13 bits. Only discharging current is on 13
+	 * bits.
+	 */
+	if (chan->type == IIO_CURRENT && chan->channel == AXP20X_BATT_DISCHRG_I)
+		size = 13;
+	else
+		size = 12;
+
+	*val = axp20x_read_variable_width(info->regmap, chan->address, size);
+	if (*val < 0)
+		return *val;
+
+	return IIO_VAL_INT;
+}
+
+static int axp22x_adc_raw(struct iio_dev *indio_dev,
+			  struct iio_chan_spec const *chan, int *val)
+{
+	struct axp20x_adc_iio *info = iio_priv(indio_dev);
+	int size;
+
+	/*
+	 * N.B.: Unlike the Chinese datasheets tell, the charging current is
+	 * stored on 12 bits, not 13 bits. Only discharging current is on 13
+	 * bits.
+	 */
+	if (chan->type == IIO_CURRENT && chan->channel == AXP22X_BATT_DISCHRG_I)
+		size = 13;
+	else
+		size = 12;
+
+	*val = axp20x_read_variable_width(info->regmap, chan->address, size);
+	if (*val < 0)
+		return *val;
+
+	return IIO_VAL_INT;
+}
+
+static int axp20x_adc_scale_voltage(int channel, int *val, int *val2)
+{
+	switch (channel) {
+	case AXP20X_ACIN_V:
+	case AXP20X_VBUS_V:
+		*val = 1;
+		*val2 = 700000;
+		return IIO_VAL_INT_PLUS_MICRO;
+
+	case AXP20X_GPIO0_V:
+	case AXP20X_GPIO1_V:
+		*val = 0;
+		*val2 = 500000;
+		return IIO_VAL_INT_PLUS_MICRO;
+
+	case AXP20X_BATT_V:
+		*val = 1;
+		*val2 = 100000;
+		return IIO_VAL_INT_PLUS_MICRO;
+
+	case AXP20X_IPSOUT_V:
+		*val = 1;
+		*val2 = 400000;
+		return IIO_VAL_INT_PLUS_MICRO;
+
+	default:
+		return -EINVAL;
+	}
+}
+
+static int axp20x_adc_scale_current(int channel, int *val, int *val2)
+{
+	switch (channel) {
+	case AXP20X_ACIN_I:
+		*val = 0;
+		*val2 = 625000;
+		return IIO_VAL_INT_PLUS_MICRO;
+
+	case AXP20X_VBUS_I:
+		*val = 0;
+		*val2 = 375000;
+		return IIO_VAL_INT_PLUS_MICRO;
+
+	case AXP20X_BATT_DISCHRG_I:
+	case AXP20X_BATT_CHRG_I:
+		*val = 0;
+		*val2 = 500000;
+		return IIO_VAL_INT_PLUS_MICRO;
+
+	default:
+		return -EINVAL;
+	}
+}
+
+static int axp20x_adc_scale(struct iio_chan_spec const *chan, int *val,
+			    int *val2)
+{
+	switch (chan->type) {
+	case IIO_VOLTAGE:
+		return axp20x_adc_scale_voltage(chan->channel, val, val2);
+
+	case IIO_CURRENT:
+		return axp20x_adc_scale_current(chan->channel, val, val2);
+
+	case IIO_TEMP:
+		*val = 100;
+		return IIO_VAL_INT;
+
+	default:
+		return -EINVAL;
+	}
+}
+
+static int axp22x_adc_scale(struct iio_chan_spec const *chan, int *val,
+			    int *val2)
+{
+	switch (chan->type) {
+	case IIO_VOLTAGE:
+		if (chan->channel != AXP22X_BATT_V)
+			return -EINVAL;
+
+		*val = 1;
+		*val2 = 100000;
+		return IIO_VAL_INT_PLUS_MICRO;
+
+	case IIO_CURRENT:
+		*val = 0;
+		*val2 = 500000;
+		return IIO_VAL_INT_PLUS_MICRO;
+
+	case IIO_TEMP:
+		*val = 100;
+		return IIO_VAL_INT;
+
+	default:
+		return -EINVAL;
+	}
+}
+
+static int axp20x_adc_offset_voltage(struct iio_dev *indio_dev, int channel,
+				     int *val)
+{
+	struct axp20x_adc_iio *info = iio_priv(indio_dev);
+	int ret, reg;
+
+	if (channel != AXP20X_GPIO0_V && channel != AXP20X_GPIO1_V)
+		return -EINVAL;
+
+	ret = regmap_read(info->regmap, AXP20X_GPIO10_IN_RANGE, &reg);
+	if (ret < 0)
+		return ret;
+
+	if (channel == AXP20X_GPIO0_V)
+		*val = reg & AXP20X_GPIO10_IN_RANGE_GPIO0;
+	else
+		*val = reg & AXP20X_GPIO10_IN_RANGE_GPIO1;
+
+	*val = !!(*val) * 700000;
+
+	return IIO_VAL_INT;
+}
+
+static int axp20x_adc_offset(struct iio_dev *indio_dev,
+			     struct iio_chan_spec const *chan, int *val)
+{
+	switch (chan->type) {
+	case IIO_VOLTAGE:
+		return axp20x_adc_offset_voltage(indio_dev, chan->channel, val);
+
+	case IIO_TEMP:
+		*val = -1447;
+		return IIO_VAL_INT;
+
+	default:
+		return -EINVAL;
+	}
+}
+
+static int axp20x_read_raw(struct iio_dev *indio_dev,
+			   struct iio_chan_spec const *chan, int *val,
+			   int *val2, long mask)
+{
+	switch (mask) {
+	case IIO_CHAN_INFO_OFFSET:
+		return axp20x_adc_offset(indio_dev, chan, val);
+
+	case IIO_CHAN_INFO_SCALE:
+		return axp20x_adc_scale(chan, val, val2);
+
+	case IIO_CHAN_INFO_RAW:
+		return axp20x_adc_raw(indio_dev, chan, val);
+
+	default:
+		return -EINVAL;
+	}
+}
+
+static int axp22x_read_raw(struct iio_dev *indio_dev,
+			   struct iio_chan_spec const *chan, int *val,
+			   int *val2, long mask)
+{
+	switch (mask) {
+	case IIO_CHAN_INFO_OFFSET:
+		*val = -2677;
+		return IIO_VAL_INT;
+
+	case IIO_CHAN_INFO_SCALE:
+		return axp22x_adc_scale(chan, val, val2);
+
+	case IIO_CHAN_INFO_RAW:
+		return axp22x_adc_raw(indio_dev, chan, val);
+
+	default:
+		return -EINVAL;
+	}
+}
+
+static int axp20x_write_raw(struct iio_dev *indio_dev,
+			    struct iio_chan_spec const *chan, int val, int val2,
+			    long mask)
+{
+	struct axp20x_adc_iio *info = iio_priv(indio_dev);
+
+	/*
+	 * The AXP20X PMIC allows the user to choose between 0V and 0.7V offsets
+	 * for (independently) GPIO0 and GPIO1 when in ADC mode.
+	 */
+	if (mask != IIO_CHAN_INFO_OFFSET)
+		return -EINVAL;
+
+	if (chan->channel != AXP20X_GPIO0_V && chan->channel != AXP20X_GPIO1_V)
+		return -EINVAL;
+
+	if (val != 0 && val != 700000)
+		return -EINVAL;
+
+	if (chan->channel == AXP20X_GPIO0_V)
+		return regmap_update_bits(info->regmap, AXP20X_GPIO10_IN_RANGE,
+					  AXP20X_GPIO10_IN_RANGE_GPIO0,
+					  AXP20X_GPIO10_IN_RANGE_GPIO0_VAL(!!val));
+
+	return regmap_update_bits(info->regmap, AXP20X_GPIO10_IN_RANGE,
+				  AXP20X_GPIO10_IN_RANGE_GPIO1,
+				  AXP20X_GPIO10_IN_RANGE_GPIO1_VAL(!!val));
+}
+
+static const struct iio_info axp20x_adc_iio_info = {
+	.read_raw = axp20x_read_raw,
+	.write_raw = axp20x_write_raw,
+	.driver_module = THIS_MODULE,
+};
+
+static const struct iio_info axp22x_adc_iio_info = {
+	.read_raw = axp22x_read_raw,
+	.driver_module = THIS_MODULE,
+};
+
+static int axp20x_adc_rate(int rate)
+{
+	return AXP20X_ADC_RATE_HZ(rate);
+}
+
+static int axp22x_adc_rate(int rate)
+{
+	return AXP22X_ADC_RATE_HZ(rate);
+}
+
+struct axp_data {
+	const struct iio_info		*iio_info;
+	int				num_channels;
+	struct iio_chan_spec const	*channels;
+	unsigned long			adc_en1_mask;
+	int				(*adc_rate)(int rate);
+	bool				adc_en2;
+	struct iio_map			*maps;
+};
+
+static const struct axp_data axp20x_data = {
+	.iio_info = &axp20x_adc_iio_info,
+	.num_channels = ARRAY_SIZE(axp20x_adc_channels),
+	.channels = axp20x_adc_channels,
+	.adc_en1_mask = AXP20X_ADC_EN1_MASK,
+	.adc_rate = axp20x_adc_rate,
+	.adc_en2 = true,
+	.maps = axp20x_maps,
+};
+
+static const struct axp_data axp22x_data = {
+	.iio_info = &axp22x_adc_iio_info,
+	.num_channels = ARRAY_SIZE(axp22x_adc_channels),
+	.channels = axp22x_adc_channels,
+	.adc_en1_mask = AXP22X_ADC_EN1_MASK,
+	.adc_rate = axp22x_adc_rate,
+	.adc_en2 = false,
+	.maps = axp22x_maps,
+};
+
+static const struct platform_device_id axp20x_adc_id_match[] = {
+	{ .name = "axp20x-adc", .driver_data = (kernel_ulong_t)&axp20x_data, },
+	{ .name = "axp22x-adc", .driver_data = (kernel_ulong_t)&axp22x_data, },
+	{ /* sentinel */ },
+};
+
+static int axp20x_probe(struct platform_device *pdev)
+{
+	struct axp20x_adc_iio *info;
+	struct iio_dev *indio_dev;
+	struct axp20x_dev *axp20x_dev;
+	int ret;
+
+	axp20x_dev = dev_get_drvdata(pdev->dev.parent);
+
+	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*info));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	info = iio_priv(indio_dev);
+	platform_set_drvdata(pdev, indio_dev);
+
+	info->regmap = axp20x_dev->regmap;
+	indio_dev->dev.parent = &pdev->dev;
+	indio_dev->dev.of_node = pdev->dev.of_node;
+	indio_dev->modes = INDIO_DIRECT_MODE;
+
+	info->data = (struct axp_data *)platform_get_device_id(pdev)->driver_data;
+
+	indio_dev->name = platform_get_device_id(pdev)->name;
+	indio_dev->info = info->data->iio_info;
+	indio_dev->num_channels = info->data->num_channels;
+	indio_dev->channels = info->data->channels;
+
+	/* Enable the ADCs on IP */
+	regmap_write(info->regmap, AXP20X_ADC_EN1, info->data->adc_en1_mask);
+
+	if (info->data->adc_en2)
+		/* Enable GPIO0/1 and internal temperature ADCs */
+		regmap_update_bits(info->regmap, AXP20X_ADC_EN2,
+				   AXP20X_ADC_EN2_MASK, AXP20X_ADC_EN2_MASK);
+
+	/* Configure ADCs rate */
+	regmap_update_bits(info->regmap, AXP20X_ADC_RATE, AXP20X_ADC_RATE_MASK,
+			   info->data->adc_rate(100));
+
+	ret = iio_map_array_register(indio_dev, info->data->maps);
+	if (ret < 0) {
+		dev_err(&pdev->dev, "failed to register IIO maps: %d\n", ret);
+		goto fail_map;
+	}
+
+	ret = iio_device_register(indio_dev);
+	if (ret < 0) {
+		dev_err(&pdev->dev, "could not register the device\n");
+		goto fail_register;
+	}
+
+	return 0;
+
+fail_register:
+	iio_map_array_unregister(indio_dev);
+
+fail_map:
+	regmap_write(info->regmap, AXP20X_ADC_EN1, 0);
+
+	if (info->data->adc_en2)
+		regmap_write(info->regmap, AXP20X_ADC_EN2, 0);
+
+	return ret;
+}
+
+static int axp20x_remove(struct platform_device *pdev)
+{
+	struct axp20x_adc_iio *info;
+	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
+
+	info = iio_priv(indio_dev);
+
+	iio_device_unregister(indio_dev);
+	iio_map_array_unregister(indio_dev);
+
+	regmap_write(info->regmap, AXP20X_ADC_EN1, 0);
+
+	if (info->data->adc_en2)
+		regmap_write(info->regmap, AXP20X_ADC_EN2, 0);
+
+	return 0;
+}
+
+static struct platform_driver axp20x_adc_driver = {
+	.driver = {
+		.name = "axp20x-adc",
+	},
+	.id_table = axp20x_adc_id_match,
+	.probe = axp20x_probe,
+	.remove = axp20x_remove,
+};
+
+module_platform_driver(axp20x_adc_driver);
+
+MODULE_DESCRIPTION("ADC driver for AXP20X and AXP22X PMICs");
+MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
+MODULE_LICENSE("GPL");
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 05/18] mfd: axp20x: add ADC cells for AXP20X and AXP22X PMICs
  2017-02-14  9:40 ` Quentin Schulz
  (?)
@ 2017-02-14  9:40   ` Quentin Schulz
  -1 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:40 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: Quentin Schulz, linux-iio, devicetree, linux-kernel, linux-pm,
	linux-arm-kernel, thomas.petazzoni, icenowy, bonbons

This adds the AXP20X/AXP22x ADCs driver to the mfd cells of the AXP209,
AXP221 and AXP223 MFD.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
---

v3:
 - removed of_compatible as DT node has been removed,
 - use different names to probe the ADC driver,

 drivers/mfd/axp20x.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/drivers/mfd/axp20x.c b/drivers/mfd/axp20x.c
index 9c2fd37..a44a2fe 100644
--- a/drivers/mfd/axp20x.c
+++ b/drivers/mfd/axp20x.c
@@ -581,6 +581,8 @@ static struct mfd_cell axp20x_cells[] = {
 	}, {
 		.name		= "axp20x-regulator",
 	}, {
+		.name		= "axp20x-adc",
+	}, {
 		.name		= "axp20x-ac-power-supply",
 		.of_compatible	= "x-powers,axp202-ac-power-supply",
 		.num_resources	= ARRAY_SIZE(axp20x_ac_power_supply_resources),
@@ -601,6 +603,8 @@ static struct mfd_cell axp221_cells[] = {
 	}, {
 		.name		= "axp20x-regulator",
 	}, {
+		.name		= "axp22x-adc"
+	}, {
 		.name		= "axp20x-usb-power-supply",
 		.of_compatible	= "x-powers,axp221-usb-power-supply",
 		.num_resources	= ARRAY_SIZE(axp22x_usb_power_supply_resources),
@@ -614,6 +618,8 @@ static struct mfd_cell axp223_cells[] = {
 		.num_resources		= ARRAY_SIZE(axp22x_pek_resources),
 		.resources		= axp22x_pek_resources,
 	}, {
+		.name		= "axp22x-adc",
+	}, {
 		.name			= "axp20x-regulator",
 	}, {
 		.name		= "axp20x-usb-power-supply",
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 05/18] mfd: axp20x: add ADC cells for AXP20X and AXP22X PMICs
@ 2017-02-14  9:40   ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:40 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: thomas.petazzoni, devicetree, linux-pm, linux-iio, linux-kernel,
	Quentin Schulz, bonbons, icenowy, linux-arm-kernel

This adds the AXP20X/AXP22x ADCs driver to the mfd cells of the AXP209,
AXP221 and AXP223 MFD.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
---

v3:
 - removed of_compatible as DT node has been removed,
 - use different names to probe the ADC driver,

 drivers/mfd/axp20x.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/drivers/mfd/axp20x.c b/drivers/mfd/axp20x.c
index 9c2fd37..a44a2fe 100644
--- a/drivers/mfd/axp20x.c
+++ b/drivers/mfd/axp20x.c
@@ -581,6 +581,8 @@ static struct mfd_cell axp20x_cells[] = {
 	}, {
 		.name		= "axp20x-regulator",
 	}, {
+		.name		= "axp20x-adc",
+	}, {
 		.name		= "axp20x-ac-power-supply",
 		.of_compatible	= "x-powers,axp202-ac-power-supply",
 		.num_resources	= ARRAY_SIZE(axp20x_ac_power_supply_resources),
@@ -601,6 +603,8 @@ static struct mfd_cell axp221_cells[] = {
 	}, {
 		.name		= "axp20x-regulator",
 	}, {
+		.name		= "axp22x-adc"
+	}, {
 		.name		= "axp20x-usb-power-supply",
 		.of_compatible	= "x-powers,axp221-usb-power-supply",
 		.num_resources	= ARRAY_SIZE(axp22x_usb_power_supply_resources),
@@ -614,6 +618,8 @@ static struct mfd_cell axp223_cells[] = {
 		.num_resources		= ARRAY_SIZE(axp22x_pek_resources),
 		.resources		= axp22x_pek_resources,
 	}, {
+		.name		= "axp22x-adc",
+	}, {
 		.name			= "axp20x-regulator",
 	}, {
 		.name		= "axp20x-usb-power-supply",
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 05/18] mfd: axp20x: add ADC cells for AXP20X and AXP22X PMICs
@ 2017-02-14  9:40   ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:40 UTC (permalink / raw)
  To: linux-arm-kernel

This adds the AXP20X/AXP22x ADCs driver to the mfd cells of the AXP209,
AXP221 and AXP223 MFD.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
---

v3:
 - removed of_compatible as DT node has been removed,
 - use different names to probe the ADC driver,

 drivers/mfd/axp20x.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/drivers/mfd/axp20x.c b/drivers/mfd/axp20x.c
index 9c2fd37..a44a2fe 100644
--- a/drivers/mfd/axp20x.c
+++ b/drivers/mfd/axp20x.c
@@ -581,6 +581,8 @@ static struct mfd_cell axp20x_cells[] = {
 	}, {
 		.name		= "axp20x-regulator",
 	}, {
+		.name		= "axp20x-adc",
+	}, {
 		.name		= "axp20x-ac-power-supply",
 		.of_compatible	= "x-powers,axp202-ac-power-supply",
 		.num_resources	= ARRAY_SIZE(axp20x_ac_power_supply_resources),
@@ -601,6 +603,8 @@ static struct mfd_cell axp221_cells[] = {
 	}, {
 		.name		= "axp20x-regulator",
 	}, {
+		.name		= "axp22x-adc"
+	}, {
 		.name		= "axp20x-usb-power-supply",
 		.of_compatible	= "x-powers,axp221-usb-power-supply",
 		.num_resources	= ARRAY_SIZE(axp22x_usb_power_supply_resources),
@@ -614,6 +618,8 @@ static struct mfd_cell axp223_cells[] = {
 		.num_resources		= ARRAY_SIZE(axp22x_pek_resources),
 		.resources		= axp22x_pek_resources,
 	}, {
+		.name		= "axp22x-adc",
+	}, {
 		.name			= "axp20x-regulator",
 	}, {
 		.name		= "axp20x-usb-power-supply",
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 06/18] mfd: axp20x: add AC power supply cells for AXP22X PMICs
  2017-02-14  9:40 ` Quentin Schulz
  (?)
@ 2017-02-14  9:41   ` Quentin Schulz
  -1 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: Quentin Schulz, linux-iio, devicetree, linux-kernel, linux-pm,
	linux-arm-kernel, thomas.petazzoni, icenowy, bonbons

The X-Powers AXP20X and AXP22X PMICs expose the status of AC power
supply.

This adds the AC power supply driver to the MFD cells of the AXP22X
PMICs.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
Acked-By: Sebastian Reichel <sre@kernel.org>
Acked-by: Chen-Yu Tsai <wens@csie.org>
Acked-for-MFD-by: Lee Jones <lee.jones@linaro.org>
---
 drivers/mfd/axp20x.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/drivers/mfd/axp20x.c b/drivers/mfd/axp20x.c
index a44a2fe..98abe4b 100644
--- a/drivers/mfd/axp20x.c
+++ b/drivers/mfd/axp20x.c
@@ -605,6 +605,11 @@ static struct mfd_cell axp221_cells[] = {
 	}, {
 		.name		= "axp22x-adc"
 	}, {
+		.name		= "axp20x-ac-power-supply",
+		.of_compatible	= "x-powers,axp221-ac-power-supply",
+		.num_resources	= ARRAY_SIZE(axp20x_ac_power_supply_resources),
+		.resources	= axp20x_ac_power_supply_resources,
+	}, {
 		.name		= "axp20x-usb-power-supply",
 		.of_compatible	= "x-powers,axp221-usb-power-supply",
 		.num_resources	= ARRAY_SIZE(axp22x_usb_power_supply_resources),
@@ -622,6 +627,11 @@ static struct mfd_cell axp223_cells[] = {
 	}, {
 		.name			= "axp20x-regulator",
 	}, {
+		.name		= "axp20x-ac-power-supply",
+		.of_compatible	= "x-powers,axp221-ac-power-supply",
+		.num_resources	= ARRAY_SIZE(axp20x_ac_power_supply_resources),
+		.resources	= axp20x_ac_power_supply_resources,
+	}, {
 		.name		= "axp20x-usb-power-supply",
 		.of_compatible	= "x-powers,axp223-usb-power-supply",
 		.num_resources	= ARRAY_SIZE(axp22x_usb_power_supply_resources),
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 06/18] mfd: axp20x: add AC power supply cells for AXP22X PMICs
@ 2017-02-14  9:41   ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: thomas.petazzoni, devicetree, linux-pm, linux-iio, linux-kernel,
	Quentin Schulz, bonbons, icenowy, linux-arm-kernel

The X-Powers AXP20X and AXP22X PMICs expose the status of AC power
supply.

This adds the AC power supply driver to the MFD cells of the AXP22X
PMICs.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
Acked-By: Sebastian Reichel <sre@kernel.org>
Acked-by: Chen-Yu Tsai <wens@csie.org>
Acked-for-MFD-by: Lee Jones <lee.jones@linaro.org>
---
 drivers/mfd/axp20x.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/drivers/mfd/axp20x.c b/drivers/mfd/axp20x.c
index a44a2fe..98abe4b 100644
--- a/drivers/mfd/axp20x.c
+++ b/drivers/mfd/axp20x.c
@@ -605,6 +605,11 @@ static struct mfd_cell axp221_cells[] = {
 	}, {
 		.name		= "axp22x-adc"
 	}, {
+		.name		= "axp20x-ac-power-supply",
+		.of_compatible	= "x-powers,axp221-ac-power-supply",
+		.num_resources	= ARRAY_SIZE(axp20x_ac_power_supply_resources),
+		.resources	= axp20x_ac_power_supply_resources,
+	}, {
 		.name		= "axp20x-usb-power-supply",
 		.of_compatible	= "x-powers,axp221-usb-power-supply",
 		.num_resources	= ARRAY_SIZE(axp22x_usb_power_supply_resources),
@@ -622,6 +627,11 @@ static struct mfd_cell axp223_cells[] = {
 	}, {
 		.name			= "axp20x-regulator",
 	}, {
+		.name		= "axp20x-ac-power-supply",
+		.of_compatible	= "x-powers,axp221-ac-power-supply",
+		.num_resources	= ARRAY_SIZE(axp20x_ac_power_supply_resources),
+		.resources	= axp20x_ac_power_supply_resources,
+	}, {
 		.name		= "axp20x-usb-power-supply",
 		.of_compatible	= "x-powers,axp223-usb-power-supply",
 		.num_resources	= ARRAY_SIZE(axp22x_usb_power_supply_resources),
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 06/18] mfd: axp20x: add AC power supply cells for AXP22X PMICs
@ 2017-02-14  9:41   ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: linux-arm-kernel

The X-Powers AXP20X and AXP22X PMICs expose the status of AC power
supply.

This adds the AC power supply driver to the MFD cells of the AXP22X
PMICs.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
Acked-By: Sebastian Reichel <sre@kernel.org>
Acked-by: Chen-Yu Tsai <wens@csie.org>
Acked-for-MFD-by: Lee Jones <lee.jones@linaro.org>
---
 drivers/mfd/axp20x.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/drivers/mfd/axp20x.c b/drivers/mfd/axp20x.c
index a44a2fe..98abe4b 100644
--- a/drivers/mfd/axp20x.c
+++ b/drivers/mfd/axp20x.c
@@ -605,6 +605,11 @@ static struct mfd_cell axp221_cells[] = {
 	}, {
 		.name		= "axp22x-adc"
 	}, {
+		.name		= "axp20x-ac-power-supply",
+		.of_compatible	= "x-powers,axp221-ac-power-supply",
+		.num_resources	= ARRAY_SIZE(axp20x_ac_power_supply_resources),
+		.resources	= axp20x_ac_power_supply_resources,
+	}, {
 		.name		= "axp20x-usb-power-supply",
 		.of_compatible	= "x-powers,axp221-usb-power-supply",
 		.num_resources	= ARRAY_SIZE(axp22x_usb_power_supply_resources),
@@ -622,6 +627,11 @@ static struct mfd_cell axp223_cells[] = {
 	}, {
 		.name			= "axp20x-regulator",
 	}, {
+		.name		= "axp20x-ac-power-supply",
+		.of_compatible	= "x-powers,axp221-ac-power-supply",
+		.num_resources	= ARRAY_SIZE(axp20x_ac_power_supply_resources),
+		.resources	= axp20x_ac_power_supply_resources,
+	}, {
 		.name		= "axp20x-usb-power-supply",
 		.of_compatible	= "x-powers,axp223-usb-power-supply",
 		.num_resources	= ARRAY_SIZE(axp22x_usb_power_supply_resources),
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 07/18] ARM: dtsi: axp209: add AC power supply subnode
  2017-02-14  9:40 ` Quentin Schulz
  (?)
@ 2017-02-14  9:41   ` Quentin Schulz
  -1 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: Quentin Schulz, linux-iio, devicetree, linux-kernel, linux-pm,
	linux-arm-kernel, thomas.petazzoni, icenowy, bonbons

The X-Powers AXP20X PMIC exposes the status of AC power supply, the
current current and voltage supplied to the board by the AC power
supply.

This adds the AC power supply subnode for AXP20X PMIC.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Chen-Yu Tsai <wens@csie.org>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
---

v2:
 - changed DT node name from ac_power_supply to ac-power-supply,
 - removed io-channels and io-channel-names from DT (the IIO mapping is
 done in the IIO ADC driver now),

 arch/arm/boot/dts/axp209.dtsi | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/arch/arm/boot/dts/axp209.dtsi b/arch/arm/boot/dts/axp209.dtsi
index 675bb0f..9677dd5 100644
--- a/arch/arm/boot/dts/axp209.dtsi
+++ b/arch/arm/boot/dts/axp209.dtsi
@@ -53,6 +53,11 @@
 	interrupt-controller;
 	#interrupt-cells = <1>;
 
+	ac_power_supply: ac-power-supply {
+		compatible = "x-powers,axp202-ac-power-supply";
+		status = "disabled";
+	};
+
 	axp_gpio: gpio {
 		compatible = "x-powers,axp209-gpio";
 		gpio-controller;
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 07/18] ARM: dtsi: axp209: add AC power supply subnode
@ 2017-02-14  9:41   ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: thomas.petazzoni, devicetree, linux-pm, linux-iio, linux-kernel,
	Quentin Schulz, bonbons, icenowy, linux-arm-kernel

The X-Powers AXP20X PMIC exposes the status of AC power supply, the
current current and voltage supplied to the board by the AC power
supply.

This adds the AC power supply subnode for AXP20X PMIC.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Chen-Yu Tsai <wens@csie.org>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
---

v2:
 - changed DT node name from ac_power_supply to ac-power-supply,
 - removed io-channels and io-channel-names from DT (the IIO mapping is
 done in the IIO ADC driver now),

 arch/arm/boot/dts/axp209.dtsi | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/arch/arm/boot/dts/axp209.dtsi b/arch/arm/boot/dts/axp209.dtsi
index 675bb0f..9677dd5 100644
--- a/arch/arm/boot/dts/axp209.dtsi
+++ b/arch/arm/boot/dts/axp209.dtsi
@@ -53,6 +53,11 @@
 	interrupt-controller;
 	#interrupt-cells = <1>;
 
+	ac_power_supply: ac-power-supply {
+		compatible = "x-powers,axp202-ac-power-supply";
+		status = "disabled";
+	};
+
 	axp_gpio: gpio {
 		compatible = "x-powers,axp209-gpio";
 		gpio-controller;
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 07/18] ARM: dtsi: axp209: add AC power supply subnode
@ 2017-02-14  9:41   ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: linux-arm-kernel

The X-Powers AXP20X PMIC exposes the status of AC power supply, the
current current and voltage supplied to the board by the AC power
supply.

This adds the AC power supply subnode for AXP20X PMIC.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Chen-Yu Tsai <wens@csie.org>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
---

v2:
 - changed DT node name from ac_power_supply to ac-power-supply,
 - removed io-channels and io-channel-names from DT (the IIO mapping is
 done in the IIO ADC driver now),

 arch/arm/boot/dts/axp209.dtsi | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/arch/arm/boot/dts/axp209.dtsi b/arch/arm/boot/dts/axp209.dtsi
index 675bb0f..9677dd5 100644
--- a/arch/arm/boot/dts/axp209.dtsi
+++ b/arch/arm/boot/dts/axp209.dtsi
@@ -53,6 +53,11 @@
 	interrupt-controller;
 	#interrupt-cells = <1>;
 
+	ac_power_supply: ac-power-supply {
+		compatible = "x-powers,axp202-ac-power-supply";
+		status = "disabled";
+	};
+
 	axp_gpio: gpio {
 		compatible = "x-powers,axp209-gpio";
 		gpio-controller;
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 08/18] ARM: dtsi: axp22x: add AC power supply subnode
  2017-02-14  9:40 ` Quentin Schulz
  (?)
@ 2017-02-14  9:41   ` Quentin Schulz
  -1 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: Quentin Schulz, linux-iio, devicetree, linux-kernel, linux-pm,
	linux-arm-kernel, thomas.petazzoni, icenowy, bonbons

The X-Powers AXP22X PMIC exposes the status of AC power supply.

This adds the AC power supply subnode for the AXP22X PMIC.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
Acked-by: Chen-Yu Tsai <wens@csie.org>
---

v2:
 - changed DT node name from ac_power_supply to ac-power-supply,
 - removed io-channels and io-channel-names from DT (the IIO mapping is
 done in the IIO ADC driver now),

 arch/arm/boot/dts/axp22x.dtsi | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/arch/arm/boot/dts/axp22x.dtsi b/arch/arm/boot/dts/axp22x.dtsi
index 458b668..67331c5 100644
--- a/arch/arm/boot/dts/axp22x.dtsi
+++ b/arch/arm/boot/dts/axp22x.dtsi
@@ -52,6 +52,11 @@
 	interrupt-controller;
 	#interrupt-cells = <1>;
 
+	ac_power_supply: ac-power-supply {
+		compatible = "x-powers,axp221-ac-power-supply";
+		status = "disabled";
+	};
+
 	regulators {
 		/* Default work frequency for buck regulators */
 		x-powers,dcdc-freq = <3000>;
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 08/18] ARM: dtsi: axp22x: add AC power supply subnode
@ 2017-02-14  9:41   ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: thomas.petazzoni, devicetree, linux-pm, linux-iio, linux-kernel,
	Quentin Schulz, bonbons, icenowy, linux-arm-kernel

The X-Powers AXP22X PMIC exposes the status of AC power supply.

This adds the AC power supply subnode for the AXP22X PMIC.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
Acked-by: Chen-Yu Tsai <wens@csie.org>
---

v2:
 - changed DT node name from ac_power_supply to ac-power-supply,
 - removed io-channels and io-channel-names from DT (the IIO mapping is
 done in the IIO ADC driver now),

 arch/arm/boot/dts/axp22x.dtsi | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/arch/arm/boot/dts/axp22x.dtsi b/arch/arm/boot/dts/axp22x.dtsi
index 458b668..67331c5 100644
--- a/arch/arm/boot/dts/axp22x.dtsi
+++ b/arch/arm/boot/dts/axp22x.dtsi
@@ -52,6 +52,11 @@
 	interrupt-controller;
 	#interrupt-cells = <1>;
 
+	ac_power_supply: ac-power-supply {
+		compatible = "x-powers,axp221-ac-power-supply";
+		status = "disabled";
+	};
+
 	regulators {
 		/* Default work frequency for buck regulators */
 		x-powers,dcdc-freq = <3000>;
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 08/18] ARM: dtsi: axp22x: add AC power supply subnode
@ 2017-02-14  9:41   ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: linux-arm-kernel

The X-Powers AXP22X PMIC exposes the status of AC power supply.

This adds the AC power supply subnode for the AXP22X PMIC.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
Acked-by: Chen-Yu Tsai <wens@csie.org>
---

v2:
 - changed DT node name from ac_power_supply to ac-power-supply,
 - removed io-channels and io-channel-names from DT (the IIO mapping is
 done in the IIO ADC driver now),

 arch/arm/boot/dts/axp22x.dtsi | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/arch/arm/boot/dts/axp22x.dtsi b/arch/arm/boot/dts/axp22x.dtsi
index 458b668..67331c5 100644
--- a/arch/arm/boot/dts/axp22x.dtsi
+++ b/arch/arm/boot/dts/axp22x.dtsi
@@ -52,6 +52,11 @@
 	interrupt-controller;
 	#interrupt-cells = <1>;
 
+	ac_power_supply: ac-power-supply {
+		compatible = "x-powers,axp221-ac-power-supply";
+		status = "disabled";
+	};
+
 	regulators {
 		/* Default work frequency for buck regulators */
 		x-powers,dcdc-freq = <3000>;
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 09/18] ARM: dts: sun8i: sina33: enable ACIN power supply subnode
  2017-02-14  9:40 ` Quentin Schulz
  (?)
@ 2017-02-14  9:41   ` Quentin Schulz
  -1 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: Quentin Schulz, linux-iio, devicetree, linux-kernel, linux-pm,
	linux-arm-kernel, thomas.petazzoni, icenowy, bonbons

The Sinlinx SinA33 has an AXP223 PMIC and an ACIN connector, thus, we
enable the ACIN power supply in its Device Tree.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Chen-Yu Tsai <wens@csie.org>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
---
 arch/arm/boot/dts/sun8i-a33-sinlinx-sina33.dts | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/arch/arm/boot/dts/sun8i-a33-sinlinx-sina33.dts b/arch/arm/boot/dts/sun8i-a33-sinlinx-sina33.dts
index 28c58c5..bf53408 100644
--- a/arch/arm/boot/dts/sun8i-a33-sinlinx-sina33.dts
+++ b/arch/arm/boot/dts/sun8i-a33-sinlinx-sina33.dts
@@ -147,6 +147,10 @@
 
 #include "axp223.dtsi"
 
+&ac_power_supply {
+	status = "okay";
+};
+
 &reg_aldo1 {
 	regulator-always-on;
 	regulator-min-microvolt = <3000000>;
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 09/18] ARM: dts: sun8i: sina33: enable ACIN power supply subnode
@ 2017-02-14  9:41   ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: thomas.petazzoni, devicetree, linux-pm, linux-iio, linux-kernel,
	Quentin Schulz, bonbons, icenowy, linux-arm-kernel

The Sinlinx SinA33 has an AXP223 PMIC and an ACIN connector, thus, we
enable the ACIN power supply in its Device Tree.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Chen-Yu Tsai <wens@csie.org>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
---
 arch/arm/boot/dts/sun8i-a33-sinlinx-sina33.dts | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/arch/arm/boot/dts/sun8i-a33-sinlinx-sina33.dts b/arch/arm/boot/dts/sun8i-a33-sinlinx-sina33.dts
index 28c58c5..bf53408 100644
--- a/arch/arm/boot/dts/sun8i-a33-sinlinx-sina33.dts
+++ b/arch/arm/boot/dts/sun8i-a33-sinlinx-sina33.dts
@@ -147,6 +147,10 @@
 
 #include "axp223.dtsi"
 
+&ac_power_supply {
+	status = "okay";
+};
+
 &reg_aldo1 {
 	regulator-always-on;
 	regulator-min-microvolt = <3000000>;
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 09/18] ARM: dts: sun8i: sina33: enable ACIN power supply subnode
@ 2017-02-14  9:41   ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: linux-arm-kernel

The Sinlinx SinA33 has an AXP223 PMIC and an ACIN connector, thus, we
enable the ACIN power supply in its Device Tree.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Chen-Yu Tsai <wens@csie.org>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
---
 arch/arm/boot/dts/sun8i-a33-sinlinx-sina33.dts | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/arch/arm/boot/dts/sun8i-a33-sinlinx-sina33.dts b/arch/arm/boot/dts/sun8i-a33-sinlinx-sina33.dts
index 28c58c5..bf53408 100644
--- a/arch/arm/boot/dts/sun8i-a33-sinlinx-sina33.dts
+++ b/arch/arm/boot/dts/sun8i-a33-sinlinx-sina33.dts
@@ -147,6 +147,10 @@
 
 #include "axp223.dtsi"
 
+&ac_power_supply {
+	status = "okay";
+};
+
 &reg_aldo1 {
 	regulator-always-on;
 	regulator-min-microvolt = <3000000>;
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 10/18] ARM: sun5i: chip: enable ACIN power supply subnode
  2017-02-14  9:40 ` Quentin Schulz
  (?)
@ 2017-02-14  9:41   ` Quentin Schulz
  -1 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: Quentin Schulz, linux-iio, devicetree, linux-kernel, linux-pm,
	linux-arm-kernel, thomas.petazzoni, icenowy, bonbons

The NextThing Co. CHIP has an AXP209 PMIC and can be power-supplied by
ACIN via the CHG-IN pin.

This enables the ACIN power supply subnode in the DT.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Chen-Yu Tsai <wens@csie.org>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
---
 arch/arm/boot/dts/sun5i-r8-chip.dts | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/arch/arm/boot/dts/sun5i-r8-chip.dts b/arch/arm/boot/dts/sun5i-r8-chip.dts
index c6da5ad..6011757 100644
--- a/arch/arm/boot/dts/sun5i-r8-chip.dts
+++ b/arch/arm/boot/dts/sun5i-r8-chip.dts
@@ -128,6 +128,10 @@
 
 #include "axp209.dtsi"
 
+&ac_power_supply {
+	status = "okay";
+};
+
 &i2c1 {
 	pinctrl-names = "default";
 	pinctrl-0 = <&i2c1_pins_a>;
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 10/18] ARM: sun5i: chip: enable ACIN power supply subnode
@ 2017-02-14  9:41   ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: thomas.petazzoni, devicetree, linux-pm, linux-iio, linux-kernel,
	Quentin Schulz, bonbons, icenowy, linux-arm-kernel

The NextThing Co. CHIP has an AXP209 PMIC and can be power-supplied by
ACIN via the CHG-IN pin.

This enables the ACIN power supply subnode in the DT.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Chen-Yu Tsai <wens@csie.org>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
---
 arch/arm/boot/dts/sun5i-r8-chip.dts | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/arch/arm/boot/dts/sun5i-r8-chip.dts b/arch/arm/boot/dts/sun5i-r8-chip.dts
index c6da5ad..6011757 100644
--- a/arch/arm/boot/dts/sun5i-r8-chip.dts
+++ b/arch/arm/boot/dts/sun5i-r8-chip.dts
@@ -128,6 +128,10 @@
 
 #include "axp209.dtsi"
 
+&ac_power_supply {
+	status = "okay";
+};
+
 &i2c1 {
 	pinctrl-names = "default";
 	pinctrl-0 = <&i2c1_pins_a>;
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 10/18] ARM: sun5i: chip: enable ACIN power supply subnode
@ 2017-02-14  9:41   ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: linux-arm-kernel

The NextThing Co. CHIP has an AXP209 PMIC and can be power-supplied by
ACIN via the CHG-IN pin.

This enables the ACIN power supply subnode in the DT.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Chen-Yu Tsai <wens@csie.org>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
---
 arch/arm/boot/dts/sun5i-r8-chip.dts | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/arch/arm/boot/dts/sun5i-r8-chip.dts b/arch/arm/boot/dts/sun5i-r8-chip.dts
index c6da5ad..6011757 100644
--- a/arch/arm/boot/dts/sun5i-r8-chip.dts
+++ b/arch/arm/boot/dts/sun5i-r8-chip.dts
@@ -128,6 +128,10 @@
 
 #include "axp209.dtsi"
 
+&ac_power_supply {
+	status = "okay";
+};
+
 &i2c1 {
 	pinctrl-names = "default";
 	pinctrl-0 = <&i2c1_pins_a>;
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 11/18] dt-bindings: power: supply: add AXP20X/AXP22X battery DT binding
  2017-02-14  9:40 ` Quentin Schulz
  (?)
@ 2017-02-14  9:41   ` Quentin Schulz
  -1 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: Quentin Schulz, linux-iio, devicetree, linux-kernel, linux-pm,
	linux-arm-kernel, thomas.petazzoni, icenowy, bonbons

The X-Powers AXP20X and AXP22X PMICs can have a battery as power supply.

This patch adds the DT binding documentation for the battery power
supply which gets various data from the PMIC, such as the battery status
(charging, discharging, full, dead), current max limit, current current,
battery capacity (in percentage), voltage max and min limits, current
voltage and battery capacity (in Ah).

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
---

v3:
 - removed constant charge current property, now should use the WIP
 battery framework,

v2:
 - changed DT node name from ac_power_supply to ac-power-supply,
 - removed io-channels and io-channel-names from DT (the IIO mapping is
 done in the IIO ADC driver now),
 - added x-powers,constant-charge-current property to set the maximal
 default constant current charge of the battery,

 .../bindings/power/supply/axp20x_battery.txt         | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/power/supply/axp20x_battery.txt

diff --git a/Documentation/devicetree/bindings/power/supply/axp20x_battery.txt b/Documentation/devicetree/bindings/power/supply/axp20x_battery.txt
new file mode 100644
index 0000000..c248866
--- /dev/null
+++ b/Documentation/devicetree/bindings/power/supply/axp20x_battery.txt
@@ -0,0 +1,20 @@
+AXP20x and AXP22x battery power supply
+
+Required Properties:
+ - compatible, one of:
+			"x-powers,axp209-battery-power-supply"
+			"x-powers,axp221-battery-power-supply"
+
+This node is a subnode of the axp20x/axp22x PMIC.
+
+The AXP20X and AXP22X can read the battery voltage, charge and discharge
+currents of the battery by reading ADC channels from the AXP20X/AXP22X
+ADC.
+
+Example:
+
+&axp209 {
+	battery_power_supply: battery-power-supply {
+		compatible = "x-powers,axp209-battery-power-supply";
+	}
+};
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 11/18] dt-bindings: power: supply: add AXP20X/AXP22X battery DT binding
@ 2017-02-14  9:41   ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: thomas.petazzoni, devicetree, linux-pm, linux-iio, linux-kernel,
	Quentin Schulz, bonbons, icenowy, linux-arm-kernel

The X-Powers AXP20X and AXP22X PMICs can have a battery as power supply.

This patch adds the DT binding documentation for the battery power
supply which gets various data from the PMIC, such as the battery status
(charging, discharging, full, dead), current max limit, current current,
battery capacity (in percentage), voltage max and min limits, current
voltage and battery capacity (in Ah).

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
---

v3:
 - removed constant charge current property, now should use the WIP
 battery framework,

v2:
 - changed DT node name from ac_power_supply to ac-power-supply,
 - removed io-channels and io-channel-names from DT (the IIO mapping is
 done in the IIO ADC driver now),
 - added x-powers,constant-charge-current property to set the maximal
 default constant current charge of the battery,

 .../bindings/power/supply/axp20x_battery.txt         | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/power/supply/axp20x_battery.txt

diff --git a/Documentation/devicetree/bindings/power/supply/axp20x_battery.txt b/Documentation/devicetree/bindings/power/supply/axp20x_battery.txt
new file mode 100644
index 0000000..c248866
--- /dev/null
+++ b/Documentation/devicetree/bindings/power/supply/axp20x_battery.txt
@@ -0,0 +1,20 @@
+AXP20x and AXP22x battery power supply
+
+Required Properties:
+ - compatible, one of:
+			"x-powers,axp209-battery-power-supply"
+			"x-powers,axp221-battery-power-supply"
+
+This node is a subnode of the axp20x/axp22x PMIC.
+
+The AXP20X and AXP22X can read the battery voltage, charge and discharge
+currents of the battery by reading ADC channels from the AXP20X/AXP22X
+ADC.
+
+Example:
+
+&axp209 {
+	battery_power_supply: battery-power-supply {
+		compatible = "x-powers,axp209-battery-power-supply";
+	}
+};
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 11/18] dt-bindings: power: supply: add AXP20X/AXP22X battery DT binding
@ 2017-02-14  9:41   ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: linux-arm-kernel

The X-Powers AXP20X and AXP22X PMICs can have a battery as power supply.

This patch adds the DT binding documentation for the battery power
supply which gets various data from the PMIC, such as the battery status
(charging, discharging, full, dead), current max limit, current current,
battery capacity (in percentage), voltage max and min limits, current
voltage and battery capacity (in Ah).

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
---

v3:
 - removed constant charge current property, now should use the WIP
 battery framework,

v2:
 - changed DT node name from ac_power_supply to ac-power-supply,
 - removed io-channels and io-channel-names from DT (the IIO mapping is
 done in the IIO ADC driver now),
 - added x-powers,constant-charge-current property to set the maximal
 default constant current charge of the battery,

 .../bindings/power/supply/axp20x_battery.txt         | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/power/supply/axp20x_battery.txt

diff --git a/Documentation/devicetree/bindings/power/supply/axp20x_battery.txt b/Documentation/devicetree/bindings/power/supply/axp20x_battery.txt
new file mode 100644
index 0000000..c248866
--- /dev/null
+++ b/Documentation/devicetree/bindings/power/supply/axp20x_battery.txt
@@ -0,0 +1,20 @@
+AXP20x and AXP22x battery power supply
+
+Required Properties:
+ - compatible, one of:
+			"x-powers,axp209-battery-power-supply"
+			"x-powers,axp221-battery-power-supply"
+
+This node is a subnode of the axp20x/axp22x PMIC.
+
+The AXP20X and AXP22X can read the battery voltage, charge and discharge
+currents of the battery by reading ADC channels from the AXP20X/AXP22X
+ADC.
+
+Example:
+
+&axp209 {
+	battery_power_supply: battery-power-supply {
+		compatible = "x-powers,axp209-battery-power-supply";
+	}
+};
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 12/18] mfd: axp20x: add CHRG_CTRL1/2/3 to writeable regs for AXP20X/AXP22X
  2017-02-14  9:40 ` Quentin Schulz
  (?)
@ 2017-02-14  9:41   ` Quentin Schulz
  -1 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: Quentin Schulz, linux-iio, devicetree, linux-kernel, linux-pm,
	linux-arm-kernel, thomas.petazzoni, icenowy, bonbons

The CHRG_CTRL1 and CHRG_CTRL2 registers are made for controlling
different battery charging settings such as the constant current charge
value.

The AXP22X also have a third register CHRG_CTRL3 which has settings for
battery charging too.

This adds the CHRG_CTRL1, CHRG_CTRL2 and CHRG_CTRL3 registers to the
list of writeable registers for AXP20X and AXP22X PMICs.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
Acked-by: Chen-Yu Tsai <wens@csie.org>
Acked-for-MFD-by: Lee Jones <lee.jones@linaro.org>
---

v2:
 - added AXP20X_CHRG_CTRL2 and AXP20X_CHRG_CTRL3 to the writeable
 registers table,
 - removed added reg range for ADC data in volatile regs range,

 drivers/mfd/axp20x.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/mfd/axp20x.c b/drivers/mfd/axp20x.c
index 98abe4b..86bc1d5 100644
--- a/drivers/mfd/axp20x.c
+++ b/drivers/mfd/axp20x.c
@@ -66,6 +66,7 @@ static const struct regmap_access_table axp152_volatile_table = {
 static const struct regmap_range axp20x_writeable_ranges[] = {
 	regmap_reg_range(AXP20X_DATACACHE(0), AXP20X_IRQ5_STATE),
 	regmap_reg_range(AXP20X_VBUS_IPSOUT_MGMT, AXP20X_VBUS_IPSOUT_MGMT),
+	regmap_reg_range(AXP20X_CHRG_CTRL1, AXP20X_CHRG_CTRL2),
 	regmap_reg_range(AXP20X_DCDC_MODE, AXP20X_FG_RES),
 	regmap_reg_range(AXP20X_RDC_H, AXP20X_OCV(AXP20X_OCV_MAX)),
 };
@@ -94,6 +95,7 @@ static const struct regmap_access_table axp20x_volatile_table = {
 static const struct regmap_range axp22x_writeable_ranges[] = {
 	regmap_reg_range(AXP20X_DATACACHE(0), AXP20X_IRQ5_STATE),
 	regmap_reg_range(AXP20X_VBUS_IPSOUT_MGMT, AXP20X_VBUS_IPSOUT_MGMT),
+	regmap_reg_range(AXP20X_CHRG_CTRL1, AXP22X_CHRG_CTRL3),
 	regmap_reg_range(AXP20X_DCDC_MODE, AXP22X_BATLOW_THRES1),
 };
 
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 12/18] mfd: axp20x: add CHRG_CTRL1/2/3 to writeable regs for AXP20X/AXP22X
@ 2017-02-14  9:41   ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: thomas.petazzoni, devicetree, linux-pm, linux-iio, linux-kernel,
	Quentin Schulz, bonbons, icenowy, linux-arm-kernel

The CHRG_CTRL1 and CHRG_CTRL2 registers are made for controlling
different battery charging settings such as the constant current charge
value.

The AXP22X also have a third register CHRG_CTRL3 which has settings for
battery charging too.

This adds the CHRG_CTRL1, CHRG_CTRL2 and CHRG_CTRL3 registers to the
list of writeable registers for AXP20X and AXP22X PMICs.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
Acked-by: Chen-Yu Tsai <wens@csie.org>
Acked-for-MFD-by: Lee Jones <lee.jones@linaro.org>
---

v2:
 - added AXP20X_CHRG_CTRL2 and AXP20X_CHRG_CTRL3 to the writeable
 registers table,
 - removed added reg range for ADC data in volatile regs range,

 drivers/mfd/axp20x.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/mfd/axp20x.c b/drivers/mfd/axp20x.c
index 98abe4b..86bc1d5 100644
--- a/drivers/mfd/axp20x.c
+++ b/drivers/mfd/axp20x.c
@@ -66,6 +66,7 @@ static const struct regmap_access_table axp152_volatile_table = {
 static const struct regmap_range axp20x_writeable_ranges[] = {
 	regmap_reg_range(AXP20X_DATACACHE(0), AXP20X_IRQ5_STATE),
 	regmap_reg_range(AXP20X_VBUS_IPSOUT_MGMT, AXP20X_VBUS_IPSOUT_MGMT),
+	regmap_reg_range(AXP20X_CHRG_CTRL1, AXP20X_CHRG_CTRL2),
 	regmap_reg_range(AXP20X_DCDC_MODE, AXP20X_FG_RES),
 	regmap_reg_range(AXP20X_RDC_H, AXP20X_OCV(AXP20X_OCV_MAX)),
 };
@@ -94,6 +95,7 @@ static const struct regmap_access_table axp20x_volatile_table = {
 static const struct regmap_range axp22x_writeable_ranges[] = {
 	regmap_reg_range(AXP20X_DATACACHE(0), AXP20X_IRQ5_STATE),
 	regmap_reg_range(AXP20X_VBUS_IPSOUT_MGMT, AXP20X_VBUS_IPSOUT_MGMT),
+	regmap_reg_range(AXP20X_CHRG_CTRL1, AXP22X_CHRG_CTRL3),
 	regmap_reg_range(AXP20X_DCDC_MODE, AXP22X_BATLOW_THRES1),
 };
 
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 12/18] mfd: axp20x: add CHRG_CTRL1/2/3 to writeable regs for AXP20X/AXP22X
@ 2017-02-14  9:41   ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: linux-arm-kernel

The CHRG_CTRL1 and CHRG_CTRL2 registers are made for controlling
different battery charging settings such as the constant current charge
value.

The AXP22X also have a third register CHRG_CTRL3 which has settings for
battery charging too.

This adds the CHRG_CTRL1, CHRG_CTRL2 and CHRG_CTRL3 registers to the
list of writeable registers for AXP20X and AXP22X PMICs.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
Acked-by: Chen-Yu Tsai <wens@csie.org>
Acked-for-MFD-by: Lee Jones <lee.jones@linaro.org>
---

v2:
 - added AXP20X_CHRG_CTRL2 and AXP20X_CHRG_CTRL3 to the writeable
 registers table,
 - removed added reg range for ADC data in volatile regs range,

 drivers/mfd/axp20x.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/mfd/axp20x.c b/drivers/mfd/axp20x.c
index 98abe4b..86bc1d5 100644
--- a/drivers/mfd/axp20x.c
+++ b/drivers/mfd/axp20x.c
@@ -66,6 +66,7 @@ static const struct regmap_access_table axp152_volatile_table = {
 static const struct regmap_range axp20x_writeable_ranges[] = {
 	regmap_reg_range(AXP20X_DATACACHE(0), AXP20X_IRQ5_STATE),
 	regmap_reg_range(AXP20X_VBUS_IPSOUT_MGMT, AXP20X_VBUS_IPSOUT_MGMT),
+	regmap_reg_range(AXP20X_CHRG_CTRL1, AXP20X_CHRG_CTRL2),
 	regmap_reg_range(AXP20X_DCDC_MODE, AXP20X_FG_RES),
 	regmap_reg_range(AXP20X_RDC_H, AXP20X_OCV(AXP20X_OCV_MAX)),
 };
@@ -94,6 +95,7 @@ static const struct regmap_access_table axp20x_volatile_table = {
 static const struct regmap_range axp22x_writeable_ranges[] = {
 	regmap_reg_range(AXP20X_DATACACHE(0), AXP20X_IRQ5_STATE),
 	regmap_reg_range(AXP20X_VBUS_IPSOUT_MGMT, AXP20X_VBUS_IPSOUT_MGMT),
+	regmap_reg_range(AXP20X_CHRG_CTRL1, AXP22X_CHRG_CTRL3),
 	regmap_reg_range(AXP20X_DCDC_MODE, AXP22X_BATLOW_THRES1),
 };
 
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 13/18] power: supply: add battery driver for AXP20X and AXP22X PMICs
  2017-02-14  9:40 ` Quentin Schulz
  (?)
@ 2017-02-14  9:41   ` Quentin Schulz
  -1 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: Quentin Schulz, linux-iio, devicetree, linux-kernel, linux-pm,
	linux-arm-kernel, thomas.petazzoni, icenowy, bonbons

The X-Powers AXP20X and AXP22X PMICs can have a battery as power supply.

This patch adds the battery power supply driver to get various data from
the PMIC, such as the battery status (charging, discharging, full,
dead), current max limit, current current, battery capacity (in
percentage), voltage max and min limits, current voltage and battery
capacity (in Ah).

This battery driver uses the AXP20X/AXP22X ADC driver as PMIC data
provider.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Jonathan Cameron <jic23@kernel.org>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
---

v3:
 - added axp20x_set_voltage_min_design function so it can be reused,
 - used power_supply_get_battery_info for setting constant charge current
 instead of x-powers,constant-charge-current introduced in v2,
 - used power_supply_get_battery_info for setting voltage min design of
 the battery,

v2:
 - changed BIT(x) to 1 << x when describing bits purpose for which 2 <<
 x or 3 << x exists, to be consistent,
 - switched from POWER_SUPPLY_PROP_CURRENT_MAX to
 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
 - added POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX to the list of
 readable properties,
 - replaced µ character by a common u for micro units to make checkpatch
 happy,
 - factorized code in axp20x_battery_set_max_voltage,
 - added a axp20x_set_constant_charge_current function to be used when
 setting the value from sysfs and from the DT,
 - removed some dead code,
 - added a DT property to set constant current charge of the battery
 (x-powers,constant-charge-current),
 - migrated to dev_get_regmap instead of manually looking for the regmap
 in the drvdata of the parent,
 - switched from int to uintptr_t cast to make sure the cast is always
 for the same size type (make build on 64bits platforms happy mainly),
 drivers/power/supply/Kconfig          |  12 +
 drivers/power/supply/Makefile         |   1 +
 drivers/power/supply/axp20x_battery.c | 492 ++++++++++++++++++++++++++++++++++
 3 files changed, 505 insertions(+)
 create mode 100644 drivers/power/supply/axp20x_battery.c

diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig
index c552b4b..48619de 100644
--- a/drivers/power/supply/Kconfig
+++ b/drivers/power/supply/Kconfig
@@ -226,6 +226,18 @@ config CHARGER_AXP20X
 	  This driver can also be built as a module. If so, the module will be
 	  called axp20x_ac_power.
 
+config BATTERY_AXP20X
+	tristate "X-Powers AXP20X battery driver"
+	depends on MFD_AXP20X
+	depends on AXP20X_ADC
+	depends on IIO
+	help
+	  Say Y here to enable support for X-Powers AXP20X PMICs' battery power
+	  supply.
+
+	  This driver can also be built as a module. If so, the module will be
+	  called axp20x_battery.
+
 config AXP288_CHARGER
 	tristate "X-Powers AXP288 Charger"
 	depends on MFD_AXP20X && EXTCON_AXP288
diff --git a/drivers/power/supply/Makefile b/drivers/power/supply/Makefile
index 7d22417..5a217b2 100644
--- a/drivers/power/supply/Makefile
+++ b/drivers/power/supply/Makefile
@@ -18,6 +18,7 @@ obj-$(CONFIG_TEST_POWER)	+= test_power.o
 
 obj-$(CONFIG_BATTERY_88PM860X)	+= 88pm860x_battery.o
 obj-$(CONFIG_BATTERY_ACT8945A)	+= act8945a_charger.o
+obj-$(CONFIG_BATTERY_AXP20X)	+= axp20x_battery.o
 obj-$(CONFIG_CHARGER_AXP20X)	+= axp20x_ac_power.o
 obj-$(CONFIG_BATTERY_DS2760)	+= ds2760_battery.o
 obj-$(CONFIG_BATTERY_DS2780)	+= ds2780_battery.o
diff --git a/drivers/power/supply/axp20x_battery.c b/drivers/power/supply/axp20x_battery.c
new file mode 100644
index 0000000..bd16ac6
--- /dev/null
+++ b/drivers/power/supply/axp20x_battery.c
@@ -0,0 +1,492 @@
+/*
+ * Battery power supply driver for X-Powers AXP20X and AXP22X PMICs
+ *
+ * Copyright 2016 Free Electrons NextThing Co.
+ *	Quentin Schulz <quentin.schulz@free-electrons.com>
+ *
+ * This driver is based on a previous upstreaming attempt by:
+ *	Bruno Prémont <bonbons@linux-vserver.org>
+ *
+ * This file is subject to the terms and conditions of the GNU General
+ * Public License. See the file "COPYING" in the main directory of this
+ * archive for more details.
+ *
+ * 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.
+ */
+
+#include <linux/err.h>
+#include <linux/interrupt.h>
+#include <linux/irq.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_device.h>
+#include <linux/platform_device.h>
+#include <linux/power_supply.h>
+#include <linux/regmap.h>
+#include <linux/slab.h>
+#include <linux/time.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/consumer.h>
+#include <linux/mfd/axp20x.h>
+
+#define AXP20X_PWR_STATUS_BAT_CHARGING	BIT(2)
+
+#define AXP20X_PWR_OP_BATT_PRESENT	BIT(5)
+#define AXP20X_PWR_OP_BATT_ACTIVATED	BIT(3)
+
+#define AXP209_FG_PERCENT		GENMASK(6, 0)
+#define AXP22X_FG_VALID			BIT(7)
+
+#define AXP20X_CHRG_CTRL1_TGT_VOLT	GENMASK(6, 5)
+#define AXP20X_CHRG_CTRL1_TGT_4_1V	(0 << 5)
+#define AXP20X_CHRG_CTRL1_TGT_4_15V	(1 << 5)
+#define AXP20X_CHRG_CTRL1_TGT_4_2V	(2 << 5)
+#define AXP20X_CHRG_CTRL1_TGT_4_36V	(3 << 5)
+
+#define AXP22X_CHRG_CTRL1_TGT_4_22V	(1 << 5)
+#define AXP22X_CHRG_CTRL1_TGT_4_24V	(3 << 5)
+
+#define AXP20X_CHRG_CTRL1_TGT_CURR	GENMASK(3, 0)
+
+#define AXP20X_V_OFF_MASK		GENMASK(2, 0)
+
+struct axp20x_batt_ps {
+	struct regmap *regmap;
+	struct power_supply *batt;
+	struct axp20x_dev *axp20x;
+	struct iio_channel *batt_chrg_i;
+	struct iio_channel *batt_dischrg_i;
+	struct iio_channel *batt_v;
+	u8 axp_id;
+};
+
+static int axp20x_battery_get_max_voltage(struct axp20x_batt_ps *axp20x_batt,
+					  int *val)
+{
+	int ret, reg;
+
+	ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, &reg);
+	if (ret)
+		return ret;
+
+	switch (reg & AXP20X_CHRG_CTRL1_TGT_VOLT) {
+	case AXP20X_CHRG_CTRL1_TGT_4_1V:
+		*val = 4100000;
+		break;
+	case AXP20X_CHRG_CTRL1_TGT_4_15V:
+		*val = 4150000;
+		break;
+	case AXP20X_CHRG_CTRL1_TGT_4_2V:
+		*val = 4200000;
+		break;
+	case AXP20X_CHRG_CTRL1_TGT_4_36V:
+		*val = 4360000;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int axp22x_battery_get_max_voltage(struct axp20x_batt_ps *axp20x_batt,
+					  int *val)
+{
+	int ret, reg;
+
+	ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, &reg);
+	if (ret)
+		return ret;
+
+	switch (reg & AXP20X_CHRG_CTRL1_TGT_VOLT) {
+	case AXP20X_CHRG_CTRL1_TGT_4_1V:
+		*val = 4100000;
+		break;
+	case AXP20X_CHRG_CTRL1_TGT_4_2V:
+		*val = 4200000;
+		break;
+	case AXP22X_CHRG_CTRL1_TGT_4_22V:
+		*val = 4220000;
+		break;
+	case AXP22X_CHRG_CTRL1_TGT_4_24V:
+		*val = 4240000;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int axp20x_battery_get_prop(struct power_supply *psy,
+				   enum power_supply_property psp,
+				   union power_supply_propval *val)
+{
+	struct axp20x_batt_ps *axp20x_batt = power_supply_get_drvdata(psy);
+	struct iio_channel *chan;
+	int ret = 0, reg, val1;
+
+	switch (psp) {
+	case POWER_SUPPLY_PROP_PRESENT:
+	case POWER_SUPPLY_PROP_ONLINE:
+		ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
+				  &reg);
+		if (ret)
+			return ret;
+
+		val->intval = !!(reg & AXP20X_PWR_OP_BATT_PRESENT);
+		break;
+
+	case POWER_SUPPLY_PROP_STATUS:
+		ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_INPUT_STATUS,
+				  &reg);
+		if (ret)
+			return ret;
+
+		if (reg & AXP20X_PWR_STATUS_BAT_CHARGING) {
+			val->intval = POWER_SUPPLY_STATUS_CHARGING;
+			return 0;
+		}
+
+		ret = iio_read_channel_processed(axp20x_batt->batt_dischrg_i,
+						 &val1);
+		if (ret)
+			return ret;
+
+		if (val1) {
+			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
+			return 0;
+		}
+
+		ret = regmap_read(axp20x_batt->regmap, AXP20X_FG_RES, &val1);
+		if (ret)
+			return ret;
+
+		/*
+		 * Fuel Gauge data takes 7 bits but the stored value seems to be
+		 * directly the raw percentage without any scaling to 7 bits.
+		 */
+		if ((val1 & AXP209_FG_PERCENT) == 100)
+			val->intval = POWER_SUPPLY_STATUS_FULL;
+		else
+			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
+		break;
+
+	case POWER_SUPPLY_PROP_HEALTH:
+		ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
+				  &val1);
+		if (ret)
+			return ret;
+
+		if (val1 & AXP20X_PWR_OP_BATT_ACTIVATED) {
+			val->intval = POWER_SUPPLY_HEALTH_DEAD;
+			return 0;
+		}
+
+		val->intval = POWER_SUPPLY_HEALTH_GOOD;
+		break;
+
+	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
+		ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, &reg);
+		if (ret)
+			return ret;
+
+		reg &= AXP20X_CHRG_CTRL1_TGT_CURR;
+		val->intval = reg * 100000 + 300000;
+		break;
+
+	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
+		val->intval = AXP20X_CHRG_CTRL1_TGT_CURR * 100000 + 300000;
+		break;
+
+	case POWER_SUPPLY_PROP_CURRENT_NOW:
+		ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_INPUT_STATUS,
+				  &reg);
+		if (ret)
+			return ret;
+
+		if (reg & AXP20X_PWR_STATUS_BAT_CHARGING)
+			chan = axp20x_batt->batt_chrg_i;
+		else
+			chan = axp20x_batt->batt_dischrg_i;
+
+		ret = iio_read_channel_processed(chan, &val->intval);
+		if (ret)
+			return ret;
+
+		/* IIO framework gives mA but Power Supply framework gives uA */
+		val->intval *= 1000;
+		break;
+
+	case POWER_SUPPLY_PROP_CAPACITY:
+		/* When no battery is present, return capacity is 100% */
+		ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
+				  &reg);
+		if (ret)
+			return ret;
+
+		if (!(reg & AXP20X_PWR_OP_BATT_PRESENT)) {
+			val->intval = 100;
+			return 0;
+		}
+
+		ret = regmap_read(axp20x_batt->regmap, AXP20X_FG_RES, &reg);
+		if (ret)
+			return ret;
+
+		if (axp20x_batt->axp_id == AXP221_ID &&
+		    !(reg & AXP22X_FG_VALID))
+			return -EINVAL;
+
+		/*
+		 * Fuel Gauge data takes 7 bits but the stored value seems to be
+		 * directly the raw percentage without any scaling to 7 bits.
+		 */
+		val->intval = reg & AXP209_FG_PERCENT;
+		break;
+
+	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
+		if (axp20x_batt->axp_id == AXP209_ID)
+			return axp20x_battery_get_max_voltage(axp20x_batt,
+							      &val->intval);
+		return axp22x_battery_get_max_voltage(axp20x_batt,
+						      &val->intval);
+
+	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
+		ret = regmap_read(axp20x_batt->regmap, AXP20X_V_OFF, &reg);
+		if (ret)
+			return ret;
+
+		val->intval = 2600000 + 100000 * (reg & AXP20X_V_OFF_MASK);
+		break;
+
+	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
+		ret = iio_read_channel_processed(axp20x_batt->batt_v,
+						 &val->intval);
+		if (ret)
+			return ret;
+
+		/* IIO framework gives mV but Power Supply framework gives uV */
+		val->intval *= 1000;
+		break;
+
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int axp20x_battery_set_max_voltage(struct axp20x_batt_ps *axp20x_batt,
+					  int val)
+{
+	switch (val) {
+	case 4100000:
+		val = AXP20X_CHRG_CTRL1_TGT_4_1V;
+		break;
+
+	case 4150000:
+		if (axp20x_batt->axp_id == AXP221_ID)
+			return -EINVAL;
+
+		val = AXP20X_CHRG_CTRL1_TGT_4_15V;
+		break;
+
+	case 4200000:
+		val = AXP20X_CHRG_CTRL1_TGT_4_2V;
+		break;
+
+	default:
+		/*
+		 * AXP20x max voltage can be set to 4.36V and AXP22X max voltage
+		 * can be set to 4.22V and 4.24V, but these voltages are too
+		 * high for Lithium based batteries (AXP PMICs are supposed to
+		 * be used with these kinds of battery).
+		 */
+		return -EINVAL;
+	}
+
+	return regmap_update_bits(axp20x_batt->regmap, AXP20X_CHRG_CTRL1,
+				  AXP20X_CHRG_CTRL1_TGT_VOLT, val);
+}
+
+static int axp20x_set_constant_charge_current(struct axp20x_batt_ps *axp_batt,
+					      int charge_current)
+{
+	if (axp_batt->axp_id == AXP209_ID)
+		charge_current = (charge_current - 300000) / 100000;
+	else
+		charge_current = (charge_current - 300000) / 150000;
+
+	if (charge_current > AXP20X_CHRG_CTRL1_TGT_CURR || charge_current < 0)
+		return -EINVAL;
+
+	return regmap_update_bits(axp_batt->regmap, AXP20X_CHRG_CTRL1,
+				  AXP20X_CHRG_CTRL1_TGT_CURR, charge_current);
+}
+
+static int axp20x_set_voltage_min_design(struct axp20x_batt_ps *axp_batt,
+					 int min_voltage)
+{
+	int val1 = (min_voltage - 2600000) / 100000;
+
+	if (val1 < 0 || val1 > AXP20X_V_OFF_MASK)
+		return -EINVAL;
+
+	return regmap_update_bits(axp_batt->regmap, AXP20X_V_OFF,
+				  AXP20X_V_OFF_MASK, val1);
+}
+
+static int axp20x_battery_set_prop(struct power_supply *psy,
+				   enum power_supply_property psp,
+				   const union power_supply_propval *val)
+{
+	struct axp20x_batt_ps *axp20x_batt = power_supply_get_drvdata(psy);
+
+	switch (psp) {
+	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
+		return axp20x_set_voltage_min_design(axp20x_batt, val->intval);
+
+	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
+		return axp20x_battery_set_max_voltage(axp20x_batt, val->intval);
+
+	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
+		return axp20x_set_constant_charge_current(axp20x_batt,
+							  val->intval);
+
+	default:
+		return -EINVAL;
+	}
+}
+
+static enum power_supply_property axp20x_battery_props[] = {
+	POWER_SUPPLY_PROP_PRESENT,
+	POWER_SUPPLY_PROP_ONLINE,
+	POWER_SUPPLY_PROP_STATUS,
+	POWER_SUPPLY_PROP_VOLTAGE_NOW,
+	POWER_SUPPLY_PROP_CURRENT_NOW,
+	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
+	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
+	POWER_SUPPLY_PROP_HEALTH,
+	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
+	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
+	POWER_SUPPLY_PROP_CAPACITY,
+};
+
+static int axp20x_battery_prop_writeable(struct power_supply *psy,
+					 enum power_supply_property psp)
+{
+	return psp == POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN ||
+	       psp == POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN ||
+	       psp == POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT;
+}
+
+static const struct power_supply_desc axp20x_batt_ps_desc = {
+	.name = "axp20x-battery",
+	.type = POWER_SUPPLY_TYPE_BATTERY,
+	.properties = axp20x_battery_props,
+	.num_properties = ARRAY_SIZE(axp20x_battery_props),
+	.property_is_writeable = axp20x_battery_prop_writeable,
+	.get_property = axp20x_battery_get_prop,
+	.set_property = axp20x_battery_set_prop,
+};
+
+static const struct of_device_id axp20x_battery_ps_id[] = {
+	{
+		.compatible = "x-powers,axp209-battery-power-supply",
+		.data = (void *)AXP209_ID,
+	}, {
+		.compatible = "x-powers,axp221-battery-power-supply",
+		.data = (void *)AXP221_ID,
+	}, { /* sentinel */ },
+};
+MODULE_DEVICE_TABLE(of, axp20x_battery_ps_id);
+
+static int axp20x_power_probe(struct platform_device *pdev)
+{
+	struct axp20x_batt_ps *axp20x_batt;
+	struct power_supply_config psy_cfg = {};
+	struct power_supply_battery_info info;
+
+	if (!of_device_is_available(pdev->dev.of_node))
+		return -ENODEV;
+
+	axp20x_batt = devm_kzalloc(&pdev->dev, sizeof(*axp20x_batt),
+				   GFP_KERNEL);
+	if (!axp20x_batt)
+		return -ENOMEM;
+
+	axp20x_batt->batt_v = devm_iio_channel_get(&pdev->dev, "batt_v");
+	if (IS_ERR(axp20x_batt->batt_v)) {
+		if (PTR_ERR(axp20x_batt->batt_v) == -ENODEV)
+			return -EPROBE_DEFER;
+		return PTR_ERR(axp20x_batt->batt_v);
+	}
+
+	axp20x_batt->batt_chrg_i = devm_iio_channel_get(&pdev->dev,
+							"batt_chrg_i");
+	if (IS_ERR(axp20x_batt->batt_chrg_i)) {
+		if (PTR_ERR(axp20x_batt->batt_chrg_i) == -ENODEV)
+			return -EPROBE_DEFER;
+		return PTR_ERR(axp20x_batt->batt_chrg_i);
+	}
+
+	axp20x_batt->batt_dischrg_i = devm_iio_channel_get(&pdev->dev,
+							   "batt_dischrg_i");
+	if (IS_ERR(axp20x_batt->batt_dischrg_i)) {
+		if (PTR_ERR(axp20x_batt->batt_dischrg_i) == -ENODEV)
+			return -EPROBE_DEFER;
+		return PTR_ERR(axp20x_batt->batt_dischrg_i);
+	}
+
+	axp20x_batt->regmap = dev_get_regmap(pdev->dev.parent, NULL);
+	platform_set_drvdata(pdev, axp20x_batt);
+
+	psy_cfg.drv_data = axp20x_batt;
+	psy_cfg.of_node = pdev->dev.of_node;
+
+	axp20x_batt->axp_id = (uintptr_t)of_device_get_match_data(&pdev->dev);
+
+	axp20x_batt->batt = devm_power_supply_register(&pdev->dev,
+						       &axp20x_batt_ps_desc,
+						       &psy_cfg);
+	if (IS_ERR(axp20x_batt->batt)) {
+		dev_err(&pdev->dev, "failed to register power supply: %ld\n",
+			PTR_ERR(axp20x_batt->batt));
+		return PTR_ERR(axp20x_batt->batt);
+	}
+
+	if (!power_supply_get_battery_info(axp20x_batt->batt, &info)) {
+		int vmin = info.voltage_min_design_uv;
+		int ccc = info.constant_charge_current_ua;
+
+		if (vmin > 0 && axp20x_set_voltage_min_design(axp20x_batt,
+							      vmin))
+			dev_err(&pdev->dev,
+				"couldn't set voltage_min_design\n");
+
+		if (ccc > 0 && axp20x_set_constant_charge_current(axp20x_batt,
+								  ccc))
+			dev_err(&pdev->dev,
+				"couldn't set constant charge current\n");
+	}
+
+	return 0;
+}
+
+static struct platform_driver axp20x_batt_driver = {
+	.probe    = axp20x_power_probe,
+	.driver   = {
+		.name  = "axp20x-battery-power-supply",
+		.of_match_table = axp20x_battery_ps_id,
+	},
+};
+
+module_platform_driver(axp20x_batt_driver);
+
+MODULE_DESCRIPTION("Battery power supply driver for AXP20X and AXP22X PMICs");
+MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
+MODULE_LICENSE("GPL");
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 13/18] power: supply: add battery driver for AXP20X and AXP22X PMICs
@ 2017-02-14  9:41   ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: thomas.petazzoni, devicetree, linux-pm, linux-iio, linux-kernel,
	Quentin Schulz, bonbons, icenowy, linux-arm-kernel

The X-Powers AXP20X and AXP22X PMICs can have a battery as power supply.

This patch adds the battery power supply driver to get various data from
the PMIC, such as the battery status (charging, discharging, full,
dead), current max limit, current current, battery capacity (in
percentage), voltage max and min limits, current voltage and battery
capacity (in Ah).

This battery driver uses the AXP20X/AXP22X ADC driver as PMIC data
provider.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Jonathan Cameron <jic23@kernel.org>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
---

v3:
 - added axp20x_set_voltage_min_design function so it can be reused,
 - used power_supply_get_battery_info for setting constant charge current
 instead of x-powers,constant-charge-current introduced in v2,
 - used power_supply_get_battery_info for setting voltage min design of
 the battery,

v2:
 - changed BIT(x) to 1 << x when describing bits purpose for which 2 <<
 x or 3 << x exists, to be consistent,
 - switched from POWER_SUPPLY_PROP_CURRENT_MAX to
 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
 - added POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX to the list of
 readable properties,
 - replaced µ character by a common u for micro units to make checkpatch
 happy,
 - factorized code in axp20x_battery_set_max_voltage,
 - added a axp20x_set_constant_charge_current function to be used when
 setting the value from sysfs and from the DT,
 - removed some dead code,
 - added a DT property to set constant current charge of the battery
 (x-powers,constant-charge-current),
 - migrated to dev_get_regmap instead of manually looking for the regmap
 in the drvdata of the parent,
 - switched from int to uintptr_t cast to make sure the cast is always
 for the same size type (make build on 64bits platforms happy mainly),
 drivers/power/supply/Kconfig          |  12 +
 drivers/power/supply/Makefile         |   1 +
 drivers/power/supply/axp20x_battery.c | 492 ++++++++++++++++++++++++++++++++++
 3 files changed, 505 insertions(+)
 create mode 100644 drivers/power/supply/axp20x_battery.c

diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig
index c552b4b..48619de 100644
--- a/drivers/power/supply/Kconfig
+++ b/drivers/power/supply/Kconfig
@@ -226,6 +226,18 @@ config CHARGER_AXP20X
 	  This driver can also be built as a module. If so, the module will be
 	  called axp20x_ac_power.
 
+config BATTERY_AXP20X
+	tristate "X-Powers AXP20X battery driver"
+	depends on MFD_AXP20X
+	depends on AXP20X_ADC
+	depends on IIO
+	help
+	  Say Y here to enable support for X-Powers AXP20X PMICs' battery power
+	  supply.
+
+	  This driver can also be built as a module. If so, the module will be
+	  called axp20x_battery.
+
 config AXP288_CHARGER
 	tristate "X-Powers AXP288 Charger"
 	depends on MFD_AXP20X && EXTCON_AXP288
diff --git a/drivers/power/supply/Makefile b/drivers/power/supply/Makefile
index 7d22417..5a217b2 100644
--- a/drivers/power/supply/Makefile
+++ b/drivers/power/supply/Makefile
@@ -18,6 +18,7 @@ obj-$(CONFIG_TEST_POWER)	+= test_power.o
 
 obj-$(CONFIG_BATTERY_88PM860X)	+= 88pm860x_battery.o
 obj-$(CONFIG_BATTERY_ACT8945A)	+= act8945a_charger.o
+obj-$(CONFIG_BATTERY_AXP20X)	+= axp20x_battery.o
 obj-$(CONFIG_CHARGER_AXP20X)	+= axp20x_ac_power.o
 obj-$(CONFIG_BATTERY_DS2760)	+= ds2760_battery.o
 obj-$(CONFIG_BATTERY_DS2780)	+= ds2780_battery.o
diff --git a/drivers/power/supply/axp20x_battery.c b/drivers/power/supply/axp20x_battery.c
new file mode 100644
index 0000000..bd16ac6
--- /dev/null
+++ b/drivers/power/supply/axp20x_battery.c
@@ -0,0 +1,492 @@
+/*
+ * Battery power supply driver for X-Powers AXP20X and AXP22X PMICs
+ *
+ * Copyright 2016 Free Electrons NextThing Co.
+ *	Quentin Schulz <quentin.schulz@free-electrons.com>
+ *
+ * This driver is based on a previous upstreaming attempt by:
+ *	Bruno Prémont <bonbons@linux-vserver.org>
+ *
+ * This file is subject to the terms and conditions of the GNU General
+ * Public License. See the file "COPYING" in the main directory of this
+ * archive for more details.
+ *
+ * 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.
+ */
+
+#include <linux/err.h>
+#include <linux/interrupt.h>
+#include <linux/irq.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_device.h>
+#include <linux/platform_device.h>
+#include <linux/power_supply.h>
+#include <linux/regmap.h>
+#include <linux/slab.h>
+#include <linux/time.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/consumer.h>
+#include <linux/mfd/axp20x.h>
+
+#define AXP20X_PWR_STATUS_BAT_CHARGING	BIT(2)
+
+#define AXP20X_PWR_OP_BATT_PRESENT	BIT(5)
+#define AXP20X_PWR_OP_BATT_ACTIVATED	BIT(3)
+
+#define AXP209_FG_PERCENT		GENMASK(6, 0)
+#define AXP22X_FG_VALID			BIT(7)
+
+#define AXP20X_CHRG_CTRL1_TGT_VOLT	GENMASK(6, 5)
+#define AXP20X_CHRG_CTRL1_TGT_4_1V	(0 << 5)
+#define AXP20X_CHRG_CTRL1_TGT_4_15V	(1 << 5)
+#define AXP20X_CHRG_CTRL1_TGT_4_2V	(2 << 5)
+#define AXP20X_CHRG_CTRL1_TGT_4_36V	(3 << 5)
+
+#define AXP22X_CHRG_CTRL1_TGT_4_22V	(1 << 5)
+#define AXP22X_CHRG_CTRL1_TGT_4_24V	(3 << 5)
+
+#define AXP20X_CHRG_CTRL1_TGT_CURR	GENMASK(3, 0)
+
+#define AXP20X_V_OFF_MASK		GENMASK(2, 0)
+
+struct axp20x_batt_ps {
+	struct regmap *regmap;
+	struct power_supply *batt;
+	struct axp20x_dev *axp20x;
+	struct iio_channel *batt_chrg_i;
+	struct iio_channel *batt_dischrg_i;
+	struct iio_channel *batt_v;
+	u8 axp_id;
+};
+
+static int axp20x_battery_get_max_voltage(struct axp20x_batt_ps *axp20x_batt,
+					  int *val)
+{
+	int ret, reg;
+
+	ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, &reg);
+	if (ret)
+		return ret;
+
+	switch (reg & AXP20X_CHRG_CTRL1_TGT_VOLT) {
+	case AXP20X_CHRG_CTRL1_TGT_4_1V:
+		*val = 4100000;
+		break;
+	case AXP20X_CHRG_CTRL1_TGT_4_15V:
+		*val = 4150000;
+		break;
+	case AXP20X_CHRG_CTRL1_TGT_4_2V:
+		*val = 4200000;
+		break;
+	case AXP20X_CHRG_CTRL1_TGT_4_36V:
+		*val = 4360000;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int axp22x_battery_get_max_voltage(struct axp20x_batt_ps *axp20x_batt,
+					  int *val)
+{
+	int ret, reg;
+
+	ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, &reg);
+	if (ret)
+		return ret;
+
+	switch (reg & AXP20X_CHRG_CTRL1_TGT_VOLT) {
+	case AXP20X_CHRG_CTRL1_TGT_4_1V:
+		*val = 4100000;
+		break;
+	case AXP20X_CHRG_CTRL1_TGT_4_2V:
+		*val = 4200000;
+		break;
+	case AXP22X_CHRG_CTRL1_TGT_4_22V:
+		*val = 4220000;
+		break;
+	case AXP22X_CHRG_CTRL1_TGT_4_24V:
+		*val = 4240000;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int axp20x_battery_get_prop(struct power_supply *psy,
+				   enum power_supply_property psp,
+				   union power_supply_propval *val)
+{
+	struct axp20x_batt_ps *axp20x_batt = power_supply_get_drvdata(psy);
+	struct iio_channel *chan;
+	int ret = 0, reg, val1;
+
+	switch (psp) {
+	case POWER_SUPPLY_PROP_PRESENT:
+	case POWER_SUPPLY_PROP_ONLINE:
+		ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
+				  &reg);
+		if (ret)
+			return ret;
+
+		val->intval = !!(reg & AXP20X_PWR_OP_BATT_PRESENT);
+		break;
+
+	case POWER_SUPPLY_PROP_STATUS:
+		ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_INPUT_STATUS,
+				  &reg);
+		if (ret)
+			return ret;
+
+		if (reg & AXP20X_PWR_STATUS_BAT_CHARGING) {
+			val->intval = POWER_SUPPLY_STATUS_CHARGING;
+			return 0;
+		}
+
+		ret = iio_read_channel_processed(axp20x_batt->batt_dischrg_i,
+						 &val1);
+		if (ret)
+			return ret;
+
+		if (val1) {
+			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
+			return 0;
+		}
+
+		ret = regmap_read(axp20x_batt->regmap, AXP20X_FG_RES, &val1);
+		if (ret)
+			return ret;
+
+		/*
+		 * Fuel Gauge data takes 7 bits but the stored value seems to be
+		 * directly the raw percentage without any scaling to 7 bits.
+		 */
+		if ((val1 & AXP209_FG_PERCENT) == 100)
+			val->intval = POWER_SUPPLY_STATUS_FULL;
+		else
+			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
+		break;
+
+	case POWER_SUPPLY_PROP_HEALTH:
+		ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
+				  &val1);
+		if (ret)
+			return ret;
+
+		if (val1 & AXP20X_PWR_OP_BATT_ACTIVATED) {
+			val->intval = POWER_SUPPLY_HEALTH_DEAD;
+			return 0;
+		}
+
+		val->intval = POWER_SUPPLY_HEALTH_GOOD;
+		break;
+
+	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
+		ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, &reg);
+		if (ret)
+			return ret;
+
+		reg &= AXP20X_CHRG_CTRL1_TGT_CURR;
+		val->intval = reg * 100000 + 300000;
+		break;
+
+	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
+		val->intval = AXP20X_CHRG_CTRL1_TGT_CURR * 100000 + 300000;
+		break;
+
+	case POWER_SUPPLY_PROP_CURRENT_NOW:
+		ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_INPUT_STATUS,
+				  &reg);
+		if (ret)
+			return ret;
+
+		if (reg & AXP20X_PWR_STATUS_BAT_CHARGING)
+			chan = axp20x_batt->batt_chrg_i;
+		else
+			chan = axp20x_batt->batt_dischrg_i;
+
+		ret = iio_read_channel_processed(chan, &val->intval);
+		if (ret)
+			return ret;
+
+		/* IIO framework gives mA but Power Supply framework gives uA */
+		val->intval *= 1000;
+		break;
+
+	case POWER_SUPPLY_PROP_CAPACITY:
+		/* When no battery is present, return capacity is 100% */
+		ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
+				  &reg);
+		if (ret)
+			return ret;
+
+		if (!(reg & AXP20X_PWR_OP_BATT_PRESENT)) {
+			val->intval = 100;
+			return 0;
+		}
+
+		ret = regmap_read(axp20x_batt->regmap, AXP20X_FG_RES, &reg);
+		if (ret)
+			return ret;
+
+		if (axp20x_batt->axp_id == AXP221_ID &&
+		    !(reg & AXP22X_FG_VALID))
+			return -EINVAL;
+
+		/*
+		 * Fuel Gauge data takes 7 bits but the stored value seems to be
+		 * directly the raw percentage without any scaling to 7 bits.
+		 */
+		val->intval = reg & AXP209_FG_PERCENT;
+		break;
+
+	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
+		if (axp20x_batt->axp_id == AXP209_ID)
+			return axp20x_battery_get_max_voltage(axp20x_batt,
+							      &val->intval);
+		return axp22x_battery_get_max_voltage(axp20x_batt,
+						      &val->intval);
+
+	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
+		ret = regmap_read(axp20x_batt->regmap, AXP20X_V_OFF, &reg);
+		if (ret)
+			return ret;
+
+		val->intval = 2600000 + 100000 * (reg & AXP20X_V_OFF_MASK);
+		break;
+
+	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
+		ret = iio_read_channel_processed(axp20x_batt->batt_v,
+						 &val->intval);
+		if (ret)
+			return ret;
+
+		/* IIO framework gives mV but Power Supply framework gives uV */
+		val->intval *= 1000;
+		break;
+
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int axp20x_battery_set_max_voltage(struct axp20x_batt_ps *axp20x_batt,
+					  int val)
+{
+	switch (val) {
+	case 4100000:
+		val = AXP20X_CHRG_CTRL1_TGT_4_1V;
+		break;
+
+	case 4150000:
+		if (axp20x_batt->axp_id == AXP221_ID)
+			return -EINVAL;
+
+		val = AXP20X_CHRG_CTRL1_TGT_4_15V;
+		break;
+
+	case 4200000:
+		val = AXP20X_CHRG_CTRL1_TGT_4_2V;
+		break;
+
+	default:
+		/*
+		 * AXP20x max voltage can be set to 4.36V and AXP22X max voltage
+		 * can be set to 4.22V and 4.24V, but these voltages are too
+		 * high for Lithium based batteries (AXP PMICs are supposed to
+		 * be used with these kinds of battery).
+		 */
+		return -EINVAL;
+	}
+
+	return regmap_update_bits(axp20x_batt->regmap, AXP20X_CHRG_CTRL1,
+				  AXP20X_CHRG_CTRL1_TGT_VOLT, val);
+}
+
+static int axp20x_set_constant_charge_current(struct axp20x_batt_ps *axp_batt,
+					      int charge_current)
+{
+	if (axp_batt->axp_id == AXP209_ID)
+		charge_current = (charge_current - 300000) / 100000;
+	else
+		charge_current = (charge_current - 300000) / 150000;
+
+	if (charge_current > AXP20X_CHRG_CTRL1_TGT_CURR || charge_current < 0)
+		return -EINVAL;
+
+	return regmap_update_bits(axp_batt->regmap, AXP20X_CHRG_CTRL1,
+				  AXP20X_CHRG_CTRL1_TGT_CURR, charge_current);
+}
+
+static int axp20x_set_voltage_min_design(struct axp20x_batt_ps *axp_batt,
+					 int min_voltage)
+{
+	int val1 = (min_voltage - 2600000) / 100000;
+
+	if (val1 < 0 || val1 > AXP20X_V_OFF_MASK)
+		return -EINVAL;
+
+	return regmap_update_bits(axp_batt->regmap, AXP20X_V_OFF,
+				  AXP20X_V_OFF_MASK, val1);
+}
+
+static int axp20x_battery_set_prop(struct power_supply *psy,
+				   enum power_supply_property psp,
+				   const union power_supply_propval *val)
+{
+	struct axp20x_batt_ps *axp20x_batt = power_supply_get_drvdata(psy);
+
+	switch (psp) {
+	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
+		return axp20x_set_voltage_min_design(axp20x_batt, val->intval);
+
+	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
+		return axp20x_battery_set_max_voltage(axp20x_batt, val->intval);
+
+	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
+		return axp20x_set_constant_charge_current(axp20x_batt,
+							  val->intval);
+
+	default:
+		return -EINVAL;
+	}
+}
+
+static enum power_supply_property axp20x_battery_props[] = {
+	POWER_SUPPLY_PROP_PRESENT,
+	POWER_SUPPLY_PROP_ONLINE,
+	POWER_SUPPLY_PROP_STATUS,
+	POWER_SUPPLY_PROP_VOLTAGE_NOW,
+	POWER_SUPPLY_PROP_CURRENT_NOW,
+	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
+	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
+	POWER_SUPPLY_PROP_HEALTH,
+	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
+	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
+	POWER_SUPPLY_PROP_CAPACITY,
+};
+
+static int axp20x_battery_prop_writeable(struct power_supply *psy,
+					 enum power_supply_property psp)
+{
+	return psp == POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN ||
+	       psp == POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN ||
+	       psp == POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT;
+}
+
+static const struct power_supply_desc axp20x_batt_ps_desc = {
+	.name = "axp20x-battery",
+	.type = POWER_SUPPLY_TYPE_BATTERY,
+	.properties = axp20x_battery_props,
+	.num_properties = ARRAY_SIZE(axp20x_battery_props),
+	.property_is_writeable = axp20x_battery_prop_writeable,
+	.get_property = axp20x_battery_get_prop,
+	.set_property = axp20x_battery_set_prop,
+};
+
+static const struct of_device_id axp20x_battery_ps_id[] = {
+	{
+		.compatible = "x-powers,axp209-battery-power-supply",
+		.data = (void *)AXP209_ID,
+	}, {
+		.compatible = "x-powers,axp221-battery-power-supply",
+		.data = (void *)AXP221_ID,
+	}, { /* sentinel */ },
+};
+MODULE_DEVICE_TABLE(of, axp20x_battery_ps_id);
+
+static int axp20x_power_probe(struct platform_device *pdev)
+{
+	struct axp20x_batt_ps *axp20x_batt;
+	struct power_supply_config psy_cfg = {};
+	struct power_supply_battery_info info;
+
+	if (!of_device_is_available(pdev->dev.of_node))
+		return -ENODEV;
+
+	axp20x_batt = devm_kzalloc(&pdev->dev, sizeof(*axp20x_batt),
+				   GFP_KERNEL);
+	if (!axp20x_batt)
+		return -ENOMEM;
+
+	axp20x_batt->batt_v = devm_iio_channel_get(&pdev->dev, "batt_v");
+	if (IS_ERR(axp20x_batt->batt_v)) {
+		if (PTR_ERR(axp20x_batt->batt_v) == -ENODEV)
+			return -EPROBE_DEFER;
+		return PTR_ERR(axp20x_batt->batt_v);
+	}
+
+	axp20x_batt->batt_chrg_i = devm_iio_channel_get(&pdev->dev,
+							"batt_chrg_i");
+	if (IS_ERR(axp20x_batt->batt_chrg_i)) {
+		if (PTR_ERR(axp20x_batt->batt_chrg_i) == -ENODEV)
+			return -EPROBE_DEFER;
+		return PTR_ERR(axp20x_batt->batt_chrg_i);
+	}
+
+	axp20x_batt->batt_dischrg_i = devm_iio_channel_get(&pdev->dev,
+							   "batt_dischrg_i");
+	if (IS_ERR(axp20x_batt->batt_dischrg_i)) {
+		if (PTR_ERR(axp20x_batt->batt_dischrg_i) == -ENODEV)
+			return -EPROBE_DEFER;
+		return PTR_ERR(axp20x_batt->batt_dischrg_i);
+	}
+
+	axp20x_batt->regmap = dev_get_regmap(pdev->dev.parent, NULL);
+	platform_set_drvdata(pdev, axp20x_batt);
+
+	psy_cfg.drv_data = axp20x_batt;
+	psy_cfg.of_node = pdev->dev.of_node;
+
+	axp20x_batt->axp_id = (uintptr_t)of_device_get_match_data(&pdev->dev);
+
+	axp20x_batt->batt = devm_power_supply_register(&pdev->dev,
+						       &axp20x_batt_ps_desc,
+						       &psy_cfg);
+	if (IS_ERR(axp20x_batt->batt)) {
+		dev_err(&pdev->dev, "failed to register power supply: %ld\n",
+			PTR_ERR(axp20x_batt->batt));
+		return PTR_ERR(axp20x_batt->batt);
+	}
+
+	if (!power_supply_get_battery_info(axp20x_batt->batt, &info)) {
+		int vmin = info.voltage_min_design_uv;
+		int ccc = info.constant_charge_current_ua;
+
+		if (vmin > 0 && axp20x_set_voltage_min_design(axp20x_batt,
+							      vmin))
+			dev_err(&pdev->dev,
+				"couldn't set voltage_min_design\n");
+
+		if (ccc > 0 && axp20x_set_constant_charge_current(axp20x_batt,
+								  ccc))
+			dev_err(&pdev->dev,
+				"couldn't set constant charge current\n");
+	}
+
+	return 0;
+}
+
+static struct platform_driver axp20x_batt_driver = {
+	.probe    = axp20x_power_probe,
+	.driver   = {
+		.name  = "axp20x-battery-power-supply",
+		.of_match_table = axp20x_battery_ps_id,
+	},
+};
+
+module_platform_driver(axp20x_batt_driver);
+
+MODULE_DESCRIPTION("Battery power supply driver for AXP20X and AXP22X PMICs");
+MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
+MODULE_LICENSE("GPL");
-- 
2.9.3


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 13/18] power: supply: add battery driver for AXP20X and AXP22X PMICs
@ 2017-02-14  9:41   ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: linux-arm-kernel

The X-Powers AXP20X and AXP22X PMICs can have a battery as power supply.

This patch adds the battery power supply driver to get various data from
the PMIC, such as the battery status (charging, discharging, full,
dead), current max limit, current current, battery capacity (in
percentage), voltage max and min limits, current voltage and battery
capacity (in Ah).

This battery driver uses the AXP20X/AXP22X ADC driver as PMIC data
provider.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Jonathan Cameron <jic23@kernel.org>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
---

v3:
 - added axp20x_set_voltage_min_design function so it can be reused,
 - used power_supply_get_battery_info for setting constant charge current
 instead of x-powers,constant-charge-current introduced in v2,
 - used power_supply_get_battery_info for setting voltage min design of
 the battery,

v2:
 - changed BIT(x) to 1 << x when describing bits purpose for which 2 <<
 x or 3 << x exists, to be consistent,
 - switched from POWER_SUPPLY_PROP_CURRENT_MAX to
 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
 - added POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX to the list of
 readable properties,
 - replaced ? character by a common u for micro units to make checkpatch
 happy,
 - factorized code in axp20x_battery_set_max_voltage,
 - added a axp20x_set_constant_charge_current function to be used when
 setting the value from sysfs and from the DT,
 - removed some dead code,
 - added a DT property to set constant current charge of the battery
 (x-powers,constant-charge-current),
 - migrated to dev_get_regmap instead of manually looking for the regmap
 in the drvdata of the parent,
 - switched from int to uintptr_t cast to make sure the cast is always
 for the same size type (make build on 64bits platforms happy mainly),
 drivers/power/supply/Kconfig          |  12 +
 drivers/power/supply/Makefile         |   1 +
 drivers/power/supply/axp20x_battery.c | 492 ++++++++++++++++++++++++++++++++++
 3 files changed, 505 insertions(+)
 create mode 100644 drivers/power/supply/axp20x_battery.c

diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig
index c552b4b..48619de 100644
--- a/drivers/power/supply/Kconfig
+++ b/drivers/power/supply/Kconfig
@@ -226,6 +226,18 @@ config CHARGER_AXP20X
 	  This driver can also be built as a module. If so, the module will be
 	  called axp20x_ac_power.
 
+config BATTERY_AXP20X
+	tristate "X-Powers AXP20X battery driver"
+	depends on MFD_AXP20X
+	depends on AXP20X_ADC
+	depends on IIO
+	help
+	  Say Y here to enable support for X-Powers AXP20X PMICs' battery power
+	  supply.
+
+	  This driver can also be built as a module. If so, the module will be
+	  called axp20x_battery.
+
 config AXP288_CHARGER
 	tristate "X-Powers AXP288 Charger"
 	depends on MFD_AXP20X && EXTCON_AXP288
diff --git a/drivers/power/supply/Makefile b/drivers/power/supply/Makefile
index 7d22417..5a217b2 100644
--- a/drivers/power/supply/Makefile
+++ b/drivers/power/supply/Makefile
@@ -18,6 +18,7 @@ obj-$(CONFIG_TEST_POWER)	+= test_power.o
 
 obj-$(CONFIG_BATTERY_88PM860X)	+= 88pm860x_battery.o
 obj-$(CONFIG_BATTERY_ACT8945A)	+= act8945a_charger.o
+obj-$(CONFIG_BATTERY_AXP20X)	+= axp20x_battery.o
 obj-$(CONFIG_CHARGER_AXP20X)	+= axp20x_ac_power.o
 obj-$(CONFIG_BATTERY_DS2760)	+= ds2760_battery.o
 obj-$(CONFIG_BATTERY_DS2780)	+= ds2780_battery.o
diff --git a/drivers/power/supply/axp20x_battery.c b/drivers/power/supply/axp20x_battery.c
new file mode 100644
index 0000000..bd16ac6
--- /dev/null
+++ b/drivers/power/supply/axp20x_battery.c
@@ -0,0 +1,492 @@
+/*
+ * Battery power supply driver for X-Powers AXP20X and AXP22X PMICs
+ *
+ * Copyright 2016 Free Electrons NextThing Co.
+ *	Quentin Schulz <quentin.schulz@free-electrons.com>
+ *
+ * This driver is based on a previous upstreaming attempt by:
+ *	Bruno Pr?mont <bonbons@linux-vserver.org>
+ *
+ * This file is subject to the terms and conditions of the GNU General
+ * Public License. See the file "COPYING" in the main directory of this
+ * archive for more details.
+ *
+ * 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.
+ */
+
+#include <linux/err.h>
+#include <linux/interrupt.h>
+#include <linux/irq.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_device.h>
+#include <linux/platform_device.h>
+#include <linux/power_supply.h>
+#include <linux/regmap.h>
+#include <linux/slab.h>
+#include <linux/time.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/consumer.h>
+#include <linux/mfd/axp20x.h>
+
+#define AXP20X_PWR_STATUS_BAT_CHARGING	BIT(2)
+
+#define AXP20X_PWR_OP_BATT_PRESENT	BIT(5)
+#define AXP20X_PWR_OP_BATT_ACTIVATED	BIT(3)
+
+#define AXP209_FG_PERCENT		GENMASK(6, 0)
+#define AXP22X_FG_VALID			BIT(7)
+
+#define AXP20X_CHRG_CTRL1_TGT_VOLT	GENMASK(6, 5)
+#define AXP20X_CHRG_CTRL1_TGT_4_1V	(0 << 5)
+#define AXP20X_CHRG_CTRL1_TGT_4_15V	(1 << 5)
+#define AXP20X_CHRG_CTRL1_TGT_4_2V	(2 << 5)
+#define AXP20X_CHRG_CTRL1_TGT_4_36V	(3 << 5)
+
+#define AXP22X_CHRG_CTRL1_TGT_4_22V	(1 << 5)
+#define AXP22X_CHRG_CTRL1_TGT_4_24V	(3 << 5)
+
+#define AXP20X_CHRG_CTRL1_TGT_CURR	GENMASK(3, 0)
+
+#define AXP20X_V_OFF_MASK		GENMASK(2, 0)
+
+struct axp20x_batt_ps {
+	struct regmap *regmap;
+	struct power_supply *batt;
+	struct axp20x_dev *axp20x;
+	struct iio_channel *batt_chrg_i;
+	struct iio_channel *batt_dischrg_i;
+	struct iio_channel *batt_v;
+	u8 axp_id;
+};
+
+static int axp20x_battery_get_max_voltage(struct axp20x_batt_ps *axp20x_batt,
+					  int *val)
+{
+	int ret, reg;
+
+	ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, &reg);
+	if (ret)
+		return ret;
+
+	switch (reg & AXP20X_CHRG_CTRL1_TGT_VOLT) {
+	case AXP20X_CHRG_CTRL1_TGT_4_1V:
+		*val = 4100000;
+		break;
+	case AXP20X_CHRG_CTRL1_TGT_4_15V:
+		*val = 4150000;
+		break;
+	case AXP20X_CHRG_CTRL1_TGT_4_2V:
+		*val = 4200000;
+		break;
+	case AXP20X_CHRG_CTRL1_TGT_4_36V:
+		*val = 4360000;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int axp22x_battery_get_max_voltage(struct axp20x_batt_ps *axp20x_batt,
+					  int *val)
+{
+	int ret, reg;
+
+	ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, &reg);
+	if (ret)
+		return ret;
+
+	switch (reg & AXP20X_CHRG_CTRL1_TGT_VOLT) {
+	case AXP20X_CHRG_CTRL1_TGT_4_1V:
+		*val = 4100000;
+		break;
+	case AXP20X_CHRG_CTRL1_TGT_4_2V:
+		*val = 4200000;
+		break;
+	case AXP22X_CHRG_CTRL1_TGT_4_22V:
+		*val = 4220000;
+		break;
+	case AXP22X_CHRG_CTRL1_TGT_4_24V:
+		*val = 4240000;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int axp20x_battery_get_prop(struct power_supply *psy,
+				   enum power_supply_property psp,
+				   union power_supply_propval *val)
+{
+	struct axp20x_batt_ps *axp20x_batt = power_supply_get_drvdata(psy);
+	struct iio_channel *chan;
+	int ret = 0, reg, val1;
+
+	switch (psp) {
+	case POWER_SUPPLY_PROP_PRESENT:
+	case POWER_SUPPLY_PROP_ONLINE:
+		ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
+				  &reg);
+		if (ret)
+			return ret;
+
+		val->intval = !!(reg & AXP20X_PWR_OP_BATT_PRESENT);
+		break;
+
+	case POWER_SUPPLY_PROP_STATUS:
+		ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_INPUT_STATUS,
+				  &reg);
+		if (ret)
+			return ret;
+
+		if (reg & AXP20X_PWR_STATUS_BAT_CHARGING) {
+			val->intval = POWER_SUPPLY_STATUS_CHARGING;
+			return 0;
+		}
+
+		ret = iio_read_channel_processed(axp20x_batt->batt_dischrg_i,
+						 &val1);
+		if (ret)
+			return ret;
+
+		if (val1) {
+			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
+			return 0;
+		}
+
+		ret = regmap_read(axp20x_batt->regmap, AXP20X_FG_RES, &val1);
+		if (ret)
+			return ret;
+
+		/*
+		 * Fuel Gauge data takes 7 bits but the stored value seems to be
+		 * directly the raw percentage without any scaling to 7 bits.
+		 */
+		if ((val1 & AXP209_FG_PERCENT) == 100)
+			val->intval = POWER_SUPPLY_STATUS_FULL;
+		else
+			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
+		break;
+
+	case POWER_SUPPLY_PROP_HEALTH:
+		ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
+				  &val1);
+		if (ret)
+			return ret;
+
+		if (val1 & AXP20X_PWR_OP_BATT_ACTIVATED) {
+			val->intval = POWER_SUPPLY_HEALTH_DEAD;
+			return 0;
+		}
+
+		val->intval = POWER_SUPPLY_HEALTH_GOOD;
+		break;
+
+	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
+		ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, &reg);
+		if (ret)
+			return ret;
+
+		reg &= AXP20X_CHRG_CTRL1_TGT_CURR;
+		val->intval = reg * 100000 + 300000;
+		break;
+
+	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
+		val->intval = AXP20X_CHRG_CTRL1_TGT_CURR * 100000 + 300000;
+		break;
+
+	case POWER_SUPPLY_PROP_CURRENT_NOW:
+		ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_INPUT_STATUS,
+				  &reg);
+		if (ret)
+			return ret;
+
+		if (reg & AXP20X_PWR_STATUS_BAT_CHARGING)
+			chan = axp20x_batt->batt_chrg_i;
+		else
+			chan = axp20x_batt->batt_dischrg_i;
+
+		ret = iio_read_channel_processed(chan, &val->intval);
+		if (ret)
+			return ret;
+
+		/* IIO framework gives mA but Power Supply framework gives uA */
+		val->intval *= 1000;
+		break;
+
+	case POWER_SUPPLY_PROP_CAPACITY:
+		/* When no battery is present, return capacity is 100% */
+		ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
+				  &reg);
+		if (ret)
+			return ret;
+
+		if (!(reg & AXP20X_PWR_OP_BATT_PRESENT)) {
+			val->intval = 100;
+			return 0;
+		}
+
+		ret = regmap_read(axp20x_batt->regmap, AXP20X_FG_RES, &reg);
+		if (ret)
+			return ret;
+
+		if (axp20x_batt->axp_id == AXP221_ID &&
+		    !(reg & AXP22X_FG_VALID))
+			return -EINVAL;
+
+		/*
+		 * Fuel Gauge data takes 7 bits but the stored value seems to be
+		 * directly the raw percentage without any scaling to 7 bits.
+		 */
+		val->intval = reg & AXP209_FG_PERCENT;
+		break;
+
+	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
+		if (axp20x_batt->axp_id == AXP209_ID)
+			return axp20x_battery_get_max_voltage(axp20x_batt,
+							      &val->intval);
+		return axp22x_battery_get_max_voltage(axp20x_batt,
+						      &val->intval);
+
+	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
+		ret = regmap_read(axp20x_batt->regmap, AXP20X_V_OFF, &reg);
+		if (ret)
+			return ret;
+
+		val->intval = 2600000 + 100000 * (reg & AXP20X_V_OFF_MASK);
+		break;
+
+	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
+		ret = iio_read_channel_processed(axp20x_batt->batt_v,
+						 &val->intval);
+		if (ret)
+			return ret;
+
+		/* IIO framework gives mV but Power Supply framework gives uV */
+		val->intval *= 1000;
+		break;
+
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int axp20x_battery_set_max_voltage(struct axp20x_batt_ps *axp20x_batt,
+					  int val)
+{
+	switch (val) {
+	case 4100000:
+		val = AXP20X_CHRG_CTRL1_TGT_4_1V;
+		break;
+
+	case 4150000:
+		if (axp20x_batt->axp_id == AXP221_ID)
+			return -EINVAL;
+
+		val = AXP20X_CHRG_CTRL1_TGT_4_15V;
+		break;
+
+	case 4200000:
+		val = AXP20X_CHRG_CTRL1_TGT_4_2V;
+		break;
+
+	default:
+		/*
+		 * AXP20x max voltage can be set to 4.36V and AXP22X max voltage
+		 * can be set to 4.22V and 4.24V, but these voltages are too
+		 * high for Lithium based batteries (AXP PMICs are supposed to
+		 * be used with these kinds of battery).
+		 */
+		return -EINVAL;
+	}
+
+	return regmap_update_bits(axp20x_batt->regmap, AXP20X_CHRG_CTRL1,
+				  AXP20X_CHRG_CTRL1_TGT_VOLT, val);
+}
+
+static int axp20x_set_constant_charge_current(struct axp20x_batt_ps *axp_batt,
+					      int charge_current)
+{
+	if (axp_batt->axp_id == AXP209_ID)
+		charge_current = (charge_current - 300000) / 100000;
+	else
+		charge_current = (charge_current - 300000) / 150000;
+
+	if (charge_current > AXP20X_CHRG_CTRL1_TGT_CURR || charge_current < 0)
+		return -EINVAL;
+
+	return regmap_update_bits(axp_batt->regmap, AXP20X_CHRG_CTRL1,
+				  AXP20X_CHRG_CTRL1_TGT_CURR, charge_current);
+}
+
+static int axp20x_set_voltage_min_design(struct axp20x_batt_ps *axp_batt,
+					 int min_voltage)
+{
+	int val1 = (min_voltage - 2600000) / 100000;
+
+	if (val1 < 0 || val1 > AXP20X_V_OFF_MASK)
+		return -EINVAL;
+
+	return regmap_update_bits(axp_batt->regmap, AXP20X_V_OFF,
+				  AXP20X_V_OFF_MASK, val1);
+}
+
+static int axp20x_battery_set_prop(struct power_supply *psy,
+				   enum power_supply_property psp,
+				   const union power_supply_propval *val)
+{
+	struct axp20x_batt_ps *axp20x_batt = power_supply_get_drvdata(psy);
+
+	switch (psp) {
+	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
+		return axp20x_set_voltage_min_design(axp20x_batt, val->intval);
+
+	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
+		return axp20x_battery_set_max_voltage(axp20x_batt, val->intval);
+
+	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
+		return axp20x_set_constant_charge_current(axp20x_batt,
+							  val->intval);
+
+	default:
+		return -EINVAL;
+	}
+}
+
+static enum power_supply_property axp20x_battery_props[] = {
+	POWER_SUPPLY_PROP_PRESENT,
+	POWER_SUPPLY_PROP_ONLINE,
+	POWER_SUPPLY_PROP_STATUS,
+	POWER_SUPPLY_PROP_VOLTAGE_NOW,
+	POWER_SUPPLY_PROP_CURRENT_NOW,
+	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
+	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
+	POWER_SUPPLY_PROP_HEALTH,
+	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
+	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
+	POWER_SUPPLY_PROP_CAPACITY,
+};
+
+static int axp20x_battery_prop_writeable(struct power_supply *psy,
+					 enum power_supply_property psp)
+{
+	return psp == POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN ||
+	       psp == POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN ||
+	       psp == POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT;
+}
+
+static const struct power_supply_desc axp20x_batt_ps_desc = {
+	.name = "axp20x-battery",
+	.type = POWER_SUPPLY_TYPE_BATTERY,
+	.properties = axp20x_battery_props,
+	.num_properties = ARRAY_SIZE(axp20x_battery_props),
+	.property_is_writeable = axp20x_battery_prop_writeable,
+	.get_property = axp20x_battery_get_prop,
+	.set_property = axp20x_battery_set_prop,
+};
+
+static const struct of_device_id axp20x_battery_ps_id[] = {
+	{
+		.compatible = "x-powers,axp209-battery-power-supply",
+		.data = (void *)AXP209_ID,
+	}, {
+		.compatible = "x-powers,axp221-battery-power-supply",
+		.data = (void *)AXP221_ID,
+	}, { /* sentinel */ },
+};
+MODULE_DEVICE_TABLE(of, axp20x_battery_ps_id);
+
+static int axp20x_power_probe(struct platform_device *pdev)
+{
+	struct axp20x_batt_ps *axp20x_batt;
+	struct power_supply_config psy_cfg = {};
+	struct power_supply_battery_info info;
+
+	if (!of_device_is_available(pdev->dev.of_node))
+		return -ENODEV;
+
+	axp20x_batt = devm_kzalloc(&pdev->dev, sizeof(*axp20x_batt),
+				   GFP_KERNEL);
+	if (!axp20x_batt)
+		return -ENOMEM;
+
+	axp20x_batt->batt_v = devm_iio_channel_get(&pdev->dev, "batt_v");
+	if (IS_ERR(axp20x_batt->batt_v)) {
+		if (PTR_ERR(axp20x_batt->batt_v) == -ENODEV)
+			return -EPROBE_DEFER;
+		return PTR_ERR(axp20x_batt->batt_v);
+	}
+
+	axp20x_batt->batt_chrg_i = devm_iio_channel_get(&pdev->dev,
+							"batt_chrg_i");
+	if (IS_ERR(axp20x_batt->batt_chrg_i)) {
+		if (PTR_ERR(axp20x_batt->batt_chrg_i) == -ENODEV)
+			return -EPROBE_DEFER;
+		return PTR_ERR(axp20x_batt->batt_chrg_i);
+	}
+
+	axp20x_batt->batt_dischrg_i = devm_iio_channel_get(&pdev->dev,
+							   "batt_dischrg_i");
+	if (IS_ERR(axp20x_batt->batt_dischrg_i)) {
+		if (PTR_ERR(axp20x_batt->batt_dischrg_i) == -ENODEV)
+			return -EPROBE_DEFER;
+		return PTR_ERR(axp20x_batt->batt_dischrg_i);
+	}
+
+	axp20x_batt->regmap = dev_get_regmap(pdev->dev.parent, NULL);
+	platform_set_drvdata(pdev, axp20x_batt);
+
+	psy_cfg.drv_data = axp20x_batt;
+	psy_cfg.of_node = pdev->dev.of_node;
+
+	axp20x_batt->axp_id = (uintptr_t)of_device_get_match_data(&pdev->dev);
+
+	axp20x_batt->batt = devm_power_supply_register(&pdev->dev,
+						       &axp20x_batt_ps_desc,
+						       &psy_cfg);
+	if (IS_ERR(axp20x_batt->batt)) {
+		dev_err(&pdev->dev, "failed to register power supply: %ld\n",
+			PTR_ERR(axp20x_batt->batt));
+		return PTR_ERR(axp20x_batt->batt);
+	}
+
+	if (!power_supply_get_battery_info(axp20x_batt->batt, &info)) {
+		int vmin = info.voltage_min_design_uv;
+		int ccc = info.constant_charge_current_ua;
+
+		if (vmin > 0 && axp20x_set_voltage_min_design(axp20x_batt,
+							      vmin))
+			dev_err(&pdev->dev,
+				"couldn't set voltage_min_design\n");
+
+		if (ccc > 0 && axp20x_set_constant_charge_current(axp20x_batt,
+								  ccc))
+			dev_err(&pdev->dev,
+				"couldn't set constant charge current\n");
+	}
+
+	return 0;
+}
+
+static struct platform_driver axp20x_batt_driver = {
+	.probe    = axp20x_power_probe,
+	.driver   = {
+		.name  = "axp20x-battery-power-supply",
+		.of_match_table = axp20x_battery_ps_id,
+	},
+};
+
+module_platform_driver(axp20x_batt_driver);
+
+MODULE_DESCRIPTION("Battery power supply driver for AXP20X and AXP22X PMICs");
+MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
+MODULE_LICENSE("GPL");
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 14/18] mfd: axp20x: add MFD cells for AXP20X and AXP22X battery driver
  2017-02-14  9:40 ` Quentin Schulz
  (?)
@ 2017-02-14  9:41   ` Quentin Schulz
  -1 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: Quentin Schulz, linux-iio, devicetree, linux-kernel, linux-pm,
	linux-arm-kernel, thomas.petazzoni, icenowy, bonbons

The X-Powers AXP20X and AXP22X PMICs can have a battery as power supply.

This patch adds the AXP20X/AXP22X battery driver to the MFD cells of the
AXP209, AXP221 and AXP223 MFD.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-for-MFD-by: Lee Jones <lee.jones@linaro.org>
---
 drivers/mfd/axp20x.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/drivers/mfd/axp20x.c b/drivers/mfd/axp20x.c
index 86bc1d5..b75d5c5 100644
--- a/drivers/mfd/axp20x.c
+++ b/drivers/mfd/axp20x.c
@@ -585,6 +585,9 @@ static struct mfd_cell axp20x_cells[] = {
 	}, {
 		.name		= "axp20x-adc",
 	}, {
+		.name		= "axp20x-battery-power-supply",
+		.of_compatible	= "x-powers,axp209-battery-power-supply",
+	}, {
 		.name		= "axp20x-ac-power-supply",
 		.of_compatible	= "x-powers,axp202-ac-power-supply",
 		.num_resources	= ARRAY_SIZE(axp20x_ac_power_supply_resources),
@@ -612,6 +615,9 @@ static struct mfd_cell axp221_cells[] = {
 		.num_resources	= ARRAY_SIZE(axp20x_ac_power_supply_resources),
 		.resources	= axp20x_ac_power_supply_resources,
 	}, {
+		.name		= "axp20x-battery-power-supply",
+		.of_compatible	= "x-powers,axp221-battery-power-supply",
+	}, {
 		.name		= "axp20x-usb-power-supply",
 		.of_compatible	= "x-powers,axp221-usb-power-supply",
 		.num_resources	= ARRAY_SIZE(axp22x_usb_power_supply_resources),
@@ -627,6 +633,9 @@ static struct mfd_cell axp223_cells[] = {
 	}, {
 		.name		= "axp22x-adc",
 	}, {
+		.name		= "axp20x-battery-power-supply",
+		.of_compatible	= "x-powers,axp221-battery-power-supply",
+	}, {
 		.name			= "axp20x-regulator",
 	}, {
 		.name		= "axp20x-ac-power-supply",
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 14/18] mfd: axp20x: add MFD cells for AXP20X and AXP22X battery driver
@ 2017-02-14  9:41   ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: thomas.petazzoni, devicetree, linux-pm, linux-iio, linux-kernel,
	Quentin Schulz, bonbons, icenowy, linux-arm-kernel

The X-Powers AXP20X and AXP22X PMICs can have a battery as power supply.

This patch adds the AXP20X/AXP22X battery driver to the MFD cells of the
AXP209, AXP221 and AXP223 MFD.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-for-MFD-by: Lee Jones <lee.jones@linaro.org>
---
 drivers/mfd/axp20x.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/drivers/mfd/axp20x.c b/drivers/mfd/axp20x.c
index 86bc1d5..b75d5c5 100644
--- a/drivers/mfd/axp20x.c
+++ b/drivers/mfd/axp20x.c
@@ -585,6 +585,9 @@ static struct mfd_cell axp20x_cells[] = {
 	}, {
 		.name		= "axp20x-adc",
 	}, {
+		.name		= "axp20x-battery-power-supply",
+		.of_compatible	= "x-powers,axp209-battery-power-supply",
+	}, {
 		.name		= "axp20x-ac-power-supply",
 		.of_compatible	= "x-powers,axp202-ac-power-supply",
 		.num_resources	= ARRAY_SIZE(axp20x_ac_power_supply_resources),
@@ -612,6 +615,9 @@ static struct mfd_cell axp221_cells[] = {
 		.num_resources	= ARRAY_SIZE(axp20x_ac_power_supply_resources),
 		.resources	= axp20x_ac_power_supply_resources,
 	}, {
+		.name		= "axp20x-battery-power-supply",
+		.of_compatible	= "x-powers,axp221-battery-power-supply",
+	}, {
 		.name		= "axp20x-usb-power-supply",
 		.of_compatible	= "x-powers,axp221-usb-power-supply",
 		.num_resources	= ARRAY_SIZE(axp22x_usb_power_supply_resources),
@@ -627,6 +633,9 @@ static struct mfd_cell axp223_cells[] = {
 	}, {
 		.name		= "axp22x-adc",
 	}, {
+		.name		= "axp20x-battery-power-supply",
+		.of_compatible	= "x-powers,axp221-battery-power-supply",
+	}, {
 		.name			= "axp20x-regulator",
 	}, {
 		.name		= "axp20x-ac-power-supply",
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 14/18] mfd: axp20x: add MFD cells for AXP20X and AXP22X battery driver
@ 2017-02-14  9:41   ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: linux-arm-kernel

The X-Powers AXP20X and AXP22X PMICs can have a battery as power supply.

This patch adds the AXP20X/AXP22X battery driver to the MFD cells of the
AXP209, AXP221 and AXP223 MFD.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-for-MFD-by: Lee Jones <lee.jones@linaro.org>
---
 drivers/mfd/axp20x.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/drivers/mfd/axp20x.c b/drivers/mfd/axp20x.c
index 86bc1d5..b75d5c5 100644
--- a/drivers/mfd/axp20x.c
+++ b/drivers/mfd/axp20x.c
@@ -585,6 +585,9 @@ static struct mfd_cell axp20x_cells[] = {
 	}, {
 		.name		= "axp20x-adc",
 	}, {
+		.name		= "axp20x-battery-power-supply",
+		.of_compatible	= "x-powers,axp209-battery-power-supply",
+	}, {
 		.name		= "axp20x-ac-power-supply",
 		.of_compatible	= "x-powers,axp202-ac-power-supply",
 		.num_resources	= ARRAY_SIZE(axp20x_ac_power_supply_resources),
@@ -612,6 +615,9 @@ static struct mfd_cell axp221_cells[] = {
 		.num_resources	= ARRAY_SIZE(axp20x_ac_power_supply_resources),
 		.resources	= axp20x_ac_power_supply_resources,
 	}, {
+		.name		= "axp20x-battery-power-supply",
+		.of_compatible	= "x-powers,axp221-battery-power-supply",
+	}, {
 		.name		= "axp20x-usb-power-supply",
 		.of_compatible	= "x-powers,axp221-usb-power-supply",
 		.num_resources	= ARRAY_SIZE(axp22x_usb_power_supply_resources),
@@ -627,6 +633,9 @@ static struct mfd_cell axp223_cells[] = {
 	}, {
 		.name		= "axp22x-adc",
 	}, {
+		.name		= "axp20x-battery-power-supply",
+		.of_compatible	= "x-powers,axp221-battery-power-supply",
+	}, {
 		.name			= "axp20x-regulator",
 	}, {
 		.name		= "axp20x-ac-power-supply",
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 15/18] ARM: dtsi: axp209: add battery power supply subnode
  2017-02-14  9:40 ` Quentin Schulz
  (?)
@ 2017-02-14  9:41   ` Quentin Schulz
  -1 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: Quentin Schulz, linux-iio, devicetree, linux-kernel, linux-pm,
	linux-arm-kernel, thomas.petazzoni, icenowy, bonbons

The X-Powers AXP209 PMIC exposes battery supply various data such as
the battery status (charging, discharging, full, dead), current max
limit, current current, battery capacity (in percentage), voltage max
and min limits, current voltage, and battery capacity (in Ah).

This adds the battery power supply subnode for AXP20X PMIC.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
---

v2:
 - changed DT node name from battery_power_supply to
 battery-power-supply,
 - removed io-channels and io-channel-names from DT (the IIO mapping is
 done in the IIO ADC driver now),

 arch/arm/boot/dts/axp209.dtsi | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/arch/arm/boot/dts/axp209.dtsi b/arch/arm/boot/dts/axp209.dtsi
index 9677dd5..3c8fa26 100644
--- a/arch/arm/boot/dts/axp209.dtsi
+++ b/arch/arm/boot/dts/axp209.dtsi
@@ -64,6 +64,11 @@
 		#gpio-cells = <2>;
 	};
 
+	battery_power_supply: battery-power-supply {
+		compatible = "x-powers,axp209-battery-power-supply";
+		status = "disabled";
+	};
+
 	regulators {
 		/* Default work frequency for buck regulators */
 		x-powers,dcdc-freq = <1500>;
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 15/18] ARM: dtsi: axp209: add battery power supply subnode
@ 2017-02-14  9:41   ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: thomas.petazzoni, devicetree, linux-pm, linux-iio, linux-kernel,
	Quentin Schulz, bonbons, icenowy, linux-arm-kernel

The X-Powers AXP209 PMIC exposes battery supply various data such as
the battery status (charging, discharging, full, dead), current max
limit, current current, battery capacity (in percentage), voltage max
and min limits, current voltage, and battery capacity (in Ah).

This adds the battery power supply subnode for AXP20X PMIC.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
---

v2:
 - changed DT node name from battery_power_supply to
 battery-power-supply,
 - removed io-channels and io-channel-names from DT (the IIO mapping is
 done in the IIO ADC driver now),

 arch/arm/boot/dts/axp209.dtsi | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/arch/arm/boot/dts/axp209.dtsi b/arch/arm/boot/dts/axp209.dtsi
index 9677dd5..3c8fa26 100644
--- a/arch/arm/boot/dts/axp209.dtsi
+++ b/arch/arm/boot/dts/axp209.dtsi
@@ -64,6 +64,11 @@
 		#gpio-cells = <2>;
 	};
 
+	battery_power_supply: battery-power-supply {
+		compatible = "x-powers,axp209-battery-power-supply";
+		status = "disabled";
+	};
+
 	regulators {
 		/* Default work frequency for buck regulators */
 		x-powers,dcdc-freq = <1500>;
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 15/18] ARM: dtsi: axp209: add battery power supply subnode
@ 2017-02-14  9:41   ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: linux-arm-kernel

The X-Powers AXP209 PMIC exposes battery supply various data such as
the battery status (charging, discharging, full, dead), current max
limit, current current, battery capacity (in percentage), voltage max
and min limits, current voltage, and battery capacity (in Ah).

This adds the battery power supply subnode for AXP20X PMIC.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
---

v2:
 - changed DT node name from battery_power_supply to
 battery-power-supply,
 - removed io-channels and io-channel-names from DT (the IIO mapping is
 done in the IIO ADC driver now),

 arch/arm/boot/dts/axp209.dtsi | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/arch/arm/boot/dts/axp209.dtsi b/arch/arm/boot/dts/axp209.dtsi
index 9677dd5..3c8fa26 100644
--- a/arch/arm/boot/dts/axp209.dtsi
+++ b/arch/arm/boot/dts/axp209.dtsi
@@ -64,6 +64,11 @@
 		#gpio-cells = <2>;
 	};
 
+	battery_power_supply: battery-power-supply {
+		compatible = "x-powers,axp209-battery-power-supply";
+		status = "disabled";
+	};
+
 	regulators {
 		/* Default work frequency for buck regulators */
 		x-powers,dcdc-freq = <1500>;
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 16/18] ARM: dtsi: axp22x: add battery power supply subnode
  2017-02-14  9:40 ` Quentin Schulz
  (?)
@ 2017-02-14  9:41   ` Quentin Schulz
  -1 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: Quentin Schulz, linux-iio, devicetree, linux-kernel, linux-pm,
	linux-arm-kernel, thomas.petazzoni, icenowy, bonbons

The X-Powers AXP22X PMIC exposes battery supply various data such as
the battery status (charging, discharging, full, dead), current max
limit, current current, battery capacity (in percentage), voltage max
limit, current voltage, and battery capacity (in Ah).

This adds the battery power supply subnode for AXP22X PMIC.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
---

v2:
 - changed DT node name from battery_power_supply to
 battery-power-supply,
 - removed io-channels and io-channel-names from DT (the IIO mapping is
 done in the IIO ADC driver now),

 arch/arm/boot/dts/axp22x.dtsi | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/arch/arm/boot/dts/axp22x.dtsi b/arch/arm/boot/dts/axp22x.dtsi
index 67331c5..87fb08e 100644
--- a/arch/arm/boot/dts/axp22x.dtsi
+++ b/arch/arm/boot/dts/axp22x.dtsi
@@ -57,6 +57,11 @@
 		status = "disabled";
 	};
 
+	battery_power_supply: battery-power-supply {
+		compatible = "x-powers,axp221-battery-power-supply";
+		status = "disabled";
+	};
+
 	regulators {
 		/* Default work frequency for buck regulators */
 		x-powers,dcdc-freq = <3000>;
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 16/18] ARM: dtsi: axp22x: add battery power supply subnode
@ 2017-02-14  9:41   ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: thomas.petazzoni, devicetree, linux-pm, linux-iio, linux-kernel,
	Quentin Schulz, bonbons, icenowy, linux-arm-kernel

The X-Powers AXP22X PMIC exposes battery supply various data such as
the battery status (charging, discharging, full, dead), current max
limit, current current, battery capacity (in percentage), voltage max
limit, current voltage, and battery capacity (in Ah).

This adds the battery power supply subnode for AXP22X PMIC.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
---

v2:
 - changed DT node name from battery_power_supply to
 battery-power-supply,
 - removed io-channels and io-channel-names from DT (the IIO mapping is
 done in the IIO ADC driver now),

 arch/arm/boot/dts/axp22x.dtsi | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/arch/arm/boot/dts/axp22x.dtsi b/arch/arm/boot/dts/axp22x.dtsi
index 67331c5..87fb08e 100644
--- a/arch/arm/boot/dts/axp22x.dtsi
+++ b/arch/arm/boot/dts/axp22x.dtsi
@@ -57,6 +57,11 @@
 		status = "disabled";
 	};
 
+	battery_power_supply: battery-power-supply {
+		compatible = "x-powers,axp221-battery-power-supply";
+		status = "disabled";
+	};
+
 	regulators {
 		/* Default work frequency for buck regulators */
 		x-powers,dcdc-freq = <3000>;
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 16/18] ARM: dtsi: axp22x: add battery power supply subnode
@ 2017-02-14  9:41   ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: linux-arm-kernel

The X-Powers AXP22X PMIC exposes battery supply various data such as
the battery status (charging, discharging, full, dead), current max
limit, current current, battery capacity (in percentage), voltage max
limit, current voltage, and battery capacity (in Ah).

This adds the battery power supply subnode for AXP22X PMIC.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
---

v2:
 - changed DT node name from battery_power_supply to
 battery-power-supply,
 - removed io-channels and io-channel-names from DT (the IIO mapping is
 done in the IIO ADC driver now),

 arch/arm/boot/dts/axp22x.dtsi | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/arch/arm/boot/dts/axp22x.dtsi b/arch/arm/boot/dts/axp22x.dtsi
index 67331c5..87fb08e 100644
--- a/arch/arm/boot/dts/axp22x.dtsi
+++ b/arch/arm/boot/dts/axp22x.dtsi
@@ -57,6 +57,11 @@
 		status = "disabled";
 	};
 
+	battery_power_supply: battery-power-supply {
+		compatible = "x-powers,axp221-battery-power-supply";
+		status = "disabled";
+	};
+
 	regulators {
 		/* Default work frequency for buck regulators */
 		x-powers,dcdc-freq = <3000>;
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 17/18] ARM: dts: sun8i: sina33: enable battery power supply subnode
  2017-02-14  9:40 ` Quentin Schulz
  (?)
@ 2017-02-14  9:41   ` Quentin Schulz
  -1 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: Quentin Schulz, linux-iio, devicetree, linux-kernel, linux-pm,
	linux-arm-kernel, thomas.petazzoni, icenowy, bonbons

The Sinlinx SinA33 has an AXP223 PMIC and a battery connector, thus, we
enable the battery power supply subnode in its Device Tree.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
---
 arch/arm/boot/dts/sun8i-a33-sinlinx-sina33.dts | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/arch/arm/boot/dts/sun8i-a33-sinlinx-sina33.dts b/arch/arm/boot/dts/sun8i-a33-sinlinx-sina33.dts
index bf53408..2fe9299 100644
--- a/arch/arm/boot/dts/sun8i-a33-sinlinx-sina33.dts
+++ b/arch/arm/boot/dts/sun8i-a33-sinlinx-sina33.dts
@@ -151,6 +151,10 @@
 	status = "okay";
 };
 
+&battery_power_supply {
+	status = "okay";
+};
+
 &reg_aldo1 {
 	regulator-always-on;
 	regulator-min-microvolt = <3000000>;
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 17/18] ARM: dts: sun8i: sina33: enable battery power supply subnode
@ 2017-02-14  9:41   ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: thomas.petazzoni, devicetree, linux-pm, linux-iio, linux-kernel,
	Quentin Schulz, bonbons, icenowy, linux-arm-kernel

The Sinlinx SinA33 has an AXP223 PMIC and a battery connector, thus, we
enable the battery power supply subnode in its Device Tree.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
---
 arch/arm/boot/dts/sun8i-a33-sinlinx-sina33.dts | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/arch/arm/boot/dts/sun8i-a33-sinlinx-sina33.dts b/arch/arm/boot/dts/sun8i-a33-sinlinx-sina33.dts
index bf53408..2fe9299 100644
--- a/arch/arm/boot/dts/sun8i-a33-sinlinx-sina33.dts
+++ b/arch/arm/boot/dts/sun8i-a33-sinlinx-sina33.dts
@@ -151,6 +151,10 @@
 	status = "okay";
 };
 
+&battery_power_supply {
+	status = "okay";
+};
+
 &reg_aldo1 {
 	regulator-always-on;
 	regulator-min-microvolt = <3000000>;
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 17/18] ARM: dts: sun8i: sina33: enable battery power supply subnode
@ 2017-02-14  9:41   ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: linux-arm-kernel

The Sinlinx SinA33 has an AXP223 PMIC and a battery connector, thus, we
enable the battery power supply subnode in its Device Tree.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
---
 arch/arm/boot/dts/sun8i-a33-sinlinx-sina33.dts | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/arch/arm/boot/dts/sun8i-a33-sinlinx-sina33.dts b/arch/arm/boot/dts/sun8i-a33-sinlinx-sina33.dts
index bf53408..2fe9299 100644
--- a/arch/arm/boot/dts/sun8i-a33-sinlinx-sina33.dts
+++ b/arch/arm/boot/dts/sun8i-a33-sinlinx-sina33.dts
@@ -151,6 +151,10 @@
 	status = "okay";
 };
 
+&battery_power_supply {
+	status = "okay";
+};
+
 &reg_aldo1 {
 	regulator-always-on;
 	regulator-min-microvolt = <3000000>;
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 18/18] ARM: sun5i: chip: enable battery power supply subnode
  2017-02-14  9:40 ` Quentin Schulz
  (?)
@ 2017-02-14  9:41   ` Quentin Schulz
  -1 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: Quentin Schulz, linux-iio, devicetree, linux-kernel, linux-pm,
	linux-arm-kernel, thomas.petazzoni, icenowy, bonbons

The NextThing Co. CHIP has an AXP209 PMIC with battery connector.

This enables the battery power supply subnode.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
---
 arch/arm/boot/dts/sun5i-r8-chip.dts | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/arch/arm/boot/dts/sun5i-r8-chip.dts b/arch/arm/boot/dts/sun5i-r8-chip.dts
index 6011757..d4332b1 100644
--- a/arch/arm/boot/dts/sun5i-r8-chip.dts
+++ b/arch/arm/boot/dts/sun5i-r8-chip.dts
@@ -132,6 +132,10 @@
 	status = "okay";
 };
 
+&battery_power_supply {
+	status = "okay";
+};
+
 &i2c1 {
 	pinctrl-names = "default";
 	pinctrl-0 = <&i2c1_pins_a>;
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 18/18] ARM: sun5i: chip: enable battery power supply subnode
@ 2017-02-14  9:41   ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: thomas.petazzoni, devicetree, linux-pm, linux-iio, linux-kernel,
	Quentin Schulz, bonbons, icenowy, linux-arm-kernel

The NextThing Co. CHIP has an AXP209 PMIC with battery connector.

This enables the battery power supply subnode.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
---
 arch/arm/boot/dts/sun5i-r8-chip.dts | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/arch/arm/boot/dts/sun5i-r8-chip.dts b/arch/arm/boot/dts/sun5i-r8-chip.dts
index 6011757..d4332b1 100644
--- a/arch/arm/boot/dts/sun5i-r8-chip.dts
+++ b/arch/arm/boot/dts/sun5i-r8-chip.dts
@@ -132,6 +132,10 @@
 	status = "okay";
 };
 
+&battery_power_supply {
+	status = "okay";
+};
+
 &i2c1 {
 	pinctrl-names = "default";
 	pinctrl-0 = <&i2c1_pins_a>;
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* [PATCH v3 18/18] ARM: sun5i: chip: enable battery power supply subnode
@ 2017-02-14  9:41   ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-14  9:41 UTC (permalink / raw)
  To: linux-arm-kernel

The NextThing Co. CHIP has an AXP209 PMIC with battery connector.

This enables the battery power supply subnode.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
---
 arch/arm/boot/dts/sun5i-r8-chip.dts | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/arch/arm/boot/dts/sun5i-r8-chip.dts b/arch/arm/boot/dts/sun5i-r8-chip.dts
index 6011757..d4332b1 100644
--- a/arch/arm/boot/dts/sun5i-r8-chip.dts
+++ b/arch/arm/boot/dts/sun5i-r8-chip.dts
@@ -132,6 +132,10 @@
 	status = "okay";
 };
 
+&battery_power_supply {
+	status = "okay";
+};
+
 &i2c1 {
 	pinctrl-names = "default";
 	pinctrl-0 = <&i2c1_pins_a>;
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 01/18] dt-bindings: power: battery: add constant-charge-current property
  2017-02-14  9:40   ` Quentin Schulz
  (?)
@ 2017-02-15  0:46     ` Liam Breck
  -1 siblings, 0 replies; 126+ messages in thread
From: Liam Breck @ 2017-02-15  0:46 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: Quentin Schulz, linux-iio, devicetree, linux-kernel, linux-pm,
	linux-arm-kernel, thomas.petazzoni, icenowy, bonbons


On Tue, 14 Feb 2017 10:40:55 +0100 Quentin Schulz wrote:
> This adds the constant-charge-current property to the list of optional
> properties of the battery.
>
> The constant charge current is critical for batteries as they can't
> handle all charge currents.
>
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> ---
>
> added in v3
>
>  Documentation/devicetree/bindings/power/supply/battery.txt | 2 ++

Is constant-charge-current dependent on the battery (e.g. capacity, nominal voltage, etc) or the 
system (charger chip, input current/voltage, etc)?

It belongs in Doc.../power/supply/battery.txt if it's a characteristic of the battery.

Note, this page asserts that constant-current charging applies to NiMH batteries:
http://power-topics.blogspot.com/2016/05/constant-voltage-constant-current.html

Related properties to be added to battery.txt near-future in a patchset for the BQ24190 
charger are as follows. These are not currently in enum power_supply_property, so the actual names 
are still to be decided.

precharge-current-microamp:
   maximum charge current during precharge phase (typically 20% of battery capacity)

termination-current-microamp (or endcharge-current):
   a charge cycle terminates when the battery voltage is above recharge threshold,
   and the current is below this setting (typically 10% of battery capacity)


~.~

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 01/18] dt-bindings: power: battery: add constant-charge-current property
@ 2017-02-15  0:46     ` Liam Breck
  0 siblings, 0 replies; 126+ messages in thread
From: Liam Breck @ 2017-02-15  0:46 UTC (permalink / raw)
  To: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones
  Cc: thomas.petazzoni, devicetree, linux-pm, linux-iio, linux-kernel,
	Quentin Schulz, bonbons, icenowy, linux-arm-kernel


On Tue, 14 Feb 2017 10:40:55 +0100 Quentin Schulz wrote:
> This adds the constant-charge-current property to the list of optional
> properties of the battery.
>
> The constant charge current is critical for batteries as they can't
> handle all charge currents.
>
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> ---
>
> added in v3
>
>  Documentation/devicetree/bindings/power/supply/battery.txt | 2 ++

Is constant-charge-current dependent on the battery (e.g. capacity, nominal voltage, etc) or the 
system (charger chip, input current/voltage, etc)?

It belongs in Doc.../power/supply/battery.txt if it's a characteristic of the battery.

Note, this page asserts that constant-current charging applies to NiMH batteries:
http://power-topics.blogspot.com/2016/05/constant-voltage-constant-current.html

Related properties to be added to battery.txt near-future in a patchset for the BQ24190 
charger are as follows. These are not currently in enum power_supply_property, so the actual names 
are still to be decided.

precharge-current-microamp:
   maximum charge current during precharge phase (typically 20% of battery capacity)

termination-current-microamp (or endcharge-current):
   a charge cycle terminates when the battery voltage is above recharge threshold,
   and the current is below this setting (typically 10% of battery capacity)


~.~

^ permalink raw reply	[flat|nested] 126+ messages in thread

* [PATCH v3 01/18] dt-bindings: power: battery: add constant-charge-current property
@ 2017-02-15  0:46     ` Liam Breck
  0 siblings, 0 replies; 126+ messages in thread
From: Liam Breck @ 2017-02-15  0:46 UTC (permalink / raw)
  To: linux-arm-kernel


On Tue, 14 Feb 2017 10:40:55 +0100 Quentin Schulz wrote:
> This adds the constant-charge-current property to the list of optional
> properties of the battery.
>
> The constant charge current is critical for batteries as they can't
> handle all charge currents.
>
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> ---
>
> added in v3
>
>  Documentation/devicetree/bindings/power/supply/battery.txt | 2 ++

Is constant-charge-current dependent on the battery (e.g. capacity, nominal voltage, etc) or the 
system (charger chip, input current/voltage, etc)?

It belongs in Doc.../power/supply/battery.txt if it's a characteristic of the battery.

Note, this page asserts that constant-current charging applies to NiMH batteries:
http://power-topics.blogspot.com/2016/05/constant-voltage-constant-current.html

Related properties to be added to battery.txt near-future in a patchset for the BQ24190 
charger are as follows. These are not currently in enum power_supply_property, so the actual names 
are still to be decided.

precharge-current-microamp:
   maximum charge current during precharge phase (typically 20% of battery capacity)

termination-current-microamp (or endcharge-current):
   a charge cycle terminates when the battery voltage is above recharge threshold,
   and the current is below this setting (typically 10% of battery capacity)


~.~

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 01/18] dt-bindings: power: battery: add constant-charge-current property
  2017-02-15  0:46     ` Liam Breck
@ 2017-02-15  8:53       ` Quentin Schulz
  -1 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-15  8:53 UTC (permalink / raw)
  To: Liam Breck, knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens,
	sre, linux, maxime.ripard, lee.jones
  Cc: thomas.petazzoni, devicetree, linux-pm, linux-iio, linux-kernel,
	bonbons, icenowy, linux-arm-kernel

Hi,

On 15/02/2017 01:46, Liam Breck wrote:
> 
> On Tue, 14 Feb 2017 10:40:55 +0100 Quentin Schulz wrote:
>> This adds the constant-charge-current property to the list of optional
>> properties of the battery.
>>
>> The constant charge current is critical for batteries as they can't
>> handle all charge currents.
>>
>> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
>> ---
>>
>> added in v3
>>
>>  Documentation/devicetree/bindings/power/supply/battery.txt | 2 ++
> 
> Is constant-charge-current dependent on the battery (e.g. capacity, nominal voltage, etc) or the 
> system (charger chip, input current/voltage, etc)?
> 
> It belongs in Doc.../power/supply/battery.txt if it's a characteristic of the battery.
> 
> Note, this page asserts that constant-current charging applies to NiMH batteries:
> http://power-topics.blogspot.com/2016/05/constant-voltage-constant-current.html
> 
> Related properties to be added to battery.txt near-future in a patchset for the BQ24190 
> charger are as follows. These are not currently in enum power_supply_property, so the actual names 
> are still to be decided.
> 
> precharge-current-microamp:
>    maximum charge current during precharge phase (typically 20% of battery capacity)
> 
> termination-current-microamp (or endcharge-current):
>    a charge cycle terminates when the battery voltage is above recharge threshold,
>    and the current is below this setting (typically 10% of battery capacity)
> 

We have a client with a board whose battery accepts a maximum of 300mA
for charging. So depending on the battery, we cannot have any charging
current we want. The AXP PmMICs set constant charge current in a range
of 300mA-1800mA, so it is enforced by the charger but needs to be
adapted depending on the battery present in the system.

The AXP PMICs charge battery with constant current (Ichrg) between the
trickle voltage (Vtrkl which is ~3.0V) and the targeted voltage (Vtrgt;
which seems to be the voltage telling the battery is fully charged).

So if I understand correctly, "my" constant-charge-current would be
located in the charging cycle between your precharge-current-microamp
and the termination-current-microamp as it is the current for the
charging process as a whole.

See here[1] for the explanation in the datasheet (page 20).

That would definitely match what is explained in your link for constant
current.

[1] http://dl.linux-sunxi.org/AXP/AXP209_Datasheet_v1.0en.pdf

Let me know if something seems odd,
Thanks,
Quentin

-- 
Quentin Schulz, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

^ permalink raw reply	[flat|nested] 126+ messages in thread

* [PATCH v3 01/18] dt-bindings: power: battery: add constant-charge-current property
@ 2017-02-15  8:53       ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-15  8:53 UTC (permalink / raw)
  To: linux-arm-kernel

Hi,

On 15/02/2017 01:46, Liam Breck wrote:
> 
> On Tue, 14 Feb 2017 10:40:55 +0100 Quentin Schulz wrote:
>> This adds the constant-charge-current property to the list of optional
>> properties of the battery.
>>
>> The constant charge current is critical for batteries as they can't
>> handle all charge currents.
>>
>> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
>> ---
>>
>> added in v3
>>
>>  Documentation/devicetree/bindings/power/supply/battery.txt | 2 ++
> 
> Is constant-charge-current dependent on the battery (e.g. capacity, nominal voltage, etc) or the 
> system (charger chip, input current/voltage, etc)?
> 
> It belongs in Doc.../power/supply/battery.txt if it's a characteristic of the battery.
> 
> Note, this page asserts that constant-current charging applies to NiMH batteries:
> http://power-topics.blogspot.com/2016/05/constant-voltage-constant-current.html
> 
> Related properties to be added to battery.txt near-future in a patchset for the BQ24190 
> charger are as follows. These are not currently in enum power_supply_property, so the actual names 
> are still to be decided.
> 
> precharge-current-microamp:
>    maximum charge current during precharge phase (typically 20% of battery capacity)
> 
> termination-current-microamp (or endcharge-current):
>    a charge cycle terminates when the battery voltage is above recharge threshold,
>    and the current is below this setting (typically 10% of battery capacity)
> 

We have a client with a board whose battery accepts a maximum of 300mA
for charging. So depending on the battery, we cannot have any charging
current we want. The AXP PmMICs set constant charge current in a range
of 300mA-1800mA, so it is enforced by the charger but needs to be
adapted depending on the battery present in the system.

The AXP PMICs charge battery with constant current (Ichrg) between the
trickle voltage (Vtrkl which is ~3.0V) and the targeted voltage (Vtrgt;
which seems to be the voltage telling the battery is fully charged).

So if I understand correctly, "my" constant-charge-current would be
located in the charging cycle between your precharge-current-microamp
and the termination-current-microamp as it is the current for the
charging process as a whole.

See here[1] for the explanation in the datasheet (page 20).

That would definitely match what is explained in your link for constant
current.

[1] http://dl.linux-sunxi.org/AXP/AXP209_Datasheet_v1.0en.pdf

Let me know if something seems odd,
Thanks,
Quentin

-- 
Quentin Schulz, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 00/18] add support for AXP20X and AXP22X power supply drivers
  2017-02-14  9:40 ` Quentin Schulz
  (?)
@ 2017-02-15  9:36   ` Lee Jones
  -1 siblings, 0 replies; 126+ messages in thread
From: Lee Jones @ 2017-02-15  9:36 UTC (permalink / raw)
  To: Quentin Schulz
  Cc: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, linux-iio, devicetree, linux-kernel, linux-pm,
	linux-arm-kernel, thomas.petazzoni, icenowy, bonbons


[...]

> Quentin Schulz (18):
>   dt-bindings: power: battery: add constant-charge-current property
>   power: supply: power_supply_core: add constant-current-charge optional
>     property
>   mfd: axp20x: correct name of temperature data ADC registers
>   iio: adc: add support for X-Powers AXP20X and AXP22X PMICs ADCs
>   mfd: axp20x: add ADC cells for AXP20X and AXP22X PMICs
>   mfd: axp20x: add AC power supply cells for AXP22X PMICs
>   ARM: dtsi: axp209: add AC power supply subnode
>   ARM: dtsi: axp22x: add AC power supply subnode
>   ARM: dts: sun8i: sina33: enable ACIN power supply subnode
>   ARM: sun5i: chip: enable ACIN power supply subnode
>   dt-bindings: power: supply: add AXP20X/AXP22X battery DT binding
>   mfd: axp20x: add CHRG_CTRL1/2/3 to writeable regs for AXP20X/AXP22X
>   power: supply: add battery driver for AXP20X and AXP22X PMICs
>   mfd: axp20x: add MFD cells for AXP20X and AXP22X battery driver
>   ARM: dtsi: axp209: add battery power supply subnode
>   ARM: dtsi: axp22x: add battery power supply subnode
>   ARM: dts: sun8i: sina33: enable battery power supply subnode
>   ARM: sun5i: chip: enable battery power supply subnode

This set is still missing a bunch of Acks.

>  .../bindings/power/supply/axp20x_battery.txt       |  20 +
>  .../devicetree/bindings/power/supply/battery.txt   |   2 +
>  arch/arm/boot/dts/axp209.dtsi                      |  10 +
>  arch/arm/boot/dts/axp22x.dtsi                      |  10 +
>  arch/arm/boot/dts/sun5i-r8-chip.dts                |   8 +
>  arch/arm/boot/dts/sun8i-a33-sinlinx-sina33.dts     |   8 +
>  drivers/iio/adc/Kconfig                            |  10 +
>  drivers/iio/adc/Makefile                           |   1 +
>  drivers/iio/adc/axp20x_adc.c                       | 606 +++++++++++++++++++++
>  drivers/mfd/axp20x.c                               |  29 +-
>  drivers/power/supply/Kconfig                       |  12 +
>  drivers/power/supply/Makefile                      |   1 +
>  drivers/power/supply/axp20x_battery.c              | 492 +++++++++++++++++
>  drivers/power/supply/power_supply_core.c           |   3 +
>  include/linux/mfd/axp20x.h                         |   4 +-
>  include/linux/power_supply.h                       |   1 +
>  16 files changed, 1214 insertions(+), 3 deletions(-)
>  create mode 100644 Documentation/devicetree/bindings/power/supply/axp20x_battery.txt
>  create mode 100644 drivers/iio/adc/axp20x_adc.c
>  create mode 100644 drivers/power/supply/axp20x_battery.c
> 

-- 
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 00/18] add support for AXP20X and AXP22X power supply drivers
@ 2017-02-15  9:36   ` Lee Jones
  0 siblings, 0 replies; 126+ messages in thread
From: Lee Jones @ 2017-02-15  9:36 UTC (permalink / raw)
  To: Quentin Schulz
  Cc: mark.rutland, devicetree, lars, linux-pm, linux-iio,
	linux-kernel, sre, linux, wens, robh+dt, icenowy, pmeerw,
	knaack.h, maxime.ripard, bonbons, thomas.petazzoni,
	linux-arm-kernel


[...]

> Quentin Schulz (18):
>   dt-bindings: power: battery: add constant-charge-current property
>   power: supply: power_supply_core: add constant-current-charge optional
>     property
>   mfd: axp20x: correct name of temperature data ADC registers
>   iio: adc: add support for X-Powers AXP20X and AXP22X PMICs ADCs
>   mfd: axp20x: add ADC cells for AXP20X and AXP22X PMICs
>   mfd: axp20x: add AC power supply cells for AXP22X PMICs
>   ARM: dtsi: axp209: add AC power supply subnode
>   ARM: dtsi: axp22x: add AC power supply subnode
>   ARM: dts: sun8i: sina33: enable ACIN power supply subnode
>   ARM: sun5i: chip: enable ACIN power supply subnode
>   dt-bindings: power: supply: add AXP20X/AXP22X battery DT binding
>   mfd: axp20x: add CHRG_CTRL1/2/3 to writeable regs for AXP20X/AXP22X
>   power: supply: add battery driver for AXP20X and AXP22X PMICs
>   mfd: axp20x: add MFD cells for AXP20X and AXP22X battery driver
>   ARM: dtsi: axp209: add battery power supply subnode
>   ARM: dtsi: axp22x: add battery power supply subnode
>   ARM: dts: sun8i: sina33: enable battery power supply subnode
>   ARM: sun5i: chip: enable battery power supply subnode

This set is still missing a bunch of Acks.

>  .../bindings/power/supply/axp20x_battery.txt       |  20 +
>  .../devicetree/bindings/power/supply/battery.txt   |   2 +
>  arch/arm/boot/dts/axp209.dtsi                      |  10 +
>  arch/arm/boot/dts/axp22x.dtsi                      |  10 +
>  arch/arm/boot/dts/sun5i-r8-chip.dts                |   8 +
>  arch/arm/boot/dts/sun8i-a33-sinlinx-sina33.dts     |   8 +
>  drivers/iio/adc/Kconfig                            |  10 +
>  drivers/iio/adc/Makefile                           |   1 +
>  drivers/iio/adc/axp20x_adc.c                       | 606 +++++++++++++++++++++
>  drivers/mfd/axp20x.c                               |  29 +-
>  drivers/power/supply/Kconfig                       |  12 +
>  drivers/power/supply/Makefile                      |   1 +
>  drivers/power/supply/axp20x_battery.c              | 492 +++++++++++++++++
>  drivers/power/supply/power_supply_core.c           |   3 +
>  include/linux/mfd/axp20x.h                         |   4 +-
>  include/linux/power_supply.h                       |   1 +
>  16 files changed, 1214 insertions(+), 3 deletions(-)
>  create mode 100644 Documentation/devicetree/bindings/power/supply/axp20x_battery.txt
>  create mode 100644 drivers/iio/adc/axp20x_adc.c
>  create mode 100644 drivers/power/supply/axp20x_battery.c
> 

-- 
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 126+ messages in thread

* [PATCH v3 00/18] add support for AXP20X and AXP22X power supply drivers
@ 2017-02-15  9:36   ` Lee Jones
  0 siblings, 0 replies; 126+ messages in thread
From: Lee Jones @ 2017-02-15  9:36 UTC (permalink / raw)
  To: linux-arm-kernel


[...]

> Quentin Schulz (18):
>   dt-bindings: power: battery: add constant-charge-current property
>   power: supply: power_supply_core: add constant-current-charge optional
>     property
>   mfd: axp20x: correct name of temperature data ADC registers
>   iio: adc: add support for X-Powers AXP20X and AXP22X PMICs ADCs
>   mfd: axp20x: add ADC cells for AXP20X and AXP22X PMICs
>   mfd: axp20x: add AC power supply cells for AXP22X PMICs
>   ARM: dtsi: axp209: add AC power supply subnode
>   ARM: dtsi: axp22x: add AC power supply subnode
>   ARM: dts: sun8i: sina33: enable ACIN power supply subnode
>   ARM: sun5i: chip: enable ACIN power supply subnode
>   dt-bindings: power: supply: add AXP20X/AXP22X battery DT binding
>   mfd: axp20x: add CHRG_CTRL1/2/3 to writeable regs for AXP20X/AXP22X
>   power: supply: add battery driver for AXP20X and AXP22X PMICs
>   mfd: axp20x: add MFD cells for AXP20X and AXP22X battery driver
>   ARM: dtsi: axp209: add battery power supply subnode
>   ARM: dtsi: axp22x: add battery power supply subnode
>   ARM: dts: sun8i: sina33: enable battery power supply subnode
>   ARM: sun5i: chip: enable battery power supply subnode

This set is still missing a bunch of Acks.

>  .../bindings/power/supply/axp20x_battery.txt       |  20 +
>  .../devicetree/bindings/power/supply/battery.txt   |   2 +
>  arch/arm/boot/dts/axp209.dtsi                      |  10 +
>  arch/arm/boot/dts/axp22x.dtsi                      |  10 +
>  arch/arm/boot/dts/sun5i-r8-chip.dts                |   8 +
>  arch/arm/boot/dts/sun8i-a33-sinlinx-sina33.dts     |   8 +
>  drivers/iio/adc/Kconfig                            |  10 +
>  drivers/iio/adc/Makefile                           |   1 +
>  drivers/iio/adc/axp20x_adc.c                       | 606 +++++++++++++++++++++
>  drivers/mfd/axp20x.c                               |  29 +-
>  drivers/power/supply/Kconfig                       |  12 +
>  drivers/power/supply/Makefile                      |   1 +
>  drivers/power/supply/axp20x_battery.c              | 492 +++++++++++++++++
>  drivers/power/supply/power_supply_core.c           |   3 +
>  include/linux/mfd/axp20x.h                         |   4 +-
>  include/linux/power_supply.h                       |   1 +
>  16 files changed, 1214 insertions(+), 3 deletions(-)
>  create mode 100644 Documentation/devicetree/bindings/power/supply/axp20x_battery.txt
>  create mode 100644 drivers/iio/adc/axp20x_adc.c
>  create mode 100644 drivers/power/supply/axp20x_battery.c
> 

-- 
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org ? Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 05/18] mfd: axp20x: add ADC cells for AXP20X and AXP22X PMICs
  2017-02-14  9:40   ` Quentin Schulz
  (?)
@ 2017-02-15  9:37     ` Lee Jones
  -1 siblings, 0 replies; 126+ messages in thread
From: Lee Jones @ 2017-02-15  9:37 UTC (permalink / raw)
  To: Quentin Schulz
  Cc: knaack.h, lars, pmeerw, robh+dt, mark.rutland, wens, sre, linux,
	maxime.ripard, linux-iio, devicetree, linux-kernel, linux-pm,
	linux-arm-kernel, thomas.petazzoni, icenowy, bonbons

On Tue, 14 Feb 2017, Quentin Schulz wrote:

> This adds the AXP20X/AXP22x ADCs driver to the mfd cells of the AXP209,
> AXP221 and AXP223 MFD.
> 
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
> ---
> 
> v3:
>  - removed of_compatible as DT node has been removed,
>  - use different names to probe the ADC driver,
> 
>  drivers/mfd/axp20x.c | 6 ++++++
>  1 file changed, 6 insertions(+)

For my own reference:
  Acked-for-MFD-by: Lee Jones <lee.jones@linaro.org>

> diff --git a/drivers/mfd/axp20x.c b/drivers/mfd/axp20x.c
> index 9c2fd37..a44a2fe 100644
> --- a/drivers/mfd/axp20x.c
> +++ b/drivers/mfd/axp20x.c
> @@ -581,6 +581,8 @@ static struct mfd_cell axp20x_cells[] = {
>  	}, {
>  		.name		= "axp20x-regulator",
>  	}, {
> +		.name		= "axp20x-adc",
> +	}, {
>  		.name		= "axp20x-ac-power-supply",
>  		.of_compatible	= "x-powers,axp202-ac-power-supply",
>  		.num_resources	= ARRAY_SIZE(axp20x_ac_power_supply_resources),
> @@ -601,6 +603,8 @@ static struct mfd_cell axp221_cells[] = {
>  	}, {
>  		.name		= "axp20x-regulator",
>  	}, {
> +		.name		= "axp22x-adc"
> +	}, {
>  		.name		= "axp20x-usb-power-supply",
>  		.of_compatible	= "x-powers,axp221-usb-power-supply",
>  		.num_resources	= ARRAY_SIZE(axp22x_usb_power_supply_resources),
> @@ -614,6 +618,8 @@ static struct mfd_cell axp223_cells[] = {
>  		.num_resources		= ARRAY_SIZE(axp22x_pek_resources),
>  		.resources		= axp22x_pek_resources,
>  	}, {
> +		.name		= "axp22x-adc",
> +	}, {
>  		.name			= "axp20x-regulator",
>  	}, {
>  		.name		= "axp20x-usb-power-supply",

-- 
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 05/18] mfd: axp20x: add ADC cells for AXP20X and AXP22X PMICs
@ 2017-02-15  9:37     ` Lee Jones
  0 siblings, 0 replies; 126+ messages in thread
From: Lee Jones @ 2017-02-15  9:37 UTC (permalink / raw)
  To: Quentin Schulz
  Cc: mark.rutland, devicetree, lars, linux-pm, linux-iio,
	linux-kernel, sre, linux, wens, robh+dt, icenowy, pmeerw,
	knaack.h, maxime.ripard, bonbons, thomas.petazzoni,
	linux-arm-kernel

On Tue, 14 Feb 2017, Quentin Schulz wrote:

> This adds the AXP20X/AXP22x ADCs driver to the mfd cells of the AXP209,
> AXP221 and AXP223 MFD.
> 
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
> ---
> 
> v3:
>  - removed of_compatible as DT node has been removed,
>  - use different names to probe the ADC driver,
> 
>  drivers/mfd/axp20x.c | 6 ++++++
>  1 file changed, 6 insertions(+)

For my own reference:
  Acked-for-MFD-by: Lee Jones <lee.jones@linaro.org>

> diff --git a/drivers/mfd/axp20x.c b/drivers/mfd/axp20x.c
> index 9c2fd37..a44a2fe 100644
> --- a/drivers/mfd/axp20x.c
> +++ b/drivers/mfd/axp20x.c
> @@ -581,6 +581,8 @@ static struct mfd_cell axp20x_cells[] = {
>  	}, {
>  		.name		= "axp20x-regulator",
>  	}, {
> +		.name		= "axp20x-adc",
> +	}, {
>  		.name		= "axp20x-ac-power-supply",
>  		.of_compatible	= "x-powers,axp202-ac-power-supply",
>  		.num_resources	= ARRAY_SIZE(axp20x_ac_power_supply_resources),
> @@ -601,6 +603,8 @@ static struct mfd_cell axp221_cells[] = {
>  	}, {
>  		.name		= "axp20x-regulator",
>  	}, {
> +		.name		= "axp22x-adc"
> +	}, {
>  		.name		= "axp20x-usb-power-supply",
>  		.of_compatible	= "x-powers,axp221-usb-power-supply",
>  		.num_resources	= ARRAY_SIZE(axp22x_usb_power_supply_resources),
> @@ -614,6 +618,8 @@ static struct mfd_cell axp223_cells[] = {
>  		.num_resources		= ARRAY_SIZE(axp22x_pek_resources),
>  		.resources		= axp22x_pek_resources,
>  	}, {
> +		.name		= "axp22x-adc",
> +	}, {
>  		.name			= "axp20x-regulator",
>  	}, {
>  		.name		= "axp20x-usb-power-supply",

-- 
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 126+ messages in thread

* [PATCH v3 05/18] mfd: axp20x: add ADC cells for AXP20X and AXP22X PMICs
@ 2017-02-15  9:37     ` Lee Jones
  0 siblings, 0 replies; 126+ messages in thread
From: Lee Jones @ 2017-02-15  9:37 UTC (permalink / raw)
  To: linux-arm-kernel

On Tue, 14 Feb 2017, Quentin Schulz wrote:

> This adds the AXP20X/AXP22x ADCs driver to the mfd cells of the AXP209,
> AXP221 and AXP223 MFD.
> 
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
> ---
> 
> v3:
>  - removed of_compatible as DT node has been removed,
>  - use different names to probe the ADC driver,
> 
>  drivers/mfd/axp20x.c | 6 ++++++
>  1 file changed, 6 insertions(+)

For my own reference:
  Acked-for-MFD-by: Lee Jones <lee.jones@linaro.org>

> diff --git a/drivers/mfd/axp20x.c b/drivers/mfd/axp20x.c
> index 9c2fd37..a44a2fe 100644
> --- a/drivers/mfd/axp20x.c
> +++ b/drivers/mfd/axp20x.c
> @@ -581,6 +581,8 @@ static struct mfd_cell axp20x_cells[] = {
>  	}, {
>  		.name		= "axp20x-regulator",
>  	}, {
> +		.name		= "axp20x-adc",
> +	}, {
>  		.name		= "axp20x-ac-power-supply",
>  		.of_compatible	= "x-powers,axp202-ac-power-supply",
>  		.num_resources	= ARRAY_SIZE(axp20x_ac_power_supply_resources),
> @@ -601,6 +603,8 @@ static struct mfd_cell axp221_cells[] = {
>  	}, {
>  		.name		= "axp20x-regulator",
>  	}, {
> +		.name		= "axp22x-adc"
> +	}, {
>  		.name		= "axp20x-usb-power-supply",
>  		.of_compatible	= "x-powers,axp221-usb-power-supply",
>  		.num_resources	= ARRAY_SIZE(axp22x_usb_power_supply_resources),
> @@ -614,6 +618,8 @@ static struct mfd_cell axp223_cells[] = {
>  		.num_resources		= ARRAY_SIZE(axp22x_pek_resources),
>  		.resources		= axp22x_pek_resources,
>  	}, {
> +		.name		= "axp22x-adc",
> +	}, {
>  		.name			= "axp20x-regulator",
>  	}, {
>  		.name		= "axp20x-usb-power-supply",

-- 
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org ? Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 01/18] dt-bindings: power: battery: add constant-charge-current property
  2017-02-15  8:53       ` Quentin Schulz
  (?)
@ 2017-02-15 20:18         ` Liam Breck
  -1 siblings, 0 replies; 126+ messages in thread
From: Liam Breck @ 2017-02-15 20:18 UTC (permalink / raw)
  To: Quentin Schulz
  Cc: knaack.h, lars, Peter Meerwald-Stadler, robh+dt, Mark Rutland,
	Chen-Yu Tsai, Sebastian Reichel, linux, maxime.ripard, Lee Jones,
	thomas.petazzoni, devicetree, linux-pm, linux-iio, linux-kernel,
	bonbons, icenowy, linux-arm-kernel

On Wed, Feb 15, 2017 at 12:53 AM, Quentin Schulz
<quentin.schulz@free-electrons.com> wrote:
> Hi,
>
> On 15/02/2017 01:46, Liam Breck wrote:
>>
>> On Tue, 14 Feb 2017 10:40:55 +0100 Quentin Schulz wrote:
>>> This adds the constant-charge-current property to the list of optional
>>> properties of the battery.
>>>
>>> The constant charge current is critical for batteries as they can't
>>> handle all charge currents.
>>>
>>> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
>>> ---
>>>
>>> added in v3
>>>
>>>  Documentation/devicetree/bindings/power/supply/battery.txt | 2 ++
>>
>> Is constant-charge-current dependent on the battery (e.g. capacity, nominal voltage, etc) or the
>> system (charger chip, input current/voltage, etc)?
>>
>> It belongs in Doc.../power/supply/battery.txt if it's a characteristic of the battery.
>>
>> Note, this page asserts that constant-current charging applies to NiMH batteries:
>> http://power-topics.blogspot.com/2016/05/constant-voltage-constant-current.html
>>
>> Related properties to be added to battery.txt near-future in a patchset for the BQ24190
>> charger are as follows. These are not currently in enum power_supply_property, so the actual names
>> are still to be decided.
>>
>> precharge-current-microamp:
>>    maximum charge current during precharge phase (typically 20% of battery capacity)
>>
>> termination-current-microamp (or endcharge-current):
>>    a charge cycle terminates when the battery voltage is above recharge threshold,
>>    and the current is below this setting (typically 10% of battery capacity)
>>
>
> We have a client with a board whose battery accepts a maximum of 300mA
> for charging. So depending on the battery, we cannot have any charging
> current we want. The AXP PmMICs set constant charge current in a range
> of 300mA-1800mA, so it is enforced by the charger but needs to be
> adapted depending on the battery present in the system.
>
> The AXP PMICs charge battery with constant current (Ichrg) between the
> trickle voltage (Vtrkl which is ~3.0V) and the targeted voltage (Vtrgt;
> which seems to be the voltage telling the battery is fully charged).
>
> So if I understand correctly, "my" constant-charge-current would be
> located in the charging cycle between your precharge-current-microamp
> and the termination-current-microamp as it is the current for the
> charging process as a whole.
>
> See here[1] for the explanation in the datasheet (page 20).
>
> That would definitely match what is explained in your link for constant
> current.
>
> [1] http://dl.linux-sunxi.org/AXP/AXP209_Datasheet_v1.0en.pdf
>
> Let me know if something seems odd,

Sounds OK to me. I'm happy to ack this, after my patchset goes in.
I'll be posting v7 this weekend, with a minor addition to battery.txt.

BTW there's rather a lot of ppl and lists CC'd on this, none of which
are listed in the patch comment...

> Thanks,
> Quentin
>
> --
> Quentin Schulz, Free Electrons
> Embedded Linux and Kernel engineering
> http://free-electrons.com

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 01/18] dt-bindings: power: battery: add constant-charge-current property
@ 2017-02-15 20:18         ` Liam Breck
  0 siblings, 0 replies; 126+ messages in thread
From: Liam Breck @ 2017-02-15 20:18 UTC (permalink / raw)
  To: Quentin Schulz
  Cc: Mark Rutland, thomas.petazzoni, lars, devicetree, linux-iio,
	linux-kernel, linux-pm, Sebastian Reichel, linux, Chen-Yu Tsai,
	robh+dt, icenowy, Peter Meerwald-Stadler, knaack.h,
	maxime.ripard, bonbons, Lee Jones, linux-arm-kernel

On Wed, Feb 15, 2017 at 12:53 AM, Quentin Schulz
<quentin.schulz@free-electrons.com> wrote:
> Hi,
>
> On 15/02/2017 01:46, Liam Breck wrote:
>>
>> On Tue, 14 Feb 2017 10:40:55 +0100 Quentin Schulz wrote:
>>> This adds the constant-charge-current property to the list of optional
>>> properties of the battery.
>>>
>>> The constant charge current is critical for batteries as they can't
>>> handle all charge currents.
>>>
>>> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
>>> ---
>>>
>>> added in v3
>>>
>>>  Documentation/devicetree/bindings/power/supply/battery.txt | 2 ++
>>
>> Is constant-charge-current dependent on the battery (e.g. capacity, nominal voltage, etc) or the
>> system (charger chip, input current/voltage, etc)?
>>
>> It belongs in Doc.../power/supply/battery.txt if it's a characteristic of the battery.
>>
>> Note, this page asserts that constant-current charging applies to NiMH batteries:
>> http://power-topics.blogspot.com/2016/05/constant-voltage-constant-current.html
>>
>> Related properties to be added to battery.txt near-future in a patchset for the BQ24190
>> charger are as follows. These are not currently in enum power_supply_property, so the actual names
>> are still to be decided.
>>
>> precharge-current-microamp:
>>    maximum charge current during precharge phase (typically 20% of battery capacity)
>>
>> termination-current-microamp (or endcharge-current):
>>    a charge cycle terminates when the battery voltage is above recharge threshold,
>>    and the current is below this setting (typically 10% of battery capacity)
>>
>
> We have a client with a board whose battery accepts a maximum of 300mA
> for charging. So depending on the battery, we cannot have any charging
> current we want. The AXP PmMICs set constant charge current in a range
> of 300mA-1800mA, so it is enforced by the charger but needs to be
> adapted depending on the battery present in the system.
>
> The AXP PMICs charge battery with constant current (Ichrg) between the
> trickle voltage (Vtrkl which is ~3.0V) and the targeted voltage (Vtrgt;
> which seems to be the voltage telling the battery is fully charged).
>
> So if I understand correctly, "my" constant-charge-current would be
> located in the charging cycle between your precharge-current-microamp
> and the termination-current-microamp as it is the current for the
> charging process as a whole.
>
> See here[1] for the explanation in the datasheet (page 20).
>
> That would definitely match what is explained in your link for constant
> current.
>
> [1] http://dl.linux-sunxi.org/AXP/AXP209_Datasheet_v1.0en.pdf
>
> Let me know if something seems odd,

Sounds OK to me. I'm happy to ack this, after my patchset goes in.
I'll be posting v7 this weekend, with a minor addition to battery.txt.

BTW there's rather a lot of ppl and lists CC'd on this, none of which
are listed in the patch comment...

> Thanks,
> Quentin
>
> --
> Quentin Schulz, Free Electrons
> Embedded Linux and Kernel engineering
> http://free-electrons.com

^ permalink raw reply	[flat|nested] 126+ messages in thread

* [PATCH v3 01/18] dt-bindings: power: battery: add constant-charge-current property
@ 2017-02-15 20:18         ` Liam Breck
  0 siblings, 0 replies; 126+ messages in thread
From: Liam Breck @ 2017-02-15 20:18 UTC (permalink / raw)
  To: linux-arm-kernel

On Wed, Feb 15, 2017 at 12:53 AM, Quentin Schulz
<quentin.schulz@free-electrons.com> wrote:
> Hi,
>
> On 15/02/2017 01:46, Liam Breck wrote:
>>
>> On Tue, 14 Feb 2017 10:40:55 +0100 Quentin Schulz wrote:
>>> This adds the constant-charge-current property to the list of optional
>>> properties of the battery.
>>>
>>> The constant charge current is critical for batteries as they can't
>>> handle all charge currents.
>>>
>>> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
>>> ---
>>>
>>> added in v3
>>>
>>>  Documentation/devicetree/bindings/power/supply/battery.txt | 2 ++
>>
>> Is constant-charge-current dependent on the battery (e.g. capacity, nominal voltage, etc) or the
>> system (charger chip, input current/voltage, etc)?
>>
>> It belongs in Doc.../power/supply/battery.txt if it's a characteristic of the battery.
>>
>> Note, this page asserts that constant-current charging applies to NiMH batteries:
>> http://power-topics.blogspot.com/2016/05/constant-voltage-constant-current.html
>>
>> Related properties to be added to battery.txt near-future in a patchset for the BQ24190
>> charger are as follows. These are not currently in enum power_supply_property, so the actual names
>> are still to be decided.
>>
>> precharge-current-microamp:
>>    maximum charge current during precharge phase (typically 20% of battery capacity)
>>
>> termination-current-microamp (or endcharge-current):
>>    a charge cycle terminates when the battery voltage is above recharge threshold,
>>    and the current is below this setting (typically 10% of battery capacity)
>>
>
> We have a client with a board whose battery accepts a maximum of 300mA
> for charging. So depending on the battery, we cannot have any charging
> current we want. The AXP PmMICs set constant charge current in a range
> of 300mA-1800mA, so it is enforced by the charger but needs to be
> adapted depending on the battery present in the system.
>
> The AXP PMICs charge battery with constant current (Ichrg) between the
> trickle voltage (Vtrkl which is ~3.0V) and the targeted voltage (Vtrgt;
> which seems to be the voltage telling the battery is fully charged).
>
> So if I understand correctly, "my" constant-charge-current would be
> located in the charging cycle between your precharge-current-microamp
> and the termination-current-microamp as it is the current for the
> charging process as a whole.
>
> See here[1] for the explanation in the datasheet (page 20).
>
> That would definitely match what is explained in your link for constant
> current.
>
> [1] http://dl.linux-sunxi.org/AXP/AXP209_Datasheet_v1.0en.pdf
>
> Let me know if something seems odd,

Sounds OK to me. I'm happy to ack this, after my patchset goes in.
I'll be posting v7 this weekend, with a minor addition to battery.txt.

BTW there's rather a lot of ppl and lists CC'd on this, none of which
are listed in the patch comment...

> Thanks,
> Quentin
>
> --
> Quentin Schulz, Free Electrons
> Embedded Linux and Kernel engineering
> http://free-electrons.com

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 04/18] iio: adc: add support for X-Powers AXP20X and AXP22X PMICs ADCs
  2017-02-14  9:40   ` Quentin Schulz
  (?)
@ 2017-02-19 12:40     ` Jonathan Cameron
  -1 siblings, 0 replies; 126+ messages in thread
From: Jonathan Cameron @ 2017-02-19 12:40 UTC (permalink / raw)
  To: Quentin Schulz, knaack.h, lars, pmeerw, robh+dt, mark.rutland,
	wens, sre, linux, maxime.ripard, lee.jones
  Cc: linux-iio, devicetree, linux-kernel, linux-pm, linux-arm-kernel,
	thomas.petazzoni, icenowy, bonbons

On 14/02/17 09:40, Quentin Schulz wrote:
> The X-Powers AXP20X and AXP22X PMICs have multiple ADCs. They expose the
> battery voltage, battery charge and discharge currents, AC-in and VBUS
> voltages and currents, 2 GPIOs muxable in ADC mode and PMIC temperature.
> 
> This adds support for most of AXP20X and AXP22X ADCs.
> 
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
Hi Quentin,

A few bits and bobs inline.  The bigest one is that I don't think
you need to carry your struct axp_data pointer around in the iio_priv
structure as it only seems to be used in probe.

Anyhow, tidy these little bits up (quite a few are optional in the
sense that they are a matter or personal taste and I don't feel
strongly about them) and you can add

Reviewed-by: Jonathan Cameron <jic23@kernel.org>

A nice driver.

Jonathan
> ---
> 
> v3:
>  - moved from switch to if condition in axp20x_adc_raw and
>  axp22x_adc_raw,
>  - removed DT support as DT node has been dropped,
>    - use of platform_device_id
>  - correctly defined the name of the iio device (name used to probe the
>  driver),
>  - added goto for errors in probe,
>  - added iio_map_array_unregister to the remove function,
> 
> v2:
>  - removed unused defines,
>  - changed BIT(x) to 1 << x when describing bits purpose for which 2 <<
>  x or 3 << x exists, to be consistent,
>  - changed ADC rate defines to macro formulas,
>  - reordered IIO channels, now different measures (current/voltage) of
>  the same part of the PMIC (e.g. battery), have the same IIO channel in
>  their respective IIO type. When a part of the PMIC have only one
>  measure, a number is jumped,
>  - left IIO channel mapping in DT to use iio_map structure,
>  - removed indexing of ADC internal temperature,
>  - removed unused iio_dev structure in axp20x_adc_iio,
>  - added a structure for data specific to AXP20X or AXP22X PMICs instead
>  of using an ID and an if condition when needing to separate the
>  behaviour of both,
>  - added a comment on batt_chrg_i really being on 12bits rather than
>  what the Chinese datasheets say (13 bits),
>  - corrected the offset for AXP22X PMIC temperature,
>  - set the ADC rate to a value (100Hz) shared by the AXP20X and AXP22X,
>  - created macro formulas to compute the ADC rate for each,
>  - added a condition on presence of ADC_EN2 reg before setting/resetting
>  it,
>  - switched from devm_iio_device_unregister to the non-devm function
>  because of the need for a remove function,
>  - removed some dead code,
> 
>  drivers/iio/adc/Kconfig      |  10 +
>  drivers/iio/adc/Makefile     |   1 +
>  drivers/iio/adc/axp20x_adc.c | 606 +++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 617 insertions(+)
>  create mode 100644 drivers/iio/adc/axp20x_adc.c
> 
> diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
> index 9c8b558..ed17fe1 100644
> --- a/drivers/iio/adc/Kconfig
> +++ b/drivers/iio/adc/Kconfig
> @@ -154,6 +154,16 @@ config AT91_SAMA5D2_ADC
>  	  To compile this driver as a module, choose M here: the module will be
>  	  called at91-sama5d2_adc.
>  
> +config AXP20X_ADC
> +	tristate "X-Powers AXP20X and AXP22X ADC driver"
> +	depends on MFD_AXP20X
> +	help
> +	  Say yes here to have support for X-Powers power management IC (PMIC)
> +	  AXP20X and AXP22X ADC devices.
> +
> +	  To compile this driver as a module, choose M here: the module will be
> +	  called axp20x_adc.
> +
>  config AXP288_ADC
>  	tristate "X-Powers AXP288 ADC driver"
>  	depends on MFD_AXP20X
> diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile
> index d36c4be..f5c28a5 100644
> --- a/drivers/iio/adc/Makefile
> +++ b/drivers/iio/adc/Makefile
> @@ -16,6 +16,7 @@ obj-$(CONFIG_AD7887) += ad7887.o
>  obj-$(CONFIG_AD799X) += ad799x.o
>  obj-$(CONFIG_AT91_ADC) += at91_adc.o
>  obj-$(CONFIG_AT91_SAMA5D2_ADC) += at91-sama5d2_adc.o
> +obj-$(CONFIG_AXP20X_ADC) += axp20x_adc.o
>  obj-$(CONFIG_AXP288_ADC) += axp288_adc.o
>  obj-$(CONFIG_BCM_IPROC_ADC) += bcm_iproc_adc.o
>  obj-$(CONFIG_BERLIN2_ADC) += berlin2-adc.o
> diff --git a/drivers/iio/adc/axp20x_adc.c b/drivers/iio/adc/axp20x_adc.c
> new file mode 100644
> index 0000000..5ef6af8
> --- /dev/null
> +++ b/drivers/iio/adc/axp20x_adc.c
> @@ -0,0 +1,606 @@
> +/* ADC driver for AXP20X and AXP22X PMICs
> + *
> + * Copyright (c) 2016 Free Electrons NextThing Co.
> + *	Quentin Schulz <quentin.schulz@free-electrons.com>
> + *
> + * This program is free software; you can redistribute it and/or modify it under
> + * the terms of the GNU General Public License version 2 as published by the
> + * Free Software Foundation.
> + */
> +
> +#include <linux/completion.h>
> +#include <linux/interrupt.h>
> +#include <linux/io.h>
> +#include <linux/module.h>
> +#include <linux/of.h>
> +#include <linux/of_device.h>
> +#include <linux/platform_device.h>
> +#include <linux/pm_runtime.h>
> +#include <linux/regmap.h>
> +#include <linux/thermal.h>
> +
> +#include <linux/iio/iio.h>
> +#include <linux/iio/driver.h>
> +#include <linux/iio/machine.h>
> +#include <linux/mfd/axp20x.h>
> +
> +#define AXP20X_ADC_EN1_MASK			GENMASK(7, 0)
> +
> +#define AXP20X_ADC_EN2_MASK			(GENMASK(3, 2) | BIT(7))
> +#define AXP22X_ADC_EN1_MASK			(GENMASK(7, 5) | BIT(0))
> +
> +#define AXP20X_GPIO10_IN_RANGE_GPIO0		BIT(0)
> +#define AXP20X_GPIO10_IN_RANGE_GPIO1		BIT(1)
> +#define AXP20X_GPIO10_IN_RANGE_GPIO0_VAL(x)	((x) & BIT(0))
> +#define AXP20X_GPIO10_IN_RANGE_GPIO1_VAL(x)	(((x) & BIT(0)) << 1)
> +
> +#define AXP20X_ADC_RATE_MASK			GENMASK(7, 6)
> +#define AXP20X_ADC_RATE_HZ(x)			((ilog2((x) / 25) << 6) & AXP20X_ADC_RATE_MASK)
> +#define AXP22X_ADC_RATE_HZ(x)			((ilog2((x) / 100) << 6) & AXP20X_ADC_RATE_MASK)
> +
> +#define AXP20X_ADC_CHANNEL(_channel, _name, _type, _reg)	\
> +	{							\
> +		.type = _type,					\
> +		.indexed = 1,					\
> +		.channel = _channel,				\
> +		.address = _reg,				\
> +		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |	\
> +				      BIT(IIO_CHAN_INFO_SCALE),	\
> +		.datasheet_name = _name,			\
> +	}
> +
> +#define AXP20X_ADC_CHANNEL_OFFSET(_channel, _name, _type, _reg) \
> +	{							\
> +		.type = _type,					\
> +		.indexed = 1,					\
> +		.channel = _channel,				\
> +		.address = _reg,				\
> +		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |	\
> +				      BIT(IIO_CHAN_INFO_SCALE) |\
> +				      BIT(IIO_CHAN_INFO_OFFSET),\
> +		.datasheet_name = _name,			\
> +	}
> +
> +struct axp_data;
> +
> +struct axp20x_adc_iio {
> +	struct regmap		*regmap;
I 'think' you only actually use data in probe.  As such it doesn't need
to be in here at all..

At that point you could just have the regmap pointer directly as your
private data and drop this structure entirely.  Whether you want to do
that though is up to you.
> +	struct axp_data		*data;
> +};
> +
> +enum axp20x_adc_channel_v {
> +	AXP20X_ACIN_V = 0,
> +	AXP20X_VBUS_V,
> +	AXP20X_TS_IN,
> +	AXP20X_GPIO0_V,
> +	AXP20X_GPIO1_V,
> +	AXP20X_IPSOUT_V,
> +	AXP20X_BATT_V,
> +};
> +
> +enum axp20x_adc_channel_i {
> +	AXP20X_ACIN_I = 0,
> +	AXP20X_VBUS_I,
> +	AXP20X_BATT_CHRG_I,
> +	AXP20X_BATT_DISCHRG_I,
> +};
> +
> +enum axp22x_adc_channel_v {
> +	AXP22X_TS_IN = 0,
> +	AXP22X_BATT_V,
> +};
> +
> +enum axp22x_adc_channel_i {
> +	AXP22X_BATT_CHRG_I = 1,
> +	AXP22X_BATT_DISCHRG_I,
> +};
> +
> +static struct iio_map axp20x_maps[] = {
> +	{
> +		.consumer_dev_name = "axp20x-usb-power-supply",
> +		.consumer_channel = "vbus_v",
> +		.adc_channel_label = "vbus_v",
> +	}, {
> +		.consumer_dev_name = "axp20x-usb-power-supply",
> +		.consumer_channel = "vbus_i",
> +		.adc_channel_label = "vbus_i",
> +	}, {
> +		.consumer_dev_name = "axp20x-ac-power-supply",
> +		.consumer_channel = "acin_v",
> +		.adc_channel_label = "acin_v",
> +	}, {
> +		.consumer_dev_name = "axp20x-ac-power-supply",
> +		.consumer_channel = "acin_i",
> +		.adc_channel_label = "acin_i",
> +	}, {
> +		.consumer_dev_name = "axp20x-battery-power-supply",
> +		.consumer_channel = "batt_v",
> +		.adc_channel_label = "batt_v",
> +	}, {
> +		.consumer_dev_name = "axp20x-battery-power-supply",
> +		.consumer_channel = "batt_chrg_i",
> +		.adc_channel_label = "batt_chrg_i",
> +	}, {
> +		.consumer_dev_name = "axp20x-battery-power-supply",
> +		.consumer_channel = "batt_dischrg_i",
> +		.adc_channel_label = "batt_dischrg_i",
> +	}, { /* sentinel */ }
> +};
> +
> +static struct iio_map axp22x_maps[] = {
> +	{
> +		.consumer_dev_name = "axp20x-battery-power-supply",
> +		.consumer_channel = "batt_v",
> +		.adc_channel_label = "batt_v",
> +	}, {
> +		.consumer_dev_name = "axp20x-battery-power-supply",
> +		.consumer_channel = "batt_chrg_i",
> +		.adc_channel_label = "batt_chrg_i",
> +	}, {
> +		.consumer_dev_name = "axp20x-battery-power-supply",
> +		.consumer_channel = "batt_dischrg_i",
> +		.adc_channel_label = "batt_dischrg_i",
> +	}, { /* sentinel */ }
> +};
> +
> +/*
> + * Channels are mapped by physical system. Their channels share the same index.
> + * i.e. acin_i is in_current0_raw and acin_v is in_voltage0_raw.
> + * The only exception is for the battery. batt_v will be in_voltage6_raw and
> + * charge current in_current6_raw and discharge current will be in_current7_raw.
> + */
> +static const struct iio_chan_spec axp20x_adc_channels[] = {
> +	AXP20X_ADC_CHANNEL(AXP20X_ACIN_V, "acin_v", IIO_VOLTAGE,
> +			   AXP20X_ACIN_V_ADC_H),
> +	AXP20X_ADC_CHANNEL(AXP20X_ACIN_I, "acin_i", IIO_CURRENT,
> +			   AXP20X_ACIN_I_ADC_H),
> +	AXP20X_ADC_CHANNEL(AXP20X_VBUS_V, "vbus_v", IIO_VOLTAGE,
> +			   AXP20X_VBUS_V_ADC_H),
> +	AXP20X_ADC_CHANNEL(AXP20X_VBUS_I, "vbus_i", IIO_CURRENT,
> +			   AXP20X_VBUS_I_ADC_H),
> +	{
> +		.type = IIO_TEMP,
> +		.address = AXP20X_TEMP_ADC_H,
> +		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
> +				      BIT(IIO_CHAN_INFO_SCALE) |
> +				      BIT(IIO_CHAN_INFO_OFFSET),
> +		.datasheet_name = "pmic_temp",
> +	},
> +	AXP20X_ADC_CHANNEL_OFFSET(AXP20X_GPIO0_V, "gpio0_v", IIO_VOLTAGE,
> +				  AXP20X_GPIO0_V_ADC_H),
> +	AXP20X_ADC_CHANNEL_OFFSET(AXP20X_GPIO1_V, "gpio1_v", IIO_VOLTAGE,
> +				  AXP20X_GPIO1_V_ADC_H),
> +	AXP20X_ADC_CHANNEL(AXP20X_IPSOUT_V, "ipsout_v", IIO_VOLTAGE,
> +			   AXP20X_IPSOUT_V_HIGH_H),
> +	AXP20X_ADC_CHANNEL(AXP20X_BATT_V, "batt_v", IIO_VOLTAGE,
> +			   AXP20X_BATT_V_H),
> +	AXP20X_ADC_CHANNEL(AXP20X_BATT_CHRG_I, "batt_chrg_i", IIO_CURRENT,
> +			   AXP20X_BATT_CHRG_I_H),
> +	AXP20X_ADC_CHANNEL(AXP20X_BATT_DISCHRG_I, "batt_dischrg_i", IIO_CURRENT,
> +			   AXP20X_BATT_DISCHRG_I_H),
> +};
> +
> +static const struct iio_chan_spec axp22x_adc_channels[] = {
> +	{
> +		.type = IIO_TEMP,
> +		.address = AXP22X_PMIC_TEMP_H,
> +		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
> +				      BIT(IIO_CHAN_INFO_SCALE) |
> +				      BIT(IIO_CHAN_INFO_OFFSET),
> +		.datasheet_name = "pmic_temp",
> +	},
> +	AXP20X_ADC_CHANNEL(AXP22X_BATT_V, "batt_v", IIO_VOLTAGE,
> +			   AXP20X_BATT_V_H),
> +	AXP20X_ADC_CHANNEL(AXP22X_BATT_CHRG_I, "batt_chrg_i", IIO_CURRENT,
> +			   AXP20X_BATT_CHRG_I_H),
> +	AXP20X_ADC_CHANNEL(AXP22X_BATT_DISCHRG_I, "batt_dischrg_i", IIO_CURRENT,
> +			   AXP20X_BATT_DISCHRG_I_H),
> +};
> +
> +static int axp20x_adc_raw(struct iio_dev *indio_dev,
> +			  struct iio_chan_spec const *chan, int *val)
> +{
> +	struct axp20x_adc_iio *info = iio_priv(indio_dev);
> +	int size = 12;
> +
> +	/*
> +	 *N.B.:  Unlike the Chinese datasheets tell, the charging current is
Space before the N
> +	 * stored on 12 bits, not 13 bits. Only discharging current is on 13
> +	 * bits.
> +	 */
> +	if (chan->type == IIO_CURRENT && chan->channel == AXP20X_BATT_DISCHRG_I)
> +		size = 13;
> +	else
> +		size = 12;
> +
> +	*val = axp20x_read_variable_width(info->regmap, chan->address, size);
> +	if (*val < 0)
> +		return *val;
> +
> +	return IIO_VAL_INT;
> +}
> +
> +static int axp22x_adc_raw(struct iio_dev *indio_dev,
> +			  struct iio_chan_spec const *chan, int *val)
> +{
> +	struct axp20x_adc_iio *info = iio_priv(indio_dev);
> +	int size;
> +
> +	/*
> +	 * N.B.: Unlike the Chinese datasheets tell, the charging current is
> +	 * stored on 12 bits, not 13 bits. Only discharging current is on 13
> +	 * bits.
> +	 */
> +	if (chan->type == IIO_CURRENT && chan->channel == AXP22X_BATT_DISCHRG_I)
> +		size = 13;
> +	else
> +		size = 12;
> +
> +	*val = axp20x_read_variable_width(info->regmap, chan->address, size);
> +	if (*val < 0)
> +		return *val;
> +
> +	return IIO_VAL_INT;
> +}
> +
> +static int axp20x_adc_scale_voltage(int channel, int *val, int *val2)
> +{
> +	switch (channel) {
> +	case AXP20X_ACIN_V:
> +	case AXP20X_VBUS_V:
> +		*val = 1;
> +		*val2 = 700000;
> +		return IIO_VAL_INT_PLUS_MICRO;
> +
> +	case AXP20X_GPIO0_V:
> +	case AXP20X_GPIO1_V:
> +		*val = 0;
> +		*val2 = 500000;
> +		return IIO_VAL_INT_PLUS_MICRO;
> +
> +	case AXP20X_BATT_V:
> +		*val = 1;
> +		*val2 = 100000;
> +		return IIO_VAL_INT_PLUS_MICRO;
> +
> +	case AXP20X_IPSOUT_V:
> +		*val = 1;
> +		*val2 = 400000;
> +		return IIO_VAL_INT_PLUS_MICRO;
> +
> +	default:
> +		return -EINVAL;
> +	}
> +}
> +
> +static int axp20x_adc_scale_current(int channel, int *val, int *val2)
> +{
> +	switch (channel) {
> +	case AXP20X_ACIN_I:
> +		*val = 0;
> +		*val2 = 625000;
> +		return IIO_VAL_INT_PLUS_MICRO;
> +
> +	case AXP20X_VBUS_I:
> +		*val = 0;
> +		*val2 = 375000;
> +		return IIO_VAL_INT_PLUS_MICRO;
> +
> +	case AXP20X_BATT_DISCHRG_I:
> +	case AXP20X_BATT_CHRG_I:
> +		*val = 0;
> +		*val2 = 500000;
> +		return IIO_VAL_INT_PLUS_MICRO;
> +
> +	default:
> +		return -EINVAL;
> +	}
> +}
> +
> +static int axp20x_adc_scale(struct iio_chan_spec const *chan, int *val,
> +			    int *val2)
> +{
> +	switch (chan->type) {
> +	case IIO_VOLTAGE:
> +		return axp20x_adc_scale_voltage(chan->channel, val, val2);
> +
> +	case IIO_CURRENT:
> +		return axp20x_adc_scale_current(chan->channel, val, val2);
> +
> +	case IIO_TEMP:
> +		*val = 100;
> +		return IIO_VAL_INT;
> +
> +	default:
> +		return -EINVAL;
> +	}
> +}
> +
> +static int axp22x_adc_scale(struct iio_chan_spec const *chan, int *val,
> +			    int *val2)
> +{
> +	switch (chan->type) {
> +	case IIO_VOLTAGE:
> +		if (chan->channel != AXP22X_BATT_V)
> +			return -EINVAL;
> +
> +		*val = 1;
> +		*val2 = 100000;
> +		return IIO_VAL_INT_PLUS_MICRO;
> +
> +	case IIO_CURRENT:
> +		*val = 0;
> +		*val2 = 500000;
> +		return IIO_VAL_INT_PLUS_MICRO;
> +
> +	case IIO_TEMP:
> +		*val = 100;
> +		return IIO_VAL_INT;
> +
> +	default:
> +		return -EINVAL;
> +	}
> +}
> +
> +static int axp20x_adc_offset_voltage(struct iio_dev *indio_dev, int channel,
> +				     int *val)
> +{
> +	struct axp20x_adc_iio *info = iio_priv(indio_dev);
> +	int ret, reg;
> +
> +	if (channel != AXP20X_GPIO0_V && channel != AXP20X_GPIO1_V)
> +		return -EINVAL;
Again, you could mess with the ordering here to only check the channel
value once. (i.e. use a switch after the register read). Might be ever
so slightly tidier.  Up to you.
> +
> +	ret = regmap_read(info->regmap, AXP20X_GPIO10_IN_RANGE, &reg);
> +	if (ret < 0)
> +		return ret;
> +
> +	if (channel == AXP20X_GPIO0_V)
> +		*val = reg & AXP20X_GPIO10_IN_RANGE_GPIO0;
> +	else
> +		*val = reg & AXP20X_GPIO10_IN_RANGE_GPIO1;
> +
> +	*val = !!(*val) * 700000;
> +
> +	return IIO_VAL_INT;
> +}
> +
> +static int axp20x_adc_offset(struct iio_dev *indio_dev,
> +			     struct iio_chan_spec const *chan, int *val)
> +{
> +	switch (chan->type) {
> +	case IIO_VOLTAGE:
> +		return axp20x_adc_offset_voltage(indio_dev, chan->channel, val);
> +
> +	case IIO_TEMP:
> +		*val = -1447;
> +		return IIO_VAL_INT;
> +
> +	default:
> +		return -EINVAL;
> +	}
> +}
> +
> +static int axp20x_read_raw(struct iio_dev *indio_dev,
> +			   struct iio_chan_spec const *chan, int *val,
> +			   int *val2, long mask)
> +{
> +	switch (mask) {
> +	case IIO_CHAN_INFO_OFFSET:
> +		return axp20x_adc_offset(indio_dev, chan, val);
> +
> +	case IIO_CHAN_INFO_SCALE:
> +		return axp20x_adc_scale(chan, val, val2);
> +
> +	case IIO_CHAN_INFO_RAW:
> +		return axp20x_adc_raw(indio_dev, chan, val);
> +
> +	default:
> +		return -EINVAL;
> +	}
> +}
> +
> +static int axp22x_read_raw(struct iio_dev *indio_dev,
> +			   struct iio_chan_spec const *chan, int *val,
> +			   int *val2, long mask)
> +{
> +	switch (mask) {
> +	case IIO_CHAN_INFO_OFFSET:
> +		*val = -2677;
> +		return IIO_VAL_INT;
> +
> +	case IIO_CHAN_INFO_SCALE:
> +		return axp22x_adc_scale(chan, val, val2);
> +
> +	case IIO_CHAN_INFO_RAW:
> +		return axp22x_adc_raw(indio_dev, chan, val);
> +
> +	default:
> +		return -EINVAL;
> +	}
> +}
> +
> +static int axp20x_write_raw(struct iio_dev *indio_dev,
> +			    struct iio_chan_spec const *chan, int val, int val2,
> +			    long mask)
> +{
> +	struct axp20x_adc_iio *info = iio_priv(indio_dev);
> +
> +	/*
> +	 * The AXP20X PMIC allows the user to choose between 0V and 0.7V offsets
> +	 * for (independently) GPIO0 and GPIO1 when in ADC mode.
> +	 */
> +	if (mask != IIO_CHAN_INFO_OFFSET)
> +		return -EINVAL;
> +
> +	if (chan->channel != AXP20X_GPIO0_V && chan->channel != AXP20X_GPIO1_V)
> +		return -EINVAL;
Might be a tiny bit nicer to move this down to where you are chosing between
the two channels, then make that a switch with a default that returns -EINVAL.
> +
> +	if (val != 0 && val != 700000)
> +		return -EINVAL;
> +
> +	if (chan->channel == AXP20X_GPIO0_V)
> +		return regmap_update_bits(info->regmap, AXP20X_GPIO10_IN_RANGE,
> +					  AXP20X_GPIO10_IN_RANGE_GPIO0,
> +					  AXP20X_GPIO10_IN_RANGE_GPIO0_VAL(!!val));
> +
> +	return regmap_update_bits(info->regmap, AXP20X_GPIO10_IN_RANGE,
> +				  AXP20X_GPIO10_IN_RANGE_GPIO1,
> +				  AXP20X_GPIO10_IN_RANGE_GPIO1_VAL(!!val));
> +}
> +
> +static const struct iio_info axp20x_adc_iio_info = {
> +	.read_raw = axp20x_read_raw,
> +	.write_raw = axp20x_write_raw,
> +	.driver_module = THIS_MODULE,
> +};
> +
> +static const struct iio_info axp22x_adc_iio_info = {
> +	.read_raw = axp22x_read_raw,
> +	.driver_module = THIS_MODULE,
> +};
> +
> +static int axp20x_adc_rate(int rate)
> +{
> +	return AXP20X_ADC_RATE_HZ(rate);
> +}
> +
> +static int axp22x_adc_rate(int rate)
> +{
> +	return AXP22X_ADC_RATE_HZ(rate);
> +}
> +
> +struct axp_data {
> +	const struct iio_info		*iio_info;
> +	int				num_channels;
> +	struct iio_chan_spec const	*channels;
> +	unsigned long			adc_en1_mask;
> +	int				(*adc_rate)(int rate);
> +	bool				adc_en2;
> +	struct iio_map			*maps;
> +};
> +
> +static const struct axp_data axp20x_data = {
> +	.iio_info = &axp20x_adc_iio_info,
> +	.num_channels = ARRAY_SIZE(axp20x_adc_channels),
> +	.channels = axp20x_adc_channels,
> +	.adc_en1_mask = AXP20X_ADC_EN1_MASK,
> +	.adc_rate = axp20x_adc_rate,
> +	.adc_en2 = true,
> +	.maps = axp20x_maps,
> +};
> +
> +static const struct axp_data axp22x_data = {
> +	.iio_info = &axp22x_adc_iio_info,
> +	.num_channels = ARRAY_SIZE(axp22x_adc_channels),
> +	.channels = axp22x_adc_channels,
> +	.adc_en1_mask = AXP22X_ADC_EN1_MASK,
> +	.adc_rate = axp22x_adc_rate,
> +	.adc_en2 = false,
> +	.maps = axp22x_maps,
> +};
> +
> +static const struct platform_device_id axp20x_adc_id_match[] = {
> +	{ .name = "axp20x-adc", .driver_data = (kernel_ulong_t)&axp20x_data, },
> +	{ .name = "axp22x-adc", .driver_data = (kernel_ulong_t)&axp22x_data, },
> +	{ /* sentinel */ },
> +};
> +
> +static int axp20x_probe(struct platform_device *pdev)
> +{
> +	struct axp20x_adc_iio *info;
> +	struct iio_dev *indio_dev;
> +	struct axp20x_dev *axp20x_dev;
> +	int ret;
> +
> +	axp20x_dev = dev_get_drvdata(pdev->dev.parent);
> +
> +	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*info));
> +	if (!indio_dev)
> +		return -ENOMEM;
> +
> +	info = iio_priv(indio_dev);
> +	platform_set_drvdata(pdev, indio_dev);
> +
> +	info->regmap = axp20x_dev->regmap;
> +	indio_dev->dev.parent = &pdev->dev;
> +	indio_dev->dev.of_node = pdev->dev.of_node;
> +	indio_dev->modes = INDIO_DIRECT_MODE;
> +
> +	info->data = (struct axp_data *)platform_get_device_id(pdev)->driver_data;
I think this is only actually used in probe, so could be handled more
neatly with a local variable rather than sticking it in info.

> +
> +	indio_dev->name = platform_get_device_id(pdev)->name;
> +	indio_dev->info = info->data->iio_info;
> +	indio_dev->num_channels = info->data->num_channels;
> +	indio_dev->channels = info->data->channels;
> +
> +	/* Enable the ADCs on IP */
> +	regmap_write(info->regmap, AXP20X_ADC_EN1, info->data->adc_en1_mask);
> +
> +	if (info->data->adc_en2)
> +		/* Enable GPIO0/1 and internal temperature ADCs */
> +		regmap_update_bits(info->regmap, AXP20X_ADC_EN2,
> +				   AXP20X_ADC_EN2_MASK, AXP20X_ADC_EN2_MASK);
> +
> +	/* Configure ADCs rate */
> +	regmap_update_bits(info->regmap, AXP20X_ADC_RATE, AXP20X_ADC_RATE_MASK,
> +			   info->data->adc_rate(100));
> +
> +	ret = iio_map_array_register(indio_dev, info->data->maps);
> +	if (ret < 0) {
> +		dev_err(&pdev->dev, "failed to register IIO maps: %d\n", ret);
> +		goto fail_map;
> +	}
> +
> +	ret = iio_device_register(indio_dev);
> +	if (ret < 0) {
> +		dev_err(&pdev->dev, "could not register the device\n");
> +		goto fail_register;
> +	}
> +
> +	return 0;
> +
> +fail_register:
> +	iio_map_array_unregister(indio_dev);
> +
> +fail_map:
> +	regmap_write(info->regmap, AXP20X_ADC_EN1, 0);
> +
> +	if (info->data->adc_en2)
> +		regmap_write(info->regmap, AXP20X_ADC_EN2, 0);
> +
> +	return ret;
> +}
> +
> +static int axp20x_remove(struct platform_device *pdev)
> +{
> +	struct axp20x_adc_iio *info;
> +	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
> +
> +	info = iio_priv(indio_dev);
Reorder the two lines above this one and then you could just do this
inline. (really trivial so feel free to not bother ;)
> +
> +	iio_device_unregister(indio_dev);
> +	iio_map_array_unregister(indio_dev);
> +
> +	regmap_write(info->regmap, AXP20X_ADC_EN1, 0);
> +
> +	if (info->data->adc_en2)
> +		regmap_write(info->regmap, AXP20X_ADC_EN2, 0);
> +
> +	return 0;
> +}
> +
> +static struct platform_driver axp20x_adc_driver = {
> +	.driver = {
> +		.name = "axp20x-adc",
> +	},
> +	.id_table = axp20x_adc_id_match,
> +	.probe = axp20x_probe,
> +	.remove = axp20x_remove,
> +};
> +
> +module_platform_driver(axp20x_adc_driver);
> +
> +MODULE_DESCRIPTION("ADC driver for AXP20X and AXP22X PMICs");
> +MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
> +MODULE_LICENSE("GPL");
> 

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 04/18] iio: adc: add support for X-Powers AXP20X and AXP22X PMICs ADCs
@ 2017-02-19 12:40     ` Jonathan Cameron
  0 siblings, 0 replies; 126+ messages in thread
From: Jonathan Cameron @ 2017-02-19 12:40 UTC (permalink / raw)
  To: Quentin Schulz, knaack.h, lars, pmeerw, robh+dt, mark.rutland,
	wens, sre, linux, maxime.ripard, lee.jones
  Cc: thomas.petazzoni, devicetree, linux-pm, linux-iio, linux-kernel,
	bonbons, icenowy, linux-arm-kernel

On 14/02/17 09:40, Quentin Schulz wrote:
> The X-Powers AXP20X and AXP22X PMICs have multiple ADCs. They expose the
> battery voltage, battery charge and discharge currents, AC-in and VBUS
> voltages and currents, 2 GPIOs muxable in ADC mode and PMIC temperature.
> 
> This adds support for most of AXP20X and AXP22X ADCs.
> 
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
Hi Quentin,

A few bits and bobs inline.  The bigest one is that I don't think
you need to carry your struct axp_data pointer around in the iio_priv
structure as it only seems to be used in probe.

Anyhow, tidy these little bits up (quite a few are optional in the
sense that they are a matter or personal taste and I don't feel
strongly about them) and you can add

Reviewed-by: Jonathan Cameron <jic23@kernel.org>

A nice driver.

Jonathan
> ---
> 
> v3:
>  - moved from switch to if condition in axp20x_adc_raw and
>  axp22x_adc_raw,
>  - removed DT support as DT node has been dropped,
>    - use of platform_device_id
>  - correctly defined the name of the iio device (name used to probe the
>  driver),
>  - added goto for errors in probe,
>  - added iio_map_array_unregister to the remove function,
> 
> v2:
>  - removed unused defines,
>  - changed BIT(x) to 1 << x when describing bits purpose for which 2 <<
>  x or 3 << x exists, to be consistent,
>  - changed ADC rate defines to macro formulas,
>  - reordered IIO channels, now different measures (current/voltage) of
>  the same part of the PMIC (e.g. battery), have the same IIO channel in
>  their respective IIO type. When a part of the PMIC have only one
>  measure, a number is jumped,
>  - left IIO channel mapping in DT to use iio_map structure,
>  - removed indexing of ADC internal temperature,
>  - removed unused iio_dev structure in axp20x_adc_iio,
>  - added a structure for data specific to AXP20X or AXP22X PMICs instead
>  of using an ID and an if condition when needing to separate the
>  behaviour of both,
>  - added a comment on batt_chrg_i really being on 12bits rather than
>  what the Chinese datasheets say (13 bits),
>  - corrected the offset for AXP22X PMIC temperature,
>  - set the ADC rate to a value (100Hz) shared by the AXP20X and AXP22X,
>  - created macro formulas to compute the ADC rate for each,
>  - added a condition on presence of ADC_EN2 reg before setting/resetting
>  it,
>  - switched from devm_iio_device_unregister to the non-devm function
>  because of the need for a remove function,
>  - removed some dead code,
> 
>  drivers/iio/adc/Kconfig      |  10 +
>  drivers/iio/adc/Makefile     |   1 +
>  drivers/iio/adc/axp20x_adc.c | 606 +++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 617 insertions(+)
>  create mode 100644 drivers/iio/adc/axp20x_adc.c
> 
> diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
> index 9c8b558..ed17fe1 100644
> --- a/drivers/iio/adc/Kconfig
> +++ b/drivers/iio/adc/Kconfig
> @@ -154,6 +154,16 @@ config AT91_SAMA5D2_ADC
>  	  To compile this driver as a module, choose M here: the module will be
>  	  called at91-sama5d2_adc.
>  
> +config AXP20X_ADC
> +	tristate "X-Powers AXP20X and AXP22X ADC driver"
> +	depends on MFD_AXP20X
> +	help
> +	  Say yes here to have support for X-Powers power management IC (PMIC)
> +	  AXP20X and AXP22X ADC devices.
> +
> +	  To compile this driver as a module, choose M here: the module will be
> +	  called axp20x_adc.
> +
>  config AXP288_ADC
>  	tristate "X-Powers AXP288 ADC driver"
>  	depends on MFD_AXP20X
> diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile
> index d36c4be..f5c28a5 100644
> --- a/drivers/iio/adc/Makefile
> +++ b/drivers/iio/adc/Makefile
> @@ -16,6 +16,7 @@ obj-$(CONFIG_AD7887) += ad7887.o
>  obj-$(CONFIG_AD799X) += ad799x.o
>  obj-$(CONFIG_AT91_ADC) += at91_adc.o
>  obj-$(CONFIG_AT91_SAMA5D2_ADC) += at91-sama5d2_adc.o
> +obj-$(CONFIG_AXP20X_ADC) += axp20x_adc.o
>  obj-$(CONFIG_AXP288_ADC) += axp288_adc.o
>  obj-$(CONFIG_BCM_IPROC_ADC) += bcm_iproc_adc.o
>  obj-$(CONFIG_BERLIN2_ADC) += berlin2-adc.o
> diff --git a/drivers/iio/adc/axp20x_adc.c b/drivers/iio/adc/axp20x_adc.c
> new file mode 100644
> index 0000000..5ef6af8
> --- /dev/null
> +++ b/drivers/iio/adc/axp20x_adc.c
> @@ -0,0 +1,606 @@
> +/* ADC driver for AXP20X and AXP22X PMICs
> + *
> + * Copyright (c) 2016 Free Electrons NextThing Co.
> + *	Quentin Schulz <quentin.schulz@free-electrons.com>
> + *
> + * This program is free software; you can redistribute it and/or modify it under
> + * the terms of the GNU General Public License version 2 as published by the
> + * Free Software Foundation.
> + */
> +
> +#include <linux/completion.h>
> +#include <linux/interrupt.h>
> +#include <linux/io.h>
> +#include <linux/module.h>
> +#include <linux/of.h>
> +#include <linux/of_device.h>
> +#include <linux/platform_device.h>
> +#include <linux/pm_runtime.h>
> +#include <linux/regmap.h>
> +#include <linux/thermal.h>
> +
> +#include <linux/iio/iio.h>
> +#include <linux/iio/driver.h>
> +#include <linux/iio/machine.h>
> +#include <linux/mfd/axp20x.h>
> +
> +#define AXP20X_ADC_EN1_MASK			GENMASK(7, 0)
> +
> +#define AXP20X_ADC_EN2_MASK			(GENMASK(3, 2) | BIT(7))
> +#define AXP22X_ADC_EN1_MASK			(GENMASK(7, 5) | BIT(0))
> +
> +#define AXP20X_GPIO10_IN_RANGE_GPIO0		BIT(0)
> +#define AXP20X_GPIO10_IN_RANGE_GPIO1		BIT(1)
> +#define AXP20X_GPIO10_IN_RANGE_GPIO0_VAL(x)	((x) & BIT(0))
> +#define AXP20X_GPIO10_IN_RANGE_GPIO1_VAL(x)	(((x) & BIT(0)) << 1)
> +
> +#define AXP20X_ADC_RATE_MASK			GENMASK(7, 6)
> +#define AXP20X_ADC_RATE_HZ(x)			((ilog2((x) / 25) << 6) & AXP20X_ADC_RATE_MASK)
> +#define AXP22X_ADC_RATE_HZ(x)			((ilog2((x) / 100) << 6) & AXP20X_ADC_RATE_MASK)
> +
> +#define AXP20X_ADC_CHANNEL(_channel, _name, _type, _reg)	\
> +	{							\
> +		.type = _type,					\
> +		.indexed = 1,					\
> +		.channel = _channel,				\
> +		.address = _reg,				\
> +		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |	\
> +				      BIT(IIO_CHAN_INFO_SCALE),	\
> +		.datasheet_name = _name,			\
> +	}
> +
> +#define AXP20X_ADC_CHANNEL_OFFSET(_channel, _name, _type, _reg) \
> +	{							\
> +		.type = _type,					\
> +		.indexed = 1,					\
> +		.channel = _channel,				\
> +		.address = _reg,				\
> +		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |	\
> +				      BIT(IIO_CHAN_INFO_SCALE) |\
> +				      BIT(IIO_CHAN_INFO_OFFSET),\
> +		.datasheet_name = _name,			\
> +	}
> +
> +struct axp_data;
> +
> +struct axp20x_adc_iio {
> +	struct regmap		*regmap;
I 'think' you only actually use data in probe.  As such it doesn't need
to be in here at all..

At that point you could just have the regmap pointer directly as your
private data and drop this structure entirely.  Whether you want to do
that though is up to you.
> +	struct axp_data		*data;
> +};
> +
> +enum axp20x_adc_channel_v {
> +	AXP20X_ACIN_V = 0,
> +	AXP20X_VBUS_V,
> +	AXP20X_TS_IN,
> +	AXP20X_GPIO0_V,
> +	AXP20X_GPIO1_V,
> +	AXP20X_IPSOUT_V,
> +	AXP20X_BATT_V,
> +};
> +
> +enum axp20x_adc_channel_i {
> +	AXP20X_ACIN_I = 0,
> +	AXP20X_VBUS_I,
> +	AXP20X_BATT_CHRG_I,
> +	AXP20X_BATT_DISCHRG_I,
> +};
> +
> +enum axp22x_adc_channel_v {
> +	AXP22X_TS_IN = 0,
> +	AXP22X_BATT_V,
> +};
> +
> +enum axp22x_adc_channel_i {
> +	AXP22X_BATT_CHRG_I = 1,
> +	AXP22X_BATT_DISCHRG_I,
> +};
> +
> +static struct iio_map axp20x_maps[] = {
> +	{
> +		.consumer_dev_name = "axp20x-usb-power-supply",
> +		.consumer_channel = "vbus_v",
> +		.adc_channel_label = "vbus_v",
> +	}, {
> +		.consumer_dev_name = "axp20x-usb-power-supply",
> +		.consumer_channel = "vbus_i",
> +		.adc_channel_label = "vbus_i",
> +	}, {
> +		.consumer_dev_name = "axp20x-ac-power-supply",
> +		.consumer_channel = "acin_v",
> +		.adc_channel_label = "acin_v",
> +	}, {
> +		.consumer_dev_name = "axp20x-ac-power-supply",
> +		.consumer_channel = "acin_i",
> +		.adc_channel_label = "acin_i",
> +	}, {
> +		.consumer_dev_name = "axp20x-battery-power-supply",
> +		.consumer_channel = "batt_v",
> +		.adc_channel_label = "batt_v",
> +	}, {
> +		.consumer_dev_name = "axp20x-battery-power-supply",
> +		.consumer_channel = "batt_chrg_i",
> +		.adc_channel_label = "batt_chrg_i",
> +	}, {
> +		.consumer_dev_name = "axp20x-battery-power-supply",
> +		.consumer_channel = "batt_dischrg_i",
> +		.adc_channel_label = "batt_dischrg_i",
> +	}, { /* sentinel */ }
> +};
> +
> +static struct iio_map axp22x_maps[] = {
> +	{
> +		.consumer_dev_name = "axp20x-battery-power-supply",
> +		.consumer_channel = "batt_v",
> +		.adc_channel_label = "batt_v",
> +	}, {
> +		.consumer_dev_name = "axp20x-battery-power-supply",
> +		.consumer_channel = "batt_chrg_i",
> +		.adc_channel_label = "batt_chrg_i",
> +	}, {
> +		.consumer_dev_name = "axp20x-battery-power-supply",
> +		.consumer_channel = "batt_dischrg_i",
> +		.adc_channel_label = "batt_dischrg_i",
> +	}, { /* sentinel */ }
> +};
> +
> +/*
> + * Channels are mapped by physical system. Their channels share the same index.
> + * i.e. acin_i is in_current0_raw and acin_v is in_voltage0_raw.
> + * The only exception is for the battery. batt_v will be in_voltage6_raw and
> + * charge current in_current6_raw and discharge current will be in_current7_raw.
> + */
> +static const struct iio_chan_spec axp20x_adc_channels[] = {
> +	AXP20X_ADC_CHANNEL(AXP20X_ACIN_V, "acin_v", IIO_VOLTAGE,
> +			   AXP20X_ACIN_V_ADC_H),
> +	AXP20X_ADC_CHANNEL(AXP20X_ACIN_I, "acin_i", IIO_CURRENT,
> +			   AXP20X_ACIN_I_ADC_H),
> +	AXP20X_ADC_CHANNEL(AXP20X_VBUS_V, "vbus_v", IIO_VOLTAGE,
> +			   AXP20X_VBUS_V_ADC_H),
> +	AXP20X_ADC_CHANNEL(AXP20X_VBUS_I, "vbus_i", IIO_CURRENT,
> +			   AXP20X_VBUS_I_ADC_H),
> +	{
> +		.type = IIO_TEMP,
> +		.address = AXP20X_TEMP_ADC_H,
> +		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
> +				      BIT(IIO_CHAN_INFO_SCALE) |
> +				      BIT(IIO_CHAN_INFO_OFFSET),
> +		.datasheet_name = "pmic_temp",
> +	},
> +	AXP20X_ADC_CHANNEL_OFFSET(AXP20X_GPIO0_V, "gpio0_v", IIO_VOLTAGE,
> +				  AXP20X_GPIO0_V_ADC_H),
> +	AXP20X_ADC_CHANNEL_OFFSET(AXP20X_GPIO1_V, "gpio1_v", IIO_VOLTAGE,
> +				  AXP20X_GPIO1_V_ADC_H),
> +	AXP20X_ADC_CHANNEL(AXP20X_IPSOUT_V, "ipsout_v", IIO_VOLTAGE,
> +			   AXP20X_IPSOUT_V_HIGH_H),
> +	AXP20X_ADC_CHANNEL(AXP20X_BATT_V, "batt_v", IIO_VOLTAGE,
> +			   AXP20X_BATT_V_H),
> +	AXP20X_ADC_CHANNEL(AXP20X_BATT_CHRG_I, "batt_chrg_i", IIO_CURRENT,
> +			   AXP20X_BATT_CHRG_I_H),
> +	AXP20X_ADC_CHANNEL(AXP20X_BATT_DISCHRG_I, "batt_dischrg_i", IIO_CURRENT,
> +			   AXP20X_BATT_DISCHRG_I_H),
> +};
> +
> +static const struct iio_chan_spec axp22x_adc_channels[] = {
> +	{
> +		.type = IIO_TEMP,
> +		.address = AXP22X_PMIC_TEMP_H,
> +		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
> +				      BIT(IIO_CHAN_INFO_SCALE) |
> +				      BIT(IIO_CHAN_INFO_OFFSET),
> +		.datasheet_name = "pmic_temp",
> +	},
> +	AXP20X_ADC_CHANNEL(AXP22X_BATT_V, "batt_v", IIO_VOLTAGE,
> +			   AXP20X_BATT_V_H),
> +	AXP20X_ADC_CHANNEL(AXP22X_BATT_CHRG_I, "batt_chrg_i", IIO_CURRENT,
> +			   AXP20X_BATT_CHRG_I_H),
> +	AXP20X_ADC_CHANNEL(AXP22X_BATT_DISCHRG_I, "batt_dischrg_i", IIO_CURRENT,
> +			   AXP20X_BATT_DISCHRG_I_H),
> +};
> +
> +static int axp20x_adc_raw(struct iio_dev *indio_dev,
> +			  struct iio_chan_spec const *chan, int *val)
> +{
> +	struct axp20x_adc_iio *info = iio_priv(indio_dev);
> +	int size = 12;
> +
> +	/*
> +	 *N.B.:  Unlike the Chinese datasheets tell, the charging current is
Space before the N
> +	 * stored on 12 bits, not 13 bits. Only discharging current is on 13
> +	 * bits.
> +	 */
> +	if (chan->type == IIO_CURRENT && chan->channel == AXP20X_BATT_DISCHRG_I)
> +		size = 13;
> +	else
> +		size = 12;
> +
> +	*val = axp20x_read_variable_width(info->regmap, chan->address, size);
> +	if (*val < 0)
> +		return *val;
> +
> +	return IIO_VAL_INT;
> +}
> +
> +static int axp22x_adc_raw(struct iio_dev *indio_dev,
> +			  struct iio_chan_spec const *chan, int *val)
> +{
> +	struct axp20x_adc_iio *info = iio_priv(indio_dev);
> +	int size;
> +
> +	/*
> +	 * N.B.: Unlike the Chinese datasheets tell, the charging current is
> +	 * stored on 12 bits, not 13 bits. Only discharging current is on 13
> +	 * bits.
> +	 */
> +	if (chan->type == IIO_CURRENT && chan->channel == AXP22X_BATT_DISCHRG_I)
> +		size = 13;
> +	else
> +		size = 12;
> +
> +	*val = axp20x_read_variable_width(info->regmap, chan->address, size);
> +	if (*val < 0)
> +		return *val;
> +
> +	return IIO_VAL_INT;
> +}
> +
> +static int axp20x_adc_scale_voltage(int channel, int *val, int *val2)
> +{
> +	switch (channel) {
> +	case AXP20X_ACIN_V:
> +	case AXP20X_VBUS_V:
> +		*val = 1;
> +		*val2 = 700000;
> +		return IIO_VAL_INT_PLUS_MICRO;
> +
> +	case AXP20X_GPIO0_V:
> +	case AXP20X_GPIO1_V:
> +		*val = 0;
> +		*val2 = 500000;
> +		return IIO_VAL_INT_PLUS_MICRO;
> +
> +	case AXP20X_BATT_V:
> +		*val = 1;
> +		*val2 = 100000;
> +		return IIO_VAL_INT_PLUS_MICRO;
> +
> +	case AXP20X_IPSOUT_V:
> +		*val = 1;
> +		*val2 = 400000;
> +		return IIO_VAL_INT_PLUS_MICRO;
> +
> +	default:
> +		return -EINVAL;
> +	}
> +}
> +
> +static int axp20x_adc_scale_current(int channel, int *val, int *val2)
> +{
> +	switch (channel) {
> +	case AXP20X_ACIN_I:
> +		*val = 0;
> +		*val2 = 625000;
> +		return IIO_VAL_INT_PLUS_MICRO;
> +
> +	case AXP20X_VBUS_I:
> +		*val = 0;
> +		*val2 = 375000;
> +		return IIO_VAL_INT_PLUS_MICRO;
> +
> +	case AXP20X_BATT_DISCHRG_I:
> +	case AXP20X_BATT_CHRG_I:
> +		*val = 0;
> +		*val2 = 500000;
> +		return IIO_VAL_INT_PLUS_MICRO;
> +
> +	default:
> +		return -EINVAL;
> +	}
> +}
> +
> +static int axp20x_adc_scale(struct iio_chan_spec const *chan, int *val,
> +			    int *val2)
> +{
> +	switch (chan->type) {
> +	case IIO_VOLTAGE:
> +		return axp20x_adc_scale_voltage(chan->channel, val, val2);
> +
> +	case IIO_CURRENT:
> +		return axp20x_adc_scale_current(chan->channel, val, val2);
> +
> +	case IIO_TEMP:
> +		*val = 100;
> +		return IIO_VAL_INT;
> +
> +	default:
> +		return -EINVAL;
> +	}
> +}
> +
> +static int axp22x_adc_scale(struct iio_chan_spec const *chan, int *val,
> +			    int *val2)
> +{
> +	switch (chan->type) {
> +	case IIO_VOLTAGE:
> +		if (chan->channel != AXP22X_BATT_V)
> +			return -EINVAL;
> +
> +		*val = 1;
> +		*val2 = 100000;
> +		return IIO_VAL_INT_PLUS_MICRO;
> +
> +	case IIO_CURRENT:
> +		*val = 0;
> +		*val2 = 500000;
> +		return IIO_VAL_INT_PLUS_MICRO;
> +
> +	case IIO_TEMP:
> +		*val = 100;
> +		return IIO_VAL_INT;
> +
> +	default:
> +		return -EINVAL;
> +	}
> +}
> +
> +static int axp20x_adc_offset_voltage(struct iio_dev *indio_dev, int channel,
> +				     int *val)
> +{
> +	struct axp20x_adc_iio *info = iio_priv(indio_dev);
> +	int ret, reg;
> +
> +	if (channel != AXP20X_GPIO0_V && channel != AXP20X_GPIO1_V)
> +		return -EINVAL;
Again, you could mess with the ordering here to only check the channel
value once. (i.e. use a switch after the register read). Might be ever
so slightly tidier.  Up to you.
> +
> +	ret = regmap_read(info->regmap, AXP20X_GPIO10_IN_RANGE, &reg);
> +	if (ret < 0)
> +		return ret;
> +
> +	if (channel == AXP20X_GPIO0_V)
> +		*val = reg & AXP20X_GPIO10_IN_RANGE_GPIO0;
> +	else
> +		*val = reg & AXP20X_GPIO10_IN_RANGE_GPIO1;
> +
> +	*val = !!(*val) * 700000;
> +
> +	return IIO_VAL_INT;
> +}
> +
> +static int axp20x_adc_offset(struct iio_dev *indio_dev,
> +			     struct iio_chan_spec const *chan, int *val)
> +{
> +	switch (chan->type) {
> +	case IIO_VOLTAGE:
> +		return axp20x_adc_offset_voltage(indio_dev, chan->channel, val);
> +
> +	case IIO_TEMP:
> +		*val = -1447;
> +		return IIO_VAL_INT;
> +
> +	default:
> +		return -EINVAL;
> +	}
> +}
> +
> +static int axp20x_read_raw(struct iio_dev *indio_dev,
> +			   struct iio_chan_spec const *chan, int *val,
> +			   int *val2, long mask)
> +{
> +	switch (mask) {
> +	case IIO_CHAN_INFO_OFFSET:
> +		return axp20x_adc_offset(indio_dev, chan, val);
> +
> +	case IIO_CHAN_INFO_SCALE:
> +		return axp20x_adc_scale(chan, val, val2);
> +
> +	case IIO_CHAN_INFO_RAW:
> +		return axp20x_adc_raw(indio_dev, chan, val);
> +
> +	default:
> +		return -EINVAL;
> +	}
> +}
> +
> +static int axp22x_read_raw(struct iio_dev *indio_dev,
> +			   struct iio_chan_spec const *chan, int *val,
> +			   int *val2, long mask)
> +{
> +	switch (mask) {
> +	case IIO_CHAN_INFO_OFFSET:
> +		*val = -2677;
> +		return IIO_VAL_INT;
> +
> +	case IIO_CHAN_INFO_SCALE:
> +		return axp22x_adc_scale(chan, val, val2);
> +
> +	case IIO_CHAN_INFO_RAW:
> +		return axp22x_adc_raw(indio_dev, chan, val);
> +
> +	default:
> +		return -EINVAL;
> +	}
> +}
> +
> +static int axp20x_write_raw(struct iio_dev *indio_dev,
> +			    struct iio_chan_spec const *chan, int val, int val2,
> +			    long mask)
> +{
> +	struct axp20x_adc_iio *info = iio_priv(indio_dev);
> +
> +	/*
> +	 * The AXP20X PMIC allows the user to choose between 0V and 0.7V offsets
> +	 * for (independently) GPIO0 and GPIO1 when in ADC mode.
> +	 */
> +	if (mask != IIO_CHAN_INFO_OFFSET)
> +		return -EINVAL;
> +
> +	if (chan->channel != AXP20X_GPIO0_V && chan->channel != AXP20X_GPIO1_V)
> +		return -EINVAL;
Might be a tiny bit nicer to move this down to where you are chosing between
the two channels, then make that a switch with a default that returns -EINVAL.
> +
> +	if (val != 0 && val != 700000)
> +		return -EINVAL;
> +
> +	if (chan->channel == AXP20X_GPIO0_V)
> +		return regmap_update_bits(info->regmap, AXP20X_GPIO10_IN_RANGE,
> +					  AXP20X_GPIO10_IN_RANGE_GPIO0,
> +					  AXP20X_GPIO10_IN_RANGE_GPIO0_VAL(!!val));
> +
> +	return regmap_update_bits(info->regmap, AXP20X_GPIO10_IN_RANGE,
> +				  AXP20X_GPIO10_IN_RANGE_GPIO1,
> +				  AXP20X_GPIO10_IN_RANGE_GPIO1_VAL(!!val));
> +}
> +
> +static const struct iio_info axp20x_adc_iio_info = {
> +	.read_raw = axp20x_read_raw,
> +	.write_raw = axp20x_write_raw,
> +	.driver_module = THIS_MODULE,
> +};
> +
> +static const struct iio_info axp22x_adc_iio_info = {
> +	.read_raw = axp22x_read_raw,
> +	.driver_module = THIS_MODULE,
> +};
> +
> +static int axp20x_adc_rate(int rate)
> +{
> +	return AXP20X_ADC_RATE_HZ(rate);
> +}
> +
> +static int axp22x_adc_rate(int rate)
> +{
> +	return AXP22X_ADC_RATE_HZ(rate);
> +}
> +
> +struct axp_data {
> +	const struct iio_info		*iio_info;
> +	int				num_channels;
> +	struct iio_chan_spec const	*channels;
> +	unsigned long			adc_en1_mask;
> +	int				(*adc_rate)(int rate);
> +	bool				adc_en2;
> +	struct iio_map			*maps;
> +};
> +
> +static const struct axp_data axp20x_data = {
> +	.iio_info = &axp20x_adc_iio_info,
> +	.num_channels = ARRAY_SIZE(axp20x_adc_channels),
> +	.channels = axp20x_adc_channels,
> +	.adc_en1_mask = AXP20X_ADC_EN1_MASK,
> +	.adc_rate = axp20x_adc_rate,
> +	.adc_en2 = true,
> +	.maps = axp20x_maps,
> +};
> +
> +static const struct axp_data axp22x_data = {
> +	.iio_info = &axp22x_adc_iio_info,
> +	.num_channels = ARRAY_SIZE(axp22x_adc_channels),
> +	.channels = axp22x_adc_channels,
> +	.adc_en1_mask = AXP22X_ADC_EN1_MASK,
> +	.adc_rate = axp22x_adc_rate,
> +	.adc_en2 = false,
> +	.maps = axp22x_maps,
> +};
> +
> +static const struct platform_device_id axp20x_adc_id_match[] = {
> +	{ .name = "axp20x-adc", .driver_data = (kernel_ulong_t)&axp20x_data, },
> +	{ .name = "axp22x-adc", .driver_data = (kernel_ulong_t)&axp22x_data, },
> +	{ /* sentinel */ },
> +};
> +
> +static int axp20x_probe(struct platform_device *pdev)
> +{
> +	struct axp20x_adc_iio *info;
> +	struct iio_dev *indio_dev;
> +	struct axp20x_dev *axp20x_dev;
> +	int ret;
> +
> +	axp20x_dev = dev_get_drvdata(pdev->dev.parent);
> +
> +	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*info));
> +	if (!indio_dev)
> +		return -ENOMEM;
> +
> +	info = iio_priv(indio_dev);
> +	platform_set_drvdata(pdev, indio_dev);
> +
> +	info->regmap = axp20x_dev->regmap;
> +	indio_dev->dev.parent = &pdev->dev;
> +	indio_dev->dev.of_node = pdev->dev.of_node;
> +	indio_dev->modes = INDIO_DIRECT_MODE;
> +
> +	info->data = (struct axp_data *)platform_get_device_id(pdev)->driver_data;
I think this is only actually used in probe, so could be handled more
neatly with a local variable rather than sticking it in info.

> +
> +	indio_dev->name = platform_get_device_id(pdev)->name;
> +	indio_dev->info = info->data->iio_info;
> +	indio_dev->num_channels = info->data->num_channels;
> +	indio_dev->channels = info->data->channels;
> +
> +	/* Enable the ADCs on IP */
> +	regmap_write(info->regmap, AXP20X_ADC_EN1, info->data->adc_en1_mask);
> +
> +	if (info->data->adc_en2)
> +		/* Enable GPIO0/1 and internal temperature ADCs */
> +		regmap_update_bits(info->regmap, AXP20X_ADC_EN2,
> +				   AXP20X_ADC_EN2_MASK, AXP20X_ADC_EN2_MASK);
> +
> +	/* Configure ADCs rate */
> +	regmap_update_bits(info->regmap, AXP20X_ADC_RATE, AXP20X_ADC_RATE_MASK,
> +			   info->data->adc_rate(100));
> +
> +	ret = iio_map_array_register(indio_dev, info->data->maps);
> +	if (ret < 0) {
> +		dev_err(&pdev->dev, "failed to register IIO maps: %d\n", ret);
> +		goto fail_map;
> +	}
> +
> +	ret = iio_device_register(indio_dev);
> +	if (ret < 0) {
> +		dev_err(&pdev->dev, "could not register the device\n");
> +		goto fail_register;
> +	}
> +
> +	return 0;
> +
> +fail_register:
> +	iio_map_array_unregister(indio_dev);
> +
> +fail_map:
> +	regmap_write(info->regmap, AXP20X_ADC_EN1, 0);
> +
> +	if (info->data->adc_en2)
> +		regmap_write(info->regmap, AXP20X_ADC_EN2, 0);
> +
> +	return ret;
> +}
> +
> +static int axp20x_remove(struct platform_device *pdev)
> +{
> +	struct axp20x_adc_iio *info;
> +	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
> +
> +	info = iio_priv(indio_dev);
Reorder the two lines above this one and then you could just do this
inline. (really trivial so feel free to not bother ;)
> +
> +	iio_device_unregister(indio_dev);
> +	iio_map_array_unregister(indio_dev);
> +
> +	regmap_write(info->regmap, AXP20X_ADC_EN1, 0);
> +
> +	if (info->data->adc_en2)
> +		regmap_write(info->regmap, AXP20X_ADC_EN2, 0);
> +
> +	return 0;
> +}
> +
> +static struct platform_driver axp20x_adc_driver = {
> +	.driver = {
> +		.name = "axp20x-adc",
> +	},
> +	.id_table = axp20x_adc_id_match,
> +	.probe = axp20x_probe,
> +	.remove = axp20x_remove,
> +};
> +
> +module_platform_driver(axp20x_adc_driver);
> +
> +MODULE_DESCRIPTION("ADC driver for AXP20X and AXP22X PMICs");
> +MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
> +MODULE_LICENSE("GPL");
> 

^ permalink raw reply	[flat|nested] 126+ messages in thread

* [PATCH v3 04/18] iio: adc: add support for X-Powers AXP20X and AXP22X PMICs ADCs
@ 2017-02-19 12:40     ` Jonathan Cameron
  0 siblings, 0 replies; 126+ messages in thread
From: Jonathan Cameron @ 2017-02-19 12:40 UTC (permalink / raw)
  To: linux-arm-kernel

On 14/02/17 09:40, Quentin Schulz wrote:
> The X-Powers AXP20X and AXP22X PMICs have multiple ADCs. They expose the
> battery voltage, battery charge and discharge currents, AC-in and VBUS
> voltages and currents, 2 GPIOs muxable in ADC mode and PMIC temperature.
> 
> This adds support for most of AXP20X and AXP22X ADCs.
> 
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
Hi Quentin,

A few bits and bobs inline.  The bigest one is that I don't think
you need to carry your struct axp_data pointer around in the iio_priv
structure as it only seems to be used in probe.

Anyhow, tidy these little bits up (quite a few are optional in the
sense that they are a matter or personal taste and I don't feel
strongly about them) and you can add

Reviewed-by: Jonathan Cameron <jic23@kernel.org>

A nice driver.

Jonathan
> ---
> 
> v3:
>  - moved from switch to if condition in axp20x_adc_raw and
>  axp22x_adc_raw,
>  - removed DT support as DT node has been dropped,
>    - use of platform_device_id
>  - correctly defined the name of the iio device (name used to probe the
>  driver),
>  - added goto for errors in probe,
>  - added iio_map_array_unregister to the remove function,
> 
> v2:
>  - removed unused defines,
>  - changed BIT(x) to 1 << x when describing bits purpose for which 2 <<
>  x or 3 << x exists, to be consistent,
>  - changed ADC rate defines to macro formulas,
>  - reordered IIO channels, now different measures (current/voltage) of
>  the same part of the PMIC (e.g. battery), have the same IIO channel in
>  their respective IIO type. When a part of the PMIC have only one
>  measure, a number is jumped,
>  - left IIO channel mapping in DT to use iio_map structure,
>  - removed indexing of ADC internal temperature,
>  - removed unused iio_dev structure in axp20x_adc_iio,
>  - added a structure for data specific to AXP20X or AXP22X PMICs instead
>  of using an ID and an if condition when needing to separate the
>  behaviour of both,
>  - added a comment on batt_chrg_i really being on 12bits rather than
>  what the Chinese datasheets say (13 bits),
>  - corrected the offset for AXP22X PMIC temperature,
>  - set the ADC rate to a value (100Hz) shared by the AXP20X and AXP22X,
>  - created macro formulas to compute the ADC rate for each,
>  - added a condition on presence of ADC_EN2 reg before setting/resetting
>  it,
>  - switched from devm_iio_device_unregister to the non-devm function
>  because of the need for a remove function,
>  - removed some dead code,
> 
>  drivers/iio/adc/Kconfig      |  10 +
>  drivers/iio/adc/Makefile     |   1 +
>  drivers/iio/adc/axp20x_adc.c | 606 +++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 617 insertions(+)
>  create mode 100644 drivers/iio/adc/axp20x_adc.c
> 
> diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
> index 9c8b558..ed17fe1 100644
> --- a/drivers/iio/adc/Kconfig
> +++ b/drivers/iio/adc/Kconfig
> @@ -154,6 +154,16 @@ config AT91_SAMA5D2_ADC
>  	  To compile this driver as a module, choose M here: the module will be
>  	  called at91-sama5d2_adc.
>  
> +config AXP20X_ADC
> +	tristate "X-Powers AXP20X and AXP22X ADC driver"
> +	depends on MFD_AXP20X
> +	help
> +	  Say yes here to have support for X-Powers power management IC (PMIC)
> +	  AXP20X and AXP22X ADC devices.
> +
> +	  To compile this driver as a module, choose M here: the module will be
> +	  called axp20x_adc.
> +
>  config AXP288_ADC
>  	tristate "X-Powers AXP288 ADC driver"
>  	depends on MFD_AXP20X
> diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile
> index d36c4be..f5c28a5 100644
> --- a/drivers/iio/adc/Makefile
> +++ b/drivers/iio/adc/Makefile
> @@ -16,6 +16,7 @@ obj-$(CONFIG_AD7887) += ad7887.o
>  obj-$(CONFIG_AD799X) += ad799x.o
>  obj-$(CONFIG_AT91_ADC) += at91_adc.o
>  obj-$(CONFIG_AT91_SAMA5D2_ADC) += at91-sama5d2_adc.o
> +obj-$(CONFIG_AXP20X_ADC) += axp20x_adc.o
>  obj-$(CONFIG_AXP288_ADC) += axp288_adc.o
>  obj-$(CONFIG_BCM_IPROC_ADC) += bcm_iproc_adc.o
>  obj-$(CONFIG_BERLIN2_ADC) += berlin2-adc.o
> diff --git a/drivers/iio/adc/axp20x_adc.c b/drivers/iio/adc/axp20x_adc.c
> new file mode 100644
> index 0000000..5ef6af8
> --- /dev/null
> +++ b/drivers/iio/adc/axp20x_adc.c
> @@ -0,0 +1,606 @@
> +/* ADC driver for AXP20X and AXP22X PMICs
> + *
> + * Copyright (c) 2016 Free Electrons NextThing Co.
> + *	Quentin Schulz <quentin.schulz@free-electrons.com>
> + *
> + * This program is free software; you can redistribute it and/or modify it under
> + * the terms of the GNU General Public License version 2 as published by the
> + * Free Software Foundation.
> + */
> +
> +#include <linux/completion.h>
> +#include <linux/interrupt.h>
> +#include <linux/io.h>
> +#include <linux/module.h>
> +#include <linux/of.h>
> +#include <linux/of_device.h>
> +#include <linux/platform_device.h>
> +#include <linux/pm_runtime.h>
> +#include <linux/regmap.h>
> +#include <linux/thermal.h>
> +
> +#include <linux/iio/iio.h>
> +#include <linux/iio/driver.h>
> +#include <linux/iio/machine.h>
> +#include <linux/mfd/axp20x.h>
> +
> +#define AXP20X_ADC_EN1_MASK			GENMASK(7, 0)
> +
> +#define AXP20X_ADC_EN2_MASK			(GENMASK(3, 2) | BIT(7))
> +#define AXP22X_ADC_EN1_MASK			(GENMASK(7, 5) | BIT(0))
> +
> +#define AXP20X_GPIO10_IN_RANGE_GPIO0		BIT(0)
> +#define AXP20X_GPIO10_IN_RANGE_GPIO1		BIT(1)
> +#define AXP20X_GPIO10_IN_RANGE_GPIO0_VAL(x)	((x) & BIT(0))
> +#define AXP20X_GPIO10_IN_RANGE_GPIO1_VAL(x)	(((x) & BIT(0)) << 1)
> +
> +#define AXP20X_ADC_RATE_MASK			GENMASK(7, 6)
> +#define AXP20X_ADC_RATE_HZ(x)			((ilog2((x) / 25) << 6) & AXP20X_ADC_RATE_MASK)
> +#define AXP22X_ADC_RATE_HZ(x)			((ilog2((x) / 100) << 6) & AXP20X_ADC_RATE_MASK)
> +
> +#define AXP20X_ADC_CHANNEL(_channel, _name, _type, _reg)	\
> +	{							\
> +		.type = _type,					\
> +		.indexed = 1,					\
> +		.channel = _channel,				\
> +		.address = _reg,				\
> +		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |	\
> +				      BIT(IIO_CHAN_INFO_SCALE),	\
> +		.datasheet_name = _name,			\
> +	}
> +
> +#define AXP20X_ADC_CHANNEL_OFFSET(_channel, _name, _type, _reg) \
> +	{							\
> +		.type = _type,					\
> +		.indexed = 1,					\
> +		.channel = _channel,				\
> +		.address = _reg,				\
> +		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |	\
> +				      BIT(IIO_CHAN_INFO_SCALE) |\
> +				      BIT(IIO_CHAN_INFO_OFFSET),\
> +		.datasheet_name = _name,			\
> +	}
> +
> +struct axp_data;
> +
> +struct axp20x_adc_iio {
> +	struct regmap		*regmap;
I 'think' you only actually use data in probe.  As such it doesn't need
to be in here at all..

At that point you could just have the regmap pointer directly as your
private data and drop this structure entirely.  Whether you want to do
that though is up to you.
> +	struct axp_data		*data;
> +};
> +
> +enum axp20x_adc_channel_v {
> +	AXP20X_ACIN_V = 0,
> +	AXP20X_VBUS_V,
> +	AXP20X_TS_IN,
> +	AXP20X_GPIO0_V,
> +	AXP20X_GPIO1_V,
> +	AXP20X_IPSOUT_V,
> +	AXP20X_BATT_V,
> +};
> +
> +enum axp20x_adc_channel_i {
> +	AXP20X_ACIN_I = 0,
> +	AXP20X_VBUS_I,
> +	AXP20X_BATT_CHRG_I,
> +	AXP20X_BATT_DISCHRG_I,
> +};
> +
> +enum axp22x_adc_channel_v {
> +	AXP22X_TS_IN = 0,
> +	AXP22X_BATT_V,
> +};
> +
> +enum axp22x_adc_channel_i {
> +	AXP22X_BATT_CHRG_I = 1,
> +	AXP22X_BATT_DISCHRG_I,
> +};
> +
> +static struct iio_map axp20x_maps[] = {
> +	{
> +		.consumer_dev_name = "axp20x-usb-power-supply",
> +		.consumer_channel = "vbus_v",
> +		.adc_channel_label = "vbus_v",
> +	}, {
> +		.consumer_dev_name = "axp20x-usb-power-supply",
> +		.consumer_channel = "vbus_i",
> +		.adc_channel_label = "vbus_i",
> +	}, {
> +		.consumer_dev_name = "axp20x-ac-power-supply",
> +		.consumer_channel = "acin_v",
> +		.adc_channel_label = "acin_v",
> +	}, {
> +		.consumer_dev_name = "axp20x-ac-power-supply",
> +		.consumer_channel = "acin_i",
> +		.adc_channel_label = "acin_i",
> +	}, {
> +		.consumer_dev_name = "axp20x-battery-power-supply",
> +		.consumer_channel = "batt_v",
> +		.adc_channel_label = "batt_v",
> +	}, {
> +		.consumer_dev_name = "axp20x-battery-power-supply",
> +		.consumer_channel = "batt_chrg_i",
> +		.adc_channel_label = "batt_chrg_i",
> +	}, {
> +		.consumer_dev_name = "axp20x-battery-power-supply",
> +		.consumer_channel = "batt_dischrg_i",
> +		.adc_channel_label = "batt_dischrg_i",
> +	}, { /* sentinel */ }
> +};
> +
> +static struct iio_map axp22x_maps[] = {
> +	{
> +		.consumer_dev_name = "axp20x-battery-power-supply",
> +		.consumer_channel = "batt_v",
> +		.adc_channel_label = "batt_v",
> +	}, {
> +		.consumer_dev_name = "axp20x-battery-power-supply",
> +		.consumer_channel = "batt_chrg_i",
> +		.adc_channel_label = "batt_chrg_i",
> +	}, {
> +		.consumer_dev_name = "axp20x-battery-power-supply",
> +		.consumer_channel = "batt_dischrg_i",
> +		.adc_channel_label = "batt_dischrg_i",
> +	}, { /* sentinel */ }
> +};
> +
> +/*
> + * Channels are mapped by physical system. Their channels share the same index.
> + * i.e. acin_i is in_current0_raw and acin_v is in_voltage0_raw.
> + * The only exception is for the battery. batt_v will be in_voltage6_raw and
> + * charge current in_current6_raw and discharge current will be in_current7_raw.
> + */
> +static const struct iio_chan_spec axp20x_adc_channels[] = {
> +	AXP20X_ADC_CHANNEL(AXP20X_ACIN_V, "acin_v", IIO_VOLTAGE,
> +			   AXP20X_ACIN_V_ADC_H),
> +	AXP20X_ADC_CHANNEL(AXP20X_ACIN_I, "acin_i", IIO_CURRENT,
> +			   AXP20X_ACIN_I_ADC_H),
> +	AXP20X_ADC_CHANNEL(AXP20X_VBUS_V, "vbus_v", IIO_VOLTAGE,
> +			   AXP20X_VBUS_V_ADC_H),
> +	AXP20X_ADC_CHANNEL(AXP20X_VBUS_I, "vbus_i", IIO_CURRENT,
> +			   AXP20X_VBUS_I_ADC_H),
> +	{
> +		.type = IIO_TEMP,
> +		.address = AXP20X_TEMP_ADC_H,
> +		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
> +				      BIT(IIO_CHAN_INFO_SCALE) |
> +				      BIT(IIO_CHAN_INFO_OFFSET),
> +		.datasheet_name = "pmic_temp",
> +	},
> +	AXP20X_ADC_CHANNEL_OFFSET(AXP20X_GPIO0_V, "gpio0_v", IIO_VOLTAGE,
> +				  AXP20X_GPIO0_V_ADC_H),
> +	AXP20X_ADC_CHANNEL_OFFSET(AXP20X_GPIO1_V, "gpio1_v", IIO_VOLTAGE,
> +				  AXP20X_GPIO1_V_ADC_H),
> +	AXP20X_ADC_CHANNEL(AXP20X_IPSOUT_V, "ipsout_v", IIO_VOLTAGE,
> +			   AXP20X_IPSOUT_V_HIGH_H),
> +	AXP20X_ADC_CHANNEL(AXP20X_BATT_V, "batt_v", IIO_VOLTAGE,
> +			   AXP20X_BATT_V_H),
> +	AXP20X_ADC_CHANNEL(AXP20X_BATT_CHRG_I, "batt_chrg_i", IIO_CURRENT,
> +			   AXP20X_BATT_CHRG_I_H),
> +	AXP20X_ADC_CHANNEL(AXP20X_BATT_DISCHRG_I, "batt_dischrg_i", IIO_CURRENT,
> +			   AXP20X_BATT_DISCHRG_I_H),
> +};
> +
> +static const struct iio_chan_spec axp22x_adc_channels[] = {
> +	{
> +		.type = IIO_TEMP,
> +		.address = AXP22X_PMIC_TEMP_H,
> +		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
> +				      BIT(IIO_CHAN_INFO_SCALE) |
> +				      BIT(IIO_CHAN_INFO_OFFSET),
> +		.datasheet_name = "pmic_temp",
> +	},
> +	AXP20X_ADC_CHANNEL(AXP22X_BATT_V, "batt_v", IIO_VOLTAGE,
> +			   AXP20X_BATT_V_H),
> +	AXP20X_ADC_CHANNEL(AXP22X_BATT_CHRG_I, "batt_chrg_i", IIO_CURRENT,
> +			   AXP20X_BATT_CHRG_I_H),
> +	AXP20X_ADC_CHANNEL(AXP22X_BATT_DISCHRG_I, "batt_dischrg_i", IIO_CURRENT,
> +			   AXP20X_BATT_DISCHRG_I_H),
> +};
> +
> +static int axp20x_adc_raw(struct iio_dev *indio_dev,
> +			  struct iio_chan_spec const *chan, int *val)
> +{
> +	struct axp20x_adc_iio *info = iio_priv(indio_dev);
> +	int size = 12;
> +
> +	/*
> +	 *N.B.:  Unlike the Chinese datasheets tell, the charging current is
Space before the N
> +	 * stored on 12 bits, not 13 bits. Only discharging current is on 13
> +	 * bits.
> +	 */
> +	if (chan->type == IIO_CURRENT && chan->channel == AXP20X_BATT_DISCHRG_I)
> +		size = 13;
> +	else
> +		size = 12;
> +
> +	*val = axp20x_read_variable_width(info->regmap, chan->address, size);
> +	if (*val < 0)
> +		return *val;
> +
> +	return IIO_VAL_INT;
> +}
> +
> +static int axp22x_adc_raw(struct iio_dev *indio_dev,
> +			  struct iio_chan_spec const *chan, int *val)
> +{
> +	struct axp20x_adc_iio *info = iio_priv(indio_dev);
> +	int size;
> +
> +	/*
> +	 * N.B.: Unlike the Chinese datasheets tell, the charging current is
> +	 * stored on 12 bits, not 13 bits. Only discharging current is on 13
> +	 * bits.
> +	 */
> +	if (chan->type == IIO_CURRENT && chan->channel == AXP22X_BATT_DISCHRG_I)
> +		size = 13;
> +	else
> +		size = 12;
> +
> +	*val = axp20x_read_variable_width(info->regmap, chan->address, size);
> +	if (*val < 0)
> +		return *val;
> +
> +	return IIO_VAL_INT;
> +}
> +
> +static int axp20x_adc_scale_voltage(int channel, int *val, int *val2)
> +{
> +	switch (channel) {
> +	case AXP20X_ACIN_V:
> +	case AXP20X_VBUS_V:
> +		*val = 1;
> +		*val2 = 700000;
> +		return IIO_VAL_INT_PLUS_MICRO;
> +
> +	case AXP20X_GPIO0_V:
> +	case AXP20X_GPIO1_V:
> +		*val = 0;
> +		*val2 = 500000;
> +		return IIO_VAL_INT_PLUS_MICRO;
> +
> +	case AXP20X_BATT_V:
> +		*val = 1;
> +		*val2 = 100000;
> +		return IIO_VAL_INT_PLUS_MICRO;
> +
> +	case AXP20X_IPSOUT_V:
> +		*val = 1;
> +		*val2 = 400000;
> +		return IIO_VAL_INT_PLUS_MICRO;
> +
> +	default:
> +		return -EINVAL;
> +	}
> +}
> +
> +static int axp20x_adc_scale_current(int channel, int *val, int *val2)
> +{
> +	switch (channel) {
> +	case AXP20X_ACIN_I:
> +		*val = 0;
> +		*val2 = 625000;
> +		return IIO_VAL_INT_PLUS_MICRO;
> +
> +	case AXP20X_VBUS_I:
> +		*val = 0;
> +		*val2 = 375000;
> +		return IIO_VAL_INT_PLUS_MICRO;
> +
> +	case AXP20X_BATT_DISCHRG_I:
> +	case AXP20X_BATT_CHRG_I:
> +		*val = 0;
> +		*val2 = 500000;
> +		return IIO_VAL_INT_PLUS_MICRO;
> +
> +	default:
> +		return -EINVAL;
> +	}
> +}
> +
> +static int axp20x_adc_scale(struct iio_chan_spec const *chan, int *val,
> +			    int *val2)
> +{
> +	switch (chan->type) {
> +	case IIO_VOLTAGE:
> +		return axp20x_adc_scale_voltage(chan->channel, val, val2);
> +
> +	case IIO_CURRENT:
> +		return axp20x_adc_scale_current(chan->channel, val, val2);
> +
> +	case IIO_TEMP:
> +		*val = 100;
> +		return IIO_VAL_INT;
> +
> +	default:
> +		return -EINVAL;
> +	}
> +}
> +
> +static int axp22x_adc_scale(struct iio_chan_spec const *chan, int *val,
> +			    int *val2)
> +{
> +	switch (chan->type) {
> +	case IIO_VOLTAGE:
> +		if (chan->channel != AXP22X_BATT_V)
> +			return -EINVAL;
> +
> +		*val = 1;
> +		*val2 = 100000;
> +		return IIO_VAL_INT_PLUS_MICRO;
> +
> +	case IIO_CURRENT:
> +		*val = 0;
> +		*val2 = 500000;
> +		return IIO_VAL_INT_PLUS_MICRO;
> +
> +	case IIO_TEMP:
> +		*val = 100;
> +		return IIO_VAL_INT;
> +
> +	default:
> +		return -EINVAL;
> +	}
> +}
> +
> +static int axp20x_adc_offset_voltage(struct iio_dev *indio_dev, int channel,
> +				     int *val)
> +{
> +	struct axp20x_adc_iio *info = iio_priv(indio_dev);
> +	int ret, reg;
> +
> +	if (channel != AXP20X_GPIO0_V && channel != AXP20X_GPIO1_V)
> +		return -EINVAL;
Again, you could mess with the ordering here to only check the channel
value once. (i.e. use a switch after the register read). Might be ever
so slightly tidier.  Up to you.
> +
> +	ret = regmap_read(info->regmap, AXP20X_GPIO10_IN_RANGE, &reg);
> +	if (ret < 0)
> +		return ret;
> +
> +	if (channel == AXP20X_GPIO0_V)
> +		*val = reg & AXP20X_GPIO10_IN_RANGE_GPIO0;
> +	else
> +		*val = reg & AXP20X_GPIO10_IN_RANGE_GPIO1;
> +
> +	*val = !!(*val) * 700000;
> +
> +	return IIO_VAL_INT;
> +}
> +
> +static int axp20x_adc_offset(struct iio_dev *indio_dev,
> +			     struct iio_chan_spec const *chan, int *val)
> +{
> +	switch (chan->type) {
> +	case IIO_VOLTAGE:
> +		return axp20x_adc_offset_voltage(indio_dev, chan->channel, val);
> +
> +	case IIO_TEMP:
> +		*val = -1447;
> +		return IIO_VAL_INT;
> +
> +	default:
> +		return -EINVAL;
> +	}
> +}
> +
> +static int axp20x_read_raw(struct iio_dev *indio_dev,
> +			   struct iio_chan_spec const *chan, int *val,
> +			   int *val2, long mask)
> +{
> +	switch (mask) {
> +	case IIO_CHAN_INFO_OFFSET:
> +		return axp20x_adc_offset(indio_dev, chan, val);
> +
> +	case IIO_CHAN_INFO_SCALE:
> +		return axp20x_adc_scale(chan, val, val2);
> +
> +	case IIO_CHAN_INFO_RAW:
> +		return axp20x_adc_raw(indio_dev, chan, val);
> +
> +	default:
> +		return -EINVAL;
> +	}
> +}
> +
> +static int axp22x_read_raw(struct iio_dev *indio_dev,
> +			   struct iio_chan_spec const *chan, int *val,
> +			   int *val2, long mask)
> +{
> +	switch (mask) {
> +	case IIO_CHAN_INFO_OFFSET:
> +		*val = -2677;
> +		return IIO_VAL_INT;
> +
> +	case IIO_CHAN_INFO_SCALE:
> +		return axp22x_adc_scale(chan, val, val2);
> +
> +	case IIO_CHAN_INFO_RAW:
> +		return axp22x_adc_raw(indio_dev, chan, val);
> +
> +	default:
> +		return -EINVAL;
> +	}
> +}
> +
> +static int axp20x_write_raw(struct iio_dev *indio_dev,
> +			    struct iio_chan_spec const *chan, int val, int val2,
> +			    long mask)
> +{
> +	struct axp20x_adc_iio *info = iio_priv(indio_dev);
> +
> +	/*
> +	 * The AXP20X PMIC allows the user to choose between 0V and 0.7V offsets
> +	 * for (independently) GPIO0 and GPIO1 when in ADC mode.
> +	 */
> +	if (mask != IIO_CHAN_INFO_OFFSET)
> +		return -EINVAL;
> +
> +	if (chan->channel != AXP20X_GPIO0_V && chan->channel != AXP20X_GPIO1_V)
> +		return -EINVAL;
Might be a tiny bit nicer to move this down to where you are chosing between
the two channels, then make that a switch with a default that returns -EINVAL.
> +
> +	if (val != 0 && val != 700000)
> +		return -EINVAL;
> +
> +	if (chan->channel == AXP20X_GPIO0_V)
> +		return regmap_update_bits(info->regmap, AXP20X_GPIO10_IN_RANGE,
> +					  AXP20X_GPIO10_IN_RANGE_GPIO0,
> +					  AXP20X_GPIO10_IN_RANGE_GPIO0_VAL(!!val));
> +
> +	return regmap_update_bits(info->regmap, AXP20X_GPIO10_IN_RANGE,
> +				  AXP20X_GPIO10_IN_RANGE_GPIO1,
> +				  AXP20X_GPIO10_IN_RANGE_GPIO1_VAL(!!val));
> +}
> +
> +static const struct iio_info axp20x_adc_iio_info = {
> +	.read_raw = axp20x_read_raw,
> +	.write_raw = axp20x_write_raw,
> +	.driver_module = THIS_MODULE,
> +};
> +
> +static const struct iio_info axp22x_adc_iio_info = {
> +	.read_raw = axp22x_read_raw,
> +	.driver_module = THIS_MODULE,
> +};
> +
> +static int axp20x_adc_rate(int rate)
> +{
> +	return AXP20X_ADC_RATE_HZ(rate);
> +}
> +
> +static int axp22x_adc_rate(int rate)
> +{
> +	return AXP22X_ADC_RATE_HZ(rate);
> +}
> +
> +struct axp_data {
> +	const struct iio_info		*iio_info;
> +	int				num_channels;
> +	struct iio_chan_spec const	*channels;
> +	unsigned long			adc_en1_mask;
> +	int				(*adc_rate)(int rate);
> +	bool				adc_en2;
> +	struct iio_map			*maps;
> +};
> +
> +static const struct axp_data axp20x_data = {
> +	.iio_info = &axp20x_adc_iio_info,
> +	.num_channels = ARRAY_SIZE(axp20x_adc_channels),
> +	.channels = axp20x_adc_channels,
> +	.adc_en1_mask = AXP20X_ADC_EN1_MASK,
> +	.adc_rate = axp20x_adc_rate,
> +	.adc_en2 = true,
> +	.maps = axp20x_maps,
> +};
> +
> +static const struct axp_data axp22x_data = {
> +	.iio_info = &axp22x_adc_iio_info,
> +	.num_channels = ARRAY_SIZE(axp22x_adc_channels),
> +	.channels = axp22x_adc_channels,
> +	.adc_en1_mask = AXP22X_ADC_EN1_MASK,
> +	.adc_rate = axp22x_adc_rate,
> +	.adc_en2 = false,
> +	.maps = axp22x_maps,
> +};
> +
> +static const struct platform_device_id axp20x_adc_id_match[] = {
> +	{ .name = "axp20x-adc", .driver_data = (kernel_ulong_t)&axp20x_data, },
> +	{ .name = "axp22x-adc", .driver_data = (kernel_ulong_t)&axp22x_data, },
> +	{ /* sentinel */ },
> +};
> +
> +static int axp20x_probe(struct platform_device *pdev)
> +{
> +	struct axp20x_adc_iio *info;
> +	struct iio_dev *indio_dev;
> +	struct axp20x_dev *axp20x_dev;
> +	int ret;
> +
> +	axp20x_dev = dev_get_drvdata(pdev->dev.parent);
> +
> +	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*info));
> +	if (!indio_dev)
> +		return -ENOMEM;
> +
> +	info = iio_priv(indio_dev);
> +	platform_set_drvdata(pdev, indio_dev);
> +
> +	info->regmap = axp20x_dev->regmap;
> +	indio_dev->dev.parent = &pdev->dev;
> +	indio_dev->dev.of_node = pdev->dev.of_node;
> +	indio_dev->modes = INDIO_DIRECT_MODE;
> +
> +	info->data = (struct axp_data *)platform_get_device_id(pdev)->driver_data;
I think this is only actually used in probe, so could be handled more
neatly with a local variable rather than sticking it in info.

> +
> +	indio_dev->name = platform_get_device_id(pdev)->name;
> +	indio_dev->info = info->data->iio_info;
> +	indio_dev->num_channels = info->data->num_channels;
> +	indio_dev->channels = info->data->channels;
> +
> +	/* Enable the ADCs on IP */
> +	regmap_write(info->regmap, AXP20X_ADC_EN1, info->data->adc_en1_mask);
> +
> +	if (info->data->adc_en2)
> +		/* Enable GPIO0/1 and internal temperature ADCs */
> +		regmap_update_bits(info->regmap, AXP20X_ADC_EN2,
> +				   AXP20X_ADC_EN2_MASK, AXP20X_ADC_EN2_MASK);
> +
> +	/* Configure ADCs rate */
> +	regmap_update_bits(info->regmap, AXP20X_ADC_RATE, AXP20X_ADC_RATE_MASK,
> +			   info->data->adc_rate(100));
> +
> +	ret = iio_map_array_register(indio_dev, info->data->maps);
> +	if (ret < 0) {
> +		dev_err(&pdev->dev, "failed to register IIO maps: %d\n", ret);
> +		goto fail_map;
> +	}
> +
> +	ret = iio_device_register(indio_dev);
> +	if (ret < 0) {
> +		dev_err(&pdev->dev, "could not register the device\n");
> +		goto fail_register;
> +	}
> +
> +	return 0;
> +
> +fail_register:
> +	iio_map_array_unregister(indio_dev);
> +
> +fail_map:
> +	regmap_write(info->regmap, AXP20X_ADC_EN1, 0);
> +
> +	if (info->data->adc_en2)
> +		regmap_write(info->regmap, AXP20X_ADC_EN2, 0);
> +
> +	return ret;
> +}
> +
> +static int axp20x_remove(struct platform_device *pdev)
> +{
> +	struct axp20x_adc_iio *info;
> +	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
> +
> +	info = iio_priv(indio_dev);
Reorder the two lines above this one and then you could just do this
inline. (really trivial so feel free to not bother ;)
> +
> +	iio_device_unregister(indio_dev);
> +	iio_map_array_unregister(indio_dev);
> +
> +	regmap_write(info->regmap, AXP20X_ADC_EN1, 0);
> +
> +	if (info->data->adc_en2)
> +		regmap_write(info->regmap, AXP20X_ADC_EN2, 0);
> +
> +	return 0;
> +}
> +
> +static struct platform_driver axp20x_adc_driver = {
> +	.driver = {
> +		.name = "axp20x-adc",
> +	},
> +	.id_table = axp20x_adc_id_match,
> +	.probe = axp20x_probe,
> +	.remove = axp20x_remove,
> +};
> +
> +module_platform_driver(axp20x_adc_driver);
> +
> +MODULE_DESCRIPTION("ADC driver for AXP20X and AXP22X PMICs");
> +MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
> +MODULE_LICENSE("GPL");
> 

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 04/18] iio: adc: add support for X-Powers AXP20X and AXP22X PMICs ADCs
  2017-02-14  9:40   ` Quentin Schulz
  (?)
@ 2017-02-21  4:32     ` Chen-Yu Tsai
  -1 siblings, 0 replies; 126+ messages in thread
From: Chen-Yu Tsai @ 2017-02-21  4:32 UTC (permalink / raw)
  To: Quentin Schulz
  Cc: knaack.h, Lars-Peter Clausen, Peter Meerwald-Stadler,
	Rob Herring, Mark Rutland, Chen-Yu Tsai, Sebastian Reichel,
	Russell King, Maxime Ripard, Lee Jones, linux-iio, devicetree,
	linux-kernel, open list:THERMAL, linux-arm-kernel,
	Thomas Petazzoni, Icenowy Zheng, Bruno Prémont

On Tue, Feb 14, 2017 at 5:40 PM, Quentin Schulz
<quentin.schulz@free-electrons.com> wrote:
> The X-Powers AXP20X and AXP22X PMICs have multiple ADCs. They expose the
> battery voltage, battery charge and discharge currents, AC-in and VBUS
> voltages and currents, 2 GPIOs muxable in ADC mode and PMIC temperature.
>
> This adds support for most of AXP20X and AXP22X ADCs.
>
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
> ---
>
> v3:
>  - moved from switch to if condition in axp20x_adc_raw and
>  axp22x_adc_raw,
>  - removed DT support as DT node has been dropped,
>    - use of platform_device_id
>  - correctly defined the name of the iio device (name used to probe the
>  driver),
>  - added goto for errors in probe,
>  - added iio_map_array_unregister to the remove function,
>
> v2:
>  - removed unused defines,
>  - changed BIT(x) to 1 << x when describing bits purpose for which 2 <<
>  x or 3 << x exists, to be consistent,
>  - changed ADC rate defines to macro formulas,
>  - reordered IIO channels, now different measures (current/voltage) of
>  the same part of the PMIC (e.g. battery), have the same IIO channel in
>  their respective IIO type. When a part of the PMIC have only one
>  measure, a number is jumped,
>  - left IIO channel mapping in DT to use iio_map structure,
>  - removed indexing of ADC internal temperature,
>  - removed unused iio_dev structure in axp20x_adc_iio,
>  - added a structure for data specific to AXP20X or AXP22X PMICs instead
>  of using an ID and an if condition when needing to separate the
>  behaviour of both,
>  - added a comment on batt_chrg_i really being on 12bits rather than
>  what the Chinese datasheets say (13 bits),
>  - corrected the offset for AXP22X PMIC temperature,
>  - set the ADC rate to a value (100Hz) shared by the AXP20X and AXP22X,
>  - created macro formulas to compute the ADC rate for each,
>  - added a condition on presence of ADC_EN2 reg before setting/resetting
>  it,
>  - switched from devm_iio_device_unregister to the non-devm function
>  because of the need for a remove function,
>  - removed some dead code,
>
>  drivers/iio/adc/Kconfig      |  10 +
>  drivers/iio/adc/Makefile     |   1 +
>  drivers/iio/adc/axp20x_adc.c | 606 +++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 617 insertions(+)
>  create mode 100644 drivers/iio/adc/axp20x_adc.c
>
> diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
> index 9c8b558..ed17fe1 100644
> --- a/drivers/iio/adc/Kconfig
> +++ b/drivers/iio/adc/Kconfig
> @@ -154,6 +154,16 @@ config AT91_SAMA5D2_ADC
>           To compile this driver as a module, choose M here: the module will be
>           called at91-sama5d2_adc.
>
> +config AXP20X_ADC
> +       tristate "X-Powers AXP20X and AXP22X ADC driver"
> +       depends on MFD_AXP20X
> +       help
> +         Say yes here to have support for X-Powers power management IC (PMIC)
> +         AXP20X and AXP22X ADC devices.
> +
> +         To compile this driver as a module, choose M here: the module will be
> +         called axp20x_adc.
> +
>  config AXP288_ADC
>         tristate "X-Powers AXP288 ADC driver"
>         depends on MFD_AXP20X
> diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile
> index d36c4be..f5c28a5 100644
> --- a/drivers/iio/adc/Makefile
> +++ b/drivers/iio/adc/Makefile
> @@ -16,6 +16,7 @@ obj-$(CONFIG_AD7887) += ad7887.o
>  obj-$(CONFIG_AD799X) += ad799x.o
>  obj-$(CONFIG_AT91_ADC) += at91_adc.o
>  obj-$(CONFIG_AT91_SAMA5D2_ADC) += at91-sama5d2_adc.o
> +obj-$(CONFIG_AXP20X_ADC) += axp20x_adc.o
>  obj-$(CONFIG_AXP288_ADC) += axp288_adc.o
>  obj-$(CONFIG_BCM_IPROC_ADC) += bcm_iproc_adc.o
>  obj-$(CONFIG_BERLIN2_ADC) += berlin2-adc.o
> diff --git a/drivers/iio/adc/axp20x_adc.c b/drivers/iio/adc/axp20x_adc.c
> new file mode 100644
> index 0000000..5ef6af8
> --- /dev/null
> +++ b/drivers/iio/adc/axp20x_adc.c
> @@ -0,0 +1,606 @@

[...]

> +
> +static const struct platform_device_id axp20x_adc_id_match[] = {
> +       { .name = "axp20x-adc", .driver_data = (kernel_ulong_t)&axp20x_data, },
> +       { .name = "axp22x-adc", .driver_data = (kernel_ulong_t)&axp22x_data, },
> +       { /* sentinel */ },
> +};

MODULE_DEVICE_TABLE(platform, axp20x_adc_id_match);

So module autoloading works. Otherwise,

Acked-by: Chen-Yu Tsai <wens@csie.org>

> +
> +static int axp20x_probe(struct platform_device *pdev)
> +{
> +       struct axp20x_adc_iio *info;
> +       struct iio_dev *indio_dev;
> +       struct axp20x_dev *axp20x_dev;
> +       int ret;
> +
> +       axp20x_dev = dev_get_drvdata(pdev->dev.parent);
> +
> +       indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*info));
> +       if (!indio_dev)
> +               return -ENOMEM;
> +
> +       info = iio_priv(indio_dev);
> +       platform_set_drvdata(pdev, indio_dev);
> +
> +       info->regmap = axp20x_dev->regmap;
> +       indio_dev->dev.parent = &pdev->dev;
> +       indio_dev->dev.of_node = pdev->dev.of_node;
> +       indio_dev->modes = INDIO_DIRECT_MODE;
> +
> +       info->data = (struct axp_data *)platform_get_device_id(pdev)->driver_data;
> +
> +       indio_dev->name = platform_get_device_id(pdev)->name;
> +       indio_dev->info = info->data->iio_info;
> +       indio_dev->num_channels = info->data->num_channels;
> +       indio_dev->channels = info->data->channels;
> +
> +       /* Enable the ADCs on IP */
> +       regmap_write(info->regmap, AXP20X_ADC_EN1, info->data->adc_en1_mask);
> +
> +       if (info->data->adc_en2)
> +               /* Enable GPIO0/1 and internal temperature ADCs */
> +               regmap_update_bits(info->regmap, AXP20X_ADC_EN2,
> +                                  AXP20X_ADC_EN2_MASK, AXP20X_ADC_EN2_MASK);
> +
> +       /* Configure ADCs rate */
> +       regmap_update_bits(info->regmap, AXP20X_ADC_RATE, AXP20X_ADC_RATE_MASK,
> +                          info->data->adc_rate(100));
> +
> +       ret = iio_map_array_register(indio_dev, info->data->maps);
> +       if (ret < 0) {
> +               dev_err(&pdev->dev, "failed to register IIO maps: %d\n", ret);
> +               goto fail_map;
> +       }
> +
> +       ret = iio_device_register(indio_dev);
> +       if (ret < 0) {
> +               dev_err(&pdev->dev, "could not register the device\n");
> +               goto fail_register;
> +       }
> +
> +       return 0;
> +
> +fail_register:
> +       iio_map_array_unregister(indio_dev);
> +
> +fail_map:
> +       regmap_write(info->regmap, AXP20X_ADC_EN1, 0);
> +
> +       if (info->data->adc_en2)
> +               regmap_write(info->regmap, AXP20X_ADC_EN2, 0);
> +
> +       return ret;
> +}
> +
> +static int axp20x_remove(struct platform_device *pdev)
> +{
> +       struct axp20x_adc_iio *info;
> +       struct iio_dev *indio_dev = platform_get_drvdata(pdev);
> +
> +       info = iio_priv(indio_dev);
> +
> +       iio_device_unregister(indio_dev);
> +       iio_map_array_unregister(indio_dev);
> +
> +       regmap_write(info->regmap, AXP20X_ADC_EN1, 0);
> +
> +       if (info->data->adc_en2)
> +               regmap_write(info->regmap, AXP20X_ADC_EN2, 0);
> +
> +       return 0;
> +}
> +
> +static struct platform_driver axp20x_adc_driver = {
> +       .driver = {
> +               .name = "axp20x-adc",
> +       },
> +       .id_table = axp20x_adc_id_match,
> +       .probe = axp20x_probe,
> +       .remove = axp20x_remove,
> +};
> +
> +module_platform_driver(axp20x_adc_driver);
> +
> +MODULE_DESCRIPTION("ADC driver for AXP20X and AXP22X PMICs");
> +MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
> +MODULE_LICENSE("GPL");
> --
> 2.9.3
>

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 04/18] iio: adc: add support for X-Powers AXP20X and AXP22X PMICs ADCs
@ 2017-02-21  4:32     ` Chen-Yu Tsai
  0 siblings, 0 replies; 126+ messages in thread
From: Chen-Yu Tsai @ 2017-02-21  4:32 UTC (permalink / raw)
  To: Quentin Schulz
  Cc: Mark Rutland, devicetree, Lars-Peter Clausen, open list:THERMAL,
	linux-iio, linux-kernel, Sebastian Reichel, Russell King,
	Chen-Yu Tsai, Rob Herring, Icenowy Zheng, Peter Meerwald-Stadler,
	knaack.h, Maxime Ripard, Bruno Prémont, Lee Jones,
	Thomas Petazzoni, linux-arm-kernel

On Tue, Feb 14, 2017 at 5:40 PM, Quentin Schulz
<quentin.schulz@free-electrons.com> wrote:
> The X-Powers AXP20X and AXP22X PMICs have multiple ADCs. They expose the
> battery voltage, battery charge and discharge currents, AC-in and VBUS
> voltages and currents, 2 GPIOs muxable in ADC mode and PMIC temperature.
>
> This adds support for most of AXP20X and AXP22X ADCs.
>
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
> ---
>
> v3:
>  - moved from switch to if condition in axp20x_adc_raw and
>  axp22x_adc_raw,
>  - removed DT support as DT node has been dropped,
>    - use of platform_device_id
>  - correctly defined the name of the iio device (name used to probe the
>  driver),
>  - added goto for errors in probe,
>  - added iio_map_array_unregister to the remove function,
>
> v2:
>  - removed unused defines,
>  - changed BIT(x) to 1 << x when describing bits purpose for which 2 <<
>  x or 3 << x exists, to be consistent,
>  - changed ADC rate defines to macro formulas,
>  - reordered IIO channels, now different measures (current/voltage) of
>  the same part of the PMIC (e.g. battery), have the same IIO channel in
>  their respective IIO type. When a part of the PMIC have only one
>  measure, a number is jumped,
>  - left IIO channel mapping in DT to use iio_map structure,
>  - removed indexing of ADC internal temperature,
>  - removed unused iio_dev structure in axp20x_adc_iio,
>  - added a structure for data specific to AXP20X or AXP22X PMICs instead
>  of using an ID and an if condition when needing to separate the
>  behaviour of both,
>  - added a comment on batt_chrg_i really being on 12bits rather than
>  what the Chinese datasheets say (13 bits),
>  - corrected the offset for AXP22X PMIC temperature,
>  - set the ADC rate to a value (100Hz) shared by the AXP20X and AXP22X,
>  - created macro formulas to compute the ADC rate for each,
>  - added a condition on presence of ADC_EN2 reg before setting/resetting
>  it,
>  - switched from devm_iio_device_unregister to the non-devm function
>  because of the need for a remove function,
>  - removed some dead code,
>
>  drivers/iio/adc/Kconfig      |  10 +
>  drivers/iio/adc/Makefile     |   1 +
>  drivers/iio/adc/axp20x_adc.c | 606 +++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 617 insertions(+)
>  create mode 100644 drivers/iio/adc/axp20x_adc.c
>
> diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
> index 9c8b558..ed17fe1 100644
> --- a/drivers/iio/adc/Kconfig
> +++ b/drivers/iio/adc/Kconfig
> @@ -154,6 +154,16 @@ config AT91_SAMA5D2_ADC
>           To compile this driver as a module, choose M here: the module will be
>           called at91-sama5d2_adc.
>
> +config AXP20X_ADC
> +       tristate "X-Powers AXP20X and AXP22X ADC driver"
> +       depends on MFD_AXP20X
> +       help
> +         Say yes here to have support for X-Powers power management IC (PMIC)
> +         AXP20X and AXP22X ADC devices.
> +
> +         To compile this driver as a module, choose M here: the module will be
> +         called axp20x_adc.
> +
>  config AXP288_ADC
>         tristate "X-Powers AXP288 ADC driver"
>         depends on MFD_AXP20X
> diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile
> index d36c4be..f5c28a5 100644
> --- a/drivers/iio/adc/Makefile
> +++ b/drivers/iio/adc/Makefile
> @@ -16,6 +16,7 @@ obj-$(CONFIG_AD7887) += ad7887.o
>  obj-$(CONFIG_AD799X) += ad799x.o
>  obj-$(CONFIG_AT91_ADC) += at91_adc.o
>  obj-$(CONFIG_AT91_SAMA5D2_ADC) += at91-sama5d2_adc.o
> +obj-$(CONFIG_AXP20X_ADC) += axp20x_adc.o
>  obj-$(CONFIG_AXP288_ADC) += axp288_adc.o
>  obj-$(CONFIG_BCM_IPROC_ADC) += bcm_iproc_adc.o
>  obj-$(CONFIG_BERLIN2_ADC) += berlin2-adc.o
> diff --git a/drivers/iio/adc/axp20x_adc.c b/drivers/iio/adc/axp20x_adc.c
> new file mode 100644
> index 0000000..5ef6af8
> --- /dev/null
> +++ b/drivers/iio/adc/axp20x_adc.c
> @@ -0,0 +1,606 @@

[...]

> +
> +static const struct platform_device_id axp20x_adc_id_match[] = {
> +       { .name = "axp20x-adc", .driver_data = (kernel_ulong_t)&axp20x_data, },
> +       { .name = "axp22x-adc", .driver_data = (kernel_ulong_t)&axp22x_data, },
> +       { /* sentinel */ },
> +};

MODULE_DEVICE_TABLE(platform, axp20x_adc_id_match);

So module autoloading works. Otherwise,

Acked-by: Chen-Yu Tsai <wens@csie.org>

> +
> +static int axp20x_probe(struct platform_device *pdev)
> +{
> +       struct axp20x_adc_iio *info;
> +       struct iio_dev *indio_dev;
> +       struct axp20x_dev *axp20x_dev;
> +       int ret;
> +
> +       axp20x_dev = dev_get_drvdata(pdev->dev.parent);
> +
> +       indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*info));
> +       if (!indio_dev)
> +               return -ENOMEM;
> +
> +       info = iio_priv(indio_dev);
> +       platform_set_drvdata(pdev, indio_dev);
> +
> +       info->regmap = axp20x_dev->regmap;
> +       indio_dev->dev.parent = &pdev->dev;
> +       indio_dev->dev.of_node = pdev->dev.of_node;
> +       indio_dev->modes = INDIO_DIRECT_MODE;
> +
> +       info->data = (struct axp_data *)platform_get_device_id(pdev)->driver_data;
> +
> +       indio_dev->name = platform_get_device_id(pdev)->name;
> +       indio_dev->info = info->data->iio_info;
> +       indio_dev->num_channels = info->data->num_channels;
> +       indio_dev->channels = info->data->channels;
> +
> +       /* Enable the ADCs on IP */
> +       regmap_write(info->regmap, AXP20X_ADC_EN1, info->data->adc_en1_mask);
> +
> +       if (info->data->adc_en2)
> +               /* Enable GPIO0/1 and internal temperature ADCs */
> +               regmap_update_bits(info->regmap, AXP20X_ADC_EN2,
> +                                  AXP20X_ADC_EN2_MASK, AXP20X_ADC_EN2_MASK);
> +
> +       /* Configure ADCs rate */
> +       regmap_update_bits(info->regmap, AXP20X_ADC_RATE, AXP20X_ADC_RATE_MASK,
> +                          info->data->adc_rate(100));
> +
> +       ret = iio_map_array_register(indio_dev, info->data->maps);
> +       if (ret < 0) {
> +               dev_err(&pdev->dev, "failed to register IIO maps: %d\n", ret);
> +               goto fail_map;
> +       }
> +
> +       ret = iio_device_register(indio_dev);
> +       if (ret < 0) {
> +               dev_err(&pdev->dev, "could not register the device\n");
> +               goto fail_register;
> +       }
> +
> +       return 0;
> +
> +fail_register:
> +       iio_map_array_unregister(indio_dev);
> +
> +fail_map:
> +       regmap_write(info->regmap, AXP20X_ADC_EN1, 0);
> +
> +       if (info->data->adc_en2)
> +               regmap_write(info->regmap, AXP20X_ADC_EN2, 0);
> +
> +       return ret;
> +}
> +
> +static int axp20x_remove(struct platform_device *pdev)
> +{
> +       struct axp20x_adc_iio *info;
> +       struct iio_dev *indio_dev = platform_get_drvdata(pdev);
> +
> +       info = iio_priv(indio_dev);
> +
> +       iio_device_unregister(indio_dev);
> +       iio_map_array_unregister(indio_dev);
> +
> +       regmap_write(info->regmap, AXP20X_ADC_EN1, 0);
> +
> +       if (info->data->adc_en2)
> +               regmap_write(info->regmap, AXP20X_ADC_EN2, 0);
> +
> +       return 0;
> +}
> +
> +static struct platform_driver axp20x_adc_driver = {
> +       .driver = {
> +               .name = "axp20x-adc",
> +       },
> +       .id_table = axp20x_adc_id_match,
> +       .probe = axp20x_probe,
> +       .remove = axp20x_remove,
> +};
> +
> +module_platform_driver(axp20x_adc_driver);
> +
> +MODULE_DESCRIPTION("ADC driver for AXP20X and AXP22X PMICs");
> +MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
> +MODULE_LICENSE("GPL");
> --
> 2.9.3
>

^ permalink raw reply	[flat|nested] 126+ messages in thread

* [PATCH v3 04/18] iio: adc: add support for X-Powers AXP20X and AXP22X PMICs ADCs
@ 2017-02-21  4:32     ` Chen-Yu Tsai
  0 siblings, 0 replies; 126+ messages in thread
From: Chen-Yu Tsai @ 2017-02-21  4:32 UTC (permalink / raw)
  To: linux-arm-kernel

On Tue, Feb 14, 2017 at 5:40 PM, Quentin Schulz
<quentin.schulz@free-electrons.com> wrote:
> The X-Powers AXP20X and AXP22X PMICs have multiple ADCs. They expose the
> battery voltage, battery charge and discharge currents, AC-in and VBUS
> voltages and currents, 2 GPIOs muxable in ADC mode and PMIC temperature.
>
> This adds support for most of AXP20X and AXP22X ADCs.
>
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
> ---
>
> v3:
>  - moved from switch to if condition in axp20x_adc_raw and
>  axp22x_adc_raw,
>  - removed DT support as DT node has been dropped,
>    - use of platform_device_id
>  - correctly defined the name of the iio device (name used to probe the
>  driver),
>  - added goto for errors in probe,
>  - added iio_map_array_unregister to the remove function,
>
> v2:
>  - removed unused defines,
>  - changed BIT(x) to 1 << x when describing bits purpose for which 2 <<
>  x or 3 << x exists, to be consistent,
>  - changed ADC rate defines to macro formulas,
>  - reordered IIO channels, now different measures (current/voltage) of
>  the same part of the PMIC (e.g. battery), have the same IIO channel in
>  their respective IIO type. When a part of the PMIC have only one
>  measure, a number is jumped,
>  - left IIO channel mapping in DT to use iio_map structure,
>  - removed indexing of ADC internal temperature,
>  - removed unused iio_dev structure in axp20x_adc_iio,
>  - added a structure for data specific to AXP20X or AXP22X PMICs instead
>  of using an ID and an if condition when needing to separate the
>  behaviour of both,
>  - added a comment on batt_chrg_i really being on 12bits rather than
>  what the Chinese datasheets say (13 bits),
>  - corrected the offset for AXP22X PMIC temperature,
>  - set the ADC rate to a value (100Hz) shared by the AXP20X and AXP22X,
>  - created macro formulas to compute the ADC rate for each,
>  - added a condition on presence of ADC_EN2 reg before setting/resetting
>  it,
>  - switched from devm_iio_device_unregister to the non-devm function
>  because of the need for a remove function,
>  - removed some dead code,
>
>  drivers/iio/adc/Kconfig      |  10 +
>  drivers/iio/adc/Makefile     |   1 +
>  drivers/iio/adc/axp20x_adc.c | 606 +++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 617 insertions(+)
>  create mode 100644 drivers/iio/adc/axp20x_adc.c
>
> diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
> index 9c8b558..ed17fe1 100644
> --- a/drivers/iio/adc/Kconfig
> +++ b/drivers/iio/adc/Kconfig
> @@ -154,6 +154,16 @@ config AT91_SAMA5D2_ADC
>           To compile this driver as a module, choose M here: the module will be
>           called at91-sama5d2_adc.
>
> +config AXP20X_ADC
> +       tristate "X-Powers AXP20X and AXP22X ADC driver"
> +       depends on MFD_AXP20X
> +       help
> +         Say yes here to have support for X-Powers power management IC (PMIC)
> +         AXP20X and AXP22X ADC devices.
> +
> +         To compile this driver as a module, choose M here: the module will be
> +         called axp20x_adc.
> +
>  config AXP288_ADC
>         tristate "X-Powers AXP288 ADC driver"
>         depends on MFD_AXP20X
> diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile
> index d36c4be..f5c28a5 100644
> --- a/drivers/iio/adc/Makefile
> +++ b/drivers/iio/adc/Makefile
> @@ -16,6 +16,7 @@ obj-$(CONFIG_AD7887) += ad7887.o
>  obj-$(CONFIG_AD799X) += ad799x.o
>  obj-$(CONFIG_AT91_ADC) += at91_adc.o
>  obj-$(CONFIG_AT91_SAMA5D2_ADC) += at91-sama5d2_adc.o
> +obj-$(CONFIG_AXP20X_ADC) += axp20x_adc.o
>  obj-$(CONFIG_AXP288_ADC) += axp288_adc.o
>  obj-$(CONFIG_BCM_IPROC_ADC) += bcm_iproc_adc.o
>  obj-$(CONFIG_BERLIN2_ADC) += berlin2-adc.o
> diff --git a/drivers/iio/adc/axp20x_adc.c b/drivers/iio/adc/axp20x_adc.c
> new file mode 100644
> index 0000000..5ef6af8
> --- /dev/null
> +++ b/drivers/iio/adc/axp20x_adc.c
> @@ -0,0 +1,606 @@

[...]

> +
> +static const struct platform_device_id axp20x_adc_id_match[] = {
> +       { .name = "axp20x-adc", .driver_data = (kernel_ulong_t)&axp20x_data, },
> +       { .name = "axp22x-adc", .driver_data = (kernel_ulong_t)&axp22x_data, },
> +       { /* sentinel */ },
> +};

MODULE_DEVICE_TABLE(platform, axp20x_adc_id_match);

So module autoloading works. Otherwise,

Acked-by: Chen-Yu Tsai <wens@csie.org>

> +
> +static int axp20x_probe(struct platform_device *pdev)
> +{
> +       struct axp20x_adc_iio *info;
> +       struct iio_dev *indio_dev;
> +       struct axp20x_dev *axp20x_dev;
> +       int ret;
> +
> +       axp20x_dev = dev_get_drvdata(pdev->dev.parent);
> +
> +       indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*info));
> +       if (!indio_dev)
> +               return -ENOMEM;
> +
> +       info = iio_priv(indio_dev);
> +       platform_set_drvdata(pdev, indio_dev);
> +
> +       info->regmap = axp20x_dev->regmap;
> +       indio_dev->dev.parent = &pdev->dev;
> +       indio_dev->dev.of_node = pdev->dev.of_node;
> +       indio_dev->modes = INDIO_DIRECT_MODE;
> +
> +       info->data = (struct axp_data *)platform_get_device_id(pdev)->driver_data;
> +
> +       indio_dev->name = platform_get_device_id(pdev)->name;
> +       indio_dev->info = info->data->iio_info;
> +       indio_dev->num_channels = info->data->num_channels;
> +       indio_dev->channels = info->data->channels;
> +
> +       /* Enable the ADCs on IP */
> +       regmap_write(info->regmap, AXP20X_ADC_EN1, info->data->adc_en1_mask);
> +
> +       if (info->data->adc_en2)
> +               /* Enable GPIO0/1 and internal temperature ADCs */
> +               regmap_update_bits(info->regmap, AXP20X_ADC_EN2,
> +                                  AXP20X_ADC_EN2_MASK, AXP20X_ADC_EN2_MASK);
> +
> +       /* Configure ADCs rate */
> +       regmap_update_bits(info->regmap, AXP20X_ADC_RATE, AXP20X_ADC_RATE_MASK,
> +                          info->data->adc_rate(100));
> +
> +       ret = iio_map_array_register(indio_dev, info->data->maps);
> +       if (ret < 0) {
> +               dev_err(&pdev->dev, "failed to register IIO maps: %d\n", ret);
> +               goto fail_map;
> +       }
> +
> +       ret = iio_device_register(indio_dev);
> +       if (ret < 0) {
> +               dev_err(&pdev->dev, "could not register the device\n");
> +               goto fail_register;
> +       }
> +
> +       return 0;
> +
> +fail_register:
> +       iio_map_array_unregister(indio_dev);
> +
> +fail_map:
> +       regmap_write(info->regmap, AXP20X_ADC_EN1, 0);
> +
> +       if (info->data->adc_en2)
> +               regmap_write(info->regmap, AXP20X_ADC_EN2, 0);
> +
> +       return ret;
> +}
> +
> +static int axp20x_remove(struct platform_device *pdev)
> +{
> +       struct axp20x_adc_iio *info;
> +       struct iio_dev *indio_dev = platform_get_drvdata(pdev);
> +
> +       info = iio_priv(indio_dev);
> +
> +       iio_device_unregister(indio_dev);
> +       iio_map_array_unregister(indio_dev);
> +
> +       regmap_write(info->regmap, AXP20X_ADC_EN1, 0);
> +
> +       if (info->data->adc_en2)
> +               regmap_write(info->regmap, AXP20X_ADC_EN2, 0);
> +
> +       return 0;
> +}
> +
> +static struct platform_driver axp20x_adc_driver = {
> +       .driver = {
> +               .name = "axp20x-adc",
> +       },
> +       .id_table = axp20x_adc_id_match,
> +       .probe = axp20x_probe,
> +       .remove = axp20x_remove,
> +};
> +
> +module_platform_driver(axp20x_adc_driver);
> +
> +MODULE_DESCRIPTION("ADC driver for AXP20X and AXP22X PMICs");
> +MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
> +MODULE_LICENSE("GPL");
> --
> 2.9.3
>

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 13/18] power: supply: add battery driver for AXP20X and AXP22X PMICs
  2017-02-14  9:41   ` Quentin Schulz
  (?)
@ 2017-02-21  4:44     ` Chen-Yu Tsai
  -1 siblings, 0 replies; 126+ messages in thread
From: Chen-Yu Tsai @ 2017-02-21  4:44 UTC (permalink / raw)
  To: Quentin Schulz
  Cc: knaack.h, Lars-Peter Clausen, Peter Meerwald-Stadler,
	Rob Herring, Mark Rutland, Chen-Yu Tsai, Sebastian Reichel,
	Russell King, Maxime Ripard, Lee Jones, linux-iio, devicetree,
	linux-kernel, open list:THERMAL, linux-arm-kernel,
	Thomas Petazzoni, Icenowy Zheng, Bruno Prémont

On Tue, Feb 14, 2017 at 5:41 PM, Quentin Schulz
<quentin.schulz@free-electrons.com> wrote:
> The X-Powers AXP20X and AXP22X PMICs can have a battery as power supply.
>
> This patch adds the battery power supply driver to get various data from
> the PMIC, such as the battery status (charging, discharging, full,
> dead), current max limit, current current, battery capacity (in
> percentage), voltage max and min limits, current voltage and battery
> capacity (in Ah).
>
> This battery driver uses the AXP20X/AXP22X ADC driver as PMIC data
> provider.
>
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> Acked-by: Jonathan Cameron <jic23@kernel.org>
> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
> ---
>
> v3:
>  - added axp20x_set_voltage_min_design function so it can be reused,
>  - used power_supply_get_battery_info for setting constant charge current
>  instead of x-powers,constant-charge-current introduced in v2,
>  - used power_supply_get_battery_info for setting voltage min design of
>  the battery,
>
> v2:
>  - changed BIT(x) to 1 << x when describing bits purpose for which 2 <<
>  x or 3 << x exists, to be consistent,
>  - switched from POWER_SUPPLY_PROP_CURRENT_MAX to
>  POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
>  - added POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX to the list of
>  readable properties,
>  - replaced µ character by a common u for micro units to make checkpatch
>  happy,
>  - factorized code in axp20x_battery_set_max_voltage,
>  - added a axp20x_set_constant_charge_current function to be used when
>  setting the value from sysfs and from the DT,
>  - removed some dead code,
>  - added a DT property to set constant current charge of the battery
>  (x-powers,constant-charge-current),
>  - migrated to dev_get_regmap instead of manually looking for the regmap
>  in the drvdata of the parent,
>  - switched from int to uintptr_t cast to make sure the cast is always
>  for the same size type (make build on 64bits platforms happy mainly),
>  drivers/power/supply/Kconfig          |  12 +
>  drivers/power/supply/Makefile         |   1 +
>  drivers/power/supply/axp20x_battery.c | 492 ++++++++++++++++++++++++++++++++++
>  3 files changed, 505 insertions(+)
>  create mode 100644 drivers/power/supply/axp20x_battery.c
>
> diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig
> index c552b4b..48619de 100644
> --- a/drivers/power/supply/Kconfig
> +++ b/drivers/power/supply/Kconfig
> @@ -226,6 +226,18 @@ config CHARGER_AXP20X
>           This driver can also be built as a module. If so, the module will be
>           called axp20x_ac_power.
>
> +config BATTERY_AXP20X
> +       tristate "X-Powers AXP20X battery driver"
> +       depends on MFD_AXP20X
> +       depends on AXP20X_ADC
> +       depends on IIO
> +       help
> +         Say Y here to enable support for X-Powers AXP20X PMICs' battery power
> +         supply.
> +
> +         This driver can also be built as a module. If so, the module will be
> +         called axp20x_battery.
> +
>  config AXP288_CHARGER
>         tristate "X-Powers AXP288 Charger"
>         depends on MFD_AXP20X && EXTCON_AXP288
> diff --git a/drivers/power/supply/Makefile b/drivers/power/supply/Makefile
> index 7d22417..5a217b2 100644
> --- a/drivers/power/supply/Makefile
> +++ b/drivers/power/supply/Makefile
> @@ -18,6 +18,7 @@ obj-$(CONFIG_TEST_POWER)      += test_power.o
>
>  obj-$(CONFIG_BATTERY_88PM860X) += 88pm860x_battery.o
>  obj-$(CONFIG_BATTERY_ACT8945A) += act8945a_charger.o
> +obj-$(CONFIG_BATTERY_AXP20X)   += axp20x_battery.o
>  obj-$(CONFIG_CHARGER_AXP20X)   += axp20x_ac_power.o
>  obj-$(CONFIG_BATTERY_DS2760)   += ds2760_battery.o
>  obj-$(CONFIG_BATTERY_DS2780)   += ds2780_battery.o
> diff --git a/drivers/power/supply/axp20x_battery.c b/drivers/power/supply/axp20x_battery.c
> new file mode 100644
> index 0000000..bd16ac6
> --- /dev/null
> +++ b/drivers/power/supply/axp20x_battery.c
> @@ -0,0 +1,492 @@
> +/*
> + * Battery power supply driver for X-Powers AXP20X and AXP22X PMICs
> + *
> + * Copyright 2016 Free Electrons NextThing Co.
> + *     Quentin Schulz <quentin.schulz@free-electrons.com>
> + *
> + * This driver is based on a previous upstreaming attempt by:
> + *     Bruno Prémont <bonbons@linux-vserver.org>
> + *
> + * This file is subject to the terms and conditions of the GNU General
> + * Public License. See the file "COPYING" in the main directory of this
> + * archive for more details.
> + *
> + * 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.
> + */
> +
> +#include <linux/err.h>
> +#include <linux/interrupt.h>
> +#include <linux/irq.h>
> +#include <linux/module.h>
> +#include <linux/of.h>
> +#include <linux/of_device.h>
> +#include <linux/platform_device.h>
> +#include <linux/power_supply.h>
> +#include <linux/regmap.h>
> +#include <linux/slab.h>
> +#include <linux/time.h>
> +#include <linux/iio/iio.h>
> +#include <linux/iio/consumer.h>
> +#include <linux/mfd/axp20x.h>
> +
> +#define AXP20X_PWR_STATUS_BAT_CHARGING BIT(2)
> +
> +#define AXP20X_PWR_OP_BATT_PRESENT     BIT(5)
> +#define AXP20X_PWR_OP_BATT_ACTIVATED   BIT(3)
> +
> +#define AXP209_FG_PERCENT              GENMASK(6, 0)
> +#define AXP22X_FG_VALID                        BIT(7)
> +
> +#define AXP20X_CHRG_CTRL1_TGT_VOLT     GENMASK(6, 5)
> +#define AXP20X_CHRG_CTRL1_TGT_4_1V     (0 << 5)
> +#define AXP20X_CHRG_CTRL1_TGT_4_15V    (1 << 5)
> +#define AXP20X_CHRG_CTRL1_TGT_4_2V     (2 << 5)
> +#define AXP20X_CHRG_CTRL1_TGT_4_36V    (3 << 5)
> +
> +#define AXP22X_CHRG_CTRL1_TGT_4_22V    (1 << 5)
> +#define AXP22X_CHRG_CTRL1_TGT_4_24V    (3 << 5)
> +
> +#define AXP20X_CHRG_CTRL1_TGT_CURR     GENMASK(3, 0)
> +
> +#define AXP20X_V_OFF_MASK              GENMASK(2, 0)
> +
> +struct axp20x_batt_ps {
> +       struct regmap *regmap;
> +       struct power_supply *batt;
> +       struct axp20x_dev *axp20x;
> +       struct iio_channel *batt_chrg_i;
> +       struct iio_channel *batt_dischrg_i;
> +       struct iio_channel *batt_v;
> +       u8 axp_id;
> +};
> +
> +static int axp20x_battery_get_max_voltage(struct axp20x_batt_ps *axp20x_batt,
> +                                         int *val)
> +{
> +       int ret, reg;
> +
> +       ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, &reg);
> +       if (ret)
> +               return ret;
> +
> +       switch (reg & AXP20X_CHRG_CTRL1_TGT_VOLT) {
> +       case AXP20X_CHRG_CTRL1_TGT_4_1V:
> +               *val = 4100000;
> +               break;
> +       case AXP20X_CHRG_CTRL1_TGT_4_15V:
> +               *val = 4150000;
> +               break;
> +       case AXP20X_CHRG_CTRL1_TGT_4_2V:
> +               *val = 4200000;
> +               break;
> +       case AXP20X_CHRG_CTRL1_TGT_4_36V:
> +               *val = 4360000;
> +               break;
> +       default:
> +               return -EINVAL;
> +       }
> +
> +       return 0;
> +}
> +
> +static int axp22x_battery_get_max_voltage(struct axp20x_batt_ps *axp20x_batt,
> +                                         int *val)
> +{
> +       int ret, reg;
> +
> +       ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, &reg);
> +       if (ret)
> +               return ret;
> +
> +       switch (reg & AXP20X_CHRG_CTRL1_TGT_VOLT) {
> +       case AXP20X_CHRG_CTRL1_TGT_4_1V:
> +               *val = 4100000;
> +               break;
> +       case AXP20X_CHRG_CTRL1_TGT_4_2V:
> +               *val = 4200000;
> +               break;
> +       case AXP22X_CHRG_CTRL1_TGT_4_22V:
> +               *val = 4220000;
> +               break;
> +       case AXP22X_CHRG_CTRL1_TGT_4_24V:
> +               *val = 4240000;
> +               break;
> +       default:
> +               return -EINVAL;
> +       }
> +
> +       return 0;
> +}
> +
> +static int axp20x_battery_get_prop(struct power_supply *psy,
> +                                  enum power_supply_property psp,
> +                                  union power_supply_propval *val)
> +{
> +       struct axp20x_batt_ps *axp20x_batt = power_supply_get_drvdata(psy);
> +       struct iio_channel *chan;
> +       int ret = 0, reg, val1;
> +
> +       switch (psp) {
> +       case POWER_SUPPLY_PROP_PRESENT:
> +       case POWER_SUPPLY_PROP_ONLINE:
> +               ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
> +                                 &reg);
> +               if (ret)
> +                       return ret;
> +
> +               val->intval = !!(reg & AXP20X_PWR_OP_BATT_PRESENT);
> +               break;
> +
> +       case POWER_SUPPLY_PROP_STATUS:
> +               ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_INPUT_STATUS,
> +                                 &reg);
> +               if (ret)
> +                       return ret;
> +
> +               if (reg & AXP20X_PWR_STATUS_BAT_CHARGING) {
> +                       val->intval = POWER_SUPPLY_STATUS_CHARGING;
> +                       return 0;
> +               }
> +
> +               ret = iio_read_channel_processed(axp20x_batt->batt_dischrg_i,
> +                                                &val1);
> +               if (ret)
> +                       return ret;
> +
> +               if (val1) {
> +                       val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
> +                       return 0;
> +               }
> +
> +               ret = regmap_read(axp20x_batt->regmap, AXP20X_FG_RES, &val1);
> +               if (ret)
> +                       return ret;
> +
> +               /*
> +                * Fuel Gauge data takes 7 bits but the stored value seems to be
> +                * directly the raw percentage without any scaling to 7 bits.
> +                */
> +               if ((val1 & AXP209_FG_PERCENT) == 100)
> +                       val->intval = POWER_SUPPLY_STATUS_FULL;
> +               else
> +                       val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
> +               break;
> +
> +       case POWER_SUPPLY_PROP_HEALTH:
> +               ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
> +                                 &val1);
> +               if (ret)
> +                       return ret;
> +
> +               if (val1 & AXP20X_PWR_OP_BATT_ACTIVATED) {
> +                       val->intval = POWER_SUPPLY_HEALTH_DEAD;
> +                       return 0;
> +               }
> +
> +               val->intval = POWER_SUPPLY_HEALTH_GOOD;
> +               break;
> +
> +       case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
> +               ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, &reg);
> +               if (ret)
> +                       return ret;
> +
> +               reg &= AXP20X_CHRG_CTRL1_TGT_CURR;
> +               val->intval = reg * 100000 + 300000;
> +               break;
> +
> +       case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
> +               val->intval = AXP20X_CHRG_CTRL1_TGT_CURR * 100000 + 300000;
> +               break;
> +
> +       case POWER_SUPPLY_PROP_CURRENT_NOW:
> +               ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_INPUT_STATUS,
> +                                 &reg);
> +               if (ret)
> +                       return ret;
> +
> +               if (reg & AXP20X_PWR_STATUS_BAT_CHARGING)
> +                       chan = axp20x_batt->batt_chrg_i;
> +               else
> +                       chan = axp20x_batt->batt_dischrg_i;
> +
> +               ret = iio_read_channel_processed(chan, &val->intval);
> +               if (ret)
> +                       return ret;
> +
> +               /* IIO framework gives mA but Power Supply framework gives uA */
> +               val->intval *= 1000;
> +               break;
> +
> +       case POWER_SUPPLY_PROP_CAPACITY:
> +               /* When no battery is present, return capacity is 100% */
> +               ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
> +                                 &reg);
> +               if (ret)
> +                       return ret;
> +
> +               if (!(reg & AXP20X_PWR_OP_BATT_PRESENT)) {
> +                       val->intval = 100;
> +                       return 0;
> +               }
> +
> +               ret = regmap_read(axp20x_batt->regmap, AXP20X_FG_RES, &reg);
> +               if (ret)
> +                       return ret;
> +
> +               if (axp20x_batt->axp_id == AXP221_ID &&
> +                   !(reg & AXP22X_FG_VALID))
> +                       return -EINVAL;
> +
> +               /*
> +                * Fuel Gauge data takes 7 bits but the stored value seems to be
> +                * directly the raw percentage without any scaling to 7 bits.
> +                */
> +               val->intval = reg & AXP209_FG_PERCENT;
> +               break;
> +
> +       case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
> +               if (axp20x_batt->axp_id == AXP209_ID)
> +                       return axp20x_battery_get_max_voltage(axp20x_batt,
> +                                                             &val->intval);
> +               return axp22x_battery_get_max_voltage(axp20x_batt,
> +                                                     &val->intval);
> +
> +       case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
> +               ret = regmap_read(axp20x_batt->regmap, AXP20X_V_OFF, &reg);
> +               if (ret)
> +                       return ret;
> +
> +               val->intval = 2600000 + 100000 * (reg & AXP20X_V_OFF_MASK);
> +               break;
> +
> +       case POWER_SUPPLY_PROP_VOLTAGE_NOW:
> +               ret = iio_read_channel_processed(axp20x_batt->batt_v,
> +                                                &val->intval);
> +               if (ret)
> +                       return ret;
> +
> +               /* IIO framework gives mV but Power Supply framework gives uV */
> +               val->intval *= 1000;
> +               break;
> +
> +       default:
> +               return -EINVAL;
> +       }
> +
> +       return 0;
> +}
> +
> +static int axp20x_battery_set_max_voltage(struct axp20x_batt_ps *axp20x_batt,
> +                                         int val)
> +{
> +       switch (val) {
> +       case 4100000:
> +               val = AXP20X_CHRG_CTRL1_TGT_4_1V;
> +               break;
> +
> +       case 4150000:
> +               if (axp20x_batt->axp_id == AXP221_ID)
> +                       return -EINVAL;
> +
> +               val = AXP20X_CHRG_CTRL1_TGT_4_15V;
> +               break;
> +
> +       case 4200000:
> +               val = AXP20X_CHRG_CTRL1_TGT_4_2V;
> +               break;
> +
> +       default:
> +               /*
> +                * AXP20x max voltage can be set to 4.36V and AXP22X max voltage
> +                * can be set to 4.22V and 4.24V, but these voltages are too
> +                * high for Lithium based batteries (AXP PMICs are supposed to
> +                * be used with these kinds of battery).
> +                */
> +               return -EINVAL;
> +       }
> +
> +       return regmap_update_bits(axp20x_batt->regmap, AXP20X_CHRG_CTRL1,
> +                                 AXP20X_CHRG_CTRL1_TGT_VOLT, val);
> +}
> +
> +static int axp20x_set_constant_charge_current(struct axp20x_batt_ps *axp_batt,
> +                                             int charge_current)
> +{
> +       if (axp_batt->axp_id == AXP209_ID)
> +               charge_current = (charge_current - 300000) / 100000;
> +       else
> +               charge_current = (charge_current - 300000) / 150000;
> +
> +       if (charge_current > AXP20X_CHRG_CTRL1_TGT_CURR || charge_current < 0)
> +               return -EINVAL;

I would add a check or warn if the value to be programmed exceeds that returned
by power_supply_get_battery_info. A charge current exceeding the limits of the
battery is potentially disastrous. The battery may be destroyed or even burst
into flames and explode, taking the board and anything nearby with it. Otherwise

Acked-by: Chen-Yu Tsai <wens@csie.org>

Speaking of power_supply_get_battery_info, is it merged or ready to be merged?

> +
> +       return regmap_update_bits(axp_batt->regmap, AXP20X_CHRG_CTRL1,
> +                                 AXP20X_CHRG_CTRL1_TGT_CURR, charge_current);
> +}
> +
> +static int axp20x_set_voltage_min_design(struct axp20x_batt_ps *axp_batt,
> +                                        int min_voltage)
> +{
> +       int val1 = (min_voltage - 2600000) / 100000;
> +
> +       if (val1 < 0 || val1 > AXP20X_V_OFF_MASK)
> +               return -EINVAL;
> +
> +       return regmap_update_bits(axp_batt->regmap, AXP20X_V_OFF,
> +                                 AXP20X_V_OFF_MASK, val1);
> +}
> +
> +static int axp20x_battery_set_prop(struct power_supply *psy,
> +                                  enum power_supply_property psp,
> +                                  const union power_supply_propval *val)
> +{
> +       struct axp20x_batt_ps *axp20x_batt = power_supply_get_drvdata(psy);
> +
> +       switch (psp) {
> +       case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
> +               return axp20x_set_voltage_min_design(axp20x_batt, val->intval);
> +
> +       case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
> +               return axp20x_battery_set_max_voltage(axp20x_batt, val->intval);
> +
> +       case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
> +               return axp20x_set_constant_charge_current(axp20x_batt,
> +                                                         val->intval);
> +
> +       default:
> +               return -EINVAL;
> +       }
> +}
> +
> +static enum power_supply_property axp20x_battery_props[] = {
> +       POWER_SUPPLY_PROP_PRESENT,
> +       POWER_SUPPLY_PROP_ONLINE,
> +       POWER_SUPPLY_PROP_STATUS,
> +       POWER_SUPPLY_PROP_VOLTAGE_NOW,
> +       POWER_SUPPLY_PROP_CURRENT_NOW,
> +       POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
> +       POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
> +       POWER_SUPPLY_PROP_HEALTH,
> +       POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
> +       POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
> +       POWER_SUPPLY_PROP_CAPACITY,
> +};
> +
> +static int axp20x_battery_prop_writeable(struct power_supply *psy,
> +                                        enum power_supply_property psp)
> +{
> +       return psp == POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN ||
> +              psp == POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN ||
> +              psp == POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT;
> +}
> +
> +static const struct power_supply_desc axp20x_batt_ps_desc = {
> +       .name = "axp20x-battery",
> +       .type = POWER_SUPPLY_TYPE_BATTERY,
> +       .properties = axp20x_battery_props,
> +       .num_properties = ARRAY_SIZE(axp20x_battery_props),
> +       .property_is_writeable = axp20x_battery_prop_writeable,
> +       .get_property = axp20x_battery_get_prop,
> +       .set_property = axp20x_battery_set_prop,
> +};
> +
> +static const struct of_device_id axp20x_battery_ps_id[] = {
> +       {
> +               .compatible = "x-powers,axp209-battery-power-supply",
> +               .data = (void *)AXP209_ID,
> +       }, {
> +               .compatible = "x-powers,axp221-battery-power-supply",
> +               .data = (void *)AXP221_ID,
> +       }, { /* sentinel */ },
> +};
> +MODULE_DEVICE_TABLE(of, axp20x_battery_ps_id);
> +
> +static int axp20x_power_probe(struct platform_device *pdev)
> +{
> +       struct axp20x_batt_ps *axp20x_batt;
> +       struct power_supply_config psy_cfg = {};
> +       struct power_supply_battery_info info;
> +
> +       if (!of_device_is_available(pdev->dev.of_node))
> +               return -ENODEV;
> +
> +       axp20x_batt = devm_kzalloc(&pdev->dev, sizeof(*axp20x_batt),
> +                                  GFP_KERNEL);
> +       if (!axp20x_batt)
> +               return -ENOMEM;
> +
> +       axp20x_batt->batt_v = devm_iio_channel_get(&pdev->dev, "batt_v");
> +       if (IS_ERR(axp20x_batt->batt_v)) {
> +               if (PTR_ERR(axp20x_batt->batt_v) == -ENODEV)
> +                       return -EPROBE_DEFER;
> +               return PTR_ERR(axp20x_batt->batt_v);
> +       }
> +
> +       axp20x_batt->batt_chrg_i = devm_iio_channel_get(&pdev->dev,
> +                                                       "batt_chrg_i");
> +       if (IS_ERR(axp20x_batt->batt_chrg_i)) {
> +               if (PTR_ERR(axp20x_batt->batt_chrg_i) == -ENODEV)
> +                       return -EPROBE_DEFER;
> +               return PTR_ERR(axp20x_batt->batt_chrg_i);
> +       }
> +
> +       axp20x_batt->batt_dischrg_i = devm_iio_channel_get(&pdev->dev,
> +                                                          "batt_dischrg_i");
> +       if (IS_ERR(axp20x_batt->batt_dischrg_i)) {
> +               if (PTR_ERR(axp20x_batt->batt_dischrg_i) == -ENODEV)
> +                       return -EPROBE_DEFER;
> +               return PTR_ERR(axp20x_batt->batt_dischrg_i);
> +       }
> +
> +       axp20x_batt->regmap = dev_get_regmap(pdev->dev.parent, NULL);
> +       platform_set_drvdata(pdev, axp20x_batt);
> +
> +       psy_cfg.drv_data = axp20x_batt;
> +       psy_cfg.of_node = pdev->dev.of_node;
> +
> +       axp20x_batt->axp_id = (uintptr_t)of_device_get_match_data(&pdev->dev);
> +
> +       axp20x_batt->batt = devm_power_supply_register(&pdev->dev,
> +                                                      &axp20x_batt_ps_desc,
> +                                                      &psy_cfg);
> +       if (IS_ERR(axp20x_batt->batt)) {
> +               dev_err(&pdev->dev, "failed to register power supply: %ld\n",
> +                       PTR_ERR(axp20x_batt->batt));
> +               return PTR_ERR(axp20x_batt->batt);
> +       }
> +
> +       if (!power_supply_get_battery_info(axp20x_batt->batt, &info)) {
> +               int vmin = info.voltage_min_design_uv;
> +               int ccc = info.constant_charge_current_ua;
> +
> +               if (vmin > 0 && axp20x_set_voltage_min_design(axp20x_batt,
> +                                                             vmin))
> +                       dev_err(&pdev->dev,
> +                               "couldn't set voltage_min_design\n");
> +
> +               if (ccc > 0 && axp20x_set_constant_charge_current(axp20x_batt,
> +                                                                 ccc))
> +                       dev_err(&pdev->dev,
> +                               "couldn't set constant charge current\n");
> +       }
> +
> +       return 0;
> +}
> +
> +static struct platform_driver axp20x_batt_driver = {
> +       .probe    = axp20x_power_probe,
> +       .driver   = {
> +               .name  = "axp20x-battery-power-supply",
> +               .of_match_table = axp20x_battery_ps_id,
> +       },
> +};
> +
> +module_platform_driver(axp20x_batt_driver);
> +
> +MODULE_DESCRIPTION("Battery power supply driver for AXP20X and AXP22X PMICs");
> +MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
> +MODULE_LICENSE("GPL");
> --
> 2.9.3
>

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 13/18] power: supply: add battery driver for AXP20X and AXP22X PMICs
@ 2017-02-21  4:44     ` Chen-Yu Tsai
  0 siblings, 0 replies; 126+ messages in thread
From: Chen-Yu Tsai @ 2017-02-21  4:44 UTC (permalink / raw)
  To: Quentin Schulz
  Cc: Mark Rutland, devicetree, Lars-Peter Clausen, open list:THERMAL,
	linux-iio, linux-kernel, Sebastian Reichel, Russell King,
	Chen-Yu Tsai, Rob Herring, Icenowy Zheng, Peter Meerwald-Stadler,
	knaack.h, Maxime Ripard, Bruno Prémont, Lee Jones,
	Thomas Petazzoni, linux-arm-kernel

On Tue, Feb 14, 2017 at 5:41 PM, Quentin Schulz
<quentin.schulz@free-electrons.com> wrote:
> The X-Powers AXP20X and AXP22X PMICs can have a battery as power supply.
>
> This patch adds the battery power supply driver to get various data from
> the PMIC, such as the battery status (charging, discharging, full,
> dead), current max limit, current current, battery capacity (in
> percentage), voltage max and min limits, current voltage and battery
> capacity (in Ah).
>
> This battery driver uses the AXP20X/AXP22X ADC driver as PMIC data
> provider.
>
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> Acked-by: Jonathan Cameron <jic23@kernel.org>
> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
> ---
>
> v3:
>  - added axp20x_set_voltage_min_design function so it can be reused,
>  - used power_supply_get_battery_info for setting constant charge current
>  instead of x-powers,constant-charge-current introduced in v2,
>  - used power_supply_get_battery_info for setting voltage min design of
>  the battery,
>
> v2:
>  - changed BIT(x) to 1 << x when describing bits purpose for which 2 <<
>  x or 3 << x exists, to be consistent,
>  - switched from POWER_SUPPLY_PROP_CURRENT_MAX to
>  POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
>  - added POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX to the list of
>  readable properties,
>  - replaced µ character by a common u for micro units to make checkpatch
>  happy,
>  - factorized code in axp20x_battery_set_max_voltage,
>  - added a axp20x_set_constant_charge_current function to be used when
>  setting the value from sysfs and from the DT,
>  - removed some dead code,
>  - added a DT property to set constant current charge of the battery
>  (x-powers,constant-charge-current),
>  - migrated to dev_get_regmap instead of manually looking for the regmap
>  in the drvdata of the parent,
>  - switched from int to uintptr_t cast to make sure the cast is always
>  for the same size type (make build on 64bits platforms happy mainly),
>  drivers/power/supply/Kconfig          |  12 +
>  drivers/power/supply/Makefile         |   1 +
>  drivers/power/supply/axp20x_battery.c | 492 ++++++++++++++++++++++++++++++++++
>  3 files changed, 505 insertions(+)
>  create mode 100644 drivers/power/supply/axp20x_battery.c
>
> diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig
> index c552b4b..48619de 100644
> --- a/drivers/power/supply/Kconfig
> +++ b/drivers/power/supply/Kconfig
> @@ -226,6 +226,18 @@ config CHARGER_AXP20X
>           This driver can also be built as a module. If so, the module will be
>           called axp20x_ac_power.
>
> +config BATTERY_AXP20X
> +       tristate "X-Powers AXP20X battery driver"
> +       depends on MFD_AXP20X
> +       depends on AXP20X_ADC
> +       depends on IIO
> +       help
> +         Say Y here to enable support for X-Powers AXP20X PMICs' battery power
> +         supply.
> +
> +         This driver can also be built as a module. If so, the module will be
> +         called axp20x_battery.
> +
>  config AXP288_CHARGER
>         tristate "X-Powers AXP288 Charger"
>         depends on MFD_AXP20X && EXTCON_AXP288
> diff --git a/drivers/power/supply/Makefile b/drivers/power/supply/Makefile
> index 7d22417..5a217b2 100644
> --- a/drivers/power/supply/Makefile
> +++ b/drivers/power/supply/Makefile
> @@ -18,6 +18,7 @@ obj-$(CONFIG_TEST_POWER)      += test_power.o
>
>  obj-$(CONFIG_BATTERY_88PM860X) += 88pm860x_battery.o
>  obj-$(CONFIG_BATTERY_ACT8945A) += act8945a_charger.o
> +obj-$(CONFIG_BATTERY_AXP20X)   += axp20x_battery.o
>  obj-$(CONFIG_CHARGER_AXP20X)   += axp20x_ac_power.o
>  obj-$(CONFIG_BATTERY_DS2760)   += ds2760_battery.o
>  obj-$(CONFIG_BATTERY_DS2780)   += ds2780_battery.o
> diff --git a/drivers/power/supply/axp20x_battery.c b/drivers/power/supply/axp20x_battery.c
> new file mode 100644
> index 0000000..bd16ac6
> --- /dev/null
> +++ b/drivers/power/supply/axp20x_battery.c
> @@ -0,0 +1,492 @@
> +/*
> + * Battery power supply driver for X-Powers AXP20X and AXP22X PMICs
> + *
> + * Copyright 2016 Free Electrons NextThing Co.
> + *     Quentin Schulz <quentin.schulz@free-electrons.com>
> + *
> + * This driver is based on a previous upstreaming attempt by:
> + *     Bruno Prémont <bonbons@linux-vserver.org>
> + *
> + * This file is subject to the terms and conditions of the GNU General
> + * Public License. See the file "COPYING" in the main directory of this
> + * archive for more details.
> + *
> + * 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.
> + */
> +
> +#include <linux/err.h>
> +#include <linux/interrupt.h>
> +#include <linux/irq.h>
> +#include <linux/module.h>
> +#include <linux/of.h>
> +#include <linux/of_device.h>
> +#include <linux/platform_device.h>
> +#include <linux/power_supply.h>
> +#include <linux/regmap.h>
> +#include <linux/slab.h>
> +#include <linux/time.h>
> +#include <linux/iio/iio.h>
> +#include <linux/iio/consumer.h>
> +#include <linux/mfd/axp20x.h>
> +
> +#define AXP20X_PWR_STATUS_BAT_CHARGING BIT(2)
> +
> +#define AXP20X_PWR_OP_BATT_PRESENT     BIT(5)
> +#define AXP20X_PWR_OP_BATT_ACTIVATED   BIT(3)
> +
> +#define AXP209_FG_PERCENT              GENMASK(6, 0)
> +#define AXP22X_FG_VALID                        BIT(7)
> +
> +#define AXP20X_CHRG_CTRL1_TGT_VOLT     GENMASK(6, 5)
> +#define AXP20X_CHRG_CTRL1_TGT_4_1V     (0 << 5)
> +#define AXP20X_CHRG_CTRL1_TGT_4_15V    (1 << 5)
> +#define AXP20X_CHRG_CTRL1_TGT_4_2V     (2 << 5)
> +#define AXP20X_CHRG_CTRL1_TGT_4_36V    (3 << 5)
> +
> +#define AXP22X_CHRG_CTRL1_TGT_4_22V    (1 << 5)
> +#define AXP22X_CHRG_CTRL1_TGT_4_24V    (3 << 5)
> +
> +#define AXP20X_CHRG_CTRL1_TGT_CURR     GENMASK(3, 0)
> +
> +#define AXP20X_V_OFF_MASK              GENMASK(2, 0)
> +
> +struct axp20x_batt_ps {
> +       struct regmap *regmap;
> +       struct power_supply *batt;
> +       struct axp20x_dev *axp20x;
> +       struct iio_channel *batt_chrg_i;
> +       struct iio_channel *batt_dischrg_i;
> +       struct iio_channel *batt_v;
> +       u8 axp_id;
> +};
> +
> +static int axp20x_battery_get_max_voltage(struct axp20x_batt_ps *axp20x_batt,
> +                                         int *val)
> +{
> +       int ret, reg;
> +
> +       ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, &reg);
> +       if (ret)
> +               return ret;
> +
> +       switch (reg & AXP20X_CHRG_CTRL1_TGT_VOLT) {
> +       case AXP20X_CHRG_CTRL1_TGT_4_1V:
> +               *val = 4100000;
> +               break;
> +       case AXP20X_CHRG_CTRL1_TGT_4_15V:
> +               *val = 4150000;
> +               break;
> +       case AXP20X_CHRG_CTRL1_TGT_4_2V:
> +               *val = 4200000;
> +               break;
> +       case AXP20X_CHRG_CTRL1_TGT_4_36V:
> +               *val = 4360000;
> +               break;
> +       default:
> +               return -EINVAL;
> +       }
> +
> +       return 0;
> +}
> +
> +static int axp22x_battery_get_max_voltage(struct axp20x_batt_ps *axp20x_batt,
> +                                         int *val)
> +{
> +       int ret, reg;
> +
> +       ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, &reg);
> +       if (ret)
> +               return ret;
> +
> +       switch (reg & AXP20X_CHRG_CTRL1_TGT_VOLT) {
> +       case AXP20X_CHRG_CTRL1_TGT_4_1V:
> +               *val = 4100000;
> +               break;
> +       case AXP20X_CHRG_CTRL1_TGT_4_2V:
> +               *val = 4200000;
> +               break;
> +       case AXP22X_CHRG_CTRL1_TGT_4_22V:
> +               *val = 4220000;
> +               break;
> +       case AXP22X_CHRG_CTRL1_TGT_4_24V:
> +               *val = 4240000;
> +               break;
> +       default:
> +               return -EINVAL;
> +       }
> +
> +       return 0;
> +}
> +
> +static int axp20x_battery_get_prop(struct power_supply *psy,
> +                                  enum power_supply_property psp,
> +                                  union power_supply_propval *val)
> +{
> +       struct axp20x_batt_ps *axp20x_batt = power_supply_get_drvdata(psy);
> +       struct iio_channel *chan;
> +       int ret = 0, reg, val1;
> +
> +       switch (psp) {
> +       case POWER_SUPPLY_PROP_PRESENT:
> +       case POWER_SUPPLY_PROP_ONLINE:
> +               ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
> +                                 &reg);
> +               if (ret)
> +                       return ret;
> +
> +               val->intval = !!(reg & AXP20X_PWR_OP_BATT_PRESENT);
> +               break;
> +
> +       case POWER_SUPPLY_PROP_STATUS:
> +               ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_INPUT_STATUS,
> +                                 &reg);
> +               if (ret)
> +                       return ret;
> +
> +               if (reg & AXP20X_PWR_STATUS_BAT_CHARGING) {
> +                       val->intval = POWER_SUPPLY_STATUS_CHARGING;
> +                       return 0;
> +               }
> +
> +               ret = iio_read_channel_processed(axp20x_batt->batt_dischrg_i,
> +                                                &val1);
> +               if (ret)
> +                       return ret;
> +
> +               if (val1) {
> +                       val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
> +                       return 0;
> +               }
> +
> +               ret = regmap_read(axp20x_batt->regmap, AXP20X_FG_RES, &val1);
> +               if (ret)
> +                       return ret;
> +
> +               /*
> +                * Fuel Gauge data takes 7 bits but the stored value seems to be
> +                * directly the raw percentage without any scaling to 7 bits.
> +                */
> +               if ((val1 & AXP209_FG_PERCENT) == 100)
> +                       val->intval = POWER_SUPPLY_STATUS_FULL;
> +               else
> +                       val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
> +               break;
> +
> +       case POWER_SUPPLY_PROP_HEALTH:
> +               ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
> +                                 &val1);
> +               if (ret)
> +                       return ret;
> +
> +               if (val1 & AXP20X_PWR_OP_BATT_ACTIVATED) {
> +                       val->intval = POWER_SUPPLY_HEALTH_DEAD;
> +                       return 0;
> +               }
> +
> +               val->intval = POWER_SUPPLY_HEALTH_GOOD;
> +               break;
> +
> +       case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
> +               ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, &reg);
> +               if (ret)
> +                       return ret;
> +
> +               reg &= AXP20X_CHRG_CTRL1_TGT_CURR;
> +               val->intval = reg * 100000 + 300000;
> +               break;
> +
> +       case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
> +               val->intval = AXP20X_CHRG_CTRL1_TGT_CURR * 100000 + 300000;
> +               break;
> +
> +       case POWER_SUPPLY_PROP_CURRENT_NOW:
> +               ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_INPUT_STATUS,
> +                                 &reg);
> +               if (ret)
> +                       return ret;
> +
> +               if (reg & AXP20X_PWR_STATUS_BAT_CHARGING)
> +                       chan = axp20x_batt->batt_chrg_i;
> +               else
> +                       chan = axp20x_batt->batt_dischrg_i;
> +
> +               ret = iio_read_channel_processed(chan, &val->intval);
> +               if (ret)
> +                       return ret;
> +
> +               /* IIO framework gives mA but Power Supply framework gives uA */
> +               val->intval *= 1000;
> +               break;
> +
> +       case POWER_SUPPLY_PROP_CAPACITY:
> +               /* When no battery is present, return capacity is 100% */
> +               ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
> +                                 &reg);
> +               if (ret)
> +                       return ret;
> +
> +               if (!(reg & AXP20X_PWR_OP_BATT_PRESENT)) {
> +                       val->intval = 100;
> +                       return 0;
> +               }
> +
> +               ret = regmap_read(axp20x_batt->regmap, AXP20X_FG_RES, &reg);
> +               if (ret)
> +                       return ret;
> +
> +               if (axp20x_batt->axp_id == AXP221_ID &&
> +                   !(reg & AXP22X_FG_VALID))
> +                       return -EINVAL;
> +
> +               /*
> +                * Fuel Gauge data takes 7 bits but the stored value seems to be
> +                * directly the raw percentage without any scaling to 7 bits.
> +                */
> +               val->intval = reg & AXP209_FG_PERCENT;
> +               break;
> +
> +       case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
> +               if (axp20x_batt->axp_id == AXP209_ID)
> +                       return axp20x_battery_get_max_voltage(axp20x_batt,
> +                                                             &val->intval);
> +               return axp22x_battery_get_max_voltage(axp20x_batt,
> +                                                     &val->intval);
> +
> +       case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
> +               ret = regmap_read(axp20x_batt->regmap, AXP20X_V_OFF, &reg);
> +               if (ret)
> +                       return ret;
> +
> +               val->intval = 2600000 + 100000 * (reg & AXP20X_V_OFF_MASK);
> +               break;
> +
> +       case POWER_SUPPLY_PROP_VOLTAGE_NOW:
> +               ret = iio_read_channel_processed(axp20x_batt->batt_v,
> +                                                &val->intval);
> +               if (ret)
> +                       return ret;
> +
> +               /* IIO framework gives mV but Power Supply framework gives uV */
> +               val->intval *= 1000;
> +               break;
> +
> +       default:
> +               return -EINVAL;
> +       }
> +
> +       return 0;
> +}
> +
> +static int axp20x_battery_set_max_voltage(struct axp20x_batt_ps *axp20x_batt,
> +                                         int val)
> +{
> +       switch (val) {
> +       case 4100000:
> +               val = AXP20X_CHRG_CTRL1_TGT_4_1V;
> +               break;
> +
> +       case 4150000:
> +               if (axp20x_batt->axp_id == AXP221_ID)
> +                       return -EINVAL;
> +
> +               val = AXP20X_CHRG_CTRL1_TGT_4_15V;
> +               break;
> +
> +       case 4200000:
> +               val = AXP20X_CHRG_CTRL1_TGT_4_2V;
> +               break;
> +
> +       default:
> +               /*
> +                * AXP20x max voltage can be set to 4.36V and AXP22X max voltage
> +                * can be set to 4.22V and 4.24V, but these voltages are too
> +                * high for Lithium based batteries (AXP PMICs are supposed to
> +                * be used with these kinds of battery).
> +                */
> +               return -EINVAL;
> +       }
> +
> +       return regmap_update_bits(axp20x_batt->regmap, AXP20X_CHRG_CTRL1,
> +                                 AXP20X_CHRG_CTRL1_TGT_VOLT, val);
> +}
> +
> +static int axp20x_set_constant_charge_current(struct axp20x_batt_ps *axp_batt,
> +                                             int charge_current)
> +{
> +       if (axp_batt->axp_id == AXP209_ID)
> +               charge_current = (charge_current - 300000) / 100000;
> +       else
> +               charge_current = (charge_current - 300000) / 150000;
> +
> +       if (charge_current > AXP20X_CHRG_CTRL1_TGT_CURR || charge_current < 0)
> +               return -EINVAL;

I would add a check or warn if the value to be programmed exceeds that returned
by power_supply_get_battery_info. A charge current exceeding the limits of the
battery is potentially disastrous. The battery may be destroyed or even burst
into flames and explode, taking the board and anything nearby with it. Otherwise

Acked-by: Chen-Yu Tsai <wens@csie.org>

Speaking of power_supply_get_battery_info, is it merged or ready to be merged?

> +
> +       return regmap_update_bits(axp_batt->regmap, AXP20X_CHRG_CTRL1,
> +                                 AXP20X_CHRG_CTRL1_TGT_CURR, charge_current);
> +}
> +
> +static int axp20x_set_voltage_min_design(struct axp20x_batt_ps *axp_batt,
> +                                        int min_voltage)
> +{
> +       int val1 = (min_voltage - 2600000) / 100000;
> +
> +       if (val1 < 0 || val1 > AXP20X_V_OFF_MASK)
> +               return -EINVAL;
> +
> +       return regmap_update_bits(axp_batt->regmap, AXP20X_V_OFF,
> +                                 AXP20X_V_OFF_MASK, val1);
> +}
> +
> +static int axp20x_battery_set_prop(struct power_supply *psy,
> +                                  enum power_supply_property psp,
> +                                  const union power_supply_propval *val)
> +{
> +       struct axp20x_batt_ps *axp20x_batt = power_supply_get_drvdata(psy);
> +
> +       switch (psp) {
> +       case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
> +               return axp20x_set_voltage_min_design(axp20x_batt, val->intval);
> +
> +       case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
> +               return axp20x_battery_set_max_voltage(axp20x_batt, val->intval);
> +
> +       case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
> +               return axp20x_set_constant_charge_current(axp20x_batt,
> +                                                         val->intval);
> +
> +       default:
> +               return -EINVAL;
> +       }
> +}
> +
> +static enum power_supply_property axp20x_battery_props[] = {
> +       POWER_SUPPLY_PROP_PRESENT,
> +       POWER_SUPPLY_PROP_ONLINE,
> +       POWER_SUPPLY_PROP_STATUS,
> +       POWER_SUPPLY_PROP_VOLTAGE_NOW,
> +       POWER_SUPPLY_PROP_CURRENT_NOW,
> +       POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
> +       POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
> +       POWER_SUPPLY_PROP_HEALTH,
> +       POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
> +       POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
> +       POWER_SUPPLY_PROP_CAPACITY,
> +};
> +
> +static int axp20x_battery_prop_writeable(struct power_supply *psy,
> +                                        enum power_supply_property psp)
> +{
> +       return psp == POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN ||
> +              psp == POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN ||
> +              psp == POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT;
> +}
> +
> +static const struct power_supply_desc axp20x_batt_ps_desc = {
> +       .name = "axp20x-battery",
> +       .type = POWER_SUPPLY_TYPE_BATTERY,
> +       .properties = axp20x_battery_props,
> +       .num_properties = ARRAY_SIZE(axp20x_battery_props),
> +       .property_is_writeable = axp20x_battery_prop_writeable,
> +       .get_property = axp20x_battery_get_prop,
> +       .set_property = axp20x_battery_set_prop,
> +};
> +
> +static const struct of_device_id axp20x_battery_ps_id[] = {
> +       {
> +               .compatible = "x-powers,axp209-battery-power-supply",
> +               .data = (void *)AXP209_ID,
> +       }, {
> +               .compatible = "x-powers,axp221-battery-power-supply",
> +               .data = (void *)AXP221_ID,
> +       }, { /* sentinel */ },
> +};
> +MODULE_DEVICE_TABLE(of, axp20x_battery_ps_id);
> +
> +static int axp20x_power_probe(struct platform_device *pdev)
> +{
> +       struct axp20x_batt_ps *axp20x_batt;
> +       struct power_supply_config psy_cfg = {};
> +       struct power_supply_battery_info info;
> +
> +       if (!of_device_is_available(pdev->dev.of_node))
> +               return -ENODEV;
> +
> +       axp20x_batt = devm_kzalloc(&pdev->dev, sizeof(*axp20x_batt),
> +                                  GFP_KERNEL);
> +       if (!axp20x_batt)
> +               return -ENOMEM;
> +
> +       axp20x_batt->batt_v = devm_iio_channel_get(&pdev->dev, "batt_v");
> +       if (IS_ERR(axp20x_batt->batt_v)) {
> +               if (PTR_ERR(axp20x_batt->batt_v) == -ENODEV)
> +                       return -EPROBE_DEFER;
> +               return PTR_ERR(axp20x_batt->batt_v);
> +       }
> +
> +       axp20x_batt->batt_chrg_i = devm_iio_channel_get(&pdev->dev,
> +                                                       "batt_chrg_i");
> +       if (IS_ERR(axp20x_batt->batt_chrg_i)) {
> +               if (PTR_ERR(axp20x_batt->batt_chrg_i) == -ENODEV)
> +                       return -EPROBE_DEFER;
> +               return PTR_ERR(axp20x_batt->batt_chrg_i);
> +       }
> +
> +       axp20x_batt->batt_dischrg_i = devm_iio_channel_get(&pdev->dev,
> +                                                          "batt_dischrg_i");
> +       if (IS_ERR(axp20x_batt->batt_dischrg_i)) {
> +               if (PTR_ERR(axp20x_batt->batt_dischrg_i) == -ENODEV)
> +                       return -EPROBE_DEFER;
> +               return PTR_ERR(axp20x_batt->batt_dischrg_i);
> +       }
> +
> +       axp20x_batt->regmap = dev_get_regmap(pdev->dev.parent, NULL);
> +       platform_set_drvdata(pdev, axp20x_batt);
> +
> +       psy_cfg.drv_data = axp20x_batt;
> +       psy_cfg.of_node = pdev->dev.of_node;
> +
> +       axp20x_batt->axp_id = (uintptr_t)of_device_get_match_data(&pdev->dev);
> +
> +       axp20x_batt->batt = devm_power_supply_register(&pdev->dev,
> +                                                      &axp20x_batt_ps_desc,
> +                                                      &psy_cfg);
> +       if (IS_ERR(axp20x_batt->batt)) {
> +               dev_err(&pdev->dev, "failed to register power supply: %ld\n",
> +                       PTR_ERR(axp20x_batt->batt));
> +               return PTR_ERR(axp20x_batt->batt);
> +       }
> +
> +       if (!power_supply_get_battery_info(axp20x_batt->batt, &info)) {
> +               int vmin = info.voltage_min_design_uv;
> +               int ccc = info.constant_charge_current_ua;
> +
> +               if (vmin > 0 && axp20x_set_voltage_min_design(axp20x_batt,
> +                                                             vmin))
> +                       dev_err(&pdev->dev,
> +                               "couldn't set voltage_min_design\n");
> +
> +               if (ccc > 0 && axp20x_set_constant_charge_current(axp20x_batt,
> +                                                                 ccc))
> +                       dev_err(&pdev->dev,
> +                               "couldn't set constant charge current\n");
> +       }
> +
> +       return 0;
> +}
> +
> +static struct platform_driver axp20x_batt_driver = {
> +       .probe    = axp20x_power_probe,
> +       .driver   = {
> +               .name  = "axp20x-battery-power-supply",
> +               .of_match_table = axp20x_battery_ps_id,
> +       },
> +};
> +
> +module_platform_driver(axp20x_batt_driver);
> +
> +MODULE_DESCRIPTION("Battery power supply driver for AXP20X and AXP22X PMICs");
> +MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
> +MODULE_LICENSE("GPL");
> --
> 2.9.3
>

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 126+ messages in thread

* [PATCH v3 13/18] power: supply: add battery driver for AXP20X and AXP22X PMICs
@ 2017-02-21  4:44     ` Chen-Yu Tsai
  0 siblings, 0 replies; 126+ messages in thread
From: Chen-Yu Tsai @ 2017-02-21  4:44 UTC (permalink / raw)
  To: linux-arm-kernel

On Tue, Feb 14, 2017 at 5:41 PM, Quentin Schulz
<quentin.schulz@free-electrons.com> wrote:
> The X-Powers AXP20X and AXP22X PMICs can have a battery as power supply.
>
> This patch adds the battery power supply driver to get various data from
> the PMIC, such as the battery status (charging, discharging, full,
> dead), current max limit, current current, battery capacity (in
> percentage), voltage max and min limits, current voltage and battery
> capacity (in Ah).
>
> This battery driver uses the AXP20X/AXP22X ADC driver as PMIC data
> provider.
>
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> Acked-by: Jonathan Cameron <jic23@kernel.org>
> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
> ---
>
> v3:
>  - added axp20x_set_voltage_min_design function so it can be reused,
>  - used power_supply_get_battery_info for setting constant charge current
>  instead of x-powers,constant-charge-current introduced in v2,
>  - used power_supply_get_battery_info for setting voltage min design of
>  the battery,
>
> v2:
>  - changed BIT(x) to 1 << x when describing bits purpose for which 2 <<
>  x or 3 << x exists, to be consistent,
>  - switched from POWER_SUPPLY_PROP_CURRENT_MAX to
>  POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
>  - added POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX to the list of
>  readable properties,
>  - replaced ? character by a common u for micro units to make checkpatch
>  happy,
>  - factorized code in axp20x_battery_set_max_voltage,
>  - added a axp20x_set_constant_charge_current function to be used when
>  setting the value from sysfs and from the DT,
>  - removed some dead code,
>  - added a DT property to set constant current charge of the battery
>  (x-powers,constant-charge-current),
>  - migrated to dev_get_regmap instead of manually looking for the regmap
>  in the drvdata of the parent,
>  - switched from int to uintptr_t cast to make sure the cast is always
>  for the same size type (make build on 64bits platforms happy mainly),
>  drivers/power/supply/Kconfig          |  12 +
>  drivers/power/supply/Makefile         |   1 +
>  drivers/power/supply/axp20x_battery.c | 492 ++++++++++++++++++++++++++++++++++
>  3 files changed, 505 insertions(+)
>  create mode 100644 drivers/power/supply/axp20x_battery.c
>
> diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig
> index c552b4b..48619de 100644
> --- a/drivers/power/supply/Kconfig
> +++ b/drivers/power/supply/Kconfig
> @@ -226,6 +226,18 @@ config CHARGER_AXP20X
>           This driver can also be built as a module. If so, the module will be
>           called axp20x_ac_power.
>
> +config BATTERY_AXP20X
> +       tristate "X-Powers AXP20X battery driver"
> +       depends on MFD_AXP20X
> +       depends on AXP20X_ADC
> +       depends on IIO
> +       help
> +         Say Y here to enable support for X-Powers AXP20X PMICs' battery power
> +         supply.
> +
> +         This driver can also be built as a module. If so, the module will be
> +         called axp20x_battery.
> +
>  config AXP288_CHARGER
>         tristate "X-Powers AXP288 Charger"
>         depends on MFD_AXP20X && EXTCON_AXP288
> diff --git a/drivers/power/supply/Makefile b/drivers/power/supply/Makefile
> index 7d22417..5a217b2 100644
> --- a/drivers/power/supply/Makefile
> +++ b/drivers/power/supply/Makefile
> @@ -18,6 +18,7 @@ obj-$(CONFIG_TEST_POWER)      += test_power.o
>
>  obj-$(CONFIG_BATTERY_88PM860X) += 88pm860x_battery.o
>  obj-$(CONFIG_BATTERY_ACT8945A) += act8945a_charger.o
> +obj-$(CONFIG_BATTERY_AXP20X)   += axp20x_battery.o
>  obj-$(CONFIG_CHARGER_AXP20X)   += axp20x_ac_power.o
>  obj-$(CONFIG_BATTERY_DS2760)   += ds2760_battery.o
>  obj-$(CONFIG_BATTERY_DS2780)   += ds2780_battery.o
> diff --git a/drivers/power/supply/axp20x_battery.c b/drivers/power/supply/axp20x_battery.c
> new file mode 100644
> index 0000000..bd16ac6
> --- /dev/null
> +++ b/drivers/power/supply/axp20x_battery.c
> @@ -0,0 +1,492 @@
> +/*
> + * Battery power supply driver for X-Powers AXP20X and AXP22X PMICs
> + *
> + * Copyright 2016 Free Electrons NextThing Co.
> + *     Quentin Schulz <quentin.schulz@free-electrons.com>
> + *
> + * This driver is based on a previous upstreaming attempt by:
> + *     Bruno Pr?mont <bonbons@linux-vserver.org>
> + *
> + * This file is subject to the terms and conditions of the GNU General
> + * Public License. See the file "COPYING" in the main directory of this
> + * archive for more details.
> + *
> + * 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.
> + */
> +
> +#include <linux/err.h>
> +#include <linux/interrupt.h>
> +#include <linux/irq.h>
> +#include <linux/module.h>
> +#include <linux/of.h>
> +#include <linux/of_device.h>
> +#include <linux/platform_device.h>
> +#include <linux/power_supply.h>
> +#include <linux/regmap.h>
> +#include <linux/slab.h>
> +#include <linux/time.h>
> +#include <linux/iio/iio.h>
> +#include <linux/iio/consumer.h>
> +#include <linux/mfd/axp20x.h>
> +
> +#define AXP20X_PWR_STATUS_BAT_CHARGING BIT(2)
> +
> +#define AXP20X_PWR_OP_BATT_PRESENT     BIT(5)
> +#define AXP20X_PWR_OP_BATT_ACTIVATED   BIT(3)
> +
> +#define AXP209_FG_PERCENT              GENMASK(6, 0)
> +#define AXP22X_FG_VALID                        BIT(7)
> +
> +#define AXP20X_CHRG_CTRL1_TGT_VOLT     GENMASK(6, 5)
> +#define AXP20X_CHRG_CTRL1_TGT_4_1V     (0 << 5)
> +#define AXP20X_CHRG_CTRL1_TGT_4_15V    (1 << 5)
> +#define AXP20X_CHRG_CTRL1_TGT_4_2V     (2 << 5)
> +#define AXP20X_CHRG_CTRL1_TGT_4_36V    (3 << 5)
> +
> +#define AXP22X_CHRG_CTRL1_TGT_4_22V    (1 << 5)
> +#define AXP22X_CHRG_CTRL1_TGT_4_24V    (3 << 5)
> +
> +#define AXP20X_CHRG_CTRL1_TGT_CURR     GENMASK(3, 0)
> +
> +#define AXP20X_V_OFF_MASK              GENMASK(2, 0)
> +
> +struct axp20x_batt_ps {
> +       struct regmap *regmap;
> +       struct power_supply *batt;
> +       struct axp20x_dev *axp20x;
> +       struct iio_channel *batt_chrg_i;
> +       struct iio_channel *batt_dischrg_i;
> +       struct iio_channel *batt_v;
> +       u8 axp_id;
> +};
> +
> +static int axp20x_battery_get_max_voltage(struct axp20x_batt_ps *axp20x_batt,
> +                                         int *val)
> +{
> +       int ret, reg;
> +
> +       ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, &reg);
> +       if (ret)
> +               return ret;
> +
> +       switch (reg & AXP20X_CHRG_CTRL1_TGT_VOLT) {
> +       case AXP20X_CHRG_CTRL1_TGT_4_1V:
> +               *val = 4100000;
> +               break;
> +       case AXP20X_CHRG_CTRL1_TGT_4_15V:
> +               *val = 4150000;
> +               break;
> +       case AXP20X_CHRG_CTRL1_TGT_4_2V:
> +               *val = 4200000;
> +               break;
> +       case AXP20X_CHRG_CTRL1_TGT_4_36V:
> +               *val = 4360000;
> +               break;
> +       default:
> +               return -EINVAL;
> +       }
> +
> +       return 0;
> +}
> +
> +static int axp22x_battery_get_max_voltage(struct axp20x_batt_ps *axp20x_batt,
> +                                         int *val)
> +{
> +       int ret, reg;
> +
> +       ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, &reg);
> +       if (ret)
> +               return ret;
> +
> +       switch (reg & AXP20X_CHRG_CTRL1_TGT_VOLT) {
> +       case AXP20X_CHRG_CTRL1_TGT_4_1V:
> +               *val = 4100000;
> +               break;
> +       case AXP20X_CHRG_CTRL1_TGT_4_2V:
> +               *val = 4200000;
> +               break;
> +       case AXP22X_CHRG_CTRL1_TGT_4_22V:
> +               *val = 4220000;
> +               break;
> +       case AXP22X_CHRG_CTRL1_TGT_4_24V:
> +               *val = 4240000;
> +               break;
> +       default:
> +               return -EINVAL;
> +       }
> +
> +       return 0;
> +}
> +
> +static int axp20x_battery_get_prop(struct power_supply *psy,
> +                                  enum power_supply_property psp,
> +                                  union power_supply_propval *val)
> +{
> +       struct axp20x_batt_ps *axp20x_batt = power_supply_get_drvdata(psy);
> +       struct iio_channel *chan;
> +       int ret = 0, reg, val1;
> +
> +       switch (psp) {
> +       case POWER_SUPPLY_PROP_PRESENT:
> +       case POWER_SUPPLY_PROP_ONLINE:
> +               ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
> +                                 &reg);
> +               if (ret)
> +                       return ret;
> +
> +               val->intval = !!(reg & AXP20X_PWR_OP_BATT_PRESENT);
> +               break;
> +
> +       case POWER_SUPPLY_PROP_STATUS:
> +               ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_INPUT_STATUS,
> +                                 &reg);
> +               if (ret)
> +                       return ret;
> +
> +               if (reg & AXP20X_PWR_STATUS_BAT_CHARGING) {
> +                       val->intval = POWER_SUPPLY_STATUS_CHARGING;
> +                       return 0;
> +               }
> +
> +               ret = iio_read_channel_processed(axp20x_batt->batt_dischrg_i,
> +                                                &val1);
> +               if (ret)
> +                       return ret;
> +
> +               if (val1) {
> +                       val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
> +                       return 0;
> +               }
> +
> +               ret = regmap_read(axp20x_batt->regmap, AXP20X_FG_RES, &val1);
> +               if (ret)
> +                       return ret;
> +
> +               /*
> +                * Fuel Gauge data takes 7 bits but the stored value seems to be
> +                * directly the raw percentage without any scaling to 7 bits.
> +                */
> +               if ((val1 & AXP209_FG_PERCENT) == 100)
> +                       val->intval = POWER_SUPPLY_STATUS_FULL;
> +               else
> +                       val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
> +               break;
> +
> +       case POWER_SUPPLY_PROP_HEALTH:
> +               ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
> +                                 &val1);
> +               if (ret)
> +                       return ret;
> +
> +               if (val1 & AXP20X_PWR_OP_BATT_ACTIVATED) {
> +                       val->intval = POWER_SUPPLY_HEALTH_DEAD;
> +                       return 0;
> +               }
> +
> +               val->intval = POWER_SUPPLY_HEALTH_GOOD;
> +               break;
> +
> +       case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
> +               ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, &reg);
> +               if (ret)
> +                       return ret;
> +
> +               reg &= AXP20X_CHRG_CTRL1_TGT_CURR;
> +               val->intval = reg * 100000 + 300000;
> +               break;
> +
> +       case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
> +               val->intval = AXP20X_CHRG_CTRL1_TGT_CURR * 100000 + 300000;
> +               break;
> +
> +       case POWER_SUPPLY_PROP_CURRENT_NOW:
> +               ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_INPUT_STATUS,
> +                                 &reg);
> +               if (ret)
> +                       return ret;
> +
> +               if (reg & AXP20X_PWR_STATUS_BAT_CHARGING)
> +                       chan = axp20x_batt->batt_chrg_i;
> +               else
> +                       chan = axp20x_batt->batt_dischrg_i;
> +
> +               ret = iio_read_channel_processed(chan, &val->intval);
> +               if (ret)
> +                       return ret;
> +
> +               /* IIO framework gives mA but Power Supply framework gives uA */
> +               val->intval *= 1000;
> +               break;
> +
> +       case POWER_SUPPLY_PROP_CAPACITY:
> +               /* When no battery is present, return capacity is 100% */
> +               ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
> +                                 &reg);
> +               if (ret)
> +                       return ret;
> +
> +               if (!(reg & AXP20X_PWR_OP_BATT_PRESENT)) {
> +                       val->intval = 100;
> +                       return 0;
> +               }
> +
> +               ret = regmap_read(axp20x_batt->regmap, AXP20X_FG_RES, &reg);
> +               if (ret)
> +                       return ret;
> +
> +               if (axp20x_batt->axp_id == AXP221_ID &&
> +                   !(reg & AXP22X_FG_VALID))
> +                       return -EINVAL;
> +
> +               /*
> +                * Fuel Gauge data takes 7 bits but the stored value seems to be
> +                * directly the raw percentage without any scaling to 7 bits.
> +                */
> +               val->intval = reg & AXP209_FG_PERCENT;
> +               break;
> +
> +       case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
> +               if (axp20x_batt->axp_id == AXP209_ID)
> +                       return axp20x_battery_get_max_voltage(axp20x_batt,
> +                                                             &val->intval);
> +               return axp22x_battery_get_max_voltage(axp20x_batt,
> +                                                     &val->intval);
> +
> +       case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
> +               ret = regmap_read(axp20x_batt->regmap, AXP20X_V_OFF, &reg);
> +               if (ret)
> +                       return ret;
> +
> +               val->intval = 2600000 + 100000 * (reg & AXP20X_V_OFF_MASK);
> +               break;
> +
> +       case POWER_SUPPLY_PROP_VOLTAGE_NOW:
> +               ret = iio_read_channel_processed(axp20x_batt->batt_v,
> +                                                &val->intval);
> +               if (ret)
> +                       return ret;
> +
> +               /* IIO framework gives mV but Power Supply framework gives uV */
> +               val->intval *= 1000;
> +               break;
> +
> +       default:
> +               return -EINVAL;
> +       }
> +
> +       return 0;
> +}
> +
> +static int axp20x_battery_set_max_voltage(struct axp20x_batt_ps *axp20x_batt,
> +                                         int val)
> +{
> +       switch (val) {
> +       case 4100000:
> +               val = AXP20X_CHRG_CTRL1_TGT_4_1V;
> +               break;
> +
> +       case 4150000:
> +               if (axp20x_batt->axp_id == AXP221_ID)
> +                       return -EINVAL;
> +
> +               val = AXP20X_CHRG_CTRL1_TGT_4_15V;
> +               break;
> +
> +       case 4200000:
> +               val = AXP20X_CHRG_CTRL1_TGT_4_2V;
> +               break;
> +
> +       default:
> +               /*
> +                * AXP20x max voltage can be set to 4.36V and AXP22X max voltage
> +                * can be set to 4.22V and 4.24V, but these voltages are too
> +                * high for Lithium based batteries (AXP PMICs are supposed to
> +                * be used with these kinds of battery).
> +                */
> +               return -EINVAL;
> +       }
> +
> +       return regmap_update_bits(axp20x_batt->regmap, AXP20X_CHRG_CTRL1,
> +                                 AXP20X_CHRG_CTRL1_TGT_VOLT, val);
> +}
> +
> +static int axp20x_set_constant_charge_current(struct axp20x_batt_ps *axp_batt,
> +                                             int charge_current)
> +{
> +       if (axp_batt->axp_id == AXP209_ID)
> +               charge_current = (charge_current - 300000) / 100000;
> +       else
> +               charge_current = (charge_current - 300000) / 150000;
> +
> +       if (charge_current > AXP20X_CHRG_CTRL1_TGT_CURR || charge_current < 0)
> +               return -EINVAL;

I would add a check or warn if the value to be programmed exceeds that returned
by power_supply_get_battery_info. A charge current exceeding the limits of the
battery is potentially disastrous. The battery may be destroyed or even burst
into flames and explode, taking the board and anything nearby with it. Otherwise

Acked-by: Chen-Yu Tsai <wens@csie.org>

Speaking of power_supply_get_battery_info, is it merged or ready to be merged?

> +
> +       return regmap_update_bits(axp_batt->regmap, AXP20X_CHRG_CTRL1,
> +                                 AXP20X_CHRG_CTRL1_TGT_CURR, charge_current);
> +}
> +
> +static int axp20x_set_voltage_min_design(struct axp20x_batt_ps *axp_batt,
> +                                        int min_voltage)
> +{
> +       int val1 = (min_voltage - 2600000) / 100000;
> +
> +       if (val1 < 0 || val1 > AXP20X_V_OFF_MASK)
> +               return -EINVAL;
> +
> +       return regmap_update_bits(axp_batt->regmap, AXP20X_V_OFF,
> +                                 AXP20X_V_OFF_MASK, val1);
> +}
> +
> +static int axp20x_battery_set_prop(struct power_supply *psy,
> +                                  enum power_supply_property psp,
> +                                  const union power_supply_propval *val)
> +{
> +       struct axp20x_batt_ps *axp20x_batt = power_supply_get_drvdata(psy);
> +
> +       switch (psp) {
> +       case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
> +               return axp20x_set_voltage_min_design(axp20x_batt, val->intval);
> +
> +       case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
> +               return axp20x_battery_set_max_voltage(axp20x_batt, val->intval);
> +
> +       case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
> +               return axp20x_set_constant_charge_current(axp20x_batt,
> +                                                         val->intval);
> +
> +       default:
> +               return -EINVAL;
> +       }
> +}
> +
> +static enum power_supply_property axp20x_battery_props[] = {
> +       POWER_SUPPLY_PROP_PRESENT,
> +       POWER_SUPPLY_PROP_ONLINE,
> +       POWER_SUPPLY_PROP_STATUS,
> +       POWER_SUPPLY_PROP_VOLTAGE_NOW,
> +       POWER_SUPPLY_PROP_CURRENT_NOW,
> +       POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
> +       POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
> +       POWER_SUPPLY_PROP_HEALTH,
> +       POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
> +       POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
> +       POWER_SUPPLY_PROP_CAPACITY,
> +};
> +
> +static int axp20x_battery_prop_writeable(struct power_supply *psy,
> +                                        enum power_supply_property psp)
> +{
> +       return psp == POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN ||
> +              psp == POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN ||
> +              psp == POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT;
> +}
> +
> +static const struct power_supply_desc axp20x_batt_ps_desc = {
> +       .name = "axp20x-battery",
> +       .type = POWER_SUPPLY_TYPE_BATTERY,
> +       .properties = axp20x_battery_props,
> +       .num_properties = ARRAY_SIZE(axp20x_battery_props),
> +       .property_is_writeable = axp20x_battery_prop_writeable,
> +       .get_property = axp20x_battery_get_prop,
> +       .set_property = axp20x_battery_set_prop,
> +};
> +
> +static const struct of_device_id axp20x_battery_ps_id[] = {
> +       {
> +               .compatible = "x-powers,axp209-battery-power-supply",
> +               .data = (void *)AXP209_ID,
> +       }, {
> +               .compatible = "x-powers,axp221-battery-power-supply",
> +               .data = (void *)AXP221_ID,
> +       }, { /* sentinel */ },
> +};
> +MODULE_DEVICE_TABLE(of, axp20x_battery_ps_id);
> +
> +static int axp20x_power_probe(struct platform_device *pdev)
> +{
> +       struct axp20x_batt_ps *axp20x_batt;
> +       struct power_supply_config psy_cfg = {};
> +       struct power_supply_battery_info info;
> +
> +       if (!of_device_is_available(pdev->dev.of_node))
> +               return -ENODEV;
> +
> +       axp20x_batt = devm_kzalloc(&pdev->dev, sizeof(*axp20x_batt),
> +                                  GFP_KERNEL);
> +       if (!axp20x_batt)
> +               return -ENOMEM;
> +
> +       axp20x_batt->batt_v = devm_iio_channel_get(&pdev->dev, "batt_v");
> +       if (IS_ERR(axp20x_batt->batt_v)) {
> +               if (PTR_ERR(axp20x_batt->batt_v) == -ENODEV)
> +                       return -EPROBE_DEFER;
> +               return PTR_ERR(axp20x_batt->batt_v);
> +       }
> +
> +       axp20x_batt->batt_chrg_i = devm_iio_channel_get(&pdev->dev,
> +                                                       "batt_chrg_i");
> +       if (IS_ERR(axp20x_batt->batt_chrg_i)) {
> +               if (PTR_ERR(axp20x_batt->batt_chrg_i) == -ENODEV)
> +                       return -EPROBE_DEFER;
> +               return PTR_ERR(axp20x_batt->batt_chrg_i);
> +       }
> +
> +       axp20x_batt->batt_dischrg_i = devm_iio_channel_get(&pdev->dev,
> +                                                          "batt_dischrg_i");
> +       if (IS_ERR(axp20x_batt->batt_dischrg_i)) {
> +               if (PTR_ERR(axp20x_batt->batt_dischrg_i) == -ENODEV)
> +                       return -EPROBE_DEFER;
> +               return PTR_ERR(axp20x_batt->batt_dischrg_i);
> +       }
> +
> +       axp20x_batt->regmap = dev_get_regmap(pdev->dev.parent, NULL);
> +       platform_set_drvdata(pdev, axp20x_batt);
> +
> +       psy_cfg.drv_data = axp20x_batt;
> +       psy_cfg.of_node = pdev->dev.of_node;
> +
> +       axp20x_batt->axp_id = (uintptr_t)of_device_get_match_data(&pdev->dev);
> +
> +       axp20x_batt->batt = devm_power_supply_register(&pdev->dev,
> +                                                      &axp20x_batt_ps_desc,
> +                                                      &psy_cfg);
> +       if (IS_ERR(axp20x_batt->batt)) {
> +               dev_err(&pdev->dev, "failed to register power supply: %ld\n",
> +                       PTR_ERR(axp20x_batt->batt));
> +               return PTR_ERR(axp20x_batt->batt);
> +       }
> +
> +       if (!power_supply_get_battery_info(axp20x_batt->batt, &info)) {
> +               int vmin = info.voltage_min_design_uv;
> +               int ccc = info.constant_charge_current_ua;
> +
> +               if (vmin > 0 && axp20x_set_voltage_min_design(axp20x_batt,
> +                                                             vmin))
> +                       dev_err(&pdev->dev,
> +                               "couldn't set voltage_min_design\n");
> +
> +               if (ccc > 0 && axp20x_set_constant_charge_current(axp20x_batt,
> +                                                                 ccc))
> +                       dev_err(&pdev->dev,
> +                               "couldn't set constant charge current\n");
> +       }
> +
> +       return 0;
> +}
> +
> +static struct platform_driver axp20x_batt_driver = {
> +       .probe    = axp20x_power_probe,
> +       .driver   = {
> +               .name  = "axp20x-battery-power-supply",
> +               .of_match_table = axp20x_battery_ps_id,
> +       },
> +};
> +
> +module_platform_driver(axp20x_batt_driver);
> +
> +MODULE_DESCRIPTION("Battery power supply driver for AXP20X and AXP22X PMICs");
> +MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
> +MODULE_LICENSE("GPL");
> --
> 2.9.3
>

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 11/18] dt-bindings: power: supply: add AXP20X/AXP22X battery DT binding
  2017-02-14  9:41   ` Quentin Schulz
  (?)
@ 2017-02-21  4:45     ` Chen-Yu Tsai
  -1 siblings, 0 replies; 126+ messages in thread
From: Chen-Yu Tsai @ 2017-02-21  4:45 UTC (permalink / raw)
  To: Quentin Schulz
  Cc: knaack.h, Lars-Peter Clausen, Peter Meerwald-Stadler,
	Rob Herring, Mark Rutland, Chen-Yu Tsai, Sebastian Reichel,
	Russell King, Maxime Ripard, Lee Jones, linux-iio, devicetree,
	linux-kernel, open list:THERMAL, linux-arm-kernel,
	Thomas Petazzoni, Icenowy Zheng, Bruno Prémont

On Tue, Feb 14, 2017 at 5:41 PM, Quentin Schulz
<quentin.schulz@free-electrons.com> wrote:
> The X-Powers AXP20X and AXP22X PMICs can have a battery as power supply.
>
> This patch adds the DT binding documentation for the battery power
> supply which gets various data from the PMIC, such as the battery status
> (charging, discharging, full, dead), current max limit, current current,
> battery capacity (in percentage), voltage max and min limits, current
> voltage and battery capacity (in Ah).
>
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
> ---
>
> v3:
>  - removed constant charge current property, now should use the WIP
>  battery framework,

IIRC this should also include a property for referencing the battery?

Otherwise,

Acked-by: Chen-Yu Tsai <wens@csie.org>

>
> v2:
>  - changed DT node name from ac_power_supply to ac-power-supply,
>  - removed io-channels and io-channel-names from DT (the IIO mapping is
>  done in the IIO ADC driver now),
>  - added x-powers,constant-charge-current property to set the maximal
>  default constant current charge of the battery,
>
>  .../bindings/power/supply/axp20x_battery.txt         | 20 ++++++++++++++++++++
>  1 file changed, 20 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/power/supply/axp20x_battery.txt
>
> diff --git a/Documentation/devicetree/bindings/power/supply/axp20x_battery.txt b/Documentation/devicetree/bindings/power/supply/axp20x_battery.txt
> new file mode 100644
> index 0000000..c248866
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/power/supply/axp20x_battery.txt
> @@ -0,0 +1,20 @@
> +AXP20x and AXP22x battery power supply
> +
> +Required Properties:
> + - compatible, one of:
> +                       "x-powers,axp209-battery-power-supply"
> +                       "x-powers,axp221-battery-power-supply"
> +
> +This node is a subnode of the axp20x/axp22x PMIC.
> +
> +The AXP20X and AXP22X can read the battery voltage, charge and discharge
> +currents of the battery by reading ADC channels from the AXP20X/AXP22X
> +ADC.
> +
> +Example:
> +
> +&axp209 {
> +       battery_power_supply: battery-power-supply {
> +               compatible = "x-powers,axp209-battery-power-supply";
> +       }
> +};
> --
> 2.9.3
>

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 11/18] dt-bindings: power: supply: add AXP20X/AXP22X battery DT binding
@ 2017-02-21  4:45     ` Chen-Yu Tsai
  0 siblings, 0 replies; 126+ messages in thread
From: Chen-Yu Tsai @ 2017-02-21  4:45 UTC (permalink / raw)
  To: Quentin Schulz
  Cc: Mark Rutland, devicetree, Lars-Peter Clausen, open list:THERMAL,
	linux-iio, linux-kernel, Sebastian Reichel, Russell King,
	Chen-Yu Tsai, Rob Herring, Icenowy Zheng, Peter Meerwald-Stadler,
	knaack.h, Maxime Ripard, Bruno Prémont, Lee Jones,
	Thomas Petazzoni, linux-arm-kernel

On Tue, Feb 14, 2017 at 5:41 PM, Quentin Schulz
<quentin.schulz@free-electrons.com> wrote:
> The X-Powers AXP20X and AXP22X PMICs can have a battery as power supply.
>
> This patch adds the DT binding documentation for the battery power
> supply which gets various data from the PMIC, such as the battery status
> (charging, discharging, full, dead), current max limit, current current,
> battery capacity (in percentage), voltage max and min limits, current
> voltage and battery capacity (in Ah).
>
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
> ---
>
> v3:
>  - removed constant charge current property, now should use the WIP
>  battery framework,

IIRC this should also include a property for referencing the battery?

Otherwise,

Acked-by: Chen-Yu Tsai <wens@csie.org>

>
> v2:
>  - changed DT node name from ac_power_supply to ac-power-supply,
>  - removed io-channels and io-channel-names from DT (the IIO mapping is
>  done in the IIO ADC driver now),
>  - added x-powers,constant-charge-current property to set the maximal
>  default constant current charge of the battery,
>
>  .../bindings/power/supply/axp20x_battery.txt         | 20 ++++++++++++++++++++
>  1 file changed, 20 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/power/supply/axp20x_battery.txt
>
> diff --git a/Documentation/devicetree/bindings/power/supply/axp20x_battery.txt b/Documentation/devicetree/bindings/power/supply/axp20x_battery.txt
> new file mode 100644
> index 0000000..c248866
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/power/supply/axp20x_battery.txt
> @@ -0,0 +1,20 @@
> +AXP20x and AXP22x battery power supply
> +
> +Required Properties:
> + - compatible, one of:
> +                       "x-powers,axp209-battery-power-supply"
> +                       "x-powers,axp221-battery-power-supply"
> +
> +This node is a subnode of the axp20x/axp22x PMIC.
> +
> +The AXP20X and AXP22X can read the battery voltage, charge and discharge
> +currents of the battery by reading ADC channels from the AXP20X/AXP22X
> +ADC.
> +
> +Example:
> +
> +&axp209 {
> +       battery_power_supply: battery-power-supply {
> +               compatible = "x-powers,axp209-battery-power-supply";
> +       }
> +};
> --
> 2.9.3
>

^ permalink raw reply	[flat|nested] 126+ messages in thread

* [PATCH v3 11/18] dt-bindings: power: supply: add AXP20X/AXP22X battery DT binding
@ 2017-02-21  4:45     ` Chen-Yu Tsai
  0 siblings, 0 replies; 126+ messages in thread
From: Chen-Yu Tsai @ 2017-02-21  4:45 UTC (permalink / raw)
  To: linux-arm-kernel

On Tue, Feb 14, 2017 at 5:41 PM, Quentin Schulz
<quentin.schulz@free-electrons.com> wrote:
> The X-Powers AXP20X and AXP22X PMICs can have a battery as power supply.
>
> This patch adds the DT binding documentation for the battery power
> supply which gets various data from the PMIC, such as the battery status
> (charging, discharging, full, dead), current max limit, current current,
> battery capacity (in percentage), voltage max and min limits, current
> voltage and battery capacity (in Ah).
>
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
> ---
>
> v3:
>  - removed constant charge current property, now should use the WIP
>  battery framework,

IIRC this should also include a property for referencing the battery?

Otherwise,

Acked-by: Chen-Yu Tsai <wens@csie.org>

>
> v2:
>  - changed DT node name from ac_power_supply to ac-power-supply,
>  - removed io-channels and io-channel-names from DT (the IIO mapping is
>  done in the IIO ADC driver now),
>  - added x-powers,constant-charge-current property to set the maximal
>  default constant current charge of the battery,
>
>  .../bindings/power/supply/axp20x_battery.txt         | 20 ++++++++++++++++++++
>  1 file changed, 20 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/power/supply/axp20x_battery.txt
>
> diff --git a/Documentation/devicetree/bindings/power/supply/axp20x_battery.txt b/Documentation/devicetree/bindings/power/supply/axp20x_battery.txt
> new file mode 100644
> index 0000000..c248866
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/power/supply/axp20x_battery.txt
> @@ -0,0 +1,20 @@
> +AXP20x and AXP22x battery power supply
> +
> +Required Properties:
> + - compatible, one of:
> +                       "x-powers,axp209-battery-power-supply"
> +                       "x-powers,axp221-battery-power-supply"
> +
> +This node is a subnode of the axp20x/axp22x PMIC.
> +
> +The AXP20X and AXP22X can read the battery voltage, charge and discharge
> +currents of the battery by reading ADC channels from the AXP20X/AXP22X
> +ADC.
> +
> +Example:
> +
> +&axp209 {
> +       battery_power_supply: battery-power-supply {
> +               compatible = "x-powers,axp209-battery-power-supply";
> +       }
> +};
> --
> 2.9.3
>

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 14/18] mfd: axp20x: add MFD cells for AXP20X and AXP22X battery driver
  2017-02-14  9:41   ` Quentin Schulz
  (?)
@ 2017-02-21  4:46     ` Chen-Yu Tsai
  -1 siblings, 0 replies; 126+ messages in thread
From: Chen-Yu Tsai @ 2017-02-21  4:46 UTC (permalink / raw)
  To: Quentin Schulz
  Cc: knaack.h, Lars-Peter Clausen, Peter Meerwald-Stadler,
	Rob Herring, Mark Rutland, Chen-Yu Tsai, Sebastian Reichel,
	Russell King, Maxime Ripard, Lee Jones, linux-iio, devicetree,
	linux-kernel, open list:THERMAL, linux-arm-kernel,
	Thomas Petazzoni, Icenowy Zheng, Bruno Prémont

On Tue, Feb 14, 2017 at 5:41 PM, Quentin Schulz
<quentin.schulz@free-electrons.com> wrote:
> The X-Powers AXP20X and AXP22X PMICs can have a battery as power supply.
>
> This patch adds the AXP20X/AXP22X battery driver to the MFD cells of the
> AXP209, AXP221 and AXP223 MFD.
>
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> Acked-for-MFD-by: Lee Jones <lee.jones@linaro.org>

Acked-by: Chen-Yu Tsai <wens@csie.org>

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 14/18] mfd: axp20x: add MFD cells for AXP20X and AXP22X battery driver
@ 2017-02-21  4:46     ` Chen-Yu Tsai
  0 siblings, 0 replies; 126+ messages in thread
From: Chen-Yu Tsai @ 2017-02-21  4:46 UTC (permalink / raw)
  To: Quentin Schulz
  Cc: Mark Rutland, devicetree, Lars-Peter Clausen, open list:THERMAL,
	linux-iio, linux-kernel, Sebastian Reichel, Russell King,
	Chen-Yu Tsai, Rob Herring, Icenowy Zheng, Peter Meerwald-Stadler,
	knaack.h, Maxime Ripard, Bruno Prémont, Lee Jones,
	Thomas Petazzoni, linux-arm-kernel

On Tue, Feb 14, 2017 at 5:41 PM, Quentin Schulz
<quentin.schulz@free-electrons.com> wrote:
> The X-Powers AXP20X and AXP22X PMICs can have a battery as power supply.
>
> This patch adds the AXP20X/AXP22X battery driver to the MFD cells of the
> AXP209, AXP221 and AXP223 MFD.
>
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> Acked-for-MFD-by: Lee Jones <lee.jones@linaro.org>

Acked-by: Chen-Yu Tsai <wens@csie.org>

^ permalink raw reply	[flat|nested] 126+ messages in thread

* [PATCH v3 14/18] mfd: axp20x: add MFD cells for AXP20X and AXP22X battery driver
@ 2017-02-21  4:46     ` Chen-Yu Tsai
  0 siblings, 0 replies; 126+ messages in thread
From: Chen-Yu Tsai @ 2017-02-21  4:46 UTC (permalink / raw)
  To: linux-arm-kernel

On Tue, Feb 14, 2017 at 5:41 PM, Quentin Schulz
<quentin.schulz@free-electrons.com> wrote:
> The X-Powers AXP20X and AXP22X PMICs can have a battery as power supply.
>
> This patch adds the AXP20X/AXP22X battery driver to the MFD cells of the
> AXP209, AXP221 and AXP223 MFD.
>
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> Acked-for-MFD-by: Lee Jones <lee.jones@linaro.org>

Acked-by: Chen-Yu Tsai <wens@csie.org>

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 15/18] ARM: dtsi: axp209: add battery power supply subnode
  2017-02-14  9:41   ` Quentin Schulz
  (?)
@ 2017-02-21  4:47     ` Chen-Yu Tsai
  -1 siblings, 0 replies; 126+ messages in thread
From: Chen-Yu Tsai @ 2017-02-21  4:47 UTC (permalink / raw)
  To: Quentin Schulz
  Cc: knaack.h, Lars-Peter Clausen, Peter Meerwald-Stadler,
	Rob Herring, Mark Rutland, Chen-Yu Tsai, Sebastian Reichel,
	Russell King, Maxime Ripard, Lee Jones, linux-iio, devicetree,
	linux-kernel, open list:THERMAL, linux-arm-kernel,
	Thomas Petazzoni, Icenowy Zheng, Bruno Prémont

On Tue, Feb 14, 2017 at 5:41 PM, Quentin Schulz
<quentin.schulz@free-electrons.com> wrote:
> The X-Powers AXP209 PMIC exposes battery supply various data such as
> the battery status (charging, discharging, full, dead), current max
> limit, current current, battery capacity (in percentage), voltage max
> and min limits, current voltage, and battery capacity (in Ah).
>
> This adds the battery power supply subnode for AXP20X PMIC.
>
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>

Acked-by: Chen-Yu Tsai <wens@csie.org>

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 15/18] ARM: dtsi: axp209: add battery power supply subnode
@ 2017-02-21  4:47     ` Chen-Yu Tsai
  0 siblings, 0 replies; 126+ messages in thread
From: Chen-Yu Tsai @ 2017-02-21  4:47 UTC (permalink / raw)
  To: Quentin Schulz
  Cc: Mark Rutland, devicetree, Lars-Peter Clausen, open list:THERMAL,
	linux-iio, linux-kernel, Sebastian Reichel, Russell King,
	Chen-Yu Tsai, Rob Herring, Icenowy Zheng, Peter Meerwald-Stadler,
	knaack.h, Maxime Ripard, Bruno Prémont, Lee Jones,
	Thomas Petazzoni, linux-arm-kernel

On Tue, Feb 14, 2017 at 5:41 PM, Quentin Schulz
<quentin.schulz@free-electrons.com> wrote:
> The X-Powers AXP209 PMIC exposes battery supply various data such as
> the battery status (charging, discharging, full, dead), current max
> limit, current current, battery capacity (in percentage), voltage max
> and min limits, current voltage, and battery capacity (in Ah).
>
> This adds the battery power supply subnode for AXP20X PMIC.
>
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>

Acked-by: Chen-Yu Tsai <wens@csie.org>

^ permalink raw reply	[flat|nested] 126+ messages in thread

* [PATCH v3 15/18] ARM: dtsi: axp209: add battery power supply subnode
@ 2017-02-21  4:47     ` Chen-Yu Tsai
  0 siblings, 0 replies; 126+ messages in thread
From: Chen-Yu Tsai @ 2017-02-21  4:47 UTC (permalink / raw)
  To: linux-arm-kernel

On Tue, Feb 14, 2017 at 5:41 PM, Quentin Schulz
<quentin.schulz@free-electrons.com> wrote:
> The X-Powers AXP209 PMIC exposes battery supply various data such as
> the battery status (charging, discharging, full, dead), current max
> limit, current current, battery capacity (in percentage), voltage max
> and min limits, current voltage, and battery capacity (in Ah).
>
> This adds the battery power supply subnode for AXP20X PMIC.
>
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>

Acked-by: Chen-Yu Tsai <wens@csie.org>

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 16/18] ARM: dtsi: axp22x: add battery power supply subnode
  2017-02-14  9:41   ` Quentin Schulz
  (?)
@ 2017-02-21  4:47     ` Chen-Yu Tsai
  -1 siblings, 0 replies; 126+ messages in thread
From: Chen-Yu Tsai @ 2017-02-21  4:47 UTC (permalink / raw)
  To: Quentin Schulz
  Cc: knaack.h, Lars-Peter Clausen, Peter Meerwald-Stadler,
	Rob Herring, Mark Rutland, Chen-Yu Tsai, Sebastian Reichel,
	Russell King, Maxime Ripard, Lee Jones, linux-iio, devicetree,
	linux-kernel, open list:THERMAL, linux-arm-kernel,
	Thomas Petazzoni, Icenowy Zheng, Bruno Prémont

On Tue, Feb 14, 2017 at 5:41 PM, Quentin Schulz
<quentin.schulz@free-electrons.com> wrote:
> The X-Powers AXP22X PMIC exposes battery supply various data such as
> the battery status (charging, discharging, full, dead), current max
> limit, current current, battery capacity (in percentage), voltage max
> limit, current voltage, and battery capacity (in Ah).
>
> This adds the battery power supply subnode for AXP22X PMIC.
>
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>

Acked-by: Chen-Yu Tsai <wens@csie.org>

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 16/18] ARM: dtsi: axp22x: add battery power supply subnode
@ 2017-02-21  4:47     ` Chen-Yu Tsai
  0 siblings, 0 replies; 126+ messages in thread
From: Chen-Yu Tsai @ 2017-02-21  4:47 UTC (permalink / raw)
  To: Quentin Schulz
  Cc: Mark Rutland, devicetree, Lars-Peter Clausen, open list:THERMAL,
	linux-iio, linux-kernel, Sebastian Reichel, Russell King,
	Chen-Yu Tsai, Rob Herring, Icenowy Zheng, Peter Meerwald-Stadler,
	knaack.h, Maxime Ripard, Bruno Prémont, Lee Jones,
	Thomas Petazzoni, linux-arm-kernel

On Tue, Feb 14, 2017 at 5:41 PM, Quentin Schulz
<quentin.schulz@free-electrons.com> wrote:
> The X-Powers AXP22X PMIC exposes battery supply various data such as
> the battery status (charging, discharging, full, dead), current max
> limit, current current, battery capacity (in percentage), voltage max
> limit, current voltage, and battery capacity (in Ah).
>
> This adds the battery power supply subnode for AXP22X PMIC.
>
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>

Acked-by: Chen-Yu Tsai <wens@csie.org>

^ permalink raw reply	[flat|nested] 126+ messages in thread

* [PATCH v3 16/18] ARM: dtsi: axp22x: add battery power supply subnode
@ 2017-02-21  4:47     ` Chen-Yu Tsai
  0 siblings, 0 replies; 126+ messages in thread
From: Chen-Yu Tsai @ 2017-02-21  4:47 UTC (permalink / raw)
  To: linux-arm-kernel

On Tue, Feb 14, 2017 at 5:41 PM, Quentin Schulz
<quentin.schulz@free-electrons.com> wrote:
> The X-Powers AXP22X PMIC exposes battery supply various data such as
> the battery status (charging, discharging, full, dead), current max
> limit, current current, battery capacity (in percentage), voltage max
> limit, current voltage, and battery capacity (in Ah).
>
> This adds the battery power supply subnode for AXP22X PMIC.
>
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>

Acked-by: Chen-Yu Tsai <wens@csie.org>

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 17/18] ARM: dts: sun8i: sina33: enable battery power supply subnode
  2017-02-14  9:41   ` Quentin Schulz
  (?)
@ 2017-02-21  4:50     ` Chen-Yu Tsai
  -1 siblings, 0 replies; 126+ messages in thread
From: Chen-Yu Tsai @ 2017-02-21  4:50 UTC (permalink / raw)
  To: Quentin Schulz
  Cc: knaack.h, Lars-Peter Clausen, Peter Meerwald-Stadler,
	Rob Herring, Mark Rutland, Chen-Yu Tsai, Sebastian Reichel,
	Russell King, Maxime Ripard, Lee Jones, linux-iio, devicetree,
	linux-kernel, open list:THERMAL, linux-arm-kernel,
	Thomas Petazzoni, Icenowy Zheng, Bruno Prémont

On Tue, Feb 14, 2017 at 5:41 PM, Quentin Schulz
<quentin.schulz@free-electrons.com> wrote:
> The Sinlinx SinA33 has an AXP223 PMIC and a battery connector, thus, we
> enable the battery power supply subnode in its Device Tree.
>
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>

Battery charger enabled without any description of the associated battery...
In this case I think the driver should use the lowest charge current to be
safe?

Acked-by: Chen-Yu Tsai <wens@csie.org>

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 17/18] ARM: dts: sun8i: sina33: enable battery power supply subnode
@ 2017-02-21  4:50     ` Chen-Yu Tsai
  0 siblings, 0 replies; 126+ messages in thread
From: Chen-Yu Tsai @ 2017-02-21  4:50 UTC (permalink / raw)
  To: Quentin Schulz
  Cc: Mark Rutland, devicetree, Lars-Peter Clausen, open list:THERMAL,
	linux-iio, linux-kernel, Sebastian Reichel, Russell King,
	Chen-Yu Tsai, Rob Herring, Icenowy Zheng, Peter Meerwald-Stadler,
	knaack.h, Maxime Ripard, Bruno Prémont, Lee Jones,
	Thomas Petazzoni, linux-arm-kernel

On Tue, Feb 14, 2017 at 5:41 PM, Quentin Schulz
<quentin.schulz@free-electrons.com> wrote:
> The Sinlinx SinA33 has an AXP223 PMIC and a battery connector, thus, we
> enable the battery power supply subnode in its Device Tree.
>
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>

Battery charger enabled without any description of the associated battery...
In this case I think the driver should use the lowest charge current to be
safe?

Acked-by: Chen-Yu Tsai <wens@csie.org>

^ permalink raw reply	[flat|nested] 126+ messages in thread

* [PATCH v3 17/18] ARM: dts: sun8i: sina33: enable battery power supply subnode
@ 2017-02-21  4:50     ` Chen-Yu Tsai
  0 siblings, 0 replies; 126+ messages in thread
From: Chen-Yu Tsai @ 2017-02-21  4:50 UTC (permalink / raw)
  To: linux-arm-kernel

On Tue, Feb 14, 2017 at 5:41 PM, Quentin Schulz
<quentin.schulz@free-electrons.com> wrote:
> The Sinlinx SinA33 has an AXP223 PMIC and a battery connector, thus, we
> enable the battery power supply subnode in its Device Tree.
>
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>

Battery charger enabled without any description of the associated battery...
In this case I think the driver should use the lowest charge current to be
safe?

Acked-by: Chen-Yu Tsai <wens@csie.org>

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 18/18] ARM: sun5i: chip: enable battery power supply subnode
  2017-02-14  9:41   ` Quentin Schulz
@ 2017-02-21  4:50     ` Chen-Yu Tsai
  -1 siblings, 0 replies; 126+ messages in thread
From: Chen-Yu Tsai @ 2017-02-21  4:50 UTC (permalink / raw)
  To: Quentin Schulz
  Cc: knaack.h, Lars-Peter Clausen, Peter Meerwald-Stadler,
	Rob Herring, Mark Rutland, Chen-Yu Tsai, Sebastian Reichel,
	Russell King, Maxime Ripard, Lee Jones, linux-iio, devicetree,
	linux-kernel, open list:THERMAL, linux-arm-kernel,
	Thomas Petazzoni, Icenowy Zheng, Bruno Prémont

On Tue, Feb 14, 2017 at 5:41 PM, Quentin Schulz
<quentin.schulz@free-electrons.com> wrote:
> The NextThing Co. CHIP has an AXP209 PMIC with battery connector.
>
> This enables the battery power supply subnode.
>
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>

Acked-by: Chen-Yu Tsai <wens@csie.org>

^ permalink raw reply	[flat|nested] 126+ messages in thread

* [PATCH v3 18/18] ARM: sun5i: chip: enable battery power supply subnode
@ 2017-02-21  4:50     ` Chen-Yu Tsai
  0 siblings, 0 replies; 126+ messages in thread
From: Chen-Yu Tsai @ 2017-02-21  4:50 UTC (permalink / raw)
  To: linux-arm-kernel

On Tue, Feb 14, 2017 at 5:41 PM, Quentin Schulz
<quentin.schulz@free-electrons.com> wrote:
> The NextThing Co. CHIP has an AXP209 PMIC with battery connector.
>
> This enables the battery power supply subnode.
>
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>

Acked-by: Chen-Yu Tsai <wens@csie.org>

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 05/18] mfd: axp20x: add ADC cells for AXP20X and AXP22X PMICs
  2017-02-15  9:37     ` Lee Jones
@ 2017-02-21  4:51       ` Chen-Yu Tsai
  -1 siblings, 0 replies; 126+ messages in thread
From: Chen-Yu Tsai @ 2017-02-21  4:51 UTC (permalink / raw)
  To: Lee Jones
  Cc: Quentin Schulz, knaack.h, Lars-Peter Clausen,
	Peter Meerwald-Stadler, Rob Herring, Mark Rutland, Chen-Yu Tsai,
	Sebastian Reichel, Russell King, Maxime Ripard, linux-iio,
	devicetree, linux-kernel, open list:THERMAL, linux-arm-kernel,
	Thomas Petazzoni, Icenowy Zheng, Bruno Prémont

On Wed, Feb 15, 2017 at 5:37 PM, Lee Jones <lee.jones@linaro.org> wrote:
> On Tue, 14 Feb 2017, Quentin Schulz wrote:
>
>> This adds the AXP20X/AXP22x ADCs driver to the mfd cells of the AXP209,
>> AXP221 and AXP223 MFD.
>>
>> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
>> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
>> ---
>>
>> v3:
>>  - removed of_compatible as DT node has been removed,
>>  - use different names to probe the ADC driver,
>>
>>  drivers/mfd/axp20x.c | 6 ++++++
>>  1 file changed, 6 insertions(+)
>
> For my own reference:
>   Acked-for-MFD-by: Lee Jones <lee.jones@linaro.org>

Acked-by: Chen-Yu Tsai <wens@csie.org>

^ permalink raw reply	[flat|nested] 126+ messages in thread

* [PATCH v3 05/18] mfd: axp20x: add ADC cells for AXP20X and AXP22X PMICs
@ 2017-02-21  4:51       ` Chen-Yu Tsai
  0 siblings, 0 replies; 126+ messages in thread
From: Chen-Yu Tsai @ 2017-02-21  4:51 UTC (permalink / raw)
  To: linux-arm-kernel

On Wed, Feb 15, 2017 at 5:37 PM, Lee Jones <lee.jones@linaro.org> wrote:
> On Tue, 14 Feb 2017, Quentin Schulz wrote:
>
>> This adds the AXP20X/AXP22x ADCs driver to the mfd cells of the AXP209,
>> AXP221 and AXP223 MFD.
>>
>> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
>> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
>> ---
>>
>> v3:
>>  - removed of_compatible as DT node has been removed,
>>  - use different names to probe the ADC driver,
>>
>>  drivers/mfd/axp20x.c | 6 ++++++
>>  1 file changed, 6 insertions(+)
>
> For my own reference:
>   Acked-for-MFD-by: Lee Jones <lee.jones@linaro.org>

Acked-by: Chen-Yu Tsai <wens@csie.org>

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 01/18] dt-bindings: power: battery: add constant-charge-current property
  2017-02-15  8:53       ` Quentin Schulz
  (?)
@ 2017-02-21  4:55         ` Chen-Yu Tsai
  -1 siblings, 0 replies; 126+ messages in thread
From: Chen-Yu Tsai @ 2017-02-21  4:55 UTC (permalink / raw)
  To: Quentin Schulz
  Cc: Liam Breck, knaack.h, Lars-Peter Clausen, Peter Meerwald-Stadler,
	Rob Herring, Mark Rutland, Chen-Yu Tsai, Sebastian Reichel,
	Russell King, Maxime Ripard, Lee Jones, Thomas Petazzoni,
	devicetree, open list:THERMAL, linux-iio, linux-kernel,
	Bruno Prémont, Icenowy Zheng, linux-arm-kernel

On Wed, Feb 15, 2017 at 4:53 PM, Quentin Schulz
<quentin.schulz@free-electrons.com> wrote:
> Hi,
>
> On 15/02/2017 01:46, Liam Breck wrote:
>>
>> On Tue, 14 Feb 2017 10:40:55 +0100 Quentin Schulz wrote:
>>> This adds the constant-charge-current property to the list of optional
>>> properties of the battery.
>>>
>>> The constant charge current is critical for batteries as they can't
>>> handle all charge currents.
>>>
>>> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
>>> ---
>>>
>>> added in v3
>>>
>>>  Documentation/devicetree/bindings/power/supply/battery.txt | 2 ++
>>
>> Is constant-charge-current dependent on the battery (e.g. capacity, nominal voltage, etc) or the
>> system (charger chip, input current/voltage, etc)?
>>
>> It belongs in Doc.../power/supply/battery.txt if it's a characteristic of the battery.
>>
>> Note, this page asserts that constant-current charging applies to NiMH batteries:
>> http://power-topics.blogspot.com/2016/05/constant-voltage-constant-current.html
>>
>> Related properties to be added to battery.txt near-future in a patchset for the BQ24190
>> charger are as follows. These are not currently in enum power_supply_property, so the actual names
>> are still to be decided.
>>
>> precharge-current-microamp:
>>    maximum charge current during precharge phase (typically 20% of battery capacity)
>>
>> termination-current-microamp (or endcharge-current):
>>    a charge cycle terminates when the battery voltage is above recharge threshold,
>>    and the current is below this setting (typically 10% of battery capacity)
>>
>
> We have a client with a board whose battery accepts a maximum of 300mA
> for charging. So depending on the battery, we cannot have any charging
> current we want. The AXP PmMICs set constant charge current in a range
> of 300mA-1800mA, so it is enforced by the charger but needs to be
> adapted depending on the battery present in the system.

May I ask the capacity of the battery in question?

Typically one charges LiPo batteries at no more than 1C. So if it's
a 1800 mAh battery, the charge current should be no more than 1800 mA.

ChenYu

> The AXP PMICs charge battery with constant current (Ichrg) between the
> trickle voltage (Vtrkl which is ~3.0V) and the targeted voltage (Vtrgt;
> which seems to be the voltage telling the battery is fully charged).
>
> So if I understand correctly, "my" constant-charge-current would be
> located in the charging cycle between your precharge-current-microamp
> and the termination-current-microamp as it is the current for the
> charging process as a whole.
>
> See here[1] for the explanation in the datasheet (page 20).
>
> That would definitely match what is explained in your link for constant
> current.
>
> [1] http://dl.linux-sunxi.org/AXP/AXP209_Datasheet_v1.0en.pdf
>
> Let me know if something seems odd,
> Thanks,
> Quentin
>
> --
> Quentin Schulz, Free Electrons
> Embedded Linux and Kernel engineering
> http://free-electrons.com

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 01/18] dt-bindings: power: battery: add constant-charge-current property
@ 2017-02-21  4:55         ` Chen-Yu Tsai
  0 siblings, 0 replies; 126+ messages in thread
From: Chen-Yu Tsai @ 2017-02-21  4:55 UTC (permalink / raw)
  To: Quentin Schulz
  Cc: Mark Rutland, Thomas Petazzoni, Lars-Peter Clausen, devicetree,
	Liam Breck, linux-kernel, open list:THERMAL, Sebastian Reichel,
	Russell King, linux-iio, Chen-Yu Tsai, Rob Herring,
	Icenowy Zheng, Peter Meerwald-Stadler, knaack.h, Maxime Ripard,
	Bruno Prémont, Lee Jones, linux-arm-kernel

On Wed, Feb 15, 2017 at 4:53 PM, Quentin Schulz
<quentin.schulz@free-electrons.com> wrote:
> Hi,
>
> On 15/02/2017 01:46, Liam Breck wrote:
>>
>> On Tue, 14 Feb 2017 10:40:55 +0100 Quentin Schulz wrote:
>>> This adds the constant-charge-current property to the list of optional
>>> properties of the battery.
>>>
>>> The constant charge current is critical for batteries as they can't
>>> handle all charge currents.
>>>
>>> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
>>> ---
>>>
>>> added in v3
>>>
>>>  Documentation/devicetree/bindings/power/supply/battery.txt | 2 ++
>>
>> Is constant-charge-current dependent on the battery (e.g. capacity, nominal voltage, etc) or the
>> system (charger chip, input current/voltage, etc)?
>>
>> It belongs in Doc.../power/supply/battery.txt if it's a characteristic of the battery.
>>
>> Note, this page asserts that constant-current charging applies to NiMH batteries:
>> http://power-topics.blogspot.com/2016/05/constant-voltage-constant-current.html
>>
>> Related properties to be added to battery.txt near-future in a patchset for the BQ24190
>> charger are as follows. These are not currently in enum power_supply_property, so the actual names
>> are still to be decided.
>>
>> precharge-current-microamp:
>>    maximum charge current during precharge phase (typically 20% of battery capacity)
>>
>> termination-current-microamp (or endcharge-current):
>>    a charge cycle terminates when the battery voltage is above recharge threshold,
>>    and the current is below this setting (typically 10% of battery capacity)
>>
>
> We have a client with a board whose battery accepts a maximum of 300mA
> for charging. So depending on the battery, we cannot have any charging
> current we want. The AXP PmMICs set constant charge current in a range
> of 300mA-1800mA, so it is enforced by the charger but needs to be
> adapted depending on the battery present in the system.

May I ask the capacity of the battery in question?

Typically one charges LiPo batteries at no more than 1C. So if it's
a 1800 mAh battery, the charge current should be no more than 1800 mA.

ChenYu

> The AXP PMICs charge battery with constant current (Ichrg) between the
> trickle voltage (Vtrkl which is ~3.0V) and the targeted voltage (Vtrgt;
> which seems to be the voltage telling the battery is fully charged).
>
> So if I understand correctly, "my" constant-charge-current would be
> located in the charging cycle between your precharge-current-microamp
> and the termination-current-microamp as it is the current for the
> charging process as a whole.
>
> See here[1] for the explanation in the datasheet (page 20).
>
> That would definitely match what is explained in your link for constant
> current.
>
> [1] http://dl.linux-sunxi.org/AXP/AXP209_Datasheet_v1.0en.pdf
>
> Let me know if something seems odd,
> Thanks,
> Quentin
>
> --
> Quentin Schulz, Free Electrons
> Embedded Linux and Kernel engineering
> http://free-electrons.com

^ permalink raw reply	[flat|nested] 126+ messages in thread

* [PATCH v3 01/18] dt-bindings: power: battery: add constant-charge-current property
@ 2017-02-21  4:55         ` Chen-Yu Tsai
  0 siblings, 0 replies; 126+ messages in thread
From: Chen-Yu Tsai @ 2017-02-21  4:55 UTC (permalink / raw)
  To: linux-arm-kernel

On Wed, Feb 15, 2017 at 4:53 PM, Quentin Schulz
<quentin.schulz@free-electrons.com> wrote:
> Hi,
>
> On 15/02/2017 01:46, Liam Breck wrote:
>>
>> On Tue, 14 Feb 2017 10:40:55 +0100 Quentin Schulz wrote:
>>> This adds the constant-charge-current property to the list of optional
>>> properties of the battery.
>>>
>>> The constant charge current is critical for batteries as they can't
>>> handle all charge currents.
>>>
>>> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
>>> ---
>>>
>>> added in v3
>>>
>>>  Documentation/devicetree/bindings/power/supply/battery.txt | 2 ++
>>
>> Is constant-charge-current dependent on the battery (e.g. capacity, nominal voltage, etc) or the
>> system (charger chip, input current/voltage, etc)?
>>
>> It belongs in Doc.../power/supply/battery.txt if it's a characteristic of the battery.
>>
>> Note, this page asserts that constant-current charging applies to NiMH batteries:
>> http://power-topics.blogspot.com/2016/05/constant-voltage-constant-current.html
>>
>> Related properties to be added to battery.txt near-future in a patchset for the BQ24190
>> charger are as follows. These are not currently in enum power_supply_property, so the actual names
>> are still to be decided.
>>
>> precharge-current-microamp:
>>    maximum charge current during precharge phase (typically 20% of battery capacity)
>>
>> termination-current-microamp (or endcharge-current):
>>    a charge cycle terminates when the battery voltage is above recharge threshold,
>>    and the current is below this setting (typically 10% of battery capacity)
>>
>
> We have a client with a board whose battery accepts a maximum of 300mA
> for charging. So depending on the battery, we cannot have any charging
> current we want. The AXP PmMICs set constant charge current in a range
> of 300mA-1800mA, so it is enforced by the charger but needs to be
> adapted depending on the battery present in the system.

May I ask the capacity of the battery in question?

Typically one charges LiPo batteries at no more than 1C. So if it's
a 1800 mAh battery, the charge current should be no more than 1800 mA.

ChenYu

> The AXP PMICs charge battery with constant current (Ichrg) between the
> trickle voltage (Vtrkl which is ~3.0V) and the targeted voltage (Vtrgt;
> which seems to be the voltage telling the battery is fully charged).
>
> So if I understand correctly, "my" constant-charge-current would be
> located in the charging cycle between your precharge-current-microamp
> and the termination-current-microamp as it is the current for the
> charging process as a whole.
>
> See here[1] for the explanation in the datasheet (page 20).
>
> That would definitely match what is explained in your link for constant
> current.
>
> [1] http://dl.linux-sunxi.org/AXP/AXP209_Datasheet_v1.0en.pdf
>
> Let me know if something seems odd,
> Thanks,
> Quentin
>
> --
> Quentin Schulz, Free Electrons
> Embedded Linux and Kernel engineering
> http://free-electrons.com

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 04/18] iio: adc: add support for X-Powers AXP20X and AXP22X PMICs ADCs
  2017-02-19 12:40     ` Jonathan Cameron
  (?)
@ 2017-02-21 18:03       ` Quentin Schulz
  -1 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-21 18:03 UTC (permalink / raw)
  To: Jonathan Cameron, knaack.h, lars, pmeerw, robh+dt, mark.rutland,
	wens, sre, linux, maxime.ripard, lee.jones
  Cc: linux-iio, devicetree, linux-kernel, linux-pm, linux-arm-kernel,
	thomas.petazzoni, icenowy, bonbons

Hi Jonathan,

On 19/02/2017 13:40, Jonathan Cameron wrote:
> On 14/02/17 09:40, Quentin Schulz wrote:
>> The X-Powers AXP20X and AXP22X PMICs have multiple ADCs. They expose the
>> battery voltage, battery charge and discharge currents, AC-in and VBUS
>> voltages and currents, 2 GPIOs muxable in ADC mode and PMIC temperature.
>>
>> This adds support for most of AXP20X and AXP22X ADCs.
>>
>> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
>> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
> Hi Quentin,
> 
> A few bits and bobs inline.  The bigest one is that I don't think
> you need to carry your struct axp_data pointer around in the iio_priv
> structure as it only seems to be used in probe.
> 
> Anyhow, tidy these little bits up (quite a few are optional in the
> sense that they are a matter or personal taste and I don't feel
> strongly about them) and you can add
> 
> Reviewed-by: Jonathan Cameron <jic23@kernel.org>
> 
> A nice driver.
> 
> Jonathan
[...]
>> +static int axp20x_probe(struct platform_device *pdev)
>> +{
>> +	struct axp20x_adc_iio *info;
>> +	struct iio_dev *indio_dev;
>> +	struct axp20x_dev *axp20x_dev;
>> +	int ret;
>> +
>> +	axp20x_dev = dev_get_drvdata(pdev->dev.parent);
>> +
>> +	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*info));
>> +	if (!indio_dev)
>> +		return -ENOMEM;
>> +
>> +	info = iio_priv(indio_dev);
>> +	platform_set_drvdata(pdev, indio_dev);
>> +
>> +	info->regmap = axp20x_dev->regmap;
>> +	indio_dev->dev.parent = &pdev->dev;
>> +	indio_dev->dev.of_node = pdev->dev.of_node;
>> +	indio_dev->modes = INDIO_DIRECT_MODE;
>> +
>> +	info->data = (struct axp_data *)platform_get_device_id(pdev)->driver_data;
> I think this is only actually used in probe, so could be handled more
> neatly with a local variable rather than sticking it in info.
> 
[...]
>> +static int axp20x_remove(struct platform_device *pdev)
>> +{
>> +	struct axp20x_adc_iio *info;
>> +	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
>> +
>> +	info = iio_priv(indio_dev);
> Reorder the two lines above this one and then you could just do this
> inline. (really trivial so feel free to not bother ;)
>> +
>> +	iio_device_unregister(indio_dev);
>> +	iio_map_array_unregister(indio_dev);
>> +
>> +	regmap_write(info->regmap, AXP20X_ADC_EN1, 0);
>> +
>> +	if (info->data->adc_en2)

data is used here to know if I should disable misc ADCs (if supported by
the PMIC). So kinda need it only for that purpose, maybe I can use a
slightly simpler structure to only store regmap and this adc_en2 boolean.

Ack for everything else.

Thanks,
Quentin

-- 
Quentin Schulz, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 04/18] iio: adc: add support for X-Powers AXP20X and AXP22X PMICs ADCs
@ 2017-02-21 18:03       ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-21 18:03 UTC (permalink / raw)
  To: Jonathan Cameron, knaack.h, lars, pmeerw, robh+dt, mark.rutland,
	wens, sre, linux, maxime.ripard, lee.jones
  Cc: thomas.petazzoni, devicetree, linux-pm, linux-iio, linux-kernel,
	bonbons, icenowy, linux-arm-kernel

Hi Jonathan,

On 19/02/2017 13:40, Jonathan Cameron wrote:
> On 14/02/17 09:40, Quentin Schulz wrote:
>> The X-Powers AXP20X and AXP22X PMICs have multiple ADCs. They expose the
>> battery voltage, battery charge and discharge currents, AC-in and VBUS
>> voltages and currents, 2 GPIOs muxable in ADC mode and PMIC temperature.
>>
>> This adds support for most of AXP20X and AXP22X ADCs.
>>
>> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
>> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
> Hi Quentin,
> 
> A few bits and bobs inline.  The bigest one is that I don't think
> you need to carry your struct axp_data pointer around in the iio_priv
> structure as it only seems to be used in probe.
> 
> Anyhow, tidy these little bits up (quite a few are optional in the
> sense that they are a matter or personal taste and I don't feel
> strongly about them) and you can add
> 
> Reviewed-by: Jonathan Cameron <jic23@kernel.org>
> 
> A nice driver.
> 
> Jonathan
[...]
>> +static int axp20x_probe(struct platform_device *pdev)
>> +{
>> +	struct axp20x_adc_iio *info;
>> +	struct iio_dev *indio_dev;
>> +	struct axp20x_dev *axp20x_dev;
>> +	int ret;
>> +
>> +	axp20x_dev = dev_get_drvdata(pdev->dev.parent);
>> +
>> +	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*info));
>> +	if (!indio_dev)
>> +		return -ENOMEM;
>> +
>> +	info = iio_priv(indio_dev);
>> +	platform_set_drvdata(pdev, indio_dev);
>> +
>> +	info->regmap = axp20x_dev->regmap;
>> +	indio_dev->dev.parent = &pdev->dev;
>> +	indio_dev->dev.of_node = pdev->dev.of_node;
>> +	indio_dev->modes = INDIO_DIRECT_MODE;
>> +
>> +	info->data = (struct axp_data *)platform_get_device_id(pdev)->driver_data;
> I think this is only actually used in probe, so could be handled more
> neatly with a local variable rather than sticking it in info.
> 
[...]
>> +static int axp20x_remove(struct platform_device *pdev)
>> +{
>> +	struct axp20x_adc_iio *info;
>> +	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
>> +
>> +	info = iio_priv(indio_dev);
> Reorder the two lines above this one and then you could just do this
> inline. (really trivial so feel free to not bother ;)
>> +
>> +	iio_device_unregister(indio_dev);
>> +	iio_map_array_unregister(indio_dev);
>> +
>> +	regmap_write(info->regmap, AXP20X_ADC_EN1, 0);
>> +
>> +	if (info->data->adc_en2)

data is used here to know if I should disable misc ADCs (if supported by
the PMIC). So kinda need it only for that purpose, maybe I can use a
slightly simpler structure to only store regmap and this adc_en2 boolean.

Ack for everything else.

Thanks,
Quentin

-- 
Quentin Schulz, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

^ permalink raw reply	[flat|nested] 126+ messages in thread

* [PATCH v3 04/18] iio: adc: add support for X-Powers AXP20X and AXP22X PMICs ADCs
@ 2017-02-21 18:03       ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-21 18:03 UTC (permalink / raw)
  To: linux-arm-kernel

Hi Jonathan,

On 19/02/2017 13:40, Jonathan Cameron wrote:
> On 14/02/17 09:40, Quentin Schulz wrote:
>> The X-Powers AXP20X and AXP22X PMICs have multiple ADCs. They expose the
>> battery voltage, battery charge and discharge currents, AC-in and VBUS
>> voltages and currents, 2 GPIOs muxable in ADC mode and PMIC temperature.
>>
>> This adds support for most of AXP20X and AXP22X ADCs.
>>
>> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
>> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
> Hi Quentin,
> 
> A few bits and bobs inline.  The bigest one is that I don't think
> you need to carry your struct axp_data pointer around in the iio_priv
> structure as it only seems to be used in probe.
> 
> Anyhow, tidy these little bits up (quite a few are optional in the
> sense that they are a matter or personal taste and I don't feel
> strongly about them) and you can add
> 
> Reviewed-by: Jonathan Cameron <jic23@kernel.org>
> 
> A nice driver.
> 
> Jonathan
[...]
>> +static int axp20x_probe(struct platform_device *pdev)
>> +{
>> +	struct axp20x_adc_iio *info;
>> +	struct iio_dev *indio_dev;
>> +	struct axp20x_dev *axp20x_dev;
>> +	int ret;
>> +
>> +	axp20x_dev = dev_get_drvdata(pdev->dev.parent);
>> +
>> +	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*info));
>> +	if (!indio_dev)
>> +		return -ENOMEM;
>> +
>> +	info = iio_priv(indio_dev);
>> +	platform_set_drvdata(pdev, indio_dev);
>> +
>> +	info->regmap = axp20x_dev->regmap;
>> +	indio_dev->dev.parent = &pdev->dev;
>> +	indio_dev->dev.of_node = pdev->dev.of_node;
>> +	indio_dev->modes = INDIO_DIRECT_MODE;
>> +
>> +	info->data = (struct axp_data *)platform_get_device_id(pdev)->driver_data;
> I think this is only actually used in probe, so could be handled more
> neatly with a local variable rather than sticking it in info.
> 
[...]
>> +static int axp20x_remove(struct platform_device *pdev)
>> +{
>> +	struct axp20x_adc_iio *info;
>> +	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
>> +
>> +	info = iio_priv(indio_dev);
> Reorder the two lines above this one and then you could just do this
> inline. (really trivial so feel free to not bother ;)
>> +
>> +	iio_device_unregister(indio_dev);
>> +	iio_map_array_unregister(indio_dev);
>> +
>> +	regmap_write(info->regmap, AXP20X_ADC_EN1, 0);
>> +
>> +	if (info->data->adc_en2)

data is used here to know if I should disable misc ADCs (if supported by
the PMIC). So kinda need it only for that purpose, maybe I can use a
slightly simpler structure to only store regmap and this adc_en2 boolean.

Ack for everything else.

Thanks,
Quentin

-- 
Quentin Schulz, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 11/18] dt-bindings: power: supply: add AXP20X/AXP22X battery DT binding
  2017-02-21  4:45     ` Chen-Yu Tsai
  (?)
@ 2017-02-21 18:05       ` Quentin Schulz
  -1 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-21 18:05 UTC (permalink / raw)
  To: Chen-Yu Tsai
  Cc: knaack.h, Lars-Peter Clausen, Peter Meerwald-Stadler,
	Rob Herring, Mark Rutland, Sebastian Reichel, Russell King,
	Maxime Ripard, Lee Jones, linux-iio, devicetree, linux-kernel,
	open list:THERMAL, linux-arm-kernel, Thomas Petazzoni,
	Icenowy Zheng, Bruno Prémont

Hi Chen-Yu,

On 21/02/2017 05:45, Chen-Yu Tsai wrote:
> On Tue, Feb 14, 2017 at 5:41 PM, Quentin Schulz
> <quentin.schulz@free-electrons.com> wrote:
>> The X-Powers AXP20X and AXP22X PMICs can have a battery as power supply.
>>
>> This patch adds the DT binding documentation for the battery power
>> supply which gets various data from the PMIC, such as the battery status
>> (charging, discharging, full, dead), current max limit, current current,
>> battery capacity (in percentage), voltage max and min limits, current
>> voltage and battery capacity (in Ah).
>>
>> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
>> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
>> ---
>>
>> v3:
>>  - removed constant charge current property, now should use the WIP
>>  battery framework,
> 
> IIRC this should also include a property for referencing the battery?
> 

Indeed.

Quentin

> Otherwise,
> 
> Acked-by: Chen-Yu Tsai <wens@csie.org>
> 
>>
>> v2:
>>  - changed DT node name from ac_power_supply to ac-power-supply,
>>  - removed io-channels and io-channel-names from DT (the IIO mapping is
>>  done in the IIO ADC driver now),
>>  - added x-powers,constant-charge-current property to set the maximal
>>  default constant current charge of the battery,
>>
>>  .../bindings/power/supply/axp20x_battery.txt         | 20 ++++++++++++++++++++
>>  1 file changed, 20 insertions(+)
>>  create mode 100644 Documentation/devicetree/bindings/power/supply/axp20x_battery.txt
>>
>> diff --git a/Documentation/devicetree/bindings/power/supply/axp20x_battery.txt b/Documentation/devicetree/bindings/power/supply/axp20x_battery.txt
>> new file mode 100644
>> index 0000000..c248866
>> --- /dev/null
>> +++ b/Documentation/devicetree/bindings/power/supply/axp20x_battery.txt
>> @@ -0,0 +1,20 @@
>> +AXP20x and AXP22x battery power supply
>> +
>> +Required Properties:
>> + - compatible, one of:
>> +                       "x-powers,axp209-battery-power-supply"
>> +                       "x-powers,axp221-battery-power-supply"
>> +
>> +This node is a subnode of the axp20x/axp22x PMIC.
>> +
>> +The AXP20X and AXP22X can read the battery voltage, charge and discharge
>> +currents of the battery by reading ADC channels from the AXP20X/AXP22X
>> +ADC.
>> +
>> +Example:
>> +
>> +&axp209 {
>> +       battery_power_supply: battery-power-supply {
>> +               compatible = "x-powers,axp209-battery-power-supply";
>> +       }
>> +};
>> --
>> 2.9.3
>>

-- 
Quentin Schulz, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 11/18] dt-bindings: power: supply: add AXP20X/AXP22X battery DT binding
@ 2017-02-21 18:05       ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-21 18:05 UTC (permalink / raw)
  To: Chen-Yu Tsai
  Cc: Mark Rutland, devicetree, Lars-Peter Clausen, open list:THERMAL,
	linux-iio, linux-kernel, Sebastian Reichel, Russell King,
	Bruno Prémont, Rob Herring, Icenowy Zheng,
	Peter Meerwald-Stadler, knaack.h, Maxime Ripard, Lee Jones,
	Thomas Petazzoni, linux-arm-kernel

Hi Chen-Yu,

On 21/02/2017 05:45, Chen-Yu Tsai wrote:
> On Tue, Feb 14, 2017 at 5:41 PM, Quentin Schulz
> <quentin.schulz@free-electrons.com> wrote:
>> The X-Powers AXP20X and AXP22X PMICs can have a battery as power supply.
>>
>> This patch adds the DT binding documentation for the battery power
>> supply which gets various data from the PMIC, such as the battery status
>> (charging, discharging, full, dead), current max limit, current current,
>> battery capacity (in percentage), voltage max and min limits, current
>> voltage and battery capacity (in Ah).
>>
>> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
>> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
>> ---
>>
>> v3:
>>  - removed constant charge current property, now should use the WIP
>>  battery framework,
> 
> IIRC this should also include a property for referencing the battery?
> 

Indeed.

Quentin

> Otherwise,
> 
> Acked-by: Chen-Yu Tsai <wens@csie.org>
> 
>>
>> v2:
>>  - changed DT node name from ac_power_supply to ac-power-supply,
>>  - removed io-channels and io-channel-names from DT (the IIO mapping is
>>  done in the IIO ADC driver now),
>>  - added x-powers,constant-charge-current property to set the maximal
>>  default constant current charge of the battery,
>>
>>  .../bindings/power/supply/axp20x_battery.txt         | 20 ++++++++++++++++++++
>>  1 file changed, 20 insertions(+)
>>  create mode 100644 Documentation/devicetree/bindings/power/supply/axp20x_battery.txt
>>
>> diff --git a/Documentation/devicetree/bindings/power/supply/axp20x_battery.txt b/Documentation/devicetree/bindings/power/supply/axp20x_battery.txt
>> new file mode 100644
>> index 0000000..c248866
>> --- /dev/null
>> +++ b/Documentation/devicetree/bindings/power/supply/axp20x_battery.txt
>> @@ -0,0 +1,20 @@
>> +AXP20x and AXP22x battery power supply
>> +
>> +Required Properties:
>> + - compatible, one of:
>> +                       "x-powers,axp209-battery-power-supply"
>> +                       "x-powers,axp221-battery-power-supply"
>> +
>> +This node is a subnode of the axp20x/axp22x PMIC.
>> +
>> +The AXP20X and AXP22X can read the battery voltage, charge and discharge
>> +currents of the battery by reading ADC channels from the AXP20X/AXP22X
>> +ADC.
>> +
>> +Example:
>> +
>> +&axp209 {
>> +       battery_power_supply: battery-power-supply {
>> +               compatible = "x-powers,axp209-battery-power-supply";
>> +       }
>> +};
>> --
>> 2.9.3
>>

-- 
Quentin Schulz, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

^ permalink raw reply	[flat|nested] 126+ messages in thread

* [PATCH v3 11/18] dt-bindings: power: supply: add AXP20X/AXP22X battery DT binding
@ 2017-02-21 18:05       ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-21 18:05 UTC (permalink / raw)
  To: linux-arm-kernel

Hi Chen-Yu,

On 21/02/2017 05:45, Chen-Yu Tsai wrote:
> On Tue, Feb 14, 2017 at 5:41 PM, Quentin Schulz
> <quentin.schulz@free-electrons.com> wrote:
>> The X-Powers AXP20X and AXP22X PMICs can have a battery as power supply.
>>
>> This patch adds the DT binding documentation for the battery power
>> supply which gets various data from the PMIC, such as the battery status
>> (charging, discharging, full, dead), current max limit, current current,
>> battery capacity (in percentage), voltage max and min limits, current
>> voltage and battery capacity (in Ah).
>>
>> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
>> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
>> ---
>>
>> v3:
>>  - removed constant charge current property, now should use the WIP
>>  battery framework,
> 
> IIRC this should also include a property for referencing the battery?
> 

Indeed.

Quentin

> Otherwise,
> 
> Acked-by: Chen-Yu Tsai <wens@csie.org>
> 
>>
>> v2:
>>  - changed DT node name from ac_power_supply to ac-power-supply,
>>  - removed io-channels and io-channel-names from DT (the IIO mapping is
>>  done in the IIO ADC driver now),
>>  - added x-powers,constant-charge-current property to set the maximal
>>  default constant current charge of the battery,
>>
>>  .../bindings/power/supply/axp20x_battery.txt         | 20 ++++++++++++++++++++
>>  1 file changed, 20 insertions(+)
>>  create mode 100644 Documentation/devicetree/bindings/power/supply/axp20x_battery.txt
>>
>> diff --git a/Documentation/devicetree/bindings/power/supply/axp20x_battery.txt b/Documentation/devicetree/bindings/power/supply/axp20x_battery.txt
>> new file mode 100644
>> index 0000000..c248866
>> --- /dev/null
>> +++ b/Documentation/devicetree/bindings/power/supply/axp20x_battery.txt
>> @@ -0,0 +1,20 @@
>> +AXP20x and AXP22x battery power supply
>> +
>> +Required Properties:
>> + - compatible, one of:
>> +                       "x-powers,axp209-battery-power-supply"
>> +                       "x-powers,axp221-battery-power-supply"
>> +
>> +This node is a subnode of the axp20x/axp22x PMIC.
>> +
>> +The AXP20X and AXP22X can read the battery voltage, charge and discharge
>> +currents of the battery by reading ADC channels from the AXP20X/AXP22X
>> +ADC.
>> +
>> +Example:
>> +
>> +&axp209 {
>> +       battery_power_supply: battery-power-supply {
>> +               compatible = "x-powers,axp209-battery-power-supply";
>> +       }
>> +};
>> --
>> 2.9.3
>>

-- 
Quentin Schulz, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 13/18] power: supply: add battery driver for AXP20X and AXP22X PMICs
  2017-02-21  4:44     ` Chen-Yu Tsai
  (?)
@ 2017-02-21 18:19       ` Quentin Schulz
  -1 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-21 18:19 UTC (permalink / raw)
  To: Chen-Yu Tsai
  Cc: knaack.h, Lars-Peter Clausen, Peter Meerwald-Stadler,
	Rob Herring, Mark Rutland, Sebastian Reichel, Russell King,
	Maxime Ripard, Lee Jones, linux-iio, devicetree, linux-kernel,
	open list:THERMAL, linux-arm-kernel, Thomas Petazzoni,
	Icenowy Zheng, Bruno Prémont

Hi Chen-Yu,

On 21/02/2017 05:44, Chen-Yu Tsai wrote:
> On Tue, Feb 14, 2017 at 5:41 PM, Quentin Schulz
> <quentin.schulz@free-electrons.com> wrote:
>> The X-Powers AXP20X and AXP22X PMICs can have a battery as power supply.
>>
>> This patch adds the battery power supply driver to get various data from
>> the PMIC, such as the battery status (charging, discharging, full,
>> dead), current max limit, current current, battery capacity (in
>> percentage), voltage max and min limits, current voltage and battery
>> capacity (in Ah).
>>
>> This battery driver uses the AXP20X/AXP22X ADC driver as PMIC data
>> provider.
>>
>> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
>> Acked-by: Jonathan Cameron <jic23@kernel.org>
>> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
[...]
>> +static int axp20x_set_constant_charge_current(struct axp20x_batt_ps *axp_batt,
>> +                                             int charge_current)
>> +{
>> +       if (axp_batt->axp_id == AXP209_ID)
>> +               charge_current = (charge_current - 300000) / 100000;
>> +       else
>> +               charge_current = (charge_current - 300000) / 150000;
>> +
>> +       if (charge_current > AXP20X_CHRG_CTRL1_TGT_CURR || charge_current < 0)
>> +               return -EINVAL;
> 
> I would add a check or warn if the value to be programmed exceeds that returned
> by power_supply_get_battery_info.

Completely agree on a warning.

> A charge current exceeding the limits of the
> battery is potentially disastrous. The battery may be destroyed or even burst
> into flames and explode, taking the board and anything nearby with it. Otherwise
> 

Yes, I understand. Maybe I'm seeing this ability to set the (max)
constant current charge the wrong way. Here is what I think:
If we limit the max constant charge current with a DT property, it would
require a DT rebuild when changing the battery (i.e. if an end-user
decides to change the battery with a bigger constant charge current, he
has to recompile the DT to change the DT value).

What I can suggest is the following:
 - set the max constant charge current and the default constant charge
current from the DT property,
 - allow the user to change the constant charge current via sysfs within
minimal-DT value range,
 - allow the user to set max constant charge current via sysfs (and
print a warning as well when setting it), then the user can set a higher
constant charge current,

That would require a two steps modification with a printed warning.
"Safer" but does not remove the ability to change the constant charge
current in the case of battery swapping/changing.

> Acked-by: Chen-Yu Tsai <wens@csie.org>
> 
> Speaking of power_supply_get_battery_info, is it merged or ready to be merged?
> 

v7 under way IIRC.

Thanks,
Quentin

-- 
Quentin Schulz, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 13/18] power: supply: add battery driver for AXP20X and AXP22X PMICs
@ 2017-02-21 18:19       ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-21 18:19 UTC (permalink / raw)
  To: Chen-Yu Tsai
  Cc: Mark Rutland, devicetree, Lars-Peter Clausen, open list:THERMAL,
	linux-iio, linux-kernel, Sebastian Reichel, Russell King,
	Bruno Prémont, Rob Herring, Icenowy Zheng,
	Peter Meerwald-Stadler, knaack.h, Maxime Ripard, Lee Jones,
	Thomas Petazzoni, linux-arm-kernel

Hi Chen-Yu,

On 21/02/2017 05:44, Chen-Yu Tsai wrote:
> On Tue, Feb 14, 2017 at 5:41 PM, Quentin Schulz
> <quentin.schulz@free-electrons.com> wrote:
>> The X-Powers AXP20X and AXP22X PMICs can have a battery as power supply.
>>
>> This patch adds the battery power supply driver to get various data from
>> the PMIC, such as the battery status (charging, discharging, full,
>> dead), current max limit, current current, battery capacity (in
>> percentage), voltage max and min limits, current voltage and battery
>> capacity (in Ah).
>>
>> This battery driver uses the AXP20X/AXP22X ADC driver as PMIC data
>> provider.
>>
>> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
>> Acked-by: Jonathan Cameron <jic23@kernel.org>
>> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
[...]
>> +static int axp20x_set_constant_charge_current(struct axp20x_batt_ps *axp_batt,
>> +                                             int charge_current)
>> +{
>> +       if (axp_batt->axp_id == AXP209_ID)
>> +               charge_current = (charge_current - 300000) / 100000;
>> +       else
>> +               charge_current = (charge_current - 300000) / 150000;
>> +
>> +       if (charge_current > AXP20X_CHRG_CTRL1_TGT_CURR || charge_current < 0)
>> +               return -EINVAL;
> 
> I would add a check or warn if the value to be programmed exceeds that returned
> by power_supply_get_battery_info.

Completely agree on a warning.

> A charge current exceeding the limits of the
> battery is potentially disastrous. The battery may be destroyed or even burst
> into flames and explode, taking the board and anything nearby with it. Otherwise
> 

Yes, I understand. Maybe I'm seeing this ability to set the (max)
constant current charge the wrong way. Here is what I think:
If we limit the max constant charge current with a DT property, it would
require a DT rebuild when changing the battery (i.e. if an end-user
decides to change the battery with a bigger constant charge current, he
has to recompile the DT to change the DT value).

What I can suggest is the following:
 - set the max constant charge current and the default constant charge
current from the DT property,
 - allow the user to change the constant charge current via sysfs within
minimal-DT value range,
 - allow the user to set max constant charge current via sysfs (and
print a warning as well when setting it), then the user can set a higher
constant charge current,

That would require a two steps modification with a printed warning.
"Safer" but does not remove the ability to change the constant charge
current in the case of battery swapping/changing.

> Acked-by: Chen-Yu Tsai <wens@csie.org>
> 
> Speaking of power_supply_get_battery_info, is it merged or ready to be merged?
> 

v7 under way IIRC.

Thanks,
Quentin

-- 
Quentin Schulz, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

^ permalink raw reply	[flat|nested] 126+ messages in thread

* [PATCH v3 13/18] power: supply: add battery driver for AXP20X and AXP22X PMICs
@ 2017-02-21 18:19       ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-21 18:19 UTC (permalink / raw)
  To: linux-arm-kernel

Hi Chen-Yu,

On 21/02/2017 05:44, Chen-Yu Tsai wrote:
> On Tue, Feb 14, 2017 at 5:41 PM, Quentin Schulz
> <quentin.schulz@free-electrons.com> wrote:
>> The X-Powers AXP20X and AXP22X PMICs can have a battery as power supply.
>>
>> This patch adds the battery power supply driver to get various data from
>> the PMIC, such as the battery status (charging, discharging, full,
>> dead), current max limit, current current, battery capacity (in
>> percentage), voltage max and min limits, current voltage and battery
>> capacity (in Ah).
>>
>> This battery driver uses the AXP20X/AXP22X ADC driver as PMIC data
>> provider.
>>
>> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
>> Acked-by: Jonathan Cameron <jic23@kernel.org>
>> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
[...]
>> +static int axp20x_set_constant_charge_current(struct axp20x_batt_ps *axp_batt,
>> +                                             int charge_current)
>> +{
>> +       if (axp_batt->axp_id == AXP209_ID)
>> +               charge_current = (charge_current - 300000) / 100000;
>> +       else
>> +               charge_current = (charge_current - 300000) / 150000;
>> +
>> +       if (charge_current > AXP20X_CHRG_CTRL1_TGT_CURR || charge_current < 0)
>> +               return -EINVAL;
> 
> I would add a check or warn if the value to be programmed exceeds that returned
> by power_supply_get_battery_info.

Completely agree on a warning.

> A charge current exceeding the limits of the
> battery is potentially disastrous. The battery may be destroyed or even burst
> into flames and explode, taking the board and anything nearby with it. Otherwise
> 

Yes, I understand. Maybe I'm seeing this ability to set the (max)
constant current charge the wrong way. Here is what I think:
If we limit the max constant charge current with a DT property, it would
require a DT rebuild when changing the battery (i.e. if an end-user
decides to change the battery with a bigger constant charge current, he
has to recompile the DT to change the DT value).

What I can suggest is the following:
 - set the max constant charge current and the default constant charge
current from the DT property,
 - allow the user to change the constant charge current via sysfs within
minimal-DT value range,
 - allow the user to set max constant charge current via sysfs (and
print a warning as well when setting it), then the user can set a higher
constant charge current,

That would require a two steps modification with a printed warning.
"Safer" but does not remove the ability to change the constant charge
current in the case of battery swapping/changing.

> Acked-by: Chen-Yu Tsai <wens@csie.org>
> 
> Speaking of power_supply_get_battery_info, is it merged or ready to be merged?
> 

v7 under way IIRC.

Thanks,
Quentin

-- 
Quentin Schulz, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 17/18] ARM: dts: sun8i: sina33: enable battery power supply subnode
  2017-02-21  4:50     ` Chen-Yu Tsai
  (?)
@ 2017-02-21 18:20       ` Quentin Schulz
  -1 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-21 18:20 UTC (permalink / raw)
  To: Chen-Yu Tsai
  Cc: knaack.h, Lars-Peter Clausen, Peter Meerwald-Stadler,
	Rob Herring, Mark Rutland, Sebastian Reichel, Russell King,
	Maxime Ripard, Lee Jones, linux-iio, devicetree, linux-kernel,
	open list:THERMAL, linux-arm-kernel, Thomas Petazzoni,
	Icenowy Zheng, Bruno Prémont

On 21/02/2017 05:50, Chen-Yu Tsai wrote:
> On Tue, Feb 14, 2017 at 5:41 PM, Quentin Schulz
> <quentin.schulz@free-electrons.com> wrote:
>> The Sinlinx SinA33 has an AXP223 PMIC and a battery connector, thus, we
>> enable the battery power supply subnode in its Device Tree.
>>
>> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
>> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
> 
> Battery charger enabled without any description of the associated battery...
> In this case I think the driver should use the lowest charge current to be
> safe?
> 

Seems fair.

Quentin

-- 
Quentin Schulz, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 17/18] ARM: dts: sun8i: sina33: enable battery power supply subnode
@ 2017-02-21 18:20       ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-21 18:20 UTC (permalink / raw)
  To: Chen-Yu Tsai
  Cc: Mark Rutland, devicetree, Lars-Peter Clausen, open list:THERMAL,
	linux-iio, linux-kernel, Sebastian Reichel, Russell King,
	Bruno Prémont, Rob Herring, Icenowy Zheng,
	Peter Meerwald-Stadler, knaack.h, Maxime Ripard, Lee Jones,
	Thomas Petazzoni, linux-arm-kernel

On 21/02/2017 05:50, Chen-Yu Tsai wrote:
> On Tue, Feb 14, 2017 at 5:41 PM, Quentin Schulz
> <quentin.schulz@free-electrons.com> wrote:
>> The Sinlinx SinA33 has an AXP223 PMIC and a battery connector, thus, we
>> enable the battery power supply subnode in its Device Tree.
>>
>> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
>> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
> 
> Battery charger enabled without any description of the associated battery...
> In this case I think the driver should use the lowest charge current to be
> safe?
> 

Seems fair.

Quentin

-- 
Quentin Schulz, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

^ permalink raw reply	[flat|nested] 126+ messages in thread

* [PATCH v3 17/18] ARM: dts: sun8i: sina33: enable battery power supply subnode
@ 2017-02-21 18:20       ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-02-21 18:20 UTC (permalink / raw)
  To: linux-arm-kernel

On 21/02/2017 05:50, Chen-Yu Tsai wrote:
> On Tue, Feb 14, 2017 at 5:41 PM, Quentin Schulz
> <quentin.schulz@free-electrons.com> wrote:
>> The Sinlinx SinA33 has an AXP223 PMIC and a battery connector, thus, we
>> enable the battery power supply subnode in its Device Tree.
>>
>> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
>> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
> 
> Battery charger enabled without any description of the associated battery...
> In this case I think the driver should use the lowest charge current to be
> safe?
> 

Seems fair.

Quentin

-- 
Quentin Schulz, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 13/18] power: supply: add battery driver for AXP20X and AXP22X PMICs
  2017-02-21 18:19       ` Quentin Schulz
  (?)
@ 2017-02-22  6:22         ` Chen-Yu Tsai
  -1 siblings, 0 replies; 126+ messages in thread
From: Chen-Yu Tsai @ 2017-02-22  6:22 UTC (permalink / raw)
  To: Quentin Schulz
  Cc: Chen-Yu Tsai, knaack.h, Lars-Peter Clausen,
	Peter Meerwald-Stadler, Rob Herring, Mark Rutland,
	Sebastian Reichel, Russell King, Maxime Ripard, Lee Jones,
	linux-iio, devicetree, linux-kernel, open list:THERMAL,
	linux-arm-kernel, Thomas Petazzoni, Icenowy Zheng,
	Bruno Prémont

Hi,

On Wed, Feb 22, 2017 at 2:19 AM, Quentin Schulz
<quentin.schulz@free-electrons.com> wrote:
> Hi Chen-Yu,
>
> On 21/02/2017 05:44, Chen-Yu Tsai wrote:
>> On Tue, Feb 14, 2017 at 5:41 PM, Quentin Schulz
>> <quentin.schulz@free-electrons.com> wrote:
>>> The X-Powers AXP20X and AXP22X PMICs can have a battery as power supply.
>>>
>>> This patch adds the battery power supply driver to get various data from
>>> the PMIC, such as the battery status (charging, discharging, full,
>>> dead), current max limit, current current, battery capacity (in
>>> percentage), voltage max and min limits, current voltage and battery
>>> capacity (in Ah).
>>>
>>> This battery driver uses the AXP20X/AXP22X ADC driver as PMIC data
>>> provider.
>>>
>>> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
>>> Acked-by: Jonathan Cameron <jic23@kernel.org>
>>> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
> [...]
>>> +static int axp20x_set_constant_charge_current(struct axp20x_batt_ps *axp_batt,
>>> +                                             int charge_current)
>>> +{
>>> +       if (axp_batt->axp_id == AXP209_ID)
>>> +               charge_current = (charge_current - 300000) / 100000;
>>> +       else
>>> +               charge_current = (charge_current - 300000) / 150000;
>>> +
>>> +       if (charge_current > AXP20X_CHRG_CTRL1_TGT_CURR || charge_current < 0)
>>> +               return -EINVAL;
>>
>> I would add a check or warn if the value to be programmed exceeds that returned
>> by power_supply_get_battery_info.
>
> Completely agree on a warning.
>
>> A charge current exceeding the limits of the
>> battery is potentially disastrous. The battery may be destroyed or even burst
>> into flames and explode, taking the board and anything nearby with it. Otherwise
>>
>
> Yes, I understand. Maybe I'm seeing this ability to set the (max)
> constant current charge the wrong way. Here is what I think:
> If we limit the max constant charge current with a DT property, it would
> require a DT rebuild when changing the battery (i.e. if an end-user
> decides to change the battery with a bigger constant charge current, he
> has to recompile the DT to change the DT value).

I guess this mostly applies for development boards.

Swapping out the battery would also affect design capacity, which may be
exported to userspace via the power supply / fuel gauge device? What then?

> What I can suggest is the following:
>  - set the max constant charge current and the default constant charge
> current from the DT property,

OK.

>  - allow the user to change the constant charge current via sysfs within
> minimal-DT value range,

You mean the "current" max constant charge current known to the driver, right?

>  - allow the user to set max constant charge current via sysfs (and
> print a warning as well when setting it), then the user can set a higher
> constant charge current,
>
> That would require a two steps modification with a printed warning.
> "Safer" but does not remove the ability to change the constant charge
> current in the case of battery swapping/changing.

Yeah that sounds like a good proposal. If you want to be really safe,
you could make the option to override the maximum a sysfs option? It
is possible warning messages aren't immediately noticed, such as when
the user is running X.

Reminds me of the anti foot-shooting option in FreeBSD.

Regards
ChenYu


>> Acked-by: Chen-Yu Tsai <wens@csie.org>
>>
>> Speaking of power_supply_get_battery_info, is it merged or ready to be merged?
>>
>
> v7 under way IIRC.
>
> Thanks,
> Quentin
>
> --
> Quentin Schulz, Free Electrons
> Embedded Linux and Kernel engineering
> http://free-electrons.com

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 13/18] power: supply: add battery driver for AXP20X and AXP22X PMICs
@ 2017-02-22  6:22         ` Chen-Yu Tsai
  0 siblings, 0 replies; 126+ messages in thread
From: Chen-Yu Tsai @ 2017-02-22  6:22 UTC (permalink / raw)
  To: Quentin Schulz
  Cc: Mark Rutland, devicetree, Lars-Peter Clausen, open list:THERMAL,
	linux-iio, linux-kernel, Sebastian Reichel, Russell King,
	Chen-Yu Tsai, Rob Herring, Icenowy Zheng, Peter Meerwald-Stadler,
	knaack.h, Maxime Ripard, Bruno Prémont, Lee Jones,
	Thomas Petazzoni, linux-arm-kernel

Hi,

On Wed, Feb 22, 2017 at 2:19 AM, Quentin Schulz
<quentin.schulz@free-electrons.com> wrote:
> Hi Chen-Yu,
>
> On 21/02/2017 05:44, Chen-Yu Tsai wrote:
>> On Tue, Feb 14, 2017 at 5:41 PM, Quentin Schulz
>> <quentin.schulz@free-electrons.com> wrote:
>>> The X-Powers AXP20X and AXP22X PMICs can have a battery as power supply.
>>>
>>> This patch adds the battery power supply driver to get various data from
>>> the PMIC, such as the battery status (charging, discharging, full,
>>> dead), current max limit, current current, battery capacity (in
>>> percentage), voltage max and min limits, current voltage and battery
>>> capacity (in Ah).
>>>
>>> This battery driver uses the AXP20X/AXP22X ADC driver as PMIC data
>>> provider.
>>>
>>> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
>>> Acked-by: Jonathan Cameron <jic23@kernel.org>
>>> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
> [...]
>>> +static int axp20x_set_constant_charge_current(struct axp20x_batt_ps *axp_batt,
>>> +                                             int charge_current)
>>> +{
>>> +       if (axp_batt->axp_id == AXP209_ID)
>>> +               charge_current = (charge_current - 300000) / 100000;
>>> +       else
>>> +               charge_current = (charge_current - 300000) / 150000;
>>> +
>>> +       if (charge_current > AXP20X_CHRG_CTRL1_TGT_CURR || charge_current < 0)
>>> +               return -EINVAL;
>>
>> I would add a check or warn if the value to be programmed exceeds that returned
>> by power_supply_get_battery_info.
>
> Completely agree on a warning.
>
>> A charge current exceeding the limits of the
>> battery is potentially disastrous. The battery may be destroyed or even burst
>> into flames and explode, taking the board and anything nearby with it. Otherwise
>>
>
> Yes, I understand. Maybe I'm seeing this ability to set the (max)
> constant current charge the wrong way. Here is what I think:
> If we limit the max constant charge current with a DT property, it would
> require a DT rebuild when changing the battery (i.e. if an end-user
> decides to change the battery with a bigger constant charge current, he
> has to recompile the DT to change the DT value).

I guess this mostly applies for development boards.

Swapping out the battery would also affect design capacity, which may be
exported to userspace via the power supply / fuel gauge device? What then?

> What I can suggest is the following:
>  - set the max constant charge current and the default constant charge
> current from the DT property,

OK.

>  - allow the user to change the constant charge current via sysfs within
> minimal-DT value range,

You mean the "current" max constant charge current known to the driver, right?

>  - allow the user to set max constant charge current via sysfs (and
> print a warning as well when setting it), then the user can set a higher
> constant charge current,
>
> That would require a two steps modification with a printed warning.
> "Safer" but does not remove the ability to change the constant charge
> current in the case of battery swapping/changing.

Yeah that sounds like a good proposal. If you want to be really safe,
you could make the option to override the maximum a sysfs option? It
is possible warning messages aren't immediately noticed, such as when
the user is running X.

Reminds me of the anti foot-shooting option in FreeBSD.

Regards
ChenYu


>> Acked-by: Chen-Yu Tsai <wens@csie.org>
>>
>> Speaking of power_supply_get_battery_info, is it merged or ready to be merged?
>>
>
> v7 under way IIRC.
>
> Thanks,
> Quentin
>
> --
> Quentin Schulz, Free Electrons
> Embedded Linux and Kernel engineering
> http://free-electrons.com

^ permalink raw reply	[flat|nested] 126+ messages in thread

* [PATCH v3 13/18] power: supply: add battery driver for AXP20X and AXP22X PMICs
@ 2017-02-22  6:22         ` Chen-Yu Tsai
  0 siblings, 0 replies; 126+ messages in thread
From: Chen-Yu Tsai @ 2017-02-22  6:22 UTC (permalink / raw)
  To: linux-arm-kernel

Hi,

On Wed, Feb 22, 2017 at 2:19 AM, Quentin Schulz
<quentin.schulz@free-electrons.com> wrote:
> Hi Chen-Yu,
>
> On 21/02/2017 05:44, Chen-Yu Tsai wrote:
>> On Tue, Feb 14, 2017 at 5:41 PM, Quentin Schulz
>> <quentin.schulz@free-electrons.com> wrote:
>>> The X-Powers AXP20X and AXP22X PMICs can have a battery as power supply.
>>>
>>> This patch adds the battery power supply driver to get various data from
>>> the PMIC, such as the battery status (charging, discharging, full,
>>> dead), current max limit, current current, battery capacity (in
>>> percentage), voltage max and min limits, current voltage and battery
>>> capacity (in Ah).
>>>
>>> This battery driver uses the AXP20X/AXP22X ADC driver as PMIC data
>>> provider.
>>>
>>> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
>>> Acked-by: Jonathan Cameron <jic23@kernel.org>
>>> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
> [...]
>>> +static int axp20x_set_constant_charge_current(struct axp20x_batt_ps *axp_batt,
>>> +                                             int charge_current)
>>> +{
>>> +       if (axp_batt->axp_id == AXP209_ID)
>>> +               charge_current = (charge_current - 300000) / 100000;
>>> +       else
>>> +               charge_current = (charge_current - 300000) / 150000;
>>> +
>>> +       if (charge_current > AXP20X_CHRG_CTRL1_TGT_CURR || charge_current < 0)
>>> +               return -EINVAL;
>>
>> I would add a check or warn if the value to be programmed exceeds that returned
>> by power_supply_get_battery_info.
>
> Completely agree on a warning.
>
>> A charge current exceeding the limits of the
>> battery is potentially disastrous. The battery may be destroyed or even burst
>> into flames and explode, taking the board and anything nearby with it. Otherwise
>>
>
> Yes, I understand. Maybe I'm seeing this ability to set the (max)
> constant current charge the wrong way. Here is what I think:
> If we limit the max constant charge current with a DT property, it would
> require a DT rebuild when changing the battery (i.e. if an end-user
> decides to change the battery with a bigger constant charge current, he
> has to recompile the DT to change the DT value).

I guess this mostly applies for development boards.

Swapping out the battery would also affect design capacity, which may be
exported to userspace via the power supply / fuel gauge device? What then?

> What I can suggest is the following:
>  - set the max constant charge current and the default constant charge
> current from the DT property,

OK.

>  - allow the user to change the constant charge current via sysfs within
> minimal-DT value range,

You mean the "current" max constant charge current known to the driver, right?

>  - allow the user to set max constant charge current via sysfs (and
> print a warning as well when setting it), then the user can set a higher
> constant charge current,
>
> That would require a two steps modification with a printed warning.
> "Safer" but does not remove the ability to change the constant charge
> current in the case of battery swapping/changing.

Yeah that sounds like a good proposal. If you want to be really safe,
you could make the option to override the maximum a sysfs option? It
is possible warning messages aren't immediately noticed, such as when
the user is running X.

Reminds me of the anti foot-shooting option in FreeBSD.

Regards
ChenYu


>> Acked-by: Chen-Yu Tsai <wens@csie.org>
>>
>> Speaking of power_supply_get_battery_info, is it merged or ready to be merged?
>>
>
> v7 under way IIRC.
>
> Thanks,
> Quentin
>
> --
> Quentin Schulz, Free Electrons
> Embedded Linux and Kernel engineering
> http://free-electrons.com

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 01/18] dt-bindings: power: battery: add constant-charge-current property
  2017-02-14  9:40   ` Quentin Schulz
  (?)
@ 2017-02-22 21:25     ` Rob Herring
  -1 siblings, 0 replies; 126+ messages in thread
From: Rob Herring @ 2017-02-22 21:25 UTC (permalink / raw)
  To: Quentin Schulz
  Cc: knaack.h, lars, pmeerw, mark.rutland, wens, sre, linux,
	maxime.ripard, lee.jones, linux-iio, devicetree, linux-kernel,
	linux-pm, linux-arm-kernel, thomas.petazzoni, icenowy, bonbons

On Tue, Feb 14, 2017 at 10:40:55AM +0100, Quentin Schulz wrote:
> This adds the constant-charge-current property to the list of optional
> properties of the battery.
> 
> The constant charge current is critical for batteries as they can't
> handle all charge currents.
> 
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> ---
> 
> added in v3
> 
>  Documentation/devicetree/bindings/power/supply/battery.txt | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/Documentation/devicetree/bindings/power/supply/battery.txt b/Documentation/devicetree/bindings/power/supply/battery.txt
> index d78a4aa..3e830fe 100644
> --- a/Documentation/devicetree/bindings/power/supply/battery.txt
> +++ b/Documentation/devicetree/bindings/power/supply/battery.txt
> @@ -7,6 +7,7 @@ Optional Properties:
>   - voltage-min-design-microvolt: drained battery voltage
>   - energy-full-design-microwatt-hours: battery design energy
>   - charge-full-design-microamp-hours: battery design capacity
> + - constant-charge-current-microamp: battery constant charge current

Kind of long, though so are the others. Maybe just 
"constant-charge-microamp" as uA implies current.

Otherwise,

Acked-by: Rob Herring <robh@kernel.org>

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 01/18] dt-bindings: power: battery: add constant-charge-current property
@ 2017-02-22 21:25     ` Rob Herring
  0 siblings, 0 replies; 126+ messages in thread
From: Rob Herring @ 2017-02-22 21:25 UTC (permalink / raw)
  To: Quentin Schulz
  Cc: mark.rutland, devicetree, lars, linux-pm, linux-iio,
	linux-kernel, sre, linux, wens, icenowy, pmeerw, knaack.h,
	maxime.ripard, bonbons, lee.jones, thomas.petazzoni,
	linux-arm-kernel

On Tue, Feb 14, 2017 at 10:40:55AM +0100, Quentin Schulz wrote:
> This adds the constant-charge-current property to the list of optional
> properties of the battery.
> 
> The constant charge current is critical for batteries as they can't
> handle all charge currents.
> 
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> ---
> 
> added in v3
> 
>  Documentation/devicetree/bindings/power/supply/battery.txt | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/Documentation/devicetree/bindings/power/supply/battery.txt b/Documentation/devicetree/bindings/power/supply/battery.txt
> index d78a4aa..3e830fe 100644
> --- a/Documentation/devicetree/bindings/power/supply/battery.txt
> +++ b/Documentation/devicetree/bindings/power/supply/battery.txt
> @@ -7,6 +7,7 @@ Optional Properties:
>   - voltage-min-design-microvolt: drained battery voltage
>   - energy-full-design-microwatt-hours: battery design energy
>   - charge-full-design-microamp-hours: battery design capacity
> + - constant-charge-current-microamp: battery constant charge current

Kind of long, though so are the others. Maybe just 
"constant-charge-microamp" as uA implies current.

Otherwise,

Acked-by: Rob Herring <robh@kernel.org>

^ permalink raw reply	[flat|nested] 126+ messages in thread

* [PATCH v3 01/18] dt-bindings: power: battery: add constant-charge-current property
@ 2017-02-22 21:25     ` Rob Herring
  0 siblings, 0 replies; 126+ messages in thread
From: Rob Herring @ 2017-02-22 21:25 UTC (permalink / raw)
  To: linux-arm-kernel

On Tue, Feb 14, 2017 at 10:40:55AM +0100, Quentin Schulz wrote:
> This adds the constant-charge-current property to the list of optional
> properties of the battery.
> 
> The constant charge current is critical for batteries as they can't
> handle all charge currents.
> 
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> ---
> 
> added in v3
> 
>  Documentation/devicetree/bindings/power/supply/battery.txt | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/Documentation/devicetree/bindings/power/supply/battery.txt b/Documentation/devicetree/bindings/power/supply/battery.txt
> index d78a4aa..3e830fe 100644
> --- a/Documentation/devicetree/bindings/power/supply/battery.txt
> +++ b/Documentation/devicetree/bindings/power/supply/battery.txt
> @@ -7,6 +7,7 @@ Optional Properties:
>   - voltage-min-design-microvolt: drained battery voltage
>   - energy-full-design-microwatt-hours: battery design energy
>   - charge-full-design-microamp-hours: battery design capacity
> + - constant-charge-current-microamp: battery constant charge current

Kind of long, though so are the others. Maybe just 
"constant-charge-microamp" as uA implies current.

Otherwise,

Acked-by: Rob Herring <robh@kernel.org>

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 04/18] iio: adc: add support for X-Powers AXP20X and AXP22X PMICs ADCs
  2017-02-21 18:03       ` Quentin Schulz
  (?)
@ 2017-02-25 16:53         ` Jonathan Cameron
  -1 siblings, 0 replies; 126+ messages in thread
From: Jonathan Cameron @ 2017-02-25 16:53 UTC (permalink / raw)
  To: Quentin Schulz, knaack.h, lars, pmeerw, robh+dt, mark.rutland,
	wens, sre, linux, maxime.ripard, lee.jones
  Cc: linux-iio, devicetree, linux-kernel, linux-pm, linux-arm-kernel,
	thomas.petazzoni, icenowy, bonbons

On 21/02/17 18:03, Quentin Schulz wrote:
> Hi Jonathan,
> 
> On 19/02/2017 13:40, Jonathan Cameron wrote:
>> On 14/02/17 09:40, Quentin Schulz wrote:
>>> The X-Powers AXP20X and AXP22X PMICs have multiple ADCs. They expose the
>>> battery voltage, battery charge and discharge currents, AC-in and VBUS
>>> voltages and currents, 2 GPIOs muxable in ADC mode and PMIC temperature.
>>>
>>> This adds support for most of AXP20X and AXP22X ADCs.
>>>
>>> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
>>> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
>> Hi Quentin,
>>
>> A few bits and bobs inline.  The bigest one is that I don't think
>> you need to carry your struct axp_data pointer around in the iio_priv
>> structure as it only seems to be used in probe.
>>
>> Anyhow, tidy these little bits up (quite a few are optional in the
>> sense that they are a matter or personal taste and I don't feel
>> strongly about them) and you can add
>>
>> Reviewed-by: Jonathan Cameron <jic23@kernel.org>
>>
>> A nice driver.
>>
>> Jonathan
> [...]
>>> +static int axp20x_probe(struct platform_device *pdev)
>>> +{
>>> +	struct axp20x_adc_iio *info;
>>> +	struct iio_dev *indio_dev;
>>> +	struct axp20x_dev *axp20x_dev;
>>> +	int ret;
>>> +
>>> +	axp20x_dev = dev_get_drvdata(pdev->dev.parent);
>>> +
>>> +	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*info));
>>> +	if (!indio_dev)
>>> +		return -ENOMEM;
>>> +
>>> +	info = iio_priv(indio_dev);
>>> +	platform_set_drvdata(pdev, indio_dev);
>>> +
>>> +	info->regmap = axp20x_dev->regmap;
>>> +	indio_dev->dev.parent = &pdev->dev;
>>> +	indio_dev->dev.of_node = pdev->dev.of_node;
>>> +	indio_dev->modes = INDIO_DIRECT_MODE;
>>> +
>>> +	info->data = (struct axp_data *)platform_get_device_id(pdev)->driver_data;
>> I think this is only actually used in probe, so could be handled more
>> neatly with a local variable rather than sticking it in info.
>>
> [...]
>>> +static int axp20x_remove(struct platform_device *pdev)
>>> +{
>>> +	struct axp20x_adc_iio *info;
>>> +	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
>>> +
>>> +	info = iio_priv(indio_dev);
>> Reorder the two lines above this one and then you could just do this
>> inline. (really trivial so feel free to not bother ;)
>>> +
>>> +	iio_device_unregister(indio_dev);
>>> +	iio_map_array_unregister(indio_dev);
>>> +
>>> +	regmap_write(info->regmap, AXP20X_ADC_EN1, 0);
>>> +
>>> +	if (info->data->adc_en2)
> 
> data is used here to know if I should disable misc ADCs (if supported by
> the PMIC). So kinda need it only for that purpose, maybe I can use a
> slightly simpler structure to only store regmap and this adc_en2 boolean.
> 
> Ack for everything else.
cool. Not sure how my searching missed that!

Up to you on whether you simplify that structure or not, I'm happy either way.
> 
> Thanks,
> Quentin
> 

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 04/18] iio: adc: add support for X-Powers AXP20X and AXP22X PMICs ADCs
@ 2017-02-25 16:53         ` Jonathan Cameron
  0 siblings, 0 replies; 126+ messages in thread
From: Jonathan Cameron @ 2017-02-25 16:53 UTC (permalink / raw)
  To: Quentin Schulz, knaack.h, lars, pmeerw, robh+dt, mark.rutland,
	wens, sre, linux, maxime.ripard, lee.jones
  Cc: thomas.petazzoni, devicetree, linux-pm, linux-iio, linux-kernel,
	bonbons, icenowy, linux-arm-kernel

On 21/02/17 18:03, Quentin Schulz wrote:
> Hi Jonathan,
> 
> On 19/02/2017 13:40, Jonathan Cameron wrote:
>> On 14/02/17 09:40, Quentin Schulz wrote:
>>> The X-Powers AXP20X and AXP22X PMICs have multiple ADCs. They expose the
>>> battery voltage, battery charge and discharge currents, AC-in and VBUS
>>> voltages and currents, 2 GPIOs muxable in ADC mode and PMIC temperature.
>>>
>>> This adds support for most of AXP20X and AXP22X ADCs.
>>>
>>> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
>>> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
>> Hi Quentin,
>>
>> A few bits and bobs inline.  The bigest one is that I don't think
>> you need to carry your struct axp_data pointer around in the iio_priv
>> structure as it only seems to be used in probe.
>>
>> Anyhow, tidy these little bits up (quite a few are optional in the
>> sense that they are a matter or personal taste and I don't feel
>> strongly about them) and you can add
>>
>> Reviewed-by: Jonathan Cameron <jic23@kernel.org>
>>
>> A nice driver.
>>
>> Jonathan
> [...]
>>> +static int axp20x_probe(struct platform_device *pdev)
>>> +{
>>> +	struct axp20x_adc_iio *info;
>>> +	struct iio_dev *indio_dev;
>>> +	struct axp20x_dev *axp20x_dev;
>>> +	int ret;
>>> +
>>> +	axp20x_dev = dev_get_drvdata(pdev->dev.parent);
>>> +
>>> +	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*info));
>>> +	if (!indio_dev)
>>> +		return -ENOMEM;
>>> +
>>> +	info = iio_priv(indio_dev);
>>> +	platform_set_drvdata(pdev, indio_dev);
>>> +
>>> +	info->regmap = axp20x_dev->regmap;
>>> +	indio_dev->dev.parent = &pdev->dev;
>>> +	indio_dev->dev.of_node = pdev->dev.of_node;
>>> +	indio_dev->modes = INDIO_DIRECT_MODE;
>>> +
>>> +	info->data = (struct axp_data *)platform_get_device_id(pdev)->driver_data;
>> I think this is only actually used in probe, so could be handled more
>> neatly with a local variable rather than sticking it in info.
>>
> [...]
>>> +static int axp20x_remove(struct platform_device *pdev)
>>> +{
>>> +	struct axp20x_adc_iio *info;
>>> +	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
>>> +
>>> +	info = iio_priv(indio_dev);
>> Reorder the two lines above this one and then you could just do this
>> inline. (really trivial so feel free to not bother ;)
>>> +
>>> +	iio_device_unregister(indio_dev);
>>> +	iio_map_array_unregister(indio_dev);
>>> +
>>> +	regmap_write(info->regmap, AXP20X_ADC_EN1, 0);
>>> +
>>> +	if (info->data->adc_en2)
> 
> data is used here to know if I should disable misc ADCs (if supported by
> the PMIC). So kinda need it only for that purpose, maybe I can use a
> slightly simpler structure to only store regmap and this adc_en2 boolean.
> 
> Ack for everything else.
cool. Not sure how my searching missed that!

Up to you on whether you simplify that structure or not, I'm happy either way.
> 
> Thanks,
> Quentin
> 

^ permalink raw reply	[flat|nested] 126+ messages in thread

* [PATCH v3 04/18] iio: adc: add support for X-Powers AXP20X and AXP22X PMICs ADCs
@ 2017-02-25 16:53         ` Jonathan Cameron
  0 siblings, 0 replies; 126+ messages in thread
From: Jonathan Cameron @ 2017-02-25 16:53 UTC (permalink / raw)
  To: linux-arm-kernel

On 21/02/17 18:03, Quentin Schulz wrote:
> Hi Jonathan,
> 
> On 19/02/2017 13:40, Jonathan Cameron wrote:
>> On 14/02/17 09:40, Quentin Schulz wrote:
>>> The X-Powers AXP20X and AXP22X PMICs have multiple ADCs. They expose the
>>> battery voltage, battery charge and discharge currents, AC-in and VBUS
>>> voltages and currents, 2 GPIOs muxable in ADC mode and PMIC temperature.
>>>
>>> This adds support for most of AXP20X and AXP22X ADCs.
>>>
>>> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
>>> Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
>> Hi Quentin,
>>
>> A few bits and bobs inline.  The bigest one is that I don't think
>> you need to carry your struct axp_data pointer around in the iio_priv
>> structure as it only seems to be used in probe.
>>
>> Anyhow, tidy these little bits up (quite a few are optional in the
>> sense that they are a matter or personal taste and I don't feel
>> strongly about them) and you can add
>>
>> Reviewed-by: Jonathan Cameron <jic23@kernel.org>
>>
>> A nice driver.
>>
>> Jonathan
> [...]
>>> +static int axp20x_probe(struct platform_device *pdev)
>>> +{
>>> +	struct axp20x_adc_iio *info;
>>> +	struct iio_dev *indio_dev;
>>> +	struct axp20x_dev *axp20x_dev;
>>> +	int ret;
>>> +
>>> +	axp20x_dev = dev_get_drvdata(pdev->dev.parent);
>>> +
>>> +	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*info));
>>> +	if (!indio_dev)
>>> +		return -ENOMEM;
>>> +
>>> +	info = iio_priv(indio_dev);
>>> +	platform_set_drvdata(pdev, indio_dev);
>>> +
>>> +	info->regmap = axp20x_dev->regmap;
>>> +	indio_dev->dev.parent = &pdev->dev;
>>> +	indio_dev->dev.of_node = pdev->dev.of_node;
>>> +	indio_dev->modes = INDIO_DIRECT_MODE;
>>> +
>>> +	info->data = (struct axp_data *)platform_get_device_id(pdev)->driver_data;
>> I think this is only actually used in probe, so could be handled more
>> neatly with a local variable rather than sticking it in info.
>>
> [...]
>>> +static int axp20x_remove(struct platform_device *pdev)
>>> +{
>>> +	struct axp20x_adc_iio *info;
>>> +	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
>>> +
>>> +	info = iio_priv(indio_dev);
>> Reorder the two lines above this one and then you could just do this
>> inline. (really trivial so feel free to not bother ;)
>>> +
>>> +	iio_device_unregister(indio_dev);
>>> +	iio_map_array_unregister(indio_dev);
>>> +
>>> +	regmap_write(info->regmap, AXP20X_ADC_EN1, 0);
>>> +
>>> +	if (info->data->adc_en2)
> 
> data is used here to know if I should disable misc ADCs (if supported by
> the PMIC). So kinda need it only for that purpose, maybe I can use a
> slightly simpler structure to only store regmap and this adc_en2 boolean.
> 
> Ack for everything else.
cool. Not sure how my searching missed that!

Up to you on whether you simplify that structure or not, I'm happy either way.
> 
> Thanks,
> Quentin
> 

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 01/18] dt-bindings: power: battery: add constant-charge-current property
  2017-02-21  4:55         ` Chen-Yu Tsai
  (?)
@ 2017-03-14 13:44           ` Quentin Schulz
  -1 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-03-14 13:44 UTC (permalink / raw)
  To: Chen-Yu Tsai
  Cc: Mark Rutland, Thomas Petazzoni, Lars-Peter Clausen, devicetree,
	Liam Breck, linux-kernel, open list:THERMAL, Sebastian Reichel,
	Russell King, linux-iio, Rob Herring, Icenowy Zheng,
	Peter Meerwald-Stadler, knaack.h, Maxime Ripard,
	Bruno Prémont, Lee Jones, linux-arm-kernel

Hi Chen-Yu,

Sorry for the late reply.

On 21/02/2017 05:55, Chen-Yu Tsai wrote:
> On Wed, Feb 15, 2017 at 4:53 PM, Quentin Schulz
> <quentin.schulz@free-electrons.com> wrote:
>> Hi,
>>
>> On 15/02/2017 01:46, Liam Breck wrote:
>>>
>>> On Tue, 14 Feb 2017 10:40:55 +0100 Quentin Schulz wrote:
>>>> This adds the constant-charge-current property to the list of optional
>>>> properties of the battery.
>>>>
>>>> The constant charge current is critical for batteries as they can't
>>>> handle all charge currents.
>>>>
>>>> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
>>>> ---
>>>>
>>>> added in v3
>>>>
>>>>  Documentation/devicetree/bindings/power/supply/battery.txt | 2 ++
>>>
>>> Is constant-charge-current dependent on the battery (e.g. capacity, nominal voltage, etc) or the
>>> system (charger chip, input current/voltage, etc)?
>>>
>>> It belongs in Doc.../power/supply/battery.txt if it's a characteristic of the battery.
>>>
>>> Note, this page asserts that constant-current charging applies to NiMH batteries:
>>> http://power-topics.blogspot.com/2016/05/constant-voltage-constant-current.html
>>>
>>> Related properties to be added to battery.txt near-future in a patchset for the BQ24190
>>> charger are as follows. These are not currently in enum power_supply_property, so the actual names
>>> are still to be decided.
>>>
>>> precharge-current-microamp:
>>>    maximum charge current during precharge phase (typically 20% of battery capacity)
>>>
>>> termination-current-microamp (or endcharge-current):
>>>    a charge cycle terminates when the battery voltage is above recharge threshold,
>>>    and the current is below this setting (typically 10% of battery capacity)
>>>
>>
>> We have a client with a board whose battery accepts a maximum of 300mA
>> for charging. So depending on the battery, we cannot have any charging
>> current we want. The AXP PmMICs set constant charge current in a range
>> of 300mA-1800mA, so it is enforced by the charger but needs to be
>> adapted depending on the battery present in the system.
> 
> May I ask the capacity of the battery in question?
> 

It is a 400mAh battery.

Quentin

> Typically one charges LiPo batteries at no more than 1C. So if it's
> a 1800 mAh battery, the charge current should be no more than 1800 mA.
> 
> ChenYu
> 
>> The AXP PMICs charge battery with constant current (Ichrg) between the
>> trickle voltage (Vtrkl which is ~3.0V) and the targeted voltage (Vtrgt;
>> which seems to be the voltage telling the battery is fully charged).
>>
>> So if I understand correctly, "my" constant-charge-current would be
>> located in the charging cycle between your precharge-current-microamp
>> and the termination-current-microamp as it is the current for the
>> charging process as a whole.
>>
>> See here[1] for the explanation in the datasheet (page 20).
>>
>> That would definitely match what is explained in your link for constant
>> current.
>>
>> [1] http://dl.linux-sunxi.org/AXP/AXP209_Datasheet_v1.0en.pdf
>>
>> Let me know if something seems odd,
>> Thanks,
>> Quentin
>>
>> --
>> Quentin Schulz, Free Electrons
>> Embedded Linux and Kernel engineering
>> http://free-electrons.com
> 
> _______________________________________________
> linux-arm-kernel mailing list
> linux-arm-kernel@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
> 

-- 
Quentin Schulz, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

^ permalink raw reply	[flat|nested] 126+ messages in thread

* Re: [PATCH v3 01/18] dt-bindings: power: battery: add constant-charge-current property
@ 2017-03-14 13:44           ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-03-14 13:44 UTC (permalink / raw)
  To: Chen-Yu Tsai
  Cc: Mark Rutland, Thomas Petazzoni, Lars-Peter Clausen, devicetree,
	linux-iio, open list:THERMAL, linux-kernel, Rob Herring,
	Sebastian Reichel, Bruno Prémont, Liam Breck, Icenowy Zheng,
	Peter Meerwald-Stadler, knaack.h, Russell King, Maxime Ripard,
	Lee Jones, linux-arm-kernel

Hi Chen-Yu,

Sorry for the late reply.

On 21/02/2017 05:55, Chen-Yu Tsai wrote:
> On Wed, Feb 15, 2017 at 4:53 PM, Quentin Schulz
> <quentin.schulz@free-electrons.com> wrote:
>> Hi,
>>
>> On 15/02/2017 01:46, Liam Breck wrote:
>>>
>>> On Tue, 14 Feb 2017 10:40:55 +0100 Quentin Schulz wrote:
>>>> This adds the constant-charge-current property to the list of optional
>>>> properties of the battery.
>>>>
>>>> The constant charge current is critical for batteries as they can't
>>>> handle all charge currents.
>>>>
>>>> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
>>>> ---
>>>>
>>>> added in v3
>>>>
>>>>  Documentation/devicetree/bindings/power/supply/battery.txt | 2 ++
>>>
>>> Is constant-charge-current dependent on the battery (e.g. capacity, nominal voltage, etc) or the
>>> system (charger chip, input current/voltage, etc)?
>>>
>>> It belongs in Doc.../power/supply/battery.txt if it's a characteristic of the battery.
>>>
>>> Note, this page asserts that constant-current charging applies to NiMH batteries:
>>> http://power-topics.blogspot.com/2016/05/constant-voltage-constant-current.html
>>>
>>> Related properties to be added to battery.txt near-future in a patchset for the BQ24190
>>> charger are as follows. These are not currently in enum power_supply_property, so the actual names
>>> are still to be decided.
>>>
>>> precharge-current-microamp:
>>>    maximum charge current during precharge phase (typically 20% of battery capacity)
>>>
>>> termination-current-microamp (or endcharge-current):
>>>    a charge cycle terminates when the battery voltage is above recharge threshold,
>>>    and the current is below this setting (typically 10% of battery capacity)
>>>
>>
>> We have a client with a board whose battery accepts a maximum of 300mA
>> for charging. So depending on the battery, we cannot have any charging
>> current we want. The AXP PmMICs set constant charge current in a range
>> of 300mA-1800mA, so it is enforced by the charger but needs to be
>> adapted depending on the battery present in the system.
> 
> May I ask the capacity of the battery in question?
> 

It is a 400mAh battery.

Quentin

> Typically one charges LiPo batteries at no more than 1C. So if it's
> a 1800 mAh battery, the charge current should be no more than 1800 mA.
> 
> ChenYu
> 
>> The AXP PMICs charge battery with constant current (Ichrg) between the
>> trickle voltage (Vtrkl which is ~3.0V) and the targeted voltage (Vtrgt;
>> which seems to be the voltage telling the battery is fully charged).
>>
>> So if I understand correctly, "my" constant-charge-current would be
>> located in the charging cycle between your precharge-current-microamp
>> and the termination-current-microamp as it is the current for the
>> charging process as a whole.
>>
>> See here[1] for the explanation in the datasheet (page 20).
>>
>> That would definitely match what is explained in your link for constant
>> current.
>>
>> [1] http://dl.linux-sunxi.org/AXP/AXP209_Datasheet_v1.0en.pdf
>>
>> Let me know if something seems odd,
>> Thanks,
>> Quentin
>>
>> --
>> Quentin Schulz, Free Electrons
>> Embedded Linux and Kernel engineering
>> http://free-electrons.com
> 
> _______________________________________________
> linux-arm-kernel mailing list
> linux-arm-kernel@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
> 

-- 
Quentin Schulz, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

^ permalink raw reply	[flat|nested] 126+ messages in thread

* [PATCH v3 01/18] dt-bindings: power: battery: add constant-charge-current property
@ 2017-03-14 13:44           ` Quentin Schulz
  0 siblings, 0 replies; 126+ messages in thread
From: Quentin Schulz @ 2017-03-14 13:44 UTC (permalink / raw)
  To: linux-arm-kernel

Hi Chen-Yu,

Sorry for the late reply.

On 21/02/2017 05:55, Chen-Yu Tsai wrote:
> On Wed, Feb 15, 2017 at 4:53 PM, Quentin Schulz
> <quentin.schulz@free-electrons.com> wrote:
>> Hi,
>>
>> On 15/02/2017 01:46, Liam Breck wrote:
>>>
>>> On Tue, 14 Feb 2017 10:40:55 +0100 Quentin Schulz wrote:
>>>> This adds the constant-charge-current property to the list of optional
>>>> properties of the battery.
>>>>
>>>> The constant charge current is critical for batteries as they can't
>>>> handle all charge currents.
>>>>
>>>> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
>>>> ---
>>>>
>>>> added in v3
>>>>
>>>>  Documentation/devicetree/bindings/power/supply/battery.txt | 2 ++
>>>
>>> Is constant-charge-current dependent on the battery (e.g. capacity, nominal voltage, etc) or the
>>> system (charger chip, input current/voltage, etc)?
>>>
>>> It belongs in Doc.../power/supply/battery.txt if it's a characteristic of the battery.
>>>
>>> Note, this page asserts that constant-current charging applies to NiMH batteries:
>>> http://power-topics.blogspot.com/2016/05/constant-voltage-constant-current.html
>>>
>>> Related properties to be added to battery.txt near-future in a patchset for the BQ24190
>>> charger are as follows. These are not currently in enum power_supply_property, so the actual names
>>> are still to be decided.
>>>
>>> precharge-current-microamp:
>>>    maximum charge current during precharge phase (typically 20% of battery capacity)
>>>
>>> termination-current-microamp (or endcharge-current):
>>>    a charge cycle terminates when the battery voltage is above recharge threshold,
>>>    and the current is below this setting (typically 10% of battery capacity)
>>>
>>
>> We have a client with a board whose battery accepts a maximum of 300mA
>> for charging. So depending on the battery, we cannot have any charging
>> current we want. The AXP PmMICs set constant charge current in a range
>> of 300mA-1800mA, so it is enforced by the charger but needs to be
>> adapted depending on the battery present in the system.
> 
> May I ask the capacity of the battery in question?
> 

It is a 400mAh battery.

Quentin

> Typically one charges LiPo batteries at no more than 1C. So if it's
> a 1800 mAh battery, the charge current should be no more than 1800 mA.
> 
> ChenYu
> 
>> The AXP PMICs charge battery with constant current (Ichrg) between the
>> trickle voltage (Vtrkl which is ~3.0V) and the targeted voltage (Vtrgt;
>> which seems to be the voltage telling the battery is fully charged).
>>
>> So if I understand correctly, "my" constant-charge-current would be
>> located in the charging cycle between your precharge-current-microamp
>> and the termination-current-microamp as it is the current for the
>> charging process as a whole.
>>
>> See here[1] for the explanation in the datasheet (page 20).
>>
>> That would definitely match what is explained in your link for constant
>> current.
>>
>> [1] http://dl.linux-sunxi.org/AXP/AXP209_Datasheet_v1.0en.pdf
>>
>> Let me know if something seems odd,
>> Thanks,
>> Quentin
>>
>> --
>> Quentin Schulz, Free Electrons
>> Embedded Linux and Kernel engineering
>> http://free-electrons.com
> 
> _______________________________________________
> linux-arm-kernel mailing list
> linux-arm-kernel at lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
> 

-- 
Quentin Schulz, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

^ permalink raw reply	[flat|nested] 126+ messages in thread

end of thread, other threads:[~2017-03-14 13:45 UTC | newest]

Thread overview: 126+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-02-14  9:40 [PATCH v3 00/18] add support for AXP20X and AXP22X power supply drivers Quentin Schulz
2017-02-14  9:40 ` Quentin Schulz
2017-02-14  9:40 ` Quentin Schulz
2017-02-14  9:40 ` [PATCH v3 01/18] dt-bindings: power: battery: add constant-charge-current property Quentin Schulz
2017-02-14  9:40   ` Quentin Schulz
2017-02-14  9:40   ` Quentin Schulz
2017-02-15  0:46   ` Liam Breck
2017-02-15  0:46     ` Liam Breck
2017-02-15  0:46     ` Liam Breck
2017-02-15  8:53     ` Quentin Schulz
2017-02-15  8:53       ` Quentin Schulz
2017-02-15 20:18       ` Liam Breck
2017-02-15 20:18         ` Liam Breck
2017-02-15 20:18         ` Liam Breck
2017-02-21  4:55       ` Chen-Yu Tsai
2017-02-21  4:55         ` Chen-Yu Tsai
2017-02-21  4:55         ` Chen-Yu Tsai
2017-03-14 13:44         ` Quentin Schulz
2017-03-14 13:44           ` Quentin Schulz
2017-03-14 13:44           ` Quentin Schulz
2017-02-22 21:25   ` Rob Herring
2017-02-22 21:25     ` Rob Herring
2017-02-22 21:25     ` Rob Herring
2017-02-14  9:40 ` [PATCH v3 02/18] power: supply: power_supply_core: add constant-current-charge optional property Quentin Schulz
2017-02-14  9:40   ` Quentin Schulz
2017-02-14  9:40   ` Quentin Schulz
2017-02-14  9:40 ` [PATCH v3 03/18] mfd: axp20x: correct name of temperature data ADC registers Quentin Schulz
2017-02-14  9:40   ` Quentin Schulz
2017-02-14  9:40   ` Quentin Schulz
2017-02-14  9:40 ` [PATCH v3 04/18] iio: adc: add support for X-Powers AXP20X and AXP22X PMICs ADCs Quentin Schulz
2017-02-14  9:40   ` Quentin Schulz
2017-02-14  9:40   ` Quentin Schulz
2017-02-19 12:40   ` Jonathan Cameron
2017-02-19 12:40     ` Jonathan Cameron
2017-02-19 12:40     ` Jonathan Cameron
2017-02-21 18:03     ` Quentin Schulz
2017-02-21 18:03       ` Quentin Schulz
2017-02-21 18:03       ` Quentin Schulz
2017-02-25 16:53       ` Jonathan Cameron
2017-02-25 16:53         ` Jonathan Cameron
2017-02-25 16:53         ` Jonathan Cameron
2017-02-21  4:32   ` Chen-Yu Tsai
2017-02-21  4:32     ` Chen-Yu Tsai
2017-02-21  4:32     ` Chen-Yu Tsai
2017-02-14  9:40 ` [PATCH v3 05/18] mfd: axp20x: add ADC cells for AXP20X and AXP22X PMICs Quentin Schulz
2017-02-14  9:40   ` Quentin Schulz
2017-02-14  9:40   ` Quentin Schulz
2017-02-15  9:37   ` Lee Jones
2017-02-15  9:37     ` Lee Jones
2017-02-15  9:37     ` Lee Jones
2017-02-21  4:51     ` Chen-Yu Tsai
2017-02-21  4:51       ` Chen-Yu Tsai
2017-02-14  9:41 ` [PATCH v3 06/18] mfd: axp20x: add AC power supply cells for " Quentin Schulz
2017-02-14  9:41   ` Quentin Schulz
2017-02-14  9:41   ` Quentin Schulz
2017-02-14  9:41 ` [PATCH v3 07/18] ARM: dtsi: axp209: add AC power supply subnode Quentin Schulz
2017-02-14  9:41   ` Quentin Schulz
2017-02-14  9:41   ` Quentin Schulz
2017-02-14  9:41 ` [PATCH v3 08/18] ARM: dtsi: axp22x: " Quentin Schulz
2017-02-14  9:41   ` Quentin Schulz
2017-02-14  9:41   ` Quentin Schulz
2017-02-14  9:41 ` [PATCH v3 09/18] ARM: dts: sun8i: sina33: enable ACIN " Quentin Schulz
2017-02-14  9:41   ` Quentin Schulz
2017-02-14  9:41   ` Quentin Schulz
2017-02-14  9:41 ` [PATCH v3 10/18] ARM: sun5i: chip: " Quentin Schulz
2017-02-14  9:41   ` Quentin Schulz
2017-02-14  9:41   ` Quentin Schulz
2017-02-14  9:41 ` [PATCH v3 11/18] dt-bindings: power: supply: add AXP20X/AXP22X battery DT binding Quentin Schulz
2017-02-14  9:41   ` Quentin Schulz
2017-02-14  9:41   ` Quentin Schulz
2017-02-21  4:45   ` Chen-Yu Tsai
2017-02-21  4:45     ` Chen-Yu Tsai
2017-02-21  4:45     ` Chen-Yu Tsai
2017-02-21 18:05     ` Quentin Schulz
2017-02-21 18:05       ` Quentin Schulz
2017-02-21 18:05       ` Quentin Schulz
2017-02-14  9:41 ` [PATCH v3 12/18] mfd: axp20x: add CHRG_CTRL1/2/3 to writeable regs for AXP20X/AXP22X Quentin Schulz
2017-02-14  9:41   ` Quentin Schulz
2017-02-14  9:41   ` Quentin Schulz
2017-02-14  9:41 ` [PATCH v3 13/18] power: supply: add battery driver for AXP20X and AXP22X PMICs Quentin Schulz
2017-02-14  9:41   ` Quentin Schulz
2017-02-14  9:41   ` Quentin Schulz
2017-02-21  4:44   ` Chen-Yu Tsai
2017-02-21  4:44     ` Chen-Yu Tsai
2017-02-21  4:44     ` Chen-Yu Tsai
2017-02-21 18:19     ` Quentin Schulz
2017-02-21 18:19       ` Quentin Schulz
2017-02-21 18:19       ` Quentin Schulz
2017-02-22  6:22       ` Chen-Yu Tsai
2017-02-22  6:22         ` Chen-Yu Tsai
2017-02-22  6:22         ` Chen-Yu Tsai
2017-02-14  9:41 ` [PATCH v3 14/18] mfd: axp20x: add MFD cells for AXP20X and AXP22X battery driver Quentin Schulz
2017-02-14  9:41   ` Quentin Schulz
2017-02-14  9:41   ` Quentin Schulz
2017-02-21  4:46   ` Chen-Yu Tsai
2017-02-21  4:46     ` Chen-Yu Tsai
2017-02-21  4:46     ` Chen-Yu Tsai
2017-02-14  9:41 ` [PATCH v3 15/18] ARM: dtsi: axp209: add battery power supply subnode Quentin Schulz
2017-02-14  9:41   ` Quentin Schulz
2017-02-14  9:41   ` Quentin Schulz
2017-02-21  4:47   ` Chen-Yu Tsai
2017-02-21  4:47     ` Chen-Yu Tsai
2017-02-21  4:47     ` Chen-Yu Tsai
2017-02-14  9:41 ` [PATCH v3 16/18] ARM: dtsi: axp22x: " Quentin Schulz
2017-02-14  9:41   ` Quentin Schulz
2017-02-14  9:41   ` Quentin Schulz
2017-02-21  4:47   ` Chen-Yu Tsai
2017-02-21  4:47     ` Chen-Yu Tsai
2017-02-21  4:47     ` Chen-Yu Tsai
2017-02-14  9:41 ` [PATCH v3 17/18] ARM: dts: sun8i: sina33: enable " Quentin Schulz
2017-02-14  9:41   ` Quentin Schulz
2017-02-14  9:41   ` Quentin Schulz
2017-02-21  4:50   ` Chen-Yu Tsai
2017-02-21  4:50     ` Chen-Yu Tsai
2017-02-21  4:50     ` Chen-Yu Tsai
2017-02-21 18:20     ` Quentin Schulz
2017-02-21 18:20       ` Quentin Schulz
2017-02-21 18:20       ` Quentin Schulz
2017-02-14  9:41 ` [PATCH v3 18/18] ARM: sun5i: chip: " Quentin Schulz
2017-02-14  9:41   ` Quentin Schulz
2017-02-14  9:41   ` Quentin Schulz
2017-02-21  4:50   ` Chen-Yu Tsai
2017-02-21  4:50     ` Chen-Yu Tsai
2017-02-15  9:36 ` [PATCH v3 00/18] add support for AXP20X and AXP22X power supply drivers Lee Jones
2017-02-15  9:36   ` Lee Jones
2017-02-15  9:36   ` Lee Jones

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.