Skip to contentSkip to conversation

mfd: twl4030-madc: Add devicetree support.

PATCH In review Marek Belisko 9 participants 92 messages Feb 14, 2014 – Apr 20, 2014

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

PATCH 1/2 mfd: twl4030-madc: Add devicetree support.

---
 .../devicetree/bindings/mfd/twl4030-madc.txt       | 18 +++++++++++++
 drivers/mfd/twl4030-madc.c                         | 31 ++++++++++++++++++++--
 2 files changed, 47 insertions(+), 2 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/mfd/twl4030-madc.txt
Documentation/devicetree/bindings/mfd/twl4030-madc.txt new file +18 -0
new file mode 100644
index 0000000..9df66ab
@@ -0,0 +1,18 @@
+Texas Instruments TWL family (twl4030) analog signal conversion monitor module
+
+Required properties:
+- compatible : must be "ti,twl4030-madc"
+- interrupts : interrupt number
+
+Example:
+twl: twl at 48 {
+	reg = <0x48>;
+	interrupts = <7>; /* SYS_NIRQ cascaded to intc */
+	interrupt-parent = <&intc>;
+
+	twl_madc: madc {
+		compatible = "ti,twl4030-madc";
+		interrupts = <1>;
+	};
+};
+
drivers/mfd/twl4030-madc.c +29 -2
index 4c583e4..9a2bbcf 100644
@@ -695,6 +695,29 @@ static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on)
 	return 0;
 }
 
+#ifdef CONFIG_OF
+static struct twl4030_madc_platform_data *
+	twl4030_madc_of_parse(struct platform_device *pdev)
+{
+	struct twl4030_madc_platform_data *pdata;
+
+	pdata = devm_kzalloc(&pdev->dev,
+			sizeof(struct twl4030_madc_platform_data), GFP_KERNEL);
+	if (!pdata)
+		return ERR_PTR(-ENOMEM);
+
+	pdata->irq_line = platform_get_irq(pdev, 0);
+
+	return pdata;
+}
+
+static const struct of_device_id twl4030_madc_dt_match_table[] = {
+	{ .compatible = "ti,twl4030-madc" },
+	{},
+};
+
+#endif
+
 /*
  * Initialize MADC and request for threaded irq
  */
@@ -706,8 +729,11 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 	u8 regval;
 
 	if (!pdata) {
-		dev_err(&pdev->dev, "platform_data not available\n");
-		return -EINVAL;
+		pdata = twl4030_madc_of_parse(pdev);
+		if (!pdata) {
+			dev_err(&pdev->dev, "platform_data not available\n");
+			return -EINVAL;
+		}
 	}
 	madc = kzalloc(sizeof(*madc), GFP_KERNEL);
 	if (!madc)
@@ -807,6 +833,7 @@ static struct platform_driver twl4030_madc_driver = {
 	.driver = {
 		   .name = "twl4030_madc",
 		   .owner = THIS_MODULE,
+		   .of_match_table = of_match_ptr(twl4030_madc_dt_match_table),
 		   },
 };
 
Signature · 1 line
1.8.3.2
#1 lore raw

PATCH 2/2 ARM: dts: twl4030: Add twl4030-madc node.

---
 arch/arm/boot/dts/twl4030.dtsi | 5 +++++
 1 file changed, 5 insertions(+)
arch/arm/boot/dts/twl4030.dtsi +5 -0
index 4217096..e6dec53 100644
@@ -145,4 +145,9 @@
 		compatible = "ti,twl4030-pwrbutton";
 		interrupts = <8>;
 	};
+
+	twl_madc: madc {
+		compatible = "ti,twl4030-madc";
+		interrupts = <1>;
+	};
 };
Signature · 1 line
1.8.3.2
#2 lore raw
In reply to Marek Belisko
Quoted 5 lines
---
 .../devicetree/bindings/mfd/twl4030-madc.txt       | 18 +++++++++++++
 drivers/mfd/twl4030-madc.c                         | 31

++++++++++++++++++++--

Please separate these into different patches.

Quoted 2 lines
 2 files changed, 47 insertions(+), 2 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/mfd/twl4030-madc.txt

<snip>

drivers/mfd/twl4030-madc.c static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on) · line 698

Quoted 6 lines

+++ b/drivers/mfd/twl4030-madc.c

drivers/mfd/twl4030-madc.c +1 -0
@@ -695,6 +695,29 @@ static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on)
 	return 0;
 }
 
+#ifdef CONFIG_OF

I believe we're heading for a more:

if (IS_ENABLED(CONFIG_OF))

... approach. I won't enforce it, but please consider using it.

drivers/mfd/twl4030-madc.c static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on) · line 705

Quoted 7 lines
drivers/mfd/twl4030-madc.c +7 -0
+static struct twl4030_madc_platform_data *
+	twl4030_madc_of_parse(struct platform_device *pdev)
+{
+	struct twl4030_madc_platform_data *pdata;
+
+	pdata = devm_kzalloc(&pdev->dev,
+			sizeof(struct twl4030_madc_platform_data), GFP_KERNEL);

s/struct twl4030_madc_platform_data/*pdata/

drivers/mfd/twl4030-madc.c static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on) · line 709

Quoted 4 lines
drivers/mfd/twl4030-madc.c +4 -0
+	if (!pdata)
+		return ERR_PTR(-ENOMEM);
+
+	pdata->irq_line = platform_get_irq(pdev, 0);

Why weren't 'resources' used in the original implementation?

Quoted 14 lines
drivers/mfd/twl4030-madc.c +10 -0
+	return pdata;
+}
+
+static const struct of_device_id twl4030_madc_dt_match_table[] = {
+	{ .compatible = "ti,twl4030-madc" },
+	{},
+};
+
+#endif
+
 /*
  * Initialize MADC and request for threaded irq
  */

drivers/mfd/twl4030-madc.c static int twl4030_madc_probe(struct platform_device *pdev) · line 733

Quoted 7 lines
drivers/mfd/twl4030-madc.c +2 -2
@@ -706,8 +729,11 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 	u8 regval;
 
 	if (!pdata) {
-		dev_err(&pdev->dev, "platform_data not available\n");
-		return -EINVAL;
+		pdata = twl4030_madc_of_parse(pdev);
+		if (!pdata) {

And if you received -ENOMEM?

Quoted 14 lines
drivers/mfd/twl4030-madc.c +4 -0
+			dev_err(&pdev->dev, "platform_data not available\n");
+			return -EINVAL;
+		}
 	}
 	madc = kzalloc(sizeof(*madc), GFP_KERNEL);
 	if (!madc)
@@ -807,6 +833,7 @@ static struct platform_driver twl4030_madc_driver = {
 	.driver = {
 		   .name = "twl4030_madc",
 		   .owner = THIS_MODULE,
+		   .of_match_table = of_match_ptr(twl4030_madc_dt_match_table),
 		   },
 };
 
Signature · 4 lines
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org ? Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
#3 lore raw
Quoted 7 lines · Lee Jones
Quoted 5 lines
---
 .../devicetree/bindings/mfd/twl4030-madc.txt       | 18 +++++++++++++
 drivers/mfd/twl4030-madc.c                         | 31

++++++++++++++++++++--

Please separate these into different patches.

OK.

Quoted 11 lines
Quoted 3 lines
 2 files changed, 47 insertions(+), 2 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/mfd/twl4030-madc.txt

<snip>

Quoted 5 lines

+++ b/drivers/mfd/twl4030-madc.c

drivers/mfd/twl4030-madc.c +0 -0
@@ -695,6 +695,29 @@ static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on)
      return 0;
 }
 
Quoted 7 lines

drivers/mfd/twl4030-madc.c static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on) · line 698

Quoted 1 line
drivers/mfd/twl4030-madc.c +1 -0
+#ifdef CONFIG_OF

I believe we're heading for a more:

if (IS_ENABLED(CONFIG_OF))

... approach. I won't enforce it, but please consider using it.

OK I'll use it in next version.

Quoted 10 lines

drivers/mfd/twl4030-madc.c static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on) · line 705

Quoted 8 lines
drivers/mfd/twl4030-madc.c +7 -0
+static struct twl4030_madc_platform_data *
+     twl4030_madc_of_parse(struct platform_device *pdev)
+{
+     struct twl4030_madc_platform_data *pdata;
+
+     pdata = devm_kzalloc(&pdev->dev,
+                     sizeof(struct twl4030_madc_platform_data), GFP_KERNEL);

s/struct twl4030_madc_platform_data/*pdata/

Right typo.

Quoted 7 lines

drivers/mfd/twl4030-madc.c static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on) · line 709

Quoted 5 lines
drivers/mfd/twl4030-madc.c +4 -0
+     if (!pdata)
+             return ERR_PTR(-ENOMEM);
+
+     pdata->irq_line = platform_get_irq(pdev, 0);

Why weren't 'resources' used in the original implementation?

Not sure I'm not an author :). It's passed in platform data.

Quoted 17 lines
Quoted 17 lines
drivers/mfd/twl4030-madc.c +10 -0
+     return pdata;
+}
+
+static const struct of_device_id twl4030_madc_dt_match_table[] = {
+     { .compatible = "ti,twl4030-madc" },
+     {},
+};
+
+#endif
+
 /*
  * Initialize MADC and request for threaded irq
  */
@@ -706,8 +729,11 @@ static int twl4030_madc_probe(struct platform_device *pdev)
      u8 regval;
 
Quoted 7 lines

drivers/mfd/twl4030-madc.c static int twl4030_madc_probe(struct platform_device *pdev) · line 733

Quoted 5 lines
drivers/mfd/twl4030-madc.c +2 -2
      if (!pdata) {
-             dev_err(&pdev->dev, "platform_data not available\n");
-             return -EINVAL;
+             pdata = twl4030_madc_of_parse(pdev);
+             if (!pdata) {

And if you received -ENOMEM?

Hmm right. I'll fix that.

Quoted 13 lines
Quoted 13 lines
drivers/mfd/twl4030-madc.c +4 -0
+                     dev_err(&pdev->dev, "platform_data not available\n");
+                     return -EINVAL;
+             }
      }
      madc = kzalloc(sizeof(*madc), GFP_KERNEL);
      if (!madc)
@@ -807,6 +833,7 @@ static struct platform_driver twl4030_madc_driver = {
      .driver = {
                 .name = "twl4030_madc",
                 .owner = THIS_MODULE,
+                .of_match_table = of_match_ptr(twl4030_madc_dt_match_table),
                 },
Quoted 8 lines

Review static struct platform_driver twl4030_madc_driver = { · line 839

Quoted 2 lines
(quoted diff) +0 -0
 };
 

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

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
#4 lore raw
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
---
 .../devicetree/bindings/mfd/twl4030-madc.txt       | 18 +++++++++++++
 drivers/mfd/twl4030-madc.c                         | 31

++++++++++++++++++++--

<snip>

Quoted 10 lines
Quoted 9 lines

drivers/mfd/twl4030-madc.c static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on) · line 705

Quoted 7 lines
drivers/mfd/twl4030-madc.c +7 -0
+static struct twl4030_madc_platform_data *
+     twl4030_madc_of_parse(struct platform_device *pdev)
+{
+     struct twl4030_madc_platform_data *pdata;
+
+     pdata = devm_kzalloc(&pdev->dev,
+                     sizeof(struct twl4030_madc_platform_data), GFP_KERNEL);

s/struct twl4030_madc_platform_data/*pdata/

Right typo.

Sorry, my comment of ambiguous.

I mean do this:

sizeof(*pdata)

... instead of this:

sizeof(struct twl4030_madc_platform_data)

Quoted 7 lines
Quoted 6 lines

drivers/mfd/twl4030-madc.c static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on) · line 709

Quoted 4 lines
drivers/mfd/twl4030-madc.c +4 -0
+     if (!pdata)
+             return ERR_PTR(-ENOMEM);
+
+     pdata->irq_line = platform_get_irq(pdev, 0);

Why weren't 'resources' used in the original implementation?

Not sure I'm not an author :). It's passed in platform data.

Yes, I saw that. It should be changed.

Signature · 4 lines
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org ? Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
#5 lore raw
In reply to Marek Belisko

Hi Marek,

I have prepared a patchset, which adds DT bindings for twl4030-madc using the standard IIO DT API. I have not yet send the patchset, since I have not yet found the time to test the patchset. I will send them as RFC now.

-- Sebastian
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20140214/2a7c85d4/attachment.sig>

#6 lore raw

RFC 1/4 mfd: twl4030-madc: Use managed resources

Update twl4030-madc driver to use managed resources.

---
 drivers/mfd/twl4030-madc.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
drivers/mfd/twl4030-madc.c +6 -9
index 4c583e4..5458561 100644
@@ -702,14 +702,14 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 {
 	struct twl4030_madc_data *madc;
 	struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev);
-	int ret;
+	int irq, ret;
 	u8 regval;
 
 	if (!pdata) {
 		dev_err(&pdev->dev, "platform_data not available\n");
 		return -EINVAL;
 	}
-	madc = kzalloc(sizeof(*madc), GFP_KERNEL);
+	madc = devm_kzalloc(&pdev->dev, sizeof(*madc), GFP_KERNEL);
 	if (!madc)
 		return -ENOMEM;
 
@@ -726,7 +726,7 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 	    TWL4030_MADC_ISR1 : TWL4030_MADC_ISR2;
 	ret = twl4030_madc_set_power(madc, 1);
 	if (ret < 0)
-		goto err_power;
+		return ret;
 	ret = twl4030_madc_set_current_generator(madc, 0, 1);
 	if (ret < 0)
 		goto err_current_generator;
@@ -770,7 +770,9 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 
 	platform_set_drvdata(pdev, madc);
 	mutex_init(&madc->lock);
-	ret = request_threaded_irq(platform_get_irq(pdev, 0), NULL,
+
+	irq = platform_get_irq(pdev, 0);
+	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
 				   twl4030_madc_threaded_irq_handler,
 				   IRQF_TRIGGER_RISING, "twl4030_madc", madc);
 	if (ret) {
@@ -783,9 +785,6 @@ err_i2c:
 	twl4030_madc_set_current_generator(madc, 0, 0);
 err_current_generator:
 	twl4030_madc_set_power(madc, 0);
-err_power:
-	kfree(madc);
-
 	return ret;
 }
 
@@ -793,10 +792,8 @@ static int twl4030_madc_remove(struct platform_device *pdev)
 {
 	struct twl4030_madc_data *madc = platform_get_drvdata(pdev);
 
-	free_irq(platform_get_irq(pdev, 0), madc);
 	twl4030_madc_set_current_generator(madc, 0, 0);
 	twl4030_madc_set_power(madc, 0);
-	kfree(madc);
 
 	return 0;
 }
Signature · 1 line
1.8.5.3
#7 lore raw

RFC 2/4 mfd: twl4030-madc: Add DT support and convert to IIO framework

This converts twl4030-madc module to use the Industrial IO ADC framework and adds device tree support.

---
 drivers/mfd/twl4030-madc.c | 121 ++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 114 insertions(+), 7 deletions(-)
drivers/mfd/twl4030-madc.c +114 -7
index 5458561..4da61c4 100644
@@ -47,6 +47,10 @@
 #include <linux/gfp.h>
 #include <linux/err.h>
 
+#include <linux/iio/iio.h>
+#include <linux/iio/machine.h>
+#include <linux/iio/driver.h>
+
 /*
  * struct twl4030_madc_data - a container for madc info
  * @dev - pointer to device structure for madc
@@ -59,10 +63,72 @@ struct twl4030_madc_data {
 	struct device *dev;
 	struct mutex lock;	/* mutex protecting this data structure */
 	struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS];
+	bool use_second_irq;
 	int imr;
 	int isr;
 };
 
+static int twl4030_madc_read(struct iio_dev *iio_dev,
+			     const struct iio_chan_spec *chan,
+			     int *val, int *val2, long mask)
+{
+	struct twl4030_madc_data *madc = iio_priv(iio_dev);
+	struct twl4030_madc_request req;
+	int channel = chan->channel;
+	int ret;
+
+	req.method = madc->use_second_irq ? TWL4030_MADC_SW2 : TWL4030_MADC_SW1;
+
+	req.channels = BIT(channel);
+	req.active = 0;
+	req.func_cb = NULL;
+	req.raw = (mask & IIO_CHAN_INFO_PROCESSED) ? false : true;
+	req.do_avg = (mask & IIO_CHAN_INFO_AVERAGE_RAW) ? true : false;
+
+	ret = twl4030_madc_conversion(&req);
+	if (ret < 0)
+		return ret;
+
+	*val = req.rbuf[channel];
+
+	return IIO_VAL_INT;
+}
+
+static const struct iio_info twl4030_madc_iio_info = {
+	.read_raw = &twl4030_madc_read,
+	.driver_module = THIS_MODULE,
+};
+
+#define ADC_CHANNEL(_channel, _type, _name, _mask) {	\
+	.type = _type,					\
+	.scan_type = IIO_ST('u', 10, 16, 0),		\
+	.channel = _channel,				\
+	.info_mask_separate = _mask,			\
+	.datasheet_name = _name,			\
+	.indexed = 1,					\
+}
+
+static const struct iio_chan_spec twl4030_madc_iio_channels[] = {
+	ADC_CHANNEL(0, IIO_VOLTAGE, "ADCIN0", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(1, IIO_TEMP, "ADCIN1", BIT(IIO_CHAN_INFO_PROCESSED) |
+					   BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(2, IIO_VOLTAGE, "ADCIN2", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(3, IIO_VOLTAGE, "ADCIN3", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(4, IIO_VOLTAGE, "ADCIN4", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(5, IIO_VOLTAGE, "ADCIN5", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(6, IIO_VOLTAGE, "ADCIN6", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(7, IIO_VOLTAGE, "ADCIN7", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(8, IIO_VOLTAGE, "ADCIN8", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(9, IIO_VOLTAGE, "ADCIN9", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(10, IIO_CURRENT, "ADCIN10", BIT(IIO_CHAN_INFO_PROCESSED) |
+						BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(11, IIO_VOLTAGE, "ADCIN11", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(12, IIO_VOLTAGE, "ADCIN12", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(13, IIO_VOLTAGE, "ADCIN13", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(14, IIO_VOLTAGE, "ADCIN14", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(15, IIO_VOLTAGE, "ADCIN15", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+};
+
 static struct twl4030_madc_data *twl4030_madc;
 
 struct twl4030_prescale_divider_ratios {
@@ -702,28 +768,49 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 {
 	struct twl4030_madc_data *madc;
 	struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev);
+	struct device_node *np = pdev->dev.of_node;
 	int irq, ret;
 	u8 regval;
+	struct iio_dev *iio_dev = NULL;
 
-	if (!pdata) {
+	if (!pdata && !np) {
 		dev_err(&pdev->dev, "platform_data not available\n");
 		return -EINVAL;
 	}
-	madc = devm_kzalloc(&pdev->dev, sizeof(*madc), GFP_KERNEL);
-	if (!madc)
+
+	iio_dev = devm_iio_device_alloc(&pdev->dev,
+					sizeof(struct twl4030_madc_data));
+	if (!iio_dev) {
+		dev_err(&pdev->dev, "failed allocating iio device\n");
 		return -ENOMEM;
+	}
 
+	madc = iio_priv(iio_dev);
 	madc->dev = &pdev->dev;
 
+	iio_dev->name = dev_name(&pdev->dev);
+	iio_dev->dev.parent = &pdev->dev;
+	iio_dev->dev.of_node = pdev->dev.of_node;
+	iio_dev->info = &twl4030_madc_iio_info;
+	iio_dev->modes = INDIO_DIRECT_MODE;
+	iio_dev->channels = twl4030_madc_iio_channels;
+	iio_dev->num_channels = 16;
+
 	/*
 	 * Phoenix provides 2 interrupt lines. The first one is connected to
 	 * the OMAP. The other one can be connected to the other processor such
 	 * as modem. Hence two separate ISR and IMR registers.
 	 */
-	madc->imr = (pdata->irq_line == 1) ?
+	if (pdata)
+		madc->use_second_irq = pdata->irq_line != 1;
+	else
+		madc->use_second_irq = false;
+
+	madc->imr = (madc->use_second_irq == 1) ?
 	    TWL4030_MADC_IMR1 : TWL4030_MADC_IMR2;
-	madc->isr = (pdata->irq_line == 1) ?
+	madc->isr = (madc->use_second_irq == 1) ?
 	    TWL4030_MADC_ISR1 : TWL4030_MADC_ISR2;
+
 	ret = twl4030_madc_set_power(madc, 1);
 	if (ret < 0)
 		return ret;
@@ -768,7 +855,7 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 		}
 	}
 
-	platform_set_drvdata(pdev, madc);
+	platform_set_drvdata(pdev, iio_dev);
 	mutex_init(&madc->lock);
 
 	irq = platform_get_irq(pdev, 0);
@@ -780,7 +867,15 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 		goto err_i2c;
 	}
 	twl4030_madc = madc;
+
+	ret = iio_device_register(iio_dev);
+	if (ret) {
+		dev_dbg(&pdev->dev, "could not register iio device\n");
+		goto err_i2c;
+	}
+
 	return 0;
+
 err_i2c:
 	twl4030_madc_set_current_generator(madc, 0, 0);
 err_current_generator:
@@ -790,20 +885,32 @@ err_current_generator:
 
 static int twl4030_madc_remove(struct platform_device *pdev)
 {
-	struct twl4030_madc_data *madc = platform_get_drvdata(pdev);
+	struct iio_dev *iio_dev = platform_get_drvdata(pdev);
+	struct twl4030_madc_data *madc = iio_priv(iio_dev);
 
 	twl4030_madc_set_current_generator(madc, 0, 0);
 	twl4030_madc_set_power(madc, 0);
 
+	iio_device_unregister(iio_dev);
+
 	return 0;
 }
 
+#ifdef CONFIG_OF
+static const struct of_device_id twl_madc_of_match[] = {
+	{.compatible = "ti,twl4030-madc", },
+	{ },
+};
+MODULE_DEVICE_TABLE(of, twl_madc_of_match);
+#endif
+
 static struct platform_driver twl4030_madc_driver = {
 	.probe = twl4030_madc_probe,
 	.remove = twl4030_madc_remove,
 	.driver = {
 		   .name = "twl4030_madc",
 		   .owner = THIS_MODULE,
+		   .of_match_table = of_match_ptr(twl_madc_of_match),
 		   },
 };
 
Signature · 1 line
1.8.5.3
#8 lore raw

Re: [RFCv1 2/4] mfd: twl4030-madc: Add DT support and convert to IIO framework

Hi Sebastian,

Quoted 131 lines · Sebastian Reichel

This converts twl4030-madc module to use the Industrial IO ADC framework and adds device tree support.

---
 drivers/mfd/twl4030-madc.c | 121 ++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 114 insertions(+), 7 deletions(-)
drivers/mfd/twl4030-madc.c +84 -3
index 5458561..4da61c4 100644
@@ -47,6 +47,10 @@
 #include <linux/gfp.h>
 #include <linux/err.h>
 
+#include <linux/iio/iio.h>
+#include <linux/iio/machine.h>
+#include <linux/iio/driver.h>
+
 /*
  * struct twl4030_madc_data - a container for madc info
  * @dev - pointer to device structure for madc
@@ -59,10 +63,72 @@ struct twl4030_madc_data {
        struct device *dev;
        struct mutex lock;      /* mutex protecting this data structure */
        struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS];
+       bool use_second_irq;
        int imr;
        int isr;
 };
 
+static int twl4030_madc_read(struct iio_dev *iio_dev,
+                            const struct iio_chan_spec *chan,
+                            int *val, int *val2, long mask)
+{
+       struct twl4030_madc_data *madc = iio_priv(iio_dev);
+       struct twl4030_madc_request req;
+       int channel = chan->channel;
+       int ret;
+
+       req.method = madc->use_second_irq ? TWL4030_MADC_SW2 : TWL4030_MADC_SW1;
+
+       req.channels = BIT(channel);
+       req.active = 0;
+       req.func_cb = NULL;
+       req.raw = (mask & IIO_CHAN_INFO_PROCESSED) ? false : true;
+       req.do_avg = (mask & IIO_CHAN_INFO_AVERAGE_RAW) ? true : false;
+
+       ret = twl4030_madc_conversion(&req);
+       if (ret < 0)
+               return ret;
+
+       *val = req.rbuf[channel];
+
+       return IIO_VAL_INT;
+}
+
+static const struct iio_info twl4030_madc_iio_info = {
+       .read_raw = &twl4030_madc_read,
+       .driver_module = THIS_MODULE,
+};
+
+#define ADC_CHANNEL(_channel, _type, _name, _mask) {   \
+       .type = _type,                                  \
+       .scan_type = IIO_ST('u', 10, 16, 0),            \
+       .channel = _channel,                            \
+       .info_mask_separate = _mask,                    \
+       .datasheet_name = _name,                        \
+       .indexed = 1,                                   \
+}
+
+static const struct iio_chan_spec twl4030_madc_iio_channels[] = {
+       ADC_CHANNEL(0, IIO_VOLTAGE, "ADCIN0", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+       ADC_CHANNEL(1, IIO_TEMP, "ADCIN1", BIT(IIO_CHAN_INFO_PROCESSED) |
+                                          BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+       ADC_CHANNEL(2, IIO_VOLTAGE, "ADCIN2", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+       ADC_CHANNEL(3, IIO_VOLTAGE, "ADCIN3", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+       ADC_CHANNEL(4, IIO_VOLTAGE, "ADCIN4", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+       ADC_CHANNEL(5, IIO_VOLTAGE, "ADCIN5", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+       ADC_CHANNEL(6, IIO_VOLTAGE, "ADCIN6", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+       ADC_CHANNEL(7, IIO_VOLTAGE, "ADCIN7", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+       ADC_CHANNEL(8, IIO_VOLTAGE, "ADCIN8", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+       ADC_CHANNEL(9, IIO_VOLTAGE, "ADCIN9", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+       ADC_CHANNEL(10, IIO_CURRENT, "ADCIN10", BIT(IIO_CHAN_INFO_PROCESSED) |
+                                               BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+       ADC_CHANNEL(11, IIO_VOLTAGE, "ADCIN11", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+       ADC_CHANNEL(12, IIO_VOLTAGE, "ADCIN12", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+       ADC_CHANNEL(13, IIO_VOLTAGE, "ADCIN13", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+       ADC_CHANNEL(14, IIO_VOLTAGE, "ADCIN14", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+       ADC_CHANNEL(15, IIO_VOLTAGE, "ADCIN15", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+};
+
 static struct twl4030_madc_data *twl4030_madc;
 
 struct twl4030_prescale_divider_ratios {
@@ -702,28 +768,49 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 {
        struct twl4030_madc_data *madc;
        struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev);
+       struct device_node *np = pdev->dev.of_node;
        int irq, ret;
        u8 regval;
+       struct iio_dev *iio_dev = NULL;
 
-       if (!pdata) {
+       if (!pdata && !np) {
                dev_err(&pdev->dev, "platform_data not available\n");
                return -EINVAL;
        }
-       madc = devm_kzalloc(&pdev->dev, sizeof(*madc), GFP_KERNEL);
-       if (!madc)
+
+       iio_dev = devm_iio_device_alloc(&pdev->dev,
+                                       sizeof(struct twl4030_madc_data));
+       if (!iio_dev) {
+               dev_err(&pdev->dev, "failed allocating iio device\n");
                return -ENOMEM;
+       }
 
+       madc = iio_priv(iio_dev);
        madc->dev = &pdev->dev;
 
+       iio_dev->name = dev_name(&pdev->dev);
+       iio_dev->dev.parent = &pdev->dev;
+       iio_dev->dev.of_node = pdev->dev.of_node;
+       iio_dev->info = &twl4030_madc_iio_info;
+       iio_dev->modes = INDIO_DIRECT_MODE;
+       iio_dev->channels = twl4030_madc_iio_channels;
+       iio_dev->num_channels = 16;
+

drivers/mfd/twl4030-madc.c static int twl4030_madc_probe(struct platform_device *pdev) · line 807

Quoted 10 lines
drivers/mfd/twl4030-madc.c +4 -1
        /*
         * Phoenix provides 2 interrupt lines. The first one is connected to
         * the OMAP. The other one can be connected to the other processor such
         * as modem. Hence two separate ISR and IMR registers.
         */
-       madc->imr = (pdata->irq_line == 1) ?
+       if (pdata)
+               madc->use_second_irq = pdata->irq_line != 1;
+       else
+               madc->use_second_irq = false;

Can we add some property to use second IRQ also in DT?

Quoted 70 lines
drivers/mfd/twl4030-madc.c +26 -4
+
+       madc->imr = (madc->use_second_irq == 1) ?
            TWL4030_MADC_IMR1 : TWL4030_MADC_IMR2;
-       madc->isr = (pdata->irq_line == 1) ?
+       madc->isr = (madc->use_second_irq == 1) ?
            TWL4030_MADC_ISR1 : TWL4030_MADC_ISR2;
+
        ret = twl4030_madc_set_power(madc, 1);
        if (ret < 0)
                return ret;
@@ -768,7 +855,7 @@ static int twl4030_madc_probe(struct platform_device *pdev)
                }
        }
 
-       platform_set_drvdata(pdev, madc);
+       platform_set_drvdata(pdev, iio_dev);
        mutex_init(&madc->lock);
 
        irq = platform_get_irq(pdev, 0);
@@ -780,7 +867,15 @@ static int twl4030_madc_probe(struct platform_device *pdev)
                goto err_i2c;
        }
        twl4030_madc = madc;
+
+       ret = iio_device_register(iio_dev);
+       if (ret) {
+               dev_dbg(&pdev->dev, "could not register iio device\n");
+               goto err_i2c;
+       }
+
        return 0;
+
 err_i2c:
        twl4030_madc_set_current_generator(madc, 0, 0);
 err_current_generator:
@@ -790,20 +885,32 @@ err_current_generator:
 
 static int twl4030_madc_remove(struct platform_device *pdev)
 {
-       struct twl4030_madc_data *madc = platform_get_drvdata(pdev);
+       struct iio_dev *iio_dev = platform_get_drvdata(pdev);
+       struct twl4030_madc_data *madc = iio_priv(iio_dev);
 
        twl4030_madc_set_current_generator(madc, 0, 0);
        twl4030_madc_set_power(madc, 0);
 
+       iio_device_unregister(iio_dev);
+
        return 0;
 }
 
+#ifdef CONFIG_OF
+static const struct of_device_id twl_madc_of_match[] = {
+       {.compatible = "ti,twl4030-madc", },
+       { },
+};
+MODULE_DEVICE_TABLE(of, twl_madc_of_match);
+#endif
+
 static struct platform_driver twl4030_madc_driver = {
        .probe = twl4030_madc_probe,
        .remove = twl4030_madc_remove,
        .driver = {
                   .name = "twl4030_madc",
                   .owner = THIS_MODULE,
+                  .of_match_table = of_match_ptr(twl_madc_of_match),
                   },
 };
 
--
Quoted 2 lines

1.8.5.3

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
#9 lore raw

Re: [RFCv1 2/4] mfd: twl4030-madc: Add DT support and convert to IIO framework

Hi Marek,

Quoted 11 lines · Belisko Marek

drivers/mfd/twl4030-madc.c static int twl4030_madc_probe(struct platform_device *pdev) · line 807

Quoted 10 lines

/*
* Phoenix provides 2 interrupt lines. The first one is connected to
* the OMAP. The other one can be connected to the other processor such
* as modem. Hence two separate ISR and IMR registers.
*/

drivers/mfd/twl4030-madc.c +4 -1
-       madc->imr = (pdata->irq_line == 1) ?
+       if (pdata)
+               madc->use_second_irq = pdata->irq_line != 1;
+       else
+               madc->use_second_irq = false;
Quoted 1 line

Can we add some property to use second IRQ also in DT?

Sure, a boolean property like this could be added:

ti,system-uses-second-madc-irq;

I did not add support for it initially, since I could not find any board in the mainline kernel with this setup. If such a board is added at some later point the driver can be extended easily.

-- Sebastian

#10 lore raw

RFC 3/4 Documentation: DT: Document twl4030-madc binding

In reply to Sebastian Reichel

Add devicetree binding documentation for twl4030-madc analog digital converter.

---
 .../devicetree/bindings/iio/adc/twl4030-madc.txt      | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/adc/twl4030-madc.txt
Documentation/devicetree/bindings/iio/adc/twl4030-madc.txt new file +19 -0
new file mode 100644
index 0000000..d074286
@@ -0,0 +1,19 @@
+* TWL4030 Monitoring Analog to Digital Converter (MADC)
+
+The MADC subsystem in the TWL4030 consists of a 10-bit ADC
+combined with a 16-input analog multiplexer.
+
+Required properties:
+  - compatible: Should contain "ti,twl4030-madc".
+  - interrupts: IRQ line for the MADC submodule.
+  - #io-channel-cells: Should be set to <1>.
+
+Example:
+
+&twl {
+	madc {
+		compatible = "ti,twl4030-madc";
+		interrupts = <3>;
+		#io-channel-cells = <1>;
+	};
+};
Signature · 1 line
1.8.5.3
#11 lore raw

RFC 4/4 mfd: twl4030-madc: Move driver to drivers/iio/adc

In reply to Sebastian Reichel

This is a driver for an A/D converter, which belongs into drivers/iio/adc.

---
 drivers/iio/adc/Kconfig        |  10 +
 drivers/iio/adc/Makefile       |   1 +
 drivers/iio/adc/twl4030-madc.c | 922 +++++++++++++++++++++++++++++++++++++++++
 drivers/mfd/Kconfig            |  10 -
 drivers/mfd/Makefile           |   1 -
 drivers/mfd/twl4030-madc.c     | 922 -----------------------------------------
 6 files changed, 933 insertions(+), 933 deletions(-)
 create mode 100644 drivers/iio/adc/twl4030-madc.c
 delete mode 100644 drivers/mfd/twl4030-madc.c
drivers/iio/adc/Kconfig +10 -0
index 2209f28..427f75c 100644
@@ -183,6 +183,16 @@ config TI_AM335X_ADC
 	  Say yes here to build support for Texas Instruments ADC
 	  driver which is also a MFD client.
 
+config TWL4030_MADC
+	tristate "TWL4030 MADC (Monitoring A/D Converter)"
+	depends on TWL4030_CORE
+	help
+	This driver provides support for Triton TWL4030-MADC. The
+	driver supports both RT and SW conversion methods.
+
+	This driver can also be built as a module. If so, the module will be
+	called twl4030-madc.
+
 config TWL6030_GPADC
 	tristate "TWL6030 GPADC (General Purpose A/D Converter) Support"
 	depends on TWL4030_CORE
drivers/iio/adc/Makefile +1 -0
index ba9a10a..9acf2df 100644
@@ -20,5 +20,6 @@ obj-$(CONFIG_MCP3422) += mcp3422.o
 obj-$(CONFIG_NAU7802) += nau7802.o
 obj-$(CONFIG_TI_ADC081C) += ti-adc081c.o
 obj-$(CONFIG_TI_AM335X_ADC) += ti_am335x_adc.o
+obj-$(CONFIG_TWL4030_MADC) += twl4030-madc.o
 obj-$(CONFIG_TWL6030_GPADC) += twl6030-gpadc.o
 obj-$(CONFIG_VIPERBOARD_ADC) += viperboard_adc.o
drivers/iio/adc/twl4030-madc.c new file +922 -0

922 lines not on this page · raw message

drivers/mfd/Kconfig +0 -10
index 49bb445..23a8a51 100644
@@ -935,16 +935,6 @@ config TWL4030_CORE
 	  high speed USB OTG transceiver, an audio codec (on most
 	  versions) and many other features.
 
-config TWL4030_MADC
-	tristate "TI TWL4030 MADC"
-	depends on TWL4030_CORE
-	help
-	This driver provides support for triton TWL4030-MADC. The
-	driver supports both RT and SW conversion methods.
-
-	This driver can be built as a module. If so it will be
-	named twl4030-madc
-
 config TWL4030_POWER
 	bool "TI TWL4030 power resources"
 	depends on TWL4030_CORE && ARM
drivers/mfd/Makefile +0 -1
index 5aea5ef..c8eb0bc 100644
@@ -71,7 +71,6 @@ obj-$(CONFIG_MFD_TPS80031)	+= tps80031.o
 obj-$(CONFIG_MENELAUS)		+= menelaus.o
 
 obj-$(CONFIG_TWL4030_CORE)	+= twl-core.o twl4030-irq.o twl6030-irq.o
-obj-$(CONFIG_TWL4030_MADC)      += twl4030-madc.o
 obj-$(CONFIG_TWL4030_POWER)    += twl4030-power.o
 obj-$(CONFIG_MFD_TWL4030_AUDIO)	+= twl4030-audio.o
 obj-$(CONFIG_TWL6040_CORE)	+= twl6040.o
drivers/mfd/twl4030-madc.c deleted +0 -922

922 lines not on this page · raw message

Signature · 1 line
1.8.5.3
maintainer
#12 lore raw

Re: [RFCv1 4/4] mfd: twl4030-madc: Move driver to drivers/iio/adc

Quoted 4 lines · Sebastian Reichel

This is a driver for an A/D converter, which belongs into drivers/iio/adc.

Being inherently lazy I'm going to review this patch as it gives the complete driver rather than taking on the conversion patch directly!

It's a long shot, but are there any docs freely available for this part?

Obviously that may well mean that some of my comments apply to the driver in general rather than the changes you've made. Please feel free to pick and choose!

I'd like ideally to see a little more generic tidying up in this driver. As you'll notice inline I get irritated at having lots and lots of error messages. Still that's personal preference but I felt a lot more justified in moaning after finding one that was incorrect ;)

There is also a bit of functionality in here that I'm not sure is ever used (the request callback functions). It complicates the code so if no using it I'd be tempted to drop it.

It's nice in a way that the driver before your conversion provided only a very limited and in kernel interface given we don't have to hang on to any old ABI elements.

Quoted 89 lines
---
  drivers/iio/adc/Kconfig        |  10 +
  drivers/iio/adc/Makefile       |   1 +
  drivers/iio/adc/twl4030-madc.c | 922 +++++++++++++++++++++++++++++++++++++++++
  drivers/mfd/Kconfig            |  10 -
  drivers/mfd/Makefile           |   1 -
  drivers/mfd/twl4030-madc.c     | 922 -----------------------------------------
  6 files changed, 933 insertions(+), 933 deletions(-)

create mode 100644 drivers/iio/adc/twl4030-madc.c delete mode 100644 drivers/mfd/twl4030-madc.c

drivers/iio/adc/Kconfig +10 -0
index 2209f28..427f75c 100644
@@ -183,6 +183,16 @@ config TI_AM335X_ADC
  	  Say yes here to build support for Texas Instruments ADC
  	  driver which is also a MFD client.
 
+config TWL4030_MADC
+	tristate "TWL4030 MADC (Monitoring A/D Converter)"
+	depends on TWL4030_CORE
+	help
+	This driver provides support for Triton TWL4030-MADC. The
+	driver supports both RT and SW conversion methods.
+
+	This driver can also be built as a module. If so, the module will be
+	called twl4030-madc.
+
  config TWL6030_GPADC
  	tristate "TWL6030 GPADC (General Purpose A/D Converter) Support"
  	depends on TWL4030_CORE
drivers/iio/adc/Makefile +1 -0
index ba9a10a..9acf2df 100644
@@ -20,5 +20,6 @@ obj-$(CONFIG_MCP3422) += mcp3422.o
  obj-$(CONFIG_NAU7802) += nau7802.o
  obj-$(CONFIG_TI_ADC081C) += ti-adc081c.o
  obj-$(CONFIG_TI_AM335X_ADC) += ti_am335x_adc.o
+obj-$(CONFIG_TWL4030_MADC) += twl4030-madc.o
  obj-$(CONFIG_TWL6030_GPADC) += twl6030-gpadc.o
  obj-$(CONFIG_VIPERBOARD_ADC) += viperboard_adc.o
drivers/iio/adc/twl4030-madc.c new file +40 -0
new file mode 100644
index 0000000..4da61c4
@@ -0,0 +1,922 @@
+/*
+ *
+ * TWL4030 MADC module driver-This driver monitors the real time
+ * conversion of analog signals like battery temperature,
+ * battery type, battery level etc.
+ *
+ * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
+ * J Keerthy <[email protected]>
+ *
+ * Based on twl4030-madc.c
+ * Copyright (C) 2008 Nokia Corporation
+ * Mikko Ylinen <[email protected]>
+ *
+ * Amit Kucheria <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ *
+ */
+
+#include <linux/init.h>
+#include <linux/device.h>
+#include <linux/interrupt.h>
+#include <linux/kernel.h>
+#include <linux/delay.h>
+#include <linux/platform_device.h>
+#include <linux/slab.h>
+#include <linux/i2c/twl.h>
+#include <linux/i2c/twl4030-madc.h>

drivers/iio/adc/twl4030-madc.c line 50

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+#include <linux/module.h>
+#include <linux/stddef.h>
+#include <linux/mutex.h>
+#include <linux/bitops.h>
+#include <linux/jiffies.h>
+#include <linux/types.h>
+#include <linux/gfp.h>
+#include <linux/err.h>
+
+#include <linux/iio/iio.h>

Why include machine.h or driver.h. You aren't currently using the mapping coding anyway that these provide.

Quoted 6 lines
drivers/iio/adc/twl4030-madc.c new file +6 -0
+#include <linux/iio/machine.h>
+#include <linux/iio/driver.h>
+
+/*
+ * struct twl4030_madc_data - a container for madc info
+ * @dev - pointer to device structure for madc

drivers/iio/adc/twl4030-madc.c line 66

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+ * @lock - mutex protecting this data structure
+ * @requests - Array of request struct corresponding to SW1, SW2 and RT
+ * @imr - Interrupt mask register of MADC
+ * @isr - Interrupt status register of MADC
+ */
+struct twl4030_madc_data {
+	struct device *dev;
+	struct mutex lock;	/* mutex protecting this data structure */
+	struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS];
+	bool use_second_irq;

These are 32 bit signed values?

Quoted 8 lines
drivers/iio/adc/twl4030-madc.c new file +8 -0
+	int imr;
+	int isr;
+};
+
+static int twl4030_madc_read(struct iio_dev *iio_dev,
+			     const struct iio_chan_spec *chan,
+			     int *val, int *val2, long mask)
+{

drivers/iio/adc/twl4030-madc.c line 84

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+	struct twl4030_madc_data *madc = iio_priv(iio_dev);
+	struct twl4030_madc_request req;
+	int channel = chan->channel;
+	int ret;
+
+	req.method = madc->use_second_irq ? TWL4030_MADC_SW2 : TWL4030_MADC_SW1;
+
+	req.channels = BIT(channel);
+	req.active = 0;
+	req.func_cb = NULL;

req.raw = !(mask & IIO_CHAN_INFO_PROCESSED); req.do_avg = !!(mask & IIO_CHAN_INFO_AVERAGE_RAW);

Quoted 7 lines
drivers/iio/adc/twl4030-madc.c new file +7 -0
+	req.raw = (mask & IIO_CHAN_INFO_PROCESSED) ? false : true;
+	req.do_avg = (mask & IIO_CHAN_INFO_AVERAGE_RAW) ? true : false;
+
+	ret = twl4030_madc_conversion(&req);
+	if (ret < 0)
+		return ret;
+

drivers/iio/adc/twl4030-madc.c line 101

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+	*val = req.rbuf[channel];
+
+	return IIO_VAL_INT;
+}
+
+static const struct iio_info twl4030_madc_iio_info = {
+	.read_raw = &twl4030_madc_read,
+	.driver_module = THIS_MODULE,
+};
+

Please give this a prefixed name. Chances of ADC_CHANNEL turning up in an IIO header is a little too high to do this without! e.g. #define TWL4030_ADC_CHANNEL

drivers/iio/adc/twl4030-madc.c line 103

Quoted 2 lines
drivers/iio/adc/twl4030-madc.c new file +2 -0
+#define ADC_CHANNEL(_channel, _type, _name, _mask) {	\
+	.type = _type,					\

I don't think you use scan_type anywhere so don't bother specifying it. here.

drivers/iio/adc/twl4030-madc.c line 110

Quoted 7 lines
drivers/iio/adc/twl4030-madc.c new file +7 -0
+	.scan_type = IIO_ST('u', 10, 16, 0),		\
+	.channel = _channel,				\
+	.info_mask_separate = _mask,			\
+	.datasheet_name = _name,			\
+	.indexed = 1,					\
+}
+

Is the average have an adjustable number of samples?

If not I'd be tempted to use the more common option of IIO_CHAN_INFO_RAW rather than the average version (tends only to be used in fairly obscure cases where both a raw access and an averaged one are available).

Quoted 11 lines
drivers/iio/adc/twl4030-madc.c new file +11 -0
+static const struct iio_chan_spec twl4030_madc_iio_channels[] = {
+	ADC_CHANNEL(0, IIO_VOLTAGE, "ADCIN0", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(1, IIO_TEMP, "ADCIN1", BIT(IIO_CHAN_INFO_PROCESSED) |
+					   BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(2, IIO_VOLTAGE, "ADCIN2", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(3, IIO_VOLTAGE, "ADCIN3", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(4, IIO_VOLTAGE, "ADCIN4", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(5, IIO_VOLTAGE, "ADCIN5", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(6, IIO_VOLTAGE, "ADCIN6", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(7, IIO_VOLTAGE, "ADCIN7", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(8, IIO_VOLTAGE, "ADCIN8", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),

drivers/iio/adc/twl4030-madc.c line 131

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+	ADC_CHANNEL(9, IIO_VOLTAGE, "ADCIN9", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(10, IIO_CURRENT, "ADCIN10", BIT(IIO_CHAN_INFO_PROCESSED) |
+						BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(11, IIO_VOLTAGE, "ADCIN11", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(12, IIO_VOLTAGE, "ADCIN12", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(13, IIO_VOLTAGE, "ADCIN13", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(14, IIO_VOLTAGE, "ADCIN14", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(15, IIO_VOLTAGE, "ADCIN15", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+};
+

Why the artificial limitation of one of these devices? I guess this is to allow the exported function to work without needing to be associated with any particular device... Hmm.

Quoted 104 lines
drivers/iio/adc/twl4030-madc.c new file +104 -0
+static struct twl4030_madc_data *twl4030_madc;
+
+struct twl4030_prescale_divider_ratios {
+	s16 numerator;
+	s16 denominator;
+};
+
+static const struct twl4030_prescale_divider_ratios
+twl4030_divider_ratios[16] = {
+	{1, 1},		/* CHANNEL 0 No Prescaler */
+	{1, 1},		/* CHANNEL 1 No Prescaler */
+	{6, 10},	/* CHANNEL 2 */
+	{6, 10},	/* CHANNEL 3 */
+	{6, 10},	/* CHANNEL 4 */
+	{6, 10},	/* CHANNEL 5 */
+	{6, 10},	/* CHANNEL 6 */
+	{6, 10},	/* CHANNEL 7 */
+	{3, 14},	/* CHANNEL 8 */
+	{1, 3},		/* CHANNEL 9 */
+	{1, 1},		/* CHANNEL 10 No Prescaler */
+	{15, 100},	/* CHANNEL 11 */
+	{1, 4},		/* CHANNEL 12 */
+	{1, 1},		/* CHANNEL 13 Reserved channels */
+	{1, 1},		/* CHANNEL 14 Reseved channels */
+	{5, 11},	/* CHANNEL 15 */
+};
+
+
+/*
+ * Conversion table from -3 to 55 degree Celcius
+ */
+static int therm_tbl[] = {
+30800,	29500,	28300,	27100,
+26000,	24900,	23900,	22900,	22000,	21100,	20300,	19400,	18700,	17900,
+17200,	16500,	15900,	15300,	14700,	14100,	13600,	13100,	12600,	12100,
+11600,	11200,	10800,	10400,	10000,	9630,	9280,	8950,	8620,	8310,
+8020,	7730,	7460,	7200,	6950,	6710,	6470,	6250,	6040,	5830,
+5640,	5450,	5260,	5090,	4920,	4760,	4600,	4450,	4310,	4170,
+4040,	3910,	3790,	3670,	3550
+};
+
+/*
+ * Structure containing the registers
+ * of different conversion methods supported by MADC.
+ * Hardware or RT real time conversion request initiated by external host
+ * processor for RT Signal conversions.
+ * External host processors can also request for non RT conversions
+ * SW1 and SW2 software conversions also called asynchronous or GPC request.
+ */
+static
+const struct twl4030_madc_conversion_method twl4030_conversion_methods[] = {
+	[TWL4030_MADC_RT] = {
+			     .sel = TWL4030_MADC_RTSELECT_LSB,
+			     .avg = TWL4030_MADC_RTAVERAGE_LSB,
+			     .rbase = TWL4030_MADC_RTCH0_LSB,
+			     },
+	[TWL4030_MADC_SW1] = {
+			      .sel = TWL4030_MADC_SW1SELECT_LSB,
+			      .avg = TWL4030_MADC_SW1AVERAGE_LSB,
+			      .rbase = TWL4030_MADC_GPCH0_LSB,
+			      .ctrl = TWL4030_MADC_CTRL_SW1,
+			      },
+	[TWL4030_MADC_SW2] = {
+			      .sel = TWL4030_MADC_SW2SELECT_LSB,
+			      .avg = TWL4030_MADC_SW2AVERAGE_LSB,
+			      .rbase = TWL4030_MADC_GPCH0_LSB,
+			      .ctrl = TWL4030_MADC_CTRL_SW2,
+			      },
+};
+
+/*
+ * Function to read a particular channel value.
+ * @madc - pointer to struct twl4030_madc_data
+ * @reg - lsb of ADC Channel
+ * If the i2c read fails it returns an error else returns 0.
+ */
+static int twl4030_madc_channel_raw_read(struct twl4030_madc_data *madc, u8 reg)
+{
+	u8 msb, lsb;
+	int ret;
+	/*
+	 * For each ADC channel, we have MSB and LSB register pair. MSB address
+	 * is always LSB address+1. reg parameter is the address of LSB register
+	 */
+	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &msb, reg + 1);
+	if (ret) {
+		dev_err(madc->dev, "unable to read MSB register 0x%X\n",
+			reg + 1);
+		return ret;
+	}
+	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &lsb, reg);
+	if (ret) {
+		dev_err(madc->dev, "unable to read LSB register 0x%X\n", reg);
+		return ret;
+	}
+
+	return (int)(((msb << 8) | lsb) >> 6);
+}
+
+/*
+ * Return battery temperature
+ * Or < 0 on failure.
+ */
+static int twl4030battery_temperature(int raw_volt)

drivers/iio/adc/twl4030-madc.c line 245

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+{
+	u8 val;
+	int temp, curr, volt, res, ret;
+
+	volt = (raw_volt * TEMP_STEP_SIZE) / TEMP_PSR_R;
+	/* Getting and calculating the supply current in micro ampers */
+	ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val,
+		REG_BCICTL2);
+	if (ret < 0)
+		return ret;

blank line here and after similar error cases makes it a little easier to read.

drivers/iio/adc/twl4030-madc.c line 252

Quoted 7 lines
drivers/iio/adc/twl4030-madc.c new file +7 -0
+	curr = ((val & TWL4030_BCI_ITHEN) + 1) * 10;
+	/* Getting and calculating the thermistor resistance in ohms */
+	res = volt * 1000 / curr;
+	/* calculating temperature */
+	for (temp = 58; temp >= 0; temp--) {
+		int actual = therm_tbl[temp];
+

No blank line here.

Quoted 11 lines
drivers/iio/adc/twl4030-madc.c new file +11 -0
+		if ((actual - res) >= 0)
+			break;
+	}
+
+	return temp + 1;
+}
+
+static int twl4030battery_current(int raw_volt)
+{
+	int ret;
+	u8 val;

drivers/iio/adc/twl4030-madc.c line 273

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+
+	ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val,
+		TWL4030_BCI_BCICTL1);
+	if (ret)
+		return ret;
+	if (val & TWL4030_BCI_CGAIN) /* slope of 0.44 mV/mA */
+		return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R1;
+	else /* slope of 0.88 mV/mA */
+		return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R2;
+}

blank line here please.

Various comments on this in the next function:
This would be much simpler if any error immediately resulted in an exit with error code. If it's gone wrong enough to issue a dev_err then don't try to muddle through. Given there is nothing to clear up in here, just error out directly on the first problem.

Quoted 9 lines
drivers/iio/adc/twl4030-madc.c new file +9 -0
+/*
+ * Function to read channel values
+ * @madc - pointer to twl4030_madc_data struct
+ * @reg_base - Base address of the first channel
+ * @Channels - 16 bit bitmap. If the bit is set, channel value is read
+ * @buf - The channel values are stored here. if read fails error
+ * @raw - Return raw values without conversion
+ * value is stored
+ * Returns the number of successfully read channels.

drivers/iio/adc/twl4030-madc.c line 292

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+ */
+static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
+				      u8 reg_base, unsigned
+				      long channels, int *buf,
+				      bool raw)
+{
+	int count = 0, count_req = 0, i;
+	u8 reg;
+
+	for_each_set_bit(i, &channels, TWL4030_MADC_MAX_CHANNELS) {

Skip the temporary for reg. Yes, it gets used twice, but easier to read inline.

drivers/iio/adc/twl4030-madc.c line 300

Quoted 8 lines
drivers/iio/adc/twl4030-madc.c new file +8 -0
+		reg = reg_base + 2 * i;
+		buf[i] = twl4030_madc_channel_raw_read(madc, reg);
+		if (buf[i] < 0) {
+			dev_err(madc->dev,
+				"Unable to read register 0x%X\n", reg);
+			count_req++;
+			continue;
+		}

I'd prefer this as
if (raw) {
count ++;
} else {
switch (i) {
..
}
Also near as I can tell count gets incremented unless there is an error anyway. So just increment it outside and allow this function to return the error code.

drivers/iio/adc/twl4030-madc.c line 305

Quoted 5 lines
drivers/iio/adc/twl4030-madc.c new file +5 -0
+		if (raw) {
+			count++;
+			continue;
+		}
+		switch (i) {

These cases could do with a suitable #define to give them a name.

Quoted 25 lines
drivers/iio/adc/twl4030-madc.c new file +25 -0
+		case 10:
+			buf[i] = twl4030battery_current(buf[i]);
+			if (buf[i] < 0) {
+				dev_err(madc->dev, "err reading current\n");
+				count_req++;
+			} else {
+				count++;
+				buf[i] = buf[i] - 750;
+			}
+			break;
+		case 1:
+			buf[i] = twl4030battery_temperature(buf[i]);
+			if (buf[i] < 0) {
+				dev_err(madc->dev, "err reading temperature\n");
+				count_req++;
+			} else {
+				buf[i] -= 3;
+				count++;
+			}
+			break;
+		default:
+			count++;
+			/* Analog Input (V) = conv_result * step_size / R
+			 * conv_result = decimal value of 10-bit conversion
+			 *		 result

drivers/iio/adc/twl4030-madc.c line 340

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+			 * step size = 1.5 / (2 ^ 10 -1)
+			 * R = Prescaler ratio for input channels.
+			 * Result given in mV hence multiplied by 1000.
+			 */
+			buf[i] = (buf[i] * 3 * 1000 *
+				 twl4030_divider_ratios[i].denominator)
+				/ (2 * 1023 *
+				twl4030_divider_ratios[i].numerator);
+		}
+	}

This is already apparant from the errors emmited earlier. I'd drop this and the count_req counter in general.
Personally I'd error out in those cases anyway rather than carrying on with the rest of the channels. If it's worth of a dev_err it's worthy of getting out as fast as possible rather than trying to muddle on.

Quoted 15 lines
drivers/iio/adc/twl4030-madc.c new file +15 -0
+	if (count_req)
+		dev_err(madc->dev, "%d channel conversion failed\n", count_req);
+
+	return count;
+}
+
+/*
+ * Enables irq.
+ * @madc - pointer to twl4030_madc_data struct
+ * @id - irq number to be enabled
+ * can take one of TWL4030_MADC_RT, TWL4030_MADC_SW1, TWL4030_MADC_SW2
+ * corresponding to RT, SW1, SW2 conversion requests.
+ * If the i2c read fails it returns an error else returns 0.
+ */
+static int twl4030_madc_enable_irq(struct twl4030_madc_data *madc, u8 id)

drivers/iio/adc/twl4030-madc.c line 365

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+{
+	u8 val;
+	int ret;
+
+	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr);
+	if (ret) {
+		dev_err(madc->dev, "unable to read imr register 0x%X\n",
+			madc->imr);
+		return ret;
+	}

A blank line here and in similar cases would slight aid readability.

drivers/iio/adc/twl4030-madc.c line 372

Quoted 7 lines
drivers/iio/adc/twl4030-madc.c new file +7 -0
+	val &= ~(1 << id);
+	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr);
+	if (ret) {
+		dev_err(madc->dev,
+			"unable to write imr register 0x%X\n", madc->imr);
+		return ret;
+

Loose blank line here.

Quoted 55 lines
drivers/iio/adc/twl4030-madc.c new file +55 -0
+	}
+
+	return 0;
+}
+
+/*
+ * Disables irq.
+ * @madc - pointer to twl4030_madc_data struct
+ * @id - irq number to be disabled
+ * can take one of TWL4030_MADC_RT, TWL4030_MADC_SW1, TWL4030_MADC_SW2
+ * corresponding to RT, SW1, SW2 conversion requests.
+ * Returns error if i2c read/write fails.
+ */
+static int twl4030_madc_disable_irq(struct twl4030_madc_data *madc, u8 id)
+{
+	u8 val;
+	int ret;
+
+	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr);
+	if (ret) {
+		dev_err(madc->dev, "unable to read imr register 0x%X\n",
+			madc->imr);
+		return ret;
+	}
+	val |= (1 << id);
+	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr);
+	if (ret) {
+		dev_err(madc->dev,
+			"unable to write imr register 0x%X\n", madc->imr);
+		return ret;
+	}
+
+	return 0;
+}
+
+static irqreturn_t twl4030_madc_threaded_irq_handler(int irq, void *_madc)
+{
+	struct twl4030_madc_data *madc = _madc;
+	const struct twl4030_madc_conversion_method *method;
+	u8 isr_val, imr_val;
+	int i, len, ret;
+	struct twl4030_madc_request *r;
+
+	mutex_lock(&madc->lock);
+	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &isr_val, madc->isr);
+	if (ret) {
+		dev_err(madc->dev, "unable to read isr register 0x%X\n",
+			madc->isr);
+		goto err_i2c;
+	}
+	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &imr_val, madc->imr);
+	if (ret) {
+		dev_err(madc->dev, "unable to read imr register 0x%X\n",
+			madc->imr);
+		goto err_i2c;

drivers/iio/adc/twl4030-madc.c line 437

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+	}
+	isr_val &= ~imr_val;
+	for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
+		if (!(isr_val & (1 << i)))
+			continue;
+		ret = twl4030_madc_disable_irq(madc, i);
+		if (ret < 0)
+			dev_dbg(madc->dev, "Disable interrupt failed%d\n", i);
+		madc->requests[i].result_pending = 1;
+	}

Can this and the previous loop not be combined thus simplifying some tests?

Quoted 39 lines
drivers/iio/adc/twl4030-madc.c new file +39 -0
+	for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
+		r = &madc->requests[i];
+		/* No pending results for this method, move to next one */
+		if (!r->result_pending)
+			continue;
+		method = &twl4030_conversion_methods[r->method];
+		/* Read results */
+		len = twl4030_madc_read_channels(madc, method->rbase,
+						 r->channels, r->rbuf, r->raw);
+		/* Return results to caller */
+		if (r->func_cb != NULL) {
+			r->func_cb(len, r->channels, r->rbuf);
+			r->func_cb = NULL;
+		}
+		/* Free request */
+		r->result_pending = 0;
+		r->active = 0;
+	}
+	mutex_unlock(&madc->lock);
+
+	return IRQ_HANDLED;
+
+err_i2c:
+	/*
+	 * In case of error check whichever request is active
+	 * and service the same.
+	 */
+	for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
+		r = &madc->requests[i];
+		if (r->active == 0)
+			continue;
+		method = &twl4030_conversion_methods[r->method];
+		/* Read results */
+		len = twl4030_madc_read_channels(madc, method->rbase,
+						 r->channels, r->rbuf, r->raw);
+		/* Return results to caller */
+		if (r->func_cb != NULL) {
+			r->func_cb(len, r->channels, r->rbuf);
+			r->func_cb = NULL;

drivers/iio/adc/twl4030-madc.c line 486

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+		}
+		/* Free request */
+		r->result_pending = 0;
+		r->active = 0;
+	}
+	mutex_unlock(&madc->lock);
+
+	return IRQ_HANDLED;
+}
+

This structure does seem a little complex. I'd have been more tempted by using a completion and having the calling function wait on it. How often does an actual callback make sense?

Quoted 22 lines
drivers/iio/adc/twl4030-madc.c new file +22 -0
+static int twl4030_madc_set_irq(struct twl4030_madc_data *madc,
+				struct twl4030_madc_request *req)
+{
+	struct twl4030_madc_request *p;
+	int ret;
+
+	p = &madc->requests[req->method];
+	memcpy(p, req, sizeof(*req));
+	ret = twl4030_madc_enable_irq(madc, req->method);
+	if (ret < 0) {
+		dev_err(madc->dev, "enable irq failed!!\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+/*
+ * Function which enables the madc conversion
+ * by writing to the control register.
+ * @madc - pointer to twl4030_madc_data struct
+ * @conv_method - can be TWL4030_MADC_RT, TWL4030_MADC_SW2, TWL4030_MADC_SW1

drivers/iio/adc/twl4030-madc.c line 518

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+ * corresponding to RT SW1 or SW2 conversion methods.
+ * Returns 0 if succeeds else a negative error value
+ */
+static int twl4030_madc_start_conversion(struct twl4030_madc_data *madc,
+					 int conv_method)
+{
+	const struct twl4030_madc_conversion_method *method;
+	int ret = 0;
+	method = &twl4030_conversion_methods[conv_method];
+	switch (conv_method) {

Can we get here via any paths where these aren't the methods set?

Quoted 8 lines
drivers/iio/adc/twl4030-madc.c new file +8 -0
+	case TWL4030_MADC_SW1:
+	case TWL4030_MADC_SW2:
+		ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
+				       TWL4030_MADC_SW_START, method->ctrl);
+		if (ret) {
+			dev_err(madc->dev,
+				"unable to write ctrl register 0x%X\n",
+				method->ctrl);

drivers/iio/adc/twl4030-madc.c line 536

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+			return ret;
+		}
+		break;
+	default:
+		break;
+	}
+
+	return 0;
+}
+

Could we fix up the kernel doc in this file in general. It's so nearly there but not quite!

Quoted 27 lines
drivers/iio/adc/twl4030-madc.c new file +27 -0
+/*
+ * Function that waits for conversion to be ready
+ * @madc - pointer to twl4030_madc_data struct
+ * @timeout_ms - timeout value in milliseconds
+ * @status_reg - ctrl register
+ * returns 0 if succeeds else a negative error value
+ */
+static int twl4030_madc_wait_conversion_ready(struct twl4030_madc_data *madc,
+					      unsigned int timeout_ms,
+					      u8 status_reg)
+{
+	unsigned long timeout;
+	int ret;
+
+	timeout = jiffies + msecs_to_jiffies(timeout_ms);
+	do {
+		u8 reg;
+
+		ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &reg, status_reg);
+		if (ret) {
+			dev_err(madc->dev,
+				"unable to read status register 0x%X\n",
+				status_reg);
+			return ret;
+		}
+		if (!(reg & TWL4030_MADC_BUSY) && (reg & TWL4030_MADC_EOC_SW))
+			return 0;

drivers/iio/adc/twl4030-madc.c line 572

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +9 -0
+		usleep_range(500, 2000);
+	} while (!time_after(jiffies, timeout));
+	dev_err(madc->dev, "conversion timeout!\n");
+
+	return -EAGAIN;
+}
+
 
+/*
+ * An exported function which can be called from other kernel drivers.

What uses this currently? Ideally we'd do this through the standard IIO paths for in kernel users. Right now the device tree mappings for that are less than ideal (ask Mark Rutland if you want some choice comments ;) Perhaps we take the view this can be cleaned up later. There are some elements in here we haven't yet looked at supporting for client drivers. Will have to think about that.

It would be particularly nice if possible to use a generic battery driver for the two cases that seem to be using this functionality at the moment.

Quoted 45 lines

+ * @req twl4030_madc_request structure
+ * req->rbuf will be filled with read values of channels based on the
+ * channel index. If a particular channel reading fails there will
+ * be a negative error value in the corresponding array element.
+ * returns 0 if succeeds else error value
+ */

drivers/iio/adc/twl4030-madc.c new file +39 -0
+int twl4030_madc_conversion(struct twl4030_madc_request *req)
+{
+	const struct twl4030_madc_conversion_method *method;
+	u8 ch_msb, ch_lsb;
+	int ret;
+
+	if (!req || !twl4030_madc)
+		return -EINVAL;
+
+	mutex_lock(&twl4030_madc->lock);
+	if (req->method < TWL4030_MADC_RT || req->method > TWL4030_MADC_SW2) {
+		ret = -EINVAL;
+		goto out;
+	}
+	/* Do we have a conversion request ongoing */
+	if (twl4030_madc->requests[req->method].active) {
+		ret = -EBUSY;
+		goto out;
+	}
+	ch_msb = (req->channels >> 8) & 0xff;
+	ch_lsb = req->channels & 0xff;
+	method = &twl4030_conversion_methods[req->method];
+	/* Select channels to be converted */
+	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_msb, method->sel + 1);
+	if (ret) {
+		dev_err(twl4030_madc->dev,
+			"unable to write sel register 0x%X\n", method->sel + 1);
+		goto out;
+	}
+	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_lsb, method->sel);
+	if (ret) {
+		dev_err(twl4030_madc->dev,
+			"unable to write sel register 0x%X\n", method->sel + 1);
+		goto out;
+	}
+	/* Select averaging for all channels if do_avg is set */
+	if (req->do_avg) {
+		ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
+				       ch_msb, method->avg + 1);

drivers/iio/adc/twl4030-madc.c line 627

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+		if (ret) {
+			dev_err(twl4030_madc->dev,
+				"unable to write avg register 0x%X\n",
+				method->avg + 1);
+			goto out;
+		}
+		ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
+				       ch_lsb, method->avg);
+		if (ret) {
+			dev_err(twl4030_madc->dev,

Excesive error messages are sometimes irritating as they break up code flow. They are really irritating when incorrect ;)

Quoted 6 lines
drivers/iio/adc/twl4030-madc.c new file +6 -0
+				"unable to write sel reg 0x%X\n",
+				method->sel + 1);
+			goto out;
+		}
+	}
+	if (req->type == TWL4030_MADC_IRQ_ONESHOT && req->func_cb != NULL) {

drivers/iio/adc/twl4030-madc.c line 643

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+		ret = twl4030_madc_set_irq(twl4030_madc, req);
+		if (ret < 0)
+			goto out;
+		ret = twl4030_madc_start_conversion(twl4030_madc, req->method);
+		if (ret < 0)
+			goto out;
+		twl4030_madc->requests[req->method].active = 1;
+		ret = 0;
+		goto out;
+	}

Is it possible to get here? This comment suggests not. If so, why have the test?

drivers/iio/adc/twl4030-madc.c line 653

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+	/* With RT method we should not be here anymore */
+	if (req->method == TWL4030_MADC_RT) {
+		ret = -EINVAL;
+		goto out;
+	}
+	ret = twl4030_madc_start_conversion(twl4030_madc, req->method);
+	if (ret < 0)
+		goto out;
+	twl4030_madc->requests[req->method].active = 1;
+	/* Wait until conversion is ready (ctrl register returns EOC) */

So no interrupts in this case?

Quoted 7 lines
drivers/iio/adc/twl4030-madc.c new file +7 -0
+	ret = twl4030_madc_wait_conversion_ready(twl4030_madc, 5, method->ctrl);
+	if (ret) {
+		twl4030_madc->requests[req->method].active = 0;
+		goto out;
+	}
+	ret = twl4030_madc_read_channels(twl4030_madc, method->rbase,
+					 req->channels, req->rbuf, req->raw);

drivers/iio/adc/twl4030-madc.c line 670

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+	twl4030_madc->requests[req->method].active = 0;
+
+out:
+	mutex_unlock(&twl4030_madc->lock);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(twl4030_madc_conversion);
+
+/*

Proper kerneldoc would be nice here. Otherwise the comment doesn't really add anything so I'd be tempted to drop it ;)

Quoted 18 lines

+ * Return channel value
+ * Or < 0 on failure.
+ */

drivers/iio/adc/twl4030-madc.c new file +15 -0
+int twl4030_get_madc_conversion(int channel_no)
+{
+	struct twl4030_madc_request req;
+	int temp = 0;
+	int ret;
+
+	req.channels = (1 << channel_no);
+	req.method = TWL4030_MADC_SW2;
+	req.active = 0;
+	req.func_cb = NULL;
+	ret = twl4030_madc_conversion(&req);
+	if (ret < 0)
+		return ret;
+	if (req.rbuf[channel_no] > 0)
+		temp = req.rbuf[channel_no];

drivers/iio/adc/twl4030-madc.c line 698

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+
+	return temp;
+}
+EXPORT_SYMBOL_GPL(twl4030_get_madc_conversion);
+
+/*
+ * Function to enable or disable bias current for
+ * main battery type reading or temperature sensing
+ * @madc - pointer to twl4030_madc_data struct
+ * @chan - can be one of the two values

What is a battery type reading? Voltage? Current? Charge?

Quoted 2 lines

+ * TWL4030_BCI_ITHEN - Enables bias current for main battery type reading
+ * TWL4030_BCI_TYPEN - Enables bias current for main battery temperature

These constants have rather incomprehensible names. Whilst I gues they are straight off the data sheet, seems to me that we could make them a little longer and easier to follow! The comment here helps, but sensible names would be better!

Quoted 6 lines

+ * sensing
+ * @on - enable or disable chan.
+ */

drivers/iio/adc/twl4030-madc.c new file +3 -0
+static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc,
+					      int chan, int on)
+{

drivers/iio/adc/twl4030-madc.c line 716

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+	int ret;
+	u8 regval;
+
+	ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE,
+			      &regval, TWL4030_BCI_BCICTL1);
+	if (ret) {
+		dev_err(madc->dev, "unable to read BCICTL1 reg 0x%X",
+			TWL4030_BCI_BCICTL1);
+		return ret;
+	}

Introduce an intermediate variable and this could be nice and easy to read Say

int regmask;
if (chan == 0)
regmask = TWL4030_BCI_TYPEN;
else
regmask = TWL4030_BFI_ITHEN;

if (on)
regval |= regmask;
else
regval &= ~regmask;

It's longer but doesn't give me a headache.

Quoted 113 lines
drivers/iio/adc/twl4030-madc.c new file +113 -0
+	if (on)
+		regval |= chan ? TWL4030_BCI_ITHEN : TWL4030_BCI_TYPEN;
+	else
+		regval &= chan ? ~TWL4030_BCI_ITHEN : ~TWL4030_BCI_TYPEN;
+	ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
+			       regval, TWL4030_BCI_BCICTL1);
+	if (ret) {
+		dev_err(madc->dev, "unable to write BCICTL1 reg 0x%X\n",
+			TWL4030_BCI_BCICTL1);
+		return ret;
+	}
+
+	return 0;
+}
+
+/*
+ * Function that sets MADC software power on bit to enable MADC
+ * @madc - pointer to twl4030_madc_data struct
+ * @on - Enable or disable MADC software powen on bit.
+ * returns error if i2c read/write fails else 0
+ */
+static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on)
+{
+	u8 regval;
+	int ret;
+
+	ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE,
+			      &regval, TWL4030_MADC_CTRL1);
+	if (ret) {
+		dev_err(madc->dev, "unable to read madc ctrl1 reg 0x%X\n",
+			TWL4030_MADC_CTRL1);
+		return ret;
+	}
+	if (on)
+		regval |= TWL4030_MADC_MADCON;
+	else
+		regval &= ~TWL4030_MADC_MADCON;
+	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, regval, TWL4030_MADC_CTRL1);
+	if (ret) {
+		dev_err(madc->dev, "unable to write madc ctrl1 reg 0x%X\n",
+			TWL4030_MADC_CTRL1);
+		return ret;
+	}
+
+	return 0;
+}
+
+/*
+ * Initialize MADC and request for threaded irq
+ */
+static int twl4030_madc_probe(struct platform_device *pdev)
+{
+	struct twl4030_madc_data *madc;
+	struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev);
+	struct device_node *np = pdev->dev.of_node;
+	int irq, ret;
+	u8 regval;
+	struct iio_dev *iio_dev = NULL;
+
+	if (!pdata && !np) {
+		dev_err(&pdev->dev, "platform_data not available\n");
+		return -EINVAL;
+	}
+
+	iio_dev = devm_iio_device_alloc(&pdev->dev,
+					sizeof(struct twl4030_madc_data));
+	if (!iio_dev) {
+		dev_err(&pdev->dev, "failed allocating iio device\n");
+		return -ENOMEM;
+	}
+
+	madc = iio_priv(iio_dev);
+	madc->dev = &pdev->dev;
+
+	iio_dev->name = dev_name(&pdev->dev);
+	iio_dev->dev.parent = &pdev->dev;
+	iio_dev->dev.of_node = pdev->dev.of_node;
+	iio_dev->info = &twl4030_madc_iio_info;
+	iio_dev->modes = INDIO_DIRECT_MODE;
+	iio_dev->channels = twl4030_madc_iio_channels;
+	iio_dev->num_channels = 16;
+
+	/*
+	 * Phoenix provides 2 interrupt lines. The first one is connected to
+	 * the OMAP. The other one can be connected to the other processor such
+	 * as modem. Hence two separate ISR and IMR registers.
+	 */
+	if (pdata)
+		madc->use_second_irq = pdata->irq_line != 1;
+	else
+		madc->use_second_irq = false;
+
+	madc->imr = (madc->use_second_irq == 1) ?
+	    TWL4030_MADC_IMR1 : TWL4030_MADC_IMR2;
+	madc->isr = (madc->use_second_irq == 1) ?
+	    TWL4030_MADC_ISR1 : TWL4030_MADC_ISR2;
+
+	ret = twl4030_madc_set_power(madc, 1);
+	if (ret < 0)
+		return ret;
+	ret = twl4030_madc_set_current_generator(madc, 0, 1);
+	if (ret < 0)
+		goto err_current_generator;
+
+	ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE,
+			      &regval, TWL4030_BCI_BCICTL1);
+	if (ret) {
+		dev_err(&pdev->dev, "unable to read reg BCI CTL1 0x%X\n",
+			TWL4030_BCI_BCICTL1);
+		goto err_i2c;
+	}
+	regval |= TWL4030_BCI_MESBAT;
+	ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,

drivers/iio/adc/twl4030-madc.c line 839

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+			       regval, TWL4030_BCI_BCICTL1);
+	if (ret) {
+		dev_err(&pdev->dev, "unable to write reg BCI Ctl1 0x%X\n",
+			TWL4030_BCI_BCICTL1);
+		goto err_i2c;
+	}
+
+	/* Check that MADC clock is on */
+	ret = twl_i2c_read_u8(TWL4030_MODULE_INTBR, &regval, TWL4030_REG_GPBR1);
+	if (ret) {

Personally I'd rank the driver as rather to vebose on read error messages given they are pretty unusual. Ah well I guess each to their own.

Quoted 12 lines
drivers/iio/adc/twl4030-madc.c new file +12 -0
+		dev_err(&pdev->dev, "unable to read reg GPBR1 0x%X\n",
+				TWL4030_REG_GPBR1);
+		goto err_i2c;
+	}
+
+	/* If MADC clk is not on, turn it on */
+	if (!(regval & TWL4030_GPBR1_MADC_HFCLK_EN)) {
+		dev_info(&pdev->dev, "clk disabled, enabling\n");
+		regval |= TWL4030_GPBR1_MADC_HFCLK_EN;
+		ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, regval,
+				       TWL4030_REG_GPBR1);
+		if (ret) {

drivers/iio/adc/twl4030-madc.c line 861

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+			dev_err(&pdev->dev, "unable to write reg GPBR1 0x%X\n",
+					TWL4030_REG_GPBR1);
+			goto err_i2c;
+		}
+	}
+
+	platform_set_drvdata(pdev, iio_dev);
+	mutex_init(&madc->lock);
+
+	irq = platform_get_irq(pdev, 0);

As ever using devm for a irq request makes for a bit of a review nightmare as we have to be very careful that nothing has been removed that this might use before the devm cleanup occurs (at end of remove). In this case the fact that the current generator is off and the adc unit is disabled sounds to me like it might cause interesting results in the interrupt handler?

It's one of those things that will probably never happen but I find it hard to convince myself 'can't happen'

drivers/iio/adc/twl4030-madc.c line 868

Quoted 7 lines
drivers/iio/adc/twl4030-madc.c new file +7 -0
+	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
+				   twl4030_madc_threaded_irq_handler,
+				   IRQF_TRIGGER_RISING, "twl4030_madc", madc);
+	if (ret) {
+		dev_dbg(&pdev->dev, "could not request irq\n");
+		goto err_i2c;
+	}

Err, where is this defined? Ah, found it above. I'm not keen on preventing multiple instances of a device like this. It is so easy to not do this I'd prefer to see the driver fixed to remove this.

Quoted 44 lines
drivers/iio/adc/twl4030-madc.c new file +44 -0
+	twl4030_madc = madc;
+
+	ret = iio_device_register(iio_dev);
+	if (ret) {
+		dev_dbg(&pdev->dev, "could not register iio device\n");
+		goto err_i2c;
+	}
+
+	return 0;
+
+err_i2c:
+	twl4030_madc_set_current_generator(madc, 0, 0);
+err_current_generator:
+	twl4030_madc_set_power(madc, 0);
+	return ret;
+}
+
+static int twl4030_madc_remove(struct platform_device *pdev)
+{
+	struct iio_dev *iio_dev = platform_get_drvdata(pdev);
+	struct twl4030_madc_data *madc = iio_priv(iio_dev);
+
+	twl4030_madc_set_current_generator(madc, 0, 0);
+	twl4030_madc_set_power(madc, 0);
+
+	iio_device_unregister(iio_dev);
+
+	return 0;
+}
+
+#ifdef CONFIG_OF
+static const struct of_device_id twl_madc_of_match[] = {
+	{.compatible = "ti,twl4030-madc", },
+	{ },
+};
+MODULE_DEVICE_TABLE(of, twl_madc_of_match);
+#endif
+
+static struct platform_driver twl4030_madc_driver = {
+	.probe = twl4030_madc_probe,
+	.remove = twl4030_madc_remove,
+	.driver = {
+		   .name = "twl4030_madc",
+		   .owner = THIS_MODULE,

drivers/iio/adc/twl4030-madc.c line 922

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+		   .of_match_table = of_match_ptr(twl_madc_of_match),
+		   },
+};
+
+module_platform_driver(twl4030_madc_driver);
+
+MODULE_DESCRIPTION("TWL4030 ADC driver");
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("J Keerthy");
+MODULE_ALIAS("platform:twl4030_madc");

Rest of patch was removal of code so no comments!

#13 lore raw

Re: [RFCv1 4/4] mfd: twl4030-madc: Move driver to drivers/iio/adc

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

This is a driver for an A/D converter, which belongs into drivers/iio/adc.

Being inherently lazy I'm going to review this patch as it gives the complete driver rather than taking on the conversion patch directly!

OK.

Quoted 1 line

It's a long shot, but are there any docs freely available for this part?

There is documentation on Texas Instrument's website for TPS65950, which is supposed to be very similar to the TWL4030 and contains a section about the MADC module:

http://www.ti.com/lit/gpn/tps65950

Quoted 3 lines

Obviously that may well mean that some of my comments apply to the driver in general rather than the changes you've made. Please feel free to pick and choose!

Having skipped over your comments I guess 70% are about the driver in general, but I can add another patch to the patchset, which fixes the driver style.

Quoted 8 lines

I'd like ideally to see a little more generic tidying up in this driver. As you'll notice inline I get irritated at having lots and lots of error messages. Still that's personal preference but I felt a lot more justified in moaning after finding one that was incorrect ;)

There is also a bit of functionality in here that I'm not sure is ever used (the request callback functions). It complicates the code so if no using it I'd be tempted to drop it.

I think the optimal workflow is:

1. convert madc driver to IIO
2. convert twl4030-madc-battery driver to IIO API
3. convert rx51-battery to IIO API
4. convert twl4030-madc-hwmon to IIO API / deprecate it
5. remove old in-kernel ABI from madc
6. cleanup/simplify madc

I guess its much simpler to do the driver cleanup/simplification once we can get rid of the old API.

Quoted 97 lines

It's nice in a way that the driver before your conversion provided only a very limited and in kernel interface given we don't have to hang on to any old ABI elements.

Quoted 93 lines
---
 drivers/iio/adc/Kconfig        |  10 +
 drivers/iio/adc/Makefile       |   1 +
 drivers/iio/adc/twl4030-madc.c | 922 +++++++++++++++++++++++++++++++++++++++++
 drivers/mfd/Kconfig            |  10 -
 drivers/mfd/Makefile           |   1 -
 drivers/mfd/twl4030-madc.c     | 922 -----------------------------------------
 6 files changed, 933 insertions(+), 933 deletions(-)
 create mode 100644 drivers/iio/adc/twl4030-madc.c
 delete mode 100644 drivers/mfd/twl4030-madc.c
drivers/iio/adc/Kconfig +10 -0
index 2209f28..427f75c 100644
@@ -183,6 +183,16 @@ config TI_AM335X_ADC
 	  Say yes here to build support for Texas Instruments ADC
 	  driver which is also a MFD client.
 
+config TWL4030_MADC
+	tristate "TWL4030 MADC (Monitoring A/D Converter)"
+	depends on TWL4030_CORE
+	help
+	This driver provides support for Triton TWL4030-MADC. The
+	driver supports both RT and SW conversion methods.
+
+	This driver can also be built as a module. If so, the module will be
+	called twl4030-madc.
+
 config TWL6030_GPADC
 	tristate "TWL6030 GPADC (General Purpose A/D Converter) Support"
 	depends on TWL4030_CORE
drivers/iio/adc/Makefile +1 -0
index ba9a10a..9acf2df 100644
@@ -20,5 +20,6 @@ obj-$(CONFIG_MCP3422) += mcp3422.o
 obj-$(CONFIG_NAU7802) += nau7802.o
 obj-$(CONFIG_TI_ADC081C) += ti-adc081c.o
 obj-$(CONFIG_TI_AM335X_ADC) += ti_am335x_adc.o
+obj-$(CONFIG_TWL4030_MADC) += twl4030-madc.o
 obj-$(CONFIG_TWL6030_GPADC) += twl6030-gpadc.o
 obj-$(CONFIG_VIPERBOARD_ADC) += viperboard_adc.o
drivers/iio/adc/twl4030-madc.c new file +44 -0
new file mode 100644
index 0000000..4da61c4
@@ -0,0 +1,922 @@
+/*
+ *
+ * TWL4030 MADC module driver-This driver monitors the real time
+ * conversion of analog signals like battery temperature,
+ * battery type, battery level etc.
+ *
+ * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
+ * J Keerthy <[email protected]>
+ *
+ * Based on twl4030-madc.c
+ * Copyright (C) 2008 Nokia Corporation
+ * Mikko Ylinen <[email protected]>
+ *
+ * Amit Kucheria <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ *
+ */
+
+#include <linux/init.h>
+#include <linux/device.h>
+#include <linux/interrupt.h>
+#include <linux/kernel.h>
+#include <linux/delay.h>
+#include <linux/platform_device.h>
+#include <linux/slab.h>
+#include <linux/i2c/twl.h>
+#include <linux/i2c/twl4030-madc.h>
+#include <linux/module.h>
+#include <linux/stddef.h>
+#include <linux/mutex.h>
+#include <linux/bitops.h>
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 50

Quoted 6 lines
drivers/iio/adc/twl4030-madc.c new file +6 -0
+#include <linux/jiffies.h>
+#include <linux/types.h>
+#include <linux/gfp.h>
+#include <linux/err.h>
+
+#include <linux/iio/iio.h>

Why include machine.h or driver.h. You aren't currently using the mapping coding anyway that these provide.

My bad. I copied that over from another driver without checking if it is actually needed.

Quoted 10 lines
Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+#include <linux/iio/machine.h>
+#include <linux/iio/driver.h>
+
+/*
+ * struct twl4030_madc_data - a container for madc info
+ * @dev - pointer to device structure for madc
+ * @lock - mutex protecting this data structure
+ * @requests - Array of request struct corresponding to SW1, SW2 and RT
+ * @imr - Interrupt mask register of MADC
+ * @isr - Interrupt status register of MADC
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 66

Quoted 6 lines
drivers/iio/adc/twl4030-madc.c new file +6 -0
+ */
+struct twl4030_madc_data {
+	struct device *dev;
+	struct mutex lock;	/* mutex protecting this data structure */
+	struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS];
+	bool use_second_irq;

These are 32 bit signed values?

These were already defined as int in the original driver. I will change these to u8 in an additional patch.

Quoted 12 lines
Quoted 12 lines
drivers/iio/adc/twl4030-madc.c new file +12 -0
+	int imr;
+	int isr;
+};
+
+static int twl4030_madc_read(struct iio_dev *iio_dev,
+			     const struct iio_chan_spec *chan,
+			     int *val, int *val2, long mask)
+{
+	struct twl4030_madc_data *madc = iio_priv(iio_dev);
+	struct twl4030_madc_request req;
+	int channel = chan->channel;
+	int ret;
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 84

Quoted 6 lines
drivers/iio/adc/twl4030-madc.c new file +6 -0
+
+	req.method = madc->use_second_irq ? TWL4030_MADC_SW2 : TWL4030_MADC_SW1;
+
+	req.channels = BIT(channel);
+	req.active = 0;
+	req.func_cb = NULL;

req.raw = !(mask & IIO_CHAN_INFO_PROCESSED); req.do_avg = !!(mask & IIO_CHAN_INFO_AVERAGE_RAW);

Right.

Quoted 12 lines
Quoted 12 lines
drivers/iio/adc/twl4030-madc.c new file +12 -0
+	req.raw = (mask & IIO_CHAN_INFO_PROCESSED) ? false : true;
+	req.do_avg = (mask & IIO_CHAN_INFO_AVERAGE_RAW) ? true : false;
+
+	ret = twl4030_madc_conversion(&req);
+	if (ret < 0)
+		return ret;
+
+	*val = req.rbuf[channel];
+
+	return IIO_VAL_INT;
+}
+
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 101

Quoted 5 lines
drivers/iio/adc/twl4030-madc.c new file +5 -0
+static const struct iio_info twl4030_madc_iio_info = {
+	.read_raw = &twl4030_madc_read,
+	.driver_module = THIS_MODULE,
+};
+

Please give this a prefixed name. Chances of ADC_CHANNEL turning up in an IIO header is a little too high to do this without! e.g. #define TWL4030_ADC_CHANNEL

Probably drivers/iio/adc/exynos_adc.c should also be fixed.

Quoted 4 lines

drivers/iio/adc/twl4030-madc.c line 103

Quoted 2 lines
drivers/iio/adc/twl4030-madc.c new file +2 -0
+#define ADC_CHANNEL(_channel, _type, _name, _mask) {	\
+	.type = _type,					\

I don't think you use scan_type anywhere so don't bother specifying it. here.

ok.

Quoted 9 lines

drivers/iio/adc/twl4030-madc.c line 110

Quoted 7 lines
drivers/iio/adc/twl4030-madc.c new file +7 -0
+	.scan_type = IIO_ST('u', 10, 16, 0),		\
+	.channel = _channel,				\
+	.info_mask_separate = _mask,			\
+	.datasheet_name = _name,			\
+	.indexed = 1,					\
+}
+

Is the average have an adjustable number of samples?

The TWL supports reading a value directly (1 sample) or reading the average of 4 samples.

Quoted 3 lines

If not I'd be tempted to use the more common option of IIO_CHAN_INFO_RAW rather than the average version (tends only to be used in fairly obscure cases where both a raw access and an averaged one are available).

currently some drivers use the averaged read and some use the direct read.

Quoted 16 lines
Quoted 16 lines
drivers/iio/adc/twl4030-madc.c new file +16 -0
+static const struct iio_chan_spec twl4030_madc_iio_channels[] = {
+	ADC_CHANNEL(0, IIO_VOLTAGE, "ADCIN0", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(1, IIO_TEMP, "ADCIN1", BIT(IIO_CHAN_INFO_PROCESSED) |
+					   BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(2, IIO_VOLTAGE, "ADCIN2", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(3, IIO_VOLTAGE, "ADCIN3", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(4, IIO_VOLTAGE, "ADCIN4", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(5, IIO_VOLTAGE, "ADCIN5", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(6, IIO_VOLTAGE, "ADCIN6", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(7, IIO_VOLTAGE, "ADCIN7", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(8, IIO_VOLTAGE, "ADCIN8", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(9, IIO_VOLTAGE, "ADCIN9", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(10, IIO_CURRENT, "ADCIN10", BIT(IIO_CHAN_INFO_PROCESSED) |
+						BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(11, IIO_VOLTAGE, "ADCIN11", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(12, IIO_VOLTAGE, "ADCIN12", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 131

Quoted 5 lines
drivers/iio/adc/twl4030-madc.c new file +5 -0
+	ADC_CHANNEL(13, IIO_VOLTAGE, "ADCIN13", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(14, IIO_VOLTAGE, "ADCIN14", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(15, IIO_VOLTAGE, "ADCIN15", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+};
+

Why the artificial limitation of one of these devices? I guess this is to allow the exported function to work without needing to be associated with any particular device... Hmm.

Artificial limitation? The madc is a ADC, which has some special functions defined for some pins in the datasheet:

ADC0 = Battery Voltage
ADC1 = Battery Temperature
ADC10 = Battery Current

Nokia misused some of those pins on the Nokia N900, though. For example they have their own conversion tables for the temperature, which is not compatible with the generic one for the madc module.

I planned to convert the rx51-battery driver to simply read the raw values. All other users can use the processed information.

Quoted 117 lines
Quoted 104 lines
drivers/iio/adc/twl4030-madc.c new file +104 -0
+static struct twl4030_madc_data *twl4030_madc;
+
+struct twl4030_prescale_divider_ratios {
+	s16 numerator;
+	s16 denominator;
+};
+
+static const struct twl4030_prescale_divider_ratios
+twl4030_divider_ratios[16] = {
+	{1, 1},		/* CHANNEL 0 No Prescaler */
+	{1, 1},		/* CHANNEL 1 No Prescaler */
+	{6, 10},	/* CHANNEL 2 */
+	{6, 10},	/* CHANNEL 3 */
+	{6, 10},	/* CHANNEL 4 */
+	{6, 10},	/* CHANNEL 5 */
+	{6, 10},	/* CHANNEL 6 */
+	{6, 10},	/* CHANNEL 7 */
+	{3, 14},	/* CHANNEL 8 */
+	{1, 3},		/* CHANNEL 9 */
+	{1, 1},		/* CHANNEL 10 No Prescaler */
+	{15, 100},	/* CHANNEL 11 */
+	{1, 4},		/* CHANNEL 12 */
+	{1, 1},		/* CHANNEL 13 Reserved channels */
+	{1, 1},		/* CHANNEL 14 Reseved channels */
+	{5, 11},	/* CHANNEL 15 */
+};
+
+
+/*
+ * Conversion table from -3 to 55 degree Celcius
+ */
+static int therm_tbl[] = {
+30800,	29500,	28300,	27100,
+26000,	24900,	23900,	22900,	22000,	21100,	20300,	19400,	18700,	17900,
+17200,	16500,	15900,	15300,	14700,	14100,	13600,	13100,	12600,	12100,
+11600,	11200,	10800,	10400,	10000,	9630,	9280,	8950,	8620,	8310,
+8020,	7730,	7460,	7200,	6950,	6710,	6470,	6250,	6040,	5830,
+5640,	5450,	5260,	5090,	4920,	4760,	4600,	4450,	4310,	4170,
+4040,	3910,	3790,	3670,	3550
+};
+
+/*
+ * Structure containing the registers
+ * of different conversion methods supported by MADC.
+ * Hardware or RT real time conversion request initiated by external host
+ * processor for RT Signal conversions.
+ * External host processors can also request for non RT conversions
+ * SW1 and SW2 software conversions also called asynchronous or GPC request.
+ */
+static
+const struct twl4030_madc_conversion_method twl4030_conversion_methods[] = {
+	[TWL4030_MADC_RT] = {
+			     .sel = TWL4030_MADC_RTSELECT_LSB,
+			     .avg = TWL4030_MADC_RTAVERAGE_LSB,
+			     .rbase = TWL4030_MADC_RTCH0_LSB,
+			     },
+	[TWL4030_MADC_SW1] = {
+			      .sel = TWL4030_MADC_SW1SELECT_LSB,
+			      .avg = TWL4030_MADC_SW1AVERAGE_LSB,
+			      .rbase = TWL4030_MADC_GPCH0_LSB,
+			      .ctrl = TWL4030_MADC_CTRL_SW1,
+			      },
+	[TWL4030_MADC_SW2] = {
+			      .sel = TWL4030_MADC_SW2SELECT_LSB,
+			      .avg = TWL4030_MADC_SW2AVERAGE_LSB,
+			      .rbase = TWL4030_MADC_GPCH0_LSB,
+			      .ctrl = TWL4030_MADC_CTRL_SW2,
+			      },
+};
+
+/*
+ * Function to read a particular channel value.
+ * @madc - pointer to struct twl4030_madc_data
+ * @reg - lsb of ADC Channel
+ * If the i2c read fails it returns an error else returns 0.
+ */
+static int twl4030_madc_channel_raw_read(struct twl4030_madc_data *madc, u8 reg)
+{
+	u8 msb, lsb;
+	int ret;
+	/*
+	 * For each ADC channel, we have MSB and LSB register pair. MSB address
+	 * is always LSB address+1. reg parameter is the address of LSB register
+	 */
+	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &msb, reg + 1);
+	if (ret) {
+		dev_err(madc->dev, "unable to read MSB register 0x%X\n",
+			reg + 1);
+		return ret;
+	}
+	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &lsb, reg);
+	if (ret) {
+		dev_err(madc->dev, "unable to read LSB register 0x%X\n", reg);
+		return ret;
+	}
+
+	return (int)(((msb << 8) | lsb) >> 6);
+}
+
+/*
+ * Return battery temperature
+ * Or < 0 on failure.
+ */
+static int twl4030battery_temperature(int raw_volt)

drivers/iio/adc/twl4030-madc.c line 245

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+{
+	u8 val;
+	int temp, curr, volt, res, ret;
+
+	volt = (raw_volt * TEMP_STEP_SIZE) / TEMP_PSR_R;
+	/* Getting and calculating the supply current in micro ampers */
+	ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val,
+		REG_BCICTL2);
+	if (ret < 0)
+		return ret;

blank line here and after similar error cases makes it a little easier to read.

Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 252

Quoted 7 lines
drivers/iio/adc/twl4030-madc.c new file +7 -0
+	curr = ((val & TWL4030_BCI_ITHEN) + 1) * 10;
+	/* Getting and calculating the thermistor resistance in ohms */
+	res = volt * 1000 / curr;
+	/* calculating temperature */
+	for (temp = 58; temp >= 0; temp--) {
+		int actual = therm_tbl[temp];
+

No blank line here.

I added the newline to the style fix patch, since these lines were not touched by me before.

Quoted 14 lines
Quoted 14 lines
drivers/iio/adc/twl4030-madc.c new file +14 -0
+		if ((actual - res) >= 0)
+			break;
+	}
+
+	return temp + 1;
+}
+
+static int twl4030battery_current(int raw_volt)
+{
+	int ret;
+	u8 val;
+
+	ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val,
+		TWL4030_BCI_BCICTL1);
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 273

Quoted 7 lines
drivers/iio/adc/twl4030-madc.c new file +7 -0
+	if (ret)
+		return ret;
+	if (val & TWL4030_BCI_CGAIN) /* slope of 0.44 mV/mA */
+		return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R1;
+	else /* slope of 0.88 mV/mA */
+		return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R2;
+}

blank line here please.

Also added to the style fix patch.

Quoted 5 lines

Various comments on this in the next function:
This would be much simpler if any error immediately resulted in an exit with error code. If it's gone wrong enough to issue a dev_err then don't try to muddle through. Given there is nothing to clear up in here, just error out directly on the first problem.

I guess this should be fixed once the old API is removed.

Quoted 13 lines
Quoted 13 lines
drivers/iio/adc/twl4030-madc.c new file +13 -0
+/*
+ * Function to read channel values
+ * @madc - pointer to twl4030_madc_data struct
+ * @reg_base - Base address of the first channel
+ * @Channels - 16 bit bitmap. If the bit is set, channel value is read
+ * @buf - The channel values are stored here. if read fails error
+ * @raw - Return raw values without conversion
+ * value is stored
+ * Returns the number of successfully read channels.
+ */
+static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
+				      u8 reg_base, unsigned
+				      long channels, int *buf,
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 292

Quoted 6 lines
drivers/iio/adc/twl4030-madc.c new file +6 -0
+				      bool raw)
+{
+	int count = 0, count_req = 0, i;
+	u8 reg;
+
+	for_each_set_bit(i, &channels, TWL4030_MADC_MAX_CHANNELS) {

Skip the temporary for reg. Yes, it gets used twice, but easier to read inline.

Also fixed in the style patch.

Quoted 7 lines
Quoted 7 lines
drivers/iio/adc/twl4030-madc.c new file +7 -0
+		reg = reg_base + 2 * i;
+		buf[i] = twl4030_madc_channel_raw_read(madc, reg);
+		if (buf[i] < 0) {
+			dev_err(madc->dev,
+				"Unable to read register 0x%X\n", reg);
+			count_req++;
+			continue;
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 300

Quoted 1 line
drivers/iio/adc/twl4030-madc.c new file +1 -0
+		}

I'd prefer this as
if (raw) {
count ++;
} else {
switch (i) {
..
}

That increases the code indention by another level. I don't think that's a good idea.

Quoted 3 lines

Also near as I can tell count gets incremented unless there is an error anyway. So just increment it outside and allow this function to return the error code.

Returning error codes would change the existing API. I would prefer to do this change after the old API is removed.

Quoted 6 lines

drivers/iio/adc/twl4030-madc.c line 305

Quoted 5 lines
drivers/iio/adc/twl4030-madc.c new file +5 -0
+		if (raw) {
+			count++;
+			continue;
+		}
+		switch (i) {

These cases could do with a suitable #define to give them a name.

TWL4030_MADC_ADCIN10 is already in use for (1 << 10). I guess this can be simplified more easily once the old API is removed.

Quoted 32 lines
Quoted 32 lines
drivers/iio/adc/twl4030-madc.c new file +32 -0
+		case 10:
+			buf[i] = twl4030battery_current(buf[i]);
+			if (buf[i] < 0) {
+				dev_err(madc->dev, "err reading current\n");
+				count_req++;
+			} else {
+				count++;
+				buf[i] = buf[i] - 750;
+			}
+			break;
+		case 1:
+			buf[i] = twl4030battery_temperature(buf[i]);
+			if (buf[i] < 0) {
+				dev_err(madc->dev, "err reading temperature\n");
+				count_req++;
+			} else {
+				buf[i] -= 3;
+				count++;
+			}
+			break;
+		default:
+			count++;
+			/* Analog Input (V) = conv_result * step_size / R
+			 * conv_result = decimal value of 10-bit conversion
+			 *		 result
+			 * step size = 1.5 / (2 ^ 10 -1)
+			 * R = Prescaler ratio for input channels.
+			 * Result given in mV hence multiplied by 1000.
+			 */
+			buf[i] = (buf[i] * 3 * 1000 *
+				 twl4030_divider_ratios[i].denominator)
+				/ (2 * 1023 *
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 340

Quoted 3 lines
drivers/iio/adc/twl4030-madc.c new file +3 -0
+				twl4030_divider_ratios[i].numerator);
+		}
+	}

This is already apparant from the errors emmited earlier. I'd drop this and the count_req counter in general.
Personally I'd error out in those cases anyway rather than carrying on with the rest of the channels. If it's worth of a dev_err it's worthy of getting out as fast as possible rather than trying to muddle on.

I also think this should be postponed until the old API is removed.

Quoted 29 lines
Quoted 15 lines
drivers/iio/adc/twl4030-madc.c new file +15 -0
+	if (count_req)
+		dev_err(madc->dev, "%d channel conversion failed\n", count_req);
+
+	return count;
+}
+
+/*
+ * Enables irq.
+ * @madc - pointer to twl4030_madc_data struct
+ * @id - irq number to be enabled
+ * can take one of TWL4030_MADC_RT, TWL4030_MADC_SW1, TWL4030_MADC_SW2
+ * corresponding to RT, SW1, SW2 conversion requests.
+ * If the i2c read fails it returns an error else returns 0.
+ */
+static int twl4030_madc_enable_irq(struct twl4030_madc_data *madc, u8 id)

drivers/iio/adc/twl4030-madc.c line 365

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+{
+	u8 val;
+	int ret;
+
+	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr);
+	if (ret) {
+		dev_err(madc->dev, "unable to read imr register 0x%X\n",
+			madc->imr);
+		return ret;
+	}

A blank line here and in similar cases would slight aid readability.

Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 372

Quoted 7 lines
drivers/iio/adc/twl4030-madc.c new file +7 -0
+	val &= ~(1 << id);
+	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr);
+	if (ret) {
+		dev_err(madc->dev,
+			"unable to write imr register 0x%X\n", madc->imr);
+		return ret;
+

Loose blank line here.

both fixed in the style patch.

Quoted 58 lines
Quoted 58 lines
drivers/iio/adc/twl4030-madc.c new file +58 -0
+	}
+
+	return 0;
+}
+
+/*
+ * Disables irq.
+ * @madc - pointer to twl4030_madc_data struct
+ * @id - irq number to be disabled
+ * can take one of TWL4030_MADC_RT, TWL4030_MADC_SW1, TWL4030_MADC_SW2
+ * corresponding to RT, SW1, SW2 conversion requests.
+ * Returns error if i2c read/write fails.
+ */
+static int twl4030_madc_disable_irq(struct twl4030_madc_data *madc, u8 id)
+{
+	u8 val;
+	int ret;
+
+	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr);
+	if (ret) {
+		dev_err(madc->dev, "unable to read imr register 0x%X\n",
+			madc->imr);
+		return ret;
+	}
+	val |= (1 << id);
+	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr);
+	if (ret) {
+		dev_err(madc->dev,
+			"unable to write imr register 0x%X\n", madc->imr);
+		return ret;
+	}
+
+	return 0;
+}
+
+static irqreturn_t twl4030_madc_threaded_irq_handler(int irq, void *_madc)
+{
+	struct twl4030_madc_data *madc = _madc;
+	const struct twl4030_madc_conversion_method *method;
+	u8 isr_val, imr_val;
+	int i, len, ret;
+	struct twl4030_madc_request *r;
+
+	mutex_lock(&madc->lock);
+	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &isr_val, madc->isr);
+	if (ret) {
+		dev_err(madc->dev, "unable to read isr register 0x%X\n",
+			madc->isr);
+		goto err_i2c;
+	}
+	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &imr_val, madc->imr);
+	if (ret) {
+		dev_err(madc->dev, "unable to read imr register 0x%X\n",
+			madc->imr);
+		goto err_i2c;
+	}
+	isr_val &= ~imr_val;
+	for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 437

Quoted 7 lines
drivers/iio/adc/twl4030-madc.c new file +7 -0
+		if (!(isr_val & (1 << i)))
+			continue;
+		ret = twl4030_madc_disable_irq(madc, i);
+		if (ret < 0)
+			dev_dbg(madc->dev, "Disable interrupt failed%d\n", i);
+		madc->requests[i].result_pending = 1;
+	}

Can this and the previous loop not be combined thus simplifying some tests?

It probably can be, but I think this can wait until the old API is removed.

Quoted 44 lines
Quoted 44 lines
drivers/iio/adc/twl4030-madc.c new file +44 -0
+	for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
+		r = &madc->requests[i];
+		/* No pending results for this method, move to next one */
+		if (!r->result_pending)
+			continue;
+		method = &twl4030_conversion_methods[r->method];
+		/* Read results */
+		len = twl4030_madc_read_channels(madc, method->rbase,
+						 r->channels, r->rbuf, r->raw);
+		/* Return results to caller */
+		if (r->func_cb != NULL) {
+			r->func_cb(len, r->channels, r->rbuf);
+			r->func_cb = NULL;
+		}
+		/* Free request */
+		r->result_pending = 0;
+		r->active = 0;
+	}
+	mutex_unlock(&madc->lock);
+
+	return IRQ_HANDLED;
+
+err_i2c:
+	/*
+	 * In case of error check whichever request is active
+	 * and service the same.
+	 */
+	for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
+		r = &madc->requests[i];
+		if (r->active == 0)
+			continue;
+		method = &twl4030_conversion_methods[r->method];
+		/* Read results */
+		len = twl4030_madc_read_channels(madc, method->rbase,
+						 r->channels, r->rbuf, r->raw);
+		/* Return results to caller */
+		if (r->func_cb != NULL) {
+			r->func_cb(len, r->channels, r->rbuf);
+			r->func_cb = NULL;
+		}
+		/* Free request */
+		r->result_pending = 0;
+		r->active = 0;
+	}
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 483

Quoted 5 lines
drivers/iio/adc/twl4030-madc.c new file +5 -0
+	mutex_unlock(&madc->lock);
+
+	return IRQ_HANDLED;
+}
+

This structure does seem a little complex. I'd have been more tempted by using a completion and having the calling function wait on it. How often does an actual callback make sense?

This is part of the old API, so let's convert all consumers to the IIO API and deprecate the callback stuff.

Quoted 25 lines
Quoted 25 lines
drivers/iio/adc/twl4030-madc.c new file +25 -0
+static int twl4030_madc_set_irq(struct twl4030_madc_data *madc,
+				struct twl4030_madc_request *req)
+{
+	struct twl4030_madc_request *p;
+	int ret;
+
+	p = &madc->requests[req->method];
+	memcpy(p, req, sizeof(*req));
+	ret = twl4030_madc_enable_irq(madc, req->method);
+	if (ret < 0) {
+		dev_err(madc->dev, "enable irq failed!!\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+/*
+ * Function which enables the madc conversion
+ * by writing to the control register.
+ * @madc - pointer to twl4030_madc_data struct
+ * @conv_method - can be TWL4030_MADC_RT, TWL4030_MADC_SW2, TWL4030_MADC_SW1
+ * corresponding to RT SW1 or SW2 conversion methods.
+ * Returns 0 if succeeds else a negative error value
+ */
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 518

Quoted 7 lines
drivers/iio/adc/twl4030-madc.c new file +7 -0
+static int twl4030_madc_start_conversion(struct twl4030_madc_data *madc,
+					 int conv_method)
+{
+	const struct twl4030_madc_conversion_method *method;
+	int ret = 0;
+	method = &twl4030_conversion_methods[conv_method];
+	switch (conv_method) {

Can we get here via any paths where these aren't the methods set?

conv_method is set from outside of the driver, so it's probably safer to check. This can be simplified once the old API is removed.

Quoted 12 lines
Quoted 12 lines
drivers/iio/adc/twl4030-madc.c new file +12 -0
+	case TWL4030_MADC_SW1:
+	case TWL4030_MADC_SW2:
+		ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
+				       TWL4030_MADC_SW_START, method->ctrl);
+		if (ret) {
+			dev_err(madc->dev,
+				"unable to write ctrl register 0x%X\n",
+				method->ctrl);
+			return ret;
+		}
+		break;
+	default:
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 377

Quoted 6 lines
drivers/iio/adc/twl4030-madc.c new file +6 -0
+		break;
+	}
+
+	return 0;
+}
+

Could we fix up the kernel doc in this file in general. It's so nearly there but not quite!

I assume, that the driver will get a huge cleanup once all consumers of the old API are converted. Maybe a deprecation flag should be added together with this patchset?

Quoted 44 lines
Quoted 27 lines
drivers/iio/adc/twl4030-madc.c new file +27 -0
+/*
+ * Function that waits for conversion to be ready
+ * @madc - pointer to twl4030_madc_data struct
+ * @timeout_ms - timeout value in milliseconds
+ * @status_reg - ctrl register
+ * returns 0 if succeeds else a negative error value
+ */
+static int twl4030_madc_wait_conversion_ready(struct twl4030_madc_data *madc,
+					      unsigned int timeout_ms,
+					      u8 status_reg)
+{
+	unsigned long timeout;
+	int ret;
+
+	timeout = jiffies + msecs_to_jiffies(timeout_ms);
+	do {
+		u8 reg;
+
+		ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &reg, status_reg);
+		if (ret) {
+			dev_err(madc->dev,
+				"unable to read status register 0x%X\n",
+				status_reg);
+			return ret;
+		}
+		if (!(reg & TWL4030_MADC_BUSY) && (reg & TWL4030_MADC_EOC_SW))
+			return 0;

drivers/iio/adc/twl4030-madc.c line 572

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +9 -0
+		usleep_range(500, 2000);
+	} while (!time_after(jiffies, timeout));
+	dev_err(madc->dev, "conversion timeout!\n");
+
+	return -EAGAIN;
+}
+
 
+/*
+ * An exported function which can be called from other kernel drivers.

What uses this currently? Ideally we'd do this through the standard IIO paths for in kernel users. Right now the device tree mappings for that are less than ideal (ask Mark Rutland if you want some choice comments ;) Perhaps we take the view this can be cleaned up later. There are some elements in here we haven't yet looked at supporting for client drivers. Will have to think about that.

Quoted 2 lines

It would be particularly nice if possible to use a generic battery driver for the two cases that seem to be using this functionality at the moment.

Currently there are 3 consumer drivers using this:

* twl4030-madc-battery
* rx51-battery
* twl4030-madc-hwmon

I guess the first two can be updated to use the IIO API and the last one can be replaced by the IIO hwmon driver. Since it's an in-kernel-api there should be no problems to simply remove the API afterwards.

Quoted 49 lines
Quoted 49 lines

+ * @req twl4030_madc_request structure
+ * req->rbuf will be filled with read values of channels based on the
+ * channel index. If a particular channel reading fails there will
+ * be a negative error value in the corresponding array element.
+ * returns 0 if succeeds else error value
+ */

drivers/iio/adc/twl4030-madc.c new file +43 -0
+int twl4030_madc_conversion(struct twl4030_madc_request *req)
+{
+	const struct twl4030_madc_conversion_method *method;
+	u8 ch_msb, ch_lsb;
+	int ret;
+
+	if (!req || !twl4030_madc)
+		return -EINVAL;
+
+	mutex_lock(&twl4030_madc->lock);
+	if (req->method < TWL4030_MADC_RT || req->method > TWL4030_MADC_SW2) {
+		ret = -EINVAL;
+		goto out;
+	}
+	/* Do we have a conversion request ongoing */
+	if (twl4030_madc->requests[req->method].active) {
+		ret = -EBUSY;
+		goto out;
+	}
+	ch_msb = (req->channels >> 8) & 0xff;
+	ch_lsb = req->channels & 0xff;
+	method = &twl4030_conversion_methods[req->method];
+	/* Select channels to be converted */
+	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_msb, method->sel + 1);
+	if (ret) {
+		dev_err(twl4030_madc->dev,
+			"unable to write sel register 0x%X\n", method->sel + 1);
+		goto out;
+	}
+	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_lsb, method->sel);
+	if (ret) {
+		dev_err(twl4030_madc->dev,
+			"unable to write sel register 0x%X\n", method->sel + 1);
+		goto out;
+	}
+	/* Select averaging for all channels if do_avg is set */
+	if (req->do_avg) {
+		ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
+				       ch_msb, method->avg + 1);
+		if (ret) {
+			dev_err(twl4030_madc->dev,
+				"unable to write avg register 0x%X\n",
+				method->avg + 1);
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 627

Quoted 6 lines
drivers/iio/adc/twl4030-madc.c new file +6 -0
+			goto out;
+		}
+		ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
+				       ch_lsb, method->avg);
+		if (ret) {
+			dev_err(twl4030_madc->dev,

Excesive error messages are sometimes irritating as they break up code flow. They are really irritating when incorrect ;)

Added to the fixes patch.

Quoted 10 lines
Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+				"unable to write sel reg 0x%X\n",
+				method->sel + 1);
+			goto out;
+		}
+	}
+	if (req->type == TWL4030_MADC_IRQ_ONESHOT && req->func_cb != NULL) {
+		ret = twl4030_madc_set_irq(twl4030_madc, req);
+		if (ret < 0)
+			goto out;
+		ret = twl4030_madc_start_conversion(twl4030_madc, req->method);
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 643

Quoted 6 lines
drivers/iio/adc/twl4030-madc.c new file +6 -0
+		if (ret < 0)
+			goto out;
+		twl4030_madc->requests[req->method].active = 1;
+		ret = 0;
+		goto out;
+	}

Is it possible to get here? This comment suggests not. If so, why have the test?

I guess it's an additional safety check. This function can be simplified a lot once the old API is removed, so I guess this can also be postponed.

Quoted 22 lines

drivers/iio/adc/twl4030-madc.c line 653

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+	/* With RT method we should not be here anymore */
+	if (req->method == TWL4030_MADC_RT) {
+		ret = -EINVAL;
+		goto out;
+	}
+	ret = twl4030_madc_start_conversion(twl4030_madc, req->method);
+	if (ret < 0)
+		goto out;
+	twl4030_madc->requests[req->method].active = 1;
+	/* Wait until conversion is ready (ctrl register returns EOC) */

So no interrupts in this case?

Quoted 11 lines
drivers/iio/adc/twl4030-madc.c new file +11 -0
+	ret = twl4030_madc_wait_conversion_ready(twl4030_madc, 5, method->ctrl);
+	if (ret) {
+		twl4030_madc->requests[req->method].active = 0;
+		goto out;
+	}
+	ret = twl4030_madc_read_channels(twl4030_madc, method->rbase,
+					 req->channels, req->rbuf, req->raw);
+	twl4030_madc->requests[req->method].active = 0;
+
+out:
+	mutex_unlock(&twl4030_madc->lock);
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 670

Quoted 6 lines
drivers/iio/adc/twl4030-madc.c new file +6 -0
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(twl4030_madc_conversion);
+
+/*

Proper kerneldoc would be nice here. Otherwise the comment doesn't really add anything so I'd be tempted to drop it ;)

removed in fixes patch.

Quoted 21 lines
Quoted 21 lines

+ * Return channel value
+ * Or < 0 on failure.
+ */

drivers/iio/adc/twl4030-madc.c new file +18 -0
+int twl4030_get_madc_conversion(int channel_no)
+{
+	struct twl4030_madc_request req;
+	int temp = 0;
+	int ret;
+
+	req.channels = (1 << channel_no);
+	req.method = TWL4030_MADC_SW2;
+	req.active = 0;
+	req.func_cb = NULL;
+	ret = twl4030_madc_conversion(&req);
+	if (ret < 0)
+		return ret;
+	if (req.rbuf[channel_no] > 0)
+		temp = req.rbuf[channel_no];
+
+	return temp;
+}
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 698

Quoted 7 lines
drivers/iio/adc/twl4030-madc.c new file +7 -0
+EXPORT_SYMBOL_GPL(twl4030_get_madc_conversion);
+
+/*
+ * Function to enable or disable bias current for
+ * main battery type reading or temperature sensing
+ * @madc - pointer to twl4030_madc_data struct
+ * @chan - can be one of the two values

What is a battery type reading? Voltage? Current? Charge?

It's used for battery type identification.

Quoted 6 lines
Quoted 2 lines

+ * TWL4030_BCI_ITHEN - Enables bias current for main battery type reading
+ * TWL4030_BCI_TYPEN - Enables bias current for main battery temperature

These constants have rather incomprehensible names. Whilst I gues they are straight off the data sheet, seems to me that we could make them a little longer and easier to follow! The comment here helps, but sensible names would be better!

The names come from the datasheet. I think we should fix this after the old API is removed, since the constants are exposed for the API.

Quoted 25 lines
Quoted 6 lines

+ * sensing
+ * @on - enable or disable chan.
+ */

drivers/iio/adc/twl4030-madc.c new file +3 -0
+static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc,
+					      int chan, int on)
+{

drivers/iio/adc/twl4030-madc.c line 716

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+	int ret;
+	u8 regval;
+
+	ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE,
+			      &regval, TWL4030_BCI_BCICTL1);
+	if (ret) {
+		dev_err(madc->dev, "unable to read BCICTL1 reg 0x%X",
+			TWL4030_BCI_BCICTL1);
+		return ret;
+	}

Introduce an intermediate variable and this could be nice and easy to read Say

int regmask;
if (chan == 0)
regmask = TWL4030_BCI_TYPEN;
else
regmask = TWL4030_BFI_ITHEN;

Quoted 6 lines

if (on)
regval |= regmask;
else
regval &= ~regmask;

It's longer but doesn't give me a headache.

I added this to the fixes patch:

    int regmask;
	regmask = (chan == 0) ? TWL4030_BCI_TYPEN : TWL4030_BFI_ITHEN;
	if (on)
		regval |= regmask;
	else
		regval &= ~regmask;
Quoted 117 lines
Quoted 117 lines
drivers/iio/adc/twl4030-madc.c new file +117 -0
+	if (on)
+		regval |= chan ? TWL4030_BCI_ITHEN : TWL4030_BCI_TYPEN;
+	else
+		regval &= chan ? ~TWL4030_BCI_ITHEN : ~TWL4030_BCI_TYPEN;
+	ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
+			       regval, TWL4030_BCI_BCICTL1);
+	if (ret) {
+		dev_err(madc->dev, "unable to write BCICTL1 reg 0x%X\n",
+			TWL4030_BCI_BCICTL1);
+		return ret;
+	}
+
+	return 0;
+}
+
+/*
+ * Function that sets MADC software power on bit to enable MADC
+ * @madc - pointer to twl4030_madc_data struct
+ * @on - Enable or disable MADC software powen on bit.
+ * returns error if i2c read/write fails else 0
+ */
+static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on)
+{
+	u8 regval;
+	int ret;
+
+	ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE,
+			      &regval, TWL4030_MADC_CTRL1);
+	if (ret) {
+		dev_err(madc->dev, "unable to read madc ctrl1 reg 0x%X\n",
+			TWL4030_MADC_CTRL1);
+		return ret;
+	}
+	if (on)
+		regval |= TWL4030_MADC_MADCON;
+	else
+		regval &= ~TWL4030_MADC_MADCON;
+	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, regval, TWL4030_MADC_CTRL1);
+	if (ret) {
+		dev_err(madc->dev, "unable to write madc ctrl1 reg 0x%X\n",
+			TWL4030_MADC_CTRL1);
+		return ret;
+	}
+
+	return 0;
+}
+
+/*
+ * Initialize MADC and request for threaded irq
+ */
+static int twl4030_madc_probe(struct platform_device *pdev)
+{
+	struct twl4030_madc_data *madc;
+	struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev);
+	struct device_node *np = pdev->dev.of_node;
+	int irq, ret;
+	u8 regval;
+	struct iio_dev *iio_dev = NULL;
+
+	if (!pdata && !np) {
+		dev_err(&pdev->dev, "platform_data not available\n");
+		return -EINVAL;
+	}
+
+	iio_dev = devm_iio_device_alloc(&pdev->dev,
+					sizeof(struct twl4030_madc_data));
+	if (!iio_dev) {
+		dev_err(&pdev->dev, "failed allocating iio device\n");
+		return -ENOMEM;
+	}
+
+	madc = iio_priv(iio_dev);
+	madc->dev = &pdev->dev;
+
+	iio_dev->name = dev_name(&pdev->dev);
+	iio_dev->dev.parent = &pdev->dev;
+	iio_dev->dev.of_node = pdev->dev.of_node;
+	iio_dev->info = &twl4030_madc_iio_info;
+	iio_dev->modes = INDIO_DIRECT_MODE;
+	iio_dev->channels = twl4030_madc_iio_channels;
+	iio_dev->num_channels = 16;
+
+	/*
+	 * Phoenix provides 2 interrupt lines. The first one is connected to
+	 * the OMAP. The other one can be connected to the other processor such
+	 * as modem. Hence two separate ISR and IMR registers.
+	 */
+	if (pdata)
+		madc->use_second_irq = pdata->irq_line != 1;
+	else
+		madc->use_second_irq = false;
+
+	madc->imr = (madc->use_second_irq == 1) ?
+	    TWL4030_MADC_IMR1 : TWL4030_MADC_IMR2;
+	madc->isr = (madc->use_second_irq == 1) ?
+	    TWL4030_MADC_ISR1 : TWL4030_MADC_ISR2;
+
+	ret = twl4030_madc_set_power(madc, 1);
+	if (ret < 0)
+		return ret;
+	ret = twl4030_madc_set_current_generator(madc, 0, 1);
+	if (ret < 0)
+		goto err_current_generator;
+
+	ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE,
+			      &regval, TWL4030_BCI_BCICTL1);
+	if (ret) {
+		dev_err(&pdev->dev, "unable to read reg BCI CTL1 0x%X\n",
+			TWL4030_BCI_BCICTL1);
+		goto err_i2c;
+	}
+	regval |= TWL4030_BCI_MESBAT;
+	ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
+			       regval, TWL4030_BCI_BCICTL1);
+	if (ret) {
+		dev_err(&pdev->dev, "unable to write reg BCI Ctl1 0x%X\n",
+			TWL4030_BCI_BCICTL1);
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 839

Quoted 6 lines
drivers/iio/adc/twl4030-madc.c new file +6 -0
+		goto err_i2c;
+	}
+
+	/* Check that MADC clock is on */
+	ret = twl_i2c_read_u8(TWL4030_MODULE_INTBR, &regval, TWL4030_REG_GPBR1);
+	if (ret) {

Personally I'd rank the driver as rather to vebose on read error messages given they are pretty unusual. Ah well I guess each to their own.

All of this is not written by me.

Quoted 33 lines
Quoted 12 lines
drivers/iio/adc/twl4030-madc.c new file +12 -0
+		dev_err(&pdev->dev, "unable to read reg GPBR1 0x%X\n",
+				TWL4030_REG_GPBR1);
+		goto err_i2c;
+	}
+
+	/* If MADC clk is not on, turn it on */
+	if (!(regval & TWL4030_GPBR1_MADC_HFCLK_EN)) {
+		dev_info(&pdev->dev, "clk disabled, enabling\n");
+		regval |= TWL4030_GPBR1_MADC_HFCLK_EN;
+		ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, regval,
+				       TWL4030_REG_GPBR1);
+		if (ret) {

drivers/iio/adc/twl4030-madc.c line 861

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+			dev_err(&pdev->dev, "unable to write reg GPBR1 0x%X\n",
+					TWL4030_REG_GPBR1);
+			goto err_i2c;
+		}
+	}
+
+	platform_set_drvdata(pdev, iio_dev);
+	mutex_init(&madc->lock);
+
+	irq = platform_get_irq(pdev, 0);

As ever using devm for a irq request makes for a bit of a review nightmare as we have to be very careful that nothing has been removed that this might use before the devm cleanup occurs (at end of remove). In this case the fact that the current generator is off and the adc unit is disabled sounds to me like it might cause interesting results in the interrupt handler?

It's one of those things that will probably never happen but I find it hard to convince myself 'can't happen'

Quoted 11 lines

drivers/iio/adc/twl4030-madc.c line 868

Quoted 7 lines
drivers/iio/adc/twl4030-madc.c new file +7 -0
+	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
+				   twl4030_madc_threaded_irq_handler,
+				   IRQF_TRIGGER_RISING, "twl4030_madc", madc);
+	if (ret) {
+		dev_dbg(&pdev->dev, "could not request irq\n");
+		goto err_i2c;
+	}

Err, where is this defined? Ah, found it above. I'm not keen on preventing multiple instances of a device like this. It is so easy to not do this I'd prefer to see the driver fixed to remove this.

This concept has also not introduced by me of course. I think we should fix this after removal of the old API, since the old API does not have an device parameter.

Quoted 48 lines
Quoted 48 lines
drivers/iio/adc/twl4030-madc.c new file +48 -0
+	twl4030_madc = madc;
+
+	ret = iio_device_register(iio_dev);
+	if (ret) {
+		dev_dbg(&pdev->dev, "could not register iio device\n");
+		goto err_i2c;
+	}
+
+	return 0;
+
+err_i2c:
+	twl4030_madc_set_current_generator(madc, 0, 0);
+err_current_generator:
+	twl4030_madc_set_power(madc, 0);
+	return ret;
+}
+
+static int twl4030_madc_remove(struct platform_device *pdev)
+{
+	struct iio_dev *iio_dev = platform_get_drvdata(pdev);
+	struct twl4030_madc_data *madc = iio_priv(iio_dev);
+
+	twl4030_madc_set_current_generator(madc, 0, 0);
+	twl4030_madc_set_power(madc, 0);
+
+	iio_device_unregister(iio_dev);
+
+	return 0;
+}
+
+#ifdef CONFIG_OF
+static const struct of_device_id twl_madc_of_match[] = {
+	{.compatible = "ti,twl4030-madc", },
+	{ },
+};
+MODULE_DEVICE_TABLE(of, twl_madc_of_match);
+#endif
+
+static struct platform_driver twl4030_madc_driver = {
+	.probe = twl4030_madc_probe,
+	.remove = twl4030_madc_remove,
+	.driver = {
+		   .name = "twl4030_madc",
+		   .owner = THIS_MODULE,
+		   .of_match_table = of_match_ptr(twl_madc_of_match),
+		   },
+};
+
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 922

Quoted 6 lines
drivers/iio/adc/twl4030-madc.c new file +6 -0
+module_platform_driver(twl4030_madc_driver);
+
+MODULE_DESCRIPTION("TWL4030 ADC driver");
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("J Keerthy");
+MODULE_ALIAS("platform:twl4030_madc");

Rest of patch was removal of code so no comments!

FYI: For me it was a bit odd to see inline comments about code lines coming after the inline comment. Normally I receive patch reviews with comments after the referenced code, which makes more sense imho.

-- Sebastian

maintainer
#14 lore raw

Re: [RFCv1 4/4] mfd: twl4030-madc: Move driver to drivers/iio/adc

Quoted 12 lines · Sebastian Reichel
Quoted 8 lines · Jonathan Cameron
Quoted 4 lines · Sebastian Reichel

This is a driver for an A/D converter, which belongs into drivers/iio/adc.

Being inherently lazy I'm going to review this patch as it gives the complete driver rather than taking on the conversion patch directly!

OK.

Quoted 7 lines
Quoted 1 line

It's a long shot, but are there any docs freely available for this part?

There is documentation on Texas Instrument's website for TPS65950, which is supposed to be very similar to the TWL4030 and contains a section about the MADC module:

http://www.ti.com/lit/gpn/tps65950

I had a look at that and to put it lightly the docs aren't particularly informative. Ah well, such is life.

Quoted 8 lines
Quoted 4 lines

Obviously that may well mean that some of my comments apply to the driver in general rather than the changes you've made. Please feel free to pick and choose!

Having skipped over your comments I guess 70% are about the driver in general, but I can add another patch to the patchset, which fixes the driver style.

That would be great thanks!

Quoted 19 lines
Quoted 9 lines

I'd like ideally to see a little more generic tidying up in this driver. As you'll notice inline I get irritated at having lots and lots of error messages. Still that's personal preference but I felt a lot more justified in moaning after finding one that was incorrect ;)

There is also a bit of functionality in here that I'm not sure is ever used (the request callback functions). It complicates the code so if no using it I'd be tempted to drop it.

I think the optimal workflow is:

1. convert madc driver to IIO
2. convert twl4030-madc-battery driver to IIO API
3. convert rx51-battery to IIO API
4. convert twl4030-madc-hwmon to IIO API / deprecate it
5. remove old in-kernel ABI from madc
6. cleanup/simplify madc

Quoted 2 lines

I guess its much simpler to do the driver cleanup/simplification once we can get rid of the old API.

Perhaps. I guess it depends on your planned schedule. If you are intending to plough through the above in the near future then fair enough. If the chances are it'll take a while, I'd be inclined to fix some of the more glarringly hideous bits now (such as error code reporting) and then visit your list above over time.

Note that to my mind it is converted drivers like this that sneak some nasty code into an a subsystem I look after. We have a lot of 'interesting' code out in staging still, but for new submissions we are being increasingly fussy about little bits. So I hope I don't come across as being too mean about this driver. I'm glad you are putting in the work to convert it over. There are a quite a few similar drivers sculking through the tree so it's nice that someone has gotten started on bringing one over to IIO.

Quoted 176 lines
Quoted 98 lines

It's nice in a way that the driver before your conversion provided only a very limited and in kernel interface given we don't have to hang on to any old ABI elements.

Quoted 93 lines
---
  drivers/iio/adc/Kconfig        |  10 +
  drivers/iio/adc/Makefile       |   1 +
  drivers/iio/adc/twl4030-madc.c | 922 +++++++++++++++++++++++++++++++++++++++++
  drivers/mfd/Kconfig            |  10 -
  drivers/mfd/Makefile           |   1 -
  drivers/mfd/twl4030-madc.c     | 922 -----------------------------------------
  6 files changed, 933 insertions(+), 933 deletions(-)

create mode 100644 drivers/iio/adc/twl4030-madc.c delete mode 100644 drivers/mfd/twl4030-madc.c

drivers/iio/adc/Kconfig +10 -0
index 2209f28..427f75c 100644
@@ -183,6 +183,16 @@ config TI_AM335X_ADC
  	  Say yes here to build support for Texas Instruments ADC
  	  driver which is also a MFD client.
 
+config TWL4030_MADC
+	tristate "TWL4030 MADC (Monitoring A/D Converter)"
+	depends on TWL4030_CORE
+	help
+	This driver provides support for Triton TWL4030-MADC. The
+	driver supports both RT and SW conversion methods.
+
+	This driver can also be built as a module. If so, the module will be
+	called twl4030-madc.
+
  config TWL6030_GPADC
  	tristate "TWL6030 GPADC (General Purpose A/D Converter) Support"
  	depends on TWL4030_CORE
drivers/iio/adc/Makefile +1 -0
index ba9a10a..9acf2df 100644
@@ -20,5 +20,6 @@ obj-$(CONFIG_MCP3422) += mcp3422.o
  obj-$(CONFIG_NAU7802) += nau7802.o
  obj-$(CONFIG_TI_ADC081C) += ti-adc081c.o
  obj-$(CONFIG_TI_AM335X_ADC) += ti_am335x_adc.o
+obj-$(CONFIG_TWL4030_MADC) += twl4030-madc.o
  obj-$(CONFIG_TWL6030_GPADC) += twl6030-gpadc.o
  obj-$(CONFIG_VIPERBOARD_ADC) += viperboard_adc.o
drivers/iio/adc/twl4030-madc.c new file +44 -0
new file mode 100644
index 0000000..4da61c4
@@ -0,0 +1,922 @@
+/*
+ *
+ * TWL4030 MADC module driver-This driver monitors the real time
+ * conversion of analog signals like battery temperature,
+ * battery type, battery level etc.
+ *
+ * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
+ * J Keerthy <[email protected]>
+ *
+ * Based on twl4030-madc.c
+ * Copyright (C) 2008 Nokia Corporation
+ * Mikko Ylinen <[email protected]>
+ *
+ * Amit Kucheria <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ *
+ */
+
+#include <linux/init.h>
+#include <linux/device.h>
+#include <linux/interrupt.h>
+#include <linux/kernel.h>
+#include <linux/delay.h>
+#include <linux/platform_device.h>
+#include <linux/slab.h>
+#include <linux/i2c/twl.h>
+#include <linux/i2c/twl4030-madc.h>
+#include <linux/module.h>
+#include <linux/stddef.h>
+#include <linux/mutex.h>
+#include <linux/bitops.h>
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 50

Quoted 6 lines
drivers/iio/adc/twl4030-madc.c new file +6 -0
+#include <linux/jiffies.h>
+#include <linux/types.h>
+#include <linux/gfp.h>
+#include <linux/err.h>
+
+#include <linux/iio/iio.h>

Why include machine.h or driver.h. You aren't currently using the mapping coding anyway that these provide.

My bad. I copied that over from another driver without checking if it is actually needed.

Quoted 10 lines
Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+#include <linux/iio/machine.h>
+#include <linux/iio/driver.h>
+
+/*
+ * struct twl4030_madc_data - a container for madc info
+ * @dev - pointer to device structure for madc
+ * @lock - mutex protecting this data structure
+ * @requests - Array of request struct corresponding to SW1, SW2 and RT
+ * @imr - Interrupt mask register of MADC
+ * @isr - Interrupt status register of MADC
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 66

Quoted 6 lines
drivers/iio/adc/twl4030-madc.c new file +6 -0
+ */
+struct twl4030_madc_data {
+	struct device *dev;
+	struct mutex lock;	/* mutex protecting this data structure */
+	struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS];
+	bool use_second_irq;

These are 32 bit signed values?

These were already defined as int in the original driver. I will change these to u8 in an additional patch.

Quoted 12 lines
Quoted 12 lines
drivers/iio/adc/twl4030-madc.c new file +12 -0
+	int imr;
+	int isr;
+};
+
+static int twl4030_madc_read(struct iio_dev *iio_dev,
+			     const struct iio_chan_spec *chan,
+			     int *val, int *val2, long mask)
+{
+	struct twl4030_madc_data *madc = iio_priv(iio_dev);
+	struct twl4030_madc_request req;
+	int channel = chan->channel;
+	int ret;
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 84

Quoted 6 lines
drivers/iio/adc/twl4030-madc.c new file +6 -0
+
+	req.method = madc->use_second_irq ? TWL4030_MADC_SW2 : TWL4030_MADC_SW1;
+
+	req.channels = BIT(channel);
+	req.active = 0;
+	req.func_cb = NULL;

req.raw = !(mask & IIO_CHAN_INFO_PROCESSED); req.do_avg = !!(mask & IIO_CHAN_INFO_AVERAGE_RAW);

Right.

Quoted 12 lines
Quoted 12 lines
drivers/iio/adc/twl4030-madc.c new file +12 -0
+	req.raw = (mask & IIO_CHAN_INFO_PROCESSED) ? false : true;
+	req.do_avg = (mask & IIO_CHAN_INFO_AVERAGE_RAW) ? true : false;
+
+	ret = twl4030_madc_conversion(&req);
+	if (ret < 0)
+		return ret;
+
+	*val = req.rbuf[channel];
+
+	return IIO_VAL_INT;
+}
+
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 101

Quoted 5 lines
drivers/iio/adc/twl4030-madc.c new file +5 -0
+static const struct iio_info twl4030_madc_iio_info = {
+	.read_raw = &twl4030_madc_read,
+	.driver_module = THIS_MODULE,
+};
+

Please give this a prefixed name. Chances of ADC_CHANNEL turning up in an IIO header is a little too high to do this without! e.g. #define TWL4030_ADC_CHANNEL

Quoted 1 line

Probably drivers/iio/adc/exynos_adc.c should also be fixed.

Sounds like it. Patches always welcome ;) Like any other reviewer I'm not known for absolute consistency.

Quoted 21 lines
Quoted 5 lines

drivers/iio/adc/twl4030-madc.c line 103

Quoted 3 lines
drivers/iio/adc/twl4030-madc.c new file +2 -0
+#define ADC_CHANNEL(_channel, _type, _name, _mask) {	\
+	.type = _type,					\

I don't think you use scan_type anywhere so don't bother specifying it. here.

ok.

Quoted 9 lines

drivers/iio/adc/twl4030-madc.c line 110

Quoted 7 lines
drivers/iio/adc/twl4030-madc.c new file +7 -0
+	.scan_type = IIO_ST('u', 10, 16, 0),		\
+	.channel = _channel,				\
+	.info_mask_separate = _mask,			\
+	.datasheet_name = _name,			\
+	.indexed = 1,					\
+}
+

Is the average have an adjustable number of samples?

The TWL supports reading a value directly (1 sample) or reading the average of 4 samples.

Quoted 7 lines
Quoted 3 lines

If not I'd be tempted to use the more common option of IIO_CHAN_INFO_RAW rather than the average version (tends only to be used in fairly obscure cases where both a raw access and an averaged one are available).

currently some drivers use the averaged read and some use the direct read.

In that case I'd expose both the 1 sample and averaged versions via the IIO interfaces. If you want to pick 1 then do IIO_CHAN_INFO_RAW not the averaged version.

Quoted 28 lines
Quoted 16 lines
Quoted 16 lines
drivers/iio/adc/twl4030-madc.c new file +16 -0
+static const struct iio_chan_spec twl4030_madc_iio_channels[] = {
+	ADC_CHANNEL(0, IIO_VOLTAGE, "ADCIN0", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(1, IIO_TEMP, "ADCIN1", BIT(IIO_CHAN_INFO_PROCESSED) |
+					   BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(2, IIO_VOLTAGE, "ADCIN2", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(3, IIO_VOLTAGE, "ADCIN3", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(4, IIO_VOLTAGE, "ADCIN4", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(5, IIO_VOLTAGE, "ADCIN5", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(6, IIO_VOLTAGE, "ADCIN6", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(7, IIO_VOLTAGE, "ADCIN7", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(8, IIO_VOLTAGE, "ADCIN8", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(9, IIO_VOLTAGE, "ADCIN9", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(10, IIO_CURRENT, "ADCIN10", BIT(IIO_CHAN_INFO_PROCESSED) |
+						BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(11, IIO_VOLTAGE, "ADCIN11", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(12, IIO_VOLTAGE, "ADCIN12", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 131

Quoted 5 lines
drivers/iio/adc/twl4030-madc.c new file +5 -0
+	ADC_CHANNEL(13, IIO_VOLTAGE, "ADCIN13", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(14, IIO_VOLTAGE, "ADCIN14", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(15, IIO_VOLTAGE, "ADCIN15", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+};
+

Why the artificial limitation of one of these devices? I guess this is to allow the exported function to work without needing to be associated with any particular device... Hmm.

Artificial limitation? The madc is a ADC, which has some special functions defined for some pins in the datasheet:

Quoted 7 lines

ADC0 = Battery Voltage
ADC1 = Battery Temperature
ADC10 = Battery Current

Nokia misused some of those pins on the Nokia N900, though. For example they have their own conversion tables for the temperature, which is not compatible with the generic one for the madc module.

*laughs*. If someone documents a pin as having a particular purpose but it isn't wired inside a package. Someone will always think they know better. Basic principal is to never limit ourselves to one instance of a chip because the chances of someone deciding to do something interesting with multiple chips is way to high.

Quoted 3 lines

I planned to convert the rx51-battery driver to simply read the raw values. All other users can use the processed information.

Makes sense.

Quoted 203 lines
Quoted 118 lines
Quoted 105 lines
drivers/iio/adc/twl4030-madc.c new file +104 -0
+static struct twl4030_madc_data *twl4030_madc;
+
+struct twl4030_prescale_divider_ratios {
+	s16 numerator;
+	s16 denominator;
+};
+
+static const struct twl4030_prescale_divider_ratios
+twl4030_divider_ratios[16] = {
+	{1, 1},		/* CHANNEL 0 No Prescaler */
+	{1, 1},		/* CHANNEL 1 No Prescaler */
+	{6, 10},	/* CHANNEL 2 */
+	{6, 10},	/* CHANNEL 3 */
+	{6, 10},	/* CHANNEL 4 */
+	{6, 10},	/* CHANNEL 5 */
+	{6, 10},	/* CHANNEL 6 */
+	{6, 10},	/* CHANNEL 7 */
+	{3, 14},	/* CHANNEL 8 */
+	{1, 3},		/* CHANNEL 9 */
+	{1, 1},		/* CHANNEL 10 No Prescaler */
+	{15, 100},	/* CHANNEL 11 */
+	{1, 4},		/* CHANNEL 12 */
+	{1, 1},		/* CHANNEL 13 Reserved channels */
+	{1, 1},		/* CHANNEL 14 Reseved channels */
+	{5, 11},	/* CHANNEL 15 */
+};
+
+
+/*
+ * Conversion table from -3 to 55 degree Celcius
+ */
+static int therm_tbl[] = {
+30800,	29500,	28300,	27100,
+26000,	24900,	23900,	22900,	22000,	21100,	20300,	19400,	18700,	17900,
+17200,	16500,	15900,	15300,	14700,	14100,	13600,	13100,	12600,	12100,
+11600,	11200,	10800,	10400,	10000,	9630,	9280,	8950,	8620,	8310,
+8020,	7730,	7460,	7200,	6950,	6710,	6470,	6250,	6040,	5830,
+5640,	5450,	5260,	5090,	4920,	4760,	4600,	4450,	4310,	4170,
+4040,	3910,	3790,	3670,	3550
+};
+
+/*
+ * Structure containing the registers
+ * of different conversion methods supported by MADC.
+ * Hardware or RT real time conversion request initiated by external host
+ * processor for RT Signal conversions.
+ * External host processors can also request for non RT conversions
+ * SW1 and SW2 software conversions also called asynchronous or GPC request.
+ */
+static
+const struct twl4030_madc_conversion_method twl4030_conversion_methods[] = {
+	[TWL4030_MADC_RT] = {
+			     .sel = TWL4030_MADC_RTSELECT_LSB,
+			     .avg = TWL4030_MADC_RTAVERAGE_LSB,
+			     .rbase = TWL4030_MADC_RTCH0_LSB,
+			     },
+	[TWL4030_MADC_SW1] = {
+			      .sel = TWL4030_MADC_SW1SELECT_LSB,
+			      .avg = TWL4030_MADC_SW1AVERAGE_LSB,
+			      .rbase = TWL4030_MADC_GPCH0_LSB,
+			      .ctrl = TWL4030_MADC_CTRL_SW1,
+			      },
+	[TWL4030_MADC_SW2] = {
+			      .sel = TWL4030_MADC_SW2SELECT_LSB,
+			      .avg = TWL4030_MADC_SW2AVERAGE_LSB,
+			      .rbase = TWL4030_MADC_GPCH0_LSB,
+			      .ctrl = TWL4030_MADC_CTRL_SW2,
+			      },
+};
+
+/*
+ * Function to read a particular channel value.
+ * @madc - pointer to struct twl4030_madc_data
+ * @reg - lsb of ADC Channel
+ * If the i2c read fails it returns an error else returns 0.
+ */
+static int twl4030_madc_channel_raw_read(struct twl4030_madc_data *madc, u8 reg)
+{
+	u8 msb, lsb;
+	int ret;
+	/*
+	 * For each ADC channel, we have MSB and LSB register pair. MSB address
+	 * is always LSB address+1. reg parameter is the address of LSB register
+	 */
+	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &msb, reg + 1);
+	if (ret) {
+		dev_err(madc->dev, "unable to read MSB register 0x%X\n",
+			reg + 1);
+		return ret;
+	}
+	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &lsb, reg);
+	if (ret) {
+		dev_err(madc->dev, "unable to read LSB register 0x%X\n", reg);
+		return ret;
+	}
+
+	return (int)(((msb << 8) | lsb) >> 6);
+}
+
+/*
+ * Return battery temperature
+ * Or < 0 on failure.
+ */
+static int twl4030battery_temperature(int raw_volt)

drivers/iio/adc/twl4030-madc.c line 245

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+{
+	u8 val;
+	int temp, curr, volt, res, ret;
+
+	volt = (raw_volt * TEMP_STEP_SIZE) / TEMP_PSR_R;
+	/* Getting and calculating the supply current in micro ampers */
+	ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val,
+		REG_BCICTL2);
+	if (ret < 0)
+		return ret;

blank line here and after similar error cases makes it a little easier to read.

Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 252

Quoted 7 lines
drivers/iio/adc/twl4030-madc.c new file +7 -0
+	curr = ((val & TWL4030_BCI_ITHEN) + 1) * 10;
+	/* Getting and calculating the thermistor resistance in ohms */
+	res = volt * 1000 / curr;
+	/* calculating temperature */
+	for (temp = 58; temp >= 0; temp--) {
+		int actual = therm_tbl[temp];
+

No blank line here.

I added the newline to the style fix patch, since these lines were not touched by me before.

Quoted 14 lines
Quoted 14 lines
drivers/iio/adc/twl4030-madc.c new file +14 -0
+		if ((actual - res) >= 0)
+			break;
+	}
+
+	return temp + 1;
+}
+
+static int twl4030battery_current(int raw_volt)
+{
+	int ret;
+	u8 val;
+
+	ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val,
+		TWL4030_BCI_BCICTL1);
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 273

Quoted 7 lines
drivers/iio/adc/twl4030-madc.c new file +7 -0
+	if (ret)
+		return ret;
+	if (val & TWL4030_BCI_CGAIN) /* slope of 0.44 mV/mA */
+		return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R1;
+	else /* slope of 0.88 mV/mA */
+		return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R2;
+}

blank line here please.

Also added to the style fix patch.

Quoted 5 lines

Various comments on this in the next function:
This would be much simpler if any error immediately resulted in an exit with error code. If it's gone wrong enough to issue a dev_err then don't try to muddle through. Given there is nothing to clear up in here, just error out directly on the first problem.

I guess this should be fixed once the old API is removed.

Quoted 13 lines
Quoted 13 lines
drivers/iio/adc/twl4030-madc.c new file +13 -0
+/*
+ * Function to read channel values
+ * @madc - pointer to twl4030_madc_data struct
+ * @reg_base - Base address of the first channel
+ * @Channels - 16 bit bitmap. If the bit is set, channel value is read
+ * @buf - The channel values are stored here. if read fails error
+ * @raw - Return raw values without conversion
+ * value is stored
+ * Returns the number of successfully read channels.
+ */
+static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
+				      u8 reg_base, unsigned
+				      long channels, int *buf,
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 292

Quoted 6 lines
drivers/iio/adc/twl4030-madc.c new file +6 -0
+				      bool raw)
+{
+	int count = 0, count_req = 0, i;
+	u8 reg;
+
+	for_each_set_bit(i, &channels, TWL4030_MADC_MAX_CHANNELS) {

Skip the temporary for reg. Yes, it gets used twice, but easier to read inline.

Also fixed in the style patch.

Quoted 9 lines

drivers/iio/adc/twl4030-madc.c line 300

Quoted 8 lines
drivers/iio/adc/twl4030-madc.c new file +8 -0
+		reg = reg_base + 2 * i;
+		buf[i] = twl4030_madc_channel_raw_read(madc, reg);
+		if (buf[i] < 0) {
+			dev_err(madc->dev,
+				"Unable to read register 0x%X\n", reg);
+			count_req++;
+			continue;
+		}

I'd prefer this as

Quoted 6 lines
if (raw) {
   count ++;
} else {

switch (i) {
..
}

Quoted 2 lines

That increases the code indention by another level. I don't think that's a good idea.

For clarity I'd do it anyway, but if the count is brought out of the statements then the flow will be more obvious anyway.

Quoted 6 lines
Quoted 5 lines

Also near as I can tell count gets incremented unless there is an error anyway. So just increment it outside and allow this function to return the error code.
Returning error codes would change the existing API. I would prefer

to do this change after the old API is removed.

This function isn't exported directly and I suspect a quick look would show no one relying on the successfully reading some subset of drivers. It might make sense to eat the error at the exported interface and return a length of 0.

Quoted 52 lines
Quoted 7 lines

drivers/iio/adc/twl4030-madc.c line 305

Quoted 6 lines
drivers/iio/adc/twl4030-madc.c new file +5 -0
+		if (raw) {
+			count++;
+			continue;
+		}
+		switch (i) {

These cases could do with a suitable #define to give them a name.

TWL4030_MADC_ADCIN10 is already in use for (1 << 10). I guess this can be simplified more easily once the old API is removed.

Quoted 32 lines
Quoted 32 lines
drivers/iio/adc/twl4030-madc.c new file +32 -0
+		case 10:
+			buf[i] = twl4030battery_current(buf[i]);
+			if (buf[i] < 0) {
+				dev_err(madc->dev, "err reading current\n");
+				count_req++;
+			} else {
+				count++;
+				buf[i] = buf[i] - 750;
+			}
+			break;
+		case 1:
+			buf[i] = twl4030battery_temperature(buf[i]);
+			if (buf[i] < 0) {
+				dev_err(madc->dev, "err reading temperature\n");
+				count_req++;
+			} else {
+				buf[i] -= 3;
+				count++;
+			}
+			break;
+		default:
+			count++;
+			/* Analog Input (V) = conv_result * step_size / R
+			 * conv_result = decimal value of 10-bit conversion
+			 *		 result
+			 * step size = 1.5 / (2 ^ 10 -1)
+			 * R = Prescaler ratio for input channels.
+			 * Result given in mV hence multiplied by 1000.
+			 */
+			buf[i] = (buf[i] * 3 * 1000 *
+				 twl4030_divider_ratios[i].denominator)
+				/ (2 * 1023 *
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 340

Quoted 3 lines
drivers/iio/adc/twl4030-madc.c new file +3 -0
+				twl4030_divider_ratios[i].numerator);
+		}
+	}

This is already apparant from the errors emmited earlier. I'd drop this and the count_req counter in general.
Personally I'd error out in those cases anyway rather than carrying on with the rest of the channels. If it's worth of a dev_err it's worthy of getting out as fast as possible rather than trying to muddle on.

Quoted 1 line

I also think this should be postponed until the old API is removed.

I'd prefer to see standalone cleanups and fixes integrated first as I suspect it may be a little while before we manage to fully drop the old API.

Quoted 108 lines
Quoted 30 lines
Quoted 16 lines
drivers/iio/adc/twl4030-madc.c new file +15 -0
+	if (count_req)
+		dev_err(madc->dev, "%d channel conversion failed\n", count_req);
+
+	return count;
+}
+
+/*
+ * Enables irq.
+ * @madc - pointer to twl4030_madc_data struct
+ * @id - irq number to be enabled
+ * can take one of TWL4030_MADC_RT, TWL4030_MADC_SW1, TWL4030_MADC_SW2
+ * corresponding to RT, SW1, SW2 conversion requests.
+ * If the i2c read fails it returns an error else returns 0.
+ */
+static int twl4030_madc_enable_irq(struct twl4030_madc_data *madc, u8 id)

drivers/iio/adc/twl4030-madc.c line 365

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+{
+	u8 val;
+	int ret;
+
+	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr);
+	if (ret) {
+		dev_err(madc->dev, "unable to read imr register 0x%X\n",
+			madc->imr);
+		return ret;
+	}

A blank line here and in similar cases would slight aid readability.

Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 372

Quoted 7 lines
drivers/iio/adc/twl4030-madc.c new file +7 -0
+	val &= ~(1 << id);
+	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr);
+	if (ret) {
+		dev_err(madc->dev,
+			"unable to write imr register 0x%X\n", madc->imr);
+		return ret;
+

Loose blank line here.

both fixed in the style patch.

Quoted 58 lines
Quoted 58 lines
drivers/iio/adc/twl4030-madc.c new file +58 -0
+	}
+
+	return 0;
+}
+
+/*
+ * Disables irq.
+ * @madc - pointer to twl4030_madc_data struct
+ * @id - irq number to be disabled
+ * can take one of TWL4030_MADC_RT, TWL4030_MADC_SW1, TWL4030_MADC_SW2
+ * corresponding to RT, SW1, SW2 conversion requests.
+ * Returns error if i2c read/write fails.
+ */
+static int twl4030_madc_disable_irq(struct twl4030_madc_data *madc, u8 id)
+{
+	u8 val;
+	int ret;
+
+	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr);
+	if (ret) {
+		dev_err(madc->dev, "unable to read imr register 0x%X\n",
+			madc->imr);
+		return ret;
+	}
+	val |= (1 << id);
+	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr);
+	if (ret) {
+		dev_err(madc->dev,
+			"unable to write imr register 0x%X\n", madc->imr);
+		return ret;
+	}
+
+	return 0;
+}
+
+static irqreturn_t twl4030_madc_threaded_irq_handler(int irq, void *_madc)
+{
+	struct twl4030_madc_data *madc = _madc;
+	const struct twl4030_madc_conversion_method *method;
+	u8 isr_val, imr_val;
+	int i, len, ret;
+	struct twl4030_madc_request *r;
+
+	mutex_lock(&madc->lock);
+	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &isr_val, madc->isr);
+	if (ret) {
+		dev_err(madc->dev, "unable to read isr register 0x%X\n",
+			madc->isr);
+		goto err_i2c;
+	}
+	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &imr_val, madc->imr);
+	if (ret) {
+		dev_err(madc->dev, "unable to read imr register 0x%X\n",
+			madc->imr);
+		goto err_i2c;
+	}
+	isr_val &= ~imr_val;
+	for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 437

Quoted 7 lines
drivers/iio/adc/twl4030-madc.c new file +7 -0
+		if (!(isr_val & (1 << i)))
+			continue;
+		ret = twl4030_madc_disable_irq(madc, i);
+		if (ret < 0)
+			dev_dbg(madc->dev, "Disable interrupt failed%d\n", i);
+		madc->requests[i].result_pending = 1;
+	}

Can this and the previous loop not be combined thus simplifying some tests?

Quoted 2 lines

It probably can be, but I think this can wait until the old API is removed.

Hmm. As I state below, if this is moving over to IIO (which is sensible) it becomes my problem so I reserve the right to be fussy about less than optimal code. If it was coming in clean as a new driver I'd push back hard on a lot of this stuff before merging it. I have plenty of slightly uggly drivers in staging to look after as it is!

Quoted 91 lines
Quoted 45 lines
Quoted 45 lines
drivers/iio/adc/twl4030-madc.c new file +44 -0
+	for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
+		r = &madc->requests[i];
+		/* No pending results for this method, move to next one */
+		if (!r->result_pending)
+			continue;
+		method = &twl4030_conversion_methods[r->method];
+		/* Read results */
+		len = twl4030_madc_read_channels(madc, method->rbase,
+						 r->channels, r->rbuf, r->raw);
+		/* Return results to caller */
+		if (r->func_cb != NULL) {
+			r->func_cb(len, r->channels, r->rbuf);
+			r->func_cb = NULL;
+		}
+		/* Free request */
+		r->result_pending = 0;
+		r->active = 0;
+	}
+	mutex_unlock(&madc->lock);
+
+	return IRQ_HANDLED;
+
+err_i2c:
+	/*
+	 * In case of error check whichever request is active
+	 * and service the same.
+	 */
+	for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
+		r = &madc->requests[i];
+		if (r->active == 0)
+			continue;
+		method = &twl4030_conversion_methods[r->method];
+		/* Read results */
+		len = twl4030_madc_read_channels(madc, method->rbase,
+						 r->channels, r->rbuf, r->raw);
+		/* Return results to caller */
+		if (r->func_cb != NULL) {
+			r->func_cb(len, r->channels, r->rbuf);
+			r->func_cb = NULL;
+		}
+		/* Free request */
+		r->result_pending = 0;
+		r->active = 0;
+	}
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 483

Quoted 5 lines
drivers/iio/adc/twl4030-madc.c new file +5 -0
+	mutex_unlock(&madc->lock);
+
+	return IRQ_HANDLED;
+}
+

This structure does seem a little complex. I'd have been more tempted by using a completion and having the calling function wait on it. How often does an actual callback make sense?

This is part of the old API, so let's convert all consumers to the IIO API and deprecate the callback stuff.

Quoted 25 lines
Quoted 25 lines
drivers/iio/adc/twl4030-madc.c new file +25 -0
+static int twl4030_madc_set_irq(struct twl4030_madc_data *madc,
+				struct twl4030_madc_request *req)
+{
+	struct twl4030_madc_request *p;
+	int ret;
+
+	p = &madc->requests[req->method];
+	memcpy(p, req, sizeof(*req));
+	ret = twl4030_madc_enable_irq(madc, req->method);
+	if (ret < 0) {
+		dev_err(madc->dev, "enable irq failed!!\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+/*
+ * Function which enables the madc conversion
+ * by writing to the control register.
+ * @madc - pointer to twl4030_madc_data struct
+ * @conv_method - can be TWL4030_MADC_RT, TWL4030_MADC_SW2, TWL4030_MADC_SW1
+ * corresponding to RT SW1 or SW2 conversion methods.
+ * Returns 0 if succeeds else a negative error value
+ */
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 518

Quoted 7 lines
drivers/iio/adc/twl4030-madc.c new file +7 -0
+static int twl4030_madc_start_conversion(struct twl4030_madc_data *madc,
+					 int conv_method)
+{
+	const struct twl4030_madc_conversion_method *method;
+	int ret = 0;
+	method = &twl4030_conversion_methods[conv_method];
+	switch (conv_method) {

Can we get here via any paths where these aren't the methods set?

Quoted 2 lines

conv_method is set from outside of the driver, so it's probably safer to check. This can be simplified once the old API is removed.

Then we should be returning an error if it's not one of these.

Quoted 22 lines
Quoted 13 lines
Quoted 13 lines
drivers/iio/adc/twl4030-madc.c new file +12 -0
+	case TWL4030_MADC_SW1:
+	case TWL4030_MADC_SW2:
+		ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
+				       TWL4030_MADC_SW_START, method->ctrl);
+		if (ret) {
+			dev_err(madc->dev,
+				"unable to write ctrl register 0x%X\n",
+				method->ctrl);
+			return ret;
+		}
+		break;
+	default:
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 377

Quoted 6 lines
drivers/iio/adc/twl4030-madc.c new file +6 -0
+		break;
+	}
+
+	return 0;
+}
+

Could we fix up the kernel doc in this file in general. It's so nearly there but not quite!

Quoted 3 lines

I assume, that the driver will get a huge cleanup once all consumers of the old API are converted. Maybe a deprecation flag should be added together with this patchset?

Probably not worth bothering for an inkernel interface that seems to have relatively few users. How many out of tree users will be moving to new kernels and using this device?

Quoted 50 lines
Quoted 45 lines
Quoted 28 lines
drivers/iio/adc/twl4030-madc.c new file +27 -0
+/*
+ * Function that waits for conversion to be ready
+ * @madc - pointer to twl4030_madc_data struct
+ * @timeout_ms - timeout value in milliseconds
+ * @status_reg - ctrl register
+ * returns 0 if succeeds else a negative error value
+ */
+static int twl4030_madc_wait_conversion_ready(struct twl4030_madc_data *madc,
+					      unsigned int timeout_ms,
+					      u8 status_reg)
+{
+	unsigned long timeout;
+	int ret;
+
+	timeout = jiffies + msecs_to_jiffies(timeout_ms);
+	do {
+		u8 reg;
+
+		ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &reg, status_reg);
+		if (ret) {
+			dev_err(madc->dev,
+				"unable to read status register 0x%X\n",
+				status_reg);
+			return ret;
+		}
+		if (!(reg & TWL4030_MADC_BUSY) && (reg & TWL4030_MADC_EOC_SW))
+			return 0;

drivers/iio/adc/twl4030-madc.c line 572

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +9 -0
+		usleep_range(500, 2000);
+	} while (!time_after(jiffies, timeout));
+	dev_err(madc->dev, "conversion timeout!\n");
+
+	return -EAGAIN;
+}
+
 
+/*
+ * An exported function which can be called from other kernel drivers.

What uses this currently? Ideally we'd do this through the standard IIO paths for in kernel users. Right now the device tree mappings for that are less than ideal (ask Mark Rutland if you want some choice comments ;) Perhaps we take the view this can be cleaned up later. There are some elements in here we haven't yet looked at supporting for client drivers. Will have to think about that.

Quoted 2 lines

It would be particularly nice if possible to use a generic battery driver for the two cases that seem to be using this functionality at the moment.

Currently there are 3 consumer drivers using this:

Quoted 8 lines

* twl4030-madc-battery
* rx51-battery
* twl4030-madc-hwmon

I guess the first two can be updated to use the IIO API and the last one can be replaced by the IIO hwmon driver. Since it's an in-kernel-api there should be no problems to simply remove the API afterwards.

Yes that sounds like a good paln.

Quoted 147 lines
Quoted 50 lines
Quoted 50 lines

+ * @req twl4030_madc_request structure
+ * req->rbuf will be filled with read values of channels based on the
+ * channel index. If a particular channel reading fails there will
+ * be a negative error value in the corresponding array element.
+ * returns 0 if succeeds else error value
+ */

drivers/iio/adc/twl4030-madc.c new file +43 -0
+int twl4030_madc_conversion(struct twl4030_madc_request *req)
+{
+	const struct twl4030_madc_conversion_method *method;
+	u8 ch_msb, ch_lsb;
+	int ret;
+
+	if (!req || !twl4030_madc)
+		return -EINVAL;
+
+	mutex_lock(&twl4030_madc->lock);
+	if (req->method < TWL4030_MADC_RT || req->method > TWL4030_MADC_SW2) {
+		ret = -EINVAL;
+		goto out;
+	}
+	/* Do we have a conversion request ongoing */
+	if (twl4030_madc->requests[req->method].active) {
+		ret = -EBUSY;
+		goto out;
+	}
+	ch_msb = (req->channels >> 8) & 0xff;
+	ch_lsb = req->channels & 0xff;
+	method = &twl4030_conversion_methods[req->method];
+	/* Select channels to be converted */
+	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_msb, method->sel + 1);
+	if (ret) {
+		dev_err(twl4030_madc->dev,
+			"unable to write sel register 0x%X\n", method->sel + 1);
+		goto out;
+	}
+	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_lsb, method->sel);
+	if (ret) {
+		dev_err(twl4030_madc->dev,
+			"unable to write sel register 0x%X\n", method->sel + 1);
+		goto out;
+	}
+	/* Select averaging for all channels if do_avg is set */
+	if (req->do_avg) {
+		ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
+				       ch_msb, method->avg + 1);
+		if (ret) {
+			dev_err(twl4030_madc->dev,
+				"unable to write avg register 0x%X\n",
+				method->avg + 1);
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 627

Quoted 6 lines
drivers/iio/adc/twl4030-madc.c new file +6 -0
+			goto out;
+		}
+		ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
+				       ch_lsb, method->avg);
+		if (ret) {
+			dev_err(twl4030_madc->dev,

Excesive error messages are sometimes irritating as they break up code flow. They are really irritating when incorrect ;)

Added to the fixes patch.

Quoted 10 lines
Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+				"unable to write sel reg 0x%X\n",
+				method->sel + 1);
+			goto out;
+		}
+	}
+	if (req->type == TWL4030_MADC_IRQ_ONESHOT && req->func_cb != NULL) {
+		ret = twl4030_madc_set_irq(twl4030_madc, req);
+		if (ret < 0)
+			goto out;
+		ret = twl4030_madc_start_conversion(twl4030_madc, req->method);
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 643

Quoted 6 lines
drivers/iio/adc/twl4030-madc.c new file +6 -0
+		if (ret < 0)
+			goto out;
+		twl4030_madc->requests[req->method].active = 1;
+		ret = 0;
+		goto out;
+	}

Is it possible to get here? This comment suggests not. If so, why have the test?

I guess it's an additional safety check. This function can be simplified a lot once the old API is removed, so I guess this can also be postponed.

Quoted 22 lines

drivers/iio/adc/twl4030-madc.c line 653

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+	/* With RT method we should not be here anymore */
+	if (req->method == TWL4030_MADC_RT) {
+		ret = -EINVAL;
+		goto out;
+	}
+	ret = twl4030_madc_start_conversion(twl4030_madc, req->method);
+	if (ret < 0)
+		goto out;
+	twl4030_madc->requests[req->method].active = 1;
+	/* Wait until conversion is ready (ctrl register returns EOC) */

So no interrupts in this case?

Quoted 11 lines
drivers/iio/adc/twl4030-madc.c new file +11 -0
+	ret = twl4030_madc_wait_conversion_ready(twl4030_madc, 5, method->ctrl);
+	if (ret) {
+		twl4030_madc->requests[req->method].active = 0;
+		goto out;
+	}
+	ret = twl4030_madc_read_channels(twl4030_madc, method->rbase,
+					 req->channels, req->rbuf, req->raw);
+	twl4030_madc->requests[req->method].active = 0;
+
+out:
+	mutex_unlock(&twl4030_madc->lock);
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 670

Quoted 6 lines
drivers/iio/adc/twl4030-madc.c new file +6 -0
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(twl4030_madc_conversion);
+
+/*

Proper kerneldoc would be nice here. Otherwise the comment doesn't really add anything so I'd be tempted to drop it ;)

removed in fixes patch.

Quoted 21 lines
Quoted 21 lines

+ * Return channel value
+ * Or < 0 on failure.
+ */

drivers/iio/adc/twl4030-madc.c new file +18 -0
+int twl4030_get_madc_conversion(int channel_no)
+{
+	struct twl4030_madc_request req;
+	int temp = 0;
+	int ret;
+
+	req.channels = (1 << channel_no);
+	req.method = TWL4030_MADC_SW2;
+	req.active = 0;
+	req.func_cb = NULL;
+	ret = twl4030_madc_conversion(&req);
+	if (ret < 0)
+		return ret;
+	if (req.rbuf[channel_no] > 0)
+		temp = req.rbuf[channel_no];
+
+	return temp;
+}
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 698

Quoted 7 lines
drivers/iio/adc/twl4030-madc.c new file +7 -0
+EXPORT_SYMBOL_GPL(twl4030_get_madc_conversion);
+
+/*
+ * Function to enable or disable bias current for
+ * main battery type reading or temperature sensing
+ * @madc - pointer to twl4030_madc_data struct
+ * @chan - can be one of the two values

What is a battery type reading? Voltage? Current? Charge?

Quoted 1 line

It's used for battery type identification.

Ah the comment really did mean what it said!

Quoted 10 lines
Quoted 7 lines
Quoted 3 lines

+ * TWL4030_BCI_ITHEN - Enables bias current for main battery type reading
+ * TWL4030_BCI_TYPEN - Enables bias current for main battery temperature

These constants have rather incomprehensible names. Whilst I gues they are straight off the data sheet, seems to me that we could make them a little longer and easier to follow! The comment here helps, but sensible names would be better!

The names come from the datasheet. I think we should fix this after the old API is removed, since the constants are exposed for the API.

Fair enough.

Quoted 168 lines
Quoted 26 lines
Quoted 7 lines

+ * sensing
+ * @on - enable or disable chan.
+ */

drivers/iio/adc/twl4030-madc.c new file +3 -0
+static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc,
+					      int chan, int on)
+{

drivers/iio/adc/twl4030-madc.c line 716

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+	int ret;
+	u8 regval;
+
+	ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE,
+			      &regval, TWL4030_BCI_BCICTL1);
+	if (ret) {
+		dev_err(madc->dev, "unable to read BCICTL1 reg 0x%X",
+			TWL4030_BCI_BCICTL1);
+		return ret;
+	}

Introduce an intermediate variable and this could be nice and easy to read Say

int regmask;
if (chan == 0)

    regmask = TWL4030_BCI_TYPEN;
else
    regmask = TWL4030_BFI_ITHEN;
Quoted 6 lines

if (on)

    regval |= regmask;
else
    regval &= ~regmask;

It's longer but doesn't give me a headache.

I added this to the fixes patch:

     int regmask;
	regmask = (chan == 0) ? TWL4030_BCI_TYPEN : TWL4030_BFI_ITHEN;
	if (on)
		regval |= regmask;
	else
		regval &= ~regmask;
Quoted 117 lines
Quoted 117 lines
drivers/iio/adc/twl4030-madc.c new file +117 -0
+	if (on)
+		regval |= chan ? TWL4030_BCI_ITHEN : TWL4030_BCI_TYPEN;
+	else
+		regval &= chan ? ~TWL4030_BCI_ITHEN : ~TWL4030_BCI_TYPEN;
+	ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
+			       regval, TWL4030_BCI_BCICTL1);
+	if (ret) {
+		dev_err(madc->dev, "unable to write BCICTL1 reg 0x%X\n",
+			TWL4030_BCI_BCICTL1);
+		return ret;
+	}
+
+	return 0;
+}
+
+/*
+ * Function that sets MADC software power on bit to enable MADC
+ * @madc - pointer to twl4030_madc_data struct
+ * @on - Enable or disable MADC software powen on bit.
+ * returns error if i2c read/write fails else 0
+ */
+static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on)
+{
+	u8 regval;
+	int ret;
+
+	ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE,
+			      &regval, TWL4030_MADC_CTRL1);
+	if (ret) {
+		dev_err(madc->dev, "unable to read madc ctrl1 reg 0x%X\n",
+			TWL4030_MADC_CTRL1);
+		return ret;
+	}
+	if (on)
+		regval |= TWL4030_MADC_MADCON;
+	else
+		regval &= ~TWL4030_MADC_MADCON;
+	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, regval, TWL4030_MADC_CTRL1);
+	if (ret) {
+		dev_err(madc->dev, "unable to write madc ctrl1 reg 0x%X\n",
+			TWL4030_MADC_CTRL1);
+		return ret;
+	}
+
+	return 0;
+}
+
+/*
+ * Initialize MADC and request for threaded irq
+ */
+static int twl4030_madc_probe(struct platform_device *pdev)
+{
+	struct twl4030_madc_data *madc;
+	struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev);
+	struct device_node *np = pdev->dev.of_node;
+	int irq, ret;
+	u8 regval;
+	struct iio_dev *iio_dev = NULL;
+
+	if (!pdata && !np) {
+		dev_err(&pdev->dev, "platform_data not available\n");
+		return -EINVAL;
+	}
+
+	iio_dev = devm_iio_device_alloc(&pdev->dev,
+					sizeof(struct twl4030_madc_data));
+	if (!iio_dev) {
+		dev_err(&pdev->dev, "failed allocating iio device\n");
+		return -ENOMEM;
+	}
+
+	madc = iio_priv(iio_dev);
+	madc->dev = &pdev->dev;
+
+	iio_dev->name = dev_name(&pdev->dev);
+	iio_dev->dev.parent = &pdev->dev;
+	iio_dev->dev.of_node = pdev->dev.of_node;
+	iio_dev->info = &twl4030_madc_iio_info;
+	iio_dev->modes = INDIO_DIRECT_MODE;
+	iio_dev->channels = twl4030_madc_iio_channels;
+	iio_dev->num_channels = 16;
+
+	/*
+	 * Phoenix provides 2 interrupt lines. The first one is connected to
+	 * the OMAP. The other one can be connected to the other processor such
+	 * as modem. Hence two separate ISR and IMR registers.
+	 */
+	if (pdata)
+		madc->use_second_irq = pdata->irq_line != 1;
+	else
+		madc->use_second_irq = false;
+
+	madc->imr = (madc->use_second_irq == 1) ?
+	    TWL4030_MADC_IMR1 : TWL4030_MADC_IMR2;
+	madc->isr = (madc->use_second_irq == 1) ?
+	    TWL4030_MADC_ISR1 : TWL4030_MADC_ISR2;
+
+	ret = twl4030_madc_set_power(madc, 1);
+	if (ret < 0)
+		return ret;
+	ret = twl4030_madc_set_current_generator(madc, 0, 1);
+	if (ret < 0)
+		goto err_current_generator;
+
+	ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE,
+			      &regval, TWL4030_BCI_BCICTL1);
+	if (ret) {
+		dev_err(&pdev->dev, "unable to read reg BCI CTL1 0x%X\n",
+			TWL4030_BCI_BCICTL1);
+		goto err_i2c;
+	}
+	regval |= TWL4030_BCI_MESBAT;
+	ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
+			       regval, TWL4030_BCI_BCICTL1);
+	if (ret) {
+		dev_err(&pdev->dev, "unable to write reg BCI Ctl1 0x%X\n",
+			TWL4030_BCI_BCICTL1);
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 839

Quoted 6 lines
drivers/iio/adc/twl4030-madc.c new file +6 -0
+		goto err_i2c;
+	}
+
+	/* Check that MADC clock is on */
+	ret = twl_i2c_read_u8(TWL4030_MODULE_INTBR, &regval, TWL4030_REG_GPBR1);
+	if (ret) {

Personally I'd rank the driver as rather to vebose on read error messages given they are pretty unusual. Ah well I guess each to their own.

Quoted 1 line

All of this is not written by me.

Sure. Doesn't stop me moaning :) At the end of the day you are proposing moving this driver into a place where it becomes at least partly my problem. As such I'm keen on any tidying / cleaning occuring ASAP.

Quoted 46 lines
Quoted 34 lines
Quoted 13 lines
drivers/iio/adc/twl4030-madc.c new file +12 -0
+		dev_err(&pdev->dev, "unable to read reg GPBR1 0x%X\n",
+				TWL4030_REG_GPBR1);
+		goto err_i2c;
+	}
+
+	/* If MADC clk is not on, turn it on */
+	if (!(regval & TWL4030_GPBR1_MADC_HFCLK_EN)) {
+		dev_info(&pdev->dev, "clk disabled, enabling\n");
+		regval |= TWL4030_GPBR1_MADC_HFCLK_EN;
+		ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, regval,
+				       TWL4030_REG_GPBR1);
+		if (ret) {

drivers/iio/adc/twl4030-madc.c line 861

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+			dev_err(&pdev->dev, "unable to write reg GPBR1 0x%X\n",
+					TWL4030_REG_GPBR1);
+			goto err_i2c;
+		}
+	}
+
+	platform_set_drvdata(pdev, iio_dev);
+	mutex_init(&madc->lock);
+
+	irq = platform_get_irq(pdev, 0);

As ever using devm for a irq request makes for a bit of a review nightmare as we have to be very careful that nothing has been removed that this might use before the devm cleanup occurs (at end of remove). In this case the fact that the current generator is off and the adc unit is disabled sounds to me like it might cause interesting results in the interrupt handler?

It's one of those things that will probably never happen but I find it hard to convince myself 'can't happen'

Quoted 11 lines

drivers/iio/adc/twl4030-madc.c line 868

Quoted 7 lines
drivers/iio/adc/twl4030-madc.c new file +7 -0
+	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
+				   twl4030_madc_threaded_irq_handler,
+				   IRQF_TRIGGER_RISING, "twl4030_madc", madc);
+	if (ret) {
+		dev_dbg(&pdev->dev, "could not request irq\n");
+		goto err_i2c;
+	}

Err, where is this defined? Ah, found it above. I'm not keen on preventing multiple instances of a device like this. It is so easy to not do this I'd prefer to see the driver fixed to remove this.

Quoted 3 lines

This concept has also not introduced by me of course. I think we should fix this after removal of the old API, since the old API does not have an device parameter.

Fair enough.

Quoted 58 lines
Quoted 49 lines
Quoted 49 lines
drivers/iio/adc/twl4030-madc.c new file +48 -0
+	twl4030_madc = madc;
+
+	ret = iio_device_register(iio_dev);
+	if (ret) {
+		dev_dbg(&pdev->dev, "could not register iio device\n");
+		goto err_i2c;
+	}
+
+	return 0;
+
+err_i2c:
+	twl4030_madc_set_current_generator(madc, 0, 0);
+err_current_generator:
+	twl4030_madc_set_power(madc, 0);
+	return ret;
+}
+
+static int twl4030_madc_remove(struct platform_device *pdev)
+{
+	struct iio_dev *iio_dev = platform_get_drvdata(pdev);
+	struct twl4030_madc_data *madc = iio_priv(iio_dev);
+
+	twl4030_madc_set_current_generator(madc, 0, 0);
+	twl4030_madc_set_power(madc, 0);
+
+	iio_device_unregister(iio_dev);
+
+	return 0;
+}
+
+#ifdef CONFIG_OF
+static const struct of_device_id twl_madc_of_match[] = {
+	{.compatible = "ti,twl4030-madc", },
+	{ },
+};
+MODULE_DEVICE_TABLE(of, twl_madc_of_match);
+#endif
+
+static struct platform_driver twl4030_madc_driver = {
+	.probe = twl4030_madc_probe,
+	.remove = twl4030_madc_remove,
+	.driver = {
+		   .name = "twl4030_madc",
+		   .owner = THIS_MODULE,
+		   .of_match_table = of_match_ptr(twl_madc_of_match),
+		   },
+};
+
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 922

Quoted 6 lines
drivers/iio/adc/twl4030-madc.c new file +6 -0
+module_platform_driver(twl4030_madc_driver);
+
+MODULE_DESCRIPTION("TWL4030 ADC driver");
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("J Keerthy");
+MODULE_ALIAS("platform:twl4030_madc");

Rest of patch was removal of code so no comments!

Quoted 3 lines

FYI: For me it was a bit odd to see inline comments about code lines coming after the inline comment. Normally I receive patch reviews with comments after the referenced code, which makes more sense imho.

I never claimed to be consistent ;) Tend to review backwards that means that sometimes comments end up in fairly random places.

Quoted 3 lines

-- Sebastian

#15 lore raw

Re: [RFCv1 4/4] mfd: twl4030-madc: Move driver to drivers/iio/adc

Hi,

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

I think the optimal workflow is:

1. convert madc driver to IIO
2. convert twl4030-madc-battery driver to IIO API
3. convert rx51-battery to IIO API
4. convert twl4030-madc-hwmon to IIO API / deprecate it
5. remove old in-kernel ABI from madc
6. cleanup/simplify madc

Quoted 8 lines
Quoted 2 lines

I guess its much simpler to do the driver cleanup/simplification once we can get rid of the old API.

Perhaps. I guess it depends on your planned schedule. If you are intending to plough through the above in the near future then fair enough. If the chances are it'll take a while, I'd be inclined to fix some of the more glarringly hideous bits now (such as error code reporting) and then visit your list above over time.

I plan to update the rx51-battery driver, since that's the only one I can test. Actually I only did the changes to the twl4030-madc driver, so that I can use the standard DT API for AD converters for rx51-battery. I hope to get it updated ASAP.

Marek seems to work on getting DT support for the other two consumers (twl4030-madc-battery and twl4030-madc-hwmon), so chances are, that the old API can be removed in the near future :)

Quoted 7 lines

Note that to my mind it is converted drivers like this that sneak some nasty code into an a subsystem I look after. We have a lot of 'interesting' code out in staging still, but for new submissions we are being increasingly fussy about little bits. So I hope I don't come across as being too mean about this driver. I'm glad you are putting in the work to convert it over. There are a quite a few similar drivers sculking through the tree so it's nice that someone has gotten started on bringing one over to IIO.

If it makes you feel more at ease I will try to forward the errors and replace the error code with 0 at the end.

Quoted 11 lines

[...]

Quoted 9 lines
Quoted 1 line

Is the average have an adjustable number of samples?

The TWL supports reading a value directly (1 sample) or reading the average of 4 samples.

Quoted 3 lines

If not I'd be tempted to use the more common option of IIO_CHAN_INFO_RAW rather than the average version (tends only to be used in fairly obscure cases where both a raw access and an averaged one are available).

Quoted 7 lines
Quoted 3 lines

currently some drivers use the averaged read and some use the direct read.

In that case I'd expose both the 1 sample and averaged versions via the IIO interfaces. If you want to pick 1 then do IIO_CHAN_INFO_RAW not the averaged version.

That's what I intended to do. Now I see, that I forgot to expose IIO_CHAN_INFO_RAW. Sorry for the confusion.

Quoted 36 lines
Quoted 28 lines
Quoted 16 lines
Quoted 16 lines
drivers/iio/adc/twl4030-madc.c new file +16 -0
+static const struct iio_chan_spec twl4030_madc_iio_channels[] = {
+	ADC_CHANNEL(0, IIO_VOLTAGE, "ADCIN0", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(1, IIO_TEMP, "ADCIN1", BIT(IIO_CHAN_INFO_PROCESSED) |
+					   BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(2, IIO_VOLTAGE, "ADCIN2", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(3, IIO_VOLTAGE, "ADCIN3", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(4, IIO_VOLTAGE, "ADCIN4", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(5, IIO_VOLTAGE, "ADCIN5", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(6, IIO_VOLTAGE, "ADCIN6", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(7, IIO_VOLTAGE, "ADCIN7", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(8, IIO_VOLTAGE, "ADCIN8", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(9, IIO_VOLTAGE, "ADCIN9", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(10, IIO_CURRENT, "ADCIN10", BIT(IIO_CHAN_INFO_PROCESSED) |
+						BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(11, IIO_VOLTAGE, "ADCIN11", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(12, IIO_VOLTAGE, "ADCIN12", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 131

Quoted 5 lines
drivers/iio/adc/twl4030-madc.c new file +5 -0
+	ADC_CHANNEL(13, IIO_VOLTAGE, "ADCIN13", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(14, IIO_VOLTAGE, "ADCIN14", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+	ADC_CHANNEL(15, IIO_VOLTAGE, "ADCIN15", BIT(IIO_CHAN_INFO_AVERAGE_RAW)),
+};
+

Why the artificial limitation of one of these devices? I guess this is to allow the exported function to work without needing to be associated with any particular device... Hmm.

Artificial limitation? The madc is a ADC, which has some special functions defined for some pins in the datasheet:

Quoted 7 lines

ADC0 = Battery Voltage
ADC1 = Battery Temperature
ADC10 = Battery Current

Nokia misused some of those pins on the Nokia N900, though. For example they have their own conversion tables for the temperature, which is not compatible with the generic one for the madc module.

Quoted 5 lines

*laughs*. If someone documents a pin as having a particular purpose but it isn't wired inside a package. Someone will always think they know better. Basic principal is to never limit ourselves to one instance of a chip because the chances of someone deciding to do something interesting with multiple chips is way to high.

;)

Quoted 4 lines
Quoted 3 lines

I planned to convert the rx51-battery driver to simply read the raw values. All other users can use the processed information.

Makes sense.

ok :)

Quoted 11 lines

[...]

Quoted 10 lines
Quoted 9 lines

drivers/iio/adc/twl4030-madc.c line 344

Quoted 4 lines
drivers/iio/adc/twl4030-madc.c new file +4 -0
+	if (count_req)
+		dev_err(madc->dev, "%d channel conversion failed\n", count_req);
+
+	return count;

This is already apparant from the errors emmited earlier. I'd drop this and the count_req counter in general.
Personally I'd error out in those cases anyway rather than carrying on with the rest of the channels. If it's worth of a dev_err it's worthy of getting out as fast as possible rather than trying to muddle on.

Quoted 3 lines
Quoted 1 line

I also think this should be postponed until the old API is removed.

I'd prefer to see standalone cleanups and fixes integrated first as I suspect it may be a little while before we manage to fully drop the old API.

ok. I have included this change in the fixup patch. This slightly changes the old API (callback may be called with negative error values), but the callback feature doesn't seem to be used by any mainline user.

Quoted 10 lines

[...]

Quoted 4 lines
Quoted 1 line

Can this and the previous loop not be combined thus simplifying some tests?

It probably can be, but I think this can wait until the old API is removed.

Hmm. As I state below, if this is moving over to IIO (which is sensible) it becomes my problem so I reserve the right to be fussy about less than optimal code. If it was coming in clean as a new driver I'd push back hard on a lot of this stuff before merging it. I have plenty of slightly uggly drivers in staging to look after as it is!

I would feel more comfortable leaving the second loop as is for now. It's a speed optimization, which acknowledges all IRQs as fast as possible (=> before triggering further conversion of the raw data).

Quoted 10 lines

[...]

Quoted 9 lines
Quoted 8 lines

drivers/iio/adc/twl4030-madc.c line 518

Quoted 7 lines
drivers/iio/adc/twl4030-madc.c new file +7 -0
+static int twl4030_madc_start_conversion(struct twl4030_madc_data *madc,
+					 int conv_method)
+{
+	const struct twl4030_madc_conversion_method *method;
+	int ret = 0;
+	method = &twl4030_conversion_methods[conv_method];
+	switch (conv_method) {

Can we get here via any paths where these aren't the methods set?

Quoted 3 lines
Quoted 2 lines

conv_method is set from outside of the driver, so it's probably safer to check. This can be simplified once the old API is removed.

Then we should be returning an error if it's not one of these.

right and reading the code more carefully it should be tested before using it for array access.

Quoted 6 lines
Quoted 3 lines

I assume, that the driver will get a huge cleanup once all consumers of the old API are converted. Maybe a deprecation flag should be added together with this patchset?

Probably not worth bothering for an inkernel interface that seems to have relatively few users. How many out of tree users will be moving to new kernels and using this device?

At least no new twl4030-madc consumer has been posted to linux-kernel or linux-omap in the last months. So I will ignore this.

Quoted 8 lines

[...]

Quoted 4 lines
Quoted 2 lines

Personally I'd rank the driver as rather to vebose on read error messages given they are pretty unusual. Ah well I guess each to their own.

All of this is not written by me.

Sure. Doesn't stop me moaning :) At the end of the day you are proposing moving this driver into a place where it becomes at least partly my problem. As such I'm keen on any tidying / cleaning occuring ASAP.

Fair enough.

-- Sebastian

#16 lore raw

v2 cover Convert twl4030-madc to IIO API

Hi,

This is RFCv2 for converting twl4030-madc to the IIO API and adding DT support. The patchset compiles, but has not yet been tested on real hardware. I plan to convert rx51-battery to the IIO framework in the near future and test this patchset on my Nokia N900.

Changes since RFCv1:
* Add ti,system-uses-second-madc-irq DT property needed by Belisko Marek
* Fix issues reported by Jonathan Cameron

TODO:
* Test patchset on real hardware
* Convert consumer drivers to IIO API
- rx51-battery
- twl4030-madc-battery
- twl4030-madc-hwmon (=> deprecate in favour of iio-hwmon)
* remove old kernel API from twl4030-madc

-- Sebastian

Sebastian Reichel (5):
mfd: twl4030-madc: Use managed resources
mfd: twl4030-madc: Add DT support and convert to IIO framework
mfd: twl4030-madc: Cleanup driver
Documentation: DT: Document twl4030-madc binding
mfd: twl4030-madc: Move driver to drivers/iio/adc

 .../devicetree/bindings/iio/adc/twl4030-madc.txt   |  24 +
 drivers/iio/adc/Kconfig                            |  10 +
 drivers/iio/adc/Makefile                           |   1 +
 drivers/iio/adc/twl4030-madc.c                     | 914 +++++++++++++++++++++
 drivers/mfd/Kconfig                                |  10 -
 drivers/mfd/Makefile                               |   1 -
 drivers/mfd/twl4030-madc.c                         | 818 ------------------
 7 files changed, 949 insertions(+), 829 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/iio/adc/twl4030-madc.txt
 create mode 100644 drivers/iio/adc/twl4030-madc.c
 delete mode 100644 drivers/mfd/twl4030-madc.c
Signature · 1 line
1.8.5.3
#17 lore raw

RFC v2 1/5 mfd: twl4030-madc: Use managed resources

Update twl4030-madc driver to use managed resources.

---
 drivers/mfd/twl4030-madc.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
drivers/mfd/twl4030-madc.c +6 -9
index 4c583e4..5458561 100644
@@ -702,14 +702,14 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 {
 	struct twl4030_madc_data *madc;
 	struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev);
-	int ret;
+	int irq, ret;
 	u8 regval;
 
 	if (!pdata) {
 		dev_err(&pdev->dev, "platform_data not available\n");
 		return -EINVAL;
 	}
-	madc = kzalloc(sizeof(*madc), GFP_KERNEL);
+	madc = devm_kzalloc(&pdev->dev, sizeof(*madc), GFP_KERNEL);
 	if (!madc)
 		return -ENOMEM;
 
@@ -726,7 +726,7 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 	    TWL4030_MADC_ISR1 : TWL4030_MADC_ISR2;
 	ret = twl4030_madc_set_power(madc, 1);
 	if (ret < 0)
-		goto err_power;
+		return ret;
 	ret = twl4030_madc_set_current_generator(madc, 0, 1);
 	if (ret < 0)
 		goto err_current_generator;
@@ -770,7 +770,9 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 
 	platform_set_drvdata(pdev, madc);
 	mutex_init(&madc->lock);
-	ret = request_threaded_irq(platform_get_irq(pdev, 0), NULL,
+
+	irq = platform_get_irq(pdev, 0);
+	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
 				   twl4030_madc_threaded_irq_handler,
 				   IRQF_TRIGGER_RISING, "twl4030_madc", madc);
 	if (ret) {
@@ -783,9 +785,6 @@ err_i2c:
 	twl4030_madc_set_current_generator(madc, 0, 0);
 err_current_generator:
 	twl4030_madc_set_power(madc, 0);
-err_power:
-	kfree(madc);
-
 	return ret;
 }
 
@@ -793,10 +792,8 @@ static int twl4030_madc_remove(struct platform_device *pdev)
 {
 	struct twl4030_madc_data *madc = platform_get_drvdata(pdev);
 
-	free_irq(platform_get_irq(pdev, 0), madc);
 	twl4030_madc_set_current_generator(madc, 0, 0);
 	twl4030_madc_set_power(madc, 0);
-	kfree(madc);
 
 	return 0;
 }
Signature · 1 line
1.8.5.3
#18 lore raw

RFC v2 2/5 mfd: twl4030-madc: Add DT support and convert to IIO framework

In reply to Sebastian Reichel

This converts twl4030-madc module to use the Industrial IO ADC framework and adds device tree support.

---
 drivers/mfd/twl4030-madc.c | 121 ++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 114 insertions(+), 7 deletions(-)
drivers/mfd/twl4030-madc.c +114 -7
index 5458561..5e67c75 100644
@@ -47,6 +47,8 @@
 #include <linux/gfp.h>
 #include <linux/err.h>
 
+#include <linux/iio/iio.h>
+
 /*
  * struct twl4030_madc_data - a container for madc info
  * @dev - pointer to device structure for madc
@@ -59,10 +61,73 @@ struct twl4030_madc_data {
 	struct device *dev;
 	struct mutex lock;	/* mutex protecting this data structure */
 	struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS];
+	bool use_second_irq;
 	int imr;
 	int isr;
 };
 
+static int twl4030_madc_read(struct iio_dev *iio_dev,
+			     const struct iio_chan_spec *chan,
+			     int *val, int *val2, long mask)
+{
+	struct twl4030_madc_data *madc = iio_priv(iio_dev);
+	struct twl4030_madc_request req;
+	int channel = chan->channel;
+	int ret;
+
+	req.method = madc->use_second_irq ? TWL4030_MADC_SW2 : TWL4030_MADC_SW1;
+
+	req.channels = BIT(channel);
+	req.active = 0;
+	req.func_cb = NULL;
+	req.raw = !(mask & IIO_CHAN_INFO_PROCESSED);
+	req.do_avg = !!(mask & IIO_CHAN_INFO_AVERAGE_RAW);
+
+	ret = twl4030_madc_conversion(&req);
+	if (ret < 0)
+		return ret;
+
+	*val = req.rbuf[channel];
+
+	return IIO_VAL_INT;
+}
+
+static const struct iio_info twl4030_madc_iio_info = {
+	.read_raw = &twl4030_madc_read,
+	.driver_module = THIS_MODULE,
+};
+
+#define TWL4030_ADC_CHANNEL(_channel, _type, _name, _mask) {	\
+	.type = _type,					\
+	.channel = _channel,				\
+	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |  \
+			      BIT(IIO_CHAN_INFO_AVERAGE_RAW) | \
+			      _mask,			\
+	.datasheet_name = _name,			\
+	.indexed = 1,					\
+}
+
+static const struct iio_chan_spec twl4030_madc_iio_channels[] = {
+	TWL4030_ADC_CHANNEL(0, IIO_VOLTAGE, "ADCIN0", 0),
+	TWL4030_ADC_CHANNEL(1, IIO_TEMP, "ADCIN1",
+		BIT(IIO_CHAN_INFO_PROCESSED)),
+	TWL4030_ADC_CHANNEL(2, IIO_VOLTAGE, "ADCIN2", 0),
+	TWL4030_ADC_CHANNEL(3, IIO_VOLTAGE, "ADCIN3", 0),
+	TWL4030_ADC_CHANNEL(4, IIO_VOLTAGE, "ADCIN4", 0),
+	TWL4030_ADC_CHANNEL(5, IIO_VOLTAGE, "ADCIN5", 0),
+	TWL4030_ADC_CHANNEL(6, IIO_VOLTAGE, "ADCIN6", 0),
+	TWL4030_ADC_CHANNEL(7, IIO_VOLTAGE, "ADCIN7", 0),
+	TWL4030_ADC_CHANNEL(8, IIO_VOLTAGE, "ADCIN8", 0),
+	TWL4030_ADC_CHANNEL(9, IIO_VOLTAGE, "ADCIN9", 0),
+	TWL4030_ADC_CHANNEL(10, IIO_CURRENT, "ADCIN10",
+		BIT(IIO_CHAN_INFO_PROCESSED)),
+	TWL4030_ADC_CHANNEL(11, IIO_VOLTAGE, "ADCIN11", 0),
+	TWL4030_ADC_CHANNEL(12, IIO_VOLTAGE, "ADCIN12", 0),
+	TWL4030_ADC_CHANNEL(13, IIO_VOLTAGE, "ADCIN13", 0),
+	TWL4030_ADC_CHANNEL(14, IIO_VOLTAGE, "ADCIN14", 0),
+	TWL4030_ADC_CHANNEL(15, IIO_VOLTAGE, "ADCIN15", 0),
+};
+
 static struct twl4030_madc_data *twl4030_madc;
 
 struct twl4030_prescale_divider_ratios {
@@ -702,28 +767,50 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 {
 	struct twl4030_madc_data *madc;
 	struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev);
+	struct device_node *np = pdev->dev.of_node;
 	int irq, ret;
 	u8 regval;
+	struct iio_dev *iio_dev = NULL;
 
-	if (!pdata) {
+	if (!pdata && !np) {
 		dev_err(&pdev->dev, "platform_data not available\n");
 		return -EINVAL;
 	}
-	madc = devm_kzalloc(&pdev->dev, sizeof(*madc), GFP_KERNEL);
-	if (!madc)
+
+	iio_dev = devm_iio_device_alloc(&pdev->dev,
+					sizeof(struct twl4030_madc_data));
+	if (!iio_dev) {
+		dev_err(&pdev->dev, "failed allocating iio device\n");
 		return -ENOMEM;
+	}
 
+	madc = iio_priv(iio_dev);
 	madc->dev = &pdev->dev;
 
+	iio_dev->name = dev_name(&pdev->dev);
+	iio_dev->dev.parent = &pdev->dev;
+	iio_dev->dev.of_node = pdev->dev.of_node;
+	iio_dev->info = &twl4030_madc_iio_info;
+	iio_dev->modes = INDIO_DIRECT_MODE;
+	iio_dev->channels = twl4030_madc_iio_channels;
+	iio_dev->num_channels = 16;
+
 	/*
 	 * Phoenix provides 2 interrupt lines. The first one is connected to
 	 * the OMAP. The other one can be connected to the other processor such
 	 * as modem. Hence two separate ISR and IMR registers.
 	 */
-	madc->imr = (pdata->irq_line == 1) ?
+	if (pdata)
+		madc->use_second_irq = pdata->irq_line != 1;
+	else
+		madc->use_second_irq = of_property_read_bool(np,
+				       "ti,system-uses-second-madc-irq");
+
+	madc->imr = (madc->use_second_irq == 1) ?
 	    TWL4030_MADC_IMR1 : TWL4030_MADC_IMR2;
-	madc->isr = (pdata->irq_line == 1) ?
+	madc->isr = (madc->use_second_irq == 1) ?
 	    TWL4030_MADC_ISR1 : TWL4030_MADC_ISR2;
+
 	ret = twl4030_madc_set_power(madc, 1);
 	if (ret < 0)
 		return ret;
@@ -768,7 +855,7 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 		}
 	}
 
-	platform_set_drvdata(pdev, madc);
+	platform_set_drvdata(pdev, iio_dev);
 	mutex_init(&madc->lock);
 
 	irq = platform_get_irq(pdev, 0);
@@ -780,7 +867,15 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 		goto err_i2c;
 	}
 	twl4030_madc = madc;
+
+	ret = iio_device_register(iio_dev);
+	if (ret) {
+		dev_dbg(&pdev->dev, "could not register iio device\n");
+		goto err_i2c;
+	}
+
 	return 0;
+
 err_i2c:
 	twl4030_madc_set_current_generator(madc, 0, 0);
 err_current_generator:
@@ -790,20 +885,32 @@ err_current_generator:
 
 static int twl4030_madc_remove(struct platform_device *pdev)
 {
-	struct twl4030_madc_data *madc = platform_get_drvdata(pdev);
+	struct iio_dev *iio_dev = platform_get_drvdata(pdev);
+	struct twl4030_madc_data *madc = iio_priv(iio_dev);
 
 	twl4030_madc_set_current_generator(madc, 0, 0);
 	twl4030_madc_set_power(madc, 0);
 
+	iio_device_unregister(iio_dev);
+
 	return 0;
 }
 
+#ifdef CONFIG_OF
+static const struct of_device_id twl_madc_of_match[] = {
+	{.compatible = "ti,twl4030-madc", },
+	{ },
+};
+MODULE_DEVICE_TABLE(of, twl_madc_of_match);
+#endif
+
 static struct platform_driver twl4030_madc_driver = {
 	.probe = twl4030_madc_probe,
 	.remove = twl4030_madc_remove,
 	.driver = {
 		   .name = "twl4030_madc",
 		   .owner = THIS_MODULE,
+		   .of_match_table = of_match_ptr(twl_madc_of_match),
 		   },
 };
 
Signature · 1 line
1.8.5.3
#19 lore raw

RFC v2 3/5 mfd: twl4030-madc: Cleanup driver

In reply to Sebastian Reichel

Some style fixes in twl4030-madc driver.

---
 drivers/mfd/twl4030-madc.c | 66 ++++++++++++++++++++--------------------------
 1 file changed, 29 insertions(+), 37 deletions(-)
drivers/mfd/twl4030-madc.c +29 -37
index 5e67c75..0bc79a7 100644
@@ -62,8 +62,8 @@ struct twl4030_madc_data {
 	struct mutex lock;	/* mutex protecting this data structure */
 	struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS];
 	bool use_second_irq;
-	int imr;
-	int isr;
+	u8 imr;
+	u8 isr;
 };
 
 static int twl4030_madc_read(struct iio_dev *iio_dev,
@@ -242,13 +242,13 @@ static int twl4030battery_temperature(int raw_volt)
 		REG_BCICTL2);
 	if (ret < 0)
 		return ret;
+
 	curr = ((val & TWL4030_BCI_ITHEN) + 1) * 10;
 	/* Getting and calculating the thermistor resistance in ohms */
 	res = volt * 1000 / curr;
 	/* calculating temperature */
 	for (temp = 58; temp >= 0; temp--) {
 		int actual = therm_tbl[temp];
-
 		if ((actual - res) >= 0)
 			break;
 	}
@@ -270,6 +270,7 @@ static int twl4030battery_current(int raw_volt)
 	else /* slope of 0.88 mV/mA */
 		return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R2;
 }
+
 /*
  * Function to read channel values
  * @madc - pointer to twl4030_madc_data struct
@@ -285,17 +286,14 @@ static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
 				      long channels, int *buf,
 				      bool raw)
 {
-	int count = 0, count_req = 0, i;
-	u8 reg;
+	int count = 0, i;
 
 	for_each_set_bit(i, &channels, TWL4030_MADC_MAX_CHANNELS) {
-		reg = reg_base + 2 * i;
-		buf[i] = twl4030_madc_channel_raw_read(madc, reg);
+		buf[i] = twl4030_madc_channel_raw_read(madc, reg_base + 2 * i);
 		if (buf[i] < 0) {
-			dev_err(madc->dev,
-				"Unable to read register 0x%X\n", reg);
-			count_req++;
-			continue;
+			dev_err(madc->dev, "Unable to read register 0x%X\n",
+				reg_base + 2 * i);
+			return buf[i];
 		}
 		if (raw) {
 			count++;
@@ -306,7 +304,7 @@ static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
 			buf[i] = twl4030battery_current(buf[i]);
 			if (buf[i] < 0) {
 				dev_err(madc->dev, "err reading current\n");
-				count_req++;
+				return buf[i];
 			} else {
 				count++;
 				buf[i] = buf[i] - 750;
@@ -316,7 +314,7 @@ static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
 			buf[i] = twl4030battery_temperature(buf[i]);
 			if (buf[i] < 0) {
 				dev_err(madc->dev, "err reading temperature\n");
-				count_req++;
+				return buf[i];
 			} else {
 				buf[i] -= 3;
 				count++;
@@ -337,8 +335,6 @@ static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
 				twl4030_divider_ratios[i].numerator);
 		}
 	}
-	if (count_req)
-		dev_err(madc->dev, "%d channel conversion failed\n", count_req);
 
 	return count;
 }
@@ -362,13 +358,13 @@ static int twl4030_madc_enable_irq(struct twl4030_madc_data *madc, u8 id)
 			madc->imr);
 		return ret;
 	}
+
 	val &= ~(1 << id);
 	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr);
 	if (ret) {
 		dev_err(madc->dev,
 			"unable to write imr register 0x%X\n", madc->imr);
 		return ret;
-
 	}
 
 	return 0;
@@ -513,21 +509,17 @@ static int twl4030_madc_start_conversion(struct twl4030_madc_data *madc,
 {
 	const struct twl4030_madc_conversion_method *method;
 	int ret = 0;
+
+	if (conv_method != TWL4030_MADC_SW1 && conv_method != TWL4030_MADC_SW2)
+		return -ENOTSUPP;
+
 	method = &twl4030_conversion_methods[conv_method];
-	switch (conv_method) {
-	case TWL4030_MADC_SW1:
-	case TWL4030_MADC_SW2:
-		ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
-				       TWL4030_MADC_SW_START, method->ctrl);
-		if (ret) {
-			dev_err(madc->dev,
-				"unable to write ctrl register 0x%X\n",
-				method->ctrl);
-			return ret;
-		}
-		break;
-	default:
-		break;
+	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, TWL4030_MADC_SW_START,
+			       method->ctrl);
+	if (ret) {
+		dev_err(madc->dev, "unable to write ctrl register 0x%X\n",
+			method->ctrl);
+		return ret;
 	}
 
 	return 0;
@@ -625,7 +617,7 @@ int twl4030_madc_conversion(struct twl4030_madc_request *req)
 		if (ret) {
 			dev_err(twl4030_madc->dev,
 				"unable to write sel reg 0x%X\n",
-				method->sel + 1);
+				method->avg);
 			goto out;
 		}
 	}
@@ -666,10 +658,6 @@ out:
 }
 EXPORT_SYMBOL_GPL(twl4030_madc_conversion);
 
-/*
- * Return channel value
- * Or < 0 on failure.
- */
 int twl4030_get_madc_conversion(int channel_no)
 {
 	struct twl4030_madc_request req;
@@ -704,6 +692,7 @@ static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc,
 					      int chan, int on)
 {
 	int ret;
+	int regmask;
 	u8 regval;
 
 	ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE,
@@ -713,10 +702,13 @@ static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc,
 			TWL4030_BCI_BCICTL1);
 		return ret;
 	}
+
+	regmask = (chan == 0) ? TWL4030_BCI_TYPEN : TWL4030_BCI_ITHEN;
 	if (on)
-		regval |= chan ? TWL4030_BCI_ITHEN : TWL4030_BCI_TYPEN;
+		regval |= regmask;
 	else
-		regval &= chan ? ~TWL4030_BCI_ITHEN : ~TWL4030_BCI_TYPEN;
+		regval &= ~regmask;
+
 	ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
 			       regval, TWL4030_BCI_BCICTL1);
 	if (ret) {
Signature · 1 line
1.8.5.3
#20 lore raw

RFC v2 4/5 Documentation: DT: Document twl4030-madc binding

In reply to Sebastian Reichel

Add devicetree binding documentation for twl4030-madc analog digital converter.

---
 .../devicetree/bindings/iio/adc/twl4030-madc.txt   | 24 ++++++++++++++++++++++
 1 file changed, 24 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/adc/twl4030-madc.txt
Documentation/devicetree/bindings/iio/adc/twl4030-madc.txt new file +24 -0
new file mode 100644
index 0000000..6bdd214
@@ -0,0 +1,24 @@
+* TWL4030 Monitoring Analog to Digital Converter (MADC)
+
+The MADC subsystem in the TWL4030 consists of a 10-bit ADC
+combined with a 16-input analog multiplexer.
+
+Required properties:
+  - compatible: Should contain "ti,twl4030-madc".
+  - interrupts: IRQ line for the MADC submodule.
+  - #io-channel-cells: Should be set to <1>.
+
+Optional properties:
+  - ti,system-uses-second-madc-irq: boolean, set if the second madc irq register
+				    should be used, which is intended to be used
+				    by Co-Processors (e.g. a modem).
+
+Example:
+
+&twl {
+	madc {
+		compatible = "ti,twl4030-madc";
+		interrupts = <3>;
+		#io-channel-cells = <1>;
+	};
+};
Signature · 1 line
1.8.5.3
#21 lore raw

RFC v2 5/5 mfd: twl4030-madc: Move driver to drivers/iio/adc

In reply to Sebastian Reichel

This is a driver for an A/D converter, which belongs into drivers/iio/adc.

---
 drivers/iio/adc/Kconfig        |  10 +
 drivers/iio/adc/Makefile       |   1 +
 drivers/iio/adc/twl4030-madc.c | 914 +++++++++++++++++++++++++++++++++++++++++
 drivers/mfd/Kconfig            |  10 -
 drivers/mfd/Makefile           |   1 -
 drivers/mfd/twl4030-madc.c     | 914 -----------------------------------------
 6 files changed, 925 insertions(+), 925 deletions(-)
 create mode 100644 drivers/iio/adc/twl4030-madc.c
 delete mode 100644 drivers/mfd/twl4030-madc.c
drivers/iio/adc/Kconfig +10 -0
index 2209f28..427f75c 100644
@@ -183,6 +183,16 @@ config TI_AM335X_ADC
 	  Say yes here to build support for Texas Instruments ADC
 	  driver which is also a MFD client.
 
+config TWL4030_MADC
+	tristate "TWL4030 MADC (Monitoring A/D Converter)"
+	depends on TWL4030_CORE
+	help
+	This driver provides support for Triton TWL4030-MADC. The
+	driver supports both RT and SW conversion methods.
+
+	This driver can also be built as a module. If so, the module will be
+	called twl4030-madc.
+
 config TWL6030_GPADC
 	tristate "TWL6030 GPADC (General Purpose A/D Converter) Support"
 	depends on TWL4030_CORE
drivers/iio/adc/Makefile +1 -0
index ba9a10a..9acf2df 100644
@@ -20,5 +20,6 @@ obj-$(CONFIG_MCP3422) += mcp3422.o
 obj-$(CONFIG_NAU7802) += nau7802.o
 obj-$(CONFIG_TI_ADC081C) += ti-adc081c.o
 obj-$(CONFIG_TI_AM335X_ADC) += ti_am335x_adc.o
+obj-$(CONFIG_TWL4030_MADC) += twl4030-madc.o
 obj-$(CONFIG_TWL6030_GPADC) += twl6030-gpadc.o
 obj-$(CONFIG_VIPERBOARD_ADC) += viperboard_adc.o
drivers/iio/adc/twl4030-madc.c new file +914 -0

914 lines not on this page · raw message

drivers/mfd/Kconfig +0 -10
index 49bb445..23a8a51 100644
@@ -935,16 +935,6 @@ config TWL4030_CORE
 	  high speed USB OTG transceiver, an audio codec (on most
 	  versions) and many other features.
 
-config TWL4030_MADC
-	tristate "TI TWL4030 MADC"
-	depends on TWL4030_CORE
-	help
-	This driver provides support for triton TWL4030-MADC. The
-	driver supports both RT and SW conversion methods.
-
-	This driver can be built as a module. If so it will be
-	named twl4030-madc
-
 config TWL4030_POWER
 	bool "TI TWL4030 power resources"
 	depends on TWL4030_CORE && ARM
drivers/mfd/Makefile +0 -1
index 5aea5ef..c8eb0bc 100644
@@ -71,7 +71,6 @@ obj-$(CONFIG_MFD_TPS80031)	+= tps80031.o
 obj-$(CONFIG_MENELAUS)		+= menelaus.o
 
 obj-$(CONFIG_TWL4030_CORE)	+= twl-core.o twl4030-irq.o twl6030-irq.o
-obj-$(CONFIG_TWL4030_MADC)      += twl4030-madc.o
 obj-$(CONFIG_TWL4030_POWER)    += twl4030-power.o
 obj-$(CONFIG_MFD_TWL4030_AUDIO)	+= twl4030-audio.o
 obj-$(CONFIG_TWL6040_CORE)	+= twl6040.o
drivers/mfd/twl4030-madc.c deleted +0 -914

914 lines not on this page · raw message

Signature · 1 line
1.8.5.3
#22 lore raw

Re: [RFCv2 5/5] mfd: twl4030-madc: Move driver to drivers/iio/adc

Quoted 2 lines · Sebastian Reichel

This is a driver for an A/D converter, which belongs into drivers/iio/adc.

Please use git format-patch -M

drivers/iio/adc/twl4030-madc.c

Quoted 1 line
drivers/iio/adc/twl4030-madc.c +0 -0

[]

drivers/iio/adc/twl4030-madc.c line 72

Quoted 4 lines
drivers/iio/adc/twl4030-madc.c +4 -0
+static int twl4030_madc_read(struct iio_dev *iio_dev,
+			     const struct iio_chan_spec *chan,
+			     int *val, int *val2, long mask)
+{

[]

drivers/iio/adc/twl4030-madc.c line 84

Quoted 1 line
drivers/iio/adc/twl4030-madc.c +1 -0
+	req.do_avg = !!(mask & IIO_CHAN_INFO_AVERAGE_RAW);

It looks like do_avg should be bool not u16

#23 lore raw

Re: [RFCv2 5/5] mfd: twl4030-madc: Move driver to drivers/iio/adc

In reply to Sebastian Reichel
Quoted 3 lines

This is a driver for an A/D converter, which belongs into drivers/iio/adc.

like!

please find some comments below; not all might relate to the iio conversion

I plan to test this on a beagleboard-xm

I always needed the following setup to make the twl4030_madc work for me

/* TWL4030 CATKIT_ANA_CTRL Register */
#define TWL4030_CARKIT_ANA_CTRL 0xBB
#define SEL_MADC_MCPC (1 << 3)

	/*
	 * set SEL_MADC_MCPC in CARKIT_ANA_CTRL
	 * makes ADCIN3 to ADCIN6 available to the MADC
	 */
	twl_i2c_read_u8(TWL4030_MODULE_USB, &pin_mux,
						TWL4030_CARKIT_ANA_CTRL);
	pin_mux |= SEL_MADC_MCPC;
	twl_i2c_write_u8(TWL4030_MODULE_USB, pin_mux,
						TWL4030_CARKIT_ANA_CTRL);

regards, p.

Quoted 92 lines
---
 drivers/iio/adc/Kconfig        |  10 +
 drivers/iio/adc/Makefile       |   1 +
 drivers/iio/adc/twl4030-madc.c | 914 +++++++++++++++++++++++++++++++++++++++++
 drivers/mfd/Kconfig            |  10 -
 drivers/mfd/Makefile           |   1 -
 drivers/mfd/twl4030-madc.c     | 914 -----------------------------------------
 6 files changed, 925 insertions(+), 925 deletions(-)
 create mode 100644 drivers/iio/adc/twl4030-madc.c
 delete mode 100644 drivers/mfd/twl4030-madc.c
drivers/iio/adc/Kconfig +10 -0
index 2209f28..427f75c 100644
@@ -183,6 +183,16 @@ config TI_AM335X_ADC
 	  Say yes here to build support for Texas Instruments ADC
 	  driver which is also a MFD client.
 
+config TWL4030_MADC
+	tristate "TWL4030 MADC (Monitoring A/D Converter)"
+	depends on TWL4030_CORE
+	help
+	This driver provides support for Triton TWL4030-MADC. The
+	driver supports both RT and SW conversion methods.
+
+	This driver can also be built as a module. If so, the module will be
+	called twl4030-madc.
+
 config TWL6030_GPADC
 	tristate "TWL6030 GPADC (General Purpose A/D Converter) Support"
 	depends on TWL4030_CORE
drivers/iio/adc/Makefile +1 -0
index ba9a10a..9acf2df 100644
@@ -20,5 +20,6 @@ obj-$(CONFIG_MCP3422) += mcp3422.o
 obj-$(CONFIG_NAU7802) += nau7802.o
 obj-$(CONFIG_TI_ADC081C) += ti-adc081c.o
 obj-$(CONFIG_TI_AM335X_ADC) += ti_am335x_adc.o
+obj-$(CONFIG_TWL4030_MADC) += twl4030-madc.o
 obj-$(CONFIG_TWL6030_GPADC) += twl6030-gpadc.o
 obj-$(CONFIG_VIPERBOARD_ADC) += viperboard_adc.o
drivers/iio/adc/twl4030-madc.c new file +42 -0
new file mode 100644
index 0000000..0bc79a7
@@ -0,0 +1,914 @@
+/*
+ *
+ * TWL4030 MADC module driver-This driver monitors the real time
+ * conversion of analog signals like battery temperature,
+ * battery type, battery level etc.
+ *
+ * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
+ * J Keerthy <[email protected]>
+ *
+ * Based on twl4030-madc.c
+ * Copyright (C) 2008 Nokia Corporation
+ * Mikko Ylinen <[email protected]>
+ *
+ * Amit Kucheria <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ *
+ */
+
+#include <linux/init.h>
+#include <linux/device.h>
+#include <linux/interrupt.h>
+#include <linux/kernel.h>
+#include <linux/delay.h>
+#include <linux/platform_device.h>
+#include <linux/slab.h>
+#include <linux/i2c/twl.h>
+#include <linux/i2c/twl4030-madc.h>
+#include <linux/module.h>
+#include <linux/stddef.h>

drivers/iio/adc/twl4030-madc.c line 52

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+#include <linux/mutex.h>
+#include <linux/bitops.h>
+#include <linux/jiffies.h>
+#include <linux/types.h>
+#include <linux/gfp.h>
+#include <linux/err.h>
+
+#include <linux/iio/iio.h>
+
+/*

this could/should be kerneldoc, hecen /**

Quoted 99 lines
drivers/iio/adc/twl4030-madc.c new file +99 -0
+ * struct twl4030_madc_data - a container for madc info
+ * @dev - pointer to device structure for madc
+ * @lock - mutex protecting this data structure
+ * @requests - Array of request struct corresponding to SW1, SW2 and RT
+ * @imr - Interrupt mask register of MADC
+ * @isr - Interrupt status register of MADC
+ */
+struct twl4030_madc_data {
+	struct device *dev;
+	struct mutex lock;	/* mutex protecting this data structure */
+	struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS];
+	bool use_second_irq;
+	u8 imr;
+	u8 isr;
+};
+
+static int twl4030_madc_read(struct iio_dev *iio_dev,
+			     const struct iio_chan_spec *chan,
+			     int *val, int *val2, long mask)
+{
+	struct twl4030_madc_data *madc = iio_priv(iio_dev);
+	struct twl4030_madc_request req;
+	int channel = chan->channel;
+	int ret;
+
+	req.method = madc->use_second_irq ? TWL4030_MADC_SW2 : TWL4030_MADC_SW1;
+
+	req.channels = BIT(channel);
+	req.active = 0;
+	req.func_cb = NULL;
+	req.raw = !(mask & IIO_CHAN_INFO_PROCESSED);
+	req.do_avg = !!(mask & IIO_CHAN_INFO_AVERAGE_RAW);
+
+	ret = twl4030_madc_conversion(&req);
+	if (ret < 0)
+		return ret;
+
+	*val = req.rbuf[channel];
+
+	return IIO_VAL_INT;
+}
+
+static const struct iio_info twl4030_madc_iio_info = {
+	.read_raw = &twl4030_madc_read,
+	.driver_module = THIS_MODULE,
+};
+
+#define TWL4030_ADC_CHANNEL(_channel, _type, _name, _mask) {	\
+	.type = _type,					\
+	.channel = _channel,				\
+	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |  \
+			      BIT(IIO_CHAN_INFO_AVERAGE_RAW) | \
+			      _mask,			\
+	.datasheet_name = _name,			\
+	.indexed = 1,					\
+}
+
+static const struct iio_chan_spec twl4030_madc_iio_channels[] = {
+	TWL4030_ADC_CHANNEL(0, IIO_VOLTAGE, "ADCIN0", 0),
+	TWL4030_ADC_CHANNEL(1, IIO_TEMP, "ADCIN1",
+		BIT(IIO_CHAN_INFO_PROCESSED)),
+	TWL4030_ADC_CHANNEL(2, IIO_VOLTAGE, "ADCIN2", 0),
+	TWL4030_ADC_CHANNEL(3, IIO_VOLTAGE, "ADCIN3", 0),
+	TWL4030_ADC_CHANNEL(4, IIO_VOLTAGE, "ADCIN4", 0),
+	TWL4030_ADC_CHANNEL(5, IIO_VOLTAGE, "ADCIN5", 0),
+	TWL4030_ADC_CHANNEL(6, IIO_VOLTAGE, "ADCIN6", 0),
+	TWL4030_ADC_CHANNEL(7, IIO_VOLTAGE, "ADCIN7", 0),
+	TWL4030_ADC_CHANNEL(8, IIO_VOLTAGE, "ADCIN8", 0),
+	TWL4030_ADC_CHANNEL(9, IIO_VOLTAGE, "ADCIN9", 0),
+	TWL4030_ADC_CHANNEL(10, IIO_CURRENT, "ADCIN10",
+		BIT(IIO_CHAN_INFO_PROCESSED)),
+	TWL4030_ADC_CHANNEL(11, IIO_VOLTAGE, "ADCIN11", 0),
+	TWL4030_ADC_CHANNEL(12, IIO_VOLTAGE, "ADCIN12", 0),
+	TWL4030_ADC_CHANNEL(13, IIO_VOLTAGE, "ADCIN13", 0),
+	TWL4030_ADC_CHANNEL(14, IIO_VOLTAGE, "ADCIN14", 0),
+	TWL4030_ADC_CHANNEL(15, IIO_VOLTAGE, "ADCIN15", 0),
+};
+
+static struct twl4030_madc_data *twl4030_madc;
+
+struct twl4030_prescale_divider_ratios {
+	s16 numerator;
+	s16 denominator;
+};
+
+static const struct twl4030_prescale_divider_ratios
+twl4030_divider_ratios[16] = {
+	{1, 1},		/* CHANNEL 0 No Prescaler */
+	{1, 1},		/* CHANNEL 1 No Prescaler */
+	{6, 10},	/* CHANNEL 2 */
+	{6, 10},	/* CHANNEL 3 */
+	{6, 10},	/* CHANNEL 4 */
+	{6, 10},	/* CHANNEL 5 */
+	{6, 10},	/* CHANNEL 6 */
+	{6, 10},	/* CHANNEL 7 */
+	{3, 14},	/* CHANNEL 8 */
+	{1, 3},		/* CHANNEL 9 */
+	{1, 1},		/* CHANNEL 10 No Prescaler */
+	{15, 100},	/* CHANNEL 11 */

drivers/iio/adc/twl4030-madc.c line 161

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+	{1, 4},		/* CHANNEL 12 */
+	{1, 1},		/* CHANNEL 13 Reserved channels */
+	{1, 1},		/* CHANNEL 14 Reseved channels */
+	{5, 11},	/* CHANNEL 15 */
+};
+
+
+/*
+ * Conversion table from -3 to 55 degree Celcius
+ */

could use a single-line comment

drivers/iio/adc/twl4030-madc.c line 162

Quoted 1 line
drivers/iio/adc/twl4030-madc.c new file +1 -0
+static int therm_tbl[] = {

no twl4030_ prefix here

drivers/iio/adc/twl4030-madc.c line 169

Quoted 7 lines
drivers/iio/adc/twl4030-madc.c new file +7 -0
+30800,	29500,	28300,	27100,
+26000,	24900,	23900,	22900,	22000,	21100,	20300,	19400,	18700,	17900,
+17200,	16500,	15900,	15300,	14700,	14100,	13600,	13100,	12600,	12100,
+11600,	11200,	10800,	10400,	10000,	9630,	9280,	8950,	8620,	8310,
+8020,	7730,	7460,	7200,	6950,	6710,	6470,	6250,	6040,	5830,
+5640,	5450,	5260,	5090,	4920,	4760,	4600,	4450,	4310,	4170,
+4040,	3910,	3790,	3670,	3550

some indentation would be nice

Quoted 22 lines
drivers/iio/adc/twl4030-madc.c new file +22 -0
+};
+
+/*
+ * Structure containing the registers
+ * of different conversion methods supported by MADC.
+ * Hardware or RT real time conversion request initiated by external host
+ * processor for RT Signal conversions.
+ * External host processors can also request for non RT conversions
+ * SW1 and SW2 software conversions also called asynchronous or GPC request.
+ */
+static
+const struct twl4030_madc_conversion_method twl4030_conversion_methods[] = {
+	[TWL4030_MADC_RT] = {
+			     .sel = TWL4030_MADC_RTSELECT_LSB,
+			     .avg = TWL4030_MADC_RTAVERAGE_LSB,
+			     .rbase = TWL4030_MADC_RTCH0_LSB,
+			     },
+	[TWL4030_MADC_SW1] = {
+			      .sel = TWL4030_MADC_SW1SELECT_LSB,
+			      .avg = TWL4030_MADC_SW1AVERAGE_LSB,
+			      .rbase = TWL4030_MADC_GPCH0_LSB,
+			      .ctrl = TWL4030_MADC_CTRL_SW1,

drivers/iio/adc/twl4030-madc.c line 201

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+			      },
+	[TWL4030_MADC_SW2] = {
+			      .sel = TWL4030_MADC_SW2SELECT_LSB,
+			      .avg = TWL4030_MADC_SW2AVERAGE_LSB,
+			      .rbase = TWL4030_MADC_GPCH0_LSB,
+			      .ctrl = TWL4030_MADC_CTRL_SW2,
+			      },
+};
+
+/*

/**

Quoted 14 lines
drivers/iio/adc/twl4030-madc.c new file +14 -0
+ * Function to read a particular channel value.
+ * @madc - pointer to struct twl4030_madc_data
+ * @reg - lsb of ADC Channel
+ * If the i2c read fails it returns an error else returns 0.
+ */
+static int twl4030_madc_channel_raw_read(struct twl4030_madc_data *madc, u8 reg)
+{
+	u8 msb, lsb;
+	int ret;
+	/*
+	 * For each ADC channel, we have MSB and LSB register pair. MSB address
+	 * is always LSB address+1. reg parameter is the address of LSB register
+	 */
+	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &msb, reg + 1);

drivers/iio/adc/twl4030-madc.c line 225

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+	if (ret) {
+		dev_err(madc->dev, "unable to read MSB register 0x%X\n",
+			reg + 1);
+		return ret;
+	}
+	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &lsb, reg);
+	if (ret) {
+		dev_err(madc->dev, "unable to read LSB register 0x%X\n", reg);
+		return ret;
+	}

could this be a word/u16 read? I haven't checked if there is autoincrement support...

drivers/iio/adc/twl4030-madc.c line 231

Quoted 6 lines
drivers/iio/adc/twl4030-madc.c new file +6 -0
+
+	return (int)(((msb << 8) | lsb) >> 6);
+}
+
+/*
+ * Return battery temperature

stating the unit should nice

drivers/iio/adc/twl4030-madc.c line 240

Quoted 9 lines
drivers/iio/adc/twl4030-madc.c new file +9 -0
+ * Or < 0 on failure.
+ */
+static int twl4030battery_temperature(int raw_volt)
+{
+	u8 val;
+	int temp, curr, volt, res, ret;
+
+	volt = (raw_volt * TEMP_STEP_SIZE) / TEMP_PSR_R;
+	/* Getting and calculating the supply current in micro ampers */

amperes

Quoted 28 lines
drivers/iio/adc/twl4030-madc.c new file +28 -0
+	ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val,
+		REG_BCICTL2);
+	if (ret < 0)
+		return ret;
+
+	curr = ((val & TWL4030_BCI_ITHEN) + 1) * 10;
+	/* Getting and calculating the thermistor resistance in ohms */
+	res = volt * 1000 / curr;
+	/* calculating temperature */
+	for (temp = 58; temp >= 0; temp--) {
+		int actual = therm_tbl[temp];
+		if ((actual - res) >= 0)
+			break;
+	}
+
+	return temp + 1;
+}
+
+static int twl4030battery_current(int raw_volt)
+{
+	int ret;
+	u8 val;
+
+	ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val,
+		TWL4030_BCI_BCICTL1);
+	if (ret)
+		return ret;
+	if (val & TWL4030_BCI_CGAIN) /* slope of 0.44 mV/mA */

drivers/iio/adc/twl4030-madc.c line 278

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+		return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R1;
+	else /* slope of 0.88 mV/mA */
+		return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R2;
+}
+
+/*
+ * Function to read channel values
+ * @madc - pointer to twl4030_madc_data struct
+ * @reg_base - Base address of the first channel
+ * @Channels - 16 bit bitmap. If the bit is set, channel value is read

channels

Quoted 142 lines
drivers/iio/adc/twl4030-madc.c new file +142 -0
+ * @buf - The channel values are stored here. if read fails error
+ * @raw - Return raw values without conversion
+ * value is stored
+ * Returns the number of successfully read channels.
+ */
+static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
+				      u8 reg_base, unsigned
+				      long channels, int *buf,
+				      bool raw)
+{
+	int count = 0, i;
+
+	for_each_set_bit(i, &channels, TWL4030_MADC_MAX_CHANNELS) {
+		buf[i] = twl4030_madc_channel_raw_read(madc, reg_base + 2 * i);
+		if (buf[i] < 0) {
+			dev_err(madc->dev, "Unable to read register 0x%X\n",
+				reg_base + 2 * i);
+			return buf[i];
+		}
+		if (raw) {
+			count++;
+			continue;
+		}
+		switch (i) {
+		case 10:
+			buf[i] = twl4030battery_current(buf[i]);
+			if (buf[i] < 0) {
+				dev_err(madc->dev, "err reading current\n");
+				return buf[i];
+			} else {
+				count++;
+				buf[i] = buf[i] - 750;
+			}
+			break;
+		case 1:
+			buf[i] = twl4030battery_temperature(buf[i]);
+			if (buf[i] < 0) {
+				dev_err(madc->dev, "err reading temperature\n");
+				return buf[i];
+			} else {
+				buf[i] -= 3;
+				count++;
+			}
+			break;
+		default:
+			count++;
+			/* Analog Input (V) = conv_result * step_size / R
+			 * conv_result = decimal value of 10-bit conversion
+			 *		 result
+			 * step size = 1.5 / (2 ^ 10 -1)
+			 * R = Prescaler ratio for input channels.
+			 * Result given in mV hence multiplied by 1000.
+			 */
+			buf[i] = (buf[i] * 3 * 1000 *
+				 twl4030_divider_ratios[i].denominator)
+				/ (2 * 1023 *
+				twl4030_divider_ratios[i].numerator);
+		}
+	}
+
+	return count;
+}
+
+/*
+ * Enables irq.
+ * @madc - pointer to twl4030_madc_data struct
+ * @id - irq number to be enabled
+ * can take one of TWL4030_MADC_RT, TWL4030_MADC_SW1, TWL4030_MADC_SW2
+ * corresponding to RT, SW1, SW2 conversion requests.
+ * If the i2c read fails it returns an error else returns 0.
+ */
+static int twl4030_madc_enable_irq(struct twl4030_madc_data *madc, u8 id)
+{
+	u8 val;
+	int ret;
+
+	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr);
+	if (ret) {
+		dev_err(madc->dev, "unable to read imr register 0x%X\n",
+			madc->imr);
+		return ret;
+	}
+
+	val &= ~(1 << id);
+	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr);
+	if (ret) {
+		dev_err(madc->dev,
+			"unable to write imr register 0x%X\n", madc->imr);
+		return ret;
+	}
+
+	return 0;
+}
+
+/*
+ * Disables irq.
+ * @madc - pointer to twl4030_madc_data struct
+ * @id - irq number to be disabled
+ * can take one of TWL4030_MADC_RT, TWL4030_MADC_SW1, TWL4030_MADC_SW2
+ * corresponding to RT, SW1, SW2 conversion requests.
+ * Returns error if i2c read/write fails.
+ */
+static int twl4030_madc_disable_irq(struct twl4030_madc_data *madc, u8 id)
+{
+	u8 val;
+	int ret;
+
+	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr);
+	if (ret) {
+		dev_err(madc->dev, "unable to read imr register 0x%X\n",
+			madc->imr);
+		return ret;
+	}
+	val |= (1 << id);
+	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr);
+	if (ret) {
+		dev_err(madc->dev,
+			"unable to write imr register 0x%X\n", madc->imr);
+		return ret;
+	}
+
+	return 0;
+}
+
+static irqreturn_t twl4030_madc_threaded_irq_handler(int irq, void *_madc)
+{
+	struct twl4030_madc_data *madc = _madc;
+	const struct twl4030_madc_conversion_method *method;
+	u8 isr_val, imr_val;
+	int i, len, ret;
+	struct twl4030_madc_request *r;
+
+	mutex_lock(&madc->lock);
+	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &isr_val, madc->isr);
+	if (ret) {
+		dev_err(madc->dev, "unable to read isr register 0x%X\n",
+			madc->isr);
+		goto err_i2c;
+	}
+	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &imr_val, madc->imr);
+	if (ret) {
+		dev_err(madc->dev, "unable to read imr register 0x%X\n",

drivers/iio/adc/twl4030-madc.c line 430

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+			madc->imr);
+		goto err_i2c;
+	}
+	isr_val &= ~imr_val;
+	for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
+		if (!(isr_val & (1 << i)))
+			continue;
+		ret = twl4030_madc_disable_irq(madc, i);
+		if (ret < 0)
+			dev_dbg(madc->dev, "Disable interrupt failed%d\n", i);

some space before %d

Quoted 286 lines
drivers/iio/adc/twl4030-madc.c new file +286 -0
+		madc->requests[i].result_pending = 1;
+	}
+	for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
+		r = &madc->requests[i];
+		/* No pending results for this method, move to next one */
+		if (!r->result_pending)
+			continue;
+		method = &twl4030_conversion_methods[r->method];
+		/* Read results */
+		len = twl4030_madc_read_channels(madc, method->rbase,
+						 r->channels, r->rbuf, r->raw);
+		/* Return results to caller */
+		if (r->func_cb != NULL) {
+			r->func_cb(len, r->channels, r->rbuf);
+			r->func_cb = NULL;
+		}
+		/* Free request */
+		r->result_pending = 0;
+		r->active = 0;
+	}
+	mutex_unlock(&madc->lock);
+
+	return IRQ_HANDLED;
+
+err_i2c:
+	/*
+	 * In case of error check whichever request is active
+	 * and service the same.
+	 */
+	for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
+		r = &madc->requests[i];
+		if (r->active == 0)
+			continue;
+		method = &twl4030_conversion_methods[r->method];
+		/* Read results */
+		len = twl4030_madc_read_channels(madc, method->rbase,
+						 r->channels, r->rbuf, r->raw);
+		/* Return results to caller */
+		if (r->func_cb != NULL) {
+			r->func_cb(len, r->channels, r->rbuf);
+			r->func_cb = NULL;
+		}
+		/* Free request */
+		r->result_pending = 0;
+		r->active = 0;
+	}
+	mutex_unlock(&madc->lock);
+
+	return IRQ_HANDLED;
+}
+
+static int twl4030_madc_set_irq(struct twl4030_madc_data *madc,
+				struct twl4030_madc_request *req)
+{
+	struct twl4030_madc_request *p;
+	int ret;
+
+	p = &madc->requests[req->method];
+	memcpy(p, req, sizeof(*req));
+	ret = twl4030_madc_enable_irq(madc, req->method);
+	if (ret < 0) {
+		dev_err(madc->dev, "enable irq failed!!\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+/*
+ * Function which enables the madc conversion
+ * by writing to the control register.
+ * @madc - pointer to twl4030_madc_data struct
+ * @conv_method - can be TWL4030_MADC_RT, TWL4030_MADC_SW2, TWL4030_MADC_SW1
+ * corresponding to RT SW1 or SW2 conversion methods.
+ * Returns 0 if succeeds else a negative error value
+ */
+static int twl4030_madc_start_conversion(struct twl4030_madc_data *madc,
+					 int conv_method)
+{
+	const struct twl4030_madc_conversion_method *method;
+	int ret = 0;
+
+	if (conv_method != TWL4030_MADC_SW1 && conv_method != TWL4030_MADC_SW2)
+		return -ENOTSUPP;
+
+	method = &twl4030_conversion_methods[conv_method];
+	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, TWL4030_MADC_SW_START,
+			       method->ctrl);
+	if (ret) {
+		dev_err(madc->dev, "unable to write ctrl register 0x%X\n",
+			method->ctrl);
+		return ret;
+	}
+
+	return 0;
+}
+
+/*
+ * Function that waits for conversion to be ready
+ * @madc - pointer to twl4030_madc_data struct
+ * @timeout_ms - timeout value in milliseconds
+ * @status_reg - ctrl register
+ * returns 0 if succeeds else a negative error value
+ */
+static int twl4030_madc_wait_conversion_ready(struct twl4030_madc_data *madc,
+					      unsigned int timeout_ms,
+					      u8 status_reg)
+{
+	unsigned long timeout;
+	int ret;
+
+	timeout = jiffies + msecs_to_jiffies(timeout_ms);
+	do {
+		u8 reg;
+
+		ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &reg, status_reg);
+		if (ret) {
+			dev_err(madc->dev,
+				"unable to read status register 0x%X\n",
+				status_reg);
+			return ret;
+		}
+		if (!(reg & TWL4030_MADC_BUSY) && (reg & TWL4030_MADC_EOC_SW))
+			return 0;
+		usleep_range(500, 2000);
+	} while (!time_after(jiffies, timeout));
+	dev_err(madc->dev, "conversion timeout!\n");
+
+	return -EAGAIN;
+}
+
+/*
+ * An exported function which can be called from other kernel drivers.
+ * @req twl4030_madc_request structure
+ * req->rbuf will be filled with read values of channels based on the
+ * channel index. If a particular channel reading fails there will
+ * be a negative error value in the corresponding array element.
+ * returns 0 if succeeds else error value
+ */
+int twl4030_madc_conversion(struct twl4030_madc_request *req)
+{
+	const struct twl4030_madc_conversion_method *method;
+	u8 ch_msb, ch_lsb;
+	int ret;
+
+	if (!req || !twl4030_madc)
+		return -EINVAL;
+
+	mutex_lock(&twl4030_madc->lock);
+	if (req->method < TWL4030_MADC_RT || req->method > TWL4030_MADC_SW2) {
+		ret = -EINVAL;
+		goto out;
+	}
+	/* Do we have a conversion request ongoing */
+	if (twl4030_madc->requests[req->method].active) {
+		ret = -EBUSY;
+		goto out;
+	}
+	ch_msb = (req->channels >> 8) & 0xff;
+	ch_lsb = req->channels & 0xff;
+	method = &twl4030_conversion_methods[req->method];
+	/* Select channels to be converted */
+	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_msb, method->sel + 1);
+	if (ret) {
+		dev_err(twl4030_madc->dev,
+			"unable to write sel register 0x%X\n", method->sel + 1);
+		goto out;
+	}
+	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_lsb, method->sel);
+	if (ret) {
+		dev_err(twl4030_madc->dev,
+			"unable to write sel register 0x%X\n", method->sel + 1);
+		goto out;
+	}
+	/* Select averaging for all channels if do_avg is set */
+	if (req->do_avg) {
+		ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
+				       ch_msb, method->avg + 1);
+		if (ret) {
+			dev_err(twl4030_madc->dev,
+				"unable to write avg register 0x%X\n",
+				method->avg + 1);
+			goto out;
+		}
+		ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
+				       ch_lsb, method->avg);
+		if (ret) {
+			dev_err(twl4030_madc->dev,
+				"unable to write sel reg 0x%X\n",
+				method->avg);
+			goto out;
+		}
+	}
+	if (req->type == TWL4030_MADC_IRQ_ONESHOT && req->func_cb != NULL) {
+		ret = twl4030_madc_set_irq(twl4030_madc, req);
+		if (ret < 0)
+			goto out;
+		ret = twl4030_madc_start_conversion(twl4030_madc, req->method);
+		if (ret < 0)
+			goto out;
+		twl4030_madc->requests[req->method].active = 1;
+		ret = 0;
+		goto out;
+	}
+	/* With RT method we should not be here anymore */
+	if (req->method == TWL4030_MADC_RT) {
+		ret = -EINVAL;
+		goto out;
+	}
+	ret = twl4030_madc_start_conversion(twl4030_madc, req->method);
+	if (ret < 0)
+		goto out;
+	twl4030_madc->requests[req->method].active = 1;
+	/* Wait until conversion is ready (ctrl register returns EOC) */
+	ret = twl4030_madc_wait_conversion_ready(twl4030_madc, 5, method->ctrl);
+	if (ret) {
+		twl4030_madc->requests[req->method].active = 0;
+		goto out;
+	}
+	ret = twl4030_madc_read_channels(twl4030_madc, method->rbase,
+					 req->channels, req->rbuf, req->raw);
+	twl4030_madc->requests[req->method].active = 0;
+
+out:
+	mutex_unlock(&twl4030_madc->lock);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(twl4030_madc_conversion);
+
+int twl4030_get_madc_conversion(int channel_no)
+{
+	struct twl4030_madc_request req;
+	int temp = 0;
+	int ret;
+
+	req.channels = (1 << channel_no);
+	req.method = TWL4030_MADC_SW2;
+	req.active = 0;
+	req.func_cb = NULL;
+	ret = twl4030_madc_conversion(&req);
+	if (ret < 0)
+		return ret;
+	if (req.rbuf[channel_no] > 0)
+		temp = req.rbuf[channel_no];
+
+	return temp;
+}
+EXPORT_SYMBOL_GPL(twl4030_get_madc_conversion);
+
+/*
+ * Function to enable or disable bias current for
+ * main battery type reading or temperature sensing
+ * @madc - pointer to twl4030_madc_data struct
+ * @chan - can be one of the two values
+ * TWL4030_BCI_ITHEN - Enables bias current for main battery type reading
+ * TWL4030_BCI_TYPEN - Enables bias current for main battery temperature
+ * sensing
+ * @on - enable or disable chan.
+ */
+static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc,
+					      int chan, int on)
+{
+	int ret;
+	int regmask;
+	u8 regval;
+
+	ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE,
+			      &regval, TWL4030_BCI_BCICTL1);
+	if (ret) {
+		dev_err(madc->dev, "unable to read BCICTL1 reg 0x%X",
+			TWL4030_BCI_BCICTL1);
+		return ret;
+	}
+
+	regmask = (chan == 0) ? TWL4030_BCI_TYPEN : TWL4030_BCI_ITHEN;
+	if (on)
+		regval |= regmask;
+	else
+		regval &= ~regmask;
+
+	ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
+			       regval, TWL4030_BCI_BCICTL1);
+	if (ret) {
+		dev_err(madc->dev, "unable to write BCICTL1 reg 0x%X\n",
+			TWL4030_BCI_BCICTL1);

drivers/iio/adc/twl4030-madc.c line 726

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+		return ret;
+	}
+
+	return 0;
+}
+
+/*
+ * Function that sets MADC software power on bit to enable MADC
+ * @madc - pointer to twl4030_madc_data struct
+ * @on - Enable or disable MADC software powen on bit.

power

Quoted 52 lines
drivers/iio/adc/twl4030-madc.c new file +52 -0
+ * returns error if i2c read/write fails else 0
+ */
+static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on)
+{
+	u8 regval;
+	int ret;
+
+	ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE,
+			      &regval, TWL4030_MADC_CTRL1);
+	if (ret) {
+		dev_err(madc->dev, "unable to read madc ctrl1 reg 0x%X\n",
+			TWL4030_MADC_CTRL1);
+		return ret;
+	}
+	if (on)
+		regval |= TWL4030_MADC_MADCON;
+	else
+		regval &= ~TWL4030_MADC_MADCON;
+	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, regval, TWL4030_MADC_CTRL1);
+	if (ret) {
+		dev_err(madc->dev, "unable to write madc ctrl1 reg 0x%X\n",
+			TWL4030_MADC_CTRL1);
+		return ret;
+	}
+
+	return 0;
+}
+
+/*
+ * Initialize MADC and request for threaded irq
+ */
+static int twl4030_madc_probe(struct platform_device *pdev)
+{
+	struct twl4030_madc_data *madc;
+	struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev);
+	struct device_node *np = pdev->dev.of_node;
+	int irq, ret;
+	u8 regval;
+	struct iio_dev *iio_dev = NULL;
+
+	if (!pdata && !np) {
+		dev_err(&pdev->dev, "platform_data not available\n");
+		return -EINVAL;
+	}
+
+	iio_dev = devm_iio_device_alloc(&pdev->dev,
+					sizeof(struct twl4030_madc_data));
+	if (!iio_dev) {
+		dev_err(&pdev->dev, "failed allocating iio device\n");
+		return -ENOMEM;
+	}
+

drivers/iio/adc/twl4030-madc.c line 788

Quoted 10 lines
drivers/iio/adc/twl4030-madc.c new file +10 -0
+	madc = iio_priv(iio_dev);
+	madc->dev = &pdev->dev;
+
+	iio_dev->name = dev_name(&pdev->dev);
+	iio_dev->dev.parent = &pdev->dev;
+	iio_dev->dev.of_node = pdev->dev.of_node;
+	iio_dev->info = &twl4030_madc_iio_info;
+	iio_dev->modes = INDIO_DIRECT_MODE;
+	iio_dev->channels = twl4030_madc_iio_channels;
+	iio_dev->num_channels = 16;

ARRAY_SIZE(twl4030_madc_iio_channels)

Quoted 1,080 lines
drivers/iio/adc/twl4030-madc.c new file +126 -0
+
+	/*
+	 * Phoenix provides 2 interrupt lines. The first one is connected to
+	 * the OMAP. The other one can be connected to the other processor such
+	 * as modem. Hence two separate ISR and IMR registers.
+	 */
+	if (pdata)
+		madc->use_second_irq = pdata->irq_line != 1;
+	else
+		madc->use_second_irq = of_property_read_bool(np,
+				       "ti,system-uses-second-madc-irq");
+
+	madc->imr = (madc->use_second_irq == 1) ?
+	    TWL4030_MADC_IMR1 : TWL4030_MADC_IMR2;
+	madc->isr = (madc->use_second_irq == 1) ?
+	    TWL4030_MADC_ISR1 : TWL4030_MADC_ISR2;
+
+	ret = twl4030_madc_set_power(madc, 1);
+	if (ret < 0)
+		return ret;
+	ret = twl4030_madc_set_current_generator(madc, 0, 1);
+	if (ret < 0)
+		goto err_current_generator;
+
+	ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE,
+			      &regval, TWL4030_BCI_BCICTL1);
+	if (ret) {
+		dev_err(&pdev->dev, "unable to read reg BCI CTL1 0x%X\n",
+			TWL4030_BCI_BCICTL1);
+		goto err_i2c;
+	}
+	regval |= TWL4030_BCI_MESBAT;
+	ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
+			       regval, TWL4030_BCI_BCICTL1);
+	if (ret) {
+		dev_err(&pdev->dev, "unable to write reg BCI Ctl1 0x%X\n",
+			TWL4030_BCI_BCICTL1);
+		goto err_i2c;
+	}
+
+	/* Check that MADC clock is on */
+	ret = twl_i2c_read_u8(TWL4030_MODULE_INTBR, &regval, TWL4030_REG_GPBR1);
+	if (ret) {
+		dev_err(&pdev->dev, "unable to read reg GPBR1 0x%X\n",
+				TWL4030_REG_GPBR1);
+		goto err_i2c;
+	}
+
+	/* If MADC clk is not on, turn it on */
+	if (!(regval & TWL4030_GPBR1_MADC_HFCLK_EN)) {
+		dev_info(&pdev->dev, "clk disabled, enabling\n");
+		regval |= TWL4030_GPBR1_MADC_HFCLK_EN;
+		ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, regval,
+				       TWL4030_REG_GPBR1);
+		if (ret) {
+			dev_err(&pdev->dev, "unable to write reg GPBR1 0x%X\n",
+					TWL4030_REG_GPBR1);
+			goto err_i2c;
+		}
+	}
+
+	platform_set_drvdata(pdev, iio_dev);
+	mutex_init(&madc->lock);
+
+	irq = platform_get_irq(pdev, 0);
+	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
+				   twl4030_madc_threaded_irq_handler,
+				   IRQF_TRIGGER_RISING, "twl4030_madc", madc);
+	if (ret) {
+		dev_dbg(&pdev->dev, "could not request irq\n");
+		goto err_i2c;
+	}
+	twl4030_madc = madc;
+
+	ret = iio_device_register(iio_dev);
+	if (ret) {
+		dev_dbg(&pdev->dev, "could not register iio device\n");
+		goto err_i2c;
+	}
+
+	return 0;
+
+err_i2c:
+	twl4030_madc_set_current_generator(madc, 0, 0);
+err_current_generator:
+	twl4030_madc_set_power(madc, 0);
+	return ret;
+}
+
+static int twl4030_madc_remove(struct platform_device *pdev)
+{
+	struct iio_dev *iio_dev = platform_get_drvdata(pdev);
+	struct twl4030_madc_data *madc = iio_priv(iio_dev);
+
+	twl4030_madc_set_current_generator(madc, 0, 0);
+	twl4030_madc_set_power(madc, 0);
+
+	iio_device_unregister(iio_dev);
+
+	return 0;
+}
+
+#ifdef CONFIG_OF
+static const struct of_device_id twl_madc_of_match[] = {
+	{.compatible = "ti,twl4030-madc", },
+	{ },
+};
+MODULE_DEVICE_TABLE(of, twl_madc_of_match);
+#endif
+
+static struct platform_driver twl4030_madc_driver = {
+	.probe = twl4030_madc_probe,
+	.remove = twl4030_madc_remove,
+	.driver = {
+		   .name = "twl4030_madc",
+		   .owner = THIS_MODULE,
+		   .of_match_table = of_match_ptr(twl_madc_of_match),
+		   },
+};
+
+module_platform_driver(twl4030_madc_driver);
+
+MODULE_DESCRIPTION("TWL4030 ADC driver");
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("J Keerthy");
+MODULE_ALIAS("platform:twl4030_madc");
drivers/mfd/Kconfig +0 -10
index 49bb445..23a8a51 100644
@@ -935,16 +935,6 @@ config TWL4030_CORE
 	  high speed USB OTG transceiver, an audio codec (on most
 	  versions) and many other features.
 
-config TWL4030_MADC
-	tristate "TI TWL4030 MADC"
-	depends on TWL4030_CORE
-	help
-	This driver provides support for triton TWL4030-MADC. The
-	driver supports both RT and SW conversion methods.
-
-	This driver can be built as a module. If so it will be
-	named twl4030-madc
-
 config TWL4030_POWER
 	bool "TI TWL4030 power resources"
 	depends on TWL4030_CORE && ARM
drivers/mfd/Makefile +0 -1
index 5aea5ef..c8eb0bc 100644
@@ -71,7 +71,6 @@ obj-$(CONFIG_MFD_TPS80031)	+= tps80031.o
 obj-$(CONFIG_MENELAUS)		+= menelaus.o
 
 obj-$(CONFIG_TWL4030_CORE)	+= twl-core.o twl4030-irq.o twl6030-irq.o
-obj-$(CONFIG_TWL4030_MADC)      += twl4030-madc.o
 obj-$(CONFIG_TWL4030_POWER)    += twl4030-power.o
 obj-$(CONFIG_MFD_TWL4030_AUDIO)	+= twl4030-audio.o
 obj-$(CONFIG_TWL6040_CORE)	+= twl6040.o
drivers/mfd/twl4030-madc.c deleted +0 -914
deleted file mode 100644
index 0bc79a7..0000000
@@ -1,914 +0,0 @@
-/*
- *
- * TWL4030 MADC module driver-This driver monitors the real time
- * conversion of analog signals like battery temperature,
- * battery type, battery level etc.
- *
- * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
- * J Keerthy <[email protected]>
- *
- * Based on twl4030-madc.c
- * Copyright (C) 2008 Nokia Corporation
- * Mikko Ylinen <[email protected]>
- *
- * Amit Kucheria <[email protected]>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * version 2 as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA
- *
- */
-
-#include <linux/init.h>
-#include <linux/device.h>
-#include <linux/interrupt.h>
-#include <linux/kernel.h>
-#include <linux/delay.h>
-#include <linux/platform_device.h>
-#include <linux/slab.h>
-#include <linux/i2c/twl.h>
-#include <linux/i2c/twl4030-madc.h>
-#include <linux/module.h>
-#include <linux/stddef.h>
-#include <linux/mutex.h>
-#include <linux/bitops.h>
-#include <linux/jiffies.h>
-#include <linux/types.h>
-#include <linux/gfp.h>
-#include <linux/err.h>
-
-#include <linux/iio/iio.h>
-
-/*
- * struct twl4030_madc_data - a container for madc info
- * @dev - pointer to device structure for madc
- * @lock - mutex protecting this data structure
- * @requests - Array of request struct corresponding to SW1, SW2 and RT
- * @imr - Interrupt mask register of MADC
- * @isr - Interrupt status register of MADC
- */
-struct twl4030_madc_data {
-	struct device *dev;
-	struct mutex lock;	/* mutex protecting this data structure */
-	struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS];
-	bool use_second_irq;
-	u8 imr;
-	u8 isr;
-};
-
-static int twl4030_madc_read(struct iio_dev *iio_dev,
-			     const struct iio_chan_spec *chan,
-			     int *val, int *val2, long mask)
-{
-	struct twl4030_madc_data *madc = iio_priv(iio_dev);
-	struct twl4030_madc_request req;
-	int channel = chan->channel;
-	int ret;
-
-	req.method = madc->use_second_irq ? TWL4030_MADC_SW2 : TWL4030_MADC_SW1;
-
-	req.channels = BIT(channel);
-	req.active = 0;
-	req.func_cb = NULL;
-	req.raw = !(mask & IIO_CHAN_INFO_PROCESSED);
-	req.do_avg = !!(mask & IIO_CHAN_INFO_AVERAGE_RAW);
-
-	ret = twl4030_madc_conversion(&req);
-	if (ret < 0)
-		return ret;
-
-	*val = req.rbuf[channel];
-
-	return IIO_VAL_INT;
-}
-
-static const struct iio_info twl4030_madc_iio_info = {
-	.read_raw = &twl4030_madc_read,
-	.driver_module = THIS_MODULE,
-};
-
-#define TWL4030_ADC_CHANNEL(_channel, _type, _name, _mask) {	\
-	.type = _type,					\
-	.channel = _channel,				\
-	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |  \
-			      BIT(IIO_CHAN_INFO_AVERAGE_RAW) | \
-			      _mask,			\
-	.datasheet_name = _name,			\
-	.indexed = 1,					\
-}
-
-static const struct iio_chan_spec twl4030_madc_iio_channels[] = {
-	TWL4030_ADC_CHANNEL(0, IIO_VOLTAGE, "ADCIN0", 0),
-	TWL4030_ADC_CHANNEL(1, IIO_TEMP, "ADCIN1",
-		BIT(IIO_CHAN_INFO_PROCESSED)),
-	TWL4030_ADC_CHANNEL(2, IIO_VOLTAGE, "ADCIN2", 0),
-	TWL4030_ADC_CHANNEL(3, IIO_VOLTAGE, "ADCIN3", 0),
-	TWL4030_ADC_CHANNEL(4, IIO_VOLTAGE, "ADCIN4", 0),
-	TWL4030_ADC_CHANNEL(5, IIO_VOLTAGE, "ADCIN5", 0),
-	TWL4030_ADC_CHANNEL(6, IIO_VOLTAGE, "ADCIN6", 0),
-	TWL4030_ADC_CHANNEL(7, IIO_VOLTAGE, "ADCIN7", 0),
-	TWL4030_ADC_CHANNEL(8, IIO_VOLTAGE, "ADCIN8", 0),
-	TWL4030_ADC_CHANNEL(9, IIO_VOLTAGE, "ADCIN9", 0),
-	TWL4030_ADC_CHANNEL(10, IIO_CURRENT, "ADCIN10",
-		BIT(IIO_CHAN_INFO_PROCESSED)),
-	TWL4030_ADC_CHANNEL(11, IIO_VOLTAGE, "ADCIN11", 0),
-	TWL4030_ADC_CHANNEL(12, IIO_VOLTAGE, "ADCIN12", 0),
-	TWL4030_ADC_CHANNEL(13, IIO_VOLTAGE, "ADCIN13", 0),
-	TWL4030_ADC_CHANNEL(14, IIO_VOLTAGE, "ADCIN14", 0),
-	TWL4030_ADC_CHANNEL(15, IIO_VOLTAGE, "ADCIN15", 0),
-};
-
-static struct twl4030_madc_data *twl4030_madc;
-
-struct twl4030_prescale_divider_ratios {
-	s16 numerator;
-	s16 denominator;
-};
-
-static const struct twl4030_prescale_divider_ratios
-twl4030_divider_ratios[16] = {
-	{1, 1},		/* CHANNEL 0 No Prescaler */
-	{1, 1},		/* CHANNEL 1 No Prescaler */
-	{6, 10},	/* CHANNEL 2 */
-	{6, 10},	/* CHANNEL 3 */
-	{6, 10},	/* CHANNEL 4 */
-	{6, 10},	/* CHANNEL 5 */
-	{6, 10},	/* CHANNEL 6 */
-	{6, 10},	/* CHANNEL 7 */
-	{3, 14},	/* CHANNEL 8 */
-	{1, 3},		/* CHANNEL 9 */
-	{1, 1},		/* CHANNEL 10 No Prescaler */
-	{15, 100},	/* CHANNEL 11 */
-	{1, 4},		/* CHANNEL 12 */
-	{1, 1},		/* CHANNEL 13 Reserved channels */
-	{1, 1},		/* CHANNEL 14 Reseved channels */
-	{5, 11},	/* CHANNEL 15 */
-};
-
-
-/*
- * Conversion table from -3 to 55 degree Celcius
- */
-static int therm_tbl[] = {
-30800,	29500,	28300,	27100,
-26000,	24900,	23900,	22900,	22000,	21100,	20300,	19400,	18700,	17900,
-17200,	16500,	15900,	15300,	14700,	14100,	13600,	13100,	12600,	12100,
-11600,	11200,	10800,	10400,	10000,	9630,	9280,	8950,	8620,	8310,
-8020,	7730,	7460,	7200,	6950,	6710,	6470,	6250,	6040,	5830,
-5640,	5450,	5260,	5090,	4920,	4760,	4600,	4450,	4310,	4170,
-4040,	3910,	3790,	3670,	3550
-};
-
-/*
- * Structure containing the registers
- * of different conversion methods supported by MADC.
- * Hardware or RT real time conversion request initiated by external host
- * processor for RT Signal conversions.
- * External host processors can also request for non RT conversions
- * SW1 and SW2 software conversions also called asynchronous or GPC request.
- */
-static
-const struct twl4030_madc_conversion_method twl4030_conversion_methods[] = {
-	[TWL4030_MADC_RT] = {
-			     .sel = TWL4030_MADC_RTSELECT_LSB,
-			     .avg = TWL4030_MADC_RTAVERAGE_LSB,
-			     .rbase = TWL4030_MADC_RTCH0_LSB,
-			     },
-	[TWL4030_MADC_SW1] = {
-			      .sel = TWL4030_MADC_SW1SELECT_LSB,
-			      .avg = TWL4030_MADC_SW1AVERAGE_LSB,
-			      .rbase = TWL4030_MADC_GPCH0_LSB,
-			      .ctrl = TWL4030_MADC_CTRL_SW1,
-			      },
-	[TWL4030_MADC_SW2] = {
-			      .sel = TWL4030_MADC_SW2SELECT_LSB,
-			      .avg = TWL4030_MADC_SW2AVERAGE_LSB,
-			      .rbase = TWL4030_MADC_GPCH0_LSB,
-			      .ctrl = TWL4030_MADC_CTRL_SW2,
-			      },
-};
-
-/*
- * Function to read a particular channel value.
- * @madc - pointer to struct twl4030_madc_data
- * @reg - lsb of ADC Channel
- * If the i2c read fails it returns an error else returns 0.
- */
-static int twl4030_madc_channel_raw_read(struct twl4030_madc_data *madc, u8 reg)
-{
-	u8 msb, lsb;
-	int ret;
-	/*
-	 * For each ADC channel, we have MSB and LSB register pair. MSB address
-	 * is always LSB address+1. reg parameter is the address of LSB register
-	 */
-	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &msb, reg + 1);
-	if (ret) {
-		dev_err(madc->dev, "unable to read MSB register 0x%X\n",
-			reg + 1);
-		return ret;
-	}
-	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &lsb, reg);
-	if (ret) {
-		dev_err(madc->dev, "unable to read LSB register 0x%X\n", reg);
-		return ret;
-	}
-
-	return (int)(((msb << 8) | lsb) >> 6);
-}
-
-/*
- * Return battery temperature
- * Or < 0 on failure.
- */
-static int twl4030battery_temperature(int raw_volt)
-{
-	u8 val;
-	int temp, curr, volt, res, ret;
-
-	volt = (raw_volt * TEMP_STEP_SIZE) / TEMP_PSR_R;
-	/* Getting and calculating the supply current in micro ampers */
-	ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val,
-		REG_BCICTL2);
-	if (ret < 0)
-		return ret;
-
-	curr = ((val & TWL4030_BCI_ITHEN) + 1) * 10;
-	/* Getting and calculating the thermistor resistance in ohms */
-	res = volt * 1000 / curr;
-	/* calculating temperature */
-	for (temp = 58; temp >= 0; temp--) {
-		int actual = therm_tbl[temp];
-		if ((actual - res) >= 0)
-			break;
-	}
-
-	return temp + 1;
-}
-
-static int twl4030battery_current(int raw_volt)
-{
-	int ret;
-	u8 val;
-
-	ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val,
-		TWL4030_BCI_BCICTL1);
-	if (ret)
-		return ret;
-	if (val & TWL4030_BCI_CGAIN) /* slope of 0.44 mV/mA */
-		return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R1;
-	else /* slope of 0.88 mV/mA */
-		return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R2;
-}
-
-/*
- * Function to read channel values
- * @madc - pointer to twl4030_madc_data struct
- * @reg_base - Base address of the first channel
- * @Channels - 16 bit bitmap. If the bit is set, channel value is read
- * @buf - The channel values are stored here. if read fails error
- * @raw - Return raw values without conversion
- * value is stored
- * Returns the number of successfully read channels.
- */
-static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
-				      u8 reg_base, unsigned
-				      long channels, int *buf,
-				      bool raw)
-{
-	int count = 0, i;
-
-	for_each_set_bit(i, &channels, TWL4030_MADC_MAX_CHANNELS) {
-		buf[i] = twl4030_madc_channel_raw_read(madc, reg_base + 2 * i);
-		if (buf[i] < 0) {
-			dev_err(madc->dev, "Unable to read register 0x%X\n",
-				reg_base + 2 * i);
-			return buf[i];
-		}
-		if (raw) {
-			count++;
-			continue;
-		}
-		switch (i) {
-		case 10:
-			buf[i] = twl4030battery_current(buf[i]);
-			if (buf[i] < 0) {
-				dev_err(madc->dev, "err reading current\n");
-				return buf[i];
-			} else {
-				count++;
-				buf[i] = buf[i] - 750;
-			}
-			break;
-		case 1:
-			buf[i] = twl4030battery_temperature(buf[i]);
-			if (buf[i] < 0) {
-				dev_err(madc->dev, "err reading temperature\n");
-				return buf[i];
-			} else {
-				buf[i] -= 3;
-				count++;
-			}
-			break;
-		default:
-			count++;
-			/* Analog Input (V) = conv_result * step_size / R
-			 * conv_result = decimal value of 10-bit conversion
-			 *		 result
-			 * step size = 1.5 / (2 ^ 10 -1)
-			 * R = Prescaler ratio for input channels.
-			 * Result given in mV hence multiplied by 1000.
-			 */
-			buf[i] = (buf[i] * 3 * 1000 *
-				 twl4030_divider_ratios[i].denominator)
-				/ (2 * 1023 *
-				twl4030_divider_ratios[i].numerator);
-		}
-	}
-
-	return count;
-}
-
-/*
- * Enables irq.
- * @madc - pointer to twl4030_madc_data struct
- * @id - irq number to be enabled
- * can take one of TWL4030_MADC_RT, TWL4030_MADC_SW1, TWL4030_MADC_SW2
- * corresponding to RT, SW1, SW2 conversion requests.
- * If the i2c read fails it returns an error else returns 0.
- */
-static int twl4030_madc_enable_irq(struct twl4030_madc_data *madc, u8 id)
-{
-	u8 val;
-	int ret;
-
-	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr);
-	if (ret) {
-		dev_err(madc->dev, "unable to read imr register 0x%X\n",
-			madc->imr);
-		return ret;
-	}
-
-	val &= ~(1 << id);
-	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr);
-	if (ret) {
-		dev_err(madc->dev,
-			"unable to write imr register 0x%X\n", madc->imr);
-		return ret;
-	}
-
-	return 0;
-}
-
-/*
- * Disables irq.
- * @madc - pointer to twl4030_madc_data struct
- * @id - irq number to be disabled
- * can take one of TWL4030_MADC_RT, TWL4030_MADC_SW1, TWL4030_MADC_SW2
- * corresponding to RT, SW1, SW2 conversion requests.
- * Returns error if i2c read/write fails.
- */
-static int twl4030_madc_disable_irq(struct twl4030_madc_data *madc, u8 id)
-{
-	u8 val;
-	int ret;
-
-	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr);
-	if (ret) {
-		dev_err(madc->dev, "unable to read imr register 0x%X\n",
-			madc->imr);
-		return ret;
-	}
-	val |= (1 << id);
-	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr);
-	if (ret) {
-		dev_err(madc->dev,
-			"unable to write imr register 0x%X\n", madc->imr);
-		return ret;
-	}
-
-	return 0;
-}
-
-static irqreturn_t twl4030_madc_threaded_irq_handler(int irq, void *_madc)
-{
-	struct twl4030_madc_data *madc = _madc;
-	const struct twl4030_madc_conversion_method *method;
-	u8 isr_val, imr_val;
-	int i, len, ret;
-	struct twl4030_madc_request *r;
-
-	mutex_lock(&madc->lock);
-	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &isr_val, madc->isr);
-	if (ret) {
-		dev_err(madc->dev, "unable to read isr register 0x%X\n",
-			madc->isr);
-		goto err_i2c;
-	}
-	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &imr_val, madc->imr);
-	if (ret) {
-		dev_err(madc->dev, "unable to read imr register 0x%X\n",
-			madc->imr);
-		goto err_i2c;
-	}
-	isr_val &= ~imr_val;
-	for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
-		if (!(isr_val & (1 << i)))
-			continue;
-		ret = twl4030_madc_disable_irq(madc, i);
-		if (ret < 0)
-			dev_dbg(madc->dev, "Disable interrupt failed%d\n", i);
-		madc->requests[i].result_pending = 1;
-	}
-	for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
-		r = &madc->requests[i];
-		/* No pending results for this method, move to next one */
-		if (!r->result_pending)
-			continue;
-		method = &twl4030_conversion_methods[r->method];
-		/* Read results */
-		len = twl4030_madc_read_channels(madc, method->rbase,
-						 r->channels, r->rbuf, r->raw);
-		/* Return results to caller */
-		if (r->func_cb != NULL) {
-			r->func_cb(len, r->channels, r->rbuf);
-			r->func_cb = NULL;
-		}
-		/* Free request */
-		r->result_pending = 0;
-		r->active = 0;
-	}
-	mutex_unlock(&madc->lock);
-
-	return IRQ_HANDLED;
-
-err_i2c:
-	/*
-	 * In case of error check whichever request is active
-	 * and service the same.
-	 */
-	for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
-		r = &madc->requests[i];
-		if (r->active == 0)
-			continue;
-		method = &twl4030_conversion_methods[r->method];
-		/* Read results */
-		len = twl4030_madc_read_channels(madc, method->rbase,
-						 r->channels, r->rbuf, r->raw);
-		/* Return results to caller */
-		if (r->func_cb != NULL) {
-			r->func_cb(len, r->channels, r->rbuf);
-			r->func_cb = NULL;
-		}
-		/* Free request */
-		r->result_pending = 0;
-		r->active = 0;
-	}
-	mutex_unlock(&madc->lock);
-
-	return IRQ_HANDLED;
-}
-
-static int twl4030_madc_set_irq(struct twl4030_madc_data *madc,
-				struct twl4030_madc_request *req)
-{
-	struct twl4030_madc_request *p;
-	int ret;
-
-	p = &madc->requests[req->method];
-	memcpy(p, req, sizeof(*req));
-	ret = twl4030_madc_enable_irq(madc, req->method);
-	if (ret < 0) {
-		dev_err(madc->dev, "enable irq failed!!\n");
-		return ret;
-	}
-
-	return 0;
-}
-
-/*
- * Function which enables the madc conversion
- * by writing to the control register.
- * @madc - pointer to twl4030_madc_data struct
- * @conv_method - can be TWL4030_MADC_RT, TWL4030_MADC_SW2, TWL4030_MADC_SW1
- * corresponding to RT SW1 or SW2 conversion methods.
- * Returns 0 if succeeds else a negative error value
- */
-static int twl4030_madc_start_conversion(struct twl4030_madc_data *madc,
-					 int conv_method)
-{
-	const struct twl4030_madc_conversion_method *method;
-	int ret = 0;
-
-	if (conv_method != TWL4030_MADC_SW1 && conv_method != TWL4030_MADC_SW2)
-		return -ENOTSUPP;
-
-	method = &twl4030_conversion_methods[conv_method];
-	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, TWL4030_MADC_SW_START,
-			       method->ctrl);
-	if (ret) {
-		dev_err(madc->dev, "unable to write ctrl register 0x%X\n",
-			method->ctrl);
-		return ret;
-	}
-
-	return 0;
-}
-
-/*
- * Function that waits for conversion to be ready
- * @madc - pointer to twl4030_madc_data struct
- * @timeout_ms - timeout value in milliseconds
- * @status_reg - ctrl register
- * returns 0 if succeeds else a negative error value
- */
-static int twl4030_madc_wait_conversion_ready(struct twl4030_madc_data *madc,
-					      unsigned int timeout_ms,
-					      u8 status_reg)
-{
-	unsigned long timeout;
-	int ret;
-
-	timeout = jiffies + msecs_to_jiffies(timeout_ms);
-	do {
-		u8 reg;
-
-		ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &reg, status_reg);
-		if (ret) {
-			dev_err(madc->dev,
-				"unable to read status register 0x%X\n",
-				status_reg);
-			return ret;
-		}
-		if (!(reg & TWL4030_MADC_BUSY) && (reg & TWL4030_MADC_EOC_SW))
-			return 0;
-		usleep_range(500, 2000);
-	} while (!time_after(jiffies, timeout));
-	dev_err(madc->dev, "conversion timeout!\n");
-
-	return -EAGAIN;
-}
-
-/*
- * An exported function which can be called from other kernel drivers.
- * @req twl4030_madc_request structure
- * req->rbuf will be filled with read values of channels based on the
- * channel index. If a particular channel reading fails there will
- * be a negative error value in the corresponding array element.
- * returns 0 if succeeds else error value
- */
-int twl4030_madc_conversion(struct twl4030_madc_request *req)
-{
-	const struct twl4030_madc_conversion_method *method;
-	u8 ch_msb, ch_lsb;
-	int ret;
-
-	if (!req || !twl4030_madc)
-		return -EINVAL;
-
-	mutex_lock(&twl4030_madc->lock);
-	if (req->method < TWL4030_MADC_RT || req->method > TWL4030_MADC_SW2) {
-		ret = -EINVAL;
-		goto out;
-	}
-	/* Do we have a conversion request ongoing */
-	if (twl4030_madc->requests[req->method].active) {
-		ret = -EBUSY;
-		goto out;
-	}
-	ch_msb = (req->channels >> 8) & 0xff;
-	ch_lsb = req->channels & 0xff;
-	method = &twl4030_conversion_methods[req->method];
-	/* Select channels to be converted */
-	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_msb, method->sel + 1);
-	if (ret) {
-		dev_err(twl4030_madc->dev,
-			"unable to write sel register 0x%X\n", method->sel + 1);
-		goto out;
-	}
-	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_lsb, method->sel);
-	if (ret) {
-		dev_err(twl4030_madc->dev,
-			"unable to write sel register 0x%X\n", method->sel + 1);
-		goto out;
-	}
-	/* Select averaging for all channels if do_avg is set */
-	if (req->do_avg) {
-		ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
-				       ch_msb, method->avg + 1);
-		if (ret) {
-			dev_err(twl4030_madc->dev,
-				"unable to write avg register 0x%X\n",
-				method->avg + 1);
-			goto out;
-		}
-		ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
-				       ch_lsb, method->avg);
-		if (ret) {
-			dev_err(twl4030_madc->dev,
-				"unable to write sel reg 0x%X\n",
-				method->avg);
-			goto out;
-		}
-	}
-	if (req->type == TWL4030_MADC_IRQ_ONESHOT && req->func_cb != NULL) {
-		ret = twl4030_madc_set_irq(twl4030_madc, req);
-		if (ret < 0)
-			goto out;
-		ret = twl4030_madc_start_conversion(twl4030_madc, req->method);
-		if (ret < 0)
-			goto out;
-		twl4030_madc->requests[req->method].active = 1;
-		ret = 0;
-		goto out;
-	}
-	/* With RT method we should not be here anymore */
-	if (req->method == TWL4030_MADC_RT) {
-		ret = -EINVAL;
-		goto out;
-	}
-	ret = twl4030_madc_start_conversion(twl4030_madc, req->method);
-	if (ret < 0)
-		goto out;
-	twl4030_madc->requests[req->method].active = 1;
-	/* Wait until conversion is ready (ctrl register returns EOC) */
-	ret = twl4030_madc_wait_conversion_ready(twl4030_madc, 5, method->ctrl);
-	if (ret) {
-		twl4030_madc->requests[req->method].active = 0;
-		goto out;
-	}
-	ret = twl4030_madc_read_channels(twl4030_madc, method->rbase,
-					 req->channels, req->rbuf, req->raw);
-	twl4030_madc->requests[req->method].active = 0;
-
-out:
-	mutex_unlock(&twl4030_madc->lock);
-
-	return ret;
-}
-EXPORT_SYMBOL_GPL(twl4030_madc_conversion);
-
-int twl4030_get_madc_conversion(int channel_no)
-{
-	struct twl4030_madc_request req;
-	int temp = 0;
-	int ret;
-
-	req.channels = (1 << channel_no);
-	req.method = TWL4030_MADC_SW2;
-	req.active = 0;
-	req.func_cb = NULL;
-	ret = twl4030_madc_conversion(&req);
-	if (ret < 0)
-		return ret;
-	if (req.rbuf[channel_no] > 0)
-		temp = req.rbuf[channel_no];
-
-	return temp;
-}
-EXPORT_SYMBOL_GPL(twl4030_get_madc_conversion);
-
-/*
- * Function to enable or disable bias current for
- * main battery type reading or temperature sensing
- * @madc - pointer to twl4030_madc_data struct
- * @chan - can be one of the two values
- * TWL4030_BCI_ITHEN - Enables bias current for main battery type reading
- * TWL4030_BCI_TYPEN - Enables bias current for main battery temperature
- * sensing
- * @on - enable or disable chan.
- */
-static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc,
-					      int chan, int on)
-{
-	int ret;
-	int regmask;
-	u8 regval;
-
-	ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE,
-			      &regval, TWL4030_BCI_BCICTL1);
-	if (ret) {
-		dev_err(madc->dev, "unable to read BCICTL1 reg 0x%X",
-			TWL4030_BCI_BCICTL1);
-		return ret;
-	}
-
-	regmask = (chan == 0) ? TWL4030_BCI_TYPEN : TWL4030_BCI_ITHEN;
-	if (on)
-		regval |= regmask;
-	else
-		regval &= ~regmask;
-
-	ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
-			       regval, TWL4030_BCI_BCICTL1);
-	if (ret) {
-		dev_err(madc->dev, "unable to write BCICTL1 reg 0x%X\n",
-			TWL4030_BCI_BCICTL1);
-		return ret;
-	}
-
-	return 0;
-}
-
-/*
- * Function that sets MADC software power on bit to enable MADC
- * @madc - pointer to twl4030_madc_data struct
- * @on - Enable or disable MADC software powen on bit.
- * returns error if i2c read/write fails else 0
- */
-static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on)
-{
-	u8 regval;
-	int ret;
-
-	ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE,
-			      &regval, TWL4030_MADC_CTRL1);
-	if (ret) {
-		dev_err(madc->dev, "unable to read madc ctrl1 reg 0x%X\n",
-			TWL4030_MADC_CTRL1);
-		return ret;
-	}
-	if (on)
-		regval |= TWL4030_MADC_MADCON;
-	else
-		regval &= ~TWL4030_MADC_MADCON;
-	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, regval, TWL4030_MADC_CTRL1);
-	if (ret) {
-		dev_err(madc->dev, "unable to write madc ctrl1 reg 0x%X\n",
-			TWL4030_MADC_CTRL1);
-		return ret;
-	}
-
-	return 0;
-}
-
-/*
- * Initialize MADC and request for threaded irq
- */
-static int twl4030_madc_probe(struct platform_device *pdev)
-{
-	struct twl4030_madc_data *madc;
-	struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev);
-	struct device_node *np = pdev->dev.of_node;
-	int irq, ret;
-	u8 regval;
-	struct iio_dev *iio_dev = NULL;
-
-	if (!pdata && !np) {
-		dev_err(&pdev->dev, "platform_data not available\n");
-		return -EINVAL;
-	}
-
-	iio_dev = devm_iio_device_alloc(&pdev->dev,
-					sizeof(struct twl4030_madc_data));
-	if (!iio_dev) {
-		dev_err(&pdev->dev, "failed allocating iio device\n");
-		return -ENOMEM;
-	}
-
-	madc = iio_priv(iio_dev);
-	madc->dev = &pdev->dev;
-
-	iio_dev->name = dev_name(&pdev->dev);
-	iio_dev->dev.parent = &pdev->dev;
-	iio_dev->dev.of_node = pdev->dev.of_node;
-	iio_dev->info = &twl4030_madc_iio_info;
-	iio_dev->modes = INDIO_DIRECT_MODE;
-	iio_dev->channels = twl4030_madc_iio_channels;
-	iio_dev->num_channels = 16;
-
-	/*
-	 * Phoenix provides 2 interrupt lines. The first one is connected to
-	 * the OMAP. The other one can be connected to the other processor such
-	 * as modem. Hence two separate ISR and IMR registers.
-	 */
-	if (pdata)
-		madc->use_second_irq = pdata->irq_line != 1;
-	else
-		madc->use_second_irq = of_property_read_bool(np,
-				       "ti,system-uses-second-madc-irq");
-
-	madc->imr = (madc->use_second_irq == 1) ?
-	    TWL4030_MADC_IMR1 : TWL4030_MADC_IMR2;
-	madc->isr = (madc->use_second_irq == 1) ?
-	    TWL4030_MADC_ISR1 : TWL4030_MADC_ISR2;
-
-	ret = twl4030_madc_set_power(madc, 1);
-	if (ret < 0)
-		return ret;
-	ret = twl4030_madc_set_current_generator(madc, 0, 1);
-	if (ret < 0)
-		goto err_current_generator;
-
-	ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE,
-			      &regval, TWL4030_BCI_BCICTL1);
-	if (ret) {
-		dev_err(&pdev->dev, "unable to read reg BCI CTL1 0x%X\n",
-			TWL4030_BCI_BCICTL1);
-		goto err_i2c;
-	}
-	regval |= TWL4030_BCI_MESBAT;
-	ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
-			       regval, TWL4030_BCI_BCICTL1);
-	if (ret) {
-		dev_err(&pdev->dev, "unable to write reg BCI Ctl1 0x%X\n",
-			TWL4030_BCI_BCICTL1);
-		goto err_i2c;
-	}
-
-	/* Check that MADC clock is on */
-	ret = twl_i2c_read_u8(TWL4030_MODULE_INTBR, &regval, TWL4030_REG_GPBR1);
-	if (ret) {
-		dev_err(&pdev->dev, "unable to read reg GPBR1 0x%X\n",
-				TWL4030_REG_GPBR1);
-		goto err_i2c;
-	}
-
-	/* If MADC clk is not on, turn it on */
-	if (!(regval & TWL4030_GPBR1_MADC_HFCLK_EN)) {
-		dev_info(&pdev->dev, "clk disabled, enabling\n");
-		regval |= TWL4030_GPBR1_MADC_HFCLK_EN;
-		ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, regval,
-				       TWL4030_REG_GPBR1);
-		if (ret) {
-			dev_err(&pdev->dev, "unable to write reg GPBR1 0x%X\n",
-					TWL4030_REG_GPBR1);
-			goto err_i2c;
-		}
-	}
-
-	platform_set_drvdata(pdev, iio_dev);
-	mutex_init(&madc->lock);
-
-	irq = platform_get_irq(pdev, 0);
-	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
-				   twl4030_madc_threaded_irq_handler,
-				   IRQF_TRIGGER_RISING, "twl4030_madc", madc);
-	if (ret) {
-		dev_dbg(&pdev->dev, "could not request irq\n");
-		goto err_i2c;
-	}
-	twl4030_madc = madc;
-
-	ret = iio_device_register(iio_dev);
-	if (ret) {
-		dev_dbg(&pdev->dev, "could not register iio device\n");
-		goto err_i2c;
-	}
-
-	return 0;
-
-err_i2c:
-	twl4030_madc_set_current_generator(madc, 0, 0);
-err_current_generator:
-	twl4030_madc_set_power(madc, 0);
-	return ret;
-}
-
-static int twl4030_madc_remove(struct platform_device *pdev)
-{
-	struct iio_dev *iio_dev = platform_get_drvdata(pdev);
-	struct twl4030_madc_data *madc = iio_priv(iio_dev);
-
-	twl4030_madc_set_current_generator(madc, 0, 0);
-	twl4030_madc_set_power(madc, 0);
-
-	iio_device_unregister(iio_dev);
-
-	return 0;
-}
-
-#ifdef CONFIG_OF
-static const struct of_device_id twl_madc_of_match[] = {
-	{.compatible = "ti,twl4030-madc", },
-	{ },
-};
-MODULE_DEVICE_TABLE(of, twl_madc_of_match);
-#endif
-
-static struct platform_driver twl4030_madc_driver = {
-	.probe = twl4030_madc_probe,
-	.remove = twl4030_madc_remove,
-	.driver = {
-		   .name = "twl4030_madc",
-		   .owner = THIS_MODULE,
-		   .of_match_table = of_match_ptr(twl_madc_of_match),
-		   },
-};
-
-module_platform_driver(twl4030_madc_driver);
-
-MODULE_DESCRIPTION("TWL4030 ADC driver");
-MODULE_LICENSE("GPL");
-MODULE_AUTHOR("J Keerthy");
-MODULE_ALIAS("platform:twl4030_madc");
Signature · 2 lines
Peter Meerwald
+43-664-2444418 (mobile)
#24 lore raw

v3 cover Convert twl4030-madc to IIO API and add DT support

In reply to Sebastian Reichel

Hi,

This is RFCv3 for converting twl4030-madc to the IIO API and adding DT support. The patchset compiles and has been tested on my Nokia N900.

Changes since RFCv2:
* Use "--find-renames" for the move from mfd/ to iio/adc/ ;)
* Added Acked-by from Lee Jones (MFD subsystem maintainer)
* Fixed issue found by Joe Perches
- twl4030_madc_request.do_avg should be bool instead of u16
* Fixed issues found by Peter Meerwald
- some style issues (e.g. /** instead of /*)
- typos
- usage of u16 read/write
* Tested with converted rx51-battery driver on Nokia N900

TODO:
* Remove twl4030-madc-hwmon in favour of iio-hwmon This driver is currently only used by board-omap3beagle.c and board-rx51-peripherals.c. The instanciation in the rx51 boardcode is a bug, since actually only twl4030-madc is needed. Thus board-omap3beagle.c needs to be updated to iio-hwmon and board-rx51-peripherals.c to twl4030-madc. Both tasks are pretty trivial and twl4030-madc-hwmon can be removed afterwards :)
* Update twl4030-madc-battery to the IIO API. I guess Marek will do this update in the near future, since he is currently writing DT bindings for the driver.
* I updated twl4030-madc-battery already and will send it in a separate patchset.
* Once all of the above steps happened the old API can be removed from the driver, which allows some more cleanups.

-- Sebastian

Sebastian Reichel (7):
mfd: twl4030-madc: Use managed resources
mfd: twl4030-madc: Add DT support and convert to IIO framework
mfd: twl4030-madc: Cleanup driver
mfd: twl-core: Add twl_i2c_read/write_u16
mfd: twl4030-madc: Use twl_i2c_read/write_u16 for 16 bit registers
Documentation: DT: Document twl4030-madc binding
mfd: twl4030-madc: Move driver to drivers/iio/adc

 .../devicetree/bindings/iio/adc/twl4030-madc.txt   |  24 ++
 drivers/iio/adc/Kconfig                            |  10 +
 drivers/iio/adc/Makefile                           |   1 +
 drivers/{mfd => iio/adc}/twl4030-madc.c            | 278 +++++++++++++--------
 drivers/mfd/Kconfig                                |  10 -
 drivers/mfd/Makefile                               |   1 -
 include/linux/i2c/twl.h                            |  12 +
 include/linux/i2c/twl4030-madc.h                   |   2 +-
 8 files changed, 224 insertions(+), 114 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/iio/adc/twl4030-madc.txt
 rename drivers/{mfd => iio/adc}/twl4030-madc.c (77%)
Signature · 1 line
1.8.5.3
#25 lore raw

RFC v3 1/7 mfd: twl4030-madc: Use managed resources

Update twl4030-madc driver to use managed resources.

---
 drivers/mfd/twl4030-madc.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
drivers/mfd/twl4030-madc.c +6 -9
index 4c583e4..5458561 100644
@@ -702,14 +702,14 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 {
 	struct twl4030_madc_data *madc;
 	struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev);
-	int ret;
+	int irq, ret;
 	u8 regval;
 
 	if (!pdata) {
 		dev_err(&pdev->dev, "platform_data not available\n");
 		return -EINVAL;
 	}
-	madc = kzalloc(sizeof(*madc), GFP_KERNEL);
+	madc = devm_kzalloc(&pdev->dev, sizeof(*madc), GFP_KERNEL);
 	if (!madc)
 		return -ENOMEM;
 
@@ -726,7 +726,7 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 	    TWL4030_MADC_ISR1 : TWL4030_MADC_ISR2;
 	ret = twl4030_madc_set_power(madc, 1);
 	if (ret < 0)
-		goto err_power;
+		return ret;
 	ret = twl4030_madc_set_current_generator(madc, 0, 1);
 	if (ret < 0)
 		goto err_current_generator;
@@ -770,7 +770,9 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 
 	platform_set_drvdata(pdev, madc);
 	mutex_init(&madc->lock);
-	ret = request_threaded_irq(platform_get_irq(pdev, 0), NULL,
+
+	irq = platform_get_irq(pdev, 0);
+	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
 				   twl4030_madc_threaded_irq_handler,
 				   IRQF_TRIGGER_RISING, "twl4030_madc", madc);
 	if (ret) {
@@ -783,9 +785,6 @@ err_i2c:
 	twl4030_madc_set_current_generator(madc, 0, 0);
 err_current_generator:
 	twl4030_madc_set_power(madc, 0);
-err_power:
-	kfree(madc);
-
 	return ret;
 }
 
@@ -793,10 +792,8 @@ static int twl4030_madc_remove(struct platform_device *pdev)
 {
 	struct twl4030_madc_data *madc = platform_get_drvdata(pdev);
 
-	free_irq(platform_get_irq(pdev, 0), madc);
 	twl4030_madc_set_current_generator(madc, 0, 0);
 	twl4030_madc_set_power(madc, 0);
-	kfree(madc);
 
 	return 0;
 }
Signature · 1 line
1.8.5.3
#26 lore raw

RFC v3 2/7 mfd: twl4030-madc: Add DT support and convert to IIO framework

In reply to Sebastian Reichel

This converts twl4030-madc module to use the Industrial IO ADC framework and adds device tree support.

---
 drivers/mfd/twl4030-madc.c | 122 ++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 115 insertions(+), 7 deletions(-)
drivers/mfd/twl4030-madc.c +115 -7
index 5458561..81484ee 100644
@@ -47,6 +47,8 @@
 #include <linux/gfp.h>
 #include <linux/err.h>
 
+#include <linux/iio/iio.h>
+
 /*
  * struct twl4030_madc_data - a container for madc info
  * @dev - pointer to device structure for madc
@@ -59,10 +61,74 @@ struct twl4030_madc_data {
 	struct device *dev;
 	struct mutex lock;	/* mutex protecting this data structure */
 	struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS];
+	bool use_second_irq;
 	int imr;
 	int isr;
 };
 
+static int twl4030_madc_read(struct iio_dev *iio_dev,
+			     const struct iio_chan_spec *chan,
+			     int *val, int *val2, long mask)
+{
+	struct twl4030_madc_data *madc = iio_priv(iio_dev);
+	struct twl4030_madc_request req;
+	int channel = chan->channel;
+	int ret;
+
+	req.method = madc->use_second_irq ? TWL4030_MADC_SW2 : TWL4030_MADC_SW1;
+
+	req.channels = BIT(channel);
+	req.active = false;
+	req.func_cb = NULL;
+	req.type = TWL4030_MADC_WAIT;
+	req.raw = !(mask == IIO_CHAN_INFO_PROCESSED);
+	req.do_avg = (mask == IIO_CHAN_INFO_AVERAGE_RAW);
+
+	ret = twl4030_madc_conversion(&req);
+	if (ret < 0)
+		return ret;
+
+	*val = req.rbuf[channel];
+
+	return IIO_VAL_INT;
+}
+
+static const struct iio_info twl4030_madc_iio_info = {
+	.read_raw = &twl4030_madc_read,
+	.driver_module = THIS_MODULE,
+};
+
+#define TWL4030_ADC_CHANNEL(_channel, _type, _name, _mask) {	\
+	.type = _type,					\
+	.channel = _channel,				\
+	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |  \
+			      BIT(IIO_CHAN_INFO_AVERAGE_RAW) | \
+			      _mask,			\
+	.datasheet_name = _name,			\
+	.indexed = 1,					\
+}
+
+static const struct iio_chan_spec twl4030_madc_iio_channels[] = {
+	TWL4030_ADC_CHANNEL(0, IIO_VOLTAGE, "ADCIN0", 0),
+	TWL4030_ADC_CHANNEL(1, IIO_TEMP, "ADCIN1",
+		BIT(IIO_CHAN_INFO_PROCESSED)),
+	TWL4030_ADC_CHANNEL(2, IIO_VOLTAGE, "ADCIN2", 0),
+	TWL4030_ADC_CHANNEL(3, IIO_VOLTAGE, "ADCIN3", 0),
+	TWL4030_ADC_CHANNEL(4, IIO_VOLTAGE, "ADCIN4", 0),
+	TWL4030_ADC_CHANNEL(5, IIO_VOLTAGE, "ADCIN5", 0),
+	TWL4030_ADC_CHANNEL(6, IIO_VOLTAGE, "ADCIN6", 0),
+	TWL4030_ADC_CHANNEL(7, IIO_VOLTAGE, "ADCIN7", 0),
+	TWL4030_ADC_CHANNEL(8, IIO_VOLTAGE, "ADCIN8", 0),
+	TWL4030_ADC_CHANNEL(9, IIO_VOLTAGE, "ADCIN9", 0),
+	TWL4030_ADC_CHANNEL(10, IIO_CURRENT, "ADCIN10",
+		BIT(IIO_CHAN_INFO_PROCESSED)),
+	TWL4030_ADC_CHANNEL(11, IIO_VOLTAGE, "ADCIN11", 0),
+	TWL4030_ADC_CHANNEL(12, IIO_VOLTAGE, "ADCIN12", 0),
+	TWL4030_ADC_CHANNEL(13, IIO_VOLTAGE, "ADCIN13", 0),
+	TWL4030_ADC_CHANNEL(14, IIO_VOLTAGE, "ADCIN14", 0),
+	TWL4030_ADC_CHANNEL(15, IIO_VOLTAGE, "ADCIN15", 0),
+};
+
 static struct twl4030_madc_data *twl4030_madc;
 
 struct twl4030_prescale_divider_ratios {
@@ -702,28 +768,50 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 {
 	struct twl4030_madc_data *madc;
 	struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev);
+	struct device_node *np = pdev->dev.of_node;
 	int irq, ret;
 	u8 regval;
+	struct iio_dev *iio_dev = NULL;
 
-	if (!pdata) {
+	if (!pdata && !np) {
 		dev_err(&pdev->dev, "platform_data not available\n");
 		return -EINVAL;
 	}
-	madc = devm_kzalloc(&pdev->dev, sizeof(*madc), GFP_KERNEL);
-	if (!madc)
+
+	iio_dev = devm_iio_device_alloc(&pdev->dev,
+					sizeof(struct twl4030_madc_data));
+	if (!iio_dev) {
+		dev_err(&pdev->dev, "failed allocating iio device\n");
 		return -ENOMEM;
+	}
 
+	madc = iio_priv(iio_dev);
 	madc->dev = &pdev->dev;
 
+	iio_dev->name = dev_name(&pdev->dev);
+	iio_dev->dev.parent = &pdev->dev;
+	iio_dev->dev.of_node = pdev->dev.of_node;
+	iio_dev->info = &twl4030_madc_iio_info;
+	iio_dev->modes = INDIO_DIRECT_MODE;
+	iio_dev->channels = twl4030_madc_iio_channels;
+	iio_dev->num_channels = 16;
+
 	/*
 	 * Phoenix provides 2 interrupt lines. The first one is connected to
 	 * the OMAP. The other one can be connected to the other processor such
 	 * as modem. Hence two separate ISR and IMR registers.
 	 */
-	madc->imr = (pdata->irq_line == 1) ?
+	if (pdata)
+		madc->use_second_irq = pdata->irq_line != 1;
+	else
+		madc->use_second_irq = of_property_read_bool(np,
+				       "ti,system-uses-second-madc-irq");
+
+	madc->imr = (madc->use_second_irq == 1) ?
 	    TWL4030_MADC_IMR1 : TWL4030_MADC_IMR2;
-	madc->isr = (pdata->irq_line == 1) ?
+	madc->isr = (madc->use_second_irq == 1) ?
 	    TWL4030_MADC_ISR1 : TWL4030_MADC_ISR2;
+
 	ret = twl4030_madc_set_power(madc, 1);
 	if (ret < 0)
 		return ret;
@@ -768,7 +856,7 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 		}
 	}
 
-	platform_set_drvdata(pdev, madc);
+	platform_set_drvdata(pdev, iio_dev);
 	mutex_init(&madc->lock);
 
 	irq = platform_get_irq(pdev, 0);
@@ -780,7 +868,15 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 		goto err_i2c;
 	}
 	twl4030_madc = madc;
+
+	ret = iio_device_register(iio_dev);
+	if (ret) {
+		dev_dbg(&pdev->dev, "could not register iio device\n");
+		goto err_i2c;
+	}
+
 	return 0;
+
 err_i2c:
 	twl4030_madc_set_current_generator(madc, 0, 0);
 err_current_generator:
@@ -790,20 +886,32 @@ err_current_generator:
 
 static int twl4030_madc_remove(struct platform_device *pdev)
 {
-	struct twl4030_madc_data *madc = platform_get_drvdata(pdev);
+	struct iio_dev *iio_dev = platform_get_drvdata(pdev);
+	struct twl4030_madc_data *madc = iio_priv(iio_dev);
 
 	twl4030_madc_set_current_generator(madc, 0, 0);
 	twl4030_madc_set_power(madc, 0);
 
+	iio_device_unregister(iio_dev);
+
 	return 0;
 }
 
+#ifdef CONFIG_OF
+static const struct of_device_id twl_madc_of_match[] = {
+	{.compatible = "ti,twl4030-madc", },
+	{ },
+};
+MODULE_DEVICE_TABLE(of, twl_madc_of_match);
+#endif
+
 static struct platform_driver twl4030_madc_driver = {
 	.probe = twl4030_madc_probe,
 	.remove = twl4030_madc_remove,
 	.driver = {
 		   .name = "twl4030_madc",
 		   .owner = THIS_MODULE,
+		   .of_match_table = of_match_ptr(twl_madc_of_match),
 		   },
 };
 
Signature · 1 line
1.8.5.3
#27 lore raw

Re: [RFCv3 2/7] mfd: twl4030-madc: Add DT support and convert to IIO framework

Quoted 22 lines

This converts twl4030-madc module to use the Industrial IO ADC framework and adds device tree support.

---
 drivers/mfd/twl4030-madc.c | 122 ++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 115 insertions(+), 7 deletions(-)
drivers/mfd/twl4030-madc.c +2 -0
index 5458561..81484ee 100644
@@ -47,6 +47,8 @@
 #include <linux/gfp.h>
 #include <linux/err.h>
 
+#include <linux/iio/iio.h>
+
 /*
  * struct twl4030_madc_data - a container for madc info
  * @dev - pointer to device structure for madc

drivers/mfd/twl4030-madc.c struct twl4030_madc_data { · line 64

Quoted 4 lines
drivers/mfd/twl4030-madc.c +1 -0
@@ -59,10 +61,74 @@ struct twl4030_madc_data {
 	struct device *dev;
 	struct mutex lock;	/* mutex protecting this data structure */
 	struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS];
+	bool use_second_irq;

This should go into the same patch that adds the header doc, or visa versa.

drivers/mfd/twl4030-madc.c struct twl4030_madc_data { · line 75

Quoted 11 lines
drivers/mfd/twl4030-madc.c +7 -0
 	int imr;
 	int isr;
 };
 
+static int twl4030_madc_read(struct iio_dev *iio_dev,
+			     const struct iio_chan_spec *chan,
+			     int *val, int *val2, long mask)
+{
+	struct twl4030_madc_data *madc = iio_priv(iio_dev);
+	struct twl4030_madc_request req;
+	int channel = chan->channel;

Does this really add anything you save 6 chars.

<snip>

drivers/mfd/twl4030-madc.c static int twl4030_madc_probe(struct platform_device *pdev) · line 805

Quoted 8 lines
drivers/mfd/twl4030-madc.c +2 -1
 	/*
 	 * Phoenix provides 2 interrupt lines. The first one is connected to
 	 * the OMAP. The other one can be connected to the other processor such
 	 * as modem. Hence two separate ISR and IMR registers.
 	 */
-	madc->imr = (pdata->irq_line == 1) ?
+	if (pdata)
+		madc->use_second_irq = pdata->irq_line != 1;

Unconventional an unclear.

Stick the comparison in the if statement.

drivers/mfd/twl4030-madc.c static int twl4030_madc_probe(struct platform_device *pdev) · line 810

Quoted 5 lines
drivers/mfd/twl4030-madc.c +5 -0
+	else
+		madc->use_second_irq = of_property_read_bool(np,
+				       "ti,system-uses-second-madc-irq");
+
+	madc->imr = (madc->use_second_irq == 1) ?

madc->imr = madc->use_second_irq ? <blah> : <blah>;

drivers/mfd/twl4030-madc.c static int twl4030_madc_probe(struct platform_device *pdev) · line 812

Quoted 3 lines
drivers/mfd/twl4030-madc.c +1 -1
 	    TWL4030_MADC_IMR1 : TWL4030_MADC_IMR2;
-	madc->isr = (pdata->irq_line == 1) ?
+	madc->isr = (madc->use_second_irq == 1) ?

Same here.

<snip>

Signature · 4 lines
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
#28 lore raw

RFC v3 3/7 mfd: twl4030-madc: Cleanup driver

In reply to Sebastian Reichel

Some style fixes in twl4030-madc driver.

---
 drivers/mfd/twl4030-madc.c       | 108 ++++++++++++++++++---------------------
 include/linux/i2c/twl4030-madc.h |   2 +-
 2 files changed, 51 insertions(+), 59 deletions(-)
drivers/mfd/twl4030-madc.c +50 -58
index 81484ee..c23025b 100644
@@ -49,11 +49,12 @@
 
 #include <linux/iio/iio.h>
 
-/*
+/**
  * struct twl4030_madc_data - a container for madc info
  * @dev - pointer to device structure for madc
  * @lock - mutex protecting this data structure
  * @requests - Array of request struct corresponding to SW1, SW2 and RT
+ * @use_second_irq - IRQ selection (main or co-processor)
  * @imr - Interrupt mask register of MADC
  * @isr - Interrupt status register of MADC
  */
@@ -62,8 +63,8 @@ struct twl4030_madc_data {
 	struct mutex lock;	/* mutex protecting this data structure */
 	struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS];
 	bool use_second_irq;
-	int imr;
-	int isr;
+	u8 imr;
+	u8 isr;
 };
 
 static int twl4030_madc_read(struct iio_dev *iio_dev,
@@ -157,17 +158,16 @@ twl4030_divider_ratios[16] = {
 };
 
 
-/*
- * Conversion table from -3 to 55 degree Celcius
- */
-static int therm_tbl[] = {
-30800,	29500,	28300,	27100,
-26000,	24900,	23900,	22900,	22000,	21100,	20300,	19400,	18700,	17900,
-17200,	16500,	15900,	15300,	14700,	14100,	13600,	13100,	12600,	12100,
-11600,	11200,	10800,	10400,	10000,	9630,	9280,	8950,	8620,	8310,
-8020,	7730,	7460,	7200,	6950,	6710,	6470,	6250,	6040,	5830,
-5640,	5450,	5260,	5090,	4920,	4760,	4600,	4450,	4310,	4170,
-4040,	3910,	3790,	3670,	3550
+/* Conversion table from -3 to 55 degree Celcius */
+static int twl4030_therm_tbl[] = {
+	30800,	29500,	28300,	27100,
+	26000,	24900,	23900,	22900,	22000,	21100,	20300,	19400,	18700,
+	17900,	17200,	16500,	15900,	15300,	14700,	14100,	13600,	13100,
+	12600,	12100,	11600,	11200,	10800,	10400,	10000,	9630,	9280,
+	8950,	8620,	8310,	8020,	7730,	7460,	7200,	6950,	6710,
+	6470,	6250,	6040,	5830,	5640,	5450,	5260,	5090,	4920,
+	4760,	4600,	4450,	4310,	4170,	4040,	3910,	3790,	3670,
+	3550
 };
 
 /*
@@ -199,7 +199,7 @@ const struct twl4030_madc_conversion_method twl4030_conversion_methods[] = {
 			      },
 };
 
-/*
+/**
  * Function to read a particular channel value.
  * @madc - pointer to struct twl4030_madc_data
  * @reg - lsb of ADC Channel
@@ -229,7 +229,7 @@ static int twl4030_madc_channel_raw_read(struct twl4030_madc_data *madc, u8 reg)
 }
 
 /*
- * Return battery temperature
+ * Return battery temperature in degree Celsius
  * Or < 0 on failure.
  */
 static int twl4030battery_temperature(int raw_volt)
@@ -238,18 +238,18 @@ static int twl4030battery_temperature(int raw_volt)
 	int temp, curr, volt, res, ret;
 
 	volt = (raw_volt * TEMP_STEP_SIZE) / TEMP_PSR_R;
-	/* Getting and calculating the supply current in micro ampers */
+	/* Getting and calculating the supply current in micro amperes */
 	ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val,
 		REG_BCICTL2);
 	if (ret < 0)
 		return ret;
+
 	curr = ((val & TWL4030_BCI_ITHEN) + 1) * 10;
 	/* Getting and calculating the thermistor resistance in ohms */
 	res = volt * 1000 / curr;
 	/* calculating temperature */
 	for (temp = 58; temp >= 0; temp--) {
-		int actual = therm_tbl[temp];
-
+		int actual = twl4030_therm_tbl[temp];
 		if ((actual - res) >= 0)
 			break;
 	}
@@ -271,11 +271,12 @@ static int twl4030battery_current(int raw_volt)
 	else /* slope of 0.88 mV/mA */
 		return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R2;
 }
+
 /*
  * Function to read channel values
  * @madc - pointer to twl4030_madc_data struct
  * @reg_base - Base address of the first channel
- * @Channels - 16 bit bitmap. If the bit is set, channel value is read
+ * @Channels - 16 bit bitmap. If the bit is set, channel's value is read
  * @buf - The channel values are stored here. if read fails error
  * @raw - Return raw values without conversion
  * value is stored
@@ -286,17 +287,14 @@ static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
 				      long channels, int *buf,
 				      bool raw)
 {
-	int count = 0, count_req = 0, i;
-	u8 reg;
+	int count = 0, i;
 
 	for_each_set_bit(i, &channels, TWL4030_MADC_MAX_CHANNELS) {
-		reg = reg_base + 2 * i;
-		buf[i] = twl4030_madc_channel_raw_read(madc, reg);
+		buf[i] = twl4030_madc_channel_raw_read(madc, reg_base + 2 * i);
 		if (buf[i] < 0) {
-			dev_err(madc->dev,
-				"Unable to read register 0x%X\n", reg);
-			count_req++;
-			continue;
+			dev_err(madc->dev, "Unable to read register 0x%X\n",
+				reg_base + 2 * i);
+			return buf[i];
 		}
 		if (raw) {
 			count++;
@@ -307,7 +305,7 @@ static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
 			buf[i] = twl4030battery_current(buf[i]);
 			if (buf[i] < 0) {
 				dev_err(madc->dev, "err reading current\n");
-				count_req++;
+				return buf[i];
 			} else {
 				count++;
 				buf[i] = buf[i] - 750;
@@ -317,7 +315,7 @@ static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
 			buf[i] = twl4030battery_temperature(buf[i]);
 			if (buf[i] < 0) {
 				dev_err(madc->dev, "err reading temperature\n");
-				count_req++;
+				return buf[i];
 			} else {
 				buf[i] -= 3;
 				count++;
@@ -338,8 +336,6 @@ static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
 				twl4030_divider_ratios[i].numerator);
 		}
 	}
-	if (count_req)
-		dev_err(madc->dev, "%d channel conversion failed\n", count_req);
 
 	return count;
 }
@@ -363,13 +359,13 @@ static int twl4030_madc_enable_irq(struct twl4030_madc_data *madc, u8 id)
 			madc->imr);
 		return ret;
 	}
+
 	val &= ~(1 << id);
 	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr);
 	if (ret) {
 		dev_err(madc->dev,
 			"unable to write imr register 0x%X\n", madc->imr);
 		return ret;
-
 	}
 
 	return 0;
@@ -432,7 +428,7 @@ static irqreturn_t twl4030_madc_threaded_irq_handler(int irq, void *_madc)
 			continue;
 		ret = twl4030_madc_disable_irq(madc, i);
 		if (ret < 0)
-			dev_dbg(madc->dev, "Disable interrupt failed%d\n", i);
+			dev_dbg(madc->dev, "Disable interrupt failed %d\n", i);
 		madc->requests[i].result_pending = 1;
 	}
 	for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
@@ -514,21 +510,17 @@ static int twl4030_madc_start_conversion(struct twl4030_madc_data *madc,
 {
 	const struct twl4030_madc_conversion_method *method;
 	int ret = 0;
+
+	if (conv_method != TWL4030_MADC_SW1 && conv_method != TWL4030_MADC_SW2)
+		return -ENOTSUPP;
+
 	method = &twl4030_conversion_methods[conv_method];
-	switch (conv_method) {
-	case TWL4030_MADC_SW1:
-	case TWL4030_MADC_SW2:
-		ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
-				       TWL4030_MADC_SW_START, method->ctrl);
-		if (ret) {
-			dev_err(madc->dev,
-				"unable to write ctrl register 0x%X\n",
-				method->ctrl);
-			return ret;
-		}
-		break;
-	default:
-		break;
+	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, TWL4030_MADC_SW_START,
+			       method->ctrl);
+	if (ret) {
+		dev_err(madc->dev, "unable to write ctrl register 0x%X\n",
+			method->ctrl);
+		return ret;
 	}
 
 	return 0;
@@ -625,8 +617,8 @@ int twl4030_madc_conversion(struct twl4030_madc_request *req)
 				       ch_lsb, method->avg);
 		if (ret) {
 			dev_err(twl4030_madc->dev,
-				"unable to write sel reg 0x%X\n",
-				method->sel + 1);
+				"unable to write avg reg 0x%X\n",
+				method->avg);
 			goto out;
 		}
 	}
@@ -667,10 +659,6 @@ out:
 }
 EXPORT_SYMBOL_GPL(twl4030_madc_conversion);
 
-/*
- * Return channel value
- * Or < 0 on failure.
- */
 int twl4030_get_madc_conversion(int channel_no)
 {
 	struct twl4030_madc_request req;
@@ -705,6 +693,7 @@ static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc,
 					      int chan, int on)
 {
 	int ret;
+	int regmask;
 	u8 regval;
 
 	ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE,
@@ -714,10 +703,13 @@ static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc,
 			TWL4030_BCI_BCICTL1);
 		return ret;
 	}
+
+	regmask = (chan == 0) ? TWL4030_BCI_TYPEN : TWL4030_BCI_ITHEN;
 	if (on)
-		regval |= chan ? TWL4030_BCI_ITHEN : TWL4030_BCI_TYPEN;
+		regval |= regmask;
 	else
-		regval &= chan ? ~TWL4030_BCI_ITHEN : ~TWL4030_BCI_TYPEN;
+		regval &= ~regmask;
+
 	ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
 			       regval, TWL4030_BCI_BCICTL1);
 	if (ret) {
@@ -732,7 +724,7 @@ static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc,
 /*
  * Function that sets MADC software power on bit to enable MADC
  * @madc - pointer to twl4030_madc_data struct
- * @on - Enable or disable MADC software powen on bit.
+ * @on - Enable or disable MADC software power on bit.
  * returns error if i2c read/write fails else 0
  */
 static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on)
@@ -794,7 +786,7 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 	iio_dev->info = &twl4030_madc_iio_info;
 	iio_dev->modes = INDIO_DIRECT_MODE;
 	iio_dev->channels = twl4030_madc_iio_channels;
-	iio_dev->num_channels = 16;
+	iio_dev->num_channels = ARRAY_SIZE(twl4030_madc_iio_channels);
 
 	/*
 	 * Phoenix provides 2 interrupt lines. The first one is connected to
include/linux/i2c/twl4030-madc.h +1 -1
index 01f5951..1c0134d 100644
@@ -44,7 +44,7 @@ struct twl4030_madc_conversion_method {
 
 struct twl4030_madc_request {
 	unsigned long channels;
-	u16 do_avg;
+	bool do_avg;
 	u16 method;
 	u16 type;
 	bool active;
Signature · 1 line
1.8.5.3
#29 lore raw

Re: [RFCv3 3/7] mfd: twl4030-madc: Cleanup driver

Quoted 13 lines

Some style fixes in twl4030-madc driver.

---
 drivers/mfd/twl4030-madc.c       | 108 ++++++++++++++++++---------------------
 include/linux/i2c/twl4030-madc.h |   2 +-
 2 files changed, 51 insertions(+), 59 deletions(-)
drivers/mfd/twl4030-madc.c +0 -0
index 81484ee..c23025b 100644

drivers/mfd/twl4030-madc.c static int twl4030_madc_channel_raw_read(struct twl4030_madc_data *madc, u8 reg) · line 232

Quoted 4 lines
drivers/mfd/twl4030-madc.c +1 -1
 
 /*
- * Return battery temperature
+ * Return battery temperature in degree Celsius

degrees

<snip>

drivers/mfd/twl4030-madc.c static int twl4030_madc_read_channels(struct twl4030_madc_data *madc, · line 290

Quoted 3 lines
drivers/mfd/twl4030-madc.c +1 -2
-	int count = 0, count_req = 0, i;
-	u8 reg;
+	int count = 0, i;

Ugly, please declare on separate lines.

drivers/mfd/twl4030-madc.c static int twl4030_madc_read_channels(struct twl4030_madc_data *madc, · line 293

Quoted 2 lines
drivers/mfd/twl4030-madc.c +0 -1
 	for_each_set_bit(i, &channels, TWL4030_MADC_MAX_CHANNELS) {
-		reg = reg_base + 2 * i;

Odd for this to be removed, as you calculate more than once now.

drivers/mfd/twl4030-madc.c static int twl4030_madc_read_channels(struct twl4030_madc_data *madc, · line 293

Quoted 2 lines
drivers/mfd/twl4030-madc.c +1 -1
-		buf[i] = twl4030_madc_channel_raw_read(madc, reg);
+		buf[i] = twl4030_madc_channel_raw_read(madc, reg_base + 2 * i);

Better to bracket the math for clarity.

<snip>

drivers/mfd/twl4030-madc.c static int twl4030_madc_read_channels(struct twl4030_madc_data *madc, · line 296

Quoted 2 lines
drivers/mfd/twl4030-madc.c +2 -0
+			dev_err(madc->dev, "Unable to read register 0x%X\n",
+				reg_base + 2 * i);

Here's the other calculation.

drivers/mfd/twl4030-madc.c static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc, · line 707

Quoted 1 line
drivers/mfd/twl4030-madc.c +1 -0
+	regmask = (chan == 0) ? TWL4030_BCI_TYPEN : TWL4030_BCI_ITHEN;

regmask = chan ? TWL4030_BCI_ITHEN : TWL4030_BCI_TYPEN;

Signature · 4 lines
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
#30 lore raw

RFC v3 4/7 mfd: twl-core: Add twl_i2c_read/write_u16

In reply to Sebastian Reichel

Add a simple twl_i2c_read/write_u16 wrapper over
the twl_i2c_read/write, which is similar to the
twl_i2c_read/write_u8 wrapper.

---
 include/linux/i2c/twl.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)
include/linux/i2c/twl.h +12 -0
index ade1c06..d2b1670 100644
@@ -195,6 +195,18 @@ static inline int twl_i2c_read_u8(u8 mod_no, u8 *val, u8 reg) {
 	return twl_i2c_read(mod_no, val, reg, 1);
 }
 
+static inline int twl_i2c_write_u16(u8 mod_no, u16 val, u8 reg) {
+	val = cpu_to_le16(val);
+	return twl_i2c_write(mod_no, (u8*) &val, reg, 2);
+}
+
+static inline int twl_i2c_read_u16(u8 mod_no, u16 *val, u8 reg) {
+	int ret;
+	ret = twl_i2c_read(mod_no, (u8*) val, reg, 2);
+	*val = le16_to_cpu(*val);
+	return ret;
+}
+
 int twl_get_type(void);
 int twl_get_version(void);
 int twl_get_hfclk_rate(void);
Signature · 6 lines
1.8.5.3

--
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
Acked-by on v3 4/7
#31 lore raw

Re: [RFCv3 4/7] mfd: twl-core: Add twl_i2c_read/write_u16

Quoted 8 lines

Add a simple twl_i2c_read/write_u16 wrapper over
the twl_i2c_read/write, which is similar to the
twl_i2c_read/write_u8 wrapper.

---
 include/linux/i2c/twl.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)
Signature · 8 lines
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
--
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
#32 lore raw

RFC v3 5/7 mfd: twl4030-madc: Use twl_i2c_read/write_u16 for 16 bit registers

In reply to Sebastian Reichel

Simplify reading and writing of 16 bit TWL registers in the driver by using twl_i2c_read_u16 and twl_i2c_write_u16.

---
 drivers/mfd/twl4030-madc.c | 39 ++++++++-------------------------------
 1 file changed, 8 insertions(+), 31 deletions(-)
drivers/mfd/twl4030-madc.c +8 -31
index c23025b..d19aa3e 100644
@@ -207,25 +207,19 @@ const struct twl4030_madc_conversion_method twl4030_conversion_methods[] = {
  */
 static int twl4030_madc_channel_raw_read(struct twl4030_madc_data *madc, u8 reg)
 {
-	u8 msb, lsb;
+	u16 val;
 	int ret;
 	/*
 	 * For each ADC channel, we have MSB and LSB register pair. MSB address
 	 * is always LSB address+1. reg parameter is the address of LSB register
 	 */
-	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &msb, reg + 1);
+	ret = twl_i2c_read_u16(TWL4030_MODULE_MADC, &val, reg);
 	if (ret) {
-		dev_err(madc->dev, "unable to read MSB register 0x%X\n",
-			reg + 1);
-		return ret;
-	}
-	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &lsb, reg);
-	if (ret) {
-		dev_err(madc->dev, "unable to read LSB register 0x%X\n", reg);
+		dev_err(madc->dev, "unable to read register 0x%X\n", reg);
 		return ret;
 	}
 
-	return (int)(((msb << 8) | lsb) >> 6);
+	return (int)(val >> 6);
 }
 
 /*
@@ -571,7 +565,6 @@ static int twl4030_madc_wait_conversion_ready(struct twl4030_madc_data *madc,
 int twl4030_madc_conversion(struct twl4030_madc_request *req)
 {
 	const struct twl4030_madc_conversion_method *method;
-	u8 ch_msb, ch_lsb;
 	int ret;
 
 	if (!req || !twl4030_madc)
@@ -587,37 +580,21 @@ int twl4030_madc_conversion(struct twl4030_madc_request *req)
 		ret = -EBUSY;
 		goto out;
 	}
-	ch_msb = (req->channels >> 8) & 0xff;
-	ch_lsb = req->channels & 0xff;
 	method = &twl4030_conversion_methods[req->method];
 	/* Select channels to be converted */
-	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_msb, method->sel + 1);
+	ret = twl_i2c_write_u16(TWL4030_MODULE_MADC, req->channels, method->sel);
 	if (ret) {
 		dev_err(twl4030_madc->dev,
-			"unable to write sel register 0x%X\n", method->sel + 1);
-		goto out;
-	}
-	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_lsb, method->sel);
-	if (ret) {
-		dev_err(twl4030_madc->dev,
-			"unable to write sel register 0x%X\n", method->sel + 1);
+			"unable to write sel register 0x%X\n", method->sel);
 		goto out;
 	}
 	/* Select averaging for all channels if do_avg is set */
 	if (req->do_avg) {
-		ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
-				       ch_msb, method->avg + 1);
+		ret = twl_i2c_write_u16(TWL4030_MODULE_MADC, req->channels,
+				       method->avg);
 		if (ret) {
 			dev_err(twl4030_madc->dev,
 				"unable to write avg register 0x%X\n",
-				method->avg + 1);
-			goto out;
-		}
-		ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
-				       ch_lsb, method->avg);
-		if (ret) {
-			dev_err(twl4030_madc->dev,
-				"unable to write avg reg 0x%X\n",
 				method->avg);
 			goto out;
 		}
Signature · 1 line
1.8.5.3
Acked-by on v3 5/7
#33 lore raw

Re: [RFCv3 5/7] mfd: twl4030-madc: Use twl_i2c_read/write_u16 for 16 bit registers

Quoted 7 lines

Simplify reading and writing of 16 bit TWL registers in the driver by using twl_i2c_read_u16 and twl_i2c_write_u16.

---
 drivers/mfd/twl4030-madc.c | 39 ++++++++-------------------------------
 1 file changed, 8 insertions(+), 31 deletions(-)
Signature · 4 lines
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
#34 lore raw

RFC v3 6/7 Documentation: DT: Document twl4030-madc binding

In reply to Sebastian Reichel

Add devicetree binding documentation for twl4030-madc analog digital converter.

---
 .../devicetree/bindings/iio/adc/twl4030-madc.txt   | 24 ++++++++++++++++++++++
 1 file changed, 24 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/adc/twl4030-madc.txt
Documentation/devicetree/bindings/iio/adc/twl4030-madc.txt new file +24 -0
new file mode 100644
index 0000000..6bdd214
@@ -0,0 +1,24 @@
+* TWL4030 Monitoring Analog to Digital Converter (MADC)
+
+The MADC subsystem in the TWL4030 consists of a 10-bit ADC
+combined with a 16-input analog multiplexer.
+
+Required properties:
+  - compatible: Should contain "ti,twl4030-madc".
+  - interrupts: IRQ line for the MADC submodule.
+  - #io-channel-cells: Should be set to <1>.
+
+Optional properties:
+  - ti,system-uses-second-madc-irq: boolean, set if the second madc irq register
+				    should be used, which is intended to be used
+				    by Co-Processors (e.g. a modem).
+
+Example:
+
+&twl {
+	madc {
+		compatible = "ti,twl4030-madc";
+		interrupts = <3>;
+		#io-channel-cells = <1>;
+	};
+};
Signature · 1 line
1.8.5.3
#35 lore raw

RFC v3 7/7 mfd: twl4030-madc: Move driver to drivers/iio/adc

In reply to Sebastian Reichel

This is a driver for an A/D converter, which belongs into drivers/iio/adc.

---
 drivers/iio/adc/Kconfig                 | 10 ++++++++++
 drivers/iio/adc/Makefile                |  1 +
 drivers/{mfd => iio/adc}/twl4030-madc.c |  0
 drivers/mfd/Kconfig                     | 10 ----------
 drivers/mfd/Makefile                    |  1 -
 5 files changed, 11 insertions(+), 11 deletions(-)
 rename drivers/{mfd => iio/adc}/twl4030-madc.c (100%)
drivers/iio/adc/Kconfig +10 -0
index 2209f28..427f75c 100644
@@ -183,6 +183,16 @@ config TI_AM335X_ADC
 	  Say yes here to build support for Texas Instruments ADC
 	  driver which is also a MFD client.
 
+config TWL4030_MADC
+	tristate "TWL4030 MADC (Monitoring A/D Converter)"
+	depends on TWL4030_CORE
+	help
+	This driver provides support for Triton TWL4030-MADC. The
+	driver supports both RT and SW conversion methods.
+
+	This driver can also be built as a module. If so, the module will be
+	called twl4030-madc.
+
 config TWL6030_GPADC
 	tristate "TWL6030 GPADC (General Purpose A/D Converter) Support"
 	depends on TWL4030_CORE
drivers/iio/adc/Makefile +1 -0
index ba9a10a..9acf2df 100644
@@ -20,5 +20,6 @@ obj-$(CONFIG_MCP3422) += mcp3422.o
 obj-$(CONFIG_NAU7802) += nau7802.o
 obj-$(CONFIG_TI_ADC081C) += ti-adc081c.o
 obj-$(CONFIG_TI_AM335X_ADC) += ti_am335x_adc.o
+obj-$(CONFIG_TWL4030_MADC) += twl4030-madc.o
 obj-$(CONFIG_TWL6030_GPADC) += twl6030-gpadc.o
 obj-$(CONFIG_VIPERBOARD_ADC) += viperboard_adc.o
drivers/iio/adc/twl4030-madc.c renamed from drivers/mfd/twl4030-madc.c +0 -0
similarity index 100%
rename from drivers/mfd/twl4030-madc.c
rename to drivers/iio/adc/twl4030-madc.c
drivers/mfd/Kconfig +0 -10
index 49bb445..23a8a51 100644
@@ -935,16 +935,6 @@ config TWL4030_CORE
 	  high speed USB OTG transceiver, an audio codec (on most
 	  versions) and many other features.
 
-config TWL4030_MADC
-	tristate "TI TWL4030 MADC"
-	depends on TWL4030_CORE
-	help
-	This driver provides support for triton TWL4030-MADC. The
-	driver supports both RT and SW conversion methods.
-
-	This driver can be built as a module. If so it will be
-	named twl4030-madc
-
 config TWL4030_POWER
 	bool "TI TWL4030 power resources"
 	depends on TWL4030_CORE && ARM
drivers/mfd/Makefile +0 -1
index 5aea5ef..c8eb0bc 100644
@@ -71,7 +71,6 @@ obj-$(CONFIG_MFD_TPS80031)	+= tps80031.o
 obj-$(CONFIG_MENELAUS)		+= menelaus.o
 
 obj-$(CONFIG_TWL4030_CORE)	+= twl-core.o twl4030-irq.o twl6030-irq.o
-obj-$(CONFIG_TWL4030_MADC)      += twl4030-madc.o
 obj-$(CONFIG_TWL4030_POWER)    += twl4030-power.o
 obj-$(CONFIG_MFD_TWL4030_AUDIO)	+= twl4030-audio.o
 obj-$(CONFIG_TWL6040_CORE)	+= twl6040.o
Signature · 1 line
1.8.5.3
#36 lore raw

Re: [RFCv3 0/7] Convert twl4030-madc to IIO API and add DT support

In reply to Sebastian Reichel

Hi Sebastian,

Quoted 17 lines · Sebastian Reichel

Hi,

This is RFCv3 for converting twl4030-madc to the IIO API and adding DT support. The patchset compiles and has been tested on my Nokia N900.

Changes since RFCv2:
* Use "--find-renames" for the move from mfd/ to iio/adc/ ;)
* Added Acked-by from Lee Jones (MFD subsystem maintainer)
* Fixed issue found by Joe Perches
- twl4030_madc_request.do_avg should be bool instead of u16
* Fixed issues found by Peter Meerwald
- some style issues (e.g. /** instead of /*)
- typos
- usage of u16 read/write
* Tested with converted rx51-battery driver on Nokia N900

Quoted 12 lines

TODO:
* Remove twl4030-madc-hwmon in favour of iio-hwmon This driver is currently only used by board-omap3beagle.c and board-rx51-peripherals.c. The instanciation in the rx51 boardcode is a bug, since actually only twl4030-madc is needed. Thus board-omap3beagle.c needs to be updated to iio-hwmon and board-rx51-peripherals.c to twl4030-madc. Both tasks are pretty trivial and twl4030-madc-hwmon can be removed afterwards :)
* Update twl4030-madc-battery to the IIO API. I guess Marek will do this update in the near future, since he is currently writing DT bindings for the driver.

Yes I'm working on DT for twl4030-madc-battery. I'll convert it also to IIO and post for comments. Thanks for doing that it's really nice work!

Quoted 28 lines

* I updated twl4030-madc-battery already and will send it in a separate patchset.
* Once all of the above steps happened the old API can be removed from the driver, which allows some more cleanups.

-- Sebastian

Sebastian Reichel (7):
mfd: twl4030-madc: Use managed resources
mfd: twl4030-madc: Add DT support and convert to IIO framework
mfd: twl4030-madc: Cleanup driver
mfd: twl-core: Add twl_i2c_read/write_u16
mfd: twl4030-madc: Use twl_i2c_read/write_u16 for 16 bit registers
Documentation: DT: Document twl4030-madc binding
mfd: twl4030-madc: Move driver to drivers/iio/adc

 .../devicetree/bindings/iio/adc/twl4030-madc.txt   |  24 ++
 drivers/iio/adc/Kconfig                            |  10 +
 drivers/iio/adc/Makefile                           |   1 +
 drivers/{mfd => iio/adc}/twl4030-madc.c            | 278 +++++++++++++--------
 drivers/mfd/Kconfig                                |  10 -
 drivers/mfd/Makefile                               |   1 -
 include/linux/i2c/twl.h                            |  12 +
 include/linux/i2c/twl4030-madc.h                   |   2 +-
 8 files changed, 224 insertions(+), 114 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/iio/adc/twl4030-madc.txt
 rename drivers/{mfd => iio/adc}/twl4030-madc.c (77%)
Quoted 3 lines

--
1.8.5.3

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
#37 lore raw

v4 cover Convert twl4030-madc to IIO API and add DT support

In reply to Sebastian Reichel

Hi,

This is RFCv4 for converting twl4030-madc to the IIO API and adding DT support. The patchset compiles and has been tested on my Nokia N900.

Changes since RFCv3 [0]:
* Added Acked-by from Lee Jones (MFD subsystem maintainer) to twl_i2c_read/write_u16 patches
* Fixed style issues found by Lee Jones
This readded the temp variable Jonathan Cameron wanted to be removed, but makes the code cleaner - especially once the brackets for math for clarity are added.
* Tested with converted rx51-battery driver on Nokia N900, which can be found at [1].

[0] https://lkml.org/lkml/2014/2/25/627
[1] https://lkml.org/lkml/2014/2/25/639

-- Sebastian

Sebastian Reichel (7):
mfd: twl4030-madc: Use managed resources
mfd: twl4030-madc: Add DT support and convert to IIO framework
mfd: twl4030-madc: Cleanup driver
mfd: twl-core: Add twl_i2c_read/write_u16
mfd: twl4030-madc: Use twl_i2c_read/write_u16 for 16 bit registers
Documentation: DT: Document twl4030-madc binding
mfd: twl4030-madc: Move driver to drivers/iio/adc

 .../devicetree/bindings/iio/adc/twl4030-madc.txt   |  24 ++
 drivers/iio/adc/Kconfig                            |  10 +
 drivers/iio/adc/Makefile                           |   1 +
 drivers/{mfd => iio/adc}/twl4030-madc.c            | 281 +++++++++++++--------
 drivers/mfd/Kconfig                                |  10 -
 drivers/mfd/Makefile                               |   1 -
 include/linux/i2c/twl.h                            |  12 +
 include/linux/i2c/twl4030-madc.h                   |   2 +-
 8 files changed, 227 insertions(+), 114 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/iio/adc/twl4030-madc.txt
 rename drivers/{mfd => iio/adc}/twl4030-madc.c (77%)
Signature · 1 line
1.8.5.3
#38 lore raw

RFC v4 1/7 mfd: twl4030-madc: Use managed resources

Update twl4030-madc driver to use managed resources.

---
 drivers/mfd/twl4030-madc.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
drivers/mfd/twl4030-madc.c +6 -9
index 4c583e4..5458561 100644
@@ -702,14 +702,14 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 {
 	struct twl4030_madc_data *madc;
 	struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev);
-	int ret;
+	int irq, ret;
 	u8 regval;
 
 	if (!pdata) {
 		dev_err(&pdev->dev, "platform_data not available\n");
 		return -EINVAL;
 	}
-	madc = kzalloc(sizeof(*madc), GFP_KERNEL);
+	madc = devm_kzalloc(&pdev->dev, sizeof(*madc), GFP_KERNEL);
 	if (!madc)
 		return -ENOMEM;
 
@@ -726,7 +726,7 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 	    TWL4030_MADC_ISR1 : TWL4030_MADC_ISR2;
 	ret = twl4030_madc_set_power(madc, 1);
 	if (ret < 0)
-		goto err_power;
+		return ret;
 	ret = twl4030_madc_set_current_generator(madc, 0, 1);
 	if (ret < 0)
 		goto err_current_generator;
@@ -770,7 +770,9 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 
 	platform_set_drvdata(pdev, madc);
 	mutex_init(&madc->lock);
-	ret = request_threaded_irq(platform_get_irq(pdev, 0), NULL,
+
+	irq = platform_get_irq(pdev, 0);
+	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
 				   twl4030_madc_threaded_irq_handler,
 				   IRQF_TRIGGER_RISING, "twl4030_madc", madc);
 	if (ret) {
@@ -783,9 +785,6 @@ err_i2c:
 	twl4030_madc_set_current_generator(madc, 0, 0);
 err_current_generator:
 	twl4030_madc_set_power(madc, 0);
-err_power:
-	kfree(madc);
-
 	return ret;
 }
 
@@ -793,10 +792,8 @@ static int twl4030_madc_remove(struct platform_device *pdev)
 {
 	struct twl4030_madc_data *madc = platform_get_drvdata(pdev);
 
-	free_irq(platform_get_irq(pdev, 0), madc);
 	twl4030_madc_set_current_generator(madc, 0, 0);
 	twl4030_madc_set_power(madc, 0);
-	kfree(madc);
 
 	return 0;
 }
Signature · 1 line
1.8.5.3
#39 lore raw

RFC v4 2/7 mfd: twl4030-madc: Add DT support and convert to IIO framework

In reply to Sebastian Reichel

This converts twl4030-madc module to use the Industrial IO ADC framework and adds device tree support.

---
 drivers/mfd/twl4030-madc.c | 127 +++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 118 insertions(+), 9 deletions(-)
drivers/mfd/twl4030-madc.c +118 -9
index 5458561..37cb3ad 100644
@@ -47,11 +47,14 @@
 #include <linux/gfp.h>
 #include <linux/err.h>
 
+#include <linux/iio/iio.h>
+
 /*
  * struct twl4030_madc_data - a container for madc info
  * @dev - pointer to device structure for madc
  * @lock - mutex protecting this data structure
  * @requests - Array of request struct corresponding to SW1, SW2 and RT
+ * @use_second_irq - IRQ selection (main or co-processor)
  * @imr - Interrupt mask register of MADC
  * @isr - Interrupt status register of MADC
  */
@@ -59,10 +62,73 @@ struct twl4030_madc_data {
 	struct device *dev;
 	struct mutex lock;	/* mutex protecting this data structure */
 	struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS];
+	bool use_second_irq;
 	int imr;
 	int isr;
 };
 
+static int twl4030_madc_read(struct iio_dev *iio_dev,
+			     const struct iio_chan_spec *chan,
+			     int *val, int *val2, long mask)
+{
+	struct twl4030_madc_data *madc = iio_priv(iio_dev);
+	struct twl4030_madc_request req;
+	int ret;
+
+	req.method = madc->use_second_irq ? TWL4030_MADC_SW2 : TWL4030_MADC_SW1;
+
+	req.channels = BIT(chan->channel);
+	req.active = false;
+	req.func_cb = NULL;
+	req.type = TWL4030_MADC_WAIT;
+	req.raw = !(mask == IIO_CHAN_INFO_PROCESSED);
+	req.do_avg = (mask == IIO_CHAN_INFO_AVERAGE_RAW);
+
+	ret = twl4030_madc_conversion(&req);
+	if (ret < 0)
+		return ret;
+
+	*val = req.rbuf[chan->channel];
+
+	return IIO_VAL_INT;
+}
+
+static const struct iio_info twl4030_madc_iio_info = {
+	.read_raw = &twl4030_madc_read,
+	.driver_module = THIS_MODULE,
+};
+
+#define TWL4030_ADC_CHANNEL(_channel, _type, _name, _mask) {	\
+	.type = _type,					\
+	.channel = _channel,				\
+	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |  \
+			      BIT(IIO_CHAN_INFO_AVERAGE_RAW) | \
+			      _mask,			\
+	.datasheet_name = _name,			\
+	.indexed = 1,					\
+}
+
+static const struct iio_chan_spec twl4030_madc_iio_channels[] = {
+	TWL4030_ADC_CHANNEL(0, IIO_VOLTAGE, "ADCIN0", 0),
+	TWL4030_ADC_CHANNEL(1, IIO_TEMP, "ADCIN1",
+		BIT(IIO_CHAN_INFO_PROCESSED)),
+	TWL4030_ADC_CHANNEL(2, IIO_VOLTAGE, "ADCIN2", 0),
+	TWL4030_ADC_CHANNEL(3, IIO_VOLTAGE, "ADCIN3", 0),
+	TWL4030_ADC_CHANNEL(4, IIO_VOLTAGE, "ADCIN4", 0),
+	TWL4030_ADC_CHANNEL(5, IIO_VOLTAGE, "ADCIN5", 0),
+	TWL4030_ADC_CHANNEL(6, IIO_VOLTAGE, "ADCIN6", 0),
+	TWL4030_ADC_CHANNEL(7, IIO_VOLTAGE, "ADCIN7", 0),
+	TWL4030_ADC_CHANNEL(8, IIO_VOLTAGE, "ADCIN8", 0),
+	TWL4030_ADC_CHANNEL(9, IIO_VOLTAGE, "ADCIN9", 0),
+	TWL4030_ADC_CHANNEL(10, IIO_CURRENT, "ADCIN10",
+		BIT(IIO_CHAN_INFO_PROCESSED)),
+	TWL4030_ADC_CHANNEL(11, IIO_VOLTAGE, "ADCIN11", 0),
+	TWL4030_ADC_CHANNEL(12, IIO_VOLTAGE, "ADCIN12", 0),
+	TWL4030_ADC_CHANNEL(13, IIO_VOLTAGE, "ADCIN13", 0),
+	TWL4030_ADC_CHANNEL(14, IIO_VOLTAGE, "ADCIN14", 0),
+	TWL4030_ADC_CHANNEL(15, IIO_VOLTAGE, "ADCIN15", 0),
+};
+
 static struct twl4030_madc_data *twl4030_madc;
 
 struct twl4030_prescale_divider_ratios {
@@ -702,28 +768,51 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 {
 	struct twl4030_madc_data *madc;
 	struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev);
+	struct device_node *np = pdev->dev.of_node;
 	int irq, ret;
 	u8 regval;
+	struct iio_dev *iio_dev = NULL;
 
-	if (!pdata) {
+	if (!pdata && !np) {
 		dev_err(&pdev->dev, "platform_data not available\n");
 		return -EINVAL;
 	}
-	madc = devm_kzalloc(&pdev->dev, sizeof(*madc), GFP_KERNEL);
-	if (!madc)
+
+	iio_dev = devm_iio_device_alloc(&pdev->dev,
+					sizeof(struct twl4030_madc_data));
+	if (!iio_dev) {
+		dev_err(&pdev->dev, "failed allocating iio device\n");
 		return -ENOMEM;
+	}
 
+	madc = iio_priv(iio_dev);
 	madc->dev = &pdev->dev;
+	madc->use_second_irq = false;
+
+	iio_dev->name = dev_name(&pdev->dev);
+	iio_dev->dev.parent = &pdev->dev;
+	iio_dev->dev.of_node = pdev->dev.of_node;
+	iio_dev->info = &twl4030_madc_iio_info;
+	iio_dev->modes = INDIO_DIRECT_MODE;
+	iio_dev->channels = twl4030_madc_iio_channels;
+	iio_dev->num_channels = 16;
 
 	/*
 	 * Phoenix provides 2 interrupt lines. The first one is connected to
 	 * the OMAP. The other one can be connected to the other processor such
 	 * as modem. Hence two separate ISR and IMR registers.
 	 */
-	madc->imr = (pdata->irq_line == 1) ?
-	    TWL4030_MADC_IMR1 : TWL4030_MADC_IMR2;
-	madc->isr = (pdata->irq_line == 1) ?
-	    TWL4030_MADC_ISR1 : TWL4030_MADC_ISR2;
+	if (pdata && pdata->irq_line != 1)
+		madc->use_second_irq = true;
+	else
+		madc->use_second_irq = of_property_read_bool(np,
+				       "ti,system-uses-second-madc-irq");
+
+	madc->imr = madc->use_second_irq ? TWL4030_MADC_IMR2 :
+					   TWL4030_MADC_IMR1;
+	madc->isr = madc->use_second_irq ? TWL4030_MADC_ISR2 :
+					   TWL4030_MADC_ISR1;
+
 	ret = twl4030_madc_set_power(madc, 1);
 	if (ret < 0)
 		return ret;
@@ -768,7 +857,7 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 		}
 	}
 
-	platform_set_drvdata(pdev, madc);
+	platform_set_drvdata(pdev, iio_dev);
 	mutex_init(&madc->lock);
 
 	irq = platform_get_irq(pdev, 0);
@@ -780,7 +869,15 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 		goto err_i2c;
 	}
 	twl4030_madc = madc;
+
+	ret = iio_device_register(iio_dev);
+	if (ret) {
+		dev_dbg(&pdev->dev, "could not register iio device\n");
+		goto err_i2c;
+	}
+
 	return 0;
+
 err_i2c:
 	twl4030_madc_set_current_generator(madc, 0, 0);
 err_current_generator:
@@ -790,20 +887,32 @@ err_current_generator:
 
 static int twl4030_madc_remove(struct platform_device *pdev)
 {
-	struct twl4030_madc_data *madc = platform_get_drvdata(pdev);
+	struct iio_dev *iio_dev = platform_get_drvdata(pdev);
+	struct twl4030_madc_data *madc = iio_priv(iio_dev);
 
 	twl4030_madc_set_current_generator(madc, 0, 0);
 	twl4030_madc_set_power(madc, 0);
 
+	iio_device_unregister(iio_dev);
+
 	return 0;
 }
 
+#ifdef CONFIG_OF
+static const struct of_device_id twl_madc_of_match[] = {
+	{.compatible = "ti,twl4030-madc", },
+	{ },
+};
+MODULE_DEVICE_TABLE(of, twl_madc_of_match);
+#endif
+
 static struct platform_driver twl4030_madc_driver = {
 	.probe = twl4030_madc_probe,
 	.remove = twl4030_madc_remove,
 	.driver = {
 		   .name = "twl4030_madc",
 		   .owner = THIS_MODULE,
+		   .of_match_table = of_match_ptr(twl_madc_of_match),
 		   },
 };
 
Signature · 1 line
1.8.5.3
#40 lore raw

Re: [RFCv4 2/7] mfd: twl4030-madc: Add DT support and convert to IIO framework

Quoted 12 lines · Sebastian Reichel

This converts twl4030-madc module to use the Industrial IO ADC framework and adds device tree support.

---
 drivers/mfd/twl4030-madc.c | 127 +++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 118 insertions(+), 9 deletions(-)
drivers/mfd/twl4030-madc.c +0 -0
index 5458561..37cb3ad 100644

<snip>

drivers/mfd/twl4030-madc.c static int twl4030_madc_probe(struct platform_device *pdev) · line 777

Quoted 3 lines
drivers/mfd/twl4030-madc.c +1 -1
-	if (!pdata) {
+	if (!pdata && !np) {
 		dev_err(&pdev->dev, "platform_data not available\n");

and Device Tree node ...

<snip>

drivers/mfd/twl4030-madc.c static int twl4030_madc_probe(struct platform_device *pdev) · line 798

Quoted 2 lines
drivers/mfd/twl4030-madc.c +2 -0
+	iio_dev->channels = twl4030_madc_iio_channels;
+	iio_dev->num_channels = 16;

ARRAY_SIZE()

<snip>

Signature · 4 lines
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
maintainer
#41 lore raw

Re: [RFCv4 2/7] mfd: twl4030-madc: Add DT support and convert to IIO framework

In reply to Sebastian Reichel
Quoted 4 lines · Sebastian Reichel

This converts twl4030-madc module to use the Industrial IO ADC framework and adds device tree support.

Mostly fine. I've replicated a few things Lee spotted + one logic bug I think in handling the platform_data, device tree interaction.

Quoted 62 lines
---
  drivers/mfd/twl4030-madc.c | 127 +++++++++++++++++++++++++++++++++++++++++----
  1 file changed, 118 insertions(+), 9 deletions(-)
drivers/mfd/twl4030-madc.c +34 -0
index 5458561..37cb3ad 100644
@@ -47,11 +47,14 @@
  #include <linux/gfp.h>
  #include <linux/err.h>
 
+#include <linux/iio/iio.h>
+
  /*
   * struct twl4030_madc_data - a container for madc info
   * @dev - pointer to device structure for madc
   * @lock - mutex protecting this data structure
   * @requests - Array of request struct corresponding to SW1, SW2 and RT
+ * @use_second_irq - IRQ selection (main or co-processor)
   * @imr - Interrupt mask register of MADC
   * @isr - Interrupt status register of MADC
   */
@@ -59,10 +62,73 @@ struct twl4030_madc_data {
  	struct device *dev;
  	struct mutex lock;	/* mutex protecting this data structure */
  	struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS];
+	bool use_second_irq;
  	int imr;
  	int isr;
  };
 
+static int twl4030_madc_read(struct iio_dev *iio_dev,
+			     const struct iio_chan_spec *chan,
+			     int *val, int *val2, long mask)
+{
+	struct twl4030_madc_data *madc = iio_priv(iio_dev);
+	struct twl4030_madc_request req;
+	int ret;
+
+	req.method = madc->use_second_irq ? TWL4030_MADC_SW2 : TWL4030_MADC_SW1;
+
+	req.channels = BIT(chan->channel);
+	req.active = false;
+	req.func_cb = NULL;
+	req.type = TWL4030_MADC_WAIT;
+	req.raw = !(mask == IIO_CHAN_INFO_PROCESSED);
+	req.do_avg = (mask == IIO_CHAN_INFO_AVERAGE_RAW);
+
+	ret = twl4030_madc_conversion(&req);
+	if (ret < 0)
+		return ret;
+
+	*val = req.rbuf[chan->channel];
+
+	return IIO_VAL_INT;
+}
+
+static const struct iio_info twl4030_madc_iio_info = {
+	.read_raw = &twl4030_madc_read,
+	.driver_module = THIS_MODULE,
+};

drivers/mfd/twl4030-madc.c struct twl4030_madc_data { · line 109

Quoted 10 lines
drivers/mfd/twl4030-madc.c +10 -0
+
+#define TWL4030_ADC_CHANNEL(_channel, _type, _name, _mask) {	\
+	.type = _type,					\
+	.channel = _channel,				\
+	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |  \
+			      BIT(IIO_CHAN_INFO_AVERAGE_RAW) | \
+			      _mask,			\
+	.datasheet_name = _name,			\
+	.indexed = 1,					\
+}

A small addition to the ABI docs to include the average case would be good. Obviously it should be there already but it isn't as far as I can see.

Quoted 34 lines
drivers/mfd/twl4030-madc.c +24 -0
+
+static const struct iio_chan_spec twl4030_madc_iio_channels[] = {
+	TWL4030_ADC_CHANNEL(0, IIO_VOLTAGE, "ADCIN0", 0),
+	TWL4030_ADC_CHANNEL(1, IIO_TEMP, "ADCIN1",
+		BIT(IIO_CHAN_INFO_PROCESSED)),
+	TWL4030_ADC_CHANNEL(2, IIO_VOLTAGE, "ADCIN2", 0),
+	TWL4030_ADC_CHANNEL(3, IIO_VOLTAGE, "ADCIN3", 0),
+	TWL4030_ADC_CHANNEL(4, IIO_VOLTAGE, "ADCIN4", 0),
+	TWL4030_ADC_CHANNEL(5, IIO_VOLTAGE, "ADCIN5", 0),
+	TWL4030_ADC_CHANNEL(6, IIO_VOLTAGE, "ADCIN6", 0),
+	TWL4030_ADC_CHANNEL(7, IIO_VOLTAGE, "ADCIN7", 0),
+	TWL4030_ADC_CHANNEL(8, IIO_VOLTAGE, "ADCIN8", 0),
+	TWL4030_ADC_CHANNEL(9, IIO_VOLTAGE, "ADCIN9", 0),
+	TWL4030_ADC_CHANNEL(10, IIO_CURRENT, "ADCIN10",
+		BIT(IIO_CHAN_INFO_PROCESSED)),
+	TWL4030_ADC_CHANNEL(11, IIO_VOLTAGE, "ADCIN11", 0),
+	TWL4030_ADC_CHANNEL(12, IIO_VOLTAGE, "ADCIN12", 0),
+	TWL4030_ADC_CHANNEL(13, IIO_VOLTAGE, "ADCIN13", 0),
+	TWL4030_ADC_CHANNEL(14, IIO_VOLTAGE, "ADCIN14", 0),
+	TWL4030_ADC_CHANNEL(15, IIO_VOLTAGE, "ADCIN15", 0),
+};
+
  static struct twl4030_madc_data *twl4030_madc;
 
  struct twl4030_prescale_divider_ratios {
@@ -702,28 +768,51 @@ static int twl4030_madc_probe(struct platform_device *pdev)
  {
  	struct twl4030_madc_data *madc;
  	struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev);
+	struct device_node *np = pdev->dev.of_node;
  	int irq, ret;
  	u8 regval;
+	struct iio_dev *iio_dev = NULL;
 

drivers/mfd/twl4030-madc.c static int twl4030_madc_probe(struct platform_device *pdev) · line 782

Quoted 10 lines
drivers/mfd/twl4030-madc.c +4 -3
-	if (!pdata) {
+	if (!pdata && !np) {
  		dev_err(&pdev->dev, "platform_data not available\n");
  		return -EINVAL;
  	}
-	madc = devm_kzalloc(&pdev->dev, sizeof(*madc), GFP_KERNEL);
-	if (!madc)
+
+	iio_dev = devm_iio_device_alloc(&pdev->dev,
+					sizeof(struct twl4030_madc_data));

sizeof(*madc)) is shorter and slightly prefered.

Quoted 6 lines
drivers/mfd/twl4030-madc.c +4 -0
+	if (!iio_dev) {
+		dev_err(&pdev->dev, "failed allocating iio device\n");
  		return -ENOMEM;
+	}
 
+	madc = iio_priv(iio_dev);

drivers/mfd/twl4030-madc.c static int twl4030_madc_probe(struct platform_device *pdev) · line 798

Quoted 10 lines
drivers/mfd/twl4030-madc.c +9 -0
  	madc->dev = &pdev->dev;
+	madc->use_second_irq = false;
+
+	iio_dev->name = dev_name(&pdev->dev);
+	iio_dev->dev.parent = &pdev->dev;
+	iio_dev->dev.of_node = pdev->dev.of_node;
+	iio_dev->info = &twl4030_madc_iio_info;
+	iio_dev->modes = INDIO_DIRECT_MODE;
+	iio_dev->channels = twl4030_madc_iio_channels;
+	iio_dev->num_channels = 16;

Preferred to use ARRAY_SIZE(twl4030_madc_iio_channels); That's obviously right, whereas with an explicity number I had to go check :(

drivers/mfd/twl4030-madc.c static int twl4030_madc_probe(struct platform_device *pdev) · line 806

Quoted 12 lines
drivers/mfd/twl4030-madc.c +2 -4
  	/*
  	 * Phoenix provides 2 interrupt lines. The first one is connected to
  	 * the OMAP. The other one can be connected to the other processor such
  	 * as modem. Hence two separate ISR and IMR registers.
  	 */
-	madc->imr = (pdata->irq_line == 1) ?
-	    TWL4030_MADC_IMR1 : TWL4030_MADC_IMR2;
-	madc->isr = (pdata->irq_line == 1) ?
-	    TWL4030_MADC_ISR1 : TWL4030_MADC_ISR2;
+	if (pdata && pdata->irq_line != 1)
+		madc->use_second_irq = true;

This logic looks wrong. If pdata is true, but pdata->irq_line = 0 it will read the of data which might not be there.

Quoted 72 lines
drivers/mfd/twl4030-madc.c +31 -2
+	else
+		madc->use_second_irq = of_property_read_bool(np,
+				       "ti,system-uses-second-madc-irq");
+
+	madc->imr = madc->use_second_irq ? TWL4030_MADC_IMR2 :
+					   TWL4030_MADC_IMR1;
+	madc->isr = madc->use_second_irq ? TWL4030_MADC_ISR2 :
+					   TWL4030_MADC_ISR1;
+
  	ret = twl4030_madc_set_power(madc, 1);
  	if (ret < 0)
  		return ret;
@@ -768,7 +857,7 @@ static int twl4030_madc_probe(struct platform_device *pdev)
  		}
  	}
 
-	platform_set_drvdata(pdev, madc);
+	platform_set_drvdata(pdev, iio_dev);
  	mutex_init(&madc->lock);
 
  	irq = platform_get_irq(pdev, 0);
@@ -780,7 +869,15 @@ static int twl4030_madc_probe(struct platform_device *pdev)
  		goto err_i2c;
  	}
  	twl4030_madc = madc;
+
+	ret = iio_device_register(iio_dev);
+	if (ret) {
+		dev_dbg(&pdev->dev, "could not register iio device\n");
+		goto err_i2c;
+	}
+
  	return 0;
+
  err_i2c:
  	twl4030_madc_set_current_generator(madc, 0, 0);
  err_current_generator:
@@ -790,20 +887,32 @@ err_current_generator:
 
  static int twl4030_madc_remove(struct platform_device *pdev)
  {
-	struct twl4030_madc_data *madc = platform_get_drvdata(pdev);
+	struct iio_dev *iio_dev = platform_get_drvdata(pdev);
+	struct twl4030_madc_data *madc = iio_priv(iio_dev);
 
  	twl4030_madc_set_current_generator(madc, 0, 0);
  	twl4030_madc_set_power(madc, 0);
 
+	iio_device_unregister(iio_dev);
+
  	return 0;
  }
 
+#ifdef CONFIG_OF
+static const struct of_device_id twl_madc_of_match[] = {
+	{.compatible = "ti,twl4030-madc", },
+	{ },
+};
+MODULE_DEVICE_TABLE(of, twl_madc_of_match);
+#endif
+
  static struct platform_driver twl4030_madc_driver = {
  	.probe = twl4030_madc_probe,
  	.remove = twl4030_madc_remove,
  	.driver = {
  		   .name = "twl4030_madc",
  		   .owner = THIS_MODULE,
+		   .of_match_table = of_match_ptr(twl_madc_of_match),
  		   },
  };
 
#42 lore raw

RFC v4 3/7 mfd: twl4030-madc: Cleanup driver

In reply to Sebastian Reichel

Some style fixes in twl4030-madc driver.

---
 drivers/mfd/twl4030-madc.c       | 106 ++++++++++++++++++---------------------
 include/linux/i2c/twl4030-madc.h |   2 +-
 2 files changed, 51 insertions(+), 57 deletions(-)
drivers/mfd/twl4030-madc.c +50 -56
index 37cb3ad..c90013e 100644
@@ -49,7 +49,7 @@
 
 #include <linux/iio/iio.h>
 
-/*
+/**
  * struct twl4030_madc_data - a container for madc info
  * @dev - pointer to device structure for madc
  * @lock - mutex protecting this data structure
@@ -63,8 +63,8 @@ struct twl4030_madc_data {
 	struct mutex lock;	/* mutex protecting this data structure */
 	struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS];
 	bool use_second_irq;
-	int imr;
-	int isr;
+	u8 imr;
+	u8 isr;
 };
 
 static int twl4030_madc_read(struct iio_dev *iio_dev,
@@ -157,17 +157,16 @@ twl4030_divider_ratios[16] = {
 };
 
 
-/*
- * Conversion table from -3 to 55 degree Celcius
- */
-static int therm_tbl[] = {
-30800,	29500,	28300,	27100,
-26000,	24900,	23900,	22900,	22000,	21100,	20300,	19400,	18700,	17900,
-17200,	16500,	15900,	15300,	14700,	14100,	13600,	13100,	12600,	12100,
-11600,	11200,	10800,	10400,	10000,	9630,	9280,	8950,	8620,	8310,
-8020,	7730,	7460,	7200,	6950,	6710,	6470,	6250,	6040,	5830,
-5640,	5450,	5260,	5090,	4920,	4760,	4600,	4450,	4310,	4170,
-4040,	3910,	3790,	3670,	3550
+/* Conversion table from -3 to 55 degrees Celcius */
+static int twl4030_therm_tbl[] = {
+	30800,	29500,	28300,	27100,
+	26000,	24900,	23900,	22900,	22000,	21100,	20300,	19400,	18700,
+	17900,	17200,	16500,	15900,	15300,	14700,	14100,	13600,	13100,
+	12600,	12100,	11600,	11200,	10800,	10400,	10000,	9630,	9280,
+	8950,	8620,	8310,	8020,	7730,	7460,	7200,	6950,	6710,
+	6470,	6250,	6040,	5830,	5640,	5450,	5260,	5090,	4920,
+	4760,	4600,	4450,	4310,	4170,	4040,	3910,	3790,	3670,
+	3550
 };
 
 /*
@@ -199,7 +198,7 @@ const struct twl4030_madc_conversion_method twl4030_conversion_methods[] = {
 			      },
 };
 
-/*
+/**
  * Function to read a particular channel value.
  * @madc - pointer to struct twl4030_madc_data
  * @reg - lsb of ADC Channel
@@ -229,7 +228,7 @@ static int twl4030_madc_channel_raw_read(struct twl4030_madc_data *madc, u8 reg)
 }
 
 /*
- * Return battery temperature
+ * Return battery temperature in degrees Celsius
  * Or < 0 on failure.
  */
 static int twl4030battery_temperature(int raw_volt)
@@ -238,18 +237,18 @@ static int twl4030battery_temperature(int raw_volt)
 	int temp, curr, volt, res, ret;
 
 	volt = (raw_volt * TEMP_STEP_SIZE) / TEMP_PSR_R;
-	/* Getting and calculating the supply current in micro ampers */
+	/* Getting and calculating the supply current in micro amperes */
 	ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val,
 		REG_BCICTL2);
 	if (ret < 0)
 		return ret;
+
 	curr = ((val & TWL4030_BCI_ITHEN) + 1) * 10;
 	/* Getting and calculating the thermistor resistance in ohms */
 	res = volt * 1000 / curr;
 	/* calculating temperature */
 	for (temp = 58; temp >= 0; temp--) {
-		int actual = therm_tbl[temp];
-
+		int actual = twl4030_therm_tbl[temp];
 		if ((actual - res) >= 0)
 			break;
 	}
@@ -271,11 +270,12 @@ static int twl4030battery_current(int raw_volt)
 	else /* slope of 0.88 mV/mA */
 		return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R2;
 }
+
 /*
  * Function to read channel values
  * @madc - pointer to twl4030_madc_data struct
  * @reg_base - Base address of the first channel
- * @Channels - 16 bit bitmap. If the bit is set, channel value is read
+ * @Channels - 16 bit bitmap. If the bit is set, channel's value is read
  * @buf - The channel values are stored here. if read fails error
  * @raw - Return raw values without conversion
  * value is stored
@@ -286,17 +286,17 @@ static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
 				      long channels, int *buf,
 				      bool raw)
 {
-	int count = 0, count_req = 0, i;
+	int count = 0;
+	int i;
 	u8 reg;
 
 	for_each_set_bit(i, &channels, TWL4030_MADC_MAX_CHANNELS) {
-		reg = reg_base + 2 * i;
+		u8 reg = reg_base + (2 * i);
 		buf[i] = twl4030_madc_channel_raw_read(madc, reg);
 		if (buf[i] < 0) {
-			dev_err(madc->dev,
-				"Unable to read register 0x%X\n", reg);
-			count_req++;
-			continue;
+			dev_err(madc->dev, "Unable to read register 0x%X\n",
+				reg);
+			return buf[i];
 		}
 		if (raw) {
 			count++;
@@ -307,7 +307,7 @@ static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
 			buf[i] = twl4030battery_current(buf[i]);
 			if (buf[i] < 0) {
 				dev_err(madc->dev, "err reading current\n");
-				count_req++;
+				return buf[i];
 			} else {
 				count++;
 				buf[i] = buf[i] - 750;
@@ -317,7 +317,7 @@ static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
 			buf[i] = twl4030battery_temperature(buf[i]);
 			if (buf[i] < 0) {
 				dev_err(madc->dev, "err reading temperature\n");
-				count_req++;
+				return buf[i];
 			} else {
 				buf[i] -= 3;
 				count++;
@@ -338,8 +338,6 @@ static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
 				twl4030_divider_ratios[i].numerator);
 		}
 	}
-	if (count_req)
-		dev_err(madc->dev, "%d channel conversion failed\n", count_req);
 
 	return count;
 }
@@ -363,13 +361,13 @@ static int twl4030_madc_enable_irq(struct twl4030_madc_data *madc, u8 id)
 			madc->imr);
 		return ret;
 	}
+
 	val &= ~(1 << id);
 	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr);
 	if (ret) {
 		dev_err(madc->dev,
 			"unable to write imr register 0x%X\n", madc->imr);
 		return ret;
-
 	}
 
 	return 0;
@@ -432,7 +430,7 @@ static irqreturn_t twl4030_madc_threaded_irq_handler(int irq, void *_madc)
 			continue;
 		ret = twl4030_madc_disable_irq(madc, i);
 		if (ret < 0)
-			dev_dbg(madc->dev, "Disable interrupt failed%d\n", i);
+			dev_dbg(madc->dev, "Disable interrupt failed %d\n", i);
 		madc->requests[i].result_pending = 1;
 	}
 	for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
@@ -514,21 +512,17 @@ static int twl4030_madc_start_conversion(struct twl4030_madc_data *madc,
 {
 	const struct twl4030_madc_conversion_method *method;
 	int ret = 0;
+
+	if (conv_method != TWL4030_MADC_SW1 && conv_method != TWL4030_MADC_SW2)
+		return -ENOTSUPP;
+
 	method = &twl4030_conversion_methods[conv_method];
-	switch (conv_method) {
-	case TWL4030_MADC_SW1:
-	case TWL4030_MADC_SW2:
-		ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
-				       TWL4030_MADC_SW_START, method->ctrl);
-		if (ret) {
-			dev_err(madc->dev,
-				"unable to write ctrl register 0x%X\n",
-				method->ctrl);
-			return ret;
-		}
-		break;
-	default:
-		break;
+	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, TWL4030_MADC_SW_START,
+			       method->ctrl);
+	if (ret) {
+		dev_err(madc->dev, "unable to write ctrl register 0x%X\n",
+			method->ctrl);
+		return ret;
 	}
 
 	return 0;
@@ -625,8 +619,8 @@ int twl4030_madc_conversion(struct twl4030_madc_request *req)
 				       ch_lsb, method->avg);
 		if (ret) {
 			dev_err(twl4030_madc->dev,
-				"unable to write sel reg 0x%X\n",
-				method->sel + 1);
+				"unable to write avg reg 0x%X\n",
+				method->avg);
 			goto out;
 		}
 	}
@@ -667,10 +661,6 @@ out:
 }
 EXPORT_SYMBOL_GPL(twl4030_madc_conversion);
 
-/*
- * Return channel value
- * Or < 0 on failure.
- */
 int twl4030_get_madc_conversion(int channel_no)
 {
 	struct twl4030_madc_request req;
@@ -705,6 +695,7 @@ static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc,
 					      int chan, int on)
 {
 	int ret;
+	int regmask;
 	u8 regval;
 
 	ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE,
@@ -714,10 +705,13 @@ static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc,
 			TWL4030_BCI_BCICTL1);
 		return ret;
 	}
+
+	regmask = chan ? TWL4030_BCI_ITHEN : TWL4030_BCI_TYPEN;
 	if (on)
-		regval |= chan ? TWL4030_BCI_ITHEN : TWL4030_BCI_TYPEN;
+		regval |= regmask;
 	else
-		regval &= chan ? ~TWL4030_BCI_ITHEN : ~TWL4030_BCI_TYPEN;
+		regval &= ~regmask;
+
 	ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
 			       regval, TWL4030_BCI_BCICTL1);
 	if (ret) {
@@ -732,7 +726,7 @@ static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc,
 /*
  * Function that sets MADC software power on bit to enable MADC
  * @madc - pointer to twl4030_madc_data struct
- * @on - Enable or disable MADC software powen on bit.
+ * @on - Enable or disable MADC software power on bit.
  * returns error if i2c read/write fails else 0
  */
 static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on)
@@ -795,7 +789,7 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 	iio_dev->info = &twl4030_madc_iio_info;
 	iio_dev->modes = INDIO_DIRECT_MODE;
 	iio_dev->channels = twl4030_madc_iio_channels;
-	iio_dev->num_channels = 16;
+	iio_dev->num_channels = ARRAY_SIZE(twl4030_madc_iio_channels);
 
 	/*
 	 * Phoenix provides 2 interrupt lines. The first one is connected to
include/linux/i2c/twl4030-madc.h +1 -1
index 01f5951..1c0134d 100644
@@ -44,7 +44,7 @@ struct twl4030_madc_conversion_method {
 
 struct twl4030_madc_request {
 	unsigned long channels;
-	u16 do_avg;
+	bool do_avg;
 	u16 method;
 	u16 type;
 	bool active;
Signature · 1 line
1.8.5.3
Acked-by on v4 3/7
#43 lore raw

Re: [RFCv4 3/7] mfd: twl4030-madc: Cleanup driver

drivers/mfd/twl4030-madc.c

Quoted 10 lines

Some style fixes in twl4030-madc driver.

---
 drivers/mfd/twl4030-madc.c       | 106 ++++++++++++++++++---------------------
 include/linux/i2c/twl4030-madc.h |   2 +-
 2 files changed, 51 insertions(+), 57 deletions(-)
drivers/mfd/twl4030-madc.c +0 -0
Signature · 4 lines
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
maintainer
#44 lore raw

Re: [RFCv4 3/7] mfd: twl4030-madc: Cleanup driver

In reply to Sebastian Reichel
Quoted 4 lines · Sebastian Reichel

Some style fixes in twl4030-madc driver.

Good stuff, but would be nice to hammer the comments into proper kernel-doc style rather than going part of the way. Also, the ARRAY_SIZE bit that both Lee and I picked up on in the previous patch snuck into this one instead. Please move it back one patch.

Quoted 10 lines
---
  drivers/mfd/twl4030-madc.c       | 106 ++++++++++++++++++---------------------
  include/linux/i2c/twl4030-madc.h |   2 +-
  2 files changed, 51 insertions(+), 57 deletions(-)
drivers/mfd/twl4030-madc.c +0 -0
index 37cb3ad..c90013e 100644

drivers/mfd/twl4030-madc.c line 53

Quoted 6 lines
drivers/mfd/twl4030-madc.c +1 -1
@@ -49,7 +49,7 @@
 
  #include <linux/iio/iio.h>
 
-/*
+/**
   * struct twl4030_madc_data - a container for madc info

@dev: for kerneldoc.

Quoted 42 lines
drivers/mfd/twl4030-madc.c +12 -13
   * @dev - pointer to device structure for madc
   * @lock - mutex protecting this data structure
@@ -63,8 +63,8 @@ struct twl4030_madc_data {
  	struct mutex lock;	/* mutex protecting this data structure */
  	struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS];
  	bool use_second_irq;
-	int imr;
-	int isr;
+	u8 imr;
+	u8 isr;
  };
 
  static int twl4030_madc_read(struct iio_dev *iio_dev,
@@ -157,17 +157,16 @@ twl4030_divider_ratios[16] = {
  };
 
 
-/*
- * Conversion table from -3 to 55 degree Celcius
- */
-static int therm_tbl[] = {
-30800,	29500,	28300,	27100,
-26000,	24900,	23900,	22900,	22000,	21100,	20300,	19400,	18700,	17900,
-17200,	16500,	15900,	15300,	14700,	14100,	13600,	13100,	12600,	12100,
-11600,	11200,	10800,	10400,	10000,	9630,	9280,	8950,	8620,	8310,
-8020,	7730,	7460,	7200,	6950,	6710,	6470,	6250,	6040,	5830,
-5640,	5450,	5260,	5090,	4920,	4760,	4600,	4450,	4310,	4170,
-4040,	3910,	3790,	3670,	3550
+/* Conversion table from -3 to 55 degrees Celcius */
+static int twl4030_therm_tbl[] = {
+	30800,	29500,	28300,	27100,
+	26000,	24900,	23900,	22900,	22000,	21100,	20300,	19400,	18700,
+	17900,	17200,	16500,	15900,	15300,	14700,	14100,	13600,	13100,
+	12600,	12100,	11600,	11200,	10800,	10400,	10000,	9630,	9280,
+	8950,	8620,	8310,	8020,	7730,	7460,	7200,	6950,	6710,
+	6470,	6250,	6040,	5830,	5640,	5450,	5260,	5090,	4920,
+	4760,	4600,	4450,	4310,	4170,	4040,	3910,	3790,	3670,
+	3550
  };
 
  /*

drivers/mfd/twl4030-madc.c const struct twl4030_madc_conversion_method twl4030_conversion_methods[] = { · line 201

Quoted 5 lines
drivers/mfd/twl4030-madc.c +1 -1
@@ -199,7 +198,7 @@ const struct twl4030_madc_conversion_method twl4030_conversion_methods[] = {
  			      },
  };
 
-/*
+/**

Whilst better - these are still not in kernel doc. Please read the nano howto and fix them up.

Quoted 50 lines
drivers/mfd/twl4030-madc.c +6 -5
   * Function to read a particular channel value.
   * @madc - pointer to struct twl4030_madc_data
   * @reg - lsb of ADC Channel
@@ -229,7 +228,7 @@ static int twl4030_madc_channel_raw_read(struct twl4030_madc_data *madc, u8 reg)
  }
 
  /*
- * Return battery temperature
+ * Return battery temperature in degrees Celsius
   * Or < 0 on failure.
   */
  static int twl4030battery_temperature(int raw_volt)
@@ -238,18 +237,18 @@ static int twl4030battery_temperature(int raw_volt)
  	int temp, curr, volt, res, ret;
 
  	volt = (raw_volt * TEMP_STEP_SIZE) / TEMP_PSR_R;
-	/* Getting and calculating the supply current in micro ampers */
+	/* Getting and calculating the supply current in micro amperes */
  	ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val,
  		REG_BCICTL2);
  	if (ret < 0)
  		return ret;
+
  	curr = ((val & TWL4030_BCI_ITHEN) + 1) * 10;
  	/* Getting and calculating the thermistor resistance in ohms */
  	res = volt * 1000 / curr;
  	/* calculating temperature */
  	for (temp = 58; temp >= 0; temp--) {
-		int actual = therm_tbl[temp];
-
+		int actual = twl4030_therm_tbl[temp];
  		if ((actual - res) >= 0)
  			break;
  	}
@@ -271,11 +270,12 @@ static int twl4030battery_current(int raw_volt)
  	else /* slope of 0.88 mV/mA */
  		return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R2;
  }
+
  /*
   * Function to read channel values
   * @madc - pointer to twl4030_madc_data struct
   * @reg_base - Base address of the first channel
- * @Channels - 16 bit bitmap. If the bit is set, channel value is read
+ * @Channels - 16 bit bitmap. If the bit is set, channel's value is read
   * @buf - The channel values are stored here. if read fails error
   * @raw - Return raw values without conversion
   * value is stored
@@ -286,17 +286,17 @@ static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
  				      long channels, int *buf,

drivers/mfd/twl4030-madc.c static int twl4030_madc_read_channels(struct twl4030_madc_data *madc, · line 294

Quoted 10 lines
drivers/mfd/twl4030-madc.c +3 -2
  				      bool raw)
  {
-	int count = 0, count_req = 0, i;
+	int count = 0;
+	int i;
  	u8 reg;
 
  	for_each_set_bit(i, &channels, TWL4030_MADC_MAX_CHANNELS) {
-		reg = reg_base + 2 * i;
+		u8 reg = reg_base + (2 * i);

Precedence means the brackets aren't needed. I don't suppose they do much harm though if you want to keep them.

Quoted 151 lines
drivers/mfd/twl4030-madc.c +26 -33
  		buf[i] = twl4030_madc_channel_raw_read(madc, reg);
  		if (buf[i] < 0) {
-			dev_err(madc->dev,
-				"Unable to read register 0x%X\n", reg);
-			count_req++;
-			continue;
+			dev_err(madc->dev, "Unable to read register 0x%X\n",
+				reg);
+			return buf[i];
  		}
  		if (raw) {
  			count++;
@@ -307,7 +307,7 @@ static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
  			buf[i] = twl4030battery_current(buf[i]);
  			if (buf[i] < 0) {
  				dev_err(madc->dev, "err reading current\n");
-				count_req++;
+				return buf[i];
  			} else {
  				count++;
  				buf[i] = buf[i] - 750;
@@ -317,7 +317,7 @@ static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
  			buf[i] = twl4030battery_temperature(buf[i]);
  			if (buf[i] < 0) {
  				dev_err(madc->dev, "err reading temperature\n");
-				count_req++;
+				return buf[i];
  			} else {
  				buf[i] -= 3;
  				count++;
@@ -338,8 +338,6 @@ static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
  				twl4030_divider_ratios[i].numerator);
  		}
  	}
-	if (count_req)
-		dev_err(madc->dev, "%d channel conversion failed\n", count_req);
 
  	return count;
  }
@@ -363,13 +361,13 @@ static int twl4030_madc_enable_irq(struct twl4030_madc_data *madc, u8 id)
  			madc->imr);
  		return ret;
  	}
+
  	val &= ~(1 << id);
  	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr);
  	if (ret) {
  		dev_err(madc->dev,
  			"unable to write imr register 0x%X\n", madc->imr);
  		return ret;
-
  	}
 
  	return 0;
@@ -432,7 +430,7 @@ static irqreturn_t twl4030_madc_threaded_irq_handler(int irq, void *_madc)
  			continue;
  		ret = twl4030_madc_disable_irq(madc, i);
  		if (ret < 0)
-			dev_dbg(madc->dev, "Disable interrupt failed%d\n", i);
+			dev_dbg(madc->dev, "Disable interrupt failed %d\n", i);
  		madc->requests[i].result_pending = 1;
  	}
  	for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
@@ -514,21 +512,17 @@ static int twl4030_madc_start_conversion(struct twl4030_madc_data *madc,
  {
  	const struct twl4030_madc_conversion_method *method;
  	int ret = 0;
+
+	if (conv_method != TWL4030_MADC_SW1 && conv_method != TWL4030_MADC_SW2)
+		return -ENOTSUPP;
+
  	method = &twl4030_conversion_methods[conv_method];
-	switch (conv_method) {
-	case TWL4030_MADC_SW1:
-	case TWL4030_MADC_SW2:
-		ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
-				       TWL4030_MADC_SW_START, method->ctrl);
-		if (ret) {
-			dev_err(madc->dev,
-				"unable to write ctrl register 0x%X\n",
-				method->ctrl);
-			return ret;
-		}
-		break;
-	default:
-		break;
+	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, TWL4030_MADC_SW_START,
+			       method->ctrl);
+	if (ret) {
+		dev_err(madc->dev, "unable to write ctrl register 0x%X\n",
+			method->ctrl);
+		return ret;
  	}
 
  	return 0;
@@ -625,8 +619,8 @@ int twl4030_madc_conversion(struct twl4030_madc_request *req)
  				       ch_lsb, method->avg);
  		if (ret) {
  			dev_err(twl4030_madc->dev,
-				"unable to write sel reg 0x%X\n",
-				method->sel + 1);
+				"unable to write avg reg 0x%X\n",
+				method->avg);
  			goto out;
  		}
  	}
@@ -667,10 +661,6 @@ out:
  }
  EXPORT_SYMBOL_GPL(twl4030_madc_conversion);
 
-/*
- * Return channel value
- * Or < 0 on failure.
- */
  int twl4030_get_madc_conversion(int channel_no)
  {
  	struct twl4030_madc_request req;
@@ -705,6 +695,7 @@ static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc,
  					      int chan, int on)
  {
  	int ret;
+	int regmask;
  	u8 regval;
 
  	ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE,
@@ -714,10 +705,13 @@ static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc,
  			TWL4030_BCI_BCICTL1);
  		return ret;
  	}
+
+	regmask = chan ? TWL4030_BCI_ITHEN : TWL4030_BCI_TYPEN;
  	if (on)
-		regval |= chan ? TWL4030_BCI_ITHEN : TWL4030_BCI_TYPEN;
+		regval |= regmask;
  	else
-		regval &= chan ? ~TWL4030_BCI_ITHEN : ~TWL4030_BCI_TYPEN;
+		regval &= ~regmask;
+
  	ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
  			       regval, TWL4030_BCI_BCICTL1);
  	if (ret) {
@@ -732,7 +726,7 @@ static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc,
  /*
   * Function that sets MADC software power on bit to enable MADC
   * @madc - pointer to twl4030_madc_data struct
- * @on - Enable or disable MADC software powen on bit.
+ * @on - Enable or disable MADC software power on bit.
   * returns error if i2c read/write fails else 0
   */
  static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on)

drivers/mfd/twl4030-madc.c static int twl4030_madc_probe(struct platform_device *pdev) · line 792

Quoted 5 lines
drivers/mfd/twl4030-madc.c +1 -1
@@ -795,7 +789,7 @@ static int twl4030_madc_probe(struct platform_device *pdev)
  	iio_dev->info = &twl4030_madc_iio_info;
  	iio_dev->modes = INDIO_DIRECT_MODE;
  	iio_dev->channels = twl4030_madc_iio_channels;
-	iio_dev->num_channels = 16;
+	iio_dev->num_channels = ARRAY_SIZE(twl4030_madc_iio_channels);

Move this back into the previous patch please. We like to pretend that within a series we got everything right first time ;)

Quoted 17 lines
drivers/mfd/twl4030-madc.c +0 -0
  	/*
  	 * Phoenix provides 2 interrupt lines. The first one is connected to
include/linux/i2c/twl4030-madc.h +1 -1
index 01f5951..1c0134d 100644
@@ -44,7 +44,7 @@ struct twl4030_madc_conversion_method {
 
  struct twl4030_madc_request {
  	unsigned long channels;
-	u16 do_avg;
+	bool do_avg;
  	u16 method;
  	u16 type;
  	bool active;
#45 lore raw

RFC v4 4/7 mfd: twl-core: Add twl_i2c_read/write_u16

In reply to Sebastian Reichel

Add a simple twl_i2c_read/write_u16 wrapper over
the twl_i2c_read/write, which is similar to the
twl_i2c_read/write_u8 wrapper.

---
 include/linux/i2c/twl.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)
include/linux/i2c/twl.h +12 -0
index ade1c06..d2b1670 100644
@@ -195,6 +195,18 @@ static inline int twl_i2c_read_u8(u8 mod_no, u8 *val, u8 reg) {
 	return twl_i2c_read(mod_no, val, reg, 1);
 }
 
+static inline int twl_i2c_write_u16(u8 mod_no, u16 val, u8 reg) {
+	val = cpu_to_le16(val);
+	return twl_i2c_write(mod_no, (u8*) &val, reg, 2);
+}
+
+static inline int twl_i2c_read_u16(u8 mod_no, u16 *val, u8 reg) {
+	int ret;
+	ret = twl_i2c_read(mod_no, (u8*) val, reg, 2);
+	*val = le16_to_cpu(*val);
+	return ret;
+}
+
 int twl_get_type(void);
 int twl_get_version(void);
 int twl_get_hfclk_rate(void);
Signature · 1 line
1.8.5.3
maintainer Acked-by on v4 4/7
#46 lore raw

Re: [RFCv4 4/7] mfd: twl-core: Add twl_i2c_read/write_u16

Quoted 6 lines · Sebastian Reichel

Add a simple twl_i2c_read/write_u16 wrapper over
the twl_i2c_read/write, which is similar to the
twl_i2c_read/write_u8 wrapper.

(I'll start a separate conversation about who will take this series in a mo).

include/linux/i2c/twl.h static inline int twl_i2c_read_u8(u8 mod_no, u8 *val, u8 reg) { · line 198

Quoted 13 lines
---
  include/linux/i2c/twl.h | 12 ++++++++++++
  1 file changed, 12 insertions(+)
include/linux/i2c/twl.h +0 -0
index ade1c06..d2b1670 100644
@@ -195,6 +195,18 @@ static inline int twl_i2c_read_u8(u8 mod_no, u8 *val, u8 reg) {
  	return twl_i2c_read(mod_no, val, reg, 1);
  }
 
 

Hmm. This had me confused for a few mins given word rights in i2c are guaranteed to be little endian unless the swapped versions are used. I see that the twl driver is using a generic i2c bulk write. Ah well, I can see that simplifies the regmap i2c code and works in most cases...
+static inline int twl_i2c_write_u16(u8 mod_no, u16 val, u8 reg) {

Quoted 15 lines
include/linux/i2c/twl.h +11 -0
+	val = cpu_to_le16(val);
+	return twl_i2c_write(mod_no, (u8*) &val, reg, 2);
+}
+
+static inline int twl_i2c_read_u16(u8 mod_no, u16 *val, u8 reg) {
+	int ret;
+	ret = twl_i2c_read(mod_no, (u8*) val, reg, 2);
+	*val = le16_to_cpu(*val);
+	return ret;
+}
+
  int twl_get_type(void);
  int twl_get_version(void);
  int twl_get_hfclk_rate(void);
#47 lore raw

RFC v4 5/7 mfd: twl4030-madc: Use twl_i2c_read/write_u16 for 16 bit registers

In reply to Sebastian Reichel

Simplify reading and writing of 16 bit TWL registers in the driver by using twl_i2c_read_u16 and twl_i2c_write_u16.

---
 drivers/mfd/twl4030-madc.c | 39 ++++++++-------------------------------
 1 file changed, 8 insertions(+), 31 deletions(-)
drivers/mfd/twl4030-madc.c +8 -31
index c90013e..d918670 100644
@@ -206,25 +206,19 @@ const struct twl4030_madc_conversion_method twl4030_conversion_methods[] = {
  */
 static int twl4030_madc_channel_raw_read(struct twl4030_madc_data *madc, u8 reg)
 {
-	u8 msb, lsb;
+	u16 val;
 	int ret;
 	/*
 	 * For each ADC channel, we have MSB and LSB register pair. MSB address
 	 * is always LSB address+1. reg parameter is the address of LSB register
 	 */
-	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &msb, reg + 1);
+	ret = twl_i2c_read_u16(TWL4030_MODULE_MADC, &val, reg);
 	if (ret) {
-		dev_err(madc->dev, "unable to read MSB register 0x%X\n",
-			reg + 1);
-		return ret;
-	}
-	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &lsb, reg);
-	if (ret) {
-		dev_err(madc->dev, "unable to read LSB register 0x%X\n", reg);
+		dev_err(madc->dev, "unable to read register 0x%X\n", reg);
 		return ret;
 	}
 
-	return (int)(((msb << 8) | lsb) >> 6);
+	return (int)(val >> 6);
 }
 
 /*
@@ -573,7 +567,6 @@ static int twl4030_madc_wait_conversion_ready(struct twl4030_madc_data *madc,
 int twl4030_madc_conversion(struct twl4030_madc_request *req)
 {
 	const struct twl4030_madc_conversion_method *method;
-	u8 ch_msb, ch_lsb;
 	int ret;
 
 	if (!req || !twl4030_madc)
@@ -589,37 +582,21 @@ int twl4030_madc_conversion(struct twl4030_madc_request *req)
 		ret = -EBUSY;
 		goto out;
 	}
-	ch_msb = (req->channels >> 8) & 0xff;
-	ch_lsb = req->channels & 0xff;
 	method = &twl4030_conversion_methods[req->method];
 	/* Select channels to be converted */
-	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_msb, method->sel + 1);
+	ret = twl_i2c_write_u16(TWL4030_MODULE_MADC, req->channels, method->sel);
 	if (ret) {
 		dev_err(twl4030_madc->dev,
-			"unable to write sel register 0x%X\n", method->sel + 1);
-		goto out;
-	}
-	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_lsb, method->sel);
-	if (ret) {
-		dev_err(twl4030_madc->dev,
-			"unable to write sel register 0x%X\n", method->sel + 1);
+			"unable to write sel register 0x%X\n", method->sel);
 		goto out;
 	}
 	/* Select averaging for all channels if do_avg is set */
 	if (req->do_avg) {
-		ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
-				       ch_msb, method->avg + 1);
+		ret = twl_i2c_write_u16(TWL4030_MODULE_MADC, req->channels,
+				       method->avg);
 		if (ret) {
 			dev_err(twl4030_madc->dev,
 				"unable to write avg register 0x%X\n",
-				method->avg + 1);
-			goto out;
-		}
-		ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
-				       ch_lsb, method->avg);
-		if (ret) {
-			dev_err(twl4030_madc->dev,
-				"unable to write avg reg 0x%X\n",
 				method->avg);
 			goto out;
 		}
Signature · 1 line
1.8.5.3
maintainer Acked-by on v4 5/7
#48 lore raw

Re: [RFCv4 5/7] mfd: twl4030-madc: Use twl_i2c_read/write_u16 for 16 bit registers

Quoted 5 lines · Sebastian Reichel

Simplify reading and writing of 16 bit TWL registers in the driver by using twl_i2c_read_u16 and twl_i2c_write_u16.

Nice

Quoted 89 lines
---
  drivers/mfd/twl4030-madc.c | 39 ++++++++-------------------------------
  1 file changed, 8 insertions(+), 31 deletions(-)
drivers/mfd/twl4030-madc.c +8 -31
index c90013e..d918670 100644
@@ -206,25 +206,19 @@ const struct twl4030_madc_conversion_method twl4030_conversion_methods[] = {
   */
  static int twl4030_madc_channel_raw_read(struct twl4030_madc_data *madc, u8 reg)
  {
-	u8 msb, lsb;
+	u16 val;
  	int ret;
  	/*
  	 * For each ADC channel, we have MSB and LSB register pair. MSB address
  	 * is always LSB address+1. reg parameter is the address of LSB register
  	 */
-	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &msb, reg + 1);
+	ret = twl_i2c_read_u16(TWL4030_MODULE_MADC, &val, reg);
  	if (ret) {
-		dev_err(madc->dev, "unable to read MSB register 0x%X\n",
-			reg + 1);
-		return ret;
-	}
-	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &lsb, reg);
-	if (ret) {
-		dev_err(madc->dev, "unable to read LSB register 0x%X\n", reg);
+		dev_err(madc->dev, "unable to read register 0x%X\n", reg);
  		return ret;
  	}
 
-	return (int)(((msb << 8) | lsb) >> 6);
+	return (int)(val >> 6);
  }
 
  /*
@@ -573,7 +567,6 @@ static int twl4030_madc_wait_conversion_ready(struct twl4030_madc_data *madc,
  int twl4030_madc_conversion(struct twl4030_madc_request *req)
  {
  	const struct twl4030_madc_conversion_method *method;
-	u8 ch_msb, ch_lsb;
  	int ret;
 
  	if (!req || !twl4030_madc)
@@ -589,37 +582,21 @@ int twl4030_madc_conversion(struct twl4030_madc_request *req)
  		ret = -EBUSY;
  		goto out;
  	}
-	ch_msb = (req->channels >> 8) & 0xff;
-	ch_lsb = req->channels & 0xff;
  	method = &twl4030_conversion_methods[req->method];
  	/* Select channels to be converted */
-	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_msb, method->sel + 1);
+	ret = twl_i2c_write_u16(TWL4030_MODULE_MADC, req->channels, method->sel);
  	if (ret) {
  		dev_err(twl4030_madc->dev,
-			"unable to write sel register 0x%X\n", method->sel + 1);
-		goto out;
-	}
-	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_lsb, method->sel);
-	if (ret) {
-		dev_err(twl4030_madc->dev,
-			"unable to write sel register 0x%X\n", method->sel + 1);
+			"unable to write sel register 0x%X\n", method->sel);
  		goto out;
  	}
  	/* Select averaging for all channels if do_avg is set */
  	if (req->do_avg) {
-		ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
-				       ch_msb, method->avg + 1);
+		ret = twl_i2c_write_u16(TWL4030_MODULE_MADC, req->channels,
+				       method->avg);
  		if (ret) {
  			dev_err(twl4030_madc->dev,
  				"unable to write avg register 0x%X\n",
-				method->avg + 1);
-			goto out;
-		}
-		ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
-				       ch_lsb, method->avg);
-		if (ret) {
-			dev_err(twl4030_madc->dev,
-				"unable to write avg reg 0x%X\n",
  				method->avg);
  			goto out;
  		}
#49 lore raw

RFC v4 6/7 Documentation: DT: Document twl4030-madc binding

In reply to Sebastian Reichel

Add devicetree binding documentation for twl4030-madc analog digital converter.

---
 .../devicetree/bindings/iio/adc/twl4030-madc.txt   | 24 ++++++++++++++++++++++
 1 file changed, 24 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/adc/twl4030-madc.txt
Documentation/devicetree/bindings/iio/adc/twl4030-madc.txt new file +24 -0
new file mode 100644
index 0000000..6bdd214
@@ -0,0 +1,24 @@
+* TWL4030 Monitoring Analog to Digital Converter (MADC)
+
+The MADC subsystem in the TWL4030 consists of a 10-bit ADC
+combined with a 16-input analog multiplexer.
+
+Required properties:
+  - compatible: Should contain "ti,twl4030-madc".
+  - interrupts: IRQ line for the MADC submodule.
+  - #io-channel-cells: Should be set to <1>.
+
+Optional properties:
+  - ti,system-uses-second-madc-irq: boolean, set if the second madc irq register
+				    should be used, which is intended to be used
+				    by Co-Processors (e.g. a modem).
+
+Example:
+
+&twl {
+	madc {
+		compatible = "ti,twl4030-madc";
+		interrupts = <3>;
+		#io-channel-cells = <1>;
+	};
+};
Signature · 1 line
1.8.5.3
maintainer
#50 lore raw

Re: [RFCv4 6/7] Documentation: DT: Document twl4030-madc binding

Quoted 4 lines · Sebastian Reichel

Add devicetree binding documentation for twl4030-madc analog digital converter.

Looks fine to me, but as ever needs a device tree maintainer ack or 3 weeks and someone else to take a view on it.

Quoted 36 lines
---
  .../devicetree/bindings/iio/adc/twl4030-madc.txt   | 24 ++++++++++++++++++++++
  1 file changed, 24 insertions(+)

create mode 100644 Documentation/devicetree/bindings/iio/adc/twl4030-madc.txt

Documentation/devicetree/bindings/iio/adc/twl4030-madc.txt new file +24 -0
new file mode 100644
index 0000000..6bdd214
@@ -0,0 +1,24 @@
+* TWL4030 Monitoring Analog to Digital Converter (MADC)
+
+The MADC subsystem in the TWL4030 consists of a 10-bit ADC
+combined with a 16-input analog multiplexer.
+
+Required properties:
+  - compatible: Should contain "ti,twl4030-madc".
+  - interrupts: IRQ line for the MADC submodule.
+  - #io-channel-cells: Should be set to <1>.
+
+Optional properties:
+  - ti,system-uses-second-madc-irq: boolean, set if the second madc irq register
+				    should be used, which is intended to be used
+				    by Co-Processors (e.g. a modem).
+
+Example:
+
+&twl {
+	madc {
+		compatible = "ti,twl4030-madc";
+		interrupts = <3>;
+		#io-channel-cells = <1>;
+	};
+};
#51 lore raw

RFC v4 7/7 mfd: twl4030-madc: Move driver to drivers/iio/adc

In reply to Sebastian Reichel

This is a driver for an A/D converter, which belongs into drivers/iio/adc.

---
 drivers/iio/adc/Kconfig                 | 10 ++++++++++
 drivers/iio/adc/Makefile                |  1 +
 drivers/{mfd => iio/adc}/twl4030-madc.c |  0
 drivers/mfd/Kconfig                     | 10 ----------
 drivers/mfd/Makefile                    |  1 -
 5 files changed, 11 insertions(+), 11 deletions(-)
 rename drivers/{mfd => iio/adc}/twl4030-madc.c (100%)
drivers/iio/adc/Kconfig +10 -0
index 2209f28..427f75c 100644
@@ -183,6 +183,16 @@ config TI_AM335X_ADC
 	  Say yes here to build support for Texas Instruments ADC
 	  driver which is also a MFD client.
 
+config TWL4030_MADC
+	tristate "TWL4030 MADC (Monitoring A/D Converter)"
+	depends on TWL4030_CORE
+	help
+	This driver provides support for Triton TWL4030-MADC. The
+	driver supports both RT and SW conversion methods.
+
+	This driver can also be built as a module. If so, the module will be
+	called twl4030-madc.
+
 config TWL6030_GPADC
 	tristate "TWL6030 GPADC (General Purpose A/D Converter) Support"
 	depends on TWL4030_CORE
drivers/iio/adc/Makefile +1 -0
index ba9a10a..9acf2df 100644
@@ -20,5 +20,6 @@ obj-$(CONFIG_MCP3422) += mcp3422.o
 obj-$(CONFIG_NAU7802) += nau7802.o
 obj-$(CONFIG_TI_ADC081C) += ti-adc081c.o
 obj-$(CONFIG_TI_AM335X_ADC) += ti_am335x_adc.o
+obj-$(CONFIG_TWL4030_MADC) += twl4030-madc.o
 obj-$(CONFIG_TWL6030_GPADC) += twl6030-gpadc.o
 obj-$(CONFIG_VIPERBOARD_ADC) += viperboard_adc.o
drivers/iio/adc/twl4030-madc.c renamed from drivers/mfd/twl4030-madc.c +0 -0
similarity index 100%
rename from drivers/mfd/twl4030-madc.c
rename to drivers/iio/adc/twl4030-madc.c
drivers/mfd/Kconfig +0 -10
index 49bb445..23a8a51 100644
@@ -935,16 +935,6 @@ config TWL4030_CORE
 	  high speed USB OTG transceiver, an audio codec (on most
 	  versions) and many other features.
 
-config TWL4030_MADC
-	tristate "TI TWL4030 MADC"
-	depends on TWL4030_CORE
-	help
-	This driver provides support for triton TWL4030-MADC. The
-	driver supports both RT and SW conversion methods.
-
-	This driver can be built as a module. If so it will be
-	named twl4030-madc
-
 config TWL4030_POWER
 	bool "TI TWL4030 power resources"
 	depends on TWL4030_CORE && ARM
drivers/mfd/Makefile +0 -1
index 5aea5ef..c8eb0bc 100644
@@ -71,7 +71,6 @@ obj-$(CONFIG_MFD_TPS80031)	+= tps80031.o
 obj-$(CONFIG_MENELAUS)		+= menelaus.o
 
 obj-$(CONFIG_TWL4030_CORE)	+= twl-core.o twl4030-irq.o twl6030-irq.o
-obj-$(CONFIG_TWL4030_MADC)      += twl4030-madc.o
 obj-$(CONFIG_TWL4030_POWER)    += twl4030-power.o
 obj-$(CONFIG_MFD_TWL4030_AUDIO)	+= twl4030-audio.o
 obj-$(CONFIG_TWL6040_CORE)	+= twl6040.o
Signature · 6 lines
1.8.5.3

--
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 v4 7/7
#52 lore raw

Re: [RFCv4 7/7] mfd: twl4030-madc: Move driver to drivers/iio/adc

Quoted 5 lines · Sebastian Reichel

This is a driver for an A/D converter, which belongs into drivers/iio/adc.

Quoted 79 lines
---
  drivers/iio/adc/Kconfig                 | 10 ++++++++++
  drivers/iio/adc/Makefile                |  1 +
  drivers/{mfd => iio/adc}/twl4030-madc.c |  0
  drivers/mfd/Kconfig                     | 10 ----------
  drivers/mfd/Makefile                    |  1 -
  5 files changed, 11 insertions(+), 11 deletions(-)

rename drivers/{mfd => iio/adc}/twl4030-madc.c (100%)

drivers/iio/adc/Kconfig +10 -0
index 2209f28..427f75c 100644
@@ -183,6 +183,16 @@ config TI_AM335X_ADC
  	  Say yes here to build support for Texas Instruments ADC
  	  driver which is also a MFD client.
 
+config TWL4030_MADC
+	tristate "TWL4030 MADC (Monitoring A/D Converter)"
+	depends on TWL4030_CORE
+	help
+	This driver provides support for Triton TWL4030-MADC. The
+	driver supports both RT and SW conversion methods.
+
+	This driver can also be built as a module. If so, the module will be
+	called twl4030-madc.
+
  config TWL6030_GPADC
  	tristate "TWL6030 GPADC (General Purpose A/D Converter) Support"
  	depends on TWL4030_CORE
drivers/iio/adc/Makefile +1 -0
index ba9a10a..9acf2df 100644
@@ -20,5 +20,6 @@ obj-$(CONFIG_MCP3422) += mcp3422.o
  obj-$(CONFIG_NAU7802) += nau7802.o
  obj-$(CONFIG_TI_ADC081C) += ti-adc081c.o
  obj-$(CONFIG_TI_AM335X_ADC) += ti_am335x_adc.o
+obj-$(CONFIG_TWL4030_MADC) += twl4030-madc.o
  obj-$(CONFIG_TWL6030_GPADC) += twl6030-gpadc.o
  obj-$(CONFIG_VIPERBOARD_ADC) += viperboard_adc.o
drivers/iio/adc/twl4030-madc.c renamed from drivers/mfd/twl4030-madc.c +0 -0
similarity index 100%
rename from drivers/mfd/twl4030-madc.c
rename to drivers/iio/adc/twl4030-madc.c
drivers/mfd/Kconfig +0 -10
index 49bb445..23a8a51 100644
@@ -935,16 +935,6 @@ config TWL4030_CORE
  	  high speed USB OTG transceiver, an audio codec (on most
  	  versions) and many other features.
 
-config TWL4030_MADC
-	tristate "TI TWL4030 MADC"
-	depends on TWL4030_CORE
-	help
-	This driver provides support for triton TWL4030-MADC. The
-	driver supports both RT and SW conversion methods.
-
-	This driver can be built as a module. If so it will be
-	named twl4030-madc
-
  config TWL4030_POWER
  	bool "TI TWL4030 power resources"
  	depends on TWL4030_CORE && ARM
drivers/mfd/Makefile +0 -1
index 5aea5ef..c8eb0bc 100644
@@ -71,7 +71,6 @@ obj-$(CONFIG_MFD_TPS80031)	+= tps80031.o
  obj-$(CONFIG_MENELAUS)		+= menelaus.o
 
  obj-$(CONFIG_TWL4030_CORE)	+= twl-core.o twl4030-irq.o twl6030-irq.o
-obj-$(CONFIG_TWL4030_MADC)      += twl4030-madc.o
  obj-$(CONFIG_TWL4030_POWER)    += twl4030-power.o
  obj-$(CONFIG_MFD_TWL4030_AUDIO)	+= twl4030-audio.o
  obj-$(CONFIG_TWL6040_CORE)	+= twl6040.o
maintainer
#53 lore raw

Re: [RFCv4 0/7] Convert twl4030-madc to IIO API and add DT support

In reply to Sebastian Reichel
Quoted 16 lines · Sebastian Reichel

Hi,

This is RFCv4 for converting twl4030-madc to the IIO API and adding DT support. The patchset compiles and has been tested on my Nokia N900.

Changes since RFCv3 [0]:
* Added Acked-by from Lee Jones (MFD subsystem maintainer) to twl_i2c_read/write_u16 patches
* Fixed style issues found by Lee Jones
This readded the temp variable Jonathan Cameron wanted to be removed, but makes the code cleaner - especially once the brackets for math for clarity are added.
* Tested with converted rx51-battery driver on Nokia N900, which can be found at [1].

Quoted 2 lines

[0] https://lkml.org/lkml/2014/2/25/627
[1] https://lkml.org/lkml/2014/2/25/639

Hi All,

Obviously there are a few outstanding bits and pieces to be addressed.

To get this lined up for a merge...

Lee, do you want to take this through mfd or shall I take it through IIO? I don't mind either way. It stands pretty separate in IIO so shouldn't cause any merge issues beyond minor fuzz.

Jonathan

Quoted 24 lines

-- Sebastian

Sebastian Reichel (7):
mfd: twl4030-madc: Use managed resources
mfd: twl4030-madc: Add DT support and convert to IIO framework
mfd: twl4030-madc: Cleanup driver
mfd: twl-core: Add twl_i2c_read/write_u16
mfd: twl4030-madc: Use twl_i2c_read/write_u16 for 16 bit registers
Documentation: DT: Document twl4030-madc binding
mfd: twl4030-madc: Move driver to drivers/iio/adc

  .../devicetree/bindings/iio/adc/twl4030-madc.txt   |  24 ++
  drivers/iio/adc/Kconfig                            |  10 +
  drivers/iio/adc/Makefile                           |   1 +
  drivers/{mfd => iio/adc}/twl4030-madc.c            | 281 +++++++++++++--------
  drivers/mfd/Kconfig                                |  10 -
  drivers/mfd/Makefile                               |   1 -
  include/linux/i2c/twl.h                            |  12 +
  include/linux/i2c/twl4030-madc.h                   |   2 +-
  8 files changed, 227 insertions(+), 114 deletions(-)

create mode 100644 Documentation/devicetree/bindings/iio/adc/twl4030-madc.txt rename drivers/{mfd => iio/adc}/twl4030-madc.c (77%)

#54 lore raw

Re: [RFCv4 0/7] Convert twl4030-madc to IIO API and add DT support

Hi Jonathan,

Quoted 2 lines · Jonathan Cameron

Obviously there are a few outstanding bits and pieces to be addressed. To get this lined up for a merge...

I will send out a new patchset in a few minutes.

Quoted 3 lines

Lee, do you want to take this through mfd or shall I take it through IIO? I don't mind either way. It stands pretty separate in IIO so shouldn't cause any merge issues beyond minor fuzz.

I think it makes sense to queue it via IIO once I add the ABI documentation for the IIO *_mean_raw sysfs entries. Also I think it makes sense to add iio_read_channel_average_raw() in this patchset, since its needed to convert users of the old madc API to the IIO API.

-- Sebastian

#55 lore raw

Re: [RFCv4 0/7] Convert twl4030-madc to IIO API and add DT support

In reply to Jonathan Cameron
Quoted 22 lines · Jonathan Cameron
Quoted 16 lines · Sebastian Reichel

Hi,

This is RFCv4 for converting twl4030-madc to the IIO API and adding DT support. The patchset compiles and has been tested on my Nokia N900.

Changes since RFCv3 [0]:
* Added Acked-by from Lee Jones (MFD subsystem maintainer) to twl_i2c_read/write_u16 patches
* Fixed style issues found by Lee Jones
This readded the temp variable Jonathan Cameron wanted to be removed, but makes the code cleaner - especially once the brackets for math for clarity are added.
* Tested with converted rx51-battery driver on Nokia N900, which can be found at [1].

Quoted 2 lines

[0] https://lkml.org/lkml/2014/2/25/627
[1] https://lkml.org/lkml/2014/2/25/639

Hi All,

Quoted 7 lines

Obviously there are a few outstanding bits and pieces to be addressed.

To get this lined up for a merge...

Lee, do you want to take this through mfd or shall I take it through IIO? I don't mind either way. It stands pretty separate in IIO so shouldn't cause any merge issues beyond minor fuzz.

I'm happy to take it through MFD with your Ack.

Signature · 4 lines
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
#56 lore raw

cover Convert twl4030-madc to IIO API and add DT support

In reply to Sebastian Reichel

Hi,

This is PATCHv1 for converting twl4030-madc to the IIO API and adding DT support. The plan is to remove the private twl4030-madc API once all users have been removed or converted to the IIO API. The patchset compiles and has been tested on my Nokia N900.

Changes since RFCv4 [0]:
* Fixup all stuff found by Lee Jones and Jonathan Cameron in RFCv4.
- Fixup kerneldoc comments
- Move ARRAY_SIZE() into correct patch. I accidently rebased it into the wrong one.
- Use sizeof(*madc) instead of sizeof(struct twl4030_madc_data)
- Only call of_property_read_bool() if there is no pdata
- I left the brackets in "reg_base + (2 * i)". They have been requested by Lee Jones and I think they make it easier to understand what is going on.
* Add a bunch of Acked-By statements from those two
* Add new patch, which adds *_mean_raw sysfs ABI documentation as requested by Jonathan Cameron
* Add another patch, which provides iio_read_channel_average_raw(), so that drivers using the old API can be converted to the IIO API.
* I switched from RFC to PATCH prefix.

TODO:
* Wait for feedback from DT binding maintainers
* Wait for feedback from Marek Belisko, who is testing the change with twl4030-madc-battery driver on GTA04

[0] https://lkml.org/lkml/2014/2/26/482

-- Sebastian

Sebastian Reichel (9):
mfd: twl4030-madc: Use managed resources
mfd: twl4030-madc: Add DT support and convert to IIO framework
mfd: twl4030-madc: Cleanup driver
mfd: twl-core: Add twl_i2c_read/write_u16
mfd: twl4030-madc: Use twl_i2c_read/write_u16 for 16 bit registers
Documentation: DT: Document twl4030-madc binding
mfd: twl4030-madc: Move driver to drivers/iio/adc
iio: documentation: Add ABI documentation for *_mean_raw
iio: inkern: add iio_read_channel_average_raw

 Documentation/ABI/testing/sysfs-bus-iio            |   8 +
 .../devicetree/bindings/iio/adc/twl4030-madc.txt   |  24 ++
 drivers/iio/adc/Kconfig                            |  10 +
 drivers/iio/adc/Makefile                           |   1 +
 drivers/{mfd => iio/adc}/twl4030-madc.c            | 301 +++++++++++++--------
 drivers/iio/inkern.c                               |  18 ++
 drivers/mfd/Kconfig                                |  10 -
 drivers/mfd/Makefile                               |   1 -
 include/linux/i2c/twl.h                            |  12 +
 include/linux/i2c/twl4030-madc.h                   |   2 +-
 include/linux/iio/consumer.h                       |  13 +
 11 files changed, 276 insertions(+), 124 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/iio/adc/twl4030-madc.txt
 rename drivers/{mfd => iio/adc}/twl4030-madc.c (75%)
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
#57 lore raw

PATCH 1/9 mfd: twl4030-madc: Use managed resources

Update twl4030-madc driver to use managed resources.

---
 drivers/mfd/twl4030-madc.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
drivers/mfd/twl4030-madc.c +6 -9
index 4c583e4..5458561 100644
@@ -702,14 +702,14 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 {
 	struct twl4030_madc_data *madc;
 	struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev);
-	int ret;
+	int irq, ret;
 	u8 regval;
 
 	if (!pdata) {
 		dev_err(&pdev->dev, "platform_data not available\n");
 		return -EINVAL;
 	}
-	madc = kzalloc(sizeof(*madc), GFP_KERNEL);
+	madc = devm_kzalloc(&pdev->dev, sizeof(*madc), GFP_KERNEL);
 	if (!madc)
 		return -ENOMEM;
 
@@ -726,7 +726,7 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 	    TWL4030_MADC_ISR1 : TWL4030_MADC_ISR2;
 	ret = twl4030_madc_set_power(madc, 1);
 	if (ret < 0)
-		goto err_power;
+		return ret;
 	ret = twl4030_madc_set_current_generator(madc, 0, 1);
 	if (ret < 0)
 		goto err_current_generator;
@@ -770,7 +770,9 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 
 	platform_set_drvdata(pdev, madc);
 	mutex_init(&madc->lock);
-	ret = request_threaded_irq(platform_get_irq(pdev, 0), NULL,
+
+	irq = platform_get_irq(pdev, 0);
+	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
 				   twl4030_madc_threaded_irq_handler,
 				   IRQF_TRIGGER_RISING, "twl4030_madc", madc);
 	if (ret) {
@@ -783,9 +785,6 @@ err_i2c:
 	twl4030_madc_set_current_generator(madc, 0, 0);
 err_current_generator:
 	twl4030_madc_set_power(madc, 0);
-err_power:
-	kfree(madc);
-
 	return ret;
 }
 
@@ -793,10 +792,8 @@ static int twl4030_madc_remove(struct platform_device *pdev)
 {
 	struct twl4030_madc_data *madc = platform_get_drvdata(pdev);
 
-	free_irq(platform_get_irq(pdev, 0), madc);
 	twl4030_madc_set_current_generator(madc, 0, 0);
 	twl4030_madc_set_power(madc, 0);
-	kfree(madc);
 
 	return 0;
 }
Signature · 1 line
1.9.0
#58 lore raw

PATCH 2/9 mfd: twl4030-madc: Add DT support and convert to IIO framework

In reply to Sebastian Reichel

This converts twl4030-madc module to use the Industrial IO ADC framework and adds device tree support.

---
 drivers/mfd/twl4030-madc.c | 128 +++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 118 insertions(+), 10 deletions(-)
drivers/mfd/twl4030-madc.c +118 -10
index 5458561..8d09abb 100644
@@ -47,11 +47,14 @@
 #include <linux/gfp.h>
 #include <linux/err.h>
 
+#include <linux/iio/iio.h>
+
 /*
  * struct twl4030_madc_data - a container for madc info
  * @dev - pointer to device structure for madc
  * @lock - mutex protecting this data structure
  * @requests - Array of request struct corresponding to SW1, SW2 and RT
+ * @use_second_irq - IRQ selection (main or co-processor)
  * @imr - Interrupt mask register of MADC
  * @isr - Interrupt status register of MADC
  */
@@ -59,10 +62,73 @@ struct twl4030_madc_data {
 	struct device *dev;
 	struct mutex lock;	/* mutex protecting this data structure */
 	struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS];
+	bool use_second_irq;
 	int imr;
 	int isr;
 };
 
+static int twl4030_madc_read(struct iio_dev *iio_dev,
+			     const struct iio_chan_spec *chan,
+			     int *val, int *val2, long mask)
+{
+	struct twl4030_madc_data *madc = iio_priv(iio_dev);
+	struct twl4030_madc_request req;
+	int ret;
+
+	req.method = madc->use_second_irq ? TWL4030_MADC_SW2 : TWL4030_MADC_SW1;
+
+	req.channels = BIT(chan->channel);
+	req.active = false;
+	req.func_cb = NULL;
+	req.type = TWL4030_MADC_WAIT;
+	req.raw = !(mask == IIO_CHAN_INFO_PROCESSED);
+	req.do_avg = (mask == IIO_CHAN_INFO_AVERAGE_RAW);
+
+	ret = twl4030_madc_conversion(&req);
+	if (ret < 0)
+		return ret;
+
+	*val = req.rbuf[chan->channel];
+
+	return IIO_VAL_INT;
+}
+
+static const struct iio_info twl4030_madc_iio_info = {
+	.read_raw = &twl4030_madc_read,
+	.driver_module = THIS_MODULE,
+};
+
+#define TWL4030_ADC_CHANNEL(_channel, _type, _name, _mask) {	\
+	.type = _type,					\
+	.channel = _channel,				\
+	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |  \
+			      BIT(IIO_CHAN_INFO_AVERAGE_RAW) | \
+			      _mask,			\
+	.datasheet_name = _name,			\
+	.indexed = 1,					\
+}
+
+static const struct iio_chan_spec twl4030_madc_iio_channels[] = {
+	TWL4030_ADC_CHANNEL(0, IIO_VOLTAGE, "ADCIN0", 0),
+	TWL4030_ADC_CHANNEL(1, IIO_TEMP, "ADCIN1",
+		BIT(IIO_CHAN_INFO_PROCESSED)),
+	TWL4030_ADC_CHANNEL(2, IIO_VOLTAGE, "ADCIN2", 0),
+	TWL4030_ADC_CHANNEL(3, IIO_VOLTAGE, "ADCIN3", 0),
+	TWL4030_ADC_CHANNEL(4, IIO_VOLTAGE, "ADCIN4", 0),
+	TWL4030_ADC_CHANNEL(5, IIO_VOLTAGE, "ADCIN5", 0),
+	TWL4030_ADC_CHANNEL(6, IIO_VOLTAGE, "ADCIN6", 0),
+	TWL4030_ADC_CHANNEL(7, IIO_VOLTAGE, "ADCIN7", 0),
+	TWL4030_ADC_CHANNEL(8, IIO_VOLTAGE, "ADCIN8", 0),
+	TWL4030_ADC_CHANNEL(9, IIO_VOLTAGE, "ADCIN9", 0),
+	TWL4030_ADC_CHANNEL(10, IIO_CURRENT, "ADCIN10",
+		BIT(IIO_CHAN_INFO_PROCESSED)),
+	TWL4030_ADC_CHANNEL(11, IIO_VOLTAGE, "ADCIN11", 0),
+	TWL4030_ADC_CHANNEL(12, IIO_VOLTAGE, "ADCIN12", 0),
+	TWL4030_ADC_CHANNEL(13, IIO_VOLTAGE, "ADCIN13", 0),
+	TWL4030_ADC_CHANNEL(14, IIO_VOLTAGE, "ADCIN14", 0),
+	TWL4030_ADC_CHANNEL(15, IIO_VOLTAGE, "ADCIN15", 0),
+};
+
 static struct twl4030_madc_data *twl4030_madc;
 
 struct twl4030_prescale_divider_ratios {
@@ -702,28 +768,50 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 {
 	struct twl4030_madc_data *madc;
 	struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev);
+	struct device_node *np = pdev->dev.of_node;
 	int irq, ret;
 	u8 regval;
+	struct iio_dev *iio_dev = NULL;
 
-	if (!pdata) {
-		dev_err(&pdev->dev, "platform_data not available\n");
+	if (!pdata && !np) {
+		dev_err(&pdev->dev, "neither platform data nor Device Tree node available\n");
 		return -EINVAL;
 	}
-	madc = devm_kzalloc(&pdev->dev, sizeof(*madc), GFP_KERNEL);
-	if (!madc)
+
+	iio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*madc));
+	if (!iio_dev) {
+		dev_err(&pdev->dev, "failed allocating iio device\n");
 		return -ENOMEM;
+	}
 
+	madc = iio_priv(iio_dev);
 	madc->dev = &pdev->dev;
+	madc->use_second_irq = false;
+
+	iio_dev->name = dev_name(&pdev->dev);
+	iio_dev->dev.parent = &pdev->dev;
+	iio_dev->dev.of_node = pdev->dev.of_node;
+	iio_dev->info = &twl4030_madc_iio_info;
+	iio_dev->modes = INDIO_DIRECT_MODE;
+	iio_dev->channels = twl4030_madc_iio_channels;
+	iio_dev->num_channels = ARRAY_SIZE(twl4030_madc_iio_channels);
 
 	/*
 	 * Phoenix provides 2 interrupt lines. The first one is connected to
 	 * the OMAP. The other one can be connected to the other processor such
 	 * as modem. Hence two separate ISR and IMR registers.
 	 */
-	madc->imr = (pdata->irq_line == 1) ?
-	    TWL4030_MADC_IMR1 : TWL4030_MADC_IMR2;
-	madc->isr = (pdata->irq_line == 1) ?
-	    TWL4030_MADC_ISR1 : TWL4030_MADC_ISR2;
+	if (pdata && pdata->irq_line != 1)
+		madc->use_second_irq = true;
+	else if (!pdata)
+		madc->use_second_irq = of_property_read_bool(np,
+				       "ti,system-uses-second-madc-irq");
+
+	madc->imr = madc->use_second_irq ? TWL4030_MADC_IMR2 :
+					   TWL4030_MADC_IMR1;
+	madc->isr = madc->use_second_irq ? TWL4030_MADC_ISR2 :
+					   TWL4030_MADC_ISR1;
+
 	ret = twl4030_madc_set_power(madc, 1);
 	if (ret < 0)
 		return ret;
@@ -768,7 +856,7 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 		}
 	}
 
-	platform_set_drvdata(pdev, madc);
+	platform_set_drvdata(pdev, iio_dev);
 	mutex_init(&madc->lock);
 
 	irq = platform_get_irq(pdev, 0);
@@ -780,7 +868,15 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 		goto err_i2c;
 	}
 	twl4030_madc = madc;
+
+	ret = iio_device_register(iio_dev);
+	if (ret) {
+		dev_dbg(&pdev->dev, "could not register iio device\n");
+		goto err_i2c;
+	}
+
 	return 0;
+
 err_i2c:
 	twl4030_madc_set_current_generator(madc, 0, 0);
 err_current_generator:
@@ -790,20 +886,32 @@ err_current_generator:
 
 static int twl4030_madc_remove(struct platform_device *pdev)
 {
-	struct twl4030_madc_data *madc = platform_get_drvdata(pdev);
+	struct iio_dev *iio_dev = platform_get_drvdata(pdev);
+	struct twl4030_madc_data *madc = iio_priv(iio_dev);
 
 	twl4030_madc_set_current_generator(madc, 0, 0);
 	twl4030_madc_set_power(madc, 0);
 
+	iio_device_unregister(iio_dev);
+
 	return 0;
 }
 
+#ifdef CONFIG_OF
+static const struct of_device_id twl_madc_of_match[] = {
+	{.compatible = "ti,twl4030-madc", },
+	{ },
+};
+MODULE_DEVICE_TABLE(of, twl_madc_of_match);
+#endif
+
 static struct platform_driver twl4030_madc_driver = {
 	.probe = twl4030_madc_probe,
 	.remove = twl4030_madc_remove,
 	.driver = {
 		   .name = "twl4030_madc",
 		   .owner = THIS_MODULE,
+		   .of_match_table = of_match_ptr(twl_madc_of_match),
 		   },
 };
 
Signature · 1 line
1.9.0
#59 lore raw

PATCH 3/9 mfd: twl4030-madc: Cleanup driver

In reply to Sebastian Reichel

Some style fixes in twl4030-madc driver.

---
 drivers/mfd/twl4030-madc.c       | 125 +++++++++++++++++++--------------------
 include/linux/i2c/twl4030-madc.h |   2 +-
 2 files changed, 61 insertions(+), 66 deletions(-)
drivers/mfd/twl4030-madc.c +60 -65
index 8d09abb..35514e0 100644
@@ -49,22 +49,22 @@
 
 #include <linux/iio/iio.h>
 
-/*
+/**
  * struct twl4030_madc_data - a container for madc info
- * @dev - pointer to device structure for madc
- * @lock - mutex protecting this data structure
- * @requests - Array of request struct corresponding to SW1, SW2 and RT
- * @use_second_irq - IRQ selection (main or co-processor)
- * @imr - Interrupt mask register of MADC
- * @isr - Interrupt status register of MADC
+ * @dev:		Pointer to device structure for madc
+ * @lock:		Mutex protecting this data structure
+ * @requests:		Array of request struct corresponding to SW1, SW2 and RT
+ * @use_second_irq:	IRQ selection (main or co-processor)
+ * @imr:		Interrupt mask register of MADC
+ * @isr:		Interrupt status register of MADC
  */
 struct twl4030_madc_data {
 	struct device *dev;
 	struct mutex lock;	/* mutex protecting this data structure */
 	struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS];
 	bool use_second_irq;
-	int imr;
-	int isr;
+	u8 imr;
+	u8 isr;
 };
 
 static int twl4030_madc_read(struct iio_dev *iio_dev,
@@ -157,17 +157,16 @@ twl4030_divider_ratios[16] = {
 };
 
 
-/*
- * Conversion table from -3 to 55 degree Celcius
- */
-static int therm_tbl[] = {
-30800,	29500,	28300,	27100,
-26000,	24900,	23900,	22900,	22000,	21100,	20300,	19400,	18700,	17900,
-17200,	16500,	15900,	15300,	14700,	14100,	13600,	13100,	12600,	12100,
-11600,	11200,	10800,	10400,	10000,	9630,	9280,	8950,	8620,	8310,
-8020,	7730,	7460,	7200,	6950,	6710,	6470,	6250,	6040,	5830,
-5640,	5450,	5260,	5090,	4920,	4760,	4600,	4450,	4310,	4170,
-4040,	3910,	3790,	3670,	3550
+/* Conversion table from -3 to 55 degrees Celcius */
+static int twl4030_therm_tbl[] = {
+	30800,	29500,	28300,	27100,
+	26000,	24900,	23900,	22900,	22000,	21100,	20300,	19400,	18700,
+	17900,	17200,	16500,	15900,	15300,	14700,	14100,	13600,	13100,
+	12600,	12100,	11600,	11200,	10800,	10400,	10000,	9630,	9280,
+	8950,	8620,	8310,	8020,	7730,	7460,	7200,	6950,	6710,
+	6470,	6250,	6040,	5830,	5640,	5450,	5260,	5090,	4920,
+	4760,	4600,	4450,	4310,	4170,	4040,	3910,	3790,	3670,
+	3550
 };
 
 /*
@@ -199,11 +198,12 @@ const struct twl4030_madc_conversion_method twl4030_conversion_methods[] = {
 			      },
 };
 
-/*
- * Function to read a particular channel value.
- * @madc - pointer to struct twl4030_madc_data
- * @reg - lsb of ADC Channel
- * If the i2c read fails it returns an error else returns 0.
+/**
+ * twl4030_madc_channel_raw_read() - Function to read a particular channel value
+ * @madc:	pointer to struct twl4030_madc_data
+ * @reg:	lsb of ADC Channel
+ *
+ * Return: 0 on success, an error code otherwise.
  */
 static int twl4030_madc_channel_raw_read(struct twl4030_madc_data *madc, u8 reg)
 {
@@ -229,7 +229,7 @@ static int twl4030_madc_channel_raw_read(struct twl4030_madc_data *madc, u8 reg)
 }
 
 /*
- * Return battery temperature
+ * Return battery temperature in degrees Celsius
  * Or < 0 on failure.
  */
 static int twl4030battery_temperature(int raw_volt)
@@ -238,18 +238,18 @@ static int twl4030battery_temperature(int raw_volt)
 	int temp, curr, volt, res, ret;
 
 	volt = (raw_volt * TEMP_STEP_SIZE) / TEMP_PSR_R;
-	/* Getting and calculating the supply current in micro ampers */
+	/* Getting and calculating the supply current in micro amperes */
 	ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val,
 		REG_BCICTL2);
 	if (ret < 0)
 		return ret;
+
 	curr = ((val & TWL4030_BCI_ITHEN) + 1) * 10;
 	/* Getting and calculating the thermistor resistance in ohms */
 	res = volt * 1000 / curr;
 	/* calculating temperature */
 	for (temp = 58; temp >= 0; temp--) {
-		int actual = therm_tbl[temp];
-
+		int actual = twl4030_therm_tbl[temp];
 		if ((actual - res) >= 0)
 			break;
 	}
@@ -271,11 +271,12 @@ static int twl4030battery_current(int raw_volt)
 	else /* slope of 0.88 mV/mA */
 		return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R2;
 }
+
 /*
  * Function to read channel values
  * @madc - pointer to twl4030_madc_data struct
  * @reg_base - Base address of the first channel
- * @Channels - 16 bit bitmap. If the bit is set, channel value is read
+ * @Channels - 16 bit bitmap. If the bit is set, channel's value is read
  * @buf - The channel values are stored here. if read fails error
  * @raw - Return raw values without conversion
  * value is stored
@@ -286,17 +287,17 @@ static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
 				      long channels, int *buf,
 				      bool raw)
 {
-	int count = 0, count_req = 0, i;
+	int count = 0;
+	int i;
 	u8 reg;
 
 	for_each_set_bit(i, &channels, TWL4030_MADC_MAX_CHANNELS) {
-		reg = reg_base + 2 * i;
+		u8 reg = reg_base + (2 * i);
 		buf[i] = twl4030_madc_channel_raw_read(madc, reg);
 		if (buf[i] < 0) {
-			dev_err(madc->dev,
-				"Unable to read register 0x%X\n", reg);
-			count_req++;
-			continue;
+			dev_err(madc->dev, "Unable to read register 0x%X\n",
+				reg);
+			return buf[i];
 		}
 		if (raw) {
 			count++;
@@ -307,7 +308,7 @@ static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
 			buf[i] = twl4030battery_current(buf[i]);
 			if (buf[i] < 0) {
 				dev_err(madc->dev, "err reading current\n");
-				count_req++;
+				return buf[i];
 			} else {
 				count++;
 				buf[i] = buf[i] - 750;
@@ -317,7 +318,7 @@ static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
 			buf[i] = twl4030battery_temperature(buf[i]);
 			if (buf[i] < 0) {
 				dev_err(madc->dev, "err reading temperature\n");
-				count_req++;
+				return buf[i];
 			} else {
 				buf[i] -= 3;
 				count++;
@@ -338,8 +339,6 @@ static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
 				twl4030_divider_ratios[i].numerator);
 		}
 	}
-	if (count_req)
-		dev_err(madc->dev, "%d channel conversion failed\n", count_req);
 
 	return count;
 }
@@ -363,13 +362,13 @@ static int twl4030_madc_enable_irq(struct twl4030_madc_data *madc, u8 id)
 			madc->imr);
 		return ret;
 	}
+
 	val &= ~(1 << id);
 	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr);
 	if (ret) {
 		dev_err(madc->dev,
 			"unable to write imr register 0x%X\n", madc->imr);
 		return ret;
-
 	}
 
 	return 0;
@@ -432,7 +431,7 @@ static irqreturn_t twl4030_madc_threaded_irq_handler(int irq, void *_madc)
 			continue;
 		ret = twl4030_madc_disable_irq(madc, i);
 		if (ret < 0)
-			dev_dbg(madc->dev, "Disable interrupt failed%d\n", i);
+			dev_dbg(madc->dev, "Disable interrupt failed %d\n", i);
 		madc->requests[i].result_pending = 1;
 	}
 	for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
@@ -514,21 +513,17 @@ static int twl4030_madc_start_conversion(struct twl4030_madc_data *madc,
 {
 	const struct twl4030_madc_conversion_method *method;
 	int ret = 0;
+
+	if (conv_method != TWL4030_MADC_SW1 && conv_method != TWL4030_MADC_SW2)
+		return -ENOTSUPP;
+
 	method = &twl4030_conversion_methods[conv_method];
-	switch (conv_method) {
-	case TWL4030_MADC_SW1:
-	case TWL4030_MADC_SW2:
-		ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
-				       TWL4030_MADC_SW_START, method->ctrl);
-		if (ret) {
-			dev_err(madc->dev,
-				"unable to write ctrl register 0x%X\n",
-				method->ctrl);
-			return ret;
-		}
-		break;
-	default:
-		break;
+	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, TWL4030_MADC_SW_START,
+			       method->ctrl);
+	if (ret) {
+		dev_err(madc->dev, "unable to write ctrl register 0x%X\n",
+			method->ctrl);
+		return ret;
 	}
 
 	return 0;
@@ -625,8 +620,8 @@ int twl4030_madc_conversion(struct twl4030_madc_request *req)
 				       ch_lsb, method->avg);
 		if (ret) {
 			dev_err(twl4030_madc->dev,
-				"unable to write sel reg 0x%X\n",
-				method->sel + 1);
+				"unable to write avg reg 0x%X\n",
+				method->avg);
 			goto out;
 		}
 	}
@@ -667,10 +662,6 @@ out:
 }
 EXPORT_SYMBOL_GPL(twl4030_madc_conversion);
 
-/*
- * Return channel value
- * Or < 0 on failure.
- */
 int twl4030_get_madc_conversion(int channel_no)
 {
 	struct twl4030_madc_request req;
@@ -705,6 +696,7 @@ static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc,
 					      int chan, int on)
 {
 	int ret;
+	int regmask;
 	u8 regval;
 
 	ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE,
@@ -714,10 +706,13 @@ static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc,
 			TWL4030_BCI_BCICTL1);
 		return ret;
 	}
+
+	regmask = chan ? TWL4030_BCI_ITHEN : TWL4030_BCI_TYPEN;
 	if (on)
-		regval |= chan ? TWL4030_BCI_ITHEN : TWL4030_BCI_TYPEN;
+		regval |= regmask;
 	else
-		regval &= chan ? ~TWL4030_BCI_ITHEN : ~TWL4030_BCI_TYPEN;
+		regval &= ~regmask;
+
 	ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
 			       regval, TWL4030_BCI_BCICTL1);
 	if (ret) {
@@ -732,7 +727,7 @@ static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc,
 /*
  * Function that sets MADC software power on bit to enable MADC
  * @madc - pointer to twl4030_madc_data struct
- * @on - Enable or disable MADC software powen on bit.
+ * @on - Enable or disable MADC software power on bit.
  * returns error if i2c read/write fails else 0
  */
 static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on)
include/linux/i2c/twl4030-madc.h +1 -1
index 01f5951..1c0134d 100644
@@ -44,7 +44,7 @@ struct twl4030_madc_conversion_method {
 
 struct twl4030_madc_request {
 	unsigned long channels;
-	u16 do_avg;
+	bool do_avg;
 	u16 method;
 	u16 type;
 	bool active;
Signature · 1 line
1.9.0
#60 lore raw

PATCH 4/9 mfd: twl-core: Add twl_i2c_read/write_u16

In reply to Sebastian Reichel

Add a simple twl_i2c_read/write_u16 wrapper over the twl_i2c_read/write, which is similar to the twl_i2c_read/write_u8 wrapper.

---
 include/linux/i2c/twl.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)
include/linux/i2c/twl.h +12 -0
index ade1c06..d2b1670 100644
@@ -195,6 +195,18 @@ static inline int twl_i2c_read_u8(u8 mod_no, u8 *val, u8 reg) {
 	return twl_i2c_read(mod_no, val, reg, 1);
 }
 
+static inline int twl_i2c_write_u16(u8 mod_no, u16 val, u8 reg) {
+	val = cpu_to_le16(val);
+	return twl_i2c_write(mod_no, (u8*) &val, reg, 2);
+}
+
+static inline int twl_i2c_read_u16(u8 mod_no, u16 *val, u8 reg) {
+	int ret;
+	ret = twl_i2c_read(mod_no, (u8*) val, reg, 2);
+	*val = le16_to_cpu(*val);
+	return ret;
+}
+
 int twl_get_type(void);
 int twl_get_version(void);
 int twl_get_hfclk_rate(void);
Signature · 1 line
1.9.0
#61 lore raw

PATCH 5/9 mfd: twl4030-madc: Use twl_i2c_read/write_u16 for 16 bit registers

In reply to Sebastian Reichel

Simplify reading and writing of 16 bit TWL registers in the driver by using twl_i2c_read_u16 and twl_i2c_write_u16.

---
 drivers/mfd/twl4030-madc.c | 39 ++++++++-------------------------------
 1 file changed, 8 insertions(+), 31 deletions(-)
drivers/mfd/twl4030-madc.c +8 -31
index 35514e0..abfc3e6 100644
@@ -207,25 +207,19 @@ const struct twl4030_madc_conversion_method twl4030_conversion_methods[] = {
  */
 static int twl4030_madc_channel_raw_read(struct twl4030_madc_data *madc, u8 reg)
 {
-	u8 msb, lsb;
+	u16 val;
 	int ret;
 	/*
 	 * For each ADC channel, we have MSB and LSB register pair. MSB address
 	 * is always LSB address+1. reg parameter is the address of LSB register
 	 */
-	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &msb, reg + 1);
+	ret = twl_i2c_read_u16(TWL4030_MODULE_MADC, &val, reg);
 	if (ret) {
-		dev_err(madc->dev, "unable to read MSB register 0x%X\n",
-			reg + 1);
-		return ret;
-	}
-	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &lsb, reg);
-	if (ret) {
-		dev_err(madc->dev, "unable to read LSB register 0x%X\n", reg);
+		dev_err(madc->dev, "unable to read register 0x%X\n", reg);
 		return ret;
 	}
 
-	return (int)(((msb << 8) | lsb) >> 6);
+	return (int)(val >> 6);
 }
 
 /*
@@ -574,7 +568,6 @@ static int twl4030_madc_wait_conversion_ready(struct twl4030_madc_data *madc,
 int twl4030_madc_conversion(struct twl4030_madc_request *req)
 {
 	const struct twl4030_madc_conversion_method *method;
-	u8 ch_msb, ch_lsb;
 	int ret;
 
 	if (!req || !twl4030_madc)
@@ -590,37 +583,21 @@ int twl4030_madc_conversion(struct twl4030_madc_request *req)
 		ret = -EBUSY;
 		goto out;
 	}
-	ch_msb = (req->channels >> 8) & 0xff;
-	ch_lsb = req->channels & 0xff;
 	method = &twl4030_conversion_methods[req->method];
 	/* Select channels to be converted */
-	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_msb, method->sel + 1);
+	ret = twl_i2c_write_u16(TWL4030_MODULE_MADC, req->channels, method->sel);
 	if (ret) {
 		dev_err(twl4030_madc->dev,
-			"unable to write sel register 0x%X\n", method->sel + 1);
-		goto out;
-	}
-	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_lsb, method->sel);
-	if (ret) {
-		dev_err(twl4030_madc->dev,
-			"unable to write sel register 0x%X\n", method->sel + 1);
+			"unable to write sel register 0x%X\n", method->sel);
 		goto out;
 	}
 	/* Select averaging for all channels if do_avg is set */
 	if (req->do_avg) {
-		ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
-				       ch_msb, method->avg + 1);
+		ret = twl_i2c_write_u16(TWL4030_MODULE_MADC, req->channels,
+				       method->avg);
 		if (ret) {
 			dev_err(twl4030_madc->dev,
 				"unable to write avg register 0x%X\n",
-				method->avg + 1);
-			goto out;
-		}
-		ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
-				       ch_lsb, method->avg);
-		if (ret) {
-			dev_err(twl4030_madc->dev,
-				"unable to write avg reg 0x%X\n",
 				method->avg);
 			goto out;
 		}
Signature · 1 line
1.9.0
#62 lore raw

PATCH 6/9 Documentation: DT: Document twl4030-madc binding

In reply to Sebastian Reichel

Add devicetree binding documentation for twl4030-madc analog digital converter.

---
 .../devicetree/bindings/iio/adc/twl4030-madc.txt   | 24 ++++++++++++++++++++++
 1 file changed, 24 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/adc/twl4030-madc.txt
Documentation/devicetree/bindings/iio/adc/twl4030-madc.txt new file +24 -0
new file mode 100644
index 0000000..6bdd214
@@ -0,0 +1,24 @@
+* TWL4030 Monitoring Analog to Digital Converter (MADC)
+
+The MADC subsystem in the TWL4030 consists of a 10-bit ADC
+combined with a 16-input analog multiplexer.
+
+Required properties:
+  - compatible: Should contain "ti,twl4030-madc".
+  - interrupts: IRQ line for the MADC submodule.
+  - #io-channel-cells: Should be set to <1>.
+
+Optional properties:
+  - ti,system-uses-second-madc-irq: boolean, set if the second madc irq register
+				    should be used, which is intended to be used
+				    by Co-Processors (e.g. a modem).
+
+Example:
+
+&twl {
+	madc {
+		compatible = "ti,twl4030-madc";
+		interrupts = <3>;
+		#io-channel-cells = <1>;
+	};
+};
Signature · 1 line
1.9.0
#63 lore raw

PATCH 7/9 mfd: twl4030-madc: Move driver to drivers/iio/adc

In reply to Sebastian Reichel

This is a driver for an A/D converter, which belongs into drivers/iio/adc.

---
 drivers/iio/adc/Kconfig                 | 10 ++++++++++
 drivers/iio/adc/Makefile                |  1 +
 drivers/{mfd => iio/adc}/twl4030-madc.c |  0
 drivers/mfd/Kconfig                     | 10 ----------
 drivers/mfd/Makefile                    |  1 -
 5 files changed, 11 insertions(+), 11 deletions(-)
 rename drivers/{mfd => iio/adc}/twl4030-madc.c (100%)
drivers/iio/adc/Kconfig +10 -0
index 2209f28..427f75c 100644
@@ -183,6 +183,16 @@ config TI_AM335X_ADC
 	  Say yes here to build support for Texas Instruments ADC
 	  driver which is also a MFD client.
 
+config TWL4030_MADC
+	tristate "TWL4030 MADC (Monitoring A/D Converter)"
+	depends on TWL4030_CORE
+	help
+	This driver provides support for Triton TWL4030-MADC. The
+	driver supports both RT and SW conversion methods.
+
+	This driver can also be built as a module. If so, the module will be
+	called twl4030-madc.
+
 config TWL6030_GPADC
 	tristate "TWL6030 GPADC (General Purpose A/D Converter) Support"
 	depends on TWL4030_CORE
drivers/iio/adc/Makefile +1 -0
index ba9a10a..9acf2df 100644
@@ -20,5 +20,6 @@ obj-$(CONFIG_MCP3422) += mcp3422.o
 obj-$(CONFIG_NAU7802) += nau7802.o
 obj-$(CONFIG_TI_ADC081C) += ti-adc081c.o
 obj-$(CONFIG_TI_AM335X_ADC) += ti_am335x_adc.o
+obj-$(CONFIG_TWL4030_MADC) += twl4030-madc.o
 obj-$(CONFIG_TWL6030_GPADC) += twl6030-gpadc.o
 obj-$(CONFIG_VIPERBOARD_ADC) += viperboard_adc.o
drivers/iio/adc/twl4030-madc.c renamed from drivers/mfd/twl4030-madc.c +0 -0
similarity index 100%
rename from drivers/mfd/twl4030-madc.c
rename to drivers/iio/adc/twl4030-madc.c
drivers/mfd/Kconfig +0 -10
index 49bb445..23a8a51 100644
@@ -935,16 +935,6 @@ config TWL4030_CORE
 	  high speed USB OTG transceiver, an audio codec (on most
 	  versions) and many other features.
 
-config TWL4030_MADC
-	tristate "TI TWL4030 MADC"
-	depends on TWL4030_CORE
-	help
-	This driver provides support for triton TWL4030-MADC. The
-	driver supports both RT and SW conversion methods.
-
-	This driver can be built as a module. If so it will be
-	named twl4030-madc
-
 config TWL4030_POWER
 	bool "TI TWL4030 power resources"
 	depends on TWL4030_CORE && ARM
drivers/mfd/Makefile +0 -1
index 5aea5ef..c8eb0bc 100644
@@ -71,7 +71,6 @@ obj-$(CONFIG_MFD_TPS80031)	+= tps80031.o
 obj-$(CONFIG_MENELAUS)		+= menelaus.o
 
 obj-$(CONFIG_TWL4030_CORE)	+= twl-core.o twl4030-irq.o twl6030-irq.o
-obj-$(CONFIG_TWL4030_MADC)      += twl4030-madc.o
 obj-$(CONFIG_TWL4030_POWER)    += twl4030-power.o
 obj-$(CONFIG_MFD_TWL4030_AUDIO)	+= twl4030-audio.o
 obj-$(CONFIG_TWL6040_CORE)	+= twl6040.o
Signature · 1 line
1.9.0
#64 lore raw

PATCH 8/9 iio: documentation: Add ABI documentation for *_mean_raw

In reply to Sebastian Reichel

Add ABI documentation for in_*_mean_raw files, which are already supported and used in the kernel for some time.

---
 Documentation/ABI/testing/sysfs-bus-iio | 8 ++++++++
 1 file changed, 8 insertions(+)
Documentation/ABI/testing/sysfs-bus-iio +8 -0
index 6e02c50..58ba333 100644
@@ -210,6 +210,14 @@ Contact:	[email protected]
 Description:
 		Scaled humidity measurement in milli percent.
 
+What:		/sys/bus/iio/devices/iio:deviceX/in_X_mean_raw
+KernelVersion:	3.5
+Contact:	[email protected]
+Description:
+		Averaged raw measurement from channel X. The number of values
+		used for averaging is device specific. The converting rules for
+		normal raw values also applies to the averaged raw values.
+
 What:		/sys/bus/iio/devices/iio:deviceX/in_accel_offset
 What:		/sys/bus/iio/devices/iio:deviceX/in_accel_x_offset
 What:		/sys/bus/iio/devices/iio:deviceX/in_accel_y_offset
Signature · 1 line
1.9.0
#65 lore raw

PATCH 9/9 iio: inkern: add iio_read_channel_average_raw

In reply to Sebastian Reichel

Add iio_read_channel_average_raw to support reading averaged raw values in consumer drivers.

---
 drivers/iio/inkern.c         | 18 ++++++++++++++++++
 include/linux/iio/consumer.h | 13 +++++++++++++
 2 files changed, 31 insertions(+)
drivers/iio/inkern.c +18 -0
index 0cf5f8e..adeba5a 100644
@@ -443,6 +443,24 @@ err_unlock:
 }
 EXPORT_SYMBOL_GPL(iio_read_channel_raw);
 
+int iio_read_channel_average_raw(struct iio_channel *chan, int *val)
+{
+	int ret;
+
+	mutex_lock(&chan->indio_dev->info_exist_lock);
+	if (chan->indio_dev->info == NULL) {
+		ret = -ENODEV;
+		goto err_unlock;
+	}
+
+	ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_AVERAGE_RAW);
+err_unlock:
+	mutex_unlock(&chan->indio_dev->info_exist_lock);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(iio_read_channel_average_raw);
+
 static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,
 	int raw, int *processed, unsigned int scale)
 {
include/linux/iio/consumer.h +13 -0
index 2752b1f..651f9a0 100644
@@ -123,6 +123,19 @@ int iio_read_channel_raw(struct iio_channel *chan,
 			 int *val);
 
 /**
+ * iio_read_channel_average_raw() - read from a given channel
+ * @chan:		The channel being queried.
+ * @val:		Value read back.
+ *
+ * Note raw reads from iio channels are in adc counts and hence
+ * scale will need to be applied if standard units required.
+ *
+ * In opposit to the normal iio_read_channel_raw this function
+ * returns the average of multiple reads.
+ */
+int iio_read_channel_average_raw(struct iio_channel *chan, int *val);
+
+/**
  * iio_read_channel_processed() - read processed value from a given channel
  * @chan:		The channel being queried.
  * @val:		Value read back.
Signature · 1 line
1.9.0
#66 lore raw

v2 cover Convert twl4030-madc to IIO API and add DT support

In reply to Sebastian Reichel

Hi,

This is PATCHv2 for converting twl4030-madc to the IIO API and adding DT support. The plan is to remove the private twl4030-madc API once all users have been removed or converted to the IIO API. The patchset compiles and has been tested on my Nokia N900.

Changes since PATCHv1 [0]:
* Fix warning about unused variable
* Add IIO_CHAN_INFO_PROCESSED to all channels, since voltage channels also need some postprocessing

TODO:
* Wait for feedback from DT binding maintainers
* Wait for feedback from Marek Belisko, who is testing the changes with twl4030-madc-battery driver on GTA04

[0] https://lkml.org/lkml/2014/3/1/105

-- Sebastian

Sebastian Reichel (9):
mfd: twl4030-madc: Use managed resources
mfd: twl4030-madc: Add DT support and convert to IIO framework
mfd: twl4030-madc: Cleanup driver
mfd: twl-core: Add twl_i2c_read/write_u16
mfd: twl4030-madc: Use twl_i2c_read/write_u16 for 16 bit registers
Documentation: DT: Document twl4030-madc binding
mfd: twl4030-madc: Move driver to drivers/iio/adc
iio: documentation: Add ABI documentation for *_mean_raw
iio: inkern: add iio_read_channel_average_raw

 Documentation/ABI/testing/sysfs-bus-iio            |   8 +
 .../devicetree/bindings/iio/adc/twl4030-madc.txt   |  24 ++
 drivers/iio/adc/Kconfig                            |  10 +
 drivers/iio/adc/Makefile                           |   1 +
 drivers/{mfd => iio/adc}/twl4030-madc.c            | 299 +++++++++++++--------
 drivers/iio/inkern.c                               |  18 ++
 drivers/mfd/Kconfig                                |  10 -
 drivers/mfd/Makefile                               |   1 -
 include/linux/i2c/twl.h                            |  12 +
 include/linux/i2c/twl4030-madc.h                   |   2 +-
 include/linux/iio/consumer.h                       |  13 +
 11 files changed, 274 insertions(+), 124 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/iio/adc/twl4030-madc.txt
 rename drivers/{mfd => iio/adc}/twl4030-madc.c (75%)
Signature · 1 line
1.9.0
#67 lore raw

PATCH v2 1/9 mfd: twl4030-madc: Use managed resources

Update twl4030-madc driver to use managed resources.

---
 drivers/mfd/twl4030-madc.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
drivers/mfd/twl4030-madc.c +6 -9
index 4c583e4..5458561 100644
@@ -702,14 +702,14 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 {
 	struct twl4030_madc_data *madc;
 	struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev);
-	int ret;
+	int irq, ret;
 	u8 regval;
 
 	if (!pdata) {
 		dev_err(&pdev->dev, "platform_data not available\n");
 		return -EINVAL;
 	}
-	madc = kzalloc(sizeof(*madc), GFP_KERNEL);
+	madc = devm_kzalloc(&pdev->dev, sizeof(*madc), GFP_KERNEL);
 	if (!madc)
 		return -ENOMEM;
 
@@ -726,7 +726,7 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 	    TWL4030_MADC_ISR1 : TWL4030_MADC_ISR2;
 	ret = twl4030_madc_set_power(madc, 1);
 	if (ret < 0)
-		goto err_power;
+		return ret;
 	ret = twl4030_madc_set_current_generator(madc, 0, 1);
 	if (ret < 0)
 		goto err_current_generator;
@@ -770,7 +770,9 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 
 	platform_set_drvdata(pdev, madc);
 	mutex_init(&madc->lock);
-	ret = request_threaded_irq(platform_get_irq(pdev, 0), NULL,
+
+	irq = platform_get_irq(pdev, 0);
+	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
 				   twl4030_madc_threaded_irq_handler,
 				   IRQF_TRIGGER_RISING, "twl4030_madc", madc);
 	if (ret) {
@@ -783,9 +785,6 @@ err_i2c:
 	twl4030_madc_set_current_generator(madc, 0, 0);
 err_current_generator:
 	twl4030_madc_set_power(madc, 0);
-err_power:
-	kfree(madc);
-
 	return ret;
 }
 
@@ -793,10 +792,8 @@ static int twl4030_madc_remove(struct platform_device *pdev)
 {
 	struct twl4030_madc_data *madc = platform_get_drvdata(pdev);
 
-	free_irq(platform_get_irq(pdev, 0), madc);
 	twl4030_madc_set_current_generator(madc, 0, 0);
 	twl4030_madc_set_power(madc, 0);
-	kfree(madc);
 
 	return 0;
 }
Signature · 1 line
1.9.0
maintainer Acked-by on v2 1/9
#68 lore raw

Re: [PATCHv2 1/9] mfd: twl4030-madc: Use managed resources

Quoted 4 lines · Sebastian Reichel

Update twl4030-madc driver to use managed resources.

Quoted 67 lines
---
  drivers/mfd/twl4030-madc.c | 15 ++++++---------
  1 file changed, 6 insertions(+), 9 deletions(-)
drivers/mfd/twl4030-madc.c +6 -9
index 4c583e4..5458561 100644
@@ -702,14 +702,14 @@ static int twl4030_madc_probe(struct platform_device *pdev)
  {
  	struct twl4030_madc_data *madc;
  	struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev);
-	int ret;
+	int irq, ret;
  	u8 regval;
 
  	if (!pdata) {
  		dev_err(&pdev->dev, "platform_data not available\n");
  		return -EINVAL;
  	}
-	madc = kzalloc(sizeof(*madc), GFP_KERNEL);
+	madc = devm_kzalloc(&pdev->dev, sizeof(*madc), GFP_KERNEL);
  	if (!madc)
  		return -ENOMEM;
 
@@ -726,7 +726,7 @@ static int twl4030_madc_probe(struct platform_device *pdev)
  	    TWL4030_MADC_ISR1 : TWL4030_MADC_ISR2;
  	ret = twl4030_madc_set_power(madc, 1);
  	if (ret < 0)
-		goto err_power;
+		return ret;
  	ret = twl4030_madc_set_current_generator(madc, 0, 1);
  	if (ret < 0)
  		goto err_current_generator;
@@ -770,7 +770,9 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 
  	platform_set_drvdata(pdev, madc);
  	mutex_init(&madc->lock);
-	ret = request_threaded_irq(platform_get_irq(pdev, 0), NULL,
+
+	irq = platform_get_irq(pdev, 0);
+	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  				   twl4030_madc_threaded_irq_handler,
  				   IRQF_TRIGGER_RISING, "twl4030_madc", madc);
  	if (ret) {
@@ -783,9 +785,6 @@ err_i2c:
  	twl4030_madc_set_current_generator(madc, 0, 0);
  err_current_generator:
  	twl4030_madc_set_power(madc, 0);
-err_power:
-	kfree(madc);
-
  	return ret;
  }
 
@@ -793,10 +792,8 @@ static int twl4030_madc_remove(struct platform_device *pdev)
  {
  	struct twl4030_madc_data *madc = platform_get_drvdata(pdev);
 
-	free_irq(platform_get_irq(pdev, 0), madc);
  	twl4030_madc_set_current_generator(madc, 0, 0);
  	twl4030_madc_set_power(madc, 0);
-	kfree(madc);
 
  	return 0;
  }
Reviewed-by on v2 1/9
#69 lore raw

Re: [PATCHv2 1/9] mfd: twl4030-madc: Use managed resources

In reply to Sebastian Reichel
Quoted 4 lines · Sebastian Reichel

Update twl4030-madc driver to use managed resources.

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

PATCH v2 2/9 mfd: twl4030-madc: Add DT support and convert to IIO framework

In reply to Sebastian Reichel

This converts twl4030-madc module to use the Industrial IO ADC framework and adds device tree support.

---
 drivers/mfd/twl4030-madc.c | 126 +++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 116 insertions(+), 10 deletions(-)
drivers/mfd/twl4030-madc.c +116 -10
index 5458561..0479af0 100644
@@ -47,11 +47,14 @@
 #include <linux/gfp.h>
 #include <linux/err.h>
 
+#include <linux/iio/iio.h>
+
 /*
  * struct twl4030_madc_data - a container for madc info
  * @dev - pointer to device structure for madc
  * @lock - mutex protecting this data structure
  * @requests - Array of request struct corresponding to SW1, SW2 and RT
+ * @use_second_irq - IRQ selection (main or co-processor)
  * @imr - Interrupt mask register of MADC
  * @isr - Interrupt status register of MADC
  */
@@ -59,10 +62,71 @@ struct twl4030_madc_data {
 	struct device *dev;
 	struct mutex lock;	/* mutex protecting this data structure */
 	struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS];
+	bool use_second_irq;
 	int imr;
 	int isr;
 };
 
+static int twl4030_madc_read(struct iio_dev *iio_dev,
+			     const struct iio_chan_spec *chan,
+			     int *val, int *val2, long mask)
+{
+	struct twl4030_madc_data *madc = iio_priv(iio_dev);
+	struct twl4030_madc_request req;
+	int ret;
+
+	req.method = madc->use_second_irq ? TWL4030_MADC_SW2 : TWL4030_MADC_SW1;
+
+	req.channels = BIT(chan->channel);
+	req.active = false;
+	req.func_cb = NULL;
+	req.type = TWL4030_MADC_WAIT;
+	req.raw = !(mask == IIO_CHAN_INFO_PROCESSED);
+	req.do_avg = (mask == IIO_CHAN_INFO_AVERAGE_RAW);
+
+	ret = twl4030_madc_conversion(&req);
+	if (ret < 0)
+		return ret;
+
+	*val = req.rbuf[chan->channel];
+
+	return IIO_VAL_INT;
+}
+
+static const struct iio_info twl4030_madc_iio_info = {
+	.read_raw = &twl4030_madc_read,
+	.driver_module = THIS_MODULE,
+};
+
+#define TWL4030_ADC_CHANNEL(_channel, _type, _name) {	\
+	.type = _type,					\
+	.channel = _channel,				\
+	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |  \
+			      BIT(IIO_CHAN_INFO_AVERAGE_RAW) | \
+			      BIT(IIO_CHAN_INFO_PROCESSED), \
+	.datasheet_name = _name,			\
+	.indexed = 1,					\
+}
+
+static const struct iio_chan_spec twl4030_madc_iio_channels[] = {
+	TWL4030_ADC_CHANNEL(0, IIO_VOLTAGE, "ADCIN0"),
+	TWL4030_ADC_CHANNEL(1, IIO_TEMP, "ADCIN1"),
+	TWL4030_ADC_CHANNEL(2, IIO_VOLTAGE, "ADCIN2"),
+	TWL4030_ADC_CHANNEL(3, IIO_VOLTAGE, "ADCIN3"),
+	TWL4030_ADC_CHANNEL(4, IIO_VOLTAGE, "ADCIN4"),
+	TWL4030_ADC_CHANNEL(5, IIO_VOLTAGE, "ADCIN5"),
+	TWL4030_ADC_CHANNEL(6, IIO_VOLTAGE, "ADCIN6"),
+	TWL4030_ADC_CHANNEL(7, IIO_VOLTAGE, "ADCIN7"),
+	TWL4030_ADC_CHANNEL(8, IIO_VOLTAGE, "ADCIN8"),
+	TWL4030_ADC_CHANNEL(9, IIO_VOLTAGE, "ADCIN9"),
+	TWL4030_ADC_CHANNEL(10, IIO_CURRENT, "ADCIN10"),
+	TWL4030_ADC_CHANNEL(11, IIO_VOLTAGE, "ADCIN11"),
+	TWL4030_ADC_CHANNEL(12, IIO_VOLTAGE, "ADCIN12"),
+	TWL4030_ADC_CHANNEL(13, IIO_VOLTAGE, "ADCIN13"),
+	TWL4030_ADC_CHANNEL(14, IIO_VOLTAGE, "ADCIN14"),
+	TWL4030_ADC_CHANNEL(15, IIO_VOLTAGE, "ADCIN15"),
+};
+
 static struct twl4030_madc_data *twl4030_madc;
 
 struct twl4030_prescale_divider_ratios {
@@ -702,28 +766,50 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 {
 	struct twl4030_madc_data *madc;
 	struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev);
+	struct device_node *np = pdev->dev.of_node;
 	int irq, ret;
 	u8 regval;
+	struct iio_dev *iio_dev = NULL;
 
-	if (!pdata) {
-		dev_err(&pdev->dev, "platform_data not available\n");
+	if (!pdata && !np) {
+		dev_err(&pdev->dev, "neither platform data nor Device Tree node available\n");
 		return -EINVAL;
 	}
-	madc = devm_kzalloc(&pdev->dev, sizeof(*madc), GFP_KERNEL);
-	if (!madc)
+
+	iio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*madc));
+	if (!iio_dev) {
+		dev_err(&pdev->dev, "failed allocating iio device\n");
 		return -ENOMEM;
+	}
 
+	madc = iio_priv(iio_dev);
 	madc->dev = &pdev->dev;
+	madc->use_second_irq = false;
+
+	iio_dev->name = dev_name(&pdev->dev);
+	iio_dev->dev.parent = &pdev->dev;
+	iio_dev->dev.of_node = pdev->dev.of_node;
+	iio_dev->info = &twl4030_madc_iio_info;
+	iio_dev->modes = INDIO_DIRECT_MODE;
+	iio_dev->channels = twl4030_madc_iio_channels;
+	iio_dev->num_channels = ARRAY_SIZE(twl4030_madc_iio_channels);
 
 	/*
 	 * Phoenix provides 2 interrupt lines. The first one is connected to
 	 * the OMAP. The other one can be connected to the other processor such
 	 * as modem. Hence two separate ISR and IMR registers.
 	 */
-	madc->imr = (pdata->irq_line == 1) ?
-	    TWL4030_MADC_IMR1 : TWL4030_MADC_IMR2;
-	madc->isr = (pdata->irq_line == 1) ?
-	    TWL4030_MADC_ISR1 : TWL4030_MADC_ISR2;
+	if (pdata && pdata->irq_line != 1)
+		madc->use_second_irq = true;
+	else if (!pdata)
+		madc->use_second_irq = of_property_read_bool(np,
+				       "ti,system-uses-second-madc-irq");
+
+	madc->imr = madc->use_second_irq ? TWL4030_MADC_IMR2 :
+					   TWL4030_MADC_IMR1;
+	madc->isr = madc->use_second_irq ? TWL4030_MADC_ISR2 :
+					   TWL4030_MADC_ISR1;
+
 	ret = twl4030_madc_set_power(madc, 1);
 	if (ret < 0)
 		return ret;
@@ -768,7 +854,7 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 		}
 	}
 
-	platform_set_drvdata(pdev, madc);
+	platform_set_drvdata(pdev, iio_dev);
 	mutex_init(&madc->lock);
 
 	irq = platform_get_irq(pdev, 0);
@@ -780,7 +866,15 @@ static int twl4030_madc_probe(struct platform_device *pdev)
 		goto err_i2c;
 	}
 	twl4030_madc = madc;
+
+	ret = iio_device_register(iio_dev);
+	if (ret) {
+		dev_dbg(&pdev->dev, "could not register iio device\n");
+		goto err_i2c;
+	}
+
 	return 0;
+
 err_i2c:
 	twl4030_madc_set_current_generator(madc, 0, 0);
 err_current_generator:
@@ -790,20 +884,32 @@ err_current_generator:
 
 static int twl4030_madc_remove(struct platform_device *pdev)
 {
-	struct twl4030_madc_data *madc = platform_get_drvdata(pdev);
+	struct iio_dev *iio_dev = platform_get_drvdata(pdev);
+	struct twl4030_madc_data *madc = iio_priv(iio_dev);
 
 	twl4030_madc_set_current_generator(madc, 0, 0);
 	twl4030_madc_set_power(madc, 0);
 
+	iio_device_unregister(iio_dev);
+
 	return 0;
 }
 
+#ifdef CONFIG_OF
+static const struct of_device_id twl_madc_of_match[] = {
+	{.compatible = "ti,twl4030-madc", },
+	{ },
+};
+MODULE_DEVICE_TABLE(of, twl_madc_of_match);
+#endif
+
 static struct platform_driver twl4030_madc_driver = {
 	.probe = twl4030_madc_probe,
 	.remove = twl4030_madc_remove,
 	.driver = {
 		   .name = "twl4030_madc",
 		   .owner = THIS_MODULE,
+		   .of_match_table = of_match_ptr(twl_madc_of_match),
 		   },
 };
 
Signature · 1 line
1.9.0
#71 lore raw

Re: [PATCHv2 2/9] mfd: twl4030-madc: Add DT support and convert to IIO framework

drivers/mfd/twl4030-madc.c

Quoted 12 lines

This converts twl4030-madc module to use the Industrial IO ADC framework and adds device tree support.

---
 drivers/mfd/twl4030-madc.c | 126 +++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 116 insertions(+), 10 deletions(-)
drivers/mfd/twl4030-madc.c +0 -0
index 5458561..0479af0 100644

<snip>

@@ -702,28 +766,50 @@ static int twl4030_madc_probe(struct platform_device *pdev)

drivers/mfd/twl4030-madc.c static int twl4030_madc_probe(struct platform_device *pdev) · line 787

Quoted 12 lines
drivers/mfd/twl4030-madc.c +7 -2
-	madc = devm_kzalloc(&pdev->dev, sizeof(*madc), GFP_KERNEL);
-	if (!madc)
+
+	iio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*madc));
+	if (!iio_dev) {
+		dev_err(&pdev->dev, "failed allocating iio device\n");
 		return -ENOMEM;
+	}
 
+	madc = iio_priv(iio_dev);
 	madc->dev = &pdev->dev;
+	madc->use_second_irq = false;

You don't need to do this a) it will be set below and b) it's already false since you used kzalloc() to zero the region.

<snip>

drivers/mfd/twl4030-madc.c static int twl4030_madc_probe(struct platform_device *pdev) · line 806

Quoted 9 lines
drivers/mfd/twl4030-madc.c +5 -4
-	madc->imr = (pdata->irq_line == 1) ?
-	    TWL4030_MADC_IMR1 : TWL4030_MADC_IMR2;
-	madc->isr = (pdata->irq_line == 1) ?
-	    TWL4030_MADC_ISR1 : TWL4030_MADC_ISR2;
+	if (pdata && pdata->irq_line != 1)
+		madc->use_second_irq = true;
+	else if (!pdata)
+		madc->use_second_irq = of_property_read_bool(np,
+				       "ti,system-uses-second-madc-irq");

How about:

	if (pdata)
		madc->use_second_irq = (pdata->irq_line != 1);
	else 
		madc->use_second_irq = of_property_read_bool(np,
					"ti,system-uses-second-madc-irq");

<snip>

drivers/mfd/twl4030-madc.c static int twl4030_madc_probe(struct platform_device *pdev) · line 872

Quoted 3 lines
drivers/mfd/twl4030-madc.c +3 -0
+	ret = iio_device_register(iio_dev);
+	if (ret) {
+		dev_dbg(&pdev->dev, "could not register iio device\n");

This should be a dev_err().

<snip>

drivers/mfd/twl4030-madc.c err_current_generator: · line 900

Quoted 3 lines
drivers/mfd/twl4030-madc.c +3 -0
+#ifdef CONFIG_OF
+static const struct of_device_id twl_madc_of_match[] = {
+	{.compatible = "ti,twl4030-madc", },

nit: "{ .compatible"

drivers/mfd/twl4030-madc.c err_current_generator: · line 913

Quoted 8 lines

static struct platform_driver twl4030_madc_driver = {

drivers/mfd/twl4030-madc.c +1 -0
 	.probe = twl4030_madc_probe,
 	.remove = twl4030_madc_remove,
 	.driver = {
 		   .name = "twl4030_madc",
 		   .owner = THIS_MODULE,
+		   .of_match_table = of_match_ptr(twl_madc_of_match),
 		   },

Also fix this please.

Quoted 2 lines

};

Signature · 4 lines
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
maintainer
#72 lore raw

Re: [PATCHv2 2/9] mfd: twl4030-madc: Add DT support and convert to IIO framework

In reply to Sebastian Reichel
Quoted 4 lines · Sebastian Reichel

This converts twl4030-madc module to use the Industrial IO ADC framework and adds device tree support.

One issues right down in the remove function alongside the ones Lee raised. Otherwise looks pretty much there to me.

Quoted 181 lines
---
  drivers/mfd/twl4030-madc.c | 126 +++++++++++++++++++++++++++++++++++++++++----
  1 file changed, 116 insertions(+), 10 deletions(-)
drivers/mfd/twl4030-madc.c +103 -9
index 5458561..0479af0 100644
@@ -47,11 +47,14 @@
  #include <linux/gfp.h>
  #include <linux/err.h>
 
+#include <linux/iio/iio.h>
+
  /*
   * struct twl4030_madc_data - a container for madc info
   * @dev - pointer to device structure for madc
   * @lock - mutex protecting this data structure
   * @requests - Array of request struct corresponding to SW1, SW2 and RT
+ * @use_second_irq - IRQ selection (main or co-processor)
   * @imr - Interrupt mask register of MADC
   * @isr - Interrupt status register of MADC
   */
@@ -59,10 +62,71 @@ struct twl4030_madc_data {
  	struct device *dev;
  	struct mutex lock;	/* mutex protecting this data structure */
  	struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS];
+	bool use_second_irq;
  	int imr;
  	int isr;
  };
 
+static int twl4030_madc_read(struct iio_dev *iio_dev,
+			     const struct iio_chan_spec *chan,
+			     int *val, int *val2, long mask)
+{
+	struct twl4030_madc_data *madc = iio_priv(iio_dev);
+	struct twl4030_madc_request req;
+	int ret;
+
+	req.method = madc->use_second_irq ? TWL4030_MADC_SW2 : TWL4030_MADC_SW1;
+
+	req.channels = BIT(chan->channel);
+	req.active = false;
+	req.func_cb = NULL;
+	req.type = TWL4030_MADC_WAIT;
+	req.raw = !(mask == IIO_CHAN_INFO_PROCESSED);
+	req.do_avg = (mask == IIO_CHAN_INFO_AVERAGE_RAW);
+
+	ret = twl4030_madc_conversion(&req);
+	if (ret < 0)
+		return ret;
+
+	*val = req.rbuf[chan->channel];
+
+	return IIO_VAL_INT;
+}
+
+static const struct iio_info twl4030_madc_iio_info = {
+	.read_raw = &twl4030_madc_read,
+	.driver_module = THIS_MODULE,
+};
+
+#define TWL4030_ADC_CHANNEL(_channel, _type, _name) {	\
+	.type = _type,					\
+	.channel = _channel,				\
+	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |  \
+			      BIT(IIO_CHAN_INFO_AVERAGE_RAW) | \
+			      BIT(IIO_CHAN_INFO_PROCESSED), \
+	.datasheet_name = _name,			\
+	.indexed = 1,					\
+}
+
+static const struct iio_chan_spec twl4030_madc_iio_channels[] = {
+	TWL4030_ADC_CHANNEL(0, IIO_VOLTAGE, "ADCIN0"),
+	TWL4030_ADC_CHANNEL(1, IIO_TEMP, "ADCIN1"),
+	TWL4030_ADC_CHANNEL(2, IIO_VOLTAGE, "ADCIN2"),
+	TWL4030_ADC_CHANNEL(3, IIO_VOLTAGE, "ADCIN3"),
+	TWL4030_ADC_CHANNEL(4, IIO_VOLTAGE, "ADCIN4"),
+	TWL4030_ADC_CHANNEL(5, IIO_VOLTAGE, "ADCIN5"),
+	TWL4030_ADC_CHANNEL(6, IIO_VOLTAGE, "ADCIN6"),
+	TWL4030_ADC_CHANNEL(7, IIO_VOLTAGE, "ADCIN7"),
+	TWL4030_ADC_CHANNEL(8, IIO_VOLTAGE, "ADCIN8"),
+	TWL4030_ADC_CHANNEL(9, IIO_VOLTAGE, "ADCIN9"),
+	TWL4030_ADC_CHANNEL(10, IIO_CURRENT, "ADCIN10"),
+	TWL4030_ADC_CHANNEL(11, IIO_VOLTAGE, "ADCIN11"),
+	TWL4030_ADC_CHANNEL(12, IIO_VOLTAGE, "ADCIN12"),
+	TWL4030_ADC_CHANNEL(13, IIO_VOLTAGE, "ADCIN13"),
+	TWL4030_ADC_CHANNEL(14, IIO_VOLTAGE, "ADCIN14"),
+	TWL4030_ADC_CHANNEL(15, IIO_VOLTAGE, "ADCIN15"),
+};
+
  static struct twl4030_madc_data *twl4030_madc;
 
  struct twl4030_prescale_divider_ratios {
@@ -702,28 +766,50 @@ static int twl4030_madc_probe(struct platform_device *pdev)
  {
  	struct twl4030_madc_data *madc;
  	struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev);
+	struct device_node *np = pdev->dev.of_node;
  	int irq, ret;
  	u8 regval;
+	struct iio_dev *iio_dev = NULL;
 
-	if (!pdata) {
-		dev_err(&pdev->dev, "platform_data not available\n");
+	if (!pdata && !np) {
+		dev_err(&pdev->dev, "neither platform data nor Device Tree node available\n");
  		return -EINVAL;
  	}
-	madc = devm_kzalloc(&pdev->dev, sizeof(*madc), GFP_KERNEL);
-	if (!madc)
+
+	iio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*madc));
+	if (!iio_dev) {
+		dev_err(&pdev->dev, "failed allocating iio device\n");
  		return -ENOMEM;
+	}
 
+	madc = iio_priv(iio_dev);
  	madc->dev = &pdev->dev;
+	madc->use_second_irq = false;
+
+	iio_dev->name = dev_name(&pdev->dev);
+	iio_dev->dev.parent = &pdev->dev;
+	iio_dev->dev.of_node = pdev->dev.of_node;
+	iio_dev->info = &twl4030_madc_iio_info;
+	iio_dev->modes = INDIO_DIRECT_MODE;
+	iio_dev->channels = twl4030_madc_iio_channels;
+	iio_dev->num_channels = ARRAY_SIZE(twl4030_madc_iio_channels);
 
  	/*
  	 * Phoenix provides 2 interrupt lines. The first one is connected to
  	 * the OMAP. The other one can be connected to the other processor such
  	 * as modem. Hence two separate ISR and IMR registers.
  	 */
-	madc->imr = (pdata->irq_line == 1) ?
-	    TWL4030_MADC_IMR1 : TWL4030_MADC_IMR2;
-	madc->isr = (pdata->irq_line == 1) ?
-	    TWL4030_MADC_ISR1 : TWL4030_MADC_ISR2;
+	if (pdata && pdata->irq_line != 1)
+		madc->use_second_irq = true;
+	else if (!pdata)
+		madc->use_second_irq = of_property_read_bool(np,
+				       "ti,system-uses-second-madc-irq");
+
+	madc->imr = madc->use_second_irq ? TWL4030_MADC_IMR2 :
+					   TWL4030_MADC_IMR1;
+	madc->isr = madc->use_second_irq ? TWL4030_MADC_ISR2 :
+					   TWL4030_MADC_ISR1;
+
  	ret = twl4030_madc_set_power(madc, 1);
  	if (ret < 0)
  		return ret;
@@ -768,7 +854,7 @@ static int twl4030_madc_probe(struct platform_device *pdev)
  		}
  	}
 
-	platform_set_drvdata(pdev, madc);
+	platform_set_drvdata(pdev, iio_dev);
  	mutex_init(&madc->lock);
 
  	irq = platform_get_irq(pdev, 0);
@@ -780,7 +866,15 @@ static int twl4030_madc_probe(struct platform_device *pdev)
  		goto err_i2c;
  	}
  	twl4030_madc = madc;
+
+	ret = iio_device_register(iio_dev);
+	if (ret) {
+		dev_dbg(&pdev->dev, "could not register iio device\n");
+		goto err_i2c;
+	}
+
  	return 0;
+
  err_i2c:
  	twl4030_madc_set_current_generator(madc, 0, 0);
  err_current_generator:
@@ -790,20 +884,32 @@ err_current_generator:
 

drivers/mfd/twl4030-madc.c err_current_generator: · line 893

Quoted 10 lines
drivers/mfd/twl4030-madc.c +3 -1
  static int twl4030_madc_remove(struct platform_device *pdev)
  {
-	struct twl4030_madc_data *madc = platform_get_drvdata(pdev);
+	struct iio_dev *iio_dev = platform_get_drvdata(pdev);
+	struct twl4030_madc_data *madc = iio_priv(iio_dev);
 
  	twl4030_madc_set_current_generator(madc, 0, 0);
  	twl4030_madc_set_power(madc, 0);
 
+	iio_device_unregister(iio_dev);

Removing the user interfaces after disabling the current generator and turning the device off seems to me to be a bad idea. There is a reason iio_device_unregister is almost always the first thing done in the remove function.

Also from a general ease of review point of view. The remove function should be in 'precise' reverse order of the probe function. Makes it nice and obviously correct assuming the functions it is calling are sensible!

Quoted 23 lines
drivers/mfd/twl4030-madc.c +10 -0
+
  	return 0;
  }
 
+#ifdef CONFIG_OF
+static const struct of_device_id twl_madc_of_match[] = {
+	{.compatible = "ti,twl4030-madc", },
+	{ },
+};
+MODULE_DEVICE_TABLE(of, twl_madc_of_match);
+#endif
+
  static struct platform_driver twl4030_madc_driver = {
  	.probe = twl4030_madc_probe,
  	.remove = twl4030_madc_remove,
  	.driver = {
  		   .name = "twl4030_madc",
  		   .owner = THIS_MODULE,
+		   .of_match_table = of_match_ptr(twl_madc_of_match),
  		   },
  };
 
Reviewed-by on v2 2/9
#73 lore raw

Re: [PATCHv2 2/9] mfd: twl4030-madc: Add DT support and convert to IIO framework

In reply to Sebastian Reichel
Quoted 4 lines · Sebastian Reichel

This converts twl4030-madc module to use the Industrial IO ADC framework and adds device tree support.

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

PATCH v2 3/9 mfd: twl4030-madc: Cleanup driver

In reply to Sebastian Reichel

Some style fixes in twl4030-madc driver.

---
 drivers/mfd/twl4030-madc.c       | 125 +++++++++++++++++++--------------------
 include/linux/i2c/twl4030-madc.h |   2 +-
 2 files changed, 61 insertions(+), 66 deletions(-)
drivers/mfd/twl4030-madc.c +60 -65
index 0479af0..f37da65 100644
@@ -49,22 +49,22 @@
 
 #include <linux/iio/iio.h>
 
-/*
+/**
  * struct twl4030_madc_data - a container for madc info
- * @dev - pointer to device structure for madc
- * @lock - mutex protecting this data structure
- * @requests - Array of request struct corresponding to SW1, SW2 and RT
- * @use_second_irq - IRQ selection (main or co-processor)
- * @imr - Interrupt mask register of MADC
- * @isr - Interrupt status register of MADC
+ * @dev:		Pointer to device structure for madc
+ * @lock:		Mutex protecting this data structure
+ * @requests:		Array of request struct corresponding to SW1, SW2 and RT
+ * @use_second_irq:	IRQ selection (main or co-processor)
+ * @imr:		Interrupt mask register of MADC
+ * @isr:		Interrupt status register of MADC
  */
 struct twl4030_madc_data {
 	struct device *dev;
 	struct mutex lock;	/* mutex protecting this data structure */
 	struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS];
 	bool use_second_irq;
-	int imr;
-	int isr;
+	u8 imr;
+	u8 isr;
 };
 
 static int twl4030_madc_read(struct iio_dev *iio_dev,
@@ -155,17 +155,16 @@ twl4030_divider_ratios[16] = {
 };
 
 
-/*
- * Conversion table from -3 to 55 degree Celcius
- */
-static int therm_tbl[] = {
-30800,	29500,	28300,	27100,
-26000,	24900,	23900,	22900,	22000,	21100,	20300,	19400,	18700,	17900,
-17200,	16500,	15900,	15300,	14700,	14100,	13600,	13100,	12600,	12100,
-11600,	11200,	10800,	10400,	10000,	9630,	9280,	8950,	8620,	8310,
-8020,	7730,	7460,	7200,	6950,	6710,	6470,	6250,	6040,	5830,
-5640,	5450,	5260,	5090,	4920,	4760,	4600,	4450,	4310,	4170,
-4040,	3910,	3790,	3670,	3550
+/* Conversion table from -3 to 55 degrees Celcius */
+static int twl4030_therm_tbl[] = {
+	30800,	29500,	28300,	27100,
+	26000,	24900,	23900,	22900,	22000,	21100,	20300,	19400,	18700,
+	17900,	17200,	16500,	15900,	15300,	14700,	14100,	13600,	13100,
+	12600,	12100,	11600,	11200,	10800,	10400,	10000,	9630,	9280,
+	8950,	8620,	8310,	8020,	7730,	7460,	7200,	6950,	6710,
+	6470,	6250,	6040,	5830,	5640,	5450,	5260,	5090,	4920,
+	4760,	4600,	4450,	4310,	4170,	4040,	3910,	3790,	3670,
+	3550
 };
 
 /*
@@ -197,11 +196,12 @@ const struct twl4030_madc_conversion_method twl4030_conversion_methods[] = {
 			      },
 };
 
-/*
- * Function to read a particular channel value.
- * @madc - pointer to struct twl4030_madc_data
- * @reg - lsb of ADC Channel
- * If the i2c read fails it returns an error else returns 0.
+/**
+ * twl4030_madc_channel_raw_read() - Function to read a particular channel value
+ * @madc:	pointer to struct twl4030_madc_data
+ * @reg:	lsb of ADC Channel
+ *
+ * Return: 0 on success, an error code otherwise.
  */
 static int twl4030_madc_channel_raw_read(struct twl4030_madc_data *madc, u8 reg)
 {
@@ -227,7 +227,7 @@ static int twl4030_madc_channel_raw_read(struct twl4030_madc_data *madc, u8 reg)
 }
 
 /*
- * Return battery temperature
+ * Return battery temperature in degrees Celsius
  * Or < 0 on failure.
  */
 static int twl4030battery_temperature(int raw_volt)
@@ -236,18 +236,18 @@ static int twl4030battery_temperature(int raw_volt)
 	int temp, curr, volt, res, ret;
 
 	volt = (raw_volt * TEMP_STEP_SIZE) / TEMP_PSR_R;
-	/* Getting and calculating the supply current in micro ampers */
+	/* Getting and calculating the supply current in micro amperes */
 	ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val,
 		REG_BCICTL2);
 	if (ret < 0)
 		return ret;
+
 	curr = ((val & TWL4030_BCI_ITHEN) + 1) * 10;
 	/* Getting and calculating the thermistor resistance in ohms */
 	res = volt * 1000 / curr;
 	/* calculating temperature */
 	for (temp = 58; temp >= 0; temp--) {
-		int actual = therm_tbl[temp];
-
+		int actual = twl4030_therm_tbl[temp];
 		if ((actual - res) >= 0)
 			break;
 	}
@@ -269,11 +269,12 @@ static int twl4030battery_current(int raw_volt)
 	else /* slope of 0.88 mV/mA */
 		return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R2;
 }
+
 /*
  * Function to read channel values
  * @madc - pointer to twl4030_madc_data struct
  * @reg_base - Base address of the first channel
- * @Channels - 16 bit bitmap. If the bit is set, channel value is read
+ * @Channels - 16 bit bitmap. If the bit is set, channel's value is read
  * @buf - The channel values are stored here. if read fails error
  * @raw - Return raw values without conversion
  * value is stored
@@ -284,17 +285,17 @@ static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
 				      long channels, int *buf,
 				      bool raw)
 {
-	int count = 0, count_req = 0, i;
+	int count = 0;
+	int i;
 	u8 reg;
 
 	for_each_set_bit(i, &channels, TWL4030_MADC_MAX_CHANNELS) {
-		reg = reg_base + 2 * i;
+		reg = reg_base + (2 * i);
 		buf[i] = twl4030_madc_channel_raw_read(madc, reg);
 		if (buf[i] < 0) {
-			dev_err(madc->dev,
-				"Unable to read register 0x%X\n", reg);
-			count_req++;
-			continue;
+			dev_err(madc->dev, "Unable to read register 0x%X\n",
+				reg);
+			return buf[i];
 		}
 		if (raw) {
 			count++;
@@ -305,7 +306,7 @@ static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
 			buf[i] = twl4030battery_current(buf[i]);
 			if (buf[i] < 0) {
 				dev_err(madc->dev, "err reading current\n");
-				count_req++;
+				return buf[i];
 			} else {
 				count++;
 				buf[i] = buf[i] - 750;
@@ -315,7 +316,7 @@ static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
 			buf[i] = twl4030battery_temperature(buf[i]);
 			if (buf[i] < 0) {
 				dev_err(madc->dev, "err reading temperature\n");
-				count_req++;
+				return buf[i];
 			} else {
 				buf[i] -= 3;
 				count++;
@@ -336,8 +337,6 @@ static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
 				twl4030_divider_ratios[i].numerator);
 		}
 	}
-	if (count_req)
-		dev_err(madc->dev, "%d channel conversion failed\n", count_req);
 
 	return count;
 }
@@ -361,13 +360,13 @@ static int twl4030_madc_enable_irq(struct twl4030_madc_data *madc, u8 id)
 			madc->imr);
 		return ret;
 	}
+
 	val &= ~(1 << id);
 	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr);
 	if (ret) {
 		dev_err(madc->dev,
 			"unable to write imr register 0x%X\n", madc->imr);
 		return ret;
-
 	}
 
 	return 0;
@@ -430,7 +429,7 @@ static irqreturn_t twl4030_madc_threaded_irq_handler(int irq, void *_madc)
 			continue;
 		ret = twl4030_madc_disable_irq(madc, i);
 		if (ret < 0)
-			dev_dbg(madc->dev, "Disable interrupt failed%d\n", i);
+			dev_dbg(madc->dev, "Disable interrupt failed %d\n", i);
 		madc->requests[i].result_pending = 1;
 	}
 	for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
@@ -512,21 +511,17 @@ static int twl4030_madc_start_conversion(struct twl4030_madc_data *madc,
 {
 	const struct twl4030_madc_conversion_method *method;
 	int ret = 0;
+
+	if (conv_method != TWL4030_MADC_SW1 && conv_method != TWL4030_MADC_SW2)
+		return -ENOTSUPP;
+
 	method = &twl4030_conversion_methods[conv_method];
-	switch (conv_method) {
-	case TWL4030_MADC_SW1:
-	case TWL4030_MADC_SW2:
-		ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
-				       TWL4030_MADC_SW_START, method->ctrl);
-		if (ret) {
-			dev_err(madc->dev,
-				"unable to write ctrl register 0x%X\n",
-				method->ctrl);
-			return ret;
-		}
-		break;
-	default:
-		break;
+	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, TWL4030_MADC_SW_START,
+			       method->ctrl);
+	if (ret) {
+		dev_err(madc->dev, "unable to write ctrl register 0x%X\n",
+			method->ctrl);
+		return ret;
 	}
 
 	return 0;
@@ -623,8 +618,8 @@ int twl4030_madc_conversion(struct twl4030_madc_request *req)
 				       ch_lsb, method->avg);
 		if (ret) {
 			dev_err(twl4030_madc->dev,
-				"unable to write sel reg 0x%X\n",
-				method->sel + 1);
+				"unable to write avg reg 0x%X\n",
+				method->avg);
 			goto out;
 		}
 	}
@@ -665,10 +660,6 @@ out:
 }
 EXPORT_SYMBOL_GPL(twl4030_madc_conversion);
 
-/*
- * Return channel value
- * Or < 0 on failure.
- */
 int twl4030_get_madc_conversion(int channel_no)
 {
 	struct twl4030_madc_request req;
@@ -703,6 +694,7 @@ static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc,
 					      int chan, int on)
 {
 	int ret;
+	int regmask;
 	u8 regval;
 
 	ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE,
@@ -712,10 +704,13 @@ static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc,
 			TWL4030_BCI_BCICTL1);
 		return ret;
 	}
+
+	regmask = chan ? TWL4030_BCI_ITHEN : TWL4030_BCI_TYPEN;
 	if (on)
-		regval |= chan ? TWL4030_BCI_ITHEN : TWL4030_BCI_TYPEN;
+		regval |= regmask;
 	else
-		regval &= chan ? ~TWL4030_BCI_ITHEN : ~TWL4030_BCI_TYPEN;
+		regval &= ~regmask;
+
 	ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
 			       regval, TWL4030_BCI_BCICTL1);
 	if (ret) {
@@ -730,7 +725,7 @@ static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc,
 /*
  * Function that sets MADC software power on bit to enable MADC
  * @madc - pointer to twl4030_madc_data struct
- * @on - Enable or disable MADC software powen on bit.
+ * @on - Enable or disable MADC software power on bit.
  * returns error if i2c read/write fails else 0
  */
 static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on)
include/linux/i2c/twl4030-madc.h +1 -1
index 01f5951..1c0134d 100644
@@ -44,7 +44,7 @@ struct twl4030_madc_conversion_method {
 
 struct twl4030_madc_request {
 	unsigned long channels;
-	u16 do_avg;
+	bool do_avg;
 	u16 method;
 	u16 type;
 	bool active;
Signature · 1 line
1.9.0
maintainer Acked-by on v2 3/9
#75 lore raw

Re: [PATCHv2 3/9] mfd: twl4030-madc: Cleanup driver

Quoted 3 lines · Sebastian Reichel

Some style fixes in twl4030-madc driver.

Gah - not sure I want to be known for reporting style issues :)

Quoted 2 lines

Looks good - one little whilst you are here comment below.

whether you do that or not. We can hardly hold there being remaining style issues against a style cleanup patch ;)

Quoted 286 lines
---
  drivers/mfd/twl4030-madc.c       | 125 +++++++++++++++++++--------------------
  include/linux/i2c/twl4030-madc.h |   2 +-
  2 files changed, 61 insertions(+), 66 deletions(-)
drivers/mfd/twl4030-madc.c +59 -64
index 0479af0..f37da65 100644
@@ -49,22 +49,22 @@
 
  #include <linux/iio/iio.h>
 
-/*
+/**
   * struct twl4030_madc_data - a container for madc info
- * @dev - pointer to device structure for madc
- * @lock - mutex protecting this data structure
- * @requests - Array of request struct corresponding to SW1, SW2 and RT
- * @use_second_irq - IRQ selection (main or co-processor)
- * @imr - Interrupt mask register of MADC
- * @isr - Interrupt status register of MADC
+ * @dev:		Pointer to device structure for madc
+ * @lock:		Mutex protecting this data structure
+ * @requests:		Array of request struct corresponding to SW1, SW2 and RT
+ * @use_second_irq:	IRQ selection (main or co-processor)
+ * @imr:		Interrupt mask register of MADC
+ * @isr:		Interrupt status register of MADC
   */
  struct twl4030_madc_data {
  	struct device *dev;
  	struct mutex lock;	/* mutex protecting this data structure */
  	struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS];
  	bool use_second_irq;
-	int imr;
-	int isr;
+	u8 imr;
+	u8 isr;
  };
 
  static int twl4030_madc_read(struct iio_dev *iio_dev,
@@ -155,17 +155,16 @@ twl4030_divider_ratios[16] = {
  };
 
 
-/*
- * Conversion table from -3 to 55 degree Celcius
- */
-static int therm_tbl[] = {
-30800,	29500,	28300,	27100,
-26000,	24900,	23900,	22900,	22000,	21100,	20300,	19400,	18700,	17900,
-17200,	16500,	15900,	15300,	14700,	14100,	13600,	13100,	12600,	12100,
-11600,	11200,	10800,	10400,	10000,	9630,	9280,	8950,	8620,	8310,
-8020,	7730,	7460,	7200,	6950,	6710,	6470,	6250,	6040,	5830,
-5640,	5450,	5260,	5090,	4920,	4760,	4600,	4450,	4310,	4170,
-4040,	3910,	3790,	3670,	3550
+/* Conversion table from -3 to 55 degrees Celcius */
+static int twl4030_therm_tbl[] = {
+	30800,	29500,	28300,	27100,
+	26000,	24900,	23900,	22900,	22000,	21100,	20300,	19400,	18700,
+	17900,	17200,	16500,	15900,	15300,	14700,	14100,	13600,	13100,
+	12600,	12100,	11600,	11200,	10800,	10400,	10000,	9630,	9280,
+	8950,	8620,	8310,	8020,	7730,	7460,	7200,	6950,	6710,
+	6470,	6250,	6040,	5830,	5640,	5450,	5260,	5090,	4920,
+	4760,	4600,	4450,	4310,	4170,	4040,	3910,	3790,	3670,
+	3550
  };
 
  /*
@@ -197,11 +196,12 @@ const struct twl4030_madc_conversion_method twl4030_conversion_methods[] = {
  			      },
  };
 
-/*
- * Function to read a particular channel value.
- * @madc - pointer to struct twl4030_madc_data
- * @reg - lsb of ADC Channel
- * If the i2c read fails it returns an error else returns 0.
+/**
+ * twl4030_madc_channel_raw_read() - Function to read a particular channel value
+ * @madc:	pointer to struct twl4030_madc_data
+ * @reg:	lsb of ADC Channel
+ *
+ * Return: 0 on success, an error code otherwise.
   */
  static int twl4030_madc_channel_raw_read(struct twl4030_madc_data *madc, u8 reg)
  {
@@ -227,7 +227,7 @@ static int twl4030_madc_channel_raw_read(struct twl4030_madc_data *madc, u8 reg)
  }
 
  /*
- * Return battery temperature
+ * Return battery temperature in degrees Celsius
   * Or < 0 on failure.
   */
  static int twl4030battery_temperature(int raw_volt)
@@ -236,18 +236,18 @@ static int twl4030battery_temperature(int raw_volt)
  	int temp, curr, volt, res, ret;
 
  	volt = (raw_volt * TEMP_STEP_SIZE) / TEMP_PSR_R;
-	/* Getting and calculating the supply current in micro ampers */
+	/* Getting and calculating the supply current in micro amperes */
  	ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val,
  		REG_BCICTL2);
  	if (ret < 0)
  		return ret;
+
  	curr = ((val & TWL4030_BCI_ITHEN) + 1) * 10;
  	/* Getting and calculating the thermistor resistance in ohms */
  	res = volt * 1000 / curr;
  	/* calculating temperature */
  	for (temp = 58; temp >= 0; temp--) {
-		int actual = therm_tbl[temp];
-
+		int actual = twl4030_therm_tbl[temp];
  		if ((actual - res) >= 0)
  			break;
  	}
@@ -269,11 +269,12 @@ static int twl4030battery_current(int raw_volt)
  	else /* slope of 0.88 mV/mA */
  		return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R2;
  }
+
  /*
   * Function to read channel values
   * @madc - pointer to twl4030_madc_data struct
   * @reg_base - Base address of the first channel
- * @Channels - 16 bit bitmap. If the bit is set, channel value is read
+ * @Channels - 16 bit bitmap. If the bit is set, channel's value is read
   * @buf - The channel values are stored here. if read fails error
   * @raw - Return raw values without conversion
   * value is stored
@@ -284,17 +285,17 @@ static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
  				      long channels, int *buf,
  				      bool raw)
  {
-	int count = 0, count_req = 0, i;
+	int count = 0;
+	int i;
  	u8 reg;
 
  	for_each_set_bit(i, &channels, TWL4030_MADC_MAX_CHANNELS) {
-		reg = reg_base + 2 * i;
+		reg = reg_base + (2 * i);
  		buf[i] = twl4030_madc_channel_raw_read(madc, reg);
  		if (buf[i] < 0) {
-			dev_err(madc->dev,
-				"Unable to read register 0x%X\n", reg);
-			count_req++;
-			continue;
+			dev_err(madc->dev, "Unable to read register 0x%X\n",
+				reg);
+			return buf[i];
  		}
  		if (raw) {
  			count++;
@@ -305,7 +306,7 @@ static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
  			buf[i] = twl4030battery_current(buf[i]);
  			if (buf[i] < 0) {
  				dev_err(madc->dev, "err reading current\n");
-				count_req++;
+				return buf[i];
  			} else {
  				count++;
  				buf[i] = buf[i] - 750;
@@ -315,7 +316,7 @@ static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
  			buf[i] = twl4030battery_temperature(buf[i]);
  			if (buf[i] < 0) {
  				dev_err(madc->dev, "err reading temperature\n");
-				count_req++;
+				return buf[i];
  			} else {
  				buf[i] -= 3;
  				count++;
@@ -336,8 +337,6 @@ static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
  				twl4030_divider_ratios[i].numerator);
  		}
  	}
-	if (count_req)
-		dev_err(madc->dev, "%d channel conversion failed\n", count_req);
 
  	return count;
  }
@@ -361,13 +360,13 @@ static int twl4030_madc_enable_irq(struct twl4030_madc_data *madc, u8 id)
  			madc->imr);
  		return ret;
  	}
+
  	val &= ~(1 << id);
  	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr);
  	if (ret) {
  		dev_err(madc->dev,
  			"unable to write imr register 0x%X\n", madc->imr);
  		return ret;
-
  	}
 
  	return 0;
@@ -430,7 +429,7 @@ static irqreturn_t twl4030_madc_threaded_irq_handler(int irq, void *_madc)
  			continue;
  		ret = twl4030_madc_disable_irq(madc, i);
  		if (ret < 0)
-			dev_dbg(madc->dev, "Disable interrupt failed%d\n", i);
+			dev_dbg(madc->dev, "Disable interrupt failed %d\n", i);
  		madc->requests[i].result_pending = 1;
  	}
  	for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
@@ -512,21 +511,17 @@ static int twl4030_madc_start_conversion(struct twl4030_madc_data *madc,
  {
  	const struct twl4030_madc_conversion_method *method;
  	int ret = 0;
+
+	if (conv_method != TWL4030_MADC_SW1 && conv_method != TWL4030_MADC_SW2)
+		return -ENOTSUPP;
+
  	method = &twl4030_conversion_methods[conv_method];
-	switch (conv_method) {
-	case TWL4030_MADC_SW1:
-	case TWL4030_MADC_SW2:
-		ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
-				       TWL4030_MADC_SW_START, method->ctrl);
-		if (ret) {
-			dev_err(madc->dev,
-				"unable to write ctrl register 0x%X\n",
-				method->ctrl);
-			return ret;
-		}
-		break;
-	default:
-		break;
+	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, TWL4030_MADC_SW_START,
+			       method->ctrl);
+	if (ret) {
+		dev_err(madc->dev, "unable to write ctrl register 0x%X\n",
+			method->ctrl);
+		return ret;
  	}
 
  	return 0;
@@ -623,8 +618,8 @@ int twl4030_madc_conversion(struct twl4030_madc_request *req)
  				       ch_lsb, method->avg);
  		if (ret) {
  			dev_err(twl4030_madc->dev,
-				"unable to write sel reg 0x%X\n",
-				method->sel + 1);
+				"unable to write avg reg 0x%X\n",
+				method->avg);
  			goto out;
  		}
  	}
@@ -665,10 +660,6 @@ out:
  }
  EXPORT_SYMBOL_GPL(twl4030_madc_conversion);
 
-/*
- * Return channel value
- * Or < 0 on failure.
- */
  int twl4030_get_madc_conversion(int channel_no)
  {
  	struct twl4030_madc_request req;
@@ -703,6 +694,7 @@ static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc,
  					      int chan, int on)
  {
  	int ret;
+	int regmask;
  	u8 regval;
 
  	ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE,
@@ -712,10 +704,13 @@ static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc,
  			TWL4030_BCI_BCICTL1);
  		return ret;
  	}
+
+	regmask = chan ? TWL4030_BCI_ITHEN : TWL4030_BCI_TYPEN;
  	if (on)
-		regval |= chan ? TWL4030_BCI_ITHEN : TWL4030_BCI_TYPEN;
+		regval |= regmask;
  	else
-		regval &= chan ? ~TWL4030_BCI_ITHEN : ~TWL4030_BCI_TYPEN;
+		regval &= ~regmask;
+
  	ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
  			       regval, TWL4030_BCI_BCICTL1);
  	if (ret) {

drivers/mfd/twl4030-madc.c static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc, · line 725

Quoted 1 line
drivers/mfd/twl4030-madc.c +0 -0
@@ -730,7 +725,7 @@ static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc,
  /*

If you are cleaning up anyway. Lets put this in kernel doc format.

Quoted 21 lines
drivers/mfd/twl4030-madc.c +1 -1
   * Function that sets MADC software power on bit to enable MADC
   * @madc - pointer to twl4030_madc_data struct
- * @on - Enable or disable MADC software powen on bit.
+ * @on - Enable or disable MADC software power on bit.
   * returns error if i2c read/write fails else 0
   */
  static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on)
include/linux/i2c/twl4030-madc.h +1 -1
index 01f5951..1c0134d 100644
@@ -44,7 +44,7 @@ struct twl4030_madc_conversion_method {
 
  struct twl4030_madc_request {
  	unsigned long channels;
-	u16 do_avg;
+	bool do_avg;
  	u16 method;
  	u16 type;
  	bool active;
Reviewed-by on v2 3/9
#76 lore raw

Re: [PATCHv2 3/9] mfd: twl4030-madc: Cleanup driver

In reply to Sebastian Reichel

Hi!

Quoted 5 lines

Some style fixes in twl4030-madc driver.

drivers/mfd/twl4030-madc.c out: · line 665

Quoted 11 lines
drivers/mfd/twl4030-madc.c +0 -4
@@ -665,10 +660,6 @@ out:
 }
 EXPORT_SYMBOL_GPL(twl4030_madc_conversion);
 
-/*
- * Return channel value
- * Or < 0 on failure.
- */
 int twl4030_get_madc_conversion(int channel_no)
 {
 	struct twl4030_madc_request req;

Maybe I'd put the comment on one line, but it seems useful...?

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

PATCH v2 4/9 mfd: twl-core: Add twl_i2c_read/write_u16

In reply to Sebastian Reichel

Add a simple twl_i2c_read/write_u16 wrapper over
the twl_i2c_read/write, which is similar to the
twl_i2c_read/write_u8 wrapper.

---
 include/linux/i2c/twl.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)
include/linux/i2c/twl.h +12 -0
index ade1c06..d2b1670 100644
@@ -195,6 +195,18 @@ static inline int twl_i2c_read_u8(u8 mod_no, u8 *val, u8 reg) {
 	return twl_i2c_read(mod_no, val, reg, 1);
 }
 
+static inline int twl_i2c_write_u16(u8 mod_no, u16 val, u8 reg) {
+	val = cpu_to_le16(val);
+	return twl_i2c_write(mod_no, (u8*) &val, reg, 2);
+}
+
+static inline int twl_i2c_read_u16(u8 mod_no, u16 *val, u8 reg) {
+	int ret;
+	ret = twl_i2c_read(mod_no, (u8*) val, reg, 2);
+	*val = le16_to_cpu(*val);
+	return ret;
+}
+
 int twl_get_type(void);
 int twl_get_version(void);
 int twl_get_hfclk_rate(void);
Signature · 1 line
1.9.0
#78 lore raw

PATCH v2 5/9 mfd: twl4030-madc: Use twl_i2c_read/write_u16 for 16 bit registers

In reply to Sebastian Reichel

Simplify reading and writing of 16 bit TWL registers in the driver by using twl_i2c_read_u16 and twl_i2c_write_u16.

---
 drivers/mfd/twl4030-madc.c | 39 ++++++++-------------------------------
 1 file changed, 8 insertions(+), 31 deletions(-)
drivers/mfd/twl4030-madc.c +8 -31
index f37da65..d1ca36a 100644
@@ -205,25 +205,19 @@ const struct twl4030_madc_conversion_method twl4030_conversion_methods[] = {
  */
 static int twl4030_madc_channel_raw_read(struct twl4030_madc_data *madc, u8 reg)
 {
-	u8 msb, lsb;
+	u16 val;
 	int ret;
 	/*
 	 * For each ADC channel, we have MSB and LSB register pair. MSB address
 	 * is always LSB address+1. reg parameter is the address of LSB register
 	 */
-	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &msb, reg + 1);
+	ret = twl_i2c_read_u16(TWL4030_MODULE_MADC, &val, reg);
 	if (ret) {
-		dev_err(madc->dev, "unable to read MSB register 0x%X\n",
-			reg + 1);
-		return ret;
-	}
-	ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &lsb, reg);
-	if (ret) {
-		dev_err(madc->dev, "unable to read LSB register 0x%X\n", reg);
+		dev_err(madc->dev, "unable to read register 0x%X\n", reg);
 		return ret;
 	}
 
-	return (int)(((msb << 8) | lsb) >> 6);
+	return (int)(val >> 6);
 }
 
 /*
@@ -572,7 +566,6 @@ static int twl4030_madc_wait_conversion_ready(struct twl4030_madc_data *madc,
 int twl4030_madc_conversion(struct twl4030_madc_request *req)
 {
 	const struct twl4030_madc_conversion_method *method;
-	u8 ch_msb, ch_lsb;
 	int ret;
 
 	if (!req || !twl4030_madc)
@@ -588,37 +581,21 @@ int twl4030_madc_conversion(struct twl4030_madc_request *req)
 		ret = -EBUSY;
 		goto out;
 	}
-	ch_msb = (req->channels >> 8) & 0xff;
-	ch_lsb = req->channels & 0xff;
 	method = &twl4030_conversion_methods[req->method];
 	/* Select channels to be converted */
-	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_msb, method->sel + 1);
+	ret = twl_i2c_write_u16(TWL4030_MODULE_MADC, req->channels, method->sel);
 	if (ret) {
 		dev_err(twl4030_madc->dev,
-			"unable to write sel register 0x%X\n", method->sel + 1);
-		goto out;
-	}
-	ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_lsb, method->sel);
-	if (ret) {
-		dev_err(twl4030_madc->dev,
-			"unable to write sel register 0x%X\n", method->sel + 1);
+			"unable to write sel register 0x%X\n", method->sel);
 		goto out;
 	}
 	/* Select averaging for all channels if do_avg is set */
 	if (req->do_avg) {
-		ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
-				       ch_msb, method->avg + 1);
+		ret = twl_i2c_write_u16(TWL4030_MODULE_MADC, req->channels,
+				       method->avg);
 		if (ret) {
 			dev_err(twl4030_madc->dev,
 				"unable to write avg register 0x%X\n",
-				method->avg + 1);
-			goto out;
-		}
-		ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
-				       ch_lsb, method->avg);
-		if (ret) {
-			dev_err(twl4030_madc->dev,
-				"unable to write avg reg 0x%X\n",
 				method->avg);
 			goto out;
 		}
Signature · 1 line
1.9.0
#79 lore raw

PATCH v2 6/9 Documentation: DT: Document twl4030-madc binding

In reply to Sebastian Reichel

Add devicetree binding documentation for twl4030-madc analog digital converter.

---
 .../devicetree/bindings/iio/adc/twl4030-madc.txt   | 24 ++++++++++++++++++++++
 1 file changed, 24 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/adc/twl4030-madc.txt
Documentation/devicetree/bindings/iio/adc/twl4030-madc.txt new file +24 -0
new file mode 100644
index 0000000..6bdd214
@@ -0,0 +1,24 @@
+* TWL4030 Monitoring Analog to Digital Converter (MADC)
+
+The MADC subsystem in the TWL4030 consists of a 10-bit ADC
+combined with a 16-input analog multiplexer.
+
+Required properties:
+  - compatible: Should contain "ti,twl4030-madc".
+  - interrupts: IRQ line for the MADC submodule.
+  - #io-channel-cells: Should be set to <1>.
+
+Optional properties:
+  - ti,system-uses-second-madc-irq: boolean, set if the second madc irq register
+				    should be used, which is intended to be used
+				    by Co-Processors (e.g. a modem).
+
+Example:
+
+&twl {
+	madc {
+		compatible = "ti,twl4030-madc";
+		interrupts = <3>;
+		#io-channel-cells = <1>;
+	};
+};
Signature · 1 line
1.9.0
maintainer Acked-by on v2 6/9
#80 lore raw

Re: [PATCHv2 6/9] Documentation: DT: Document twl4030-madc binding

Quoted 4 lines · Sebastian Reichel

Add devicetree binding documentation for twl4030-madc analog digital converter.

I'm happy with this, but it needs the usual sign off form a device tree maintainer or 3 weeks to pass since it was posted.

Quoted 36 lines
---
  .../devicetree/bindings/iio/adc/twl4030-madc.txt   | 24 ++++++++++++++++++++++
  1 file changed, 24 insertions(+)

create mode 100644 Documentation/devicetree/bindings/iio/adc/twl4030-madc.txt

Documentation/devicetree/bindings/iio/adc/twl4030-madc.txt new file +24 -0
new file mode 100644
index 0000000..6bdd214
@@ -0,0 +1,24 @@
+* TWL4030 Monitoring Analog to Digital Converter (MADC)
+
+The MADC subsystem in the TWL4030 consists of a 10-bit ADC
+combined with a 16-input analog multiplexer.
+
+Required properties:
+  - compatible: Should contain "ti,twl4030-madc".
+  - interrupts: IRQ line for the MADC submodule.
+  - #io-channel-cells: Should be set to <1>.
+
+Optional properties:
+  - ti,system-uses-second-madc-irq: boolean, set if the second madc irq register
+				    should be used, which is intended to be used
+				    by Co-Processors (e.g. a modem).
+
+Example:
+
+&twl {
+	madc {
+		compatible = "ti,twl4030-madc";
+		interrupts = <3>;
+		#io-channel-cells = <1>;
+	};
+};
#81 lore raw

PATCH v2 7/9 mfd: twl4030-madc: Move driver to drivers/iio/adc

In reply to Sebastian Reichel

This is a driver for an A/D converter, which belongs into drivers/iio/adc.

---
 drivers/iio/adc/Kconfig                 | 10 ++++++++++
 drivers/iio/adc/Makefile                |  1 +
 drivers/{mfd => iio/adc}/twl4030-madc.c |  0
 drivers/mfd/Kconfig                     | 10 ----------
 drivers/mfd/Makefile                    |  1 -
 5 files changed, 11 insertions(+), 11 deletions(-)
 rename drivers/{mfd => iio/adc}/twl4030-madc.c (100%)
drivers/iio/adc/Kconfig +10 -0
index 2209f28..427f75c 100644
@@ -183,6 +183,16 @@ config TI_AM335X_ADC
 	  Say yes here to build support for Texas Instruments ADC
 	  driver which is also a MFD client.
 
+config TWL4030_MADC
+	tristate "TWL4030 MADC (Monitoring A/D Converter)"
+	depends on TWL4030_CORE
+	help
+	This driver provides support for Triton TWL4030-MADC. The
+	driver supports both RT and SW conversion methods.
+
+	This driver can also be built as a module. If so, the module will be
+	called twl4030-madc.
+
 config TWL6030_GPADC
 	tristate "TWL6030 GPADC (General Purpose A/D Converter) Support"
 	depends on TWL4030_CORE
drivers/iio/adc/Makefile +1 -0
index ba9a10a..9acf2df 100644
@@ -20,5 +20,6 @@ obj-$(CONFIG_MCP3422) += mcp3422.o
 obj-$(CONFIG_NAU7802) += nau7802.o
 obj-$(CONFIG_TI_ADC081C) += ti-adc081c.o
 obj-$(CONFIG_TI_AM335X_ADC) += ti_am335x_adc.o
+obj-$(CONFIG_TWL4030_MADC) += twl4030-madc.o
 obj-$(CONFIG_TWL6030_GPADC) += twl6030-gpadc.o
 obj-$(CONFIG_VIPERBOARD_ADC) += viperboard_adc.o
drivers/iio/adc/twl4030-madc.c renamed from drivers/mfd/twl4030-madc.c +0 -0
similarity index 100%
rename from drivers/mfd/twl4030-madc.c
rename to drivers/iio/adc/twl4030-madc.c
drivers/mfd/Kconfig +0 -10
index 49bb445..23a8a51 100644
@@ -935,16 +935,6 @@ config TWL4030_CORE
 	  high speed USB OTG transceiver, an audio codec (on most
 	  versions) and many other features.
 
-config TWL4030_MADC
-	tristate "TI TWL4030 MADC"
-	depends on TWL4030_CORE
-	help
-	This driver provides support for triton TWL4030-MADC. The
-	driver supports both RT and SW conversion methods.
-
-	This driver can be built as a module. If so it will be
-	named twl4030-madc
-
 config TWL4030_POWER
 	bool "TI TWL4030 power resources"
 	depends on TWL4030_CORE && ARM
drivers/mfd/Makefile +0 -1
index 5aea5ef..c8eb0bc 100644
@@ -71,7 +71,6 @@ obj-$(CONFIG_MFD_TPS80031)	+= tps80031.o
 obj-$(CONFIG_MENELAUS)		+= menelaus.o
 
 obj-$(CONFIG_TWL4030_CORE)	+= twl-core.o twl4030-irq.o twl6030-irq.o
-obj-$(CONFIG_TWL4030_MADC)      += twl4030-madc.o
 obj-$(CONFIG_TWL4030_POWER)    += twl4030-power.o
 obj-$(CONFIG_MFD_TWL4030_AUDIO)	+= twl4030-audio.o
 obj-$(CONFIG_TWL6040_CORE)	+= twl6040.o
Signature · 1 line
1.9.0
#82 lore raw

PATCH v2 8/9 iio: documentation: Add ABI documentation for *_mean_raw

In reply to Sebastian Reichel

Add ABI documentation for in_*_mean_raw files, which are already supported and used in the kernel for some time.

---
 Documentation/ABI/testing/sysfs-bus-iio | 8 ++++++++
 1 file changed, 8 insertions(+)
Documentation/ABI/testing/sysfs-bus-iio +8 -0
index 6e02c50..58ba333 100644
@@ -210,6 +210,14 @@ Contact:	[email protected]
 Description:
 		Scaled humidity measurement in milli percent.
 
+What:		/sys/bus/iio/devices/iio:deviceX/in_X_mean_raw
+KernelVersion:	3.5
+Contact:	[email protected]
+Description:
+		Averaged raw measurement from channel X. The number of values
+		used for averaging is device specific. The converting rules for
+		normal raw values also applies to the averaged raw values.
+
 What:		/sys/bus/iio/devices/iio:deviceX/in_accel_offset
 What:		/sys/bus/iio/devices/iio:deviceX/in_accel_x_offset
 What:		/sys/bus/iio/devices/iio:deviceX/in_accel_y_offset
Signature · 1 line
1.9.0
maintainer Acked-by on v2 8/9
#83 lore raw

Re: [PATCHv2 8/9] iio: documentation: Add ABI documentation for *_mean_raw

Quoted 4 lines · Sebastian Reichel

Add ABI documentation for in_*_mean_raw files, which are already supported and used in the kernel for some time.

Thanks for doing this.

Quoted 24 lines
---
  Documentation/ABI/testing/sysfs-bus-iio | 8 ++++++++
  1 file changed, 8 insertions(+)
Documentation/ABI/testing/sysfs-bus-iio +8 -0
index 6e02c50..58ba333 100644
@@ -210,6 +210,14 @@ Contact:	[email protected]
  Description:
  		Scaled humidity measurement in milli percent.
 
+What:		/sys/bus/iio/devices/iio:deviceX/in_X_mean_raw
+KernelVersion:	3.5
+Contact:	[email protected]
+Description:
+		Averaged raw measurement from channel X. The number of values
+		used for averaging is device specific. The converting rules for
+		normal raw values also applies to the averaged raw values.
+
  What:		/sys/bus/iio/devices/iio:deviceX/in_accel_offset
  What:		/sys/bus/iio/devices/iio:deviceX/in_accel_x_offset
  What:		/sys/bus/iio/devices/iio:deviceX/in_accel_y_offset
#84 lore raw

PATCH v2 9/9 iio: inkern: add iio_read_channel_average_raw

In reply to Sebastian Reichel

Add iio_read_channel_average_raw to support reading averaged raw values in consumer drivers.

---
 drivers/iio/inkern.c         | 18 ++++++++++++++++++
 include/linux/iio/consumer.h | 13 +++++++++++++
 2 files changed, 31 insertions(+)
drivers/iio/inkern.c +18 -0
index 0cf5f8e..adeba5a 100644
@@ -443,6 +443,24 @@ err_unlock:
 }
 EXPORT_SYMBOL_GPL(iio_read_channel_raw);
 
+int iio_read_channel_average_raw(struct iio_channel *chan, int *val)
+{
+	int ret;
+
+	mutex_lock(&chan->indio_dev->info_exist_lock);
+	if (chan->indio_dev->info == NULL) {
+		ret = -ENODEV;
+		goto err_unlock;
+	}
+
+	ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_AVERAGE_RAW);
+err_unlock:
+	mutex_unlock(&chan->indio_dev->info_exist_lock);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(iio_read_channel_average_raw);
+
 static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,
 	int raw, int *processed, unsigned int scale)
 {
include/linux/iio/consumer.h +13 -0
index 2752b1f..651f9a0 100644
@@ -123,6 +123,19 @@ int iio_read_channel_raw(struct iio_channel *chan,
 			 int *val);
 
 /**
+ * iio_read_channel_average_raw() - read from a given channel
+ * @chan:		The channel being queried.
+ * @val:		Value read back.
+ *
+ * Note raw reads from iio channels are in adc counts and hence
+ * scale will need to be applied if standard units required.
+ *
+ * In opposit to the normal iio_read_channel_raw this function
+ * returns the average of multiple reads.
+ */
+int iio_read_channel_average_raw(struct iio_channel *chan, int *val);
+
+/**
  * iio_read_channel_processed() - read processed value from a given channel
  * @chan:		The channel being queried.
  * @val:		Value read back.
Signature · 1 line
1.9.0
maintainer Acked-by on v2 9/9
#85 lore raw

Re: [PATCHv2 9/9] iio: inkern: add iio_read_channel_average_raw

Quoted 4 lines · Sebastian Reichel

Add iio_read_channel_average_raw to support reading averaged raw values in consumer drivers.

Sometimes these wrappers seem a little bit silly. There might be a case for automating them somewhat via some macros, but for now this is fine.

Docs should in theory probably be with the implementation, but that's wrong throughout consumer.h so this isn't the time to change it!

Jonathan

Quoted 39 lines
---
  drivers/iio/inkern.c         | 18 ++++++++++++++++++
  include/linux/iio/consumer.h | 13 +++++++++++++
  2 files changed, 31 insertions(+)
drivers/iio/inkern.c +18 -0
index 0cf5f8e..adeba5a 100644
@@ -443,6 +443,24 @@ err_unlock:
  }
  EXPORT_SYMBOL_GPL(iio_read_channel_raw);
 
+int iio_read_channel_average_raw(struct iio_channel *chan, int *val)
+{
+	int ret;
+
+	mutex_lock(&chan->indio_dev->info_exist_lock);
+	if (chan->indio_dev->info == NULL) {
+		ret = -ENODEV;
+		goto err_unlock;
+	}
+
+	ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_AVERAGE_RAW);
+err_unlock:
+	mutex_unlock(&chan->indio_dev->info_exist_lock);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(iio_read_channel_average_raw);
+
  static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,
  	int raw, int *processed, unsigned int scale)
  {
include/linux/iio/consumer.h +0 -0
index 2752b1f..651f9a0 100644

include/linux/iio/consumer.h int iio_read_channel_raw(struct iio_channel *chan, · line 124

Quoted 2 lines
include/linux/iio/consumer.h +0 -0
@@ -123,6 +123,19 @@ int iio_read_channel_raw(struct iio_channel *chan,
  			 int *val);
 

Ah, my bad habits propogate onwards. The docs should be with the implementation. Oh well, at somepoint I'll clean all remaining cases of this up.

Quoted 18 lines
include/linux/iio/consumer.h +13 -0
  /**
+ * iio_read_channel_average_raw() - read from a given channel
+ * @chan:		The channel being queried.
+ * @val:		Value read back.
+ *
+ * Note raw reads from iio channels are in adc counts and hence
+ * scale will need to be applied if standard units required.
+ *
+ * In opposit to the normal iio_read_channel_raw this function
+ * returns the average of multiple reads.
+ */
+int iio_read_channel_average_raw(struct iio_channel *chan, int *val);
+
+/**
   * iio_read_channel_processed() - read processed value from a given channel
   * @chan:		The channel being queried.
   * @val:		Value read back.
#86 lore raw

Re: [PATCHv2 0/9] Convert twl4030-madc to IIO API and add DT support

In reply to Sebastian Reichel

Hi,

Quoted 12 lines · Sebastian Reichel

Hi,

This is PATCHv2 for converting twl4030-madc to the IIO API and adding DT support. The plan is to remove the private twl4030-madc API once all users have been removed or converted to the IIO API. The patchset compiles and has been tested on my Nokia N900.

Changes since PATCHv1 [0]:
* Fix warning about unused variable
* Add IIO_CHAN_INFO_PROCESSED to all channels, since voltage channels also need some postprocessing

Quoted 4 lines

TODO:
* Wait for feedback from DT binding maintainers
* Wait for feedback from Marek Belisko, who is testing the changes with twl4030-madc-battery driver on GTA04

Tested on gta04 board. Works as expected with twl4030_madc_battery patches [1] on top. So you can add my Tested-by: Marek Belisko <[email protected]>

Quoted 31 lines

[0] https://lkml.org/lkml/2014/3/1/105

-- Sebastian

Sebastian Reichel (9):
mfd: twl4030-madc: Use managed resources
mfd: twl4030-madc: Add DT support and convert to IIO framework
mfd: twl4030-madc: Cleanup driver
mfd: twl-core: Add twl_i2c_read/write_u16
mfd: twl4030-madc: Use twl_i2c_read/write_u16 for 16 bit registers
Documentation: DT: Document twl4030-madc binding
mfd: twl4030-madc: Move driver to drivers/iio/adc
iio: documentation: Add ABI documentation for *_mean_raw
iio: inkern: add iio_read_channel_average_raw

 Documentation/ABI/testing/sysfs-bus-iio            |   8 +
 .../devicetree/bindings/iio/adc/twl4030-madc.txt   |  24 ++
 drivers/iio/adc/Kconfig                            |  10 +
 drivers/iio/adc/Makefile                           |   1 +
 drivers/{mfd => iio/adc}/twl4030-madc.c            | 299 +++++++++++++--------
 drivers/iio/inkern.c                               |  18 ++
 drivers/mfd/Kconfig                                |  10 -
 drivers/mfd/Makefile                               |   1 -
 include/linux/i2c/twl.h                            |  12 +
 include/linux/i2c/twl4030-madc.h                   |   2 +-
 include/linux/iio/consumer.h                       |  13 +
 11 files changed, 274 insertions(+), 124 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/iio/adc/twl4030-madc.txt
 rename drivers/{mfd => iio/adc}/twl4030-madc.c (75%)
Quoted 3 lines

--
1.9.0

[1]: http://www.spinics.net/lists/kernel/msg1700793.html

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
#87 lore raw

Re: [PATCHv2 0/9] Convert twl4030-madc to IIO API and add DT support

In reply to Sebastian Reichel
Quoted 4 lines

TODO:
* Wait for feedback from DT binding maintainers
* Wait for feedback from Marek Belisko, who is testing the changes with twl4030-madc-battery driver on GTA04

This patch-set has stalled.

Can you send a fresh patch-set (not linked to the old ones, as the thread is getting very complicated again) complete with all the Acks you have received?

Quoted 30 lines

[0] https://lkml.org/lkml/2014/3/1/105

-- Sebastian

Sebastian Reichel (9):
mfd: twl4030-madc: Use managed resources
mfd: twl4030-madc: Add DT support and convert to IIO framework
mfd: twl4030-madc: Cleanup driver
mfd: twl-core: Add twl_i2c_read/write_u16
mfd: twl4030-madc: Use twl_i2c_read/write_u16 for 16 bit registers
Documentation: DT: Document twl4030-madc binding
mfd: twl4030-madc: Move driver to drivers/iio/adc
iio: documentation: Add ABI documentation for *_mean_raw
iio: inkern: add iio_read_channel_average_raw

 Documentation/ABI/testing/sysfs-bus-iio            |   8 +
 .../devicetree/bindings/iio/adc/twl4030-madc.txt   |  24 ++
 drivers/iio/adc/Kconfig                            |  10 +
 drivers/iio/adc/Makefile                           |   1 +
 drivers/{mfd => iio/adc}/twl4030-madc.c            | 299 +++++++++++++--------
 drivers/iio/inkern.c                               |  18 ++
 drivers/mfd/Kconfig                                |  10 -
 drivers/mfd/Makefile                               |   1 -
 include/linux/i2c/twl.h                            |  12 +
 include/linux/i2c/twl4030-madc.h                   |   2 +-
 include/linux/iio/consumer.h                       |  13 +
 11 files changed, 274 insertions(+), 124 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/iio/adc/twl4030-madc.txt
 rename drivers/{mfd => iio/adc}/twl4030-madc.c (75%)
Signature · 4 lines
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
#88 lore raw

Re: [PATCHv2 0/9] Convert twl4030-madc to IIO API and add DT support

Quoted 6 lines · Lee Jones
Quoted 4 lines

TODO:
* Wait for feedback from DT binding maintainers
* Wait for feedback from Marek Belisko, who is testing the changes with twl4030-madc-battery driver on GTA04

This patch-set has stalled.

Right. I waited a few days for more feedback.

Quoted 3 lines

Can you send a fresh patch-set (not linked to the old ones, as the thread is getting very complicated again) complete with all the Acks you have received?

Sure. I will send it later.

-- Sebastian

Acked-by on v1 4/4
#89 lore raw

Re: [RFCv1 4/4] mfd: twl4030-madc: Move driver to drivers/iio/adc

In reply to Sebastian Reichel
Quoted 4 lines

This is a driver for an A/D converter, which belongs into drivers/iio/adc.

Quoted 10 lines
---
 drivers/iio/adc/Kconfig        |  10 +
 drivers/iio/adc/Makefile       |   1 +
 drivers/iio/adc/twl4030-madc.c | 922 +++++++++++++++++++++++++++++++++++++++++
 drivers/mfd/Kconfig            |  10 -
 drivers/mfd/Makefile           |   1 -
 drivers/mfd/twl4030-madc.c     | 922 -----------------------------------------
 6 files changed, 933 insertions(+), 933 deletions(-)
 create mode 100644 drivers/iio/adc/twl4030-madc.c
 delete mode 100644 drivers/mfd/twl4030-madc.c

For the MFD changes:

Signature · 4 lines
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
Acked-by on v1 1/4
#90 lore raw

Re: [RFCv1 1/4] mfd: twl4030-madc: Use managed resources

In reply to Sebastian Reichel
Quoted 6 lines

Update twl4030-madc driver to use managed resources.

---
 drivers/mfd/twl4030-madc.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
Signature · 4 lines
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
#91 lore raw
In reply to Sebastian Reichel

Hi Sebastian,

Quoted 6 lines · Sebastian Reichel

Hi Marek,

I have prepared a patchset, which adds DT bindings for twl4030-madc using the standard IIO DT API. I have not yet send the patchset, since I have not yet found the time to test the patchset. I will send them as RFC now.

Great. I'll look on that today evening and test on gta04 board. Thanks. I think my patch can be dropped :).

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