blob: d25d4a064badb26e0af793179430f35beb9c2678 [file] [log] [blame]
Maxime Coquelinaceb16d2016-01-14 13:16:30 +01001/*
2 * Copyright (C) Maxime Coquelin 2015
3 * Author: Maxime Coquelin <mcoquelin.stm32@gmail.com>
4 * License terms: GNU General Public License (GPL), version 2
5 *
6 * Heavily based on Mediatek's pinctrl driver
7 */
8#include <linux/clk.h>
9#include <linux/gpio.h>
10#include <linux/io.h>
11#include <linux/module.h>
12#include <linux/of.h>
13#include <linux/of_address.h>
14#include <linux/of_device.h>
15#include <linux/of_irq.h>
16#include <linux/pinctrl/consumer.h>
17#include <linux/pinctrl/machine.h>
18#include <linux/pinctrl/pinconf.h>
19#include <linux/pinctrl/pinconf-generic.h>
20#include <linux/pinctrl/pinctrl.h>
21#include <linux/pinctrl/pinmux.h>
22#include <linux/platform_device.h>
23#include <linux/reset.h>
24#include <linux/slab.h>
25
26#include <dt-bindings/pinctrl/pinctrl-stm32.h>
27
28#include "../core.h"
29#include "../pinconf.h"
30#include "../pinctrl-utils.h"
31#include "pinctrl-stm32.h"
32
33#define STM32_GPIO_MODER 0x00
34#define STM32_GPIO_TYPER 0x04
35#define STM32_GPIO_SPEEDR 0x08
36#define STM32_GPIO_PUPDR 0x0c
37#define STM32_GPIO_IDR 0x10
38#define STM32_GPIO_ODR 0x14
39#define STM32_GPIO_BSRR 0x18
40#define STM32_GPIO_LCKR 0x1c
41#define STM32_GPIO_AFRL 0x20
42#define STM32_GPIO_AFRH 0x24
43
44#define STM32_GPIO_PINS_PER_BANK 16
45
46#define gpio_range_to_bank(chip) \
47 container_of(chip, struct stm32_gpio_bank, range)
48
49#define gpio_chip_to_bank(chip) \
50 container_of(chip, struct stm32_gpio_bank, gpio_chip)
51
52static const char * const stm32_gpio_functions[] = {
53 "gpio", "af0", "af1",
54 "af2", "af3", "af4",
55 "af5", "af6", "af7",
56 "af8", "af9", "af10",
57 "af11", "af12", "af13",
58 "af14", "af15", "analog",
59};
60
61struct stm32_pinctrl_group {
62 const char *name;
63 unsigned long config;
64 unsigned pin;
65};
66
67struct stm32_gpio_bank {
68 void __iomem *base;
69 struct clk *clk;
70 spinlock_t lock;
71 struct gpio_chip gpio_chip;
72 struct pinctrl_gpio_range range;
73};
74
75struct stm32_pinctrl {
76 struct device *dev;
77 struct pinctrl_dev *pctl_dev;
78 struct pinctrl_desc pctl_desc;
79 struct stm32_pinctrl_group *groups;
80 unsigned ngroups;
81 const char **grp_names;
82 struct stm32_gpio_bank *banks;
83 unsigned nbanks;
84 const struct stm32_pinctrl_match_data *match_data;
85};
86
87static inline int stm32_gpio_pin(int gpio)
88{
89 return gpio % STM32_GPIO_PINS_PER_BANK;
90}
91
92static inline u32 stm32_gpio_get_mode(u32 function)
93{
94 switch (function) {
95 case STM32_PIN_GPIO:
96 return 0;
97 case STM32_PIN_AF(0) ... STM32_PIN_AF(15):
98 return 2;
99 case STM32_PIN_ANALOG:
100 return 3;
101 }
102
103 return 0;
104}
105
106static inline u32 stm32_gpio_get_alt(u32 function)
107{
108 switch (function) {
109 case STM32_PIN_GPIO:
110 return 0;
111 case STM32_PIN_AF(0) ... STM32_PIN_AF(15):
112 return function - 1;
113 case STM32_PIN_ANALOG:
114 return 0;
115 }
116
117 return 0;
118}
119
120/* GPIO functions */
121
122static inline void __stm32_gpio_set(struct stm32_gpio_bank *bank,
123 unsigned offset, int value)
124{
125 if (!value)
126 offset += STM32_GPIO_PINS_PER_BANK;
127
128 clk_enable(bank->clk);
129
130 writel_relaxed(BIT(offset), bank->base + STM32_GPIO_BSRR);
131
132 clk_disable(bank->clk);
133}
134
135static int stm32_gpio_request(struct gpio_chip *chip, unsigned offset)
136{
137 return pinctrl_request_gpio(chip->base + offset);
138}
139
140static void stm32_gpio_free(struct gpio_chip *chip, unsigned offset)
141{
142 pinctrl_free_gpio(chip->base + offset);
143}
144
145static int stm32_gpio_get(struct gpio_chip *chip, unsigned offset)
146{
147 struct stm32_gpio_bank *bank = gpio_chip_to_bank(chip);
148 int ret;
149
150 clk_enable(bank->clk);
151
152 ret = !!(readl_relaxed(bank->base + STM32_GPIO_IDR) & BIT(offset));
153
154 clk_disable(bank->clk);
155
156 return ret;
157}
158
159static void stm32_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
160{
161 struct stm32_gpio_bank *bank = gpio_chip_to_bank(chip);
162
163 __stm32_gpio_set(bank, offset, value);
164}
165
166static int stm32_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
167{
168 return pinctrl_gpio_direction_input(chip->base + offset);
169}
170
171static int stm32_gpio_direction_output(struct gpio_chip *chip,
172 unsigned offset, int value)
173{
174 struct stm32_gpio_bank *bank = gpio_chip_to_bank(chip);
175
176 __stm32_gpio_set(bank, offset, value);
177 pinctrl_gpio_direction_output(chip->base + offset);
178
179 return 0;
180}
181
182static struct gpio_chip stm32_gpio_template = {
183 .request = stm32_gpio_request,
184 .free = stm32_gpio_free,
185 .get = stm32_gpio_get,
186 .set = stm32_gpio_set,
187 .direction_input = stm32_gpio_direction_input,
188 .direction_output = stm32_gpio_direction_output,
189};
190
191/* Pinctrl functions */
192
193static struct stm32_pinctrl_group *
194stm32_pctrl_find_group_by_pin(struct stm32_pinctrl *pctl, u32 pin)
195{
196 int i;
197
198 for (i = 0; i < pctl->ngroups; i++) {
199 struct stm32_pinctrl_group *grp = pctl->groups + i;
200
201 if (grp->pin == pin)
202 return grp;
203 }
204
205 return NULL;
206}
207
208static bool stm32_pctrl_is_function_valid(struct stm32_pinctrl *pctl,
209 u32 pin_num, u32 fnum)
210{
211 int i;
212
213 for (i = 0; i < pctl->match_data->npins; i++) {
214 const struct stm32_desc_pin *pin = pctl->match_data->pins + i;
215 const struct stm32_desc_function *func = pin->functions;
216
217 if (pin->pin.number != pin_num)
218 continue;
219
220 while (func && func->name) {
221 if (func->num == fnum)
222 return true;
223 func++;
224 }
225
226 break;
227 }
228
229 return false;
230}
231
232static int stm32_pctrl_dt_node_to_map_func(struct stm32_pinctrl *pctl,
233 u32 pin, u32 fnum, struct stm32_pinctrl_group *grp,
234 struct pinctrl_map **map, unsigned *reserved_maps,
235 unsigned *num_maps)
236{
237 if (*num_maps == *reserved_maps)
238 return -ENOSPC;
239
240 (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
241 (*map)[*num_maps].data.mux.group = grp->name;
242
243 if (!stm32_pctrl_is_function_valid(pctl, pin, fnum)) {
244 dev_err(pctl->dev, "invalid function %d on pin %d .\n",
245 fnum, pin);
246 return -EINVAL;
247 }
248
249 (*map)[*num_maps].data.mux.function = stm32_gpio_functions[fnum];
250 (*num_maps)++;
251
252 return 0;
253}
254
255static int stm32_pctrl_dt_subnode_to_map(struct pinctrl_dev *pctldev,
256 struct device_node *node,
257 struct pinctrl_map **map,
258 unsigned *reserved_maps,
259 unsigned *num_maps)
260{
261 struct stm32_pinctrl *pctl;
262 struct stm32_pinctrl_group *grp;
263 struct property *pins;
264 u32 pinfunc, pin, func;
265 unsigned long *configs;
266 unsigned int num_configs;
267 bool has_config = 0;
268 unsigned reserve = 0;
269 int num_pins, num_funcs, maps_per_pin, i, err;
270
271 pctl = pinctrl_dev_get_drvdata(pctldev);
272
273 pins = of_find_property(node, "pinmux", NULL);
274 if (!pins) {
275 dev_err(pctl->dev, "missing pins property in node %s .\n",
276 node->name);
277 return -EINVAL;
278 }
279
280 err = pinconf_generic_parse_dt_config(node, pctldev, &configs,
281 &num_configs);
282 if (err)
283 return err;
284
285 if (num_configs)
286 has_config = 1;
287
288 num_pins = pins->length / sizeof(u32);
289 num_funcs = num_pins;
290 maps_per_pin = 0;
291 if (num_funcs)
292 maps_per_pin++;
293 if (has_config && num_pins >= 1)
294 maps_per_pin++;
295
296 if (!num_pins || !maps_per_pin)
297 return -EINVAL;
298
299 reserve = num_pins * maps_per_pin;
300
301 err = pinctrl_utils_reserve_map(pctldev, map,
302 reserved_maps, num_maps, reserve);
303 if (err)
304 return err;
305
306 for (i = 0; i < num_pins; i++) {
307 err = of_property_read_u32_index(node, "pinmux",
308 i, &pinfunc);
309 if (err)
310 return err;
311
312 pin = STM32_GET_PIN_NO(pinfunc);
313 func = STM32_GET_PIN_FUNC(pinfunc);
314
315 if (pin >= pctl->match_data->npins) {
316 dev_err(pctl->dev, "invalid pin number.\n");
317 return -EINVAL;
318 }
319
320 if (!stm32_pctrl_is_function_valid(pctl, pin, func)) {
321 dev_err(pctl->dev, "invalid function.\n");
322 return -EINVAL;
323 }
324
325 grp = stm32_pctrl_find_group_by_pin(pctl, pin);
326 if (!grp) {
327 dev_err(pctl->dev, "unable to match pin %d to group\n",
328 pin);
329 return -EINVAL;
330 }
331
332 err = stm32_pctrl_dt_node_to_map_func(pctl, pin, func, grp, map,
333 reserved_maps, num_maps);
334 if (err)
335 return err;
336
337 if (has_config) {
338 err = pinctrl_utils_add_map_configs(pctldev, map,
339 reserved_maps, num_maps, grp->name,
340 configs, num_configs,
341 PIN_MAP_TYPE_CONFIGS_GROUP);
342 if (err)
343 return err;
344 }
345 }
346
347 return 0;
348}
349
350static int stm32_pctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
351 struct device_node *np_config,
352 struct pinctrl_map **map, unsigned *num_maps)
353{
354 struct device_node *np;
355 unsigned reserved_maps;
356 int ret;
357
358 *map = NULL;
359 *num_maps = 0;
360 reserved_maps = 0;
361
362 for_each_child_of_node(np_config, np) {
363 ret = stm32_pctrl_dt_subnode_to_map(pctldev, np, map,
364 &reserved_maps, num_maps);
365 if (ret < 0) {
366 pinctrl_utils_dt_free_map(pctldev, *map, *num_maps);
367 return ret;
368 }
369 }
370
371 return 0;
372}
373
374static int stm32_pctrl_get_groups_count(struct pinctrl_dev *pctldev)
375{
376 struct stm32_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
377
378 return pctl->ngroups;
379}
380
381static const char *stm32_pctrl_get_group_name(struct pinctrl_dev *pctldev,
382 unsigned group)
383{
384 struct stm32_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
385
386 return pctl->groups[group].name;
387}
388
389static int stm32_pctrl_get_group_pins(struct pinctrl_dev *pctldev,
390 unsigned group,
391 const unsigned **pins,
392 unsigned *num_pins)
393{
394 struct stm32_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
395
396 *pins = (unsigned *)&pctl->groups[group].pin;
397 *num_pins = 1;
398
399 return 0;
400}
401
402static const struct pinctrl_ops stm32_pctrl_ops = {
403 .dt_node_to_map = stm32_pctrl_dt_node_to_map,
404 .dt_free_map = pinctrl_utils_dt_free_map,
405 .get_groups_count = stm32_pctrl_get_groups_count,
406 .get_group_name = stm32_pctrl_get_group_name,
407 .get_group_pins = stm32_pctrl_get_group_pins,
408};
409
410
411/* Pinmux functions */
412
413static int stm32_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
414{
415 return ARRAY_SIZE(stm32_gpio_functions);
416}
417
418static const char *stm32_pmx_get_func_name(struct pinctrl_dev *pctldev,
419 unsigned selector)
420{
421 return stm32_gpio_functions[selector];
422}
423
424static int stm32_pmx_get_func_groups(struct pinctrl_dev *pctldev,
425 unsigned function,
426 const char * const **groups,
427 unsigned * const num_groups)
428{
429 struct stm32_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
430
431 *groups = pctl->grp_names;
432 *num_groups = pctl->ngroups;
433
434 return 0;
435}
436
437static void stm32_pmx_set_mode(struct stm32_gpio_bank *bank,
438 int pin, u32 mode, u32 alt)
439{
440 u32 val;
441 int alt_shift = (pin % 8) * 4;
442 int alt_offset = STM32_GPIO_AFRL + (pin / 8) * 4;
443 unsigned long flags;
444
445 clk_enable(bank->clk);
446 spin_lock_irqsave(&bank->lock, flags);
447
448 val = readl_relaxed(bank->base + alt_offset);
449 val &= ~GENMASK(alt_shift + 3, alt_shift);
450 val |= (alt << alt_shift);
451 writel_relaxed(val, bank->base + alt_offset);
452
453 val = readl_relaxed(bank->base + STM32_GPIO_MODER);
454 val &= ~GENMASK(pin * 2 + 1, pin * 2);
455 val |= mode << (pin * 2);
456 writel_relaxed(val, bank->base + STM32_GPIO_MODER);
457
458 spin_unlock_irqrestore(&bank->lock, flags);
459 clk_disable(bank->clk);
460}
461
462static int stm32_pmx_set_mux(struct pinctrl_dev *pctldev,
463 unsigned function,
464 unsigned group)
465{
466 bool ret;
467 struct stm32_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
468 struct stm32_pinctrl_group *g = pctl->groups + group;
469 struct pinctrl_gpio_range *range;
470 struct stm32_gpio_bank *bank;
471 u32 mode, alt;
472 int pin;
473
474 ret = stm32_pctrl_is_function_valid(pctl, g->pin, function);
475 if (!ret) {
476 dev_err(pctl->dev, "invalid function %d on group %d .\n",
477 function, group);
478 return -EINVAL;
479 }
480
481 range = pinctrl_find_gpio_range_from_pin(pctldev, g->pin);
482 bank = gpio_range_to_bank(range);
483 pin = stm32_gpio_pin(g->pin);
484
485 mode = stm32_gpio_get_mode(function);
486 alt = stm32_gpio_get_alt(function);
487
488 stm32_pmx_set_mode(bank, pin, mode, alt);
489
490 return 0;
491}
492
493static int stm32_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
494 struct pinctrl_gpio_range *range, unsigned gpio,
495 bool input)
496{
497 struct stm32_gpio_bank *bank = gpio_range_to_bank(range);
498 int pin = stm32_gpio_pin(gpio);
499
500 stm32_pmx_set_mode(bank, pin, !input, 0);
501
502 return 0;
503}
504
505static const struct pinmux_ops stm32_pmx_ops = {
506 .get_functions_count = stm32_pmx_get_funcs_cnt,
507 .get_function_name = stm32_pmx_get_func_name,
508 .get_function_groups = stm32_pmx_get_func_groups,
509 .set_mux = stm32_pmx_set_mux,
510 .gpio_set_direction = stm32_pmx_gpio_set_direction,
511};
512
513/* Pinconf functions */
514
515static void stm32_pconf_set_driving(struct stm32_gpio_bank *bank,
516 unsigned offset, u32 drive)
517{
518 unsigned long flags;
519 u32 val;
520
521 clk_enable(bank->clk);
522 spin_lock_irqsave(&bank->lock, flags);
523
524 val = readl_relaxed(bank->base + STM32_GPIO_TYPER);
525 val &= ~BIT(offset);
526 val |= drive << offset;
527 writel_relaxed(val, bank->base + STM32_GPIO_TYPER);
528
529 spin_unlock_irqrestore(&bank->lock, flags);
530 clk_disable(bank->clk);
531}
532
533static void stm32_pconf_set_speed(struct stm32_gpio_bank *bank,
534 unsigned offset, u32 speed)
535{
536 unsigned long flags;
537 u32 val;
538
539 clk_enable(bank->clk);
540 spin_lock_irqsave(&bank->lock, flags);
541
542 val = readl_relaxed(bank->base + STM32_GPIO_SPEEDR);
543 val &= ~GENMASK(offset * 2 + 1, offset * 2);
544 val |= speed << (offset * 2);
545 writel_relaxed(val, bank->base + STM32_GPIO_SPEEDR);
546
547 spin_unlock_irqrestore(&bank->lock, flags);
548 clk_disable(bank->clk);
549}
550
551static void stm32_pconf_set_bias(struct stm32_gpio_bank *bank,
552 unsigned offset, u32 bias)
553{
554 unsigned long flags;
555 u32 val;
556
557 clk_enable(bank->clk);
558 spin_lock_irqsave(&bank->lock, flags);
559
560 val = readl_relaxed(bank->base + STM32_GPIO_PUPDR);
561 val &= ~GENMASK(offset * 2 + 1, offset * 2);
562 val |= bias << (offset * 2);
563 writel_relaxed(val, bank->base + STM32_GPIO_PUPDR);
564
565 spin_unlock_irqrestore(&bank->lock, flags);
566 clk_disable(bank->clk);
567}
568
569static int stm32_pconf_parse_conf(struct pinctrl_dev *pctldev,
570 unsigned int pin, enum pin_config_param param,
571 enum pin_config_param arg)
572{
573 struct pinctrl_gpio_range *range;
574 struct stm32_gpio_bank *bank;
575 int offset, ret = 0;
576
577 range = pinctrl_find_gpio_range_from_pin(pctldev, pin);
578 bank = gpio_range_to_bank(range);
579 offset = stm32_gpio_pin(pin);
580
581 switch (param) {
582 case PIN_CONFIG_DRIVE_PUSH_PULL:
583 stm32_pconf_set_driving(bank, offset, 0);
584 break;
585 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
586 stm32_pconf_set_driving(bank, offset, 1);
587 break;
588 case PIN_CONFIG_SLEW_RATE:
589 stm32_pconf_set_speed(bank, offset, arg);
590 break;
591 case PIN_CONFIG_BIAS_DISABLE:
592 stm32_pconf_set_bias(bank, offset, 0);
593 break;
594 case PIN_CONFIG_BIAS_PULL_UP:
595 stm32_pconf_set_bias(bank, offset, 1);
596 break;
597 case PIN_CONFIG_BIAS_PULL_DOWN:
598 stm32_pconf_set_bias(bank, offset, 2);
599 break;
600 case PIN_CONFIG_OUTPUT:
601 __stm32_gpio_set(bank, offset, arg);
602 ret = stm32_pmx_gpio_set_direction(pctldev, NULL, pin, false);
603 break;
604 default:
605 ret = -EINVAL;
606 }
607
608 return ret;
609}
610
611static int stm32_pconf_group_get(struct pinctrl_dev *pctldev,
612 unsigned group,
613 unsigned long *config)
614{
615 struct stm32_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
616
617 *config = pctl->groups[group].config;
618
619 return 0;
620}
621
622static int stm32_pconf_group_set(struct pinctrl_dev *pctldev, unsigned group,
623 unsigned long *configs, unsigned num_configs)
624{
625 struct stm32_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
626 struct stm32_pinctrl_group *g = &pctl->groups[group];
627 int i, ret;
628
629 for (i = 0; i < num_configs; i++) {
630 ret = stm32_pconf_parse_conf(pctldev, g->pin,
631 pinconf_to_config_param(configs[i]),
632 pinconf_to_config_argument(configs[i]));
633 if (ret < 0)
634 return ret;
635
636 g->config = configs[i];
637 }
638
639 return 0;
640}
641
642static const struct pinconf_ops stm32_pconf_ops = {
643 .pin_config_group_get = stm32_pconf_group_get,
644 .pin_config_group_set = stm32_pconf_group_set,
645};
646
647static int stm32_gpiolib_register_bank(struct stm32_pinctrl *pctl,
648 struct device_node *np)
649{
650 int bank_nr = pctl->nbanks;
651 struct stm32_gpio_bank *bank = &pctl->banks[bank_nr];
652 struct pinctrl_gpio_range *range = &bank->range;
653 struct device *dev = pctl->dev;
654 struct resource res;
655 struct reset_control *rstc;
656 int err, npins;
657
658 rstc = of_reset_control_get(np, NULL);
659 if (!IS_ERR(rstc))
660 reset_control_deassert(rstc);
661
662 if (of_address_to_resource(np, 0, &res))
663 return -ENODEV;
664
665 bank->base = devm_ioremap_resource(dev, &res);
666 if (IS_ERR(bank->base))
667 return PTR_ERR(bank->base);
668
669 bank->clk = of_clk_get_by_name(np, NULL);
670 if (IS_ERR(bank->clk)) {
671 dev_err(dev, "failed to get clk (%ld)\n", PTR_ERR(bank->clk));
672 return PTR_ERR(bank->clk);
673 }
674
675 err = clk_prepare(bank->clk);
676 if (err) {
677 dev_err(dev, "failed to prepare clk (%d)\n", err);
678 return err;
679 }
680
681 npins = pctl->match_data->npins;
682 npins -= bank_nr * STM32_GPIO_PINS_PER_BANK;
683 if (npins < 0)
684 return -EINVAL;
685 else if (npins > STM32_GPIO_PINS_PER_BANK)
686 npins = STM32_GPIO_PINS_PER_BANK;
687
688 bank->gpio_chip = stm32_gpio_template;
689 bank->gpio_chip.base = bank_nr * STM32_GPIO_PINS_PER_BANK;
690 bank->gpio_chip.ngpio = npins;
691 bank->gpio_chip.of_node = np;
692 bank->gpio_chip.dev = dev;
693 spin_lock_init(&bank->lock);
694
695 of_property_read_string(np, "st,bank-name", &range->name);
696 bank->gpio_chip.label = range->name;
697
698 range->id = bank_nr;
699 range->pin_base = range->base = range->id * STM32_GPIO_PINS_PER_BANK;
700 range->npins = bank->gpio_chip.ngpio;
701 range->gc = &bank->gpio_chip;
702 err = gpiochip_add(&bank->gpio_chip);
703 if (err) {
704 dev_err(dev, "Failed to add gpiochip(%d)!\n", bank_nr);
705 return err;
706 }
707
708 dev_info(dev, "%s bank added\n", range->name);
709 return 0;
710}
711
712static int stm32_pctrl_build_state(struct platform_device *pdev)
713{
714 struct stm32_pinctrl *pctl = platform_get_drvdata(pdev);
715 int i;
716
717 pctl->ngroups = pctl->match_data->npins;
718
719 /* Allocate groups */
720 pctl->groups = devm_kcalloc(&pdev->dev, pctl->ngroups,
721 sizeof(*pctl->groups), GFP_KERNEL);
722 if (!pctl->groups)
723 return -ENOMEM;
724
725 /* We assume that one pin is one group, use pin name as group name. */
726 pctl->grp_names = devm_kcalloc(&pdev->dev, pctl->ngroups,
727 sizeof(*pctl->grp_names), GFP_KERNEL);
728 if (!pctl->grp_names)
729 return -ENOMEM;
730
731 for (i = 0; i < pctl->match_data->npins; i++) {
732 const struct stm32_desc_pin *pin = pctl->match_data->pins + i;
733 struct stm32_pinctrl_group *group = pctl->groups + i;
734
735 group->name = pin->pin.name;
736 group->pin = pin->pin.number;
737
738 pctl->grp_names[i] = pin->pin.name;
739 }
740
741 return 0;
742}
743
744int stm32_pctl_probe(struct platform_device *pdev)
745{
746 struct device_node *np = pdev->dev.of_node;
747 struct device_node *child;
748 const struct of_device_id *match;
749 struct device *dev = &pdev->dev;
750 struct stm32_pinctrl *pctl;
751 struct pinctrl_pin_desc *pins;
752 int i, ret, banks = 0;
753
754 if (!np)
755 return -EINVAL;
756
757 match = of_match_device(dev->driver->of_match_table, dev);
758 if (!match || !match->data)
759 return -EINVAL;
760
761 if (!of_find_property(np, "pins-are-numbered", NULL)) {
762 dev_err(dev, "only support pins-are-numbered format\n");
763 return -EINVAL;
764 }
765
766 pctl = devm_kzalloc(dev, sizeof(*pctl), GFP_KERNEL);
767 if (!pctl)
768 return -ENOMEM;
769
770 platform_set_drvdata(pdev, pctl);
771
772 pctl->dev = dev;
773 pctl->match_data = match->data;
774 ret = stm32_pctrl_build_state(pdev);
775 if (ret) {
776 dev_err(dev, "build state failed: %d\n", ret);
777 return -EINVAL;
778 }
779
780 for_each_child_of_node(np, child)
781 if (of_property_read_bool(child, "gpio-controller"))
782 banks++;
783
784 if (!banks) {
785 dev_err(dev, "at least one GPIO bank is required\n");
786 return -EINVAL;
787 }
788
789 pctl->banks = devm_kcalloc(dev, banks, sizeof(*pctl->banks),
790 GFP_KERNEL);
791 if (!pctl->banks)
792 return -ENOMEM;
793
794 for_each_child_of_node(np, child) {
795 if (of_property_read_bool(child, "gpio-controller")) {
796 ret = stm32_gpiolib_register_bank(pctl, child);
797 if (ret)
798 return ret;
799
800 pctl->nbanks++;
801 }
802 }
803
804 pins = devm_kcalloc(&pdev->dev, pctl->match_data->npins, sizeof(*pins),
805 GFP_KERNEL);
806 if (!pins)
807 return -ENOMEM;
808
809 for (i = 0; i < pctl->match_data->npins; i++)
810 pins[i] = pctl->match_data->pins[i].pin;
811
812 pctl->pctl_desc.name = dev_name(&pdev->dev);
813 pctl->pctl_desc.owner = THIS_MODULE;
814 pctl->pctl_desc.pins = pins;
815 pctl->pctl_desc.npins = pctl->match_data->npins;
816 pctl->pctl_desc.confops = &stm32_pconf_ops;
817 pctl->pctl_desc.pctlops = &stm32_pctrl_ops;
818 pctl->pctl_desc.pmxops = &stm32_pmx_ops;
819 pctl->dev = &pdev->dev;
820
821 pctl->pctl_dev = pinctrl_register(&pctl->pctl_desc, &pdev->dev, pctl);
822 if (!pctl->pctl_dev) {
823 dev_err(&pdev->dev, "Failed pinctrl registration\n");
824 return -EINVAL;
825 }
826
827 for (i = 0; i < pctl->nbanks; i++)
828 pinctrl_add_gpio_range(pctl->pctl_dev, &pctl->banks[i].range);
829
830 dev_info(dev, "Pinctrl STM32 initialized\n");
831
832 return 0;
833}
834