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