blob: 394689a9219825f9640f377bcb15d4c648af6cb0 [file] [log] [blame]
Gregory Bean0cc2fc12010-11-24 11:53:51 -08001/* Copyright (c) 2010, Code Aurora Forum. All rights reserved.
2 *
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 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 * 02110-1301, USA.
16 *
17 */
Gregory Bean70cc2c02010-11-24 11:53:52 -080018#define pr_fmt(fmt) "%s: " fmt, __func__
19
20#include <linux/bitmap.h>
21#include <linux/bitops.h>
Gregory Bean0cc2fc12010-11-24 11:53:51 -080022#include <linux/gpio.h>
Gregory Bean70cc2c02010-11-24 11:53:52 -080023#include <linux/init.h>
24#include <linux/interrupt.h>
Gregory Bean0cc2fc12010-11-24 11:53:51 -080025#include <linux/io.h>
26#include <linux/irq.h>
27#include <linux/module.h>
28#include <linux/platform_device.h>
29#include <linux/spinlock.h>
30#include <mach/msm_iomap.h>
31#include "gpiomux.h"
32
33/* Bits of interest in the GPIO_IN_OUT register.
34 */
35enum {
Gregory Bean70cc2c02010-11-24 11:53:52 -080036 GPIO_IN = 0,
37 GPIO_OUT = 1
38};
39
40/* Bits of interest in the GPIO_INTR_STATUS register.
41 */
42enum {
43 INTR_STATUS = 0,
Gregory Bean0cc2fc12010-11-24 11:53:51 -080044};
45
46/* Bits of interest in the GPIO_CFG register.
47 */
48enum {
Gregory Bean70cc2c02010-11-24 11:53:52 -080049 GPIO_OE = 9,
Gregory Bean0cc2fc12010-11-24 11:53:51 -080050};
51
Gregory Bean70cc2c02010-11-24 11:53:52 -080052/* 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,
76};
77
78
79#define GPIO_INTR_CFG_SU(gpio) (MSM_TLMM_BASE + 0x0400 + (0x04 * (gpio)))
Gregory Bean0cc2fc12010-11-24 11:53:51 -080080#define GPIO_CONFIG(gpio) (MSM_TLMM_BASE + 0x1000 + (0x10 * (gpio)))
81#define GPIO_IN_OUT(gpio) (MSM_TLMM_BASE + 0x1004 + (0x10 * (gpio)))
Gregory Bean70cc2c02010-11-24 11:53:52 -080082#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};
Gregory Bean0cc2fc12010-11-24 11:53:51 -0800107
108static DEFINE_SPINLOCK(tlmm_lock);
109
Gregory Bean70cc2c02010-11-24 11:53:52 -0800110static 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
Gregory Bean0cc2fc12010-11-24 11:53:51 -0800125static int msm_gpio_get(struct gpio_chip *chip, unsigned offset)
126{
Gregory Bean70cc2c02010-11-24 11:53:52 -0800127 return readl(GPIO_IN_OUT(offset)) & BIT(GPIO_IN);
Gregory Bean0cc2fc12010-11-24 11:53:51 -0800128}
129
130static void msm_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
131{
Gregory Bean70cc2c02010-11-24 11:53:52 -0800132 writel(val ? BIT(GPIO_OUT) : 0, GPIO_IN_OUT(offset));
Gregory Bean0cc2fc12010-11-24 11:53:51 -0800133}
134
135static int msm_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
136{
137 unsigned long irq_flags;
138
139 spin_lock_irqsave(&tlmm_lock, irq_flags);
Gregory Bean70cc2c02010-11-24 11:53:52 -0800140 clear_gpio_bits(BIT(GPIO_OE), GPIO_CONFIG(offset));
Gregory Bean0cc2fc12010-11-24 11:53:51 -0800141 spin_unlock_irqrestore(&tlmm_lock, irq_flags);
142 return 0;
143}
144
145static int msm_gpio_direction_output(struct gpio_chip *chip,
146 unsigned offset,
147 int val)
148{
149 unsigned long irq_flags;
150
151 spin_lock_irqsave(&tlmm_lock, irq_flags);
152 msm_gpio_set(chip, offset, val);
Gregory Bean70cc2c02010-11-24 11:53:52 -0800153 set_gpio_bits(BIT(GPIO_OE), GPIO_CONFIG(offset));
Gregory Bean0cc2fc12010-11-24 11:53:51 -0800154 spin_unlock_irqrestore(&tlmm_lock, irq_flags);
155 return 0;
156}
157
158static int msm_gpio_request(struct gpio_chip *chip, unsigned offset)
159{
160 return msm_gpiomux_get(chip->base + offset);
161}
162
163static void msm_gpio_free(struct gpio_chip *chip, unsigned offset)
164{
165 msm_gpiomux_put(chip->base + offset);
166}
167
Gregory Bean70cc2c02010-11-24 11:53:52 -0800168static int msm_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
169{
170 return MSM_GPIO_TO_INT(chip->base + offset);
171}
172
173static inline int msm_irq_to_gpio(struct gpio_chip *chip, unsigned irq)
174{
175 return irq - MSM_GPIO_TO_INT(chip->base);
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
Thomas Gleixnercf8d1582011-03-24 11:58:31 +0100233static void msm_gpio_irq_ack(struct irq_data *d)
Gregory Bean70cc2c02010-11-24 11:53:52 -0800234{
Thomas Gleixnercf8d1582011-03-24 11:58:31 +0100235 int gpio = msm_irq_to_gpio(&msm_gpio.gpio_chip, d->irq);
Gregory Bean70cc2c02010-11-24 11:53:52 -0800236
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
Thomas Gleixnercf8d1582011-03-24 11:58:31 +0100242static void msm_gpio_irq_mask(struct irq_data *d)
Gregory Bean70cc2c02010-11-24 11:53:52 -0800243{
Thomas Gleixnercf8d1582011-03-24 11:58:31 +0100244 int gpio = msm_irq_to_gpio(&msm_gpio.gpio_chip, d->irq);
Gregory Bean70cc2c02010-11-24 11:53:52 -0800245 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
Thomas Gleixnercf8d1582011-03-24 11:58:31 +0100254static void msm_gpio_irq_unmask(struct irq_data *d)
Gregory Bean70cc2c02010-11-24 11:53:52 -0800255{
Thomas Gleixnercf8d1582011-03-24 11:58:31 +0100256 int gpio = msm_irq_to_gpio(&msm_gpio.gpio_chip, d->irq);
Gregory Bean70cc2c02010-11-24 11:53:52 -0800257 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
Thomas Gleixnercf8d1582011-03-24 11:58:31 +0100266static int msm_gpio_irq_set_type(struct irq_data *d, unsigned int flow_type)
Gregory Bean70cc2c02010-11-24 11:53:52 -0800267{
Thomas Gleixnercf8d1582011-03-24 11:58:31 +0100268 int gpio = msm_irq_to_gpio(&msm_gpio.gpio_chip, d->irq);
Gregory Bean70cc2c02010-11-24 11:53:52 -0800269 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{
Thomas Gleixnercf8d1582011-03-24 11:58:31 +0100312 struct irq_data *data = irq_desc_get_irq_data(desc);
Gregory Bean70cc2c02010-11-24 11:53:52 -0800313 unsigned long i;
314
315 for (i = find_first_bit(msm_gpio.enabled_irqs, NR_GPIO_IRQS);
316 i < NR_GPIO_IRQS;
317 i = find_next_bit(msm_gpio.enabled_irqs, NR_GPIO_IRQS, i + 1)) {
318 if (readl(GPIO_INTR_STATUS(i)) & BIT(INTR_STATUS))
319 generic_handle_irq(msm_gpio_to_irq(&msm_gpio.gpio_chip,
320 i));
321 }
Thomas Gleixnercf8d1582011-03-24 11:58:31 +0100322 data->chip->irq_ack(data);
Gregory Bean70cc2c02010-11-24 11:53:52 -0800323}
324
Thomas Gleixnercf8d1582011-03-24 11:58:31 +0100325static int msm_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
Gregory Bean70cc2c02010-11-24 11:53:52 -0800326{
Thomas Gleixnercf8d1582011-03-24 11:58:31 +0100327 int gpio = msm_irq_to_gpio(&msm_gpio.gpio_chip, d->irq);
Gregory Bean70cc2c02010-11-24 11:53:52 -0800328
329 if (on) {
330 if (bitmap_empty(msm_gpio.wake_irqs, NR_GPIO_IRQS))
331 set_irq_wake(TLMM_SCSS_SUMMARY_IRQ, 1);
332 set_bit(gpio, msm_gpio.wake_irqs);
333 } else {
334 clear_bit(gpio, msm_gpio.wake_irqs);
335 if (bitmap_empty(msm_gpio.wake_irqs, NR_GPIO_IRQS))
336 set_irq_wake(TLMM_SCSS_SUMMARY_IRQ, 0);
337 }
338
339 return 0;
340}
341
342static struct irq_chip msm_gpio_irq_chip = {
343 .name = "msmgpio",
Thomas Gleixnercf8d1582011-03-24 11:58:31 +0100344 .irq_mask = msm_gpio_irq_mask,
345 .irq_unmask = msm_gpio_irq_unmask,
346 .irq_ack = msm_gpio_irq_ack,
347 .irq_set_type = msm_gpio_irq_set_type,
348 .irq_set_wake = msm_gpio_irq_set_wake,
Gregory Bean0cc2fc12010-11-24 11:53:51 -0800349};
350
351static int __devinit msm_gpio_probe(struct platform_device *dev)
352{
Gregory Bean70cc2c02010-11-24 11:53:52 -0800353 int i, irq, ret;
Gregory Bean0cc2fc12010-11-24 11:53:51 -0800354
Gregory Bean70cc2c02010-11-24 11:53:52 -0800355 bitmap_zero(msm_gpio.enabled_irqs, NR_GPIO_IRQS);
356 bitmap_zero(msm_gpio.wake_irqs, NR_GPIO_IRQS);
357 bitmap_zero(msm_gpio.dual_edge_irqs, NR_GPIO_IRQS);
358 msm_gpio.gpio_chip.label = dev->name;
359 ret = gpiochip_add(&msm_gpio.gpio_chip);
360 if (ret < 0)
361 return ret;
Gregory Bean0cc2fc12010-11-24 11:53:51 -0800362
Gregory Bean70cc2c02010-11-24 11:53:52 -0800363 for (i = 0; i < msm_gpio.gpio_chip.ngpio; ++i) {
364 irq = msm_gpio_to_irq(&msm_gpio.gpio_chip, i);
365 set_irq_chip(irq, &msm_gpio_irq_chip);
366 set_irq_handler(irq, handle_level_irq);
367 set_irq_flags(irq, IRQF_VALID);
368 }
369
370 set_irq_chained_handler(TLMM_SCSS_SUMMARY_IRQ,
371 msm_summary_irq_handler);
372 return 0;
Gregory Bean0cc2fc12010-11-24 11:53:51 -0800373}
374
375static int __devexit msm_gpio_remove(struct platform_device *dev)
376{
Gregory Bean70cc2c02010-11-24 11:53:52 -0800377 int ret = gpiochip_remove(&msm_gpio.gpio_chip);
Gregory Bean0cc2fc12010-11-24 11:53:51 -0800378
379 if (ret < 0)
380 return ret;
381
382 set_irq_handler(TLMM_SCSS_SUMMARY_IRQ, NULL);
383
384 return 0;
385}
386
387static struct platform_driver msm_gpio_driver = {
388 .probe = msm_gpio_probe,
389 .remove = __devexit_p(msm_gpio_remove),
390 .driver = {
391 .name = "msmgpio",
392 .owner = THIS_MODULE,
393 },
394};
395
396static struct platform_device msm_device_gpio = {
397 .name = "msmgpio",
398 .id = -1,
399};
400
401static int __init msm_gpio_init(void)
402{
403 int rc;
404
405 rc = platform_driver_register(&msm_gpio_driver);
406 if (!rc) {
407 rc = platform_device_register(&msm_device_gpio);
408 if (rc)
409 platform_driver_unregister(&msm_gpio_driver);
410 }
411
412 return rc;
413}
414
415static void __exit msm_gpio_exit(void)
416{
417 platform_device_unregister(&msm_device_gpio);
418 platform_driver_unregister(&msm_gpio_driver);
419}
420
421postcore_initcall(msm_gpio_init);
422module_exit(msm_gpio_exit);
423
424MODULE_AUTHOR("Gregory Bean <gbean@codeaurora.org>");
425MODULE_DESCRIPTION("Driver for Qualcomm MSM TLMMv2 SoC GPIOs");
426MODULE_LICENSE("GPL v2");
427MODULE_ALIAS("platform:msmgpio");