blob: 31fee5e918b7d14fafde0477231316fee14dbb53 [file] [log] [blame]
Linus Walleijdae5f0a2018-09-25 09:08:48 +02001// SPDX-License-Identifier: GPL-2.0
Mathias Nymane29482e2012-11-30 12:37:36 +01002/*
3 * ACPI helpers for GPIO API
4 *
5 * Copyright (C) 2012, Intel Corporation
6 * Authors: Mathias Nyman <mathias.nyman@linux.intel.com>
7 * Mika Westerberg <mika.westerberg@linux.intel.com>
Mathias Nymane29482e2012-11-30 12:37:36 +01008 */
9
Hans de Goede61f7f7c2019-08-27 22:28:35 +020010#include <linux/dmi.h>
Mathias Nymane29482e2012-11-30 12:37:36 +010011#include <linux/errno.h>
Mika Westerberg936e15d2013-10-10 11:01:08 +030012#include <linux/gpio/consumer.h>
Mika Westerberg5ccff852014-01-08 12:40:56 +020013#include <linux/gpio/driver.h>
Linus Walleij031ba282016-10-03 10:40:03 +020014#include <linux/gpio/machine.h>
Mathias Nymane29482e2012-11-30 12:37:36 +010015#include <linux/export.h>
Mathias Nymane29482e2012-11-30 12:37:36 +010016#include <linux/acpi.h>
Mathias Nyman0d1c28a2013-01-28 16:23:10 +020017#include <linux/interrupt.h>
Mika Westerberg473ed7b2014-03-14 17:58:07 +020018#include <linux/mutex.h>
Mika Westerberg354567e2014-11-03 13:01:32 +020019#include <linux/pinctrl/pinctrl.h>
Mathias Nymane29482e2012-11-30 12:37:36 +010020
Mika Westerberg5ccff852014-01-08 12:40:56 +020021#include "gpiolib.h"
Andy Shevchenko77cb9072019-07-30 13:43:36 +030022#include "gpiolib-acpi.h"
Mika Westerberg5ccff852014-01-08 12:40:56 +020023
Hans de Goede1ad1b542020-01-05 17:03:56 +010024#define QUIRK_NO_EDGE_EVENTS_ON_BOOT 0x01l
Hans de Goedeaa23ca32020-01-05 17:03:57 +010025#define QUIRK_NO_WAKEUP 0x02l
Hans de Goede1ad1b542020-01-05 17:03:56 +010026
Hans de Goede61f7f7c2019-08-27 22:28:35 +020027static int run_edge_events_on_boot = -1;
28module_param(run_edge_events_on_boot, int, 0444);
29MODULE_PARM_DESC(run_edge_events_on_boot,
30 "Run edge _AEI event-handlers at boot: 0=no, 1=yes, -1=auto");
31
Hans de Goedeaa23ca32020-01-05 17:03:57 +010032static int honor_wakeup = -1;
33module_param(honor_wakeup, int, 0444);
34MODULE_PARM_DESC(honor_wakeup,
35 "Honor the ACPI wake-capable flag: 0=no, 1=yes, -1=auto");
36
Hans de Goedee59f5e02018-11-28 17:57:55 +010037/**
38 * struct acpi_gpio_event - ACPI GPIO event handler data
39 *
40 * @node: list-entry of the events list of the struct acpi_gpio_chip
41 * @handle: handle of ACPI method to execute when the IRQ triggers
Andy Shevchenko85edcd02019-03-30 00:33:45 +030042 * @handler: handler function to pass to request_irq() when requesting the IRQ
43 * @pin: GPIO pin number on the struct gpio_chip
44 * @irq: Linux IRQ number for the event, for request_irq() / free_irq()
45 * @irqflags: flags to pass to request_irq() when requesting the IRQ
Hans de Goedee59f5e02018-11-28 17:57:55 +010046 * @irq_is_wake: If the ACPI flags indicate the IRQ is a wakeup source
Andy Shevchenko85edcd02019-03-30 00:33:45 +030047 * @irq_requested:True if request_irq() has been done
48 * @desc: struct gpio_desc for the GPIO pin for this event
Hans de Goedee59f5e02018-11-28 17:57:55 +010049 */
Mika Westerberg4b01a142014-03-10 14:54:52 +020050struct acpi_gpio_event {
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +020051 struct list_head node;
Mika Westerberg4b01a142014-03-10 14:54:52 +020052 acpi_handle handle;
Hans de Goedee59f5e02018-11-28 17:57:55 +010053 irq_handler_t handler;
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +020054 unsigned int pin;
55 unsigned int irq;
Hans de Goedee59f5e02018-11-28 17:57:55 +010056 unsigned long irqflags;
57 bool irq_is_wake;
58 bool irq_requested;
Alexandre Courbote46cf322014-08-19 10:06:08 -070059 struct gpio_desc *desc;
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +020060};
61
Mika Westerberg473ed7b2014-03-14 17:58:07 +020062struct acpi_gpio_connection {
63 struct list_head node;
Alexandre Courbote46cf322014-08-19 10:06:08 -070064 unsigned int pin;
Mika Westerberg473ed7b2014-03-14 17:58:07 +020065 struct gpio_desc *desc;
66};
67
Mika Westerbergaa92b6f2014-03-10 14:54:51 +020068struct acpi_gpio_chip {
Mika Westerberg473ed7b2014-03-14 17:58:07 +020069 /*
70 * ACPICA requires that the first field of the context parameter
71 * passed to acpi_install_address_space_handler() is large enough
72 * to hold struct acpi_connection_info.
73 */
74 struct acpi_connection_info conn_info;
75 struct list_head conns;
76 struct mutex conn_lock;
Mika Westerbergaa92b6f2014-03-10 14:54:51 +020077 struct gpio_chip *chip;
Mika Westerberg4b01a142014-03-10 14:54:52 +020078 struct list_head events;
Hans de Goede78d3a922018-08-14 16:07:03 +020079 struct list_head deferred_req_irqs_list_entry;
Mika Westerbergaa92b6f2014-03-10 14:54:51 +020080};
81
Hans de Goede78d3a922018-08-14 16:07:03 +020082/*
Andy Shevchenko85edcd02019-03-30 00:33:45 +030083 * For GPIO chips which call acpi_gpiochip_request_interrupts() before late_init
Hans de Goedee59f5e02018-11-28 17:57:55 +010084 * (so builtin drivers) we register the ACPI GpioInt IRQ handlers from a
Andy Shevchenko85edcd02019-03-30 00:33:45 +030085 * late_initcall_sync() handler, so that other builtin drivers can register their
86 * OpRegions before the event handlers can run. This list contains GPIO chips
Hans de Goedee59f5e02018-11-28 17:57:55 +010087 * for which the acpi_gpiochip_request_irqs() call has been deferred.
Hans de Goede78d3a922018-08-14 16:07:03 +020088 */
89static DEFINE_MUTEX(acpi_gpio_deferred_req_irqs_lock);
90static LIST_HEAD(acpi_gpio_deferred_req_irqs_list);
91static bool acpi_gpio_deferred_req_irqs_done;
Benjamin Tissoiresca876c72018-07-12 17:25:06 +020092
Mathias Nymane29482e2012-11-30 12:37:36 +010093static int acpi_gpiochip_find(struct gpio_chip *gc, void *data)
94{
Linus Walleij58383c782015-11-04 09:56:26 +010095 if (!gc->parent)
Mathias Nymane29482e2012-11-30 12:37:36 +010096 return false;
97
Linus Walleij58383c782015-11-04 09:56:26 +010098 return ACPI_HANDLE(gc->parent) == data;
Mathias Nymane29482e2012-11-30 12:37:36 +010099}
100
101/**
Mika Westerberg936e15d2013-10-10 11:01:08 +0300102 * acpi_get_gpiod() - Translate ACPI GPIO pin to GPIO descriptor usable with GPIO API
Mathias Nymane29482e2012-11-30 12:37:36 +0100103 * @path: ACPI GPIO controller full path name, (e.g. "\\_SB.GPO1")
104 * @pin: ACPI GPIO pin number (0-based, controller-relative)
105 *
Mika Westerbergf35bbf62015-06-10 16:05:05 +0300106 * Return: GPIO descriptor to use with Linux generic GPIO API, or ERR_PTR
107 * error value. Specifically returns %-EPROBE_DEFER if the referenced GPIO
Andy Shevchenko85edcd02019-03-30 00:33:45 +0300108 * controller does not have GPIO chip registered at the moment. This is to
Mika Westerbergf35bbf62015-06-10 16:05:05 +0300109 * support probe deferral.
Mathias Nymane29482e2012-11-30 12:37:36 +0100110 */
Mika Westerberg936e15d2013-10-10 11:01:08 +0300111static struct gpio_desc *acpi_get_gpiod(char *path, int pin)
Mathias Nymane29482e2012-11-30 12:37:36 +0100112{
113 struct gpio_chip *chip;
114 acpi_handle handle;
115 acpi_status status;
116
117 status = acpi_get_handle(NULL, path, &handle);
118 if (ACPI_FAILURE(status))
Mika Westerberg936e15d2013-10-10 11:01:08 +0300119 return ERR_PTR(-ENODEV);
Mathias Nymane29482e2012-11-30 12:37:36 +0100120
121 chip = gpiochip_find(handle, acpi_gpiochip_find);
122 if (!chip)
Mika Westerbergf35bbf62015-06-10 16:05:05 +0300123 return ERR_PTR(-EPROBE_DEFER);
Mathias Nymane29482e2012-11-30 12:37:36 +0100124
Mika Westerberg03c47492017-11-27 16:54:42 +0300125 return gpiochip_get_desc(chip, pin);
Mathias Nymane29482e2012-11-30 12:37:36 +0100126}
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200127
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200128static irqreturn_t acpi_gpio_irq_handler(int irq, void *data)
129{
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200130 struct acpi_gpio_event *event = data;
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200131
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200132 acpi_evaluate_object(event->handle, NULL, NULL, NULL);
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200133
134 return IRQ_HANDLED;
135}
136
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +0200137static irqreturn_t acpi_gpio_irq_handler_evt(int irq, void *data)
138{
Mika Westerberg4b01a142014-03-10 14:54:52 +0200139 struct acpi_gpio_event *event = data;
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +0200140
Mika Westerberg4b01a142014-03-10 14:54:52 +0200141 acpi_execute_simple_method(event->handle, NULL, event->pin);
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +0200142
143 return IRQ_HANDLED;
144}
145
Mika Westerbergaa92b6f2014-03-10 14:54:51 +0200146static void acpi_gpio_chip_dh(acpi_handle handle, void *data)
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +0200147{
148 /* The address of this function is used as a key. */
149}
150
Andy Shevchenko25e3ef82017-05-23 20:03:24 +0300151bool acpi_gpio_get_irq_resource(struct acpi_resource *ares,
152 struct acpi_resource_gpio **agpio)
153{
154 struct acpi_resource_gpio *gpio;
155
156 if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
157 return false;
158
159 gpio = &ares->data.gpio;
160 if (gpio->connection_type != ACPI_RESOURCE_GPIO_TYPE_INT)
161 return false;
162
163 *agpio = gpio;
164 return true;
165}
166EXPORT_SYMBOL_GPL(acpi_gpio_get_irq_resource);
167
Hans de Goedee59f5e02018-11-28 17:57:55 +0100168static void acpi_gpiochip_request_irq(struct acpi_gpio_chip *acpi_gpio,
169 struct acpi_gpio_event *event)
170{
171 int ret, value;
172
173 ret = request_threaded_irq(event->irq, NULL, event->handler,
174 event->irqflags, "ACPI:Event", event);
175 if (ret) {
176 dev_err(acpi_gpio->chip->parent,
177 "Failed to setup interrupt handler for %d\n",
178 event->irq);
179 return;
180 }
181
182 if (event->irq_is_wake)
183 enable_irq_wake(event->irq);
184
185 event->irq_requested = true;
186
187 /* Make sure we trigger the initial state of edge-triggered IRQs */
Hans de Goede61f7f7c2019-08-27 22:28:35 +0200188 if (run_edge_events_on_boot &&
189 (event->irqflags & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING))) {
190 value = gpiod_get_raw_value_cansleep(event->desc);
191 if (((event->irqflags & IRQF_TRIGGER_RISING) && value == 1) ||
192 ((event->irqflags & IRQF_TRIGGER_FALLING) && value == 0))
193 event->handler(event->irq, event);
194 }
Hans de Goedee59f5e02018-11-28 17:57:55 +0100195}
196
197static void acpi_gpiochip_request_irqs(struct acpi_gpio_chip *acpi_gpio)
198{
199 struct acpi_gpio_event *event;
200
201 list_for_each_entry(event, &acpi_gpio->events, node)
202 acpi_gpiochip_request_irq(acpi_gpio, event);
203}
204
Hans de Goeded4fc46f2019-11-14 11:26:00 +0100205/* Always returns AE_OK so that we keep looping over the resources */
Hans de Goedee59f5e02018-11-28 17:57:55 +0100206static acpi_status acpi_gpiochip_alloc_event(struct acpi_resource *ares,
207 void *context)
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200208{
209 struct acpi_gpio_chip *acpi_gpio = context;
210 struct gpio_chip *chip = acpi_gpio->chip;
211 struct acpi_resource_gpio *agpio;
212 acpi_handle handle, evt_handle;
213 struct acpi_gpio_event *event;
214 irq_handler_t handler = NULL;
215 struct gpio_desc *desc;
Hans de Goedee59f5e02018-11-28 17:57:55 +0100216 int ret, pin, irq;
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200217
Andy Shevchenko25e3ef82017-05-23 20:03:24 +0300218 if (!acpi_gpio_get_irq_resource(ares, &agpio))
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200219 return AE_OK;
220
Linus Walleij58383c782015-11-04 09:56:26 +0100221 handle = ACPI_HANDLE(chip->parent);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200222 pin = agpio->pin_table[0];
223
224 if (pin <= 255) {
225 char ev_name[5];
Arnd Bergmanne40a3ae2017-09-06 17:47:45 +0200226 sprintf(ev_name, "_%c%02hhX",
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200227 agpio->triggering == ACPI_EDGE_SENSITIVE ? 'E' : 'L',
228 pin);
229 if (ACPI_SUCCESS(acpi_get_handle(handle, ev_name, &evt_handle)))
230 handler = acpi_gpio_irq_handler;
231 }
232 if (!handler) {
233 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EVT", &evt_handle)))
234 handler = acpi_gpio_irq_handler_evt;
235 }
236 if (!handler)
Hans de Goedec06632e2017-06-23 09:26:13 +0200237 return AE_OK;
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200238
Linus Walleij5923ea62019-04-26 14:40:18 +0200239 desc = gpiochip_request_own_desc(chip, pin, "ACPI:Event",
240 GPIO_ACTIVE_HIGH, GPIOD_IN);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200241 if (IS_ERR(desc)) {
Hans de Goede3f86a7e2019-11-14 11:25:59 +0100242 dev_err(chip->parent,
243 "Failed to request GPIO for pin 0x%04X, err %ld\n",
244 pin, PTR_ERR(desc));
Hans de Goeded4fc46f2019-11-14 11:26:00 +0100245 return AE_OK;
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200246 }
247
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900248 ret = gpiochip_lock_as_irq(chip, pin);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200249 if (ret) {
Hans de Goede3f86a7e2019-11-14 11:25:59 +0100250 dev_err(chip->parent,
251 "Failed to lock GPIO pin 0x%04X as interrupt, err %d\n",
252 pin, ret);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200253 goto fail_free_desc;
254 }
255
256 irq = gpiod_to_irq(desc);
257 if (irq < 0) {
Hans de Goede3f86a7e2019-11-14 11:25:59 +0100258 dev_err(chip->parent,
259 "Failed to translate GPIO pin 0x%04X to IRQ, err %d\n",
260 pin, irq);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200261 goto fail_unlock_irq;
262 }
263
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200264 event = kzalloc(sizeof(*event), GFP_KERNEL);
265 if (!event)
266 goto fail_unlock_irq;
267
Hans de Goedee59f5e02018-11-28 17:57:55 +0100268 event->irqflags = IRQF_ONESHOT;
269 if (agpio->triggering == ACPI_LEVEL_SENSITIVE) {
270 if (agpio->polarity == ACPI_ACTIVE_HIGH)
271 event->irqflags |= IRQF_TRIGGER_HIGH;
272 else
273 event->irqflags |= IRQF_TRIGGER_LOW;
274 } else {
275 switch (agpio->polarity) {
276 case ACPI_ACTIVE_HIGH:
277 event->irqflags |= IRQF_TRIGGER_RISING;
278 break;
279 case ACPI_ACTIVE_LOW:
280 event->irqflags |= IRQF_TRIGGER_FALLING;
281 break;
282 default:
283 event->irqflags |= IRQF_TRIGGER_RISING |
284 IRQF_TRIGGER_FALLING;
285 break;
286 }
287 }
288
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200289 event->handle = evt_handle;
Hans de Goedee59f5e02018-11-28 17:57:55 +0100290 event->handler = handler;
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200291 event->irq = irq;
Hans de Goedeaa23ca32020-01-05 17:03:57 +0100292 event->irq_is_wake = honor_wakeup && agpio->wake_capable == ACPI_WAKE_CAPABLE;
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200293 event->pin = pin;
Alexandre Courbote46cf322014-08-19 10:06:08 -0700294 event->desc = desc;
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200295
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200296 list_add_tail(&event->node, &acpi_gpio->events);
Benjamin Tissoiresca876c72018-07-12 17:25:06 +0200297
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200298 return AE_OK;
299
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200300fail_unlock_irq:
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900301 gpiochip_unlock_as_irq(chip, pin);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200302fail_free_desc:
303 gpiochip_free_own_desc(desc);
304
Hans de Goeded4fc46f2019-11-14 11:26:00 +0100305 return AE_OK;
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200306}
307
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200308/**
309 * acpi_gpiochip_request_interrupts() - Register isr for gpio chip ACPI events
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300310 * @chip: GPIO chip
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200311 *
312 * ACPI5 platforms can use GPIO signaled ACPI events. These GPIO interrupts are
313 * handled by ACPI event methods which need to be called from the GPIO
Andy Shevchenko85edcd02019-03-30 00:33:45 +0300314 * chip's interrupt handler. acpi_gpiochip_request_interrupts() finds out which
315 * GPIO pins have ACPI event methods and assigns interrupt handlers that calls
316 * the ACPI event methods for those pins.
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200317 */
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300318void acpi_gpiochip_request_interrupts(struct gpio_chip *chip)
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200319{
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300320 struct acpi_gpio_chip *acpi_gpio;
321 acpi_handle handle;
322 acpi_status status;
Hans de Goede78d3a922018-08-14 16:07:03 +0200323 bool defer;
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200324
Linus Walleij58383c782015-11-04 09:56:26 +0100325 if (!chip->parent || !chip->to_irq)
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300326 return;
327
Linus Walleij58383c782015-11-04 09:56:26 +0100328 handle = ACPI_HANDLE(chip->parent);
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300329 if (!handle)
330 return;
331
332 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
333 if (ACPI_FAILURE(status))
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200334 return;
335
Hans de Goedee59f5e02018-11-28 17:57:55 +0100336 acpi_walk_resources(handle, "_AEI",
337 acpi_gpiochip_alloc_event, acpi_gpio);
338
Hans de Goede78d3a922018-08-14 16:07:03 +0200339 mutex_lock(&acpi_gpio_deferred_req_irqs_lock);
340 defer = !acpi_gpio_deferred_req_irqs_done;
341 if (defer)
342 list_add(&acpi_gpio->deferred_req_irqs_list_entry,
343 &acpi_gpio_deferred_req_irqs_list);
344 mutex_unlock(&acpi_gpio_deferred_req_irqs_lock);
345
346 if (defer)
347 return;
348
Hans de Goedee59f5e02018-11-28 17:57:55 +0100349 acpi_gpiochip_request_irqs(acpi_gpio);
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200350}
Hanjun Guo2b528ff2015-06-10 17:12:07 +0800351EXPORT_SYMBOL_GPL(acpi_gpiochip_request_interrupts);
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +0200352
Mika Westerberg70b534112013-10-10 11:01:07 +0300353/**
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200354 * acpi_gpiochip_free_interrupts() - Free GPIO ACPI event interrupts.
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300355 * @chip: GPIO chip
Mika Westerberg70b534112013-10-10 11:01:07 +0300356 *
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200357 * Free interrupts associated with GPIO ACPI event method for the given
358 * GPIO chip.
Mika Westerberg70b534112013-10-10 11:01:07 +0300359 */
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300360void acpi_gpiochip_free_interrupts(struct gpio_chip *chip)
Mika Westerberg70b534112013-10-10 11:01:07 +0300361{
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300362 struct acpi_gpio_chip *acpi_gpio;
Mika Westerberg4b01a142014-03-10 14:54:52 +0200363 struct acpi_gpio_event *event, *ep;
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300364 acpi_handle handle;
365 acpi_status status;
Mika Westerberg70b534112013-10-10 11:01:07 +0300366
Linus Walleij58383c782015-11-04 09:56:26 +0100367 if (!chip->parent || !chip->to_irq)
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300368 return;
369
Linus Walleij58383c782015-11-04 09:56:26 +0100370 handle = ACPI_HANDLE(chip->parent);
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300371 if (!handle)
372 return;
373
374 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
375 if (ACPI_FAILURE(status))
Mika Westerberg70b534112013-10-10 11:01:07 +0300376 return;
377
Hans de Goede78d3a922018-08-14 16:07:03 +0200378 mutex_lock(&acpi_gpio_deferred_req_irqs_lock);
379 if (!list_empty(&acpi_gpio->deferred_req_irqs_list_entry))
380 list_del_init(&acpi_gpio->deferred_req_irqs_list_entry);
381 mutex_unlock(&acpi_gpio_deferred_req_irqs_lock);
382
Mika Westerberg4b01a142014-03-10 14:54:52 +0200383 list_for_each_entry_safe_reverse(event, ep, &acpi_gpio->events, node) {
Hans de Goedee59f5e02018-11-28 17:57:55 +0100384 if (event->irq_requested) {
385 if (event->irq_is_wake)
386 disable_irq_wake(event->irq);
Hans de Goede8a146fb2017-03-24 11:08:47 +0100387
Hans de Goedee59f5e02018-11-28 17:57:55 +0100388 free_irq(event->irq, event);
389 }
390
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900391 gpiochip_unlock_as_irq(chip, event->pin);
Hans de Goede86252322018-11-28 17:57:56 +0100392 gpiochip_free_own_desc(event->desc);
Mika Westerberg4b01a142014-03-10 14:54:52 +0200393 list_del(&event->node);
394 kfree(event);
Mika Westerberg70b534112013-10-10 11:01:07 +0300395 }
Mika Westerberg70b534112013-10-10 11:01:07 +0300396}
Hanjun Guo2b528ff2015-06-10 17:12:07 +0800397EXPORT_SYMBOL_GPL(acpi_gpiochip_free_interrupts);
Mika Westerberg70b534112013-10-10 11:01:07 +0300398
Rafael J. Wysockif028d522014-11-03 23:39:41 +0100399int acpi_dev_add_driver_gpios(struct acpi_device *adev,
400 const struct acpi_gpio_mapping *gpios)
401{
402 if (adev && gpios) {
403 adev->driver_gpios = gpios;
404 return 0;
405 }
406 return -EINVAL;
407}
408EXPORT_SYMBOL_GPL(acpi_dev_add_driver_gpios);
409
Andy Shevchenko2838bf92019-07-30 13:43:37 +0300410void acpi_dev_remove_driver_gpios(struct acpi_device *adev)
411{
412 if (adev)
413 adev->driver_gpios = NULL;
414}
415EXPORT_SYMBOL_GPL(acpi_dev_remove_driver_gpios);
416
Andy Shevchenko85c73d52017-03-02 15:48:05 +0200417static void devm_acpi_dev_release_driver_gpios(struct device *dev, void *res)
418{
419 acpi_dev_remove_driver_gpios(ACPI_COMPANION(dev));
420}
421
422int devm_acpi_dev_add_driver_gpios(struct device *dev,
423 const struct acpi_gpio_mapping *gpios)
424{
425 void *res;
426 int ret;
427
428 res = devres_alloc(devm_acpi_dev_release_driver_gpios, 0, GFP_KERNEL);
429 if (!res)
430 return -ENOMEM;
431
432 ret = acpi_dev_add_driver_gpios(ACPI_COMPANION(dev), gpios);
433 if (ret) {
434 devres_free(res);
435 return ret;
436 }
437 devres_add(dev, res);
438 return 0;
439}
440EXPORT_SYMBOL_GPL(devm_acpi_dev_add_driver_gpios);
441
442void devm_acpi_dev_remove_driver_gpios(struct device *dev)
443{
444 WARN_ON(devres_release(dev, devm_acpi_dev_release_driver_gpios, NULL, NULL));
445}
446EXPORT_SYMBOL_GPL(devm_acpi_dev_remove_driver_gpios);
447
Rafael J. Wysockif028d522014-11-03 23:39:41 +0100448static bool acpi_get_driver_gpio_data(struct acpi_device *adev,
449 const char *name, int index,
Sakari Ailus977d5ad2018-07-17 17:19:11 +0300450 struct fwnode_reference_args *args,
Andy Shevchenkoce0929d2017-11-10 15:40:32 +0200451 unsigned int *quirks)
Rafael J. Wysockif028d522014-11-03 23:39:41 +0100452{
453 const struct acpi_gpio_mapping *gm;
454
455 if (!adev->driver_gpios)
456 return false;
457
458 for (gm = adev->driver_gpios; gm->name; gm++)
459 if (!strcmp(name, gm->name) && gm->data && index < gm->size) {
460 const struct acpi_gpio_params *par = gm->data + index;
461
Sakari Ailus977d5ad2018-07-17 17:19:11 +0300462 args->fwnode = acpi_fwnode_handle(adev);
Rafael J. Wysockif028d522014-11-03 23:39:41 +0100463 args->args[0] = par->crs_entry_index;
464 args->args[1] = par->line_index;
465 args->args[2] = par->active_low;
466 args->nargs = 3;
Andy Shevchenkoce0929d2017-11-10 15:40:32 +0200467
468 *quirks = gm->quirks;
Rafael J. Wysockif028d522014-11-03 23:39:41 +0100469 return true;
470 }
471
472 return false;
473}
474
Andy Shevchenko2eca25a2017-05-23 20:03:22 +0300475static enum gpiod_flags
476acpi_gpio_to_gpiod_flags(const struct acpi_resource_gpio *agpio)
477{
Andy Shevchenko2eca25a2017-05-23 20:03:22 +0300478 switch (agpio->io_restriction) {
479 case ACPI_IO_RESTRICT_INPUT:
480 return GPIOD_IN;
481 case ACPI_IO_RESTRICT_OUTPUT:
482 /*
483 * ACPI GPIO resources don't contain an initial value for the
484 * GPIO. Therefore we deduce that value from the pull field
485 * instead. If the pin is pulled up we assume default to be
Andy Shevchenko24a49542019-04-10 18:39:19 +0300486 * high, if it is pulled down we assume default to be low,
487 * otherwise we leave pin untouched.
Andy Shevchenko2eca25a2017-05-23 20:03:22 +0300488 */
Andy Shevchenko24a49542019-04-10 18:39:19 +0300489 switch (agpio->pin_config) {
490 case ACPI_PIN_CONFIG_PULLUP:
491 return GPIOD_OUT_HIGH;
492 case ACPI_PIN_CONFIG_PULLDOWN:
493 return GPIOD_OUT_LOW;
494 default:
495 break;
496 }
Andy Shevchenko2eca25a2017-05-23 20:03:22 +0300497 default:
Andy Shevchenko24a49542019-04-10 18:39:19 +0300498 break;
Andy Shevchenko2eca25a2017-05-23 20:03:22 +0300499 }
Andy Shevchenko24a49542019-04-10 18:39:19 +0300500
501 /*
502 * Assume that the BIOS has configured the direction and pull
503 * accordingly.
504 */
505 return GPIOD_ASIS;
Andy Shevchenko2eca25a2017-05-23 20:03:22 +0300506}
507
Andy Shevchenko5c34b6c2017-11-10 15:40:31 +0200508static int
509__acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, enum gpiod_flags update)
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300510{
Hans de Goede72893f02018-12-31 21:55:21 +0100511 const enum gpiod_flags mask =
512 GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT |
513 GPIOD_FLAGS_BIT_DIR_VAL;
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300514 int ret = 0;
515
516 /*
517 * Check if the BIOS has IoRestriction with explicitly set direction
518 * and update @flags accordingly. Otherwise use whatever caller asked
519 * for.
520 */
521 if (update & GPIOD_FLAGS_BIT_DIR_SET) {
522 enum gpiod_flags diff = *flags ^ update;
523
524 /*
525 * Check if caller supplied incompatible GPIO initialization
526 * flags.
527 *
528 * Return %-EINVAL to notify that firmware has different
529 * settings and we are going to use them.
530 */
531 if (((*flags & GPIOD_FLAGS_BIT_DIR_SET) && (diff & GPIOD_FLAGS_BIT_DIR_OUT)) ||
532 ((*flags & GPIOD_FLAGS_BIT_DIR_OUT) && (diff & GPIOD_FLAGS_BIT_DIR_VAL)))
533 ret = -EINVAL;
Hans de Goede72893f02018-12-31 21:55:21 +0100534 *flags = (*flags & ~mask) | (update & mask);
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300535 }
536 return ret;
537}
538
Andy Shevchenko5c34b6c2017-11-10 15:40:31 +0200539int
540acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, struct acpi_gpio_info *info)
541{
542 struct device *dev = &info->adev->dev;
Andy Shevchenko1b2ca322017-11-10 15:40:33 +0200543 enum gpiod_flags old = *flags;
Andy Shevchenko5c34b6c2017-11-10 15:40:31 +0200544 int ret;
545
Andy Shevchenko1b2ca322017-11-10 15:40:33 +0200546 ret = __acpi_gpio_update_gpiod_flags(&old, info->flags);
547 if (info->quirks & ACPI_GPIO_QUIRK_NO_IO_RESTRICTION) {
548 if (ret)
549 dev_warn(dev, FW_BUG "GPIO not in correct mode, fixing\n");
550 } else {
551 if (ret)
552 dev_dbg(dev, "Override GPIO initialization flags\n");
553 *flags = old;
554 }
Andy Shevchenko5c34b6c2017-11-10 15:40:31 +0200555
556 return ret;
557}
558
Andy Shevchenko606be342019-04-10 18:39:20 +0300559int acpi_gpio_update_gpiod_lookup_flags(unsigned long *lookupflags,
560 struct acpi_gpio_info *info)
561{
Andy Shevchenko2d3b6db2019-04-10 18:39:21 +0300562 switch (info->pin_config) {
563 case ACPI_PIN_CONFIG_PULLUP:
564 *lookupflags |= GPIO_PULL_UP;
565 break;
566 case ACPI_PIN_CONFIG_PULLDOWN:
567 *lookupflags |= GPIO_PULL_DOWN;
568 break;
569 default:
570 break;
571 }
572
Andy Shevchenko606be342019-04-10 18:39:20 +0300573 if (info->polarity == GPIO_ACTIVE_LOW)
574 *lookupflags |= GPIO_ACTIVE_LOW;
575
576 return 0;
577}
578
Mika Westerberg12028d22013-04-03 13:56:54 +0300579struct acpi_gpio_lookup {
580 struct acpi_gpio_info info;
581 int index;
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100582 int pin_index;
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200583 bool active_low;
Mika Westerberg936e15d2013-10-10 11:01:08 +0300584 struct gpio_desc *desc;
Mika Westerberg12028d22013-04-03 13:56:54 +0300585 int n;
586};
587
Linus Walleij031ba282016-10-03 10:40:03 +0200588static int acpi_populate_gpio_lookup(struct acpi_resource *ares, void *data)
Mika Westerberg12028d22013-04-03 13:56:54 +0300589{
590 struct acpi_gpio_lookup *lookup = data;
591
592 if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
593 return 1;
594
Andy Shevchenko4d1f7a62019-02-06 22:49:46 +0200595 if (!lookup->desc) {
Mika Westerberg12028d22013-04-03 13:56:54 +0300596 const struct acpi_resource_gpio *agpio = &ares->data.gpio;
Andy Shevchenko4d1f7a62019-02-06 22:49:46 +0200597 bool gpioint = agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
598 int pin_index;
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100599
Andy Shevchenko4d1f7a62019-02-06 22:49:46 +0200600 if (lookup->info.quirks & ACPI_GPIO_QUIRK_ONLY_GPIOIO && gpioint)
601 lookup->index++;
602
603 if (lookup->n++ != lookup->index)
604 return 1;
605
606 pin_index = lookup->pin_index;
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100607 if (pin_index >= agpio->pin_table_length)
608 return 1;
Mika Westerberg12028d22013-04-03 13:56:54 +0300609
Mika Westerberg936e15d2013-10-10 11:01:08 +0300610 lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr,
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100611 agpio->pin_table[pin_index]);
Andy Shevchenko2d3b6db2019-04-10 18:39:21 +0300612 lookup->info.pin_config = agpio->pin_config;
Andy Shevchenko4d1f7a62019-02-06 22:49:46 +0200613 lookup->info.gpioint = gpioint;
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100614
615 /*
Andy Shevchenkoe567c352017-01-03 19:01:18 +0200616 * Polarity and triggering are only specified for GpioInt
617 * resource.
Christophe RICARD52044722015-12-23 23:25:34 +0100618 * Note: we expect here:
619 * - ACPI_ACTIVE_LOW == GPIO_ACTIVE_LOW
620 * - ACPI_ACTIVE_HIGH == GPIO_ACTIVE_HIGH
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100621 */
Christophe RICARD52044722015-12-23 23:25:34 +0100622 if (lookup->info.gpioint) {
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300623 lookup->info.flags = GPIOD_IN;
Christophe RICARD52044722015-12-23 23:25:34 +0100624 lookup->info.polarity = agpio->polarity;
625 lookup->info.triggering = agpio->triggering;
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300626 } else {
627 lookup->info.flags = acpi_gpio_to_gpiod_flags(agpio);
Andy Shevchenkof67a6c12017-11-10 15:40:28 +0200628 lookup->info.polarity = lookup->active_low;
Christophe RICARD52044722015-12-23 23:25:34 +0100629 }
Mika Westerberg12028d22013-04-03 13:56:54 +0300630 }
631
632 return 1;
633}
634
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200635static int acpi_gpio_resource_lookup(struct acpi_gpio_lookup *lookup,
636 struct acpi_gpio_info *info)
637{
Andy Shevchenko5870cff472017-11-10 15:40:30 +0200638 struct acpi_device *adev = lookup->info.adev;
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200639 struct list_head res_list;
640 int ret;
641
642 INIT_LIST_HEAD(&res_list);
643
Andy Shevchenko5870cff472017-11-10 15:40:30 +0200644 ret = acpi_dev_get_resources(adev, &res_list,
Linus Walleij031ba282016-10-03 10:40:03 +0200645 acpi_populate_gpio_lookup,
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200646 lookup);
647 if (ret < 0)
648 return ret;
649
650 acpi_dev_free_resource_list(&res_list);
651
652 if (!lookup->desc)
653 return -ENOENT;
654
Andy Shevchenkof67a6c12017-11-10 15:40:28 +0200655 if (info)
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200656 *info = lookup->info;
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200657 return 0;
658}
659
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200660static int acpi_gpio_property_lookup(struct fwnode_handle *fwnode,
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200661 const char *propname, int index,
662 struct acpi_gpio_lookup *lookup)
663{
Sakari Ailus977d5ad2018-07-17 17:19:11 +0300664 struct fwnode_reference_args args;
Andy Shevchenkoce0929d2017-11-10 15:40:32 +0200665 unsigned int quirks = 0;
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200666 int ret;
667
668 memset(&args, 0, sizeof(args));
Mika Westerberg6f7194a2016-10-21 17:21:29 +0300669 ret = __acpi_node_get_property_reference(fwnode, propname, index, 3,
670 &args);
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200671 if (ret) {
672 struct acpi_device *adev = to_acpi_device_node(fwnode);
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200673
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200674 if (!adev)
675 return ret;
676
Andy Shevchenkoce0929d2017-11-10 15:40:32 +0200677 if (!acpi_get_driver_gpio_data(adev, propname, index, &args,
678 &quirks))
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200679 return ret;
680 }
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200681 /*
682 * The property was found and resolved, so need to lookup the GPIO based
683 * on returned args.
684 */
Sakari Ailus977d5ad2018-07-17 17:19:11 +0300685 if (!to_acpi_device_node(args.fwnode))
686 return -EINVAL;
Mika Westerberg6f7194a2016-10-21 17:21:29 +0300687 if (args.nargs != 3)
688 return -EPROTO;
689
690 lookup->index = args.args[0];
691 lookup->pin_index = args.args[1];
692 lookup->active_low = !!args.args[2];
693
Sakari Ailus977d5ad2018-07-17 17:19:11 +0300694 lookup->info.adev = to_acpi_device_node(args.fwnode);
Andy Shevchenkoce0929d2017-11-10 15:40:32 +0200695 lookup->info.quirks = quirks;
Sakari Ailus977d5ad2018-07-17 17:19:11 +0300696
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200697 return 0;
698}
699
Mika Westerberg12028d22013-04-03 13:56:54 +0300700/**
Mika Westerberg936e15d2013-10-10 11:01:08 +0300701 * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100702 * @adev: pointer to a ACPI device to get GPIO from
703 * @propname: Property name of the GPIO (optional)
Mika Westerberg12028d22013-04-03 13:56:54 +0300704 * @index: index of GpioIo/GpioInt resource (starting from %0)
705 * @info: info pointer to fill in (optional)
706 *
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100707 * Function goes through ACPI resources for @adev and based on @index looks
Mika Westerberg936e15d2013-10-10 11:01:08 +0300708 * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor,
Mika Westerberg12028d22013-04-03 13:56:54 +0300709 * and returns it. @index matches GpioIo/GpioInt resources only so if there
710 * are total %3 GPIO resources, the index goes from %0 to %2.
711 *
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100712 * If @propname is specified the GPIO is looked using device property. In
713 * that case @index is used to select the GPIO entry in the property value
714 * (in case of multiple).
715 *
Andy Shevchenko85edcd02019-03-30 00:33:45 +0300716 * If the GPIO cannot be translated or there is an error, an ERR_PTR is
Mika Westerberg12028d22013-04-03 13:56:54 +0300717 * returned.
718 *
719 * Note: if the GPIO resource has multiple entries in the pin list, this
720 * function only returns the first.
721 */
Linus Walleij031ba282016-10-03 10:40:03 +0200722static struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100723 const char *propname, int index,
Mika Westerberg936e15d2013-10-10 11:01:08 +0300724 struct acpi_gpio_info *info)
Mika Westerberg12028d22013-04-03 13:56:54 +0300725{
726 struct acpi_gpio_lookup lookup;
Mika Westerberg12028d22013-04-03 13:56:54 +0300727 int ret;
728
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100729 if (!adev)
Mika Westerberg936e15d2013-10-10 11:01:08 +0300730 return ERR_PTR(-ENODEV);
Mika Westerberg12028d22013-04-03 13:56:54 +0300731
732 memset(&lookup, 0, sizeof(lookup));
733 lookup.index = index;
Mika Westerberg12028d22013-04-03 13:56:54 +0300734
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100735 if (propname) {
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100736 dev_dbg(&adev->dev, "GPIO: looking up %s\n", propname);
737
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200738 ret = acpi_gpio_property_lookup(acpi_fwnode_handle(adev),
739 propname, index, &lookup);
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200740 if (ret)
741 return ERR_PTR(ret);
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100742
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200743 dev_dbg(&adev->dev, "GPIO: _DSD returned %s %d %d %u\n",
Andy Shevchenko5870cff472017-11-10 15:40:30 +0200744 dev_name(&lookup.info.adev->dev), lookup.index,
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200745 lookup.pin_index, lookup.active_low);
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100746 } else {
747 dev_dbg(&adev->dev, "GPIO: looking up %d in _CRS\n", index);
Andy Shevchenko5870cff472017-11-10 15:40:30 +0200748 lookup.info.adev = adev;
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100749 }
750
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200751 ret = acpi_gpio_resource_lookup(&lookup, info);
752 return ret ? ERR_PTR(ret) : lookup.desc;
Mika Westerberg12028d22013-04-03 13:56:54 +0300753}
Mika Westerberg664e3e52014-01-08 12:40:54 +0200754
Dmitry Torokhov4f78d912019-09-04 10:26:24 -0700755static bool acpi_can_fallback_to_crs(struct acpi_device *adev,
756 const char *con_id)
757{
758 /* Never allow fallback if the device has properties */
759 if (acpi_dev_has_props(adev) || adev->driver_gpios)
760 return false;
761
762 return con_id == NULL;
763}
764
Linus Walleij031ba282016-10-03 10:40:03 +0200765struct gpio_desc *acpi_find_gpio(struct device *dev,
766 const char *con_id,
767 unsigned int idx,
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300768 enum gpiod_flags *dflags,
Andy Shevchenkofed70262019-04-10 18:39:16 +0300769 unsigned long *lookupflags)
Linus Walleij031ba282016-10-03 10:40:03 +0200770{
771 struct acpi_device *adev = ACPI_COMPANION(dev);
772 struct acpi_gpio_info info;
773 struct gpio_desc *desc;
774 char propname[32];
775 int i;
776
777 /* Try first from _DSD */
778 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Andy Shevchenko9e665042017-05-23 20:03:17 +0300779 if (con_id) {
Linus Walleij031ba282016-10-03 10:40:03 +0200780 snprintf(propname, sizeof(propname), "%s-%s",
781 con_id, gpio_suffixes[i]);
782 } else {
783 snprintf(propname, sizeof(propname), "%s",
784 gpio_suffixes[i]);
785 }
786
787 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
Dmitry Torokhov693bdaa2017-03-23 13:21:38 -0700788 if (!IS_ERR(desc))
Linus Walleij031ba282016-10-03 10:40:03 +0200789 break;
Dmitry Torokhov693bdaa2017-03-23 13:21:38 -0700790 if (PTR_ERR(desc) == -EPROBE_DEFER)
791 return ERR_CAST(desc);
Linus Walleij031ba282016-10-03 10:40:03 +0200792 }
793
794 /* Then from plain _CRS GPIOs */
795 if (IS_ERR(desc)) {
796 if (!acpi_can_fallback_to_crs(adev, con_id))
797 return ERR_PTR(-ENOENT);
798
799 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
800 if (IS_ERR(desc))
801 return desc;
Andy Shevchenkofe06b562017-05-23 20:03:18 +0300802 }
Linus Walleij031ba282016-10-03 10:40:03 +0200803
Andy Shevchenkofe06b562017-05-23 20:03:18 +0300804 if (info.gpioint &&
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300805 (*dflags == GPIOD_OUT_LOW || *dflags == GPIOD_OUT_HIGH)) {
Andy Shevchenkofe06b562017-05-23 20:03:18 +0300806 dev_dbg(dev, "refusing GpioInt() entry when doing GPIOD_OUT_* lookup\n");
807 return ERR_PTR(-ENOENT);
Linus Walleij031ba282016-10-03 10:40:03 +0200808 }
809
Andy Shevchenko5c34b6c2017-11-10 15:40:31 +0200810 acpi_gpio_update_gpiod_flags(dflags, &info);
Andy Shevchenko606be342019-04-10 18:39:20 +0300811 acpi_gpio_update_gpiod_lookup_flags(lookupflags, &info);
Linus Walleij031ba282016-10-03 10:40:03 +0200812 return desc;
813}
814
Mika Westerbergc884fbd2015-05-06 13:29:06 +0300815/**
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200816 * acpi_node_get_gpiod() - get a GPIO descriptor from ACPI resources
817 * @fwnode: pointer to an ACPI firmware node to get the GPIO information from
818 * @propname: Property name of the GPIO
819 * @index: index of GpioIo/GpioInt resource (starting from %0)
820 * @info: info pointer to fill in (optional)
821 *
Andy Shevchenko85edcd02019-03-30 00:33:45 +0300822 * If @fwnode is an ACPI device object, call acpi_get_gpiod_by_index() for it.
823 * Otherwise (i.e. it is a data-only non-device object), use the property-based
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200824 * GPIO lookup to get to the GPIO resource with the relevant information and use
825 * that to obtain the GPIO descriptor to return.
Andy Shevchenko85edcd02019-03-30 00:33:45 +0300826 *
827 * If the GPIO cannot be translated or there is an error an ERR_PTR is
828 * returned.
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200829 */
830struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode,
831 const char *propname, int index,
832 struct acpi_gpio_info *info)
833{
834 struct acpi_gpio_lookup lookup;
835 struct acpi_device *adev;
836 int ret;
837
838 adev = to_acpi_device_node(fwnode);
839 if (adev)
840 return acpi_get_gpiod_by_index(adev, propname, index, info);
841
842 if (!is_acpi_data_node(fwnode))
843 return ERR_PTR(-ENODEV);
844
845 if (!propname)
846 return ERR_PTR(-EINVAL);
847
848 memset(&lookup, 0, sizeof(lookup));
849 lookup.index = index;
850
851 ret = acpi_gpio_property_lookup(fwnode, propname, index, &lookup);
852 if (ret)
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200853 return ERR_PTR(ret);
854
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200855 ret = acpi_gpio_resource_lookup(&lookup, info);
856 return ret ? ERR_PTR(ret) : lookup.desc;
Mika Westerberg664e3e52014-01-08 12:40:54 +0200857}
858
Mika Westerbergc884fbd2015-05-06 13:29:06 +0300859/**
860 * acpi_dev_gpio_irq_get() - Find GpioInt and translate it to Linux IRQ number
861 * @adev: pointer to a ACPI device to get IRQ from
862 * @index: index of GpioInt resource (starting from %0)
863 *
864 * If the device has one or more GpioInt resources, this function can be
865 * used to translate from the GPIO offset in the resource to the Linux IRQ
866 * number.
867 *
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300868 * The function is idempotent, though each time it runs it will configure GPIO
869 * pin direction according to the flags in GpioInt resource.
870 *
Thierry Redingdb05c7e2017-07-24 16:57:25 +0200871 * Return: Linux IRQ number (> %0) on success, negative errno on failure.
Mika Westerbergc884fbd2015-05-06 13:29:06 +0300872 */
873int acpi_dev_gpio_irq_get(struct acpi_device *adev, int index)
874{
875 int idx, i;
Christophe RICARD52044722015-12-23 23:25:34 +0100876 unsigned int irq_flags;
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300877 int ret;
Mika Westerbergc884fbd2015-05-06 13:29:06 +0300878
879 for (i = 0, idx = 0; idx <= index; i++) {
880 struct acpi_gpio_info info;
881 struct gpio_desc *desc;
882
883 desc = acpi_get_gpiod_by_index(adev, NULL, i, &info);
Christophe RICARD52044722015-12-23 23:25:34 +0100884
Hans de Goede6798d722017-03-13 23:04:21 +0100885 /* Ignore -EPROBE_DEFER, it only matters if idx matches */
886 if (IS_ERR(desc) && PTR_ERR(desc) != -EPROBE_DEFER)
887 return PTR_ERR(desc);
888
889 if (info.gpioint && idx++ == index) {
Andy Shevchenko2d6c06f2019-04-10 18:39:17 +0300890 unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300891 char label[32];
Hans de Goede6798d722017-03-13 23:04:21 +0100892 int irq;
893
894 if (IS_ERR(desc))
895 return PTR_ERR(desc);
896
897 irq = gpiod_to_irq(desc);
Christophe RICARD52044722015-12-23 23:25:34 +0100898 if (irq < 0)
899 return irq;
900
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300901 snprintf(label, sizeof(label), "GpioInt() %d", index);
Andy Shevchenko2d6c06f2019-04-10 18:39:17 +0300902 ret = gpiod_configure_flags(desc, label, lflags, info.flags);
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300903 if (ret < 0)
904 return ret;
905
Christophe RICARD52044722015-12-23 23:25:34 +0100906 irq_flags = acpi_dev_get_irq_type(info.triggering,
907 info.polarity);
908
909 /* Set type if specified and different than the current one */
910 if (irq_flags != IRQ_TYPE_NONE &&
911 irq_flags != irq_get_trigger_type(irq))
912 irq_set_irq_type(irq, irq_flags);
913
914 return irq;
915 }
916
Mika Westerbergc884fbd2015-05-06 13:29:06 +0300917 }
Hans de Goede6798d722017-03-13 23:04:21 +0100918 return -ENOENT;
Mika Westerbergc884fbd2015-05-06 13:29:06 +0300919}
920EXPORT_SYMBOL_GPL(acpi_dev_gpio_irq_get);
921
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200922static acpi_status
923acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
924 u32 bits, u64 *value, void *handler_context,
925 void *region_context)
926{
927 struct acpi_gpio_chip *achip = region_context;
928 struct gpio_chip *chip = achip->chip;
929 struct acpi_resource_gpio *agpio;
930 struct acpi_resource *ares;
931 int pin_index = (int)address;
932 acpi_status status;
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200933 int length;
934 int i;
935
936 status = acpi_buffer_to_resource(achip->conn_info.connection,
937 achip->conn_info.length, &ares);
938 if (ACPI_FAILURE(status))
939 return status;
940
941 if (WARN_ON(ares->type != ACPI_RESOURCE_TYPE_GPIO)) {
942 ACPI_FREE(ares);
943 return AE_BAD_PARAMETER;
944 }
945
946 agpio = &ares->data.gpio;
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200947
948 if (WARN_ON(agpio->io_restriction == ACPI_IO_RESTRICT_INPUT &&
949 function == ACPI_WRITE)) {
950 ACPI_FREE(ares);
951 return AE_BAD_PARAMETER;
952 }
953
954 length = min(agpio->pin_table_length, (u16)(pin_index + bits));
955 for (i = pin_index; i < length; ++i) {
Qipeng Zhaa4811622015-04-10 06:25:10 +0800956 int pin = agpio->pin_table[i];
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200957 struct acpi_gpio_connection *conn;
958 struct gpio_desc *desc;
959 bool found;
960
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200961 mutex_lock(&achip->conn_lock);
962
963 found = false;
964 list_for_each_entry(conn, &achip->conns, node) {
Alexandre Courbote46cf322014-08-19 10:06:08 -0700965 if (conn->pin == pin) {
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200966 found = true;
Alexandre Courbote46cf322014-08-19 10:06:08 -0700967 desc = conn->desc;
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200968 break;
969 }
970 }
Mika Westerbergc103a102015-10-30 12:02:05 +0200971
972 /*
973 * The same GPIO can be shared between operation region and
974 * event but only if the access here is ACPI_READ. In that
975 * case we "borrow" the event GPIO instead.
976 */
Erik Schmaussc163f90c2019-02-15 13:36:19 -0800977 if (!found && agpio->shareable == ACPI_SHARED &&
Mika Westerbergc103a102015-10-30 12:02:05 +0200978 function == ACPI_READ) {
979 struct acpi_gpio_event *event;
980
981 list_for_each_entry(event, &achip->events, node) {
982 if (event->pin == pin) {
983 desc = event->desc;
984 found = true;
985 break;
986 }
987 }
988 }
989
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200990 if (!found) {
Andy Shevchenko2eca25a2017-05-23 20:03:22 +0300991 enum gpiod_flags flags = acpi_gpio_to_gpiod_flags(agpio);
992 const char *label = "ACPI:OpRegion";
Andy Shevchenko2eca25a2017-05-23 20:03:22 +0300993
Linus Walleij21abf102018-09-04 13:31:45 +0200994 desc = gpiochip_request_own_desc(chip, pin, label,
Linus Walleij5923ea62019-04-26 14:40:18 +0200995 GPIO_ACTIVE_HIGH,
Linus Walleij21abf102018-09-04 13:31:45 +0200996 flags);
Alexandre Courbote46cf322014-08-19 10:06:08 -0700997 if (IS_ERR(desc)) {
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200998 status = AE_ERROR;
999 mutex_unlock(&achip->conn_lock);
1000 goto out;
1001 }
1002
Mika Westerberg473ed7b2014-03-14 17:58:07 +02001003 conn = kzalloc(sizeof(*conn), GFP_KERNEL);
1004 if (!conn) {
1005 status = AE_NO_MEMORY;
1006 gpiochip_free_own_desc(desc);
1007 mutex_unlock(&achip->conn_lock);
1008 goto out;
1009 }
1010
Alexandre Courbote46cf322014-08-19 10:06:08 -07001011 conn->pin = pin;
Mika Westerberg473ed7b2014-03-14 17:58:07 +02001012 conn->desc = desc;
1013 list_add_tail(&conn->node, &achip->conns);
1014 }
1015
1016 mutex_unlock(&achip->conn_lock);
1017
1018 if (function == ACPI_WRITE)
Aaron Ludc62b562014-05-20 17:07:38 +08001019 gpiod_set_raw_value_cansleep(desc,
1020 !!((1 << i) & *value));
Mika Westerberg473ed7b2014-03-14 17:58:07 +02001021 else
Aaron Ludc62b562014-05-20 17:07:38 +08001022 *value |= (u64)gpiod_get_raw_value_cansleep(desc) << i;
Mika Westerberg473ed7b2014-03-14 17:58:07 +02001023 }
1024
1025out:
1026 ACPI_FREE(ares);
1027 return status;
1028}
1029
1030static void acpi_gpiochip_request_regions(struct acpi_gpio_chip *achip)
1031{
1032 struct gpio_chip *chip = achip->chip;
Linus Walleij58383c782015-11-04 09:56:26 +01001033 acpi_handle handle = ACPI_HANDLE(chip->parent);
Mika Westerberg473ed7b2014-03-14 17:58:07 +02001034 acpi_status status;
1035
1036 INIT_LIST_HEAD(&achip->conns);
1037 mutex_init(&achip->conn_lock);
1038 status = acpi_install_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
1039 acpi_gpio_adr_space_handler,
1040 NULL, achip);
1041 if (ACPI_FAILURE(status))
Linus Walleij58383c782015-11-04 09:56:26 +01001042 dev_err(chip->parent,
1043 "Failed to install GPIO OpRegion handler\n");
Mika Westerberg473ed7b2014-03-14 17:58:07 +02001044}
1045
1046static void acpi_gpiochip_free_regions(struct acpi_gpio_chip *achip)
1047{
1048 struct gpio_chip *chip = achip->chip;
Linus Walleij58383c782015-11-04 09:56:26 +01001049 acpi_handle handle = ACPI_HANDLE(chip->parent);
Mika Westerberg473ed7b2014-03-14 17:58:07 +02001050 struct acpi_gpio_connection *conn, *tmp;
1051 acpi_status status;
1052
1053 status = acpi_remove_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
1054 acpi_gpio_adr_space_handler);
1055 if (ACPI_FAILURE(status)) {
Linus Walleij58383c782015-11-04 09:56:26 +01001056 dev_err(chip->parent,
1057 "Failed to remove GPIO OpRegion handler\n");
Mika Westerberg473ed7b2014-03-14 17:58:07 +02001058 return;
1059 }
1060
1061 list_for_each_entry_safe_reverse(conn, tmp, &achip->conns, node) {
1062 gpiochip_free_own_desc(conn->desc);
1063 list_del(&conn->node);
1064 kfree(conn);
1065 }
1066}
1067
Andy Shevchenkofed70262019-04-10 18:39:16 +03001068static struct gpio_desc *
1069acpi_gpiochip_parse_own_gpio(struct acpi_gpio_chip *achip,
1070 struct fwnode_handle *fwnode,
1071 const char **name,
1072 unsigned long *lflags,
Andy Shevchenko80c8d922019-04-10 18:39:18 +03001073 enum gpiod_flags *dflags)
Mika Westerbergc80f1ba2016-10-21 17:21:30 +03001074{
1075 struct gpio_chip *chip = achip->chip;
1076 struct gpio_desc *desc;
1077 u32 gpios[2];
1078 int ret;
1079
Andy Shevchenko2d6c06f2019-04-10 18:39:17 +03001080 *lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
Arnd Bergmannc82064f2016-11-08 14:40:06 +01001081 *dflags = 0;
1082 *name = NULL;
1083
Mika Westerbergc80f1ba2016-10-21 17:21:30 +03001084 ret = fwnode_property_read_u32_array(fwnode, "gpios", gpios,
1085 ARRAY_SIZE(gpios));
1086 if (ret < 0)
1087 return ERR_PTR(ret);
1088
Mika Westerberg03c47492017-11-27 16:54:42 +03001089 desc = gpiochip_get_desc(chip, gpios[0]);
Mika Westerbergc80f1ba2016-10-21 17:21:30 +03001090 if (IS_ERR(desc))
1091 return desc;
1092
Mika Westerbergc80f1ba2016-10-21 17:21:30 +03001093 if (gpios[1])
1094 *lflags |= GPIO_ACTIVE_LOW;
1095
1096 if (fwnode_property_present(fwnode, "input"))
1097 *dflags |= GPIOD_IN;
1098 else if (fwnode_property_present(fwnode, "output-low"))
1099 *dflags |= GPIOD_OUT_LOW;
1100 else if (fwnode_property_present(fwnode, "output-high"))
1101 *dflags |= GPIOD_OUT_HIGH;
1102 else
1103 return ERR_PTR(-EINVAL);
1104
1105 fwnode_property_read_string(fwnode, "line-name", name);
1106
1107 return desc;
1108}
1109
1110static void acpi_gpiochip_scan_gpios(struct acpi_gpio_chip *achip)
1111{
1112 struct gpio_chip *chip = achip->chip;
1113 struct fwnode_handle *fwnode;
1114
1115 device_for_each_child_node(chip->parent, fwnode) {
Andy Shevchenkofed70262019-04-10 18:39:16 +03001116 unsigned long lflags;
Andy Shevchenko80c8d922019-04-10 18:39:18 +03001117 enum gpiod_flags dflags;
Mika Westerbergc80f1ba2016-10-21 17:21:30 +03001118 struct gpio_desc *desc;
1119 const char *name;
1120 int ret;
1121
1122 if (!fwnode_property_present(fwnode, "gpio-hog"))
1123 continue;
1124
1125 desc = acpi_gpiochip_parse_own_gpio(achip, fwnode, &name,
1126 &lflags, &dflags);
1127 if (IS_ERR(desc))
1128 continue;
1129
1130 ret = gpiod_hog(desc, name, lflags, dflags);
1131 if (ret) {
1132 dev_err(chip->parent, "Failed to hog GPIO\n");
Wei Yongjun1b6998c2016-10-29 16:11:57 +00001133 fwnode_handle_put(fwnode);
Mika Westerbergc80f1ba2016-10-21 17:21:30 +03001134 return;
1135 }
1136 }
1137}
1138
Mika Westerberg664e3e52014-01-08 12:40:54 +02001139void acpi_gpiochip_add(struct gpio_chip *chip)
1140{
Mika Westerbergaa92b6f2014-03-10 14:54:51 +02001141 struct acpi_gpio_chip *acpi_gpio;
1142 acpi_handle handle;
1143 acpi_status status;
1144
Linus Walleij58383c782015-11-04 09:56:26 +01001145 if (!chip || !chip->parent)
Mika Westerberge9595f82014-03-31 15:16:49 +03001146 return;
1147
Linus Walleij58383c782015-11-04 09:56:26 +01001148 handle = ACPI_HANDLE(chip->parent);
Mika Westerbergaa92b6f2014-03-10 14:54:51 +02001149 if (!handle)
1150 return;
1151
1152 acpi_gpio = kzalloc(sizeof(*acpi_gpio), GFP_KERNEL);
1153 if (!acpi_gpio) {
Linus Walleij58383c782015-11-04 09:56:26 +01001154 dev_err(chip->parent,
Mika Westerbergaa92b6f2014-03-10 14:54:51 +02001155 "Failed to allocate memory for ACPI GPIO chip\n");
1156 return;
1157 }
1158
1159 acpi_gpio->chip = chip;
Mika Westerbergc103a102015-10-30 12:02:05 +02001160 INIT_LIST_HEAD(&acpi_gpio->events);
Hans de Goede78d3a922018-08-14 16:07:03 +02001161 INIT_LIST_HEAD(&acpi_gpio->deferred_req_irqs_list_entry);
Mika Westerbergaa92b6f2014-03-10 14:54:51 +02001162
1163 status = acpi_attach_data(handle, acpi_gpio_chip_dh, acpi_gpio);
1164 if (ACPI_FAILURE(status)) {
Linus Walleij58383c782015-11-04 09:56:26 +01001165 dev_err(chip->parent, "Failed to attach ACPI GPIO chip\n");
Mika Westerbergaa92b6f2014-03-10 14:54:51 +02001166 kfree(acpi_gpio);
1167 return;
1168 }
1169
Mika Westerberg4035cc12016-10-21 17:21:32 +03001170 if (!chip->names)
Christophe Leroy82270332017-12-15 15:02:33 +01001171 devprop_gpiochip_set_names(chip, dev_fwnode(chip->parent));
Mika Westerberg4035cc12016-10-21 17:21:32 +03001172
Mika Westerberg473ed7b2014-03-14 17:58:07 +02001173 acpi_gpiochip_request_regions(acpi_gpio);
Mika Westerbergc80f1ba2016-10-21 17:21:30 +03001174 acpi_gpiochip_scan_gpios(acpi_gpio);
Rui Zhang3f86a632016-06-15 08:47:51 +02001175 acpi_walk_dep_device_list(handle);
Mika Westerberg664e3e52014-01-08 12:40:54 +02001176}
1177
1178void acpi_gpiochip_remove(struct gpio_chip *chip)
1179{
Mika Westerbergaa92b6f2014-03-10 14:54:51 +02001180 struct acpi_gpio_chip *acpi_gpio;
1181 acpi_handle handle;
1182 acpi_status status;
1183
Linus Walleij58383c782015-11-04 09:56:26 +01001184 if (!chip || !chip->parent)
Mika Westerberge9595f82014-03-31 15:16:49 +03001185 return;
1186
Linus Walleij58383c782015-11-04 09:56:26 +01001187 handle = ACPI_HANDLE(chip->parent);
Mika Westerbergaa92b6f2014-03-10 14:54:51 +02001188 if (!handle)
1189 return;
1190
1191 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
1192 if (ACPI_FAILURE(status)) {
Linus Walleij58383c782015-11-04 09:56:26 +01001193 dev_warn(chip->parent, "Failed to retrieve ACPI GPIO chip\n");
Mika Westerbergaa92b6f2014-03-10 14:54:51 +02001194 return;
1195 }
1196
Mika Westerberg473ed7b2014-03-14 17:58:07 +02001197 acpi_gpiochip_free_regions(acpi_gpio);
Mika Westerbergaa92b6f2014-03-10 14:54:51 +02001198
1199 acpi_detach_data(handle, acpi_gpio_chip_dh);
1200 kfree(acpi_gpio);
Mika Westerberg664e3e52014-01-08 12:40:54 +02001201}
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001202
Mika Westerberg6f7194a2016-10-21 17:21:29 +03001203static int acpi_gpio_package_count(const union acpi_object *obj)
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001204{
1205 const union acpi_object *element = obj->package.elements;
1206 const union acpi_object *end = element + obj->package.count;
1207 unsigned int count = 0;
1208
1209 while (element < end) {
Mika Westerberg6f7194a2016-10-21 17:21:29 +03001210 switch (element->type) {
1211 case ACPI_TYPE_LOCAL_REFERENCE:
1212 element += 3;
1213 /* Fallthrough */
1214 case ACPI_TYPE_INTEGER:
1215 element++;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001216 count++;
Mika Westerberg6f7194a2016-10-21 17:21:29 +03001217 break;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001218
Mika Westerberg6f7194a2016-10-21 17:21:29 +03001219 default:
1220 return -EPROTO;
1221 }
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001222 }
Mika Westerberg6f7194a2016-10-21 17:21:29 +03001223
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001224 return count;
1225}
1226
1227static int acpi_find_gpio_count(struct acpi_resource *ares, void *data)
1228{
1229 unsigned int *count = data;
1230
1231 if (ares->type == ACPI_RESOURCE_TYPE_GPIO)
1232 *count += ares->data.gpio.pin_table_length;
1233
1234 return 1;
1235}
1236
1237/**
Andy Shevchenko85edcd02019-03-30 00:33:45 +03001238 * acpi_gpio_count - count the GPIOs associated with a device / function
1239 * @dev: GPIO consumer, can be %NULL for system-global GPIOs
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001240 * @con_id: function within the GPIO consumer
Andy Shevchenko85edcd02019-03-30 00:33:45 +03001241 *
1242 * Return:
1243 * The number of GPIOs associated with a device / function or %-ENOENT,
1244 * if no GPIO has been assigned to the requested function.
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001245 */
1246int acpi_gpio_count(struct device *dev, const char *con_id)
1247{
1248 struct acpi_device *adev = ACPI_COMPANION(dev);
1249 const union acpi_object *obj;
1250 const struct acpi_gpio_mapping *gm;
1251 int count = -ENOENT;
1252 int ret;
1253 char propname[32];
1254 unsigned int i;
1255
1256 /* Try first from _DSD */
1257 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Andy Shevchenko9e665042017-05-23 20:03:17 +03001258 if (con_id)
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001259 snprintf(propname, sizeof(propname), "%s-%s",
1260 con_id, gpio_suffixes[i]);
1261 else
1262 snprintf(propname, sizeof(propname), "%s",
1263 gpio_suffixes[i]);
1264
1265 ret = acpi_dev_get_property(adev, propname, ACPI_TYPE_ANY,
1266 &obj);
1267 if (ret == 0) {
1268 if (obj->type == ACPI_TYPE_LOCAL_REFERENCE)
1269 count = 1;
1270 else if (obj->type == ACPI_TYPE_PACKAGE)
1271 count = acpi_gpio_package_count(obj);
1272 } else if (adev->driver_gpios) {
1273 for (gm = adev->driver_gpios; gm->name; gm++)
1274 if (strcmp(propname, gm->name) == 0) {
1275 count = gm->size;
1276 break;
1277 }
1278 }
Andy Shevchenko4ed55012017-02-20 18:15:46 +02001279 if (count > 0)
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001280 break;
1281 }
1282
1283 /* Then from plain _CRS GPIOs */
1284 if (count < 0) {
1285 struct list_head resource_list;
1286 unsigned int crs_count = 0;
1287
Andy Shevchenko6fe9da42017-05-23 20:03:20 +03001288 if (!acpi_can_fallback_to_crs(adev, con_id))
1289 return count;
1290
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001291 INIT_LIST_HEAD(&resource_list);
1292 acpi_dev_get_resources(adev, &resource_list,
1293 acpi_find_gpio_count, &crs_count);
1294 acpi_dev_free_resource_list(&resource_list);
1295 if (crs_count > 0)
1296 count = crs_count;
1297 }
Andy Shevchenko4ed55012017-02-20 18:15:46 +02001298 return count ? count : -ENOENT;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001299}
Dmitry Torokhov10cf4892015-11-11 11:45:30 -08001300
Hans de Goedee59f5e02018-11-28 17:57:55 +01001301/* Run deferred acpi_gpiochip_request_irqs() */
1302static int acpi_gpio_handle_deferred_request_irqs(void)
Benjamin Tissoiresca876c72018-07-12 17:25:06 +02001303{
Hans de Goede78d3a922018-08-14 16:07:03 +02001304 struct acpi_gpio_chip *acpi_gpio, *tmp;
Benjamin Tissoiresca876c72018-07-12 17:25:06 +02001305
Hans de Goede78d3a922018-08-14 16:07:03 +02001306 mutex_lock(&acpi_gpio_deferred_req_irqs_lock);
1307 list_for_each_entry_safe(acpi_gpio, tmp,
1308 &acpi_gpio_deferred_req_irqs_list,
Hans de Goedee59f5e02018-11-28 17:57:55 +01001309 deferred_req_irqs_list_entry)
1310 acpi_gpiochip_request_irqs(acpi_gpio);
Hans de Goede78d3a922018-08-14 16:07:03 +02001311
1312 acpi_gpio_deferred_req_irqs_done = true;
1313 mutex_unlock(&acpi_gpio_deferred_req_irqs_lock);
Benjamin Tissoiresca876c72018-07-12 17:25:06 +02001314
1315 return 0;
1316}
1317/* We must use _sync so that this runs after the first deferred_probe run */
Hans de Goedee59f5e02018-11-28 17:57:55 +01001318late_initcall_sync(acpi_gpio_handle_deferred_request_irqs);
Hans de Goede61f7f7c2019-08-27 22:28:35 +02001319
Hans de Goede1ad1b542020-01-05 17:03:56 +01001320static const struct dmi_system_id gpiolib_acpi_quirks[] = {
Hans de Goede61f7f7c2019-08-27 22:28:35 +02001321 {
Hans de Goede27273152019-11-06 12:51:09 +01001322 /*
1323 * The Minix Neo Z83-4 has a micro-USB-B id-pin handler for
1324 * a non existing micro-USB-B connector which puts the HDMI
1325 * DDC pins in GPIO mode, breaking HDMI support.
1326 */
Hans de Goede61f7f7c2019-08-27 22:28:35 +02001327 .matches = {
1328 DMI_MATCH(DMI_SYS_VENDOR, "MINIX"),
1329 DMI_MATCH(DMI_PRODUCT_NAME, "Z83-4"),
Hans de Goede1ad1b542020-01-05 17:03:56 +01001330 },
1331 .driver_data = (void *)QUIRK_NO_EDGE_EVENTS_ON_BOOT,
Hans de Goede61f7f7c2019-08-27 22:28:35 +02001332 },
Hans de Goede27273152019-11-06 12:51:09 +01001333 {
1334 /*
1335 * The Terra Pad 1061 has a micro-USB-B id-pin handler, which
1336 * instead of controlling the actual micro-USB-B turns the 5V
1337 * boost for its USB-A connector off. The actual micro-USB-B
1338 * connector is wired for charging only.
1339 */
1340 .matches = {
1341 DMI_MATCH(DMI_SYS_VENDOR, "Wortmann_AG"),
1342 DMI_MATCH(DMI_PRODUCT_NAME, "TERRA_PAD_1061"),
Hans de Goede1ad1b542020-01-05 17:03:56 +01001343 },
1344 .driver_data = (void *)QUIRK_NO_EDGE_EVENTS_ON_BOOT,
Hans de Goede27273152019-11-06 12:51:09 +01001345 },
Hans de Goedeaa23ca32020-01-05 17:03:57 +01001346 {
1347 /*
1348 * Various HP X2 10 Cherry Trail models use an external
1349 * embedded-controller connected via I2C + an ACPI GPIO
1350 * event handler. The embedded controller generates various
1351 * spurious wakeup events when suspended. So disable wakeup
1352 * for its handler (it uses the only ACPI GPIO event handler).
1353 * This breaks wakeup when opening the lid, the user needs
1354 * to press the power-button to wakeup the system. The
1355 * alternative is suspend simply not working, which is worse.
1356 */
1357 .matches = {
1358 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
1359 DMI_MATCH(DMI_PRODUCT_NAME, "HP x2 Detachable 10-p0XX"),
1360 },
1361 .driver_data = (void *)QUIRK_NO_WAKEUP,
1362 },
Hans de Goede61f7f7c2019-08-27 22:28:35 +02001363 {} /* Terminating entry */
1364};
1365
1366static int acpi_gpio_setup_params(void)
1367{
Hans de Goede1ad1b542020-01-05 17:03:56 +01001368 const struct dmi_system_id *id;
1369 long quirks = 0;
1370
1371 id = dmi_first_match(gpiolib_acpi_quirks);
1372 if (id)
1373 quirks = (long)id->driver_data;
1374
Hans de Goede61f7f7c2019-08-27 22:28:35 +02001375 if (run_edge_events_on_boot < 0) {
Hans de Goede1ad1b542020-01-05 17:03:56 +01001376 if (quirks & QUIRK_NO_EDGE_EVENTS_ON_BOOT)
Hans de Goede61f7f7c2019-08-27 22:28:35 +02001377 run_edge_events_on_boot = 0;
1378 else
1379 run_edge_events_on_boot = 1;
1380 }
1381
Hans de Goedeaa23ca32020-01-05 17:03:57 +01001382 if (honor_wakeup < 0) {
1383 if (quirks & QUIRK_NO_WAKEUP)
1384 honor_wakeup = 0;
1385 else
1386 honor_wakeup = 1;
1387 }
1388
Hans de Goede61f7f7c2019-08-27 22:28:35 +02001389 return 0;
1390}
1391
1392/* Directly after dmi_setup() which runs as core_initcall() */
1393postcore_initcall(acpi_gpio_setup_params);