Revert "gpio debug"
[firefly-linux-kernel-4.4.55.git] / drivers / mfd / tps65910-irq.c
1 /*
2  * tps65910-irq.c  --  TI TPS6591x
3  *
4  * Copyright 2010 Texas Instruments Inc.
5  *
6  * Author: Graeme Gregory <gg@slimlogic.co.uk>
7  * Author: Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>
8  *
9  *  This program is free software; you can redistribute it and/or modify it
10  *  under  the terms of the GNU General  Public License as published by the
11  *  Free Software Foundation;  either version 2 of the License, or (at your
12  *  option) any later version.
13  *
14  */
15
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/bug.h>
20 #include <linux/device.h>
21 #include <linux/interrupt.h>
22 #include <linux/irq.h>
23 #include <linux/gpio.h>
24 #include <linux/mfd/tps65910.h>
25 #include <linux/wakelock.h>
26 #include <linux/kthread.h>
27
28 static inline int irq_to_tps65910_irq(struct tps65910 *tps65910,
29                                                         int irq)
30 {
31         return (irq - tps65910->irq_base);
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 tps65910_irq(int irq, void *irq_data)
44 {
45         struct tps65910 *tps65910 = irq_data;
46         u32 irq_sts;
47         u32 irq_mask;
48         u8 reg;
49         int i;
50         
51         wake_lock(&tps65910->irq_wake); 
52         tps65910->read(tps65910, TPS65910_INT_STS, 1, &reg);
53         irq_sts = reg;
54         tps65910->read(tps65910, TPS65910_INT_STS2, 1, &reg);
55         irq_sts |= reg << 8;
56         switch (tps65910_chip_id(tps65910)) {
57         case TPS65911:
58                 tps65910->read(tps65910, TPS65910_INT_STS3, 1, &reg);
59                 irq_sts |= reg << 16;
60         }
61
62         tps65910->read(tps65910, TPS65910_INT_MSK, 1, &reg);
63         irq_mask = reg;
64         tps65910->read(tps65910, TPS65910_INT_MSK2, 1, &reg);
65         irq_mask |= reg << 8;
66         switch (tps65910_chip_id(tps65910)) {
67         case TPS65911:
68                 tps65910->read(tps65910, TPS65910_INT_MSK3, 1, &reg);
69                 irq_mask |= reg << 16;
70         }
71
72         irq_sts &= ~irq_mask;
73
74         if (!irq_sts)
75         {
76                 wake_unlock(&tps65910->irq_wake);
77                 return IRQ_NONE;
78         }
79
80         for (i = 0; i < tps65910->irq_num; i++) {
81
82                 if (!(irq_sts & (1 << i)))
83                         continue;
84
85                 handle_nested_irq(tps65910->irq_base + i);
86         }
87
88         /* Write the STS register back to clear IRQs we handled */
89         reg = irq_sts & 0xFF;
90         irq_sts >>= 8;
91         tps65910->write(tps65910, TPS65910_INT_STS, 1, &reg);
92         reg = irq_sts & 0xFF;
93         tps65910->write(tps65910, TPS65910_INT_STS2, 1, &reg);
94         switch (tps65910_chip_id(tps65910)) {
95         case TPS65911:
96                 reg = irq_sts >> 8;
97                 tps65910->write(tps65910, TPS65910_INT_STS3, 1, &reg);
98         }
99         wake_unlock(&tps65910->irq_wake);
100         return IRQ_HANDLED;
101 }
102
103 static void tps65910_irq_lock(struct irq_data *data)
104 {
105         struct tps65910 *tps65910 = irq_data_get_irq_chip_data(data);
106
107         mutex_lock(&tps65910->irq_lock);
108 }
109
110 static void tps65910_irq_sync_unlock(struct irq_data *data)
111 {
112         struct tps65910 *tps65910 = irq_data_get_irq_chip_data(data);
113         u32 reg_mask;
114         u8 reg;
115
116         tps65910->read(tps65910, TPS65910_INT_MSK, 1, &reg);
117         reg_mask = reg;
118         tps65910->read(tps65910, TPS65910_INT_MSK2, 1, &reg);
119         reg_mask |= reg << 8;
120         switch (tps65910_chip_id(tps65910)) {
121         case TPS65911:
122                 tps65910->read(tps65910, TPS65910_INT_MSK3, 1, &reg);
123                 reg_mask |= reg << 16;
124         }
125
126         if (tps65910->irq_mask != reg_mask) {
127                 reg = tps65910->irq_mask & 0xFF;
128                 tps65910->write(tps65910, TPS65910_INT_MSK, 1, &reg);
129                 reg = tps65910->irq_mask >> 8 & 0xFF;
130                 tps65910->write(tps65910, TPS65910_INT_MSK2, 1, &reg);
131                 switch (tps65910_chip_id(tps65910)) {
132                 case TPS65911:
133                         reg = tps65910->irq_mask >> 16;
134                         tps65910->write(tps65910, TPS65910_INT_MSK3, 1, &reg);
135                 }
136         }
137         mutex_unlock(&tps65910->irq_lock);
138 }
139
140 static void tps65910_irq_enable(struct irq_data *data)
141 {
142         struct tps65910 *tps65910 = irq_data_get_irq_chip_data(data);
143
144         tps65910->irq_mask &= ~( 1 << irq_to_tps65910_irq(tps65910, data->irq));
145 }
146
147 static void tps65910_irq_disable(struct irq_data *data)
148 {
149         struct tps65910 *tps65910 = irq_data_get_irq_chip_data(data);
150
151         tps65910->irq_mask |= ( 1 << irq_to_tps65910_irq(tps65910, data->irq));
152 }
153
154 #ifdef CONFIG_PM_SLEEP
155 static int tps65910_irq_set_wake(struct irq_data *data, unsigned int enable)
156 {
157         struct tps65910 *tps65910 = irq_data_get_irq_chip_data(data);
158         return irq_set_irq_wake(tps65910->chip_irq, enable);
159 }
160 #else
161 #define tps65910_irq_set_wake NULL
162 #endif
163
164 static struct irq_chip tps65910_irq_chip = {
165         .name = "tps65910",
166         .irq_bus_lock = tps65910_irq_lock,
167         .irq_bus_sync_unlock = tps65910_irq_sync_unlock,
168         .irq_disable = tps65910_irq_disable,
169         .irq_enable = tps65910_irq_enable,
170         .irq_set_wake = tps65910_irq_set_wake,
171 };
172
173 int tps65910_irq_init(struct tps65910 *tps65910, int irq,
174                     struct tps65910_platform_data *pdata)
175 {
176         int ret, cur_irq;
177         int flags = IRQF_ONESHOT;
178         u8 reg;
179
180         if (!irq) {
181                 dev_warn(tps65910->dev, "No interrupt support, no core IRQ\n");
182                 return 0;
183         }
184
185         if (!pdata || !pdata->irq_base) {
186                 dev_warn(tps65910->dev, "No interrupt support, no IRQ base\n");
187                 return 0;
188         }
189
190         /* Clear unattended interrupts */
191         tps65910->read(tps65910, TPS65910_INT_STS, 1, &reg);
192         tps65910->write(tps65910, TPS65910_INT_STS, 1, &reg);
193         tps65910->read(tps65910, TPS65910_INT_STS2, 1, &reg);
194         tps65910->write(tps65910, TPS65910_INT_STS2, 1, &reg);
195         tps65910->read(tps65910, TPS65910_INT_STS3, 1, &reg);
196         tps65910->write(tps65910, TPS65910_INT_STS3, 1, &reg);
197         tps65910->read(tps65910, TPS65910_RTC_STATUS, 1, &reg); 
198         tps65910->write(tps65910, TPS65910_RTC_STATUS, 1, &reg);//clear alarm and timer interrupt
199
200         /* Mask top level interrupts */
201         tps65910->irq_mask = 0xFFFFFF;
202
203         mutex_init(&tps65910->irq_lock);        
204         wake_lock_init(&tps65910->irq_wake, WAKE_LOCK_SUSPEND, "tps65910_irq_wake");
205         tps65910->chip_irq = irq;
206         tps65910->irq_base = pdata->irq_base;
207         
208         switch (tps65910_chip_id(tps65910)) {
209         case TPS65910:
210                 tps65910->irq_num = TPS65910_NUM_IRQ;
211                 break;
212         case TPS65911:
213                 tps65910->irq_num = TPS65911_NUM_IRQ;
214                 break;
215         }
216
217         /* Register with genirq */
218         for (cur_irq = tps65910->irq_base;
219              cur_irq < tps65910->irq_num + tps65910->irq_base;
220              cur_irq++) {
221                 irq_set_chip_data(cur_irq, tps65910);
222                 irq_set_chip_and_handler(cur_irq, &tps65910_irq_chip,
223                                          handle_edge_irq);
224                 irq_set_nested_thread(cur_irq, 1);
225
226                 /* ARM needs us to explicitly flag the IRQ as valid
227                  * and will set them noprobe when we do so. */
228 #ifdef CONFIG_ARM
229                 set_irq_flags(cur_irq, IRQF_VALID);
230 #else
231                 irq_set_noprobe(cur_irq);
232 #endif
233         }
234
235         ret = request_threaded_irq(irq, NULL, tps65910_irq, flags,
236                                    "tps65910", tps65910);
237
238         irq_set_irq_type(irq, IRQ_TYPE_LEVEL_LOW);
239
240         if (ret != 0)
241                 dev_err(tps65910->dev, "Failed to request IRQ: %d\n", ret);
242
243         return ret;
244 }
245
246 int tps65910_irq_exit(struct tps65910 *tps65910)
247 {
248         if (tps65910->chip_irq)
249                 free_irq(tps65910->chip_irq, tps65910);
250         return 0;
251 }