blob: 72a6f31e8223e8874c7114d1c150676185195edc [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;
Stephen Warren7ac140e2012-08-01 11:40:47 -060025 struct irq_chip irq_chip;
Mark Brownf8beab22011-10-28 23:50:49 +020026
27 struct regmap *map;
Mark Brownb026ddb2012-05-31 21:01:46 +010028 const struct regmap_irq_chip *chip;
Mark Brownf8beab22011-10-28 23:50:49 +020029
30 int irq_base;
Mark Brown4af8be62012-05-13 10:59:56 +010031 struct irq_domain *domain;
Mark Brownf8beab22011-10-28 23:50:49 +020032
Mark Browna43fd502012-06-05 14:34:03 +010033 int irq;
34 int wake_count;
35
Mark Brownf8beab22011-10-28 23:50:49 +020036 unsigned int *status_buf;
37 unsigned int *mask_buf;
38 unsigned int *mask_buf_def;
Mark Browna43fd502012-06-05 14:34:03 +010039 unsigned int *wake_buf;
Graeme Gregory022f926a2012-05-14 22:40:43 +090040
41 unsigned int irq_reg_stride;
Mark Brownf8beab22011-10-28 23:50:49 +020042};
43
44static inline const
45struct regmap_irq *irq_to_regmap_irq(struct regmap_irq_chip_data *data,
46 int irq)
47{
Mark Brown4af8be62012-05-13 10:59:56 +010048 return &data->chip->irqs[irq];
Mark Brownf8beab22011-10-28 23:50:49 +020049}
50
51static void regmap_irq_lock(struct irq_data *data)
52{
53 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
54
55 mutex_lock(&d->lock);
56}
57
58static void regmap_irq_sync_unlock(struct irq_data *data)
59{
60 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
Stephen Warren56806552012-04-10 23:37:22 -060061 struct regmap *map = d->map;
Mark Brownf8beab22011-10-28 23:50:49 +020062 int i, ret;
Stephen Warren16032622012-07-27 13:01:54 -060063 u32 reg;
Mark Brownf8beab22011-10-28 23:50:49 +020064
65 /*
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.
69 */
70 for (i = 0; i < d->chip->num_regs; i++) {
Stephen Warren16032622012-07-27 13:01:54 -060071 reg = d->chip->mask_base +
72 (i * map->reg_stride * d->irq_reg_stride);
73 ret = regmap_update_bits(d->map, reg,
Mark Brownf8beab22011-10-28 23:50:49 +020074 d->mask_buf_def[i], d->mask_buf[i]);
75 if (ret != 0)
76 dev_err(d->map->dev, "Failed to sync masks in %x\n",
Stephen Warren16032622012-07-27 13:01:54 -060077 reg);
Mark Brownf8beab22011-10-28 23:50:49 +020078 }
79
Mark Browna43fd502012-06-05 14:34:03 +010080 /* 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);
87
88 d->wake_count = 0;
89
Mark Brownf8beab22011-10-28 23:50:49 +020090 mutex_unlock(&d->lock);
91}
92
93static void regmap_irq_enable(struct irq_data *data)
94{
95 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
Stephen Warren56806552012-04-10 23:37:22 -060096 struct regmap *map = d->map;
Mark Brown4af8be62012-05-13 10:59:56 +010097 const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->hwirq);
Mark Brownf8beab22011-10-28 23:50:49 +020098
Stephen Warrenf01ee602012-04-09 13:40:24 -060099 d->mask_buf[irq_data->reg_offset / map->reg_stride] &= ~irq_data->mask;
Mark Brownf8beab22011-10-28 23:50:49 +0200100}
101
102static void regmap_irq_disable(struct irq_data *data)
103{
104 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
Stephen Warren56806552012-04-10 23:37:22 -0600105 struct regmap *map = d->map;
Mark Brown4af8be62012-05-13 10:59:56 +0100106 const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->hwirq);
Mark Brownf8beab22011-10-28 23:50:49 +0200107
Stephen Warrenf01ee602012-04-09 13:40:24 -0600108 d->mask_buf[irq_data->reg_offset / map->reg_stride] |= irq_data->mask;
Mark Brownf8beab22011-10-28 23:50:49 +0200109}
110
Mark Browna43fd502012-06-05 14:34:03 +0100111static int regmap_irq_set_wake(struct irq_data *data, unsigned int on)
112{
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);
116
117 if (!d->chip->wake_base)
118 return -EINVAL;
119
120 if (on) {
121 d->wake_buf[irq_data->reg_offset / map->reg_stride]
122 &= ~irq_data->mask;
123 d->wake_count++;
124 } else {
125 d->wake_buf[irq_data->reg_offset / map->reg_stride]
126 |= irq_data->mask;
127 d->wake_count--;
128 }
129
130 return 0;
131}
132
Stephen Warren7ac140e2012-08-01 11:40:47 -0600133static const struct irq_chip regmap_irq_chip = {
Mark Brownf8beab22011-10-28 23:50:49 +0200134 .name = "regmap",
135 .irq_bus_lock = regmap_irq_lock,
136 .irq_bus_sync_unlock = regmap_irq_sync_unlock,
137 .irq_disable = regmap_irq_disable,
138 .irq_enable = regmap_irq_enable,
Mark Browna43fd502012-06-05 14:34:03 +0100139 .irq_set_wake = regmap_irq_set_wake,
Mark Brownf8beab22011-10-28 23:50:49 +0200140};
141
142static irqreturn_t regmap_irq_thread(int irq, void *d)
143{
144 struct regmap_irq_chip_data *data = d;
Mark Brownb026ddb2012-05-31 21:01:46 +0100145 const struct regmap_irq_chip *chip = data->chip;
Mark Brownf8beab22011-10-28 23:50:49 +0200146 struct regmap *map = data->map;
147 int ret, i;
Mark Brownd23511f2011-11-28 18:50:39 +0000148 bool handled = false;
Stephen Warren16032622012-07-27 13:01:54 -0600149 u32 reg;
Mark Brownf8beab22011-10-28 23:50:49 +0200150
Mark Brownf8beab22011-10-28 23:50:49 +0200151 /*
152 * Ignore masked IRQs and ack if we need to; we ack early so
153 * there is no race between handling and acknowleding the
154 * interrupt. We assume that typically few of the interrupts
155 * will fire simultaneously so don't worry about overhead from
156 * doing a write per register.
157 */
158 for (i = 0; i < data->chip->num_regs; i++) {
Mark Brown38e7f5d2012-05-17 13:59:40 +0100159 ret = regmap_read(map, chip->status_base + (i * map->reg_stride
Graeme Gregory022f926a2012-05-14 22:40:43 +0900160 * data->irq_reg_stride),
161 &data->status_buf[i]);
162
163 if (ret != 0) {
164 dev_err(map->dev, "Failed to read IRQ status: %d\n",
165 ret);
Mark Brownf8beab22011-10-28 23:50:49 +0200166 return IRQ_NONE;
167 }
168
169 data->status_buf[i] &= ~data->mask_buf[i];
170
171 if (data->status_buf[i] && chip->ack_base) {
Stephen Warren16032622012-07-27 13:01:54 -0600172 reg = chip->ack_base +
173 (i * map->reg_stride * data->irq_reg_stride);
174 ret = regmap_write(map, reg, data->status_buf[i]);
Mark Brownf8beab22011-10-28 23:50:49 +0200175 if (ret != 0)
176 dev_err(map->dev, "Failed to ack 0x%x: %d\n",
Stephen Warren16032622012-07-27 13:01:54 -0600177 reg, ret);
Mark Brownf8beab22011-10-28 23:50:49 +0200178 }
179 }
180
181 for (i = 0; i < chip->num_irqs; i++) {
Stephen Warrenf01ee602012-04-09 13:40:24 -0600182 if (data->status_buf[chip->irqs[i].reg_offset /
183 map->reg_stride] & chip->irqs[i].mask) {
Mark Brown4af8be62012-05-13 10:59:56 +0100184 handle_nested_irq(irq_find_mapping(data->domain, i));
Mark Brownd23511f2011-11-28 18:50:39 +0000185 handled = true;
Mark Brownf8beab22011-10-28 23:50:49 +0200186 }
187 }
188
Mark Brownd23511f2011-11-28 18:50:39 +0000189 if (handled)
190 return IRQ_HANDLED;
191 else
192 return IRQ_NONE;
Mark Brownf8beab22011-10-28 23:50:49 +0200193}
194
Mark Brown4af8be62012-05-13 10:59:56 +0100195static int regmap_irq_map(struct irq_domain *h, unsigned int virq,
196 irq_hw_number_t hw)
197{
198 struct regmap_irq_chip_data *data = h->host_data;
199
200 irq_set_chip_data(virq, data);
Stephen Warren7ac140e2012-08-01 11:40:47 -0600201 irq_set_chip_and_handler(virq, &data->irq_chip, handle_edge_irq);
Mark Brown4af8be62012-05-13 10:59:56 +0100202 irq_set_nested_thread(virq, 1);
203
204 /* ARM needs us to explicitly flag the IRQ as valid
205 * and will set them noprobe when we do so. */
206#ifdef CONFIG_ARM
207 set_irq_flags(virq, IRQF_VALID);
208#else
209 irq_set_noprobe(virq);
210#endif
211
212 return 0;
213}
214
215static struct irq_domain_ops regmap_domain_ops = {
216 .map = regmap_irq_map,
217 .xlate = irq_domain_xlate_twocell,
218};
219
Mark Brownf8beab22011-10-28 23:50:49 +0200220/**
221 * regmap_add_irq_chip(): Use standard regmap IRQ controller handling
222 *
223 * map: The regmap for the device.
224 * irq: The IRQ the device uses to signal interrupts
225 * irq_flags: The IRQF_ flags to use for the primary interrupt.
226 * chip: Configuration for the interrupt controller.
227 * data: Runtime data structure for the controller, allocated on success
228 *
229 * Returns 0 on success or an errno on failure.
230 *
231 * In order for this to be efficient the chip really should use a
232 * register cache. The chip driver is responsible for restoring the
233 * register values used by the IRQ controller over suspend and resume.
234 */
235int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
Mark Brownb026ddb2012-05-31 21:01:46 +0100236 int irq_base, const struct regmap_irq_chip *chip,
Mark Brownf8beab22011-10-28 23:50:49 +0200237 struct regmap_irq_chip_data **data)
238{
239 struct regmap_irq_chip_data *d;
Mark Brown4af8be62012-05-13 10:59:56 +0100240 int i;
Mark Brownf8beab22011-10-28 23:50:49 +0200241 int ret = -ENOMEM;
Stephen Warren16032622012-07-27 13:01:54 -0600242 u32 reg;
Mark Brownf8beab22011-10-28 23:50:49 +0200243
Stephen Warrenf01ee602012-04-09 13:40:24 -0600244 for (i = 0; i < chip->num_irqs; i++) {
245 if (chip->irqs[i].reg_offset % map->reg_stride)
246 return -EINVAL;
247 if (chip->irqs[i].reg_offset / map->reg_stride >=
248 chip->num_regs)
249 return -EINVAL;
250 }
251
Mark Brown4af8be62012-05-13 10:59:56 +0100252 if (irq_base) {
253 irq_base = irq_alloc_descs(irq_base, 0, chip->num_irqs, 0);
254 if (irq_base < 0) {
255 dev_warn(map->dev, "Failed to allocate IRQs: %d\n",
256 irq_base);
257 return irq_base;
258 }
Mark Brownf8beab22011-10-28 23:50:49 +0200259 }
260
261 d = kzalloc(sizeof(*d), GFP_KERNEL);
262 if (!d)
263 return -ENOMEM;
264
Mark Brown2431d0a2012-05-13 11:18:34 +0100265 *data = d;
266
Mark Brownf8beab22011-10-28 23:50:49 +0200267 d->status_buf = kzalloc(sizeof(unsigned int) * chip->num_regs,
268 GFP_KERNEL);
269 if (!d->status_buf)
270 goto err_alloc;
271
Mark Brownf8beab22011-10-28 23:50:49 +0200272 d->mask_buf = kzalloc(sizeof(unsigned int) * chip->num_regs,
273 GFP_KERNEL);
274 if (!d->mask_buf)
275 goto err_alloc;
276
277 d->mask_buf_def = kzalloc(sizeof(unsigned int) * chip->num_regs,
278 GFP_KERNEL);
279 if (!d->mask_buf_def)
280 goto err_alloc;
281
Mark Browna43fd502012-06-05 14:34:03 +0100282 if (chip->wake_base) {
283 d->wake_buf = kzalloc(sizeof(unsigned int) * chip->num_regs,
284 GFP_KERNEL);
285 if (!d->wake_buf)
286 goto err_alloc;
287 }
288
Stephen Warren7ac140e2012-08-01 11:40:47 -0600289 d->irq_chip = regmap_irq_chip;
Mark Browna43fd502012-06-05 14:34:03 +0100290 d->irq = irq;
Mark Brownf8beab22011-10-28 23:50:49 +0200291 d->map = map;
292 d->chip = chip;
293 d->irq_base = irq_base;
Graeme Gregory022f926a2012-05-14 22:40:43 +0900294
295 if (chip->irq_reg_stride)
296 d->irq_reg_stride = chip->irq_reg_stride;
297 else
298 d->irq_reg_stride = 1;
299
Mark Brownf8beab22011-10-28 23:50:49 +0200300 mutex_init(&d->lock);
301
302 for (i = 0; i < chip->num_irqs; i++)
Stephen Warrenf01ee602012-04-09 13:40:24 -0600303 d->mask_buf_def[chip->irqs[i].reg_offset / map->reg_stride]
Mark Brownf8beab22011-10-28 23:50:49 +0200304 |= chip->irqs[i].mask;
305
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];
Stephen Warren16032622012-07-27 13:01:54 -0600309 reg = chip->mask_base +
310 (i * map->reg_stride * d->irq_reg_stride);
Mark Brown0eb46ad2012-08-01 20:29:14 +0100311 ret = regmap_update_bits(map, reg,
312 d->mask_buf[i], d->mask_buf[i]);
Mark Brownf8beab22011-10-28 23:50:49 +0200313 if (ret != 0) {
314 dev_err(map->dev, "Failed to set masks in 0x%x: %d\n",
Stephen Warren16032622012-07-27 13:01:54 -0600315 reg, ret);
Mark Brownf8beab22011-10-28 23:50:49 +0200316 goto err_alloc;
317 }
318 }
319
Mark Brown4af8be62012-05-13 10:59:56 +0100320 if (irq_base)
321 d->domain = irq_domain_add_legacy(map->dev->of_node,
322 chip->num_irqs, irq_base, 0,
323 &regmap_domain_ops, d);
324 else
325 d->domain = irq_domain_add_linear(map->dev->of_node,
326 chip->num_irqs,
327 &regmap_domain_ops, d);
328 if (!d->domain) {
329 dev_err(map->dev, "Failed to create IRQ domain\n");
330 ret = -ENOMEM;
331 goto err_alloc;
Mark Brownf8beab22011-10-28 23:50:49 +0200332 }
333
334 ret = request_threaded_irq(irq, NULL, regmap_irq_thread, irq_flags,
335 chip->name, d);
336 if (ret != 0) {
337 dev_err(map->dev, "Failed to request IRQ %d: %d\n", irq, ret);
Mark Brown4af8be62012-05-13 10:59:56 +0100338 goto err_domain;
Mark Brownf8beab22011-10-28 23:50:49 +0200339 }
340
341 return 0;
342
Mark Brown4af8be62012-05-13 10:59:56 +0100343err_domain:
344 /* Should really dispose of the domain but... */
Mark Brownf8beab22011-10-28 23:50:49 +0200345err_alloc:
Mark Browna43fd502012-06-05 14:34:03 +0100346 kfree(d->wake_buf);
Mark Brownf8beab22011-10-28 23:50:49 +0200347 kfree(d->mask_buf_def);
348 kfree(d->mask_buf);
Mark Brownf8beab22011-10-28 23:50:49 +0200349 kfree(d->status_buf);
350 kfree(d);
351 return ret;
352}
353EXPORT_SYMBOL_GPL(regmap_add_irq_chip);
354
355/**
356 * regmap_del_irq_chip(): Stop interrupt handling for a regmap IRQ chip
357 *
358 * @irq: Primary IRQ for the device
359 * @d: regmap_irq_chip_data allocated by regmap_add_irq_chip()
360 */
361void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *d)
362{
363 if (!d)
364 return;
365
366 free_irq(irq, d);
Mark Brown4af8be62012-05-13 10:59:56 +0100367 /* We should unmap the domain but... */
Mark Browna43fd502012-06-05 14:34:03 +0100368 kfree(d->wake_buf);
Mark Brownf8beab22011-10-28 23:50:49 +0200369 kfree(d->mask_buf_def);
370 kfree(d->mask_buf);
Mark Brownf8beab22011-10-28 23:50:49 +0200371 kfree(d->status_buf);
372 kfree(d);
373}
374EXPORT_SYMBOL_GPL(regmap_del_irq_chip);
Mark Brown209a6002011-12-05 16:10:15 +0000375
376/**
377 * regmap_irq_chip_get_base(): Retrieve interrupt base for a regmap IRQ chip
378 *
379 * Useful for drivers to request their own IRQs.
380 *
381 * @data: regmap_irq controller to operate on.
382 */
383int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data)
384{
Mark Brown4af8be62012-05-13 10:59:56 +0100385 WARN_ON(!data->irq_base);
Mark Brown209a6002011-12-05 16:10:15 +0000386 return data->irq_base;
387}
388EXPORT_SYMBOL_GPL(regmap_irq_chip_get_base);
Mark Brown4af8be62012-05-13 10:59:56 +0100389
390/**
391 * regmap_irq_get_virq(): Map an interrupt on a chip to a virtual IRQ
392 *
393 * Useful for drivers to request their own IRQs.
394 *
395 * @data: regmap_irq controller to operate on.
396 * @irq: index of the interrupt requested in the chip IRQs
397 */
398int regmap_irq_get_virq(struct regmap_irq_chip_data *data, int irq)
399{
Mark Brownbfd6185d2012-06-05 14:29:36 +0100400 /* Handle holes in the IRQ list */
401 if (!data->chip->irqs[irq].mask)
402 return -EINVAL;
403
Mark Brown4af8be62012-05-13 10:59:56 +0100404 return irq_create_mapping(data->domain, irq);
405}
406EXPORT_SYMBOL_GPL(regmap_irq_get_virq);