Skip to contentSkip to conversation

Convert rx51-battery to IIO API and add DT support

PATCH In review Sebastian Reichel 6 participants 29 messages Feb 26, 2014 – Jun 14, 2014

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Convert rx51-battery to IIO API and add DT support

Hi,

This is PATCHv1 for converting rx51-battery to the IIO API and adding DT support. The patchset compiles and has been tested on my Nokia N900. It depends on another patchset converting twl4030-madc to the IIO API:

https://lkml.org/lkml/2014/2/25/627

-- Sebastian

Sebastian Reichel (2):
rx51_battery: convert to iio consumer
Documentation: DT: Document rx51-battery binding

 .../devicetree/bindings/power/rx51-battery.txt     | 25 ++++++++
 drivers/power/rx51_battery.c                       | 68 ++++++++++++++--------
 2 files changed, 70 insertions(+), 23 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/power/rx51-battery.txt
Signature · 1 line
1.8.5.3
#1 lore raw

PATCH 1/2 rx51_battery: convert to iio consumer

Update rx51-battery driver to use the new IIO API of twl4030-madc and add DT support.

---
 drivers/power/rx51_battery.c | 68 +++++++++++++++++++++++++++++---------------
 1 file changed, 45 insertions(+), 23 deletions(-)
drivers/power/rx51_battery.c +45 -23
index 1bc5857..f7cb58e 100644
@@ -24,34 +24,27 @@
 #include <linux/power_supply.h>
 #include <linux/slab.h>
 #include <linux/i2c/twl4030-madc.h>
-
-/* RX51 specific channels */
-#define TWL4030_MADC_BTEMP_RX51	TWL4030_MADC_ADCIN0
-#define TWL4030_MADC_BCI_RX51	TWL4030_MADC_ADCIN4
+#include <linux/iio/consumer.h>
+#include <linux/of.h>
 
 struct rx51_device_info {
 	struct device *dev;
 	struct power_supply bat;
+	struct iio_channel *channel_temp;
+	struct iio_channel *channel_bsi;
+	struct iio_channel *channel_vbat;
 };
 
 /*
  * Read ADCIN channel value, code copied from maemo kernel
  */
-static int rx51_battery_read_adc(int channel)
+static int rx51_battery_read_adc(struct iio_channel *channel)
 {
-	struct twl4030_madc_request req;
-
-	req.channels = channel;
-	req.do_avg = 1;
-	req.method = TWL4030_MADC_SW1;
-	req.func_cb = NULL;
-	req.type = TWL4030_MADC_WAIT;
-	req.raw = true;
-
-	if (twl4030_madc_conversion(&req) <= 0)
-		return -ENODATA;
-
-	return req.rbuf[ffs(channel) - 1];
+	int val, err;
+	err = iio_read_channel_average_raw(channel, &val);
+	if (err < 0)
+		return err;
+	return val;
 }
 
 /*
@@ -60,10 +53,12 @@ static int rx51_battery_read_adc(int channel)
  */
 static int rx51_battery_read_voltage(struct rx51_device_info *di)
 {
-	int voltage = rx51_battery_read_adc(TWL4030_MADC_VBAT);
+	int voltage = rx51_battery_read_adc(di->channel_vbat);
 
-	if (voltage < 0)
+	if (voltage < 0) {
+		dev_err(di->dev, "Could not read ADC: %d\n", voltage);
 		return voltage;
+	}
 
 	return 1000 * (10000 * voltage / 1705);
 }
@@ -112,7 +107,10 @@ static int rx51_battery_read_temperature(struct rx51_device_info *di)
 {
 	int min = 0;
 	int max = ARRAY_SIZE(rx51_temp_table2) - 1;
-	int raw = rx51_battery_read_adc(TWL4030_MADC_BTEMP_RX51);
+	int raw = rx51_battery_read_adc(di->channel_temp);
+
+	if (raw < 0)
+		dev_err(di->dev, "Could not read ADC: %d\n", raw);
 
 	/* Zero and negative values are undefined */
 	if (raw <= 0)
@@ -146,10 +144,12 @@ static int rx51_battery_read_temperature(struct rx51_device_info *di)
  */
 static int rx51_battery_read_capacity(struct rx51_device_info *di)
 {
-	int capacity = rx51_battery_read_adc(TWL4030_MADC_BCI_RX51);
+	int capacity = rx51_battery_read_adc(di->channel_bsi);
 
-	if (capacity < 0)
+	if (capacity < 0) {
+		dev_err(di->dev, "Could not read ADC: %d\n", capacity);
 		return capacity;
+	}
 
 	return 1280 * (1200 * capacity)/(1024 - capacity);
 }
@@ -213,12 +213,25 @@ static int rx51_battery_probe(struct platform_device *pdev)
 
 	platform_set_drvdata(pdev, di);
 
+	di->dev = &pdev->dev;
 	di->bat.name = dev_name(&pdev->dev);
 	di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
 	di->bat.properties = rx51_battery_props;
 	di->bat.num_properties = ARRAY_SIZE(rx51_battery_props);
 	di->bat.get_property = rx51_battery_get_property;
 
+	di->channel_temp = iio_channel_get(di->dev, "temp");
+	if (IS_ERR(di->channel_temp))
+		return PTR_ERR(di->channel_temp);
+
+	di->channel_bsi  = iio_channel_get(di->dev, "bsi");
+	if (IS_ERR(di->channel_bsi))
+		return PTR_ERR(di->channel_bsi);
+
+	di->channel_vbat = iio_channel_get(di->dev, "vbat");
+	if (IS_ERR(di->channel_vbat))
+		return PTR_ERR(di->channel_vbat);
+
 	ret = power_supply_register(di->dev, &di->bat);
 	if (ret)
 		return ret;
@@ -235,12 +248,21 @@ static int rx51_battery_remove(struct platform_device *pdev)
 	return 0;
 }
 
+#ifdef CONFIG_OF
+static const struct of_device_id n900_battery_of_match[] = {
+	{.compatible = "nokia,n900-battery", },
+	{ },
+};
+MODULE_DEVICE_TABLE(of, n900_battery_of_match);
+#endif
+
 static struct platform_driver rx51_battery_driver = {
 	.probe = rx51_battery_probe,
 	.remove = rx51_battery_remove,
 	.driver = {
 		.name = "rx51-battery",
 		.owner = THIS_MODULE,
+		.of_match_table = of_match_ptr(n900_battery_of_match),
 	},
 };
 module_platform_driver(rx51_battery_driver);
Signature · 1 line
1.8.5.3
#2 lore raw

Re: [PATCHv1 1/2] rx51_battery: convert to iio consumer

Hi Sebastian,

Quoted 42 lines · Sebastian Reichel

Update rx51-battery driver to use the new IIO API of twl4030-madc and add DT support.

---
 drivers/power/rx51_battery.c | 68 +++++++++++++++++++++++++++++---------------
 1 file changed, 45 insertions(+), 23 deletions(-)
drivers/power/rx51_battery.c +6 -10
index 1bc5857..f7cb58e 100644
@@ -24,34 +24,27 @@
 #include <linux/power_supply.h>
 #include <linux/slab.h>
 #include <linux/i2c/twl4030-madc.h>
-
-/* RX51 specific channels */
-#define TWL4030_MADC_BTEMP_RX51        TWL4030_MADC_ADCIN0
-#define TWL4030_MADC_BCI_RX51  TWL4030_MADC_ADCIN4
+#include <linux/iio/consumer.h>
+#include <linux/of.h>
 
 struct rx51_device_info {
        struct device *dev;
        struct power_supply bat;
+       struct iio_channel *channel_temp;
+       struct iio_channel *channel_bsi;
+       struct iio_channel *channel_vbat;
 };
 
 /*
  * Read ADCIN channel value, code copied from maemo kernel
  */
