Skip to content

Commit 1f16638

Browse files
committed
drivers: pinctrl: Support pinctrl with GD32VF103
Change the settings to support pinctrl on the GD32VF103. - Split soc/arm/gigadevice/common/pinctrl_soc.h and put it into include/dt-bindings. - Leave some definitions that can't handle with device tree compiler in pinctrl_soc.h. - Remove dependency to SOC_FAMILY_GD32 because always enabled it if GD32_HAS_AF(IO)_PINMAX was selected. Signed-off-by: TOKITA Hiroshi <[email protected]>
1 parent 5b7937a commit 1f16638

File tree

6 files changed

+281
-177
lines changed

6 files changed

+281
-177
lines changed

drivers/pinctrl/Kconfig.gd32

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ DT_COMPAT_GIGADEVICE_GD32_PINCTRL_AFIO := gd,gd32-pinctrl-afio
66

77
config PINCTRL_GD32_AF
88
bool "GD32 AF pin controller driver"
9-
depends on SOC_FAMILY_GD32 && GD32_HAS_AF_PINMUX
9+
depends on GD32_HAS_AF_PINMUX
1010
default $(dt_compat_enabled,$(DT_COMPAT_GIGADEVICE_GD32_PINCTRL_AF))
1111
help
1212
GD32 AF pin controller driver. This driver is used by series using the
1313
AF pin multiplexing model.
1414

1515
config PINCTRL_GD32_AFIO
1616
bool "GD32 AFIO pin controller driver"
17-
depends on SOC_FAMILY_GD32 && GD32_HAS_AFIO_PINMUX
17+
depends on GD32_HAS_AFIO_PINMUX
1818
default $(dt_compat_enabled,$(DT_COMPAT_GIGADEVICE_GD32_PINCTRL_AFIO))
1919
help
2020
GD32 AFIO pin controller driver. This driver is used by series using the

dts/riscv/gigadevice/gd32vf103.dtsi

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,72 @@
5353
write-block-size = <2>;
5454
};
5555
};
56+
57+
afio: afio@40010000 {
58+
compatible = "gd,gd32-afio";
59+
reg = <0x40010000 0x400>;
60+
rcu-periph-clock = <0x600>;
61+
status = "okay";
62+
label = "AFIO";
63+
};
64+
65+
pinctrl: pin-controller@40010800 {
66+
compatible = "gd,gd32-pinctrl-afio";
67+
reg = <0x40010800 0x1c00>;
68+
#address-cells = <1>;
69+
#size-cells = <1>;
70+
status = "okay";
71+
label = "PINCTRL";
72+
73+
gpioa: gpio@40010800 {
74+
compatible = "gd,gd32-gpio";
75+
reg = <0x40010800 0x400>;
76+
gpio-controller;
77+
#gpio-cells = <2>;
78+
rcu-periph-clock = <0x602>;
79+
status = "disabled";
80+
label = "GPIOA";
81+
};
82+
83+
gpiob: gpio@40010c00 {
84+
compatible = "gd,gd32-gpio";
85+
reg = <0x40010c00 0x400>;
86+
gpio-controller;
87+
#gpio-cells = <2>;
88+
rcu-periph-clock = <0x603>;
89+
status = "disabled";
90+
label = "GPIOB";
91+
};
92+
93+
gpioc: gpio@40011000 {
94+
compatible = "gd,gd32-gpio";
95+
reg = <0x40011000 0x400>;
96+
gpio-controller;
97+
#gpio-cells = <2>;
98+
rcu-periph-clock = <0x604>;
99+
status = "disabled";
100+
label = "GPIOC";
101+
};
102+
103+
gpiod: gpio@40011400 {
104+
compatible = "gd,gd32-gpio";
105+
reg = <0x40011400 0x400>;
106+
gpio-controller;
107+
#gpio-cells = <2>;
108+
rcu-periph-clock = <0x605>;
109+
status = "disabled";
110+
label = "GPIOD";
111+
};
112+
113+
gpioe: gpio@40011800 {
114+
compatible = "gd,gd32-gpio";
115+
reg = <0x40011800 0x400>;
116+
gpio-controller;
117+
#gpio-cells = <2>;
118+
rcu-periph-clock = <0x606>;
119+
status = "disabled";
120+
label = "GPIOE";
121+
};
122+
};
56123
};
57124
};
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
/*
2+
* Copyright (c) 2021 Teslabs Engineering S.L.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/**
8+
* @file
9+
* Gigadevice SoC specific helpers for pinctrl driver
10+
*/
11+
12+
#ifndef ZEPHYR_INCLUDE_DRIVERS_PINCTRL_PINCTRL_SOC_GD32_COMMON_H_
13+
#define ZEPHYR_INCLUDE_DRIVERS_PINCTRL_PINCTRL_SOC_GD32_COMMON_H_
14+
15+
#include <devicetree.h>
16+
#include <zephyr/types.h>
17+
18+
#ifdef CONFIG_PINCTRL_GD32_AF
19+
#include <dt-bindings/pinctrl/gd32-af.h>
20+
#else
21+
#include <dt-bindings/pinctrl/gd32-afio.h>
22+
#endif /* CONFIG_PINCTRL_GD32_AF */
23+
24+
#ifdef __cplusplus
25+
extern "C" {
26+
#endif
27+
28+
/** @cond INTERNAL_HIDDEN */
29+
30+
/** @brief Type for GD32 pin.
31+
*
32+
* Bits (AF model):
33+
* - 0-12: GD32_PINMUX_AF bit field.
34+
* - 13-25: Reserved.
35+
* - 26-31: Pin configuration bit field (@ref GD32_PINCFG).
36+
*
37+
* Bits (AFIO model):
38+
* - 0-19: GD32_PINMUX_AFIO bit field.
39+
* - 20-25: Reserved.
40+
* - 26-31: Pin configuration bit field (@ref GD32_PINCFG).
41+
*/
42+
typedef uint32_t pinctrl_soc_pin_t;
43+
44+
/**
45+
* @brief Utility macro to initialize each pin.
46+
*
47+
* @param node_id Node identifier.
48+
* @param prop Property name.
49+
* @param idx Property entry index.
50+
*/
51+
#define Z_PINCTRL_STATE_PIN_INIT(node_id, prop, idx) \
52+
(DT_PROP_BY_IDX(node_id, prop, idx) | \
53+
((GD32_PUPD_PULLUP * DT_PROP(node_id, bias_pull_up)) \
54+
<< GD32_PUPD_POS) | \
55+
((GD32_PUPD_PULLDOWN * DT_PROP(node_id, bias_pull_down)) \
56+
<< GD32_PUPD_POS) | \
57+
((GD32_OTYPE_OD * DT_PROP(node_id, drive_open_drain)) \
58+
<< GD32_OTYPE_POS) | \
59+
(DT_ENUM_IDX(node_id, slew_rate) << GD32_OSPEED_POS)),
60+
61+
/**
62+
* @brief Utility macro to initialize state pins contained in a given property.
63+
*
64+
* @param node_id Node identifier.
65+
* @param prop Property name describing state pins.
66+
*/
67+
#define Z_PINCTRL_STATE_PINS_INIT(node_id, prop) \
68+
{DT_FOREACH_CHILD_VARGS(DT_PHANDLE(node_id, prop), \
69+
DT_FOREACH_PROP_ELEM, pinmux, \
70+
Z_PINCTRL_STATE_PIN_INIT)}
71+
72+
/** @endcond */
73+
74+
/**
75+
* @name GD32 PUPD (values match the ones in the HAL for AF model).
76+
* @{
77+
*/
78+
79+
/** No pull-up/down */
80+
#define GD32_PUPD_NONE 0U
81+
/** Pull-up */
82+
#define GD32_PUPD_PULLUP 1U
83+
/** Pull-down */
84+
#define GD32_PUPD_PULLDOWN 2U
85+
86+
/** @} */
87+
88+
/**
89+
* @name GD32 OTYPE (values match the ones in the HAL for AF model).
90+
* @{
91+
*/
92+
93+
/** Push-pull */
94+
#define GD32_OTYPE_PP 0U
95+
/** Open-drain */
96+
#define GD32_OTYPE_OD 1U
97+
98+
/** @} */
99+
100+
/**
101+
* @name GD32 OSPEED (values match the ones in the HAL for AF model, mode minus
102+
* one for AFIO model).
103+
* @{
104+
*/
105+
106+
#ifdef CONFIG_PINCTRL_GD32_AF
107+
/** Maximum 2MHz */
108+
#define GD32_OSPEED_2MHZ 0U
109+
#ifdef CONFIG_SOC_SERIES_GD32F3X0
110+
/** Maximum 10MHz */
111+
#define GD32_OSPEED_10MHZ 1U
112+
/** Maximum 50MHz */
113+
#define GD32_OSPEED_50MHZ 3U
114+
#else
115+
/** Maximum 25MHz */
116+
#define GD32_OSPEED_25MHZ 1U
117+
/** Maximum 50MHz */
118+
#define GD32_OSPEED_50MHZ 2U
119+
/** Maximum 200MHz */
120+
#define GD32_OSPEED_200MHZ 3U
121+
#endif /* CONFIG_SOC_SERIES_GD32F3X0 */
122+
#else
123+
/** Maximum 10MHz */
124+
#define GD32_OSPEED_10MHZ 0U
125+
/** Maximum 2MHz */
126+
#define GD32_OSPEED_2MHZ 1U
127+
/** Maximum 50MHz */
128+
#define GD32_OSPEED_50MHZ 2U
129+
/** Maximum speed */
130+
#define GD32_OSPEED_MAX 3U
131+
#endif /* CONFIG_PINCTRL_GD32_AF */
132+
133+
/** @} */
134+
135+
/**
136+
* @name GD32 pin configuration bit field mask and positions.
137+
* @anchor GD32_PINCFG
138+
*
139+
* Fields:
140+
*
141+
* - 31..29: Pull-up/down
142+
* - 28: Output type
143+
* - 27..26: Output speed
144+
*
145+
* @{
146+
*/
147+
148+
/** PUPD field mask. */
149+
#define GD32_PUPD_MSK 0x3U
150+
/** PUPD field position. */
151+
#define GD32_PUPD_POS 29U
152+
/** OTYPE field mask. */
153+
#define GD32_OTYPE_MSK 0x1U
154+
/** OTYPE field position. */
155+
#define GD32_OTYPE_POS 28U
156+
/** OSPEED field mask. */
157+
#define GD32_OSPEED_MSK 0x3U
158+
/** OSPEED field position. */
159+
#define GD32_OSPEED_POS 26U
160+
161+
/** @} */
162+
163+
/**
164+
* Obtain PUPD field from pinctrl_soc_pin_t configuration.
165+
*
166+
* @param pincfg pinctrl_soc_pin_t bit field value.
167+
*/
168+
#define GD32_PUPD_GET(pincfg) \
169+
(((pincfg) >> GD32_PUPD_POS) & GD32_PUPD_MSK)
170+
171+
/**
172+
* Obtain OTYPE field from pinctrl_soc_pin_t configuration.
173+
*
174+
* @param pincfg pinctrl_soc_pin_t bit field value.
175+
*/
176+
#define GD32_OTYPE_GET(pincfg) \
177+
(((pincfg) >> GD32_OTYPE_POS) & GD32_OTYPE_MSK)
178+
179+
/**
180+
* Obtain OSPEED field from pinctrl_soc_pin_t configuration.
181+
*
182+
* @param pincfg pinctrl_soc_pin_t bit field value.
183+
*/
184+
#define GD32_OSPEED_GET(pincfg) \
185+
(((pincfg) >> GD32_OSPEED_POS) & GD32_OSPEED_MSK)
186+
187+
#ifdef __cplusplus
188+
}
189+
#endif
190+
191+
#endif /* ZEPHYR_INCLUDE_DRIVERS_PINCTRL_PINCTRL_SOC_GD32_COMMON_H_ */

0 commit comments

Comments
 (0)