gpiolib-acpi.c 9.3 KB
Newer Older
M
Mathias Nyman 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * ACPI helpers for GPIO API
 *
 * Copyright (C) 2012, Intel Corporation
 * Authors: Mathias Nyman <mathias.nyman@linux.intel.com>
 *          Mika Westerberg <mika.westerberg@linux.intel.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/errno.h>
14
#include <linux/gpio/consumer.h>
15
#include <linux/gpio/driver.h>
M
Mathias Nyman 已提交
16 17
#include <linux/export.h>
#include <linux/acpi.h>
18
#include <linux/interrupt.h>
M
Mathias Nyman 已提交
19

20 21
#include "gpiolib.h"

22
struct acpi_gpio_event {
23
	struct list_head node;
24
	acpi_handle handle;
25 26 27 28
	unsigned int pin;
	unsigned int irq;
};

29 30
struct acpi_gpio_chip {
	struct gpio_chip *chip;
31
	struct list_head events;
32 33
};

M
Mathias Nyman 已提交
34 35 36 37 38 39 40 41 42
static int acpi_gpiochip_find(struct gpio_chip *gc, void *data)
{
	if (!gc->dev)
		return false;

	return ACPI_HANDLE(gc->dev) == data;
}

/**
43
 * acpi_get_gpiod() - Translate ACPI GPIO pin to GPIO descriptor usable with GPIO API
M
Mathias Nyman 已提交
44 45 46
 * @path:	ACPI GPIO controller full path name, (e.g. "\\_SB.GPO1")
 * @pin:	ACPI GPIO pin number (0-based, controller-relative)
 *
47 48
 * Returns GPIO descriptor to use with Linux generic GPIO API, or ERR_PTR
 * error value
M
Mathias Nyman 已提交
49 50
 */

51
static struct gpio_desc *acpi_get_gpiod(char *path, int pin)
M
Mathias Nyman 已提交
52 53 54 55 56 57 58
{
	struct gpio_chip *chip;
	acpi_handle handle;
	acpi_status status;

	status = acpi_get_handle(NULL, path, &handle);
	if (ACPI_FAILURE(status))
59
		return ERR_PTR(-ENODEV);
M
Mathias Nyman 已提交
60 61 62

	chip = gpiochip_find(handle, acpi_gpiochip_find);
	if (!chip)
63
		return ERR_PTR(-ENODEV);
M
Mathias Nyman 已提交
64

65 66
	if (pin < 0 || pin > chip->ngpio)
		return ERR_PTR(-EINVAL);
M
Mathias Nyman 已提交
67

68
	return gpiochip_get_desc(chip, pin);
M
Mathias Nyman 已提交
69
}
70 71 72

static irqreturn_t acpi_gpio_irq_handler(int irq, void *data)
{
73
	struct acpi_gpio_event *event = data;
74

75
	acpi_evaluate_object(event->handle, NULL, NULL, NULL);
76 77 78 79

	return IRQ_HANDLED;
}

80 81
static irqreturn_t acpi_gpio_irq_handler_evt(int irq, void *data)
{
82
	struct acpi_gpio_event *event = data;
83

84
	acpi_execute_simple_method(event->handle, NULL, event->pin);
85 86 87 88

	return IRQ_HANDLED;
}

89
static void acpi_gpio_chip_dh(acpi_handle handle, void *data)
90 91 92 93
{
	/* The address of this function is used as a key. */
}

94 95
static acpi_status acpi_gpiochip_request_interrupt(struct acpi_resource *ares,
						   void *context)
