drm/rockchip: vop: optimize register take effect check
[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 #include <linux/irqdomain.h>
25 #include <linux/regmap.h>
26
27
28 static inline int irq_to_rk808_irq(struct rk808 *rk808,
29                                                         int irq)
30 {
31         return (irq - rk808->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 rk808_irq(int irq, void *irq_data)
44 {
45         struct rk808 *rk808 = irq_data;
46         u32 irq_sts;
47         u32 irq_mask;
48         u8 reg;
49         int i, cur_irq;
50 //      printk("%s,line=%d\n", __func__,__LINE__);      
51
52         wake_lock(&rk808->irq_wake);    
53         rk808_i2c_read(rk808, RK808_INT_STS_REG1, 1, &reg);
54         irq_sts = reg;
55         rk808_i2c_read(rk808, RK808_INT_STS_REG2, 1, &reg);
56         irq_sts |= reg << 8;
57         
58         rk808_i2c_read(rk808, RK808_INT_STS_MSK_REG1, 1, &reg);
59         irq_mask = reg;
60         rk808_i2c_read(rk808, RK808_INT_STS_MSK_REG2, 1, &reg);
61         irq_mask |= reg << 8;
62
63         irq_sts &= ~irq_mask;
64
65         if (!irq_sts)
66         {
67                 wake_unlock(&rk808->irq_wake);
68                 return IRQ_NONE;
69         }
70
71         for (i = 0; i < rk808->irq_num; i++) {
72                 if (!(irq_sts & (1 << i)))
73                         continue;
74                 cur_irq = irq_find_mapping(rk808->irq_domain, i);
75
76                 if (cur_irq)
77                 handle_nested_irq(cur_irq);
78         }
79
80         /* Write the STS register back to clear IRQs we handled */
81         reg = irq_sts & 0xFF;
82         irq_sts >>= 8;
83         rk808_i2c_write(rk808, RK808_INT_STS_REG1, 1, reg);
84         reg = irq_sts & 0xFF;
85         rk808_i2c_write(rk808, RK808_INT_STS_REG2, 1, reg);
86         wake_unlock(&rk808->irq_wake);
87         return IRQ_HANDLED;
88 }
89
90 static void rk808_irq_lock(struct irq_data *data)
91 {
92         struct rk808 *rk808 = irq_data_get_irq_chip_data(data);
93
94         mutex_lock(&rk808->irq_lock);
95 }
96
97 static void rk808_irq_sync_unlock(struct irq_data *data)
98 {
99         struct rk808 *rk808 = irq_data_get_irq_chip_data(data);
100         u32 reg_mask;
101         u8 reg;
102
103         rk808_i2c_read(rk808, RK808_INT_STS_MSK_REG1, 1, &reg);
104         reg_mask = reg;
105         rk808_i2c_read(rk808, RK808_INT_STS_MSK_REG2, 1, &reg);
106         reg_mask |= reg << 8;
107
108         if (rk808->irq_mask != reg_mask) {
109                 reg = rk808->irq_mask & 0xff;
110 //              rk808_i2c_write(rk808, RK808_INT_STS_MSK_REG1, 1, reg);
111                 reg = rk808->irq_mask >> 8 & 0xff;
112 //              rk808_i2c_write(rk808, RK808_INT_STS_MSK_REG2, 1, reg);
113         }
114         mutex_unlock(&rk808->irq_lock);
115 }
116
117 static void rk808_irq_enable(struct irq_data *data)
118 {
119         struct rk808 *rk808 = irq_data_get_irq_chip_data(data);
120
121         rk808->irq_mask &= ~( 1 << irq_to_rk808_irq(rk808, data->irq));
122 }
123
124 static void rk808_irq_disable(struct irq_data *data)
125 {
126         struct rk808 *rk808 = irq_data_get_irq_chip_data(data);
127
128         rk808->irq_mask |= ( 1 << irq_to_rk808_irq(rk808, data->irq));
129 }
130
131 #ifdef CONFIG_PM_SLEEP
132 static int rk808_irq_set_wake(struct irq_data *data, unsigned int enable)
133 {
134         struct rk808 *rk808 = irq_data_get_irq_chip_data(data);
135         return irq_set_irq_wake(rk808->chip_irq, enable);
136 }
137 #else
138 #define rk808_irq_set_wake NULL
139 #endif
140
141 static struct irq_chip rk808_irq_chip = {
142         .name = "rk808",
143         .irq_bus_lock = rk808_irq_lock,
144         .irq_bus_sync_unlock = rk808_irq_sync_unlock,
145         .irq_disable = rk808_irq_disable,
146         .irq_enable = rk808_irq_enable,
147         .irq_set_wake = rk808_irq_set_wake,
148 };
149 static int rk808_irq_domain_map(struct irq_domain *d, unsigned int irq,
150                                         irq_hw_number_t hw)
151 {
152         struct rk808 *rk808 = d->host_data;
153
154         irq_set_chip_data(irq, rk808);
155         irq_set_chip_and_handler(irq, &rk808_irq_chip, handle_edge_irq);
156         irq_set_nested_thread(irq, 1);
157 #ifdef CONFIG_ARM
158         set_irq_flags(irq, IRQF_VALID);
159 #else
160         irq_set_noprobe(irq);
161 #endif
162         return 0;
163 }
164
165 static struct irq_domain_ops rk808_irq_domain_ops = {
166         .map = rk808_irq_domain_map,
167 };
168
169 int rk808_irq_init(struct rk808 *rk808, int irq,struct rk808_board *pdata)
170 {
171         struct irq_domain *domain;
172         int ret,val,irq_type,flags;
173         u8 reg;
174
175 //      printk("%s,line=%d\n", __func__,__LINE__);      
176         if (!irq) {
177                 dev_warn(rk808->dev, "No interrupt support, no core IRQ\n");
178                 return 0;
179         }
180
181         /* Clear unattended interrupts */
182         rk808_i2c_read(rk808, RK808_INT_STS_REG1, 1, &reg);
183         rk808_i2c_write(rk808, RK808_INT_STS_REG1, 1, reg);
184         rk808_i2c_read(rk808, RK808_INT_STS_REG2, 1, &reg);
185         rk808_i2c_write(rk808, RK808_INT_STS_REG2, 1, reg);
186         rk808_i2c_read(rk808, RK808_RTC_STATUS_REG, 1, &reg);   
187         rk808_i2c_write(rk808, RK808_RTC_STATUS_REG, 1, reg);//clear alarm and timer interrupt
188
189         /* Mask top level interrupts */
190         rk808->irq_mask = 0xFFFFFF;
191         mutex_init(&rk808->irq_lock);   
192         wake_lock_init(&rk808->irq_wake, WAKE_LOCK_SUSPEND, "rk808_irq_wake");
193         rk808->irq_num = RK808_NUM_IRQ;
194         rk808->irq_gpio = pdata->irq_gpio;
195         if (rk808->irq_gpio && !rk808->chip_irq) {
196                 rk808->chip_irq = gpio_to_irq(rk808->irq_gpio);
197
198                 if (rk808->irq_gpio) {
199                         ret = gpio_request(rk808->irq_gpio, "rk808_pmic_irq");
200                         if (ret < 0) {
201                                 dev_err(rk808->dev,
202                                         "Failed to request gpio %d with ret:"
203                                         "%d\n", rk808->irq_gpio, ret);
204                                 return IRQ_NONE;
205                         }
206                         gpio_direction_input(rk808->irq_gpio);
207                         val = gpio_get_value(rk808->irq_gpio);
208                         if (val){
209                                 irq_type = IRQ_TYPE_LEVEL_LOW;
210                                 flags = IRQF_TRIGGER_FALLING;
211                         }
212                         else{
213                                 irq_type = IRQ_TYPE_LEVEL_HIGH;
214                                 flags = IRQF_TRIGGER_RISING;
215                         }
216                         gpio_free(rk808->irq_gpio);
217                         pr_info("%s: rk808_pmic_irq=%x\n", __func__, val);
218                 }
219         }
220         
221         domain = irq_domain_add_linear(NULL, RK808_NUM_IRQ,
222                                         &rk808_irq_domain_ops, rk808);
223         if (!domain) {
224                 dev_err(rk808->dev, "could not create irq domain\n");
225                 return -ENODEV;
226         }
227         rk808->irq_domain = domain;
228
229         ret = request_threaded_irq(rk808->chip_irq, NULL, rk808_irq, flags | IRQF_ONESHOT, "rk808", rk808);
230
231         irq_set_irq_type(rk808->chip_irq, irq_type);
232         enable_irq_wake(rk808->chip_irq);
233
234         if (ret != 0)
235                 dev_err(rk808->dev, "Failed to request IRQ: %d\n", ret);
236         
237         return ret;
238 }
239
240 int rk808_irq_exit(struct rk808 *rk808)
241 {
242         if (rk808->chip_irq)
243                 free_irq(rk808->chip_irq, rk808);
244         return 0;
245 }