xref: /openbmc/linux/drivers/iio/imu/kmx61.c (revision 11788d9b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * KMX61 - Kionix 6-axis Accelerometer/Magnetometer
4  *
5  * Copyright (c) 2014, Intel Corporation.
6  *
7  * IIO driver for KMX61 (7-bit I2C slave address 0x0E or 0x0F).
8  */
9 
10 #include <linux/module.h>
11 #include <linux/i2c.h>
12 #include <linux/acpi.h>
13 #include <linux/interrupt.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/events.h>
19 #include <linux/iio/trigger.h>
20 #include <linux/iio/buffer.h>
21 #include <linux/iio/triggered_buffer.h>
22 #include <linux/iio/trigger_consumer.h>
23 
24 #define KMX61_DRV_NAME "kmx61"
25 #define KMX61_IRQ_NAME "kmx61_event"
26 
27 #define KMX61_REG_WHO_AM_I	0x00
28 #define KMX61_REG_INS1		0x01
29 #define KMX61_REG_INS2		0x02
30 
31 /*
32  * three 16-bit accelerometer output registers for X/Y/Z axis
33  * we use only XOUT_L as a base register, all other addresses
34  * can be obtained by applying an offset and are provided here
35  * only for clarity.
36  */
37 #define KMX61_ACC_XOUT_L	0x0A
38 #define KMX61_ACC_XOUT_H	0x0B
39 #define KMX61_ACC_YOUT_L	0x0C
40 #define KMX61_ACC_YOUT_H	0x0D
41 #define KMX61_ACC_ZOUT_L	0x0E
42 #define KMX61_ACC_ZOUT_H	0x0F
43 
44 /*
45  * one 16-bit temperature output register
46  */
47 #define KMX61_TEMP_L		0x10
48 #define KMX61_TEMP_H		0x11
49 
50 /*
51  * three 16-bit magnetometer output registers for X/Y/Z axis
52  */
53 #define KMX61_MAG_XOUT_L	0x12
54 #define KMX61_MAG_XOUT_H	0x13
55 #define KMX61_MAG_YOUT_L	0x14
56 #define KMX61_MAG_YOUT_H	0x15
57 #define KMX61_MAG_ZOUT_L	0x16
58 #define KMX61_MAG_ZOUT_H	0x17
59 
60 #define KMX61_REG_INL		0x28
61 #define KMX61_REG_STBY		0x29
62 #define KMX61_REG_CTRL1		0x2A
63 #define KMX61_REG_CTRL2		0x2B
64 #define KMX61_REG_ODCNTL	0x2C
65 #define KMX61_REG_INC1		0x2D
66 
67 #define KMX61_REG_WUF_THRESH	0x3D
68 #define KMX61_REG_WUF_TIMER	0x3E
69 
70 #define KMX61_ACC_STBY_BIT	BIT(0)
71 #define KMX61_MAG_STBY_BIT	BIT(1)
72 #define KMX61_ACT_STBY_BIT	BIT(7)
73 
74 #define KMX61_ALL_STBY		(KMX61_ACC_STBY_BIT | KMX61_MAG_STBY_BIT)
75 
76 #define KMX61_REG_INS1_BIT_WUFS		BIT(1)
77 
78 #define KMX61_REG_INS2_BIT_ZP		BIT(0)
79 #define KMX61_REG_INS2_BIT_ZN		BIT(1)
80 #define KMX61_REG_INS2_BIT_YP		BIT(2)
81 #define KMX61_REG_INS2_BIT_YN		BIT(3)
82 #define KMX61_REG_INS2_BIT_XP		BIT(4)
83 #define KMX61_REG_INS2_BIT_XN		BIT(5)
84 
85 #define KMX61_REG_CTRL1_GSEL_MASK	0x03
86 
87 #define KMX61_REG_CTRL1_BIT_RES		BIT(4)
88 #define KMX61_REG_CTRL1_BIT_DRDYE	BIT(5)
89 #define KMX61_REG_CTRL1_BIT_WUFE	BIT(6)
90 #define KMX61_REG_CTRL1_BIT_BTSE	BIT(7)
91 
92 #define KMX61_REG_INC1_BIT_WUFS		BIT(0)
93 #define KMX61_REG_INC1_BIT_DRDYM	BIT(1)
94 #define KMX61_REG_INC1_BIT_DRDYA	BIT(2)
95 #define KMX61_REG_INC1_BIT_IEN		BIT(5)
96 
97 #define KMX61_ACC_ODR_SHIFT	0
98 #define KMX61_MAG_ODR_SHIFT	4
99 #define KMX61_ACC_ODR_MASK	0x0F
100 #define KMX61_MAG_ODR_MASK	0xF0
101 
102 #define KMX61_OWUF_MASK		0x7
103 
104 #define KMX61_DEFAULT_WAKE_THRESH	1
105 #define KMX61_DEFAULT_WAKE_DURATION	1
106 
107 #define KMX61_SLEEP_DELAY_MS	2000
108 
109 #define KMX61_CHIP_ID		0x12
110 
111 /* KMX61 devices */
112 #define KMX61_ACC	0x01
113 #define KMX61_MAG	0x02
114 
115 struct kmx61_data {
116 	struct i2c_client *client;
117 
118 	/* serialize access to non-atomic ops, e.g set_mode */
119 	struct mutex lock;
120 
121 	/* standby state */
122 	bool acc_stby;
123 	bool mag_stby;
124 
125 	/* power state */
126 	bool acc_ps;
127 	bool mag_ps;
128 
129 	/* config bits */
130 	u8 range;
131 	u8 odr_bits;
132 	u8 wake_thresh;
133 	u8 wake_duration;
134 
135 	/* accelerometer specific data */
136 	struct iio_dev *acc_indio_dev;
137 	struct iio_trigger *acc_dready_trig;
138 	struct iio_trigger *motion_trig;
139 	bool acc_dready_trig_on;
140 	bool motion_trig_on;
141 	bool ev_enable_state;
142 
143 	/* magnetometer specific data */
144 	struct iio_dev *mag_indio_dev;
145 	struct iio_trigger *mag_dready_trig;
146 	bool mag_dready_trig_on;
147 };
148 
149 enum kmx61_range {
150 	KMX61_RANGE_2G,
151 	KMX61_RANGE_4G,
152 	KMX61_RANGE_8G,
153 };
154 
155 enum kmx61_axis {
156 	KMX61_AXIS_X,
157 	KMX61_AXIS_Y,
158 	KMX61_AXIS_Z,
159 };
160 
161 static const u16 kmx61_uscale_table[] = {9582, 19163, 38326};
162 
163 static const struct {
164 	int val;
165 	int val2;
166 } kmx61_samp_freq_table[] = { {12, 500000},
167 			{25, 0},
168 			{50, 0},
169 			{100, 0},
170 			{200, 0},
171 			{400, 0},
172 			{800, 0},
173 			{1600, 0},
174 			{0, 781000},
175 			{1, 563000},
176 			{3, 125000},
177 			{6, 250000} };
178 
179 static const struct {
180 	int val;
181 	int val2;
182 	int odr_bits;
183 } kmx61_wake_up_odr_table[] = { {0, 781000, 0x00},
184 				 {1, 563000, 0x01},
185 				 {3, 125000, 0x02},
186 				 {6, 250000, 0x03},
187 				 {12, 500000, 0x04},
188 				 {25, 0, 0x05},
189 				 {50, 0, 0x06},
190 				 {100, 0, 0x06},
191 				 {200, 0, 0x06},
192 				 {400, 0, 0x06},
193 				 {800, 0, 0x06},
194 				 {1600, 0, 0x06} };
195 
196 static IIO_CONST_ATTR(accel_scale_available, "0.009582 0.019163 0.038326");
197 static IIO_CONST_ATTR(magn_scale_available, "0.001465");
198 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
199 	"0.781000 1.563000 3.125000 6.250000 12.500000 25 50 100 200 400 800");
200 
201 static struct attribute *kmx61_acc_attributes[] = {
202 	&iio_const_attr_accel_scale_available.dev_attr.attr,
203 	&iio_const_attr_sampling_frequency_available.dev_attr.attr,
204 	NULL,
205 };
206 
207 static struct attribute *kmx61_mag_attributes[] = {
208 	&iio_const_attr_magn_scale_available.dev_attr.attr,
209 	&iio_const_attr_sampling_frequency_available.dev_attr.attr,
210 	NULL,
211 };
212 
213 static const struct attribute_group kmx61_acc_attribute_group = {
214 	.attrs = kmx61_acc_attributes,
215 };
216 
217 static const struct attribute_group kmx61_mag_attribute_group = {
218 	.attrs = kmx61_mag_attributes,
219 };
220 
221 static const struct iio_event_spec kmx61_event = {
222 	.type = IIO_EV_TYPE_THRESH,
223 	.dir = IIO_EV_DIR_EITHER,
224 	.mask_separate = BIT(IIO_EV_INFO_VALUE) |
225 			 BIT(IIO_EV_INFO_ENABLE) |
226 			 BIT(IIO_EV_INFO_PERIOD),
227 };
228 
229 #define KMX61_ACC_CHAN(_axis) { \
230 	.type = IIO_ACCEL, \
231 	.modified = 1, \
232 	.channel2 = IIO_MOD_ ## _axis, \
233 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
234 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
235 				BIT(IIO_CHAN_INFO_SAMP_FREQ), \
236 	.address = KMX61_ACC, \
237 	.scan_index = KMX61_AXIS_ ## _axis, \
238 	.scan_type = { \
239 		.sign = 's', \
240 		.realbits = 12, \
241 		.storagebits = 16, \
242 		.shift = 4, \
243 		.endianness = IIO_LE, \
244 	}, \
245 	.event_spec = &kmx61_event, \
246 	.num_event_specs = 1 \
247 }
248 
249 #define KMX61_MAG_CHAN(_axis) { \
250 	.type = IIO_MAGN, \
251 	.modified = 1, \
252 	.channel2 = IIO_MOD_ ## _axis, \
253 	.address = KMX61_MAG, \
254 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
255 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
256 				BIT(IIO_CHAN_INFO_SAMP_FREQ), \
257 	.scan_index = KMX61_AXIS_ ## _axis, \
258 	.scan_type = { \
259 		.sign = 's', \
260 		.realbits = 14, \
261 		.storagebits = 16, \
262 		.shift = 2, \
263 		.endianness = IIO_LE, \
264 	}, \
265 }
266 
267 static const struct iio_chan_spec kmx61_acc_channels[] = {
268 	KMX61_ACC_CHAN(X),
269 	KMX61_ACC_CHAN(Y),
270 	KMX61_ACC_CHAN(Z),
271 };
272 
273 static const struct iio_chan_spec kmx61_mag_channels[] = {
274 	KMX61_MAG_CHAN(X),
275 	KMX61_MAG_CHAN(Y),
276 	KMX61_MAG_CHAN(Z),
277 };
278 
279 static void kmx61_set_data(struct iio_dev *indio_dev, struct kmx61_data *data)
280 {
281 	struct kmx61_data **priv = iio_priv(indio_dev);
282 
283 	*priv = data;
284 }
285 
286 static struct kmx61_data *kmx61_get_data(struct iio_dev *indio_dev)
287 {
288 	return *(struct kmx61_data **)iio_priv(indio_dev);
289 }
290 
291 static int kmx61_convert_freq_to_bit(int val, int val2)
292 {
293 	int i;
294 
295 	for (i = 0; i < ARRAY_SIZE(kmx61_samp_freq_table); i++)
296 		if (val == kmx61_samp_freq_table[i].val &&
297 		    val2 == kmx61_samp_freq_table[i].val2)
298 			return i;
299 	return -EINVAL;
300 }
301 
302 static int kmx61_convert_wake_up_odr_to_bit(int val, int val2)
303 {
304 	int i;
305 
306 	for (i = 0; i < ARRAY_SIZE(kmx61_wake_up_odr_table); ++i)
307 		if (kmx61_wake_up_odr_table[i].val == val &&
308 			kmx61_wake_up_odr_table[i].val2 == val2)
309 				return kmx61_wake_up_odr_table[i].odr_bits;
310 	return -EINVAL;
311 }
312 
313 /**
314  * kmx61_set_mode() - set KMX61 device operating mode
315  * @data: kmx61 device private data pointer
316  * @mode: bitmask, indicating operating mode for @device
317  * @device: bitmask, indicating device for which @mode needs to be set
318  * @update: update stby bits stored in device's private  @data
319  *
320  * For each sensor (accelerometer/magnetometer) there are two operating modes
321  * STANDBY and OPERATION. Neither accel nor magn can be disabled independently
322  * if they are both enabled. Internal sensors state is saved in acc_stby and
323  * mag_stby members of driver's private @data.
324  */
325 static int kmx61_set_mode(struct kmx61_data *data, u8 mode, u8 device,
326 			  bool update)
327 {
328 	int ret;
329 	int acc_stby = -1, mag_stby = -1;
330 
331 	ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_STBY);
332 	if (ret < 0) {
333 		dev_err(&data->client->dev, "Error reading reg_stby\n");
334 		return ret;
335 	}
336 	if (device & KMX61_ACC) {
337 		if (mode & KMX61_ACC_STBY_BIT) {
338 			ret |= KMX61_ACC_STBY_BIT;
339 			acc_stby = 1;
340 		} else {
341 			ret &= ~KMX61_ACC_STBY_BIT;
342 			acc_stby = 0;
343 		}
344 	}
345 
346 	if (device & KMX61_MAG) {
347 		if (mode & KMX61_MAG_STBY_BIT) {
348 			ret |= KMX61_MAG_STBY_BIT;
349 			mag_stby = 1;
350 		} else {
351 			ret &= ~KMX61_MAG_STBY_BIT;
352 			mag_stby = 0;
353 		}
354 	}
355 
356 	if (mode & KMX61_ACT_STBY_BIT)
357 		ret |= KMX61_ACT_STBY_BIT;
358 
359 	ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_STBY, ret);
360 	if (ret < 0) {
361 		dev_err(&data->client->dev, "Error writing reg_stby\n");
362 		return ret;
363 	}
364 
365 	if (acc_stby != -1 && update)
366 		data->acc_stby = acc_stby;
367 	if (mag_stby != -1 && update)
368 		data->mag_stby = mag_stby;
369 
370 	return 0;
371 }
372 
373 static int kmx61_get_mode(struct kmx61_data *data, u8 *mode, u8 device)
374 {
375 	int ret;
376 
377 	ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_STBY);
378 	if (ret < 0) {
379 		dev_err(&data->client->dev, "Error reading reg_stby\n");
380 		return ret;
381 	}
382 	*mode = 0;
383 
384 	if (device & KMX61_ACC) {
385 		if (ret & KMX61_ACC_STBY_BIT)
386 			*mode |= KMX61_ACC_STBY_BIT;
387 		else
388 			*mode &= ~KMX61_ACC_STBY_BIT;
389 	}
390 
391 	if (device & KMX61_MAG) {
392 		if (ret & KMX61_MAG_STBY_BIT)
393 			*mode |= KMX61_MAG_STBY_BIT;
394 		else
395 			*mode &= ~KMX61_MAG_STBY_BIT;
396 	}
397 
398 	return 0;
399 }
400 
401 static int kmx61_set_wake_up_odr(struct kmx61_data *data, int val, int val2)
402 {
403 	int ret, odr_bits;
404 
405 	odr_bits = kmx61_convert_wake_up_odr_to_bit(val, val2);
406 	if (odr_bits < 0)
407 		return odr_bits;
408 
409 	ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL2,
410 					odr_bits);
411 	if (ret < 0)
412 		dev_err(&data->client->dev, "Error writing reg_ctrl2\n");
413 	return ret;
414 }
415 
416 static int kmx61_set_odr(struct kmx61_data *data, int val, int val2, u8 device)
417 {
418 	int ret;
419 	u8 mode;
420 	int lodr_bits, odr_bits;
421 
422 	ret = kmx61_get_mode(data, &mode, KMX61_ACC | KMX61_MAG);
423 	if (ret < 0)
424 		return ret;
425 
426 	lodr_bits = kmx61_convert_freq_to_bit(val, val2);
427 	if (lodr_bits < 0)
428 		return lodr_bits;
429 
430 	/* To change ODR, accel and magn must be in STDBY */
431 	ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG,
432 			     true);
433 	if (ret < 0)
434 		return ret;
435 
436 	odr_bits = 0;
437 	if (device & KMX61_ACC)
438 		odr_bits |= lodr_bits << KMX61_ACC_ODR_SHIFT;
439 	if (device & KMX61_MAG)
440 		odr_bits |= lodr_bits << KMX61_MAG_ODR_SHIFT;
441 
442 	ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_ODCNTL,
443 					odr_bits);
444 	if (ret < 0)
445 		return ret;
446 
447 	data->odr_bits = odr_bits;
448 
449 	if (device & KMX61_ACC) {
450 		ret = kmx61_set_wake_up_odr(data, val, val2);
451 		if (ret)
452 			return ret;
453 	}
454 
455 	return kmx61_set_mode(data, mode, KMX61_ACC | KMX61_MAG, true);
456 }
457 
458 static int kmx61_get_odr(struct kmx61_data *data, int *val, int *val2,
459 			 u8 device)
460 {
461 	u8 lodr_bits;
462 
463 	if (device & KMX61_ACC)
464 		lodr_bits = (data->odr_bits >> KMX61_ACC_ODR_SHIFT) &
465 			     KMX61_ACC_ODR_MASK;
466 	else if (device & KMX61_MAG)
467 		lodr_bits = (data->odr_bits >> KMX61_MAG_ODR_SHIFT) &
468 			     KMX61_MAG_ODR_MASK;
469 	else
470 		return -EINVAL;
471 
472 	if (lodr_bits >= ARRAY_SIZE(kmx61_samp_freq_table))
473 		return -EINVAL;
474 
475 	*val = kmx61_samp_freq_table[lodr_bits].val;
476 	*val2 = kmx61_samp_freq_table[lodr_bits].val2;
477 
478 	return 0;
479 }
480 
481 static int kmx61_set_range(struct kmx61_data *data, u8 range)
482 {
483 	int ret;
484 
485 	ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
486 	if (ret < 0) {
487 		dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
488 		return ret;
489 	}
490 
491 	ret &= ~KMX61_REG_CTRL1_GSEL_MASK;
492 	ret |= range & KMX61_REG_CTRL1_GSEL_MASK;
493 
494 	ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
495 	if (ret < 0) {
496 		dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
497 		return ret;
498 	}
499 
500 	data->range = range;
501 
502 	return 0;
503 }
504 
505 static int kmx61_set_scale(struct kmx61_data *data, u16 uscale)
506 {
507 	int ret, i;
508 	u8  mode;
509 
510 	for (i = 0; i < ARRAY_SIZE(kmx61_uscale_table); i++) {
511 		if (kmx61_uscale_table[i] == uscale) {
512 			ret = kmx61_get_mode(data, &mode,
513 					     KMX61_ACC | KMX61_MAG);
514 			if (ret < 0)
515 				return ret;
516 
517 			ret = kmx61_set_mode(data, KMX61_ALL_STBY,
518 					     KMX61_ACC | KMX61_MAG, true);
519 			if (ret < 0)
520 				return ret;
521 
522 			ret = kmx61_set_range(data, i);
523 			if (ret < 0)
524 				return ret;
525 
526 			return  kmx61_set_mode(data, mode,
527 					       KMX61_ACC | KMX61_MAG, true);
528 		}
529 	}
530 	return -EINVAL;
531 }
532 
533 static int kmx61_chip_init(struct kmx61_data *data)
534 {
535 	int ret, val, val2;
536 
537 	ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_WHO_AM_I);
538 	if (ret < 0) {
539 		dev_err(&data->client->dev, "Error reading who_am_i\n");
540 		return ret;
541 	}
542 
543 	if (ret != KMX61_CHIP_ID) {
544 		dev_err(&data->client->dev,
545 			"Wrong chip id, got %x expected %x\n",
546 			 ret, KMX61_CHIP_ID);
547 		return -EINVAL;
548 	}
549 
550 	/* set accel 12bit, 4g range */
551 	ret = kmx61_set_range(data, KMX61_RANGE_4G);
552 	if (ret < 0)
553 		return ret;
554 
555 	ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_ODCNTL);
556 	if (ret < 0) {
557 		dev_err(&data->client->dev, "Error reading reg_odcntl\n");
558 		return ret;
559 	}
560 	data->odr_bits = ret;
561 
562 	/*
563 	 * set output data rate for wake up (motion detection) function
564 	 * to match data rate for accelerometer sampling
565 	 */
566 	ret = kmx61_get_odr(data, &val, &val2, KMX61_ACC);
567 	if (ret < 0)
568 		return ret;
569 
570 	ret = kmx61_set_wake_up_odr(data, val, val2);
571 	if (ret < 0)
572 		return ret;
573 
574 	/* set acc/magn to OPERATION mode */
575 	ret = kmx61_set_mode(data, 0, KMX61_ACC | KMX61_MAG, true);
576 	if (ret < 0)
577 		return ret;
578 
579 	data->wake_thresh = KMX61_DEFAULT_WAKE_THRESH;
580 	data->wake_duration = KMX61_DEFAULT_WAKE_DURATION;
581 
582 	return 0;
583 }
584 
585 static int kmx61_setup_new_data_interrupt(struct kmx61_data *data,
586 					  bool status, u8 device)
587 {
588 	u8 mode;
589 	int ret;
590 
591 	ret = kmx61_get_mode(data, &mode, KMX61_ACC | KMX61_MAG);
592 	if (ret < 0)
593 		return ret;
594 
595 	ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
596 	if (ret < 0)
597 		return ret;
598 
599 	ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INC1);
600 	if (ret < 0) {
601 		dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
602 		return ret;
603 	}
604 
605 	if (status) {
606 		ret |= KMX61_REG_INC1_BIT_IEN;
607 		if (device & KMX61_ACC)
608 			ret |= KMX61_REG_INC1_BIT_DRDYA;
609 		if (device & KMX61_MAG)
610 			ret |=  KMX61_REG_INC1_BIT_DRDYM;
611 	} else {
612 		ret &= ~KMX61_REG_INC1_BIT_IEN;
613 		if (device & KMX61_ACC)
614 			ret &= ~KMX61_REG_INC1_BIT_DRDYA;
615 		if (device & KMX61_MAG)
616 			ret &= ~KMX61_REG_INC1_BIT_DRDYM;
617 	}
618 	ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_INC1, ret);
619 	if (ret < 0) {
620 		dev_err(&data->client->dev, "Error writing reg_int_ctrl1\n");
621 		return ret;
622 	}
623 
624 	ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
625 	if (ret < 0) {
626 		dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
627 		return ret;
628 	}
629 
630 	if (status)
631 		ret |= KMX61_REG_CTRL1_BIT_DRDYE;
632 	else
633 		ret &= ~KMX61_REG_CTRL1_BIT_DRDYE;
634 
635 	ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
636 	if (ret < 0) {
637 		dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
638 		return ret;
639 	}
640 
641 	return kmx61_set_mode(data, mode, KMX61_ACC | KMX61_MAG, true);
642 }
643 
644 static int kmx61_chip_update_thresholds(struct kmx61_data *data)
645 {
646 	int ret;
647 
648 	ret = i2c_smbus_write_byte_data(data->client,
649 					KMX61_REG_WUF_TIMER,
650 					data->wake_duration);
651 	if (ret < 0) {
652 		dev_err(&data->client->dev, "Errow writing reg_wuf_timer\n");
653 		return ret;
654 	}
655 
656 	ret = i2c_smbus_write_byte_data(data->client,
657 					KMX61_REG_WUF_THRESH,
658 					data->wake_thresh);
659 	if (ret < 0)
660 		dev_err(&data->client->dev, "Error writing reg_wuf_thresh\n");
661 
662 	return ret;
663 }
664 
665 static int kmx61_setup_any_motion_interrupt(struct kmx61_data *data,
666 					    bool status)
667 {
668 	u8 mode;
669 	int ret;
670 
671 	ret = kmx61_get_mode(data, &mode, KMX61_ACC | KMX61_MAG);
672 	if (ret < 0)
673 		return ret;
674 
675 	ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
676 	if (ret < 0)
677 		return ret;
678 
679 	ret = kmx61_chip_update_thresholds(data);
680 	if (ret < 0)
681 		return ret;
682 
683 	ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INC1);
684 	if (ret < 0) {
685 		dev_err(&data->client->dev, "Error reading reg_inc1\n");
686 		return ret;
687 	}
688 	if (status)
689 		ret |= (KMX61_REG_INC1_BIT_IEN | KMX61_REG_INC1_BIT_WUFS);
690 	else
691 		ret &= ~(KMX61_REG_INC1_BIT_IEN | KMX61_REG_INC1_BIT_WUFS);
692 
693 	ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_INC1, ret);
694 	if (ret < 0) {
695 		dev_err(&data->client->dev, "Error writing reg_inc1\n");
696 		return ret;
697 	}
698 
699 	ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
700 	if (ret < 0) {
701 		dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
702 		return ret;
703 	}
704 
705 	if (status)
706 		ret |= KMX61_REG_CTRL1_BIT_WUFE | KMX61_REG_CTRL1_BIT_BTSE;
707 	else
708 		ret &= ~(KMX61_REG_CTRL1_BIT_WUFE | KMX61_REG_CTRL1_BIT_BTSE);
709 
710 	ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
711 	if (ret < 0) {
712 		dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
713 		return ret;
714 	}
715 	mode |= KMX61_ACT_STBY_BIT;
716 	return kmx61_set_mode(data, mode, KMX61_ACC | KMX61_MAG, true);
717 }
718 
719 /**
720  * kmx61_set_power_state() - set power state for kmx61 @device
721  * @data: kmx61 device private pointer
722  * @on: power state to be set for @device
723  * @device: bitmask indicating device for which @on state needs to be set
724  *
725  * Notice that when ACC power state needs to be set to ON and MAG is in
726  * OPERATION then we know that kmx61_runtime_resume was already called
727  * so we must set ACC OPERATION mode here. The same happens when MAG power
728  * state needs to be set to ON and ACC is in OPERATION.
729  */
730 static int kmx61_set_power_state(struct kmx61_data *data, bool on, u8 device)
731 {
732 #ifdef CONFIG_PM
733 	int ret;
734 
735 	if (device & KMX61_ACC) {
736 		if (on && !data->acc_ps && !data->mag_stby) {
737 			ret = kmx61_set_mode(data, 0, KMX61_ACC, true);
738 			if (ret < 0)
739 				return ret;
740 		}
741 		data->acc_ps = on;
742 	}
743 	if (device & KMX61_MAG) {
744 		if (on && !data->mag_ps && !data->acc_stby) {
745 			ret = kmx61_set_mode(data, 0, KMX61_MAG, true);
746 			if (ret < 0)
747 				return ret;
748 		}
749 		data->mag_ps = on;
750 	}
751 
752 	if (on) {
753 		ret = pm_runtime_get_sync(&data->client->dev);
754 	} else {
755 		pm_runtime_mark_last_busy(&data->client->dev);
756 		ret = pm_runtime_put_autosuspend(&data->client->dev);
757 	}
758 	if (ret < 0) {
759 		dev_err(&data->client->dev,
760 			"Failed: kmx61_set_power_state for %d, ret %d\n",
761 			on, ret);
762 		if (on)
763 			pm_runtime_put_noidle(&data->client->dev);
764 
765 		return ret;
766 	}
767 #endif
768 	return 0;
769 }
770 
771 static int kmx61_read_measurement(struct kmx61_data *data, u8 base, u8 offset)
772 {
773 	int ret;
774 	u8 reg = base + offset * 2;
775 
776 	ret = i2c_smbus_read_word_data(data->client, reg);
777 	if (ret < 0)
778 		dev_err(&data->client->dev, "failed to read reg at %x\n", reg);
779 
780 	return ret;
781 }
782 
783 static int kmx61_read_raw(struct iio_dev *indio_dev,
784 			  struct iio_chan_spec const *chan, int *val,
785 			  int *val2, long mask)
786 {
787 	int ret;
788 	u8 base_reg;
789 	struct kmx61_data *data = kmx61_get_data(indio_dev);
790 
791 	switch (mask) {
792 	case IIO_CHAN_INFO_RAW:
793 		switch (chan->type) {
794 		case IIO_ACCEL:
795 			base_reg = KMX61_ACC_XOUT_L;
796 			break;
797 		case IIO_MAGN:
798 			base_reg = KMX61_MAG_XOUT_L;
799 			break;
800 		default:
801 			return -EINVAL;
802 		}
803 		mutex_lock(&data->lock);
804 
805 		ret = kmx61_set_power_state(data, true, chan->address);
806 		if (ret) {
807 			mutex_unlock(&data->lock);
808 			return ret;
809 		}
810 
811 		ret = kmx61_read_measurement(data, base_reg, chan->scan_index);
812 		if (ret < 0) {
813 			kmx61_set_power_state(data, false, chan->address);
814 			mutex_unlock(&data->lock);
815 			return ret;
816 		}
817 		*val = sign_extend32(ret >> chan->scan_type.shift,
818 				     chan->scan_type.realbits - 1);
819 		ret = kmx61_set_power_state(data, false, chan->address);
820 
821 		mutex_unlock(&data->lock);
822 		if (ret)
823 			return ret;
824 		return IIO_VAL_INT;
825 	case IIO_CHAN_INFO_SCALE:
826 		switch (chan->type) {
827 		case IIO_ACCEL:
828 			*val = 0;
829 			*val2 = kmx61_uscale_table[data->range];
830 			return IIO_VAL_INT_PLUS_MICRO;
831 		case IIO_MAGN:
832 			/* 14 bits res, 1465 microGauss per magn count */
833 			*val = 0;
834 			*val2 = 1465;
835 			return IIO_VAL_INT_PLUS_MICRO;
836 		default:
837 			return -EINVAL;
838 		}
839 	case IIO_CHAN_INFO_SAMP_FREQ:
840 		if (chan->type != IIO_ACCEL && chan->type != IIO_MAGN)
841 			return -EINVAL;
842 
843 		mutex_lock(&data->lock);
844 		ret = kmx61_get_odr(data, val, val2, chan->address);
845 		mutex_unlock(&data->lock);
846 		if (ret)
847 			return -EINVAL;
848 		return IIO_VAL_INT_PLUS_MICRO;
849 	}
850 	return -EINVAL;
851 }
852 
853 static int kmx61_write_raw(struct iio_dev *indio_dev,
854 			   struct iio_chan_spec const *chan, int val,
855 			   int val2, long mask)
856 {
857 	int ret;
858 	struct kmx61_data *data = kmx61_get_data(indio_dev);
859 
860 	switch (mask) {
861 	case IIO_CHAN_INFO_SAMP_FREQ:
862 		if (chan->type != IIO_ACCEL && chan->type != IIO_MAGN)
863 			return -EINVAL;
864 
865 		mutex_lock(&data->lock);
866 		ret = kmx61_set_odr(data, val, val2, chan->address);
867 		mutex_unlock(&data->lock);
868 		return ret;
869 	case IIO_CHAN_INFO_SCALE:
870 		switch (chan->type) {
871 		case IIO_ACCEL:
872 			if (val != 0)
873 				return -EINVAL;
874 			mutex_lock(&data->lock);
875 			ret = kmx61_set_scale(data, val2);
876 			mutex_unlock(&data->lock);
877 			return ret;
878 		default:
879 			return -EINVAL;
880 		}
881 	default:
882 		return -EINVAL;
883 	}
884 }
885 
886 static int kmx61_read_event(struct iio_dev *indio_dev,
887 			    const struct iio_chan_spec *chan,
888 			    enum iio_event_type type,
889 			    enum iio_event_direction dir,
890 			    enum iio_event_info info,
891 			    int *val, int *val2)
892 {
893 	struct kmx61_data *data = kmx61_get_data(indio_dev);
894 
895 	*val2 = 0;
896 	switch (info) {
897 	case IIO_EV_INFO_VALUE:
898 		*val = data->wake_thresh;
899 		return IIO_VAL_INT;
900 	case IIO_EV_INFO_PERIOD:
901 		*val = data->wake_duration;
902 		return IIO_VAL_INT;
903 	default:
904 		return -EINVAL;
905 	}
906 }
907 
908 static int kmx61_write_event(struct iio_dev *indio_dev,
909 			     const struct iio_chan_spec *chan,
910 			     enum iio_event_type type,
911 			     enum iio_event_direction dir,
912 			     enum iio_event_info info,
913 			     int val, int val2)
914 {
915 	struct kmx61_data *data = kmx61_get_data(indio_dev);
916 
917 	if (data->ev_enable_state)
918 		return -EBUSY;
919 
920 	switch (info) {
921 	case IIO_EV_INFO_VALUE:
922 		data->wake_thresh = val;
923 		return IIO_VAL_INT;
924 	case IIO_EV_INFO_PERIOD:
925 		data->wake_duration = val;
926 		return IIO_VAL_INT;
927 	default:
928 		return -EINVAL;
929 	}
930 }
931 
932 static int kmx61_read_event_config(struct iio_dev *indio_dev,
933 				   const struct iio_chan_spec *chan,
934 				   enum iio_event_type type,
935 				   enum iio_event_direction dir)
936 {
937 	struct kmx61_data *data = kmx61_get_data(indio_dev);
938 
939 	return data->ev_enable_state;
940 }
941 
942 static int kmx61_write_event_config(struct iio_dev *indio_dev,
943 				    const struct iio_chan_spec *chan,
944 				    enum iio_event_type type,
945 				    enum iio_event_direction dir,
946 				    int state)
947 {
948 	struct kmx61_data *data = kmx61_get_data(indio_dev);
949 	int ret = 0;
950 
951 	if (state && data->ev_enable_state)
952 		return 0;
953 
954 	mutex_lock(&data->lock);
955 
956 	if (!state && data->motion_trig_on) {
957 		data->ev_enable_state = false;
958 		goto err_unlock;
959 	}
960 
961 	ret = kmx61_set_power_state(data, state, KMX61_ACC);
962 	if (ret < 0)
963 		goto err_unlock;
964 
965 	ret = kmx61_setup_any_motion_interrupt(data, state);
966 	if (ret < 0) {
967 		kmx61_set_power_state(data, false, KMX61_ACC);
968 		goto err_unlock;
969 	}
970 
971 	data->ev_enable_state = state;
972 
973 err_unlock:
974 	mutex_unlock(&data->lock);
975 
976 	return ret;
977 }
978 
979 static int kmx61_acc_validate_trigger(struct iio_dev *indio_dev,
980 				      struct iio_trigger *trig)
981 {
982 	struct kmx61_data *data = kmx61_get_data(indio_dev);
983 
984 	if (data->acc_dready_trig != trig && data->motion_trig != trig)
985 		return -EINVAL;
986 
987 	return 0;
988 }
989 
990 static int kmx61_mag_validate_trigger(struct iio_dev *indio_dev,
991 				      struct iio_trigger *trig)
992 {
993 	struct kmx61_data *data = kmx61_get_data(indio_dev);
994 
995 	if (data->mag_dready_trig != trig)
996 		return -EINVAL;
997 
998 	return 0;
999 }
1000 
1001 static const struct iio_info kmx61_acc_info = {
1002 	.read_raw		= kmx61_read_raw,
1003 	.write_raw		= kmx61_write_raw,
1004 	.attrs			= &kmx61_acc_attribute_group,
1005 	.read_event_value	= kmx61_read_event,
1006 	.write_event_value	= kmx61_write_event,
1007 	.read_event_config	= kmx61_read_event_config,
1008 	.write_event_config	= kmx61_write_event_config,
1009 	.validate_trigger	= kmx61_acc_validate_trigger,
1010 };
1011 
1012 static const struct iio_info kmx61_mag_info = {
1013 	.read_raw		= kmx61_read_raw,
1014 	.write_raw		= kmx61_write_raw,
1015 	.attrs			= &kmx61_mag_attribute_group,
1016 	.validate_trigger	= kmx61_mag_validate_trigger,
1017 };
1018 
1019 
1020 static int kmx61_data_rdy_trigger_set_state(struct iio_trigger *trig,
1021 					    bool state)
1022 {
1023 	int ret = 0;
1024 	u8 device;
1025 
1026 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
1027 	struct kmx61_data *data = kmx61_get_data(indio_dev);
1028 
1029 	mutex_lock(&data->lock);
1030 
1031 	if (!state && data->ev_enable_state && data->motion_trig_on) {
1032 		data->motion_trig_on = false;
1033 		goto err_unlock;
1034 	}
1035 
1036 	if (data->acc_dready_trig == trig || data->motion_trig == trig)
1037 		device = KMX61_ACC;
1038 	else
1039 		device = KMX61_MAG;
1040 
1041 	ret = kmx61_set_power_state(data, state, device);
1042 	if (ret < 0)
1043 		goto err_unlock;
1044 
1045 	if (data->acc_dready_trig == trig || data->mag_dready_trig == trig)
1046 		ret = kmx61_setup_new_data_interrupt(data, state, device);
1047 	else
1048 		ret = kmx61_setup_any_motion_interrupt(data, state);
1049 	if (ret < 0) {
1050 		kmx61_set_power_state(data, false, device);
1051 		goto err_unlock;
1052 	}
1053 
1054 	if (data->acc_dready_trig == trig)
1055 		data->acc_dready_trig_on = state;
1056 	else if (data->mag_dready_trig == trig)
1057 		data->mag_dready_trig_on = state;
1058 	else
1059 		data->motion_trig_on = state;
1060 err_unlock:
1061 	mutex_unlock(&data->lock);
1062 
1063 	return ret;
1064 }
1065 
1066 static int kmx61_trig_try_reenable(struct iio_trigger *trig)
1067 {
1068 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
1069 	struct kmx61_data *data = kmx61_get_data(indio_dev);
1070 	int ret;
1071 
1072 	ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INL);
1073 	if (ret < 0) {
1074 		dev_err(&data->client->dev, "Error reading reg_inl\n");
1075 		return ret;
1076 	}
1077 
1078 	return 0;
1079 }
1080 
1081 static const struct iio_trigger_ops kmx61_trigger_ops = {
1082 	.set_trigger_state = kmx61_data_rdy_trigger_set_state,
1083 	.try_reenable = kmx61_trig_try_reenable,
1084 };
1085 
1086 static irqreturn_t kmx61_event_handler(int irq, void *private)
1087 {
1088 	struct kmx61_data *data = private;
1089 	struct iio_dev *indio_dev = data->acc_indio_dev;
1090 	int ret;
1091 
1092 	ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INS1);
1093 	if (ret < 0) {
1094 		dev_err(&data->client->dev, "Error reading reg_ins1\n");
1095 		goto ack_intr;
1096 	}
1097 
1098 	if (ret & KMX61_REG_INS1_BIT_WUFS) {
1099 		ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INS2);
1100 		if (ret < 0) {
1101 			dev_err(&data->client->dev, "Error reading reg_ins2\n");
1102 			goto ack_intr;
1103 		}
1104 
1105 		if (ret & KMX61_REG_INS2_BIT_XN)
1106 			iio_push_event(indio_dev,
1107 				       IIO_MOD_EVENT_CODE(IIO_ACCEL,
1108 				       0,
1109 				       IIO_MOD_X,
1110 				       IIO_EV_TYPE_THRESH,
1111 				       IIO_EV_DIR_FALLING),
1112 				       0);
1113 
1114 		if (ret & KMX61_REG_INS2_BIT_XP)
1115 			iio_push_event(indio_dev,
1116 				       IIO_MOD_EVENT_CODE(IIO_ACCEL,
1117 				       0,
1118 				       IIO_MOD_X,
1119 				       IIO_EV_TYPE_THRESH,
1120 				       IIO_EV_DIR_RISING),
1121 				       0);
1122 
1123 		if (ret & KMX61_REG_INS2_BIT_YN)
1124 			iio_push_event(indio_dev,
1125 				       IIO_MOD_EVENT_CODE(IIO_ACCEL,
1126 				       0,
1127 				       IIO_MOD_Y,
1128 				       IIO_EV_TYPE_THRESH,
1129 				       IIO_EV_DIR_FALLING),
1130 				       0);
1131 
1132 		if (ret & KMX61_REG_INS2_BIT_YP)
1133 			iio_push_event(indio_dev,
1134 				       IIO_MOD_EVENT_CODE(IIO_ACCEL,
1135 				       0,
1136 				       IIO_MOD_Y,
1137 				       IIO_EV_TYPE_THRESH,
1138 				       IIO_EV_DIR_RISING),
1139 				       0);
1140 
1141 		if (ret & KMX61_REG_INS2_BIT_ZN)
1142 			iio_push_event(indio_dev,
1143 				       IIO_MOD_EVENT_CODE(IIO_ACCEL,
1144 				       0,
1145 				       IIO_MOD_Z,
1146 				       IIO_EV_TYPE_THRESH,
1147 				       IIO_EV_DIR_FALLING),
1148 				       0);
1149 
1150 		if (ret & KMX61_REG_INS2_BIT_ZP)
1151 			iio_push_event(indio_dev,
1152 				       IIO_MOD_EVENT_CODE(IIO_ACCEL,
1153 				       0,
1154 				       IIO_MOD_Z,
1155 				       IIO_EV_TYPE_THRESH,
1156 				       IIO_EV_DIR_RISING),
1157 				       0);
1158 	}
1159 
1160 ack_intr:
1161 	ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
1162 	if (ret < 0)
1163 		dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
1164 
1165 	ret |= KMX61_REG_CTRL1_BIT_RES;
1166 	ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
1167 	if (ret < 0)
1168 		dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
1169 
1170 	ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INL);
1171 	if (ret < 0)
1172 		dev_err(&data->client->dev, "Error reading reg_inl\n");
1173 
1174 	return IRQ_HANDLED;
1175 }
1176 
1177 static irqreturn_t kmx61_data_rdy_trig_poll(int irq, void *private)
1178 {
1179 	struct kmx61_data *data = private;
1180 
1181 	if (data->acc_dready_trig_on)
1182 		iio_trigger_poll(data->acc_dready_trig);
1183 	if (data->mag_dready_trig_on)
1184 		iio_trigger_poll(data->mag_dready_trig);
1185 
1186 	if (data->motion_trig_on)
1187 		iio_trigger_poll(data->motion_trig);
1188 
1189 	if (data->ev_enable_state)
1190 		return IRQ_WAKE_THREAD;
1191 	return IRQ_HANDLED;
1192 }
1193 
1194 static irqreturn_t kmx61_trigger_handler(int irq, void *p)
1195 {
1196 	struct iio_poll_func *pf = p;
1197 	struct iio_dev *indio_dev = pf->indio_dev;
1198 	struct kmx61_data *data = kmx61_get_data(indio_dev);
1199 	int bit, ret, i = 0;
1200 	u8 base;
1201 	s16 buffer[8];
1202 
1203 	if (indio_dev == data->acc_indio_dev)
1204 		base = KMX61_ACC_XOUT_L;
1205 	else
1206 		base = KMX61_MAG_XOUT_L;
1207 
1208 	mutex_lock(&data->lock);
1209 	for_each_set_bit(bit, indio_dev->active_scan_mask,
1210 			 indio_dev->masklength) {
1211 		ret = kmx61_read_measurement(data, base, bit);
1212 		if (ret < 0) {
1213 			mutex_unlock(&data->lock);
1214 			goto err;
1215 		}
1216 		buffer[i++] = ret;
1217 	}
1218 	mutex_unlock(&data->lock);
1219 
1220 	iio_push_to_buffers(indio_dev, buffer);
1221 err:
1222 	iio_trigger_notify_done(indio_dev->trig);
1223 
1224 	return IRQ_HANDLED;
1225 }
1226 
1227 static const char *kmx61_match_acpi_device(struct device *dev)
1228 {
1229 	const struct acpi_device_id *id;
1230 
1231 	id = acpi_match_device(dev->driver->acpi_match_table, dev);
1232 	if (!id)
1233 		return NULL;
1234 	return dev_name(dev);
1235 }
1236 
1237 static struct iio_dev *kmx61_indiodev_setup(struct kmx61_data *data,
1238 					    const struct iio_info *info,
1239 					    const struct iio_chan_spec *chan,
1240 					    int num_channels,
1241 					    const char *name)
1242 {
1243 	struct iio_dev *indio_dev;
1244 
1245 	indio_dev = devm_iio_device_alloc(&data->client->dev, sizeof(data));
1246 	if (!indio_dev)
1247 		return ERR_PTR(-ENOMEM);
1248 
1249 	kmx61_set_data(indio_dev, data);
1250 
1251 	indio_dev->channels = chan;
1252 	indio_dev->num_channels = num_channels;
1253 	indio_dev->name = name;
1254 	indio_dev->modes = INDIO_DIRECT_MODE;
1255 	indio_dev->info = info;
1256 
1257 	return indio_dev;
1258 }
1259 
1260 static struct iio_trigger *kmx61_trigger_setup(struct kmx61_data *data,
1261 					       struct iio_dev *indio_dev,
1262 					       const char *tag)
1263 {
1264 	struct iio_trigger *trig;
1265 	int ret;
1266 
1267 	trig = devm_iio_trigger_alloc(&data->client->dev,
1268 				      "%s-%s-dev%d",
1269 				      indio_dev->name,
1270 				      tag,
1271 				      indio_dev->id);
1272 	if (!trig)
1273 		return ERR_PTR(-ENOMEM);
1274 
1275 	trig->dev.parent = &data->client->dev;
1276 	trig->ops = &kmx61_trigger_ops;
1277 	iio_trigger_set_drvdata(trig, indio_dev);
1278 
1279 	ret = iio_trigger_register(trig);
1280 	if (ret)
1281 		return ERR_PTR(ret);
1282 
1283 	return trig;
1284 }
1285 
1286 static int kmx61_probe(struct i2c_client *client,
1287 		       const struct i2c_device_id *id)
1288 {
1289 	int ret;
1290 	struct kmx61_data *data;
1291 	const char *name = NULL;
1292 
1293 	data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
1294 	if (!data)
1295 		return -ENOMEM;
1296 
1297 	i2c_set_clientdata(client, data);
1298 	data->client = client;
1299 
1300 	mutex_init(&data->lock);
1301 
1302 	if (id)
1303 		name = id->name;
1304 	else if (ACPI_HANDLE(&client->dev))
1305 		name = kmx61_match_acpi_device(&client->dev);
1306 	else
1307 		return -ENODEV;
1308 
1309 	data->acc_indio_dev =
1310 		kmx61_indiodev_setup(data, &kmx61_acc_info,
1311 				     kmx61_acc_channels,
1312 				     ARRAY_SIZE(kmx61_acc_channels),
1313 				     name);
1314 	if (IS_ERR(data->acc_indio_dev))
1315 		return PTR_ERR(data->acc_indio_dev);
1316 
1317 	data->mag_indio_dev =
1318 		kmx61_indiodev_setup(data, &kmx61_mag_info,
1319 				     kmx61_mag_channels,
1320 				     ARRAY_SIZE(kmx61_mag_channels),
1321 				     name);
1322 	if (IS_ERR(data->mag_indio_dev))
1323 		return PTR_ERR(data->mag_indio_dev);
1324 
1325 	ret = kmx61_chip_init(data);
1326 	if (ret < 0)
1327 		return ret;
1328 
1329 	if (client->irq > 0) {
1330 		ret = devm_request_threaded_irq(&client->dev, client->irq,
1331 						kmx61_data_rdy_trig_poll,
1332 						kmx61_event_handler,
1333 						IRQF_TRIGGER_RISING,
1334 						KMX61_IRQ_NAME,
1335 						data);
1336 		if (ret)
1337 			goto err_chip_uninit;
1338 
1339 		data->acc_dready_trig =
1340 			kmx61_trigger_setup(data, data->acc_indio_dev,
1341 					    "dready");
1342 		if (IS_ERR(data->acc_dready_trig)) {
1343 			ret = PTR_ERR(data->acc_dready_trig);
1344 			goto err_chip_uninit;
1345 		}
1346 
1347 		data->mag_dready_trig =
1348 			kmx61_trigger_setup(data, data->mag_indio_dev,
1349 					    "dready");
1350 		if (IS_ERR(data->mag_dready_trig)) {
1351 			ret = PTR_ERR(data->mag_dready_trig);
1352 			goto err_trigger_unregister_acc_dready;
1353 		}
1354 
1355 		data->motion_trig =
1356 			kmx61_trigger_setup(data, data->acc_indio_dev,
1357 					    "any-motion");
1358 		if (IS_ERR(data->motion_trig)) {
1359 			ret = PTR_ERR(data->motion_trig);
1360 			goto err_trigger_unregister_mag_dready;
1361 		}
1362 
1363 		ret = iio_triggered_buffer_setup(data->acc_indio_dev,
1364 						 &iio_pollfunc_store_time,
1365 						 kmx61_trigger_handler,
1366 						 NULL);
1367 		if (ret < 0) {
1368 			dev_err(&data->client->dev,
1369 				"Failed to setup acc triggered buffer\n");
1370 			goto err_trigger_unregister_motion;
1371 		}
1372 
1373 		ret = iio_triggered_buffer_setup(data->mag_indio_dev,
1374 						 &iio_pollfunc_store_time,
1375 						 kmx61_trigger_handler,
1376 						 NULL);
1377 		if (ret < 0) {
1378 			dev_err(&data->client->dev,
1379 				"Failed to setup mag triggered buffer\n");
1380 			goto err_buffer_cleanup_acc;
1381 		}
1382 	}
1383 
1384 	ret = pm_runtime_set_active(&client->dev);
1385 	if (ret < 0)
1386 		goto err_buffer_cleanup_mag;
1387 
1388 	pm_runtime_enable(&client->dev);
1389 	pm_runtime_set_autosuspend_delay(&client->dev, KMX61_SLEEP_DELAY_MS);
1390 	pm_runtime_use_autosuspend(&client->dev);
1391 
1392 	ret = iio_device_register(data->acc_indio_dev);
1393 	if (ret < 0) {
1394 		dev_err(&client->dev, "Failed to register acc iio device\n");
1395 		goto err_buffer_cleanup_mag;
1396 	}
1397 
1398 	ret = iio_device_register(data->mag_indio_dev);
1399 	if (ret < 0) {
1400 		dev_err(&client->dev, "Failed to register mag iio device\n");
1401 		goto err_iio_unregister_acc;
1402 	}
1403 
1404 	return 0;
1405 
1406 err_iio_unregister_acc:
1407 	iio_device_unregister(data->acc_indio_dev);
1408 err_buffer_cleanup_mag:
1409 	if (client->irq > 0)
1410 		iio_triggered_buffer_cleanup(data->mag_indio_dev);
1411 err_buffer_cleanup_acc:
1412 	if (client->irq > 0)
1413 		iio_triggered_buffer_cleanup(data->acc_indio_dev);
1414 err_trigger_unregister_motion:
1415 	iio_trigger_unregister(data->motion_trig);
1416 err_trigger_unregister_mag_dready:
1417 	iio_trigger_unregister(data->mag_dready_trig);
1418 err_trigger_unregister_acc_dready:
1419 	iio_trigger_unregister(data->acc_dready_trig);
1420 err_chip_uninit:
1421 	kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
1422 	return ret;
1423 }
1424 
1425 static int kmx61_remove(struct i2c_client *client)
1426 {
1427 	struct kmx61_data *data = i2c_get_clientdata(client);
1428 
1429 	iio_device_unregister(data->acc_indio_dev);
1430 	iio_device_unregister(data->mag_indio_dev);
1431 
1432 	pm_runtime_disable(&client->dev);
1433 	pm_runtime_set_suspended(&client->dev);
1434 	pm_runtime_put_noidle(&client->dev);
1435 
1436 	if (client->irq > 0) {
1437 		iio_triggered_buffer_cleanup(data->acc_indio_dev);
1438 		iio_triggered_buffer_cleanup(data->mag_indio_dev);
1439 		iio_trigger_unregister(data->acc_dready_trig);
1440 		iio_trigger_unregister(data->mag_dready_trig);
1441 		iio_trigger_unregister(data->motion_trig);
1442 	}
1443 
1444 	mutex_lock(&data->lock);
1445 	kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
1446 	mutex_unlock(&data->lock);
1447 
1448 	return 0;
1449 }
1450 
1451 #ifdef CONFIG_PM_SLEEP
1452 static int kmx61_suspend(struct device *dev)
1453 {
1454 	int ret;
1455 	struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
1456 
1457 	mutex_lock(&data->lock);
1458 	ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG,
1459 			     false);
1460 	mutex_unlock(&data->lock);
1461 
1462 	return ret;
1463 }
1464 
1465 static int kmx61_resume(struct device *dev)
1466 {
1467 	u8 stby = 0;
1468 	struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
1469 
1470 	if (data->acc_stby)
1471 		stby |= KMX61_ACC_STBY_BIT;
1472 	if (data->mag_stby)
1473 		stby |= KMX61_MAG_STBY_BIT;
1474 
1475 	return kmx61_set_mode(data, stby, KMX61_ACC | KMX61_MAG, true);
1476 }
1477 #endif
1478 
1479 #ifdef CONFIG_PM
1480 static int kmx61_runtime_suspend(struct device *dev)
1481 {
1482 	struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
1483 	int ret;
1484 
1485 	mutex_lock(&data->lock);
1486 	ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
1487 	mutex_unlock(&data->lock);
1488 
1489 	return ret;
1490 }
1491 
1492 static int kmx61_runtime_resume(struct device *dev)
1493 {
1494 	struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
1495 	u8 stby = 0;
1496 
1497 	if (!data->acc_ps)
1498 		stby |= KMX61_ACC_STBY_BIT;
1499 	if (!data->mag_ps)
1500 		stby |= KMX61_MAG_STBY_BIT;
1501 
1502 	return kmx61_set_mode(data, stby, KMX61_ACC | KMX61_MAG, true);
1503 }
1504 #endif
1505 
1506 static const struct dev_pm_ops kmx61_pm_ops = {
1507 	SET_SYSTEM_SLEEP_PM_OPS(kmx61_suspend, kmx61_resume)
1508 	SET_RUNTIME_PM_OPS(kmx61_runtime_suspend, kmx61_runtime_resume, NULL)
1509 };
1510 
1511 static const struct acpi_device_id kmx61_acpi_match[] = {
1512 	{"KMX61021", 0},
1513 	{}
1514 };
1515 
1516 MODULE_DEVICE_TABLE(acpi, kmx61_acpi_match);
1517 
1518 static const struct i2c_device_id kmx61_id[] = {
1519 	{"kmx611021", 0},
1520 	{}
1521 };
1522 
1523 MODULE_DEVICE_TABLE(i2c, kmx61_id);
1524 
1525 static struct i2c_driver kmx61_driver = {
1526 	.driver = {
1527 		.name = KMX61_DRV_NAME,
1528 		.acpi_match_table = ACPI_PTR(kmx61_acpi_match),
1529 		.pm = &kmx61_pm_ops,
1530 	},
1531 	.probe		= kmx61_probe,
1532 	.remove		= kmx61_remove,
1533 	.id_table	= kmx61_id,
1534 };
1535 
1536 module_i2c_driver(kmx61_driver);
1537 
1538 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
1539 MODULE_DESCRIPTION("KMX61 accelerometer/magnetometer driver");
1540 MODULE_LICENSE("GPL v2");
1541