96
{
97
	struct acpi_gpio_chip *acpi_gpio = context;
98
	struct gpio_chip *chip = acpi_gpio->chip;
99
	struct acpi_resource_gpio *agpio;
100
	acpi_handle handle, evt_handle;
101 102 103 104 105
	struct acpi_gpio_event *event;
	irq_handler_t handler = NULL;
	struct gpio_desc *desc;
	unsigned long irqflags;
	int ret, pin, irq;
106

107 108 109 110 111 112
	if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
		return AE_OK;

	agpio = &ares->data.gpio;
	if (agpio->connection_type != ACPI_RESOURCE_GPIO_TYPE_INT)
		return AE_OK;
113 114

	handle = ACPI_HANDLE(chip->dev);
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
	pin = agpio->pin_table[0];

	if (pin <= 255) {
		char ev_name[5];
		sprintf(ev_name, "_%c%02X",
			agpio->triggering == ACPI_EDGE_SENSITIVE ? 'E' : 'L',
			pin);
		if (ACPI_SUCCESS(acpi_get_handle(handle, ev_name, &evt_handle)))
			handler = acpi_gpio_irq_handler;
	}
	if (!handler) {
		if (ACPI_SUCCESS(acpi_get_handle(handle, "_EVT", &evt_handle)))
			handler = acpi_gpio_irq_handler_evt;
	}
	if (!handler)
		return AE_BAD_PARAMETER;
131

132 133 134 135 136
	desc = gpiochip_get_desc(chip, pin);
	if (IS_ERR(desc)) {
		dev_err(chip->dev, "Failed to get GPIO descriptor\n");
		return AE_ERROR;
	}
137

138 139 140 141 142
	ret = gpiochip_request_own_desc(desc, "ACPI:Event");
	if (ret) {
		dev_err(chip->dev, "Failed to request GPIO\n");
		return AE_ERROR;
	}
143

144
	gpiod_direction_input(desc);
145

146 147 148 149 150
	ret = gpiod_lock_as_irq(desc);
	if (ret) {
		dev_err(chip->dev, "Failed to lock GPIO as interrupt\n");
		goto fail_free_desc;
	}
151

152 153 154 155 156
	irq = gpiod_to_irq(desc);
	if (irq < 0) {
		dev_err(chip->dev, "Failed to translate GPIO to IRQ\n");
		goto fail_unlock_irq;
	}
157

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
	irqflags = IRQF_ONESHOT;
	if (agpio->triggering == ACPI_LEVEL_SENSITIVE) {
		if (agpio->polarity == ACPI_ACTIVE_HIGH)
			irqflags |= IRQF_TRIGGER_HIGH;
		else
			irqflags |= IRQF_TRIGGER_LOW;
	} else {
		switch (agpio->polarity) {
		case ACPI_ACTIVE_HIGH:
			irqflags |= IRQF_TRIGGER_RISING;
			break;
		case ACPI_ACTIVE_LOW:
			irqflags |= IRQF_TRIGGER_FALLING;
			break;
		default:
			irqflags |= IRQF_TRIGGER_RISING |
				    IRQF_TRIGGER_FALLING;
			break;
176
		}
177
	}
178

179 180 181
	event = kzalloc(sizeof(*event), GFP_KERNEL);
	if (!event)
		goto fail_unlock_irq;
182

183 184 185
	event->handle = evt_handle;
	event->irq = irq;
	event->pin = pin;
186

187 188 189 190 191 192
	ret = request_threaded_irq(event->irq, NULL, handler, irqflags,
				   "ACPI:Event", event);
	if (ret) {
		dev_err(chip->dev, "Failed to setup interrupt handler for %d\n",
			event->irq);
		goto fail_free_event;
193
	}
194 195 196 197 198 199 200 201 202 203 204 205

	list_add_tail(&event->node, &acpi_gpio->events);
	return AE_OK;

fail_free_event:
	kfree(event);
fail_unlock_irq:
	gpiod_unlock_as_irq(desc);
fail_free_desc:
	gpiochip_free_own_desc(desc);

	return AE_ERROR;
206
}
207

208
/**
209
 * acpi_gpiochip_request_interrupts() - Register isr for gpio chip ACPI events
210
 * @acpi_gpio:      ACPI GPIO chip
211
 *
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
 * ACPI5 platforms can use GPIO signaled ACPI events. These GPIO interrupts are
 * handled by ACPI event methods which need to be called from the GPIO
 * chip's interrupt handler. acpi_gpiochip_request_interrupts finds out which
 * gpio pins have acpi event methods and assigns interrupt handlers that calls
 * the acpi event methods for those pins.
 */
static void acpi_gpiochip_request_interrupts(struct acpi_gpio_chip *acpi_gpio)
{
	struct gpio_chip *chip = acpi_gpio->chip;

	if (!chip->dev || !chip->to_irq)
		return;

	INIT_LIST_HEAD(&acpi_gpio->events);
	acpi_walk_resources(ACPI_HANDLE(chip->dev), "_AEI",
			    acpi_gpiochip_request_interrupt, acpi_gpio);
}

/**
 * acpi_gpiochip_free_interrupts() - Free GPIO ACPI event interrupts.
 * @acpi_gpio:      ACPI GPIO chip
233
 *
234 235
 * Free interrupts associated with GPIO ACPI event method for the given
 * GPIO chip.
236
 */
237
static void acpi_gpiochip_free_interrupts(struct acpi_gpio_chip *acpi_gpio)
238
{
239
	struct acpi_gpio_event *event, *ep;
240
	struct gpio_chip *chip = acpi_gpio->chip;
241 242 243 244

	if (!chip->dev || !chip->to_irq)
		return;

245
	list_for_each_entry_safe_reverse(event, ep, &acpi_gpio->events, node) {
246 247 248 249 250 251 252 253
		struct gpio_desc *desc;

		free_irq(event->irq, event);
		desc = gpiochip_get_desc(chip, event->pin);
		if (WARN_ON(IS_ERR(desc)))
			continue;
		gpiod_unlock_as_irq(desc);
		gpiochip_free_own_desc(desc);
254 255
		list_del(&event->node);
		kfree(event);
256 257 258
	}
}

259 260 261
struct acpi_gpio_lookup {
	struct acpi_gpio_info info;
	int index;
262
	struct gpio_desc *desc;
263 264 265 266 267 268 269 270 271 272
	int n;
};

static int acpi_find_gpio(struct acpi_resource *ares, void *data)
{
	struct acpi_gpio_lookup *lookup = data;

	if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
		return 1;

273
	if (lookup->n++ == lookup->index && !lookup->desc) {
274 275
		const struct acpi_resource_gpio *agpio = &ares->data.gpio;

276 277
		lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr,
					      agpio->pin_table[0]);
278 279
		lookup->info.gpioint =
			agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
280 281
		lookup->info.active_low =
			agpio->polarity == ACPI_ACTIVE_LOW;
282 283 284 285 286 287
	}

	return 1;
}

/**
288
 * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources
289 290 291 292 293
 * @dev: pointer to a device to get GPIO from
 * @index: index of GpioIo/GpioInt resource (starting from %0)
 * @info: info pointer to fill in (optional)
 *
 * Function goes through ACPI resources for @dev and based on @index looks
294
 * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor,
295 296 297
 * and returns it. @index matches GpioIo/GpioInt resources only so if there
 * are total %3 GPIO resources, the index goes from %0 to %2.
 *
298
 * If the GPIO cannot be translated or there is an error an ERR_PTR is
299 300 301 302 303
 * returned.
 *
 * Note: if the GPIO resource has multiple entries in the pin list, this
 * function only returns the first.
 */
304 305
struct gpio_desc *acpi_get_gpiod_by_index(struct device *dev, int index,
					  struct acpi_gpio_info *info)
306 307 308 309 310 311 312 313
{
	struct acpi_gpio_lookup lookup;
	struct list_head resource_list;
	struct acpi_device *adev;
	acpi_handle handle;
	int ret;

	if (!dev)
314
		return ERR_PTR(-EINVAL);
315 316 317

	handle = ACPI_HANDLE(dev);
	if (!handle || acpi_bus_get_device(handle, &adev))
318
		return ERR_PTR(-ENODEV);
319 320 321 322 323 324 325 326

	memset(&lookup, 0, sizeof(lookup));
	lookup.index = index;

	INIT_LIST_HEAD(&resource_list);
	ret = acpi_dev_get_resources(adev, &resource_list, acpi_find_gpio,
				     &lookup);
	if (ret < 0)
327
		return ERR_PTR(ret);
328 329 330

	acpi_dev_free_resource_list(&resource_list);

331
	if (lookup.desc && info)
332 333
		*info = lookup.info;

334
	return lookup.desc ? lookup.desc : ERR_PTR(-ENOENT);
335
}
336 337 338

void acpi_gpiochip_add(struct gpio_chip *chip)
{
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
	struct acpi_gpio_chip *acpi_gpio;
	acpi_handle handle;
	acpi_status status;

	handle = ACPI_HANDLE(chip->dev);
	if (!handle)
		return;

	acpi_gpio = kzalloc(sizeof(*acpi_gpio), GFP_KERNEL);
	if (!acpi_gpio) {
		dev_err(chip->dev,
			"Failed to allocate memory for ACPI GPIO chip\n");
		return;
	}

	acpi_gpio->chip = chip;

	status = acpi_attach_data(handle, acpi_gpio_chip_dh, acpi_gpio);
	if (ACPI_FAILURE(status)) {
		dev_err(chip->dev, "Failed to attach ACPI GPIO chip\n");
		kfree(acpi_gpio);
		return;
	}

	acpi_gpiochip_request_interrupts(acpi_gpio);
364 365 366 367
}

void acpi_gpiochip_remove(struct gpio_chip *chip)
{
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
	struct acpi_gpio_chip *acpi_gpio;
	acpi_handle handle;
	acpi_status status;

	handle = ACPI_HANDLE(chip->dev);
	if (!handle)
		return;

	status = acpi_get_data(handle, acpi_gpio_chip_dh, (void **)&acpi_gpio);
	if (ACPI_FAILURE(status)) {
		dev_warn(chip->dev, "Failed to retrieve ACPI GPIO chip\n");
		return;
	}

	acpi_gpiochip_free_interrupts(acpi_gpio);

	acpi_detach_data(handle, acpi_gpio_chip_dh);
	kfree(acpi_gpio);
386
}