blob: 2998c0119e0e6f186d287be78a0331a20d41cd10 [file] [log] [blame]
Michael Bohan115cf652012-01-05 14:32:59 -08001/* Copyright (c) 2012, Code Aurora Forum. All rights reserved.
2 *
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>
29
30#include <asm/irq.h>
31#include <asm/mach/irq.h>
32#include <mach/qpnp-int.h>
33
34#define QPNPINT_MAX_BUSSES 1
35
36/* 16 slave_ids, 256 per_ids per slave, and 8 ints per per_id */
37#define QPNPINT_NR_IRQS (16 * 256 * 8)
38
39enum qpnpint_regs {
40 QPNPINT_REG_RT_STS = 0x10,
41 QPNPINT_REG_SET_TYPE = 0x11,
42 QPNPINT_REG_POLARITY_HIGH = 0x12,
43 QPNPINT_REG_POLARITY_LOW = 0x13,
44 QPNPINT_REG_LATCHED_CLR = 0x14,
45 QPNPINT_REG_EN_SET = 0x15,
46 QPNPINT_REG_EN_CLR = 0x16,
47 QPNPINT_REG_LATCHED_STS = 0x18,
48};
49
50struct q_perip_data {
51 uint8_t type; /* bitmap */
52 uint8_t pol_high; /* bitmap */
53 uint8_t pol_low; /* bitmap */
54 uint8_t int_en; /* bitmap */
55 uint8_t use_count;
56};
57
58struct q_irq_data {
59 uint32_t priv_d; /* data to optimize arbiter interactions */
60 struct q_chip_data *chip_d;
61 struct q_perip_data *per_d;
62 uint8_t mask_shift;
63 uint8_t spmi_slave;
64 uint16_t spmi_offset;
65};
66
67struct q_chip_data {
68 int bus_nr;
69 struct irq_domain domain;
70 struct qpnp_local_int cb;
71 struct spmi_controller *spmi_ctrl;
72 struct radix_tree_root per_tree;
73};
74
75static struct q_chip_data chip_data[QPNPINT_MAX_BUSSES] __read_mostly;
76
77/**
78 * qpnpint_encode_hwirq - translate between qpnp_irq_spec and
79 * hwirq representation.
80 *
81 * slave_offset = (addr->slave * 256 * 8);
82 * perip_offset = slave_offset + (addr->perip * 8);
83 * return perip_offset + addr->irq;
84 */
85static inline int qpnpint_encode_hwirq(struct qpnp_irq_spec *spec)
86{
87 uint32_t hwirq;
88
89 if (spec->slave > 15 || spec->irq > 7)
90 return -EINVAL;
91
92 hwirq = (spec->slave << 11);
93 hwirq |= (spec->per << 3);
94 hwirq |= spec->irq;
95
96 return hwirq;
97}
98/**
99 * qpnpint_decode_hwirq - translate between hwirq and
100 * qpnp_irq_spec representation.
101 */
102static inline int qpnpint_decode_hwirq(unsigned long hwirq,
103 struct qpnp_irq_spec *spec)
104{
105 if (hwirq > 65535)
106 return -EINVAL;
107
108 spec->slave = (hwirq >> 11) & 0xF;
109 spec->per = (hwirq >> 3) & 0xFF;
110 spec->irq = hwirq & 0x7;
111 return 0;
112}
113
114static int qpnpint_spmi_write(struct q_irq_data *irq_d, uint8_t reg,
115 void *buf, uint32_t len)
116{
117 struct q_chip_data *chip_d = irq_d->chip_d;
118 int rc;
119
120 if (!chip_d->spmi_ctrl)
121 return -ENODEV;
122
123 rc = spmi_ext_register_writel(chip_d->spmi_ctrl, irq_d->spmi_slave,
124 irq_d->spmi_offset + reg, buf, len);
125 return rc;
126}
127
128static void qpnpint_irq_mask(struct irq_data *d)
129{
130 struct q_irq_data *irq_d = irq_data_get_irq_chip_data(d);
131 struct q_chip_data *chip_d = irq_d->chip_d;
132 struct q_perip_data *per_d = irq_d->per_d;
133 struct qpnp_irq_spec q_spec;
134 int rc;
135
136 pr_debug("hwirq %lu irq: %d\n", d->hwirq, d->irq);
137
138 if (chip_d->cb.mask) {
139 rc = qpnpint_decode_hwirq(d->hwirq, &q_spec);
140 if (rc)
141 pr_err("%s: decode failed on hwirq %lu\n",
142 __func__, d->hwirq);
143 else
144 chip_d->cb.mask(chip_d->spmi_ctrl, &q_spec,
145 irq_d->priv_d);
146 }
147
148 per_d->int_en &= ~irq_d->mask_shift;
149
150 rc = qpnpint_spmi_write(irq_d, QPNPINT_REG_EN_CLR,
151 (u8 *)&irq_d->mask_shift, 1);
152 if (rc)
153 pr_err("%s: spmi failure on irq %d\n",
154 __func__, d->irq);
155}
156
157static void qpnpint_irq_mask_ack(struct irq_data *d)
158{
159 struct q_irq_data *irq_d = irq_data_get_irq_chip_data(d);
160 struct q_chip_data *chip_d = irq_d->chip_d;
161 struct q_perip_data *per_d = irq_d->per_d;
162 struct qpnp_irq_spec q_spec;
163 int rc;
164
165 pr_debug("hwirq %lu irq: %d mask: 0x%x\n", d->hwirq, d->irq,
166 irq_d->mask_shift);
167
168 if (chip_d->cb.mask) {
169 rc = qpnpint_decode_hwirq(d->hwirq, &q_spec);
170 if (rc)
171 pr_err("%s: decode failed on hwirq %lu\n",
172 __func__, d->hwirq);
173 else
174 chip_d->cb.mask(chip_d->spmi_ctrl, &q_spec,
175 irq_d->priv_d);
176 }
177
178 per_d->int_en &= ~irq_d->mask_shift;
179
180 rc = qpnpint_spmi_write(irq_d, QPNPINT_REG_EN_CLR,
181 &irq_d->mask_shift, 1);
182 if (rc)
183 pr_err("%s: spmi failure on irq %d\n",
184 __func__, d->irq);
185
186 rc = qpnpint_spmi_write(irq_d, QPNPINT_REG_LATCHED_CLR,
187 &irq_d->mask_shift, 1);
188 if (rc)
189 pr_err("%s: spmi failure on irq %d\n",
190 __func__, d->irq);
191}
192
193static void qpnpint_irq_unmask(struct irq_data *d)
194{
195 struct q_irq_data *irq_d = irq_data_get_irq_chip_data(d);
196 struct q_chip_data *chip_d = irq_d->chip_d;
197 struct q_perip_data *per_d = irq_d->per_d;
198 struct qpnp_irq_spec q_spec;
199 int rc;
200
201 pr_debug("hwirq %lu irq: %d\n", d->hwirq, d->irq);
202
203 if (chip_d->cb.unmask) {
204 rc = qpnpint_decode_hwirq(d->hwirq, &q_spec);
205 if (rc)
206 pr_err("%s: decode failed on hwirq %lu\n",
207 __func__, d->hwirq);
208 else
209 chip_d->cb.unmask(chip_d->spmi_ctrl, &q_spec,
210 irq_d->priv_d);
211 }
212
213 per_d->int_en |= irq_d->mask_shift;
214 rc = qpnpint_spmi_write(irq_d, QPNPINT_REG_EN_SET,
215 &irq_d->mask_shift, 1);
216 if (rc)
217 pr_err("%s: spmi failure on irq %d\n",
218 __func__, d->irq);
219}
220
221static int qpnpint_irq_set_type(struct irq_data *d, unsigned int flow_type)
222{
223 struct q_irq_data *irq_d = irq_data_get_irq_chip_data(d);
224 struct q_perip_data *per_d = irq_d->per_d;
225 int rc;
226 u8 buf[3];
227
228 pr_debug("hwirq %lu irq: %d flow: 0x%x\n", d->hwirq,
229 d->irq, flow_type);
230
231 per_d->pol_high &= ~irq_d->mask_shift;
232 per_d->pol_low &= ~irq_d->mask_shift;
233 if (flow_type & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)) {
234 per_d->type |= irq_d->mask_shift; /* edge trig */
235 if (flow_type & IRQF_TRIGGER_RISING)
236 per_d->pol_high |= irq_d->mask_shift;
237 if (flow_type & IRQF_TRIGGER_FALLING)
238 per_d->pol_low |= irq_d->mask_shift;
239 } else {
240 if ((flow_type & IRQF_TRIGGER_HIGH) &&
241 (flow_type & IRQF_TRIGGER_LOW))
242 return -EINVAL;
243 per_d->type &= ~irq_d->mask_shift; /* level trig */
244 if (flow_type & IRQF_TRIGGER_HIGH)
245 per_d->pol_high |= irq_d->mask_shift;
246 else
247 per_d->pol_high &= ~irq_d->mask_shift;
248 }
249
250 buf[0] = per_d->type;
251 buf[1] = per_d->pol_high;
252 buf[2] = per_d->pol_low;
253
254 rc = qpnpint_spmi_write(irq_d, QPNPINT_REG_SET_TYPE, &buf, 3);
255 if (rc)
256 pr_err("%s: spmi failure on irq %d\n",
257 __func__, d->irq);
258 return rc;
259}
260
261static struct irq_chip qpnpint_chip = {
262 .name = "qpnp-int",
263 .irq_mask = qpnpint_irq_mask,
264 .irq_mask_ack = qpnpint_irq_mask_ack,
265 .irq_unmask = qpnpint_irq_unmask,
266 .irq_set_type = qpnpint_irq_set_type,
267};
268
269static int qpnpint_init_irq_data(struct q_chip_data *chip_d,
270 struct q_irq_data *irq_d,
271 unsigned long hwirq)
272{
273 struct qpnp_irq_spec q_spec;
274 int rc;
275
276 irq_d->mask_shift = 1 << (hwirq & 0x7);
277 rc = qpnpint_decode_hwirq(hwirq, &q_spec);
278 if (rc < 0)
279 return rc;
280 irq_d->spmi_slave = q_spec.slave;
281 irq_d->spmi_offset = q_spec.per << 8;
282 irq_d->per_d->use_count++;
283 irq_d->chip_d = chip_d;
284
285 if (chip_d->cb.register_priv_data)
286 rc = chip_d->cb.register_priv_data(chip_d->spmi_ctrl, &q_spec,
287 &irq_d->priv_d);
288 return rc;
289}
290
291static struct q_irq_data *qpnpint_alloc_irq_data(
292 struct q_chip_data *chip_d,
293 unsigned long hwirq)
294{
295 struct q_irq_data *irq_d;
296 struct q_perip_data *per_d;
297
298 irq_d = kzalloc(sizeof(struct q_irq_data), GFP_KERNEL);
299 if (!irq_d)
300 return ERR_PTR(-ENOMEM);
301
302 /**
303 * The Peripheral Tree is keyed from the slave + per_id. We're
304 * ignoring the irq bits here since this peripheral structure
305 * should be common for all irqs on the same peripheral.
306 */
307 per_d = radix_tree_lookup(&chip_d->per_tree, (hwirq & ~0x7));
308 if (!per_d) {
309 per_d = kzalloc(sizeof(struct q_perip_data), GFP_KERNEL);
310 if (!per_d)
311 return ERR_PTR(-ENOMEM);
312 radix_tree_insert(&chip_d->per_tree,
313 (hwirq & ~0x7), per_d);
314 }
315 irq_d->per_d = per_d;
316
317 return irq_d;
318}
319
320static int qpnpint_register_int(uint32_t busno, unsigned long hwirq)
321{
322 int irq, rc;
323 struct irq_domain *domain;
324 struct q_irq_data *irq_d;
325
326 pr_debug("busno = %u hwirq = %lu\n", busno, hwirq);
327
328 if (hwirq < 0 || hwirq >= 32768) {
329 pr_err("%s: hwirq %lu out of qpnp interrupt bounds\n",
330 __func__, hwirq);
331 return -EINVAL;
332 }
333
334 if (busno < 0 || busno > QPNPINT_MAX_BUSSES) {
335 pr_err("%s: invalid bus number %d\n", __func__, busno);
336 return -EINVAL;
337 }
338
339 domain = &chip_data[busno].domain;
340 irq = irq_domain_to_irq(domain, hwirq);
341
342 rc = irq_alloc_desc_at(irq, numa_node_id());
343 if (rc < 0) {
344 if (rc != -EEXIST)
345 pr_err("%s: failed to alloc irq at %d with "
346 "rc %d\n", __func__, irq, rc);
347 return rc;
348 }
349 irq_d = qpnpint_alloc_irq_data(&chip_data[busno], hwirq);
350 if (IS_ERR(irq_d)) {
351 pr_err("%s: failed to alloc irq data %d with "
352 "rc %d\n", __func__, irq, rc);
353 rc = PTR_ERR(irq_d);
354 goto register_err_cleanup;
355 }
356 rc = qpnpint_init_irq_data(&chip_data[busno], irq_d, hwirq);
357 if (rc) {
358 pr_err("%s: failed to init irq data %d with "
359 "rc %d\n", __func__, irq, rc);
360 goto register_err_cleanup;
361 }
362
363 irq_domain_register_irq(domain, hwirq);
364
365 irq_set_chip_and_handler(irq,
366 &qpnpint_chip,
367 handle_level_irq);
368 irq_set_chip_data(irq, irq_d);
369#ifdef CONFIG_ARM
370 set_irq_flags(irq, IRQF_VALID);
371#else
372 irq_set_noprobe(irq);
373#endif
374 return 0;
375
376register_err_cleanup:
377 irq_free_desc(irq);
378 if (!IS_ERR(irq_d)) {
379 if (irq_d->per_d->use_count == 1)
380 kfree(irq_d->per_d);
381 else
382 irq_d->per_d->use_count--;
383 kfree(irq_d);
384 }
385 return rc;
386}
387
388static int qpnpint_irq_domain_dt_translate(struct irq_domain *d,
389 struct device_node *controller,
390 const u32 *intspec, unsigned int intsize,
391 unsigned long *out_hwirq,
392 unsigned int *out_type)
393{
394 struct qpnp_irq_spec addr;
395 struct q_chip_data *chip_d = d->priv;
396 int ret;
397
398 pr_debug("%s: intspec[0] 0x%x intspec[1] 0x%x intspec[2] 0x%x\n",
399 __func__, intspec[0], intspec[1], intspec[2]);
400
401 if (d->of_node != controller)
402 return -EINVAL;
403 if (intsize != 3)
404 return -EINVAL;
405
406 addr.irq = intspec[2] & 0x7;
407 addr.per = intspec[1] & 0xFF;
408 addr.slave = intspec[0] & 0xF;
409
410 ret = qpnpint_encode_hwirq(&addr);
411 if (ret < 0) {
412 pr_err("%s: invalid intspec\n", __func__);
413 return ret;
414 }
415 *out_hwirq = ret;
416 *out_type = IRQ_TYPE_NONE;
417
418 /**
419 * Register the interrupt if it's not already registered.
420 * This implies that mapping a qpnp interrupt allocates
421 * resources.
422 */
423 ret = qpnpint_register_int(chip_d->bus_nr, *out_hwirq);
424 if (ret && ret != -EEXIST) {
425 pr_err("%s: Cannot register hwirq %lu\n", __func__, *out_hwirq);
426 return ret;
427 }
428
429 return 0;
430}
431
432const struct irq_domain_ops qpnpint_irq_domain_ops = {
433 .dt_translate = qpnpint_irq_domain_dt_translate,
434};
435
436int qpnpint_register_controller(unsigned int busno,
437 struct qpnp_local_int *li_cb)
438{
439 if (busno >= QPNPINT_MAX_BUSSES)
440 return -EINVAL;
441 chip_data[busno].cb = *li_cb;
442 chip_data[busno].spmi_ctrl = spmi_busnum_to_ctrl(busno);
443 if (!chip_data[busno].spmi_ctrl)
444 return -ENOENT;
445
446 return 0;
447}
448EXPORT_SYMBOL(qpnpint_register_controller);
449
450int qpnpint_handle_irq(struct spmi_controller *spmi_ctrl,
451 struct qpnp_irq_spec *spec)
452{
453 struct irq_domain *domain;
454 unsigned long hwirq, busno;
455 int irq;
456
457 pr_debug("spec slave = %u per = %u irq = %u\n",
458 spec->slave, spec->per, spec->irq);
459
460 if (!spec || !spmi_ctrl)
461 return -EINVAL;
462
463 busno = spmi_ctrl->nr;
464 if (busno >= QPNPINT_MAX_BUSSES)
465 return -EINVAL;
466
467 hwirq = qpnpint_encode_hwirq(spec);
468 if (hwirq < 0) {
469 pr_err("%s: invalid irq spec passed\n", __func__);
470 return -EINVAL;
471 }
472
473 domain = &chip_data[busno].domain;
474 irq = irq_domain_to_irq(domain, hwirq);
475
476 generic_handle_irq(irq);
477
478 return 0;
479}
480EXPORT_SYMBOL(qpnpint_handle_irq);
481
482/**
483 * This assumes that there's a relationship between the order of the interrupt
484 * controllers specified to of_irq_match() is the SPMI device topology. If
485 * this ever turns out to be a bad assumption, then of_irq_init_cb_t should
486 * be modified to pass a parameter to this function.
487 */
488static int qpnpint_cnt __initdata;
489
490int __init qpnpint_of_init(struct device_node *node, struct device_node *parent)
491{
492 struct q_chip_data *chip_d = &chip_data[qpnpint_cnt];
493 struct irq_domain *domain = &chip_d->domain;
494
495 INIT_RADIX_TREE(&chip_d->per_tree, GFP_ATOMIC);
496
497 domain->irq_base = irq_domain_find_free_range(0, QPNPINT_NR_IRQS);
498 domain->nr_irq = QPNPINT_NR_IRQS;
499 domain->of_node = of_node_get(node);
500 domain->priv = chip_d;
501 domain->ops = &qpnpint_irq_domain_ops;
502 irq_domain_add(domain);
503
504 pr_info("irq_base = %d\n", domain->irq_base);
505
506 qpnpint_cnt++;
507
508 return 0;
509}
510EXPORT_SYMBOL(qpnpint_of_init);