Linux IIO development
 help / color / mirror / Atom feed
* [PATCH 1/2] IIO-onwards: ADC: AD7780: Convert to new channel registration method
@ 2011-04-27  7:45 michael.hennerich
  2011-04-27  7:45 ` [PATCH 2/2] IIO-onwards: ADC: AD7780: Use private data space from iio_allocate_device michael.hennerich
  2011-04-27  8:39 ` [PATCH 1/2] IIO-onwards: ADC: AD7780: Convert to new channel registration method Jonathan Cameron
  0 siblings, 2 replies; 4+ messages in thread
From: michael.hennerich @ 2011-04-27  7:45 UTC (permalink / raw)
  To: jic23; +Cc: linux-iio, drivers, device-drivers-devel, Michael Hennerich

From: Michael Hennerich <michael.hennerich@analog.com>

Convert to new channel registration method

Signed-off-by: Michael Hennerich <michael.hennerich@analog.com>
---
 drivers/staging/iio/adc/ad7780.c |  113 +++++++++++++++++---------------------
 1 files changed, 51 insertions(+), 62 deletions(-)

diff --git a/drivers/staging/iio/adc/ad7780.c b/drivers/staging/iio/adc/ad7780.c
index f828e83..c000efc 100644
--- a/drivers/staging/iio/adc/ad7780.c
+++ b/drivers/staging/iio/adc/ad7780.c
@@ -36,9 +36,7 @@
 #define AD7780_PAT0	(1 << 0)
 
 struct ad7780_chip_info {
-	u8				bits;
-	u8				storagebits;
-	u8				res_shift;
+	struct iio_chan_spec		channel;
 };
 
 struct ad7780_state {
@@ -88,70 +86,59 @@ out:
 	return ret;
 }
 
-static ssize_t ad7780_scan(struct device *dev,
-			    struct device_attribute *attr,
-			    char *buf)
+static int ad7780_read_raw(struct iio_dev *indio_dev,
+			   struct iio_chan_spec const *chan,
+			   int *val,
+			   int *val2,
+			   long m)
 {
-	struct iio_dev *dev_info = dev_get_drvdata(dev);
-	struct ad7780_state *st = dev_info->dev_data;
-	int ret, val, smpl;
-
-	mutex_lock(&dev_info->mlock);
-	ret = ad7780_read(st, &smpl);
-	mutex_unlock(&dev_info->mlock);
-
-	if (ret < 0)
-		return ret;
-
-	if ((smpl & AD7780_ERR) ||
-		!((smpl & AD7780_PAT0) && !(smpl & AD7780_PAT1)))
-		return -EIO;
-
-	val = (smpl >> st->chip_info->res_shift) &
-		((1 << (st->chip_info->bits)) - 1);
-	val -= (1 << (st->chip_info->bits - 1));
-
-	if (!(smpl & AD7780_GAIN))
-		val *= 128;
-
-	return sprintf(buf, "%d\n", val);
-}
-static IIO_DEV_ATTR_IN_RAW(0, ad7780_scan, 0);
-
-static ssize_t ad7780_show_scale(struct device *dev,
-				struct device_attribute *attr,
-				char *buf)
-{
-	struct iio_dev *dev_info = dev_get_drvdata(dev);
-	struct ad7780_state *st = iio_dev_get_devdata(dev_info);
-	/* Corresponds to Vref / 2^(bits-1) */
-	unsigned int scale = (st->int_vref_mv * 100000) >>
-		(st->chip_info->bits - 1);
-
-	return sprintf(buf, "%d.%05d\n", scale / 100000, scale % 100000);
+	int ret, smpl;
+	struct ad7780_state *st = indio_dev->dev_data;
+	unsigned int scale_uv;
+
+	switch (m) {
+	case 0:
+		mutex_lock(&indio_dev->mlock);
+		ret = ad7780_read(st, &smpl);
+		mutex_unlock(&indio_dev->mlock);
+
+		if (ret < 0)
+			return ret;
+
+		if ((smpl & AD7780_ERR) ||
+			!((smpl & AD7780_PAT0) && !(smpl & AD7780_PAT1)))
+			return -EIO;
+
+		*val = (smpl >> st->chip_info->channel.scan_type.shift) &
+			((1 << (st->chip_info->channel.scan_type.realbits))
+			- 1);
+		*val -= (1 << (st->chip_info->channel.scan_type.realbits
+			- 1));
+
+		if (!(smpl & AD7780_GAIN))
+			*val *= 128;
+
+		return IIO_VAL_INT;
+	case (1 << IIO_CHAN_INFO_SCALE_SHARED):
+		scale_uv = (st->int_vref_mv * 100000)
+			>> (st->chip_info->channel.scan_type.realbits - 1);
+		*val =  scale_uv / 100000;
+		*val2 = (scale_uv % 100000) * 10;
+		return IIO_VAL_INT_PLUS_MICRO;
+	}
+	return -EINVAL;
 }
