xref: /openbmc/linux/drivers/iio/imu/bmi160/bmi160_core.c (revision f97cee494dc92395a668445bcd24d34c89f4ff8c)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * BMI160 - Bosch IMU (accel, gyro plus external magnetometer)
4  *
5  * Copyright (c) 2016, Intel Corporation.
6  * Copyright (c) 2019, Martin Kelly.
7  *
8  * IIO core driver for BMI160, with support for I2C/SPI busses
9  *
10  * TODO: magnetometer, hardware FIFO
11  */
12 #include <linux/module.h>
13 #include <linux/regmap.h>
14 #include <linux/acpi.h>
15 #include <linux/delay.h>
16 #include <linux/irq.h>
17 #include <linux/of_irq.h>
18 #include <linux/regulator/consumer.h>
19 
20 #include <linux/iio/iio.h>
21 #include <linux/iio/triggered_buffer.h>
22 #include <linux/iio/trigger_consumer.h>
23 #include <linux/iio/buffer.h>
24 #include <linux/iio/sysfs.h>
25 #include <linux/iio/trigger.h>
26 
27 #include "bmi160.h"
28 
29 #define BMI160_REG_CHIP_ID	0x00
30 #define BMI160_CHIP_ID_VAL	0xD1
31 
32 #define BMI160_REG_PMU_STATUS	0x03
33 
34 /* X axis data low byte address, the rest can be obtained using axis offset */
35 #define BMI160_REG_DATA_MAGN_XOUT_L	0x04
36 #define BMI160_REG_DATA_GYRO_XOUT_L	0x0C
37 #define BMI160_REG_DATA_ACCEL_XOUT_L	0x12
38 
39 #define BMI160_REG_ACCEL_CONFIG		0x40
40 #define BMI160_ACCEL_CONFIG_ODR_MASK	GENMASK(3, 0)
41 #define BMI160_ACCEL_CONFIG_BWP_MASK	GENMASK(6, 4)
42 
43 #define BMI160_REG_ACCEL_RANGE		0x41
44 #define BMI160_ACCEL_RANGE_2G		0x03
45 #define BMI160_ACCEL_RANGE_4G		0x05
46 #define BMI160_ACCEL_RANGE_8G		0x08
47 #define BMI160_ACCEL_RANGE_16G		0x0C
48 
49 #define BMI160_REG_GYRO_CONFIG		0x42
50 #define BMI160_GYRO_CONFIG_ODR_MASK	GENMASK(3, 0)
51 #define BMI160_GYRO_CONFIG_BWP_MASK	GENMASK(5, 4)
52 
53 #define BMI160_REG_GYRO_RANGE		0x43
54 #define BMI160_GYRO_RANGE_2000DPS	0x00
55 #define BMI160_GYRO_RANGE_1000DPS	0x01
56 #define BMI160_GYRO_RANGE_500DPS	0x02
57 #define BMI160_GYRO_RANGE_250DPS	0x03
58 #define BMI160_GYRO_RANGE_125DPS	0x04
59 
60 #define BMI160_REG_CMD			0x7E
61 #define BMI160_CMD_ACCEL_PM_SUSPEND	0x10
62 #define BMI160_CMD_ACCEL_PM_NORMAL	0x11
63 #define BMI160_CMD_ACCEL_PM_LOW_POWER	0x12
64 #define BMI160_CMD_GYRO_PM_SUSPEND	0x14
65 #define BMI160_CMD_GYRO_PM_NORMAL	0x15
66 #define BMI160_CMD_GYRO_PM_FAST_STARTUP	0x17
67 #define BMI160_CMD_SOFTRESET		0xB6
68 
69 #define BMI160_REG_INT_EN		0x51
70 #define BMI160_DRDY_INT_EN		BIT(4)
71 
72 #define BMI160_REG_INT_OUT_CTRL		0x53
73 #define BMI160_INT_OUT_CTRL_MASK	0x0f
74 #define BMI160_INT1_OUT_CTRL_SHIFT	0
75 #define BMI160_INT2_OUT_CTRL_SHIFT	4
76 #define BMI160_EDGE_TRIGGERED		BIT(0)
77 #define BMI160_ACTIVE_HIGH		BIT(1)
78 #define BMI160_OPEN_DRAIN		BIT(2)
79 #define BMI160_OUTPUT_EN		BIT(3)
80 
81 #define BMI160_REG_INT_LATCH		0x54
82 #define BMI160_INT1_LATCH_MASK		BIT(4)
83 #define BMI160_INT2_LATCH_MASK		BIT(5)
84 
85 /* INT1 and INT2 are in the opposite order as in INT_OUT_CTRL! */
86 #define BMI160_REG_INT_MAP		0x56
87 #define BMI160_INT1_MAP_DRDY_EN		0x80
88 #define BMI160_INT2_MAP_DRDY_EN		0x08
89 
90 #define BMI160_REG_DUMMY		0x7F
91 
92 #define BMI160_NORMAL_WRITE_USLEEP	2
93 #define BMI160_SUSPENDED_WRITE_USLEEP	450
94 
95 #define BMI160_ACCEL_PMU_MIN_USLEEP	3800
96 #define BMI160_GYRO_PMU_MIN_USLEEP	80000
97 #define BMI160_SOFTRESET_USLEEP		1000
98 
99 #define BMI160_CHANNEL(_type, _axis, _index) {			\
100 	.type = _type,						\
101 	.modified = 1,						\
102 	.channel2 = IIO_MOD_##_axis,				\
103 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
104 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |  \
105 		BIT(IIO_CHAN_INFO_SAMP_FREQ),			\
106 	.scan_index = _index,					\
107 	.scan_type = {						\
108 		.sign = 's',					\
109 		.realbits = 16,					\
110 		.storagebits = 16,				\
111 		.endianness = IIO_LE,				\
112 	},							\
113 	.ext_info = bmi160_ext_info,				\
114 }
115 
116 /* scan indexes follow DATA register order */
117 enum bmi160_scan_axis {
118 	BMI160_SCAN_EXT_MAGN_X = 0,
119 	BMI160_SCAN_EXT_MAGN_Y,
120 	BMI160_SCAN_EXT_MAGN_Z,
121 	BMI160_SCAN_RHALL,
122 	BMI160_SCAN_GYRO_X,
123 	BMI160_SCAN_GYRO_Y,
124 	BMI160_SCAN_GYRO_Z,
125 	BMI160_SCAN_ACCEL_X,
126 	BMI160_SCAN_ACCEL_Y,
127 	BMI160_SCAN_ACCEL_Z,
128 	BMI160_SCAN_TIMESTAMP,
129 };
130 
131 enum bmi160_sensor_type {
132 	BMI160_ACCEL	= 0,
133 	BMI160_GYRO,
134 	BMI160_EXT_MAGN,
135 	BMI160_NUM_SENSORS /* must be last */
136 };
137 
138 enum bmi160_int_pin {
139 	BMI160_PIN_INT1,
140 	BMI160_PIN_INT2
141 };
142 
143 const struct regmap_config bmi160_regmap_config = {
144 	.reg_bits = 8,
145 	.val_bits = 8,
146 };
147 EXPORT_SYMBOL(bmi160_regmap_config);
148 
149 struct bmi160_regs {
150 	u8 data; /* LSB byte register for X-axis */
151 	u8 config;
152 	u8 config_odr_mask;
153 	u8 config_bwp_mask;
154 	u8 range;
155 	u8 pmu_cmd_normal;
156 	u8 pmu_cmd_suspend;
157 };
158 
159 static struct bmi160_regs bmi160_regs[] = {
160 	[BMI160_ACCEL] = {
161 		.data	= BMI160_REG_DATA_ACCEL_XOUT_L,
162 		.config	= BMI160_REG_ACCEL_CONFIG,
163 		.config_odr_mask = BMI160_ACCEL_CONFIG_ODR_MASK,
164 		.config_bwp_mask = BMI160_ACCEL_CONFIG_BWP_MASK,
165 		.range	= BMI160_REG_ACCEL_RANGE,
166 		.pmu_cmd_normal = BMI160_CMD_ACCEL_PM_NORMAL,
167 		.pmu_cmd_suspend = BMI160_CMD_ACCEL_PM_SUSPEND,
168 	},
169 	[BMI160_GYRO] = {
170 		.data	= BMI160_REG_DATA_GYRO_XOUT_L,
171 		.config	= BMI160_REG_GYRO_CONFIG,
172 		.config_odr_mask = BMI160_GYRO_CONFIG_ODR_MASK,
173 		.config_bwp_mask = BMI160_GYRO_CONFIG_BWP_MASK,
174 		.range	= BMI160_REG_GYRO_RANGE,
175 		.pmu_cmd_normal = BMI160_CMD_GYRO_PM_NORMAL,
176 		.pmu_cmd_suspend = BMI160_CMD_GYRO_PM_SUSPEND,
177 	},
178 };
179 
180 static unsigned long bmi160_pmu_time[] = {
181 	[BMI160_ACCEL] = BMI160_ACCEL_PMU_MIN_USLEEP,
182 	[BMI160_GYRO] = BMI160_GYRO_PMU_MIN_USLEEP,
183 };
184 
185 struct bmi160_scale {
186 	u8 bits;
187 	int uscale;
188 };
189 
190 struct bmi160_odr {
191 	u8 bits;
192 	int odr;
193 	int uodr;
194 };
195 
196 static const struct bmi160_scale bmi160_accel_scale[] = {
197 	{ BMI160_ACCEL_RANGE_2G, 598},
198 	{ BMI160_ACCEL_RANGE_4G, 1197},
199 	{ BMI160_ACCEL_RANGE_8G, 2394},
200 	{ BMI160_ACCEL_RANGE_16G, 4788},
201 };
202 
203 static const struct bmi160_scale bmi160_gyro_scale[] = {
204 	{ BMI160_GYRO_RANGE_2000DPS, 1065},
205 	{ BMI160_GYRO_RANGE_1000DPS, 532},
206 	{ BMI160_GYRO_RANGE_500DPS, 266},
207 	{ BMI160_GYRO_RANGE_250DPS, 133},
208 	{ BMI160_GYRO_RANGE_125DPS, 66},
209 };
210 
211 struct bmi160_scale_item {
212 	const struct bmi160_scale *tbl;
213 	int num;
214 };
215 
216 static const struct  bmi160_scale_item bmi160_scale_table[] = {
217 	[BMI160_ACCEL] = {
218 		.tbl	= bmi160_accel_scale,
219 		.num	= ARRAY_SIZE(bmi160_accel_scale),
220 	},
221 	[BMI160_GYRO] = {
222 		.tbl	= bmi160_gyro_scale,
223 		.num	= ARRAY_SIZE(bmi160_gyro_scale),
224 	},
225 };
226 
227 static const struct bmi160_odr bmi160_accel_odr[] = {
228 	{0x01, 0, 781250},
229 	{0x02, 1, 562500},
230 	{0x03, 3, 125000},
231 	{0x04, 6, 250000},
232 	{0x05, 12, 500000},
233 	{0x06, 25, 0},
234 	{0x07, 50, 0},
235 	{0x08, 100, 0},
236 	{0x09, 200, 0},
237 	{0x0A, 400, 0},
238 	{0x0B, 800, 0},
239 	{0x0C, 1600, 0},
240 };
241 
242 static const struct bmi160_odr bmi160_gyro_odr[] = {
243 	{0x06, 25, 0},
244 	{0x07, 50, 0},
245 	{0x08, 100, 0},
246 	{0x09, 200, 0},
247 	{0x0A, 400, 0},
248 	{0x0B, 800, 0},
249 	{0x0C, 1600, 0},
250 	{0x0D, 3200, 0},
251 };
252 
253 struct bmi160_odr_item {
254 	const struct bmi160_odr *tbl;
255 	int num;
256 };
257 
258 static const struct  bmi160_odr_item bmi160_odr_table[] = {
259 	[BMI160_ACCEL] = {
260 		.tbl	= bmi160_accel_odr,
261 		.num	= ARRAY_SIZE(bmi160_accel_odr),
262 	},
263 	[BMI160_GYRO] = {
264 		.tbl	= bmi160_gyro_odr,
265 		.num	= ARRAY_SIZE(bmi160_gyro_odr),
266 	},
267 };
268 
269 static const struct iio_mount_matrix *
270 bmi160_get_mount_matrix(const struct iio_dev *indio_dev,
271 			const struct iio_chan_spec *chan)
272 {
273 	struct bmi160_data *data = iio_priv(indio_dev);
274 
275 	return &data->orientation;
276 }
277 
278 static const struct iio_chan_spec_ext_info bmi160_ext_info[] = {
279 	IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, bmi160_get_mount_matrix),
280 	{ }
281 };
282 
283 static const struct iio_chan_spec bmi160_channels[] = {
284 	BMI160_CHANNEL(IIO_ACCEL, X, BMI160_SCAN_ACCEL_X),
285 	BMI160_CHANNEL(IIO_ACCEL, Y, BMI160_SCAN_ACCEL_Y),
286 	BMI160_CHANNEL(IIO_ACCEL, Z, BMI160_SCAN_ACCEL_Z),
287 	BMI160_CHANNEL(IIO_ANGL_VEL, X, BMI160_SCAN_GYRO_X),
288 	BMI160_CHANNEL(IIO_ANGL_VEL, Y, BMI160_SCAN_GYRO_Y),
289 	BMI160_CHANNEL(IIO_ANGL_VEL, Z, BMI160_SCAN_GYRO_Z),
290 	IIO_CHAN_SOFT_TIMESTAMP(BMI160_SCAN_TIMESTAMP),
291 };
292 
293 static enum bmi160_sensor_type bmi160_to_sensor(enum iio_chan_type iio_type)
294 {
295 	switch (iio_type) {
296 	case IIO_ACCEL:
297 		return BMI160_ACCEL;
298 	case IIO_ANGL_VEL:
299 		return BMI160_GYRO;
300 	default:
301 		return -EINVAL;
302 	}
303 }
304 
305 static
306 int bmi160_set_mode(struct bmi160_data *data, enum bmi160_sensor_type t,
307 		    bool mode)
308 {
309 	int ret;
310 	u8 cmd;
311 
312 	if (mode)
313 		cmd = bmi160_regs[t].pmu_cmd_normal;
314 	else
315 		cmd = bmi160_regs[t].pmu_cmd_suspend;
316 
317 	ret = regmap_write(data->regmap, BMI160_REG_CMD, cmd);
318 	if (ret)
319 		return ret;
320 
321 	usleep_range(bmi160_pmu_time[t], bmi160_pmu_time[t] + 1000);
322 
323 	return 0;
324 }
325 
326 static
327 int bmi160_set_scale(struct bmi160_data *data, enum bmi160_sensor_type t,
328 		     int uscale)
329 {
330 	int i;
331 
332 	for (i = 0; i < bmi160_scale_table[t].num; i++)
333 		if (bmi160_scale_table[t].tbl[i].uscale == uscale)
334 			break;
335 
336 	if (i == bmi160_scale_table[t].num)
337 		return -EINVAL;
338 
339 	return regmap_write(data->regmap, bmi160_regs[t].range,
340 			    bmi160_scale_table[t].tbl[i].bits);
341 }
342 
343 static
344 int bmi160_get_scale(struct bmi160_data *data, enum bmi160_sensor_type t,
345 		     int *uscale)
346 {
347 	int i, ret, val;
348 
349 	ret = regmap_read(data->regmap, bmi160_regs[t].range, &val);
350 	if (ret)
351 		return ret;
352 
353 	for (i = 0; i < bmi160_scale_table[t].num; i++)
354 		if (bmi160_scale_table[t].tbl[i].bits == val) {
355 			*uscale = bmi160_scale_table[t].tbl[i].uscale;
356 			return 0;
357 		}
358 
359 	return -EINVAL;
360 }
361 
362 static int bmi160_get_data(struct bmi160_data *data, int chan_type,
363 			   int axis, int *val)
364 {
365 	u8 reg;
366 	int ret;
367 	__le16 sample;
368 	enum bmi160_sensor_type t = bmi160_to_sensor(chan_type);
369 
370 	reg = bmi160_regs[t].data + (axis - IIO_MOD_X) * sizeof(sample);
371 
372 	ret = regmap_bulk_read(data->regmap, reg, &sample, sizeof(sample));
373 	if (ret)
374 		return ret;
375 
376 	*val = sign_extend32(le16_to_cpu(sample), 15);
377 
378 	return 0;
379 }
380 
381 static
382 int bmi160_set_odr(struct bmi160_data *data, enum bmi160_sensor_type t,
383 		   int odr, int uodr)
384 {
385 	int i;
386 
387 	for (i = 0; i < bmi160_odr_table[t].num; i++)
388 		if (bmi160_odr_table[t].tbl[i].odr == odr &&
389 		    bmi160_odr_table[t].tbl[i].uodr == uodr)
390 			break;
391 
392 	if (i >= bmi160_odr_table[t].num)
393 		return -EINVAL;
394 
395 	return regmap_update_bits(data->regmap,
396 				  bmi160_regs[t].config,
397 				  bmi160_regs[t].config_odr_mask,
398 				  bmi160_odr_table[t].tbl[i].bits);
399 }
400 
401 static int bmi160_get_odr(struct bmi160_data *data, enum bmi160_sensor_type t,
402 			  int *odr, int *uodr)
403 {
404 	int i, val, ret;
405 
406 	ret = regmap_read(data->regmap, bmi160_regs[t].config, &val);
407 	if (ret)
408 		return ret;
409 
410 	val &= bmi160_regs[t].config_odr_mask;
411 
412 	for (i = 0; i < bmi160_odr_table[t].num; i++)
413 		if (val == bmi160_odr_table[t].tbl[i].bits)
414 			break;
415 
416 	if (i >= bmi160_odr_table[t].num)
417 		return -EINVAL;
418 
419 	*odr = bmi160_odr_table[t].tbl[i].odr;
420 	*uodr = bmi160_odr_table[t].tbl[i].uodr;
421 
422 	return 0;
423 }
424 
425 static irqreturn_t bmi160_trigger_handler(int irq, void *p)
426 {
427 	struct iio_poll_func *pf = p;
428 	struct iio_dev *indio_dev = pf->indio_dev;
429 	struct bmi160_data *data = iio_priv(indio_dev);
430 	__le16 buf[16];
431 	/* 3 sens x 3 axis x __le16 + 3 x __le16 pad + 4 x __le16 tstamp */
432 	int i, ret, j = 0, base = BMI160_REG_DATA_MAGN_XOUT_L;
433 	__le16 sample;
434 
435 	for_each_set_bit(i, indio_dev->active_scan_mask,
436 			 indio_dev->masklength) {
437 		ret = regmap_bulk_read(data->regmap, base + i * sizeof(sample),
438 				       &sample, sizeof(sample));
439 		if (ret)
440 			goto done;
441 		buf[j++] = sample;
442 	}
443 
444 	iio_push_to_buffers_with_timestamp(indio_dev, buf, pf->timestamp);
445 done:
446 	iio_trigger_notify_done(indio_dev->trig);
447 	return IRQ_HANDLED;
448 }
449 
450 static int bmi160_read_raw(struct iio_dev *indio_dev,
451 			   struct iio_chan_spec const *chan,
452 			   int *val, int *val2, long mask)
453 {
454 	int ret;
455 	struct bmi160_data *data = iio_priv(indio_dev);
456 
457 	switch (mask) {
458 	case IIO_CHAN_INFO_RAW:
459 		ret = bmi160_get_data(data, chan->type, chan->channel2, val);
460 		if (ret)
461 			return ret;
462 		return IIO_VAL_INT;
463 	case IIO_CHAN_INFO_SCALE:
464 		*val = 0;
465 		ret = bmi160_get_scale(data,
466 				       bmi160_to_sensor(chan->type), val2);
467 		return ret ? ret : IIO_VAL_INT_PLUS_MICRO;
468 	case IIO_CHAN_INFO_SAMP_FREQ:
469 		ret = bmi160_get_odr(data, bmi160_to_sensor(chan->type),
470 				     val, val2);
471 		return ret ? ret : IIO_VAL_INT_PLUS_MICRO;
472 	default:
473 		return -EINVAL;
474 	}
475 
476 	return 0;
477 }
478 
479 static int bmi160_write_raw(struct iio_dev *indio_dev,
480 			    struct iio_chan_spec const *chan,
481 			    int val, int val2, long mask)
482 {
483 	struct bmi160_data *data = iio_priv(indio_dev);
484 
485 	switch (mask) {
486 	case IIO_CHAN_INFO_SCALE:
487 		return bmi160_set_scale(data,
488 					bmi160_to_sensor(chan->type), val2);
489 	case IIO_CHAN_INFO_SAMP_FREQ:
490 		return bmi160_set_odr(data, bmi160_to_sensor(chan->type),
491 				      val, val2);
492 	default:
493 		return -EINVAL;
494 	}
495 
496 	return 0;
497 }
498 
499 static
500 IIO_CONST_ATTR(in_accel_sampling_frequency_available,
501 	       "0.78125 1.5625 3.125 6.25 12.5 25 50 100 200 400 800 1600");
502 static
503 IIO_CONST_ATTR(in_anglvel_sampling_frequency_available,
504 	       "25 50 100 200 400 800 1600 3200");
505 static
506 IIO_CONST_ATTR(in_accel_scale_available,
507 	       "0.000598 0.001197 0.002394 0.004788");
508 static
509 IIO_CONST_ATTR(in_anglvel_scale_available,
510 	       "0.001065 0.000532 0.000266 0.000133 0.000066");
511 
512 static struct attribute *bmi160_attrs[] = {
513 	&iio_const_attr_in_accel_sampling_frequency_available.dev_attr.attr,
514 	&iio_const_attr_in_anglvel_sampling_frequency_available.dev_attr.attr,
515 	&iio_const_attr_in_accel_scale_available.dev_attr.attr,
516 	&iio_const_attr_in_anglvel_scale_available.dev_attr.attr,
517 	NULL,
518 };
519 
520 static const struct attribute_group bmi160_attrs_group = {
521 	.attrs = bmi160_attrs,
522 };
523 
524 static const struct iio_info bmi160_info = {
525 	.read_raw = bmi160_read_raw,
526 	.write_raw = bmi160_write_raw,
527 	.attrs = &bmi160_attrs_group,
528 };
529 
530 static const char *bmi160_match_acpi_device(struct device *dev)
531 {
532 	const struct acpi_device_id *id;
533 
534 	id = acpi_match_device(dev->driver->acpi_match_table, dev);
535 	if (!id)
536 		return NULL;
537 
538 	return dev_name(dev);
539 }
540 
541 static int bmi160_write_conf_reg(struct regmap *regmap, unsigned int reg,
542 				 unsigned int mask, unsigned int bits,
543 				 unsigned int write_usleep)
544 {
545 	int ret;
546 	unsigned int val;
547 
548 	ret = regmap_read(regmap, reg, &val);
549 	if (ret)
550 		return ret;
551 
552 	val = (val & ~mask) | bits;
553 
554 	ret = regmap_write(regmap, reg, val);
555 	if (ret)
556 		return ret;
557 
558 	/*
559 	 * We need to wait after writing before we can write again. See the
560 	 * datasheet, page 93.
561 	 */
562 	usleep_range(write_usleep, write_usleep + 1000);
563 
564 	return 0;
565 }
566 
567 static int bmi160_config_pin(struct regmap *regmap, enum bmi160_int_pin pin,
568 			     bool open_drain, u8 irq_mask,
569 			     unsigned long write_usleep)
570 {
571 	int ret;
572 	struct device *dev = regmap_get_device(regmap);
573 	u8 int_out_ctrl_shift;
574 	u8 int_latch_mask;
575 	u8 int_map_mask;
576 	u8 int_out_ctrl_mask;
577 	u8 int_out_ctrl_bits;
578 	const char *pin_name;
579 
580 	switch (pin) {
581 	case BMI160_PIN_INT1:
582 		int_out_ctrl_shift = BMI160_INT1_OUT_CTRL_SHIFT;
583 		int_latch_mask = BMI160_INT1_LATCH_MASK;
584 		int_map_mask = BMI160_INT1_MAP_DRDY_EN;
585 		break;
586 	case BMI160_PIN_INT2:
587 		int_out_ctrl_shift = BMI160_INT2_OUT_CTRL_SHIFT;
588 		int_latch_mask = BMI160_INT2_LATCH_MASK;
589 		int_map_mask = BMI160_INT2_MAP_DRDY_EN;
590 		break;
591 	}
592 	int_out_ctrl_mask = BMI160_INT_OUT_CTRL_MASK << int_out_ctrl_shift;
593 
594 	/*
595 	 * Enable the requested pin with the right settings:
596 	 * - Push-pull/open-drain
597 	 * - Active low/high
598 	 * - Edge/level triggered
599 	 */
600 	int_out_ctrl_bits = BMI160_OUTPUT_EN;
601 	if (open_drain)
602 		/* Default is push-pull. */
603 		int_out_ctrl_bits |= BMI160_OPEN_DRAIN;
604 	int_out_ctrl_bits |= irq_mask;
605 	int_out_ctrl_bits <<= int_out_ctrl_shift;
606 
607 	ret = bmi160_write_conf_reg(regmap, BMI160_REG_INT_OUT_CTRL,
608 				    int_out_ctrl_mask, int_out_ctrl_bits,
609 				    write_usleep);
610 	if (ret)
611 		return ret;
612 
613 	/* Set the pin to input mode with no latching. */
614 	ret = bmi160_write_conf_reg(regmap, BMI160_REG_INT_LATCH,
615 				    int_latch_mask, int_latch_mask,
616 				    write_usleep);
617 	if (ret)
618 		return ret;
619 
620 	/* Map interrupts to the requested pin. */
621 	ret = bmi160_write_conf_reg(regmap, BMI160_REG_INT_MAP,
622 				    int_map_mask, int_map_mask,
623 				    write_usleep);
624 	if (ret) {
625 		switch (pin) {
626 		case BMI160_PIN_INT1:
627 			pin_name = "INT1";
628 			break;
629 		case BMI160_PIN_INT2:
630 			pin_name = "INT2";
631 			break;
632 		}
633 		dev_err(dev, "Failed to configure %s IRQ pin", pin_name);
634 	}
635 
636 	return ret;
637 }
638 
639 int bmi160_enable_irq(struct regmap *regmap, bool enable)
640 {
641 	unsigned int enable_bit = 0;
642 
643 	if (enable)
644 		enable_bit = BMI160_DRDY_INT_EN;
645 
646 	return bmi160_write_conf_reg(regmap, BMI160_REG_INT_EN,
647 				     BMI160_DRDY_INT_EN, enable_bit,
648 				     BMI160_NORMAL_WRITE_USLEEP);
649 }
650 EXPORT_SYMBOL(bmi160_enable_irq);
651 
652 static int bmi160_get_irq(struct device_node *of_node, enum bmi160_int_pin *pin)
653 {
654 	int irq;
655 
656 	/* Use INT1 if possible, otherwise fall back to INT2. */
657 	irq = of_irq_get_byname(of_node, "INT1");
658 	if (irq > 0) {
659 		*pin = BMI160_PIN_INT1;
660 		return irq;
661 	}
662 
663 	irq = of_irq_get_byname(of_node, "INT2");
664 	if (irq > 0)
665 		*pin = BMI160_PIN_INT2;
666 
667 	return irq;
668 }
669 
670 static int bmi160_config_device_irq(struct iio_dev *indio_dev, int irq_type,
671 				    enum bmi160_int_pin pin)
672 {
673 	bool open_drain;
674 	u8 irq_mask;
675 	struct bmi160_data *data = iio_priv(indio_dev);
676 	struct device *dev = regmap_get_device(data->regmap);
677 
678 	/* Level-triggered, active-low is the default if we set all zeroes. */
679 	if (irq_type == IRQF_TRIGGER_RISING)
680 		irq_mask = BMI160_ACTIVE_HIGH | BMI160_EDGE_TRIGGERED;
681 	else if (irq_type == IRQF_TRIGGER_FALLING)
682 		irq_mask = BMI160_EDGE_TRIGGERED;
683 	else if (irq_type == IRQF_TRIGGER_HIGH)
684 		irq_mask = BMI160_ACTIVE_HIGH;
685 	else if (irq_type == IRQF_TRIGGER_LOW)
686 		irq_mask = 0;
687 	else {
688 		dev_err(&indio_dev->dev,
689 			"Invalid interrupt type 0x%x specified\n", irq_type);
690 		return -EINVAL;
691 	}
692 
693 	open_drain = of_property_read_bool(dev->of_node, "drive-open-drain");
694 
695 	return bmi160_config_pin(data->regmap, pin, open_drain, irq_mask,
696 				 BMI160_NORMAL_WRITE_USLEEP);
697 }
698 
699 static int bmi160_setup_irq(struct iio_dev *indio_dev, int irq,
700 			    enum bmi160_int_pin pin)
701 {
702 	struct irq_data *desc;
703 	u32 irq_type;
704 	int ret;
705 
706 	desc = irq_get_irq_data(irq);
707 	if (!desc) {
708 		dev_err(&indio_dev->dev, "Could not find IRQ %d\n", irq);
709 		return -EINVAL;
710 	}
711 
712 	irq_type = irqd_get_trigger_type(desc);
713 
714 	ret = bmi160_config_device_irq(indio_dev, irq_type, pin);
715 	if (ret)
716 		return ret;
717 
718 	return bmi160_probe_trigger(indio_dev, irq, irq_type);
719 }
720 
721 static int bmi160_chip_init(struct bmi160_data *data, bool use_spi)
722 {
723 	int ret;
724 	unsigned int val;
725 	struct device *dev = regmap_get_device(data->regmap);
726 
727 	ret = regulator_bulk_enable(ARRAY_SIZE(data->supplies), data->supplies);
728 	if (ret) {
729 		dev_err(dev, "Failed to enable regulators: %d\n", ret);
730 		return ret;
731 	}
732 
733 	ret = regmap_write(data->regmap, BMI160_REG_CMD, BMI160_CMD_SOFTRESET);
734 	if (ret)
735 		return ret;
736 
737 	usleep_range(BMI160_SOFTRESET_USLEEP, BMI160_SOFTRESET_USLEEP + 1);
738 
739 	/*
740 	 * CS rising edge is needed before starting SPI, so do a dummy read
741 	 * See Section 3.2.1, page 86 of the datasheet
742 	 */
743 	if (use_spi) {
744 		ret = regmap_read(data->regmap, BMI160_REG_DUMMY, &val);
745 		if (ret)
746 			return ret;
747 	}
748 
749 	ret = regmap_read(data->regmap, BMI160_REG_CHIP_ID, &val);
750 	if (ret) {
751 		dev_err(dev, "Error reading chip id\n");
752 		return ret;
753 	}
754 	if (val != BMI160_CHIP_ID_VAL) {
755 		dev_err(dev, "Wrong chip id, got %x expected %x\n",
756 			val, BMI160_CHIP_ID_VAL);
757 		return -ENODEV;
758 	}
759 
760 	ret = bmi160_set_mode(data, BMI160_ACCEL, true);
761 	if (ret)
762 		return ret;
763 
764 	ret = bmi160_set_mode(data, BMI160_GYRO, true);
765 	if (ret)
766 		return ret;
767 
768 	return 0;
769 }
770 
771 static int bmi160_data_rdy_trigger_set_state(struct iio_trigger *trig,
772 					     bool enable)
773 {
774 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
775 	struct bmi160_data *data = iio_priv(indio_dev);
776 
777 	return bmi160_enable_irq(data->regmap, enable);
778 }
779 
780 static const struct iio_trigger_ops bmi160_trigger_ops = {
781 	.set_trigger_state = &bmi160_data_rdy_trigger_set_state,
782 };
783 
784 int bmi160_probe_trigger(struct iio_dev *indio_dev, int irq, u32 irq_type)
785 {
786 	struct bmi160_data *data = iio_priv(indio_dev);
787 	int ret;
788 
789 	data->trig = devm_iio_trigger_alloc(&indio_dev->dev, "%s-dev%d",
790 					    indio_dev->name, indio_dev->id);
791 
792 	if (data->trig == NULL)
793 		return -ENOMEM;
794 
795 	ret = devm_request_irq(&indio_dev->dev, irq,
796 			       &iio_trigger_generic_data_rdy_poll,
797 			       irq_type, "bmi160", data->trig);
798 	if (ret)
799 		return ret;
800 
801 	data->trig->dev.parent = regmap_get_device(data->regmap);
802 	data->trig->ops = &bmi160_trigger_ops;
803 	iio_trigger_set_drvdata(data->trig, indio_dev);
804 
805 	ret = devm_iio_trigger_register(&indio_dev->dev, data->trig);
806 	if (ret)
807 		return ret;
808 
809 	indio_dev->trig = iio_trigger_get(data->trig);
810 
811 	return 0;
812 }
813 
814 static void bmi160_chip_uninit(void *data)
815 {
816 	struct bmi160_data *bmi_data = data;
817 	struct device *dev = regmap_get_device(bmi_data->regmap);
818 	int ret;
819 
820 	bmi160_set_mode(bmi_data, BMI160_GYRO, false);
821 	bmi160_set_mode(bmi_data, BMI160_ACCEL, false);
822 
823 	ret = regulator_bulk_disable(ARRAY_SIZE(bmi_data->supplies),
824 				     bmi_data->supplies);
825 	if (ret)
826 		dev_err(dev, "Failed to disable regulators: %d\n", ret);
827 }
828 
829 int bmi160_core_probe(struct device *dev, struct regmap *regmap,
830 		      const char *name, bool use_spi)
831 {
832 	struct iio_dev *indio_dev;
833 	struct bmi160_data *data;
834 	int irq;
835 	enum bmi160_int_pin int_pin;
836 	int ret;
837 
838 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
839 	if (!indio_dev)
840 		return -ENOMEM;
841 
842 	data = iio_priv(indio_dev);
843 	dev_set_drvdata(dev, indio_dev);
844 	data->regmap = regmap;
845 
846 	data->supplies[0].supply = "vdd";
847 	data->supplies[1].supply = "vddio";
848 	ret = devm_regulator_bulk_get(dev,
849 				      ARRAY_SIZE(data->supplies),
850 				      data->supplies);
851 	if (ret) {
852 		dev_err(dev, "Failed to get regulators: %d\n", ret);
853 		return ret;
854 	}
855 
856 	ret = iio_read_mount_matrix(dev, "mount-matrix",
857 				    &data->orientation);
858 	if (ret)
859 		return ret;
860 
861 	ret = bmi160_chip_init(data, use_spi);
862 	if (ret)
863 		return ret;
864 
865 	ret = devm_add_action_or_reset(dev, bmi160_chip_uninit, data);
866 	if (ret)
867 		return ret;
868 
869 	if (!name && ACPI_HANDLE(dev))
870 		name = bmi160_match_acpi_device(dev);
871 
872 	indio_dev->channels = bmi160_channels;
873 	indio_dev->num_channels = ARRAY_SIZE(bmi160_channels);
874 	indio_dev->name = name;
875 	indio_dev->modes = INDIO_DIRECT_MODE;
876 	indio_dev->info = &bmi160_info;
877 
878 	ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
879 					      iio_pollfunc_store_time,
880 					      bmi160_trigger_handler, NULL);
881 	if (ret)
882 		return ret;
883 
884 	irq = bmi160_get_irq(dev->of_node, &int_pin);
885 	if (irq > 0) {
886 		ret = bmi160_setup_irq(indio_dev, irq, int_pin);
887 		if (ret)
888 			dev_err(&indio_dev->dev, "Failed to setup IRQ %d\n",
889 				irq);
890 	} else {
891 		dev_info(&indio_dev->dev, "Not setting up IRQ trigger\n");
892 	}
893 
894 	return devm_iio_device_register(dev, indio_dev);
895 }
896 EXPORT_SYMBOL_GPL(bmi160_core_probe);
897 
898 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
899 MODULE_DESCRIPTION("Bosch BMI160 driver");
900 MODULE_LICENSE("GPL v2");
901