blob: d5d6b927d3c2f570f8a5783afe3d39979b71a669 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
Gregory Bean0cc2fc12010-11-24 11:53:51 -08002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
Gregory Bean0cc2fc12010-11-24 11:53:51 -080012 */
Gregory Bean70cc2c02010-11-24 11:53:52 -080013#include <linux/bitmap.h>
14#include <linux/bitops.h>
Gregory Bean0cc2fc12010-11-24 11:53:51 -080015#include <linux/gpio.h>
Gregory Bean70cc2c02010-11-24 11:53:52 -080016#include <linux/init.h>
17#include <linux/interrupt.h>
Gregory Bean0cc2fc12010-11-24 11:53:51 -080018#include <linux/irq.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070019#include <linux/io.h>
Gregory Bean0cc2fc12010-11-24 11:53:51 -080020#include <linux/module.h>
Gregory Bean0cc2fc12010-11-24 11:53:51 -080021#include <linux/spinlock.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070022#include <linux/syscore_ops.h>
Will Deacon03dd7652011-02-21 14:54:57 +000023
24#include <asm/mach/irq.h>
25
Gregory Bean0cc2fc12010-11-24 11:53:51 -080026#include <mach/msm_iomap.h>
27#include "gpiomux.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070028#include "mpm.h"
Gregory Bean0cc2fc12010-11-24 11:53:51 -080029
30/* Bits of interest in the GPIO_IN_OUT register.
31 */
32enum {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070033 GPIO_IN_BIT = 0,
34 GPIO_OUT_BIT = 1
Gregory Bean70cc2c02010-11-24 11:53:52 -080035};
36
37/* Bits of interest in the GPIO_INTR_STATUS register.
38 */
39enum {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070040 INTR_STATUS_BIT = 0,
Gregory Bean0cc2fc12010-11-24 11:53:51 -080041};
42
43/* Bits of interest in the GPIO_CFG register.
44 */
45enum {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070046 GPIO_OE_BIT = 9,
Gregory Bean0cc2fc12010-11-24 11:53:51 -080047};
48
Gregory Bean70cc2c02010-11-24 11:53:52 -080049/* Bits of interest in the GPIO_INTR_CFG register.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070050 */
51enum {
52 INTR_ENABLE_BIT = 0,
53 INTR_POL_CTL_BIT = 1,
54 INTR_DECT_CTL_BIT = 2,
55 INTR_RAW_STATUS_EN_BIT = 3,
56};
57
58/* Codes of interest in GPIO_INTR_CFG_SU.
59 */
60enum {
61 TARGET_PROC_SCORPION = 4,
62 TARGET_PROC_NONE = 7,
63};
64
65/*
66 * There is no 'DC_POLARITY_LO' because the GIC is incapable
67 * of asserting on falling edge or level-low conditions. Even though
68 * the registers allow for low-polarity inputs, the case can never arise.
69 */
70enum {
71 DC_POLARITY_HI = BIT(11),
72 DC_IRQ_ENABLE = BIT(3),
73};
74
75enum msm_tlmm_register {
76 SDC4_HDRV_PULL_CTL = 0x20a0,
77 SDC3_HDRV_PULL_CTL = 0x20a4,
78 SDC1_HDRV_PULL_CTL = 0x20a0,
79};
80
81struct tlmm_field_cfg {
82 enum msm_tlmm_register reg;
83 u8 off;
84};
85
86static const struct tlmm_field_cfg tlmm_hdrv_cfgs[] = {
87 {SDC4_HDRV_PULL_CTL, 6}, /* TLMM_HDRV_SDC4_CLK */
88 {SDC4_HDRV_PULL_CTL, 3}, /* TLMM_HDRV_SDC4_CMD */
89 {SDC4_HDRV_PULL_CTL, 0}, /* TLMM_HDRV_SDC4_DATA */
90 {SDC3_HDRV_PULL_CTL, 6}, /* TLMM_HDRV_SDC3_CLK */
91 {SDC3_HDRV_PULL_CTL, 3}, /* TLMM_HDRV_SDC3_CMD */
92 {SDC3_HDRV_PULL_CTL, 0}, /* TLMM_HDRV_SDC3_DATA */
93 {SDC1_HDRV_PULL_CTL, 6}, /* TLMM_HDRV_SDC1_CLK */
94 {SDC1_HDRV_PULL_CTL, 3}, /* TLMM_HDRV_SDC1_CMD */
95 {SDC1_HDRV_PULL_CTL, 0}, /* TLMM_HDRV_SDC1_DATA */
96};
97
98static const struct tlmm_field_cfg tlmm_pull_cfgs[] = {
99 {SDC4_HDRV_PULL_CTL, 11}, /* TLMM_PULL_SDC4_CMD */
100 {SDC4_HDRV_PULL_CTL, 9}, /* TLMM_PULL_SDC4_DATA */
101 {SDC3_HDRV_PULL_CTL, 14}, /* TLMM_PULL_SDC3_CLK */
102 {SDC3_HDRV_PULL_CTL, 11}, /* TLMM_PULL_SDC3_CMD */
103 {SDC3_HDRV_PULL_CTL, 9}, /* TLMM_PULL_SDC3_DATA */
104 {SDC1_HDRV_PULL_CTL, 13}, /* TLMM_PULL_SDC1_CLK */
105 {SDC1_HDRV_PULL_CTL, 11}, /* TLMM_PULL_SDC1_CMD */
106 {SDC1_HDRV_PULL_CTL, 9}, /* TLMM_PULL_SDC1_DATA */
107};
108
109/*
110 * Supported arch specific irq extension.
111 * Default make them NULL.
112 */
113struct irq_chip msm_gpio_irq_extn = {
114 .irq_eoi = NULL,
115 .irq_mask = NULL,
116 .irq_unmask = NULL,
117 .irq_retrigger = NULL,
118 .irq_set_type = NULL,
119 .irq_set_wake = NULL,
120 .irq_disable = NULL,
121};
122
123/*
Gregory Bean70cc2c02010-11-24 11:53:52 -0800124 * When a GPIO triggers, two separate decisions are made, controlled
125 * by two separate flags.
126 *
127 * - First, INTR_RAW_STATUS_EN controls whether or not the GPIO_INTR_STATUS
128 * register for that GPIO will be updated to reflect the triggering of that
129 * gpio. If this bit is 0, this register will not be updated.
130 * - Second, INTR_ENABLE controls whether an interrupt is triggered.
131 *
132 * If INTR_ENABLE is set and INTR_RAW_STATUS_EN is NOT set, an interrupt
133 * can be triggered but the status register will not reflect it.
134 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700135#define INTR_RAW_STATUS_EN BIT(INTR_RAW_STATUS_EN_BIT)
136#define INTR_ENABLE BIT(INTR_ENABLE_BIT)
137#define INTR_DECT_CTL_EDGE BIT(INTR_DECT_CTL_BIT)
138#define INTR_POL_CTL_HI BIT(INTR_POL_CTL_BIT)
Gregory Bean70cc2c02010-11-24 11:53:52 -0800139
140#define GPIO_INTR_CFG_SU(gpio) (MSM_TLMM_BASE + 0x0400 + (0x04 * (gpio)))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700141#define DIR_CONN_INTR_CFG_SU(irq) (MSM_TLMM_BASE + 0x0700 + (0x04 * (irq)))
Gregory Bean0cc2fc12010-11-24 11:53:51 -0800142#define GPIO_CONFIG(gpio) (MSM_TLMM_BASE + 0x1000 + (0x10 * (gpio)))
143#define GPIO_IN_OUT(gpio) (MSM_TLMM_BASE + 0x1004 + (0x10 * (gpio)))
Gregory Bean70cc2c02010-11-24 11:53:52 -0800144#define GPIO_INTR_CFG(gpio) (MSM_TLMM_BASE + 0x1008 + (0x10 * (gpio)))
145#define GPIO_INTR_STATUS(gpio) (MSM_TLMM_BASE + 0x100c + (0x10 * (gpio)))
146
147/**
148 * struct msm_gpio_dev: the MSM8660 SoC GPIO device structure
149 *
150 * @enabled_irqs: a bitmap used to optimize the summary-irq handler. By
151 * keeping track of which gpios are unmasked as irq sources, we avoid
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700152 * having to do __raw_readl calls on hundreds of iomapped registers each time
Gregory Bean70cc2c02010-11-24 11:53:52 -0800153 * the summary interrupt fires in order to locate the active interrupts.
154 *
155 * @wake_irqs: a bitmap for tracking which interrupt lines are enabled
156 * as wakeup sources. When the device is suspended, interrupts which are
157 * not wakeup sources are disabled.
158 *
159 * @dual_edge_irqs: a bitmap used to track which irqs are configured
160 * as dual-edge, as this is not supported by the hardware and requires
161 * some special handling in the driver.
162 */
163struct msm_gpio_dev {
164 struct gpio_chip gpio_chip;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700165 DECLARE_BITMAP(enabled_irqs, NR_MSM_GPIOS);
166 DECLARE_BITMAP(wake_irqs, NR_MSM_GPIOS);
167 DECLARE_BITMAP(dual_edge_irqs, NR_MSM_GPIOS);
Gregory Bean70cc2c02010-11-24 11:53:52 -0800168};
Gregory Bean0cc2fc12010-11-24 11:53:51 -0800169
170static DEFINE_SPINLOCK(tlmm_lock);
171
Gregory Bean70cc2c02010-11-24 11:53:52 -0800172static inline struct msm_gpio_dev *to_msm_gpio_dev(struct gpio_chip *chip)
173{
174 return container_of(chip, struct msm_gpio_dev, gpio_chip);
175}
176
177static inline void set_gpio_bits(unsigned n, void __iomem *reg)
178{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700179 __raw_writel(__raw_readl(reg) | n, reg);
Gregory Bean70cc2c02010-11-24 11:53:52 -0800180}
181
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700182static inline void clr_gpio_bits(unsigned n, void __iomem *reg)
Gregory Bean70cc2c02010-11-24 11:53:52 -0800183{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700184 __raw_writel(__raw_readl(reg) & ~n, reg);
Gregory Bean70cc2c02010-11-24 11:53:52 -0800185}
186
Gregory Bean0cc2fc12010-11-24 11:53:51 -0800187static int msm_gpio_get(struct gpio_chip *chip, unsigned offset)
188{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700189 int rc;
190 rc = __raw_readl(GPIO_IN_OUT(offset)) & BIT(GPIO_IN_BIT);
191 mb();
192 return rc;
Gregory Bean0cc2fc12010-11-24 11:53:51 -0800193}
194
195static void msm_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
196{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700197 __raw_writel(val ? BIT(GPIO_OUT_BIT) : 0, GPIO_IN_OUT(offset));
198 mb();
Gregory Bean0cc2fc12010-11-24 11:53:51 -0800199}
200
201static int msm_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
202{
203 unsigned long irq_flags;
204
205 spin_lock_irqsave(&tlmm_lock, irq_flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700206 clr_gpio_bits(BIT(GPIO_OE_BIT), GPIO_CONFIG(offset));
207 mb();
Gregory Bean0cc2fc12010-11-24 11:53:51 -0800208 spin_unlock_irqrestore(&tlmm_lock, irq_flags);
209 return 0;
210}
211
212static int msm_gpio_direction_output(struct gpio_chip *chip,
213 unsigned offset,
214 int val)
215{
216 unsigned long irq_flags;
217
218 spin_lock_irqsave(&tlmm_lock, irq_flags);
219 msm_gpio_set(chip, offset, val);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700220 set_gpio_bits(BIT(GPIO_OE_BIT), GPIO_CONFIG(offset));
221 mb();
Gregory Bean0cc2fc12010-11-24 11:53:51 -0800222 spin_unlock_irqrestore(&tlmm_lock, irq_flags);
223 return 0;
224}
225
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700226static int msm_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
227{
228 return MSM_GPIO_TO_INT(offset - chip->base);
229}
230
231static inline int msm_irq_to_gpio(struct gpio_chip *chip, unsigned irq)
232{
233 return irq - MSM_GPIO_TO_INT(chip->base);
234}
235
Gregory Bean0cc2fc12010-11-24 11:53:51 -0800236static int msm_gpio_request(struct gpio_chip *chip, unsigned offset)
237{
238 return msm_gpiomux_get(chip->base + offset);
239}
240
241static void msm_gpio_free(struct gpio_chip *chip, unsigned offset)
242{
243 msm_gpiomux_put(chip->base + offset);
244}
245
Gregory Bean70cc2c02010-11-24 11:53:52 -0800246static struct msm_gpio_dev msm_gpio = {
247 .gpio_chip = {
248 .base = 0,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700249 .ngpio = NR_MSM_GPIOS,
Gregory Bean70cc2c02010-11-24 11:53:52 -0800250 .direction_input = msm_gpio_direction_input,
251 .direction_output = msm_gpio_direction_output,
252 .get = msm_gpio_get,
253 .set = msm_gpio_set,
254 .to_irq = msm_gpio_to_irq,
255 .request = msm_gpio_request,
256 .free = msm_gpio_free,
257 },
258};
259
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700260static void switch_mpm_config(struct irq_data *d, unsigned val)
261{
262 /* switch the configuration in the mpm as well */
263 if (!msm_gpio_irq_extn.irq_set_type)
264 return;
265
266 if (val)
267 msm_gpio_irq_extn.irq_set_type(d, IRQF_TRIGGER_FALLING);
268 else
269 msm_gpio_irq_extn.irq_set_type(d, IRQF_TRIGGER_RISING);
270}
271
Gregory Bean70cc2c02010-11-24 11:53:52 -0800272/* For dual-edge interrupts in software, since the hardware has no
273 * such support:
274 *
275 * At appropriate moments, this function may be called to flip the polarity
276 * settings of both-edge irq lines to try and catch the next edge.
277 *
278 * The attempt is considered successful if:
279 * - the status bit goes high, indicating that an edge was caught, or
280 * - the input value of the gpio doesn't change during the attempt.
281 * If the value changes twice during the process, that would cause the first
282 * test to fail but would force the second, as two opposite
283 * transitions would cause a detection no matter the polarity setting.
284 *
285 * The do-loop tries to sledge-hammer closed the timing hole between
286 * the initial value-read and the polarity-write - if the line value changes
287 * during that window, an interrupt is lost, the new polarity setting is
288 * incorrect, and the first success test will fail, causing a retry.
289 *
290 * Algorithm comes from Google's msmgpio driver, see mach-msm/gpio.c.
291 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700292static void msm_gpio_update_dual_edge_pos(struct irq_data *d, unsigned gpio)
Gregory Bean70cc2c02010-11-24 11:53:52 -0800293{
294 int loop_limit = 100;
295 unsigned val, val2, intstat;
296
297 do {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700298 val = __raw_readl(GPIO_IN_OUT(gpio)) & BIT(GPIO_IN_BIT);
Gregory Bean70cc2c02010-11-24 11:53:52 -0800299 if (val)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700300 clr_gpio_bits(INTR_POL_CTL_HI, GPIO_INTR_CFG(gpio));
Gregory Bean70cc2c02010-11-24 11:53:52 -0800301 else
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700302 set_gpio_bits(INTR_POL_CTL_HI, GPIO_INTR_CFG(gpio));
303 val2 = __raw_readl(GPIO_IN_OUT(gpio)) & BIT(GPIO_IN_BIT);
304 intstat = __raw_readl(GPIO_INTR_STATUS(gpio)) &
305 BIT(INTR_STATUS_BIT);
306 if (intstat || val == val2) {
307 switch_mpm_config(d, val);
Gregory Bean70cc2c02010-11-24 11:53:52 -0800308 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700309 }
Gregory Bean70cc2c02010-11-24 11:53:52 -0800310 } while (loop_limit-- > 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700311 pr_err("%s: dual-edge irq failed to stabilize, "
Gregory Bean70cc2c02010-11-24 11:53:52 -0800312 "interrupts dropped. %#08x != %#08x\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700313 __func__, val, val2);
Gregory Bean70cc2c02010-11-24 11:53:52 -0800314}
315
Thomas Gleixnercf8d1582011-03-24 11:58:31 +0100316static void msm_gpio_irq_ack(struct irq_data *d)
Gregory Bean70cc2c02010-11-24 11:53:52 -0800317{
Thomas Gleixnercf8d1582011-03-24 11:58:31 +0100318 int gpio = msm_irq_to_gpio(&msm_gpio.gpio_chip, d->irq);
Gregory Bean70cc2c02010-11-24 11:53:52 -0800319
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700320 __raw_writel(BIT(INTR_STATUS_BIT), GPIO_INTR_STATUS(gpio));
Gregory Bean70cc2c02010-11-24 11:53:52 -0800321 if (test_bit(gpio, msm_gpio.dual_edge_irqs))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700322 msm_gpio_update_dual_edge_pos(d, gpio);
323 mb();
324}
325
326static void __msm_gpio_irq_mask(unsigned int gpio)
327{
328 __raw_writel(TARGET_PROC_NONE, GPIO_INTR_CFG_SU(gpio));
329 clr_gpio_bits(INTR_RAW_STATUS_EN | INTR_ENABLE, GPIO_INTR_CFG(gpio));
Gregory Bean70cc2c02010-11-24 11:53:52 -0800330}
331
Thomas Gleixnercf8d1582011-03-24 11:58:31 +0100332static void msm_gpio_irq_mask(struct irq_data *d)
Gregory Bean70cc2c02010-11-24 11:53:52 -0800333{
Thomas Gleixnercf8d1582011-03-24 11:58:31 +0100334 int gpio = msm_irq_to_gpio(&msm_gpio.gpio_chip, d->irq);
Gregory Bean70cc2c02010-11-24 11:53:52 -0800335 unsigned long irq_flags;
336
337 spin_lock_irqsave(&tlmm_lock, irq_flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700338 __msm_gpio_irq_mask(gpio);
Gregory Bean70cc2c02010-11-24 11:53:52 -0800339 __clear_bit(gpio, msm_gpio.enabled_irqs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700340 mb();
Gregory Bean70cc2c02010-11-24 11:53:52 -0800341 spin_unlock_irqrestore(&tlmm_lock, irq_flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700342
343 if (msm_gpio_irq_extn.irq_mask)
344 msm_gpio_irq_extn.irq_mask(d);
345
346}
347
348static void __msm_gpio_irq_unmask(unsigned int gpio)
349{
350 set_gpio_bits(INTR_RAW_STATUS_EN | INTR_ENABLE, GPIO_INTR_CFG(gpio));
351 __raw_writel(TARGET_PROC_SCORPION, GPIO_INTR_CFG_SU(gpio));
Gregory Bean70cc2c02010-11-24 11:53:52 -0800352}
353
Thomas Gleixnercf8d1582011-03-24 11:58:31 +0100354static void msm_gpio_irq_unmask(struct irq_data *d)
Gregory Bean70cc2c02010-11-24 11:53:52 -0800355{
Thomas Gleixnercf8d1582011-03-24 11:58:31 +0100356 int gpio = msm_irq_to_gpio(&msm_gpio.gpio_chip, d->irq);
Gregory Bean70cc2c02010-11-24 11:53:52 -0800357 unsigned long irq_flags;
358
359 spin_lock_irqsave(&tlmm_lock, irq_flags);
360 __set_bit(gpio, msm_gpio.enabled_irqs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700361 __msm_gpio_irq_unmask(gpio);
362 mb();
Gregory Bean70cc2c02010-11-24 11:53:52 -0800363 spin_unlock_irqrestore(&tlmm_lock, irq_flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700364
365 if (msm_gpio_irq_extn.irq_mask)
366 msm_gpio_irq_extn.irq_unmask(d);
367}
368
369static void msm_gpio_irq_disable(struct irq_data *d)
370{
371 if (msm_gpio_irq_extn.irq_disable)
372 msm_gpio_irq_extn.irq_disable(d);
Gregory Bean70cc2c02010-11-24 11:53:52 -0800373}
374
Thomas Gleixnercf8d1582011-03-24 11:58:31 +0100375static int msm_gpio_irq_set_type(struct irq_data *d, unsigned int flow_type)
Gregory Bean70cc2c02010-11-24 11:53:52 -0800376{
Thomas Gleixnercf8d1582011-03-24 11:58:31 +0100377 int gpio = msm_irq_to_gpio(&msm_gpio.gpio_chip, d->irq);
Gregory Bean70cc2c02010-11-24 11:53:52 -0800378 unsigned long irq_flags;
379 uint32_t bits;
380
381 spin_lock_irqsave(&tlmm_lock, irq_flags);
382
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700383 bits = __raw_readl(GPIO_INTR_CFG(gpio));
Gregory Bean70cc2c02010-11-24 11:53:52 -0800384
385 if (flow_type & IRQ_TYPE_EDGE_BOTH) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700386 bits |= INTR_DECT_CTL_EDGE;
Thomas Gleixner70c4fa22011-03-24 12:41:27 +0100387 __irq_set_handler_locked(d->irq, handle_edge_irq);
Gregory Bean70cc2c02010-11-24 11:53:52 -0800388 if ((flow_type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH)
389 __set_bit(gpio, msm_gpio.dual_edge_irqs);
390 else
391 __clear_bit(gpio, msm_gpio.dual_edge_irqs);
392 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700393 bits &= ~INTR_DECT_CTL_EDGE;
Thomas Gleixner70c4fa22011-03-24 12:41:27 +0100394 __irq_set_handler_locked(d->irq, handle_level_irq);
Gregory Bean70cc2c02010-11-24 11:53:52 -0800395 __clear_bit(gpio, msm_gpio.dual_edge_irqs);
396 }
397
398 if (flow_type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_LEVEL_HIGH))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700399 bits |= INTR_POL_CTL_HI;
Gregory Bean70cc2c02010-11-24 11:53:52 -0800400 else
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700401 bits &= ~INTR_POL_CTL_HI;
Gregory Bean70cc2c02010-11-24 11:53:52 -0800402
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700403 __raw_writel(bits, GPIO_INTR_CFG(gpio));
Gregory Bean70cc2c02010-11-24 11:53:52 -0800404
405 if ((flow_type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700406 msm_gpio_update_dual_edge_pos(d, gpio);
Gregory Bean70cc2c02010-11-24 11:53:52 -0800407
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700408 mb();
Gregory Bean70cc2c02010-11-24 11:53:52 -0800409 spin_unlock_irqrestore(&tlmm_lock, irq_flags);
410
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700411 if (msm_gpio_irq_extn.irq_set_type)
412 msm_gpio_irq_extn.irq_set_type(d, flow_type);
413
Gregory Bean70cc2c02010-11-24 11:53:52 -0800414 return 0;
415}
416
417/*
418 * When the summary IRQ is raised, any number of GPIO lines may be high.
419 * It is the job of the summary handler to find all those GPIO lines
420 * which have been set as summary IRQ lines and which are triggered,
421 * and to call their interrupt handlers.
422 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700423static irqreturn_t msm_summary_irq_handler(int irq, void *data)
Gregory Bean70cc2c02010-11-24 11:53:52 -0800424{
425 unsigned long i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700426 struct irq_desc *desc = irq_to_desc(irq);
Will Deacon03dd7652011-02-21 14:54:57 +0000427 struct irq_chip *chip = irq_desc_get_chip(desc);
428
429 chained_irq_enter(chip, desc);
Gregory Bean70cc2c02010-11-24 11:53:52 -0800430
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700431 for (i = find_first_bit(msm_gpio.enabled_irqs, NR_MSM_GPIOS);
432 i < NR_MSM_GPIOS;
433 i = find_next_bit(msm_gpio.enabled_irqs, NR_MSM_GPIOS, i + 1)) {
434 if (__raw_readl(GPIO_INTR_STATUS(i)) & BIT(INTR_STATUS_BIT))
Gregory Bean70cc2c02010-11-24 11:53:52 -0800435 generic_handle_irq(msm_gpio_to_irq(&msm_gpio.gpio_chip,
436 i));
437 }
Will Deacon03dd7652011-02-21 14:54:57 +0000438
439 chained_irq_exit(chip, desc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700440 return IRQ_HANDLED;
Gregory Bean70cc2c02010-11-24 11:53:52 -0800441}
442
Thomas Gleixnercf8d1582011-03-24 11:58:31 +0100443static int msm_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
Gregory Bean70cc2c02010-11-24 11:53:52 -0800444{
Thomas Gleixnercf8d1582011-03-24 11:58:31 +0100445 int gpio = msm_irq_to_gpio(&msm_gpio.gpio_chip, d->irq);
Gregory Bean70cc2c02010-11-24 11:53:52 -0800446
447 if (on) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700448 if (bitmap_empty(msm_gpio.wake_irqs, NR_MSM_GPIOS))
449 irq_set_irq_wake(TLMM_MSM_SUMMARY_IRQ, 1);
Gregory Bean70cc2c02010-11-24 11:53:52 -0800450 set_bit(gpio, msm_gpio.wake_irqs);
451 } else {
452 clear_bit(gpio, msm_gpio.wake_irqs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700453 if (bitmap_empty(msm_gpio.wake_irqs, NR_MSM_GPIOS))
454 irq_set_irq_wake(TLMM_MSM_SUMMARY_IRQ, 0);
Gregory Bean70cc2c02010-11-24 11:53:52 -0800455 }
456
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700457 if (msm_gpio_irq_extn.irq_set_wake)
458 msm_gpio_irq_extn.irq_set_wake(d, on);
459
Gregory Bean70cc2c02010-11-24 11:53:52 -0800460 return 0;
461}
462
463static struct irq_chip msm_gpio_irq_chip = {
464 .name = "msmgpio",
Thomas Gleixnercf8d1582011-03-24 11:58:31 +0100465 .irq_mask = msm_gpio_irq_mask,
466 .irq_unmask = msm_gpio_irq_unmask,
467 .irq_ack = msm_gpio_irq_ack,
468 .irq_set_type = msm_gpio_irq_set_type,
469 .irq_set_wake = msm_gpio_irq_set_wake,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700470 .irq_disable = msm_gpio_irq_disable,
Gregory Bean0cc2fc12010-11-24 11:53:51 -0800471};
472
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700473static int __devinit msm_gpio_probe(void)
Gregory Bean0cc2fc12010-11-24 11:53:51 -0800474{
Gregory Bean70cc2c02010-11-24 11:53:52 -0800475 int i, irq, ret;
Gregory Bean0cc2fc12010-11-24 11:53:51 -0800476
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700477 spin_lock_init(&tlmm_lock);
478 bitmap_zero(msm_gpio.enabled_irqs, NR_MSM_GPIOS);
479 bitmap_zero(msm_gpio.wake_irqs, NR_MSM_GPIOS);
480 bitmap_zero(msm_gpio.dual_edge_irqs, NR_MSM_GPIOS);
Gregory Bean70cc2c02010-11-24 11:53:52 -0800481 ret = gpiochip_add(&msm_gpio.gpio_chip);
482 if (ret < 0)
483 return ret;
Gregory Bean0cc2fc12010-11-24 11:53:51 -0800484
Gregory Bean70cc2c02010-11-24 11:53:52 -0800485 for (i = 0; i < msm_gpio.gpio_chip.ngpio; ++i) {
486 irq = msm_gpio_to_irq(&msm_gpio.gpio_chip, i);
Thomas Gleixnerf38c02f2011-03-24 13:35:09 +0100487 irq_set_chip_and_handler(irq, &msm_gpio_irq_chip,
488 handle_level_irq);
Gregory Bean70cc2c02010-11-24 11:53:52 -0800489 set_irq_flags(irq, IRQF_VALID);
490 }
491
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700492 ret = request_irq(TLMM_MSM_SUMMARY_IRQ, msm_summary_irq_handler,
493 IRQF_TRIGGER_HIGH, "msmgpio", NULL);
494 if (ret) {
495 pr_err("Request_irq failed for TLMM_MSM_SUMMARY_IRQ - %d\n",
496 ret);
497 return ret;
498 }
Gregory Bean70cc2c02010-11-24 11:53:52 -0800499 return 0;
Gregory Bean0cc2fc12010-11-24 11:53:51 -0800500}
501
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700502static int __devexit msm_gpio_remove(void)
Gregory Bean0cc2fc12010-11-24 11:53:51 -0800503{
Gregory Bean70cc2c02010-11-24 11:53:52 -0800504 int ret = gpiochip_remove(&msm_gpio.gpio_chip);
Gregory Bean0cc2fc12010-11-24 11:53:51 -0800505
506 if (ret < 0)
507 return ret;
508
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700509 irq_set_handler(TLMM_MSM_SUMMARY_IRQ, NULL);
Gregory Bean0cc2fc12010-11-24 11:53:51 -0800510
511 return 0;
512}
513
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700514#ifdef CONFIG_PM
515static int msm_gpio_suspend(void)
516{
517 unsigned long irq_flags;
518 unsigned long i;
Gregory Bean0cc2fc12010-11-24 11:53:51 -0800519
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700520 spin_lock_irqsave(&tlmm_lock, irq_flags);
521 for_each_set_bit(i, msm_gpio.enabled_irqs, NR_MSM_GPIOS)
522 __msm_gpio_irq_mask(i);
523
524 for_each_set_bit(i, msm_gpio.wake_irqs, NR_MSM_GPIOS)
525 __msm_gpio_irq_unmask(i);
526 mb();
527 spin_unlock_irqrestore(&tlmm_lock, irq_flags);
528 return 0;
529}
530
531extern int msm_show_resume_irq_mask;
532
533void msm_gpio_show_resume_irq(void)
534{
535 unsigned long irq_flags;
536 int i, irq, intstat;
537
538 if (!msm_show_resume_irq_mask)
539 return;
540
541 spin_lock_irqsave(&tlmm_lock, irq_flags);
542 for_each_set_bit(i, msm_gpio.wake_irqs, NR_MSM_GPIOS) {
543 intstat = __raw_readl(GPIO_INTR_STATUS(i)) &
544 BIT(INTR_STATUS_BIT);
545 if (intstat) {
546 irq = msm_gpio_to_irq(&msm_gpio.gpio_chip, i);
547 pr_warning("%s: %d triggered\n",
548 __func__, irq);
549 }
550 }
551 spin_unlock_irqrestore(&tlmm_lock, irq_flags);
552}
553
554static void msm_gpio_resume(void)
555{
556 unsigned long irq_flags;
557 unsigned long i;
558
559 msm_gpio_show_resume_irq();
560
561 spin_lock_irqsave(&tlmm_lock, irq_flags);
562 for_each_set_bit(i, msm_gpio.wake_irqs, NR_MSM_GPIOS)
563 __msm_gpio_irq_mask(i);
564
565 for_each_set_bit(i, msm_gpio.enabled_irqs, NR_MSM_GPIOS)
566 __msm_gpio_irq_unmask(i);
567 mb();
568 spin_unlock_irqrestore(&tlmm_lock, irq_flags);
569}
570#else
571#define msm_gpio_suspend NULL
572#define msm_gpio_resume NULL
573#endif
574
575static struct syscore_ops msm_gpio_syscore_ops = {
576 .suspend = msm_gpio_suspend,
577 .resume = msm_gpio_resume,
Gregory Bean0cc2fc12010-11-24 11:53:51 -0800578};
579
580static int __init msm_gpio_init(void)
581{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700582 msm_gpio_probe();
583 register_syscore_ops(&msm_gpio_syscore_ops);
584 return 0;
Gregory Bean0cc2fc12010-11-24 11:53:51 -0800585}
586
587static void __exit msm_gpio_exit(void)
588{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700589 unregister_syscore_ops(&msm_gpio_syscore_ops);
590 msm_gpio_remove();
Gregory Bean0cc2fc12010-11-24 11:53:51 -0800591}
592
593postcore_initcall(msm_gpio_init);
594module_exit(msm_gpio_exit);
595
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700596static void msm_tlmm_set_field(const struct tlmm_field_cfg *configs,
597 unsigned id, unsigned width, unsigned val)
598{
599 unsigned long irqflags;
600 u32 mask = (1 << width) - 1;
601 u32 __iomem *reg = MSM_TLMM_BASE + configs[id].reg;
602 u32 reg_val;
603
604 spin_lock_irqsave(&tlmm_lock, irqflags);
605 reg_val = __raw_readl(reg);
606 reg_val &= ~(mask << configs[id].off);
607 reg_val |= (val & mask) << configs[id].off;
608 __raw_writel(reg_val, reg);
609 mb();
610 spin_unlock_irqrestore(&tlmm_lock, irqflags);
611}
612
613void msm_tlmm_set_hdrive(enum msm_tlmm_hdrive_tgt tgt, int drv_str)
614{
615 msm_tlmm_set_field(tlmm_hdrv_cfgs, tgt, 3, drv_str);
616}
617EXPORT_SYMBOL(msm_tlmm_set_hdrive);
618
619void msm_tlmm_set_pull(enum msm_tlmm_pull_tgt tgt, int pull)
620{
621 msm_tlmm_set_field(tlmm_pull_cfgs, tgt, 2, pull);
622}
623EXPORT_SYMBOL(msm_tlmm_set_pull);
624
625int gpio_tlmm_config(unsigned config, unsigned disable)
626{
627 uint32_t flags;
628 unsigned gpio = GPIO_PIN(config);
629
630 if (gpio > NR_MSM_GPIOS)
631 return -EINVAL;
632
633 flags = ((GPIO_DIR(config) << 9) & (0x1 << 9)) |
634 ((GPIO_DRVSTR(config) << 6) & (0x7 << 6)) |
635 ((GPIO_FUNC(config) << 2) & (0xf << 2)) |
636 ((GPIO_PULL(config) & 0x3));
637 __raw_writel(flags, GPIO_CONFIG(gpio));
638 mb();
639
640 return 0;
641}
642EXPORT_SYMBOL(gpio_tlmm_config);
643
644int msm_gpio_install_direct_irq(unsigned gpio, unsigned irq,
645 unsigned int input_polarity)
646{
647 unsigned long irq_flags;
648 uint32_t bits;
649
650 if (gpio >= NR_MSM_GPIOS || irq >= NR_TLMM_MSM_DIR_CONN_IRQ)
651 return -EINVAL;
652
653 spin_lock_irqsave(&tlmm_lock, irq_flags);
654
655 __raw_writel(__raw_readl(GPIO_CONFIG(gpio)) | BIT(GPIO_OE_BIT),
656 GPIO_CONFIG(gpio));
657 __raw_writel(__raw_readl(GPIO_INTR_CFG(gpio)) &
658 ~(INTR_RAW_STATUS_EN | INTR_ENABLE),
659 GPIO_INTR_CFG(gpio));
660 __raw_writel(DC_IRQ_ENABLE | TARGET_PROC_NONE,
661 GPIO_INTR_CFG_SU(gpio));
662
663 bits = TARGET_PROC_SCORPION | (gpio << 3);
664 if (input_polarity)
665 bits |= DC_POLARITY_HI;
666 __raw_writel(bits, DIR_CONN_INTR_CFG_SU(irq));
667
668 mb();
669 spin_unlock_irqrestore(&tlmm_lock, irq_flags);
670
671 return 0;
672}
673EXPORT_SYMBOL(msm_gpio_install_direct_irq);
674
Gregory Bean0cc2fc12010-11-24 11:53:51 -0800675MODULE_AUTHOR("Gregory Bean <gbean@codeaurora.org>");
676MODULE_DESCRIPTION("Driver for Qualcomm MSM TLMMv2 SoC GPIOs");
677MODULE_LICENSE("GPL v2");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700678MODULE_ALIAS("sysdev:msmgpio");