Skip to content

Commit 72d3200

Browse files
committed
gpio: set up initial state from .get_direction()
If the gpiochip supports the .get_direction() callback, then the initial state of the descriptor flags should be set up as output accordingly. Also put in comments explaining what is going on. Signed-off-by: Linus Walleij <[email protected]>
1 parent e9f4d56 commit 72d3200

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

drivers/gpio/gpiolib.c

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -565,14 +565,31 @@ int gpiochip_add_data(struct gpio_chip *chip, void *data)
565565
struct gpio_desc *desc = &gdev->descs[i];
566566

567567
desc->gdev = gdev;
568-
569-
/* REVISIT: most hardware initializes GPIOs as inputs (often
570-
* with pullups enabled) so power usage is minimized. Linux
571-
* code should set the gpio direction first thing; but until
572-
* it does, and in case chip->get_direction is not set, we may
573-
* expose the wrong direction in sysfs.
568+
/*
569+
* REVISIT: most hardware initializes GPIOs as inputs
570+
* (often with pullups enabled) so power usage is
571+
* minimized. Linux code should set the gpio direction
572+
* first thing; but until it does, and in case
573+
* chip->get_direction is not set, we may expose the
574+
* wrong direction in sysfs.
574575
*/
575-
desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
576+
577+
if (chip->get_direction) {
578+
/*
579+
* If we have .get_direction, set up the initial
580+
* direction flag from the hardware.
581+
*/
582+
int dir = chip->get_direction(chip, i);
583+
584+
if (!dir)
585+
set_bit(FLAG_IS_OUT, &desc->flags);
586+
} else if (!chip->direction_input) {
587+
/*
588+
* If the chip lacks the .direction_input callback
589+
* we logically assume all lines are outputs.
590+
*/
591+
set_bit(FLAG_IS_OUT, &desc->flags);
592+
}
576593
}
577594

578595
spin_unlock_irqrestore(&gpio_lock, flags);

0 commit comments

Comments
 (0)