Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp1629556iog; Sat, 18 Jun 2022 15:24:24 -0700 (PDT) X-Google-Smtp-Source: AGRyM1s10KeonhOLyUjawPFyfC34dYIgVEnfY6Pij5rlOyoh8somMd7uG7TSGduAU/T8rzRxLrn0 X-Received: by 2002:a17:907:94cb:b0:721:252c:d4bb with SMTP id dn11-20020a17090794cb00b00721252cd4bbmr4003279ejc.148.1655591064444; Sat, 18 Jun 2022 15:24:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655591064; cv=none; d=google.com; s=arc-20160816; b=HWNWh+tRSiglV9+wCC/pdcfJZpLZwN6QO5WRZ8GPnf+iiNtMmKFeq7GYVpl4CejPPX 0TI2pYqQk2mag/Py2EDQgRS21Zq2NtOWG6/4YGdrAeVr/EagBELkBoYBsWgQT1Uo+VL8 AB85HBPDTAmqQ2G4rr6QhVaqRtHvg3aGpkiSQxaHgt442rQflJOa16rPF1ikdS/Yv625 bnbMM5UUnfHiO5ij8m5sFRPcND5N1xFK2QZroGdWcy7kVkIt72LVFhzI5Oz+ruSYp5Ho XI+f9Nd30idbyzpOStbndnQt1Bpbek7QU+UD8Wpv0ai84Tc4ZgJStzPC4E8k9S/7Vbi7 BPWw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=iblggP5OiZqP+FqgfuEd0tW9JbHj3v1KPpGeBiW9/qc=; b=OIWEFrZz55UWIkCze/p261zqw+6kT6zMUon7jtdcYdGLFwp6QkZzWlcD5IdJ+/HbUs 71x7sp1krzU8U4Vn8fdbpkH632EMqxv6YfXh/lOEFF7MC/W3a4ivHkAG/Pfm/sfl/ju8 3ZBpNQJRC8k0J5sTrw+n2DOJTCy2VAWlKhbfzKfIsKgrbSoDMfYiEc3gwzmUHiccMFIS 7/zasjbjIyifO9RbzgLmKaU9umzrtttbGez6kU33jWIgBM8F1HACd2Pfj5W7R9nGI7SU quZ55ExIdSh7sRf+R3q/JgqLklSxIYb1sn4m/KQXge6UYoBgSyX8Sj+HV55pO2C7YZzu d6KA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=qOa7MW+v; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z1-20020a05640235c100b0042dd658ab17si10371779edc.231.2022.06.18.15.24.00; Sat, 18 Jun 2022 15:24:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=qOa7MW+v; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235957AbiFRVkq (ORCPT + 99 others); Sat, 18 Jun 2022 17:40:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44000 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235432AbiFRVkS (ORCPT ); Sat, 18 Jun 2022 17:40:18 -0400 Received: from mail-ej1-x630.google.com (mail-ej1-x630.google.com [IPv6:2a00:1450:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4ACDF101F3; Sat, 18 Jun 2022 14:40:12 -0700 (PDT) Received: by mail-ej1-x630.google.com with SMTP id me5so14614983ejb.2; Sat, 18 Jun 2022 14:40:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=iblggP5OiZqP+FqgfuEd0tW9JbHj3v1KPpGeBiW9/qc=; b=qOa7MW+v9K2mHIdT4jcQ6MA1kODbcLVFRmepCgSmpLJIGpqXJRW3nLD5dF4/4RwnHT rbYXFfgc6vFTrBV7Xh5Fj9qjR9Y08YBaQtRDHCjUcPlEu3NZ8/xlhC8AYM53RePaP4rE P+CFMgvfOayGASVOl2JaJojBZjyjefOZHynzWrxYgcPe5oHrnuqLFFRH6M/CcG0KqNuf ZuhkeiCeNT+noOxYzz00Rrd8zU+3J9oCtDgR2g7dSVecY+Ei6le5rqi7yd2UBI54SPuj A1/jbfs1o/E9JGGq+V+5TTDMUccPFLQqPIPeW7M6xBTotcr4n5+07VraXQBYLyAPJHwZ TYpw== 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=iblggP5OiZqP+FqgfuEd0tW9JbHj3v1KPpGeBiW9/qc=; b=dsJElocxcfPZzYGi/e9XQhHXOFLpWLqleUDKjLNLYkd8PIOPIw8oRtgtPSj3SHBWNC eBUFrYNsCCYeKPmmyfSlXRTVVwcSpzZWgEznxymwEab6IrQwtJpSo35HQC4b+pNeOnhK arFmgzxbESNLY/RVtmK9jHBhJGCX5P5iVGFJRNZbvBK7i2M4jj6WU0SmS80C+cpYD48b 8Kme2O5ICiC0Tynf2PR/TcaQ75w/ZNLxzm3wdIYsM86gNzeJ4P9Irjb2ozBz3taMwq+f EaUbETnhj3EcHu7XFnWtYxISM+T7Wcxe746/jk/Ugb2kuNMs561WLEC7+lBTVrt88twu I1+A== X-Gm-Message-State: AJIora95BQciYjTZWTW9qK9Fm9m9zatYyVnCZ+l8U34RkUuF9wSuanWT weWjfthQAZw4xbY2Fr1S/hI= X-Received: by 2002:a17:907:3f97:b0:711:d61d:df9 with SMTP id hr23-20020a1709073f9700b00711d61d0df9mr14967091ejc.644.1655588411636; Sat, 18 Jun 2022 14:40:11 -0700 (PDT) Received: from localhost (92.40.169.177.threembb.co.uk. [92.40.169.177]) by smtp.gmail.com with ESMTPSA id y4-20020aa7ccc4000000b004316f94ec4esm6459638edt.66.2022.06.18.14.40.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Jun 2022 14:40:11 -0700 (PDT) From: Aidan MacDonald To: linus.walleij@linaro.org, brgl@bgdev.pl, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, wens@csie.org, jic23@kernel.org, lee.jones@linaro.org, sre@kernel.org, broonie@kernel.org, gregkh@linuxfoundation.org, lgirdwood@gmail.com Cc: lars@metafoo.de, rafael@kernel.org, quic_gurus@quicinc.com, sebastian.reichel@collabora.com, andy.shevchenko@gmail.com, michael@walle.cc, linux-gpio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, linux-pm@vger.kernel.org Subject: [PATCH v3 10/16] iio: adc: axp20x_adc: Minor code cleanups Date: Sat, 18 Jun 2022 22:40:03 +0100 Message-Id: <20220618214009.2178567-11-aidanmacdonald.0x0@gmail.com> In-Reply-To: <20220618214009.2178567-1-aidanmacdonald.0x0@gmail.com> References: <20220618214009.2178567-1-aidanmacdonald.0x0@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The code may be clearer if parameters are not re-purposed to hold temporary results like register values, so introduce local variables as necessary to avoid that. Also, use the common FIELD_PREP macro instead of a hand-rolled version. Suggested-by: Jonathan Cameron Signed-off-by: Aidan MacDonald --- drivers/iio/adc/axp20x_adc.c | 61 +++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/drivers/iio/adc/axp20x_adc.c b/drivers/iio/adc/axp20x_adc.c index 53bf7d4899d2..041511280e1e 100644 --- a/drivers/iio/adc/axp20x_adc.c +++ b/drivers/iio/adc/axp20x_adc.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -22,20 +23,20 @@ #include #define AXP20X_ADC_EN1_MASK GENMASK(7, 0) - #define AXP20X_ADC_EN2_MASK (GENMASK(3, 2) | BIT(7)) + #define AXP22X_ADC_EN1_MASK (GENMASK(7, 5) | BIT(0)) #define AXP20X_GPIO10_IN_RANGE_GPIO0 BIT(0) #define AXP20X_GPIO10_IN_RANGE_GPIO1 BIT(1) -#define AXP20X_GPIO10_IN_RANGE_GPIO0_VAL(x) ((x) & BIT(0)) -#define AXP20X_GPIO10_IN_RANGE_GPIO1_VAL(x) (((x) & BIT(0)) << 1) #define AXP20X_ADC_RATE_MASK GENMASK(7, 6) -#define AXP813_V_I_ADC_RATE_MASK GENMASK(5, 4) -#define AXP813_ADC_RATE_MASK (AXP20X_ADC_RATE_MASK | AXP813_V_I_ADC_RATE_MASK) #define AXP20X_ADC_RATE_HZ(x) ((ilog2((x) / 25) << 6) & AXP20X_ADC_RATE_MASK) + #define AXP22X_ADC_RATE_HZ(x) ((ilog2((x) / 100) << 6) & AXP20X_ADC_RATE_MASK) + +#define AXP813_V_I_ADC_RATE_MASK GENMASK(5, 4) +#define AXP813_ADC_RATE_MASK (AXP20X_ADC_RATE_MASK | AXP813_V_I_ADC_RATE_MASK) #define AXP813_TS_GPIO0_ADC_RATE_HZ(x) AXP20X_ADC_RATE_HZ(x) #define AXP813_V_I_ADC_RATE_HZ(x) ((ilog2((x) / 100) << 4) & AXP813_V_I_ADC_RATE_MASK) #define AXP813_ADC_RATE_HZ(x) (AXP20X_ADC_RATE_HZ(x) | AXP813_V_I_ADC_RATE_HZ(x)) @@ -234,7 +235,7 @@ static int axp20x_adc_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int *val) { struct axp20x_adc_iio *info = iio_priv(indio_dev); - int size = 12; + int ret, size; /* * N.B.: Unlike the Chinese datasheets tell, the charging current is @@ -246,10 +247,11 @@ static int axp20x_adc_raw(struct iio_dev *indio_dev, else size = 12; - *val = axp20x_read_variable_width(info->regmap, chan->address, size); - if (*val < 0) - return *val; + ret = axp20x_read_variable_width(info->regmap, chan->address, size); + if (ret < 0) + return ret; + *val = ret; return IIO_VAL_INT; } @@ -257,11 +259,13 @@ static int axp22x_adc_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int *val) { struct axp20x_adc_iio *info = iio_priv(indio_dev); + int ret; - *val = axp20x_read_variable_width(info->regmap, chan->address, 12); - if (*val < 0) - return *val; + ret = axp20x_read_variable_width(info->regmap, chan->address, 12); + if (ret < 0) + return ret; + *val = ret; return IIO_VAL_INT; } @@ -269,11 +273,13 @@ static int axp813_adc_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int *val) { struct axp20x_adc_iio *info = iio_priv(indio_dev); + int ret; - *val = axp20x_read_variable_width(info->regmap, chan->address, 12); - if (*val < 0) - return *val; + ret = axp20x_read_variable_width(info->regmap, chan->address, 12); + if (ret < 0) + return ret; + *val = ret; return IIO_VAL_INT; } @@ -443,27 +449,27 @@ static int axp20x_adc_offset_voltage(struct iio_dev *indio_dev, int channel, int *val) { struct axp20x_adc_iio *info = iio_priv(indio_dev); + unsigned int regval; int ret; - ret = regmap_read(info->regmap, AXP20X_GPIO10_IN_RANGE, val); + ret = regmap_read(info->regmap, AXP20X_GPIO10_IN_RANGE, ®val); if (ret < 0) return ret; switch (channel) { case AXP20X_GPIO0_V: - *val &= AXP20X_GPIO10_IN_RANGE_GPIO0; + regval = FIELD_GET(AXP20X_GPIO10_IN_RANGE_GPIO0, regval); break; case AXP20X_GPIO1_V: - *val &= AXP20X_GPIO10_IN_RANGE_GPIO1; + regval = FIELD_GET(AXP20X_GPIO10_IN_RANGE_GPIO1, regval); break; default: return -EINVAL; } - *val = *val ? 700000 : 0; - + *val = regval ? 700000 : 0; return IIO_VAL_INT; } @@ -548,7 +554,7 @@ static int axp20x_write_raw(struct iio_dev *indio_dev, long mask) { struct axp20x_adc_iio *info = iio_priv(indio_dev); - unsigned int reg, regval; + unsigned int regmask, regval; /* * The AXP20X PMIC allows the user to choose between 0V and 0.7V offsets @@ -560,25 +566,24 @@ static int axp20x_write_raw(struct iio_dev *indio_dev, if (val != 0 && val != 700000) return -EINVAL; - val = val ? 1 : 0; + regval = val ? 1 : 0; switch (chan->channel) { case AXP20X_GPIO0_V: - reg = AXP20X_GPIO10_IN_RANGE_GPIO0; - regval = AXP20X_GPIO10_IN_RANGE_GPIO0_VAL(val); + regmask = AXP20X_GPIO10_IN_RANGE_GPIO0; + regval = FIELD_PREP(AXP20X_GPIO10_IN_RANGE_GPIO0, regval); break; case AXP20X_GPIO1_V: - reg = AXP20X_GPIO10_IN_RANGE_GPIO1; - regval = AXP20X_GPIO10_IN_RANGE_GPIO1_VAL(val); + regmask = AXP20X_GPIO10_IN_RANGE_GPIO1; + regval = FIELD_PREP(AXP20X_GPIO10_IN_RANGE_GPIO1, regval); break; default: return -EINVAL; } - return regmap_update_bits(info->regmap, AXP20X_GPIO10_IN_RANGE, reg, - regval); + return regmap_update_bits(info->regmap, AXP20X_GPIO10_IN_RANGE, regmask, regval); } static const struct iio_info axp20x_adc_iio_info = { -- 2.35.1