|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 ENE Technology Inc. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#define DT_DRV_COMPAT ene_kb106x_gpio |
| 8 | + |
| 9 | +#include <zephyr/kernel.h> |
| 10 | +#include <zephyr/drivers/gpio.h> |
| 11 | +#include <zephyr/drivers/gpio/gpio_utils.h> |
| 12 | +#include <zephyr/dt-bindings/gpio/ene-kb106x-gpio.h> |
| 13 | +#include <zephyr/sys/util_macro.h> |
| 14 | +#include <reg/gpio.h> |
| 15 | +#include <reg/gptd.h> |
| 16 | + |
| 17 | +struct gpio_kb106x_data { |
| 18 | + /* gpio_driver_data needs to be first */ |
| 19 | + struct gpio_driver_data common; |
| 20 | + sys_slist_t cb; |
| 21 | +}; |
| 22 | + |
| 23 | +struct gpio_kb106x_config { |
| 24 | + /* gpio_driver_config needs to be first */ |
| 25 | + struct gpio_driver_config common; |
| 26 | + /* base address of GPIO port */ |
| 27 | + struct gpio_regs *gpio_regs; |
| 28 | + struct gptd_regs *gptd_regs; |
| 29 | +}; |
| 30 | + |
| 31 | +static void gpio_kb106x_isr(const struct device *dev) |
| 32 | +{ |
| 33 | + const struct gpio_kb106x_config *config = dev->config; |
| 34 | + struct gpio_kb106x_data *context = dev->data; |
| 35 | + uint32_t pending_flag = config->gptd_regs->GPTDPF; |
| 36 | + |
| 37 | + gpio_fire_callbacks(&context->cb, dev, pending_flag); |
| 38 | + config->gptd_regs->GPTDPF = pending_flag; |
| 39 | +} |
| 40 | + |
| 41 | +static int kb106x_gpio_pin_configure(const struct device *dev, gpio_pin_t pin, gpio_flags_t flags) |
| 42 | +{ |
| 43 | + const struct gpio_kb106x_config *config = dev->config; |
| 44 | + |
| 45 | + /* ene specific flags. low voltage mode, input voltage threshold (ViH & ViL) 1.8V */ |
| 46 | + if (flags & ENE_GPIO_VOLTAGE_1P8) { |
| 47 | + WRITE_BIT(config->gpio_regs->GPIOLV, pin, 1); |
| 48 | + } else { |
| 49 | + WRITE_BIT(config->gpio_regs->GPIOLV, pin, 0); |
| 50 | + } |
| 51 | + /* ene specific flags. max current driving ability, max support 16 mA */ |
| 52 | + if (flags & ENE_GPIO_DRIVING_16MA) { |
| 53 | + WRITE_BIT(config->gpio_regs->GPIODC, pin, 1); |
| 54 | + } else { |
| 55 | + WRITE_BIT(config->gpio_regs->GPIODC, pin, 0); |
| 56 | + } |
| 57 | + /* pull-up function */ |
| 58 | + if (flags & GPIO_PULL_UP) { |
| 59 | + WRITE_BIT(config->gpio_regs->GPIOPU, pin, 1); |
| 60 | + } else { |
| 61 | + WRITE_BIT(config->gpio_regs->GPIOPU, pin, 0); |
| 62 | + } |
| 63 | + /* output data high/low */ |
| 64 | + if (flags & GPIO_OUTPUT_INIT_HIGH) { |
| 65 | + WRITE_BIT(config->gpio_regs->GPIOD, pin, 1); |
| 66 | + } else if (flags & GPIO_OUTPUT_INIT_LOW) { |
| 67 | + WRITE_BIT(config->gpio_regs->GPIOD, pin, 0); |
| 68 | + } |
| 69 | + /* output enable function */ |
| 70 | + if (flags & GPIO_OUTPUT) { |
| 71 | + /* [patch] setting open-drain only when output is enabled */ |
| 72 | + /* output type push-pull/open-drain */ |
| 73 | + if (flags & GPIO_SINGLE_ENDED) { |
| 74 | + if (flags & GPIO_LINE_OPEN_DRAIN) { |
| 75 | + WRITE_BIT(config->gpio_regs->GPIOOD, pin, 1); |
| 76 | + } else { |
| 77 | + WRITE_BIT(config->gpio_regs->GPIOOD, pin, 0); |
| 78 | + } |
| 79 | + } else { |
| 80 | + WRITE_BIT(config->gpio_regs->GPIOOD, pin, 0); |
| 81 | + } |
| 82 | + WRITE_BIT(config->gpio_regs->GPIOOE, pin, 1); |
| 83 | + } else { |
| 84 | + WRITE_BIT(config->gpio_regs->GPIOOE, pin, 0); |
| 85 | + /* [patch] disable open-drain when output is disabled */ |
| 86 | + WRITE_BIT(config->gpio_regs->GPIOOD, pin, 0); |
| 87 | + } |
| 88 | + /* input enable function */ |
| 89 | + if (flags & GPIO_INPUT) { |
| 90 | + WRITE_BIT(config->gpio_regs->GPIOIE, pin, 1); |
| 91 | + } else { |
| 92 | + WRITE_BIT(config->gpio_regs->GPIOIE, pin, 0); |
| 93 | + } |
| 94 | + |
| 95 | + return 0; |
| 96 | +} |
| 97 | + |
| 98 | +static int kb106x_gpio_port_get_raw(const struct device *dev, gpio_port_value_t *value) |
| 99 | +{ |
| 100 | + const struct gpio_kb106x_config *config = dev->config; |
| 101 | + |
| 102 | + *value = config->gpio_regs->GPIOIN; |
| 103 | + return 0; |
| 104 | +} |
| 105 | + |
| 106 | +static int kb106x_gpio_port_set_masked_raw(const struct device *dev, gpio_port_pins_t mask, |
| 107 | + gpio_port_value_t value) |
| 108 | +{ |
| 109 | + const struct gpio_kb106x_config *config = dev->config; |
| 110 | + |
| 111 | + config->gpio_regs->GPIOD |= (value & mask); |
| 112 | + return 0; |
| 113 | +} |
| 114 | + |
| 115 | +static int kb106x_gpio_port_set_bits_raw(const struct device *dev, gpio_port_pins_t pins) |
| 116 | +{ |
| 117 | + const struct gpio_kb106x_config *config = dev->config; |
| 118 | + |
| 119 | + config->gpio_regs->GPIOD |= pins; |
| 120 | + return 0; |
| 121 | +} |
| 122 | + |
| 123 | +static int kb106x_gpio_port_clear_bits_raw(const struct device *dev, gpio_port_pins_t pins) |
| 124 | +{ |
| 125 | + const struct gpio_kb106x_config *config = dev->config; |
| 126 | + |
| 127 | + config->gpio_regs->GPIOD &= ~pins; |
| 128 | + return 0; |
| 129 | +} |
| 130 | + |
| 131 | +static int kb106x_gpio_port_toggle_bits(const struct device *dev, gpio_port_pins_t pins) |
| 132 | +{ |
| 133 | + const struct gpio_kb106x_config *config = dev->config; |
| 134 | + |
| 135 | + config->gpio_regs->GPIOD ^= pins; |
| 136 | + return 0; |
| 137 | +} |
| 138 | + |
| 139 | +static int kb106x_gpio_pin_interrupt_configure(const struct device *dev, gpio_pin_t pin, |
| 140 | + enum gpio_int_mode mode, enum gpio_int_trig trig) |
| 141 | +{ |
| 142 | + const struct gpio_kb106x_config *config = dev->config; |
| 143 | + |
| 144 | + if (mode & GPIO_INT_EDGE) { |
| 145 | + WRITE_BIT(config->gptd_regs->GPTDEL, pin, 0); |
| 146 | + if (trig & GPIO_INT_HIGH_1) { |
| 147 | + if (trig & GPIO_INT_LOW_0) { /* Falling & Rising edge trigger */ |
| 148 | + /* Enable toggle trigger */ |
| 149 | + WRITE_BIT(config->gptd_regs->GPTDCHG, pin, 1); |
| 150 | + } else { /* Rising edge */ |
| 151 | + /* Disable toggle trigger */ |
| 152 | + WRITE_BIT(config->gptd_regs->GPTDCHG, pin, 0); |
| 153 | + WRITE_BIT(config->gptd_regs->GPTDPS, pin, 1); |
| 154 | + } |
| 155 | + } else { /* Falling edge */ |
| 156 | + /* Disable Toggle trigger */ |
| 157 | + WRITE_BIT(config->gptd_regs->GPTDCHG, pin, 0); |
| 158 | + WRITE_BIT(config->gptd_regs->GPTDPS, pin, 0); |
| 159 | + } |
| 160 | + } else { |
| 161 | + WRITE_BIT(config->gptd_regs->GPTDEL, pin, 1); |
| 162 | + /* Disable Toggle trigger */ |
| 163 | + WRITE_BIT(config->gptd_regs->GPTDCHG, pin, 0); |
| 164 | + if (trig & GPIO_INT_HIGH_1) { |
| 165 | + WRITE_BIT(config->gptd_regs->GPTDPS, pin, 1); |
| 166 | + } else { |
| 167 | + WRITE_BIT(config->gptd_regs->GPTDPS, pin, 0); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + /* clear pending flag */ |
| 172 | + WRITE_BIT(config->gptd_regs->GPTDPF, pin, 1); |
| 173 | + |
| 174 | + /* Check if GPIO port needs interrupt support */ |
| 175 | + if ((mode & GPIO_INT_DISABLE) || (mode & GPIO_INT_ENABLE) == 0) { |
| 176 | + /* Set the mask to disable the interrupt */ |
| 177 | + WRITE_BIT(config->gptd_regs->GPTDIE, pin, 0); |
| 178 | + } else { |
| 179 | + /* Enable the interrupt */ |
| 180 | + WRITE_BIT(config->gptd_regs->GPTDIE, pin, 1); |
| 181 | + } |
| 182 | + |
| 183 | + /* Check GPIO wakeup enable */ |
| 184 | + if (trig & GPIO_INT_TRIG_WAKE) { |
| 185 | + WRITE_BIT(config->gptd_regs->GPTDWE, pin, 1); |
| 186 | + } else { |
| 187 | + WRITE_BIT(config->gptd_regs->GPTDWE, pin, 0); |
| 188 | + } |
| 189 | + return 0; |
| 190 | +} |
| 191 | + |
| 192 | +static int kb106x_gpio_manage_callback(const struct device *dev, struct gpio_callback *cb, bool set) |
| 193 | +{ |
| 194 | + struct gpio_kb106x_data *context = dev->data; |
| 195 | + |
| 196 | + gpio_manage_callback(&context->cb, cb, set); |
| 197 | + return 0; |
| 198 | +} |
| 199 | + |
| 200 | +static uint32_t kb106x_gpio_get_pending_int(const struct device *dev) |
| 201 | +{ |
| 202 | + const struct gpio_kb106x_config *const config = dev->config; |
| 203 | + |
| 204 | + return config->gptd_regs->GPTDPF; |
| 205 | +} |
| 206 | + |
| 207 | +static const struct gpio_driver_api kb106x_gpio_api = { |
| 208 | + .pin_configure = kb106x_gpio_pin_configure, |
| 209 | + .port_get_raw = kb106x_gpio_port_get_raw, |
| 210 | + .port_set_masked_raw = kb106x_gpio_port_set_masked_raw, |
| 211 | + .port_set_bits_raw = kb106x_gpio_port_set_bits_raw, |
| 212 | + .port_clear_bits_raw = kb106x_gpio_port_clear_bits_raw, |
| 213 | + .port_toggle_bits = kb106x_gpio_port_toggle_bits, |
| 214 | + .pin_interrupt_configure = kb106x_gpio_pin_interrupt_configure, |
| 215 | + .manage_callback = kb106x_gpio_manage_callback, |
| 216 | + .get_pending_int = kb106x_gpio_get_pending_int, |
| 217 | +}; |
| 218 | + |
| 219 | +#define KB106X_GPIO_INIT(n) \ |
| 220 | + static int kb106x_gpio_##n##_init(const struct device *dev) \ |
| 221 | + { \ |
| 222 | + IRQ_CONNECT(DT_INST_IRQ_BY_IDX(n, 0, irq), DT_INST_IRQ_BY_IDX(n, 0, priority), \ |
| 223 | + gpio_kb106x_isr, DEVICE_DT_INST_GET(n), 0); \ |
| 224 | + irq_enable(DT_INST_IRQ_BY_IDX(n, 0, irq)); \ |
| 225 | + IRQ_CONNECT(DT_INST_IRQ_BY_IDX(n, 1, irq), DT_INST_IRQ_BY_IDX(n, 1, priority), \ |
| 226 | + gpio_kb106x_isr, DEVICE_DT_INST_GET(n), 0); \ |
| 227 | + irq_enable(DT_INST_IRQ_BY_IDX(n, 1, irq)); \ |
| 228 | + return 0; \ |
| 229 | + }; \ |
| 230 | + static const struct gpio_kb106x_config port_##n##_kb106x_config = { \ |
| 231 | + .common = {.port_pin_mask = GPIO_PORT_PIN_MASK_FROM_DT_INST(n)}, \ |
| 232 | + .gpio_regs = (struct gpio_regs *)DT_INST_REG_ADDR_BY_IDX(n, 0), \ |
| 233 | + .gptd_regs = (struct gptd_regs *)DT_INST_REG_ADDR_BY_IDX(n, 1), \ |
| 234 | + }; \ |
| 235 | + static struct gpio_kb106x_data gpio_kb106x_##n##_data; \ |
| 236 | + DEVICE_DT_INST_DEFINE(n, &kb106x_gpio_##n##_init, NULL, &gpio_kb106x_##n##_data, \ |
| 237 | + &port_##n##_kb106x_config, PRE_KERNEL_1, CONFIG_GPIO_INIT_PRIORITY, \ |
| 238 | + &kb106x_gpio_api); |
| 239 | + |
| 240 | +DT_INST_FOREACH_STATUS_OKAY(KB106X_GPIO_INIT) |
0 commit comments