From patchwork Wed Sep 7 14:49:28 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 75672 Delivered-To: patch@linaro.org Received: by 10.140.106.11 with SMTP id d11csp365122qgf; Wed, 7 Sep 2016 07:52:24 -0700 (PDT) X-Received: by 10.66.254.102 with SMTP id ah6mr84196871pad.59.1473259943892; Wed, 07 Sep 2016 07:52:23 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id ul1si41536545pac.252.2016.09.07.07.52.23; Wed, 07 Sep 2016 07:52:23 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-gpio-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@baylibre-com.20150623.gappssmtp.com; spf=pass (google.com: best guess record for domain of linux-gpio-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-gpio-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752810AbcIGOuM (ORCPT + 4 others); Wed, 7 Sep 2016 10:50:12 -0400 Received: from mail-wm0-f51.google.com ([74.125.82.51]:36128 "EHLO mail-wm0-f51.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S938707AbcIGOtn (ORCPT ); Wed, 7 Sep 2016 10:49:43 -0400 Received: by mail-wm0-f51.google.com with SMTP id b187so120180413wme.1 for ; Wed, 07 Sep 2016 07:49:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=6sTuKv61wj6vv6zWVolYFeRRy51spQuViCt8HdS+6Ac=; b=sEBbBQ11zixI2OJ4cGk1irsQzm5RBEhX1E0BAsI9/aBIJoGglCBek0abRfEWTsUlcx sKCJsKT670QMsarKJHY3Pi21FV7e6twRtH+TD7py81qMh3NLMkoZrTsHtXE8vJtOLyIJ R/PvFJr2TILXAWi7fPfu9rEcqMZXAWqdMK+aye3i41iA0eXDqbgaoU3B5G4OrYWV6FOs kCsTiPtZy4A6fmrNzv8c2HeS+YCBUjUft6k6ZZEknKJvgNRYSFUhu6XaKiyWarnQJNFu AaDwRCVBaBTcrVQBUrIb91MDmG3aw3kK14ILI/GoljcptBPlE/kLeZuuX+O7tShnmfef EA5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=6sTuKv61wj6vv6zWVolYFeRRy51spQuViCt8HdS+6Ac=; b=G0yNw9/n86ELSG6rFwncs9Zp73+22TiOCYIq/ubDzsIX7m3g/rGPI7yzCKK65nhegL z4TIYNPYfzSQPe6Gi2qMIKiYnrs3FLPrD91oOeCBa07j+J2hoYpXebrBcdBQLPRzmJzr slaTOXMskLtINU/vIIHGI/SofmlDtshWCU19/4h9EKJ6GlBGE2dnpClTr+oaCdOT4b7Q iVyVk4CpYAsV+2TuNIp9J6ow3w8CH9jmvWSV9JCXe8oBjUMtJWm15Wu1ETsSLGD+3JA6 sfve/agNgSnHHPDTp+eL2f77bRcxOdoD5/PpEf91PBRPzlsY5sF/uQooHoOi8so5rhiY S38Q== X-Gm-Message-State: AE9vXwOoUsz67CxA00waJlUqKbToIMW1NXKDCK9U9tU5ZkYT1wsAjKEh4zXKGvQ9DVoBwSGQ X-Received: by 10.28.12.133 with SMTP id 127mr4116145wmm.119.1473259781864; Wed, 07 Sep 2016 07:49:41 -0700 (PDT) Received: from localhost.localdomain ([90.63.244.31]) by smtp.gmail.com with ESMTPSA id s8sm8295544wjz.29.2016.09.07.07.49.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 07 Sep 2016 07:49:41 -0700 (PDT) From: Bartosz Golaszewski To: Linus Walleij , Alexandre Courbot , Andy Shevchenko , Vignesh R , Yong Li , Geert Uytterhoeven Cc: linux-gpio , LKML , Bartosz Golaszewski Subject: [PATCH v4 3/5] gpio: pca953x: refactor pca953x_read_regs() Date: Wed, 7 Sep 2016 16:49:28 +0200 Message-Id: <1473259770-26224-4-git-send-email-bgolaszewski@baylibre.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1473259770-26224-1-git-send-email-bgolaszewski@baylibre.com> References: <1473259770-26224-1-git-send-email-bgolaszewski@baylibre.com> Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Avoid the unnecessary if-else in pca953x_read_regs() by spltting the routine into smaller, specialized functions and calling the right one via a function pointer held in struct pca953x. Signed-off-by: Bartosz Golaszewski Reviewed-by: Andy Shevchenko --- drivers/gpio/gpio-pca953x.c | 49 ++++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 14 deletions(-) -- 2.7.4 -- To unsubscribe from this list: send the line "unsubscribe linux-gpio" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/drivers/gpio/gpio-pca953x.c b/drivers/gpio/gpio-pca953x.c index 093bec1..52165ce 100644 --- a/drivers/gpio/gpio-pca953x.c +++ b/drivers/gpio/gpio-pca953x.c @@ -135,6 +135,7 @@ struct pca953x_chip { const struct pca953x_offset *offset; int (*write_regs)(struct pca953x_chip *, int, u8 *); + int (*read_regs)(struct pca953x_chip *, int, u8 *); }; static int pca953x_read_single(struct pca953x_chip *chip, int reg, u32 *val, @@ -220,24 +221,41 @@ static int pca953x_write_regs(struct pca953x_chip *chip, int reg, u8 *val) return 0; } -static int pca953x_read_regs(struct pca953x_chip *chip, int reg, u8 *val) +static int pca953x_read_regs_8(struct pca953x_chip *chip, int reg, u8 *val) { int ret; - if (chip->gpio_chip.ngpio <= 8) { - ret = i2c_smbus_read_byte_data(chip->client, reg); - *val = ret; - } else if (chip->gpio_chip.ngpio >= 24) { - int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ); + ret = i2c_smbus_read_byte_data(chip->client, reg); + *val = ret; - ret = i2c_smbus_read_i2c_block_data(chip->client, - (reg << bank_shift) | REG_ADDR_AI, - NBANK(chip), val); - } else { - ret = i2c_smbus_read_word_data(chip->client, reg << 1); - val[0] = (u16)ret & 0xFF; - val[1] = (u16)ret >> 8; - } + return ret; +} + +static int pca953x_read_regs_16(struct pca953x_chip *chip, int reg, u8 *val) +{ + int ret; + + ret = i2c_smbus_read_word_data(chip->client, reg << 1); + val[0] = (u16)ret & 0xFF; + val[1] = (u16)ret >> 8; + + return ret; +} + +static int pca953x_read_regs_24(struct pca953x_chip *chip, int reg, u8 *val) +{ + int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ); + + return i2c_smbus_read_i2c_block_data(chip->client, + (reg << bank_shift) | REG_ADDR_AI, + NBANK(chip), val); +} + +static int pca953x_read_regs(struct pca953x_chip *chip, int reg, u8 *val) +{ + int ret; + + ret = chip->read_regs(chip, reg, val); if (ret < 0) { dev_err(&chip->client->dev, "failed reading register\n"); return ret; @@ -764,13 +782,16 @@ static int pca953x_probe(struct i2c_client *client, if (chip->gpio_chip.ngpio <= 8) { chip->write_regs = pca953x_write_regs_8; + chip->read_regs = pca953x_read_regs_8; } else if (chip->gpio_chip.ngpio >= 24) { chip->write_regs = pca953x_write_regs_24; + chip->read_regs = pca953x_read_regs_24; } else { if (PCA_CHIP_TYPE(chip->driver_data) == PCA953X_TYPE) chip->write_regs = pca953x_write_regs_16; else chip->write_regs = pca957x_write_regs_16; + chip->read_regs = pca953x_read_regs_16; } if (chip->chip_type == PCA953X_TYPE)