1 /*
2  * BMI160 - Bosch IMU (accel, gyro plus external magnetometer)
3  *
4  * Copyright (c) 2016, Intel Corporation.
5  *
6  * This file is subject to the terms and conditions of version 2 of
7  * the GNU General Public License.  See the file COPYING in the main
8  * directory of this archive for more details.
9  *
10  * IIO core driver for BMI160, with support for I2C/SPI busses
11  *
12  * TODO: magnetometer, interrupts, hardware FIFO
13  */
14 #include <linux/module.h>
15 #include <linux/regmap.h>
16 #include <linux/acpi.h>
17 #include <linux/delay.h>
18 
19 #include <linux/iio/iio.h>
20 #include <linux/iio/triggered_buffer.h>
21 #include <linux/iio/trigger_consumer.h>
22 #include <linux/iio/buffer.h>
23 #include <linux/iio/sysfs.h>
24 
25 #include "bmi160.h"
26 
27 #define BMI160_REG_CHIP_ID	0x00
28 #define BMI160_CHIP_ID_VAL	0xD1
29 
30 #define BMI160_REG_PMU_STATUS	0x03
31 
32 /* X axis data low byte address, the rest can be obtained using axis offset */
33 #define BMI160_REG_DATA_MAGN_XOUT_L	0x04
34 #define BMI160_REG_DATA_GYRO_XOUT_L	0x0C
35 #define BMI160_REG_DATA_ACCEL_XOUT_L	0x12
36 
37 #define BMI160_REG_ACCEL_CONFIG		0x40
38 #define BMI160_ACCEL_CONFIG_ODR_MASK	GENMASK(3, 0)
39 #define BMI160_ACCEL_CONFIG_BWP_MASK	GENMASK(6, 4)
40 
41 #define BMI160_REG_ACCEL_RANGE		0x41
42 #define BMI160_ACCEL_RANGE_2G		0x03
43 #define BMI160_ACCEL_RANGE_4G		0x05
44 #define BMI160_ACCEL_RANGE_8G		0x08
45 #define BMI160_ACCEL_RANGE_16G		0x0C
46 
47 #define BMI160_REG_GYRO_CONFIG		0x42
48 #define BMI160_GYRO_CONFIG_ODR_MASK	GENMASK(3, 0)
49 #define BMI160_GYRO_CONFIG_BWP_MASK	GENMASK(5, 4)
50 
51 #define BMI160_REG_GYRO_RANGE		0x43
52 #define BMI160_GYRO_RANGE_2000DPS	0x00
53 #define BMI160_GYRO_RANGE_1000DPS	0x01
54 #define BMI160_GYRO_RANGE_500DPS	0x02
55 #define BMI160_GYRO_RANGE_250DPS	0x03
56 #define BMI160_GYRO_RANGE_125DPS	0x04
57 
58 #define BMI160_REG_CMD			0x7E
59 #define BMI160_CMD_ACCEL_PM_SUSPEND	0x10
60 #define BMI160_CMD_ACCEL_PM_NORMAL	0x11
61 #define BMI160_CMD_ACCEL_PM_LOW_POWER	0x12
62 #define BMI160_CMD_GYRO_PM_SUSPEND	0x14
63 #define BMI160_CMD_GYRO_PM_NORMAL	0x15
64 #define BMI160_CMD_GYRO_PM_FAST_STARTUP	0x17
65 #define BMI160_CMD_SOFTRESET		0xB6
66 
67 #define BMI160_REG_DUMMY		0x7F
68 
69 #define BMI160_ACCEL_PMU_MIN_USLEEP	3800
70 #define BMI160_GYRO_PMU_MIN_USLEEP	80000
71 #define BMI160_SOFTRESET_USLEEP		1000
72 
73 #define BMI160_CHANNEL(_type, _axis, _index) {			\
74 	.type = _type,						\
75 	.modified = 1,						\
76 	.channel2 = IIO_MOD_##_axis,				\
77 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
78 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |  \
79 		BIT(IIO_CHAN_INFO_SAMP_FREQ),			\
80 	.scan_index = _index,					\
81 	.scan_type = {						\
82 		.sign = 's',					\
83 		.realbits = 16,					\
84 		.storagebits = 16,				\
85 		.endianness = IIO_LE,				\
86 	},							\
87 }
88 
89 /* scan indexes follow DATA register order */
90 enum bmi160_scan_axis {
91 	BMI160_SCAN_EXT_MAGN_X = 0,
92 	BMI160_SCAN_EXT_MAGN_Y,
93 	BMI160_SCAN_EXT_MAGN_Z,
94 	BMI160_SCAN_RHALL,
95 	BMI160_SCAN_GYRO_X,
96 	BMI160_SCAN_GYRO_Y,
97 	BMI160_SCAN_GYRO_Z,
98 	BMI160_SCAN_ACCEL_X,
99 	BMI160_SCAN_ACCEL_Y,
100 	BMI160_SCAN_ACCEL_Z,
101 	BMI160_SCAN_TIMESTAMP,
102 };
103 
104 enum bmi160_sensor_type {
105 	BMI160_ACCEL	= 0,
106 	BMI160_GYRO,
107 	BMI160_EXT_MAGN,
108 	BMI160_NUM_SENSORS /* must be last */
109 };
110 
111 struct bmi160_data {
112 	struct regmap *regmap;
113 };
114 
115 const struct regmap_config bmi160_regmap_config = {
116 	.reg_bits = 8,
117 	.val_bits = 8,
118 };
119 EXPORT_SYMBOL(bmi160_regmap_config);
120 
121 struct bmi160_regs {
122 	u8 data; /* LSB byte register for X-axis */
123 	u8 config;
124 	u8 config_odr_mask;
125 	u8 config_bwp_mask;
126 	u8 range;
127 	u8 pmu_cmd_normal;
128 	u8 pmu_cmd_suspend;
129 };
130 
131 static struct bmi160_regs bmi160_regs[] = {
132 	[BMI160_ACCEL] = {
133 		.data	= BMI160_REG_DATA_ACCEL_XOUT_L,
134 		.config	= BMI160_REG_ACCEL_CONFIG,
135 		.config_odr_mask = BMI160_ACCEL_CONFIG_ODR_MASK,
136 		.config_bwp_mask = BMI160_ACCEL_CONFIG_BWP_MASK,
137 		.range	= BMI160_REG_ACCEL_RANGE,
138 		.pmu_cmd_normal = BMI160_CMD_ACCEL_PM_NORMAL,
139 		.pmu_cmd_suspend = BMI160_CMD_ACCEL_PM_SUSPEND,
140 	},
141 	[BMI160_GYRO] = {
142 		.data	= BMI160_REG_DATA_GYRO_XOUT_L,
143 		.config	= BMI160_REG_GYRO_CONFIG,
144 		.config_odr_mask = BMI160_GYRO_CONFIG_ODR_MASK,
145 		.config_bwp_mask = BMI160_GYRO_CONFIG_BWP_MASK,
146 		.range	= BMI160_REG_GYRO_RANGE,
147 		.pmu_cmd_normal = BMI160_CMD_GYRO_PM_NORMAL,
148 		.pmu_cmd_suspend = BMI160_CMD_GYRO_PM_SUSPEND,
149 	},
150 };
151 
152 static unsigned long bmi160_pmu_time[] = {
153 	[BMI160_ACCEL] = BMI160_ACCEL_PMU_MIN_USLEEP,
154 	[BMI160_GYRO] = BMI160_GYRO_PMU_MIN_USLEEP,
155 };
156 
157 struct bmi160_scale {
158 	u8 bits;
159 	int uscale;
160 };
161 
162 struct bmi160_odr {
163 	u8 bits;
164 	int odr;
165 	int uodr;
166 };
167 
168 static const struct bmi160_scale bmi160_accel_scale[] = {
169 	{ BMI160_ACCEL_RANGE_2G, 598},
170 	{ BMI160_ACCEL_RANGE_4G, 1197},
171 	{ BMI160_ACCEL_RANGE_8G, 2394},
172 	{ BMI160_ACCEL_RANGE_16G, 4788},
173 };
174 
175 static const struct bmi160_scale bmi160_gyro_scale[] = {
176 	{ BMI160_GYRO_RANGE_2000DPS, 1065},
177 	{ BMI160_GYRO_RANGE_1000DPS, 532},
178 	{ BMI160_GYRO_RANGE_500DPS, 266},
179 	{ BMI160_GYRO_RANGE_250DPS, 133},
180 	{ BMI160_GYRO_RANGE_125DPS, 66},
181 };
182 
183 struct bmi160_scale_item {
184 	const struct bmi160_scale *tbl;
185 	int num;
186 };
187 
188 static const struct  bmi160_scale_item bmi160_scale_table[] = {
189 	[BMI160_ACCEL] = {
190 		.tbl	= bmi160_accel_scale,
191 		.num	= ARRAY_SIZE(bmi160_accel_scale),
192 	},
193 	[BMI160_GYRO] = {
194 		.tbl	= bmi160_gyro_scale,
195 		.num	= ARRAY_SIZE(bmi160_gyro_scale),
196 	},
197 };
198 
199 static const struct bmi160_odr bmi160_accel_odr[] = {
200 	{0x01, 0, 781250},
201 	{0x02, 1, 562500},
202 	{0x03, 3, 125000},
203 	{0x04, 6, 250000},
204 	{0x05, 12, 500000},
205 	{0x06, 25, 0},
206 	{0x07, 50, 0},
207 	{0x08, 100, 0},
208 	{0x09, 200, 0},
209 	{0x0A, 400, 0},
210 	{0x0B, 800, 0},
211 	{0x0C, 1600, 0},
212 };
213 
214 static const struct bmi160_odr bmi160_gyro_odr[] = {
215 	{0x06, 25, 0},
216 	{0x07, 50, 0},
217 	{0x08, 100, 0},
218 	{0x09, 200, 0},
219 	{0x0A, 400, 0},
220 	{0x0B, 800, 0},
221 	{0x0C, 1600, 0},
222 	{0x0D, 3200, 0},
223 };
224 
225 struct bmi160_odr_item {
226 	const struct bmi160_odr *tbl;
227 	int num;
228 };
229 
230 static const struct  bmi160_odr_item bmi160_odr_table[] = {
231 	[BMI160_ACCEL] = {
232 		.tbl	= bmi160_accel_odr,
233 		.num	= ARRAY_SIZE(bmi160_accel_odr),
234 	},
235 	[BMI160_GYRO] = {
236 		.tbl	= bmi160_gyro_odr,
237 		.num	= ARRAY_SIZE(bmi160_gyro_odr),
238 	},
239 };
240 
241 static const struct iio_chan_spec bmi160_channels[] = {
242 	BMI160_CHANNEL(IIO_ACCEL, X, BMI160_SCAN_ACCEL_X),
243 	BMI160_CHANNEL(IIO_ACCEL, Y, BMI160_SCAN_ACCEL_Y),
244 	BMI160_CHANNEL(IIO_ACCEL, Z, BMI160_SCAN_ACCEL_Z),
245 	BMI160_CHANNEL(IIO_ANGL_VEL, X, BMI160_SCAN_GYRO_X),
246 	BMI160_CHANNEL(IIO_ANGL_VEL, Y, BMI160_SCAN_GYRO_Y),
247 	BMI160_CHANNEL(IIO_ANGL_VEL, Z, BMI160_SCAN_GYRO_Z),
248 	IIO_CHAN_SOFT_TIMESTAMP(BMI160_SCAN_TIMESTAMP),
249 };
250 
251 static enum bmi160_sensor_type bmi160_to_sensor(enum iio_chan_type iio_type)
252 {
253 	switch (iio_type) {
254 	case IIO_ACCEL:
255 		return BMI160_ACCEL;
256 	case IIO_ANGL_VEL:
257 		return BMI160_GYRO;
258 	default:
259 		return -EINVAL;
260 	}
261 }
262 
263 static
264 int bmi160_set_mode(struct bmi160_data *data, enum bmi160_sensor_type t,
265 		    bool mode)
266 {
267 	int ret;
268 	u8 cmd;
269 
270 	if (mode)
271 		cmd = bmi160_regs[t].pmu_cmd_normal;
272 	else
273 		cmd = bmi160_regs[t].pmu_cmd_suspend;
274 
275 	ret = regmap_write(data->regmap, BMI160_REG_CMD, cmd);
276 	if (ret < 0)
277 		return ret;
278 
279 	usleep_range(bmi160_pmu_time[t], bmi160_pmu_time[t] + 1000);
280 
281 	return 0;
282 }
283 
284 static
285 int bmi160_set_scale(struct bmi160_data *data, enum bmi160_sensor_type t,
286 		     int uscale)
287 {
288 	int i;
289 
290 	for (i = 0; i < bmi160_scale_table[t].num; i++)
291 		if (bmi160_scale_table[t].tbl[i].uscale == uscale)
292 			break;
293 
294 	if (i == bmi160_scale_table[t].num)
295 		return -EINVAL;
296 
297 	return regmap_write(data->regmap, bmi160_regs[t].range,
298 			    bmi160_scale_table[t].tbl[i].bits);
299 }
300 
301 static
302 int bmi160_get_scale(struct bmi160_data *data, enum bmi160_sensor_type t,
303 		     int *uscale)
304 {
305 	int i, ret, val;
306 
307 	ret = regmap_read(data->regmap, bmi160_regs[t].range, &val);
308 	if (ret < 0)
309 		return ret;
310 
311 	for (i = 0; i < bmi160_scale_table[t].num; i++)
312 		if (bmi160_scale_table[t].tbl[i].bits == val) {
313 			*uscale = bmi160_scale_table[t].tbl[i].uscale;
314 			return 0;
315 		}
316 
317 	return -EINVAL;
318 }
319 
320 static int bmi160_get_data(struct bmi160_data *data, int chan_type,
321 			   int axis, int *val)
322 {
323 	u8 reg;
324 	int ret;
325 	__le16 sample;
326 	enum bmi160_sensor_type t = bmi160_to_sensor(chan_type);
327 
328 	reg = bmi160_regs[t].data + (axis - IIO_MOD_X) * sizeof(sample);
329 
330 	ret = regmap_bulk_read(data->regmap, reg, &sample, sizeof(sample));
331 	if (ret < 0)
332 		return ret;
333 
334 	*val = sign_extend32(le16_to_cpu(sample), 15);
335 
336 	return 0;
337 }
338 
339 static
340 int bmi160_set_odr(struct bmi160_data *data, enum bmi160_sensor_type t,
341 		   int odr, int uodr)
342 {
343 	int i;
344 
345 	for (i = 0; i < bmi160_odr_table[t].num; i++)
346 		if (bmi160_odr_table[t].tbl[i].odr == odr &&
347 		    bmi160_odr_table[t].tbl[i].uodr == uodr)
348 			break;
349 
350 	if (i >= bmi160_odr_table[t].num)
351 		return -EINVAL;
352 
353 	return regmap_update_bits(data->regmap,
354 				  bmi160_regs[t].config,
355 				  bmi160_regs[t].config_odr_mask,
356 				  bmi160_odr_table[t].tbl[i].bits);
357 }
358 
359 static int bmi160_get_odr(struct bmi160_data *data, enum bmi160_sensor_type t,
360 			  int *odr, int *uodr)
361 {
362 	int i, val, ret;
363 
364 	ret = regmap_read(data->regmap, bmi160_regs[t].config, &val);
365 	if (ret < 0)
366 		return ret;
367 
368 	val &= bmi160_regs[t].config_odr_mask;
369 
370 	for (i = 0; i < bmi160_odr_table[t].num; i++)
371 		if (val == bmi160_odr_table[t].tbl[i].bits)
372 			break;
373 
374 	if (i >= bmi160_odr_table[t].num)
375 		return -EINVAL;
376 
377 	*odr = bmi160_odr_table[t].tbl[i].odr;
378 	*uodr = bmi160_odr_table[t].tbl[i].uodr;
379 
380 	return 0;
381 }
382 
383 static irqreturn_t bmi160_trigger_handler(int irq, void *p)
384 {
385 	struct iio_poll_func *pf = p;
386 	struct iio_dev *indio_dev = pf->indio_dev;
387 	struct bmi160_data *data = iio_priv(indio_dev);
388 	__le16 buf[16];
389 	/* 3 sens x 3 axis x __le16 + 3 x __le16 pad + 4 x __le16 tstamp */
390 	int i, ret, j = 0, base = BMI160_REG_DATA_MAGN_XOUT_L;
391 	__le16 sample;
392 
393 	for_each_set_bit(i, indio_dev->active_scan_mask,
394 			 indio_dev->masklength) {
395 		ret = regmap_bulk_read(data->regmap, base + i * sizeof(sample),
396 				       &sample, sizeof(sample));
397 		if (ret < 0)
398 			goto done;
399 		buf[j++] = sample;
400 	}
401 
402 	iio_push_to_buffers_with_timestamp(indio_dev, buf,
403 					   iio_get_time_ns(indio_dev));
404 done:
405 	iio_trigger_notify_done(indio_dev->trig);
406 	return IRQ_HANDLED;
407 }
408 
409 static int bmi160_read_raw(struct iio_dev *indio_dev,
410 			   struct iio_chan_spec const *chan,
411 			   int *val, int *val2, long mask)
412 {
413 	int ret;
414 	struct bmi160_data *data = iio_priv(indio_dev);
415 
416 	switch (mask) {
417 	case IIO_CHAN_INFO_RAW:
418 		ret = bmi160_get_data(data, chan->type, chan->channel2, val);
419 		if (ret < 0)
420 			return ret;
421 		return IIO_VAL_INT;
422 	case IIO_CHAN_INFO_SCALE:
423 		*val = 0;
424 		ret = bmi160_get_scale(data,
425 				       bmi160_to_sensor(chan->type), val2);
426 		return ret < 0 ? ret : IIO_VAL_INT_PLUS_MICRO;
427 	case IIO_CHAN_INFO_SAMP_FREQ:
428 		ret = bmi160_get_odr(data, bmi160_to_sensor(chan->type),
429 				     val, val2);
430 		return ret < 0 ? ret : IIO_VAL_INT_PLUS_MICRO;
431 	default:
432 		return -EINVAL;
433 	}
434 
435 	return 0;
436 }
437 
438 static int bmi160_write_raw(struct iio_dev *indio_dev,
439 			    struct iio_chan_spec const *chan,
440 			    int val, int val2, long mask)
441 {
442 	struct bmi160_data *data = iio_priv(indio_dev);
443 
444 	switch (mask) {
445 	case IIO_CHAN_INFO_SCALE:
446 		return bmi160_set_scale(data,
447 					bmi160_to_sensor(chan->type), val2);
448 		break;
449 	case IIO_CHAN_INFO_SAMP_FREQ:
450 		return bmi160_set_odr(data, bmi160_to_sensor(chan->type),
451 				      val, val2);
452 	default:
453 		return -EINVAL;
454 	}
455 
456 	return 0;
457 }
458 
459 static
460 IIO_CONST_ATTR(in_accel_sampling_frequency_available,
461 	       "0.78125 1.5625 3.125 6.25 12.5 25 50 100 200 400 800 1600");
462 static
463 IIO_CONST_ATTR(in_anglvel_sampling_frequency_available,
464 	       "25 50 100 200 400 800 1600 3200");
465 static
466 IIO_CONST_ATTR(in_accel_scale_available,
467 	       "0.000598 0.001197 0.002394 0.004788");
468 static
469 IIO_CONST_ATTR(in_anglvel_scale_available,
470 	       "0.001065 0.000532 0.000266 0.000133 0.000066");
471 
472 static struct attribute *bmi160_attrs[] = {
473 	&iio_const_attr_in_accel_sampling_frequency_available.dev_attr.attr,
474 	&iio_const_attr_in_anglvel_sampling_frequency_available.dev_attr.attr,
475 	&iio_const_attr_in_accel_scale_available.dev_attr.attr,
476 	&iio_const_attr_in_anglvel_scale_available.dev_attr.attr,
477 	NULL,
478 };
479 
480 static const struct attribute_group bmi160_attrs_group = {
481 	.attrs = bmi160_attrs,
482 };
483 
484 static const struct iio_info bmi160_info = {
485 	.read_raw = bmi160_read_raw,
486 	.write_raw = bmi160_write_raw,
487 	.attrs = &bmi160_attrs_group,
488 };
489 
490 static const char *bmi160_match_acpi_device(struct device *dev)
491 {
492 	const struct acpi_device_id *id;
493 
494 	id = acpi_match_device(dev->driver->acpi_match_table, dev);
495 	if (!id)
496 		return NULL;
497 
498 	return dev_name(dev);
499 }
500 
501 static int bmi160_chip_init(struct bmi160_data *data, bool use_spi)
502 {
503 	int ret;
504 	unsigned int val;
505 	struct device *dev = regmap_get_device(data->regmap);
506 
507 	ret = regmap_write(data->regmap, BMI160_REG_CMD, BMI160_CMD_SOFTRESET);
508 	if (ret < 0)
509 		return ret;
510 
511 	usleep_range(BMI160_SOFTRESET_USLEEP, BMI160_SOFTRESET_USLEEP + 1);
512 
513 	/*
514 	 * CS rising edge is needed before starting SPI, so do a dummy read
515 	 * See Section 3.2.1, page 86 of the datasheet
516 	 */
517 	if (use_spi) {
518 		ret = regmap_read(data->regmap, BMI160_REG_DUMMY, &val);
519 		if (ret < 0)
520 			return ret;
521 	}
522 
523 	ret = regmap_read(data->regmap, BMI160_REG_CHIP_ID, &val);
524 	if (ret < 0) {
525 		dev_err(dev, "Error reading chip id\n");
526 		return ret;
527 	}
528 	if (val != BMI160_CHIP_ID_VAL) {
529 		dev_err(dev, "Wrong chip id, got %x expected %x\n",
530 			val, BMI160_CHIP_ID_VAL);
531 		return -ENODEV;
532 	}
533 
534 	ret = bmi160_set_mode(data, BMI160_ACCEL, true);
535 	if (ret < 0)
536 		return ret;
537 
538 	ret = bmi160_set_mode(data, BMI160_GYRO, true);
539 	if (ret < 0)
540 		return ret;
541 
542 	return 0;
543 }
544 
545 static void bmi160_chip_uninit(struct bmi160_data *data)
546 {
547 	bmi160_set_mode(data, BMI160_GYRO, false);
548 	bmi160_set_mode(data, BMI160_ACCEL, false);
549 }
550 
551 int bmi160_core_probe(struct device *dev, struct regmap *regmap,
552 		      const char *name, bool use_spi)
553 {
554 	struct iio_dev *indio_dev;
555 	struct bmi160_data *data;
556 	int ret;
557 
558 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
559 	if (!indio_dev)
560 		return -ENOMEM;
561 
562 	data = iio_priv(indio_dev);
563 	dev_set_drvdata(dev, indio_dev);
564 	data->regmap = regmap;
565 
566 	ret = bmi160_chip_init(data, use_spi);
567 	if (ret < 0)
568 		return ret;
569 
570 	if (!name && ACPI_HANDLE(dev))
571 		name = bmi160_match_acpi_device(dev);
572 
573 	indio_dev->dev.parent = dev;
574 	indio_dev->channels = bmi160_channels;
575 	indio_dev->num_channels = ARRAY_SIZE(bmi160_channels);
576 	indio_dev->name = name;
577 	indio_dev->modes = INDIO_DIRECT_MODE;
578 	indio_dev->info = &bmi160_info;
579 
580 	ret = iio_triggered_buffer_setup(indio_dev, NULL,
581 					 bmi160_trigger_handler, NULL);
582 	if (ret < 0)
583 		goto uninit;
584 
585 	ret = iio_device_register(indio_dev);
586 	if (ret < 0)
587 		goto buffer_cleanup;
588 
589 	return 0;
590 buffer_cleanup:
591 	iio_triggered_buffer_cleanup(indio_dev);
592 uninit:
593 	bmi160_chip_uninit(data);
594 	return ret;
595 }
596 EXPORT_SYMBOL_GPL(bmi160_core_probe);
597 
598 void bmi160_core_remove(struct device *dev)
599 {
600 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
601 	struct bmi160_data *data = iio_priv(indio_dev);
602 
603 	iio_device_unregister(indio_dev);
604 	iio_triggered_buffer_cleanup(indio_dev);
605 	bmi160_chip_uninit(data);
606 }
607 EXPORT_SYMBOL_GPL(bmi160_core_remove);
608 
609 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com");
610 MODULE_DESCRIPTION("Bosch BMI160 driver");
611 MODULE_LICENSE("GPL v2");
612