aboutsummaryrefslogtreecommitdiffstats
path: root/arch/arm/mach-msm/gpio-v2.c
diff options
context:
space:
mode:
Diffstat (limited to 'arch/arm/mach-msm/gpio-v2.c')
-rw-r--r--arch/arm/mach-msm/gpio-v2.c309
1 files changed, 286 insertions, 23 deletions
diff --git a/arch/arm/mach-msm/gpio-v2.c b/arch/arm/mach-msm/gpio-v2.c
index d907af68c8ee..0de19ec74e34 100644
--- a/arch/arm/mach-msm/gpio-v2.c
+++ b/arch/arm/mach-msm/gpio-v2.c
@@ -15,7 +15,13 @@
15 * 02110-1301, USA. 15 * 02110-1301, USA.
16 * 16 *
17 */ 17 */
18#define pr_fmt(fmt) "%s: " fmt, __func__
19
20#include <linux/bitmap.h>
21#include <linux/bitops.h>
18#include <linux/gpio.h> 22#include <linux/gpio.h>
23#include <linux/init.h>
24#include <linux/interrupt.h>
19#include <linux/io.h> 25#include <linux/io.h>
20#include <linux/irq.h> 26#include <linux/irq.h>
21#include <linux/module.h> 27#include <linux/module.h>
@@ -27,29 +33,103 @@
27/* Bits of interest in the GPIO_IN_OUT register. 33/* Bits of interest in the GPIO_IN_OUT register.
28 */ 34 */
29enum { 35enum {
30 GPIO_IN_BIT = 0, 36 GPIO_IN = 0,
31 GPIO_OUT_BIT = 1 37 GPIO_OUT = 1
38};
39
40/* Bits of interest in the GPIO_INTR_STATUS register.
41 */
42enum {
43 INTR_STATUS = 0,
32}; 44};
33 45
34/* Bits of interest in the GPIO_CFG register. 46/* Bits of interest in the GPIO_CFG register.
35 */ 47 */
36enum { 48enum {
37 GPIO_OE_BIT = 9, 49 GPIO_OE = 9,
50};
51
52/* Bits of interest in the GPIO_INTR_CFG register.
53 * When a GPIO triggers, two separate decisions are made, controlled
54 * by two separate flags.
55 *
56 * - First, INTR_RAW_STATUS_EN controls whether or not the GPIO_INTR_STATUS
57 * register for that GPIO will be updated to reflect the triggering of that
58 * gpio. If this bit is 0, this register will not be updated.
59 * - Second, INTR_ENABLE controls whether an interrupt is triggered.
60 *
61 * If INTR_ENABLE is set and INTR_RAW_STATUS_EN is NOT set, an interrupt
62 * can be triggered but the status register will not reflect it.
63 */
64enum {
65 INTR_ENABLE = 0,
66 INTR_POL_CTL = 1,
67 INTR_DECT_CTL = 2,
68 INTR_RAW_STATUS_EN = 3,
69};
70
71/* Codes of interest in GPIO_INTR_CFG_SU.
72 */
73enum {
74 TARGET_PROC_SCORPION = 4,
75 TARGET_PROC_NONE = 7,
38}; 76};
39 77
78
79#define GPIO_INTR_CFG_SU(gpio) (MSM_TLMM_BASE + 0x0400 + (0x04 * (gpio)))
40#define GPIO_CONFIG(gpio) (MSM_TLMM_BASE + 0x1000 + (0x10 * (gpio))) 80#define GPIO_CONFIG(gpio) (MSM_TLMM_BASE + 0x1000 + (0x10 * (gpio)))
41#define GPIO_IN_OUT(gpio) (MSM_TLMM_BASE + 0x1004 + (0x10 * (gpio))) 81#define GPIO_IN_OUT(gpio) (MSM_TLMM_BASE + 0x1004 + (0x10 * (gpio)))
82#define GPIO_INTR_CFG(gpio) (MSM_TLMM_BASE + 0x1008 + (0x10 * (gpio)))
83#define GPIO_INTR_STATUS(gpio) (MSM_TLMM_BASE + 0x100c + (0x10 * (gpio)))
84
85/**
86 * struct msm_gpio_dev: the MSM8660 SoC GPIO device structure
87 *
88 * @enabled_irqs: a bitmap used to optimize the summary-irq handler. By
89 * keeping track of which gpios are unmasked as irq sources, we avoid
90 * having to do readl calls on hundreds of iomapped registers each time
91 * the summary interrupt fires in order to locate the active interrupts.
92 *
93 * @wake_irqs: a bitmap for tracking which interrupt lines are enabled
94 * as wakeup sources. When the device is suspended, interrupts which are
95 * not wakeup sources are disabled.
96 *
97 * @dual_edge_irqs: a bitmap used to track which irqs are configured
98 * as dual-edge, as this is not supported by the hardware and requires
99 * some special handling in the driver.
100 */
101struct msm_gpio_dev {
102 struct gpio_chip gpio_chip;
103 DECLARE_BITMAP(enabled_irqs, NR_GPIO_IRQS);
104 DECLARE_BITMAP(wake_irqs, NR_GPIO_IRQS);
105 DECLARE_BITMAP(dual_edge_irqs, NR_GPIO_IRQS);
106};
42 107
43static DEFINE_SPINLOCK(tlmm_lock); 108static DEFINE_SPINLOCK(tlmm_lock);
44 109
110static inline struct msm_gpio_dev *to_msm_gpio_dev(struct gpio_chip *chip)
111{
112 return container_of(chip, struct msm_gpio_dev, gpio_chip);
113}
114
115static inline void set_gpio_bits(unsigned n, void __iomem *reg)
116{
117 writel(readl(reg) | n, reg);
118}
119
120static inline void clear_gpio_bits(unsigned n, void __iomem *reg)
121{
122 writel(readl(reg) & ~n, reg);
123}
124
45static int msm_gpio_get(struct gpio_chip *chip, unsigned offset) 125static int msm_gpio_get(struct gpio_chip *chip, unsigned offset)
46{ 126{
47 return readl(GPIO_IN_OUT(offset)) & BIT(GPIO_IN_BIT); 127 return readl(GPIO_IN_OUT(offset)) & BIT(GPIO_IN);
48} 128}
49 129
50static void msm_gpio_set(struct gpio_chip *chip, unsigned offset, int val) 130static void msm_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
51{ 131{
52 writel(val ? BIT(GPIO_OUT_BIT) : 0, GPIO_IN_OUT(offset)); 132 writel(val ? BIT(GPIO_OUT) : 0, GPIO_IN_OUT(offset));
53} 133}
54 134
55static int msm_gpio_direction_input(struct gpio_chip *chip, unsigned offset) 135static int msm_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
@@ -57,8 +137,7 @@ static int msm_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
57 unsigned long irq_flags; 137 unsigned long irq_flags;
58 138
59 spin_lock_irqsave(&tlmm_lock, irq_flags); 139 spin_lock_irqsave(&tlmm_lock, irq_flags);
60 writel(readl(GPIO_CONFIG(offset)) & ~BIT(GPIO_OE_BIT), 140 clear_gpio_bits(BIT(GPIO_OE), GPIO_CONFIG(offset));
61 GPIO_CONFIG(offset));
62 spin_unlock_irqrestore(&tlmm_lock, irq_flags); 141 spin_unlock_irqrestore(&tlmm_lock, irq_flags);
63 return 0; 142 return 0;
64} 143}
@@ -71,8 +150,7 @@ static int msm_gpio_direction_output(struct gpio_chip *chip,
71 150
72 spin_lock_irqsave(&tlmm_lock, irq_flags); 151 spin_lock_irqsave(&tlmm_lock, irq_flags);
73 msm_gpio_set(chip, offset, val); 152 msm_gpio_set(chip, offset, val);
74 writel(readl(GPIO_CONFIG(offset)) | BIT(GPIO_OE_BIT), 153 set_gpio_bits(BIT(GPIO_OE), GPIO_CONFIG(offset));
75 GPIO_CONFIG(offset));
76 spin_unlock_irqrestore(&tlmm_lock, irq_flags); 154 spin_unlock_irqrestore(&tlmm_lock, irq_flags);
77 return 0; 155 return 0;
78} 156}
@@ -87,30 +165,215 @@ static void msm_gpio_free(struct gpio_chip *chip, unsigned offset)
87 msm_gpiomux_put(chip->base + offset); 165 msm_gpiomux_put(chip->base + offset);
88} 166}
89 167
90static struct gpio_chip msm_gpio = { 168static int msm_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
91 .base = 0, 169{
92 .ngpio = NR_GPIO_IRQS, 170 return MSM_GPIO_TO_INT(chip->base + offset);
93 .direction_input = msm_gpio_direction_input, 171}
94 .direction_output = msm_gpio_direction_output, 172
95 .get = msm_gpio_get, 173static inline int msm_irq_to_gpio(struct gpio_chip *chip, unsigned irq)
96 .set = msm_gpio_set, 174{
97 .request = msm_gpio_request, 175 return irq - MSM_GPIO_TO_INT(chip->base);
98 .free = msm_gpio_free, 176}
177
178static struct msm_gpio_dev msm_gpio = {
179 .gpio_chip = {
180 .base = 0,
181 .ngpio = NR_GPIO_IRQS,
182 .direction_input = msm_gpio_direction_input,
183 .direction_output = msm_gpio_direction_output,
184 .get = msm_gpio_get,
185 .set = msm_gpio_set,
186 .to_irq = msm_gpio_to_irq,
187 .request = msm_gpio_request,
188 .free = msm_gpio_free,
189 },
190};
191
192/* For dual-edge interrupts in software, since the hardware has no
193 * such support:
194 *
195 * At appropriate moments, this function may be called to flip the polarity
196 * settings of both-edge irq lines to try and catch the next edge.
197 *
198 * The attempt is considered successful if:
199 * - the status bit goes high, indicating that an edge was caught, or
200 * - the input value of the gpio doesn't change during the attempt.
201 * If the value changes twice during the process, that would cause the first
202 * test to fail but would force the second, as two opposite
203 * transitions would cause a detection no matter the polarity setting.
204 *
205 * The do-loop tries to sledge-hammer closed the timing hole between
206 * the initial value-read and the polarity-write - if the line value changes
207 * during that window, an interrupt is lost, the new polarity setting is
208 * incorrect, and the first success test will fail, causing a retry.
209 *
210 * Algorithm comes from Google's msmgpio driver, see mach-msm/gpio.c.
211 */
212static void msm_gpio_update_dual_edge_pos(unsigned gpio)
213{
214 int loop_limit = 100;
215 unsigned val, val2, intstat;
216
217 do {
218 val = readl(GPIO_IN_OUT(gpio)) & BIT(GPIO_IN);
219 if (val)
220 clear_gpio_bits(BIT(INTR_POL_CTL), GPIO_INTR_CFG(gpio));
221 else
222 set_gpio_bits(BIT(INTR_POL_CTL), GPIO_INTR_CFG(gpio));
223 val2 = readl(GPIO_IN_OUT(gpio)) & BIT(GPIO_IN);
224 intstat = readl(GPIO_INTR_STATUS(gpio)) & BIT(INTR_STATUS);
225 if (intstat || val == val2)
226 return;
227 } while (loop_limit-- > 0);
228 pr_err("dual-edge irq failed to stabilize, "
229 "interrupts dropped. %#08x != %#08x\n",
230 val, val2);
231}
232
233static void msm_gpio_irq_ack(unsigned int irq)
234{
235 int gpio = msm_irq_to_gpio(&msm_gpio.gpio_chip, irq);
236
237 writel(BIT(INTR_STATUS), GPIO_INTR_STATUS(gpio));
238 if (test_bit(gpio, msm_gpio.dual_edge_irqs))
239 msm_gpio_update_dual_edge_pos(gpio);
240}
241
242static void msm_gpio_irq_mask(unsigned int irq)
243{
244 int gpio = msm_irq_to_gpio(&msm_gpio.gpio_chip, irq);
245 unsigned long irq_flags;
246
247 spin_lock_irqsave(&tlmm_lock, irq_flags);
248 writel(TARGET_PROC_NONE, GPIO_INTR_CFG_SU(gpio));
249 clear_gpio_bits(INTR_RAW_STATUS_EN | INTR_ENABLE, GPIO_INTR_CFG(gpio));
250 __clear_bit(gpio, msm_gpio.enabled_irqs);
251 spin_unlock_irqrestore(&tlmm_lock, irq_flags);
252}
253
254static void msm_gpio_irq_unmask(unsigned int irq)
255{
256 int gpio = msm_irq_to_gpio(&msm_gpio.gpio_chip, irq);
257 unsigned long irq_flags;
258
259 spin_lock_irqsave(&tlmm_lock, irq_flags);
260 __set_bit(gpio, msm_gpio.enabled_irqs);
261 set_gpio_bits(INTR_RAW_STATUS_EN | INTR_ENABLE, GPIO_INTR_CFG(gpio));
262 writel(TARGET_PROC_SCORPION, GPIO_INTR_CFG_SU(gpio));
263 spin_unlock_irqrestore(&tlmm_lock, irq_flags);
264}
265
266static int msm_gpio_irq_set_type(unsigned int irq, unsigned int flow_type)
267{
268 int gpio = msm_irq_to_gpio(&msm_gpio.gpio_chip, irq);
269 unsigned long irq_flags;
270 uint32_t bits;
271
272 spin_lock_irqsave(&tlmm_lock, irq_flags);
273
274 bits = readl(GPIO_INTR_CFG(gpio));
275
276 if (flow_type & IRQ_TYPE_EDGE_BOTH) {
277 bits |= BIT(INTR_DECT_CTL);
278 irq_desc[irq].handle_irq = handle_edge_irq;
279 if ((flow_type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH)
280 __set_bit(gpio, msm_gpio.dual_edge_irqs);
281 else
282 __clear_bit(gpio, msm_gpio.dual_edge_irqs);
283 } else {
284 bits &= ~BIT(INTR_DECT_CTL);
285 irq_desc[irq].handle_irq = handle_level_irq;
286 __clear_bit(gpio, msm_gpio.dual_edge_irqs);
287 }
288
289 if (flow_type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_LEVEL_HIGH))
290 bits |= BIT(INTR_POL_CTL);
291 else
292 bits &= ~BIT(INTR_POL_CTL);
293
294 writel(bits, GPIO_INTR_CFG(gpio));
295
296 if ((flow_type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH)
297 msm_gpio_update_dual_edge_pos(gpio);
298
299 spin_unlock_irqrestore(&tlmm_lock, irq_flags);
300
301 return 0;
302}
303
304/*
305 * When the summary IRQ is raised, any number of GPIO lines may be high.
306 * It is the job of the summary handler to find all those GPIO lines
307 * which have been set as summary IRQ lines and which are triggered,
308 * and to call their interrupt handlers.
309 */
310static void msm_summary_irq_handler(unsigned int irq, struct irq_desc *desc)
311{
312 unsigned long i;
313
314 for (i = find_first_bit(msm_gpio.enabled_irqs, NR_GPIO_IRQS);
315 i < NR_GPIO_IRQS;
316 i = find_next_bit(msm_gpio.enabled_irqs, NR_GPIO_IRQS, i + 1)) {
317 if (readl(GPIO_INTR_STATUS(i)) & BIT(INTR_STATUS))
318 generic_handle_irq(msm_gpio_to_irq(&msm_gpio.gpio_chip,
319 i));
320 }
321 desc->chip->ack(irq);
322}
323
324static int msm_gpio_irq_set_wake(unsigned int irq, unsigned int on)
325{
326 int gpio = msm_irq_to_gpio(&msm_gpio.gpio_chip, irq);
327
328 if (on) {
329 if (bitmap_empty(msm_gpio.wake_irqs, NR_GPIO_IRQS))
330 set_irq_wake(TLMM_SCSS_SUMMARY_IRQ, 1);
331 set_bit(gpio, msm_gpio.wake_irqs);
332 } else {
333 clear_bit(gpio, msm_gpio.wake_irqs);
334 if (bitmap_empty(msm_gpio.wake_irqs, NR_GPIO_IRQS))
335 set_irq_wake(TLMM_SCSS_SUMMARY_IRQ, 0);
336 }
337
338 return 0;
339}
340
341static struct irq_chip msm_gpio_irq_chip = {
342 .name = "msmgpio",
343 .mask = msm_gpio_irq_mask,
344 .unmask = msm_gpio_irq_unmask,
345 .ack = msm_gpio_irq_ack,
346 .set_type = msm_gpio_irq_set_type,
347 .set_wake = msm_gpio_irq_set_wake,
99}; 348};
100 349
101static int __devinit msm_gpio_probe(struct platform_device *dev) 350static int __devinit msm_gpio_probe(struct platform_device *dev)
102{ 351{
103 int ret; 352 int i, irq, ret;
353
354 bitmap_zero(msm_gpio.enabled_irqs, NR_GPIO_IRQS);
355 bitmap_zero(msm_gpio.wake_irqs, NR_GPIO_IRQS);
356 bitmap_zero(msm_gpio.dual_edge_irqs, NR_GPIO_IRQS);
357 msm_gpio.gpio_chip.label = dev->name;
358 ret = gpiochip_add(&msm_gpio.gpio_chip);
359 if (ret < 0)
360 return ret;
104 361
105 msm_gpio.label = dev->name; 362 for (i = 0; i < msm_gpio.gpio_chip.ngpio; ++i) {
106 ret = gpiochip_add(&msm_gpio); 363 irq = msm_gpio_to_irq(&msm_gpio.gpio_chip, i);
364 set_irq_chip(irq, &msm_gpio_irq_chip);
365 set_irq_handler(irq, handle_level_irq);
366 set_irq_flags(irq, IRQF_VALID);
367 }
107 368
108 return ret; 369 set_irq_chained_handler(TLMM_SCSS_SUMMARY_IRQ,
370 msm_summary_irq_handler);
371 return 0;
109} 372}
110 373
111static int __devexit msm_gpio_remove(struct platform_device *dev) 374static int __devexit msm_gpio_remove(struct platform_device *dev)
112{ 375{
113 int ret = gpiochip_remove(&msm_gpio); 376 int ret = gpiochip_remove(&msm_gpio.gpio_chip);
114 377
115 if (ret < 0) 378 if (ret < 0)
116 return ret; 379 return ret;