1 /* linux/arch/arm/mach-msm/gpio.c
3 * Copyright (C) 2007 Google, Inc.
4 * Copyright (c) 2009-2010, Code Aurora Forum. All rights reserved.
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.
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.
17 #include <linux/bitops.h>
18 #include <linux/gpio.h>
19 #include <linux/interrupt.h>
21 #include <linux/irq.h>
22 #include <linux/module.h>
24 #include <mach/msm_gpiomux.h>
27 #define FIRST_GPIO_IRQ MSM_GPIO_TO_INT(0)
29 #define MSM_GPIO_BANK(soc, bank, first, last) \
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, \
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, \
54 #define MSM_GPIO_BROKEN_INT_CLEAR 1
56 struct msm_gpio_regs {
59 void __iomem *int_status;
60 void __iomem *int_clear;
62 void __iomem *int_edge;
63 void __iomem *int_pos;
67 struct msm_gpio_chip {
69 struct gpio_chip chip;
70 struct msm_gpio_regs regs;
71 #if MSM_GPIO_BROKEN_INT_CLEAR
72 unsigned int_status_copy;
74 unsigned int both_edge_detect;
75 unsigned int int_enable[2]; /* 0: awake, 1: sleep */
78 static int msm_gpio_write(struct msm_gpio_chip *msm_chip,
79 unsigned offset, unsigned on)
81 unsigned mask = BIT(offset);
84 val = readl(msm_chip->regs.out);
86 writel(val | mask, msm_chip->regs.out);
88 writel(val & ~mask, msm_chip->regs.out);
92 static void msm_gpio_update_both_edge_detect(struct msm_gpio_chip *msm_chip)
95 unsigned pol, val, val2, intstat;
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)
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);
111 static int msm_gpio_clear_detect_status(struct msm_gpio_chip *msm_chip,
114 unsigned bit = BIT(offset);
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;
123 writel(bit, msm_chip->regs.int_clear);
124 msm_gpio_update_both_edge_detect(msm_chip);
128 static int msm_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
130 struct msm_gpio_chip *msm_chip;
131 unsigned long irq_flags;
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);
141 msm_gpio_direction_output(struct gpio_chip *chip, unsigned offset, int value)
143 struct msm_gpio_chip *msm_chip;
144 unsigned long irq_flags;
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);
154 static int msm_gpio_get(struct gpio_chip *chip, unsigned offset)
156 struct msm_gpio_chip *msm_chip;
158 msm_chip = container_of(chip, struct msm_gpio_chip, chip);
159 return (readl(msm_chip->regs.in) & (1U << offset)) ? 1 : 0;
162 static void msm_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
164 struct msm_gpio_chip *msm_chip;
165 unsigned long irq_flags;
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);
173 static int msm_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
175 return MSM_GPIO_TO_INT(chip->base + offset);
178 #ifdef CONFIG_MSM_GPIOMUX
179 static int msm_gpio_request(struct gpio_chip *chip, unsigned offset)
181 return msm_gpiomux_get(chip->base + offset);
184 static void msm_gpio_free(struct gpio_chip *chip, unsigned offset)
186 msm_gpiomux_put(chip->base + offset);
189 #define msm_gpio_request NULL
190 #define msm_gpio_free NULL
193 static struct msm_gpio_chip *msm_gpio_chips;
194 static int msm_gpio_count;
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),
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),
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),
227 static void msm_gpio_irq_ack(struct irq_data *d)
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);
237 static void msm_gpio_irq_mask(struct irq_data *d)
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);
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);
252 static void msm_gpio_irq_unmask(struct irq_data *d)
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);
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);
267 static int msm_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
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);
273 spin_lock_irqsave(&msm_chip->lock, irq_flags);
276 msm_chip->int_enable[1] |= BIT(offset);
278 msm_chip->int_enable[1] &= ~BIT(offset);
280 spin_unlock_irqrestore(&msm_chip->lock, irq_flags);
284 static int msm_gpio_irq_set_type(struct irq_data *d, unsigned int flow_type)
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);
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);
297 writel(val & ~mask, msm_chip->regs.int_edge);
298 __irq_set_handler_locked(d->irq, handle_level_irq);
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);
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);
309 writel(val & ~mask, msm_chip->regs.int_pos);
311 spin_unlock_irqrestore(&msm_chip->lock, irq_flags);
315 static void msm_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
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];
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); */
331 generic_handle_irq(FIRST_GPIO_IRQ +
332 msm_chip->chip.base + j);
335 desc->irq_data.chip->irq_ack(&desc->irq_data);
338 static struct irq_chip msm_gpio_irq_chip = {
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,
347 static int __init msm_init_gpio(void)
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);
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)
369 irq_set_chip_data(i, &msm_gpio_chips[j]);
370 irq_set_chip_and_handler(i, &msm_gpio_irq_chip,
372 set_irq_flags(i, IRQF_VALID);
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);
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);
388 postcore_initcall(msm_init_gpio);