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>
This commit is contained in:
Marek Vasut 2018-12-12 02:39:57 +01:00 committed by Linus Walleij
parent 25a1b7102f
commit b32cecb46b
1 changed files with 29 additions and 30 deletions

View File

@ -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) {