xref: /linux/drivers/mfd/twl4030-irq.c (revision 5a2f1b5fae593dbdf4f3656ee5a5d111df3e9acb)
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>
3178518ffaSBenoit Cousson #include <linux/export.h>
32a30d46c0SDavid Brownell #include <linux/interrupt.h>
33a30d46c0SDavid Brownell #include <linux/irq.h>
345a0e3ad6STejun Heo #include <linux/slab.h>
3578518ffaSBenoit Cousson #include <linux/of.h>
3678518ffaSBenoit Cousson #include <linux/irqdomain.h>
37b07682b6SSantosh Shilimkar #include <linux/i2c/twl.h>
38a30d46c0SDavid Brownell 
39b0b4a7c2SG, Manjunath Kondaiah #include "twl-core.h"
40a30d46c0SDavid Brownell 
41a30d46c0SDavid Brownell /*
42a30d46c0SDavid Brownell  * TWL4030 IRQ handling has two stages in hardware, and thus in software.
43a30d46c0SDavid Brownell  * The Primary Interrupt Handler (PIH) stage exposes status bits saying
44a30d46c0SDavid Brownell  * which Secondary Interrupt Handler (SIH) stage is raising an interrupt.
45a30d46c0SDavid Brownell  * SIH modules are more traditional IRQ components, which support per-IRQ
46a30d46c0SDavid Brownell  * enable/disable and trigger controls; they do most of the work.
47a30d46c0SDavid Brownell  *
48a30d46c0SDavid Brownell  * These chips are designed to support IRQ handling from two different
49a30d46c0SDavid Brownell  * I2C masters.  Each has a dedicated IRQ line, and dedicated IRQ status
50a30d46c0SDavid Brownell  * and mask registers in the PIH and SIH modules.
51a30d46c0SDavid Brownell  *
52a30d46c0SDavid Brownell  * We set up IRQs starting at a platform-specified base, always starting
53a30d46c0SDavid Brownell  * with PIH and the SIH for PWR_INT and then usually adding GPIO:
54a30d46c0SDavid Brownell  *	base + 0  .. base + 7	PIH
55a30d46c0SDavid Brownell  *	base + 8  .. base + 15	SIH for PWR_INT
56a30d46c0SDavid Brownell  *	base + 16 .. base + 33	SIH for GPIO
57a30d46c0SDavid Brownell  */
5878518ffaSBenoit Cousson #define TWL4030_CORE_NR_IRQS	8
5978518ffaSBenoit Cousson #define TWL4030_PWR_NR_IRQS	8
60a30d46c0SDavid Brownell 
61a30d46c0SDavid Brownell /* PIH register offsets */
62a30d46c0SDavid Brownell #define REG_PIH_ISR_P1			0x01
63a30d46c0SDavid Brownell #define REG_PIH_ISR_P2			0x02
64a30d46c0SDavid Brownell #define REG_PIH_SIR			0x03	/* for testing */
65a30d46c0SDavid Brownell 
66a30d46c0SDavid Brownell /* Linux could (eventually) use either IRQ line */
67a30d46c0SDavid Brownell static int irq_line;
68a30d46c0SDavid Brownell 
69a30d46c0SDavid Brownell struct sih {
70a30d46c0SDavid Brownell 	char	name[8];
71a30d46c0SDavid Brownell 	u8	module;			/* module id */
72a30d46c0SDavid Brownell 	u8	control_offset;		/* for SIH_CTRL */
73a30d46c0SDavid Brownell 	bool	set_cor;
74a30d46c0SDavid Brownell 
75a30d46c0SDavid Brownell 	u8	bits;			/* valid in isr/imr */
76a30d46c0SDavid Brownell 	u8	bytes_ixr;		/* bytelen of ISR/IMR/SIR */
77a30d46c0SDavid Brownell 
78a30d46c0SDavid Brownell 	u8	edr_offset;
79a30d46c0SDavid Brownell 	u8	bytes_edr;		/* bytelen of EDR */
80a30d46c0SDavid Brownell 
811920a61eSIlkka Koskinen 	u8	irq_lines;		/* number of supported irq lines */
821920a61eSIlkka Koskinen 
83a30d46c0SDavid Brownell 	/* SIR ignored -- set interrupt, for testing only */
8435a27e8eSThomas Gleixner 	struct sih_irq_data {
85a30d46c0SDavid Brownell 		u8	isr_offset;
86a30d46c0SDavid Brownell 		u8	imr_offset;
87a30d46c0SDavid Brownell 	} mask[2];
88a30d46c0SDavid Brownell 	/* + 2 bytes padding */
89a30d46c0SDavid Brownell };
90a30d46c0SDavid Brownell 
911920a61eSIlkka Koskinen static const struct sih *sih_modules;
921920a61eSIlkka Koskinen static int nr_sih_modules;
931920a61eSIlkka Koskinen 
94a30d46c0SDavid Brownell #define SIH_INITIALIZER(modname, nbits) \
95a30d46c0SDavid Brownell 	.module		= TWL4030_MODULE_ ## modname, \
96a30d46c0SDavid Brownell 	.control_offset = TWL4030_ ## modname ## _SIH_CTRL, \
97a30d46c0SDavid Brownell 	.bits		= nbits, \
98a30d46c0SDavid Brownell 	.bytes_ixr	= DIV_ROUND_UP(nbits, 8), \
99a30d46c0SDavid Brownell 	.edr_offset	= TWL4030_ ## modname ## _EDR, \
100a30d46c0SDavid Brownell 	.bytes_edr	= DIV_ROUND_UP((2*(nbits)), 8), \
1011920a61eSIlkka Koskinen 	.irq_lines	= 2, \
102a30d46c0SDavid Brownell 	.mask = { { \
103a30d46c0SDavid Brownell 		.isr_offset	= TWL4030_ ## modname ## _ISR1, \
104a30d46c0SDavid Brownell 		.imr_offset	= TWL4030_ ## modname ## _IMR1, \
105a30d46c0SDavid Brownell 	}, \
106a30d46c0SDavid Brownell 	{ \
107a30d46c0SDavid Brownell 		.isr_offset	= TWL4030_ ## modname ## _ISR2, \
108a30d46c0SDavid Brownell 		.imr_offset	= TWL4030_ ## modname ## _IMR2, \
109a30d46c0SDavid Brownell 	}, },
110a30d46c0SDavid Brownell 
111a30d46c0SDavid Brownell /* register naming policies are inconsistent ... */
112a30d46c0SDavid Brownell #define TWL4030_INT_PWR_EDR		TWL4030_INT_PWR_EDR1
113a30d46c0SDavid Brownell #define TWL4030_MODULE_KEYPAD_KEYP	TWL4030_MODULE_KEYPAD
114a30d46c0SDavid Brownell #define TWL4030_MODULE_INT_PWR		TWL4030_MODULE_INT
115a30d46c0SDavid Brownell 
116a30d46c0SDavid Brownell 
117cbcde05eSFelipe Contreras /*
118cbcde05eSFelipe Contreras  * Order in this table matches order in PIH_ISR.  That is,
119a30d46c0SDavid Brownell  * BIT(n) in PIH_ISR is sih_modules[n].
120a30d46c0SDavid Brownell  */
1211920a61eSIlkka Koskinen /* sih_modules_twl4030 is used both in twl4030 and twl5030 */
1221920a61eSIlkka Koskinen static const struct sih sih_modules_twl4030[6] = {
123a30d46c0SDavid Brownell 	[0] = {
124a30d46c0SDavid Brownell 		.name		= "gpio",
125a30d46c0SDavid Brownell 		.module		= TWL4030_MODULE_GPIO,
126a30d46c0SDavid Brownell 		.control_offset	= REG_GPIO_SIH_CTRL,
127a30d46c0SDavid Brownell 		.set_cor	= true,
128a30d46c0SDavid Brownell 		.bits		= TWL4030_GPIO_MAX,
129a30d46c0SDavid Brownell 		.bytes_ixr	= 3,
130a30d46c0SDavid Brownell 		/* Note: *all* of these IRQs default to no-trigger */
131a30d46c0SDavid Brownell 		.edr_offset	= REG_GPIO_EDR1,
132a30d46c0SDavid Brownell 		.bytes_edr	= 5,
1331920a61eSIlkka Koskinen 		.irq_lines	= 2,
134a30d46c0SDavid Brownell 		.mask = { {
135a30d46c0SDavid Brownell 			.isr_offset	= REG_GPIO_ISR1A,
136a30d46c0SDavid Brownell 			.imr_offset	= REG_GPIO_IMR1A,
137a30d46c0SDavid Brownell 		}, {
138a30d46c0SDavid Brownell 			.isr_offset	= REG_GPIO_ISR1B,
139a30d46c0SDavid Brownell 			.imr_offset	= REG_GPIO_IMR1B,
140a30d46c0SDavid Brownell 		}, },
141a30d46c0SDavid Brownell 	},
142a30d46c0SDavid Brownell 	[1] = {
143a30d46c0SDavid Brownell 		.name		= "keypad",
144a30d46c0SDavid Brownell 		.set_cor	= true,
145a30d46c0SDavid Brownell 		SIH_INITIALIZER(KEYPAD_KEYP, 4)
146a30d46c0SDavid Brownell 	},
147a30d46c0SDavid Brownell 	[2] = {
148a30d46c0SDavid Brownell 		.name		= "bci",
149a30d46c0SDavid Brownell 		.module		= TWL4030_MODULE_INTERRUPTS,
150a30d46c0SDavid Brownell 		.control_offset	= TWL4030_INTERRUPTS_BCISIHCTRL,
1518e52e279SGrazvydas Ignotas 		.set_cor	= true,
152a30d46c0SDavid Brownell 		.bits		= 12,
153a30d46c0SDavid Brownell 		.bytes_ixr	= 2,
154a30d46c0SDavid Brownell 		.edr_offset	= TWL4030_INTERRUPTS_BCIEDR1,
155a30d46c0SDavid Brownell 		/* Note: most of these IRQs default to no-trigger */
156a30d46c0SDavid Brownell 		.bytes_edr	= 3,
1571920a61eSIlkka Koskinen 		.irq_lines	= 2,
158a30d46c0SDavid Brownell 		.mask = { {
159a30d46c0SDavid Brownell 			.isr_offset	= TWL4030_INTERRUPTS_BCIISR1A,
160a30d46c0SDavid Brownell 			.imr_offset	= TWL4030_INTERRUPTS_BCIIMR1A,
161a30d46c0SDavid Brownell 		}, {
162a30d46c0SDavid Brownell 			.isr_offset	= TWL4030_INTERRUPTS_BCIISR1B,
163a30d46c0SDavid Brownell 			.imr_offset	= TWL4030_INTERRUPTS_BCIIMR1B,
164a30d46c0SDavid Brownell 		}, },
165a30d46c0SDavid Brownell 	},
166a30d46c0SDavid Brownell 	[3] = {
167a30d46c0SDavid Brownell 		.name		= "madc",
168a30d46c0SDavid Brownell 		SIH_INITIALIZER(MADC, 4)
169a30d46c0SDavid Brownell 	},
170a30d46c0SDavid Brownell 	[4] = {
171a30d46c0SDavid Brownell 		/* USB doesn't use the same SIH organization */
172a30d46c0SDavid Brownell 		.name		= "usb",
173a30d46c0SDavid Brownell 	},
174a30d46c0SDavid Brownell 	[5] = {
175a30d46c0SDavid Brownell 		.name		= "power",
176a30d46c0SDavid Brownell 		.set_cor	= true,
177a30d46c0SDavid Brownell 		SIH_INITIALIZER(INT_PWR, 8)
178a30d46c0SDavid Brownell 	},
179a30d46c0SDavid Brownell 		/* there are no SIH modules #6 or #7 ... */
180a30d46c0SDavid Brownell };
181a30d46c0SDavid Brownell 
1821920a61eSIlkka Koskinen static const struct sih sih_modules_twl5031[8] = {
1831920a61eSIlkka Koskinen 	[0] = {
1841920a61eSIlkka Koskinen 		.name		= "gpio",
1851920a61eSIlkka Koskinen 		.module		= TWL4030_MODULE_GPIO,
1861920a61eSIlkka Koskinen 		.control_offset	= REG_GPIO_SIH_CTRL,
1871920a61eSIlkka Koskinen 		.set_cor	= true,
1881920a61eSIlkka Koskinen 		.bits		= TWL4030_GPIO_MAX,
1891920a61eSIlkka Koskinen 		.bytes_ixr	= 3,
1901920a61eSIlkka Koskinen 		/* Note: *all* of these IRQs default to no-trigger */
1911920a61eSIlkka Koskinen 		.edr_offset	= REG_GPIO_EDR1,
1921920a61eSIlkka Koskinen 		.bytes_edr	= 5,
1931920a61eSIlkka Koskinen 		.irq_lines	= 2,
1941920a61eSIlkka Koskinen 		.mask = { {
1951920a61eSIlkka Koskinen 			.isr_offset	= REG_GPIO_ISR1A,
1961920a61eSIlkka Koskinen 			.imr_offset	= REG_GPIO_IMR1A,
1971920a61eSIlkka Koskinen 		}, {
1981920a61eSIlkka Koskinen 			.isr_offset	= REG_GPIO_ISR1B,
1991920a61eSIlkka Koskinen 			.imr_offset	= REG_GPIO_IMR1B,
2001920a61eSIlkka Koskinen 		}, },
2011920a61eSIlkka Koskinen 	},
2021920a61eSIlkka Koskinen 	[1] = {
2031920a61eSIlkka Koskinen 		.name		= "keypad",
2041920a61eSIlkka Koskinen 		.set_cor	= true,
2051920a61eSIlkka Koskinen 		SIH_INITIALIZER(KEYPAD_KEYP, 4)
2061920a61eSIlkka Koskinen 	},
2071920a61eSIlkka Koskinen 	[2] = {
2081920a61eSIlkka Koskinen 		.name		= "bci",
2091920a61eSIlkka Koskinen 		.module		= TWL5031_MODULE_INTERRUPTS,
2101920a61eSIlkka Koskinen 		.control_offset	= TWL5031_INTERRUPTS_BCISIHCTRL,
2111920a61eSIlkka Koskinen 		.bits		= 7,
2121920a61eSIlkka Koskinen 		.bytes_ixr	= 1,
2131920a61eSIlkka Koskinen 		.edr_offset	= TWL5031_INTERRUPTS_BCIEDR1,
2141920a61eSIlkka Koskinen 		/* Note: most of these IRQs default to no-trigger */
2151920a61eSIlkka Koskinen 		.bytes_edr	= 2,
2161920a61eSIlkka Koskinen 		.irq_lines	= 2,
2171920a61eSIlkka Koskinen 		.mask = { {
2181920a61eSIlkka Koskinen 			.isr_offset	= TWL5031_INTERRUPTS_BCIISR1,
2191920a61eSIlkka Koskinen 			.imr_offset	= TWL5031_INTERRUPTS_BCIIMR1,
2201920a61eSIlkka Koskinen 		}, {
2211920a61eSIlkka Koskinen 			.isr_offset	= TWL5031_INTERRUPTS_BCIISR2,
2221920a61eSIlkka Koskinen 			.imr_offset	= TWL5031_INTERRUPTS_BCIIMR2,
2231920a61eSIlkka Koskinen 		}, },
2241920a61eSIlkka Koskinen 	},
2251920a61eSIlkka Koskinen 	[3] = {
2261920a61eSIlkka Koskinen 		.name		= "madc",
2271920a61eSIlkka Koskinen 		SIH_INITIALIZER(MADC, 4)
2281920a61eSIlkka Koskinen 	},
2291920a61eSIlkka Koskinen 	[4] = {
2301920a61eSIlkka Koskinen 		/* USB doesn't use the same SIH organization */
2311920a61eSIlkka Koskinen 		.name		= "usb",
2321920a61eSIlkka Koskinen 	},
2331920a61eSIlkka Koskinen 	[5] = {
2341920a61eSIlkka Koskinen 		.name		= "power",
2351920a61eSIlkka Koskinen 		.set_cor	= true,
2361920a61eSIlkka Koskinen 		SIH_INITIALIZER(INT_PWR, 8)
2371920a61eSIlkka Koskinen 	},
2381920a61eSIlkka Koskinen 	[6] = {
2391920a61eSIlkka Koskinen 		/*
240191211f5SIlkka Koskinen 		 * ECI/DBI doesn't use the same SIH organization.
241191211f5SIlkka Koskinen 		 * For example, it supports only one interrupt output line.
242191211f5SIlkka Koskinen 		 * That is, the interrupts are seen on both INT1 and INT2 lines.
2431920a61eSIlkka Koskinen 		 */
244191211f5SIlkka Koskinen 		.name		= "eci_dbi",
2451920a61eSIlkka Koskinen 		.module		= TWL5031_MODULE_ACCESSORY,
2461920a61eSIlkka Koskinen 		.bits		= 9,
2471920a61eSIlkka Koskinen 		.bytes_ixr	= 2,
2481920a61eSIlkka Koskinen 		.irq_lines	= 1,
2491920a61eSIlkka Koskinen 		.mask = { {
2501920a61eSIlkka Koskinen 			.isr_offset	= TWL5031_ACIIDR_LSB,
2511920a61eSIlkka Koskinen 			.imr_offset	= TWL5031_ACIIMR_LSB,
2521920a61eSIlkka Koskinen 		}, },
2531920a61eSIlkka Koskinen 
2541920a61eSIlkka Koskinen 	},
2551920a61eSIlkka Koskinen 	[7] = {
256191211f5SIlkka Koskinen 		/* Audio accessory */
257191211f5SIlkka Koskinen 		.name		= "audio",
2581920a61eSIlkka Koskinen 		.module		= TWL5031_MODULE_ACCESSORY,
2591920a61eSIlkka Koskinen 		.control_offset	= TWL5031_ACCSIHCTRL,
2601920a61eSIlkka Koskinen 		.bits		= 2,
2611920a61eSIlkka Koskinen 		.bytes_ixr	= 1,
2621920a61eSIlkka Koskinen 		.edr_offset	= TWL5031_ACCEDR1,
2631920a61eSIlkka Koskinen 		/* Note: most of these IRQs default to no-trigger */
2641920a61eSIlkka Koskinen 		.bytes_edr	= 1,
2651920a61eSIlkka Koskinen 		.irq_lines	= 2,
2661920a61eSIlkka Koskinen 		.mask = { {
2671920a61eSIlkka Koskinen 			.isr_offset	= TWL5031_ACCISR1,
2681920a61eSIlkka Koskinen 			.imr_offset	= TWL5031_ACCIMR1,
2691920a61eSIlkka Koskinen 		}, {
2701920a61eSIlkka Koskinen 			.isr_offset	= TWL5031_ACCISR2,
2711920a61eSIlkka Koskinen 			.imr_offset	= TWL5031_ACCIMR2,
2721920a61eSIlkka Koskinen 		}, },
2731920a61eSIlkka Koskinen 	},
2741920a61eSIlkka Koskinen };
2751920a61eSIlkka Koskinen 
276a30d46c0SDavid Brownell #undef TWL4030_MODULE_KEYPAD_KEYP
277a30d46c0SDavid Brownell #undef TWL4030_MODULE_INT_PWR
278a30d46c0SDavid Brownell #undef TWL4030_INT_PWR_EDR
279a30d46c0SDavid Brownell 
280a30d46c0SDavid Brownell /*----------------------------------------------------------------------*/
281a30d46c0SDavid Brownell 
282a30d46c0SDavid Brownell static unsigned twl4030_irq_base;
283a30d46c0SDavid Brownell 
284a30d46c0SDavid Brownell /*
285a30d46c0SDavid Brownell  * handle_twl4030_pih() is the desc->handle method for the twl4030 interrupt.
286a30d46c0SDavid Brownell  * This is a chained interrupt, so there is no desc->action method for it.
287a30d46c0SDavid Brownell  * Now we need to query the interrupt controller in the twl4030 to determine
288a30d46c0SDavid Brownell  * which module is generating the interrupt request.  However, we can't do i2c
289a30d46c0SDavid Brownell  * transactions in interrupt context, so we must defer that work to a kernel
290a30d46c0SDavid Brownell  * thread.  All we do here is acknowledge and mask the interrupt and wakeup
291a30d46c0SDavid Brownell  * the kernel thread.
292a30d46c0SDavid Brownell  */
2931cef8e41SRussell King static irqreturn_t handle_twl4030_pih(int irq, void *devid)
294a30d46c0SDavid Brownell {
2957750c9b0SFelipe Balbi 	irqreturn_t	ret;
2967750c9b0SFelipe Balbi 	u8		pih_isr;
2977750c9b0SFelipe Balbi 
2987750c9b0SFelipe Balbi 	ret = twl_i2c_read_u8(TWL4030_MODULE_PIH, &pih_isr,
2997750c9b0SFelipe Balbi 			REG_PIH_ISR_P1);
3007750c9b0SFelipe Balbi 	if (ret) {
3017750c9b0SFelipe Balbi 		pr_warning("twl4030: I2C error %d reading PIH ISR\n", ret);
3027750c9b0SFelipe Balbi 		return IRQ_NONE;
3037750c9b0SFelipe Balbi 	}
3047750c9b0SFelipe Balbi 
3055a903090SFelipe Balbi 	while (pih_isr) {
3065a903090SFelipe Balbi 		unsigned long	pending = __ffs(pih_isr);
3075a903090SFelipe Balbi 		unsigned int	irq;
3085a903090SFelipe Balbi 
3095a903090SFelipe Balbi 		pih_isr &= ~BIT(pending);
3105a903090SFelipe Balbi 		irq = pending + twl4030_irq_base;
3115a903090SFelipe Balbi 		handle_nested_irq(irq);
3127750c9b0SFelipe Balbi 	}
3137750c9b0SFelipe Balbi 
3141cef8e41SRussell King 	return IRQ_HANDLED;
315a30d46c0SDavid Brownell }
316cbcde05eSFelipe Contreras 
317a30d46c0SDavid Brownell /*----------------------------------------------------------------------*/
318a30d46c0SDavid Brownell 
319a30d46c0SDavid Brownell /*
320a30d46c0SDavid Brownell  * twl4030_init_sih_modules() ... start from a known state where no
321a30d46c0SDavid Brownell  * IRQs will be coming in, and where we can quickly enable them then
322a30d46c0SDavid Brownell  * handle them as they arrive.  Mask all IRQs: maybe init SIH_CTRL.
323a30d46c0SDavid Brownell  *
324a30d46c0SDavid Brownell  * NOTE:  we don't touch EDR registers here; they stay with hardware
325a30d46c0SDavid Brownell  * defaults or whatever the last value was.  Note that when both EDR
326a30d46c0SDavid Brownell  * bits for an IRQ are clear, that's as if its IMR bit is set...
327a30d46c0SDavid Brownell  */
328a30d46c0SDavid Brownell static int twl4030_init_sih_modules(unsigned line)
329a30d46c0SDavid Brownell {
330a30d46c0SDavid Brownell 	const struct sih *sih;
331a30d46c0SDavid Brownell 	u8 buf[4];
332a30d46c0SDavid Brownell 	int i;
333a30d46c0SDavid Brownell 	int status;
334a30d46c0SDavid Brownell 
335a30d46c0SDavid Brownell 	/* line 0 == int1_n signal; line 1 == int2_n signal */
336a30d46c0SDavid Brownell 	if (line > 1)
337a30d46c0SDavid Brownell 		return -EINVAL;
338a30d46c0SDavid Brownell 
339a30d46c0SDavid Brownell 	irq_line = line;
340a30d46c0SDavid Brownell 
341a30d46c0SDavid Brownell 	/* disable all interrupts on our line */
342a30d46c0SDavid Brownell 	memset(buf, 0xff, sizeof buf);
343a30d46c0SDavid Brownell 	sih = sih_modules;
3441920a61eSIlkka Koskinen 	for (i = 0; i < nr_sih_modules; i++, sih++) {
345a30d46c0SDavid Brownell 		/* skip USB -- it's funky */
346a30d46c0SDavid Brownell 		if (!sih->bytes_ixr)
347a30d46c0SDavid Brownell 			continue;
348a30d46c0SDavid Brownell 
3491920a61eSIlkka Koskinen 		/* Not all the SIH modules support multiple interrupt lines */
3501920a61eSIlkka Koskinen 		if (sih->irq_lines <= line)
3511920a61eSIlkka Koskinen 			continue;
3521920a61eSIlkka Koskinen 
353fc7b92fcSBalaji T K 		status = twl_i2c_write(sih->module, buf,
354a30d46c0SDavid Brownell 				sih->mask[line].imr_offset, sih->bytes_ixr);
355a30d46c0SDavid Brownell 		if (status < 0)
356a30d46c0SDavid Brownell 			pr_err("twl4030: err %d initializing %s %s\n",
357a30d46c0SDavid Brownell 					status, sih->name, "IMR");
358a30d46c0SDavid Brownell 
359cbcde05eSFelipe Contreras 		/*
360cbcde05eSFelipe Contreras 		 * Maybe disable "exclusive" mode; buffer second pending irq;
361a30d46c0SDavid Brownell 		 * set Clear-On-Read (COR) bit.
362a30d46c0SDavid Brownell 		 *
363a30d46c0SDavid Brownell 		 * NOTE that sometimes COR polarity is documented as being
3648e52e279SGrazvydas Ignotas 		 * inverted:  for MADC, COR=1 means "clear on write".
365a30d46c0SDavid Brownell 		 * And for PWR_INT it's not documented...
366a30d46c0SDavid Brownell 		 */
367a30d46c0SDavid Brownell 		if (sih->set_cor) {
368fc7b92fcSBalaji T K 			status = twl_i2c_write_u8(sih->module,
369a30d46c0SDavid Brownell 					TWL4030_SIH_CTRL_COR_MASK,
370a30d46c0SDavid Brownell 					sih->control_offset);
371a30d46c0SDavid Brownell 			if (status < 0)
372a30d46c0SDavid Brownell 				pr_err("twl4030: err %d initializing %s %s\n",
373a30d46c0SDavid Brownell 						status, sih->name, "SIH_CTRL");
374a30d46c0SDavid Brownell 		}
375a30d46c0SDavid Brownell 	}
376a30d46c0SDavid Brownell 
377a30d46c0SDavid Brownell 	sih = sih_modules;
3781920a61eSIlkka Koskinen 	for (i = 0; i < nr_sih_modules; i++, sih++) {
379a30d46c0SDavid Brownell 		u8 rxbuf[4];
380a30d46c0SDavid Brownell 		int j;
381a30d46c0SDavid Brownell 
382a30d46c0SDavid Brownell 		/* skip USB */
383a30d46c0SDavid Brownell 		if (!sih->bytes_ixr)
384a30d46c0SDavid Brownell 			continue;
385a30d46c0SDavid Brownell 
3861920a61eSIlkka Koskinen 		/* Not all the SIH modules support multiple interrupt lines */
3871920a61eSIlkka Koskinen 		if (sih->irq_lines <= line)
3881920a61eSIlkka Koskinen 			continue;
3891920a61eSIlkka Koskinen 
390cbcde05eSFelipe Contreras 		/*
391cbcde05eSFelipe Contreras 		 * Clear pending interrupt status.  Either the read was
392a30d46c0SDavid Brownell 		 * enough, or we need to write those bits.  Repeat, in
393a30d46c0SDavid Brownell 		 * case an IRQ is pending (PENDDIS=0) ... that's not
394a30d46c0SDavid Brownell 		 * uncommon with PWR_INT.PWRON.
395a30d46c0SDavid Brownell 		 */
396a30d46c0SDavid Brownell 		for (j = 0; j < 2; j++) {
397fc7b92fcSBalaji T K 			status = twl_i2c_read(sih->module, rxbuf,
398a30d46c0SDavid Brownell 				sih->mask[line].isr_offset, sih->bytes_ixr);
399a30d46c0SDavid Brownell 			if (status < 0)
400a30d46c0SDavid Brownell 				pr_err("twl4030: err %d initializing %s %s\n",
401a30d46c0SDavid Brownell 					status, sih->name, "ISR");
402a30d46c0SDavid Brownell 
403a30d46c0SDavid Brownell 			if (!sih->set_cor)
404fc7b92fcSBalaji T K 				status = twl_i2c_write(sih->module, buf,
405a30d46c0SDavid Brownell 					sih->mask[line].isr_offset,
406a30d46c0SDavid Brownell 					sih->bytes_ixr);
407cbcde05eSFelipe Contreras 			/*
408cbcde05eSFelipe Contreras 			 * else COR=1 means read sufficed.
409a30d46c0SDavid Brownell 			 * (for most SIH modules...)
410a30d46c0SDavid Brownell 			 */
411a30d46c0SDavid Brownell 		}
412a30d46c0SDavid Brownell 	}
413a30d46c0SDavid Brownell 
414a30d46c0SDavid Brownell 	return 0;
415a30d46c0SDavid Brownell }
416a30d46c0SDavid Brownell 
417a30d46c0SDavid Brownell static inline void activate_irq(int irq)
418a30d46c0SDavid Brownell {
419a30d46c0SDavid Brownell #ifdef CONFIG_ARM
420cbcde05eSFelipe Contreras 	/*
421cbcde05eSFelipe Contreras 	 * ARM requires an extra step to clear IRQ_NOREQUEST, which it
422a30d46c0SDavid Brownell 	 * sets on behalf of every irq_chip.  Also sets IRQ_NOPROBE.
423a30d46c0SDavid Brownell 	 */
424a30d46c0SDavid Brownell 	set_irq_flags(irq, IRQF_VALID);
425a30d46c0SDavid Brownell #else
426a30d46c0SDavid Brownell 	/* same effect on other architectures */
427d5bb1221SThomas Gleixner 	irq_set_noprobe(irq);
428a30d46c0SDavid Brownell #endif
429a30d46c0SDavid Brownell }
430a30d46c0SDavid Brownell 
431a30d46c0SDavid Brownell /*----------------------------------------------------------------------*/
432a30d46c0SDavid Brownell 
433a30d46c0SDavid Brownell struct sih_agent {
434a30d46c0SDavid Brownell 	int			irq_base;
435a30d46c0SDavid Brownell 	const struct sih	*sih;
436a30d46c0SDavid Brownell 
437a30d46c0SDavid Brownell 	u32			imr;
438a30d46c0SDavid Brownell 	bool			imr_change_pending;
439a30d46c0SDavid Brownell 
440a30d46c0SDavid Brownell 	u32			edge_change;
44191e3569fSFelipe Balbi 
44291e3569fSFelipe Balbi 	struct mutex		irq_lock;
443c1e61bcfSNeilBrown 	char			*irq_name;
444a30d46c0SDavid Brownell };
445a30d46c0SDavid Brownell 
446a30d46c0SDavid Brownell /*----------------------------------------------------------------------*/
447a30d46c0SDavid Brownell 
448a30d46c0SDavid Brownell /*
449a30d46c0SDavid Brownell  * All irq_chip methods get issued from code holding irq_desc[irq].lock,
450a30d46c0SDavid Brownell  * which can't perform the underlying I2C operations (because they sleep).
451a30d46c0SDavid Brownell  * So we must hand them off to a thread (workqueue) and cope with asynch
452a30d46c0SDavid Brownell  * completion, potentially including some re-ordering, of these requests.
453a30d46c0SDavid Brownell  */
454a30d46c0SDavid Brownell 
455845aeab5SMark Brown static void twl4030_sih_mask(struct irq_data *data)
456a30d46c0SDavid Brownell {
45784868424SFelipe Balbi 	struct sih_agent *agent = irq_data_get_irq_chip_data(data);
458a30d46c0SDavid Brownell 
45984868424SFelipe Balbi 	agent->imr |= BIT(data->irq - agent->irq_base);
46084868424SFelipe Balbi 	agent->imr_change_pending = true;
461a30d46c0SDavid Brownell }
462a30d46c0SDavid Brownell 
463845aeab5SMark Brown static void twl4030_sih_unmask(struct irq_data *data)
464a30d46c0SDavid Brownell {
46584868424SFelipe Balbi 	struct sih_agent *agent = irq_data_get_irq_chip_data(data);
466a30d46c0SDavid Brownell 
46784868424SFelipe Balbi 	agent->imr &= ~BIT(data->irq - agent->irq_base);
46884868424SFelipe Balbi 	agent->imr_change_pending = true;
469a30d46c0SDavid Brownell }
470a30d46c0SDavid Brownell 
471845aeab5SMark Brown static int twl4030_sih_set_type(struct irq_data *data, unsigned trigger)
472a30d46c0SDavid Brownell {
47384868424SFelipe Balbi 	struct sih_agent *agent = irq_data_get_irq_chip_data(data);
474a30d46c0SDavid Brownell 
475a30d46c0SDavid Brownell 	if (trigger & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
476a30d46c0SDavid Brownell 		return -EINVAL;
477a30d46c0SDavid Brownell 
4782f2a7d5eSFelipe Balbi 	if (irqd_get_trigger_type(data) != trigger)
47984868424SFelipe Balbi 		agent->edge_change |= BIT(data->irq - agent->irq_base);
48091e3569fSFelipe Balbi 
481a30d46c0SDavid Brownell 	return 0;
482a30d46c0SDavid Brownell }
483a30d46c0SDavid Brownell 
48491e3569fSFelipe Balbi static void twl4030_sih_bus_lock(struct irq_data *data)
48591e3569fSFelipe Balbi {
48684868424SFelipe Balbi 	struct sih_agent	*agent = irq_data_get_irq_chip_data(data);
48791e3569fSFelipe Balbi 
48884868424SFelipe Balbi 	mutex_lock(&agent->irq_lock);
48991e3569fSFelipe Balbi }
49091e3569fSFelipe Balbi 
49191e3569fSFelipe Balbi static void twl4030_sih_bus_sync_unlock(struct irq_data *data)
49291e3569fSFelipe Balbi {
49384868424SFelipe Balbi 	struct sih_agent	*agent = irq_data_get_irq_chip_data(data);
49484868424SFelipe Balbi 	const struct sih	*sih = agent->sih;
49584868424SFelipe Balbi 	int			status;
49691e3569fSFelipe Balbi 
49784868424SFelipe Balbi 	if (agent->imr_change_pending) {
49884868424SFelipe Balbi 		union {
49984868424SFelipe Balbi 			u32	word;
50084868424SFelipe Balbi 			u8	bytes[4];
50184868424SFelipe Balbi 		} imr;
50284868424SFelipe Balbi 
503c9531227SNeilBrown 		/* byte[0] gets overwritten as we write ... */
50484868424SFelipe Balbi 		imr.word = cpu_to_le32(agent->imr << 8);
50584868424SFelipe Balbi 		agent->imr_change_pending = false;
50684868424SFelipe Balbi 
50784868424SFelipe Balbi 		/* write the whole mask ... simpler than subsetting it */
50884868424SFelipe Balbi 		status = twl_i2c_write(sih->module, imr.bytes,
50984868424SFelipe Balbi 				sih->mask[irq_line].imr_offset,
51084868424SFelipe Balbi 				sih->bytes_ixr);
51184868424SFelipe Balbi 		if (status)
51284868424SFelipe Balbi 			pr_err("twl4030: %s, %s --> %d\n", __func__,
51384868424SFelipe Balbi 					"write", status);
51484868424SFelipe Balbi 	}
51584868424SFelipe Balbi 
5162f2a7d5eSFelipe Balbi 	if (agent->edge_change) {
5172f2a7d5eSFelipe Balbi 		u32		edge_change;
5182f2a7d5eSFelipe Balbi 		u8		bytes[6];
5192f2a7d5eSFelipe Balbi 
5202f2a7d5eSFelipe Balbi 		edge_change = agent->edge_change;
5212f2a7d5eSFelipe Balbi 		agent->edge_change = 0;
5222f2a7d5eSFelipe Balbi 
5232f2a7d5eSFelipe Balbi 		/*
5242f2a7d5eSFelipe Balbi 		 * Read, reserving first byte for write scratch.  Yes, this
5252f2a7d5eSFelipe Balbi 		 * could be cached for some speedup ... but be careful about
5262f2a7d5eSFelipe Balbi 		 * any processor on the other IRQ line, EDR registers are
5272f2a7d5eSFelipe Balbi 		 * shared.
5282f2a7d5eSFelipe Balbi 		 */
5292f2a7d5eSFelipe Balbi 		status = twl_i2c_read(sih->module, bytes + 1,
5302f2a7d5eSFelipe Balbi 				sih->edr_offset, sih->bytes_edr);
5312f2a7d5eSFelipe Balbi 		if (status) {
5322f2a7d5eSFelipe Balbi 			pr_err("twl4030: %s, %s --> %d\n", __func__,
5332f2a7d5eSFelipe Balbi 					"read", status);
5342f2a7d5eSFelipe Balbi 			return;
5352f2a7d5eSFelipe Balbi 		}
5362f2a7d5eSFelipe Balbi 
5372f2a7d5eSFelipe Balbi 		/* Modify only the bits we know must change */
5382f2a7d5eSFelipe Balbi 		while (edge_change) {
5392f2a7d5eSFelipe Balbi 			int		i = fls(edge_change) - 1;
5402f2a7d5eSFelipe Balbi 			struct irq_data	*idata;
5412f2a7d5eSFelipe Balbi 			int		byte = 1 + (i >> 2);
5422f2a7d5eSFelipe Balbi 			int		off = (i & 0x3) * 2;
5432f2a7d5eSFelipe Balbi 			unsigned int	type;
5442f2a7d5eSFelipe Balbi 
5452f2a7d5eSFelipe Balbi 			idata = irq_get_irq_data(i + agent->irq_base);
5462f2a7d5eSFelipe Balbi 
5472f2a7d5eSFelipe Balbi 			bytes[byte] &= ~(0x03 << off);
5482f2a7d5eSFelipe Balbi 
5492f2a7d5eSFelipe Balbi 			type = irqd_get_trigger_type(idata);
5502f2a7d5eSFelipe Balbi 			if (type & IRQ_TYPE_EDGE_RISING)
5512f2a7d5eSFelipe Balbi 				bytes[byte] |= BIT(off + 1);
5522f2a7d5eSFelipe Balbi 			if (type & IRQ_TYPE_EDGE_FALLING)
5532f2a7d5eSFelipe Balbi 				bytes[byte] |= BIT(off + 0);
5542f2a7d5eSFelipe Balbi 
5552f2a7d5eSFelipe Balbi 			edge_change &= ~BIT(i);
5562f2a7d5eSFelipe Balbi 		}
5572f2a7d5eSFelipe Balbi 
5582f2a7d5eSFelipe Balbi 		/* Write */
5592f2a7d5eSFelipe Balbi 		status = twl_i2c_write(sih->module, bytes,
5602f2a7d5eSFelipe Balbi 				sih->edr_offset, sih->bytes_edr);
5612f2a7d5eSFelipe Balbi 		if (status)
5622f2a7d5eSFelipe Balbi 			pr_err("twl4030: %s, %s --> %d\n", __func__,
5632f2a7d5eSFelipe Balbi 					"write", status);
5642f2a7d5eSFelipe Balbi 	}
5652f2a7d5eSFelipe Balbi 
56684868424SFelipe Balbi 	mutex_unlock(&agent->irq_lock);
56791e3569fSFelipe Balbi }
56891e3569fSFelipe Balbi 
569a30d46c0SDavid Brownell static struct irq_chip twl4030_sih_irq_chip = {
570a30d46c0SDavid Brownell 	.name		= "twl4030",
571845aeab5SMark Brown 	.irq_mask	= twl4030_sih_mask,
572845aeab5SMark Brown 	.irq_unmask	= twl4030_sih_unmask,
573845aeab5SMark Brown 	.irq_set_type	= twl4030_sih_set_type,
57491e3569fSFelipe Balbi 	.irq_bus_lock	= twl4030_sih_bus_lock,
57591e3569fSFelipe Balbi 	.irq_bus_sync_unlock = twl4030_sih_bus_sync_unlock,
576a30d46c0SDavid Brownell };
577a30d46c0SDavid Brownell 
578a30d46c0SDavid Brownell /*----------------------------------------------------------------------*/
579a30d46c0SDavid Brownell 
580a30d46c0SDavid Brownell static inline int sih_read_isr(const struct sih *sih)
581a30d46c0SDavid Brownell {
582a30d46c0SDavid Brownell 	int status;
583a30d46c0SDavid Brownell 	union {
584a30d46c0SDavid Brownell 		u8 bytes[4];
585a30d46c0SDavid Brownell 		u32 word;
586a30d46c0SDavid Brownell 	} isr;
587a30d46c0SDavid Brownell 
588a30d46c0SDavid Brownell 	/* FIXME need retry-on-error ... */
589a30d46c0SDavid Brownell 
590a30d46c0SDavid Brownell 	isr.word = 0;
591fc7b92fcSBalaji T K 	status = twl_i2c_read(sih->module, isr.bytes,
592a30d46c0SDavid Brownell 			sih->mask[irq_line].isr_offset, sih->bytes_ixr);
593a30d46c0SDavid Brownell 
594a30d46c0SDavid Brownell 	return (status < 0) ? status : le32_to_cpu(isr.word);
595a30d46c0SDavid Brownell }
596a30d46c0SDavid Brownell 
597a30d46c0SDavid Brownell /*
598a30d46c0SDavid Brownell  * Generic handler for SIH interrupts ... we "know" this is called
599a30d46c0SDavid Brownell  * in task context, with IRQs enabled.
600a30d46c0SDavid Brownell  */
601c1e61bcfSNeilBrown static irqreturn_t handle_twl4030_sih(int irq, void *data)
602a30d46c0SDavid Brownell {
603d5bb1221SThomas Gleixner 	struct sih_agent *agent = irq_get_handler_data(irq);
604a30d46c0SDavid Brownell 	const struct sih *sih = agent->sih;
605a30d46c0SDavid Brownell 	int isr;
606a30d46c0SDavid Brownell 
607a30d46c0SDavid Brownell 	/* reading ISR acks the IRQs, using clear-on-read mode */
608a30d46c0SDavid Brownell 	isr = sih_read_isr(sih);
609a30d46c0SDavid Brownell 
610a30d46c0SDavid Brownell 	if (isr < 0) {
611a30d46c0SDavid Brownell 		pr_err("twl4030: %s SIH, read ISR error %d\n",
612a30d46c0SDavid Brownell 			sih->name, isr);
613a30d46c0SDavid Brownell 		/* REVISIT:  recover; eventually mask it all, etc */
614c1e61bcfSNeilBrown 		return IRQ_HANDLED;
615a30d46c0SDavid Brownell 	}
616a30d46c0SDavid Brownell 
617a30d46c0SDavid Brownell 	while (isr) {
618a30d46c0SDavid Brownell 		irq = fls(isr);
619a30d46c0SDavid Brownell 		irq--;
620a30d46c0SDavid Brownell 		isr &= ~BIT(irq);
621a30d46c0SDavid Brownell 
622a30d46c0SDavid Brownell 		if (irq < sih->bits)
623925e853cSFelipe Balbi 			handle_nested_irq(agent->irq_base + irq);
624a30d46c0SDavid Brownell 		else
625a30d46c0SDavid Brownell 			pr_err("twl4030: %s SIH, invalid ISR bit %d\n",
626a30d46c0SDavid Brownell 				sih->name, irq);
627a30d46c0SDavid Brownell 	}
628c1e61bcfSNeilBrown 	return IRQ_HANDLED;
629a30d46c0SDavid Brownell }
630a30d46c0SDavid Brownell 
631cbcde05eSFelipe Contreras /* returns the first IRQ used by this SIH bank, or negative errno */
632f01b1f90SBenoit Cousson int twl4030_sih_setup(struct device *dev, int module, int irq_base)
633a30d46c0SDavid Brownell {
634a30d46c0SDavid Brownell 	int			sih_mod;
635a30d46c0SDavid Brownell 	const struct sih	*sih = NULL;
636a30d46c0SDavid Brownell 	struct sih_agent	*agent;
637a30d46c0SDavid Brownell 	int			i, irq;
638a30d46c0SDavid Brownell 	int			status = -EINVAL;
639a30d46c0SDavid Brownell 
640a30d46c0SDavid Brownell 	/* only support modules with standard clear-on-read for now */
641ec1a07b3SBenoit Cousson 	for (sih_mod = 0, sih = sih_modules; sih_mod < nr_sih_modules;
642a30d46c0SDavid Brownell 			sih_mod++, sih++) {
643a30d46c0SDavid Brownell 		if (sih->module == module && sih->set_cor) {
644a30d46c0SDavid Brownell 			status = 0;
645a30d46c0SDavid Brownell 			break;
646a30d46c0SDavid Brownell 		}
647a30d46c0SDavid Brownell 	}
648ec1a07b3SBenoit Cousson 
649a30d46c0SDavid Brownell 	if (status < 0)
650a30d46c0SDavid Brownell 		return status;
651a30d46c0SDavid Brownell 
652a30d46c0SDavid Brownell 	agent = kzalloc(sizeof *agent, GFP_KERNEL);
653a30d46c0SDavid Brownell 	if (!agent)
654a30d46c0SDavid Brownell 		return -ENOMEM;
655a30d46c0SDavid Brownell 
656a30d46c0SDavid Brownell 	agent->irq_base = irq_base;
657a30d46c0SDavid Brownell 	agent->sih = sih;
658a30d46c0SDavid Brownell 	agent->imr = ~0;
65991e3569fSFelipe Balbi 	mutex_init(&agent->irq_lock);
660a30d46c0SDavid Brownell 
661a30d46c0SDavid Brownell 	for (i = 0; i < sih->bits; i++) {
662a30d46c0SDavid Brownell 		irq = irq_base + i;
663a30d46c0SDavid Brownell 
66491e3569fSFelipe Balbi 		irq_set_chip_data(irq, agent);
665d5bb1221SThomas Gleixner 		irq_set_chip_and_handler(irq, &twl4030_sih_irq_chip,
666a30d46c0SDavid Brownell 					 handle_edge_irq);
667b18d1f0fSNeilBrown 		irq_set_nested_thread(irq, 1);
668a30d46c0SDavid Brownell 		activate_irq(irq);
669a30d46c0SDavid Brownell 	}
670a30d46c0SDavid Brownell 
671a30d46c0SDavid Brownell 	/* replace generic PIH handler (handle_simple_irq) */
672a30d46c0SDavid Brownell 	irq = sih_mod + twl4030_irq_base;
673d5bb1221SThomas Gleixner 	irq_set_handler_data(irq, agent);
674c1e61bcfSNeilBrown 	agent->irq_name = kasprintf(GFP_KERNEL, "twl4030_%s", sih->name);
675c1e61bcfSNeilBrown 	status = request_threaded_irq(irq, NULL, handle_twl4030_sih, 0,
676c1e61bcfSNeilBrown 				      agent->irq_name ?: sih->name, NULL);
677a30d46c0SDavid Brownell 
678ec1a07b3SBenoit Cousson 	dev_info(dev, "%s (irq %d) chaining IRQs %d..%d\n", sih->name,
679f01b1f90SBenoit Cousson 			irq, irq_base, irq_base + i - 1);
680a30d46c0SDavid Brownell 
681c1e61bcfSNeilBrown 	return status < 0 ? status : irq_base;
682a30d46c0SDavid Brownell }
683a30d46c0SDavid Brownell 
684a30d46c0SDavid Brownell /* FIXME need a call to reverse twl4030_sih_setup() ... */
685a30d46c0SDavid Brownell 
686a30d46c0SDavid Brownell /*----------------------------------------------------------------------*/
687a30d46c0SDavid Brownell 
688a30d46c0SDavid Brownell /* FIXME pass in which interrupt line we'll use ... */
689a30d46c0SDavid Brownell #define twl_irq_line	0
690a30d46c0SDavid Brownell 
69178518ffaSBenoit Cousson int twl4030_init_irq(struct device *dev, int irq_num)
692a30d46c0SDavid Brownell {
693a30d46c0SDavid Brownell 	static struct irq_chip	twl4030_irq_chip;
694ec1a07b3SBenoit Cousson 	int			status, i;
69578518ffaSBenoit Cousson 	int			irq_base, irq_end, nr_irqs;
69678518ffaSBenoit Cousson 	struct			device_node *node = dev->of_node;
697a30d46c0SDavid Brownell 
698a30d46c0SDavid Brownell 	/*
69978518ffaSBenoit Cousson 	 * TWL core and pwr interrupts must be contiguous because
70078518ffaSBenoit Cousson 	 * the hwirqs numbers are defined contiguously from 1 to 15.
70178518ffaSBenoit Cousson 	 * Create only one domain for both.
70278518ffaSBenoit Cousson 	 */
70378518ffaSBenoit Cousson 	nr_irqs = TWL4030_PWR_NR_IRQS + TWL4030_CORE_NR_IRQS;
70478518ffaSBenoit Cousson 
70578518ffaSBenoit Cousson 	irq_base = irq_alloc_descs(-1, 0, nr_irqs, 0);
70678518ffaSBenoit Cousson 	if (IS_ERR_VALUE(irq_base)) {
70778518ffaSBenoit Cousson 		dev_err(dev, "Fail to allocate IRQ descs\n");
70878518ffaSBenoit Cousson 		return irq_base;
70978518ffaSBenoit Cousson 	}
71078518ffaSBenoit Cousson 
71178518ffaSBenoit Cousson 	irq_domain_add_legacy(node, nr_irqs, irq_base, 0,
71278518ffaSBenoit Cousson 			      &irq_domain_simple_ops, NULL);
71378518ffaSBenoit Cousson 
71478518ffaSBenoit Cousson 	irq_end = irq_base + TWL4030_CORE_NR_IRQS;
71578518ffaSBenoit Cousson 
71678518ffaSBenoit Cousson 	/*
717a30d46c0SDavid Brownell 	 * Mask and clear all TWL4030 interrupts since initially we do
718a30d46c0SDavid Brownell 	 * not have any TWL4030 module interrupt handlers present
719a30d46c0SDavid Brownell 	 */
720a30d46c0SDavid Brownell 	status = twl4030_init_sih_modules(twl_irq_line);
721a30d46c0SDavid Brownell 	if (status < 0)
722a30d46c0SDavid Brownell 		return status;
723a30d46c0SDavid Brownell 
724a30d46c0SDavid Brownell 	twl4030_irq_base = irq_base;
725a30d46c0SDavid Brownell 
726cbcde05eSFelipe Contreras 	/*
727ec1a07b3SBenoit Cousson 	 * Install an irq handler for each of the SIH modules;
728a30d46c0SDavid Brownell 	 * clone dummy irq_chip since PIH can't *do* anything
729a30d46c0SDavid Brownell 	 */
730a30d46c0SDavid Brownell 	twl4030_irq_chip = dummy_irq_chip;
731a30d46c0SDavid Brownell 	twl4030_irq_chip.name = "twl4030";
732a30d46c0SDavid Brownell 
733fe212213SThomas Gleixner 	twl4030_sih_irq_chip.irq_ack = dummy_irq_chip.irq_ack;
734a30d46c0SDavid Brownell 
735a30d46c0SDavid Brownell 	for (i = irq_base; i < irq_end; i++) {
736d5bb1221SThomas Gleixner 		irq_set_chip_and_handler(i, &twl4030_irq_chip,
737a30d46c0SDavid Brownell 					 handle_simple_irq);
738925e853cSFelipe Balbi 		irq_set_nested_thread(i, 1);
739a30d46c0SDavid Brownell 		activate_irq(i);
740a30d46c0SDavid Brownell 	}
741f01b1f90SBenoit Cousson 
742ec1a07b3SBenoit Cousson 	dev_info(dev, "%s (irq %d) chaining IRQs %d..%d\n", "PIH",
743f01b1f90SBenoit Cousson 			irq_num, irq_base, irq_end);
744a30d46c0SDavid Brownell 
745a30d46c0SDavid Brownell 	/* ... and the PWR_INT module ... */
746f01b1f90SBenoit Cousson 	status = twl4030_sih_setup(dev, TWL4030_MODULE_INT, irq_end);
747a30d46c0SDavid Brownell 	if (status < 0) {
748ec1a07b3SBenoit Cousson 		dev_err(dev, "sih_setup PWR INT --> %d\n", status);
749a30d46c0SDavid Brownell 		goto fail;
750a30d46c0SDavid Brownell 	}
751a30d46c0SDavid Brownell 
752a30d46c0SDavid Brownell 	/* install an irq handler to demultiplex the TWL4030 interrupt */
753286f8f3cSNeilBrown 	status = request_threaded_irq(irq_num, NULL, handle_twl4030_pih,
754286f8f3cSNeilBrown 				      IRQF_ONESHOT,
755a980bf73SSamuel Ortiz 				      "TWL4030-PIH", NULL);
7561cef8e41SRussell King 	if (status < 0) {
757ec1a07b3SBenoit Cousson 		dev_err(dev, "could not claim irq%d: %d\n", irq_num, status);
7581cef8e41SRussell King 		goto fail_rqirq;
759a30d46c0SDavid Brownell 	}
760*5a2f1b5fSNeilBrown 	enable_irq_wake(irq_num);
761a30d46c0SDavid Brownell 
76278518ffaSBenoit Cousson 	return irq_base;
7631cef8e41SRussell King fail_rqirq:
7641cef8e41SRussell King 	/* clean up twl4030_sih_setup */
765a30d46c0SDavid Brownell fail:
766925e853cSFelipe Balbi 	for (i = irq_base; i < irq_end; i++) {
767925e853cSFelipe Balbi 		irq_set_nested_thread(i, 0);
768d5bb1221SThomas Gleixner 		irq_set_chip_and_handler(i, NULL, NULL);
769925e853cSFelipe Balbi 	}
7702f2a7d5eSFelipe Balbi 
771a30d46c0SDavid Brownell 	return status;
772a30d46c0SDavid Brownell }
773a30d46c0SDavid Brownell 
774e8deb28cSBalaji T K int twl4030_exit_irq(void)
775a30d46c0SDavid Brownell {
776a30d46c0SDavid Brownell 	/* FIXME undo twl_init_irq() */
777a30d46c0SDavid Brownell 	if (twl4030_irq_base) {
778a30d46c0SDavid Brownell 		pr_err("twl4030: can't yet clean up IRQs?\n");
779a30d46c0SDavid Brownell 		return -ENOSYS;
780a30d46c0SDavid Brownell 	}
781a30d46c0SDavid Brownell 	return 0;
782a30d46c0SDavid Brownell }
7831920a61eSIlkka Koskinen 
784e8deb28cSBalaji T K int twl4030_init_chip_irq(const char *chip)
7851920a61eSIlkka Koskinen {
7861920a61eSIlkka Koskinen 	if (!strcmp(chip, "twl5031")) {
7871920a61eSIlkka Koskinen 		sih_modules = sih_modules_twl5031;
7881920a61eSIlkka Koskinen 		nr_sih_modules = ARRAY_SIZE(sih_modules_twl5031);
7891920a61eSIlkka Koskinen 	} else {
7901920a61eSIlkka Koskinen 		sih_modules = sih_modules_twl4030;
7911920a61eSIlkka Koskinen 		nr_sih_modules = ARRAY_SIZE(sih_modules_twl4030);
7921920a61eSIlkka Koskinen 	}
7931920a61eSIlkka Koskinen 
7941920a61eSIlkka Koskinen 	return 0;
7951920a61eSIlkka Koskinen }
796