blob: fc69d29d272aa56cea9d8cd9910e2c4399f26c12 [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);
Stephen Warren56806552012-04-10 23:37:22 -060053 struct regmap *map = d->map;
Mark Brownf8beab22011-10-28 23:50:49 +020054 int i, ret;
55
56 /*
57 * If there's been a change in the mask write it back to the
58 * hardware. We rely on the use of the regmap core cache to
59 * suppress pointless writes.
60 */
61 for (i = 0; i < d->chip->num_regs; i++) {
Stephen Warrenf01ee602012-04-09 13:40:24 -060062 ret = regmap_update_bits(d->map, d->chip->mask_base +
Stephen Warren56806552012-04-10 23:37:22 -060063 (i * map->reg_stride),
Mark Brownf8beab22011-10-28 23:50:49 +020064 d->mask_buf_def[i], d->mask_buf[i]);
65 if (ret != 0)
66 dev_err(d->map->dev, "Failed to sync masks in %x\n",
Stephen Warrenf01ee602012-04-09 13:40:24 -060067 d->chip->mask_base + (i * map->reg_stride));
Mark Brownf8beab22011-10-28 23:50:49 +020068 }
69
70 mutex_unlock(&d->lock);
71}
72
73static void regmap_irq_enable(struct irq_data *data)
74{
75 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
Stephen Warren56806552012-04-10 23:37:22 -060076 struct regmap *map = d->map;
Mark Brownf8beab22011-10-28 23:50:49 +020077 const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->irq);
78
Stephen Warrenf01ee602012-04-09 13:40:24 -060079 d->mask_buf[irq_data->reg_offset / map->reg_stride] &= ~irq_data->mask;
Mark Brownf8beab22011-10-28 23:50:49 +020080}
81
82static void regmap_irq_disable(struct irq_data *data)
83{
84 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
Stephen Warren56806552012-04-10 23:37:22 -060085 struct regmap *map = d->map;
Mark Brownf8beab22011-10-28 23:50:49 +020086 const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->irq);
87
Stephen Warrenf01ee602012-04-09 13:40:24 -060088 d->mask_buf[irq_data->reg_offset / map->reg_stride] |= irq_data->mask;
Mark Brownf8beab22011-10-28 23:50:49 +020089}
90
91static struct irq_chip regmap_irq_chip = {
92 .name = "regmap",
93 .irq_bus_lock = regmap_irq_lock,
94 .irq_bus_sync_unlock = regmap_irq_sync_unlock,
95 .irq_disable = regmap_irq_disable,
96 .irq_enable = regmap_irq_enable,
97};
98
99static irqreturn_t regmap_irq_thread(int irq, void *d)
100{
101 struct regmap_irq_chip_data *data = d;
102 struct regmap_irq_chip *chip = data->chip;
103 struct regmap *map = data->map;
104 int ret, i;
105 u8 *buf8 = data->status_reg_buf;
106 u16 *buf16 = data->status_reg_buf;
107 u32 *buf32 = data->status_reg_buf;
Mark Brownd23511f2011-11-28 18:50:39 +0000108 bool handled = false;
Mark Brownf8beab22011-10-28 23:50:49 +0200109
110 ret = regmap_bulk_read(map, chip->status_base, data->status_reg_buf,
111 chip->num_regs);
112 if (ret != 0) {
113 dev_err(map->dev, "Failed to read IRQ status: %d\n", ret);
114 return IRQ_NONE;
115 }
116
117 /*
118 * Ignore masked IRQs and ack if we need to; we ack early so
119 * there is no race between handling and acknowleding the
120 * interrupt. We assume that typically few of the interrupts
121 * will fire simultaneously so don't worry about overhead from
122 * doing a write per register.
123 */
124 for (i = 0; i < data->chip->num_regs; i++) {
125 switch (map->format.val_bytes) {
126 case 1:
127 data->status_buf[i] = buf8[i];
128 break;
129 case 2:
130 data->status_buf[i] = buf16[i];
131 break;
132 case 4:
133 data->status_buf[i] = buf32[i];
134 break;
135 default:
136 BUG();
137 return IRQ_NONE;
138 }
139
140 data->status_buf[i] &= ~data->mask_buf[i];
141
142 if (data->status_buf[i] && chip->ack_base) {
Stephen Warrenf01ee602012-04-09 13:40:24 -0600143 ret = regmap_write(map, chip->ack_base +
144 (i * map->reg_stride),
Mark Brownf8beab22011-10-28 23:50:49 +0200145 data->status_buf[i]);
146 if (ret != 0)
147 dev_err(map->dev, "Failed to ack 0x%x: %d\n",
Stephen Warrenf01ee602012-04-09 13:40:24 -0600148 chip->ack_base + (i * map->reg_stride),
149 ret);
Mark Brownf8beab22011-10-28 23:50:49 +0200150 }
151 }
152
153 for (i = 0; i < chip->num_irqs; i++) {
Stephen Warrenf01ee602012-04-09 13:40:24 -0600154 if (data->status_buf[chip->irqs[i].reg_offset /
155 map->reg_stride] & chip->irqs[i].mask) {
Mark Brownf8beab22011-10-28 23:50:49 +0200156 handle_nested_irq(data->irq_base + i);
Mark Brownd23511f2011-11-28 18:50:39 +0000157 handled = true;
Mark Brownf8beab22011-10-28 23:50:49 +0200158 }
159 }
160
Mark Brownd23511f2011-11-28 18:50:39 +0000161 if (handled)
162 return IRQ_HANDLED;
163 else
164 return IRQ_NONE;
Mark Brownf8beab22011-10-28 23:50:49 +0200165}
166
167/**
168 * regmap_add_irq_chip(): Use standard regmap IRQ controller handling
169 *
170 * map: The regmap for the device.
171 * irq: The IRQ the device uses to signal interrupts
172 * irq_flags: The IRQF_ flags to use for the primary interrupt.
173 * chip: Configuration for the interrupt controller.
174 * data: Runtime data structure for the controller, allocated on success
175 *
176 * Returns 0 on success or an errno on failure.
177 *
178 * In order for this to be efficient the chip really should use a
179 * register cache. The chip driver is responsible for restoring the
180 * register values used by the IRQ controller over suspend and resume.
181 */
182int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
183 int irq_base, struct regmap_irq_chip *chip,
184 struct regmap_irq_chip_data **data)
185{
186 struct regmap_irq_chip_data *d;
187 int cur_irq, i;
188 int ret = -ENOMEM;
189
Stephen Warrenf01ee602012-04-09 13:40:24 -0600190 for (i = 0; i < chip->num_irqs; i++) {
191 if (chip->irqs[i].reg_offset % map->reg_stride)
192 return -EINVAL;
193 if (chip->irqs[i].reg_offset / map->reg_stride >=
194 chip->num_regs)
195 return -EINVAL;
196 }
197
Mark Brownf8beab22011-10-28 23:50:49 +0200198 irq_base = irq_alloc_descs(irq_base, 0, chip->num_irqs, 0);
199 if (irq_base < 0) {
200 dev_warn(map->dev, "Failed to allocate IRQs: %d\n",
201 irq_base);
202 return irq_base;
203 }
204
205 d = kzalloc(sizeof(*d), GFP_KERNEL);
206 if (!d)
207 return -ENOMEM;
208
209 d->status_buf = kzalloc(sizeof(unsigned int) * chip->num_regs,
210 GFP_KERNEL);
211 if (!d->status_buf)
212 goto err_alloc;
213
214 d->status_reg_buf = kzalloc(map->format.val_bytes * chip->num_regs,
215 GFP_KERNEL);
216 if (!d->status_reg_buf)
217 goto err_alloc;
218
219 d->mask_buf = kzalloc(sizeof(unsigned int) * chip->num_regs,
220 GFP_KERNEL);
221 if (!d->mask_buf)
222 goto err_alloc;
223
224 d->mask_buf_def = kzalloc(sizeof(unsigned int) * chip->num_regs,
225 GFP_KERNEL);
226 if (!d->mask_buf_def)
227 goto err_alloc;
228
229 d->map = map;
230 d->chip = chip;
231 d->irq_base = irq_base;
232 mutex_init(&d->lock);
233
234 for (i = 0; i < chip->num_irqs; i++)
Stephen Warrenf01ee602012-04-09 13:40:24 -0600235 d->mask_buf_def[chip->irqs[i].reg_offset / map->reg_stride]
Mark Brownf8beab22011-10-28 23:50:49 +0200236 |= chip->irqs[i].mask;
237
238 /* Mask all the interrupts by default */
239 for (i = 0; i < chip->num_regs; i++) {
240 d->mask_buf[i] = d->mask_buf_def[i];
Stephen Warrenf01ee602012-04-09 13:40:24 -0600241 ret = regmap_write(map, chip->mask_base + (i * map->reg_stride),
242 d->mask_buf[i]);
Mark Brownf8beab22011-10-28 23:50:49 +0200243 if (ret != 0) {
244 dev_err(map->dev, "Failed to set masks in 0x%x: %d\n",
Stephen Warrenf01ee602012-04-09 13:40:24 -0600245 chip->mask_base + (i * map->reg_stride), ret);
Mark Brownf8beab22011-10-28 23:50:49 +0200246 goto err_alloc;
247 }
248 }
249
250 /* Register them with genirq */
251 for (cur_irq = irq_base;
252 cur_irq < chip->num_irqs + irq_base;
253 cur_irq++) {
254 irq_set_chip_data(cur_irq, d);
255 irq_set_chip_and_handler(cur_irq, &regmap_irq_chip,
256 handle_edge_irq);
257 irq_set_nested_thread(cur_irq, 1);
258
259 /* ARM needs us to explicitly flag the IRQ as valid
260 * and will set them noprobe when we do so. */
261#ifdef CONFIG_ARM
262 set_irq_flags(cur_irq, IRQF_VALID);
263#else
264 irq_set_noprobe(cur_irq);
265#endif
266 }
267
268 ret = request_threaded_irq(irq, NULL, regmap_irq_thread, irq_flags,
269 chip->name, d);
270 if (ret != 0) {
271 dev_err(map->dev, "Failed to request IRQ %d: %d\n", irq, ret);
272 goto err_alloc;
273 }
274
275 return 0;
276
277err_alloc:
278 kfree(d->mask_buf_def);
279 kfree(d->mask_buf);
280 kfree(d->status_reg_buf);
281 kfree(d->status_buf);
282 kfree(d);
283 return ret;
284}
285EXPORT_SYMBOL_GPL(regmap_add_irq_chip);
286
287/**
288 * regmap_del_irq_chip(): Stop interrupt handling for a regmap IRQ chip
289 *
290 * @irq: Primary IRQ for the device
291 * @d: regmap_irq_chip_data allocated by regmap_add_irq_chip()
292 */
293void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *d)
294{
295 if (!d)
296 return;
297
298 free_irq(irq, d);
299 kfree(d->mask_buf_def);
300 kfree(d->mask_buf);
301 kfree(d->status_reg_buf);
302 kfree(d->status_buf);
303 kfree(d);
304}
305EXPORT_SYMBOL_GPL(regmap_del_irq_chip);
Mark Brown209a6002011-12-05 16:10:15 +0000306
307/**
308 * regmap_irq_chip_get_base(): Retrieve interrupt base for a regmap IRQ chip
309 *
310 * Useful for drivers to request their own IRQs.
311 *
312 * @data: regmap_irq controller to operate on.
313 */
314int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data)
315{
316 return data->irq_base;
317}
318EXPORT_SYMBOL_GPL(regmap_irq_chip_get_base);