xref: /linux/drivers/mfd/twl4030-irq.c (revision b0b4a7c28ebee0763cae90d470cfff501a83be37)
1a30d46c0SDavid Brownell /*
2a30d46c0SDavid Brownell  * twl4030-irq.c - TWL4030/TPS659x0 irq support
3a30d46c0SDavid Brownell  *
4a30d46c0SDavid Brownell  * Copyright (C) 2005-2006 Texas Instruments, Inc.
5a30d46c0SDavid Brownell  *
6a30d46c0SDavid Brownell  * Modifications to defer interrupt handling to a kernel thread:
7a30d46c0SDavid Brownell  * Copyright (C) 2006 MontaVista Software, Inc.
8a30d46c0SDavid Brownell  *
9a30d46c0SDavid Brownell  * Based on tlv320aic23.c:
10a30d46c0SDavid Brownell  * Copyright (c) by Kai Svahn <kai.svahn@nokia.com>
11a30d46c0SDavid Brownell  *
12a30d46c0SDavid Brownell  * Code cleanup and modifications to IRQ handler.
13a30d46c0SDavid Brownell  * by syed khasim <x0khasim@ti.com>
14a30d46c0SDavid Brownell  *
15a30d46c0SDavid Brownell  * This program is free software; you can redistribute it and/or modify
16a30d46c0SDavid Brownell  * it under the terms of the GNU General Public License as published by
17a30d46c0SDavid Brownell  * the Free Software Foundation; either version 2 of the License, or
18a30d46c0SDavid Brownell  * (at your option) any later version.
19a30d46c0SDavid Brownell  *
20a30d46c0SDavid Brownell  * This program is distributed in the hope that it will be useful,
21a30d46c0SDavid Brownell  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22a30d46c0SDavid Brownell  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23a30d46c0SDavid Brownell  * GNU General Public License for more details.
24a30d46c0SDavid Brownell  *
25a30d46c0SDavid Brownell  * You should have received a copy of the GNU General Public License
26a30d46c0SDavid Brownell  * along with this program; if not, write to the Free Software
27a30d46c0SDavid Brownell  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
28a30d46c0SDavid Brownell  */
29a30d46c0SDavid Brownell 
30a30d46c0SDavid Brownell #include <linux/init.h>
31a30d46c0SDavid Brownell #include <linux/interrupt.h>
32a30d46c0SDavid Brownell #include <linux/irq.h>
33a30d46c0SDavid Brownell #include <linux/kthread.h>
345a0e3ad6STejun Heo #include <linux/slab.h>
35a30d46c0SDavid Brownell 
36b07682b6SSantosh Shilimkar #include <linux/i2c/twl.h>
37a30d46c0SDavid Brownell 
38*b0b4a7c2SG, Manjunath Kondaiah #include "twl-core.h"
39a30d46c0SDavid Brownell 
40a30d46c0SDavid Brownell /*
41a30d46c0SDavid Brownell  * TWL4030 IRQ handling has two stages in hardware, and thus in software.
42a30d46c0SDavid Brownell  * The Primary Interrupt Handler (PIH) stage exposes status bits saying
43a30d46c0SDavid Brownell  * which Secondary Interrupt Handler (SIH) stage is raising an interrupt.
44a30d46c0SDavid Brownell  * SIH modules are more traditional IRQ components, which support per-IRQ
45a30d46c0SDavid Brownell  * enable/disable and trigger controls; they do most of the work.
46a30d46c0SDavid Brownell  *
47a30d46c0SDavid Brownell  * These chips are designed to support IRQ handling from two different
48a30d46c0SDavid Brownell  * I2C masters.  Each has a dedicated IRQ line, and dedicated IRQ status
49a30d46c0SDavid Brownell  * and mask registers in the PIH and SIH modules.
50a30d46c0SDavid Brownell  *
51a30d46c0SDavid Brownell  * We set up IRQs starting at a platform-specified base, always starting
52a30d46c0SDavid Brownell  * with PIH and the SIH for PWR_INT and then usually adding GPIO:
53a30d46c0SDavid Brownell  *	base + 0  .. base + 7	PIH
54a30d46c0SDavid Brownell  *	base + 8  .. base + 15	SIH for PWR_INT
55a30d46c0SDavid Brownell  *	base + 16 .. base + 33	SIH for GPIO
56a30d46c0SDavid Brownell  */
57a30d46c0SDavid Brownell 
58a30d46c0SDavid Brownell /* PIH register offsets */
59a30d46c0SDavid Brownell #define REG_PIH_ISR_P1			0x01
60a30d46c0SDavid Brownell #define REG_PIH_ISR_P2			0x02
61a30d46c0SDavid Brownell #define REG_PIH_SIR			0x03	/* for testing */
62a30d46c0SDavid Brownell 
63a30d46c0SDavid Brownell 
64a30d46c0SDavid Brownell /* Linux could (eventually) use either IRQ line */
65a30d46c0SDavid Brownell static int irq_line;
66a30d46c0SDavid Brownell 
67a30d46c0SDavid Brownell struct sih {
68a30d46c0SDavid Brownell 	char	name[8];
69a30d46c0SDavid Brownell 	u8	module;			/* module id */
70a30d46c0SDavid Brownell 	u8	control_offset;		/* for SIH_CTRL */
71a30d46c0SDavid Brownell 	bool	set_cor;
72a30d46c0SDavid Brownell 
73a30d46c0SDavid Brownell 	u8	bits;			/* valid in isr/imr */
74a30d46c0SDavid Brownell 	u8	bytes_ixr;		/* bytelen of ISR/IMR/SIR */
75a30d46c0SDavid Brownell 
76a30d46c0SDavid Brownell 	u8	edr_offset;
77a30d46c0SDavid Brownell 	u8	bytes_edr;		/* bytelen of EDR */
78a30d46c0SDavid Brownell 
791920a61eSIlkka Koskinen 	u8	irq_lines;		/* number of supported irq lines */
801920a61eSIlkka Koskinen 
81a30d46c0SDavid Brownell 	/* SIR ignored -- set interrupt, for testing only */
8235a27e8eSThomas Gleixner 	struct sih_irq_data {
83a30d46c0SDavid Brownell 		u8	isr_offset;
84a30d46c0SDavid Brownell 		u8	imr_offset;
85a30d46c0SDavid Brownell 	} mask[2];
86a30d46c0SDavid Brownell 	/* + 2 bytes padding */
87a30d46c0SDavid Brownell };
88a30d46c0SDavid Brownell 
891920a61eSIlkka Koskinen static const struct sih *sih_modules;
901920a61eSIlkka Koskinen static int nr_sih_modules;
911920a61eSIlkka Koskinen 
92a30d46c0SDavid Brownell #define SIH_INITIALIZER(modname, nbits) \
93a30d46c0SDavid Brownell 	.module		= TWL4030_MODULE_ ## modname, \
94a30d46c0SDavid Brownell 	.control_offset = TWL4030_ ## modname ## _SIH_CTRL, \
95a30d46c0SDavid Brownell 	.bits		= nbits, \
96a30d46c0SDavid Brownell 	.bytes_ixr	= DIV_ROUND_UP(nbits, 8), \
97a30d46c0SDavid Brownell 	.edr_offset	= TWL4030_ ## modname ## _EDR, \
98a30d46c0SDavid Brownell 	.bytes_edr	= DIV_ROUND_UP((2*(nbits)), 8), \
991920a61eSIlkka Koskinen 	.irq_lines	= 2, \
100a30d46c0SDavid Brownell 	.mask = { { \
101a30d46c0SDavid Brownell 		.isr_offset	= TWL4030_ ## modname ## _ISR1, \
102a30d46c0SDavid Brownell 		.imr_offset	= TWL4030_ ## modname ## _IMR1, \
103a30d46c0SDavid Brownell 	}, \
104a30d46c0SDavid Brownell 	{ \
105a30d46c0SDavid Brownell 		.isr_offset	= TWL4030_ ## modname ## _ISR2, \
106a30d46c0SDavid Brownell 		.imr_offset	= TWL4030_ ## modname ## _IMR2, \
107a30d46c0SDavid Brownell 	}, },
108a30d46c0SDavid Brownell 
109a30d46c0SDavid Brownell /* register naming policies are inconsistent ... */
110a30d46c0SDavid Brownell #define TWL4030_INT_PWR_EDR		TWL4030_INT_PWR_EDR1
111a30d46c0SDavid Brownell #define TWL4030_MODULE_KEYPAD_KEYP	TWL4030_MODULE_KEYPAD
112a30d46c0SDavid Brownell #define TWL4030_MODULE_INT_PWR		TWL4030_MODULE_INT
113a30d46c0SDavid Brownell 
114a30d46c0SDavid Brownell 
115a30d46c0SDavid Brownell /* Order in this table matches order in PIH_ISR.  That is,
116a30d46c0SDavid Brownell  * BIT(n) in PIH_ISR is sih_modules[n].
117a30d46c0SDavid Brownell  */
1181920a61eSIlkka Koskinen /* sih_modules_twl4030 is used both in twl4030 and twl5030 */
1191920a61eSIlkka Koskinen static const struct sih sih_modules_twl4030[6] = {
120a30d46c0SDavid Brownell 	[0] = {
121a30d46c0SDavid Brownell 		.name		= "gpio",
122a30d46c0SDavid Brownell 		.module		= TWL4030_MODULE_GPIO,
123a30d46c0SDavid Brownell 		.control_offset	= REG_GPIO_SIH_CTRL,
124a30d46c0SDavid Brownell 		.set_cor	= true,
125a30d46c0SDavid Brownell 		.bits		= TWL4030_GPIO_MAX,
126a30d46c0SDavid Brownell 		.bytes_ixr	= 3,
127a30d46c0SDavid Brownell 		/* Note: *all* of these IRQs default to no-trigger */
128a30d46c0SDavid Brownell 		.edr_offset	= REG_GPIO_EDR1,
129a30d46c0SDavid Brownell 		.bytes_edr	= 5,
1301920a61eSIlkka Koskinen 		.irq_lines	= 2,
131a30d46c0SDavid Brownell 		.mask = { {
132a30d46c0SDavid Brownell 			.isr_offset	= REG_GPIO_ISR1A,
133a30d46c0SDavid Brownell 			.imr_offset	= REG_GPIO_IMR1A,
134a30d46c0SDavid Brownell 		}, {
135a30d46c0SDavid Brownell 			.isr_offset	= REG_GPIO_ISR1B,
136a30d46c0SDavid Brownell 			.imr_offset	= REG_GPIO_IMR1B,
137a30d46c0SDavid Brownell 		}, },
138a30d46c0SDavid Brownell 	},
139a30d46c0SDavid Brownell 	[1] = {
140a30d46c0SDavid Brownell 		.name		= "keypad",
141a30d46c0SDavid Brownell 		.set_cor	= true,
142a30d46c0SDavid Brownell 		SIH_INITIALIZER(KEYPAD_KEYP, 4)
143a30d46c0SDavid Brownell 	},
144a30d46c0SDavid Brownell 	[2] = {
145a30d46c0SDavid Brownell 		.name		= "bci",
146a30d46c0SDavid Brownell 		.module		= TWL4030_MODULE_INTERRUPTS,
147a30d46c0SDavid Brownell 		.control_offset	= TWL4030_INTERRUPTS_BCISIHCTRL,
1488e52e279SGrazvydas Ignotas 		.set_cor	= true,
149a30d46c0SDavid Brownell 		.bits		= 12,
150a30d46c0SDavid Brownell 		.bytes_ixr	= 2,
151a30d46c0SDavid Brownell 		.edr_offset	= TWL4030_INTERRUPTS_BCIEDR1,
152a30d46c0SDavid Brownell 		/* Note: most of these IRQs default to no-trigger */
153a30d46c0SDavid Brownell 		.bytes_edr	= 3,
1541920a61eSIlkka Koskinen 		.irq_lines	= 2,
155a30d46c0SDavid Brownell 		.mask = { {
156a30d46c0SDavid Brownell 			.isr_offset	= TWL4030_INTERRUPTS_BCIISR1A,
157a30d46c0SDavid Brownell 			.imr_offset	= TWL4030_INTERRUPTS_BCIIMR1A,
158a30d46c0SDavid Brownell 		}, {
159a30d46c0SDavid Brownell 			.isr_offset	= TWL4030_INTERRUPTS_BCIISR1B,
160a30d46c0SDavid Brownell 			.imr_offset	= TWL4030_INTERRUPTS_BCIIMR1B,
161a30d46c0SDavid Brownell 		}, },
162a30d46c0SDavid Brownell 	},
163a30d46c0SDavid Brownell 	[3] = {
164a30d46c0SDavid Brownell 		.name		= "madc",
165a30d46c0SDavid Brownell 		SIH_INITIALIZER(MADC, 4)
166a30d46c0SDavid Brownell 	},
167a30d46c0SDavid Brownell 	[4] = {
168a30d46c0SDavid Brownell 		/* USB doesn't use the same SIH organization */
169a30d46c0SDavid Brownell 		.name		= "usb",
170a30d46c0SDavid Brownell 	},
171a30d46c0SDavid Brownell 	[5] = {
172a30d46c0SDavid Brownell 		.name		= "power",
173a30d46c0SDavid Brownell 		.set_cor	= true,
174a30d46c0SDavid Brownell 		SIH_INITIALIZER(INT_PWR, 8)
175a30d46c0SDavid Brownell 	},
176a30d46c0SDavid Brownell 		/* there are no SIH modules #6 or #7 ... */
177a30d46c0SDavid Brownell };
178a30d46c0SDavid Brownell 
1791920a61eSIlkka Koskinen static const struct sih sih_modules_twl5031[8] = {
1801920a61eSIlkka Koskinen 	[0] = {
1811920a61eSIlkka Koskinen 		.name		= "gpio",
1821920a61eSIlkka Koskinen 		.module		= TWL4030_MODULE_GPIO,
1831920a61eSIlkka Koskinen 		.control_offset	= REG_GPIO_SIH_CTRL,
1841920a61eSIlkka Koskinen 		.set_cor	= true,
1851920a61eSIlkka Koskinen 		.bits		= TWL4030_GPIO_MAX,
1861920a61eSIlkka Koskinen 		.bytes_ixr	= 3,
1871920a61eSIlkka Koskinen 		/* Note: *all* of these IRQs default to no-trigger */
1881920a61eSIlkka Koskinen 		.edr_offset	= REG_GPIO_EDR1,
1891920a61eSIlkka Koskinen 		.bytes_edr	= 5,
1901920a61eSIlkka Koskinen 		.irq_lines	= 2,
1911920a61eSIlkka Koskinen 		.mask = { {
1921920a61eSIlkka Koskinen 			.isr_offset	= REG_GPIO_ISR1A,
1931920a61eSIlkka Koskinen 			.imr_offset	= REG_GPIO_IMR1A,
1941920a61eSIlkka Koskinen 		}, {
1951920a61eSIlkka Koskinen 			.isr_offset	= REG_GPIO_ISR1B,
1961920a61eSIlkka Koskinen 			.imr_offset	= REG_GPIO_IMR1B,
1971920a61eSIlkka Koskinen 		}, },
1981920a61eSIlkka Koskinen 	},
1991920a61eSIlkka Koskinen 	[1] = {
2001920a61eSIlkka Koskinen 		.name		= "keypad",
2011920a61eSIlkka Koskinen 		.set_cor	= true,
2021920a61eSIlkka Koskinen 		SIH_INITIALIZER(KEYPAD_KEYP, 4)
2031920a61eSIlkka Koskinen 	},
2041920a61eSIlkka Koskinen 	[2] = {
2051920a61eSIlkka Koskinen 		.name		= "bci",
2061920a61eSIlkka Koskinen 		.module		= TWL5031_MODULE_INTERRUPTS,
2071920a61eSIlkka Koskinen 		.control_offset	= TWL5031_INTERRUPTS_BCISIHCTRL,
2081920a61eSIlkka Koskinen 		.bits		= 7,
2091920a61eSIlkka Koskinen 		.bytes_ixr	= 1,
2101920a61eSIlkka Koskinen 		.edr_offset	= TWL5031_INTERRUPTS_BCIEDR1,
2111920a61eSIlkka Koskinen 		/* Note: most of these IRQs default to no-trigger */
2121920a61eSIlkka Koskinen 		.bytes_edr	= 2,
2131920a61eSIlkka Koskinen 		.irq_lines	= 2,
2141920a61eSIlkka Koskinen 		.mask = { {
2151920a61eSIlkka Koskinen 			.isr_offset	= TWL5031_INTERRUPTS_BCIISR1,
2161920a61eSIlkka Koskinen 			.imr_offset	= TWL5031_INTERRUPTS_BCIIMR1,
2171920a61eSIlkka Koskinen 		}, {
2181920a61eSIlkka Koskinen 			.isr_offset	= TWL5031_INTERRUPTS_BCIISR2,
2191920a61eSIlkka Koskinen 			.imr_offset	= TWL5031_INTERRUPTS_BCIIMR2,
2201920a61eSIlkka Koskinen 		}, },
2211920a61eSIlkka Koskinen 	},
2221920a61eSIlkka Koskinen 	[3] = {
2231920a61eSIlkka Koskinen 		.name		= "madc",
2241920a61eSIlkka Koskinen 		SIH_INITIALIZER(MADC, 4)
2251920a61eSIlkka Koskinen 	},
2261920a61eSIlkka Koskinen 	[4] = {
2271920a61eSIlkka Koskinen 		/* USB doesn't use the same SIH organization */
2281920a61eSIlkka Koskinen 		.name		= "usb",
2291920a61eSIlkka Koskinen 	},
2301920a61eSIlkka Koskinen 	[5] = {
2311920a61eSIlkka Koskinen 		.name		= "power",
2321920a61eSIlkka Koskinen 		.set_cor	= true,
2331920a61eSIlkka Koskinen 		SIH_INITIALIZER(INT_PWR, 8)
2341920a61eSIlkka Koskinen 	},
2351920a61eSIlkka Koskinen 	[6] = {
2361920a61eSIlkka Koskinen 		/*
237191211f5SIlkka Koskinen 		 * ECI/DBI doesn't use the same SIH organization.
238191211f5SIlkka Koskinen 		 * For example, it supports only one interrupt output line.
239191211f5SIlkka Koskinen 		 * That is, the interrupts are seen on both INT1 and INT2 lines.
2401920a61eSIlkka Koskinen 		 */
241191211f5SIlkka Koskinen 		.name		= "eci_dbi",
2421920a61eSIlkka Koskinen 		.module		= TWL5031_MODULE_ACCESSORY,
2431920a61eSIlkka Koskinen 		.bits		= 9,
2441920a61eSIlkka Koskinen 		.bytes_ixr	= 2,
2451920a61eSIlkka Koskinen 		.irq_lines	= 1,
2461920a61eSIlkka Koskinen 		.mask = { {
2471920a61eSIlkka Koskinen 			.isr_offset	= TWL5031_ACIIDR_LSB,
2481920a61eSIlkka Koskinen 			.imr_offset	= TWL5031_ACIIMR_LSB,
2491920a61eSIlkka Koskinen 		}, },
2501920a61eSIlkka Koskinen 
2511920a61eSIlkka Koskinen 	},
2521920a61eSIlkka Koskinen 	[7] = {
253191211f5SIlkka Koskinen 		/* Audio accessory */
254191211f5SIlkka Koskinen 		.name		= "audio",
2551920a61eSIlkka Koskinen 		.module		= TWL5031_MODULE_ACCESSORY,
2561920a61eSIlkka Koskinen 		.control_offset	= TWL5031_ACCSIHCTRL,
2571920a61eSIlkka Koskinen 		.bits		= 2,
2581920a61eSIlkka Koskinen 		.bytes_ixr	= 1,
2591920a61eSIlkka Koskinen 		.edr_offset	= TWL5031_ACCEDR1,
2601920a61eSIlkka Koskinen 		/* Note: most of these IRQs default to no-trigger */
2611920a61eSIlkka Koskinen 		.bytes_edr	= 1,
2621920a61eSIlkka Koskinen 		.irq_lines	= 2,
2631920a61eSIlkka Koskinen 		.mask = { {
2641920a61eSIlkka Koskinen 			.isr_offset	= TWL5031_ACCISR1,
2651920a61eSIlkka Koskinen 			.imr_offset	= TWL5031_ACCIMR1,
2661920a61eSIlkka Koskinen 		}, {
2671920a61eSIlkka Koskinen 			.isr_offset	= TWL5031_ACCISR2,
2681920a61eSIlkka Koskinen 			.imr_offset	= TWL5031_ACCIMR2,
2691920a61eSIlkka Koskinen 		}, },
2701920a61eSIlkka Koskinen 	},
2711920a61eSIlkka Koskinen };
2721920a61eSIlkka Koskinen 
273a30d46c0SDavid Brownell #undef TWL4030_MODULE_KEYPAD_KEYP
274a30d46c0SDavid Brownell #undef TWL4030_MODULE_INT_PWR
275a30d46c0SDavid Brownell #undef TWL4030_INT_PWR_EDR
276a30d46c0SDavid Brownell 
277a30d46c0SDavid Brownell /*----------------------------------------------------------------------*/
278a30d46c0SDavid Brownell 
279a30d46c0SDavid Brownell static unsigned twl4030_irq_base;
280a30d46c0SDavid Brownell 
281a30d46c0SDavid Brownell static struct completion irq_event;
282a30d46c0SDavid Brownell 
283a30d46c0SDavid Brownell /*
284a30d46c0SDavid Brownell  * This thread processes interrupts reported by the Primary Interrupt Handler.
285a30d46c0SDavid Brownell  */
286a30d46c0SDavid Brownell static int twl4030_irq_thread(void *data)
287a30d46c0SDavid Brownell {
288a30d46c0SDavid Brownell 	long irq = (long)data;
289a30d46c0SDavid Brownell 	static unsigned i2c_errors;
2903446d4bbSTobias Klauser 	static const unsigned max_i2c_errors = 100;
291a30d46c0SDavid Brownell 
29294964f96SSamuel Ortiz 
293a30d46c0SDavid Brownell 	current->flags |= PF_NOFREEZE;
294a30d46c0SDavid Brownell 
295a30d46c0SDavid Brownell 	while (!kthread_should_stop()) {
296a30d46c0SDavid Brownell 		int ret;
297a30d46c0SDavid Brownell 		int module_irq;
298a30d46c0SDavid Brownell 		u8 pih_isr;
299a30d46c0SDavid Brownell 
300a30d46c0SDavid Brownell 		/* Wait for IRQ, then read PIH irq status (also blocking) */
301a30d46c0SDavid Brownell 		wait_for_completion_interruptible(&irq_event);
302a30d46c0SDavid Brownell 
303fc7b92fcSBalaji T K 		ret = twl_i2c_read_u8(TWL4030_MODULE_PIH, &pih_isr,
304a30d46c0SDavid Brownell 					  REG_PIH_ISR_P1);
305a30d46c0SDavid Brownell 		if (ret) {
306a30d46c0SDavid Brownell 			pr_warning("twl4030: I2C error %d reading PIH ISR\n",
307a30d46c0SDavid Brownell 					ret);
308a30d46c0SDavid Brownell 			if (++i2c_errors >= max_i2c_errors) {
309a30d46c0SDavid Brownell 				printk(KERN_ERR "Maximum I2C error count"
310a30d46c0SDavid Brownell 						" exceeded.  Terminating %s.\n",
311a30d46c0SDavid Brownell 						__func__);
312a30d46c0SDavid Brownell 				break;
313a30d46c0SDavid Brownell 			}
314a30d46c0SDavid Brownell 			complete(&irq_event);
315a30d46c0SDavid Brownell 			continue;
316a30d46c0SDavid Brownell 		}
317a30d46c0SDavid Brownell 
318a30d46c0SDavid Brownell 		/* these handlers deal with the relevant SIH irq status */
319a30d46c0SDavid Brownell 		local_irq_disable();
320a30d46c0SDavid Brownell 		for (module_irq = twl4030_irq_base;
321a30d46c0SDavid Brownell 				pih_isr;
322a30d46c0SDavid Brownell 				pih_isr >>= 1, module_irq++) {
323a30d46c0SDavid Brownell 			if (pih_isr & 0x1) {
32494964f96SSamuel Ortiz 				struct irq_desc *d = irq_to_desc(module_irq);
32594964f96SSamuel Ortiz 
32694964f96SSamuel Ortiz 				if (!d) {
32794964f96SSamuel Ortiz 					pr_err("twl4030: Invalid SIH IRQ: %d\n",
32894964f96SSamuel Ortiz 					       module_irq);
32994964f96SSamuel Ortiz 					return -EINVAL;
33094964f96SSamuel Ortiz 				}
331a30d46c0SDavid Brownell 
332a30d46c0SDavid Brownell 				/* These can't be masked ... always warn
333a30d46c0SDavid Brownell 				 * if we get any surprises.
334a30d46c0SDavid Brownell 				 */
335a30d46c0SDavid Brownell 				if (d->status & IRQ_DISABLED)
336a30d46c0SDavid Brownell 					note_interrupt(module_irq, d,
337a30d46c0SDavid Brownell 							IRQ_NONE);
338a30d46c0SDavid Brownell 				else
339a30d46c0SDavid Brownell 					d->handle_irq(module_irq, d);
340a30d46c0SDavid Brownell 			}
341a30d46c0SDavid Brownell 		}
342a30d46c0SDavid Brownell 		local_irq_enable();
343a30d46c0SDavid Brownell 
3441cef8e41SRussell King 		enable_irq(irq);
345a30d46c0SDavid Brownell 	}
346a30d46c0SDavid Brownell 
347a30d46c0SDavid Brownell 	return 0;
348a30d46c0SDavid Brownell }
349a30d46c0SDavid Brownell 
350a30d46c0SDavid Brownell /*
351a30d46c0SDavid Brownell  * handle_twl4030_pih() is the desc->handle method for the twl4030 interrupt.
352a30d46c0SDavid Brownell  * This is a chained interrupt, so there is no desc->action method for it.
353a30d46c0SDavid Brownell  * Now we need to query the interrupt controller in the twl4030 to determine
354a30d46c0SDavid Brownell  * which module is generating the interrupt request.  However, we can't do i2c
355a30d46c0SDavid Brownell  * transactions in interrupt context, so we must defer that work to a kernel
356a30d46c0SDavid Brownell  * thread.  All we do here is acknowledge and mask the interrupt and wakeup
357a30d46c0SDavid Brownell  * the kernel thread.
358a30d46c0SDavid Brownell  */
3591cef8e41SRussell King static irqreturn_t handle_twl4030_pih(int irq, void *devid)
360a30d46c0SDavid Brownell {
361a30d46c0SDavid Brownell 	/* Acknowledge, clear *AND* mask the interrupt... */
3621cef8e41SRussell King 	disable_irq_nosync(irq);
3631cef8e41SRussell King 	complete(devid);
3641cef8e41SRussell King 	return IRQ_HANDLED;
365a30d46c0SDavid Brownell }
366a30d46c0SDavid Brownell /*----------------------------------------------------------------------*/
367a30d46c0SDavid Brownell 
368a30d46c0SDavid Brownell /*
369a30d46c0SDavid Brownell  * twl4030_init_sih_modules() ... start from a known state where no
370a30d46c0SDavid Brownell  * IRQs will be coming in, and where we can quickly enable them then
371a30d46c0SDavid Brownell  * handle them as they arrive.  Mask all IRQs: maybe init SIH_CTRL.
372a30d46c0SDavid Brownell  *
373a30d46c0SDavid Brownell  * NOTE:  we don't touch EDR registers here; they stay with hardware
374a30d46c0SDavid Brownell  * defaults or whatever the last value was.  Note that when both EDR
375a30d46c0SDavid Brownell  * bits for an IRQ are clear, that's as if its IMR bit is set...
376a30d46c0SDavid Brownell  */
377a30d46c0SDavid Brownell static int twl4030_init_sih_modules(unsigned line)
378a30d46c0SDavid Brownell {
379a30d46c0SDavid Brownell 	const struct sih *sih;
380a30d46c0SDavid Brownell 	u8 buf[4];
381a30d46c0SDavid Brownell 	int i;
382a30d46c0SDavid Brownell 	int status;
383a30d46c0SDavid Brownell 
384a30d46c0SDavid Brownell 	/* line 0 == int1_n signal; line 1 == int2_n signal */
385a30d46c0SDavid Brownell 	if (line > 1)
386a30d46c0SDavid Brownell 		return -EINVAL;
387a30d46c0SDavid Brownell 
388a30d46c0SDavid Brownell 	irq_line = line;
389a30d46c0SDavid Brownell 
390a30d46c0SDavid Brownell 	/* disable all interrupts on our line */
391a30d46c0SDavid Brownell 	memset(buf, 0xff, sizeof buf);
392a30d46c0SDavid Brownell 	sih = sih_modules;
3931920a61eSIlkka Koskinen 	for (i = 0; i < nr_sih_modules; i++, sih++) {
394a30d46c0SDavid Brownell 
395a30d46c0SDavid Brownell 		/* skip USB -- it's funky */
396a30d46c0SDavid Brownell 		if (!sih->bytes_ixr)
397a30d46c0SDavid Brownell 			continue;
398a30d46c0SDavid Brownell 
3991920a61eSIlkka Koskinen 		/* Not all the SIH modules support multiple interrupt lines */
4001920a61eSIlkka Koskinen 		if (sih->irq_lines <= line)
4011920a61eSIlkka Koskinen 			continue;
4021920a61eSIlkka Koskinen 
403fc7b92fcSBalaji T K 		status = twl_i2c_write(sih->module, buf,
404a30d46c0SDavid Brownell 				sih->mask[line].imr_offset, sih->bytes_ixr);
405a30d46c0SDavid Brownell 		if (status < 0)
406a30d46c0SDavid Brownell 			pr_err("twl4030: err %d initializing %s %s\n",
407a30d46c0SDavid Brownell 					status, sih->name, "IMR");
408a30d46c0SDavid Brownell 
409a30d46c0SDavid Brownell 		/* Maybe disable "exclusive" mode; buffer second pending irq;
410a30d46c0SDavid Brownell 		 * set Clear-On-Read (COR) bit.
411a30d46c0SDavid Brownell 		 *
412a30d46c0SDavid Brownell 		 * NOTE that sometimes COR polarity is documented as being
4138e52e279SGrazvydas Ignotas 		 * inverted:  for MADC, COR=1 means "clear on write".
414a30d46c0SDavid Brownell 		 * And for PWR_INT it's not documented...
415a30d46c0SDavid Brownell 		 */
416a30d46c0SDavid Brownell 		if (sih->set_cor) {
417fc7b92fcSBalaji T K 			status = twl_i2c_write_u8(sih->module,
418a30d46c0SDavid Brownell 					TWL4030_SIH_CTRL_COR_MASK,
419a30d46c0SDavid Brownell 					sih->control_offset);
420a30d46c0SDavid Brownell 			if (status < 0)
421a30d46c0SDavid Brownell 				pr_err("twl4030: err %d initializing %s %s\n",
422a30d46c0SDavid Brownell 						status, sih->name, "SIH_CTRL");
423a30d46c0SDavid Brownell 		}
424a30d46c0SDavid Brownell 	}
425a30d46c0SDavid Brownell 
426a30d46c0SDavid Brownell 	sih = sih_modules;
4271920a61eSIlkka Koskinen 	for (i = 0; i < nr_sih_modules; i++, sih++) {
428a30d46c0SDavid Brownell 		u8 rxbuf[4];
429a30d46c0SDavid Brownell 		int j;
430a30d46c0SDavid Brownell 
431a30d46c0SDavid Brownell 		/* skip USB */
432a30d46c0SDavid Brownell 		if (!sih->bytes_ixr)
433a30d46c0SDavid Brownell 			continue;
434a30d46c0SDavid Brownell 
4351920a61eSIlkka Koskinen 		/* Not all the SIH modules support multiple interrupt lines */
4361920a61eSIlkka Koskinen 		if (sih->irq_lines <= line)
4371920a61eSIlkka Koskinen 			continue;
4381920a61eSIlkka Koskinen 
439a30d46c0SDavid Brownell 		/* Clear pending interrupt status.  Either the read was
440a30d46c0SDavid Brownell 		 * enough, or we need to write those bits.  Repeat, in
441a30d46c0SDavid Brownell 		 * case an IRQ is pending (PENDDIS=0) ... that's not
442a30d46c0SDavid Brownell 		 * uncommon with PWR_INT.PWRON.
443a30d46c0SDavid Brownell 		 */
444a30d46c0SDavid Brownell 		for (j = 0; j < 2; j++) {
445fc7b92fcSBalaji T K 			status = twl_i2c_read(sih->module, rxbuf,
446a30d46c0SDavid Brownell 				sih->mask[line].isr_offset, sih->bytes_ixr);
447a30d46c0SDavid Brownell 			if (status < 0)
448a30d46c0SDavid Brownell 				pr_err("twl4030: err %d initializing %s %s\n",
449a30d46c0SDavid Brownell 					status, sih->name, "ISR");
450a30d46c0SDavid Brownell 
451a30d46c0SDavid Brownell 			if (!sih->set_cor)
452fc7b92fcSBalaji T K 				status = twl_i2c_write(sih->module, buf,
453a30d46c0SDavid Brownell 					sih->mask[line].isr_offset,
454a30d46c0SDavid Brownell 					sih->bytes_ixr);
455a30d46c0SDavid Brownell 			/* else COR=1 means read sufficed.
456a30d46c0SDavid Brownell 			 * (for most SIH modules...)
457a30d46c0SDavid Brownell 			 */
458a30d46c0SDavid Brownell 		}
459a30d46c0SDavid Brownell 	}
460a30d46c0SDavid Brownell 
461a30d46c0SDavid Brownell 	return 0;
462a30d46c0SDavid Brownell }
463a30d46c0SDavid Brownell 
464a30d46c0SDavid Brownell static inline void activate_irq(int irq)
465a30d46c0SDavid Brownell {
466a30d46c0SDavid Brownell #ifdef CONFIG_ARM
467a30d46c0SDavid Brownell 	/* ARM requires an extra step to clear IRQ_NOREQUEST, which it
468a30d46c0SDavid Brownell 	 * sets on behalf of every irq_chip.  Also sets IRQ_NOPROBE.
469a30d46c0SDavid Brownell 	 */
470a30d46c0SDavid Brownell 	set_irq_flags(irq, IRQF_VALID);
471a30d46c0SDavid Brownell #else
472a30d46c0SDavid Brownell 	/* same effect on other architectures */
473a30d46c0SDavid Brownell 	set_irq_noprobe(irq);
474a30d46c0SDavid Brownell #endif
475a30d46c0SDavid Brownell }
476a30d46c0SDavid Brownell 
477a30d46c0SDavid Brownell /*----------------------------------------------------------------------*/
478a30d46c0SDavid Brownell 
479a30d46c0SDavid Brownell static DEFINE_SPINLOCK(sih_agent_lock);
480a30d46c0SDavid Brownell 
481a30d46c0SDavid Brownell static struct workqueue_struct *wq;
482a30d46c0SDavid Brownell 
483a30d46c0SDavid Brownell struct sih_agent {
484a30d46c0SDavid Brownell 	int			irq_base;
485a30d46c0SDavid Brownell 	const struct sih	*sih;
486a30d46c0SDavid Brownell 
487a30d46c0SDavid Brownell 	u32			imr;
488a30d46c0SDavid Brownell 	bool			imr_change_pending;
489a30d46c0SDavid Brownell 	struct work_struct	mask_work;
490a30d46c0SDavid Brownell 
491a30d46c0SDavid Brownell 	u32			edge_change;
492a30d46c0SDavid Brownell 	struct work_struct	edge_work;
493a30d46c0SDavid Brownell };
494a30d46c0SDavid Brownell 
495a30d46c0SDavid Brownell static void twl4030_sih_do_mask(struct work_struct *work)
496a30d46c0SDavid Brownell {
497a30d46c0SDavid Brownell 	struct sih_agent	*agent;
498a30d46c0SDavid Brownell 	const struct sih	*sih;
499a30d46c0SDavid Brownell 	union {
500a30d46c0SDavid Brownell 		u8	bytes[4];
501a30d46c0SDavid Brownell 		u32	word;
502a30d46c0SDavid Brownell 	}			imr;
503a30d46c0SDavid Brownell 	int			status;
504a30d46c0SDavid Brownell 
505a30d46c0SDavid Brownell 	agent = container_of(work, struct sih_agent, mask_work);
506a30d46c0SDavid Brownell 
507a30d46c0SDavid Brownell 	/* see what work we have */
508a30d46c0SDavid Brownell 	spin_lock_irq(&sih_agent_lock);
509a30d46c0SDavid Brownell 	if (agent->imr_change_pending) {
510a30d46c0SDavid Brownell 		sih = agent->sih;
511a30d46c0SDavid Brownell 		/* byte[0] gets overwritten as we write ... */
512a30d46c0SDavid Brownell 		imr.word = cpu_to_le32(agent->imr << 8);
513a30d46c0SDavid Brownell 		agent->imr_change_pending = false;
514a30d46c0SDavid Brownell 	} else
515a30d46c0SDavid Brownell 		sih = NULL;
516a30d46c0SDavid Brownell 	spin_unlock_irq(&sih_agent_lock);
517a30d46c0SDavid Brownell 	if (!sih)
518a30d46c0SDavid Brownell 		return;
519a30d46c0SDavid Brownell 
520a30d46c0SDavid Brownell 	/* write the whole mask ... simpler than subsetting it */
521fc7b92fcSBalaji T K 	status = twl_i2c_write(sih->module, imr.bytes,
522a30d46c0SDavid Brownell 			sih->mask[irq_line].imr_offset, sih->bytes_ixr);
523a30d46c0SDavid Brownell 	if (status)
524a30d46c0SDavid Brownell 		pr_err("twl4030: %s, %s --> %d\n", __func__,
525a30d46c0SDavid Brownell 				"write", status);
526a30d46c0SDavid Brownell }
527a30d46c0SDavid Brownell 
528a30d46c0SDavid Brownell static void twl4030_sih_do_edge(struct work_struct *work)
529a30d46c0SDavid Brownell {
530a30d46c0SDavid Brownell 	struct sih_agent	*agent;
531a30d46c0SDavid Brownell 	const struct sih	*sih;
532a30d46c0SDavid Brownell 	u8			bytes[6];
533a30d46c0SDavid Brownell 	u32			edge_change;
534a30d46c0SDavid Brownell 	int			status;
535a30d46c0SDavid Brownell 
536a30d46c0SDavid Brownell 	agent = container_of(work, struct sih_agent, edge_work);
537a30d46c0SDavid Brownell 
538a30d46c0SDavid Brownell 	/* see what work we have */
539a30d46c0SDavid Brownell 	spin_lock_irq(&sih_agent_lock);
540a30d46c0SDavid Brownell 	edge_change = agent->edge_change;
541df10d646SJoe Perches 	agent->edge_change = 0;
542a30d46c0SDavid Brownell 	sih = edge_change ? agent->sih : NULL;
543a30d46c0SDavid Brownell 	spin_unlock_irq(&sih_agent_lock);
544a30d46c0SDavid Brownell 	if (!sih)
545a30d46c0SDavid Brownell 		return;
546a30d46c0SDavid Brownell 
547a30d46c0SDavid Brownell 	/* Read, reserving first byte for write scratch.  Yes, this
548a30d46c0SDavid Brownell 	 * could be cached for some speedup ... but be careful about
549a30d46c0SDavid Brownell 	 * any processor on the other IRQ line, EDR registers are
550a30d46c0SDavid Brownell 	 * shared.
551a30d46c0SDavid Brownell 	 */
552fc7b92fcSBalaji T K 	status = twl_i2c_read(sih->module, bytes + 1,
553a30d46c0SDavid Brownell 			sih->edr_offset, sih->bytes_edr);
554a30d46c0SDavid Brownell 	if (status) {
555a30d46c0SDavid Brownell 		pr_err("twl4030: %s, %s --> %d\n", __func__,
556a30d46c0SDavid Brownell 				"read", status);
557a30d46c0SDavid Brownell 		return;
558a30d46c0SDavid Brownell 	}
559a30d46c0SDavid Brownell 
560a30d46c0SDavid Brownell 	/* Modify only the bits we know must change */
561a30d46c0SDavid Brownell 	while (edge_change) {
562a30d46c0SDavid Brownell 		int		i = fls(edge_change) - 1;
56394964f96SSamuel Ortiz 		struct irq_desc	*d = irq_to_desc(i + agent->irq_base);
564a30d46c0SDavid Brownell 		int		byte = 1 + (i >> 2);
565a30d46c0SDavid Brownell 		int		off = (i & 0x3) * 2;
566a30d46c0SDavid Brownell 
56794964f96SSamuel Ortiz 		if (!d) {
56894964f96SSamuel Ortiz 			pr_err("twl4030: Invalid IRQ: %d\n",
56994964f96SSamuel Ortiz 			       i + agent->irq_base);
57094964f96SSamuel Ortiz 			return;
57194964f96SSamuel Ortiz 		}
57294964f96SSamuel Ortiz 
573a30d46c0SDavid Brownell 		bytes[byte] &= ~(0x03 << off);
574a30d46c0SDavid Brownell 
575cd6e125cSLinus Torvalds 		raw_spin_lock_irq(&d->lock);
576a30d46c0SDavid Brownell 		if (d->status & IRQ_TYPE_EDGE_RISING)
577a30d46c0SDavid Brownell 			bytes[byte] |= BIT(off + 1);
578a30d46c0SDavid Brownell 		if (d->status & IRQ_TYPE_EDGE_FALLING)
579a30d46c0SDavid Brownell 			bytes[byte] |= BIT(off + 0);
580cd6e125cSLinus Torvalds 		raw_spin_unlock_irq(&d->lock);
581a30d46c0SDavid Brownell 
582a30d46c0SDavid Brownell 		edge_change &= ~BIT(i);
583a30d46c0SDavid Brownell 	}
584a30d46c0SDavid Brownell 
585a30d46c0SDavid Brownell 	/* Write */
586fc7b92fcSBalaji T K 	status = twl_i2c_write(sih->module, bytes,
587a30d46c0SDavid Brownell 			sih->edr_offset, sih->bytes_edr);
588a30d46c0SDavid Brownell 	if (status)
589a30d46c0SDavid Brownell 		pr_err("twl4030: %s, %s --> %d\n", __func__,
590a30d46c0SDavid Brownell 				"write", status);
591a30d46c0SDavid Brownell }
592a30d46c0SDavid Brownell 
593a30d46c0SDavid Brownell /*----------------------------------------------------------------------*/
594a30d46c0SDavid Brownell 
595a30d46c0SDavid Brownell /*
596a30d46c0SDavid Brownell  * All irq_chip methods get issued from code holding irq_desc[irq].lock,
597a30d46c0SDavid Brownell  * which can't perform the underlying I2C operations (because they sleep).
598a30d46c0SDavid Brownell  * So we must hand them off to a thread (workqueue) and cope with asynch
599a30d46c0SDavid Brownell  * completion, potentially including some re-ordering, of these requests.
600a30d46c0SDavid Brownell  */
601a30d46c0SDavid Brownell 
602a30d46c0SDavid Brownell static void twl4030_sih_mask(unsigned irq)
603a30d46c0SDavid Brownell {
604a30d46c0SDavid Brownell 	struct sih_agent *sih = get_irq_chip_data(irq);
605a30d46c0SDavid Brownell 	unsigned long flags;
606a30d46c0SDavid Brownell 
607a30d46c0SDavid Brownell 	spin_lock_irqsave(&sih_agent_lock, flags);
608a30d46c0SDavid Brownell 	sih->imr |= BIT(irq - sih->irq_base);
609a30d46c0SDavid Brownell 	sih->imr_change_pending = true;
610a30d46c0SDavid Brownell 	queue_work(wq, &sih->mask_work);
611a30d46c0SDavid Brownell 	spin_unlock_irqrestore(&sih_agent_lock, flags);
612a30d46c0SDavid Brownell }
613a30d46c0SDavid Brownell 
614a30d46c0SDavid Brownell static void twl4030_sih_unmask(unsigned irq)
615a30d46c0SDavid Brownell {
616a30d46c0SDavid Brownell 	struct sih_agent *sih = get_irq_chip_data(irq);
617a30d46c0SDavid Brownell 	unsigned long flags;
618a30d46c0SDavid Brownell 
619a30d46c0SDavid Brownell 	spin_lock_irqsave(&sih_agent_lock, flags);
620a30d46c0SDavid Brownell 	sih->imr &= ~BIT(irq - sih->irq_base);
621a30d46c0SDavid Brownell 	sih->imr_change_pending = true;
622a30d46c0SDavid Brownell 	queue_work(wq, &sih->mask_work);
623a30d46c0SDavid Brownell 	spin_unlock_irqrestore(&sih_agent_lock, flags);
624a30d46c0SDavid Brownell }
625a30d46c0SDavid Brownell 
626a30d46c0SDavid Brownell static int twl4030_sih_set_type(unsigned irq, unsigned trigger)
627a30d46c0SDavid Brownell {
628a30d46c0SDavid Brownell 	struct sih_agent *sih = get_irq_chip_data(irq);
62994964f96SSamuel Ortiz 	struct irq_desc *desc = irq_to_desc(irq);
630a30d46c0SDavid Brownell 	unsigned long flags;
631a30d46c0SDavid Brownell 
63294964f96SSamuel Ortiz 	if (!desc) {
63394964f96SSamuel Ortiz 		pr_err("twl4030: Invalid IRQ: %d\n", irq);
63494964f96SSamuel Ortiz 		return -EINVAL;
63594964f96SSamuel Ortiz 	}
63694964f96SSamuel Ortiz 
637a30d46c0SDavid Brownell 	if (trigger & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
638a30d46c0SDavid Brownell 		return -EINVAL;
639a30d46c0SDavid Brownell 
640a30d46c0SDavid Brownell 	spin_lock_irqsave(&sih_agent_lock, flags);
641a30d46c0SDavid Brownell 	if ((desc->status & IRQ_TYPE_SENSE_MASK) != trigger) {
642a30d46c0SDavid Brownell 		desc->status &= ~IRQ_TYPE_SENSE_MASK;
643a30d46c0SDavid Brownell 		desc->status |= trigger;
644a30d46c0SDavid Brownell 		sih->edge_change |= BIT(irq - sih->irq_base);
645a30d46c0SDavid Brownell 		queue_work(wq, &sih->edge_work);
646a30d46c0SDavid Brownell 	}
647a30d46c0SDavid Brownell 	spin_unlock_irqrestore(&sih_agent_lock, flags);
648a30d46c0SDavid Brownell 	return 0;
649a30d46c0SDavid Brownell }
650a30d46c0SDavid Brownell 
651a30d46c0SDavid Brownell static struct irq_chip twl4030_sih_irq_chip = {
652a30d46c0SDavid Brownell 	.name		= "twl4030",
653a30d46c0SDavid Brownell 	.mask		= twl4030_sih_mask,
654a30d46c0SDavid Brownell 	.unmask		= twl4030_sih_unmask,
655a30d46c0SDavid Brownell 	.set_type	= twl4030_sih_set_type,
656a30d46c0SDavid Brownell };
657a30d46c0SDavid Brownell 
658a30d46c0SDavid Brownell /*----------------------------------------------------------------------*/
659a30d46c0SDavid Brownell 
660a30d46c0SDavid Brownell static inline int sih_read_isr(const struct sih *sih)
661a30d46c0SDavid Brownell {
662a30d46c0SDavid Brownell 	int status;
663a30d46c0SDavid Brownell 	union {
664a30d46c0SDavid Brownell 		u8 bytes[4];
665a30d46c0SDavid Brownell 		u32 word;
666a30d46c0SDavid Brownell 	} isr;
667a30d46c0SDavid Brownell 
668a30d46c0SDavid Brownell 	/* FIXME need retry-on-error ... */
669a30d46c0SDavid Brownell 
670a30d46c0SDavid Brownell 	isr.word = 0;
671fc7b92fcSBalaji T K 	status = twl_i2c_read(sih->module, isr.bytes,
672a30d46c0SDavid Brownell 			sih->mask[irq_line].isr_offset, sih->bytes_ixr);
673a30d46c0SDavid Brownell 
674a30d46c0SDavid Brownell 	return (status < 0) ? status : le32_to_cpu(isr.word);
675a30d46c0SDavid Brownell }
676a30d46c0SDavid Brownell 
677a30d46c0SDavid Brownell /*
678a30d46c0SDavid Brownell  * Generic handler for SIH interrupts ... we "know" this is called
679a30d46c0SDavid Brownell  * in task context, with IRQs enabled.
680a30d46c0SDavid Brownell  */
681a30d46c0SDavid Brownell static void handle_twl4030_sih(unsigned irq, struct irq_desc *desc)
682a30d46c0SDavid Brownell {
683a30d46c0SDavid Brownell 	struct sih_agent *agent = get_irq_data(irq);
684a30d46c0SDavid Brownell 	const struct sih *sih = agent->sih;
685a30d46c0SDavid Brownell 	int isr;
686a30d46c0SDavid Brownell 
687a30d46c0SDavid Brownell 	/* reading ISR acks the IRQs, using clear-on-read mode */
688a30d46c0SDavid Brownell 	local_irq_enable();
689a30d46c0SDavid Brownell 	isr = sih_read_isr(sih);
690a30d46c0SDavid Brownell 	local_irq_disable();
691a30d46c0SDavid Brownell 
692a30d46c0SDavid Brownell 	if (isr < 0) {
693a30d46c0SDavid Brownell 		pr_err("twl4030: %s SIH, read ISR error %d\n",
694a30d46c0SDavid Brownell 			sih->name, isr);
695a30d46c0SDavid Brownell 		/* REVISIT:  recover; eventually mask it all, etc */
696a30d46c0SDavid Brownell 		return;
697a30d46c0SDavid Brownell 	}
698a30d46c0SDavid Brownell 
699a30d46c0SDavid Brownell 	while (isr) {
700a30d46c0SDavid Brownell 		irq = fls(isr);
701a30d46c0SDavid Brownell 		irq--;
702a30d46c0SDavid Brownell 		isr &= ~BIT(irq);
703a30d46c0SDavid Brownell 
704a30d46c0SDavid Brownell 		if (irq < sih->bits)
705a30d46c0SDavid Brownell 			generic_handle_irq(agent->irq_base + irq);
706a30d46c0SDavid Brownell 		else
707a30d46c0SDavid Brownell 			pr_err("twl4030: %s SIH, invalid ISR bit %d\n",
708a30d46c0SDavid Brownell 				sih->name, irq);
709a30d46c0SDavid Brownell 	}
710a30d46c0SDavid Brownell }
711a30d46c0SDavid Brownell 
712a30d46c0SDavid Brownell static unsigned twl4030_irq_next;
713a30d46c0SDavid Brownell 
714a30d46c0SDavid Brownell /* returns the first IRQ used by this SIH bank,
715a30d46c0SDavid Brownell  * or negative errno
716a30d46c0SDavid Brownell  */
717a30d46c0SDavid Brownell int twl4030_sih_setup(int module)
718a30d46c0SDavid Brownell {
719a30d46c0SDavid Brownell 	int			sih_mod;
720a30d46c0SDavid Brownell 	const struct sih	*sih = NULL;
721a30d46c0SDavid Brownell 	struct sih_agent	*agent;
722a30d46c0SDavid Brownell 	int			i, irq;
723a30d46c0SDavid Brownell 	int			status = -EINVAL;
724a30d46c0SDavid Brownell 	unsigned		irq_base = twl4030_irq_next;
725a30d46c0SDavid Brownell 
726a30d46c0SDavid Brownell 	/* only support modules with standard clear-on-read for now */
727a30d46c0SDavid Brownell 	for (sih_mod = 0, sih = sih_modules;
7281920a61eSIlkka Koskinen 			sih_mod < nr_sih_modules;
729a30d46c0SDavid Brownell 			sih_mod++, sih++) {
730a30d46c0SDavid Brownell 		if (sih->module == module && sih->set_cor) {
731a30d46c0SDavid Brownell 			if (!WARN((irq_base + sih->bits) > NR_IRQS,
732a30d46c0SDavid Brownell 					"irq %d for %s too big\n",
733a30d46c0SDavid Brownell 					irq_base + sih->bits,
734a30d46c0SDavid Brownell 					sih->name))
735a30d46c0SDavid Brownell 				status = 0;
736a30d46c0SDavid Brownell 			break;
737a30d46c0SDavid Brownell 		}
738a30d46c0SDavid Brownell 	}
739a30d46c0SDavid Brownell 	if (status < 0)
740a30d46c0SDavid Brownell 		return status;
741a30d46c0SDavid Brownell 
742a30d46c0SDavid Brownell 	agent = kzalloc(sizeof *agent, GFP_KERNEL);
743a30d46c0SDavid Brownell 	if (!agent)
744a30d46c0SDavid Brownell 		return -ENOMEM;
745a30d46c0SDavid Brownell 
746a30d46c0SDavid Brownell 	status = 0;
747a30d46c0SDavid Brownell 
748a30d46c0SDavid Brownell 	agent->irq_base = irq_base;
749a30d46c0SDavid Brownell 	agent->sih = sih;
750a30d46c0SDavid Brownell 	agent->imr = ~0;
751a30d46c0SDavid Brownell 	INIT_WORK(&agent->mask_work, twl4030_sih_do_mask);
752a30d46c0SDavid Brownell 	INIT_WORK(&agent->edge_work, twl4030_sih_do_edge);
753a30d46c0SDavid Brownell 
754a30d46c0SDavid Brownell 	for (i = 0; i < sih->bits; i++) {
755a30d46c0SDavid Brownell 		irq = irq_base + i;
756a30d46c0SDavid Brownell 
757a30d46c0SDavid Brownell 		set_irq_chip_and_handler(irq, &twl4030_sih_irq_chip,
758a30d46c0SDavid Brownell 				handle_edge_irq);
759a30d46c0SDavid Brownell 		set_irq_chip_data(irq, agent);
760a30d46c0SDavid Brownell 		activate_irq(irq);
761a30d46c0SDavid Brownell 	}
762a30d46c0SDavid Brownell 
763a30d46c0SDavid Brownell 	status = irq_base;
764a30d46c0SDavid Brownell 	twl4030_irq_next += i;
765a30d46c0SDavid Brownell 
766a30d46c0SDavid Brownell 	/* replace generic PIH handler (handle_simple_irq) */
767a30d46c0SDavid Brownell 	irq = sih_mod + twl4030_irq_base;
768a30d46c0SDavid Brownell 	set_irq_data(irq, agent);
769a30d46c0SDavid Brownell 	set_irq_chained_handler(irq, handle_twl4030_sih);
770a30d46c0SDavid Brownell 
771a30d46c0SDavid Brownell 	pr_info("twl4030: %s (irq %d) chaining IRQs %d..%d\n", sih->name,
772a30d46c0SDavid Brownell 			irq, irq_base, twl4030_irq_next - 1);
773a30d46c0SDavid Brownell 
774a30d46c0SDavid Brownell 	return status;
775a30d46c0SDavid Brownell }
776a30d46c0SDavid Brownell 
777a30d46c0SDavid Brownell /* FIXME need a call to reverse twl4030_sih_setup() ... */
778a30d46c0SDavid Brownell 
779a30d46c0SDavid Brownell 
780a30d46c0SDavid Brownell /*----------------------------------------------------------------------*/
781a30d46c0SDavid Brownell 
782a30d46c0SDavid Brownell /* FIXME pass in which interrupt line we'll use ... */
783a30d46c0SDavid Brownell #define twl_irq_line	0
784a30d46c0SDavid Brownell 
785e8deb28cSBalaji T K int twl4030_init_irq(int irq_num, unsigned irq_base, unsigned irq_end)
786a30d46c0SDavid Brownell {
787a30d46c0SDavid Brownell 	static struct irq_chip	twl4030_irq_chip;
788a30d46c0SDavid Brownell 
789a30d46c0SDavid Brownell 	int			status;
790a30d46c0SDavid Brownell 	int			i;
791a30d46c0SDavid Brownell 	struct task_struct	*task;
792a30d46c0SDavid Brownell 
793a30d46c0SDavid Brownell 	/*
794a30d46c0SDavid Brownell 	 * Mask and clear all TWL4030 interrupts since initially we do
795a30d46c0SDavid Brownell 	 * not have any TWL4030 module interrupt handlers present
796a30d46c0SDavid Brownell 	 */
797a30d46c0SDavid Brownell 	status = twl4030_init_sih_modules(twl_irq_line);
798a30d46c0SDavid Brownell 	if (status < 0)
799a30d46c0SDavid Brownell 		return status;
800a30d46c0SDavid Brownell 
801a30d46c0SDavid Brownell 	wq = create_singlethread_workqueue("twl4030-irqchip");
802a30d46c0SDavid Brownell 	if (!wq) {
803a30d46c0SDavid Brownell 		pr_err("twl4030: workqueue FAIL\n");
804a30d46c0SDavid Brownell 		return -ESRCH;
805a30d46c0SDavid Brownell 	}
806a30d46c0SDavid Brownell 
807a30d46c0SDavid Brownell 	twl4030_irq_base = irq_base;
808a30d46c0SDavid Brownell 
809a30d46c0SDavid Brownell 	/* install an irq handler for each of the SIH modules;
810a30d46c0SDavid Brownell 	 * clone dummy irq_chip since PIH can't *do* anything
811a30d46c0SDavid Brownell 	 */
812a30d46c0SDavid Brownell 	twl4030_irq_chip = dummy_irq_chip;
813a30d46c0SDavid Brownell 	twl4030_irq_chip.name = "twl4030";
814a30d46c0SDavid Brownell 
815fe212213SThomas Gleixner 	twl4030_sih_irq_chip.irq_ack = dummy_irq_chip.irq_ack;
816a30d46c0SDavid Brownell 
817a30d46c0SDavid Brownell 	for (i = irq_base; i < irq_end; i++) {
818a30d46c0SDavid Brownell 		set_irq_chip_and_handler(i, &twl4030_irq_chip,
819a30d46c0SDavid Brownell 				handle_simple_irq);
820a30d46c0SDavid Brownell 		activate_irq(i);
821a30d46c0SDavid Brownell 	}
822a30d46c0SDavid Brownell 	twl4030_irq_next = i;
823a30d46c0SDavid Brownell 	pr_info("twl4030: %s (irq %d) chaining IRQs %d..%d\n", "PIH",
824a30d46c0SDavid Brownell 			irq_num, irq_base, twl4030_irq_next - 1);
825a30d46c0SDavid Brownell 
826a30d46c0SDavid Brownell 	/* ... and the PWR_INT module ... */
827a30d46c0SDavid Brownell 	status = twl4030_sih_setup(TWL4030_MODULE_INT);
828a30d46c0SDavid Brownell 	if (status < 0) {
829a30d46c0SDavid Brownell 		pr_err("twl4030: sih_setup PWR INT --> %d\n", status);
830a30d46c0SDavid Brownell 		goto fail;
831a30d46c0SDavid Brownell 	}
832a30d46c0SDavid Brownell 
833a30d46c0SDavid Brownell 	/* install an irq handler to demultiplex the TWL4030 interrupt */
8341cef8e41SRussell King 
8351cef8e41SRussell King 
8361cef8e41SRussell King 	init_completion(&irq_event);
8371cef8e41SRussell King 
8381cef8e41SRussell King 	status = request_irq(irq_num, handle_twl4030_pih, IRQF_DISABLED,
8391cef8e41SRussell King 				"TWL4030-PIH", &irq_event);
8401cef8e41SRussell King 	if (status < 0) {
8411cef8e41SRussell King 		pr_err("twl4030: could not claim irq%d: %d\n", irq_num, status);
8421cef8e41SRussell King 		goto fail_rqirq;
843a30d46c0SDavid Brownell 	}
844a30d46c0SDavid Brownell 
84589f5f9f7SAlan Cox 	task = kthread_run(twl4030_irq_thread, (void *)(long)irq_num,
84689f5f9f7SAlan Cox 								"twl4030-irq");
8471cef8e41SRussell King 	if (IS_ERR(task)) {
8481cef8e41SRussell King 		pr_err("twl4030: could not create irq %d thread!\n", irq_num);
8491cef8e41SRussell King 		status = PTR_ERR(task);
8501cef8e41SRussell King 		goto fail_kthread;
8511cef8e41SRussell King 	}
852a30d46c0SDavid Brownell 	return status;
8531cef8e41SRussell King fail_kthread:
8541cef8e41SRussell King 	free_irq(irq_num, &irq_event);
8551cef8e41SRussell King fail_rqirq:
8561cef8e41SRussell King 	/* clean up twl4030_sih_setup */
857a30d46c0SDavid Brownell fail:
858a30d46c0SDavid Brownell 	for (i = irq_base; i < irq_end; i++)
859a30d46c0SDavid Brownell 		set_irq_chip_and_handler(i, NULL, NULL);
860a30d46c0SDavid Brownell 	destroy_workqueue(wq);
861a30d46c0SDavid Brownell 	wq = NULL;
862a30d46c0SDavid Brownell 	return status;
863a30d46c0SDavid Brownell }
864a30d46c0SDavid Brownell 
865e8deb28cSBalaji T K int twl4030_exit_irq(void)
866a30d46c0SDavid Brownell {
867a30d46c0SDavid Brownell 	/* FIXME undo twl_init_irq() */
868a30d46c0SDavid Brownell 	if (twl4030_irq_base) {
869a30d46c0SDavid Brownell 		pr_err("twl4030: can't yet clean up IRQs?\n");
870a30d46c0SDavid Brownell 		return -ENOSYS;
871a30d46c0SDavid Brownell 	}
872a30d46c0SDavid Brownell 	return 0;
873a30d46c0SDavid Brownell }
8741920a61eSIlkka Koskinen 
875e8deb28cSBalaji T K int twl4030_init_chip_irq(const char *chip)
8761920a61eSIlkka Koskinen {
8771920a61eSIlkka Koskinen 	if (!strcmp(chip, "twl5031")) {
8781920a61eSIlkka Koskinen 		sih_modules = sih_modules_twl5031;
8791920a61eSIlkka Koskinen 		nr_sih_modules = ARRAY_SIZE(sih_modules_twl5031);
8801920a61eSIlkka Koskinen 	} else {
8811920a61eSIlkka Koskinen 		sih_modules = sih_modules_twl4030;
8821920a61eSIlkka Koskinen 		nr_sih_modules = ARRAY_SIZE(sih_modules_twl4030);
8831920a61eSIlkka Koskinen 	}
8841920a61eSIlkka Koskinen 
8851920a61eSIlkka Koskinen 	return 0;
8861920a61eSIlkka Koskinen }
887