Skip to content

Commit 0f25fda

Browse files
marexlinusw
authored andcommitted
gpio: pca953x: Zap ad-hoc reg_direction cache
Replace the ad-hoc reg_direction direction register caching with generic regcache cache. This reduces code duplication. Signed-off-by: Marek Vasut <[email protected]> Cc: Bartosz Golaszewski <[email protected]> Signed-off-by: Linus Walleij <[email protected]>
1 parent 4942723 commit 0f25fda

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

drivers/gpio/gpio-pca953x.c

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ static const struct pca953x_reg_config pca957x_regs = {
142142
struct pca953x_chip {
143143
unsigned gpio_start;
144144
u8 reg_output[MAX_BANK];
145-
u8 reg_direction[MAX_BANK];
146145
struct mutex i2c_lock;
147146
struct regmap *regmap;
148147

@@ -387,18 +386,13 @@ static int pca953x_read_regs(struct pca953x_chip *chip, int reg, u8 *val)
387386
static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
388387
{
389388
struct pca953x_chip *chip = gpiochip_get_data(gc);
390-
u8 reg_val;
389+
u8 dirreg = pca953x_recalc_addr(chip, chip->regs->direction, off,
390+
true, false);
391+
u8 bit = BIT(off % BANK_SZ);
391392
int ret;
392393

393394
mutex_lock(&chip->i2c_lock);
394-
reg_val = chip->reg_direction[off / BANK_SZ] | (1u << (off % BANK_SZ));
395-
396-
ret = pca953x_write_single(chip, chip->regs->direction, reg_val, off);
397-
if (ret)
398-
goto exit;
399-
400-
chip->reg_direction[off / BANK_SZ] = reg_val;
401-
exit:
395+
ret = regmap_write_bits(chip->regmap, dirreg, bit, bit);
402396
mutex_unlock(&chip->i2c_lock);
403397
return ret;
404398
}
@@ -407,6 +401,9 @@ static int pca953x_gpio_direction_output(struct gpio_chip *gc,
407401
unsigned off, int val)
408402
{
409403
struct pca953x_chip *chip = gpiochip_get_data(gc);
404+
u8 dirreg = pca953x_recalc_addr(chip, chip->regs->direction, off,
405+
true, false);
406+
u8 bit = BIT(off % BANK_SZ);
410407
u8 reg_val;
411408
int ret;
412409

@@ -426,12 +423,7 @@ static int pca953x_gpio_direction_output(struct gpio_chip *gc,
426423
chip->reg_output[off / BANK_SZ] = reg_val;
427424

428425
/* then direction */
429-
reg_val = chip->reg_direction[off / BANK_SZ] & ~(1u << (off % BANK_SZ));
430-
ret = pca953x_write_single(chip, chip->regs->direction, reg_val, off);
431-
if (ret)
432-
goto exit;
433-
434-
chip->reg_direction[off / BANK_SZ] = reg_val;
426+
ret = regmap_write_bits(chip->regmap, dirreg, bit, 0);
435427
exit:
436428
mutex_unlock(&chip->i2c_lock);
437429
return ret;
@@ -483,16 +475,19 @@ static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
483475
static int pca953x_gpio_get_direction(struct gpio_chip *gc, unsigned off)
484476
{
485477
struct pca953x_chip *chip = gpiochip_get_data(gc);
478+
u8 dirreg = pca953x_recalc_addr(chip, chip->regs->direction, off,
479+
true, false);
480+
u8 bit = BIT(off % BANK_SZ);
486481
u32 reg_val;
487482
int ret;
488483

489484
mutex_lock(&chip->i2c_lock);
490-
ret = pca953x_read_single(chip, chip->regs->direction, &reg_val, off);
485+
ret = regmap_read(chip->regmap, dirreg, &reg_val);
491486
mutex_unlock(&chip->i2c_lock);
492487
if (ret < 0)
493488
return ret;
494489

495-
return !!(reg_val & (1u << (off % BANK_SZ)));
490+
return !!(reg_val & bit);
496491
}
497492

498493
static void pca953x_gpio_set_multiple(struct gpio_chip *gc,
@@ -580,6 +575,10 @@ static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
580575
u8 new_irqs;
581576
int level, i;
582577
u8 invert_irq_mask[MAX_BANK];
578+
int reg_direction[MAX_BANK];
579+
580+
regmap_bulk_read(chip->regmap, chip->regs->direction, reg_direction,
581+
NBANK(chip));
583582

584583
if (chip->driver_data & PCA_PCAL) {
585584
/* Enable latch on interrupt-enabled inputs */
@@ -595,7 +594,7 @@ static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
595594
/* Look for any newly setup interrupt */
596595
for (i = 0; i < NBANK(chip); i++) {
597596
new_irqs = chip->irq_trig_fall[i] | chip->irq_trig_raise[i];
598-
new_irqs &= ~chip->reg_direction[i];
597+
new_irqs &= reg_direction[i];
599598

600599
while (new_irqs) {
601600
level = __ffs(new_irqs);
@@ -660,6 +659,7 @@ static bool pca953x_irq_pending(struct pca953x_chip *chip, u8 *pending)
660659
bool pending_seen = false;
661660
bool trigger_seen = false;
662661
u8 trigger[MAX_BANK];
662+
int reg_direction[MAX_BANK];
663663
int ret, i;
664664

665665
if (chip->driver_data & PCA_PCAL) {
@@ -690,8 +690,10 @@ static bool pca953x_irq_pending(struct pca953x_chip *chip, u8 *pending)
690690
return false;
691691

692692
/* Remove output pins from the equation */
693+
regmap_bulk_read(chip->regmap, chip->regs->direction, reg_direction,
694+
NBANK(chip));
693695
for (i = 0; i < NBANK(chip); i++)
694-
cur_stat[i] &= chip->reg_direction[i];
696+
cur_stat[i] &= reg_direction[i];
695697

696698
memcpy(old_stat, chip->irq_stat, NBANK(chip));
697699

@@ -745,6 +747,7 @@ static int pca953x_irq_setup(struct pca953x_chip *chip,
745747
int irq_base)
746748
{
747749
struct i2c_client *client = chip->client;
750+
int reg_direction[MAX_BANK];
748751
int ret, i;
749752

750753
if (client->irq && irq_base != -1
@@ -759,8 +762,10 @@ static int pca953x_irq_setup(struct pca953x_chip *chip,
759762
* interrupt. We have to rely on the previous read for
760763
* this purpose.
761764
*/
765+
regmap_bulk_read(chip->regmap, chip->regs->direction,
766+
reg_direction, NBANK(chip));
762767
for (i = 0; i < NBANK(chip); i++)
763-
chip->irq_stat[i] &= chip->reg_direction[i];
768+
chip->irq_stat[i] &= reg_direction[i];
764769
mutex_init(&chip->irq_lock);
765770

766771
ret = devm_request_threaded_irq(&client->dev,
@@ -817,9 +822,9 @@ static int device_pca95xx_init(struct pca953x_chip *chip, u32 invert)
817822
if (ret)
818823
goto out;
819824

820-
ret = pca953x_read_regs(chip, chip->regs->direction,
821-
chip->reg_direction);
822-
if (ret)
825+
ret = regcache_sync_region(chip->regmap, chip->regs->direction,
826+
chip->regs->direction + NBANK(chip));
827+
if (ret != 0)
823828
goto out;
824829

825830
/* set platform specific polarity inversion */
@@ -937,6 +942,8 @@ static int pca953x_probe(struct i2c_client *client,
937942
goto err_exit;
938943
}
939944

945+
regcache_mark_dirty(chip->regmap);
946+
940947
mutex_init(&chip->i2c_lock);
941948
/*
942949
* In case we have an i2c-mux controlled by a GPIO provided by an

0 commit comments

Comments
 (0)