arm64: dts: rk3368: add iep device node
[firefly-linux-kernel-4.4.55.git] / drivers / mfd / rk818-irq.c
1 /*
2  * rk818-irq.c 
3  *
4  * Author: zhangqing <zhangqing@rock-chips.com>
5  *
6  *  This program is free software; you can redistribute it and/or modify it
7  *  under  the terms of the GNU General  Public License as published by the
8  *  Free Software Foundation;  either version 2 of the License, or (at your
9  *  option) any later version.
10  *
11  */
12
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/bug.h>
17 #include <linux/device.h>
18 #include <linux/interrupt.h>
19 #include <linux/irq.h>
20 #include <linux/gpio.h>
21 #include <linux/mfd/rk818.h>
22 #include <linux/wakelock.h>
23 #include <linux/kthread.h>
24 #include <linux/irqdomain.h>
25 #include <linux/regmap.h>
26
27
28 static inline int irq_to_rk818_irq(struct rk818 *rk818,
29                                                         int irq)
30 {
31         return (irq - rk818->chip_irq);
32 }
33
34 /*
35  * This is a threaded IRQ handler so can access I2C/SPI.  Since all
36  * interrupts are clear on read the IRQ line will be reasserted and
37  * the physical IRQ will be handled again if another interrupt is
38  * asserted while we run - in the normal course of events this is a
39  * rare occurrence so we save I2C/SPI reads.  We're also assuming that
40  * it's rare to get lots of interrupts firing simultaneously so try to
41  * minimise I/O.
42  */
43 static irqreturn_t rk818_irq(int irq, void *irq_data)
44 {
45         struct rk818 *rk818 = irq_data;
46         u32 irq_sts;
47         u32 irq_mask;
48         u8 reg;
49         int i, cur_irq;
50         //printk(" rk818 irq %d \n",irq);       
51         wake_lock(&rk818->irq_wake);    
52         rk818_i2c_read(rk818, RK818_INT_STS_REG1, 1, &reg);
53         irq_sts = reg;
54         rk818_i2c_read(rk818, RK818_INT_STS_REG2, 1, &reg);
55         irq_sts |= reg << 8;
56         
57         rk818_i2c_read(rk818, RK818_INT_STS_MSK_REG1, 1, &reg);
58         irq_mask = reg;
59         rk818_i2c_read(rk818, RK818_INT_STS_MSK_REG2, 1, &reg);
60         irq_mask |= reg << 8;
61
62         irq_sts &= ~irq_mask;
63
64         if (!irq_sts)
65         {
66                 wake_unlock(&rk818->irq_wake);
67                 return IRQ_NONE;
68         }
69
70         for (i = 0; i < rk818->irq_num; i++) {
71
72                 if (!(irq_sts & (1 << i)))
73                         continue;
74
75                 cur_irq = irq_find_mapping(rk818->irq_domain, i);
76
77                 if (cur_irq)
78                         handle_nested_irq(cur_irq);
79         }
80
81         /* Write the STS register back to clear IRQs we handled */
82         reg = irq_sts & 0xFF;
83         irq_sts >>= 8;
84         rk818_i2c_write(rk818, RK818_INT_STS_REG1, 1, reg);
85         reg = irq_sts & 0xFF;
86         rk818_i2c_write(rk818, RK818_INT_STS_REG2, 1, reg);
87         wake_unlock(&rk818->irq_wake);
88         return IRQ_HANDLED;
89 }
90
91 static void rk818_irq_lock(struct irq_data *data)
92 {
93         struct rk818 *rk818 = irq_data_get_irq_chip_data(data);
94
95         mutex_lock(&rk818->irq_lock);
96 }
97
98 static void rk818_irq_sync_unlock(struct irq_data *data)
99 {
100         struct rk818 *rk818 = irq_data_get_irq_chip_data(data);
101         u32 reg_mask;
102         u8 reg;
103
104         rk818_i2c_read(rk818, RK818_INT_STS_MSK_REG1, 1, &reg);
105         reg_mask = reg;
106         rk818_i2c_read(rk818, RK818_INT_STS_MSK_REG2, 1, &reg);
107         reg_mask |= reg << 8;
108
109         if (rk818->irq_mask != reg_mask) {
110                 reg = rk818->irq_mask & 0xff;
111 //              rk818_i2c_write(rk818, RK818_INT_STS_MSK_REG1, 1, reg);
112                 reg = rk818->irq_mask >> 8 & 0xff;
113 //              rk818_i2c_write(rk818, RK818_INT_STS_MSK_REG2, 1, reg);
114         }
115         mutex_unlock(&rk818->irq_lock);
116 }
117
118 static void rk818_irq_enable(struct irq_data *data)
119 {
120         struct rk818 *rk818 = irq_data_get_irq_chip_data(data);
121
122         rk818->irq_mask &= ~( 1 << irq_to_rk818_irq(rk818, data->irq));
123 }
124
125 static void rk818_irq_disable(struct irq_data *data)
126 {
127         struct rk818 *rk818 = irq_data_get_irq_chip_data(data);
128
129         rk818->irq_mask |= ( 1 << irq_to_rk818_irq(rk818, data->irq));
130 }
131
132 #ifdef CONFIG_PM_SLEEP
133 static int rk818_irq_set_wake(struct irq_data *data, unsigned int enable)
134 {
135         struct rk818 *rk818 = irq_data_get_irq_chip_data(data);
136         return irq_set_irq_wake(rk818->chip_irq, enable);
137 }
138 #else
139 #define rk818_irq_set_wake NULL
140 #endif
141
142 static struct irq_chip rk818_irq_chip = {
143         .name = "rk818",
144         .irq_bus_lock = rk818_irq_lock,
145         .irq_bus_sync_unlock = rk818_irq_sync_unlock,
146         .irq_disable = rk818_irq_disable,
147         .irq_enable = rk818_irq_enable,
148         .irq_set_wake = rk818_irq_set_wake,
149 };
150
151 static int rk818_irq_domain_map(struct irq_domain *d, unsigned int irq,
152                                         irq_hw_number_t hw)
153 {
154         struct rk818 *rk818 = d->host_data;
155
156         irq_set_chip_data(irq, rk818);
157         irq_set_chip_and_handler(irq, &rk818_irq_chip, handle_edge_irq);
158         irq_set_nested_thread(irq, 1);
159 #ifdef CONFIG_ARM
160         set_irq_flags(irq, IRQF_VALID);
161 #else
162         irq_set_noprobe(irq);
163 #endif
164         return 0;
165 }
166
167 static struct irq_domain_ops rk818_irq_domain_ops = {
168         .map = rk818_irq_domain_map,
169 };
170
171 int rk818_irq_init(struct rk818 *rk818, int irq,struct rk818_board *pdata)
172 {
173         struct irq_domain *domain;
174         int ret,val,irq_type,flags;
175         u8 reg;
176
177 //      printk("%s,line=%d\n", __func__,__LINE__);      
178         if (!irq) {
179                 dev_warn(rk818->dev, "No interrupt support, no core IRQ\n");
180                 return 0;
181         }
182
183         /* Clear unattended interrupts */
184         rk818_i2c_read(rk818, RK818_INT_STS_REG1, 1, &reg);
185         rk818_i2c_write(rk818, RK818_INT_STS_REG1, 1, reg);
186         rk818_i2c_read(rk818, RK818_INT_STS_REG2, 1, &reg);
187         rk818_i2c_write(rk818, RK818_INT_STS_REG2, 1, reg);
188         rk818_i2c_read(rk818, RK818_RTC_STATUS_REG, 1, &reg);   
189         rk818_i2c_write(rk818, RK818_RTC_STATUS_REG, 1, reg);//clear alarm and timer interrupt
190
191         /* Mask top level interrupts */
192         rk818->irq_mask = 0xFFFFFF;
193         mutex_init(&rk818->irq_lock);   
194         wake_lock_init(&rk818->irq_wake, WAKE_LOCK_SUSPEND, "rk818_irq_wake");
195         rk818->irq_num = RK818_NUM_IRQ;
196         rk818->irq_gpio = pdata->irq_gpio;
197         if (rk818->irq_gpio && !rk818->chip_irq) {
198                 rk818->chip_irq = gpio_to_irq(rk818->irq_gpio);
199
200                 if (rk818->irq_gpio) {
201                         ret = gpio_request(rk818->irq_gpio, "rk818_pmic_irq");
202                         if (ret < 0) {
203                                 dev_err(rk818->dev,
204                                         "Failed to request gpio %d with ret:"
205                                         "%d\n", rk818->irq_gpio, ret);
206                                 return IRQ_NONE;
207                         }
208                         gpio_direction_input(rk818->irq_gpio);
209                         val = gpio_get_value(rk818->irq_gpio);
210                         if (val){
211                                 irq_type = IRQ_TYPE_LEVEL_LOW;
212                                 flags = IRQF_TRIGGER_FALLING;
213                         }
214                         else{
215                                 irq_type = IRQ_TYPE_LEVEL_HIGH;
216                                 flags = IRQF_TRIGGER_RISING;
217                         }
218                         gpio_free(rk818->irq_gpio);
219                         pr_info("%s: rk818_pmic_irq=%x\n", __func__, val);
220                 }
221         }
222         
223         domain = irq_domain_add_linear(NULL, RK818_NUM_IRQ,
224                                         &rk818_irq_domain_ops, rk818);
225         if (!domain) {
226                 dev_err(rk818->dev, "could not create irq domain\n");
227                 return -ENODEV;
228         }
229         rk818->irq_domain = domain;
230
231         ret = request_threaded_irq(rk818->chip_irq, NULL, rk818_irq, flags | IRQF_ONESHOT, "rk818", rk818);
232
233         irq_set_irq_type(rk818->chip_irq, irq_type);
234         enable_irq_wake(rk818->chip_irq);
235         if (ret != 0)
236                 dev_err(rk818->dev, "Failed to request IRQ: %d\n", ret);
237         
238         return ret;
239 }
240
241 int rk818_irq_exit(struct rk818 *rk818)
242 {
243         if (rk818->chip_irq)
244                 free_irq(rk818->chip_irq, rk818);
245         return 0;
246 }