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 | |
Simon Glass | fa4689a | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 18 | #if !CONFIG_IS_ENABLED(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_input(struct davinci_gpio *bank, unsigned int gpio) |
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 | setbits_le32(&bank->dir, 1U << GPIO_BIT(gpio)); |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 348 | return 0; |
| 349 | } |
| 350 | |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 351 | static int _gpio_get_value(struct davinci_gpio *bank, unsigned int gpio) |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 352 | { |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 353 | unsigned int ip; |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 354 | ip = in_le32(&bank->in_data) & (1U << GPIO_BIT(gpio)); |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 355 | return ip ? 1 : 0; |
| 356 | } |
| 357 | |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 358 | 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] | 359 | { |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 360 | if (value) |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 361 | bank->set_data = 1U << GPIO_BIT(gpio); |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 362 | else |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 363 | bank->clr_data = 1U << GPIO_BIT(gpio); |
| 364 | |
| 365 | return 0; |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 366 | } |
| 367 | |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 368 | static int _gpio_get_dir(struct davinci_gpio *bank, unsigned int gpio) |
| 369 | { |
| 370 | return in_le32(&bank->dir) & (1U << GPIO_BIT(gpio)); |
| 371 | } |
| 372 | |
Keerthy | d9951c9 | 2019-10-24 13:52:28 +0530 | [diff] [blame] | 373 | static int _gpio_direction_output(struct davinci_gpio *bank, unsigned int gpio, |
| 374 | int value) |
| 375 | { |
| 376 | clrbits_le32(&bank->dir, 1U << GPIO_BIT(gpio)); |
| 377 | _gpio_set_value(bank, gpio, value); |
| 378 | return 0; |
| 379 | } |
Simon Glass | fa4689a | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 380 | |
| 381 | #if !CONFIG_IS_ENABLED(DM_GPIO) |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 382 | |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 383 | void gpio_info(void) |
| 384 | { |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 385 | unsigned int gpio, dir, val; |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 386 | struct davinci_gpio *bank; |
| 387 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 388 | for (gpio = 0; gpio < MAX_NUM_GPIOS; ++gpio) { |
| 389 | bank = GPIO_BANK(gpio); |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 390 | dir = _gpio_get_dir(bank, gpio); |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 391 | val = gpio_get_value(gpio); |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 392 | |
| 393 | printf("% 4d: %s: %d [%c] %s\n", |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 394 | gpio, dir ? " in" : "out", val, |
| 395 | gpio_registry[gpio].is_registered ? 'x' : ' ', |
| 396 | gpio_registry[gpio].name); |
Laurence Withers | 3ffd968 | 2011-07-18 09:26:01 -0400 | [diff] [blame] | 397 | } |
| 398 | } |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 399 | |
| 400 | int gpio_direction_input(unsigned int gpio) |
| 401 | { |
| 402 | struct davinci_gpio *bank; |
| 403 | |
| 404 | bank = GPIO_BANK(gpio); |
| 405 | return _gpio_direction_input(bank, gpio); |
| 406 | } |
| 407 | |
| 408 | int gpio_direction_output(unsigned int gpio, int value) |
| 409 | { |
| 410 | struct davinci_gpio *bank; |
| 411 | |
| 412 | bank = GPIO_BANK(gpio); |
| 413 | return _gpio_direction_output(bank, gpio, value); |
| 414 | } |
| 415 | |
| 416 | int gpio_get_value(unsigned int gpio) |
| 417 | { |
| 418 | struct davinci_gpio *bank; |
| 419 | |
| 420 | bank = GPIO_BANK(gpio); |
| 421 | return _gpio_get_value(bank, gpio); |
| 422 | } |
| 423 | |
| 424 | int gpio_set_value(unsigned int gpio, int value) |
| 425 | { |
| 426 | struct davinci_gpio *bank; |
| 427 | |
| 428 | bank = GPIO_BANK(gpio); |
| 429 | return _gpio_set_value(bank, gpio, value); |
| 430 | } |
| 431 | |
Simon Glass | fa4689a | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 432 | #else /* DM_GPIO */ |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 433 | |
| 434 | static struct davinci_gpio *davinci_get_gpio_bank(struct udevice *dev, unsigned int offset) |
| 435 | { |
| 436 | struct davinci_gpio_bank *bank = dev_get_priv(dev); |
Adam Ford | a112939 | 2018-08-16 23:13:34 -0500 | [diff] [blame] | 437 | unsigned int addr; |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 438 | |
Adam Ford | a112939 | 2018-08-16 23:13:34 -0500 | [diff] [blame] | 439 | /* |
| 440 | * The device tree is not broken into banks but the infrastructure is |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 441 | * expecting it this way, so we'll first include the 0x10 offset, then |
| 442 | * calculate the bank manually based on the offset. |
Adam Ford | a112939 | 2018-08-16 23:13:34 -0500 | [diff] [blame] | 443 | * Casting 'addr' as Unsigned long is needed to make the math work. |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 444 | */ |
Adam Ford | a112939 | 2018-08-16 23:13:34 -0500 | [diff] [blame] | 445 | addr = ((unsigned long)(struct davinci_gpio *)bank->base) + |
| 446 | 0x10 + (0x28 * (offset >> 5)); |
| 447 | return (struct davinci_gpio *)addr; |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | static int davinci_gpio_direction_input(struct udevice *dev, unsigned int offset) |
| 451 | { |
| 452 | struct davinci_gpio *base = davinci_get_gpio_bank(dev, offset); |
| 453 | |
Adam Ford | a112939 | 2018-08-16 23:13:34 -0500 | [diff] [blame] | 454 | /* |
| 455 | * Fetch the address based on GPIO, but only pass the masked low 32-bits |
| 456 | */ |
| 457 | _gpio_direction_input(base, (offset & 0x1f)); |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 458 | return 0; |
| 459 | } |
| 460 | |
| 461 | static int davinci_gpio_direction_output(struct udevice *dev, unsigned int offset, |
| 462 | int value) |
| 463 | { |
| 464 | struct davinci_gpio *base = davinci_get_gpio_bank(dev, offset); |
| 465 | |
Adam Ford | a112939 | 2018-08-16 23:13:34 -0500 | [diff] [blame] | 466 | _gpio_direction_output(base, (offset & 0x1f), value); |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | static int davinci_gpio_get_value(struct udevice *dev, unsigned int offset) |
| 471 | { |
| 472 | struct davinci_gpio *base = davinci_get_gpio_bank(dev, offset); |
| 473 | |
Adam Ford | a112939 | 2018-08-16 23:13:34 -0500 | [diff] [blame] | 474 | return _gpio_get_value(base, (offset & 0x1f)); |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | static int davinci_gpio_set_value(struct udevice *dev, unsigned int offset, |
| 478 | int value) |
| 479 | { |
| 480 | struct davinci_gpio *base = davinci_get_gpio_bank(dev, offset); |
| 481 | |
Adam Ford | a112939 | 2018-08-16 23:13:34 -0500 | [diff] [blame] | 482 | _gpio_set_value(base, (offset & 0x1f), value); |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 483 | |
| 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | static int davinci_gpio_get_function(struct udevice *dev, unsigned int offset) |
| 488 | { |
| 489 | unsigned int dir; |
| 490 | struct davinci_gpio *base = davinci_get_gpio_bank(dev, offset); |
| 491 | |
| 492 | dir = _gpio_get_dir(base, offset); |
| 493 | |
| 494 | if (dir) |
| 495 | return GPIOF_INPUT; |
| 496 | |
| 497 | return GPIOF_OUTPUT; |
| 498 | } |
| 499 | |
Adam Ford | a4cb26d | 2018-08-16 23:21:57 -0500 | [diff] [blame] | 500 | static int davinci_gpio_xlate(struct udevice *dev, struct gpio_desc *desc, |
| 501 | struct ofnode_phandle_args *args) |
| 502 | { |
| 503 | desc->offset = args->args[0]; |
| 504 | |
| 505 | if (args->args[1] & GPIO_ACTIVE_LOW) |
| 506 | desc->flags = GPIOD_ACTIVE_LOW; |
| 507 | else |
| 508 | desc->flags = 0; |
| 509 | return 0; |
| 510 | } |
| 511 | |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 512 | static const struct dm_gpio_ops gpio_davinci_ops = { |
| 513 | .direction_input = davinci_gpio_direction_input, |
| 514 | .direction_output = davinci_gpio_direction_output, |
| 515 | .get_value = davinci_gpio_get_value, |
| 516 | .set_value = davinci_gpio_set_value, |
| 517 | .get_function = davinci_gpio_get_function, |
Adam Ford | a4cb26d | 2018-08-16 23:21:57 -0500 | [diff] [blame] | 518 | .xlate = davinci_gpio_xlate, |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 519 | }; |
| 520 | |
| 521 | static int davinci_gpio_probe(struct udevice *dev) |
| 522 | { |
| 523 | struct davinci_gpio_bank *bank = dev_get_priv(dev); |
| 524 | struct davinci_gpio_platdata *plat = dev_get_platdata(dev); |
| 525 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 526 | const void *fdt = gd->fdt_blob; |
| 527 | int node = dev_of_offset(dev); |
| 528 | |
| 529 | uc_priv->bank_name = plat->port_name; |
| 530 | uc_priv->gpio_count = fdtdec_get_int(fdt, node, "ti,ngpio", -1); |
| 531 | bank->base = (struct davinci_gpio *)plat->base; |
| 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | static const struct udevice_id davinci_gpio_ids[] = { |
| 536 | { .compatible = "ti,dm6441-gpio" }, |
Keerthy | 0e9265c | 2018-10-03 17:55:13 +0530 | [diff] [blame] | 537 | { .compatible = "ti,k2g-gpio" }, |
Adam Ford | 9c286a7 | 2018-06-10 22:25:57 -0500 | [diff] [blame] | 538 | { } |
| 539 | }; |
| 540 | |
| 541 | static int davinci_gpio_ofdata_to_platdata(struct udevice *dev) |
| 542 | { |
| 543 | struct davinci_gpio_platdata *plat = dev_get_platdata(dev); |
| 544 | fdt_addr_t addr; |
| 545 | |
| 546 | addr = devfdt_get_addr(dev); |
| 547 | if (addr == FDT_ADDR_T_NONE) |
| 548 | return -EINVAL; |
| 549 | |
| 550 | plat->base = addr; |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | U_BOOT_DRIVER(gpio_davinci) = { |
| 555 | .name = "gpio_davinci", |
| 556 | .id = UCLASS_GPIO, |
| 557 | .ops = &gpio_davinci_ops, |
| 558 | .ofdata_to_platdata = of_match_ptr(davinci_gpio_ofdata_to_platdata), |
| 559 | .of_match = davinci_gpio_ids, |
| 560 | .bind = dm_scan_fdt_dev, |
| 561 | .platdata_auto_alloc_size = sizeof(struct davinci_gpio_platdata), |
| 562 | .probe = davinci_gpio_probe, |
| 563 | .priv_auto_alloc_size = sizeof(struct davinci_gpio_bank), |
| 564 | }; |
| 565 | |
| 566 | #endif |