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:
parent
be1e512aad
commit
b22c6a8867
3 changed files with 24 additions and 31 deletions
|
@ -158,6 +158,8 @@ static struct hmc5883l_data hmc5883l_data_inst;
|
||||||
|
|
||||||
static const struct hmc5883l_config hmc5883l_config_inst = {
|
static const struct hmc5883l_config hmc5883l_config_inst = {
|
||||||
.i2c = I2C_DT_SPEC_INST_GET(0),
|
.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,
|
DEVICE_DT_INST_DEFINE(0, hmc5883l_init, NULL, &hmc5883l_data_inst,
|
||||||
|
|
|
@ -49,7 +49,6 @@ struct hmc5883l_data {
|
||||||
|
|
||||||
#ifdef CONFIG_HMC5883L_TRIGGER
|
#ifdef CONFIG_HMC5883L_TRIGGER
|
||||||
const struct device *dev;
|
const struct device *dev;
|
||||||
const struct device *gpio;
|
|
||||||
struct gpio_callback gpio_cb;
|
struct gpio_callback gpio_cb;
|
||||||
|
|
||||||
struct sensor_trigger data_ready_trigger;
|
struct sensor_trigger data_ready_trigger;
|
||||||
|
@ -68,6 +67,9 @@ struct hmc5883l_data {
|
||||||
|
|
||||||
struct hmc5883l_config {
|
struct hmc5883l_config {
|
||||||
struct i2c_dt_spec i2c;
|
struct i2c_dt_spec i2c;
|
||||||
|
#ifdef CONFIG_HMC5883L_TRIGGER
|
||||||
|
struct gpio_dt_spec int_gpio;
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifdef CONFIG_HMC5883L_TRIGGER
|
#ifdef CONFIG_HMC5883L_TRIGGER
|
||||||
|
|
|
@ -22,12 +22,11 @@ int hmc5883l_trigger_set(const struct device *dev,
|
||||||
sensor_trigger_handler_t handler)
|
sensor_trigger_handler_t handler)
|
||||||
{
|
{
|
||||||
struct hmc5883l_data *drv_data = dev->data;
|
struct hmc5883l_data *drv_data = dev->data;
|
||||||
|
const struct hmc5883l_config *config = dev->config;
|
||||||
|
|
||||||
__ASSERT_NO_MSG(trig->type == SENSOR_TRIG_DATA_READY);
|
__ASSERT_NO_MSG(trig->type == SENSOR_TRIG_DATA_READY);
|
||||||
|
|
||||||
gpio_pin_interrupt_configure(drv_data->gpio,
|
gpio_pin_interrupt_configure_dt(&config->int_gpio, GPIO_INT_DISABLE);
|
||||||
DT_INST_GPIO_PIN(0, int_gpios),
|
|
||||||
GPIO_INT_DISABLE);
|
|
||||||
|
|
||||||
drv_data->data_ready_handler = handler;
|
drv_data->data_ready_handler = handler;
|
||||||
if (handler == NULL) {
|
if (handler == NULL) {
|
||||||
|
@ -36,9 +35,8 @@ int hmc5883l_trigger_set(const struct device *dev,
|
||||||
|
|
||||||
drv_data->data_ready_trigger = *trig;
|
drv_data->data_ready_trigger = *trig;
|
||||||
|
|
||||||
gpio_pin_interrupt_configure(drv_data->gpio,
|
gpio_pin_interrupt_configure_dt(&config->int_gpio,
|
||||||
DT_INST_GPIO_PIN(0, int_gpios),
|
GPIO_INT_EDGE_TO_ACTIVE);
|
||||||
GPIO_INT_EDGE_TO_ACTIVE);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -48,12 +46,11 @@ static void hmc5883l_gpio_callback(const struct device *dev,
|
||||||
{
|
{
|
||||||
struct hmc5883l_data *drv_data =
|
struct hmc5883l_data *drv_data =
|
||||||
CONTAINER_OF(cb, struct hmc5883l_data, gpio_cb);
|
CONTAINER_OF(cb, struct hmc5883l_data, gpio_cb);
|
||||||
|
const struct hmc5883l_config *config = drv_data->dev->config;
|
||||||
|
|
||||||
ARG_UNUSED(pins);
|
ARG_UNUSED(pins);
|
||||||
|
|
||||||
gpio_pin_interrupt_configure(dev,
|
gpio_pin_interrupt_configure_dt(&config->int_gpio, GPIO_INT_DISABLE);
|
||||||
DT_INST_GPIO_PIN(0, int_gpios),
|
|
||||||
GPIO_INT_DISABLE);
|
|
||||||
|
|
||||||
#if defined(CONFIG_HMC5883L_TRIGGER_OWN_THREAD)
|
#if defined(CONFIG_HMC5883L_TRIGGER_OWN_THREAD)
|
||||||
k_sem_give(&drv_data->gpio_sem);
|
k_sem_give(&drv_data->gpio_sem);
|
||||||
|
@ -65,15 +62,15 @@ static void hmc5883l_gpio_callback(const struct device *dev,
|
||||||
static void hmc5883l_thread_cb(const struct device *dev)
|
static void hmc5883l_thread_cb(const struct device *dev)
|
||||||
{
|
{
|
||||||
struct hmc5883l_data *drv_data = dev->data;
|
struct hmc5883l_data *drv_data = dev->data;
|
||||||
|
const struct hmc5883l_config *config = dev->config;
|
||||||
|
|
||||||
if (drv_data->data_ready_handler != NULL) {
|
if (drv_data->data_ready_handler != NULL) {
|
||||||
drv_data->data_ready_handler(dev,
|
drv_data->data_ready_handler(dev,
|
||||||
&drv_data->data_ready_trigger);
|
&drv_data->data_ready_trigger);
|
||||||
}
|
}
|
||||||
|
|
||||||
gpio_pin_interrupt_configure(drv_data->gpio,
|
gpio_pin_interrupt_configure_dt(&config->int_gpio,
|
||||||
DT_INST_GPIO_PIN(0, int_gpios),
|
GPIO_INT_EDGE_TO_ACTIVE);
|
||||||
GPIO_INT_EDGE_TO_ACTIVE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef CONFIG_HMC5883L_TRIGGER_OWN_THREAD
|
#ifdef CONFIG_HMC5883L_TRIGGER_OWN_THREAD
|
||||||
|
@ -99,26 +96,19 @@ static void hmc5883l_work_cb(struct k_work *work)
|
||||||
int hmc5883l_init_interrupt(const struct device *dev)
|
int hmc5883l_init_interrupt(const struct device *dev)
|
||||||
{
|
{
|
||||||
struct hmc5883l_data *drv_data = dev->data;
|
struct hmc5883l_data *drv_data = dev->data;
|
||||||
|
const struct hmc5883l_config *config = dev->config;
|
||||||
|
|
||||||
/* setup data ready gpio interrupt */
|
if (!device_is_ready(config->int_gpio.port)) {
|
||||||
drv_data->gpio = device_get_binding(
|
LOG_ERR("GPIO device not ready");
|
||||||
DT_INST_GPIO_LABEL(0, int_gpios));
|
return -ENODEV;
|
||||||
if (drv_data->gpio == NULL) {
|
|
||||||
LOG_ERR("Failed to get pointer to %s device.",
|
|
||||||
DT_INST_GPIO_LABEL(0, int_gpios));
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
gpio_pin_configure(drv_data->gpio,
|
gpio_pin_configure_dt(&config->int_gpio, GPIO_INPUT);
|
||||||
DT_INST_GPIO_PIN(0, int_gpios),
|
|
||||||
GPIO_INPUT |
|
|
||||||
DT_INST_GPIO_FLAGS(0, int_gpios));
|
|
||||||
|
|
||||||
gpio_init_callback(&drv_data->gpio_cb,
|
gpio_init_callback(&drv_data->gpio_cb, hmc5883l_gpio_callback,
|
||||||
hmc5883l_gpio_callback,
|
BIT(config->int_gpio.pin));
|
||||||
BIT(DT_INST_GPIO_PIN(0, int_gpios)));
|
|
||||||
|
|
||||||
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.");
|
LOG_ERR("Failed to set gpio callback.");
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
@ -138,9 +128,8 @@ int hmc5883l_init_interrupt(const struct device *dev)
|
||||||
drv_data->work.handler = hmc5883l_work_cb;
|
drv_data->work.handler = hmc5883l_work_cb;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
gpio_pin_interrupt_configure(drv_data->gpio,
|
gpio_pin_interrupt_configure_dt(&config->int_gpio,
|
||||||
DT_INST_GPIO_PIN(0, int_gpios),
|
GPIO_INT_EDGE_TO_ACTIVE);
|
||||||
GPIO_INT_EDGE_TO_ACTIVE);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue