blob: addd9fecc198360d03a4cfb420e1e2d6ecf90a67 [file] [log] [blame]
Mathias Nymane29482e2012-11-30 12:37:36 +01001/*
2 * ACPI helpers for GPIO API
3 *
4 * Copyright (C) 2012, Intel Corporation
5 * Authors: Mathias Nyman <mathias.nyman@linux.intel.com>
6 * Mika Westerberg <mika.westerberg@linux.intel.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/errno.h>
Mika Westerberg354567e2014-11-03 13:01:32 +020014#include <linux/gpio.h>
Mika Westerberg936e15d2013-10-10 11:01:08 +030015#include <linux/gpio/consumer.h>
Mika Westerberg5ccff852014-01-08 12:40:56 +020016#include <linux/gpio/driver.h>
Linus Walleij031ba282016-10-03 10:40:03 +020017#include <linux/gpio/machine.h>
Mathias Nymane29482e2012-11-30 12:37:36 +010018#include <linux/export.h>
Mathias Nymane29482e2012-11-30 12:37:36 +010019#include <linux/acpi.h>
Mathias Nyman0d1c28a2013-01-28 16:23:10 +020020#include <linux/interrupt.h>
Mika Westerberg473ed7b2014-03-14 17:58:07 +020021#include <linux/mutex.h>
Mika Westerberg354567e2014-11-03 13:01:32 +020022#include <linux/pinctrl/pinctrl.h>
Mathias Nymane29482e2012-11-30 12:37:36 +010023
Mika Westerberg5ccff852014-01-08 12:40:56 +020024#include "gpiolib.h"
25
Mika Westerberg4b01a142014-03-10 14:54:52 +020026struct acpi_gpio_event {
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +020027 struct list_head node;
Benjamin Tissoiresca876c72018-07-12 17:25:06 +020028 struct list_head initial_sync_list;
Mika Westerberg4b01a142014-03-10 14:54:52 +020029 acpi_handle handle;
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +020030 unsigned int pin;
31 unsigned int irq;
Alexandre Courbote46cf322014-08-19 10:06:08 -070032 struct gpio_desc *desc;
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +020033};
34
Mika Westerberg473ed7b2014-03-14 17:58:07 +020035struct acpi_gpio_connection {
36 struct list_head node;
Alexandre Courbote46cf322014-08-19 10:06:08 -070037 unsigned int pin;
Mika Westerberg473ed7b2014-03-14 17:58:07 +020038 struct gpio_desc *desc;
39};
40
Mika Westerbergaa92b6f2014-03-10 14:54:51 +020041struct acpi_gpio_chip {
Mika Westerberg473ed7b2014-03-14 17:58:07 +020042 /*
43 * ACPICA requires that the first field of the context parameter
44 * passed to acpi_install_address_space_handler() is large enough
45 * to hold struct acpi_connection_info.
46 */
47 struct acpi_connection_info conn_info;
48 struct list_head conns;
49 struct mutex conn_lock;
Mika Westerbergaa92b6f2014-03-10 14:54:51 +020050 struct gpio_chip *chip;
Mika Westerberg4b01a142014-03-10 14:54:52 +020051 struct list_head events;
Mika Westerbergaa92b6f2014-03-10 14:54:51 +020052};
53
Benjamin Tissoiresca876c72018-07-12 17:25:06 +020054static LIST_HEAD(acpi_gpio_initial_sync_list);
55static DEFINE_MUTEX(acpi_gpio_initial_sync_list_lock);
56
Mathias Nymane29482e2012-11-30 12:37:36 +010057static int acpi_gpiochip_find(struct gpio_chip *gc, void *data)
58{
Linus Walleij58383c782015-11-04 09:56:26 +010059 if (!gc->parent)
Mathias Nymane29482e2012-11-30 12:37:36 +010060 return false;
61
Linus Walleij58383c782015-11-04 09:56:26 +010062 return ACPI_HANDLE(gc->parent) == data;
Mathias Nymane29482e2012-11-30 12:37:36 +010063}
64
65/**
Mika Westerberg936e15d2013-10-10 11:01:08 +030066 * acpi_get_gpiod() - Translate ACPI GPIO pin to GPIO descriptor usable with GPIO API
Mathias Nymane29482e2012-11-30 12:37:36 +010067 * @path: ACPI GPIO controller full path name, (e.g. "\\_SB.GPO1")
68 * @pin: ACPI GPIO pin number (0-based, controller-relative)
69 *
Mika Westerbergf35bbf612015-06-10 16:05:05 +030070 * Return: GPIO descriptor to use with Linux generic GPIO API, or ERR_PTR
71 * error value. Specifically returns %-EPROBE_DEFER if the referenced GPIO
72 * controller does not have gpiochip registered at the moment. This is to
73 * support probe deferral.
Mathias Nymane29482e2012-11-30 12:37:36 +010074 */
Mika Westerberg936e15d2013-10-10 11:01:08 +030075static struct gpio_desc *acpi_get_gpiod(char *path, int pin)
Mathias Nymane29482e2012-11-30 12:37:36 +010076{
77 struct gpio_chip *chip;
78 acpi_handle handle;
79 acpi_status status;
80
81 status = acpi_get_handle(NULL, path, &handle);
82 if (ACPI_FAILURE(status))
Mika Westerberg936e15d2013-10-10 11:01:08 +030083 return ERR_PTR(-ENODEV);
Mathias Nymane29482e2012-11-30 12:37:36 +010084
85 chip = gpiochip_find(handle, acpi_gpiochip_find);
86 if (!chip)
Mika Westerbergf35bbf612015-06-10 16:05:05 +030087 return ERR_PTR(-EPROBE_DEFER);
Mathias Nymane29482e2012-11-30 12:37:36 +010088
Mika Westerberg03c47492017-11-27 16:54:42 +030089 return gpiochip_get_desc(chip, pin);
Mathias Nymane29482e2012-11-30 12:37:36 +010090}
Mathias Nyman0d1c28a2013-01-28 16:23:10 +020091
Benjamin Tissoiresca876c72018-07-12 17:25:06 +020092static void acpi_gpio_add_to_initial_sync_list(struct acpi_gpio_event *event)
93{
94 mutex_lock(&acpi_gpio_initial_sync_list_lock);
95 list_add(&event->initial_sync_list, &acpi_gpio_initial_sync_list);
96 mutex_unlock(&acpi_gpio_initial_sync_list_lock);
97}
98
99static void acpi_gpio_del_from_initial_sync_list(struct acpi_gpio_event *event)
100{
101 mutex_lock(&acpi_gpio_initial_sync_list_lock);
102 if (!list_empty(&event->initial_sync_list))
103 list_del_init(&event->initial_sync_list);
104 mutex_unlock(&acpi_gpio_initial_sync_list_lock);
105}
106
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200107static irqreturn_t acpi_gpio_irq_handler(int irq, void *data)
108{
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200109 struct acpi_gpio_event *event = data;
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200110
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200111 acpi_evaluate_object(event->handle, NULL, NULL, NULL);
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200112
113 return IRQ_HANDLED;
114}
115
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +0200116static irqreturn_t acpi_gpio_irq_handler_evt(int irq, void *data)
117{
Mika Westerberg4b01a142014-03-10 14:54:52 +0200118 struct acpi_gpio_event *event = data;
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +0200119
Mika Westerberg4b01a142014-03-10 14:54:52 +0200120 acpi_execute_simple_method(event->handle, NULL, event->pin);
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +0200121
122 return IRQ_HANDLED;
123}
124
Mika Westerbergaa92b6f2014-03-10 14:54:51 +0200125static void acpi_gpio_chip_dh(acpi_handle handle, void *data)
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +0200126{
127 /* The address of this function is used as a key. */
128}
129
Andy Shevchenko25e3ef82017-05-23 20:03:24 +0300130bool acpi_gpio_get_irq_resource(struct acpi_resource *ares,
131 struct acpi_resource_gpio **agpio)
132{
133 struct acpi_resource_gpio *gpio;
134
135 if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
136 return false;
137
138 gpio = &ares->data.gpio;
139 if (gpio->connection_type != ACPI_RESOURCE_GPIO_TYPE_INT)
140 return false;
141
142 *agpio = gpio;
143 return true;
144}
145EXPORT_SYMBOL_GPL(acpi_gpio_get_irq_resource);
146
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200147static acpi_status acpi_gpiochip_request_interrupt(struct acpi_resource *ares,
148 void *context)
149{
150 struct acpi_gpio_chip *acpi_gpio = context;
151 struct gpio_chip *chip = acpi_gpio->chip;
152 struct acpi_resource_gpio *agpio;
153 acpi_handle handle, evt_handle;
154 struct acpi_gpio_event *event;
155 irq_handler_t handler = NULL;
156 struct gpio_desc *desc;
157 unsigned long irqflags;
Benjamin Tissoiresca876c72018-07-12 17:25:06 +0200158 int ret, pin, irq, value;
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200159
Andy Shevchenko25e3ef82017-05-23 20:03:24 +0300160 if (!acpi_gpio_get_irq_resource(ares, &agpio))
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200161 return AE_OK;
162
Linus Walleij58383c782015-11-04 09:56:26 +0100163 handle = ACPI_HANDLE(chip->parent);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200164 pin = agpio->pin_table[0];
165
166 if (pin <= 255) {
167 char ev_name[5];
Arnd Bergmanne40a3ae2017-09-06 17:47:45 +0200168 sprintf(ev_name, "_%c%02hhX",
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200169 agpio->triggering == ACPI_EDGE_SENSITIVE ? 'E' : 'L',
170 pin);
171 if (ACPI_SUCCESS(acpi_get_handle(handle, ev_name, &evt_handle)))
172 handler = acpi_gpio_irq_handler;
173 }
174 if (!handler) {
175 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EVT", &evt_handle)))
176 handler = acpi_gpio_irq_handler_evt;
177 }
178 if (!handler)
Hans de Goedec06632e2017-06-23 09:26:13 +0200179 return AE_OK;
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200180
Alexandre Courbotabdc08a2014-08-19 10:06:09 -0700181 desc = gpiochip_request_own_desc(chip, pin, "ACPI:Event");
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200182 if (IS_ERR(desc)) {
Linus Walleij58383c782015-11-04 09:56:26 +0100183 dev_err(chip->parent, "Failed to request GPIO\n");
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200184 return AE_ERROR;
185 }
186
187 gpiod_direction_input(desc);
188
Benjamin Tissoiresca876c72018-07-12 17:25:06 +0200189 value = gpiod_get_value(desc);
190
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900191 ret = gpiochip_lock_as_irq(chip, pin);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200192 if (ret) {
Linus Walleij58383c782015-11-04 09:56:26 +0100193 dev_err(chip->parent, "Failed to lock GPIO as interrupt\n");
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200194 goto fail_free_desc;
195 }
196
197 irq = gpiod_to_irq(desc);
198 if (irq < 0) {
Linus Walleij58383c782015-11-04 09:56:26 +0100199 dev_err(chip->parent, "Failed to translate GPIO to IRQ\n");
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200200 goto fail_unlock_irq;
201 }
202
203 irqflags = IRQF_ONESHOT;
204 if (agpio->triggering == ACPI_LEVEL_SENSITIVE) {
205 if (agpio->polarity == ACPI_ACTIVE_HIGH)
206 irqflags |= IRQF_TRIGGER_HIGH;
207 else
208 irqflags |= IRQF_TRIGGER_LOW;
209 } else {
210 switch (agpio->polarity) {
211 case ACPI_ACTIVE_HIGH:
212 irqflags |= IRQF_TRIGGER_RISING;
213 break;
214 case ACPI_ACTIVE_LOW:
215 irqflags |= IRQF_TRIGGER_FALLING;
216 break;
217 default:
218 irqflags |= IRQF_TRIGGER_RISING |
219 IRQF_TRIGGER_FALLING;
220 break;
221 }
222 }
223
224 event = kzalloc(sizeof(*event), GFP_KERNEL);
225 if (!event)
226 goto fail_unlock_irq;
227
228 event->handle = evt_handle;
229 event->irq = irq;
230 event->pin = pin;
Alexandre Courbote46cf322014-08-19 10:06:08 -0700231 event->desc = desc;
Benjamin Tissoiresca876c72018-07-12 17:25:06 +0200232 INIT_LIST_HEAD(&event->initial_sync_list);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200233
234 ret = request_threaded_irq(event->irq, NULL, handler, irqflags,
235 "ACPI:Event", event);
236 if (ret) {
Linus Walleij58383c782015-11-04 09:56:26 +0100237 dev_err(chip->parent,
238 "Failed to setup interrupt handler for %d\n",
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200239 event->irq);
240 goto fail_free_event;
241 }
242
Hans de Goede8a146fb2017-03-24 11:08:47 +0100243 if (agpio->wake_capable == ACPI_WAKE_CAPABLE)
244 enable_irq_wake(irq);
245
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200246 list_add_tail(&event->node, &acpi_gpio->events);
Benjamin Tissoiresca876c72018-07-12 17:25:06 +0200247
248 /*
249 * Make sure we trigger the initial state of the IRQ when using RISING
250 * or FALLING. Note we run the handlers on late_init, the AML code
251 * may refer to OperationRegions from other (builtin) drivers which
252 * may be probed after us.
253 */
254 if (handler == acpi_gpio_irq_handler &&
255 (((irqflags & IRQF_TRIGGER_RISING) && value == 1) ||
256 ((irqflags & IRQF_TRIGGER_FALLING) && value == 0)))
257 acpi_gpio_add_to_initial_sync_list(event);
258
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200259 return AE_OK;
260
261fail_free_event:
262 kfree(event);
263fail_unlock_irq:
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900264 gpiochip_unlock_as_irq(chip, pin);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200265fail_free_desc:
266 gpiochip_free_own_desc(desc);
267
268 return AE_ERROR;
269}
270
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200271/**
272 * acpi_gpiochip_request_interrupts() - Register isr for gpio chip ACPI events
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300273 * @chip: GPIO chip
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200274 *
275 * ACPI5 platforms can use GPIO signaled ACPI events. These GPIO interrupts are
276 * handled by ACPI event methods which need to be called from the GPIO
277 * chip's interrupt handler. acpi_gpiochip_request_interrupts finds out which
278 * gpio pins have acpi event methods and assigns interrupt handlers that calls
279 * the acpi event methods for those pins.
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200280 */
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300281void acpi_gpiochip_request_interrupts(struct gpio_chip *chip)
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200282{
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300283 struct acpi_gpio_chip *acpi_gpio;
284 acpi_handle handle;
285 acpi_status status;
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200286
Linus Walleij58383c782015-11-04 09:56:26 +0100287 if (!chip->parent || !chip->to_irq)
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300288 return;
289
Linus Walleij58383c782015-11-04 09:56:26 +0100290 handle = ACPI_HANDLE(chip->parent);
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300291 if (!handle)
292 return;
293
294 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
295 if (ACPI_FAILURE(status))
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200296 return;
297
Rafael J. Wysocki1ecb0162015-03-10 23:10:01 +0100298 acpi_walk_resources(handle, "_AEI",
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200299 acpi_gpiochip_request_interrupt, acpi_gpio);
Mathias Nyman0d1c28a2013-01-28 16:23:10 +0200300}
Hanjun Guo2b528ff2015-06-10 17:12:07 +0800301EXPORT_SYMBOL_GPL(acpi_gpiochip_request_interrupts);
Rafael J. Wysocki7fc7acb2013-04-09 15:57:25 +0200302
Mika Westerberg70b53412013-10-10 11:01:07 +0300303/**
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200304 * acpi_gpiochip_free_interrupts() - Free GPIO ACPI event interrupts.
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300305 * @chip: GPIO chip
Mika Westerberg70b53412013-10-10 11:01:07 +0300306 *
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200307 * Free interrupts associated with GPIO ACPI event method for the given
308 * GPIO chip.
Mika Westerberg70b53412013-10-10 11:01:07 +0300309 */
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300310void acpi_gpiochip_free_interrupts(struct gpio_chip *chip)
Mika Westerberg70b53412013-10-10 11:01:07 +0300311{
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300312 struct acpi_gpio_chip *acpi_gpio;
Mika Westerberg4b01a142014-03-10 14:54:52 +0200313 struct acpi_gpio_event *event, *ep;
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300314 acpi_handle handle;
315 acpi_status status;
Mika Westerberg70b53412013-10-10 11:01:07 +0300316
Linus Walleij58383c782015-11-04 09:56:26 +0100317 if (!chip->parent || !chip->to_irq)
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300318 return;
319
Linus Walleij58383c782015-11-04 09:56:26 +0100320 handle = ACPI_HANDLE(chip->parent);
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300321 if (!handle)
322 return;
323
324 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
325 if (ACPI_FAILURE(status))
Mika Westerberg70b53412013-10-10 11:01:07 +0300326 return;
327
Mika Westerberg4b01a142014-03-10 14:54:52 +0200328 list_for_each_entry_safe_reverse(event, ep, &acpi_gpio->events, node) {
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200329 struct gpio_desc *desc;
330
Benjamin Tissoiresca876c72018-07-12 17:25:06 +0200331 acpi_gpio_del_from_initial_sync_list(event);
332
Hans de Goede8a146fb2017-03-24 11:08:47 +0100333 if (irqd_is_wakeup_set(irq_get_irq_data(event->irq)))
334 disable_irq_wake(event->irq);
335
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200336 free_irq(event->irq, event);
Alexandre Courbote46cf322014-08-19 10:06:08 -0700337 desc = event->desc;
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200338 if (WARN_ON(IS_ERR(desc)))
339 continue;
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900340 gpiochip_unlock_as_irq(chip, event->pin);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200341 gpiochip_free_own_desc(desc);
Mika Westerberg4b01a142014-03-10 14:54:52 +0200342 list_del(&event->node);
343 kfree(event);
Mika Westerberg70b53412013-10-10 11:01:07 +0300344 }
Mika Westerberg70b53412013-10-10 11:01:07 +0300345}
Hanjun Guo2b528ff2015-06-10 17:12:07 +0800346EXPORT_SYMBOL_GPL(acpi_gpiochip_free_interrupts);
Mika Westerberg70b53412013-10-10 11:01:07 +0300347
Rafael J. Wysockif028d522014-11-03 23:39:41 +0100348int acpi_dev_add_driver_gpios(struct acpi_device *adev,
349 const struct acpi_gpio_mapping *gpios)
350{
351 if (adev && gpios) {
352 adev->driver_gpios = gpios;
353 return 0;
354 }
355 return -EINVAL;
356}
357EXPORT_SYMBOL_GPL(acpi_dev_add_driver_gpios);
358
Andy Shevchenko85c73d52017-03-02 15:48:05 +0200359static void devm_acpi_dev_release_driver_gpios(struct device *dev, void *res)
360{
361 acpi_dev_remove_driver_gpios(ACPI_COMPANION(dev));
362}
363
364int devm_acpi_dev_add_driver_gpios(struct device *dev,
365 const struct acpi_gpio_mapping *gpios)
366{
367 void *res;
368 int ret;
369
370 res = devres_alloc(devm_acpi_dev_release_driver_gpios, 0, GFP_KERNEL);
371 if (!res)
372 return -ENOMEM;
373
374 ret = acpi_dev_add_driver_gpios(ACPI_COMPANION(dev), gpios);
375 if (ret) {
376 devres_free(res);
377 return ret;
378 }
379 devres_add(dev, res);
380 return 0;
381}
382EXPORT_SYMBOL_GPL(devm_acpi_dev_add_driver_gpios);
383
384void devm_acpi_dev_remove_driver_gpios(struct device *dev)
385{
386 WARN_ON(devres_release(dev, devm_acpi_dev_release_driver_gpios, NULL, NULL));
387}
388EXPORT_SYMBOL_GPL(devm_acpi_dev_remove_driver_gpios);
389
Rafael J. Wysockif028d522014-11-03 23:39:41 +0100390static bool acpi_get_driver_gpio_data(struct acpi_device *adev,
391 const char *name, int index,
Andy Shevchenkoce0929d2017-11-10 15:40:32 +0200392 struct acpi_reference_args *args,
393 unsigned int *quirks)
Rafael J. Wysockif028d522014-11-03 23:39:41 +0100394{
395 const struct acpi_gpio_mapping *gm;
396
397 if (!adev->driver_gpios)
398 return false;
399
400 for (gm = adev->driver_gpios; gm->name; gm++)
401 if (!strcmp(name, gm->name) && gm->data && index < gm->size) {
402 const struct acpi_gpio_params *par = gm->data + index;
403
404 args->adev = adev;
405 args->args[0] = par->crs_entry_index;
406 args->args[1] = par->line_index;
407 args->args[2] = par->active_low;
408 args->nargs = 3;
Andy Shevchenkoce0929d2017-11-10 15:40:32 +0200409
410 *quirks = gm->quirks;
Rafael J. Wysockif028d522014-11-03 23:39:41 +0100411 return true;
412 }
413
414 return false;
415}
416
Andy Shevchenko2eca25a2017-05-23 20:03:22 +0300417static enum gpiod_flags
418acpi_gpio_to_gpiod_flags(const struct acpi_resource_gpio *agpio)
419{
420 bool pull_up = agpio->pin_config == ACPI_PIN_CONFIG_PULLUP;
421
422 switch (agpio->io_restriction) {
423 case ACPI_IO_RESTRICT_INPUT:
424 return GPIOD_IN;
425 case ACPI_IO_RESTRICT_OUTPUT:
426 /*
427 * ACPI GPIO resources don't contain an initial value for the
428 * GPIO. Therefore we deduce that value from the pull field
429 * instead. If the pin is pulled up we assume default to be
430 * high, otherwise low.
431 */
432 return pull_up ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
433 default:
434 /*
435 * Assume that the BIOS has configured the direction and pull
436 * accordingly.
437 */
438 return GPIOD_ASIS;
439 }
440}
441
Andy Shevchenko5c34b6c2017-11-10 15:40:31 +0200442static int
443__acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, enum gpiod_flags update)
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300444{
445 int ret = 0;
446
447 /*
448 * Check if the BIOS has IoRestriction with explicitly set direction
449 * and update @flags accordingly. Otherwise use whatever caller asked
450 * for.
451 */
452 if (update & GPIOD_FLAGS_BIT_DIR_SET) {
453 enum gpiod_flags diff = *flags ^ update;
454
455 /*
456 * Check if caller supplied incompatible GPIO initialization
457 * flags.
458 *
459 * Return %-EINVAL to notify that firmware has different
460 * settings and we are going to use them.
461 */
462 if (((*flags & GPIOD_FLAGS_BIT_DIR_SET) && (diff & GPIOD_FLAGS_BIT_DIR_OUT)) ||
463 ((*flags & GPIOD_FLAGS_BIT_DIR_OUT) && (diff & GPIOD_FLAGS_BIT_DIR_VAL)))
464 ret = -EINVAL;
465 *flags = update;
466 }
467 return ret;
468}
469
Andy Shevchenko5c34b6c2017-11-10 15:40:31 +0200470int
471acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, struct acpi_gpio_info *info)
472{
473 struct device *dev = &info->adev->dev;
Andy Shevchenko1b2ca322017-11-10 15:40:33 +0200474 enum gpiod_flags old = *flags;
Andy Shevchenko5c34b6c2017-11-10 15:40:31 +0200475 int ret;
476
Andy Shevchenko1b2ca322017-11-10 15:40:33 +0200477 ret = __acpi_gpio_update_gpiod_flags(&old, info->flags);
478 if (info->quirks & ACPI_GPIO_QUIRK_NO_IO_RESTRICTION) {
479 if (ret)
480 dev_warn(dev, FW_BUG "GPIO not in correct mode, fixing\n");
481 } else {
482 if (ret)
483 dev_dbg(dev, "Override GPIO initialization flags\n");
484 *flags = old;
485 }
Andy Shevchenko5c34b6c2017-11-10 15:40:31 +0200486
487 return ret;
488}
489
Mika Westerberg12028d22013-04-03 13:56:54 +0300490struct acpi_gpio_lookup {
491 struct acpi_gpio_info info;
492 int index;
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100493 int pin_index;
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200494 bool active_low;
Mika Westerberg936e15d2013-10-10 11:01:08 +0300495 struct gpio_desc *desc;
Mika Westerberg12028d22013-04-03 13:56:54 +0300496 int n;
497};
498
Linus Walleij031ba282016-10-03 10:40:03 +0200499static int acpi_populate_gpio_lookup(struct acpi_resource *ares, void *data)
Mika Westerberg12028d22013-04-03 13:56:54 +0300500{
501 struct acpi_gpio_lookup *lookup = data;
502
503 if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
504 return 1;
505
Mika Westerberg936e15d2013-10-10 11:01:08 +0300506 if (lookup->n++ == lookup->index && !lookup->desc) {
Mika Westerberg12028d22013-04-03 13:56:54 +0300507 const struct acpi_resource_gpio *agpio = &ares->data.gpio;
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100508 int pin_index = lookup->pin_index;
509
510 if (pin_index >= agpio->pin_table_length)
511 return 1;
Mika Westerberg12028d22013-04-03 13:56:54 +0300512
Mika Westerberg936e15d2013-10-10 11:01:08 +0300513 lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr,
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100514 agpio->pin_table[pin_index]);
Mika Westerberg12028d22013-04-03 13:56:54 +0300515 lookup->info.gpioint =
516 agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100517
518 /*
Andy Shevchenkoe567c352017-01-03 19:01:18 +0200519 * Polarity and triggering are only specified for GpioInt
520 * resource.
Christophe RICARD52044722015-12-23 23:25:34 +0100521 * Note: we expect here:
522 * - ACPI_ACTIVE_LOW == GPIO_ACTIVE_LOW
523 * - ACPI_ACTIVE_HIGH == GPIO_ACTIVE_HIGH
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100524 */
Christophe RICARD52044722015-12-23 23:25:34 +0100525 if (lookup->info.gpioint) {
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300526 lookup->info.flags = GPIOD_IN;
Christophe RICARD52044722015-12-23 23:25:34 +0100527 lookup->info.polarity = agpio->polarity;
528 lookup->info.triggering = agpio->triggering;
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300529 } else {
530 lookup->info.flags = acpi_gpio_to_gpiod_flags(agpio);
Andy Shevchenkof67a6c12017-11-10 15:40:28 +0200531 lookup->info.polarity = lookup->active_low;
Christophe RICARD52044722015-12-23 23:25:34 +0100532 }
Mika Westerberg12028d22013-04-03 13:56:54 +0300533 }
534
535 return 1;
536}
537
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200538static int acpi_gpio_resource_lookup(struct acpi_gpio_lookup *lookup,
539 struct acpi_gpio_info *info)
540{
Andy Shevchenko5870cff472017-11-10 15:40:30 +0200541 struct acpi_device *adev = lookup->info.adev;
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200542 struct list_head res_list;
543 int ret;
544
545 INIT_LIST_HEAD(&res_list);
546
Andy Shevchenko5870cff472017-11-10 15:40:30 +0200547 ret = acpi_dev_get_resources(adev, &res_list,
Linus Walleij031ba282016-10-03 10:40:03 +0200548 acpi_populate_gpio_lookup,
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200549 lookup);
550 if (ret < 0)
551 return ret;
552
553 acpi_dev_free_resource_list(&res_list);
554
555 if (!lookup->desc)
556 return -ENOENT;
557
Andy Shevchenkof67a6c12017-11-10 15:40:28 +0200558 if (info)
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200559 *info = lookup->info;
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200560 return 0;
561}
562
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200563static int acpi_gpio_property_lookup(struct fwnode_handle *fwnode,
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200564 const char *propname, int index,
565 struct acpi_gpio_lookup *lookup)
566{
567 struct acpi_reference_args args;
Andy Shevchenkoce0929d2017-11-10 15:40:32 +0200568 unsigned int quirks = 0;
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200569 int ret;
570
571 memset(&args, 0, sizeof(args));
Mika Westerberg6f7194a2016-10-21 17:21:29 +0300572 ret = __acpi_node_get_property_reference(fwnode, propname, index, 3,
573 &args);
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200574 if (ret) {
575 struct acpi_device *adev = to_acpi_device_node(fwnode);
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200576
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200577 if (!adev)
578 return ret;
579
Andy Shevchenkoce0929d2017-11-10 15:40:32 +0200580 if (!acpi_get_driver_gpio_data(adev, propname, index, &args,
581 &quirks))
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200582 return ret;
583 }
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200584 /*
585 * The property was found and resolved, so need to lookup the GPIO based
586 * on returned args.
587 */
Mika Westerberg6f7194a2016-10-21 17:21:29 +0300588 if (args.nargs != 3)
589 return -EPROTO;
590
591 lookup->index = args.args[0];
592 lookup->pin_index = args.args[1];
593 lookup->active_low = !!args.args[2];
594
Andy Shevchenko5870cff472017-11-10 15:40:30 +0200595 lookup->info.adev = args.adev;
Andy Shevchenkoce0929d2017-11-10 15:40:32 +0200596 lookup->info.quirks = quirks;
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200597 return 0;
598}
599
Mika Westerberg12028d22013-04-03 13:56:54 +0300600/**
Mika Westerberg936e15d2013-10-10 11:01:08 +0300601 * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100602 * @adev: pointer to a ACPI device to get GPIO from
603 * @propname: Property name of the GPIO (optional)
Mika Westerberg12028d22013-04-03 13:56:54 +0300604 * @index: index of GpioIo/GpioInt resource (starting from %0)
605 * @info: info pointer to fill in (optional)
606 *
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100607 * Function goes through ACPI resources for @adev and based on @index looks
Mika Westerberg936e15d2013-10-10 11:01:08 +0300608 * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor,
Mika Westerberg12028d22013-04-03 13:56:54 +0300609 * and returns it. @index matches GpioIo/GpioInt resources only so if there
610 * are total %3 GPIO resources, the index goes from %0 to %2.
611 *
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100612 * If @propname is specified the GPIO is looked using device property. In
613 * that case @index is used to select the GPIO entry in the property value
614 * (in case of multiple).
615 *
Mika Westerberg936e15d2013-10-10 11:01:08 +0300616 * If the GPIO cannot be translated or there is an error an ERR_PTR is
Mika Westerberg12028d22013-04-03 13:56:54 +0300617 * returned.
618 *
619 * Note: if the GPIO resource has multiple entries in the pin list, this
620 * function only returns the first.
621 */
Linus Walleij031ba282016-10-03 10:40:03 +0200622static struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100623 const char *propname, int index,
Mika Westerberg936e15d2013-10-10 11:01:08 +0300624 struct acpi_gpio_info *info)
Mika Westerberg12028d22013-04-03 13:56:54 +0300625{
626 struct acpi_gpio_lookup lookup;
Mika Westerberg12028d22013-04-03 13:56:54 +0300627 int ret;
628
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100629 if (!adev)
Mika Westerberg936e15d2013-10-10 11:01:08 +0300630 return ERR_PTR(-ENODEV);
Mika Westerberg12028d22013-04-03 13:56:54 +0300631
632 memset(&lookup, 0, sizeof(lookup));
633 lookup.index = index;
Mika Westerberg12028d22013-04-03 13:56:54 +0300634
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100635 if (propname) {
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100636 dev_dbg(&adev->dev, "GPIO: looking up %s\n", propname);
637
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200638 ret = acpi_gpio_property_lookup(acpi_fwnode_handle(adev),
639 propname, index, &lookup);
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200640 if (ret)
641 return ERR_PTR(ret);
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100642
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200643 dev_dbg(&adev->dev, "GPIO: _DSD returned %s %d %d %u\n",
Andy Shevchenko5870cff472017-11-10 15:40:30 +0200644 dev_name(&lookup.info.adev->dev), lookup.index,
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200645 lookup.pin_index, lookup.active_low);
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100646 } else {
647 dev_dbg(&adev->dev, "GPIO: looking up %d in _CRS\n", index);
Andy Shevchenko5870cff472017-11-10 15:40:30 +0200648 lookup.info.adev = adev;
Mika Westerberg0d9a6932014-10-29 15:41:01 +0100649 }
650
Rafael J. Wysockid0795242015-08-27 04:41:33 +0200651 ret = acpi_gpio_resource_lookup(&lookup, info);
652 return ret ? ERR_PTR(ret) : lookup.desc;
Mika Westerberg12028d22013-04-03 13:56:54 +0300653}
Mika Westerberg664e3e52014-01-08 12:40:54 +0200654
Linus Walleij031ba282016-10-03 10:40:03 +0200655struct gpio_desc *acpi_find_gpio(struct device *dev,
656 const char *con_id,
657 unsigned int idx,
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300658 enum gpiod_flags *dflags,
Linus Walleij031ba282016-10-03 10:40:03 +0200659 enum gpio_lookup_flags *lookupflags)
660{
661 struct acpi_device *adev = ACPI_COMPANION(dev);
662 struct acpi_gpio_info info;
663 struct gpio_desc *desc;
664 char propname[32];
665 int i;
666
667 /* Try first from _DSD */
668 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Andy Shevchenko9e665042017-05-23 20:03:17 +0300669 if (con_id) {
Linus Walleij031ba282016-10-03 10:40:03 +0200670 snprintf(propname, sizeof(propname), "%s-%s",
671 con_id, gpio_suffixes[i]);
672 } else {
673 snprintf(propname, sizeof(propname), "%s",
674 gpio_suffixes[i]);
675 }
676
677 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
Dmitry Torokhov693bdaa2017-03-23 13:21:38 -0700678 if (!IS_ERR(desc))
Linus Walleij031ba282016-10-03 10:40:03 +0200679 break;
Dmitry Torokhov693bdaa2017-03-23 13:21:38 -0700680 if (PTR_ERR(desc) == -EPROBE_DEFER)
681 return ERR_CAST(desc);
Linus Walleij031ba282016-10-03 10:40:03 +0200682 }
683
684 /* Then from plain _CRS GPIOs */
685 if (IS_ERR(desc)) {
686 if (!acpi_can_fallback_to_crs(adev, con_id))
687 return ERR_PTR(-ENOENT);
688
689 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
690 if (IS_ERR(desc))
691 return desc;
Andy Shevchenkofe06b562017-05-23 20:03:18 +0300692 }
Linus Walleij031ba282016-10-03 10:40:03 +0200693
Andy Shevchenkofe06b562017-05-23 20:03:18 +0300694 if (info.gpioint &&
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300695 (*dflags == GPIOD_OUT_LOW || *dflags == GPIOD_OUT_HIGH)) {
Andy Shevchenkofe06b562017-05-23 20:03:18 +0300696 dev_dbg(dev, "refusing GpioInt() entry when doing GPIOD_OUT_* lookup\n");
697 return ERR_PTR(-ENOENT);
Linus Walleij031ba282016-10-03 10:40:03 +0200698 }
699
700 if (info.polarity == GPIO_ACTIVE_LOW)
701 *lookupflags |= GPIO_ACTIVE_LOW;
702
Andy Shevchenko5c34b6c2017-11-10 15:40:31 +0200703 acpi_gpio_update_gpiod_flags(dflags, &info);
Linus Walleij031ba282016-10-03 10:40:03 +0200704 return desc;
705}
706
Mika Westerbergc884fbd2015-05-06 13:29:06 +0300707/**
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200708 * acpi_node_get_gpiod() - get a GPIO descriptor from ACPI resources
709 * @fwnode: pointer to an ACPI firmware node to get the GPIO information from
710 * @propname: Property name of the GPIO
711 * @index: index of GpioIo/GpioInt resource (starting from %0)
712 * @info: info pointer to fill in (optional)
713 *
714 * If @fwnode is an ACPI device object, call %acpi_get_gpiod_by_index() for it.
715 * Otherwise (ie. it is a data-only non-device object), use the property-based
716 * GPIO lookup to get to the GPIO resource with the relevant information and use
717 * that to obtain the GPIO descriptor to return.
718 */
719struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode,
720 const char *propname, int index,
721 struct acpi_gpio_info *info)
722{
723 struct acpi_gpio_lookup lookup;
724 struct acpi_device *adev;
725 int ret;
726
727 adev = to_acpi_device_node(fwnode);
728 if (adev)
729 return acpi_get_gpiod_by_index(adev, propname, index, info);
730
731 if (!is_acpi_data_node(fwnode))
732 return ERR_PTR(-ENODEV);
733
734 if (!propname)
735 return ERR_PTR(-EINVAL);
736
737 memset(&lookup, 0, sizeof(lookup));
738 lookup.index = index;
739
740 ret = acpi_gpio_property_lookup(fwnode, propname, index, &lookup);
741 if (ret)
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200742 return ERR_PTR(ret);
743
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200744 ret = acpi_gpio_resource_lookup(&lookup, info);
745 return ret ? ERR_PTR(ret) : lookup.desc;
Mika Westerberg664e3e52014-01-08 12:40:54 +0200746}
747
Mika Westerbergc884fbd2015-05-06 13:29:06 +0300748/**
749 * acpi_dev_gpio_irq_get() - Find GpioInt and translate it to Linux IRQ number
750 * @adev: pointer to a ACPI device to get IRQ from
751 * @index: index of GpioInt resource (starting from %0)
752 *
753 * If the device has one or more GpioInt resources, this function can be
754 * used to translate from the GPIO offset in the resource to the Linux IRQ
755 * number.
756 *
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300757 * The function is idempotent, though each time it runs it will configure GPIO
758 * pin direction according to the flags in GpioInt resource.
759 *
Thierry Redingdb05c7e2017-07-24 16:57:25 +0200760 * Return: Linux IRQ number (> %0) on success, negative errno on failure.
Mika Westerbergc884fbd2015-05-06 13:29:06 +0300761 */
762int acpi_dev_gpio_irq_get(struct acpi_device *adev, int index)
763{
764 int idx, i;
Christophe RICARD52044722015-12-23 23:25:34 +0100765 unsigned int irq_flags;
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300766 int ret;
Mika Westerbergc884fbd2015-05-06 13:29:06 +0300767
768 for (i = 0, idx = 0; idx <= index; i++) {
769 struct acpi_gpio_info info;
770 struct gpio_desc *desc;
771
772 desc = acpi_get_gpiod_by_index(adev, NULL, i, &info);
Christophe RICARD52044722015-12-23 23:25:34 +0100773
Hans de Goede6798d722017-03-13 23:04:21 +0100774 /* Ignore -EPROBE_DEFER, it only matters if idx matches */
775 if (IS_ERR(desc) && PTR_ERR(desc) != -EPROBE_DEFER)
776 return PTR_ERR(desc);
777
778 if (info.gpioint && idx++ == index) {
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300779 char label[32];
Hans de Goede6798d722017-03-13 23:04:21 +0100780 int irq;
781
782 if (IS_ERR(desc))
783 return PTR_ERR(desc);
784
785 irq = gpiod_to_irq(desc);
Christophe RICARD52044722015-12-23 23:25:34 +0100786 if (irq < 0)
787 return irq;
788
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300789 snprintf(label, sizeof(label), "GpioInt() %d", index);
790 ret = gpiod_configure_flags(desc, label, 0, info.flags);
791 if (ret < 0)
792 return ret;
793
Christophe RICARD52044722015-12-23 23:25:34 +0100794 irq_flags = acpi_dev_get_irq_type(info.triggering,
795 info.polarity);
796
797 /* Set type if specified and different than the current one */
798 if (irq_flags != IRQ_TYPE_NONE &&
799 irq_flags != irq_get_trigger_type(irq))
800 irq_set_irq_type(irq, irq_flags);
801
802 return irq;
803 }
804
Mika Westerbergc884fbd2015-05-06 13:29:06 +0300805 }
Hans de Goede6798d722017-03-13 23:04:21 +0100806 return -ENOENT;
Mika Westerbergc884fbd2015-05-06 13:29:06 +0300807}
808EXPORT_SYMBOL_GPL(acpi_dev_gpio_irq_get);
809
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200810static acpi_status
811acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
812 u32 bits, u64 *value, void *handler_context,
813 void *region_context)
814{
815 struct acpi_gpio_chip *achip = region_context;
816 struct gpio_chip *chip = achip->chip;
817 struct acpi_resource_gpio *agpio;
818 struct acpi_resource *ares;
819 int pin_index = (int)address;
820 acpi_status status;
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200821 int length;
822 int i;
823
824 status = acpi_buffer_to_resource(achip->conn_info.connection,
825 achip->conn_info.length, &ares);
826 if (ACPI_FAILURE(status))
827 return status;
828
829 if (WARN_ON(ares->type != ACPI_RESOURCE_TYPE_GPIO)) {
830 ACPI_FREE(ares);
831 return AE_BAD_PARAMETER;
832 }
833
834 agpio = &ares->data.gpio;
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200835
836 if (WARN_ON(agpio->io_restriction == ACPI_IO_RESTRICT_INPUT &&
837 function == ACPI_WRITE)) {
838 ACPI_FREE(ares);
839 return AE_BAD_PARAMETER;
840 }
841
842 length = min(agpio->pin_table_length, (u16)(pin_index + bits));
843 for (i = pin_index; i < length; ++i) {
Qipeng Zhaa4811622015-04-10 06:25:10 +0800844 int pin = agpio->pin_table[i];
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200845 struct acpi_gpio_connection *conn;
846 struct gpio_desc *desc;
847 bool found;
848
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200849 mutex_lock(&achip->conn_lock);
850
851 found = false;
852 list_for_each_entry(conn, &achip->conns, node) {
Alexandre Courbote46cf322014-08-19 10:06:08 -0700853 if (conn->pin == pin) {
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200854 found = true;
Alexandre Courbote46cf322014-08-19 10:06:08 -0700855 desc = conn->desc;
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200856 break;
857 }
858 }
Mika Westerbergc103a102015-10-30 12:02:05 +0200859
860 /*
861 * The same GPIO can be shared between operation region and
862 * event but only if the access here is ACPI_READ. In that
863 * case we "borrow" the event GPIO instead.
864 */
865 if (!found && agpio->sharable == ACPI_SHARED &&
866 function == ACPI_READ) {
867 struct acpi_gpio_event *event;
868
869 list_for_each_entry(event, &achip->events, node) {
870 if (event->pin == pin) {
871 desc = event->desc;
872 found = true;
873 break;
874 }
875 }
876 }
877
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200878 if (!found) {
Andy Shevchenko2eca25a2017-05-23 20:03:22 +0300879 enum gpiod_flags flags = acpi_gpio_to_gpiod_flags(agpio);
880 const char *label = "ACPI:OpRegion";
881 int err;
882
883 desc = gpiochip_request_own_desc(chip, pin, label);
Alexandre Courbote46cf322014-08-19 10:06:08 -0700884 if (IS_ERR(desc)) {
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200885 status = AE_ERROR;
886 mutex_unlock(&achip->conn_lock);
887 goto out;
888 }
889
Andy Shevchenko2eca25a2017-05-23 20:03:22 +0300890 err = gpiod_configure_flags(desc, label, 0, flags);
891 if (err < 0) {
892 status = AE_NOT_CONFIGURED;
893 gpiochip_free_own_desc(desc);
894 mutex_unlock(&achip->conn_lock);
895 goto out;
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200896 }
897
898 conn = kzalloc(sizeof(*conn), GFP_KERNEL);
899 if (!conn) {
900 status = AE_NO_MEMORY;
901 gpiochip_free_own_desc(desc);
902 mutex_unlock(&achip->conn_lock);
903 goto out;
904 }
905
Alexandre Courbote46cf322014-08-19 10:06:08 -0700906 conn->pin = pin;
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200907 conn->desc = desc;
908 list_add_tail(&conn->node, &achip->conns);
909 }
910
911 mutex_unlock(&achip->conn_lock);
912
913 if (function == ACPI_WRITE)
Aaron Ludc62b562014-05-20 17:07:38 +0800914 gpiod_set_raw_value_cansleep(desc,
915 !!((1 << i) & *value));
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200916 else
Aaron Ludc62b562014-05-20 17:07:38 +0800917 *value |= (u64)gpiod_get_raw_value_cansleep(desc) << i;
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200918 }
919
920out:
921 ACPI_FREE(ares);
922 return status;
923}
924
925static void acpi_gpiochip_request_regions(struct acpi_gpio_chip *achip)
926{
927 struct gpio_chip *chip = achip->chip;
Linus Walleij58383c782015-11-04 09:56:26 +0100928 acpi_handle handle = ACPI_HANDLE(chip->parent);
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200929 acpi_status status;
930
931 INIT_LIST_HEAD(&achip->conns);
932 mutex_init(&achip->conn_lock);
933 status = acpi_install_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
934 acpi_gpio_adr_space_handler,
935 NULL, achip);
936 if (ACPI_FAILURE(status))
Linus Walleij58383c782015-11-04 09:56:26 +0100937 dev_err(chip->parent,
938 "Failed to install GPIO OpRegion handler\n");
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200939}
940
941static void acpi_gpiochip_free_regions(struct acpi_gpio_chip *achip)
942{
943 struct gpio_chip *chip = achip->chip;
Linus Walleij58383c782015-11-04 09:56:26 +0100944 acpi_handle handle = ACPI_HANDLE(chip->parent);
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200945 struct acpi_gpio_connection *conn, *tmp;
946 acpi_status status;
947
948 status = acpi_remove_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
949 acpi_gpio_adr_space_handler);
950 if (ACPI_FAILURE(status)) {
Linus Walleij58383c782015-11-04 09:56:26 +0100951 dev_err(chip->parent,
952 "Failed to remove GPIO OpRegion handler\n");
Mika Westerberg473ed7b2014-03-14 17:58:07 +0200953 return;
954 }
955
956 list_for_each_entry_safe_reverse(conn, tmp, &achip->conns, node) {
957 gpiochip_free_own_desc(conn->desc);
958 list_del(&conn->node);
959 kfree(conn);
960 }
961}
962
Wei Yongjun550a9532016-10-29 16:13:12 +0000963static struct gpio_desc *acpi_gpiochip_parse_own_gpio(
964 struct acpi_gpio_chip *achip, struct fwnode_handle *fwnode,
965 const char **name, unsigned int *lflags, unsigned int *dflags)
Mika Westerbergc80f1ba2016-10-21 17:21:30 +0300966{
967 struct gpio_chip *chip = achip->chip;
968 struct gpio_desc *desc;
969 u32 gpios[2];
970 int ret;
971
Arnd Bergmannc82064f2016-11-08 14:40:06 +0100972 *lflags = 0;
973 *dflags = 0;
974 *name = NULL;
975
Mika Westerbergc80f1ba2016-10-21 17:21:30 +0300976 ret = fwnode_property_read_u32_array(fwnode, "gpios", gpios,
977 ARRAY_SIZE(gpios));
978 if (ret < 0)
979 return ERR_PTR(ret);
980
Mika Westerberg03c47492017-11-27 16:54:42 +0300981 desc = gpiochip_get_desc(chip, gpios[0]);
Mika Westerbergc80f1ba2016-10-21 17:21:30 +0300982 if (IS_ERR(desc))
983 return desc;
984
Mika Westerbergc80f1ba2016-10-21 17:21:30 +0300985 if (gpios[1])
986 *lflags |= GPIO_ACTIVE_LOW;
987
988 if (fwnode_property_present(fwnode, "input"))
989 *dflags |= GPIOD_IN;
990 else if (fwnode_property_present(fwnode, "output-low"))
991 *dflags |= GPIOD_OUT_LOW;
992 else if (fwnode_property_present(fwnode, "output-high"))
993 *dflags |= GPIOD_OUT_HIGH;
994 else
995 return ERR_PTR(-EINVAL);
996
997 fwnode_property_read_string(fwnode, "line-name", name);
998
999 return desc;
1000}
1001
1002static void acpi_gpiochip_scan_gpios(struct acpi_gpio_chip *achip)
1003{
1004 struct gpio_chip *chip = achip->chip;
1005 struct fwnode_handle *fwnode;
1006
1007 device_for_each_child_node(chip->parent, fwnode) {
1008 unsigned int lflags, dflags;
1009 struct gpio_desc *desc;
1010 const char *name;
1011 int ret;
1012
1013 if (!fwnode_property_present(fwnode, "gpio-hog"))
1014 continue;
1015
1016 desc = acpi_gpiochip_parse_own_gpio(achip, fwnode, &name,
1017 &lflags, &dflags);
1018 if (IS_ERR(desc))
1019 continue;
1020
1021 ret = gpiod_hog(desc, name, lflags, dflags);
1022 if (ret) {
1023 dev_err(chip->parent, "Failed to hog GPIO\n");
Wei Yongjun1b6998c2016-10-29 16:11:57 +00001024 fwnode_handle_put(fwnode);
Mika Westerbergc80f1ba2016-10-21 17:21:30 +03001025 return;
1026 }
1027 }
1028}
1029
Mika Westerberg664e3e52014-01-08 12:40:54 +02001030void acpi_gpiochip_add(struct gpio_chip *chip)
1031{
Mika Westerbergaa92b6f2014-03-10 14:54:51 +02001032 struct acpi_gpio_chip *acpi_gpio;
1033 acpi_handle handle;
1034 acpi_status status;
1035
Linus Walleij58383c782015-11-04 09:56:26 +01001036 if (!chip || !chip->parent)
Mika Westerberge9595f82014-03-31 15:16:49 +03001037 return;
1038
Linus Walleij58383c782015-11-04 09:56:26 +01001039 handle = ACPI_HANDLE(chip->parent);
Mika Westerbergaa92b6f2014-03-10 14:54:51 +02001040 if (!handle)
1041 return;
1042
1043 acpi_gpio = kzalloc(sizeof(*acpi_gpio), GFP_KERNEL);
1044 if (!acpi_gpio) {
Linus Walleij58383c782015-11-04 09:56:26 +01001045 dev_err(chip->parent,
Mika Westerbergaa92b6f2014-03-10 14:54:51 +02001046 "Failed to allocate memory for ACPI GPIO chip\n");
1047 return;
1048 }
1049
1050 acpi_gpio->chip = chip;
Mika Westerbergc103a102015-10-30 12:02:05 +02001051 INIT_LIST_HEAD(&acpi_gpio->events);
Mika Westerbergaa92b6f2014-03-10 14:54:51 +02001052
1053 status = acpi_attach_data(handle, acpi_gpio_chip_dh, acpi_gpio);
1054 if (ACPI_FAILURE(status)) {
Linus Walleij58383c782015-11-04 09:56:26 +01001055 dev_err(chip->parent, "Failed to attach ACPI GPIO chip\n");
Mika Westerbergaa92b6f2014-03-10 14:54:51 +02001056 kfree(acpi_gpio);
1057 return;
1058 }
1059
Mika Westerberg4035cc12016-10-21 17:21:32 +03001060 if (!chip->names)
Christophe Leroy82270332017-12-15 15:02:33 +01001061 devprop_gpiochip_set_names(chip, dev_fwnode(chip->parent));
Mika Westerberg4035cc12016-10-21 17:21:32 +03001062
Mika Westerberg473ed7b2014-03-14 17:58:07 +02001063 acpi_gpiochip_request_regions(acpi_gpio);
Mika Westerbergc80f1ba2016-10-21 17:21:30 +03001064 acpi_gpiochip_scan_gpios(acpi_gpio);
Rui Zhang3f86a632016-06-15 08:47:51 +02001065 acpi_walk_dep_device_list(handle);
Mika Westerberg664e3e52014-01-08 12:40:54 +02001066}
1067
1068void acpi_gpiochip_remove(struct gpio_chip *chip)
1069{
Mika Westerbergaa92b6f2014-03-10 14:54:51 +02001070 struct acpi_gpio_chip *acpi_gpio;
1071 acpi_handle handle;
1072 acpi_status status;
1073
Linus Walleij58383c782015-11-04 09:56:26 +01001074 if (!chip || !chip->parent)
Mika Westerberge9595f82014-03-31 15:16:49 +03001075 return;
1076
Linus Walleij58383c782015-11-04 09:56:26 +01001077 handle = ACPI_HANDLE(chip->parent);
Mika Westerbergaa92b6f2014-03-10 14:54:51 +02001078 if (!handle)
1079 return;
1080
1081 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
1082 if (ACPI_FAILURE(status)) {
Linus Walleij58383c782015-11-04 09:56:26 +01001083 dev_warn(chip->parent, "Failed to retrieve ACPI GPIO chip\n");
Mika Westerbergaa92b6f2014-03-10 14:54:51 +02001084 return;
1085 }
1086
Mika Westerberg473ed7b2014-03-14 17:58:07 +02001087 acpi_gpiochip_free_regions(acpi_gpio);
Mika Westerbergaa92b6f2014-03-10 14:54:51 +02001088
1089 acpi_detach_data(handle, acpi_gpio_chip_dh);
1090 kfree(acpi_gpio);
Mika Westerberg664e3e52014-01-08 12:40:54 +02001091}
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001092
Mika Westerberg6f7194a2016-10-21 17:21:29 +03001093static int acpi_gpio_package_count(const union acpi_object *obj)
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001094{
1095 const union acpi_object *element = obj->package.elements;
1096 const union acpi_object *end = element + obj->package.count;
1097 unsigned int count = 0;
1098
1099 while (element < end) {
Mika Westerberg6f7194a2016-10-21 17:21:29 +03001100 switch (element->type) {
1101 case ACPI_TYPE_LOCAL_REFERENCE:
1102 element += 3;
1103 /* Fallthrough */
1104 case ACPI_TYPE_INTEGER:
1105 element++;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001106 count++;
Mika Westerberg6f7194a2016-10-21 17:21:29 +03001107 break;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001108
Mika Westerberg6f7194a2016-10-21 17:21:29 +03001109 default:
1110 return -EPROTO;
1111 }
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001112 }
Mika Westerberg6f7194a2016-10-21 17:21:29 +03001113
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001114 return count;
1115}
1116
1117static int acpi_find_gpio_count(struct acpi_resource *ares, void *data)
1118{
1119 unsigned int *count = data;
1120
1121 if (ares->type == ACPI_RESOURCE_TYPE_GPIO)
1122 *count += ares->data.gpio.pin_table_length;
1123
1124 return 1;
1125}
1126
1127/**
1128 * acpi_gpio_count - return the number of GPIOs associated with a
1129 * device / function or -ENOENT if no GPIO has been
1130 * assigned to the requested function.
1131 * @dev: GPIO consumer, can be NULL for system-global GPIOs
1132 * @con_id: function within the GPIO consumer
1133 */
1134int acpi_gpio_count(struct device *dev, const char *con_id)
1135{
1136 struct acpi_device *adev = ACPI_COMPANION(dev);
1137 const union acpi_object *obj;
1138 const struct acpi_gpio_mapping *gm;
1139 int count = -ENOENT;
1140 int ret;
1141 char propname[32];
1142 unsigned int i;
1143
1144 /* Try first from _DSD */
1145 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Andy Shevchenko9e665042017-05-23 20:03:17 +03001146 if (con_id)
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001147 snprintf(propname, sizeof(propname), "%s-%s",
1148 con_id, gpio_suffixes[i]);
1149 else
1150 snprintf(propname, sizeof(propname), "%s",
1151 gpio_suffixes[i]);
1152
1153 ret = acpi_dev_get_property(adev, propname, ACPI_TYPE_ANY,
1154 &obj);
1155 if (ret == 0) {
1156 if (obj->type == ACPI_TYPE_LOCAL_REFERENCE)
1157 count = 1;
1158 else if (obj->type == ACPI_TYPE_PACKAGE)
1159 count = acpi_gpio_package_count(obj);
1160 } else if (adev->driver_gpios) {
1161 for (gm = adev->driver_gpios; gm->name; gm++)
1162 if (strcmp(propname, gm->name) == 0) {
1163 count = gm->size;
1164 break;
1165 }
1166 }
Andy Shevchenko4ed55012017-02-20 18:15:46 +02001167 if (count > 0)
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001168 break;
1169 }
1170
1171 /* Then from plain _CRS GPIOs */
1172 if (count < 0) {
1173 struct list_head resource_list;
1174 unsigned int crs_count = 0;
1175
Andy Shevchenko6fe9da42017-05-23 20:03:20 +03001176 if (!acpi_can_fallback_to_crs(adev, con_id))
1177 return count;
1178
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001179 INIT_LIST_HEAD(&resource_list);
1180 acpi_dev_get_resources(adev, &resource_list,
1181 acpi_find_gpio_count, &crs_count);
1182 acpi_dev_free_resource_list(&resource_list);
1183 if (crs_count > 0)
1184 count = crs_count;
1185 }
Andy Shevchenko4ed55012017-02-20 18:15:46 +02001186 return count ? count : -ENOENT;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01001187}
Dmitry Torokhov10cf4892015-11-11 11:45:30 -08001188
Dmitry Torokhov10cf4892015-11-11 11:45:30 -08001189bool acpi_can_fallback_to_crs(struct acpi_device *adev, const char *con_id)
1190{
Dmitry Torokhov10cf4892015-11-11 11:45:30 -08001191 /* Never allow fallback if the device has properties */
1192 if (adev->data.properties || adev->driver_gpios)
1193 return false;
1194
Andy Shevchenkof10e4bf2017-05-23 20:03:19 +03001195 return con_id == NULL;
Dmitry Torokhov10cf4892015-11-11 11:45:30 -08001196}
Benjamin Tissoiresca876c72018-07-12 17:25:06 +02001197
1198/* Sync the initial state of handlers after all builtin drivers have probed */
1199static int acpi_gpio_initial_sync(void)
1200{
1201 struct acpi_gpio_event *event, *ep;
1202
1203 mutex_lock(&acpi_gpio_initial_sync_list_lock);
1204 list_for_each_entry_safe(event, ep, &acpi_gpio_initial_sync_list,
1205 initial_sync_list) {
1206 acpi_evaluate_object(event->handle, NULL, NULL, NULL);
1207 list_del_init(&event->initial_sync_list);
1208 }
1209 mutex_unlock(&acpi_gpio_initial_sync_list_lock);
1210
1211 return 0;
1212}
1213/* We must use _sync so that this runs after the first deferred_probe run */
1214late_initcall_sync(acpi_gpio_initial_sync);