blob: 7120b8edba002d467a30e12640bdca519f7229bd [file] [log] [blame]
Patrick Delaunay4b3f0122020-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 Delaunay84a9a4e2020-11-06 19:01:32 +01006#define LOG_CATEGORY UCLASS_PINCTRL
7
Vikas Manocha07e9e412017-02-12 10:25:49 -08008#include <common.h>
Vikas Manocha07e9e412017-02-12 10:25:49 -08009#include <dm.h>
Benjamin Gaignard16f6f332018-11-27 13:49:53 +010010#include <hwspinlock.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070012#include <malloc.h>
Vikas Manochaec8630a2017-04-10 15:02:57 -070013#include <asm/gpio.h>
14#include <asm/io.h>
Simon Glass9bc15642020-02-03 07:36:16 -070015#include <dm/device_compat.h>
Patrice Chotarde16e8f42019-07-30 19:16:10 +020016#include <dm/lists.h>
17#include <dm/pinctrl.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060018#include <linux/bitops.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070019#include <linux/err.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060020#include <linux/libfdt.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060021#include <linux/printk.h>
Vikas Manocha07e9e412017-02-12 10:25:49 -080022
Patrick Delaunay7dccd892021-10-22 20:12:34 +020023#include "../gpio/stm32_gpio_priv.h"
24
Vikas Manocha40ddb3a2017-04-10 15:03:04 -070025#define MAX_PINS_ONE_IP 70
Vikas Manochaec8630a2017-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 Chotardaaf68e82018-10-24 14:10:18 +020032struct stm32_pinctrl_priv {
Benjamin Gaignard16f6f332018-11-27 13:49:53 +010033 struct hwspinlock hws;
Patrice Chotardaaf68e82018-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 Gaignard16f6f332018-11-27 13:49:53 +010043#ifndef CONFIG_SPL_BUILD
44
Patrice Chotard881e8672018-10-24 14:10:19 +020045static char pin_name[PINNAME_SIZE];
Patrice Chotard22663902022-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 Chotarda46fb392018-10-24 14:10:20 +020052};
53
Patrick Delaunay6347ed92020-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 Delaunay8274fab2020-06-04 14:30:33 +020058};
59
Patrick Delaunay764d3ba2021-01-21 17:39:07 +010060static const char * const pinmux_otype[] = {
Patrick Delaunay6347ed92020-10-28 10:49:07 +010061 [STM32_GPIO_OTYPE_PP] = "push-pull",
62 [STM32_GPIO_OTYPE_OD] = "open-drain",
Patrick Delaunay8274fab2020-06-04 14:30:33 +020063};
64
Patrice Chotard8f0c89c2023-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 Chotarda46fb392018-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 Chotard7ef91082018-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 Chotardaaf68e82018-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 Chotard7ef91082018-12-03 10:52:50 +0100132 if (list_empty(&priv->gpio_dev))
133 stm32_populate_gpio_dev_list(dev);
Patrice Chotardaaf68e82018-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 Chotard881e8672018-10-24 14:10:19 +0200147static struct udevice *stm32_pinctrl_get_gpio_dev(struct udevice *dev,
Patrice Chotard0b968002018-12-03 10:52:54 +0100148 unsigned int selector,
149 unsigned int *idx)
Patrice Chotard881e8672018-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 Chotard0b968002018-12-03 10:52:54 +0100154 int pin_count = 0;
Patrice Chotard881e8672018-10-24 14:10:19 +0200155
Patrice Chotard7ef91082018-12-03 10:52:50 +0100156 if (list_empty(&priv->gpio_dev))
157 stm32_populate_gpio_dev_list(dev);
158
Patrice Chotard881e8672018-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 Chotard0b968002018-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 Chotard5554a542022-04-22 09:38:31 +0200168 *idx = selector - pin_count;
Patrice Chotard881e8672018-10-24 14:10:19 +0200169
Patrice Chotard0b968002018-12-03 10:52:54 +0100170 return gpio_bank->gpio_dev;
171 }
172 pin_count += uc_priv->gpio_count;
Patrice Chotard881e8672018-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 Chotard0b968002018-12-03 10:52:54 +0100183 unsigned int gpio_idx;
Patrice Chotard881e8672018-10-24 14:10:19 +0200184
185 /* look up for the bank which owns the requested pin */
Patrice Chotard0b968002018-12-03 10:52:54 +0100186 gpio_dev = stm32_pinctrl_get_gpio_dev(dev, selector, &gpio_idx);
Patrice Chotard881e8672018-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 Chotard0b968002018-12-03 10:52:54 +0100194 gpio_idx);
Patrice Chotard881e8672018-10-24 14:10:19 +0200195 }
196
197 return pin_name;
198}
Patrice Chotarda46fb392018-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 Delaunay8274fab2020-06-04 14:30:33 +0200206 struct stm32_gpio_priv *priv;
Patrice Chotarda46fb392018-10-24 14:10:20 +0200207 const char *label;
Patrice Chotarda46fb392018-10-24 14:10:20 +0200208 int mode;
209 int af_num;
Patrice Chotard0b968002018-12-03 10:52:54 +0100210 unsigned int gpio_idx;
Patrick Delaunay8274fab2020-06-04 14:30:33 +0200211 u32 pupd, otype;
Patrice Chotard8f0c89c2023-03-27 09:46:41 +0200212 u8 speed;
Patrice Chotarda46fb392018-10-24 14:10:20 +0200213
214 /* look up for the bank which owns the requested pin */
Patrice Chotard0b968002018-12-03 10:52:54 +0100215 gpio_dev = stm32_pinctrl_get_gpio_dev(dev, selector, &gpio_idx);
Patrice Chotarda46fb392018-10-24 14:10:20 +0200216
217 if (!gpio_dev)
218 return -ENODEV;
219
Patrice Chotard0b968002018-12-03 10:52:54 +0100220 mode = gpio_get_raw_function(gpio_dev, gpio_idx, &label);
Patrice Chotard0b968002018-12-03 10:52:54 +0100221 dev_dbg(dev, "selector = %d gpio_idx = %d mode = %d\n",
222 selector, gpio_idx, mode);
Patrick Delaunay8274fab2020-06-04 14:30:33 +0200223 priv = dev_get_priv(gpio_dev);
Patrick Delaunay6347ed92020-10-28 10:49:07 +0100224 pupd = (readl(&priv->regs->pupdr) >> (gpio_idx * 2)) & PUPD_MASK;
Patrick Delaunay764d3ba2021-01-21 17:39:07 +0100225 otype = (readl(&priv->regs->otyper) >> gpio_idx) & OTYPE_MSK;
Patrice Chotard8f0c89c2023-03-27 09:46:41 +0200226 speed = (readl(&priv->regs->ospeedr) >> gpio_idx * 2) & OSPEED_MASK;
Patrice Chotarda46fb392018-10-24 14:10:20 +0200227
228 switch (mode) {
229 case GPIOF_UNKNOWN:
Patrice Chotarda46fb392018-10-24 14:10:20 +0200230 case GPIOF_UNUSED:
231 snprintf(buf, size, "%s", pinmux_mode[mode]);
232 break;
233 case GPIOF_FUNC:
Patrice Chotard0b968002018-12-03 10:52:54 +0100234 af_num = stm32_pinctrl_get_af(gpio_dev, gpio_idx);
Patrice Chotard8f0c89c2023-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 Chotarda46fb392018-10-24 14:10:20 +0200238 break;
239 case GPIOF_OUTPUT:
Patrice Chotard8f0c89c2023-03-27 09:46:41 +0200240 snprintf(buf, size, "%s %s %s %s %s",
Patrick Delaunay764d3ba2021-01-21 17:39:07 +0100241 pinmux_mode[mode], pinmux_otype[otype],
Patrice Chotard8f0c89c2023-03-27 09:46:41 +0200242 pinmux_bias[pupd], label ? label : "",
243 pinmux_speed[speed]);
Patrick Delaunay8274fab2020-06-04 14:30:33 +0200244 break;
Patrice Chotarda46fb392018-10-24 14:10:20 +0200245 case GPIOF_INPUT:
Patrick Delaunay764d3ba2021-01-21 17:39:07 +0100246 snprintf(buf, size, "%s %s %s", pinmux_mode[mode],
Patrick Delaunay6347ed92020-10-28 10:49:07 +0100247 pinmux_bias[pupd], label ? label : "");
Patrice Chotarda46fb392018-10-24 14:10:20 +0200248 break;
249 }
250
251 return 0;
252}
253
Benjamin Gaignard16f6f332018-11-27 13:49:53 +0100254#endif
255
Patrick Delaunay4c11a112019-06-21 15:26:52 +0200256static int stm32_pinctrl_probe(struct udevice *dev)
Patrice Chotardaaf68e82018-10-24 14:10:18 +0200257{
258 struct stm32_pinctrl_priv *priv = dev_get_priv(dev);
Patrice Chotardaaf68e82018-10-24 14:10:18 +0200259 int ret;
260
261 INIT_LIST_HEAD(&priv->gpio_dev);
262
Benjamin Gaignard16f6f332018-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 Delaunay84a9a4e2020-11-06 19:01:32 +0100266 dev_dbg(dev, "hwspinlock_get_by_index may have failed (%d)\n",
267 ret);
Benjamin Gaignard16f6f332018-11-27 13:49:53 +0100268
Patrice Chotardaaf68e82018-10-24 14:10:18 +0200269 return 0;
270}
Patrice Chotardaaf68e82018-10-24 14:10:18 +0200271
Patrice Chotard866dfea2022-08-30 14:09:13 +0200272static int stm32_gpio_config(ofnode node,
273 struct gpio_desc *desc,
Vikas Manochaec8630a2017-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 Chotard866dfea2022-08-30 14:09:13 +0200277 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc->dev);
Vikas Manochaec8630a2017-04-10 15:02:57 -0700278 struct stm32_gpio_regs *regs = priv->regs;
Benjamin Gaignard16f6f332018-11-27 13:49:53 +0100279 struct stm32_pinctrl_priv *ctrl_priv;
280 int ret;
Vikas Manochaec8630a2017-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 Gaignard16f6f332018-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 Manochaec8630a2017-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 Chotard866dfea2022-08-30 14:09:13 +0200308 uc_priv->name[desc->offset] = strdup(ofnode_get_name(node));
309
Benjamin Gaignard16f6f332018-11-27 13:49:53 +0100310 hwspinlock_unlock(&ctrl_priv->hws);
311
Vikas Manochaec8630a2017-04-10 15:02:57 -0700312 return 0;
313}
Patrick Delaunayd252d752018-03-12 10:46:13 +0100314
Vikas Manocha07e9e412017-02-12 10:25:49 -0800315static int prep_gpio_dsc(struct stm32_gpio_dsc *gpio_dsc, u32 port_pin)
316{
Patrick Delaunayd252d752018-03-12 10:46:13 +0100317 gpio_dsc->port = (port_pin & 0x1F000) >> 12;
Vikas Manocha07e9e412017-02-12 10:25:49 -0800318 gpio_dsc->pin = (port_pin & 0x0F00) >> 8;
Patrick Delaunay84a9a4e2020-11-06 19:01:32 +0100319 log_debug("GPIO:port= %d, pin= %d\n", gpio_dsc->port, gpio_dsc->pin);
Vikas Manocha07e9e412017-02-12 10:25:49 -0800320
321 return 0;
322}
323
Patrick Delaunay11ec5972020-09-09 17:50:14 +0200324static int prep_gpio_ctl(struct stm32_gpio_ctl *gpio_ctl, u32 gpio_fn,
325 ofnode node)
Vikas Manocha07e9e412017-02-12 10:25:49 -0800326{
327 gpio_fn &= 0x00FF;
Vikas Manochaec8630a2017-04-10 15:02:57 -0700328 gpio_ctl->af = 0;
Vikas Manocha07e9e412017-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 Delaunay11ec5972020-09-09 17:50:14 +0200346 gpio_ctl->speed = ofnode_read_u32_default(node, "slew-rate", 0);
Vikas Manocha07e9e412017-02-12 10:25:49 -0800347
Patrick Delaunay11ec5972020-09-09 17:50:14 +0200348 if (ofnode_read_bool(node, "drive-open-drain"))
Vikas Manocha07e9e412017-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 Delaunay11ec5972020-09-09 17:50:14 +0200353 if (ofnode_read_bool(node, "bias-pull-up"))
Vikas Manocha07e9e412017-02-12 10:25:49 -0800354 gpio_ctl->pupd = STM32_GPIO_PUPD_UP;
Patrick Delaunay11ec5972020-09-09 17:50:14 +0200355 else if (ofnode_read_bool(node, "bias-pull-down"))
Vikas Manocha07e9e412017-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 Delaunay84a9a4e2020-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 Manocha07e9e412017-02-12 10:25:49 -0800363
364 return 0;
365}
366
Patrick Delaunay11ec5972020-09-09 17:50:14 +0200367static int stm32_pinctrl_config(ofnode node)
Vikas Manocha07e9e412017-02-12 10:25:49 -0800368{
Vikas Manocha40ddb3a2017-04-10 15:03:04 -0700369 u32 pin_mux[MAX_PINS_ONE_IP];
Vikas Manocha07e9e412017-02-12 10:25:49 -0800370 int rv, len;
Patrick Delaunay11ec5972020-09-09 17:50:14 +0200371 ofnode subnode;
Vikas Manocha07e9e412017-02-12 10:25:49 -0800372
Vikas Manocha07e9e412017-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 Delaunay11ec5972020-09-09 17:50:14 +0200377 ofnode_for_each_subnode(subnode, node) {
Vikas Manocha07e9e412017-02-12 10:25:49 -0800378 struct stm32_gpio_dsc gpio_dsc;
379 struct stm32_gpio_ctl gpio_ctl;
380 int i;
381
Patrick Delaunay11ec5972020-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 Delaunay84a9a4e2020-11-06 19:01:32 +0100386 log_debug("No of pinmux entries= %d\n", len);
Patrick Delaunay11ec5972020-09-09 17:50:14 +0200387 if (len > MAX_PINS_ONE_IP)
Vikas Manocha07e9e412017-02-12 10:25:49 -0800388 return -EINVAL;
Patrick Delaunay11ec5972020-09-09 17:50:14 +0200389 rv = ofnode_read_u32_array(subnode, "pinmux", pin_mux, len);
390 if (rv < 0)
391 return rv;
Vikas Manocha07e9e412017-02-12 10:25:49 -0800392 for (i = 0; i < len; i++) {
Vikas Manocha1a8fde72017-04-10 15:02:59 -0700393 struct gpio_desc desc;
Patrick Delaunayd252d752018-03-12 10:46:13 +0100394
Patrick Delaunay84a9a4e2020-11-06 19:01:32 +0100395 log_debug("pinmux = %x\n", *(pin_mux + i));
Vikas Manocha07e9e412017-02-12 10:25:49 -0800396 prep_gpio_dsc(&gpio_dsc, *(pin_mux + i));
Patrick Delaunay11ec5972020-09-09 17:50:14 +0200397 prep_gpio_ctl(&gpio_ctl, *(pin_mux + i), subnode);
Vikas Manocha1a8fde72017-04-10 15:02:59 -0700398 rv = uclass_get_device_by_seq(UCLASS_GPIO,
Patrick Delaunayd252d752018-03-12 10:46:13 +0100399 gpio_dsc.port,
400 &desc.dev);
Vikas Manocha1a8fde72017-04-10 15:02:59 -0700401 if (rv)
402 return rv;
403 desc.offset = gpio_dsc.pin;
Patrice Chotard866dfea2022-08-30 14:09:13 +0200404 rv = stm32_gpio_config(node, &desc, &gpio_ctl);
Patrick Delaunay84a9a4e2020-11-06 19:01:32 +0100405 log_debug("rv = %d\n\n", rv);
Vikas Manocha07e9e412017-02-12 10:25:49 -0800406 if (rv)
407 return rv;
408 }
Christophe Kerelloa466d212017-06-20 17:04:18 +0200409 }
410
411 return 0;
412}
413
Patrice Chotard05a93192019-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 Delaunay84a9a4e2020-11-06 19:01:32 +0100421 dev_dbg(dev, "bind %s\n", ofnode_get_name(node));
Patrice Chotard05a93192019-06-21 15:39:23 +0200422
Patrick Delaunay13fd15c2021-01-21 17:39:08 +0100423 if (!ofnode_is_enabled(node))
424 continue;
425
Patrice Chotard05a93192019-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 Delaunay84a9a4e2020-11-06 19:01:32 +0100440 dev_dbg(dev, "bind %s\n", name);
Patrice Chotard05a93192019-06-21 15:39:23 +0200441 }
442
443 return 0;
444}
445
Christophe Kerellod6661552017-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 Delaunay11ec5972020-09-09 17:50:14 +0200449 return stm32_pinctrl_config(dev_ofnode(config));
Christophe Kerellod6661552017-06-20 17:04:19 +0200450}
451#else /* PINCTRL_FULL */
Christophe Kerelloa466d212017-06-20 17:04:18 +0200452static int stm32_pinctrl_set_state_simple(struct udevice *dev,
453 struct udevice *periph)
454{
Christophe Kerelloa466d212017-06-20 17:04:18 +0200455 const fdt32_t *list;
456 uint32_t phandle;
Patrick Delaunay11ec5972020-09-09 17:50:14 +0200457 ofnode config_node;
Christophe Kerelloa466d212017-06-20 17:04:18 +0200458 int size, i, ret;
459
Patrick Delaunay11ec5972020-09-09 17:50:14 +0200460 list = ofnode_get_property(dev_ofnode(periph), "pinctrl-0", &size);
Christophe Kerelloa466d212017-06-20 17:04:18 +0200461 if (!list)
462 return -EINVAL;
463
Patrick Delaunay84a9a4e2020-11-06 19:01:32 +0100464 dev_dbg(dev, "periph->name = %s\n", periph->name);
Christophe Kerelloa466d212017-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 Delaunay11ec5972020-09-09 17:50:14 +0200470 config_node = ofnode_get_by_phandle(phandle);
471 if (!ofnode_valid(config_node)) {
Patrick Delaunay84a9a4e2020-11-06 19:01:32 +0100472 dev_err(periph,
473 "prop pinctrl-0 index %d invalid phandle\n", i);
Christophe Kerelloa466d212017-06-20 17:04:18 +0200474 return -EINVAL;
475 }
476
477 ret = stm32_pinctrl_config(config_node);
478 if (ret)
479 return ret;
Vikas Manocha07e9e412017-02-12 10:25:49 -0800480 }
481
482 return 0;
483}
Christophe Kerellod6661552017-06-20 17:04:19 +0200484#endif /* PINCTRL_FULL */
Vikas Manocha07e9e412017-02-12 10:25:49 -0800485
486static struct pinctrl_ops stm32_pinctrl_ops = {
Christophe Kerellod6661552017-06-20 17:04:19 +0200487#if CONFIG_IS_ENABLED(PINCTRL_FULL)
488 .set_state = stm32_pinctrl_set_state,
489#else /* PINCTRL_FULL */
Vikas Manocha07e9e412017-02-12 10:25:49 -0800490 .set_state_simple = stm32_pinctrl_set_state_simple,
Christophe Kerellod6661552017-06-20 17:04:19 +0200491#endif /* PINCTRL_FULL */
Patrice Chotardaaf68e82018-10-24 14:10:18 +0200492#ifndef CONFIG_SPL_BUILD
Patrice Chotard881e8672018-10-24 14:10:19 +0200493 .get_pin_name = stm32_pinctrl_get_pin_name,
Patrice Chotardaaf68e82018-10-24 14:10:18 +0200494 .get_pins_count = stm32_pinctrl_get_pins_count,
Patrice Chotarda46fb392018-10-24 14:10:20 +0200495 .get_pin_muxing = stm32_pinctrl_get_pin_muxing,
Patrice Chotardaaf68e82018-10-24 14:10:18 +0200496#endif
Vikas Manocha07e9e412017-02-12 10:25:49 -0800497};
498
499static const struct udevice_id stm32_pinctrl_ids[] = {
Patrice Chotardb5652b72017-12-12 09:49:35 +0100500 { .compatible = "st,stm32f429-pinctrl" },
501 { .compatible = "st,stm32f469-pinctrl" },
Vikas Manocha07e9e412017-02-12 10:25:49 -0800502 { .compatible = "st,stm32f746-pinctrl" },
Patrice Chotard636768f2018-12-11 14:49:18 +0100503 { .compatible = "st,stm32f769-pinctrl" },
Patrice Chotard6502c472017-09-13 18:00:04 +0200504 { .compatible = "st,stm32h743-pinctrl" },
Patrick Delaunayd252d752018-03-12 10:46:13 +0100505 { .compatible = "st,stm32mp157-pinctrl" },
506 { .compatible = "st,stm32mp157-z-pinctrl" },
Patrick Delaunayfd65f0a2022-05-20 18:24:48 +0200507 { .compatible = "st,stm32mp135-pinctrl" },
Patrice Chotard692f8722023-10-27 16:43:02 +0200508 { .compatible = "st,stm32mp257-pinctrl" },
509 { .compatible = "st,stm32mp257-z-pinctrl" },
Vikas Manocha07e9e412017-02-12 10:25:49 -0800510 { }
511};
512
513U_BOOT_DRIVER(pinctrl_stm32) = {
Patrice Chotardaaf68e82018-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 Chotard05a93192019-06-21 15:39:23 +0200518 .bind = stm32_pinctrl_bind,
Patrice Chotardaaf68e82018-10-24 14:10:18 +0200519 .probe = stm32_pinctrl_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700520 .priv_auto = sizeof(struct stm32_pinctrl_priv),
Vikas Manocha07e9e412017-02-12 10:25:49 -0800521};