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