drivers: sensor: lis3mdl: Update driver to use gpio_dt_spec
Simplify driver by using gpio_dt_spec for bus access. Signed-off-by: Benjamin Björnsson <benjamin.bjornsson@gmail.com>
This commit is contained in:
parent
37b59c3427
commit
1e44af1e2c
3 changed files with 22 additions and 29 deletions
|
@ -165,6 +165,8 @@ static struct lis3mdl_data lis3mdl_data_inst;
|
|||
|
||||
static struct lis3mdl_config lis3mdl_config_inst = {
|
||||
.i2c = I2C_DT_SPEC_INST_GET(0),
|
||||
IF_ENABLED(CONFIG_LIS3MDL_TRIGGER,
|
||||
(.irq_gpio = GPIO_DT_SPEC_INST_GET(0, irq_gpios),))
|
||||
};
|
||||
|
||||
DEVICE_DT_INST_DEFINE(0, lis3mdl_init, NULL, &lis3mdl_data_inst,
|
||||
|
|
|
@ -116,7 +116,6 @@ struct lis3mdl_data {
|
|||
|
||||
#ifdef CONFIG_LIS3MDL_TRIGGER
|
||||
const struct device *dev;
|
||||
const struct device *gpio;
|
||||
struct gpio_callback gpio_cb;
|
||||
|
||||
struct sensor_trigger data_ready_trigger;
|
||||
|
@ -135,6 +134,9 @@ struct lis3mdl_data {
|
|||
|
||||
struct lis3mdl_config {
|
||||
struct i2c_dt_spec i2c;
|
||||
#ifdef CONFIG_LIS3MDL_TRIGGER
|
||||
struct gpio_dt_spec irq_gpio;
|
||||
#endif
|
||||
};
|
||||
|
||||
#ifdef CONFIG_LIS3MDL_TRIGGER
|
||||
|
|
|
@ -35,9 +35,7 @@ int lis3mdl_trigger_set(const struct device *dev,
|
|||
return ret;
|
||||
}
|
||||
|
||||
gpio_pin_interrupt_configure(drv_data->gpio,
|
||||
DT_INST_GPIO_PIN(0, irq_gpios),
|
||||
GPIO_INT_DISABLE);
|
||||
gpio_pin_interrupt_configure_dt(&config->irq_gpio, GPIO_INT_DISABLE);
|
||||
|
||||
drv_data->data_ready_handler = handler;
|
||||
if (handler == NULL) {
|
||||
|
@ -46,9 +44,8 @@ int lis3mdl_trigger_set(const struct device *dev,
|
|||
|
||||
drv_data->data_ready_trigger = *trig;
|
||||
|
||||
gpio_pin_interrupt_configure(drv_data->gpio,
|
||||
DT_INST_GPIO_PIN(0, irq_gpios),
|
||||
GPIO_INT_EDGE_TO_ACTIVE);
|
||||
gpio_pin_interrupt_configure_dt(&config->irq_gpio,
|
||||
GPIO_INT_EDGE_TO_ACTIVE);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -58,12 +55,11 @@ static void lis3mdl_gpio_callback(const struct device *dev,
|
|||
{
|
||||
struct lis3mdl_data *drv_data =
|
||||
CONTAINER_OF(cb, struct lis3mdl_data, gpio_cb);
|
||||
const struct lis3mdl_config *config = drv_data->dev->config;
|
||||
|
||||
ARG_UNUSED(pins);
|
||||
|
||||
gpio_pin_interrupt_configure(dev,
|
||||
DT_INST_GPIO_PIN(0, irq_gpios),
|
||||
GPIO_INT_DISABLE);
|
||||
gpio_pin_interrupt_configure_dt(&config->irq_gpio, GPIO_INT_DISABLE);
|
||||
|
||||
#if defined(CONFIG_LIS3MDL_TRIGGER_OWN_THREAD)
|
||||
k_sem_give(&drv_data->gpio_sem);
|
||||
|
@ -75,15 +71,15 @@ static void lis3mdl_gpio_callback(const struct device *dev,
|
|||
static void lis3mdl_thread_cb(const struct device *dev)
|
||||
{
|
||||
struct lis3mdl_data *drv_data = dev->data;
|
||||
const struct lis3mdl_config *config = dev->config;
|
||||
|
||||
if (drv_data->data_ready_handler != NULL) {
|
||||
drv_data->data_ready_handler(dev,
|
||||
&drv_data->data_ready_trigger);
|
||||
}
|
||||
|
||||
gpio_pin_interrupt_configure(drv_data->gpio,
|
||||
DT_INST_GPIO_PIN(0, irq_gpios),
|
||||
GPIO_INT_EDGE_TO_ACTIVE);
|
||||
gpio_pin_interrupt_configure_dt(&config->irq_gpio,
|
||||
GPIO_INT_EDGE_TO_ACTIVE);
|
||||
}
|
||||
|
||||
#ifdef CONFIG_LIS3MDL_TRIGGER_OWN_THREAD
|
||||
|
@ -109,26 +105,20 @@ static void lis3mdl_work_cb(struct k_work *work)
|
|||
int lis3mdl_init_interrupt(const struct device *dev)
|
||||
{
|
||||
struct lis3mdl_data *drv_data = dev->data;
|
||||
const struct lis3mdl_config *config = dev->config;
|
||||
|
||||
/* setup data ready gpio interrupt */
|
||||
drv_data->gpio =
|
||||
device_get_binding(DT_INST_GPIO_LABEL(0, irq_gpios));
|
||||
if (drv_data->gpio == NULL) {
|
||||
LOG_DBG("Cannot get pointer to %s device.",
|
||||
DT_INST_GPIO_LABEL(0, irq_gpios));
|
||||
return -EINVAL;
|
||||
if (!device_is_ready(config->irq_gpio.port)) {
|
||||
LOG_ERR("GPIO device not ready");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
gpio_pin_configure(drv_data->gpio,
|
||||
DT_INST_GPIO_PIN(0, irq_gpios),
|
||||
GPIO_INPUT |
|
||||
DT_INST_GPIO_FLAGS(0, irq_gpios));
|
||||
gpio_pin_configure_dt(&config->irq_gpio, GPIO_INPUT);
|
||||
|
||||
gpio_init_callback(&drv_data->gpio_cb,
|
||||
lis3mdl_gpio_callback,
|
||||
BIT(DT_INST_GPIO_PIN(0, irq_gpios)));
|
||||
BIT(config->irq_gpio.pin));
|
||||
|
||||
if (gpio_add_callback(drv_data->gpio, &drv_data->gpio_cb) < 0) {
|
||||
if (gpio_add_callback(config->irq_gpio.port, &drv_data->gpio_cb) < 0) {
|
||||
LOG_DBG("Could not set gpio callback.");
|
||||
return -EIO;
|
||||
}
|
||||
|
@ -153,9 +143,8 @@ int lis3mdl_init_interrupt(const struct device *dev)
|
|||
drv_data->work.handler = lis3mdl_work_cb;
|
||||
#endif
|
||||
|
||||
gpio_pin_interrupt_configure(drv_data->gpio,
|
||||
DT_INST_GPIO_PIN(0, irq_gpios),
|
||||
GPIO_INT_EDGE_TO_ACTIVE);
|
||||
gpio_pin_interrupt_configure_dt(&config->irq_gpio,
|
||||
GPIO_INT_EDGE_TO_ACTIVE);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue