drivers: sensor: fxos8700: 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 09:04:30 +02:00 committed by Carles Cufí
commit 82901bd563
3 changed files with 23 additions and 42 deletions

View file

@ -387,33 +387,31 @@ static int fxos8700_init(const struct device *dev)
const struct fxos8700_config *config = dev->config; const struct fxos8700_config *config = dev->config;
struct fxos8700_data *data = dev->data; struct fxos8700_data *data = dev->data;
struct sensor_value odr = {.val1 = 6, .val2 = 250000}; struct sensor_value odr = {.val1 = 6, .val2 = 250000};
const struct device *rst;
if (!device_is_ready(config->i2c.bus)) { if (!device_is_ready(config->i2c.bus)) {
LOG_ERR("I2C bus device not ready"); LOG_ERR("I2C bus device not ready");
return -ENODEV; return -ENODEV;
} }
if (config->reset_name) { if (config->reset_gpio.port) {
/* Pulse RST pin high to perform a hardware reset of /* Pulse RST pin high to perform a hardware reset of
* the sensor. * the sensor.
*/ */
rst = device_get_binding(config->reset_name);
if (!rst) { if (!device_is_ready(config->reset_gpio.port)) {
LOG_ERR("Could not find reset GPIO device"); LOG_ERR("GPIO device not ready");
return -EINVAL; return -ENODEV;
} }
gpio_pin_configure(rst, config->reset_pin, gpio_pin_configure_dt(&config->reset_gpio, GPIO_OUTPUT_INACTIVE);
GPIO_OUTPUT_INACTIVE | config->reset_flags);
gpio_pin_set(rst, config->reset_pin, 1); gpio_pin_set_dt(&config->reset_gpio, 1);
/* The datasheet does not mention how long to pulse /* The datasheet does not mention how long to pulse
* the RST pin high in order to reset. Stay on the * the RST pin high in order to reset. Stay on the
* safe side and pulse for 1 millisecond. * safe side and pulse for 1 millisecond.
*/ */
k_busy_wait(USEC_PER_MSEC); k_busy_wait(USEC_PER_MSEC);
gpio_pin_set(rst, config->reset_pin, 0); gpio_pin_set_dt(&config->reset_gpio, 0);
} else { } else {
/* Software reset the sensor. Upon issuing a software /* Software reset the sensor. Upon issuing a software
* reset command over the I2C interface, the sensor * reset command over the I2C interface, the sensor
@ -555,9 +553,7 @@ static const struct sensor_driver_api fxos8700_driver_api = {
(FXOS8700_MODE_PROPS_HYBRID)))) (FXOS8700_MODE_PROPS_HYBRID))))
#define FXOS8700_RESET_PROPS(n) \ #define FXOS8700_RESET_PROPS(n) \
.reset_name = DT_INST_GPIO_LABEL(n, reset_gpios), \ .reset_gpio = GPIO_DT_SPEC_INST_GET(n, reset_gpios),
.reset_pin = DT_INST_GPIO_PIN(n, reset_gpios), \
.reset_flags = DT_INST_GPIO_FLAGS(n, reset_gpios),
#define FXOS8700_RESET(n) \ #define FXOS8700_RESET(n) \
COND_CODE_1(DT_INST_NODE_HAS_PROP(n, reset_gpios), \ COND_CODE_1(DT_INST_NODE_HAS_PROP(n, reset_gpios), \
@ -565,9 +561,7 @@ static const struct sensor_driver_api fxos8700_driver_api = {
()) ())
#define FXOS8700_INTM_PROPS(n, m) \ #define FXOS8700_INTM_PROPS(n, m) \
.gpio_name = DT_INST_GPIO_LABEL(n, int##m##_gpios), \ .int_gpio = GPIO_DT_SPEC_INST_GET(n, int##m##_gpios),
.gpio_pin = DT_INST_GPIO_PIN(n, int##m##_gpios), \
.gpio_flags = DT_INST_GPIO_FLAGS(n, int##m##_gpios),
#define FXOS8700_INT_PROPS(n) \ #define FXOS8700_INT_PROPS(n) \
COND_CODE_1(CONFIG_FXOS8700_DRDY_INT1, \ COND_CODE_1(CONFIG_FXOS8700_DRDY_INT1, \

View file

@ -127,13 +127,9 @@ enum fxos_trigger_type {
struct fxos8700_config { struct fxos8700_config {
struct i2c_dt_spec i2c; struct i2c_dt_spec i2c;
#ifdef CONFIG_FXOS8700_TRIGGER #ifdef CONFIG_FXOS8700_TRIGGER
char *gpio_name; struct gpio_dt_spec int_gpio;
uint8_t gpio_pin;
gpio_dt_flags_t gpio_flags;
#endif #endif
char *reset_name; struct gpio_dt_spec reset_gpio;
uint8_t reset_pin;
gpio_dt_flags_t reset_flags;
enum fxos8700_mode mode; enum fxos8700_mode mode;
enum fxos8700_power_mode power_mode; enum fxos8700_power_mode power_mode;
uint8_t range; uint8_t range;
@ -157,8 +153,6 @@ struct fxos8700_data {
struct k_sem sem; struct k_sem sem;
#ifdef CONFIG_FXOS8700_TRIGGER #ifdef CONFIG_FXOS8700_TRIGGER
const struct device *dev; const struct device *dev;
const struct device *gpio;
uint8_t gpio_pin;
struct gpio_callback gpio_cb; struct gpio_callback gpio_cb;
sensor_trigger_handler_t drdy_handler; sensor_trigger_handler_t drdy_handler;
#endif #endif

View file

@ -16,13 +16,13 @@ static void fxos8700_gpio_callback(const struct device *dev,
{ {
struct fxos8700_data *data = struct fxos8700_data *data =
CONTAINER_OF(cb, struct fxos8700_data, gpio_cb); CONTAINER_OF(cb, struct fxos8700_data, gpio_cb);
const struct fxos8700_config *config = data->dev->config;
if ((pin_mask & BIT(data->gpio_pin)) == 0U) { if ((pin_mask & BIT(config->int_gpio.pin)) == 0U) {
return; return;
} }
gpio_pin_interrupt_configure(data->gpio, data->gpio_pin, gpio_pin_interrupt_configure_dt(&config->int_gpio, GPIO_INT_DISABLE);
GPIO_INT_DISABLE);
#if defined(CONFIG_FXOS8700_TRIGGER_OWN_THREAD) #if defined(CONFIG_FXOS8700_TRIGGER_OWN_THREAD)
k_sem_give(&data->trig_sem); k_sem_give(&data->trig_sem);
@ -179,8 +179,7 @@ static void fxos8700_handle_int(const struct device *dev)
} }
#endif #endif
gpio_pin_interrupt_configure(data->gpio, config->gpio_pin, gpio_pin_interrupt_configure_dt(&config->int_gpio, GPIO_INT_EDGE_TO_ACTIVE);
GPIO_INT_EDGE_TO_ACTIVE);
} }
#ifdef CONFIG_FXOS8700_TRIGGER_OWN_THREAD #ifdef CONFIG_FXOS8700_TRIGGER_OWN_THREAD
@ -451,31 +450,25 @@ int fxos8700_trigger_init(const struct device *dev)
} }
#endif #endif
/* Get the GPIO device */ if (!device_is_ready(config->int_gpio.port)) {
data->gpio = device_get_binding(config->gpio_name); LOG_ERR("GPIO device not ready");
if (data->gpio == NULL) { return -ENODEV;
LOG_ERR("Could not find GPIO device");
return -EINVAL;
} }
data->gpio_pin = config->gpio_pin; ret = gpio_pin_configure_dt(&config->int_gpio, GPIO_INPUT);
ret = gpio_pin_configure(data->gpio, config->gpio_pin,
GPIO_INPUT | config->gpio_flags);
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
gpio_init_callback(&data->gpio_cb, fxos8700_gpio_callback, gpio_init_callback(&data->gpio_cb, fxos8700_gpio_callback,
BIT(config->gpio_pin)); BIT(config->int_gpio.pin));
ret = gpio_add_callback(data->gpio, &data->gpio_cb); ret = gpio_add_callback(config->int_gpio.port, &data->gpio_cb);
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
ret = gpio_pin_interrupt_configure(data->gpio, config->gpio_pin, ret = gpio_pin_interrupt_configure_dt(&config->int_gpio, GPIO_INT_EDGE_TO_ACTIVE);
GPIO_INT_EDGE_TO_ACTIVE);
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }