Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Ian Campbell | af47147 | 2014-06-05 19:00:15 +0100 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2012 Henrik Nordstrom <henrik@henriknordstrom.net> |
| 4 | * |
| 5 | * Based on earlier arch/arm/cpu/armv7/sunxi/gpio.c: |
| 6 | * |
| 7 | * (C) Copyright 2007-2011 |
| 8 | * Allwinner Technology Co., Ltd. <www.allwinnertech.com> |
| 9 | * Tom Cubie <tangliang@allwinnertech.com> |
Ian Campbell | af47147 | 2014-06-05 19:00:15 +0100 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <common.h> |
Simon Glass | 7830453 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 13 | #include <dm.h> |
| 14 | #include <errno.h> |
| 15 | #include <fdtdec.h> |
| 16 | #include <malloc.h> |
Ian Campbell | af47147 | 2014-06-05 19:00:15 +0100 | [diff] [blame] | 17 | #include <asm/io.h> |
| 18 | #include <asm/gpio.h> |
Simon Glass | 7830453 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 19 | #include <dm/device-internal.h> |
Chen-Yu Tsai | f3aa282 | 2016-07-22 16:12:59 +0800 | [diff] [blame] | 20 | #include <dt-bindings/gpio/gpio.h> |
Ian Campbell | af47147 | 2014-06-05 19:00:15 +0100 | [diff] [blame] | 21 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 22 | struct sunxi_gpio_plat { |
Simon Glass | 7830453 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 23 | struct sunxi_gpio *regs; |
| 24 | const char *bank_name; /* Name of bank, e.g. "B" */ |
| 25 | int gpio_count; |
| 26 | }; |
| 27 | |
Simon Glass | fa4689a | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 28 | #if !CONFIG_IS_ENABLED(DM_GPIO) |
Ian Campbell | af47147 | 2014-06-05 19:00:15 +0100 | [diff] [blame] | 29 | static int sunxi_gpio_output(u32 pin, u32 val) |
| 30 | { |
| 31 | u32 dat; |
| 32 | u32 bank = GPIO_BANK(pin); |
| 33 | u32 num = GPIO_NUM(pin); |
| 34 | struct sunxi_gpio *pio = BANK_TO_GPIO(bank); |
| 35 | |
| 36 | dat = readl(&pio->dat); |
| 37 | if (val) |
| 38 | dat |= 0x1 << num; |
| 39 | else |
| 40 | dat &= ~(0x1 << num); |
| 41 | |
| 42 | writel(dat, &pio->dat); |
| 43 | |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | static int sunxi_gpio_input(u32 pin) |
| 48 | { |
| 49 | u32 dat; |
| 50 | u32 bank = GPIO_BANK(pin); |
| 51 | u32 num = GPIO_NUM(pin); |
| 52 | struct sunxi_gpio *pio = BANK_TO_GPIO(bank); |
| 53 | |
| 54 | dat = readl(&pio->dat); |
| 55 | dat >>= num; |
| 56 | |
| 57 | return dat & 0x1; |
| 58 | } |
| 59 | |
| 60 | int gpio_request(unsigned gpio, const char *label) |
| 61 | { |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | int gpio_free(unsigned gpio) |
| 66 | { |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | int gpio_direction_input(unsigned gpio) |
| 71 | { |
| 72 | sunxi_gpio_set_cfgpin(gpio, SUNXI_GPIO_INPUT); |
| 73 | |
Axel Lin | 06da346 | 2014-12-20 11:41:25 +0800 | [diff] [blame] | 74 | return 0; |
Ian Campbell | af47147 | 2014-06-05 19:00:15 +0100 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | int gpio_direction_output(unsigned gpio, int value) |
| 78 | { |
| 79 | sunxi_gpio_set_cfgpin(gpio, SUNXI_GPIO_OUTPUT); |
| 80 | |
| 81 | return sunxi_gpio_output(gpio, value); |
| 82 | } |
| 83 | |
| 84 | int gpio_get_value(unsigned gpio) |
| 85 | { |
| 86 | return sunxi_gpio_input(gpio); |
| 87 | } |
| 88 | |
| 89 | int gpio_set_value(unsigned gpio, int value) |
| 90 | { |
| 91 | return sunxi_gpio_output(gpio, value); |
| 92 | } |
| 93 | |
| 94 | int sunxi_name_to_gpio(const char *name) |
| 95 | { |
| 96 | int group = 0; |
| 97 | int groupsize = 9 * 32; |
| 98 | long pin; |
| 99 | char *eptr; |
Hans de Goede | 1fc9c4a | 2014-12-24 19:34:38 +0100 | [diff] [blame] | 100 | |
Ian Campbell | af47147 | 2014-06-05 19:00:15 +0100 | [diff] [blame] | 101 | if (*name == 'P' || *name == 'p') |
| 102 | name++; |
| 103 | if (*name >= 'A') { |
| 104 | group = *name - (*name > 'a' ? 'a' : 'A'); |
| 105 | groupsize = 32; |
| 106 | name++; |
| 107 | } |
| 108 | |
| 109 | pin = simple_strtol(name, &eptr, 10); |
| 110 | if (!*name || *eptr) |
| 111 | return -1; |
| 112 | if (pin < 0 || pin > groupsize || group >= 9) |
| 113 | return -1; |
| 114 | return group * 32 + pin; |
| 115 | } |
Simon Glass | fa4689a | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 116 | #endif /* DM_GPIO */ |
Simon Glass | 7830453 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 117 | |
Simon Glass | fa4689a | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 118 | #if CONFIG_IS_ENABLED(DM_GPIO) |
Simon Glass | 9754d93 | 2015-04-18 11:33:43 -0600 | [diff] [blame] | 119 | /* TODO(sjg@chromium.org): Remove this function and use device tree */ |
| 120 | int sunxi_name_to_gpio(const char *name) |
| 121 | { |
| 122 | unsigned int gpio; |
| 123 | int ret; |
Hans de Goede | 08607d1 | 2015-04-22 11:31:22 +0200 | [diff] [blame] | 124 | #if !defined CONFIG_SPL_BUILD && defined CONFIG_AXP_GPIO |
| 125 | char lookup[8]; |
Simon Glass | 9754d93 | 2015-04-18 11:33:43 -0600 | [diff] [blame] | 126 | |
Hans de Goede | 08607d1 | 2015-04-22 11:31:22 +0200 | [diff] [blame] | 127 | if (strcasecmp(name, "AXP0-VBUS-DETECT") == 0) { |
| 128 | sprintf(lookup, SUNXI_GPIO_AXP0_PREFIX "%d", |
| 129 | SUNXI_GPIO_AXP0_VBUS_DETECT); |
| 130 | name = lookup; |
| 131 | } else if (strcasecmp(name, "AXP0-VBUS-ENABLE") == 0) { |
| 132 | sprintf(lookup, SUNXI_GPIO_AXP0_PREFIX "%d", |
| 133 | SUNXI_GPIO_AXP0_VBUS_ENABLE); |
| 134 | name = lookup; |
| 135 | } |
| 136 | #endif |
Simon Glass | 9754d93 | 2015-04-18 11:33:43 -0600 | [diff] [blame] | 137 | ret = gpio_lookup_name(name, NULL, NULL, &gpio); |
| 138 | |
| 139 | return ret ? ret : gpio; |
| 140 | } |
| 141 | |
Simon Glass | 7830453 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 142 | static int sunxi_gpio_get_value(struct udevice *dev, unsigned offset) |
| 143 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 144 | struct sunxi_gpio_plat *plat = dev_get_plat(dev); |
Simon Glass | 7830453 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 145 | u32 num = GPIO_NUM(offset); |
| 146 | unsigned dat; |
| 147 | |
| 148 | dat = readl(&plat->regs->dat); |
| 149 | dat >>= num; |
| 150 | |
| 151 | return dat & 0x1; |
| 152 | } |
| 153 | |
Simon Glass | 7830453 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 154 | static int sunxi_gpio_get_function(struct udevice *dev, unsigned offset) |
| 155 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 156 | struct sunxi_gpio_plat *plat = dev_get_plat(dev); |
Simon Glass | 7830453 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 157 | int func; |
| 158 | |
| 159 | func = sunxi_gpio_get_cfgbank(plat->regs, offset); |
| 160 | if (func == SUNXI_GPIO_OUTPUT) |
| 161 | return GPIOF_OUTPUT; |
| 162 | else if (func == SUNXI_GPIO_INPUT) |
| 163 | return GPIOF_INPUT; |
| 164 | else |
| 165 | return GPIOF_FUNC; |
| 166 | } |
| 167 | |
Chen-Yu Tsai | f3aa282 | 2016-07-22 16:12:59 +0800 | [diff] [blame] | 168 | static int sunxi_gpio_xlate(struct udevice *dev, struct gpio_desc *desc, |
Simon Glass | 12faa02 | 2017-05-18 20:09:18 -0600 | [diff] [blame] | 169 | struct ofnode_phandle_args *args) |
Chen-Yu Tsai | f3aa282 | 2016-07-22 16:12:59 +0800 | [diff] [blame] | 170 | { |
| 171 | int ret; |
| 172 | |
| 173 | ret = device_get_child(dev, args->args[0], &desc->dev); |
| 174 | if (ret) |
| 175 | return ret; |
| 176 | desc->offset = args->args[1]; |
Samuel Holland | bfda949 | 2021-10-20 23:52:56 -0500 | [diff] [blame] | 177 | desc->flags = gpio_flags_xlate(args->args[2]); |
| 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | static int sunxi_gpio_set_flags(struct udevice *dev, unsigned int offset, |
| 183 | ulong flags) |
| 184 | { |
| 185 | struct sunxi_gpio_plat *plat = dev_get_plat(dev); |
| 186 | |
| 187 | if (flags & GPIOD_IS_OUT) { |
| 188 | u32 value = !!(flags & GPIOD_IS_OUT_ACTIVE); |
| 189 | u32 num = GPIO_NUM(offset); |
| 190 | |
| 191 | clrsetbits_le32(&plat->regs->dat, 1 << num, value << num); |
| 192 | sunxi_gpio_set_cfgbank(plat->regs, offset, SUNXI_GPIO_OUTPUT); |
| 193 | } else if (flags & GPIOD_IS_IN) { |
| 194 | u32 pull = 0; |
| 195 | |
| 196 | if (flags & GPIOD_PULL_UP) |
| 197 | pull = 1; |
| 198 | else if (flags & GPIOD_PULL_DOWN) |
| 199 | pull = 2; |
| 200 | sunxi_gpio_set_pull_bank(plat->regs, offset, pull); |
| 201 | sunxi_gpio_set_cfgbank(plat->regs, offset, SUNXI_GPIO_INPUT); |
| 202 | } |
Chen-Yu Tsai | f3aa282 | 2016-07-22 16:12:59 +0800 | [diff] [blame] | 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
Simon Glass | 7830453 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 207 | static const struct dm_gpio_ops gpio_sunxi_ops = { |
Simon Glass | 7830453 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 208 | .get_value = sunxi_gpio_get_value, |
Simon Glass | 7830453 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 209 | .get_function = sunxi_gpio_get_function, |
Chen-Yu Tsai | f3aa282 | 2016-07-22 16:12:59 +0800 | [diff] [blame] | 210 | .xlate = sunxi_gpio_xlate, |
Samuel Holland | bfda949 | 2021-10-20 23:52:56 -0500 | [diff] [blame] | 211 | .set_flags = sunxi_gpio_set_flags, |
Simon Glass | 7830453 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | /** |
| 215 | * Returns the name of a GPIO bank |
| 216 | * |
| 217 | * GPIO banks are named A, B, C, ... |
| 218 | * |
| 219 | * @bank: Bank number (0, 1..n-1) |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 220 | * Return: allocated string containing the name |
Simon Glass | 7830453 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 221 | */ |
| 222 | static char *gpio_bank_name(int bank) |
| 223 | { |
| 224 | char *name; |
| 225 | |
Simon Glass | e2d3054 | 2015-04-18 11:33:44 -0600 | [diff] [blame] | 226 | name = malloc(3); |
Simon Glass | 7830453 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 227 | if (name) { |
Simon Glass | e2d3054 | 2015-04-18 11:33:44 -0600 | [diff] [blame] | 228 | name[0] = 'P'; |
| 229 | name[1] = 'A' + bank; |
| 230 | name[2] = '\0'; |
Simon Glass | 7830453 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | return name; |
| 234 | } |
| 235 | |
| 236 | static int gpio_sunxi_probe(struct udevice *dev) |
| 237 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 238 | struct sunxi_gpio_plat *plat = dev_get_plat(dev); |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 239 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | 7830453 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 240 | |
| 241 | /* Tell the uclass how many GPIOs we have */ |
| 242 | if (plat) { |
| 243 | uc_priv->gpio_count = plat->gpio_count; |
| 244 | uc_priv->bank_name = plat->bank_name; |
| 245 | } |
| 246 | |
| 247 | return 0; |
| 248 | } |
Stephen Warren | b56989e | 2016-05-11 15:26:25 -0600 | [diff] [blame] | 249 | |
| 250 | struct sunxi_gpio_soc_data { |
| 251 | int start; |
| 252 | int no_banks; |
| 253 | }; |
| 254 | |
Simon Glass | 7830453 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 255 | /** |
| 256 | * We have a top-level GPIO device with no actual GPIOs. It has a child |
| 257 | * device for each Sunxi bank. |
| 258 | */ |
| 259 | static int gpio_sunxi_bind(struct udevice *parent) |
| 260 | { |
Stephen Warren | b56989e | 2016-05-11 15:26:25 -0600 | [diff] [blame] | 261 | struct sunxi_gpio_soc_data *soc_data = |
| 262 | (struct sunxi_gpio_soc_data *)dev_get_driver_data(parent); |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 263 | struct sunxi_gpio_plat *plat = dev_get_plat(parent); |
Simon Glass | 7830453 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 264 | struct sunxi_gpio_reg *ctlr; |
Stephen Warren | b56989e | 2016-05-11 15:26:25 -0600 | [diff] [blame] | 265 | int bank, ret; |
Simon Glass | 7830453 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 266 | |
| 267 | /* If this is a child device, there is nothing to do here */ |
| 268 | if (plat) |
| 269 | return 0; |
| 270 | |
Masahiro Yamada | 1096ae1 | 2020-07-17 14:36:46 +0900 | [diff] [blame] | 271 | ctlr = dev_read_addr_ptr(parent); |
Stephen Warren | b56989e | 2016-05-11 15:26:25 -0600 | [diff] [blame] | 272 | for (bank = 0; bank < soc_data->no_banks; bank++) { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 273 | struct sunxi_gpio_plat *plat; |
Simon Glass | 7830453 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 274 | struct udevice *dev; |
| 275 | |
| 276 | plat = calloc(1, sizeof(*plat)); |
| 277 | if (!plat) |
| 278 | return -ENOMEM; |
| 279 | plat->regs = &ctlr->gpio_bank[bank]; |
Stephen Warren | b56989e | 2016-05-11 15:26:25 -0600 | [diff] [blame] | 280 | plat->bank_name = gpio_bank_name(soc_data->start + bank); |
Simon Glass | 7830453 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 281 | plat->gpio_count = SUNXI_GPIOS_PER_BANK; |
| 282 | |
Simon Glass | 6996c66 | 2020-11-28 17:50:03 -0700 | [diff] [blame] | 283 | ret = device_bind(parent, parent->driver, plat->bank_name, plat, |
Simon Glass | 9030b39 | 2020-11-28 17:50:04 -0700 | [diff] [blame] | 284 | dev_ofnode(parent), &dev); |
Simon Glass | 7830453 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 285 | if (ret) |
| 286 | return ret; |
Simon Glass | 7830453 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
Stephen Warren | b56989e | 2016-05-11 15:26:25 -0600 | [diff] [blame] | 292 | static const struct sunxi_gpio_soc_data soc_data_a_all = { |
| 293 | .start = 0, |
| 294 | .no_banks = SUNXI_GPIO_BANKS, |
| 295 | }; |
| 296 | |
| 297 | static const struct sunxi_gpio_soc_data soc_data_l_1 = { |
| 298 | .start = 'L' - 'A', |
| 299 | .no_banks = 1, |
| 300 | }; |
| 301 | |
| 302 | static const struct sunxi_gpio_soc_data soc_data_l_2 = { |
| 303 | .start = 'L' - 'A', |
| 304 | .no_banks = 2, |
| 305 | }; |
| 306 | |
| 307 | static const struct sunxi_gpio_soc_data soc_data_l_3 = { |
| 308 | .start = 'L' - 'A', |
| 309 | .no_banks = 3, |
| 310 | }; |
| 311 | |
| 312 | #define ID(_compat_, _soc_data_) \ |
| 313 | { .compatible = _compat_, .data = (ulong)&soc_data_##_soc_data_ } |
| 314 | |
Simon Glass | 7830453 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 315 | static const struct udevice_id sunxi_gpio_ids[] = { |
Stephen Warren | b56989e | 2016-05-11 15:26:25 -0600 | [diff] [blame] | 316 | ID("allwinner,sun4i-a10-pinctrl", a_all), |
| 317 | ID("allwinner,sun5i-a10s-pinctrl", a_all), |
| 318 | ID("allwinner,sun5i-a13-pinctrl", a_all), |
Chris Blake | e3b0a65 | 2018-01-15 16:28:18 -0600 | [diff] [blame] | 319 | ID("allwinner,sun50i-h5-pinctrl", a_all), |
Stephen Warren | b56989e | 2016-05-11 15:26:25 -0600 | [diff] [blame] | 320 | ID("allwinner,sun6i-a31-pinctrl", a_all), |
| 321 | ID("allwinner,sun6i-a31s-pinctrl", a_all), |
| 322 | ID("allwinner,sun7i-a20-pinctrl", a_all), |
| 323 | ID("allwinner,sun8i-a23-pinctrl", a_all), |
| 324 | ID("allwinner,sun8i-a33-pinctrl", a_all), |
| 325 | ID("allwinner,sun8i-a83t-pinctrl", a_all), |
| 326 | ID("allwinner,sun8i-h3-pinctrl", a_all), |
Chen-Yu Tsai | c9d9774 | 2016-11-30 17:23:52 +0800 | [diff] [blame] | 327 | ID("allwinner,sun8i-r40-pinctrl", a_all), |
Icenowy Zheng | 5e92e97 | 2020-10-26 22:18:01 +0800 | [diff] [blame] | 328 | ID("allwinner,sun8i-v3-pinctrl", a_all), |
Andre Przywara | 4088d82 | 2018-04-04 01:31:14 +0100 | [diff] [blame] | 329 | ID("allwinner,sun8i-v3s-pinctrl", a_all), |
Stephen Warren | b56989e | 2016-05-11 15:26:25 -0600 | [diff] [blame] | 330 | ID("allwinner,sun9i-a80-pinctrl", a_all), |
Andre Przywara | 4088d82 | 2018-04-04 01:31:14 +0100 | [diff] [blame] | 331 | ID("allwinner,sun50i-a64-pinctrl", a_all), |
Andre Przywara | 60c8417 | 2019-06-23 15:09:47 +0100 | [diff] [blame] | 332 | ID("allwinner,sun50i-h6-pinctrl", a_all), |
Jernej Skrabec | 91dd215 | 2021-01-11 21:11:51 +0100 | [diff] [blame] | 333 | ID("allwinner,sun50i-h616-pinctrl", a_all), |
Stephen Warren | b56989e | 2016-05-11 15:26:25 -0600 | [diff] [blame] | 334 | ID("allwinner,sun6i-a31-r-pinctrl", l_2), |
| 335 | ID("allwinner,sun8i-a23-r-pinctrl", l_1), |
| 336 | ID("allwinner,sun8i-a83t-r-pinctrl", l_1), |
| 337 | ID("allwinner,sun8i-h3-r-pinctrl", l_1), |
| 338 | ID("allwinner,sun9i-a80-r-pinctrl", l_3), |
Andre Przywara | 4088d82 | 2018-04-04 01:31:14 +0100 | [diff] [blame] | 339 | ID("allwinner,sun50i-a64-r-pinctrl", l_1), |
Andre Przywara | 60c8417 | 2019-06-23 15:09:47 +0100 | [diff] [blame] | 340 | ID("allwinner,sun50i-h6-r-pinctrl", l_2), |
Jernej Skrabec | 91dd215 | 2021-01-11 21:11:51 +0100 | [diff] [blame] | 341 | ID("allwinner,sun50i-h616-r-pinctrl", l_1), |
Simon Glass | 7830453 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 342 | { } |
| 343 | }; |
| 344 | |
| 345 | U_BOOT_DRIVER(gpio_sunxi) = { |
| 346 | .name = "gpio_sunxi", |
| 347 | .id = UCLASS_GPIO, |
| 348 | .ops = &gpio_sunxi_ops, |
| 349 | .of_match = sunxi_gpio_ids, |
| 350 | .bind = gpio_sunxi_bind, |
| 351 | .probe = gpio_sunxi_probe, |
| 352 | }; |
Simon Glass | fa4689a | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 353 | #endif /* DM_GPIO */ |