Skip to content

Commit a31f5c3

Browse files
andy-shevLinus Walleij
authored andcommitted
gpio: acpi: Override GPIO initialization flags
This allows ACPI GPIO code to modify flags based on ACPI GpioIo() / GpioInt() resources. Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Tested-by: Jarkko Nikula <jarkko.nikula@linux.intel.com> Reviewed-by: Mika Westerberg <mika.westerberg@linux.intel.com> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
1 parent 2eca25a commit a31f5c3

3 files changed

Lines changed: 67 additions & 6 deletions

File tree

‎drivers/gpio/gpiolib-acpi.c‎

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,34 @@ acpi_gpio_to_gpiod_flags(const struct acpi_resource_gpio *agpio)
448448
}
449449
}
450450

451+
int
452+
acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, enum gpiod_flags update)
453+
{
454+
int ret = 0;
455+
456+
/*
457+
* Check if the BIOS has IoRestriction with explicitly set direction
458+
* and update @flags accordingly. Otherwise use whatever caller asked
459+
* for.
460+
*/
461+
if (update & GPIOD_FLAGS_BIT_DIR_SET) {
462+
enum gpiod_flags diff = *flags ^ update;
463+
464+
/*
465+
* Check if caller supplied incompatible GPIO initialization
466+
* flags.
467+
*
468+
* Return %-EINVAL to notify that firmware has different
469+
* settings and we are going to use them.
470+
*/
471+
if (((*flags & GPIOD_FLAGS_BIT_DIR_SET) && (diff & GPIOD_FLAGS_BIT_DIR_OUT)) ||
472+
((*flags & GPIOD_FLAGS_BIT_DIR_OUT) && (diff & GPIOD_FLAGS_BIT_DIR_VAL)))
473+
ret = -EINVAL;
474+
*flags = update;
475+
}
476+
return ret;
477+
}
478+
451479
struct acpi_gpio_lookup {
452480
struct acpi_gpio_info info;
453481
int index;
@@ -485,8 +513,11 @@ static int acpi_populate_gpio_lookup(struct acpi_resource *ares, void *data)
485513
* - ACPI_ACTIVE_HIGH == GPIO_ACTIVE_HIGH
486514
*/
487515
if (lookup->info.gpioint) {
516+
lookup->info.flags = GPIOD_IN;
488517
lookup->info.polarity = agpio->polarity;
489518
lookup->info.triggering = agpio->triggering;
519+
} else {
520+
lookup->info.flags = acpi_gpio_to_gpiod_flags(agpio);
490521
}
491522

492523
}
@@ -613,13 +644,14 @@ static struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
613644
struct gpio_desc *acpi_find_gpio(struct device *dev,
614645
const char *con_id,
615646
unsigned int idx,
616-
enum gpiod_flags flags,
647+
enum gpiod_flags *dflags,
617648
enum gpio_lookup_flags *lookupflags)
618649
{
619650
struct acpi_device *adev = ACPI_COMPANION(dev);
620651
struct acpi_gpio_info info;
621652
struct gpio_desc *desc;
622653
char propname[32];
654+
int err;
623655
int i;
624656

625657
/* Try first from _DSD */
@@ -650,14 +682,18 @@ struct gpio_desc *acpi_find_gpio(struct device *dev,
650682
}
651683

652684
if (info.gpioint &&
653-
(flags == GPIOD_OUT_LOW || flags == GPIOD_OUT_HIGH)) {
685+
(*dflags == GPIOD_OUT_LOW || *dflags == GPIOD_OUT_HIGH)) {
654686
dev_dbg(dev, "refusing GpioInt() entry when doing GPIOD_OUT_* lookup\n");
655687
return ERR_PTR(-ENOENT);
656688
}
657689

658690
if (info.polarity == GPIO_ACTIVE_LOW)
659691
*lookupflags |= GPIO_ACTIVE_LOW;
660692

693+
err = acpi_gpio_update_gpiod_flags(dflags, info.flags);
694+
if (err)
695+
dev_dbg(dev, "Override GPIO initialization flags\n");
696+
661697
return desc;
662698
}
663699