-static int rx51_battery_read_adc(int channel)
+static int rx51_battery_read_adc(struct iio_channel *channel)
 {
-       struct twl4030_madc_request req;
-
-       req.channels = channel;
-       req.do_avg = 1;
-       req.method = TWL4030_MADC_SW1;

drivers/power/rx51_battery.c line 44

Quoted 10 lines
drivers/power/rx51_battery.c +2 -8
-       req.func_cb = NULL;
-       req.type = TWL4030_MADC_WAIT;
-       req.raw = true;
-
-       if (twl4030_madc_conversion(&req) <= 0)
-               return -ENODATA;
-
-       return req.rbuf[ffs(channel) - 1];
+       int val, err;
+       err = iio_read_channel_average_raw(channel, &val);

Where this function comes from? I cannot find it in current linux-next (only iio_read_channel_raw()).
Am I missing some patches? Thx. BTW when I convert to iio consumer and use DT some of values work
but some of them just report 0 :( (I don't have latest twl4030-madc iio patches).

Quoted 97 lines
drivers/power/rx51_battery.c +37 -6
+       if (err < 0)
+               return err;
+       return val;
 }
 
 /*
@@ -60,10 +53,12 @@ static int rx51_battery_read_adc(int channel)
  */
 static int rx51_battery_read_voltage(struct rx51_device_info *di)
 {
-       int voltage = rx51_battery_read_adc(TWL4030_MADC_VBAT);
+       int voltage = rx51_battery_read_adc(di->channel_vbat);
 
-       if (voltage < 0)
+       if (voltage < 0) {
+               dev_err(di->dev, "Could not read ADC: %d\n", voltage);
                return voltage;
+       }
 
        return 1000 * (10000 * voltage / 1705);
 }
@@ -112,7 +107,10 @@ static int rx51_battery_read_temperature(struct rx51_device_info *di)
 {
        int min = 0;
        int max = ARRAY_SIZE(rx51_temp_table2) - 1;
-       int raw = rx51_battery_read_adc(TWL4030_MADC_BTEMP_RX51);
+       int raw = rx51_battery_read_adc(di->channel_temp);
+
+       if (raw < 0)
+               dev_err(di->dev, "Could not read ADC: %d\n", raw);
 
        /* Zero and negative values are undefined */
        if (raw <= 0)
@@ -146,10 +144,12 @@ static int rx51_battery_read_temperature(struct rx51_device_info *di)
  */
 static int rx51_battery_read_capacity(struct rx51_device_info *di)
 {
-       int capacity = rx51_battery_read_adc(TWL4030_MADC_BCI_RX51);
+       int capacity = rx51_battery_read_adc(di->channel_bsi);
 
-       if (capacity < 0)
+       if (capacity < 0) {
+               dev_err(di->dev, "Could not read ADC: %d\n", capacity);
                return capacity;
+       }
 
        return 1280 * (1200 * capacity)/(1024 - capacity);
 }
@@ -213,12 +213,25 @@ static int rx51_battery_probe(struct platform_device *pdev)
 
        platform_set_drvdata(pdev, di);
 
+       di->dev = &pdev->dev;
        di->bat.name = dev_name(&pdev->dev);
        di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
        di->bat.properties = rx51_battery_props;
        di->bat.num_properties = ARRAY_SIZE(rx51_battery_props);
        di->bat.get_property = rx51_battery_get_property;
 
+       di->channel_temp = iio_channel_get(di->dev, "temp");
+       if (IS_ERR(di->channel_temp))
+               return PTR_ERR(di->channel_temp);
+
+       di->channel_bsi  = iio_channel_get(di->dev, "bsi");
+       if (IS_ERR(di->channel_bsi))
+               return PTR_ERR(di->channel_bsi);
+
+       di->channel_vbat = iio_channel_get(di->dev, "vbat");
+       if (IS_ERR(di->channel_vbat))
+               return PTR_ERR(di->channel_vbat);
+
        ret = power_supply_register(di->dev, &di->bat);
        if (ret)
                return ret;
@@ -235,12 +248,21 @@ static int rx51_battery_remove(struct platform_device *pdev)
        return 0;
 }
 
+#ifdef CONFIG_OF
+static const struct of_device_id n900_battery_of_match[] = {
+       {.compatible = "nokia,n900-battery", },
+       { },
+};
+MODULE_DEVICE_TABLE(of, n900_battery_of_match);
+#endif
+
 static struct platform_driver rx51_battery_driver = {
        .probe = rx51_battery_probe,
        .remove = rx51_battery_remove,
        .driver = {
                .name = "rx51-battery",
                .owner = THIS_MODULE,
+               .of_match_table = of_match_ptr(n900_battery_of_match),
        },
 };
 module_platform_driver(rx51_battery_driver);
--
Quoted 2 lines

1.8.5.3

BR,

marek

Signature · 14 lines
as simple and primitive as possible
-------------------------------------------------
Marek Belisko - OPEN-NANDRA
Freelance Developer

Ruska Nova Ves 219 | Presov, 08005 Slovak Republic
Tel: +421 915 052 184
skype: marekwhite
twitter: #opennandra
web: http://open-nandra.com
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#3 lore raw

Re: [PATCHv1 1/2] rx51_battery: convert to iio consumer

Hi,

Quoted 5 lines · Belisko Marek

[...]

drivers/power/rx51_battery.c line 44

Quoted 2 lines
drivers/power/rx51_battery.c +2 -0
+       int val, err;
+       err = iio_read_channel_average_raw(channel, &val);

Where this function comes from? I cannot find it in current linux-next (only iio_read_channel_raw()). Am I missing some patches? Thx.

Ah right. I planned to send a patch adding this function together with the rx51-battery patchset, but it seems I forgot to include it.

Sorry for the inconvenience. I will send it out as a separate patch now.

Quoted 3 lines

BTW when I convert to iio consumer and use DT some of values work but some of them just report 0 :( (I don't have latest twl4030-madc iio patches).

Can you retry with the twl4030-madc iio patch from today? The older patchsets, which do not contain the "tested on real hw" note are slightly broken.

-- Sebastian

#4 lore raw

Re: [PATCHv1 1/2] rx51_battery: convert to iio consumer

Hi Sebastian,

Quoted 15 lines · Sebastian Reichel

Hi,

Quoted 5 lines · Belisko Marek

[...]

drivers/power/rx51_battery.c line 44

Quoted 2 lines
drivers/power/rx51_battery.c +2 -0
+       int val, err;
+       err = iio_read_channel_average_raw(channel, &val);

Where this function comes from? I cannot find it in current linux-next (only iio_read_channel_raw()). Am I missing some patches? Thx.

Ah right. I planned to send a patch adding this function together with the rx51-battery patchset, but it seems I forgot to include it.

Sorry for the inconvenience. I will send it out as a separate patch now.

Quoted 7 lines
Quoted 3 lines

BTW when I convert to iio consumer and use DT some of values work but some of them just report 0 :( (I don't have latest twl4030-madc iio patches).

Can you retry with the twl4030-madc iio patch from today? The older patchsets, which do not contain the "tested on real hw" note are slightly broken.

Well I've tried and it's worse :). I got during booting:

[    2.218383] ERROR: could not get IIO channel /battery:temp(0)
[    2.224639] platform battery.4: Driver twl4030_madc_battery

requests probe deferral
Not sure if it's just error or warning but temp is always reported as 0 (and also other values in sysfs).

My patches ca be found here:
https://patchwork.kernel.org/patch/3735981/
https://patchwork.kernel.org/patch/3735961/
https://patchwork.kernel.org/patch/3735941/

Quoted 2 lines

-- Sebastian

BR,

marek

Signature · 10 lines
as simple and primitive as possible
-------------------------------------------------
Marek Belisko - OPEN-NANDRA
Freelance Developer

Ruska Nova Ves 219 | Presov, 08005 Slovak Republic
Tel: +421 915 052 184
skype: marekwhite
twitter: #opennandra
web: http://open-nandra.com
#5 lore raw

Re: [PATCHv1 1/2] rx51_battery: convert to iio consumer

Quoted 6 lines · Belisko Marek

Well I've tried and it's worse :). I got during booting:

[    2.218383] ERROR: could not get IIO channel /battery:temp(0)
[    2.224639] platform battery.4: Driver twl4030_madc_battery

requests probe deferral
Not sure if it's just error or warning but temp is always reported as 0 (and also other values in sysfs).

This is an error, which basically means, that twl4030-madc has not yet been loaded. Do you get proper values when you use the old madc API with the patchset applied?

-- Sebastian

#6 lore raw

Re: [PATCHv1 1/2] rx51_battery: convert to iio consumer

Hi Sebastian,

Quoted 11 lines · Sebastian Reichel
Quoted 6 lines · Belisko Marek

Well I've tried and it's worse :). I got during booting:

[    2.218383] ERROR: could not get IIO channel /battery:temp(0)
[    2.224639] platform battery.4: Driver twl4030_madc_battery

requests probe deferral
Not sure if it's just error or warning but temp is always reported as 0 (and also other values in sysfs).

This is an error, which basically means, that twl4030-madc has not yet been loaded. Do you get proper values when you use the old madc API with the patchset applied?

It works without converting to iio consumer (at least I get some reasonable values).
With conversion it fails with above error. I recheck (add printk to iio twl4030-madc) that
madc driver is loaded. Could this be that twl4030_madc_battery is loaded earlier then
twl4030_madc and than it fails to get iio channels?

Quoted 2 lines

-- Sebastian

BR,

marek

Signature · 10 lines
as simple and primitive as possible
-------------------------------------------------
Marek Belisko - OPEN-NANDRA
Freelance Developer

Ruska Nova Ves 219 | Presov, 08005 Slovak Republic
Tel: +421 915 052 184
skype: marekwhite
twitter: #opennandra
web: http://open-nandra.com
#7 lore raw

Re: [PATCHv1 1/2] rx51_battery: convert to iio consumer

Quoted 14 lines · Belisko Marek

Hi Sebastian,

Quoted 11 lines · Sebastian Reichel
Quoted 6 lines · Belisko Marek

Well I've tried and it's worse :). I got during booting:

[    2.218383] ERROR: could not get IIO channel /battery:temp(0)
[    2.224639] platform battery.4: Driver twl4030_madc_battery

requests probe deferral
Not sure if it's just error or warning but temp is always reported as 0 (and also other values in sysfs).

This is an error, which basically means, that twl4030-madc has not yet been loaded. Do you get proper values when you use the old madc API with the patchset applied?

Quoted 7 lines

It works without converting to iio consumer (at least I get some reasonable values).
With conversion it fails with above error. I recheck (add printk to iio twl4030-madc) that
madc driver is loaded. Could this be that twl4030_madc_battery is loaded earlier then
twl4030_madc and than it fails to get iio channels?

Hmm I wasn't far away from truth in previous email. I think that channel doesn't exists because twl4030_madc driver isn't loaded yet and that is the reason for deferral probe
message in log. So it seems after some time it is loaded correctly but not working. No ideas.

Quoted 13 lines
Quoted 2 lines

-- Sebastian

BR,

marek

--
as simple and primitive as possible
-------------------------------------------------
Marek Belisko - OPEN-NANDRA
Freelance Developer

Quoted 5 lines

Ruska Nova Ves 219 | Presov, 08005 Slovak Republic
Tel: +421 915 052 184
skype: marekwhite
twitter: #opennandra
web: http://open-nandra.com

BR,

marek

Signature · 10 lines
as simple and primitive as possible
-------------------------------------------------
Marek Belisko - OPEN-NANDRA
Freelance Developer

Ruska Nova Ves 219 | Presov, 08005 Slovak Republic
Tel: +421 915 052 184
skype: marekwhite
twitter: #opennandra
web: http://open-nandra.com
#8 lore raw

Re: [PATCHv1 1/2] rx51_battery: convert to iio consumer

In reply to Belisko Marek
Quoted 13 lines · Belisko Marek
Quoted 11 lines · Sebastian Reichel
Quoted 6 lines · Belisko Marek

Well I've tried and it's worse :). I got during booting:

[    2.218383] ERROR: could not get IIO channel /battery:temp(0)
[    2.224639] platform battery.4: Driver twl4030_madc_battery

requests probe deferral
Not sure if it's just error or warning but temp is always reported as 0 (and also other values in sysfs).

This is an error, which basically means, that twl4030-madc has not yet been loaded. Do you get proper values when you use the old madc API with the patchset applied?

Quoted 5 lines

It works without converting to iio consumer (at least I get some reasonable values). With conversion it fails with above error. I recheck (add printk to iio twl4030-madc) that madc driver is loaded. Could this be that twl4030_madc_battery is loaded earlier then twl4030_madc and than it fails to get iio channels?

The error above implies, that twl4030-madc has not been loaded when twl4030-madc-battery was loaded. This iio_channel_get() fails and returns -EPROBE_DEFER. This results in twl4030-madc-battery probe function returning -EPROBE_DEFER. Thus you can simply ignore the error if the twl4030-madc-battery driver is loaded later.

I guess the easiest way to debug the problem is adding some dev_dbg() at the start of twl4030_madc_conversion(), which prints out the entries of twl4030_madc_request. Currently the IIO API simply calls twl4030_madc_request(), so you should be able to find out the difference.

Also: Can you post you DTS? I use the following for Nokia N900:

/ {
	battery: n900-battery {
		compatible = "nokia,n900-battery";
		io-channels = <&twl_madc 0>, <&twl_madc 4>, <&twl_madc 12>;
		io-channel-names = "temp", "bsi", "vbat";
	};
};
&twl {
	twl_madc: madc {
		compatible = "ti,twl4030-madc";
		interrupts = <3>;
		#io-channel-cells = <1>;
	};
};

-- Sebastian

#9 lore raw

Re: [PATCHv1 1/2] rx51_battery: convert to iio consumer

Quoted 26 lines · Sebastian Reichel
Quoted 13 lines · Belisko Marek
Quoted 11 lines · Sebastian Reichel
Quoted 6 lines · Belisko Marek

Well I've tried and it's worse :). I got during booting:

[    2.218383] ERROR: could not get IIO channel /battery:temp(0)
[    2.224639] platform battery.4: Driver twl4030_madc_battery

requests probe deferral
Not sure if it's just error or warning but temp is always reported as 0 (and also other values in sysfs).

This is an error, which basically means, that twl4030-madc has not yet been loaded. Do you get proper values when you use the old madc API with the patchset applied?

Quoted 5 lines

It works without converting to iio consumer (at least I get some reasonable values). With conversion it fails with above error. I recheck (add printk to iio twl4030-madc) that madc driver is loaded. Could this be that twl4030_madc_battery is loaded earlier then twl4030_madc and than it fails to get iio channels?

The error above implies, that twl4030-madc has not been loaded when twl4030-madc-battery was loaded. This iio_channel_get() fails and returns -EPROBE_DEFER. This results in twl4030-madc-battery probe function returning -EPROBE_DEFER. Thus you can simply ignore the error if the twl4030-madc-battery driver is loaded later.

Quoted 5 lines

I guess the easiest way to debug the problem is adding some dev_dbg() at the start of twl4030_madc_conversion(), which prints out the entries of twl4030_madc_request. Currently the IIO API simply calls twl4030_madc_request(), so you should be able to find out the difference.

OK I'll try to debug it deeper.

Quoted 11 lines

Also: Can you post you DTS? I use the following for Nokia N900:

/ {
        battery: n900-battery {
                compatible = "nokia,n900-battery";
                io-channels = <&twl_madc 0>, <&twl_madc 4>, <&twl_madc 12>;
                io-channel-names = "temp", "bsi", "vbat";
        };
};
Quoted 7 lines
&twl {
        twl_madc: madc {
                compatible = "ti,twl4030-madc";
                interrupts = <3>;
                #io-channel-cells = <1>;
        };
};

I put twl_madc property to twl4030.dtsi as following:
twl_madc: madc {
compatible = "ti,twl4030-madc";
interrupts = <3>;
#io-channel-cells = <1>;
};

and node for battery is:
battery {
compatible = "ti,twl4030-madc-battery";
capacity = <1200000>;
charging-calibration-data = <4200 100

    4100 75
    4000 55
    3900 25
    3800 5
    3700 2
    3600 1
    3300 0>;

discharging-calibration-data = <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";
};
Quoted 2 lines

-- Sebastian

BR,

marek

Signature · 10 lines
as simple and primitive as possible
-------------------------------------------------
Marek Belisko - OPEN-NANDRA
Freelance Developer

Ruska Nova Ves 219 | Presov, 08005 Slovak Republic
Tel: +421 915 052 184
skype: marekwhite
twitter: #opennandra
web: http://open-nandra.com
#10 lore raw

Re: [PATCHv1 1/2] rx51_battery: convert to iio consumer

Quoted 27 lines · Belisko Marek
Quoted 26 lines · Sebastian Reichel
Quoted 13 lines · Belisko Marek
Quoted 11 lines · Sebastian Reichel
Quoted 6 lines · Belisko Marek

Well I've tried and it's worse :). I got during booting:

[    2.218383] ERROR: could not get IIO channel /battery:temp(0)
[    2.224639] platform battery.4: Driver twl4030_madc_battery

requests probe deferral
Not sure if it's just error or warning but temp is always reported as 0 (and also other values in sysfs).

This is an error, which basically means, that twl4030-madc has not yet been loaded. Do you get proper values when you use the old madc API with the patchset applied?

Quoted 5 lines

It works without converting to iio consumer (at least I get some reasonable values). With conversion it fails with above error. I recheck (add printk to iio twl4030-madc) that madc driver is loaded. Could this be that twl4030_madc_battery is loaded earlier then twl4030_madc and than it fails to get iio channels?

The error above implies, that twl4030-madc has not been loaded when twl4030-madc-battery was loaded. This iio_channel_get() fails and returns -EPROBE_DEFER. This results in twl4030-madc-battery probe function returning -EPROBE_DEFER. Thus you can simply ignore the error if the twl4030-madc-battery driver is loaded later.

Quoted 6 lines
Quoted 5 lines

I guess the easiest way to debug the problem is adding some dev_dbg() at the start of twl4030_madc_conversion(), which prints out the entries of twl4030_madc_request. Currently the IIO API simply calls twl4030_madc_request(), so you should be able to find out the difference.

OK I'll try to debug it deeper.

Seems I found issue. I have missing property ti,system-uses-second-madc-irq as original twl4030_madc_battery used SW2 but this change doesn't fix completely the problem. What it fixed completely is change:
- req.raw = !(mask == IIO_CHAN_INFO_PROCESSED);
+ req.raw = 0;//!(mask == IIO_CHAN_INFO_PROCESSED);

