Merge tag 'clk-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux.git] / drivers / iio / imu / bmi270 / bmi270_core.c
blob7fec52e0b48624f07031b63a9caf6c318f33f5dc
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
3 #include <linux/bitfield.h>
4 #include <linux/firmware.h>
5 #include <linux/i2c.h>
6 #include <linux/module.h>
7 #include <linux/regmap.h>
9 #include <linux/iio/iio.h>
10 #include <linux/iio/sysfs.h>
11 #include <linux/iio/triggered_buffer.h>
12 #include <linux/iio/trigger_consumer.h>
14 #include "bmi270.h"
16 #define BMI270_CHIP_ID_REG 0x00
18 /* Checked to prevent sending incompatible firmware to BMI160 devices */
19 #define BMI160_CHIP_ID_VAL 0xD1
21 #define BMI260_CHIP_ID_VAL 0x27
22 #define BMI270_CHIP_ID_VAL 0x24
23 #define BMI270_CHIP_ID_MSK GENMASK(7, 0)
25 #define BMI270_ACCEL_X_REG 0x0c
26 #define BMI270_ANG_VEL_X_REG 0x12
28 #define BMI270_INTERNAL_STATUS_REG 0x21
29 #define BMI270_INTERNAL_STATUS_MSG_MSK GENMASK(3, 0)
30 #define BMI270_INTERNAL_STATUS_MSG_INIT_OK 0x01
32 #define BMI270_INTERNAL_STATUS_AXES_REMAP_ERR_MSK BIT(5)
33 #define BMI270_INTERNAL_STATUS_ODR_50HZ_ERR_MSK BIT(6)
35 #define BMI270_ACC_CONF_REG 0x40
36 #define BMI270_ACC_CONF_ODR_MSK GENMASK(3, 0)
37 #define BMI270_ACC_CONF_ODR_100HZ 0x08
38 #define BMI270_ACC_CONF_BWP_MSK GENMASK(6, 4)
39 #define BMI270_ACC_CONF_BWP_NORMAL_MODE 0x02
40 #define BMI270_ACC_CONF_FILTER_PERF_MSK BIT(7)
42 #define BMI270_ACC_CONF_RANGE_REG 0x41
43 #define BMI270_ACC_CONF_RANGE_MSK GENMASK(1, 0)
45 #define BMI270_GYR_CONF_REG 0x42
46 #define BMI270_GYR_CONF_ODR_MSK GENMASK(3, 0)
47 #define BMI270_GYR_CONF_ODR_200HZ 0x09
48 #define BMI270_GYR_CONF_BWP_MSK GENMASK(5, 4)
49 #define BMI270_GYR_CONF_BWP_NORMAL_MODE 0x02
50 #define BMI270_GYR_CONF_NOISE_PERF_MSK BIT(6)
51 #define BMI270_GYR_CONF_FILTER_PERF_MSK BIT(7)
53 #define BMI270_GYR_CONF_RANGE_REG 0x43
54 #define BMI270_GYR_CONF_RANGE_MSK GENMASK(2, 0)
56 #define BMI270_INIT_CTRL_REG 0x59
57 #define BMI270_INIT_CTRL_LOAD_DONE_MSK BIT(0)
59 #define BMI270_INIT_DATA_REG 0x5e
61 #define BMI270_PWR_CONF_REG 0x7c
62 #define BMI270_PWR_CONF_ADV_PWR_SAVE_MSK BIT(0)
63 #define BMI270_PWR_CONF_FIFO_WKUP_MSK BIT(1)
64 #define BMI270_PWR_CONF_FUP_EN_MSK BIT(2)
66 #define BMI270_PWR_CTRL_REG 0x7d
67 #define BMI270_PWR_CTRL_AUX_EN_MSK BIT(0)
68 #define BMI270_PWR_CTRL_GYR_EN_MSK BIT(1)
69 #define BMI270_PWR_CTRL_ACCEL_EN_MSK BIT(2)
70 #define BMI270_PWR_CTRL_TEMP_EN_MSK BIT(3)
72 #define BMI260_INIT_DATA_FILE "bmi260-init-data.fw"
73 #define BMI270_INIT_DATA_FILE "bmi270-init-data.fw"
75 enum bmi270_scan {
76 BMI270_SCAN_ACCEL_X,
77 BMI270_SCAN_ACCEL_Y,
78 BMI270_SCAN_ACCEL_Z,
79 BMI270_SCAN_GYRO_X,
80 BMI270_SCAN_GYRO_Y,
81 BMI270_SCAN_GYRO_Z,
82 BMI270_SCAN_TIMESTAMP,
85 static const unsigned long bmi270_avail_scan_masks[] = {
86 (BIT(BMI270_SCAN_ACCEL_X) |
87 BIT(BMI270_SCAN_ACCEL_Y) |
88 BIT(BMI270_SCAN_ACCEL_Z) |
89 BIT(BMI270_SCAN_GYRO_X) |
90 BIT(BMI270_SCAN_GYRO_Y) |
91 BIT(BMI270_SCAN_GYRO_Z)),
95 const struct bmi270_chip_info bmi260_chip_info = {
96 .name = "bmi260",
97 .chip_id = BMI260_CHIP_ID_VAL,
98 .fw_name = BMI260_INIT_DATA_FILE,
100 EXPORT_SYMBOL_NS_GPL(bmi260_chip_info, "IIO_BMI270");
102 const struct bmi270_chip_info bmi270_chip_info = {
103 .name = "bmi270",
104 .chip_id = BMI270_CHIP_ID_VAL,
105 .fw_name = BMI270_INIT_DATA_FILE,
107 EXPORT_SYMBOL_NS_GPL(bmi270_chip_info, "IIO_BMI270");
109 enum bmi270_sensor_type {
110 BMI270_ACCEL = 0,
111 BMI270_GYRO,
114 struct bmi270_scale {
115 int scale;
116 int uscale;
119 struct bmi270_odr {
120 int odr;
121 int uodr;
124 static const struct bmi270_scale bmi270_accel_scale[] = {
125 { 0, 598 },
126 { 0, 1197 },
127 { 0, 2394 },
128 { 0, 4788 },
131 static const struct bmi270_scale bmi270_gyro_scale[] = {
132 { 0, 1065 },
133 { 0, 532 },
134 { 0, 266 },
135 { 0, 133 },
136 { 0, 66 },
139 struct bmi270_scale_item {
140 const struct bmi270_scale *tbl;
141 int num;
144 static const struct bmi270_scale_item bmi270_scale_table[] = {
145 [BMI270_ACCEL] = {
146 .tbl = bmi270_accel_scale,
147 .num = ARRAY_SIZE(bmi270_accel_scale),
149 [BMI270_GYRO] = {
150 .tbl = bmi270_gyro_scale,
151 .num = ARRAY_SIZE(bmi270_gyro_scale),
155 static const struct bmi270_odr bmi270_accel_odr[] = {
156 { 0, 781250 },
157 { 1, 562500 },
158 { 3, 125000 },
159 { 6, 250000 },
160 { 12, 500000 },
161 { 25, 0 },
162 { 50, 0 },
163 { 100, 0 },
164 { 200, 0 },
165 { 400, 0 },
166 { 800, 0 },
167 { 1600, 0 },
170 static const u8 bmi270_accel_odr_vals[] = {
171 0x01,
172 0x02,
173 0x03,
174 0x04,
175 0x05,
176 0x06,
177 0x07,
178 0x08,
179 0x09,
180 0x0A,
181 0x0B,
182 0x0C,
185 static const struct bmi270_odr bmi270_gyro_odr[] = {
186 { 25, 0 },
187 { 50, 0 },
188 { 100, 0 },
189 { 200, 0 },
190 { 400, 0 },
191 { 800, 0 },
192 { 1600, 0 },
193 { 3200, 0 },
196 static const u8 bmi270_gyro_odr_vals[] = {
197 0x06,
198 0x07,
199 0x08,
200 0x09,
201 0x0A,
202 0x0B,
203 0x0C,
204 0x0D,
207 struct bmi270_odr_item {
208 const struct bmi270_odr *tbl;
209 const u8 *vals;
210 int num;
213 static const struct bmi270_odr_item bmi270_odr_table[] = {
214 [BMI270_ACCEL] = {
215 .tbl = bmi270_accel_odr,
216 .vals = bmi270_accel_odr_vals,
217 .num = ARRAY_SIZE(bmi270_accel_odr),
219 [BMI270_GYRO] = {
220 .tbl = bmi270_gyro_odr,
221 .vals = bmi270_gyro_odr_vals,
222 .num = ARRAY_SIZE(bmi270_gyro_odr),
226 static int bmi270_set_scale(struct bmi270_data *data, int chan_type, int uscale)
228 int i;
229 int reg, mask;
230 struct bmi270_scale_item bmi270_scale_item;
232 switch (chan_type) {
233 case IIO_ACCEL:
234 reg = BMI270_ACC_CONF_RANGE_REG;
235 mask = BMI270_ACC_CONF_RANGE_MSK;
236 bmi270_scale_item = bmi270_scale_table[BMI270_ACCEL];
237 break;
238 case IIO_ANGL_VEL:
239 reg = BMI270_GYR_CONF_RANGE_REG;
240 mask = BMI270_GYR_CONF_RANGE_MSK;
241 bmi270_scale_item = bmi270_scale_table[BMI270_GYRO];
242 break;
243 default:
244 return -EINVAL;
247 for (i = 0; i < bmi270_scale_item.num; i++) {
248 if (bmi270_scale_item.tbl[i].uscale != uscale)
249 continue;
251 return regmap_update_bits(data->regmap, reg, mask, i);
254 return -EINVAL;
257 static int bmi270_get_scale(struct bmi270_data *bmi270_device, int chan_type,
258 int *uscale)
260 int ret;
261 unsigned int val;
262 struct bmi270_scale_item bmi270_scale_item;
264 switch (chan_type) {
265 case IIO_ACCEL:
266 ret = regmap_read(bmi270_device->regmap,
267 BMI270_ACC_CONF_RANGE_REG, &val);
268 if (ret)
269 return ret;
271 val = FIELD_GET(BMI270_ACC_CONF_RANGE_MSK, val);
272 bmi270_scale_item = bmi270_scale_table[BMI270_ACCEL];
273 break;
274 case IIO_ANGL_VEL:
275 ret = regmap_read(bmi270_device->regmap,
276 BMI270_GYR_CONF_RANGE_REG, &val);
277 if (ret)
278 return ret;
280 val = FIELD_GET(BMI270_GYR_CONF_RANGE_MSK, val);
281 bmi270_scale_item = bmi270_scale_table[BMI270_GYRO];
282 break;
283 default:
284 return -EINVAL;
287 if (val >= bmi270_scale_item.num)
288 return -EINVAL;
290 *uscale = bmi270_scale_item.tbl[val].uscale;
291 return 0;
294 static int bmi270_set_odr(struct bmi270_data *data, int chan_type, int odr,
295 int uodr)
297 int i;
298 int reg, mask;
299 struct bmi270_odr_item bmi270_odr_item;
301 switch (chan_type) {
302 case IIO_ACCEL:
303 reg = BMI270_ACC_CONF_REG;
304 mask = BMI270_ACC_CONF_ODR_MSK;
305 bmi270_odr_item = bmi270_odr_table[BMI270_ACCEL];
306 break;
307 case IIO_ANGL_VEL:
308 reg = BMI270_GYR_CONF_REG;
309 mask = BMI270_GYR_CONF_ODR_MSK;
310 bmi270_odr_item = bmi270_odr_table[BMI270_GYRO];
311 break;
312 default:
313 return -EINVAL;
316 for (i = 0; i < bmi270_odr_item.num; i++) {
317 if (bmi270_odr_item.tbl[i].odr != odr ||
318 bmi270_odr_item.tbl[i].uodr != uodr)
319 continue;
321 return regmap_update_bits(data->regmap, reg, mask,
322 bmi270_odr_item.vals[i]);
325 return -EINVAL;
328 static int bmi270_get_odr(struct bmi270_data *data, int chan_type, int *odr,
329 int *uodr)
331 int i, val, ret;
332 struct bmi270_odr_item bmi270_odr_item;
334 switch (chan_type) {
335 case IIO_ACCEL:
336 ret = regmap_read(data->regmap, BMI270_ACC_CONF_REG, &val);
337 if (ret)
338 return ret;
340 val = FIELD_GET(BMI270_ACC_CONF_ODR_MSK, val);
341 bmi270_odr_item = bmi270_odr_table[BMI270_ACCEL];
342 break;
343 case IIO_ANGL_VEL:
344 ret = regmap_read(data->regmap, BMI270_GYR_CONF_REG, &val);
345 if (ret)
346 return ret;
348 val = FIELD_GET(BMI270_GYR_CONF_ODR_MSK, val);
349 bmi270_odr_item = bmi270_odr_table[BMI270_GYRO];
350 break;
351 default:
352 return -EINVAL;
355 for (i = 0; i < bmi270_odr_item.num; i++) {
356 if (val != bmi270_odr_item.vals[i])
357 continue;
359 *odr = bmi270_odr_item.tbl[i].odr;
360 *uodr = bmi270_odr_item.tbl[i].uodr;
361 return 0;
364 return -EINVAL;
367 static irqreturn_t bmi270_trigger_handler(int irq, void *p)
369 struct iio_poll_func *pf = p;
370 struct iio_dev *indio_dev = pf->indio_dev;
371 struct bmi270_data *bmi270_device = iio_priv(indio_dev);
372 int ret;
374 ret = regmap_bulk_read(bmi270_device->regmap, BMI270_ACCEL_X_REG,
375 &bmi270_device->data.channels,
376 sizeof(bmi270_device->data.channels));
378 if (ret)
379 goto done;
381 iio_push_to_buffers_with_timestamp(indio_dev, &bmi270_device->data,
382 pf->timestamp);
383 done:
384 iio_trigger_notify_done(indio_dev->trig);
385 return IRQ_HANDLED;
388 static int bmi270_get_data(struct bmi270_data *bmi270_device,
389 int chan_type, int axis, int *val)
391 __le16 sample;
392 int reg;
393 int ret;
395 switch (chan_type) {
396 case IIO_ACCEL:
397 reg = BMI270_ACCEL_X_REG + (axis - IIO_MOD_X) * 2;
398 break;
399 case IIO_ANGL_VEL:
400 reg = BMI270_ANG_VEL_X_REG + (axis - IIO_MOD_X) * 2;
401 break;
402 default:
403 return -EINVAL;
406 ret = regmap_bulk_read(bmi270_device->regmap, reg, &sample, sizeof(sample));
407 if (ret)
408 return ret;
410 *val = sign_extend32(le16_to_cpu(sample), 15);
412 return 0;
415 static int bmi270_read_raw(struct iio_dev *indio_dev,
416 struct iio_chan_spec const *chan,
417 int *val, int *val2, long mask)
419 int ret;
420 struct bmi270_data *bmi270_device = iio_priv(indio_dev);
422 switch (mask) {
423 case IIO_CHAN_INFO_RAW:
424 ret = bmi270_get_data(bmi270_device, chan->type, chan->channel2, val);
425 if (ret)
426 return ret;
428 return IIO_VAL_INT;
429 case IIO_CHAN_INFO_SCALE:
430 *val = 0;
431 ret = bmi270_get_scale(bmi270_device, chan->type, val2);
432 return ret ? ret : IIO_VAL_INT_PLUS_MICRO;
433 case IIO_CHAN_INFO_SAMP_FREQ:
434 ret = bmi270_get_odr(bmi270_device, chan->type, val, val2);
435 return ret ? ret : IIO_VAL_INT_PLUS_MICRO;
436 default:
437 return -EINVAL;
441 static int bmi270_write_raw(struct iio_dev *indio_dev,
442 struct iio_chan_spec const *chan,
443 int val, int val2, long mask)
445 struct bmi270_data *data = iio_priv(indio_dev);
447 switch (mask) {
448 case IIO_CHAN_INFO_SCALE:
449 return bmi270_set_scale(data, chan->type, val2);
450 case IIO_CHAN_INFO_SAMP_FREQ:
451 return bmi270_set_odr(data, chan->type, val, val2);
452 default:
453 return -EINVAL;
457 static int bmi270_read_avail(struct iio_dev *indio_dev,
458 struct iio_chan_spec const *chan,
459 const int **vals, int *type, int *length,
460 long mask)
462 switch (mask) {
463 case IIO_CHAN_INFO_SCALE:
464 *type = IIO_VAL_INT_PLUS_MICRO;
465 switch (chan->type) {
466 case IIO_ANGL_VEL:
467 *vals = (const int *)bmi270_gyro_scale;
468 *length = ARRAY_SIZE(bmi270_gyro_scale) * 2;
469 return IIO_AVAIL_LIST;
470 case IIO_ACCEL:
471 *vals = (const int *)bmi270_accel_scale;
472 *length = ARRAY_SIZE(bmi270_accel_scale) * 2;
473 return IIO_AVAIL_LIST;
474 default:
475 return -EINVAL;
477 case IIO_CHAN_INFO_SAMP_FREQ:
478 *type = IIO_VAL_INT_PLUS_MICRO;
479 switch (chan->type) {
480 case IIO_ANGL_VEL:
481 *vals = (const int *)bmi270_gyro_odr;
482 *length = ARRAY_SIZE(bmi270_gyro_odr) * 2;
483 return IIO_AVAIL_LIST;
484 case IIO_ACCEL:
485 *vals = (const int *)bmi270_accel_odr;
486 *length = ARRAY_SIZE(bmi270_accel_odr) * 2;
487 return IIO_AVAIL_LIST;
488 default:
489 return -EINVAL;
491 default:
492 return -EINVAL;
496 static const struct iio_info bmi270_info = {
497 .read_raw = bmi270_read_raw,
498 .write_raw = bmi270_write_raw,
499 .read_avail = bmi270_read_avail,
502 #define BMI270_ACCEL_CHANNEL(_axis) { \
503 .type = IIO_ACCEL, \
504 .modified = 1, \
505 .channel2 = IIO_MOD_##_axis, \
506 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
507 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
508 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
509 .info_mask_shared_by_type_available = \
510 BIT(IIO_CHAN_INFO_SCALE) | \
511 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
512 .scan_index = BMI270_SCAN_ACCEL_##_axis, \
513 .scan_type = { \
514 .sign = 's', \
515 .realbits = 16, \
516 .storagebits = 16, \
517 .endianness = IIO_LE, \
518 }, \
521 #define BMI270_ANG_VEL_CHANNEL(_axis) { \
522 .type = IIO_ANGL_VEL, \
523 .modified = 1, \
524 .channel2 = IIO_MOD_##_axis, \
525 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
526 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
527 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
528 .info_mask_shared_by_type_available = \
529 BIT(IIO_CHAN_INFO_SCALE) | \
530 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
531 .scan_index = BMI270_SCAN_GYRO_##_axis, \
532 .scan_type = { \
533 .sign = 's', \
534 .realbits = 16, \
535 .storagebits = 16, \
536 .endianness = IIO_LE, \
537 }, \
540 static const struct iio_chan_spec bmi270_channels[] = {
541 BMI270_ACCEL_CHANNEL(X),
542 BMI270_ACCEL_CHANNEL(Y),
543 BMI270_ACCEL_CHANNEL(Z),
544 BMI270_ANG_VEL_CHANNEL(X),
545 BMI270_ANG_VEL_CHANNEL(Y),
546 BMI270_ANG_VEL_CHANNEL(Z),
547 IIO_CHAN_SOFT_TIMESTAMP(BMI270_SCAN_TIMESTAMP),
550 static int bmi270_validate_chip_id(struct bmi270_data *bmi270_device)
552 int chip_id;
553 int ret;
554 struct device *dev = bmi270_device->dev;
555 struct regmap *regmap = bmi270_device->regmap;
557 ret = regmap_read(regmap, BMI270_CHIP_ID_REG, &chip_id);
558 if (ret)
559 return dev_err_probe(dev, ret, "Failed to read chip id");
562 * Some manufacturers use "BMI0160" for both the BMI160 and
563 * BMI260. If the device is actually a BMI160, the bmi160
564 * driver should handle it and this driver should not.
566 if (chip_id == BMI160_CHIP_ID_VAL)
567 return -ENODEV;
569 if (chip_id != bmi270_device->chip_info->chip_id)
570 dev_info(dev, "Unexpected chip id 0x%x", chip_id);
572 if (chip_id == bmi260_chip_info.chip_id)
573 bmi270_device->chip_info = &bmi260_chip_info;
574 else if (chip_id == bmi270_chip_info.chip_id)
575 bmi270_device->chip_info = &bmi270_chip_info;
577 return 0;
580 static int bmi270_write_calibration_data(struct bmi270_data *bmi270_device)
582 int ret;
583 int status = 0;
584 const struct firmware *init_data;
585 struct device *dev = bmi270_device->dev;
586 struct regmap *regmap = bmi270_device->regmap;
588 ret = regmap_clear_bits(regmap, BMI270_PWR_CONF_REG,
589 BMI270_PWR_CONF_ADV_PWR_SAVE_MSK);
590 if (ret)
591 return dev_err_probe(dev, ret,
592 "Failed to write power configuration");
595 * After disabling advanced power save, all registers are accessible
596 * after a 450us delay. This delay is specified in table A of the
597 * datasheet.
599 usleep_range(450, 1000);
601 ret = regmap_clear_bits(regmap, BMI270_INIT_CTRL_REG,
602 BMI270_INIT_CTRL_LOAD_DONE_MSK);
603 if (ret)
604 return dev_err_probe(dev, ret,
605 "Failed to prepare device to load init data");
607 ret = request_firmware(&init_data,
608 bmi270_device->chip_info->fw_name, dev);
609 if (ret)
610 return dev_err_probe(dev, ret, "Failed to load init data file");
612 ret = regmap_bulk_write(regmap, BMI270_INIT_DATA_REG,
613 init_data->data, init_data->size);
614 release_firmware(init_data);
615 if (ret)
616 return dev_err_probe(dev, ret, "Failed to write init data");
618 ret = regmap_set_bits(regmap, BMI270_INIT_CTRL_REG,
619 BMI270_INIT_CTRL_LOAD_DONE_MSK);
620 if (ret)
621 return dev_err_probe(dev, ret,
622 "Failed to stop device initialization");
625 * Wait at least 140ms for the device to complete configuration.
626 * This delay is specified in table C of the datasheet.
628 usleep_range(140000, 160000);
630 ret = regmap_read(regmap, BMI270_INTERNAL_STATUS_REG, &status);
631 if (ret)
632 return dev_err_probe(dev, ret, "Failed to read internal status");
634 if (status != BMI270_INTERNAL_STATUS_MSG_INIT_OK)
635 return dev_err_probe(dev, -ENODEV, "Device failed to initialize");
637 return 0;
640 static int bmi270_configure_imu(struct bmi270_data *bmi270_device)
642 int ret;
643 struct device *dev = bmi270_device->dev;
644 struct regmap *regmap = bmi270_device->regmap;
646 ret = regmap_set_bits(regmap, BMI270_PWR_CTRL_REG,
647 BMI270_PWR_CTRL_AUX_EN_MSK |
648 BMI270_PWR_CTRL_GYR_EN_MSK |
649 BMI270_PWR_CTRL_ACCEL_EN_MSK);
650 if (ret)
651 return dev_err_probe(dev, ret, "Failed to enable accelerometer and gyroscope");
653 ret = regmap_set_bits(regmap, BMI270_ACC_CONF_REG,
654 FIELD_PREP(BMI270_ACC_CONF_ODR_MSK,
655 BMI270_ACC_CONF_ODR_100HZ) |
656 FIELD_PREP(BMI270_ACC_CONF_BWP_MSK,
657 BMI270_ACC_CONF_BWP_NORMAL_MODE) |
658 BMI270_PWR_CONF_ADV_PWR_SAVE_MSK);
659 if (ret)
660 return dev_err_probe(dev, ret, "Failed to configure accelerometer");
662 ret = regmap_set_bits(regmap, BMI270_GYR_CONF_REG,
663 FIELD_PREP(BMI270_GYR_CONF_ODR_MSK,
664 BMI270_GYR_CONF_ODR_200HZ) |
665 FIELD_PREP(BMI270_GYR_CONF_BWP_MSK,
666 BMI270_GYR_CONF_BWP_NORMAL_MODE) |
667 BMI270_PWR_CONF_ADV_PWR_SAVE_MSK);
668 if (ret)
669 return dev_err_probe(dev, ret, "Failed to configure gyroscope");
671 /* Enable FIFO_WKUP, Disable ADV_PWR_SAVE and FUP_EN */
672 ret = regmap_write(regmap, BMI270_PWR_CONF_REG,
673 BMI270_PWR_CONF_FIFO_WKUP_MSK);
674 if (ret)
675 return dev_err_probe(dev, ret, "Failed to set power configuration");
677 return 0;
680 static int bmi270_chip_init(struct bmi270_data *bmi270_device)
682 int ret;
684 ret = bmi270_validate_chip_id(bmi270_device);
685 if (ret)
686 return ret;
688 ret = bmi270_write_calibration_data(bmi270_device);
689 if (ret)
690 return ret;
692 return bmi270_configure_imu(bmi270_device);
695 int bmi270_core_probe(struct device *dev, struct regmap *regmap,
696 const struct bmi270_chip_info *chip_info)
698 int ret;
699 struct bmi270_data *bmi270_device;
700 struct iio_dev *indio_dev;
702 indio_dev = devm_iio_device_alloc(dev, sizeof(*bmi270_device));
703 if (!indio_dev)
704 return -ENOMEM;
706 bmi270_device = iio_priv(indio_dev);
707 bmi270_device->dev = dev;
708 bmi270_device->regmap = regmap;
709 bmi270_device->chip_info = chip_info;
711 ret = bmi270_chip_init(bmi270_device);
712 if (ret)
713 return ret;
715 indio_dev->channels = bmi270_channels;
716 indio_dev->num_channels = ARRAY_SIZE(bmi270_channels);
717 indio_dev->name = chip_info->name;
718 indio_dev->available_scan_masks = bmi270_avail_scan_masks;
719 indio_dev->modes = INDIO_DIRECT_MODE;
720 indio_dev->info = &bmi270_info;
722 ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
723 iio_pollfunc_store_time,
724 bmi270_trigger_handler, NULL);
725 if (ret)
726 return ret;
728 return devm_iio_device_register(dev, indio_dev);
730 EXPORT_SYMBOL_NS_GPL(bmi270_core_probe, "IIO_BMI270");
732 MODULE_AUTHOR("Alex Lanzano");
733 MODULE_DESCRIPTION("BMI270 driver");
734 MODULE_LICENSE("GPL");