xref: /linux/drivers/iio/accel/bmc150-accel-core.c (revision 46e6acfe3501fa938af9c5bd730f0020235b08a2)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * 3-axis accelerometer driver supporting many Bosch-Sensortec chips
4  * Copyright (c) 2014, Intel Corporation.
5  */
6 
7 #include <linux/module.h>
8 #include <linux/i2c.h>
9 #include <linux/interrupt.h>
10 #include <linux/delay.h>
11 #include <linux/slab.h>
12 #include <linux/acpi.h>
13 #include <linux/of_irq.h>
14 #include <linux/pm.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/iio/iio.h>
17 #include <linux/iio/sysfs.h>
18 #include <linux/iio/buffer.h>
19 #include <linux/iio/events.h>
20 #include <linux/iio/trigger.h>
21 #include <linux/iio/trigger_consumer.h>
22 #include <linux/iio/triggered_buffer.h>
23 #include <linux/regmap.h>
24 #include <linux/regulator/consumer.h>
25 
26 #include "bmc150-accel.h"
27 
28 #define BMC150_ACCEL_DRV_NAME			"bmc150_accel"
29 #define BMC150_ACCEL_IRQ_NAME			"bmc150_accel_event"
30 
31 #define BMC150_ACCEL_REG_CHIP_ID		0x00
32 
33 #define BMC150_ACCEL_REG_INT_STATUS_2		0x0B
34 #define BMC150_ACCEL_ANY_MOTION_MASK		0x07
35 #define BMC150_ACCEL_ANY_MOTION_BIT_X		BIT(0)
36 #define BMC150_ACCEL_ANY_MOTION_BIT_Y		BIT(1)
37 #define BMC150_ACCEL_ANY_MOTION_BIT_Z		BIT(2)
38 #define BMC150_ACCEL_ANY_MOTION_BIT_SIGN	BIT(3)
39 
40 #define BMC150_ACCEL_REG_PMU_LPW		0x11
41 #define BMC150_ACCEL_PMU_MODE_MASK		0xE0
42 #define BMC150_ACCEL_PMU_MODE_SHIFT		5
43 #define BMC150_ACCEL_PMU_BIT_SLEEP_DUR_MASK	0x17
44 #define BMC150_ACCEL_PMU_BIT_SLEEP_DUR_SHIFT	1
45 
46 #define BMC150_ACCEL_REG_PMU_RANGE		0x0F
47 
48 #define BMC150_ACCEL_DEF_RANGE_2G		0x03
49 #define BMC150_ACCEL_DEF_RANGE_4G		0x05
50 #define BMC150_ACCEL_DEF_RANGE_8G		0x08
51 #define BMC150_ACCEL_DEF_RANGE_16G		0x0C
52 
53 /* Default BW: 125Hz */
54 #define BMC150_ACCEL_REG_PMU_BW		0x10
55 #define BMC150_ACCEL_DEF_BW			125
56 
57 #define BMC150_ACCEL_REG_RESET			0x14
58 #define BMC150_ACCEL_RESET_VAL			0xB6
59 
60 #define BMC150_ACCEL_REG_INT_MAP_0		0x19
61 #define BMC150_ACCEL_INT_MAP_0_BIT_INT1_SLOPE	BIT(2)
62 
63 #define BMC150_ACCEL_REG_INT_MAP_1		0x1A
64 #define BMC150_ACCEL_INT_MAP_1_BIT_INT1_DATA	BIT(0)
65 #define BMC150_ACCEL_INT_MAP_1_BIT_INT1_FWM	BIT(1)
66 #define BMC150_ACCEL_INT_MAP_1_BIT_INT1_FFULL	BIT(2)
67 #define BMC150_ACCEL_INT_MAP_1_BIT_INT2_FFULL	BIT(5)
68 #define BMC150_ACCEL_INT_MAP_1_BIT_INT2_FWM	BIT(6)
69 #define BMC150_ACCEL_INT_MAP_1_BIT_INT2_DATA	BIT(7)
70 
71 #define BMC150_ACCEL_REG_INT_MAP_2		0x1B
72 #define BMC150_ACCEL_INT_MAP_2_BIT_INT2_SLOPE	BIT(2)
73 
74 #define BMC150_ACCEL_REG_INT_RST_LATCH		0x21
75 #define BMC150_ACCEL_INT_MODE_LATCH_RESET	0x80
76 #define BMC150_ACCEL_INT_MODE_LATCH_INT	0x0F
77 #define BMC150_ACCEL_INT_MODE_NON_LATCH_INT	0x00
78 
79 #define BMC150_ACCEL_REG_INT_EN_0		0x16
80 #define BMC150_ACCEL_INT_EN_BIT_SLP_X		BIT(0)
81 #define BMC150_ACCEL_INT_EN_BIT_SLP_Y		BIT(1)
82 #define BMC150_ACCEL_INT_EN_BIT_SLP_Z		BIT(2)
83 
84 #define BMC150_ACCEL_REG_INT_EN_1		0x17
85 #define BMC150_ACCEL_INT_EN_BIT_DATA_EN		BIT(4)
86 #define BMC150_ACCEL_INT_EN_BIT_FFULL_EN	BIT(5)
87 #define BMC150_ACCEL_INT_EN_BIT_FWM_EN		BIT(6)
88 
89 #define BMC150_ACCEL_REG_INT_OUT_CTRL		0x20
90 #define BMC150_ACCEL_INT_OUT_CTRL_INT1_LVL	BIT(0)
91 #define BMC150_ACCEL_INT_OUT_CTRL_INT2_LVL	BIT(2)
92 
93 #define BMC150_ACCEL_REG_INT_5			0x27
94 #define BMC150_ACCEL_SLOPE_DUR_MASK		0x03
95 
96 #define BMC150_ACCEL_REG_INT_6			0x28
97 #define BMC150_ACCEL_SLOPE_THRES_MASK		0xFF
98 
99 /* Slope duration in terms of number of samples */
100 #define BMC150_ACCEL_DEF_SLOPE_DURATION		1
101 /* in terms of multiples of g's/LSB, based on range */
102 #define BMC150_ACCEL_DEF_SLOPE_THRESHOLD	1
103 
104 #define BMC150_ACCEL_REG_XOUT_L		0x02
105 
106 #define BMC150_ACCEL_MAX_STARTUP_TIME_MS	100
107 
108 /* Sleep Duration values */
109 #define BMC150_ACCEL_SLEEP_500_MICRO		0x05
110 #define BMC150_ACCEL_SLEEP_1_MS		0x06
111 #define BMC150_ACCEL_SLEEP_2_MS		0x07
112 #define BMC150_ACCEL_SLEEP_4_MS		0x08
113 #define BMC150_ACCEL_SLEEP_6_MS		0x09
114 #define BMC150_ACCEL_SLEEP_10_MS		0x0A
115 #define BMC150_ACCEL_SLEEP_25_MS		0x0B
116 #define BMC150_ACCEL_SLEEP_50_MS		0x0C
117 #define BMC150_ACCEL_SLEEP_100_MS		0x0D
118 #define BMC150_ACCEL_SLEEP_500_MS		0x0E
119 #define BMC150_ACCEL_SLEEP_1_SEC		0x0F
120 
121 #define BMC150_ACCEL_REG_TEMP			0x08
122 #define BMC150_ACCEL_TEMP_CENTER_VAL		23
123 
124 #define BMC150_ACCEL_AXIS_TO_REG(axis)	(BMC150_ACCEL_REG_XOUT_L + (axis * 2))
125 #define BMC150_AUTO_SUSPEND_DELAY_MS		2000
126 
127 #define BMC150_ACCEL_REG_FIFO_STATUS		0x0E
128 #define BMC150_ACCEL_REG_FIFO_CONFIG0		0x30
129 #define BMC150_ACCEL_REG_FIFO_CONFIG1		0x3E
130 #define BMC150_ACCEL_REG_FIFO_DATA		0x3F
131 #define BMC150_ACCEL_FIFO_LENGTH		32
132 
133 enum bmc150_accel_axis {
134 	AXIS_X,
135 	AXIS_Y,
136 	AXIS_Z,
137 	AXIS_MAX,
138 };
139 
140 enum bmc150_power_modes {
141 	BMC150_ACCEL_SLEEP_MODE_NORMAL,
142 	BMC150_ACCEL_SLEEP_MODE_DEEP_SUSPEND,
143 	BMC150_ACCEL_SLEEP_MODE_LPM,
144 	BMC150_ACCEL_SLEEP_MODE_SUSPEND = 0x04,
145 };
146 
147 struct bmc150_scale_info {
148 	int scale;
149 	u8 reg_range;
150 };
151 
152 struct bmc150_accel_chip_info {
153 	const char *name;
154 	u8 chip_id;
155 	const struct iio_chan_spec *channels;
156 	int num_channels;
157 	const struct bmc150_scale_info scale_table[4];
158 };
159 
160 static const struct {
161 	int val;
162 	int val2;
163 	u8 bw_bits;
164 } bmc150_accel_samp_freq_table[] = { {15, 620000, 0x08},
165 				     {31, 260000, 0x09},
166 				     {62, 500000, 0x0A},
167 				     {125, 0, 0x0B},
168 				     {250, 0, 0x0C},
169 				     {500, 0, 0x0D},
170 				     {1000, 0, 0x0E},
171 				     {2000, 0, 0x0F} };
172 
173 static __maybe_unused const struct {
174 	int bw_bits;
175 	int msec;
176 } bmc150_accel_sample_upd_time[] = { {0x08, 64},
177 				     {0x09, 32},
178 				     {0x0A, 16},
179 				     {0x0B, 8},
180 				     {0x0C, 4},
181 				     {0x0D, 2},
182 				     {0x0E, 1},
183 				     {0x0F, 1} };
184 
185 static const struct {
186 	int sleep_dur;
187 	u8 reg_value;
188 } bmc150_accel_sleep_value_table[] = { {0, 0},
189 				       {500, BMC150_ACCEL_SLEEP_500_MICRO},
190 				       {1000, BMC150_ACCEL_SLEEP_1_MS},
191 				       {2000, BMC150_ACCEL_SLEEP_2_MS},
192 				       {4000, BMC150_ACCEL_SLEEP_4_MS},
193 				       {6000, BMC150_ACCEL_SLEEP_6_MS},
194 				       {10000, BMC150_ACCEL_SLEEP_10_MS},
195 				       {25000, BMC150_ACCEL_SLEEP_25_MS},
196 				       {50000, BMC150_ACCEL_SLEEP_50_MS},
197 				       {100000, BMC150_ACCEL_SLEEP_100_MS},
198 				       {500000, BMC150_ACCEL_SLEEP_500_MS},
199 				       {1000000, BMC150_ACCEL_SLEEP_1_SEC} };
200 
201 const struct regmap_config bmc150_regmap_conf = {
202 	.reg_bits = 8,
203 	.val_bits = 8,
204 	.max_register = 0x3f,
205 };
206 EXPORT_SYMBOL_NS_GPL(bmc150_regmap_conf, IIO_BMC150);
207 
208 static int bmc150_accel_set_mode(struct bmc150_accel_data *data,
209 				 enum bmc150_power_modes mode,
210 				 int dur_us)
211 {
212 	struct device *dev = regmap_get_device(data->regmap);
213 	int i;
214 	int ret;
215 	u8 lpw_bits;
216 	int dur_val = -1;
217 
218 	if (dur_us > 0) {
219 		for (i = 0; i < ARRAY_SIZE(bmc150_accel_sleep_value_table);
220 									 ++i) {
221 			if (bmc150_accel_sleep_value_table[i].sleep_dur ==
222 									dur_us)
223 				dur_val =
224 				bmc150_accel_sleep_value_table[i].reg_value;
225 		}
226 	} else {
227 		dur_val = 0;
228 	}
229 
230 	if (dur_val < 0)
231 		return -EINVAL;
232 
233 	lpw_bits = mode << BMC150_ACCEL_PMU_MODE_SHIFT;
234 	lpw_bits |= (dur_val << BMC150_ACCEL_PMU_BIT_SLEEP_DUR_SHIFT);
235 
236 	dev_dbg(dev, "Set Mode bits %x\n", lpw_bits);
237 
238 	ret = regmap_write(data->regmap, BMC150_ACCEL_REG_PMU_LPW, lpw_bits);
239 	if (ret < 0) {
240 		dev_err(dev, "Error writing reg_pmu_lpw\n");
241 		return ret;
242 	}
243 
244 	return 0;
245 }
246 
247 static int bmc150_accel_set_bw(struct bmc150_accel_data *data, int val,
248 			       int val2)
249 {
250 	int i;
251 	int ret;
252 
253 	for (i = 0; i < ARRAY_SIZE(bmc150_accel_samp_freq_table); ++i) {
254 		if (bmc150_accel_samp_freq_table[i].val == val &&
255 		    bmc150_accel_samp_freq_table[i].val2 == val2) {
256 			ret = regmap_write(data->regmap,
257 				BMC150_ACCEL_REG_PMU_BW,
258 				bmc150_accel_samp_freq_table[i].bw_bits);
259 			if (ret < 0)
260 				return ret;
261 
262 			data->bw_bits =
263 				bmc150_accel_samp_freq_table[i].bw_bits;
264 			return 0;
265 		}
266 	}
267 
268 	return -EINVAL;
269 }
270 
271 static int bmc150_accel_update_slope(struct bmc150_accel_data *data)
272 {
273 	struct device *dev = regmap_get_device(data->regmap);
274 	int ret;
275 
276 	ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_6,
277 					data->slope_thres);
278 	if (ret < 0) {
279 		dev_err(dev, "Error writing reg_int_6\n");
280 		return ret;
281 	}
282 
283 	ret = regmap_update_bits(data->regmap, BMC150_ACCEL_REG_INT_5,
284 				 BMC150_ACCEL_SLOPE_DUR_MASK, data->slope_dur);
285 	if (ret < 0) {
286 		dev_err(dev, "Error updating reg_int_5\n");
287 		return ret;
288 	}
289 
290 	dev_dbg(dev, "%x %x\n", data->slope_thres, data->slope_dur);
291 
292 	return ret;
293 }
294 
295 static int bmc150_accel_any_motion_setup(struct bmc150_accel_trigger *t,
296 					 bool state)
297 {
298 	if (state)
299 		return bmc150_accel_update_slope(t->data);
300 
301 	return 0;
302 }
303 
304 static int bmc150_accel_get_bw(struct bmc150_accel_data *data, int *val,
305 			       int *val2)
306 {
307 	int i;
308 
309 	for (i = 0; i < ARRAY_SIZE(bmc150_accel_samp_freq_table); ++i) {
310 		if (bmc150_accel_samp_freq_table[i].bw_bits == data->bw_bits) {
311 			*val = bmc150_accel_samp_freq_table[i].val;
312 			*val2 = bmc150_accel_samp_freq_table[i].val2;
313 			return IIO_VAL_INT_PLUS_MICRO;
314 		}
315 	}
316 
317 	return -EINVAL;
318 }
319 
320 #ifdef CONFIG_PM
321 static int bmc150_accel_get_startup_times(struct bmc150_accel_data *data)
322 {
323 	int i;
324 
325 	for (i = 0; i < ARRAY_SIZE(bmc150_accel_sample_upd_time); ++i) {
326 		if (bmc150_accel_sample_upd_time[i].bw_bits == data->bw_bits)
327 			return bmc150_accel_sample_upd_time[i].msec;
328 	}
329 
330 	return BMC150_ACCEL_MAX_STARTUP_TIME_MS;
331 }
332 
333 static int bmc150_accel_set_power_state(struct bmc150_accel_data *data, bool on)
334 {
335 	struct device *dev = regmap_get_device(data->regmap);
336 	int ret;
337 
338 	if (on) {
339 		ret = pm_runtime_resume_and_get(dev);
340 	} else {
341 		pm_runtime_mark_last_busy(dev);
342 		ret = pm_runtime_put_autosuspend(dev);
343 	}
344 
345 	if (ret < 0) {
346 		dev_err(dev,
347 			"Failed: %s for %d\n", __func__, on);
348 		return ret;
349 	}
350 
351 	return 0;
352 }
353 #else
354 static int bmc150_accel_set_power_state(struct bmc150_accel_data *data, bool on)
355 {
356 	return 0;
357 }
358 #endif
359 
360 #ifdef CONFIG_ACPI
361 /*
362  * Support for getting accelerometer information from BOSC0200 ACPI nodes.
363  *
364  * There are 2 variants of the BOSC0200 ACPI node. Some 2-in-1s with 360 degree
365  * hinges declare 2 I2C ACPI-resources for 2 accelerometers, 1 in the display
366  * and 1 in the base of the 2-in-1. On these 2-in-1s the ROMS ACPI object
367  * contains the mount-matrix for the sensor in the display and ROMK contains
368  * the mount-matrix for the sensor in the base. On devices using a single
369  * sensor there is a ROTM ACPI object which contains the mount-matrix.
370  *
371  * Here is an incomplete list of devices known to use 1 of these setups:
372  *
373  * Yoga devices with 2 accelerometers using ROMS + ROMK for the mount-matrices:
374  * Lenovo Thinkpad Yoga 11e 3th gen
375  * Lenovo Thinkpad Yoga 11e 4th gen
376  *
377  * Tablets using a single accelerometer using ROTM for the mount-matrix:
378  * Chuwi Hi8 Pro (CWI513)
379  * Chuwi Vi8 Plus (CWI519)
380  * Chuwi Hi13
381  * Irbis TW90
382  * Jumper EZpad mini 3
383  * Onda V80 plus
384  * Predia Basic Tablet
385  */
386 static bool bmc150_apply_bosc0200_acpi_orientation(struct device *dev,
387 						   struct iio_mount_matrix *orientation)
388 {
389 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
390 	struct acpi_device *adev = ACPI_COMPANION(dev);
391 	char *name, *alt_name, *label;
392 
393 	if (strcmp(dev_name(dev), "i2c-BOSC0200:base") == 0) {
394 		alt_name = "ROMK";
395 		label = "accel-base";
396 	} else {
397 		alt_name = "ROMS";
398 		label = "accel-display";
399 	}
400 
401 	if (acpi_has_method(adev->handle, "ROTM")) {
402 		name = "ROTM";
403 	} else if (acpi_has_method(adev->handle, alt_name)) {
404 		name = alt_name;
405 		indio_dev->label = label;
406 	} else {
407 		return false;
408 	}
409 
410 	return iio_read_acpi_mount_matrix(dev, orientation, name);
411 }
412 
413 static bool bmc150_apply_dual250e_acpi_orientation(struct device *dev,
414 						   struct iio_mount_matrix *orientation)
415 {
416 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
417 
418 	if (strcmp(dev_name(dev), "i2c-DUAL250E:base") == 0)
419 		indio_dev->label = "accel-base";
420 	else
421 		indio_dev->label = "accel-display";
422 
423 	return false; /* DUAL250E fwnodes have no mount matrix info */
424 }
425 
426 static bool bmc150_apply_acpi_orientation(struct device *dev,
427 					  struct iio_mount_matrix *orientation)
428 {
429 	struct acpi_device *adev = ACPI_COMPANION(dev);
430 
431 	if (adev && acpi_dev_hid_uid_match(adev, "BOSC0200", NULL))
432 		return bmc150_apply_bosc0200_acpi_orientation(dev, orientation);
433 
434 	if (adev && acpi_dev_hid_uid_match(adev, "DUAL250E", NULL))
435 		return bmc150_apply_dual250e_acpi_orientation(dev, orientation);
436 
437 	return false;
438 }
439 #else
440 static bool bmc150_apply_acpi_orientation(struct device *dev,
441 					  struct iio_mount_matrix *orientation)
442 {
443 	return false;
444 }
445 #endif
446 
447 struct bmc150_accel_interrupt_info {
448 	u8 map_reg;
449 	u8 map_bitmask;
450 	u8 en_reg;
451 	u8 en_bitmask;
452 };
453 
454 static const struct bmc150_accel_interrupt_info
455 bmc150_accel_interrupts_int1[BMC150_ACCEL_INTERRUPTS] = {
456 	{ /* data ready interrupt */
457 		.map_reg = BMC150_ACCEL_REG_INT_MAP_1,
458 		.map_bitmask = BMC150_ACCEL_INT_MAP_1_BIT_INT1_DATA,
459 		.en_reg = BMC150_ACCEL_REG_INT_EN_1,
460 		.en_bitmask = BMC150_ACCEL_INT_EN_BIT_DATA_EN,
461 	},
462 	{  /* motion interrupt */
463 		.map_reg = BMC150_ACCEL_REG_INT_MAP_0,
464 		.map_bitmask = BMC150_ACCEL_INT_MAP_0_BIT_INT1_SLOPE,
465 		.en_reg = BMC150_ACCEL_REG_INT_EN_0,
466 		.en_bitmask =  BMC150_ACCEL_INT_EN_BIT_SLP_X |
467 			BMC150_ACCEL_INT_EN_BIT_SLP_Y |
468 			BMC150_ACCEL_INT_EN_BIT_SLP_Z
469 	},
470 	{ /* fifo watermark interrupt */
471 		.map_reg = BMC150_ACCEL_REG_INT_MAP_1,
472 		.map_bitmask = BMC150_ACCEL_INT_MAP_1_BIT_INT1_FWM,
473 		.en_reg = BMC150_ACCEL_REG_INT_EN_1,
474 		.en_bitmask = BMC150_ACCEL_INT_EN_BIT_FWM_EN,
475 	},
476 };
477 
478 static const struct bmc150_accel_interrupt_info
479 bmc150_accel_interrupts_int2[BMC150_ACCEL_INTERRUPTS] = {
480 	{ /* data ready interrupt */
481 		.map_reg = BMC150_ACCEL_REG_INT_MAP_1,
482 		.map_bitmask = BMC150_ACCEL_INT_MAP_1_BIT_INT2_DATA,
483 		.en_reg = BMC150_ACCEL_REG_INT_EN_1,
484 		.en_bitmask = BMC150_ACCEL_INT_EN_BIT_DATA_EN,
485 	},
486 	{  /* motion interrupt */
487 		.map_reg = BMC150_ACCEL_REG_INT_MAP_2,
488 		.map_bitmask = BMC150_ACCEL_INT_MAP_2_BIT_INT2_SLOPE,
489 		.en_reg = BMC150_ACCEL_REG_INT_EN_0,
490 		.en_bitmask =  BMC150_ACCEL_INT_EN_BIT_SLP_X |
491 			BMC150_ACCEL_INT_EN_BIT_SLP_Y |
492 			BMC150_ACCEL_INT_EN_BIT_SLP_Z
493 	},
494 	{ /* fifo watermark interrupt */
495 		.map_reg = BMC150_ACCEL_REG_INT_MAP_1,
496 		.map_bitmask = BMC150_ACCEL_INT_MAP_1_BIT_INT2_FWM,
497 		.en_reg = BMC150_ACCEL_REG_INT_EN_1,
498 		.en_bitmask = BMC150_ACCEL_INT_EN_BIT_FWM_EN,
499 	},
500 };
501 
502 static void bmc150_accel_interrupts_setup(struct iio_dev *indio_dev,
503 					  struct bmc150_accel_data *data, int irq)
504 {
505 	const struct bmc150_accel_interrupt_info *irq_info = NULL;
506 	struct device *dev = regmap_get_device(data->regmap);
507 	int i;
508 
509 	/*
510 	 * For now we map all interrupts to the same output pin.
511 	 * However, some boards may have just INT2 (and not INT1) connected,
512 	 * so we try to detect which IRQ it is based on the interrupt-names.
513 	 * Without interrupt-names, we assume the irq belongs to INT1.
514 	 */
515 	irq_info = bmc150_accel_interrupts_int1;
516 	if (data->type == BOSCH_BMC156 ||
517 	    irq == of_irq_get_byname(dev->of_node, "INT2"))
518 		irq_info = bmc150_accel_interrupts_int2;
519 
520 	for (i = 0; i < BMC150_ACCEL_INTERRUPTS; i++)
521 		data->interrupts[i].info = &irq_info[i];
522 }
523 
524 static int bmc150_accel_set_interrupt(struct bmc150_accel_data *data, int i,
525 				      bool state)
526 {
527 	struct device *dev = regmap_get_device(data->regmap);
528 	struct bmc150_accel_interrupt *intr = &data->interrupts[i];
529 	const struct bmc150_accel_interrupt_info *info = intr->info;
530 	int ret;
531 
532 	if (state) {
533 		if (atomic_inc_return(&intr->users) > 1)
534 			return 0;
535 	} else {
536 		if (atomic_dec_return(&intr->users) > 0)
537 			return 0;
538 	}
539 
540 	/*
541 	 * We will expect the enable and disable to do operation in reverse
542 	 * order. This will happen here anyway, as our resume operation uses
543 	 * sync mode runtime pm calls. The suspend operation will be delayed
544 	 * by autosuspend delay.
545 	 * So the disable operation will still happen in reverse order of
546 	 * enable operation. When runtime pm is disabled the mode is always on,
547 	 * so sequence doesn't matter.
548 	 */
549 	ret = bmc150_accel_set_power_state(data, state);
550 	if (ret < 0)
551 		return ret;
552 
553 	/* map the interrupt to the appropriate pins */
554 	ret = regmap_update_bits(data->regmap, info->map_reg, info->map_bitmask,
555 				 (state ? info->map_bitmask : 0));
556 	if (ret < 0) {
557 		dev_err(dev, "Error updating reg_int_map\n");
558 		goto out_fix_power_state;
559 	}
560 
561 	/* enable/disable the interrupt */
562 	ret = regmap_update_bits(data->regmap, info->en_reg, info->en_bitmask,
563 				 (state ? info->en_bitmask : 0));
564 	if (ret < 0) {
565 		dev_err(dev, "Error updating reg_int_en\n");
566 		goto out_fix_power_state;
567 	}
568 
569 	return 0;
570 
571 out_fix_power_state:
572 	bmc150_accel_set_power_state(data, false);
573 	return ret;
574 }
575 
576 static int bmc150_accel_set_scale(struct bmc150_accel_data *data, int val)
577 {
578 	struct device *dev = regmap_get_device(data->regmap);
579 	int ret, i;
580 
581 	for (i = 0; i < ARRAY_SIZE(data->chip_info->scale_table); ++i) {
582 		if (data->chip_info->scale_table[i].scale == val) {
583 			ret = regmap_write(data->regmap,
584 				     BMC150_ACCEL_REG_PMU_RANGE,
585 				     data->chip_info->scale_table[i].reg_range);
586 			if (ret < 0) {
587 				dev_err(dev, "Error writing pmu_range\n");
588 				return ret;
589 			}
590 
591 			data->range = data->chip_info->scale_table[i].reg_range;
592 			return 0;
593 		}
594 	}
595 
596 	return -EINVAL;
597 }
598 
599 static int bmc150_accel_get_temp(struct bmc150_accel_data *data, int *val)
600 {
601 	struct device *dev = regmap_get_device(data->regmap);
602 	int ret;
603 	unsigned int value;
604 
605 	mutex_lock(&data->mutex);
606 
607 	ret = regmap_read(data->regmap, BMC150_ACCEL_REG_TEMP, &value);
608 	if (ret < 0) {
609 		dev_err(dev, "Error reading reg_temp\n");
610 		mutex_unlock(&data->mutex);
611 		return ret;
612 	}
613 	*val = sign_extend32(value, 7);
614 
615 	mutex_unlock(&data->mutex);
616 
617 	return IIO_VAL_INT;
618 }
619 
620 static int bmc150_accel_get_axis(struct bmc150_accel_data *data,
621 				 struct iio_chan_spec const *chan,
622 				 int *val)
623 {
624 	struct device *dev = regmap_get_device(data->regmap);
625 	int ret;
626 	int axis = chan->scan_index;
627 	__le16 raw_val;
628 
629 	mutex_lock(&data->mutex);
630 	ret = bmc150_accel_set_power_state(data, true);
631 	if (ret < 0) {
632 		mutex_unlock(&data->mutex);
633 		return ret;
634 	}
635 
636 	ret = regmap_bulk_read(data->regmap, BMC150_ACCEL_AXIS_TO_REG(axis),
637 			       &raw_val, sizeof(raw_val));
638 	if (ret < 0) {
639 		dev_err(dev, "Error reading axis %d\n", axis);
640 		bmc150_accel_set_power_state(data, false);
641 		mutex_unlock(&data->mutex);
642 		return ret;
643 	}
644 	*val = sign_extend32(le16_to_cpu(raw_val) >> chan->scan_type.shift,
645 			     chan->scan_type.realbits - 1);
646 	ret = bmc150_accel_set_power_state(data, false);
647 	mutex_unlock(&data->mutex);
648 	if (ret < 0)
649 		return ret;
650 
651 	return IIO_VAL_INT;
652 }
653 
654 static int bmc150_accel_read_raw(struct iio_dev *indio_dev,
655 				 struct iio_chan_spec const *chan,
656 				 int *val, int *val2, long mask)
657 {
658 	struct bmc150_accel_data *data = iio_priv(indio_dev);
659 	int ret;
660 
661 	switch (mask) {
662 	case IIO_CHAN_INFO_RAW:
663 		switch (chan->type) {
664 		case IIO_TEMP:
665 			return bmc150_accel_get_temp(data, val);
666 		case IIO_ACCEL:
667 			if (iio_buffer_enabled(indio_dev))
668 				return -EBUSY;
669 			else
670 				return bmc150_accel_get_axis(data, chan, val);
671 		default:
672 			return -EINVAL;
673 		}
674 	case IIO_CHAN_INFO_OFFSET:
675 		if (chan->type == IIO_TEMP) {
676 			*val = BMC150_ACCEL_TEMP_CENTER_VAL;
677 			return IIO_VAL_INT;
678 		} else {
679 			return -EINVAL;
680 		}
681 	case IIO_CHAN_INFO_SCALE:
682 		*val = 0;
683 		switch (chan->type) {
684 		case IIO_TEMP:
685 			*val2 = 500000;
686 			return IIO_VAL_INT_PLUS_MICRO;
687 		case IIO_ACCEL:
688 		{
689 			int i;
690 			const struct bmc150_scale_info *si;
691 			int st_size = ARRAY_SIZE(data->chip_info->scale_table);
692 
693 			for (i = 0; i < st_size; ++i) {
694 				si = &data->chip_info->scale_table[i];
695 				if (si->reg_range == data->range) {
696 					*val2 = si->scale;
697 					return IIO_VAL_INT_PLUS_MICRO;
698 				}
699 			}
700 			return -EINVAL;
701 		}
702 		default:
703 			return -EINVAL;
704 		}
705 	case IIO_CHAN_INFO_SAMP_FREQ:
706 		mutex_lock(&data->mutex);
707 		ret = bmc150_accel_get_bw(data, val, val2);
708 		mutex_unlock(&data->mutex);
709 		return ret;
710 	default:
711 		return -EINVAL;
712 	}
713 }
714 
715 static int bmc150_accel_write_raw(struct iio_dev *indio_dev,
716 				  struct iio_chan_spec const *chan,
717 				  int val, int val2, long mask)
718 {
719 	struct bmc150_accel_data *data = iio_priv(indio_dev);
720 	int ret;
721 
722 	switch (mask) {
723 	case IIO_CHAN_INFO_SAMP_FREQ:
724 		mutex_lock(&data->mutex);
725 		ret = bmc150_accel_set_bw(data, val, val2);
726 		mutex_unlock(&data->mutex);
727 		break;
728 	case IIO_CHAN_INFO_SCALE:
729 		if (val)
730 			return -EINVAL;
731 
732 		mutex_lock(&data->mutex);
733 		ret = bmc150_accel_set_scale(data, val2);
734 		mutex_unlock(&data->mutex);
735 		return ret;
736 	default:
737 		ret = -EINVAL;
738 	}
739 
740 	return ret;
741 }
742 
743 static int bmc150_accel_read_event(struct iio_dev *indio_dev,
744 				   const struct iio_chan_spec *chan,
745 				   enum iio_event_type type,
746 				   enum iio_event_direction dir,
747 				   enum iio_event_info info,
748 				   int *val, int *val2)
749 {
750 	struct bmc150_accel_data *data = iio_priv(indio_dev);
751 
752 	*val2 = 0;
753 	switch (info) {
754 	case IIO_EV_INFO_VALUE:
755 		*val = data->slope_thres;
756 		break;
757 	case IIO_EV_INFO_PERIOD:
758 		*val = data->slope_dur;
759 		break;
760 	default:
761 		return -EINVAL;
762 	}
763 
764 	return IIO_VAL_INT;
765 }
766 
767 static int bmc150_accel_write_event(struct iio_dev *indio_dev,
768 				    const struct iio_chan_spec *chan,
769 				    enum iio_event_type type,
770 				    enum iio_event_direction dir,
771 				    enum iio_event_info info,
772 				    int val, int val2)
773 {
774 	struct bmc150_accel_data *data = iio_priv(indio_dev);
775 
776 	if (data->ev_enable_state)
777 		return -EBUSY;
778 
779 	switch (info) {
780 	case IIO_EV_INFO_VALUE:
781 		data->slope_thres = val & BMC150_ACCEL_SLOPE_THRES_MASK;
782 		break;
783 	case IIO_EV_INFO_PERIOD:
784 		data->slope_dur = val & BMC150_ACCEL_SLOPE_DUR_MASK;
785 		break;
786 	default:
787 		return -EINVAL;
788 	}
789 
790 	return 0;
791 }
792 
793 static int bmc150_accel_read_event_config(struct iio_dev *indio_dev,
794 					  const struct iio_chan_spec *chan,
795 					  enum iio_event_type type,
796 					  enum iio_event_direction dir)
797 {
798 	struct bmc150_accel_data *data = iio_priv(indio_dev);
799 
800 	return data->ev_enable_state;
801 }
802 
803 static int bmc150_accel_write_event_config(struct iio_dev *indio_dev,
804 					   const struct iio_chan_spec *chan,
805 					   enum iio_event_type type,
806 					   enum iio_event_direction dir,
807 					   int state)
808 {
809 	struct bmc150_accel_data *data = iio_priv(indio_dev);
810 	int ret;
811 
812 	if (state == data->ev_enable_state)
813 		return 0;
814 
815 	mutex_lock(&data->mutex);
816 
817 	ret = bmc150_accel_set_interrupt(data, BMC150_ACCEL_INT_ANY_MOTION,
818 					 state);
819 	if (ret < 0) {
820 		mutex_unlock(&data->mutex);
821 		return ret;
822 	}
823 
824 	data->ev_enable_state = state;
825 	mutex_unlock(&data->mutex);
826 
827 	return 0;
828 }
829 
830 static int bmc150_accel_validate_trigger(struct iio_dev *indio_dev,
831 					 struct iio_trigger *trig)
832 {
833 	struct bmc150_accel_data *data = iio_priv(indio_dev);
834 	int i;
835 
836 	for (i = 0; i < BMC150_ACCEL_TRIGGERS; i++) {
837 		if (data->triggers[i].indio_trig == trig)
838 			return 0;
839 	}
840 
841 	return -EINVAL;
842 }
843 
844 static ssize_t bmc150_accel_get_fifo_watermark(struct device *dev,
845 					       struct device_attribute *attr,
846 					       char *buf)
847 {
848 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
849 	struct bmc150_accel_data *data = iio_priv(indio_dev);
850 	int wm;
851 
852 	mutex_lock(&data->mutex);
853 	wm = data->watermark;
854 	mutex_unlock(&data->mutex);
855 
856 	return sprintf(buf, "%d\n", wm);
857 }
858 
859 static ssize_t bmc150_accel_get_fifo_state(struct device *dev,
860 					   struct device_attribute *attr,
861 					   char *buf)
862 {
863 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
864 	struct bmc150_accel_data *data = iio_priv(indio_dev);
865 	bool state;
866 
867 	mutex_lock(&data->mutex);
868 	state = data->fifo_mode;
869 	mutex_unlock(&data->mutex);
870 
871 	return sprintf(buf, "%d\n", state);
872 }
873 
874 static const struct iio_mount_matrix *
875 bmc150_accel_get_mount_matrix(const struct iio_dev *indio_dev,
876 				const struct iio_chan_spec *chan)
877 {
878 	struct bmc150_accel_data *data = iio_priv(indio_dev);
879 
880 	return &data->orientation;
881 }
882 
883 static const struct iio_chan_spec_ext_info bmc150_accel_ext_info[] = {
884 	IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, bmc150_accel_get_mount_matrix),
885 	{ }
886 };
887 
888 IIO_STATIC_CONST_DEVICE_ATTR(hwfifo_watermark_min, "1");
889 IIO_STATIC_CONST_DEVICE_ATTR(hwfifo_watermark_max,
890 			     __stringify(BMC150_ACCEL_FIFO_LENGTH));
891 static IIO_DEVICE_ATTR(hwfifo_enabled, S_IRUGO,
892 		       bmc150_accel_get_fifo_state, NULL, 0);
893 static IIO_DEVICE_ATTR(hwfifo_watermark, S_IRUGO,
894 		       bmc150_accel_get_fifo_watermark, NULL, 0);
895 
896 static const struct iio_dev_attr *bmc150_accel_fifo_attributes[] = {
897 	&iio_dev_attr_hwfifo_watermark_min,
898 	&iio_dev_attr_hwfifo_watermark_max,
899 	&iio_dev_attr_hwfifo_watermark,
900 	&iio_dev_attr_hwfifo_enabled,
901 	NULL,
902 };
903 
904 static int bmc150_accel_set_watermark(struct iio_dev *indio_dev, unsigned val)
905 {
906 	struct bmc150_accel_data *data = iio_priv(indio_dev);
907 
908 	if (val > BMC150_ACCEL_FIFO_LENGTH)
909 		val = BMC150_ACCEL_FIFO_LENGTH;
910 
911 	mutex_lock(&data->mutex);
912 	data->watermark = val;
913 	mutex_unlock(&data->mutex);
914 
915 	return 0;
916 }
917 
918 /*
919  * We must read at least one full frame in one burst, otherwise the rest of the
920  * frame data is discarded.
921  */
922 static int bmc150_accel_fifo_transfer(struct bmc150_accel_data *data,
923 				      char *buffer, int samples)
924 {
925 	struct device *dev = regmap_get_device(data->regmap);
926 	int sample_length = 3 * 2;
927 	int ret;
928 	int total_length = samples * sample_length;
929 
930 	ret = regmap_raw_read(data->regmap, BMC150_ACCEL_REG_FIFO_DATA,
931 			      buffer, total_length);
932 	if (ret)
933 		dev_err(dev,
934 			"Error transferring data from fifo: %d\n", ret);
935 
936 	return ret;
937 }
938 
939 static int __bmc150_accel_fifo_flush(struct iio_dev *indio_dev,
940 				     unsigned samples, bool irq)
941 {
942 	struct bmc150_accel_data *data = iio_priv(indio_dev);
943 	struct device *dev = regmap_get_device(data->regmap);
944 	int ret, i;
945 	u8 count;
946 	u16 buffer[BMC150_ACCEL_FIFO_LENGTH * 3];
947 	int64_t tstamp;
948 	uint64_t sample_period;
949 	unsigned int val;
950 
951 	ret = regmap_read(data->regmap, BMC150_ACCEL_REG_FIFO_STATUS, &val);
952 	if (ret < 0) {
953 		dev_err(dev, "Error reading reg_fifo_status\n");
954 		return ret;
955 	}
956 
957 	count = val & 0x7F;
958 
959 	if (!count)
960 		return 0;
961 
962 	/*
963 	 * If we getting called from IRQ handler we know the stored timestamp is
964 	 * fairly accurate for the last stored sample. Otherwise, if we are
965 	 * called as a result of a read operation from userspace and hence
966 	 * before the watermark interrupt was triggered, take a timestamp
967 	 * now. We can fall anywhere in between two samples so the error in this
968 	 * case is at most one sample period.
969 	 */
970 	if (!irq) {
971 		data->old_timestamp = data->timestamp;
972 		data->timestamp = iio_get_time_ns(indio_dev);
973 	}
974 
975 	/*
976 	 * Approximate timestamps for each of the sample based on the sampling
977 	 * frequency, timestamp for last sample and number of samples.
978 	 *
979 	 * Note that we can't use the current bandwidth settings to compute the
980 	 * sample period because the sample rate varies with the device
981 	 * (e.g. between 31.70ms to 32.20ms for a bandwidth of 15.63HZ). That
982 	 * small variation adds when we store a large number of samples and
983 	 * creates significant jitter between the last and first samples in
984 	 * different batches (e.g. 32ms vs 21ms).
985 	 *
986 	 * To avoid this issue we compute the actual sample period ourselves
987 	 * based on the timestamp delta between the last two flush operations.
988 	 */
989 	sample_period = (data->timestamp - data->old_timestamp);
990 	do_div(sample_period, count);
991 	tstamp = data->timestamp - (count - 1) * sample_period;
992 
993 	if (samples && count > samples)
994 		count = samples;
995 
996 	ret = bmc150_accel_fifo_transfer(data, (u8 *)buffer, count);
997 	if (ret)
998 		return ret;
999 
1000 	/*
1001 	 * Ideally we want the IIO core to handle the demux when running in fifo
1002 	 * mode but not when running in triggered buffer mode. Unfortunately
1003 	 * this does not seem to be possible, so stick with driver demux for
1004 	 * now.
1005 	 */
1006 	for (i = 0; i < count; i++) {
1007 		int j, bit;
1008 
1009 		j = 0;
1010 		for_each_set_bit(bit, indio_dev->active_scan_mask,
1011 				 indio_dev->masklength)
1012 			memcpy(&data->scan.channels[j++], &buffer[i * 3 + bit],
1013 			       sizeof(data->scan.channels[0]));
1014 
1015 		iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
1016 						   tstamp);
1017 
1018 		tstamp += sample_period;
1019 	}
1020 
1021 	return count;
1022 }
1023 
1024 static int bmc150_accel_fifo_flush(struct iio_dev *indio_dev, unsigned samples)
1025 {
1026 	struct bmc150_accel_data *data = iio_priv(indio_dev);
1027 	int ret;
1028 
1029 	mutex_lock(&data->mutex);
1030 	ret = __bmc150_accel_fifo_flush(indio_dev, samples, false);
1031 	mutex_unlock(&data->mutex);
1032 
1033 	return ret;
1034 }
1035 
1036 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
1037 		"15.620000 31.260000 62.50000 125 250 500 1000 2000");
1038 
1039 static struct attribute *bmc150_accel_attributes[] = {
1040 	&iio_const_attr_sampling_frequency_available.dev_attr.attr,
1041 	NULL,
1042 };
1043 
1044 static const struct attribute_group bmc150_accel_attrs_group = {
1045 	.attrs = bmc150_accel_attributes,
1046 };
1047 
1048 static const struct iio_event_spec bmc150_accel_event = {
1049 		.type = IIO_EV_TYPE_ROC,
1050 		.dir = IIO_EV_DIR_EITHER,
1051 		.mask_separate = BIT(IIO_EV_INFO_VALUE) |
1052 				 BIT(IIO_EV_INFO_ENABLE) |
1053 				 BIT(IIO_EV_INFO_PERIOD)
1054 };
1055 
1056 #define BMC150_ACCEL_CHANNEL(_axis, bits) {				\
1057 	.type = IIO_ACCEL,						\
1058 	.modified = 1,							\
1059 	.channel2 = IIO_MOD_##_axis,					\
1060 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),			\
1061 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |		\
1062 				BIT(IIO_CHAN_INFO_SAMP_FREQ),		\
1063 	.scan_index = AXIS_##_axis,					\
1064 	.scan_type = {							\
1065 		.sign = 's',						\
1066 		.realbits = (bits),					\
1067 		.storagebits = 16,					\
1068 		.shift = 16 - (bits),					\
1069 		.endianness = IIO_LE,					\
1070 	},								\
1071 	.ext_info = bmc150_accel_ext_info,				\
1072 	.event_spec = &bmc150_accel_event,				\
1073 	.num_event_specs = 1						\
1074 }
1075 
1076 #define BMC150_ACCEL_CHANNELS(bits) {					\
1077 	{								\
1078 		.type = IIO_TEMP,					\
1079 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |		\
1080 				      BIT(IIO_CHAN_INFO_SCALE) |	\
1081 				      BIT(IIO_CHAN_INFO_OFFSET),	\
1082 		.scan_index = -1,					\
1083 	},								\
1084 	BMC150_ACCEL_CHANNEL(X, bits),					\
1085 	BMC150_ACCEL_CHANNEL(Y, bits),					\
1086 	BMC150_ACCEL_CHANNEL(Z, bits),					\
1087 	IIO_CHAN_SOFT_TIMESTAMP(3),					\
1088 }
1089 
1090 static const struct iio_chan_spec bma222e_accel_channels[] =
1091 	BMC150_ACCEL_CHANNELS(8);
1092 static const struct iio_chan_spec bma250e_accel_channels[] =
1093 	BMC150_ACCEL_CHANNELS(10);
1094 static const struct iio_chan_spec bmc150_accel_channels[] =
1095 	BMC150_ACCEL_CHANNELS(12);
1096 static const struct iio_chan_spec bma280_accel_channels[] =
1097 	BMC150_ACCEL_CHANNELS(14);
1098 
1099 /*
1100  * The range for the Bosch sensors is typically +-2g/4g/8g/16g, distributed
1101  * over the amount of bits (see above). The scale table can be calculated using
1102  *     (range / 2^bits) * g = (range / 2^bits) * 9.80665 m/s^2
1103  * e.g. for +-2g and 12 bits: (4 / 2^12) * 9.80665 m/s^2 = 0.0095768... m/s^2
1104  * Multiply 10^6 and round to get the values listed below.
1105  */
1106 static const struct bmc150_accel_chip_info bmc150_accel_chip_info_tbl[] = {
1107 	{
1108 		.name = "BMA222",
1109 		.chip_id = 0x03,
1110 		.channels = bma222e_accel_channels,
1111 		.num_channels = ARRAY_SIZE(bma222e_accel_channels),
1112 		.scale_table = { {153229, BMC150_ACCEL_DEF_RANGE_2G},
1113 				 {306458, BMC150_ACCEL_DEF_RANGE_4G},
1114 				 {612916, BMC150_ACCEL_DEF_RANGE_8G},
1115 				 {1225831, BMC150_ACCEL_DEF_RANGE_16G} },
1116 	},
1117 	{
1118 		.name = "BMA222E",
1119 		.chip_id = 0xF8,
1120 		.channels = bma222e_accel_channels,
1121 		.num_channels = ARRAY_SIZE(bma222e_accel_channels),
1122 		.scale_table = { {153229, BMC150_ACCEL_DEF_RANGE_2G},
1123 				 {306458, BMC150_ACCEL_DEF_RANGE_4G},
1124 				 {612916, BMC150_ACCEL_DEF_RANGE_8G},
1125 				 {1225831, BMC150_ACCEL_DEF_RANGE_16G} },
1126 	},
1127 	{
1128 		.name = "BMA250E",
1129 		.chip_id = 0xF9,
1130 		.channels = bma250e_accel_channels,
1131 		.num_channels = ARRAY_SIZE(bma250e_accel_channels),
1132 		.scale_table = { {38307, BMC150_ACCEL_DEF_RANGE_2G},
1133 				 {76614, BMC150_ACCEL_DEF_RANGE_4G},
1134 				 {153229, BMC150_ACCEL_DEF_RANGE_8G},
1135 				 {306458, BMC150_ACCEL_DEF_RANGE_16G} },
1136 	},
1137 	{
1138 		.name = "BMA253/BMA254/BMA255/BMC150/BMC156/BMI055",
1139 		.chip_id = 0xFA,
1140 		.channels = bmc150_accel_channels,
1141 		.num_channels = ARRAY_SIZE(bmc150_accel_channels),
1142 		.scale_table = { {9577, BMC150_ACCEL_DEF_RANGE_2G},
1143 				 {19154, BMC150_ACCEL_DEF_RANGE_4G},
1144 				 {38307, BMC150_ACCEL_DEF_RANGE_8G},
1145 				 {76614, BMC150_ACCEL_DEF_RANGE_16G} },
1146 	},
1147 	{
1148 		.name = "BMA280",
1149 		.chip_id = 0xFB,
1150 		.channels = bma280_accel_channels,
1151 		.num_channels = ARRAY_SIZE(bma280_accel_channels),
1152 		.scale_table = { {2394, BMC150_ACCEL_DEF_RANGE_2G},
1153 				 {4788, BMC150_ACCEL_DEF_RANGE_4G},
1154 				 {9577, BMC150_ACCEL_DEF_RANGE_8G},
1155 				 {19154, BMC150_ACCEL_DEF_RANGE_16G} },
1156 	},
1157 };
1158 
1159 static const struct iio_info bmc150_accel_info = {
1160 	.attrs			= &bmc150_accel_attrs_group,
1161 	.read_raw		= bmc150_accel_read_raw,
1162 	.write_raw		= bmc150_accel_write_raw,
1163 	.read_event_value	= bmc150_accel_read_event,
1164 	.write_event_value	= bmc150_accel_write_event,
1165 	.write_event_config	= bmc150_accel_write_event_config,
1166 	.read_event_config	= bmc150_accel_read_event_config,
1167 };
1168 
1169 static const struct iio_info bmc150_accel_info_fifo = {
1170 	.attrs			= &bmc150_accel_attrs_group,
1171 	.read_raw		= bmc150_accel_read_raw,
1172 	.write_raw		= bmc150_accel_write_raw,
1173 	.read_event_value	= bmc150_accel_read_event,
1174 	.write_event_value	= bmc150_accel_write_event,
1175 	.write_event_config	= bmc150_accel_write_event_config,
1176 	.read_event_config	= bmc150_accel_read_event_config,
1177 	.validate_trigger	= bmc150_accel_validate_trigger,
1178 	.hwfifo_set_watermark	= bmc150_accel_set_watermark,
1179 	.hwfifo_flush_to_buffer	= bmc150_accel_fifo_flush,
1180 };
1181 
1182 static const unsigned long bmc150_accel_scan_masks[] = {
1183 					BIT(AXIS_X) | BIT(AXIS_Y) | BIT(AXIS_Z),
1184 					0};
1185 
1186 static irqreturn_t bmc150_accel_trigger_handler(int irq, void *p)
1187 {
1188 	struct iio_poll_func *pf = p;
1189 	struct iio_dev *indio_dev = pf->indio_dev;
1190 	struct bmc150_accel_data *data = iio_priv(indio_dev);
1191 	int ret;
1192 
1193 	mutex_lock(&data->mutex);
1194 	ret = regmap_bulk_read(data->regmap, BMC150_ACCEL_REG_XOUT_L,
1195 			       data->buffer, AXIS_MAX * 2);
1196 	mutex_unlock(&data->mutex);
1197 	if (ret < 0)
1198 		goto err_read;
1199 
1200 	iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
1201 					   pf->timestamp);
1202 err_read:
1203 	iio_trigger_notify_done(indio_dev->trig);
1204 
1205 	return IRQ_HANDLED;
1206 }
1207 
1208 static void bmc150_accel_trig_reen(struct iio_trigger *trig)
1209 {
1210 	struct bmc150_accel_trigger *t = iio_trigger_get_drvdata(trig);
1211 	struct bmc150_accel_data *data = t->data;
1212 	struct device *dev = regmap_get_device(data->regmap);
1213 	int ret;
1214 
1215 	/* new data interrupts don't need ack */
1216 	if (t == &t->data->triggers[BMC150_ACCEL_TRIGGER_DATA_READY])
1217 		return;
1218 
1219 	mutex_lock(&data->mutex);
1220 	/* clear any latched interrupt */
1221 	ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_RST_LATCH,
1222 			   BMC150_ACCEL_INT_MODE_LATCH_INT |
1223 			   BMC150_ACCEL_INT_MODE_LATCH_RESET);
1224 	mutex_unlock(&data->mutex);
1225 	if (ret < 0)
1226 		dev_err(dev, "Error writing reg_int_rst_latch\n");
1227 }
1228 
1229 static int bmc150_accel_trigger_set_state(struct iio_trigger *trig,
1230 					  bool state)
1231 {
1232 	struct bmc150_accel_trigger *t = iio_trigger_get_drvdata(trig);
1233 	struct bmc150_accel_data *data = t->data;
1234 	int ret;
1235 
1236 	mutex_lock(&data->mutex);
1237 
1238 	if (t->enabled == state) {
1239 		mutex_unlock(&data->mutex);
1240 		return 0;
1241 	}
1242 
1243 	if (t->setup) {
1244 		ret = t->setup(t, state);
1245 		if (ret < 0) {
1246 			mutex_unlock(&data->mutex);
1247 			return ret;
1248 		}
1249 	}
1250 
1251 	ret = bmc150_accel_set_interrupt(data, t->intr, state);
1252 	if (ret < 0) {
1253 		mutex_unlock(&data->mutex);
1254 		return ret;
1255 	}
1256 
1257 	t->enabled = state;
1258 
1259 	mutex_unlock(&data->mutex);
1260 
1261 	return ret;
1262 }
1263 
1264 static const struct iio_trigger_ops bmc150_accel_trigger_ops = {
1265 	.set_trigger_state = bmc150_accel_trigger_set_state,
1266 	.reenable = bmc150_accel_trig_reen,
1267 };
1268 
1269 static int bmc150_accel_handle_roc_event(struct iio_dev *indio_dev)
1270 {
1271 	struct bmc150_accel_data *data = iio_priv(indio_dev);
1272 	struct device *dev = regmap_get_device(data->regmap);
1273 	int dir;
1274 	int ret;
1275 	unsigned int val;
1276 
1277 	ret = regmap_read(data->regmap, BMC150_ACCEL_REG_INT_STATUS_2, &val);
1278 	if (ret < 0) {
1279 		dev_err(dev, "Error reading reg_int_status_2\n");
1280 		return ret;
1281 	}
1282 
1283 	if (val & BMC150_ACCEL_ANY_MOTION_BIT_SIGN)
1284 		dir = IIO_EV_DIR_FALLING;
1285 	else
1286 		dir = IIO_EV_DIR_RISING;
1287 
1288 	if (val & BMC150_ACCEL_ANY_MOTION_BIT_X)
1289 		iio_push_event(indio_dev,
1290 			       IIO_MOD_EVENT_CODE(IIO_ACCEL,
1291 						  0,
1292 						  IIO_MOD_X,
1293 						  IIO_EV_TYPE_ROC,
1294 						  dir),
1295 			       data->timestamp);
1296 
1297 	if (val & BMC150_ACCEL_ANY_MOTION_BIT_Y)
1298 		iio_push_event(indio_dev,
1299 			       IIO_MOD_EVENT_CODE(IIO_ACCEL,
1300 						  0,
1301 						  IIO_MOD_Y,
1302 						  IIO_EV_TYPE_ROC,
1303 						  dir),
1304 			       data->timestamp);
1305 
1306 	if (val & BMC150_ACCEL_ANY_MOTION_BIT_Z)
1307 		iio_push_event(indio_dev,
1308 			       IIO_MOD_EVENT_CODE(IIO_ACCEL,
1309 						  0,
1310 						  IIO_MOD_Z,
1311 						  IIO_EV_TYPE_ROC,
1312 						  dir),
1313 			       data->timestamp);
1314 
1315 	return ret;
1316 }
1317 
1318 static irqreturn_t bmc150_accel_irq_thread_handler(int irq, void *private)
1319 {
1320 	struct iio_dev *indio_dev = private;
1321 	struct bmc150_accel_data *data = iio_priv(indio_dev);
1322 	struct device *dev = regmap_get_device(data->regmap);
1323 	bool ack = false;
1324 	int ret;
1325 
1326 	mutex_lock(&data->mutex);
1327 
1328 	if (data->fifo_mode) {
1329 		ret = __bmc150_accel_fifo_flush(indio_dev,
1330 						BMC150_ACCEL_FIFO_LENGTH, true);
1331 		if (ret > 0)
1332 			ack = true;
1333 	}
1334 
1335 	if (data->ev_enable_state) {
1336 		ret = bmc150_accel_handle_roc_event(indio_dev);
1337 		if (ret > 0)
1338 			ack = true;
1339 	}
1340 
1341 	if (ack) {
1342 		ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_RST_LATCH,
1343 				   BMC150_ACCEL_INT_MODE_LATCH_INT |
1344 				   BMC150_ACCEL_INT_MODE_LATCH_RESET);
1345 		if (ret)
1346 			dev_err(dev, "Error writing reg_int_rst_latch\n");
1347 
1348 		ret = IRQ_HANDLED;
1349 	} else {
1350 		ret = IRQ_NONE;
1351 	}
1352 
1353 	mutex_unlock(&data->mutex);
1354 
1355 	return ret;
1356 }
1357 
1358 static irqreturn_t bmc150_accel_irq_handler(int irq, void *private)
1359 {
1360 	struct iio_dev *indio_dev = private;
1361 	struct bmc150_accel_data *data = iio_priv(indio_dev);
1362 	bool ack = false;
1363 	int i;
1364 
1365 	data->old_timestamp = data->timestamp;
1366 	data->timestamp = iio_get_time_ns(indio_dev);
1367 
1368 	for (i = 0; i < BMC150_ACCEL_TRIGGERS; i++) {
1369 		if (data->triggers[i].enabled) {
1370 			iio_trigger_poll(data->triggers[i].indio_trig);
1371 			ack = true;
1372 			break;
1373 		}
1374 	}
1375 
1376 	if (data->ev_enable_state || data->fifo_mode)
1377 		return IRQ_WAKE_THREAD;
1378 
1379 	if (ack)
1380 		return IRQ_HANDLED;
1381 
1382 	return IRQ_NONE;
1383 }
1384 
1385 static const struct {
1386 	int intr;
1387 	const char *name;
1388 	int (*setup)(struct bmc150_accel_trigger *t, bool state);
1389 } bmc150_accel_triggers[BMC150_ACCEL_TRIGGERS] = {
1390 	{
1391 		.intr = 0,
1392 		.name = "%s-dev%d",
1393 	},
1394 	{
1395 		.intr = 1,
1396 		.name = "%s-any-motion-dev%d",
1397 		.setup = bmc150_accel_any_motion_setup,
1398 	},
1399 };
1400 
1401 static void bmc150_accel_unregister_triggers(struct bmc150_accel_data *data,
1402 					     int from)
1403 {
1404 	int i;
1405 
1406 	for (i = from; i >= 0; i--) {
1407 		if (data->triggers[i].indio_trig) {
1408 			iio_trigger_unregister(data->triggers[i].indio_trig);
1409 			data->triggers[i].indio_trig = NULL;
1410 		}
1411 	}
1412 }
1413 
1414 static int bmc150_accel_triggers_setup(struct iio_dev *indio_dev,
1415 				       struct bmc150_accel_data *data)
1416 {
1417 	struct device *dev = regmap_get_device(data->regmap);
1418 	int i, ret;
1419 
1420 	for (i = 0; i < BMC150_ACCEL_TRIGGERS; i++) {
1421 		struct bmc150_accel_trigger *t = &data->triggers[i];
1422 
1423 		t->indio_trig = devm_iio_trigger_alloc(dev,
1424 						       bmc150_accel_triggers[i].name,
1425 						       indio_dev->name,
1426 						       iio_device_id(indio_dev));
1427 		if (!t->indio_trig) {
1428 			ret = -ENOMEM;
1429 			break;
1430 		}
1431 
1432 		t->indio_trig->ops = &bmc150_accel_trigger_ops;
1433 		t->intr = bmc150_accel_triggers[i].intr;
1434 		t->data = data;
1435 		t->setup = bmc150_accel_triggers[i].setup;
1436 		iio_trigger_set_drvdata(t->indio_trig, t);
1437 
1438 		ret = iio_trigger_register(t->indio_trig);
1439 		if (ret)
1440 			break;
1441 	}
1442 
1443 	if (ret)
1444 		bmc150_accel_unregister_triggers(data, i - 1);
1445 
1446 	return ret;
1447 }
1448 
1449 #define BMC150_ACCEL_FIFO_MODE_STREAM          0x80
1450 #define BMC150_ACCEL_FIFO_MODE_FIFO            0x40
1451 #define BMC150_ACCEL_FIFO_MODE_BYPASS          0x00
1452 
1453 static int bmc150_accel_fifo_set_mode(struct bmc150_accel_data *data)
1454 {
1455 	struct device *dev = regmap_get_device(data->regmap);
1456 	u8 reg = BMC150_ACCEL_REG_FIFO_CONFIG1;
1457 	int ret;
1458 
1459 	ret = regmap_write(data->regmap, reg, data->fifo_mode);
1460 	if (ret < 0) {
1461 		dev_err(dev, "Error writing reg_fifo_config1\n");
1462 		return ret;
1463 	}
1464 
1465 	if (!data->fifo_mode)
1466 		return 0;
1467 
1468 	ret = regmap_write(data->regmap, BMC150_ACCEL_REG_FIFO_CONFIG0,
1469 			   data->watermark);
1470 	if (ret < 0)
1471 		dev_err(dev, "Error writing reg_fifo_config0\n");
1472 
1473 	return ret;
1474 }
1475 
1476 static int bmc150_accel_buffer_preenable(struct iio_dev *indio_dev)
1477 {
1478 	struct bmc150_accel_data *data = iio_priv(indio_dev);
1479 
1480 	return bmc150_accel_set_power_state(data, true);
1481 }
1482 
1483 static int bmc150_accel_buffer_postenable(struct iio_dev *indio_dev)
1484 {
1485 	struct bmc150_accel_data *data = iio_priv(indio_dev);
1486 	int ret = 0;
1487 
1488 	if (iio_device_get_current_mode(indio_dev) == INDIO_BUFFER_TRIGGERED)
1489 		return 0;
1490 
1491 	mutex_lock(&data->mutex);
1492 
1493 	if (!data->watermark)
1494 		goto out;
1495 
1496 	ret = bmc150_accel_set_interrupt(data, BMC150_ACCEL_INT_WATERMARK,
1497 					 true);
1498 	if (ret)
1499 		goto out;
1500 
1501 	data->fifo_mode = BMC150_ACCEL_FIFO_MODE_FIFO;
1502 
1503 	ret = bmc150_accel_fifo_set_mode(data);
1504 	if (ret) {
1505 		data->fifo_mode = 0;
1506 		bmc150_accel_set_interrupt(data, BMC150_ACCEL_INT_WATERMARK,
1507 					   false);
1508 	}
1509 
1510 out:
1511 	mutex_unlock(&data->mutex);
1512 
1513 	return ret;
1514 }
1515 
1516 static int bmc150_accel_buffer_predisable(struct iio_dev *indio_dev)
1517 {
1518 	struct bmc150_accel_data *data = iio_priv(indio_dev);
1519 
1520 	if (iio_device_get_current_mode(indio_dev) == INDIO_BUFFER_TRIGGERED)
1521 		return 0;
1522 
1523 	mutex_lock(&data->mutex);
1524 
1525 	if (!data->fifo_mode)
1526 		goto out;
1527 
1528 	bmc150_accel_set_interrupt(data, BMC150_ACCEL_INT_WATERMARK, false);
1529 	__bmc150_accel_fifo_flush(indio_dev, BMC150_ACCEL_FIFO_LENGTH, false);
1530 	data->fifo_mode = 0;
1531 	bmc150_accel_fifo_set_mode(data);
1532 
1533 out:
1534 	mutex_unlock(&data->mutex);
1535 
1536 	return 0;
1537 }
1538 
1539 static int bmc150_accel_buffer_postdisable(struct iio_dev *indio_dev)
1540 {
1541 	struct bmc150_accel_data *data = iio_priv(indio_dev);
1542 
1543 	return bmc150_accel_set_power_state(data, false);
1544 }
1545 
1546 static const struct iio_buffer_setup_ops bmc150_accel_buffer_ops = {
1547 	.preenable = bmc150_accel_buffer_preenable,
1548 	.postenable = bmc150_accel_buffer_postenable,
1549 	.predisable = bmc150_accel_buffer_predisable,
1550 	.postdisable = bmc150_accel_buffer_postdisable,
1551 };
1552 
1553 static int bmc150_accel_chip_init(struct bmc150_accel_data *data)
1554 {
1555 	struct device *dev = regmap_get_device(data->regmap);
1556 	int ret, i;
1557 	unsigned int val;
1558 
1559 	/*
1560 	 * Reset chip to get it in a known good state. A delay of 1.8ms after
1561 	 * reset is required according to the data sheets of supported chips.
1562 	 */
1563 	regmap_write(data->regmap, BMC150_ACCEL_REG_RESET,
1564 		     BMC150_ACCEL_RESET_VAL);
1565 	usleep_range(1800, 2500);
1566 
1567 	ret = regmap_read(data->regmap, BMC150_ACCEL_REG_CHIP_ID, &val);
1568 	if (ret < 0) {
1569 		dev_err(dev, "Error: Reading chip id\n");
1570 		return ret;
1571 	}
1572 
1573 	dev_dbg(dev, "Chip Id %x\n", val);
1574 	for (i = 0; i < ARRAY_SIZE(bmc150_accel_chip_info_tbl); i++) {
1575 		if (bmc150_accel_chip_info_tbl[i].chip_id == val) {
1576 			data->chip_info = &bmc150_accel_chip_info_tbl[i];
1577 			break;
1578 		}
1579 	}
1580 
1581 	if (!data->chip_info) {
1582 		dev_err(dev, "Invalid chip %x\n", val);
1583 		return -ENODEV;
1584 	}
1585 
1586 	ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0);
1587 	if (ret < 0)
1588 		return ret;
1589 
1590 	/* Set Bandwidth */
1591 	ret = bmc150_accel_set_bw(data, BMC150_ACCEL_DEF_BW, 0);
1592 	if (ret < 0)
1593 		return ret;
1594 
1595 	/* Set Default Range */
1596 	ret = regmap_write(data->regmap, BMC150_ACCEL_REG_PMU_RANGE,
1597 			   BMC150_ACCEL_DEF_RANGE_4G);
1598 	if (ret < 0) {
1599 		dev_err(dev, "Error writing reg_pmu_range\n");
1600 		return ret;
1601 	}
1602 
1603 	data->range = BMC150_ACCEL_DEF_RANGE_4G;
1604 
1605 	/* Set default slope duration and thresholds */
1606 	data->slope_thres = BMC150_ACCEL_DEF_SLOPE_THRESHOLD;
1607 	data->slope_dur = BMC150_ACCEL_DEF_SLOPE_DURATION;
1608 	ret = bmc150_accel_update_slope(data);
1609 	if (ret < 0)
1610 		return ret;
1611 
1612 	/* Set default as latched interrupts */
1613 	ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_RST_LATCH,
1614 			   BMC150_ACCEL_INT_MODE_LATCH_INT |
1615 			   BMC150_ACCEL_INT_MODE_LATCH_RESET);
1616 	if (ret < 0) {
1617 		dev_err(dev, "Error writing reg_int_rst_latch\n");
1618 		return ret;
1619 	}
1620 
1621 	return 0;
1622 }
1623 
1624 int bmc150_accel_core_probe(struct device *dev, struct regmap *regmap, int irq,
1625 			    enum bmc150_type type, const char *name,
1626 			    bool block_supported)
1627 {
1628 	const struct iio_dev_attr **fifo_attrs;
1629 	struct bmc150_accel_data *data;
1630 	struct iio_dev *indio_dev;
1631 	int ret;
1632 
1633 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
1634 	if (!indio_dev)
1635 		return -ENOMEM;
1636 
1637 	data = iio_priv(indio_dev);
1638 	dev_set_drvdata(dev, indio_dev);
1639 
1640 	data->regmap = regmap;
1641 	data->type = type;
1642 
1643 	if (!bmc150_apply_acpi_orientation(dev, &data->orientation)) {
1644 		ret = iio_read_mount_matrix(dev, &data->orientation);
1645 		if (ret)
1646 			return ret;
1647 	}
1648 
1649 	/*
1650 	 * VDD   is the analog and digital domain voltage supply
1651 	 * VDDIO is the digital I/O voltage supply
1652 	 */
1653 	data->regulators[0].supply = "vdd";
1654 	data->regulators[1].supply = "vddio";
1655 	ret = devm_regulator_bulk_get(dev,
1656 				      ARRAY_SIZE(data->regulators),
1657 				      data->regulators);
1658 	if (ret)
1659 		return dev_err_probe(dev, ret, "failed to get regulators\n");
1660 
1661 	ret = regulator_bulk_enable(ARRAY_SIZE(data->regulators),
1662 				    data->regulators);
1663 	if (ret) {
1664 		dev_err(dev, "failed to enable regulators: %d\n", ret);
1665 		return ret;
1666 	}
1667 	/*
1668 	 * 2ms or 3ms power-on time according to datasheets, let's better
1669 	 * be safe than sorry and set this delay to 5ms.
1670 	 */
1671 	msleep(5);
1672 
1673 	ret = bmc150_accel_chip_init(data);
1674 	if (ret < 0)
1675 		goto err_disable_regulators;
1676 
1677 	mutex_init(&data->mutex);
1678 
1679 	indio_dev->channels = data->chip_info->channels;
1680 	indio_dev->num_channels = data->chip_info->num_channels;
1681 	indio_dev->name = name ? name : data->chip_info->name;
1682 	indio_dev->available_scan_masks = bmc150_accel_scan_masks;
1683 	indio_dev->modes = INDIO_DIRECT_MODE;
1684 	indio_dev->info = &bmc150_accel_info;
1685 
1686 	if (block_supported) {
1687 		indio_dev->modes |= INDIO_BUFFER_SOFTWARE;
1688 		indio_dev->info = &bmc150_accel_info_fifo;
1689 		fifo_attrs = bmc150_accel_fifo_attributes;
1690 	} else {
1691 		fifo_attrs = NULL;
1692 	}
1693 
1694 	ret = iio_triggered_buffer_setup_ext(indio_dev,
1695 					     &iio_pollfunc_store_time,
1696 					     bmc150_accel_trigger_handler,
1697 					     IIO_BUFFER_DIRECTION_IN,
1698 					     &bmc150_accel_buffer_ops,
1699 					     fifo_attrs);
1700 	if (ret < 0) {
1701 		dev_err(dev, "Failed: iio triggered buffer setup\n");
1702 		goto err_disable_regulators;
1703 	}
1704 
1705 	if (irq > 0) {
1706 		ret = devm_request_threaded_irq(dev, irq,
1707 						bmc150_accel_irq_handler,
1708 						bmc150_accel_irq_thread_handler,
1709 						IRQF_TRIGGER_RISING,
1710 						BMC150_ACCEL_IRQ_NAME,
1711 						indio_dev);
1712 		if (ret)
1713 			goto err_buffer_cleanup;
1714 
1715 		/*
1716 		 * Set latched mode interrupt. While certain interrupts are
1717 		 * non-latched regardless of this settings (e.g. new data) we
1718 		 * want to use latch mode when we can to prevent interrupt
1719 		 * flooding.
1720 		 */
1721 		ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_RST_LATCH,
1722 				   BMC150_ACCEL_INT_MODE_LATCH_RESET);
1723 		if (ret < 0) {
1724 			dev_err(dev, "Error writing reg_int_rst_latch\n");
1725 			goto err_buffer_cleanup;
1726 		}
1727 
1728 		bmc150_accel_interrupts_setup(indio_dev, data, irq);
1729 
1730 		ret = bmc150_accel_triggers_setup(indio_dev, data);
1731 		if (ret)
1732 			goto err_buffer_cleanup;
1733 	}
1734 
1735 	ret = pm_runtime_set_active(dev);
1736 	if (ret)
1737 		goto err_trigger_unregister;
1738 
1739 	pm_runtime_enable(dev);
1740 	pm_runtime_set_autosuspend_delay(dev, BMC150_AUTO_SUSPEND_DELAY_MS);
1741 	pm_runtime_use_autosuspend(dev);
1742 
1743 	ret = iio_device_register(indio_dev);
1744 	if (ret < 0) {
1745 		dev_err(dev, "Unable to register iio device\n");
1746 		goto err_pm_cleanup;
1747 	}
1748 
1749 	return 0;
1750 
1751 err_pm_cleanup:
1752 	pm_runtime_dont_use_autosuspend(dev);
1753 	pm_runtime_disable(dev);
1754 err_trigger_unregister:
1755 	bmc150_accel_unregister_triggers(data, BMC150_ACCEL_TRIGGERS - 1);
1756 err_buffer_cleanup:
1757 	iio_triggered_buffer_cleanup(indio_dev);
1758 err_disable_regulators:
1759 	regulator_bulk_disable(ARRAY_SIZE(data->regulators),
1760 			       data->regulators);
1761 
1762 	return ret;
1763 }
1764 EXPORT_SYMBOL_NS_GPL(bmc150_accel_core_probe, IIO_BMC150);
1765 
1766 void bmc150_accel_core_remove(struct device *dev)
1767 {
1768 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
1769 	struct bmc150_accel_data *data = iio_priv(indio_dev);
1770 
1771 	iio_device_unregister(indio_dev);
1772 
1773 	pm_runtime_disable(dev);
1774 	pm_runtime_set_suspended(dev);
1775 
1776 	bmc150_accel_unregister_triggers(data, BMC150_ACCEL_TRIGGERS - 1);
1777 
1778 	iio_triggered_buffer_cleanup(indio_dev);
1779 
1780 	mutex_lock(&data->mutex);
1781 	bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_DEEP_SUSPEND, 0);
1782 	mutex_unlock(&data->mutex);
1783 
1784 	regulator_bulk_disable(ARRAY_SIZE(data->regulators),
1785 			       data->regulators);
1786 }
1787 EXPORT_SYMBOL_NS_GPL(bmc150_accel_core_remove, IIO_BMC150);
1788 
1789 #ifdef CONFIG_PM_SLEEP
1790 static int bmc150_accel_suspend(struct device *dev)
1791 {
1792 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
1793 	struct bmc150_accel_data *data = iio_priv(indio_dev);
1794 
1795 	mutex_lock(&data->mutex);
1796 	bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_SUSPEND, 0);
1797 	mutex_unlock(&data->mutex);
1798 
1799 	return 0;
1800 }
1801 
1802 static int bmc150_accel_resume(struct device *dev)
1803 {
1804 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
1805 	struct bmc150_accel_data *data = iio_priv(indio_dev);
1806 
1807 	mutex_lock(&data->mutex);
1808 	bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0);
1809 	bmc150_accel_fifo_set_mode(data);
1810 	mutex_unlock(&data->mutex);
1811 
1812 	if (data->resume_callback)
1813 		data->resume_callback(dev);
1814 
1815 	return 0;
1816 }
1817 #endif
1818 
1819 #ifdef CONFIG_PM
1820 static int bmc150_accel_runtime_suspend(struct device *dev)
1821 {
1822 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
1823 	struct bmc150_accel_data *data = iio_priv(indio_dev);
1824 	int ret;
1825 
1826 	ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_SUSPEND, 0);
1827 	if (ret < 0)
1828 		return -EAGAIN;
1829 
1830 	return 0;
1831 }
1832 
1833 static int bmc150_accel_runtime_resume(struct device *dev)
1834 {
1835 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
1836 	struct bmc150_accel_data *data = iio_priv(indio_dev);
1837 	int ret;
1838 	int sleep_val;
1839 
1840 	ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0);
1841 	if (ret < 0)
1842 		return ret;
1843 	ret = bmc150_accel_fifo_set_mode(data);
1844 	if (ret < 0)
1845 		return ret;
1846 
1847 	sleep_val = bmc150_accel_get_startup_times(data);
1848 	if (sleep_val < 20)
1849 		usleep_range(sleep_val * 1000, 20000);
1850 	else
1851 		msleep_interruptible(sleep_val);
1852 
1853 	return 0;
1854 }
1855 #endif
1856 
1857 const struct dev_pm_ops bmc150_accel_pm_ops = {
1858 	SET_SYSTEM_SLEEP_PM_OPS(bmc150_accel_suspend, bmc150_accel_resume)
1859 	SET_RUNTIME_PM_OPS(bmc150_accel_runtime_suspend,
1860 			   bmc150_accel_runtime_resume, NULL)
1861 };
1862 EXPORT_SYMBOL_NS_GPL(bmc150_accel_pm_ops, IIO_BMC150);
1863 
1864 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
1865 MODULE_LICENSE("GPL v2");
1866 MODULE_DESCRIPTION("BMC150 accelerometer driver");
1867