Quoted 64 lines
Quoted 11 lines

Also: Can you post you DTS? I use the following for Nokia N900:

/ {
        battery: n900-battery {
                compatible = "nokia,n900-battery";
                io-channels = <&twl_madc 0>, <&twl_madc 4>, <&twl_madc 12>;
                io-channel-names = "temp", "bsi", "vbat";
        };
};
Quoted 7 lines
&twl {
        twl_madc: madc {
                compatible = "ti,twl4030-madc";
                interrupts = <3>;
                #io-channel-cells = <1>;
        };
};

I put twl_madc property to twl4030.dtsi as following:
twl_madc: madc {
compatible = "ti,twl4030-madc";
interrupts = <3>;
#io-channel-cells = <1>;
};

and node for battery is:
battery {
compatible = "ti,twl4030-madc-battery";
capacity = <1200000>;
charging-calibration-data = <4200 100

    4100 75
    4000 55
    3900 25
    3800 5
    3700 2
    3600 1
    3300 0>;

discharging-calibration-data = <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";
};
Quoted 2 lines

-- Sebastian

BR,

marek

--
as simple and primitive as possible
-------------------------------------------------
Marek Belisko - OPEN-NANDRA
Freelance Developer

Quoted 5 lines

Ruska Nova Ves 219 | Presov, 08005 Slovak Republic
Tel: +421 915 052 184
skype: marekwhite
twitter: #opennandra
web: http://open-nandra.com

BR,

marek

Signature · 10 lines
as simple and primitive as possible
-------------------------------------------------
Marek Belisko - OPEN-NANDRA
Freelance Developer

Ruska Nova Ves 219 | Presov, 08005 Slovak Republic
Tel: +421 915 052 184
skype: marekwhite
twitter: #opennandra
web: http://open-nandra.com
#11 lore raw

Re: [PATCHv1 1/2] rx51_battery: convert to iio consumer

Hi Marek,

Quoted 3 lines · Belisko Marek

Seems I found issue. I have missing property ti,system-uses-second-madc-irq as original twl4030_madc_battery used SW2 but this change doesn't fix completely the problem.

I remember adding this property because you requested it :) Good to know, that its really needed.

Review

Quoted 3 lines

What it fixed completely is change:

(quoted diff) +1 -1
-       req.raw = !(mask == IIO_CHAN_INFO_PROCESSED);
+       req.raw = 0;//!(mask == IIO_CHAN_INFO_PROCESSED);

ok, that figures it. Instead of changing the twl4030-madc driver you should change the twl4030-madc-battery driver. You currently call iio_read_channel_raw(), which gives you raw values. Your driver wants processed data from twl4030-madc, so you should use iio_read_channel_processed() instead.

-- Sebastian

#12 lore raw

Re: [PATCHv1 1/2] rx51_battery: convert to iio consumer

Hi Sebastian,

Quoted 14 lines · Sebastian Reichel

Hi Marek,

Quoted 3 lines · Belisko Marek

Seems I found issue. I have missing property ti,system-uses-second-madc-irq as original twl4030_madc_battery used SW2 but this change doesn't fix completely the problem.

I remember adding this property because you requested it :) Good to know, that its really needed.

Review

Quoted 3 lines

What it fixed completely is change:

(quoted diff) +1 -1
-       req.raw = !(mask == IIO_CHAN_INFO_PROCESSED);
+       req.raw = 0;//!(mask == IIO_CHAN_INFO_PROCESSED);
Quoted 5 lines

ok, that figures it. Instead of changing the twl4030-madc driver you should change the twl4030-madc-battery driver. You currently call iio_read_channel_raw(), which gives you raw values. Your driver wants processed data from twl4030-madc, so you should use iio_read_channel_processed() instead.

Yes this helps but also not completely :(. twl4030_madc_battery using chanel 1, 10, 12. Channel 1 and 10 have flag IIO_CHAN_INFO_PROCESSED set but channel 12 not and values from channel 12 are read as raw which is not what I want. Can I simply add IIO_CHAN_INFO_PROCESSED also to channel 12?

Quoted 2 lines

-- Sebastian

BR,

marek

Signature · 10 lines
as simple and primitive as possible
-------------------------------------------------
Marek Belisko - OPEN-NANDRA
Freelance Developer

Ruska Nova Ves 219 | Presov, 08005 Slovak Republic
Tel: +421 915 052 184
skype: marekwhite
twitter: #opennandra
web: http://open-nandra.com
#13 lore raw

Re: [PATCHv1 1/2] rx51_battery: convert to iio consumer

Hi,

Quoted 18 lines · Belisko Marek
Quoted 12 lines · Sebastian Reichel
Quoted 3 lines · Belisko Marek

Seems I found issue. I have missing property ti,system-uses-second-madc-irq as original twl4030_madc_battery used SW2 but this change doesn't fix completely the problem.

I remember adding this property because you requested it :) Good to know, that its really needed.

Review

Quoted 3 lines

What it fixed completely is change:

(quoted diff) +1 -1
-       req.raw = !(mask == IIO_CHAN_INFO_PROCESSED);
+       req.raw = 0;//!(mask == IIO_CHAN_INFO_PROCESSED);
Quoted 5 lines

ok, that figures it. Instead of changing the twl4030-madc driver you should change the twl4030-madc-battery driver. You currently call iio_read_channel_raw(), which gives you raw values. Your driver wants processed data from twl4030-madc, so you should use iio_read_channel_processed() instead.

Quoted 4 lines

