Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 2 | /* |
| 3 | * GPIO driver for TI DaVinci DA8xx SOCs. |
| 4 | * |
| 5 | * (C) Copyright 2011 Guralp Systems Ltd. |
| 6 | * Laurence Withers <lwithers@guralp.com> |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 10 | #include <dm.h> |
| 11 | #include <fdtdec.h> |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 12 | #include <asm/io.h> |
| 13 | #include <asm/gpio.h> |
Adam Ford | a112939 | 2018-08-16 23:13:34 -0500 | [diff] [blame] | 14 | #include <dt-bindings/gpio/gpio.h> |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 15 | |
Keerthy | 6d9e7a8 | 2018-10-03 17:55:14 +0530 | [diff] [blame] | 16 | #include "da8xx_gpio.h" |
| 17 | |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 18 | #ifndef CONFIG_DM_GPIO |
Keerthy | 6d9e7a8 | 2018-10-03 17:55:14 +0530 | [diff] [blame] | 19 | #include <asm/arch/hardware.h> |
| 20 | #include <asm/arch/davinci_misc.h> |
| 21 | |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 22 | static struct gpio_registry { |
| 23 | int is_registered; |
| 24 | char name[GPIO_NAME_SIZE]; |
| 25 | } gpio_registry[MAX_NUM_GPIOS]; |
| 26 | |
Holger Hans Peter Freyther | 1356f8e | 2013-02-07 23:41:01 +0000 | [diff] [blame] | 27 | #if defined(CONFIG_SOC_DA8XX) |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 28 | #define pinmux(x) (&davinci_syscfg_regs->pinmux[x]) |
| 29 | |
Tomas Novotny | 5903287 | 2013-02-01 06:46:00 +0000 | [diff] [blame] | 30 | #if defined(CONFIG_SOC_DA8XX) && !defined(CONFIG_SOC_DA850) |
| 31 | static const struct pinmux_config gpio_pinmux[] = { |
| 32 | { pinmux(13), 8, 6 }, /* GP0[0] */ |
| 33 | { pinmux(13), 8, 7 }, |
| 34 | { pinmux(14), 8, 0 }, |
| 35 | { pinmux(14), 8, 1 }, |
| 36 | { pinmux(14), 8, 2 }, |
| 37 | { pinmux(14), 8, 3 }, |
| 38 | { pinmux(14), 8, 4 }, |
| 39 | { pinmux(14), 8, 5 }, |
| 40 | { pinmux(14), 8, 6 }, |
| 41 | { pinmux(14), 8, 7 }, |
| 42 | { pinmux(15), 8, 0 }, |
| 43 | { pinmux(15), 8, 1 }, |
| 44 | { pinmux(15), 8, 2 }, |
| 45 | { pinmux(15), 8, 3 }, |
| 46 | { pinmux(15), 8, 4 }, |
| 47 | { pinmux(15), 8, 5 }, |
| 48 | { pinmux(15), 8, 6 }, /* GP1[0] */ |
| 49 | { pinmux(15), 8, 7 }, |
| 50 | { pinmux(16), 8, 0 }, |
| 51 | { pinmux(16), 8, 1 }, |
| 52 | { pinmux(16), 8, 2 }, |
| 53 | { pinmux(16), 8, 3 }, |
| 54 | { pinmux(16), 8, 4 }, |
| 55 | { pinmux(16), 8, 5 }, |
| 56 | { pinmux(16), 8, 6 }, |
| 57 | { pinmux(16), 8, 7 }, |
| 58 | { pinmux(17), 8, 0 }, |
| 59 | { pinmux(17), 8, 1 }, |
| 60 | { pinmux(17), 8, 2 }, |
| 61 | { pinmux(17), 8, 3 }, |
| 62 | { pinmux(17), 8, 4 }, |
| 63 | { pinmux(17), 8, 5 }, |
| 64 | { pinmux(17), 8, 6 }, /* GP2[0] */ |
| 65 | { pinmux(17), 8, 7 }, |
| 66 | { pinmux(18), 8, 0 }, |
| 67 | { pinmux(18), 8, 1 }, |
| 68 | { pinmux(18), 8, 2 }, |
| 69 | { pinmux(18), 8, 3 }, |
| 70 | { pinmux(18), 8, 4 }, |
| 71 | { pinmux(18), 8, 5 }, |
| 72 | { pinmux(18), 8, 6 }, |
| 73 | { pinmux(18), 8, 7 }, |
| 74 | { pinmux(19), 8, 0 }, |
| 75 | { pinmux(9), 8, 2 }, |
| 76 | { pinmux(9), 8, 3 }, |
| 77 | { pinmux(9), 8, 4 }, |
| 78 | { pinmux(9), 8, 5 }, |
| 79 | { pinmux(9), 8, 6 }, |
| 80 | { pinmux(10), 8, 1 }, /* GP3[0] */ |
| 81 | { pinmux(10), 8, 2 }, |
| 82 | { pinmux(10), 8, 3 }, |
| 83 | { pinmux(10), 8, 4 }, |
| 84 | { pinmux(10), 8, 5 }, |
| 85 | { pinmux(10), 8, 6 }, |
| 86 | { pinmux(10), 8, 7 }, |
| 87 | { pinmux(11), 8, 0 }, |
| 88 | { pinmux(11), 8, 1 }, |
| 89 | { pinmux(11), 8, 2 }, |
| 90 | { pinmux(11), 8, 3 }, |
| 91 | { pinmux(11), 8, 4 }, |
| 92 | { pinmux(9), 8, 7 }, |
| 93 | { pinmux(2), 8, 6 }, |
| 94 | { pinmux(11), 8, 5 }, |
| 95 | { pinmux(11), 8, 6 }, |
| 96 | { pinmux(12), 8, 4 }, /* GP4[0] */ |
| 97 | { pinmux(12), 8, 5 }, |
| 98 | { pinmux(12), 8, 6 }, |
| 99 | { pinmux(12), 8, 7 }, |
| 100 | { pinmux(13), 8, 0 }, |
| 101 | { pinmux(13), 8, 1 }, |
| 102 | { pinmux(13), 8, 2 }, |
| 103 | { pinmux(13), 8, 3 }, |
| 104 | { pinmux(13), 8, 4 }, |
| 105 | { pinmux(13), 8, 5 }, |
| 106 | { pinmux(11), 8, 7 }, |
| 107 | { pinmux(12), 8, 0 }, |
| 108 | { pinmux(12), 8, 1 }, |
| 109 | { pinmux(12), 8, 2 }, |
| 110 | { pinmux(12), 8, 3 }, |
| 111 | { pinmux(9), 8, 1 }, |
| 112 | { pinmux(7), 8, 3 }, /* GP5[0] */ |
| 113 | { pinmux(7), 8, 4 }, |
| 114 | { pinmux(7), 8, 5 }, |
| 115 | { pinmux(7), 8, 6 }, |
| 116 | { pinmux(7), 8, 7 }, |
| 117 | { pinmux(8), 8, 0 }, |
| 118 | { pinmux(8), 8, 1 }, |
| 119 | { pinmux(8), 8, 2 }, |
| 120 | { pinmux(8), 8, 3 }, |
| 121 | { pinmux(8), 8, 4 }, |
| 122 | { pinmux(8), 8, 5 }, |
| 123 | { pinmux(8), 8, 6 }, |
| 124 | { pinmux(8), 8, 7 }, |
| 125 | { pinmux(9), 8, 0 }, |
| 126 | { pinmux(7), 8, 1 }, |
| 127 | { pinmux(7), 8, 2 }, |
| 128 | { pinmux(5), 8, 1 }, /* GP6[0] */ |
| 129 | { pinmux(5), 8, 2 }, |
| 130 | { pinmux(5), 8, 3 }, |
| 131 | { pinmux(5), 8, 4 }, |
| 132 | { pinmux(5), 8, 5 }, |
| 133 | { pinmux(5), 8, 6 }, |
| 134 | { pinmux(5), 8, 7 }, |
| 135 | { pinmux(6), 8, 0 }, |
| 136 | { pinmux(6), 8, 1 }, |
| 137 | { pinmux(6), 8, 2 }, |
| 138 | { pinmux(6), 8, 3 }, |
| 139 | { pinmux(6), 8, 4 }, |
| 140 | { pinmux(6), 8, 5 }, |
| 141 | { pinmux(6), 8, 6 }, |
| 142 | { pinmux(6), 8, 7 }, |
| 143 | { pinmux(7), 8, 0 }, |
| 144 | { pinmux(1), 8, 0 }, /* GP7[0] */ |
| 145 | { pinmux(1), 8, 1 }, |
| 146 | { pinmux(1), 8, 2 }, |
| 147 | { pinmux(1), 8, 3 }, |
| 148 | { pinmux(1), 8, 4 }, |
| 149 | { pinmux(1), 8, 5 }, |
| 150 | { pinmux(1), 8, 6 }, |
| 151 | { pinmux(1), 8, 7 }, |
| 152 | { pinmux(2), 8, 0 }, |
| 153 | { pinmux(2), 8, 1 }, |
| 154 | { pinmux(2), 8, 2 }, |
| 155 | { pinmux(2), 8, 3 }, |
| 156 | { pinmux(2), 8, 4 }, |
| 157 | { pinmux(2), 8, 5 }, |
| 158 | { pinmux(0), 1, 0 }, |
| 159 | { pinmux(0), 1, 1 }, |
| 160 | }; |
Tom Rini | 9dbc836 | 2013-03-11 12:02:40 -0400 | [diff] [blame] | 161 | #else /* CONFIG_SOC_DA8XX && CONFIG_SOC_DA850 */ |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 162 | static const struct pinmux_config gpio_pinmux[] = { |
| 163 | { pinmux(1), 8, 7 }, /* GP0[0] */ |
| 164 | { pinmux(1), 8, 6 }, |
| 165 | { pinmux(1), 8, 5 }, |
| 166 | { pinmux(1), 8, 4 }, |
| 167 | { pinmux(1), 8, 3 }, |
| 168 | { pinmux(1), 8, 2 }, |
| 169 | { pinmux(1), 8, 1 }, |
| 170 | { pinmux(1), 8, 0 }, |
| 171 | { pinmux(0), 8, 7 }, |
| 172 | { pinmux(0), 8, 6 }, |
| 173 | { pinmux(0), 8, 5 }, |
| 174 | { pinmux(0), 8, 4 }, |
| 175 | { pinmux(0), 8, 3 }, |
| 176 | { pinmux(0), 8, 2 }, |
| 177 | { pinmux(0), 8, 1 }, |
| 178 | { pinmux(0), 8, 0 }, |
| 179 | { pinmux(4), 8, 7 }, /* GP1[0] */ |
| 180 | { pinmux(4), 8, 6 }, |
| 181 | { pinmux(4), 8, 5 }, |
| 182 | { pinmux(4), 8, 4 }, |
| 183 | { pinmux(4), 8, 3 }, |
| 184 | { pinmux(4), 8, 2 }, |
| 185 | { pinmux(4), 4, 1 }, |
| 186 | { pinmux(4), 4, 0 }, |
| 187 | { pinmux(3), 4, 0 }, |
| 188 | { pinmux(2), 4, 6 }, |
| 189 | { pinmux(2), 4, 5 }, |
| 190 | { pinmux(2), 4, 4 }, |
| 191 | { pinmux(2), 4, 3 }, |
| 192 | { pinmux(2), 4, 2 }, |
| 193 | { pinmux(2), 4, 1 }, |
| 194 | { pinmux(2), 8, 0 }, |
| 195 | { pinmux(6), 8, 7 }, /* GP2[0] */ |
| 196 | { pinmux(6), 8, 6 }, |
| 197 | { pinmux(6), 8, 5 }, |
| 198 | { pinmux(6), 8, 4 }, |
| 199 | { pinmux(6), 8, 3 }, |
| 200 | { pinmux(6), 8, 2 }, |
| 201 | { pinmux(6), 8, 1 }, |
| 202 | { pinmux(6), 8, 0 }, |
| 203 | { pinmux(5), 8, 7 }, |
| 204 | { pinmux(5), 8, 6 }, |
| 205 | { pinmux(5), 8, 5 }, |
| 206 | { pinmux(5), 8, 4 }, |
| 207 | { pinmux(5), 8, 3 }, |
| 208 | { pinmux(5), 8, 2 }, |
| 209 | { pinmux(5), 8, 1 }, |
| 210 | { pinmux(5), 8, 0 }, |
| 211 | { pinmux(8), 8, 7 }, /* GP3[0] */ |
| 212 | { pinmux(8), 8, 6 }, |
| 213 | { pinmux(8), 8, 5 }, |
| 214 | { pinmux(8), 8, 4 }, |
| 215 | { pinmux(8), 8, 3 }, |
| 216 | { pinmux(8), 8, 2 }, |
| 217 | { pinmux(8), 8, 1 }, |
| 218 | { pinmux(8), 8, 0 }, |
| 219 | { pinmux(7), 8, 7 }, |
| 220 | { pinmux(7), 8, 6 }, |
| 221 | { pinmux(7), 8, 5 }, |
| 222 | { pinmux(7), 8, 4 }, |
| 223 | { pinmux(7), 8, 3 }, |
| 224 | { pinmux(7), 8, 2 }, |
| 225 | { pinmux(7), 8, 1 }, |
| 226 | { pinmux(7), 8, 0 }, |
| 227 | { pinmux(10), 8, 7 }, /* GP4[0] */ |
| 228 | { pinmux(10), 8, 6 }, |
| 229 | { pinmux(10), 8, 5 }, |
| 230 | { pinmux(10), 8, 4 }, |
| 231 | { pinmux(10), 8, 3 }, |
| 232 | { pinmux(10), 8, 2 }, |
| 233 | { pinmux(10), 8, 1 }, |
| 234 | { pinmux(10), 8, 0 }, |
| 235 | { pinmux(9), 8, 7 }, |
| 236 | { pinmux(9), 8, 6 }, |
| 237 | { pinmux(9), 8, 5 }, |
| 238 | { pinmux(9), 8, 4 }, |
| 239 | { pinmux(9), 8, 3 }, |
| 240 | { pinmux(9), 8, 2 }, |
| 241 | { pinmux(9), 8, 1 }, |
| 242 | { pinmux(9), 8, 0 }, |
| 243 | { pinmux(12), 8, 7 }, /* GP5[0] */ |
| 244 | { pinmux(12), 8, 6 }, |
| 245 | { pinmux(12), 8, 5 }, |
| 246 | { pinmux(12), 8, 4 }, |
| 247 | { pinmux(12), 8, 3 }, |
| 248 | { pinmux(12), 8, 2 }, |
| 249 | { pinmux(12), 8, 1 }, |
| 250 | { pinmux(12), 8, 0 }, |
| 251 | { pinmux(11), 8, 7 }, |
| 252 | { pinmux(11), 8, 6 }, |
| 253 | { pinmux(11), 8, 5 }, |
| 254 | { pinmux(11), 8, 4 }, |
| 255 | { pinmux(11), 8, 3 }, |
| 256 | { pinmux(11), 8, 2 }, |
| 257 | { pinmux(11), 8, 1 }, |
| 258 | { pinmux(11), 8, 0 }, |
| 259 | { pinmux(19), 8, 6 }, /* GP6[0] */ |
| 260 | { pinmux(19), 8, 5 }, |
| 261 | { pinmux(19), 8, 4 }, |
| 262 | { pinmux(19), 8, 3 }, |
| 263 | { pinmux(19), 8, 2 }, |
| 264 | { pinmux(16), 8, 1 }, |
| 265 | { pinmux(14), 8, 1 }, |
| 266 | { pinmux(14), 8, 0 }, |
| 267 | { pinmux(13), 8, 7 }, |
| 268 | { pinmux(13), 8, 6 }, |
| 269 | { pinmux(13), 8, 5 }, |
| 270 | { pinmux(13), 8, 4 }, |
| 271 | { pinmux(13), 8, 3 }, |
| 272 | { pinmux(13), 8, 2 }, |
| 273 | { pinmux(13), 8, 1 }, |
| 274 | { pinmux(13), 8, 0 }, |
| 275 | { pinmux(18), 8, 1 }, /* GP7[0] */ |
| 276 | { pinmux(18), 8, 0 }, |
| 277 | { pinmux(17), 8, 7 }, |
| 278 | { pinmux(17), 8, 6 }, |
| 279 | { pinmux(17), 8, 5 }, |
| 280 | { pinmux(17), 8, 4 }, |
| 281 | { pinmux(17), 8, 3 }, |
| 282 | { pinmux(17), 8, 2 }, |
| 283 | { pinmux(17), 8, 1 }, |
| 284 | { pinmux(17), 8, 0 }, |
| 285 | { pinmux(16), 8, 7 }, |
| 286 | { pinmux(16), 8, 6 }, |
| 287 | { pinmux(16), 8, 5 }, |
| 288 | { pinmux(16), 8, 4 }, |
| 289 | { pinmux(16), 8, 3 }, |
| 290 | { pinmux(16), 8, 2 }, |
| 291 | { pinmux(19), 8, 0 }, /* GP8[0] */ |
| 292 | { pinmux(3), 4, 7 }, |
| 293 | { pinmux(3), 4, 6 }, |
| 294 | { pinmux(3), 4, 5 }, |
| 295 | { pinmux(3), 4, 4 }, |
| 296 | { pinmux(3), 4, 3 }, |
| 297 | { pinmux(3), 4, 2 }, |
| 298 | { pinmux(2), 4, 7 }, |
| 299 | { pinmux(19), 8, 1 }, |
| 300 | { pinmux(19), 8, 0 }, |
| 301 | { pinmux(18), 8, 7 }, |
| 302 | { pinmux(18), 8, 6 }, |
| 303 | { pinmux(18), 8, 5 }, |
| 304 | { pinmux(18), 8, 4 }, |
| 305 | { pinmux(18), 8, 3 }, |
| 306 | { pinmux(18), 8, 2 }, |
| 307 | }; |
Tom Rini | 9dbc836 | 2013-03-11 12:02:40 -0400 | [diff] [blame] | 308 | #endif /* CONFIG_SOC_DA8XX && !CONFIG_SOC_DA850 */ |
| 309 | #else /* !CONFIG_SOC_DA8XX */ |
Holger Hans Peter Freyther | 1356f8e | 2013-02-07 23:41:01 +0000 | [diff] [blame] | 310 | #define davinci_configure_pin_mux(a, b) |
Tom Rini | 9dbc836 | 2013-03-11 12:02:40 -0400 | [diff] [blame] | 311 | #endif /* CONFIG_SOC_DA8XX */ |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 312 | |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 313 | int gpio_request(unsigned int gpio, const char *label) |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 314 | { |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 315 | if (gpio >= MAX_NUM_GPIOS) |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 316 | return -1; |
| 317 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 318 | if (gpio_registry[gpio].is_registered) |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 319 | return -1; |
| 320 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 321 | gpio_registry[gpio].is_registered = 1; |
| 322 | strncpy(gpio_registry[gpio].name, label, GPIO_NAME_SIZE); |
| 323 | gpio_registry[gpio].name[GPIO_NAME_SIZE - 1] = 0; |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 324 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 325 | davinci_configure_pin_mux(&gpio_pinmux[gpio], 1); |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 330 | int gpio_free(unsigned int gpio) |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 331 | { |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 332 | if (gpio >= MAX_NUM_GPIOS) |
| 333 | return -1; |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 334 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 335 | if (!gpio_registry[gpio].is_registered) |
| 336 | return -1; |
| 337 | |
| 338 | gpio_registry[gpio].is_registered = 0; |
| 339 | gpio_registry[gpio].name[0] = '\0'; |
| 340 | /* Do not configure as input or change pin mux here */ |
| 341 | return 0; |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 342 | } |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 343 | #endif |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 344 | |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 345 | static int _gpio_direction_output(struct davinci_gpio *bank, unsigned int gpio, int value) |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 346 | { |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 347 | clrbits_le32(&bank->dir, 1U << GPIO_BIT(gpio)); |
| 348 | gpio_set_value(gpio, value); |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 349 | return 0; |
| 350 | } |
| 351 | |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 352 | static int _gpio_direction_input(struct davinci_gpio *bank, unsigned int gpio) |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 353 | { |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 354 | setbits_le32(&bank->dir, 1U << GPIO_BIT(gpio)); |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 355 | return 0; |
| 356 | } |
| 357 | |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 358 | static int _gpio_get_value(struct davinci_gpio *bank, unsigned int gpio) |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 359 | { |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 360 | unsigned int ip; |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 361 | ip = in_le32(&bank->in_data) & (1U << GPIO_BIT(gpio)); |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 362 | return ip ? 1 : 0; |
| 363 | } |
| 364 | |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 365 | static int _gpio_set_value(struct davinci_gpio *bank, unsigned int gpio, int value) |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 366 | { |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 367 | if (value) |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 368 | bank->set_data = 1U << GPIO_BIT(gpio); |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 369 | else |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 370 | bank->clr_data = 1U << GPIO_BIT(gpio); |
| 371 | |
| 372 | return 0; |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 373 | } |
| 374 | |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 375 | static int _gpio_get_dir(struct davinci_gpio *bank, unsigned int gpio) |
| 376 | { |
| 377 | return in_le32(&bank->dir) & (1U << GPIO_BIT(gpio)); |
| 378 | } |
| 379 | |
| 380 | #ifndef CONFIG_DM_GPIO |
| 381 | |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 382 | void gpio_info(void) |
| 383 | { |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 384 | unsigned int gpio, dir, val; |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 385 | struct davinci_gpio *bank; |
| 386 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 387 | for (gpio = 0; gpio < MAX_NUM_GPIOS; ++gpio) { |
| 388 | bank = GPIO_BANK(gpio); |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 389 | dir = _gpio_get_dir(bank, gpio); |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 390 | val = gpio_get_value(gpio); |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 391 | |
| 392 | printf("% 4d: %s: %d [%c] %s\n", |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 393 | gpio, dir ? " in" : "out", val, |
| 394 | gpio_registry[gpio].is_registered ? 'x' : ' ', |
| 395 | gpio_registry[gpio].name); |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 396 | } |
| 397 | } |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 398 | |
| 399 | int gpio_direction_input(unsigned int gpio) |
| 400 | { |
| 401 | struct davinci_gpio *bank; |
| 402 | |
| 403 | bank = GPIO_BANK(gpio); |
| 404 | return _gpio_direction_input(bank, gpio); |
| 405 | } |
| 406 | |
| 407 | int gpio_direction_output(unsigned int gpio, int value) |
| 408 | { |
| 409 | struct davinci_gpio *bank; |
| 410 | |
| 411 | bank = GPIO_BANK(gpio); |
| 412 | return _gpio_direction_output(bank, gpio, value); |
| 413 | } |
| 414 | |
| 415 | int gpio_get_value(unsigned int gpio) |
| 416 | { |
| 417 | struct davinci_gpio *bank; |
| 418 | |
| 419 | bank = GPIO_BANK(gpio); |
| 420 | return _gpio_get_value(bank, gpio); |
| 421 | } |
| 422 | |
| 423 | int gpio_set_value(unsigned int gpio, int value) |
| 424 | { |
| 425 | struct davinci_gpio *bank; |
| 426 | |
| 427 | bank = GPIO_BANK(gpio); |
| 428 | return _gpio_set_value(bank, gpio, value); |
| 429 | } |
| 430 | |
| 431 | #else /* CONFIG_DM_GPIO */ |
| 432 | |
| 433 | static struct davinci_gpio *davinci_get_gpio_bank(struct udevice *dev, unsigned int offset) |
| 434 | { |
| 435 | struct davinci_gpio_bank *bank = dev_get_priv(dev); |
Adam Ford | a112939 | 2018-08-16 23:13:34 -0500 | [diff] [blame] | 436 | unsigned int addr; |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 437 | |
Adam Ford | a112939 | 2018-08-16 23:13:34 -0500 | [diff] [blame] | 438 | /* |
| 439 | * The device tree is not broken into banks but the infrastructure is |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 440 | * expecting it this way, so we'll first include the 0x10 offset, then |
| 441 | * calculate the bank manually based on the offset. |
Adam Ford | a112939 | 2018-08-16 23:13:34 -0500 | [diff] [blame] | 442 | * Casting 'addr' as Unsigned long is needed to make the math work. |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 443 | */ |
Adam Ford | a112939 | 2018-08-16 23:13:34 -0500 | [diff] [blame] | 444 | addr = ((unsigned long)(struct davinci_gpio *)bank->base) + |
| 445 | 0x10 + (0x28 * (offset >> 5)); |
| 446 | return (struct davinci_gpio *)addr; |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | static int davinci_gpio_direction_input(struct udevice *dev, unsigned int offset) |
| 450 | { |
| 451 | struct davinci_gpio *base = davinci_get_gpio_bank(dev, offset); |
| 452 | |
Adam Ford | a112939 | 2018-08-16 23:13:34 -0500 | [diff] [blame] | 453 | /* |
| 454 | * Fetch the address based on GPIO, but only pass the masked low 32-bits |
| 455 | */ |
| 456 | _gpio_direction_input(base, (offset & 0x1f)); |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 457 | return 0; |
| 458 | } |
| 459 | |
| 460 | static int davinci_gpio_direction_output(struct udevice *dev, unsigned int offset, |
| 461 | int value) |
| 462 | { |
| 463 | struct davinci_gpio *base = davinci_get_gpio_bank(dev, offset); |
| 464 | |
Adam Ford | a112939 | 2018-08-16 23:13:34 -0500 | [diff] [blame] | 465 | _gpio_direction_output(base, (offset & 0x1f), value); |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | static int davinci_gpio_get_value(struct udevice *dev, unsigned int offset) |
| 470 | { |
| 471 | struct davinci_gpio *base = davinci_get_gpio_bank(dev, offset); |
| 472 | |
Adam Ford | a112939 | 2018-08-16 23:13:34 -0500 | [diff] [blame] | 473 | return _gpio_get_value(base, (offset & 0x1f)); |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | static int davinci_gpio_set_value(struct udevice *dev, unsigned int offset, |
| 477 | int value) |
| 478 | { |
| 479 | struct davinci_gpio *base = davinci_get_gpio_bank(dev, offset); |
| 480 | |
Adam Ford | a112939 | 2018-08-16 23:13:34 -0500 | [diff] [blame] | 481 | _gpio_set_value(base, (offset & 0x1f), value); |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 482 | |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | static int davinci_gpio_get_function(struct udevice *dev, unsigned int offset) |
| 487 | { |
| 488 | unsigned int dir; |
| 489 | struct davinci_gpio *base = davinci_get_gpio_bank(dev, offset); |
| 490 | |
| 491 | dir = _gpio_get_dir(base, offset); |
| 492 | |
| 493 | if (dir) |
| 494 | return GPIOF_INPUT; |
| 495 | |
| 496 | return GPIOF_OUTPUT; |
| 497 | } |
| 498 | |
Adam Ford | a4cb26d | 2018-08-16 23:21:57 -0500 | [diff] [blame] | 499 | static int davinci_gpio_xlate(struct udevice *dev, struct gpio_desc *desc, |
| 500 | struct ofnode_phandle_args *args) |
| 501 | { |
| 502 | desc->offset = args->args[0]; |
| 503 | |
| 504 | if (args->args[1] & GPIO_ACTIVE_LOW) |
| 505 | desc->flags = GPIOD_ACTIVE_LOW; |
| 506 | else |
| 507 | desc->flags = 0; |
| 508 | return 0; |
| 509 | } |
| 510 | |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 511 | static const struct dm_gpio_ops gpio_davinci_ops = { |
| 512 | .direction_input = davinci_gpio_direction_input, |
| 513 | .direction_output = davinci_gpio_direction_output, |
| 514 | .get_value = davinci_gpio_get_value, |
| 515 | .set_value = davinci_gpio_set_value, |
| 516 | .get_function = davinci_gpio_get_function, |
Adam Ford | a4cb26d | 2018-08-16 23:21:57 -0500 | [diff] [blame] | 517 | .xlate = davinci_gpio_xlate, |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 518 | }; |
| 519 | |
| 520 | static int davinci_gpio_probe(struct udevice *dev) |
| 521 | { |
| 522 | struct davinci_gpio_bank *bank = dev_get_priv(dev); |
| 523 | struct davinci_gpio_platdata *plat = dev_get_platdata(dev); |
| 524 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 525 | const void *fdt = gd->fdt_blob; |
| 526 | int node = dev_of_offset(dev); |
| 527 | |
| 528 | uc_priv->bank_name = plat->port_name; |
| 529 | uc_priv->gpio_count = fdtdec_get_int(fdt, node, "ti,ngpio", -1); |
| 530 | bank->base = (struct davinci_gpio *)plat->base; |
| 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | static const struct udevice_id davinci_gpio_ids[] = { |
| 535 | { .compatible = "ti,dm6441-gpio" }, |
Keerthy | 0e9265c | 2018-10-03 17:55:13 +0530 | [diff] [blame] | 536 | { .compatible = "ti,k2g-gpio" }, |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 537 | { } |
| 538 | }; |
| 539 | |
| 540 | static int davinci_gpio_ofdata_to_platdata(struct udevice *dev) |
| 541 | { |
| 542 | struct davinci_gpio_platdata *plat = dev_get_platdata(dev); |
| 543 | fdt_addr_t addr; |
| 544 | |
| 545 | addr = devfdt_get_addr(dev); |
| 546 | if (addr == FDT_ADDR_T_NONE) |
| 547 | return -EINVAL; |
| 548 | |
| 549 | plat->base = addr; |
| 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | U_BOOT_DRIVER(gpio_davinci) = { |
| 554 | .name = "gpio_davinci", |
| 555 | .id = UCLASS_GPIO, |
| 556 | .ops = &gpio_davinci_ops, |
| 557 | .ofdata_to_platdata = of_match_ptr(davinci_gpio_ofdata_to_platdata), |
| 558 | .of_match = davinci_gpio_ids, |
| 559 | .bind = dm_scan_fdt_dev, |
| 560 | .platdata_auto_alloc_size = sizeof(struct davinci_gpio_platdata), |
| 561 | .probe = davinci_gpio_probe, |
| 562 | .priv_auto_alloc_size = sizeof(struct davinci_gpio_bank), |
| 563 | }; |
| 564 | |
| 565 | #endif |