xref: /linux/drivers/mfd/twl4030-irq.c (revision 6c6a8c6af6423b5d316dcb8016857e33f36ddb35)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2a30d46c0SDavid Brownell /*
3a30d46c0SDavid Brownell  * twl4030-irq.c - TWL4030/TPS659x0 irq support
4a30d46c0SDavid Brownell  *
5a30d46c0SDavid Brownell  * Copyright (C) 2005-2006 Texas Instruments, Inc.
6a30d46c0SDavid Brownell  *
7a30d46c0SDavid Brownell  * Modifications to defer interrupt handling to a kernel thread:
8a30d46c0SDavid Brownell  * Copyright (C) 2006 MontaVista Software, Inc.
9a30d46c0SDavid Brownell  *
10a30d46c0SDavid Brownell  * Based on tlv320aic23.c:
11a30d46c0SDavid Brownell  * Copyright (c) by Kai Svahn <kai.svahn@nokia.com>
12a30d46c0SDavid Brownell  *
13a30d46c0SDavid Brownell  * Code cleanup and modifications to IRQ handler.
14a30d46c0SDavid Brownell  * by syed khasim <x0khasim@ti.com>
15a30d46c0SDavid Brownell  */
16a30d46c0SDavid Brownell 
17*6c6a8c6aSAndy Shevchenko #include <linux/device.h>
1878518ffaSBenoit Cousson #include <linux/export.h>
19a30d46c0SDavid Brownell #include <linux/interrupt.h>
20a30d46c0SDavid Brownell #include <linux/irq.h>
215a0e3ad6STejun Heo #include <linux/slab.h>
2278518ffaSBenoit Cousson #include <linux/of.h>
2378518ffaSBenoit Cousson #include <linux/irqdomain.h>
24a2054256SWolfram Sang #include <linux/mfd/twl.h>
25a30d46c0SDavid Brownell 
26b0b4a7c2SG, Manjunath Kondaiah #include "twl-core.h"
27a30d46c0SDavid Brownell 
28a30d46c0SDavid Brownell /*
29a30d46c0SDavid Brownell  * TWL4030 IRQ handling has two stages in hardware, and thus in software.
30a30d46c0SDavid Brownell  * The Primary Interrupt Handler (PIH) stage exposes status bits saying
31a30d46c0SDavid Brownell  * which Secondary Interrupt Handler (SIH) stage is raising an interrupt.
32a30d46c0SDavid Brownell  * SIH modules are more traditional IRQ components, which support per-IRQ
33a30d46c0SDavid Brownell  * enable/disable and trigger controls; they do most of the work.
34a30d46c0SDavid Brownell  *
35a30d46c0SDavid Brownell  * These chips are designed to support IRQ handling from two different
36a30d46c0SDavid Brownell  * I2C masters.  Each has a dedicated IRQ line, and dedicated IRQ status
37a30d46c0SDavid Brownell  * and mask registers in the PIH and SIH modules.
38a30d46c0SDavid Brownell  *
39a30d46c0SDavid Brownell  * We set up IRQs starting at a platform-specified base, always starting
40a30d46c0SDavid Brownell  * with PIH and the SIH for PWR_INT and then usually adding GPIO:
41a30d46c0SDavid Brownell  *	base + 0  .. base + 7	PIH
42a30d46c0SDavid Brownell  *	base + 8  .. base + 15	SIH for PWR_INT
43a30d46c0SDavid Brownell  *	base + 16 .. base + 33	SIH for GPIO
44a30d46c0SDavid Brownell  */
4578518ffaSBenoit Cousson #define TWL4030_CORE_NR_IRQS	8
4678518ffaSBenoit Cousson #define TWL4030_PWR_NR_IRQS	8
47a30d46c0SDavid Brownell 
48a30d46c0SDavid Brownell /* PIH register offsets */
49a30d46c0SDavid Brownell #define REG_PIH_ISR_P1			0x01
50a30d46c0SDavid Brownell #define REG_PIH_ISR_P2			0x02
51a30d46c0SDavid Brownell #define REG_PIH_SIR			0x03	/* for testing */
52a30d46c0SDavid Brownell 
53a30d46c0SDavid Brownell /* Linux could (eventually) use either IRQ line */
54a30d46c0SDavid Brownell static int irq_line;
55a30d46c0SDavid Brownell 
56a30d46c0SDavid Brownell struct sih {
57a30d46c0SDavid Brownell 	char	name[8];
58a30d46c0SDavid Brownell 	u8	module;			/* module id */
59a30d46c0SDavid Brownell 	u8	control_offset;		/* for SIH_CTRL */
60a30d46c0SDavid Brownell 	bool	set_cor;
61a30d46c0SDavid Brownell 
62a30d46c0SDavid Brownell 	u8	bits;			/* valid in isr/imr */
63a30d46c0SDavid Brownell 	u8	bytes_ixr;		/* bytelen of ISR/IMR/SIR */
64a30d46c0SDavid Brownell 
65a30d46c0SDavid Brownell 	u8	edr_offset;
66a30d46c0SDavid Brownell 	u8	bytes_edr;		/* bytelen of EDR */
67a30d46c0SDavid Brownell 
681920a61eSIlkka Koskinen 	u8	irq_lines;		/* number of supported irq lines */
691920a61eSIlkka Koskinen 
70a30d46c0SDavid Brownell 	/* SIR ignored -- set interrupt, for testing only */
7135a27e8eSThomas Gleixner 	struct sih_irq_data {
72a30d46c0SDavid Brownell 		u8	isr_offset;
73a30d46c0SDavid Brownell 		u8	imr_offset;
74a30d46c0SDavid Brownell 	} mask[2];
75a30d46c0SDavid Brownell 	/* + 2 bytes padding */
76a30d46c0SDavid Brownell };
77a30d46c0SDavid Brownell 
781920a61eSIlkka Koskinen static const struct sih *sih_modules;
791920a61eSIlkka Koskinen static int nr_sih_modules;
801920a61eSIlkka Koskinen 
81a30d46c0SDavid Brownell #define SIH_INITIALIZER(modname, nbits) \
82a30d46c0SDavid Brownell 	.module		= TWL4030_MODULE_ ## modname, \
83a30d46c0SDavid Brownell 	.control_offset = TWL4030_ ## modname ## _SIH_CTRL, \
84a30d46c0SDavid Brownell 	.bits		= nbits, \
85a30d46c0SDavid Brownell 	.bytes_ixr	= DIV_ROUND_UP(nbits, 8), \
86a30d46c0SDavid Brownell 	.edr_offset	= TWL4030_ ## modname ## _EDR, \
87a30d46c0SDavid Brownell 	.bytes_edr	= DIV_ROUND_UP((2*(nbits)), 8), \
881920a61eSIlkka Koskinen 	.irq_lines	= 2, \
89a30d46c0SDavid Brownell 	.mask = { { \
90a30d46c0SDavid Brownell 		.isr_offset	= TWL4030_ ## modname ## _ISR1, \
91a30d46c0SDavid Brownell 		.imr_offset	= TWL4030_ ## modname ## _IMR1, \
92a30d46c0SDavid Brownell 	}, \
93a30d46c0SDavid Brownell 	{ \
94a30d46c0SDavid Brownell 		.isr_offset	= TWL4030_ ## modname ## _ISR2, \
95a30d46c0SDavid Brownell 		.imr_offset	= TWL4030_ ## modname ## _IMR2, \
96a30d46c0SDavid Brownell 	}, },
97a30d46c0SDavid Brownell 
98a30d46c0SDavid Brownell /* register naming policies are inconsistent ... */
99a30d46c0SDavid Brownell #define TWL4030_INT_PWR_EDR		TWL4030_INT_PWR_EDR1
100a30d46c0SDavid Brownell #define TWL4030_MODULE_KEYPAD_KEYP	TWL4030_MODULE_KEYPAD
101a30d46c0SDavid Brownell #define TWL4030_MODULE_INT_PWR		TWL4030_MODULE_INT
102a30d46c0SDavid Brownell 
103a30d46c0SDavid Brownell 
104cbcde05eSFelipe Contreras /*
105cbcde05eSFelipe Contreras  * Order in this table matches order in PIH_ISR.  That is,
106a30d46c0SDavid Brownell  * BIT(n) in PIH_ISR is sih_modules[n].
107a30d46c0SDavid Brownell  */
1081920a61eSIlkka Koskinen /* sih_modules_twl4030 is used both in twl4030 and twl5030 */
1091920a61eSIlkka Koskinen static const struct sih sih_modules_twl4030[6] = {
110a30d46c0SDavid Brownell 	[0] = {
111a30d46c0SDavid Brownell 		.name		= "gpio",
112a30d46c0SDavid Brownell 		.module		= TWL4030_MODULE_GPIO,
113a30d46c0SDavid Brownell 		.control_offset	= REG_GPIO_SIH_CTRL,
114a30d46c0SDavid Brownell 		.set_cor	= true,
115a30d46c0SDavid Brownell 		.bits		= TWL4030_GPIO_MAX,
116a30d46c0SDavid Brownell 		.bytes_ixr	= 3,
117a30d46c0SDavid Brownell 		/* Note: *all* of these IRQs default to no-trigger */
118a30d46c0SDavid Brownell 		.edr_offset	= REG_GPIO_EDR1,
119a30d46c0SDavid Brownell 		.bytes_edr	= 5,
1201920a61eSIlkka Koskinen 		.irq_lines	= 2,
121a30d46c0SDavid Brownell 		.mask = { {
122a30d46c0SDavid Brownell 			.isr_offset	= REG_GPIO_ISR1A,
123a30d46c0SDavid Brownell 			.imr_offset	= REG_GPIO_IMR1A,
124a30d46c0SDavid Brownell 		}, {
125a30d46c0SDavid Brownell 			.isr_offset	= REG_GPIO_ISR1B,
126a30d46c0SDavid Brownell 			.imr_offset	= REG_GPIO_IMR1B,
127a30d46c0SDavid Brownell 		}, },
128a30d46c0SDavid Brownell 	},
129a30d46c0SDavid Brownell 	[1] = {
130a30d46c0SDavid Brownell 		.name		= "keypad",
131a30d46c0SDavid Brownell 		.set_cor	= true,
132a30d46c0SDavid Brownell 		SIH_INITIALIZER(KEYPAD_KEYP, 4)
133a30d46c0SDavid Brownell 	},
134a30d46c0SDavid Brownell 	[2] = {
135a30d46c0SDavid Brownell 		.name		= "bci",
136a30d46c0SDavid Brownell 		.module		= TWL4030_MODULE_INTERRUPTS,
137a30d46c0SDavid Brownell 		.control_offset	= TWL4030_INTERRUPTS_BCISIHCTRL,
1388e52e279SGrazvydas Ignotas 		.set_cor	= true,
139a30d46c0SDavid Brownell 		.bits		= 12,
140a30d46c0SDavid Brownell 		.bytes_ixr	= 2,
141a30d46c0SDavid Brownell 		.edr_offset	= TWL4030_INTERRUPTS_BCIEDR1,
142a30d46c0SDavid Brownell 		/* Note: most of these IRQs default to no-trigger */
143a30d46c0SDavid Brownell 		.bytes_edr	= 3,
1441920a61eSIlkka Koskinen 		.irq_lines	= 2,
145a30d46c0SDavid Brownell 		.mask = { {
146a30d46c0SDavid Brownell 			.isr_offset	= TWL4030_INTERRUPTS_BCIISR1A,
147a30d46c0SDavid Brownell 			.imr_offset	= TWL4030_INTERRUPTS_BCIIMR1A,
148a30d46c0SDavid Brownell 		}, {
149a30d46c0SDavid Brownell 			.isr_offset	= TWL4030_INTERRUPTS_BCIISR1B,
150a30d46c0SDavid Brownell 			.imr_offset	= TWL4030_INTERRUPTS_BCIIMR1B,
151a30d46c0SDavid Brownell 		}, },
152a30d46c0SDavid Brownell 	},
153a30d46c0SDavid Brownell 	[3] = {
154a30d46c0SDavid Brownell 		.name		= "madc",
155a30d46c0SDavid Brownell 		SIH_INITIALIZER(MADC, 4)
156a30d46c0SDavid Brownell 	},
157a30d46c0SDavid Brownell 	[4] = {
158a30d46c0SDavid Brownell 		/* USB doesn't use the same SIH organization */
159a30d46c0SDavid Brownell 		.name		= "usb",
160a30d46c0SDavid Brownell 	},
161a30d46c0SDavid Brownell 	[5] = {
162a30d46c0SDavid Brownell 		.name		= "power",
163a30d46c0SDavid Brownell 		.set_cor	= true,
164a30d46c0SDavid Brownell 		SIH_INITIALIZER(INT_PWR, 8)
165a30d46c0SDavid Brownell 	},
166a30d46c0SDavid Brownell 		/* there are no SIH modules #6 or #7 ... */
167a30d46c0SDavid Brownell };
168a30d46c0SDavid Brownell 
1691920a61eSIlkka Koskinen static const struct sih sih_modules_twl5031[8] = {
1701920a61eSIlkka Koskinen 	[0] = {
1711920a61eSIlkka Koskinen 		.name		= "gpio",
1721920a61eSIlkka Koskinen 		.module		= TWL4030_MODULE_GPIO,
1731920a61eSIlkka Koskinen 		.control_offset	= REG_GPIO_SIH_CTRL,
1741920a61eSIlkka Koskinen 		.set_cor	= true,
1751920a61eSIlkka Koskinen 		.bits		= TWL4030_GPIO_MAX,
1761920a61eSIlkka Koskinen 		.bytes_ixr	= 3,
1771920a61eSIlkka Koskinen 		/* Note: *all* of these IRQs default to no-trigger */
1781920a61eSIlkka Koskinen 		.edr_offset	= REG_GPIO_EDR1,
1791920a61eSIlkka Koskinen 		.bytes_edr	= 5,
1801920a61eSIlkka Koskinen 		.irq_lines	= 2,
1811920a61eSIlkka Koskinen 		.mask = { {
1821920a61eSIlkka Koskinen 			.isr_offset	= REG_GPIO_ISR1A,
1831920a61eSIlkka Koskinen 			.imr_offset	= REG_GPIO_IMR1A,
1841920a61eSIlkka Koskinen 		}, {
1851920a61eSIlkka Koskinen 			.isr_offset	= REG_GPIO_ISR1B,
1861920a61eSIlkka Koskinen 			.imr_offset	= REG_GPIO_IMR1B,
1871920a61eSIlkka Koskinen 		}, },
1881920a61eSIlkka Koskinen 	},
1891920a61eSIlkka Koskinen 	[1] = {
1901920a61eSIlkka Koskinen 		.name		= "keypad",
1911920a61eSIlkka Koskinen 		.set_cor	= true,
1921920a61eSIlkka Koskinen 		SIH_INITIALIZER(KEYPAD_KEYP, 4)
1931920a61eSIlkka Koskinen 	},
1941920a61eSIlkka Koskinen 	[2] = {
1951920a61eSIlkka Koskinen 		.name		= "bci",
1961920a61eSIlkka Koskinen 		.module		= TWL5031_MODULE_INTERRUPTS,
1971920a61eSIlkka Koskinen 		.control_offset	= TWL5031_INTERRUPTS_BCISIHCTRL,
1981920a61eSIlkka Koskinen 		.bits		= 7,
1991920a61eSIlkka Koskinen 		.bytes_ixr	= 1,
2001920a61eSIlkka Koskinen 		.edr_offset	= TWL5031_INTERRUPTS_BCIEDR1,
2011920a61eSIlkka Koskinen 		/* Note: most of these IRQs default to no-trigger */
2021920a61eSIlkka Koskinen 		.bytes_edr	= 2,
2031920a61eSIlkka Koskinen 		.irq_lines	= 2,
2041920a61eSIlkka Koskinen 		.mask = { {
2051920a61eSIlkka Koskinen 			.isr_offset	= TWL5031_INTERRUPTS_BCIISR1,
2061920a61eSIlkka Koskinen 			.imr_offset	= TWL5031_INTERRUPTS_BCIIMR1,
2071920a61eSIlkka Koskinen 		}, {
2081920a61eSIlkka Koskinen 			.isr_offset	= TWL5031_INTERRUPTS_BCIISR2,
2091920a61eSIlkka Koskinen 			.imr_offset	= TWL5031_INTERRUPTS_BCIIMR2,
2101920a61eSIlkka Koskinen 		}, },
2111920a61eSIlkka Koskinen 	},
2121920a61eSIlkka Koskinen 	[3] = {
2131920a61eSIlkka Koskinen 		.name		= "madc",
2141920a61eSIlkka Koskinen 		SIH_INITIALIZER(MADC, 4)
2151920a61eSIlkka Koskinen 	},
2161920a61eSIlkka Koskinen 	[4] = {
2171920a61eSIlkka Koskinen 		/* USB doesn't use the same SIH organization */
2181920a61eSIlkka Koskinen 		.name		= "usb",
2191920a61eSIlkka Koskinen 	},
2201920a61eSIlkka Koskinen 	[5] = {
2211920a61eSIlkka Koskinen 		.name		= "power",
2221920a61eSIlkka Koskinen 		.set_cor	= true,
2231920a61eSIlkka Koskinen 		SIH_INITIALIZER(INT_PWR, 8)
2241920a61eSIlkka Koskinen 	},
2251920a61eSIlkka Koskinen 	[6] = {
2261920a61eSIlkka Koskinen 		/*
227191211f5SIlkka Koskinen 		 * ECI/DBI doesn't use the same SIH organization.
228191211f5SIlkka Koskinen 		 * For example, it supports only one interrupt output line.
229191211f5SIlkka Koskinen 		 * That is, the interrupts are seen on both INT1 and INT2 lines.
2301920a61eSIlkka Koskinen 		 */
231191211f5SIlkka Koskinen 		.name		= "eci_dbi",
2321920a61eSIlkka Koskinen 		.module		= TWL5031_MODULE_ACCESSORY,
2331920a61eSIlkka Koskinen 		.bits		= 9,
2341920a61eSIlkka Koskinen 		.bytes_ixr	= 2,
2351920a61eSIlkka Koskinen 		.irq_lines	= 1,
2361920a61eSIlkka Koskinen 		.mask = { {
2371920a61eSIlkka Koskinen 			.isr_offset	= TWL5031_ACIIDR_LSB,
2381920a61eSIlkka Koskinen 			.imr_offset	= TWL5031_ACIIMR_LSB,
2391920a61eSIlkka Koskinen 		}, },
2401920a61eSIlkka Koskinen 
2411920a61eSIlkka Koskinen 	},
2421920a61eSIlkka Koskinen 	[7] = {
243191211f5SIlkka Koskinen 		/* Audio accessory */
244191211f5SIlkka Koskinen 		.name		= "audio",
2451920a61eSIlkka Koskinen 		.module		= TWL5031_MODULE_ACCESSORY,
2461920a61eSIlkka Koskinen 		.control_offset	= TWL5031_ACCSIHCTRL,
2471920a61eSIlkka Koskinen 		.bits		= 2,
2481920a61eSIlkka Koskinen 		.bytes_ixr	= 1,
2491920a61eSIlkka Koskinen 		.edr_offset	= TWL5031_ACCEDR1,
2501920a61eSIlkka Koskinen 		/* Note: most of these IRQs default to no-trigger */
2511920a61eSIlkka Koskinen 		.bytes_edr	= 1,
2521920a61eSIlkka Koskinen 		.irq_lines	= 2,
2531920a61eSIlkka Koskinen 		.mask = { {
2541920a61eSIlkka Koskinen 			.isr_offset	= TWL5031_ACCISR1,
2551920a61eSIlkka Koskinen 			.imr_offset	= TWL5031_ACCIMR1,
2561920a61eSIlkka Koskinen 		}, {
2571920a61eSIlkka Koskinen 			.isr_offset	= TWL5031_ACCISR2,
2581920a61eSIlkka Koskinen 			.imr_offset	= TWL5031_ACCIMR2,
2591920a61eSIlkka Koskinen 		}, },
2601920a61eSIlkka Koskinen 	},
2611920a61eSIlkka Koskinen };
2621920a61eSIlkka Koskinen 
263a30d46c0SDavid Brownell #undef TWL4030_MODULE_KEYPAD_KEYP
264a30d46c0SDavid Brownell #undef TWL4030_MODULE_INT_PWR
265a30d46c0SDavid Brownell #undef TWL4030_INT_PWR_EDR
266a30d46c0SDavid Brownell 
267a30d46c0SDavid Brownell /*----------------------------------------------------------------------*/
268a30d46c0SDavid Brownell 
269a30d46c0SDavid Brownell static unsigned twl4030_irq_base;
270a30d46c0SDavid Brownell 
271a30d46c0SDavid Brownell /*
272a30d46c0SDavid Brownell  * handle_twl4030_pih() is the desc->handle method for the twl4030 interrupt.
273a30d46c0SDavid Brownell  * This is a chained interrupt, so there is no desc->action method for it.
274a30d46c0SDavid Brownell  * Now we need to query the interrupt controller in the twl4030 to determine
275a30d46c0SDavid Brownell  * which module is generating the interrupt request.  However, we can't do i2c
276a30d46c0SDavid Brownell  * transactions in interrupt context, so we must defer that work to a kernel
277a30d46c0SDavid Brownell  * thread.  All we do here is acknowledge and mask the interrupt and wakeup
278a30d46c0SDavid Brownell  * the kernel thread.
279a30d46c0SDavid Brownell  */
2801cef8e41SRussell King static irqreturn_t handle_twl4030_pih(int irq, void *devid)
281a30d46c0SDavid Brownell {
2827750c9b0SFelipe Balbi 	irqreturn_t	ret;
2837750c9b0SFelipe Balbi 	u8		pih_isr;
2847750c9b0SFelipe Balbi 
2856fbc6420SPeter Ujfalusi 	ret = twl_i2c_read_u8(TWL_MODULE_PIH, &pih_isr,
2867750c9b0SFelipe Balbi 			      REG_PIH_ISR_P1);
2877750c9b0SFelipe Balbi 	if (ret) {
28804aa4438SLee Jones 		pr_warn("twl4030: I2C error %d reading PIH ISR\n", ret);
2897750c9b0SFelipe Balbi 		return IRQ_NONE;
2907750c9b0SFelipe Balbi 	}
2917750c9b0SFelipe Balbi 
2925a903090SFelipe Balbi 	while (pih_isr) {
2935a903090SFelipe Balbi 		unsigned long	pending = __ffs(pih_isr);
2945a903090SFelipe Balbi 		unsigned int	irq;
2955a903090SFelipe Balbi 
2965a903090SFelipe Balbi 		pih_isr &= ~BIT(pending);
2975a903090SFelipe Balbi 		irq = pending + twl4030_irq_base;
2985a903090SFelipe Balbi 		handle_nested_irq(irq);
2997750c9b0SFelipe Balbi 	}
3007750c9b0SFelipe Balbi 
3011cef8e41SRussell King 	return IRQ_HANDLED;
302a30d46c0SDavid Brownell }
303cbcde05eSFelipe Contreras 
304a30d46c0SDavid Brownell /*----------------------------------------------------------------------*/
305a30d46c0SDavid Brownell 
306a30d46c0SDavid Brownell /*
307a30d46c0SDavid Brownell  * twl4030_init_sih_modules() ... start from a known state where no
308a30d46c0SDavid Brownell  * IRQs will be coming in, and where we can quickly enable them then
309a30d46c0SDavid Brownell  * handle them as they arrive.  Mask all IRQs: maybe init SIH_CTRL.
310a30d46c0SDavid Brownell  *
311a30d46c0SDavid Brownell  * NOTE:  we don't touch EDR registers here; they stay with hardware
312a30d46c0SDavid Brownell  * defaults or whatever the last value was.  Note that when both EDR
313a30d46c0SDavid Brownell  * bits for an IRQ are clear, that's as if its IMR bit is set...
314a30d46c0SDavid Brownell  */
315a30d46c0SDavid Brownell static int twl4030_init_sih_modules(unsigned line)
316a30d46c0SDavid Brownell {
317a30d46c0SDavid Brownell 	const struct sih *sih;
318a30d46c0SDavid Brownell 	u8 buf[4];
319a30d46c0SDavid Brownell 	int i;
320a30d46c0SDavid Brownell 	int status;
321a30d46c0SDavid Brownell 
322a30d46c0SDavid Brownell 	/* line 0 == int1_n signal; line 1 == int2_n signal */
323a30d46c0SDavid Brownell 	if (line > 1)
324a30d46c0SDavid Brownell 		return -EINVAL;
325a30d46c0SDavid Brownell 
326a30d46c0SDavid Brownell 	irq_line = line;
327a30d46c0SDavid Brownell 
328a30d46c0SDavid Brownell 	/* disable all interrupts on our line */
32904aa4438SLee Jones 	memset(buf, 0xff, sizeof(buf));
330a30d46c0SDavid Brownell 	sih = sih_modules;
3311920a61eSIlkka Koskinen 	for (i = 0; i < nr_sih_modules; i++, sih++) {
332a30d46c0SDavid Brownell 		/* skip USB -- it's funky */
333a30d46c0SDavid Brownell 		if (!sih->bytes_ixr)
334a30d46c0SDavid Brownell 			continue;
335a30d46c0SDavid Brownell 
3361920a61eSIlkka Koskinen 		/* Not all the SIH modules support multiple interrupt lines */
3371920a61eSIlkka Koskinen 		if (sih->irq_lines <= line)
3381920a61eSIlkka Koskinen 			continue;
3391920a61eSIlkka Koskinen 
340fc7b92fcSBalaji T K 		status = twl_i2c_write(sih->module, buf,
341a30d46c0SDavid Brownell 				sih->mask[line].imr_offset, sih->bytes_ixr);
342a30d46c0SDavid Brownell 		if (status < 0)
343a30d46c0SDavid Brownell 			pr_err("twl4030: err %d initializing %s %s\n",
344a30d46c0SDavid Brownell 					status, sih->name, "IMR");
345a30d46c0SDavid Brownell 
346cbcde05eSFelipe Contreras 		/*
347cbcde05eSFelipe Contreras 		 * Maybe disable "exclusive" mode; buffer second pending irq;
348a30d46c0SDavid Brownell 		 * set Clear-On-Read (COR) bit.
349a30d46c0SDavid Brownell 		 *
350a30d46c0SDavid Brownell 		 * NOTE that sometimes COR polarity is documented as being
3518e52e279SGrazvydas Ignotas 		 * inverted:  for MADC, COR=1 means "clear on write".
352a30d46c0SDavid Brownell 		 * And for PWR_INT it's not documented...
353a30d46c0SDavid Brownell 		 */
354a30d46c0SDavid Brownell 		if (sih->set_cor) {
355fc7b92fcSBalaji T K 			status = twl_i2c_write_u8(sih->module,
356a30d46c0SDavid Brownell 					TWL4030_SIH_CTRL_COR_MASK,
357a30d46c0SDavid Brownell 					sih->control_offset);
358a30d46c0SDavid Brownell 			if (status < 0)
359a30d46c0SDavid Brownell 				pr_err("twl4030: err %d initializing %s %s\n",
360a30d46c0SDavid Brownell 						status, sih->name, "SIH_CTRL");
361a30d46c0SDavid Brownell 		}
362a30d46c0SDavid Brownell 	}
363a30d46c0SDavid Brownell 
364a30d46c0SDavid Brownell 	sih = sih_modules;
3651920a61eSIlkka Koskinen 	for (i = 0; i < nr_sih_modules; i++, sih++) {
366a30d46c0SDavid Brownell 		u8 rxbuf[4];
367a30d46c0SDavid Brownell 		int j;
368a30d46c0SDavid Brownell 
369a30d46c0SDavid Brownell 		/* skip USB */
370a30d46c0SDavid Brownell 		if (!sih->bytes_ixr)
371a30d46c0SDavid Brownell 			continue;
372a30d46c0SDavid Brownell 
3731920a61eSIlkka Koskinen 		/* Not all the SIH modules support multiple interrupt lines */
3741920a61eSIlkka Koskinen 		if (sih->irq_lines <= line)
3751920a61eSIlkka Koskinen 			continue;
3761920a61eSIlkka Koskinen 
377cbcde05eSFelipe Contreras 		/*
378cbcde05eSFelipe Contreras 		 * Clear pending interrupt status.  Either the read was
379a30d46c0SDavid Brownell 		 * enough, or we need to write those bits.  Repeat, in
380a30d46c0SDavid Brownell 		 * case an IRQ is pending (PENDDIS=0) ... that's not
381a30d46c0SDavid Brownell 		 * uncommon with PWR_INT.PWRON.
382a30d46c0SDavid Brownell 		 */
383a30d46c0SDavid Brownell 		for (j = 0; j < 2; j++) {
384fc7b92fcSBalaji T K 			status = twl_i2c_read(sih->module, rxbuf,
385a30d46c0SDavid Brownell 				sih->mask[line].isr_offset, sih->bytes_ixr);
386a30d46c0SDavid Brownell 			if (status < 0)
3878a012ff9SLee Jones 				pr_warn("twl4030: err %d initializing %s %s\n",
388a30d46c0SDavid Brownell 					status, sih->name, "ISR");
389a30d46c0SDavid Brownell 
3908a012ff9SLee Jones 			if (!sih->set_cor) {
391fc7b92fcSBalaji T K 				status = twl_i2c_write(sih->module, buf,
392a30d46c0SDavid Brownell 					sih->mask[line].isr_offset,
393a30d46c0SDavid Brownell 					sih->bytes_ixr);
3948a012ff9SLee Jones 				if (status < 0)
3958a012ff9SLee Jones 					pr_warn("twl4030: write failed: %d\n",
3968a012ff9SLee Jones 						status);
3978a012ff9SLee Jones 			}
398cbcde05eSFelipe Contreras 			/*
399cbcde05eSFelipe Contreras 			 * else COR=1 means read sufficed.
400a30d46c0SDavid Brownell 			 * (for most SIH modules...)
401a30d46c0SDavid Brownell 			 */
402a30d46c0SDavid Brownell 		}
403a30d46c0SDavid Brownell 	}
404a30d46c0SDavid Brownell 
405a30d46c0SDavid Brownell 	return 0;
406a30d46c0SDavid Brownell }
407a30d46c0SDavid Brownell 
408a30d46c0SDavid Brownell static inline void activate_irq(int irq)
409a30d46c0SDavid Brownell {
4109bd09f34SRob Herring 	irq_clear_status_flags(irq, IRQ_NOREQUEST | IRQ_NOPROBE);
411a30d46c0SDavid Brownell }
412a30d46c0SDavid Brownell 
413a30d46c0SDavid Brownell /*----------------------------------------------------------------------*/
414a30d46c0SDavid Brownell 
415a30d46c0SDavid Brownell struct sih_agent {
416a30d46c0SDavid Brownell 	int			irq_base;
417a30d46c0SDavid Brownell 	const struct sih	*sih;
418a30d46c0SDavid Brownell 
419a30d46c0SDavid Brownell 	u32			imr;
420a30d46c0SDavid Brownell 	bool			imr_change_pending;
421a30d46c0SDavid Brownell 
422a30d46c0SDavid Brownell 	u32			edge_change;
42391e3569fSFelipe Balbi 
42491e3569fSFelipe Balbi 	struct mutex		irq_lock;
425c1e61bcfSNeilBrown 	char			*irq_name;
426a30d46c0SDavid Brownell };
427a30d46c0SDavid Brownell 
428a30d46c0SDavid Brownell /*----------------------------------------------------------------------*/
429a30d46c0SDavid Brownell 
430a30d46c0SDavid Brownell /*
431a30d46c0SDavid Brownell  * All irq_chip methods get issued from code holding irq_desc[irq].lock,
432a30d46c0SDavid Brownell  * which can't perform the underlying I2C operations (because they sleep).
433a30d46c0SDavid Brownell  * So we must hand them off to a thread (workqueue) and cope with asynch
434a30d46c0SDavid Brownell  * completion, potentially including some re-ordering, of these requests.
435a30d46c0SDavid Brownell  */
436a30d46c0SDavid Brownell 
437845aeab5SMark Brown static void twl4030_sih_mask(struct irq_data *data)
438a30d46c0SDavid Brownell {
43984868424SFelipe Balbi 	struct sih_agent *agent = irq_data_get_irq_chip_data(data);
440a30d46c0SDavid Brownell 
44184868424SFelipe Balbi 	agent->imr |= BIT(data->irq - agent->irq_base);
44284868424SFelipe Balbi 	agent->imr_change_pending = true;
443a30d46c0SDavid Brownell }
444a30d46c0SDavid Brownell 
445845aeab5SMark Brown static void twl4030_sih_unmask(struct irq_data *data)
446a30d46c0SDavid Brownell {
44784868424SFelipe Balbi 	struct sih_agent *agent = irq_data_get_irq_chip_data(data);
448a30d46c0SDavid Brownell 
44984868424SFelipe Balbi 	agent->imr &= ~BIT(data->irq - agent->irq_base);
45084868424SFelipe Balbi 	agent->imr_change_pending = true;
451a30d46c0SDavid Brownell }
452a30d46c0SDavid Brownell 
453845aeab5SMark Brown static int twl4030_sih_set_type(struct irq_data *data, unsigned trigger)
454a30d46c0SDavid Brownell {
45584868424SFelipe Balbi 	struct sih_agent *agent = irq_data_get_irq_chip_data(data);
456a30d46c0SDavid Brownell 
457a30d46c0SDavid Brownell 	if (trigger & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
458a30d46c0SDavid Brownell 		return -EINVAL;
459a30d46c0SDavid Brownell 
4602f2a7d5eSFelipe Balbi 	if (irqd_get_trigger_type(data) != trigger)
46184868424SFelipe Balbi 		agent->edge_change |= BIT(data->irq - agent->irq_base);
46291e3569fSFelipe Balbi 
463a30d46c0SDavid Brownell 	return 0;
464a30d46c0SDavid Brownell }
465a30d46c0SDavid Brownell 
46691e3569fSFelipe Balbi static void twl4030_sih_bus_lock(struct irq_data *data)
46791e3569fSFelipe Balbi {
46884868424SFelipe Balbi 	struct sih_agent	*agent = irq_data_get_irq_chip_data(data);
46991e3569fSFelipe Balbi 
47084868424SFelipe Balbi 	mutex_lock(&agent->irq_lock);
47191e3569fSFelipe Balbi }
47291e3569fSFelipe Balbi 
47391e3569fSFelipe Balbi static void twl4030_sih_bus_sync_unlock(struct irq_data *data)
47491e3569fSFelipe Balbi {
47584868424SFelipe Balbi 	struct sih_agent	*agent = irq_data_get_irq_chip_data(data);
47684868424SFelipe Balbi 	const struct sih	*sih = agent->sih;
47784868424SFelipe Balbi 	int			status;
47891e3569fSFelipe Balbi 
47984868424SFelipe Balbi 	if (agent->imr_change_pending) {
48084868424SFelipe Balbi 		union {
4816fef0d4eSLee Jones 			__le32	word;
48284868424SFelipe Balbi 			u8	bytes[4];
48384868424SFelipe Balbi 		} imr;
48484868424SFelipe Balbi 
485c9531227SNeilBrown 		/* byte[0] gets overwritten as we write ... */
48614591d88SPeter Ujfalusi 		imr.word = cpu_to_le32(agent->imr);
48784868424SFelipe Balbi 		agent->imr_change_pending = false;
48884868424SFelipe Balbi 
48984868424SFelipe Balbi 		/* write the whole mask ... simpler than subsetting it */
49084868424SFelipe Balbi 		status = twl_i2c_write(sih->module, imr.bytes,
49184868424SFelipe Balbi 				sih->mask[irq_line].imr_offset,
49284868424SFelipe Balbi 				sih->bytes_ixr);
49384868424SFelipe Balbi 		if (status)
49484868424SFelipe Balbi 			pr_err("twl4030: %s, %s --> %d\n", __func__,
49584868424SFelipe Balbi 					"write", status);
49684868424SFelipe Balbi 	}
49784868424SFelipe Balbi 
4982f2a7d5eSFelipe Balbi 	if (agent->edge_change) {
4992f2a7d5eSFelipe Balbi 		u32		edge_change;
5002f2a7d5eSFelipe Balbi 		u8		bytes[6];
5012f2a7d5eSFelipe Balbi 
5022f2a7d5eSFelipe Balbi 		edge_change = agent->edge_change;
5032f2a7d5eSFelipe Balbi 		agent->edge_change = 0;
5042f2a7d5eSFelipe Balbi 
5052f2a7d5eSFelipe Balbi 		/*
5062f2a7d5eSFelipe Balbi 		 * Read, reserving first byte for write scratch.  Yes, this
5072f2a7d5eSFelipe Balbi 		 * could be cached for some speedup ... but be careful about
5082f2a7d5eSFelipe Balbi 		 * any processor on the other IRQ line, EDR registers are
5092f2a7d5eSFelipe Balbi 		 * shared.
5102f2a7d5eSFelipe Balbi 		 */
51114591d88SPeter Ujfalusi 		status = twl_i2c_read(sih->module, bytes,
5122f2a7d5eSFelipe Balbi 				sih->edr_offset, sih->bytes_edr);
5132f2a7d5eSFelipe Balbi 		if (status) {
5142f2a7d5eSFelipe Balbi 			pr_err("twl4030: %s, %s --> %d\n", __func__,
5152f2a7d5eSFelipe Balbi 					"read", status);
5162f2a7d5eSFelipe Balbi 			return;
5172f2a7d5eSFelipe Balbi 		}
5182f2a7d5eSFelipe Balbi 
5192f2a7d5eSFelipe Balbi 		/* Modify only the bits we know must change */
5202f2a7d5eSFelipe Balbi 		while (edge_change) {
5212f2a7d5eSFelipe Balbi 			int		i = fls(edge_change) - 1;
52214591d88SPeter Ujfalusi 			int		byte = i >> 2;
5232f2a7d5eSFelipe Balbi 			int		off = (i & 0x3) * 2;
5242f2a7d5eSFelipe Balbi 			unsigned int	type;
5252f2a7d5eSFelipe Balbi 
5262f2a7d5eSFelipe Balbi 			bytes[byte] &= ~(0x03 << off);
5272f2a7d5eSFelipe Balbi 
5285dbf79d4SJavier Martinez Canillas 			type = irq_get_trigger_type(i + agent->irq_base);
5292f2a7d5eSFelipe Balbi 			if (type & IRQ_TYPE_EDGE_RISING)
5302f2a7d5eSFelipe Balbi 				bytes[byte] |= BIT(off + 1);
5312f2a7d5eSFelipe Balbi 			if (type & IRQ_TYPE_EDGE_FALLING)
5322f2a7d5eSFelipe Balbi 				bytes[byte] |= BIT(off + 0);
5332f2a7d5eSFelipe Balbi 
5342f2a7d5eSFelipe Balbi 			edge_change &= ~BIT(i);
5352f2a7d5eSFelipe Balbi 		}
5362f2a7d5eSFelipe Balbi 
5372f2a7d5eSFelipe Balbi 		/* Write */
5382f2a7d5eSFelipe Balbi 		status = twl_i2c_write(sih->module, bytes,
5392f2a7d5eSFelipe Balbi 				sih->edr_offset, sih->bytes_edr);
5402f2a7d5eSFelipe Balbi 		if (status)
5412f2a7d5eSFelipe Balbi 			pr_err("twl4030: %s, %s --> %d\n", __func__,
5422f2a7d5eSFelipe Balbi 					"write", status);
5432f2a7d5eSFelipe Balbi 	}
5442f2a7d5eSFelipe Balbi 
54584868424SFelipe Balbi 	mutex_unlock(&agent->irq_lock);
54691e3569fSFelipe Balbi }
54791e3569fSFelipe Balbi 
548a30d46c0SDavid Brownell static struct irq_chip twl4030_sih_irq_chip = {
549a30d46c0SDavid Brownell 	.name		= "twl4030",
550845aeab5SMark Brown 	.irq_mask	= twl4030_sih_mask,
551845aeab5SMark Brown 	.irq_unmask	= twl4030_sih_unmask,
552845aeab5SMark Brown 	.irq_set_type	= twl4030_sih_set_type,
55391e3569fSFelipe Balbi 	.irq_bus_lock	= twl4030_sih_bus_lock,
55491e3569fSFelipe Balbi 	.irq_bus_sync_unlock = twl4030_sih_bus_sync_unlock,
55555098ff7SKevin Hilman 	.flags		= IRQCHIP_SKIP_SET_WAKE,
556a30d46c0SDavid Brownell };
557a30d46c0SDavid Brownell 
558a30d46c0SDavid Brownell /*----------------------------------------------------------------------*/
559a30d46c0SDavid Brownell 
560a30d46c0SDavid Brownell static inline int sih_read_isr(const struct sih *sih)
561a30d46c0SDavid Brownell {
562a30d46c0SDavid Brownell 	int status;
563a30d46c0SDavid Brownell 	union {
564a30d46c0SDavid Brownell 		u8 bytes[4];
565b174015bSLee Jones 		__le32 word;
566a30d46c0SDavid Brownell 	} isr;
567a30d46c0SDavid Brownell 
568a30d46c0SDavid Brownell 	/* FIXME need retry-on-error ... */
569a30d46c0SDavid Brownell 
570a30d46c0SDavid Brownell 	isr.word = 0;
571fc7b92fcSBalaji T K 	status = twl_i2c_read(sih->module, isr.bytes,
572a30d46c0SDavid Brownell 			sih->mask[irq_line].isr_offset, sih->bytes_ixr);
573a30d46c0SDavid Brownell 
574a30d46c0SDavid Brownell 	return (status < 0) ? status : le32_to_cpu(isr.word);
575a30d46c0SDavid Brownell }
576a30d46c0SDavid Brownell 
577a30d46c0SDavid Brownell /*
578a30d46c0SDavid Brownell  * Generic handler for SIH interrupts ... we "know" this is called
579a30d46c0SDavid Brownell  * in task context, with IRQs enabled.
580a30d46c0SDavid Brownell  */
581c1e61bcfSNeilBrown static irqreturn_t handle_twl4030_sih(int irq, void *data)
582a30d46c0SDavid Brownell {
583d5bb1221SThomas Gleixner 	struct sih_agent *agent = irq_get_handler_data(irq);
584a30d46c0SDavid Brownell 	const struct sih *sih = agent->sih;
585a30d46c0SDavid Brownell 	int isr;
586a30d46c0SDavid Brownell 
587a30d46c0SDavid Brownell 	/* reading ISR acks the IRQs, using clear-on-read mode */
588a30d46c0SDavid Brownell 	isr = sih_read_isr(sih);
589a30d46c0SDavid Brownell 
590a30d46c0SDavid Brownell 	if (isr < 0) {
591a30d46c0SDavid Brownell 		pr_err("twl4030: %s SIH, read ISR error %d\n",
592a30d46c0SDavid Brownell 			sih->name, isr);
593a30d46c0SDavid Brownell 		/* REVISIT:  recover; eventually mask it all, etc */
594c1e61bcfSNeilBrown 		return IRQ_HANDLED;
595a30d46c0SDavid Brownell 	}
596a30d46c0SDavid Brownell 
597a30d46c0SDavid Brownell 	while (isr) {
598a30d46c0SDavid Brownell 		irq = fls(isr);
599a30d46c0SDavid Brownell 		irq--;
600a30d46c0SDavid Brownell 		isr &= ~BIT(irq);
601a30d46c0SDavid Brownell 
602a30d46c0SDavid Brownell 		if (irq < sih->bits)
603925e853cSFelipe Balbi 			handle_nested_irq(agent->irq_base + irq);
604a30d46c0SDavid Brownell 		else
605a30d46c0SDavid Brownell 			pr_err("twl4030: %s SIH, invalid ISR bit %d\n",
606a30d46c0SDavid Brownell 				sih->name, irq);
607a30d46c0SDavid Brownell 	}
608c1e61bcfSNeilBrown 	return IRQ_HANDLED;
609a30d46c0SDavid Brownell }
610a30d46c0SDavid Brownell 
611cbcde05eSFelipe Contreras /* returns the first IRQ used by this SIH bank, or negative errno */
612f01b1f90SBenoit Cousson int twl4030_sih_setup(struct device *dev, int module, int irq_base)
613a30d46c0SDavid Brownell {
614a30d46c0SDavid Brownell 	int			sih_mod;
615a30d46c0SDavid Brownell 	const struct sih	*sih = NULL;
616a30d46c0SDavid Brownell 	struct sih_agent	*agent;
617a30d46c0SDavid Brownell 	int			i, irq;
618a30d46c0SDavid Brownell 	int			status = -EINVAL;
619a30d46c0SDavid Brownell 
620a30d46c0SDavid Brownell 	/* only support modules with standard clear-on-read for now */
621ec1a07b3SBenoit Cousson 	for (sih_mod = 0, sih = sih_modules; sih_mod < nr_sih_modules;
622a30d46c0SDavid Brownell 			sih_mod++, sih++) {
623a30d46c0SDavid Brownell 		if (sih->module == module && sih->set_cor) {
624a30d46c0SDavid Brownell 			status = 0;
625a30d46c0SDavid Brownell 			break;
626a30d46c0SDavid Brownell 		}
627a30d46c0SDavid Brownell 	}
628ec1a07b3SBenoit Cousson 
62948585739SUwe Kleine-König 	if (status < 0) {
63048585739SUwe Kleine-König 		dev_err(dev, "module to setup SIH for not found\n");
631a30d46c0SDavid Brownell 		return status;
63248585739SUwe Kleine-König 	}
633a30d46c0SDavid Brownell 
63404aa4438SLee Jones 	agent = kzalloc(sizeof(*agent), GFP_KERNEL);
635a30d46c0SDavid Brownell 	if (!agent)
636a30d46c0SDavid Brownell 		return -ENOMEM;
637a30d46c0SDavid Brownell 
638a30d46c0SDavid Brownell 	agent->irq_base = irq_base;
639a30d46c0SDavid Brownell 	agent->sih = sih;
640a30d46c0SDavid Brownell 	agent->imr = ~0;
64191e3569fSFelipe Balbi 	mutex_init(&agent->irq_lock);
642a30d46c0SDavid Brownell 
643a30d46c0SDavid Brownell 	for (i = 0; i < sih->bits; i++) {
644a30d46c0SDavid Brownell 		irq = irq_base + i;
645a30d46c0SDavid Brownell 
64691e3569fSFelipe Balbi 		irq_set_chip_data(irq, agent);
647d5bb1221SThomas Gleixner 		irq_set_chip_and_handler(irq, &twl4030_sih_irq_chip,
648a30d46c0SDavid Brownell 					 handle_edge_irq);
649b18d1f0fSNeilBrown 		irq_set_nested_thread(irq, 1);
650a30d46c0SDavid Brownell 		activate_irq(irq);
651a30d46c0SDavid Brownell 	}
652a30d46c0SDavid Brownell 
653a30d46c0SDavid Brownell 	/* replace generic PIH handler (handle_simple_irq) */
654a30d46c0SDavid Brownell 	irq = sih_mod + twl4030_irq_base;
655d5bb1221SThomas Gleixner 	irq_set_handler_data(irq, agent);
656c1e61bcfSNeilBrown 	agent->irq_name = kasprintf(GFP_KERNEL, "twl4030_%s", sih->name);
6578b41669cSKalle Jokiniemi 	status = request_threaded_irq(irq, NULL, handle_twl4030_sih,
6587d5b1ed8SFabio Estevam 				      IRQF_EARLY_RESUME | IRQF_ONESHOT,
659c1e61bcfSNeilBrown 				      agent->irq_name ?: sih->name, NULL);
660a30d46c0SDavid Brownell 
661ec1a07b3SBenoit Cousson 	dev_info(dev, "%s (irq %d) chaining IRQs %d..%d\n", sih->name,
662f01b1f90SBenoit Cousson 			irq, irq_base, irq_base + i - 1);
663a30d46c0SDavid Brownell 
664c1e61bcfSNeilBrown 	return status < 0 ? status : irq_base;
665a30d46c0SDavid Brownell }
666a30d46c0SDavid Brownell 
667a30d46c0SDavid Brownell /* FIXME need a call to reverse twl4030_sih_setup() ... */
668a30d46c0SDavid Brownell 
669a30d46c0SDavid Brownell /*----------------------------------------------------------------------*/
670a30d46c0SDavid Brownell 
671a30d46c0SDavid Brownell /* FIXME pass in which interrupt line we'll use ... */
672a30d46c0SDavid Brownell #define twl_irq_line	0
673a30d46c0SDavid Brownell 
67478518ffaSBenoit Cousson int twl4030_init_irq(struct device *dev, int irq_num)
675a30d46c0SDavid Brownell {
676a30d46c0SDavid Brownell 	static struct irq_chip	twl4030_irq_chip;
677ec1a07b3SBenoit Cousson 	int			status, i;
67878518ffaSBenoit Cousson 	int			irq_base, irq_end, nr_irqs;
67978518ffaSBenoit Cousson 	struct			device_node *node = dev->of_node;
680a30d46c0SDavid Brownell 
681a30d46c0SDavid Brownell 	/*
68278518ffaSBenoit Cousson 	 * TWL core and pwr interrupts must be contiguous because
68378518ffaSBenoit Cousson 	 * the hwirqs numbers are defined contiguously from 1 to 15.
68478518ffaSBenoit Cousson 	 * Create only one domain for both.
68578518ffaSBenoit Cousson 	 */
68678518ffaSBenoit Cousson 	nr_irqs = TWL4030_PWR_NR_IRQS + TWL4030_CORE_NR_IRQS;
68778518ffaSBenoit Cousson 
68878518ffaSBenoit Cousson 	irq_base = irq_alloc_descs(-1, 0, nr_irqs, 0);
689287980e4SArnd Bergmann 	if (irq_base < 0) {
69078518ffaSBenoit Cousson 		dev_err(dev, "Fail to allocate IRQ descs\n");
69178518ffaSBenoit Cousson 		return irq_base;
69278518ffaSBenoit Cousson 	}
69378518ffaSBenoit Cousson 
69478518ffaSBenoit Cousson 	irq_domain_add_legacy(node, nr_irqs, irq_base, 0,
69578518ffaSBenoit Cousson 			      &irq_domain_simple_ops, NULL);
69678518ffaSBenoit Cousson 
69778518ffaSBenoit Cousson 	irq_end = irq_base + TWL4030_CORE_NR_IRQS;
69878518ffaSBenoit Cousson 
69978518ffaSBenoit Cousson 	/*
700a30d46c0SDavid Brownell 	 * Mask and clear all TWL4030 interrupts since initially we do
701a30d46c0SDavid Brownell 	 * not have any TWL4030 module interrupt handlers present
702a30d46c0SDavid Brownell 	 */
703a30d46c0SDavid Brownell 	status = twl4030_init_sih_modules(twl_irq_line);
704a30d46c0SDavid Brownell 	if (status < 0)
705a30d46c0SDavid Brownell 		return status;
706a30d46c0SDavid Brownell 
707a30d46c0SDavid Brownell 	twl4030_irq_base = irq_base;
708a30d46c0SDavid Brownell 
709cbcde05eSFelipe Contreras 	/*
710ec1a07b3SBenoit Cousson 	 * Install an irq handler for each of the SIH modules;
711a30d46c0SDavid Brownell 	 * clone dummy irq_chip since PIH can't *do* anything
712a30d46c0SDavid Brownell 	 */
713a30d46c0SDavid Brownell 	twl4030_irq_chip = dummy_irq_chip;
714a30d46c0SDavid Brownell 	twl4030_irq_chip.name = "twl4030";
715a30d46c0SDavid Brownell 
716fe212213SThomas Gleixner 	twl4030_sih_irq_chip.irq_ack = dummy_irq_chip.irq_ack;
717a30d46c0SDavid Brownell 
718a30d46c0SDavid Brownell 	for (i = irq_base; i < irq_end; i++) {
719d5bb1221SThomas Gleixner 		irq_set_chip_and_handler(i, &twl4030_irq_chip,
720a30d46c0SDavid Brownell 					 handle_simple_irq);
721925e853cSFelipe Balbi 		irq_set_nested_thread(i, 1);
722a30d46c0SDavid Brownell 		activate_irq(i);
723a30d46c0SDavid Brownell 	}
724f01b1f90SBenoit Cousson 
725ec1a07b3SBenoit Cousson 	dev_info(dev, "%s (irq %d) chaining IRQs %d..%d\n", "PIH",
726f01b1f90SBenoit Cousson 			irq_num, irq_base, irq_end);
727a30d46c0SDavid Brownell 
728a30d46c0SDavid Brownell 	/* ... and the PWR_INT module ... */
729f01b1f90SBenoit Cousson 	status = twl4030_sih_setup(dev, TWL4030_MODULE_INT, irq_end);
730a30d46c0SDavid Brownell 	if (status < 0) {
731ec1a07b3SBenoit Cousson 		dev_err(dev, "sih_setup PWR INT --> %d\n", status);
732a30d46c0SDavid Brownell 		goto fail;
733a30d46c0SDavid Brownell 	}
734a30d46c0SDavid Brownell 
735a30d46c0SDavid Brownell 	/* install an irq handler to demultiplex the TWL4030 interrupt */
736286f8f3cSNeilBrown 	status = request_threaded_irq(irq_num, NULL, handle_twl4030_pih,
737286f8f3cSNeilBrown 				      IRQF_ONESHOT,
738a980bf73SSamuel Ortiz 				      "TWL4030-PIH", NULL);
7391cef8e41SRussell King 	if (status < 0) {
740ec1a07b3SBenoit Cousson 		dev_err(dev, "could not claim irq%d: %d\n", irq_num, status);
7411cef8e41SRussell King 		goto fail_rqirq;
742a30d46c0SDavid Brownell 	}
7435a2f1b5fSNeilBrown 	enable_irq_wake(irq_num);
744a30d46c0SDavid Brownell 
74578518ffaSBenoit Cousson 	return irq_base;
7461cef8e41SRussell King fail_rqirq:
7471cef8e41SRussell King 	/* clean up twl4030_sih_setup */
748a30d46c0SDavid Brownell fail:
749925e853cSFelipe Balbi 	for (i = irq_base; i < irq_end; i++) {
750925e853cSFelipe Balbi 		irq_set_nested_thread(i, 0);
751d5bb1221SThomas Gleixner 		irq_set_chip_and_handler(i, NULL, NULL);
752925e853cSFelipe Balbi 	}
7532f2a7d5eSFelipe Balbi 
754a30d46c0SDavid Brownell 	return status;
755a30d46c0SDavid Brownell }
756a30d46c0SDavid Brownell 
757724c3be3SUwe Kleine-König void twl4030_exit_irq(void)
758a30d46c0SDavid Brownell {
759a30d46c0SDavid Brownell 	/* FIXME undo twl_init_irq() */
760724c3be3SUwe Kleine-König 	if (twl4030_irq_base)
761a30d46c0SDavid Brownell 		pr_err("twl4030: can't yet clean up IRQs?\n");
762a30d46c0SDavid Brownell }
7631920a61eSIlkka Koskinen 
764e8deb28cSBalaji T K int twl4030_init_chip_irq(const char *chip)
7651920a61eSIlkka Koskinen {
7661920a61eSIlkka Koskinen 	if (!strcmp(chip, "twl5031")) {
7671920a61eSIlkka Koskinen 		sih_modules = sih_modules_twl5031;
7681920a61eSIlkka Koskinen 		nr_sih_modules = ARRAY_SIZE(sih_modules_twl5031);
7691920a61eSIlkka Koskinen 	} else {
7701920a61eSIlkka Koskinen 		sih_modules = sih_modules_twl4030;
7711920a61eSIlkka Koskinen 		nr_sih_modules = ARRAY_SIZE(sih_modules_twl4030);
7721920a61eSIlkka Koskinen 	}
7731920a61eSIlkka Koskinen 
7741920a61eSIlkka Koskinen 	return 0;
7751920a61eSIlkka Koskinen }
776