Yes this helps but also not completely :(. twl4030_madc_battery using chanel 1, 10, 12. Channel 1 and 10 have flag IIO_CHAN_INFO_PROCESSED set but channel 12 not and values from channel 12 are read as raw which is not what I want.

mh. I should have added IIO_CHAN_INFO_PROCESSED for all channels and not just for current & temperature special cases.

I will send an updated patchset.

Quoted 1 line

Can I simply add IIO_CHAN_INFO_PROCESSED also to channel 12?

Yes, but if you wait some minutes you can use my updated twl4030-madc patchset. This way you can add your Tested-by once its working.

-- Sebastian

maintainer
#14 lore raw

Re: [PATCHv1 1/2] rx51_battery: convert to iio consumer

In reply to Sebastian Reichel
Quoted 4 lines · Sebastian Reichel

Update rx51-battery driver to use the new IIO API of twl4030-madc and add DT support.

The error handling needs tidying up.
Otherwise this looks fine to me. Note that you (really me) may get some grief over the DT bindings. Theoretically we have been planning to rewrite those entirely for some time...

Quoted 104 lines
---
  drivers/power/rx51_battery.c | 68 +++++++++++++++++++++++++++++---------------
  1 file changed, 45 insertions(+), 23 deletions(-)
drivers/power/rx51_battery.c +24 -23
index 1bc5857..f7cb58e 100644
@@ -24,34 +24,27 @@
  #include <linux/power_supply.h>
  #include <linux/slab.h>
  #include <linux/i2c/twl4030-madc.h>
-
-/* RX51 specific channels */
-#define TWL4030_MADC_BTEMP_RX51	TWL4030_MADC_ADCIN0
-#define TWL4030_MADC_BCI_RX51	TWL4030_MADC_ADCIN4
+#include <linux/iio/consumer.h>
+#include <linux/of.h>
 
  struct rx51_device_info {
  	struct device *dev;
  	struct power_supply bat;
+	struct iio_channel *channel_temp;
+	struct iio_channel *channel_bsi;
+	struct iio_channel *channel_vbat;
  };
 
  /*
   * Read ADCIN channel value, code copied from maemo kernel
   */
-static int rx51_battery_read_adc(int channel)
+static int rx51_battery_read_adc(struct iio_channel *channel)
  {
-	struct twl4030_madc_request req;
-
-	req.channels = channel;
-	req.do_avg = 1;
-	req.method = TWL4030_MADC_SW1;
-	req.func_cb = NULL;
-	req.type = TWL4030_MADC_WAIT;
-	req.raw = true;
-
-	if (twl4030_madc_conversion(&req) <= 0)
-		return -ENODATA;
-
-	return req.rbuf[ffs(channel) - 1];
+	int val, err;
+	err = iio_read_channel_average_raw(channel, &val);
+	if (err < 0)
+		return err;
+	return val;
  }
 
  /*
@@ -60,10 +53,12 @@ static int rx51_battery_read_adc(int channel)
   */
  static int rx51_battery_read_voltage(struct rx51_device_info *di)
  {
-	int voltage = rx51_battery_read_adc(TWL4030_MADC_VBAT);
+	int voltage = rx51_battery_read_adc(di->channel_vbat);
 
-	if (voltage < 0)
+	if (voltage < 0) {
+		dev_err(di->dev, "Could not read ADC: %d\n", voltage);
  		return voltage;
+	}
 
  	return 1000 * (10000 * voltage / 1705);
  }
@@ -112,7 +107,10 @@ static int rx51_battery_read_temperature(struct rx51_device_info *di)
  {
  	int min = 0;
  	int max = ARRAY_SIZE(rx51_temp_table2) - 1;
-	int raw = rx51_battery_read_adc(TWL4030_MADC_BTEMP_RX51);
+	int raw = rx51_battery_read_adc(di->channel_temp);
+
+	if (raw < 0)
+		dev_err(di->dev, "Could not read ADC: %d\n", raw);
 
  	/* Zero and negative values are undefined */
  	if (raw <= 0)
@@ -146,10 +144,12 @@ static int rx51_battery_read_temperature(struct rx51_device_info *di)
   */
  static int rx51_battery_read_capacity(struct rx51_device_info *di)
  {
-	int capacity = rx51_battery_read_adc(TWL4030_MADC_BCI_RX51);
+	int capacity = rx51_battery_read_adc(di->channel_bsi);
 
-	if (capacity < 0)
+	if (capacity < 0) {
+		dev_err(di->dev, "Could not read ADC: %d\n", capacity);
  		return capacity;
+	}
 
  	return 1280 * (1200 * capacity)/(1024 - capacity);
  }
@@ -213,12 +213,25 @@ static int rx51_battery_probe(struct platform_device *pdev)
 
  	platform_set_drvdata(pdev, di);
 
+	di->dev = &pdev->dev;
  	di->bat.name = dev_name(&pdev->dev);
  	di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
  	di->bat.properties = rx51_battery_props;

drivers/power/rx51_battery.c static int rx51_battery_probe(struct platform_device *pdev) · line 229

Quoted 10 lines
drivers/power/rx51_battery.c +7 -0
  	di->bat.num_properties = ARRAY_SIZE(rx51_battery_props);
  	di->bat.get_property = rx51_battery_get_property;
 
+	di->channel_temp = iio_channel_get(di->dev, "temp");
+	if (IS_ERR(di->channel_temp))
+		return PTR_ERR(di->channel_temp);
+
+	di->channel_bsi  = iio_channel_get(di->dev, "bsi");
+	if (IS_ERR(di->channel_bsi))
+		return PTR_ERR(di->channel_bsi);

You need to unwind the iio_channel_get that did succeed if we get here. Otherwise references to the iio driver will still be held despite this driver failing to probe.

Quoted 31 lines
drivers/power/rx51_battery.c +14 -0
+
+	di->channel_vbat = iio_channel_get(di->dev, "vbat");
+	if (IS_ERR(di->channel_vbat))
+		return PTR_ERR(di->channel_vbat);
+
  	ret = power_supply_register(di->dev, &di->bat);
  	if (ret)
  		return ret;
@@ -235,12 +248,21 @@ static int rx51_battery_remove(struct platform_device *pdev)
  	return 0;
  }
 
+#ifdef CONFIG_OF
+static const struct of_device_id n900_battery_of_match[] = {
+	{.compatible = "nokia,n900-battery", },
+	{ },
+};
+MODULE_DEVICE_TABLE(of, n900_battery_of_match);
+#endif
+
  static struct platform_driver rx51_battery_driver = {
  	.probe = rx51_battery_probe,
  	.remove = rx51_battery_remove,
  	.driver = {
  		.name = "rx51-battery",
  		.owner = THIS_MODULE,
+		.of_match_table = of_match_ptr(n900_battery_of_match),
  	},
  };
  module_platform_driver(rx51_battery_driver);
maintainer
#15 lore raw

Re: [PATCHv1 1/2] rx51_battery: convert to iio consumer

Quoted 9 lines · Jonathan Cameron
Quoted 4 lines · Sebastian Reichel

Update rx51-battery driver to use the new IIO API of twl4030-madc and add DT support.

The error handling needs tidying up.
Otherwise this looks fine to me. Note that you (really me) may get some grief over the DT bindings. Theoretically we have been planning to rewrite those entirely for some time...

It's a longer term point, but it does rather feel like it ought to be possible to write a generic battery driver sometime down the line where any conversion characteristics would be in the device tree. Definitely a question for the future though!

Quoted 154 lines
Quoted 104 lines
---
  drivers/power/rx51_battery.c | 68 +++++++++++++++++++++++++++++---------------
  1 file changed, 45 insertions(+), 23 deletions(-)
drivers/power/rx51_battery.c +24 -23
index 1bc5857..f7cb58e 100644
@@ -24,34 +24,27 @@
  #include <linux/power_supply.h>
  #include <linux/slab.h>
  #include <linux/i2c/twl4030-madc.h>
-
-/* RX51 specific channels */
-#define TWL4030_MADC_BTEMP_RX51    TWL4030_MADC_ADCIN0
-#define TWL4030_MADC_BCI_RX51    TWL4030_MADC_ADCIN4
+#include <linux/iio/consumer.h>
+#include <linux/of.h>
 
  struct rx51_device_info {
      struct device *dev;
      struct power_supply bat;
+    struct iio_channel *channel_temp;
+    struct iio_channel *channel_bsi;
+    struct iio_channel *channel_vbat;
  };
 
  /*
   * Read ADCIN channel value, code copied from maemo kernel
   */
-static int rx51_battery_read_adc(int channel)
+static int rx51_battery_read_adc(struct iio_channel *channel)
  {
-    struct twl4030_madc_request req;
-
-    req.channels = channel;
-    req.do_avg = 1;
-    req.method = TWL4030_MADC_SW1;
-    req.func_cb = NULL;
-    req.type = TWL4030_MADC_WAIT;
-    req.raw = true;
-
-    if (twl4030_madc_conversion(&req) <= 0)
-        return -ENODATA;
-
-    return req.rbuf[ffs(channel) - 1];
+    int val, err;
+    err = iio_read_channel_average_raw(channel, &val);
+    if (err < 0)
+        return err;
+    return val;
  }
 
  /*
@@ -60,10 +53,12 @@ static int rx51_battery_read_adc(int channel)
   */
  static int rx51_battery_read_voltage(struct rx51_device_info *di)
  {
-    int voltage = rx51_battery_read_adc(TWL4030_MADC_VBAT);
+    int voltage = rx51_battery_read_adc(di->channel_vbat);
 
-    if (voltage < 0)
+    if (voltage < 0) {
+        dev_err(di->dev, "Could not read ADC: %d\n", voltage);
          return voltage;
+    }
 
      return 1000 * (10000 * voltage / 1705);
  }
@@ -112,7 +107,10 @@ static int rx51_battery_read_temperature(struct rx51_device_info *di)
  {
      int min = 0;
      int max = ARRAY_SIZE(rx51_temp_table2) - 1;
-    int raw = rx51_battery_read_adc(TWL4030_MADC_BTEMP_RX51);
+    int raw = rx51_battery_read_adc(di->channel_temp);
+
+    if (raw < 0)
+        dev_err(di->dev, "Could not read ADC: %d\n", raw);
 
      /* Zero and negative values are undefined */
      if (raw <= 0)
@@ -146,10 +144,12 @@ static int rx51_battery_read_temperature(struct rx51_device_info *di)
   */
  static int rx51_battery_read_capacity(struct rx51_device_info *di)
  {
-    int capacity = rx51_battery_read_adc(TWL4030_MADC_BCI_RX51);
+    int capacity = rx51_battery_read_adc(di->channel_bsi);
 
-    if (capacity < 0)
+    if (capacity < 0) {
+        dev_err(di->dev, "Could not read ADC: %d\n", capacity);
          return capacity;
+    }
 
      return 1280 * (1200 * capacity)/(1024 - capacity);
  }
@@ -213,12 +213,25 @@ static int rx51_battery_probe(struct platform_device *pdev)
 
      platform_set_drvdata(pdev, di);
 
+    di->dev = &pdev->dev;
      di->bat.name = dev_name(&pdev->dev);
      di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
      di->bat.properties = rx51_battery_props;

drivers/power/rx51_battery.c static int rx51_battery_probe(struct platform_device *pdev) · line 229

Quoted 10 lines
drivers/power/rx51_battery.c +7 -0
      di->bat.num_properties = ARRAY_SIZE(rx51_battery_props);
      di->bat.get_property = rx51_battery_get_property;
 
+    di->channel_temp = iio_channel_get(di->dev, "temp");
+    if (IS_ERR(di->channel_temp))
+        return PTR_ERR(di->channel_temp);
+
+    di->channel_bsi  = iio_channel_get(di->dev, "bsi");
+    if (IS_ERR(di->channel_bsi))
+        return PTR_ERR(di->channel_bsi);

You need to unwind the iio_channel_get that did succeed if we get here. Otherwise references to the iio driver will still be held despite this driver failing to probe.

Quoted 21 lines
drivers/power/rx51_battery.c +13 -0
+
+    di->channel_vbat = iio_channel_get(di->dev, "vbat");
+    if (IS_ERR(di->channel_vbat))
+        return PTR_ERR(di->channel_vbat);
+
      ret = power_supply_register(di->dev, &di->bat);
      if (ret)
          return ret;
@@ -235,12 +248,21 @@ static int rx51_battery_remove(struct platform_device *pdev)
      return 0;
  }
 
+#ifdef CONFIG_OF
+static const struct of_device_id n900_battery_of_match[] = {
+    {.compatible = "nokia,n900-battery", },
+    { },
+};
+MODULE_DEVICE_TABLE(of, n900_battery_of_match);
+#endif
+

drivers/power/rx51_battery.c static int rx51_battery_remove(struct platform_device *pdev) · line 268

Quoted 10 lines
drivers/power/rx51_battery.c +1 -0
  static struct platform_driver rx51_battery_driver = {
      .probe = rx51_battery_probe,
      .remove = rx51_battery_remove,
      .driver = {
          .name = "rx51-battery",
          .owner = THIS_MODULE,
+        .of_match_table = of_match_ptr(n900_battery_of_match),
      },
  };
  module_platform_driver(rx51_battery_driver);

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

#16 lore raw

PATCH 2/2 Documentation: DT: Document rx51-battery binding

In reply to Sebastian Reichel

Add devicetree binding documentation for rx51-battery, which is a simple A/D converter consumer.

---
 .../devicetree/bindings/power/rx51-battery.txt     | 25 ++++++++++++++++++++++
 1 file changed, 25 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/power/rx51-battery.txt
Documentation/devicetree/bindings/power/rx51-battery.txt new file +25 -0
new file mode 100644
index 0000000..9043845
@@ -0,0 +1,25 @@
+Binding for Nokia N900 battery
+
+The Nokia N900 battery status can be read via the TWL4030's A/D converter.
+
+Required properties:
+- compatible: Should contain one of the following:
+ * "nokia,n900-battery"
+- 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
+ * "bsi"  - The ADC channel for battery size identification
+ * "vbat" - The ADC channel to measure the battery voltage
+
+Example from Nokia N900:
+
+battery: n900-battery {
+	compatible = "nokia,n900-battery";
+	io-channels = <&twl4030_madc 0>,
+		      <&twl4030_madc 4>,
+		      <&twl4030_madc 12>;
+	io-channel-names = "temp",
+			   "bsi",
+			   "vbat";
+};
Signature · 1 line
1.8.5.3
#17 lore raw
In reply to Sebastian Reichel

Hi!

2014-02-26 1:46 GMT+01:00 Sebastian Reichel <[email protected]>:

Quoted 15 lines

Hi,

This is PATCHv1 for converting rx51-battery to the IIO API and adding DT support. The patchset compiles and has been tested on my Nokia N900. It depends on another patchset converting twl4030-madc to the IIO API:

https://lkml.org/lkml/2014/2/25/627

-- Sebastian

Sebastian Reichel (2):
rx51_battery: convert to iio consumer
Documentation: DT: Document rx51-battery binding

Quoted 8 lines
 .../devicetree/bindings/power/rx51-battery.txt     | 25 ++++++++
 drivers/power/rx51_battery.c                       | 68 ++++++++++++++--------
 2 files changed, 70 insertions(+), 23 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/power/rx51-battery.txt

--
1.8.5.3

Thanks for patch!

I would like to ask other kernel developers what do you think about moving ADC channel numbers from rx51_battery.ko driver code to DT. Driver rx51_battery.ko is platform specific for Nokia RX-51 (N900) so it is usefull only for this one device.

Before this patch all driver data (look-up tables, adc channel numbers, etc...) were in driver code. Now after this patch adc channel numbers were moved to DT. What do you think? It is better to have all data in one place (driver code) or some in DT and some in driver code?

For me it does not make sense to move these numbers to DT, because driver is rx51 device specific and chaning it in DT does not make sense. And I think it is better to have add driver data in one place and not in two...

Sebastian already wrote me that this is normal to have numbers in DT and other code in driver. But I think that driver which can be used only in one device (so specified only in one DT file) does not need to have configuration (via DT or board files).

Or do you think that driver specified only for one device needs to have ADC numbers configuration via DT?

Signature · 2 lines
Pali Rohár
[email protected]
#18 lore raw

Hi Pali,

Quoted 15 lines · Pali Rohár

I would like to ask other kernel developers what do you think about moving ADC channel numbers from rx51_battery.ko driver code to DT. Driver rx51_battery.ko is platform specific for Nokia RX-51 (N900) so it is usefull only for this one device.

Before this patch all driver data (look-up tables, adc channel numbers, etc...) were in driver code. Now after this patch adc channel numbers were moved to DT. What do you think? It is better to have all data in one place (driver code) or some in DT and some in driver code?

For me it does not make sense to move these numbers to DT, because driver is rx51 device specific and chaning it in DT does not make sense. And I think it is better to have add driver data in one place and not in two...

Quoted 7 lines

Sebastian already wrote me that this is normal to have numbers in DT and other code in driver. But I think that driver which can be used only in one device (so specified only in one DT file) does not need to have configuration (via DT or board files).

Or do you think that driver specified only for one device needs to have ADC numbers configuration via DT?

I think the problem is, that you think of ADC channel numbers as configuration data. This means you think of rx51-battery as an alternative driver for the twl4030-madc.

I think of rx51-battery as its own platform device, which makes use of the ADC similar to a button making use of a GPIO chip. For me the ADC channel numbers are not configuration data, but an inter-device resource reference, like e.g. GPIO references. This is exactly the data you would put into the device tree.

Now let's take the rx51-audio device as another example for an n900 specific device (not yet in mainline kernel). It does not need ADC channels, but GPIO lines:

	sound: n900-audio {
		/* ... some more references ... */
		nokia,tvout-selection-gpio = <&gpio2 8 GPIO_ACTIVE_HIGH>; /* 40 */
		nokia,jack-detection-gpio = <&gpio6 17 GPIO_ACTIVE_HIGH>; /* 177 */
		nokia,eci-switch-gpio = <&gpio6 22 GPIO_ACTIVE_HIGH>; /* 182 */
		nokia,speaker-amplifier-gpio = <&twl_gpio 7 GPIO_ACTIVE_HIGH>;
	};

Since GPIO numbers are not guaranteed to be consistent in DT boot mode at least the GPIO chip must be referenced. Do you really think it's a good idea to leave out the exact GPIO number just because the driver knows it needs the 8th GPIO from the second GPIO chip? IMHO this is really ugly, since it splits the information, which GPIO is used into two parts - one living in the DT and one living in the driver with no advantage at all. So it does make sense to specify inter-device resources via DT even for platform specific devices.

-- Sebastian

#19 lore raw

v2 cover Convert rx51-battery to IIO API and add DT support

In reply to Sebastian Reichel

Hi,

This is PATCHv2 for converting rx51-battery to the IIO API and adding DT support. The patchset compiles and has been tested on my Nokia N900. It depends on another patchset converting twl4030-madc to the IIO API [0].

Changes since PATCHv1 [1]:
* Release IIO channels on device removal
* Release IIO channels when errors occur in device probe

[0] https://lkml.org/lkml/2014/3/1/105
[1] Convert rx51-battery to IIO API and add DT support

-- Sebastian

Sebastian Reichel (2):
rx51_battery: convert to iio consumer
Documentation: DT: Document rx51-battery binding

 .../devicetree/bindings/power/rx51-battery.txt     | 25 ++++++
 drivers/power/rx51_battery.c                       | 90 ++++++++++++++++------
 2 files changed, 91 insertions(+), 24 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/power/rx51-battery.txt
Signature · 1 line
1.9.0
#20 lore raw

PATCH v2 1/2 rx51_battery: convert to iio consumer

Update rx51-battery driver to use the new IIO API of twl4030-madc and add DT support.

---
 drivers/power/rx51_battery.c | 90 ++++++++++++++++++++++++++++++++------------
 1 file changed, 66 insertions(+), 24 deletions(-)
drivers/power/rx51_battery.c +66 -24
index 1bc5857..d5a2acf 100644
@@ -24,34 +24,27 @@
 #include <linux/power_supply.h>
 #include <linux/slab.h>
 #include <linux/i2c/twl4030-madc.h>
-
-/* RX51 specific channels */
-#define TWL4030_MADC_BTEMP_RX51	TWL4030_MADC_ADCIN0
-#define TWL4030_MADC_BCI_RX51	TWL4030_MADC_ADCIN4
+#include <linux/iio/consumer.h>
+#include <linux/of.h>
 
 struct rx51_device_info {
 	struct device *dev;
 	struct power_supply bat;
+	struct iio_channel *channel_temp;
+	struct iio_channel *channel_bsi;
+	struct iio_channel *channel_vbat;
 };
 
 /*
  * Read ADCIN channel value, code copied from maemo kernel
  */
-static int rx51_battery_read_adc(int channel)
+static int rx51_battery_read_adc(struct iio_channel *channel)
 {
-	struct twl4030_madc_request req;
-
-	req.channels = channel;
-	req.do_avg = 1;
-	req.method = TWL4030_MADC_SW1;
-	req.func_cb = NULL;
-	req.type = TWL4030_MADC_WAIT;
-	req.raw = true;
-
-	if (twl4030_madc_conversion(&req) <= 0)
-		return -ENODATA;
-
-	return req.rbuf[ffs(channel) - 1];
+	int val, err;
+	err = iio_read_channel_average_raw(channel, &val);
+	if (err < 0)
+		return err;
+	return val;
 }
 
 /*
@@ -60,10 +53,12 @@ static int rx51_battery_read_adc(int channel)
  */
 static int rx51_battery_read_voltage(struct rx51_device_info *di)
 {
-	int voltage = rx51_battery_read_adc(TWL4030_MADC_VBAT);
+	int voltage = rx51_battery_read_adc(di->channel_vbat);
 
-	if (voltage < 0)
+	if (voltage < 0) {
+		dev_err(di->dev, "Could not read ADC: %d\n", voltage);
 		return voltage;
+	}
 
 	return 1000 * (10000 * voltage / 1705);
 }
@@ -112,7 +107,10 @@ static int rx51_battery_read_temperature(struct rx51_device_info *di)
 {
 	int min = 0;
 	int max = ARRAY_SIZE(rx51_temp_table2) - 1;
-	int raw = rx51_battery_read_adc(TWL4030_MADC_BTEMP_RX51);
+	int raw = rx51_battery_read_adc(di->channel_temp);
+
+	if (raw < 0)
+		dev_err(di->dev, "Could not read ADC: %d\n", raw);
 
 	/* Zero and negative values are undefined */
 	if (raw <= 0)
@@ -146,10 +144,12 @@ static int rx51_battery_read_temperature(struct rx51_device_info *di)
  */
 static int rx51_battery_read_capacity(struct rx51_device_info *di)
 {
-	int capacity = rx51_battery_read_adc(TWL4030_MADC_BCI_RX51);
+	int capacity = rx51_battery_read_adc(di->channel_bsi);
 
-	if (capacity < 0)
+	if (capacity < 0) {
+		dev_err(di->dev, "Could not read ADC: %d\n", capacity);
 		return capacity;
+	}
 
 	return 1280 * (1200 * capacity)/(1024 - capacity);
 }
@@ -213,17 +213,46 @@ static int rx51_battery_probe(struct platform_device *pdev)
 
 	platform_set_drvdata(pdev, di);
 
+	di->dev = &pdev->dev;
 	di->bat.name = dev_name(&pdev->dev);
 	di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
 	di->bat.properties = rx51_battery_props;
 	di->bat.num_properties = ARRAY_SIZE(rx51_battery_props);
 	di->bat.get_property = rx51_battery_get_property;
 
+	di->channel_temp = iio_channel_get(di->dev, "temp");
+	if (IS_ERR(di->channel_temp)) {
+		ret = PTR_ERR(di->channel_temp);
+		goto error;
+	}
+
+	di->channel_bsi  = iio_channel_get(di->dev, "bsi");
+	if (IS_ERR(di->channel_bsi)) {
+		ret = PTR_ERR(di->channel_bsi);
+		goto error_channel_temp;
+	}
+
+	di->channel_vbat = iio_channel_get(di->dev, "vbat");
+	if (IS_ERR(di->channel_vbat)) {
+		ret = PTR_ERR(di->channel_vbat);
+		goto error_channel_bsi;
+	}
+
 	ret = power_supply_register(di->dev, &di->bat);
 	if (ret)
-		return ret;
+		goto error_channel_vbat;
 
 	return 0;
+
+error_channel_vbat:
+	iio_channel_release(di->channel_vbat);
+error_channel_bsi:
+	iio_channel_release(di->channel_bsi);
+error_channel_temp:
+	iio_channel_release(di->channel_temp);
+error:
+
+	return ret;
 }
 
 static int rx51_battery_remove(struct platform_device *pdev)
@@ -232,15 +261,28 @@ static int rx51_battery_remove(struct platform_device *pdev)
 
 	power_supply_unregister(&di->bat);
 
+	iio_channel_release(di->channel_vbat);
+	iio_channel_release(di->channel_bsi);
+	iio_channel_release(di->channel_temp);
+
 	return 0;
 }
 
+#ifdef CONFIG_OF
+static const struct of_device_id n900_battery_of_match[] = {
+	{.compatible = "nokia,n900-battery", },
+	{ },
+};
+MODULE_DEVICE_TABLE(of, n900_battery_of_match);
+#endif
+
 static struct platform_driver rx51_battery_driver = {
 	.probe = rx51_battery_probe,
 	.remove = rx51_battery_remove,
 	.driver = {
 		.name = "rx51-battery",
 		.owner = THIS_MODULE,
+		.of_match_table = of_match_ptr(n900_battery_of_match),
 	},
 };
 module_platform_driver(rx51_battery_driver);
Signature · 1 line
1.9.0
maintainer Acked-by on v2 1/2
#21 lore raw

Re: [PATCHv2 1/2] rx51_battery: convert to iio consumer

Quoted 4 lines · Sebastian Reichel

Update rx51-battery driver to use the new IIO API of twl4030-madc and add DT support.

This looks fine to me. I'd love to see a more generic solution, but we can work on that another time.

Quoted 174 lines
---
  drivers/power/rx51_battery.c | 90 ++++++++++++++++++++++++++++++++------------
  1 file changed, 66 insertions(+), 24 deletions(-)
drivers/power/rx51_battery.c +66 -24
index 1bc5857..d5a2acf 100644
@@ -24,34 +24,27 @@
  #include <linux/power_supply.h>
  #include <linux/slab.h>
  #include <linux/i2c/twl4030-madc.h>
