blob: 8d275014e463c001383aed34e4076d072e332a9b [file] [log] [blame]
Grant Likely6469dfb2011-07-26 03:19:06 -06001#include <linux/irq.h>
2#include <linux/irqdomain.h>
3#include <linux/module.h>
4#include <linux/mutex.h>
5#include <linux/of.h>
Grant Likely971a0ee2011-07-26 03:19:06 -06006#include <linux/of_address.h>
7#include <linux/slab.h>
Grant Likely6469dfb2011-07-26 03:19:06 -06008
9static LIST_HEAD(irq_domain_list);
10static DEFINE_MUTEX(irq_domain_mutex);
11
12/**
13 * irq_domain_add() - Register an irq_domain
14 * @domain: ptr to initialized irq_domain structure
15 *
Michael Bohane6717fd2012-01-03 16:37:43 -080016 * Adds a irq_domain structure. The irq_domain must at a minimum be
Grant Likely6469dfb2011-07-26 03:19:06 -060017 * initialized with an ops structure pointer, and either a ->to_irq hook or
18 * a valid irq_base value. Everything else is optional.
19 */
20void irq_domain_add(struct irq_domain *domain)
21{
Michael Bohane6717fd2012-01-03 16:37:43 -080022 mutex_lock(&irq_domain_mutex);
23 list_add(&domain->list, &irq_domain_list);
24 mutex_unlock(&irq_domain_mutex);
25}
26
27/**
28 * irq_domain_register() - Register an entire irq_domain
29 * @domain: ptr to initialized irq_domain structure
30 *
31 * Registers the entire irq_domain. The irq_domain must at a minimum be
32 * initialized with an ops structure pointer, and either a ->to_irq hook or
33 * a valid irq_base value. Everything else is optional.
34 */
35void irq_domain_register(struct irq_domain *domain)
36{
Grant Likely6469dfb2011-07-26 03:19:06 -060037 struct irq_data *d;
Rob Herring60fe4602011-09-30 10:48:38 -050038 int hwirq, irq;
Grant Likely6469dfb2011-07-26 03:19:06 -060039
Rob Herring60fe4602011-09-30 10:48:38 -050040 irq_domain_for_each_irq(domain, hwirq, irq) {
41 d = irq_get_irq_data(irq);
Rob Herring36b2c362011-09-14 11:31:37 -050042 if (!d) {
43 WARN(1, "error: assigning domain to non existant irq_desc");
44 return;
45 }
46 if (d->domain) {
Grant Likely6469dfb2011-07-26 03:19:06 -060047 /* things are broken; just report, don't clean up */
48 WARN(1, "error: irq_desc already assigned to a domain");
49 return;
50 }
51 d->domain = domain;
52 d->hwirq = hwirq;
53 }
Grant Likely6469dfb2011-07-26 03:19:06 -060054}
55
56/**
Michael Bohane6717fd2012-01-03 16:37:43 -080057 * irq_domain_register_irq() - Register an irq_domain
58 * @domain: ptr to initialized irq_domain structure
59 * @hwirq: irq_domain hwirq to register
60 *
61 * Registers a specific hwirq within the irq_domain. The irq_domain
62 * must at a minimum be initialized with an ops structure pointer, and
63 * either a ->to_irq hook or a valid irq_base value. Everything else is
64 * optional.
65 */
66void irq_domain_register_irq(struct irq_domain *domain, int hwirq)
67{
68 struct irq_data *d;
69
70 d = irq_get_irq_data(irq_domain_to_irq(domain, hwirq));
71 if (!d) {
72 WARN(1, "error: assigning domain to non existant irq_desc");
73 return;
74 }
75 if (d->domain) {
76 /* things are broken; just report, don't clean up */
77 WARN(1, "error: irq_desc already assigned to a domain");
78 return;
79 }
80 d->domain = domain;
81 d->hwirq = hwirq;
82}
83
84/**
85 * irq_domain_del() - Removes a irq_domain from the system
Grant Likely6469dfb2011-07-26 03:19:06 -060086 * @domain: ptr to registered irq_domain.
87 */
88void irq_domain_del(struct irq_domain *domain)
89{
Grant Likely6469dfb2011-07-26 03:19:06 -060090 mutex_lock(&irq_domain_mutex);
91 list_del(&domain->list);
92 mutex_unlock(&irq_domain_mutex);
Michael Bohane6717fd2012-01-03 16:37:43 -080093}
94
95/**
96 * irq_domain_unregister() - Unregister an irq_domain
97 * @domain: ptr to registered irq_domain.
98 */
99void irq_domain_unregister(struct irq_domain *domain)
100{
101 struct irq_data *d;
102 int hwirq, irq;
Grant Likely6469dfb2011-07-26 03:19:06 -0600103
104 /* Clear the irq_domain assignments */
Rob Herring60fe4602011-09-30 10:48:38 -0500105 irq_domain_for_each_irq(domain, hwirq, irq) {
106 d = irq_get_irq_data(irq);
Grant Likely6469dfb2011-07-26 03:19:06 -0600107 d->domain = NULL;
108 }
109}
110
Michael Bohane6717fd2012-01-03 16:37:43 -0800111/**
112 * irq_domain_unregister_irq() - Unregister a hwirq within a irq_domain
113 * @domain: ptr to registered irq_domain.
114 * @hwirq: irq_domain hwirq to unregister.
115 */
116void irq_domain_unregister_irq(struct irq_domain *domain, int hwirq)
117{
118 struct irq_data *d;
119
120 /* Clear the irq_domain assignment */
121 d = irq_get_irq_data(irq_domain_to_irq(domain, hwirq));
122 d->domain = NULL;
123}
124
Grant Likely6469dfb2011-07-26 03:19:06 -0600125#if defined(CONFIG_OF_IRQ)
126/**
127 * irq_create_of_mapping() - Map a linux irq number from a DT interrupt spec
128 *
129 * Used by the device tree interrupt mapping code to translate a device tree
130 * interrupt specifier to a valid linux irq number. Returns either a valid
131 * linux IRQ number or 0.
132 *
133 * When the caller no longer need the irq number returned by this function it
134 * should arrange to call irq_dispose_mapping().
135 */
136unsigned int irq_create_of_mapping(struct device_node *controller,
137 const u32 *intspec, unsigned int intsize)
138{
139 struct irq_domain *domain;
140 unsigned long hwirq;
141 unsigned int irq, type;
142 int rc = -EINVAL;
143
144 /* Find a domain which can translate the irq spec */
145 mutex_lock(&irq_domain_mutex);
146 list_for_each_entry(domain, &irq_domain_list, list) {
147 if (!domain->ops->dt_translate)
148 continue;
149 rc = domain->ops->dt_translate(domain, controller,
150 intspec, intsize, &hwirq, &type);
151 if (rc == 0)
152 break;
153 }
154 mutex_unlock(&irq_domain_mutex);
155
156 if (rc != 0)
157 return 0;
158
159 irq = irq_domain_to_irq(domain, hwirq);
160 if (type != IRQ_TYPE_NONE)
161 irq_set_irq_type(irq, type);
162 pr_debug("%s: mapped hwirq=%i to irq=%i, flags=%x\n",
163 controller->full_name, (int)hwirq, irq, type);
164 return irq;
165}
166EXPORT_SYMBOL_GPL(irq_create_of_mapping);
167
168/**
169 * irq_dispose_mapping() - Discard a mapping created by irq_create_of_mapping()
170 * @irq: linux irq number to be discarded
171 *
172 * Calling this function indicates the caller no longer needs a reference to
173 * the linux irq number returned by a prior call to irq_create_of_mapping().
174 */
175void irq_dispose_mapping(unsigned int irq)
176{
177 /*
178 * nothing yet; will be filled when support for dynamic allocation of
179 * irq_descs is added to irq_domain
180 */
181}
182EXPORT_SYMBOL_GPL(irq_dispose_mapping);
Grant Likely971a0ee2011-07-26 03:19:06 -0600183
184int irq_domain_simple_dt_translate(struct irq_domain *d,
185 struct device_node *controller,
186 const u32 *intspec, unsigned int intsize,
187 unsigned long *out_hwirq, unsigned int *out_type)
188{
189 if (d->of_node != controller)
190 return -EINVAL;
191 if (intsize < 1)
192 return -EINVAL;
193
194 *out_hwirq = intspec[0];
195 *out_type = IRQ_TYPE_NONE;
196 if (intsize > 1)
197 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
198 return 0;
199}
200
201struct irq_domain_ops irq_domain_simple_ops = {
202 .dt_translate = irq_domain_simple_dt_translate,
203};
204EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
205
206/**
207 * irq_domain_create_simple() - Set up a 'simple' translation range
208 */
209void irq_domain_add_simple(struct device_node *controller, int irq_base)
210{
211 struct irq_domain *domain;
212
213 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
214 if (!domain) {
215 WARN_ON(1);
216 return;
217 }
218
219 domain->irq_base = irq_base;
220 domain->of_node = of_node_get(controller);
221 domain->ops = &irq_domain_simple_ops;
222 irq_domain_add(domain);
Michael Bohane6717fd2012-01-03 16:37:43 -0800223 irq_domain_register(domain);
Grant Likely971a0ee2011-07-26 03:19:06 -0600224}
225EXPORT_SYMBOL_GPL(irq_domain_add_simple);
226
227void irq_domain_generate_simple(const struct of_device_id *match,
228 u64 phys_base, unsigned int irq_start)
229{
230 struct device_node *node;
231 pr_info("looking for phys_base=%llx, irq_start=%i\n",
232 (unsigned long long) phys_base, (int) irq_start);
233 node = of_find_matching_node_by_address(NULL, match, phys_base);
234 if (node)
235 irq_domain_add_simple(node, irq_start);
236 else
237 pr_info("no node found\n");
238}
239EXPORT_SYMBOL_GPL(irq_domain_generate_simple);
Grant Likely6469dfb2011-07-26 03:19:06 -0600240#endif /* CONFIG_OF_IRQ */