From patchwork Wed Sep 7 13:37:50 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 667018 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3sTl0L0JqBz9s9N for ; Wed, 7 Sep 2016 23:38:10 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=baylibre-com.20150623.gappssmtp.com header.i=@baylibre-com.20150623.gappssmtp.com header.b=ds1UPr/p; dkim-atps=neutral Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757804AbcIGNiE (ORCPT ); Wed, 7 Sep 2016 09:38:04 -0400 Received: from mail-wm0-f53.google.com ([74.125.82.53]:36102 "EHLO mail-wm0-f53.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757773AbcIGNiB (ORCPT ); Wed, 7 Sep 2016 09:38:01 -0400 Received: by mail-wm0-f53.google.com with SMTP id b187so116471319wme.1 for ; Wed, 07 Sep 2016 06:38:00 -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=IFbi/V8k0lH9+JyOX+2WDUpht67MdrfLADZpzU7xYys=; b=ds1UPr/peFx6mHMhoQdM7kbFfjvT1OK87xnOBP1P1DttIdtd2PqGQf5wf0V921xUCq KWClQgzkDsYbASAgm/WNx6PKT5vhHnEoYYoQMRHa/AiLGliyQi7nTxVmJtPQLgaFEig2 pzGZ8zPo1n/Dr5mlfZqBfXmRVtJw/rUsGuxQnOyoDvax6yMGKmw5jPxvLq6/OIxsxmnv n6UW9O9/kwBpbwlerUnRLtNX7TNQSyS4WAW1tx+xQT6kV3tjQMAxo/2DmApURjro/+Ux WAilVWZIu9lJMMKpAmWDmL5rbeXnPZmnr3FSMC96iV2u2supsNvrKs5kTxz/XcLqbru8 EK1A== 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=IFbi/V8k0lH9+JyOX+2WDUpht67MdrfLADZpzU7xYys=; b=iMZzMavpWB3fuxqTvTybF1eaouvNVNveyUuYo3NCaXZqOIlvUNgn3PcT7NMgL6kzyx QTxrpq+wm13kGl7A7THgUR48eO9esc2qtggoBfea1vQCrqxdy3eArAu2Ju3xoi3ua79g YwfA3nwfHUhMnI9+dqLLux2KN+eOzaKbwzCy+P7UYW8notsX/8sfcXwhIlz/1ylscuRH KU44xSnIviqhgwNJ3J8l3Xn7EAhgKF/HLlOIaCEav1qg2ZGA+ArAGYTsOBCKJ3SmHVN+ NFNxELJjlDwt8h5S4FhOj97KQszgU6DyHa0H20MMDGtLZO6JgfzB05NU6ZyTuzJklzDA 6NVw== X-Gm-Message-State: AE9vXwOtJ+QaxPKYMo6XOlzqkN5yGlJcUhCY4k+8nnxQZELA0DjQ1WUAgQB1yj1Z6aYQ/gBx X-Received: by 10.194.61.164 with SMTP id q4mr34040887wjr.89.1473255480074; Wed, 07 Sep 2016 06:38:00 -0700 (PDT) Received: from localhost.localdomain ([90.63.244.31]) by smtp.gmail.com with ESMTPSA id 190sm4363889wmk.13.2016.09.07.06.37.59 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 07 Sep 2016 06:37:59 -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 v3 3/5] gpio: pca953x: refactor pca953x_read_regs() Date: Wed, 7 Sep 2016 15:37:50 +0200 Message-Id: <1473255472-16090-4-git-send-email-bgolaszewski@baylibre.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1473255472-16090-1-git-send-email-bgolaszewski@baylibre.com> References: <1473255472-16090-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 --- drivers/gpio/gpio-pca953x.c | 56 +++++++++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/drivers/gpio/gpio-pca953x.c b/drivers/gpio/gpio-pca953x.c index b3020ee..018bd18 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; @@ -762,14 +780,18 @@ static int pca953x_probe(struct i2c_client *client, */ pca953x_setup_gpio(chip, chip->driver_data & PCA_GPIO_MASK); - if (chip->gpio_chip.ngpio <= 8) + if (chip->gpio_chip.ngpio <= 8) { chip->write_regs = pca953x_write_regs_8; - else if (chip->gpio_chip.ngpio >= 24) + chip->read_regs = pca953x_read_regs_8; + } else if (chip->gpio_chip.ngpio >= 24) { chip->write_regs = pca953x_write_regs_24; - else + chip->read_regs = pca953x_read_regs_24; + } else { chip->write_regs = chip->chip_type == PCA953X_TYPE ? pca953x_write_regs_16 : pca957x_write_regs_16; + chip->read_regs = pca953x_read_regs_16; + } if (chip->chip_type == PCA953X_TYPE) ret = device_pca953x_init(chip, invert);