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>
11 #include <linux/hwspinlock.h>
13 #include <linux/irq.h>
14 #include <linux/mfd/syscon.h>
15 #include <linux/module.h>
17 #include <linux/of_address.h>
18 #include <linux/of_device.h>
19 #include <linux/of_irq.h>
20 #include <linux/pinctrl/consumer.h>
21 #include <linux/pinctrl/machine.h>
22 #include <linux/pinctrl/pinconf.h>
23 #include <linux/pinctrl/pinconf-generic.h>
24 #include <linux/pinctrl/pinctrl.h>
25 #include <linux/pinctrl/pinmux.h>
26 #include <linux/platform_device.h>
27 #include <linux/regmap.h>
28 #include <linux/reset.h>
29 #include <linux/slab.h>
32 #include "../pinconf.h"
33 #include "../pinctrl-utils.h"
34 #include "pinctrl-stm32.h"
36 #define STM32_GPIO_MODER 0x00
37 #define STM32_GPIO_TYPER 0x04
38 #define STM32_GPIO_SPEEDR 0x08
39 #define STM32_GPIO_PUPDR 0x0c
40 #define STM32_GPIO_IDR 0x10
41 #define STM32_GPIO_ODR 0x14
42 #define STM32_GPIO_BSRR 0x18
43 #define STM32_GPIO_LCKR 0x1c
44 #define STM32_GPIO_AFRL 0x20
45 #define STM32_GPIO_AFRH 0x24
47 /* custom bitfield to backup pin status */
48 #define STM32_GPIO_BKP_MODE_SHIFT 0
49 #define STM32_GPIO_BKP_MODE_MASK GENMASK(1, 0)
50 #define STM32_GPIO_BKP_ALT_SHIFT 2
51 #define STM32_GPIO_BKP_ALT_MASK GENMASK(5, 2)
52 #define STM32_GPIO_BKP_SPEED_SHIFT 6
53 #define STM32_GPIO_BKP_SPEED_MASK GENMASK(7, 6)
54 #define STM32_GPIO_BKP_PUPD_SHIFT 8
55 #define STM32_GPIO_BKP_PUPD_MASK GENMASK(9, 8)
56 #define STM32_GPIO_BKP_TYPE 10
57 #define STM32_GPIO_BKP_VAL 11
59 #define STM32_GPIO_PINS_PER_BANK 16
60 #define STM32_GPIO_IRQ_LINE 16
62 #define SYSCFG_IRQMUX_MASK GENMASK(3, 0)
64 #define gpio_range_to_bank(chip) \
65 container_of(chip, struct stm32_gpio_bank, range)
67 #define HWSPINLOCK_TIMEOUT 5 /* msec */
69 static const char * const stm32_gpio_functions
[] = {
74 "af11", "af12", "af13",
75 "af14", "af15", "analog",
78 struct stm32_pinctrl_group
{
84 struct stm32_gpio_bank
{
88 struct gpio_chip gpio_chip
;
89 struct pinctrl_gpio_range range
;
90 struct fwnode_handle
*fwnode
;
91 struct irq_domain
*domain
;
94 u32 pin_backup
[STM32_GPIO_PINS_PER_BANK
];
95 u8 irq_type
[STM32_GPIO_PINS_PER_BANK
];
98 struct stm32_pinctrl
{
100 struct pinctrl_dev
*pctl_dev
;
101 struct pinctrl_desc pctl_desc
;
102 struct stm32_pinctrl_group
*groups
;
104 const char **grp_names
;
105 struct stm32_gpio_bank
*banks
;
107 const struct stm32_pinctrl_match_data
*match_data
;
108 struct irq_domain
*domain
;
109 struct regmap
*regmap
;
110 struct regmap_field
*irqmux
[STM32_GPIO_PINS_PER_BANK
];
111 struct hwspinlock
*hwlock
;
112 struct stm32_desc_pin
*pins
;
116 spinlock_t irqmux_lock
;
119 static inline int stm32_gpio_pin(int gpio
)
121 return gpio
% STM32_GPIO_PINS_PER_BANK
;
124 static inline u32
stm32_gpio_get_mode(u32 function
)
129 case STM32_PIN_AF(0) ... STM32_PIN_AF(15):
131 case STM32_PIN_ANALOG
:
138 static inline u32
stm32_gpio_get_alt(u32 function
)
143 case STM32_PIN_AF(0) ... STM32_PIN_AF(15):
145 case STM32_PIN_ANALOG
:
152 static void stm32_gpio_backup_value(struct stm32_gpio_bank
*bank
,
153 u32 offset
, u32 value
)
155 bank
->pin_backup
[offset
] &= ~BIT(STM32_GPIO_BKP_VAL
);
156 bank
->pin_backup
[offset
] |= value
<< STM32_GPIO_BKP_VAL
;
159 static void stm32_gpio_backup_mode(struct stm32_gpio_bank
*bank
, u32 offset
,
162 bank
->pin_backup
[offset
] &= ~(STM32_GPIO_BKP_MODE_MASK
|
163 STM32_GPIO_BKP_ALT_MASK
);
164 bank
->pin_backup
[offset
] |= mode
<< STM32_GPIO_BKP_MODE_SHIFT
;
165 bank
->pin_backup
[offset
] |= alt
<< STM32_GPIO_BKP_ALT_SHIFT
;
168 static void stm32_gpio_backup_driving(struct stm32_gpio_bank
*bank
, u32 offset
,
171 bank
->pin_backup
[offset
] &= ~BIT(STM32_GPIO_BKP_TYPE
);
172 bank
->pin_backup
[offset
] |= drive
<< STM32_GPIO_BKP_TYPE
;
175 static void stm32_gpio_backup_speed(struct stm32_gpio_bank
*bank
, u32 offset
,
178 bank
->pin_backup
[offset
] &= ~STM32_GPIO_BKP_SPEED_MASK
;
179 bank
->pin_backup
[offset
] |= speed
<< STM32_GPIO_BKP_SPEED_SHIFT
;
182 static void stm32_gpio_backup_bias(struct stm32_gpio_bank
*bank
, u32 offset
,
185 bank
->pin_backup
[offset
] &= ~STM32_GPIO_BKP_PUPD_MASK
;
186 bank
->pin_backup
[offset
] |= bias
<< STM32_GPIO_BKP_PUPD_SHIFT
;
191 static inline void __stm32_gpio_set(struct stm32_gpio_bank
*bank
,
192 unsigned offset
, int value
)
194 stm32_gpio_backup_value(bank
, offset
, value
);
197 offset
+= STM32_GPIO_PINS_PER_BANK
;
199 clk_enable(bank
->clk
);
201 writel_relaxed(BIT(offset
), bank
->base
+ STM32_GPIO_BSRR
);
203 clk_disable(bank
->clk
);
206 static int stm32_gpio_request(struct gpio_chip
*chip
, unsigned offset
)
208 struct stm32_gpio_bank
*bank
= gpiochip_get_data(chip
);
209 struct stm32_pinctrl
*pctl
= dev_get_drvdata(bank
->gpio_chip
.parent
);
210 struct pinctrl_gpio_range
*range
;
211 int pin
= offset
+ (bank
->bank_nr
* STM32_GPIO_PINS_PER_BANK
);
213 range
= pinctrl_find_gpio_range_from_pin_nolock(pctl
->pctl_dev
, pin
);
215 dev_err(pctl
->dev
, "pin %d not in range.\n", pin
);
219 return pinctrl_gpio_request(chip
->base
+ offset
);
222 static void stm32_gpio_free(struct gpio_chip
*chip
, unsigned offset
)
224 pinctrl_gpio_free(chip
->base
+ offset
);
227 static int stm32_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
229 struct stm32_gpio_bank
*bank
= gpiochip_get_data(chip
);
232 clk_enable(bank
->clk
);
234 ret
= !!(readl_relaxed(bank
->base
+ STM32_GPIO_IDR
) & BIT(offset
));
236 clk_disable(bank
->clk
);
241 static void stm32_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
243 struct stm32_gpio_bank
*bank
= gpiochip_get_data(chip
);
245 __stm32_gpio_set(bank
, offset
, value
);
248 static int stm32_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
250 return pinctrl_gpio_direction_input(chip
->base
+ offset
);
253 static int stm32_gpio_direction_output(struct gpio_chip
*chip
,
254 unsigned offset
, int value
)
256 struct stm32_gpio_bank
*bank
= gpiochip_get_data(chip
);
258 __stm32_gpio_set(bank
, offset
, value
);
259 pinctrl_gpio_direction_output(chip
->base
+ offset
);
265 static int stm32_gpio_to_irq(struct gpio_chip
*chip
, unsigned int offset
)
267 struct stm32_gpio_bank
*bank
= gpiochip_get_data(chip
);
268 struct irq_fwspec fwspec
;
270 fwspec
.fwnode
= bank
->fwnode
;
271 fwspec
.param_count
= 2;
272 fwspec
.param
[0] = offset
;
273 fwspec
.param
[1] = IRQ_TYPE_NONE
;
275 return irq_create_fwspec_mapping(&fwspec
);
278 static int stm32_gpio_get_direction(struct gpio_chip
*chip
, unsigned int offset
)
280 struct stm32_gpio_bank
*bank
= gpiochip_get_data(chip
);
281 int pin
= stm32_gpio_pin(offset
);
285 stm32_pmx_get_mode(bank
, pin
, &mode
, &alt
);
286 if ((alt
== 0) && (mode
== 0))
287 ret
= GPIO_LINE_DIRECTION_IN
;
288 else if ((alt
== 0) && (mode
== 1))
289 ret
= GPIO_LINE_DIRECTION_OUT
;
296 static const struct gpio_chip stm32_gpio_template
= {
297 .request
= stm32_gpio_request
,
298 .free
= stm32_gpio_free
,
299 .get
= stm32_gpio_get
,
300 .set
= stm32_gpio_set
,
301 .direction_input
= stm32_gpio_direction_input
,
302 .direction_output
= stm32_gpio_direction_output
,
303 .to_irq
= stm32_gpio_to_irq
,
304 .get_direction
= stm32_gpio_get_direction
,
307 static void stm32_gpio_irq_trigger(struct irq_data
*d
)
309 struct stm32_gpio_bank
*bank
= d
->domain
->host_data
;
312 /* If level interrupt type then retrig */
313 level
= stm32_gpio_get(&bank
->gpio_chip
, d
->hwirq
);
314 if ((level
== 0 && bank
->irq_type
[d
->hwirq
] == IRQ_TYPE_LEVEL_LOW
) ||
315 (level
== 1 && bank
->irq_type
[d
->hwirq
] == IRQ_TYPE_LEVEL_HIGH
))
316 irq_chip_retrigger_hierarchy(d
);
319 static void stm32_gpio_irq_eoi(struct irq_data
*d
)
321 irq_chip_eoi_parent(d
);
322 stm32_gpio_irq_trigger(d
);
325 static int stm32_gpio_set_type(struct irq_data
*d
, unsigned int type
)
327 struct stm32_gpio_bank
*bank
= d
->domain
->host_data
;
331 case IRQ_TYPE_EDGE_RISING
:
332 case IRQ_TYPE_EDGE_FALLING
:
333 case IRQ_TYPE_EDGE_BOTH
:
336 case IRQ_TYPE_LEVEL_HIGH
:
337 parent_type
= IRQ_TYPE_EDGE_RISING
;
339 case IRQ_TYPE_LEVEL_LOW
:
340 parent_type
= IRQ_TYPE_EDGE_FALLING
;
346 bank
->irq_type
[d
->hwirq
] = type
;
348 return irq_chip_set_type_parent(d
, parent_type
);
351 static int stm32_gpio_irq_request_resources(struct irq_data
*irq_data
)
353 struct stm32_gpio_bank
*bank
= irq_data
->domain
->host_data
;
354 struct stm32_pinctrl
*pctl
= dev_get_drvdata(bank
->gpio_chip
.parent
);
357 ret
= stm32_gpio_direction_input(&bank
->gpio_chip
, irq_data
->hwirq
);
361 ret
= gpiochip_lock_as_irq(&bank
->gpio_chip
, irq_data
->hwirq
);
363 dev_err(pctl
->dev
, "unable to lock HW IRQ %lu for IRQ\n",
371 static void stm32_gpio_irq_release_resources(struct irq_data
*irq_data
)
373 struct stm32_gpio_bank
*bank
= irq_data
->domain
->host_data
;
375 gpiochip_unlock_as_irq(&bank
->gpio_chip
, irq_data
->hwirq
);
378 static void stm32_gpio_irq_unmask(struct irq_data
*d
)
380 irq_chip_unmask_parent(d
);
381 stm32_gpio_irq_trigger(d
);
384 static struct irq_chip stm32_gpio_irq_chip
= {
386 .irq_eoi
= stm32_gpio_irq_eoi
,
387 .irq_ack
= irq_chip_ack_parent
,
388 .irq_mask
= irq_chip_mask_parent
,
389 .irq_unmask
= stm32_gpio_irq_unmask
,
390 .irq_set_type
= stm32_gpio_set_type
,
391 .irq_set_wake
= irq_chip_set_wake_parent
,
392 .irq_request_resources
= stm32_gpio_irq_request_resources
,
393 .irq_release_resources
= stm32_gpio_irq_release_resources
,
396 static int stm32_gpio_domain_translate(struct irq_domain
*d
,
397 struct irq_fwspec
*fwspec
,
398 unsigned long *hwirq
,
401 if ((fwspec
->param_count
!= 2) ||
402 (fwspec
->param
[0] >= STM32_GPIO_IRQ_LINE
))
405 *hwirq
= fwspec
->param
[0];
406 *type
= fwspec
->param
[1];
410 static int stm32_gpio_domain_activate(struct irq_domain
*d
,
411 struct irq_data
*irq_data
, bool reserve
)
413 struct stm32_gpio_bank
*bank
= d
->host_data
;
414 struct stm32_pinctrl
*pctl
= dev_get_drvdata(bank
->gpio_chip
.parent
);
419 * gpio irq mux is shared between several banks, a lock has to be done
420 * to avoid overriding.
422 spin_lock_irqsave(&pctl
->irqmux_lock
, flags
);
424 ret
= hwspin_lock_timeout(pctl
->hwlock
, HWSPINLOCK_TIMEOUT
);
427 dev_err(pctl
->dev
, "Can't get hwspinlock\n");
431 if (pctl
->irqmux_map
& BIT(irq_data
->hwirq
)) {
432 dev_err(pctl
->dev
, "irq line %ld already requested.\n",
436 hwspin_unlock(pctl
->hwlock
);
439 pctl
->irqmux_map
|= BIT(irq_data
->hwirq
);
442 regmap_field_write(pctl
->irqmux
[irq_data
->hwirq
], bank
->bank_ioport_nr
);
445 hwspin_unlock(pctl
->hwlock
);
448 spin_unlock_irqrestore(&pctl
->irqmux_lock
, flags
);
452 static void stm32_gpio_domain_deactivate(struct irq_domain
*d
,
453 struct irq_data
*irq_data
)
455 struct stm32_gpio_bank
*bank
= d
->host_data
;
456 struct stm32_pinctrl
*pctl
= dev_get_drvdata(bank
->gpio_chip
.parent
);
459 spin_lock_irqsave(&pctl
->irqmux_lock
, flags
);
460 pctl
->irqmux_map
&= ~BIT(irq_data
->hwirq
);
461 spin_unlock_irqrestore(&pctl
->irqmux_lock
, flags
);
464 static int stm32_gpio_domain_alloc(struct irq_domain
*d
,
466 unsigned int nr_irqs
, void *data
)
468 struct stm32_gpio_bank
*bank
= d
->host_data
;
469 struct irq_fwspec
*fwspec
= data
;
470 struct irq_fwspec parent_fwspec
;
471 irq_hw_number_t hwirq
;
473 hwirq
= fwspec
->param
[0];
474 parent_fwspec
.fwnode
= d
->parent
->fwnode
;
475 parent_fwspec
.param_count
= 2;
476 parent_fwspec
.param
[0] = fwspec
->param
[0];
477 parent_fwspec
.param
[1] = fwspec
->param
[1];
479 irq_domain_set_hwirq_and_chip(d
, virq
, hwirq
, &stm32_gpio_irq_chip
,
482 return irq_domain_alloc_irqs_parent(d
, virq
, nr_irqs
, &parent_fwspec
);
485 static const struct irq_domain_ops stm32_gpio_domain_ops
= {
486 .translate
= stm32_gpio_domain_translate
,
487 .alloc
= stm32_gpio_domain_alloc
,
488 .free
= irq_domain_free_irqs_common
,
489 .activate
= stm32_gpio_domain_activate
,
490 .deactivate
= stm32_gpio_domain_deactivate
,
493 /* Pinctrl functions */
494 static struct stm32_pinctrl_group
*
495 stm32_pctrl_find_group_by_pin(struct stm32_pinctrl
*pctl
, u32 pin
)
499 for (i
= 0; i
< pctl
->ngroups
; i
++) {
500 struct stm32_pinctrl_group
*grp
= pctl
->groups
+ i
;
509 static bool stm32_pctrl_is_function_valid(struct stm32_pinctrl
*pctl
,
510 u32 pin_num
, u32 fnum
)
514 for (i
= 0; i
< pctl
->npins
; i
++) {
515 const struct stm32_desc_pin
*pin
= pctl
->pins
+ i
;
516 const struct stm32_desc_function
*func
= pin
->functions
;
518 if (pin
->pin
.number
!= pin_num
)
521 while (func
&& func
->name
) {
522 if (func
->num
== fnum
)
533 static int stm32_pctrl_dt_node_to_map_func(struct stm32_pinctrl
*pctl
,
534 u32 pin
, u32 fnum
, struct stm32_pinctrl_group
*grp
,
535 struct pinctrl_map
**map
, unsigned *reserved_maps
,
538 if (*num_maps
== *reserved_maps
)
541 (*map
)[*num_maps
].type
= PIN_MAP_TYPE_MUX_GROUP
;
542 (*map
)[*num_maps
].data
.mux
.group
= grp
->name
;
544 if (!stm32_pctrl_is_function_valid(pctl
, pin
, fnum
)) {
545 dev_err(pctl
->dev
, "invalid function %d on pin %d .\n",
550 (*map
)[*num_maps
].data
.mux
.function
= stm32_gpio_functions
[fnum
];
556 static int stm32_pctrl_dt_subnode_to_map(struct pinctrl_dev
*pctldev
,
557 struct device_node
*node
,
558 struct pinctrl_map
**map
,
559 unsigned *reserved_maps
,
562 struct stm32_pinctrl
*pctl
;
563 struct stm32_pinctrl_group
*grp
;
564 struct property
*pins
;
565 u32 pinfunc
, pin
, func
;
566 unsigned long *configs
;
567 unsigned int num_configs
;
569 unsigned reserve
= 0;
570 int num_pins
, num_funcs
, maps_per_pin
, i
, err
= 0;
572 pctl
= pinctrl_dev_get_drvdata(pctldev
);
574 pins
= of_find_property(node
, "pinmux", NULL
);
576 dev_err(pctl
->dev
, "missing pins property in node %pOFn .\n",
581 err
= pinconf_generic_parse_dt_config(node
, pctldev
, &configs
,
589 num_pins
= pins
->length
/ sizeof(u32
);
590 num_funcs
= num_pins
;
594 if (has_config
&& num_pins
>= 1)
597 if (!num_pins
|| !maps_per_pin
) {
602 reserve
= num_pins
* maps_per_pin
;
604 err
= pinctrl_utils_reserve_map(pctldev
, map
,
605 reserved_maps
, num_maps
, reserve
);
609 for (i
= 0; i
< num_pins
; i
++) {
610 err
= of_property_read_u32_index(node
, "pinmux",
615 pin
= STM32_GET_PIN_NO(pinfunc
);
616 func
= STM32_GET_PIN_FUNC(pinfunc
);
618 if (!stm32_pctrl_is_function_valid(pctl
, pin
, func
)) {
619 dev_err(pctl
->dev
, "invalid function.\n");
624 grp
= stm32_pctrl_find_group_by_pin(pctl
, pin
);
626 dev_err(pctl
->dev
, "unable to match pin %d to group\n",
632 err
= stm32_pctrl_dt_node_to_map_func(pctl
, pin
, func
, grp
, map
,
633 reserved_maps
, num_maps
);
638 err
= pinctrl_utils_add_map_configs(pctldev
, map
,
639 reserved_maps
, num_maps
, grp
->name
,
640 configs
, num_configs
,
641 PIN_MAP_TYPE_CONFIGS_GROUP
);
652 static int stm32_pctrl_dt_node_to_map(struct pinctrl_dev
*pctldev
,
653 struct device_node
*np_config
,
654 struct pinctrl_map
**map
, unsigned *num_maps
)
656 struct device_node
*np
;
657 unsigned reserved_maps
;
664 for_each_child_of_node(np_config
, np
) {
665 ret
= stm32_pctrl_dt_subnode_to_map(pctldev
, np
, map
,
666 &reserved_maps
, num_maps
);
668 pinctrl_utils_free_map(pctldev
, *map
, *num_maps
);
677 static int stm32_pctrl_get_groups_count(struct pinctrl_dev
*pctldev
)
679 struct stm32_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
681 return pctl
->ngroups
;
684 static const char *stm32_pctrl_get_group_name(struct pinctrl_dev
*pctldev
,
687 struct stm32_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
689 return pctl
->groups
[group
].name
;
692 static int stm32_pctrl_get_group_pins(struct pinctrl_dev
*pctldev
,
694 const unsigned **pins
,
697 struct stm32_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
699 *pins
= (unsigned *)&pctl
->groups
[group
].pin
;
705 static const struct pinctrl_ops stm32_pctrl_ops
= {
706 .dt_node_to_map
= stm32_pctrl_dt_node_to_map
,
707 .dt_free_map
= pinctrl_utils_free_map
,
708 .get_groups_count
= stm32_pctrl_get_groups_count
,
709 .get_group_name
= stm32_pctrl_get_group_name
,
710 .get_group_pins
= stm32_pctrl_get_group_pins
,
714 /* Pinmux functions */
716 static int stm32_pmx_get_funcs_cnt(struct pinctrl_dev
*pctldev
)
718 return ARRAY_SIZE(stm32_gpio_functions
);
721 static const char *stm32_pmx_get_func_name(struct pinctrl_dev
*pctldev
,
724 return stm32_gpio_functions
[selector
];
727 static int stm32_pmx_get_func_groups(struct pinctrl_dev
*pctldev
,
729 const char * const **groups
,
730 unsigned * const num_groups
)
732 struct stm32_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
734 *groups
= pctl
->grp_names
;
735 *num_groups
= pctl
->ngroups
;
740 static int stm32_pmx_set_mode(struct stm32_gpio_bank
*bank
,
741 int pin
, u32 mode
, u32 alt
)
743 struct stm32_pinctrl
*pctl
= dev_get_drvdata(bank
->gpio_chip
.parent
);
745 int alt_shift
= (pin
% 8) * 4;
746 int alt_offset
= STM32_GPIO_AFRL
+ (pin
/ 8) * 4;
750 clk_enable(bank
->clk
);
751 spin_lock_irqsave(&bank
->lock
, flags
);
754 err
= hwspin_lock_timeout(pctl
->hwlock
, HWSPINLOCK_TIMEOUT
);
757 dev_err(pctl
->dev
, "Can't get hwspinlock\n");
761 val
= readl_relaxed(bank
->base
+ alt_offset
);
762 val
&= ~GENMASK(alt_shift
+ 3, alt_shift
);
763 val
|= (alt
<< alt_shift
);
764 writel_relaxed(val
, bank
->base
+ alt_offset
);
766 val
= readl_relaxed(bank
->base
+ STM32_GPIO_MODER
);
767 val
&= ~GENMASK(pin
* 2 + 1, pin
* 2);
768 val
|= mode
<< (pin
* 2);
769 writel_relaxed(val
, bank
->base
+ STM32_GPIO_MODER
);
772 hwspin_unlock(pctl
->hwlock
);
774 stm32_gpio_backup_mode(bank
, pin
, mode
, alt
);
777 spin_unlock_irqrestore(&bank
->lock
, flags
);
778 clk_disable(bank
->clk
);
783 void stm32_pmx_get_mode(struct stm32_gpio_bank
*bank
, int pin
, u32
*mode
,
787 int alt_shift
= (pin
% 8) * 4;
788 int alt_offset
= STM32_GPIO_AFRL
+ (pin
/ 8) * 4;
791 clk_enable(bank
->clk
);
792 spin_lock_irqsave(&bank
->lock
, flags
);
794 val
= readl_relaxed(bank
->base
+ alt_offset
);
795 val
&= GENMASK(alt_shift
+ 3, alt_shift
);
796 *alt
= val
>> alt_shift
;
798 val
= readl_relaxed(bank
->base
+ STM32_GPIO_MODER
);
799 val
&= GENMASK(pin
* 2 + 1, pin
* 2);
800 *mode
= val
>> (pin
* 2);
802 spin_unlock_irqrestore(&bank
->lock
, flags
);
803 clk_disable(bank
->clk
);
806 static int stm32_pmx_set_mux(struct pinctrl_dev
*pctldev
,
811 struct stm32_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
812 struct stm32_pinctrl_group
*g
= pctl
->groups
+ group
;
813 struct pinctrl_gpio_range
*range
;
814 struct stm32_gpio_bank
*bank
;
818 ret
= stm32_pctrl_is_function_valid(pctl
, g
->pin
, function
);
820 dev_err(pctl
->dev
, "invalid function %d on group %d .\n",
825 range
= pinctrl_find_gpio_range_from_pin(pctldev
, g
->pin
);
827 dev_err(pctl
->dev
, "No gpio range defined.\n");
831 bank
= gpiochip_get_data(range
->gc
);
832 pin
= stm32_gpio_pin(g
->pin
);
834 mode
= stm32_gpio_get_mode(function
);
835 alt
= stm32_gpio_get_alt(function
);
837 return stm32_pmx_set_mode(bank
, pin
, mode
, alt
);
840 static int stm32_pmx_gpio_set_direction(struct pinctrl_dev
*pctldev
,
841 struct pinctrl_gpio_range
*range
, unsigned gpio
,
844 struct stm32_gpio_bank
*bank
= gpiochip_get_data(range
->gc
);
845 int pin
= stm32_gpio_pin(gpio
);
847 return stm32_pmx_set_mode(bank
, pin
, !input
, 0);
850 static const struct pinmux_ops stm32_pmx_ops
= {
851 .get_functions_count
= stm32_pmx_get_funcs_cnt
,
852 .get_function_name
= stm32_pmx_get_func_name
,
853 .get_function_groups
= stm32_pmx_get_func_groups
,
854 .set_mux
= stm32_pmx_set_mux
,
855 .gpio_set_direction
= stm32_pmx_gpio_set_direction
,
859 /* Pinconf functions */
861 static int stm32_pconf_set_driving(struct stm32_gpio_bank
*bank
,
862 unsigned offset
, u32 drive
)
864 struct stm32_pinctrl
*pctl
= dev_get_drvdata(bank
->gpio_chip
.parent
);
869 clk_enable(bank
->clk
);
870 spin_lock_irqsave(&bank
->lock
, flags
);
873 err
= hwspin_lock_timeout(pctl
->hwlock
, HWSPINLOCK_TIMEOUT
);
876 dev_err(pctl
->dev
, "Can't get hwspinlock\n");
880 val
= readl_relaxed(bank
->base
+ STM32_GPIO_TYPER
);
882 val
|= drive
<< offset
;
883 writel_relaxed(val
, bank
->base
+ STM32_GPIO_TYPER
);
886 hwspin_unlock(pctl
->hwlock
);
888 stm32_gpio_backup_driving(bank
, offset
, drive
);
891 spin_unlock_irqrestore(&bank
->lock
, flags
);
892 clk_disable(bank
->clk
);
897 static u32
stm32_pconf_get_driving(struct stm32_gpio_bank
*bank
,
903 clk_enable(bank
->clk
);
904 spin_lock_irqsave(&bank
->lock
, flags
);
906 val
= readl_relaxed(bank
->base
+ STM32_GPIO_TYPER
);
909 spin_unlock_irqrestore(&bank
->lock
, flags
);
910 clk_disable(bank
->clk
);
912 return (val
>> offset
);
915 static int stm32_pconf_set_speed(struct stm32_gpio_bank
*bank
,
916 unsigned offset
, u32 speed
)
918 struct stm32_pinctrl
*pctl
= dev_get_drvdata(bank
->gpio_chip
.parent
);
923 clk_enable(bank
->clk
);
924 spin_lock_irqsave(&bank
->lock
, flags
);
927 err
= hwspin_lock_timeout(pctl
->hwlock
, HWSPINLOCK_TIMEOUT
);
930 dev_err(pctl
->dev
, "Can't get hwspinlock\n");
934 val
= readl_relaxed(bank
->base
+ STM32_GPIO_SPEEDR
);
935 val
&= ~GENMASK(offset
* 2 + 1, offset
* 2);
936 val
|= speed
<< (offset
* 2);
937 writel_relaxed(val
, bank
->base
+ STM32_GPIO_SPEEDR
);
940 hwspin_unlock(pctl
->hwlock
);
942 stm32_gpio_backup_speed(bank
, offset
, speed
);
945 spin_unlock_irqrestore(&bank
->lock
, flags
);
946 clk_disable(bank
->clk
);
951 static u32
stm32_pconf_get_speed(struct stm32_gpio_bank
*bank
,
957 clk_enable(bank
->clk
);
958 spin_lock_irqsave(&bank
->lock
, flags
);
960 val
= readl_relaxed(bank
->base
+ STM32_GPIO_SPEEDR
);
961 val
&= GENMASK(offset
* 2 + 1, offset
* 2);
963 spin_unlock_irqrestore(&bank
->lock
, flags
);
964 clk_disable(bank
->clk
);
966 return (val
>> (offset
* 2));
969 static int stm32_pconf_set_bias(struct stm32_gpio_bank
*bank
,
970 unsigned offset
, u32 bias
)
972 struct stm32_pinctrl
*pctl
= dev_get_drvdata(bank
->gpio_chip
.parent
);
977 clk_enable(bank
->clk
);
978 spin_lock_irqsave(&bank
->lock
, flags
);
981 err
= hwspin_lock_timeout(pctl
->hwlock
, HWSPINLOCK_TIMEOUT
);
984 dev_err(pctl
->dev
, "Can't get hwspinlock\n");
988 val
= readl_relaxed(bank
->base
+ STM32_GPIO_PUPDR
);
989 val
&= ~GENMASK(offset
* 2 + 1, offset
* 2);
990 val
|= bias
<< (offset
* 2);
991 writel_relaxed(val
, bank
->base
+ STM32_GPIO_PUPDR
);
994 hwspin_unlock(pctl
->hwlock
);
996 stm32_gpio_backup_bias(bank
, offset
, bias
);
999 spin_unlock_irqrestore(&bank
->lock
, flags
);
1000 clk_disable(bank
->clk
);
1005 static u32
stm32_pconf_get_bias(struct stm32_gpio_bank
*bank
,
1006 unsigned int offset
)
1008 unsigned long flags
;
1011 clk_enable(bank
->clk
);
1012 spin_lock_irqsave(&bank
->lock
, flags
);
1014 val
= readl_relaxed(bank
->base
+ STM32_GPIO_PUPDR
);
1015 val
&= GENMASK(offset
* 2 + 1, offset
* 2);
1017 spin_unlock_irqrestore(&bank
->lock
, flags
);
1018 clk_disable(bank
->clk
);
1020 return (val
>> (offset
* 2));
1023 static bool stm32_pconf_get(struct stm32_gpio_bank
*bank
,
1024 unsigned int offset
, bool dir
)
1026 unsigned long flags
;
1029 clk_enable(bank
->clk
);
1030 spin_lock_irqsave(&bank
->lock
, flags
);
1033 val
= !!(readl_relaxed(bank
->base
+ STM32_GPIO_IDR
) &
1036 val
= !!(readl_relaxed(bank
->base
+ STM32_GPIO_ODR
) &
1039 spin_unlock_irqrestore(&bank
->lock
, flags
);
1040 clk_disable(bank
->clk
);
1045 static int stm32_pconf_parse_conf(struct pinctrl_dev
*pctldev
,
1046 unsigned int pin
, enum pin_config_param param
,
1047 enum pin_config_param arg
)
1049 struct stm32_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
1050 struct pinctrl_gpio_range
*range
;
1051 struct stm32_gpio_bank
*bank
;
1052 int offset
, ret
= 0;
1054 range
= pinctrl_find_gpio_range_from_pin(pctldev
, pin
);
1056 dev_err(pctl
->dev
, "No gpio range defined.\n");
1060 bank
= gpiochip_get_data(range
->gc
);
1061 offset
= stm32_gpio_pin(pin
);
1064 case PIN_CONFIG_DRIVE_PUSH_PULL
:
1065 ret
= stm32_pconf_set_driving(bank
, offset
, 0);
1067 case PIN_CONFIG_DRIVE_OPEN_DRAIN
:
1068 ret
= stm32_pconf_set_driving(bank
, offset
, 1);
1070 case PIN_CONFIG_SLEW_RATE
:
1071 ret
= stm32_pconf_set_speed(bank
, offset
, arg
);
1073 case PIN_CONFIG_BIAS_DISABLE
:
1074 ret
= stm32_pconf_set_bias(bank
, offset
, 0);
1076 case PIN_CONFIG_BIAS_PULL_UP
:
1077 ret
= stm32_pconf_set_bias(bank
, offset
, 1);
1079 case PIN_CONFIG_BIAS_PULL_DOWN
:
1080 ret
= stm32_pconf_set_bias(bank
, offset
, 2);
1082 case PIN_CONFIG_OUTPUT
:
1083 __stm32_gpio_set(bank
, offset
, arg
);
1084 ret
= stm32_pmx_gpio_set_direction(pctldev
, range
, pin
, false);
1093 static int stm32_pconf_group_get(struct pinctrl_dev
*pctldev
,
1095 unsigned long *config
)
1097 struct stm32_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
1099 *config
= pctl
->groups
[group
].config
;
1104 static int stm32_pconf_group_set(struct pinctrl_dev
*pctldev
, unsigned group
,
1105 unsigned long *configs
, unsigned num_configs
)
1107 struct stm32_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
1108 struct stm32_pinctrl_group
*g
= &pctl
->groups
[group
];
1111 for (i
= 0; i
< num_configs
; i
++) {
1112 ret
= stm32_pconf_parse_conf(pctldev
, g
->pin
,
1113 pinconf_to_config_param(configs
[i
]),
1114 pinconf_to_config_argument(configs
[i
]));
1118 g
->config
= configs
[i
];
1124 static void stm32_pconf_dbg_show(struct pinctrl_dev
*pctldev
,
1128 struct pinctrl_gpio_range
*range
;
1129 struct stm32_gpio_bank
*bank
;
1131 u32 mode
, alt
, drive
, speed
, bias
;
1132 static const char * const modes
[] = {
1133 "input", "output", "alternate", "analog" };
1134 static const char * const speeds
[] = {
1135 "low", "medium", "high", "very high" };
1136 static const char * const biasing
[] = {
1137 "floating", "pull up", "pull down", "" };
1140 range
= pinctrl_find_gpio_range_from_pin_nolock(pctldev
, pin
);
1144 bank
= gpiochip_get_data(range
->gc
);
1145 offset
= stm32_gpio_pin(pin
);
1147 stm32_pmx_get_mode(bank
, offset
, &mode
, &alt
);
1148 bias
= stm32_pconf_get_bias(bank
, offset
);
1150 seq_printf(s
, "%s ", modes
[mode
]);
1155 val
= stm32_pconf_get(bank
, offset
, true);
1156 seq_printf(s
, "- %s - %s",
1157 val
? "high" : "low",
1163 drive
= stm32_pconf_get_driving(bank
, offset
);
1164 speed
= stm32_pconf_get_speed(bank
, offset
);
1165 val
= stm32_pconf_get(bank
, offset
, false);
1166 seq_printf(s
, "- %s - %s - %s - %s %s",
1167 val
? "high" : "low",
1168 drive
? "open drain" : "push pull",
1170 speeds
[speed
], "speed");
1175 drive
= stm32_pconf_get_driving(bank
, offset
);
1176 speed
= stm32_pconf_get_speed(bank
, offset
);
1177 seq_printf(s
, "%d - %s - %s - %s %s", alt
,
1178 drive
? "open drain" : "push pull",
1180 speeds
[speed
], "speed");
1190 static const struct pinconf_ops stm32_pconf_ops
= {
1191 .pin_config_group_get
= stm32_pconf_group_get
,
1192 .pin_config_group_set
= stm32_pconf_group_set
,
1193 .pin_config_dbg_show
= stm32_pconf_dbg_show
,
1196 static int stm32_gpiolib_register_bank(struct stm32_pinctrl
*pctl
,
1197 struct device_node
*np
)
1199 struct stm32_gpio_bank
*bank
= &pctl
->banks
[pctl
->nbanks
];
1201 struct pinctrl_gpio_range
*range
= &bank
->range
;
1202 struct of_phandle_args args
;
1203 struct device
*dev
= pctl
->dev
;
1204 struct resource res
;
1205 struct reset_control
*rstc
;
1206 int npins
= STM32_GPIO_PINS_PER_BANK
;
1209 rstc
= of_reset_control_get_exclusive(np
, NULL
);
1211 reset_control_deassert(rstc
);
1213 if (of_address_to_resource(np
, 0, &res
))
1216 bank
->base
= devm_ioremap_resource(dev
, &res
);
1217 if (IS_ERR(bank
->base
))
1218 return PTR_ERR(bank
->base
);
1220 bank
->clk
= of_clk_get_by_name(np
, NULL
);
1221 if (IS_ERR(bank
->clk
)) {
1222 dev_err(dev
, "failed to get clk (%ld)\n", PTR_ERR(bank
->clk
));
1223 return PTR_ERR(bank
->clk
);
1226 err
= clk_prepare(bank
->clk
);
1228 dev_err(dev
, "failed to prepare clk (%d)\n", err
);
1232 bank
->gpio_chip
= stm32_gpio_template
;
1234 of_property_read_string(np
, "st,bank-name", &bank
->gpio_chip
.label
);
1236 if (!of_parse_phandle_with_fixed_args(np
, "gpio-ranges", 3, 0, &args
)) {
1237 bank_nr
= args
.args
[1] / STM32_GPIO_PINS_PER_BANK
;
1238 bank
->gpio_chip
.base
= args
.args
[1];
1240 bank_nr
= pctl
->nbanks
;
1241 bank
->gpio_chip
.base
= bank_nr
* STM32_GPIO_PINS_PER_BANK
;
1242 range
->name
= bank
->gpio_chip
.label
;
1243 range
->id
= bank_nr
;
1244 range
->pin_base
= range
->id
* STM32_GPIO_PINS_PER_BANK
;
1245 range
->base
= range
->id
* STM32_GPIO_PINS_PER_BANK
;
1246 range
->npins
= npins
;
1247 range
->gc
= &bank
->gpio_chip
;
1248 pinctrl_add_gpio_range(pctl
->pctl_dev
,
1249 &pctl
->banks
[bank_nr
].range
);
1252 if (of_property_read_u32(np
, "st,bank-ioport", &bank_ioport_nr
))
1253 bank_ioport_nr
= bank_nr
;
1255 bank
->gpio_chip
.base
= bank_nr
* STM32_GPIO_PINS_PER_BANK
;
1257 bank
->gpio_chip
.ngpio
= npins
;
1258 bank
->gpio_chip
.of_node
= np
;
1259 bank
->gpio_chip
.parent
= dev
;
1260 bank
->bank_nr
= bank_nr
;
1261 bank
->bank_ioport_nr
= bank_ioport_nr
;
1262 spin_lock_init(&bank
->lock
);
1264 /* create irq hierarchical domain */
1265 bank
->fwnode
= of_node_to_fwnode(np
);
1267 bank
->domain
= irq_domain_create_hierarchy(pctl
->domain
, 0,
1268 STM32_GPIO_IRQ_LINE
, bank
->fwnode
,
1269 &stm32_gpio_domain_ops
, bank
);
1274 err
= gpiochip_add_data(&bank
->gpio_chip
, bank
);
1276 dev_err(dev
, "Failed to add gpiochip(%d)!\n", bank_nr
);
1280 dev_info(dev
, "%s bank added\n", bank
->gpio_chip
.label
);
1284 static struct irq_domain
*stm32_pctrl_get_irq_domain(struct device_node
*np
)
1286 struct device_node
*parent
;
1287 struct irq_domain
*domain
;
1289 if (!of_find_property(np
, "interrupt-parent", NULL
))
1292 parent
= of_irq_find_parent(np
);
1294 return ERR_PTR(-ENXIO
);
1296 domain
= irq_find_host(parent
);
1298 /* domain not registered yet */
1299 return ERR_PTR(-EPROBE_DEFER
);
1304 static int stm32_pctrl_dt_setup_irq(struct platform_device
*pdev
,
1305 struct stm32_pinctrl
*pctl
)
1307 struct device_node
*np
= pdev
->dev
.of_node
;
1308 struct device
*dev
= &pdev
->dev
;
1311 int mask
, mask_width
;
1313 pctl
->regmap
= syscon_regmap_lookup_by_phandle(np
, "st,syscfg");
1314 if (IS_ERR(pctl
->regmap
))
1315 return PTR_ERR(pctl
->regmap
);
1319 ret
= of_property_read_u32_index(np
, "st,syscfg", 1, &offset
);
1323 ret
= of_property_read_u32_index(np
, "st,syscfg", 2, &mask
);
1325 mask
= SYSCFG_IRQMUX_MASK
;
1327 mask_width
= fls(mask
);
1329 for (i
= 0; i
< STM32_GPIO_PINS_PER_BANK
; i
++) {
1330 struct reg_field mux
;
1332 mux
.reg
= offset
+ (i
/ 4) * 4;
1333 mux
.lsb
= (i
% 4) * mask_width
;
1334 mux
.msb
= mux
.lsb
+ mask_width
- 1;
1336 dev_dbg(dev
, "irqmux%d: reg:%#x, lsb:%d, msb:%d\n",
1337 i
, mux
.reg
, mux
.lsb
, mux
.msb
);
1339 pctl
->irqmux
[i
] = devm_regmap_field_alloc(dev
, rm
, mux
);
1340 if (IS_ERR(pctl
->irqmux
[i
]))
1341 return PTR_ERR(pctl
->irqmux
[i
]);
1347 static int stm32_pctrl_build_state(struct platform_device
*pdev
)
1349 struct stm32_pinctrl
*pctl
= platform_get_drvdata(pdev
);
1352 pctl
->ngroups
= pctl
->npins
;
1354 /* Allocate groups */
1355 pctl
->groups
= devm_kcalloc(&pdev
->dev
, pctl
->ngroups
,
1356 sizeof(*pctl
->groups
), GFP_KERNEL
);
1360 /* We assume that one pin is one group, use pin name as group name. */
1361 pctl
->grp_names
= devm_kcalloc(&pdev
->dev
, pctl
->ngroups
,
1362 sizeof(*pctl
->grp_names
), GFP_KERNEL
);
1363 if (!pctl
->grp_names
)
1366 for (i
= 0; i
< pctl
->npins
; i
++) {
1367 const struct stm32_desc_pin
*pin
= pctl
->pins
+ i
;
1368 struct stm32_pinctrl_group
*group
= pctl
->groups
+ i
;
1370 group
->name
= pin
->pin
.name
;
1371 group
->pin
= pin
->pin
.number
;
1372 pctl
->grp_names
[i
] = pin
->pin
.name
;
1378 static int stm32_pctrl_create_pins_tab(struct stm32_pinctrl
*pctl
,
1379 struct stm32_desc_pin
*pins
)
1381 const struct stm32_desc_pin
*p
;
1382 int i
, nb_pins_available
= 0;
1384 for (i
= 0; i
< pctl
->match_data
->npins
; i
++) {
1385 p
= pctl
->match_data
->pins
+ i
;
1386 if (pctl
->pkg
&& !(pctl
->pkg
& p
->pkg
))
1389 pins
->functions
= p
->functions
;
1391 nb_pins_available
++;
1394 pctl
->npins
= nb_pins_available
;
1399 static void stm32_pctl_get_package(struct device_node
*np
,
1400 struct stm32_pinctrl
*pctl
)
1402 if (of_property_read_u32(np
, "st,package", &pctl
->pkg
)) {
1404 dev_warn(pctl
->dev
, "No package detected, use default one\n");
1406 dev_dbg(pctl
->dev
, "package detected: %x\n", pctl
->pkg
);
1410 int stm32_pctl_probe(struct platform_device
*pdev
)
1412 struct device_node
*np
= pdev
->dev
.of_node
;
1413 struct device_node
*child
;
1414 const struct of_device_id
*match
;
1415 struct device
*dev
= &pdev
->dev
;
1416 struct stm32_pinctrl
*pctl
;
1417 struct pinctrl_pin_desc
*pins
;
1418 int i
, ret
, hwlock_id
, banks
= 0;
1423 match
= of_match_device(dev
->driver
->of_match_table
, dev
);
1424 if (!match
|| !match
->data
)
1427 if (!of_find_property(np
, "pins-are-numbered", NULL
)) {
1428 dev_err(dev
, "only support pins-are-numbered format\n");
1432 pctl
= devm_kzalloc(dev
, sizeof(*pctl
), GFP_KERNEL
);
1436 platform_set_drvdata(pdev
, pctl
);
1438 /* check for IRQ controller (may require deferred probe) */
1439 pctl
->domain
= stm32_pctrl_get_irq_domain(np
);
1440 if (IS_ERR(pctl
->domain
))
1441 return PTR_ERR(pctl
->domain
);
1443 /* hwspinlock is optional */
1444 hwlock_id
= of_hwspin_lock_get_id(pdev
->dev
.of_node
, 0);
1445 if (hwlock_id
< 0) {
1446 if (hwlock_id
== -EPROBE_DEFER
)
1449 pctl
->hwlock
= hwspin_lock_request_specific(hwlock_id
);
1452 spin_lock_init(&pctl
->irqmux_lock
);
1455 pctl
->match_data
= match
->data
;
1457 /* get package information */
1458 stm32_pctl_get_package(np
, pctl
);
1460 pctl
->pins
= devm_kcalloc(pctl
->dev
, pctl
->match_data
->npins
,
1461 sizeof(*pctl
->pins
), GFP_KERNEL
);
1465 ret
= stm32_pctrl_create_pins_tab(pctl
, pctl
->pins
);
1469 ret
= stm32_pctrl_build_state(pdev
);
1471 dev_err(dev
, "build state failed: %d\n", ret
);
1476 ret
= stm32_pctrl_dt_setup_irq(pdev
, pctl
);
1481 pins
= devm_kcalloc(&pdev
->dev
, pctl
->npins
, sizeof(*pins
),
1486 for (i
= 0; i
< pctl
->npins
; i
++)
1487 pins
[i
] = pctl
->pins
[i
].pin
;
1489 pctl
->pctl_desc
.name
= dev_name(&pdev
->dev
);
1490 pctl
->pctl_desc
.owner
= THIS_MODULE
;
1491 pctl
->pctl_desc
.pins
= pins
;
1492 pctl
->pctl_desc
.npins
= pctl
->npins
;
1493 pctl
->pctl_desc
.link_consumers
= true;
1494 pctl
->pctl_desc
.confops
= &stm32_pconf_ops
;
1495 pctl
->pctl_desc
.pctlops
= &stm32_pctrl_ops
;
1496 pctl
->pctl_desc
.pmxops
= &stm32_pmx_ops
;
1497 pctl
->dev
= &pdev
->dev
;
1499 pctl
->pctl_dev
= devm_pinctrl_register(&pdev
->dev
, &pctl
->pctl_desc
,
1502 if (IS_ERR(pctl
->pctl_dev
)) {
1503 dev_err(&pdev
->dev
, "Failed pinctrl registration\n");
1504 return PTR_ERR(pctl
->pctl_dev
);
1507 for_each_available_child_of_node(np
, child
)
1508 if (of_property_read_bool(child
, "gpio-controller"))
1512 dev_err(dev
, "at least one GPIO bank is required\n");
1515 pctl
->banks
= devm_kcalloc(dev
, banks
, sizeof(*pctl
->banks
),
1520 for_each_available_child_of_node(np
, child
) {
1521 if (of_property_read_bool(child
, "gpio-controller")) {
1522 ret
= stm32_gpiolib_register_bank(pctl
, child
);
1532 dev_info(dev
, "Pinctrl STM32 initialized\n");
1537 static int __maybe_unused
stm32_pinctrl_restore_gpio_regs(
1538 struct stm32_pinctrl
*pctl
, u32 pin
)
1540 const struct pin_desc
*desc
= pin_desc_get(pctl
->pctl_dev
, pin
);
1541 u32 val
, alt
, mode
, offset
= stm32_gpio_pin(pin
);
1542 struct pinctrl_gpio_range
*range
;
1543 struct stm32_gpio_bank
*bank
;
1547 range
= pinctrl_find_gpio_range_from_pin(pctl
->pctl_dev
, pin
);
1551 pin_is_irq
= gpiochip_line_is_irq(range
->gc
, offset
);
1553 if (!desc
|| (!pin_is_irq
&& !desc
->gpio_owner
))
1556 bank
= gpiochip_get_data(range
->gc
);
1558 alt
= bank
->pin_backup
[offset
] & STM32_GPIO_BKP_ALT_MASK
;
1559 alt
>>= STM32_GPIO_BKP_ALT_SHIFT
;
1560 mode
= bank
->pin_backup
[offset
] & STM32_GPIO_BKP_MODE_MASK
;
1561 mode
>>= STM32_GPIO_BKP_MODE_SHIFT
;
1563 ret
= stm32_pmx_set_mode(bank
, offset
, mode
, alt
);
1568 val
= bank
->pin_backup
[offset
] & BIT(STM32_GPIO_BKP_VAL
);
1569 val
= val
>> STM32_GPIO_BKP_VAL
;
1570 __stm32_gpio_set(bank
, offset
, val
);
1573 val
= bank
->pin_backup
[offset
] & BIT(STM32_GPIO_BKP_TYPE
);
1574 val
>>= STM32_GPIO_BKP_TYPE
;
1575 ret
= stm32_pconf_set_driving(bank
, offset
, val
);
1579 val
= bank
->pin_backup
[offset
] & STM32_GPIO_BKP_SPEED_MASK
;
1580 val
>>= STM32_GPIO_BKP_SPEED_SHIFT
;
1581 ret
= stm32_pconf_set_speed(bank
, offset
, val
);
1585 val
= bank
->pin_backup
[offset
] & STM32_GPIO_BKP_PUPD_MASK
;
1586 val
>>= STM32_GPIO_BKP_PUPD_SHIFT
;
1587 ret
= stm32_pconf_set_bias(bank
, offset
, val
);
1592 regmap_field_write(pctl
->irqmux
[offset
], bank
->bank_ioport_nr
);
1597 int __maybe_unused
stm32_pinctrl_resume(struct device
*dev
)
1599 struct stm32_pinctrl
*pctl
= dev_get_drvdata(dev
);
1600 struct stm32_pinctrl_group
*g
= pctl
->groups
;
1603 for (i
= g
->pin
; i
< g
->pin
+ pctl
->ngroups
; i
++)
1604 stm32_pinctrl_restore_gpio_regs(pctl
, i
);