2016-11-24 00:47:20 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016, Texas Instruments Incorporated
|
|
|
|
*
|
2017-01-19 02:01:01 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2016-11-24 00:47:20 +01:00
|
|
|
*/
|
2020-03-25 17:33:47 +01:00
|
|
|
|
|
|
|
#define DT_DRV_COMPAT ti_cc32xx_gpio
|
2016-11-24 00:47:20 +01:00
|
|
|
#include <errno.h>
|
|
|
|
|
2022-05-06 10:25:46 +02:00
|
|
|
#include <zephyr/device.h>
|
|
|
|
#include <zephyr/drivers/gpio.h>
|
|
|
|
#include <zephyr/init.h>
|
|
|
|
#include <zephyr/kernel.h>
|
|
|
|
#include <zephyr/sys/sys_io.h>
|
2016-11-24 00:47:20 +01:00
|
|
|
|
|
|
|
/* Driverlib includes */
|
|
|
|
#include <inc/hw_types.h>
|
|
|
|
#include <inc/hw_memmap.h>
|
|
|
|
#include <inc/hw_ints.h>
|
|
|
|
#include <inc/hw_gpio.h>
|
|
|
|
#include <driverlib/rom.h>
|
|
|
|
#include <driverlib/pin.h>
|
2017-08-15 03:26:39 +02:00
|
|
|
#undef __GPIO_H__ /* Zephyr and CC32XX SDK gpio.h conflict */
|
2016-11-24 00:47:20 +01:00
|
|
|
#include <driverlib/gpio.h>
|
|
|
|
#include <driverlib/rom_map.h>
|
|
|
|
#include <driverlib/interrupt.h>
|
2022-10-17 10:24:11 +02:00
|
|
|
#include <zephyr/irq.h>
|
2016-11-24 00:47:20 +01:00
|
|
|
|
2022-10-21 20:18:01 +02:00
|
|
|
#include <zephyr/drivers/gpio/gpio_utils.h>
|
2016-11-24 00:47:20 +01:00
|
|
|
|
2019-09-25 22:54:43 +02:00
|
|
|
/* Reserved */
|
|
|
|
#define PIN_XX 0xFF
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
static const uint8_t pinTable[] = {
|
2019-09-25 22:54:43 +02:00
|
|
|
/* 00 01 02 03 04 05 06 07 */
|
|
|
|
PIN_50, PIN_55, PIN_57, PIN_58, PIN_59, PIN_60, PIN_61, PIN_62,
|
|
|
|
/* 08 09 10 11 12 13 14 15 */
|
|
|
|
PIN_63, PIN_64, PIN_01, PIN_02, PIN_03, PIN_04, PIN_05, PIN_06,
|
|
|
|
/* 16 17 18 19 20 21 22 23 */
|
|
|
|
PIN_07, PIN_08, PIN_XX, PIN_XX, PIN_XX, PIN_XX, PIN_15, PIN_16,
|
|
|
|
/* 24 25 26 27 28 29 30 31 */
|
|
|
|
PIN_17, PIN_21, PIN_29, PIN_30, PIN_18, PIN_20, PIN_53, PIN_45,
|
|
|
|
/* 32 */
|
|
|
|
PIN_52
|
|
|
|
};
|
|
|
|
|
2016-11-24 00:47:20 +01:00
|
|
|
struct gpio_cc32xx_config {
|
2019-12-03 20:02:34 +01:00
|
|
|
/* gpio_driver_config needs to be first */
|
|
|
|
struct gpio_driver_config common;
|
2016-11-24 00:47:20 +01:00
|
|
|
/* base address of GPIO port */
|
|
|
|
unsigned long port_base;
|
2019-09-25 22:54:43 +02:00
|
|
|
/* GPIO port number */
|
2020-05-27 18:26:57 +02:00
|
|
|
uint8_t port_num;
|
2016-11-24 00:47:20 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct gpio_cc32xx_data {
|
2019-09-20 14:34:34 +02:00
|
|
|
/* gpio_driver_data needs to be first */
|
|
|
|
struct gpio_driver_data common;
|
2016-11-24 00:47:20 +01:00
|
|
|
/* list of registered callbacks */
|
|
|
|
sys_slist_t callbacks;
|
|
|
|
};
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int gpio_cc32xx_port_set_bits_raw(const struct device *port,
|
|
|
|
uint32_t mask);
|
|
|
|
static int gpio_cc32xx_port_clear_bits_raw(const struct device *port,
|
|
|
|
uint32_t mask);
|
2019-09-25 22:54:43 +02:00
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static inline int gpio_cc32xx_config(const struct device *port,
|
2020-01-30 19:12:39 +01:00
|
|
|
gpio_pin_t pin,
|
|
|
|
gpio_flags_t flags)
|
2016-11-24 00:47:20 +01:00
|
|
|
{
|
2022-01-18 16:01:52 +01:00
|
|
|
const struct gpio_cc32xx_config *gpio_config = port->config;
|
2016-11-24 00:47:20 +01:00
|
|
|
unsigned long port_base = gpio_config->port_base;
|
|
|
|
|
2019-09-25 22:54:43 +02:00
|
|
|
if (((flags & GPIO_INPUT) != 0) && ((flags & GPIO_OUTPUT) != 0)) {
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
2016-11-24 00:47:20 +01:00
|
|
|
|
2019-09-25 22:54:43 +02:00
|
|
|
if ((flags & (GPIO_INPUT | GPIO_OUTPUT)) == 0) {
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((flags & (GPIO_PULL_UP | GPIO_PULL_DOWN)) != 0) {
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
MAP_PinTypeGPIO(pinTable[gpio_config->port_num * 8 + pin],
|
|
|
|
PIN_MODE_0, false);
|
|
|
|
if (flags & GPIO_OUTPUT) {
|
|
|
|
MAP_GPIODirModeSet(port_base, (1 << pin), GPIO_DIR_MODE_OUT);
|
|
|
|
if ((flags & GPIO_OUTPUT_INIT_HIGH) != 0) {
|
|
|
|
gpio_cc32xx_port_set_bits_raw(port, BIT(pin));
|
|
|
|
} else if ((flags & GPIO_OUTPUT_INIT_LOW) != 0) {
|
|
|
|
gpio_cc32xx_port_clear_bits_raw(port, BIT(pin));
|
2016-11-24 00:47:20 +01:00
|
|
|
}
|
|
|
|
} else {
|
2019-09-25 22:54:43 +02:00
|
|
|
MAP_GPIODirModeSet(port_base, (1 << pin), GPIO_DIR_MODE_IN);
|
2016-11-24 00:47:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int gpio_cc32xx_port_get_raw(const struct device *port,
|
|
|
|
uint32_t *value)
|
2019-09-25 22:54:43 +02:00
|
|
|
{
|
2022-01-18 16:01:52 +01:00
|
|
|
const struct gpio_cc32xx_config *gpio_config = port->config;
|
2019-09-25 22:54:43 +02:00
|
|
|
unsigned long port_base = gpio_config->port_base;
|
|
|
|
unsigned char pin_packed = 0xFF;
|
|
|
|
|
|
|
|
*value = MAP_GPIOPinRead(port_base, pin_packed);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int gpio_cc32xx_port_set_masked_raw(const struct device *port,
|
|
|
|
uint32_t mask,
|
|
|
|
uint32_t value)
|
2019-09-25 22:54:43 +02:00
|
|
|
{
|
2022-01-18 16:01:52 +01:00
|
|
|
const struct gpio_cc32xx_config *gpio_config = port->config;
|
2019-09-25 22:54:43 +02:00
|
|
|
unsigned long port_base = gpio_config->port_base;
|
|
|
|
|
|
|
|
MAP_GPIOPinWrite(port_base, (unsigned char)mask, (unsigned char)value);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int gpio_cc32xx_port_set_bits_raw(const struct device *port,
|
|
|
|
uint32_t mask)
|
2019-09-25 22:54:43 +02:00
|
|
|
{
|
2022-01-18 16:01:52 +01:00
|
|
|
const struct gpio_cc32xx_config *gpio_config = port->config;
|
2019-09-25 22:54:43 +02:00
|
|
|
unsigned long port_base = gpio_config->port_base;
|
|
|
|
|
|
|
|
MAP_GPIOPinWrite(port_base, (unsigned char)mask, (unsigned char)mask);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int gpio_cc32xx_port_clear_bits_raw(const struct device *port,
|
|
|
|
uint32_t mask)
|
2019-09-25 22:54:43 +02:00
|
|
|
{
|
2022-01-18 16:01:52 +01:00
|
|
|
const struct gpio_cc32xx_config *gpio_config = port->config;
|
2019-09-25 22:54:43 +02:00
|
|
|
unsigned long port_base = gpio_config->port_base;
|
|
|
|
|
|
|
|
MAP_GPIOPinWrite(port_base, (unsigned char)mask, (unsigned char)~mask);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int gpio_cc32xx_port_toggle_bits(const struct device *port,
|
|
|
|
uint32_t mask)
|
2019-09-25 22:54:43 +02:00
|
|
|
{
|
2022-01-18 16:01:52 +01:00
|
|
|
const struct gpio_cc32xx_config *gpio_config = port->config;
|
2019-09-25 22:54:43 +02:00
|
|
|
unsigned long port_base = gpio_config->port_base;
|
|
|
|
long value;
|
|
|
|
|
|
|
|
value = MAP_GPIOPinRead(port_base, mask);
|
|
|
|
|
|
|
|
MAP_GPIOPinWrite(port_base, (unsigned char)mask,
|
|
|
|
(unsigned char)~value);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int gpio_cc32xx_pin_interrupt_configure(const struct device *port,
|
|
|
|
gpio_pin_t pin,
|
|
|
|
enum gpio_int_mode mode,
|
|
|
|
enum gpio_int_trig trig)
|
2019-09-25 22:54:43 +02:00
|
|
|
{
|
2022-01-18 16:01:52 +01:00
|
|
|
const struct gpio_cc32xx_config *gpio_config = port->config;
|
2019-09-25 22:54:43 +02:00
|
|
|
unsigned long port_base = gpio_config->port_base;
|
|
|
|
unsigned long int_type;
|
|
|
|
|
2019-10-08 00:27:37 +02:00
|
|
|
__ASSERT(pin < 8, "Invalid pin number - only 8 pins per port");
|
|
|
|
|
2020-02-15 01:33:46 +01:00
|
|
|
/*
|
|
|
|
* disable interrupt prior to changing int type helps
|
|
|
|
* prevent spurious interrupts observed when switching
|
|
|
|
* to level-based
|
|
|
|
*/
|
|
|
|
MAP_GPIOIntDisable(port_base, (1 << pin));
|
|
|
|
|
2019-09-25 22:54:43 +02:00
|
|
|
if (mode != GPIO_INT_MODE_DISABLED) {
|
|
|
|
if (mode == GPIO_INT_MODE_EDGE) {
|
|
|
|
if (trig == GPIO_INT_TRIG_BOTH) {
|
|
|
|
int_type = GPIO_BOTH_EDGES;
|
|
|
|
} else if (trig == GPIO_INT_TRIG_HIGH) {
|
|
|
|
int_type = GPIO_RISING_EDGE;
|
|
|
|
} else {
|
|
|
|
int_type = GPIO_FALLING_EDGE;
|
|
|
|
}
|
2020-01-29 15:22:44 +01:00
|
|
|
} else { /* GPIO_INT_MODE_LEVEL */
|
2019-09-25 22:54:43 +02:00
|
|
|
if (trig == GPIO_INT_TRIG_HIGH) {
|
|
|
|
int_type = GPIO_HIGH_LEVEL;
|
|
|
|
} else {
|
|
|
|
int_type = GPIO_LOW_LEVEL;
|
|
|
|
}
|
|
|
|
}
|
2020-02-15 01:33:46 +01:00
|
|
|
|
2019-09-25 22:54:43 +02:00
|
|
|
MAP_GPIOIntTypeSet(port_base, (1 << pin), int_type);
|
|
|
|
MAP_GPIOIntClear(port_base, (1 << pin));
|
|
|
|
MAP_GPIOIntEnable(port_base, (1 << pin));
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int gpio_cc32xx_manage_callback(const struct device *dev,
|
|
|
|
struct gpio_callback *callback,
|
|
|
|
bool set)
|
2016-11-24 00:47:20 +01:00
|
|
|
{
|
2022-01-18 16:01:52 +01:00
|
|
|
struct gpio_cc32xx_data *data = dev->data;
|
2016-11-24 00:47:20 +01:00
|
|
|
|
2019-03-12 22:15:42 +01:00
|
|
|
return gpio_manage_callback(&data->callbacks, callback, set);
|
2016-11-24 00:47:20 +01:00
|
|
|
}
|
|
|
|
|
isr: Normalize usage of device instance through ISR
The goal of this patch is to replace the 'void *' parameter by 'struct
device *' if they use such variable or just 'const void *' on all
relevant ISRs
This will avoid not-so-nice const qualifier tweaks when device instances
will be constant.
Note that only the ISR passed to IRQ_CONNECT are of interest here.
In order to do so, the script fix_isr.py below is necessary:
from pathlib import Path
import subprocess
import pickle
import mmap
import sys
import re
import os
cocci_template = """
@r_fix_isr_0
@
type ret_type;
identifier P;
identifier D;
@@
-ret_type <!fn!>(void *P)
+ret_type <!fn!>(const struct device *P)
{
...
(
const struct device *D = (const struct device *)P;
|
const struct device *D = P;
)
...
}
@r_fix_isr_1
@
type ret_type;
identifier P;
identifier D;
@@
-ret_type <!fn!>(void *P)
+ret_type <!fn!>(const struct device *P)
{
...
const struct device *D;
...
(
D = (const struct device *)P;
|
D = P;
)
...
}
@r_fix_isr_2
@
type ret_type;
identifier A;
@@
-ret_type <!fn!>(void *A)
+ret_type <!fn!>(const void *A)
{
...
}
@r_fix_isr_3
@
const struct device *D;
@@
-<!fn!>((void *)D);
+<!fn!>(D);
@r_fix_isr_4
@
type ret_type;
identifier D;
identifier P;
@@
-ret_type <!fn!>(const struct device *P)
+ret_type <!fn!>(const struct device *D)
{
...
(
-const struct device *D = (const struct device *)P;
|
-const struct device *D = P;
)
...
}
@r_fix_isr_5
@
type ret_type;
identifier D;
identifier P;
@@
-ret_type <!fn!>(const struct device *P)
+ret_type <!fn!>(const struct device *D)
{
...
-const struct device *D;
...
(
-D = (const struct device *)P;
|
-D = P;
)
...
}
"""
def find_isr(fn):
db = []
data = None
start = 0
try:
with open(fn, 'r+') as f:
data = str(mmap.mmap(f.fileno(), 0).read())
except Exception as e:
return db
while True:
isr = ""
irq = data.find('IRQ_CONNECT', start)
while irq > -1:
p = 1
arg = 1
p_o = data.find('(', irq)
if p_o < 0:
irq = -1
break;
pos = p_o + 1
while p > 0:
if data[pos] == ')':
p -= 1
elif data[pos] == '(':
p += 1
elif data[pos] == ',' and p == 1:
arg += 1
if arg == 3:
isr += data[pos]
pos += 1
isr = isr.strip(',\\n\\t ')
if isr not in db and len(isr) > 0:
db.append(isr)
start = pos
break
if irq < 0:
break
return db
def patch_isr(fn, isr_list):
if len(isr_list) <= 0:
return
for isr in isr_list:
tmplt = cocci_template.replace('<!fn!>', isr)
with open('/tmp/isr_fix.cocci', 'w') as f:
f.write(tmplt)
cmd = ['spatch', '--sp-file', '/tmp/isr_fix.cocci', '--in-place', fn]
subprocess.run(cmd)
def process_files(path):
if path.is_file() and path.suffix in ['.h', '.c']:
p = str(path.parent) + '/' + path.name
isr_list = find_isr(p)
patch_isr(p, isr_list)
elif path.is_dir():
for p in path.iterdir():
process_files(p)
if len(sys.argv) < 2:
print("You need to provide a dir/file path")
sys.exit(1)
process_files(Path(sys.argv[1]))
And is run: ./fix_isr.py <zephyr root directory>
Finally, some files needed manual fixes such.
Fixes #27399
Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2020-06-17 14:58:56 +02:00
|
|
|
static void gpio_cc32xx_port_isr(const struct device *dev)
|
2016-11-24 00:47:20 +01:00
|
|
|
{
|
2022-01-18 16:01:52 +01:00
|
|
|
const struct gpio_cc32xx_config *config = dev->config;
|
|
|
|
struct gpio_cc32xx_data *data = dev->data;
|
2020-06-13 02:07:29 +02:00
|
|
|
uint32_t int_status;
|
2016-11-24 00:47:20 +01:00
|
|
|
|
|
|
|
/* See which interrupts triggered: */
|
2020-06-13 02:07:29 +02:00
|
|
|
int_status = (uint32_t)MAP_GPIOIntStatus(config->port_base, 1);
|
2016-11-24 00:47:20 +01:00
|
|
|
|
2020-02-15 01:24:55 +01:00
|
|
|
/* Clear GPIO Interrupt */
|
2016-11-24 00:47:20 +01:00
|
|
|
MAP_GPIOIntClear(config->port_base, int_status);
|
|
|
|
|
|
|
|
/* Call the registered callbacks */
|
2020-06-13 02:07:29 +02:00
|
|
|
gpio_fire_callbacks(&data->callbacks, dev, int_status);
|
2016-11-24 00:47:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct gpio_driver_api api_funcs = {
|
2020-01-30 19:12:39 +01:00
|
|
|
.pin_configure = gpio_cc32xx_config,
|
2019-09-25 22:54:43 +02:00
|
|
|
.port_get_raw = gpio_cc32xx_port_get_raw,
|
|
|
|
.port_set_masked_raw = gpio_cc32xx_port_set_masked_raw,
|
|
|
|
.port_set_bits_raw = gpio_cc32xx_port_set_bits_raw,
|
|
|
|
.port_clear_bits_raw = gpio_cc32xx_port_clear_bits_raw,
|
|
|
|
.port_toggle_bits = gpio_cc32xx_port_toggle_bits,
|
|
|
|
.pin_interrupt_configure = gpio_cc32xx_pin_interrupt_configure,
|
2016-11-24 00:47:20 +01:00
|
|
|
.manage_callback = gpio_cc32xx_manage_callback,
|
|
|
|
};
|
|
|
|
|
2020-04-24 06:15:38 +02:00
|
|
|
#define GPIO_CC32XX_INIT_FUNC(n) \
|
2020-04-30 20:33:38 +02:00
|
|
|
static int gpio_cc32xx_a##n##_init(const struct device *dev) \
|
2020-04-24 06:15:38 +02:00
|
|
|
{ \
|
|
|
|
ARG_UNUSED(dev); \
|
|
|
|
\
|
|
|
|
IRQ_CONNECT(DT_INST_IRQN(n), DT_INST_IRQ(n, priority), \
|
2020-12-10 17:20:42 +01:00
|
|
|
gpio_cc32xx_port_isr, DEVICE_DT_INST_GET(n), \
|
2020-04-24 06:15:38 +02:00
|
|
|
0); \
|
|
|
|
\
|
|
|
|
MAP_IntPendClear(DT_INST_IRQN(n) + 16); \
|
|
|
|
irq_enable(DT_INST_IRQN(n)); \
|
|
|
|
\
|
|
|
|
return 0; \
|
|
|
|
}
|
2016-11-24 00:47:20 +01:00
|
|
|
|
2020-04-24 06:15:38 +02:00
|
|
|
#define GPIO_CC32XX_DEVICE_INIT(n) \
|
2020-12-10 17:20:42 +01:00
|
|
|
DEVICE_DT_INST_DEFINE(n, &gpio_cc32xx_a##n##_init, \
|
2021-04-28 10:55:48 +02:00
|
|
|
NULL, &gpio_cc32xx_a##n##_data, \
|
2020-04-24 06:15:38 +02:00
|
|
|
&gpio_cc32xx_a##n##_config, \
|
2021-11-05 22:58:21 +01:00
|
|
|
POST_KERNEL, CONFIG_GPIO_INIT_PRIORITY, \
|
2020-04-24 06:15:38 +02:00
|
|
|
&api_funcs)
|
|
|
|
|
|
|
|
#define GPIO_CC32XX_INIT(n) \
|
|
|
|
static const struct gpio_cc32xx_config gpio_cc32xx_a##n##_config = { \
|
|
|
|
.common = { \
|
|
|
|
.port_pin_mask = GPIO_PORT_PIN_MASK_FROM_DT_INST(n), \
|
|
|
|
}, \
|
|
|
|
.port_base = DT_INST_REG_ADDR(n), \
|
|
|
|
.port_num = n \
|
|
|
|
}; \
|
|
|
|
\
|
|
|
|
static struct gpio_cc32xx_data gpio_cc32xx_a##n##_data; \
|
|
|
|
\
|
|
|
|
GPIO_CC32XX_INIT_FUNC(n) \
|
|
|
|
\
|
2020-05-07 21:09:05 +02:00
|
|
|
GPIO_CC32XX_DEVICE_INIT(n);
|
2020-04-24 06:15:38 +02:00
|
|
|
|
2020-05-06 20:23:07 +02:00
|
|
|
DT_INST_FOREACH_STATUS_OKAY(GPIO_CC32XX_INIT)
|