drivers/sensor: lps22hh: Add multi-instance support

This commit aligns lps22hh sensor driver to latest multi
instance sensor driver model.

In particular it makes use of the stmemsc common routines
and move ctx handler inside struct config, so that the
bus_init routines can be totally avoided.

Signed-off-by: Armando Visconti <armando.visconti@st.com>
This commit is contained in:
Armando Visconti 2021-12-13 16:26:11 +01:00 committed by Maureen Helm
commit 177b53a944
6 changed files with 162 additions and 308 deletions

View file

@ -7,6 +7,6 @@
zephyr_library() zephyr_library()
zephyr_library_sources(lps22hh.c) zephyr_library_sources(lps22hh.c)
zephyr_library_sources(lps22hh_i2c.c)
zephyr_library_sources(lps22hh_spi.c)
zephyr_library_sources_ifdef(CONFIG_LPS22HH_TRIGGER lps22hh_trigger.c) zephyr_library_sources_ifdef(CONFIG_LPS22HH_TRIGGER lps22hh_trigger.c)
zephyr_library_include_directories(../stmemsc)

View file

@ -24,25 +24,28 @@ LOG_MODULE_REGISTER(LPS22HH, CONFIG_SENSOR_LOG_LEVEL);
static inline int lps22hh_set_odr_raw(const struct device *dev, uint8_t odr) static inline int lps22hh_set_odr_raw(const struct device *dev, uint8_t odr)
{ {
struct lps22hh_data *data = dev->data; const struct lps22hh_config * const cfg = dev->config;
stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
return lps22hh_data_rate_set(data->ctx, odr); return lps22hh_data_rate_set(ctx, odr);
} }
static int lps22hh_sample_fetch(const struct device *dev, static int lps22hh_sample_fetch(const struct device *dev,
enum sensor_channel chan) enum sensor_channel chan)
{ {
struct lps22hh_data *data = dev->data; struct lps22hh_data *data = dev->data;
const struct lps22hh_config * const cfg = dev->config;
stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
uint32_t raw_press; uint32_t raw_press;
int16_t raw_temp; int16_t raw_temp;
__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL); __ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);
if (lps22hh_pressure_raw_get(data->ctx, &raw_press) < 0) { if (lps22hh_pressure_raw_get(ctx, &raw_press) < 0) {
LOG_DBG("Failed to read sample"); LOG_DBG("Failed to read sample");
return -EIO; return -EIO;
} }
if (lps22hh_temperature_raw_get(data->ctx, &raw_temp) < 0) { if (lps22hh_temperature_raw_get(ctx, &raw_temp) < 0) {
LOG_DBG("Failed to read sample"); LOG_DBG("Failed to read sample");
return -EIO; return -EIO;
} }
@ -140,7 +143,7 @@ static int lps22hh_attr_set(const struct device *dev,
return 0; return 0;
} }
static const struct sensor_driver_api lps22hh_api_funcs = { static const struct sensor_driver_api lps22hh_driver_api = {
.attr_set = lps22hh_attr_set, .attr_set = lps22hh_attr_set,
.sample_fetch = lps22hh_sample_fetch, .sample_fetch = lps22hh_sample_fetch,
.channel_get = lps22hh_channel_get, .channel_get = lps22hh_channel_get,
@ -151,27 +154,32 @@ static const struct sensor_driver_api lps22hh_api_funcs = {
static int lps22hh_init_chip(const struct device *dev) static int lps22hh_init_chip(const struct device *dev)
{ {
struct lps22hh_data *data = dev->data; const struct lps22hh_config * const cfg = dev->config;
stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
uint8_t chip_id; uint8_t chip_id;
int ret;
if (lps22hh_device_id_get(data->ctx, &chip_id) < 0) { if (lps22hh_device_id_get(ctx, &chip_id) < 0) {
LOG_DBG("Failed reading chip id"); LOG_ERR("%s: Not able to read dev id", dev->name);
return -EIO; return -EIO;
} }
if (chip_id != LPS22HH_ID) { if (chip_id != LPS22HH_ID) {
LOG_DBG("Invalid chip id 0x%x", chip_id); LOG_ERR("%s: Invalid chip ID 0x%02x", dev->name, chip_id);
return -EIO; return -EIO;
} }
if (lps22hh_set_odr_raw(dev, CONFIG_LPS22HH_SAMPLING_RATE) < 0) { LOG_DBG("%s: chip id 0x%x", dev->name, chip_id);
LOG_DBG("Failed to set sampling rate");
return -EIO; ret = lps22hh_set_odr_raw(dev, CONFIG_LPS22HH_SAMPLING_RATE);
if (ret < 0) {
LOG_ERR("%s: Failed to set sampling rate", dev->name);
return ret;
} }
if (lps22hh_block_data_update_set(data->ctx, PROPERTY_ENABLE) < 0) { if (lps22hh_block_data_update_set(ctx, PROPERTY_ENABLE) < 0) {
LOG_DBG("Failed to set BDU"); LOG_ERR("%s: Failed to set BDU", dev->name);
return -EIO; return ret;
} }
return 0; return 0;
@ -179,19 +187,6 @@ static int lps22hh_init_chip(const struct device *dev)
static int lps22hh_init(const struct device *dev) static int lps22hh_init(const struct device *dev)
{ {
const struct lps22hh_config * const config = dev->config;
struct lps22hh_data *data = dev->data;
data->dev = dev;
data->bus = device_get_binding(config->master_dev_name);
if (!data->bus) {
LOG_DBG("bus master not found: %s", config->master_dev_name);
return -EINVAL;
}
config->bus_init(dev);
if (lps22hh_init_chip(dev) < 0) { if (lps22hh_init_chip(dev) < 0) {
LOG_DBG("Failed to initialize chip"); LOG_DBG("Failed to initialize chip");
return -EIO; return -EIO;
@ -207,38 +202,79 @@ static int lps22hh_init(const struct device *dev)
return 0; return 0;
} }
static struct lps22hh_data lps22hh_data; #if DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT) == 0
#warning "LPS22HH driver enabled without any devices"
#endif
/*
* Instantiation macros used when a device is on a SPI bus.
*/
static const struct lps22hh_config lps22hh_config = {
.master_dev_name = DT_INST_BUS_LABEL(0),
#ifdef CONFIG_LPS22HH_TRIGGER #ifdef CONFIG_LPS22HH_TRIGGER
.drdy_port = DT_INST_GPIO_LABEL(0, drdy_gpios), #define LPS22HH_CFG_IRQ(inst) \
.drdy_pin = DT_INST_GPIO_PIN(0, drdy_gpios), .gpio_int = GPIO_DT_SPEC_INST_GET(inst, drdy_gpios),
.drdy_flags = DT_INST_GPIO_FLAGS(0, drdy_gpios),
#endif
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
.bus_init = lps22hh_spi_init,
.spi_conf.frequency = DT_INST_PROP(0, spi_max_frequency),
.spi_conf.operation = (SPI_OP_MODE_MASTER | SPI_MODE_CPOL |
SPI_MODE_CPHA | SPI_WORD_SET(8)),
.spi_conf.slave = DT_INST_REG_ADDR(0),
#if DT_INST_SPI_DEV_HAS_CS_GPIOS(0)
.gpio_cs_port = DT_INST_SPI_DEV_CS_GPIOS_LABEL(0),
.cs_gpio = DT_INST_SPI_DEV_CS_GPIOS_PIN(0),
.cs_gpio_flags = DT_INST_SPI_DEV_CS_GPIOS_FLAGS(0),
.spi_conf.cs = &lps22hh_data.cs_ctrl,
#else #else
.spi_conf.cs = NULL, #define LPS22HH_CFG_IRQ(inst)
#endif #endif /* CONFIG_LPS22HH_TRIGGER */
#elif DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
.bus_init = lps22hh_i2c_init,
.i2c_slv_addr = DT_INST_REG_ADDR(0),
#else
#error "BUS MACRO NOT DEFINED IN DTS"
#endif
};
DEVICE_DT_INST_DEFINE(0, lps22hh_init, NULL, #define LPS22HH_SPI_OPERATION (SPI_WORD_SET(8) | \
&lps22hh_data, &lps22hh_config, POST_KERNEL, SPI_OP_MODE_MASTER | \
CONFIG_SENSOR_INIT_PRIORITY, &lps22hh_api_funcs); SPI_MODE_CPOL | \
SPI_MODE_CPHA) \
#define LPS22HH_CONFIG_SPI(inst) \
{ \
.ctx = { \
.read_reg = \
(stmdev_read_ptr) stmemsc_spi_read, \
.write_reg = \
(stmdev_write_ptr) stmemsc_spi_write, \
.handle = \
(void *)&lps22hh_config_##inst.stmemsc_cfg, \
}, \
.stmemsc_cfg = { \
.spi = SPI_DT_SPEC_INST_GET(inst, \
LPS22HH_SPI_OPERATION, \
0), \
}, \
COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, drdy_gpios), \
(LPS22HH_CFG_IRQ(inst)), ()) \
}
/*
* Instantiation macros used when a device is on an I2C bus.
*/
#define LPS22HH_CONFIG_I2C(inst) \
{ \
.ctx = { \
.read_reg = \
(stmdev_read_ptr) stmemsc_i2c_read, \
.write_reg = \
(stmdev_write_ptr) stmemsc_i2c_write, \
.handle = \
(void *)&lps22hh_config_##inst.stmemsc_cfg, \
}, \
.stmemsc_cfg = { \
.i2c = I2C_DT_SPEC_INST_GET(inst), \
}, \
COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, drdy_gpios), \
(LPS22HH_CFG_IRQ(inst)), ()) \
}
/*
* Main instantiation macro. Use of COND_CODE_1() selects the right
* bus-specific macro at preprocessor time.
*/
#define LPS22HH_DEFINE(inst) \
static struct lps22hh_data lps22hh_data_##inst; \
static const struct lps22hh_config lps22hh_config_##inst = \
COND_CODE_1(DT_INST_ON_BUS(inst, spi), \
(LPS22HH_CONFIG_SPI(inst)), \
(LPS22HH_CONFIG_I2C(inst))); \
DEVICE_DT_INST_DEFINE(inst, lps22hh_init, NULL, &lps22hh_data_##inst, \
&lps22hh_config_##inst, POST_KERNEL, \
CONFIG_SENSOR_INIT_PRIORITY, &lps22hh_driver_api);
DT_INST_FOREACH_STATUS_OKAY(LPS22HH_DEFINE)

View file

@ -12,55 +12,42 @@
#define ZEPHYR_DRIVERS_SENSOR_LPS22HH_LPS22HH_H_ #define ZEPHYR_DRIVERS_SENSOR_LPS22HH_LPS22HH_H_
#include <stdint.h> #include <stdint.h>
#include <drivers/i2c.h> #include <stmemsc.h>
#include <drivers/spi.h>
#include <drivers/gpio.h>
#include <drivers/sensor.h>
#include <zephyr/types.h>
#include <sys/util.h>
#include "lps22hh_reg.h" #include "lps22hh_reg.h"
struct lps22hh_config { #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
char *master_dev_name; #include <drivers/spi.h>
int (*bus_init)(const struct device *dev); #endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) */
#ifdef CONFIG_LPS22HH_TRIGGER
const char *drdy_port;
uint8_t drdy_pin;
uint8_t drdy_flags;
#endif
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
uint16_t i2c_slv_addr; #include <drivers/i2c.h>
#elif DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) #endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) */
struct spi_config spi_conf;
#if DT_INST_SPI_DEV_HAS_CS_GPIOS(0) struct lps22hh_config {
const char *gpio_cs_port; stmdev_ctx_t ctx;
uint8_t cs_gpio; union {
uint8_t cs_gpio_flags; #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
const struct i2c_dt_spec i2c;
#endif #endif
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
const struct spi_dt_spec spi;
#endif
} stmemsc_cfg;
#ifdef CONFIG_LPS22HH_TRIGGER
struct gpio_dt_spec gpio_int;
#endif #endif
}; };
struct lps22hh_data { struct lps22hh_data {
const struct device *dev;
const struct device *bus;
int32_t sample_press; int32_t sample_press;
int16_t sample_temp; int16_t sample_temp;
stmdev_ctx_t *ctx;
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
stmdev_ctx_t ctx_i2c;
#elif DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
stmdev_ctx_t ctx_spi;
#endif
#ifdef CONFIG_LPS22HH_TRIGGER #ifdef CONFIG_LPS22HH_TRIGGER
const struct device *gpio;
uint32_t pin;
struct gpio_callback gpio_cb; struct gpio_callback gpio_cb;
struct sensor_trigger data_ready_trigger; struct sensor_trigger data_ready_trigger;
sensor_trigger_handler_t handler_drdy; sensor_trigger_handler_t handler_drdy;
const struct device *dev;
#if defined(CONFIG_LPS22HH_TRIGGER_OWN_THREAD) #if defined(CONFIG_LPS22HH_TRIGGER_OWN_THREAD)
K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_LPS22HH_THREAD_STACK_SIZE); K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_LPS22HH_THREAD_STACK_SIZE);
@ -71,14 +58,8 @@ struct lps22hh_data {
#endif #endif
#endif /* CONFIG_LPS22HH_TRIGGER */ #endif /* CONFIG_LPS22HH_TRIGGER */
#if DT_INST_SPI_DEV_HAS_CS_GPIOS(0)
struct spi_cs_control cs_ctrl;
#endif
}; };
int lps22hh_i2c_init(const struct device *dev);
int lps22hh_spi_init(const struct device *dev);
#ifdef CONFIG_LPS22HH_TRIGGER #ifdef CONFIG_LPS22HH_TRIGGER
int lps22hh_trigger_set(const struct device *dev, int lps22hh_trigger_set(const struct device *dev,
const struct sensor_trigger *trig, const struct sensor_trigger *trig,

View file

@ -1,53 +0,0 @@
/* ST Microelectronics LPS22HH pressure and temperature sensor
*
* Copyright (c) 2019 STMicroelectronics
*
* SPDX-License-Identifier: Apache-2.0
*
* Datasheet:
* https://www.st.com/resource/en/datasheet/lps22hh.pdf
*/
#define DT_DRV_COMPAT st_lps22hh
#include <string.h>
#include <drivers/i2c.h>
#include <logging/log.h>
#include "lps22hh.h"
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
LOG_MODULE_DECLARE(LPS22HH, CONFIG_SENSOR_LOG_LEVEL);
static int lps22hh_i2c_read(struct lps22hh_data *data, uint8_t reg_addr,
uint8_t *value, uint16_t len)
{
const struct lps22hh_config *cfg = data->dev->config;
return i2c_burst_read(data->bus, cfg->i2c_slv_addr,
reg_addr, value, len);
}
static int lps22hh_i2c_write(struct lps22hh_data *data, uint8_t reg_addr,
uint8_t *value, uint16_t len)
{
const struct lps22hh_config *cfg = data->dev->config;
return i2c_burst_write(data->bus, cfg->i2c_slv_addr,
reg_addr, value, len);
}
int lps22hh_i2c_init(const struct device *dev)
{
struct lps22hh_data *data = dev->data;
data->ctx_i2c.read_reg = (stmdev_read_ptr) lps22hh_i2c_read;
data->ctx_i2c.write_reg = (stmdev_write_ptr) lps22hh_i2c_write;
data->ctx = &data->ctx_i2c;
data->ctx->handle = data;
return 0;
}
#endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) */

View file

@ -1,128 +0,0 @@
/* ST Microelectronics LPS22HH pressure and temperature sensor
*
* Copyright (c) 2019 STMicroelectronics
*
* SPDX-License-Identifier: Apache-2.0
*
* Datasheet:
* https://www.st.com/resource/en/datasheet/lps22hh.pdf
*/
#define DT_DRV_COMPAT st_lps22hh
#include <string.h>
#include "lps22hh.h"
#include <logging/log.h>
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
#define LPS22HH_SPI_READ (1 << 7)
LOG_MODULE_DECLARE(LPS22HH, CONFIG_SENSOR_LOG_LEVEL);
static int lps22hh_spi_read(struct lps22hh_data *data, uint8_t reg_addr,
uint8_t *value, uint8_t len)
{
const struct lps22hh_config *cfg = data->dev->config;
const struct spi_config *spi_cfg = &cfg->spi_conf;
uint8_t buffer_tx[2] = { reg_addr | LPS22HH_SPI_READ, 0 };
const struct spi_buf tx_buf = {
.buf = buffer_tx,
.len = 2,
};
const struct spi_buf_set tx = {
.buffers = &tx_buf,
.count = 1
};
const struct spi_buf rx_buf[2] = {
{
.buf = NULL,
.len = 1,
},
{
.buf = value,
.len = len,
}
};
const struct spi_buf_set rx = {
.buffers = rx_buf,
.count = 2
};
if (len > 64) {
return -EIO;
}
if (spi_transceive(data->bus, spi_cfg, &tx, &rx)) {
return -EIO;
}
return 0;
}
static int lps22hh_spi_write(struct lps22hh_data *data, uint8_t reg_addr,
uint8_t *value, uint8_t len)
{
const struct lps22hh_config *cfg = data->dev->config;
const struct spi_config *spi_cfg = &cfg->spi_conf;
uint8_t buffer_tx[1] = { reg_addr & ~LPS22HH_SPI_READ };
const struct spi_buf tx_buf[2] = {
{
.buf = buffer_tx,
.len = 1,
},
{
.buf = value,
.len = len,
}
};
const struct spi_buf_set tx = {
.buffers = tx_buf,
.count = 2
};
if (len > 64) {
return -EIO;
}
if (spi_write(data->bus, spi_cfg, &tx)) {
return -EIO;
}
return 0;
}
int lps22hh_spi_init(const struct device *dev)
{
struct lps22hh_data *data = dev->data;
data->ctx_spi.read_reg = (stmdev_read_ptr) lps22hh_spi_read;
data->ctx_spi.write_reg = (stmdev_write_ptr) lps22hh_spi_write;
data->ctx = &data->ctx_spi;
data->ctx->handle = data;
#if DT_INST_SPI_DEV_HAS_CS_GPIOS(0)
const struct lps22hh_config *cfg = dev->config;
/* handle SPI CS thru GPIO if it is the case */
data->cs_ctrl.gpio_dev = device_get_binding(cfg->gpio_cs_port);
if (!data->cs_ctrl.gpio_dev) {
LOG_ERR("Unable to get GPIO SPI CS device");
return -ENODEV;
}
data->cs_ctrl.gpio_pin = cfg->cs_gpio;
data->cs_ctrl.gpio_dt_flags = cfg->cs_gpio_flags;
data->cs_ctrl.delay = 0;
LOG_DBG("SPI GPIO CS configured on %s:%u",
cfg->gpio_cs_port, cfg->cs_gpio);
#endif
return 0;
}
#endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) */

View file

@ -24,15 +24,14 @@ LOG_MODULE_DECLARE(LPS22HH, CONFIG_SENSOR_LOG_LEVEL);
*/ */
static int lps22hh_enable_int(const struct device *dev, int enable) static int lps22hh_enable_int(const struct device *dev, int enable)
{ {
struct lps22hh_data *lps22hh = dev->data; const struct lps22hh_config * const cfg = dev->config;
stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
lps22hh_reg_t int_route; lps22hh_reg_t int_route;
/* set interrupt */ /* set interrupt */
lps22hh_pin_int_route_get(lps22hh->ctx, lps22hh_pin_int_route_get(ctx, &int_route.ctrl_reg3);
&int_route.ctrl_reg3);
int_route.ctrl_reg3.drdy = enable; int_route.ctrl_reg3.drdy = enable;
return lps22hh_pin_int_route_set(lps22hh->ctx, return lps22hh_pin_int_route_set(ctx, &int_route.ctrl_reg3);
&int_route.ctrl_reg3);
} }
/** /**
@ -43,14 +42,15 @@ int lps22hh_trigger_set(const struct device *dev,
sensor_trigger_handler_t handler) sensor_trigger_handler_t handler)
{ {
struct lps22hh_data *lps22hh = dev->data; struct lps22hh_data *lps22hh = dev->data;
const struct lps22hh_config * const cfg = dev->config;
stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
uint32_t raw_press; uint32_t raw_press;
if (trig->chan == SENSOR_CHAN_ALL) { if (trig->chan == SENSOR_CHAN_ALL) {
lps22hh->handler_drdy = handler; lps22hh->handler_drdy = handler;
if (handler) { if (handler) {
/* dummy read: re-trigger interrupt */ /* dummy read: re-trigger interrupt */
if (lps22hh_pressure_raw_get(lps22hh->ctx, if (lps22hh_pressure_raw_get(ctx, &raw_press) < 0) {
&raw_press) < 0) {
LOG_DBG("Failed to read sample"); LOG_DBG("Failed to read sample");
return -EIO; return -EIO;
} }
@ -69,6 +69,7 @@ int lps22hh_trigger_set(const struct device *dev,
*/ */
static void lps22hh_handle_interrupt(const struct device *dev) static void lps22hh_handle_interrupt(const struct device *dev)
{ {
int ret;
struct lps22hh_data *lps22hh = dev->data; struct lps22hh_data *lps22hh = dev->data;
const struct lps22hh_config *cfg = dev->config; const struct lps22hh_config *cfg = dev->config;
struct sensor_trigger drdy_trigger = { struct sensor_trigger drdy_trigger = {
@ -79,8 +80,11 @@ static void lps22hh_handle_interrupt(const struct device *dev)
lps22hh->handler_drdy(dev, &drdy_trigger); lps22hh->handler_drdy(dev, &drdy_trigger);
} }
gpio_pin_interrupt_configure(lps22hh->gpio, cfg->drdy_pin, ret = gpio_pin_interrupt_configure_dt(&cfg->gpio_int,
GPIO_INT_EDGE_TO_ACTIVE); GPIO_INT_EDGE_TO_ACTIVE);
if (ret < 0) {
LOG_ERR("%s: Not able to configure pin_int", dev->name);
}
} }
static void lps22hh_gpio_callback(const struct device *dev, static void lps22hh_gpio_callback(const struct device *dev,
@ -91,9 +95,12 @@ static void lps22hh_gpio_callback(const struct device *dev,
ARG_UNUSED(pins); ARG_UNUSED(pins);
const struct lps22hh_config *cfg = lps22hh->dev->config; const struct lps22hh_config *cfg = lps22hh->dev->config;
int ret;
gpio_pin_interrupt_configure(lps22hh->gpio, cfg->drdy_pin, ret = gpio_pin_interrupt_configure_dt(&cfg->gpio_int, GPIO_INT_DISABLE);
GPIO_INT_DISABLE); if (ret < 0) {
LOG_ERR("%s: Not able to configure pin_int", dev->name);
}
#if defined(CONFIG_LPS22HH_TRIGGER_OWN_THREAD) #if defined(CONFIG_LPS22HH_TRIGGER_OWN_THREAD)
k_sem_give(&lps22hh->gpio_sem); k_sem_give(&lps22hh->gpio_sem);
@ -126,15 +133,23 @@ int lps22hh_init_interrupt(const struct device *dev)
{ {
struct lps22hh_data *lps22hh = dev->data; struct lps22hh_data *lps22hh = dev->data;
const struct lps22hh_config *cfg = dev->config; const struct lps22hh_config *cfg = dev->config;
stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
int ret; int ret;
/* setup data ready gpio interrupt */ /* setup data ready gpio interrupt */
lps22hh->gpio = device_get_binding(cfg->drdy_port); if (!device_is_ready(cfg->gpio_int.port)) {
if (lps22hh->gpio == NULL) { if (cfg->gpio_int.port) {
LOG_DBG("Cannot get pointer to %s device", cfg->drdy_port); LOG_ERR("%s: device %s is not ready", dev->name,
return -EINVAL; cfg->gpio_int.port->name);
return -ENODEV;
}
LOG_DBG("%s: gpio_int not defined in DT", dev->name);
return 0;
} }
lps22hh->dev = dev;
#if defined(CONFIG_LPS22HH_TRIGGER_OWN_THREAD) #if defined(CONFIG_LPS22HH_TRIGGER_OWN_THREAD)
k_sem_init(&lps22hh->gpio_sem, 0, K_SEM_MAX_LIMIT); k_sem_init(&lps22hh->gpio_sem, 0, K_SEM_MAX_LIMIT);
@ -147,27 +162,30 @@ int lps22hh_init_interrupt(const struct device *dev)
lps22hh->work.handler = lps22hh_work_cb; lps22hh->work.handler = lps22hh_work_cb;
#endif /* CONFIG_LPS22HH_TRIGGER_OWN_THREAD */ #endif /* CONFIG_LPS22HH_TRIGGER_OWN_THREAD */
ret = gpio_pin_configure(lps22hh->gpio, cfg->drdy_pin, ret = gpio_pin_configure_dt(&cfg->gpio_int, GPIO_INPUT);
GPIO_INPUT | cfg->drdy_flags);
if (ret < 0) { if (ret < 0) {
LOG_DBG("Could not configure gpio"); LOG_ERR("Could not configure gpio");
return ret; return ret;
} }
gpio_init_callback(&lps22hh->gpio_cb, lps22hh_gpio_callback, LOG_INF("%s: int on %s.%02u", dev->name, cfg->gpio_int.port->name,
BIT(cfg->drdy_pin)); cfg->gpio_int.pin);
if (gpio_add_callback(lps22hh->gpio, &lps22hh->gpio_cb) < 0) { gpio_init_callback(&lps22hh->gpio_cb,
LOG_DBG("Could not set gpio callback"); lps22hh_gpio_callback,
return -EIO; BIT(cfg->gpio_int.pin));
ret = gpio_add_callback(cfg->gpio_int.port, &lps22hh->gpio_cb);
if (ret < 0) {
LOG_ERR("Could not set gpio callback");
return ret;
} }
/* enable interrupt in pulse mode */ /* enable interrupt in pulse mode */
if (lps22hh_int_notification_set(lps22hh->ctx, if (lps22hh_int_notification_set(ctx, LPS22HH_INT_PULSED) < 0) {
LPS22HH_INT_PULSED) < 0) {
return -EIO; return -EIO;
} }
return gpio_pin_interrupt_configure(lps22hh->gpio, cfg->drdy_pin, return gpio_pin_interrupt_configure_dt(&cfg->gpio_int,
GPIO_INT_EDGE_TO_ACTIVE); GPIO_INT_EDGE_TO_ACTIVE);
} }