-
-/* RX51 specific channels */
-#define TWL4030_MADC_BTEMP_RX51	TWL4030_MADC_ADCIN0
-#define TWL4030_MADC_BCI_RX51	TWL4030_MADC_ADCIN4
+#include <linux/iio/consumer.h>
+#include <linux/of.h>
 
  struct rx51_device_info {
  	struct device *dev;
  	struct power_supply bat;
+	struct iio_channel *channel_temp;
+	struct iio_channel *channel_bsi;
+	struct iio_channel *channel_vbat;
  };
 
  /*
   * Read ADCIN channel value, code copied from maemo kernel
   */
-static int rx51_battery_read_adc(int channel)
+static int rx51_battery_read_adc(struct iio_channel *channel)
  {
-	struct twl4030_madc_request req;
-
-	req.channels = channel;
-	req.do_avg = 1;
-	req.method = TWL4030_MADC_SW1;
-	req.func_cb = NULL;
-	req.type = TWL4030_MADC_WAIT;
-	req.raw = true;
-
-	if (twl4030_madc_conversion(&req) <= 0)
-		return -ENODATA;
-
-	return req.rbuf[ffs(channel) - 1];
+	int val, err;
+	err = iio_read_channel_average_raw(channel, &val);
+	if (err < 0)
+		return err;
+	return val;
  }
 
  /*
@@ -60,10 +53,12 @@ static int rx51_battery_read_adc(int channel)
   */
  static int rx51_battery_read_voltage(struct rx51_device_info *di)
  {
-	int voltage = rx51_battery_read_adc(TWL4030_MADC_VBAT);
+	int voltage = rx51_battery_read_adc(di->channel_vbat);
 
-	if (voltage < 0)
+	if (voltage < 0) {
+		dev_err(di->dev, "Could not read ADC: %d\n", voltage);
  		return voltage;
+	}
 
  	return 1000 * (10000 * voltage / 1705);
  }
