linux/drivers/gpio/gpiolib-acpi.c
<<
>>
Prefs
   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>
  14#include <linux/gpio/consumer.h>
  15#include <linux/gpio/driver.h>
  16#include <linux/export.h>
  17#include <linux/acpi.h>
  18#include <linux/interrupt.h>
  19
  20#include "gpiolib.h"
  21
  22struct acpi_gpio_evt_pin {
  23        struct list_head node;
  24        acpi_handle *evt_handle;
  25        unsigned int pin;
  26        unsigned int irq;
  27};
  28
  29static int acpi_gpiochip_find(struct gpio_chip *gc, void *data)
  30{
  31        if (!gc->dev)
  32                return false;
  33
  34        return ACPI_HANDLE(gc->dev) == data;
  35}
  36
  37/**
  38 * acpi_get_gpiod() - Translate ACPI GPIO pin to GPIO descriptor usable with GPIO API
  39 * @path:       ACPI GPIO controller full path name, (e.g. "\\_SB.GPO1")
  40 * @pin:        ACPI GPIO pin number (0-based, controller-relative)
  41 *
  42 * Return: GPIO descriptor to use with Linux generic GPIO API, or ERR_PTR
  43 * error value. Specifically returns %-EPROBE_DEFER if the referenced GPIO
  44 * controller does not have gpiochip registered at the moment. This is to
  45 * support probe deferral.
  46 */
  47static struct gpio_desc *acpi_get_gpiod(char *path, int pin)
  48{
  49        struct gpio_chip *chip;
  50        acpi_handle handle;
  51        acpi_status status;
  52
  53        status = acpi_get_handle(NULL, path, &handle);
  54        if (ACPI_FAILURE(status))
  55                return ERR_PTR(-ENODEV);
  56
  57        chip = gpiochip_find(handle, acpi_gpiochip_find);
  58        if (!chip)
  59                return ERR_PTR(-EPROBE_DEFER);
  60
  61        if (pin < 0 || pin > chip->ngpio)
  62                return ERR_PTR(-EINVAL);
  63
  64        return gpio_to_desc(chip->base + pin);
  65}
  66
  67static irqreturn_t acpi_gpio_irq_handler(int irq, void *data)
  68{
  69        acpi_handle handle = data;
  70
  71        acpi_evaluate_object(handle, NULL, NULL, NULL);
  72
  73        return IRQ_HANDLED;
  74}
  75
  76static irqreturn_t acpi_gpio_irq_handler_evt(int irq, void *data)
  77{
  78        struct acpi_gpio_evt_pin *evt_pin = data;
  79
  80        acpi_execute_simple_method(evt_pin->evt_handle, NULL, evt_pin->pin);
  81
  82        return IRQ_HANDLED;
  83}
  84
  85static void acpi_gpio_evt_dh(acpi_handle handle, void *data)
  86{
  87        /* The address of this function is used as a key. */
  88}
  89
  90/**
  91 * acpi_gpiochip_request_interrupts() - Register isr for gpio chip ACPI events
  92 * @chip:      gpio chip
  93 *
  94 * ACPI5 platforms can use GPIO signaled ACPI events. These GPIO interrupts are
  95 * handled by ACPI event methods which need to be called from the GPIO
  96 * chip's interrupt handler. acpi_gpiochip_request_interrupts finds out which
  97 * gpio pins have acpi event methods and assigns interrupt handlers that calls
  98 * the acpi event methods for those pins.
  99 */
 100static void acpi_gpiochip_request_interrupts(struct gpio_chip *chip)
 101{
 102        struct acpi_buffer buf = {ACPI_ALLOCATE_BUFFER, NULL};
 103        struct acpi_resource *res;
 104        acpi_handle handle, evt_handle;
 105        struct list_head *evt_pins = NULL;
 106        acpi_status status;
 107        unsigned int pin;
 108        int irq, ret;
 109        char ev_name[5];
 110
 111        if (!chip->dev || !chip->to_irq)
 112                return;
 113
 114        handle = ACPI_HANDLE(chip->dev);
 115        if (!handle)
 116                return;
 117
 118        status = acpi_get_event_resources(handle, &buf);
 119        if (ACPI_FAILURE(status))
 120                return;
 121
 122        status = acpi_get_handle(handle, "_EVT", &evt_handle);
 123        if (ACPI_SUCCESS(status)) {
 124                evt_pins = kzalloc(sizeof(*evt_pins), GFP_KERNEL);
 125                if (evt_pins) {
 126                        INIT_LIST_HEAD(evt_pins);
 127                        status = acpi_attach_data(handle, acpi_gpio_evt_dh,
 128                                                  evt_pins);
 129                        if (ACPI_FAILURE(status)) {
 130                                kfree(evt_pins);
 131                                evt_pins = NULL;
 132                        }
 133                }
 134        }
 135
 136        /*
 137         * If a GPIO interrupt has an ACPI event handler method, or _EVT is
 138         * present, set up an interrupt handler that calls the ACPI event
 139         * handler.
 140         */
 141        for (res = buf.pointer;
 142             res && (res->type != ACPI_RESOURCE_TYPE_END_TAG);
 143             res = ACPI_NEXT_RESOURCE(res)) {
 144                irq_handler_t handler = NULL;
 145                void *data;
 146
 147                if (res->type != ACPI_RESOURCE_TYPE_GPIO ||
 148                    res->data.gpio.connection_type !=
 149                    ACPI_RESOURCE_GPIO_TYPE_INT)
 150                        continue;
 151
 152                pin = res->data.gpio.pin_table[0];
 153                if (pin > chip->ngpio)
 154                        continue;
 155
 156                irq = chip->to_irq(chip, pin);
 157                if (irq < 0)
 158                        continue;
 159
 160                if (pin <= 255) {
 161                        acpi_handle ev_handle;
 162
 163                        sprintf(ev_name, "_%c%02X",
 164                                res->data.gpio.triggering ? 'E' : 'L', pin);
 165                        status = acpi_get_handle(handle, ev_name, &ev_handle);
 166                        if (ACPI_SUCCESS(status)) {
 167                                handler = acpi_gpio_irq_handler;
 168                                data = ev_handle;
 169                        }
 170                }
 171                if (!handler && evt_pins) {
 172                        struct acpi_gpio_evt_pin *evt_pin;
 173
 174                        evt_pin = kzalloc(sizeof(*evt_pin), GFP_KERNEL);
 175                        if (!evt_pin)
 176                                continue;
 177
 178                        list_add_tail(&evt_pin->node, evt_pins);
 179                        evt_pin->evt_handle = evt_handle;
 180                        evt_pin->pin = pin;
 181                        evt_pin->irq = irq;
 182                        handler = acpi_gpio_irq_handler_evt;
 183                        data = evt_pin;
 184                }
 185                if (!handler)
 186                        continue;
 187
 188                /* Assume BIOS sets the triggering, so no flags */
 189                ret = devm_request_threaded_irq(chip->dev, irq, NULL, handler,
 190                                                0, "GPIO-signaled-ACPI-event",
 191                                                data);
 192                if (ret)
 193                        dev_err(chip->dev,
 194                                "Failed to request IRQ %d ACPI event handler\n",
 195                                irq);
 196        }
 197}
 198
 199/**
 200 * acpi_gpiochip_free_interrupts() - Free GPIO _EVT ACPI event interrupts.
 201 * @chip:      gpio chip
 202 *
 203 * Free interrupts associated with the _EVT method for the given GPIO chip.
 204 *
 205 * The remaining ACPI event interrupts associated with the chip are freed
 206 * automatically.
 207 */
 208static void acpi_gpiochip_free_interrupts(struct gpio_chip *chip)
 209{
 210        acpi_handle handle;
 211        acpi_status status;
 212        struct list_head *evt_pins;
 213        struct acpi_gpio_evt_pin *evt_pin, *ep;
 214
 215        if (!chip->dev || !chip->to_irq)
 216                return;
 217
 218        handle = ACPI_HANDLE(chip->dev);
 219        if (!handle)
 220                return;
 221
 222        status = acpi_get_data(handle, acpi_gpio_evt_dh, (void **)&evt_pins);
 223        if (ACPI_FAILURE(status))
 224                return;
 225
 226        list_for_each_entry_safe_reverse(evt_pin, ep, evt_pins, node) {
 227                devm_free_irq(chip->dev, evt_pin->irq, evt_pin);
 228                list_del(&evt_pin->node);
 229                kfree(evt_pin);
 230        }
 231
 232        acpi_detach_data(handle, acpi_gpio_evt_dh);
 233        kfree(evt_pins);
 234}
 235
 236int acpi_dev_add_driver_gpios(struct acpi_device *adev,
 237                              const struct acpi_gpio_mapping *gpios)
 238{
 239        if (adev && gpios) {
 240                adev->driver_gpios = gpios;
 241                return 0;
 242        }
 243        return -EINVAL;
 244}
 245EXPORT_SYMBOL_GPL(acpi_dev_add_driver_gpios);
 246
 247static void devm_acpi_dev_release_driver_gpios(struct device *dev, void *res)
 248{
 249        acpi_dev_remove_driver_gpios(ACPI_COMPANION(dev));
 250}
 251
 252int devm_acpi_dev_add_driver_gpios(struct device *dev,
 253                                   const struct acpi_gpio_mapping *gpios)
 254{
 255        void *res;
 256        int ret;
 257
 258        res = devres_alloc(devm_acpi_dev_release_driver_gpios, 0, GFP_KERNEL);
 259        if (!res)
 260                return -ENOMEM;
 261
 262        ret = acpi_dev_add_driver_gpios(ACPI_COMPANION(dev), gpios);
 263        if (ret) {
 264                devres_free(res);
 265                return ret;
 266        }
 267        devres_add(dev, res);
 268        return 0;
 269}
 270EXPORT_SYMBOL_GPL(devm_acpi_dev_add_driver_gpios);
 271
 272void devm_acpi_dev_remove_driver_gpios(struct device *dev)
 273{
 274        WARN_ON(devres_release(dev, devm_acpi_dev_release_driver_gpios, NULL, NULL));
 275}
 276EXPORT_SYMBOL_GPL(devm_acpi_dev_remove_driver_gpios);
 277
 278static bool acpi_get_driver_gpio_data(struct acpi_device *adev,
 279                                      const char *name, int index,
 280                                      struct acpi_reference_args *args)
 281{
 282        const struct acpi_gpio_mapping *gm;
 283
 284        if (!adev->driver_gpios)
 285                return false;
 286
 287        for (gm = adev->driver_gpios; gm->name; gm++)
 288                if (!strcmp(name, gm->name) && gm->data && index < gm->size) {
 289                        const struct acpi_gpio_params *par = gm->data + index;
 290
 291                        args->adev = adev;
 292                        args->args[0] = par->crs_entry_index;
 293                        args->args[1] = par->line_index;
 294                        args->args[2] = par->active_low;
 295                        args->nargs = 3;
 296                        return true;
 297                }
 298
 299        return false;
 300}
 301
 302struct acpi_gpio_lookup {
 303        struct acpi_gpio_info info;
 304        int index;
 305        int pin_index;
 306        struct gpio_desc *desc;
 307        int n;
 308};
 309
 310static int acpi_find_gpio(struct acpi_resource *ares, void *data)
 311{
 312        struct acpi_gpio_lookup *lookup = data;
 313
 314        if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
 315                return 1;
 316
 317        if (lookup->n++ == lookup->index && !lookup->desc) {
 318                const struct acpi_resource_gpio *agpio = &ares->data.gpio;
 319                int pin_index = lookup->pin_index;
 320
 321                if (pin_index >= agpio->pin_table_length)
 322                        return 1;
 323
 324                lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr,
 325                                              agpio->pin_table[pin_index]);
 326                lookup->info.gpioint =
 327                        agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
 328
 329                /*
 330                 * ActiveLow is only specified for GpioInt resource. If
 331                 * GpioIo is used then the only way to set the flag is
 332                 * to use _DSD "gpios" property.
 333                 */
 334                if (lookup->info.gpioint)
 335                        lookup->info.active_low =
 336                                agpio->polarity == ACPI_ACTIVE_LOW;
 337        }
 338
 339        return 1;
 340}
 341
 342/**
 343 * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources
 344 * @adev: pointer to a ACPI device to get GPIO from
 345 * @propname: Property name of the GPIO (optional)
 346 * @index: index of GpioIo/GpioInt resource (starting from %0)
 347 * @info: info pointer to fill in (optional)
 348 *
 349 * Function goes through ACPI resources for @adev and based on @index looks
 350 * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor,
 351 * and returns it. @index matches GpioIo/GpioInt resources only so if there
 352 * are total %3 GPIO resources, the index goes from %0 to %2.
 353 *
 354 * If @propname is specified the GPIO is looked using device property. In
 355 * that case @index is used to select the GPIO entry in the property value
 356 * (in case of multiple).
 357 *
 358 * If the GPIO cannot be translated or there is an error an ERR_PTR is
 359 * returned.
 360 *
 361 * Note: if the GPIO resource has multiple entries in the pin list, this
 362 * function only returns the first.
 363 */
 364struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
 365                                          const char *propname, int index,
 366                                          struct acpi_gpio_info *info)
 367{
 368        struct acpi_gpio_lookup lookup;
 369        struct list_head resource_list;
 370        bool active_low = false;
 371        int ret;
 372
 373        if (!adev)
 374                return ERR_PTR(-ENODEV);
 375
 376        memset(&lookup, 0, sizeof(lookup));
 377        lookup.index = index;
 378
 379        if (propname) {
 380                struct acpi_reference_args args;
 381
 382                dev_dbg(&adev->dev, "GPIO: looking up %s\n", propname);
 383
 384                memset(&args, 0, sizeof(args));
 385                ret = acpi_node_get_property_reference(acpi_fwnode_handle(adev),
 386                                                       propname, index, &args);
 387                if (ret) {
 388                        bool found = acpi_get_driver_gpio_data(adev, propname,
 389                                                               index, &args);
 390                        if (!found)
 391                                return ERR_PTR(ret);
 392                }
 393
 394                /*
 395                 * The property was found and resolved so need to
 396                 * lookup the GPIO based on returned args instead.
 397                 */
 398                adev = args.adev;
 399                if (args.nargs >= 2) {
 400                        lookup.index = args.args[0];
 401                        lookup.pin_index = args.args[1];
 402                        /*
 403                         * 3rd argument, if present is used to
 404                         * specify active_low.
 405                         */
 406                        if (args.nargs >= 3)
 407                                active_low = !!args.args[2];
 408                }
 409
 410                dev_dbg(&adev->dev, "GPIO: _DSD returned %s %zd %llu %llu %llu\n",
 411                        dev_name(&adev->dev), args.nargs,
 412                        args.args[0], args.args[1], args.args[2]);
 413        } else {
 414                dev_dbg(&adev->dev, "GPIO: looking up %d in _CRS\n", index);
 415        }
 416
 417        INIT_LIST_HEAD(&resource_list);
 418        ret = acpi_dev_get_resources(adev, &resource_list, acpi_find_gpio,
 419                                     &lookup);
 420        if (ret < 0)
 421                return ERR_PTR(ret);
 422
 423        acpi_dev_free_resource_list(&resource_list);
 424
 425        if (lookup.desc && info) {
 426                *info = lookup.info;
 427                if (active_low)
 428                        info->active_low = active_low;
 429        }
 430
 431        return lookup.desc ? lookup.desc : ERR_PTR(-ENOENT);
 432}
 433
 434/**
 435 * acpi_dev_gpio_irq_get() - Find GpioInt and translate it to Linux IRQ number
 436 * @adev: pointer to a ACPI device to get IRQ from
 437 * @index: index of GpioInt resource (starting from %0)
 438 *
 439 * If the device has one or more GpioInt resources, this function can be
 440 * used to translate from the GPIO offset in the resource to the Linux IRQ
 441 * number.
 442 *
 443 * Return: Linux IRQ number (>%0) on success, negative errno on failure.
 444 */
 445int acpi_dev_gpio_irq_get(struct acpi_device *adev, int index)
 446{
 447        int idx, i;
 448
 449        for (i = 0, idx = 0; idx <= index; i++) {
 450                struct acpi_gpio_info info;
 451                struct gpio_desc *desc;
 452
 453                desc = acpi_get_gpiod_by_index(adev, NULL, i, &info);
 454                if (IS_ERR(desc))
 455                        break;
 456                if (info.gpioint && idx++ == index)
 457                        return gpiod_to_irq(desc);
 458        }
 459        return -ENOENT;
 460}
 461EXPORT_SYMBOL_GPL(acpi_dev_gpio_irq_get);
 462
 463void acpi_gpiochip_add(struct gpio_chip *chip)
 464{
 465        acpi_gpiochip_request_interrupts(chip);
 466}
 467
 468void acpi_gpiochip_remove(struct gpio_chip *chip)
 469{
 470        acpi_gpiochip_free_interrupts(chip);
 471}
 472