1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) Maxime Coquelin 2015
4 * Copyright (C) STMicroelectronics 2017
5 * Author: Maxime Coquelin <mcoquelin.stm32@gmail.com>
7 * Heavily based on Mediatek's pinctrl driver
10 #include <linux/gpio/driver.h>
12 #include <linux/irq.h>
13 #include <linux/mfd/syscon.h>
14 #include <linux/module.h>
16 #include <linux/of_address.h>
17 #include <linux/of_device.h>
18 #include <linux/of_irq.h>
19 #include <linux/pinctrl/consumer.h>
20 #include <linux/pinctrl/machine.h>
21 #include <linux/pinctrl/pinconf.h>
22 #include <linux/pinctrl/pinconf-generic.h>
23 #include <linux/pinctrl/pinctrl.h>
24 #include <linux/pinctrl/pinmux.h>
25 #include <linux/platform_device.h>
26 #include <linux/regmap.h>
27 #include <linux/reset.h>
28 #include <linux/slab.h>
31 #include "../pinconf.h"
32 #include "../pinctrl-utils.h"
33 #include "pinctrl-stm32.h"
35 #define STM32_GPIO_MODER 0x00
36 #define STM32_GPIO_TYPER 0x04
37 #define STM32_GPIO_SPEEDR 0x08
38 #define STM32_GPIO_PUPDR 0x0c
39 #define STM32_GPIO_IDR 0x10
40 #define STM32_GPIO_ODR 0x14
41 #define STM32_GPIO_BSRR 0x18
42 #define STM32_GPIO_LCKR 0x1c
43 #define STM32_GPIO_AFRL 0x20
44 #define STM32_GPIO_AFRH 0x24
46 #define STM32_GPIO_PINS_PER_BANK 16
47 #define STM32_GPIO_IRQ_LINE 16
49 #define gpio_range_to_bank(chip) \
50 container_of(chip, struct stm32_gpio_bank, range)
52 static const char * const stm32_gpio_functions
[] = {
57 "af11", "af12", "af13",
58 "af14", "af15", "analog",
61 struct stm32_pinctrl_group
{
67 struct stm32_gpio_bank
{
71 struct gpio_chip gpio_chip
;
72 struct pinctrl_gpio_range range
;
73 struct fwnode_handle
*fwnode
;
74 struct irq_domain
*domain
;
78 struct stm32_pinctrl
{
80 struct pinctrl_dev
*pctl_dev
;
81 struct pinctrl_desc pctl_desc
;
82 struct stm32_pinctrl_group
*groups
;
84 const char **grp_names
;
85 struct stm32_gpio_bank
*banks
;
87 const struct stm32_pinctrl_match_data
*match_data
;
88 struct irq_domain
*domain
;
89 struct regmap
*regmap
;
90 struct regmap_field
*irqmux
[STM32_GPIO_PINS_PER_BANK
];
93 static inline int stm32_gpio_pin(int gpio
)
95 return gpio
% STM32_GPIO_PINS_PER_BANK
;
98 static inline u32
stm32_gpio_get_mode(u32 function
)
103 case STM32_PIN_AF(0) ... STM32_PIN_AF(15):
105 case STM32_PIN_ANALOG
:
112 static inline u32
stm32_gpio_get_alt(u32 function
)
117 case STM32_PIN_AF(0) ... STM32_PIN_AF(15):
119 case STM32_PIN_ANALOG
:
128 static inline void __stm32_gpio_set(struct stm32_gpio_bank
*bank
,
129 unsigned offset
, int value
)
132 offset
+= STM32_GPIO_PINS_PER_BANK
;
134 clk_enable(bank
->clk
);
136 writel_relaxed(BIT(offset
), bank
->base
+ STM32_GPIO_BSRR
);
138 clk_disable(bank
->clk
);
141 static int stm32_gpio_request(struct gpio_chip
*chip
, unsigned offset
)
143 struct stm32_gpio_bank
*bank
= gpiochip_get_data(chip
);
144 struct stm32_pinctrl
*pctl
= dev_get_drvdata(bank
->gpio_chip
.parent
);
145 struct pinctrl_gpio_range
*range
;
146 int pin
= offset
+ (bank
->bank_nr
* STM32_GPIO_PINS_PER_BANK
);
148 range
= pinctrl_find_gpio_range_from_pin_nolock(pctl
->pctl_dev
, pin
);
150 dev_err(pctl
->dev
, "pin %d not in range.\n", pin
);
154 return pinctrl_gpio_request(chip
->base
+ offset
);
157 static void stm32_gpio_free(struct gpio_chip
*chip
, unsigned offset
)
159 pinctrl_gpio_free(chip
->base
+ offset
);
162 static int stm32_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
164 struct stm32_gpio_bank
*bank
= gpiochip_get_data(chip
);
167 clk_enable(bank
->clk
);
169 ret
= !!(readl_relaxed(bank
->base
+ STM32_GPIO_IDR
) & BIT(offset
));
171 clk_disable(bank
->clk
);
176 static void stm32_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
178 struct stm32_gpio_bank
*bank
= gpiochip_get_data(chip
);
180 __stm32_gpio_set(bank
, offset
, value
);
183 static int stm32_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
185 return pinctrl_gpio_direction_input(chip
->base
+ offset
);
188 static int stm32_gpio_direction_output(struct gpio_chip
*chip
,
189 unsigned offset
, int value
)
191 struct stm32_gpio_bank
*bank
= gpiochip_get_data(chip
);
193 __stm32_gpio_set(bank
, offset
, value
);
194 pinctrl_gpio_direction_output(chip
->base
+ offset
);
200 static int stm32_gpio_to_irq(struct gpio_chip
*chip
, unsigned int offset
)
202 struct stm32_gpio_bank
*bank
= gpiochip_get_data(chip
);
203 struct irq_fwspec fwspec
;
205 fwspec
.fwnode
= bank
->fwnode
;
206 fwspec
.param_count
= 2;
207 fwspec
.param
[0] = offset
;
208 fwspec
.param
[1] = IRQ_TYPE_NONE
;
210 return irq_create_fwspec_mapping(&fwspec
);
213 static int stm32_gpio_get_direction(struct gpio_chip
*chip
, unsigned int offset
)
215 struct stm32_gpio_bank
*bank
= gpiochip_get_data(chip
);
216 int pin
= stm32_gpio_pin(offset
);
220 stm32_pmx_get_mode(bank
, pin
, &mode
, &alt
);
221 if ((alt
== 0) && (mode
== 0))
223 else if ((alt
== 0) && (mode
== 1))
231 static const struct gpio_chip stm32_gpio_template
= {
232 .request
= stm32_gpio_request
,
233 .free
= stm32_gpio_free
,
234 .get
= stm32_gpio_get
,
235 .set
= stm32_gpio_set
,
236 .direction_input
= stm32_gpio_direction_input
,
237 .direction_output
= stm32_gpio_direction_output
,
238 .to_irq
= stm32_gpio_to_irq
,
239 .get_direction
= stm32_gpio_get_direction
,
242 static int stm32_gpio_irq_request_resources(struct irq_data
*irq_data
)
244 struct stm32_gpio_bank
*bank
= irq_data
->domain
->host_data
;
245 struct stm32_pinctrl
*pctl
= dev_get_drvdata(bank
->gpio_chip
.parent
);
248 ret
= stm32_gpio_direction_input(&bank
->gpio_chip
, irq_data
->hwirq
);
252 ret
= gpiochip_lock_as_irq(&bank
->gpio_chip
, irq_data
->hwirq
);
254 dev_err(pctl
->dev
, "unable to lock HW IRQ %lu for IRQ\n",
262 static void stm32_gpio_irq_release_resources(struct irq_data
*irq_data
)
264 struct stm32_gpio_bank
*bank
= irq_data
->domain
->host_data
;
266 gpiochip_unlock_as_irq(&bank
->gpio_chip
, irq_data
->hwirq
);
269 static struct irq_chip stm32_gpio_irq_chip
= {
271 .irq_eoi
= irq_chip_eoi_parent
,
272 .irq_mask
= irq_chip_mask_parent
,
273 .irq_unmask
= irq_chip_unmask_parent
,
274 .irq_set_type
= irq_chip_set_type_parent
,
275 .irq_request_resources
= stm32_gpio_irq_request_resources
,
276 .irq_release_resources
= stm32_gpio_irq_release_resources
,
279 static int stm32_gpio_domain_translate(struct irq_domain
*d
,
280 struct irq_fwspec
*fwspec
,
281 unsigned long *hwirq
,
284 if ((fwspec
->param_count
!= 2) ||
285 (fwspec
->param
[0] >= STM32_GPIO_IRQ_LINE
))
288 *hwirq
= fwspec
->param
[0];
289 *type
= fwspec
->param
[1];
293 static int stm32_gpio_domain_activate(struct irq_domain
*d
,
294 struct irq_data
*irq_data
, bool reserve
)
296 struct stm32_gpio_bank
*bank
= d
->host_data
;
297 struct stm32_pinctrl
*pctl
= dev_get_drvdata(bank
->gpio_chip
.parent
);
299 regmap_field_write(pctl
->irqmux
[irq_data
->hwirq
], bank
->bank_nr
);
303 static int stm32_gpio_domain_alloc(struct irq_domain
*d
,
305 unsigned int nr_irqs
, void *data
)
307 struct stm32_gpio_bank
*bank
= d
->host_data
;
308 struct irq_fwspec
*fwspec
= data
;
309 struct irq_fwspec parent_fwspec
;
310 irq_hw_number_t hwirq
;
312 hwirq
= fwspec
->param
[0];
313 parent_fwspec
.fwnode
= d
->parent
->fwnode
;
314 parent_fwspec
.param_count
= 2;
315 parent_fwspec
.param
[0] = fwspec
->param
[0];
316 parent_fwspec
.param
[1] = fwspec
->param
[1];
318 irq_domain_set_hwirq_and_chip(d
, virq
, hwirq
, &stm32_gpio_irq_chip
,
321 return irq_domain_alloc_irqs_parent(d
, virq
, nr_irqs
, &parent_fwspec
);
324 static const struct irq_domain_ops stm32_gpio_domain_ops
= {
325 .translate
= stm32_gpio_domain_translate
,
326 .alloc
= stm32_gpio_domain_alloc
,
327 .free
= irq_domain_free_irqs_common
,
328 .activate
= stm32_gpio_domain_activate
,
331 /* Pinctrl functions */
332 static struct stm32_pinctrl_group
*
333 stm32_pctrl_find_group_by_pin(struct stm32_pinctrl
*pctl
, u32 pin
)
337 for (i
= 0; i
< pctl
->ngroups
; i
++) {
338 struct stm32_pinctrl_group
*grp
= pctl
->groups
+ i
;
347 static bool stm32_pctrl_is_function_valid(struct stm32_pinctrl
*pctl
,
348 u32 pin_num
, u32 fnum
)
352 for (i
= 0; i
< pctl
->match_data
->npins
; i
++) {
353 const struct stm32_desc_pin
*pin
= pctl
->match_data
->pins
+ i
;
354 const struct stm32_desc_function
*func
= pin
->functions
;
356 if (pin
->pin
.number
!= pin_num
)
359 while (func
&& func
->name
) {
360 if (func
->num
== fnum
)
371 static int stm32_pctrl_dt_node_to_map_func(struct stm32_pinctrl
*pctl
,
372 u32 pin
, u32 fnum
, struct stm32_pinctrl_group
*grp
,
373 struct pinctrl_map
**map
, unsigned *reserved_maps
,
376 if (*num_maps
== *reserved_maps
)
379 (*map
)[*num_maps
].type
= PIN_MAP_TYPE_MUX_GROUP
;
380 (*map
)[*num_maps
].data
.mux
.group
= grp
->name
;
382 if (!stm32_pctrl_is_function_valid(pctl
, pin
, fnum
)) {
383 dev_err(pctl
->dev
, "invalid function %d on pin %d .\n",
388 (*map
)[*num_maps
].data
.mux
.function
= stm32_gpio_functions
[fnum
];
394 static int stm32_pctrl_dt_subnode_to_map(struct pinctrl_dev
*pctldev
,
395 struct device_node
*node
,
396 struct pinctrl_map
**map
,
397 unsigned *reserved_maps
,
400 struct stm32_pinctrl
*pctl
;
401 struct stm32_pinctrl_group
*grp
;
402 struct property
*pins
;
403 u32 pinfunc
, pin
, func
;
404 unsigned long *configs
;
405 unsigned int num_configs
;
407 unsigned reserve
= 0;
408 int num_pins
, num_funcs
, maps_per_pin
, i
, err
;
410 pctl
= pinctrl_dev_get_drvdata(pctldev
);
412 pins
= of_find_property(node
, "pinmux", NULL
);
414 dev_err(pctl
->dev
, "missing pins property in node %s .\n",
419 err
= pinconf_generic_parse_dt_config(node
, pctldev
, &configs
,
427 num_pins
= pins
->length
/ sizeof(u32
);
428 num_funcs
= num_pins
;
432 if (has_config
&& num_pins
>= 1)
435 if (!num_pins
|| !maps_per_pin
)
438 reserve
= num_pins
* maps_per_pin
;
440 err
= pinctrl_utils_reserve_map(pctldev
, map
,
441 reserved_maps
, num_maps
, reserve
);
445 for (i
= 0; i
< num_pins
; i
++) {
446 err
= of_property_read_u32_index(node
, "pinmux",
451 pin
= STM32_GET_PIN_NO(pinfunc
);
452 func
= STM32_GET_PIN_FUNC(pinfunc
);
454 if (!stm32_pctrl_is_function_valid(pctl
, pin
, func
)) {
455 dev_err(pctl
->dev
, "invalid function.\n");
459 grp
= stm32_pctrl_find_group_by_pin(pctl
, pin
);
461 dev_err(pctl
->dev
, "unable to match pin %d to group\n",
466 err
= stm32_pctrl_dt_node_to_map_func(pctl
, pin
, func
, grp
, map
,
467 reserved_maps
, num_maps
);
472 err
= pinctrl_utils_add_map_configs(pctldev
, map
,
473 reserved_maps
, num_maps
, grp
->name
,
474 configs
, num_configs
,
475 PIN_MAP_TYPE_CONFIGS_GROUP
);
484 static int stm32_pctrl_dt_node_to_map(struct pinctrl_dev
*pctldev
,
485 struct device_node
*np_config
,
486 struct pinctrl_map
**map
, unsigned *num_maps
)
488 struct device_node
*np
;
489 unsigned reserved_maps
;
496 for_each_child_of_node(np_config
, np
) {
497 ret
= stm32_pctrl_dt_subnode_to_map(pctldev
, np
, map
,
498 &reserved_maps
, num_maps
);
500 pinctrl_utils_free_map(pctldev
, *map
, *num_maps
);
508 static int stm32_pctrl_get_groups_count(struct pinctrl_dev
*pctldev
)
510 struct stm32_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
512 return pctl
->ngroups
;
515 static const char *stm32_pctrl_get_group_name(struct pinctrl_dev
*pctldev
,
518 struct stm32_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
520 return pctl
->groups
[group
].name
;
523 static int stm32_pctrl_get_group_pins(struct pinctrl_dev
*pctldev
,
525 const unsigned **pins
,
528 struct stm32_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
530 *pins
= (unsigned *)&pctl
->groups
[group
].pin
;
536 static const struct pinctrl_ops stm32_pctrl_ops
= {
537 .dt_node_to_map
= stm32_pctrl_dt_node_to_map
,
538 .dt_free_map
= pinctrl_utils_free_map
,
539 .get_groups_count
= stm32_pctrl_get_groups_count
,
540 .get_group_name
= stm32_pctrl_get_group_name
,
541 .get_group_pins
= stm32_pctrl_get_group_pins
,
545 /* Pinmux functions */
547 static int stm32_pmx_get_funcs_cnt(struct pinctrl_dev
*pctldev
)
549 return ARRAY_SIZE(stm32_gpio_functions
);
552 static const char *stm32_pmx_get_func_name(struct pinctrl_dev
*pctldev
,
555 return stm32_gpio_functions
[selector
];
558 static int stm32_pmx_get_func_groups(struct pinctrl_dev
*pctldev
,
560 const char * const **groups
,
561 unsigned * const num_groups
)
563 struct stm32_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
565 *groups
= pctl
->grp_names
;
566 *num_groups
= pctl
->ngroups
;
571 static void stm32_pmx_set_mode(struct stm32_gpio_bank
*bank
,
572 int pin
, u32 mode
, u32 alt
)
575 int alt_shift
= (pin
% 8) * 4;
576 int alt_offset
= STM32_GPIO_AFRL
+ (pin
/ 8) * 4;
579 clk_enable(bank
->clk
);
580 spin_lock_irqsave(&bank
->lock
, flags
);
582 val
= readl_relaxed(bank
->base
+ alt_offset
);
583 val
&= ~GENMASK(alt_shift
+ 3, alt_shift
);
584 val
|= (alt
<< alt_shift
);
585 writel_relaxed(val
, bank
->base
+ alt_offset
);
587 val
= readl_relaxed(bank
->base
+ STM32_GPIO_MODER
);
588 val
&= ~GENMASK(pin
* 2 + 1, pin
* 2);
589 val
|= mode
<< (pin
* 2);
590 writel_relaxed(val
, bank
->base
+ STM32_GPIO_MODER
);
592 spin_unlock_irqrestore(&bank
->lock
, flags
);
593 clk_disable(bank
->clk
);
596 void stm32_pmx_get_mode(struct stm32_gpio_bank
*bank
, int pin
, u32
*mode
,
600 int alt_shift
= (pin
% 8) * 4;
601 int alt_offset
= STM32_GPIO_AFRL
+ (pin
/ 8) * 4;
604 clk_enable(bank
->clk
);
605 spin_lock_irqsave(&bank
->lock
, flags
);
607 val
= readl_relaxed(bank
->base
+ alt_offset
);
608 val
&= GENMASK(alt_shift
+ 3, alt_shift
);
609 *alt
= val
>> alt_shift
;
611 val
= readl_relaxed(bank
->base
+ STM32_GPIO_MODER
);
612 val
&= GENMASK(pin
* 2 + 1, pin
* 2);
613 *mode
= val
>> (pin
* 2);
615 spin_unlock_irqrestore(&bank
->lock
, flags
);
616 clk_disable(bank
->clk
);
619 static int stm32_pmx_set_mux(struct pinctrl_dev
*pctldev
,
624 struct stm32_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
625 struct stm32_pinctrl_group
*g
= pctl
->groups
+ group
;
626 struct pinctrl_gpio_range
*range
;
627 struct stm32_gpio_bank
*bank
;
631 ret
= stm32_pctrl_is_function_valid(pctl
, g
->pin
, function
);
633 dev_err(pctl
->dev
, "invalid function %d on group %d .\n",
638 range
= pinctrl_find_gpio_range_from_pin(pctldev
, g
->pin
);
639 bank
= gpiochip_get_data(range
->gc
);
640 pin
= stm32_gpio_pin(g
->pin
);
642 mode
= stm32_gpio_get_mode(function
);
643 alt
= stm32_gpio_get_alt(function
);
645 stm32_pmx_set_mode(bank
, pin
, mode
, alt
);
650 static int stm32_pmx_gpio_set_direction(struct pinctrl_dev
*pctldev
,
651 struct pinctrl_gpio_range
*range
, unsigned gpio
,
654 struct stm32_gpio_bank
*bank
= gpiochip_get_data(range
->gc
);
655 int pin
= stm32_gpio_pin(gpio
);
657 stm32_pmx_set_mode(bank
, pin
, !input
, 0);
662 static const struct pinmux_ops stm32_pmx_ops
= {
663 .get_functions_count
= stm32_pmx_get_funcs_cnt
,
664 .get_function_name
= stm32_pmx_get_func_name
,
665 .get_function_groups
= stm32_pmx_get_func_groups
,
666 .set_mux
= stm32_pmx_set_mux
,
667 .gpio_set_direction
= stm32_pmx_gpio_set_direction
,
671 /* Pinconf functions */
673 static void stm32_pconf_set_driving(struct stm32_gpio_bank
*bank
,
674 unsigned offset
, u32 drive
)
679 clk_enable(bank
->clk
);
680 spin_lock_irqsave(&bank
->lock
, flags
);
682 val
= readl_relaxed(bank
->base
+ STM32_GPIO_TYPER
);
684 val
|= drive
<< offset
;
685 writel_relaxed(val
, bank
->base
+ STM32_GPIO_TYPER
);
687 spin_unlock_irqrestore(&bank
->lock
, flags
);
688 clk_disable(bank
->clk
);
691 static u32
stm32_pconf_get_driving(struct stm32_gpio_bank
*bank
,
697 clk_enable(bank
->clk
);
698 spin_lock_irqsave(&bank
->lock
, flags
);
700 val
= readl_relaxed(bank
->base
+ STM32_GPIO_TYPER
);
703 spin_unlock_irqrestore(&bank
->lock
, flags
);
704 clk_disable(bank
->clk
);
706 return (val
>> offset
);
709 static void stm32_pconf_set_speed(struct stm32_gpio_bank
*bank
,
710 unsigned offset
, u32 speed
)
715 clk_enable(bank
->clk
);
716 spin_lock_irqsave(&bank
->lock
, flags
);
718 val
= readl_relaxed(bank
->base
+ STM32_GPIO_SPEEDR
);
719 val
&= ~GENMASK(offset
* 2 + 1, offset
* 2);
720 val
|= speed
<< (offset
* 2);
721 writel_relaxed(val
, bank
->base
+ STM32_GPIO_SPEEDR
);
723 spin_unlock_irqrestore(&bank
->lock
, flags
);
724 clk_disable(bank
->clk
);
727 static u32
stm32_pconf_get_speed(struct stm32_gpio_bank
*bank
,
733 clk_enable(bank
->clk
);
734 spin_lock_irqsave(&bank
->lock
, flags
);
736 val
= readl_relaxed(bank
->base
+ STM32_GPIO_SPEEDR
);
737 val
&= GENMASK(offset
* 2 + 1, offset
* 2);
739 spin_unlock_irqrestore(&bank
->lock
, flags
);
740 clk_disable(bank
->clk
);
742 return (val
>> (offset
* 2));
745 static void stm32_pconf_set_bias(struct stm32_gpio_bank
*bank
,
746 unsigned offset
, u32 bias
)
751 clk_enable(bank
->clk
);
752 spin_lock_irqsave(&bank
->lock
, flags
);
754 val
= readl_relaxed(bank
->base
+ STM32_GPIO_PUPDR
);
755 val
&= ~GENMASK(offset
* 2 + 1, offset
* 2);
756 val
|= bias
<< (offset
* 2);
757 writel_relaxed(val
, bank
->base
+ STM32_GPIO_PUPDR
);
759 spin_unlock_irqrestore(&bank
->lock
, flags
);
760 clk_disable(bank
->clk
);
763 static u32
stm32_pconf_get_bias(struct stm32_gpio_bank
*bank
,
769 clk_enable(bank
->clk
);
770 spin_lock_irqsave(&bank
->lock
, flags
);
772 val
= readl_relaxed(bank
->base
+ STM32_GPIO_PUPDR
);
773 val
&= GENMASK(offset
* 2 + 1, offset
* 2);
775 spin_unlock_irqrestore(&bank
->lock
, flags
);
776 clk_disable(bank
->clk
);
778 return (val
>> (offset
* 2));
781 static bool stm32_pconf_get(struct stm32_gpio_bank
*bank
,
782 unsigned int offset
, bool dir
)
787 clk_enable(bank
->clk
);
788 spin_lock_irqsave(&bank
->lock
, flags
);
791 val
= !!(readl_relaxed(bank
->base
+ STM32_GPIO_IDR
) &
794 val
= !!(readl_relaxed(bank
->base
+ STM32_GPIO_ODR
) &
797 spin_unlock_irqrestore(&bank
->lock
, flags
);
798 clk_disable(bank
->clk
);
803 static int stm32_pconf_parse_conf(struct pinctrl_dev
*pctldev
,
804 unsigned int pin
, enum pin_config_param param
,
805 enum pin_config_param arg
)
807 struct pinctrl_gpio_range
*range
;
808 struct stm32_gpio_bank
*bank
;
811 range
= pinctrl_find_gpio_range_from_pin(pctldev
, pin
);
812 bank
= gpiochip_get_data(range
->gc
);
813 offset
= stm32_gpio_pin(pin
);
816 case PIN_CONFIG_DRIVE_PUSH_PULL
:
817 stm32_pconf_set_driving(bank
, offset
, 0);
819 case PIN_CONFIG_DRIVE_OPEN_DRAIN
:
820 stm32_pconf_set_driving(bank
, offset
, 1);
822 case PIN_CONFIG_SLEW_RATE
:
823 stm32_pconf_set_speed(bank
, offset
, arg
);
825 case PIN_CONFIG_BIAS_DISABLE
:
826 stm32_pconf_set_bias(bank
, offset
, 0);
828 case PIN_CONFIG_BIAS_PULL_UP
:
829 stm32_pconf_set_bias(bank
, offset
, 1);
831 case PIN_CONFIG_BIAS_PULL_DOWN
:
832 stm32_pconf_set_bias(bank
, offset
, 2);
834 case PIN_CONFIG_OUTPUT
:
835 __stm32_gpio_set(bank
, offset
, arg
);
836 ret
= stm32_pmx_gpio_set_direction(pctldev
, range
, pin
, false);
845 static int stm32_pconf_group_get(struct pinctrl_dev
*pctldev
,
847 unsigned long *config
)
849 struct stm32_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
851 *config
= pctl
->groups
[group
].config
;
856 static int stm32_pconf_group_set(struct pinctrl_dev
*pctldev
, unsigned group
,
857 unsigned long *configs
, unsigned num_configs
)
859 struct stm32_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
860 struct stm32_pinctrl_group
*g
= &pctl
->groups
[group
];
863 for (i
= 0; i
< num_configs
; i
++) {
864 ret
= stm32_pconf_parse_conf(pctldev
, g
->pin
,
865 pinconf_to_config_param(configs
[i
]),
866 pinconf_to_config_argument(configs
[i
]));
870 g
->config
= configs
[i
];
876 static void stm32_pconf_dbg_show(struct pinctrl_dev
*pctldev
,
880 struct pinctrl_gpio_range
*range
;
881 struct stm32_gpio_bank
*bank
;
883 u32 mode
, alt
, drive
, speed
, bias
;
884 static const char * const modes
[] = {
885 "input", "output", "alternate", "analog" };
886 static const char * const speeds
[] = {
887 "low", "medium", "high", "very high" };
888 static const char * const biasing
[] = {
889 "floating", "pull up", "pull down", "" };
892 range
= pinctrl_find_gpio_range_from_pin_nolock(pctldev
, pin
);
893 bank
= gpiochip_get_data(range
->gc
);
894 offset
= stm32_gpio_pin(pin
);
896 stm32_pmx_get_mode(bank
, offset
, &mode
, &alt
);
897 bias
= stm32_pconf_get_bias(bank
, offset
);
899 seq_printf(s
, "%s ", modes
[mode
]);
904 val
= stm32_pconf_get(bank
, offset
, true);
905 seq_printf(s
, "- %s - %s",
906 val
? "high" : "low",
912 drive
= stm32_pconf_get_driving(bank
, offset
);
913 speed
= stm32_pconf_get_speed(bank
, offset
);
914 val
= stm32_pconf_get(bank
, offset
, false);
915 seq_printf(s
, "- %s - %s - %s - %s %s",
916 val
? "high" : "low",
917 drive
? "open drain" : "push pull",
919 speeds
[speed
], "speed");
924 drive
= stm32_pconf_get_driving(bank
, offset
);
925 speed
= stm32_pconf_get_speed(bank
, offset
);
926 seq_printf(s
, "%d - %s - %s - %s %s", alt
,
927 drive
? "open drain" : "push pull",
929 speeds
[speed
], "speed");
939 static const struct pinconf_ops stm32_pconf_ops
= {
940 .pin_config_group_get
= stm32_pconf_group_get
,
941 .pin_config_group_set
= stm32_pconf_group_set
,
942 .pin_config_dbg_show
= stm32_pconf_dbg_show
,
945 static int stm32_gpiolib_register_bank(struct stm32_pinctrl
*pctl
,
946 struct device_node
*np
)
948 struct stm32_gpio_bank
*bank
= &pctl
->banks
[pctl
->nbanks
];
949 struct pinctrl_gpio_range
*range
= &bank
->range
;
950 struct of_phandle_args args
;
951 struct device
*dev
= pctl
->dev
;
953 struct reset_control
*rstc
;
954 int npins
= STM32_GPIO_PINS_PER_BANK
;
957 rstc
= of_reset_control_get_exclusive(np
, NULL
);
959 reset_control_deassert(rstc
);
961 if (of_address_to_resource(np
, 0, &res
))
964 bank
->base
= devm_ioremap_resource(dev
, &res
);
965 if (IS_ERR(bank
->base
))
966 return PTR_ERR(bank
->base
);
968 bank
->clk
= of_clk_get_by_name(np
, NULL
);
969 if (IS_ERR(bank
->clk
)) {
970 dev_err(dev
, "failed to get clk (%ld)\n", PTR_ERR(bank
->clk
));
971 return PTR_ERR(bank
->clk
);
974 err
= clk_prepare(bank
->clk
);
976 dev_err(dev
, "failed to prepare clk (%d)\n", err
);
980 bank
->gpio_chip
= stm32_gpio_template
;
982 of_property_read_string(np
, "st,bank-name", &bank
->gpio_chip
.label
);
984 if (!of_parse_phandle_with_fixed_args(np
, "gpio-ranges", 3, 0, &args
)) {
985 bank_nr
= args
.args
[1] / STM32_GPIO_PINS_PER_BANK
;
986 bank
->gpio_chip
.base
= args
.args
[1];
988 bank_nr
= pctl
->nbanks
;
989 bank
->gpio_chip
.base
= bank_nr
* STM32_GPIO_PINS_PER_BANK
;
990 range
->name
= bank
->gpio_chip
.label
;
992 range
->pin_base
= range
->id
* STM32_GPIO_PINS_PER_BANK
;
993 range
->base
= range
->id
* STM32_GPIO_PINS_PER_BANK
;
994 range
->npins
= npins
;
995 range
->gc
= &bank
->gpio_chip
;
996 pinctrl_add_gpio_range(pctl
->pctl_dev
,
997 &pctl
->banks
[bank_nr
].range
);
999 bank
->gpio_chip
.base
= bank_nr
* STM32_GPIO_PINS_PER_BANK
;
1001 bank
->gpio_chip
.ngpio
= npins
;
1002 bank
->gpio_chip
.of_node
= np
;
1003 bank
->gpio_chip
.parent
= dev
;
1004 bank
->bank_nr
= bank_nr
;
1005 spin_lock_init(&bank
->lock
);
1007 /* create irq hierarchical domain */
1008 bank
->fwnode
= of_node_to_fwnode(np
);
1010 bank
->domain
= irq_domain_create_hierarchy(pctl
->domain
, 0,
1011 STM32_GPIO_IRQ_LINE
, bank
->fwnode
,
1012 &stm32_gpio_domain_ops
, bank
);
1017 err
= gpiochip_add_data(&bank
->gpio_chip
, bank
);
1019 dev_err(dev
, "Failed to add gpiochip(%d)!\n", bank_nr
);
1023 dev_info(dev
, "%s bank added\n", bank
->gpio_chip
.label
);
1027 static int stm32_pctrl_dt_setup_irq(struct platform_device
*pdev
,
1028 struct stm32_pinctrl
*pctl
)
1030 struct device_node
*np
= pdev
->dev
.of_node
, *parent
;
1031 struct device
*dev
= &pdev
->dev
;
1035 parent
= of_irq_find_parent(np
);
1039 pctl
->domain
= irq_find_host(parent
);
1043 pctl
->regmap
= syscon_regmap_lookup_by_phandle(np
, "st,syscfg");
1044 if (IS_ERR(pctl
->regmap
))
1045 return PTR_ERR(pctl
->regmap
);
1049 ret
= of_property_read_u32_index(np
, "st,syscfg", 1, &offset
);
1053 for (i
= 0; i
< STM32_GPIO_PINS_PER_BANK
; i
++) {
1054 struct reg_field mux
;
1056 mux
.reg
= offset
+ (i
/ 4) * 4;
1057 mux
.lsb
= (i
% 4) * 4;
1058 mux
.msb
= mux
.lsb
+ 3;
1060 pctl
->irqmux
[i
] = devm_regmap_field_alloc(dev
, rm
, mux
);
1061 if (IS_ERR(pctl
->irqmux
[i
]))
1062 return PTR_ERR(pctl
->irqmux
[i
]);
1068 static int stm32_pctrl_build_state(struct platform_device
*pdev
)
1070 struct stm32_pinctrl
*pctl
= platform_get_drvdata(pdev
);
1073 pctl
->ngroups
= pctl
->match_data
->npins
;
1075 /* Allocate groups */
1076 pctl
->groups
= devm_kcalloc(&pdev
->dev
, pctl
->ngroups
,
1077 sizeof(*pctl
->groups
), GFP_KERNEL
);
1081 /* We assume that one pin is one group, use pin name as group name. */
1082 pctl
->grp_names
= devm_kcalloc(&pdev
->dev
, pctl
->ngroups
,
1083 sizeof(*pctl
->grp_names
), GFP_KERNEL
);
1084 if (!pctl
->grp_names
)
1087 for (i
= 0; i
< pctl
->match_data
->npins
; i
++) {
1088 const struct stm32_desc_pin
*pin
= pctl
->match_data
->pins
+ i
;
1089 struct stm32_pinctrl_group
*group
= pctl
->groups
+ i
;
1091 group
->name
= pin
->pin
.name
;
1092 group
->pin
= pin
->pin
.number
;
1094 pctl
->grp_names
[i
] = pin
->pin
.name
;
1100 int stm32_pctl_probe(struct platform_device
*pdev
)
1102 struct device_node
*np
= pdev
->dev
.of_node
;
1103 struct device_node
*child
;
1104 const struct of_device_id
*match
;
1105 struct device
*dev
= &pdev
->dev
;
1106 struct stm32_pinctrl
*pctl
;
1107 struct pinctrl_pin_desc
*pins
;
1108 int i
, ret
, banks
= 0;
1113 match
= of_match_device(dev
->driver
->of_match_table
, dev
);
1114 if (!match
|| !match
->data
)
1117 if (!of_find_property(np
, "pins-are-numbered", NULL
)) {
1118 dev_err(dev
, "only support pins-are-numbered format\n");
1122 pctl
= devm_kzalloc(dev
, sizeof(*pctl
), GFP_KERNEL
);
1126 platform_set_drvdata(pdev
, pctl
);
1129 pctl
->match_data
= match
->data
;
1130 ret
= stm32_pctrl_build_state(pdev
);
1132 dev_err(dev
, "build state failed: %d\n", ret
);
1136 if (of_find_property(np
, "interrupt-parent", NULL
)) {
1137 ret
= stm32_pctrl_dt_setup_irq(pdev
, pctl
);
1142 pins
= devm_kcalloc(&pdev
->dev
, pctl
->match_data
->npins
, sizeof(*pins
),
1147 for (i
= 0; i
< pctl
->match_data
->npins
; i
++)
1148 pins
[i
] = pctl
->match_data
->pins
[i
].pin
;
1150 pctl
->pctl_desc
.name
= dev_name(&pdev
->dev
);
1151 pctl
->pctl_desc
.owner
= THIS_MODULE
;
1152 pctl
->pctl_desc
.pins
= pins
;
1153 pctl
->pctl_desc
.npins
= pctl
->match_data
->npins
;
1154 pctl
->pctl_desc
.confops
= &stm32_pconf_ops
;
1155 pctl
->pctl_desc
.pctlops
= &stm32_pctrl_ops
;
1156 pctl
->pctl_desc
.pmxops
= &stm32_pmx_ops
;
1157 pctl
->dev
= &pdev
->dev
;
1159 pctl
->pctl_dev
= devm_pinctrl_register(&pdev
->dev
, &pctl
->pctl_desc
,
1162 if (IS_ERR(pctl
->pctl_dev
)) {
1163 dev_err(&pdev
->dev
, "Failed pinctrl registration\n");
1164 return PTR_ERR(pctl
->pctl_dev
);
1167 for_each_child_of_node(np
, child
)
1168 if (of_property_read_bool(child
, "gpio-controller"))
1172 dev_err(dev
, "at least one GPIO bank is required\n");
1175 pctl
->banks
= devm_kcalloc(dev
, banks
, sizeof(*pctl
->banks
),
1180 for_each_child_of_node(np
, child
) {
1181 if (of_property_read_bool(child
, "gpio-controller")) {
1182 ret
= stm32_gpiolib_register_bank(pctl
, child
);
1190 dev_info(dev
, "Pinctrl STM32 initialized\n");