drivers/sensor: iis2iclx: fix multi-instance using new helpers
This commit simplifies the Device Tree configuration by using the new helpers introduced in #30536. In particular: - get bus devices with DEVICE_DT_GET - get SPI information with SPI_CONFIG_DT_INST Signed-off-by: Armando Visconti <armando.visconti@st.com>
This commit is contained in:
parent
057e4cbc5d
commit
de707b6870
4 changed files with 37 additions and 112 deletions
|
@ -589,17 +589,12 @@ static int iis2iclx_init_chip(const struct device *dev)
|
||||||
static int iis2iclx_init(const struct device *dev)
|
static int iis2iclx_init(const struct device *dev)
|
||||||
{
|
{
|
||||||
const struct iis2iclx_config * const config = dev->config;
|
const struct iis2iclx_config * const config = dev->config;
|
||||||
struct iis2iclx_data *data = dev->data;
|
|
||||||
|
|
||||||
data->bus = device_get_binding(config->bus_name);
|
if (config->bus_init(dev) < 0) {
|
||||||
if (!data->bus) {
|
LOG_ERR("failed to initialize bus");
|
||||||
LOG_DBG("master not found: %s",
|
return -EIO;
|
||||||
config->bus_name);
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
config->bus_init(dev);
|
|
||||||
|
|
||||||
if (iis2iclx_init_chip(dev) < 0) {
|
if (iis2iclx_init_chip(dev) < 0) {
|
||||||
LOG_ERR("failed to initialize chip");
|
LOG_ERR("failed to initialize chip");
|
||||||
return -EIO;
|
return -EIO;
|
||||||
|
@ -646,45 +641,6 @@ static int iis2iclx_init(const struct device *dev)
|
||||||
* Instantiation macros used when a device is on a SPI bus.
|
* Instantiation macros used when a device is on a SPI bus.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define IIS2ICLX_HAS_CS(inst) DT_INST_SPI_DEV_HAS_CS_GPIOS(inst)
|
|
||||||
|
|
||||||
#define IIS2ICLX_DATA_SPI_CS(inst) \
|
|
||||||
{ .cs_ctrl = { \
|
|
||||||
.gpio_pin = DT_INST_SPI_DEV_CS_GPIOS_PIN(inst), \
|
|
||||||
.gpio_dt_flags = DT_INST_SPI_DEV_CS_GPIOS_FLAGS(inst), \
|
|
||||||
}, \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define IIS2ICLX_DATA_SPI(inst) \
|
|
||||||
COND_CODE_1(IIS2ICLX_HAS_CS(inst), \
|
|
||||||
(IIS2ICLX_DATA_SPI_CS(inst)), \
|
|
||||||
({}))
|
|
||||||
|
|
||||||
#define IIS2ICLX_SPI_CS_PTR(inst) \
|
|
||||||
COND_CODE_1(IIS2ICLX_HAS_CS(inst), \
|
|
||||||
(&(iis2iclx_data_##inst.cs_ctrl)), \
|
|
||||||
(NULL))
|
|
||||||
|
|
||||||
#define IIS2ICLX_SPI_CS_LABEL(inst) \
|
|
||||||
COND_CODE_1(IIS2ICLX_HAS_CS(inst), \
|
|
||||||
(DT_INST_SPI_DEV_CS_GPIOS_LABEL(inst)), (NULL))
|
|
||||||
|
|
||||||
#define IIS2ICLX_SPI_CFG(inst) \
|
|
||||||
(&(struct iis2iclx_spi_cfg) { \
|
|
||||||
.spi_conf = { \
|
|
||||||
.frequency = \
|
|
||||||
DT_INST_PROP(inst, spi_max_frequency), \
|
|
||||||
.operation = (SPI_WORD_SET(8) | \
|
|
||||||
SPI_OP_MODE_MASTER | \
|
|
||||||
SPI_MODE_CPOL | \
|
|
||||||
SPI_MODE_CPHA), \
|
|
||||||
.slave = DT_INST_REG_ADDR(inst), \
|
|
||||||
.cs = IIS2ICLX_SPI_CS_PTR(inst), \
|
|
||||||
}, \
|
|
||||||
.cs_gpios_label = IIS2ICLX_SPI_CS_LABEL(inst), \
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef CONFIG_IIS2ICLX_TRIGGER
|
#ifdef CONFIG_IIS2ICLX_TRIGGER
|
||||||
#define IIS2ICLX_CFG_IRQ(inst) \
|
#define IIS2ICLX_CFG_IRQ(inst) \
|
||||||
.irq_dev_name = DT_INST_GPIO_LABEL(inst, drdy_gpios), \
|
.irq_dev_name = DT_INST_GPIO_LABEL(inst, drdy_gpios), \
|
||||||
|
@ -695,52 +651,51 @@ static int iis2iclx_init(const struct device *dev)
|
||||||
#define IIS2ICLX_CFG_IRQ(inst)
|
#define IIS2ICLX_CFG_IRQ(inst)
|
||||||
#endif /* CONFIG_IIS2ICLX_TRIGGER */
|
#endif /* CONFIG_IIS2ICLX_TRIGGER */
|
||||||
|
|
||||||
|
#define IIS2ICLX_SPI_OPERATION (SPI_WORD_SET(8) | \
|
||||||
|
SPI_OP_MODE_MASTER | \
|
||||||
|
SPI_MODE_CPOL | \
|
||||||
|
SPI_MODE_CPHA) \
|
||||||
|
|
||||||
#define IIS2ICLX_CONFIG_SPI(inst) \
|
#define IIS2ICLX_CONFIG_SPI(inst) \
|
||||||
{ \
|
{ \
|
||||||
.bus_name = DT_INST_BUS_LABEL(inst), \
|
.bus = DEVICE_DT_GET(DT_INST_BUS(inst)), \
|
||||||
.bus_init = iis2iclx_spi_init, \
|
.bus_init = iis2iclx_spi_init, \
|
||||||
.bus_cfg = { .spi_cfg = IIS2ICLX_SPI_CFG(inst) }, \
|
.bus_cfg.spi_cfg = \
|
||||||
|
SPI_CONFIG_DT_INST(inst, \
|
||||||
|
IIS2ICLX_SPI_OPERATION, \
|
||||||
|
0), \
|
||||||
.odr = DT_INST_PROP(inst, odr), \
|
.odr = DT_INST_PROP(inst, odr), \
|
||||||
.range = DT_INST_PROP(inst, range), \
|
.range = DT_INST_PROP(inst, range), \
|
||||||
COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, drdy_gpios), \
|
COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, drdy_gpios), \
|
||||||
(IIS2ICLX_CFG_IRQ(inst)), ()) \
|
(IIS2ICLX_CFG_IRQ(inst)), ()) \
|
||||||
}
|
}
|
||||||
|
|
||||||
#define IIS2ICLX_DEFINE_SPI(inst) \
|
|
||||||
static struct iis2iclx_data iis2iclx_data_##inst = \
|
|
||||||
IIS2ICLX_DATA_SPI(inst); \
|
|
||||||
static const struct iis2iclx_config iis2iclx_config_##inst = \
|
|
||||||
IIS2ICLX_CONFIG_SPI(inst); \
|
|
||||||
IIS2ICLX_DEVICE_INIT(inst)
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Instantiation macros used when a device is on an I2C bus.
|
* Instantiation macros used when a device is on an I2C bus.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define IIS2ICLX_CONFIG_I2C(inst) \
|
#define IIS2ICLX_CONFIG_I2C(inst) \
|
||||||
{ \
|
{ \
|
||||||
.bus_name = DT_INST_BUS_LABEL(inst), \
|
.bus = DEVICE_DT_GET(DT_INST_BUS(inst)), \
|
||||||
.bus_init = iis2iclx_i2c_init, \
|
.bus_init = iis2iclx_i2c_init, \
|
||||||
.bus_cfg = { .i2c_slv_addr = DT_INST_REG_ADDR(inst), }, \
|
.bus_cfg.i2c_slv_addr = DT_INST_REG_ADDR(inst), \
|
||||||
.odr = DT_INST_PROP(inst, odr), \
|
.odr = DT_INST_PROP(inst, odr), \
|
||||||
.range = DT_INST_PROP(inst, range), \
|
.range = DT_INST_PROP(inst, range), \
|
||||||
COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, drdy_gpios), \
|
COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, drdy_gpios), \
|
||||||
(IIS2ICLX_CFG_IRQ(inst)), ()) \
|
(IIS2ICLX_CFG_IRQ(inst)), ()) \
|
||||||
}
|
}
|
||||||
|
|
||||||
#define IIS2ICLX_DEFINE_I2C(inst) \
|
|
||||||
static struct iis2iclx_data iis2iclx_data_##inst; \
|
|
||||||
static const struct iis2iclx_config iis2iclx_config_##inst = \
|
|
||||||
IIS2ICLX_CONFIG_I2C(inst); \
|
|
||||||
IIS2ICLX_DEVICE_INIT(inst)
|
|
||||||
/*
|
/*
|
||||||
* Main instantiation macro. Use of COND_CODE_1() selects the right
|
* Main instantiation macro. Use of COND_CODE_1() selects the right
|
||||||
* bus-specific macro at preprocessor time.
|
* bus-specific macro at preprocessor time.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define IIS2ICLX_DEFINE(inst) \
|
#define IIS2ICLX_DEFINE(inst) \
|
||||||
|
static struct iis2iclx_data iis2iclx_data_##inst; \
|
||||||
|
static const struct iis2iclx_config iis2iclx_config_##inst = \
|
||||||
COND_CODE_1(DT_INST_ON_BUS(inst, spi), \
|
COND_CODE_1(DT_INST_ON_BUS(inst, spi), \
|
||||||
(IIS2ICLX_DEFINE_SPI(inst)), \
|
(IIS2ICLX_CONFIG_SPI(inst)), \
|
||||||
(IIS2ICLX_DEFINE_I2C(inst)))
|
(IIS2ICLX_CONFIG_I2C(inst))); \
|
||||||
|
IIS2ICLX_DEVICE_INIT(inst)
|
||||||
|
|
||||||
DT_INST_FOREACH_STATUS_OKAY(IIS2ICLX_DEFINE)
|
DT_INST_FOREACH_STATUS_OKAY(IIS2ICLX_DEFINE)
|
||||||
|
|
|
@ -35,25 +35,18 @@
|
||||||
#define SENSOR_DEG2RAD_DOUBLE (SENSOR_PI_DOUBLE / 180)
|
#define SENSOR_DEG2RAD_DOUBLE (SENSOR_PI_DOUBLE / 180)
|
||||||
#define SENSOR_G_DOUBLE (SENSOR_G / 1000000.0)
|
#define SENSOR_G_DOUBLE (SENSOR_G / 1000000.0)
|
||||||
|
|
||||||
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
|
|
||||||
struct iis2iclx_spi_cfg {
|
|
||||||
struct spi_config spi_conf;
|
|
||||||
const char *cs_gpios_label;
|
|
||||||
};
|
|
||||||
#endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) */
|
|
||||||
|
|
||||||
union iis2iclx_bus_cfg {
|
union iis2iclx_bus_cfg {
|
||||||
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
|
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
|
||||||
uint16_t i2c_slv_addr;
|
uint16_t i2c_slv_addr;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
|
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
|
||||||
const struct iis2iclx_spi_cfg *spi_cfg;
|
struct spi_config spi_cfg;
|
||||||
#endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) */
|
#endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) */
|
||||||
};
|
};
|
||||||
|
|
||||||
struct iis2iclx_config {
|
struct iis2iclx_config {
|
||||||
char *bus_name;
|
const struct device *bus;
|
||||||
int (*bus_init)(const struct device *dev);
|
int (*bus_init)(const struct device *dev);
|
||||||
const union iis2iclx_bus_cfg bus_cfg;
|
const union iis2iclx_bus_cfg bus_cfg;
|
||||||
uint8_t odr;
|
uint8_t odr;
|
||||||
|
@ -119,10 +112,6 @@ struct iis2iclx_data {
|
||||||
struct k_work work;
|
struct k_work work;
|
||||||
#endif
|
#endif
|
||||||
#endif /* CONFIG_IIS2ICLX_TRIGGER */
|
#endif /* CONFIG_IIS2ICLX_TRIGGER */
|
||||||
|
|
||||||
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
|
|
||||||
struct spi_cs_control cs_ctrl;
|
|
||||||
#endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) */
|
|
||||||
};
|
};
|
||||||
|
|
||||||
int iis2iclx_spi_init(const struct device *dev);
|
int iis2iclx_spi_init(const struct device *dev);
|
||||||
|
|
|
@ -19,23 +19,21 @@
|
||||||
|
|
||||||
LOG_MODULE_DECLARE(IIS2ICLX, CONFIG_SENSOR_LOG_LEVEL);
|
LOG_MODULE_DECLARE(IIS2ICLX, CONFIG_SENSOR_LOG_LEVEL);
|
||||||
|
|
||||||
static int iis2iclx_i2c_read(struct iis2iclx_data *data, uint8_t reg_addr,
|
static int iis2iclx_i2c_read(const struct device *dev, uint8_t reg_addr,
|
||||||
uint8_t *value, uint8_t len)
|
uint8_t *value, uint8_t len)
|
||||||
{
|
{
|
||||||
const struct device *dev = data->dev;
|
|
||||||
const struct iis2iclx_config *cfg = dev->config;
|
const struct iis2iclx_config *cfg = dev->config;
|
||||||
|
|
||||||
return i2c_burst_read(data->bus, cfg->bus_cfg.i2c_slv_addr,
|
return i2c_burst_read(cfg->bus, cfg->bus_cfg.i2c_slv_addr,
|
||||||
reg_addr, value, len);
|
reg_addr, value, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int iis2iclx_i2c_write(struct iis2iclx_data *data, uint8_t reg_addr,
|
static int iis2iclx_i2c_write(const struct device *dev, uint8_t reg_addr,
|
||||||
uint8_t *value, uint8_t len)
|
uint8_t *value, uint8_t len)
|
||||||
{
|
{
|
||||||
const struct device *dev = data->dev;
|
|
||||||
const struct iis2iclx_config *cfg = dev->config;
|
const struct iis2iclx_config *cfg = dev->config;
|
||||||
|
|
||||||
return i2c_burst_write(data->bus, cfg->bus_cfg.i2c_slv_addr,
|
return i2c_burst_write(cfg->bus, cfg->bus_cfg.i2c_slv_addr,
|
||||||
reg_addr, value, len);
|
reg_addr, value, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,11 +41,11 @@ int iis2iclx_i2c_init(const struct device *dev)
|
||||||
{
|
{
|
||||||
struct iis2iclx_data *data = dev->data;
|
struct iis2iclx_data *data = dev->data;
|
||||||
|
|
||||||
data->ctx_i2c.read_reg = (stmdev_read_ptr) iis2iclx_i2c_read,
|
data->ctx_i2c.read_reg = (stmdev_read_ptr) iis2iclx_i2c_read;
|
||||||
data->ctx_i2c.write_reg = (stmdev_write_ptr) iis2iclx_i2c_write,
|
data->ctx_i2c.write_reg = (stmdev_write_ptr) iis2iclx_i2c_write;
|
||||||
|
|
||||||
data->ctx = &data->ctx_i2c;
|
data->ctx = &data->ctx_i2c;
|
||||||
data->ctx->handle = data;
|
data->ctx->handle = (void *)dev;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,12 +20,11 @@
|
||||||
|
|
||||||
LOG_MODULE_DECLARE(IIS2ICLX, CONFIG_SENSOR_LOG_LEVEL);
|
LOG_MODULE_DECLARE(IIS2ICLX, CONFIG_SENSOR_LOG_LEVEL);
|
||||||
|
|
||||||
static int iis2iclx_spi_read(struct iis2iclx_data *data, uint8_t reg_addr,
|
static int iis2iclx_spi_read(const struct device *dev, uint8_t reg_addr,
|
||||||
uint8_t *value, uint8_t len)
|
uint8_t *value, uint8_t len)
|
||||||
{
|
{
|
||||||
const struct device *dev = data->dev;
|
|
||||||
const struct iis2iclx_config *cfg = dev->config;
|
const struct iis2iclx_config *cfg = dev->config;
|
||||||
const struct spi_config *spi_cfg = &cfg->bus_cfg.spi_cfg->spi_conf;
|
const struct spi_config *spi_cfg = &cfg->bus_cfg.spi_cfg;
|
||||||
uint8_t buffer_tx[2] = { reg_addr | IIS2ICLX_SPI_READ, 0 };
|
uint8_t buffer_tx[2] = { reg_addr | IIS2ICLX_SPI_READ, 0 };
|
||||||
const struct spi_buf tx_buf = {
|
const struct spi_buf tx_buf = {
|
||||||
.buf = buffer_tx,
|
.buf = buffer_tx,
|
||||||
|
@ -55,19 +54,18 @@ static int iis2iclx_spi_read(struct iis2iclx_data *data, uint8_t reg_addr,
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (spi_transceive(data->bus, spi_cfg, &tx, &rx)) {
|
if (spi_transceive(cfg->bus, spi_cfg, &tx, &rx)) {
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int iis2iclx_spi_write(struct iis2iclx_data *data, uint8_t reg_addr,
|
static int iis2iclx_spi_write(const struct device *dev, uint8_t reg_addr,
|
||||||
uint8_t *value, uint8_t len)
|
uint8_t *value, uint8_t len)
|
||||||
{
|
{
|
||||||
const struct device *dev = data->dev;
|
|
||||||
const struct iis2iclx_config *cfg = dev->config;
|
const struct iis2iclx_config *cfg = dev->config;
|
||||||
const struct spi_config *spi_cfg = &cfg->bus_cfg.spi_cfg->spi_conf;
|
const struct spi_config *spi_cfg = &cfg->bus_cfg.spi_cfg;
|
||||||
uint8_t buffer_tx[1] = { reg_addr & ~IIS2ICLX_SPI_READ };
|
uint8_t buffer_tx[1] = { reg_addr & ~IIS2ICLX_SPI_READ };
|
||||||
const struct spi_buf tx_buf[2] = {
|
const struct spi_buf tx_buf[2] = {
|
||||||
{
|
{
|
||||||
|
@ -89,7 +87,7 @@ static int iis2iclx_spi_write(struct iis2iclx_data *data, uint8_t reg_addr,
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (spi_write(data->bus, spi_cfg, &tx)) {
|
if (spi_write(cfg->bus, spi_cfg, &tx)) {
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,27 +97,12 @@ static int iis2iclx_spi_write(struct iis2iclx_data *data, uint8_t reg_addr,
|
||||||
int iis2iclx_spi_init(const struct device *dev)
|
int iis2iclx_spi_init(const struct device *dev)
|
||||||
{
|
{
|
||||||
struct iis2iclx_data *data = dev->data;
|
struct iis2iclx_data *data = dev->data;
|
||||||
const struct iis2iclx_config *cfg = dev->config;
|
|
||||||
const struct iis2iclx_spi_cfg *spi_cfg = cfg->bus_cfg.spi_cfg;
|
|
||||||
|
|
||||||
data->ctx_spi.read_reg = (stmdev_read_ptr) iis2iclx_spi_read;
|
data->ctx_spi.read_reg = (stmdev_read_ptr) iis2iclx_spi_read;
|
||||||
data->ctx_spi.write_reg = (stmdev_write_ptr) iis2iclx_spi_write;
|
data->ctx_spi.write_reg = (stmdev_write_ptr) iis2iclx_spi_write;
|
||||||
|
|
||||||
data->ctx = &data->ctx_spi;
|
data->ctx = &data->ctx_spi;
|
||||||
data->ctx->handle = data;
|
data->ctx->handle = (void *)dev;
|
||||||
|
|
||||||
if (spi_cfg->cs_gpios_label != NULL) {
|
|
||||||
/* handle SPI CS thru GPIO if it is the case */
|
|
||||||
data->cs_ctrl.gpio_dev =
|
|
||||||
device_get_binding(spi_cfg->cs_gpios_label);
|
|
||||||
if (!data->cs_ctrl.gpio_dev) {
|
|
||||||
LOG_ERR("Unable to get GPIO SPI CS device");
|
|
||||||
return -ENODEV;
|
|
||||||
}
|
|
||||||
|
|
||||||
LOG_DBG("SPI GPIO CS configured on %s:%u",
|
|
||||||
spi_cfg->cs_gpios_label, data->cs_ctrl.gpio_pin);
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue