diff mbox

[v4,3/6] Documentation: DT: Document twl4030-madc-battery bindings

Message ID 1426022847-30912-4-git-send-email-marek@goldelico.com
State New, archived
Headers show

Commit Message

Marek Belisko March 10, 2015, 9:27 p.m. UTC
Signed-off-by: Marek Belisko <marek@goldelico.com>
---
 .../bindings/power_supply/twl4030_madc_battery.txt | 43 ++++++++++++++++++++++
 1 file changed, 43 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/power_supply/twl4030_madc_battery.txt

Comments

Tony Lindgren March 11, 2015, 3:24 p.m. UTC | #1
Hi,

* Marek Belisko <marek@goldelico.com> [150310 14:28]:
> Signed-off-by: Marek Belisko <marek@goldelico.com>
> ---
>  .../bindings/power_supply/twl4030_madc_battery.txt | 43 ++++++++++++++++++++++
>  1 file changed, 43 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/power_supply/twl4030_madc_battery.txt
> 
> diff --git a/Documentation/devicetree/bindings/power_supply/twl4030_madc_battery.txt b/Documentation/devicetree/bindings/power_supply/twl4030_madc_battery.txt
> new file mode 100644
> index 0000000..d3dd9d8
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/power_supply/twl4030_madc_battery.txt
> @@ -0,0 +1,43 @@
> +twl4030_madc_battery
> +
> +Required properties:
> + - compatible : "ti,twl4030-madc-battery"
> + - capacity-uah : battery capacity in uAh
> + - ti,volt-to-capacity-charging-map : list of voltage(mV):level(%) values
> +	for charging calibration (see example)
> + - ti,volt-to-capacity-discharging-map : list of voltage(mV):level(%) values
> +	for discharging calibration (see example)
> + - io-channels: Should contain IIO channel specifiers
> +	for each element in io-channel-names.
> +- io-channel-names: Should contain the following values:
> + * "temp" - The ADC channel for temperature reading
> + * "ichg" - The ADC channel for battery charging status
> + * "vbat" - The ADC channel to measure the battery voltage
> +
> +Example:
> +	madc-battery {
> +		compatible = "ti,twl4030-madc-battery";
> +		capacity-uah = <1200000>;
> +		ti,volt-to-capacity-charging-map = <4200 100>,
> +					        <4100 75>,
> +					        <4000 55>,
> +					        <3900 25>,
> +					        <3800 5>,
> +					        <3700 2>,
> +					        <3600 1>,
> +					        <3300 0>;
> +
> +		ti,volt-to-capacity-discharging-map = <4200 100>
> +						   <4100 95>,
> +						   <4000 70>,
> +						   <3800 50>,
> +						   <3700 10>,
> +						   <3600 5>,
> +						   <3300 0>;
> +		io-channels = <&twl_madc 1>,
> +	                      <&twl_madc 10>,
> +			      <&twl_madc 12>;
> +		io-channel-names = "temp",
> +		                   "ichg",
> +		                   "vbat";
> +	};

Rather than just making platform_data into device tree properties..

Can't you hide the these custom properties behind the compatible flag?

You can initialize that data in the driver based on the compatible
flag and the match data.

This makes sense if you can group things to similar configurations.

Regards,

Tony

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
H. Nikolaus Schaller March 11, 2015, 4:16 p.m. UTC | #2
Hi,

Am 11.03.2015 um 16:24 schrieb Tony Lindgren <tony@atomide.com>:

> Hi,
> 
> * Marek Belisko <marek@goldelico.com> [150310 14:28]:
>> Signed-off-by: Marek Belisko <marek@goldelico.com>
>> ---
>> .../bindings/power_supply/twl4030_madc_battery.txt | 43 ++++++++++++++++++++++
>> 1 file changed, 43 insertions(+)
>> create mode 100644 Documentation/devicetree/bindings/power_supply/twl4030_madc_battery.txt
>> 
>> diff --git a/Documentation/devicetree/bindings/power_supply/twl4030_madc_battery.txt b/Documentation/devicetree/bindings/power_supply/twl4030_madc_battery.txt
>> new file mode 100644
>> index 0000000..d3dd9d8
>> --- /dev/null
>> +++ b/Documentation/devicetree/bindings/power_supply/twl4030_madc_battery.txt
>> @@ -0,0 +1,43 @@
>> +twl4030_madc_battery
>> +
>> +Required properties:
>> + - compatible : "ti,twl4030-madc-battery"
>> + - capacity-uah : battery capacity in uAh
>> + - ti,volt-to-capacity-charging-map : list of voltage(mV):level(%) values
>> +	for charging calibration (see example)
>> + - ti,volt-to-capacity-discharging-map : list of voltage(mV):level(%) values
>> +	for discharging calibration (see example)
>> + - io-channels: Should contain IIO channel specifiers
>> +	for each element in io-channel-names.
>> +- io-channel-names: Should contain the following values:
>> + * "temp" - The ADC channel for temperature reading
>> + * "ichg" - The ADC channel for battery charging status
>> + * "vbat" - The ADC channel to measure the battery voltage
>> +
>> +Example:
>> +	madc-battery {
>> +		compatible = "ti,twl4030-madc-battery";
>> +		capacity-uah = <1200000>;
>> +		ti,volt-to-capacity-charging-map = <4200 100>,
>> +					        <4100 75>,
>> +					        <4000 55>,
>> +					        <3900 25>,
>> +					        <3800 5>,
>> +					        <3700 2>,
>> +					        <3600 1>,
>> +					        <3300 0>;
>> +
>> +		ti,volt-to-capacity-discharging-map = <4200 100>
>> +						   <4100 95>,
>> +						   <4000 70>,
>> +						   <3800 50>,
>> +						   <3700 10>,
>> +						   <3600 5>,
>> +						   <3300 0>;
>> +		io-channels = <&twl_madc 1>,
>> +	                      <&twl_madc 10>,
>> +			      <&twl_madc 12>;
>> +		io-channel-names = "temp",
>> +		                   "ichg",
>> +		                   "vbat";
>> +	};
> 
> Rather than just making platform_data into device tree properties..
> 
> Can't you hide the these custom properties behind the compatible flag?
> 
> You can initialize that data in the driver based on the compatible
> flag and the match data.
> 
> This makes sense if you can group things to similar configurations.

Maybe I have not completely understood your proposal.

Do you mean to go back to have big parameter tables for each device/battery
combination in the driver code and the compatible flag (e.g. compatible = “board17”)
chooses the right data set for the charging map and channels?

I thought this is what the DT was introduced for - to have the same driver 
code but adapt to different boards depending on hardware variations.

And batteries have very different characteristics and vary between devices…

The charging maps are depending on the battery type connected to the twl4030
and which madc channel is which value is also a little hardware dependent
(although the twl4030 doesn’t give much choice).

And moving this information into the driver for each board that uses it
would blow up the code.

