]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/gpio/gpiolib-acpi.c
gpio: acpi: normalize use of gpiochip_get_desc()
[mirror_ubuntu-hirsute-kernel.git] / drivers / gpio / gpiolib-acpi.c
CommitLineData
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
13#include <linux/errno.h>
936e15dd 14#include <linux/gpio/consumer.h>
5ccff852 15#include <linux/gpio/driver.h>
e29482e8 16#include <linux/export.h>
e29482e8 17#include <linux/acpi.h>
0d1c28a4 18#include <linux/interrupt.h>
473ed7be 19#include <linux/mutex.h>
e29482e8 20
5ccff852
MW
21#include "gpiolib.h"
22
4b01a14b 23struct acpi_gpio_event {
7fc7acb9 24 struct list_head node;
4b01a14b 25 acpi_handle handle;
7fc7acb9
RW
26 unsigned int pin;
27 unsigned int irq;
e46cf32c 28 struct gpio_desc *desc;
7fc7acb9
RW
29};
30
473ed7be
MW
31struct acpi_gpio_connection {
32 struct list_head node;
e46cf32c 33 unsigned int pin;
473ed7be
MW
34 struct gpio_desc *desc;
35};
36
aa92b6f6 37struct acpi_gpio_chip {
473ed7be
MW
38 /*
39 * ACPICA requires that the first field of the context parameter
40 * passed to acpi_install_address_space_handler() is large enough
41 * to hold struct acpi_connection_info.
42 */
43 struct acpi_connection_info conn_info;
44 struct list_head conns;
45 struct mutex conn_lock;
aa92b6f6 46 struct gpio_chip *chip;
4b01a14b 47 struct list_head events;
aa92b6f6
MW
48};
49
e29482e8
MN
50static int acpi_gpiochip_find(struct gpio_chip *gc, void *data)
51{
52 if (!gc->dev)
53 return false;
54
55 return ACPI_HANDLE(gc->dev) == data;
56}
57
58/**
936e15dd 59 * acpi_get_gpiod() - Translate ACPI GPIO pin to GPIO descriptor usable with GPIO API
e29482e8
MN
60 * @path: ACPI GPIO controller full path name, (e.g. "\\_SB.GPO1")
61 * @pin: ACPI GPIO pin number (0-based, controller-relative)
62 *
936e15dd
MW
63 * Returns GPIO descriptor to use with Linux generic GPIO API, or ERR_PTR
64 * error value
e29482e8
MN
65 */
66
936e15dd 67static struct gpio_desc *acpi_get_gpiod(char *path, int pin)
e29482e8
MN
68{
69 struct gpio_chip *chip;
70 acpi_handle handle;
71 acpi_status status;
72
73 status = acpi_get_handle(NULL, path, &handle);
74 if (ACPI_FAILURE(status))
936e15dd 75 return ERR_PTR(-ENODEV);
e29482e8
MN
76
77 chip = gpiochip_find(handle, acpi_gpiochip_find);
78 if (!chip)
936e15dd 79 return ERR_PTR(-ENODEV);
e29482e8 80
936e15dd
MW
81 if (pin < 0 || pin > chip->ngpio)
82 return ERR_PTR(-EINVAL);
e29482e8 83
390d82e3 84 return gpiochip_get_desc(chip, pin);
e29482e8 85}
0d1c28a4 86
0d1c28a4
MN
87static irqreturn_t acpi_gpio_irq_handler(int irq, void *data)
88{
6072b9dc 89 struct acpi_gpio_event *event = data;
0d1c28a4 90
6072b9dc 91 acpi_evaluate_object(event->handle, NULL, NULL, NULL);
0d1c28a4
MN
92
93 return IRQ_HANDLED;
94}
95
7fc7acb9
RW
96static irqreturn_t acpi_gpio_irq_handler_evt(int irq, void *data)
97{
4b01a14b 98 struct acpi_gpio_event *event = data;
7fc7acb9 99
4b01a14b 100 acpi_execute_simple_method(event->handle, NULL, event->pin);
7fc7acb9
RW
101
102 return IRQ_HANDLED;
103}
104
aa92b6f6 105static void acpi_gpio_chip_dh(acpi_handle handle, void *data)
7fc7acb9
RW
106{
107 /* The address of this function is used as a key. */
108}
109
6072b9dc
MW
110static acpi_status acpi_gpiochip_request_interrupt(struct acpi_resource *ares,
111 void *context)
0d1c28a4 112{
6072b9dc 113 struct acpi_gpio_chip *acpi_gpio = context;
aa92b6f6 114 struct gpio_chip *chip = acpi_gpio->chip;
6072b9dc 115 struct acpi_resource_gpio *agpio;
7fc7acb9 116 acpi_handle handle, evt_handle;
6072b9dc
MW
117 struct acpi_gpio_event *event;
118 irq_handler_t handler = NULL;
119 struct gpio_desc *desc;
120 unsigned long irqflags;
121 int ret, pin, irq;
0d1c28a4 122
6072b9dc
MW
123 if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
124 return AE_OK;
125
126 agpio = &ares->data.gpio;
127 if (agpio->connection_type != ACPI_RESOURCE_GPIO_TYPE_INT)
128 return AE_OK;
0d1c28a4
MN
129
130 handle = ACPI_HANDLE(chip->dev);
6072b9dc
MW
131 pin = agpio->pin_table[0];
132
133 if (pin <= 255) {
134 char ev_name[5];
135 sprintf(ev_name, "_%c%02X",
136 agpio->triggering == ACPI_EDGE_SENSITIVE ? 'E' : 'L',
137 pin);
138 if (ACPI_SUCCESS(acpi_get_handle(handle, ev_name, &evt_handle)))
139 handler = acpi_gpio_irq_handler;
140 }
141 if (!handler) {
142 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EVT", &evt_handle)))
143 handler = acpi_gpio_irq_handler_evt;
144 }
145 if (!handler)
146 return AE_BAD_PARAMETER;
0d1c28a4 147
6072b9dc
MW
148 desc = gpiochip_get_desc(chip, pin);
149 if (IS_ERR(desc)) {
150 dev_err(chip->dev, "Failed to get GPIO descriptor\n");
151 return AE_ERROR;
152 }
0d1c28a4 153
6072b9dc
MW
154 ret = gpiochip_request_own_desc(desc, "ACPI:Event");
155 if (ret) {
156 dev_err(chip->dev, "Failed to request GPIO\n");
157 return AE_ERROR;
158 }
0d1c28a4 159
6072b9dc 160 gpiod_direction_input(desc);
0d1c28a4 161
d74be6df 162 ret = gpio_lock_as_irq(chip, pin);
6072b9dc
MW
163 if (ret) {
164 dev_err(chip->dev, "Failed to lock GPIO as interrupt\n");
165 goto fail_free_desc;
166 }
0d1c28a4 167
6072b9dc
MW
168 irq = gpiod_to_irq(desc);
169 if (irq < 0) {
170 dev_err(chip->dev, "Failed to translate GPIO to IRQ\n");
171 goto fail_unlock_irq;
172 }
7fc7acb9 173
6072b9dc
MW
174 irqflags = IRQF_ONESHOT;
175 if (agpio->triggering == ACPI_LEVEL_SENSITIVE) {
176 if (agpio->polarity == ACPI_ACTIVE_HIGH)
177 irqflags |= IRQF_TRIGGER_HIGH;
178 else
179 irqflags |= IRQF_TRIGGER_LOW;
180 } else {
181 switch (agpio->polarity) {
182 case ACPI_ACTIVE_HIGH:
183 irqflags |= IRQF_TRIGGER_RISING;
184 break;
185 case ACPI_ACTIVE_LOW:
186 irqflags |= IRQF_TRIGGER_FALLING;
187 break;
188 default:
189 irqflags |= IRQF_TRIGGER_RISING |
190 IRQF_TRIGGER_FALLING;
191 break;
7fc7acb9 192 }
6072b9dc 193 }
aa92b6f6 194
6072b9dc
MW
195 event = kzalloc(sizeof(*event), GFP_KERNEL);
196 if (!event)
197 goto fail_unlock_irq;
7fc7acb9 198
6072b9dc
MW
199 event->handle = evt_handle;
200 event->irq = irq;
201 event->pin = pin;
e46cf32c 202 event->desc = desc;
7fc7acb9 203
6072b9dc
MW
204 ret = request_threaded_irq(event->irq, NULL, handler, irqflags,
205 "ACPI:Event", event);
206 if (ret) {
207 dev_err(chip->dev, "Failed to setup interrupt handler for %d\n",
208 event->irq);
209 goto fail_free_event;
0d1c28a4 210 }
6072b9dc
MW
211
212 list_add_tail(&event->node, &acpi_gpio->events);
213 return AE_OK;
214
215fail_free_event:
216 kfree(event);
217fail_unlock_irq:
d74be6df 218 gpio_unlock_as_irq(chip, pin);
6072b9dc
MW
219fail_free_desc:
220 gpiochip_free_own_desc(desc);
221
222 return AE_ERROR;
0d1c28a4 223}
7fc7acb9 224
70b53411 225/**
6072b9dc 226 * acpi_gpiochip_request_interrupts() - Register isr for gpio chip ACPI events
afa82fab 227 * @chip: GPIO chip
70b53411 228 *
6072b9dc
MW
229 * ACPI5 platforms can use GPIO signaled ACPI events. These GPIO interrupts are
230 * handled by ACPI event methods which need to be called from the GPIO
231 * chip's interrupt handler. acpi_gpiochip_request_interrupts finds out which
232 * gpio pins have acpi event methods and assigns interrupt handlers that calls
233 * the acpi event methods for those pins.
234 */
afa82fab 235void acpi_gpiochip_request_interrupts(struct gpio_chip *chip)
6072b9dc 236{
afa82fab
MW
237 struct acpi_gpio_chip *acpi_gpio;
238 acpi_handle handle;
239 acpi_status status;
240
241 if (!chip->dev || !chip->to_irq)
242 return;
6072b9dc 243
afa82fab
MW
244 handle = ACPI_HANDLE(chip->dev);
245 if (!handle)
246 return;
247
248 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
249 if (ACPI_FAILURE(status))
6072b9dc
MW
250 return;
251
252 INIT_LIST_HEAD(&acpi_gpio->events);
253 acpi_walk_resources(ACPI_HANDLE(chip->dev), "_AEI",
254 acpi_gpiochip_request_interrupt, acpi_gpio);
255}
256
257/**
258 * acpi_gpiochip_free_interrupts() - Free GPIO ACPI event interrupts.
afa82fab 259 * @chip: GPIO chip
70b53411 260 *
6072b9dc
MW
261 * Free interrupts associated with GPIO ACPI event method for the given
262 * GPIO chip.
70b53411 263 */
afa82fab 264void acpi_gpiochip_free_interrupts(struct gpio_chip *chip)
70b53411 265{
afa82fab 266 struct acpi_gpio_chip *acpi_gpio;
4b01a14b 267 struct acpi_gpio_event *event, *ep;
afa82fab
MW
268 acpi_handle handle;
269 acpi_status status;
270
271 if (!chip->dev || !chip->to_irq)
272 return;
70b53411 273
afa82fab
MW
274 handle = ACPI_HANDLE(chip->dev);
275 if (!handle)
276 return;
277
278 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
279 if (ACPI_FAILURE(status))
70b53411
MW
280 return;
281
4b01a14b 282 list_for_each_entry_safe_reverse(event, ep, &acpi_gpio->events, node) {
6072b9dc
MW
283 struct gpio_desc *desc;
284
285 free_irq(event->irq, event);
e46cf32c 286 desc = event->desc;
6072b9dc
MW
287 if (WARN_ON(IS_ERR(desc)))
288 continue;
d74be6df 289 gpio_unlock_as_irq(chip, event->pin);
6072b9dc 290 gpiochip_free_own_desc(desc);
4b01a14b
MW
291 list_del(&event->node);
292 kfree(event);
70b53411 293 }
70b53411 294}
70b53411 295
12028d2d
MW
296struct acpi_gpio_lookup {
297 struct acpi_gpio_info info;
298 int index;
936e15dd 299 struct gpio_desc *desc;
12028d2d
MW
300 int n;
301};
302
303static int acpi_find_gpio(struct acpi_resource *ares, void *data)
304{
305 struct acpi_gpio_lookup *lookup = data;
306
307 if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
308 return 1;
309
936e15dd 310 if (lookup->n++ == lookup->index && !lookup->desc) {
12028d2d
MW
311 const struct acpi_resource_gpio *agpio = &ares->data.gpio;
312
936e15dd
MW
313 lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr,
314 agpio->pin_table[0]);
12028d2d
MW
315 lookup->info.gpioint =
316 agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
e01f440a
MW
317 lookup->info.active_low =
318 agpio->polarity == ACPI_ACTIVE_LOW;
12028d2d
MW
319 }
320
321 return 1;
322}
323
324/**
936e15dd 325 * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources
12028d2d
MW
326 * @dev: pointer to a device to get GPIO from
327 * @index: index of GpioIo/GpioInt resource (starting from %0)
328 * @info: info pointer to fill in (optional)
329 *
330 * Function goes through ACPI resources for @dev and based on @index looks
936e15dd 331 * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor,
12028d2d
MW
332 * and returns it. @index matches GpioIo/GpioInt resources only so if there
333 * are total %3 GPIO resources, the index goes from %0 to %2.
334 *
936e15dd 335 * If the GPIO cannot be translated or there is an error an ERR_PTR is
12028d2d
MW
336 * returned.
337 *
338 * Note: if the GPIO resource has multiple entries in the pin list, this
339 * function only returns the first.
340 */
936e15dd
MW
341struct gpio_desc *acpi_get_gpiod_by_index(struct device *dev, int index,
342 struct acpi_gpio_info *info)
12028d2d
MW
343{
344 struct acpi_gpio_lookup lookup;
345 struct list_head resource_list;
346 struct acpi_device *adev;
347 acpi_handle handle;
348 int ret;
349
350 if (!dev)
936e15dd 351 return ERR_PTR(-EINVAL);
12028d2d
MW
352
353 handle = ACPI_HANDLE(dev);
354 if (!handle || acpi_bus_get_device(handle, &adev))
936e15dd 355 return ERR_PTR(-ENODEV);
12028d2d
MW
356
357 memset(&lookup, 0, sizeof(lookup));
358 lookup.index = index;
12028d2d
MW
359
360 INIT_LIST_HEAD(&resource_list);
361 ret = acpi_dev_get_resources(adev, &resource_list, acpi_find_gpio,
362 &lookup);
363 if (ret < 0)
936e15dd 364 return ERR_PTR(ret);
12028d2d
MW
365
366 acpi_dev_free_resource_list(&resource_list);
367
936e15dd 368 if (lookup.desc && info)
12028d2d
MW
369 *info = lookup.info;
370
a00580c2 371 return lookup.desc ? lookup.desc : ERR_PTR(-ENOENT);
12028d2d 372}
664e3e5a 373
473ed7be
MW
374static acpi_status
375acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
376 u32 bits, u64 *value, void *handler_context,
377 void *region_context)
378{
379 struct acpi_gpio_chip *achip = region_context;
380 struct gpio_chip *chip = achip->chip;
381 struct acpi_resource_gpio *agpio;
382 struct acpi_resource *ares;
383 acpi_status status;
384 bool pull_up;
385 int i;
386
387 status = acpi_buffer_to_resource(achip->conn_info.connection,
388 achip->conn_info.length, &ares);
389 if (ACPI_FAILURE(status))
390 return status;
391
392 if (WARN_ON(ares->type != ACPI_RESOURCE_TYPE_GPIO)) {
393 ACPI_FREE(ares);
394 return AE_BAD_PARAMETER;
395 }
396
397 agpio = &ares->data.gpio;
398 pull_up = agpio->pin_config == ACPI_PIN_CONFIG_PULLUP;
399
400 if (WARN_ON(agpio->io_restriction == ACPI_IO_RESTRICT_INPUT &&
401 function == ACPI_WRITE)) {
402 ACPI_FREE(ares);
403 return AE_BAD_PARAMETER;
404 }
405
406 for (i = 0; i < agpio->pin_table_length; i++) {
407 unsigned pin = agpio->pin_table[i];
408 struct acpi_gpio_connection *conn;
409 struct gpio_desc *desc;
410 bool found;
411
473ed7be
MW
412 mutex_lock(&achip->conn_lock);
413
414 found = false;
415 list_for_each_entry(conn, &achip->conns, node) {
e46cf32c 416 if (conn->pin == pin) {
473ed7be 417 found = true;
e46cf32c 418 desc = conn->desc;
473ed7be
MW
419 break;
420 }
421 }
422 if (!found) {
423 int ret;
424
e46cf32c
AC
425 desc = gpiochip_get_desc(chip, pin);
426 if (IS_ERR(desc)) {
427 status = AE_ERROR;
428 mutex_unlock(&achip->conn_lock);
429 goto out;
430 }
431
473ed7be
MW
432 ret = gpiochip_request_own_desc(desc, "ACPI:OpRegion");
433 if (ret) {
434 status = AE_ERROR;
435 mutex_unlock(&achip->conn_lock);
436 goto out;
437 }
438
439 switch (agpio->io_restriction) {
440 case ACPI_IO_RESTRICT_INPUT:
441 gpiod_direction_input(desc);
442 break;
443 case ACPI_IO_RESTRICT_OUTPUT:
444 /*
445 * ACPI GPIO resources don't contain an
446 * initial value for the GPIO. Therefore we
447 * deduce that value from the pull field
448 * instead. If the pin is pulled up we
449 * assume default to be high, otherwise
450 * low.
451 */
452 gpiod_direction_output(desc, pull_up);
453 break;
454 default:
455 /*
456 * Assume that the BIOS has configured the
457 * direction and pull accordingly.
458 */
459 break;
460 }
461
462 conn = kzalloc(sizeof(*conn), GFP_KERNEL);
463 if (!conn) {
464 status = AE_NO_MEMORY;
465 gpiochip_free_own_desc(desc);
466 mutex_unlock(&achip->conn_lock);
467 goto out;
468 }
469
e46cf32c 470 conn->pin = pin;
473ed7be
MW
471 conn->desc = desc;
472 list_add_tail(&conn->node, &achip->conns);
473 }
474
475 mutex_unlock(&achip->conn_lock);
476
477 if (function == ACPI_WRITE)
dc62b56a
AL
478 gpiod_set_raw_value_cansleep(desc,
479 !!((1 << i) & *value));
473ed7be 480 else
dc62b56a 481 *value |= (u64)gpiod_get_raw_value_cansleep(desc) << i;
473ed7be
MW
482 }
483
484out:
485 ACPI_FREE(ares);
486 return status;
487}
488
489static void acpi_gpiochip_request_regions(struct acpi_gpio_chip *achip)
490{
491 struct gpio_chip *chip = achip->chip;
492 acpi_handle handle = ACPI_HANDLE(chip->dev);
493 acpi_status status;
494
495 INIT_LIST_HEAD(&achip->conns);
496 mutex_init(&achip->conn_lock);
497 status = acpi_install_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
498 acpi_gpio_adr_space_handler,
499 NULL, achip);
500 if (ACPI_FAILURE(status))
501 dev_err(chip->dev, "Failed to install GPIO OpRegion handler\n");
502}
503
504static void acpi_gpiochip_free_regions(struct acpi_gpio_chip *achip)
505{
506 struct gpio_chip *chip = achip->chip;
507 acpi_handle handle = ACPI_HANDLE(chip->dev);
508 struct acpi_gpio_connection *conn, *tmp;
509 acpi_status status;
510
511 status = acpi_remove_address_space_handler(handle, ACPI_ADR_SPACE_GPIO,
512 acpi_gpio_adr_space_handler);
513 if (ACPI_FAILURE(status)) {
514 dev_err(chip->dev, "Failed to remove GPIO OpRegion handler\n");
515 return;
516 }
517
518 list_for_each_entry_safe_reverse(conn, tmp, &achip->conns, node) {
519 gpiochip_free_own_desc(conn->desc);
520 list_del(&conn->node);
521 kfree(conn);
522 }
523}
524
664e3e5a
MW
525void acpi_gpiochip_add(struct gpio_chip *chip)
526{
aa92b6f6
MW
527 struct acpi_gpio_chip *acpi_gpio;
528 acpi_handle handle;
529 acpi_status status;
530
e9595f84
MW
531 if (!chip || !chip->dev)
532 return;
533
aa92b6f6
MW
534 handle = ACPI_HANDLE(chip->dev);
535 if (!handle)
536 return;
537
538 acpi_gpio = kzalloc(sizeof(*acpi_gpio), GFP_KERNEL);
539 if (!acpi_gpio) {
540 dev_err(chip->dev,
541 "Failed to allocate memory for ACPI GPIO chip\n");
542 return;
543 }
544
545 acpi_gpio->chip = chip;
546
547 status = acpi_attach_data(handle, acpi_gpio_chip_dh, acpi_gpio);
548 if (ACPI_FAILURE(status)) {
549 dev_err(chip->dev, "Failed to attach ACPI GPIO chip\n");
550 kfree(acpi_gpio);
551 return;
552 }
553
473ed7be 554 acpi_gpiochip_request_regions(acpi_gpio);
664e3e5a
MW
555}
556
557void acpi_gpiochip_remove(struct gpio_chip *chip)
558{
aa92b6f6
MW
559 struct acpi_gpio_chip *acpi_gpio;
560 acpi_handle handle;
561 acpi_status status;
562
e9595f84
MW
563 if (!chip || !chip->dev)
564 return;
565
aa92b6f6
MW
566 handle = ACPI_HANDLE(chip->dev);
567 if (!handle)
568 return;
569
570 status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
571 if (ACPI_FAILURE(status)) {
572 dev_warn(chip->dev, "Failed to retrieve ACPI GPIO chip\n");
573 return;
574 }
575
473ed7be 576 acpi_gpiochip_free_regions(acpi_gpio);
aa92b6f6
MW
577
578 acpi_detach_data(handle, acpi_gpio_chip_dh);
579 kfree(acpi_gpio);
664e3e5a 580}