xref: /openbmc/linux/drivers/mfd/twl4030-irq.c (revision 1a59d1b8e05ea6ab45f7e18897de1ef0e6bc3da6)
1*1a59d1b8SThomas 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 
1778518ffaSBenoit Cousson #include <linux/export.h>
18a30d46c0SDavid Brownell #include <linux/interrupt.h>
19a30d46c0SDavid Brownell #include <linux/irq.h>
205a0e3ad6STejun Heo #include <linux/slab.h>
2178518ffaSBenoit Cousson #include <linux/of.h>
2278518ffaSBenoit Cousson #include <linux/irqdomain.h>
23a2054256SWolfram Sang #include <linux/mfd/twl.h>
24a30d46c0SDavid Brownell 
25b0b4a7c2SG, Manjunath Kondaiah #include "twl-core.h"
26a30d46c0SDavid Brownell 
27a30d46c0SDavid Brownell /*
28a30d46c0SDavid Brownell  * TWL4030 IRQ handling has two stages in hardware, and thus in software.
29a30d46c0SDavid Brownell  * The Primary Interrupt Handler (PIH) stage exposes status bits saying
30a30d46c0SDavid Brownell  * which Secondary Interrupt Handler (SIH) stage is raising an interrupt.
31a30d46c0SDavid Brownell  * SIH modules are more traditional IRQ components, which support per-IRQ
32a30d46c0SDavid Brownell  * enable/disable and trigger controls; they do most of the work.
33a30d46c0SDavid Brownell  *
34a30d46c0SDavid Brownell  * These chips are designed to support IRQ handling from two different
35a30d46c0SDavid Brownell  * I2C masters.  Each has a dedicated IRQ line, and dedicated IRQ status
36a30d46c0SDavid Brownell  * and mask registers in the PIH and SIH modules.
37a30d46c0SDavid Brownell  *
38a30d46c0SDavid Brownell  * We set up IRQs starting at a platform-specified base, always starting
39a30d46c0SDavid Brownell  * with PIH and the SIH for PWR_INT and then usually adding GPIO:
40a30d46c0SDavid Brownell  *	base + 0  .. base + 7	PIH
41a30d46c0SDavid Brownell  *	base + 8  .. base + 15	SIH for PWR_INT
42a30d46c0SDavid Brownell  *	base + 16 .. base + 33	SIH for GPIO
43a30d46c0SDavid Brownell  */
4478518ffaSBenoit Cousson #define TWL4030_CORE_NR_IRQS	8
4578518ffaSBenoit Cousson #define TWL4030_PWR_NR_IRQS	8
46a30d46c0SDavid Brownell 
47a30d46c0SDavid Brownell /* PIH register offsets */
48a30d46c0SDavid Brownell #define REG_PIH_ISR_P1			0x01
49a30d46c0SDavid Brownell #define REG_PIH_ISR_P2			0x02
50a30d46c0SDavid Brownell #define REG_PIH_SIR			0x03	/* for testing */
51a30d46c0SDavid Brownell 
52a30d46c0SDavid Brownell /* Linux could (eventually) use either IRQ line */
53a30d46c0SDavid Brownell static int irq_line;
54a30d46c0SDavid Brownell 
55a30d46c0SDavid Brownell struct sih {
56a30d46c0SDavid Brownell 	char	name[8];
57a30d46c0SDavid Brownell 	u8	module;			/* module id */
58a30d46c0SDavid Brownell 	u8	control_offset;		/* for SIH_CTRL */
59a30d46c0SDavid Brownell 	bool	set_cor;
60a30d46c0SDavid Brownell 
61a30d46c0SDavid Brownell 	u8	bits;			/* valid in isr/imr */
62a30d46c0SDavid Brownell 	u8	bytes_ixr;		/* bytelen of ISR/IMR/SIR */
63a30d46c0SDavid Brownell 
64a30d46c0SDavid Brownell 	u8	edr_offset;
65a30d46c0SDavid Brownell 	u8	bytes_edr;		/* bytelen of EDR */
66a30d46c0SDavid Brownell 
671920a61eSIlkka Koskinen 	u8	irq_lines;		/* number of supported irq lines */
681920a61eSIlkka Koskinen 
69a30d46c0SDavid Brownell 	/* SIR ignored -- set interrupt, for testing only */
7035a27e8eSThomas Gleixner 	struct sih_irq_data {
71a30d46c0SDavid Brownell 		u8	isr_offset;
72a30d46c0SDavid Brownell 		u8	imr_offset;
73a30d46c0SDavid Brownell 	} mask[2];
74a30d46c0SDavid Brownell 	/* + 2 bytes padding */
75a30d46c0SDavid Brownell };
76a30d46c0SDavid Brownell 
771920a61eSIlkka Koskinen static const struct sih *sih_modules;
781920a61eSIlkka Koskinen static int nr_sih_modules;
791920a61eSIlkka Koskinen 
80a30d46c0SDavid Brownell #define SIH_INITIALIZER(modname, nbits) \
81a30d46c0SDavid Brownell 	.module		= TWL4030_MODULE_ ## modname, \
82a30d46c0SDavid Brownell 	.control_offset = TWL4030_ ## modname ## _SIH_CTRL, \
83a30d46c0SDavid Brownell 	.bits		= nbits, \
84a30d46c0SDavid Brownell 	.bytes_ixr	= DIV_ROUND_UP(nbits, 8), \
85a30d46c0SDavid Brownell 	.edr_offset	= TWL4030_ ## modname ## _EDR, \
86a30d46c0SDavid Brownell 	.bytes_edr	= DIV_ROUND_UP((2*(nbits)), 8), \
871920a61eSIlkka Koskinen 	.irq_lines	= 2, \
88a30d46c0SDavid Brownell 	.mask = { { \
89a30d46c0SDavid Brownell 		.isr_offset	= TWL4030_ ## modname ## _ISR1, \
90a30d46c0SDavid Brownell 		.imr_offset	= TWL4030_ ## modname ## _IMR1, \
91a30d46c0SDavid Brownell 	}, \
92a30d46c0SDavid Brownell 	{ \
93a30d46c0SDavid Brownell 		.isr_offset	= TWL4030_ ## modname ## _ISR2, \
94a30d46c0SDavid Brownell 		.imr_offset	= TWL4030_ ## modname ## _IMR2, \
95a30d46c0SDavid Brownell 	}, },
96a30d46c0SDavid Brownell 
97a30d46c0SDavid Brownell /* register naming policies are inconsistent ... */
98a30d46c0SDavid Brownell #define TWL4030_INT_PWR_EDR		TWL4030_INT_PWR_EDR1
99a30d46c0SDavid Brownell #define TWL4030_MODULE_KEYPAD_KEYP	TWL4030_MODULE_KEYPAD
100a30d46c0SDavid Brownell #define TWL4030_MODULE_INT_PWR		TWL4030_MODULE_INT
101a30d46c0SDavid Brownell 
102a30d46c0SDavid Brownell 
103cbcde05eSFelipe Contreras /*
104cbcde05eSFelipe Contreras  * Order in this table matches order in PIH_ISR.  That is,
105a30d46c0SDavid Brownell  * BIT(n) in PIH_ISR is sih_modules[n].
106a30d46c0SDavid Brownell  */
1071920a61eSIlkka Koskinen /* sih_modules_twl4030 is used both in twl4030 and twl5030 */
1081920a61eSIlkka Koskinen static const struct sih sih_modules_twl4030[6] = {
109a30d46c0SDavid Brownell 	[0] = {
110a30d46c0SDavid Brownell 		.name		= "gpio",
111a30d46c0SDavid Brownell 		.module		= TWL4030_MODULE_GPIO,
112a30d46c0SDavid Brownell 		.control_offset	= REG_GPIO_SIH_CTRL,
113a30d46c0SDavid Brownell 		.set_cor	= true,
114a30d46c0SDavid Brownell 		.bits		= TWL4030_GPIO_MAX,
115a30d46c0SDavid Brownell 		.bytes_ixr	= 3,
116a30d46c0SDavid Brownell 		/* Note: *all* of these IRQs default to no-trigger */
117a30d46c0SDavid Brownell 		.edr_offset	= REG_GPIO_EDR1,
118a30d46c0SDavid Brownell 		.bytes_edr	= 5,
1191920a61eSIlkka Koskinen 		.irq_lines	= 2,
120a30d46c0SDavid Brownell 		.mask = { {
121a30d46c0SDavid Brownell 			.isr_offset	= REG_GPIO_ISR1A,
122a30d46c0SDavid Brownell 			.imr_offset	= REG_GPIO_IMR1A,
123a30d46c0SDavid Brownell 		}, {
124a30d46c0SDavid Brownell 			.isr_offset	= REG_GPIO_ISR1B,
125a30d46c0SDavid Brownell 			.imr_offset	= REG_GPIO_IMR1B,
126a30d46c0SDavid Brownell 		}, },
127a30d46c0SDavid Brownell 	},
128a30d46c0SDavid Brownell 	[1] = {
129a30d46c0SDavid Brownell 		.name		= "keypad",
130a30d46c0SDavid Brownell 		.set_cor	= true,
131a30d46c0SDavid Brownell 		SIH_INITIALIZER(KEYPAD_KEYP, 4)
132a30d46c0SDavid Brownell 	},
133a30d46c0SDavid Brownell 	[2] = {
134a30d46c0SDavid Brownell 		.name		= "bci",
135a30d46c0SDavid Brownell 		.module		= TWL4030_MODULE_INTERRUPTS,
136a30d46c0SDavid Brownell 		.control_offset	= TWL4030_INTERRUPTS_BCISIHCTRL,
1378e52e279SGrazvydas Ignotas 		.set_cor	= true,
138a30d46c0SDavid Brownell 		.bits		= 12,
139a30d46c0SDavid Brownell 		.bytes_ixr	= 2,
140a30d46c0SDavid Brownell 		.edr_offset	= TWL4030_INTERRUPTS_BCIEDR1,
141a30d46c0SDavid Brownell 		/* Note: most of these IRQs default to no-trigger */
142a30d46c0SDavid Brownell 		.bytes_edr	= 3,
1431920a61eSIlkka Koskinen 		.irq_lines	= 2,
144a30d46c0SDavid Brownell 		.mask = { {
145a30d46c0SDavid Brownell 			.isr_offset	= TWL4030_INTERRUPTS_BCIISR1A,
146a30d46c0SDavid Brownell 			.imr_offset	= TWL4030_INTERRUPTS_BCIIMR1A,
147a30d46c0SDavid Brownell 		}, {
148a30d46c0SDavid Brownell 			.isr_offset	= TWL4030_INTERRUPTS_BCIISR1B,
149a30d46c0SDavid Brownell 			.imr_offset	= TWL4030_INTERRUPTS_BCIIMR1B,
150a30d46c0SDavid Brownell 		}, },
151a30d46c0SDavid Brownell 	},
152a30d46c0SDavid Brownell 	[3] = {
153a30d46c0SDavid Brownell 		.name		= "madc",
154a30d46c0SDavid Brownell 		SIH_INITIALIZER(MADC, 4)
155a30d46c0SDavid Brownell 	},
156a30d46c0SDavid Brownell 	[4] = {
157a30d46c0SDavid Brownell 		/* USB doesn't use the same SIH organization */
158a30d46c0SDavid Brownell 		.name		= "usb",
159a30d46c0SDavid Brownell 	},
160a30d46c0SDavid Brownell 	[5] = {
161a30d46c0SDavid Brownell 		.name		= "power",
162a30d46c0SDavid Brownell 		.set_cor	= true,
163a30d46c0SDavid Brownell 		SIH_INITIALIZER(INT_PWR, 8)
164a30d46c0SDavid Brownell 	},
165a30d46c0SDavid Brownell 		/* there are no SIH modules #6 or #7 ... */
166a30d46c0SDavid Brownell };
167a30d46c0SDavid Brownell 
1681920a61eSIlkka Koskinen static const struct sih sih_modules_twl5031[8] = {
1691920a61eSIlkka Koskinen 	[0] = {
1701920a61eSIlkka Koskinen 		.name		= "gpio",
1711920a61eSIlkka Koskinen 		.module		= TWL4030_MODULE_GPIO,
1721920a61eSIlkka Koskinen 		.control_offset	= REG_GPIO_SIH_CTRL,
1731920a61eSIlkka Koskinen 		.set_cor	= true,
1741920a61eSIlkka Koskinen 		.bits		= TWL4030_GPIO_MAX,
1751920a61eSIlkka Koskinen 		.bytes_ixr	= 3,
1761920a61eSIlkka Koskinen 		/* Note: *all* of these IRQs default to no-trigger */
1771920a61eSIlkka Koskinen 		.edr_offset	= REG_GPIO_EDR1,
1781920a61eSIlkka Koskinen 		.bytes_edr	= 5,
1791920a61eSIlkka Koskinen 		.irq_lines	= 2,
1801920a61eSIlkka Koskinen 		.mask = { {
1811920a61eSIlkka Koskinen 			.isr_offset	= REG_GPIO_ISR1A,
1821920a61eSIlkka Koskinen 			.imr_offset	= REG_GPIO_IMR1A,
1831920a61eSIlkka Koskinen 		}, {
1841920a61eSIlkka Koskinen 			.isr_offset	= REG_GPIO_ISR1B,
1851920a61eSIlkka Koskinen 			.imr_offset	= REG_GPIO_IMR1B,
1861920a61eSIlkka Koskinen 		}, },
1871920a61eSIlkka Koskinen 	},
1881920a61eSIlkka Koskinen 	[1] = {
1891920a61eSIlkka Koskinen 		.name		= "keypad",
1901920a61eSIlkka Koskinen 		.set_cor	= true,
1911920a61eSIlkka Koskinen 		SIH_INITIALIZER(KEYPAD_KEYP, 4)
1921920a61eSIlkka Koskinen 	},
1931920a61eSIlkka Koskinen 	[2] = {
1941920a61eSIlkka Koskinen 		.name		= "bci",
1951920a61eSIlkka Koskinen 		.module		= TWL5031_MODULE_INTERRUPTS,
1961920a61eSIlkka Koskinen 		.control_offset	= TWL5031_INTERRUPTS_BCISIHCTRL,
1971920a61eSIlkka Koskinen 		.bits		= 7,
1981920a61eSIlkka Koskinen 		.bytes_ixr	= 1,
1991920a61eSIlkka Koskinen 		.edr_offset	= TWL5031_INTERRUPTS_BCIEDR1,
2001920a61eSIlkka Koskinen 		/* Note: most of these IRQs default to no-trigger */
2011920a61eSIlkka Koskinen 		.bytes_edr	= 2,
2021920a61eSIlkka Koskinen 		.irq_lines	= 2,
2031920a61eSIlkka Koskinen 		.mask = { {
2041920a61eSIlkka Koskinen 			.isr_offset	= TWL5031_INTERRUPTS_BCIISR1,
2051920a61eSIlkka Koskinen 			.imr_offset	= TWL5031_INTERRUPTS_BCIIMR1,
2061920a61eSIlkka Koskinen 		}, {
2071920a61eSIlkka Koskinen 			.isr_offset	= TWL5031_INTERRUPTS_BCIISR2,
2081920a61eSIlkka Koskinen 			.imr_offset	= TWL5031_INTERRUPTS_BCIIMR2,
2091920a61eSIlkka Koskinen 		}, },
2101920a61eSIlkka Koskinen 	},
2111920a61eSIlkka Koskinen 	[3] = {
2121920a61eSIlkka Koskinen 		.name		= "madc",
2131920a61eSIlkka Koskinen 		SIH_INITIALIZER(MADC, 4)
2141920a61eSIlkka Koskinen 	},
2151920a61eSIlkka Koskinen 	[4] = {
2161920a61eSIlkka Koskinen 		/* USB doesn't use the same SIH organization */
2171920a61eSIlkka Koskinen 		.name		= "usb",
2181920a61eSIlkka Koskinen 	},
2191920a61eSIlkka Koskinen 	[5] = {
2201920a61eSIlkka Koskinen 		.name		= "power",
2211920a61eSIlkka Koskinen 		.set_cor	= true,
2221920a61eSIlkka Koskinen 		SIH_INITIALIZER(INT_PWR, 8)
2231920a61eSIlkka Koskinen 	},
2241920a61eSIlkka Koskinen 	[6] = {
2251920a61eSIlkka Koskinen 		/*
226191211f5SIlkka Koskinen 		 * ECI/DBI doesn't use the same SIH organization.
227191211f5SIlkka Koskinen 		 * For example, it supports only one interrupt output line.
228191211f5SIlkka Koskinen 		 * That is, the interrupts are seen on both INT1 and INT2 lines.
2291920a61eSIlkka Koskinen 		 */
230191211f5SIlkka Koskinen 		.name		= "eci_dbi",
2311920a61eSIlkka Koskinen 		.module		= TWL5031_MODULE_ACCESSORY,
2321920a61eSIlkka Koskinen 		.bits		= 9,
2331920a61eSIlkka Koskinen 		.bytes_ixr	= 2,
2341920a61eSIlkka Koskinen 		.irq_lines	= 1,
2351920a61eSIlkka Koskinen 		.mask = { {
2361920a61eSIlkka Koskinen 			.isr_offset	= TWL5031_ACIIDR_LSB,
2371920a61eSIlkka Koskinen 			.imr_offset	= TWL5031_ACIIMR_LSB,
2381920a61eSIlkka Koskinen 		}, },
2391920a61eSIlkka Koskinen 
2401920a61eSIlkka Koskinen 	},
2411920a61eSIlkka Koskinen 	[7] = {
242191211f5SIlkka Koskinen 		/* Audio accessory */
243191211f5SIlkka Koskinen 		.name		= "audio",
2441920a61eSIlkka Koskinen 		.module		= TWL5031_MODULE_ACCESSORY,
2451920a61eSIlkka Koskinen 		.control_offset	= TWL5031_ACCSIHCTRL,
2461920a61eSIlkka Koskinen 		.bits		= 2,
2471920a61eSIlkka Koskinen 		.bytes_ixr	= 1,
2481920a61eSIlkka Koskinen 		.edr_offset	= TWL5031_ACCEDR1,
2491920a61eSIlkka Koskinen 		/* Note: most of these IRQs default to no-trigger */
2501920a61eSIlkka Koskinen 		.bytes_edr	= 1,
2511920a61eSIlkka Koskinen 		.irq_lines	= 2,
2521920a61eSIlkka Koskinen 		.mask = { {
2531920a61eSIlkka Koskinen 			.isr_offset	= TWL5031_ACCISR1,
2541920a61eSIlkka Koskinen 			.imr_offset	= TWL5031_ACCIMR1,
2551920a61eSIlkka Koskinen 		}, {
2561920a61eSIlkka Koskinen 			.isr_offset	= TWL5031_ACCISR2,
2571920a61eSIlkka Koskinen 			.imr_offset	= TWL5031_ACCIMR2,
2581920a61eSIlkka Koskinen 		}, },
2591920a61eSIlkka Koskinen 	},
2601920a61eSIlkka Koskinen };
2611920a61eSIlkka Koskinen 
262a30d46c0SDavid Brownell #undef TWL4030_MODULE_KEYPAD_KEYP
263a30d46c0SDavid Brownell #undef TWL4030_MODULE_INT_PWR
264a30d46c0SDavid Brownell #undef TWL4030_INT_PWR_EDR
265a30d46c0SDavid Brownell 
266a30d46c0SDavid Brownell /*----------------------------------------------------------------------*/
267a30d46c0SDavid Brownell 
268a30d46c0SDavid Brownell static unsigned twl4030_irq_base;
269a30d46c0SDavid Brownell 
270a30d46c0SDavid Brownell /*
271a30d46c0SDavid Brownell  * handle_twl4030_pih() is the desc->handle method for the twl4030 interrupt.
272a30d46c0SDavid Brownell  * This is a chained interrupt, so there is no desc->action method for it.
273a30d46c0SDavid Brownell  * Now we need to query the interrupt controller in the twl4030 to determine
274a30d46c0SDavid Brownell  * which module is generating the interrupt request.  However, we can't do i2c
275a30d46c0SDavid Brownell  * transactions in interrupt context, so we must defer that work to a kernel
276a30d46c0SDavid Brownell  * thread.  All we do here is acknowledge and mask the interrupt and wakeup
277a30d46c0SDavid Brownell  * the kernel thread.
278a30d46c0SDavid Brownell  */
2791cef8e41SRussell King static irqreturn_t handle_twl4030_pih(int irq, void *devid)
280a30d46c0SDavid Brownell {
2817750c9b0SFelipe Balbi 	irqreturn_t	ret;
2827750c9b0SFelipe Balbi 	u8		pih_isr;
2837750c9b0SFelipe Balbi 
2846fbc6420SPeter Ujfalusi 	ret = twl_i2c_read_u8(TWL_MODULE_PIH, &pih_isr,
2857750c9b0SFelipe Balbi 			      REG_PIH_ISR_P1);
2867750c9b0SFelipe Balbi 	if (ret) {
28704aa4438SLee Jones 		pr_warn("twl4030: I2C error %d reading PIH ISR\n", ret);
2887750c9b0SFelipe Balbi 		return IRQ_NONE;
2897750c9b0SFelipe Balbi 	}
2907750c9b0SFelipe Balbi 
2915a903090SFelipe Balbi 	while (pih_isr) {
2925a903090SFelipe Balbi 		unsigned long	pending = __ffs(pih_isr);
2935a903090SFelipe Balbi 		unsigned int	irq;
2945a903090SFelipe Balbi 
2955a903090SFelipe Balbi 		pih_isr &= ~BIT(pending);
2965a903090SFelipe Balbi 		irq = pending + twl4030_irq_base;
2975a903090SFelipe Balbi 		handle_nested_irq(irq);
2987750c9b0SFelipe Balbi 	}
2997750c9b0SFelipe Balbi 
3001cef8e41SRussell King 	return IRQ_HANDLED;
301a30d46c0SDavid Brownell }
302cbcde05eSFelipe Contreras 
303a30d46c0SDavid Brownell /*----------------------------------------------------------------------*/
304a30d46c0SDavid Brownell 
305a30d46c0SDavid Brownell /*
306a30d46c0SDavid Brownell  * twl4030_init_sih_modules() ... start from a known state where no
307a30d46c0SDavid Brownell  * IRQs will be coming in, and where we can quickly enable them then
308a30d46c0SDavid Brownell  * handle them as they arrive.  Mask all IRQs: maybe init SIH_CTRL.
309a30d46c0SDavid Brownell  *
310a30d46c0SDavid Brownell  * NOTE:  we don't touch EDR registers here; they stay with hardware
311a30d46c0SDavid Brownell  * defaults or whatever the last value was.  Note that when both EDR
312a30d46c0SDavid Brownell  * bits for an IRQ are clear, that's as if its IMR bit is set...
313a30d46c0SDavid Brownell  */
314a30d46c0SDavid Brownell static int twl4030_init_sih_modules(unsigned line)
315a30d46c0SDavid Brownell {
316a30d46c0SDavid Brownell 	const struct sih *sih;
317a30d46c0SDavid Brownell 	u8 buf[4];
318a30d46c0SDavid Brownell 	int i;
319a30d46c0SDavid Brownell 	int status;
320a30d46c0SDavid Brownell 
321a30d46c0SDavid Brownell 	/* line 0 == int1_n signal; line 1 == int2_n signal */
322a30d46c0SDavid Brownell 	if (line > 1)
323a30d46c0SDavid Brownell 		return -EINVAL;
324a30d46c0SDavid Brownell 
325a30d46c0SDavid Brownell 	irq_line = line;
326a30d46c0SDavid Brownell 
327a30d46c0SDavid Brownell 	/* disable all interrupts on our line */
32804aa4438SLee Jones 	memset(buf, 0xff, sizeof(buf));
329a30d46c0SDavid Brownell 	sih = sih_modules;
3301920a61eSIlkka Koskinen 	for (i = 0; i < nr_sih_modules; i++, sih++) {
331a30d46c0SDavid Brownell 		/* skip USB -- it's funky */
332a30d46c0SDavid Brownell 		if (!sih->bytes_ixr)
333a30d46c0SDavid Brownell 			continue;
334a30d46c0SDavid Brownell 
3351920a61eSIlkka Koskinen 		/* Not all the SIH modules support multiple interrupt lines */
3361920a61eSIlkka Koskinen 		if (sih->irq_lines <= line)
3371920a61eSIlkka Koskinen 			continue;
3381920a61eSIlkka Koskinen 
339fc7b92fcSBalaji T K 		status = twl_i2c_write(sih->module, buf,
340a30d46c0SDavid Brownell 				sih->mask[line].imr_offset, sih->bytes_ixr);
341a30d46c0SDavid Brownell 		if (status < 0)
342a30d46c0SDavid Brownell 			pr_err("twl4030: err %d initializing %s %s\n",
343a30d46c0SDavid Brownell 					status, sih->name, "IMR");
344a30d46c0SDavid Brownell 
345cbcde05eSFelipe Contreras 		/*
346cbcde05eSFelipe Contreras 		 * Maybe disable "exclusive" mode; buffer second pending irq;
347a30d46c0SDavid Brownell 		 * set Clear-On-Read (COR) bit.
348a30d46c0SDavid Brownell 		 *
349a30d46c0SDavid Brownell 		 * NOTE that sometimes COR polarity is documented as being
3508e52e279SGrazvydas Ignotas 		 * inverted:  for MADC, COR=1 means "clear on write".
351a30d46c0SDavid Brownell 		 * And for PWR_INT it's not documented...
352a30d46c0SDavid Brownell 		 */
353a30d46c0SDavid Brownell 		if (sih->set_cor) {
354fc7b92fcSBalaji T K 			status = twl_i2c_write_u8(sih->module,
355a30d46c0SDavid Brownell 					TWL4030_SIH_CTRL_COR_MASK,
356a30d46c0SDavid Brownell 					sih->control_offset);
357a30d46c0SDavid Brownell 			if (status < 0)
358a30d46c0SDavid Brownell 				pr_err("twl4030: err %d initializing %s %s\n",
359a30d46c0SDavid Brownell 						status, sih->name, "SIH_CTRL");
360a30d46c0SDavid Brownell 		}
361a30d46c0SDavid Brownell 	}
362a30d46c0SDavid Brownell 
363a30d46c0SDavid Brownell 	sih = sih_modules;
3641920a61eSIlkka Koskinen 	for (i = 0; i < nr_sih_modules; i++, sih++) {
365a30d46c0SDavid Brownell 		u8 rxbuf[4];
366a30d46c0SDavid Brownell 		int j;
367a30d46c0SDavid Brownell 
368a30d46c0SDavid Brownell 		/* skip USB */
369a30d46c0SDavid Brownell 		if (!sih->bytes_ixr)
370a30d46c0SDavid Brownell 			continue;
371a30d46c0SDavid Brownell 
3721920a61eSIlkka Koskinen 		/* Not all the SIH modules support multiple interrupt lines */
3731920a61eSIlkka Koskinen 		if (sih->irq_lines <= line)
3741920a61eSIlkka Koskinen 			continue;
3751920a61eSIlkka Koskinen 
376cbcde05eSFelipe Contreras 		/*
377cbcde05eSFelipe Contreras 		 * Clear pending interrupt status.  Either the read was
378a30d46c0SDavid Brownell 		 * enough, or we need to write those bits.  Repeat, in
379a30d46c0SDavid Brownell 		 * case an IRQ is pending (PENDDIS=0) ... that's not
380a30d46c0SDavid Brownell 		 * uncommon with PWR_INT.PWRON.
381a30d46c0SDavid Brownell 		 */
382a30d46c0SDavid Brownell 		for (j = 0; j < 2; j++) {
383fc7b92fcSBalaji T K 			status = twl_i2c_read(sih->module, rxbuf,
384a30d46c0SDavid Brownell 				sih->mask[line].isr_offset, sih->bytes_ixr);
385a30d46c0SDavid Brownell 			if (status < 0)
3868a012ff9SLee Jones 				pr_warn("twl4030: err %d initializing %s %s\n",
387a30d46c0SDavid Brownell 					status, sih->name, "ISR");
388a30d46c0SDavid Brownell 
3898a012ff9SLee Jones 			if (!sih->set_cor) {
390fc7b92fcSBalaji T K 				status = twl_i2c_write(sih->module, buf,
391a30d46c0SDavid Brownell 					sih->mask[line].isr_offset,
392a30d46c0SDavid Brownell 					sih->bytes_ixr);
3938a012ff9SLee Jones 				if (status < 0)
3948a012ff9SLee Jones 					pr_warn("twl4030: write failed: %d\n",
3958a012ff9SLee Jones 						status);
3968a012ff9SLee Jones 			}
397cbcde05eSFelipe Contreras 			/*
398cbcde05eSFelipe Contreras 			 * else COR=1 means read sufficed.
399a30d46c0SDavid Brownell 			 * (for most SIH modules...)
400a30d46c0SDavid Brownell 			 */
401a30d46c0SDavid Brownell 		}
402a30d46c0SDavid Brownell 	}
403a30d46c0SDavid Brownell 
404a30d46c0SDavid Brownell 	return 0;
405a30d46c0SDavid Brownell }
406a30d46c0SDavid Brownell 
407a30d46c0SDavid Brownell static inline void activate_irq(int irq)
408a30d46c0SDavid Brownell {
4099bd09f34SRob Herring 	irq_clear_status_flags(irq, IRQ_NOREQUEST | IRQ_NOPROBE);
410a30d46c0SDavid Brownell }
411a30d46c0SDavid Brownell 
412a30d46c0SDavid Brownell /*----------------------------------------------------------------------*/
413a30d46c0SDavid Brownell 
414a30d46c0SDavid Brownell struct sih_agent {
415a30d46c0SDavid Brownell 	int			irq_base;
416a30d46c0SDavid Brownell 	const struct sih	*sih;
417a30d46c0SDavid Brownell 
418a30d46c0SDavid Brownell 	u32			imr;
419a30d46c0SDavid Brownell 	bool			imr_change_pending;
420a30d46c0SDavid Brownell 
421a30d46c0SDavid Brownell 	u32			edge_change;
42291e3569fSFelipe Balbi 
42391e3569fSFelipe Balbi 	struct mutex		irq_lock;
424c1e61bcfSNeilBrown 	char			*irq_name;
425a30d46c0SDavid Brownell };
426a30d46c0SDavid Brownell 
427a30d46c0SDavid Brownell /*----------------------------------------------------------------------*/
428a30d46c0SDavid Brownell 
429a30d46c0SDavid Brownell /*
430a30d46c0SDavid Brownell  * All irq_chip methods get issued from code holding irq_desc[irq].lock,
431a30d46c0SDavid Brownell  * which can't perform the underlying I2C operations (because they sleep).
432a30d46c0SDavid Brownell  * So we must hand them off to a thread (workqueue) and cope with asynch
433a30d46c0SDavid Brownell  * completion, potentially including some re-ordering, of these requests.
434a30d46c0SDavid Brownell  */
435a30d46c0SDavid Brownell 
436845aeab5SMark Brown static void twl4030_sih_mask(struct irq_data *data)
437a30d46c0SDavid Brownell {
43884868424SFelipe Balbi 	struct sih_agent *agent = irq_data_get_irq_chip_data(data);
439a30d46c0SDavid Brownell 
44084868424SFelipe Balbi 	agent->imr |= BIT(data->irq - agent->irq_base);
44184868424SFelipe Balbi 	agent->imr_change_pending = true;
442a30d46c0SDavid Brownell }
443a30d46c0SDavid Brownell 
444845aeab5SMark Brown static void twl4030_sih_unmask(struct irq_data *data)
445a30d46c0SDavid Brownell {
44684868424SFelipe Balbi 	struct sih_agent *agent = irq_data_get_irq_chip_data(data);
447a30d46c0SDavid Brownell 
44884868424SFelipe Balbi 	agent->imr &= ~BIT(data->irq - agent->irq_base);
44984868424SFelipe Balbi 	agent->imr_change_pending = true;
450a30d46c0SDavid Brownell }
451a30d46c0SDavid Brownell 
452845aeab5SMark Brown static int twl4030_sih_set_type(struct irq_data *data, unsigned trigger)
453a30d46c0SDavid Brownell {
45484868424SFelipe Balbi 	struct sih_agent *agent = irq_data_get_irq_chip_data(data);
455a30d46c0SDavid Brownell 
456a30d46c0SDavid Brownell 	if (trigger & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
457a30d46c0SDavid Brownell 		return -EINVAL;
458a30d46c0SDavid Brownell 
4592f2a7d5eSFelipe Balbi 	if (irqd_get_trigger_type(data) != trigger)
46084868424SFelipe Balbi 		agent->edge_change |= BIT(data->irq - agent->irq_base);
46191e3569fSFelipe Balbi 
462a30d46c0SDavid Brownell 	return 0;
463a30d46c0SDavid Brownell }
464a30d46c0SDavid Brownell 
46591e3569fSFelipe Balbi static void twl4030_sih_bus_lock(struct irq_data *data)
46691e3569fSFelipe Balbi {
46784868424SFelipe Balbi 	struct sih_agent	*agent = irq_data_get_irq_chip_data(data);
46891e3569fSFelipe Balbi 
46984868424SFelipe Balbi 	mutex_lock(&agent->irq_lock);
47091e3569fSFelipe Balbi }
47191e3569fSFelipe Balbi 
47291e3569fSFelipe Balbi static void twl4030_sih_bus_sync_unlock(struct irq_data *data)
47391e3569fSFelipe Balbi {
47484868424SFelipe Balbi 	struct sih_agent	*agent = irq_data_get_irq_chip_data(data);
47584868424SFelipe Balbi 	const struct sih	*sih = agent->sih;
47684868424SFelipe Balbi 	int			status;
47791e3569fSFelipe Balbi 
47884868424SFelipe Balbi 	if (agent->imr_change_pending) {
47984868424SFelipe Balbi 		union {
48084868424SFelipe Balbi 			u32	word;
48184868424SFelipe Balbi 			u8	bytes[4];
48284868424SFelipe Balbi 		} imr;
48384868424SFelipe Balbi 
484c9531227SNeilBrown 		/* byte[0] gets overwritten as we write ... */
48514591d88SPeter Ujfalusi 		imr.word = cpu_to_le32(agent->imr);
48684868424SFelipe Balbi 		agent->imr_change_pending = false;
48784868424SFelipe Balbi 
48884868424SFelipe Balbi 		/* write the whole mask ... simpler than subsetting it */
48984868424SFelipe Balbi 		status = twl_i2c_write(sih->module, imr.bytes,
49084868424SFelipe Balbi 				sih->mask[irq_line].imr_offset,
49184868424SFelipe Balbi 				sih->bytes_ixr);
49284868424SFelipe Balbi 		if (status)
49384868424SFelipe Balbi 			pr_err("twl4030: %s, %s --> %d\n", __func__,
49484868424SFelipe Balbi 					"write", status);
49584868424SFelipe Balbi 	}
49684868424SFelipe Balbi 
4972f2a7d5eSFelipe Balbi 	if (agent->edge_change) {
4982f2a7d5eSFelipe Balbi 		u32		edge_change;
4992f2a7d5eSFelipe Balbi 		u8		bytes[6];
5002f2a7d5eSFelipe Balbi 
5012f2a7d5eSFelipe Balbi 		edge_change = agent->edge_change;
5022f2a7d5eSFelipe Balbi 		agent->edge_change = 0;
5032f2a7d5eSFelipe Balbi 
5042f2a7d5eSFelipe Balbi 		/*
5052f2a7d5eSFelipe Balbi 		 * Read, reserving first byte for write scratch.  Yes, this
5062f2a7d5eSFelipe Balbi 		 * could be cached for some speedup ... but be careful about
5072f2a7d5eSFelipe Balbi 		 * any processor on the other IRQ line, EDR registers are
5082f2a7d5eSFelipe Balbi 		 * shared.
5092f2a7d5eSFelipe Balbi 		 */
51014591d88SPeter Ujfalusi 		status = twl_i2c_read(sih->module, bytes,
5112f2a7d5eSFelipe Balbi 				sih->edr_offset, sih->bytes_edr);
5122f2a7d5eSFelipe Balbi 		if (status) {
5132f2a7d5eSFelipe Balbi 			pr_err("twl4030: %s, %s --> %d\n", __func__,
5142f2a7d5eSFelipe Balbi 					"read", status);
5152f2a7d5eSFelipe Balbi 			return;
5162f2a7d5eSFelipe Balbi 		}
5172f2a7d5eSFelipe Balbi 
5182f2a7d5eSFelipe Balbi 		/* Modify only the bits we know must change */
5192f2a7d5eSFelipe Balbi 		while (edge_change) {
5202f2a7d5eSFelipe Balbi 			int		i = fls(edge_change) - 1;
52114591d88SPeter Ujfalusi 			int		byte = i >> 2;
5222f2a7d5eSFelipe Balbi 			int		off = (i & 0x3) * 2;
5232f2a7d5eSFelipe Balbi 			unsigned int	type;
5242f2a7d5eSFelipe Balbi 
5252f2a7d5eSFelipe Balbi 			bytes[byte] &= ~(0x03 << off);
5262f2a7d5eSFelipe Balbi 
5275dbf79d4SJavier Martinez Canillas 			type = irq_get_trigger_type(i + agent->irq_base);
5282f2a7d5eSFelipe Balbi 			if (type & IRQ_TYPE_EDGE_RISING)
5292f2a7d5eSFelipe Balbi 				bytes[byte] |= BIT(off + 1);
5302f2a7d5eSFelipe Balbi 			if (type & IRQ_TYPE_EDGE_FALLING)
5312f2a7d5eSFelipe Balbi 				bytes[byte] |= BIT(off + 0);
5322f2a7d5eSFelipe Balbi 
5332f2a7d5eSFelipe Balbi 			edge_change &= ~BIT(i);
5342f2a7d5eSFelipe Balbi 		}
5352f2a7d5eSFelipe Balbi 
5362f2a7d5eSFelipe Balbi 		/* Write */
5372f2a7d5eSFelipe Balbi 		status = twl_i2c_write(sih->module, bytes,
5382f2a7d5eSFelipe Balbi 				sih->edr_offset, sih->bytes_edr);
5392f2a7d5eSFelipe Balbi 		if (status)
5402f2a7d5eSFelipe Balbi 			pr_err("twl4030: %s, %s --> %d\n", __func__,
5412f2a7d5eSFelipe Balbi 					"write", status);
5422f2a7d5eSFelipe Balbi 	}
5432f2a7d5eSFelipe Balbi 
54484868424SFelipe Balbi 	mutex_unlock(&agent->irq_lock);
54591e3569fSFelipe Balbi }
54691e3569fSFelipe Balbi 
547a30d46c0SDavid Brownell static struct irq_chip twl4030_sih_irq_chip = {
548a30d46c0SDavid Brownell 	.name		= "twl4030",
549845aeab5SMark Brown 	.irq_mask	= twl4030_sih_mask,
550845aeab5SMark Brown 	.irq_unmask	= twl4030_sih_unmask,
551845aeab5SMark Brown 	.irq_set_type	= twl4030_sih_set_type,
55291e3569fSFelipe Balbi 	.irq_bus_lock	= twl4030_sih_bus_lock,
55391e3569fSFelipe Balbi 	.irq_bus_sync_unlock = twl4030_sih_bus_sync_unlock,
55455098ff7SKevin Hilman 	.flags		= IRQCHIP_SKIP_SET_WAKE,
555a30d46c0SDavid Brownell };
556a30d46c0SDavid Brownell 
557a30d46c0SDavid Brownell /*----------------------------------------------------------------------*/
558a30d46c0SDavid Brownell 
559a30d46c0SDavid Brownell static inline int sih_read_isr(const struct sih *sih)
560a30d46c0SDavid Brownell {
561a30d46c0SDavid Brownell 	int status;
562a30d46c0SDavid Brownell 	union {
563a30d46c0SDavid Brownell 		u8 bytes[4];
564a30d46c0SDavid Brownell 		u32 word;
565a30d46c0SDavid Brownell 	} isr;
566a30d46c0SDavid Brownell 
567a30d46c0SDavid Brownell 	/* FIXME need retry-on-error ... */
568a30d46c0SDavid Brownell 
569a30d46c0SDavid Brownell 	isr.word = 0;
570fc7b92fcSBalaji T K 	status = twl_i2c_read(sih->module, isr.bytes,
571a30d46c0SDavid Brownell 			sih->mask[irq_line].isr_offset, sih->bytes_ixr);
572a30d46c0SDavid Brownell 
573a30d46c0SDavid Brownell 	return (status < 0) ? status : le32_to_cpu(isr.word);
574a30d46c0SDavid Brownell }
575a30d46c0SDavid Brownell 
576a30d46c0SDavid Brownell /*
577a30d46c0SDavid Brownell  * Generic handler for SIH interrupts ... we "know" this is called
578a30d46c0SDavid Brownell  * in task context, with IRQs enabled.
579a30d46c0SDavid Brownell  */
580c1e61bcfSNeilBrown static irqreturn_t handle_twl4030_sih(int irq, void *data)
581a30d46c0SDavid Brownell {
582d5bb1221SThomas Gleixner 	struct sih_agent *agent = irq_get_handler_data(irq);
583a30d46c0SDavid Brownell 	const struct sih *sih = agent->sih;
584a30d46c0SDavid Brownell 	int isr;
585a30d46c0SDavid Brownell 
586a30d46c0SDavid Brownell 	/* reading ISR acks the IRQs, using clear-on-read mode */
587a30d46c0SDavid Brownell 	isr = sih_read_isr(sih);
588a30d46c0SDavid Brownell 
589a30d46c0SDavid Brownell 	if (isr < 0) {
590a30d46c0SDavid Brownell 		pr_err("twl4030: %s SIH, read ISR error %d\n",
591a30d46c0SDavid Brownell 			sih->name, isr);
592a30d46c0SDavid Brownell 		/* REVISIT:  recover; eventually mask it all, etc */
593c1e61bcfSNeilBrown 		return IRQ_HANDLED;
594a30d46c0SDavid Brownell 	}
595a30d46c0SDavid Brownell 
596a30d46c0SDavid Brownell 	while (isr) {
597a30d46c0SDavid Brownell 		irq = fls(isr);
598a30d46c0SDavid Brownell 		irq--;
599a30d46c0SDavid Brownell 		isr &= ~BIT(irq);
600a30d46c0SDavid Brownell 
601a30d46c0SDavid Brownell 		if (irq < sih->bits)
602925e853cSFelipe Balbi 			handle_nested_irq(agent->irq_base + irq);
603a30d46c0SDavid Brownell 		else
604a30d46c0SDavid Brownell 			pr_err("twl4030: %s SIH, invalid ISR bit %d\n",
605a30d46c0SDavid Brownell 				sih->name, irq);
606a30d46c0SDavid Brownell 	}
607c1e61bcfSNeilBrown 	return IRQ_HANDLED;
608a30d46c0SDavid Brownell }
609a30d46c0SDavid Brownell 
610cbcde05eSFelipe Contreras /* returns the first IRQ used by this SIH bank, or negative errno */
611f01b1f90SBenoit Cousson int twl4030_sih_setup(struct device *dev, int module, int irq_base)
612a30d46c0SDavid Brownell {
613a30d46c0SDavid Brownell 	int			sih_mod;
614a30d46c0SDavid Brownell 	const struct sih	*sih = NULL;
615a30d46c0SDavid Brownell 	struct sih_agent	*agent;
616a30d46c0SDavid Brownell 	int			i, irq;
617a30d46c0SDavid Brownell 	int			status = -EINVAL;
618a30d46c0SDavid Brownell 
619a30d46c0SDavid Brownell 	/* only support modules with standard clear-on-read for now */
620ec1a07b3SBenoit Cousson 	for (sih_mod = 0, sih = sih_modules; sih_mod < nr_sih_modules;
621a30d46c0SDavid Brownell 			sih_mod++, sih++) {
622a30d46c0SDavid Brownell 		if (sih->module == module && sih->set_cor) {
623a30d46c0SDavid Brownell 			status = 0;
624a30d46c0SDavid Brownell 			break;
625a30d46c0SDavid Brownell 		}
626a30d46c0SDavid Brownell 	}
627ec1a07b3SBenoit Cousson 
62848585739SUwe Kleine-König 	if (status < 0) {
62948585739SUwe Kleine-König 		dev_err(dev, "module to setup SIH for not found\n");
630a30d46c0SDavid Brownell 		return status;
63148585739SUwe Kleine-König 	}
632a30d46c0SDavid Brownell 
63304aa4438SLee Jones 	agent = kzalloc(sizeof(*agent), GFP_KERNEL);
634a30d46c0SDavid Brownell 	if (!agent)
635a30d46c0SDavid Brownell 		return -ENOMEM;
636a30d46c0SDavid Brownell 
637a30d46c0SDavid Brownell 	agent->irq_base = irq_base;
638a30d46c0SDavid Brownell 	agent->sih = sih;
639a30d46c0SDavid Brownell 	agent->imr = ~0;
64091e3569fSFelipe Balbi 	mutex_init(&agent->irq_lock);
641a30d46c0SDavid Brownell 
642a30d46c0SDavid Brownell 	for (i = 0; i < sih->bits; i++) {
643a30d46c0SDavid Brownell 		irq = irq_base + i;
644a30d46c0SDavid Brownell 
64591e3569fSFelipe Balbi 		irq_set_chip_data(irq, agent);
646d5bb1221SThomas Gleixner 		irq_set_chip_and_handler(irq, &twl4030_sih_irq_chip,
647a30d46c0SDavid Brownell 					 handle_edge_irq);
648b18d1f0fSNeilBrown 		irq_set_nested_thread(irq, 1);
649a30d46c0SDavid Brownell 		activate_irq(irq);
650a30d46c0SDavid Brownell 	}
651a30d46c0SDavid Brownell 
652a30d46c0SDavid Brownell 	/* replace generic PIH handler (handle_simple_irq) */
653a30d46c0SDavid Brownell 	irq = sih_mod + twl4030_irq_base;
654d5bb1221SThomas Gleixner 	irq_set_handler_data(irq, agent);
655c1e61bcfSNeilBrown 	agent->irq_name = kasprintf(GFP_KERNEL, "twl4030_%s", sih->name);
6568b41669cSKalle Jokiniemi 	status = request_threaded_irq(irq, NULL, handle_twl4030_sih,
6577d5b1ed8SFabio Estevam 				      IRQF_EARLY_RESUME | IRQF_ONESHOT,
658c1e61bcfSNeilBrown 				      agent->irq_name ?: sih->name, NULL);
659a30d46c0SDavid Brownell 
660ec1a07b3SBenoit Cousson 	dev_info(dev, "%s (irq %d) chaining IRQs %d..%d\n", sih->name,
661f01b1f90SBenoit Cousson 			irq, irq_base, irq_base + i - 1);
662a30d46c0SDavid Brownell 
663c1e61bcfSNeilBrown 	return status < 0 ? status : irq_base;
664a30d46c0SDavid Brownell }
665a30d46c0SDavid Brownell 
666a30d46c0SDavid Brownell /* FIXME need a call to reverse twl4030_sih_setup() ... */
667a30d46c0SDavid Brownell 
668a30d46c0SDavid Brownell /*----------------------------------------------------------------------*/
669a30d46c0SDavid Brownell 
670a30d46c0SDavid Brownell /* FIXME pass in which interrupt line we'll use ... */
671a30d46c0SDavid Brownell #define twl_irq_line	0
672a30d46c0SDavid Brownell 
67378518ffaSBenoit Cousson int twl4030_init_irq(struct device *dev, int irq_num)
674a30d46c0SDavid Brownell {
675a30d46c0SDavid Brownell 	static struct irq_chip	twl4030_irq_chip;
676ec1a07b3SBenoit Cousson 	int			status, i;
67778518ffaSBenoit Cousson 	int			irq_base, irq_end, nr_irqs;
67878518ffaSBenoit Cousson 	struct			device_node *node = dev->of_node;
679a30d46c0SDavid Brownell 
680a30d46c0SDavid Brownell 	/*
68178518ffaSBenoit Cousson 	 * TWL core and pwr interrupts must be contiguous because
68278518ffaSBenoit Cousson 	 * the hwirqs numbers are defined contiguously from 1 to 15.
68378518ffaSBenoit Cousson 	 * Create only one domain for both.
68478518ffaSBenoit Cousson 	 */
68578518ffaSBenoit Cousson 	nr_irqs = TWL4030_PWR_NR_IRQS + TWL4030_CORE_NR_IRQS;
68678518ffaSBenoit Cousson 
68778518ffaSBenoit Cousson 	irq_base = irq_alloc_descs(-1, 0, nr_irqs, 0);
688287980e4SArnd Bergmann 	if (irq_base < 0) {
68978518ffaSBenoit Cousson 		dev_err(dev, "Fail to allocate IRQ descs\n");
69078518ffaSBenoit Cousson 		return irq_base;
69178518ffaSBenoit Cousson 	}
69278518ffaSBenoit Cousson 
69378518ffaSBenoit Cousson 	irq_domain_add_legacy(node, nr_irqs, irq_base, 0,
69478518ffaSBenoit Cousson 			      &irq_domain_simple_ops, NULL);
69578518ffaSBenoit Cousson 
69678518ffaSBenoit Cousson 	irq_end = irq_base + TWL4030_CORE_NR_IRQS;
69778518ffaSBenoit Cousson 
69878518ffaSBenoit Cousson 	/*
699a30d46c0SDavid Brownell 	 * Mask and clear all TWL4030 interrupts since initially we do
700a30d46c0SDavid Brownell 	 * not have any TWL4030 module interrupt handlers present
701a30d46c0SDavid Brownell 	 */
702a30d46c0SDavid Brownell 	status = twl4030_init_sih_modules(twl_irq_line);
703a30d46c0SDavid Brownell 	if (status < 0)
704a30d46c0SDavid Brownell 		return status;
705a30d46c0SDavid Brownell 
706a30d46c0SDavid Brownell 	twl4030_irq_base = irq_base;
707a30d46c0SDavid Brownell 
708cbcde05eSFelipe Contreras 	/*
709ec1a07b3SBenoit Cousson 	 * Install an irq handler for each of the SIH modules;
710a30d46c0SDavid Brownell 	 * clone dummy irq_chip since PIH can't *do* anything
711a30d46c0SDavid Brownell 	 */
712a30d46c0SDavid Brownell 	twl4030_irq_chip = dummy_irq_chip;
713a30d46c0SDavid Brownell 	twl4030_irq_chip.name = "twl4030";
714a30d46c0SDavid Brownell 
715fe212213SThomas Gleixner 	twl4030_sih_irq_chip.irq_ack = dummy_irq_chip.irq_ack;
716a30d46c0SDavid Brownell 
717a30d46c0SDavid Brownell 	for (i = irq_base; i < irq_end; i++) {
718d5bb1221SThomas Gleixner 		irq_set_chip_and_handler(i, &twl4030_irq_chip,
719a30d46c0SDavid Brownell 					 handle_simple_irq);
720925e853cSFelipe Balbi 		irq_set_nested_thread(i, 1);
721a30d46c0SDavid Brownell 		activate_irq(i);
722a30d46c0SDavid Brownell 	}
723f01b1f90SBenoit Cousson 
724ec1a07b3SBenoit Cousson 	dev_info(dev, "%s (irq %d) chaining IRQs %d..%d\n", "PIH",
725f01b1f90SBenoit Cousson 			irq_num, irq_base, irq_end);
726a30d46c0SDavid Brownell 
727a30d46c0SDavid Brownell 	/* ... and the PWR_INT module ... */
728f01b1f90SBenoit Cousson 	status = twl4030_sih_setup(dev, TWL4030_MODULE_INT, irq_end);
729a30d46c0SDavid Brownell 	if (status < 0) {
730ec1a07b3SBenoit Cousson 		dev_err(dev, "sih_setup PWR INT --> %d\n", status);
731a30d46c0SDavid Brownell 		goto fail;
732a30d46c0SDavid Brownell 	}
733a30d46c0SDavid Brownell 
734a30d46c0SDavid Brownell 	/* install an irq handler to demultiplex the TWL4030 interrupt */
735286f8f3cSNeilBrown 	status = request_threaded_irq(irq_num, NULL, handle_twl4030_pih,
736286f8f3cSNeilBrown 				      IRQF_ONESHOT,
737a980bf73SSamuel Ortiz 				      "TWL4030-PIH", NULL);
7381cef8e41SRussell King 	if (status < 0) {
739ec1a07b3SBenoit Cousson 		dev_err(dev, "could not claim irq%d: %d\n", irq_num, status);
7401cef8e41SRussell King 		goto fail_rqirq;
741a30d46c0SDavid Brownell 	}
7425a2f1b5fSNeilBrown 	enable_irq_wake(irq_num);
743a30d46c0SDavid Brownell 
74478518ffaSBenoit Cousson 	return irq_base;
7451cef8e41SRussell King fail_rqirq:
7461cef8e41SRussell King 	/* clean up twl4030_sih_setup */
747a30d46c0SDavid Brownell fail:
748925e853cSFelipe Balbi 	for (i = irq_base; i < irq_end; i++) {
749925e853cSFelipe Balbi 		irq_set_nested_thread(i, 0);
750d5bb1221SThomas Gleixner 		irq_set_chip_and_handler(i, NULL, NULL);
751925e853cSFelipe Balbi 	}
7522f2a7d5eSFelipe Balbi 
753a30d46c0SDavid Brownell 	return status;
754a30d46c0SDavid Brownell }
755a30d46c0SDavid Brownell 
756e8deb28cSBalaji T K int twl4030_exit_irq(void)
757a30d46c0SDavid Brownell {
758a30d46c0SDavid Brownell 	/* FIXME undo twl_init_irq() */
759a30d46c0SDavid Brownell 	if (twl4030_irq_base) {
760a30d46c0SDavid Brownell 		pr_err("twl4030: can't yet clean up IRQs?\n");
761a30d46c0SDavid Brownell 		return -ENOSYS;
762a30d46c0SDavid Brownell 	}
763a30d46c0SDavid Brownell 	return 0;
764a30d46c0SDavid Brownell }
7651920a61eSIlkka Koskinen 
766e8deb28cSBalaji T K int twl4030_init_chip_irq(const char *chip)
7671920a61eSIlkka Koskinen {
7681920a61eSIlkka Koskinen 	if (!strcmp(chip, "twl5031")) {
7691920a61eSIlkka Koskinen 		sih_modules = sih_modules_twl5031;
7701920a61eSIlkka Koskinen 		nr_sih_modules = ARRAY_SIZE(sih_modules_twl5031);
7711920a61eSIlkka Koskinen 	} else {
7721920a61eSIlkka Koskinen 		sih_modules = sih_modules_twl4030;
7731920a61eSIlkka Koskinen 		nr_sih_modules = ARRAY_SIZE(sih_modules_twl4030);
7741920a61eSIlkka Koskinen 	}
7751920a61eSIlkka Koskinen 
7761920a61eSIlkka Koskinen 	return 0;
7771920a61eSIlkka Koskinen }
778