blob: 591cd1a0ecce6253515b5dc10cb998ca90f4dd76 [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
Vikas Manocha07e9e412017-02-12 10:25:49 -08006#include <common.h>
Vikas Manocha07e9e412017-02-12 10:25:49 -08007#include <dm.h>
Benjamin Gaignard16f6f332018-11-27 13:49:53 +01008#include <hwspinlock.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070010#include <malloc.h>
Vikas Manochaec8630a2017-04-10 15:02:57 -070011#include <asm/arch/gpio.h>
12#include <asm/gpio.h>
13#include <asm/io.h>
Simon Glass9bc15642020-02-03 07:36:16 -070014#include <dm/device_compat.h>
Patrice Chotarde16e8f42019-07-30 19:16:10 +020015#include <dm/lists.h>
16#include <dm/pinctrl.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060017#include <linux/bitops.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070018#include <linux/err.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060019#include <linux/libfdt.h>
Vikas Manocha07e9e412017-02-12 10:25:49 -080020
Vikas Manocha40ddb3a2017-04-10 15:03:04 -070021#define MAX_PINS_ONE_IP 70
Vikas Manochaec8630a2017-04-10 15:02:57 -070022#define MODE_BITS_MASK 3
23#define OSPEED_MASK 3
24#define PUPD_MASK 3
25#define OTYPE_MSK 1
26#define AFR_MASK 0xF
27
Patrice Chotardaaf68e82018-10-24 14:10:18 +020028struct stm32_pinctrl_priv {
Benjamin Gaignard16f6f332018-11-27 13:49:53 +010029 struct hwspinlock hws;
Patrice Chotardaaf68e82018-10-24 14:10:18 +020030 int pinctrl_ngpios;
31 struct list_head gpio_dev;
32};
33
34struct stm32_gpio_bank {
35 struct udevice *gpio_dev;
36 struct list_head list;
37};
38
Benjamin Gaignard16f6f332018-11-27 13:49:53 +010039#ifndef CONFIG_SPL_BUILD
40
Patrice Chotard881e8672018-10-24 14:10:19 +020041static char pin_name[PINNAME_SIZE];
Patrice Chotarda46fb392018-10-24 14:10:20 +020042#define PINMUX_MODE_COUNT 5
43static const char * const pinmux_mode[PINMUX_MODE_COUNT] = {
44 "gpio input",
45 "gpio output",
46 "analog",
47 "unknown",
48 "alt function",
49};
50
Patrick Delaunay6347ed92020-10-28 10:49:07 +010051static const char * const pinmux_bias[] = {
52 [STM32_GPIO_PUPD_NO] = "",
53 [STM32_GPIO_PUPD_UP] = "pull-up",
54 [STM32_GPIO_PUPD_DOWN] = "pull-down",
Patrick Delaunay8274fab2020-06-04 14:30:33 +020055};
56
57static const char * const pinmux_input[] = {
Patrick Delaunay6347ed92020-10-28 10:49:07 +010058 [STM32_GPIO_OTYPE_PP] = "push-pull",
59 [STM32_GPIO_OTYPE_OD] = "open-drain",
Patrick Delaunay8274fab2020-06-04 14:30:33 +020060};
61
Patrice Chotarda46fb392018-10-24 14:10:20 +020062static int stm32_pinctrl_get_af(struct udevice *dev, unsigned int offset)
63{
64 struct stm32_gpio_priv *priv = dev_get_priv(dev);
65 struct stm32_gpio_regs *regs = priv->regs;
66 u32 af;
67 u32 alt_shift = (offset % 8) * 4;
68 u32 alt_index = offset / 8;
69
70 af = (readl(&regs->afr[alt_index]) &
71 GENMASK(alt_shift + 3, alt_shift)) >> alt_shift;
72
73 return af;
74}
75
Patrice Chotard7ef91082018-12-03 10:52:50 +010076static int stm32_populate_gpio_dev_list(struct udevice *dev)
77{
78 struct stm32_pinctrl_priv *priv = dev_get_priv(dev);
79 struct udevice *gpio_dev;
80 struct udevice *child;
81 struct stm32_gpio_bank *gpio_bank;
82 int ret;
83
84 /*
85 * parse pin-controller sub-nodes (ie gpio bank nodes) and fill
86 * a list with all gpio device reference which belongs to the
87 * current pin-controller. This list is used to find pin_name and
88 * pin muxing
89 */
90 list_for_each_entry(child, &dev->child_head, sibling_node) {
91 ret = uclass_get_device_by_name(UCLASS_GPIO, child->name,
92 &gpio_dev);
93 if (ret < 0)
94 continue;
95
96 gpio_bank = malloc(sizeof(*gpio_bank));
97 if (!gpio_bank) {
98 dev_err(dev, "Not enough memory\n");
99 return -ENOMEM;
100 }
101
102 gpio_bank->gpio_dev = gpio_dev;
103 list_add_tail(&gpio_bank->list, &priv->gpio_dev);
104 }
105
106 return 0;
107}
108
Patrice Chotardaaf68e82018-10-24 14:10:18 +0200109static int stm32_pinctrl_get_pins_count(struct udevice *dev)
110{
111 struct stm32_pinctrl_priv *priv = dev_get_priv(dev);
112 struct gpio_dev_priv *uc_priv;
113 struct stm32_gpio_bank *gpio_bank;
114
115 /*
116 * if get_pins_count has already been executed once on this
117 * pin-controller, no need to run it again
118 */
119 if (priv->pinctrl_ngpios)
120 return priv->pinctrl_ngpios;
121
Patrice Chotard7ef91082018-12-03 10:52:50 +0100122 if (list_empty(&priv->gpio_dev))
123 stm32_populate_gpio_dev_list(dev);
Patrice Chotardaaf68e82018-10-24 14:10:18 +0200124 /*
125 * walk through all banks to retrieve the pin-controller
126 * pins number
127 */
128 list_for_each_entry(gpio_bank, &priv->gpio_dev, list) {
129 uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev);
130
131 priv->pinctrl_ngpios += uc_priv->gpio_count;
132 }
133
134 return priv->pinctrl_ngpios;
135}
136
Patrice Chotard881e8672018-10-24 14:10:19 +0200137static struct udevice *stm32_pinctrl_get_gpio_dev(struct udevice *dev,
Patrice Chotard0b968002018-12-03 10:52:54 +0100138 unsigned int selector,
139 unsigned int *idx)
Patrice Chotard881e8672018-10-24 14:10:19 +0200140{
141 struct stm32_pinctrl_priv *priv = dev_get_priv(dev);
142 struct stm32_gpio_bank *gpio_bank;
143 struct gpio_dev_priv *uc_priv;
Patrice Chotard0b968002018-12-03 10:52:54 +0100144 int pin_count = 0;
Patrice Chotard881e8672018-10-24 14:10:19 +0200145
Patrice Chotard7ef91082018-12-03 10:52:50 +0100146 if (list_empty(&priv->gpio_dev))
147 stm32_populate_gpio_dev_list(dev);
148
Patrice Chotard881e8672018-10-24 14:10:19 +0200149 /* look up for the bank which owns the requested pin */
150 list_for_each_entry(gpio_bank, &priv->gpio_dev, list) {
151 uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev);
152
Patrice Chotard0b968002018-12-03 10:52:54 +0100153 if (selector < (pin_count + uc_priv->gpio_count)) {
154 /*
155 * we found the bank, convert pin selector to
156 * gpio bank index
157 */
158 *idx = stm32_offset_to_index(gpio_bank->gpio_dev,
159 selector - pin_count);
Patrick Delaunay4c11a112019-06-21 15:26:52 +0200160 if (IS_ERR_VALUE(*idx))
Patrice Chotard0b968002018-12-03 10:52:54 +0100161 return NULL;
Patrice Chotard881e8672018-10-24 14:10:19 +0200162
Patrice Chotard0b968002018-12-03 10:52:54 +0100163 return gpio_bank->gpio_dev;
164 }
165 pin_count += uc_priv->gpio_count;
Patrice Chotard881e8672018-10-24 14:10:19 +0200166 }
167
168 return NULL;
169}
170
171static const char *stm32_pinctrl_get_pin_name(struct udevice *dev,
172 unsigned int selector)
173{
174 struct gpio_dev_priv *uc_priv;
175 struct udevice *gpio_dev;
Patrice Chotard0b968002018-12-03 10:52:54 +0100176 unsigned int gpio_idx;
Patrice Chotard881e8672018-10-24 14:10:19 +0200177
178 /* look up for the bank which owns the requested pin */
Patrice Chotard0b968002018-12-03 10:52:54 +0100179 gpio_dev = stm32_pinctrl_get_gpio_dev(dev, selector, &gpio_idx);
Patrice Chotard881e8672018-10-24 14:10:19 +0200180 if (!gpio_dev) {
181 snprintf(pin_name, PINNAME_SIZE, "Error");
182 } else {
183 uc_priv = dev_get_uclass_priv(gpio_dev);
184
185 snprintf(pin_name, PINNAME_SIZE, "%s%d",
186 uc_priv->bank_name,
Patrice Chotard0b968002018-12-03 10:52:54 +0100187 gpio_idx);
Patrice Chotard881e8672018-10-24 14:10:19 +0200188 }
189
190 return pin_name;
191}
Patrice Chotarda46fb392018-10-24 14:10:20 +0200192
193static int stm32_pinctrl_get_pin_muxing(struct udevice *dev,
194 unsigned int selector,
195 char *buf,
196 int size)
197{
198 struct udevice *gpio_dev;
Patrick Delaunay8274fab2020-06-04 14:30:33 +0200199 struct stm32_gpio_priv *priv;
Patrice Chotarda46fb392018-10-24 14:10:20 +0200200 const char *label;
Patrice Chotarda46fb392018-10-24 14:10:20 +0200201 int mode;
202 int af_num;
Patrice Chotard0b968002018-12-03 10:52:54 +0100203 unsigned int gpio_idx;
Patrick Delaunay8274fab2020-06-04 14:30:33 +0200204 u32 pupd, otype;
Patrice Chotarda46fb392018-10-24 14:10:20 +0200205
206 /* look up for the bank which owns the requested pin */
Patrice Chotard0b968002018-12-03 10:52:54 +0100207 gpio_dev = stm32_pinctrl_get_gpio_dev(dev, selector, &gpio_idx);
Patrice Chotarda46fb392018-10-24 14:10:20 +0200208
209 if (!gpio_dev)
210 return -ENODEV;
211
Patrice Chotard0b968002018-12-03 10:52:54 +0100212 mode = gpio_get_raw_function(gpio_dev, gpio_idx, &label);
Patrice Chotard0b968002018-12-03 10:52:54 +0100213 dev_dbg(dev, "selector = %d gpio_idx = %d mode = %d\n",
214 selector, gpio_idx, mode);
Patrick Delaunay8274fab2020-06-04 14:30:33 +0200215 priv = dev_get_priv(gpio_dev);
Patrick Delaunay6347ed92020-10-28 10:49:07 +0100216 pupd = (readl(&priv->regs->pupdr) >> (gpio_idx * 2)) & PUPD_MASK;
Patrice Chotarda46fb392018-10-24 14:10:20 +0200217
Patrice Chotarda46fb392018-10-24 14:10:20 +0200218
219 switch (mode) {
220 case GPIOF_UNKNOWN:
221 /* should never happen */
222 return -EINVAL;
223 case GPIOF_UNUSED:
224 snprintf(buf, size, "%s", pinmux_mode[mode]);
225 break;
226 case GPIOF_FUNC:
Patrice Chotard0b968002018-12-03 10:52:54 +0100227 af_num = stm32_pinctrl_get_af(gpio_dev, gpio_idx);
Patrick Delaunay6347ed92020-10-28 10:49:07 +0100228 snprintf(buf, size, "%s %d %s", pinmux_mode[mode], af_num,
229 pinmux_bias[pupd]);
Patrice Chotarda46fb392018-10-24 14:10:20 +0200230 break;
231 case GPIOF_OUTPUT:
Patrick Delaunay8274fab2020-06-04 14:30:33 +0200232 snprintf(buf, size, "%s %s %s",
Patrick Delaunay6347ed92020-10-28 10:49:07 +0100233 pinmux_mode[mode], pinmux_bias[pupd],
Patrick Delaunay8274fab2020-06-04 14:30:33 +0200234 label ? label : "");
235 break;
Patrice Chotarda46fb392018-10-24 14:10:20 +0200236 case GPIOF_INPUT:
Patrick Delaunay8274fab2020-06-04 14:30:33 +0200237 otype = (readl(&priv->regs->otyper) >> gpio_idx) & OTYPE_MSK;
Patrick Delaunay6347ed92020-10-28 10:49:07 +0100238 snprintf(buf, size, "%s %s %s %s",
Patrick Delaunay8274fab2020-06-04 14:30:33 +0200239 pinmux_mode[mode], pinmux_input[otype],
Patrick Delaunay6347ed92020-10-28 10:49:07 +0100240 pinmux_bias[pupd], label ? label : "");
Patrice Chotarda46fb392018-10-24 14:10:20 +0200241 break;
242 }
243
244 return 0;
245}
246
Benjamin Gaignard16f6f332018-11-27 13:49:53 +0100247#endif
248
Patrick Delaunay4c11a112019-06-21 15:26:52 +0200249static int stm32_pinctrl_probe(struct udevice *dev)
Patrice Chotardaaf68e82018-10-24 14:10:18 +0200250{
251 struct stm32_pinctrl_priv *priv = dev_get_priv(dev);
Patrice Chotardaaf68e82018-10-24 14:10:18 +0200252 int ret;
253
254 INIT_LIST_HEAD(&priv->gpio_dev);
255
Benjamin Gaignard16f6f332018-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)
259 debug("%s: hwspinlock_get_by_index may have failed (%d)\n",
260 __func__, ret);
261
Patrice Chotardaaf68e82018-10-24 14:10:18 +0200262 return 0;
263}
Patrice Chotardaaf68e82018-10-24 14:10:18 +0200264
Vikas Manochaec8630a2017-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 Gaignard16f6f332018-11-27 13:49:53 +0100270 struct stm32_pinctrl_priv *ctrl_priv;
271 int ret;
Vikas Manochaec8630a2017-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 Gaignard16f6f332018-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 Manochaec8630a2017-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 Gaignard16f6f332018-11-27 13:49:53 +0100299 hwspinlock_unlock(&ctrl_priv->hws);
300
Vikas Manochaec8630a2017-04-10 15:02:57 -0700301 return 0;
302}
Patrick Delaunayd252d752018-03-12 10:46:13 +0100303
Vikas Manocha07e9e412017-02-12 10:25:49 -0800304static int prep_gpio_dsc(struct stm32_gpio_dsc *gpio_dsc, u32 port_pin)
305{
Patrick Delaunayd252d752018-03-12 10:46:13 +0100306 gpio_dsc->port = (port_pin & 0x1F000) >> 12;
Vikas Manocha07e9e412017-02-12 10:25:49 -0800307 gpio_dsc->pin = (port_pin & 0x0F00) >> 8;
308 debug("%s: GPIO:port= %d, pin= %d\n", __func__, gpio_dsc->port,
309 gpio_dsc->pin);
310
311 return 0;
312}
313
Patrick Delaunay11ec5972020-09-09 17:50:14 +0200314static int prep_gpio_ctl(struct stm32_gpio_ctl *gpio_ctl, u32 gpio_fn,
315 ofnode node)
Vikas Manocha07e9e412017-02-12 10:25:49 -0800316{
317 gpio_fn &= 0x00FF;
Vikas Manochaec8630a2017-04-10 15:02:57 -0700318 gpio_ctl->af = 0;
Vikas Manocha07e9e412017-02-12 10:25:49 -0800319
320 switch (gpio_fn) {
321 case 0:
322 gpio_ctl->mode = STM32_GPIO_MODE_IN;
323 break;
324 case 1 ... 16:
325 gpio_ctl->mode = STM32_GPIO_MODE_AF;
326 gpio_ctl->af = gpio_fn - 1;
327 break;
328 case 17:
329 gpio_ctl->mode = STM32_GPIO_MODE_AN;
330 break;
331 default:
332 gpio_ctl->mode = STM32_GPIO_MODE_OUT;
333 break;
334 }
335
Patrick Delaunay11ec5972020-09-09 17:50:14 +0200336 gpio_ctl->speed = ofnode_read_u32_default(node, "slew-rate", 0);
Vikas Manocha07e9e412017-02-12 10:25:49 -0800337
Patrick Delaunay11ec5972020-09-09 17:50:14 +0200338 if (ofnode_read_bool(node, "drive-open-drain"))
Vikas Manocha07e9e412017-02-12 10:25:49 -0800339 gpio_ctl->otype = STM32_GPIO_OTYPE_OD;
340 else
341 gpio_ctl->otype = STM32_GPIO_OTYPE_PP;
342
Patrick Delaunay11ec5972020-09-09 17:50:14 +0200343 if (ofnode_read_bool(node, "bias-pull-up"))
Vikas Manocha07e9e412017-02-12 10:25:49 -0800344 gpio_ctl->pupd = STM32_GPIO_PUPD_UP;
Patrick Delaunay11ec5972020-09-09 17:50:14 +0200345 else if (ofnode_read_bool(node, "bias-pull-down"))
Vikas Manocha07e9e412017-02-12 10:25:49 -0800346 gpio_ctl->pupd = STM32_GPIO_PUPD_DOWN;
347 else
348 gpio_ctl->pupd = STM32_GPIO_PUPD_NO;
349
350 debug("%s: gpio fn= %d, slew-rate= %x, op type= %x, pull-upd is = %x\n",
351 __func__, gpio_fn, gpio_ctl->speed, gpio_ctl->otype,
352 gpio_ctl->pupd);
353
354 return 0;
355}
356
Patrick Delaunay11ec5972020-09-09 17:50:14 +0200357static int stm32_pinctrl_config(ofnode node)
Vikas Manocha07e9e412017-02-12 10:25:49 -0800358{
Vikas Manocha40ddb3a2017-04-10 15:03:04 -0700359 u32 pin_mux[MAX_PINS_ONE_IP];
Vikas Manocha07e9e412017-02-12 10:25:49 -0800360 int rv, len;
Patrick Delaunay11ec5972020-09-09 17:50:14 +0200361 ofnode subnode;
Vikas Manocha07e9e412017-02-12 10:25:49 -0800362
Vikas Manocha07e9e412017-02-12 10:25:49 -0800363 /*
364 * check for "pinmux" property in each subnode (e.g. pins1 and pins2 for
365 * usart1) of pin controller phandle "pinctrl-0"
366 * */
Patrick Delaunay11ec5972020-09-09 17:50:14 +0200367 ofnode_for_each_subnode(subnode, node) {
Vikas Manocha07e9e412017-02-12 10:25:49 -0800368 struct stm32_gpio_dsc gpio_dsc;
369 struct stm32_gpio_ctl gpio_ctl;
370 int i;
371
Patrick Delaunay11ec5972020-09-09 17:50:14 +0200372 rv = ofnode_read_size(subnode, "pinmux");
373 if (rv < 0)
374 return rv;
375 len = rv / sizeof(pin_mux[0]);
Christophe Kerelloa466d212017-06-20 17:04:18 +0200376 debug("%s: no of pinmux entries= %d\n", __func__, len);
Patrick Delaunay11ec5972020-09-09 17:50:14 +0200377 if (len > MAX_PINS_ONE_IP)
Vikas Manocha07e9e412017-02-12 10:25:49 -0800378 return -EINVAL;
Patrick Delaunay11ec5972020-09-09 17:50:14 +0200379 rv = ofnode_read_u32_array(subnode, "pinmux", pin_mux, len);
380 if (rv < 0)
381 return rv;
Vikas Manocha07e9e412017-02-12 10:25:49 -0800382 for (i = 0; i < len; i++) {
Vikas Manocha1a8fde72017-04-10 15:02:59 -0700383 struct gpio_desc desc;
Patrick Delaunayd252d752018-03-12 10:46:13 +0100384
Vikas Manocha07e9e412017-02-12 10:25:49 -0800385 debug("%s: pinmux = %x\n", __func__, *(pin_mux + i));
386 prep_gpio_dsc(&gpio_dsc, *(pin_mux + i));
Patrick Delaunay11ec5972020-09-09 17:50:14 +0200387 prep_gpio_ctl(&gpio_ctl, *(pin_mux + i), subnode);
Vikas Manocha1a8fde72017-04-10 15:02:59 -0700388 rv = uclass_get_device_by_seq(UCLASS_GPIO,
Patrick Delaunayd252d752018-03-12 10:46:13 +0100389 gpio_dsc.port,
390 &desc.dev);
Vikas Manocha1a8fde72017-04-10 15:02:59 -0700391 if (rv)
392 return rv;
393 desc.offset = gpio_dsc.pin;
394 rv = stm32_gpio_config(&desc, &gpio_ctl);
Vikas Manocha07e9e412017-02-12 10:25:49 -0800395 debug("%s: rv = %d\n\n", __func__, rv);
396 if (rv)
397 return rv;
398 }
Christophe Kerelloa466d212017-06-20 17:04:18 +0200399 }
400
401 return 0;
402}
403
Patrice Chotard05a93192019-06-21 15:39:23 +0200404static int stm32_pinctrl_bind(struct udevice *dev)
405{
406 ofnode node;
407 const char *name;
408 int ret;
409
410 dev_for_each_subnode(node, dev) {
411 debug("%s: bind %s\n", __func__, ofnode_get_name(node));
412
413 ofnode_get_property(node, "gpio-controller", &ret);
414 if (ret < 0)
415 continue;
416 /* Get the name of each gpio node */
417 name = ofnode_get_name(node);
418 if (!name)
419 return -EINVAL;
420
421 /* Bind each gpio node */
422 ret = device_bind_driver_to_node(dev, "gpio_stm32",
423 name, node, NULL);
424 if (ret)
425 return ret;
426
427 debug("%s: bind %s\n", __func__, name);
428 }
429
430 return 0;
431}
432
Christophe Kerellod6661552017-06-20 17:04:19 +0200433#if CONFIG_IS_ENABLED(PINCTRL_FULL)
434static int stm32_pinctrl_set_state(struct udevice *dev, struct udevice *config)
435{
Patrick Delaunay11ec5972020-09-09 17:50:14 +0200436 return stm32_pinctrl_config(dev_ofnode(config));
Christophe Kerellod6661552017-06-20 17:04:19 +0200437}
438#else /* PINCTRL_FULL */
Christophe Kerelloa466d212017-06-20 17:04:18 +0200439static int stm32_pinctrl_set_state_simple(struct udevice *dev,
440 struct udevice *periph)
441{
Christophe Kerelloa466d212017-06-20 17:04:18 +0200442 const fdt32_t *list;
443 uint32_t phandle;
Patrick Delaunay11ec5972020-09-09 17:50:14 +0200444 ofnode config_node;
Christophe Kerelloa466d212017-06-20 17:04:18 +0200445 int size, i, ret;
446
Patrick Delaunay11ec5972020-09-09 17:50:14 +0200447 list = ofnode_get_property(dev_ofnode(periph), "pinctrl-0", &size);
Christophe Kerelloa466d212017-06-20 17:04:18 +0200448 if (!list)
449 return -EINVAL;
450
451 debug("%s: periph->name = %s\n", __func__, periph->name);
452
453 size /= sizeof(*list);
454 for (i = 0; i < size; i++) {
455 phandle = fdt32_to_cpu(*list++);
456
Patrick Delaunay11ec5972020-09-09 17:50:14 +0200457 config_node = ofnode_get_by_phandle(phandle);
458 if (!ofnode_valid(config_node)) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900459 pr_err("prop pinctrl-0 index %d invalid phandle\n", i);
Christophe Kerelloa466d212017-06-20 17:04:18 +0200460 return -EINVAL;
461 }
462
463 ret = stm32_pinctrl_config(config_node);
464 if (ret)
465 return ret;
Vikas Manocha07e9e412017-02-12 10:25:49 -0800466 }
467
468 return 0;
469}
Christophe Kerellod6661552017-06-20 17:04:19 +0200470#endif /* PINCTRL_FULL */
Vikas Manocha07e9e412017-02-12 10:25:49 -0800471
472static struct pinctrl_ops stm32_pinctrl_ops = {
Christophe Kerellod6661552017-06-20 17:04:19 +0200473#if CONFIG_IS_ENABLED(PINCTRL_FULL)
474 .set_state = stm32_pinctrl_set_state,
475#else /* PINCTRL_FULL */
Vikas Manocha07e9e412017-02-12 10:25:49 -0800476 .set_state_simple = stm32_pinctrl_set_state_simple,
Christophe Kerellod6661552017-06-20 17:04:19 +0200477#endif /* PINCTRL_FULL */
Patrice Chotardaaf68e82018-10-24 14:10:18 +0200478#ifndef CONFIG_SPL_BUILD
Patrice Chotard881e8672018-10-24 14:10:19 +0200479 .get_pin_name = stm32_pinctrl_get_pin_name,
Patrice Chotardaaf68e82018-10-24 14:10:18 +0200480 .get_pins_count = stm32_pinctrl_get_pins_count,
Patrice Chotarda46fb392018-10-24 14:10:20 +0200481 .get_pin_muxing = stm32_pinctrl_get_pin_muxing,
Patrice Chotardaaf68e82018-10-24 14:10:18 +0200482#endif
Vikas Manocha07e9e412017-02-12 10:25:49 -0800483};
484
485static const struct udevice_id stm32_pinctrl_ids[] = {
Patrice Chotardb5652b72017-12-12 09:49:35 +0100486 { .compatible = "st,stm32f429-pinctrl" },
487 { .compatible = "st,stm32f469-pinctrl" },
Vikas Manocha07e9e412017-02-12 10:25:49 -0800488 { .compatible = "st,stm32f746-pinctrl" },
Patrice Chotard636768f2018-12-11 14:49:18 +0100489 { .compatible = "st,stm32f769-pinctrl" },
Patrice Chotard6502c472017-09-13 18:00:04 +0200490 { .compatible = "st,stm32h743-pinctrl" },
Patrick Delaunayd252d752018-03-12 10:46:13 +0100491 { .compatible = "st,stm32mp157-pinctrl" },
492 { .compatible = "st,stm32mp157-z-pinctrl" },
Vikas Manocha07e9e412017-02-12 10:25:49 -0800493 { }
494};
495
496U_BOOT_DRIVER(pinctrl_stm32) = {
Patrice Chotardaaf68e82018-10-24 14:10:18 +0200497 .name = "pinctrl_stm32",
498 .id = UCLASS_PINCTRL,
499 .of_match = stm32_pinctrl_ids,
500 .ops = &stm32_pinctrl_ops,
Patrice Chotard05a93192019-06-21 15:39:23 +0200501 .bind = stm32_pinctrl_bind,
Patrice Chotardaaf68e82018-10-24 14:10:18 +0200502 .probe = stm32_pinctrl_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700503 .priv_auto = sizeof(struct stm32_pinctrl_priv),
Vikas Manocha07e9e412017-02-12 10:25:49 -0800504};