Merge remote-tracking branch 'bluetooth/master'
[linux-2.6/next.git] / kernel / irq / irqdomain.c
blobd5828da3fd383908c142971a0b5a91c10be17f71
1 #include <linux/irq.h>
2 #include <linux/irqdomain.h>
3 #include <linux/module.h>
4 #include <linux/mutex.h>
5 #include <linux/of.h>
6 #include <linux/of_address.h>
7 #include <linux/slab.h>
9 static LIST_HEAD(irq_domain_list);
10 static DEFINE_MUTEX(irq_domain_mutex);
12 /**
13 * irq_domain_add() - Register an irq_domain
14 * @domain: ptr to initialized irq_domain structure
16 * Registers an irq_domain structure. The irq_domain must at a minimum be
17 * initialized with an ops structure pointer, and either a ->to_irq hook or
18 * a valid irq_base value. Everything else is optional.
20 void irq_domain_add(struct irq_domain *domain)
22 struct irq_data *d;
23 int hwirq;
26 * This assumes that the irq_domain owner has already allocated
27 * the irq_descs. This block will be removed when support for dynamic
28 * allocation of irq_descs is added to irq_domain.
30 for (hwirq = 0; hwirq < domain->nr_irq; hwirq++) {
31 d = irq_get_irq_data(irq_domain_to_irq(domain, hwirq));
32 if (d || d->domain) {
33 /* things are broken; just report, don't clean up */
34 WARN(1, "error: irq_desc already assigned to a domain");
35 return;
37 d->domain = domain;
38 d->hwirq = hwirq;
41 mutex_lock(&irq_domain_mutex);
42 list_add(&domain->list, &irq_domain_list);
43 mutex_unlock(&irq_domain_mutex);
46 /**
47 * irq_domain_del() - Unregister an irq_domain
48 * @domain: ptr to registered irq_domain.
50 void irq_domain_del(struct irq_domain *domain)
52 struct irq_data *d;
53 int hwirq;
55 mutex_lock(&irq_domain_mutex);
56 list_del(&domain->list);
57 mutex_unlock(&irq_domain_mutex);
59 /* Clear the irq_domain assignments */
60 for (hwirq = 0; hwirq < domain->nr_irq; hwirq++) {
61 d = irq_get_irq_data(irq_domain_to_irq(domain, hwirq));
62 d->domain = NULL;
66 #if defined(CONFIG_OF_IRQ)
67 /**
68 * irq_create_of_mapping() - Map a linux irq number from a DT interrupt spec
70 * Used by the device tree interrupt mapping code to translate a device tree
71 * interrupt specifier to a valid linux irq number. Returns either a valid
72 * linux IRQ number or 0.
74 * When the caller no longer need the irq number returned by this function it
75 * should arrange to call irq_dispose_mapping().
77 unsigned int irq_create_of_mapping(struct device_node *controller,
78 const u32 *intspec, unsigned int intsize)
80 struct irq_domain *domain;
81 unsigned long hwirq;
82 unsigned int irq, type;
83 int rc = -EINVAL;
85 /* Find a domain which can translate the irq spec */
86 mutex_lock(&irq_domain_mutex);
87 list_for_each_entry(domain, &irq_domain_list, list) {
88 if (!domain->ops->dt_translate)
89 continue;
90 rc = domain->ops->dt_translate(domain, controller,
91 intspec, intsize, &hwirq, &type);
92 if (rc == 0)
93 break;
95 mutex_unlock(&irq_domain_mutex);
97 if (rc != 0)
98 return 0;
100 irq = irq_domain_to_irq(domain, hwirq);
101 if (type != IRQ_TYPE_NONE)
102 irq_set_irq_type(irq, type);
103 pr_debug("%s: mapped hwirq=%i to irq=%i, flags=%x\n",
104 controller->full_name, (int)hwirq, irq, type);
105 return irq;
107 EXPORT_SYMBOL_GPL(irq_create_of_mapping);
110 * irq_dispose_mapping() - Discard a mapping created by irq_create_of_mapping()
111 * @irq: linux irq number to be discarded
113 * Calling this function indicates the caller no longer needs a reference to
114 * the linux irq number returned by a prior call to irq_create_of_mapping().
116 void irq_dispose_mapping(unsigned int irq)
119 * nothing yet; will be filled when support for dynamic allocation of
120 * irq_descs is added to irq_domain
123 EXPORT_SYMBOL_GPL(irq_dispose_mapping);
125 int irq_domain_simple_dt_translate(struct irq_domain *d,
126 struct device_node *controller,
127 const u32 *intspec, unsigned int intsize,
128 unsigned long *out_hwirq, unsigned int *out_type)
130 if (d->of_node != controller)
131 return -EINVAL;
132 if (intsize < 1)
133 return -EINVAL;
135 *out_hwirq = intspec[0];
136 *out_type = IRQ_TYPE_NONE;
137 if (intsize > 1)
138 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
139 return 0;
142 struct irq_domain_ops irq_domain_simple_ops = {
143 .dt_translate = irq_domain_simple_dt_translate,
145 EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
148 * irq_domain_create_simple() - Set up a 'simple' translation range
150 void irq_domain_add_simple(struct device_node *controller, int irq_base)
152 struct irq_domain *domain;
154 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
155 if (!domain) {
156 WARN_ON(1);
157 return;
160 domain->irq_base = irq_base;
161 domain->of_node = of_node_get(controller);
162 domain->ops = &irq_domain_simple_ops;
163 irq_domain_add(domain);
165 EXPORT_SYMBOL_GPL(irq_domain_add_simple);
167 void irq_domain_generate_simple(const struct of_device_id *match,
168 u64 phys_base, unsigned int irq_start)
170 struct device_node *node;
171 pr_info("looking for phys_base=%llx, irq_start=%i\n",
172 (unsigned long long) phys_base, (int) irq_start);
173 node = of_find_matching_node_by_address(NULL, match, phys_base);
174 if (node)
175 irq_domain_add_simple(node, irq_start);
176 else
177 pr_info("no node found\n");
179 EXPORT_SYMBOL_GPL(irq_domain_generate_simple);
180 #endif /* CONFIG_OF_IRQ */