drivers/sensor: lis2dh: use gpio_dt_spec oriented macros
Use gpio_dt_spec structure and related macros for both drdy and AnyMotion interrupts, to have a more compat, readable and safe code. Moreover, skip setting DRDY or AnyMotion trigger from application if the corresponding irq-gpios has not been set. (This commit also fixes #34794) Signed-off-by: Armando Visconti <armando.visconti@st.com>
This commit is contained in:
parent
79788137cf
commit
282a644b0e
3 changed files with 76 additions and 80 deletions
|
@ -332,10 +332,12 @@ int lis2dh_init(const struct device *dev)
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef CONFIG_LIS2DH_TRIGGER
|
#ifdef CONFIG_LIS2DH_TRIGGER
|
||||||
status = lis2dh_init_interrupt(dev);
|
if (cfg->gpio_drdy.port != NULL || cfg->gpio_int.port != NULL) {
|
||||||
if (status < 0) {
|
status = lis2dh_init_interrupt(dev);
|
||||||
LOG_ERR("Failed to initialize interrupts.");
|
if (status < 0) {
|
||||||
return status;
|
LOG_ERR("Failed to initialize interrupts.");
|
||||||
|
return status;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -417,28 +419,16 @@ int lis2dh_init(const struct device *dev)
|
||||||
})
|
})
|
||||||
|
|
||||||
#ifdef CONFIG_LIS2DH_TRIGGER
|
#ifdef CONFIG_LIS2DH_TRIGGER
|
||||||
#define LIS2DH_HAS_IRQ_IDX(inst, idx) \
|
#define GPIO_DT_SPEC_INST_GET_BY_IDX_COND(id, prop, idx) \
|
||||||
DT_INST_PROP_HAS_IDX(inst, irq_gpios, idx)
|
COND_CODE_1(DT_INST_PROP_HAS_IDX(id, prop, idx), \
|
||||||
|
(GPIO_DT_SPEC_INST_GET_BY_IDX(id, prop, idx)), \
|
||||||
|
({.port = NULL, .pin = 0, .dt_flags = 0}))
|
||||||
|
|
||||||
#define LIS2DH_CFG_INT(inst) \
|
#define LIS2DH_CFG_INT(inst) \
|
||||||
.irq1_dev_name = COND_CODE_1(LIS2DH_HAS_IRQ_IDX(inst, 0), \
|
.gpio_drdy = \
|
||||||
(DT_INST_GPIO_LABEL_BY_IDX(inst, irq_gpios, 0)), \
|
GPIO_DT_SPEC_INST_GET_BY_IDX_COND(inst, irq_gpios, 0), \
|
||||||
(NULL)), \
|
.gpio_int = \
|
||||||
.irq1_pin = COND_CODE_1(LIS2DH_HAS_IRQ_IDX(inst, 0), \
|
GPIO_DT_SPEC_INST_GET_BY_IDX_COND(inst, irq_gpios, 1),
|
||||||
(DT_INST_GPIO_PIN_BY_IDX(inst, irq_gpios, 0)), \
|
|
||||||
(0)), \
|
|
||||||
.irq1_flags = COND_CODE_1(LIS2DH_HAS_IRQ_IDX(inst, 0), \
|
|
||||||
(DT_INST_GPIO_FLAGS_BY_IDX(inst, irq_gpios, 0)), \
|
|
||||||
(0)), \
|
|
||||||
.irq2_dev_name = COND_CODE_1(LIS2DH_HAS_IRQ_IDX(inst, 1), \
|
|
||||||
(DT_INST_GPIO_LABEL_BY_IDX(inst, irq_gpios, 1)), \
|
|
||||||
(NULL)), \
|
|
||||||
.irq2_pin = COND_CODE_1(LIS2DH_HAS_IRQ_IDX(inst, 1), \
|
|
||||||
(DT_INST_GPIO_PIN_BY_IDX(inst, irq_gpios, 1)), \
|
|
||||||
(0)), \
|
|
||||||
.irq2_flags = COND_CODE_1(LIS2DH_HAS_IRQ_IDX(inst, 1), \
|
|
||||||
(DT_INST_GPIO_FLAGS_BY_IDX(inst, irq_gpios, 1)), \
|
|
||||||
(0)),
|
|
||||||
#else
|
#else
|
||||||
#define LIS2DH_CFG_INT(inst)
|
#define LIS2DH_CFG_INT(inst)
|
||||||
#endif /* CONFIG_LIS2DH_TRIGGER */
|
#endif /* CONFIG_LIS2DH_TRIGGER */
|
||||||
|
|
|
@ -193,12 +193,8 @@ struct lis2dh_config {
|
||||||
int (*bus_init)(const struct device *dev);
|
int (*bus_init)(const struct device *dev);
|
||||||
const union lis2dh_bus_cfg bus_cfg;
|
const union lis2dh_bus_cfg bus_cfg;
|
||||||
#ifdef CONFIG_LIS2DH_TRIGGER
|
#ifdef CONFIG_LIS2DH_TRIGGER
|
||||||
const char *irq1_dev_name;
|
const struct gpio_dt_spec gpio_drdy;
|
||||||
gpio_pin_t irq1_pin;
|
const struct gpio_dt_spec gpio_int;
|
||||||
gpio_dt_flags_t irq1_flags;
|
|
||||||
const char *irq2_dev_name;
|
|
||||||
gpio_pin_t irq2_pin;
|
|
||||||
gpio_dt_flags_t irq2_flags;
|
|
||||||
#endif /* CONFIG_LIS2DH_TRIGGER */
|
#endif /* CONFIG_LIS2DH_TRIGGER */
|
||||||
bool is_lsm303agr_dev;
|
bool is_lsm303agr_dev;
|
||||||
bool disc_pull_up;
|
bool disc_pull_up;
|
||||||
|
@ -227,8 +223,6 @@ struct lis2dh_data {
|
||||||
|
|
||||||
#ifdef CONFIG_LIS2DH_TRIGGER
|
#ifdef CONFIG_LIS2DH_TRIGGER
|
||||||
const struct device *dev;
|
const struct device *dev;
|
||||||
const struct device *gpio_int1;
|
|
||||||
const struct device *gpio_int2;
|
|
||||||
struct gpio_callback gpio_int1_cb;
|
struct gpio_callback gpio_int1_cb;
|
||||||
struct gpio_callback gpio_int2_cb;
|
struct gpio_callback gpio_int2_cb;
|
||||||
|
|
||||||
|
|
|
@ -21,23 +21,27 @@ LOG_MODULE_DECLARE(lis2dh, CONFIG_SENSOR_LOG_LEVEL);
|
||||||
static inline void setup_int1(const struct device *dev,
|
static inline void setup_int1(const struct device *dev,
|
||||||
bool enable)
|
bool enable)
|
||||||
{
|
{
|
||||||
struct lis2dh_data *lis2dh = dev->data;
|
|
||||||
const struct lis2dh_config *cfg = dev->config;
|
const struct lis2dh_config *cfg = dev->config;
|
||||||
|
|
||||||
gpio_pin_interrupt_configure(lis2dh->gpio_int1,
|
gpio_pin_interrupt_configure_dt(&cfg->gpio_drdy,
|
||||||
cfg->irq1_pin,
|
enable
|
||||||
enable
|
? GPIO_INT_EDGE_TO_ACTIVE
|
||||||
? GPIO_INT_EDGE_TO_ACTIVE
|
: GPIO_INT_DISABLE);
|
||||||
: GPIO_INT_DISABLE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int lis2dh_trigger_drdy_set(const struct device *dev,
|
static int lis2dh_trigger_drdy_set(const struct device *dev,
|
||||||
enum sensor_channel chan,
|
enum sensor_channel chan,
|
||||||
sensor_trigger_handler_t handler)
|
sensor_trigger_handler_t handler)
|
||||||
{
|
{
|
||||||
|
const struct lis2dh_config *cfg = dev->config;
|
||||||
struct lis2dh_data *lis2dh = dev->data;
|
struct lis2dh_data *lis2dh = dev->data;
|
||||||
int status;
|
int status;
|
||||||
|
|
||||||
|
if (cfg->gpio_drdy.port == NULL) {
|
||||||
|
LOG_ERR("trigger_set DRDY int not supported");
|
||||||
|
return -ENOTSUP;
|
||||||
|
}
|
||||||
|
|
||||||
setup_int1(dev, false);
|
setup_int1(dev, false);
|
||||||
|
|
||||||
/* cancel potentially pending trigger */
|
/* cancel potentially pending trigger */
|
||||||
|
@ -113,23 +117,27 @@ static int lis2dh_start_trigger_int1(const struct device *dev)
|
||||||
static inline void setup_int2(const struct device *dev,
|
static inline void setup_int2(const struct device *dev,
|
||||||
bool enable)
|
bool enable)
|
||||||
{
|
{
|
||||||
struct lis2dh_data *lis2dh = dev->data;
|
|
||||||
const struct lis2dh_config *cfg = dev->config;
|
const struct lis2dh_config *cfg = dev->config;
|
||||||
|
|
||||||
gpio_pin_interrupt_configure(lis2dh->gpio_int2,
|
gpio_pin_interrupt_configure_dt(&cfg->gpio_int,
|
||||||
cfg->irq2_pin,
|
enable
|
||||||
enable
|
? GPIO_INT_EDGE_TO_ACTIVE
|
||||||
? GPIO_INT_EDGE_TO_ACTIVE
|
: GPIO_INT_DISABLE);
|
||||||
: GPIO_INT_DISABLE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int lis2dh_trigger_anym_set(const struct device *dev,
|
static int lis2dh_trigger_anym_set(const struct device *dev,
|
||||||
sensor_trigger_handler_t handler)
|
sensor_trigger_handler_t handler)
|
||||||
{
|
{
|
||||||
|
const struct lis2dh_config *cfg = dev->config;
|
||||||
struct lis2dh_data *lis2dh = dev->data;
|
struct lis2dh_data *lis2dh = dev->data;
|
||||||
int status;
|
int status;
|
||||||
uint8_t reg_val;
|
uint8_t reg_val;
|
||||||
|
|
||||||
|
if (cfg->gpio_int.port == NULL) {
|
||||||
|
LOG_ERR("trigger_set AnyMotion int not supported");
|
||||||
|
return -ENOTSUP;
|
||||||
|
}
|
||||||
|
|
||||||
setup_int2(dev, false);
|
setup_int2(dev, false);
|
||||||
|
|
||||||
/* cancel potentially pending trigger */
|
/* cancel potentially pending trigger */
|
||||||
|
@ -172,23 +180,10 @@ int lis2dh_trigger_set(const struct device *dev,
|
||||||
const struct sensor_trigger *trig,
|
const struct sensor_trigger *trig,
|
||||||
sensor_trigger_handler_t handler)
|
sensor_trigger_handler_t handler)
|
||||||
{
|
{
|
||||||
struct lis2dh_data *lis2dh = dev->data;
|
|
||||||
|
|
||||||
if (trig->type == SENSOR_TRIG_DATA_READY &&
|
if (trig->type == SENSOR_TRIG_DATA_READY &&
|
||||||
trig->chan == SENSOR_CHAN_ACCEL_XYZ) {
|
trig->chan == SENSOR_CHAN_ACCEL_XYZ) {
|
||||||
/* If irq_gpio is not configured in DT just return error */
|
|
||||||
if (!lis2dh->gpio_int1) {
|
|
||||||
LOG_ERR("DRDY (INT1) trigger not supported");
|
|
||||||
return -ENOTSUP;
|
|
||||||
}
|
|
||||||
|
|
||||||
return lis2dh_trigger_drdy_set(dev, trig->chan, handler);
|
return lis2dh_trigger_drdy_set(dev, trig->chan, handler);
|
||||||
} else if (trig->type == SENSOR_TRIG_DELTA) {
|
} else if (trig->type == SENSOR_TRIG_DELTA) {
|
||||||
/* If irq_gpio is not configured in DT just return error */
|
|
||||||
if (!lis2dh->gpio_int2) {
|
|
||||||
LOG_ERR("AnyMotion (INT2) trigger not supported");
|
|
||||||
return -ENOTSUP;
|
|
||||||
}
|
|
||||||
return lis2dh_trigger_anym_set(dev, handler);
|
return lis2dh_trigger_anym_set(dev, handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -286,9 +281,10 @@ static void lis2dh_gpio_int2_callback(const struct device *dev,
|
||||||
static void lis2dh_thread_cb(const struct device *dev)
|
static void lis2dh_thread_cb(const struct device *dev)
|
||||||
{
|
{
|
||||||
struct lis2dh_data *lis2dh = dev->data;
|
struct lis2dh_data *lis2dh = dev->data;
|
||||||
|
const struct lis2dh_config *cfg = dev->config;
|
||||||
int status;
|
int status;
|
||||||
|
|
||||||
if (lis2dh->gpio_int1 &&
|
if (cfg->gpio_drdy.port &&
|
||||||
unlikely(atomic_test_and_clear_bit(&lis2dh->trig_flags,
|
unlikely(atomic_test_and_clear_bit(&lis2dh->trig_flags,
|
||||||
START_TRIG_INT1))) {
|
START_TRIG_INT1))) {
|
||||||
status = lis2dh_start_trigger_int1(dev);
|
status = lis2dh_start_trigger_int1(dev);
|
||||||
|
@ -299,7 +295,7 @@ static void lis2dh_thread_cb(const struct device *dev)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lis2dh->gpio_int2 &&
|
if (cfg->gpio_int.port &&
|
||||||
unlikely(atomic_test_and_clear_bit(&lis2dh->trig_flags,
|
unlikely(atomic_test_and_clear_bit(&lis2dh->trig_flags,
|
||||||
START_TRIG_INT2))) {
|
START_TRIG_INT2))) {
|
||||||
status = lis2dh_start_trigger_int2(dev);
|
status = lis2dh_start_trigger_int2(dev);
|
||||||
|
@ -310,7 +306,7 @@ static void lis2dh_thread_cb(const struct device *dev)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lis2dh->gpio_int1 &&
|
if (cfg->gpio_drdy.port &&
|
||||||
atomic_test_and_clear_bit(&lis2dh->trig_flags,
|
atomic_test_and_clear_bit(&lis2dh->trig_flags,
|
||||||
TRIGGED_INT1)) {
|
TRIGGED_INT1)) {
|
||||||
struct sensor_trigger drdy_trigger = {
|
struct sensor_trigger drdy_trigger = {
|
||||||
|
@ -325,7 +321,7 @@ static void lis2dh_thread_cb(const struct device *dev)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lis2dh->gpio_int2 &&
|
if (cfg->gpio_int.port &&
|
||||||
atomic_test_and_clear_bit(&lis2dh->trig_flags,
|
atomic_test_and_clear_bit(&lis2dh->trig_flags,
|
||||||
TRIGGED_INT2)) {
|
TRIGGED_INT2)) {
|
||||||
struct sensor_trigger anym_trigger = {
|
struct sensor_trigger anym_trigger = {
|
||||||
|
@ -385,11 +381,16 @@ int lis2dh_init_interrupt(const struct device *dev)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* setup data ready gpio interrupt */
|
/* setup data ready gpio interrupt */
|
||||||
lis2dh->gpio_int1 = device_get_binding(cfg->irq1_dev_name);
|
if (!device_is_ready(cfg->gpio_drdy.port)) {
|
||||||
if (lis2dh->gpio_int1 == NULL) {
|
/* API may return false even when ptr is NULL */
|
||||||
LOG_INF("Cannot get pointer to irq1_dev_name");
|
if (cfg->gpio_drdy.port != NULL) {
|
||||||
|
LOG_ERR("device %s is not ready", cfg->gpio_drdy.port->name);
|
||||||
|
return -ENODEV;
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG_DBG("gpio_drdy not defined in DT");
|
||||||
status = 0;
|
status = 0;
|
||||||
goto end;
|
goto check_gpio_int;
|
||||||
}
|
}
|
||||||
|
|
||||||
lis2dh->dev = dev;
|
lis2dh->dev = dev;
|
||||||
|
@ -407,56 +408,67 @@ int lis2dh_init_interrupt(const struct device *dev)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* data ready int1 gpio configuration */
|
/* data ready int1 gpio configuration */
|
||||||
status = gpio_pin_configure(lis2dh->gpio_int1, cfg->irq1_pin,
|
status = gpio_pin_configure_dt(&cfg->gpio_drdy, GPIO_INPUT);
|
||||||
GPIO_INPUT | cfg->irq1_flags);
|
|
||||||
if (status < 0) {
|
if (status < 0) {
|
||||||
LOG_ERR("Could not configure gpio %d", cfg->irq1_pin);
|
LOG_ERR("Could not configure %s.%02u",
|
||||||
|
cfg->gpio_drdy.port->name, cfg->gpio_drdy.pin);
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
gpio_init_callback(&lis2dh->gpio_int1_cb,
|
gpio_init_callback(&lis2dh->gpio_int1_cb,
|
||||||
lis2dh_gpio_int1_callback,
|
lis2dh_gpio_int1_callback,
|
||||||
BIT(cfg->irq1_pin));
|
BIT(cfg->gpio_drdy.pin));
|
||||||
|
|
||||||
status = gpio_add_callback(lis2dh->gpio_int1, &lis2dh->gpio_int1_cb);
|
status = gpio_add_callback(cfg->gpio_drdy.port, &lis2dh->gpio_int1_cb);
|
||||||
if (status < 0) {
|
if (status < 0) {
|
||||||
LOG_ERR("Could not add gpio int1 callback");
|
LOG_ERR("Could not add gpio int1 callback");
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_INF("int1 on %s.%02u", cfg->irq1_dev_name, cfg->irq1_pin);
|
LOG_INF("%s: int1 on %s.%02u", dev->name,
|
||||||
|
cfg->gpio_drdy.port->name,
|
||||||
|
cfg->gpio_drdy.pin);
|
||||||
|
|
||||||
|
check_gpio_int:
|
||||||
/*
|
/*
|
||||||
* Setup INT2 (for Any Motion) if defined in DT
|
* Setup INT2 (for Any Motion) if defined in DT
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* setup any motion gpio interrupt */
|
/* setup any motion gpio interrupt */
|
||||||
lis2dh->gpio_int2 = device_get_binding(cfg->irq2_dev_name);
|
if (!device_is_ready(cfg->gpio_int.port)) {
|
||||||
if (lis2dh->gpio_int2 == NULL) {
|
/* API may return false even when ptr is NULL */
|
||||||
LOG_INF("Cannot get pointer to irq2_dev_name");
|
if (cfg->gpio_int.port != NULL) {
|
||||||
|
LOG_ERR("device %s is not ready", cfg->gpio_int.port->name);
|
||||||
|
return -ENODEV;
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG_DBG("gpio_int not defined in DT");
|
||||||
status = 0;
|
status = 0;
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* any motion int2 gpio configuration */
|
/* any motion int2 gpio configuration */
|
||||||
status = gpio_pin_configure(lis2dh->gpio_int2, cfg->irq2_pin,
|
status = gpio_pin_configure_dt(&cfg->gpio_int, GPIO_INPUT);
|
||||||
GPIO_INPUT | cfg->irq2_flags);
|
|
||||||
if (status < 0) {
|
if (status < 0) {
|
||||||
LOG_ERR("Could not configure gpio %d", cfg->irq2_pin);
|
LOG_ERR("Could not configure %s.%02u",
|
||||||
|
cfg->gpio_int.port->name, cfg->gpio_int.pin);
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
gpio_init_callback(&lis2dh->gpio_int2_cb,
|
gpio_init_callback(&lis2dh->gpio_int2_cb,
|
||||||
lis2dh_gpio_int2_callback,
|
lis2dh_gpio_int2_callback,
|
||||||
BIT(cfg->irq2_pin));
|
BIT(cfg->gpio_int.pin));
|
||||||
|
|
||||||
/* callback is going to be enabled by trigger setting function */
|
/* callback is going to be enabled by trigger setting function */
|
||||||
status = gpio_add_callback(lis2dh->gpio_int2, &lis2dh->gpio_int2_cb);
|
status = gpio_add_callback(cfg->gpio_int.port, &lis2dh->gpio_int2_cb);
|
||||||
if (status < 0) {
|
if (status < 0) {
|
||||||
LOG_ERR("Could not add gpio int2 callback (%d)", status);
|
LOG_ERR("Could not add gpio int2 callback (%d)", status);
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_INF("int2 on %s.%02u", cfg->irq2_dev_name, cfg->irq2_pin);
|
LOG_INF("%s: int2 on %s.%02u", dev->name,
|
||||||
|
cfg->gpio_int.port->name,
|
||||||
|
cfg->gpio_int.pin);
|
||||||
|
|
||||||
/* disable interrupt 2 in case of warm (re)boot */
|
/* disable interrupt 2 in case of warm (re)boot */
|
||||||
status = lis2dh->hw_tf->write_reg(dev, LIS2DH_REG_INT2_CFG, 0);
|
status = lis2dh->hw_tf->write_reg(dev, LIS2DH_REG_INT2_CFG, 0);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue