Merge remote-tracking branch 'origin/upstream/linux-linaro-lsk-v3.10-android+android...
[firefly-linux-kernel-4.4.55.git] / drivers / mfd / rk808-irq.c
1 /*
2  * rk808-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/rk808.h>
22 #include <linux/wakelock.h>
23 #include <linux/kthread.h>
24
25 static inline int irq_to_rk808_irq(struct rk808 *rk808,
26                                                         int irq)
27 {
28         return (irq - rk808->irq_base);
29 }
30
31 /*
32  * This is a threaded IRQ handler so can access I2C/SPI.  Since all
33  * interrupts are clear on read the IRQ line will be reasserted and
34  * the physical IRQ will be handled again if another interrupt is
35  * asserted while we run - in the normal course of events this is a
36  * rare occurrence so we save I2C/SPI reads.  We're also assuming that
37  * it's rare to get lots of interrupts firing simultaneously so try to
38  * minimise I/O.
39  */
40 static irqreturn_t rk808_irq(int irq, void *irq_data)
41 {
42         struct rk808 *rk808 = irq_data;
43         u32 irq_sts;
44         u32 irq_mask;
45         u8 reg;
46         int i;
47         //printk(" rk808 irq %d \n",irq);       
48         wake_lock(&rk808->irq_wake);    
49         rk808_i2c_read(rk808, RK808_INT_STS_REG1, 1, &reg);
50         irq_sts = reg;
51         rk808_i2c_read(rk808, RK808_INT_STS_REG2, 1, &reg);
52         irq_sts |= reg << 8;
53         
54         rk808_i2c_read(rk808, RK808_INT_STS_MSK_REG1, 1, &reg);
55         irq_mask = reg;
56         rk808_i2c_read(rk808, RK808_INT_STS_MSK_REG2, 1, &reg);
57         irq_mask |= reg << 8;
58
59         irq_sts &= ~irq_mask;
60
61         if (!irq_sts)
62         {
63                 wake_unlock(&rk808->irq_wake);
64                 return IRQ_NONE;
65         }
66
67         for (i = 0; i < rk808->irq_num; i++) {
68
69                 if (!(irq_sts & (1 << i)))
70                         continue;
71
72                 handle_nested_irq(rk808->irq_base + i);
73         }
74
75         /* Write the STS register back to clear IRQs we handled */
76         reg = irq_sts & 0xFF;
77         irq_sts >>= 8;
78         rk808_i2c_write(rk808, RK808_INT_STS_REG1, 1, reg);
79         reg = irq_sts & 0xFF;
80         rk808_i2c_write(rk808, RK808_INT_STS_REG2, 1, reg);
81         wake_unlock(&rk808->irq_wake);
82         return IRQ_HANDLED;
83 }
84
85 static void rk808_irq_lock(struct irq_data *data)
86 {
87         struct rk808 *rk808 = irq_data_get_irq_chip_data(data);
88
89         mutex_lock(&rk808->irq_lock);
90 }
91
92 static void rk808_irq_sync_unlock(struct irq_data *data)
93 {
94         struct rk808 *rk808 = irq_data_get_irq_chip_data(data);
95         u32 reg_mask;
96         u8 reg;
97
98         rk808_i2c_read(rk808, RK808_INT_STS_MSK_REG1, 1, &reg);
99         reg_mask = reg;
100         rk808_i2c_read(rk808, RK808_INT_STS_MSK_REG2, 1, &reg);
101         reg_mask |= reg << 8;
102
103         if (rk808->irq_mask != reg_mask) {
104                 reg = rk808->irq_mask & 0xff;
105 //              rk808_i2c_write(rk808, RK808_INT_STS_MSK_REG1, 1, reg);
106                 reg = rk808->irq_mask >> 8 & 0xff;
107 //              rk808_i2c_write(rk808, RK808_INT_STS_MSK_REG2, 1, reg);
108         }
109         mutex_unlock(&rk808->irq_lock);
110 }
111
112 static void rk808_irq_enable(struct irq_data *data)
113 {
114         struct rk808 *rk808 = irq_data_get_irq_chip_data(data);
115
116         rk808->irq_mask &= ~( 1 << irq_to_rk808_irq(rk808, data->irq));
117 }
118
119 static void rk808_irq_disable(struct irq_data *data)
120 {
121         struct rk808 *rk808 = irq_data_get_irq_chip_data(data);
122
123         rk808->irq_mask |= ( 1 << irq_to_rk808_irq(rk808, data->irq));
124 }
125
126 #ifdef CONFIG_PM_SLEEP
127 static int rk808_irq_set_wake(struct irq_data *data, unsigned int enable)
128 {
129         struct rk808 *rk808 = irq_data_get_irq_chip_data(data);
130         return irq_set_irq_wake(rk808->chip_irq, enable);
131 }
132 #else
133 #define rk808_irq_set_wake NULL
134 #endif
135
136 static struct irq_chip rk808_irq_chip = {
137         .name = "rk808",
138         .irq_bus_lock = rk808_irq_lock,
139         .irq_bus_sync_unlock = rk808_irq_sync_unlock,
140         .irq_disable = rk808_irq_disable,
141         .irq_enable = rk808_irq_enable,
142         .irq_set_wake = rk808_irq_set_wake,
143 };
144
145 int rk808_irq_init(struct rk808 *rk808, int irq,struct rk808_platform_data *pdata)
146 {
147         int ret, cur_irq;
148         int flags = IRQF_ONESHOT;
149         u8 reg;
150
151         printk("%s,line=%d\n", __func__,__LINE__);
152
153
154         if (!irq) {
155                 dev_warn(rk808->dev, "No interrupt support, no core IRQ\n");
156                 return 0;
157         }
158
159         if (!pdata || !pdata->irq_base) {
160                 dev_warn(rk808->dev, "No interrupt support, no IRQ base\n");
161                 return 0;
162         }
163
164         /* Clear unattended interrupts */
165         rk808_i2c_read(rk808, RK808_INT_STS_REG1, 1, &reg);
166         rk808_i2c_write(rk808, RK808_INT_STS_REG1, 1, reg);
167         rk808_i2c_read(rk808, RK808_INT_STS_REG2, 1, &reg);
168         rk808_i2c_write(rk808, RK808_INT_STS_REG2, 1, reg);
169         rk808_i2c_read(rk808, RK808_RTC_STATUS_REG, 1, &reg);   
170         rk808_i2c_write(rk808, RK808_RTC_STATUS_REG, 1, reg);//clear alarm and timer interrupt
171
172         /* Mask top level interrupts */
173         rk808->irq_mask = 0xFFFFFF;
174
175         mutex_init(&rk808->irq_lock);   
176         wake_lock_init(&rk808->irq_wake, WAKE_LOCK_SUSPEND, "rk808_irq_wake");
177         rk808->chip_irq = irq;
178         rk808->irq_base = pdata->irq_base;
179
180         rk808->irq_num = RK808_NUM_IRQ;
181
182         /* Register with genirq */
183         for (cur_irq = rk808->irq_base;
184              cur_irq < rk808->irq_num + rk808->irq_base;
185              cur_irq++) {
186                 irq_set_chip_data(cur_irq, rk808);
187                 irq_set_chip_and_handler(cur_irq, &rk808_irq_chip,
188                                          handle_edge_irq);
189                 irq_set_nested_thread(cur_irq, 1);
190
191                 /* ARM needs us to explicitly flag the IRQ as valid
192                  * and will set them noprobe when we do so. */
193 #ifdef CONFIG_ARM
194                 set_irq_flags(cur_irq, IRQF_VALID);
195 #else
196                 irq_set_noprobe(cur_irq);
197 #endif
198         }
199
200         ret = request_threaded_irq(irq, NULL, rk808_irq, flags, "rk808", rk808);
201
202         irq_set_irq_type(irq, IRQ_TYPE_LEVEL_LOW);
203
204         if (ret != 0)
205                 dev_err(rk808->dev, "Failed to request IRQ: %d\n", ret);
206         
207         return ret;
208 }
209
210 int rk808_irq_exit(struct rk808 *rk808)
211 {
212         if (rk808->chip_irq)
213                 free_irq(rk808->chip_irq, rk808);
214         return 0;
215 }