Merge tag 'lsk-v3.10-android-14.07' into develop-3.10
[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;
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                 handle_nested_irq(rk818->irq_base + i);
76         }
77
78         /* Write the STS register back to clear IRQs we handled */
79         reg = irq_sts & 0xFF;
80         irq_sts >>= 8;
81         rk818_i2c_write(rk818, RK818_INT_STS_REG1, 1, reg);
82         reg = irq_sts & 0xFF;
83         rk818_i2c_write(rk818, RK818_INT_STS_REG2, 1, reg);
84         wake_unlock(&rk818->irq_wake);
85         return IRQ_HANDLED;
86 }
87
88 static void rk818_irq_lock(struct irq_data *data)
89 {
90         struct rk818 *rk818 = irq_data_get_irq_chip_data(data);
91
92         mutex_lock(&rk818->irq_lock);
93 }
94
95 static void rk818_irq_sync_unlock(struct irq_data *data)
96 {
97         struct rk818 *rk818 = irq_data_get_irq_chip_data(data);
98         u32 reg_mask;
99         u8 reg;
100
101         rk818_i2c_read(rk818, RK818_INT_STS_MSK_REG1, 1, &reg);
102         reg_mask = reg;
103         rk818_i2c_read(rk818, RK818_INT_STS_MSK_REG2, 1, &reg);
104         reg_mask |= reg << 8;
105
106         if (rk818->irq_mask != reg_mask) {
107                 reg = rk818->irq_mask & 0xff;
108 //              rk818_i2c_write(rk818, RK818_INT_STS_MSK_REG1, 1, reg);
109                 reg = rk818->irq_mask >> 8 & 0xff;
110 //              rk818_i2c_write(rk818, RK818_INT_STS_MSK_REG2, 1, reg);
111         }
112         mutex_unlock(&rk818->irq_lock);
113 }
114
115 static void rk818_irq_enable(struct irq_data *data)
116 {
117         struct rk818 *rk818 = irq_data_get_irq_chip_data(data);
118
119         rk818->irq_mask &= ~( 1 << irq_to_rk818_irq(rk818, data->irq));
120 }
121
122 static void rk818_irq_disable(struct irq_data *data)
123 {
124         struct rk818 *rk818 = irq_data_get_irq_chip_data(data);
125
126         rk818->irq_mask |= ( 1 << irq_to_rk818_irq(rk818, data->irq));
127 }
128
129 #ifdef CONFIG_PM_SLEEP
130 static int rk818_irq_set_wake(struct irq_data *data, unsigned int enable)
131 {
132         struct rk818 *rk818 = irq_data_get_irq_chip_data(data);
133         return irq_set_irq_wake(rk818->chip_irq, enable);
134 }
135 #else
136 #define rk818_irq_set_wake NULL
137 #endif
138
139 static struct irq_chip rk818_irq_chip = {
140         .name = "rk818",
141         .irq_bus_lock = rk818_irq_lock,
142         .irq_bus_sync_unlock = rk818_irq_sync_unlock,
143         .irq_disable = rk818_irq_disable,
144         .irq_enable = rk818_irq_enable,
145         .irq_set_wake = rk818_irq_set_wake,
146 };
147
148 static int rk818_irq_domain_map(struct irq_domain *d, unsigned int irq,
149                                         irq_hw_number_t hw)
150 {
151         struct rk818 *rk818 = d->host_data;
152
153         irq_set_chip_data(irq, rk818);
154         irq_set_chip_and_handler(irq, &rk818_irq_chip, handle_edge_irq);
155         irq_set_nested_thread(irq, 1);
156 #ifdef CONFIG_ARM
157         set_irq_flags(irq, IRQF_VALID);
158 #else
159         irq_set_noprobe(irq);
160 #endif
161         return 0;
162 }
163
164 static struct irq_domain_ops rk818_irq_domain_ops = {
165         .map = rk818_irq_domain_map,
166 };
167
168 int rk818_irq_init(struct rk818 *rk818, int irq,struct rk818_board *pdata)
169 {
170         struct irq_domain *domain;
171         int ret,val,irq_type,flags;
172         u8 reg;
173
174 //      printk("%s,line=%d\n", __func__,__LINE__);      
175         if (!irq) {
176                 dev_warn(rk818->dev, "No interrupt support, no core IRQ\n");
177                 return 0;
178         }
179
180         /* Clear unattended interrupts */
181         rk818_i2c_read(rk818, RK818_INT_STS_REG1, 1, &reg);
182         rk818_i2c_write(rk818, RK818_INT_STS_REG1, 1, reg);
183         rk818_i2c_read(rk818, RK818_INT_STS_REG2, 1, &reg);
184         rk818_i2c_write(rk818, RK818_INT_STS_REG2, 1, reg);
185         rk818_i2c_read(rk818, RK818_RTC_STATUS_REG, 1, &reg);   
186         rk818_i2c_write(rk818, RK818_RTC_STATUS_REG, 1, reg);//clear alarm and timer interrupt
187
188         /* Mask top level interrupts */
189         rk818->irq_mask = 0xFFFFFF;
190         mutex_init(&rk818->irq_lock);   
191         wake_lock_init(&rk818->irq_wake, WAKE_LOCK_SUSPEND, "rk818_irq_wake");
192         rk818->irq_num = RK818_NUM_IRQ;
193         rk818->irq_gpio = pdata->irq_gpio;
194         if (rk818->irq_gpio && !rk818->chip_irq) {
195                 rk818->chip_irq = gpio_to_irq(rk818->irq_gpio);
196
197                 if (rk818->irq_gpio) {
198                         ret = gpio_request(rk818->irq_gpio, "rk818_pmic_irq");
199                         if (ret < 0) {
200                                 dev_err(rk818->dev,
201                                         "Failed to request gpio %d with ret:"
202                                         "%d\n", rk818->irq_gpio, ret);
203                                 return IRQ_NONE;
204                         }
205                         gpio_direction_input(rk818->irq_gpio);
206                         val = gpio_get_value(rk818->irq_gpio);
207                         if (val){
208                                 irq_type = IRQ_TYPE_LEVEL_LOW;
209                                 flags = IRQF_TRIGGER_FALLING;
210                         }
211                         else{
212                                 irq_type = IRQ_TYPE_LEVEL_HIGH;
213                                 flags = IRQF_TRIGGER_RISING;
214                         }
215                         gpio_free(rk818->irq_gpio);
216                         pr_info("%s: rk818_pmic_irq=%x\n", __func__, val);
217                 }
218         }
219         
220         domain = irq_domain_add_linear(NULL, RK818_NUM_IRQ,
221                                         &rk818_irq_domain_ops, rk818);
222         if (!domain) {
223                 dev_err(rk818->dev, "could not create irq domain\n");
224                 return -ENODEV;
225         }
226         rk818->irq_domain = domain;
227
228         ret = request_threaded_irq(rk818->chip_irq, NULL, rk818_irq, flags | IRQF_ONESHOT, "rk818", rk818);
229
230         irq_set_irq_type(rk818->chip_irq, irq_type);
231         enable_irq_wake(rk818->chip_irq);
232         if (ret != 0)
233                 dev_err(rk818->dev, "Failed to request IRQ: %d\n", ret);
234         
235         return ret;
236 }
237
238 int rk818_irq_exit(struct rk818 *rk818)
239 {
240         if (rk818->chip_irq)
241                 free_irq(rk818->chip_irq, rk818);
242         return 0;
243 }