@@ -711,12 +747,16 @@ struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode,
711747
* used to translate from the GPIO offset in the resource to the Linux IRQ
712748
* number.
713749
*
750+
* The function is idempotent, though each time it runs it will configure GPIO
751+
* pin direction according to the flags in GpioInt resource.
752+
*
714753
* Return: Linux IRQ number (>%0) on success, negative errno on failure.
715754
*/
716755
int acpi_dev_gpio_irq_get(struct acpi_device *adev, int index)
717756
{
718757
int idx, i;
719758
unsigned int irq_flags;
759+
int ret;
720760

721761
for (i = 0, idx = 0; idx <= index; i++) {
722762
struct acpi_gpio_info info;
@@ -729,6 +769,7 @@ int acpi_dev_gpio_irq_get(struct acpi_device *adev, int index)
729769
return PTR_ERR(desc);
730770

731771
if (info.gpioint && idx++ == index) {
772+
char label[32];
732773
int irq;
733774

734775
if (IS_ERR(desc))
@@ -738,6 +779,11 @@ int acpi_dev_gpio_irq_get(struct acpi_device *adev, int index)
738779
if (irq < 0)
739780
return irq;
740781

782+
snprintf(label, sizeof(label), "GpioInt() %d", index);
783+
ret = gpiod_configure_flags(desc, label, 0, info.flags);
784+
if (ret < 0)
785+
return ret;
786+
741787
irq_flags = acpi_dev_get_irq_type(info.triggering,
742788
info.polarity);
743789

‎drivers/gpio/gpiolib.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3286,7 +3286,7 @@ struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
32863286
desc = of_find_gpio(dev, con_id, idx, &lookupflags);
32873287
} else if (ACPI_COMPANION(dev)) {
32883288
dev_dbg(dev, "using ACPI for GPIO lookup\n");
3289-
desc = acpi_find_gpio(dev, con_id, idx, flags, &lookupflags);
3289+
desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
32903290
}
32913291
}
32923292

@@ -3367,8 +3367,12 @@ struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
33673367
struct acpi_gpio_info info;
33683368

33693369
desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
3370-
if (!IS_ERR(desc))
3370+
if (!IS_ERR(desc)) {
33713371
active_low = info.polarity == GPIO_ACTIVE_LOW;
3372+
ret = acpi_gpio_update_gpiod_flags(&dflags, info.flags);
3373+
if (ret)
3374+
pr_debug("Override GPIO initialization flags\n");
3375+
}
33723376
}
33733377

33743378
if (IS_ERR(desc))

‎drivers/gpio/gpiolib.h‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,13 @@ struct gpio_device {
7575

7676
/**
7777
* struct acpi_gpio_info - ACPI GPIO specific information
78+
* @flags: GPIO initialization flags
7879
* @gpioint: if %true this GPIO is of type GpioInt otherwise type is GpioIo
7980
* @polarity: interrupt polarity as provided by ACPI
8081
* @triggering: triggering type as provided by ACPI
8182
*/
8283
struct acpi_gpio_info {
84+
enum gpiod_flags flags;
8385
bool gpioint;
8486
int polarity;
8587
int triggering;
@@ -121,10 +123,13 @@ void acpi_gpiochip_remove(struct gpio_chip *chip);
121123
void acpi_gpiochip_request_interrupts(struct gpio_chip *chip);
122124
void acpi_gpiochip_free_interrupts(struct gpio_chip *chip);
123125

126+
int acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags,
127+
enum gpiod_flags update);
128+
124129
struct gpio_desc *acpi_find_gpio(struct device *dev,
125130
const char *con_id,
126131
unsigned int idx,
127-
enum gpiod_flags flags,
132+
enum gpiod_flags *dflags,
128133
enum gpio_lookup_flags *lookupflags);
129134
struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode,
130135
const char *propname, int index,
@@ -143,9 +148,15 @@ acpi_gpiochip_request_interrupts(struct gpio_chip *chip) { }
143148
static inline void
144149
acpi_gpiochip_free_interrupts(struct gpio_chip *chip) { }
145150

151+
static inline int
152+
acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, enum gpiod_flags update)
153+
{
154+
return 0;
155+
}
156+
146157
static inline struct gpio_desc *
147158
acpi_find_gpio(struct device *dev, const char *con_id,
148-
unsigned int idx, enum gpiod_flags flags,
159+
unsigned int idx, enum gpiod_flags *dflags,
149160
enum gpio_lookup_flags *lookupflags)
150161
{
151162
return ERR_PTR(-ENOENT);

0 commit comments

Comments
 (0)