GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / acpi / evged.c
1 /*
2  * Generic Event Device for ACPI.
3  *
4  * Copyright (c) 2016, The Linux Foundation. All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 and
8  * only version 2 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * Generic Event Device allows platforms to handle interrupts in ACPI
16  * ASL statements. It follows very similar to  _EVT method approach
17  * from GPIO events. All interrupts are listed in _CRS and the handler
18  * is written in _EVT method. Here is an example.
19  *
20  * Device (GED0)
21  * {
22  *
23  *     Name (_HID, "ACPI0013")
24  *     Name (_UID, 0)
25  *     Method (_CRS, 0x0, Serialized)
26  *     {
27  *              Name (RBUF, ResourceTemplate ()
28  *              {
29  *              Interrupt(ResourceConsumer, Edge, ActiveHigh, Shared, , , )
30  *              {123}
31  *              }
32  *     })
33  *
34  *     Method (_EVT, 1) {
35  *             if (Lequal(123, Arg0))
36  *             {
37  *             }
38  *     }
39  * }
40  *
41  */
42
43 #include <linux/err.h>
44 #include <linux/init.h>
45 #include <linux/interrupt.h>
46 #include <linux/list.h>
47 #include <linux/platform_device.h>
48 #include <linux/acpi.h>
49
50 #define MODULE_NAME     "acpi-ged"
51
52 struct acpi_ged_device {
53         struct device *dev;
54         struct list_head event_list;
55 };
56
57 struct acpi_ged_event {
58         struct list_head node;
59         struct device *dev;
60         unsigned int gsi;
61         unsigned int irq;
62         acpi_handle handle;
63 };
64
65 static irqreturn_t acpi_ged_irq_handler(int irq, void *data)
66 {
67         struct acpi_ged_event *event = data;
68         acpi_status acpi_ret;
69
70         acpi_ret = acpi_execute_simple_method(event->handle, NULL, event->gsi);
71         if (ACPI_FAILURE(acpi_ret))
72                 dev_err_once(event->dev, "IRQ method execution failed\n");
73
74         return IRQ_HANDLED;
75 }
76
77 static acpi_status acpi_ged_request_interrupt(struct acpi_resource *ares,
78                                               void *context)
79 {
80         struct acpi_ged_event *event;
81         unsigned int irq;
82         unsigned int gsi;
83         unsigned int irqflags = IRQF_ONESHOT;
84         struct acpi_ged_device *geddev = context;
85         struct device *dev = geddev->dev;
86         acpi_handle handle = ACPI_HANDLE(dev);
87         acpi_handle evt_handle;
88         struct resource r;
89         struct acpi_resource_irq *p = &ares->data.irq;
90         struct acpi_resource_extended_irq *pext = &ares->data.extended_irq;
91         char ev_name[5];
92         u8 trigger;
93
94         if (ares->type == ACPI_RESOURCE_TYPE_END_TAG)
95                 return AE_OK;
96
97         if (!acpi_dev_resource_interrupt(ares, 0, &r)) {
98                 dev_err(dev, "unable to parse IRQ resource\n");
99                 return AE_ERROR;
100         }
101         if (ares->type == ACPI_RESOURCE_TYPE_IRQ) {
102                 gsi = p->interrupts[0];
103                 trigger = p->triggering;
104         } else {
105                 gsi = pext->interrupts[0];
106                 trigger = pext->triggering;
107         }
108
109         irq = r.start;
110
111         switch (gsi) {
112         case 0 ... 255:
113                 sprintf(ev_name, "_%c%02X",
114                         trigger == ACPI_EDGE_SENSITIVE ? 'E' : 'L', gsi);
115
116                 if (ACPI_SUCCESS(acpi_get_handle(handle, ev_name, &evt_handle)))
117                         break;
118                 /* fall through */
119         default:
120                 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EVT", &evt_handle)))
121                         break;
122
123                 dev_err(dev, "cannot locate _EVT method\n");
124                 return AE_ERROR;
125         }
126
127         event = devm_kzalloc(dev, sizeof(*event), GFP_KERNEL);
128         if (!event)
129                 return AE_ERROR;
130
131         event->gsi = gsi;
132         event->dev = dev;
133         event->irq = irq;
134         event->handle = evt_handle;
135
136         if (r.flags & IORESOURCE_IRQ_SHAREABLE)
137                 irqflags |= IRQF_SHARED;
138
139         if (request_threaded_irq(irq, NULL, acpi_ged_irq_handler,
140                                  irqflags, "ACPI:Ged", event)) {
141                 dev_err(dev, "failed to setup event handler for irq %u\n", irq);
142                 return AE_ERROR;
143         }
144
145         dev_dbg(dev, "GED listening GSI %u @ IRQ %u\n", gsi, irq);
146         list_add_tail(&event->node, &geddev->event_list);
147         return AE_OK;
148 }
149
150 static int ged_probe(struct platform_device *pdev)
151 {
152         struct acpi_ged_device *geddev;
153         acpi_status acpi_ret;
154
155         geddev = devm_kzalloc(&pdev->dev, sizeof(*geddev), GFP_KERNEL);
156         if (!geddev)
157                 return -ENOMEM;
158
159         geddev->dev = &pdev->dev;
160         INIT_LIST_HEAD(&geddev->event_list);
161         acpi_ret = acpi_walk_resources(ACPI_HANDLE(&pdev->dev), "_CRS",
162                                        acpi_ged_request_interrupt, geddev);
163         if (ACPI_FAILURE(acpi_ret)) {
164                 dev_err(&pdev->dev, "unable to parse the _CRS record\n");
165                 return -EINVAL;
166         }
167         platform_set_drvdata(pdev, geddev);
168
169         return 0;
170 }
171
172 static void ged_shutdown(struct platform_device *pdev)
173 {
174         struct acpi_ged_device *geddev = platform_get_drvdata(pdev);
175         struct acpi_ged_event *event, *next;
176
177         list_for_each_entry_safe(event, next, &geddev->event_list, node) {
178                 free_irq(event->irq, event);
179                 list_del(&event->node);
180                 dev_dbg(geddev->dev, "GED releasing GSI %u @ IRQ %u\n",
181                          event->gsi, event->irq);
182         }
183 }
184
185 static int ged_remove(struct platform_device *pdev)
186 {
187         ged_shutdown(pdev);
188         return 0;
189 }
190
191 static const struct acpi_device_id ged_acpi_ids[] = {
192         {"ACPI0013"},
193         {},
194 };
195
196 static struct platform_driver ged_driver = {
197         .probe = ged_probe,
198         .remove = ged_remove,
199         .shutdown = ged_shutdown,
200         .driver = {
201                 .name = MODULE_NAME,
202                 .acpi_match_table = ACPI_PTR(ged_acpi_ids),
203         },
204 };
205 builtin_platform_driver(ged_driver);