From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E4E3BC27C4F for ; Sun, 23 Jun 2024 15:23:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: Content-Type:MIME-Version:References:In-Reply-To:Message-ID:Subject:Cc:To: From:Date:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=bc3TvfASN8v/Wsh6xCmdmGj4Q2J+iv4ylBcjsqON06s=; b=kVriMXSFcchdL4hHfYagGomIVZ zzr+aVo/kZnDg/Y/2+FIZ0XVNLbRIBaEK+4aHvHw3gnttGXKq4QNuxLbnHYBGUb0rGkBv4SQuhpec pSFagEicrph/9GR+b9LnmxxBGxuF6o6syVMTPI5at6fUs49SYohiDkBxr4q7gjzv3w2mlcA7YyuDu /rrUDSjlT26SPp9sj7b7qOIXJY5c3CIt7u6z+SPR0CT+cCw2rlaBu4TrwHpKW/480gGEIIXVqA7J/ wdGPTORJjVD5uYUjPgMkRb70P63CkuDi0mceddRTIe8SHGZkOs1mPfMUaINylz3UJ0dOPvt09oWPa 3hgFflhQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sLP3q-0000000EHif-2mCs; Sun, 23 Jun 2024 15:23:23 +0000 Received: from sin.source.kernel.org ([145.40.73.55]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sLP1w-0000000EH6K-1DLS for linux-arm-kernel@lists.infradead.org; Sun, 23 Jun 2024 15:22:18 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id DBD2ACE0F05; Sun, 23 Jun 2024 15:21:19 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D15F4C2BD10; Sun, 23 Jun 2024 15:21:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1719156079; bh=8R5yRhx5HD3Qdpebye/X5fiOJqmtxqaT2hxCrl24IRg=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=NH88dh1lxv1ZgGw2LykIVmIsCvg40jS4wnMhugJsHZjFjYJ9pLcnnASVl1lXt1MhS hgEeTDb//LFMRbykDgY6L3T6i63jABIAQ/2HZXNTSczhTc7aVZiFoDe3MpVi39w1gM mL5aTQH/Yw6CKQ8HVfp01PplV2PHALH1t1hJpXemfI2KCljBZRgbiZVGXbT5k3GrL9 NjSs0AFwUWyvCOXHfdDrLpdCKYlloNwGac43KJK0z/wQzJEIPYhKUZ71PMA62LYYGa AhEBjcI5pIupwS9CiIWij5LO+U5H9lgFcDnACuPqKoC0yvFqt86kKnqBREGu3So07G R7TWKcvmm6pyQ== Date: Sun, 23 Jun 2024 16:21:10 +0100 From: Jonathan Cameron To: Olivier Moysan Cc: Lars-Peter Clausen , Maxime Coquelin , Alexandre Torgue , , , , Subject: Re: [PATCH 8/8] iio: adc: stm32-dfsdm: add scaling support to dfsdm Message-ID: <20240623162110.708032af@jic23-huawei> In-Reply-To: <20240618160836.945242-9-olivier.moysan@foss.st.com> References: <20240618160836.945242-1-olivier.moysan@foss.st.com> <20240618160836.945242-9-olivier.moysan@foss.st.com> X-Mailer: Claws Mail 4.3.0 (GTK 3.24.42; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240623_082127_286389_0F43AC73 X-CRM114-Status: GOOD ( 24.61 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Tue, 18 Jun 2024 18:08:34 +0200 Olivier Moysan wrote: > Add scaling support to STM32 DFSDM. Perhaps a short description here of how this works? Where does the scale come from, what assumptions are made etc. > > Signed-off-by: Olivier Moysan Some minor stuff. > diff --git a/drivers/iio/adc/stm32-dfsdm-adc.c b/drivers/iio/adc/stm32-dfsdm-adc.c > index 69b4764d7cba..93bf6035bd6d 100644 > --- a/drivers/iio/adc/stm32-dfsdm-adc.c > +++ b/drivers/iio/adc/stm32-dfsdm-adc.c urn 0; > } > > @@ -1060,7 +1072,7 @@ static int stm32_dfsdm_update_scan_mode(struct iio_dev *indio_dev, > static int stm32_dfsdm_postenable(struct iio_dev *indio_dev) > { > struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); > - int ret; > + int i = 0, ret; Don't mix assigned and unassigned variable declarations. Just use a separate line as this can mean subtle assignment or lack of assignment issues sneak in. > > /* Reset adc buffer index */ > adc->bufi = 0; > @@ -1071,6 +1083,15 @@ static int stm32_dfsdm_postenable(struct iio_dev *indio_dev) > return ret; > } > > + if (adc->backend) { > + while (adc->backend[i]) { Could do similar to the suggestion below. Mostly I don't like the index variable manipulation. > + ret = iio_backend_enable(&indio_dev->dev, adc->backend[i]); > + if (ret < 0) > + return ret; > + i++; > + } > + } > + > ret = stm32_dfsdm_start_dfsdm(adc->dfsdm); > if (ret < 0) > goto err_stop_hwc; > @@ -1103,6 +1124,7 @@ static int stm32_dfsdm_postenable(struct iio_dev *indio_dev) > static int stm32_dfsdm_predisable(struct iio_dev *indio_dev) > { > struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); > + int i = 0; > > stm32_dfsdm_stop_conv(indio_dev); > > @@ -1110,6 +1132,13 @@ static int stm32_dfsdm_predisable(struct iio_dev *indio_dev) > > stm32_dfsdm_stop_dfsdm(adc->dfsdm); > > + if (adc->backend) { > + while (adc->backend[i]) { > + iio_backend_disable(&indio_dev->dev, adc->backend[i]); > + i++; > + } Something like struct iio_backend **be = &adc->backend[0]; do { iio_backend_disable(&indio-dev->dev, be); } while (be++); maybe. Up to you. > + } > @@ -1320,6 +1360,45 @@ static int stm32_dfsdm_read_raw(struct iio_dev *indio_dev, > *val = adc->sample_freq; > > return IIO_VAL_INT; > + > + case IIO_CHAN_INFO_SCALE: > + /* > + * Scale is expressed in mV. > + * When fast mode is disabled, actual resolution may be lower > + * than 2^n, where n=realbits-1. As below, use a few more spaces. > + * This leads to underestimating input voltage. To > + * compensate this deviation, the voltage reference can be > + * corrected with a factor = realbits resolution / actual max > + */ > + if (adc->backend[idx]) { > + iio_backend_read_raw(adc->backend[idx], val, val2, mask); > + > + *val = div_u64((u64)*val * (u64)BIT(DFSDM_DATA_RES - 1), max); > + *val2 = chan->scan_type.realbits; > + if (chan->differential) > + *val *= 2; > + } > + return IIO_VAL_FRACTIONAL_LOG2; > + > + case IIO_CHAN_INFO_OFFSET: > + /* > + * DFSDM output data are in the range [-2^n,2^n], Use a few more spaces. [-2^2, 2^n] > + * with n=realbits-1. n = realbits - 1 Just to keep it closer to the C coding style. > + * - Differential modulator: > + * Offset correspond to SD modulator offset. > + * - Single ended modulator: > + * Input is in [0V,Vref] range, where 0V corresponds to -2^n, and Vref to 2^n. Avoid that long line with a suitable line break. > + * Add 2^n to offset. (i.e. middle of input range) > + * offset = offset(sd) * vref / res(sd) * max / vref. > + */ > + if (adc->backend[idx]) { > + iio_backend_read_raw(adc->backend[idx], val, val2, mask); > + > + *val = div_u64((u64)max * *val, BIT(*val2 - 1)); > + if (!chan->differential) > + *val += max; > + } > + return IIO_VAL_INT; > } > > return -EINVAL; > @@ -1449,7 +1528,15 @@ static int stm32_dfsdm_adc_chan_init_one(struct iio_dev *indio_dev, struct iio_c > * IIO_CHAN_INFO_RAW: used to compute regular conversion > * IIO_CHAN_INFO_OVERSAMPLING_RATIO: used to set oversampling > */ > - ch->info_mask_separate = BIT(IIO_CHAN_INFO_RAW); > + if (child) { > + ch->info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | > + BIT(IIO_CHAN_INFO_SCALE) | > + BIT(IIO_CHAN_INFO_OFFSET); Indent looks a little odd. Maybe one more space neede? > + } else { > + /* Legacy. Scaling not supported */ > + ch->info_mask_separate = BIT(IIO_CHAN_INFO_RAW); > + } > + > ch->info_mask_shared_by_all = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) | > BIT(IIO_CHAN_INFO_SAMP_FREQ); > > @@ -1816,3 +1903,4 @@ module_platform_driver(stm32_dfsdm_adc_driver); > MODULE_DESCRIPTION("STM32 sigma delta ADC"); > MODULE_AUTHOR("Arnaud Pouliquen "); > MODULE_LICENSE("GPL v2"); > +MODULE_IMPORT_NS(IIO_BACKEND);