From patchwork Wed Nov 4 19:30:49 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 318096 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 78E52C4742C for ; Wed, 4 Nov 2020 19:31:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 26B7520709 for ; Wed, 4 Nov 2020 19:31:33 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20150623.gappssmtp.com header.i=@bgdev-pl.20150623.gappssmtp.com header.b="oji3YSQN" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729946AbgKDTbG (ORCPT ); Wed, 4 Nov 2020 14:31:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36668 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729784AbgKDTbG (ORCPT ); Wed, 4 Nov 2020 14:31:06 -0500 Received: from mail-wr1-x441.google.com (mail-wr1-x441.google.com [IPv6:2a00:1450:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9AB75C0613D3 for ; Wed, 4 Nov 2020 11:31:05 -0800 (PST) Received: by mail-wr1-x441.google.com with SMTP id x7so23325789wrl.3 for ; Wed, 04 Nov 2020 11:31:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=SyfsPFMi1Ol5bHDdeg25EiAHS6gH8P7QyAg8QmJ1Sho=; b=oji3YSQN0WvH6hSwnCdoH+Q3Da9CXUQwMm1VLJ1RaK1rHoeYeRpu1xM7QlPvyUS2UX zG7bFYiqklzWXlzX3lNBWhR6iDfnEPCH7EgUJTzuKQhThjLlx0Q/iiVTbWN99lLn3q8Y DgL7ZjUnnrWM98w1NnBplb8qdRxFNg06PBVYjIClricuAnRgAtylBs0jNhoydweSAGGP gEcSoyuUM4R0jlkA8FO6O/GgEsi88htn3ejCJ3/ieMmOwQuI6EQwKQvTXNJCfFittQGl f/tb7cAnU3H6ff/kt17ewinFRlTJogsRzVofi3heGGnbKlH+aEOSzBG5oLJApG1bvvkA Bhuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=SyfsPFMi1Ol5bHDdeg25EiAHS6gH8P7QyAg8QmJ1Sho=; b=nlDFqDaeqObnJtVGiRMha1nLwdklb+Sdr7NPh7aqfaZapd2RqzwCLCbixlT/ZqJwH2 2fg1lbsx15eei0IDkJaXvBOQO5KY4bgJYjF7e2veGX7dy7HYM4g6Z9V8fiDFZ/IPy2PE U5+tTmCRMq2xan8XSTpgnDgYrvZ1L4ITACvhfrNtLvA3+deiPzwzrDN5pbVX9L3GCi2x 9uOg8at1dtFSiUP1DGt2wJhkwtAiSvYRt9NY51IMFM3T1lPDWmyARYVkxpAibqdI3Qb+ 9g/HvU7YvBF9DscwY5QpUpCFfPBr7gN/7V0f/xZbqu+znEBuGaaWbQ8RlTQ8TmRibjsN e/6g== X-Gm-Message-State: AOAM531hK6KwnN/aGyt2uQNLtcRweVh8Alsxsg7Pm/DQlsfb466iyLMn vB3zFpz/cePWrt/iz86bmE1bXQ== X-Google-Smtp-Source: ABdhPJzkirtxNi2CRcNJeilD4kxMLO1YF0VjZBrbmGbH4/HTs0pdCvfOr9uTCAf4fJEtr5wJP0aYLA== X-Received: by 2002:adf:fc83:: with SMTP id g3mr33802908wrr.200.1604518264314; Wed, 04 Nov 2020 11:31:04 -0800 (PST) Received: from debian-brgl.home (amarseille-656-1-4-167.w90-8.abo.wanadoo.fr. [90.8.158.167]) by smtp.gmail.com with ESMTPSA id m12sm4051671wrs.92.2020.11.04.11.31.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Nov 2020 11:31:03 -0800 (PST) From: Bartosz Golaszewski To: Linus Walleij , Andy Shevchenko , Jan Kiszka , David Laight , Mark Brown Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [RFT PATCH v2 6/8] gpio: exar: unduplicate address and offset computation Date: Wed, 4 Nov 2020 20:30:49 +0100 Message-Id: <20201104193051.32236-7-brgl@bgdev.pl> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20201104193051.32236-1-brgl@bgdev.pl> References: <20201104193051.32236-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski Provide and use helpers for calculating the register address and bit offset instead of hand coding it in every function. Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpio-exar.c | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/drivers/gpio/gpio-exar.c b/drivers/gpio/gpio-exar.c index db366d85b6b4..28b0b4b5fa35 100644 --- a/drivers/gpio/gpio-exar.c +++ b/drivers/gpio/gpio-exar.c @@ -33,6 +33,26 @@ struct exar_gpio_chip { unsigned int first_pin; }; +static unsigned int +exar_offset_to_sel_addr(struct exar_gpio_chip *exar_gpio, unsigned int offset) +{ + return (offset + exar_gpio->first_pin) / 8 ? EXAR_OFFSET_MPIOSEL_HI + : EXAR_OFFSET_MPIOSEL_LO; +} + +static unsigned int +exar_offset_to_lvl_addr(struct exar_gpio_chip *exar_gpio, unsigned int offset) +{ + return (offset + exar_gpio->first_pin) / 8 ? EXAR_OFFSET_MPIOLVL_HI + : EXAR_OFFSET_MPIOLVL_LO; +} + +static unsigned int +exar_offset_to_bit(struct exar_gpio_chip *exar_gpio, unsigned int offset) +{ + return (offset + exar_gpio->first_pin) % 8; +} + static void exar_update(struct gpio_chip *chip, unsigned int reg, int val, unsigned int offset) { @@ -52,9 +72,8 @@ static int exar_set_direction(struct gpio_chip *chip, int direction, unsigned int offset) { struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip); - unsigned int addr = (offset + exar_gpio->first_pin) / 8 ? - EXAR_OFFSET_MPIOSEL_HI : EXAR_OFFSET_MPIOSEL_LO; - unsigned int bit = (offset + exar_gpio->first_pin) % 8; + unsigned int addr = exar_offset_to_sel_addr(exar_gpio, offset); + unsigned int bit = exar_offset_to_bit(exar_gpio, offset); exar_update(chip, addr, direction, bit); return 0; @@ -75,9 +94,8 @@ static int exar_get(struct gpio_chip *chip, unsigned int reg) static int exar_get_direction(struct gpio_chip *chip, unsigned int offset) { struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip); - unsigned int addr = (offset + exar_gpio->first_pin) / 8 ? - EXAR_OFFSET_MPIOSEL_HI : EXAR_OFFSET_MPIOSEL_LO; - unsigned int bit = (offset + exar_gpio->first_pin) % 8; + unsigned int addr = exar_offset_to_sel_addr(exar_gpio, offset); + unsigned int bit = exar_offset_to_bit(exar_gpio, offset); if (exar_get(chip, addr) & BIT(bit)) return GPIO_LINE_DIRECTION_IN; @@ -88,9 +106,8 @@ static int exar_get_direction(struct gpio_chip *chip, unsigned int offset) static int exar_get_value(struct gpio_chip *chip, unsigned int offset) { struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip); - unsigned int addr = (offset + exar_gpio->first_pin) / 8 ? - EXAR_OFFSET_MPIOLVL_HI : EXAR_OFFSET_MPIOLVL_LO; - unsigned int bit = (offset + exar_gpio->first_pin) % 8; + unsigned int addr = exar_offset_to_lvl_addr(exar_gpio, offset); + unsigned int bit = exar_offset_to_bit(exar_gpio, offset); return !!(exar_get(chip, addr) & BIT(bit)); } @@ -99,9 +116,8 @@ static void exar_set_value(struct gpio_chip *chip, unsigned int offset, int value) { struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip); - unsigned int addr = (offset + exar_gpio->first_pin) / 8 ? - EXAR_OFFSET_MPIOLVL_HI : EXAR_OFFSET_MPIOLVL_LO; - unsigned int bit = (offset + exar_gpio->first_pin) % 8; + unsigned int addr = exar_offset_to_lvl_addr(exar_gpio, offset); + unsigned int bit = exar_offset_to_bit(exar_gpio, offset); exar_update(chip, addr, value, bit); }