|
| 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/dt-bindings/gpio/renesas-ra8-gpio.h> |
| 12 | +#include <zephyr/drivers/gpio/gpio_utils.h> |
| 13 | +#include <zephyr/irq.h> |
| 14 | +#include <soc.h> |
| 15 | + |
| 16 | +struct gpio_ra8_config { |
| 17 | + struct gpio_driver_config common; |
| 18 | + uint8_t port_num; |
| 19 | + R_PORT0_Type *port; |
| 20 | + gpio_pin_t vbatt_pins[]; |
| 21 | +}; |
| 22 | + |
| 23 | +struct gpio_ra8_data { |
| 24 | + struct gpio_driver_data common; |
| 25 | +}; |
| 26 | + |
| 27 | +static int gpio_ra8_pin_configure(const struct device *dev, gpio_pin_t pin, gpio_flags_t flags) |
| 28 | +{ |
| 29 | + const struct gpio_ra8_config *config = dev->config; |
| 30 | + |
| 31 | + struct ra_pinctrl_soc_pin pincfg = {0}; |
| 32 | + |
| 33 | + if (((flags & GPIO_INPUT) != 0U) && ((flags & GPIO_OUTPUT) != 0U)) { |
| 34 | + return -ENOTSUP; |
| 35 | + } |
| 36 | + |
| 37 | + if ((flags & GPIO_PULL_DOWN) != 0U) { |
| 38 | + return -ENOTSUP; |
| 39 | + } |
| 40 | + |
| 41 | + if ((flags & GPIO_INT_ENABLE) != 0) { |
| 42 | + return -ENOTSUP; |
| 43 | + } |
| 44 | + |
| 45 | + if (config->vbatt_pins[0] != 0xFF) { |
| 46 | + uint32_t clear = 0; |
| 47 | + |
| 48 | + for (int i = 0; config->vbatt_pins[i] != '\0'; i++) { |
| 49 | + if (config->vbatt_pins[i] == pin) { |
| 50 | + WRITE_BIT(clear, i, 1); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + R_BSP_RegisterProtectDisable(BSP_REG_PROTECT_OM_LPC_BATT); |
| 55 | + |
| 56 | + R_SYSTEM->VBTICTLR &= (uint8_t)~clear; |
| 57 | + |
| 58 | + R_BSP_RegisterProtectEnable(BSP_REG_PROTECT_OM_LPC_BATT); |
| 59 | + } |
| 60 | + |
| 61 | + pincfg.port_num = config->port_num; |
| 62 | + pincfg.pin_num = pin; |
| 63 | + |
| 64 | + /* Change mode to general IO mode and disable IRQ and Analog input */ |
| 65 | + WRITE_BIT(pincfg.cfg, R_PFS_PORT_PIN_PmnPFS_PMR_Pos, 0); |
| 66 | + WRITE_BIT(pincfg.cfg, R_PFS_PORT_PIN_PmnPFS_ASEL_Pos, 0); |
| 67 | + WRITE_BIT(pincfg.cfg, R_PFS_PORT_PIN_PmnPFS_ISEL_Pos, 0); |
| 68 | + |
| 69 | + if ((flags & GPIO_OUTPUT) != 0U) { |
| 70 | + /* Set output pin initial value */ |
| 71 | + if ((flags & GPIO_OUTPUT_INIT_LOW) != 0U) { |
| 72 | + WRITE_BIT(pincfg.cfg, R_PFS_PORT_PIN_PmnPFS_PODR_Pos, 0); |
| 73 | + } else if ((flags & GPIO_OUTPUT_INIT_HIGH) != 0U) { |
| 74 | + WRITE_BIT(pincfg.cfg, R_PFS_PORT_PIN_PmnPFS_PODR_Pos, 1); |
| 75 | + } |
| 76 | + |
| 77 | + WRITE_BIT(pincfg.cfg, R_PFS_PORT_PIN_PmnPFS_PDR_Pos, 1); |
| 78 | + } else { |
| 79 | + WRITE_BIT(pincfg.cfg, R_PFS_PORT_PIN_PmnPFS_PDR_Pos, 0); |
| 80 | + } |
| 81 | + |
| 82 | + if ((flags & GPIO_LINE_OPEN_DRAIN) != 0) { |
| 83 | + WRITE_BIT(pincfg.cfg, R_PFS_PORT_PIN_PmnPFS_NCODR_Pos, 1); |
| 84 | + } |
| 85 | + |
| 86 | + if ((flags & GPIO_PULL_UP) != 0) { |
| 87 | + WRITE_BIT(pincfg.cfg, R_PFS_PORT_PIN_PmnPFS_PCR_Pos, 1); |
| 88 | + } |
| 89 | + |
| 90 | + pincfg.cfg = pincfg.cfg | |
| 91 | + (((flags & RENESAS_GPIO_DS_MSK) >> 8) << R_PFS_PORT_PIN_PmnPFS_DSCR_Pos); |
| 92 | + |
| 93 | + return pinctrl_configure_pins(&pincfg, 1, PINCTRL_REG_NONE); |
| 94 | +} |
| 95 | + |
| 96 | +static int gpio_ra8_port_get_raw(const struct device *dev, uint32_t *value) |
| 97 | +{ |
| 98 | + const struct gpio_ra8_config *config = dev->config; |
| 99 | + R_PORT0_Type *port = config->port; |
| 100 | + |
| 101 | + *value = port->PIDR; |
| 102 | + |
| 103 | + return 0; |
| 104 | +} |
| 105 | + |
| 106 | +static int gpio_ra8_port_set_masked_raw(const struct device *dev, gpio_port_pins_t mask, |
| 107 | + gpio_port_value_t value) |
| 108 | +{ |
| 109 | + const struct gpio_ra8_config *config = dev->config; |
| 110 | + R_PORT0_Type *port = config->port; |
| 111 | + |
| 112 | + port->PODR = ((port->PODR & ~mask) | (value & mask)); |
| 113 | + |
| 114 | + return 0; |
| 115 | +} |
| 116 | + |
| 117 | +static int gpio_ra8_port_set_bits_raw(const struct device *dev, gpio_port_pins_t pins) |
| 118 | +{ |
| 119 | + const struct gpio_ra8_config *config = dev->config; |
| 120 | + R_PORT0_Type *port = config->port; |
| 121 | + |
| 122 | + port->PODR = (port->PODR | pins); |
| 123 | + |
| 124 | + return 0; |
| 125 | +} |
| 126 | + |
| 127 | +static int gpio_ra8_port_clear_bits_raw(const struct device *dev, gpio_port_pins_t pins) |
| 128 | +{ |
| 129 | + const struct gpio_ra8_config *config = dev->config; |
| 130 | + R_PORT0_Type *port = config->port; |
| 131 | + |
| 132 | + port->PODR = (port->PODR & ~pins); |
| 133 | + |
| 134 | + return 0; |
| 135 | +} |
| 136 | + |
| 137 | +static int gpio_ra8_port_toggle_bits(const struct device *dev, gpio_port_pins_t pins) |
| 138 | +{ |
| 139 | + const struct gpio_ra8_config *config = dev->config; |
| 140 | + R_PORT0_Type *port = config->port; |
| 141 | + |
| 142 | + port->PODR = (port->PODR ^ pins); |
| 143 | + |
| 144 | + return 0; |
| 145 | +} |
| 146 | + |
| 147 | +static const struct gpio_driver_api gpio_ra8_drv_api_funcs = { |
| 148 | + .pin_configure = gpio_ra8_pin_configure, |
| 149 | + .port_get_raw = gpio_ra8_port_get_raw, |
| 150 | + .port_set_masked_raw = gpio_ra8_port_set_masked_raw, |
| 151 | + .port_set_bits_raw = gpio_ra8_port_set_bits_raw, |
| 152 | + .port_clear_bits_raw = gpio_ra8_port_clear_bits_raw, |
| 153 | + .port_toggle_bits = gpio_ra8_port_toggle_bits, |
| 154 | + .pin_interrupt_configure = NULL, |
| 155 | + .manage_callback = NULL, |
| 156 | +}; |
| 157 | + |
| 158 | +#define GPIO_DEVICE_INIT(node, port_number, suffix, addr) \ |
| 159 | + static const struct gpio_ra8_config gpio_ra8_config_##suffix = { \ |
| 160 | + .common = \ |
| 161 | + { \ |
| 162 | + .port_pin_mask = GPIO_PORT_PIN_MASK_FROM_NGPIOS(16U), \ |
| 163 | + }, \ |
| 164 | + .port_num = port_number, \ |
| 165 | + .port = (R_PORT0_Type *)addr, \ |
| 166 | + .vbatt_pins = DT_PROP_OR(DT_NODELABEL(ioport##suffix), vbatts_pins, {0xFF}), \ |
| 167 | + }; \ |
| 168 | + static struct gpio_ra8_data gpio_ra8_data_##suffix; \ |
| 169 | + DEVICE_DT_DEFINE(node, NULL, NULL, &gpio_ra8_data_##suffix, \ |
| 170 | + &gpio_ra8_config_##suffix, PRE_KERNEL_1, CONFIG_GPIO_INIT_PRIORITY, \ |
| 171 | + &gpio_ra8_drv_api_funcs) |
| 172 | + |
| 173 | +#define GPIO_DEVICE_INIT_RA8(suffix) \ |
| 174 | + GPIO_DEVICE_INIT(DT_NODELABEL(ioport##suffix), \ |
| 175 | + DT_PROP(DT_NODELABEL(ioport##suffix), port), suffix, \ |
| 176 | + DT_REG_ADDR(DT_NODELABEL(ioport##suffix))) |
| 177 | + |
| 178 | +#if DT_NODE_HAS_STATUS(DT_NODELABEL(ioport0), okay) |
| 179 | +GPIO_DEVICE_INIT_RA8(0); |
| 180 | +#endif /* DT_NODE_HAS_STATUS(DT_NODELABEL(ioport0), okay) */ |
| 181 | + |
| 182 | +#if DT_NODE_HAS_STATUS(DT_NODELABEL(ioport1), okay) |
| 183 | +GPIO_DEVICE_INIT_RA8(1); |
| 184 | +#endif /* DT_NODE_HAS_STATUS(DT_NODELABEL(ioport1), okay) */ |
| 185 | + |
| 186 | +#if DT_NODE_HAS_STATUS(DT_NODELABEL(ioport2), okay) |
| 187 | +GPIO_DEVICE_INIT_RA8(2); |
| 188 | +#endif /* DT_NODE_HAS_STATUS(DT_NODELABEL(ioport2), okay) */ |
| 189 | + |
| 190 | +#if DT_NODE_HAS_STATUS(DT_NODELABEL(ioport3), okay) |
| 191 | +GPIO_DEVICE_INIT_RA8(3); |
| 192 | +#endif /* DT_NODE_HAS_STATUS(DT_NODELABEL(ioport3), okay) */ |
| 193 | + |
| 194 | +#if DT_NODE_HAS_STATUS(DT_NODELABEL(ioport4), okay) |
| 195 | +GPIO_DEVICE_INIT_RA8(4); |
| 196 | +#endif /* DT_NODE_HAS_STATUS(DT_NODELABEL(ioport4), okay) */ |
| 197 | + |
| 198 | +#if DT_NODE_HAS_STATUS(DT_NODELABEL(ioport5), okay) |
| 199 | +GPIO_DEVICE_INIT_RA8(5); |
| 200 | +#endif /* DT_NODE_HAS_STATUS(DT_NODELABEL(ioport5), okay) */ |
| 201 | + |
| 202 | +#if DT_NODE_HAS_STATUS(DT_NODELABEL(ioport6), okay) |
| 203 | +GPIO_DEVICE_INIT_RA8(6); |
| 204 | +#endif /* DT_NODE_HAS_STATUS(DT_NODELABEL(ioport6), okay) */ |
| 205 | + |
| 206 | +#if DT_NODE_HAS_STATUS(DT_NODELABEL(ioport7), okay) |
| 207 | +GPIO_DEVICE_INIT_RA8(7); |
| 208 | +#endif /* DT_NODE_HAS_STATUS(DT_NODELABEL(ioport7), okay) */ |
| 209 | + |
| 210 | +#if DT_NODE_HAS_STATUS(DT_NODELABEL(ioport8), okay) |
| 211 | +GPIO_DEVICE_INIT_RA8(8); |
| 212 | +#endif /* DT_NODE_HAS_STATUS(DT_NODELABEL(ioport8), okay) */ |
| 213 | + |
| 214 | +#if DT_NODE_HAS_STATUS(DT_NODELABEL(ioport9), okay) |
| 215 | +GPIO_DEVICE_INIT_RA8(9); |
| 216 | +#endif /* DT_NODE_HAS_STATUS(DT_NODELABEL(ioport9), okay) */ |
| 217 | + |
| 218 | +#if DT_NODE_HAS_STATUS(DT_NODELABEL(ioporta), okay) |
| 219 | +GPIO_DEVICE_INIT_RA8(a); |
| 220 | +#endif /* DT_NODE_HAS_STATUS(DT_NODELABEL(ioporta), okay) */ |
| 221 | + |
| 222 | +#if DT_NODE_HAS_STATUS(DT_NODELABEL(ioportb), okay) |
| 223 | +GPIO_DEVICE_INIT_RA8(b); |
| 224 | +#endif /* DT_NODE_HAS_STATUS(DT_NODELABEL(ioportb), okay) */ |
0 commit comments