Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 1 | #include <common.h> |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 2 | #include <dm.h> |
Benjamin Gaignard | 16f6f33 | 2018-11-27 13:49:53 +0100 | [diff] [blame] | 3 | #include <hwspinlock.h> |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 4 | #include <asm/arch/gpio.h> |
| 5 | #include <asm/gpio.h> |
| 6 | #include <asm/io.h> |
Patrice Chotard | e16e8f4 | 2019-07-30 19:16:10 +0200 | [diff] [blame] | 7 | #include <dm/lists.h> |
| 8 | #include <dm/pinctrl.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame^] | 9 | #include <linux/err.h> |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 10 | |
| 11 | DECLARE_GLOBAL_DATA_PTR; |
| 12 | |
Vikas Manocha | 40ddb3a | 2017-04-10 15:03:04 -0700 | [diff] [blame] | 13 | #define MAX_PINS_ONE_IP 70 |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 14 | #define MODE_BITS_MASK 3 |
| 15 | #define OSPEED_MASK 3 |
| 16 | #define PUPD_MASK 3 |
| 17 | #define OTYPE_MSK 1 |
| 18 | #define AFR_MASK 0xF |
| 19 | |
Patrice Chotard | aaf68e8 | 2018-10-24 14:10:18 +0200 | [diff] [blame] | 20 | struct stm32_pinctrl_priv { |
Benjamin Gaignard | 16f6f33 | 2018-11-27 13:49:53 +0100 | [diff] [blame] | 21 | struct hwspinlock hws; |
Patrice Chotard | aaf68e8 | 2018-10-24 14:10:18 +0200 | [diff] [blame] | 22 | int pinctrl_ngpios; |
| 23 | struct list_head gpio_dev; |
| 24 | }; |
| 25 | |
| 26 | struct stm32_gpio_bank { |
| 27 | struct udevice *gpio_dev; |
| 28 | struct list_head list; |
| 29 | }; |
| 30 | |
Benjamin Gaignard | 16f6f33 | 2018-11-27 13:49:53 +0100 | [diff] [blame] | 31 | #ifndef CONFIG_SPL_BUILD |
| 32 | |
Patrice Chotard | 881e867 | 2018-10-24 14:10:19 +0200 | [diff] [blame] | 33 | static char pin_name[PINNAME_SIZE]; |
Patrice Chotard | a46fb39 | 2018-10-24 14:10:20 +0200 | [diff] [blame] | 34 | #define PINMUX_MODE_COUNT 5 |
| 35 | static const char * const pinmux_mode[PINMUX_MODE_COUNT] = { |
| 36 | "gpio input", |
| 37 | "gpio output", |
| 38 | "analog", |
| 39 | "unknown", |
| 40 | "alt function", |
| 41 | }; |
| 42 | |
| 43 | static int stm32_pinctrl_get_af(struct udevice *dev, unsigned int offset) |
| 44 | { |
| 45 | struct stm32_gpio_priv *priv = dev_get_priv(dev); |
| 46 | struct stm32_gpio_regs *regs = priv->regs; |
| 47 | u32 af; |
| 48 | u32 alt_shift = (offset % 8) * 4; |
| 49 | u32 alt_index = offset / 8; |
| 50 | |
| 51 | af = (readl(®s->afr[alt_index]) & |
| 52 | GENMASK(alt_shift + 3, alt_shift)) >> alt_shift; |
| 53 | |
| 54 | return af; |
| 55 | } |
| 56 | |
Patrice Chotard | 7ef9108 | 2018-12-03 10:52:50 +0100 | [diff] [blame] | 57 | static int stm32_populate_gpio_dev_list(struct udevice *dev) |
| 58 | { |
| 59 | struct stm32_pinctrl_priv *priv = dev_get_priv(dev); |
| 60 | struct udevice *gpio_dev; |
| 61 | struct udevice *child; |
| 62 | struct stm32_gpio_bank *gpio_bank; |
| 63 | int ret; |
| 64 | |
| 65 | /* |
| 66 | * parse pin-controller sub-nodes (ie gpio bank nodes) and fill |
| 67 | * a list with all gpio device reference which belongs to the |
| 68 | * current pin-controller. This list is used to find pin_name and |
| 69 | * pin muxing |
| 70 | */ |
| 71 | list_for_each_entry(child, &dev->child_head, sibling_node) { |
| 72 | ret = uclass_get_device_by_name(UCLASS_GPIO, child->name, |
| 73 | &gpio_dev); |
| 74 | if (ret < 0) |
| 75 | continue; |
| 76 | |
| 77 | gpio_bank = malloc(sizeof(*gpio_bank)); |
| 78 | if (!gpio_bank) { |
| 79 | dev_err(dev, "Not enough memory\n"); |
| 80 | return -ENOMEM; |
| 81 | } |
| 82 | |
| 83 | gpio_bank->gpio_dev = gpio_dev; |
| 84 | list_add_tail(&gpio_bank->list, &priv->gpio_dev); |
| 85 | } |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
Patrice Chotard | aaf68e8 | 2018-10-24 14:10:18 +0200 | [diff] [blame] | 90 | static int stm32_pinctrl_get_pins_count(struct udevice *dev) |
| 91 | { |
| 92 | struct stm32_pinctrl_priv *priv = dev_get_priv(dev); |
| 93 | struct gpio_dev_priv *uc_priv; |
| 94 | struct stm32_gpio_bank *gpio_bank; |
| 95 | |
| 96 | /* |
| 97 | * if get_pins_count has already been executed once on this |
| 98 | * pin-controller, no need to run it again |
| 99 | */ |
| 100 | if (priv->pinctrl_ngpios) |
| 101 | return priv->pinctrl_ngpios; |
| 102 | |
Patrice Chotard | 7ef9108 | 2018-12-03 10:52:50 +0100 | [diff] [blame] | 103 | if (list_empty(&priv->gpio_dev)) |
| 104 | stm32_populate_gpio_dev_list(dev); |
Patrice Chotard | aaf68e8 | 2018-10-24 14:10:18 +0200 | [diff] [blame] | 105 | /* |
| 106 | * walk through all banks to retrieve the pin-controller |
| 107 | * pins number |
| 108 | */ |
| 109 | list_for_each_entry(gpio_bank, &priv->gpio_dev, list) { |
| 110 | uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev); |
| 111 | |
| 112 | priv->pinctrl_ngpios += uc_priv->gpio_count; |
| 113 | } |
| 114 | |
| 115 | return priv->pinctrl_ngpios; |
| 116 | } |
| 117 | |
Patrice Chotard | 881e867 | 2018-10-24 14:10:19 +0200 | [diff] [blame] | 118 | static struct udevice *stm32_pinctrl_get_gpio_dev(struct udevice *dev, |
Patrice Chotard | 0b96800 | 2018-12-03 10:52:54 +0100 | [diff] [blame] | 119 | unsigned int selector, |
| 120 | unsigned int *idx) |
Patrice Chotard | 881e867 | 2018-10-24 14:10:19 +0200 | [diff] [blame] | 121 | { |
| 122 | struct stm32_pinctrl_priv *priv = dev_get_priv(dev); |
| 123 | struct stm32_gpio_bank *gpio_bank; |
| 124 | struct gpio_dev_priv *uc_priv; |
Patrice Chotard | 0b96800 | 2018-12-03 10:52:54 +0100 | [diff] [blame] | 125 | int pin_count = 0; |
Patrice Chotard | 881e867 | 2018-10-24 14:10:19 +0200 | [diff] [blame] | 126 | |
Patrice Chotard | 7ef9108 | 2018-12-03 10:52:50 +0100 | [diff] [blame] | 127 | if (list_empty(&priv->gpio_dev)) |
| 128 | stm32_populate_gpio_dev_list(dev); |
| 129 | |
Patrice Chotard | 881e867 | 2018-10-24 14:10:19 +0200 | [diff] [blame] | 130 | /* look up for the bank which owns the requested pin */ |
| 131 | list_for_each_entry(gpio_bank, &priv->gpio_dev, list) { |
| 132 | uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev); |
| 133 | |
Patrice Chotard | 0b96800 | 2018-12-03 10:52:54 +0100 | [diff] [blame] | 134 | if (selector < (pin_count + uc_priv->gpio_count)) { |
| 135 | /* |
| 136 | * we found the bank, convert pin selector to |
| 137 | * gpio bank index |
| 138 | */ |
| 139 | *idx = stm32_offset_to_index(gpio_bank->gpio_dev, |
| 140 | selector - pin_count); |
Patrick Delaunay | 4c11a11 | 2019-06-21 15:26:52 +0200 | [diff] [blame] | 141 | if (IS_ERR_VALUE(*idx)) |
Patrice Chotard | 0b96800 | 2018-12-03 10:52:54 +0100 | [diff] [blame] | 142 | return NULL; |
Patrice Chotard | 881e867 | 2018-10-24 14:10:19 +0200 | [diff] [blame] | 143 | |
Patrice Chotard | 0b96800 | 2018-12-03 10:52:54 +0100 | [diff] [blame] | 144 | return gpio_bank->gpio_dev; |
| 145 | } |
| 146 | pin_count += uc_priv->gpio_count; |
Patrice Chotard | 881e867 | 2018-10-24 14:10:19 +0200 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | return NULL; |
| 150 | } |
| 151 | |
| 152 | static const char *stm32_pinctrl_get_pin_name(struct udevice *dev, |
| 153 | unsigned int selector) |
| 154 | { |
| 155 | struct gpio_dev_priv *uc_priv; |
| 156 | struct udevice *gpio_dev; |
Patrice Chotard | 0b96800 | 2018-12-03 10:52:54 +0100 | [diff] [blame] | 157 | unsigned int gpio_idx; |
Patrice Chotard | 881e867 | 2018-10-24 14:10:19 +0200 | [diff] [blame] | 158 | |
| 159 | /* look up for the bank which owns the requested pin */ |
Patrice Chotard | 0b96800 | 2018-12-03 10:52:54 +0100 | [diff] [blame] | 160 | gpio_dev = stm32_pinctrl_get_gpio_dev(dev, selector, &gpio_idx); |
Patrice Chotard | 881e867 | 2018-10-24 14:10:19 +0200 | [diff] [blame] | 161 | if (!gpio_dev) { |
| 162 | snprintf(pin_name, PINNAME_SIZE, "Error"); |
| 163 | } else { |
| 164 | uc_priv = dev_get_uclass_priv(gpio_dev); |
| 165 | |
| 166 | snprintf(pin_name, PINNAME_SIZE, "%s%d", |
| 167 | uc_priv->bank_name, |
Patrice Chotard | 0b96800 | 2018-12-03 10:52:54 +0100 | [diff] [blame] | 168 | gpio_idx); |
Patrice Chotard | 881e867 | 2018-10-24 14:10:19 +0200 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | return pin_name; |
| 172 | } |
Patrice Chotard | a46fb39 | 2018-10-24 14:10:20 +0200 | [diff] [blame] | 173 | |
| 174 | static int stm32_pinctrl_get_pin_muxing(struct udevice *dev, |
| 175 | unsigned int selector, |
| 176 | char *buf, |
| 177 | int size) |
| 178 | { |
| 179 | struct udevice *gpio_dev; |
| 180 | const char *label; |
Patrice Chotard | a46fb39 | 2018-10-24 14:10:20 +0200 | [diff] [blame] | 181 | int mode; |
| 182 | int af_num; |
Patrice Chotard | 0b96800 | 2018-12-03 10:52:54 +0100 | [diff] [blame] | 183 | unsigned int gpio_idx; |
Patrice Chotard | a46fb39 | 2018-10-24 14:10:20 +0200 | [diff] [blame] | 184 | |
| 185 | /* look up for the bank which owns the requested pin */ |
Patrice Chotard | 0b96800 | 2018-12-03 10:52:54 +0100 | [diff] [blame] | 186 | gpio_dev = stm32_pinctrl_get_gpio_dev(dev, selector, &gpio_idx); |
Patrice Chotard | a46fb39 | 2018-10-24 14:10:20 +0200 | [diff] [blame] | 187 | |
| 188 | if (!gpio_dev) |
| 189 | return -ENODEV; |
| 190 | |
Patrice Chotard | 0b96800 | 2018-12-03 10:52:54 +0100 | [diff] [blame] | 191 | mode = gpio_get_raw_function(gpio_dev, gpio_idx, &label); |
Patrice Chotard | a46fb39 | 2018-10-24 14:10:20 +0200 | [diff] [blame] | 192 | |
Patrice Chotard | 0b96800 | 2018-12-03 10:52:54 +0100 | [diff] [blame] | 193 | dev_dbg(dev, "selector = %d gpio_idx = %d mode = %d\n", |
| 194 | selector, gpio_idx, mode); |
Patrice Chotard | a46fb39 | 2018-10-24 14:10:20 +0200 | [diff] [blame] | 195 | |
Patrice Chotard | a46fb39 | 2018-10-24 14:10:20 +0200 | [diff] [blame] | 196 | |
| 197 | switch (mode) { |
| 198 | case GPIOF_UNKNOWN: |
| 199 | /* should never happen */ |
| 200 | return -EINVAL; |
| 201 | case GPIOF_UNUSED: |
| 202 | snprintf(buf, size, "%s", pinmux_mode[mode]); |
| 203 | break; |
| 204 | case GPIOF_FUNC: |
Patrice Chotard | 0b96800 | 2018-12-03 10:52:54 +0100 | [diff] [blame] | 205 | af_num = stm32_pinctrl_get_af(gpio_dev, gpio_idx); |
Patrice Chotard | a46fb39 | 2018-10-24 14:10:20 +0200 | [diff] [blame] | 206 | snprintf(buf, size, "%s %d", pinmux_mode[mode], af_num); |
| 207 | break; |
| 208 | case GPIOF_OUTPUT: |
| 209 | case GPIOF_INPUT: |
| 210 | snprintf(buf, size, "%s %s", |
| 211 | pinmux_mode[mode], label ? label : ""); |
| 212 | break; |
| 213 | } |
| 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
Benjamin Gaignard | 16f6f33 | 2018-11-27 13:49:53 +0100 | [diff] [blame] | 218 | #endif |
| 219 | |
Patrick Delaunay | 4c11a11 | 2019-06-21 15:26:52 +0200 | [diff] [blame] | 220 | static int stm32_pinctrl_probe(struct udevice *dev) |
Patrice Chotard | aaf68e8 | 2018-10-24 14:10:18 +0200 | [diff] [blame] | 221 | { |
| 222 | struct stm32_pinctrl_priv *priv = dev_get_priv(dev); |
Patrice Chotard | aaf68e8 | 2018-10-24 14:10:18 +0200 | [diff] [blame] | 223 | int ret; |
| 224 | |
| 225 | INIT_LIST_HEAD(&priv->gpio_dev); |
| 226 | |
Benjamin Gaignard | 16f6f33 | 2018-11-27 13:49:53 +0100 | [diff] [blame] | 227 | /* hwspinlock property is optional, just log the error */ |
| 228 | ret = hwspinlock_get_by_index(dev, 0, &priv->hws); |
| 229 | if (ret) |
| 230 | debug("%s: hwspinlock_get_by_index may have failed (%d)\n", |
| 231 | __func__, ret); |
| 232 | |
Patrice Chotard | aaf68e8 | 2018-10-24 14:10:18 +0200 | [diff] [blame] | 233 | return 0; |
| 234 | } |
Patrice Chotard | aaf68e8 | 2018-10-24 14:10:18 +0200 | [diff] [blame] | 235 | |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 236 | static int stm32_gpio_config(struct gpio_desc *desc, |
| 237 | const struct stm32_gpio_ctl *ctl) |
| 238 | { |
| 239 | struct stm32_gpio_priv *priv = dev_get_priv(desc->dev); |
| 240 | struct stm32_gpio_regs *regs = priv->regs; |
Benjamin Gaignard | 16f6f33 | 2018-11-27 13:49:53 +0100 | [diff] [blame] | 241 | struct stm32_pinctrl_priv *ctrl_priv; |
| 242 | int ret; |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 243 | u32 index; |
| 244 | |
| 245 | if (!ctl || ctl->af > 15 || ctl->mode > 3 || ctl->otype > 1 || |
| 246 | ctl->pupd > 2 || ctl->speed > 3) |
| 247 | return -EINVAL; |
| 248 | |
Benjamin Gaignard | 16f6f33 | 2018-11-27 13:49:53 +0100 | [diff] [blame] | 249 | ctrl_priv = dev_get_priv(dev_get_parent(desc->dev)); |
| 250 | ret = hwspinlock_lock_timeout(&ctrl_priv->hws, 10); |
| 251 | if (ret == -ETIME) { |
| 252 | dev_err(desc->dev, "HWSpinlock timeout\n"); |
| 253 | return ret; |
| 254 | } |
| 255 | |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 256 | index = (desc->offset & 0x07) * 4; |
| 257 | clrsetbits_le32(®s->afr[desc->offset >> 3], AFR_MASK << index, |
| 258 | ctl->af << index); |
| 259 | |
| 260 | index = desc->offset * 2; |
| 261 | clrsetbits_le32(®s->moder, MODE_BITS_MASK << index, |
| 262 | ctl->mode << index); |
| 263 | clrsetbits_le32(®s->ospeedr, OSPEED_MASK << index, |
| 264 | ctl->speed << index); |
| 265 | clrsetbits_le32(®s->pupdr, PUPD_MASK << index, ctl->pupd << index); |
| 266 | |
| 267 | index = desc->offset; |
| 268 | clrsetbits_le32(®s->otyper, OTYPE_MSK << index, ctl->otype << index); |
| 269 | |
Benjamin Gaignard | 16f6f33 | 2018-11-27 13:49:53 +0100 | [diff] [blame] | 270 | hwspinlock_unlock(&ctrl_priv->hws); |
| 271 | |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 272 | return 0; |
| 273 | } |
Patrick Delaunay | d252d75 | 2018-03-12 10:46:13 +0100 | [diff] [blame] | 274 | |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 275 | static int prep_gpio_dsc(struct stm32_gpio_dsc *gpio_dsc, u32 port_pin) |
| 276 | { |
Patrick Delaunay | d252d75 | 2018-03-12 10:46:13 +0100 | [diff] [blame] | 277 | gpio_dsc->port = (port_pin & 0x1F000) >> 12; |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 278 | gpio_dsc->pin = (port_pin & 0x0F00) >> 8; |
| 279 | debug("%s: GPIO:port= %d, pin= %d\n", __func__, gpio_dsc->port, |
| 280 | gpio_dsc->pin); |
| 281 | |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | static int prep_gpio_ctl(struct stm32_gpio_ctl *gpio_ctl, u32 gpio_fn, int node) |
| 286 | { |
| 287 | gpio_fn &= 0x00FF; |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 288 | gpio_ctl->af = 0; |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 289 | |
| 290 | switch (gpio_fn) { |
| 291 | case 0: |
| 292 | gpio_ctl->mode = STM32_GPIO_MODE_IN; |
| 293 | break; |
| 294 | case 1 ... 16: |
| 295 | gpio_ctl->mode = STM32_GPIO_MODE_AF; |
| 296 | gpio_ctl->af = gpio_fn - 1; |
| 297 | break; |
| 298 | case 17: |
| 299 | gpio_ctl->mode = STM32_GPIO_MODE_AN; |
| 300 | break; |
| 301 | default: |
| 302 | gpio_ctl->mode = STM32_GPIO_MODE_OUT; |
| 303 | break; |
| 304 | } |
| 305 | |
| 306 | gpio_ctl->speed = fdtdec_get_int(gd->fdt_blob, node, "slew-rate", 0); |
| 307 | |
| 308 | if (fdtdec_get_bool(gd->fdt_blob, node, "drive-open-drain")) |
| 309 | gpio_ctl->otype = STM32_GPIO_OTYPE_OD; |
| 310 | else |
| 311 | gpio_ctl->otype = STM32_GPIO_OTYPE_PP; |
| 312 | |
| 313 | if (fdtdec_get_bool(gd->fdt_blob, node, "bias-pull-up")) |
| 314 | gpio_ctl->pupd = STM32_GPIO_PUPD_UP; |
| 315 | else if (fdtdec_get_bool(gd->fdt_blob, node, "bias-pull-down")) |
| 316 | gpio_ctl->pupd = STM32_GPIO_PUPD_DOWN; |
| 317 | else |
| 318 | gpio_ctl->pupd = STM32_GPIO_PUPD_NO; |
| 319 | |
| 320 | debug("%s: gpio fn= %d, slew-rate= %x, op type= %x, pull-upd is = %x\n", |
| 321 | __func__, gpio_fn, gpio_ctl->speed, gpio_ctl->otype, |
| 322 | gpio_ctl->pupd); |
| 323 | |
| 324 | return 0; |
| 325 | } |
| 326 | |
Christophe Kerello | a466d21 | 2017-06-20 17:04:18 +0200 | [diff] [blame] | 327 | static int stm32_pinctrl_config(int offset) |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 328 | { |
Vikas Manocha | 40ddb3a | 2017-04-10 15:03:04 -0700 | [diff] [blame] | 329 | u32 pin_mux[MAX_PINS_ONE_IP]; |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 330 | int rv, len; |
| 331 | |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 332 | /* |
| 333 | * check for "pinmux" property in each subnode (e.g. pins1 and pins2 for |
| 334 | * usart1) of pin controller phandle "pinctrl-0" |
| 335 | * */ |
Christophe Kerello | a466d21 | 2017-06-20 17:04:18 +0200 | [diff] [blame] | 336 | fdt_for_each_subnode(offset, gd->fdt_blob, offset) { |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 337 | struct stm32_gpio_dsc gpio_dsc; |
| 338 | struct stm32_gpio_ctl gpio_ctl; |
| 339 | int i; |
| 340 | |
Christophe Kerello | a466d21 | 2017-06-20 17:04:18 +0200 | [diff] [blame] | 341 | len = fdtdec_get_int_array_count(gd->fdt_blob, offset, |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 342 | "pinmux", pin_mux, |
| 343 | ARRAY_SIZE(pin_mux)); |
Christophe Kerello | a466d21 | 2017-06-20 17:04:18 +0200 | [diff] [blame] | 344 | debug("%s: no of pinmux entries= %d\n", __func__, len); |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 345 | if (len < 0) |
| 346 | return -EINVAL; |
| 347 | for (i = 0; i < len; i++) { |
Vikas Manocha | 1a8fde7 | 2017-04-10 15:02:59 -0700 | [diff] [blame] | 348 | struct gpio_desc desc; |
Patrick Delaunay | d252d75 | 2018-03-12 10:46:13 +0100 | [diff] [blame] | 349 | |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 350 | debug("%s: pinmux = %x\n", __func__, *(pin_mux + i)); |
| 351 | prep_gpio_dsc(&gpio_dsc, *(pin_mux + i)); |
Christophe Kerello | a466d21 | 2017-06-20 17:04:18 +0200 | [diff] [blame] | 352 | prep_gpio_ctl(&gpio_ctl, *(pin_mux + i), offset); |
Vikas Manocha | 1a8fde7 | 2017-04-10 15:02:59 -0700 | [diff] [blame] | 353 | rv = uclass_get_device_by_seq(UCLASS_GPIO, |
Patrick Delaunay | d252d75 | 2018-03-12 10:46:13 +0100 | [diff] [blame] | 354 | gpio_dsc.port, |
| 355 | &desc.dev); |
Vikas Manocha | 1a8fde7 | 2017-04-10 15:02:59 -0700 | [diff] [blame] | 356 | if (rv) |
| 357 | return rv; |
| 358 | desc.offset = gpio_dsc.pin; |
| 359 | rv = stm32_gpio_config(&desc, &gpio_ctl); |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 360 | debug("%s: rv = %d\n\n", __func__, rv); |
| 361 | if (rv) |
| 362 | return rv; |
| 363 | } |
Christophe Kerello | a466d21 | 2017-06-20 17:04:18 +0200 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | return 0; |
| 367 | } |
| 368 | |
Patrice Chotard | 05a9319 | 2019-06-21 15:39:23 +0200 | [diff] [blame] | 369 | static int stm32_pinctrl_bind(struct udevice *dev) |
| 370 | { |
| 371 | ofnode node; |
| 372 | const char *name; |
| 373 | int ret; |
| 374 | |
| 375 | dev_for_each_subnode(node, dev) { |
| 376 | debug("%s: bind %s\n", __func__, ofnode_get_name(node)); |
| 377 | |
| 378 | ofnode_get_property(node, "gpio-controller", &ret); |
| 379 | if (ret < 0) |
| 380 | continue; |
| 381 | /* Get the name of each gpio node */ |
| 382 | name = ofnode_get_name(node); |
| 383 | if (!name) |
| 384 | return -EINVAL; |
| 385 | |
| 386 | /* Bind each gpio node */ |
| 387 | ret = device_bind_driver_to_node(dev, "gpio_stm32", |
| 388 | name, node, NULL); |
| 389 | if (ret) |
| 390 | return ret; |
| 391 | |
| 392 | debug("%s: bind %s\n", __func__, name); |
| 393 | } |
| 394 | |
| 395 | return 0; |
| 396 | } |
| 397 | |
Christophe Kerello | d666155 | 2017-06-20 17:04:19 +0200 | [diff] [blame] | 398 | #if CONFIG_IS_ENABLED(PINCTRL_FULL) |
| 399 | static int stm32_pinctrl_set_state(struct udevice *dev, struct udevice *config) |
| 400 | { |
| 401 | return stm32_pinctrl_config(dev_of_offset(config)); |
| 402 | } |
| 403 | #else /* PINCTRL_FULL */ |
Christophe Kerello | a466d21 | 2017-06-20 17:04:18 +0200 | [diff] [blame] | 404 | static int stm32_pinctrl_set_state_simple(struct udevice *dev, |
| 405 | struct udevice *periph) |
| 406 | { |
| 407 | const void *fdt = gd->fdt_blob; |
| 408 | const fdt32_t *list; |
| 409 | uint32_t phandle; |
| 410 | int config_node; |
| 411 | int size, i, ret; |
| 412 | |
| 413 | list = fdt_getprop(fdt, dev_of_offset(periph), "pinctrl-0", &size); |
| 414 | if (!list) |
| 415 | return -EINVAL; |
| 416 | |
| 417 | debug("%s: periph->name = %s\n", __func__, periph->name); |
| 418 | |
| 419 | size /= sizeof(*list); |
| 420 | for (i = 0; i < size; i++) { |
| 421 | phandle = fdt32_to_cpu(*list++); |
| 422 | |
| 423 | config_node = fdt_node_offset_by_phandle(fdt, phandle); |
| 424 | if (config_node < 0) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 425 | pr_err("prop pinctrl-0 index %d invalid phandle\n", i); |
Christophe Kerello | a466d21 | 2017-06-20 17:04:18 +0200 | [diff] [blame] | 426 | return -EINVAL; |
| 427 | } |
| 428 | |
| 429 | ret = stm32_pinctrl_config(config_node); |
| 430 | if (ret) |
| 431 | return ret; |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | return 0; |
| 435 | } |
Christophe Kerello | d666155 | 2017-06-20 17:04:19 +0200 | [diff] [blame] | 436 | #endif /* PINCTRL_FULL */ |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 437 | |
| 438 | static struct pinctrl_ops stm32_pinctrl_ops = { |
Christophe Kerello | d666155 | 2017-06-20 17:04:19 +0200 | [diff] [blame] | 439 | #if CONFIG_IS_ENABLED(PINCTRL_FULL) |
| 440 | .set_state = stm32_pinctrl_set_state, |
| 441 | #else /* PINCTRL_FULL */ |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 442 | .set_state_simple = stm32_pinctrl_set_state_simple, |
Christophe Kerello | d666155 | 2017-06-20 17:04:19 +0200 | [diff] [blame] | 443 | #endif /* PINCTRL_FULL */ |
Patrice Chotard | aaf68e8 | 2018-10-24 14:10:18 +0200 | [diff] [blame] | 444 | #ifndef CONFIG_SPL_BUILD |
Patrice Chotard | 881e867 | 2018-10-24 14:10:19 +0200 | [diff] [blame] | 445 | .get_pin_name = stm32_pinctrl_get_pin_name, |
Patrice Chotard | aaf68e8 | 2018-10-24 14:10:18 +0200 | [diff] [blame] | 446 | .get_pins_count = stm32_pinctrl_get_pins_count, |
Patrice Chotard | a46fb39 | 2018-10-24 14:10:20 +0200 | [diff] [blame] | 447 | .get_pin_muxing = stm32_pinctrl_get_pin_muxing, |
Patrice Chotard | aaf68e8 | 2018-10-24 14:10:18 +0200 | [diff] [blame] | 448 | #endif |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 449 | }; |
| 450 | |
| 451 | static const struct udevice_id stm32_pinctrl_ids[] = { |
Patrice Chotard | b5652b7 | 2017-12-12 09:49:35 +0100 | [diff] [blame] | 452 | { .compatible = "st,stm32f429-pinctrl" }, |
| 453 | { .compatible = "st,stm32f469-pinctrl" }, |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 454 | { .compatible = "st,stm32f746-pinctrl" }, |
Patrice Chotard | 636768f | 2018-12-11 14:49:18 +0100 | [diff] [blame] | 455 | { .compatible = "st,stm32f769-pinctrl" }, |
Patrice Chotard | 6502c47 | 2017-09-13 18:00:04 +0200 | [diff] [blame] | 456 | { .compatible = "st,stm32h743-pinctrl" }, |
Patrick Delaunay | d252d75 | 2018-03-12 10:46:13 +0100 | [diff] [blame] | 457 | { .compatible = "st,stm32mp157-pinctrl" }, |
| 458 | { .compatible = "st,stm32mp157-z-pinctrl" }, |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 459 | { } |
| 460 | }; |
| 461 | |
| 462 | U_BOOT_DRIVER(pinctrl_stm32) = { |
Patrice Chotard | aaf68e8 | 2018-10-24 14:10:18 +0200 | [diff] [blame] | 463 | .name = "pinctrl_stm32", |
| 464 | .id = UCLASS_PINCTRL, |
| 465 | .of_match = stm32_pinctrl_ids, |
| 466 | .ops = &stm32_pinctrl_ops, |
Patrice Chotard | 05a9319 | 2019-06-21 15:39:23 +0200 | [diff] [blame] | 467 | .bind = stm32_pinctrl_bind, |
Patrice Chotard | aaf68e8 | 2018-10-24 14:10:18 +0200 | [diff] [blame] | 468 | .probe = stm32_pinctrl_probe, |
| 469 | .priv_auto_alloc_size = sizeof(struct stm32_pinctrl_priv), |
Vikas Manocha | 07e9e41 | 2017-02-12 10:25:49 -0800 | [diff] [blame] | 470 | }; |