|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Aesc Silicon |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#define DT_DRV_COMPAT aesc_gpio |
| 8 | + |
| 9 | +#include <errno.h> |
| 10 | +#include <ip_identification.h> |
| 11 | +#include <soc.h> |
| 12 | + |
| 13 | +#include <zephyr/arch/common/sys_bitops.h> |
| 14 | +#include <zephyr/device.h> |
| 15 | +#include <zephyr/devicetree.h> |
| 16 | +#include <zephyr/drivers/gpio.h> |
| 17 | +#include <zephyr/drivers/gpio/gpio_utils.h> |
| 18 | +#include <zephyr/init.h> |
| 19 | +#include <zephyr/kernel.h> |
| 20 | +#include <zephyr/sys/sys_io.h> |
| 21 | +#include <zephyr/spinlock.h> |
| 22 | + |
| 23 | + |
| 24 | +#include <zephyr/logging/log.h> |
| 25 | +LOG_MODULE_REGISTER(aesc_gpio, CONFIG_GPIO_LOG_LEVEL); |
| 26 | + |
| 27 | +struct gpio_aesc_config { |
| 28 | + DEVICE_MMIO_ROM; |
| 29 | +}; |
| 30 | + |
| 31 | +struct gpio_aesc_regs { |
| 32 | + uint32_t info; |
| 33 | + uint32_t read; |
| 34 | + uint32_t write; |
| 35 | + uint32_t direction; |
| 36 | + uint32_t high_ip; |
| 37 | + uint32_t high_ie; |
| 38 | + uint32_t low_ip; |
| 39 | + uint32_t low_ie; |
| 40 | + uint32_t rise_ip; |
| 41 | + uint32_t rise_ie; |
| 42 | + uint32_t fall_ip; |
| 43 | + uint32_t fall_ie; |
| 44 | +} __packed; |
| 45 | + |
| 46 | +struct gpio_aesc_data { |
| 47 | + DEVICE_MMIO_RAM; |
| 48 | + sys_slist_t cb; |
| 49 | + struct k_spinlock lock; |
| 50 | +}; |
| 51 | + |
| 52 | +#define DEV_CFG(dev) ((struct gpio_aesc_config *)(dev)->config) |
| 53 | +#define DEV_DATA(dev) ((struct gpio_aesc_data *)(dev)->data) |
| 54 | +#define DEV_GPIO(dev) ((struct gpio_aesc_regs *)DEVICE_MMIO_GET(dev)) |
| 55 | + |
| 56 | +static int gpio_aesc_config(const struct device *dev, gpio_pin_t pin, |
| 57 | + gpio_flags_t flags) |
| 58 | +{ |
| 59 | + volatile struct gpio_aesc_regs *gpio = DEV_GPIO(dev); |
| 60 | + struct gpio_aesc_data *data = DEV_DATA(dev); |
| 61 | + k_spinlock_key_t key; |
| 62 | + |
| 63 | + key = k_spin_lock(&data->lock); |
| 64 | + /* Configure gpio direction */ |
| 65 | + if (flags & GPIO_OUTPUT) { |
| 66 | + gpio->direction |= BIT(pin); |
| 67 | + } else { |
| 68 | + gpio->direction &= ~BIT(pin); |
| 69 | + } |
| 70 | + k_spin_unlock(&data->lock, key); |
| 71 | + |
| 72 | + return 0; |
| 73 | +} |
| 74 | + |
| 75 | +static int gpio_aesc_port_get_raw(const struct device *dev, |
| 76 | + gpio_port_value_t *value) |
| 77 | +{ |
| 78 | + volatile struct gpio_aesc_regs *gpio = DEV_GPIO(dev); |
| 79 | + |
| 80 | + *value = gpio->read; |
| 81 | + |
| 82 | + return 0; |
| 83 | +} |
| 84 | + |
| 85 | +static int gpio_aesc_port_set_masked_raw(const struct device *dev, |
| 86 | + gpio_port_pins_t mask, |
| 87 | + gpio_port_value_t value) |
| 88 | +{ |
| 89 | + volatile struct gpio_aesc_regs *gpio = DEV_GPIO(dev); |
| 90 | + struct gpio_aesc_data *data = DEV_DATA(dev); |
| 91 | + k_spinlock_key_t key; |
| 92 | + |
| 93 | + key = k_spin_lock(&data->lock); |
| 94 | + gpio->write = (gpio->write & ~mask) | (value & mask); |
| 95 | + k_spin_unlock(&data->lock, key); |
| 96 | + |
| 97 | + return 0; |
| 98 | +} |
| 99 | + |
| 100 | +static int gpio_aesc_port_set_bits_raw(const struct device *dev, |
| 101 | + gpio_port_pins_t mask) |
| 102 | +{ |
| 103 | + volatile struct gpio_aesc_regs *gpio = DEV_GPIO(dev); |
| 104 | + struct gpio_aesc_data *data = DEV_DATA(dev); |
| 105 | + k_spinlock_key_t key; |
| 106 | + |
| 107 | + key = k_spin_lock(&data->lock); |
| 108 | + gpio->write |= mask; |
| 109 | + k_spin_unlock(&data->lock, key); |
| 110 | + |
| 111 | + return 0; |
| 112 | +} |
| 113 | + |
| 114 | +static int gpio_aesc_port_clear_bits_raw(const struct device *dev, |
| 115 | + gpio_port_pins_t mask) |
| 116 | +{ |
| 117 | + volatile struct gpio_aesc_regs *gpio = DEV_GPIO(dev); |
| 118 | + struct gpio_aesc_data *data = DEV_DATA(dev); |
| 119 | + k_spinlock_key_t key; |
| 120 | + |
| 121 | + key = k_spin_lock(&data->lock); |
| 122 | + gpio->write &= ~mask; |
| 123 | + k_spin_unlock(&data->lock, key); |
| 124 | + |
| 125 | + return 0; |
| 126 | +} |
| 127 | + |
| 128 | +static int gpio_aesc_port_toggle_bits(const struct device *dev, |
| 129 | + gpio_port_pins_t mask) |
| 130 | +{ |
| 131 | + volatile struct gpio_aesc_regs *gpio = DEV_GPIO(dev); |
| 132 | + struct gpio_aesc_data *data = DEV_DATA(dev); |
| 133 | + k_spinlock_key_t key; |
| 134 | + |
| 135 | + key = k_spin_lock(&data->lock); |
| 136 | + gpio->write ^= mask; |
| 137 | + k_spin_unlock(&data->lock, key); |
| 138 | + |
| 139 | + return 0; |
| 140 | +} |
| 141 | + |
| 142 | +static int gpio_aesc_init(const struct device *dev) |
| 143 | +{ |
| 144 | + volatile uintptr_t *base_addr = (volatile uintptr_t *)DEV_GPIO(dev); |
| 145 | + volatile struct gpio_aesc_regs *gpio; |
| 146 | + |
| 147 | + DEVICE_MMIO_MAP(dev, K_MEM_CACHE_NONE); |
| 148 | + LOG_DBG("IP core version: %i.%i.%i.", |
| 149 | + ip_id_get_major_version(base_addr), |
| 150 | + ip_id_get_minor_version(base_addr), |
| 151 | + ip_id_get_patchlevel(base_addr) |
| 152 | + ); |
| 153 | + DEVICE_MMIO_GET(dev) = ip_id_relocate_driver(base_addr); |
| 154 | + LOG_DBG("Relocate driver to address 0x%lx.", DEVICE_MMIO_GET(dev)); |
| 155 | + gpio = DEV_GPIO(dev); |
| 156 | + |
| 157 | + gpio->high_ie = 0; |
| 158 | + gpio->low_ie = 0; |
| 159 | + gpio->rise_ie = 0; |
| 160 | + gpio->fall_ie = 0; |
| 161 | + |
| 162 | + return 0; |
| 163 | +} |
| 164 | + |
| 165 | +static DEVICE_API(gpio, gpio_aesc_driver_api) = { |
| 166 | + .pin_configure = gpio_aesc_config, |
| 167 | + .port_get_raw = gpio_aesc_port_get_raw, |
| 168 | + .port_set_masked_raw = gpio_aesc_port_set_masked_raw, |
| 169 | + .port_set_bits_raw = gpio_aesc_port_set_bits_raw, |
| 170 | + .port_clear_bits_raw = gpio_aesc_port_clear_bits_raw, |
| 171 | + .port_toggle_bits = gpio_aesc_port_toggle_bits, |
| 172 | +}; |
| 173 | + |
| 174 | +#define AESC_GPIO_INIT(no) \ |
| 175 | + static struct gpio_aesc_data gpio_aesc_dev_data_##no; \ |
| 176 | + static struct gpio_aesc_config gpio_aesc_dev_cfg_##no = { \ |
| 177 | + DEVICE_MMIO_ROM_INIT(DT_DRV_INST(no)), \ |
| 178 | + }; \ |
| 179 | + DEVICE_DT_INST_DEFINE(no, \ |
| 180 | + gpio_aesc_init, \ |
| 181 | + NULL, \ |
| 182 | + &gpio_aesc_dev_data_##no, \ |
| 183 | + &gpio_aesc_dev_cfg_##no, \ |
| 184 | + PRE_KERNEL_2, \ |
| 185 | + CONFIG_GPIO_INIT_PRIORITY, \ |
| 186 | + (void *)&gpio_aesc_driver_api); |
| 187 | + |
| 188 | +DT_INST_FOREACH_STATUS_OKAY(AESC_GPIO_INIT) |
0 commit comments