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