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 90A44C27C79 for ; Wed, 19 Jun 2024 05:48:05 +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:MIME-Version: Content-Transfer-Encoding:Content-Type:References:In-Reply-To:Date:Cc:To:From :Subject:Message-ID:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=b+kcrBGBf3/GydIe+LcjfY0IvCfWEER0PGXVlE1muRc=; b=vrtCg/kmBa5RLsb8lYHlX/7S2r DYG6IjgrMXTpvFT7/6HSrbVEvwZ0i+cUuoasoDwRoPOBpIPSA4NoeD5aS3Lmnua/Z+OwjTs0OAT6M w0/C+ipyDZNaGnrMBzB3zF2sQ1fAkxIlL8oAQUTbNH97mbLQiBrMxVV35Nll7MkkVxX9dO/uIdBJf +G6GRQiH+566uRMjKVWuEdmjubFGgdkAKWMvwa/z3g2tSZ3QAxv3QJze3LYS8w4Lj+JZMagNYD9JE gwybsz0OIIWz43G+eujqPwNGxLlbXHGCp/hva83oawEfQb22r9ciZKRuboH1WUX+RaXPFs5hh5nP7 s+FXs6Sw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sJoAj-0000000HYZD-2zAW; Wed, 19 Jun 2024 05:47:53 +0000 Received: from mail-lj1-x235.google.com ([2a00:1450:4864:20::235]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sJoAS-0000000HYOs-2nVi for linux-arm-kernel@lists.infradead.org; Wed, 19 Jun 2024 05:47:38 +0000 Received: by mail-lj1-x235.google.com with SMTP id 38308e7fff4ca-2ebeefb9a7fso79746381fa.0 for ; Tue, 18 Jun 2024 22:47:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718776054; x=1719380854; darn=lists.infradead.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:from:to:cc:subject :date:message-id:reply-to; bh=b+kcrBGBf3/GydIe+LcjfY0IvCfWEER0PGXVlE1muRc=; b=bzz7Mdt/ce6T2x3n+GMgam4Q7EQxU8ViLOCQdqbkriMGBiWROuyqHJ57xXMk68IoUy 21kf6iqvtWAVjxPRRc8ZM/39gSP3LZ73LosNaNsSeF5h+tK7WxDEBewhXnsx3wY1qYbG ZkgS4zz3Rq7s32Ago3aGLt2tcV7PmHWLtk0ba5jHzBJnrj2NfbUoU6RUEtJyKeWPgCkN 58qbRst7nCGtsJSZvS/Iq0gGSN/KCHZtCg8PqtwGU2teUjCivsXfbbkDVPo3NkW3Nx7i o1lvJMpuu9rOaqf1g42No+UvZ3fL17XTKO+2kEQIZ+NpMNrjWlaJCFrO0/G5nb/ZZTMK NwPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718776054; x=1719380854; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=b+kcrBGBf3/GydIe+LcjfY0IvCfWEER0PGXVlE1muRc=; b=Nx0wgfSqffRWiC+r94bbcN++SN3BEckpewzERmLTQx5t3n6DEHnMo6zWPm9XJVpgMy LPekTesGeOvKtFi2TnxxVSd6V+QHEQk7Ml8IhlAosB4PF1pPTDR9CQByHQEYrP0G8Oba 5OR44g6EptA5dC3lE55VMY0/9FazY1p1VdpNoWbViKNA/hl7j+GZPeizk3MUQtM3Scj7 JAgwDkcwjJSKXd8ABh0eOt2sunqYNS0x1iIDzuD4Bwd9DWqlmwGoJIDSO65WNf6kiJ79 ihgmZWsRbI8HggEJ/2HfaIUJEY32ZZEe7OhzjdDtLqeE8Ih9w6B9/Cs45L+sxW++OXMZ ScxA== X-Forwarded-Encrypted: i=1; AJvYcCWr+OCP68FVZOTbjYBn2uApTWiulWbfe9BVSLtIGX1wKAXxl/Lqux0iM7OyKrb+3klw2EiU76Jk50JhdU0/eiYJU20fjpMVFPxmb6I0gBEm4uJQlUg= X-Gm-Message-State: AOJu0Yz0RBFuA63zF6+nl+QfuNR8uRtbz7mnCBQtA1OemPdiJR5c0Psi t+6n/UWuGt2lt7ku9KsfG0vQtfKb8Epf2I0Q71ZGONUfXGaboeu+ X-Google-Smtp-Source: AGHT+IFf9QjDMZRxdXpeiL5O2FJdHK57x+se5bfjdMDBd3Pt/O+4ZdRb6IFP3txtqXmhYbvcKBOTpw== X-Received: by 2002:a2e:2a01:0:b0:2ec:3f6c:ac37 with SMTP id 38308e7fff4ca-2ec3f6cac9cmr3948431fa.48.1718776054200; Tue, 18 Jun 2024 22:47:34 -0700 (PDT) Received: from nsa.fritz.box ([2001:a61:35f9:9001:40df:88bb:5090:7ab6]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-42286eef9c1sm252656375e9.7.2024.06.18.22.47.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Jun 2024 22:47:33 -0700 (PDT) Message-ID: Subject: Re: [PATCH 8/8] iio: adc: stm32-dfsdm: add scaling support to dfsdm From: Nuno =?ISO-8859-1?Q?S=E1?= To: Olivier Moysan , Jonathan Cameron , Lars-Peter Clausen , Maxime Coquelin , Alexandre Torgue Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com, linux-arm-kernel@lists.infradead.org Date: Wed, 19 Jun 2024 07:47:33 +0200 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> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.52.2 (3.52.2-1.fc40) MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240618_224736_800357_80A0CC03 X-CRM114-Status: GOOD ( 22.97 ) 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, 2024-06-18 at 18:08 +0200, Olivier Moysan wrote: > Add scaling support to STM32 DFSDM. >=20 > Signed-off-by: Olivier Moysan > --- Just some general comments...=20 Acked-by: Nuno Sa > =C2=A0drivers/iio/adc/Kconfig=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 |=C2=A0 1 + > =C2=A0drivers/iio/adc/stm32-dfsdm-adc.c | 94 ++++++++++++++++++++++++++++= ++- > =C2=A02 files changed, 92 insertions(+), 3 deletions(-) >=20 > diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig > index f3dfdaa80678..858ae8161fa4 100644 >=20 ... > @@ -1301,6 +1339,8 @@ static int stm32_dfsdm_read_raw(struct iio_dev *ind= io_dev, > =C2=A0 ret =3D stm32_dfsdm_single_conv(indio_dev, chan, val); > =C2=A0 if (adc->hwc) > =C2=A0 iio_hw_consumer_disable(adc->hwc); > + if (adc->backend[idx]) > + iio_backend_disable(&indio_dev->dev, adc->backend[idx]); > =C2=A0 if (ret < 0) { > =C2=A0 dev_err(&indio_dev->dev, > =C2=A0 "%s: Conversion failed (channel %d)\n", > @@ -1320,6 +1360,45 @@ static int stm32_dfsdm_read_raw(struct iio_dev *in= dio_dev, > =C2=A0 *val =3D adc->sample_freq; > =C2=A0 > =C2=A0 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=3Drealbits-1. > + * This leads to underestimating input voltage. To > + * compensate this deviation, the voltage reference can be > + * corrected with a factor =3D realbits resolution / actual max > + */ > + if (adc->backend[idx]) { > + iio_backend_read_raw(adc->backend[idx], val, val2, mask); > + This is something that I've been thinking about since the beginning of back= ends support. Basically having the "catch all" read_raw()/write_raw() or more de= dicated interfaces. For example, in your usecase I think it would make more sense t= o have dedicated API's like iio_backend_read_scale() and iio_backend_read_offset()= as you're extending that functionality. Calling iio_backend_read_raw() seems a bit we= ird to me. OTOH, iio_backend_read_raw() could be useful in cases where frontends call iio_backend_extend_chan_spec() and may get some functionality they are not = aware of. And so, in the default: statement this "catch all" API could be used. Anyways, this are really minor (likely pedantic) details and you kind of ca= me first and made the decision for me. As I don't really have strong feelings about = it, I'm fine with it as-is. > + *val =3D div_u64((u64)*val * (u64)BIT(DFSDM_DATA_RES - 1), > max); > + *val2 =3D chan->scan_type.realbits; > + if (chan->differential) > + *val *=3D 2; > + } > + return IIO_VAL_FRACTIONAL_LOG2; > + > + case IIO_CHAN_INFO_OFFSET: > + /* > + * DFSDM output data are in the range [-2^n,2^n], > + * with n=3Drealbits-1. > + * - 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. > + * Add 2^n to offset. (i.e. middle of input range) > + * offset =3D offset(sd) * vref / res(sd) * max / vref. > + */ > + if (adc->backend[idx]) { > + iio_backend_read_raw(adc->backend[idx], val, val2, mask); > + > + *val =3D div_u64((u64)max * *val, BIT(*val2 - 1)); > + if (!chan->differential) > + *val +=3D max; > + } > + return IIO_VAL_INT; > =C2=A0 } > =C2=A0 > =C2=A0 return -EINVAL; > @@ -1449,7 +1528,15 @@ static int stm32_dfsdm_adc_chan_init_one(struct ii= o_dev > *indio_dev, struct iio_c > =C2=A0 * IIO_CHAN_INFO_RAW: used to compute regular conversion > =C2=A0 * IIO_CHAN_INFO_OVERSAMPLING_RATIO: used to set oversampling > =C2=A0 */ > - ch->info_mask_separate =3D BIT(IIO_CHAN_INFO_RAW); > + if (child) { > + ch->info_mask_separate =3D BIT(IIO_CHAN_INFO_RAW) | > + BIT(IIO_CHAN_INFO_SCALE) | > + BIT(IIO_CHAN_INFO_OFFSET); Not sure if the above SCALE and OFFSET are solely properties if the backend= . If so, you could maybe use iio_backend_extend_chan_spec(). - Nuno S=C3=A1