2 * regmap based irq_chip
4 * Copyright 2011 Wolfson Microelectronics plc
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/export.h>
14 #include <linux/device.h>
15 #include <linux/regmap.h>
16 #include <linux/irq.h>
17 #include <linux/interrupt.h>
18 #include <linux/irqdomain.h>
19 #include <linux/slab.h>
23 struct regmap_irq_chip_data {
25 struct irq_chip irq_chip;
28 const struct regmap_irq_chip *chip;
31 struct irq_domain *domain;
36 unsigned int *status_buf;
37 unsigned int *mask_buf;
38 unsigned int *mask_buf_def;
39 unsigned int *wake_buf;
41 unsigned int irq_reg_stride;
45 struct regmap_irq *irq_to_regmap_irq(struct regmap_irq_chip_data *data,
48 return &data->chip->irqs[irq];
51 static void regmap_irq_lock(struct irq_data *data)
53 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
58 static void regmap_irq_sync_unlock(struct irq_data *data)
60 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
61 struct regmap *map = d->map;
66 * If there's been a change in the mask write it back to the
67 * hardware. We rely on the use of the regmap core cache to
68 * suppress pointless writes.
70 for (i = 0; i < d->chip->num_regs; i++) {
71 reg = d->chip->mask_base +
72 (i * map->reg_stride * d->irq_reg_stride);
73 ret = regmap_update_bits(d->map, reg,
74 d->mask_buf_def[i], d->mask_buf[i]);
76 dev_err(d->map->dev, "Failed to sync masks in %x\n",
80 /* If we've changed our wakeup count propagate it to the parent */
81 if (d->wake_count < 0)
82 for (i = d->wake_count; i < 0; i++)
83 irq_set_irq_wake(d->irq, 0);
84 else if (d->wake_count > 0)
85 for (i = 0; i < d->wake_count; i++)
86 irq_set_irq_wake(d->irq, 1);
90 mutex_unlock(&d->lock);
93 static void regmap_irq_enable(struct irq_data *data)
95 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
96 struct regmap *map = d->map;
97 const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->hwirq);
99 d->mask_buf[irq_data->reg_offset / map->reg_stride] &= ~irq_data->mask;
102 static void regmap_irq_disable(struct irq_data *data)
104 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
105 struct regmap *map = d->map;
106 const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->hwirq);
108 d->mask_buf[irq_data->reg_offset / map->reg_stride] |= irq_data->mask;
111 static int regmap_irq_set_wake(struct irq_data *data, unsigned int on)
113 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
114 struct regmap *map = d->map;
115 const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->hwirq);
117 if (!d->chip->wake_base)
121 d->wake_buf[irq_data->reg_offset / map->reg_stride]
125 d->wake_buf[irq_data->reg_offset / map->reg_stride]
133 static const struct irq_chip regmap_irq_chip = {
134 .irq_bus_lock = regmap_irq_lock,
135 .irq_bus_sync_unlock = regmap_irq_sync_unlock,
136 .irq_disable = regmap_irq_disable,
137 .irq_enable = regmap_irq_enable,
138 .irq_set_wake = regmap_irq_set_wake,
141 static irqreturn_t regmap_irq_thread(int irq, void *d)
143 struct regmap_irq_chip_data *data = d;
144 const struct regmap_irq_chip *chip = data->chip;
145 struct regmap *map = data->map;
147 bool handled = false;
151 * Ignore masked IRQs and ack if we need to; we ack early so
152 * there is no race between handling and acknowleding the
153 * interrupt. We assume that typically few of the interrupts
154 * will fire simultaneously so don't worry about overhead from
155 * doing a write per register.
157 for (i = 0; i < data->chip->num_regs; i++) {
158 ret = regmap_read(map, chip->status_base + (i * map->reg_stride
159 * data->irq_reg_stride),
160 &data->status_buf[i]);
163 dev_err(map->dev, "Failed to read IRQ status: %d\n",
168 data->status_buf[i] &= ~data->mask_buf[i];
170 if (data->status_buf[i] && chip->ack_base) {
171 reg = chip->ack_base +
172 (i * map->reg_stride * data->irq_reg_stride);
173 ret = regmap_write(map, reg, data->status_buf[i]);
175 dev_err(map->dev, "Failed to ack 0x%x: %d\n",
180 for (i = 0; i < chip->num_irqs; i++) {
181 if (data->status_buf[chip->irqs[i].reg_offset /
182 map->reg_stride] & chip->irqs[i].mask) {
183 handle_nested_irq(irq_find_mapping(data->domain, i));
194 static int regmap_irq_map(struct irq_domain *h, unsigned int virq,
197 struct regmap_irq_chip_data *data = h->host_data;
199 irq_set_chip_data(virq, data);
200 irq_set_chip_and_handler(virq, &data->irq_chip, handle_edge_irq);
201 irq_set_nested_thread(virq, 1);
203 /* ARM needs us to explicitly flag the IRQ as valid
204 * and will set them noprobe when we do so. */
206 set_irq_flags(virq, IRQF_VALID);
208 irq_set_noprobe(virq);
214 static struct irq_domain_ops regmap_domain_ops = {
215 .map = regmap_irq_map,
216 .xlate = irq_domain_xlate_twocell,
220 * regmap_add_irq_chip(): Use standard regmap IRQ controller handling
222 * map: The regmap for the device.
223 * irq: The IRQ the device uses to signal interrupts
224 * irq_flags: The IRQF_ flags to use for the primary interrupt.
225 * chip: Configuration for the interrupt controller.
226 * data: Runtime data structure for the controller, allocated on success
228 * Returns 0 on success or an errno on failure.
230 * In order for this to be efficient the chip really should use a
231 * register cache. The chip driver is responsible for restoring the
232 * register values used by the IRQ controller over suspend and resume.
234 int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
235 int irq_base, const struct regmap_irq_chip *chip,
236 struct regmap_irq_chip_data **data)
238 struct regmap_irq_chip_data *d;
243 for (i = 0; i < chip->num_irqs; i++) {
244 if (chip->irqs[i].reg_offset % map->reg_stride)
246 if (chip->irqs[i].reg_offset / map->reg_stride >=
252 irq_base = irq_alloc_descs(irq_base, 0, chip->num_irqs, 0);
254 dev_warn(map->dev, "Failed to allocate IRQs: %d\n",
260 d = kzalloc(sizeof(*d), GFP_KERNEL);
266 d->status_buf = kzalloc(sizeof(unsigned int) * chip->num_regs,
271 d->mask_buf = kzalloc(sizeof(unsigned int) * chip->num_regs,
276 d->mask_buf_def = kzalloc(sizeof(unsigned int) * chip->num_regs,
278 if (!d->mask_buf_def)
281 if (chip->wake_base) {
282 d->wake_buf = kzalloc(sizeof(unsigned int) * chip->num_regs,
288 d->irq_chip = regmap_irq_chip;
289 d->irq_chip.name = chip->name;
293 d->irq_base = irq_base;
295 if (chip->irq_reg_stride)
296 d->irq_reg_stride = chip->irq_reg_stride;
298 d->irq_reg_stride = 1;
300 mutex_init(&d->lock);
302 for (i = 0; i < chip->num_irqs; i++)
303 d->mask_buf_def[chip->irqs[i].reg_offset / map->reg_stride]
304 |= chip->irqs[i].mask;
306 /* Mask all the interrupts by default */
307 for (i = 0; i < chip->num_regs; i++) {
308 d->mask_buf[i] = d->mask_buf_def[i];
309 reg = chip->mask_base +
310 (i * map->reg_stride * d->irq_reg_stride);
311 ret = regmap_update_bits(map, reg,
312 d->mask_buf[i], d->mask_buf[i]);
314 dev_err(map->dev, "Failed to set masks in 0x%x: %d\n",
321 d->domain = irq_domain_add_legacy(map->dev->of_node,
322 chip->num_irqs, irq_base, 0,
323 ®map_domain_ops, d);
325 d->domain = irq_domain_add_linear(map->dev->of_node,
327 ®map_domain_ops, d);
329 dev_err(map->dev, "Failed to create IRQ domain\n");
334 ret = request_threaded_irq(irq, NULL, regmap_irq_thread, irq_flags,
337 dev_err(map->dev, "Failed to request IRQ %d: %d\n", irq, ret);
344 /* Should really dispose of the domain but... */
347 kfree(d->mask_buf_def);
349 kfree(d->status_buf);
353 EXPORT_SYMBOL_GPL(regmap_add_irq_chip);
356 * regmap_del_irq_chip(): Stop interrupt handling for a regmap IRQ chip
358 * @irq: Primary IRQ for the device
359 * @d: regmap_irq_chip_data allocated by regmap_add_irq_chip()
361 void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *d)
367 /* We should unmap the domain but... */
369 kfree(d->mask_buf_def);
371 kfree(d->status_buf);
374 EXPORT_SYMBOL_GPL(regmap_del_irq_chip);
377 * regmap_irq_chip_get_base(): Retrieve interrupt base for a regmap IRQ chip
379 * Useful for drivers to request their own IRQs.
381 * @data: regmap_irq controller to operate on.
383 int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data)
385 WARN_ON(!data->irq_base);
386 return data->irq_base;
388 EXPORT_SYMBOL_GPL(regmap_irq_chip_get_base);
391 * regmap_irq_get_virq(): Map an interrupt on a chip to a virtual IRQ
393 * Useful for drivers to request their own IRQs.
395 * @data: regmap_irq controller to operate on.
396 * @irq: index of the interrupt requested in the chip IRQs
398 int regmap_irq_get_virq(struct regmap_irq_chip_data *data, int irq)
400 /* Handle holes in the IRQ list */
401 if (!data->chip->irqs[irq].mask)
404 return irq_create_mapping(data->domain, irq);
406 EXPORT_SYMBOL_GPL(regmap_irq_get_virq);