diff options
author | Marek Vasut <marek.vasut@gmail.com> | 2018-12-12 02:39:57 +0100 |
---|---|---|
committer | Linus Walleij <linus.walleij@linaro.org> | 2018-12-14 15:19:07 +0100 |
commit | b32cecb46bdc8b3ea36c7a71aaca1853c0342cae (patch) | |
tree | 4ba782795775440cb2285f09218f39f08732afa8 /drivers/gpio | |
parent | 25a1b7102f3f1181be1feaa0ed6ff82f1b526acd (diff) |
gpio: pca953x: Extract the register address mangling to single function
Instead of having the I2C register calculation function spread across
multiple accessor functions, pull it out into a single function which
returns the adjusted register address.
Signed-off-by: Marek Vasut <marek.vasut+renesas@gmail.com>
Cc: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
Diffstat (limited to 'drivers/gpio')
-rw-r--r-- | drivers/gpio/gpio-pca953x.c | 59 |
1 files changed, 29 insertions, 30 deletions
diff --git a/drivers/gpio/gpio-pca953x.c b/drivers/gpio/gpio-pca953x.c index b3386819c550..7e66f46b41b2 100644 --- a/drivers/gpio/gpio-pca953x.c +++ b/drivers/gpio/gpio-pca953x.c @@ -164,17 +164,37 @@ static int pca953x_bank_shift(struct pca953x_chip *chip) return fls((chip->gpio_chip.ngpio - 1) / BANK_SZ); } +static u8 pca953x_recalc_addr(struct pca953x_chip *chip, int reg, int off, + bool write, bool addrinc) +{ + int bank_shift = pca953x_bank_shift(chip); + int addr = (reg & PCAL_GPIO_MASK) << bank_shift; + int pinctrl = (reg & PCAL_PINCTRL_MASK) << 1; + u8 regaddr = pinctrl | addr | (off / BANK_SZ); + + /* Single byte read doesn't need AI bit set. */ + if (!addrinc) + return regaddr; + + /* Chips with 24 and more GPIOs always support Auto Increment */ + if (write && NBANK(chip) > 2) + regaddr |= REG_ADDR_AI; + + /* PCA9575 needs address-increment on multi-byte writes */ + if (PCA_CHIP_TYPE(chip->driver_data) == PCA957X_TYPE) + regaddr |= REG_ADDR_AI; + + return regaddr; +} + static int pca953x_read_single(struct pca953x_chip *chip, int reg, u32 *val, int off) { + u8 regaddr = pca953x_recalc_addr(chip, reg, off, false, false); int ret; - int bank_shift = pca953x_bank_shift(chip); - int offset = off / BANK_SZ; - ret = i2c_smbus_read_byte_data(chip->client, - (reg << bank_shift) + offset); + ret = i2c_smbus_read_byte_data(chip->client, regaddr); *val = ret; - if (ret < 0) { dev_err(&chip->client->dev, "failed reading register\n"); return ret; @@ -186,13 +206,10 @@ static int pca953x_read_single(struct pca953x_chip *chip, int reg, u32 *val, static int pca953x_write_single(struct pca953x_chip *chip, int reg, u32 val, int off) { + u8 regaddr = pca953x_recalc_addr(chip, reg, off, true, false); int ret; - int bank_shift = pca953x_bank_shift(chip); - int offset = off / BANK_SZ; - - ret = i2c_smbus_write_byte_data(chip->client, - (reg << bank_shift) + offset, val); + ret = i2c_smbus_write_byte_data(chip->client, regaddr, val); if (ret < 0) { dev_err(&chip->client->dev, "failed writing register\n"); return ret; @@ -203,20 +220,9 @@ static int pca953x_write_single(struct pca953x_chip *chip, int reg, u32 val, static int pca953x_write_regs(struct pca953x_chip *chip, int reg, u8 *val) { - int bank_shift = pca953x_bank_shift(chip); - int addr = (reg & PCAL_GPIO_MASK) << bank_shift; - int pinctrl = (reg & PCAL_PINCTRL_MASK) << 1; - u8 regaddr = pinctrl | addr; + u8 regaddr = pca953x_recalc_addr(chip, reg, 0, true, true); int ret; - /* Chips with 24 and more GPIOs always support Auto Increment */ - if (NBANK(chip) > 2) - regaddr |= REG_ADDR_AI; - - /* PCA9575 needs address-increment on multi-byte writes */ - if (PCA_CHIP_TYPE(chip->driver_data) == PCA957X_TYPE) - regaddr |= REG_ADDR_AI; - ret = i2c_smbus_write_i2c_block_data(chip->client, regaddr, NBANK(chip), val); if (ret < 0) { @@ -229,16 +235,9 @@ static int pca953x_write_regs(struct pca953x_chip *chip, int reg, u8 *val) static int pca953x_read_regs(struct pca953x_chip *chip, int reg, u8 *val) { - int bank_shift = pca953x_bank_shift(chip); - int addr = (reg & PCAL_GPIO_MASK) << bank_shift; - int pinctrl = (reg & PCAL_PINCTRL_MASK) << 1; - u8 regaddr = pinctrl | addr; + u8 regaddr = pca953x_recalc_addr(chip, reg, 0, false, true); int ret; - /* Chips with 24 and more GPIOs always support Auto Increment */ - if (NBANK(chip) > 2) - regaddr |= REG_ADDR_AI; - ret = i2c_smbus_read_i2c_block_data(chip->client, regaddr, NBANK(chip), val); if (ret < 0) { |