From patchwork Tue Jun 11 09:56:57 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Myl=C3=A8ne_Josserand?= X-Patchwork-Id: 10986361 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 60C4E6C5 for ; Tue, 11 Jun 2019 09:57:47 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 514CB28066 for ; Tue, 11 Jun 2019 09:57:47 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 45A6528701; Tue, 11 Jun 2019 09:57:47 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E07F128066 for ; Tue, 11 Jun 2019 09:57:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2404766AbfFKJ5Z (ORCPT ); Tue, 11 Jun 2019 05:57:25 -0400 Received: from relay2-d.mail.gandi.net ([217.70.183.194]:49675 "EHLO relay2-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2404553AbfFKJ5Y (ORCPT ); Tue, 11 Jun 2019 05:57:24 -0400 X-Originating-IP: 90.88.159.246 Received: from dell-desktop.home (aaubervilliers-681-1-40-246.w90-88.abo.wanadoo.fr [90.88.159.246]) (Authenticated sender: mylene.josserand@bootlin.com) by relay2-d.mail.gandi.net (Postfix) with ESMTPSA id 5FE454001A; Tue, 11 Jun 2019 09:57:21 +0000 (UTC) From: =?utf-8?q?Myl=C3=A8ne_Josserand?= To: peda@axentia.se, jic23@kernel.org, knaack.h@gmx.de, lars@metafoo.de, pmeerw@pmeerw.net, robh+dt@kernel.org, mark.rutland@arm.com Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, thomas.petazzoni@bootlin.com, mylene.josserand@bootlin.com Subject: [PATCH v1 1/3] iio: afe: rescale: Move scale conversion to new function Date: Tue, 11 Jun 2019 11:56:57 +0200 Message-Id: <20190611095659.29845-2-mylene.josserand@bootlin.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20190611095659.29845-1-mylene.josserand@bootlin.com> References: <20190611095659.29845-1-mylene.josserand@bootlin.com> MIME-Version: 1.0 Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP To prepare the support of processed value, create a function to convert the scale according to the voltage-divider node used in the device-tree. Signed-off-by: Mylène Josserand --- drivers/iio/afe/iio-rescale.c | 54 +++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index e9ceee66d1e7..3e689d6eb501 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -33,12 +33,41 @@ struct rescale { s32 denominator; }; +static int rescale_convert_scale(struct rescale *rescale, int *val, int *val2) +{ + unsigned long long tmp; + int ret; + + ret = iio_read_channel_scale(rescale->source, val, val2); + switch (ret) { + case IIO_VAL_FRACTIONAL: + *val *= rescale->numerator; + *val2 *= rescale->denominator; + return ret; + case IIO_VAL_INT: + *val *= rescale->numerator; + if (rescale->denominator == 1) + return ret; + *val2 = rescale->denominator; + return IIO_VAL_FRACTIONAL; + case IIO_VAL_FRACTIONAL_LOG2: + tmp = *val * 1000000000LL; + do_div(tmp, rescale->denominator); + tmp *= rescale->numerator; + do_div(tmp, 1000000000LL); + *val = tmp; + + return ret; + default: + return -EOPNOTSUPP; + } +} + static int rescale_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int *val, int *val2, long mask) { struct rescale *rescale = iio_priv(indio_dev); - unsigned long long tmp; int ret; switch (mask) { @@ -46,28 +75,7 @@ static int rescale_read_raw(struct iio_dev *indio_dev, return iio_read_channel_raw(rescale->source, val); case IIO_CHAN_INFO_SCALE: - ret = iio_read_channel_scale(rescale->source, val, val2); - switch (ret) { - case IIO_VAL_FRACTIONAL: - *val *= rescale->numerator; - *val2 *= rescale->denominator; - return ret; - case IIO_VAL_INT: - *val *= rescale->numerator; - if (rescale->denominator == 1) - return ret; - *val2 = rescale->denominator; - return IIO_VAL_FRACTIONAL; - case IIO_VAL_FRACTIONAL_LOG2: - tmp = *val * 1000000000LL; - do_div(tmp, rescale->denominator); - tmp *= rescale->numerator; - do_div(tmp, 1000000000LL); - *val = tmp; - return ret; - default: - return -EOPNOTSUPP; - } + return rescale_convert_scale(rescale, val, val2); default: return -EINVAL; } From patchwork Tue Jun 11 09:56:58 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Myl=C3=A8ne_Josserand?= X-Patchwork-Id: 10986357 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3B58F1515 for ; Tue, 11 Jun 2019 09:57:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2B42628066 for ; Tue, 11 Jun 2019 09:57:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1ED5228701; Tue, 11 Jun 2019 09:57:43 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BA2C928066 for ; Tue, 11 Jun 2019 09:57:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2405016AbfFKJ52 (ORCPT ); Tue, 11 Jun 2019 05:57:28 -0400 Received: from relay2-d.mail.gandi.net ([217.70.183.194]:35281 "EHLO relay2-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2404919AbfFKJ52 (ORCPT ); Tue, 11 Jun 2019 05:57:28 -0400 X-Originating-IP: 90.88.159.246 Received: from dell-desktop.home (aaubervilliers-681-1-40-246.w90-88.abo.wanadoo.fr [90.88.159.246]) (Authenticated sender: mylene.josserand@bootlin.com) by relay2-d.mail.gandi.net (Postfix) with ESMTPSA id BD70340008; Tue, 11 Jun 2019 09:57:24 +0000 (UTC) From: =?utf-8?q?Myl=C3=A8ne_Josserand?= To: peda@axentia.se, jic23@kernel.org, knaack.h@gmx.de, lars@metafoo.de, pmeerw@pmeerw.net, robh+dt@kernel.org, mark.rutland@arm.com Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, thomas.petazzoni@bootlin.com, mylene.josserand@bootlin.com Subject: [PATCH v1 2/3] iio: afe: rescale: Add support of CHAN_INFO_PROCESSED Date: Tue, 11 Jun 2019 11:56:58 +0200 Message-Id: <20190611095659.29845-3-mylene.josserand@bootlin.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20190611095659.29845-1-mylene.josserand@bootlin.com> References: <20190611095659.29845-1-mylene.josserand@bootlin.com> MIME-Version: 1.0 Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add the support of the CHAN_INFO_PROCESSED to have directly the processed value (raw * scale). It will be exported as in_voltage0_input in sysfs. Signed-off-by: Mylène Josserand --- drivers/iio/afe/iio-rescale.c | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index 3e689d6eb501..2275571fff64 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -63,14 +63,54 @@ static int rescale_convert_scale(struct rescale *rescale, int *val, int *val2) } } +static int rescale_convert_processed(struct rescale *rescale, int raw, + int *val, int *val2) +{ + unsigned long long tmp, scaled; + int ret; + + ret = rescale_convert_scale(rescale, val, val2); + switch (ret) { + case IIO_VAL_FRACTIONAL: + tmp = div_s64((s64)*val * 1000000000LL, *val2); + scaled = tmp * raw; + *val = (int)div_s64_rem(scaled, 1000000000, val2); + return ret; + case IIO_VAL_INT: + return IIO_VAL_FRACTIONAL; + case IIO_VAL_FRACTIONAL_LOG2: + tmp = shift_right((s64)*val * 1000000000LL, *val2); + scaled = tmp * raw; + *val = (int)div_s64_rem(scaled, 1000000000LL, val2); + return ret; + default: + return -EOPNOTSUPP; + } +} + static int rescale_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int *val, int *val2, long mask) { struct rescale *rescale = iio_priv(indio_dev); + unsigned int raw; int ret; switch (mask) { + case IIO_CHAN_INFO_PROCESSED: + /* Read the raw value and the scale */ + ret = iio_read_channel_raw(rescale->source, &raw); + if (!ret) + return ret; + ret = iio_read_channel_scale(rescale->source, val, val2); + if (!ret) + return ret; + /* Process the correct value with raw * scale */ + ret = rescale_convert_processed(rescale, raw, val, val2); + if (!ret) + return ret; + return IIO_VAL_INT; + case IIO_CHAN_INFO_RAW: return iio_read_channel_raw(rescale->source, val); @@ -145,7 +185,7 @@ static int rescale_configure_channel(struct device *dev, } chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | - BIT(IIO_CHAN_INFO_SCALE); + BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_PROCESSED); if (iio_channel_has_available(schan, IIO_CHAN_INFO_RAW)) chan->info_mask_separate_available |= BIT(IIO_CHAN_INFO_RAW); From patchwork Tue Jun 11 09:56:59 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Myl=C3=A8ne_Josserand?= X-Patchwork-Id: 10986355 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8F5216C5 for ; Tue, 11 Jun 2019 09:57:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7F40328066 for ; Tue, 11 Jun 2019 09:57:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 734ED28701; Tue, 11 Jun 2019 09:57:39 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1E0C228066 for ; Tue, 11 Jun 2019 09:57:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2405137AbfFKJ5d (ORCPT ); Tue, 11 Jun 2019 05:57:33 -0400 Received: from relay2-d.mail.gandi.net ([217.70.183.194]:60623 "EHLO relay2-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2405130AbfFKJ5c (ORCPT ); Tue, 11 Jun 2019 05:57:32 -0400 X-Originating-IP: 90.88.159.246 Received: from dell-desktop.home (aaubervilliers-681-1-40-246.w90-88.abo.wanadoo.fr [90.88.159.246]) (Authenticated sender: mylene.josserand@bootlin.com) by relay2-d.mail.gandi.net (Postfix) with ESMTPSA id 32F5240019; Tue, 11 Jun 2019 09:57:29 +0000 (UTC) From: =?utf-8?q?Myl=C3=A8ne_Josserand?= To: peda@axentia.se, jic23@kernel.org, knaack.h@gmx.de, lars@metafoo.de, pmeerw@pmeerw.net, robh+dt@kernel.org, mark.rutland@arm.com Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, thomas.petazzoni@bootlin.com, mylene.josserand@bootlin.com Subject: [PATCH v1 3/3] dt-bindings: iio: afe: Add hwmon example Date: Tue, 11 Jun 2019 11:56:59 +0200 Message-Id: <20190611095659.29845-4-mylene.josserand@bootlin.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20190611095659.29845-1-mylene.josserand@bootlin.com> References: <20190611095659.29845-1-mylene.josserand@bootlin.com> MIME-Version: 1.0 Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP With the support of CHAN_INFO_PROCESSED in voltage-divider, it is possible to read the processed values directly from iio's sysfs entries or by using iio-hwmon. Add an example for this last use case. Signed-off-by: Mylène Josserand --- .../bindings/iio/afe/voltage-divider.txt | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Documentation/devicetree/bindings/iio/afe/voltage-divider.txt b/Documentation/devicetree/bindings/iio/afe/voltage-divider.txt index b452a8406107..f7e1c7cb2744 100644 --- a/Documentation/devicetree/bindings/iio/afe/voltage-divider.txt +++ b/Documentation/devicetree/bindings/iio/afe/voltage-divider.txt @@ -51,3 +51,27 @@ sysv { spi-max-frequency = <1000000>; }; }; + +It is also possible to retrieve the processed values using hwmon node: + +div0: div0 { + compatible = "voltage-divider"; + io-channels = <&adc0 0>; /* Channel 0 of the ADC */ + output-ohms = <47>; /* R2 */ + full-ohms = <73>; /* R1 (26) + R2 (47) */ + #io-channel-cells = <1>; +}; + +div1: div1 { + compatible = "voltage-divider"; + io-channels = <&adc0 1>; /* Channel 1 of the ADC */ + output-ohms = <47>; /* R2 */ + full-ohms = <115>; /* R1 (68) + R2 (47) */ + #io-channel-cells = <1>; +}; + +iio-hwmon { + compatible = "iio-hwmon"; + io-channels = <&div0 0>, <&div1 0>; + io-channel-names = "3v3", "usb"; +};