Duy Truong | 790f06d | 2013-02-13 16:38:12 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2010-2012, The Linux Foundation. All rights reserved. |
Gregory Bean | 0cc2fc1 | 2010-11-24 11:53:51 -0800 | [diff] [blame] | 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 | * |
Gregory Bean | 0cc2fc1 | 2010-11-24 11:53:51 -0800 | [diff] [blame] | 12 | */ |
Gregory Bean | 70cc2c0 | 2010-11-24 11:53:52 -0800 | [diff] [blame] | 13 | #include <linux/bitmap.h> |
| 14 | #include <linux/bitops.h> |
Rohit Vaswani | e50680c | 2012-06-13 18:26:18 +0530 | [diff] [blame] | 15 | #include <linux/delay.h> |
Gregory Bean | 0cc2fc1 | 2010-11-24 11:53:51 -0800 | [diff] [blame] | 16 | #include <linux/gpio.h> |
Gregory Bean | 70cc2c0 | 2010-11-24 11:53:52 -0800 | [diff] [blame] | 17 | #include <linux/init.h> |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 18 | #include <linux/io.h> |
Sathish Ambley | d2ad0fa | 2012-03-23 11:23:47 -0700 | [diff] [blame] | 19 | #include <linux/irq.h> |
Will Deacon | 03dd765 | 2011-02-21 14:54:57 +0000 | [diff] [blame] | 20 | |
Gregory Bean | 0cc2fc1 | 2010-11-24 11:53:51 -0800 | [diff] [blame] | 21 | #include <mach/msm_iomap.h> |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 22 | #include <mach/gpiomux.h> |
Sathish Ambley | d2ad0fa | 2012-03-23 11:23:47 -0700 | [diff] [blame] | 23 | #include "gpio-msm-common.h" |
Gregory Bean | 0cc2fc1 | 2010-11-24 11:53:51 -0800 | [diff] [blame] | 24 | |
| 25 | /* Bits of interest in the GPIO_IN_OUT register. |
| 26 | */ |
| 27 | enum { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 28 | GPIO_IN_BIT = 0, |
| 29 | GPIO_OUT_BIT = 1 |
Gregory Bean | 70cc2c0 | 2010-11-24 11:53:52 -0800 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | /* Bits of interest in the GPIO_INTR_STATUS register. |
| 33 | */ |
| 34 | enum { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 35 | INTR_STATUS_BIT = 0, |
Gregory Bean | 0cc2fc1 | 2010-11-24 11:53:51 -0800 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | /* Bits of interest in the GPIO_CFG register. |
| 39 | */ |
| 40 | enum { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 41 | GPIO_OE_BIT = 9, |
Gregory Bean | 0cc2fc1 | 2010-11-24 11:53:51 -0800 | [diff] [blame] | 42 | }; |
| 43 | |
Gregory Bean | 70cc2c0 | 2010-11-24 11:53:52 -0800 | [diff] [blame] | 44 | /* Bits of interest in the GPIO_INTR_CFG register. |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 45 | */ |
| 46 | enum { |
| 47 | INTR_ENABLE_BIT = 0, |
| 48 | INTR_POL_CTL_BIT = 1, |
| 49 | INTR_DECT_CTL_BIT = 2, |
| 50 | INTR_RAW_STATUS_EN_BIT = 3, |
| 51 | }; |
| 52 | |
| 53 | /* Codes of interest in GPIO_INTR_CFG_SU. |
| 54 | */ |
| 55 | enum { |
| 56 | TARGET_PROC_SCORPION = 4, |
| 57 | TARGET_PROC_NONE = 7, |
| 58 | }; |
| 59 | |
| 60 | /* |
| 61 | * There is no 'DC_POLARITY_LO' because the GIC is incapable |
| 62 | * of asserting on falling edge or level-low conditions. Even though |
| 63 | * the registers allow for low-polarity inputs, the case can never arise. |
| 64 | */ |
| 65 | enum { |
| 66 | DC_POLARITY_HI = BIT(11), |
| 67 | DC_IRQ_ENABLE = BIT(3), |
| 68 | }; |
| 69 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 70 | /* |
Gregory Bean | 70cc2c0 | 2010-11-24 11:53:52 -0800 | [diff] [blame] | 71 | * When a GPIO triggers, two separate decisions are made, controlled |
| 72 | * by two separate flags. |
| 73 | * |
| 74 | * - First, INTR_RAW_STATUS_EN controls whether or not the GPIO_INTR_STATUS |
| 75 | * register for that GPIO will be updated to reflect the triggering of that |
| 76 | * gpio. If this bit is 0, this register will not be updated. |
| 77 | * - Second, INTR_ENABLE controls whether an interrupt is triggered. |
| 78 | * |
| 79 | * If INTR_ENABLE is set and INTR_RAW_STATUS_EN is NOT set, an interrupt |
| 80 | * can be triggered but the status register will not reflect it. |
| 81 | */ |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 82 | #define INTR_RAW_STATUS_EN BIT(INTR_RAW_STATUS_EN_BIT) |
| 83 | #define INTR_ENABLE BIT(INTR_ENABLE_BIT) |
| 84 | #define INTR_DECT_CTL_EDGE BIT(INTR_DECT_CTL_BIT) |
| 85 | #define INTR_POL_CTL_HI BIT(INTR_POL_CTL_BIT) |
Gregory Bean | 70cc2c0 | 2010-11-24 11:53:52 -0800 | [diff] [blame] | 86 | |
| 87 | #define GPIO_INTR_CFG_SU(gpio) (MSM_TLMM_BASE + 0x0400 + (0x04 * (gpio))) |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 88 | #define DIR_CONN_INTR_CFG_SU(irq) (MSM_TLMM_BASE + 0x0700 + (0x04 * (irq))) |
Gregory Bean | 0cc2fc1 | 2010-11-24 11:53:51 -0800 | [diff] [blame] | 89 | #define GPIO_CONFIG(gpio) (MSM_TLMM_BASE + 0x1000 + (0x10 * (gpio))) |
| 90 | #define GPIO_IN_OUT(gpio) (MSM_TLMM_BASE + 0x1004 + (0x10 * (gpio))) |
Gregory Bean | 70cc2c0 | 2010-11-24 11:53:52 -0800 | [diff] [blame] | 91 | #define GPIO_INTR_CFG(gpio) (MSM_TLMM_BASE + 0x1008 + (0x10 * (gpio))) |
| 92 | #define GPIO_INTR_STATUS(gpio) (MSM_TLMM_BASE + 0x100c + (0x10 * (gpio))) |
| 93 | |
Gregory Bean | 70cc2c0 | 2010-11-24 11:53:52 -0800 | [diff] [blame] | 94 | static inline void set_gpio_bits(unsigned n, void __iomem *reg) |
| 95 | { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 96 | __raw_writel(__raw_readl(reg) | n, reg); |
Gregory Bean | 70cc2c0 | 2010-11-24 11:53:52 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 99 | static inline void clr_gpio_bits(unsigned n, void __iomem *reg) |
Gregory Bean | 70cc2c0 | 2010-11-24 11:53:52 -0800 | [diff] [blame] | 100 | { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 101 | __raw_writel(__raw_readl(reg) & ~n, reg); |
Gregory Bean | 70cc2c0 | 2010-11-24 11:53:52 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Sathish Ambley | d2ad0fa | 2012-03-23 11:23:47 -0700 | [diff] [blame] | 104 | unsigned __msm_gpio_get_inout(unsigned gpio) |
Gregory Bean | 0cc2fc1 | 2010-11-24 11:53:51 -0800 | [diff] [blame] | 105 | { |
Sathish Ambley | d2ad0fa | 2012-03-23 11:23:47 -0700 | [diff] [blame] | 106 | return __raw_readl(GPIO_IN_OUT(gpio)) & BIT(GPIO_IN_BIT); |
Gregory Bean | 0cc2fc1 | 2010-11-24 11:53:51 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Sathish Ambley | d2ad0fa | 2012-03-23 11:23:47 -0700 | [diff] [blame] | 109 | void __msm_gpio_set_inout(unsigned gpio, unsigned val) |
Gregory Bean | 0cc2fc1 | 2010-11-24 11:53:51 -0800 | [diff] [blame] | 110 | { |
Sathish Ambley | d2ad0fa | 2012-03-23 11:23:47 -0700 | [diff] [blame] | 111 | __raw_writel(val ? BIT(GPIO_OUT_BIT) : 0, GPIO_IN_OUT(gpio)); |
Gregory Bean | 0cc2fc1 | 2010-11-24 11:53:51 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Sathish Ambley | d2ad0fa | 2012-03-23 11:23:47 -0700 | [diff] [blame] | 114 | void __msm_gpio_set_config_direction(unsigned gpio, int input, int val) |
Gregory Bean | 0cc2fc1 | 2010-11-24 11:53:51 -0800 | [diff] [blame] | 115 | { |
Sathish Ambley | d2ad0fa | 2012-03-23 11:23:47 -0700 | [diff] [blame] | 116 | if (input) |
| 117 | clr_gpio_bits(BIT(GPIO_OE_BIT), GPIO_CONFIG(gpio)); |
| 118 | else { |
| 119 | __msm_gpio_set_inout(gpio, val); |
| 120 | set_gpio_bits(BIT(GPIO_OE_BIT), GPIO_CONFIG(gpio)); |
| 121 | } |
Gregory Bean | 0cc2fc1 | 2010-11-24 11:53:51 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Sathish Ambley | d2ad0fa | 2012-03-23 11:23:47 -0700 | [diff] [blame] | 124 | void __msm_gpio_set_polarity(unsigned gpio, unsigned val) |
Gregory Bean | 0cc2fc1 | 2010-11-24 11:53:51 -0800 | [diff] [blame] | 125 | { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 126 | if (val) |
Sathish Ambley | d2ad0fa | 2012-03-23 11:23:47 -0700 | [diff] [blame] | 127 | clr_gpio_bits(INTR_POL_CTL_HI, GPIO_INTR_CFG(gpio)); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 128 | else |
Sathish Ambley | d2ad0fa | 2012-03-23 11:23:47 -0700 | [diff] [blame] | 129 | set_gpio_bits(INTR_POL_CTL_HI, GPIO_INTR_CFG(gpio)); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Sathish Ambley | d2ad0fa | 2012-03-23 11:23:47 -0700 | [diff] [blame] | 132 | unsigned __msm_gpio_get_intr_status(unsigned gpio) |
Gregory Bean | 70cc2c0 | 2010-11-24 11:53:52 -0800 | [diff] [blame] | 133 | { |
Sathish Ambley | d2ad0fa | 2012-03-23 11:23:47 -0700 | [diff] [blame] | 134 | return __raw_readl(GPIO_INTR_STATUS(gpio)) & |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 135 | BIT(INTR_STATUS_BIT); |
Gregory Bean | 70cc2c0 | 2010-11-24 11:53:52 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Sathish Ambley | d2ad0fa | 2012-03-23 11:23:47 -0700 | [diff] [blame] | 138 | void __msm_gpio_set_intr_status(unsigned gpio) |
Gregory Bean | 70cc2c0 | 2010-11-24 11:53:52 -0800 | [diff] [blame] | 139 | { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 140 | __raw_writel(BIT(INTR_STATUS_BIT), GPIO_INTR_STATUS(gpio)); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Sathish Ambley | d2ad0fa | 2012-03-23 11:23:47 -0700 | [diff] [blame] | 143 | unsigned __msm_gpio_get_intr_config(unsigned gpio) |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 144 | { |
Sathish Ambley | d2ad0fa | 2012-03-23 11:23:47 -0700 | [diff] [blame] | 145 | return __raw_readl(GPIO_INTR_CFG(gpio)); |
Gregory Bean | 70cc2c0 | 2010-11-24 11:53:52 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Sathish Ambley | d2ad0fa | 2012-03-23 11:23:47 -0700 | [diff] [blame] | 148 | void __msm_gpio_set_intr_cfg_enable(unsigned gpio, unsigned val) |
Gregory Bean | 70cc2c0 | 2010-11-24 11:53:52 -0800 | [diff] [blame] | 149 | { |
Sathish Ambley | d2ad0fa | 2012-03-23 11:23:47 -0700 | [diff] [blame] | 150 | if (val) { |
Rohit Vaswani | e50680c | 2012-06-13 18:26:18 +0530 | [diff] [blame] | 151 | set_gpio_bits(INTR_ENABLE, GPIO_INTR_CFG(gpio)); |
Gregory Bean | 70cc2c0 | 2010-11-24 11:53:52 -0800 | [diff] [blame] | 152 | |
Gregory Bean | 70cc2c0 | 2010-11-24 11:53:52 -0800 | [diff] [blame] | 153 | } else { |
Rohit Vaswani | e50680c | 2012-06-13 18:26:18 +0530 | [diff] [blame] | 154 | clr_gpio_bits(INTR_ENABLE, GPIO_INTR_CFG(gpio)); |
Gregory Bean | 70cc2c0 | 2010-11-24 11:53:52 -0800 | [diff] [blame] | 155 | } |
Sathish Ambley | d2ad0fa | 2012-03-23 11:23:47 -0700 | [diff] [blame] | 156 | } |
Gregory Bean | 70cc2c0 | 2010-11-24 11:53:52 -0800 | [diff] [blame] | 157 | |
Rohit Vaswani | f4f3658 | 2012-08-28 11:49:11 -0700 | [diff] [blame] | 158 | unsigned __msm_gpio_get_intr_cfg_enable(unsigned gpio) |
| 159 | { |
| 160 | return __msm_gpio_get_intr_config(gpio) & INTR_ENABLE; |
| 161 | } |
| 162 | |
Sathish Ambley | d2ad0fa | 2012-03-23 11:23:47 -0700 | [diff] [blame] | 163 | void __msm_gpio_set_intr_cfg_type(unsigned gpio, unsigned type) |
| 164 | { |
| 165 | unsigned cfg; |
| 166 | |
Rohit Vaswani | ea45a49 | 2012-08-03 11:47:42 -0700 | [diff] [blame] | 167 | /* RAW_STATUS_EN is left on for all gpio irqs. Due to the |
| 168 | * internal circuitry of TLMM, toggling the RAW_STATUS |
| 169 | * could cause the INTR_STATUS to be set for EDGE interrupts. |
| 170 | */ |
| 171 | cfg = __msm_gpio_get_intr_config(gpio); |
| 172 | cfg |= INTR_RAW_STATUS_EN; |
| 173 | __raw_writel(cfg, GPIO_INTR_CFG(gpio)); |
| 174 | __raw_writel(TARGET_PROC_SCORPION, GPIO_INTR_CFG_SU(gpio)); |
| 175 | |
Sathish Ambley | d2ad0fa | 2012-03-23 11:23:47 -0700 | [diff] [blame] | 176 | cfg = __msm_gpio_get_intr_config(gpio); |
| 177 | if (type & IRQ_TYPE_EDGE_BOTH) |
| 178 | cfg |= INTR_DECT_CTL_EDGE; |
Gregory Bean | 70cc2c0 | 2010-11-24 11:53:52 -0800 | [diff] [blame] | 179 | else |
Sathish Ambley | d2ad0fa | 2012-03-23 11:23:47 -0700 | [diff] [blame] | 180 | cfg &= ~INTR_DECT_CTL_EDGE; |
Gregory Bean | 70cc2c0 | 2010-11-24 11:53:52 -0800 | [diff] [blame] | 181 | |
Sathish Ambley | d2ad0fa | 2012-03-23 11:23:47 -0700 | [diff] [blame] | 182 | if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_LEVEL_HIGH)) |
| 183 | cfg |= INTR_POL_CTL_HI; |
| 184 | else |
| 185 | cfg &= ~INTR_POL_CTL_HI; |
Gregory Bean | 70cc2c0 | 2010-11-24 11:53:52 -0800 | [diff] [blame] | 186 | |
Sathish Ambley | d2ad0fa | 2012-03-23 11:23:47 -0700 | [diff] [blame] | 187 | __raw_writel(cfg, GPIO_INTR_CFG(gpio)); |
Rohit Vaswani | e50680c | 2012-06-13 18:26:18 +0530 | [diff] [blame] | 188 | /* Sometimes it might take a little while to update |
| 189 | * the interrupt status after the RAW_STATUS is enabled |
Rohit Vaswani | ea45a49 | 2012-08-03 11:47:42 -0700 | [diff] [blame] | 190 | * We clear the interrupt status before enabling the |
| 191 | * interrupt in the unmask call-back. |
Rohit Vaswani | e50680c | 2012-06-13 18:26:18 +0530 | [diff] [blame] | 192 | */ |
| 193 | udelay(5); |
Gregory Bean | 70cc2c0 | 2010-11-24 11:53:52 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Sathish Ambley | d2ad0fa | 2012-03-23 11:23:47 -0700 | [diff] [blame] | 196 | void __gpio_tlmm_config(unsigned config) |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 197 | { |
| 198 | uint32_t flags; |
| 199 | unsigned gpio = GPIO_PIN(config); |
| 200 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 201 | flags = ((GPIO_DIR(config) << 9) & (0x1 << 9)) | |
| 202 | ((GPIO_DRVSTR(config) << 6) & (0x7 << 6)) | |
| 203 | ((GPIO_FUNC(config) << 2) & (0xf << 2)) | |
| 204 | ((GPIO_PULL(config) & 0x3)); |
| 205 | __raw_writel(flags, GPIO_CONFIG(gpio)); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 206 | } |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 207 | |
Sathish Ambley | d2ad0fa | 2012-03-23 11:23:47 -0700 | [diff] [blame] | 208 | void __msm_gpio_install_direct_irq(unsigned gpio, unsigned irq, |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 209 | unsigned int input_polarity) |
| 210 | { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 211 | uint32_t bits; |
| 212 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 213 | __raw_writel(__raw_readl(GPIO_CONFIG(gpio)) | BIT(GPIO_OE_BIT), |
| 214 | GPIO_CONFIG(gpio)); |
| 215 | __raw_writel(__raw_readl(GPIO_INTR_CFG(gpio)) & |
| 216 | ~(INTR_RAW_STATUS_EN | INTR_ENABLE), |
| 217 | GPIO_INTR_CFG(gpio)); |
| 218 | __raw_writel(DC_IRQ_ENABLE | TARGET_PROC_NONE, |
| 219 | GPIO_INTR_CFG_SU(gpio)); |
| 220 | |
| 221 | bits = TARGET_PROC_SCORPION | (gpio << 3); |
| 222 | if (input_polarity) |
| 223 | bits |= DC_POLARITY_HI; |
| 224 | __raw_writel(bits, DIR_CONN_INTR_CFG_SU(irq)); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 225 | } |