blob: b6dfd51d7e87c5e4fbcda37144df7628603d55ba [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
Michael Bohan115cf652012-01-05 14:32:59 -080034/* 16 slave_ids, 256 per_ids per slave, and 8 ints per per_id */
35#define QPNPINT_NR_IRQS (16 * 256 * 8)
36
37enum qpnpint_regs {
38 QPNPINT_REG_RT_STS = 0x10,
39 QPNPINT_REG_SET_TYPE = 0x11,
40 QPNPINT_REG_POLARITY_HIGH = 0x12,
41 QPNPINT_REG_POLARITY_LOW = 0x13,
42 QPNPINT_REG_LATCHED_CLR = 0x14,
43 QPNPINT_REG_EN_SET = 0x15,
44 QPNPINT_REG_EN_CLR = 0x16,
45 QPNPINT_REG_LATCHED_STS = 0x18,
46};
47
48struct q_perip_data {
49 uint8_t type; /* bitmap */
50 uint8_t pol_high; /* bitmap */
51 uint8_t pol_low; /* bitmap */
52 uint8_t int_en; /* bitmap */
53 uint8_t use_count;
54};
55
56struct q_irq_data {
57 uint32_t priv_d; /* data to optimize arbiter interactions */
58 struct q_chip_data *chip_d;
59 struct q_perip_data *per_d;
60 uint8_t mask_shift;
61 uint8_t spmi_slave;
62 uint16_t spmi_offset;
63};
64
65struct q_chip_data {
66 int bus_nr;
Michael Bohanbb6b30f2012-06-01 13:33:51 -070067 struct irq_domain *domain;
Michael Bohan115cf652012-01-05 14:32:59 -080068 struct qpnp_local_int cb;
69 struct spmi_controller *spmi_ctrl;
70 struct radix_tree_root per_tree;
Michael Bohanbb6b30f2012-06-01 13:33:51 -070071 struct list_head list;
Michael Bohan115cf652012-01-05 14:32:59 -080072};
73
Michael Bohanbb6b30f2012-06-01 13:33:51 -070074static LIST_HEAD(qpnpint_chips);
75static DEFINE_MUTEX(qpnpint_chips_mutex);
76
77#define QPNPINT_MAX_BUSSES 4
78struct q_chip_data *chip_lookup[QPNPINT_MAX_BUSSES];
Michael Bohan115cf652012-01-05 14:32:59 -080079
80/**
81 * qpnpint_encode_hwirq - translate between qpnp_irq_spec and
82 * hwirq representation.
83 *
84 * slave_offset = (addr->slave * 256 * 8);
85 * perip_offset = slave_offset + (addr->perip * 8);
86 * return perip_offset + addr->irq;
87 */
88static inline int qpnpint_encode_hwirq(struct qpnp_irq_spec *spec)
89{
90 uint32_t hwirq;
91
92 if (spec->slave > 15 || spec->irq > 7)
93 return -EINVAL;
94
95 hwirq = (spec->slave << 11);
96 hwirq |= (spec->per << 3);
97 hwirq |= spec->irq;
98
99 return hwirq;
100}
101/**
102 * qpnpint_decode_hwirq - translate between hwirq and
103 * qpnp_irq_spec representation.
104 */
105static inline int qpnpint_decode_hwirq(unsigned long hwirq,
106 struct qpnp_irq_spec *spec)
107{
108 if (hwirq > 65535)
109 return -EINVAL;
110
111 spec->slave = (hwirq >> 11) & 0xF;
112 spec->per = (hwirq >> 3) & 0xFF;
113 spec->irq = hwirq & 0x7;
114 return 0;
115}
116
117static int qpnpint_spmi_write(struct q_irq_data *irq_d, uint8_t reg,
118 void *buf, uint32_t len)
119{
120 struct q_chip_data *chip_d = irq_d->chip_d;
121 int rc;
122
123 if (!chip_d->spmi_ctrl)
124 return -ENODEV;
125
126 rc = spmi_ext_register_writel(chip_d->spmi_ctrl, irq_d->spmi_slave,
127 irq_d->spmi_offset + reg, buf, len);
128 return rc;
129}
130
131static void qpnpint_irq_mask(struct irq_data *d)
132{
133 struct q_irq_data *irq_d = irq_data_get_irq_chip_data(d);
134 struct q_chip_data *chip_d = irq_d->chip_d;
135 struct q_perip_data *per_d = irq_d->per_d;
136 struct qpnp_irq_spec q_spec;
137 int rc;
138
139 pr_debug("hwirq %lu irq: %d\n", d->hwirq, d->irq);
140
141 if (chip_d->cb.mask) {
142 rc = qpnpint_decode_hwirq(d->hwirq, &q_spec);
143 if (rc)
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700144 pr_err("decode failed on hwirq %lu\n", d->hwirq);
Michael Bohan115cf652012-01-05 14:32:59 -0800145 else
146 chip_d->cb.mask(chip_d->spmi_ctrl, &q_spec,
147 irq_d->priv_d);
148 }
149
150 per_d->int_en &= ~irq_d->mask_shift;
151
152 rc = qpnpint_spmi_write(irq_d, QPNPINT_REG_EN_CLR,
153 (u8 *)&irq_d->mask_shift, 1);
154 if (rc)
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700155 pr_err("spmi failure on irq %d\n", d->irq);
Michael Bohan115cf652012-01-05 14:32:59 -0800156}
157
158static void qpnpint_irq_mask_ack(struct irq_data *d)
159{
160 struct q_irq_data *irq_d = irq_data_get_irq_chip_data(d);
161 struct q_chip_data *chip_d = irq_d->chip_d;
162 struct q_perip_data *per_d = irq_d->per_d;
163 struct qpnp_irq_spec q_spec;
164 int rc;
165
166 pr_debug("hwirq %lu irq: %d mask: 0x%x\n", d->hwirq, d->irq,
167 irq_d->mask_shift);
168
169 if (chip_d->cb.mask) {
170 rc = qpnpint_decode_hwirq(d->hwirq, &q_spec);
171 if (rc)
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700172 pr_err("decode failed on hwirq %lu\n", d->hwirq);
Michael Bohan115cf652012-01-05 14:32:59 -0800173 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)
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700183 pr_err("spmi failure on irq %d\n", d->irq);
Michael Bohan115cf652012-01-05 14:32:59 -0800184
185 rc = qpnpint_spmi_write(irq_d, QPNPINT_REG_LATCHED_CLR,
186 &irq_d->mask_shift, 1);
187 if (rc)
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700188 pr_err("spmi failure on irq %d\n", d->irq);
Michael Bohan115cf652012-01-05 14:32:59 -0800189}
190
191static void qpnpint_irq_unmask(struct irq_data *d)
192{
193 struct q_irq_data *irq_d = irq_data_get_irq_chip_data(d);
194 struct q_chip_data *chip_d = irq_d->chip_d;
195 struct q_perip_data *per_d = irq_d->per_d;
196 struct qpnp_irq_spec q_spec;
197 int rc;
198
199 pr_debug("hwirq %lu irq: %d\n", d->hwirq, d->irq);
200
201 if (chip_d->cb.unmask) {
202 rc = qpnpint_decode_hwirq(d->hwirq, &q_spec);
203 if (rc)
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700204 pr_err("decode failed on hwirq %lu\n", d->hwirq);
Michael Bohan115cf652012-01-05 14:32:59 -0800205 else
206 chip_d->cb.unmask(chip_d->spmi_ctrl, &q_spec,
207 irq_d->priv_d);
208 }
209
210 per_d->int_en |= irq_d->mask_shift;
211 rc = qpnpint_spmi_write(irq_d, QPNPINT_REG_EN_SET,
212 &irq_d->mask_shift, 1);
213 if (rc)
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700214 pr_err("spmi failure on irq %d\n", d->irq);
Michael Bohan115cf652012-01-05 14:32:59 -0800215}
216
217static int qpnpint_irq_set_type(struct irq_data *d, unsigned int flow_type)
218{
219 struct q_irq_data *irq_d = irq_data_get_irq_chip_data(d);
220 struct q_perip_data *per_d = irq_d->per_d;
221 int rc;
222 u8 buf[3];
223
224 pr_debug("hwirq %lu irq: %d flow: 0x%x\n", d->hwirq,
225 d->irq, flow_type);
226
227 per_d->pol_high &= ~irq_d->mask_shift;
228 per_d->pol_low &= ~irq_d->mask_shift;
229 if (flow_type & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)) {
230 per_d->type |= irq_d->mask_shift; /* edge trig */
231 if (flow_type & IRQF_TRIGGER_RISING)
232 per_d->pol_high |= irq_d->mask_shift;
233 if (flow_type & IRQF_TRIGGER_FALLING)
234 per_d->pol_low |= irq_d->mask_shift;
235 } else {
236 if ((flow_type & IRQF_TRIGGER_HIGH) &&
237 (flow_type & IRQF_TRIGGER_LOW))
238 return -EINVAL;
239 per_d->type &= ~irq_d->mask_shift; /* level trig */
240 if (flow_type & IRQF_TRIGGER_HIGH)
241 per_d->pol_high |= irq_d->mask_shift;
242 else
Michael Bohan69701d32012-06-07 17:05:41 -0700243 per_d->pol_low |= irq_d->mask_shift;
Michael Bohan115cf652012-01-05 14:32:59 -0800244 }
245
246 buf[0] = per_d->type;
247 buf[1] = per_d->pol_high;
248 buf[2] = per_d->pol_low;
249
250 rc = qpnpint_spmi_write(irq_d, QPNPINT_REG_SET_TYPE, &buf, 3);
251 if (rc)
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700252 pr_err("spmi failure on irq %d\n", d->irq);
Michael Bohan115cf652012-01-05 14:32:59 -0800253 return rc;
254}
255
256static struct irq_chip qpnpint_chip = {
257 .name = "qpnp-int",
258 .irq_mask = qpnpint_irq_mask,
259 .irq_mask_ack = qpnpint_irq_mask_ack,
260 .irq_unmask = qpnpint_irq_unmask,
261 .irq_set_type = qpnpint_irq_set_type,
262};
263
264static int qpnpint_init_irq_data(struct q_chip_data *chip_d,
265 struct q_irq_data *irq_d,
266 unsigned long hwirq)
267{
268 struct qpnp_irq_spec q_spec;
269 int rc;
270
271 irq_d->mask_shift = 1 << (hwirq & 0x7);
272 rc = qpnpint_decode_hwirq(hwirq, &q_spec);
273 if (rc < 0)
274 return rc;
275 irq_d->spmi_slave = q_spec.slave;
276 irq_d->spmi_offset = q_spec.per << 8;
Michael Bohan115cf652012-01-05 14:32:59 -0800277 irq_d->chip_d = chip_d;
278
279 if (chip_d->cb.register_priv_data)
280 rc = chip_d->cb.register_priv_data(chip_d->spmi_ctrl, &q_spec,
281 &irq_d->priv_d);
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700282 if (rc)
283 return rc;
284
285 irq_d->per_d->use_count++;
286 return 0;
Michael Bohan115cf652012-01-05 14:32:59 -0800287}
288
289static struct q_irq_data *qpnpint_alloc_irq_data(
290 struct q_chip_data *chip_d,
291 unsigned long hwirq)
292{
293 struct q_irq_data *irq_d;
294 struct q_perip_data *per_d;
295
296 irq_d = kzalloc(sizeof(struct q_irq_data), GFP_KERNEL);
297 if (!irq_d)
298 return ERR_PTR(-ENOMEM);
299
300 /**
301 * The Peripheral Tree is keyed from the slave + per_id. We're
302 * ignoring the irq bits here since this peripheral structure
303 * should be common for all irqs on the same peripheral.
304 */
305 per_d = radix_tree_lookup(&chip_d->per_tree, (hwirq & ~0x7));
306 if (!per_d) {
307 per_d = kzalloc(sizeof(struct q_perip_data), GFP_KERNEL);
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700308 if (!per_d) {
309 kfree(irq_d);
Michael Bohan115cf652012-01-05 14:32:59 -0800310 return ERR_PTR(-ENOMEM);
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700311 }
Michael Bohan115cf652012-01-05 14:32:59 -0800312 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
Michael Bohan115cf652012-01-05 14:32:59 -0800320static int qpnpint_irq_domain_dt_translate(struct irq_domain *d,
321 struct device_node *controller,
322 const u32 *intspec, unsigned int intsize,
323 unsigned long *out_hwirq,
324 unsigned int *out_type)
325{
326 struct qpnp_irq_spec addr;
Michael Bohan115cf652012-01-05 14:32:59 -0800327 int ret;
328
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700329 pr_debug("intspec[0] 0x%x intspec[1] 0x%x intspec[2] 0x%x\n",
330 intspec[0], intspec[1], intspec[2]);
Michael Bohan115cf652012-01-05 14:32:59 -0800331
332 if (d->of_node != controller)
333 return -EINVAL;
334 if (intsize != 3)
335 return -EINVAL;
336
337 addr.irq = intspec[2] & 0x7;
338 addr.per = intspec[1] & 0xFF;
339 addr.slave = intspec[0] & 0xF;
340
341 ret = qpnpint_encode_hwirq(&addr);
342 if (ret < 0) {
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700343 pr_err("invalid intspec\n");
Michael Bohan115cf652012-01-05 14:32:59 -0800344 return ret;
345 }
346 *out_hwirq = ret;
347 *out_type = IRQ_TYPE_NONE;
348
Michael Bohan115cf652012-01-05 14:32:59 -0800349 return 0;
350}
351
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700352static void qpnpint_free_irq_data(struct q_irq_data *irq_d)
353{
354 if (irq_d->per_d->use_count == 1)
355 kfree(irq_d->per_d);
356 else
357 irq_d->per_d->use_count--;
358 kfree(irq_d);
359}
360
361static int qpnpint_irq_domain_map(struct irq_domain *d,
362 unsigned int virq, irq_hw_number_t hwirq)
363{
364 struct q_chip_data *chip_d = d->host_data;
365 struct q_irq_data *irq_d;
366 int rc;
367
368 pr_debug("hwirq = %lu\n", hwirq);
369
370 if (hwirq < 0 || hwirq >= 32768) {
371 pr_err("hwirq %lu out of bounds\n", hwirq);
372 return -EINVAL;
373 }
374
375 irq_radix_revmap_insert(d, virq, hwirq);
376
377 irq_d = qpnpint_alloc_irq_data(chip_d, hwirq);
378 if (IS_ERR(irq_d)) {
379 pr_err("failed to alloc irq data for hwirq %lu\n", hwirq);
380 return PTR_ERR(irq_d);
381 }
382
383 rc = qpnpint_init_irq_data(chip_d, irq_d, hwirq);
384 if (rc) {
385 pr_err("failed to init irq data for hwirq %lu\n", hwirq);
386 goto map_err;
387 }
388
389 irq_set_chip_and_handler(virq,
390 &qpnpint_chip,
391 handle_level_irq);
392 irq_set_chip_data(virq, irq_d);
393#ifdef CONFIG_ARM
394 set_irq_flags(virq, IRQF_VALID);
395#else
396 irq_set_noprobe(virq);
397#endif
398 return 0;
399
400map_err:
401 qpnpint_free_irq_data(irq_d);
402 return rc;
403}
404
405void qpnpint_irq_domain_unmap(struct irq_domain *d, unsigned int virq)
406{
407 struct q_irq_data *irq_d = irq_get_chip_data(virq);
408
409 if (WARN_ON(!irq_d))
410 return;
411
412 qpnpint_free_irq_data(irq_d);
413}
414
Michael Bohan115cf652012-01-05 14:32:59 -0800415const struct irq_domain_ops qpnpint_irq_domain_ops = {
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700416 .map = qpnpint_irq_domain_map,
417 .unmap = qpnpint_irq_domain_unmap,
418 .xlate = qpnpint_irq_domain_dt_translate,
Michael Bohan115cf652012-01-05 14:32:59 -0800419};
420
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700421int qpnpint_register_controller(struct device_node *node,
422 struct spmi_controller *ctrl,
Michael Bohan115cf652012-01-05 14:32:59 -0800423 struct qpnp_local_int *li_cb)
424{
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700425 struct q_chip_data *chip_d;
Michael Bohan115cf652012-01-05 14:32:59 -0800426
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700427 if (!node || !ctrl || ctrl->nr >= QPNPINT_MAX_BUSSES)
428 return -EINVAL;
429
430 list_for_each_entry(chip_d, &qpnpint_chips, list)
431 if (node == chip_d->domain->of_node) {
432 chip_d->cb = *li_cb;
433 chip_d->spmi_ctrl = ctrl;
434 chip_lookup[ctrl->nr] = chip_d;
435 return 0;
436 }
437
438 return -ENOENT;
Michael Bohan115cf652012-01-05 14:32:59 -0800439}
440EXPORT_SYMBOL(qpnpint_register_controller);
441
442int qpnpint_handle_irq(struct spmi_controller *spmi_ctrl,
443 struct qpnp_irq_spec *spec)
444{
445 struct irq_domain *domain;
446 unsigned long hwirq, busno;
447 int irq;
448
449 pr_debug("spec slave = %u per = %u irq = %u\n",
450 spec->slave, spec->per, spec->irq);
451
Michael Bohan115cf652012-01-05 14:32:59 -0800452 busno = spmi_ctrl->nr;
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700453 if (!spec || !spmi_ctrl || busno >= QPNPINT_MAX_BUSSES)
Michael Bohan115cf652012-01-05 14:32:59 -0800454 return -EINVAL;
455
456 hwirq = qpnpint_encode_hwirq(spec);
457 if (hwirq < 0) {
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700458 pr_err("invalid irq spec passed\n");
Michael Bohan115cf652012-01-05 14:32:59 -0800459 return -EINVAL;
460 }
461
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700462 domain = chip_lookup[busno]->domain;
463 irq = irq_radix_revmap_lookup(domain, hwirq);
Michael Bohan115cf652012-01-05 14:32:59 -0800464
465 generic_handle_irq(irq);
466
467 return 0;
468}
469EXPORT_SYMBOL(qpnpint_handle_irq);
470
Michael Bohan115cf652012-01-05 14:32:59 -0800471int __init qpnpint_of_init(struct device_node *node, struct device_node *parent)
472{
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700473 struct q_chip_data *chip_d;
474
475 chip_d = kzalloc(sizeof(struct q_chip_data), GFP_KERNEL);
476 if (!chip_d)
477 return -ENOMEM;
478
479 chip_d->domain = irq_domain_add_tree(node,
480 &qpnpint_irq_domain_ops, chip_d);
481 if (!chip_d->domain) {
482 pr_err("Unable to allocate irq_domain\n");
483 kfree(chip_d);
484 return -ENOMEM;
485 }
Michael Bohan115cf652012-01-05 14:32:59 -0800486
487 INIT_RADIX_TREE(&chip_d->per_tree, GFP_ATOMIC);
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700488 list_add(&chip_d->list, &qpnpint_chips);
Michael Bohan115cf652012-01-05 14:32:59 -0800489
490 return 0;
491}
492EXPORT_SYMBOL(qpnpint_of_init);