|
| 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_get(const struct device *dev, gpio_port_value_t *value) |
| 72 | +{ |
| 73 | + const struct gpio_tms570_config *config = dev->config; |
| 74 | + |
| 75 | + *value = sys_read32(config->reg_port + REG_DIN); |
| 76 | + |
| 77 | + return 0; |
| 78 | +} |
| 79 | + |
| 80 | +static int gpio_tms570_configure(const struct device *dev, gpio_pin_t pin, gpio_flags_t flags) |
| 81 | +{ |
| 82 | + const struct gpio_tms570_config *config = dev->config; |
| 83 | + uint32_t current_config; |
| 84 | + |
| 85 | + /* Read the current configuration of the pins */ |
| 86 | + current_config = sys_read32(config->reg_port + REG_DIR); |
| 87 | + |
| 88 | + /* We only support changes in the direction of the pins */ |
| 89 | + if (flags == GPIO_INPUT) { |
| 90 | + /* Pins specified as input will have their DIR register's bit set to 0 */ |
| 91 | + sys_write32(current_config & ~BIT(pin), config->reg_port + REG_DIR); |
| 92 | + } else if (flags == GPIO_OUTPUT) { |
| 93 | + /* Pins specified as input will have their DIR register's bit set to 1 */ |
| 94 | + sys_write32(current_config | BIT(pin), config->reg_port + REG_DIR); |
| 95 | + } else { |
| 96 | + return -EINVAL; |
| 97 | + } |
| 98 | + |
| 99 | + return 0; |
| 100 | +} |
| 101 | + |
| 102 | +static int gpio_tms570_pin_interrupt_configure(const struct device *dev, gpio_pin_t pin, |
| 103 | + enum gpio_int_mode mode, enum gpio_int_trig trig) |
| 104 | +{ |
| 105 | + return -ENOSYS; |
| 106 | +} |
| 107 | + |
| 108 | +static int gpio_tms570_manage_callback(const struct device *dev, struct gpio_callback *callback, |
| 109 | + bool set) |
| 110 | +{ |
| 111 | + return -ENOSYS; |
| 112 | +} |
| 113 | + |
| 114 | +static const struct gpio_driver_api gpio_tms570_api = { |
| 115 | + .port_set_bits_raw = gpio_tms570_set_bits, |
| 116 | + .port_clear_bits_raw = gpio_tms570_clear_bits, |
| 117 | + .port_get_raw = gpio_tms570_get, |
| 118 | + .pin_configure = gpio_tms570_configure, |
| 119 | + .pin_interrupt_configure = gpio_tms570_pin_interrupt_configure, |
| 120 | + .manage_callback = gpio_tms570_manage_callback, |
| 121 | +}; |
| 122 | + |
| 123 | +static int gpio_tms570_init(const struct device *dev) |
| 124 | +{ |
| 125 | + const struct gpio_tms570_config *config = dev->config; |
| 126 | + static int gpio_tms570_init_done; |
| 127 | + |
| 128 | + if (gpio_tms570_init_done == 0) { |
| 129 | + gpio_tms570_init_done = 1; |
| 130 | + sys_write32(1, config->reg_gio + REG_GCR0); |
| 131 | + sys_write32(0xFFU, config->reg_gio + REG_ENACLR); |
| 132 | + sys_write32(0xFFU, config->reg_gio + REG_LVLCLR); |
| 133 | + } |
| 134 | + |
| 135 | + return 0; |
| 136 | +} |
| 137 | + |
| 138 | +#define TMS570_GPIO_INIT(n) \ |
| 139 | + static struct gpio_tms570_data gpio_tms570_data_##n = { \ |
| 140 | + }; \ |
| 141 | + static struct gpio_tms570_config gpio_tms570_config_##n = { \ |
| 142 | + .common = { \ |
| 143 | + .port_pin_mask = GPIO_PORT_PIN_MASK_FROM_DT_INST(n), \ |
| 144 | + }, \ |
| 145 | + .reg_port = DT_INST_REG_ADDR_BY_IDX(n, 0), \ |
| 146 | + .reg_gio = DT_INST_REG_ADDR_BY_IDX(n, 1), \ |
| 147 | + }; \ |
| 148 | + DEVICE_DT_INST_DEFINE(n, gpio_tms570_init, NULL, \ |
| 149 | + &gpio_tms570_data_##n, &gpio_tms570_config_##n, \ |
| 150 | + POST_KERNEL, CONFIG_GPIO_INIT_PRIORITY, \ |
| 151 | + &gpio_tms570_api); |
| 152 | + |
| 153 | +DT_INST_FOREACH_STATUS_OKAY(TMS570_GPIO_INIT) |
0 commit comments