xref: /linux/drivers/iio/imu/kmx61.c (revision 87c9c16317882dd6dbbc07e349bc3223e14f3244)
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 void kmx61_trig_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 }
1076 
1077 static const struct iio_trigger_ops kmx61_trigger_ops = {
1078 	.set_trigger_state = kmx61_data_rdy_trigger_set_state,
1079 	.reenable = kmx61_trig_reenable,
1080 };
1081 
1082 static irqreturn_t kmx61_event_handler(int irq, void *private)
1083 {
1084 	struct kmx61_data *data = private;
1085 	struct iio_dev *indio_dev = data->acc_indio_dev;
1086 	int ret;
1087 
1088 	ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INS1);
1089 	if (ret < 0) {
1090 		dev_err(&data->client->dev, "Error reading reg_ins1\n");
1091 		goto ack_intr;
1092 	}
1093 
1094 	if (ret & KMX61_REG_INS1_BIT_WUFS) {
1095 		ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INS2);
1096 		if (ret < 0) {
1097 			dev_err(&data->client->dev, "Error reading reg_ins2\n");
1098 			goto ack_intr;
1099 		}
1100 
1101 		if (ret & KMX61_REG_INS2_BIT_XN)
1102 			iio_push_event(indio_dev,
1103 				       IIO_MOD_EVENT_CODE(IIO_ACCEL,
1104 				       0,
1105 				       IIO_MOD_X,
1106 				       IIO_EV_TYPE_THRESH,
1107 				       IIO_EV_DIR_FALLING),
1108 				       0);
1109 
1110 		if (ret & KMX61_REG_INS2_BIT_XP)
1111 			iio_push_event(indio_dev,
1112 				       IIO_MOD_EVENT_CODE(IIO_ACCEL,
1113 				       0,
1114 				       IIO_MOD_X,
1115 				       IIO_EV_TYPE_THRESH,
1116 				       IIO_EV_DIR_RISING),
1117 				       0);
1118 
1119 		if (ret & KMX61_REG_INS2_BIT_YN)
1120 			iio_push_event(indio_dev,
1121 				       IIO_MOD_EVENT_CODE(IIO_ACCEL,
1122 				       0,
1123 				       IIO_MOD_Y,
1124 				       IIO_EV_TYPE_THRESH,
1125 				       IIO_EV_DIR_FALLING),
1126 				       0);
1127 
1128 		if (ret & KMX61_REG_INS2_BIT_YP)
1129 			iio_push_event(indio_dev,
1130 				       IIO_MOD_EVENT_CODE(IIO_ACCEL,
1131 				       0,
1132 				       IIO_MOD_Y,
1133 				       IIO_EV_TYPE_THRESH,
1134 				       IIO_EV_DIR_RISING),
1135 				       0);
1136 
1137 		if (ret & KMX61_REG_INS2_BIT_ZN)
1138 			iio_push_event(indio_dev,
1139 				       IIO_MOD_EVENT_CODE(IIO_ACCEL,
1140 				       0,
1141 				       IIO_MOD_Z,
1142 				       IIO_EV_TYPE_THRESH,
1143 				       IIO_EV_DIR_FALLING),
1144 				       0);
1145 
1146 		if (ret & KMX61_REG_INS2_BIT_ZP)
1147 			iio_push_event(indio_dev,
1148 				       IIO_MOD_EVENT_CODE(IIO_ACCEL,
1149 				       0,
1150 				       IIO_MOD_Z,
1151 				       IIO_EV_TYPE_THRESH,
1152 				       IIO_EV_DIR_RISING),
1153 				       0);
1154 	}
1155 
1156 ack_intr:
1157 	ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
1158 	if (ret < 0)
1159 		dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
1160 
1161 	ret |= KMX61_REG_CTRL1_BIT_RES;
1162 	ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
1163 	if (ret < 0)
1164 		dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
1165 
1166 	ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INL);
1167 	if (ret < 0)
1168 		dev_err(&data->client->dev, "Error reading reg_inl\n");
1169 
1170 	return IRQ_HANDLED;
1171 }
1172 
1173 static irqreturn_t kmx61_data_rdy_trig_poll(int irq, void *private)
1174 {
1175 	struct kmx61_data *data = private;
1176 
1177 	if (data->acc_dready_trig_on)
1178 		iio_trigger_poll(data->acc_dready_trig);
1179 	if (data->mag_dready_trig_on)
1180 		iio_trigger_poll(data->mag_dready_trig);
1181 
1182 	if (data->motion_trig_on)
1183 		iio_trigger_poll(data->motion_trig);
1184 
1185 	if (data->ev_enable_state)
1186 		return IRQ_WAKE_THREAD;
1187 	return IRQ_HANDLED;
1188 }
1189 
1190 static irqreturn_t kmx61_trigger_handler(int irq, void *p)
1191 {
1192 	struct iio_poll_func *pf = p;
1193 	struct iio_dev *indio_dev = pf->indio_dev;
1194 	struct kmx61_data *data = kmx61_get_data(indio_dev);
1195 	int bit, ret, i = 0;
1196 	u8 base;
1197 	s16 buffer[8];
1198 
1199 	if (indio_dev == data->acc_indio_dev)
1200 		base = KMX61_ACC_XOUT_L;
1201 	else
1202 		base = KMX61_MAG_XOUT_L;
1203 
1204 	mutex_lock(&data->lock);
1205 	for_each_set_bit(bit, indio_dev->active_scan_mask,
1206 			 indio_dev->masklength) {
1207 		ret = kmx61_read_measurement(data, base, bit);
1208 		if (ret < 0) {
1209 			mutex_unlock(&data->lock);
1210 			goto err;
1211 		}
1212 		buffer[i++] = ret;
1213 	}
1214 	mutex_unlock(&data->lock);
1215 
1216 	iio_push_to_buffers(indio_dev, buffer);
1217 err:
1218 	iio_trigger_notify_done(indio_dev->trig);
1219 
1220 	return IRQ_HANDLED;
1221 }
1222 
1223 static const char *kmx61_match_acpi_device(struct device *dev)
1224 {
1225 	const struct acpi_device_id *id;
1226 
1227 	id = acpi_match_device(dev->driver->acpi_match_table, dev);
1228 	if (!id)
1229 		return NULL;
1230 	return dev_name(dev);
1231 }
1232 
1233 static struct iio_dev *kmx61_indiodev_setup(struct kmx61_data *data,
1234 					    const struct iio_info *info,
1235 					    const struct iio_chan_spec *chan,
1236 					    int num_channels,
1237 					    const char *name)
1238 {
1239 	struct iio_dev *indio_dev;
1240 
1241 	indio_dev = devm_iio_device_alloc(&data->client->dev, sizeof(data));
1242 	if (!indio_dev)
1243 		return ERR_PTR(-ENOMEM);
1244 
1245 	kmx61_set_data(indio_dev, data);
1246 
1247 	indio_dev->channels = chan;
1248 	indio_dev->num_channels = num_channels;
1249 	indio_dev->name = name;
1250 	indio_dev->modes = INDIO_DIRECT_MODE;
1251 	indio_dev->info = info;
1252 
1253 	return indio_dev;
1254 }
1255 
1256 static struct iio_trigger *kmx61_trigger_setup(struct kmx61_data *data,
1257 					       struct iio_dev *indio_dev,
1258 					       const char *tag)
1259 {
1260 	struct iio_trigger *trig;
1261 	int ret;
1262 
1263 	trig = devm_iio_trigger_alloc(&data->client->dev,
1264 				      "%s-%s-dev%d",
1265 				      indio_dev->name,
1266 				      tag,
1267 				      indio_dev->id);
1268 	if (!trig)
1269 		return ERR_PTR(-ENOMEM);
1270 
1271 	trig->ops = &kmx61_trigger_ops;
1272 	iio_trigger_set_drvdata(trig, indio_dev);
1273 
1274 	ret = iio_trigger_register(trig);
1275 	if (ret)
1276 		return ERR_PTR(ret);
1277 
1278 	return trig;
1279 }
1280 
1281 static int kmx61_probe(struct i2c_client *client,
1282 		       const struct i2c_device_id *id)
1283 {
1284 	int ret;
1285 	struct kmx61_data *data;
1286 	const char *name = NULL;
1287 
1288 	data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
1289 	if (!data)
1290 		return -ENOMEM;
1291 
1292 	i2c_set_clientdata(client, data);
1293 	data->client = client;
1294 
1295 	mutex_init(&data->lock);
1296 
1297 	if (id)
1298 		name = id->name;
1299 	else if (ACPI_HANDLE(&client->dev))
1300 		name = kmx61_match_acpi_device(&client->dev);
1301 	else
1302 		return -ENODEV;
1303 
1304 	data->acc_indio_dev =
1305 		kmx61_indiodev_setup(data, &kmx61_acc_info,
1306 				     kmx61_acc_channels,
1307 				     ARRAY_SIZE(kmx61_acc_channels),
1308 				     name);
1309 	if (IS_ERR(data->acc_indio_dev))
1310 		return PTR_ERR(data->acc_indio_dev);
1311 
1312 	data->mag_indio_dev =
1313 		kmx61_indiodev_setup(data, &kmx61_mag_info,
1314 				     kmx61_mag_channels,
1315 				     ARRAY_SIZE(kmx61_mag_channels),
1316 				     name);
1317 	if (IS_ERR(data->mag_indio_dev))
1318 		return PTR_ERR(data->mag_indio_dev);
1319 
1320 	ret = kmx61_chip_init(data);
1321 	if (ret < 0)
1322 		return ret;
1323 
1324 	if (client->irq > 0) {
1325 		ret = devm_request_threaded_irq(&client->dev, client->irq,
1326 						kmx61_data_rdy_trig_poll,
1327 						kmx61_event_handler,
1328 						IRQF_TRIGGER_RISING,
1329 						KMX61_IRQ_NAME,
1330 						data);
1331 		if (ret)
1332 			goto err_chip_uninit;
1333 
1334 		data->acc_dready_trig =
1335 			kmx61_trigger_setup(data, data->acc_indio_dev,
1336 					    "dready");
1337 		if (IS_ERR(data->acc_dready_trig)) {
1338 			ret = PTR_ERR(data->acc_dready_trig);
1339 			goto err_chip_uninit;
1340 		}
1341 
1342 		data->mag_dready_trig =
1343 			kmx61_trigger_setup(data, data->mag_indio_dev,
1344 					    "dready");
1345 		if (IS_ERR(data->mag_dready_trig)) {
1346 			ret = PTR_ERR(data->mag_dready_trig);
1347 			goto err_trigger_unregister_acc_dready;
1348 		}
1349 
1350 		data->motion_trig =
1351 			kmx61_trigger_setup(data, data->acc_indio_dev,
1352 					    "any-motion");
1353 		if (IS_ERR(data->motion_trig)) {
1354 			ret = PTR_ERR(data->motion_trig);
1355 			goto err_trigger_unregister_mag_dready;
1356 		}
1357 
1358 		ret = iio_triggered_buffer_setup(data->acc_indio_dev,
1359 						 &iio_pollfunc_store_time,
1360 						 kmx61_trigger_handler,
1361 						 NULL);
1362 		if (ret < 0) {
1363 			dev_err(&data->client->dev,
1364 				"Failed to setup acc triggered buffer\n");
1365 			goto err_trigger_unregister_motion;
1366 		}
1367 
1368 		ret = iio_triggered_buffer_setup(data->mag_indio_dev,
1369 						 &iio_pollfunc_store_time,
1370 						 kmx61_trigger_handler,
1371 						 NULL);
1372 		if (ret < 0) {
1373 			dev_err(&data->client->dev,
1374 				"Failed to setup mag triggered buffer\n");
1375 			goto err_buffer_cleanup_acc;
1376 		}
1377 	}
1378 
1379 	ret = pm_runtime_set_active(&client->dev);
1380 	if (ret < 0)
1381 		goto err_buffer_cleanup_mag;
1382 
1383 	pm_runtime_enable(&client->dev);
1384 	pm_runtime_set_autosuspend_delay(&client->dev, KMX61_SLEEP_DELAY_MS);
1385 	pm_runtime_use_autosuspend(&client->dev);
1386 
1387 	ret = iio_device_register(data->acc_indio_dev);
1388 	if (ret < 0) {
1389 		dev_err(&client->dev, "Failed to register acc iio device\n");
1390 		goto err_buffer_cleanup_mag;
1391 	}
1392 
1393 	ret = iio_device_register(data->mag_indio_dev);
1394 	if (ret < 0) {
1395 		dev_err(&client->dev, "Failed to register mag iio device\n");
1396 		goto err_iio_unregister_acc;
1397 	}
1398 
1399 	return 0;
1400 
1401 err_iio_unregister_acc:
1402 	iio_device_unregister(data->acc_indio_dev);
1403 err_buffer_cleanup_mag:
1404 	if (client->irq > 0)
1405 		iio_triggered_buffer_cleanup(data->mag_indio_dev);
1406 err_buffer_cleanup_acc:
1407 	if (client->irq > 0)
1408 		iio_triggered_buffer_cleanup(data->acc_indio_dev);
1409 err_trigger_unregister_motion:
1410 	iio_trigger_unregister(data->motion_trig);
1411 err_trigger_unregister_mag_dready:
1412 	iio_trigger_unregister(data->mag_dready_trig);
1413 err_trigger_unregister_acc_dready:
1414 	iio_trigger_unregister(data->acc_dready_trig);
1415 err_chip_uninit:
1416 	kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
1417 	return ret;
1418 }
1419 
1420 static int kmx61_remove(struct i2c_client *client)
1421 {
1422 	struct kmx61_data *data = i2c_get_clientdata(client);
1423 
1424 	iio_device_unregister(data->acc_indio_dev);
1425 	iio_device_unregister(data->mag_indio_dev);
1426 
1427 	pm_runtime_disable(&client->dev);
1428 	pm_runtime_set_suspended(&client->dev);
1429 	pm_runtime_put_noidle(&client->dev);
1430 
1431 	if (client->irq > 0) {
1432 		iio_triggered_buffer_cleanup(data->acc_indio_dev);
1433 		iio_triggered_buffer_cleanup(data->mag_indio_dev);
1434 		iio_trigger_unregister(data->acc_dready_trig);
1435 		iio_trigger_unregister(data->mag_dready_trig);
1436 		iio_trigger_unregister(data->motion_trig);
1437 	}
1438 
1439 	mutex_lock(&data->lock);
1440 	kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
1441 	mutex_unlock(&data->lock);
1442 
1443 	return 0;
1444 }
1445 
1446 #ifdef CONFIG_PM_SLEEP
1447 static int kmx61_suspend(struct device *dev)
1448 {
1449 	int ret;
1450 	struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
1451 
1452 	mutex_lock(&data->lock);
1453 	ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG,
1454 			     false);
1455 	mutex_unlock(&data->lock);
1456 
1457 	return ret;
1458 }
1459 
1460 static int kmx61_resume(struct device *dev)
1461 {
1462 	u8 stby = 0;
1463 	struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
1464 
1465 	if (data->acc_stby)
1466 		stby |= KMX61_ACC_STBY_BIT;
1467 	if (data->mag_stby)
1468 		stby |= KMX61_MAG_STBY_BIT;
1469 
1470 	return kmx61_set_mode(data, stby, KMX61_ACC | KMX61_MAG, true);
1471 }
1472 #endif
1473 
1474 #ifdef CONFIG_PM
1475 static int kmx61_runtime_suspend(struct device *dev)
1476 {
1477 	struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
1478 	int ret;
1479 
1480 	mutex_lock(&data->lock);
1481 	ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
1482 	mutex_unlock(&data->lock);
1483 
1484 	return ret;
1485 }
1486 
1487 static int kmx61_runtime_resume(struct device *dev)
1488 {
1489 	struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
1490 	u8 stby = 0;
1491 
1492 	if (!data->acc_ps)
1493 		stby |= KMX61_ACC_STBY_BIT;
1494 	if (!data->mag_ps)
1495 		stby |= KMX61_MAG_STBY_BIT;
1496 
1497 	return kmx61_set_mode(data, stby, KMX61_ACC | KMX61_MAG, true);
1498 }
1499 #endif
1500 
1501 static const struct dev_pm_ops kmx61_pm_ops = {
1502 	SET_SYSTEM_SLEEP_PM_OPS(kmx61_suspend, kmx61_resume)
1503 	SET_RUNTIME_PM_OPS(kmx61_runtime_suspend, kmx61_runtime_resume, NULL)
1504 };
1505 
1506 static const struct acpi_device_id kmx61_acpi_match[] = {
1507 	{"KMX61021", 0},
1508 	{}
1509 };
1510 
1511 MODULE_DEVICE_TABLE(acpi, kmx61_acpi_match);
1512 
1513 static const struct i2c_device_id kmx61_id[] = {
1514 	{"kmx611021", 0},
1515 	{}
1516 };
1517 
1518 MODULE_DEVICE_TABLE(i2c, kmx61_id);
1519 
1520 static struct i2c_driver kmx61_driver = {
1521 	.driver = {
1522 		.name = KMX61_DRV_NAME,
1523 		.acpi_match_table = ACPI_PTR(kmx61_acpi_match),
1524 		.pm = &kmx61_pm_ops,
1525 	},
1526 	.probe		= kmx61_probe,
1527 	.remove		= kmx61_remove,
1528 	.id_table	= kmx61_id,
1529 };
1530 
1531 module_i2c_driver(kmx61_driver);
1532 
1533 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
1534 MODULE_DESCRIPTION("KMX61 accelerometer/magnetometer driver");
1535 MODULE_LICENSE("GPL v2");
1536