msm: gpiomux: Move public API to public header
[firefly-linux-kernel-4.4.55.git] / arch / arm / mach-msm / gpio.c
1 /* linux/arch/arm/mach-msm/gpio.c
2  *
3  * Copyright (C) 2007 Google, Inc.
4  * Copyright (c) 2009-2010, Code Aurora Forum. All rights reserved.
5  *
6  * This software is licensed under the terms of the GNU General Public
7  * License version 2, as published by the Free Software Foundation, and
8  * may be copied, distributed, and modified under those terms.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  */
16
17 #include <linux/bitops.h>
18 #include <linux/gpio.h>
19 #include <linux/interrupt.h>
20 #include <linux/io.h>
21 #include <linux/irq.h>
22 #include <linux/module.h>
23 #include <mach/cpu.h>
24 #include <mach/msm_gpiomux.h>
25 #include "gpio_hw.h"
26
27 #define FIRST_GPIO_IRQ MSM_GPIO_TO_INT(0)
28
29 #define MSM_GPIO_BANK(soc, bank, first, last)                           \
30         {                                                               \
31                 .regs = {                                               \
32                         .out =         soc##_GPIO_OUT_##bank,           \
33                         .in =          soc##_GPIO_IN_##bank,            \
34                         .int_status =  soc##_GPIO_INT_STATUS_##bank,    \
35                         .int_clear =   soc##_GPIO_INT_CLEAR_##bank,     \
36                         .int_en =      soc##_GPIO_INT_EN_##bank,        \
37                         .int_edge =    soc##_GPIO_INT_EDGE_##bank,      \
38                         .int_pos =     soc##_GPIO_INT_POS_##bank,       \
39                         .oe =          soc##_GPIO_OE_##bank,            \
40                 },                                                      \
41                 .chip = {                                               \
42                         .base = (first),                                \
43                         .ngpio = (last) - (first) + 1,                  \
44                         .get = msm_gpio_get,                            \
45                         .set = msm_gpio_set,                            \
46                         .direction_input = msm_gpio_direction_input,    \
47                         .direction_output = msm_gpio_direction_output,  \
48                         .to_irq = msm_gpio_to_irq,                      \
49                         .request = msm_gpio_request,                    \
50                         .free = msm_gpio_free,                          \
51                 }                                                       \
52         }
53
54 #define MSM_GPIO_BROKEN_INT_CLEAR 1
55
56 struct msm_gpio_regs {
57         void __iomem *out;
58         void __iomem *in;
59         void __iomem *int_status;
60         void __iomem *int_clear;
61         void __iomem *int_en;
62         void __iomem *int_edge;
63         void __iomem *int_pos;
64         void __iomem *oe;
65 };
66
67 struct msm_gpio_chip {
68         spinlock_t              lock;
69         struct gpio_chip        chip;
70         struct msm_gpio_regs    regs;
71 #if MSM_GPIO_BROKEN_INT_CLEAR
72         unsigned                int_status_copy;
73 #endif
74         unsigned int            both_edge_detect;
75         unsigned int            int_enable[2]; /* 0: awake, 1: sleep */
76 };
77
78 static int msm_gpio_write(struct msm_gpio_chip *msm_chip,
79                           unsigned offset, unsigned on)
80 {
81         unsigned mask = BIT(offset);
82         unsigned val;
83
84         val = readl(msm_chip->regs.out);
85         if (on)
86                 writel(val | mask, msm_chip->regs.out);
87         else
88                 writel(val & ~mask, msm_chip->regs.out);
89         return 0;
90 }
91
92 static void msm_gpio_update_both_edge_detect(struct msm_gpio_chip *msm_chip)
93 {
94         int loop_limit = 100;
95         unsigned pol, val, val2, intstat;
96         do {
97                 val = readl(msm_chip->regs.in);
98                 pol = readl(msm_chip->regs.int_pos);
99                 pol = (pol & ~msm_chip->both_edge_detect) |
100                       (~val & msm_chip->both_edge_detect);
101                 writel(pol, msm_chip->regs.int_pos);
102                 intstat = readl(msm_chip->regs.int_status);
103                 val2 = readl(msm_chip->regs.in);
104                 if (((val ^ val2) & msm_chip->both_edge_detect & ~intstat) == 0)
105                         return;
106         } while (loop_limit-- > 0);
107         printk(KERN_ERR "msm_gpio_update_both_edge_detect, "
108                "failed to reach stable state %x != %x\n", val, val2);
109 }
110
111 static int msm_gpio_clear_detect_status(struct msm_gpio_chip *msm_chip,
112                                         unsigned offset)
113 {
114         unsigned bit = BIT(offset);
115
116 #if MSM_GPIO_BROKEN_INT_CLEAR
117         /* Save interrupts that already triggered before we loose them. */
118         /* Any interrupt that triggers between the read of int_status */
119         /* and the write to int_clear will still be lost though. */
120         msm_chip->int_status_copy |= readl(msm_chip->regs.int_status);
121         msm_chip->int_status_copy &= ~bit;
122 #endif
123         writel(bit, msm_chip->regs.int_clear);
124         msm_gpio_update_both_edge_detect(msm_chip);
125         return 0;
126 }
127
128 static int msm_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
129 {
130         struct msm_gpio_chip *msm_chip;
131         unsigned long irq_flags;
132
133         msm_chip = container_of(chip, struct msm_gpio_chip, chip);
134         spin_lock_irqsave(&msm_chip->lock, irq_flags);
135         writel(readl(msm_chip->regs.oe) & ~BIT(offset), msm_chip->regs.oe);
136         spin_unlock_irqrestore(&msm_chip->lock, irq_flags);
137         return 0;
138 }
139
140 static int
141 msm_gpio_direction_output(struct gpio_chip *chip, unsigned offset, int value)
142 {
143         struct msm_gpio_chip *msm_chip;
144         unsigned long irq_flags;
145
146         msm_chip = container_of(chip, struct msm_gpio_chip, chip);
147         spin_lock_irqsave(&msm_chip->lock, irq_flags);
148         msm_gpio_write(msm_chip, offset, value);
149         writel(readl(msm_chip->regs.oe) | BIT(offset), msm_chip->regs.oe);
150         spin_unlock_irqrestore(&msm_chip->lock, irq_flags);
151         return 0;
152 }
153
154 static int msm_gpio_get(struct gpio_chip *chip, unsigned offset)
155 {
156         struct msm_gpio_chip *msm_chip;
157
158         msm_chip = container_of(chip, struct msm_gpio_chip, chip);
159         return (readl(msm_chip->regs.in) & (1U << offset)) ? 1 : 0;
160 }
161
162 static void msm_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
163 {
164         struct msm_gpio_chip *msm_chip;
165         unsigned long irq_flags;
166
167         msm_chip = container_of(chip, struct msm_gpio_chip, chip);
168         spin_lock_irqsave(&msm_chip->lock, irq_flags);
169         msm_gpio_write(msm_chip, offset, value);
170         spin_unlock_irqrestore(&msm_chip->lock, irq_flags);
171 }
172
173 static int msm_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
174 {
175         return MSM_GPIO_TO_INT(chip->base + offset);
176 }
177
178 #ifdef CONFIG_MSM_GPIOMUX
179 static int msm_gpio_request(struct gpio_chip *chip, unsigned offset)
180 {
181         return msm_gpiomux_get(chip->base + offset);
182 }
183
184 static void msm_gpio_free(struct gpio_chip *chip, unsigned offset)
185 {
186         msm_gpiomux_put(chip->base + offset);
187 }
188 #else
189 #define msm_gpio_request NULL
190 #define msm_gpio_free NULL
191 #endif
192
193 static struct msm_gpio_chip *msm_gpio_chips;
194 static int msm_gpio_count;
195
196 static struct msm_gpio_chip msm_gpio_chips_msm7x01[] = {
197         MSM_GPIO_BANK(MSM7X00, 0,   0,  15),
198         MSM_GPIO_BANK(MSM7X00, 1,  16,  42),
199         MSM_GPIO_BANK(MSM7X00, 2,  43,  67),
200         MSM_GPIO_BANK(MSM7X00, 3,  68,  94),
201         MSM_GPIO_BANK(MSM7X00, 4,  95, 106),
202         MSM_GPIO_BANK(MSM7X00, 5, 107, 121),
203 };
204
205 static struct msm_gpio_chip msm_gpio_chips_msm7x30[] = {
206         MSM_GPIO_BANK(MSM7X30, 0,   0,  15),
207         MSM_GPIO_BANK(MSM7X30, 1,  16,  43),
208         MSM_GPIO_BANK(MSM7X30, 2,  44,  67),
209         MSM_GPIO_BANK(MSM7X30, 3,  68,  94),
210         MSM_GPIO_BANK(MSM7X30, 4,  95, 106),
211         MSM_GPIO_BANK(MSM7X30, 5, 107, 133),
212         MSM_GPIO_BANK(MSM7X30, 6, 134, 150),
213         MSM_GPIO_BANK(MSM7X30, 7, 151, 181),
214 };
215
216 static struct msm_gpio_chip msm_gpio_chips_qsd8x50[] = {
217         MSM_GPIO_BANK(QSD8X50, 0,   0,  15),
218         MSM_GPIO_BANK(QSD8X50, 1,  16,  42),
219         MSM_GPIO_BANK(QSD8X50, 2,  43,  67),
220         MSM_GPIO_BANK(QSD8X50, 3,  68,  94),
221         MSM_GPIO_BANK(QSD8X50, 4,  95, 103),
222         MSM_GPIO_BANK(QSD8X50, 5, 104, 121),
223         MSM_GPIO_BANK(QSD8X50, 6, 122, 152),
224         MSM_GPIO_BANK(QSD8X50, 7, 153, 164),
225 };
226
227 static void msm_gpio_irq_ack(struct irq_data *d)
228 {
229         unsigned long irq_flags;
230         struct msm_gpio_chip *msm_chip = irq_data_get_irq_chip_data(d);
231         spin_lock_irqsave(&msm_chip->lock, irq_flags);
232         msm_gpio_clear_detect_status(msm_chip,
233                                      d->irq - gpio_to_irq(msm_chip->chip.base));
234         spin_unlock_irqrestore(&msm_chip->lock, irq_flags);
235 }
236
237 static void msm_gpio_irq_mask(struct irq_data *d)
238 {
239         unsigned long irq_flags;
240         struct msm_gpio_chip *msm_chip = irq_data_get_irq_chip_data(d);
241         unsigned offset = d->irq - gpio_to_irq(msm_chip->chip.base);
242
243         spin_lock_irqsave(&msm_chip->lock, irq_flags);
244         /* level triggered interrupts are also latched */
245         if (!(readl(msm_chip->regs.int_edge) & BIT(offset)))
246                 msm_gpio_clear_detect_status(msm_chip, offset);
247         msm_chip->int_enable[0] &= ~BIT(offset);
248         writel(msm_chip->int_enable[0], msm_chip->regs.int_en);
249         spin_unlock_irqrestore(&msm_chip->lock, irq_flags);
250 }
251
252 static void msm_gpio_irq_unmask(struct irq_data *d)
253 {
254         unsigned long irq_flags;
255         struct msm_gpio_chip *msm_chip = irq_data_get_irq_chip_data(d);
256         unsigned offset = d->irq - gpio_to_irq(msm_chip->chip.base);
257
258         spin_lock_irqsave(&msm_chip->lock, irq_flags);
259         /* level triggered interrupts are also latched */
260         if (!(readl(msm_chip->regs.int_edge) & BIT(offset)))
261                 msm_gpio_clear_detect_status(msm_chip, offset);
262         msm_chip->int_enable[0] |= BIT(offset);
263         writel(msm_chip->int_enable[0], msm_chip->regs.int_en);
264         spin_unlock_irqrestore(&msm_chip->lock, irq_flags);
265 }
266
267 static int msm_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
268 {
269         unsigned long irq_flags;
270         struct msm_gpio_chip *msm_chip = irq_data_get_irq_chip_data(d);
271         unsigned offset = d->irq - gpio_to_irq(msm_chip->chip.base);
272
273         spin_lock_irqsave(&msm_chip->lock, irq_flags);
274
275         if (on)
276                 msm_chip->int_enable[1] |= BIT(offset);
277         else
278                 msm_chip->int_enable[1] &= ~BIT(offset);
279
280         spin_unlock_irqrestore(&msm_chip->lock, irq_flags);
281         return 0;
282 }
283
284 static int msm_gpio_irq_set_type(struct irq_data *d, unsigned int flow_type)
285 {
286         unsigned long irq_flags;
287         struct msm_gpio_chip *msm_chip = irq_data_get_irq_chip_data(d);
288         unsigned offset = d->irq - gpio_to_irq(msm_chip->chip.base);
289         unsigned val, mask = BIT(offset);
290
291         spin_lock_irqsave(&msm_chip->lock, irq_flags);
292         val = readl(msm_chip->regs.int_edge);
293         if (flow_type & IRQ_TYPE_EDGE_BOTH) {
294                 writel(val | mask, msm_chip->regs.int_edge);
295                 __irq_set_handler_locked(d->irq, handle_edge_irq);
296         } else {
297                 writel(val & ~mask, msm_chip->regs.int_edge);
298                 __irq_set_handler_locked(d->irq, handle_level_irq);
299         }
300         if ((flow_type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
301                 msm_chip->both_edge_detect |= mask;
302                 msm_gpio_update_both_edge_detect(msm_chip);
303         } else {
304                 msm_chip->both_edge_detect &= ~mask;
305                 val = readl(msm_chip->regs.int_pos);
306                 if (flow_type & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_HIGH))
307                         writel(val | mask, msm_chip->regs.int_pos);
308                 else
309                         writel(val & ~mask, msm_chip->regs.int_pos);
310         }
311         spin_unlock_irqrestore(&msm_chip->lock, irq_flags);
312         return 0;
313 }
314
315 static void msm_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
316 {
317         int i, j, mask;
318         unsigned val;
319
320         for (i = 0; i < msm_gpio_count; i++) {
321                 struct msm_gpio_chip *msm_chip = &msm_gpio_chips[i];
322                 val = readl(msm_chip->regs.int_status);
323                 val &= msm_chip->int_enable[0];
324                 while (val) {
325                         mask = val & -val;
326                         j = fls(mask) - 1;
327                         /* printk("%s %08x %08x bit %d gpio %d irq %d\n",
328                                 __func__, v, m, j, msm_chip->chip.start + j,
329                                 FIRST_GPIO_IRQ + msm_chip->chip.start + j); */
330                         val &= ~mask;
331                         generic_handle_irq(FIRST_GPIO_IRQ +
332                                            msm_chip->chip.base + j);
333                 }
334         }
335         desc->irq_data.chip->irq_ack(&desc->irq_data);
336 }
337
338 static struct irq_chip msm_gpio_irq_chip = {
339         .name          = "msmgpio",
340         .irq_ack       = msm_gpio_irq_ack,
341         .irq_mask      = msm_gpio_irq_mask,
342         .irq_unmask    = msm_gpio_irq_unmask,
343         .irq_set_wake  = msm_gpio_irq_set_wake,
344         .irq_set_type  = msm_gpio_irq_set_type,
345 };
346
347 static int __init msm_init_gpio(void)
348 {
349         int i, j = 0;
350
351         if (cpu_is_msm7x01()) {
352                 msm_gpio_chips = msm_gpio_chips_msm7x01;
353                 msm_gpio_count = ARRAY_SIZE(msm_gpio_chips_msm7x01);
354         } else if (cpu_is_msm7x30()) {
355                 msm_gpio_chips = msm_gpio_chips_msm7x30;
356                 msm_gpio_count = ARRAY_SIZE(msm_gpio_chips_msm7x30);
357         } else if (cpu_is_qsd8x50()) {
358                 msm_gpio_chips = msm_gpio_chips_qsd8x50;
359                 msm_gpio_count = ARRAY_SIZE(msm_gpio_chips_qsd8x50);
360         } else {
361                 return 0;
362         }
363
364         for (i = FIRST_GPIO_IRQ; i < FIRST_GPIO_IRQ + NR_GPIO_IRQS; i++) {
365                 if (i - FIRST_GPIO_IRQ >=
366                         msm_gpio_chips[j].chip.base +
367                         msm_gpio_chips[j].chip.ngpio)
368                         j++;
369                 irq_set_chip_data(i, &msm_gpio_chips[j]);
370                 irq_set_chip_and_handler(i, &msm_gpio_irq_chip,
371                                          handle_edge_irq);
372                 set_irq_flags(i, IRQF_VALID);
373         }
374
375         for (i = 0; i < msm_gpio_count; i++) {
376                 spin_lock_init(&msm_gpio_chips[i].lock);
377                 writel(0, msm_gpio_chips[i].regs.int_en);
378                 gpiochip_add(&msm_gpio_chips[i].chip);
379         }
380
381         irq_set_chained_handler(INT_GPIO_GROUP1, msm_gpio_irq_handler);
382         irq_set_chained_handler(INT_GPIO_GROUP2, msm_gpio_irq_handler);
383         irq_set_irq_wake(INT_GPIO_GROUP1, 1);
384         irq_set_irq_wake(INT_GPIO_GROUP2, 2);
385         return 0;
386 }
387
388 postcore_initcall(msm_init_gpio);