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