]>
Commit | Line | Data |
---|---|---|
e29482e8 MN |
1 | /* |
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 | ||
a7e0416d | 13 | #include <linux/dmi.h> |
e29482e8 | 14 | #include <linux/errno.h> |
354567e6 | 15 | #include <linux/gpio.h> |
936e15dd | 16 | #include <linux/gpio/consumer.h> |
5ccff852 | 17 | #include <linux/gpio/driver.h> |
031ba28a | 18 | #include <linux/gpio/machine.h> |
e29482e8 | 19 | #include <linux/export.h> |
e29482e8 | 20 | #include <linux/acpi.h> |
0d1c28a4 | 21 | #include <linux/interrupt.h> |
473ed7be | 22 | #include <linux/mutex.h> |
354567e6 | 23 | #include <linux/pinctrl/pinctrl.h> |
e29482e8 | 24 | |
5ccff852 MW |
25 | #include "gpiolib.h" |
26 | ||
a7e0416d HG |
27 | static int run_edge_events_on_boot = -1; |
28 | module_param(run_edge_events_on_boot, int, 0444); | |
29 | MODULE_PARM_DESC(run_edge_events_on_boot, | |
30 | "Run edge _AEI event-handlers at boot: 0=no, 1=yes, -1=auto"); | |
31 | ||
a4adb921 HG |
32 | /** |
33 | * struct acpi_gpio_event - ACPI GPIO event handler data | |
34 | * | |
35 | * @node: list-entry of the events list of the struct acpi_gpio_chip | |
36 | * @handle: handle of ACPI method to execute when the IRQ triggers | |
37 | * @handler: irq_handler to pass to request_irq when requesting the IRQ | |
38 | * @pin: GPIO pin number on the gpio_chip | |
39 | * @irq: Linux IRQ number for the event, for request_ / free_irq | |
40 | * @irqflags: flags to pass to request_irq when requesting the IRQ | |
41 | * @irq_is_wake: If the ACPI flags indicate the IRQ is a wakeup source | |
42 | * @is_requested: True if request_irq has been done | |
43 | * @desc: gpio_desc for the GPIO pin for this event | |
44 | */ | |
4b01a14b | 45 | struct acpi_gpio_event { |
7fc7acb9 | 46 | struct list_head node; |
4b01a14b | 47 | acpi_handle handle; |
a4adb921 | 48 | irq_handler_t handler; |
7fc7acb9 RW |
49 | unsigned int pin; |
50 | unsigned int irq; | |
a4adb921 HG |
51 | unsigned long irqflags; |
52 | bool irq_is_wake; | |
53 | bool irq_requested; | |
e46cf32c | 54 | struct gpio_desc *desc; |
7fc7acb9 RW |
55 | }; |
56 | ||
473ed7be MW |
57 | struct acpi_gpio_connection { |
58 | struct list_head node; | |
e46cf32c | 59 | unsigned int pin; |
473ed7be MW |
60 | struct gpio_desc *desc; |
61 | }; | |
62 | ||
aa92b6f6 | 63 | struct acpi_gpio_chip { |
473ed7be MW |
64 | /* |
65 | * ACPICA requires that the first field of the context parameter | |
66 | * passed to acpi_install_address_space_handler() is large enough | |
67 | * to hold struct acpi_connection_info. | |
68 | */ | |
69 | struct acpi_connection_info conn_info; | |
70 | struct list_head conns; | |
71 | struct mutex conn_lock; | |
aa92b6f6 | 72 | struct gpio_chip *chip; |
4b01a14b | 73 | struct list_head events; |
2efa9e0b | 74 | struct list_head deferred_req_irqs_list_entry; |
aa92b6f6 MW |
75 | }; |
76 | ||
2efa9e0b HG |
77 | /* |
78 | * For gpiochips which call acpi_gpiochip_request_interrupts() before late_init | |
a4adb921 | 79 | * (so builtin drivers) we register the ACPI GpioInt IRQ handlers from a |
2efa9e0b HG |
80 | * late_initcall_sync handler, so that other builtin drivers can register their |
81 | * OpRegions before the event handlers can run. This list contains gpiochips | |
a4adb921 | 82 | * for which the acpi_gpiochip_request_irqs() call has been deferred. |
2efa9e0b HG |
83 | */ |
84 | static DEFINE_MUTEX(acpi_gpio_deferred_req_irqs_lock); | |
85 | static LIST_HEAD(acpi_gpio_deferred_req_irqs_list); | |
86 | static bool acpi_gpio_deferred_req_irqs_done; | |
4ea03f3e | 87 | |
e29482e8 MN |
88 | static int acpi_gpiochip_find(struct gpio_chip *gc, void *data) |
89 | { | |
58383c78 | 90 | if (!gc->parent) |
e29482e8 MN |
91 | return false; |
92 | ||
58383c78 | 93 | return ACPI_HANDLE(gc->parent) == data; |
e29482e8 MN |
94 | } |
95 | ||
96 | /** | |
936e15dd | 97 | * acpi_get_gpiod() - Translate ACPI GPIO pin to GPIO descriptor usable with GPIO API |
e29482e8 MN |
98 | * @path: ACPI GPIO controller full path name, (e.g. "\\_SB.GPO1") |
99 | * @pin: ACPI GPIO pin number (0-based, controller-relative) | |
100 | * | |
f35bbf61 MW |
101 | * Return: GPIO descriptor to use with Linux generic GPIO API, or ERR_PTR |
102 | * error value. Specifically returns %-EPROBE_DEFER if the referenced GPIO | |
103 | * controller does not have gpiochip registered at the moment. This is to | |
104 | * support probe deferral. | |
e29482e8 | 105 | */ |
936e15dd | 106 | static struct gpio_desc *acpi_get_gpiod(char *path, int pin) |
e29482e8 MN |
107 | { |
108 | struct gpio_chip *chip; | |
109 | acpi_handle handle; | |
110 | acpi_status status; | |
111 | ||
112 | status = acpi_get_handle(NULL, path, &handle); | |
113 | if (ACPI_FAILURE(status)) | |
936e15dd | 114 | return ERR_PTR(-ENODEV); |
e29482e8 MN |
115 | |
116 | chip = gpiochip_find(handle, acpi_gpiochip_find); | |
117 | if (!chip) | |
f35bbf61 | 118 | return ERR_PTR(-EPROBE_DEFER); |
e29482e8 | 119 | |
85ef722e | 120 | return gpiochip_get_desc(chip, pin); |
e29482e8 | 121 | } |
0d1c28a4 | 122 | |
0d1c28a4 MN |
123 | static irqreturn_t acpi_gpio_irq_handler(int irq, void *data) |
124 | { | |
6072b9dc | 125 | struct acpi_gpio_event *event = data; |
0d1c28a4 | 126 | |
6072b9dc | 127 | acpi_evaluate_object(event->handle, NULL, NULL, NULL); |
0d1c28a4 MN |
128 | |
129 | return IRQ_HANDLED; | |
130 | } | |
131 | ||
7fc7acb9 RW |
132 | static irqreturn_t acpi_gpio_irq_handler_evt(int irq, void *data) |
133 | { | |
4b01a14b | 134 | struct acpi_gpio_event *event = data; |
7fc7acb9 | 135 | |
4b01a14b | 136 | acpi_execute_simple_method(event->handle, NULL, event->pin); |
7fc7acb9 RW |
137 | |
138 | return IRQ_HANDLED; | |
139 | } | |
140 | ||
aa92b6f6 | 141 | static void acpi_gpio_chip_dh(acpi_handle handle, void *data) |
7fc7acb9 RW |
142 | { |
143 | /* The address of this function is used as a key. */ | |
144 | } | |
145 | ||
25e3ef89 AS |
146 | bool acpi_gpio_get_irq_resource(struct acpi_resource *ares, |
147 | struct acpi_resource_gpio **agpio) | |
148 | { | |
149 | struct acpi_resource_gpio *gpio; | |
150 | ||
151 | if (ares->type != ACPI_RESOURCE_TYPE_GPIO) | |
152 | return false; | |
153 | ||
154 | gpio = &ares->data.gpio; | |
155 | if (gpio->connection_type != ACPI_RESOURCE_GPIO_TYPE_INT) | |
156 | return false; | |
157 | ||
158 | *agpio = gpio; | |
159 | return true; | |
160 | } | |
161 | EXPORT_SYMBOL_GPL(acpi_gpio_get_irq_resource); | |
162 | ||
a4adb921 HG |
163 | static void acpi_gpiochip_request_irq(struct acpi_gpio_chip *acpi_gpio, |
164 | struct acpi_gpio_event *event) | |
165 | { | |
166 | int ret, value; | |
167 | ||
168 | ret = request_threaded_irq(event->irq, NULL, event->handler, | |
169 | event->irqflags, "ACPI:Event", event); | |
170 | if (ret) { | |
171 | dev_err(acpi_gpio->chip->parent, | |
172 | "Failed to setup interrupt handler for %d\n", | |
173 | event->irq); | |
174 | return; | |
175 | } | |
176 | ||
177 | if (event->irq_is_wake) | |
178 | enable_irq_wake(event->irq); | |
179 | ||
180 | event->irq_requested = true; | |
181 | ||
182 | /* Make sure we trigger the initial state of edge-triggered IRQs */ | |
a7e0416d HG |
183 | if (run_edge_events_on_boot && |
184 | (event->irqflags & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING))) { | |
185 | value = gpiod_get_raw_value_cansleep(event->desc); | |
186 | if (((event->irqflags & IRQF_TRIGGER_RISING) && value == 1) || | |
187 | ((event->irqflags & IRQF_TRIGGER_FALLING) && value == 0)) | |
188 | event->handler(event->irq, event); | |
189 | } | |
a4adb921 HG |
190 | } |
191 | ||
192 | static void acpi_gpiochip_request_irqs(struct acpi_gpio_chip *acpi_gpio) | |
193 | { | |
194 | struct acpi_gpio_event *event; | |
195 | ||
196 | list_for_each_entry(event, &acpi_gpio->events, node) | |
197 | acpi_gpiochip_request_irq(acpi_gpio, event); | |
198 | } | |
199 | ||
200 | static acpi_status acpi_gpiochip_alloc_event(struct acpi_resource *ares, | |
201 | void *context) | |
0d1c28a4 | 202 | { |
6072b9dc | 203 | struct acpi_gpio_chip *acpi_gpio = context; |
aa92b6f6 | 204 | struct gpio_chip *chip = acpi_gpio->chip; |
6072b9dc | 205 | struct acpi_resource_gpio *agpio; |
7fc7acb9 | 206 | acpi_handle handle, evt_handle; |
6072b9dc MW |
207 | struct acpi_gpio_event *event; |
208 | irq_handler_t handler = NULL; | |
209 | struct gpio_desc *desc; | |
a4adb921 | 210 | int ret, pin, irq; |
0d1c28a4 | 211 | |
25e3ef89 | 212 | if (!acpi_gpio_get_irq_resource(ares, &agpio)) |
6072b9dc | 213 | return AE_OK; |
0d1c28a4 | 214 | |
58383c78 | 215 | handle = ACPI_HANDLE(chip->parent); |
6072b9dc MW |
216 | pin = agpio->pin_table[0]; |
217 | ||
218 | if (pin <= 255) { | |
219 | char ev_name[5]; | |
e40a3ae1 | 220 | sprintf(ev_name, "_%c%02hhX", |
6072b9dc MW |
221 | agpio->triggering == ACPI_EDGE_SENSITIVE ? 'E' : 'L', |
222 | pin); | |
223 | if (ACPI_SUCCESS(acpi_get_handle(handle, ev_name, &evt_handle))) | |
224 | handler = acpi_gpio_irq_handler; | |
225 | } | |
226 | if (!handler) { | |
227 | if (ACPI_SUCCESS(acpi_get_handle(handle, "_EVT", &evt_handle))) | |
228 | handler = acpi_gpio_irq_handler_evt; | |
229 | } | |
230 | if (!handler) | |
c06632ea | 231 | return AE_OK; |
0d1c28a4 | 232 | |
abdc08a3 | 233 | desc = gpiochip_request_own_desc(chip, pin, "ACPI:Event"); |
6072b9dc | 234 | if (IS_ERR(desc)) { |
58383c78 | 235 | dev_err(chip->parent, "Failed to request GPIO\n"); |
6072b9dc MW |
236 | return AE_ERROR; |
237 | } | |
0d1c28a4 | 238 | |
6072b9dc | 239 | gpiod_direction_input(desc); |
0d1c28a4 | 240 | |
e3a2e878 | 241 | ret = gpiochip_lock_as_irq(chip, pin); |
6072b9dc | 242 | if (ret) { |
58383c78 | 243 | dev_err(chip->parent, "Failed to lock GPIO as interrupt\n"); |
6072b9dc MW |
244 | goto fail_free_desc; |
245 | } | |
0d1c28a4 | 246 | |
6072b9dc MW |
247 | irq = gpiod_to_irq(desc); |
248 | if (irq < 0) { | |
58383c78 | 249 | dev_err(chip->parent, "Failed to translate GPIO to IRQ\n"); |
6072b9dc MW |
250 | goto fail_unlock_irq; |
251 | } | |
7fc7acb9 | 252 | |
a4adb921 HG |
253 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
254 | if (!event) | |
255 | goto fail_unlock_irq; | |
256 | ||
257 | event->irqflags = IRQF_ONESHOT; | |
6072b9dc MW |
258 | if (agpio->triggering == ACPI_LEVEL_SENSITIVE) { |
259 | if (agpio->polarity == ACPI_ACTIVE_HIGH) | |
a4adb921 | 260 | event->irqflags |= IRQF_TRIGGER_HIGH; |
6072b9dc | 261 | else |
a4adb921 | 262 | event->irqflags |= IRQF_TRIGGER_LOW; |
6072b9dc MW |
263 | } else { |
264 | switch (agpio->polarity) { | |
265 | case ACPI_ACTIVE_HIGH: | |
a4adb921 | 266 | event->irqflags |= IRQF_TRIGGER_RISING; |
6072b9dc MW |
267 | break; |
268 | case ACPI_ACTIVE_LOW: | |
a4adb921 | 269 | event->irqflags |= IRQF_TRIGGER_FALLING; |
6072b9dc MW |
270 | break; |
271 | default: | |
a4adb921 HG |
272 | event->irqflags |= IRQF_TRIGGER_RISING | |
273 | IRQF_TRIGGER_FALLING; | |
6072b9dc | 274 | break; |
7fc7acb9 | 275 | } |
6072b9dc | 276 | } |
aa92b6f6 | 277 | |
6072b9dc | 278 | event->handle = evt_handle; |
a4adb921 | 279 | event->handler = handler; |
6072b9dc | 280 | event->irq = irq; |
a4adb921 | 281 | event->irq_is_wake = agpio->wake_capable == ACPI_WAKE_CAPABLE; |
6072b9dc | 282 | event->pin = pin; |
e46cf32c | 283 | event->desc = desc; |
7fc7acb9 | 284 | |
6072b9dc | 285 | list_add_tail(&event->node, &acpi_gpio->events); |
4ea03f3e | 286 | |
6072b9dc MW |
287 | return AE_OK; |
288 | ||
6072b9dc | 289 | fail_unlock_irq: |
e3a2e878 | 290 | gpiochip_unlock_as_irq(chip, pin); |
6072b9dc MW |
291 | fail_free_desc: |
292 | gpiochip_free_own_desc(desc); | |
293 | ||
294 | return AE_ERROR; | |
0d1c28a4 | 295 | } |
7fc7acb9 | 296 | |
70b53411 | 297 | /** |
6072b9dc | 298 | * acpi_gpiochip_request_interrupts() - Register isr for gpio chip ACPI events |
afa82fab | 299 | * @chip: GPIO chip |
70b53411 | 300 | * |
6072b9dc MW |
301 | * ACPI5 platforms can use GPIO signaled ACPI events. These GPIO interrupts are |
302 | * handled by ACPI event methods which need to be called from the GPIO | |
303 | * chip's interrupt handler. acpi_gpiochip_request_interrupts finds out which | |
304 | * gpio pins have acpi event methods and assigns interrupt handlers that calls | |
305 | * the acpi event methods for those pins. | |
306 | */ | |
afa82fab | 307 | void acpi_gpiochip_request_interrupts(struct gpio_chip *chip) |
6072b9dc | 308 | { |
afa82fab MW |
309 | struct acpi_gpio_chip *acpi_gpio; |
310 | acpi_handle handle; | |
311 | acpi_status status; | |
2efa9e0b | 312 | bool defer; |
afa82fab | 313 | |
58383c78 | 314 | if (!chip->parent || !chip->to_irq) |
afa82fab | 315 | return; |
6072b9dc | 316 | |
58383c78 | 317 | handle = ACPI_HANDLE(chip->parent); |
afa82fab MW |
318 | if (!handle) |
319 | return; | |
320 | ||
321 | status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio); | |
322 | if (ACPI_FAILURE(status)) | |
6072b9dc MW |
323 | return; |
324 | ||
a4adb921 HG |
325 | acpi_walk_resources(handle, "_AEI", |
326 | acpi_gpiochip_alloc_event, acpi_gpio); | |
327 | ||
2efa9e0b HG |
328 | mutex_lock(&acpi_gpio_deferred_req_irqs_lock); |
329 | defer = !acpi_gpio_deferred_req_irqs_done; | |
330 | if (defer) | |
331 | list_add(&acpi_gpio->deferred_req_irqs_list_entry, | |
332 | &acpi_gpio_deferred_req_irqs_list); | |
333 | mutex_unlock(&acpi_gpio_deferred_req_irqs_lock); | |
334 | ||
335 | if (defer) | |
336 | return; | |
337 | ||
a4adb921 | 338 | acpi_gpiochip_request_irqs(acpi_gpio); |
6072b9dc | 339 | } |
2b528fff | 340 | EXPORT_SYMBOL_GPL(acpi_gpiochip_request_interrupts); |
6072b9dc MW |
341 | |
342 | /** | |
343 | * acpi_gpiochip_free_interrupts() - Free GPIO ACPI event interrupts. | |
afa82fab | 344 | * @chip: GPIO chip |
70b53411 | 345 | * |
6072b9dc MW |
346 | * Free interrupts associated with GPIO ACPI event method for the given |
347 | * GPIO chip. | |
70b53411 | 348 | */ |
afa82fab | 349 | void acpi_gpiochip_free_interrupts(struct gpio_chip *chip) |
70b53411 | 350 | { |
afa82fab | 351 | struct acpi_gpio_chip *acpi_gpio; |
4b01a14b | 352 | struct acpi_gpio_event *event, *ep; |
afa82fab MW |
353 | acpi_handle handle; |
354 | acpi_status status; | |
355 | ||
58383c78 | 356 | if (!chip->parent || !chip->to_irq) |
afa82fab | 357 | return; |
70b53411 | 358 | |
58383c78 | 359 | handle = ACPI_HANDLE(chip->parent); |
afa82fab MW |
360 | if (!handle) |
361 | return; | |
362 | ||
363 | status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio); | |
364 | if (ACPI_FAILURE(status)) | |
70b53411 MW |
365 | return; |
366 | ||
2efa9e0b HG |
367 | mutex_lock(&acpi_gpio_deferred_req_irqs_lock); |
368 | if (!list_empty(&acpi_gpio->deferred_req_irqs_list_entry)) | |
369 | list_del_init(&acpi_gpio->deferred_req_irqs_list_entry); | |
370 | mutex_unlock(&acpi_gpio_deferred_req_irqs_lock); | |
371 | ||
4b01a14b | 372 | list_for_each_entry_safe_reverse(event, ep, &acpi_gpio->events, node) { |
6072b9dc MW |
373 | struct gpio_desc *desc; |
374 | ||
a4adb921 HG |
375 | if (event->irq_requested) { |
376 | if (event->irq_is_wake) | |
377 | disable_irq_wake(event->irq); | |
378 | ||
379 | free_irq(event->irq, event); | |
380 | } | |
8a146fbe | 381 | |
e46cf32c | 382 | desc = event->desc; |
6072b9dc MW |
383 | if (WARN_ON(IS_ERR(desc))) |
384 | continue; | |
e3a2e878 | 385 | gpiochip_unlock_as_irq(chip, event->pin); |
6072b9dc | 386 | gpiochip_free_own_desc(desc); |
4b01a14b MW |
387 | list_del(&event->node); |
388 | kfree(event); | |
70b53411 | 389 | } |
70b53411 | 390 | } |
2b528fff | 391 | EXPORT_SYMBOL_GPL(acpi_gpiochip_free_interrupts); |
70b53411 | 392 | |
f028d524 RW |
393 | int acpi_dev_add_driver_gpios(struct acpi_device *adev, |
394 | const struct acpi_gpio_mapping *gpios) | |
395 | { | |
396 | if (adev && gpios) { | |
397 | adev->driver_gpios = gpios; | |
398 | return 0; | |
399 | } | |
400 | return -EINVAL; | |
401 | } | |
402 | EXPORT_SYMBOL_GPL(acpi_dev_add_driver_gpios); | |
403 | ||
85c73d50 AS |
404 | static void devm_acpi_dev_release_driver_gpios(struct device *dev, void *res) |
405 | { | |
406 | acpi_dev_remove_driver_gpios(ACPI_COMPANION(dev)); | |
407 | } | |
408 | ||
409 | int devm_acpi_dev_add_driver_gpios(struct device *dev, | |
410 | const struct acpi_gpio_mapping *gpios) | |
411 | { | |
412 | void *res; | |
413 | int ret; | |
414 | ||
415 | res = devres_alloc(devm_acpi_dev_release_driver_gpios, 0, GFP_KERNEL); | |
416 | if (!res) | |
417 | return -ENOMEM; | |
418 | ||
419 | ret = acpi_dev_add_driver_gpios(ACPI_COMPANION(dev), gpios); | |
420 | if (ret) { | |
421 | devres_free(res); | |
422 | return ret; | |
423 | } | |
424 | devres_add(dev, res); | |
425 | return 0; | |
426 | } | |
427 | EXPORT_SYMBOL_GPL(devm_acpi_dev_add_driver_gpios); | |
428 | ||
429 | void devm_acpi_dev_remove_driver_gpios(struct device *dev) | |
430 | { | |
431 | WARN_ON(devres_release(dev, devm_acpi_dev_release_driver_gpios, NULL, NULL)); | |
432 | } | |
433 | EXPORT_SYMBOL_GPL(devm_acpi_dev_remove_driver_gpios); | |
434 | ||
f028d524 RW |
435 | static bool acpi_get_driver_gpio_data(struct acpi_device *adev, |
436 | const char *name, int index, | |
437 | struct acpi_reference_args *args) | |
438 | { | |
439 | const struct acpi_gpio_mapping *gm; | |
440 | ||
441 | if (!adev->driver_gpios) | |
442 | return false; | |
443 | ||
444 | for (gm = adev->driver_gpios; gm->name; gm++) | |
445 | if (!strcmp(name, gm->name) && gm->data && index < gm->size) { | |
446 | const struct acpi_gpio_params *par = gm->data + index; | |
447 | ||
448 | args->adev = adev; | |
449 | args->args[0] = par->crs_entry_index; | |
450 | args->args[1] = par->line_index; | |
451 | args->args[2] = par->active_low; | |
452 | args->nargs = 3; | |
453 | return true; | |
454 | } | |
455 | ||
456 | return false; | |
457 | } | |
458 | ||
2eca25af AS |
459 | static enum gpiod_flags |
460 | acpi_gpio_to_gpiod_flags(const struct acpi_resource_gpio *agpio) | |
461 | { | |
462 | bool pull_up = agpio->pin_config == ACPI_PIN_CONFIG_PULLUP; | |
463 | ||
464 | switch (agpio->io_restriction) { | |
465 | case ACPI_IO_RESTRICT_INPUT: | |
466 | return GPIOD_IN; | |
467 | case ACPI_IO_RESTRICT_OUTPUT: | |
468 | /* | |
469 | * ACPI GPIO resources don't contain an initial value for the | |
470 | * GPIO. Therefore we deduce that value from the pull field | |
471 | * instead. If the pin is pulled up we assume default to be | |
472 | * high, otherwise low. | |
473 | */ | |
474 | return pull_up ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW; | |
475 | default: | |
476 | /* | |
477 | * Assume that the BIOS has configured the direction and pull | |
478 | * accordingly. | |
479 | */ | |
480 | return GPIOD_ASIS; | |
481 | } | |
482 | } | |
483 | ||
a31f5c3a AS |
484 | int |
485 | acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, enum gpiod_flags update) | |
486 | { | |
487 | int ret = 0; | |
488 | ||
489 | /* | |
490 | * Check if the BIOS has IoRestriction with explicitly set direction | |
491 | * and update @flags accordingly. Otherwise use whatever caller asked | |
492 | * for. | |
493 | */ | |
494 | if (update & GPIOD_FLAGS_BIT_DIR_SET) { | |
495 | enum gpiod_flags diff = *flags ^ update; | |
496 | ||
497 | /* | |
498 | * Check if caller supplied incompatible GPIO initialization | |
499 | * flags. | |
500 | * | |
501 | * Return %-EINVAL to notify that firmware has different | |
502 | * settings and we are going to use them. | |
503 | */ | |
504 | if (((*flags & GPIOD_FLAGS_BIT_DIR_SET) && (diff & GPIOD_FLAGS_BIT_DIR_OUT)) || | |
505 | ((*flags & GPIOD_FLAGS_BIT_DIR_OUT) && (diff & GPIOD_FLAGS_BIT_DIR_VAL))) | |
506 | ret = -EINVAL; | |
507 | *flags = update; | |
508 | } | |
509 | return ret; | |
510 | } | |
511 | ||
12028d2d MW |
512 | struct acpi_gpio_lookup { |
513 | struct acpi_gpio_info info; | |
514 | int index; | |
0d9a693c | 515 | int pin_index; |
d079524a RW |
516 | bool active_low; |
517 | struct acpi_device *adev; | |
936e15dd | 518 | struct gpio_desc *desc; |
12028d2d MW |
519 | int n; |
520 | }; | |
521 | ||
031ba28a | 522 | static int acpi_populate_gpio_lookup(struct acpi_resource *ares, void *data) |
12028d2d MW |
523 | { |
524 | struct acpi_gpio_lookup *lookup = data; | |
525 | ||
526 | if (ares->type != ACPI_RESOURCE_TYPE_GPIO) | |
527 | return 1; | |
528 | ||
936e15dd | 529 | if (lookup->n++ == lookup->index && !lookup->desc) { |
12028d2d | 530 | const struct acpi_resource_gpio *agpio = &ares->data.gpio; |
0d9a693c MW |
531 | int pin_index = lookup->pin_index; |
532 | ||
533 | if (pin_index >= agpio->pin_table_length) | |
534 | return 1; | |
12028d2d | 535 | |
936e15dd | 536 | lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr, |
0d9a693c | 537 | agpio->pin_table[pin_index]); |
12028d2d MW |
538 | lookup->info.gpioint = |
539 | agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT; | |
0d9a693c MW |
540 | |
541 | /* | |
e567c35f AS |
542 | * Polarity and triggering are only specified for GpioInt |
543 | * resource. | |
52044723 CR |
544 | * Note: we expect here: |
545 | * - ACPI_ACTIVE_LOW == GPIO_ACTIVE_LOW | |
546 | * - ACPI_ACTIVE_HIGH == GPIO_ACTIVE_HIGH | |
0d9a693c | 547 | */ |
52044723 | 548 | if (lookup->info.gpioint) { |
a31f5c3a | 549 | lookup->info.flags = GPIOD_IN; |
52044723 CR |
550 | lookup->info.polarity = agpio->polarity; |
551 | lookup->info.triggering = agpio->triggering; | |
a31f5c3a AS |
552 | } else { |
553 | lookup->info.flags = acpi_gpio_to_gpiod_flags(agpio); | |
52044723 CR |
554 | } |
555 | ||
12028d2d MW |
556 | } |
557 | ||
558 | return 1; | |
559 | } | |
560 | ||
d079524a RW |
561 | static int acpi_gpio_resource_lookup(struct acpi_gpio_lookup *lookup, |
562 | struct acpi_gpio_info *info) | |
563 | { | |
564 | struct list_head res_list; | |
565 | int ret; | |
566 | ||
567 | INIT_LIST_HEAD(&res_list); | |
568 | ||
031ba28a LW |
569 | ret = acpi_dev_get_resources(lookup->adev, &res_list, |
570 | acpi_populate_gpio_lookup, | |
d079524a RW |
571 | lookup); |
572 | if (ret < 0) | |
573 | return ret; | |
574 | ||
575 | acpi_dev_free_resource_list(&res_list); | |
576 | ||
577 | if (!lookup->desc) | |
578 | return -ENOENT; | |
579 | ||
580 | if (info) { | |
581 | *info = lookup->info; | |
582 | if (lookup->active_low) | |
52044723 | 583 | info->polarity = lookup->active_low; |
d079524a RW |
584 | } |
585 | return 0; | |
586 | } | |
587 | ||
504a3374 | 588 | static int acpi_gpio_property_lookup(struct fwnode_handle *fwnode, |
d079524a RW |
589 | const char *propname, int index, |
590 | struct acpi_gpio_lookup *lookup) | |
591 | { | |
592 | struct acpi_reference_args args; | |
593 | int ret; | |
594 | ||
595 | memset(&args, 0, sizeof(args)); | |
6f7194a1 MW |
596 | ret = __acpi_node_get_property_reference(fwnode, propname, index, 3, |
597 | &args); | |
504a3374 RW |
598 | if (ret) { |
599 | struct acpi_device *adev = to_acpi_device_node(fwnode); | |
d079524a | 600 | |
504a3374 RW |
601 | if (!adev) |
602 | return ret; | |
603 | ||
604 | if (!acpi_get_driver_gpio_data(adev, propname, index, &args)) | |
605 | return ret; | |
606 | } | |
d079524a RW |
607 | /* |
608 | * The property was found and resolved, so need to lookup the GPIO based | |
609 | * on returned args. | |
610 | */ | |
611 | lookup->adev = args.adev; | |
6f7194a1 MW |
612 | if (args.nargs != 3) |
613 | return -EPROTO; | |
614 | ||
615 | lookup->index = args.args[0]; | |
616 | lookup->pin_index = args.args[1]; | |
617 | lookup->active_low = !!args.args[2]; | |
618 | ||
d079524a RW |
619 | return 0; |
620 | } | |
621 | ||
12028d2d | 622 | /** |
936e15dd | 623 | * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources |
0d9a693c MW |
624 | * @adev: pointer to a ACPI device to get GPIO from |
625 | * @propname: Property name of the GPIO (optional) | |
12028d2d MW |
626 | * @index: index of GpioIo/GpioInt resource (starting from %0) |
627 | * @info: info pointer to fill in (optional) | |
628 | * | |
0d9a693c | 629 | * Function goes through ACPI resources for @adev and based on @index looks |
936e15dd | 630 | * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor, |
12028d2d MW |
631 | * and returns it. @index matches GpioIo/GpioInt resources only so if there |
632 | * are total %3 GPIO resources, the index goes from %0 to %2. | |
633 | * | |
0d9a693c MW |
634 | * If @propname is specified the GPIO is looked using device property. In |
635 | * that case @index is used to select the GPIO entry in the property value | |
636 | * (in case of multiple). | |
637 | * | |
936e15dd | 638 | * If the GPIO cannot be translated or there is an error an ERR_PTR is |
12028d2d MW |
639 | * returned. |
640 | * | |
641 | * Note: if the GPIO resource has multiple entries in the pin list, this | |
642 | * function only returns the first. | |
643 | */ | |
031ba28a | 644 | static struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev, |
0d9a693c | 645 | const char *propname, int index, |
936e15dd | 646 | struct acpi_gpio_info *info) |
12028d2d MW |
647 | { |
648 | struct acpi_gpio_lookup lookup; | |
12028d2d MW |
649 | int ret; |
650 | ||
0d9a693c | 651 | if (!adev) |
936e15dd | 652 | return ERR_PTR(-ENODEV); |
12028d2d MW |
653 | |
654 | memset(&lookup, 0, sizeof(lookup)); | |
655 | lookup.index = index; | |
12028d2d | 656 | |
0d9a693c | 657 | if (propname) { |
0d9a693c MW |
658 | dev_dbg(&adev->dev, "GPIO: looking up %s\n", propname); |
659 | ||
504a3374 RW |
660 | ret = acpi_gpio_property_lookup(acpi_fwnode_handle(adev), |
661 | propname, index, &lookup); | |
d079524a RW |
662 | if (ret) |
663 | return ERR_PTR(ret); | |
0d9a693c | 664 | |
d079524a RW |
665 | dev_dbg(&adev->dev, "GPIO: _DSD returned %s %d %d %u\n", |
666 | dev_name(&lookup.adev->dev), lookup.index, | |
667 | lookup.pin_index, lookup.active_low); | |
0d9a693c MW |
668 | } else { |
669 | dev_dbg(&adev->dev, "GPIO: looking up %d in _CRS\n", index); | |
d079524a | 670 | lookup.adev = adev; |
0d9a693c MW |
671 | } |
672 | ||
d079524a RW |
673 | ret = acpi_gpio_resource_lookup(&lookup, info); |
674 | return ret ? ERR_PTR(ret) : lookup.desc; | |
12028d2d | 675 | } |
664e3e5a | 676 | |
031ba28a LW |
677 | struct gpio_desc *acpi_find_gpio(struct device *dev, |
678 | const char *con_id, | |
679 | unsigned int idx, | |
a31f5c3a | 680 | enum gpiod_flags *dflags, |
031ba28a LW |
681 | enum gpio_lookup_flags *lookupflags) |
682 | { | |
683 | struct acpi_device *adev = ACPI_COMPANION(dev); | |
684 | struct acpi_gpio_info info; | |
685 | struct gpio_desc *desc; | |
686 | char propname[32]; | |
a31f5c3a | 687 | int err; |
031ba28a LW |
688 | int i; |
689 | ||
690 | /* Try first from _DSD */ | |
691 | for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { | |
9e66504a | 692 | if (con_id) { |
031ba28a LW |
693 | snprintf(propname, sizeof(propname), "%s-%s", |
694 | con_id, gpio_suffixes[i]); | |
695 | } else { | |
696 | snprintf(propname, sizeof(propname), "%s", | |
697 | gpio_suffixes[i]); | |
698 | } | |
699 | ||
700 | desc = acpi_get_gpiod_by_index(adev, propname, idx, &info); | |
693bdaa1 | 701 | if (!IS_ERR(desc)) |
031ba28a | 702 | break; |
693bdaa1 DT |
703 | if (PTR_ERR(desc) == -EPROBE_DEFER) |
704 | return ERR_CAST(desc); | |
031ba28a LW |
705 | } |
706 | ||
707 | /* Then from plain _CRS GPIOs */ | |
708 | if (IS_ERR(desc)) { | |
709 | if (!acpi_can_fallback_to_crs(adev, con_id)) | |
710 | return ERR_PTR(-ENOENT); | |
711 | ||
712 | desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info); | |
713 | if (IS_ERR(desc)) | |
714 | return desc; | |
fe06b56c | 715 | } |
031ba28a | 716 | |
fe06b56c | 717 | if (info.gpioint && |
a31f5c3a | 718 | (*dflags == GPIOD_OUT_LOW || *dflags == GPIOD_OUT_HIGH)) { |
fe06b56c AS |
719 | dev_dbg(dev, "refusing GpioInt() entry when doing GPIOD_OUT_* lookup\n"); |
720 | return ERR_PTR(-ENOENT); | |
031ba28a LW |
721 | } |
722 | ||
723 | if (info.polarity == GPIO_ACTIVE_LOW) | |
724 | *lookupflags |= GPIO_ACTIVE_LOW; | |
725 | ||
a31f5c3a AS |
726 | err = acpi_gpio_update_gpiod_flags(dflags, info.flags); |
727 | if (err) | |
728 | dev_dbg(dev, "Override GPIO initialization flags\n"); | |
729 | ||
031ba28a LW |
730 | return desc; |
731 | } | |
732 | ||
504a3374 RW |
733 | /** |
734 | * acpi_node_get_gpiod() - get a GPIO descriptor from ACPI resources | |
735 | * @fwnode: pointer to an ACPI firmware node to get the GPIO information from | |
736 | * @propname: Property name of the GPIO | |
737 | * @index: index of GpioIo/GpioInt resource (starting from %0) | |
738 | * @info: info pointer to fill in (optional) | |
739 | * | |
740 | * If @fwnode is an ACPI device object, call %acpi_get_gpiod_by_index() for it. | |
741 | * Otherwise (ie. it is a data-only non-device object), use the property-based | |
742 | * GPIO lookup to get to the GPIO resource with the relevant information and use | |
743 | * that to obtain the GPIO descriptor to return. | |
744 | */ | |
745 | struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode, | |
746 | const char *propname, int index, | |
747 | struct acpi_gpio_info *info) | |
748 | { | |
749 | struct acpi_gpio_lookup lookup; | |
750 | struct acpi_device *adev; | |
751 | int ret; | |
12028d2d | 752 | |
504a3374 RW |
753 | adev = to_acpi_device_node(fwnode); |
754 | if (adev) | |
755 | return acpi_get_gpiod_by_index(adev, propname, index, info); | |
12028d2d | 756 | |
504a3374 RW |
757 | if (!is_acpi_data_node(fwnode)) |
758 | return ERR_PTR(-ENODEV); | |
759 | ||
760 | if (!propname) | |
761 | return ERR_PTR(-EINVAL); | |
762 | ||
763 | memset(&lookup, 0, sizeof(lookup)); | |
764 | lookup.index = index; | |
765 | ||
766 | ret = acpi_gpio_property_lookup(fwnode, propname, index, &lookup); | |
767 | if (ret) | |
768 | return ERR_PTR(ret); | |
12028d2d | 769 | |
504a3374 RW |
770 | ret = acpi_gpio_resource_lookup(&lookup, info); |
771 | return ret ? ERR_PTR(ret) : lookup.desc; | |
12028d2d | 772 | } |
664e3e5a | 773 | |
c884fbd4 MW |
774 | /** |
775 | * acpi_dev_gpio_irq_get() - Find GpioInt and translate it to Linux IRQ number | |
776 | * @adev: pointer to a ACPI device to get IRQ from | |
777 | * @index: index of GpioInt resource (starting from %0) | |
778 | * | |
779 | * If the device has one or more GpioInt resources, this function can be | |
780 | * used to translate from the GPIO offset in the resource to the Linux IRQ | |
781 | * number. | |
782 | * | |
a31f5c3a AS |
783 | * The function is idempotent, though each time it runs it will configure GPIO |
784 | * pin direction according to the flags in GpioInt resource. | |
785 | * | |
db05c7ef | 786 | * Return: Linux IRQ number (> %0) on success, negative errno on failure. |
c884fbd4 MW |
787 | */ |
788 | int acpi_dev_gpio_irq_get(struct acpi_device *adev, int index) | |
789 | { | |
790 | int idx, i; | |
52044723 | 791 | unsigned int irq_flags; |
a31f5c3a | 792 | int ret; |
c884fbd4 MW |
793 | |
794 | for (i = 0, idx = 0; idx <= index; i++) { | |
795 | struct acpi_gpio_info info; | |
796 | struct gpio_desc *desc; | |
797 | ||
798 | desc = acpi_get_gpiod_by_index(adev, NULL, i, &info); | |
6798d727 HG |
799 | |
800 | /* Ignore -EPROBE_DEFER, it only matters if idx matches */ | |
801 | if (IS_ERR(desc) && PTR_ERR(desc) != -EPROBE_DEFER) | |
802 | return PTR_ERR(desc); | |
803 | ||
52044723 | 804 | if (info.gpioint && idx++ == index) { |
a31f5c3a | 805 | char label[32]; |
6798d727 | 806 | int irq; |
52044723 | 807 | |
6798d727 HG |
808 | if (IS_ERR(desc)) |
809 | return PTR_ERR(desc); | |
52044723 | 810 | |
6798d727 | 811 | irq = gpiod_to_irq(desc); |
52044723 CR |
812 | if (irq < 0) |
813 | return irq; | |
814 | ||
a31f5c3a AS |
815 | snprintf(label, sizeof(label), "GpioInt() %d", index); |
816 | ret = gpiod_configure_flags(desc, label, 0, info.flags); | |
817 | if (ret < 0) | |
818 | return ret; | |
819 | ||
52044723 CR |
820 | irq_flags = acpi_dev_get_irq_type(info.triggering, |
821 | info.polarity); | |
822 | ||
823 | /* Set type if specified and different than the current one */ | |
824 | if (irq_flags != IRQ_TYPE_NONE && | |
825 | irq_flags != irq_get_trigger_type(irq)) | |
826 | irq_set_irq_type(irq, irq_flags); | |
827 | ||
828 | return irq; | |
829 | } | |
830 | ||
c884fbd4 | 831 | } |
6798d727 | 832 | return -ENOENT; |
c884fbd4 MW |
833 | } |
834 | EXPORT_SYMBOL_GPL(acpi_dev_gpio_irq_get); | |
835 | ||
473ed7be MW |
836 | static acpi_status |
837 | acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address, | |
838 | u32 bits, u64 *value, void *handler_context, | |
839 | void *region_context) | |
840 | { | |
841 | struct acpi_gpio_chip *achip = region_context; | |
842 | struct gpio_chip *chip = achip->chip; | |
843 | struct acpi_resource_gpio *agpio; | |
844 | struct acpi_resource *ares; | |
c15d821d | 845 | int pin_index = (int)address; |
473ed7be | 846 | acpi_status status; |
c15d821d | 847 | int length; |
473ed7be MW |
848 | int i; |
849 | ||
850 | status = acpi_buffer_to_resource(achip->conn_info.connection, | |
851 | achip->conn_info.length, &ares); | |
852 | if (ACPI_FAILURE(status)) | |
853 | return status; | |
854 | ||
855 | if (WARN_ON(ares->type != ACPI_RESOURCE_TYPE_GPIO)) { | |
856 | ACPI_FREE(ares); | |
857 | return AE_BAD_PARAMETER; | |
858 | } | |
859 | ||
860 | agpio = &ares->data.gpio; | |
473ed7be MW |
861 | |
862 | if (WARN_ON(agpio->io_restriction == ACPI_IO_RESTRICT_INPUT && | |
863 | function == ACPI_WRITE)) { | |
864 | ACPI_FREE(ares); | |
865 | return AE_BAD_PARAMETER; | |
866 | } | |
867 | ||
c15d821d SP |
868 | length = min(agpio->pin_table_length, (u16)(pin_index + bits)); |
869 | for (i = pin_index; i < length; ++i) { | |
a4811622 | 870 | int pin = agpio->pin_table[i]; |
473ed7be MW |
871 | struct acpi_gpio_connection *conn; |
872 | struct gpio_desc *desc; | |
873 | bool found; | |
874 | ||
473ed7be MW |
875 | mutex_lock(&achip->conn_lock); |
876 | ||
877 | found = false; | |
878 | list_for_each_entry(conn, &achip->conns, node) { | |
e46cf32c | 879 | if (conn->pin == pin) { |
473ed7be | 880 | found = true; |
e46cf32c | 881 | desc = conn->desc; |
473ed7be MW |
882 | break; |
883 | } | |
884 | } | |
c103a10f MW |
885 | |
886 | /* | |
887 | * The same GPIO can be shared between operation region and | |
888 | * event but only if the access here is ACPI_READ. In that | |
889 | * case we "borrow" the event GPIO instead. | |
890 | */ | |
891 | if (!found && agpio->sharable == ACPI_SHARED && | |
892 | function == ACPI_READ) { | |
893 | struct acpi_gpio_event *event; | |
894 | ||
895 | list_for_each_entry(event, &achip->events, node) { | |
896 | if (event->pin == pin) { | |
897 | desc = event->desc; | |
898 | found = true; | |
899 | break; | |
900 | } | |
901 | } | |
902 | } | |
903 | ||
473ed7be | 904 | if (!found) { |
2eca25af AS |
905 | enum gpiod_flags flags = acpi_gpio_to_gpiod_flags(agpio); |
906 | const char *label = "ACPI:OpRegion"; | |
907 | int err; | |
908 | ||
909 | desc = gpiochip_request_own_desc(chip, pin, label); | |
e46cf32c | 910 | if (IS_ERR(desc)) { |
473ed7be MW |
911 | status = AE_ERROR; |
912 | mutex_unlock(&achip->conn_lock); | |
913 | goto out; | |
914 | } | |
915 | ||
2eca25af AS |
916 | err = gpiod_configure_flags(desc, label, 0, flags); |
917 | if (err < 0) { | |
918 | status = AE_NOT_CONFIGURED; | |
919 | gpiochip_free_own_desc(desc); | |
920 | mutex_unlock(&achip->conn_lock); | |
921 | goto out; | |
473ed7be MW |
922 | } |
923 | ||
924 | conn = kzalloc(sizeof(*conn), GFP_KERNEL); | |
925 | if (!conn) { | |
926 | status = AE_NO_MEMORY; | |
927 | gpiochip_free_own_desc(desc); | |
928 | mutex_unlock(&achip->conn_lock); | |
929 | goto out; | |
930 | } | |
931 | ||
e46cf32c | 932 | conn->pin = pin; |
473ed7be MW |
933 | conn->desc = desc; |
934 | list_add_tail(&conn->node, &achip->conns); | |
935 | } | |
936 | ||
937 | mutex_unlock(&achip->conn_lock); | |
938 | ||
939 | if (function == ACPI_WRITE) | |
dc62b56a AL |
940 | gpiod_set_raw_value_cansleep(desc, |
941 | !!((1 << i) & *value)); | |
473ed7be | 942 | else |
dc62b56a | 943 | *value |= (u64)gpiod_get_raw_value_cansleep(desc) << i; |
473ed7be MW |
944 | } |
945 | ||
946 | out: | |
947 | ACPI_FREE(ares); | |
948 | return status; | |
949 | } | |
950 | ||
951 | static void acpi_gpiochip_request_regions(struct acpi_gpio_chip *achip) | |
952 | { | |
953 | struct gpio_chip *chip = achip->chip; | |
58383c78 | 954 | acpi_handle handle = ACPI_HANDLE(chip->parent); |
473ed7be MW |
955 | acpi_status status; |
956 | ||
957 | INIT_LIST_HEAD(&achip->conns); | |
958 | mutex_init(&achip->conn_lock); | |
959 | status = acpi_install_address_space_handler(handle, ACPI_ADR_SPACE_GPIO, | |
960 | acpi_gpio_adr_space_handler, | |
961 | NULL, achip); | |
962 | if (ACPI_FAILURE(status)) | |
58383c78 LW |
963 | dev_err(chip->parent, |
964 | "Failed to install GPIO OpRegion handler\n"); | |
473ed7be MW |
965 | } |
966 | ||
967 | static void acpi_gpiochip_free_regions(struct acpi_gpio_chip *achip) | |
968 | { | |
969 | struct gpio_chip *chip = achip->chip; | |
58383c78 | 970 | acpi_handle handle = ACPI_HANDLE(chip->parent); |
473ed7be MW |
971 | struct acpi_gpio_connection *conn, *tmp; |
972 | acpi_status status; | |
973 | ||
974 | status = acpi_remove_address_space_handler(handle, ACPI_ADR_SPACE_GPIO, | |
975 | acpi_gpio_adr_space_handler); | |
976 | if (ACPI_FAILURE(status)) { | |
58383c78 LW |
977 | dev_err(chip->parent, |
978 | "Failed to remove GPIO OpRegion handler\n"); | |
473ed7be MW |
979 | return; |
980 | } | |
981 | ||
982 | list_for_each_entry_safe_reverse(conn, tmp, &achip->conns, node) { | |
983 | gpiochip_free_own_desc(conn->desc); | |
984 | list_del(&conn->node); | |
985 | kfree(conn); | |
986 | } | |
987 | } | |
988 | ||
550a9532 WY |
989 | static struct gpio_desc *acpi_gpiochip_parse_own_gpio( |
990 | struct acpi_gpio_chip *achip, struct fwnode_handle *fwnode, | |
991 | const char **name, unsigned int *lflags, unsigned int *dflags) | |
c80f1ba7 MW |
992 | { |
993 | struct gpio_chip *chip = achip->chip; | |
994 | struct gpio_desc *desc; | |
995 | u32 gpios[2]; | |
996 | int ret; | |
997 | ||
c82064f2 AB |
998 | *lflags = 0; |
999 | *dflags = 0; | |
1000 | *name = NULL; | |
1001 | ||
c80f1ba7 MW |
1002 | ret = fwnode_property_read_u32_array(fwnode, "gpios", gpios, |
1003 | ARRAY_SIZE(gpios)); | |
1004 | if (ret < 0) | |
1005 | return ERR_PTR(ret); | |
1006 | ||
85ef722e | 1007 | desc = gpiochip_get_desc(chip, gpios[0]); |
c80f1ba7 MW |
1008 | if (IS_ERR(desc)) |
1009 | return desc; | |
1010 | ||
c80f1ba7 MW |
1011 | if (gpios[1]) |
1012 | *lflags |= GPIO_ACTIVE_LOW; | |
1013 | ||
1014 | if (fwnode_property_present(fwnode, "input")) | |
1015 | *dflags |= GPIOD_IN; | |
1016 | else if (fwnode_property_present(fwnode, "output-low")) | |
1017 | *dflags |= GPIOD_OUT_LOW; | |
1018 | else if (fwnode_property_present(fwnode, "output-high")) | |
1019 | *dflags |= GPIOD_OUT_HIGH; | |
1020 | else | |
1021 | return ERR_PTR(-EINVAL); | |
1022 | ||
1023 | fwnode_property_read_string(fwnode, "line-name", name); | |
1024 | ||
1025 | return desc; | |
1026 | } | |
1027 | ||
1028 | static void acpi_gpiochip_scan_gpios(struct acpi_gpio_chip *achip) | |
1029 | { | |
1030 | struct gpio_chip *chip = achip->chip; | |
1031 | struct fwnode_handle *fwnode; | |
1032 | ||
1033 | device_for_each_child_node(chip->parent, fwnode) { | |
1034 | unsigned int lflags, dflags; | |
1035 | struct gpio_desc *desc; | |
1036 | const char *name; | |
1037 | int ret; | |
1038 | ||
1039 | if (!fwnode_property_present(fwnode, "gpio-hog")) | |
1040 | continue; | |
1041 | ||
1042 | desc = acpi_gpiochip_parse_own_gpio(achip, fwnode, &name, | |
1043 | &lflags, &dflags); | |
1044 | if (IS_ERR(desc)) | |
1045 | continue; | |
1046 | ||
1047 | ret = gpiod_hog(desc, name, lflags, dflags); | |
1048 | if (ret) { | |
1049 | dev_err(chip->parent, "Failed to hog GPIO\n"); | |
1b6998c9 | 1050 | fwnode_handle_put(fwnode); |
c80f1ba7 MW |
1051 | return; |
1052 | } | |
1053 | } | |
1054 | } | |
1055 | ||
664e3e5a MW |
1056 | void acpi_gpiochip_add(struct gpio_chip *chip) |
1057 | { | |
aa92b6f6 MW |
1058 | struct acpi_gpio_chip *acpi_gpio; |
1059 | acpi_handle handle; | |
1060 | acpi_status status; | |
1061 | ||
58383c78 | 1062 | if (!chip || !chip->parent) |
e9595f84 MW |
1063 | return; |
1064 | ||
58383c78 | 1065 | handle = ACPI_HANDLE(chip->parent); |
aa92b6f6 MW |
1066 | if (!handle) |
1067 | return; | |
1068 | ||
1069 | acpi_gpio = kzalloc(sizeof(*acpi_gpio), GFP_KERNEL); | |
1070 | if (!acpi_gpio) { | |
58383c78 | 1071 | dev_err(chip->parent, |
aa92b6f6 MW |
1072 | "Failed to allocate memory for ACPI GPIO chip\n"); |
1073 | return; | |
1074 | } | |
1075 | ||
1076 | acpi_gpio->chip = chip; | |
c103a10f | 1077 | INIT_LIST_HEAD(&acpi_gpio->events); |
2efa9e0b | 1078 | INIT_LIST_HEAD(&acpi_gpio->deferred_req_irqs_list_entry); |
aa92b6f6 MW |
1079 | |
1080 | status = acpi_attach_data(handle, acpi_gpio_chip_dh, acpi_gpio); | |
1081 | if (ACPI_FAILURE(status)) { | |
58383c78 | 1082 | dev_err(chip->parent, "Failed to attach ACPI GPIO chip\n"); |
aa92b6f6 MW |
1083 | kfree(acpi_gpio); |
1084 | return; | |
1085 | } | |
1086 | ||
4035cc15 | 1087 | if (!chip->names) |
82270335 | 1088 | devprop_gpiochip_set_names(chip, dev_fwnode(chip->parent)); |
4035cc15 | 1089 | |
473ed7be | 1090 | acpi_gpiochip_request_regions(acpi_gpio); |
c80f1ba7 | 1091 | acpi_gpiochip_scan_gpios(acpi_gpio); |
3f86a635 | 1092 | acpi_walk_dep_device_list(handle); |
664e3e5a MW |
1093 | } |
1094 | ||
1095 | void acpi_gpiochip_remove(struct gpio_chip *chip) | |
1096 | { | |
aa92b6f6 MW |
1097 | struct acpi_gpio_chip *acpi_gpio; |
1098 | acpi_handle handle; | |
1099 | acpi_status status; | |
1100 | ||
58383c78 | 1101 | if (!chip || !chip->parent) |
e9595f84 MW |
1102 | return; |
1103 | ||
58383c78 | 1104 | handle = ACPI_HANDLE(chip->parent); |
aa92b6f6 MW |
1105 | if (!handle) |
1106 | return; | |
1107 | ||
1108 | status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio); | |
1109 | if (ACPI_FAILURE(status)) { | |
58383c78 | 1110 | dev_warn(chip->parent, "Failed to retrieve ACPI GPIO chip\n"); |
aa92b6f6 MW |
1111 | return; |
1112 | } | |
1113 | ||
473ed7be | 1114 | acpi_gpiochip_free_regions(acpi_gpio); |
aa92b6f6 MW |
1115 | |
1116 | acpi_detach_data(handle, acpi_gpio_chip_dh); | |
1117 | kfree(acpi_gpio); | |
664e3e5a | 1118 | } |
66858527 | 1119 | |
6f7194a1 | 1120 | static int acpi_gpio_package_count(const union acpi_object *obj) |
66858527 RI |
1121 | { |
1122 | const union acpi_object *element = obj->package.elements; | |
1123 | const union acpi_object *end = element + obj->package.count; | |
1124 | unsigned int count = 0; | |
1125 | ||
1126 | while (element < end) { | |
6f7194a1 MW |
1127 | switch (element->type) { |
1128 | case ACPI_TYPE_LOCAL_REFERENCE: | |
1129 | element += 3; | |
1130 | /* Fallthrough */ | |
1131 | case ACPI_TYPE_INTEGER: | |
1132 | element++; | |
66858527 | 1133 | count++; |
6f7194a1 | 1134 | break; |
66858527 | 1135 | |
6f7194a1 MW |
1136 | default: |
1137 | return -EPROTO; | |
1138 | } | |
66858527 | 1139 | } |
6f7194a1 | 1140 | |
66858527 RI |
1141 | return count; |
1142 | } | |
1143 | ||
1144 | static int acpi_find_gpio_count(struct acpi_resource *ares, void *data) | |
1145 | { | |
1146 | unsigned int *count = data; | |
1147 | ||
1148 | if (ares->type == ACPI_RESOURCE_TYPE_GPIO) | |
1149 | *count += ares->data.gpio.pin_table_length; | |
1150 | ||
1151 | return 1; | |
1152 | } | |
1153 | ||
1154 | /** | |
1155 | * acpi_gpio_count - return the number of GPIOs associated with a | |
1156 | * device / function or -ENOENT if no GPIO has been | |
1157 | * assigned to the requested function. | |
1158 | * @dev: GPIO consumer, can be NULL for system-global GPIOs | |
1159 | * @con_id: function within the GPIO consumer | |
1160 | */ | |
1161 | int acpi_gpio_count(struct device *dev, const char *con_id) | |
1162 | { | |
1163 | struct acpi_device *adev = ACPI_COMPANION(dev); | |
1164 | const union acpi_object *obj; | |
1165 | const struct acpi_gpio_mapping *gm; | |
1166 | int count = -ENOENT; | |
1167 | int ret; | |
1168 | char propname[32]; | |
1169 | unsigned int i; | |
1170 | ||
1171 | /* Try first from _DSD */ | |
1172 | for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { | |
9e66504a | 1173 | if (con_id) |
66858527 RI |
1174 | snprintf(propname, sizeof(propname), "%s-%s", |
1175 | con_id, gpio_suffixes[i]); | |
1176 | else | |
1177 | snprintf(propname, sizeof(propname), "%s", | |
1178 | gpio_suffixes[i]); | |
1179 | ||
1180 | ret = acpi_dev_get_property(adev, propname, ACPI_TYPE_ANY, | |
1181 | &obj); | |
1182 | if (ret == 0) { | |
1183 | if (obj->type == ACPI_TYPE_LOCAL_REFERENCE) | |
1184 | count = 1; | |
1185 | else if (obj->type == ACPI_TYPE_PACKAGE) | |
1186 | count = acpi_gpio_package_count(obj); | |
1187 | } else if (adev->driver_gpios) { | |
1188 | for (gm = adev->driver_gpios; gm->name; gm++) | |
1189 | if (strcmp(propname, gm->name) == 0) { | |
1190 | count = gm->size; | |
1191 | break; | |
1192 | } | |
1193 | } | |
4ed55016 | 1194 | if (count > 0) |
66858527 RI |
1195 | break; |
1196 | } | |
1197 | ||
1198 | /* Then from plain _CRS GPIOs */ | |
1199 | if (count < 0) { | |
1200 | struct list_head resource_list; | |
1201 | unsigned int crs_count = 0; | |
1202 | ||
6fe9da42 AS |
1203 | if (!acpi_can_fallback_to_crs(adev, con_id)) |
1204 | return count; | |
1205 | ||
66858527 RI |
1206 | INIT_LIST_HEAD(&resource_list); |
1207 | acpi_dev_get_resources(adev, &resource_list, | |
1208 | acpi_find_gpio_count, &crs_count); | |
1209 | acpi_dev_free_resource_list(&resource_list); | |
1210 | if (crs_count > 0) | |
1211 | count = crs_count; | |
1212 | } | |
4ed55016 | 1213 | return count ? count : -ENOENT; |
66858527 | 1214 | } |
10cf4899 | 1215 | |
10cf4899 DT |
1216 | bool acpi_can_fallback_to_crs(struct acpi_device *adev, const char *con_id) |
1217 | { | |
10cf4899 | 1218 | /* Never allow fallback if the device has properties */ |
997d2b15 | 1219 | if (acpi_dev_has_props(adev) || adev->driver_gpios) |
10cf4899 DT |
1220 | return false; |
1221 | ||
f10e4bf6 | 1222 | return con_id == NULL; |
10cf4899 | 1223 | } |
4ea03f3e | 1224 | |
a4adb921 HG |
1225 | /* Run deferred acpi_gpiochip_request_irqs() */ |
1226 | static int acpi_gpio_handle_deferred_request_irqs(void) | |
4ea03f3e | 1227 | { |
2efa9e0b HG |
1228 | struct acpi_gpio_chip *acpi_gpio, *tmp; |
1229 | ||
1230 | mutex_lock(&acpi_gpio_deferred_req_irqs_lock); | |
1231 | list_for_each_entry_safe(acpi_gpio, tmp, | |
1232 | &acpi_gpio_deferred_req_irqs_list, | |
a4adb921 HG |
1233 | deferred_req_irqs_list_entry) |
1234 | acpi_gpiochip_request_irqs(acpi_gpio); | |
2efa9e0b HG |
1235 | |
1236 | acpi_gpio_deferred_req_irqs_done = true; | |
1237 | mutex_unlock(&acpi_gpio_deferred_req_irqs_lock); | |
4ea03f3e BT |
1238 | |
1239 | return 0; | |
1240 | } | |
1241 | /* We must use _sync so that this runs after the first deferred_probe run */ | |
a4adb921 | 1242 | late_initcall_sync(acpi_gpio_handle_deferred_request_irqs); |
a7e0416d HG |
1243 | |
1244 | static const struct dmi_system_id run_edge_events_on_boot_blacklist[] = { | |
1245 | { | |
27636e72 HG |
1246 | /* |
1247 | * The Minix Neo Z83-4 has a micro-USB-B id-pin handler for | |
1248 | * a non existing micro-USB-B connector which puts the HDMI | |
1249 | * DDC pins in GPIO mode, breaking HDMI support. | |
1250 | */ | |
a7e0416d HG |
1251 | .matches = { |
1252 | DMI_MATCH(DMI_SYS_VENDOR, "MINIX"), | |
1253 | DMI_MATCH(DMI_PRODUCT_NAME, "Z83-4"), | |
1254 | } | |
1255 | }, | |
27636e72 HG |
1256 | { |
1257 | /* | |
1258 | * The Terra Pad 1061 has a micro-USB-B id-pin handler, which | |
1259 | * instead of controlling the actual micro-USB-B turns the 5V | |
1260 | * boost for its USB-A connector off. The actual micro-USB-B | |
1261 | * connector is wired for charging only. | |
1262 | */ | |
1263 | .matches = { | |
1264 | DMI_MATCH(DMI_SYS_VENDOR, "Wortmann_AG"), | |
1265 | DMI_MATCH(DMI_PRODUCT_NAME, "TERRA_PAD_1061"), | |
1266 | } | |
1267 | }, | |
a7e0416d HG |
1268 | {} /* Terminating entry */ |
1269 | }; | |
1270 | ||
1271 | static int acpi_gpio_setup_params(void) | |
1272 | { | |
1273 | if (run_edge_events_on_boot < 0) { | |
1274 | if (dmi_check_system(run_edge_events_on_boot_blacklist)) | |
1275 | run_edge_events_on_boot = 0; | |
1276 | else | |
1277 | run_edge_events_on_boot = 1; | |
1278 | } | |
1279 | ||
1280 | return 0; | |
1281 | } | |
1282 | ||
1283 | /* Directly after dmi_setup() which runs as core_initcall() */ | |
1284 | postcore_initcall(acpi_gpio_setup_params); |