1
2
3
4
5
6
7
8
9
10
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
39
40
41
42
43
44
45
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
88}
89
90
91
92
93
94
95
96
97
98
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
138
139
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
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
201
202
203
204
205
206
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
331
332
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
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
396
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
404
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
436
437
438
439
440
441
442
443
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