2017-08-15 14:05:10 -05:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017, NXP
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2020-03-24 14:11:52 -05:00
|
|
|
#define DT_DRV_COMPAT nxp_imx_gpio
|
|
|
|
|
2017-08-15 14:05:10 -05:00
|
|
|
#include <errno.h>
|
|
|
|
#include <device.h>
|
2019-06-25 15:53:52 -04:00
|
|
|
#include <drivers/gpio.h>
|
2017-08-15 14:05:10 -05:00
|
|
|
#include <soc.h>
|
|
|
|
#include <fsl_common.h>
|
2018-02-26 16:23:40 -06:00
|
|
|
#include <fsl_gpio.h>
|
2017-08-15 14:05:10 -05:00
|
|
|
|
|
|
|
#include "gpio_utils.h"
|
|
|
|
|
|
|
|
struct mcux_igpio_config {
|
2019-12-11 10:51:24 -06:00
|
|
|
/* gpio_driver_config needs to be first */
|
|
|
|
struct gpio_driver_config common;
|
2017-08-15 14:05:10 -05:00
|
|
|
GPIO_Type *base;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct mcux_igpio_data {
|
2019-09-17 17:55:33 -05:00
|
|
|
/* gpio_driver_data needs to be first */
|
|
|
|
struct gpio_driver_data general;
|
2017-08-15 14:05:10 -05:00
|
|
|
/* port ISR callback routine address */
|
|
|
|
sys_slist_t callbacks;
|
|
|
|
};
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int mcux_igpio_configure(const struct device *dev,
|
2020-01-30 12:12:39 -06:00
|
|
|
gpio_pin_t pin, gpio_flags_t flags)
|
2017-08-15 14:05:10 -05:00
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct mcux_igpio_config *config = dev->config;
|
2019-09-17 17:55:33 -05:00
|
|
|
GPIO_Type *base = config->base;
|
2017-08-15 14:05:10 -05:00
|
|
|
|
2019-09-17 17:55:33 -05:00
|
|
|
if (((flags & GPIO_INPUT) != 0) && ((flags & GPIO_OUTPUT) != 0)) {
|
|
|
|
return -ENOTSUP;
|
2017-08-15 14:05:10 -05:00
|
|
|
}
|
|
|
|
|
2019-09-17 17:55:33 -05:00
|
|
|
if ((flags & GPIO_SINGLE_ENDED) != 0) {
|
|
|
|
return -ENOTSUP;
|
2017-08-15 14:05:10 -05:00
|
|
|
}
|
|
|
|
|
2019-09-17 17:55:33 -05:00
|
|
|
if (((flags & GPIO_PULL_UP) != 0) || ((flags & GPIO_PULL_DOWN) != 0)) {
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & GPIO_OUTPUT_INIT_HIGH) {
|
|
|
|
base->DR_SET = BIT(pin);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & GPIO_OUTPUT_INIT_LOW) {
|
|
|
|
base->DR_CLEAR = BIT(pin);
|
2017-08-15 14:05:10 -05:00
|
|
|
}
|
|
|
|
|
2019-09-17 17:55:33 -05:00
|
|
|
WRITE_BIT(base->GDIR, pin, flags & GPIO_OUTPUT);
|
|
|
|
|
2017-08-15 14:05:10 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int mcux_igpio_port_get_raw(const struct device *dev, uint32_t *value)
|
2019-09-17 17:55:33 -05:00
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct mcux_igpio_config *config = dev->config;
|
2019-09-17 17:55:33 -05:00
|
|
|
GPIO_Type *base = config->base;
|
|
|
|
|
|
|
|
*value = base->DR;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int mcux_igpio_port_set_masked_raw(const struct device *dev,
|
|
|
|
uint32_t mask,
|
|
|
|
uint32_t value)
|
2019-09-17 17:55:33 -05:00
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct mcux_igpio_config *config = dev->config;
|
2019-09-17 17:55:33 -05:00
|
|
|
GPIO_Type *base = config->base;
|
|
|
|
|
|
|
|
base->DR = (base->DR & ~mask) | (mask & value);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int mcux_igpio_port_set_bits_raw(const struct device *dev,
|
|
|
|
uint32_t mask)
|
2019-09-17 17:55:33 -05:00
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct mcux_igpio_config *config = dev->config;
|
2019-09-17 17:55:33 -05:00
|
|
|
GPIO_Type *base = config->base;
|
|
|
|
|
|
|
|
base->DR_SET = mask;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int mcux_igpio_port_clear_bits_raw(const struct device *dev,
|
|
|
|
uint32_t mask)
|
2019-09-17 17:55:33 -05:00
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct mcux_igpio_config *config = dev->config;
|
2019-09-17 17:55:33 -05:00
|
|
|
GPIO_Type *base = config->base;
|
|
|
|
|
|
|
|
base->DR_CLEAR = mask;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int mcux_igpio_port_toggle_bits(const struct device *dev,
|
|
|
|
uint32_t mask)
|
2019-09-17 17:55:33 -05:00
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct mcux_igpio_config *config = dev->config;
|
2019-09-17 17:55:33 -05:00
|
|
|
GPIO_Type *base = config->base;
|
|
|
|
|
|
|
|
base->DR_TOGGLE = mask;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int mcux_igpio_pin_interrupt_configure(const struct device *dev,
|
|
|
|
gpio_pin_t pin,
|
|
|
|
enum gpio_int_mode mode,
|
|
|
|
enum gpio_int_trig trig)
|
2019-09-17 17:55:33 -05:00
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct mcux_igpio_config *config = dev->config;
|
2019-09-17 17:55:33 -05:00
|
|
|
GPIO_Type *base = config->base;
|
|
|
|
unsigned int key;
|
2020-05-27 11:26:57 -05:00
|
|
|
uint8_t icr;
|
2019-09-17 17:55:33 -05:00
|
|
|
int shift;
|
|
|
|
|
2020-03-18 22:23:34 +08:00
|
|
|
if (mode == GPIO_INT_MODE_DISABLED) {
|
|
|
|
key = irq_lock();
|
|
|
|
|
|
|
|
WRITE_BIT(base->IMR, pin, 0);
|
|
|
|
|
|
|
|
irq_unlock(key);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-09-17 17:55:33 -05:00
|
|
|
if ((mode == GPIO_INT_MODE_EDGE) && (trig == GPIO_INT_TRIG_LOW)) {
|
|
|
|
icr = 3;
|
|
|
|
} else if ((mode == GPIO_INT_MODE_EDGE) &&
|
|
|
|
(trig == GPIO_INT_TRIG_HIGH)) {
|
|
|
|
icr = 2;
|
|
|
|
} else if ((mode == GPIO_INT_MODE_LEVEL) &&
|
|
|
|
(trig == GPIO_INT_TRIG_HIGH)) {
|
|
|
|
icr = 1;
|
|
|
|
} else {
|
|
|
|
icr = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pin < 16) {
|
|
|
|
shift = 2 * pin;
|
|
|
|
base->ICR1 = (base->ICR1 & ~(3 << shift)) | (icr << shift);
|
|
|
|
} else if (pin < 32) {
|
|
|
|
shift = 2 * (pin - 16);
|
|
|
|
base->ICR2 = (base->ICR2 & ~(3 << shift)) | (icr << shift);
|
|
|
|
} else {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
key = irq_lock();
|
|
|
|
|
|
|
|
WRITE_BIT(base->EDGE_SEL, pin, trig == GPIO_INT_TRIG_BOTH);
|
2020-03-18 22:23:34 +08:00
|
|
|
WRITE_BIT(base->ISR, pin, 1);
|
|
|
|
WRITE_BIT(base->IMR, pin, 1);
|
2019-09-17 17:55:33 -05:00
|
|
|
|
|
|
|
irq_unlock(key);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int mcux_igpio_manage_callback(const struct device *dev,
|
|
|
|
struct gpio_callback *callback,
|
|
|
|
bool set)
|
2017-08-15 14:05:10 -05:00
|
|
|
{
|
2020-05-28 21:23:02 +02:00
|
|
|
struct mcux_igpio_data *data = dev->data;
|
2017-08-15 14:05:10 -05:00
|
|
|
|
2019-03-12 15:15:42 -06:00
|
|
|
return gpio_manage_callback(&data->callbacks, callback, set);
|
2017-08-15 14:05:10 -05: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 mcux_igpio_port_isr(const struct device *dev)
|
2017-08-15 14:05:10 -05:00
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct mcux_igpio_config *config = dev->config;
|
2020-05-28 21:23:02 +02:00
|
|
|
struct mcux_igpio_data *data = dev->data;
|
2019-09-17 17:55:33 -05:00
|
|
|
GPIO_Type *base = config->base;
|
2020-06-13 02:07:29 +02:00
|
|
|
uint32_t int_flags;
|
2017-08-15 14:05:10 -05:00
|
|
|
|
2019-09-17 17:55:33 -05:00
|
|
|
int_flags = base->ISR;
|
2020-06-13 02:07:29 +02:00
|
|
|
base->ISR = int_flags;
|
2017-08-15 14:05:10 -05:00
|
|
|
|
2020-06-13 02:07:29 +02:00
|
|
|
gpio_fire_callbacks(&data->callbacks, dev, int_flags);
|
2017-08-15 14:05:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct gpio_driver_api mcux_igpio_driver_api = {
|
2020-01-30 12:12:39 -06:00
|
|
|
.pin_configure = mcux_igpio_configure,
|
2019-09-17 17:55:33 -05:00
|
|
|
.port_get_raw = mcux_igpio_port_get_raw,
|
|
|
|
.port_set_masked_raw = mcux_igpio_port_set_masked_raw,
|
|
|
|
.port_set_bits_raw = mcux_igpio_port_set_bits_raw,
|
|
|
|
.port_clear_bits_raw = mcux_igpio_port_clear_bits_raw,
|
|
|
|
.port_toggle_bits = mcux_igpio_port_toggle_bits,
|
|
|
|
.pin_interrupt_configure = mcux_igpio_pin_interrupt_configure,
|
2017-08-15 14:05:10 -05:00
|
|
|
.manage_callback = mcux_igpio_manage_callback,
|
|
|
|
};
|
|
|
|
|
2020-04-14 10:38:37 -05:00
|
|
|
#define MCUX_IGPIO_IRQ_INIT(n, i) \
|
|
|
|
do { \
|
|
|
|
IRQ_CONNECT(DT_INST_IRQ_BY_IDX(n, i, irq), \
|
|
|
|
DT_INST_IRQ_BY_IDX(n, i, priority), \
|
|
|
|
mcux_igpio_port_isr, \
|
2020-12-10 10:20:42 -06:00
|
|
|
DEVICE_DT_INST_GET(n), 0); \
|
2020-04-14 10:38:37 -05:00
|
|
|
\
|
|
|
|
irq_enable(DT_INST_IRQ_BY_IDX(n, i, irq)); \
|
2020-04-22 14:49:39 +02:00
|
|
|
} while (0)
|
2020-04-14 10:38:37 -05:00
|
|
|
|
|
|
|
#define MCUX_IGPIO_INIT(n) \
|
2020-07-14 17:02:00 +02:00
|
|
|
static int mcux_igpio_##n##_init(const struct device *dev); \
|
2020-04-14 10:38:37 -05:00
|
|
|
\
|
|
|
|
static const struct mcux_igpio_config mcux_igpio_##n##_config = {\
|
|
|
|
.common = { \
|
|
|
|
.port_pin_mask = GPIO_PORT_PIN_MASK_FROM_DT_INST(n),\
|
|
|
|
}, \
|
|
|
|
.base = (GPIO_Type *)DT_INST_REG_ADDR(n), \
|
|
|
|
}; \
|
|
|
|
\
|
|
|
|
static struct mcux_igpio_data mcux_igpio_##n##_data; \
|
|
|
|
\
|
2020-12-10 10:20:42 -06:00
|
|
|
DEVICE_DT_INST_DEFINE(n, \
|
2020-04-14 10:38:37 -05:00
|
|
|
mcux_igpio_##n##_init, \
|
2020-12-10 10:20:42 -06:00
|
|
|
device_pm_control_nop, \
|
2020-04-14 10:38:37 -05:00
|
|
|
&mcux_igpio_##n##_data, \
|
|
|
|
&mcux_igpio_##n##_config, \
|
|
|
|
POST_KERNEL, \
|
|
|
|
CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, \
|
|
|
|
&mcux_igpio_driver_api); \
|
|
|
|
\
|
2020-07-14 17:02:00 +02:00
|
|
|
static int mcux_igpio_##n##_init(const struct device *dev) \
|
2020-04-14 10:38:37 -05:00
|
|
|
{ \
|
|
|
|
MCUX_IGPIO_IRQ_INIT(n, 0); \
|
|
|
|
\
|
|
|
|
IF_ENABLED(DT_INST_IRQ_HAS_IDX(n, 1), \
|
|
|
|
(MCUX_IGPIO_IRQ_INIT(n, 1);)) \
|
|
|
|
\
|
|
|
|
return 0; \
|
|
|
|
}
|
2017-08-15 14:05:10 -05:00
|
|
|
|
2020-05-06 11:23:07 -07:00
|
|
|
DT_INST_FOREACH_STATUS_OKAY(MCUX_IGPIO_INIT)
|