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/mutex.h>
8 #include <linux/regmap.h>
9 #include <linux/units.h>
10
11 #include <linux/iio/events.h>
12 #include <linux/iio/iio.h>
13 #include <linux/iio/sysfs.h>
14 #include <linux/iio/trigger.h>
15 #include <linux/iio/triggered_buffer.h>
16 #include <linux/iio/trigger_consumer.h>
17
18 #include "bmi270.h"
19
20 #define BMI270_CHIP_ID_REG 0x00
21
22 /* Checked to prevent sending incompatible firmware to BMI160 devices */
23 #define BMI160_CHIP_ID_VAL 0xD1
24
25 #define BMI260_CHIP_ID_VAL 0x27
26 #define BMI270_CHIP_ID_VAL 0x24
27 #define BMI270_CHIP_ID_MSK GENMASK(7, 0)
28
29 #define BMI270_ACCEL_X_REG 0x0c
30 #define BMI270_ANG_VEL_X_REG 0x12
31
32 #define BMI270_INT_STATUS_0_REG 0x1c
33 #define BMI270_INT_STATUS_0_STEP_CNT_MSK BIT(1)
34
35 #define BMI270_INT_STATUS_1_REG 0x1d
36 #define BMI270_INT_STATUS_1_ACC_GYR_DRDY_MSK GENMASK(7, 6)
37
38 #define BMI270_SC_OUT_0_REG 0x1e
39
40 #define BMI270_INTERNAL_STATUS_REG 0x21
41 #define BMI270_INTERNAL_STATUS_MSG_MSK GENMASK(3, 0)
42 #define BMI270_INTERNAL_STATUS_MSG_INIT_OK 0x01
43 #define BMI270_INTERNAL_STATUS_AXES_REMAP_ERR_MSK BIT(5)
44 #define BMI270_INTERNAL_STATUS_ODR_50HZ_ERR_MSK BIT(6)
45
46 #define BMI270_TEMPERATURE_0_REG 0x22
47
48 #define BMI270_FEAT_PAGE_REG 0x2f
49
50 #define BMI270_ACC_CONF_REG 0x40
51 #define BMI270_ACC_CONF_ODR_MSK GENMASK(3, 0)
52 #define BMI270_ACC_CONF_ODR_100HZ 0x08
53 #define BMI270_ACC_CONF_BWP_MSK GENMASK(6, 4)
54 #define BMI270_ACC_CONF_BWP_NORMAL_MODE 0x02
55 #define BMI270_ACC_CONF_FILTER_PERF_MSK BIT(7)
56
57 #define BMI270_ACC_CONF_RANGE_REG 0x41
58 #define BMI270_ACC_CONF_RANGE_MSK GENMASK(1, 0)
59
60 #define BMI270_GYR_CONF_REG 0x42
61 #define BMI270_GYR_CONF_ODR_MSK GENMASK(3, 0)
62 #define BMI270_GYR_CONF_ODR_200HZ 0x09
63 #define BMI270_GYR_CONF_BWP_MSK GENMASK(5, 4)
64 #define BMI270_GYR_CONF_BWP_NORMAL_MODE 0x02
65 #define BMI270_GYR_CONF_NOISE_PERF_MSK BIT(6)
66 #define BMI270_GYR_CONF_FILTER_PERF_MSK BIT(7)
67
68 #define BMI270_GYR_CONF_RANGE_REG 0x43
69 #define BMI270_GYR_CONF_RANGE_MSK GENMASK(2, 0)
70
71 #define BMI270_INT1_IO_CTRL_REG 0x53
72 #define BMI270_INT2_IO_CTRL_REG 0x54
73 #define BMI270_INT_IO_CTRL_LVL_MSK BIT(1)
74 #define BMI270_INT_IO_CTRL_OD_MSK BIT(2)
75 #define BMI270_INT_IO_CTRL_OP_MSK BIT(3)
76 #define BMI270_INT_IO_LVL_OD_OP_MSK GENMASK(3, 1)
77
78 #define BMI270_INT_LATCH_REG 0x55
79 #define BMI270_INT_LATCH_REG_MSK BIT(0)
80
81 #define BMI270_INT1_MAP_FEAT_REG 0x56
82 #define BMI270_INT2_MAP_FEAT_REG 0x57
83 #define BMI270_INT_MAP_FEAT_STEP_CNT_WTRMRK_MSK BIT(1)
84
85 #define BMI270_INT_MAP_DATA_REG 0x58
86 #define BMI270_INT_MAP_DATA_DRDY_INT1_MSK BIT(2)
87 #define BMI270_INT_MAP_DATA_DRDY_INT2_MSK BIT(6)
88
89 #define BMI270_INIT_CTRL_REG 0x59
90 #define BMI270_INIT_CTRL_LOAD_DONE_MSK BIT(0)
91
92 #define BMI270_INIT_DATA_REG 0x5e
93
94 #define BMI270_PWR_CONF_REG 0x7c
95 #define BMI270_PWR_CONF_ADV_PWR_SAVE_MSK BIT(0)
96 #define BMI270_PWR_CONF_FIFO_WKUP_MSK BIT(1)
97 #define BMI270_PWR_CONF_FUP_EN_MSK BIT(2)
98
99 #define BMI270_PWR_CTRL_REG 0x7d
100 #define BMI270_PWR_CTRL_AUX_EN_MSK BIT(0)
101 #define BMI270_PWR_CTRL_GYR_EN_MSK BIT(1)
102 #define BMI270_PWR_CTRL_ACCEL_EN_MSK BIT(2)
103 #define BMI270_PWR_CTRL_TEMP_EN_MSK BIT(3)
104
105 #define BMI270_STEP_SC26_WTRMRK_MSK GENMASK(9, 0)
106 #define BMI270_STEP_SC26_RST_CNT_MSK BIT(10)
107 #define BMI270_STEP_SC26_EN_CNT_MSK BIT(12)
108
109 /* See datasheet section 4.6.14, Temperature Sensor */
110 #define BMI270_TEMP_OFFSET 11776
111 #define BMI270_TEMP_SCALE 1953125
112
113 /* See page 90 of datasheet. The step counter "holds implicitly a 20x factor" */
114 #define BMI270_STEP_COUNTER_FACTOR 20
115 #define BMI270_STEP_COUNTER_MAX 20460
116
117 #define BMI260_INIT_DATA_FILE "bmi260-init-data.fw"
118 #define BMI270_INIT_DATA_FILE "bmi270-init-data.fw"
119
120 enum bmi270_irq_pin {
121 BMI270_IRQ_DISABLED,
122 BMI270_IRQ_INT1,
123 BMI270_IRQ_INT2,
124 };
125
126 struct bmi270_data {
127 struct device *dev;
128 struct regmap *regmap;
129 const struct bmi270_chip_info *chip_info;
130 enum bmi270_irq_pin irq_pin;
131 struct iio_trigger *trig;
132 /* Protect device's private data from concurrent access */
133 struct mutex mutex;
134 bool steps_enabled;
135
136 /*
137 * Where IIO_DMA_MINALIGN may be larger than 8 bytes, align to
138 * that to ensure a DMA safe buffer.
139 */
140 struct {
141 __le16 channels[6];
142 aligned_s64 timestamp;
143 } buffer __aligned(IIO_DMA_MINALIGN);
144 /*
145 * Variable to access feature registers. It can be accessed concurrently
146 * with the 'buffer' variable
147 */
148 __le16 regval __aligned(IIO_DMA_MINALIGN);
149 };
150
151 enum bmi270_scan {
152 BMI270_SCAN_ACCEL_X,
153 BMI270_SCAN_ACCEL_Y,
154 BMI270_SCAN_ACCEL_Z,
155 BMI270_SCAN_GYRO_X,
156 BMI270_SCAN_GYRO_Y,
157 BMI270_SCAN_GYRO_Z,
158 BMI270_SCAN_TIMESTAMP,
159 };
160
161 static const unsigned long bmi270_avail_scan_masks[] = {
162 (BIT(BMI270_SCAN_ACCEL_X) |
163 BIT(BMI270_SCAN_ACCEL_Y) |
164 BIT(BMI270_SCAN_ACCEL_Z) |
165 BIT(BMI270_SCAN_GYRO_X) |
166 BIT(BMI270_SCAN_GYRO_Y) |
167 BIT(BMI270_SCAN_GYRO_Z)),
168 0
169 };
170
171 const struct bmi270_chip_info bmi260_chip_info = {
172 .name = "bmi260",
173 .chip_id = BMI260_CHIP_ID_VAL,
174 .fw_name = BMI260_INIT_DATA_FILE,
175 };
176 EXPORT_SYMBOL_NS_GPL(bmi260_chip_info, "IIO_BMI270");
177
178 const struct bmi270_chip_info bmi270_chip_info = {
179 .name = "bmi270",
180 .chip_id = BMI270_CHIP_ID_VAL,
181 .fw_name = BMI270_INIT_DATA_FILE,
182 };
183 EXPORT_SYMBOL_NS_GPL(bmi270_chip_info, "IIO_BMI270");
184
185 enum bmi270_sensor_type {
186 BMI270_ACCEL = 0,
187 BMI270_GYRO,
188 BMI270_TEMP,
189 };
190
191 struct bmi270_scale {
192 int scale;
193 int uscale;
194 };
195
196 struct bmi270_odr {
197 int odr;
198 int uodr;
199 };
200
201 static const struct bmi270_scale bmi270_accel_scale[] = {
202 { 0, 598 },
203 { 0, 1197 },
204 { 0, 2394 },
205 { 0, 4788 },
206 };
207
208 static const struct bmi270_scale bmi270_gyro_scale[] = {
209 { 0, 1065 },
210 { 0, 532 },
211 { 0, 266 },
212 { 0, 133 },
213 { 0, 66 },
214 };
215
216 static const struct bmi270_scale bmi270_temp_scale[] = {
217 { BMI270_TEMP_SCALE / MICRO, BMI270_TEMP_SCALE % MICRO },
218 };
219
220 struct bmi270_scale_item {
221 const struct bmi270_scale *tbl;
222 int num;
223 };
224
225 static const struct bmi270_scale_item bmi270_scale_table[] = {
226 [BMI270_ACCEL] = {
227 .tbl = bmi270_accel_scale,
228 .num = ARRAY_SIZE(bmi270_accel_scale),
229 },
230 [BMI270_GYRO] = {
231 .tbl = bmi270_gyro_scale,
232 .num = ARRAY_SIZE(bmi270_gyro_scale),
233 },
234 [BMI270_TEMP] = {
235 .tbl = bmi270_temp_scale,
236 .num = ARRAY_SIZE(bmi270_temp_scale),
237 },
238 };
239
240 static const struct bmi270_odr bmi270_accel_odr[] = {
241 { 0, 781250 },
242 { 1, 562500 },
243 { 3, 125000 },
244 { 6, 250000 },
245 { 12, 500000 },
246 { 25, 0 },
247 { 50, 0 },
248 { 100, 0 },
249 { 200, 0 },
250 { 400, 0 },
251 { 800, 0 },
252 { 1600, 0 },
253 };
254
255 static const u8 bmi270_accel_odr_vals[] = {
256 0x01,
257 0x02,
258 0x03,
259 0x04,
260 0x05,
261 0x06,
262 0x07,
263 0x08,
264 0x09,
265 0x0A,
266 0x0B,
267 0x0C,
268 };
269
270 static const struct bmi270_odr bmi270_gyro_odr[] = {
271 { 25, 0 },
272 { 50, 0 },
273 { 100, 0 },
274 { 200, 0 },
275 { 400, 0 },
276 { 800, 0 },
277 { 1600, 0 },
278 { 3200, 0 },
279 };
280
281 static const u8 bmi270_gyro_odr_vals[] = {
282 0x06,
283 0x07,
284 0x08,
285 0x09,
286 0x0A,
287 0x0B,
288 0x0C,
289 0x0D,
290 };
291
292 struct bmi270_odr_item {
293 const struct bmi270_odr *tbl;
294 const u8 *vals;
295 int num;
296 };
297
298 static const struct bmi270_odr_item bmi270_odr_table[] = {
299 [BMI270_ACCEL] = {
300 .tbl = bmi270_accel_odr,
301 .vals = bmi270_accel_odr_vals,
302 .num = ARRAY_SIZE(bmi270_accel_odr),
303 },
304 [BMI270_GYRO] = {
305 .tbl = bmi270_gyro_odr,
306 .vals = bmi270_gyro_odr_vals,
307 .num = ARRAY_SIZE(bmi270_gyro_odr),
308 },
309 };
310
311 enum bmi270_feature_reg_id {
312 BMI270_SC_26_REG,
313 };
314
315 struct bmi270_feature_reg {
316 u8 page;
317 u8 addr;
318 };
319
320 static const struct bmi270_feature_reg bmi270_feature_regs[] = {
321 [BMI270_SC_26_REG] = {
322 .page = 6,
323 .addr = 0x32,
324 },
325 };
326
bmi270_write_feature_reg(struct bmi270_data * data,enum bmi270_feature_reg_id id,u16 val)327 static int bmi270_write_feature_reg(struct bmi270_data *data,
328 enum bmi270_feature_reg_id id,
329 u16 val)
330 {
331 const struct bmi270_feature_reg *reg = &bmi270_feature_regs[id];
332 int ret;
333
334 ret = regmap_write(data->regmap, BMI270_FEAT_PAGE_REG, reg->page);
335 if (ret)
336 return ret;
337
338 data->regval = cpu_to_le16(val);
339 return regmap_bulk_write(data->regmap, reg->addr, &data->regval,
340 sizeof(data->regval));
341 }
342
bmi270_read_feature_reg(struct bmi270_data * data,enum bmi270_feature_reg_id id,u16 * val)343 static int bmi270_read_feature_reg(struct bmi270_data *data,
344 enum bmi270_feature_reg_id id,
345 u16 *val)
346 {
347 const struct bmi270_feature_reg *reg = &bmi270_feature_regs[id];
348 int ret;
349
350 ret = regmap_write(data->regmap, BMI270_FEAT_PAGE_REG, reg->page);
351 if (ret)
352 return ret;
353
354 ret = regmap_bulk_read(data->regmap, reg->addr, &data->regval,
355 sizeof(data->regval));
356 if (ret)
357 return ret;
358
359 *val = le16_to_cpu(data->regval);
360 return 0;
361 }
362
bmi270_update_feature_reg(struct bmi270_data * data,enum bmi270_feature_reg_id id,u16 mask,u16 val)363 static int bmi270_update_feature_reg(struct bmi270_data *data,
364 enum bmi270_feature_reg_id id,
365 u16 mask, u16 val)
366 {
367 u16 regval;
368 int ret;
369
370 ret = bmi270_read_feature_reg(data, id, ®val);
371 if (ret)
372 return ret;
373
374 regval = (regval & ~mask) | (val & mask);
375
376 return bmi270_write_feature_reg(data, id, regval);
377 }
378
bmi270_enable_steps(struct bmi270_data * data,int val)379 static int bmi270_enable_steps(struct bmi270_data *data, int val)
380 {
381 int ret;
382
383 guard(mutex)(&data->mutex);
384 if (data->steps_enabled)
385 return 0;
386
387 ret = bmi270_update_feature_reg(data, BMI270_SC_26_REG,
388 BMI270_STEP_SC26_EN_CNT_MSK,
389 FIELD_PREP(BMI270_STEP_SC26_EN_CNT_MSK,
390 val ? 1 : 0));
391 if (ret)
392 return ret;
393
394 data->steps_enabled = true;
395 return 0;
396 }
397
bmi270_read_steps(struct bmi270_data * data,int * val)398 static int bmi270_read_steps(struct bmi270_data *data, int *val)
399 {
400 __le16 steps_count;
401 int ret;
402
403 ret = regmap_bulk_read(data->regmap, BMI270_SC_OUT_0_REG, &steps_count,
404 sizeof(steps_count));
405 if (ret)
406 return ret;
407
408 *val = sign_extend32(le16_to_cpu(steps_count), 15);
409 return IIO_VAL_INT;
410 }
411
bmi270_int_map_reg(enum bmi270_irq_pin pin)412 static int bmi270_int_map_reg(enum bmi270_irq_pin pin)
413 {
414 switch (pin) {
415 case BMI270_IRQ_INT1:
416 return BMI270_INT1_MAP_FEAT_REG;
417 case BMI270_IRQ_INT2:
418 return BMI270_INT2_MAP_FEAT_REG;
419 default:
420 return -EINVAL;
421 }
422 }
423
bmi270_step_wtrmrk_en(struct bmi270_data * data,bool state)424 static int bmi270_step_wtrmrk_en(struct bmi270_data *data, bool state)
425 {
426 int reg;
427
428 guard(mutex)(&data->mutex);
429 if (!data->steps_enabled)
430 return -EINVAL;
431
432 reg = bmi270_int_map_reg(data->irq_pin);
433 if (reg < 0)
434 return reg;
435
436 return regmap_update_bits(data->regmap, reg,
437 BMI270_INT_MAP_FEAT_STEP_CNT_WTRMRK_MSK,
438 FIELD_PREP(BMI270_INT_MAP_FEAT_STEP_CNT_WTRMRK_MSK,
439 state));
440 }
441
bmi270_set_scale(struct bmi270_data * data,int chan_type,int uscale)442 static int bmi270_set_scale(struct bmi270_data *data, int chan_type, int uscale)
443 {
444 int i;
445 int reg, mask;
446 struct bmi270_scale_item bmi270_scale_item;
447
448 switch (chan_type) {
449 case IIO_ACCEL:
450 reg = BMI270_ACC_CONF_RANGE_REG;
451 mask = BMI270_ACC_CONF_RANGE_MSK;
452 bmi270_scale_item = bmi270_scale_table[BMI270_ACCEL];
453 break;
454 case IIO_ANGL_VEL:
455 reg = BMI270_GYR_CONF_RANGE_REG;
456 mask = BMI270_GYR_CONF_RANGE_MSK;
457 bmi270_scale_item = bmi270_scale_table[BMI270_GYRO];
458 break;
459 default:
460 return -EINVAL;
461 }
462
463 guard(mutex)(&data->mutex);
464
465 for (i = 0; i < bmi270_scale_item.num; i++) {
466 if (bmi270_scale_item.tbl[i].uscale != uscale)
467 continue;
468
469 return regmap_update_bits(data->regmap, reg, mask, i);
470 }
471
472 return -EINVAL;
473 }
474
bmi270_get_scale(struct bmi270_data * data,int chan_type,int * scale,int * uscale)475 static int bmi270_get_scale(struct bmi270_data *data, int chan_type, int *scale,
476 int *uscale)
477 {
478 int ret;
479 unsigned int val;
480 struct bmi270_scale_item bmi270_scale_item;
481
482 guard(mutex)(&data->mutex);
483
484 switch (chan_type) {
485 case IIO_ACCEL:
486 ret = regmap_read(data->regmap, BMI270_ACC_CONF_RANGE_REG, &val);
487 if (ret)
488 return ret;
489
490 val = FIELD_GET(BMI270_ACC_CONF_RANGE_MSK, val);
491 bmi270_scale_item = bmi270_scale_table[BMI270_ACCEL];
492 break;
493 case IIO_ANGL_VEL:
494 ret = regmap_read(data->regmap, BMI270_GYR_CONF_RANGE_REG, &val);
495 if (ret)
496 return ret;
497
498 val = FIELD_GET(BMI270_GYR_CONF_RANGE_MSK, val);
499 bmi270_scale_item = bmi270_scale_table[BMI270_GYRO];
500 break;
501 case IIO_TEMP:
502 val = 0;
503 bmi270_scale_item = bmi270_scale_table[BMI270_TEMP];
504 break;
505 default:
506 return -EINVAL;
507 }
508
509 if (val >= bmi270_scale_item.num)
510 return -EINVAL;
511
512 *scale = bmi270_scale_item.tbl[val].scale;
513 *uscale = bmi270_scale_item.tbl[val].uscale;
514 return 0;
515 }
516
bmi270_set_odr(struct bmi270_data * data,int chan_type,int odr,int uodr)517 static int bmi270_set_odr(struct bmi270_data *data, int chan_type, int odr,
518 int uodr)
519 {
520 int i;
521 int reg, mask;
522 struct bmi270_odr_item bmi270_odr_item;
523
524 switch (chan_type) {
525 case IIO_ACCEL:
526 reg = BMI270_ACC_CONF_REG;
527 mask = BMI270_ACC_CONF_ODR_MSK;
528 bmi270_odr_item = bmi270_odr_table[BMI270_ACCEL];
529 break;
530 case IIO_ANGL_VEL:
531 reg = BMI270_GYR_CONF_REG;
532 mask = BMI270_GYR_CONF_ODR_MSK;
533 bmi270_odr_item = bmi270_odr_table[BMI270_GYRO];
534 break;
535 default:
536 return -EINVAL;
537 }
538
539 guard(mutex)(&data->mutex);
540
541 for (i = 0; i < bmi270_odr_item.num; i++) {
542 if (bmi270_odr_item.tbl[i].odr != odr ||
543 bmi270_odr_item.tbl[i].uodr != uodr)
544 continue;
545
546 return regmap_update_bits(data->regmap, reg, mask,
547 bmi270_odr_item.vals[i]);
548 }
549
550 return -EINVAL;
551 }
552
bmi270_get_odr(struct bmi270_data * data,int chan_type,int * odr,int * uodr)553 static int bmi270_get_odr(struct bmi270_data *data, int chan_type, int *odr,
554 int *uodr)
555 {
556 int i, val, ret;
557 struct bmi270_odr_item bmi270_odr_item;
558
559 guard(mutex)(&data->mutex);
560
561 switch (chan_type) {
562 case IIO_ACCEL:
563 ret = regmap_read(data->regmap, BMI270_ACC_CONF_REG, &val);
564 if (ret)
565 return ret;
566
567 val = FIELD_GET(BMI270_ACC_CONF_ODR_MSK, val);
568 bmi270_odr_item = bmi270_odr_table[BMI270_ACCEL];
569 break;
570 case IIO_ANGL_VEL:
571 ret = regmap_read(data->regmap, BMI270_GYR_CONF_REG, &val);
572 if (ret)
573 return ret;
574
575 val = FIELD_GET(BMI270_GYR_CONF_ODR_MSK, val);
576 bmi270_odr_item = bmi270_odr_table[BMI270_GYRO];
577 break;
578 default:
579 return -EINVAL;
580 }
581
582 for (i = 0; i < bmi270_odr_item.num; i++) {
583 if (val != bmi270_odr_item.vals[i])
584 continue;
585
586 *odr = bmi270_odr_item.tbl[i].odr;
587 *uodr = bmi270_odr_item.tbl[i].uodr;
588 return 0;
589 }
590
591 return -EINVAL;
592 }
593
bmi270_irq_thread_handler(int irq,void * private)594 static irqreturn_t bmi270_irq_thread_handler(int irq, void *private)
595 {
596 struct iio_dev *indio_dev = private;
597 struct bmi270_data *data = iio_priv(indio_dev);
598 unsigned int status0, status1;
599 s64 timestamp = iio_get_time_ns(indio_dev);
600 int ret;
601
602 scoped_guard(mutex, &data->mutex) {
603 ret = regmap_read(data->regmap, BMI270_INT_STATUS_0_REG,
604 &status0);
605 if (ret)
606 return IRQ_NONE;
607
608 ret = regmap_read(data->regmap, BMI270_INT_STATUS_1_REG,
609 &status1);
610 if (ret)
611 return IRQ_NONE;
612 }
613
614 if (FIELD_GET(BMI270_INT_STATUS_1_ACC_GYR_DRDY_MSK, status1))
615 iio_trigger_poll_nested(data->trig);
616
617 if (FIELD_GET(BMI270_INT_STATUS_0_STEP_CNT_MSK, status0))
618 iio_push_event(indio_dev, IIO_UNMOD_EVENT_CODE(IIO_STEPS, 0,
619 IIO_EV_TYPE_CHANGE,
620 IIO_EV_DIR_NONE),
621 timestamp);
622
623 return IRQ_HANDLED;
624 }
625
bmi270_data_rdy_trigger_set_state(struct iio_trigger * trig,bool state)626 static int bmi270_data_rdy_trigger_set_state(struct iio_trigger *trig,
627 bool state)
628 {
629 struct bmi270_data *data = iio_trigger_get_drvdata(trig);
630 unsigned int field_value = 0;
631 unsigned int mask;
632
633 guard(mutex)(&data->mutex);
634
635 switch (data->irq_pin) {
636 case BMI270_IRQ_INT1:
637 mask = BMI270_INT_MAP_DATA_DRDY_INT1_MSK;
638 set_mask_bits(&field_value, BMI270_INT_MAP_DATA_DRDY_INT1_MSK,
639 FIELD_PREP(BMI270_INT_MAP_DATA_DRDY_INT1_MSK,
640 state));
641 break;
642 case BMI270_IRQ_INT2:
643 mask = BMI270_INT_MAP_DATA_DRDY_INT2_MSK;
644 set_mask_bits(&field_value, BMI270_INT_MAP_DATA_DRDY_INT2_MSK,
645 FIELD_PREP(BMI270_INT_MAP_DATA_DRDY_INT2_MSK,
646 state));
647 break;
648 default:
649 return -EINVAL;
650 }
651
652 return regmap_update_bits(data->regmap, BMI270_INT_MAP_DATA_REG, mask,
653 field_value);
654 }
655
656 static const struct iio_trigger_ops bmi270_trigger_ops = {
657 .set_trigger_state = &bmi270_data_rdy_trigger_set_state,
658 };
659
bmi270_trigger_handler(int irq,void * p)660 static irqreturn_t bmi270_trigger_handler(int irq, void *p)
661 {
662 struct iio_poll_func *pf = p;
663 struct iio_dev *indio_dev = pf->indio_dev;
664 struct bmi270_data *data = iio_priv(indio_dev);
665 int ret;
666
667 guard(mutex)(&data->mutex);
668
669 ret = regmap_bulk_read(data->regmap, BMI270_ACCEL_X_REG,
670 &data->buffer.channels,
671 sizeof(data->buffer.channels));
672
673 if (ret)
674 goto done;
675
676 iio_push_to_buffers_with_timestamp(indio_dev, &data->buffer,
677 pf->timestamp);
678 done:
679 iio_trigger_notify_done(indio_dev->trig);
680 return IRQ_HANDLED;
681 }
682
bmi270_get_data(struct bmi270_data * data,int chan_type,int axis,int * val)683 static int bmi270_get_data(struct bmi270_data *data, int chan_type, int axis,
684 int *val)
685 {
686 __le16 sample;
687 int reg;
688 int ret;
689
690 switch (chan_type) {
691 case IIO_ACCEL:
692 reg = BMI270_ACCEL_X_REG + (axis - IIO_MOD_X) * 2;
693 break;
694 case IIO_ANGL_VEL:
695 reg = BMI270_ANG_VEL_X_REG + (axis - IIO_MOD_X) * 2;
696 break;
697 case IIO_TEMP:
698 reg = BMI270_TEMPERATURE_0_REG;
699 break;
700 default:
701 return -EINVAL;
702 }
703
704 guard(mutex)(&data->mutex);
705
706 ret = regmap_bulk_read(data->regmap, reg, &sample, sizeof(sample));
707 if (ret)
708 return ret;
709
710 *val = sign_extend32(le16_to_cpu(sample), 15);
711
712 return IIO_VAL_INT;
713 }
714
bmi270_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)715 static int bmi270_read_raw(struct iio_dev *indio_dev,
716 struct iio_chan_spec const *chan,
717 int *val, int *val2, long mask)
718 {
719 int ret;
720 struct bmi270_data *data = iio_priv(indio_dev);
721
722 switch (mask) {
723 case IIO_CHAN_INFO_PROCESSED:
724 return bmi270_read_steps(data, val);
725 case IIO_CHAN_INFO_RAW:
726 if (!iio_device_claim_direct(indio_dev))
727 return -EBUSY;
728 ret = bmi270_get_data(data, chan->type, chan->channel2, val);
729 iio_device_release_direct(indio_dev);
730 return ret;
731 case IIO_CHAN_INFO_SCALE:
732 ret = bmi270_get_scale(data, chan->type, val, val2);
733 return ret ? ret : IIO_VAL_INT_PLUS_MICRO;
734 case IIO_CHAN_INFO_OFFSET:
735 switch (chan->type) {
736 case IIO_TEMP:
737 *val = BMI270_TEMP_OFFSET;
738 return IIO_VAL_INT;
739 default:
740 return -EINVAL;
741 }
742 case IIO_CHAN_INFO_SAMP_FREQ:
743 ret = bmi270_get_odr(data, chan->type, val, val2);
744 return ret ? ret : IIO_VAL_INT_PLUS_MICRO;
745 case IIO_CHAN_INFO_ENABLE:
746 *val = data->steps_enabled ? 1 : 0;
747 return IIO_VAL_INT;
748 default:
749 return -EINVAL;
750 }
751 }
752
bmi270_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)753 static int bmi270_write_raw(struct iio_dev *indio_dev,
754 struct iio_chan_spec const *chan,
755 int val, int val2, long mask)
756 {
757 struct bmi270_data *data = iio_priv(indio_dev);
758 int ret;
759
760 switch (mask) {
761 case IIO_CHAN_INFO_SCALE:
762 if (!iio_device_claim_direct(indio_dev))
763 return -EBUSY;
764 ret = bmi270_set_scale(data, chan->type, val2);
765 iio_device_release_direct(indio_dev);
766 return ret;
767 case IIO_CHAN_INFO_SAMP_FREQ:
768 if (!iio_device_claim_direct(indio_dev))
769 return -EBUSY;
770 ret = bmi270_set_odr(data, chan->type, val, val2);
771 iio_device_release_direct(indio_dev);
772 return ret;
773 case IIO_CHAN_INFO_ENABLE:
774 return bmi270_enable_steps(data, val);
775 case IIO_CHAN_INFO_PROCESSED: {
776 if (val || !data->steps_enabled)
777 return -EINVAL;
778
779 guard(mutex)(&data->mutex);
780 /* Clear step counter value */
781 return bmi270_update_feature_reg(data, BMI270_SC_26_REG,
782 BMI270_STEP_SC26_RST_CNT_MSK,
783 FIELD_PREP(BMI270_STEP_SC26_RST_CNT_MSK,
784 1));
785 }
786 default:
787 return -EINVAL;
788 }
789 }
790
bmi270_read_avail(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,const int ** vals,int * type,int * length,long mask)791 static int bmi270_read_avail(struct iio_dev *indio_dev,
792 struct iio_chan_spec const *chan,
793 const int **vals, int *type, int *length,
794 long mask)
795 {
796 switch (mask) {
797 case IIO_CHAN_INFO_SCALE:
798 *type = IIO_VAL_INT_PLUS_MICRO;
799 switch (chan->type) {
800 case IIO_ANGL_VEL:
801 *vals = (const int *)bmi270_gyro_scale;
802 *length = ARRAY_SIZE(bmi270_gyro_scale) * 2;
803 return IIO_AVAIL_LIST;
804 case IIO_ACCEL:
805 *vals = (const int *)bmi270_accel_scale;
806 *length = ARRAY_SIZE(bmi270_accel_scale) * 2;
807 return IIO_AVAIL_LIST;
808 default:
809 return -EINVAL;
810 }
811 case IIO_CHAN_INFO_SAMP_FREQ:
812 *type = IIO_VAL_INT_PLUS_MICRO;
813 switch (chan->type) {
814 case IIO_ANGL_VEL:
815 *vals = (const int *)bmi270_gyro_odr;
816 *length = ARRAY_SIZE(bmi270_gyro_odr) * 2;
817 return IIO_AVAIL_LIST;
818 case IIO_ACCEL:
819 *vals = (const int *)bmi270_accel_odr;
820 *length = ARRAY_SIZE(bmi270_accel_odr) * 2;
821 return IIO_AVAIL_LIST;
822 default:
823 return -EINVAL;
824 }
825 default:
826 return -EINVAL;
827 }
828 }
829
bmi270_write_event_config(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,bool state)830 static int bmi270_write_event_config(struct iio_dev *indio_dev,
831 const struct iio_chan_spec *chan,
832 enum iio_event_type type,
833 enum iio_event_direction dir, bool state)
834 {
835 struct bmi270_data *data = iio_priv(indio_dev);
836
837 switch (type) {
838 case IIO_EV_TYPE_CHANGE:
839 return bmi270_step_wtrmrk_en(data, state);
840 default:
841 return -EINVAL;
842 }
843 }
844
bmi270_read_event_config(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir)845 static int bmi270_read_event_config(struct iio_dev *indio_dev,
846 const struct iio_chan_spec *chan,
847 enum iio_event_type type,
848 enum iio_event_direction dir)
849 {
850 struct bmi270_data *data = iio_priv(indio_dev);
851 int ret, reg, regval;
852
853 guard(mutex)(&data->mutex);
854
855 switch (chan->type) {
856 case IIO_STEPS:
857 reg = bmi270_int_map_reg(data->irq_pin);
858 if (reg)
859 return reg;
860
861 ret = regmap_read(data->regmap, reg, ®val);
862 if (ret)
863 return ret;
864 return FIELD_GET(BMI270_INT_MAP_FEAT_STEP_CNT_WTRMRK_MSK,
865 regval) ? 1 : 0;
866 default:
867 return -EINVAL;
868 }
869 }
870
bmi270_write_event_value(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,enum iio_event_info info,int val,int val2)871 static int bmi270_write_event_value(struct iio_dev *indio_dev,
872 const struct iio_chan_spec *chan,
873 enum iio_event_type type,
874 enum iio_event_direction dir,
875 enum iio_event_info info,
876 int val, int val2)
877 {
878 struct bmi270_data *data = iio_priv(indio_dev);
879 unsigned int raw;
880
881 guard(mutex)(&data->mutex);
882
883 switch (type) {
884 case IIO_EV_TYPE_CHANGE:
885 if (!in_range(val, 0, BMI270_STEP_COUNTER_MAX + 1))
886 return -EINVAL;
887
888 raw = val / BMI270_STEP_COUNTER_FACTOR;
889 return bmi270_update_feature_reg(data, BMI270_SC_26_REG,
890 BMI270_STEP_SC26_WTRMRK_MSK,
891 FIELD_PREP(BMI270_STEP_SC26_WTRMRK_MSK,
892 raw));
893 default:
894 return -EINVAL;
895 }
896 }
897
bmi270_read_event_value(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,enum iio_event_info info,int * val,int * val2)898 static int bmi270_read_event_value(struct iio_dev *indio_dev,
899 const struct iio_chan_spec *chan,
900 enum iio_event_type type,
901 enum iio_event_direction dir,
902 enum iio_event_info info,
903 int *val, int *val2)
904 {
905 struct bmi270_data *data = iio_priv(indio_dev);
906 unsigned int raw;
907 u16 regval;
908 int ret;
909
910 guard(mutex)(&data->mutex);
911
912 switch (type) {
913 case IIO_EV_TYPE_CHANGE:
914 ret = bmi270_read_feature_reg(data, BMI270_SC_26_REG, ®val);
915 if (ret)
916 return ret;
917
918 raw = FIELD_GET(BMI270_STEP_SC26_WTRMRK_MSK, regval);
919 *val = raw * BMI270_STEP_COUNTER_FACTOR;
920 return IIO_VAL_INT;
921 default:
922 return -EINVAL;
923 }
924 }
925
926 static const struct iio_event_spec bmi270_step_wtrmrk_event = {
927 .type = IIO_EV_TYPE_CHANGE,
928 .dir = IIO_EV_DIR_NONE,
929 .mask_shared_by_type = BIT(IIO_EV_INFO_ENABLE) | BIT(IIO_EV_INFO_VALUE),
930 };
931
932 static const struct iio_info bmi270_info = {
933 .read_raw = bmi270_read_raw,
934 .write_raw = bmi270_write_raw,
935 .read_avail = bmi270_read_avail,
936 .write_event_config = bmi270_write_event_config,
937 .read_event_config = bmi270_read_event_config,
938 .write_event_value = bmi270_write_event_value,
939 .read_event_value = bmi270_read_event_value,
940 };
941
942 #define BMI270_ACCEL_CHANNEL(_axis) { \
943 .type = IIO_ACCEL, \
944 .modified = 1, \
945 .channel2 = IIO_MOD_##_axis, \
946 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
947 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
948 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
949 .info_mask_shared_by_type_available = \
950 BIT(IIO_CHAN_INFO_SCALE) | \
951 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
952 .scan_index = BMI270_SCAN_ACCEL_##_axis, \
953 .scan_type = { \
954 .sign = 's', \
955 .realbits = 16, \
956 .storagebits = 16, \
957 .endianness = IIO_LE, \
958 }, \
959 }
960
961 #define BMI270_ANG_VEL_CHANNEL(_axis) { \
962 .type = IIO_ANGL_VEL, \
963 .modified = 1, \
964 .channel2 = IIO_MOD_##_axis, \
965 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
966 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
967 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
968 .info_mask_shared_by_type_available = \
969 BIT(IIO_CHAN_INFO_SCALE) | \
970 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
971 .scan_index = BMI270_SCAN_GYRO_##_axis, \
972 .scan_type = { \
973 .sign = 's', \
974 .realbits = 16, \
975 .storagebits = 16, \
976 .endianness = IIO_LE, \
977 }, \
978 }
979
980 static const struct iio_chan_spec bmi270_channels[] = {
981 BMI270_ACCEL_CHANNEL(X),
982 BMI270_ACCEL_CHANNEL(Y),
983 BMI270_ACCEL_CHANNEL(Z),
984 BMI270_ANG_VEL_CHANNEL(X),
985 BMI270_ANG_VEL_CHANNEL(Y),
986 BMI270_ANG_VEL_CHANNEL(Z),
987 {
988 .type = IIO_TEMP,
989 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
990 BIT(IIO_CHAN_INFO_SCALE) |
991 BIT(IIO_CHAN_INFO_OFFSET),
992 .scan_index = -1, /* No buffer support */
993 },
994 {
995 .type = IIO_STEPS,
996 .info_mask_separate = BIT(IIO_CHAN_INFO_ENABLE) |
997 BIT(IIO_CHAN_INFO_PROCESSED),
998 .scan_index = -1, /* No buffer support */
999 .event_spec = &bmi270_step_wtrmrk_event,
1000 .num_event_specs = 1,
1001 },
1002 IIO_CHAN_SOFT_TIMESTAMP(BMI270_SCAN_TIMESTAMP),
1003 };
1004
bmi270_int_pin_config(struct bmi270_data * data,enum bmi270_irq_pin irq_pin,bool active_high,bool open_drain,bool latch)1005 static int bmi270_int_pin_config(struct bmi270_data *data,
1006 enum bmi270_irq_pin irq_pin,
1007 bool active_high, bool open_drain, bool latch)
1008 {
1009 unsigned int reg, field_value;
1010 int ret;
1011
1012 ret = regmap_update_bits(data->regmap, BMI270_INT_LATCH_REG,
1013 BMI270_INT_LATCH_REG_MSK,
1014 FIELD_PREP(BMI270_INT_LATCH_REG_MSK, latch));
1015 if (ret)
1016 return ret;
1017
1018 switch (irq_pin) {
1019 case BMI270_IRQ_INT1:
1020 reg = BMI270_INT1_IO_CTRL_REG;
1021 break;
1022 case BMI270_IRQ_INT2:
1023 reg = BMI270_INT2_IO_CTRL_REG;
1024 break;
1025 default:
1026 return -EINVAL;
1027 }
1028
1029 field_value = FIELD_PREP(BMI270_INT_IO_CTRL_LVL_MSK, active_high) |
1030 FIELD_PREP(BMI270_INT_IO_CTRL_OD_MSK, open_drain) |
1031 FIELD_PREP(BMI270_INT_IO_CTRL_OP_MSK, 1);
1032 return regmap_update_bits(data->regmap, reg,
1033 BMI270_INT_IO_LVL_OD_OP_MSK, field_value);
1034 }
1035
bmi270_trigger_probe(struct bmi270_data * data,struct iio_dev * indio_dev)1036 static int bmi270_trigger_probe(struct bmi270_data *data,
1037 struct iio_dev *indio_dev)
1038 {
1039 bool open_drain, active_high, latch;
1040 struct fwnode_handle *fwnode;
1041 enum bmi270_irq_pin irq_pin;
1042 int ret, irq, irq_type;
1043
1044 fwnode = dev_fwnode(data->dev);
1045 if (!fwnode)
1046 return -ENODEV;
1047
1048 irq = fwnode_irq_get_byname(fwnode, "INT1");
1049 if (irq > 0) {
1050 irq_pin = BMI270_IRQ_INT1;
1051 } else {
1052 irq = fwnode_irq_get_byname(fwnode, "INT2");
1053 if (irq < 0)
1054 return 0;
1055
1056 irq_pin = BMI270_IRQ_INT2;
1057 }
1058
1059 irq_type = irq_get_trigger_type(irq);
1060 switch (irq_type) {
1061 case IRQF_TRIGGER_RISING:
1062 latch = false;
1063 active_high = true;
1064 break;
1065 case IRQF_TRIGGER_HIGH:
1066 latch = true;
1067 active_high = true;
1068 break;
1069 case IRQF_TRIGGER_FALLING:
1070 latch = false;
1071 active_high = false;
1072 break;
1073 case IRQF_TRIGGER_LOW:
1074 latch = true;
1075 active_high = false;
1076 break;
1077 default:
1078 return dev_err_probe(data->dev, -EINVAL,
1079 "Invalid interrupt type 0x%x specified\n",
1080 irq_type);
1081 }
1082
1083 open_drain = fwnode_property_read_bool(fwnode, "drive-open-drain");
1084
1085 ret = bmi270_int_pin_config(data, irq_pin, active_high, open_drain,
1086 latch);
1087 if (ret)
1088 return dev_err_probe(data->dev, ret,
1089 "Failed to configure irq line\n");
1090
1091 data->trig = devm_iio_trigger_alloc(data->dev, "%s-trig-%d",
1092 indio_dev->name, irq_pin);
1093 if (!data->trig)
1094 return -ENOMEM;
1095
1096 data->trig->ops = &bmi270_trigger_ops;
1097 iio_trigger_set_drvdata(data->trig, data);
1098
1099 ret = devm_request_threaded_irq(data->dev, irq, NULL,
1100 bmi270_irq_thread_handler,
1101 IRQF_ONESHOT, "bmi270-int", indio_dev);
1102 if (ret)
1103 return dev_err_probe(data->dev, ret, "Failed to request IRQ\n");
1104
1105 ret = devm_iio_trigger_register(data->dev, data->trig);
1106 if (ret)
1107 return dev_err_probe(data->dev, ret,
1108 "Trigger registration failed\n");
1109
1110 data->irq_pin = irq_pin;
1111
1112 return 0;
1113 }
1114
bmi270_validate_chip_id(struct bmi270_data * data)1115 static int bmi270_validate_chip_id(struct bmi270_data *data)
1116 {
1117 int chip_id;
1118 int ret;
1119 struct device *dev = data->dev;
1120 struct regmap *regmap = data->regmap;
1121
1122 ret = regmap_read(regmap, BMI270_CHIP_ID_REG, &chip_id);
1123 if (ret)
1124 return dev_err_probe(dev, ret, "Failed to read chip id");
1125
1126 /*
1127 * Some manufacturers use "BMI0160" for both the BMI160 and
1128 * BMI260. If the device is actually a BMI160, the bmi160
1129 * driver should handle it and this driver should not.
1130 */
1131 if (chip_id == BMI160_CHIP_ID_VAL)
1132 return -ENODEV;
1133
1134 if (chip_id != data->chip_info->chip_id)
1135 dev_info(dev, "Unexpected chip id 0x%x", chip_id);
1136
1137 if (chip_id == bmi260_chip_info.chip_id)
1138 data->chip_info = &bmi260_chip_info;
1139 else if (chip_id == bmi270_chip_info.chip_id)
1140 data->chip_info = &bmi270_chip_info;
1141
1142 return 0;
1143 }
1144
bmi270_write_calibration_data(struct bmi270_data * data)1145 static int bmi270_write_calibration_data(struct bmi270_data *data)
1146 {
1147 int ret;
1148 int status = 0;
1149 const struct firmware *init_data;
1150 struct device *dev = data->dev;
1151 struct regmap *regmap = data->regmap;
1152
1153 ret = regmap_clear_bits(regmap, BMI270_PWR_CONF_REG,
1154 BMI270_PWR_CONF_ADV_PWR_SAVE_MSK);
1155 if (ret)
1156 return dev_err_probe(dev, ret,
1157 "Failed to write power configuration");
1158
1159 /*
1160 * After disabling advanced power save, all registers are accessible
1161 * after a 450us delay. This delay is specified in table A of the
1162 * datasheet.
1163 */
1164 usleep_range(450, 1000);
1165
1166 ret = regmap_clear_bits(regmap, BMI270_INIT_CTRL_REG,
1167 BMI270_INIT_CTRL_LOAD_DONE_MSK);
1168 if (ret)
1169 return dev_err_probe(dev, ret,
1170 "Failed to prepare device to load init data");
1171
1172 ret = request_firmware(&init_data, data->chip_info->fw_name, dev);
1173 if (ret)
1174 return dev_err_probe(dev, ret, "Failed to load init data file");
1175
1176 ret = regmap_bulk_write(regmap, BMI270_INIT_DATA_REG,
1177 init_data->data, init_data->size);
1178 release_firmware(init_data);
1179 if (ret)
1180 return dev_err_probe(dev, ret, "Failed to write init data");
1181
1182 ret = regmap_set_bits(regmap, BMI270_INIT_CTRL_REG,
1183 BMI270_INIT_CTRL_LOAD_DONE_MSK);
1184 if (ret)
1185 return dev_err_probe(dev, ret,
1186 "Failed to stop device initialization");
1187
1188 /*
1189 * Wait at least 140ms for the device to complete configuration.
1190 * This delay is specified in table C of the datasheet.
1191 */
1192 usleep_range(140000, 160000);
1193
1194 ret = regmap_read(regmap, BMI270_INTERNAL_STATUS_REG, &status);
1195 if (ret)
1196 return dev_err_probe(dev, ret, "Failed to read internal status");
1197
1198 if (status != BMI270_INTERNAL_STATUS_MSG_INIT_OK)
1199 return dev_err_probe(dev, -ENODEV, "Device failed to initialize");
1200
1201 return 0;
1202 }
1203
bmi270_configure_imu(struct bmi270_data * data)1204 static int bmi270_configure_imu(struct bmi270_data *data)
1205 {
1206 int ret;
1207 struct device *dev = data->dev;
1208 struct regmap *regmap = data->regmap;
1209
1210 ret = regmap_set_bits(regmap, BMI270_PWR_CTRL_REG,
1211 BMI270_PWR_CTRL_AUX_EN_MSK |
1212 BMI270_PWR_CTRL_GYR_EN_MSK |
1213 BMI270_PWR_CTRL_ACCEL_EN_MSK |
1214 BMI270_PWR_CTRL_TEMP_EN_MSK);
1215 if (ret)
1216 return dev_err_probe(dev, ret, "Failed to enable accelerometer and gyroscope");
1217
1218 ret = regmap_set_bits(regmap, BMI270_ACC_CONF_REG,
1219 FIELD_PREP(BMI270_ACC_CONF_ODR_MSK,
1220 BMI270_ACC_CONF_ODR_100HZ) |
1221 FIELD_PREP(BMI270_ACC_CONF_BWP_MSK,
1222 BMI270_ACC_CONF_BWP_NORMAL_MODE));
1223 if (ret)
1224 return dev_err_probe(dev, ret, "Failed to configure accelerometer");
1225
1226 ret = regmap_set_bits(regmap, BMI270_GYR_CONF_REG,
1227 FIELD_PREP(BMI270_GYR_CONF_ODR_MSK,
1228 BMI270_GYR_CONF_ODR_200HZ) |
1229 FIELD_PREP(BMI270_GYR_CONF_BWP_MSK,
1230 BMI270_GYR_CONF_BWP_NORMAL_MODE));
1231 if (ret)
1232 return dev_err_probe(dev, ret, "Failed to configure gyroscope");
1233
1234 /* Enable FIFO_WKUP, Disable ADV_PWR_SAVE and FUP_EN */
1235 ret = regmap_write(regmap, BMI270_PWR_CONF_REG,
1236 BMI270_PWR_CONF_FIFO_WKUP_MSK);
1237 if (ret)
1238 return dev_err_probe(dev, ret, "Failed to set power configuration");
1239
1240 return 0;
1241 }
1242
bmi270_chip_init(struct bmi270_data * data)1243 static int bmi270_chip_init(struct bmi270_data *data)
1244 {
1245 int ret;
1246
1247 ret = bmi270_validate_chip_id(data);
1248 if (ret)
1249 return ret;
1250
1251 ret = bmi270_write_calibration_data(data);
1252 if (ret)
1253 return ret;
1254
1255 return bmi270_configure_imu(data);
1256 }
1257
bmi270_core_probe(struct device * dev,struct regmap * regmap,const struct bmi270_chip_info * chip_info)1258 int bmi270_core_probe(struct device *dev, struct regmap *regmap,
1259 const struct bmi270_chip_info *chip_info)
1260 {
1261 int ret;
1262 struct bmi270_data *data;
1263 struct iio_dev *indio_dev;
1264
1265 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
1266 if (!indio_dev)
1267 return -ENOMEM;
1268
1269 data = iio_priv(indio_dev);
1270 data->dev = dev;
1271 data->regmap = regmap;
1272 data->chip_info = chip_info;
1273 data->irq_pin = BMI270_IRQ_DISABLED;
1274 mutex_init(&data->mutex);
1275
1276 ret = bmi270_chip_init(data);
1277 if (ret)
1278 return ret;
1279
1280 indio_dev->channels = bmi270_channels;
1281 indio_dev->num_channels = ARRAY_SIZE(bmi270_channels);
1282 indio_dev->name = chip_info->name;
1283 indio_dev->available_scan_masks = bmi270_avail_scan_masks;
1284 indio_dev->modes = INDIO_DIRECT_MODE;
1285 indio_dev->info = &bmi270_info;
1286 dev_set_drvdata(data->dev, indio_dev);
1287
1288 ret = bmi270_trigger_probe(data, indio_dev);
1289 if (ret)
1290 return ret;
1291
1292 ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
1293 iio_pollfunc_store_time,
1294 bmi270_trigger_handler, NULL);
1295 if (ret)
1296 return ret;
1297
1298 return devm_iio_device_register(dev, indio_dev);
1299 }
1300 EXPORT_SYMBOL_NS_GPL(bmi270_core_probe, "IIO_BMI270");
1301
bmi270_core_runtime_suspend(struct device * dev)1302 static int bmi270_core_runtime_suspend(struct device *dev)
1303 {
1304 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1305
1306 return iio_device_suspend_triggering(indio_dev);
1307 }
1308
bmi270_core_runtime_resume(struct device * dev)1309 static int bmi270_core_runtime_resume(struct device *dev)
1310 {
1311 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1312
1313 return iio_device_resume_triggering(indio_dev);
1314 }
1315
1316 const struct dev_pm_ops bmi270_core_pm_ops = {
1317 RUNTIME_PM_OPS(bmi270_core_runtime_suspend, bmi270_core_runtime_resume, NULL)
1318 };
1319 EXPORT_SYMBOL_NS_GPL(bmi270_core_pm_ops, "IIO_BMI270");
1320
1321 MODULE_AUTHOR("Alex Lanzano");
1322 MODULE_DESCRIPTION("BMI270 driver");
1323 MODULE_LICENSE("GPL");
1324