-static IIO_DEVICE_ATTR(in_scale, S_IRUGO, ad7780_show_scale, NULL, 0);
-
-static struct attribute *ad7780_attributes[] = {
-	&iio_dev_attr_in0_raw.dev_attr.attr,
-	&iio_dev_attr_in_scale.dev_attr.attr,
-	NULL,
-};
-
-static const struct attribute_group ad7780_attribute_group = {
-	.attrs = ad7780_attributes,
-};
 
 static const struct ad7780_chip_info ad7780_chip_info_tbl[] = {
 	[ID_AD7780] = {
-		.bits = 24,
-		.storagebits = 32,
-		.res_shift = 8,
+		.channel = IIO_CHAN_EV(IIO_IN, 0,
+			(1 << IIO_CHAN_INFO_SCALE_SHARED),
+			0, 0, IIO_ST('s', 24, 32, 8), 0),
 	},
 	[ID_AD7781] = {
-		.bits = 20,
-		.storagebits = 32,
-		.res_shift = 12,
+		.channel = IIO_CHAN_EV(IIO_IN, 0,
+			(1 << IIO_CHAN_INFO_SCALE_SHARED),
+			0, 0, IIO_ST('s', 20, 32, 12), 0),
 	},
 };
 
@@ -218,17 +205,19 @@ static int __devinit ad7780_probe(struct spi_device *spi)
 	/* Establish that the iio_dev is a child of the spi device */
 	st->indio_dev->dev.parent = &spi->dev;
 	st->indio_dev->name = spi_get_device_id(spi)->name;
-	st->indio_dev->attrs = &ad7780_attribute_group;
 	st->indio_dev->dev_data = (void *)(st);
 	st->indio_dev->driver_module = THIS_MODULE;
 	st->indio_dev->modes = INDIO_DIRECT_MODE;
+	st->indio_dev->channels = &st->chip_info->channel;
+	st->indio_dev->num_channels = 1;
+	st->indio_dev->read_raw = &ad7780_read_raw;
 
 	init_waitqueue_head(&st->wq_data_avail);
 
 	/* Setup default message */
 
 	st->xfer.rx_buf = &st->data;
-	st->xfer.len = st->chip_info->storagebits / 8;
+	st->xfer.len = st->chip_info->channel.scan_type.storagebits / 8;
 
 	spi_message_init(&st->msg);
 	spi_message_add_tail(&st->xfer, &st->msg);
-- 
1.6.0.2

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

* [PATCH 2/2] IIO-onwards: ADC: AD7780: Use private data space from iio_allocate_device
  2011-04-27  7:45 [PATCH 1/2] IIO-onwards: ADC: AD7780: Convert to new channel registration method michael.hennerich
@ 2011-04-27  7:45 ` michael.hennerich
  2011-04-27  8:41   ` Jonathan Cameron
  2011-04-27  8:39 ` [PATCH 1/2] IIO-onwards: ADC: AD7780: Convert to new channel registration method Jonathan Cameron
  1 sibling, 1 reply; 4+ messages in thread
From: michael.hennerich @ 2011-04-27  7:45 UTC (permalink / raw)
  To: jic23; +Cc: linux-iio, drivers, device-drivers-devel, Michael Hennerich

From: Michael Hennerich <michael.hennerich@analog.com>

Use private data space from iio_allocate_device
Drop dev_data in favour of iio_priv()
Fix typo in gpio name
Fix error return path, free gpio
Make scale_uv type unsigned long

Signed-off-by: Michael Hennerich <michael.hennerich@analog.com>
---
 drivers/staging/iio/adc/ad7780.c |   80 +++++++++++++++++---------------------
 1 files changed, 36 insertions(+), 44 deletions(-)

diff --git a/drivers/staging/iio/adc/ad7780.c b/drivers/staging/iio/adc/ad7780.c
index c000efc..e68f2c2 100644
--- a/drivers/staging/iio/adc/ad7780.c
+++ b/drivers/staging/iio/adc/ad7780.c
@@ -40,7 +40,6 @@ struct ad7780_chip_info {
 };
 
 struct ad7780_state {
-	struct iio_dev			*indio_dev;
 	struct spi_device		*spi;
 	const struct ad7780_chip_info	*chip_info;
 	struct regulator		*reg;
@@ -92,9 +91,10 @@ static int ad7780_read_raw(struct iio_dev *indio_dev,
 			   int *val2,
 			   long m)
 {
-	int ret, smpl;
-	struct ad7780_state *st = indio_dev->dev_data;
-	unsigned int scale_uv;
+	struct ad7780_state *st = iio_priv(indio_dev);
+	struct iio_chan_spec channel = st->chip_info->channel;
+	int ret, smpl = 0;
+	unsigned long scale_uv;
 
 	switch (m) {
 	case 0:
@@ -109,11 +109,9 @@ static int ad7780_read_raw(struct iio_dev *indio_dev,
 			!((smpl & AD7780_PAT0) && !(smpl & AD7780_PAT1)))
 			return -EIO;
 
-		*val = (smpl >> st->chip_info->channel.scan_type.shift) &
-			((1 << (st->chip_info->channel.scan_type.realbits))
-			- 1);
-		*val -= (1 << (st->chip_info->channel.scan_type.realbits
-			- 1));
+		*val = (smpl >> channel.scan_type.shift) &
+			((1 << (channel.scan_type.realbits)) - 1);
+		*val -= (1 << (channel.scan_type.realbits - 1));
 
 		if (!(smpl & AD7780_GAIN))
 			*val *= 128;
@@ -121,7 +119,7 @@ static int ad7780_read_raw(struct iio_dev *indio_dev,
 		return IIO_VAL_INT;
 	case (1 << IIO_CHAN_INFO_SCALE_SHARED):
 		scale_uv = (st->int_vref_mv * 100000)
-			>> (st->chip_info->channel.scan_type.realbits - 1);
+			>> (channel.scan_type.realbits - 1);
 		*val =  scale_uv / 100000;
 		*val2 = (scale_uv % 100000) * 10;
 		return IIO_VAL_INT_PLUS_MICRO;
@@ -159,6 +157,7 @@ static int __devinit ad7780_probe(struct spi_device *spi)
 {
 	struct ad7780_platform_data *pdata = spi->dev.platform_data;
 	struct ad7780_state *st;
+	struct iio_dev *indio_dev;
 	int ret, voltage_uv = 0;
 
 	if (!pdata) {
@@ -166,11 +165,11 @@ static int __devinit ad7780_probe(struct spi_device *spi)
 		return -ENODEV;
 	}
 
-	st = kzalloc(sizeof(*st), GFP_KERNEL);
-	if (st == NULL) {
-		ret = -ENOMEM;
-		goto error_ret;
-	}
+	indio_dev = iio_allocate_device(sizeof(*st));
+	if (indio_dev == NULL)
+		return -ENOMEM;
+
+	st = iio_priv(indio_dev);
 
 	st->reg = regulator_get(&spi->dev, "vcc");
 	if (!IS_ERR(st->reg)) {
@@ -193,24 +192,16 @@ static int __devinit ad7780_probe(struct spi_device *spi)
 	else
 		dev_warn(&spi->dev, "reference voltage unspecified\n");
 
-	spi_set_drvdata(spi, st);
+	spi_set_drvdata(spi, indio_dev);
 	st->spi = spi;
 
-	st->indio_dev = iio_allocate_device(0);
-	if (st->indio_dev == NULL) {
-		ret = -ENOMEM;
-		goto error_disable_reg;
-	}
-
-	/* Establish that the iio_dev is a child of the spi device */
-	st->indio_dev->dev.parent = &spi->dev;
-	st->indio_dev->name = spi_get_device_id(spi)->name;
-	st->indio_dev->dev_data = (void *)(st);
-	st->indio_dev->driver_module = THIS_MODULE;
-	st->indio_dev->modes = INDIO_DIRECT_MODE;
-	st->indio_dev->channels = &st->chip_info->channel;
-	st->indio_dev->num_channels = 1;
-	st->indio_dev->read_raw = &ad7780_read_raw;
+	indio_dev->dev.parent = &spi->dev;
+	indio_dev->name = spi_get_device_id(spi)->name;
+	indio_dev->driver_module = THIS_MODULE;
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->channels = &st->chip_info->channel;
+	indio_dev->num_channels = 1;
+	indio_dev->read_raw = &ad7780_read_raw;
 
 	init_waitqueue_head(&st->wq_data_avail);
 
@@ -223,20 +214,20 @@ static int __devinit ad7780_probe(struct spi_device *spi)
 	spi_message_add_tail(&st->xfer, &st->msg);
 
 	ret = gpio_request_one(st->pdata->gpio_pdrst, GPIOF_OUT_INIT_LOW,
-			       "AD7877 /PDRST");
+			       "AD7780 /PDRST");
 	if (ret) {
 		dev_err(&spi->dev, "failed to request GPIO PDRST\n");
-		goto error_free_device;
+		goto error_disable_reg;
 	}
 
 	ret = request_irq(spi->irq, ad7780_interrupt,
 		IRQF_TRIGGER_FALLING, spi_get_device_id(spi)->name, st);
 	if (ret)
-		goto error_free_device;
+		goto error_free_gpio;
 
 	disable_irq(spi->irq);
 
-	ret = iio_device_register(st->indio_dev);
+	ret = iio_device_register(indio_dev);
 	if (ret)
 		goto error_free_irq;
 
@@ -244,32 +235,33 @@ static int __devinit ad7780_probe(struct spi_device *spi)
 
 error_free_irq:
 	free_irq(spi->irq, st);
-
-error_free_device:
-	iio_free_device(st->indio_dev);
+error_free_gpio:
+	gpio_free(st->pdata->gpio_pdrst);
 error_disable_reg:
 	if (!IS_ERR(st->reg))
 		regulator_disable(st->reg);
 error_put_reg:
 	if (!IS_ERR(st->reg))
 		regulator_put(st->reg);
-	kfree(st);
-error_ret:
+
+	iio_free_device(indio_dev);
+
 	return ret;
 }
 
 static int ad7780_remove(struct spi_device *spi)
 {
-	struct ad7780_state *st = spi_get_drvdata(spi);
-	struct iio_dev *indio_dev = st->indio_dev;
+	struct iio_dev *indio_dev = spi_get_drvdata(spi);
+	struct ad7780_state *st = iio_priv(indio_dev);
+
 	free_irq(spi->irq, st);
 	gpio_free(st->pdata->gpio_pdrst);
-	iio_device_unregister(indio_dev);
 	if (!IS_ERR(st->reg)) {
 		regulator_disable(st->reg);
 		regulator_put(st->reg);
 	}
-	kfree(st);
+	iio_device_unregister(indio_dev);
+
 	return 0;
 }
 
-- 
1.6.0.2

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

* Re: [PATCH 1/2] IIO-onwards: ADC: AD7780: Convert to new channel registration method
  2011-04-27  7:45 [PATCH 1/2] IIO-onwards: ADC: AD7780: Convert to new channel registration method michael.hennerich
  2011-04-27  7:45 ` [PATCH 2/2] IIO-onwards: ADC: AD7780: Use private data space from iio_allocate_device michael.hennerich
