blob: f4e6dcfc85048715cf8f2d12d07810d592cc2a38 [file] [log] [blame]
Mark Brownf8beab22011-10-28 23:50:49 +02001/*
2 * regmap based irq_chip
3 *
4 * Copyright 2011 Wolfson Microelectronics plc
5 *
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7 *
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.
11 */
12
13#include <linux/export.h>
Paul Gortmaker51990e82012-01-22 11:23:42 -050014#include <linux/device.h>
Mark Brownf8beab22011-10-28 23:50:49 +020015#include <linux/regmap.h>
16#include <linux/irq.h>
17#include <linux/interrupt.h>
18#include <linux/slab.h>
19
20#include "internal.h"
21
22struct regmap_irq_chip_data {
23 struct mutex lock;
24
25 struct regmap *map;
26 struct regmap_irq_chip *chip;
27
28 int irq_base;
29
30 void *status_reg_buf;
31 unsigned int *status_buf;
32 unsigned int *mask_buf;
33 unsigned int *mask_buf_def;
34};
35
36static inline const
37struct regmap_irq *irq_to_regmap_irq(struct regmap_irq_chip_data *data,
38 int irq)
39{
40 return &data->chip->irqs[irq - data->irq_base];
41}
42
43static void regmap_irq_lock(struct irq_data *data)
44{
45 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
46
47 mutex_lock(&d->lock);
48}
49
50static void regmap_irq_sync_unlock(struct irq_data *data)
51{
52 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
53 int i, ret;
54
55 /*
56 * If there's been a change in the mask write it back to the
57 * hardware. We rely on the use of the regmap core cache to
58 * suppress pointless writes.
59 */
60 for (i = 0; i < d->chip->num_regs; i++) {
61 ret = regmap_update_bits(d->map, d->chip->mask_base + i,
62 d->mask_buf_def[i], d->mask_buf[i]);
63 if (ret != 0)
64 dev_err(d->map->dev, "Failed to sync masks in %x\n",
65 d->chip->mask_base + i);
66 }
67
68 mutex_unlock(&d->lock);
69}
70
71static void regmap_irq_enable(struct irq_data *data)
72{
73 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
74 const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->irq);
75
76 d->mask_buf[irq_data->reg_offset] &= ~irq_data->mask;
77}
78
79static void regmap_irq_disable(struct irq_data *data)
80{
81 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
82 const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->irq);
83
84 d->mask_buf[irq_data->reg_offset] |= irq_data->mask;
85}
86
87static struct irq_chip regmap_irq_chip = {
88 .name = "regmap",
89 .irq_bus_lock = regmap_irq_lock,
90 .irq_bus_sync_unlock = regmap_irq_sync_unlock,
91 .irq_disable = regmap_irq_disable,
92 .irq_enable = regmap_irq_enable,
93};
94
95static irqreturn_t regmap_irq_thread(int irq, void *d)
96{
97 struct regmap_irq_chip_data *data = d;
98 struct regmap_irq_chip *chip = data->chip;
99 struct regmap *map = data->map;
100 int ret, i;
101 u8 *buf8 = data->status_reg_buf;
102 u16 *buf16 = data->status_reg_buf;
103 u32 *buf32 = data->status_reg_buf;
Mark Brownd23511f2011-11-28 18:50:39 +0000104 bool handled = false;
Mark Brownf8beab22011-10-28 23:50:49 +0200105
106 ret = regmap_bulk_read(map, chip->status_base, data->status_reg_buf,
107 chip->num_regs);
108 if (ret != 0) {
109 dev_err(map->dev, "Failed to read IRQ status: %d\n", ret);
110 return IRQ_NONE;
111 }
112
113 /*
114 * Ignore masked IRQs and ack if we need to; we ack early so
115 * there is no race between handling and acknowleding the
116 * interrupt. We assume that typically few of the interrupts
117 * will fire simultaneously so don't worry about overhead from
118 * doing a write per register.
119 */
120 for (i = 0; i < data->chip->num_regs; i++) {
121 switch (map->format.val_bytes) {
122 case 1:
123 data->status_buf[i] = buf8[i];
124 break;
125 case 2:
126 data->status_buf[i] = buf16[i];
127 break;
128 case 4:
129 data->status_buf[i] = buf32[i];
130 break;
131 default:
132 BUG();
133 return IRQ_NONE;
134 }
135
136 data->status_buf[i] &= ~data->mask_buf[i];
137
138 if (data->status_buf[i] && chip->ack_base) {
139 ret = regmap_write(map, chip->ack_base + i,
140 data->status_buf[i]);
141 if (ret != 0)
142 dev_err(map->dev, "Failed to ack 0x%x: %d\n",
143 chip->ack_base + i, ret);
144 }
145 }
146
147 for (i = 0; i < chip->num_irqs; i++) {
148 if (data->status_buf[chip->irqs[i].reg_offset] &
149 chip->irqs[i].mask) {
150 handle_nested_irq(data->irq_base + i);
Mark Brownd23511f2011-11-28 18:50:39 +0000151 handled = true;
Mark Brownf8beab22011-10-28 23:50:49 +0200152 }
153 }
154
Mark Brownd23511f2011-11-28 18:50:39 +0000155 if (handled)
156 return IRQ_HANDLED;
157 else
158 return IRQ_NONE;
Mark Brownf8beab22011-10-28 23:50:49 +0200159}
160
161/**
162 * regmap_add_irq_chip(): Use standard regmap IRQ controller handling
163 *
164 * map: The regmap for the device.
165 * irq: The IRQ the device uses to signal interrupts
166 * irq_flags: The IRQF_ flags to use for the primary interrupt.
167 * chip: Configuration for the interrupt controller.
168 * data: Runtime data structure for the controller, allocated on success
169 *
170 * Returns 0 on success or an errno on failure.
171 *
172 * In order for this to be efficient the chip really should use a
173 * register cache. The chip driver is responsible for restoring the
174 * register values used by the IRQ controller over suspend and resume.
175 */
176int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
177 int irq_base, struct regmap_irq_chip *chip,
178 struct regmap_irq_chip_data **data)
179{
180 struct regmap_irq_chip_data *d;
181 int cur_irq, i;
182 int ret = -ENOMEM;
183
184 irq_base = irq_alloc_descs(irq_base, 0, chip->num_irqs, 0);
185 if (irq_base < 0) {
186 dev_warn(map->dev, "Failed to allocate IRQs: %d\n",
187 irq_base);
188 return irq_base;
189 }
190
191 d = kzalloc(sizeof(*d), GFP_KERNEL);
192 if (!d)
193 return -ENOMEM;
194
Mark Brown2431d0a2012-05-13 11:18:34 +0100195 *data = d;
196
Mark Brownf8beab22011-10-28 23:50:49 +0200197 d->status_buf = kzalloc(sizeof(unsigned int) * chip->num_regs,
198 GFP_KERNEL);
199 if (!d->status_buf)
200 goto err_alloc;
201
202 d->status_reg_buf = kzalloc(map->format.val_bytes * chip->num_regs,
203 GFP_KERNEL);
204 if (!d->status_reg_buf)
205 goto err_alloc;
206
207 d->mask_buf = kzalloc(sizeof(unsigned int) * chip->num_regs,
208 GFP_KERNEL);
209 if (!d->mask_buf)
210 goto err_alloc;
211
212 d->mask_buf_def = kzalloc(sizeof(unsigned int) * chip->num_regs,
213 GFP_KERNEL);
214 if (!d->mask_buf_def)
215 goto err_alloc;
216
217 d->map = map;
218 d->chip = chip;
219 d->irq_base = irq_base;
220 mutex_init(&d->lock);
221
222 for (i = 0; i < chip->num_irqs; i++)
223 d->mask_buf_def[chip->irqs[i].reg_offset]
224 |= chip->irqs[i].mask;
225
226 /* Mask all the interrupts by default */
227 for (i = 0; i < chip->num_regs; i++) {
228 d->mask_buf[i] = d->mask_buf_def[i];
229 ret = regmap_write(map, chip->mask_base + i, d->mask_buf[i]);
230 if (ret != 0) {
231 dev_err(map->dev, "Failed to set masks in 0x%x: %d\n",
232 chip->mask_base + i, ret);
233 goto err_alloc;
234 }
235 }
236
237 /* Register them with genirq */
238 for (cur_irq = irq_base;
239 cur_irq < chip->num_irqs + irq_base;
240 cur_irq++) {
241 irq_set_chip_data(cur_irq, d);
242 irq_set_chip_and_handler(cur_irq, &regmap_irq_chip,
243 handle_edge_irq);
244 irq_set_nested_thread(cur_irq, 1);
245
246 /* ARM needs us to explicitly flag the IRQ as valid
247 * and will set them noprobe when we do so. */
248#ifdef CONFIG_ARM
249 set_irq_flags(cur_irq, IRQF_VALID);
250#else
251 irq_set_noprobe(cur_irq);
252#endif
253 }
254
255 ret = request_threaded_irq(irq, NULL, regmap_irq_thread, irq_flags,
256 chip->name, d);
257 if (ret != 0) {
258 dev_err(map->dev, "Failed to request IRQ %d: %d\n", irq, ret);
259 goto err_alloc;
260 }
261
262 return 0;
263
264err_alloc:
265 kfree(d->mask_buf_def);
266 kfree(d->mask_buf);
267 kfree(d->status_reg_buf);
268 kfree(d->status_buf);
269 kfree(d);
270 return ret;
271}
272EXPORT_SYMBOL_GPL(regmap_add_irq_chip);
273
274/**
275 * regmap_del_irq_chip(): Stop interrupt handling for a regmap IRQ chip
276 *
277 * @irq: Primary IRQ for the device
278 * @d: regmap_irq_chip_data allocated by regmap_add_irq_chip()
279 */
280void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *d)
281{
282 if (!d)
283 return;
284
285 free_irq(irq, d);
286 kfree(d->mask_buf_def);
287 kfree(d->mask_buf);
288 kfree(d->status_reg_buf);
289 kfree(d->status_buf);
290 kfree(d);
291}
292EXPORT_SYMBOL_GPL(regmap_del_irq_chip);
Mark Brown209a6002011-12-05 16:10:15 +0000293
294/**
295 * regmap_irq_chip_get_base(): Retrieve interrupt base for a regmap IRQ chip
296 *
297 * Useful for drivers to request their own IRQs.
298 *
299 * @data: regmap_irq controller to operate on.
300 */
301int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data)
302{
303 return data->irq_base;
304}
305EXPORT_SYMBOL_GPL(regmap_irq_chip_get_base);