Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2009 Wind River Systems, Inc. |
| 4 | * Tom Rix <Tom.Rix@windriver.com> |
| 5 | * |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 6 | * This work is derived from the linux 2.6.27 kernel source |
| 7 | * To fetch, use the kernel repository |
| 8 | * git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git |
| 9 | * Use the v2.6.27 tag. |
| 10 | * |
| 11 | * Below is the original's header including its copyright |
| 12 | * |
| 13 | * linux/arch/arm/plat-omap/gpio.c |
| 14 | * |
| 15 | * Support functions for OMAP GPIO |
| 16 | * |
| 17 | * Copyright (C) 2003-2005 Nokia Corporation |
| 18 | * Written by Juha Yrjölä <juha.yrjola@nokia.com> |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 19 | */ |
| 20 | #include <common.h> |
Simon Glass | 1a96d7f | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 21 | #include <dm.h> |
Tom Rini | 7bc2bca | 2015-07-31 19:55:09 -0400 | [diff] [blame] | 22 | #include <fdtdec.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 23 | #include <asm/global_data.h> |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 24 | #include <asm/gpio.h> |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 25 | #include <asm/io.h> |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 26 | #include <dm/device-internal.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 27 | #include <linux/errno.h> |
Tom Rini | 7bc2bca | 2015-07-31 19:55:09 -0400 | [diff] [blame] | 28 | #include <malloc.h> |
| 29 | |
| 30 | DECLARE_GLOBAL_DATA_PTR; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 31 | |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 32 | #define OMAP_GPIO_DIR_OUT 0 |
| 33 | #define OMAP_GPIO_DIR_IN 1 |
| 34 | |
Simon Glass | fa4689a | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 35 | #if CONFIG_IS_ENABLED(DM_GPIO) |
Simon Glass | 1a96d7f | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 36 | |
Simon Glass | 1a96d7f | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 37 | #define GPIO_PER_BANK 32 |
| 38 | |
| 39 | struct gpio_bank { |
Simon Glass | 1a96d7f | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 40 | /* TODO(sjg@chromium.org): Can we use a struct here? */ |
| 41 | void *base; /* address of registers in physical memory */ |
Simon Glass | 1a96d7f | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | #endif |
| 45 | |
Nikita Kiryanov | e3cb9c1 | 2012-11-27 22:40:57 +0000 | [diff] [blame] | 46 | int gpio_is_valid(int gpio) |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 47 | { |
Axel Lin | 01a461f | 2013-06-21 18:54:25 +0800 | [diff] [blame] | 48 | return (gpio >= 0) && (gpio < OMAP_MAX_GPIO); |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 49 | } |
| 50 | |
Aneesh V | 9a39088 | 2011-07-21 09:29:29 -0400 | [diff] [blame] | 51 | static void _set_gpio_direction(const struct gpio_bank *bank, int gpio, |
| 52 | int is_input) |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 53 | { |
| 54 | void *reg = bank->base; |
| 55 | u32 l; |
| 56 | |
Tom Rini | 7bc2bca | 2015-07-31 19:55:09 -0400 | [diff] [blame] | 57 | reg += OMAP_GPIO_OE; |
| 58 | |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 59 | l = __raw_readl(reg); |
| 60 | if (is_input) |
| 61 | l |= 1 << gpio; |
| 62 | else |
| 63 | l &= ~(1 << gpio); |
| 64 | __raw_writel(l, reg); |
| 65 | } |
| 66 | |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 67 | /** |
| 68 | * Get the direction of the GPIO by reading the GPIO_OE register |
| 69 | * corresponding to the specified bank. |
| 70 | */ |
| 71 | static int _get_gpio_direction(const struct gpio_bank *bank, int gpio) |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 72 | { |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 73 | void *reg = bank->base; |
| 74 | u32 v; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 75 | |
Tom Rini | 7bc2bca | 2015-07-31 19:55:09 -0400 | [diff] [blame] | 76 | reg += OMAP_GPIO_OE; |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 77 | |
| 78 | v = __raw_readl(reg); |
| 79 | |
| 80 | if (v & (1 << gpio)) |
| 81 | return OMAP_GPIO_DIR_IN; |
| 82 | else |
| 83 | return OMAP_GPIO_DIR_OUT; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Aneesh V | 9a39088 | 2011-07-21 09:29:29 -0400 | [diff] [blame] | 86 | static void _set_gpio_dataout(const struct gpio_bank *bank, int gpio, |
| 87 | int enable) |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 88 | { |
| 89 | void *reg = bank->base; |
| 90 | u32 l = 0; |
| 91 | |
Tom Rini | 7bc2bca | 2015-07-31 19:55:09 -0400 | [diff] [blame] | 92 | if (enable) |
| 93 | reg += OMAP_GPIO_SETDATAOUT; |
| 94 | else |
| 95 | reg += OMAP_GPIO_CLEARDATAOUT; |
| 96 | |
| 97 | l = 1 << gpio; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 98 | __raw_writel(l, reg); |
| 99 | } |
| 100 | |
Simon Glass | ba33a3f | 2014-10-22 21:37:08 -0600 | [diff] [blame] | 101 | static int _get_gpio_value(const struct gpio_bank *bank, int gpio) |
| 102 | { |
| 103 | void *reg = bank->base; |
| 104 | int input; |
| 105 | |
Tom Rini | 7bc2bca | 2015-07-31 19:55:09 -0400 | [diff] [blame] | 106 | input = _get_gpio_direction(bank, gpio); |
| 107 | switch (input) { |
| 108 | case OMAP_GPIO_DIR_IN: |
| 109 | reg += OMAP_GPIO_DATAIN; |
| 110 | break; |
| 111 | case OMAP_GPIO_DIR_OUT: |
| 112 | reg += OMAP_GPIO_DATAOUT; |
Simon Glass | ba33a3f | 2014-10-22 21:37:08 -0600 | [diff] [blame] | 113 | break; |
| 114 | default: |
| 115 | return -1; |
| 116 | } |
| 117 | |
| 118 | return (__raw_readl(reg) & (1 << gpio)) != 0; |
| 119 | } |
| 120 | |
Simon Glass | fa4689a | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 121 | #if !CONFIG_IS_ENABLED(DM_GPIO) |
Tom Rini | 74d6c9a | 2020-06-04 16:01:39 -0400 | [diff] [blame] | 122 | static inline int get_gpio_index(int gpio) |
| 123 | { |
| 124 | return gpio & 0x1f; |
| 125 | } |
Simon Glass | 1a96d7f | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 126 | |
Simon Glass | ba33a3f | 2014-10-22 21:37:08 -0600 | [diff] [blame] | 127 | static inline const struct gpio_bank *get_gpio_bank(int gpio) |
| 128 | { |
| 129 | return &omap_gpio_bank[gpio >> 5]; |
| 130 | } |
| 131 | |
| 132 | static int check_gpio(int gpio) |
| 133 | { |
| 134 | if (!gpio_is_valid(gpio)) { |
| 135 | printf("ERROR : check_gpio: invalid GPIO %d\n", gpio); |
| 136 | return -1; |
| 137 | } |
| 138 | return 0; |
| 139 | } |
| 140 | |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 141 | /** |
| 142 | * Set value of the specified gpio |
| 143 | */ |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 144 | int gpio_set_value(unsigned gpio, int value) |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 145 | { |
Aneesh V | 9a39088 | 2011-07-21 09:29:29 -0400 | [diff] [blame] | 146 | const struct gpio_bank *bank; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 147 | |
| 148 | if (check_gpio(gpio) < 0) |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 149 | return -1; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 150 | bank = get_gpio_bank(gpio); |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 151 | _set_gpio_dataout(bank, get_gpio_index(gpio), value); |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 152 | |
| 153 | return 0; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 154 | } |
| 155 | |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 156 | /** |
| 157 | * Get value of the specified gpio |
| 158 | */ |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 159 | int gpio_get_value(unsigned gpio) |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 160 | { |
Aneesh V | 9a39088 | 2011-07-21 09:29:29 -0400 | [diff] [blame] | 161 | const struct gpio_bank *bank; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 162 | |
| 163 | if (check_gpio(gpio) < 0) |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 164 | return -1; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 165 | bank = get_gpio_bank(gpio); |
Simon Glass | ba33a3f | 2014-10-22 21:37:08 -0600 | [diff] [blame] | 166 | |
| 167 | return _get_gpio_value(bank, get_gpio_index(gpio)); |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 168 | } |
| 169 | |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 170 | /** |
| 171 | * Set gpio direction as input |
| 172 | */ |
| 173 | int gpio_direction_input(unsigned gpio) |
Joel A Fernandes | 46b9c20 | 2011-09-04 11:10:03 -0500 | [diff] [blame] | 174 | { |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 175 | const struct gpio_bank *bank; |
Joel A Fernandes | 46b9c20 | 2011-09-04 11:10:03 -0500 | [diff] [blame] | 176 | |
| 177 | if (check_gpio(gpio) < 0) |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 178 | return -1; |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 179 | |
Joel A Fernandes | 46b9c20 | 2011-09-04 11:10:03 -0500 | [diff] [blame] | 180 | bank = get_gpio_bank(gpio); |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 181 | _set_gpio_direction(bank, get_gpio_index(gpio), 1); |
| 182 | |
| 183 | return 0; |
Joel A Fernandes | 46b9c20 | 2011-09-04 11:10:03 -0500 | [diff] [blame] | 184 | } |
| 185 | |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 186 | /** |
| 187 | * Set gpio direction as output |
| 188 | */ |
| 189 | int gpio_direction_output(unsigned gpio, int value) |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 190 | { |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 191 | const struct gpio_bank *bank; |
| 192 | |
| 193 | if (check_gpio(gpio) < 0) |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 194 | return -1; |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 195 | |
| 196 | bank = get_gpio_bank(gpio); |
| 197 | _set_gpio_dataout(bank, get_gpio_index(gpio), value); |
| 198 | _set_gpio_direction(bank, get_gpio_index(gpio), 0); |
| 199 | |
| 200 | return 0; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 201 | } |
| 202 | |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 203 | /** |
| 204 | * Request a gpio before using it. |
| 205 | * |
| 206 | * NOTE: Argument 'label' is unused. |
| 207 | */ |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 208 | int gpio_request(unsigned gpio, const char *label) |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 209 | { |
| 210 | if (check_gpio(gpio) < 0) |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 211 | return -1; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
Sanjeev Premi | ea2cf55 | 2011-09-08 10:47:25 -0400 | [diff] [blame] | 216 | /** |
| 217 | * Reset and free the gpio after using it. |
| 218 | */ |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 219 | int gpio_free(unsigned gpio) |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 220 | { |
Simon Glass | 1a96d7f | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | #else /* new driver model interface CONFIG_DM_GPIO */ |
| 225 | |
Simon Glass | 1a96d7f | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 226 | /* set GPIO pin 'gpio' as an input */ |
| 227 | static int omap_gpio_direction_input(struct udevice *dev, unsigned offset) |
| 228 | { |
| 229 | struct gpio_bank *bank = dev_get_priv(dev); |
Simon Glass | 1a96d7f | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 230 | |
| 231 | /* Configure GPIO direction as input. */ |
| 232 | _set_gpio_direction(bank, offset, 1); |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | /* set GPIO pin 'gpio' as an output, with polarity 'value' */ |
| 238 | static int omap_gpio_direction_output(struct udevice *dev, unsigned offset, |
| 239 | int value) |
| 240 | { |
| 241 | struct gpio_bank *bank = dev_get_priv(dev); |
Simon Glass | 1a96d7f | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 242 | |
| 243 | _set_gpio_dataout(bank, offset, value); |
| 244 | _set_gpio_direction(bank, offset, 0); |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | /* read GPIO IN value of pin 'gpio' */ |
| 250 | static int omap_gpio_get_value(struct udevice *dev, unsigned offset) |
| 251 | { |
| 252 | struct gpio_bank *bank = dev_get_priv(dev); |
Simon Glass | 1a96d7f | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 253 | |
| 254 | return _get_gpio_value(bank, offset); |
| 255 | } |
| 256 | |
| 257 | /* write GPIO OUT value to pin 'gpio' */ |
| 258 | static int omap_gpio_set_value(struct udevice *dev, unsigned offset, |
| 259 | int value) |
| 260 | { |
| 261 | struct gpio_bank *bank = dev_get_priv(dev); |
Simon Glass | 1a96d7f | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 262 | |
| 263 | _set_gpio_dataout(bank, offset, value); |
| 264 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 265 | return 0; |
Tom Rix | dd2d1cc | 2009-05-15 23:48:36 +0200 | [diff] [blame] | 266 | } |
Simon Glass | 1a96d7f | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 267 | |
Simon Glass | 1a96d7f | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 268 | static int omap_gpio_get_function(struct udevice *dev, unsigned offset) |
| 269 | { |
| 270 | struct gpio_bank *bank = dev_get_priv(dev); |
| 271 | |
Simon Glass | 1a96d7f | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 272 | /* GPIOF_FUNC is not implemented yet */ |
Axel Lin | bb4ab58 | 2015-01-31 22:23:38 +0800 | [diff] [blame] | 273 | if (_get_gpio_direction(bank, offset) == OMAP_GPIO_DIR_OUT) |
Simon Glass | 1a96d7f | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 274 | return GPIOF_OUTPUT; |
| 275 | else |
| 276 | return GPIOF_INPUT; |
| 277 | } |
| 278 | |
| 279 | static const struct dm_gpio_ops gpio_omap_ops = { |
Simon Glass | 1a96d7f | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 280 | .direction_input = omap_gpio_direction_input, |
| 281 | .direction_output = omap_gpio_direction_output, |
| 282 | .get_value = omap_gpio_get_value, |
| 283 | .set_value = omap_gpio_set_value, |
| 284 | .get_function = omap_gpio_get_function, |
Simon Glass | 1a96d7f | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 285 | }; |
| 286 | |
| 287 | static int omap_gpio_probe(struct udevice *dev) |
| 288 | { |
| 289 | struct gpio_bank *bank = dev_get_priv(dev); |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 290 | struct omap_gpio_plat *plat = dev_get_plat(dev); |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 291 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Adam Ford | 22f2e93 | 2018-06-11 20:05:38 -0500 | [diff] [blame] | 292 | char name[18], *str; |
Simon Glass | 1a96d7f | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 293 | |
Adam Ford | e387e45 | 2018-08-17 14:37:58 -0500 | [diff] [blame] | 294 | sprintf(name, "gpio@%4x_", (unsigned int)plat->base); |
Adam Ford | 22f2e93 | 2018-06-11 20:05:38 -0500 | [diff] [blame] | 295 | str = strdup(name); |
| 296 | if (!str) |
| 297 | return -ENOMEM; |
| 298 | uc_priv->bank_name = str; |
Simon Glass | 1a96d7f | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 299 | uc_priv->gpio_count = GPIO_PER_BANK; |
| 300 | bank->base = (void *)plat->base; |
Tom Rini | 7bc2bca | 2015-07-31 19:55:09 -0400 | [diff] [blame] | 301 | return 0; |
| 302 | } |
| 303 | |
Adam Ford | 1ac4c1b | 2018-06-10 09:29:51 -0500 | [diff] [blame] | 304 | #if !CONFIG_IS_ENABLED(OF_CONTROL) |
Tom Rini | 7bc2bca | 2015-07-31 19:55:09 -0400 | [diff] [blame] | 305 | static int omap_gpio_bind(struct udevice *dev) |
| 306 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 307 | struct omap_gpio_plat *plat = dev_get_plat(dev); |
Tom Rini | 7bc2bca | 2015-07-31 19:55:09 -0400 | [diff] [blame] | 308 | fdt_addr_t base_addr; |
| 309 | |
| 310 | if (plat) |
| 311 | return 0; |
| 312 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 313 | base_addr = dev_read_addr(dev); |
Tom Rini | 7bc2bca | 2015-07-31 19:55:09 -0400 | [diff] [blame] | 314 | if (base_addr == FDT_ADDR_T_NONE) |
Simon Glass | f44b4bf | 2017-09-17 16:54:53 -0600 | [diff] [blame] | 315 | return -EINVAL; |
Tom Rini | 7bc2bca | 2015-07-31 19:55:09 -0400 | [diff] [blame] | 316 | |
| 317 | /* |
| 318 | * TODO: |
| 319 | * When every board is converted to driver model and DT is |
| 320 | * supported, this can be done by auto-alloc feature, but |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 321 | * not using calloc to alloc memory for plat. |
Simon Glass | 87f4cb7 | 2017-09-17 16:54:52 -0600 | [diff] [blame] | 322 | * |
| 323 | * For example am33xx_gpio uses platform data rather than device tree. |
| 324 | * |
| 325 | * NOTE: DO NOT COPY this code if you are using device tree. |
Tom Rini | 7bc2bca | 2015-07-31 19:55:09 -0400 | [diff] [blame] | 326 | */ |
| 327 | plat = calloc(1, sizeof(*plat)); |
| 328 | if (!plat) |
| 329 | return -ENOMEM; |
| 330 | |
| 331 | plat->base = base_addr; |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 332 | plat->port_name = fdt_get_name(gd->fdt_blob, dev_of_offset(dev), NULL); |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 333 | dev_set_plat(dev, plat); |
Simon Glass | 1a96d7f | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 334 | |
| 335 | return 0; |
| 336 | } |
Adam Ford | 1ac4c1b | 2018-06-10 09:29:51 -0500 | [diff] [blame] | 337 | #endif |
Simon Glass | 1a96d7f | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 338 | |
Adam Ford | 49b7776 | 2018-08-20 20:27:48 -0500 | [diff] [blame] | 339 | #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA) |
Tom Rini | 7bc2bca | 2015-07-31 19:55:09 -0400 | [diff] [blame] | 340 | static const struct udevice_id omap_gpio_ids[] = { |
| 341 | { .compatible = "ti,omap3-gpio" }, |
| 342 | { .compatible = "ti,omap4-gpio" }, |
| 343 | { .compatible = "ti,am4372-gpio" }, |
| 344 | { } |
| 345 | }; |
| 346 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 347 | static int omap_gpio_of_to_plat(struct udevice *dev) |
Adam Ford | 1ac4c1b | 2018-06-10 09:29:51 -0500 | [diff] [blame] | 348 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 349 | struct omap_gpio_plat *plat = dev_get_plat(dev); |
Adam Ford | 1ac4c1b | 2018-06-10 09:29:51 -0500 | [diff] [blame] | 350 | fdt_addr_t addr; |
| 351 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 352 | addr = dev_read_addr(dev); |
Adam Ford | 1ac4c1b | 2018-06-10 09:29:51 -0500 | [diff] [blame] | 353 | if (addr == FDT_ADDR_T_NONE) |
| 354 | return -EINVAL; |
| 355 | |
| 356 | plat->base = addr; |
| 357 | return 0; |
| 358 | } |
| 359 | #endif |
| 360 | |
Simon Glass | 1a96d7f | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 361 | U_BOOT_DRIVER(gpio_omap) = { |
| 362 | .name = "gpio_omap", |
| 363 | .id = UCLASS_GPIO, |
Adam Ford | 1ac4c1b | 2018-06-10 09:29:51 -0500 | [diff] [blame] | 364 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Adam Ford | 49b7776 | 2018-08-20 20:27:48 -0500 | [diff] [blame] | 365 | #if !CONFIG_IS_ENABLED(OF_PLATDATA) |
| 366 | .of_match = omap_gpio_ids, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 367 | .of_to_plat = of_match_ptr(omap_gpio_of_to_plat), |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 368 | .plat_auto = sizeof(struct omap_gpio_plat), |
Adam Ford | 49b7776 | 2018-08-20 20:27:48 -0500 | [diff] [blame] | 369 | #endif |
Adam Ford | 1ac4c1b | 2018-06-10 09:29:51 -0500 | [diff] [blame] | 370 | #else |
| 371 | .bind = omap_gpio_bind, |
| 372 | #endif |
Simon Glass | 1a96d7f | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 373 | .ops = &gpio_omap_ops, |
| 374 | .probe = omap_gpio_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 375 | .priv_auto = sizeof(struct gpio_bank), |
Bin Meng | b508ee5 | 2018-10-24 06:36:30 -0700 | [diff] [blame] | 376 | #if !CONFIG_IS_ENABLED(OF_CONTROL) |
Faiz Abbas | c16d716 | 2018-03-15 21:11:34 +0530 | [diff] [blame] | 377 | .flags = DM_FLAG_PRE_RELOC, |
Bin Meng | b508ee5 | 2018-10-24 06:36:30 -0700 | [diff] [blame] | 378 | #endif |
Simon Glass | 1a96d7f | 2014-10-22 21:37:09 -0600 | [diff] [blame] | 379 | }; |
| 380 | |
Simon Glass | fa4689a | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 381 | #endif /* !DM_GPIO */ |