blob: c7e5b18f4edc9484d65e62fc1c613e6dd1760a7d [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>
Mark Brown4af8be62012-05-13 10:59:56 +010018#include <linux/irqdomain.h>
Mark Brownf8beab22011-10-28 23:50:49 +020019#include <linux/slab.h>
20
21#include "internal.h"
22
23struct regmap_irq_chip_data {
24 struct mutex lock;
25
26 struct regmap *map;
Mark Brownb026ddb2012-05-31 21:01:46 +010027 const struct regmap_irq_chip *chip;
Mark Brownf8beab22011-10-28 23:50:49 +020028
29 int irq_base;
Mark Brown4af8be62012-05-13 10:59:56 +010030 struct irq_domain *domain;
Mark Brownf8beab22011-10-28 23:50:49 +020031
Mark Browna43fd502012-06-05 14:34:03 +010032 int irq;
33 int wake_count;
34
Mark Brownf8beab22011-10-28 23:50:49 +020035 unsigned int *status_buf;
36 unsigned int *mask_buf;
37 unsigned int *mask_buf_def;
Mark Browna43fd502012-06-05 14:34:03 +010038 unsigned int *wake_buf;
Graeme Gregory022f926a2012-05-14 22:40:43 +090039
40 unsigned int irq_reg_stride;
Mark Brownf8beab22011-10-28 23:50:49 +020041};
42
43static inline const
44struct regmap_irq *irq_to_regmap_irq(struct regmap_irq_chip_data *data,
45 int irq)
46{
Mark Brown4af8be62012-05-13 10:59:56 +010047 return &data->chip->irqs[irq];
Mark Brownf8beab22011-10-28 23:50:49 +020048}
49
50static void regmap_irq_lock(struct irq_data *data)
51{
52 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
53
54 mutex_lock(&d->lock);
55}
56
57static void regmap_irq_sync_unlock(struct irq_data *data)
58{
59 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
Stephen Warren56806552012-04-10 23:37:22 -060060 struct regmap *map = d->map;
Mark Brownf8beab22011-10-28 23:50:49 +020061 int i, ret;
Stephen Warren16032622012-07-27 13:01:54 -060062 u32 reg;
Mark Brownf8beab22011-10-28 23:50:49 +020063
64 /*
65 * If there's been a change in the mask write it back to the
66 * hardware. We rely on the use of the regmap core cache to
67 * suppress pointless writes.
68 */
69 for (i = 0; i < d->chip->num_regs; i++) {
Stephen Warren16032622012-07-27 13:01:54 -060070 reg = d->chip->mask_base +
71 (i * map->reg_stride * d->irq_reg_stride);
72 ret = regmap_update_bits(d->map, reg,
Mark Brownf8beab22011-10-28 23:50:49 +020073 d->mask_buf_def[i], d->mask_buf[i]);
74 if (ret != 0)
75 dev_err(d->map->dev, "Failed to sync masks in %x\n",
Stephen Warren16032622012-07-27 13:01:54 -060076 reg);
Mark Brownf8beab22011-10-28 23:50:49 +020077 }
78
Mark Browna43fd502012-06-05 14:34:03 +010079 /* If we've changed our wakeup count propagate it to the parent */
80 if (d->wake_count < 0)
81 for (i = d->wake_count; i < 0; i++)
82 irq_set_irq_wake(d->irq, 0);
83 else if (d->wake_count > 0)
84 for (i = 0; i < d->wake_count; i++)
85 irq_set_irq_wake(d->irq, 1);
86
87 d->wake_count = 0;
88
Mark Brownf8beab22011-10-28 23:50:49 +020089 mutex_unlock(&d->lock);
90}
91
92static void regmap_irq_enable(struct irq_data *data)
93{
94 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
Stephen Warren56806552012-04-10 23:37:22 -060095 struct regmap *map = d->map;
Mark Brown4af8be62012-05-13 10:59:56 +010096 const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->hwirq);
Mark Brownf8beab22011-10-28 23:50:49 +020097
Stephen Warrenf01ee602012-04-09 13:40:24 -060098 d->mask_buf[irq_data->reg_offset / map->reg_stride] &= ~irq_data->mask;
Mark Brownf8beab22011-10-28 23:50:49 +020099}
100
101static void regmap_irq_disable(struct irq_data *data)
102{
103 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
Stephen Warren56806552012-04-10 23:37:22 -0600104 struct regmap *map = d->map;
Mark Brown4af8be62012-05-13 10:59:56 +0100105 const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->hwirq);
Mark Brownf8beab22011-10-28 23:50:49 +0200106
Stephen Warrenf01ee602012-04-09 13:40:24 -0600107 d->mask_buf[irq_data->reg_offset / map->reg_stride] |= irq_data->mask;
Mark Brownf8beab22011-10-28 23:50:49 +0200108}
109
Mark Browna43fd502012-06-05 14:34:03 +0100110static int regmap_irq_set_wake(struct irq_data *data, unsigned int on)
111{
112 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
113 struct regmap *map = d->map;
114 const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->hwirq);
115
116 if (!d->chip->wake_base)
117 return -EINVAL;
118
119 if (on) {
120 d->wake_buf[irq_data->reg_offset / map->reg_stride]
121 &= ~irq_data->mask;
122 d->wake_count++;
123 } else {
124 d->wake_buf[irq_data->reg_offset / map->reg_stride]
125 |= irq_data->mask;
126 d->wake_count--;
127 }
128
129 return 0;
130}
131
Mark Brownf8beab22011-10-28 23:50:49 +0200132static struct irq_chip regmap_irq_chip = {
133 .name = "regmap",
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,
Mark Browna43fd502012-06-05 14:34:03 +0100138 .irq_set_wake = regmap_irq_set_wake,
Mark Brownf8beab22011-10-28 23:50:49 +0200139};
140
141static irqreturn_t regmap_irq_thread(int irq, void *d)
142{
143 struct regmap_irq_chip_data *data = d;
Mark Brownb026ddb2012-05-31 21:01:46 +0100144 const struct regmap_irq_chip *chip = data->chip;
Mark Brownf8beab22011-10-28 23:50:49 +0200145 struct regmap *map = data->map;
146 int ret, i;
Mark Brownd23511f2011-11-28 18:50:39 +0000147 bool handled = false;
Stephen Warren16032622012-07-27 13:01:54 -0600148 u32 reg;
Mark Brownf8beab22011-10-28 23:50:49 +0200149
Mark Brownf8beab22011-10-28 23:50:49 +0200150 /*
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.
156 */
157 for (i = 0; i < data->chip->num_regs; i++) {
Mark Brown38e7f5d2012-05-17 13:59:40 +0100158 ret = regmap_read(map, chip->status_base + (i * map->reg_stride
Graeme Gregory022f926a2012-05-14 22:40:43 +0900159 * data->irq_reg_stride),
160 &data->status_buf[i]);
161
162 if (ret != 0) {
163 dev_err(map->dev, "Failed to read IRQ status: %d\n",
164 ret);
Mark Brownf8beab22011-10-28 23:50:49 +0200165 return IRQ_NONE;
166 }
167
168 data->status_buf[i] &= ~data->mask_buf[i];
169
170 if (data->status_buf[i] && chip->ack_base) {
Stephen Warren16032622012-07-27 13:01:54 -0600171 reg = chip->ack_base +
172 (i * map->reg_stride * data->irq_reg_stride);
173 ret = regmap_write(map, reg, data->status_buf[i]);
Mark Brownf8beab22011-10-28 23:50:49 +0200174 if (ret != 0)
175 dev_err(map->dev, "Failed to ack 0x%x: %d\n",
Stephen Warren16032622012-07-27 13:01:54 -0600176 reg, ret);
Mark Brownf8beab22011-10-28 23:50:49 +0200177 }
178 }
179
180 for (i = 0; i < chip->num_irqs; i++) {
Stephen Warrenf01ee602012-04-09 13:40:24 -0600181 if (data->status_buf[chip->irqs[i].reg_offset /
182 map->reg_stride] & chip->irqs[i].mask) {
Mark Brown4af8be62012-05-13 10:59:56 +0100183 handle_nested_irq(irq_find_mapping(data->domain, i));
Mark Brownd23511f2011-11-28 18:50:39 +0000184 handled = true;
Mark Brownf8beab22011-10-28 23:50:49 +0200185 }
186 }
187
Mark Brownd23511f2011-11-28 18:50:39 +0000188 if (handled)
189 return IRQ_HANDLED;
190 else
191 return IRQ_NONE;
Mark Brownf8beab22011-10-28 23:50:49 +0200192}
193
Mark Brown4af8be62012-05-13 10:59:56 +0100194static int regmap_irq_map(struct irq_domain *h, unsigned int virq,
195 irq_hw_number_t hw)
196{
197 struct regmap_irq_chip_data *data = h->host_data;
198
199 irq_set_chip_data(virq, data);
200 irq_set_chip_and_handler(virq, &regmap_irq_chip, handle_edge_irq);
201 irq_set_nested_thread(virq, 1);
202
203 /* ARM needs us to explicitly flag the IRQ as valid
204 * and will set them noprobe when we do so. */
205#ifdef CONFIG_ARM
206 set_irq_flags(virq, IRQF_VALID);
207#else
208 irq_set_noprobe(virq);
209#endif
210
211 return 0;
212}
213
214static struct irq_domain_ops regmap_domain_ops = {
215 .map = regmap_irq_map,
216 .xlate = irq_domain_xlate_twocell,
217};
218
Mark Brownf8beab22011-10-28 23:50:49 +0200219/**
220 * regmap_add_irq_chip(): Use standard regmap IRQ controller handling
221 *
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
227 *
228 * Returns 0 on success or an errno on failure.
229 *
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.
233 */
234int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
Mark Brownb026ddb2012-05-31 21:01:46 +0100235 int irq_base, const struct regmap_irq_chip *chip,
Mark Brownf8beab22011-10-28 23:50:49 +0200236 struct regmap_irq_chip_data **data)
237{
238 struct regmap_irq_chip_data *d;
Mark Brown4af8be62012-05-13 10:59:56 +0100239 int i;
Mark Brownf8beab22011-10-28 23:50:49 +0200240 int ret = -ENOMEM;
Stephen Warren16032622012-07-27 13:01:54 -0600241 u32 reg;
Mark Brownf8beab22011-10-28 23:50:49 +0200242
Stephen Warrenf01ee602012-04-09 13:40:24 -0600243 for (i = 0; i < chip->num_irqs; i++) {
244 if (chip->irqs[i].reg_offset % map->reg_stride)
245 return -EINVAL;
246 if (chip->irqs[i].reg_offset / map->reg_stride >=
247 chip->num_regs)
248 return -EINVAL;
249 }
250
Mark Brown4af8be62012-05-13 10:59:56 +0100251 if (irq_base) {
252 irq_base = irq_alloc_descs(irq_base, 0, chip->num_irqs, 0);
253 if (irq_base < 0) {
254 dev_warn(map->dev, "Failed to allocate IRQs: %d\n",
255 irq_base);
256 return irq_base;
257 }
Mark Brownf8beab22011-10-28 23:50:49 +0200258 }
259
260 d = kzalloc(sizeof(*d), GFP_KERNEL);
261 if (!d)
262 return -ENOMEM;
263
Mark Brown2431d0a2012-05-13 11:18:34 +0100264 *data = d;
265
Mark Brownf8beab22011-10-28 23:50:49 +0200266 d->status_buf = kzalloc(sizeof(unsigned int) * chip->num_regs,
267 GFP_KERNEL);
268 if (!d->status_buf)
269 goto err_alloc;
270
Mark Brownf8beab22011-10-28 23:50:49 +0200271 d->mask_buf = kzalloc(sizeof(unsigned int) * chip->num_regs,
272 GFP_KERNEL);
273 if (!d->mask_buf)
274 goto err_alloc;
275
276 d->mask_buf_def = kzalloc(sizeof(unsigned int) * chip->num_regs,
277 GFP_KERNEL);
278 if (!d->mask_buf_def)
279 goto err_alloc;
280
Mark Browna43fd502012-06-05 14:34:03 +0100281 if (chip->wake_base) {
282 d->wake_buf = kzalloc(sizeof(unsigned int) * chip->num_regs,
283 GFP_KERNEL);
284 if (!d->wake_buf)
285 goto err_alloc;
286 }
287
288 d->irq = irq;
Mark Brownf8beab22011-10-28 23:50:49 +0200289 d->map = map;
290 d->chip = chip;
291 d->irq_base = irq_base;
Graeme Gregory022f926a2012-05-14 22:40:43 +0900292
293 if (chip->irq_reg_stride)
294 d->irq_reg_stride = chip->irq_reg_stride;
295 else
296 d->irq_reg_stride = 1;
297
Mark Brownf8beab22011-10-28 23:50:49 +0200298 mutex_init(&d->lock);
299
300 for (i = 0; i < chip->num_irqs; i++)
Stephen Warrenf01ee602012-04-09 13:40:24 -0600301 d->mask_buf_def[chip->irqs[i].reg_offset / map->reg_stride]
Mark Brownf8beab22011-10-28 23:50:49 +0200302 |= chip->irqs[i].mask;
303
304 /* Mask all the interrupts by default */
305 for (i = 0; i < chip->num_regs; i++) {
306 d->mask_buf[i] = d->mask_buf_def[i];
Stephen Warren16032622012-07-27 13:01:54 -0600307 reg = chip->mask_base +
308 (i * map->reg_stride * d->irq_reg_stride);
309 ret = regmap_write(map, reg, d->mask_buf[i]);
Mark Brownf8beab22011-10-28 23:50:49 +0200310 if (ret != 0) {
311 dev_err(map->dev, "Failed to set masks in 0x%x: %d\n",
Stephen Warren16032622012-07-27 13:01:54 -0600312 reg, ret);
Mark Brownf8beab22011-10-28 23:50:49 +0200313 goto err_alloc;
314 }
315 }
316
Mark Brown4af8be62012-05-13 10:59:56 +0100317 if (irq_base)
318 d->domain = irq_domain_add_legacy(map->dev->of_node,
319 chip->num_irqs, irq_base, 0,
320 &regmap_domain_ops, d);
321 else
322 d->domain = irq_domain_add_linear(map->dev->of_node,
323 chip->num_irqs,
324 &regmap_domain_ops, d);
325 if (!d->domain) {
326 dev_err(map->dev, "Failed to create IRQ domain\n");
327 ret = -ENOMEM;
328 goto err_alloc;
Mark Brownf8beab22011-10-28 23:50:49 +0200329 }
330
331 ret = request_threaded_irq(irq, NULL, regmap_irq_thread, irq_flags,
332 chip->name, d);
333 if (ret != 0) {
334 dev_err(map->dev, "Failed to request IRQ %d: %d\n", irq, ret);
Mark Brown4af8be62012-05-13 10:59:56 +0100335 goto err_domain;
Mark Brownf8beab22011-10-28 23:50:49 +0200336 }
337
338 return 0;
339
Mark Brown4af8be62012-05-13 10:59:56 +0100340err_domain:
341 /* Should really dispose of the domain but... */
Mark Brownf8beab22011-10-28 23:50:49 +0200342err_alloc:
Mark Browna43fd502012-06-05 14:34:03 +0100343 kfree(d->wake_buf);
Mark Brownf8beab22011-10-28 23:50:49 +0200344 kfree(d->mask_buf_def);
345 kfree(d->mask_buf);
Mark Brownf8beab22011-10-28 23:50:49 +0200346 kfree(d->status_buf);
347 kfree(d);
348 return ret;
349}
350EXPORT_SYMBOL_GPL(regmap_add_irq_chip);
351
352/**
353 * regmap_del_irq_chip(): Stop interrupt handling for a regmap IRQ chip
354 *
355 * @irq: Primary IRQ for the device
356 * @d: regmap_irq_chip_data allocated by regmap_add_irq_chip()
357 */
358void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *d)
359{
360 if (!d)
361 return;
362
363 free_irq(irq, d);
Mark Brown4af8be62012-05-13 10:59:56 +0100364 /* We should unmap the domain but... */
Mark Browna43fd502012-06-05 14:34:03 +0100365 kfree(d->wake_buf);
Mark Brownf8beab22011-10-28 23:50:49 +0200366 kfree(d->mask_buf_def);
367 kfree(d->mask_buf);
Mark Brownf8beab22011-10-28 23:50:49 +0200368 kfree(d->status_buf);
369 kfree(d);
370}
371EXPORT_SYMBOL_GPL(regmap_del_irq_chip);
Mark Brown209a6002011-12-05 16:10:15 +0000372
373/**
374 * regmap_irq_chip_get_base(): Retrieve interrupt base for a regmap IRQ chip
375 *
376 * Useful for drivers to request their own IRQs.
377 *
378 * @data: regmap_irq controller to operate on.
379 */
380int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data)
381{
Mark Brown4af8be62012-05-13 10:59:56 +0100382 WARN_ON(!data->irq_base);
Mark Brown209a6002011-12-05 16:10:15 +0000383 return data->irq_base;
384}
385EXPORT_SYMBOL_GPL(regmap_irq_chip_get_base);
Mark Brown4af8be62012-05-13 10:59:56 +0100386
387/**
388 * regmap_irq_get_virq(): Map an interrupt on a chip to a virtual IRQ
389 *
390 * Useful for drivers to request their own IRQs.
391 *
392 * @data: regmap_irq controller to operate on.
393 * @irq: index of the interrupt requested in the chip IRQs
394 */
395int regmap_irq_get_virq(struct regmap_irq_chip_data *data, int irq)
396{
Mark Brownbfd6185d2012-06-05 14:29:36 +0100397 /* Handle holes in the IRQ list */
398 if (!data->chip->irqs[irq].mask)
399 return -EINVAL;
400
Mark Brown4af8be62012-05-13 10:59:56 +0100401 return irq_create_mapping(data->domain, irq);
402}
403EXPORT_SYMBOL_GPL(regmap_irq_get_virq);