xref: /openbmc/linux/drivers/iio/accel/bma180.c (revision 022dacdd)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * bma180.c - IIO driver for Bosch BMA180 triaxial acceleration sensor
4  *
5  * Copyright 2013 Oleksandr Kravchenko <x0199363@ti.com>
6  *
7  * Support for BMA250 (c) Peter Meerwald <pmeerw@pmeerw.net>
8  *
9  * SPI is not supported by driver
10  * BMA180: 7-bit I2C slave address 0x40 or 0x41
11  * BMA250: 7-bit I2C slave address 0x18 or 0x19
12  * BMA254: 7-bit I2C slave address 0x18 or 0x19
13  */
14 
15 #include <linux/module.h>
16 #include <linux/i2c.h>
17 #include <linux/interrupt.h>
18 #include <linux/delay.h>
19 #include <linux/of_device.h>
20 #include <linux/of.h>
21 #include <linux/bitops.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/slab.h>
24 #include <linux/string.h>
25 #include <linux/iio/iio.h>
26 #include <linux/iio/sysfs.h>
27 #include <linux/iio/buffer.h>
28 #include <linux/iio/trigger.h>
29 #include <linux/iio/trigger_consumer.h>
30 #include <linux/iio/triggered_buffer.h>
31 
32 #define BMA180_DRV_NAME "bma180"
33 #define BMA180_IRQ_NAME "bma180_event"
34 
35 enum chip_ids {
36 	BMA180,
37 	BMA250,
38 	BMA254,
39 };
40 
41 struct bma180_data;
42 
43 struct bma180_part_info {
44 	u8 chip_id;
45 	const struct iio_chan_spec *channels;
46 	unsigned int num_channels;
47 	const int *scale_table;
48 	unsigned int num_scales;
49 	const int *bw_table;
50 	unsigned int num_bw;
51 	int center_temp;
52 
53 	u8 int_reset_reg, int_reset_mask;
54 	u8 sleep_reg, sleep_mask;
55 	u8 bw_reg, bw_mask;
56 	u8 scale_reg, scale_mask;
57 	u8 power_reg, power_mask, lowpower_val;
58 	u8 int_enable_reg, int_enable_mask;
59 	u8 int_map_reg, int_enable_dataready_int1_mask;
60 	u8 softreset_reg;
61 
62 	int (*chip_config)(struct bma180_data *data);
63 	void (*chip_disable)(struct bma180_data *data);
64 };
65 
66 /* Register set */
67 #define BMA180_CHIP_ID		0x00 /* Need to distinguish BMA180 from other */
68 #define BMA180_ACC_X_LSB	0x02 /* First of 6 registers of accel data */
69 #define BMA180_TEMP		0x08
70 #define BMA180_CTRL_REG0	0x0d
71 #define BMA180_RESET		0x10
72 #define BMA180_BW_TCS		0x20
73 #define BMA180_CTRL_REG3	0x21
74 #define BMA180_TCO_Z		0x30
75 #define BMA180_OFFSET_LSB1	0x35
76 
77 /* BMA180_CTRL_REG0 bits */
78 #define BMA180_DIS_WAKE_UP	BIT(0) /* Disable wake up mode */
79 #define BMA180_SLEEP		BIT(1) /* 1 - chip will sleep */
80 #define BMA180_EE_W		BIT(4) /* Unlock writing to addr from 0x20 */
81 #define BMA180_RESET_INT	BIT(6) /* Reset pending interrupts */
82 
83 /* BMA180_CTRL_REG3 bits */
84 #define BMA180_NEW_DATA_INT	BIT(1) /* Intr every new accel data is ready */
85 
86 /* BMA180_OFFSET_LSB1 skipping mode bit */
87 #define BMA180_SMP_SKIP		BIT(0)
88 
89 /* Bit masks for registers bit fields */
90 #define BMA180_RANGE		0x0e /* Range of measured accel values */
91 #define BMA180_BW		0xf0 /* Accel bandwidth */
92 #define BMA180_MODE_CONFIG	0x03 /* Config operation modes */
93 
94 /* We have to write this value in reset register to do soft reset */
95 #define BMA180_RESET_VAL	0xb6
96 
97 #define BMA180_ID_REG_VAL	0x03
98 #define BMA250_ID_REG_VAL	0x03
99 #define BMA254_ID_REG_VAL	0xfa /* 250 decimal */
100 
101 /* Chip power modes */
102 #define BMA180_LOW_POWER	0x03
103 
104 #define BMA250_RANGE_REG	0x0f
105 #define BMA250_BW_REG		0x10
106 #define BMA250_POWER_REG	0x11
107 #define BMA250_RESET_REG	0x14
108 #define BMA250_INT_ENABLE_REG	0x17
109 #define BMA250_INT_MAP_REG	0x1a
110 #define BMA250_INT_RESET_REG	0x21
111 
112 #define BMA250_RANGE_MASK	GENMASK(3, 0) /* Range of accel values */
113 #define BMA250_BW_MASK		GENMASK(4, 0) /* Accel bandwidth */
114 #define BMA250_SUSPEND_MASK	BIT(7) /* chip will sleep */
115 #define BMA250_LOWPOWER_MASK	BIT(6)
116 #define BMA250_DATA_INTEN_MASK	BIT(4)
117 #define BMA250_INT1_DATA_MASK	BIT(0)
118 #define BMA250_INT_RESET_MASK	BIT(7) /* Reset pending interrupts */
119 
120 #define BMA254_RANGE_REG	0x0f
121 #define BMA254_BW_REG		0x10
122 #define BMA254_POWER_REG	0x11
123 #define BMA254_RESET_REG	0x14
124 #define BMA254_INT_ENABLE_REG	0x17
125 #define BMA254_INT_MAP_REG	0x1a
126 #define BMA254_INT_RESET_REG	0x21
127 
128 #define BMA254_RANGE_MASK	GENMASK(3, 0) /* Range of accel values */
129 #define BMA254_BW_MASK		GENMASK(4, 0) /* Accel bandwidth */
130 #define BMA254_SUSPEND_MASK	BIT(7) /* chip will sleep */
131 #define BMA254_LOWPOWER_MASK	BIT(6)
132 #define BMA254_DATA_INTEN_MASK	BIT(4)
133 #define BMA254_INT2_DATA_MASK	BIT(7)
134 #define BMA254_INT1_DATA_MASK	BIT(0)
135 #define BMA254_INT_RESET_MASK	BIT(7) /* Reset pending interrupts */
136 
137 struct bma180_data {
138 	struct regulator *vdd_supply;
139 	struct regulator *vddio_supply;
140 	struct i2c_client *client;
141 	struct iio_trigger *trig;
142 	const struct bma180_part_info *part_info;
143 	struct iio_mount_matrix orientation;
144 	struct mutex mutex;
145 	bool sleep_state;
146 	int scale;
147 	int bw;
148 	bool pmode;
149 	u8 buff[16]; /* 3x 16-bit + 8-bit + padding + timestamp */
150 };
151 
152 enum bma180_chan {
153 	AXIS_X,
154 	AXIS_Y,
155 	AXIS_Z,
156 	TEMP
157 };
158 
159 static int bma180_bw_table[] = { 10, 20, 40, 75, 150, 300 }; /* Hz */
160 static int bma180_scale_table[] = { 1275, 1863, 2452, 3727, 4903, 9709, 19417 };
161 
162 static int bma25x_bw_table[] = { 8, 16, 31, 63, 125, 250 }; /* Hz */
163 static int bma25x_scale_table[] = { 0, 0, 0, 38344, 0, 76590, 0, 0, 153180, 0,
164 	0, 0, 306458 };
165 
166 static int bma180_get_data_reg(struct bma180_data *data, enum bma180_chan chan)
167 {
168 	int ret;
169 
170 	if (data->sleep_state)
171 		return -EBUSY;
172 
173 	switch (chan) {
174 	case TEMP:
175 		ret = i2c_smbus_read_byte_data(data->client, BMA180_TEMP);
176 		if (ret < 0)
177 			dev_err(&data->client->dev, "failed to read temp register\n");
178 		break;
179 	default:
180 		ret = i2c_smbus_read_word_data(data->client,
181 			BMA180_ACC_X_LSB + chan * 2);
182 		if (ret < 0)
183 			dev_err(&data->client->dev,
184 				"failed to read accel_%c register\n",
185 				'x' + chan);
186 	}
187 
188 	return ret;
189 }
190 
191 static int bma180_set_bits(struct bma180_data *data, u8 reg, u8 mask, u8 val)
192 {
193 	int ret = i2c_smbus_read_byte_data(data->client, reg);
194 	u8 reg_val = (ret & ~mask) | (val << (ffs(mask) - 1));
195 
196 	if (ret < 0)
197 		return ret;
198 
199 	return i2c_smbus_write_byte_data(data->client, reg, reg_val);
200 }
201 
202 static int bma180_reset_intr(struct bma180_data *data)
203 {
204 	int ret = bma180_set_bits(data, data->part_info->int_reset_reg,
205 		data->part_info->int_reset_mask, 1);
206 
207 	if (ret)
208 		dev_err(&data->client->dev, "failed to reset interrupt\n");
209 
210 	return ret;
211 }
212 
213 static int bma180_set_new_data_intr_state(struct bma180_data *data, bool state)
214 {
215 	int ret = bma180_set_bits(data, data->part_info->int_enable_reg,
216 			data->part_info->int_enable_mask, state);
217 	if (ret)
218 		goto err;
219 	ret = bma180_reset_intr(data);
220 	if (ret)
221 		goto err;
222 
223 	return 0;
224 
225 err:
226 	dev_err(&data->client->dev,
227 		"failed to set new data interrupt state %d\n", state);
228 	return ret;
229 }
230 
231 static int bma180_set_sleep_state(struct bma180_data *data, bool state)
232 {
233 	int ret = bma180_set_bits(data, data->part_info->sleep_reg,
234 		data->part_info->sleep_mask, state);
235 
236 	if (ret) {
237 		dev_err(&data->client->dev,
238 			"failed to set sleep state %d\n", state);
239 		return ret;
240 	}
241 	data->sleep_state = state;
242 
243 	return 0;
244 }
245 
246 static int bma180_set_ee_writing_state(struct bma180_data *data, bool state)
247 {
248 	int ret = bma180_set_bits(data, BMA180_CTRL_REG0, BMA180_EE_W, state);
249 
250 	if (ret)
251 		dev_err(&data->client->dev,
252 			"failed to set ee writing state %d\n", state);
253 
254 	return ret;
255 }
256 
257 static int bma180_set_bw(struct bma180_data *data, int val)
258 {
259 	int ret, i;
260 
261 	if (data->sleep_state)
262 		return -EBUSY;
263 
264 	for (i = 0; i < data->part_info->num_bw; ++i) {
265 		if (data->part_info->bw_table[i] == val) {
266 			ret = bma180_set_bits(data, data->part_info->bw_reg,
267 				data->part_info->bw_mask, i);
268 			if (ret) {
269 				dev_err(&data->client->dev,
270 					"failed to set bandwidth\n");
271 				return ret;
272 			}
273 			data->bw = val;
274 			return 0;
275 		}
276 	}
277 
278 	return -EINVAL;
279 }
280 
281 static int bma180_set_scale(struct bma180_data *data, int val)
282 {
283 	int ret, i;
284 
285 	if (data->sleep_state)
286 		return -EBUSY;
287 
288 	for (i = 0; i < data->part_info->num_scales; ++i)
289 		if (data->part_info->scale_table[i] == val) {
290 			ret = bma180_set_bits(data, data->part_info->scale_reg,
291 				data->part_info->scale_mask, i);
292 			if (ret) {
293 				dev_err(&data->client->dev,
294 					"failed to set scale\n");
295 				return ret;
296 			}
297 			data->scale = val;
298 			return 0;
299 		}
300 
301 	return -EINVAL;
302 }
303 
304 static int bma180_set_pmode(struct bma180_data *data, bool mode)
305 {
306 	u8 reg_val = mode ? data->part_info->lowpower_val : 0;
307 	int ret = bma180_set_bits(data, data->part_info->power_reg,
308 		data->part_info->power_mask, reg_val);
309 
310 	if (ret) {
311 		dev_err(&data->client->dev, "failed to set power mode\n");
312 		return ret;
313 	}
314 	data->pmode = mode;
315 
316 	return 0;
317 }
318 
319 static int bma180_soft_reset(struct bma180_data *data)
320 {
321 	int ret = i2c_smbus_write_byte_data(data->client,
322 		data->part_info->softreset_reg, BMA180_RESET_VAL);
323 
324 	if (ret)
325 		dev_err(&data->client->dev, "failed to reset the chip\n");
326 
327 	return ret;
328 }
329 
330 static int bma180_chip_init(struct bma180_data *data)
331 {
332 	/* Try to read chip_id register. It must return 0x03. */
333 	int ret = i2c_smbus_read_byte_data(data->client, BMA180_CHIP_ID);
334 
335 	if (ret < 0)
336 		return ret;
337 	if (ret != data->part_info->chip_id) {
338 		dev_err(&data->client->dev, "wrong chip ID %d expected %d\n",
339 			ret, data->part_info->chip_id);
340 		return -ENODEV;
341 	}
342 
343 	ret = bma180_soft_reset(data);
344 	if (ret)
345 		return ret;
346 	/*
347 	 * No serial transaction should occur within minimum 10 us
348 	 * after soft_reset command
349 	 */
350 	msleep(20);
351 
352 	ret = bma180_set_new_data_intr_state(data, false);
353 	if (ret)
354 		return ret;
355 
356 	return bma180_set_pmode(data, false);
357 }
358 
359 static int bma180_chip_config(struct bma180_data *data)
360 {
361 	int ret = bma180_chip_init(data);
362 
363 	if (ret)
364 		goto err;
365 	ret = bma180_set_bits(data, BMA180_CTRL_REG0, BMA180_DIS_WAKE_UP, 1);
366 	if (ret)
367 		goto err;
368 	ret = bma180_set_ee_writing_state(data, true);
369 	if (ret)
370 		goto err;
371 	ret = bma180_set_bits(data, BMA180_OFFSET_LSB1, BMA180_SMP_SKIP, 1);
372 	if (ret)
373 		goto err;
374 	ret = bma180_set_bw(data, 20); /* 20 Hz */
375 	if (ret)
376 		goto err;
377 	ret = bma180_set_scale(data, 2452); /* 2 G */
378 	if (ret)
379 		goto err;
380 
381 	return 0;
382 
383 err:
384 	dev_err(&data->client->dev, "failed to config the chip\n");
385 	return ret;
386 }
387 
388 static int bma25x_chip_config(struct bma180_data *data)
389 {
390 	int ret = bma180_chip_init(data);
391 
392 	if (ret)
393 		goto err;
394 	ret = bma180_set_bw(data, 16); /* 16 Hz */
395 	if (ret)
396 		goto err;
397 	ret = bma180_set_scale(data, 38344); /* 2 G */
398 	if (ret)
399 		goto err;
400 	/*
401 	 * This enables dataready interrupt on the INT1 pin
402 	 * FIXME: support using the INT2 pin
403 	 */
404 	ret = bma180_set_bits(data, data->part_info->int_map_reg,
405 		data->part_info->int_enable_dataready_int1_mask, 1);
406 	if (ret)
407 		goto err;
408 
409 	return 0;
410 
411 err:
412 	dev_err(&data->client->dev, "failed to config the chip\n");
413 	return ret;
414 }
415 
416 static void bma180_chip_disable(struct bma180_data *data)
417 {
418 	if (bma180_set_new_data_intr_state(data, false))
419 		goto err;
420 	if (bma180_set_ee_writing_state(data, false))
421 		goto err;
422 	if (bma180_set_sleep_state(data, true))
423 		goto err;
424 
425 	return;
426 
427 err:
428 	dev_err(&data->client->dev, "failed to disable the chip\n");
429 }
430 
431 static void bma25x_chip_disable(struct bma180_data *data)
432 {
433 	if (bma180_set_new_data_intr_state(data, false))
434 		goto err;
435 	if (bma180_set_sleep_state(data, true))
436 		goto err;
437 
438 	return;
439 
440 err:
441 	dev_err(&data->client->dev, "failed to disable the chip\n");
442 }
443 
444 static ssize_t bma180_show_avail(char *buf, const int *vals, unsigned int n,
445 				 bool micros)
446 {
447 	size_t len = 0;
448 	int i;
449 
450 	for (i = 0; i < n; i++) {
451 		if (!vals[i])
452 			continue;
453 		len += scnprintf(buf + len, PAGE_SIZE - len,
454 			micros ? "0.%06d " : "%d ", vals[i]);
455 	}
456 	buf[len - 1] = '\n';
457 
458 	return len;
459 }
460 
461 static ssize_t bma180_show_filter_freq_avail(struct device *dev,
462 				struct device_attribute *attr, char *buf)
463 {
464 	struct bma180_data *data = iio_priv(dev_to_iio_dev(dev));
465 
466 	return bma180_show_avail(buf, data->part_info->bw_table,
467 		data->part_info->num_bw, false);
468 }
469 
470 static ssize_t bma180_show_scale_avail(struct device *dev,
471 				struct device_attribute *attr, char *buf)
472 {
473 	struct bma180_data *data = iio_priv(dev_to_iio_dev(dev));
474 
475 	return bma180_show_avail(buf, data->part_info->scale_table,
476 		data->part_info->num_scales, true);
477 }
478 
479 static IIO_DEVICE_ATTR(in_accel_filter_low_pass_3db_frequency_available,
480 	S_IRUGO, bma180_show_filter_freq_avail, NULL, 0);
481 
482 static IIO_DEVICE_ATTR(in_accel_scale_available,
483 	S_IRUGO, bma180_show_scale_avail, NULL, 0);
484 
485 static struct attribute *bma180_attributes[] = {
486 	&iio_dev_attr_in_accel_filter_low_pass_3db_frequency_available.
487 		dev_attr.attr,
488 	&iio_dev_attr_in_accel_scale_available.dev_attr.attr,
489 	NULL,
490 };
491 
492 static const struct attribute_group bma180_attrs_group = {
493 	.attrs = bma180_attributes,
494 };
495 
496 static int bma180_read_raw(struct iio_dev *indio_dev,
497 		struct iio_chan_spec const *chan, int *val, int *val2,
498 		long mask)
499 {
500 	struct bma180_data *data = iio_priv(indio_dev);
501 	int ret;
502 
503 	switch (mask) {
504 	case IIO_CHAN_INFO_RAW:
505 		ret = iio_device_claim_direct_mode(indio_dev);
506 		if (ret)
507 			return ret;
508 
509 		mutex_lock(&data->mutex);
510 		ret = bma180_get_data_reg(data, chan->scan_index);
511 		mutex_unlock(&data->mutex);
512 		iio_device_release_direct_mode(indio_dev);
513 		if (ret < 0)
514 			return ret;
515 		*val = sign_extend32(ret >> chan->scan_type.shift,
516 			chan->scan_type.realbits - 1);
517 		return IIO_VAL_INT;
518 	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
519 		*val = data->bw;
520 		return IIO_VAL_INT;
521 	case IIO_CHAN_INFO_SCALE:
522 		switch (chan->type) {
523 		case IIO_ACCEL:
524 			*val = 0;
525 			*val2 = data->scale;
526 			return IIO_VAL_INT_PLUS_MICRO;
527 		case IIO_TEMP:
528 			*val = 500;
529 			return IIO_VAL_INT;
530 		default:
531 			return -EINVAL;
532 		}
533 	case IIO_CHAN_INFO_OFFSET:
534 		*val = data->part_info->center_temp;
535 		return IIO_VAL_INT;
536 	default:
537 		return -EINVAL;
538 	}
539 }
540 
541 static int bma180_write_raw(struct iio_dev *indio_dev,
542 		struct iio_chan_spec const *chan, int val, int val2, long mask)
543 {
544 	struct bma180_data *data = iio_priv(indio_dev);
545 	int ret;
546 
547 	switch (mask) {
548 	case IIO_CHAN_INFO_SCALE:
549 		if (val)
550 			return -EINVAL;
551 		mutex_lock(&data->mutex);
552 		ret = bma180_set_scale(data, val2);
553 		mutex_unlock(&data->mutex);
554 		return ret;
555 	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
556 		if (val2)
557 			return -EINVAL;
558 		mutex_lock(&data->mutex);
559 		ret = bma180_set_bw(data, val);
560 		mutex_unlock(&data->mutex);
561 		return ret;
562 	default:
563 		return -EINVAL;
564 	}
565 }
566 
567 static const struct iio_info bma180_info = {
568 	.attrs			= &bma180_attrs_group,
569 	.read_raw		= bma180_read_raw,
570 	.write_raw		= bma180_write_raw,
571 };
572 
573 static const char * const bma180_power_modes[] = { "low_noise", "low_power" };
574 
575 static int bma180_get_power_mode(struct iio_dev *indio_dev,
576 		const struct iio_chan_spec *chan)
577 {
578 	struct bma180_data *data = iio_priv(indio_dev);
579 
580 	return data->pmode;
581 }
582 
583 static int bma180_set_power_mode(struct iio_dev *indio_dev,
584 		const struct iio_chan_spec *chan, unsigned int mode)
585 {
586 	struct bma180_data *data = iio_priv(indio_dev);
587 	int ret;
588 
589 	mutex_lock(&data->mutex);
590 	ret = bma180_set_pmode(data, mode);
591 	mutex_unlock(&data->mutex);
592 
593 	return ret;
594 }
595 
596 static const struct iio_mount_matrix *
597 bma180_accel_get_mount_matrix(const struct iio_dev *indio_dev,
598 				const struct iio_chan_spec *chan)
599 {
600 	struct bma180_data *data = iio_priv(indio_dev);
601 
602 	return &data->orientation;
603 }
604 
605 static const struct iio_enum bma180_power_mode_enum = {
606 	.items = bma180_power_modes,
607 	.num_items = ARRAY_SIZE(bma180_power_modes),
608 	.get = bma180_get_power_mode,
609 	.set = bma180_set_power_mode,
610 };
611 
612 static const struct iio_chan_spec_ext_info bma180_ext_info[] = {
613 	IIO_ENUM("power_mode", true, &bma180_power_mode_enum),
614 	IIO_ENUM_AVAILABLE("power_mode", &bma180_power_mode_enum),
615 	IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, bma180_accel_get_mount_matrix),
616 	{ }
617 };
618 
619 #define BMA180_ACC_CHANNEL(_axis, _bits) {				\
620 	.type = IIO_ACCEL,						\
621 	.modified = 1,							\
622 	.channel2 = IIO_MOD_##_axis,					\
623 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),			\
624 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |		\
625 		BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),	\
626 	.scan_index = AXIS_##_axis,					\
627 	.scan_type = {							\
628 		.sign = 's',						\
629 		.realbits = _bits,					\
630 		.storagebits = 16,					\
631 		.shift = 16 - _bits,					\
632 	},								\
633 	.ext_info = bma180_ext_info,					\
634 }
635 
636 #define BMA180_TEMP_CHANNEL {						\
637 	.type = IIO_TEMP,						\
638 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |			\
639 		BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET),	\
640 	.scan_index = TEMP,						\
641 	.scan_type = {							\
642 		.sign = 's',						\
643 		.realbits = 8,						\
644 		.storagebits = 16,					\
645 	},								\
646 }
647 
648 static const struct iio_chan_spec bma180_channels[] = {
649 	BMA180_ACC_CHANNEL(X, 14),
650 	BMA180_ACC_CHANNEL(Y, 14),
651 	BMA180_ACC_CHANNEL(Z, 14),
652 	BMA180_TEMP_CHANNEL,
653 	IIO_CHAN_SOFT_TIMESTAMP(4),
654 };
655 
656 static const struct iio_chan_spec bma250_channels[] = {
657 	BMA180_ACC_CHANNEL(X, 10),
658 	BMA180_ACC_CHANNEL(Y, 10),
659 	BMA180_ACC_CHANNEL(Z, 10),
660 	BMA180_TEMP_CHANNEL,
661 	IIO_CHAN_SOFT_TIMESTAMP(4),
662 };
663 
664 static const struct iio_chan_spec bma254_channels[] = {
665 	BMA180_ACC_CHANNEL(X, 12),
666 	BMA180_ACC_CHANNEL(Y, 12),
667 	BMA180_ACC_CHANNEL(Z, 12),
668 	BMA180_TEMP_CHANNEL,
669 	IIO_CHAN_SOFT_TIMESTAMP(4),
670 };
671 
672 static const struct bma180_part_info bma180_part_info[] = {
673 	[BMA180] = {
674 		.chip_id = BMA180_ID_REG_VAL,
675 		.channels = bma180_channels,
676 		.num_channels = ARRAY_SIZE(bma180_channels),
677 		.scale_table = bma180_scale_table,
678 		.num_scales = ARRAY_SIZE(bma180_scale_table),
679 		.bw_table = bma180_bw_table,
680 		.num_bw = ARRAY_SIZE(bma180_bw_table),
681 		.center_temp = 48, /* 0 LSB @ 24 degree C */
682 		.int_reset_reg = BMA180_CTRL_REG0,
683 		.int_reset_mask = BMA180_RESET_INT,
684 		.sleep_reg = BMA180_CTRL_REG0,
685 		.sleep_mask = BMA180_SLEEP,
686 		.bw_reg = BMA180_BW_TCS,
687 		.bw_mask = BMA180_BW,
688 		.scale_reg = BMA180_OFFSET_LSB1,
689 		.scale_mask = BMA180_RANGE,
690 		.power_reg = BMA180_TCO_Z,
691 		.power_mask = BMA180_MODE_CONFIG,
692 		.lowpower_val = BMA180_LOW_POWER,
693 		.int_enable_reg = BMA180_CTRL_REG3,
694 		.int_enable_mask = BMA180_NEW_DATA_INT,
695 		.softreset_reg = BMA180_RESET,
696 		.chip_config = bma180_chip_config,
697 		.chip_disable = bma180_chip_disable,
698 	},
699 	[BMA250] = {
700 		.chip_id = BMA250_ID_REG_VAL,
701 		.channels = bma250_channels,
702 		.num_channels = ARRAY_SIZE(bma250_channels),
703 		.scale_table = bma25x_scale_table,
704 		.num_scales = ARRAY_SIZE(bma25x_scale_table),
705 		.bw_table = bma25x_bw_table,
706 		.num_bw = ARRAY_SIZE(bma25x_bw_table),
707 		.center_temp = 48, /* 0 LSB @ 24 degree C */
708 		.int_reset_reg = BMA250_INT_RESET_REG,
709 		.int_reset_mask = BMA250_INT_RESET_MASK,
710 		.sleep_reg = BMA250_POWER_REG,
711 		.sleep_mask = BMA250_SUSPEND_MASK,
712 		.bw_reg = BMA250_BW_REG,
713 		.bw_mask = BMA250_BW_MASK,
714 		.scale_reg = BMA250_RANGE_REG,
715 		.scale_mask = BMA250_RANGE_MASK,
716 		.power_reg = BMA250_POWER_REG,
717 		.power_mask = BMA250_LOWPOWER_MASK,
718 		.lowpower_val = 1,
719 		.int_enable_reg = BMA250_INT_ENABLE_REG,
720 		.int_enable_mask = BMA250_DATA_INTEN_MASK,
721 		.int_map_reg = BMA250_INT_MAP_REG,
722 		.int_enable_dataready_int1_mask = BMA250_INT1_DATA_MASK,
723 		.softreset_reg = BMA250_RESET_REG,
724 		.chip_config = bma25x_chip_config,
725 		.chip_disable = bma25x_chip_disable,
726 	},
727 	[BMA254] = {
728 		.chip_id = BMA254_ID_REG_VAL,
729 		.channels = bma254_channels,
730 		.num_channels = ARRAY_SIZE(bma254_channels),
731 		.scale_table = bma25x_scale_table,
732 		.num_scales = ARRAY_SIZE(bma25x_scale_table),
733 		.bw_table = bma25x_bw_table,
734 		.num_bw = ARRAY_SIZE(bma25x_bw_table),
735 		.center_temp = 46, /* 0 LSB @ 23 degree C */
736 		.int_reset_reg = BMA254_INT_RESET_REG,
737 		.int_reset_mask = BMA254_INT_RESET_MASK,
738 		.sleep_reg = BMA254_POWER_REG,
739 		.sleep_mask = BMA254_SUSPEND_MASK,
740 		.bw_reg = BMA254_BW_REG,
741 		.bw_mask = BMA254_BW_MASK,
742 		.scale_reg = BMA254_RANGE_REG,
743 		.scale_mask = BMA254_RANGE_MASK,
744 		.power_reg = BMA254_POWER_REG,
745 		.power_mask = BMA254_LOWPOWER_MASK,
746 		.lowpower_val = 1,
747 		.int_enable_reg = BMA254_INT_ENABLE_REG,
748 		.int_enable_mask = BMA254_DATA_INTEN_MASK,
749 		.int_map_reg = BMA254_INT_MAP_REG,
750 		.int_enable_dataready_int1_mask = BMA254_INT1_DATA_MASK,
751 		.softreset_reg = BMA254_RESET_REG,
752 		.chip_config = bma25x_chip_config,
753 		.chip_disable = bma25x_chip_disable,
754 	},
755 };
756 
757 static irqreturn_t bma180_trigger_handler(int irq, void *p)
758 {
759 	struct iio_poll_func *pf = p;
760 	struct iio_dev *indio_dev = pf->indio_dev;
761 	struct bma180_data *data = iio_priv(indio_dev);
762 	s64 time_ns = iio_get_time_ns(indio_dev);
763 	int bit, ret, i = 0;
764 
765 	mutex_lock(&data->mutex);
766 
767 	for_each_set_bit(bit, indio_dev->active_scan_mask,
768 			 indio_dev->masklength) {
769 		ret = bma180_get_data_reg(data, bit);
770 		if (ret < 0) {
771 			mutex_unlock(&data->mutex);
772 			goto err;
773 		}
774 		((s16 *)data->buff)[i++] = ret;
775 	}
776 
777 	mutex_unlock(&data->mutex);
778 
779 	iio_push_to_buffers_with_timestamp(indio_dev, data->buff, time_ns);
780 err:
781 	iio_trigger_notify_done(indio_dev->trig);
782 
783 	return IRQ_HANDLED;
784 }
785 
786 static int bma180_data_rdy_trigger_set_state(struct iio_trigger *trig,
787 		bool state)
788 {
789 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
790 	struct bma180_data *data = iio_priv(indio_dev);
791 
792 	return bma180_set_new_data_intr_state(data, state);
793 }
794 
795 static int bma180_trig_try_reen(struct iio_trigger *trig)
796 {
797 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
798 	struct bma180_data *data = iio_priv(indio_dev);
799 
800 	return bma180_reset_intr(data);
801 }
802 
803 static const struct iio_trigger_ops bma180_trigger_ops = {
804 	.set_trigger_state = bma180_data_rdy_trigger_set_state,
805 	.try_reenable = bma180_trig_try_reen,
806 };
807 
808 static int bma180_probe(struct i2c_client *client,
809 		const struct i2c_device_id *id)
810 {
811 	struct device *dev = &client->dev;
812 	struct bma180_data *data;
813 	struct iio_dev *indio_dev;
814 	enum chip_ids chip;
815 	int ret;
816 
817 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
818 	if (!indio_dev)
819 		return -ENOMEM;
820 
821 	data = iio_priv(indio_dev);
822 	i2c_set_clientdata(client, indio_dev);
823 	data->client = client;
824 	if (client->dev.of_node)
825 		chip = (enum chip_ids)of_device_get_match_data(dev);
826 	else
827 		chip = id->driver_data;
828 	data->part_info = &bma180_part_info[chip];
829 
830 	ret = iio_read_mount_matrix(dev, "mount-matrix",
831 				&data->orientation);
832 	if (ret)
833 		return ret;
834 
835 	data->vdd_supply = devm_regulator_get(dev, "vdd");
836 	if (IS_ERR(data->vdd_supply)) {
837 		if (PTR_ERR(data->vdd_supply) != -EPROBE_DEFER)
838 			dev_err(dev, "Failed to get vdd regulator %d\n",
839 				(int)PTR_ERR(data->vdd_supply));
840 		return PTR_ERR(data->vdd_supply);
841 	}
842 	data->vddio_supply = devm_regulator_get(dev, "vddio");
843 	if (IS_ERR(data->vddio_supply)) {
844 		if (PTR_ERR(data->vddio_supply) != -EPROBE_DEFER)
845 			dev_err(dev, "Failed to get vddio regulator %d\n",
846 				(int)PTR_ERR(data->vddio_supply));
847 		return PTR_ERR(data->vddio_supply);
848 	}
849 	/* Typical voltage 2.4V these are min and max */
850 	ret = regulator_set_voltage(data->vdd_supply, 1620000, 3600000);
851 	if (ret)
852 		return ret;
853 	ret = regulator_set_voltage(data->vddio_supply, 1200000, 3600000);
854 	if (ret)
855 		return ret;
856 	ret = regulator_enable(data->vdd_supply);
857 	if (ret) {
858 		dev_err(dev, "Failed to enable vdd regulator: %d\n", ret);
859 		return ret;
860 	}
861 	ret = regulator_enable(data->vddio_supply);
862 	if (ret) {
863 		dev_err(dev, "Failed to enable vddio regulator: %d\n", ret);
864 		goto err_disable_vdd;
865 	}
866 	/* Wait to make sure we started up properly (3 ms at least) */
867 	usleep_range(3000, 5000);
868 
869 	ret = data->part_info->chip_config(data);
870 	if (ret < 0)
871 		goto err_chip_disable;
872 
873 	mutex_init(&data->mutex);
874 	indio_dev->dev.parent = dev;
875 	indio_dev->channels = data->part_info->channels;
876 	indio_dev->num_channels = data->part_info->num_channels;
877 	indio_dev->name = id->name;
878 	indio_dev->modes = INDIO_DIRECT_MODE;
879 	indio_dev->info = &bma180_info;
880 
881 	if (client->irq > 0) {
882 		data->trig = iio_trigger_alloc("%s-dev%d", indio_dev->name,
883 			indio_dev->id);
884 		if (!data->trig) {
885 			ret = -ENOMEM;
886 			goto err_chip_disable;
887 		}
888 
889 		ret = devm_request_irq(dev, client->irq,
890 			iio_trigger_generic_data_rdy_poll, IRQF_TRIGGER_RISING,
891 			"bma180_event", data->trig);
892 		if (ret) {
893 			dev_err(dev, "unable to request IRQ\n");
894 			goto err_trigger_free;
895 		}
896 
897 		data->trig->dev.parent = dev;
898 		data->trig->ops = &bma180_trigger_ops;
899 		iio_trigger_set_drvdata(data->trig, indio_dev);
900 		indio_dev->trig = iio_trigger_get(data->trig);
901 
902 		ret = iio_trigger_register(data->trig);
903 		if (ret)
904 			goto err_trigger_free;
905 	}
906 
907 	ret = iio_triggered_buffer_setup(indio_dev, NULL,
908 			bma180_trigger_handler, NULL);
909 	if (ret < 0) {
910 		dev_err(dev, "unable to setup iio triggered buffer\n");
911 		goto err_trigger_unregister;
912 	}
913 
914 	ret = iio_device_register(indio_dev);
915 	if (ret < 0) {
916 		dev_err(dev, "unable to register iio device\n");
917 		goto err_buffer_cleanup;
918 	}
919 
920 	return 0;
921 
922 err_buffer_cleanup:
923 	iio_triggered_buffer_cleanup(indio_dev);
924 err_trigger_unregister:
925 	if (data->trig)
926 		iio_trigger_unregister(data->trig);
927 err_trigger_free:
928 	iio_trigger_free(data->trig);
929 err_chip_disable:
930 	data->part_info->chip_disable(data);
931 	regulator_disable(data->vddio_supply);
932 err_disable_vdd:
933 	regulator_disable(data->vdd_supply);
934 
935 	return ret;
936 }
937 
938 static int bma180_remove(struct i2c_client *client)
939 {
940 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
941 	struct bma180_data *data = iio_priv(indio_dev);
942 
943 	iio_device_unregister(indio_dev);
944 	iio_triggered_buffer_cleanup(indio_dev);
945 	if (data->trig) {
946 		iio_trigger_unregister(data->trig);
947 		iio_trigger_free(data->trig);
948 	}
949 
950 	mutex_lock(&data->mutex);
951 	data->part_info->chip_disable(data);
952 	mutex_unlock(&data->mutex);
953 	regulator_disable(data->vddio_supply);
954 	regulator_disable(data->vdd_supply);
955 
956 	return 0;
957 }
958 
959 #ifdef CONFIG_PM_SLEEP
960 static int bma180_suspend(struct device *dev)
961 {
962 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
963 	struct bma180_data *data = iio_priv(indio_dev);
964 	int ret;
965 
966 	mutex_lock(&data->mutex);
967 	ret = bma180_set_sleep_state(data, true);
968 	mutex_unlock(&data->mutex);
969 
970 	return ret;
971 }
972 
973 static int bma180_resume(struct device *dev)
974 {
975 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
976 	struct bma180_data *data = iio_priv(indio_dev);
977 	int ret;
978 
979 	mutex_lock(&data->mutex);
980 	ret = bma180_set_sleep_state(data, false);
981 	mutex_unlock(&data->mutex);
982 
983 	return ret;
984 }
985 
986 static SIMPLE_DEV_PM_OPS(bma180_pm_ops, bma180_suspend, bma180_resume);
987 #define BMA180_PM_OPS (&bma180_pm_ops)
988 #else
989 #define BMA180_PM_OPS NULL
990 #endif
991 
992 static const struct i2c_device_id bma180_ids[] = {
993 	{ "bma180", BMA180 },
994 	{ "bma250", BMA250 },
995 	{ "bma254", BMA254 },
996 	{ }
997 };
998 
999 MODULE_DEVICE_TABLE(i2c, bma180_ids);
1000 
1001 static const struct of_device_id bma180_of_match[] = {
1002 	{
1003 		.compatible = "bosch,bma180",
1004 		.data = (void *)BMA180
1005 	},
1006 	{
1007 		.compatible = "bosch,bma250",
1008 		.data = (void *)BMA250
1009 	},
1010 	{
1011 		.compatible = "bosch,bma254",
1012 		.data = (void *)BMA254
1013 	},
1014 	{ }
1015 };
1016 MODULE_DEVICE_TABLE(of, bma180_of_match);
1017 
1018 static struct i2c_driver bma180_driver = {
1019 	.driver = {
1020 		.name	= "bma180",
1021 		.pm	= BMA180_PM_OPS,
1022 		.of_match_table = bma180_of_match,
1023 	},
1024 	.probe		= bma180_probe,
1025 	.remove		= bma180_remove,
1026 	.id_table	= bma180_ids,
1027 };
1028 
1029 module_i2c_driver(bma180_driver);
1030 
1031 MODULE_AUTHOR("Kravchenko Oleksandr <x0199363@ti.com>");
1032 MODULE_AUTHOR("Texas Instruments, Inc.");
1033 MODULE_DESCRIPTION("Bosch BMA180/BMA25x triaxial acceleration sensor");
1034 MODULE_LICENSE("GPL");
1035