blob: e96199dd14d086d9640f7c9863f730186c064a9a [file] [log] [blame]
Gregory Bean2783cc22010-09-10 15:03:36 -07001/* 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>
David Brown10eb5f62011-05-12 01:28:01 -070023#include <mach/cpu.h>
Gregory Bean2783cc22010-09-10 15:03:36 -070024#include "gpio_hw.h"
Gregory Bean26cc6662010-09-10 15:03:37 -070025#include "gpiomux.h"
Gregory Bean2783cc22010-09-10 15:03:36 -070026
27#define FIRST_GPIO_IRQ MSM_GPIO_TO_INT(0)
28
David Brown2e01d2c2011-05-12 01:16:46 -070029#define MSM_GPIO_BANK(soc, bank, first, last) \
Gregory Bean2783cc22010-09-10 15:03:36 -070030 { \
31 .regs = { \
David Brown2e01d2c2011-05-12 01:16:46 -070032 .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, \
Gregory Bean2783cc22010-09-10 15:03:36 -070040 }, \
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, \
Gregory Bean26cc6662010-09-10 15:03:37 -070049 .request = msm_gpio_request, \
50 .free = msm_gpio_free, \
Gregory Bean2783cc22010-09-10 15:03:36 -070051 } \
52 }
53
54#define MSM_GPIO_BROKEN_INT_CLEAR 1
55
56struct 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
67struct 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
78static 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
92static 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
111static 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
128static 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
140static int
141msm_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
154static 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
162static 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
173static int msm_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
174{
175 return MSM_GPIO_TO_INT(chip->base + offset);
176}
177
Gregory Bean26cc6662010-09-10 15:03:37 -0700178#ifdef CONFIG_MSM_GPIOMUX
179static int msm_gpio_request(struct gpio_chip *chip, unsigned offset)
180{
181 return msm_gpiomux_get(chip->base + offset);
182}
183
184static 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
David Brown10eb5f62011-05-12 01:28:01 -0700193static struct msm_gpio_chip *msm_gpio_chips;
194static int msm_gpio_count;
195
196static struct msm_gpio_chip msm_gpio_chips_msm7x01[] = {
David Brown2e01d2c2011-05-12 01:16:46 -0700197 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),
David Brown10eb5f62011-05-12 01:28:01 -0700203};
204
205static struct msm_gpio_chip msm_gpio_chips_msm7x30[] = {
David Brown2e01d2c2011-05-12 01:16:46 -0700206 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),
David Brown10eb5f62011-05-12 01:28:01 -0700214};
215
216static struct msm_gpio_chip msm_gpio_chips_qsd8x50[] = {
David Brown2e01d2c2011-05-12 01:16:46 -0700217 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),
Gregory Bean2783cc22010-09-10 15:03:36 -0700225};
226
Lennert Buytenhek0f86ee02010-11-29 10:37:34 +0100227static void msm_gpio_irq_ack(struct irq_data *d)
Gregory Bean2783cc22010-09-10 15:03:36 -0700228{
229 unsigned long irq_flags;
Lennert Buytenhek0f86ee02010-11-29 10:37:34 +0100230 struct msm_gpio_chip *msm_chip = irq_data_get_irq_chip_data(d);
Gregory Bean2783cc22010-09-10 15:03:36 -0700231 spin_lock_irqsave(&msm_chip->lock, irq_flags);
232 msm_gpio_clear_detect_status(msm_chip,
Lennert Buytenhek0f86ee02010-11-29 10:37:34 +0100233 d->irq - gpio_to_irq(msm_chip->chip.base));
Gregory Bean2783cc22010-09-10 15:03:36 -0700234 spin_unlock_irqrestore(&msm_chip->lock, irq_flags);
235}
236
Lennert Buytenhek0f86ee02010-11-29 10:37:34 +0100237static void msm_gpio_irq_mask(struct irq_data *d)
Gregory Bean2783cc22010-09-10 15:03:36 -0700238{
239 unsigned long irq_flags;
Lennert Buytenhek0f86ee02010-11-29 10:37:34 +0100240 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);
Gregory Bean2783cc22010-09-10 15:03:36 -0700242
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
Lennert Buytenhek0f86ee02010-11-29 10:37:34 +0100252static void msm_gpio_irq_unmask(struct irq_data *d)
Gregory Bean2783cc22010-09-10 15:03:36 -0700253{
254 unsigned long irq_flags;
Lennert Buytenhek0f86ee02010-11-29 10:37:34 +0100255 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);
Gregory Bean2783cc22010-09-10 15:03:36 -0700257
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
Lennert Buytenhek0f86ee02010-11-29 10:37:34 +0100267static int msm_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
Gregory Bean2783cc22010-09-10 15:03:36 -0700268{
269 unsigned long irq_flags;
Lennert Buytenhek0f86ee02010-11-29 10:37:34 +0100270 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);
Gregory Bean2783cc22010-09-10 15:03:36 -0700272
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
Lennert Buytenhek0f86ee02010-11-29 10:37:34 +0100284static int msm_gpio_irq_set_type(struct irq_data *d, unsigned int flow_type)
Gregory Bean2783cc22010-09-10 15:03:36 -0700285{
286 unsigned long irq_flags;
Lennert Buytenhek0f86ee02010-11-29 10:37:34 +0100287 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);
Gregory Bean2783cc22010-09-10 15:03:36 -0700289 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);
Thomas Gleixner70c4fa22011-03-24 12:41:27 +0100295 __irq_set_handler_locked(d->irq, handle_edge_irq);
Gregory Bean2783cc22010-09-10 15:03:36 -0700296 } else {
297 writel(val & ~mask, msm_chip->regs.int_edge);
Thomas Gleixner70c4fa22011-03-24 12:41:27 +0100298 __irq_set_handler_locked(d->irq, handle_level_irq);
Gregory Bean2783cc22010-09-10 15:03:36 -0700299 }
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
315static void msm_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
316{
317 int i, j, mask;
318 unsigned val;
319
David Brown10eb5f62011-05-12 01:28:01 -0700320 for (i = 0; i < msm_gpio_count; i++) {
Gregory Bean2783cc22010-09-10 15:03:36 -0700321 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 }
Lennert Buytenhek0f86ee02010-11-29 10:37:34 +0100335 desc->irq_data.chip->irq_ack(&desc->irq_data);
Gregory Bean2783cc22010-09-10 15:03:36 -0700336}
337
338static struct irq_chip msm_gpio_irq_chip = {
Lennert Buytenhek0f86ee02010-11-29 10:37:34 +0100339 .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,
Gregory Bean2783cc22010-09-10 15:03:36 -0700345};
346
347static int __init msm_init_gpio(void)
348{
349 int i, j = 0;
350
David Brown10eb5f62011-05-12 01:28:01 -0700351 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
Gregory Bean2783cc22010-09-10 15:03:36 -0700364 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++;
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100369 irq_set_chip_data(i, &msm_gpio_chips[j]);
Thomas Gleixnerf38c02f2011-03-24 13:35:09 +0100370 irq_set_chip_and_handler(i, &msm_gpio_irq_chip,
371 handle_edge_irq);
Gregory Bean2783cc22010-09-10 15:03:36 -0700372 set_irq_flags(i, IRQF_VALID);
373 }
374
David Brown10eb5f62011-05-12 01:28:01 -0700375 for (i = 0; i < msm_gpio_count; i++) {
Gregory Bean2783cc22010-09-10 15:03:36 -0700376 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
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100381 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);
Gregory Bean2783cc22010-09-10 15:03:36 -0700385 return 0;
386}
387
388postcore_initcall(msm_init_gpio);