BR,
Nikolaus

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Tony Lindgren March 11, 2015, 4:44 p.m. UTC | #3
* Dr. H. Nikolaus Schaller <hns@goldelico.com> [150311 09:17]:
> Hi,
> 
> Am 11.03.2015 um 16:24 schrieb Tony Lindgren <tony@atomide.com>:
> 
> > Hi,
> > 
> > * Marek Belisko <marek@goldelico.com> [150310 14:28]:
> >> Signed-off-by: Marek Belisko <marek@goldelico.com>
> >> ---
> >> .../bindings/power_supply/twl4030_madc_battery.txt | 43 ++++++++++++++++++++++
> >> 1 file changed, 43 insertions(+)
> >> create mode 100644 Documentation/devicetree/bindings/power_supply/twl4030_madc_battery.txt
> >> 
> >> diff --git a/Documentation/devicetree/bindings/power_supply/twl4030_madc_battery.txt b/Documentation/devicetree/bindings/power_supply/twl4030_madc_battery.txt
> >> new file mode 100644
> >> index 0000000..d3dd9d8
> >> --- /dev/null
> >> +++ b/Documentation/devicetree/bindings/power_supply/twl4030_madc_battery.txt
> >> @@ -0,0 +1,43 @@
> >> +twl4030_madc_battery
> >> +
> >> +Required properties:
> >> + - compatible : "ti,twl4030-madc-battery"
> >> + - capacity-uah : battery capacity in uAh
> >> + - ti,volt-to-capacity-charging-map : list of voltage(mV):level(%) values
> >> +	for charging calibration (see example)
> >> + - ti,volt-to-capacity-discharging-map : list of voltage(mV):level(%) values
> >> +	for discharging calibration (see example)
> >> + - io-channels: Should contain IIO channel specifiers
> >> +	for each element in io-channel-names.
> >> +- io-channel-names: Should contain the following values:
> >> + * "temp" - The ADC channel for temperature reading
> >> + * "ichg" - The ADC channel for battery charging status
> >> + * "vbat" - The ADC channel to measure the battery voltage
> >> +
> >> +Example:
> >> +	madc-battery {
> >> +		compatible = "ti,twl4030-madc-battery";
> >> +		capacity-uah = <1200000>;
> >> +		ti,volt-to-capacity-charging-map = <4200 100>,
> >> +					        <4100 75>,
> >> +					        <4000 55>,
> >> +					        <3900 25>,
> >> +					        <3800 5>,
> >> +					        <3700 2>,
> >> +					        <3600 1>,
> >> +					        <3300 0>;
> >> +
> >> +		ti,volt-to-capacity-discharging-map = <4200 100>
> >> +						   <4100 95>,
> >> +						   <4000 70>,
> >> +						   <3800 50>,
> >> +						   <3700 10>,
> >> +						   <3600 5>,
> >> +						   <3300 0>;
> >> +		io-channels = <&twl_madc 1>,
> >> +	                      <&twl_madc 10>,
> >> +			      <&twl_madc 12>;
> >> +		io-channel-names = "temp",
> >> +		                   "ichg",
> >> +		                   "vbat";
> >> +	};
> > 
> > Rather than just making platform_data into device tree properties..
> > 
> > Can't you hide the these custom properties behind the compatible flag?
> > 
> > You can initialize that data in the driver based on the compatible
> > flag and the match data.
> > 
> > This makes sense if you can group things to similar configurations.
> 
> Maybe I have not completely understood your proposal.
> 
> Do you mean to go back to have big parameter tables for each device/battery
> combination in the driver code and the compatible flag (e.g. compatible = “board17”)
> chooses the right data set for the charging map and channels?

If you can somehow group them, then yes. Not for every board if there
are many of them naturally.
 
> I thought this is what the DT was introduced for - to have the same driver 
> code but adapt to different boards depending on hardware variations.

Yeah but you also need to consider the issues related to introducing
new device tree properties. The device tree properties introduced
should be generic where possible.

> And batteries have very different characteristics and vary between devices…

Right. Maybe that has been already agreed on to use capacity-uah for
batteries in general? In that case I have not problem with that as
it's a generic property :)
 
> The charging maps are depending on the battery type connected to the twl4030
> and which madc channel is which value is also a little hardware dependent
> (although the twl4030 doesn’t give much choice).

Just to consider alternatives before introducing driver specific
property for the maps.. Maybe here you could have few different type
of maps and select something safe by default? Of course it could be this
is higly board specific, I think some devices may be able to run below
3.3V for example..
 
> And moving this information into the driver for each board that uses it
> would blow up the code.

Right, I'm not saying we should build databases into the kernel drivers.
Just trying to avoid introducing driver specific properties unless
really needed :)

Regards,

Tony
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
H. Nikolaus Schaller March 11, 2015, 5:13 p.m. UTC | #4
Am 11.03.2015 um 17:44 schrieb Tony Lindgren <tony@atomide.com>:

> * Dr. H. Nikolaus Schaller <hns@goldelico.com> [150311 09:17]:
>> Hi,
>> 
>> Am 11.03.2015 um 16:24 schrieb Tony Lindgren <tony@atomide.com>:
>> 
>>> Hi,
>>> 
>>> * Marek Belisko <marek@goldelico.com> [150310 14:28]:
>>>> Signed-off-by: Marek Belisko <marek@goldelico.com>
>>>> ---
>>>> .../bindings/power_supply/twl4030_madc_battery.txt | 43 ++++++++++++++++++++++
>>>> 1 file changed, 43 insertions(+)
>>>> create mode 100644 Documentation/devicetree/bindings/power_supply/twl4030_madc_battery.txt
>>>> 
>>>> diff --git a/Documentation/devicetree/bindings/power_supply/twl4030_madc_battery.txt b/Documentation/devicetree/bindings/power_supply/twl4030_madc_battery.txt
>>>> new file mode 100644
>>>> index 0000000..d3dd9d8
>>>> --- /dev/null
>>>> +++ b/Documentation/devicetree/bindings/power_supply/twl4030_madc_battery.txt
>>>> @@ -0,0 +1,43 @@
>>>> +twl4030_madc_battery
>>>> +
>>>> +Required properties:
>>>> + - compatible : "ti,twl4030-madc-battery"
>>>> + - capacity-uah : battery capacity in uAh
>>>> + - ti,volt-to-capacity-charging-map : list of voltage(mV):level(%) values
>>>> +	for charging calibration (see example)
>>>> + - ti,volt-to-capacity-discharging-map : list of voltage(mV):level(%) values
>>>> +	for discharging calibration (see example)
>>>> + - io-channels: Should contain IIO channel specifiers
>>>> +	for each element in io-channel-names.
>>>> +- io-channel-names: Should contain the following values:
>>>> + * "temp" - The ADC channel for temperature reading
>>>> + * "ichg" - The ADC channel for battery charging status
>>>> + * "vbat" - The ADC channel to measure the battery voltage
>>>> +
>>>> +Example:
>>>> +	madc-battery {
>>>> +		compatible = "ti,twl4030-madc-battery";
>>>> +		capacity-uah = <1200000>;
>>>> +		ti,volt-to-capacity-charging-map = <4200 100>,
>>>> +					        <4100 75>,
>>>> +					        <4000 55>,
>>>> +					        <3900 25>,
>>>> +					        <3800 5>,
>>>> +					        <3700 2>,
>>>> +					        <3600 1>,
>>>> +					        <3300 0>;
>>>> +
>>>> +		ti,volt-to-capacity-discharging-map = <4200 100>
>>>> +						   <4100 95>,
>>>> +						   <4000 70>,
>>>> +						   <3800 50>,
>>>> +						   <3700 10>,
>>>> +						   <3600 5>,
>>>> +						   <3300 0>;
>>>> +		io-channels = <&twl_madc 1>,
>>>> +	                      <&twl_madc 10>,
>>>> +			      <&twl_madc 12>;
>>>> +		io-channel-names = "temp",
>>>> +		                   "ichg",
>>>> +		                   "vbat";
>>>> +	};
>>> 
>>> Rather than just making platform_data into device tree properties..
>>> 
>>> Can't you hide the these custom properties behind the compatible flag?
>>> 
>>> You can initialize that data in the driver based on the compatible
>>> flag and the match data.
>>> 
>>> This makes sense if you can group things to similar configurations.
>> 
>> Maybe I have not completely understood your proposal.
>> 
>> Do you mean to go back to have big parameter tables for each device/battery
>> combination in the driver code and the compatible flag (e.g. compatible = “board17”)
>> chooses the right data set for the charging map and channels?
> 
> If you can somehow group them, then yes.

