xref: /linux/drivers/mfd/rc5t583-irq.c (revision 1b1247dd75aa5cf5fae54a3bec7280046e9c7957)
1*1b1247ddSLaxman Dewangan /*
2*1b1247ddSLaxman Dewangan  * Interrupt driver for RICOH583 power management chip.
3*1b1247ddSLaxman Dewangan  *
4*1b1247ddSLaxman Dewangan  * Copyright (c) 2011-2012, NVIDIA CORPORATION.  All rights reserved.
5*1b1247ddSLaxman Dewangan  * Author: Laxman dewangan <ldewangan@nvidia.com>
6*1b1247ddSLaxman Dewangan  *
7*1b1247ddSLaxman Dewangan  * based on code
8*1b1247ddSLaxman Dewangan  *      Copyright (C) 2011 RICOH COMPANY,LTD
9*1b1247ddSLaxman Dewangan  *
10*1b1247ddSLaxman Dewangan  * This program is free software; you can redistribute it and/or modify it
11*1b1247ddSLaxman Dewangan  * under the terms and conditions of the GNU General Public License,
12*1b1247ddSLaxman Dewangan  * version 2, as published by the Free Software Foundation.
13*1b1247ddSLaxman Dewangan  *
14*1b1247ddSLaxman Dewangan  * This program is distributed in the hope it will be useful, but WITHOUT
15*1b1247ddSLaxman Dewangan  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16*1b1247ddSLaxman Dewangan  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
17*1b1247ddSLaxman Dewangan  * more details.
18*1b1247ddSLaxman Dewangan  *
19*1b1247ddSLaxman Dewangan  * You should have received a copy of the GNU General Public License
20*1b1247ddSLaxman Dewangan  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21*1b1247ddSLaxman Dewangan  *
22*1b1247ddSLaxman Dewangan  */
23*1b1247ddSLaxman Dewangan #include <linux/interrupt.h>
24*1b1247ddSLaxman Dewangan #include <linux/irq.h>
25*1b1247ddSLaxman Dewangan #include <linux/init.h>
26*1b1247ddSLaxman Dewangan #include <linux/i2c.h>
27*1b1247ddSLaxman Dewangan #include <linux/mfd/rc5t583.h>
28*1b1247ddSLaxman Dewangan 
29*1b1247ddSLaxman Dewangan enum int_type {
30*1b1247ddSLaxman Dewangan 	SYS_INT  = 0x1,
31*1b1247ddSLaxman Dewangan 	DCDC_INT = 0x2,
32*1b1247ddSLaxman Dewangan 	RTC_INT  = 0x4,
33*1b1247ddSLaxman Dewangan 	ADC_INT  = 0x8,
34*1b1247ddSLaxman Dewangan 	GPIO_INT = 0x10,
35*1b1247ddSLaxman Dewangan };
36*1b1247ddSLaxman Dewangan 
37*1b1247ddSLaxman Dewangan static int gpedge_add[] = {
38*1b1247ddSLaxman Dewangan 	RC5T583_GPIO_GPEDGE2,
39*1b1247ddSLaxman Dewangan 	RC5T583_GPIO_GPEDGE2
40*1b1247ddSLaxman Dewangan };
41*1b1247ddSLaxman Dewangan 
42*1b1247ddSLaxman Dewangan static int irq_en_add[] = {
43*1b1247ddSLaxman Dewangan 	RC5T583_INT_EN_SYS1,
44*1b1247ddSLaxman Dewangan 	RC5T583_INT_EN_SYS2,
45*1b1247ddSLaxman Dewangan 	RC5T583_INT_EN_DCDC,
46*1b1247ddSLaxman Dewangan 	RC5T583_INT_EN_RTC,
47*1b1247ddSLaxman Dewangan 	RC5T583_INT_EN_ADC1,
48*1b1247ddSLaxman Dewangan 	RC5T583_INT_EN_ADC2,
49*1b1247ddSLaxman Dewangan 	RC5T583_INT_EN_ADC3,
50*1b1247ddSLaxman Dewangan 	RC5T583_GPIO_EN_INT
51*1b1247ddSLaxman Dewangan };
52*1b1247ddSLaxman Dewangan 
53*1b1247ddSLaxman Dewangan static int irq_mon_add[] = {
54*1b1247ddSLaxman Dewangan 	RC5T583_INT_MON_SYS1,
55*1b1247ddSLaxman Dewangan 	RC5T583_INT_MON_SYS2,
56*1b1247ddSLaxman Dewangan 	RC5T583_INT_MON_DCDC,
57*1b1247ddSLaxman Dewangan 	RC5T583_INT_MON_RTC,
58*1b1247ddSLaxman Dewangan 	RC5T583_INT_IR_ADCL,
59*1b1247ddSLaxman Dewangan 	RC5T583_INT_IR_ADCH,
60*1b1247ddSLaxman Dewangan 	RC5T583_INT_IR_ADCEND,
61*1b1247ddSLaxman Dewangan 	RC5T583_INT_IR_GPIOF,
62*1b1247ddSLaxman Dewangan 	RC5T583_INT_IR_GPIOR
63*1b1247ddSLaxman Dewangan };
64*1b1247ddSLaxman Dewangan 
65*1b1247ddSLaxman Dewangan static int irq_clr_add[] = {
66*1b1247ddSLaxman Dewangan 	RC5T583_INT_IR_SYS1,
67*1b1247ddSLaxman Dewangan 	RC5T583_INT_IR_SYS2,
68*1b1247ddSLaxman Dewangan 	RC5T583_INT_IR_DCDC,
69*1b1247ddSLaxman Dewangan 	RC5T583_INT_IR_RTC,
70*1b1247ddSLaxman Dewangan 	RC5T583_INT_IR_ADCL,
71*1b1247ddSLaxman Dewangan 	RC5T583_INT_IR_ADCH,
72*1b1247ddSLaxman Dewangan 	RC5T583_INT_IR_ADCEND,
73*1b1247ddSLaxman Dewangan 	RC5T583_INT_IR_GPIOF,
74*1b1247ddSLaxman Dewangan 	RC5T583_INT_IR_GPIOR
75*1b1247ddSLaxman Dewangan };
76*1b1247ddSLaxman Dewangan 
77*1b1247ddSLaxman Dewangan static int main_int_type[] = {
78*1b1247ddSLaxman Dewangan 	SYS_INT,
79*1b1247ddSLaxman Dewangan 	SYS_INT,
80*1b1247ddSLaxman Dewangan 	DCDC_INT,
81*1b1247ddSLaxman Dewangan 	RTC_INT,
82*1b1247ddSLaxman Dewangan 	ADC_INT,
83*1b1247ddSLaxman Dewangan 	ADC_INT,
84*1b1247ddSLaxman Dewangan 	ADC_INT,
85*1b1247ddSLaxman Dewangan 	GPIO_INT,
86*1b1247ddSLaxman Dewangan 	GPIO_INT,
87*1b1247ddSLaxman Dewangan };
88*1b1247ddSLaxman Dewangan 
89*1b1247ddSLaxman Dewangan struct rc5t583_irq_data {
90*1b1247ddSLaxman Dewangan 	u8	int_type;
91*1b1247ddSLaxman Dewangan 	u8	master_bit;
92*1b1247ddSLaxman Dewangan 	u8	int_en_bit;
93*1b1247ddSLaxman Dewangan 	u8	mask_reg_index;
94*1b1247ddSLaxman Dewangan 	int	grp_index;
95*1b1247ddSLaxman Dewangan };
96*1b1247ddSLaxman Dewangan 
97*1b1247ddSLaxman Dewangan #define RC5T583_IRQ(_int_type, _master_bit, _grp_index, \
98*1b1247ddSLaxman Dewangan 			_int_bit, _mask_ind)		\
99*1b1247ddSLaxman Dewangan 	{						\
100*1b1247ddSLaxman Dewangan 		.int_type	= _int_type,		\
101*1b1247ddSLaxman Dewangan 		.master_bit	= _master_bit,		\
102*1b1247ddSLaxman Dewangan 		.grp_index	= _grp_index,		\
103*1b1247ddSLaxman Dewangan 		.int_en_bit	= _int_bit,		\
104*1b1247ddSLaxman Dewangan 		.mask_reg_index	= _mask_ind,		\
105*1b1247ddSLaxman Dewangan 	}
106*1b1247ddSLaxman Dewangan 
107*1b1247ddSLaxman Dewangan static const struct rc5t583_irq_data rc5t583_irqs[RC5T583_MAX_IRQS] = {
108*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_ONKEY]		= RC5T583_IRQ(SYS_INT,  0, 0, 0, 0),
109*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_ACOK]		= RC5T583_IRQ(SYS_INT,  0, 1, 1, 0),
110*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_LIDOPEN]		= RC5T583_IRQ(SYS_INT,  0, 2, 2, 0),
111*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_PREOT]		= RC5T583_IRQ(SYS_INT,  0, 3, 3, 0),
112*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_CLKSTP]		= RC5T583_IRQ(SYS_INT,  0, 4, 4, 0),
113*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_ONKEY_OFF]		= RC5T583_IRQ(SYS_INT,  0, 5, 5, 0),
114*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_WD]		= RC5T583_IRQ(SYS_INT,  0, 7, 7, 0),
115*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_EN_PWRREQ1]	= RC5T583_IRQ(SYS_INT,  0, 8, 0, 1),
116*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_EN_PWRREQ2]	= RC5T583_IRQ(SYS_INT,  0, 9, 1, 1),
117*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_PRE_VINDET]	= RC5T583_IRQ(SYS_INT,  0, 10, 2, 1),
118*1b1247ddSLaxman Dewangan 
119*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_DC0LIM]		= RC5T583_IRQ(DCDC_INT, 1, 0, 0, 2),
120*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_DC1LIM]		= RC5T583_IRQ(DCDC_INT, 1, 1, 1, 2),
121*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_DC2LIM]		= RC5T583_IRQ(DCDC_INT, 1, 2, 2, 2),
122*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_DC3LIM]		= RC5T583_IRQ(DCDC_INT, 1, 3, 3, 2),
123*1b1247ddSLaxman Dewangan 
124*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_CTC]		= RC5T583_IRQ(RTC_INT,  2, 0, 0, 3),
125*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_YALE]		= RC5T583_IRQ(RTC_INT,  2, 5, 5, 3),
126*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_DALE]		= RC5T583_IRQ(RTC_INT,  2, 6, 6, 3),
127*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_WALE]		= RC5T583_IRQ(RTC_INT,  2, 7, 7, 3),
128*1b1247ddSLaxman Dewangan 
129*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_AIN1L]		= RC5T583_IRQ(ADC_INT,  3, 0, 0, 4),
130*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_AIN2L]		= RC5T583_IRQ(ADC_INT,  3, 1, 1, 4),
131*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_AIN3L]		= RC5T583_IRQ(ADC_INT,  3, 2, 2, 4),
132*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_VBATL]		= RC5T583_IRQ(ADC_INT,  3, 3, 3, 4),
133*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_VIN3L]		= RC5T583_IRQ(ADC_INT,  3, 4, 4, 4),
134*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_VIN8L]		= RC5T583_IRQ(ADC_INT,  3, 5, 5, 4),
135*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_AIN1H]		= RC5T583_IRQ(ADC_INT,  3, 6, 0, 5),
136*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_AIN2H]		= RC5T583_IRQ(ADC_INT,  3, 7, 1, 5),
137*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_AIN3H]		= RC5T583_IRQ(ADC_INT,  3, 8, 2, 5),
138*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_VBATH]		= RC5T583_IRQ(ADC_INT,  3, 9, 3, 5),
139*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_VIN3H]		= RC5T583_IRQ(ADC_INT,  3, 10, 4, 5),
140*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_VIN8H]		= RC5T583_IRQ(ADC_INT,  3, 11, 5, 5),
141*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_ADCEND]		= RC5T583_IRQ(ADC_INT,  3, 12, 0, 6),
142*1b1247ddSLaxman Dewangan 
143*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_GPIO0]		= RC5T583_IRQ(GPIO_INT, 4, 0, 0, 7),
144*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_GPIO1]		= RC5T583_IRQ(GPIO_INT, 4, 1, 1, 7),
145*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_GPIO2]		= RC5T583_IRQ(GPIO_INT, 4, 2, 2, 7),
146*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_GPIO3]		= RC5T583_IRQ(GPIO_INT, 4, 3, 3, 7),
147*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_GPIO4]		= RC5T583_IRQ(GPIO_INT, 4, 4, 4, 7),
148*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_GPIO5]		= RC5T583_IRQ(GPIO_INT, 4, 5, 5, 7),
149*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_GPIO6]		= RC5T583_IRQ(GPIO_INT, 4, 6, 6, 7),
150*1b1247ddSLaxman Dewangan 	[RC5T583_IRQ_GPIO7]		= RC5T583_IRQ(GPIO_INT, 4, 7, 7, 7),
151*1b1247ddSLaxman Dewangan };
152*1b1247ddSLaxman Dewangan 
153*1b1247ddSLaxman Dewangan static void rc5t583_irq_lock(struct irq_data *irq_data)
154*1b1247ddSLaxman Dewangan {
155*1b1247ddSLaxman Dewangan 	struct rc5t583 *rc5t583 = irq_data_get_irq_chip_data(irq_data);
156*1b1247ddSLaxman Dewangan 	mutex_lock(&rc5t583->irq_lock);
157*1b1247ddSLaxman Dewangan }
158*1b1247ddSLaxman Dewangan 
159*1b1247ddSLaxman Dewangan static void rc5t583_irq_unmask(struct irq_data *irq_data)
160*1b1247ddSLaxman Dewangan {
161*1b1247ddSLaxman Dewangan 	struct rc5t583 *rc5t583 = irq_data_get_irq_chip_data(irq_data);
162*1b1247ddSLaxman Dewangan 	unsigned int __irq = irq_data->irq - rc5t583->irq_base;
163*1b1247ddSLaxman Dewangan 	const struct rc5t583_irq_data *data = &rc5t583_irqs[__irq];
164*1b1247ddSLaxman Dewangan 
165*1b1247ddSLaxman Dewangan 	rc5t583->group_irq_en[data->grp_index] |= 1 << data->grp_index;
166*1b1247ddSLaxman Dewangan 	rc5t583->intc_inten_reg |= 1 << data->master_bit;
167*1b1247ddSLaxman Dewangan 	rc5t583->irq_en_reg[data->mask_reg_index] |= 1 << data->int_en_bit;
168*1b1247ddSLaxman Dewangan }
169*1b1247ddSLaxman Dewangan 
170*1b1247ddSLaxman Dewangan static void rc5t583_irq_mask(struct irq_data *irq_data)
171*1b1247ddSLaxman Dewangan {
172*1b1247ddSLaxman Dewangan 	struct rc5t583 *rc5t583 = irq_data_get_irq_chip_data(irq_data);
173*1b1247ddSLaxman Dewangan 	unsigned int __irq = irq_data->irq - rc5t583->irq_base;
174*1b1247ddSLaxman Dewangan 	const struct rc5t583_irq_data *data = &rc5t583_irqs[__irq];
175*1b1247ddSLaxman Dewangan 
176*1b1247ddSLaxman Dewangan 	rc5t583->group_irq_en[data->grp_index] &= ~(1 << data->grp_index);
177*1b1247ddSLaxman Dewangan 	if (!rc5t583->group_irq_en[data->grp_index])
178*1b1247ddSLaxman Dewangan 		rc5t583->intc_inten_reg &= ~(1 << data->master_bit);
179*1b1247ddSLaxman Dewangan 
180*1b1247ddSLaxman Dewangan 	rc5t583->irq_en_reg[data->mask_reg_index] &= ~(1 << data->int_en_bit);
181*1b1247ddSLaxman Dewangan }
182*1b1247ddSLaxman Dewangan 
183*1b1247ddSLaxman Dewangan static int rc5t583_irq_set_type(struct irq_data *irq_data, unsigned int type)
184*1b1247ddSLaxman Dewangan {
185*1b1247ddSLaxman Dewangan 	struct rc5t583 *rc5t583 = irq_data_get_irq_chip_data(irq_data);
186*1b1247ddSLaxman Dewangan 	unsigned int __irq = irq_data->irq - rc5t583->irq_base;
187*1b1247ddSLaxman Dewangan 	const struct rc5t583_irq_data *data = &rc5t583_irqs[__irq];
188*1b1247ddSLaxman Dewangan 	int val = 0;
189*1b1247ddSLaxman Dewangan 	int gpedge_index;
190*1b1247ddSLaxman Dewangan 	int gpedge_bit_pos;
191*1b1247ddSLaxman Dewangan 
192*1b1247ddSLaxman Dewangan 	/* Supporting only trigger level inetrrupt */
193*1b1247ddSLaxman Dewangan 	if ((data->int_type & GPIO_INT) && (type & IRQ_TYPE_EDGE_BOTH)) {
194*1b1247ddSLaxman Dewangan 		gpedge_index = data->int_en_bit / 4;
195*1b1247ddSLaxman Dewangan 		gpedge_bit_pos = data->int_en_bit % 4;
196*1b1247ddSLaxman Dewangan 
197*1b1247ddSLaxman Dewangan 		if (type & IRQ_TYPE_EDGE_FALLING)
198*1b1247ddSLaxman Dewangan 			val |= 0x2;
199*1b1247ddSLaxman Dewangan 
200*1b1247ddSLaxman Dewangan 		if (type & IRQ_TYPE_EDGE_RISING)
201*1b1247ddSLaxman Dewangan 			val |= 0x1;
202*1b1247ddSLaxman Dewangan 
203*1b1247ddSLaxman Dewangan 		rc5t583->gpedge_reg[gpedge_index] &= ~(3 << gpedge_bit_pos);
204*1b1247ddSLaxman Dewangan 		rc5t583->gpedge_reg[gpedge_index] |= (val << gpedge_bit_pos);
205*1b1247ddSLaxman Dewangan 		rc5t583_irq_unmask(irq_data);
206*1b1247ddSLaxman Dewangan 		return 0;
207*1b1247ddSLaxman Dewangan 	}
208*1b1247ddSLaxman Dewangan 	return -EINVAL;
209*1b1247ddSLaxman Dewangan }
210*1b1247ddSLaxman Dewangan 
211*1b1247ddSLaxman Dewangan static void rc5t583_irq_sync_unlock(struct irq_data *irq_data)
212*1b1247ddSLaxman Dewangan {
213*1b1247ddSLaxman Dewangan 	struct rc5t583 *rc5t583 = irq_data_get_irq_chip_data(irq_data);
214*1b1247ddSLaxman Dewangan 	int i;
215*1b1247ddSLaxman Dewangan 	int ret;
216*1b1247ddSLaxman Dewangan 
217*1b1247ddSLaxman Dewangan 	for (i = 0; i < ARRAY_SIZE(rc5t583->gpedge_reg); i++) {
218*1b1247ddSLaxman Dewangan 		ret = rc5t583_write(rc5t583->dev, gpedge_add[i],
219*1b1247ddSLaxman Dewangan 				rc5t583->gpedge_reg[i]);
220*1b1247ddSLaxman Dewangan 		if (ret < 0)
221*1b1247ddSLaxman Dewangan 			dev_warn(rc5t583->dev,
222*1b1247ddSLaxman Dewangan 				"Error in writing reg 0x%02x error: %d\n",
223*1b1247ddSLaxman Dewangan 				gpedge_add[i], ret);
224*1b1247ddSLaxman Dewangan 	}
225*1b1247ddSLaxman Dewangan 
226*1b1247ddSLaxman Dewangan 	for (i = 0; i < ARRAY_SIZE(rc5t583->irq_en_reg); i++) {
227*1b1247ddSLaxman Dewangan 		ret = rc5t583_write(rc5t583->dev, irq_en_add[i],
228*1b1247ddSLaxman Dewangan 					rc5t583->irq_en_reg[i]);
229*1b1247ddSLaxman Dewangan 		if (ret < 0)
230*1b1247ddSLaxman Dewangan 			dev_warn(rc5t583->dev,
231*1b1247ddSLaxman Dewangan 				"Error in writing reg 0x%02x error: %d\n",
232*1b1247ddSLaxman Dewangan 				irq_en_add[i], ret);
233*1b1247ddSLaxman Dewangan 	}
234*1b1247ddSLaxman Dewangan 
235*1b1247ddSLaxman Dewangan 	ret = rc5t583_write(rc5t583->dev, RC5T583_INTC_INTEN,
236*1b1247ddSLaxman Dewangan 				rc5t583->intc_inten_reg);
237*1b1247ddSLaxman Dewangan 	if (ret < 0)
238*1b1247ddSLaxman Dewangan 		dev_warn(rc5t583->dev,
239*1b1247ddSLaxman Dewangan 			"Error in writing reg 0x%02x error: %d\n",
240*1b1247ddSLaxman Dewangan 			RC5T583_INTC_INTEN, ret);
241*1b1247ddSLaxman Dewangan 
242*1b1247ddSLaxman Dewangan 	mutex_unlock(&rc5t583->irq_lock);
243*1b1247ddSLaxman Dewangan }
244*1b1247ddSLaxman Dewangan #ifdef CONFIG_PM_SLEEP
245*1b1247ddSLaxman Dewangan static int rc5t583_irq_set_wake(struct irq_data *irq_data, unsigned int on)
246*1b1247ddSLaxman Dewangan {
247*1b1247ddSLaxman Dewangan 	struct rc5t583 *rc5t583 = irq_data_get_irq_chip_data(irq_data);
248*1b1247ddSLaxman Dewangan 	return irq_set_irq_wake(rc5t583->chip_irq, on);
249*1b1247ddSLaxman Dewangan }
250*1b1247ddSLaxman Dewangan #else
251*1b1247ddSLaxman Dewangan #define rc5t583_irq_set_wake NULL
252*1b1247ddSLaxman Dewangan #endif
253*1b1247ddSLaxman Dewangan 
254*1b1247ddSLaxman Dewangan static irqreturn_t rc5t583_irq(int irq, void *data)
255*1b1247ddSLaxman Dewangan {
256*1b1247ddSLaxman Dewangan 	struct rc5t583 *rc5t583 = data;
257*1b1247ddSLaxman Dewangan 	uint8_t int_sts[RC5T583_MAX_INTERRUPT_MASK_REGS];
258*1b1247ddSLaxman Dewangan 	uint8_t master_int;
259*1b1247ddSLaxman Dewangan 	int i;
260*1b1247ddSLaxman Dewangan 	int ret;
261*1b1247ddSLaxman Dewangan 	unsigned int rtc_int_sts = 0;
262*1b1247ddSLaxman Dewangan 
263*1b1247ddSLaxman Dewangan 	/* Clear the status */
264*1b1247ddSLaxman Dewangan 	for (i = 0; i < RC5T583_MAX_INTERRUPT_MASK_REGS; i++)
265*1b1247ddSLaxman Dewangan 		int_sts[i] = 0;
266*1b1247ddSLaxman Dewangan 
267*1b1247ddSLaxman Dewangan 	ret  = rc5t583_read(rc5t583->dev, RC5T583_INTC_INTMON, &master_int);
268*1b1247ddSLaxman Dewangan 	if (ret < 0) {
269*1b1247ddSLaxman Dewangan 		dev_err(rc5t583->dev,
270*1b1247ddSLaxman Dewangan 			"Error in reading reg 0x%02x error: %d\n",
271*1b1247ddSLaxman Dewangan 			RC5T583_INTC_INTMON, ret);
272*1b1247ddSLaxman Dewangan 		return IRQ_HANDLED;
273*1b1247ddSLaxman Dewangan 	}
274*1b1247ddSLaxman Dewangan 
275*1b1247ddSLaxman Dewangan 	for (i = 0; i < RC5T583_MAX_INTERRUPT_MASK_REGS; ++i) {
276*1b1247ddSLaxman Dewangan 		if (!(master_int & main_int_type[i]))
277*1b1247ddSLaxman Dewangan 			continue;
278*1b1247ddSLaxman Dewangan 
279*1b1247ddSLaxman Dewangan 		ret = rc5t583_read(rc5t583->dev, irq_mon_add[i], &int_sts[i]);
280*1b1247ddSLaxman Dewangan 		if (ret < 0) {
281*1b1247ddSLaxman Dewangan 			dev_warn(rc5t583->dev,
282*1b1247ddSLaxman Dewangan 				"Error in reading reg 0x%02x error: %d\n",
283*1b1247ddSLaxman Dewangan 				irq_mon_add[i], ret);
284*1b1247ddSLaxman Dewangan 			int_sts[i] = 0;
285*1b1247ddSLaxman Dewangan 			continue;
286*1b1247ddSLaxman Dewangan 		}
287*1b1247ddSLaxman Dewangan 
288*1b1247ddSLaxman Dewangan 		if (main_int_type[i] & RTC_INT) {
289*1b1247ddSLaxman Dewangan 			rtc_int_sts = 0;
290*1b1247ddSLaxman Dewangan 			if (int_sts[i] & 0x1)
291*1b1247ddSLaxman Dewangan 				rtc_int_sts |= BIT(6);
292*1b1247ddSLaxman Dewangan 			if (int_sts[i] & 0x2)
293*1b1247ddSLaxman Dewangan 				rtc_int_sts |= BIT(7);
294*1b1247ddSLaxman Dewangan 			if (int_sts[i] & 0x4)
295*1b1247ddSLaxman Dewangan 				rtc_int_sts |= BIT(0);
296*1b1247ddSLaxman Dewangan 			if (int_sts[i] & 0x8)
297*1b1247ddSLaxman Dewangan 				rtc_int_sts |= BIT(5);
298*1b1247ddSLaxman Dewangan 		}
299*1b1247ddSLaxman Dewangan 
300*1b1247ddSLaxman Dewangan 		ret = rc5t583_write(rc5t583->dev, irq_clr_add[i],
301*1b1247ddSLaxman Dewangan 				~int_sts[i]);
302*1b1247ddSLaxman Dewangan 		if (ret < 0)
303*1b1247ddSLaxman Dewangan 			dev_warn(rc5t583->dev,
304*1b1247ddSLaxman Dewangan 				"Error in reading reg 0x%02x error: %d\n",
305*1b1247ddSLaxman Dewangan 				irq_clr_add[i], ret);
306*1b1247ddSLaxman Dewangan 
307*1b1247ddSLaxman Dewangan 		if (main_int_type[i] & RTC_INT)
308*1b1247ddSLaxman Dewangan 			int_sts[i] = rtc_int_sts;
309*1b1247ddSLaxman Dewangan 	}
310*1b1247ddSLaxman Dewangan 
311*1b1247ddSLaxman Dewangan 	/* Merge gpio interrupts for rising and falling case*/
312*1b1247ddSLaxman Dewangan 	int_sts[7] |= int_sts[8];
313*1b1247ddSLaxman Dewangan 
314*1b1247ddSLaxman Dewangan 	/* Call interrupt handler if enabled */
315*1b1247ddSLaxman Dewangan 	for (i = 0; i < RC5T583_MAX_IRQS; ++i) {
316*1b1247ddSLaxman Dewangan 		const struct rc5t583_irq_data *data = &rc5t583_irqs[i];
317*1b1247ddSLaxman Dewangan 		if ((int_sts[data->mask_reg_index] & (1 << data->int_en_bit)) &&
318*1b1247ddSLaxman Dewangan 			(rc5t583->group_irq_en[data->master_bit] &
319*1b1247ddSLaxman Dewangan 					(1 << data->grp_index)))
320*1b1247ddSLaxman Dewangan 			handle_nested_irq(rc5t583->irq_base + i);
321*1b1247ddSLaxman Dewangan 	}
322*1b1247ddSLaxman Dewangan 
323*1b1247ddSLaxman Dewangan 	return IRQ_HANDLED;
324*1b1247ddSLaxman Dewangan }
325*1b1247ddSLaxman Dewangan 
326*1b1247ddSLaxman Dewangan static struct irq_chip rc5t583_irq_chip = {
327*1b1247ddSLaxman Dewangan 	.name = "rc5t583-irq",
328*1b1247ddSLaxman Dewangan 	.irq_mask = rc5t583_irq_mask,
329*1b1247ddSLaxman Dewangan 	.irq_unmask = rc5t583_irq_unmask,
330*1b1247ddSLaxman Dewangan 	.irq_bus_lock = rc5t583_irq_lock,
331*1b1247ddSLaxman Dewangan 	.irq_bus_sync_unlock = rc5t583_irq_sync_unlock,
332*1b1247ddSLaxman Dewangan 	.irq_set_type = rc5t583_irq_set_type,
333*1b1247ddSLaxman Dewangan 	.irq_set_wake = rc5t583_irq_set_wake,
334*1b1247ddSLaxman Dewangan };
335*1b1247ddSLaxman Dewangan 
336*1b1247ddSLaxman Dewangan int rc5t583_irq_init(struct rc5t583 *rc5t583, int irq, int irq_base)
337*1b1247ddSLaxman Dewangan {
338*1b1247ddSLaxman Dewangan 	int i, ret;
339*1b1247ddSLaxman Dewangan 
340*1b1247ddSLaxman Dewangan 	if (!irq_base) {
341*1b1247ddSLaxman Dewangan 		dev_warn(rc5t583->dev, "No interrupt support on IRQ base\n");
342*1b1247ddSLaxman Dewangan 		return -EINVAL;
343*1b1247ddSLaxman Dewangan 	}
344*1b1247ddSLaxman Dewangan 
345*1b1247ddSLaxman Dewangan 	mutex_init(&rc5t583->irq_lock);
346*1b1247ddSLaxman Dewangan 
347*1b1247ddSLaxman Dewangan 	/* Initailize all int register to 0 */
348*1b1247ddSLaxman Dewangan 	for (i = 0; i < RC5T583_MAX_INTERRUPT_MASK_REGS; i++)  {
349*1b1247ddSLaxman Dewangan 		ret = rc5t583_write(rc5t583->dev, irq_en_add[i],
350*1b1247ddSLaxman Dewangan 				rc5t583->irq_en_reg[i]);
351*1b1247ddSLaxman Dewangan 		if (ret < 0)
352*1b1247ddSLaxman Dewangan 			dev_warn(rc5t583->dev,
353*1b1247ddSLaxman Dewangan 				"Error in writing reg 0x%02x error: %d\n",
354*1b1247ddSLaxman Dewangan 				irq_en_add[i], ret);
355*1b1247ddSLaxman Dewangan 	}
356*1b1247ddSLaxman Dewangan 
357*1b1247ddSLaxman Dewangan 	for (i = 0; i < RC5T583_MAX_GPEDGE_REG; i++)  {
358*1b1247ddSLaxman Dewangan 		ret = rc5t583_write(rc5t583->dev, gpedge_add[i],
359*1b1247ddSLaxman Dewangan 				rc5t583->gpedge_reg[i]);
360*1b1247ddSLaxman Dewangan 		if (ret < 0)
361*1b1247ddSLaxman Dewangan 			dev_warn(rc5t583->dev,
362*1b1247ddSLaxman Dewangan 				"Error in writing reg 0x%02x error: %d\n",
363*1b1247ddSLaxman Dewangan 				gpedge_add[i], ret);
364*1b1247ddSLaxman Dewangan 	}
365*1b1247ddSLaxman Dewangan 
366*1b1247ddSLaxman Dewangan 	ret = rc5t583_write(rc5t583->dev, RC5T583_INTC_INTEN, 0x0);
367*1b1247ddSLaxman Dewangan 	if (ret < 0)
368*1b1247ddSLaxman Dewangan 		dev_warn(rc5t583->dev,
369*1b1247ddSLaxman Dewangan 			"Error in writing reg 0x%02x error: %d\n",
370*1b1247ddSLaxman Dewangan 			RC5T583_INTC_INTEN, ret);
371*1b1247ddSLaxman Dewangan 
372*1b1247ddSLaxman Dewangan 	/* Clear all interrupts in case they woke up active. */
373*1b1247ddSLaxman Dewangan 	for (i = 0; i < RC5T583_MAX_INTERRUPT_MASK_REGS; i++)  {
374*1b1247ddSLaxman Dewangan 		ret = rc5t583_write(rc5t583->dev, irq_clr_add[i], 0);
375*1b1247ddSLaxman Dewangan 		if (ret < 0)
376*1b1247ddSLaxman Dewangan 			dev_warn(rc5t583->dev,
377*1b1247ddSLaxman Dewangan 				"Error in writing reg 0x%02x error: %d\n",
378*1b1247ddSLaxman Dewangan 				irq_clr_add[i], ret);
379*1b1247ddSLaxman Dewangan 	}
380*1b1247ddSLaxman Dewangan 
381*1b1247ddSLaxman Dewangan 	rc5t583->irq_base = irq_base;
382*1b1247ddSLaxman Dewangan 	rc5t583->chip_irq = irq;
383*1b1247ddSLaxman Dewangan 
384*1b1247ddSLaxman Dewangan 	for (i = 0; i < RC5T583_MAX_IRQS; i++) {
385*1b1247ddSLaxman Dewangan 		int __irq = i + rc5t583->irq_base;
386*1b1247ddSLaxman Dewangan 		irq_set_chip_data(__irq, rc5t583);
387*1b1247ddSLaxman Dewangan 		irq_set_chip_and_handler(__irq, &rc5t583_irq_chip,
388*1b1247ddSLaxman Dewangan 					 handle_simple_irq);
389*1b1247ddSLaxman Dewangan 		irq_set_nested_thread(__irq, 1);
390*1b1247ddSLaxman Dewangan #ifdef CONFIG_ARM
391*1b1247ddSLaxman Dewangan 		set_irq_flags(__irq, IRQF_VALID);
392*1b1247ddSLaxman Dewangan #endif
393*1b1247ddSLaxman Dewangan 	}
394*1b1247ddSLaxman Dewangan 
395*1b1247ddSLaxman Dewangan 	ret = request_threaded_irq(irq, NULL, rc5t583_irq, IRQF_ONESHOT,
396*1b1247ddSLaxman Dewangan 				"rc5t583", rc5t583);
397*1b1247ddSLaxman Dewangan 	if (ret < 0)
398*1b1247ddSLaxman Dewangan 		dev_err(rc5t583->dev,
399*1b1247ddSLaxman Dewangan 			"Error in registering interrupt error: %d\n", ret);
400*1b1247ddSLaxman Dewangan 	return ret;
401*1b1247ddSLaxman Dewangan }
402*1b1247ddSLaxman Dewangan 
403*1b1247ddSLaxman Dewangan int rc5t583_irq_exit(struct rc5t583 *rc5t583)
404*1b1247ddSLaxman Dewangan {
405*1b1247ddSLaxman Dewangan 	if (rc5t583->chip_irq)
406*1b1247ddSLaxman Dewangan 		free_irq(rc5t583->chip_irq, rc5t583);
407*1b1247ddSLaxman Dewangan 	return 0;
408*1b1247ddSLaxman Dewangan }
409