Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2009 |
| 4 | * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de> |
| 5 | * |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 6 | * Copyright (C) 2011 |
| 7 | * Stefano Babic, DENX Software Engineering, <sbabic@denx.de> |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 8 | */ |
| 9 | #include <common.h> |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 10 | #include <errno.h> |
| 11 | #include <dm.h> |
| 12 | #include <malloc.h> |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 13 | #include <asm/arch/imx-regs.h> |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 14 | #include <asm/gpio.h> |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 15 | #include <asm/io.h> |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 16 | |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 17 | enum mxc_gpio_direction { |
| 18 | MXC_GPIO_DIRECTION_IN, |
| 19 | MXC_GPIO_DIRECTION_OUT, |
| 20 | }; |
| 21 | |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 22 | #define GPIO_PER_BANK 32 |
| 23 | |
| 24 | struct mxc_gpio_plat { |
Peng Fan | 637a769 | 2015-02-10 14:46:33 +0800 | [diff] [blame] | 25 | int bank_index; |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 26 | struct gpio_regs *regs; |
| 27 | }; |
| 28 | |
| 29 | struct mxc_bank_info { |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 30 | struct gpio_regs *regs; |
| 31 | }; |
| 32 | |
Simon Glass | bcee8d6 | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 33 | #if !CONFIG_IS_ENABLED(DM_GPIO) |
Lukasz Majewski | 0194137 | 2019-06-09 22:54:40 +0200 | [diff] [blame] | 34 | #define GPIO_TO_PORT(n) ((n) / 32) |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 35 | |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 36 | /* GPIO port description */ |
| 37 | static unsigned long gpio_ports[] = { |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 38 | [0] = GPIO1_BASE_ADDR, |
| 39 | [1] = GPIO2_BASE_ADDR, |
| 40 | [2] = GPIO3_BASE_ADDR, |
trem | e71c39d | 2012-08-25 05:30:33 +0000 | [diff] [blame] | 41 | #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \ |
Adrian Alonso | 26dd346 | 2015-08-11 11:19:51 -0500 | [diff] [blame] | 42 | defined(CONFIG_MX53) || defined(CONFIG_MX6) || \ |
Peng Fan | cd357ad | 2018-11-20 10:19:25 +0000 | [diff] [blame] | 43 | defined(CONFIG_MX7) || defined(CONFIG_IMX8M) || \ |
Giulio Benetti | abd98e0 | 2020-01-10 15:47:03 +0100 | [diff] [blame] | 44 | defined(CONFIG_ARCH_IMX8) || defined(CONFIG_IMXRT1050) |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 45 | [3] = GPIO4_BASE_ADDR, |
| 46 | #endif |
Adrian Alonso | 26dd346 | 2015-08-11 11:19:51 -0500 | [diff] [blame] | 47 | #if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6) || \ |
Peng Fan | cd357ad | 2018-11-20 10:19:25 +0000 | [diff] [blame] | 48 | defined(CONFIG_MX7) || defined(CONFIG_IMX8M) || \ |
Giulio Benetti | abd98e0 | 2020-01-10 15:47:03 +0100 | [diff] [blame] | 49 | defined(CONFIG_ARCH_IMX8) || defined(CONFIG_IMXRT1050) |
Liu Hui-R64343 | 01643ec | 2011-01-03 22:27:38 +0000 | [diff] [blame] | 50 | [4] = GPIO5_BASE_ADDR, |
Giulio Benetti | abd98e0 | 2020-01-10 15:47:03 +0100 | [diff] [blame] | 51 | #if !(defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL) || \ |
| 52 | defined(CONFIG_IMX8M) || defined(CONFIG_IMXRT1050)) |
Liu Hui-R64343 | 01643ec | 2011-01-03 22:27:38 +0000 | [diff] [blame] | 53 | [5] = GPIO6_BASE_ADDR, |
trem | e71c39d | 2012-08-25 05:30:33 +0000 | [diff] [blame] | 54 | #endif |
Peng Fan | f2753b0 | 2015-07-20 19:28:31 +0800 | [diff] [blame] | 55 | #endif |
Peng Fan | 8b2a31f | 2018-10-18 14:28:27 +0200 | [diff] [blame] | 56 | #if defined(CONFIG_MX53) || defined(CONFIG_MX6) || defined(CONFIG_MX7) || \ |
| 57 | defined(CONFIG_ARCH_IMX8) |
Fabio Estevam | 290e7cf | 2018-01-03 12:33:05 -0200 | [diff] [blame] | 58 | #if !(defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)) |
Liu Hui-R64343 | 01643ec | 2011-01-03 22:27:38 +0000 | [diff] [blame] | 59 | [6] = GPIO7_BASE_ADDR, |
| 60 | #endif |
Peng Fan | f2753b0 | 2015-07-20 19:28:31 +0800 | [diff] [blame] | 61 | #endif |
Peng Fan | 8b2a31f | 2018-10-18 14:28:27 +0200 | [diff] [blame] | 62 | #if defined(CONFIG_ARCH_IMX8) |
| 63 | [7] = GPIO8_BASE_ADDR, |
| 64 | #endif |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 65 | }; |
| 66 | |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 67 | static int mxc_gpio_direction(unsigned int gpio, |
| 68 | enum mxc_gpio_direction direction) |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 69 | { |
Vikram Narayanan | be28255 | 2012-04-10 04:26:20 +0000 | [diff] [blame] | 70 | unsigned int port = GPIO_TO_PORT(gpio); |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 71 | struct gpio_regs *regs; |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 72 | u32 l; |
| 73 | |
| 74 | if (port >= ARRAY_SIZE(gpio_ports)) |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 75 | return -1; |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 76 | |
| 77 | gpio &= 0x1f; |
| 78 | |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 79 | regs = (struct gpio_regs *)gpio_ports[port]; |
| 80 | |
| 81 | l = readl(®s->gpio_dir); |
| 82 | |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 83 | switch (direction) { |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 84 | case MXC_GPIO_DIRECTION_OUT: |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 85 | l |= 1 << gpio; |
| 86 | break; |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 87 | case MXC_GPIO_DIRECTION_IN: |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 88 | l &= ~(1 << gpio); |
| 89 | } |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 90 | writel(l, ®s->gpio_dir); |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 95 | int gpio_set_value(unsigned gpio, int value) |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 96 | { |
Vikram Narayanan | be28255 | 2012-04-10 04:26:20 +0000 | [diff] [blame] | 97 | unsigned int port = GPIO_TO_PORT(gpio); |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 98 | struct gpio_regs *regs; |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 99 | u32 l; |
| 100 | |
| 101 | if (port >= ARRAY_SIZE(gpio_ports)) |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 102 | return -1; |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 103 | |
| 104 | gpio &= 0x1f; |
| 105 | |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 106 | regs = (struct gpio_regs *)gpio_ports[port]; |
| 107 | |
| 108 | l = readl(®s->gpio_dr); |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 109 | if (value) |
| 110 | l |= 1 << gpio; |
| 111 | else |
| 112 | l &= ~(1 << gpio); |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 113 | writel(l, ®s->gpio_dr); |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 114 | |
| 115 | return 0; |
Guennadi Liakhovetski | b30de3c | 2009-02-07 01:18:07 +0100 | [diff] [blame] | 116 | } |
Stefano Babic | 7d27cd0 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 117 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 118 | int gpio_get_value(unsigned gpio) |
Stefano Babic | 7d27cd0 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 119 | { |
Vikram Narayanan | be28255 | 2012-04-10 04:26:20 +0000 | [diff] [blame] | 120 | unsigned int port = GPIO_TO_PORT(gpio); |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 121 | struct gpio_regs *regs; |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 122 | u32 val; |
Stefano Babic | 7d27cd0 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 123 | |
| 124 | if (port >= ARRAY_SIZE(gpio_ports)) |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 125 | return -1; |
Stefano Babic | 7d27cd0 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 126 | |
| 127 | gpio &= 0x1f; |
| 128 | |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 129 | regs = (struct gpio_regs *)gpio_ports[port]; |
| 130 | |
Benoît Thébaudeau | 5dafa45 | 2012-08-20 10:55:41 +0000 | [diff] [blame] | 131 | val = (readl(®s->gpio_psr) >> gpio) & 0x01; |
Stefano Babic | 7d27cd0 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 132 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 133 | return val; |
Stefano Babic | 7d27cd0 | 2010-04-13 12:07:00 +0200 | [diff] [blame] | 134 | } |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 135 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 136 | int gpio_request(unsigned gpio, const char *label) |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 137 | { |
Vikram Narayanan | be28255 | 2012-04-10 04:26:20 +0000 | [diff] [blame] | 138 | unsigned int port = GPIO_TO_PORT(gpio); |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 139 | if (port >= ARRAY_SIZE(gpio_ports)) |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 140 | return -1; |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 141 | return 0; |
| 142 | } |
| 143 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 144 | int gpio_free(unsigned gpio) |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 145 | { |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 146 | return 0; |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 147 | } |
| 148 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 149 | int gpio_direction_input(unsigned gpio) |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 150 | { |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 151 | return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_IN); |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 152 | } |
| 153 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 154 | int gpio_direction_output(unsigned gpio, int value) |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 155 | { |
Dirk Behme | 04c79cb | 2013-07-15 15:58:27 +0200 | [diff] [blame] | 156 | int ret = gpio_set_value(gpio, value); |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 157 | |
| 158 | if (ret < 0) |
| 159 | return ret; |
| 160 | |
Dirk Behme | 04c79cb | 2013-07-15 15:58:27 +0200 | [diff] [blame] | 161 | return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_OUT); |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 162 | } |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 163 | #endif |
| 164 | |
Simon Glass | bcee8d6 | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 165 | #if CONFIG_IS_ENABLED(DM_GPIO) |
Peng Fan | 99c0ae1 | 2015-02-10 14:46:34 +0800 | [diff] [blame] | 166 | #include <fdtdec.h> |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 167 | static int mxc_gpio_is_output(struct gpio_regs *regs, int offset) |
| 168 | { |
| 169 | u32 val; |
| 170 | |
| 171 | val = readl(®s->gpio_dir); |
| 172 | |
| 173 | return val & (1 << offset) ? 1 : 0; |
| 174 | } |
| 175 | |
| 176 | static void mxc_gpio_bank_direction(struct gpio_regs *regs, int offset, |
| 177 | enum mxc_gpio_direction direction) |
| 178 | { |
| 179 | u32 l; |
| 180 | |
| 181 | l = readl(®s->gpio_dir); |
| 182 | |
| 183 | switch (direction) { |
| 184 | case MXC_GPIO_DIRECTION_OUT: |
| 185 | l |= 1 << offset; |
| 186 | break; |
| 187 | case MXC_GPIO_DIRECTION_IN: |
| 188 | l &= ~(1 << offset); |
| 189 | } |
| 190 | writel(l, ®s->gpio_dir); |
| 191 | } |
| 192 | |
| 193 | static void mxc_gpio_bank_set_value(struct gpio_regs *regs, int offset, |
| 194 | int value) |
| 195 | { |
| 196 | u32 l; |
| 197 | |
| 198 | l = readl(®s->gpio_dr); |
| 199 | if (value) |
| 200 | l |= 1 << offset; |
| 201 | else |
| 202 | l &= ~(1 << offset); |
| 203 | writel(l, ®s->gpio_dr); |
| 204 | } |
| 205 | |
| 206 | static int mxc_gpio_bank_get_value(struct gpio_regs *regs, int offset) |
| 207 | { |
| 208 | return (readl(®s->gpio_psr) >> offset) & 0x01; |
| 209 | } |
| 210 | |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 211 | /* set GPIO pin 'gpio' as an input */ |
| 212 | static int mxc_gpio_direction_input(struct udevice *dev, unsigned offset) |
| 213 | { |
| 214 | struct mxc_bank_info *bank = dev_get_priv(dev); |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 215 | |
| 216 | /* Configure GPIO direction as input. */ |
| 217 | mxc_gpio_bank_direction(bank->regs, offset, MXC_GPIO_DIRECTION_IN); |
| 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | /* set GPIO pin 'gpio' as an output, with polarity 'value' */ |
| 223 | static int mxc_gpio_direction_output(struct udevice *dev, unsigned offset, |
| 224 | int value) |
| 225 | { |
| 226 | struct mxc_bank_info *bank = dev_get_priv(dev); |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 227 | |
| 228 | /* Configure GPIO output value. */ |
| 229 | mxc_gpio_bank_set_value(bank->regs, offset, value); |
| 230 | |
| 231 | /* Configure GPIO direction as output. */ |
| 232 | mxc_gpio_bank_direction(bank->regs, offset, MXC_GPIO_DIRECTION_OUT); |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | /* read GPIO IN value of pin 'gpio' */ |
| 238 | static int mxc_gpio_get_value(struct udevice *dev, unsigned offset) |
| 239 | { |
| 240 | struct mxc_bank_info *bank = dev_get_priv(dev); |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 241 | |
| 242 | return mxc_gpio_bank_get_value(bank->regs, offset); |
| 243 | } |
| 244 | |
| 245 | /* write GPIO OUT value to pin 'gpio' */ |
| 246 | static int mxc_gpio_set_value(struct udevice *dev, unsigned offset, |
| 247 | int value) |
| 248 | { |
| 249 | struct mxc_bank_info *bank = dev_get_priv(dev); |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 250 | |
| 251 | mxc_gpio_bank_set_value(bank->regs, offset, value); |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 256 | static int mxc_gpio_get_function(struct udevice *dev, unsigned offset) |
| 257 | { |
| 258 | struct mxc_bank_info *bank = dev_get_priv(dev); |
| 259 | |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 260 | /* GPIOF_FUNC is not implemented yet */ |
| 261 | if (mxc_gpio_is_output(bank->regs, offset)) |
| 262 | return GPIOF_OUTPUT; |
| 263 | else |
| 264 | return GPIOF_INPUT; |
| 265 | } |
| 266 | |
| 267 | static const struct dm_gpio_ops gpio_mxc_ops = { |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 268 | .direction_input = mxc_gpio_direction_input, |
| 269 | .direction_output = mxc_gpio_direction_output, |
| 270 | .get_value = mxc_gpio_get_value, |
| 271 | .set_value = mxc_gpio_set_value, |
| 272 | .get_function = mxc_gpio_get_function, |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 273 | }; |
| 274 | |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 275 | static int mxc_gpio_probe(struct udevice *dev) |
| 276 | { |
| 277 | struct mxc_bank_info *bank = dev_get_priv(dev); |
| 278 | struct mxc_gpio_plat *plat = dev_get_platdata(dev); |
Simon Glass | e564f05 | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 279 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 280 | int banknum; |
| 281 | char name[18], *str; |
| 282 | |
Peng Fan | 637a769 | 2015-02-10 14:46:33 +0800 | [diff] [blame] | 283 | banknum = plat->bank_index; |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 284 | sprintf(name, "GPIO%d_", banknum + 1); |
| 285 | str = strdup(name); |
| 286 | if (!str) |
| 287 | return -ENOMEM; |
| 288 | uc_priv->bank_name = str; |
| 289 | uc_priv->gpio_count = GPIO_PER_BANK; |
| 290 | bank->regs = plat->regs; |
| 291 | |
| 292 | return 0; |
| 293 | } |
| 294 | |
Peng Fan | 99c0ae1 | 2015-02-10 14:46:34 +0800 | [diff] [blame] | 295 | static int mxc_gpio_bind(struct udevice *dev) |
| 296 | { |
| 297 | struct mxc_gpio_plat *plat = dev->platdata; |
| 298 | fdt_addr_t addr; |
| 299 | |
| 300 | /* |
| 301 | * If platdata already exsits, directly return. |
| 302 | * Actually only when DT is not supported, platdata |
| 303 | * is statically initialized in U_BOOT_DEVICES.Here |
| 304 | * will return. |
| 305 | */ |
| 306 | if (plat) |
| 307 | return 0; |
| 308 | |
Simon Glass | a821c4a | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 309 | addr = devfdt_get_addr(dev); |
Peng Fan | 99c0ae1 | 2015-02-10 14:46:34 +0800 | [diff] [blame] | 310 | if (addr == FDT_ADDR_T_NONE) |
Simon Glass | 7c84319 | 2017-09-17 16:54:53 -0600 | [diff] [blame] | 311 | return -EINVAL; |
Peng Fan | 99c0ae1 | 2015-02-10 14:46:34 +0800 | [diff] [blame] | 312 | |
| 313 | /* |
| 314 | * TODO: |
| 315 | * When every board is converted to driver model and DT is supported, |
| 316 | * this can be done by auto-alloc feature, but not using calloc |
| 317 | * to alloc memory for platdata. |
Simon Glass | 4d68604 | 2017-09-17 16:54:52 -0600 | [diff] [blame] | 318 | * |
| 319 | * For example mxc_plat below uses platform data rather than device |
| 320 | * tree. |
| 321 | * |
| 322 | * NOTE: DO NOT COPY this code if you are using device tree. |
Peng Fan | 99c0ae1 | 2015-02-10 14:46:34 +0800 | [diff] [blame] | 323 | */ |
| 324 | plat = calloc(1, sizeof(*plat)); |
| 325 | if (!plat) |
| 326 | return -ENOMEM; |
| 327 | |
| 328 | plat->regs = (struct gpio_regs *)addr; |
| 329 | plat->bank_index = dev->req_seq; |
| 330 | dev->platdata = plat; |
| 331 | |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | static const struct udevice_id mxc_gpio_ids[] = { |
| 336 | { .compatible = "fsl,imx35-gpio" }, |
| 337 | { } |
| 338 | }; |
| 339 | |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 340 | U_BOOT_DRIVER(gpio_mxc) = { |
| 341 | .name = "gpio_mxc", |
| 342 | .id = UCLASS_GPIO, |
| 343 | .ops = &gpio_mxc_ops, |
| 344 | .probe = mxc_gpio_probe, |
| 345 | .priv_auto_alloc_size = sizeof(struct mxc_bank_info), |
Peng Fan | 99c0ae1 | 2015-02-10 14:46:34 +0800 | [diff] [blame] | 346 | .of_match = mxc_gpio_ids, |
| 347 | .bind = mxc_gpio_bind, |
| 348 | }; |
| 349 | |
Masahiro Yamada | 0f92582 | 2015-08-12 07:31:55 +0900 | [diff] [blame] | 350 | #if !CONFIG_IS_ENABLED(OF_CONTROL) |
Peng Fan | 99c0ae1 | 2015-02-10 14:46:34 +0800 | [diff] [blame] | 351 | static const struct mxc_gpio_plat mxc_plat[] = { |
| 352 | { 0, (struct gpio_regs *)GPIO1_BASE_ADDR }, |
| 353 | { 1, (struct gpio_regs *)GPIO2_BASE_ADDR }, |
| 354 | { 2, (struct gpio_regs *)GPIO3_BASE_ADDR }, |
| 355 | #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \ |
Peng Fan | 8953d86 | 2018-01-10 13:20:42 +0800 | [diff] [blame] | 356 | defined(CONFIG_MX53) || defined(CONFIG_MX6) || \ |
Peng Fan | cd357ad | 2018-11-20 10:19:25 +0000 | [diff] [blame] | 357 | defined(CONFIG_IMX8M) || defined(CONFIG_ARCH_IMX8) |
Peng Fan | 99c0ae1 | 2015-02-10 14:46:34 +0800 | [diff] [blame] | 358 | { 3, (struct gpio_regs *)GPIO4_BASE_ADDR }, |
| 359 | #endif |
Peng Fan | 8953d86 | 2018-01-10 13:20:42 +0800 | [diff] [blame] | 360 | #if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6) || \ |
Peng Fan | cd357ad | 2018-11-20 10:19:25 +0000 | [diff] [blame] | 361 | defined(CONFIG_IMX8M) || defined(CONFIG_ARCH_IMX8) |
Peng Fan | 99c0ae1 | 2015-02-10 14:46:34 +0800 | [diff] [blame] | 362 | { 4, (struct gpio_regs *)GPIO5_BASE_ADDR }, |
Peng Fan | cd357ad | 2018-11-20 10:19:25 +0000 | [diff] [blame] | 363 | #ifndef CONFIG_IMX8M |
Peng Fan | 99c0ae1 | 2015-02-10 14:46:34 +0800 | [diff] [blame] | 364 | { 5, (struct gpio_regs *)GPIO6_BASE_ADDR }, |
| 365 | #endif |
Peng Fan | 8953d86 | 2018-01-10 13:20:42 +0800 | [diff] [blame] | 366 | #endif |
Peng Fan | 8b2a31f | 2018-10-18 14:28:27 +0200 | [diff] [blame] | 367 | #if defined(CONFIG_MX53) || defined(CONFIG_MX6) || defined(CONFIG_ARCH_IMX8) |
Peng Fan | 99c0ae1 | 2015-02-10 14:46:34 +0800 | [diff] [blame] | 368 | { 6, (struct gpio_regs *)GPIO7_BASE_ADDR }, |
| 369 | #endif |
Peng Fan | 8b2a31f | 2018-10-18 14:28:27 +0200 | [diff] [blame] | 370 | #if defined(CONFIG_ARCH_IMX8) |
| 371 | { 7, (struct gpio_regs *)GPIO8_BASE_ADDR }, |
| 372 | #endif |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 373 | }; |
| 374 | |
| 375 | U_BOOT_DEVICES(mxc_gpios) = { |
| 376 | { "gpio_mxc", &mxc_plat[0] }, |
| 377 | { "gpio_mxc", &mxc_plat[1] }, |
| 378 | { "gpio_mxc", &mxc_plat[2] }, |
| 379 | #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \ |
Peng Fan | 8953d86 | 2018-01-10 13:20:42 +0800 | [diff] [blame] | 380 | defined(CONFIG_MX53) || defined(CONFIG_MX6) || \ |
Peng Fan | cd357ad | 2018-11-20 10:19:25 +0000 | [diff] [blame] | 381 | defined(CONFIG_IMX8M) || defined(CONFIG_ARCH_IMX8) |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 382 | { "gpio_mxc", &mxc_plat[3] }, |
| 383 | #endif |
Peng Fan | 8953d86 | 2018-01-10 13:20:42 +0800 | [diff] [blame] | 384 | #if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6) || \ |
Peng Fan | cd357ad | 2018-11-20 10:19:25 +0000 | [diff] [blame] | 385 | defined(CONFIG_IMX8M) || defined(CONFIG_ARCH_IMX8) |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 386 | { "gpio_mxc", &mxc_plat[4] }, |
Peng Fan | cd357ad | 2018-11-20 10:19:25 +0000 | [diff] [blame] | 387 | #ifndef CONFIG_IMX8M |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 388 | { "gpio_mxc", &mxc_plat[5] }, |
| 389 | #endif |
Peng Fan | 8953d86 | 2018-01-10 13:20:42 +0800 | [diff] [blame] | 390 | #endif |
Peng Fan | 8b2a31f | 2018-10-18 14:28:27 +0200 | [diff] [blame] | 391 | #if defined(CONFIG_MX53) || defined(CONFIG_MX6) || defined(CONFIG_ARCH_IMX8) |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 392 | { "gpio_mxc", &mxc_plat[6] }, |
| 393 | #endif |
Peng Fan | 8b2a31f | 2018-10-18 14:28:27 +0200 | [diff] [blame] | 394 | #if defined(CONFIG_ARCH_IMX8) |
| 395 | { "gpio_mxc", &mxc_plat[7] }, |
| 396 | #endif |
Simon Glass | 441d0cf | 2014-10-01 19:57:26 -0600 | [diff] [blame] | 397 | }; |
| 398 | #endif |
Peng Fan | 99c0ae1 | 2015-02-10 14:46:34 +0800 | [diff] [blame] | 399 | #endif |