blob: 55b72fbe163169c29766fbb6e24f58ccfdfd62c2 [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
10#include <linux/errno.h>
Mika Westerberg936e15d2013-10-10 11:01:08 +030011#include <linux/gpio/consumer.h>
Mika Westerberg5ccff852014-01-08 12:40:56 +020012#include <linux/gpio/driver.h>
Linus Walleij031ba282016-10-03 10:40:03 +020013#include <linux/gpio/machine.h>
Mathias Nymane29482e2012-11-30 12:37:36 +010014#include <linux/export.h>
Mathias Nymane29482e2012-11-30 12:37:36 +010015#include <linux/acpi.h>
Mathias Nyman0d1c28a2013-01-28 16:23:10 +020016#include <linux/interrupt.h>
Mika Westerberg473ed7b2014-03-14 17:58:07 +020017#include <linux/mutex.h>
Mika Westerberg354567e2014-11-03 13:01:32 +020018#include <linux/pinctrl/pinctrl.h>
Mathias Nymane29482e2012-11-30 12:37:36 +010019
Mika Westerberg5ccff852014-01-08 12:40:56 +020020#include "gpiolib.h"
21
Mika Westerberg4b01a142014-03-10 14:54:52 +020022struct acpi_gpio_event {
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +020023 struct list_head node;
Mika Westerberg4b01a142014-03-10 14:54:52 +020024 acpi_handle handle;
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +020025 unsigned int pin;
26 unsigned int irq;
Alexandre Courbote46cf322014-08-19 10:06:08 -070027 struct gpio_desc *desc;
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +020028};
29
Mika Westerberg473ed7b2014-03-14 17:58:07 +020030struct acpi_gpio_connection {
31 struct list_head node;
Alexandre Courbote46cf322014-08-19 10:06:08 -070032 unsigned int pin;
Mika Westerberg473ed7b2014-03-14 17:58:07 +020033 struct gpio_desc *desc;
34};
35
Mika Westerbergaa92b6f2014-03-10 14:54:51 +020036struct acpi_gpio_chip {
Mika Westerberg473ed7b2014-03-14 17:58:07 +020037 /*
38 * ACPICA requires that the first field of the context parameter
39 * passed to acpi_install_address_space_handler() is large enough
40 * to hold struct acpi_connection_info.
41 */
42 struct acpi_connection_info conn_info;
43 struct list_head conns;
44 struct mutex conn_lock;
Mika Westerbergaa92b6f2014-03-10 14:54:51 +020045 struct gpio_chip *chip;
Mika Westerberg4b01a142014-03-10 14:54:52 +020046 struct list_head events;
Hans de Goede78d3a922018-08-14 16:07:03 +020047 struct list_head deferred_req_irqs_list_entry;
Mika Westerbergaa92b6f2014-03-10 14:54:51 +020048};
49
Hans de Goede78d3a922018-08-14 16:07:03 +020050/*
51 * For gpiochips which call acpi_gpiochip_request_interrupts() before late_init
52 * (so builtin drivers) we register the ACPI GpioInt event handlers from a
53 * late_initcall_sync handler, so that other builtin drivers can register their
54 * OpRegions before the event handlers can run. This list contains gpiochips
55 * for which the acpi_gpiochip_request_interrupts() has been deferred.
56 */
57static DEFINE_MUTEX(acpi_gpio_deferred_req_irqs_lock);
58static LIST_HEAD(acpi_gpio_deferred_req_irqs_list);
59static bool acpi_gpio_deferred_req_irqs_done;
Benjamin Tissoiresca876c72018-07-12 17:25:06 +020060
Mathias Nymane29482e2012-11-30 12:37:36 +010061static int acpi_gpiochip_find(struct gpio_chip *gc, void *data)
62{
Linus Walleij58383c782015-11-04 09:56:26 +010063 if (!gc->parent)
Mathias Nymane29482e2012-11-30 12:37:36 +010064 return false;
65
Linus Walleij58383c782015-11-04 09:56:26 +010066 return ACPI_HANDLE(gc->parent) == data;
Mathias Nymane29482e2012-11-30 12:37:36 +010067}
68
69/**
Mika Westerberg936e15d2013-10-10 11:01:08 +030070 * acpi_get_gpiod() - Translate ACPI GPIO pin to GPIO descriptor usable with GPIO API
Mathias Nymane29482e2012-11-30 12:37:36 +010071 * @path: ACPI GPIO controller full path name, (e.g. "\\_SB.GPO1")
72 * @pin: ACPI GPIO pin number (0-based, controller-relative)
73 *
Mika Westerbergf35bbf62015-06-10 16:05:05 +030074 * Return: GPIO descriptor to use with Linux generic GPIO API, or ERR_PTR
75 * error value. Specifically returns %-EPROBE_DEFER if the referenced GPIO
76 * controller does not have gpiochip registered at the moment. This is to
77 * support probe deferral.
Mathias Nymane29482e2012-11-30 12:37:36 +010078 */
Mika Westerberg936e15d2013-10-10 11:01:08 +030079static struct gpio_desc *acpi_get_gpiod(char *path, int pin)
Mathias Nymane29482e2012-11-30 12:37:36 +010080{
81 struct gpio_chip *chip;
82 acpi_handle handle;
83 acpi_status status;
84
85 status = acpi_get_handle(NULL, path, &handle);
86 if (ACPI_FAILURE(status))
Mika Westerberg936e15d2013-10-10 11:01:08 +030087 return ERR_PTR(-ENODEV);
Mathias Nymane29482e2012-11-30 12:37:36 +010088
89 chip = gpiochip_find(handle, acpi_gpiochip_find);
90 if (!chip)
Mika Westerbergf35bbf62015-06-10 16:05:05 +030091 return ERR_PTR(-EPROBE_DEFER);
Mathias Nymane29482e2012-11-30 12:37:36 +010092
Mika Westerberg03c47492017-11-27 16:54:42 +030093 return gpiochip_get_desc(chip, pin);
Mathias Nymane29482e2012-11-30 12:37:36 +010094}
Mathias Nyman0d1c28a2013-01-28 16:23:10 +020095
Mathias Nyman0d1c28a2013-01-28 16:23:10 +020096static irqreturn_t acpi_gpio_irq_handler(int irq, void *data)
97{
Mika Westerberg6072b9d2014-03-10 14:54:53 +020098 struct acpi_gpio_event *event = data;
Mathias Nyman0d1c28a2013-01-28 16:23:10 +020099
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200100 acpi_evaluate_object(event->handle, NULL, NULL, NULL);
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200101
102 return IRQ_HANDLED;
103}
104
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +0200105static irqreturn_t acpi_gpio_irq_handler_evt(int irq, void *data)
106{
Mika Westerberg4b01a142014-03-10 14:54:52 +0200107 struct acpi_gpio_event *event = data;
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +0200108
Mika Westerberg4b01a142014-03-10 14:54:52 +0200109 acpi_execute_simple_method(event->handle, NULL, event->pin);
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +0200110
111 return IRQ_HANDLED;
112}
113
Mika Westerbergaa92b6f2014-03-10 14:54:51 +0200114static void acpi_gpio_chip_dh(acpi_handle handle, void *data)
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +0200115{
116 /* The address of this function is used as a key. */
117}
118
Andy Shevchenko25e3ef82017-05-23 20:03:24 +0300119bool acpi_gpio_get_irq_resource(struct acpi_resource *ares,
120 struct acpi_resource_gpio **agpio)
121{
122 struct acpi_resource_gpio *gpio;
123
124 if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
125 return false;
126
127 gpio = &ares->data.gpio;
128 if (gpio->connection_type != ACPI_RESOURCE_GPIO_TYPE_INT)
129 return false;
130
131 *agpio = gpio;
132 return true;
133}
134EXPORT_SYMBOL_GPL(acpi_gpio_get_irq_resource);
135
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200136static acpi_status acpi_gpiochip_request_interrupt(struct acpi_resource *ares,
137 void *context)
138{
139 struct acpi_gpio_chip *acpi_gpio = context;
140 struct gpio_chip *chip = acpi_gpio->chip;
141 struct acpi_resource_gpio *agpio;
142 acpi_handle handle, evt_handle;
143 struct acpi_gpio_event *event;
144 irq_handler_t handler = NULL;
145 struct gpio_desc *desc;
146 unsigned long irqflags;
Benjamin Tissoiresca876c72018-07-12 17:25:06 +0200147 int ret, pin, irq, value;
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200148
Andy Shevchenko25e3ef82017-05-23 20:03:24 +0300149 if (!acpi_gpio_get_irq_resource(ares, &agpio))
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200150 return AE_OK;
151
Linus Walleij58383c782015-11-04 09:56:26 +0100152 handle = ACPI_HANDLE(chip->parent);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200153 pin = agpio->pin_table[0];
154
155 if (pin <= 255) {
156 char ev_name[5];
Arnd Bergmanne40a3ae2017-09-06 17:47:45 +0200157 sprintf(ev_name, "_%c%02hhX",
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200158 agpio->triggering == ACPI_EDGE_SENSITIVE ? 'E' : 'L',
159 pin);
160 if (ACPI_SUCCESS(acpi_get_handle(handle, ev_name, &evt_handle)))
161 handler = acpi_gpio_irq_handler;
162 }
163 if (!handler) {
164 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EVT", &evt_handle)))
165 handler = acpi_gpio_irq_handler_evt;
166 }
167 if (!handler)
Hans de Goedec06632e2017-06-23 09:26:13 +0200168 return AE_OK;
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200169
Alexandre Courbotabdc08a2014-08-19 10:06:09 -0700170 desc = gpiochip_request_own_desc(chip, pin, "ACPI:Event");
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200171 if (IS_ERR(desc)) {
Linus Walleij58383c782015-11-04 09:56:26 +0100172 dev_err(chip->parent, "Failed to request GPIO\n");
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200173 return AE_ERROR;
174 }
175
176 gpiod_direction_input(desc);
177
Andy Shevchenko993b9bc2018-08-13 19:00:27 +0300178 value = gpiod_get_value_cansleep(desc);
Benjamin Tissoiresca876c72018-07-12 17:25:06 +0200179
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900180 ret = gpiochip_lock_as_irq(chip, pin);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200181 if (ret) {
Linus Walleij58383c782015-11-04 09:56:26 +0100182 dev_err(chip->parent, "Failed to lock GPIO as interrupt\n");
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200183 goto fail_free_desc;
184 }
185
186 irq = gpiod_to_irq(desc);
187 if (irq < 0) {
Linus Walleij58383c782015-11-04 09:56:26 +0100188 dev_err(chip->parent, "Failed to translate GPIO to IRQ\n");
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200189 goto fail_unlock_irq;
190 }
191
192 irqflags = IRQF_ONESHOT;
193 if (agpio->triggering == ACPI_LEVEL_SENSITIVE) {
194 if (agpio->polarity == ACPI_ACTIVE_HIGH)
195 irqflags |= IRQF_TRIGGER_HIGH;
196 else
197 irqflags |= IRQF_TRIGGER_LOW;
198 } else {
199 switch (agpio->polarity) {
200 case ACPI_ACTIVE_HIGH:
201 irqflags |= IRQF_TRIGGER_RISING;
202 break;
203 case ACPI_ACTIVE_LOW:
204 irqflags |= IRQF_TRIGGER_FALLING;
205 break;
206 default:
207 irqflags |= IRQF_TRIGGER_RISING |
208 IRQF_TRIGGER_FALLING;
209 break;
210 }
211 }
212
213 event = kzalloc(sizeof(*event), GFP_KERNEL);
214 if (!event)
215 goto fail_unlock_irq;
216
217 event->handle = evt_handle;
218 event->irq = irq;
219 event->pin = pin;
Alexandre Courbote46cf322014-08-19 10:06:08 -0700220 event->desc = desc;
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200221
222 ret = request_threaded_irq(event->irq, NULL, handler, irqflags,
223 "ACPI:Event", event);
224 if (ret) {
Linus Walleij58383c782015-11-04 09:56:26 +0100225 dev_err(chip->parent,
226 "Failed to setup interrupt handler for %d\n",
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200227 event->irq);
228 goto fail_free_event;
229 }
230
Hans de Goede8a146fb2017-03-24 11:08:47 +0100231 if (agpio->wake_capable == ACPI_WAKE_CAPABLE)
232 enable_irq_wake(irq);
233
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200234 list_add_tail(&event->node, &acpi_gpio->events);
Benjamin Tissoiresca876c72018-07-12 17:25:06 +0200235
236 /*
237 * Make sure we trigger the initial state of the IRQ when using RISING
238 * or FALLING. Note we run the handlers on late_init, the AML code
239 * may refer to OperationRegions from other (builtin) drivers which
240 * may be probed after us.
241 */
Hans de Goede78d3a922018-08-14 16:07:03 +0200242 if (((irqflags & IRQF_TRIGGER_RISING) && value == 1) ||
243 ((irqflags & IRQF_TRIGGER_FALLING) && value == 0))
244 handler(event->irq, event);
Benjamin Tissoiresca876c72018-07-12 17:25:06 +0200245
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200246 return AE_OK;
247
248fail_free_event:
249 kfree(event);
250fail_unlock_irq:
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900251 gpiochip_unlock_as_irq(chip, pin);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200252fail_free_desc:
253 gpiochip_free_own_desc(desc);
254
255 return AE_ERROR;
256}
257
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200258/**
259 * acpi_gpiochip_request_interrupts() - Register isr for gpio chip ACPI events
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300260 * @chip: GPIO chip
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200261 *
262 * ACPI5 platforms can use GPIO signaled ACPI events. These GPIO interrupts are
263 * handled by ACPI event methods which need to be called from the GPIO
264 * chip's interrupt handler. acpi_gpiochip_request_interrupts finds out which
265 * gpio pins have acpi event methods and assigns interrupt handlers that calls
266 * the acpi event methods for those pins.
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200267 */
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300268void acpi_gpiochip_request_interrupts(struct gpio_chip *chip)
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200269{
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300270 struct acpi_gpio_chip *acpi_gpio;
271 acpi_handle handle;
272 acpi_status status;
Hans de Goede78d3a922018-08-14 16:07:03 +0200273 bool defer;
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200274
Linus Walleij58383c782015-11-04 09:56:26 +0100275 if (!chip->parent || !chip->to_irq)
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300276 return;
277
Linus Walleij58383c782015-11-04 09:56:26 +0100278 handle = ACPI_HANDLE(chip->parent);
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300279 if (!handle)
280 return;
281
282 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
283 if (ACPI_FAILURE(status))
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200284 return;
285
Hans de Goede78d3a922018-08-14 16:07:03 +0200286 mutex_lock(&acpi_gpio_deferred_req_irqs_lock);
287 defer = !acpi_gpio_deferred_req_irqs_done;
288 if (defer)
289 list_add(&acpi_gpio->deferred_req_irqs_list_entry,
290 &acpi_gpio_deferred_req_irqs_list);
291 mutex_unlock(&acpi_gpio_deferred_req_irqs_lock);
292
293 if (defer)
294 return;
295
Rafael J. Wysocki1ecb0162015-03-10 23:10:01 +0100296 acpi_walk_resources(handle, "_AEI",
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200297 acpi_gpiochip_request_interrupt, acpi_gpio);
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200298}
Hanjun Guo2b528ff2015-06-10 17:12:07 +0800299EXPORT_SYMBOL_GPL(acpi_gpiochip_request_interrupts);
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +0200300
Mika Westerberg70b534112013-10-10 11:01:07 +0300301/**
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200302 * acpi_gpiochip_free_interrupts() - Free GPIO ACPI event interrupts.
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300303 * @chip: GPIO chip
Mika Westerberg70b534112013-10-10 11:01:07 +0300304 *
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200305 * Free interrupts associated with GPIO ACPI event method for the given
306 * GPIO chip.
Mika Westerberg70b534112013-10-10 11:01:07 +0300307 */
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300308void acpi_gpiochip_free_interrupts(struct gpio_chip *chip)
Mika Westerberg70b534112013-10-10 11:01:07 +0300309{
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300310 struct acpi_gpio_chip *acpi_gpio;
Mika Westerberg4b01a142014-03-10 14:54:52 +0200311 struct acpi_gpio_event *event, *ep;
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300312 acpi_handle handle;
313 acpi_status status;
Mika Westerberg70b534112013-10-10 11:01:07 +0300314
Linus Walleij58383c782015-11-04 09:56:26 +0100315 if (!chip->parent || !chip->to_irq)
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300316 return;
317
Linus Walleij58383c782015-11-04 09:56:26 +0100318 handle = ACPI_HANDLE(chip->parent);
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300319 if (!handle)
320 return;
321
322 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
323 if (ACPI_FAILURE(status))
Mika Westerberg70b534112013-10-10 11:01:07 +0300324 return;
325
Hans de Goede78d3a922018-08-14 16:07:03 +0200326 mutex_lock(&acpi_gpio_deferred_req_irqs_lock);
327 if (!list_empty(&acpi_gpio->deferred_req_irqs_list_entry))
328 list_del_init(&acpi_gpio->deferred_req_irqs_list_entry);
329 mutex_unlock(&acpi_gpio_deferred_req_irqs_lock);
330
Mika Westerberg4b01a142014-03-10 14:54:52 +0200331 list_for_each_entry_safe_reverse(event, ep, &acpi_gpio->events, node) {
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200332 struct gpio_desc *desc;
333
Hans de Goede8a146fb2017-03-24 11:08:47 +0100334 if (irqd_is_wakeup_set(irq_get_irq_data(event->irq)))
335 disable_irq_wake(event->irq);
336
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200337 free_irq(event->irq, event);
Alexandre Courbote46cf322014-08-19 10:06:08 -0700338 desc = event->desc;
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200339 if (WARN_ON(IS_ERR(desc)))
340 continue;
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900341 gpiochip_unlock_as_irq(chip, event->pin);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200342 gpiochip_free_own_desc(desc);
Mika Westerberg4b01a142014-03-10 14:54:52 +0200343 list_del(&event->node);
344 kfree(event);
Mika Westerberg70b534112013-10-10 11:01:07 +0300345 }
Mika Westerberg70b534112013-10-10 11:01:07 +0300346}
Hanjun Guo2b528ff2015-06-10 17:12:07 +0800347EXPORT_SYMBOL_GPL(acpi_gpiochip_free_interrupts);
Mika Westerberg70b534112013-10-10 11:01:07 +0300348
Rafael J. Wysockif028d522014-11-03 23:39:41 +0100349int acpi_dev_add_driver_gpios(struct acpi_device *adev,
350 const struct acpi_gpio_mapping *gpios)
351{
352 if (adev && gpios) {
353 adev->driver_gpios = gpios;
354 return 0;
355 }
356 return -EINVAL;
357}
358EXPORT_SYMBOL_GPL(acpi_dev_add_driver_gpios);
359
Andy Shevchenko85c73d52017-03-02 15:48:05 +0200360static void devm_acpi_dev_release_driver_gpios(struct device *dev, void *res)
361{
362 acpi_dev_remove_driver_gpios(ACPI_COMPANION(dev));
363}
364
365int devm_acpi_dev_add_driver_gpios(struct device *dev,
366 const struct acpi_gpio_mapping *gpios)
367{
368 void *res;
369 int ret;
370
371 res = devres_alloc(devm_acpi_dev_release_driver_gpios, 0, GFP_KERNEL);
372 if (!res)
373 return -ENOMEM;
374
375 ret = acpi_dev_add_driver_gpios(ACPI_COMPANION(dev), gpios);
376 if (ret) {
377 devres_free(res);
378 return ret;
379 }
380 devres_add(dev, res);
381 return 0;
382}
383EXPORT_SYMBOL_GPL(devm_acpi_dev_add_driver_gpios);
384
385void devm_acpi_dev_remove_driver_gpios(struct device *dev)
386{
387 WARN_ON(devres_release(dev, devm_acpi_dev_release_driver_gpios, NULL, NULL));
388}
389EXPORT_SYMBOL_GPL(devm_acpi_dev_remove_driver_gpios);
390
Rafael J. Wysockif028d522014-11-03 23:39:41 +0100391static bool acpi_get_driver_gpio_data(struct acpi_device *adev,
392 const char *name, int index,
Sakari Ailus977d5ad2018-07-17 17:19:11 +0300393 struct fwnode_reference_args *args,
Andy Shevchenkoce0929d2017-11-10 15:40:32 +0200394 unsigned int *quirks)
Rafael J. Wysockif028d522014-11-03 23:39:41 +0100395{
396 const struct acpi_gpio_mapping *gm;
397
398 if (!adev->driver_gpios)
399 return false;
400
401 for (gm = adev->driver_gpios; gm->name; gm++)
402 if (!strcmp(name, gm->name) && gm->data && index < gm->size) {
403 const struct acpi_gpio_params *par = gm->data + index;
404
Sakari Ailus977d5ad2018-07-17 17:19:11 +0300405 args->fwnode = acpi_fwnode_handle(adev);
Rafael J. Wysockif028d522014-11-03 23:39:41 +0100406 args->args[0] = par->crs_entry_index;
407 args->args[1] = par->line_index;
408 args->args[2] = par->active_low;
409 args->nargs = 3;
Andy Shevchenkoce0929d2017-11-10 15:40:32 +0200410
411 *quirks = gm->quirks;
Rafael J. Wysockif028d522014-11-03 23:39:41 +0100412 return true;
413 }
414
415 return false;
416}
417
Andy Shevchenko2eca25a2017-05-23 20:03:22 +0300418static enum gpiod_flags
419acpi_gpio_to_gpiod_flags(const struct acpi_resource_gpio *agpio)
420{
421 bool pull_up = agpio->pin_config == ACPI_PIN_CONFIG_PULLUP;
422
423 switch (agpio->io_restriction) {
424 case ACPI_IO_RESTRICT_INPUT:
425 return GPIOD_IN;
426 case ACPI_IO_RESTRICT_OUTPUT:
427 /*
428 * ACPI GPIO resources don't contain an initial value for the
429 * GPIO. Therefore we deduce that value from the pull field
430 * instead. If the pin is pulled up we assume default to be
431 * high, otherwise low.
432 */
433 return pull_up ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
434 default:
435 /*
436 * Assume that the BIOS has configured the direction and pull
437 * accordingly.
438 */
439 return GPIOD_ASIS;
440 }
441}
442
Andy Shevchenko5c34b6c2017-11-10 15:40:31 +0200443static int
444__acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, enum gpiod_flags update)
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300445{
446 int ret = 0;
447
448 /*
449 * Check if the BIOS has IoRestriction with explicitly set direction
450 * and update @flags accordingly. Otherwise use whatever caller asked
451 * for.
452 */
453 if (update & GPIOD_FLAGS_BIT_DIR_SET) {
454 enum gpiod_flags diff = *flags ^ update;
455
456 /*
457 * Check if caller supplied incompatible GPIO initialization
458 * flags.
459 *
460 * Return %-EINVAL to notify that firmware has different
461 * settings and we are going to use them.
462 */
463 if (((*flags & GPIOD_FLAGS_BIT_DIR_SET) && (diff & GPIOD_FLAGS_BIT_DIR_OUT)) ||
464 ((*flags & GPIOD_FLAGS_BIT_DIR_OUT) && (diff & GPIOD_FLAGS_BIT_DIR_VAL)))
465 ret = -EINVAL;
466 *flags = update;
467 }
468 return ret;
469}
470
Andy Shevchenko5c34b6c2017-11-10 15:40:31 +0200471int
472acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, struct acpi_gpio_info *info)
473{
474 struct device *dev = &info->adev->dev;
Andy Shevchenko1b2ca322017-11-10 15:40:33 +0200475 enum gpiod_flags old = *flags;
Andy Shevchenko5c34b6c2017-11-10 15:40:31 +0200476 int ret;
477
Andy Shevchenko1b2ca322017-11-10 15:40:33 +0200478 ret = __acpi_gpio_update_gpiod_flags(&old, info->flags);
479 if (info->quirks & ACPI_GPIO_QUIRK_NO_IO_RESTRICTION) {
480 if (ret)
481 dev_warn(dev, FW_BUG "GPIO not in correct mode, fixing\n");
482 } else {
483 if (ret)
484 dev_dbg(dev, "Override GPIO initialization flags\n");
485 *flags = old;
486 }
Andy Shevchenko5c34b6c2017-11-10 15:40:31 +0200487
488 return ret;
489}
490
Mika Westerberg12028d22013-04-03 13:56:54 +0300491struct acpi_gpio_lookup {
492 struct acpi_gpio_info info;
493 int index;
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100494 int pin_index;
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200495 bool active_low;
Mika Westerberg936e15d2013-10-10 11:01:08 +0300496 struct gpio_desc *desc;
Mika Westerberg12028d22013-04-03 13:56:54 +0300497 int n;
498};
499
Linus Walleij031ba282016-10-03 10:40:03 +0200500static int acpi_populate_gpio_lookup(struct acpi_resource *ares, void *data)
Mika Westerberg12028d22013-04-03 13:56:54 +0300501{
502 struct acpi_gpio_lookup *lookup = data;
503
504 if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
505 return 1;
506
Mika Westerberg936e15d2013-10-10 11:01:08 +0300507 if (lookup->n++ == lookup->index && !lookup->desc) {
Mika Westerberg12028d22013-04-03 13:56:54 +0300508 const struct acpi_resource_gpio *agpio = &ares->data.gpio;
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100509 int pin_index = lookup->pin_index;
510
511 if (pin_index >= agpio->pin_table_length)
512 return 1;
Mika Westerberg12028d22013-04-03 13:56:54 +0300513
Mika Westerberg936e15d2013-10-10 11:01:08 +0300514 lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr,
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100515 agpio->pin_table[pin_index]);
Mika Westerberg12028d22013-04-03 13:56:54 +0300516 lookup->info.gpioint =
517 agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100518
519 /*
Andy Shevchenkoe567c352017-01-03 19:01:18 +0200520 * Polarity and triggering are only specified for GpioInt
521 * resource.
Christophe RICARD52044722015-12-23 23:25:34 +0100522 * Note: we expect here:
523 * - ACPI_ACTIVE_LOW == GPIO_ACTIVE_LOW
524 * - ACPI_ACTIVE_HIGH == GPIO_ACTIVE_HIGH
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100525 */
Christophe RICARD52044722015-12-23 23:25:34 +0100526 if (lookup->info.gpioint) {
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300527 lookup->info.flags = GPIOD_IN;
Christophe RICARD52044722015-12-23 23:25:34 +0100528 lookup->info.polarity = agpio->polarity;
529 lookup->info.triggering = agpio->triggering;
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300530 } else {
531 lookup->info.flags = acpi_gpio_to_gpiod_flags(agpio);
Andy Shevchenkof67a6c12017-11-10 15:40:28 +0200532 lookup->info.polarity = lookup->active_low;
Christophe RICARD52044722015-12-23 23:25:34 +0100533 }
Mika Westerberg12028d22013-04-03 13:56:54 +0300534 }
535
536 return 1;
537}
538
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200539static int acpi_gpio_resource_lookup(struct acpi_gpio_lookup *lookup,
540 struct acpi_gpio_info *info)
541{
Andy Shevchenko5870cff472017-11-10 15:40:30 +0200542 struct acpi_device *adev = lookup->info.adev;
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200543 struct list_head res_list;
544 int ret;
545
546 INIT_LIST_HEAD(&res_list);
547
Andy Shevchenko5870cff472017-11-10 15:40:30 +0200548 ret = acpi_dev_get_resources(adev, &res_list,
Linus Walleij031ba282016-10-03 10:40:03 +0200549 acpi_populate_gpio_lookup,
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200550 lookup);
551 if (ret < 0)
552 return ret;
553
554 acpi_dev_free_resource_list(&res_list);
555
556 if (!lookup->desc)
557 return -ENOENT;
558
Andy Shevchenkof67a6c12017-11-10 15:40:28 +0200559 if (info)
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200560 *info = lookup->info;
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200561 return 0;
562}
563
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200564static int acpi_gpio_property_lookup(struct fwnode_handle *fwnode,
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200565 const char *propname, int index,
566 struct acpi_gpio_lookup *lookup)
567{
Sakari Ailus977d5ad2018-07-17 17:19:11 +0300568 struct fwnode_reference_args args;
Andy Shevchenkoce0929d2017-11-10 15:40:32 +0200569 unsigned int quirks = 0;
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200570 int ret;
571
572 memset(&args, 0, sizeof(args));
Mika Westerberg6f7194a2016-10-21 17:21:29 +0300573 ret = __acpi_node_get_property_reference(fwnode, propname, index, 3,
574 &args);
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200575 if (ret) {
576 struct acpi_device *adev = to_acpi_device_node(fwnode);
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200577
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200578 if (!adev)
579 return ret;
580
Andy Shevchenkoce0929d2017-11-10 15:40:32 +0200581 if (!acpi_get_driver_gpio_data(adev, propname, index, &args,
582 &quirks))
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200583 return ret;
584 }
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200585 /*
586 * The property was found and resolved, so need to lookup the GPIO based
587 * on returned args.
588 */
Sakari Ailus977d5ad2018-07-17 17:19:11 +0300589 if (!to_acpi_device_node(args.fwnode))
590 return -EINVAL;
Mika Westerberg6f7194a2016-10-21 17:21:29 +0300591 if (args.nargs != 3)
592 return -EPROTO;
593
594 lookup->index = args.args[0];
595 lookup->pin_index = args.args[1];
596 lookup->active_low = !!args.args[2];
597
Sakari Ailus977d5ad2018-07-17 17:19:11 +0300598 lookup->info.adev = to_acpi_device_node(args.fwnode);
Andy Shevchenkoce0929d2017-11-10 15:40:32 +0200599 lookup->info.quirks = quirks;
Sakari Ailus977d5ad2018-07-17 17:19:11 +0300600
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200601 return 0;
602}
603
Mika Westerberg12028d22013-04-03 13:56:54 +0300604/**
Mika Westerberg936e15d2013-10-10 11:01:08 +0300605 * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100606 * @adev: pointer to a ACPI device to get GPIO from
607 * @propname: Property name of the GPIO (optional)
Mika Westerberg12028d22013-04-03 13:56:54 +0300608 * @index: index of GpioIo/GpioInt resource (starting from %0)
609 * @info: info pointer to fill in (optional)
610 *
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100611 * Function goes through ACPI resources for @adev and based on @index looks
Mika Westerberg936e15d2013-10-10 11:01:08 +0300612 * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor,
Mika Westerberg12028d22013-04-03 13:56:54 +0300613 * and returns it. @index matches GpioIo/GpioInt resources only so if there
614 * are total %3 GPIO resources, the index goes from %0 to %2.
615 *
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100616 * If @propname is specified the GPIO is looked using device property. In
617 * that case @index is used to select the GPIO entry in the property value
618 * (in case of multiple).
619 *
Mika Westerberg936e15d2013-10-10 11:01:08 +0300620 * If the GPIO cannot be translated or there is an error an ERR_PTR is
Mika Westerberg12028d22013-04-03 13:56:54 +0300621 * returned.
622 *
623 * Note: if the GPIO resource has multiple entries in the pin list, this
624 * function only returns the first.
625 */
Linus Walleij031ba282016-10-03 10:40:03 +0200626static struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100627 const char *propname, int index,
Mika Westerberg936e15d2013-10-10 11:01:08 +0300628 struct acpi_gpio_info *info)
Mika Westerberg12028d22013-04-03 13:56:54 +0300629{
630 struct acpi_gpio_lookup lookup;
Mika Westerberg12028d22013-04-03 13:56:54 +0300631 int ret;
632
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100633 if (!adev)
Mika Westerberg936e15d2013-10-10 11:01:08 +0300634 return ERR_PTR(-ENODEV);
Mika Westerberg12028d22013-04-03 13:56:54 +0300635
636 memset(&lookup, 0, sizeof(lookup));
637 lookup.index = index;
Mika Westerberg12028d22013-04-03 13:56:54 +0300638
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100639 if (propname) {
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100640 dev_dbg(&adev->dev, "GPIO: looking up %s\n", propname);
641
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200642 ret = acpi_gpio_property_lookup(acpi_fwnode_handle(adev),
643 propname, index, &lookup);
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200644 if (ret)
645 return ERR_PTR(ret);
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100646
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200647 dev_dbg(&adev->dev, "GPIO: _DSD returned %s %d %d %u\n",
Andy Shevchenko5870cff472017-11-10 15:40:30 +0200648 dev_name(&lookup.info.adev->dev), lookup.index,
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200649 lookup.pin_index, lookup.active_low);
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100650 } else {
651 dev_dbg(&adev->dev, "GPIO: looking up %d in _CRS\n", index);
Andy Shevchenko5870cff472017-11-10 15:40:30 +0200652 lookup.info.adev = adev;
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100653 }
654
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200655 ret = acpi_gpio_resource_lookup(&lookup, info);
656 return ret ? ERR_PTR(ret) : lookup.desc;
Mika Westerberg12028d22013-04-03 13:56:54 +0300657}
Mika Westerberg664e3e52014-01-08 12:40:54 +0200658
Linus Walleij031ba282016-10-03 10:40:03 +0200659struct gpio_desc *acpi_find_gpio(struct device *dev,
660 const char *con_id,
661 unsigned int idx,
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300662 enum gpiod_flags *dflags,
Linus Walleij031ba282016-10-03 10:40:03 +0200663 enum gpio_lookup_flags *lookupflags)
664{
665 struct acpi_device *adev = ACPI_COMPANION(dev);
666 struct acpi_gpio_info info;
667 struct gpio_desc *desc;
668 char propname[32];
669 int i;
670
671 /* Try first from _DSD */
672 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Andy Shevchenko9e665042017-05-23 20:03:17 +0300673 if (con_id) {
Linus Walleij031ba282016-10-03 10:40:03 +0200674 snprintf(propname, sizeof(propname), "%s-%s",
675 con_id, gpio_suffixes[i]);
676 } else {
677 snprintf(propname, sizeof(propname), "%s",
678 gpio_suffixes[i]);
679 }
680
681 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
Dmitry Torokhov693bdaa2017-03-23 13:21:38 -0700682 if (!IS_ERR(desc))
Linus Walleij031ba282016-10-03 10:40:03 +0200683 break;
Dmitry Torokhov693bdaa2017-03-23 13:21:38 -0700684 if (PTR_ERR(desc) == -EPROBE_DEFER)
685 return ERR_CAST(desc);
Linus Walleij031ba282016-10-03 10:40:03 +0200686 }
687
688 /* Then from plain _CRS GPIOs */
689 if (IS_ERR(desc)) {
690 if (!acpi_can_fallback_to_crs(adev, con_id))
691 return ERR_PTR(-ENOENT);
692
693 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
694 if (IS_ERR(desc))
695 return desc;
Andy Shevchenkofe06b562017-05-23 20:03:18 +0300696 }
Linus Walleij031ba282016-10-03 10:40:03 +0200697
Andy Shevchenkofe06b562017-05-23 20:03:18 +0300698 if (info.gpioint &&
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300699 (*dflags == GPIOD_OUT_LOW || *dflags == GPIOD_OUT_HIGH)) {
Andy Shevchenkofe06b562017-05-23 20:03:18 +0300700 dev_dbg(dev, "refusing GpioInt() entry when doing GPIOD_OUT_* lookup\n");
701 return ERR_PTR(-ENOENT);
Linus Walleij031ba282016-10-03 10:40:03 +0200702 }
703
704 if (info.polarity == GPIO_ACTIVE_LOW)
705 *lookupflags |= GPIO_ACTIVE_LOW;
706
Andy Shevchenko5c34b6c2017-11-10 15:40:31 +0200707 acpi_gpio_update_gpiod_flags(dflags, &info);
Linus Walleij031ba282016-10-03 10:40:03 +0200708 return desc;
709}
710
Mika Westerbergc884fbd2015-05-06 13:29:06 +0300711/**
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200712 * acpi_node_get_gpiod() - get a GPIO descriptor from ACPI resources
713 * @fwnode: pointer to an ACPI firmware node to get the GPIO information from
714 * @propname: Property name of the GPIO
715 * @index: index of GpioIo/GpioInt resource (starting from %0)
716 * @info: info pointer to fill in (optional)
717 *
718 * If @fwnode is an ACPI device object, call %acpi_get_gpiod_by_index() for it.
719 * Otherwise (ie. it is a data-only non-device object), use the property-based
720 * GPIO lookup to get to the GPIO resource with the relevant information and use
721 * that to obtain the GPIO descriptor to return.
722 */
723struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode,
724 const char *propname, int index,
725 struct acpi_gpio_info *info)
726{
727 struct acpi_gpio_lookup lookup;
728 struct acpi_device *adev;
729 int ret;
730
731 adev = to_acpi_device_node(fwnode);
732 if (adev)
733 return acpi_get_gpiod_by_index(adev, propname, index, info);
734
735 if (!is_acpi_data_node(fwnode))
736 return ERR_PTR(-ENODEV);
737
738 if (!propname)
739 return ERR_PTR(-EINVAL);
740
741 memset(&lookup, 0, sizeof(lookup));
742 lookup.index = index;
743
744 ret = acpi_gpio_property_lookup(fwnode, propname, index, &lookup);
745 if (ret)
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200746 return ERR_PTR(ret);
747
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200748 ret = acpi_gpio_resource_lookup(&lookup, info);
749 return ret ? ERR_PTR(ret) : lookup.desc;
Mika Westerberg664e3e52014-01-08 12:40:54 +0200750}
751
Mika Westerbergc884fbd2015-05-06 13:29:06 +0300752/**
753 * acpi_dev_gpio_irq_get() - Find GpioInt and translate it to Linux IRQ number
754 * @adev: pointer to a ACPI device to get IRQ from
755 * @index: index of GpioInt resource (starting from %0)
756 *
757 * If the device has one or more GpioInt resources, this function can be
758 * used to translate from the GPIO offset in the resource to the Linux IRQ
759 * number.
760 *
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300761 * The function is idempotent, though each time it runs it will configure GPIO
762 * pin direction according to the flags in GpioInt resource.
763 *
Thierry Redingdb05c7e2017-07-24 16:57:25 +0200764 * Return: Linux IRQ number (> %0) on success, negative errno on failure.
Mika Westerbergc884fbd2015-05-06 13:29:06 +0300765 */
766int acpi_dev_gpio_irq_get(struct acpi_device *adev, int index)
767{
768 int idx, i;
Christophe RICARD52044722015-12-23 23:25:34 +0100769 unsigned int irq_flags;
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300770 int ret;
Mika Westerbergc884fbd2015-05-06 13:29:06 +0300771
772 for (i = 0, idx = 0; idx <= index; i++) {
773 struct acpi_gpio_info info;
774 struct gpio_desc *desc;
775
776 desc = acpi_get_gpiod_by_index(adev, NULL, i, &info);
Christophe RICARD52044722015-12-23 23:25:34 +0100777
Hans de Goede6798d722017-03-13 23:04:21 +0100778 /* Ignore -EPROBE_DEFER, it only matters if idx matches */
779 if (IS_ERR(desc) && PTR_ERR(desc) != -EPROBE_DEFER)
780 return PTR_ERR(desc);
781
782 if (info.gpioint && idx++ == index) {
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300783 char label[32];
Hans de Goede6798d722017-03-13 23:04:21 +0100784 int irq;
785
786 if (IS_ERR(desc))
787 return PTR_ERR(desc);
788
789 irq = gpiod_to_irq(desc);
Christophe RICARD52044722015-12-23 23:25:34 +0100790 if (irq < 0)
791 return irq;
792
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300793 snprintf(label, sizeof(label), "GpioInt() %d", index);
794 ret = gpiod_configure_flags(desc, label, 0, info.flags);
795 if (ret < 0)
796 return ret;
797
Christophe RICARD52044722015-12-23 23:25:34 +0100798 irq_flags = acpi_dev_get_irq_type(info.triggering,
799 info.polarity);
800
801 /* Set type if specified and different than the current one */
802 if (irq_flags != IRQ_TYPE_NONE &&
803 irq_flags != irq_get_trigger_type(irq))
804 irq_set_irq_type(irq, irq_flags);
805
806 return irq;
807 }
808
Mika Westerbergc884fbd2015-05-06 13:29:06 +0300809 }
Hans de Goede6798d722017-03-13 23:04:21 +0100810 return -ENOENT;
Mika Westerbergc884fbd2015-05-06 13:29:06 +0300811}
812EXPORT_SYMBOL_GPL(acpi_dev_gpio_irq_get);
813
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200814static acpi_status
815acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
816 u32 bits, u64 *value, void *handler_context,
817 void *region_context)
818{
819 struct acpi_gpio_chip *achip = region_context;
820 struct gpio_chip *chip = achip->chip;
821 struct acpi_resource_gpio *agpio;
822 struct acpi_resource *ares;
823 int pin_index = (int)address;
824 acpi_status status;
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200825 int length;
826 int i;
827
828 status = acpi_buffer_to_resource(achip->conn_info.connection,
829 achip->conn_info.length, &ares);
830 if (ACPI_FAILURE(status))
831 return status;
832
833 if (WARN_ON(ares->type != ACPI_RESOURCE_TYPE_GPIO)) {
834 ACPI_FREE(ares);
835 return AE_BAD_PARAMETER;
836 }
837
838 agpio = &ares->data.gpio;
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200839
840 if (WARN_ON(agpio->io_restriction == ACPI_IO_RESTRICT_INPUT &&
841 function == ACPI_WRITE)) {
842 ACPI_FREE(ares);
843 return AE_BAD_PARAMETER;
844 }
845
846 length = min(agpio->pin_table_length, (u16)(pin_index + bits));
847 for (i = pin_index; i < length; ++i) {
Qipeng Zhaa4811622015-04-10 06:25:10 +0800848 int pin = agpio->pin_table[i];
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200849 struct acpi_gpio_connection *conn;
850 struct gpio_desc *desc;
851 bool found;
852
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200853 mutex_lock(&achip->conn_lock);
854
855 found = false;
856 list_for_each_entry(conn, &achip->conns, node) {
Alexandre Courbote46cf322014-08-19 10:06:08 -0700857 if (conn->pin == pin) {
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200858 found = true;
Alexandre Courbote46cf322014-08-19 10:06:08 -0700859 desc = conn->desc;
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200860 break;
861 }
862 }
Mika Westerbergc103a102015-10-30 12:02:05 +0200863
864 /*
865 * The same GPIO can be shared between operation region and
866 * event but only if the access here is ACPI_READ. In that
867 * case we "borrow" the event GPIO instead.
868 */
869 if (!found && agpio->sharable == ACPI_SHARED &&
870 function == ACPI_READ) {
871 struct acpi_gpio_event *event;
872
873 list_for_each_entry(event, &achip->events, node) {
874 if (event->pin == pin) {
875 desc = event->desc;
876 found = true;
877 break;
878 }
879 }
880 }
881
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200882 if (!found) {
Andy Shevchenko2eca25a2017-05-23 20:03:22 +0300883 enum gpiod_flags flags = acpi_gpio_to_gpiod_flags(agpio);
884 const char *label = "ACPI:OpRegion";
885 int err;
886
887 desc = gpiochip_request_own_desc(chip, pin, label);
Alexandre Courbote46cf322014-08-19 10:06:08 -0700888 if (IS_ERR(desc)) {
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200889 status = AE_ERROR;
890 mutex_unlock(&achip->conn_lock);
891 goto out;
892 }
893
Andy Shevchenko2eca25a2017-05-23 20:03:22 +0300894 err = gpiod_configure_flags(desc, label, 0, flags);
895 if (err < 0) {
896 status = AE_NOT_CONFIGURED;
897 gpiochip_free_own_desc(desc);
898 mutex_unlock(&achip->conn_lock);
899 goto out;
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200900 }
901
902 conn = kzalloc(sizeof(*conn), GFP_KERNEL);
903 if (!conn) {
904 status = AE_NO_MEMORY;
905 gpiochip_free_own_desc(desc);
906 mutex_unlock(&achip->conn_lock);
907 goto out;
908 }
909
Alexandre Courbote46cf322014-08-19 10:06:08 -0700910 conn->pin = pin;
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200911 conn->desc = desc;
912 list_add_tail(&conn->node, &achip->conns);
913 }
914
915 mutex_unlock(&achip->conn_lock);
916
917 if (function == ACPI_WRITE)
Aaron Ludc62b562014-05-20 17:07:38 +0800918 gpiod_set_raw_value_cansleep(desc,
919 !!((1 << i) & *value));
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200920 else
Aaron Ludc62b562014-05-20 17:07:38 +0800921 *value |= (u64)gpiod_get_raw_value_cansleep(desc) << i;
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200922 }
923
924out:
925 ACPI_FREE(ares);
926 return status;
927}
928
929static void acpi_gpiochip_request_regions(struct acpi_gpio_chip *achip)
930{
931 struct gpio_chip *chip = achip->chip;
Linus Walleij58383c782015-11-04 09:56:26 +0100932 acpi_handle handle = ACPI_HANDLE(chip->parent);
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200933 acpi_status status;
934
935 INIT_LIST_HEAD(&achip->conns);
936 mutex_init(&achip->conn_lock);
937 status = acpi_install_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
938 acpi_gpio_adr_space_handler,
939 NULL, achip);
940 if (ACPI_FAILURE(status))
Linus Walleij58383c782015-11-04 09:56:26 +0100941 dev_err(chip->parent,
942 "Failed to install GPIO OpRegion handler\n");
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200943}
944
945static void acpi_gpiochip_free_regions(struct acpi_gpio_chip *achip)
946{
947 struct gpio_chip *chip = achip->chip;
Linus Walleij58383c782015-11-04 09:56:26 +0100948 acpi_handle handle = ACPI_HANDLE(chip->parent);
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200949 struct acpi_gpio_connection *conn, *tmp;
950 acpi_status status;
951
952 status = acpi_remove_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
953 acpi_gpio_adr_space_handler);
954 if (ACPI_FAILURE(status)) {
Linus Walleij58383c782015-11-04 09:56:26 +0100955 dev_err(chip->parent,
956 "Failed to remove GPIO OpRegion handler\n");
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200957 return;
958 }
959
960 list_for_each_entry_safe_reverse(conn, tmp, &achip->conns, node) {
961 gpiochip_free_own_desc(conn->desc);
962 list_del(&conn->node);
963 kfree(conn);
964 }
965}
966
Wei Yongjun550a9532016-10-29 16:13:12 +0000967static struct gpio_desc *acpi_gpiochip_parse_own_gpio(
968 struct acpi_gpio_chip *achip, struct fwnode_handle *fwnode,
969 const char **name, unsigned int *lflags, unsigned int *dflags)
Mika Westerbergc80f1ba2016-10-21 17:21:30 +0300970{
971 struct gpio_chip *chip = achip->chip;
972 struct gpio_desc *desc;
973 u32 gpios[2];
974 int ret;
975
Arnd Bergmannc82064f2016-11-08 14:40:06 +0100976 *lflags = 0;
977 *dflags = 0;
978 *name = NULL;
979
Mika Westerbergc80f1ba2016-10-21 17:21:30 +0300980 ret = fwnode_property_read_u32_array(fwnode, "gpios", gpios,
981 ARRAY_SIZE(gpios));
982 if (ret < 0)
983 return ERR_PTR(ret);
984
Mika Westerberg03c47492017-11-27 16:54:42 +0300985 desc = gpiochip_get_desc(chip, gpios[0]);
Mika Westerbergc80f1ba2016-10-21 17:21:30 +0300986 if (IS_ERR(desc))
987 return desc;
988
Mika Westerbergc80f1ba2016-10-21 17:21:30 +0300989 if (gpios[1])
990 *lflags |= GPIO_ACTIVE_LOW;
991
992 if (fwnode_property_present(fwnode, "input"))
993 *dflags |= GPIOD_IN;
994 else if (fwnode_property_present(fwnode, "output-low"))
995 *dflags |= GPIOD_OUT_LOW;
996 else if (fwnode_property_present(fwnode, "output-high"))
997 *dflags |= GPIOD_OUT_HIGH;
998 else
999 return ERR_PTR(-EINVAL);
1000
1001 fwnode_property_read_string(fwnode, "line-name", name);
1002
1003 return desc;
1004}
1005
1006static void acpi_gpiochip_scan_gpios(struct acpi_gpio_chip *achip)
1007{
1008 struct gpio_chip *chip = achip->chip;
1009 struct fwnode_handle *fwnode;
1010
1011 device_for_each_child_node(chip->parent, fwnode) {
1012 unsigned int lflags, dflags;
1013 struct gpio_desc *desc;
1014 const char *name;
1015 int ret;
1016
1017 if (!fwnode_property_present(fwnode, "gpio-hog"))
1018 continue;
1019
1020 desc = acpi_gpiochip_parse_own_gpio(achip, fwnode, &name,
1021 &lflags, &dflags);
1022 if (IS_ERR(desc))
1023 continue;
1024
1025 ret = gpiod_hog(desc, name, lflags, dflags);
1026 if (ret) {
1027 dev_err(chip->parent, "Failed to hog GPIO\n");
Wei Yongjun1b6998c2016-10-29 16:11:57 +00001028 fwnode_handle_put(fwnode);
Mika Westerbergc80f1ba2016-10-21 17:21:30 +03001029 return;
1030 }
1031 }
1032}
1033
Mika Westerberg664e3e52014-01-08 12:40:54 +02001034void acpi_gpiochip_add(struct gpio_chip *chip)
1035{
Mika Westerbergaa92b6f2014-03-10 14:54:51 +02001036 struct acpi_gpio_chip *acpi_gpio;
1037 acpi_handle handle;
1038 acpi_status status;
1039
Linus Walleij58383c782015-11-04 09:56:26 +01001040 if (!chip || !chip->parent)
Mika Westerberge9595f82014-03-31 15:16:49 +03001041 return;
1042
Linus Walleij58383c782015-11-04 09:56:26 +01001043 handle = ACPI_HANDLE(chip->parent);
Mika Westerbergaa92b6f2014-03-10 14:54:51 +02001044 if (!handle)
1045 return;
1046
1047 acpi_gpio = kzalloc(sizeof(*acpi_gpio), GFP_KERNEL);
1048 if (!acpi_gpio) {
Linus Walleij58383c782015-11-04 09:56:26 +01001049 dev_err(chip->parent,
Mika Westerbergaa92b6f2014-03-10 14:54:51 +02001050 "Failed to allocate memory for ACPI GPIO chip\n");
1051 return;
1052 }
1053
1054 acpi_gpio->chip = chip;
Mika Westerbergc103a102015-10-30 12:02:05 +02001055 INIT_LIST_HEAD(&acpi_gpio->events);
Hans de Goede78d3a922018-08-14 16:07:03 +02001056 INIT_LIST_HEAD(&acpi_gpio->deferred_req_irqs_list_entry);
Mika Westerbergaa92b6f2014-03-10 14:54:51 +02001057
1058 status = acpi_attach_data(handle, acpi_gpio_chip_dh, acpi_gpio);
1059 if (ACPI_FAILURE(status)) {
Linus Walleij58383c782015-11-04 09:56:26 +01001060 dev_err(chip->parent, "Failed to attach ACPI GPIO chip\n");
Mika Westerbergaa92b6f2014-03-10 14:54:51 +02001061 kfree(acpi_gpio);
1062 return;
1063 }
1064
Mika Westerberg4035cc12016-10-21 17:21:32 +03001065 if (!chip->names)
Christophe Leroy82270332017-12-15 15:02:33 +01001066 devprop_gpiochip_set_names(chip, dev_fwnode(chip->parent));
Mika Westerberg4035cc12016-10-21 17:21:32 +03001067
Mika Westerberg473ed7b2014-03-14 17:58:07 +02001068 acpi_gpiochip_request_regions(acpi_gpio);
Mika Westerbergc80f1ba2016-10-21 17:21:30 +03001069 acpi_gpiochip_scan_gpios(acpi_gpio);
Rui Zhang3f86a632016-06-15 08:47:51 +02001070 acpi_walk_dep_device_list(handle);
Mika Westerberg664e3e52014-01-08 12:40:54 +02001071}
1072
1073void acpi_gpiochip_remove(struct gpio_chip *chip)
1074{
Mika Westerbergaa92b6f2014-03-10 14:54:51 +02001075 struct acpi_gpio_chip *acpi_gpio;
1076 acpi_handle handle;
1077 acpi_status status;
1078
Linus Walleij58383c782015-11-04 09:56:26 +01001079 if (!chip || !chip->parent)
Mika Westerberge9595f82014-03-31 15:16:49 +03001080 return;
1081
Linus Walleij58383c782015-11-04 09:56:26 +01001082 handle = ACPI_HANDLE(chip->parent);
Mika Westerbergaa92b6f2014-03-10 14:54:51 +02001083 if (!handle)
1084 return;
1085
1086 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
1087 if (ACPI_FAILURE(status)) {
Linus Walleij58383c782015-11-04 09:56:26 +01001088 dev_warn(chip->parent, "Failed to retrieve ACPI GPIO chip\n");
Mika Westerbergaa92b6f2014-03-10 14:54:51 +02001089 return;
1090 }
1091
Mika Westerberg473ed7b2014-03-14 17:58:07 +02001092 acpi_gpiochip_free_regions(acpi_gpio);
Mika Westerbergaa92b6f2014-03-10 14:54:51 +02001093
1094 acpi_detach_data(handle, acpi_gpio_chip_dh);
1095 kfree(acpi_gpio);
Mika Westerberg664e3e52014-01-08 12:40:54 +02001096}
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001097
Mika Westerberg6f7194a2016-10-21 17:21:29 +03001098static int acpi_gpio_package_count(const union acpi_object *obj)
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001099{
1100 const union acpi_object *element = obj->package.elements;
1101 const union acpi_object *end = element + obj->package.count;
1102 unsigned int count = 0;
1103
1104 while (element < end) {
Mika Westerberg6f7194a2016-10-21 17:21:29 +03001105 switch (element->type) {
1106 case ACPI_TYPE_LOCAL_REFERENCE:
1107 element += 3;
1108 /* Fallthrough */
1109 case ACPI_TYPE_INTEGER:
1110 element++;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001111 count++;
Mika Westerberg6f7194a2016-10-21 17:21:29 +03001112 break;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001113
Mika Westerberg6f7194a2016-10-21 17:21:29 +03001114 default:
1115 return -EPROTO;
1116 }
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001117 }
Mika Westerberg6f7194a2016-10-21 17:21:29 +03001118
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001119 return count;
1120}
1121
1122static int acpi_find_gpio_count(struct acpi_resource *ares, void *data)
1123{
1124 unsigned int *count = data;
1125
1126 if (ares->type == ACPI_RESOURCE_TYPE_GPIO)
1127 *count += ares->data.gpio.pin_table_length;
1128
1129 return 1;
1130}
1131
1132/**
1133 * acpi_gpio_count - return the number of GPIOs associated with a
1134 * device / function or -ENOENT if no GPIO has been
1135 * assigned to the requested function.
1136 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1137 * @con_id: function within the GPIO consumer
1138 */
1139int acpi_gpio_count(struct device *dev, const char *con_id)
1140{
1141 struct acpi_device *adev = ACPI_COMPANION(dev);
1142 const union acpi_object *obj;
1143 const struct acpi_gpio_mapping *gm;
1144 int count = -ENOENT;
1145 int ret;
1146 char propname[32];
1147 unsigned int i;
1148
1149 /* Try first from _DSD */
1150 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Andy Shevchenko9e665042017-05-23 20:03:17 +03001151 if (con_id)
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001152 snprintf(propname, sizeof(propname), "%s-%s",
1153 con_id, gpio_suffixes[i]);
1154 else
1155 snprintf(propname, sizeof(propname), "%s",
1156 gpio_suffixes[i]);
1157
1158 ret = acpi_dev_get_property(adev, propname, ACPI_TYPE_ANY,
1159 &obj);
1160 if (ret == 0) {
1161 if (obj->type == ACPI_TYPE_LOCAL_REFERENCE)
1162 count = 1;
1163 else if (obj->type == ACPI_TYPE_PACKAGE)
1164 count = acpi_gpio_package_count(obj);
1165 } else if (adev->driver_gpios) {
1166 for (gm = adev->driver_gpios; gm->name; gm++)
1167 if (strcmp(propname, gm->name) == 0) {
1168 count = gm->size;
1169 break;
1170 }
1171 }
Andy Shevchenko4ed55012017-02-20 18:15:46 +02001172 if (count > 0)
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001173 break;
1174 }
1175
1176 /* Then from plain _CRS GPIOs */
1177 if (count < 0) {
1178 struct list_head resource_list;
1179 unsigned int crs_count = 0;
1180
Andy Shevchenko6fe9da42017-05-23 20:03:20 +03001181 if (!acpi_can_fallback_to_crs(adev, con_id))
1182 return count;
1183
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001184 INIT_LIST_HEAD(&resource_list);
1185 acpi_dev_get_resources(adev, &resource_list,
1186 acpi_find_gpio_count, &crs_count);
1187 acpi_dev_free_resource_list(&resource_list);
1188 if (crs_count > 0)
1189 count = crs_count;
1190 }
Andy Shevchenko4ed55012017-02-20 18:15:46 +02001191 return count ? count : -ENOENT;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001192}
Dmitry Torokhov10cf4892015-11-11 11:45:30 -08001193
Dmitry Torokhov10cf4892015-11-11 11:45:30 -08001194bool acpi_can_fallback_to_crs(struct acpi_device *adev, const char *con_id)
1195{
Dmitry Torokhov10cf4892015-11-11 11:45:30 -08001196 /* Never allow fallback if the device has properties */
Mika Westerberg5f5e4892018-09-27 16:57:05 -05001197 if (acpi_dev_has_props(adev) || adev->driver_gpios)
Dmitry Torokhov10cf4892015-11-11 11:45:30 -08001198 return false;
1199
Andy Shevchenkof10e4bf2017-05-23 20:03:19 +03001200 return con_id == NULL;
Dmitry Torokhov10cf4892015-11-11 11:45:30 -08001201}
Benjamin Tissoiresca876c72018-07-12 17:25:06 +02001202
Hans de Goede78d3a922018-08-14 16:07:03 +02001203/* Run deferred acpi_gpiochip_request_interrupts() */
1204static int acpi_gpio_handle_deferred_request_interrupts(void)
Benjamin Tissoiresca876c72018-07-12 17:25:06 +02001205{
Hans de Goede78d3a922018-08-14 16:07:03 +02001206 struct acpi_gpio_chip *acpi_gpio, *tmp;
Benjamin Tissoiresca876c72018-07-12 17:25:06 +02001207
Hans de Goede78d3a922018-08-14 16:07:03 +02001208 mutex_lock(&acpi_gpio_deferred_req_irqs_lock);
1209 list_for_each_entry_safe(acpi_gpio, tmp,
1210 &acpi_gpio_deferred_req_irqs_list,
1211 deferred_req_irqs_list_entry) {
1212 acpi_handle handle;
1213
1214 handle = ACPI_HANDLE(acpi_gpio->chip->parent);
1215 acpi_walk_resources(handle, "_AEI",
1216 acpi_gpiochip_request_interrupt, acpi_gpio);
1217
1218 list_del_init(&acpi_gpio->deferred_req_irqs_list_entry);
Benjamin Tissoiresca876c72018-07-12 17:25:06 +02001219 }
Hans de Goede78d3a922018-08-14 16:07:03 +02001220
1221 acpi_gpio_deferred_req_irqs_done = true;
1222 mutex_unlock(&acpi_gpio_deferred_req_irqs_lock);
Benjamin Tissoiresca876c72018-07-12 17:25:06 +02001223
1224 return 0;
1225}
1226/* We must use _sync so that this runs after the first deferred_probe run */
Hans de Goede78d3a922018-08-14 16:07:03 +02001227late_initcall_sync(acpi_gpio_handle_deferred_request_interrupts);