blob: 6c98538f56208d9be0250ab6718e69e83c957988 [file] [log] [blame]
Patrick Delaunay10bccd02020-09-09 17:50:15 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2017-2020 STMicroelectronics - All Rights Reserved
4 */
5
Patrick Delaunay28b3e7b2020-11-06 19:01:32 +01006#define LOG_CATEGORY UCLASS_PINCTRL
7
Vikas Manocha94d53082017-02-12 10:25:49 -08008#include <common.h>
Vikas Manocha94d53082017-02-12 10:25:49 -08009#include <dm.h>
Benjamin Gaignard075b0182018-11-27 13:49:53 +010010#include <hwspinlock.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070012#include <malloc.h>
Vikas Manocha77417102017-04-10 15:02:57 -070013#include <asm/arch/gpio.h>
14#include <asm/gpio.h>
15#include <asm/io.h>
Simon Glass336d4612020-02-03 07:36:16 -070016#include <dm/device_compat.h>
Patrice Chotard73858262019-07-30 19:16:10 +020017#include <dm/lists.h>
18#include <dm/pinctrl.h>
Simon Glasscd93d622020-05-10 11:40:13 -060019#include <linux/bitops.h>
Simon Glass61b29b82020-02-03 07:36:15 -070020#include <linux/err.h>
Simon Glass4d72caa2020-05-10 11:40:01 -060021#include <linux/libfdt.h>
Vikas Manocha94d53082017-02-12 10:25:49 -080022
Vikas Manocha58fb3c82017-04-10 15:03:04 -070023#define MAX_PINS_ONE_IP 70
Vikas Manocha77417102017-04-10 15:02:57 -070024#define MODE_BITS_MASK 3
25#define OSPEED_MASK 3
26#define PUPD_MASK 3
27#define OTYPE_MSK 1
28#define AFR_MASK 0xF
29
Patrice Chotard8f651ca2018-10-24 14:10:18 +020030struct stm32_pinctrl_priv {
Benjamin Gaignard075b0182018-11-27 13:49:53 +010031 struct hwspinlock hws;
Patrice Chotard8f651ca2018-10-24 14:10:18 +020032 int pinctrl_ngpios;
33 struct list_head gpio_dev;
34};
35
36struct stm32_gpio_bank {
37 struct udevice *gpio_dev;
38 struct list_head list;
39};
40
Benjamin Gaignard075b0182018-11-27 13:49:53 +010041#ifndef CONFIG_SPL_BUILD
42
Patrice Chotard4ff1c202018-10-24 14:10:19 +020043static char pin_name[PINNAME_SIZE];
Patrice Chotardb42d9382018-10-24 14:10:20 +020044#define PINMUX_MODE_COUNT 5
45static const char * const pinmux_mode[PINMUX_MODE_COUNT] = {
46 "gpio input",
47 "gpio output",
48 "analog",
49 "unknown",
50 "alt function",
51};
52
Patrick Delaunayb305dbc2020-10-28 10:49:07 +010053static const char * const pinmux_bias[] = {
54 [STM32_GPIO_PUPD_NO] = "",
55 [STM32_GPIO_PUPD_UP] = "pull-up",
56 [STM32_GPIO_PUPD_DOWN] = "pull-down",
Patrick Delaunayda7a0bb2020-06-04 14:30:33 +020057};
58
Patrick Delaunay1da42692021-01-21 17:39:07 +010059static const char * const pinmux_otype[] = {
Patrick Delaunayb305dbc2020-10-28 10:49:07 +010060 [STM32_GPIO_OTYPE_PP] = "push-pull",
61 [STM32_GPIO_OTYPE_OD] = "open-drain",
Patrick Delaunayda7a0bb2020-06-04 14:30:33 +020062};
63
Patrice Chotardb42d9382018-10-24 14:10:20 +020064static int stm32_pinctrl_get_af(struct udevice *dev, unsigned int offset)
65{
66 struct stm32_gpio_priv *priv = dev_get_priv(dev);
67 struct stm32_gpio_regs *regs = priv->regs;
68 u32 af;
69 u32 alt_shift = (offset % 8) * 4;
70 u32 alt_index = offset / 8;
71
72 af = (readl(&regs->afr[alt_index]) &
73 GENMASK(alt_shift + 3, alt_shift)) >> alt_shift;
74
75 return af;
76}
77
Patrice Chotard04355042018-12-03 10:52:50 +010078static int stm32_populate_gpio_dev_list(struct udevice *dev)
79{
80 struct stm32_pinctrl_priv *priv = dev_get_priv(dev);
81 struct udevice *gpio_dev;
82 struct udevice *child;
83 struct stm32_gpio_bank *gpio_bank;
84 int ret;
85
86 /*
87 * parse pin-controller sub-nodes (ie gpio bank nodes) and fill
88 * a list with all gpio device reference which belongs to the
89 * current pin-controller. This list is used to find pin_name and
90 * pin muxing
91 */
92 list_for_each_entry(child, &dev->child_head, sibling_node) {
93 ret = uclass_get_device_by_name(UCLASS_GPIO, child->name,
94 &gpio_dev);
95 if (ret < 0)
96 continue;
97
98 gpio_bank = malloc(sizeof(*gpio_bank));
99 if (!gpio_bank) {
100 dev_err(dev, "Not enough memory\n");
101 return -ENOMEM;
102 }
103
104 gpio_bank->gpio_dev = gpio_dev;
105 list_add_tail(&gpio_bank->list, &priv->gpio_dev);
106 }
107
108 return 0;
109}
110
Patrice Chotard8f651ca2018-10-24 14:10:18 +0200111static int stm32_pinctrl_get_pins_count(struct udevice *dev)
112{
113 struct stm32_pinctrl_priv *priv = dev_get_priv(dev);
114 struct gpio_dev_priv *uc_priv;
115 struct stm32_gpio_bank *gpio_bank;
116
117 /*
118 * if get_pins_count has already been executed once on this
119 * pin-controller, no need to run it again
120 */
121 if (priv->pinctrl_ngpios)
122 return priv->pinctrl_ngpios;
123
Patrice Chotard04355042018-12-03 10:52:50 +0100124 if (list_empty(&priv->gpio_dev))
125 stm32_populate_gpio_dev_list(dev);
Patrice Chotard8f651ca2018-10-24 14:10:18 +0200126 /*
127 * walk through all banks to retrieve the pin-controller
128 * pins number
129 */
130 list_for_each_entry(gpio_bank, &priv->gpio_dev, list) {
131 uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev);
132
133 priv->pinctrl_ngpios += uc_priv->gpio_count;
134 }
135
136 return priv->pinctrl_ngpios;
137}
138
Patrice Chotard4ff1c202018-10-24 14:10:19 +0200139static struct udevice *stm32_pinctrl_get_gpio_dev(struct udevice *dev,
Patrice Chotard530b63c2018-12-03 10:52:54 +0100140 unsigned int selector,
141 unsigned int *idx)
Patrice Chotard4ff1c202018-10-24 14:10:19 +0200142{
143 struct stm32_pinctrl_priv *priv = dev_get_priv(dev);
144 struct stm32_gpio_bank *gpio_bank;
145 struct gpio_dev_priv *uc_priv;
Patrice Chotard530b63c2018-12-03 10:52:54 +0100146 int pin_count = 0;
Patrice Chotard4ff1c202018-10-24 14:10:19 +0200147
Patrice Chotard04355042018-12-03 10:52:50 +0100148 if (list_empty(&priv->gpio_dev))
149 stm32_populate_gpio_dev_list(dev);
150
Patrice Chotard4ff1c202018-10-24 14:10:19 +0200151 /* look up for the bank which owns the requested pin */
152 list_for_each_entry(gpio_bank, &priv->gpio_dev, list) {
153 uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev);
154
Patrice Chotard530b63c2018-12-03 10:52:54 +0100155 if (selector < (pin_count + uc_priv->gpio_count)) {
156 /*
157 * we found the bank, convert pin selector to
158 * gpio bank index
159 */
160 *idx = stm32_offset_to_index(gpio_bank->gpio_dev,
161 selector - pin_count);
Patrick Delaunay91ca91e2019-06-21 15:26:52 +0200162 if (IS_ERR_VALUE(*idx))
Patrice Chotard530b63c2018-12-03 10:52:54 +0100163 return NULL;
Patrice Chotard4ff1c202018-10-24 14:10:19 +0200164
Patrice Chotard530b63c2018-12-03 10:52:54 +0100165 return gpio_bank->gpio_dev;
166 }
167 pin_count += uc_priv->gpio_count;
Patrice Chotard4ff1c202018-10-24 14:10:19 +0200168 }
169
170 return NULL;
171}
172
173static const char *stm32_pinctrl_get_pin_name(struct udevice *dev,
174 unsigned int selector)
175{
176 struct gpio_dev_priv *uc_priv;
177 struct udevice *gpio_dev;
Patrice Chotard530b63c2018-12-03 10:52:54 +0100178 unsigned int gpio_idx;
Patrice Chotard4ff1c202018-10-24 14:10:19 +0200179
180 /* look up for the bank which owns the requested pin */
Patrice Chotard530b63c2018-12-03 10:52:54 +0100181 gpio_dev = stm32_pinctrl_get_gpio_dev(dev, selector, &gpio_idx);
Patrice Chotard4ff1c202018-10-24 14:10:19 +0200182 if (!gpio_dev) {
183 snprintf(pin_name, PINNAME_SIZE, "Error");
184 } else {
185 uc_priv = dev_get_uclass_priv(gpio_dev);
186
187 snprintf(pin_name, PINNAME_SIZE, "%s%d",
188 uc_priv->bank_name,
Patrice Chotard530b63c2018-12-03 10:52:54 +0100189 gpio_idx);
Patrice Chotard4ff1c202018-10-24 14:10:19 +0200190 }
191
192 return pin_name;
193}
Patrice Chotardb42d9382018-10-24 14:10:20 +0200194
195static int stm32_pinctrl_get_pin_muxing(struct udevice *dev,
196 unsigned int selector,
197 char *buf,
198 int size)
199{
200 struct udevice *gpio_dev;
Patrick Delaunayda7a0bb2020-06-04 14:30:33 +0200201 struct stm32_gpio_priv *priv;
Patrice Chotardb42d9382018-10-24 14:10:20 +0200202 const char *label;
Patrice Chotardb42d9382018-10-24 14:10:20 +0200203 int mode;
204 int af_num;
Patrice Chotard530b63c2018-12-03 10:52:54 +0100205 unsigned int gpio_idx;
Patrick Delaunayda7a0bb2020-06-04 14:30:33 +0200206 u32 pupd, otype;
Patrice Chotardb42d9382018-10-24 14:10:20 +0200207
208 /* look up for the bank which owns the requested pin */
Patrice Chotard530b63c2018-12-03 10:52:54 +0100209 gpio_dev = stm32_pinctrl_get_gpio_dev(dev, selector, &gpio_idx);
Patrice Chotardb42d9382018-10-24 14:10:20 +0200210
211 if (!gpio_dev)
212 return -ENODEV;
213
Patrice Chotard530b63c2018-12-03 10:52:54 +0100214 mode = gpio_get_raw_function(gpio_dev, gpio_idx, &label);
Patrice Chotard530b63c2018-12-03 10:52:54 +0100215 dev_dbg(dev, "selector = %d gpio_idx = %d mode = %d\n",
216 selector, gpio_idx, mode);
Patrick Delaunayda7a0bb2020-06-04 14:30:33 +0200217 priv = dev_get_priv(gpio_dev);
Patrick Delaunayb305dbc2020-10-28 10:49:07 +0100218 pupd = (readl(&priv->regs->pupdr) >> (gpio_idx * 2)) & PUPD_MASK;
Patrick Delaunay1da42692021-01-21 17:39:07 +0100219 otype = (readl(&priv->regs->otyper) >> gpio_idx) & OTYPE_MSK;
Patrice Chotardb42d9382018-10-24 14:10:20 +0200220
221 switch (mode) {
222 case GPIOF_UNKNOWN:
223 /* should never happen */
224 return -EINVAL;
225 case GPIOF_UNUSED:
226 snprintf(buf, size, "%s", pinmux_mode[mode]);
227 break;
228 case GPIOF_FUNC:
Patrice Chotard530b63c2018-12-03 10:52:54 +0100229 af_num = stm32_pinctrl_get_af(gpio_dev, gpio_idx);
Patrick Delaunay1da42692021-01-21 17:39:07 +0100230 snprintf(buf, size, "%s %d %s %s", pinmux_mode[mode], af_num,
231 pinmux_otype[otype], pinmux_bias[pupd]);
Patrice Chotardb42d9382018-10-24 14:10:20 +0200232 break;
233 case GPIOF_OUTPUT:
Patrick Delaunay1da42692021-01-21 17:39:07 +0100234 snprintf(buf, size, "%s %s %s %s",
235 pinmux_mode[mode], pinmux_otype[otype],
236 pinmux_bias[pupd], label ? label : "");
Patrick Delaunayda7a0bb2020-06-04 14:30:33 +0200237 break;
Patrice Chotardb42d9382018-10-24 14:10:20 +0200238 case GPIOF_INPUT:
Patrick Delaunay1da42692021-01-21 17:39:07 +0100239 snprintf(buf, size, "%s %s %s", pinmux_mode[mode],
Patrick Delaunayb305dbc2020-10-28 10:49:07 +0100240 pinmux_bias[pupd], label ? label : "");
Patrice Chotardb42d9382018-10-24 14:10:20 +0200241 break;
242 }
243
244 return 0;
245}
246
Benjamin Gaignard075b0182018-11-27 13:49:53 +0100247#endif
248
Patrick Delaunay91ca91e2019-06-21 15:26:52 +0200249static int stm32_pinctrl_probe(struct udevice *dev)
Patrice Chotard8f651ca2018-10-24 14:10:18 +0200250{
251 struct stm32_pinctrl_priv *priv = dev_get_priv(dev);
Patrice Chotard8f651ca2018-10-24 14:10:18 +0200252 int ret;
253
254 INIT_LIST_HEAD(&priv->gpio_dev);
255
Benjamin Gaignard075b0182018-11-27 13:49:53 +0100256 /* hwspinlock property is optional, just log the error */
257 ret = hwspinlock_get_by_index(dev, 0, &priv->hws);
258 if (ret)
Patrick Delaunay28b3e7b2020-11-06 19:01:32 +0100259 dev_dbg(dev, "hwspinlock_get_by_index may have failed (%d)\n",
260 ret);
Benjamin Gaignard075b0182018-11-27 13:49:53 +0100261
Patrice Chotard8f651ca2018-10-24 14:10:18 +0200262 return 0;
263}
Patrice Chotard8f651ca2018-10-24 14:10:18 +0200264
Vikas Manocha77417102017-04-10 15:02:57 -0700265static int stm32_gpio_config(struct gpio_desc *desc,
266 const struct stm32_gpio_ctl *ctl)
267{
268 struct stm32_gpio_priv *priv = dev_get_priv(desc->dev);
269 struct stm32_gpio_regs *regs = priv->regs;
Benjamin Gaignard075b0182018-11-27 13:49:53 +0100270 struct stm32_pinctrl_priv *ctrl_priv;
271 int ret;
Vikas Manocha77417102017-04-10 15:02:57 -0700272 u32 index;
273
274 if (!ctl || ctl->af > 15 || ctl->mode > 3 || ctl->otype > 1 ||
275 ctl->pupd > 2 || ctl->speed > 3)
276 return -EINVAL;
277
Benjamin Gaignard075b0182018-11-27 13:49:53 +0100278 ctrl_priv = dev_get_priv(dev_get_parent(desc->dev));
279 ret = hwspinlock_lock_timeout(&ctrl_priv->hws, 10);
280 if (ret == -ETIME) {
281 dev_err(desc->dev, "HWSpinlock timeout\n");
282 return ret;
283 }
284
Vikas Manocha77417102017-04-10 15:02:57 -0700285 index = (desc->offset & 0x07) * 4;
286 clrsetbits_le32(&regs->afr[desc->offset >> 3], AFR_MASK << index,
287 ctl->af << index);
288
289 index = desc->offset * 2;
290 clrsetbits_le32(&regs->moder, MODE_BITS_MASK << index,
291 ctl->mode << index);
292 clrsetbits_le32(&regs->ospeedr, OSPEED_MASK << index,
293 ctl->speed << index);
294 clrsetbits_le32(&regs->pupdr, PUPD_MASK << index, ctl->pupd << index);
295
296 index = desc->offset;
297 clrsetbits_le32(&regs->otyper, OTYPE_MSK << index, ctl->otype << index);
298
Benjamin Gaignard075b0182018-11-27 13:49:53 +0100299 hwspinlock_unlock(&ctrl_priv->hws);
300
Vikas Manocha77417102017-04-10 15:02:57 -0700301 return 0;
302}
Patrick Delaunay8aeba622018-03-12 10:46:13 +0100303
Vikas Manocha94d53082017-02-12 10:25:49 -0800304static int prep_gpio_dsc(struct stm32_gpio_dsc *gpio_dsc, u32 port_pin)
305{
Patrick Delaunay8aeba622018-03-12 10:46:13 +0100306 gpio_dsc->port = (port_pin & 0x1F000) >> 12;
Vikas Manocha94d53082017-02-12 10:25:49 -0800307 gpio_dsc->pin = (port_pin & 0x0F00) >> 8;
Patrick Delaunay28b3e7b2020-11-06 19:01:32 +0100308 log_debug("GPIO:port= %d, pin= %d\n", gpio_dsc->port, gpio_dsc->pin);
Vikas Manocha94d53082017-02-12 10:25:49 -0800309
310 return 0;
311}
312
Patrick Delaunayd3bfad22020-09-09 17:50:14 +0200313static int prep_gpio_ctl(struct stm32_gpio_ctl *gpio_ctl, u32 gpio_fn,
314 ofnode node)
Vikas Manocha94d53082017-02-12 10:25:49 -0800315{
316 gpio_fn &= 0x00FF;
Vikas Manocha77417102017-04-10 15:02:57 -0700317 gpio_ctl->af = 0;
Vikas Manocha94d53082017-02-12 10:25:49 -0800318
319 switch (gpio_fn) {
320 case 0:
321 gpio_ctl->mode = STM32_GPIO_MODE_IN;
322 break;
323 case 1 ... 16:
324 gpio_ctl->mode = STM32_GPIO_MODE_AF;
325 gpio_ctl->af = gpio_fn - 1;
326 break;
327 case 17:
328 gpio_ctl->mode = STM32_GPIO_MODE_AN;
329 break;
330 default:
331 gpio_ctl->mode = STM32_GPIO_MODE_OUT;
332 break;
333 }
334
Patrick Delaunayd3bfad22020-09-09 17:50:14 +0200335 gpio_ctl->speed = ofnode_read_u32_default(node, "slew-rate", 0);
Vikas Manocha94d53082017-02-12 10:25:49 -0800336
Patrick Delaunayd3bfad22020-09-09 17:50:14 +0200337 if (ofnode_read_bool(node, "drive-open-drain"))
Vikas Manocha94d53082017-02-12 10:25:49 -0800338 gpio_ctl->otype = STM32_GPIO_OTYPE_OD;
339 else
340 gpio_ctl->otype = STM32_GPIO_OTYPE_PP;
341
Patrick Delaunayd3bfad22020-09-09 17:50:14 +0200342 if (ofnode_read_bool(node, "bias-pull-up"))
Vikas Manocha94d53082017-02-12 10:25:49 -0800343 gpio_ctl->pupd = STM32_GPIO_PUPD_UP;
Patrick Delaunayd3bfad22020-09-09 17:50:14 +0200344 else if (ofnode_read_bool(node, "bias-pull-down"))
Vikas Manocha94d53082017-02-12 10:25:49 -0800345 gpio_ctl->pupd = STM32_GPIO_PUPD_DOWN;
346 else
347 gpio_ctl->pupd = STM32_GPIO_PUPD_NO;
348
Patrick Delaunay28b3e7b2020-11-06 19:01:32 +0100349 log_debug("gpio fn= %d, slew-rate= %x, op type= %x, pull-upd is = %x\n",
350 gpio_fn, gpio_ctl->speed, gpio_ctl->otype,
351 gpio_ctl->pupd);
Vikas Manocha94d53082017-02-12 10:25:49 -0800352
353 return 0;
354}
355
Patrick Delaunayd3bfad22020-09-09 17:50:14 +0200356static int stm32_pinctrl_config(ofnode node)
Vikas Manocha94d53082017-02-12 10:25:49 -0800357{
Vikas Manocha58fb3c82017-04-10 15:03:04 -0700358 u32 pin_mux[MAX_PINS_ONE_IP];
Vikas Manocha94d53082017-02-12 10:25:49 -0800359 int rv, len;
Patrick Delaunayd3bfad22020-09-09 17:50:14 +0200360 ofnode subnode;
Vikas Manocha94d53082017-02-12 10:25:49 -0800361
Vikas Manocha94d53082017-02-12 10:25:49 -0800362 /*
363 * check for "pinmux" property in each subnode (e.g. pins1 and pins2 for
364 * usart1) of pin controller phandle "pinctrl-0"
365 * */
Patrick Delaunayd3bfad22020-09-09 17:50:14 +0200366 ofnode_for_each_subnode(subnode, node) {
Vikas Manocha94d53082017-02-12 10:25:49 -0800367 struct stm32_gpio_dsc gpio_dsc;
368 struct stm32_gpio_ctl gpio_ctl;
369 int i;
370
Patrick Delaunayd3bfad22020-09-09 17:50:14 +0200371 rv = ofnode_read_size(subnode, "pinmux");
372 if (rv < 0)
373 return rv;
374 len = rv / sizeof(pin_mux[0]);
Patrick Delaunay28b3e7b2020-11-06 19:01:32 +0100375 log_debug("No of pinmux entries= %d\n", len);
Patrick Delaunayd3bfad22020-09-09 17:50:14 +0200376 if (len > MAX_PINS_ONE_IP)
Vikas Manocha94d53082017-02-12 10:25:49 -0800377 return -EINVAL;
Patrick Delaunayd3bfad22020-09-09 17:50:14 +0200378 rv = ofnode_read_u32_array(subnode, "pinmux", pin_mux, len);
379 if (rv < 0)
380 return rv;
Vikas Manocha94d53082017-02-12 10:25:49 -0800381 for (i = 0; i < len; i++) {
Vikas Manocha280057b2017-04-10 15:02:59 -0700382 struct gpio_desc desc;
Patrick Delaunay8aeba622018-03-12 10:46:13 +0100383
Patrick Delaunay28b3e7b2020-11-06 19:01:32 +0100384 log_debug("pinmux = %x\n", *(pin_mux + i));
Vikas Manocha94d53082017-02-12 10:25:49 -0800385 prep_gpio_dsc(&gpio_dsc, *(pin_mux + i));
Patrick Delaunayd3bfad22020-09-09 17:50:14 +0200386 prep_gpio_ctl(&gpio_ctl, *(pin_mux + i), subnode);
Vikas Manocha280057b2017-04-10 15:02:59 -0700387 rv = uclass_get_device_by_seq(UCLASS_GPIO,
Patrick Delaunay8aeba622018-03-12 10:46:13 +0100388 gpio_dsc.port,
389 &desc.dev);
Vikas Manocha280057b2017-04-10 15:02:59 -0700390 if (rv)
391 return rv;
392 desc.offset = gpio_dsc.pin;
393 rv = stm32_gpio_config(&desc, &gpio_ctl);
Patrick Delaunay28b3e7b2020-11-06 19:01:32 +0100394 log_debug("rv = %d\n\n", rv);
Vikas Manocha94d53082017-02-12 10:25:49 -0800395 if (rv)
396 return rv;
397 }
398 }
399
400 return 0;
401}
402
Patrice Chotard158abbf2019-06-21 15:39:23 +0200403static int stm32_pinctrl_bind(struct udevice *dev)
404{
405 ofnode node;
406 const char *name;
407 int ret;
408
409 dev_for_each_subnode(node, dev) {
Patrick Delaunay28b3e7b2020-11-06 19:01:32 +0100410 dev_dbg(dev, "bind %s\n", ofnode_get_name(node));
Patrice Chotard158abbf2019-06-21 15:39:23 +0200411
Patrick Delaunay4363aac2021-01-21 17:39:08 +0100412 if (!ofnode_is_enabled(node))
413 continue;
414
Patrice Chotard158abbf2019-06-21 15:39:23 +0200415 ofnode_get_property(node, "gpio-controller", &ret);
416 if (ret < 0)
417 continue;
418 /* Get the name of each gpio node */
419 name = ofnode_get_name(node);
420 if (!name)
421 return -EINVAL;
422
423 /* Bind each gpio node */
424 ret = device_bind_driver_to_node(dev, "gpio_stm32",
425 name, node, NULL);
426 if (ret)
427 return ret;
428
Patrick Delaunay28b3e7b2020-11-06 19:01:32 +0100429 dev_dbg(dev, "bind %s\n", name);
Patrice Chotard158abbf2019-06-21 15:39:23 +0200430 }
431
432 return 0;
433}
434
Christophe Kerellobb44b962017-06-20 17:04:19 +0200435#if CONFIG_IS_ENABLED(PINCTRL_FULL)
436static int stm32_pinctrl_set_state(struct udevice *dev, struct udevice *config)
437{
Patrick Delaunayd3bfad22020-09-09 17:50:14 +0200438 return stm32_pinctrl_config(dev_ofnode(config));
Christophe Kerellobb44b962017-06-20 17:04:19 +0200439}
440#else /* PINCTRL_FULL */
Christophe Kerelload0376e2017-06-20 17:04:18 +0200441static int stm32_pinctrl_set_state_simple(struct udevice *dev,
442 struct udevice *periph)
443{
Christophe Kerelload0376e2017-06-20 17:04:18 +0200444 const fdt32_t *list;
445 uint32_t phandle;
Patrick Delaunayd3bfad22020-09-09 17:50:14 +0200446 ofnode config_node;
Christophe Kerelload0376e2017-06-20 17:04:18 +0200447 int size, i, ret;
448
Patrick Delaunayd3bfad22020-09-09 17:50:14 +0200449 list = ofnode_get_property(dev_ofnode(periph), "pinctrl-0", &size);
Christophe Kerelload0376e2017-06-20 17:04:18 +0200450 if (!list)
451 return -EINVAL;
452
Patrick Delaunay28b3e7b2020-11-06 19:01:32 +0100453 dev_dbg(dev, "periph->name = %s\n", periph->name);
Christophe Kerelload0376e2017-06-20 17:04:18 +0200454
455 size /= sizeof(*list);
456 for (i = 0; i < size; i++) {
457 phandle = fdt32_to_cpu(*list++);
458
Patrick Delaunayd3bfad22020-09-09 17:50:14 +0200459 config_node = ofnode_get_by_phandle(phandle);
460 if (!ofnode_valid(config_node)) {
Patrick Delaunay28b3e7b2020-11-06 19:01:32 +0100461 dev_err(periph,
462 "prop pinctrl-0 index %d invalid phandle\n", i);
Christophe Kerelload0376e2017-06-20 17:04:18 +0200463 return -EINVAL;
464 }
465
466 ret = stm32_pinctrl_config(config_node);
467 if (ret)
468 return ret;
469 }
470
471 return 0;
472}
Christophe Kerellobb44b962017-06-20 17:04:19 +0200473#endif /* PINCTRL_FULL */
Christophe Kerelload0376e2017-06-20 17:04:18 +0200474
Vikas Manocha94d53082017-02-12 10:25:49 -0800475static struct pinctrl_ops stm32_pinctrl_ops = {
Christophe Kerellobb44b962017-06-20 17:04:19 +0200476#if CONFIG_IS_ENABLED(PINCTRL_FULL)
477 .set_state = stm32_pinctrl_set_state,
478#else /* PINCTRL_FULL */
Vikas Manocha94d53082017-02-12 10:25:49 -0800479 .set_state_simple = stm32_pinctrl_set_state_simple,
Christophe Kerellobb44b962017-06-20 17:04:19 +0200480#endif /* PINCTRL_FULL */
Patrice Chotard8f651ca2018-10-24 14:10:18 +0200481#ifndef CONFIG_SPL_BUILD
Patrice Chotard4ff1c202018-10-24 14:10:19 +0200482 .get_pin_name = stm32_pinctrl_get_pin_name,
Patrice Chotard8f651ca2018-10-24 14:10:18 +0200483 .get_pins_count = stm32_pinctrl_get_pins_count,
Patrice Chotardb42d9382018-10-24 14:10:20 +0200484 .get_pin_muxing = stm32_pinctrl_get_pin_muxing,
Patrice Chotard8f651ca2018-10-24 14:10:18 +0200485#endif
Vikas Manocha94d53082017-02-12 10:25:49 -0800486};
487
488static const struct udevice_id stm32_pinctrl_ids[] = {
Patrice Chotard98693c22017-12-12 09:49:35 +0100489 { .compatible = "st,stm32f429-pinctrl" },
490 { .compatible = "st,stm32f469-pinctrl" },
Vikas Manocha94d53082017-02-12 10:25:49 -0800491 { .compatible = "st,stm32f746-pinctrl" },
Patrice Chotarddd18df42018-12-11 14:49:18 +0100492 { .compatible = "st,stm32f769-pinctrl" },
Patrice Chotard092e72c2017-09-13 18:00:04 +0200493 { .compatible = "st,stm32h743-pinctrl" },
Patrick Delaunay8aeba622018-03-12 10:46:13 +0100494 { .compatible = "st,stm32mp157-pinctrl" },
495 { .compatible = "st,stm32mp157-z-pinctrl" },
Vikas Manocha94d53082017-02-12 10:25:49 -0800496 { }
497};
498
499U_BOOT_DRIVER(pinctrl_stm32) = {
Patrice Chotard8f651ca2018-10-24 14:10:18 +0200500 .name = "pinctrl_stm32",
501 .id = UCLASS_PINCTRL,
502 .of_match = stm32_pinctrl_ids,
503 .ops = &stm32_pinctrl_ops,
Patrice Chotard158abbf2019-06-21 15:39:23 +0200504 .bind = stm32_pinctrl_bind,
Patrice Chotard8f651ca2018-10-24 14:10:18 +0200505 .probe = stm32_pinctrl_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700506 .priv_auto = sizeof(struct stm32_pinctrl_priv),
Vikas Manocha94d53082017-02-12 10:25:49 -0800507};