blob: 7120b8edba002d467a30e12640bdca519f7229bd [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/gpio.h>
14#include <asm/io.h>
Simon Glass336d4612020-02-03 07:36:16 -070015#include <dm/device_compat.h>
Patrice Chotard73858262019-07-30 19:16:10 +020016#include <dm/lists.h>
17#include <dm/pinctrl.h>
Simon Glasscd93d622020-05-10 11:40:13 -060018#include <linux/bitops.h>
Simon Glass61b29b82020-02-03 07:36:15 -070019#include <linux/err.h>
Simon Glass4d72caa2020-05-10 11:40:01 -060020#include <linux/libfdt.h>
Simon Glass1e94b462023-09-14 18:21:46 -060021#include <linux/printk.h>
Vikas Manocha94d53082017-02-12 10:25:49 -080022
Patrick Delaunay56a368f2021-10-22 20:12:34 +020023#include "../gpio/stm32_gpio_priv.h"
24
Vikas Manocha58fb3c82017-04-10 15:03:04 -070025#define MAX_PINS_ONE_IP 70
Vikas Manocha77417102017-04-10 15:02:57 -070026#define MODE_BITS_MASK 3
27#define OSPEED_MASK 3
28#define PUPD_MASK 3
29#define OTYPE_MSK 1
30#define AFR_MASK 0xF
31
Patrice Chotard8f651ca2018-10-24 14:10:18 +020032struct stm32_pinctrl_priv {
Benjamin Gaignard075b0182018-11-27 13:49:53 +010033 struct hwspinlock hws;
Patrice Chotard8f651ca2018-10-24 14:10:18 +020034 int pinctrl_ngpios;
35 struct list_head gpio_dev;
36};
37
38struct stm32_gpio_bank {
39 struct udevice *gpio_dev;
40 struct list_head list;
41};
42
Benjamin Gaignard075b0182018-11-27 13:49:53 +010043#ifndef CONFIG_SPL_BUILD
44
Patrice Chotard4ff1c202018-10-24 14:10:19 +020045static char pin_name[PINNAME_SIZE];
Patrice Chotard4382e552022-04-22 09:38:29 +020046static const char * const pinmux_mode[GPIOF_COUNT] = {
47 [GPIOF_INPUT] = "gpio input",
48 [GPIOF_OUTPUT] = "gpio output",
49 [GPIOF_UNUSED] = "analog",
50 [GPIOF_UNKNOWN] = "unknown",
51 [GPIOF_FUNC] = "alt function",
Patrice Chotardb42d9382018-10-24 14:10:20 +020052};
53
Patrick Delaunayb305dbc2020-10-28 10:49:07 +010054static const char * const pinmux_bias[] = {
55 [STM32_GPIO_PUPD_NO] = "",
56 [STM32_GPIO_PUPD_UP] = "pull-up",
57 [STM32_GPIO_PUPD_DOWN] = "pull-down",
Patrick Delaunayda7a0bb2020-06-04 14:30:33 +020058};
59
Patrick Delaunay1da42692021-01-21 17:39:07 +010060static const char * const pinmux_otype[] = {
Patrick Delaunayb305dbc2020-10-28 10:49:07 +010061 [STM32_GPIO_OTYPE_PP] = "push-pull",
62 [STM32_GPIO_OTYPE_OD] = "open-drain",
Patrick Delaunayda7a0bb2020-06-04 14:30:33 +020063};
64
Patrice Chotardbff0d842023-03-27 09:46:41 +020065static const char * const pinmux_speed[] = {
66 [STM32_GPIO_SPEED_2M] = "Low speed",
67 [STM32_GPIO_SPEED_25M] = "Medium speed",
68 [STM32_GPIO_SPEED_50M] = "High speed",
69 [STM32_GPIO_SPEED_100M] = "Very-high speed",
70};
71
Patrice Chotardb42d9382018-10-24 14:10:20 +020072static int stm32_pinctrl_get_af(struct udevice *dev, unsigned int offset)
73{
74 struct stm32_gpio_priv *priv = dev_get_priv(dev);
75 struct stm32_gpio_regs *regs = priv->regs;
76 u32 af;
77 u32 alt_shift = (offset % 8) * 4;
78 u32 alt_index = offset / 8;
79
80 af = (readl(&regs->afr[alt_index]) &
81 GENMASK(alt_shift + 3, alt_shift)) >> alt_shift;
82
83 return af;
84}
85
Patrice Chotard04355042018-12-03 10:52:50 +010086static int stm32_populate_gpio_dev_list(struct udevice *dev)
87{
88 struct stm32_pinctrl_priv *priv = dev_get_priv(dev);
89 struct udevice *gpio_dev;
90 struct udevice *child;
91 struct stm32_gpio_bank *gpio_bank;
92 int ret;
93
94 /*
95 * parse pin-controller sub-nodes (ie gpio bank nodes) and fill
96 * a list with all gpio device reference which belongs to the
97 * current pin-controller. This list is used to find pin_name and
98 * pin muxing
99 */
100 list_for_each_entry(child, &dev->child_head, sibling_node) {
101 ret = uclass_get_device_by_name(UCLASS_GPIO, child->name,
102 &gpio_dev);
103 if (ret < 0)
104 continue;
105
106 gpio_bank = malloc(sizeof(*gpio_bank));
107 if (!gpio_bank) {
108 dev_err(dev, "Not enough memory\n");
109 return -ENOMEM;
110 }
111
112 gpio_bank->gpio_dev = gpio_dev;
113 list_add_tail(&gpio_bank->list, &priv->gpio_dev);
114 }
115
116 return 0;
117}
118
Patrice Chotard8f651ca2018-10-24 14:10:18 +0200119static int stm32_pinctrl_get_pins_count(struct udevice *dev)
120{
121 struct stm32_pinctrl_priv *priv = dev_get_priv(dev);
122 struct gpio_dev_priv *uc_priv;
123 struct stm32_gpio_bank *gpio_bank;
124
125 /*
126 * if get_pins_count has already been executed once on this
127 * pin-controller, no need to run it again
128 */
129 if (priv->pinctrl_ngpios)
130 return priv->pinctrl_ngpios;
131
Patrice Chotard04355042018-12-03 10:52:50 +0100132 if (list_empty(&priv->gpio_dev))
133 stm32_populate_gpio_dev_list(dev);
Patrice Chotard8f651ca2018-10-24 14:10:18 +0200134 /*
135 * walk through all banks to retrieve the pin-controller
136 * pins number
137 */
138 list_for_each_entry(gpio_bank, &priv->gpio_dev, list) {
139 uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev);
140
141 priv->pinctrl_ngpios += uc_priv->gpio_count;
142 }
143
144 return priv->pinctrl_ngpios;
145}
146
Patrice Chotard4ff1c202018-10-24 14:10:19 +0200147static struct udevice *stm32_pinctrl_get_gpio_dev(struct udevice *dev,
Patrice Chotard530b63c2018-12-03 10:52:54 +0100148 unsigned int selector,
149 unsigned int *idx)
Patrice Chotard4ff1c202018-10-24 14:10:19 +0200150{
151 struct stm32_pinctrl_priv *priv = dev_get_priv(dev);
152 struct stm32_gpio_bank *gpio_bank;
153 struct gpio_dev_priv *uc_priv;
Patrice Chotard530b63c2018-12-03 10:52:54 +0100154 int pin_count = 0;
Patrice Chotard4ff1c202018-10-24 14:10:19 +0200155
Patrice Chotard04355042018-12-03 10:52:50 +0100156 if (list_empty(&priv->gpio_dev))
157 stm32_populate_gpio_dev_list(dev);
158
Patrice Chotard4ff1c202018-10-24 14:10:19 +0200159 /* look up for the bank which owns the requested pin */
160 list_for_each_entry(gpio_bank, &priv->gpio_dev, list) {
161 uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev);
162
Patrice Chotard530b63c2018-12-03 10:52:54 +0100163 if (selector < (pin_count + uc_priv->gpio_count)) {
164 /*
165 * we found the bank, convert pin selector to
166 * gpio bank index
167 */
Patrice Chotard427f4522022-04-22 09:38:31 +0200168 *idx = selector - pin_count;
Patrice Chotard4ff1c202018-10-24 14:10:19 +0200169
Patrice Chotard530b63c2018-12-03 10:52:54 +0100170 return gpio_bank->gpio_dev;
171 }
172 pin_count += uc_priv->gpio_count;
Patrice Chotard4ff1c202018-10-24 14:10:19 +0200173 }
174
175 return NULL;
176}
177
178static const char *stm32_pinctrl_get_pin_name(struct udevice *dev,
179 unsigned int selector)
180{
181 struct gpio_dev_priv *uc_priv;
182 struct udevice *gpio_dev;
Patrice Chotard530b63c2018-12-03 10:52:54 +0100183 unsigned int gpio_idx;
Patrice Chotard4ff1c202018-10-24 14:10:19 +0200184
185 /* look up for the bank which owns the requested pin */
Patrice Chotard530b63c2018-12-03 10:52:54 +0100186 gpio_dev = stm32_pinctrl_get_gpio_dev(dev, selector, &gpio_idx);
Patrice Chotard4ff1c202018-10-24 14:10:19 +0200187 if (!gpio_dev) {
188 snprintf(pin_name, PINNAME_SIZE, "Error");
189 } else {
190 uc_priv = dev_get_uclass_priv(gpio_dev);
191
192 snprintf(pin_name, PINNAME_SIZE, "%s%d",
193 uc_priv->bank_name,
Patrice Chotard530b63c2018-12-03 10:52:54 +0100194 gpio_idx);
Patrice Chotard4ff1c202018-10-24 14:10:19 +0200195 }
196
197 return pin_name;
198}
Patrice Chotardb42d9382018-10-24 14:10:20 +0200199
200static int stm32_pinctrl_get_pin_muxing(struct udevice *dev,
201 unsigned int selector,
202 char *buf,
203 int size)
204{
205 struct udevice *gpio_dev;
Patrick Delaunayda7a0bb2020-06-04 14:30:33 +0200206 struct stm32_gpio_priv *priv;
Patrice Chotardb42d9382018-10-24 14:10:20 +0200207 const char *label;
Patrice Chotardb42d9382018-10-24 14:10:20 +0200208 int mode;
209 int af_num;
Patrice Chotard530b63c2018-12-03 10:52:54 +0100210 unsigned int gpio_idx;
Patrick Delaunayda7a0bb2020-06-04 14:30:33 +0200211 u32 pupd, otype;
Patrice Chotardbff0d842023-03-27 09:46:41 +0200212 u8 speed;
Patrice Chotardb42d9382018-10-24 14:10:20 +0200213
214 /* look up for the bank which owns the requested pin */
Patrice Chotard530b63c2018-12-03 10:52:54 +0100215 gpio_dev = stm32_pinctrl_get_gpio_dev(dev, selector, &gpio_idx);
Patrice Chotardb42d9382018-10-24 14:10:20 +0200216
217 if (!gpio_dev)
218 return -ENODEV;
219
Patrice Chotard530b63c2018-12-03 10:52:54 +0100220 mode = gpio_get_raw_function(gpio_dev, gpio_idx, &label);
Patrice Chotard530b63c2018-12-03 10:52:54 +0100221 dev_dbg(dev, "selector = %d gpio_idx = %d mode = %d\n",
222 selector, gpio_idx, mode);
Patrick Delaunayda7a0bb2020-06-04 14:30:33 +0200223 priv = dev_get_priv(gpio_dev);
Patrick Delaunayb305dbc2020-10-28 10:49:07 +0100224 pupd = (readl(&priv->regs->pupdr) >> (gpio_idx * 2)) & PUPD_MASK;
Patrick Delaunay1da42692021-01-21 17:39:07 +0100225 otype = (readl(&priv->regs->otyper) >> gpio_idx) & OTYPE_MSK;
Patrice Chotardbff0d842023-03-27 09:46:41 +0200226 speed = (readl(&priv->regs->ospeedr) >> gpio_idx * 2) & OSPEED_MASK;
Patrice Chotardb42d9382018-10-24 14:10:20 +0200227
228 switch (mode) {
229 case GPIOF_UNKNOWN:
Patrice Chotardb42d9382018-10-24 14:10:20 +0200230 case GPIOF_UNUSED:
231 snprintf(buf, size, "%s", pinmux_mode[mode]);
232 break;
233 case GPIOF_FUNC:
Patrice Chotard530b63c2018-12-03 10:52:54 +0100234 af_num = stm32_pinctrl_get_af(gpio_dev, gpio_idx);
Patrice Chotardbff0d842023-03-27 09:46:41 +0200235 snprintf(buf, size, "%s %d %s %s %s", pinmux_mode[mode], af_num,
236 pinmux_otype[otype], pinmux_bias[pupd],
237 pinmux_speed[speed]);
Patrice Chotardb42d9382018-10-24 14:10:20 +0200238 break;
239 case GPIOF_OUTPUT:
Patrice Chotardbff0d842023-03-27 09:46:41 +0200240 snprintf(buf, size, "%s %s %s %s %s",
Patrick Delaunay1da42692021-01-21 17:39:07 +0100241 pinmux_mode[mode], pinmux_otype[otype],
Patrice Chotardbff0d842023-03-27 09:46:41 +0200242 pinmux_bias[pupd], label ? label : "",
243 pinmux_speed[speed]);
Patrick Delaunayda7a0bb2020-06-04 14:30:33 +0200244 break;
Patrice Chotardb42d9382018-10-24 14:10:20 +0200245 case GPIOF_INPUT:
Patrick Delaunay1da42692021-01-21 17:39:07 +0100246 snprintf(buf, size, "%s %s %s", pinmux_mode[mode],
Patrick Delaunayb305dbc2020-10-28 10:49:07 +0100247 pinmux_bias[pupd], label ? label : "");
Patrice Chotardb42d9382018-10-24 14:10:20 +0200248 break;
249 }
250
251 return 0;
252}
253
Benjamin Gaignard075b0182018-11-27 13:49:53 +0100254#endif
255
Patrick Delaunay91ca91e2019-06-21 15:26:52 +0200256static int stm32_pinctrl_probe(struct udevice *dev)
Patrice Chotard8f651ca2018-10-24 14:10:18 +0200257{
258 struct stm32_pinctrl_priv *priv = dev_get_priv(dev);
Patrice Chotard8f651ca2018-10-24 14:10:18 +0200259 int ret;
260
261 INIT_LIST_HEAD(&priv->gpio_dev);
262
Benjamin Gaignard075b0182018-11-27 13:49:53 +0100263 /* hwspinlock property is optional, just log the error */
264 ret = hwspinlock_get_by_index(dev, 0, &priv->hws);
265 if (ret)
Patrick Delaunay28b3e7b2020-11-06 19:01:32 +0100266 dev_dbg(dev, "hwspinlock_get_by_index may have failed (%d)\n",
267 ret);
Benjamin Gaignard075b0182018-11-27 13:49:53 +0100268
Patrice Chotard8f651ca2018-10-24 14:10:18 +0200269 return 0;
270}
Patrice Chotard8f651ca2018-10-24 14:10:18 +0200271
Patrice Chotard2c38f7c2022-08-30 14:09:13 +0200272static int stm32_gpio_config(ofnode node,
273 struct gpio_desc *desc,
Vikas Manocha77417102017-04-10 15:02:57 -0700274 const struct stm32_gpio_ctl *ctl)
275{
276 struct stm32_gpio_priv *priv = dev_get_priv(desc->dev);
Patrice Chotard2c38f7c2022-08-30 14:09:13 +0200277 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc->dev);
Vikas Manocha77417102017-04-10 15:02:57 -0700278 struct stm32_gpio_regs *regs = priv->regs;
Benjamin Gaignard075b0182018-11-27 13:49:53 +0100279 struct stm32_pinctrl_priv *ctrl_priv;
280 int ret;
Vikas Manocha77417102017-04-10 15:02:57 -0700281 u32 index;
282
283 if (!ctl || ctl->af > 15 || ctl->mode > 3 || ctl->otype > 1 ||
284 ctl->pupd > 2 || ctl->speed > 3)
285 return -EINVAL;
286
Benjamin Gaignard075b0182018-11-27 13:49:53 +0100287 ctrl_priv = dev_get_priv(dev_get_parent(desc->dev));
288 ret = hwspinlock_lock_timeout(&ctrl_priv->hws, 10);
289 if (ret == -ETIME) {
290 dev_err(desc->dev, "HWSpinlock timeout\n");
291 return ret;
292 }
293
Vikas Manocha77417102017-04-10 15:02:57 -0700294 index = (desc->offset & 0x07) * 4;
295 clrsetbits_le32(&regs->afr[desc->offset >> 3], AFR_MASK << index,
296 ctl->af << index);
297
298 index = desc->offset * 2;
299 clrsetbits_le32(&regs->moder, MODE_BITS_MASK << index,
300 ctl->mode << index);
301 clrsetbits_le32(&regs->ospeedr, OSPEED_MASK << index,
302 ctl->speed << index);
303 clrsetbits_le32(&regs->pupdr, PUPD_MASK << index, ctl->pupd << index);
304
305 index = desc->offset;
306 clrsetbits_le32(&regs->otyper, OTYPE_MSK << index, ctl->otype << index);
307
Patrice Chotard2c38f7c2022-08-30 14:09:13 +0200308 uc_priv->name[desc->offset] = strdup(ofnode_get_name(node));
309
Benjamin Gaignard075b0182018-11-27 13:49:53 +0100310 hwspinlock_unlock(&ctrl_priv->hws);
311
Vikas Manocha77417102017-04-10 15:02:57 -0700312 return 0;
313}
Patrick Delaunay8aeba622018-03-12 10:46:13 +0100314
Vikas Manocha94d53082017-02-12 10:25:49 -0800315static int prep_gpio_dsc(struct stm32_gpio_dsc *gpio_dsc, u32 port_pin)
316{
Patrick Delaunay8aeba622018-03-12 10:46:13 +0100317 gpio_dsc->port = (port_pin & 0x1F000) >> 12;
Vikas Manocha94d53082017-02-12 10:25:49 -0800318 gpio_dsc->pin = (port_pin & 0x0F00) >> 8;
Patrick Delaunay28b3e7b2020-11-06 19:01:32 +0100319 log_debug("GPIO:port= %d, pin= %d\n", gpio_dsc->port, gpio_dsc->pin);
Vikas Manocha94d53082017-02-12 10:25:49 -0800320
321 return 0;
322}
323
Patrick Delaunayd3bfad22020-09-09 17:50:14 +0200324static int prep_gpio_ctl(struct stm32_gpio_ctl *gpio_ctl, u32 gpio_fn,
325 ofnode node)
Vikas Manocha94d53082017-02-12 10:25:49 -0800326{
327 gpio_fn &= 0x00FF;
Vikas Manocha77417102017-04-10 15:02:57 -0700328 gpio_ctl->af = 0;
Vikas Manocha94d53082017-02-12 10:25:49 -0800329
330 switch (gpio_fn) {
331 case 0:
332 gpio_ctl->mode = STM32_GPIO_MODE_IN;
333 break;
334 case 1 ... 16:
335 gpio_ctl->mode = STM32_GPIO_MODE_AF;
336 gpio_ctl->af = gpio_fn - 1;
337 break;
338 case 17:
339 gpio_ctl->mode = STM32_GPIO_MODE_AN;
340 break;
341 default:
342 gpio_ctl->mode = STM32_GPIO_MODE_OUT;
343 break;
344 }
345
Patrick Delaunayd3bfad22020-09-09 17:50:14 +0200346 gpio_ctl->speed = ofnode_read_u32_default(node, "slew-rate", 0);
Vikas Manocha94d53082017-02-12 10:25:49 -0800347
Patrick Delaunayd3bfad22020-09-09 17:50:14 +0200348 if (ofnode_read_bool(node, "drive-open-drain"))
Vikas Manocha94d53082017-02-12 10:25:49 -0800349 gpio_ctl->otype = STM32_GPIO_OTYPE_OD;
350 else
351 gpio_ctl->otype = STM32_GPIO_OTYPE_PP;
352
Patrick Delaunayd3bfad22020-09-09 17:50:14 +0200353 if (ofnode_read_bool(node, "bias-pull-up"))
Vikas Manocha94d53082017-02-12 10:25:49 -0800354 gpio_ctl->pupd = STM32_GPIO_PUPD_UP;
Patrick Delaunayd3bfad22020-09-09 17:50:14 +0200355 else if (ofnode_read_bool(node, "bias-pull-down"))
Vikas Manocha94d53082017-02-12 10:25:49 -0800356 gpio_ctl->pupd = STM32_GPIO_PUPD_DOWN;
357 else
358 gpio_ctl->pupd = STM32_GPIO_PUPD_NO;
359
Patrick Delaunay28b3e7b2020-11-06 19:01:32 +0100360 log_debug("gpio fn= %d, slew-rate= %x, op type= %x, pull-upd is = %x\n",
361 gpio_fn, gpio_ctl->speed, gpio_ctl->otype,
362 gpio_ctl->pupd);
Vikas Manocha94d53082017-02-12 10:25:49 -0800363
364 return 0;
365}
366
Patrick Delaunayd3bfad22020-09-09 17:50:14 +0200367static int stm32_pinctrl_config(ofnode node)
Vikas Manocha94d53082017-02-12 10:25:49 -0800368{
Vikas Manocha58fb3c82017-04-10 15:03:04 -0700369 u32 pin_mux[MAX_PINS_ONE_IP];
Vikas Manocha94d53082017-02-12 10:25:49 -0800370 int rv, len;
Patrick Delaunayd3bfad22020-09-09 17:50:14 +0200371 ofnode subnode;
Vikas Manocha94d53082017-02-12 10:25:49 -0800372
Vikas Manocha94d53082017-02-12 10:25:49 -0800373 /*
374 * check for "pinmux" property in each subnode (e.g. pins1 and pins2 for
375 * usart1) of pin controller phandle "pinctrl-0"
376 * */
Patrick Delaunayd3bfad22020-09-09 17:50:14 +0200377 ofnode_for_each_subnode(subnode, node) {
Vikas Manocha94d53082017-02-12 10:25:49 -0800378 struct stm32_gpio_dsc gpio_dsc;
379 struct stm32_gpio_ctl gpio_ctl;
380 int i;
381
Patrick Delaunayd3bfad22020-09-09 17:50:14 +0200382 rv = ofnode_read_size(subnode, "pinmux");
383 if (rv < 0)
384 return rv;
385 len = rv / sizeof(pin_mux[0]);
Patrick Delaunay28b3e7b2020-11-06 19:01:32 +0100386 log_debug("No of pinmux entries= %d\n", len);
Patrick Delaunayd3bfad22020-09-09 17:50:14 +0200387 if (len > MAX_PINS_ONE_IP)
Vikas Manocha94d53082017-02-12 10:25:49 -0800388 return -EINVAL;
Patrick Delaunayd3bfad22020-09-09 17:50:14 +0200389 rv = ofnode_read_u32_array(subnode, "pinmux", pin_mux, len);
390 if (rv < 0)
391 return rv;
Vikas Manocha94d53082017-02-12 10:25:49 -0800392 for (i = 0; i < len; i++) {
Vikas Manocha280057b2017-04-10 15:02:59 -0700393 struct gpio_desc desc;
Patrick Delaunay8aeba622018-03-12 10:46:13 +0100394
Patrick Delaunay28b3e7b2020-11-06 19:01:32 +0100395 log_debug("pinmux = %x\n", *(pin_mux + i));
Vikas Manocha94d53082017-02-12 10:25:49 -0800396 prep_gpio_dsc(&gpio_dsc, *(pin_mux + i));
Patrick Delaunayd3bfad22020-09-09 17:50:14 +0200397 prep_gpio_ctl(&gpio_ctl, *(pin_mux + i), subnode);
Vikas Manocha280057b2017-04-10 15:02:59 -0700398 rv = uclass_get_device_by_seq(UCLASS_GPIO,
Patrick Delaunay8aeba622018-03-12 10:46:13 +0100399 gpio_dsc.port,
400 &desc.dev);
Vikas Manocha280057b2017-04-10 15:02:59 -0700401 if (rv)
402 return rv;
403 desc.offset = gpio_dsc.pin;
Patrice Chotard2c38f7c2022-08-30 14:09:13 +0200404 rv = stm32_gpio_config(node, &desc, &gpio_ctl);
Patrick Delaunay28b3e7b2020-11-06 19:01:32 +0100405 log_debug("rv = %d\n\n", rv);
Vikas Manocha94d53082017-02-12 10:25:49 -0800406 if (rv)
407 return rv;
408 }
409 }
410
411 return 0;
412}
413
Patrice Chotard158abbf2019-06-21 15:39:23 +0200414static int stm32_pinctrl_bind(struct udevice *dev)
415{
416 ofnode node;
417 const char *name;
418 int ret;
419
420 dev_for_each_subnode(node, dev) {
Patrick Delaunay28b3e7b2020-11-06 19:01:32 +0100421 dev_dbg(dev, "bind %s\n", ofnode_get_name(node));
Patrice Chotard158abbf2019-06-21 15:39:23 +0200422
Patrick Delaunay4363aac2021-01-21 17:39:08 +0100423 if (!ofnode_is_enabled(node))
424 continue;
425
Patrice Chotard158abbf2019-06-21 15:39:23 +0200426 ofnode_get_property(node, "gpio-controller", &ret);
427 if (ret < 0)
428 continue;
429 /* Get the name of each gpio node */
430 name = ofnode_get_name(node);
431 if (!name)
432 return -EINVAL;
433
434 /* Bind each gpio node */
435 ret = device_bind_driver_to_node(dev, "gpio_stm32",
436 name, node, NULL);
437 if (ret)
438 return ret;
439
Patrick Delaunay28b3e7b2020-11-06 19:01:32 +0100440 dev_dbg(dev, "bind %s\n", name);
Patrice Chotard158abbf2019-06-21 15:39:23 +0200441 }
442
443 return 0;
444}
445
Christophe Kerellobb44b962017-06-20 17:04:19 +0200446#if CONFIG_IS_ENABLED(PINCTRL_FULL)
447static int stm32_pinctrl_set_state(struct udevice *dev, struct udevice *config)
448{
Patrick Delaunayd3bfad22020-09-09 17:50:14 +0200449 return stm32_pinctrl_config(dev_ofnode(config));
Christophe Kerellobb44b962017-06-20 17:04:19 +0200450}
451#else /* PINCTRL_FULL */
Christophe Kerelload0376e2017-06-20 17:04:18 +0200452static int stm32_pinctrl_set_state_simple(struct udevice *dev,
453 struct udevice *periph)
454{
Christophe Kerelload0376e2017-06-20 17:04:18 +0200455 const fdt32_t *list;
456 uint32_t phandle;
Patrick Delaunayd3bfad22020-09-09 17:50:14 +0200457 ofnode config_node;
Christophe Kerelload0376e2017-06-20 17:04:18 +0200458 int size, i, ret;
459
Patrick Delaunayd3bfad22020-09-09 17:50:14 +0200460 list = ofnode_get_property(dev_ofnode(periph), "pinctrl-0", &size);
Christophe Kerelload0376e2017-06-20 17:04:18 +0200461 if (!list)
462 return -EINVAL;
463
Patrick Delaunay28b3e7b2020-11-06 19:01:32 +0100464 dev_dbg(dev, "periph->name = %s\n", periph->name);
Christophe Kerelload0376e2017-06-20 17:04:18 +0200465
466 size /= sizeof(*list);
467 for (i = 0; i < size; i++) {
468 phandle = fdt32_to_cpu(*list++);
469
Patrick Delaunayd3bfad22020-09-09 17:50:14 +0200470 config_node = ofnode_get_by_phandle(phandle);
471 if (!ofnode_valid(config_node)) {
Patrick Delaunay28b3e7b2020-11-06 19:01:32 +0100472 dev_err(periph,
473 "prop pinctrl-0 index %d invalid phandle\n", i);
Christophe Kerelload0376e2017-06-20 17:04:18 +0200474 return -EINVAL;
475 }
476
477 ret = stm32_pinctrl_config(config_node);
478 if (ret)
479 return ret;
480 }
481
482 return 0;
483}
Christophe Kerellobb44b962017-06-20 17:04:19 +0200484#endif /* PINCTRL_FULL */
Christophe Kerelload0376e2017-06-20 17:04:18 +0200485
Vikas Manocha94d53082017-02-12 10:25:49 -0800486static struct pinctrl_ops stm32_pinctrl_ops = {
Christophe Kerellobb44b962017-06-20 17:04:19 +0200487#if CONFIG_IS_ENABLED(PINCTRL_FULL)
488 .set_state = stm32_pinctrl_set_state,
489#else /* PINCTRL_FULL */
Vikas Manocha94d53082017-02-12 10:25:49 -0800490 .set_state_simple = stm32_pinctrl_set_state_simple,
Christophe Kerellobb44b962017-06-20 17:04:19 +0200491#endif /* PINCTRL_FULL */
Patrice Chotard8f651ca2018-10-24 14:10:18 +0200492#ifndef CONFIG_SPL_BUILD
Patrice Chotard4ff1c202018-10-24 14:10:19 +0200493 .get_pin_name = stm32_pinctrl_get_pin_name,
Patrice Chotard8f651ca2018-10-24 14:10:18 +0200494 .get_pins_count = stm32_pinctrl_get_pins_count,
Patrice Chotardb42d9382018-10-24 14:10:20 +0200495 .get_pin_muxing = stm32_pinctrl_get_pin_muxing,
Patrice Chotard8f651ca2018-10-24 14:10:18 +0200496#endif
Vikas Manocha94d53082017-02-12 10:25:49 -0800497};
498
499static const struct udevice_id stm32_pinctrl_ids[] = {
Patrice Chotard98693c22017-12-12 09:49:35 +0100500 { .compatible = "st,stm32f429-pinctrl" },
501 { .compatible = "st,stm32f469-pinctrl" },
Vikas Manocha94d53082017-02-12 10:25:49 -0800502 { .compatible = "st,stm32f746-pinctrl" },
Patrice Chotarddd18df42018-12-11 14:49:18 +0100503 { .compatible = "st,stm32f769-pinctrl" },
Patrice Chotard092e72c2017-09-13 18:00:04 +0200504 { .compatible = "st,stm32h743-pinctrl" },
Patrick Delaunay8aeba622018-03-12 10:46:13 +0100505 { .compatible = "st,stm32mp157-pinctrl" },
506 { .compatible = "st,stm32mp157-z-pinctrl" },
Patrick Delaunaycf1d0fd2022-05-20 18:24:48 +0200507 { .compatible = "st,stm32mp135-pinctrl" },
Patrice Chotard778f4ea2023-10-27 16:43:02 +0200508 { .compatible = "st,stm32mp257-pinctrl" },
509 { .compatible = "st,stm32mp257-z-pinctrl" },
Vikas Manocha94d53082017-02-12 10:25:49 -0800510 { }
511};
512
513U_BOOT_DRIVER(pinctrl_stm32) = {
Patrice Chotard8f651ca2018-10-24 14:10:18 +0200514 .name = "pinctrl_stm32",
515 .id = UCLASS_PINCTRL,
516 .of_match = stm32_pinctrl_ids,
517 .ops = &stm32_pinctrl_ops,
Patrice Chotard158abbf2019-06-21 15:39:23 +0200518 .bind = stm32_pinctrl_bind,
Patrice Chotard8f651ca2018-10-24 14:10:18 +0200519 .probe = stm32_pinctrl_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700520 .priv_auto = sizeof(struct stm32_pinctrl_priv),
Vikas Manocha94d53082017-02-12 10:25:49 -0800521};