Skip to content

Commit f3ad909

Browse files
aviscontiMaureenHelm
authored andcommitted
drivers/sensor: lis2ds12: Add multi-instance support
This commit aligns lis2ds12 sensor driver to latest multi instance sensor driver model. In particular it makes use of the stmemsc common routines and move ctx handler inside struct config, so that the bus_init routines can be totally avoided. Signed-off-by: Armando Visconti <[email protected]>
1 parent 1c0acad commit f3ad909

File tree

6 files changed

+186
-284
lines changed

6 files changed

+186
-284
lines changed

drivers/sensor/lis2ds12/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
zephyr_library()
44

55
zephyr_library_sources(lis2ds12.c)
6-
zephyr_library_sources(lis2ds12_i2c.c)
7-
zephyr_library_sources(lis2ds12_spi.c)
86
zephyr_library_sources_ifdef(CONFIG_LIS2DS12_TRIGGER lis2ds12_trigger.c)
7+
8+
zephyr_library_include_directories(../stmemsc)

drivers/sensor/lis2ds12/lis2ds12.c

Lines changed: 107 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,10 @@
2323

2424
LOG_MODULE_REGISTER(LIS2DS12, CONFIG_SENSOR_LOG_LEVEL);
2525

26-
static struct lis2ds12_data lis2ds12_data;
27-
28-
static struct lis2ds12_config lis2ds12_config = {
29-
.comm_master_dev_name = DT_INST_BUS_LABEL(0),
30-
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
31-
.bus_init = lis2ds12_spi_init,
32-
#elif DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
33-
.bus_init = lis2ds12_i2c_init,
34-
#else
35-
#error "BUS MACRO NOT DEFINED IN DTS"
36-
#endif
37-
#ifdef CONFIG_LIS2DS12_TRIGGER
38-
.irq_port = DT_INST_GPIO_LABEL(0, irq_gpios),
39-
.irq_pin = DT_INST_GPIO_PIN(0, irq_gpios),
40-
.irq_flags = DT_INST_GPIO_FLAGS(0, irq_gpios),
41-
#endif
42-
};
43-
4426
static int lis2ds12_set_odr(const struct device *dev, uint16_t odr)
4527
{
46-
const struct lis2ds12_data *data = dev->data;
47-
stmdev_ctx_t *ctx = (stmdev_ctx_t *)data->ctx;
28+
const struct lis2ds12_config *cfg = dev->config;
29+
stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
4830
uint8_t val;
4931

5032
/* check if power off */
@@ -65,7 +47,8 @@ static int lis2ds12_set_range(const struct device *dev, uint8_t range)
6547
{
6648
int err;
6749
struct lis2ds12_data *data = dev->data;
68-
stmdev_ctx_t *ctx = (stmdev_ctx_t *)data->ctx;
50+
const struct lis2ds12_config *cfg = dev->config;
51+
stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
6952

7053
switch (range) {
7154
default:
@@ -127,7 +110,8 @@ static int lis2ds12_attr_set(const struct device *dev,
127110
static int lis2ds12_sample_fetch_accel(const struct device *dev)
128111
{
129112
struct lis2ds12_data *data = dev->data;
130-
stmdev_ctx_t *ctx = (stmdev_ctx_t *)data->ctx;
113+
const struct lis2ds12_config *cfg = dev->config;
114+
stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
131115
int16_t buf[3];
132116

133117
/* fetch raw data sample */
@@ -220,7 +204,7 @@ static int lis2ds12_channel_get(const struct device *dev,
220204
return lis2ds12_get_channel(chan, val, data, data->gain);
221205
}
222206

223-
static const struct sensor_driver_api lis2ds12_api_funcs = {
207+
static const struct sensor_driver_api lis2ds12_driver_api = {
224208
.attr_set = lis2ds12_attr_set,
225209
#if defined(CONFIG_LIS2DS12_TRIGGER)
226210
.trigger_set = lis2ds12_trigger_set,
@@ -231,31 +215,20 @@ static const struct sensor_driver_api lis2ds12_api_funcs = {
231215

232216
static int lis2ds12_init(const struct device *dev)
233217
{
234-
const struct lis2ds12_config * const config = dev->config;
235-
struct lis2ds12_data *data = dev->data;
236-
stmdev_ctx_t *ctx;
218+
const struct lis2ds12_config * const cfg = dev->config;
219+
stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
237220
uint8_t chip_id;
238221
int ret;
239222

240-
data->comm_master = device_get_binding(config->comm_master_dev_name);
241-
if (!data->comm_master) {
242-
LOG_ERR("master not found: %s",
243-
config->comm_master_dev_name);
244-
return -EINVAL;
245-
}
246-
247-
config->bus_init(dev);
248-
249-
ctx = (stmdev_ctx_t *)data->ctx;
250223
/* check chip ID */
251224
ret = lis2ds12_device_id_get(ctx, &chip_id);
252225
if (ret < 0) {
253-
LOG_ERR("Not able to read dev id");
226+
LOG_ERR("%s: Not able to read dev id", dev->name);
254227
return ret;
255228
}
256229

257230
if (chip_id != LIS2DS12_ID) {
258-
LOG_ERR("Invalid chip ID 0x%02x", chip_id);
231+
LOG_ERR("%s: Invalid chip ID 0x%02x", dev->name, chip_id);
259232
return -EINVAL;
260233
}
261234

@@ -267,32 +240,119 @@ static int lis2ds12_init(const struct device *dev)
267240

268241
k_busy_wait(100);
269242

270-
LOG_DBG("chip id 0x%x", chip_id);
243+
LOG_DBG("%s: chip id 0x%x", dev->name, chip_id);
271244

272245
#ifdef CONFIG_LIS2DS12_TRIGGER
273-
if (lis2ds12_trigger_init(dev) < 0) {
274-
LOG_ERR("Failed to initialize triggers.");
275-
return -EIO;
246+
ret = lis2ds12_trigger_init(dev);
247+
if (ret < 0) {
248+
LOG_ERR("%s: Failed to initialize triggers", dev->name);
249+
return ret;
276250
}
277251
#endif
278252

279253
/* set sensor default odr */
280254
ret = lis2ds12_set_odr(dev, 12);
281255
if (ret < 0) {
282-
LOG_ERR("odr init error (12.5 Hz)");
256+
LOG_ERR("%s: odr init error (12.5 Hz)", dev->name);
283257
return ret;
284258
}
285259

286260
/* set sensor default scale */
287261
ret = lis2ds12_set_range(dev, CONFIG_LIS2DS12_FS);
288262
if (ret < 0) {
289-
LOG_ERR("range init error %d", CONFIG_LIS2DS12_FS);
263+
LOG_ERR("%s: range init error %d", dev->name, CONFIG_LIS2DS12_FS);
290264
return ret;
291265
}
292266

293267
return 0;
294268
}
295269

296-
DEVICE_DT_INST_DEFINE(0, lis2ds12_init, NULL,
297-
&lis2ds12_data, &lis2ds12_config, POST_KERNEL,
298-
CONFIG_SENSOR_INIT_PRIORITY, &lis2ds12_api_funcs);
270+
#if DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT) == 0
271+
#warning "LIS2DS12 driver enabled without any devices"
272+
#endif
273+
274+
/*
275+
* Device creation macro, shared by LIS2DS12_DEFINE_SPI() and
276+
* LIS2DS12_DEFINE_I2C().
277+
*/
278+
279+
#define LIS2DS12_DEVICE_INIT(inst) \
280+
DEVICE_DT_INST_DEFINE(inst, \
281+
lis2ds12_init, \
282+
NULL, \
283+
&lis2ds12_data_##inst, \
284+
&lis2ds12_config_##inst, \
285+
POST_KERNEL, \
286+
CONFIG_SENSOR_INIT_PRIORITY, \
287+
&lis2ds12_driver_api);
288+
289+
/*
290+
* Instantiation macros used when a device is on a SPI bus.
291+
*/
292+
293+
#ifdef CONFIG_LIS2DS12_TRIGGER
294+
#define LIS2DS12_CFG_IRQ(inst) \
295+
.gpio_int = GPIO_DT_SPEC_INST_GET(inst, irq_gpios),
296+
#else
297+
#define LIS2DS12_CFG_IRQ(inst)
298+
#endif /* CONFIG_LIS2DS12_TRIGGER */
299+
300+
#define LIS2DS12_SPI_OPERATION (SPI_WORD_SET(8) | \
301+
SPI_OP_MODE_MASTER | \
302+
SPI_MODE_CPOL | \
303+
SPI_MODE_CPHA) \
304+
305+
#define LIS2DS12_CONFIG_SPI(inst) \
306+
{ \
307+
.ctx = { \
308+
.read_reg = \
309+
(stmdev_read_ptr) stmemsc_spi_read, \
310+
.write_reg = \
311+
(stmdev_write_ptr) stmemsc_spi_write, \
312+
.handle = \
313+
(void *)&lis2ds12_config_##inst.stmemsc_cfg, \
314+
}, \
315+
.stmemsc_cfg = { \
316+
.spi = SPI_DT_SPEC_INST_GET(inst, \
317+
LIS2DS12_SPI_OPERATION, \
318+
0), \
319+
}, \
320+
COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, irq_gpios), \
321+
(LIS2DS12_CFG_IRQ(inst)), ()) \
322+
}
323+
324+
/*
325+
* Instantiation macros used when a device is on an I2C bus.
326+
*/
327+
328+
#define LIS2DS12_CONFIG_I2C(inst) \
329+
{ \
330+
.ctx = { \
331+
.read_reg = \
332+
(stmdev_read_ptr) stmemsc_i2c_read, \
333+
.write_reg = \
334+
(stmdev_write_ptr) stmemsc_i2c_write, \
335+
.handle = \
336+
(void *)&lis2ds12_config_##inst.stmemsc_cfg, \
337+
}, \
338+
.stmemsc_cfg = { \
339+
.i2c = I2C_DT_SPEC_INST_GET(inst), \
340+
}, \
341+
COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, irq_gpios), \
342+
(LIS2DS12_CFG_IRQ(inst)), ()) \
343+
}
344+
345+
/*
346+
* Main instantiation macro. Use of COND_CODE_1() selects the right
347+
* bus-specific macro at preprocessor time.
348+
*/
349+
350+
#define LIS2DS12_DEFINE(inst) \
351+
static struct lis2ds12_data lis2ds12_data_##inst; \
352+
static const struct lis2ds12_config lis2ds12_config_##inst = \
353+
COND_CODE_1(DT_INST_ON_BUS(inst, spi), \
354+
(LIS2DS12_CONFIG_SPI(inst)), \
355+
(LIS2DS12_CONFIG_I2C(inst))); \
356+
LIS2DS12_DEVICE_INIT(inst)
357+
358+
DT_INST_FOREACH_STATUS_OKAY(LIS2DS12_DEFINE)

drivers/sensor/lis2ds12/lis2ds12.h

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,44 @@
1414
#include <zephyr/types.h>
1515
#include <drivers/sensor.h>
1616
#include <drivers/gpio.h>
17+
#include <stmemsc.h>
1718
#include "lis2ds12_reg.h"
1819

20+
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
21+
#include <drivers/spi.h>
22+
#endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) */
23+
24+
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
25+
#include <drivers/i2c.h>
26+
#endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) */
27+
1928
/* Return ODR reg value based on data rate set */
2029
#define LIS2DS12_HR_ODR_TO_REG(_odr) \
2130
((_odr <= 12) ? LIS2DS12_XL_ODR_12Hz5_HR : \
2231
((31 - __builtin_clz(_odr / 25))) + 2)
2332

2433
struct lis2ds12_config {
25-
char *comm_master_dev_name;
26-
int (*bus_init)(const struct device *dev);
34+
stmdev_ctx_t ctx;
35+
union {
36+
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
37+
const struct i2c_dt_spec i2c;
38+
#endif
39+
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
40+
const struct spi_dt_spec spi;
41+
#endif
42+
} stmemsc_cfg;
2743
#ifdef CONFIG_LIS2DS12_TRIGGER
28-
const char *irq_port;
29-
gpio_pin_t irq_pin;
30-
gpio_dt_flags_t irq_flags;
44+
struct gpio_dt_spec gpio_int;
3145
#endif
3246
};
3347

3448
struct lis2ds12_data {
35-
stmdev_ctx_t *ctx;
36-
const struct device *comm_master;
3749
int sample_x;
3850
int sample_y;
3951
int sample_z;
4052
float gain;
4153

4254
#ifdef CONFIG_LIS2DS12_TRIGGER
43-
const struct device *gpio;
4455
struct gpio_callback gpio_cb;
4556

4657
struct sensor_trigger data_ready_trigger;
@@ -58,9 +69,6 @@ struct lis2ds12_data {
5869
#endif /* CONFIG_LIS2DS12_TRIGGER */
5970
};
6071

61-
int lis2ds12_spi_init(const struct device *dev);
62-
int lis2ds12_i2c_init(const struct device *dev);
63-
6472
#ifdef CONFIG_LIS2DS12_TRIGGER
6573
int lis2ds12_trigger_set(const struct device *dev,
6674
const struct sensor_trigger *trig,

drivers/sensor/lis2ds12/lis2ds12_i2c.c

Lines changed: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
 (0)