Stefan Roese | 4751e55 | 2018-10-08 12:38:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2018 Stefan Roese <sr@denx.de> |
| 4 | * |
| 5 | * Based on the Linux driver version which is: |
| 6 | * Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org> |
| 7 | * Copyright (C) 2013 John Crispin <blogic@openwrt.org> |
| 8 | */ |
| 9 | |
Stefan Roese | 4751e55 | 2018-10-08 12:38:01 +0200 | [diff] [blame] | 10 | #include <dm.h> |
| 11 | #include <errno.h> |
| 12 | #include <fdtdec.h> |
| 13 | #include <malloc.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 14 | #include <linux/bitops.h> |
Stefan Roese | 4751e55 | 2018-10-08 12:38:01 +0200 | [diff] [blame] | 15 | #include <linux/io.h> |
| 16 | #include <asm/io.h> |
| 17 | #include <asm/gpio.h> |
| 18 | #include <dm/device-internal.h> |
| 19 | #include <dt-bindings/gpio/gpio.h> |
| 20 | |
| 21 | #define MTK_MAX_BANK 3 |
| 22 | #define MTK_BANK_WIDTH 32 |
| 23 | |
| 24 | enum mediatek_gpio_reg { |
| 25 | GPIO_REG_CTRL = 0, |
| 26 | GPIO_REG_POL, |
| 27 | GPIO_REG_DATA, |
| 28 | GPIO_REG_DSET, |
| 29 | GPIO_REG_DCLR, |
| 30 | GPIO_REG_REDGE, |
| 31 | GPIO_REG_FEDGE, |
| 32 | GPIO_REG_HLVL, |
| 33 | GPIO_REG_LLVL, |
| 34 | GPIO_REG_STAT, |
| 35 | GPIO_REG_EDGE, |
| 36 | }; |
| 37 | |
| 38 | static void __iomem *mediatek_gpio_membase; |
| 39 | |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 40 | struct mediatek_gpio_plat { |
Stefan Roese | 4751e55 | 2018-10-08 12:38:01 +0200 | [diff] [blame] | 41 | char bank_name[3]; /* Name of bank, e.g. "PA", "PB" etc */ |
| 42 | int gpio_count; |
| 43 | int bank; |
| 44 | }; |
| 45 | |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 46 | static u32 reg_offs(struct mediatek_gpio_plat *plat, int reg) |
Stefan Roese | 4751e55 | 2018-10-08 12:38:01 +0200 | [diff] [blame] | 47 | { |
| 48 | return (reg * 0x10) + (plat->bank * 0x4); |
| 49 | } |
| 50 | |
| 51 | static int mediatek_gpio_get_value(struct udevice *dev, unsigned int offset) |
| 52 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 53 | struct mediatek_gpio_plat *plat = dev_get_plat(dev); |
Stefan Roese | 4751e55 | 2018-10-08 12:38:01 +0200 | [diff] [blame] | 54 | |
| 55 | return !!(ioread32(mediatek_gpio_membase + |
| 56 | reg_offs(plat, GPIO_REG_DATA)) & BIT(offset)); |
| 57 | } |
| 58 | |
| 59 | static int mediatek_gpio_set_value(struct udevice *dev, unsigned int offset, |
| 60 | int value) |
| 61 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 62 | struct mediatek_gpio_plat *plat = dev_get_plat(dev); |
Stefan Roese | 4751e55 | 2018-10-08 12:38:01 +0200 | [diff] [blame] | 63 | |
| 64 | iowrite32(BIT(offset), mediatek_gpio_membase + |
| 65 | reg_offs(plat, value ? GPIO_REG_DSET : GPIO_REG_DCLR)); |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | static int mediatek_gpio_direction_input(struct udevice *dev, unsigned int offset) |
| 71 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 72 | struct mediatek_gpio_plat *plat = dev_get_plat(dev); |
Stefan Roese | 4751e55 | 2018-10-08 12:38:01 +0200 | [diff] [blame] | 73 | |
| 74 | clrbits_le32(mediatek_gpio_membase + reg_offs(plat, GPIO_REG_CTRL), |
| 75 | BIT(offset)); |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static int mediatek_gpio_direction_output(struct udevice *dev, unsigned int offset, |
| 81 | int value) |
| 82 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 83 | struct mediatek_gpio_plat *plat = dev_get_plat(dev); |
Stefan Roese | 4751e55 | 2018-10-08 12:38:01 +0200 | [diff] [blame] | 84 | |
| 85 | setbits_le32(mediatek_gpio_membase + reg_offs(plat, GPIO_REG_CTRL), |
| 86 | BIT(offset)); |
| 87 | mediatek_gpio_set_value(dev, offset, value); |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | static int mediatek_gpio_get_function(struct udevice *dev, unsigned int offset) |
| 93 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 94 | struct mediatek_gpio_plat *plat = dev_get_plat(dev); |
Stefan Roese | 4751e55 | 2018-10-08 12:38:01 +0200 | [diff] [blame] | 95 | u32 t; |
| 96 | |
| 97 | t = ioread32(mediatek_gpio_membase + reg_offs(plat, GPIO_REG_CTRL)); |
| 98 | if (t & BIT(offset)) |
| 99 | return GPIOF_OUTPUT; |
| 100 | |
| 101 | return GPIOF_INPUT; |
| 102 | } |
| 103 | |
| 104 | static const struct dm_gpio_ops gpio_mediatek_ops = { |
| 105 | .direction_input = mediatek_gpio_direction_input, |
| 106 | .direction_output = mediatek_gpio_direction_output, |
| 107 | .get_value = mediatek_gpio_get_value, |
| 108 | .set_value = mediatek_gpio_set_value, |
| 109 | .get_function = mediatek_gpio_get_function, |
| 110 | }; |
| 111 | |
| 112 | static int gpio_mediatek_probe(struct udevice *dev) |
| 113 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 114 | struct mediatek_gpio_plat *plat = dev_get_plat(dev); |
Stefan Roese | 4751e55 | 2018-10-08 12:38:01 +0200 | [diff] [blame] | 115 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 116 | |
| 117 | /* Tell the uclass how many GPIOs we have */ |
| 118 | if (plat) { |
| 119 | uc_priv->gpio_count = plat->gpio_count; |
| 120 | uc_priv->bank_name = plat->bank_name; |
| 121 | } |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * We have a top-level GPIO device with no actual GPIOs. It has a child |
| 128 | * device for each Mediatek bank. |
| 129 | */ |
| 130 | static int gpio_mediatek_bind(struct udevice *parent) |
| 131 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 132 | struct mediatek_gpio_plat *plat = dev_get_plat(parent); |
Stefan Roese | 4751e55 | 2018-10-08 12:38:01 +0200 | [diff] [blame] | 133 | ofnode node; |
| 134 | int bank = 0; |
| 135 | int ret; |
| 136 | |
| 137 | /* If this is a child device, there is nothing to do here */ |
| 138 | if (plat) |
| 139 | return 0; |
| 140 | |
| 141 | mediatek_gpio_membase = dev_remap_addr(parent); |
| 142 | if (!mediatek_gpio_membase) |
| 143 | return -EINVAL; |
| 144 | |
| 145 | for (node = dev_read_first_subnode(parent); ofnode_valid(node); |
| 146 | node = dev_read_next_subnode(node)) { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 147 | struct mediatek_gpio_plat *plat; |
Stefan Roese | 4751e55 | 2018-10-08 12:38:01 +0200 | [diff] [blame] | 148 | struct udevice *dev; |
| 149 | |
| 150 | plat = calloc(1, sizeof(*plat)); |
| 151 | if (!plat) |
| 152 | return -ENOMEM; |
| 153 | plat->bank_name[0] = 'P'; |
| 154 | plat->bank_name[1] = 'A' + bank; |
| 155 | plat->bank_name[2] = '\0'; |
| 156 | plat->gpio_count = MTK_BANK_WIDTH; |
| 157 | plat->bank = bank; |
| 158 | |
Simon Glass | a2703ce | 2020-11-28 17:50:03 -0700 | [diff] [blame] | 159 | ret = device_bind(parent, parent->driver, plat->bank_name, plat, |
Simon Glass | 20da4e0 | 2020-11-28 17:50:04 -0700 | [diff] [blame] | 160 | node, &dev); |
Stefan Roese | 4751e55 | 2018-10-08 12:38:01 +0200 | [diff] [blame] | 161 | if (ret) |
| 162 | return ret; |
| 163 | |
Stefan Roese | 4751e55 | 2018-10-08 12:38:01 +0200 | [diff] [blame] | 164 | bank++; |
| 165 | } |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | static const struct udevice_id mediatek_gpio_ids[] = { |
| 171 | { .compatible = "mtk,mt7621-gpio" }, |
| 172 | { } |
| 173 | }; |
| 174 | |
| 175 | U_BOOT_DRIVER(gpio_mediatek) = { |
| 176 | .name = "gpio_mediatek", |
| 177 | .id = UCLASS_GPIO, |
| 178 | .ops = &gpio_mediatek_ops, |
| 179 | .of_match = mediatek_gpio_ids, |
| 180 | .bind = gpio_mediatek_bind, |
| 181 | .probe = gpio_mediatek_probe, |
| 182 | }; |