From patchwork Mon Nov 1 07:18:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gwendal Grignou X-Patchwork-Id: 12595955 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AB22CC433F5 for ; Mon, 1 Nov 2021 07:18:42 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8B40A610A2 for ; Mon, 1 Nov 2021 07:18:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230289AbhKAHVO (ORCPT ); Mon, 1 Nov 2021 03:21:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43314 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229622AbhKAHVO (ORCPT ); Mon, 1 Nov 2021 03:21:14 -0400 Received: from mail-pj1-x1032.google.com (mail-pj1-x1032.google.com [IPv6:2607:f8b0:4864:20::1032]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 53495C061714 for ; Mon, 1 Nov 2021 00:18:38 -0700 (PDT) Received: by mail-pj1-x1032.google.com with SMTP id o10-20020a17090a3d4a00b001a6555878a8so3903389pjf.1 for ; Mon, 01 Nov 2021 00:18:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=K+dPilqKxL4QBcFGEUfJ9SV7PFgsCTF0dGGnItgMuKA=; b=KTn+v+oFC9DT+HmcBoXZLw7o+WHeca6CmSa5sTGvm/YigbU5X5kFOYkHI9q47yxzOa GASICwtwss2Cej6QgxS97ZOj3BgQmE0dncPgJoCuP7xrTHY9dAwOoh7ralyt8bvf9NXU Itzscar3hC3rq2tJw62yM66h/XYUVdpPEOLbk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=K+dPilqKxL4QBcFGEUfJ9SV7PFgsCTF0dGGnItgMuKA=; b=U4U/ppVRQvmRn0tqkGnq+3bY3sKBrJWz083xyasq3/8J0xgEd/se/NiITKvMEUeXtY lp05AaSHxFmbpWG3oHXZ/7Dj7KgOcKjV7ZL03MVibvRr/RmtrnfmF9eHyTBbitooz4H8 aJj860y2744XI6Lcu6xcdd9W6rmyl6jIN7JIJsr04+Sg3yWOdtSVUj6DcRg1SlRu6Plk zct7CJn3iwrQJdtEYZboCbNqxWDWjYuClzKWVfVy8zrWpIN/H43iEIkEwwCBYiO5Z9wO ouYwhZ1+DD/qUjI8eCYLSe6Zlz1QG/s0kpWUDex61IFKKT13EM0o4NGE1Y0sWcSaVpE7 JtWg== X-Gm-Message-State: AOAM531j/a51XMWYTcpXw9JgyzPu6fnNSj3HDsoWF3C6YaBp5QkQfKOu Atqt7n4Rh/hzCvu73FGkJrsKoQ== X-Google-Smtp-Source: ABdhPJyis17bjENsD7PiyE3WRhJKA/WLk6ywmDd/5rCTdImV/jE0hF6P54vL0a8TiyMOkJjsZBqrtA== X-Received: by 2002:a17:90b:1b03:: with SMTP id nu3mr35595386pjb.40.1635751117783; Mon, 01 Nov 2021 00:18:37 -0700 (PDT) Received: from localhost ([2620:15c:202:201:3f65:b35d:616a:31bd]) by smtp.gmail.com with UTF8SMTPSA id y9sm8154356pjt.27.2021.11.01.00.18.37 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 01 Nov 2021 00:18:37 -0700 (PDT) From: Gwendal Grignou To: jic23@kernel.org, lars@metafoo.de Cc: andy.shevchenko@gmail.com, linux-iio@vger.kernel.org, Gwendal Grignou Subject: [PATCH 1/4] iio: Use scan_type shift and realbits when processing raw data Date: Mon, 1 Nov 2021 00:18:19 -0700 Message-Id: <20211101071822.522178-2-gwendal@chromium.org> X-Mailer: git-send-email 2.33.1.1089.g2158813163f-goog In-Reply-To: <20211101071822.522178-1-gwendal@chromium.org> References: <20211101071822.522178-1-gwendal@chromium.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org When user space application read iio buffer though libiio, data is converted (see iio_channel_convert()) using the _type sysfs parameter. In particular, scan_type.shift and scan_type.realbits are used to shift and tell on how many bits signed elements are encoded on. When reading elements directly using the raw sysfs attributes, the same rules for shifting and signing should apply. Use channel definition as root of trust and replace constant with them for the simple cases. Signed-off-by: Gwendal Grignou --- drivers/iio/accel/bma220_spi.c | 3 ++- drivers/iio/accel/kxcjk-1013.c | 3 ++- drivers/iio/accel/mma7455_core.c | 3 ++- drivers/iio/accel/sca3000.c | 5 +++-- drivers/iio/accel/stk8312.c | 2 +- drivers/iio/accel/stk8ba50.c | 3 ++- drivers/iio/adc/ad7266.c | 3 ++- drivers/iio/adc/at91-sama5d2_adc.c | 3 ++- drivers/iio/adc/ti-adc12138.c | 3 ++- drivers/iio/magnetometer/mag3110.c | 6 ++++-- 10 files changed, 22 insertions(+), 12 deletions(-) diff --git a/drivers/iio/accel/bma220_spi.c b/drivers/iio/accel/bma220_spi.c index bc4c626e454d3..812d6749b24a7 100644 --- a/drivers/iio/accel/bma220_spi.c +++ b/drivers/iio/accel/bma220_spi.c @@ -125,7 +125,8 @@ static int bma220_read_raw(struct iio_dev *indio_dev, ret = bma220_read_reg(data->spi_device, chan->address); if (ret < 0) return -EINVAL; - *val = sign_extend32(ret >> BMA220_DATA_SHIFT, 5); + *val = sign_extend32(ret >> chan->scan_type.shift, + chan->scan_type.realbits - 1); return IIO_VAL_INT; case IIO_CHAN_INFO_SCALE: ret = bma220_read_reg(data->spi_device, BMA220_REG_RANGE); diff --git a/drivers/iio/accel/kxcjk-1013.c b/drivers/iio/accel/kxcjk-1013.c index a51fdd3c9b5b5..88cf0c276893a 100644 --- a/drivers/iio/accel/kxcjk-1013.c +++ b/drivers/iio/accel/kxcjk-1013.c @@ -927,7 +927,8 @@ static int kxcjk1013_read_raw(struct iio_dev *indio_dev, mutex_unlock(&data->mutex); return ret; } - *val = sign_extend32(ret >> 4, 11); + *val = sign_extend32(ret >> chan->scan_type.shift, + chan->scan_type.realbits - 1); ret = kxcjk1013_set_power_state(data, false); } mutex_unlock(&data->mutex); diff --git a/drivers/iio/accel/mma7455_core.c b/drivers/iio/accel/mma7455_core.c index 777c6c384b09e..e6739ba74edfa 100644 --- a/drivers/iio/accel/mma7455_core.c +++ b/drivers/iio/accel/mma7455_core.c @@ -134,7 +134,8 @@ static int mma7455_read_raw(struct iio_dev *indio_dev, if (ret) return ret; - *val = sign_extend32(le16_to_cpu(data), 9); + *val = sign_extend32(le16_to_cpu(data), + chan->scan_type.realbits - 1); return IIO_VAL_INT; diff --git a/drivers/iio/accel/sca3000.c b/drivers/iio/accel/sca3000.c index c6b75308148aa..938eb6bda73b3 100644 --- a/drivers/iio/accel/sca3000.c +++ b/drivers/iio/accel/sca3000.c @@ -730,8 +730,9 @@ static int sca3000_read_raw(struct iio_dev *indio_dev, mutex_unlock(&st->lock); return ret; } - *val = (be16_to_cpup((__be16 *)st->rx) >> 3) & 0x1FFF; - *val = sign_extend32(*val, 12); + *val = (be16_to_cpup((__be16 *)st->rx) >> + chan->scan_type.shift) & 0x1FFF; + *val = sign_extend32(*val, chan->scan_type.realbits - 1); } else { /* get the temperature when available */ ret = sca3000_read_data_short(st, diff --git a/drivers/iio/accel/stk8312.c b/drivers/iio/accel/stk8312.c index 43c621d0f11e4..de0cdf8c1f94c 100644 --- a/drivers/iio/accel/stk8312.c +++ b/drivers/iio/accel/stk8312.c @@ -355,7 +355,7 @@ static int stk8312_read_raw(struct iio_dev *indio_dev, mutex_unlock(&data->lock); return ret; } - *val = sign_extend32(ret, 7); + *val = sign_extend32(ret, chan->scan_type.realbits - 1); ret = stk8312_set_mode(data, data->mode & (~STK8312_MODE_ACTIVE)); mutex_unlock(&data->lock); diff --git a/drivers/iio/accel/stk8ba50.c b/drivers/iio/accel/stk8ba50.c index e137a34b5c9a9..517c57ed9e949 100644 --- a/drivers/iio/accel/stk8ba50.c +++ b/drivers/iio/accel/stk8ba50.c @@ -227,7 +227,8 @@ static int stk8ba50_read_raw(struct iio_dev *indio_dev, mutex_unlock(&data->lock); return -EINVAL; } - *val = sign_extend32(ret >> STK8BA50_DATA_SHIFT, 9); + *val = sign_extend32(ret >> chan->scan_type.shift, + chan->scan_type.realbits - 1); stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND); mutex_unlock(&data->lock); return IIO_VAL_INT; diff --git a/drivers/iio/adc/ad7266.c b/drivers/iio/adc/ad7266.c index a8ec3efd659ed..1d345d66742d8 100644 --- a/drivers/iio/adc/ad7266.c +++ b/drivers/iio/adc/ad7266.c @@ -159,7 +159,8 @@ static int ad7266_read_raw(struct iio_dev *indio_dev, *val = (*val >> 2) & 0xfff; if (chan->scan_type.sign == 's') - *val = sign_extend32(*val, 11); + *val = sign_extend32(*val, + chan->scan_type.realbits - 1); return IIO_VAL_INT; case IIO_CHAN_INFO_SCALE: diff --git a/drivers/iio/adc/at91-sama5d2_adc.c b/drivers/iio/adc/at91-sama5d2_adc.c index 4c922ef634f8e..92a57cf10fba4 100644 --- a/drivers/iio/adc/at91-sama5d2_adc.c +++ b/drivers/iio/adc/at91-sama5d2_adc.c @@ -1586,7 +1586,8 @@ static int at91_adc_read_info_raw(struct iio_dev *indio_dev, *val = st->conversion_value; ret = at91_adc_adjust_val_osr(st, val); if (chan->scan_type.sign == 's') - *val = sign_extend32(*val, 11); + *val = sign_extend32(*val, + chan->scan_type.realbits - 1); st->conversion_done = false; } diff --git a/drivers/iio/adc/ti-adc12138.c b/drivers/iio/adc/ti-adc12138.c index fcd5d39dd03ea..5b5d452105393 100644 --- a/drivers/iio/adc/ti-adc12138.c +++ b/drivers/iio/adc/ti-adc12138.c @@ -239,7 +239,8 @@ static int adc12138_read_raw(struct iio_dev *iio, if (ret) return ret; - *value = sign_extend32(be16_to_cpu(data) >> 3, 12); + *value = sign_extend32(be16_to_cpu(data) >> channel->scan_type.shift, + channel->scan_type.realbits - 1); return IIO_VAL_INT; case IIO_CHAN_INFO_SCALE: diff --git a/drivers/iio/magnetometer/mag3110.c b/drivers/iio/magnetometer/mag3110.c index c96415a1aeadd..17c62d806218d 100644 --- a/drivers/iio/magnetometer/mag3110.c +++ b/drivers/iio/magnetometer/mag3110.c @@ -291,7 +291,8 @@ static int mag3110_read_raw(struct iio_dev *indio_dev, if (ret < 0) goto release; *val = sign_extend32( - be16_to_cpu(buffer[chan->scan_index]), 15); + be16_to_cpu(buffer[chan->scan_index]), + chan->scan_type.realbits - 1); ret = IIO_VAL_INT; break; case IIO_TEMP: /* in 1 C / LSB */ @@ -306,7 +307,8 @@ static int mag3110_read_raw(struct iio_dev *indio_dev, mutex_unlock(&data->lock); if (ret < 0) goto release; - *val = sign_extend32(ret, 7); + *val = sign_extend32(ret, + chan->scan_type.realbits - 1); ret = IIO_VAL_INT; break; default: From patchwork Mon Nov 1 07:18:20 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gwendal Grignou X-Patchwork-Id: 12595953 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id F0C17C433EF for ; Mon, 1 Nov 2021 07:18:42 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CF864610A8 for ; Mon, 1 Nov 2021 07:18:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230312AbhKAHVO (ORCPT ); Mon, 1 Nov 2021 03:21:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43320 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229882AbhKAHVO (ORCPT ); Mon, 1 Nov 2021 03:21:14 -0400 Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9F20CC061746 for ; Mon, 1 Nov 2021 00:18:39 -0700 (PDT) Received: by mail-pl1-x629.google.com with SMTP id r5so11013466pls.1 for ; Mon, 01 Nov 2021 00:18:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=GuLak6n2D+xx1jRMo02bIST3H8cpHj9A6SUpEXVo4Kg=; b=XZSCUcC/6QicC38QcP0/x/qElDTIdCY6WA75RoT4CoDg+krz9F41NdNTzmz1+D7FF6 e95pYi9u/AQJZGaUalIrPZGd9fkdlNoMQ+AcUI2cQ5TbpiFaNXZZr8pOPSKWRx3Ywy0V QTvotVNfsx4ttSQovh+Gzg8LvPZIsaACouzLQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=GuLak6n2D+xx1jRMo02bIST3H8cpHj9A6SUpEXVo4Kg=; b=bmHBWcANWI1QLwo6KoJh05nhq6SIrGFq5wNjhScUQbAmQbUzbCe14IdkfDvh7/LQux 8dKjpT8iMGPRTnoBDN7UouYCHSJRVHxb+GOEd4ajgg3cYUNMRkNvBUoctBJYHr6OzfGV G2rL2I9qR9awjFaYzQNkHHoyG5N90rZHcpJxSG49hk0/IHetXC5dBlnaCUTHj8IWJMAm SI8/CKi2IeLoHHM7LZfEZlx4lRTcbVO/nKx6+tP7HMP5A8hTKkG9w48FIU3/zU9hHYBD H+UcDlnmEuSrOyGRmSNKgo7kLzNoDtGE02vc4yBPu4u14vpYl8k2PfTAXeU+2P6ZEhJk IKtA== X-Gm-Message-State: AOAM532miXEZtJFFDn3OBDy7ikwajXvtlnq46iobFzySw9Omhb+hnYe8 wjf3YVyJ+XeGgCT57Q9E4Mv8iBF3EZhvOQ== X-Google-Smtp-Source: ABdhPJxWIcszVXkfBKkPAfbWQApn3qgCvEAyRGUVVaVv4JugMLnAXNzuqZDrgvYYu7NagCyWAscxxg== X-Received: by 2002:a17:903:283:b0:141:c7ae:122b with SMTP id j3-20020a170903028300b00141c7ae122bmr10757498plr.53.1635751119100; Mon, 01 Nov 2021 00:18:39 -0700 (PDT) Received: from localhost ([2620:15c:202:201:3f65:b35d:616a:31bd]) by smtp.gmail.com with UTF8SMTPSA id mi11sm17598776pjb.5.2021.11.01.00.18.38 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 01 Nov 2021 00:18:38 -0700 (PDT) From: Gwendal Grignou To: jic23@kernel.org, lars@metafoo.de Cc: andy.shevchenko@gmail.com, linux-iio@vger.kernel.org, Gwendal Grignou Subject: [PATCH 2/4] iio: ti-ads1015: Remove shift variable ads1015_read_raw Date: Mon, 1 Nov 2021 00:18:20 -0700 Message-Id: <20211101071822.522178-3-gwendal@chromium.org> X-Mailer: git-send-email 2.33.1.1089.g2158813163f-goog In-Reply-To: <20211101071822.522178-1-gwendal@chromium.org> References: <20211101071822.522178-1-gwendal@chromium.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org By using scan_type.realbits when processing raw data, we use scan_type.shit only once, thus we don't need to define a local variable for it anymore. Signed-off-by: Gwendal Grignou --- drivers/iio/adc/ti-ads1015.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/drivers/iio/adc/ti-ads1015.c b/drivers/iio/adc/ti-ads1015.c index b0352e91ac165..b92d4cd1b8238 100644 --- a/drivers/iio/adc/ti-ads1015.c +++ b/drivers/iio/adc/ti-ads1015.c @@ -464,9 +464,7 @@ static int ads1015_read_raw(struct iio_dev *indio_dev, mutex_lock(&data->lock); switch (mask) { - case IIO_CHAN_INFO_RAW: { - int shift = chan->scan_type.shift; - + case IIO_CHAN_INFO_RAW: ret = iio_device_claim_direct_mode(indio_dev); if (ret) break; @@ -487,7 +485,8 @@ static int ads1015_read_raw(struct iio_dev *indio_dev, goto release_direct; } - *val = sign_extend32(*val >> shift, 15 - shift); + *val = sign_extend32(*val >> chan->scan_type.shift, + chan->scan_type.realbits - 1); ret = ads1015_set_power_state(data, false); if (ret < 0) @@ -497,7 +496,6 @@ static int ads1015_read_raw(struct iio_dev *indio_dev, release_direct: iio_device_release_direct_mode(indio_dev); break; - } case IIO_CHAN_INFO_SCALE: idx = data->channel_data[chan->address].pga; *val = ads1015_fullscale_range[idx]; From patchwork Mon Nov 1 07:18:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gwendal Grignou X-Patchwork-Id: 12595957 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7A40FC433FE for ; Mon, 1 Nov 2021 07:18:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5E467610A2 for ; Mon, 1 Nov 2021 07:18:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229882AbhKAHVP (ORCPT ); Mon, 1 Nov 2021 03:21:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43322 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230096AbhKAHVO (ORCPT ); Mon, 1 Nov 2021 03:21:14 -0400 Received: from mail-pg1-x532.google.com (mail-pg1-x532.google.com [IPv6:2607:f8b0:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BB744C061764 for ; Mon, 1 Nov 2021 00:18:40 -0700 (PDT) Received: by mail-pg1-x532.google.com with SMTP id 75so16471588pga.3 for ; Mon, 01 Nov 2021 00:18:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=S6D1JYyv2enHYz/2SFd9fXc7m/s8rpqOqULQYI4PkUs=; b=HdMHHfGyqUBtBfdvYxHDjj6stzJ+UXy2/tchpE2msmFsXsw66Kkp2NCIWTtnsjuiEQ i+iIsRjVhytM4A7o1arg9Q823A/34J2A420lzsXYK0+wRuWJIyRGqJOFfaem8t3QjmsR Cijx/bKGaZnoCqnk3JHlku0EnCD38wqPpHBsk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=S6D1JYyv2enHYz/2SFd9fXc7m/s8rpqOqULQYI4PkUs=; b=EZURLLpn19VC5A90xJNcby1qOoBn8F44PvcCo+xdHB8Dw4TjjGtYABpU1j2ZcAHF6a 82d7yf064NSQdoZqsFvi/PT/lOJKVBo3jlTuLaHEc8KIgmHxXOQ652niHIxkA7YKbYmU E9BgUfmeE5ftDBFu751ZJalFOkbXSE+1M1KkZtXgwWpv15dFDaeBrs1/9WRS6inGkZpd d4NI5IcEoNyXwTOhovHH1IJaFfhusfQdnngoN97WkYiycDexgcYophWj2REEYXL5Avzu CsDdeOUNtxsxDSNbfHQcSAlsgObMwnGYY1m12TzJeXdWydKTFoIkDly6AuQ6n2NrEqlY c+Sw== X-Gm-Message-State: AOAM530CInv55uiEZcmM6qHXsVmV90yIs3xwF6Hru2HoCQSuYR7RYnQn NguydALZQ7khkyItVdNw/KrrXw== X-Google-Smtp-Source: ABdhPJxTBcIM73CFwEapUXvjjbjr76VDPmtyi9DhNlqIfQJ3DnXGEMNy5h/g3dPyTdjA0s8pSwd4sQ== X-Received: by 2002:a63:4e06:: with SMTP id c6mr9611137pgb.299.1635751120428; Mon, 01 Nov 2021 00:18:40 -0700 (PDT) Received: from localhost ([2620:15c:202:201:3f65:b35d:616a:31bd]) by smtp.gmail.com with UTF8SMTPSA id s11sm6791153pfd.182.2021.11.01.00.18.39 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 01 Nov 2021 00:18:40 -0700 (PDT) From: Gwendal Grignou To: jic23@kernel.org, lars@metafoo.de Cc: andy.shevchenko@gmail.com, linux-iio@vger.kernel.org, Gwendal Grignou Subject: [PATCH 3/4] iio: xilinx-xadc-core: Use local variable in xadc_read_raw Date: Mon, 1 Nov 2021 00:18:21 -0700 Message-Id: <20211101071822.522178-4-gwendal@chromium.org> X-Mailer: git-send-email 2.33.1.1089.g2158813163f-goog In-Reply-To: <20211101071822.522178-1-gwendal@chromium.org> References: <20211101071822.522178-1-gwendal@chromium.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Minor cleanup: bit is already defined as chan->scan_type.realbits, use bit when needed. Signed-off-by: Gwendal Grignou --- drivers/iio/adc/xilinx-xadc-core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/iio/adc/xilinx-xadc-core.c b/drivers/iio/adc/xilinx-xadc-core.c index 83bea5ef765da..05050709e4f8a 100644 --- a/drivers/iio/adc/xilinx-xadc-core.c +++ b/drivers/iio/adc/xilinx-xadc-core.c @@ -943,7 +943,7 @@ static int xadc_read_raw(struct iio_dev *indio_dev, *val = 1000; break; } - *val2 = chan->scan_type.realbits; + *val2 = bits; return IIO_VAL_FRACTIONAL_LOG2; case IIO_TEMP: /* Temp in C = (val * 503.975) / 2**bits - 273.15 */ From patchwork Mon Nov 1 07:18:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gwendal Grignou X-Patchwork-Id: 12595959 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C6B3DC4332F for ; Mon, 1 Nov 2021 07:18:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id AA66F610A2 for ; Mon, 1 Nov 2021 07:18:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230096AbhKAHVP (ORCPT ); Mon, 1 Nov 2021 03:21:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43324 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229622AbhKAHVO (ORCPT ); Mon, 1 Nov 2021 03:21:14 -0400 Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2FD4BC061714 for ; Mon, 1 Nov 2021 00:18:42 -0700 (PDT) Received: by mail-pl1-x632.google.com with SMTP id u17so1840312plg.9 for ; Mon, 01 Nov 2021 00:18:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=2/IAh9b2ZtQRqOn+VVVEM3bR790w8gUv8rHS+EEtkRk=; b=Bke3EW2CeGaHbLSYwv9/Fok9yeJt5SJCAfoNFDh84kmgt0WrIGTmFWHCDeH6NXa6dv UbU7d5upMzfK8dhbo5Dr3DoyBzrlfOuB963V28F62mLGVdTatuOWml1pBxsuiC0KUHBQ OalWgokXlqdrzw32OFVcUIGA1GD2bRq8Tv1X8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=2/IAh9b2ZtQRqOn+VVVEM3bR790w8gUv8rHS+EEtkRk=; b=FmElrlWkyQjH9PG3/VVx8h7I6wcmqbgG48+1vIrY0rFcVhEQJ4xde5y9TaHIQ7NUO+ hEJbxhkZtOZQ2ip+r0Q6cbJVj1tUE5fn3JkbYWEe38LzvXUvIlq4MEc+6Q1JBjJ4c++n HjQyPH3gunAqqagiUdeOPCXg/adOktrnMWmnS4d23iP1F4ps8/KMIXRvAZqyRiM0Re0D +ZW3A9+HGfjcyIChd0nGsNEfz3DIknAK93rnPfKXBXzR9OG17vl9VemGOYjmZUlK8i0z dA5baYv7ekfeezA8aShDTCmiWCU5R2bZUPV4g3VZisdxzOqF8R/oeFLFNU7ba++MtF0m ljfw== X-Gm-Message-State: AOAM531NHcnQvOKuR+bOcRqDOGOdOv+Rbw+dLLGXSyr8ZPV7eqHj/Adx vFtZxtCWOYqOVHTKGsitC0VqCJ4cxeQOnA== X-Google-Smtp-Source: ABdhPJxRPup9TlvGcXXDJFL2UNMdxn+vb1loLDxd+dpJ2usLImHMftEAkbF/uD6jN7B+pF9uUfgUfw== X-Received: by 2002:a17:902:ce8f:b0:141:f85a:e0de with SMTP id f15-20020a170902ce8f00b00141f85ae0demr983850plg.69.1635751121744; Mon, 01 Nov 2021 00:18:41 -0700 (PDT) Received: from localhost ([2620:15c:202:201:3f65:b35d:616a:31bd]) by smtp.gmail.com with UTF8SMTPSA id b22sm4370464pge.2.2021.11.01.00.18.41 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 01 Nov 2021 00:18:41 -0700 (PDT) From: Gwendal Grignou To: jic23@kernel.org, lars@metafoo.de Cc: andy.shevchenko@gmail.com, linux-iio@vger.kernel.org, Gwendal Grignou Subject: [PATCH 4/4] iio: mpl3115: Use scan_type.shift and realbit in mpl3115_read_raw Date: Mon, 1 Nov 2021 00:18:22 -0700 Message-Id: <20211101071822.522178-5-gwendal@chromium.org> X-Mailer: git-send-email 2.33.1.1089.g2158813163f-goog In-Reply-To: <20211101071822.522178-1-gwendal@chromium.org> References: <20211101071822.522178-1-gwendal@chromium.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org When processing raw data using channel scan_type.shift as source of trust to shift data appropriately. When processing the temperature channel, use a 16bit big endian variable as buffer to increase conversion readability. Signed-off-by: Gwendal Grignou --- drivers/iio/pressure/mpl3115.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/drivers/iio/pressure/mpl3115.c b/drivers/iio/pressure/mpl3115.c index 1eb9e7b29e050..e95b9a5475b4e 100644 --- a/drivers/iio/pressure/mpl3115.c +++ b/drivers/iio/pressure/mpl3115.c @@ -74,7 +74,6 @@ static int mpl3115_read_raw(struct iio_dev *indio_dev, int *val, int *val2, long mask) { struct mpl3115_data *data = iio_priv(indio_dev); - __be32 tmp = 0; int ret; switch (mask) { @@ -84,7 +83,9 @@ static int mpl3115_read_raw(struct iio_dev *indio_dev, return ret; switch (chan->type) { - case IIO_PRESSURE: /* in 0.25 pascal / LSB */ + case IIO_PRESSURE: { /* in 0.25 pascal / LSB */ + __be32 tmp = 0; + mutex_lock(&data->lock); ret = mpl3115_request(data); if (ret < 0) { @@ -96,10 +97,13 @@ static int mpl3115_read_raw(struct iio_dev *indio_dev, mutex_unlock(&data->lock); if (ret < 0) break; - *val = be32_to_cpu(tmp) >> 12; + *val = be32_to_cpu(tmp) >> chan->scan_type.shift; ret = IIO_VAL_INT; break; - case IIO_TEMP: /* in 0.0625 celsius / LSB */ + } + case IIO_TEMP: { /* in 0.0625 celsius / LSB */ + __be16 tmp; + mutex_lock(&data->lock); ret = mpl3115_request(data); if (ret < 0) { @@ -111,9 +115,11 @@ static int mpl3115_read_raw(struct iio_dev *indio_dev, mutex_unlock(&data->lock); if (ret < 0) break; - *val = sign_extend32(be32_to_cpu(tmp) >> 20, 11); + *val = sign_extend32(be16_to_cpu(tmp) >> chan->scan_type.shift, + chan->scan_type.realbits - 1); ret = IIO_VAL_INT; break; + } default: ret = -EINVAL; break;