iio: adc: add driver for MCP3204/08 12-bit ADC
PATCH v2 1/1 iio: adc: add driver for MCP3204/08 12-bit ADC
This adds support for Microchip's 12 bit AD converters MCP3204 and MCP3208. These chips communicates over SPI and supports single-ended and pseudo-differential configurations.
--- drivers/iio/adc/Kconfig | 10 ++ drivers/iio/adc/Makefile | 1 + drivers/iio/adc/mcp320x.c | 255 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 266 insertions(+) create mode 100644 drivers/iio/adc/mcp320x.c
drivers/iio/adc/Kconfig +10 -0
@@ -133,6 +133,16 @@ config MAX1363 max11646, max11647) Provides direct access via sysfs and buffered data via the iio dev interface. +config MCP320X + tristate "Microchip Technology MCP3204/08" + depends on SPI + help + Say yes here to build support for Microchip Technology's MCP3204 or + MCP3208 analog to digital converter. + + This driver can also be built as a module. If so, the module will be + called mcp320x. + config TI_ADC081C tristate "Texas Instruments ADC081C021/027" depends on I2C
drivers/iio/adc/Makefile +1 -0
@@ -14,6 +14,7 @@ obj-$(CONFIG_AT91_ADC) += at91_adc.o obj-$(CONFIG_EXYNOS_ADC) += exynos_adc.o obj-$(CONFIG_LP8788_ADC) += lp8788_adc.o obj-$(CONFIG_MAX1363) += max1363.o +obj-$(CONFIG_MCP320X) += mcp320x.o obj-$(CONFIG_TI_ADC081C) += ti-adc081c.o obj-$(CONFIG_TI_AM335X_ADC) += ti_am335x_adc.o obj-$(CONFIG_VIPERBOARD_ADC) += viperboard_adc.o
drivers/iio/adc/mcp320x.c new file +255 -0
255 lines not on this page · raw message
Signature · 1 line
1.8.1.5
Quoted 7 lines · Oskar Andero
This adds support for Microchip's 12 bit AD converters MCP3204 and MCP3208. These chips communicates over SPI and supports single-ended and pseudo-differential configurations.
Cc Jonathan Cameron <[email protected]>Cc Lars-Peter Clausen <[email protected]>Signed-off-by Oskar Andero <[email protected]>
Looks good, except some issues with probe error path handling
Quoted 7 lines
--- drivers/iio/adc/Kconfig | 10 ++ drivers/iio/adc/Makefile | 1 + drivers/iio/adc/mcp320x.c | 255 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 266 insertions(+) create mode 100644 drivers/iio/adc/mcp320x.c
[...]
Quoted 4 lines
drivers/iio/adc/mcp320x.c new file +4
-0+static int mcp320x_read_raw (struct iio_dev *indio_dev, +struct iio_chan_spec const *channel,int *val, +int *val2,long mask) +{
[...]
drivers/iio/adc/mcp320x.c line 87
Quoted 9 lines
drivers/iio/adc/mcp320x.c new file +9
-0+case IIO_CHAN_INFO_SCALE : +/* Digital output code = (4096 * Vin) / Vref */ + ret =regulator_get_voltage (adc->reg); +if (ret <0 ) + goto out; + + *val = ret /1000 ; + *val2 =4096 ; + ret =IIO_VAL_FRACTIONAL ;
You can use IIO_VAL_FRACTIONAL_LOG2 here, it is slightly more efficient and take the log2 of the divisor for val2, e.g. 12 in this case.
Quoted 12 lines
drivers/iio/adc/mcp320x.c new file +12
-0+break ; + +default : +break ; + } + +out: +mutex_unlock (&adc->lock); + +return ret; +} +
[...]
Quoted 23 lines
drivers/iio/adc/mcp320x.c new file +23
-0+static int mcp320x_probe (struct spi_device *spi) +{ +struct iio_dev *indio_dev; +struct mcp320x *adc; +const struct mcp3208_chip_info *chip_info; +int ret; + + indio_dev =iio_device_alloc (sizeof (*adc)); +if (!indio_dev) +return -ENOMEM ; + + adc =iio_priv (indio_dev); + adc->spi = spi; + + indio_dev->dev.parent = &spi->dev; + indio_dev->name =spi_get_device_id (spi)->name; + indio_dev->modes =INDIO_DIRECT_MODE ; + indio_dev->info = &mcp320x_info; + + chip_info = &mcp3208_chip_infos[spi_get_device_id (spi)->driver_data]; + indio_dev->channels = chip_info->channels; + indio_dev->num_channels = chip_info->num_channels; +
drivers/iio/adc/mcp320x.c line 199
Quoted 10 lines
drivers/iio/adc/mcp320x.c new file +10
-0+ adc->transfer[0 ].tx_buf = &adc->tx_buf; + adc->transfer[0 ].len =sizeof (adc->tx_buf); + adc->transfer[1 ].rx_buf = adc->rx_buf; + adc->transfer[1 ].len =sizeof (adc->rx_buf); + +spi_message_init_with_transfers (&adc->msg, adc->transfer, +ARRAY_SIZE (adc->transfer)); + + adc->reg =regulator_get (&spi->dev,"vref" ); +if (IS_ERR (adc->reg))
You need to free the iio device in this case
Quoted 9 lines
drivers/iio/adc/mcp320x.c new file +9
-0+return PTR_ERR (adc->reg); + + ret =regulator_enable (adc->reg); +if (ret <0 ) + goto reg_free; + +mutex_init (&adc->lock); + + ret =iio_device_register (indio_dev);
drivers/iio/adc/mcp320x.c line 218
Quoted 10 lines
drivers/iio/adc/mcp320x.c new file +10
-0+if (ret <0 ) + goto iio_free; + +return 0 ; + +iio_free: +iio_device_free (indio_dev); +reg_free: +regulator_put (adc->reg); +
I think the order go reversed here, e.g. if you jump to reg_free you don't free the IIO device. And you should also disable the regulator again in case iio_device_register fails.
Quoted 2 lines
(quoted diff) +2
-0+ return ret; +}