blob: 79eb7c200dc24aa42ca3855079ebe232d8ad70e7 [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
65#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glasse7995f72021-08-07 07:24:11 -060066int irq_get_by_phandle(struct udevice *dev, const struct phandle_2_arg *cells,
67 struct irq *irq)
Simon Glass515dcff2020-02-06 09:55:00 -070068{
69 int ret;
70
Simon Glass0000e0d2021-03-15 17:25:28 +130071 ret = device_get_by_ofplat_idx(cells->idx, &irq->dev);
Simon Glass515dcff2020-02-06 09:55:00 -070072 if (ret)
73 return ret;
Walter Lozanodc5b4372020-06-25 01:10:13 -030074 irq->id = cells->arg[0];
Simon Glass515dcff2020-02-06 09:55:00 -070075
Simon Glasse7995f72021-08-07 07:24:11 -060076 /*
77 * Note: we could call irq_of_xlate_default() here to do this properly.
78 * For now, this is good enough for existing cases.
79 */
80 irq->flags = cells->arg[1];
81
Simon Glass515dcff2020-02-06 09:55:00 -070082 return 0;
83}
84#else
85static int irq_of_xlate_default(struct irq *irq,
86 struct ofnode_phandle_args *args)
87{
88 log_debug("(irq=%p)\n", irq);
89
90 if (args->args_count > 1) {
Sean Andersona1b654b2021-12-01 14:26:53 -050091 log_debug("Invalid args_count: %d\n", args->args_count);
Simon Glass515dcff2020-02-06 09:55:00 -070092 return -EINVAL;
93 }
94
95 if (args->args_count)
96 irq->id = args->args[0];
97 else
98 irq->id = 0;
99
100 return 0;
101}
102
103static int irq_get_by_index_tail(int ret, ofnode node,
104 struct ofnode_phandle_args *args,
105 const char *list_name, int index,
106 struct irq *irq)
107{
108 struct udevice *dev_irq;
109 const struct irq_ops *ops;
110
111 assert(irq);
112 irq->dev = NULL;
113 if (ret)
114 goto err;
115
116 ret = uclass_get_device_by_ofnode(UCLASS_IRQ, args->node, &dev_irq);
117 if (ret) {
118 log_debug("uclass_get_device_by_ofnode failed: err=%d\n", ret);
119 return ret;
120 }
121
122 irq->dev = dev_irq;
123
124 ops = irq_get_ops(dev_irq);
125
126 if (ops->of_xlate)
127 ret = ops->of_xlate(irq, args);
128 else
129 ret = irq_of_xlate_default(irq, args);
130 if (ret) {
131 log_debug("of_xlate() failed: %d\n", ret);
132 return ret;
133 }
134
135 return irq_request(dev_irq, irq);
136err:
137 log_debug("Node '%s', property '%s', failed to request IRQ index %d: %d\n",
138 ofnode_get_name(node), list_name, index, ret);
139 return ret;
140}
141
142int irq_get_by_index(struct udevice *dev, int index, struct irq *irq)
143{
144 struct ofnode_phandle_args args;
145 int ret;
146
147 ret = dev_read_phandle_with_args(dev, "interrupts-extended",
148 "#interrupt-cells", 0, index, &args);
149
150 return irq_get_by_index_tail(ret, dev_ofnode(dev), &args,
151 "interrupts-extended", index > 0, irq);
152}
153#endif /* OF_PLATDATA */
154
155int irq_request(struct udevice *dev, struct irq *irq)
156{
157 const struct irq_ops *ops;
158
159 log_debug("(dev=%p, irq=%p)\n", dev, irq);
Simon Glass515dcff2020-02-06 09:55:00 -0700160 ops = irq_get_ops(dev);
161
162 irq->dev = dev;
163
164 if (!ops->request)
165 return 0;
166
167 return ops->request(irq);
168}
169
Simon Glassa847b272020-02-06 09:54:57 -0700170int irq_first_device_type(enum irq_dev_t type, struct udevice **devp)
171{
172 int ret;
173
174 ret = uclass_first_device_drvdata(UCLASS_IRQ, type, devp);
175 if (ret)
Simon Glassd89f1932020-07-16 21:22:30 -0600176 return ret;
Simon Glassa847b272020-02-06 09:54:57 -0700177
178 return 0;
179}
Simon Glassb24cbf42020-07-07 13:11:41 -0600180
181#if CONFIG_IS_ENABLED(ACPIGEN)
182int irq_get_acpi(const struct irq *irq, struct acpi_irq *acpi_irq)
183{
184 struct irq_ops *ops;
185
186 if (!irq_is_valid(irq))
187 return -EINVAL;
188
189 ops = irq_get_ops(irq->dev);
190 if (!ops->get_acpi)
191 return -ENOSYS;
192
193 return ops->get_acpi(irq, acpi_irq);
194}
195#endif
Simon Glassa847b272020-02-06 09:54:57 -0700196
Simon Glassff418d92019-12-06 21:41:58 -0700197UCLASS_DRIVER(irq) = {
198 .id = UCLASS_IRQ,
199 .name = "irq",
200};