Laurence Withers | f517afd | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 1 | /* |
| 2 | * GPIO driver for TI DaVinci DA8xx SOCs. |
| 3 | * |
| 4 | * (C) Copyright 2011 Guralp Systems Ltd. |
| 5 | * Laurence Withers <lwithers@guralp.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License as |
| 9 | * published by the Free Software Foundation; either version 2 of |
| 10 | * the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 20 | * MA 02111-1307 USA |
| 21 | */ |
| 22 | |
| 23 | #include <common.h> |
| 24 | #include <asm/io.h> |
| 25 | #include <asm/gpio.h> |
Laurence Withers | f517afd | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 26 | #include <asm/arch/hardware.h> |
| 27 | #include <asm/arch/davinci_misc.h> |
| 28 | |
Laurence Withers | f517afd | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 29 | static struct gpio_registry { |
| 30 | int is_registered; |
| 31 | char name[GPIO_NAME_SIZE]; |
| 32 | } gpio_registry[MAX_NUM_GPIOS]; |
| 33 | |
Holger Hans Peter Freyther | 03414ac | 2013-02-07 23:41:01 +0000 | [diff] [blame] | 34 | #if defined(CONFIG_SOC_DA8XX) |
Laurence Withers | f517afd | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 35 | #define pinmux(x) (&davinci_syscfg_regs->pinmux[x]) |
| 36 | |
| 37 | static const struct pinmux_config gpio_pinmux[] = { |
| 38 | { pinmux(1), 8, 7 }, /* GP0[0] */ |
| 39 | { pinmux(1), 8, 6 }, |
| 40 | { pinmux(1), 8, 5 }, |
| 41 | { pinmux(1), 8, 4 }, |
| 42 | { pinmux(1), 8, 3 }, |
| 43 | { pinmux(1), 8, 2 }, |
| 44 | { pinmux(1), 8, 1 }, |
| 45 | { pinmux(1), 8, 0 }, |
| 46 | { pinmux(0), 8, 7 }, |
| 47 | { pinmux(0), 8, 6 }, |
| 48 | { pinmux(0), 8, 5 }, |
| 49 | { pinmux(0), 8, 4 }, |
| 50 | { pinmux(0), 8, 3 }, |
| 51 | { pinmux(0), 8, 2 }, |
| 52 | { pinmux(0), 8, 1 }, |
| 53 | { pinmux(0), 8, 0 }, |
| 54 | { pinmux(4), 8, 7 }, /* GP1[0] */ |
| 55 | { pinmux(4), 8, 6 }, |
| 56 | { pinmux(4), 8, 5 }, |
| 57 | { pinmux(4), 8, 4 }, |
| 58 | { pinmux(4), 8, 3 }, |
| 59 | { pinmux(4), 8, 2 }, |
| 60 | { pinmux(4), 4, 1 }, |
| 61 | { pinmux(4), 4, 0 }, |
| 62 | { pinmux(3), 4, 0 }, |
| 63 | { pinmux(2), 4, 6 }, |
| 64 | { pinmux(2), 4, 5 }, |
| 65 | { pinmux(2), 4, 4 }, |
| 66 | { pinmux(2), 4, 3 }, |
| 67 | { pinmux(2), 4, 2 }, |
| 68 | { pinmux(2), 4, 1 }, |
| 69 | { pinmux(2), 8, 0 }, |
| 70 | { pinmux(6), 8, 7 }, /* GP2[0] */ |
| 71 | { pinmux(6), 8, 6 }, |
| 72 | { pinmux(6), 8, 5 }, |
| 73 | { pinmux(6), 8, 4 }, |
| 74 | { pinmux(6), 8, 3 }, |
| 75 | { pinmux(6), 8, 2 }, |
| 76 | { pinmux(6), 8, 1 }, |
| 77 | { pinmux(6), 8, 0 }, |
| 78 | { pinmux(5), 8, 7 }, |
| 79 | { pinmux(5), 8, 6 }, |
| 80 | { pinmux(5), 8, 5 }, |
| 81 | { pinmux(5), 8, 4 }, |
| 82 | { pinmux(5), 8, 3 }, |
| 83 | { pinmux(5), 8, 2 }, |
| 84 | { pinmux(5), 8, 1 }, |
| 85 | { pinmux(5), 8, 0 }, |
| 86 | { pinmux(8), 8, 7 }, /* GP3[0] */ |
| 87 | { pinmux(8), 8, 6 }, |
| 88 | { pinmux(8), 8, 5 }, |
| 89 | { pinmux(8), 8, 4 }, |
| 90 | { pinmux(8), 8, 3 }, |
| 91 | { pinmux(8), 8, 2 }, |
| 92 | { pinmux(8), 8, 1 }, |
| 93 | { pinmux(8), 8, 0 }, |
| 94 | { pinmux(7), 8, 7 }, |
| 95 | { pinmux(7), 8, 6 }, |
| 96 | { pinmux(7), 8, 5 }, |
| 97 | { pinmux(7), 8, 4 }, |
| 98 | { pinmux(7), 8, 3 }, |
| 99 | { pinmux(7), 8, 2 }, |
| 100 | { pinmux(7), 8, 1 }, |
| 101 | { pinmux(7), 8, 0 }, |
| 102 | { pinmux(10), 8, 7 }, /* GP4[0] */ |
| 103 | { pinmux(10), 8, 6 }, |
| 104 | { pinmux(10), 8, 5 }, |
| 105 | { pinmux(10), 8, 4 }, |
| 106 | { pinmux(10), 8, 3 }, |
| 107 | { pinmux(10), 8, 2 }, |
| 108 | { pinmux(10), 8, 1 }, |
| 109 | { pinmux(10), 8, 0 }, |
| 110 | { pinmux(9), 8, 7 }, |
| 111 | { pinmux(9), 8, 6 }, |
| 112 | { pinmux(9), 8, 5 }, |
| 113 | { pinmux(9), 8, 4 }, |
| 114 | { pinmux(9), 8, 3 }, |
| 115 | { pinmux(9), 8, 2 }, |
| 116 | { pinmux(9), 8, 1 }, |
| 117 | { pinmux(9), 8, 0 }, |
| 118 | { pinmux(12), 8, 7 }, /* GP5[0] */ |
| 119 | { pinmux(12), 8, 6 }, |
| 120 | { pinmux(12), 8, 5 }, |
| 121 | { pinmux(12), 8, 4 }, |
| 122 | { pinmux(12), 8, 3 }, |
| 123 | { pinmux(12), 8, 2 }, |
| 124 | { pinmux(12), 8, 1 }, |
| 125 | { pinmux(12), 8, 0 }, |
| 126 | { pinmux(11), 8, 7 }, |
| 127 | { pinmux(11), 8, 6 }, |
| 128 | { pinmux(11), 8, 5 }, |
| 129 | { pinmux(11), 8, 4 }, |
| 130 | { pinmux(11), 8, 3 }, |
| 131 | { pinmux(11), 8, 2 }, |
| 132 | { pinmux(11), 8, 1 }, |
| 133 | { pinmux(11), 8, 0 }, |
| 134 | { pinmux(19), 8, 6 }, /* GP6[0] */ |
| 135 | { pinmux(19), 8, 5 }, |
| 136 | { pinmux(19), 8, 4 }, |
| 137 | { pinmux(19), 8, 3 }, |
| 138 | { pinmux(19), 8, 2 }, |
| 139 | { pinmux(16), 8, 1 }, |
| 140 | { pinmux(14), 8, 1 }, |
| 141 | { pinmux(14), 8, 0 }, |
| 142 | { pinmux(13), 8, 7 }, |
| 143 | { pinmux(13), 8, 6 }, |
| 144 | { pinmux(13), 8, 5 }, |
| 145 | { pinmux(13), 8, 4 }, |
| 146 | { pinmux(13), 8, 3 }, |
| 147 | { pinmux(13), 8, 2 }, |
| 148 | { pinmux(13), 8, 1 }, |
| 149 | { pinmux(13), 8, 0 }, |
| 150 | { pinmux(18), 8, 1 }, /* GP7[0] */ |
| 151 | { pinmux(18), 8, 0 }, |
| 152 | { pinmux(17), 8, 7 }, |
| 153 | { pinmux(17), 8, 6 }, |
| 154 | { pinmux(17), 8, 5 }, |
| 155 | { pinmux(17), 8, 4 }, |
| 156 | { pinmux(17), 8, 3 }, |
| 157 | { pinmux(17), 8, 2 }, |
| 158 | { pinmux(17), 8, 1 }, |
| 159 | { pinmux(17), 8, 0 }, |
| 160 | { pinmux(16), 8, 7 }, |
| 161 | { pinmux(16), 8, 6 }, |
| 162 | { pinmux(16), 8, 5 }, |
| 163 | { pinmux(16), 8, 4 }, |
| 164 | { pinmux(16), 8, 3 }, |
| 165 | { pinmux(16), 8, 2 }, |
| 166 | { pinmux(19), 8, 0 }, /* GP8[0] */ |
| 167 | { pinmux(3), 4, 7 }, |
| 168 | { pinmux(3), 4, 6 }, |
| 169 | { pinmux(3), 4, 5 }, |
| 170 | { pinmux(3), 4, 4 }, |
| 171 | { pinmux(3), 4, 3 }, |
| 172 | { pinmux(3), 4, 2 }, |
| 173 | { pinmux(2), 4, 7 }, |
| 174 | { pinmux(19), 8, 1 }, |
| 175 | { pinmux(19), 8, 0 }, |
| 176 | { pinmux(18), 8, 7 }, |
| 177 | { pinmux(18), 8, 6 }, |
| 178 | { pinmux(18), 8, 5 }, |
| 179 | { pinmux(18), 8, 4 }, |
| 180 | { pinmux(18), 8, 3 }, |
| 181 | { pinmux(18), 8, 2 }, |
| 182 | }; |
Holger Hans Peter Freyther | 03414ac | 2013-02-07 23:41:01 +0000 | [diff] [blame] | 183 | #else |
| 184 | #define davinci_configure_pin_mux(a, b) |
| 185 | #endif |
Laurence Withers | f517afd | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 186 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 187 | int gpio_request(unsigned gpio, const char *label) |
Laurence Withers | f517afd | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 188 | { |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 189 | if (gpio >= MAX_NUM_GPIOS) |
Laurence Withers | f517afd | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 190 | return -1; |
| 191 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 192 | if (gpio_registry[gpio].is_registered) |
Laurence Withers | f517afd | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 193 | return -1; |
| 194 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 195 | gpio_registry[gpio].is_registered = 1; |
| 196 | strncpy(gpio_registry[gpio].name, label, GPIO_NAME_SIZE); |
| 197 | gpio_registry[gpio].name[GPIO_NAME_SIZE - 1] = 0; |
Laurence Withers | f517afd | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 198 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 199 | davinci_configure_pin_mux(&gpio_pinmux[gpio], 1); |
Laurence Withers | f517afd | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 204 | int gpio_free(unsigned gpio) |
Laurence Withers | f517afd | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 205 | { |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 206 | if (gpio >= MAX_NUM_GPIOS) |
| 207 | return -1; |
| 208 | |
| 209 | if (!gpio_registry[gpio].is_registered) |
| 210 | return -1; |
| 211 | |
| 212 | gpio_registry[gpio].is_registered = 0; |
| 213 | gpio_registry[gpio].name[0] = '\0'; |
| 214 | /* Do not configure as input or change pin mux here */ |
| 215 | return 0; |
Laurence Withers | f517afd | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 216 | } |
| 217 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 218 | int gpio_direction_input(unsigned gpio) |
Laurence Withers | f517afd | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 219 | { |
| 220 | struct davinci_gpio *bank; |
| 221 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 222 | bank = GPIO_BANK(gpio); |
| 223 | setbits_le32(&bank->dir, 1U << GPIO_BIT(gpio)); |
Laurence Withers | f517afd | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 224 | return 0; |
| 225 | } |
| 226 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 227 | int gpio_direction_output(unsigned gpio, int value) |
Laurence Withers | f517afd | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 228 | { |
| 229 | struct davinci_gpio *bank; |
| 230 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 231 | bank = GPIO_BANK(gpio); |
| 232 | clrbits_le32(&bank->dir, 1U << GPIO_BIT(gpio)); |
| 233 | gpio_set_value(gpio, value); |
Laurence Withers | f517afd | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 234 | return 0; |
| 235 | } |
| 236 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 237 | int gpio_get_value(unsigned gpio) |
Laurence Withers | f517afd | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 238 | { |
| 239 | struct davinci_gpio *bank; |
| 240 | unsigned int ip; |
| 241 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 242 | bank = GPIO_BANK(gpio); |
| 243 | ip = in_le32(&bank->in_data) & (1U << GPIO_BIT(gpio)); |
Laurence Withers | f517afd | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 244 | return ip ? 1 : 0; |
| 245 | } |
| 246 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 247 | int gpio_set_value(unsigned gpio, int value) |
Laurence Withers | f517afd | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 248 | { |
| 249 | struct davinci_gpio *bank; |
| 250 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 251 | bank = GPIO_BANK(gpio); |
Laurence Withers | f517afd | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 252 | |
| 253 | if (value) |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 254 | bank->set_data = 1U << GPIO_BIT(gpio); |
Laurence Withers | f517afd | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 255 | else |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 256 | bank->clr_data = 1U << GPIO_BIT(gpio); |
| 257 | |
| 258 | return 0; |
Laurence Withers | f517afd | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 259 | } |
| 260 | |
Laurence Withers | f517afd | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 261 | void gpio_info(void) |
| 262 | { |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 263 | unsigned gpio, dir, val; |
Laurence Withers | f517afd | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 264 | struct davinci_gpio *bank; |
| 265 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 266 | for (gpio = 0; gpio < MAX_NUM_GPIOS; ++gpio) { |
| 267 | bank = GPIO_BANK(gpio); |
| 268 | dir = in_le32(&bank->dir) & (1U << GPIO_BIT(gpio)); |
| 269 | val = gpio_get_value(gpio); |
Laurence Withers | f517afd | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 270 | |
| 271 | printf("% 4d: %s: %d [%c] %s\n", |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 272 | gpio, dir ? " in" : "out", val, |
| 273 | gpio_registry[gpio].is_registered ? 'x' : ' ', |
| 274 | gpio_registry[gpio].name); |
Laurence Withers | f517afd | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 275 | } |
| 276 | } |