2323
2424LOG_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-
4426static 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,
127110static 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
232216static 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 )
0 commit comments