|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 ispace, inc. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#define DT_DRV_COMPAT ti_tms570_gpio |
| 8 | + |
| 9 | +#include <zephyr/drivers/gpio.h> |
| 10 | +#include <zephyr/kernel.h> |
| 11 | +#include <zephyr/sys/sys_io.h> |
| 12 | +#include <zephyr/drivers/gpio/gpio_utils.h> |
| 13 | +#include <zephyr/irq.h> |
| 14 | + |
| 15 | +/* port registers */ |
| 16 | +#define REG_DIR 0x0000 /* Data Direction Register */ |
| 17 | +#define REG_DIN 0x0004 /* Data Input Register */ |
| 18 | +#define REG_DOUT 0x0008 /* Data Output Register */ |
| 19 | +#define REG_DSET 0x000C /* Data Output Set Register */ |
| 20 | +#define REG_DCLR 0x0010 /* Data Output Clear Register */ |
| 21 | +#define REG_PDR 0x0014 /* Open Drain Register */ |
| 22 | +#define REG_PULDIS 0x0018 /* Pullup Disable Register */ |
| 23 | +#define REG_PSL 0x001C /* Pull Up/Down Selection Register */ |
| 24 | + |
| 25 | +/* GIO base registers */ |
| 26 | +#define REG_GCR0 0x0000 /* Global Control Register */ |
| 27 | +#define REG_INTDET 0x0008 /* Interrupt Detect Register*/ |
| 28 | +#define REG_POL 0x000C /* Interrupt Polarity Register */ |
| 29 | +#define REG_ENASET 0x0010 /* Interrupt Enable Set Register */ |
| 30 | +#define REG_ENACLR 0x0014 /* Interrupt Enable Clear Register */ |
| 31 | +#define REG_LVLSET 0x0018 /* Interrupt Priority Set Register */ |
| 32 | +#define REG_LVLCLR 0x001C /* Interrupt Priority Clear Register */ |
| 33 | +#define REG_FLG 0x0020 /* Interrupt Flag Register */ |
| 34 | +#define REG_OFF1 0x0024 /* Interrupt Offset A Register */ |
| 35 | +#define REG_OFF2 0x0028 /* Interrupt Offset B Register */ |
| 36 | +#define REG_EMU1 0x002C /* Emulation 1 Register */ |
| 37 | +#define REG_EMU2 0x0030 /* Emulation 2 Register */ |
| 38 | + |
| 39 | +struct gpio_tms570_config { |
| 40 | + /* gpio_driver_config needs to be first */ |
| 41 | + struct gpio_driver_config common; |
| 42 | + uint32_t reg_gio; |
| 43 | + uint32_t reg_port; |
| 44 | +}; |
| 45 | + |
| 46 | +struct gpio_tms570_data { |
| 47 | + /* gpio_driver_data needs to be first */ |
| 48 | + struct gpio_driver_data common; |
| 49 | +}; |
| 50 | + |
| 51 | +static int gpio_tms570_set_bits(const struct device *dev, gpio_port_pins_t pin) |
| 52 | +{ |
| 53 | + const struct gpio_tms570_config *config = dev->config; |
| 54 | + |
| 55 | + sys_write32(pin, config->reg_port + REG_DOUT); |
| 56 | + |
| 57 | + return 0; |
| 58 | +} |
| 59 | + |
| 60 | +static int gpio_tms570_clear_bits(const struct device *dev, gpio_port_pins_t pin) |
| 61 | +{ |
| 62 | + const struct gpio_tms570_config *config = dev->config; |
| 63 | + uint32_t val; |
| 64 | + |
| 65 | + val = sys_read32(config->reg_port + REG_DIN); |
| 66 | + sys_write32(val & (uint32_t)~pin, config->reg_port + REG_DOUT); |
| 67 | + |
| 68 | + return 0; |
| 69 | +} |
| 70 | + |
| 71 | +static int gpio_tms570_port_set_masked_raw(const struct device *dev, |
| 72 | + gpio_port_pins_t mask, |
| 73 | + gpio_port_value_t value) |
| 74 | +{ |
| 75 | + const struct gpio_tms570_config *config = dev->config; |
| 76 | + uint32_t cur_out; |
| 77 | + uint32_t cur_dir; |
| 78 | + uint32_t val_set; |
| 79 | + uint32_t val_clr; |
| 80 | + |
| 81 | + cur_out = sys_read32(config->reg_port + REG_DIN); |
| 82 | + cur_dir = sys_read32(config->reg_port + REG_DIR); |
| 83 | + val_clr = cur_dir & cur_out & ~value & mask; |
| 84 | + val_set = cur_dir & ~cur_out & value & mask; |
| 85 | + |
| 86 | + sys_write32(cur_out & (uint32_t)~val_clr, config->reg_port + REG_DOUT); |
| 87 | + sys_write32(val_set, config->reg_port + REG_DOUT); |
| 88 | + |
| 89 | + return 0; |
| 90 | +} |
| 91 | + |
| 92 | +static int gpio_tms570_port_toggle_bits(const struct device *dev, |
| 93 | + gpio_port_pins_t pins) |
| 94 | +{ |
| 95 | + const struct gpio_tms570_config *config = dev->config; |
| 96 | + uint32_t cur_out; |
| 97 | + uint32_t cur_dir; |
| 98 | + uint32_t val_set; |
| 99 | + uint32_t val_clr; |
| 100 | + |
| 101 | + cur_out = sys_read32(config->reg_port + REG_DIN); |
| 102 | + cur_dir = sys_read32(config->reg_port + REG_DIR); |
| 103 | + val_clr = cur_dir & cur_out & pins; |
| 104 | + val_set = cur_dir & ~cur_out & pins; |
| 105 | + sys_write32(cur_out & (uint32_t)~val_clr, config->reg_port + REG_DOUT); |
| 106 | + sys_write32(val_set, config->reg_port + REG_DOUT); |
| 107 | + |
| 108 | + return 0; |
| 109 | +} |
| 110 | + |
| 111 | + |
| 112 | +static int gpio_tms570_get(const struct device *dev, gpio_port_value_t *value) |
| 113 | +{ |
| 114 | + const struct gpio_tms570_config *config = dev->config; |
| 115 | + |
| 116 | + *value = sys_read32(config->reg_port + REG_DIN); |
| 117 | + |
| 118 | + return 0; |
| 119 | +} |
| 120 | + |
| 121 | +static int gpio_tms570_configure(const struct device *dev, gpio_pin_t pin, gpio_flags_t flags) |
| 122 | +{ |
| 123 | + const struct gpio_tms570_config *config = dev->config; |
| 124 | + uint32_t current_config; |
| 125 | + int ret; |
| 126 | + |
| 127 | + /* Read the current configuration of the pins */ |
| 128 | + current_config = sys_read32(config->reg_port + REG_DIR); |
| 129 | + |
| 130 | + /* We only support changes in the direction of the pins */ |
| 131 | + if ((flags & GPIO_INPUT) != 0U) { |
| 132 | + /* Pins specified as input will have their DIR register's bit set to 0 */ |
| 133 | + sys_write32(current_config & ~BIT(pin), config->reg_port + REG_DIR); |
| 134 | + } else if ((flags & GPIO_OUTPUT) != 0U) { |
| 135 | + /* Pins specified as output will have their DIR register's bit set to 1 */ |
| 136 | + sys_write32(current_config | BIT(pin), config->reg_port + REG_DIR); |
| 137 | + |
| 138 | + if ((flags & GPIO_OUTPUT_INIT_HIGH) != 0U) { |
| 139 | + ret = gpio_tms570_set_bits(dev, (gpio_port_pins_t)BIT(pin)); |
| 140 | + if (ret < 0) { |
| 141 | + return ret; |
| 142 | + } |
| 143 | + } else if ((flags & GPIO_OUTPUT_INIT_LOW) != 0U) { |
| 144 | + ret = gpio_tms570_set_bits(dev, (gpio_port_pins_t)BIT(pin)); |
| 145 | + if (ret < 0) { |
| 146 | + return ret; |
| 147 | + } |
| 148 | + } |
| 149 | + } else { |
| 150 | + return -EINVAL; |
| 151 | + } |
| 152 | + |
| 153 | + return 0; |
| 154 | +} |
| 155 | + |
| 156 | +static int gpio_tms570_pin_interrupt_configure(const struct device *dev, gpio_pin_t pin, |
| 157 | + enum gpio_int_mode mode, enum gpio_int_trig trig) |
| 158 | +{ |
| 159 | + return -ENOTSUP; |
| 160 | +} |
| 161 | + |
| 162 | +static int gpio_tms570_manage_callback(const struct device *dev, struct gpio_callback *callback, |
| 163 | + bool set) |
| 164 | +{ |
| 165 | + return -ENOTSUP; |
| 166 | +} |
| 167 | + |
| 168 | +static const struct gpio_driver_api gpio_tms570_api = { |
| 169 | + .port_get_raw = gpio_tms570_get, |
| 170 | + .port_set_masked_raw = gpio_tms570_port_set_masked_raw, |
| 171 | + .port_set_bits_raw = gpio_tms570_set_bits, |
| 172 | + .port_clear_bits_raw = gpio_tms570_clear_bits, |
| 173 | + .pin_configure = gpio_tms570_configure, |
| 174 | + .port_toggle_bits = gpio_tms570_port_toggle_bits, |
| 175 | + .pin_interrupt_configure = gpio_tms570_pin_interrupt_configure, |
| 176 | + .manage_callback = gpio_tms570_manage_callback, |
| 177 | +}; |
| 178 | + |
| 179 | +static int gpio_tms570_init(const struct device *dev) |
| 180 | +{ |
| 181 | + const struct gpio_tms570_config *config = dev->config; |
| 182 | + static int gpio_tms570_init_done; |
| 183 | + |
| 184 | + if (gpio_tms570_init_done == 0) { |
| 185 | + gpio_tms570_init_done = 1; |
| 186 | + sys_write32(1, config->reg_gio + REG_GCR0); |
| 187 | + sys_write32(0xFFU, config->reg_gio + REG_ENACLR); |
| 188 | + sys_write32(0xFFU, config->reg_gio + REG_LVLCLR); |
| 189 | + } |
| 190 | + |
| 191 | + return 0; |
| 192 | +} |
| 193 | + |
| 194 | +#define TMS570_GPIO_INIT(n) \ |
| 195 | + static struct gpio_tms570_data gpio_tms570_data_##n = { \ |
| 196 | + }; \ |
| 197 | + static struct gpio_tms570_config gpio_tms570_config_##n = { \ |
| 198 | + .common = { \ |
| 199 | + .port_pin_mask = GPIO_PORT_PIN_MASK_FROM_DT_INST(n), \ |
| 200 | + }, \ |
| 201 | + .reg_port = DT_INST_REG_ADDR_BY_IDX(n, 0), \ |
| 202 | + .reg_gio = DT_INST_REG_ADDR_BY_IDX(n, 1), \ |
| 203 | + }; \ |
| 204 | + DEVICE_DT_INST_DEFINE(n, gpio_tms570_init, NULL, \ |
| 205 | + &gpio_tms570_data_##n, &gpio_tms570_config_##n, \ |
| 206 | + POST_KERNEL, CONFIG_GPIO_INIT_PRIORITY, \ |
| 207 | + &gpio_tms570_api); |
| 208 | + |
| 209 | +DT_INST_FOREACH_STATUS_OKAY(TMS570_GPIO_INIT) |
0 commit comments