xref: /linux/drivers/iio/imu/bmi270/bmi270_core.c (revision cdd30ebb1b9f36159d66f088b61aee264e649d7a)
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 
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>
8 
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>
13 
14 #include "bmi270.h"
15 
16 #define BMI270_CHIP_ID_REG				0x00
17 
18 /* Checked to prevent sending incompatible firmware to BMI160 devices */
19 #define BMI160_CHIP_ID_VAL				0xD1
20 
21 #define BMI260_CHIP_ID_VAL				0x27
22 #define BMI270_CHIP_ID_VAL				0x24
23 #define BMI270_CHIP_ID_MSK				GENMASK(7, 0)
24 
25 #define BMI270_ACCEL_X_REG				0x0c
26 #define BMI270_ANG_VEL_X_REG				0x12
27 
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
31 
32 #define BMI270_INTERNAL_STATUS_AXES_REMAP_ERR_MSK	BIT(5)
33 #define BMI270_INTERNAL_STATUS_ODR_50HZ_ERR_MSK		BIT(6)
34 
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)
41 
42 #define BMI270_ACC_CONF_RANGE_REG			0x41
43 #define BMI270_ACC_CONF_RANGE_MSK			GENMASK(1, 0)
44 
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)
52 
53 #define BMI270_GYR_CONF_RANGE_REG			0x43
54 #define BMI270_GYR_CONF_RANGE_MSK			GENMASK(2, 0)
55 
56 #define BMI270_INIT_CTRL_REG				0x59
57 #define BMI270_INIT_CTRL_LOAD_DONE_MSK			BIT(0)
58 
59 #define BMI270_INIT_DATA_REG				0x5e
60 
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)
65 
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)
71 
72 #define BMI260_INIT_DATA_FILE "bmi260-init-data.fw"
73 #define BMI270_INIT_DATA_FILE "bmi270-init-data.fw"
74 
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,
83 };
84 
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)),
92 	0
93 };
94 
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,
99 };
100 EXPORT_SYMBOL_NS_GPL(bmi260_chip_info, "IIO_BMI270");
101 
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,
106 };
107 EXPORT_SYMBOL_NS_GPL(bmi270_chip_info, "IIO_BMI270");
108 
109 enum bmi270_sensor_type {
110 	BMI270_ACCEL	= 0,
111 	BMI270_GYRO,
112 };
113 
114 struct bmi270_scale {
115 	int scale;
116 	int uscale;
117 };
118 
119 struct bmi270_odr {
120 	int odr;
121 	int uodr;
122 };
123 
124 static const struct bmi270_scale bmi270_accel_scale[] = {
125 	{ 0, 598 },
126 	{ 0, 1197 },
127 	{ 0, 2394 },
128 	{ 0, 4788 },
129 };
130 
131 static const struct bmi270_scale bmi270_gyro_scale[] = {
132 	{ 0, 1065 },
133 	{ 0, 532 },
134 	{ 0, 266 },
135 	{ 0, 133 },
136 	{ 0, 66 },
137 };
138 
139 struct bmi270_scale_item {
140 	const struct bmi270_scale *tbl;
141 	int num;
142 };
143 
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),
148 	},
149 	[BMI270_GYRO] = {
150 		.tbl	= bmi270_gyro_scale,
151 		.num	= ARRAY_SIZE(bmi270_gyro_scale),
152 	},
153 };
154 
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 },
168 };
169 
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,
183 };
184 
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 },
194 };
195 
196 static const u8 bmi270_gyro_odr_vals[] = {
197 	0x06,
198 	0x07,
199 	0x08,
200 	0x09,
201 	0x0A,
202 	0x0B,
203 	0x0C,
204 	0x0D,
205 };
206 
207 struct bmi270_odr_item {
208 	const struct bmi270_odr *tbl;
209 	const u8 *vals;
210 	int num;
211 };
212 
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),
218 	},
219 	[BMI270_GYRO] = {
220 		.tbl	= bmi270_gyro_odr,
221 		.vals   = bmi270_gyro_odr_vals,
222 		.num	= ARRAY_SIZE(bmi270_gyro_odr),
223 	},
224 };
225 
bmi270_set_scale(struct bmi270_data * data,int chan_type,int uscale)226 static int bmi270_set_scale(struct bmi270_data *data, int chan_type, int uscale)
227 {
228 	int i;
229 	int reg, mask;
230 	struct bmi270_scale_item bmi270_scale_item;
231 
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;
245 	}
246 
247 	for (i = 0; i < bmi270_scale_item.num; i++) {
248 		if (bmi270_scale_item.tbl[i].uscale != uscale)
249 			continue;
250 
251 		return regmap_update_bits(data->regmap, reg, mask, i);
252 	}
253 
254 	return -EINVAL;
255 }
256 
bmi270_get_scale(struct bmi270_data * bmi270_device,int chan_type,int * uscale)257 static int bmi270_get_scale(struct bmi270_data *bmi270_device, int chan_type,
258 			    int *uscale)
259 {
260 	int ret;
261 	unsigned int val;
262 	struct bmi270_scale_item bmi270_scale_item;
263 
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;
270 
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;
279 
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;
285 	}
286 
287 	if (val >= bmi270_scale_item.num)
288 		return -EINVAL;
289 
290 	*uscale = bmi270_scale_item.tbl[val].uscale;
291 	return 0;
292 }
293 
bmi270_set_odr(struct bmi270_data * data,int chan_type,int odr,int uodr)294 static int bmi270_set_odr(struct bmi270_data *data, int chan_type, int odr,
295 			  int uodr)
296 {
297 	int i;
298 	int reg, mask;
299 	struct bmi270_odr_item bmi270_odr_item;
300 
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;
314 	}
315 
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;
320 
321 		return regmap_update_bits(data->regmap, reg, mask,
322 					  bmi270_odr_item.vals[i]);
323 	}
324 
325 	return -EINVAL;
326 }
327 
bmi270_get_odr(struct bmi270_data * data,int chan_type,int * odr,int * uodr)328 static int bmi270_get_odr(struct bmi270_data *data, int chan_type, int *odr,
329 			  int *uodr)
330 {
331 	int i, val, ret;
332 	struct bmi270_odr_item bmi270_odr_item;
333 
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;
339 
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;
347 
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;
353 	}
354 
355 	for (i = 0; i < bmi270_odr_item.num; i++) {
356 		if (val != bmi270_odr_item.vals[i])
357 			continue;
358 
359 		*odr = bmi270_odr_item.tbl[i].odr;
360 		*uodr = bmi270_odr_item.tbl[i].uodr;
361 		return 0;
362 	}
363 
364 	return -EINVAL;
365 }
366 
bmi270_trigger_handler(int irq,void * p)367 static irqreturn_t bmi270_trigger_handler(int irq, void *p)
368 {
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;
373 
374 	ret = regmap_bulk_read(bmi270_device->regmap, BMI270_ACCEL_X_REG,
375 			       &bmi270_device->data.channels,
376 			       sizeof(bmi270_device->data.channels));
377 
378 	if (ret)
379 		goto done;
380 
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;
386 }
387 
bmi270_get_data(struct bmi270_data * bmi270_device,int chan_type,int axis,int * val)388 static int bmi270_get_data(struct bmi270_data *bmi270_device,
389 			   int chan_type, int axis, int *val)
390 {
391 	__le16 sample;
392 	int reg;
393 	int ret;
394 
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;
404 	}
405 
406 	ret = regmap_bulk_read(bmi270_device->regmap, reg, &sample, sizeof(sample));
407 	if (ret)
408 		return ret;
409 
410 	*val = sign_extend32(le16_to_cpu(sample), 15);
411 
412 	return 0;
413 }
414 
bmi270_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)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)
418 {
419 	int ret;
420 	struct bmi270_data *bmi270_device = iio_priv(indio_dev);
421 
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;
427 
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;
438 	}
439 }
440 
bmi270_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)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)
444 {
445 	struct bmi270_data *data = iio_priv(indio_dev);
446 
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;
454 	}
455 }
456 
bmi270_read_avail(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,const int ** vals,int * type,int * length,long mask)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)
461 {
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;
476 		}
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;
490 		}
491 	default:
492 		return -EINVAL;
493 	}
494 }
495 
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,
500 };
501 
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 	},	                                                \
519 }
520 
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 	},	                                                \
538 }
539 
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),
548 };
549 
bmi270_validate_chip_id(struct bmi270_data * bmi270_device)550 static int bmi270_validate_chip_id(struct bmi270_data *bmi270_device)
551 {
552 	int chip_id;
553 	int ret;
554 	struct device *dev = bmi270_device->dev;
555 	struct regmap *regmap = bmi270_device->regmap;
556 
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");
560 
561 	/*
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.
565 	 */
566 	if (chip_id == BMI160_CHIP_ID_VAL)
567 		return -ENODEV;
568 
569 	if (chip_id != bmi270_device->chip_info->chip_id)
570 		dev_info(dev, "Unexpected chip id 0x%x", chip_id);
571 
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;
576 
577 	return 0;
578 }
579 
bmi270_write_calibration_data(struct bmi270_data * bmi270_device)580 static int bmi270_write_calibration_data(struct bmi270_data *bmi270_device)
581 {
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;
587 
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");
593 
594 	/*
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.
598 	 */
599 	usleep_range(450, 1000);
600 
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");
606 
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");
611 
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");
617 
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");
623 
624 	/*
625 	 * Wait at least 140ms for the device to complete configuration.
626 	 * This delay is specified in table C of the datasheet.
627 	 */
628 	usleep_range(140000, 160000);
629 
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");
633 
634 	if (status != BMI270_INTERNAL_STATUS_MSG_INIT_OK)
635 		return dev_err_probe(dev, -ENODEV, "Device failed to initialize");
636 
637 	return 0;
638 }
639 
bmi270_configure_imu(struct bmi270_data * bmi270_device)640 static int bmi270_configure_imu(struct bmi270_data *bmi270_device)
641 {
642 	int ret;
643 	struct device *dev = bmi270_device->dev;
644 	struct regmap *regmap = bmi270_device->regmap;
645 
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");
652 
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");
661 
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");
670 
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");
676 
677 	return 0;
678 }
679 
bmi270_chip_init(struct bmi270_data * bmi270_device)680 static int bmi270_chip_init(struct bmi270_data *bmi270_device)
681 {
682 	int ret;
683 
684 	ret = bmi270_validate_chip_id(bmi270_device);
685 	if (ret)
686 		return ret;
687 
688 	ret = bmi270_write_calibration_data(bmi270_device);
689 	if (ret)
690 		return ret;
691 
692 	return bmi270_configure_imu(bmi270_device);
693 }
694 
bmi270_core_probe(struct device * dev,struct regmap * regmap,const struct bmi270_chip_info * chip_info)695 int bmi270_core_probe(struct device *dev, struct regmap *regmap,
696 		      const struct bmi270_chip_info *chip_info)
697 {
698 	int ret;
699 	struct bmi270_data *bmi270_device;
700 	struct iio_dev *indio_dev;
701 
702 	indio_dev = devm_iio_device_alloc(dev, sizeof(*bmi270_device));
703 	if (!indio_dev)
704 		return -ENOMEM;
705 
706 	bmi270_device = iio_priv(indio_dev);
707 	bmi270_device->dev = dev;
708 	bmi270_device->regmap = regmap;
709 	bmi270_device->chip_info = chip_info;
710 
711 	ret = bmi270_chip_init(bmi270_device);
712 	if (ret)
713 		return ret;
714 
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;
721 
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;
727 
728 	return devm_iio_device_register(dev, indio_dev);
729 }
730 EXPORT_SYMBOL_NS_GPL(bmi270_core_probe, "IIO_BMI270");
731 
732 MODULE_AUTHOR("Alex Lanzano");
733 MODULE_DESCRIPTION("BMI270 driver");
734 MODULE_LICENSE("GPL");
735