sensor: adxl372: convert to _dt_spec

Convert adxl372 driver to use `spi_dt_spec` and `gpio_dt_spec`.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
This commit is contained in:
Jordan Yates 2021-08-07 13:42:03 +10:00 committed by Christopher Friedt
commit 75682f7d0e
3 changed files with 31 additions and 93 deletions

View file

@ -25,7 +25,7 @@ LOG_MODULE_REGISTER(ADXL372, CONFIG_SENSOR_LOG_LEVEL);
static int adxl372_bus_access(const struct device *dev, uint8_t reg,
void *data, size_t length)
{
struct adxl372_data *adxl372_data = dev->data;
const struct adxl372_dev_config *config = dev->config;
#ifdef CONFIG_ADXL372_SPI
const struct spi_buf buf[2] = {
@ -50,18 +50,15 @@ static int adxl372_bus_access(const struct device *dev, uint8_t reg,
tx.count = 1;
return spi_transceive(adxl372_data->bus,
&adxl372_data->spi_cfg, &tx, &rx);
return spi_transceive_dt(&config->spi, &tx, &rx);
}
tx.count = 2;
return spi_write(adxl372_data->bus, &adxl372_data->spi_cfg, &tx);
return spi_write_dt(&config->spi, &tx);
#elif CONFIG_ADXL372_I2C
const struct adxl372_dev_config *cfg = dev->config;
if (reg & ADXL372_READ) {
return i2c_burst_read(adxl372_data->bus, cfg->i2c_addr,
return i2c_burst_read_dt(&config->i2c,
ADXL372_TO_I2C_REG(reg),
(uint8_t *) data, length);
} else {
@ -69,7 +66,7 @@ static int adxl372_bus_access(const struct device *dev, uint8_t reg,
return -EINVAL;
}
return i2c_reg_write_byte(adxl372_data->bus, cfg->i2c_addr,
return i2c_reg_write_byte_dt(&config->i2c,
ADXL372_TO_I2C_REG(reg),
*(uint8_t *)data);
}
@ -886,43 +883,19 @@ static int adxl372_probe(const struct device *dev)
static int adxl372_init(const struct device *dev)
{
struct adxl372_data *data = dev->data;
const struct adxl372_dev_config *cfg = dev->config;
#ifdef CONFIG_ADXL372_I2C
data->bus = device_get_binding(cfg->i2c_port);
if (data->bus == NULL) {
LOG_ERR("Failed to get pointer to %s device!",
cfg->i2c_port);
if (!device_is_ready(cfg->i2c.bus)) {
LOG_ERR("I2C bus %s not ready!", cfg->i2c.bus->name);
return -EINVAL;
}
#endif
#ifdef CONFIG_ADXL372_SPI
data->bus = device_get_binding(cfg->spi_port);
if (!data->bus) {
LOG_ERR("spi device not found: %s", cfg->spi_port);
if (!spi_is_ready(&cfg->spi)) {
LOG_ERR("SPI bus %s not ready!", cfg->spi.bus->name);
return -EINVAL;
}
/* CPOL=0, CPHA=0, max 10MHz */
data->spi_cfg.operation = SPI_WORD_SET(8) | SPI_TRANSFER_MSB;
data->spi_cfg.frequency = cfg->spi_max_frequency;
data->spi_cfg.slave = cfg->spi_slave;
#if DT_INST_SPI_DEV_HAS_CS_GPIOS(0)
/* handle SPI CS thru GPIO if it is the case */
data->adxl372_cs_ctrl.gpio_dev = device_get_binding(cfg->gpio_cs_port);
if (!data->adxl372_cs_ctrl.gpio_dev) {
LOG_ERR("Unable to get GPIO SPI CS device");
return -ENODEV;
}
data->adxl372_cs_ctrl.gpio_pin = cfg->cs_gpio;
data->adxl372_cs_ctrl.gpio_dt_flags = cfg->cs_flags;
data->adxl372_cs_ctrl.delay = 0U;
data->spi_cfg.cs = &data->adxl372_cs_ctrl;
#endif
#endif /* CONFIG_ADXL372_SPI */
if (adxl372_probe(dev) < 0) {
@ -936,23 +909,13 @@ static struct adxl372_data adxl372_data;
static const struct adxl372_dev_config adxl372_config = {
#ifdef CONFIG_ADXL372_I2C
.i2c_port = DT_INST_BUS_LABEL(0),
.i2c_addr = DT_INST_REG_ADDR(0),
.i2c = I2C_DT_SPEC_INST_GET(0),
#endif
#ifdef CONFIG_ADXL372_SPI
.spi_port = DT_INST_BUS_LABEL(0),
.spi_slave = DT_INST_REG_ADDR(0),
.spi_max_frequency = DT_INST_PROP(0, spi_max_frequency),
#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_flags = DT_INST_SPI_DEV_CS_GPIOS_FLAGS(0),
#endif
.spi = SPI_DT_SPEC_INST_GET(0, SPI_WORD_SET(8) | SPI_TRANSFER_MSB, 0),
#endif
#ifdef CONFIG_ADXL372_TRIGGER
.gpio_port = DT_INST_GPIO_LABEL(0, int1_gpios),
.int_gpio = DT_INST_GPIO_PIN(0, int1_gpios),
.int_flags = DT_INST_GPIO_FLAGS(0, int1_gpios),
.interrupt = GPIO_DT_SPEC_INST_GET(0, int1_gpios),
#endif
.max_peak_detect_mode = IS_ENABLED(CONFIG_ADXL372_PEAK_DETECT_MODE),

View file

@ -279,18 +279,10 @@ struct adxl372_xyz_accel_data {
};
struct adxl372_data {
const struct device *bus;
#ifdef CONFIG_ADXL372_SPI
struct spi_config spi_cfg;
#if DT_INST_SPI_DEV_HAS_CS_GPIOS(0)
struct spi_cs_control adxl372_cs_ctrl;
#endif
#endif
struct adxl372_xyz_accel_data sample;
struct adxl372_fifo_config fifo_config;
#ifdef CONFIG_ADXL372_TRIGGER
const struct device *gpio;
struct gpio_callback gpio_cb;
sensor_trigger_handler_t th_handler;
@ -311,23 +303,13 @@ struct adxl372_data {
struct adxl372_dev_config {
#ifdef CONFIG_ADXL372_I2C
const char *i2c_port;
uint16_t i2c_addr;
struct i2c_dt_spec i2c;
#endif
#ifdef CONFIG_ADXL372_SPI
const char *spi_port;
uint16_t spi_slave;
uint32_t spi_max_frequency;
#if DT_INST_SPI_DEV_HAS_CS_GPIOS(0)
const char *gpio_cs_port;
gpio_pin_t cs_gpio;
gpio_dt_flags_t cs_flags;
#endif
struct spi_dt_spec spi;
#endif /* CONFIG_ADXL372_SPI */
#ifdef CONFIG_ADXL372_TRIGGER
const char *gpio_port;
gpio_pin_t int_gpio;
gpio_dt_flags_t int_flags;
struct gpio_dt_spec interrupt;
#endif
bool max_peak_detect_mode;

View file

@ -18,8 +18,8 @@ LOG_MODULE_DECLARE(ADXL372, CONFIG_SENSOR_LOG_LEVEL);
static void adxl372_thread_cb(const struct device *dev)
{
struct adxl372_data *drv_data = dev->data;
const struct adxl372_dev_config *cfg = dev->config;
struct adxl372_data *drv_data = dev->data;
uint8_t status1, status2;
/* Clear the status */
@ -46,8 +46,7 @@ static void adxl372_thread_cb(const struct device *dev)
drv_data->drdy_handler(dev, &drv_data->drdy_trigger);
}
gpio_pin_interrupt_configure(drv_data->gpio, cfg->int_gpio,
GPIO_INT_EDGE_TO_ACTIVE);
gpio_pin_interrupt_configure_dt(&cfg->interrupt, GPIO_INT_EDGE_TO_ACTIVE);
}
static void adxl372_gpio_callback(const struct device *dev,
@ -57,8 +56,7 @@ static void adxl372_gpio_callback(const struct device *dev,
CONTAINER_OF(cb, struct adxl372_data, gpio_cb);
const struct adxl372_dev_config *cfg = drv_data->dev->config;
gpio_pin_interrupt_configure(drv_data->gpio, cfg->int_gpio,
GPIO_INT_DISABLE);
gpio_pin_interrupt_configure_dt(&cfg->interrupt, GPIO_INT_DISABLE);
#if defined(CONFIG_ADXL372_TRIGGER_OWN_THREAD)
k_sem_give(&drv_data->gpio_sem);
@ -90,13 +88,12 @@ int adxl372_trigger_set(const struct device *dev,
const struct sensor_trigger *trig,
sensor_trigger_handler_t handler)
{
struct adxl372_data *drv_data = dev->data;
const struct adxl372_dev_config *cfg = dev->config;
struct adxl372_data *drv_data = dev->data;
uint8_t int_mask, int_en, status1, status2;
int ret;
gpio_pin_interrupt_configure(drv_data->gpio, cfg->int_gpio,
GPIO_INT_DISABLE);
gpio_pin_interrupt_configure_dt(&cfg->interrupt, GPIO_INT_DISABLE);
switch (trig->type) {
case SENSOR_TRIG_THRESHOLD:
@ -126,32 +123,28 @@ int adxl372_trigger_set(const struct device *dev,
adxl372_get_status(dev, &status1, &status2, NULL); /* Clear status */
out:
gpio_pin_interrupt_configure(drv_data->gpio, cfg->int_gpio,
GPIO_INT_EDGE_TO_ACTIVE);
gpio_pin_interrupt_configure_dt(&cfg->interrupt, GPIO_INT_EDGE_TO_ACTIVE);
return ret;
}
int adxl372_init_interrupt(const struct device *dev)
{
struct adxl372_data *drv_data = dev->data;
const struct adxl372_dev_config *cfg = dev->config;
struct adxl372_data *drv_data = dev->data;
drv_data->gpio = device_get_binding(cfg->gpio_port);
if (drv_data->gpio == NULL) {
LOG_ERR("Failed to get pointer to %s device!",
cfg->gpio_port);
if (!device_is_ready(cfg->interrupt.port)) {
LOG_ERR("GPIO port %s not ready", cfg->interrupt.port->name);
return -EINVAL;
}
gpio_pin_configure(drv_data->gpio, cfg->int_gpio,
GPIO_INPUT | cfg->int_flags);
gpio_pin_configure_dt(&cfg->interrupt, GPIO_INPUT);
gpio_init_callback(&drv_data->gpio_cb,
adxl372_gpio_callback,
BIT(cfg->int_gpio));
BIT(cfg->interrupt.pin));
if (gpio_add_callback(drv_data->gpio, &drv_data->gpio_cb) < 0) {
if (gpio_add_callback(cfg->interrupt.port, &drv_data->gpio_cb) < 0) {
LOG_ERR("Failed to set gpio callback!");
return -EIO;
}