blob: 082c9ffedab9238559ccadfa42a0c4edb31a31dc [file] [log] [blame]
Michael Bohan0435bd62013-02-26 15:29:22 -08001/* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
Michael Bohan115cf652012-01-05 14:32:59 -08002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#define pr_fmt(fmt) "%s: " fmt, __func__
14
15#include <linux/init.h>
16#include <linux/kernel.h>
17#include <linux/err.h>
18#include <linux/module.h>
19#include <linux/list.h>
20#include <linux/of.h>
21#include <linux/of_address.h>
22#include <linux/of_irq.h>
23#include <linux/irqdomain.h>
24#include <linux/interrupt.h>
25#include <linux/spmi.h>
26#include <linux/radix-tree.h>
27#include <linux/slab.h>
28#include <linux/printk.h>
Michael Bohan0435bd62013-02-26 15:29:22 -080029#include <linux/ratelimit.h>
Michael Bohan115cf652012-01-05 14:32:59 -080030
31#include <asm/irq.h>
32#include <asm/mach/irq.h>
33#include <mach/qpnp-int.h>
34
Michael Bohan115cf652012-01-05 14:32:59 -080035/* 16 slave_ids, 256 per_ids per slave, and 8 ints per per_id */
Michael Bohan0435bd62013-02-26 15:29:22 -080036#define QPNPINT_NR_IRQS (16 * 256 * 8)
37/* This value is guaranteed not to be valid for private data */
38#define QPNPINT_INVALID_DATA 0x80000000
Michael Bohan115cf652012-01-05 14:32:59 -080039
40enum qpnpint_regs {
41 QPNPINT_REG_RT_STS = 0x10,
42 QPNPINT_REG_SET_TYPE = 0x11,
43 QPNPINT_REG_POLARITY_HIGH = 0x12,
44 QPNPINT_REG_POLARITY_LOW = 0x13,
45 QPNPINT_REG_LATCHED_CLR = 0x14,
46 QPNPINT_REG_EN_SET = 0x15,
47 QPNPINT_REG_EN_CLR = 0x16,
48 QPNPINT_REG_LATCHED_STS = 0x18,
49};
50
51struct q_perip_data {
52 uint8_t type; /* bitmap */
53 uint8_t pol_high; /* bitmap */
54 uint8_t pol_low; /* bitmap */
55 uint8_t int_en; /* bitmap */
56 uint8_t use_count;
57};
58
59struct q_irq_data {
60 uint32_t priv_d; /* data to optimize arbiter interactions */
61 struct q_chip_data *chip_d;
62 struct q_perip_data *per_d;
63 uint8_t mask_shift;
64 uint8_t spmi_slave;
65 uint16_t spmi_offset;
66};
67
68struct q_chip_data {
69 int bus_nr;
Michael Bohanbb6b30f2012-06-01 13:33:51 -070070 struct irq_domain *domain;
Michael Bohan0435bd62013-02-26 15:29:22 -080071 struct qpnp_local_int *cb;
Michael Bohan115cf652012-01-05 14:32:59 -080072 struct spmi_controller *spmi_ctrl;
73 struct radix_tree_root per_tree;
Michael Bohanbb6b30f2012-06-01 13:33:51 -070074 struct list_head list;
Michael Bohan115cf652012-01-05 14:32:59 -080075};
76
Michael Bohanbb6b30f2012-06-01 13:33:51 -070077static LIST_HEAD(qpnpint_chips);
78static DEFINE_MUTEX(qpnpint_chips_mutex);
79
80#define QPNPINT_MAX_BUSSES 4
81struct q_chip_data *chip_lookup[QPNPINT_MAX_BUSSES];
Michael Bohan115cf652012-01-05 14:32:59 -080082
83/**
84 * qpnpint_encode_hwirq - translate between qpnp_irq_spec and
85 * hwirq representation.
86 *
87 * slave_offset = (addr->slave * 256 * 8);
88 * perip_offset = slave_offset + (addr->perip * 8);
89 * return perip_offset + addr->irq;
90 */
91static inline int qpnpint_encode_hwirq(struct qpnp_irq_spec *spec)
92{
93 uint32_t hwirq;
94
95 if (spec->slave > 15 || spec->irq > 7)
96 return -EINVAL;
97
98 hwirq = (spec->slave << 11);
99 hwirq |= (spec->per << 3);
100 hwirq |= spec->irq;
101
102 return hwirq;
103}
104/**
105 * qpnpint_decode_hwirq - translate between hwirq and
106 * qpnp_irq_spec representation.
107 */
108static inline int qpnpint_decode_hwirq(unsigned long hwirq,
109 struct qpnp_irq_spec *spec)
110{
111 if (hwirq > 65535)
112 return -EINVAL;
113
114 spec->slave = (hwirq >> 11) & 0xF;
115 spec->per = (hwirq >> 3) & 0xFF;
116 spec->irq = hwirq & 0x7;
117 return 0;
118}
119
Jack Pham68576912013-02-28 14:21:14 -0800120static int qpnpint_spmi_read(struct q_irq_data *irq_d, uint8_t reg,
121 void *buf, uint32_t len)
122{
123 struct q_chip_data *chip_d = irq_d->chip_d;
124
125 if (!chip_d->spmi_ctrl)
126 return -ENODEV;
127
128 return spmi_ext_register_readl(chip_d->spmi_ctrl, irq_d->spmi_slave,
129 irq_d->spmi_offset + reg, buf, len);
130}
131
Michael Bohan115cf652012-01-05 14:32:59 -0800132static int qpnpint_spmi_write(struct q_irq_data *irq_d, uint8_t reg,
133 void *buf, uint32_t len)
134{
135 struct q_chip_data *chip_d = irq_d->chip_d;
136 int rc;
137
138 if (!chip_d->spmi_ctrl)
139 return -ENODEV;
140
141 rc = spmi_ext_register_writel(chip_d->spmi_ctrl, irq_d->spmi_slave,
142 irq_d->spmi_offset + reg, buf, len);
143 return rc;
144}
145
Michael Bohan0435bd62013-02-26 15:29:22 -0800146static int qpnpint_arbiter_op(struct irq_data *d,
147 struct q_irq_data *irq_d,
148 int (*arb_op)(struct spmi_controller *,
149 struct qpnp_irq_spec *,
150 uint32_t))
151
152{
153 struct q_chip_data *chip_d = irq_d->chip_d;
154 struct qpnp_irq_spec q_spec;
155 int rc;
156
157 if (!arb_op)
158 return 0;
159
160 if (!chip_d->cb->register_priv_data) {
161 pr_warn_ratelimited("No ability to register arbiter registration data\n");
162 return -ENODEV;
163 }
164
165 rc = qpnpint_decode_hwirq(d->hwirq, &q_spec);
166 if (rc) {
167 pr_err_ratelimited("%s: decode failed on hwirq %lu\n",
168 __func__, d->hwirq);
169 return rc;
170 } else {
171 if (irq_d->priv_d == QPNPINT_INVALID_DATA) {
172 rc = chip_d->cb->register_priv_data(chip_d->spmi_ctrl,
173 &q_spec, &irq_d->priv_d);
174 if (rc) {
175 pr_err_ratelimited(
176 "%s: decode failed on hwirq %lu\n",
177 __func__, d->hwirq);
178 return rc;
179 }
180
181 }
182 arb_op(chip_d->spmi_ctrl, &q_spec, irq_d->priv_d);
183 }
184
185 return 0;
186}
187
Michael Bohan115cf652012-01-05 14:32:59 -0800188static void qpnpint_irq_mask(struct irq_data *d)
189{
190 struct q_irq_data *irq_d = irq_data_get_irq_chip_data(d);
191 struct q_chip_data *chip_d = irq_d->chip_d;
192 struct q_perip_data *per_d = irq_d->per_d;
Michael Bohan115cf652012-01-05 14:32:59 -0800193 int rc;
194
195 pr_debug("hwirq %lu irq: %d\n", d->hwirq, d->irq);
196
Michael Bohan0435bd62013-02-26 15:29:22 -0800197 if (!chip_d->cb) {
198 pr_warn_ratelimited("No arbiter on bus=%u slave=%u offset=%u\n",
199 chip_d->bus_nr, irq_d->spmi_slave,
200 irq_d->spmi_offset);
201 return;
Michael Bohan115cf652012-01-05 14:32:59 -0800202 }
203
Michael Bohan0435bd62013-02-26 15:29:22 -0800204 qpnpint_arbiter_op(d, irq_d, chip_d->cb->mask);
205
Michael Bohan115cf652012-01-05 14:32:59 -0800206 per_d->int_en &= ~irq_d->mask_shift;
207
208 rc = qpnpint_spmi_write(irq_d, QPNPINT_REG_EN_CLR,
209 (u8 *)&irq_d->mask_shift, 1);
Michael Bohan0435bd62013-02-26 15:29:22 -0800210 if (rc) {
211 pr_err_ratelimited("spmi failure on irq %d\n", d->irq);
212 return;
213 }
214
215 pr_debug("done hwirq %lu irq: %d\n", d->hwirq, d->irq);
Michael Bohan115cf652012-01-05 14:32:59 -0800216}
217
218static void qpnpint_irq_mask_ack(struct irq_data *d)
219{
220 struct q_irq_data *irq_d = irq_data_get_irq_chip_data(d);
221 struct q_chip_data *chip_d = irq_d->chip_d;
222 struct q_perip_data *per_d = irq_d->per_d;
Michael Bohan115cf652012-01-05 14:32:59 -0800223 int rc;
224
Michael Bohan0435bd62013-02-26 15:29:22 -0800225 pr_debug("hwirq %lu irq: %d\n", d->hwirq, d->irq);
Michael Bohan115cf652012-01-05 14:32:59 -0800226
Michael Bohan0435bd62013-02-26 15:29:22 -0800227 if (!chip_d->cb) {
228 pr_warn_ratelimited("No arbiter on bus=%u slave=%u offset=%u\n",
229 chip_d->bus_nr, irq_d->spmi_slave,
230 irq_d->spmi_offset);
231 return;
Michael Bohan115cf652012-01-05 14:32:59 -0800232 }
233
Michael Bohan0435bd62013-02-26 15:29:22 -0800234 qpnpint_arbiter_op(d, irq_d, chip_d->cb->mask);
235
Michael Bohan115cf652012-01-05 14:32:59 -0800236 per_d->int_en &= ~irq_d->mask_shift;
237
238 rc = qpnpint_spmi_write(irq_d, QPNPINT_REG_EN_CLR,
239 &irq_d->mask_shift, 1);
Michael Bohan0435bd62013-02-26 15:29:22 -0800240 if (rc) {
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700241 pr_err("spmi failure on irq %d\n", d->irq);
Michael Bohan0435bd62013-02-26 15:29:22 -0800242 return;
243 }
Michael Bohan115cf652012-01-05 14:32:59 -0800244
245 rc = qpnpint_spmi_write(irq_d, QPNPINT_REG_LATCHED_CLR,
246 &irq_d->mask_shift, 1);
Michael Bohan0435bd62013-02-26 15:29:22 -0800247 if (rc) {
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700248 pr_err("spmi failure on irq %d\n", d->irq);
Michael Bohan0435bd62013-02-26 15:29:22 -0800249 return;
250 }
Michael Bohan115cf652012-01-05 14:32:59 -0800251}
252
253static void qpnpint_irq_unmask(struct irq_data *d)
254{
255 struct q_irq_data *irq_d = irq_data_get_irq_chip_data(d);
256 struct q_chip_data *chip_d = irq_d->chip_d;
257 struct q_perip_data *per_d = irq_d->per_d;
Michael Bohan115cf652012-01-05 14:32:59 -0800258 int rc;
259
260 pr_debug("hwirq %lu irq: %d\n", d->hwirq, d->irq);
261
Michael Bohan0435bd62013-02-26 15:29:22 -0800262 if (!chip_d->cb) {
263 pr_warn_ratelimited("No arbiter on bus=%u slave=%u offset=%u\n",
264 chip_d->bus_nr, irq_d->spmi_slave,
265 irq_d->spmi_offset);
266 return;
Michael Bohan115cf652012-01-05 14:32:59 -0800267 }
268
Michael Bohan0435bd62013-02-26 15:29:22 -0800269 qpnpint_arbiter_op(d, irq_d, chip_d->cb->unmask);
270
Michael Bohan115cf652012-01-05 14:32:59 -0800271 per_d->int_en |= irq_d->mask_shift;
272 rc = qpnpint_spmi_write(irq_d, QPNPINT_REG_EN_SET,
273 &irq_d->mask_shift, 1);
Michael Bohan0435bd62013-02-26 15:29:22 -0800274 if (rc) {
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700275 pr_err("spmi failure on irq %d\n", d->irq);
Michael Bohan0435bd62013-02-26 15:29:22 -0800276 return;
277 }
Michael Bohan115cf652012-01-05 14:32:59 -0800278}
279
280static int qpnpint_irq_set_type(struct irq_data *d, unsigned int flow_type)
281{
282 struct q_irq_data *irq_d = irq_data_get_irq_chip_data(d);
283 struct q_perip_data *per_d = irq_d->per_d;
284 int rc;
285 u8 buf[3];
286
287 pr_debug("hwirq %lu irq: %d flow: 0x%x\n", d->hwirq,
288 d->irq, flow_type);
289
290 per_d->pol_high &= ~irq_d->mask_shift;
291 per_d->pol_low &= ~irq_d->mask_shift;
292 if (flow_type & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)) {
293 per_d->type |= irq_d->mask_shift; /* edge trig */
294 if (flow_type & IRQF_TRIGGER_RISING)
295 per_d->pol_high |= irq_d->mask_shift;
296 if (flow_type & IRQF_TRIGGER_FALLING)
297 per_d->pol_low |= irq_d->mask_shift;
298 } else {
299 if ((flow_type & IRQF_TRIGGER_HIGH) &&
300 (flow_type & IRQF_TRIGGER_LOW))
301 return -EINVAL;
302 per_d->type &= ~irq_d->mask_shift; /* level trig */
303 if (flow_type & IRQF_TRIGGER_HIGH)
304 per_d->pol_high |= irq_d->mask_shift;
305 else
Michael Bohan69701d32012-06-07 17:05:41 -0700306 per_d->pol_low |= irq_d->mask_shift;
Michael Bohan115cf652012-01-05 14:32:59 -0800307 }
308
309 buf[0] = per_d->type;
310 buf[1] = per_d->pol_high;
311 buf[2] = per_d->pol_low;
312
313 rc = qpnpint_spmi_write(irq_d, QPNPINT_REG_SET_TYPE, &buf, 3);
Michael Bohan0435bd62013-02-26 15:29:22 -0800314 if (rc) {
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700315 pr_err("spmi failure on irq %d\n", d->irq);
Michael Bohan0435bd62013-02-26 15:29:22 -0800316 return rc;
317 }
318
319 return 0;
Michael Bohan115cf652012-01-05 14:32:59 -0800320}
321
Jack Pham68576912013-02-28 14:21:14 -0800322static int qpnpint_irq_read_line(struct irq_data *d)
323{
324 struct q_irq_data *irq_d = irq_data_get_irq_chip_data(d);
325 int rc;
326 u8 buf;
327
328 pr_debug("hwirq %lu irq: %d\n", d->hwirq, d->irq);
329
330 rc = qpnpint_spmi_read(irq_d, QPNPINT_REG_RT_STS, &buf, 1);
331 if (rc) {
332 pr_err("spmi failure on irq %d\n", d->irq);
333 return rc;
334 }
335
336 return (buf & irq_d->mask_shift) ? 1 : 0;
337}
338
Michael Bohanc86e2b72012-05-29 16:57:52 -0700339static int qpnpint_irq_set_wake(struct irq_data *d, unsigned int on)
340{
341 return 0;
342}
343
Michael Bohan115cf652012-01-05 14:32:59 -0800344static struct irq_chip qpnpint_chip = {
345 .name = "qpnp-int",
346 .irq_mask = qpnpint_irq_mask,
347 .irq_mask_ack = qpnpint_irq_mask_ack,
348 .irq_unmask = qpnpint_irq_unmask,
349 .irq_set_type = qpnpint_irq_set_type,
Jack Pham68576912013-02-28 14:21:14 -0800350 .irq_read_line = qpnpint_irq_read_line,
Michael Bohanc86e2b72012-05-29 16:57:52 -0700351 .irq_set_wake = qpnpint_irq_set_wake,
352 .flags = IRQCHIP_MASK_ON_SUSPEND,
Michael Bohan115cf652012-01-05 14:32:59 -0800353};
354
355static int qpnpint_init_irq_data(struct q_chip_data *chip_d,
356 struct q_irq_data *irq_d,
357 unsigned long hwirq)
358{
359 struct qpnp_irq_spec q_spec;
360 int rc;
361
362 irq_d->mask_shift = 1 << (hwirq & 0x7);
363 rc = qpnpint_decode_hwirq(hwirq, &q_spec);
364 if (rc < 0)
365 return rc;
366 irq_d->spmi_slave = q_spec.slave;
367 irq_d->spmi_offset = q_spec.per << 8;
Michael Bohan115cf652012-01-05 14:32:59 -0800368 irq_d->chip_d = chip_d;
369
Michael Bohan0435bd62013-02-26 15:29:22 -0800370 irq_d->priv_d = QPNPINT_INVALID_DATA;
371
372 if (chip_d->cb && chip_d->cb->register_priv_data) {
373 rc = chip_d->cb->register_priv_data(chip_d->spmi_ctrl, &q_spec,
Michael Bohan115cf652012-01-05 14:32:59 -0800374 &irq_d->priv_d);
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700375 if (rc)
376 return rc;
Michael Bohan0435bd62013-02-26 15:29:22 -0800377 }
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700378
379 irq_d->per_d->use_count++;
380 return 0;
Michael Bohan115cf652012-01-05 14:32:59 -0800381}
382
383static struct q_irq_data *qpnpint_alloc_irq_data(
384 struct q_chip_data *chip_d,
385 unsigned long hwirq)
386{
387 struct q_irq_data *irq_d;
388 struct q_perip_data *per_d;
Michael Bohan392006f2013-01-25 14:29:41 -0800389 int rc;
Michael Bohan115cf652012-01-05 14:32:59 -0800390
391 irq_d = kzalloc(sizeof(struct q_irq_data), GFP_KERNEL);
392 if (!irq_d)
393 return ERR_PTR(-ENOMEM);
394
395 /**
396 * The Peripheral Tree is keyed from the slave + per_id. We're
397 * ignoring the irq bits here since this peripheral structure
398 * should be common for all irqs on the same peripheral.
399 */
400 per_d = radix_tree_lookup(&chip_d->per_tree, (hwirq & ~0x7));
401 if (!per_d) {
402 per_d = kzalloc(sizeof(struct q_perip_data), GFP_KERNEL);
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700403 if (!per_d) {
Michael Bohan392006f2013-01-25 14:29:41 -0800404 rc = -ENOMEM;
405 goto alloc_fail;
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700406 }
Michael Bohan392006f2013-01-25 14:29:41 -0800407 rc = radix_tree_preload(GFP_KERNEL);
408 if (rc)
409 goto alloc_fail;
410 rc = radix_tree_insert(&chip_d->per_tree,
Michael Bohan115cf652012-01-05 14:32:59 -0800411 (hwirq & ~0x7), per_d);
Michael Bohan392006f2013-01-25 14:29:41 -0800412 if (rc)
413 goto alloc_fail;
414 radix_tree_preload_end();
Michael Bohan115cf652012-01-05 14:32:59 -0800415 }
416 irq_d->per_d = per_d;
417
418 return irq_d;
Michael Bohan392006f2013-01-25 14:29:41 -0800419
420alloc_fail:
421 kfree(per_d);
422 kfree(irq_d);
423 return ERR_PTR(rc);
Michael Bohan115cf652012-01-05 14:32:59 -0800424}
425
Michael Bohan115cf652012-01-05 14:32:59 -0800426static int qpnpint_irq_domain_dt_translate(struct irq_domain *d,
427 struct device_node *controller,
428 const u32 *intspec, unsigned int intsize,
429 unsigned long *out_hwirq,
430 unsigned int *out_type)
431{
432 struct qpnp_irq_spec addr;
Michael Bohan115cf652012-01-05 14:32:59 -0800433 int ret;
434
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700435 pr_debug("intspec[0] 0x%x intspec[1] 0x%x intspec[2] 0x%x\n",
436 intspec[0], intspec[1], intspec[2]);
Michael Bohan115cf652012-01-05 14:32:59 -0800437
438 if (d->of_node != controller)
439 return -EINVAL;
440 if (intsize != 3)
441 return -EINVAL;
442
443 addr.irq = intspec[2] & 0x7;
444 addr.per = intspec[1] & 0xFF;
445 addr.slave = intspec[0] & 0xF;
446
447 ret = qpnpint_encode_hwirq(&addr);
448 if (ret < 0) {
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700449 pr_err("invalid intspec\n");
Michael Bohan115cf652012-01-05 14:32:59 -0800450 return ret;
451 }
452 *out_hwirq = ret;
453 *out_type = IRQ_TYPE_NONE;
454
Michael Bohan0435bd62013-02-26 15:29:22 -0800455 pr_debug("out_hwirq = %lu\n", *out_hwirq);
456
Michael Bohan115cf652012-01-05 14:32:59 -0800457 return 0;
458}
459
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700460static void qpnpint_free_irq_data(struct q_irq_data *irq_d)
461{
462 if (irq_d->per_d->use_count == 1)
463 kfree(irq_d->per_d);
464 else
465 irq_d->per_d->use_count--;
466 kfree(irq_d);
467}
468
469static int qpnpint_irq_domain_map(struct irq_domain *d,
470 unsigned int virq, irq_hw_number_t hwirq)
471{
472 struct q_chip_data *chip_d = d->host_data;
473 struct q_irq_data *irq_d;
474 int rc;
475
476 pr_debug("hwirq = %lu\n", hwirq);
477
Michael Bohan0435bd62013-02-26 15:29:22 -0800478 if (hwirq < 0 || hwirq >= QPNPINT_NR_IRQS) {
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700479 pr_err("hwirq %lu out of bounds\n", hwirq);
480 return -EINVAL;
481 }
482
483 irq_radix_revmap_insert(d, virq, hwirq);
484
485 irq_d = qpnpint_alloc_irq_data(chip_d, hwirq);
486 if (IS_ERR(irq_d)) {
487 pr_err("failed to alloc irq data for hwirq %lu\n", hwirq);
488 return PTR_ERR(irq_d);
489 }
490
491 rc = qpnpint_init_irq_data(chip_d, irq_d, hwirq);
492 if (rc) {
493 pr_err("failed to init irq data for hwirq %lu\n", hwirq);
494 goto map_err;
495 }
496
497 irq_set_chip_and_handler(virq,
498 &qpnpint_chip,
499 handle_level_irq);
500 irq_set_chip_data(virq, irq_d);
501#ifdef CONFIG_ARM
502 set_irq_flags(virq, IRQF_VALID);
503#else
504 irq_set_noprobe(virq);
505#endif
506 return 0;
507
508map_err:
509 qpnpint_free_irq_data(irq_d);
510 return rc;
511}
512
513void qpnpint_irq_domain_unmap(struct irq_domain *d, unsigned int virq)
514{
515 struct q_irq_data *irq_d = irq_get_chip_data(virq);
516
517 if (WARN_ON(!irq_d))
518 return;
519
520 qpnpint_free_irq_data(irq_d);
521}
522
Michael Bohan115cf652012-01-05 14:32:59 -0800523const struct irq_domain_ops qpnpint_irq_domain_ops = {
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700524 .map = qpnpint_irq_domain_map,
525 .unmap = qpnpint_irq_domain_unmap,
526 .xlate = qpnpint_irq_domain_dt_translate,
Michael Bohan115cf652012-01-05 14:32:59 -0800527};
528
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700529int qpnpint_register_controller(struct device_node *node,
530 struct spmi_controller *ctrl,
Michael Bohan115cf652012-01-05 14:32:59 -0800531 struct qpnp_local_int *li_cb)
532{
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700533 struct q_chip_data *chip_d;
Michael Bohan115cf652012-01-05 14:32:59 -0800534
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700535 if (!node || !ctrl || ctrl->nr >= QPNPINT_MAX_BUSSES)
536 return -EINVAL;
537
538 list_for_each_entry(chip_d, &qpnpint_chips, list)
539 if (node == chip_d->domain->of_node) {
Michael Bohan0435bd62013-02-26 15:29:22 -0800540 chip_d->cb = kmemdup(li_cb,
541 sizeof(*li_cb), GFP_ATOMIC);
542 if (!chip_d->cb)
543 return -ENOMEM;
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700544 chip_d->spmi_ctrl = ctrl;
545 chip_lookup[ctrl->nr] = chip_d;
546 return 0;
547 }
548
549 return -ENOENT;
Michael Bohan115cf652012-01-05 14:32:59 -0800550}
551EXPORT_SYMBOL(qpnpint_register_controller);
552
Michael Bohan0435bd62013-02-26 15:29:22 -0800553int qpnpint_unregister_controller(struct device_node *node)
554{
555 struct q_chip_data *chip_d;
556
557 if (!node)
558 return -EINVAL;
559
560 list_for_each_entry(chip_d, &qpnpint_chips, list)
561 if (node == chip_d->domain->of_node) {
562 kfree(chip_d->cb);
563 chip_d->cb = NULL;
564 if (chip_d->spmi_ctrl)
565 chip_lookup[chip_d->spmi_ctrl->nr] = NULL;
566 chip_d->spmi_ctrl = NULL;
567 return 0;
568 }
569
570 return -ENOENT;
571}
572EXPORT_SYMBOL(qpnpint_unregister_controller);
573
Michael Bohan115cf652012-01-05 14:32:59 -0800574int qpnpint_handle_irq(struct spmi_controller *spmi_ctrl,
575 struct qpnp_irq_spec *spec)
576{
577 struct irq_domain *domain;
578 unsigned long hwirq, busno;
579 int irq;
580
581 pr_debug("spec slave = %u per = %u irq = %u\n",
582 spec->slave, spec->per, spec->irq);
583
Michael Bohan115cf652012-01-05 14:32:59 -0800584 busno = spmi_ctrl->nr;
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700585 if (!spec || !spmi_ctrl || busno >= QPNPINT_MAX_BUSSES)
Michael Bohan115cf652012-01-05 14:32:59 -0800586 return -EINVAL;
587
588 hwirq = qpnpint_encode_hwirq(spec);
589 if (hwirq < 0) {
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700590 pr_err("invalid irq spec passed\n");
Michael Bohan115cf652012-01-05 14:32:59 -0800591 return -EINVAL;
592 }
593
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700594 domain = chip_lookup[busno]->domain;
595 irq = irq_radix_revmap_lookup(domain, hwirq);
Michael Bohan115cf652012-01-05 14:32:59 -0800596
597 generic_handle_irq(irq);
598
599 return 0;
600}
601EXPORT_SYMBOL(qpnpint_handle_irq);
602
Michael Bohan115cf652012-01-05 14:32:59 -0800603int __init qpnpint_of_init(struct device_node *node, struct device_node *parent)
604{
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700605 struct q_chip_data *chip_d;
606
607 chip_d = kzalloc(sizeof(struct q_chip_data), GFP_KERNEL);
608 if (!chip_d)
609 return -ENOMEM;
610
611 chip_d->domain = irq_domain_add_tree(node,
612 &qpnpint_irq_domain_ops, chip_d);
613 if (!chip_d->domain) {
614 pr_err("Unable to allocate irq_domain\n");
615 kfree(chip_d);
616 return -ENOMEM;
617 }
Michael Bohan115cf652012-01-05 14:32:59 -0800618
619 INIT_RADIX_TREE(&chip_d->per_tree, GFP_ATOMIC);
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700620 list_add(&chip_d->list, &qpnpint_chips);
Michael Bohan115cf652012-01-05 14:32:59 -0800621
622 return 0;
623}
624EXPORT_SYMBOL(qpnpint_of_init);