I don’t see how to group them. Could you make a proposal?

> Not for every board if there
> are many of them naturally.
> 
>> I thought this is what the DT was introduced for - to have the same driver 
>> code but adapt to different boards depending on hardware variations.
> 
> Yeah but you also need to consider the issues related to introducing
> new device tree properties. The device tree properties introduced
> should be generic where possible.

Yes, that was discussed for a while for this driver’s bindings leading to v4.

Which ones do you think are not generic enough?

> 
>> And batteries have very different characteristics and vary between devices…
> 
> Right. Maybe that has been already agreed on to use capacity-uah for
> batteries in general?

Ah, do you mean with generic/not generic the distinction between a “ti,” prefix
and no prefix?

Well, I don’t know if there is such an agreement and I would have no argument
against calling it “ti,capacity-uah”.

> In that case I have not problem with that as
> it’s a generic property :)

Well, many batteries and systems have a fuel gauge chip (e.g. bq27000). This
chip “knows” the capacity. But therefore it is not needed to specify
it anywhere because it can be read out (usually in uAh).

This driver is to solve the issue that there is no such factory-programmed
battery or fuel gauge chip connected to a twl4030 driver. Nobody can program
that capacity value - except someone matching the device tree with real hardware.

And, by doing and averaging some charge-discharge cycles the fuel gauge
mapping is calibrated.

> 
>> The charging maps are depending on the battery type connected to the twl4030
>> and which madc channel is which value is also a little hardware dependent
>> (although the twl4030 doesn’t give much choice).
> 
> Just to consider alternatives before introducing driver specific
> property for the maps.. Maybe here you could have few different type
> of maps and select something safe by default? Of course it could be this
> is higly board specific, I think some devices may be able to run below
> 3.3V for example..

Every battery setup has a different map (which also might depend on the
series resistance of the battery and the wiring).

> 
>> And moving this information into the driver for each board that uses it
>> would blow up the code.
> 
> Right, I'm not saying we should build databases into the kernel drivers.
> Just trying to avoid introducing driver specific properties unless
> really needed :)

They are not really driver specific, they are battery specific. They
describe properties of the battery:

* capacity - depends on battery
* voltage depending on current charging level - depends on battery (and differs between charging and discharging)
* wiring of MADC inputs to the twl4030 is board dependent.

So all these properties are not driver specific, but describe hardware.
And therefore they had been introduced exactly this way into the old
platform_data driver.

So if you want to see a “ti," prefix to the capacity property I would be
fine.

BR,
Nikolaus


--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Tony Lindgren March 11, 2015, 5:43 p.m. UTC | #5
* Dr. H. Nikolaus Schaller <hns@goldelico.com> [150311 10:13]:
> Am 11.03.2015 um 17:44 schrieb Tony Lindgren <tony@atomide.com>:
> > * Dr. H. Nikolaus Schaller <hns@goldelico.com> [150311 09:17]:
> >> Am 11.03.2015 um 16:24 schrieb Tony Lindgren <tony@atomide.com>:
> >>> 
> >>> Rather than just making platform_data into device tree properties..
> >>> 
> >>> Can't you hide the these custom properties behind the compatible flag?
> >>> 
> >>> You can initialize that data in the driver based on the compatible
> >>> flag and the match data.
> >>> 
> >>> This makes sense if you can group things to similar configurations.
> >> 
> >> Maybe I have not completely understood your proposal.
> >> 
> >> Do you mean to go back to have big parameter tables for each device/battery
> >> combination in the driver code and the compatible flag (e.g. compatible = “board17”)
> >> chooses the right data set for the charging map and channels?
> > 
> > If you can somehow group them, then yes.
> 
> I don’t see how to group them. Could you make a proposal?

Sorry no idea about that :) I though you may have some ideas based on
dealing with the driver.
 
> >> I thought this is what the DT was introduced for - to have the same driver 
> >> code but adapt to different boards depending on hardware variations.
> > 
> > Yeah but you also need to consider the issues related to introducing
> > new device tree properties. The device tree properties introduced
> > should be generic where possible.
> 
> Yes, that was discussed for a while for this driver’s bindings leading to v4.
> 
> Which ones do you think are not generic enough?

It seems maps is the only one then, assuming the cpacity-uah can be made
Linux generic. 

> >> And batteries have very different characteristics and vary between devices…
> > 
> > Right. Maybe that has been already agreed on to use capacity-uah for
> > batteries in general?
> 
> Ah, do you mean with generic/not generic the distinction between a “ti,” prefix
> and no prefix?
> 
> Well, I don’t know if there is such an agreement and I would have no argument
> against calling it “ti,capacity-uah”.

No no, "capacity-uah" is what we should use, but you need an ack from
the battery and device tree people that this is OK. Let's not add
"ti,capacity-uah” as that can obviously be a generic property.
 
> > In that case I have not problem with that as
> > it’s a generic property :)
> 
> Well, many batteries and systems have a fuel gauge chip (e.g. bq27000). This
> chip “knows” the capacity. But therefore it is not needed to specify
> it anywhere because it can be read out (usually in uAh).
> 
> This driver is to solve the issue that there is no such factory-programmed
> battery or fuel gauge chip connected to a twl4030 driver. Nobody can program
> that capacity value - except someone matching the device tree with real hardware.
> 
> And, by doing and averaging some charge-discharge cycles the fuel gauge
> mapping is calibrated.

OK 
 
> >> The charging maps are depending on the battery type connected to the twl4030
> >> and which madc channel is which value is also a little hardware dependent
> >> (although the twl4030 doesn’t give much choice).
> > 
> > Just to consider alternatives before introducing driver specific
> > property for the maps.. Maybe here you could have few different type
> > of maps and select something safe by default? Of course it could be this
> > is higly board specific, I think some devices may be able to run below
> > 3.3V for example..
> 
> Every battery setup has a different map (which also might depend on the
> series resistance of the battery and the wiring).
> 
> > 
> >> And moving this information into the driver for each board that uses it
> >> would blow up the code.
> > 
> > Right, I'm not saying we should build databases into the kernel drivers.
> > Just trying to avoid introducing driver specific properties unless
> > really needed :)
> 
> They are not really driver specific, they are battery specific. They
> describe properties of the battery:
> 
> * capacity - depends on battery
> * voltage depending on current charging level - depends on battery (and differs between charging and discharging)
> * wiring of MADC inputs to the twl4030 is board dependent.
> 
> So all these properties are not driver specific, but describe hardware.
> And therefore they had been introduced exactly this way into the old
> platform_data driver.
> 
> So if you want to see a “ti," prefix to the capacity property I would be
> fine.

Oh if they are battery spicific, then ideally we'd have generic batery
voltage to capacity maps property rather than a custom ti specific
property.

To avoid extra hassles later on, maybe you could submit a generic
binding patch only documenting it to the battery people and the device
tree people? That will make it easier to maintain this driver in the
long run.

Regards,

Tony
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Sebastian Reichel March 11, 2015, 7:36 p.m. UTC | #6
Hi,

On Wed, Mar 11, 2015 at 10:43:17AM -0700, Tony Lindgren wrote:
> No no, "capacity-uah" is what we should use, but you need an ack from
> the battery and device tree people that this is OK. Let's not add
> "ti,capacity-uah” as that can obviously be a generic property.

I'm okay with capacity-uah.

> > [...]
> 
> Oh if they are battery spicific, then ideally we'd have generic batery
> voltage to capacity maps property rather than a custom ti specific
> property.
> 
> To avoid extra hassles later on, maybe you could submit a generic
> binding patch only documenting it to the battery people and the device
> tree people? That will make it easier to maintain this driver in the
> long run.

