Skip to content

Commit 2ab7d43

Browse files
nzmichaelhDhiru Kholia
authored andcommitted
drivers: add the gpio driver for wch ch32v003
This commit adds the gpio driver for WCH CH32V003. Signed-off-by: Michael Hope <[email protected]> Signed-off-by: Dhiru Kholia <[email protected]>
1 parent bb64ab5 commit 2ab7d43

File tree

6 files changed

+188
-0
lines changed

6 files changed

+188
-0
lines changed

drivers/gpio/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ zephyr_library_sources_ifdef(CONFIG_GPIO_TCA6424A gpio_tca6424a.c)
9494
zephyr_library_sources_ifdef(CONFIG_GPIO_TELINK_B91 gpio_b91.c)
9595
zephyr_library_sources_ifdef(CONFIG_GPIO_TEST gpio_test.c)
9696
zephyr_library_sources_ifdef(CONFIG_GPIO_TLE9104 gpio_tle9104.c)
97+
zephyr_library_sources_ifdef(CONFIG_GPIO_WCH_GPIO wch_gpio_ch32v00x.c)
9798
zephyr_library_sources_ifdef(CONFIG_GPIO_XEC gpio_mchp_xec.c)
9899
zephyr_library_sources_ifdef(CONFIG_GPIO_XEC_V2 gpio_mchp_xec_v2.c)
99100
zephyr_library_sources_ifdef(CONFIG_GPIO_XLNX_AXI gpio_xlnx_axi.c)

drivers/gpio/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ source "drivers/gpio/Kconfig.sx1509b"
178178
source "drivers/gpio/Kconfig.tca6424a"
179179
source "drivers/gpio/Kconfig.test"
180180
source "drivers/gpio/Kconfig.tle9104"
181+
source "drivers/gpio/Kconfig.wch_ch32v00x"
181182
source "drivers/gpio/Kconfig.xec"
182183
source "drivers/gpio/Kconfig.xlnx"
183184
source "drivers/gpio/Kconfig.xlnx_ps"

drivers/gpio/Kconfig.wch_ch32v00x

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright (c) 2024 Michael Hope
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
config GPIO_WCH_GPIO
5+
bool "WCH CH32V00x GPIO driver"
6+
depends on DT_HAS_WCH_GPIO_ENABLED
7+
default y

