|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Renesas Electronics Corporation |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#define DT_DRV_COMPAT renesas_ra8_gpio |
| 8 | + |
| 9 | +#include <zephyr/drivers/gpio.h> |
| 10 | +#include <zephyr/drivers/pinctrl.h> |
| 11 | +#include <zephyr/drivers/gpio/gpio_utils.h> |
| 12 | +#include <zephyr/irq.h> |
| 13 | +#include <soc.h> |
| 14 | + |
| 15 | +#define PRCR_KEY 0xA500U |
| 16 | +#define VBATT_PORT 4 |
| 17 | + |
| 18 | +static const gpio_pin_t vbatt_pins[] = { |
| 19 | + 2U, |
| 20 | + 3U, |
| 21 | + 4U, |
| 22 | +}; |
| 23 | + |
| 24 | +#define RA8_INVALID_PORT_ADDR 0UL |
| 25 | + |
| 26 | +#define RA8_GPIO_PORT_ADDR(nodelabel) \ |
| 27 | + COND_CODE_1(DT_NODE_EXISTS(DT_NODELABEL(nodelabel)), \ |
| 28 | + (DT_REG_ADDR(DT_NODELABEL(nodelabel))), (RA8_INVALID_PORT_ADDR)) |
| 29 | + |
| 30 | +struct gpio_ra8_config { |
| 31 | + struct gpio_driver_config common; |
| 32 | + uint8_t port_num; |
| 33 | + R_PORT0_Type *port; |
| 34 | +}; |
| 35 | + |
| 36 | +struct gpio_ra8_data { |
| 37 | + struct gpio_driver_data common; |
| 38 | +}; |
| 39 | + |
| 40 | +static int gpio_ra8_pin_configure(const struct device *dev, gpio_pin_t pin, gpio_flags_t flags) |
| 41 | +{ |
| 42 | + const struct gpio_ra8_config *config = dev->config; |
| 43 | + |
| 44 | + if (((flags & GPIO_INPUT) != 0U) && ((flags & GPIO_OUTPUT) != 0U)) { |
| 45 | + return -ENOTSUP; |
| 46 | + } |
| 47 | + |
| 48 | + if ((flags & GPIO_PULL_DOWN) != 0U) { |
| 49 | + return -ENOTSUP; |
| 50 | + } |
| 51 | + |
| 52 | + if (config->port_num == VBATT_PORT) { |
| 53 | + uint32_t clear = 0; |
| 54 | + |
| 55 | + for (int i = 0; i < ARRAY_SIZE(vbatt_pins); i++) { |
| 56 | + if (vbatt_pins[i] == pin) { |
| 57 | + WRITE_BIT(clear, i, 1); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + R_SYSTEM->PRCR = ((R_SYSTEM->PRCR | PRCR_KEY) | R_SYSTEM_PRCR_PRC1_Msk); |
| 62 | + |
| 63 | + R_SYSTEM->VBTICTLR &= (uint8_t)~clear; |
| 64 | + |
| 65 | + R_SYSTEM->PRCR = ((R_SYSTEM->PRCR | PRCR_KEY) & (uint16_t)~R_SYSTEM_PRCR_PRC1_Msk); |
| 66 | + } |
| 67 | + |
| 68 | +#if BSP_CFG_MCU_PART_SERIES == 8 |
| 69 | + R_PMISC->PWPRS = 0; |
| 70 | + R_PMISC->PWPRS = BIT(R_PMISC_PWPR_PFSWE_Pos); |
| 71 | +#else |
| 72 | + R_PMISC->PWPR = 0; |
| 73 | + R_PMISC->PWPR = BIT(R_PMISC_PWPR_PFSWE_Pos); |
| 74 | +#endif |
| 75 | + |
| 76 | + uint32_t value = R_PFS->PORT[config->port_num].PIN[pin].PmnPFS; |
| 77 | + |
| 78 | + /* Change mode to general IO mode and disable IRQ and Analog input */ |
| 79 | + WRITE_BIT(value, R_PFS_PORT_PIN_PmnPFS_PMR_Pos, 0); |
| 80 | + WRITE_BIT(value, R_PFS_PORT_PIN_PmnPFS_ASEL_Pos, 0); |
| 81 | + WRITE_BIT(value, R_PFS_PORT_PIN_PmnPFS_ISEL_Pos, 0); |
| 82 | + |
| 83 | + if ((flags & GPIO_OUTPUT) != 0U) { |
| 84 | + /* Set output pin initial value */ |
| 85 | + if ((flags & GPIO_OUTPUT_INIT_LOW) != 0U) { |
| 86 | + WRITE_BIT(value, R_PFS_PORT_PIN_PmnPFS_PODR_Pos, 0); |
| 87 | + } else if ((flags & GPIO_OUTPUT_INIT_HIGH) != 0U) { |
| 88 | + WRITE_BIT(value, R_PFS_PORT_PIN_PmnPFS_PODR_Pos, 1); |
| 89 | + } |
| 90 | + |
| 91 | + WRITE_BIT(value, R_PFS_PORT_PIN_PmnPFS_PDR_Pos, 1); |
| 92 | + } else { |
| 93 | + WRITE_BIT(value, R_PFS_PORT_PIN_PmnPFS_PDR_Pos, 0); |
| 94 | + } |
| 95 | + |
| 96 | + R_PFS->PORT[config->port_num].PIN[pin].PmnPFS = value; |
| 97 | + |
| 98 | +#if BSP_CFG_MCU_PART_SERIES == 8 |
| 99 | + R_PMISC->PWPRS = 0; |
| 100 | + R_PMISC->PWPRS = BIT(R_PMISC_PWPR_B0WI_Pos); |
| 101 | +#else |
| 102 | + R_PMISC->PWPR = 0; |
| 103 | + R_PMISC->PWPR = BIT(R_PMISC_PWPR_B0WI_Pos); |
| 104 | +#endif |
| 105 | + |
| 106 | + return 0; |
| 107 | +} |
| 108 | + |
| 109 | +static int gpio_ra8_port_get_raw(const struct device *dev, uint32_t *value) |
| 110 | +{ |
| 111 | + const struct gpio_ra8_config *config = dev->config; |
| 112 | + R_PORT0_Type *port = config->port; |
| 113 | + |
| 114 | + *value = port->PIDR; |
| 115 | + |
| 116 | + return 0; |
| 117 | +} |
| 118 | + |
| 119 | +static int gpio_ra8_port_set_masked_raw(const struct device *dev, gpio_port_pins_t mask, |
| 120 | + gpio_port_value_t value) |
| 121 | +{ |
| 122 | + const struct gpio_ra8_config *config = dev->config; |
| 123 | + R_PORT0_Type *port = config->port; |
| 124 | + |
| 125 | + port->PODR = ((port->PODR & ~mask) | (value & mask)); |
| 126 | + |
| 127 | + return 0; |
| 128 | +} |
| 129 | + |
| 130 | +static int gpio_ra8_port_set_bits_raw(const struct device *dev, gpio_port_pins_t pins) |
| 131 | +{ |
| 132 | + const struct gpio_ra8_config *config = dev->config; |
| 133 | + R_PORT0_Type *port = config->port; |
| 134 | + |
| 135 | + port->PODR = (port->PODR | pins); |
| 136 | + |
| 137 | + return 0; |
| 138 | +} |
| 139 | + |
| 140 | +static int gpio_ra8_port_clear_bits_raw(const struct device *dev, gpio_port_pins_t pins) |
| 141 | +{ |
| 142 | + const struct gpio_ra8_config *config = dev->config; |
| 143 | + R_PORT0_Type *port = config->port; |
| 144 | + |
| 145 | + port->PODR = (port->PODR & ~pins); |
| 146 | + |
| 147 | + return 0; |
| 148 | +} |
| 149 | + |
| 150 | +static int gpio_ra8_port_toggle_bits(const struct device *dev, gpio_port_pins_t pins) |
| 151 | +{ |
| 152 | + const struct gpio_ra8_config *config = dev->config; |
| 153 | + R_PORT0_Type *port = config->port; |
| 154 | + |
| 155 | + port->PODR = (port->PODR ^ pins); |
| 156 | + |
| 157 | + return 0; |
| 158 | +} |
| 159 | + |
| 160 | +static const struct gpio_driver_api gpio_ra8_drv_api_funcs = { |
| 161 | + .pin_configure = gpio_ra8_pin_configure, |
| 162 | + .port_get_raw = gpio_ra8_port_get_raw, |
| 163 | + .port_set_masked_raw = gpio_ra8_port_set_masked_raw, |
| 164 | + .port_set_bits_raw = gpio_ra8_port_set_bits_raw, |
| 165 | + .port_clear_bits_raw = gpio_ra8_port_clear_bits_raw, |
| 166 | + .port_toggle_bits = gpio_ra8_port_toggle_bits, |
| 167 | + .pin_interrupt_configure = NULL, |
| 168 | + .manage_callback = NULL, |
| 169 | +#ifdef CONFIG_GPIO_GET_DIRECTION |
| 170 | + .port_get_direction = gpio_ra8_port_get_direction, |
| 171 | +#endif |
| 172 | +}; |
| 173 | + |
| 174 | +static int gpio_ra8_init(const struct device *dev) |
| 175 | +{ |
| 176 | + return 0; |
| 177 | +} |
| 178 | + |
| 179 | +#define GPIO_DEVICE_INIT(__node, __port_num, __suffix, __addr) \ |
| 180 | + static const struct gpio_ra8_config gpio_ra8_config_##__suffix = { \ |
| 181 | + .common = \ |
| 182 | + { \ |
| 183 | + .port_pin_mask = GPIO_PORT_PIN_MASK_FROM_NGPIOS(16U), \ |
| 184 | + }, \ |
| 185 | + .port_num = __port_num, \ |
| 186 | + .port = (R_PORT0_Type *)__addr, \ |
| 187 | + }; \ |
| 188 | + static struct gpio_ra8_data gpio_ra8_data_##__suffix; \ |
| 189 | + DEVICE_DT_DEFINE(__node, gpio_ra8_init, PM_DEVICE_DT_GET(__node), \ |
| 190 | + &gpio_ra8_data_##__suffix, &gpio_ra8_config_##__suffix, PRE_KERNEL_1, \ |
| 191 | + CONFIG_GPIO_INIT_PRIORITY, &gpio_ra8_drv_api_funcs) |
| 192 | + |
| 193 | +#define GPIO_DEVICE_INIT_RA8(__suffix) \ |
| 194 | + GPIO_DEVICE_INIT(DT_NODELABEL(gpio##__suffix), \ |
| 195 | + DT_PROP(DT_NODELABEL(gpio##__suffix), port), __suffix, \ |
| 196 | + DT_REG_ADDR(DT_NODELABEL(gpio##__suffix))) |
| 197 | + |
| 198 | +#if DT_NODE_HAS_STATUS(DT_NODELABEL(gpio0), okay) |
| 199 | +GPIO_DEVICE_INIT_RA8(0); |
| 200 | +#endif /* DT_NODE_HAS_STATUS(DT_NODELABEL(gpio0), okay) */ |
| 201 | + |
| 202 | +#if DT_NODE_HAS_STATUS(DT_NODELABEL(gpio1), okay) |
| 203 | +GPIO_DEVICE_INIT_RA8(1); |
| 204 | +#endif /* DT_NODE_HAS_STATUS(DT_NODELABEL(gpio1), okay) */ |
| 205 | + |
| 206 | +#if DT_NODE_HAS_STATUS(DT_NODELABEL(gpio2), okay) |
| 207 | +GPIO_DEVICE_INIT_RA8(2); |
| 208 | +#endif /* DT_NODE_HAS_STATUS(DT_NODELABEL(gpio2), okay) */ |
| 209 | + |
| 210 | +#if DT_NODE_HAS_STATUS(DT_NODELABEL(gpio3), okay) |
| 211 | +GPIO_DEVICE_INIT_RA8(3); |
| 212 | +#endif /* DT_NODE_HAS_STATUS(DT_NODELABEL(gpio3), okay) */ |
| 213 | + |
| 214 | +#if DT_NODE_HAS_STATUS(DT_NODELABEL(gpio4), okay) |
| 215 | +GPIO_DEVICE_INIT_RA8(4); |
| 216 | +#endif /* DT_NODE_HAS_STATUS(DT_NODELABEL(gpio4), okay) */ |
| 217 | + |
| 218 | +#if DT_NODE_HAS_STATUS(DT_NODELABEL(gpio5), okay) |
| 219 | +GPIO_DEVICE_INIT_RA8(5); |
| 220 | +#endif /* DT_NODE_HAS_STATUS(DT_NODELABEL(gpio5), okay) */ |
| 221 | + |
| 222 | +#if DT_NODE_HAS_STATUS(DT_NODELABEL(gpio6), okay) |
| 223 | +GPIO_DEVICE_INIT_RA8(6); |
| 224 | +#endif /* DT_NODE_HAS_STATUS(DT_NODELABEL(gpio6), okay) */ |
| 225 | + |
| 226 | +#if DT_NODE_HAS_STATUS(DT_NODELABEL(gpio7), okay) |
| 227 | +GPIO_DEVICE_INIT_RA8(7); |
| 228 | +#endif /* DT_NODE_HAS_STATUS(DT_NODELABEL(gpio7), okay) */ |
| 229 | + |
| 230 | +#if DT_NODE_HAS_STATUS(DT_NODELABEL(gpio8), okay) |
| 231 | +GPIO_DEVICE_INIT_RA8(8); |
| 232 | +#endif /* DT_NODE_HAS_STATUS(DT_NODELABEL(gpio8), okay) */ |
| 233 | + |
| 234 | +#if DT_NODE_HAS_STATUS(DT_NODELABEL(gpio9), okay) |
| 235 | +GPIO_DEVICE_INIT_RA8(9); |
| 236 | +#endif /* DT_NODE_HAS_STATUS(DT_NODELABEL(gpio9), okay) */ |
| 237 | + |
| 238 | +#if DT_NODE_HAS_STATUS(DT_NODELABEL(gpioa), okay) |
| 239 | +GPIO_DEVICE_INIT_RA8(a); |
| 240 | +#endif /* DT_NODE_HAS_STATUS(DT_NODELABEL(gpioa), okay) */ |
| 241 | + |
| 242 | +#if DT_NODE_HAS_STATUS(DT_NODELABEL(gpiob), okay) |
| 243 | +GPIO_DEVICE_INIT_RA8(b); |
| 244 | +#endif /* DT_NODE_HAS_STATUS(DT_NODELABEL(gpiob), okay) */ |
0 commit comments