Actually the proper way would be to differentiate between the
battery and the measurement chip / adc and that should be
implemented in the long run. The kernel's power supply framework
is not yet ready for it, though.

Example DT:

battery {
    battery-specific-data;
};

fuel-gauge {
    measures = <&battery>;
};

charger {
    charges = <&battery>;
};

Since infrastructure for generic bindings is missing, I think its
best to have the vendor properties for now and map this to generic
properties, once they have been specified.

-- Sebastian
Tony Lindgren March 11, 2015, 7:37 p.m. UTC | #7
* Sebastian Reichel <sre@kernel.org> [150311 12:37]:
> Hi,
> 
> On Wed, Mar 11, 2015 at 10:43:17AM -0700, Tony Lindgren wrote:
> > No no, "capacity-uah" is what we should use, but you need an ack from
> > the battery and device tree people that this is OK. Let's not add
> > "ti,capacity-uah” as that can obviously be a generic property.
> 
> I'm okay with capacity-uah.

OK great.
 
> > > [...]
> > 
> > Oh if they are battery spicific, then ideally we'd have generic batery
> > voltage to capacity maps property rather than a custom ti specific
> > property.
> > 
> > To avoid extra hassles later on, maybe you could submit a generic
> > binding patch only documenting it to the battery people and the device
> > tree people? That will make it easier to maintain this driver in the
> > long run.
> 
> Actually the proper way would be to differentiate between the
> battery and the measurement chip / adc and that should be
> implemented in the long run. The kernel's power supply framework
> is not yet ready for it, though.
> 
> Example DT:
> 
> battery {
>     battery-specific-data;
> };
> 
> fuel-gauge {
>     measures = <&battery>;
> };
> 
> charger {
>     charges = <&battery>;
> };
> 
> Since infrastructure for generic bindings is missing, I think its
> best to have the vendor properties for now and map this to generic
> properties, once they have been specified.

OK, sounds good to me. I'm fine with the $subject patch as it is then:

Acked-by: Tony Lindgren <tony@atomide.com>

Tony
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Pavel Machek March 31, 2015, 7:26 a.m. UTC | #8
Hi!

> > >> +		io-channels = <&twl_madc 1>,
> > >> +	                      <&twl_madc 10>,
> > >> +			      <&twl_madc 12>;
> > >> +		io-channel-names = "temp",
> > >> +		                   "ichg",
> > >> +		                   "vbat";
> > >> +	};
> > > 
> > > Rather than just making platform_data into device tree properties..
> > > 
> > > Can't you hide the these custom properties behind the compatible flag?
> > > 
> > > You can initialize that data in the driver based on the compatible
> > > flag and the match data.
> > > 
> > > This makes sense if you can group things to similar configurations.
> > 
> > Maybe I have not completely understood your proposal.
> > 
> > Do you mean to go back to have big parameter tables for each device/battery
> > combination in the driver code and the compatible flag (e.g. compatible = “board17”)
> > chooses the right data set for the charging map and channels?
> 
> If you can somehow group them, then yes. Not for every board if there
> are many of them naturally.
>  
> > I thought this is what the DT was introduced for - to have the same driver 
> > code but adapt to different boards depending on hardware variations.
> 
> Yeah but you also need to consider the issues related to introducing
> new device tree properties. The device tree properties introduced
> should be generic where possible.
> 
> > And batteries have very different characteristics and vary between devices…
> 
> Right. Maybe that has been already agreed on to use capacity-uah for
> batteries in general? In that case I have not problem with that as
> it's a generic property :)
>  
> > The charging maps are depending on the battery type connected to the twl4030
> > and which madc channel is which value is also a little hardware dependent
> > (although the twl4030 doesn’t give much choice).
> 
> Just to consider alternatives before introducing driver specific
> property for the maps.. Maybe here you could have few different type
> of maps and select something safe by default? Of course it could be this
> is higly board specific, I think some devices may be able to run below
> 3.3V for example..

As I explained in some other mail, those tables should not be
neccessary at all. They can be computed from li-ion characteristics
and internal resistance, and assumed current during charge and
discharge.

Running below 3.3V.. not really. At that point, the battery is really
_empty_, and voltage is going down really really fast.

Plus, you are damaging the battery at that point.
									Pavel
H. Nikolaus Schaller April 1, 2015, 8:18 a.m. UTC | #9
Hi Pavel,

Am 31.03.2015 um 09:26 schrieb Pavel Machek <pavel@ucw.cz>:

> Hi!
> 
>>>>> +		io-channels = <&twl_madc 1>,
>>>>> +	                      <&twl_madc 10>,
>>>>> +			      <&twl_madc 12>;
>>>>> +		io-channel-names = "temp",
>>>>> +		                   "ichg",
>>>>> +		                   "vbat";
>>>>> +	};
>>>> 
>>>> Rather than just making platform_data into device tree properties..
>>>> 
>>>> Can't you hide the these custom properties behind the compatible flag?
>>>> 
>>>> You can initialize that data in the driver based on the compatible
>>>> flag and the match data.
>>>> 
>>>> This makes sense if you can group things to similar configurations.
>>> 
>>> Maybe I have not completely understood your proposal.
>>> 
>>> Do you mean to go back to have big parameter tables for each device/battery
>>> combination in the driver code and the compatible flag (e.g. compatible = “board17”)
>>> chooses the right data set for the charging map and channels?
>> 
>> If you can somehow group them, then yes. Not for every board if there
>> are many of them naturally.
>> 
>>> I thought this is what the DT was introduced for - to have the same driver 
>>> code but adapt to different boards depending on hardware variations.
>> 
>> Yeah but you also need to consider the issues related to introducing
>> new device tree properties. The device tree properties introduced
>> should be generic where possible.
>> 
>>> And batteries have very different characteristics and vary between devices…
>> 
>> Right. Maybe that has been already agreed on to use capacity-uah for
>> batteries in general? In that case I have not problem with that as
>> it's a generic property :)
>> 
>>> The charging maps are depending on the battery type connected to the twl4030
>>> and which madc channel is which value is also a little hardware dependent
>>> (although the twl4030 doesn’t give much choice).
>> 
>> Just to consider alternatives before introducing driver specific
>> property for the maps.. Maybe here you could have few different type
>> of maps and select something safe by default? Of course it could be this
>> is higly board specific, I think some devices may be able to run below
>> 3.3V for example..
> 
> As I explained in some other mail, those tables should not be
> neccessary at all. They can be computed from li-ion characteristics
> and internal resistance, and assumed current during charge and
> discharge.

I already explained that we do not know the charging and discharging
current well enough for such a calculation.

And I explained that the “internal resistance” is a system (battery + cables +
connectors + other circuits) parameter that is not easy to derive or measure
and type into the .dts source code.

At least I have no idea how I should find it out for my boards. While I can
easily determine the curves (and we already have them for the platform_data
driver).

Please propose your own code doing that so that we can test if it is
better.

> 
> Running below 3.3V.. not really. At that point, the battery is really
> _empty_, and voltage is going down really really fast.

It is the diffference between 2% and 0% where a fuel indication might
be most important…

> 
> Plus, you are damaging the battery at that point.

The power controller will shut down - but the driver should report
reasonable (but IMHO not necessarily perfect) values until the last
moment.

BR,
Nikolaus

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Rob Herring April 1, 2015, 4:30 p.m. UTC | #10
On Tue, Mar 10, 2015 at 4:27 PM, Marek Belisko <marek@goldelico.com> wrote:
> Signed-off-by: Marek Belisko <marek@goldelico.com>
> ---
>  .../bindings/power_supply/twl4030_madc_battery.txt | 43 ++++++++++++++++++++++
>  1 file changed, 43 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/power_supply/twl4030_madc_battery.txt
>
> diff --git a/Documentation/devicetree/bindings/power_supply/twl4030_madc_battery.txt b/Documentation/devicetree/bindings/power_supply/twl4030_madc_battery.txt
> new file mode 100644
> index 0000000..d3dd9d8
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/power_supply/twl4030_madc_battery.txt
> @@ -0,0 +1,43 @@
> +twl4030_madc_battery
> +
> +Required properties:
> + - compatible : "ti,twl4030-madc-battery"

Is this a sub-node of the twl4030 or something? Please define where
this fits (hint: I would expect to be a sub node of a charging
controller or battery monitor).

> + - capacity-uah : battery capacity in uAh
> + - ti,volt-to-capacity-charging-map : list of voltage(mV):level(%) values
> +       for charging calibration (see example)
> + - ti,volt-to-capacity-discharging-map : list of voltage(mV):level(%) values
> +       for discharging calibration (see example)

These seem like properties of the battery independent of the
battery/charging controller which is the twl4030. Ideally we would
define battery nodes generically and independent of the charge
controllers. Then there are smart batteries to consider too.

Rob

> + - io-channels: Should contain IIO channel specifiers
> +       for each element in io-channel-names.
> +- io-channel-names: Should contain the following values:
> + * "temp" - The ADC channel for temperature reading
> + * "ichg" - The ADC channel for battery charging status
> + * "vbat" - The ADC channel to measure the battery voltage
> +
> +Example:
> +       madc-battery {
> +               compatible = "ti,twl4030-madc-battery";
> +               capacity-uah = <1200000>;
> +               ti,volt-to-capacity-charging-map = <4200 100>,
> +                                               <4100 75>,
> +                                               <4000 55>,
> +                                               <3900 25>,
> +                                               <3800 5>,
> +                                               <3700 2>,
> +                                               <3600 1>,
> +                                               <3300 0>;
> +
> +               ti,volt-to-capacity-discharging-map = <4200 100>
> +                                                  <4100 95>,
> +                                                  <4000 70>,
> +                                                  <3800 50>,
> +                                                  <3700 10>,
> +                                                  <3600 5>,
> +                                                  <3300 0>;
> +               io-channels = <&twl_madc 1>,
> +                             <&twl_madc 10>,
> +                             <&twl_madc 12>;
> +               io-channel-names = "temp",
> +                                  "ichg",
> +                                  "vbat";
> +       };
> --
> 1.9.1
>
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
H. Nikolaus Schaller April 1, 2015, 4:46 p.m. UTC | #11
Hi,

Am 01.04.2015 um 18:30 schrieb Rob Herring <robherring2@gmail.com>:

> On Tue, Mar 10, 2015 at 4:27 PM, Marek Belisko <marek@goldelico.com> wrote:
>> Signed-off-by: Marek Belisko <marek@goldelico.com>
>> ---
>> .../bindings/power_supply/twl4030_madc_battery.txt | 43 ++++++++++++++++++++++
>> 1 file changed, 43 insertions(+)
>> create mode 100644 Documentation/devicetree/bindings/power_supply/twl4030_madc_battery.txt
>> 
>> diff --git a/Documentation/devicetree/bindings/power_supply/twl4030_madc_battery.txt b/Documentation/devicetree/bindings/power_supply/twl4030_madc_battery.txt
>> new file mode 100644
>> index 0000000..d3dd9d8
>> --- /dev/null
>> +++ b/Documentation/devicetree/bindings/power_supply/twl4030_madc_battery.txt
>> @@ -0,0 +1,43 @@
>> +twl4030_madc_battery
>> +
>> +Required properties:
>> + - compatible : "ti,twl4030-madc-battery"
> 
> Is this a sub-node of the twl4030 or something? Please define where
> this fits (hint: I would expect to be a sub node of a charging
> controller or battery monitor).

It is a driver connecting some battery parameters with measurements provided
by the twl4030-madc to present a /sys/class/power_supply node for the battery
with a coarse estimate for the charging level.

So maybe the best wording is that it is a battery-driver assuming a twl4030-madc
providing raw measurements (voltage, charging).

Therefore it could as well be battery-twl4030-madc.

> 
>> + - capacity-uah : battery capacity in uAh
>> + - ti,volt-to-capacity-charging-map : list of voltage(mV):level(%) values
>> +       for charging calibration (see example)
>> + - ti,volt-to-capacity-discharging-map : list of voltage(mV):level(%) values
>> +       for discharging calibration (see example)
> 
> These seem like properties of the battery independent of the
> battery/charging controller which is the twl4030. Ideally we would
> define battery nodes generically and independent of the charge
> controllers. Then there are smart batteries to consider too.

For smart batteries there are completely independent mechanisms
like I2C and HDQ/1-wire communication with fuel gauge chips (e.g. b27xxx).

Those do not need such battery properties because they are stored
in EEPROMs within these chips.

So all this is a quite special solution just for those handful of board that
have no smart battery and use exactly this twl4030 chip.

It is not intended to become a generic charging/fuel solution. Just
make it work with DT (it worked with platform_data since ~3.12).

BR,
Nikolaus



> 
> Rob
> 
>> + - io-channels: Should contain IIO channel specifiers
>> +       for each element in io-channel-names.
>> +- io-channel-names: Should contain the following values:
>> + * "temp" - The ADC channel for temperature reading
>> + * "ichg" - The ADC channel for battery charging status
>> + * "vbat" - The ADC channel to measure the battery voltage
>> +
>> +Example:
>> +       madc-battery {
>> +               compatible = "ti,twl4030-madc-battery";
>> +               capacity-uah = <1200000>;
>> +               ti,volt-to-capacity-charging-map = <4200 100>,
>> +                                               <4100 75>,
>> +                                               <4000 55>,
>> +                                               <3900 25>,
>> +                                               <3800 5>,
>> +                                               <3700 2>,
>> +                                               <3600 1>,
>> +                                               <3300 0>;
>> +
>> +               ti,volt-to-capacity-discharging-map = <4200 100>
>> +                                                  <4100 95>,
>> +                                                  <4000 70>,
>> +                                                  <3800 50>,
>> +                                                  <3700 10>,
>> +                                                  <3600 5>,
>> +                                                  <3300 0>;
>> +               io-channels = <&twl_madc 1>,
>> +                             <&twl_madc 10>,
>> +                             <&twl_madc 12>;
>> +               io-channel-names = "temp",
>> +                                  "ichg",
>> +                                  "vbat";
>> +       };
>> --
>> 1.9.1
>> 

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Pavel Machek April 1, 2015, 8:16 p.m. UTC | #12
Hi!

> > As I explained in some other mail, those tables should not be
> > neccessary at all. They can be computed from li-ion characteristics
> > and internal resistance, and assumed current during charge and
> > discharge.
> 
> I already explained that we do not know the charging and discharging
> current well enough for such a calculation.
> 
> And I explained that the “internal resistance” is a system (battery + cables +
> connectors + other circuits) parameter that is not easy to derive or measure
> and type into the .dts source code.
> 
> At least I have no idea how I should find it out for my boards. While I can
> easily determine the curves (and we already have them for the platform_data
> driver).
> 
> Please propose your own code doing that so that we can test if it is
> better.

So, how does this look?

It looks to me like you have cca 0.1 Ohm resistance in your system,
and are using cca 75mA while discharging, and charge by cca 1.4A. (And
these are all the coefficients the code should need; rest is battery
characteristics -- common to all li-ions, and charger characteristics
-- that will be common to all cellphones. If current can be measured,
this code should go more precise answers).

