1 /* bmm150.c - Driver for Bosch BMM150 Geomagnetic Sensor */
2
3 /*
4 * Copyright (c) 2017 Intel Corporation
5 * Copyright (c) 2023 FTP Technologies
6 *
7 * SPDX-License-Identifier: Apache-2.0
8 */
9
10 #include <zephyr/logging/log.h>
11 #include "bmm150.h"
12
13 LOG_MODULE_REGISTER(BMM150, CONFIG_SENSOR_LOG_LEVEL);
14
15 static const struct {
16 int freq;
17 uint8_t reg_val;
18 } bmm150_samp_freq_table[] = { { 2, 0x01 },
19 { 6, 0x02 },
20 { 8, 0x03 },
21 { 10, 0x00 },
22 { 15, 0x04 },
23 { 20, 0x05 },
24 { 25, 0x06 },
25 { 30, 0x07 } };
26
27 static const struct bmm150_preset {
28 uint8_t rep_xy;
29 uint8_t rep_z;
30 uint8_t odr;
31 } bmm150_presets_table[] = {
32 [BMM150_LOW_POWER_PRESET] = { 3, 3, 10 },
33 [BMM150_REGULAR_PRESET] = { 9, 15, 10 },
34 [BMM150_ENHANCED_REGULAR_PRESET] = { 15, 27, 10 },
35 [BMM150_HIGH_ACCURACY_PRESET] = { 47, 83, 20 }
36 };
37
bmm150_bus_check(const struct device * dev)38 static inline int bmm150_bus_check(const struct device *dev)
39 {
40 const struct bmm150_config *cfg = dev->config;
41
42 return cfg->bus_io->check(&cfg->bus);
43 }
44
bmm150_reg_read(const struct device * dev,uint8_t start,uint8_t * buf,int size)45 static inline int bmm150_reg_read(const struct device *dev,
46 uint8_t start, uint8_t *buf, int size)
47 {
48 const struct bmm150_config *cfg = dev->config;
49
50 return cfg->bus_io->read(&cfg->bus, start, buf, size);
51 }
52
bmm150_reg_write(const struct device * dev,uint8_t reg,uint8_t val)53 static inline int bmm150_reg_write(const struct device *dev, uint8_t reg,
54 uint8_t val)
55 {
56 const struct bmm150_config *cfg = dev->config;
57
58 return cfg->bus_io->write(&cfg->bus, reg, val);
59 }
60
bmm150_reg_update_byte(const struct device * dev,uint8_t reg,uint8_t mask,uint8_t value)61 int bmm150_reg_update_byte(const struct device *dev, uint8_t reg,
62 uint8_t mask, uint8_t value)
63 {
64 int ret = 0;
65 uint8_t old_value, new_value;
66
67 ret = bmm150_reg_read(dev, reg, &old_value, 1);
68
69 if (ret < 0) {
70 goto failed;
71 }
72
73 new_value = (old_value & ~mask) | (value & mask);
74
75 if (new_value == old_value) {
76 return 0;
77 }
78
79 return bmm150_reg_write(dev, reg, new_value);
80 failed:
81 return ret;
82 }
83
84 /* Power control = 'bit' */
bmm150_power_control(const struct device * dev,uint8_t bit)85 static int bmm150_power_control(const struct device *dev, uint8_t bit)
86 {
87 return bmm150_reg_update_byte(dev, BMM150_REG_POWER,
88 BMM150_MASK_POWER_CTL, bit);
89 }
90
91 /* OpMode = 'mode' */
bmm150_opmode(const struct device * dev,uint8_t mode)92 static int bmm150_opmode(const struct device *dev, uint8_t mode)
93 {
94 return bmm150_reg_update_byte(dev, BMM150_REG_OPMODE_ODR,
95 BMM150_MASK_OPMODE,
96 mode << BMM150_SHIFT_OPMODE);
97 }
98
99
bmm150_set_odr(const struct device * dev,uint8_t val)100 static int bmm150_set_odr(const struct device *dev, uint8_t val)
101 {
102 uint8_t i;
103
104 for (i = 0U; i < ARRAY_SIZE(bmm150_samp_freq_table); ++i) {
105 if (val <= bmm150_samp_freq_table[i].freq) {
106 return bmm150_reg_update_byte(dev,
107 BMM150_REG_OPMODE_ODR,
108 BMM150_MASK_ODR,
109 (bmm150_samp_freq_table[i].reg_val <<
110 BMM150_SHIFT_ODR));
111 }
112 }
113 return -ENOTSUP;
114 }
115
116 #if defined(BMM150_SET_ATTR_REP)
bmm150_read_rep_xy(const struct device * dev)117 static int bmm150_read_rep_xy(const struct device *dev)
118 {
119 struct bmm150_data *data = dev->driver->data;
120 const struct bmm150_config *config = dev->config;
121 uint8_t reg_val;
122
123 if (bmm150_reg_read(dev, BMM150_REG_REP_XY, ®_val, 1) < 0) {
124 return -EIO;
125 }
126
127 data->rep_xy = BMM150_REGVAL_TO_REPXY((uint8_t)(reg_val));
128
129 return 0;
130 }
131
bmm150_read_rep_z(const struct device * dev)132 static int bmm150_read_rep_z(const struct device *dev)
133 {
134 struct bmm150_data *data = dev->data;
135 const struct bmm150_config *config = dev->config;
136 uint8_t reg_val;
137
138 if (bmm150_reg_read(dev, BMM150_REG_REP_Z, ®_val, 1) < 0) {
139 return -EIO;
140 }
141
142 data->rep_z = BMM150_REGVAL_TO_REPZ((int)(reg_val));
143
144 return 0;
145 }
146
bmm150_compute_max_odr(const struct device * dev,int rep_xy,int rep_z,int * max_odr)147 static int bmm150_compute_max_odr(const struct device *dev, int rep_xy,
148 int rep_z, int *max_odr)
149 {
150 struct bmm150_data *data = dev->data;
151
152 if (rep_xy == 0) {
153 if (data->rep_xy <= 0) {
154 if (bmm150_read_rep_xy(dev) < 0) {
155 return -EIO;
156 }
157 }
158 rep_xy = data->rep_xy;
159 }
160
161 if (rep_z == 0) {
162 if (data->rep_z <= 0) {
163 if (bmm150_read_rep_z(dev) < 0) {
164 return -EIO;
165 }
166 }
167 rep_z = data->rep_z;
168 }
169
170 /* Equation reference Datasheet 4.2.4 */
171 *max_odr = 1000000 / (145 * rep_xy + 500 * rep_z + 980);
172
173 return 0;
174 }
175 #endif
176
177 #if defined(BMM150_SET_ATTR_REP)
bmm150_read_odr(const struct device * dev)178 static int bmm150_read_odr(const struct device *dev)
179 {
180 struct bmm150_data *data = dev->data;
181 const struct bmm150_config *config = dev->config;
182 uint8_t i, odr_val, reg_val;
183
184 if (bmm150_reg_read(dev, BMM150_REG_OPMODE_ODR, ®_val, 1) < 0) {
185 return -EIO;
186 }
187
188 odr_val = (reg_val & BMM150_MASK_ODR) >> BMM150_SHIFT_ODR;
189
190 for (i = 0U; i < ARRAY_SIZE(bmm150_samp_freq_table); ++i) {
191 if (bmm150_samp_freq_table[i].reg_val == odr_val) {
192 data->odr = bmm150_samp_freq_table[i].freq;
193 return 0;
194 }
195 }
196
197 return -ENOTSUP;
198 }
199 #endif
200
201 #if defined(CONFIG_BMM150_SAMPLING_REP_XY)
bmm150_write_rep_xy(const struct device * dev,int val)202 static int bmm150_write_rep_xy(const struct device *dev, int val)
203 {
204 struct bmm150_data *data = dev->data;
205 const struct bmm150_config *config = dev->config;
206
207 if (bmm150_reg_update_byte(dev,
208 BMM150_REG_REP_XY,
209 BMM150_REG_REP_DATAMASK,
210 BMM150_REPXY_TO_REGVAL(val)) < 0) {
211 return -EIO;
212 }
213
214 data->rep_xy = val;
215
216 return 0;
217 }
218 #endif
219
220 #if defined(CONFIG_BMM150_SAMPLING_REP_Z)
bmm150_write_rep_z(const struct device * dev,int val)221 static int bmm150_write_rep_z(const struct device *dev, int val)
222 {
223 struct bmm150_data *data = dev->data;
224 const struct bmm150_config *config = dev->config;
225
226 if (bmm150_reg_update_byte(dev,
227 BMM150_REG_REP_Z,
228 BMM150_REG_REP_DATAMASK,
229 BMM150_REPZ_TO_REGVAL(val)) < 0) {
230 return -EIO;
231 }
232
233 data->rep_z = val;
234
235 return 0;
236 }
237 #endif
238
239 /* Reference Datasheet 4.3.2 */
bmm150_compensate_xy(struct bmm150_trim_regs * tregs,int16_t xy,uint16_t rhall,bool is_x)240 static int32_t bmm150_compensate_xy(struct bmm150_trim_regs *tregs,
241 int16_t xy, uint16_t rhall, bool is_x)
242 {
243 int8_t txy1, txy2;
244 int16_t val;
245 uint16_t prevalue;
246 int32_t temp1, temp2, temp3;
247
248 if (xy == BMM150_XY_OVERFLOW_VAL) {
249 return INT32_MIN;
250 }
251
252 if (!rhall) {
253 rhall = tregs->xyz1;
254 }
255
256 if (is_x) {
257 txy1 = tregs->x1;
258 txy2 = tregs->x2;
259 } else {
260 txy1 = tregs->y1;
261 txy2 = tregs->y2;
262 }
263
264 prevalue = (uint16_t)((((int32_t)tregs->xyz1) << 14) / rhall);
265
266 val = (int16_t)((prevalue) - ((uint16_t)0x4000));
267
268 temp1 = (((int32_t)tregs->xy2) * ((((int32_t)val) * ((int32_t)val)) >> 7));
269
270 temp2 = ((int32_t)val) * ((int32_t)(((int16_t)tregs->xy1) << 7));
271
272 temp3 = (((((temp1 + temp2) >> 9) +
273 ((int32_t)0x100000)) * ((int32_t)(((int16_t)txy2) +
274 ((int16_t)0xA0)))) >> 12);
275
276 val = ((int16_t)((((int32_t)xy) * temp3) >> 13)) + (((int16_t)txy1) << 3);
277
278 return (int32_t)val;
279 }
280
bmm150_compensate_z(struct bmm150_trim_regs * tregs,int16_t z,uint16_t rhall)281 static int32_t bmm150_compensate_z(struct bmm150_trim_regs *tregs,
282 int16_t z, uint16_t rhall)
283 {
284 int32_t val, temp1, temp2;
285 int16_t temp3;
286
287 if (z == BMM150_Z_OVERFLOW_VAL) {
288 return INT32_MIN;
289 }
290
291 temp1 = (((int32_t)(z - tregs->z4)) << 15);
292
293 temp2 = ((((int32_t)tregs->z3) *
294 ((int32_t)(((int16_t)rhall) - ((int16_t)tregs->xyz1)))) >> 2);
295
296 temp3 = ((int16_t)(((((int32_t)tregs->z1) *
297 ((((int16_t)rhall) << 1))) + (1 << 15)) >> 16));
298
299 val = ((temp1 - temp2) / (tregs->z2 + temp3));
300
301 return val;
302 }
303
bmm150_sample_fetch(const struct device * dev,enum sensor_channel chan)304 static int bmm150_sample_fetch(const struct device *dev,
305 enum sensor_channel chan)
306 {
307
308 struct bmm150_data *drv_data = dev->data;
309 uint16_t values[BMM150_AXIS_XYZR_MAX];
310 int16_t raw_x, raw_y, raw_z;
311 uint16_t rhall;
312
313 __ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL ||
314 chan == SENSOR_CHAN_MAGN_XYZ);
315
316 if (bmm150_reg_read(dev, BMM150_REG_X_L, (uint8_t *)values, sizeof(values)) < 0) {
317 LOG_ERR("failed to read sample");
318 return -EIO;
319 }
320
321 raw_x = (int16_t)sys_le16_to_cpu(values[BMM150_AXIS_X]) >>
322 BMM150_SHIFT_XY_L;
323 raw_y = (int16_t)sys_le16_to_cpu(values[BMM150_AXIS_Y]) >>
324 BMM150_SHIFT_XY_L;
325 raw_z = (int16_t)sys_le16_to_cpu(values[BMM150_AXIS_Z]) >>
326 BMM150_SHIFT_Z_L;
327
328 rhall = sys_le16_to_cpu(values[BMM150_RHALL]) >>
329 BMM150_SHIFT_RHALL_L;
330
331 drv_data->sample_x = bmm150_compensate_xy(&drv_data->tregs,
332 raw_x, rhall, true);
333 drv_data->sample_y = bmm150_compensate_xy(&drv_data->tregs,
334 raw_y, rhall, false);
335 drv_data->sample_z = bmm150_compensate_z(&drv_data->tregs,
336 raw_z, rhall);
337
338 return 0;
339 }
340
341 /*
342 * Datasheet specify raw units are 16 LSB/uT and this function converts it to
343 * Gauss
344 */
bmm150_convert(struct sensor_value * val,int raw_val)345 static void bmm150_convert(struct sensor_value *val, int raw_val)
346 {
347 /* val = raw_val / 1600 */
348 val->val1 = raw_val / 1600;
349 val->val2 = ((int32_t)raw_val * (1000000 / 1600)) % 1000000;
350 }
351
bmm150_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)352 static int bmm150_channel_get(const struct device *dev,
353 enum sensor_channel chan,
354 struct sensor_value *val)
355 {
356 struct bmm150_data *drv_data = dev->data;
357
358 switch (chan) {
359 case SENSOR_CHAN_MAGN_X:
360 bmm150_convert(val, drv_data->sample_x);
361 break;
362 case SENSOR_CHAN_MAGN_Y:
363 bmm150_convert(val, drv_data->sample_y);
364 break;
365 case SENSOR_CHAN_MAGN_Z:
366 bmm150_convert(val, drv_data->sample_z);
367 break;
368 case SENSOR_CHAN_MAGN_XYZ:
369 bmm150_convert(val, drv_data->sample_x);
370 bmm150_convert(val + 1, drv_data->sample_y);
371 bmm150_convert(val + 2, drv_data->sample_z);
372 break;
373 default:
374 return -ENOTSUP;
375 }
376
377 return 0;
378 }
379
380 #if defined(BMM150_SET_ATTR_REP)
bmm150_attr_set_rep(const struct device * dev,enum sensor_channel chan,const struct sensor_value * val)381 static inline int bmm150_attr_set_rep(const struct device *dev,
382 enum sensor_channel chan,
383 const struct sensor_value *val)
384 {
385 struct bmm150_data *data = dev->data;
386 int max_odr;
387
388 switch (chan) {
389 #if defined(CONFIG_BMM150_SAMPLING_REP_XY)
390 case SENSOR_CHAN_MAGN_X:
391 case SENSOR_CHAN_MAGN_Y:
392 if (val->val1 < 1 || val->val1 > 511) {
393 return -EINVAL;
394 }
395
396 if (bmm150_compute_max_odr(dev, val->val1, 0,
397 &max_odr) < 0) {
398 return -EIO;
399 }
400
401 if (data->odr <= 0) {
402 if (bmm150_read_odr(dev) < 0) {
403 return -EIO;
404 }
405 }
406
407 if (data->odr > max_odr) {
408 return -EINVAL;
409 }
410
411 if (bmm150_write_rep_xy(dev, val->val1) < 0) {
412 return -EIO;
413 }
414 break;
415 #endif
416
417 #if defined(CONFIG_BMM150_SAMPLING_REP_Z)
418 case SENSOR_CHAN_MAGN_Z:
419 if (val->val1 < 1 || val->val1 > 256) {
420 return -EINVAL;
421 }
422
423 if (bmm150_compute_max_odr(dev, 0, val->val1,
424 &max_odr) < 0) {
425 return -EIO;
426 }
427
428 if (data->odr <= 0) {
429 if (bmm150_read_odr(dev) < 0) {
430 return -EIO;
431 }
432 }
433
434 if (data->odr > max_odr) {
435 return -EINVAL;
436 }
437
438 if (bmm150_write_rep_z(dev, val->val1) < 0) {
439 return -EIO;
440 }
441 break;
442 #endif
443 default:
444 return -EINVAL;
445 }
446
447 return 0;
448 }
449 #endif
450
451 #if defined(BMM150_SET_ATTR_REP)
bmm150_attr_set(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)452 static int bmm150_attr_set(const struct device *dev,
453 enum sensor_channel chan,
454 enum sensor_attribute attr,
455 const struct sensor_value *val)
456 {
457 struct bmm150_magn_data *data = dev->data;
458
459 switch (attr) {
460 #if defined(CONFIG_BMM150_SAMPLING_RATE_RUNTIME)
461 case SENSOR_ATTR_SAMPLING_FREQUENCY:
462 if (data->max_odr <= 0) {
463 if (bmm150_compute_max_odr(dev, 0, 0,
464 &data->max_odr) < 0) {
465 return -EIO;
466 }
467 }
468
469 if (data->max_odr < val->val1) {
470 LOG_ERR("not supported with current oversampling");
471 return -ENOTSUP;
472 }
473
474 if (bmm150_set_odr(dev, (uint8_t)(val->val1)) < 0) {
475 return -EIO;
476 }
477 break;
478 #endif
479 #if defined(BMM150_SET_ATTR_REP)
480 case SENSOR_ATTR_OVERSAMPLING:
481 bmm150_attr_set_rep(dev, chan, val);
482 break;
483 #endif
484 default:
485 return -EINVAL;
486 }
487
488 return 0;
489 }
490 #endif
491
492 static DEVICE_API(sensor, bmm150_api_funcs) = {
493 #if defined(BMM150_SET_ATTR_REP)
494 .attr_set = bmm150_attr_set,
495 #endif
496 .sample_fetch = bmm150_sample_fetch,
497 .channel_get = bmm150_channel_get,
498
499 #ifdef CONFIG_BMM150_TRIGGER
500 .trigger_set = bmm150_trigger_set,
501 #endif
502 };
503
bmm150_full_por(const struct device * dev)504 static int bmm150_full_por(const struct device *dev)
505 {
506 int ret;
507
508 /* Ensure we are not in suspend mode so soft reset is not ignored */
509 ret = bmm150_power_control(dev, 1);
510 if (ret != 0) {
511 LOG_ERR("failed to ensure not in suspend mode: %d", ret);
512 return ret;
513 }
514
515 k_sleep(BMM150_START_UP_TIME);
516
517 /* Soft reset always brings the device into sleep mode */
518 ret = bmm150_reg_update_byte(dev, BMM150_REG_POWER,
519 BMM150_MASK_SOFT_RESET,
520 BMM150_SOFT_RESET);
521 if (ret != 0) {
522 LOG_ERR("failed soft reset: %d", ret);
523 return ret;
524 }
525
526 /*
527 * To perform full POR (after soft reset), bring the device into suspend
528 * mode then back into sleep mode, see datasheet section 5.6
529 */
530 ret = bmm150_power_control(dev, 0);
531 if (ret != 0) {
532 LOG_ERR("failed to enter suspend mode: %d", ret);
533 return ret;
534 }
535
536 k_sleep(BMM150_POR_TIME);
537
538 /* Full POR - back into sleep mode */
539 ret = bmm150_power_control(dev, 1);
540 if (ret != 0) {
541 LOG_ERR("failed to go back into sleep mode: %d", ret);
542 return ret;
543 }
544
545 k_sleep(BMM150_START_UP_TIME);
546
547 return 0;
548 }
549
bmm150_init_chip(const struct device * dev)550 static int bmm150_init_chip(const struct device *dev)
551 {
552 struct bmm150_data *data = dev->data;
553 struct bmm150_preset preset;
554 uint8_t chip_id;
555 int ret = -EIO;
556
557 if (bmm150_full_por(dev) != 0) {
558 goto err_poweroff;
559 }
560
561 /* Read chip ID (can only be read in sleep mode)*/
562 if (bmm150_reg_read(dev, BMM150_REG_CHIP_ID, &chip_id, 1) < 0) {
563 LOG_ERR("failed reading chip id");
564 goto err_poweroff;
565 }
566
567 if (chip_id != BMM150_CHIP_ID_VAL) {
568 LOG_ERR("invalid chip id 0x%x", chip_id);
569 goto err_poweroff;
570 }
571
572 /* Setting preset mode */
573 preset = bmm150_presets_table[BMM150_DEFAULT_PRESET];
574 if (bmm150_set_odr(dev, preset.odr) < 0) {
575 LOG_ERR("failed to set ODR to %d",
576 preset.odr);
577 goto err_poweroff;
578 }
579
580 if (bmm150_reg_write(dev, BMM150_REG_REP_XY, BMM150_REPXY_TO_REGVAL(preset.rep_xy))
581 < 0) {
582 LOG_ERR("failed to set REP XY to %d",
583 preset.rep_xy);
584 goto err_poweroff;
585 }
586
587 if (bmm150_reg_write(dev, BMM150_REG_REP_Z, BMM150_REPZ_TO_REGVAL(preset.rep_z)) < 0) {
588 LOG_ERR("failed to set REP Z to %d",
589 preset.rep_z);
590 goto err_poweroff;
591 }
592
593 /* Set chip normal mode */
594 if (bmm150_opmode(dev, BMM150_MODE_NORMAL) < 0) {
595 LOG_ERR("failed to enter normal mode");
596 }
597
598 /* Reads the trim registers of the sensor */
599 if (bmm150_reg_read(dev, BMM150_REG_TRIM_START, (uint8_t *)&data->tregs,
600 sizeof(data->tregs)) < 0) {
601 LOG_ERR("failed to read trim regs");
602 goto err_poweroff;
603 }
604
605 data->rep_xy = 0;
606 data->rep_z = 0;
607 data->odr = 0;
608 data->max_odr = 0;
609 data->sample_x = 0;
610 data->sample_y = 0;
611 data->sample_z = 0;
612
613 data->tregs.xyz1 = sys_le16_to_cpu(data->tregs.xyz1);
614 data->tregs.z1 = sys_le16_to_cpu(data->tregs.z1);
615 data->tregs.z2 = sys_le16_to_cpu(data->tregs.z2);
616 data->tregs.z3 = sys_le16_to_cpu(data->tregs.z3);
617 data->tregs.z4 = sys_le16_to_cpu(data->tregs.z4);
618
619 ret = 0;
620 err_poweroff:
621 (void)bmm150_power_control(dev, 0); /* Suspend */
622
623 return ret;
624 }
625
pm_action(const struct device * dev,enum pm_device_action action)626 static int pm_action(const struct device *dev, enum pm_device_action action)
627 {
628 int ret = 0;
629
630 switch (action) {
631 case PM_DEVICE_ACTION_TURN_ON:
632 ret = bmm150_init_chip(dev);
633 if (ret != 0) {
634 LOG_ERR("failed to initialize chip: %d", ret);
635 }
636 break;
637 case PM_DEVICE_ACTION_RESUME:
638 /* Need to enter sleep mode before setting OpMode to normal */
639 ret = bmm150_power_control(dev, 1);
640 if (ret != 0) {
641 LOG_ERR("failed to enter sleep mode: %d", ret);
642 }
643
644 k_sleep(BMM150_START_UP_TIME);
645
646 ret |= bmm150_opmode(dev, BMM150_MODE_NORMAL);
647 if (ret != 0) {
648 LOG_ERR("failed to enter normal mode: %d", ret);
649 }
650 #ifdef CONFIG_BMM150_TRIGGER
651 else {
652 ret = bmm150_trigger_mode_power_ctrl(dev, true);
653 }
654 break;
655 #endif
656 #ifdef CONFIG_PM_DEVICE
657 case PM_DEVICE_ACTION_SUSPEND:
658 ret = bmm150_power_control(dev, 0); /* Suspend */
659 if (ret != 0) {
660 LOG_ERR("failed to enter suspend mode: %d", ret);
661 }
662 #ifdef CONFIG_BMM150_TRIGGER
663 else {
664 ret = bmm150_trigger_mode_power_ctrl(dev, false);
665 }
666 #endif
667 break;
668 case PM_DEVICE_ACTION_TURN_OFF:
669 break;
670 #endif /* CONFIG_PM_DEVICE */
671 default:
672 return -ENOTSUP;
673 }
674
675 return ret;
676 }
677
bmm150_init(const struct device * dev)678 static int bmm150_init(const struct device *dev)
679 {
680 int err = 0;
681
682 err = bmm150_bus_check(dev);
683 if (err < 0) {
684 LOG_DBG("bus check failed: %d", err);
685 return err;
686 }
687
688 #ifdef CONFIG_BMM150_TRIGGER
689 if (bmm150_trigger_mode_init(dev) < 0) {
690 LOG_ERR("Cannot set up trigger mode.");
691 return -EINVAL;
692 }
693 #endif
694
695 return pm_device_driver_init(dev, pm_action);
696 }
697
698 /* Initializes a struct bmm150_config for an instance on a SPI bus. */
699 #define BMM150_CONFIG_SPI(inst) \
700 .bus.spi = SPI_DT_SPEC_INST_GET(inst, BMM150_SPI_OPERATION, 0), \
701 .bus_io = &bmm150_bus_io_spi,
702
703 /* Initializes a struct bmm150_config for an instance on an I2C bus. */
704 #define BMM150_CONFIG_I2C(inst) \
705 .bus.i2c = I2C_DT_SPEC_INST_GET(inst), \
706 .bus_io = &bmm150_bus_io_i2c,
707
708 #define BMM150_BUS_CFG(inst) \
709 COND_CODE_1(DT_INST_ON_BUS(inst, i2c), \
710 (BMM150_CONFIG_I2C(inst)), \
711 (BMM150_CONFIG_SPI(inst)))
712
713 #if defined(CONFIG_BMM150_TRIGGER)
714 #define BMM150_INT_CFG(inst) \
715 .drdy_int = GPIO_DT_SPEC_INST_GET(inst, drdy_gpios),
716 #else
717 #define BMM150_INT_CFG(inst)
718 #endif
719
720 /*
721 * Main instantiation macro, which selects the correct bus-specific
722 * instantiation macros for the instance.
723 */
724 #define BMM150_DEFINE(inst) \
725 static struct bmm150_data bmm150_data_##inst; \
726 static const struct bmm150_config bmm150_config_##inst = { \
727 BMM150_BUS_CFG(inst) \
728 BMM150_INT_CFG(inst) \
729 }; \
730 \
731 PM_DEVICE_DT_INST_DEFINE(inst, pm_action); \
732 \
733 SENSOR_DEVICE_DT_INST_DEFINE(inst, \
734 bmm150_init, \
735 PM_DEVICE_DT_INST_GET(inst), \
736 &bmm150_data_##inst, \
737 &bmm150_config_##inst, \
738 POST_KERNEL, \
739 CONFIG_SENSOR_INIT_PRIORITY, \
740 &bmm150_api_funcs);
741
742 /* Create the struct device for every status "okay" node in the devicetree. */
743 DT_INST_FOREACH_STATUS_OKAY(BMM150_DEFINE)
744