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
{
27 const struct regmap_irq_chip
*chip
;
30 struct irq_domain
*domain
;
35 unsigned int *status_buf
;
36 unsigned int *mask_buf
;
37 unsigned int *mask_buf_def
;
38 unsigned int *wake_buf
;
40 unsigned int irq_reg_stride
;
44 struct regmap_irq
*irq_to_regmap_irq(struct regmap_irq_chip_data
*data
,
47 return &data
->chip
->irqs
[irq
];
50 static void regmap_irq_lock(struct irq_data
*data
)
52 struct regmap_irq_chip_data
*d
= irq_data_get_irq_chip_data(data
);
57 static void regmap_irq_sync_unlock(struct irq_data
*data
)
59 struct regmap_irq_chip_data
*d
= irq_data_get_irq_chip_data(data
);
60 struct regmap
*map
= d
->map
;
64 * If there's been a change in the mask write it back to the
65 * hardware. We rely on the use of the regmap core cache to
66 * suppress pointless writes.
68 for (i
= 0; i
< d
->chip
->num_regs
; i
++) {
69 ret
= regmap_update_bits(d
->map
, d
->chip
->mask_base
+
70 (i
* map
->reg_stride
*
72 d
->mask_buf_def
[i
], d
->mask_buf
[i
]);
74 dev_err(d
->map
->dev
, "Failed to sync masks in %x\n",
75 d
->chip
->mask_base
+ (i
* map
->reg_stride
));
78 /* If we've changed our wakeup count propagate it to the parent */
79 if (d
->wake_count
< 0)
80 for (i
= d
->wake_count
; i
< 0; i
++)
81 irq_set_irq_wake(d
->irq
, 0);
82 else if (d
->wake_count
> 0)
83 for (i
= 0; i
< d
->wake_count
; i
++)
84 irq_set_irq_wake(d
->irq
, 1);
88 mutex_unlock(&d
->lock
);
91 static void regmap_irq_enable(struct irq_data
*data
)
93 struct regmap_irq_chip_data
*d
= irq_data_get_irq_chip_data(data
);
94 struct regmap
*map
= d
->map
;
95 const struct regmap_irq
*irq_data
= irq_to_regmap_irq(d
, data
->hwirq
);
97 d
->mask_buf
[irq_data
->reg_offset
/ map
->reg_stride
] &= ~irq_data
->mask
;
100 static void regmap_irq_disable(struct irq_data
*data
)
102 struct regmap_irq_chip_data
*d
= irq_data_get_irq_chip_data(data
);
103 struct regmap
*map
= d
->map
;
104 const struct regmap_irq
*irq_data
= irq_to_regmap_irq(d
, data
->hwirq
);
106 d
->mask_buf
[irq_data
->reg_offset
/ map
->reg_stride
] |= irq_data
->mask
;
109 static int regmap_irq_set_wake(struct irq_data
*data
, unsigned int on
)
111 struct regmap_irq_chip_data
*d
= irq_data_get_irq_chip_data(data
);
112 struct regmap
*map
= d
->map
;
113 const struct regmap_irq
*irq_data
= irq_to_regmap_irq(d
, data
->hwirq
);
115 if (!d
->chip
->wake_base
)
119 d
->wake_buf
[irq_data
->reg_offset
/ map
->reg_stride
]
123 d
->wake_buf
[irq_data
->reg_offset
/ map
->reg_stride
]
131 static struct irq_chip regmap_irq_chip
= {
133 .irq_bus_lock
= regmap_irq_lock
,
134 .irq_bus_sync_unlock
= regmap_irq_sync_unlock
,
135 .irq_disable
= regmap_irq_disable
,
136 .irq_enable
= regmap_irq_enable
,
137 .irq_set_wake
= regmap_irq_set_wake
,
140 static irqreturn_t
regmap_irq_thread(int irq
, void *d
)
142 struct regmap_irq_chip_data
*data
= d
;
143 const struct regmap_irq_chip
*chip
= data
->chip
;
144 struct regmap
*map
= data
->map
;
146 bool handled
= false;
149 * Ignore masked IRQs and ack if we need to; we ack early so
150 * there is no race between handling and acknowleding the
151 * interrupt. We assume that typically few of the interrupts
152 * will fire simultaneously so don't worry about overhead from
153 * doing a write per register.
155 for (i
= 0; i
< data
->chip
->num_regs
; i
++) {
156 ret
= regmap_read(map
, chip
->status_base
+ (i
* map
->reg_stride
157 * data
->irq_reg_stride
),
158 &data
->status_buf
[i
]);
161 dev_err(map
->dev
, "Failed to read IRQ status: %d\n",
166 data
->status_buf
[i
] &= ~data
->mask_buf
[i
];
168 if (data
->status_buf
[i
] && chip
->ack_base
) {
169 ret
= regmap_write(map
, chip
->ack_base
+
170 (i
* map
->reg_stride
*
171 data
->irq_reg_stride
),
172 data
->status_buf
[i
]);
174 dev_err(map
->dev
, "Failed to ack 0x%x: %d\n",
175 chip
->ack_base
+ (i
* map
->reg_stride
),
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
, ®map_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
;
242 for (i
= 0; i
< chip
->num_irqs
; i
++) {
243 if (chip
->irqs
[i
].reg_offset
% map
->reg_stride
)
245 if (chip
->irqs
[i
].reg_offset
/ map
->reg_stride
>=
251 irq_base
= irq_alloc_descs(irq_base
, 0, chip
->num_irqs
, 0);
253 dev_warn(map
->dev
, "Failed to allocate IRQs: %d\n",
259 d
= kzalloc(sizeof(*d
), GFP_KERNEL
);
265 d
->status_buf
= kzalloc(sizeof(unsigned int) * chip
->num_regs
,
270 d
->mask_buf
= kzalloc(sizeof(unsigned int) * chip
->num_regs
,
275 d
->mask_buf_def
= kzalloc(sizeof(unsigned int) * chip
->num_regs
,
277 if (!d
->mask_buf_def
)
280 if (chip
->wake_base
) {
281 d
->wake_buf
= kzalloc(sizeof(unsigned int) * chip
->num_regs
,
290 d
->irq_base
= irq_base
;
292 if (chip
->irq_reg_stride
)
293 d
->irq_reg_stride
= chip
->irq_reg_stride
;
295 d
->irq_reg_stride
= 1;
297 mutex_init(&d
->lock
);
299 for (i
= 0; i
< chip
->num_irqs
; i
++)
300 d
->mask_buf_def
[chip
->irqs
[i
].reg_offset
/ map
->reg_stride
]
301 |= chip
->irqs
[i
].mask
;
303 /* Mask all the interrupts by default */
304 for (i
= 0; i
< chip
->num_regs
; i
++) {
305 d
->mask_buf
[i
] = d
->mask_buf_def
[i
];
306 ret
= regmap_write(map
, chip
->mask_base
+ (i
* map
->reg_stride
307 * d
->irq_reg_stride
),
310 dev_err(map
->dev
, "Failed to set masks in 0x%x: %d\n",
311 chip
->mask_base
+ (i
* map
->reg_stride
), ret
);
317 d
->domain
= irq_domain_add_legacy(map
->dev
->of_node
,
318 chip
->num_irqs
, irq_base
, 0,
319 ®map_domain_ops
, d
);
321 d
->domain
= irq_domain_add_linear(map
->dev
->of_node
,
323 ®map_domain_ops
, d
);
325 dev_err(map
->dev
, "Failed to create IRQ domain\n");
330 ret
= request_threaded_irq(irq
, NULL
, regmap_irq_thread
, irq_flags
,
333 dev_err(map
->dev
, "Failed to request IRQ %d: %d\n", irq
, ret
);
340 /* Should really dispose of the domain but... */
343 kfree(d
->mask_buf_def
);
345 kfree(d
->status_buf
);
349 EXPORT_SYMBOL_GPL(regmap_add_irq_chip
);
352 * regmap_del_irq_chip(): Stop interrupt handling for a regmap IRQ chip
354 * @irq: Primary IRQ for the device
355 * @d: regmap_irq_chip_data allocated by regmap_add_irq_chip()
357 void regmap_del_irq_chip(int irq
, struct regmap_irq_chip_data
*d
)
363 /* We should unmap the domain but... */
365 kfree(d
->mask_buf_def
);
367 kfree(d
->status_buf
);
370 EXPORT_SYMBOL_GPL(regmap_del_irq_chip
);
373 * regmap_irq_chip_get_base(): Retrieve interrupt base for a regmap IRQ chip
375 * Useful for drivers to request their own IRQs.
377 * @data: regmap_irq controller to operate on.
379 int regmap_irq_chip_get_base(struct regmap_irq_chip_data
*data
)
381 WARN_ON(!data
->irq_base
);
382 return data
->irq_base
;
384 EXPORT_SYMBOL_GPL(regmap_irq_chip_get_base
);
387 * regmap_irq_get_virq(): Map an interrupt on a chip to a virtual IRQ
389 * Useful for drivers to request their own IRQs.
391 * @data: regmap_irq controller to operate on.
392 * @irq: index of the interrupt requested in the chip IRQs
394 int regmap_irq_get_virq(struct regmap_irq_chip_data
*data
, int irq
)
396 /* Handle holes in the IRQ list */
397 if (!data
->chip
->irqs
[irq
].mask
)
400 return irq_create_mapping(data
->domain
, irq
);
402 EXPORT_SYMBOL_GPL(regmap_irq_get_virq
);