Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | /*
* Copyright (c) 2016 Linaro Limited.
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT arm_cmsdk_gpio
#include <kernel.h>
#include <device.h>
#include <errno.h>
#include <drivers/gpio.h>
#include <init.h>
#include <soc.h>
#include <drivers/clock_control/arm_clock_control.h>
#include "gpio_cmsdk_ahb.h"
#include "gpio_utils.h"
/**
* @brief GPIO driver for ARM CMSDK AHB GPIO
*/
typedef void (*gpio_config_func_t)(const struct device *port);
struct gpio_cmsdk_ahb_cfg {
/* gpio_driver_config needs to be first */
struct gpio_driver_config common;
volatile struct gpio_cmsdk_ahb *port;
gpio_config_func_t gpio_config_func;
/* GPIO Clock control in Active State */
struct arm_clock_control_t gpio_cc_as;
/* GPIO Clock control in Sleep State */
struct arm_clock_control_t gpio_cc_ss;
/* GPIO Clock control in Deep Sleep State */
struct arm_clock_control_t gpio_cc_dss;
};
struct gpio_cmsdk_ahb_dev_data {
/* gpio_driver_data needs to be first */
struct gpio_driver_data common;
/* list of callbacks */
sys_slist_t gpio_cb;
};
static int gpio_cmsdk_ahb_port_get_raw(const struct device *dev,
uint32_t *value)
{
const struct gpio_cmsdk_ahb_cfg * const cfg = dev->config;
*value = cfg->port->data;
return 0;
}
static int gpio_cmsdk_ahb_port_set_masked_raw(const struct device *dev,
uint32_t mask,
uint32_t value)
{
const struct gpio_cmsdk_ahb_cfg * const cfg = dev->config;
cfg->port->dataout = (cfg->port->dataout & ~mask) | (mask & value);
return 0;
}
static int gpio_cmsdk_ahb_port_set_bits_raw(const struct device *dev,
uint32_t mask)
{
const struct gpio_cmsdk_ahb_cfg * const cfg = dev->config;
cfg->port->dataout |= mask;
return 0;
}
static int gpio_cmsdk_ahb_port_clear_bits_raw(const struct device *dev,
uint32_t mask)
{
const struct gpio_cmsdk_ahb_cfg * const cfg = dev->config;
cfg->port->dataout &= ~mask;
return 0;
}
static int gpio_cmsdk_ahb_port_toggle_bits(const struct device *dev,
uint32_t mask)
{
const struct gpio_cmsdk_ahb_cfg * const cfg = dev->config;
cfg->port->dataout ^= mask;
return 0;
}
static int cmsdk_ahb_gpio_config(const struct device *dev, uint32_t mask,
gpio_flags_t flags)
{
const struct gpio_cmsdk_ahb_cfg * const cfg = dev->config;
if (((flags & GPIO_INPUT) == 0) && ((flags & GPIO_OUTPUT) == 0)) {
return -ENOTSUP;
}
if ((flags & (GPIO_PULL_UP | GPIO_PULL_DOWN)) != 0) {
return -ENOTSUP;
}
if ((flags & GPIO_SINGLE_ENDED) != 0) {
return -ENOTSUP;
}
/*
* Setup the pin direction
* Output Enable:
* 0 - Input
* 1 - Output
*/
if ((flags & GPIO_OUTPUT) != 0) {
if ((flags & GPIO_OUTPUT_INIT_HIGH) != 0) {
gpio_cmsdk_ahb_port_set_bits_raw(dev, mask);
} else if ((flags & GPIO_OUTPUT_INIT_LOW) != 0) {
gpio_cmsdk_ahb_port_clear_bits_raw(dev, mask);
}
cfg->port->outenableset = mask;
} else {
cfg->port->outenableclr = mask;
}
cfg->port->altfuncclr = mask;
return 0;
}
/**
* @brief Configure pin or port
*
* @param dev Device struct
* @param pin The pin number
* @param flags Flags of pin or port
*
* @return 0 if successful, failed otherwise
*/
static int gpio_cmsdk_ahb_config(const struct device *dev,
gpio_pin_t pin,
gpio_flags_t flags)
{
return cmsdk_ahb_gpio_config(dev, BIT(pin), flags);
}
static int gpio_cmsdk_ahb_pin_interrupt_configure(const struct device *dev,
gpio_pin_t pin,
enum gpio_int_mode mode,
enum gpio_int_trig trig)
{
const struct gpio_cmsdk_ahb_cfg * const cfg = dev->config;
if (trig == GPIO_INT_TRIG_BOTH) {
return -ENOTSUP;
}
/* For now treat level interrupts as not supported, as we seem to only
* get a single 'edge' still interrupt rather than continuous
* interrupts until the cause is cleared */
if (mode == GPIO_INT_MODE_LEVEL) {
return -ENOTSUP;
}
if (mode == GPIO_INT_MODE_DISABLED) {
cfg->port->intenclr = BIT(pin);
} else {
if (mode == GPIO_INT_MODE_EDGE) {
cfg->port->inttypeset = BIT(pin);
} else {
/* LEVEL */
cfg->port->inttypeclr = BIT(pin);
}
/* Level High or Edge Risising */
if (trig == GPIO_INT_TRIG_HIGH) {
cfg->port->intpolset = BIT(pin);
} else {
cfg->port->intpolclr = BIT(pin);
}
cfg->port->intclear = BIT(pin);
cfg->port->intenset = BIT(pin);
}
return 0;
}
static void gpio_cmsdk_ahb_isr(const struct device *dev)
{
const struct gpio_cmsdk_ahb_cfg * const cfg = dev->config;
struct gpio_cmsdk_ahb_dev_data *data = dev->data;
uint32_t int_stat;
int_stat = cfg->port->intstatus;
/* clear the port interrupts */
cfg->port->intclear = int_stat;
gpio_fire_callbacks(&data->gpio_cb, dev, int_stat);
}
static int gpio_cmsdk_ahb_manage_callback(const struct device *dev,
struct gpio_callback *callback,
bool set)
{
struct gpio_cmsdk_ahb_dev_data *data = dev->data;
return gpio_manage_callback(&data->gpio_cb, callback, set);
}
static const struct gpio_driver_api gpio_cmsdk_ahb_drv_api_funcs = {
.pin_configure = gpio_cmsdk_ahb_config,
.port_get_raw = gpio_cmsdk_ahb_port_get_raw,
.port_set_masked_raw = gpio_cmsdk_ahb_port_set_masked_raw,
.port_set_bits_raw = gpio_cmsdk_ahb_port_set_bits_raw,
.port_clear_bits_raw = gpio_cmsdk_ahb_port_clear_bits_raw,
.port_toggle_bits = gpio_cmsdk_ahb_port_toggle_bits,
.pin_interrupt_configure = gpio_cmsdk_ahb_pin_interrupt_configure,
.manage_callback = gpio_cmsdk_ahb_manage_callback,
};
/**
* @brief Initialization function of GPIO
*
* @param dev Device struct
* @return 0 if successful, failed otherwise.
*/
static int gpio_cmsdk_ahb_init(const struct device *dev)
{
const struct gpio_cmsdk_ahb_cfg * const cfg = dev->config;
#ifdef CONFIG_CLOCK_CONTROL
/* Enable clock for subsystem */
const struct device *clk =
device_get_binding(CONFIG_ARM_CLOCK_CONTROL_DEV_NAME);
#ifdef CONFIG_SOC_SERIES_BEETLE
clock_control_on(clk, (clock_control_subsys_t *) &cfg->gpio_cc_as);
clock_control_off(clk, (clock_control_subsys_t *) &cfg->gpio_cc_ss);
clock_control_off(clk, (clock_control_subsys_t *) &cfg->gpio_cc_dss);
#endif /* CONFIG_SOC_SERIES_BEETLE */
#endif /* CONFIG_CLOCK_CONTROL */
cfg->gpio_config_func(dev);
return 0;
}
#define CMSDK_AHB_GPIO_DEVICE(n) \
static void gpio_cmsdk_port_##n##_config_func(const struct device *dev); \
\
static const struct gpio_cmsdk_ahb_cfg gpio_cmsdk_port_##n##_config = { \
.common = { \
.port_pin_mask = GPIO_PORT_PIN_MASK_FROM_DT_INST(n), \
}, \
.port = ((volatile struct gpio_cmsdk_ahb *)DT_INST_REG_ADDR(n)),\
.gpio_config_func = gpio_cmsdk_port_##n##_config_func, \
.gpio_cc_as = {.bus = CMSDK_AHB, .state = SOC_ACTIVE, \
.device = DT_INST_REG_ADDR(n),}, \
.gpio_cc_ss = {.bus = CMSDK_AHB, .state = SOC_SLEEP, \
.device = DT_INST_REG_ADDR(n),}, \
.gpio_cc_dss = {.bus = CMSDK_AHB, .state = SOC_DEEPSLEEP, \
.device = DT_INST_REG_ADDR(n),}, \
}; \
\
static struct gpio_cmsdk_ahb_dev_data gpio_cmsdk_port_##n##_data; \
\
DEVICE_AND_API_INIT(gpio_cmsdk_port_## n, \
DT_INST_LABEL(n), \
gpio_cmsdk_ahb_init, \
&gpio_cmsdk_port_##n##_data, \
&gpio_cmsdk_port_## n ##_config, \
POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, \
&gpio_cmsdk_ahb_drv_api_funcs); \
\
static void gpio_cmsdk_port_##n##_config_func(const struct device *dev) \
{ \
IRQ_CONNECT(DT_INST_IRQN(n), \
DT_INST_IRQ(n, priority), \
gpio_cmsdk_ahb_isr, \
DEVICE_GET(gpio_cmsdk_port_## n), 0); \
\
irq_enable(DT_INST_IRQN(n)); \
}
DT_INST_FOREACH_STATUS_OKAY(CMSDK_AHB_GPIO_DEVICE)
|