blob: ac778593c962981cfe8609e6eb1a50c012a0b407 [file] [log] [blame]
Simon Glassff418d92019-12-06 21:41:58 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
Simon Glassa847b272020-02-06 09:54:57 -07003 * Copyright 2019 Google, LLC
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glassff418d92019-12-06 21:41:58 -07005 */
6
Simon Glass515dcff2020-02-06 09:55:00 -07007#define LOG_CATEGORY UCLASS_IRQ
8
Simon Glassff418d92019-12-06 21:41:58 -07009#include <dm.h>
Simon Glass515dcff2020-02-06 09:55:00 -070010#include <dt-structs.h>
Simon Glassff418d92019-12-06 21:41:58 -070011#include <irq.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glassa847b272020-02-06 09:54:57 -070013#include <dm/device-internal.h>
Simon Glassff418d92019-12-06 21:41:58 -070014
15int irq_route_pmc_gpio_gpe(struct udevice *dev, uint pmc_gpe_num)
16{
17 const struct irq_ops *ops = irq_get_ops(dev);
18
19 if (!ops->route_pmc_gpio_gpe)
20 return -ENOSYS;
21
22 return ops->route_pmc_gpio_gpe(dev, pmc_gpe_num);
23}
24
25int irq_set_polarity(struct udevice *dev, uint irq, bool active_low)
26{
27 const struct irq_ops *ops = irq_get_ops(dev);
28
29 if (!ops->set_polarity)
30 return -ENOSYS;
31
32 return ops->set_polarity(dev, irq, active_low);
33}
34
35int irq_snapshot_polarities(struct udevice *dev)
36{
37 const struct irq_ops *ops = irq_get_ops(dev);
38
39 if (!ops->snapshot_polarities)
40 return -ENOSYS;
41
42 return ops->snapshot_polarities(dev);
43}
44
45int irq_restore_polarities(struct udevice *dev)
46{
47 const struct irq_ops *ops = irq_get_ops(dev);
48
49 if (!ops->restore_polarities)
50 return -ENOSYS;
51
52 return ops->restore_polarities(dev);
53}
54
Simon Glass515dcff2020-02-06 09:55:00 -070055int irq_read_and_clear(struct irq *irq)
56{
57 const struct irq_ops *ops = irq_get_ops(irq->dev);
58
59 if (!ops->read_and_clear)
60 return -ENOSYS;
61
62 return ops->read_and_clear(irq);
63}
64
Patrick Rudolph0fe88cc2024-10-23 15:20:05 +020065int irq_get_interrupt_parent(const struct udevice *dev,
66 struct udevice **interrupt_parent)
67{
68 struct ofnode_phandle_args phandle_args;
69 struct udevice *irq = NULL;
70 ofnode node;
71 int ret;
72
73 if (!dev || !interrupt_parent)
74 return -EINVAL;
75
76 *interrupt_parent = NULL;
77
78 node = dev_ofnode(dev);
79 if (!ofnode_valid(node))
80 return -EINVAL;
81
82 while (ofnode_valid(node)) {
83 ret = ofnode_parse_phandle_with_args(node, "interrupt-parent",
84 NULL, 0, 0, &phandle_args);
85 if (!ret && !device_get_global_by_ofnode(phandle_args.node, &irq))
86 break;
87 node = ofnode_get_parent(node);
88 }
89
90 if (!irq) {
91 log_err("Cannot find an interrupt parent for device %s\n", dev->name);
92 return -ENODEV;
93 }
94 *interrupt_parent = irq;
95
96 return 0;
97}
98
Simon Glass515dcff2020-02-06 09:55:00 -070099#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glasse7995f72021-08-07 07:24:11 -0600100int irq_get_by_phandle(struct udevice *dev, const struct phandle_2_arg *cells,
101 struct irq *irq)
Simon Glass515dcff2020-02-06 09:55:00 -0700102{
103 int ret;
104
Simon Glass0000e0d2021-03-15 17:25:28 +1300105 ret = device_get_by_ofplat_idx(cells->idx, &irq->dev);
Simon Glass515dcff2020-02-06 09:55:00 -0700106 if (ret)
107 return ret;
Walter Lozanodc5b4372020-06-25 01:10:13 -0300108 irq->id = cells->arg[0];
Simon Glass515dcff2020-02-06 09:55:00 -0700109
Simon Glasse7995f72021-08-07 07:24:11 -0600110 /*
111 * Note: we could call irq_of_xlate_default() here to do this properly.
112 * For now, this is good enough for existing cases.
113 */
114 irq->flags = cells->arg[1];
115
Simon Glass515dcff2020-02-06 09:55:00 -0700116 return 0;
117}
118#else
119static int irq_of_xlate_default(struct irq *irq,
120 struct ofnode_phandle_args *args)
121{
122 log_debug("(irq=%p)\n", irq);
123
124 if (args->args_count > 1) {
Sean Andersona1b654b2021-12-01 14:26:53 -0500125 log_debug("Invalid args_count: %d\n", args->args_count);
Simon Glass515dcff2020-02-06 09:55:00 -0700126 return -EINVAL;
127 }
128
129 if (args->args_count)
130 irq->id = args->args[0];
131 else
132 irq->id = 0;
133
134 return 0;
135}
136
137static int irq_get_by_index_tail(int ret, ofnode node,
138 struct ofnode_phandle_args *args,
139 const char *list_name, int index,
140 struct irq *irq)
141{
142 struct udevice *dev_irq;
143 const struct irq_ops *ops;
144
145 assert(irq);
146 irq->dev = NULL;
147 if (ret)
148 goto err;
149
150 ret = uclass_get_device_by_ofnode(UCLASS_IRQ, args->node, &dev_irq);
151 if (ret) {
152 log_debug("uclass_get_device_by_ofnode failed: err=%d\n", ret);
153 return ret;
154 }
155
156 irq->dev = dev_irq;
157
158 ops = irq_get_ops(dev_irq);
159
160 if (ops->of_xlate)
161 ret = ops->of_xlate(irq, args);
162 else
163 ret = irq_of_xlate_default(irq, args);
164 if (ret) {
165 log_debug("of_xlate() failed: %d\n", ret);
166 return ret;
167 }
168
169 return irq_request(dev_irq, irq);
170err:
171 log_debug("Node '%s', property '%s', failed to request IRQ index %d: %d\n",
172 ofnode_get_name(node), list_name, index, ret);
173 return ret;
174}
175
176int irq_get_by_index(struct udevice *dev, int index, struct irq *irq)
177{
178 struct ofnode_phandle_args args;
Patrick Rudolph0fe88cc2024-10-23 15:20:05 +0200179 struct udevice *interrupt_parent;
180 int ret, size, i;
181 const __be32 *list;
182 u32 count;
Simon Glass515dcff2020-02-06 09:55:00 -0700183
184 ret = dev_read_phandle_with_args(dev, "interrupts-extended",
185 "#interrupt-cells", 0, index, &args);
Patrick Rudolph0fe88cc2024-10-23 15:20:05 +0200186 if (ret) {
187 list = dev_read_prop(dev, "interrupts", &size);
188 if (!list)
189 return -ENOENT;
190
191 ret = irq_get_interrupt_parent(dev, &interrupt_parent);
192 if (ret)
193 return -ENODEV;
194 args.node = dev_ofnode(interrupt_parent);
195
196 if (dev_read_u32(interrupt_parent, "#interrupt-cells", &count)) {
197 log_err("%s: could not get #interrupt-cells for %s\n",
198 __func__, dev->name);
199 return -ENOENT;
200 }
201
202 if (index * count >= size / sizeof(*list))
203 return -ENOENT;
204 if (count > OF_MAX_PHANDLE_ARGS)
205 count = OF_MAX_PHANDLE_ARGS;
206 args.args_count = count;
207 for (i = 0; i < count; i++)
208 args.args[i] = be32_to_cpup(&list[index * count + i]);
209
210 return irq_get_by_index_tail(ret, dev_ofnode(dev), &args,
211 "interrupts", index, irq);
212 }
Simon Glass515dcff2020-02-06 09:55:00 -0700213
214 return irq_get_by_index_tail(ret, dev_ofnode(dev), &args,
215 "interrupts-extended", index > 0, irq);
216}
217#endif /* OF_PLATDATA */
218
219int irq_request(struct udevice *dev, struct irq *irq)
220{
221 const struct irq_ops *ops;
222
223 log_debug("(dev=%p, irq=%p)\n", dev, irq);
Simon Glass515dcff2020-02-06 09:55:00 -0700224 ops = irq_get_ops(dev);
225
226 irq->dev = dev;
227
228 if (!ops->request)
229 return 0;
230
231 return ops->request(irq);
232}
233
Simon Glassa847b272020-02-06 09:54:57 -0700234int irq_first_device_type(enum irq_dev_t type, struct udevice **devp)
235{
236 int ret;
237
238 ret = uclass_first_device_drvdata(UCLASS_IRQ, type, devp);
239 if (ret)
Simon Glassd89f1932020-07-16 21:22:30 -0600240 return ret;
Simon Glassa847b272020-02-06 09:54:57 -0700241
242 return 0;
243}
Simon Glassb24cbf42020-07-07 13:11:41 -0600244
245#if CONFIG_IS_ENABLED(ACPIGEN)
246int irq_get_acpi(const struct irq *irq, struct acpi_irq *acpi_irq)
247{
248 struct irq_ops *ops;
249
250 if (!irq_is_valid(irq))
251 return -EINVAL;
252
253 ops = irq_get_ops(irq->dev);
254 if (!ops->get_acpi)
255 return -ENOSYS;
256
257 return ops->get_acpi(irq, acpi_irq);
258}
259#endif
Simon Glassa847b272020-02-06 09:54:57 -0700260
Simon Glassff418d92019-12-06 21:41:58 -0700261UCLASS_DRIVER(irq) = {
262 .id = UCLASS_IRQ,
263 .name = "irq",
264};