drivers/gpio/wch_gpio_ch32v00x.c

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* Copyright (c) 2024 Michael Hope
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/drivers/clock_control.h>
8+
#include <zephyr/drivers/gpio.h>
9+
#include <zephyr/drivers/gpio/gpio_utils.h>
10+
#include <zephyr/dt-bindings/gpio/gpio.h>
11+
#include <zephyr/irq.h>
12+
13+
#include <ch32_gpio.h>
14+
15+
#define DT_DRV_COMPAT wch_gpio
16+
17+
struct gpio_ch32v00x_config {
18+
struct gpio_driver_config common;
19+
GPIO_TypeDef *regs;
20+
const struct device *clock_dev;
21+
uint8_t clock_id;
22+
};
23+
24+
struct gpio_ch32v00x_data {
25+
struct gpio_driver_data common;
26+
};
27+
28+
static int gpio_ch32v00x_configure(const struct device *dev, gpio_pin_t pin, gpio_flags_t flags)
29+
{
30+
const struct gpio_ch32v00x_config *config = dev->config;
31+
GPIO_TypeDef *regs = config->regs;
32+
uint32_t cnf_mode;
33+
uint32_t bshr = 0;
34+
35+
if ((flags & GPIO_OUTPUT) != 0) {
36+
cnf_mode = 0x01;
37+
if ((flags & GPIO_OUTPUT_INIT_HIGH) != 0) {
38+
bshr = 1 << pin;
39+
} else if ((flags & GPIO_OUTPUT_INIT_LOW) != 0) {
40+
bshr = 1 << (16 + pin);
41+
}
42+
} else if ((flags & GPIO_INPUT) != 0) {
43+
if ((flags & GPIO_PULL_UP) != 0) {
44+
cnf_mode = 0x08;
45+
bshr = 1 << pin;
46+
} else if ((flags & GPIO_PULL_DOWN) != 0) {
47+
cnf_mode = 0x08;
48+
bshr = 1 << (16 + pin);
49+
} else {
50+
cnf_mode = 0x04;
51+
}
52+
} else {
53+
cnf_mode = 0x00;
54+
}
55+
56+
regs->CFGLR = (regs->CFGLR & ~(0x0F << (4 * pin))) | (cnf_mode << (4 * pin));
57+
regs->BSHR = bshr;
58+
59+
return 0;
60+
}
61+
62+
static int gpio_ch32v00x_port_get_raw(const struct device *dev, uint32_t *value)
63+
{
64+
const struct gpio_ch32v00x_config *config = dev->config;
65+
66+
*value = config->regs->INDR;
67+
68+
return 0;
69+
}
70+
71+
static int gpio_ch32v00x_port_set_masked_raw(const struct device *dev, uint32_t mask,
72+
uint32_t value)
73+
{
74+
const struct gpio_ch32v00x_config *config = dev->config;
75+
76+
config->regs->BSHR = ((~value & mask) << 16) | (value & mask);
77+
78+
return 0;
79+
}
80+
81+
static int gpio_ch32v00x_port_set_bits_raw(const struct device *dev, uint32_t pins)
82+
{
83+
const struct gpio_ch32v00x_config *config = dev->config;
84+
85+
config->regs->BSHR = pins;
86+
87+
return 0;
88+
}
89+
90+
static int gpio_ch32v00x_port_clear_bits_raw(const struct device *dev, uint32_t pins)
91+
{
92+
const struct gpio_ch32v00x_config *config = dev->config;
93+
94+
config->regs->BCR = pins;
95+
96+
return 0;
97+
}
98+
99+
static int gpio_ch32v00x_port_toggle_bits(const struct device *dev, uint32_t pins)
100+
{
101+
const struct gpio_ch32v00x_config *config = dev->config;
102+
uint32_t changed = (config->regs->OUTDR ^ pins) & pins;
103+
104+
config->regs->BSHR = (changed & pins) | (~changed & pins) << 16;
105+
106+
return 0;
107+
}
108+
109+
static const struct gpio_driver_api gpio_ch32v00x_driver_api = {
110+
.pin_configure = gpio_ch32v00x_configure,
111+
.port_get_raw = gpio_ch32v00x_port_get_raw,
112+
.port_set_masked_raw = gpio_ch32v00x_port_set_masked_raw,
113+
.port_set_bits_raw = gpio_ch32v00x_port_set_bits_raw,
114+
.port_clear_bits_raw = gpio_ch32v00x_port_clear_bits_raw,
115+
.port_toggle_bits = gpio_ch32v00x_port_toggle_bits,
116+
};
117+
118+
static int gpio_ch32v00x_init(const struct device *dev)
119+
{
120+
const struct gpio_ch32v00x_config *config = dev->config;
121+
122+
clock_control_on(config->clock_dev, (clock_control_subsys_t *)(uintptr_t)config->clock_id);
123+
124+
return 0;
125+
}
126+
127+
#define GPIO_CH32V00X_INIT(idx) \
128+
static const struct gpio_ch32v00x_config gpio_ch32v00x_##idx##_config = { \
129+
.common = \
130+
{ \
131+
.port_pin_mask = GPIO_PORT_PIN_MASK_FROM_DT_INST(idx), \
132+
}, \
133+
.regs = (GPIO_TypeDef *)DT_INST_REG_ADDR(idx), \
134+
.clock_dev = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(idx)), \
135+
.clock_id = DT_INST_CLOCKS_CELL(idx, id), \
136+
}; \
137+
\
138+
static struct gpio_ch32v00x_data gpio_ch32v00x_##idx##_data; \
139+
\
140+
DEVICE_DT_INST_DEFINE(idx, gpio_ch32v00x_init, NULL, &gpio_ch32v00x_##idx##_data, \
141+
&gpio_ch32v00x_##idx##_config, PRE_KERNEL_1, \
142+
CONFIG_GPIO_INIT_PRIORITY, &gpio_ch32v00x_driver_api);
143+
144+
DT_INST_FOREACH_STATUS_OKAY(GPIO_CH32V00X_INIT)

dts/bindings/gpio/wch,gpio.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright (c) 2024 Michael Hope
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: WCH CH32V00x General-Purpose Input/Output (GPIO)
5+
6+
compatible: "wch,gpio"
7+
8+
include: [gpio-controller.yaml, base.yaml]
9+
10+
properties:
11+
reg:
12+
required: true
13+
14+
"#gpio-cells":
15+
const: 2
16+
17+
gpio-cells:
18+
- pin
19+
- flags

modules/hal_wch/ch32_gpio.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright (c) 2024 Dhiru Kholia
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef _CH32_GPIO_H
8+
#define _CH32_GPIO_H
9+
10+
#ifdef CONFIG_SOC_CH32V003
11+
#include <ch32v003fun.h>
12+
#else
13+
#error "SoC not supported!"
14+
#endif
15+
16+
#endif

0 commit comments

Comments
 (0)