@@ -112,7 +107,10 @@ static int rx51_battery_read_temperature(struct rx51_device_info *di)
  {
  	int min = 0;
  	int max = ARRAY_SIZE(rx51_temp_table2) - 1;
-	int raw = rx51_battery_read_adc(TWL4030_MADC_BTEMP_RX51);
+	int raw = rx51_battery_read_adc(di->channel_temp);
+
+	if (raw < 0)
+		dev_err(di->dev, "Could not read ADC: %d\n", raw);
 
  	/* Zero and negative values are undefined */
  	if (raw <= 0)
@@ -146,10 +144,12 @@ static int rx51_battery_read_temperature(struct rx51_device_info *di)
   */
  static int rx51_battery_read_capacity(struct rx51_device_info *di)
  {
-	int capacity = rx51_battery_read_adc(TWL4030_MADC_BCI_RX51);
+	int capacity = rx51_battery_read_adc(di->channel_bsi);
 
-	if (capacity < 0)
+	if (capacity < 0) {
+		dev_err(di->dev, "Could not read ADC: %d\n", capacity);
  		return capacity;
+	}
 
  	return 1280 * (1200 * capacity)/(1024 - capacity);
  }
@@ -213,17 +213,46 @@ static int rx51_battery_probe(struct platform_device *pdev)
 
  	platform_set_drvdata(pdev, di);
 
+	di->dev = &pdev->dev;
  	di->bat.name = dev_name(&pdev->dev);
  	di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
  	di->bat.properties = rx51_battery_props;
  	di->bat.num_properties = ARRAY_SIZE(rx51_battery_props);
  	di->bat.get_property = rx51_battery_get_property;
 
+	di->channel_temp = iio_channel_get(di->dev, "temp");
+	if (IS_ERR(di->channel_temp)) {
+		ret = PTR_ERR(di->channel_temp);
+		goto error;
+	}
+
+	di->channel_bsi  = iio_channel_get(di->dev, "bsi");
+	if (IS_ERR(di->channel_bsi)) {
+		ret = PTR_ERR(di->channel_bsi);
+		goto error_channel_temp;
+	}
+
+	di->channel_vbat = iio_channel_get(di->dev, "vbat");
+	if (IS_ERR(di->channel_vbat)) {
+		ret = PTR_ERR(di->channel_vbat);
+		goto error_channel_bsi;
+	}
+
  	ret = power_supply_register(di->dev, &di->bat);
  	if (ret)
-		return ret;
+		goto error_channel_vbat;
 
  	return 0;
+
+error_channel_vbat:
+	iio_channel_release(di->channel_vbat);
+error_channel_bsi:
+	iio_channel_release(di->channel_bsi);
+error_channel_temp:
+	iio_channel_release(di->channel_temp);
+error:
+
+	return ret;
  }
 
  static int rx51_battery_remove(struct platform_device *pdev)
@@ -232,15 +261,28 @@ static int rx51_battery_remove(struct platform_device *pdev)
 
  	power_supply_unregister(&di->bat);
 
+	iio_channel_release(di->channel_vbat);
+	iio_channel_release(di->channel_bsi);
+	iio_channel_release(di->channel_temp);
+
  	return 0;
  }
 
+#ifdef CONFIG_OF
+static const struct of_device_id n900_battery_of_match[] = {
+	{.compatible = "nokia,n900-battery", },
+	{ },
+};
+MODULE_DEVICE_TABLE(of, n900_battery_of_match);
+#endif
+
  static struct platform_driver rx51_battery_driver = {
  	.probe = rx51_battery_probe,
  	.remove = rx51_battery_remove,
  	.driver = {
  		.name = "rx51-battery",
  		.owner = THIS_MODULE,
+		.of_match_table = of_match_ptr(n900_battery_of_match),
  	},
  };
  module_platform_driver(rx51_battery_driver);
Reviewed-by
#22 lore raw

Re: [PATCHv2 1/2] rx51_battery: convert to iio consumer

Quoted 9 lines · Jonathan Cameron
Quoted 4 lines · Sebastian Reichel

Update rx51-battery driver to use the new IIO API of twl4030-madc and add DT support.

This looks fine to me. I'd love to see a more generic solution, but we can work on that another time.

I don't see this patch in 3.15-rc nor -next. What is needed to push this patch?

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#23 lore raw

Re: [PATCHv2 1/2] rx51_battery: convert to iio consumer

Quoted 9 lines · Pavel Machek
Quoted 8 lines · Jonathan Cameron
Quoted 4 lines · Sebastian Reichel

Update rx51-battery driver to use the new IIO API of twl4030-madc and add DT support.

This looks fine to me. I'd love to see a more generic solution, but we can work on that another time.

Quoted 6 lines
Quoted 1 line

I don't see this patch in 3.15-rc nor -next. What is needed to push this patch?

The problem is, that the power subsystem maintainers seem to be too busy with real life. The last change in their git [0] was 2014-02-01 and I haven't seen any mail from them on the mailinglist since about the same time. I have just queried his status @ Google+ (there were some life-signs there).

[0] http://git.infradead.org/battery-2.6.git/

-- Sebastian

#24 lore raw

Re: [PATCHv2 1/2] rx51_battery: convert to iio consumer

Quoted 14 lines · Sebastian Reichel
Quoted 13 lines · Pavel Machek
Quoted 9 lines · Jonathan Cameron
Quoted 4 lines · Sebastian Reichel

Update rx51-battery driver to use the new IIO API of twl4030-madc and add DT support.

This looks fine to me. I'd love to see a more generic solution, but we can work on that another time.

Quoted 8 lines
Quoted 2 lines

I don't see this patch in 3.15-rc nor -next. What is needed to push this patch?

The problem is, that the power subsystem maintainers seem to be too busy with real life. The last change in their git [0] was 2014-02-01 and I haven't seen any mail from them on the mailinglist since about the same time. I have just queried his status @ Google+ (there were some life-signs there).

If there is stilll no life out there, you may want to ask Rafael W. to queue the patches...

Signature · 6 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#25 lore raw

Re: [PATCHv2 1/2] rx51_battery: convert to iio consumer

Quoted 8 lines · Pavel Machek
Quoted 5 lines

The problem is, that the power subsystem maintainers seem to be too busy with real life. The last change in their git [0] was 2014-02-01 and I haven't seen any mail from them on the mailinglist since about the same time. I have just queried his status @ Google+ (there were some life-signs there).

If there is still no life out there, you may want to ask Rafael W. to queue the patches...

Don't worry, I haven't forgotten the patches. I don't think they make it into 3.16, but they will go into 3.17. For now I have put them into a dev branch on [0] and requested to take over the power supply subsystem (see thread [1] on LKML).

[0] https://git.kernel.org/cgit/linux/kernel/git/sre/linux-power-supply.git
[1] https://lkml.org/lkml/2014/5/16/504

-- Sebastian

#26 lore raw

PATCH v2 2/2 Documentation: DT: Document rx51-battery binding

In reply to Sebastian Reichel

Add devicetree binding documentation for rx51-battery, which is a simple A/D converter consumer.

---
 .../devicetree/bindings/power/rx51-battery.txt     | 25 ++++++++++++++++++++++
 1 file changed, 25 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/power/rx51-battery.txt
Documentation/devicetree/bindings/power/rx51-battery.txt new file +25 -0
new file mode 100644
index 0000000..9043845
@@ -0,0 +1,25 @@
+Binding for Nokia N900 battery
+
+The Nokia N900 battery status can be read via the TWL4030's A/D converter.
+
+Required properties:
+- compatible: Should contain one of the following:
+ * "nokia,n900-battery"
+- 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
+ * "bsi"  - The ADC channel for battery size identification
+ * "vbat" - The ADC channel to measure the battery voltage
+
+Example from Nokia N900:
+
+battery: n900-battery {
+	compatible = "nokia,n900-battery";
+	io-channels = <&twl4030_madc 0>,
+		      <&twl4030_madc 4>,
+		      <&twl4030_madc 12>;
+	io-channel-names = "temp",
+			   "bsi",
+			   "vbat";
+};
Signature · 6 lines
1.9.0

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
maintainer Acked-by on v2 2/2
#27 lore raw

Re: [PATCHv2 2/2] Documentation: DT: Document rx51-battery binding

Quoted 4 lines · Sebastian Reichel

Add devicetree binding documentation for rx51-battery, which is a simple A/D converter consumer.

Clearly this set has some rather large dependencies, but its good to go once those are in place.

Jonathan

Quoted 37 lines
---
  .../devicetree/bindings/power/rx51-battery.txt     | 25 ++++++++++++++++++++++
  1 file changed, 25 insertions(+)

create mode 100644 Documentation/devicetree/bindings/power/rx51-battery.txt

Documentation/devicetree/bindings/power/rx51-battery.txt new file +25 -0
new file mode 100644
index 0000000..9043845
@@ -0,0 +1,25 @@
+Binding for Nokia N900 battery
+
+The Nokia N900 battery status can be read via the TWL4030's A/D converter.
+
+Required properties:
+- compatible: Should contain one of the following:
+ * "nokia,n900-battery"
+- 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
+ * "bsi"  - The ADC channel for battery size identification
+ * "vbat" - The ADC channel to measure the battery voltage
+
+Example from Nokia N900:
+
+battery: n900-battery {
+	compatible = "nokia,n900-battery";
+	io-channels = <&twl4030_madc 0>,
+		      <&twl4030_madc 4>,
+		      <&twl4030_madc 12>;
+	io-channel-names = "temp",
+			   "bsi",
+			   "vbat";
+};
Reviewed-by on v2 2/2
#28 lore raw

Re: [PATCHv2 2/2] Documentation: DT: Document rx51-battery binding

In reply to Sebastian Reichel
Quoted 4 lines · Sebastian Reichel

Add devicetree binding documentation for rx51-battery, which is a simple A/D converter consumer.

Signature · 6 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html