blob: fa8239a9edd7478ceb6cb39cd5eb44203c85993f [file] [log] [blame]
Sathish Ambleyd2ad0fa2012-03-23 11:23:47 -07001/* Copyright (c) 2012, 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 */
13#include <linux/bitmap.h>
14#include <linux/bitops.h>
15#include <linux/gpio.h>
16#include <linux/init.h>
17#include <linux/interrupt.h>
18#include <linux/irq.h>
19#include <linux/io.h>
20#include <linux/module.h>
21#include <linux/spinlock.h>
22#include <linux/syscore_ops.h>
23#include <linux/irqdomain.h>
24#include <linux/of.h>
25#include <linux/err.h>
26
27#include <asm/mach/irq.h>
28
29#include <mach/msm_iomap.h>
30#include <mach/gpiomux.h>
31#include <mach/mpm.h>
32#include "gpio-msm-common.h"
33
Sujit Reddy Thumma39306c22012-06-26 15:39:26 +053034#ifdef CONFIG_GPIO_MSM_V3
35enum msm_tlmm_register {
36 SDC4_HDRV_PULL_CTL = 0x0, /* NOT USED */
37 SDC3_HDRV_PULL_CTL = 0x0, /* NOT USED */
38 SDC2_HDRV_PULL_CTL = 0x2048,
39 SDC1_HDRV_PULL_CTL = 0x2044,
40};
41#else
Sathish Ambleyd2ad0fa2012-03-23 11:23:47 -070042enum msm_tlmm_register {
43 SDC4_HDRV_PULL_CTL = 0x20a0,
44 SDC3_HDRV_PULL_CTL = 0x20a4,
Sujit Reddy Thumma39306c22012-06-26 15:39:26 +053045 SDC2_HDRV_PULL_CTL = 0x0, /* NOT USED */
Sathish Ambleyd2ad0fa2012-03-23 11:23:47 -070046 SDC1_HDRV_PULL_CTL = 0x20a0,
47};
Sujit Reddy Thumma39306c22012-06-26 15:39:26 +053048#endif
Sathish Ambleyd2ad0fa2012-03-23 11:23:47 -070049
50struct tlmm_field_cfg {
51 enum msm_tlmm_register reg;
52 u8 off;
53};
54
55static const struct tlmm_field_cfg tlmm_hdrv_cfgs[] = {
56 {SDC4_HDRV_PULL_CTL, 6}, /* TLMM_HDRV_SDC4_CLK */
57 {SDC4_HDRV_PULL_CTL, 3}, /* TLMM_HDRV_SDC4_CMD */
58 {SDC4_HDRV_PULL_CTL, 0}, /* TLMM_HDRV_SDC4_DATA */
59 {SDC3_HDRV_PULL_CTL, 6}, /* TLMM_HDRV_SDC3_CLK */
60 {SDC3_HDRV_PULL_CTL, 3}, /* TLMM_HDRV_SDC3_CMD */
61 {SDC3_HDRV_PULL_CTL, 0}, /* TLMM_HDRV_SDC3_DATA */
Sujit Reddy Thumma39306c22012-06-26 15:39:26 +053062 {SDC2_HDRV_PULL_CTL, 6}, /* TLMM_HDRV_SDC2_CLK */
63 {SDC2_HDRV_PULL_CTL, 3}, /* TLMM_HDRV_SDC2_CMD */
64 {SDC2_HDRV_PULL_CTL, 0}, /* TLMM_HDRV_SDC2_DATA */
Sathish Ambleyd2ad0fa2012-03-23 11:23:47 -070065 {SDC1_HDRV_PULL_CTL, 6}, /* TLMM_HDRV_SDC1_CLK */
66 {SDC1_HDRV_PULL_CTL, 3}, /* TLMM_HDRV_SDC1_CMD */
67 {SDC1_HDRV_PULL_CTL, 0}, /* TLMM_HDRV_SDC1_DATA */
68};
69
70static const struct tlmm_field_cfg tlmm_pull_cfgs[] = {
Sujit Reddy Thumma39306c22012-06-26 15:39:26 +053071 {SDC4_HDRV_PULL_CTL, 14}, /* TLMM_PULL_SDC4_CLK */
Sathish Ambleyd2ad0fa2012-03-23 11:23:47 -070072 {SDC4_HDRV_PULL_CTL, 11}, /* TLMM_PULL_SDC4_CMD */
73 {SDC4_HDRV_PULL_CTL, 9}, /* TLMM_PULL_SDC4_DATA */
74 {SDC3_HDRV_PULL_CTL, 14}, /* TLMM_PULL_SDC3_CLK */
75 {SDC3_HDRV_PULL_CTL, 11}, /* TLMM_PULL_SDC3_CMD */
76 {SDC3_HDRV_PULL_CTL, 9}, /* TLMM_PULL_SDC3_DATA */
Sujit Reddy Thumma39306c22012-06-26 15:39:26 +053077 {SDC2_HDRV_PULL_CTL, 14}, /* TLMM_PULL_SDC2_CLK */
78 {SDC2_HDRV_PULL_CTL, 11}, /* TLMM_PULL_SDC2_CMD */
79 {SDC2_HDRV_PULL_CTL, 9}, /* TLMM_PULL_SDC2_DATA */
Sathish Ambleyd2ad0fa2012-03-23 11:23:47 -070080 {SDC1_HDRV_PULL_CTL, 13}, /* TLMM_PULL_SDC1_CLK */
81 {SDC1_HDRV_PULL_CTL, 11}, /* TLMM_PULL_SDC1_CMD */
82 {SDC1_HDRV_PULL_CTL, 9}, /* TLMM_PULL_SDC1_DATA */
83};
84
85/*
86 * Supported arch specific irq extension.
87 * Default make them NULL.
88 */
89struct irq_chip msm_gpio_irq_extn = {
90 .irq_eoi = NULL,
91 .irq_mask = NULL,
92 .irq_unmask = NULL,
93 .irq_retrigger = NULL,
94 .irq_set_type = NULL,
95 .irq_set_wake = NULL,
96 .irq_disable = NULL,
97};
98
99/**
100 * struct msm_gpio_dev: the MSM8660 SoC GPIO device structure
101 *
102 * @enabled_irqs: a bitmap used to optimize the summary-irq handler. By
103 * keeping track of which gpios are unmasked as irq sources, we avoid
104 * having to do __raw_readl calls on hundreds of iomapped registers each time
105 * the summary interrupt fires in order to locate the active interrupts.
106 *
107 * @wake_irqs: a bitmap for tracking which interrupt lines are enabled
108 * as wakeup sources. When the device is suspended, interrupts which are
109 * not wakeup sources are disabled.
110 *
111 * @dual_edge_irqs: a bitmap used to track which irqs are configured
112 * as dual-edge, as this is not supported by the hardware and requires
113 * some special handling in the driver.
114 */
115struct msm_gpio_dev {
116 struct gpio_chip gpio_chip;
117 DECLARE_BITMAP(enabled_irqs, NR_MSM_GPIOS);
118 DECLARE_BITMAP(wake_irqs, NR_MSM_GPIOS);
119 DECLARE_BITMAP(dual_edge_irqs, NR_MSM_GPIOS);
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700120 struct irq_domain *domain;
Sathish Ambleyd2ad0fa2012-03-23 11:23:47 -0700121};
122
123static DEFINE_SPINLOCK(tlmm_lock);
124
125static inline struct msm_gpio_dev *to_msm_gpio_dev(struct gpio_chip *chip)
126{
127 return container_of(chip, struct msm_gpio_dev, gpio_chip);
128}
129
130static int msm_gpio_get(struct gpio_chip *chip, unsigned offset)
131{
132 int rc;
133 rc = __msm_gpio_get_inout(offset);
134 mb();
135 return rc;
136}
137
138static void msm_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
139{
140 __msm_gpio_set_inout(offset, val);
141 mb();
142}
143
144static int msm_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
145{
146 unsigned long irq_flags;
147
148 spin_lock_irqsave(&tlmm_lock, irq_flags);
149 __msm_gpio_set_config_direction(offset, 1, 0);
150 mb();
151 spin_unlock_irqrestore(&tlmm_lock, irq_flags);
152 return 0;
153}
154
155static int msm_gpio_direction_output(struct gpio_chip *chip,
156 unsigned offset,
157 int val)
158{
159 unsigned long irq_flags;
160
161 spin_lock_irqsave(&tlmm_lock, irq_flags);
162 __msm_gpio_set_config_direction(offset, 0, val);
163 mb();
164 spin_unlock_irqrestore(&tlmm_lock, irq_flags);
165 return 0;
166}
167
168#ifdef CONFIG_OF
169static int msm_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
170{
171 struct msm_gpio_dev *g_dev = to_msm_gpio_dev(chip);
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700172 struct irq_domain *domain = g_dev->domain;
Michael Bohan27e7b942012-07-06 10:25:30 -0700173 return irq_linear_revmap(domain, offset);
Sathish Ambleyd2ad0fa2012-03-23 11:23:47 -0700174}
175
176static inline int msm_irq_to_gpio(struct gpio_chip *chip, unsigned irq)
177{
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700178 struct irq_data *irq_data = irq_get_irq_data(irq);
179 return irq_data->hwirq;
Sathish Ambleyd2ad0fa2012-03-23 11:23:47 -0700180}
181#else
182static int msm_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
183{
184 return MSM_GPIO_TO_INT(offset - chip->base);
185}
186
187static inline int msm_irq_to_gpio(struct gpio_chip *chip, unsigned irq)
188{
189 return irq - MSM_GPIO_TO_INT(chip->base);
190}
191#endif
192
193static int msm_gpio_request(struct gpio_chip *chip, unsigned offset)
194{
195 return msm_gpiomux_get(chip->base + offset);
196}
197
198static void msm_gpio_free(struct gpio_chip *chip, unsigned offset)
199{
200 msm_gpiomux_put(chip->base + offset);
201}
202
203static struct msm_gpio_dev msm_gpio = {
204 .gpio_chip = {
205 .label = "msmgpio",
206 .base = 0,
207 .ngpio = NR_MSM_GPIOS,
208 .direction_input = msm_gpio_direction_input,
209 .direction_output = msm_gpio_direction_output,
210 .get = msm_gpio_get,
211 .set = msm_gpio_set,
212 .to_irq = msm_gpio_to_irq,
213 .request = msm_gpio_request,
214 .free = msm_gpio_free,
215 },
216};
217
218static void switch_mpm_config(struct irq_data *d, unsigned val)
219{
220 /* switch the configuration in the mpm as well */
221 if (!msm_gpio_irq_extn.irq_set_type)
222 return;
223
224 if (val)
225 msm_gpio_irq_extn.irq_set_type(d, IRQF_TRIGGER_FALLING);
226 else
227 msm_gpio_irq_extn.irq_set_type(d, IRQF_TRIGGER_RISING);
228}
229
230/* For dual-edge interrupts in software, since the hardware has no
231 * such support:
232 *
233 * At appropriate moments, this function may be called to flip the polarity
234 * settings of both-edge irq lines to try and catch the next edge.
235 *
236 * The attempt is considered successful if:
237 * - the status bit goes high, indicating that an edge was caught, or
238 * - the input value of the gpio doesn't change during the attempt.
239 * If the value changes twice during the process, that would cause the first
240 * test to fail but would force the second, as two opposite
241 * transitions would cause a detection no matter the polarity setting.
242 *
243 * The do-loop tries to sledge-hammer closed the timing hole between
244 * the initial value-read and the polarity-write - if the line value changes
245 * during that window, an interrupt is lost, the new polarity setting is
246 * incorrect, and the first success test will fail, causing a retry.
247 *
248 * Algorithm comes from Google's msmgpio driver, see mach-msm/gpio.c.
249 */
250static void msm_gpio_update_dual_edge_pos(struct irq_data *d, unsigned gpio)
251{
252 int loop_limit = 100;
253 unsigned val, val2, intstat;
254
255 do {
256 val = __msm_gpio_get_inout(gpio);
257 __msm_gpio_set_polarity(gpio, val);
258 val2 = __msm_gpio_get_inout(gpio);
259 intstat = __msm_gpio_get_intr_status(gpio);
260 if (intstat || val == val2) {
261 switch_mpm_config(d, val);
262 return;
263 }
264 } while (loop_limit-- > 0);
265 pr_err("%s: dual-edge irq failed to stabilize, %#08x != %#08x\n",
266 __func__, val, val2);
267}
268
269static void msm_gpio_irq_ack(struct irq_data *d)
270{
271 int gpio = msm_irq_to_gpio(&msm_gpio.gpio_chip, d->irq);
272
273 __msm_gpio_set_intr_status(gpio);
274 if (test_bit(gpio, msm_gpio.dual_edge_irqs))
275 msm_gpio_update_dual_edge_pos(d, gpio);
276 mb();
277}
278
279static void msm_gpio_irq_mask(struct irq_data *d)
280{
281 int gpio = msm_irq_to_gpio(&msm_gpio.gpio_chip, d->irq);
282 unsigned long irq_flags;
283
284 spin_lock_irqsave(&tlmm_lock, irq_flags);
285 __msm_gpio_set_intr_cfg_enable(gpio, 0);
286 __clear_bit(gpio, msm_gpio.enabled_irqs);
287 mb();
288 spin_unlock_irqrestore(&tlmm_lock, irq_flags);
289
290 if (msm_gpio_irq_extn.irq_mask)
291 msm_gpio_irq_extn.irq_mask(d);
292
293}
294
295static void msm_gpio_irq_unmask(struct irq_data *d)
296{
297 int gpio = msm_irq_to_gpio(&msm_gpio.gpio_chip, d->irq);
298 unsigned long irq_flags;
299
300 spin_lock_irqsave(&tlmm_lock, irq_flags);
301 __set_bit(gpio, msm_gpio.enabled_irqs);
302 __msm_gpio_set_intr_cfg_enable(gpio, 1);
303 mb();
304 spin_unlock_irqrestore(&tlmm_lock, irq_flags);
305
306 if (msm_gpio_irq_extn.irq_mask)
307 msm_gpio_irq_extn.irq_unmask(d);
308}
309
310static void msm_gpio_irq_disable(struct irq_data *d)
311{
312 if (msm_gpio_irq_extn.irq_disable)
313 msm_gpio_irq_extn.irq_disable(d);
314}
315
316static int msm_gpio_irq_set_type(struct irq_data *d, unsigned int flow_type)
317{
318 int gpio = msm_irq_to_gpio(&msm_gpio.gpio_chip, d->irq);
319 unsigned long irq_flags;
320
321 spin_lock_irqsave(&tlmm_lock, irq_flags);
322
323 if (flow_type & IRQ_TYPE_EDGE_BOTH) {
324 __irq_set_handler_locked(d->irq, handle_edge_irq);
325 if ((flow_type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH)
326 __set_bit(gpio, msm_gpio.dual_edge_irqs);
327 else
328 __clear_bit(gpio, msm_gpio.dual_edge_irqs);
329 } else {
330 __irq_set_handler_locked(d->irq, handle_level_irq);
331 __clear_bit(gpio, msm_gpio.dual_edge_irqs);
332 }
333
334 __msm_gpio_set_intr_cfg_type(gpio, flow_type);
335
336 if ((flow_type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH)
337 msm_gpio_update_dual_edge_pos(d, gpio);
338
339 mb();
340 spin_unlock_irqrestore(&tlmm_lock, irq_flags);
341
342 if (msm_gpio_irq_extn.irq_set_type)
343 msm_gpio_irq_extn.irq_set_type(d, flow_type);
344
345 return 0;
346}
347
348/*
349 * When the summary IRQ is raised, any number of GPIO lines may be high.
350 * It is the job of the summary handler to find all those GPIO lines
351 * which have been set as summary IRQ lines and which are triggered,
352 * and to call their interrupt handlers.
353 */
354static irqreturn_t msm_summary_irq_handler(int irq, void *data)
355{
356 unsigned long i;
357 struct irq_desc *desc = irq_to_desc(irq);
358 struct irq_chip *chip = irq_desc_get_chip(desc);
359
360 chained_irq_enter(chip, desc);
361
362 for (i = find_first_bit(msm_gpio.enabled_irqs, NR_MSM_GPIOS);
363 i < NR_MSM_GPIOS;
364 i = find_next_bit(msm_gpio.enabled_irqs, NR_MSM_GPIOS, i + 1)) {
365 if (__msm_gpio_get_intr_status(i))
366 generic_handle_irq(msm_gpio_to_irq(&msm_gpio.gpio_chip,
367 i));
368 }
369
370 chained_irq_exit(chip, desc);
371 return IRQ_HANDLED;
372}
373
374static int msm_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
375{
376 int gpio = msm_irq_to_gpio(&msm_gpio.gpio_chip, d->irq);
377
378 if (on) {
379 if (bitmap_empty(msm_gpio.wake_irqs, NR_MSM_GPIOS))
380 irq_set_irq_wake(TLMM_MSM_SUMMARY_IRQ, 1);
381 set_bit(gpio, msm_gpio.wake_irqs);
382 } else {
383 clear_bit(gpio, msm_gpio.wake_irqs);
384 if (bitmap_empty(msm_gpio.wake_irqs, NR_MSM_GPIOS))
385 irq_set_irq_wake(TLMM_MSM_SUMMARY_IRQ, 0);
386 }
387
388 if (msm_gpio_irq_extn.irq_set_wake)
389 msm_gpio_irq_extn.irq_set_wake(d, on);
390
391 return 0;
392}
393
394static struct irq_chip msm_gpio_irq_chip = {
395 .name = "msmgpio",
396 .irq_mask = msm_gpio_irq_mask,
397 .irq_unmask = msm_gpio_irq_unmask,
398 .irq_ack = msm_gpio_irq_ack,
399 .irq_set_type = msm_gpio_irq_set_type,
400 .irq_set_wake = msm_gpio_irq_set_wake,
401 .irq_disable = msm_gpio_irq_disable,
402};
403
404/*
405 * This lock class tells lockdep that GPIO irqs are in a different
406 * category than their parent, so it won't report false recursion.
407 */
408static struct lock_class_key msm_gpio_lock_class;
409
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700410/* TODO: This should be a real platform_driver */
Sathish Ambleyd2ad0fa2012-03-23 11:23:47 -0700411static int __devinit msm_gpio_probe(void)
412{
Michael Bohan27e7b942012-07-06 10:25:30 -0700413 int ret;
414#ifndef CONFIG_OF
415 int irq, i;
416#endif
Sathish Ambleyd2ad0fa2012-03-23 11:23:47 -0700417
418 spin_lock_init(&tlmm_lock);
419 bitmap_zero(msm_gpio.enabled_irqs, NR_MSM_GPIOS);
420 bitmap_zero(msm_gpio.wake_irqs, NR_MSM_GPIOS);
421 bitmap_zero(msm_gpio.dual_edge_irqs, NR_MSM_GPIOS);
422 ret = gpiochip_add(&msm_gpio.gpio_chip);
423 if (ret < 0)
424 return ret;
425
Michael Bohan27e7b942012-07-06 10:25:30 -0700426#ifndef CONFIG_OF
Sathish Ambleyd2ad0fa2012-03-23 11:23:47 -0700427 for (i = 0; i < msm_gpio.gpio_chip.ngpio; ++i) {
428 irq = msm_gpio_to_irq(&msm_gpio.gpio_chip, i);
429 irq_set_lockdep_class(irq, &msm_gpio_lock_class);
430 irq_set_chip_and_handler(irq, &msm_gpio_irq_chip,
431 handle_level_irq);
432 set_irq_flags(irq, IRQF_VALID);
433 }
Michael Bohan27e7b942012-07-06 10:25:30 -0700434#endif
Sathish Ambleyd2ad0fa2012-03-23 11:23:47 -0700435 ret = request_irq(TLMM_MSM_SUMMARY_IRQ, msm_summary_irq_handler,
436 IRQF_TRIGGER_HIGH, "msmgpio", NULL);
437 if (ret) {
438 pr_err("Request_irq failed for TLMM_MSM_SUMMARY_IRQ - %d\n",
439 ret);
440 return ret;
441 }
442 return 0;
443}
444
445static int __devexit msm_gpio_remove(void)
446{
447 int ret = gpiochip_remove(&msm_gpio.gpio_chip);
448
449 if (ret < 0)
450 return ret;
451
452 irq_set_handler(TLMM_MSM_SUMMARY_IRQ, NULL);
453
454 return 0;
455}
456
457#ifdef CONFIG_PM
458static int msm_gpio_suspend(void)
459{
460 unsigned long irq_flags;
461 unsigned long i;
462
463 spin_lock_irqsave(&tlmm_lock, irq_flags);
464 for_each_set_bit(i, msm_gpio.enabled_irqs, NR_MSM_GPIOS)
465 __msm_gpio_set_intr_cfg_enable(i, 0);
466
467 for_each_set_bit(i, msm_gpio.wake_irqs, NR_MSM_GPIOS)
468 __msm_gpio_set_intr_cfg_enable(i, 1);
469 mb();
470 spin_unlock_irqrestore(&tlmm_lock, irq_flags);
471 return 0;
472}
473
474void msm_gpio_show_resume_irq(void)
475{
476 unsigned long irq_flags;
477 int i, irq, intstat;
478
479 if (!msm_show_resume_irq_mask)
480 return;
481
482 spin_lock_irqsave(&tlmm_lock, irq_flags);
483 for_each_set_bit(i, msm_gpio.wake_irqs, NR_MSM_GPIOS) {
484 intstat = __msm_gpio_get_intr_status(i);
485 if (intstat) {
486 irq = msm_gpio_to_irq(&msm_gpio.gpio_chip, i);
487 pr_warning("%s: %d triggered\n",
488 __func__, irq);
489 }
490 }
491 spin_unlock_irqrestore(&tlmm_lock, irq_flags);
492}
493
494static void msm_gpio_resume(void)
495{
496 unsigned long irq_flags;
497 unsigned long i;
498
499 msm_gpio_show_resume_irq();
500
501 spin_lock_irqsave(&tlmm_lock, irq_flags);
502 for_each_set_bit(i, msm_gpio.wake_irqs, NR_MSM_GPIOS)
503 __msm_gpio_set_intr_cfg_enable(i, 0);
504
505 for_each_set_bit(i, msm_gpio.enabled_irqs, NR_MSM_GPIOS)
506 __msm_gpio_set_intr_cfg_enable(i, 1);
507 mb();
508 spin_unlock_irqrestore(&tlmm_lock, irq_flags);
509}
510#else
511#define msm_gpio_suspend NULL
512#define msm_gpio_resume NULL
513#endif
514
515static struct syscore_ops msm_gpio_syscore_ops = {
516 .suspend = msm_gpio_suspend,
517 .resume = msm_gpio_resume,
518};
519
520static int __init msm_gpio_init(void)
521{
522 msm_gpio_probe();
523 register_syscore_ops(&msm_gpio_syscore_ops);
524 return 0;
525}
526
527static void __exit msm_gpio_exit(void)
528{
529 unregister_syscore_ops(&msm_gpio_syscore_ops);
530 msm_gpio_remove();
531}
532
533postcore_initcall(msm_gpio_init);
534module_exit(msm_gpio_exit);
535
536static void msm_tlmm_set_field(const struct tlmm_field_cfg *configs,
537 unsigned id, unsigned width, unsigned val)
538{
539 unsigned long irqflags;
540 u32 mask = (1 << width) - 1;
541 u32 __iomem *reg = MSM_TLMM_BASE + configs[id].reg;
542 u32 reg_val;
543
544 spin_lock_irqsave(&tlmm_lock, irqflags);
545 reg_val = __raw_readl(reg);
546 reg_val &= ~(mask << configs[id].off);
547 reg_val |= (val & mask) << configs[id].off;
548 __raw_writel(reg_val, reg);
549 mb();
550 spin_unlock_irqrestore(&tlmm_lock, irqflags);
551}
552
553void msm_tlmm_set_hdrive(enum msm_tlmm_hdrive_tgt tgt, int drv_str)
554{
555 msm_tlmm_set_field(tlmm_hdrv_cfgs, tgt, 3, drv_str);
556}
557EXPORT_SYMBOL(msm_tlmm_set_hdrive);
558
559void msm_tlmm_set_pull(enum msm_tlmm_pull_tgt tgt, int pull)
560{
561 msm_tlmm_set_field(tlmm_pull_cfgs, tgt, 2, pull);
562}
563EXPORT_SYMBOL(msm_tlmm_set_pull);
564
565int gpio_tlmm_config(unsigned config, unsigned disable)
566{
567 unsigned gpio = GPIO_PIN(config);
568
569 if (gpio > NR_MSM_GPIOS)
570 return -EINVAL;
571
572 __gpio_tlmm_config(config);
573 mb();
574
575 return 0;
576}
577EXPORT_SYMBOL(gpio_tlmm_config);
578
579int msm_gpio_install_direct_irq(unsigned gpio, unsigned irq,
580 unsigned int input_polarity)
581{
582 unsigned long irq_flags;
583
584 if (gpio >= NR_MSM_GPIOS || irq >= NR_TLMM_MSM_DIR_CONN_IRQ)
585 return -EINVAL;
586
587 spin_lock_irqsave(&tlmm_lock, irq_flags);
588 __msm_gpio_install_direct_irq(gpio, irq, input_polarity);
589 mb();
590 spin_unlock_irqrestore(&tlmm_lock, irq_flags);
591
592 return 0;
593}
594EXPORT_SYMBOL(msm_gpio_install_direct_irq);
595
596#ifdef CONFIG_OF
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700597static int msm_gpio_irq_domain_xlate(struct irq_domain *d,
598 struct device_node *controller,
599 const u32 *intspec,
600 unsigned int intsize,
601 unsigned long *out_hwirq,
602 unsigned int *out_type)
Sathish Ambleyd2ad0fa2012-03-23 11:23:47 -0700603{
604 if (d->of_node != controller)
605 return -EINVAL;
606 if (intsize != 2)
607 return -EINVAL;
608
609 /* hwirq value */
610 *out_hwirq = intspec[0];
611
612 /* irq flags */
613 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
614 return 0;
615}
616
Michael Bohan27e7b942012-07-06 10:25:30 -0700617static int msm_gpio_irq_domain_map(struct irq_domain *d, unsigned int irq,
618 irq_hw_number_t hwirq)
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700619{
Michael Bohan27e7b942012-07-06 10:25:30 -0700620 irq_set_lockdep_class(irq, &msm_gpio_lock_class);
621 irq_set_chip_and_handler(irq, &msm_gpio_irq_chip,
622 handle_level_irq);
623 set_irq_flags(irq, IRQF_VALID);
624
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700625 return 0;
626}
627
Sathish Ambleyd2ad0fa2012-03-23 11:23:47 -0700628static struct irq_domain_ops msm_gpio_irq_domain_ops = {
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700629 .xlate = msm_gpio_irq_domain_xlate,
630 .map = msm_gpio_irq_domain_map,
Sathish Ambleyd2ad0fa2012-03-23 11:23:47 -0700631};
632
633int __init msm_gpio_of_init(struct device_node *node,
634 struct device_node *parent)
635{
Michael Bohanbb6b30f2012-06-01 13:33:51 -0700636 msm_gpio.domain = irq_domain_add_linear(node, NR_MSM_GPIOS,
637 &msm_gpio_irq_domain_ops, &msm_gpio);
638 if (!msm_gpio.domain) {
639 WARN(1, "Cannot allocate irq_domain\n");
640 return -ENOMEM;
Sathish Ambleyd2ad0fa2012-03-23 11:23:47 -0700641 }
642
Sathish Ambleyd2ad0fa2012-03-23 11:23:47 -0700643 return 0;
644}
645#endif
646
647MODULE_AUTHOR("Gregory Bean <gbean@codeaurora.org>");
648MODULE_DESCRIPTION("Driver for Qualcomm MSM TLMMv2 SoC GPIOs");
649MODULE_LICENSE("GPL v2");
650MODULE_ALIAS("sysdev:msmgpio");