@ 2011-04-27  8:39 ` Jonathan Cameron
  1 sibling, 0 replies; 4+ messages in thread
From: Jonathan Cameron @ 2011-04-27  8:39 UTC (permalink / raw)
  To: michael.hennerich; +Cc: linux-iio, drivers, device-drivers-devel

On 04/27/11 08:45, michael.hennerich@analog.com wrote:
> From: Michael Hennerich <michael.hennerich@analog.com>
> 
> Convert to new channel registration method
> 
Looks good. Will merge into iio-onwards.
Thanks,
> Signed-off-by: Michael Hennerich <michael.hennerich@analog.com>
Acked-by: Jonathan Cameron <jic23@cam.ac.uk>
> ---
>  drivers/staging/iio/adc/ad7780.c |  113 +++++++++++++++++---------------------
>  1 files changed, 51 insertions(+), 62 deletions(-)
> 
> diff --git a/drivers/staging/iio/adc/ad7780.c b/drivers/staging/iio/adc/ad7780.c
> index f828e83..c000efc 100644
> --- a/drivers/staging/iio/adc/ad7780.c
> +++ b/drivers/staging/iio/adc/ad7780.c
> @@ -36,9 +36,7 @@
>  #define AD7780_PAT0	(1 << 0)
>  
>  struct ad7780_chip_info {
> -	u8				bits;
> -	u8				storagebits;
> -	u8				res_shift;
> +	struct iio_chan_spec		channel;
>  };
>  
>  struct ad7780_state {
> @@ -88,70 +86,59 @@ out:
>  	return ret;
>  }
>  
> -static ssize_t ad7780_scan(struct device *dev,
> -			    struct device_attribute *attr,
> -			    char *buf)
> +static int ad7780_read_raw(struct iio_dev *indio_dev,
> +			   struct iio_chan_spec const *chan,
> +			   int *val,
> +			   int *val2,
> +			   long m)
>  {
> -	struct iio_dev *dev_info = dev_get_drvdata(dev);
> -	struct ad7780_state *st = dev_info->dev_data;
> -	int ret, val, smpl;
> -
> -	mutex_lock(&dev_info->mlock);
> -	ret = ad7780_read(st, &smpl);
> -	mutex_unlock(&dev_info->mlock);
> -
> -	if (ret < 0)
> -		return ret;
> -
> -	if ((smpl & AD7780_ERR) ||
> -		!((smpl & AD7780_PAT0) && !(smpl & AD7780_PAT1)))
> -		return -EIO;
> -
> -	val = (smpl >> st->chip_info->res_shift) &
> -		((1 << (st->chip_info->bits)) - 1);
> -	val -= (1 << (st->chip_info->bits - 1));
> -
> -	if (!(smpl & AD7780_GAIN))
> -		val *= 128;
> -
> -	return sprintf(buf, "%d\n", val);
> -}
> -static IIO_DEV_ATTR_IN_RAW(0, ad7780_scan, 0);
> -
> -static ssize_t ad7780_show_scale(struct device *dev,
> -				struct device_attribute *attr,
> -				char *buf)
> -{
> -	struct iio_dev *dev_info = dev_get_drvdata(dev);
> -	struct ad7780_state *st = iio_dev_get_devdata(dev_info);
> -	/* Corresponds to Vref / 2^(bits-1) */
> -	unsigned int scale = (st->int_vref_mv * 100000) >>
> -		(st->chip_info->bits - 1);
> -
> -	return sprintf(buf, "%d.%05d\n", scale / 100000, scale % 100000);
> +	int ret, smpl;
> +	struct ad7780_state *st = indio_dev->dev_data;
> +	unsigned int scale_uv;
> +
> +	switch (m) {
> +	case 0:
> +		mutex_lock(&indio_dev->mlock);
> +		ret = ad7780_read(st, &smpl);
> +		mutex_unlock(&indio_dev->mlock);
> +
> +		if (ret < 0)
> +			return ret;
> +
> +		if ((smpl & AD7780_ERR) ||
> +			!((smpl & AD7780_PAT0) && !(smpl & AD7780_PAT1)))
> +			return -EIO;
> +
> +		*val = (smpl >> st->chip_info->channel.scan_type.shift) &
> +			((1 << (st->chip_info->channel.scan_type.realbits))
> +			- 1);
> +		*val -= (1 << (st->chip_info->channel.scan_type.realbits
> +			- 1));
> +
> +		if (!(smpl & AD7780_GAIN))
> +			*val *= 128;
> +
> +		return IIO_VAL_INT;
> +	case (1 << IIO_CHAN_INFO_SCALE_SHARED):
> +		scale_uv = (st->int_vref_mv * 100000)
> +			>> (st->chip_info->channel.scan_type.realbits - 1);
> +		*val =  scale_uv / 100000;
> +		*val2 = (scale_uv % 100000) * 10;
> +		return IIO_VAL_INT_PLUS_MICRO;
> +	}
> +	return -EINVAL;
>  }
> -static IIO_DEVICE_ATTR(in_scale, S_IRUGO, ad7780_show_scale, NULL, 0);
> -
> -static struct attribute *ad7780_attributes[] = {
> -	&iio_dev_attr_in0_raw.dev_attr.attr,
> -	&iio_dev_attr_in_scale.dev_attr.attr,
> -	NULL,
> -};
> -
> -static const struct attribute_group ad7780_attribute_group = {
> -	.attrs = ad7780_attributes,
> -};
>  
>  static const struct ad7780_chip_info ad7780_chip_info_tbl[] = {
>  	[ID_AD7780] = {
> -		.bits = 24,
> -		.storagebits = 32,
> -		.res_shift = 8,
> +		.channel = IIO_CHAN_EV(IIO_IN, 0,
> +			(1 << IIO_CHAN_INFO_SCALE_SHARED),
> +			0, 0, IIO_ST('s', 24, 32, 8), 0),
>  	},
>  	[ID_AD7781] = {
> -		.bits = 20,
> -		.storagebits = 32,
> -		.res_shift = 12,
> +		.channel = IIO_CHAN_EV(IIO_IN, 0,
> +			(1 << IIO_CHAN_INFO_SCALE_SHARED),
> +			0, 0, IIO_ST('s', 20, 32, 12), 0),
>  	},
>  };
>  
> @@ -218,17 +205,19 @@ static int __devinit ad7780_probe(struct spi_device *spi)
>  	/* Establish that the iio_dev is a child of the spi device */
>  	st->indio_dev->dev.parent = &spi->dev;
>  	st->indio_dev->name = spi_get_device_id(spi)->name;
> -	st->indio_dev->attrs = &ad7780_attribute_group;
>  	st->indio_dev->dev_data = (void *)(st);
>  	st->indio_dev->driver_module = THIS_MODULE;
>  	st->indio_dev->modes = INDIO_DIRECT_MODE;
> +	st->indio_dev->channels = &st->chip_info->channel;
> +	st->indio_dev->num_channels = 1;
> +	st->indio_dev->read_raw = &ad7780_read_raw;
>  
>  	init_waitqueue_head(&st->wq_data_avail);
>  
>  	/* Setup default message */
>  
>  	st->xfer.rx_buf = &st->data;
> -	st->xfer.len = st->chip_info->storagebits / 8;
> +	st->xfer.len = st->chip_info->channel.scan_type.storagebits / 8;
>  
>  	spi_message_init(&st->msg);
>  	spi_message_add_tail(&st->xfer, &st->msg);


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

* Re: [PATCH 2/2] IIO-onwards: ADC: AD7780: Use private data space from iio_allocate_device
  2011-04-27  7:45 ` [PATCH 2/2] IIO-onwards: ADC: AD7780: Use private data space from iio_allocate_device michael.hennerich
