Skip to content

Commit 96d7c7b

Browse files
pthomasbrgl
authored andcommitted
gpio: gpio-pca953x, Add get_multiple function
Implement a get_multiple function for gpio-pca953x. If a driver leaves get_multiple unimplemented then gpio_chip_get_multiple() in gpiolib.c takes care of it by calling chip->get() as needed. For i2c chips this is very inefficient. For example if you do an 8-bit read then instead of a single i2c transaction there are 8 transactions reading the same byte! Signed-off-by: Paul Thomas <pthomas8589@gmail.com> Acked-by: Linus Walleij <linus.walleij@linaro.org> Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
1 parent ea06a48 commit 96d7c7b

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

drivers/gpio/gpio-pca953x.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ MODULE_DEVICE_TABLE(acpi, pca953x_acpi_ids);
115115

116116
#define MAX_BANK 5
117117
#define BANK_SZ 8
118+
#define BANK_SFT 3 /* ilog2(BANK_SZ) */
118119
#define MAX_LINE (MAX_BANK * BANK_SZ)
119120

120121
#define NBANK(chip) DIV_ROUND_UP(chip->gpio_chip.ngpio, BANK_SZ)
@@ -466,6 +467,41 @@ static int pca953x_gpio_get_direction(struct gpio_chip *gc, unsigned off)
466467
return GPIO_LINE_DIRECTION_OUT;
467468
}
468469

470+
static int pca953x_gpio_get_multiple(struct gpio_chip *gc,
471+
unsigned long *mask, unsigned long *bits)
472+
{
473+
struct pca953x_chip *chip = gpiochip_get_data(gc);
474+
unsigned int reg_val;
475+
int offset, value, i, ret = 0;
476+
u8 inreg;
477+
478+
/* Force offset outside the range of i so that
479+
* at least the first relevant register is read
480+
*/
481+
offset = gc->ngpio;
482+
for_each_set_bit(i, mask, gc->ngpio) {
483+
/* whenever i goes into a new bank update inreg
484+
* and read the register
485+
*/
486+
if ((offset >> BANK_SFT) != (i >> BANK_SFT)) {
487+
offset = i;
488+
inreg = pca953x_recalc_addr(chip, chip->regs->input,
489+
offset, true, false);
490+
mutex_lock(&chip->i2c_lock);
491+
ret = regmap_read(chip->regmap, inreg, &reg_val);
492+
mutex_unlock(&chip->i2c_lock);
493+
if (ret < 0)
494+
return ret;
495+
}
496+
/* reg_val is relative to the last read byte,
497+
* so only shift the relative bits
498+
*/
499+
value = (reg_val >> (i % 8)) & 0x01;
500+
__assign_bit(i, bits, value);
501+
}
502+
return ret;
503+
}
504+
469505
static void pca953x_gpio_set_multiple(struct gpio_chip *gc,
470506
unsigned long *mask, unsigned long *bits)
471507
{
@@ -551,6 +587,7 @@ static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
551587
gc->get = pca953x_gpio_get_value;
552588
gc->set = pca953x_gpio_set_value;
553589
gc->get_direction = pca953x_gpio_get_direction;
590+
gc->get_multiple = pca953x_gpio_get_multiple;
554591
gc->set_multiple = pca953x_gpio_set_multiple;
555592
gc->set_config = pca953x_gpio_set_config;
556593
gc->can_sleep = true;

0 commit comments

Comments
 (0)