From mboxrd@z Thu Jan 1 00:00:00 1970 From: Jan Luebbe Subject: [PATCH 2/2] iio: adc: ti_am335x_adc: make tiadc_read_raw() more robust Date: Mon, 6 May 2013 14:48:59 +0200 Message-ID: <1367844539-6812-3-git-send-email-jlu@pengutronix.de> References: <1351698938-3839-1-git-send-email-panto@antoniou-consulting.com> <1367844539-6812-1-git-send-email-jlu@pengutronix.de> Return-path: In-Reply-To: <1367844539-6812-1-git-send-email-jlu-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org> Sender: linux-iio-owner-u79uwXL29TY76Z2rM5mHXA@public.gmane.org To: Pantelis Antoniou Cc: rachna-l0cyMroinI0@public.gmane.org, linux-iio-u79uwXL29TY76Z2rM5mHXA@public.gmane.org, linux-omap-u79uwXL29TY76Z2rM5mHXA@public.gmane.org, Koen Kooi , Jan Luebbe List-Id: linux-omap@vger.kernel.org Check that mask is actually IIO_CHAN_INFO_RAW. Also handle the case where not enough data is in the fifo. Signed-off-by: Jan Luebbe --- drivers/iio/adc/ti_am335x_adc.c | 48 ++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/drivers/iio/adc/ti_am335x_adc.c b/drivers/iio/adc/ti_am335x_adc.c index 620cc0c..4b764a9 100644 --- a/drivers/iio/adc/ti_am335x_adc.c +++ b/drivers/iio/adc/ti_am335x_adc.c @@ -157,26 +157,34 @@ static int tiadc_read_raw(struct iio_dev *indio_dev, int i; unsigned int fifo1count, readx1; - /* - * When the sub-system is first enabled, - * the sequencer will always start with the - * lowest step (1) and continue until step (16). - * For ex: If we have enabled 4 ADC channels and - * currently use only 1 out of them, the - * sequencer still configures all the 4 steps, - * leading to 3 unwanted data. - * Hence we need to flush out this data. - */ + switch (mask) { + case IIO_CHAN_INFO_RAW: + /* + * When the sub-system is first enabled, + * the sequencer will always start with the + * lowest step (1) and continue until step (16). + * For ex: If we have enabled 4 ADC channels and + * currently use only 1 out of them, the + * sequencer still configures all the 4 steps, + * leading to 3 unwanted data. + * Hence we need to flush out this data. + */ + + fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT); + for (i = 0; i < fifo1count; i++) { + readx1 = tiadc_readl(adc_dev, REG_FIFO1); + if (i == chan->channel) + *val = readx1 & 0xfff; + } + tiadc_writel(adc_dev, REG_SE, STPENB_STEPENB); - fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT); - for (i = 0; i < fifo1count; i++) { - readx1 = tiadc_readl(adc_dev, REG_FIFO1); - if (i == chan->channel) - *val = readx1 & 0xfff; - } - tiadc_writel(adc_dev, REG_SE, STPENB_STEPENB); + if (fifo1count <= chan->channel) + return -EINVAL; - return IIO_VAL_INT; + return IIO_VAL_INT; + default: + return -EINVAL; + } } static const struct iio_info tiadc_info = { @@ -213,7 +221,7 @@ static int tiadc_probe(struct platform_device *pdev) else { node = of_get_child_by_name(node, "adc"); if (!node) - return -EINVAL; + return -EINVAL; else { err = of_property_read_u32(node, "ti,adc-channels", &val32); @@ -310,7 +318,7 @@ static const struct dev_pm_ops tiadc_pm_ops = { static struct platform_driver tiadc_driver = { .driver = { - .name = "tiadc", + .name = "tiadc", .owner = THIS_MODULE, .pm = TIADC_PM_OPS, }, -- 1.8.2.rc2