@ 2011-04-27  8:41   ` Jonathan Cameron
  0 siblings, 0 replies; 4+ messages in thread
From: Jonathan Cameron @ 2011-04-27  8:41 UTC (permalink / raw)
  To: michael.hennerich; +Cc: linux-iio, drivers, device-drivers-devel

On 04/27/11 08:45, michael.hennerich@analog.com wrote:
> From: Michael Hennerich <michael.hennerich@analog.com>
> 
> Use private data space from iio_allocate_device
> Drop dev_data in favour of iio_priv()
> Fix typo in gpio name
> Fix error return path, free gpio
> Make scale_uv type unsigned long
Nitpick - title of patch should mention something like 'and misc trivial fixes'
Or even better would be to have them in a separate patch...
(not that I can talk given some of my recent patches!)
> 
> Signed-off-by: Michael Hennerich <michael.hennerich@analog.com>
Acked-by: Jonathan Cameron <jic23@cam.ac.uk>
> ---
>  drivers/staging/iio/adc/ad7780.c |   80 +++++++++++++++++---------------------
>  1 files changed, 36 insertions(+), 44 deletions(-)
> 
> diff --git a/drivers/staging/iio/adc/ad7780.c b/drivers/staging/iio/adc/ad7780.c
> index c000efc..e68f2c2 100644
> --- a/drivers/staging/iio/adc/ad7780.c
> +++ b/drivers/staging/iio/adc/ad7780.c
> @@ -40,7 +40,6 @@ struct ad7780_chip_info {
>  };
>  
>  struct ad7780_state {
> -	struct iio_dev			*indio_dev;
>  	struct spi_device		*spi;
>  	const struct ad7780_chip_info	*chip_info;
>  	struct regulator		*reg;
> @@ -92,9 +91,10 @@ static int ad7780_read_raw(struct iio_dev *indio_dev,
>  			   int *val2,
>  			   long m)
>  {
> -	int ret, smpl;
> -	struct ad7780_state *st = indio_dev->dev_data;
> -	unsigned int scale_uv;
> +	struct ad7780_state *st = iio_priv(indio_dev);
> +	struct iio_chan_spec channel = st->chip_info->channel;
> +	int ret, smpl = 0;
> +	unsigned long scale_uv;
>  
>  	switch (m) {
>  	case 0:
> @@ -109,11 +109,9 @@ static int ad7780_read_raw(struct iio_dev *indio_dev,
>  			!((smpl & AD7780_PAT0) && !(smpl & AD7780_PAT1)))
>  			return -EIO;
>  
> -		*val = (smpl >> st->chip_info->channel.scan_type.shift) &
> -			((1 << (st->chip_info->channel.scan_type.realbits))
> -			- 1);
> -		*val -= (1 << (st->chip_info->channel.scan_type.realbits
> -			- 1));
> +		*val = (smpl >> channel.scan_type.shift) &
> +			((1 << (channel.scan_type.realbits)) - 1);
> +		*val -= (1 << (channel.scan_type.realbits - 1));
>  
>  		if (!(smpl & AD7780_GAIN))
>  			*val *= 128;
> @@ -121,7 +119,7 @@ static int ad7780_read_raw(struct iio_dev *indio_dev,
>  		return IIO_VAL_INT;
>  	case (1 << IIO_CHAN_INFO_SCALE_SHARED):
>  		scale_uv = (st->int_vref_mv * 100000)
> -			>> (st->chip_info->channel.scan_type.realbits - 1);
> +			>> (channel.scan_type.realbits - 1);
>  		*val =  scale_uv / 100000;
>  		*val2 = (scale_uv % 100000) * 10;
>  		return IIO_VAL_INT_PLUS_MICRO;
> @@ -159,6 +157,7 @@ static int __devinit ad7780_probe(struct spi_device *spi)
>  {
>  	struct ad7780_platform_data *pdata = spi->dev.platform_data;
>  	struct ad7780_state *st;
> +	struct iio_dev *indio_dev;
>  	int ret, voltage_uv = 0;
>  
>  	if (!pdata) {
> @@ -166,11 +165,11 @@ static int __devinit ad7780_probe(struct spi_device *spi)
>  		return -ENODEV;
>  	}
>  
> -	st = kzalloc(sizeof(*st), GFP_KERNEL);
> -	if (st == NULL) {
> -		ret = -ENOMEM;
> -		goto error_ret;
> -	}
> +	indio_dev = iio_allocate_device(sizeof(*st));
> +	if (indio_dev == NULL)
> +		return -ENOMEM;
> +
> +	st = iio_priv(indio_dev);
>  
>  	st->reg = regulator_get(&spi->dev, "vcc");
>  	if (!IS_ERR(st->reg)) {
> @@ -193,24 +192,16 @@ static int __devinit ad7780_probe(struct spi_device *spi)
>  	else
>  		dev_warn(&spi->dev, "reference voltage unspecified\n");
>  
> -	spi_set_drvdata(spi, st);
> +	spi_set_drvdata(spi, indio_dev);
>  	st->spi = spi;
>  
> -	st->indio_dev = iio_allocate_device(0);
> -	if (st->indio_dev == NULL) {
> -		ret = -ENOMEM;
> -		goto error_disable_reg;
> -	}
> -
> -	/* Establish that the iio_dev is a child of the spi device */
> -	st->indio_dev->dev.parent = &spi->dev;
> -	st->indio_dev->name = spi_get_device_id(spi)->name;
> -	st->indio_dev->dev_data = (void *)(st);
> -	st->indio_dev->driver_module = THIS_MODULE;
> -	st->indio_dev->modes = INDIO_DIRECT_MODE;
> -	st->indio_dev->channels = &st->chip_info->channel;
> -	st->indio_dev->num_channels = 1;
> -	st->indio_dev->read_raw = &ad7780_read_raw;
> +	indio_dev->dev.parent = &spi->dev;
> +	indio_dev->name = spi_get_device_id(spi)->name;
> +	indio_dev->driver_module = THIS_MODULE;
> +	indio_dev->modes = INDIO_DIRECT_MODE;
> +	indio_dev->channels = &st->chip_info->channel;
> +	indio_dev->num_channels = 1;
> +	indio_dev->read_raw = &ad7780_read_raw;
>  
>  	init_waitqueue_head(&st->wq_data_avail);
>  
> @@ -223,20 +214,20 @@ static int __devinit ad7780_probe(struct spi_device *spi)
>  	spi_message_add_tail(&st->xfer, &st->msg);
>  
>  	ret = gpio_request_one(st->pdata->gpio_pdrst, GPIOF_OUT_INIT_LOW,
> -			       "AD7877 /PDRST");
> +			       "AD7780 /PDRST");
>  	if (ret) {
>  		dev_err(&spi->dev, "failed to request GPIO PDRST\n");
> -		goto error_free_device;
> +		goto error_disable_reg;
>  	}
>  
>  	ret = request_irq(spi->irq, ad7780_interrupt,
>  		IRQF_TRIGGER_FALLING, spi_get_device_id(spi)->name, st);
>  	if (ret)
> -		goto error_free_device;
> +		goto error_free_gpio;
>  
>  	disable_irq(spi->irq);
>  
> -	ret = iio_device_register(st->indio_dev);
> +	ret = iio_device_register(indio_dev);
>  	if (ret)
>  		goto error_free_irq;
>  
> @@ -244,32 +235,33 @@ static int __devinit ad7780_probe(struct spi_device *spi)
>  
>  error_free_irq:
>  	free_irq(spi->irq, st);
> -
> -error_free_device:
> -	iio_free_device(st->indio_dev);
> +error_free_gpio:
> +	gpio_free(st->pdata->gpio_pdrst);
>  error_disable_reg:
>  	if (!IS_ERR(st->reg))
>  		regulator_disable(st->reg);
>  error_put_reg:
>  	if (!IS_ERR(st->reg))
>  		regulator_put(st->reg);
> -	kfree(st);
> -error_ret:
> +
> +	iio_free_device(indio_dev);
> +
>  	return ret;
>  }
>  
>  static int ad7780_remove(struct spi_device *spi)
>  {
> -	struct ad7780_state *st = spi_get_drvdata(spi);
> -	struct iio_dev *indio_dev = st->indio_dev;
> +	struct iio_dev *indio_dev = spi_get_drvdata(spi);
> +	struct ad7780_state *st = iio_priv(indio_dev);
> +
>  	free_irq(spi->irq, st);
>  	gpio_free(st->pdata->gpio_pdrst);
> -	iio_device_unregister(indio_dev);
>  	if (!IS_ERR(st->reg)) {
>  		regulator_disable(st->reg);
>  		regulator_put(st->reg);
>  	}
> -	kfree(st);
> +	iio_device_unregister(indio_dev);
> +
>  	return 0;
>  }
>  


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

end of thread, other threads:[~2011-04-27  8:39 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-04-27  7:45 [PATCH 1/2] IIO-onwards: ADC: AD7780: Convert to new channel registration method michael.hennerich
2011-04-27  7:45 ` [PATCH 2/2] IIO-onwards: ADC: AD7780: Use private data space from iio_allocate_device michael.hennerich
2011-04-27  8:41   ` Jonathan Cameron
2011-04-27  8:39 ` [PATCH 1/2] IIO-onwards: ADC: AD7780: Convert to new channel registration method Jonathan Cameron

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox