drivers: sensor: hmc5883l: 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:
Benjamin Björnsson 2022-06-19 11:01:50 +02:00 committed by Maureen Helm
commit b22c6a8867
3 changed files with 24 additions and 31 deletions

View file

@ -158,6 +158,8 @@ static struct hmc5883l_data hmc5883l_data_inst;
static const struct hmc5883l_config hmc5883l_config_inst = {
.i2c = I2C_DT_SPEC_INST_GET(0),
IF_ENABLED(CONFIG_HMC5883L_TRIGGER,
(.int_gpio = GPIO_DT_SPEC_INST_GET(0, int_gpios),))
};
DEVICE_DT_INST_DEFINE(0, hmc5883l_init, NULL, &hmc5883l_data_inst,

View file

@ -49,7 +49,6 @@ struct hmc5883l_data {
#ifdef CONFIG_HMC5883L_TRIGGER
const struct device *dev;
const struct device *gpio;
struct gpio_callback gpio_cb;
struct sensor_trigger data_ready_trigger;
@ -68,6 +67,9 @@ struct hmc5883l_data {
struct hmc5883l_config {
struct i2c_dt_spec i2c;
#ifdef CONFIG_HMC5883L_TRIGGER
struct gpio_dt_spec int_gpio;
#endif
};
#ifdef CONFIG_HMC5883L_TRIGGER

View file

@ -22,12 +22,11 @@ int hmc5883l_trigger_set(const struct device *dev,
sensor_trigger_handler_t handler)
{
struct hmc5883l_data *drv_data = dev->data;
const struct hmc5883l_config *config = dev->config;
__ASSERT_NO_MSG(trig->type == SENSOR_TRIG_DATA_READY);
gpio_pin_interrupt_configure(drv_data->gpio,
DT_INST_GPIO_PIN(0, int_gpios),
GPIO_INT_DISABLE);
gpio_pin_interrupt_configure_dt(&config->int_gpio, GPIO_INT_DISABLE);
drv_data->data_ready_handler = handler;
if (handler == NULL) {
@ -36,8 +35,7 @@ int hmc5883l_trigger_set(const struct device *dev,
drv_data->data_ready_trigger = *trig;
gpio_pin_interrupt_configure(drv_data->gpio,
DT_INST_GPIO_PIN(0, int_gpios),
gpio_pin_interrupt_configure_dt(&config->int_gpio,
GPIO_INT_EDGE_TO_ACTIVE);
return 0;
@ -48,12 +46,11 @@ static void hmc5883l_gpio_callback(const struct device *dev,
{
struct hmc5883l_data *drv_data =
CONTAINER_OF(cb, struct hmc5883l_data, gpio_cb);
const struct hmc5883l_config *config = drv_data->dev->config;
ARG_UNUSED(pins);
gpio_pin_interrupt_configure(dev,
DT_INST_GPIO_PIN(0, int_gpios),
GPIO_INT_DISABLE);
gpio_pin_interrupt_configure_dt(&config->int_gpio, GPIO_INT_DISABLE);
#if defined(CONFIG_HMC5883L_TRIGGER_OWN_THREAD)
k_sem_give(&drv_data->gpio_sem);
@ -65,14 +62,14 @@ static void hmc5883l_gpio_callback(const struct device *dev,
static void hmc5883l_thread_cb(const struct device *dev)
{
struct hmc5883l_data *drv_data = dev->data;
const struct hmc5883l_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, int_gpios),
gpio_pin_interrupt_configure_dt(&config->int_gpio,
GPIO_INT_EDGE_TO_ACTIVE);
}
@ -99,26 +96,19 @@ static void hmc5883l_work_cb(struct k_work *work)
int hmc5883l_init_interrupt(const struct device *dev)
{
struct hmc5883l_data *drv_data = dev->data;
const struct hmc5883l_config *config = dev->config;
/* setup data ready gpio interrupt */
drv_data->gpio = device_get_binding(
DT_INST_GPIO_LABEL(0, int_gpios));
if (drv_data->gpio == NULL) {
LOG_ERR("Failed to get pointer to %s device.",
DT_INST_GPIO_LABEL(0, int_gpios));
return -EINVAL;
if (!device_is_ready(config->int_gpio.port)) {
LOG_ERR("GPIO device not ready");
return -ENODEV;
}
gpio_pin_configure(drv_data->gpio,
DT_INST_GPIO_PIN(0, int_gpios),
GPIO_INPUT |
DT_INST_GPIO_FLAGS(0, int_gpios));
gpio_pin_configure_dt(&config->int_gpio, GPIO_INPUT);
gpio_init_callback(&drv_data->gpio_cb,
hmc5883l_gpio_callback,
BIT(DT_INST_GPIO_PIN(0, int_gpios)));
gpio_init_callback(&drv_data->gpio_cb, hmc5883l_gpio_callback,
BIT(config->int_gpio.pin));
if (gpio_add_callback(drv_data->gpio, &drv_data->gpio_cb) < 0) {
if (gpio_add_callback(config->int_gpio.port, &drv_data->gpio_cb) < 0) {
LOG_ERR("Failed to set gpio callback.");
return -EIO;
}
@ -138,8 +128,7 @@ int hmc5883l_init_interrupt(const struct device *dev)
drv_data->work.handler = hmc5883l_work_cb;
#endif
gpio_pin_interrupt_configure(drv_data->gpio,
DT_INST_GPIO_PIN(0, int_gpios),
gpio_pin_interrupt_configure_dt(&config->int_gpio,
GPIO_INT_EDGE_TO_ACTIVE);
return 0;