pavel@amd:~/g/tui/ofone$ ./liion_maps
Charging
Voltage  4.2 V ; table  100 %  internal voltage 4.18 V current 0.233 A computed  97 %
Voltage  4.1 V ; table  75 %  internal voltage 4.08 V current 0.233 A computed  87 %
Voltage  4.0 V ; table  55 %  internal voltage 3.93 V current 0.700 A computed  69 %
Voltage  3.9 V ; table  25 %  internal voltage 3.76 V current 1.400 A computed  26 %
Voltage  3.8 V ; table  5 %  internal voltage 3.66 V current 1.400 A computed  3 %
Voltage  3.7 V ; table  2 %  internal voltage 3.56 V current 1.400 A computed  2 %
Voltage  3.6 V ; table  1 %  internal voltage 3.46 V current 1.400 A computed  1 %
Voltage  3.3 V ; table  0 %  internal voltage 3.16 V current 1.400 A computed  -1 %
Badness  395.4861761427434
Discharging
Voltage  4.2 V ; table  100 %  internal voltage 4.21 V current -0.075 A computed  100 %
Voltage  4.1 V ; table  95 %  internal voltage 4.11 V current -0.075 A computed  91 %
Voltage  4.0 V ; table  70 %  internal voltage 4.01 V current -0.075 A computed  79 %
Voltage  3.8 V ; table  50 %  internal voltage 3.81 V current -0.075 A computed  46 %
Voltage  3.7 V ; table  10 %  internal voltage 3.71 V current -0.075 A computed  3 %
Voltage  3.6 V ; table  5 %  internal voltage 3.61 V current -0.075 A computed  2 %
Voltage  3.3 V ; table  0 %  internal voltage 3.31 V current -0.075 A computed  0 %
Badness  171.69576218433212

> > Running below 3.3V.. not really. At that point, the battery is really
> > _empty_, and voltage is going down really really fast.
> 
> It is the diffference between 2% and 0% where a fuel indication might
> be most important…

> > Plus, you are damaging the battery at that point.
> 
> The power controller will shut down - but the driver should report
> reasonable (but IMHO not necessarily perfect) values until the last
> moment.

It is tricky to do a good job near 0%... or anywhere else. See for
example

http://cseweb.ucsd.edu/~trosing/lectures/battery.pdf

You start a call, percentage goes down, end a call, it will go
back up. I'm pretty sure you will not be able to make a call with "5%"
indication from your code at low battery temperature (say -10C).

Anyway, see above, I think I provide reasonable values even in that range.

Signed-off-by: Pavel Machek <pavel@ucw.cz>
									Pavel

#!/usr/bin/python3
import math

def percent_internal(v):
    u = 0.0387-(1.4523*(3.7835-v))
    if u < 0:
        # Formula above does gives 19.66% for 3.756, and refuses to
        # work below that. Assume 3.3V is empty battery, and provide
        # linear dependency below that.
        u = (v - 3.3) * ((3.756 - 3.3) * 19.66)
        return u
    return (0.1966+math.sqrt(u))*100

charging = [ [4200, 100], [4100, 75], [4000, 55], [3900, 25], [3800, 5], [3700, 2], [3600, 1], [3300, 0] ]

discharging = [ [4200, 100], [4100, 95], [4000, 70], [3800, 50], [3700, 10], [3600, 5], [3300, 0] ]

# current > 0: charging
def percent_ohm(v, current):
    v_int = v - current * 0.1
    print(" internal voltage %1.2f V current %.3f A " % (v_int, current), end='')
    return percent_internal(v_int)

def percent(v, charging):
    if charging:
        # Charger model. Chargers will do constant current then
        # constant voltage, so current will go down as voltage
        # approaches 4.2V
        i = 2.8
        if v >= 4.:
            i = i/2
        if v >= 4.05:
            i = i/3
    else:
        i = -0.15

    # With current forced to 0, we get badness 4014 and 258
    # 2.5A, sloped: badness 576
    # +4A -> badness 1293
    # +3A -> badness 890
    # +2.5A ->         339
    # +2.4A -> badness 389
    # +2.3A -> badness 444    
    # +2.2A -> badness 504
    # +2A -> badness 634
    # +1A -> badness 1450
    # +0.5A -> badness 2865
    # -0.2A -> badness 252
    # -0.15A -> badness 251.37
    # -0.1A -> badness 252
    # -0.05A -> badness 254
    i/=2
    return percent_ohm(v, i)

def compute(map, charging):
    diff = 0
    if charging:
        print("Charging")
    else:
        print("Discharging")
    for v, p in map:
        v /= 1000.
        print("Voltage ", v, "V ; table ", p, "% ", end='')
        p2 = percent(v, charging)
        print("computed ", int(p2), "%")
        diff += (p-p2)*(p-p2)
    print("Badness ", diff)

                
#perc = percent(volt)
compute(charging, 1)
compute(discharging, 0)
H. Nikolaus Schaller April 1, 2015, 8:39 p.m. UTC | #13
Hi,

Am 01.04.2015 um 22:16 schrieb Pavel Machek <pavel@ucw.cz>:

> Hi!
> 
>>> As I explained in some other mail, those tables should not be
>>> neccessary at all. They can be computed from li-ion characteristics
>>> and internal resistance, and assumed current during charge and
>>> discharge.
>> 
>> I already explained that we do not know the charging and discharging
>> current well enough for such a calculation.
>> 
>> And I explained that the “internal resistance” is a system (battery + cables +
>> connectors + other circuits) parameter that is not easy to derive or measure
>> and type into the .dts source code.
>> 
>> At least I have no idea how I should find it out for my boards. While I can
>> easily determine the curves (and we already have them for the platform_data
>> driver).
>> 
>> Please propose your own code doing that so that we can test if it is
>> better.
> 
> So, how does this look?
> 
> It looks to me like you have cca 0.1 Ohm resistance in your system,

This is completely unknown.

> and are using cca 75mA while discharging, and charge by cca 1.4A.

Where do you get these figures from?

A GTA04 board discharges usually between 50 and 400 mA (depending on activities)
and if you turn on WiFi, it will be up to 600 mA. If you use 3G it can draw even more
during operation.

Total current going in is 500-800 mA (depending on USB negotiations) and this is
split into system operation and charging current. So 1.4A charging current is not
possible. Rather 200-500 mA.

So it might be that the battery is discharged although the system is connected
to a charger.

> (And
> these are all the coefficients the code should need; rest is battery
> characteristics -- common to all li-ions, and charger characteristics
> -- that will be common to all cellphones. If current can be measured,
> this code should go more precise answers).
> 
> pavel@amd:~/g/tui/ofone$ ./liion_maps
> Charging
> Voltage  4.2 V ; table  100 %  internal voltage 4.18 V current 0.233 A computed  97 %
> Voltage  4.1 V ; table  75 %  internal voltage 4.08 V current 0.233 A computed  87 %
> Voltage  4.0 V ; table  55 %  internal voltage 3.93 V current 0.700 A computed  69 %
> Voltage  3.9 V ; table  25 %  internal voltage 3.76 V current 1.400 A computed  26 %
> Voltage  3.8 V ; table  5 %  internal voltage 3.66 V current 1.400 A computed  3 %
> Voltage  3.7 V ; table  2 %  internal voltage 3.56 V current 1.400 A computed  2 %
> Voltage  3.6 V ; table  1 %  internal voltage 3.46 V current 1.400 A computed  1 %
> Voltage  3.3 V ; table  0 %  internal voltage 3.16 V current 1.400 A computed  -1 %
> Badness  395.4861761427434
> Discharging
> Voltage  4.2 V ; table  100 %  internal voltage 4.21 V current -0.075 A computed  100 %
> Voltage  4.1 V ; table  95 %  internal voltage 4.11 V current -0.075 A computed  91 %
> Voltage  4.0 V ; table  70 %  internal voltage 4.01 V current -0.075 A computed  79 %
> Voltage  3.8 V ; table  50 %  internal voltage 3.81 V current -0.075 A computed  46 %
> Voltage  3.7 V ; table  10 %  internal voltage 3.71 V current -0.075 A computed  3 %
> Voltage  3.6 V ; table  5 %  internal voltage 3.61 V current -0.075 A computed  2 %
> Voltage  3.3 V ; table  0 %  internal voltage 3.31 V current -0.075 A computed  0 %
> Badness  171.69576218433212
> 
>>> Running below 3.3V.. not really. At that point, the battery is really
>>> _empty_, and voltage is going down really really fast.
>> 
>> It is the diffference between 2% and 0% where a fuel indication might
>> be most important…
> 
>>> Plus, you are damaging the battery at that point.
>> 
>> The power controller will shut down - but the driver should report
>> reasonable (but IMHO not necessarily perfect) values until the last
>> moment.
> 
> It is tricky to do a good job near 0%... or anywhere else. See for
> example
> 
> http://cseweb.ucsd.edu/~trosing/lectures/battery.pdf
> 
> You start a call, percentage goes down, end a call, it will go
> back up. I'm pretty sure you will not be able to make a call with "5%"
> indication from your code at low battery temperature (say -10C).

The whole system is heating up a little, so that you never have -10C for the
battery.

I think you are trying to solve situations that don’t exist in practice.

And AFAIK, the GTA04 board is the only user of this driver in case the battery
has no built-in fuel gauge. So why improve it beyond what the GTA04 users need?

I repeat myself: this driver is not built for highest precision because there are
better methods (fuel gauge chip). These might not be available so this fall-back
driver has been built.

It is definitively better than no driver and worse than the optimum.

> 
> Anyway, see above, I think I provide reasonable values even in that range.
> 
> Signed-off-by: Pavel Machek <pavel@ucw.cz>
> 									Pavel
> 
> #!/usr/bin/python3
> import math
> 
> def percent_internal(v):
>    u = 0.0387-(1.4523*(3.7835-v))
>    if u < 0:
>        # Formula above does gives 19.66% for 3.756, and refuses to
>        # work below that. Assume 3.3V is empty battery, and provide
>        # linear dependency below that.
>        u = (v - 3.3) * ((3.756 - 3.3) * 19.66)
>        return u
>    return (0.1966+math.sqrt(u))*100
> 
> charging = [ [4200, 100], [4100, 75], [4000, 55], [3900, 25], [3800, 5], [3700, 2], [3600, 1], [3300, 0] ]
> 
> discharging = [ [4200, 100], [4100, 95], [4000, 70], [3800, 50], [3700, 10], [3600, 5], [3300, 0] ]
> 
> # current > 0: charging
> def percent_ohm(v, current):
>    v_int = v - current * 0.1
>    print(" internal voltage %1.2f V current %.3f A " % (v_int, current), end='')
>    return percent_internal(v_int)
> 
> def percent(v, charging):
>    if charging:
>        # Charger model. Chargers will do constant current then
>        # constant voltage, so current will go down as voltage
>        # approaches 4.2V
>        i = 2.8
>        if v >= 4.:
>            i = i/2
>        if v >= 4.05:
>            i = i/3
>    else:
>        i = -0.15
> 
>    # With current forced to 0, we get badness 4014 and 258
>    # 2.5A, sloped: badness 576
>    # +4A -> badness 1293
>    # +3A -> badness 890
>    # +2.5A ->         339
>    # +2.4A -> badness 389
>    # +2.3A -> badness 444    
>    # +2.2A -> badness 504
>    # +2A -> badness 634
>    # +1A -> badness 1450
>    # +0.5A -> badness 2865
>    # -0.2A -> badness 252
>    # -0.15A -> badness 251.37
>    # -0.1A -> badness 252
>    # -0.05A -> badness 254
>    i/=2
>    return percent_ohm(v, i)
> 
> def compute(map, charging):
>    diff = 0
>    if charging:
>        print("Charging")
>    else:
>        print("Discharging")
>    for v, p in map:
>        v /= 1000.
>        print("Voltage ", v, "V ; table ", p, "% ", end='')
>        p2 = percent(v, charging)
>        print("computed ", int(p2), "%")
>        diff += (p-p2)*(p-p2)
>    print("Badness ", diff)
> 
> 
> #perc = percent(volt)
> compute(charging, 1)
> compute(discharging, 0)

Please explain what you calculate here. Especially what “Badness” is?

BR,
Nikolaus

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Pavel Machek April 4, 2015, 8:16 a.m. UTC | #14
Hi!

> >> Please propose your own code doing that so that we can test if it is
> >> better.
> > 
> > So, how does this look?
> > 
> > It looks to me like you have cca 0.1 Ohm resistance in your system,
> 
> This is completely unknown.
> 
> > and are using cca 75mA while discharging, and charge by cca 1.4A.
> 
> Where do you get these figures from?

Least squares fitting of my coefficients to your tables.

> A GTA04 board discharges usually between 50 and 400 mA (depending on activities)
> and if you turn on WiFi, it will be up to 600 mA. If you use 3G it can draw even more
> during operation.

How big battery do you have? According to least squares fitting,
assuming maximum charge of .46A, you were taking about 25mA when
doing the discharge test.

> Total current going in is 500-800 mA (depending on USB negotiations) and this is
> split into system operation and charging current. So 1.4A charging current is not
> possible. Rather 200-500 mA.
> 
> So it might be that the battery is discharged although the system is connected
> to a charger.

Yah, and your battery meter will be wrong in such case, as will be
mine, because they are based on same information. The thing is, mine
can be improved without adding more tables. 

> > It is tricky to do a good job near 0%... or anywhere else. See for
> > example
> > 
> > http://cseweb.ucsd.edu/~trosing/lectures/battery.pdf
> > 
> > You start a call, percentage goes down, end a call, it will go
> > back up. I'm pretty sure you will not be able to make a call with "5%"
> > indication from your code at low battery temperature (say -10C).
> 
> The whole system is heating up a little, so that you never have -10C for the
> battery.

Umm. When not calling, your phone should not heat itself up. And you
definitely can power it down, leave it in outer pocket, then power it
up. It is actually something people do when they want to preserve battery...

> I think you are trying to solve situations that don’t exist in practice.
> 
> And AFAIK, the GTA04 board is the only user of this driver in case the battery
> has no built-in fuel gauge. So why improve it beyond what the GTA04
> users need?

Because then other users can share the same code, and because you
avoid big ugly tables in dts.

> I repeat myself: this driver is not built for highest precision because there are
> better methods (fuel gauge chip). These might not be available so this fall-back
> driver has been built.
> 
> It is definitively better than no driver and worse than the optimum.

And my email suggested solution better than your driver, so why not
just use it?


> > #perc = percent(volt)
> > compute(charging, 1)
> > compute(discharging, 0)
> 
> Please explain what you calculate here. Especially what “Badness” is?

Badness is error in least squares method.

Here are updated tables:

pavel@duo:~/g/tui/ofone$ ./liion_maps
Charging
Voltage  4.2 V ; table  100 %  internal voltage 4.18 V current 0.078 A computed  97 %
Voltage  4.1 V ; table  75 %  internal voltage 4.08 V current 0.078 A computed  87 %
Voltage  4.0 V ; table  55 %  internal voltage 3.93 V current 0.233 A computed  69 %
Voltage  3.9 V ; table  25 %  internal voltage 3.76 V current 0.467 A computed  26 %
Voltage  3.8 V ; table  5 %  internal voltage 3.66 V current 0.467 A computed  3 %
Voltage  3.7 V ; table  2 %  internal voltage 3.56 V current 0.467 A computed  2 %
Voltage  3.6 V ; table  1 %  internal voltage 3.46 V current 0.467 A computed  1 %
Voltage  3.3 V ; table  0 %  internal voltage 3.16 V current 0.467 A computed  -1 %
Badness  395.4861761427434
Discharging
Voltage  4.2 V ; table  100 %  internal voltage 4.21 V current -0.025 A computed  100 %
Voltage  4.1 V ; table  95 %  internal voltage 4.11 V current -0.025 A computed  91 %
Voltage  4.0 V ; table  70 %  internal voltage 4.01 V current -0.025 A computed  79 %
Voltage  3.8 V ; table  50 %  internal voltage 3.81 V current -0.025 A computed  46 %
Voltage  3.7 V ; table  10 %  internal voltage 3.71 V current -0.025 A computed  3 %
Voltage  3.6 V ; table  5 %  internal voltage 3.61 V current -0.025 A computed  2 %
Voltage  3.3 V ; table  0 %  internal voltage 3.31 V current -0.025 A computed  0 %
Badness  171.69576218433212
pavel@duo:~/g/tui/ofone$ python

									Pavel
H. Nikolaus Schaller April 4, 2015, 9:46 a.m. UTC | #15
Hi Pavel,

Am 04.04.2015 um 10:16 schrieb Pavel Machek <pavel@ucw.cz>:

> Hi!
> 
>>>> Please propose your own code doing that so that we can test if it is
>>>> better.
>>> 
>>> So, how does this look?
>>> 
>>> It looks to me like you have cca 0.1 Ohm resistance in your system,
>> 
>> This is completely unknown.
>> 
>>> and are using cca 75mA while discharging, and charge by cca 1.4A.
>> 
>> Where do you get these figures from?
> 
> Least squares fitting of my coefficients to your tables.

Ah, I see.

> 
>> A GTA04 board discharges usually between 50 and 400 mA (depending on activities)
>> and if you turn on WiFi, it will be up to 600 mA. If you use 3G it can draw even more
>> during operation.
> 
> How big battery do you have?

1200 mAh

> According to least squares fitting,
> assuming maximum charge of .46A, you were taking about 25mA when
> doing the discharge test.

No, that can’t fit well. But I do not remember who has done this calibration in which situation
because it was done many months ago for the platform data version. We have just reformatted
the table for the DT.

> 
>> Total current going in is 500-800 mA (depending on USB negotiations) and this is
>> split into system operation and charging current. So 1.4A charging current is not
>> possible. Rather 200-500 mA.
>> 
>> So it might be that the battery is discharged although the system is connected
>> to a charger.
> 
> Yah, and your battery meter will be wrong in such case, as will be
> mine, because they are based on same information.

Well, it is not “mine”, it is the platform_data based driver already in kernel.org
since ca. 3.12.

We have just updated it to DT to get rid of platform_data in this area as well…

Yes, I see your argument that hiding the tables (two characteristic curves)
into an analytic approximation can be superior. 

The problem I see is just that your formula needs some parameters which
are difficult to derive or estimate. And must be adjusted to the specific
battery and system setup in a non-trivial way.

At least we must supply a tool (in the kernel/scripts directory?) where someone
can can estimate the parameters of the formula from the characteristic curves.

Maybe a tool that automatically runs several charge/discharge cycles at
different system load. And measures time vs. voltage. And assumes a linear
capacity decrease between the end points. (i.e. if it needs 10 hours to charge
from completely empty to full, we can assume 50% after 5,0 hours).

So my goal is to measure all characteristics of the battery - and no need to
study a (potentially non-existing) data sheet.

If you can provide that for all parameters of your algorithm I am fine with it.

> The thing is, mine
> can be improved without adding more tables. 

How can you improve your algorithm without tweaking or adding new parameters?

> 
>>> It is tricky to do a good job near 0%... or anywhere else. See for
>>> example
>>> 
>>> http://cseweb.ucsd.edu/~trosing/lectures/battery.pdf
>>> 
>>> You start a call, percentage goes down, end a call, it will go
>>> back up. I'm pretty sure you will not be able to make a call with "5%"
>>> indication from your code at low battery temperature (say -10C).
>> 
>> The whole system is heating up a little, so that you never have -10C for the
>> battery.
> 
> Umm. When not calling, your phone should not heat itself up.

Yes, in suspend it needs <20 mA which does not heat or course.
But steady operation at 20-400 mA does significantly rise OMAP
temperature beyond environment temperature.

> And you
> definitely can power it down, leave it in outer pocket, then power it
> up. It is actually something people do when they want to preserve battery...
> 
>> I think you are trying to solve situations that don’t exist in practice.
>> 
>> And AFAIK, the GTA04 board is the only user of this driver in case the battery
>> has no built-in fuel gauge. So why improve it beyond what the GTA04
>> users need?
> 
> Because then other users can share the same code, and because you

But you have ugly parameters in dts that nobody can easily see in the schematics
and that are full of assumptions.

>From a perspective of uncertainty analysis, estimation of the internal parameters
needs a higher precision than the calibration points.

> avoid big ugly tables in dts.

Well, the biggest tables I have seen in dts are keyboard matrix codes…

> 
>> I repeat myself: this driver is not built for highest precision because there are
>> better methods (fuel gauge chip). These might not be available so this fall-back
>> driver has been built.
>> 
>> It is definitively better than no driver and worse than the optimum.
> 
> And my email suggested solution better than your driver, so why not
> just use it?

I am not yet convinced that it is better.

It just moves the (unavoidable) limitations (measuring multiple calibration points)
just to a different area (measuring the hidden and not precisely known parameter
of the system).

> 
> 
>>> #perc = percent(volt)
>>> compute(charging, 1)
>>> compute(discharging, 0)
>> 
>> Please explain what you calculate here. Especially what “Badness” is?
> 
> Badness is error in least squares method.

Ok, I see. Thanks for clarification.

BR,
NIkolaus

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/Documentation/devicetree/bindings/power_supply/twl4030_madc_battery.txt b/Documentation/devicetree/bindings/power_supply/twl4030_madc_battery.txt
new file mode 100644
index 0000000..d3dd9d8
--- /dev/null
+++ b/Documentation/devicetree/bindings/power_supply/twl4030_madc_battery.txt
@@ -0,0 +1,43 @@ 
+twl4030_madc_battery
+
+Required properties:
+ - compatible : "ti,twl4030-madc-battery"
+ - capacity-uah : battery capacity in uAh
+ - ti,volt-to-capacity-charging-map : list of voltage(mV):level(%) values
+	for charging calibration (see example)
+ - ti,volt-to-capacity-discharging-map : list of voltage(mV):level(%) values
+	for discharging calibration (see example)
+ - io-channels: Should contain IIO channel specifiers
+	for each element in io-channel-names.
+- io-channel-names: Should contain the following values:
+ * "temp" - The ADC channel for temperature reading
+ * "ichg" - The ADC channel for battery charging status
+ * "vbat" - The ADC channel to measure the battery voltage
+
+Example:
+	madc-battery {
+		compatible = "ti,twl4030-madc-battery";
+		capacity-uah = <1200000>;
+		ti,volt-to-capacity-charging-map = <4200 100>,
+					        <4100 75>,
+					        <4000 55>,
+					        <3900 25>,
+					        <3800 5>,
+					        <3700 2>,
+					        <3600 1>,
+					        <3300 0>;
+
+		ti,volt-to-capacity-discharging-map = <4200 100>
+						   <4100 95>,
+						   <4000 70>,
+						   <3800 50>,
+						   <3700 10>,
+						   <3600 5>,
+						   <3300 0>;
+		io-channels = <&twl_madc 1>,
+	                      <&twl_madc 10>,
+			      <&twl_madc 12>;
+		io-channel-names = "temp",
+		                   "ichg",
+		                   "vbat";
+	};