xref: /openbmc/linux/drivers/iio/accel/kxcjk-1013.c (revision 249592bf)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * KXCJK-1013 3-axis accelerometer driver
4  * Copyright (c) 2014, Intel Corporation.
5  */
6 
7 #include <linux/module.h>
8 #include <linux/i2c.h>
9 #include <linux/interrupt.h>
10 #include <linux/delay.h>
11 #include <linux/bitops.h>
12 #include <linux/slab.h>
13 #include <linux/string.h>
14 #include <linux/acpi.h>
15 #include <linux/pm.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/sysfs.h>
20 #include <linux/iio/buffer.h>
21 #include <linux/iio/trigger.h>
22 #include <linux/iio/events.h>
23 #include <linux/iio/trigger_consumer.h>
24 #include <linux/iio/triggered_buffer.h>
25 #include <linux/iio/accel/kxcjk_1013.h>
26 
27 #define KXCJK1013_DRV_NAME "kxcjk1013"
28 #define KXCJK1013_IRQ_NAME "kxcjk1013_event"
29 
30 #define KXTF9_REG_HP_XOUT_L		0x00
31 #define KXTF9_REG_HP_XOUT_H		0x01
32 #define KXTF9_REG_HP_YOUT_L		0x02
33 #define KXTF9_REG_HP_YOUT_H		0x03
34 #define KXTF9_REG_HP_ZOUT_L		0x04
35 #define KXTF9_REG_HP_ZOUT_H		0x05
36 
37 #define KXCJK1013_REG_XOUT_L		0x06
38 /*
39  * From low byte X axis register, all the other addresses of Y and Z can be
40  * obtained by just applying axis offset. The following axis defines are just
41  * provide clarity, but not used.
42  */
43 #define KXCJK1013_REG_XOUT_H		0x07
44 #define KXCJK1013_REG_YOUT_L		0x08
45 #define KXCJK1013_REG_YOUT_H		0x09
46 #define KXCJK1013_REG_ZOUT_L		0x0A
47 #define KXCJK1013_REG_ZOUT_H		0x0B
48 
49 #define KXCJK1013_REG_DCST_RESP		0x0C
50 #define KXCJK1013_REG_WHO_AM_I		0x0F
51 #define KXTF9_REG_TILT_POS_CUR		0x10
52 #define KXTF9_REG_TILT_POS_PREV		0x11
53 #define KXTF9_REG_INT_SRC1		0x15
54 #define KXCJK1013_REG_INT_SRC1		0x16	/* compatible, but called INT_SRC2 in KXTF9 ds */
55 #define KXCJK1013_REG_INT_SRC2		0x17
56 #define KXCJK1013_REG_STATUS_REG	0x18
57 #define KXCJK1013_REG_INT_REL		0x1A
58 #define KXCJK1013_REG_CTRL1		0x1B
59 #define KXTF9_REG_CTRL2			0x1C
60 #define KXCJK1013_REG_CTRL2		0x1D	/* mostly compatible, CTRL_REG3 in KTXF9 ds */
61 #define KXCJK1013_REG_INT_CTRL1		0x1E
62 #define KXCJK1013_REG_INT_CTRL2		0x1F
63 #define KXTF9_REG_INT_CTRL3		0x20
64 #define KXCJK1013_REG_DATA_CTRL		0x21
65 #define KXTF9_REG_TILT_TIMER		0x28
66 #define KXCJK1013_REG_WAKE_TIMER	0x29
67 #define KXTF9_REG_TDT_TIMER		0x2B
68 #define KXTF9_REG_TDT_THRESH_H		0x2C
69 #define KXTF9_REG_TDT_THRESH_L		0x2D
70 #define KXTF9_REG_TDT_TAP_TIMER		0x2E
71 #define KXTF9_REG_TDT_TOTAL_TIMER	0x2F
72 #define KXTF9_REG_TDT_LATENCY_TIMER	0x30
73 #define KXTF9_REG_TDT_WINDOW_TIMER	0x31
74 #define KXCJK1013_REG_SELF_TEST		0x3A
75 #define KXTF9_REG_WAKE_THRESH		0x5A
76 #define KXTF9_REG_TILT_ANGLE		0x5C
77 #define KXTF9_REG_HYST_SET		0x5F
78 #define KXCJK1013_REG_WAKE_THRES	0x6A
79 
80 #define KXCJK1013_REG_CTRL1_BIT_PC1	BIT(7)
81 #define KXCJK1013_REG_CTRL1_BIT_RES	BIT(6)
82 #define KXCJK1013_REG_CTRL1_BIT_DRDY	BIT(5)
83 #define KXCJK1013_REG_CTRL1_BIT_GSEL1	BIT(4)
84 #define KXCJK1013_REG_CTRL1_BIT_GSEL0	BIT(3)
85 #define KXCJK1013_REG_CTRL1_BIT_WUFE	BIT(1)
86 
87 #define KXCJK1013_REG_INT_CTRL1_BIT_IEU	BIT(2)	/* KXTF9 */
88 #define KXCJK1013_REG_INT_CTRL1_BIT_IEL	BIT(3)
89 #define KXCJK1013_REG_INT_CTRL1_BIT_IEA	BIT(4)
90 #define KXCJK1013_REG_INT_CTRL1_BIT_IEN	BIT(5)
91 
92 #define KXTF9_REG_TILT_BIT_LEFT_EDGE	BIT(5)
93 #define KXTF9_REG_TILT_BIT_RIGHT_EDGE	BIT(4)
94 #define KXTF9_REG_TILT_BIT_LOWER_EDGE	BIT(3)
95 #define KXTF9_REG_TILT_BIT_UPPER_EDGE	BIT(2)
96 #define KXTF9_REG_TILT_BIT_FACE_DOWN	BIT(1)
97 #define KXTF9_REG_TILT_BIT_FACE_UP	BIT(0)
98 
99 #define KXCJK1013_DATA_MASK_12_BIT	0x0FFF
100 #define KXCJK1013_MAX_STARTUP_TIME_US	100000
101 
102 #define KXCJK1013_SLEEP_DELAY_MS	2000
103 
104 #define KXCJK1013_REG_INT_SRC1_BIT_TPS	BIT(0)	/* KXTF9 */
105 #define KXCJK1013_REG_INT_SRC1_BIT_WUFS	BIT(1)
106 #define KXCJK1013_REG_INT_SRC1_MASK_TDTS	(BIT(2) | BIT(3))	/* KXTF9 */
107 #define KXCJK1013_REG_INT_SRC1_TAP_NONE		0
108 #define KXCJK1013_REG_INT_SRC1_TAP_SINGLE		BIT(2)
109 #define KXCJK1013_REG_INT_SRC1_TAP_DOUBLE		BIT(3)
110 #define KXCJK1013_REG_INT_SRC1_BIT_DRDY	BIT(4)
111 
112 /* KXCJK: INT_SOURCE2: motion detect, KXTF9: INT_SRC_REG1: tap detect */
113 #define KXCJK1013_REG_INT_SRC2_BIT_ZP	BIT(0)
114 #define KXCJK1013_REG_INT_SRC2_BIT_ZN	BIT(1)
115 #define KXCJK1013_REG_INT_SRC2_BIT_YP	BIT(2)
116 #define KXCJK1013_REG_INT_SRC2_BIT_YN	BIT(3)
117 #define KXCJK1013_REG_INT_SRC2_BIT_XP	BIT(4)
118 #define KXCJK1013_REG_INT_SRC2_BIT_XN	BIT(5)
119 
120 #define KXCJK1013_DEFAULT_WAKE_THRES	1
121 
122 enum kx_chipset {
123 	KXCJK1013,
124 	KXCJ91008,
125 	KXTJ21009,
126 	KXTF9,
127 	KX_MAX_CHIPS /* this must be last */
128 };
129 
130 enum kx_acpi_type {
131 	ACPI_GENERIC,
132 	ACPI_SMO8500,
133 	ACPI_KIOX010A,
134 };
135 
136 struct kxcjk1013_data {
137 	struct regulator_bulk_data regulators[2];
138 	struct i2c_client *client;
139 	struct iio_trigger *dready_trig;
140 	struct iio_trigger *motion_trig;
141 	struct iio_mount_matrix orientation;
142 	struct mutex mutex;
143 	s16 buffer[8];
144 	u8 odr_bits;
145 	u8 range;
146 	int wake_thres;
147 	int wake_dur;
148 	bool active_high_intr;
149 	bool dready_trigger_on;
150 	int ev_enable_state;
151 	bool motion_trigger_on;
152 	int64_t timestamp;
153 	enum kx_chipset chipset;
154 	enum kx_acpi_type acpi_type;
155 };
156 
157 enum kxcjk1013_axis {
158 	AXIS_X,
159 	AXIS_Y,
160 	AXIS_Z,
161 	AXIS_MAX,
162 };
163 
164 enum kxcjk1013_mode {
165 	STANDBY,
166 	OPERATION,
167 };
168 
169 enum kxcjk1013_range {
170 	KXCJK1013_RANGE_2G,
171 	KXCJK1013_RANGE_4G,
172 	KXCJK1013_RANGE_8G,
173 };
174 
175 struct kx_odr_map {
176 	int val;
177 	int val2;
178 	int odr_bits;
179 	int wuf_bits;
180 };
181 
182 static const struct kx_odr_map samp_freq_table[] = {
183 	{ 0, 781000, 0x08, 0x00 },
184 	{ 1, 563000, 0x09, 0x01 },
185 	{ 3, 125000, 0x0A, 0x02 },
186 	{ 6, 250000, 0x0B, 0x03 },
187 	{ 12, 500000, 0x00, 0x04 },
188 	{ 25, 0, 0x01, 0x05 },
189 	{ 50, 0, 0x02, 0x06 },
190 	{ 100, 0, 0x03, 0x06 },
191 	{ 200, 0, 0x04, 0x06 },
192 	{ 400, 0, 0x05, 0x06 },
193 	{ 800, 0, 0x06, 0x06 },
194 	{ 1600, 0, 0x07, 0x06 },
195 };
196 
197 static const char *const kxcjk1013_samp_freq_avail =
198 	"0.781000 1.563000 3.125000 6.250000 12.500000 25 50 100 200 400 800 1600";
199 
200 static const struct kx_odr_map kxtf9_samp_freq_table[] = {
201 	{ 25, 0, 0x01, 0x00 },
202 	{ 50, 0, 0x02, 0x01 },
203 	{ 100, 0, 0x03, 0x01 },
204 	{ 200, 0, 0x04, 0x01 },
205 	{ 400, 0, 0x05, 0x01 },
206 	{ 800, 0, 0x06, 0x01 },
207 };
208 
209 static const char *const kxtf9_samp_freq_avail =
210 	"25 50 100 200 400 800";
211 
212 /* Refer to section 4 of the specification */
213 static const struct {
214 	int odr_bits;
215 	int usec;
216 } odr_start_up_times[KX_MAX_CHIPS][12] = {
217 	/* KXCJK-1013 */
218 	{
219 		{0x08, 100000},
220 		{0x09, 100000},
221 		{0x0A, 100000},
222 		{0x0B, 100000},
223 		{0, 80000},
224 		{0x01, 41000},
225 		{0x02, 21000},
226 		{0x03, 11000},
227 		{0x04, 6400},
228 		{0x05, 3900},
229 		{0x06, 2700},
230 		{0x07, 2100},
231 	},
232 	/* KXCJ9-1008 */
233 	{
234 		{0x08, 100000},
235 		{0x09, 100000},
236 		{0x0A, 100000},
237 		{0x0B, 100000},
238 		{0, 80000},
239 		{0x01, 41000},
240 		{0x02, 21000},
241 		{0x03, 11000},
242 		{0x04, 6400},
243 		{0x05, 3900},
244 		{0x06, 2700},
245 		{0x07, 2100},
246 	},
247 	/* KXCTJ2-1009 */
248 	{
249 		{0x08, 1240000},
250 		{0x09, 621000},
251 		{0x0A, 309000},
252 		{0x0B, 151000},
253 		{0, 80000},
254 		{0x01, 41000},
255 		{0x02, 21000},
256 		{0x03, 11000},
257 		{0x04, 6000},
258 		{0x05, 4000},
259 		{0x06, 3000},
260 		{0x07, 2000},
261 	},
262 	/* KXTF9 */
263 	{
264 		{0x01, 81000},
265 		{0x02, 41000},
266 		{0x03, 21000},
267 		{0x04, 11000},
268 		{0x05, 5100},
269 		{0x06, 2700},
270 	},
271 };
272 
273 static const struct {
274 	u16 scale;
275 	u8 gsel_0;
276 	u8 gsel_1;
277 } KXCJK1013_scale_table[] = { {9582, 0, 0},
278 			      {19163, 1, 0},
279 			      {38326, 0, 1} };
280 
281 #ifdef CONFIG_ACPI
282 enum kiox010a_fn_index {
283 	KIOX010A_SET_LAPTOP_MODE = 1,
284 	KIOX010A_SET_TABLET_MODE = 2,
285 };
286 
287 static int kiox010a_dsm(struct device *dev, int fn_index)
288 {
289 	acpi_handle handle = ACPI_HANDLE(dev);
290 	guid_t kiox010a_dsm_guid;
291 	union acpi_object *obj;
292 
293 	if (!handle)
294 		return -ENODEV;
295 
296 	guid_parse("1f339696-d475-4e26-8cad-2e9f8e6d7a91", &kiox010a_dsm_guid);
297 
298 	obj = acpi_evaluate_dsm(handle, &kiox010a_dsm_guid, 1, fn_index, NULL);
299 	if (!obj)
300 		return -EIO;
301 
302 	ACPI_FREE(obj);
303 	return 0;
304 }
305 #endif
306 
307 static int kxcjk1013_set_mode(struct kxcjk1013_data *data,
308 			      enum kxcjk1013_mode mode)
309 {
310 	int ret;
311 
312 	ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_CTRL1);
313 	if (ret < 0) {
314 		dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
315 		return ret;
316 	}
317 
318 	if (mode == STANDBY)
319 		ret &= ~KXCJK1013_REG_CTRL1_BIT_PC1;
320 	else
321 		ret |= KXCJK1013_REG_CTRL1_BIT_PC1;
322 
323 	ret = i2c_smbus_write_byte_data(data->client,
324 					KXCJK1013_REG_CTRL1, ret);
325 	if (ret < 0) {
326 		dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
327 		return ret;
328 	}
329 
330 	return 0;
331 }
332 
333 static int kxcjk1013_get_mode(struct kxcjk1013_data *data,
334 			      enum kxcjk1013_mode *mode)
335 {
336 	int ret;
337 
338 	ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_CTRL1);
339 	if (ret < 0) {
340 		dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
341 		return ret;
342 	}
343 
344 	if (ret & KXCJK1013_REG_CTRL1_BIT_PC1)
345 		*mode = OPERATION;
346 	else
347 		*mode = STANDBY;
348 
349 	return 0;
350 }
351 
352 static int kxcjk1013_set_range(struct kxcjk1013_data *data, int range_index)
353 {
354 	int ret;
355 
356 	ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_CTRL1);
357 	if (ret < 0) {
358 		dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
359 		return ret;
360 	}
361 
362 	ret &= ~(KXCJK1013_REG_CTRL1_BIT_GSEL0 |
363 		 KXCJK1013_REG_CTRL1_BIT_GSEL1);
364 	ret |= (KXCJK1013_scale_table[range_index].gsel_0 << 3);
365 	ret |= (KXCJK1013_scale_table[range_index].gsel_1 << 4);
366 
367 	ret = i2c_smbus_write_byte_data(data->client,
368 					KXCJK1013_REG_CTRL1,
369 					ret);
370 	if (ret < 0) {
371 		dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
372 		return ret;
373 	}
374 
375 	data->range = range_index;
376 
377 	return 0;
378 }
379 
380 static int kxcjk1013_chip_init(struct kxcjk1013_data *data)
381 {
382 	int ret;
383 
384 #ifdef CONFIG_ACPI
385 	if (data->acpi_type == ACPI_KIOX010A) {
386 		/* Make sure the kbd and touchpad on 2-in-1s using 2 KXCJ91008-s work */
387 		kiox010a_dsm(&data->client->dev, KIOX010A_SET_LAPTOP_MODE);
388 	}
389 #endif
390 
391 	ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_WHO_AM_I);
392 	if (ret < 0) {
393 		dev_err(&data->client->dev, "Error reading who_am_i\n");
394 		return ret;
395 	}
396 
397 	dev_dbg(&data->client->dev, "KXCJK1013 Chip Id %x\n", ret);
398 
399 	ret = kxcjk1013_set_mode(data, STANDBY);
400 	if (ret < 0)
401 		return ret;
402 
403 	ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_CTRL1);
404 	if (ret < 0) {
405 		dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
406 		return ret;
407 	}
408 
409 	/* Set 12 bit mode */
410 	ret |= KXCJK1013_REG_CTRL1_BIT_RES;
411 
412 	ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_CTRL1,
413 					ret);
414 	if (ret < 0) {
415 		dev_err(&data->client->dev, "Error reading reg_ctrl\n");
416 		return ret;
417 	}
418 
419 	/* Setting range to 4G */
420 	ret = kxcjk1013_set_range(data, KXCJK1013_RANGE_4G);
421 	if (ret < 0)
422 		return ret;
423 
424 	ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_DATA_CTRL);
425 	if (ret < 0) {
426 		dev_err(&data->client->dev, "Error reading reg_data_ctrl\n");
427 		return ret;
428 	}
429 
430 	data->odr_bits = ret;
431 
432 	/* Set up INT polarity */
433 	ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_INT_CTRL1);
434 	if (ret < 0) {
435 		dev_err(&data->client->dev, "Error reading reg_int_ctrl1\n");
436 		return ret;
437 	}
438 
439 	if (data->active_high_intr)
440 		ret |= KXCJK1013_REG_INT_CTRL1_BIT_IEA;
441 	else
442 		ret &= ~KXCJK1013_REG_INT_CTRL1_BIT_IEA;
443 
444 	ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_INT_CTRL1,
445 					ret);
446 	if (ret < 0) {
447 		dev_err(&data->client->dev, "Error writing reg_int_ctrl1\n");
448 		return ret;
449 	}
450 
451 	ret = kxcjk1013_set_mode(data, OPERATION);
452 	if (ret < 0)
453 		return ret;
454 
455 	data->wake_thres = KXCJK1013_DEFAULT_WAKE_THRES;
456 
457 	return 0;
458 }
459 
460 #ifdef CONFIG_PM
461 static int kxcjk1013_get_startup_times(struct kxcjk1013_data *data)
462 {
463 	int i;
464 	int idx = data->chipset;
465 
466 	for (i = 0; i < ARRAY_SIZE(odr_start_up_times[idx]); ++i) {
467 		if (odr_start_up_times[idx][i].odr_bits == data->odr_bits)
468 			return odr_start_up_times[idx][i].usec;
469 	}
470 
471 	return KXCJK1013_MAX_STARTUP_TIME_US;
472 }
473 #endif
474 
475 static int kxcjk1013_set_power_state(struct kxcjk1013_data *data, bool on)
476 {
477 #ifdef CONFIG_PM
478 	int ret;
479 
480 	if (on)
481 		ret = pm_runtime_get_sync(&data->client->dev);
482 	else {
483 		pm_runtime_mark_last_busy(&data->client->dev);
484 		ret = pm_runtime_put_autosuspend(&data->client->dev);
485 	}
486 	if (ret < 0) {
487 		dev_err(&data->client->dev,
488 			"Failed: %s for %d\n", __func__, on);
489 		if (on)
490 			pm_runtime_put_noidle(&data->client->dev);
491 		return ret;
492 	}
493 #endif
494 
495 	return 0;
496 }
497 
498 static int kxcjk1013_chip_update_thresholds(struct kxcjk1013_data *data)
499 {
500 	int waketh_reg, ret;
501 
502 	ret = i2c_smbus_write_byte_data(data->client,
503 					KXCJK1013_REG_WAKE_TIMER,
504 					data->wake_dur);
505 	if (ret < 0) {
506 		dev_err(&data->client->dev,
507 			"Error writing reg_wake_timer\n");
508 		return ret;
509 	}
510 
511 	waketh_reg = data->chipset == KXTF9 ?
512 		KXTF9_REG_WAKE_THRESH : KXCJK1013_REG_WAKE_THRES;
513 	ret = i2c_smbus_write_byte_data(data->client, waketh_reg,
514 					data->wake_thres);
515 	if (ret < 0) {
516 		dev_err(&data->client->dev, "Error writing reg_wake_thres\n");
517 		return ret;
518 	}
519 
520 	return 0;
521 }
522 
523 static int kxcjk1013_setup_any_motion_interrupt(struct kxcjk1013_data *data,
524 						bool status)
525 {
526 	int ret;
527 	enum kxcjk1013_mode store_mode;
528 
529 	ret = kxcjk1013_get_mode(data, &store_mode);
530 	if (ret < 0)
531 		return ret;
532 
533 	/* This is requirement by spec to change state to STANDBY */
534 	ret = kxcjk1013_set_mode(data, STANDBY);
535 	if (ret < 0)
536 		return ret;
537 
538 	ret = kxcjk1013_chip_update_thresholds(data);
539 	if (ret < 0)
540 		return ret;
541 
542 	ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_INT_CTRL1);
543 	if (ret < 0) {
544 		dev_err(&data->client->dev, "Error reading reg_int_ctrl1\n");
545 		return ret;
546 	}
547 
548 	if (status)
549 		ret |= KXCJK1013_REG_INT_CTRL1_BIT_IEN;
550 	else
551 		ret &= ~KXCJK1013_REG_INT_CTRL1_BIT_IEN;
552 
553 	ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_INT_CTRL1,
554 					ret);
555 	if (ret < 0) {
556 		dev_err(&data->client->dev, "Error writing reg_int_ctrl1\n");
557 		return ret;
558 	}
559 
560 	ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_CTRL1);
561 	if (ret < 0) {
562 		dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
563 		return ret;
564 	}
565 
566 	if (status)
567 		ret |= KXCJK1013_REG_CTRL1_BIT_WUFE;
568 	else
569 		ret &= ~KXCJK1013_REG_CTRL1_BIT_WUFE;
570 
571 	ret = i2c_smbus_write_byte_data(data->client,
572 					KXCJK1013_REG_CTRL1, ret);
573 	if (ret < 0) {
574 		dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
575 		return ret;
576 	}
577 
578 	if (store_mode == OPERATION) {
579 		ret = kxcjk1013_set_mode(data, OPERATION);
580 		if (ret < 0)
581 			return ret;
582 	}
583 
584 	return 0;
585 }
586 
587 static int kxcjk1013_setup_new_data_interrupt(struct kxcjk1013_data *data,
588 					      bool status)
589 {
590 	int ret;
591 	enum kxcjk1013_mode store_mode;
592 
593 	ret = kxcjk1013_get_mode(data, &store_mode);
594 	if (ret < 0)
595 		return ret;
596 
597 	/* This is requirement by spec to change state to STANDBY */
598 	ret = kxcjk1013_set_mode(data, STANDBY);
599 	if (ret < 0)
600 		return ret;
601 
602 	ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_INT_CTRL1);
603 	if (ret < 0) {
604 		dev_err(&data->client->dev, "Error reading reg_int_ctrl1\n");
605 		return ret;
606 	}
607 
608 	if (status)
609 		ret |= KXCJK1013_REG_INT_CTRL1_BIT_IEN;
610 	else
611 		ret &= ~KXCJK1013_REG_INT_CTRL1_BIT_IEN;
612 
613 	ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_INT_CTRL1,
614 					ret);
615 	if (ret < 0) {
616 		dev_err(&data->client->dev, "Error writing reg_int_ctrl1\n");
617 		return ret;
618 	}
619 
620 	ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_CTRL1);
621 	if (ret < 0) {
622 		dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
623 		return ret;
624 	}
625 
626 	if (status)
627 		ret |= KXCJK1013_REG_CTRL1_BIT_DRDY;
628 	else
629 		ret &= ~KXCJK1013_REG_CTRL1_BIT_DRDY;
630 
631 	ret = i2c_smbus_write_byte_data(data->client,
632 					KXCJK1013_REG_CTRL1, ret);
633 	if (ret < 0) {
634 		dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
635 		return ret;
636 	}
637 
638 	if (store_mode == OPERATION) {
639 		ret = kxcjk1013_set_mode(data, OPERATION);
640 		if (ret < 0)
641 			return ret;
642 	}
643 
644 	return 0;
645 }
646 
647 static const struct kx_odr_map *kxcjk1013_find_odr_value(
648 	const struct kx_odr_map *map, size_t map_size, int val, int val2)
649 {
650 	int i;
651 
652 	for (i = 0; i < map_size; ++i) {
653 		if (map[i].val == val && map[i].val2 == val2)
654 			return &map[i];
655 	}
656 
657 	return ERR_PTR(-EINVAL);
658 }
659 
660 static int kxcjk1013_convert_odr_value(const struct kx_odr_map *map,
661 				       size_t map_size, int odr_bits,
662 				       int *val, int *val2)
663 {
664 	int i;
665 
666 	for (i = 0; i < map_size; ++i) {
667 		if (map[i].odr_bits == odr_bits) {
668 			*val = map[i].val;
669 			*val2 = map[i].val2;
670 			return IIO_VAL_INT_PLUS_MICRO;
671 		}
672 	}
673 
674 	return -EINVAL;
675 }
676 
677 static int kxcjk1013_set_odr(struct kxcjk1013_data *data, int val, int val2)
678 {
679 	int ret;
680 	enum kxcjk1013_mode store_mode;
681 	const struct kx_odr_map *odr_setting;
682 
683 	ret = kxcjk1013_get_mode(data, &store_mode);
684 	if (ret < 0)
685 		return ret;
686 
687 	if (data->chipset == KXTF9)
688 		odr_setting = kxcjk1013_find_odr_value(kxtf9_samp_freq_table,
689 						       ARRAY_SIZE(kxtf9_samp_freq_table),
690 						       val, val2);
691 	else
692 		odr_setting = kxcjk1013_find_odr_value(samp_freq_table,
693 						       ARRAY_SIZE(samp_freq_table),
694 						       val, val2);
695 
696 	if (IS_ERR(odr_setting))
697 		return PTR_ERR(odr_setting);
698 
699 	/* To change ODR, the chip must be set to STANDBY as per spec */
700 	ret = kxcjk1013_set_mode(data, STANDBY);
701 	if (ret < 0)
702 		return ret;
703 
704 	ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_DATA_CTRL,
705 					odr_setting->odr_bits);
706 	if (ret < 0) {
707 		dev_err(&data->client->dev, "Error writing data_ctrl\n");
708 		return ret;
709 	}
710 
711 	data->odr_bits = odr_setting->odr_bits;
712 
713 	ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_CTRL2,
714 					odr_setting->wuf_bits);
715 	if (ret < 0) {
716 		dev_err(&data->client->dev, "Error writing reg_ctrl2\n");
717 		return ret;
718 	}
719 
720 	if (store_mode == OPERATION) {
721 		ret = kxcjk1013_set_mode(data, OPERATION);
722 		if (ret < 0)
723 			return ret;
724 	}
725 
726 	return 0;
727 }
728 
729 static int kxcjk1013_get_odr(struct kxcjk1013_data *data, int *val, int *val2)
730 {
731 	if (data->chipset == KXTF9)
732 		return kxcjk1013_convert_odr_value(kxtf9_samp_freq_table,
733 						   ARRAY_SIZE(kxtf9_samp_freq_table),
734 						   data->odr_bits, val, val2);
735 	else
736 		return kxcjk1013_convert_odr_value(samp_freq_table,
737 						   ARRAY_SIZE(samp_freq_table),
738 						   data->odr_bits, val, val2);
739 }
740 
741 static int kxcjk1013_get_acc_reg(struct kxcjk1013_data *data, int axis)
742 {
743 	u8 reg = KXCJK1013_REG_XOUT_L + axis * 2;
744 	int ret;
745 
746 	ret = i2c_smbus_read_word_data(data->client, reg);
747 	if (ret < 0) {
748 		dev_err(&data->client->dev,
749 			"failed to read accel_%c registers\n", 'x' + axis);
750 		return ret;
751 	}
752 
753 	return ret;
754 }
755 
756 static int kxcjk1013_set_scale(struct kxcjk1013_data *data, int val)
757 {
758 	int ret, i;
759 	enum kxcjk1013_mode store_mode;
760 
761 	for (i = 0; i < ARRAY_SIZE(KXCJK1013_scale_table); ++i) {
762 		if (KXCJK1013_scale_table[i].scale == val) {
763 			ret = kxcjk1013_get_mode(data, &store_mode);
764 			if (ret < 0)
765 				return ret;
766 
767 			ret = kxcjk1013_set_mode(data, STANDBY);
768 			if (ret < 0)
769 				return ret;
770 
771 			ret = kxcjk1013_set_range(data, i);
772 			if (ret < 0)
773 				return ret;
774 
775 			if (store_mode == OPERATION) {
776 				ret = kxcjk1013_set_mode(data, OPERATION);
777 				if (ret)
778 					return ret;
779 			}
780 
781 			return 0;
782 		}
783 	}
784 
785 	return -EINVAL;
786 }
787 
788 static int kxcjk1013_read_raw(struct iio_dev *indio_dev,
789 			      struct iio_chan_spec const *chan, int *val,
790 			      int *val2, long mask)
791 {
792 	struct kxcjk1013_data *data = iio_priv(indio_dev);
793 	int ret;
794 
795 	switch (mask) {
796 	case IIO_CHAN_INFO_RAW:
797 		mutex_lock(&data->mutex);
798 		if (iio_buffer_enabled(indio_dev))
799 			ret = -EBUSY;
800 		else {
801 			ret = kxcjk1013_set_power_state(data, true);
802 			if (ret < 0) {
803 				mutex_unlock(&data->mutex);
804 				return ret;
805 			}
806 			ret = kxcjk1013_get_acc_reg(data, chan->scan_index);
807 			if (ret < 0) {
808 				kxcjk1013_set_power_state(data, false);
809 				mutex_unlock(&data->mutex);
810 				return ret;
811 			}
812 			*val = sign_extend32(ret >> 4, 11);
813 			ret = kxcjk1013_set_power_state(data, false);
814 		}
815 		mutex_unlock(&data->mutex);
816 
817 		if (ret < 0)
818 			return ret;
819 
820 		return IIO_VAL_INT;
821 
822 	case IIO_CHAN_INFO_SCALE:
823 		*val = 0;
824 		*val2 = KXCJK1013_scale_table[data->range].scale;
825 		return IIO_VAL_INT_PLUS_MICRO;
826 
827 	case IIO_CHAN_INFO_SAMP_FREQ:
828 		mutex_lock(&data->mutex);
829 		ret = kxcjk1013_get_odr(data, val, val2);
830 		mutex_unlock(&data->mutex);
831 		return ret;
832 
833 	default:
834 		return -EINVAL;
835 	}
836 }
837 
838 static int kxcjk1013_write_raw(struct iio_dev *indio_dev,
839 			       struct iio_chan_spec const *chan, int val,
840 			       int val2, long mask)
841 {
842 	struct kxcjk1013_data *data = iio_priv(indio_dev);
843 	int ret;
844 
845 	switch (mask) {
846 	case IIO_CHAN_INFO_SAMP_FREQ:
847 		mutex_lock(&data->mutex);
848 		ret = kxcjk1013_set_odr(data, val, val2);
849 		mutex_unlock(&data->mutex);
850 		break;
851 	case IIO_CHAN_INFO_SCALE:
852 		if (val)
853 			return -EINVAL;
854 
855 		mutex_lock(&data->mutex);
856 		ret = kxcjk1013_set_scale(data, val2);
857 		mutex_unlock(&data->mutex);
858 		break;
859 	default:
860 		ret = -EINVAL;
861 	}
862 
863 	return ret;
864 }
865 
866 static int kxcjk1013_read_event(struct iio_dev *indio_dev,
867 				   const struct iio_chan_spec *chan,
868 				   enum iio_event_type type,
869 				   enum iio_event_direction dir,
870 				   enum iio_event_info info,
871 				   int *val, int *val2)
872 {
873 	struct kxcjk1013_data *data = iio_priv(indio_dev);
874 
875 	*val2 = 0;
876 	switch (info) {
877 	case IIO_EV_INFO_VALUE:
878 		*val = data->wake_thres;
879 		break;
880 	case IIO_EV_INFO_PERIOD:
881 		*val = data->wake_dur;
882 		break;
883 	default:
884 		return -EINVAL;
885 	}
886 
887 	return IIO_VAL_INT;
888 }
889 
890 static int kxcjk1013_write_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 kxcjk1013_data *data = iio_priv(indio_dev);
898 
899 	if (data->ev_enable_state)
900 		return -EBUSY;
901 
902 	switch (info) {
903 	case IIO_EV_INFO_VALUE:
904 		data->wake_thres = val;
905 		break;
906 	case IIO_EV_INFO_PERIOD:
907 		data->wake_dur = val;
908 		break;
909 	default:
910 		return -EINVAL;
911 	}
912 
913 	return 0;
914 }
915 
916 static int kxcjk1013_read_event_config(struct iio_dev *indio_dev,
917 					  const struct iio_chan_spec *chan,
918 					  enum iio_event_type type,
919 					  enum iio_event_direction dir)
920 {
921 	struct kxcjk1013_data *data = iio_priv(indio_dev);
922 
923 	return data->ev_enable_state;
924 }
925 
926 static int kxcjk1013_write_event_config(struct iio_dev *indio_dev,
927 					   const struct iio_chan_spec *chan,
928 					   enum iio_event_type type,
929 					   enum iio_event_direction dir,
930 					   int state)
931 {
932 	struct kxcjk1013_data *data = iio_priv(indio_dev);
933 	int ret;
934 
935 	if (state && data->ev_enable_state)
936 		return 0;
937 
938 	mutex_lock(&data->mutex);
939 
940 	if (!state && data->motion_trigger_on) {
941 		data->ev_enable_state = 0;
942 		mutex_unlock(&data->mutex);
943 		return 0;
944 	}
945 
946 	/*
947 	 * We will expect the enable and disable to do operation in
948 	 * in reverse order. This will happen here anyway as our
949 	 * resume operation uses sync mode runtime pm calls, the
950 	 * suspend operation will be delayed by autosuspend delay
951 	 * So the disable operation will still happen in reverse of
952 	 * enable operation. When runtime pm is disabled the mode
953 	 * is always on so sequence doesn't matter
954 	 */
955 	ret = kxcjk1013_set_power_state(data, state);
956 	if (ret < 0) {
957 		mutex_unlock(&data->mutex);
958 		return ret;
959 	}
960 
961 	ret =  kxcjk1013_setup_any_motion_interrupt(data, state);
962 	if (ret < 0) {
963 		kxcjk1013_set_power_state(data, false);
964 		data->ev_enable_state = 0;
965 		mutex_unlock(&data->mutex);
966 		return ret;
967 	}
968 
969 	data->ev_enable_state = state;
970 	mutex_unlock(&data->mutex);
971 
972 	return 0;
973 }
974 
975 static int kxcjk1013_buffer_preenable(struct iio_dev *indio_dev)
976 {
977 	struct kxcjk1013_data *data = iio_priv(indio_dev);
978 
979 	return kxcjk1013_set_power_state(data, true);
980 }
981 
982 static int kxcjk1013_buffer_postdisable(struct iio_dev *indio_dev)
983 {
984 	struct kxcjk1013_data *data = iio_priv(indio_dev);
985 
986 	return kxcjk1013_set_power_state(data, false);
987 }
988 
989 static ssize_t kxcjk1013_get_samp_freq_avail(struct device *dev,
990 					     struct device_attribute *attr,
991 					     char *buf)
992 {
993 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
994 	struct kxcjk1013_data *data = iio_priv(indio_dev);
995 	const char *str;
996 
997 	if (data->chipset == KXTF9)
998 		str = kxtf9_samp_freq_avail;
999 	else
1000 		str = kxcjk1013_samp_freq_avail;
1001 
1002 	return sprintf(buf, "%s\n", str);
1003 }
1004 
1005 static IIO_DEVICE_ATTR(in_accel_sampling_frequency_available, S_IRUGO,
1006 		       kxcjk1013_get_samp_freq_avail, NULL, 0);
1007 
1008 static IIO_CONST_ATTR(in_accel_scale_available, "0.009582 0.019163 0.038326");
1009 
1010 static struct attribute *kxcjk1013_attributes[] = {
1011 	&iio_dev_attr_in_accel_sampling_frequency_available.dev_attr.attr,
1012 	&iio_const_attr_in_accel_scale_available.dev_attr.attr,
1013 	NULL,
1014 };
1015 
1016 static const struct attribute_group kxcjk1013_attrs_group = {
1017 	.attrs = kxcjk1013_attributes,
1018 };
1019 
1020 static const struct iio_event_spec kxcjk1013_event = {
1021 		.type = IIO_EV_TYPE_THRESH,
1022 		.dir = IIO_EV_DIR_EITHER,
1023 		.mask_separate = BIT(IIO_EV_INFO_VALUE) |
1024 				 BIT(IIO_EV_INFO_ENABLE) |
1025 				 BIT(IIO_EV_INFO_PERIOD)
1026 };
1027 
1028 static const struct iio_mount_matrix *
1029 kxcjk1013_get_mount_matrix(const struct iio_dev *indio_dev,
1030 			   const struct iio_chan_spec *chan)
1031 {
1032 	struct kxcjk1013_data *data = iio_priv(indio_dev);
1033 
1034 	return &data->orientation;
1035 }
1036 
1037 static const struct iio_chan_spec_ext_info kxcjk1013_ext_info[] = {
1038 	IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE, kxcjk1013_get_mount_matrix),
1039 	{ }
1040 };
1041 
1042 #define KXCJK1013_CHANNEL(_axis) {					\
1043 	.type = IIO_ACCEL,						\
1044 	.modified = 1,							\
1045 	.channel2 = IIO_MOD_##_axis,					\
1046 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),			\
1047 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |		\
1048 				BIT(IIO_CHAN_INFO_SAMP_FREQ),		\
1049 	.scan_index = AXIS_##_axis,					\
1050 	.scan_type = {							\
1051 		.sign = 's',						\
1052 		.realbits = 12,						\
1053 		.storagebits = 16,					\
1054 		.shift = 4,						\
1055 		.endianness = IIO_LE,					\
1056 	},								\
1057 	.event_spec = &kxcjk1013_event,				\
1058 	.ext_info = kxcjk1013_ext_info,					\
1059 	.num_event_specs = 1						\
1060 }
1061 
1062 static const struct iio_chan_spec kxcjk1013_channels[] = {
1063 	KXCJK1013_CHANNEL(X),
1064 	KXCJK1013_CHANNEL(Y),
1065 	KXCJK1013_CHANNEL(Z),
1066 	IIO_CHAN_SOFT_TIMESTAMP(3),
1067 };
1068 
1069 static const struct iio_buffer_setup_ops kxcjk1013_buffer_setup_ops = {
1070 	.preenable		= kxcjk1013_buffer_preenable,
1071 	.postdisable		= kxcjk1013_buffer_postdisable,
1072 };
1073 
1074 static const struct iio_info kxcjk1013_info = {
1075 	.attrs			= &kxcjk1013_attrs_group,
1076 	.read_raw		= kxcjk1013_read_raw,
1077 	.write_raw		= kxcjk1013_write_raw,
1078 	.read_event_value	= kxcjk1013_read_event,
1079 	.write_event_value	= kxcjk1013_write_event,
1080 	.write_event_config	= kxcjk1013_write_event_config,
1081 	.read_event_config	= kxcjk1013_read_event_config,
1082 };
1083 
1084 static const unsigned long kxcjk1013_scan_masks[] = {0x7, 0};
1085 
1086 static irqreturn_t kxcjk1013_trigger_handler(int irq, void *p)
1087 {
1088 	struct iio_poll_func *pf = p;
1089 	struct iio_dev *indio_dev = pf->indio_dev;
1090 	struct kxcjk1013_data *data = iio_priv(indio_dev);
1091 	int ret;
1092 
1093 	mutex_lock(&data->mutex);
1094 	ret = i2c_smbus_read_i2c_block_data_or_emulated(data->client,
1095 							KXCJK1013_REG_XOUT_L,
1096 							AXIS_MAX * 2,
1097 							(u8 *)data->buffer);
1098 	mutex_unlock(&data->mutex);
1099 	if (ret < 0)
1100 		goto err;
1101 
1102 	iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
1103 					   data->timestamp);
1104 err:
1105 	iio_trigger_notify_done(indio_dev->trig);
1106 
1107 	return IRQ_HANDLED;
1108 }
1109 
1110 static void kxcjk1013_trig_reen(struct iio_trigger *trig)
1111 {
1112 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
1113 	struct kxcjk1013_data *data = iio_priv(indio_dev);
1114 	int ret;
1115 
1116 	ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_INT_REL);
1117 	if (ret < 0)
1118 		dev_err(&data->client->dev, "Error reading reg_int_rel\n");
1119 }
1120 
1121 static int kxcjk1013_data_rdy_trigger_set_state(struct iio_trigger *trig,
1122 						bool state)
1123 {
1124 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
1125 	struct kxcjk1013_data *data = iio_priv(indio_dev);
1126 	int ret;
1127 
1128 	mutex_lock(&data->mutex);
1129 
1130 	if (!state && data->ev_enable_state && data->motion_trigger_on) {
1131 		data->motion_trigger_on = false;
1132 		mutex_unlock(&data->mutex);
1133 		return 0;
1134 	}
1135 
1136 	ret = kxcjk1013_set_power_state(data, state);
1137 	if (ret < 0) {
1138 		mutex_unlock(&data->mutex);
1139 		return ret;
1140 	}
1141 	if (data->motion_trig == trig)
1142 		ret = kxcjk1013_setup_any_motion_interrupt(data, state);
1143 	else
1144 		ret = kxcjk1013_setup_new_data_interrupt(data, state);
1145 	if (ret < 0) {
1146 		kxcjk1013_set_power_state(data, false);
1147 		mutex_unlock(&data->mutex);
1148 		return ret;
1149 	}
1150 	if (data->motion_trig == trig)
1151 		data->motion_trigger_on = state;
1152 	else
1153 		data->dready_trigger_on = state;
1154 
1155 	mutex_unlock(&data->mutex);
1156 
1157 	return 0;
1158 }
1159 
1160 static const struct iio_trigger_ops kxcjk1013_trigger_ops = {
1161 	.set_trigger_state = kxcjk1013_data_rdy_trigger_set_state,
1162 	.reenable = kxcjk1013_trig_reen,
1163 };
1164 
1165 static void kxcjk1013_report_motion_event(struct iio_dev *indio_dev)
1166 {
1167 	struct kxcjk1013_data *data = iio_priv(indio_dev);
1168 
1169 	int ret = i2c_smbus_read_byte_data(data->client,
1170 					   KXCJK1013_REG_INT_SRC2);
1171 	if (ret < 0) {
1172 		dev_err(&data->client->dev, "Error reading reg_int_src2\n");
1173 		return;
1174 	}
1175 
1176 	if (ret & KXCJK1013_REG_INT_SRC2_BIT_XN)
1177 		iio_push_event(indio_dev,
1178 			       IIO_MOD_EVENT_CODE(IIO_ACCEL,
1179 						  0,
1180 						  IIO_MOD_X,
1181 						  IIO_EV_TYPE_THRESH,
1182 						  IIO_EV_DIR_FALLING),
1183 			       data->timestamp);
1184 
1185 	if (ret & KXCJK1013_REG_INT_SRC2_BIT_XP)
1186 		iio_push_event(indio_dev,
1187 			       IIO_MOD_EVENT_CODE(IIO_ACCEL,
1188 						  0,
1189 						  IIO_MOD_X,
1190 						  IIO_EV_TYPE_THRESH,
1191 						  IIO_EV_DIR_RISING),
1192 			       data->timestamp);
1193 
1194 	if (ret & KXCJK1013_REG_INT_SRC2_BIT_YN)
1195 		iio_push_event(indio_dev,
1196 			       IIO_MOD_EVENT_CODE(IIO_ACCEL,
1197 						  0,
1198 						  IIO_MOD_Y,
1199 						  IIO_EV_TYPE_THRESH,
1200 						  IIO_EV_DIR_FALLING),
1201 			       data->timestamp);
1202 
1203 	if (ret & KXCJK1013_REG_INT_SRC2_BIT_YP)
1204 		iio_push_event(indio_dev,
1205 			       IIO_MOD_EVENT_CODE(IIO_ACCEL,
1206 						  0,
1207 						  IIO_MOD_Y,
1208 						  IIO_EV_TYPE_THRESH,
1209 						  IIO_EV_DIR_RISING),
1210 			       data->timestamp);
1211 
1212 	if (ret & KXCJK1013_REG_INT_SRC2_BIT_ZN)
1213 		iio_push_event(indio_dev,
1214 			       IIO_MOD_EVENT_CODE(IIO_ACCEL,
1215 						  0,
1216 						  IIO_MOD_Z,
1217 						  IIO_EV_TYPE_THRESH,
1218 						  IIO_EV_DIR_FALLING),
1219 			       data->timestamp);
1220 
1221 	if (ret & KXCJK1013_REG_INT_SRC2_BIT_ZP)
1222 		iio_push_event(indio_dev,
1223 			       IIO_MOD_EVENT_CODE(IIO_ACCEL,
1224 						  0,
1225 						  IIO_MOD_Z,
1226 						  IIO_EV_TYPE_THRESH,
1227 						  IIO_EV_DIR_RISING),
1228 			       data->timestamp);
1229 }
1230 
1231 static irqreturn_t kxcjk1013_event_handler(int irq, void *private)
1232 {
1233 	struct iio_dev *indio_dev = private;
1234 	struct kxcjk1013_data *data = iio_priv(indio_dev);
1235 	int ret;
1236 
1237 	ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_INT_SRC1);
1238 	if (ret < 0) {
1239 		dev_err(&data->client->dev, "Error reading reg_int_src1\n");
1240 		goto ack_intr;
1241 	}
1242 
1243 	if (ret & KXCJK1013_REG_INT_SRC1_BIT_WUFS) {
1244 		if (data->chipset == KXTF9)
1245 			iio_push_event(indio_dev,
1246 				       IIO_MOD_EVENT_CODE(IIO_ACCEL,
1247 				       0,
1248 				       IIO_MOD_X_AND_Y_AND_Z,
1249 				       IIO_EV_TYPE_THRESH,
1250 				       IIO_EV_DIR_RISING),
1251 				       data->timestamp);
1252 		else
1253 			kxcjk1013_report_motion_event(indio_dev);
1254 	}
1255 
1256 ack_intr:
1257 	if (data->dready_trigger_on)
1258 		return IRQ_HANDLED;
1259 
1260 	ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_INT_REL);
1261 	if (ret < 0)
1262 		dev_err(&data->client->dev, "Error reading reg_int_rel\n");
1263 
1264 	return IRQ_HANDLED;
1265 }
1266 
1267 static irqreturn_t kxcjk1013_data_rdy_trig_poll(int irq, void *private)
1268 {
1269 	struct iio_dev *indio_dev = private;
1270 	struct kxcjk1013_data *data = iio_priv(indio_dev);
1271 
1272 	data->timestamp = iio_get_time_ns(indio_dev);
1273 
1274 	if (data->dready_trigger_on)
1275 		iio_trigger_poll(data->dready_trig);
1276 	else if (data->motion_trigger_on)
1277 		iio_trigger_poll(data->motion_trig);
1278 
1279 	if (data->ev_enable_state)
1280 		return IRQ_WAKE_THREAD;
1281 	else
1282 		return IRQ_HANDLED;
1283 }
1284 
1285 static const char *kxcjk1013_match_acpi_device(struct device *dev,
1286 					       enum kx_chipset *chipset,
1287 					       enum kx_acpi_type *acpi_type,
1288 					       const char **label)
1289 {
1290 	const struct acpi_device_id *id;
1291 
1292 	id = acpi_match_device(dev->driver->acpi_match_table, dev);
1293 	if (!id)
1294 		return NULL;
1295 
1296 	if (strcmp(id->id, "SMO8500") == 0) {
1297 		*acpi_type = ACPI_SMO8500;
1298 	} else if (strcmp(id->id, "KIOX010A") == 0) {
1299 		*acpi_type = ACPI_KIOX010A;
1300 		*label = "accel-display";
1301 	} else if (strcmp(id->id, "KIOX020A") == 0) {
1302 		*label = "accel-base";
1303 	}
1304 
1305 	*chipset = (enum kx_chipset)id->driver_data;
1306 
1307 	return dev_name(dev);
1308 }
1309 
1310 static void kxcjk1013_disable_regulators(void *d)
1311 {
1312 	struct kxcjk1013_data *data = d;
1313 
1314 	regulator_bulk_disable(ARRAY_SIZE(data->regulators), data->regulators);
1315 }
1316 
1317 static int kxcjk1013_probe(struct i2c_client *client,
1318 			   const struct i2c_device_id *id)
1319 {
1320 	struct kxcjk1013_data *data;
1321 	struct iio_dev *indio_dev;
1322 	struct kxcjk_1013_platform_data *pdata;
1323 	const char *name;
1324 	int ret;
1325 
1326 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
1327 	if (!indio_dev)
1328 		return -ENOMEM;
1329 
1330 	data = iio_priv(indio_dev);
1331 	i2c_set_clientdata(client, indio_dev);
1332 	data->client = client;
1333 
1334 	pdata = dev_get_platdata(&client->dev);
1335 	if (pdata) {
1336 		data->active_high_intr = pdata->active_high_intr;
1337 		data->orientation = pdata->orientation;
1338 	} else {
1339 		data->active_high_intr = true; /* default polarity */
1340 
1341 		ret = iio_read_mount_matrix(&client->dev, "mount-matrix",
1342 					    &data->orientation);
1343 		if (ret)
1344 			return ret;
1345 	}
1346 
1347 	data->regulators[0].supply = "vdd";
1348 	data->regulators[1].supply = "vddio";
1349 	ret = devm_regulator_bulk_get(&client->dev, ARRAY_SIZE(data->regulators),
1350 				      data->regulators);
1351 	if (ret)
1352 		return dev_err_probe(&client->dev, ret, "Failed to get regulators\n");
1353 
1354 	ret = regulator_bulk_enable(ARRAY_SIZE(data->regulators),
1355 				    data->regulators);
1356 	if (ret)
1357 		return ret;
1358 
1359 	ret = devm_add_action_or_reset(&client->dev, kxcjk1013_disable_regulators, data);
1360 	if (ret)
1361 		return ret;
1362 
1363 	/*
1364 	 * A typical delay of 10ms is required for powering up
1365 	 * according to the data sheets of supported chips.
1366 	 * Hence double that to play safe.
1367 	 */
1368 	msleep(20);
1369 
1370 	if (id) {
1371 		data->chipset = (enum kx_chipset)(id->driver_data);
1372 		name = id->name;
1373 	} else if (ACPI_HANDLE(&client->dev)) {
1374 		name = kxcjk1013_match_acpi_device(&client->dev,
1375 						   &data->chipset,
1376 						   &data->acpi_type,
1377 						   &indio_dev->label);
1378 	} else
1379 		return -ENODEV;
1380 
1381 	ret = kxcjk1013_chip_init(data);
1382 	if (ret < 0)
1383 		return ret;
1384 
1385 	mutex_init(&data->mutex);
1386 
1387 	indio_dev->channels = kxcjk1013_channels;
1388 	indio_dev->num_channels = ARRAY_SIZE(kxcjk1013_channels);
1389 	indio_dev->available_scan_masks = kxcjk1013_scan_masks;
1390 	indio_dev->name = name;
1391 	indio_dev->modes = INDIO_DIRECT_MODE;
1392 	indio_dev->info = &kxcjk1013_info;
1393 
1394 	if (client->irq > 0 && data->acpi_type != ACPI_SMO8500) {
1395 		ret = devm_request_threaded_irq(&client->dev, client->irq,
1396 						kxcjk1013_data_rdy_trig_poll,
1397 						kxcjk1013_event_handler,
1398 						IRQF_TRIGGER_RISING,
1399 						KXCJK1013_IRQ_NAME,
1400 						indio_dev);
1401 		if (ret)
1402 			goto err_poweroff;
1403 
1404 		data->dready_trig = devm_iio_trigger_alloc(&client->dev,
1405 							   "%s-dev%d",
1406 							   indio_dev->name,
1407 							   indio_dev->id);
1408 		if (!data->dready_trig) {
1409 			ret = -ENOMEM;
1410 			goto err_poweroff;
1411 		}
1412 
1413 		data->motion_trig = devm_iio_trigger_alloc(&client->dev,
1414 							  "%s-any-motion-dev%d",
1415 							  indio_dev->name,
1416 							  indio_dev->id);
1417 		if (!data->motion_trig) {
1418 			ret = -ENOMEM;
1419 			goto err_poweroff;
1420 		}
1421 
1422 		data->dready_trig->ops = &kxcjk1013_trigger_ops;
1423 		iio_trigger_set_drvdata(data->dready_trig, indio_dev);
1424 		indio_dev->trig = data->dready_trig;
1425 		iio_trigger_get(indio_dev->trig);
1426 		ret = iio_trigger_register(data->dready_trig);
1427 		if (ret)
1428 			goto err_poweroff;
1429 
1430 		data->motion_trig->ops = &kxcjk1013_trigger_ops;
1431 		iio_trigger_set_drvdata(data->motion_trig, indio_dev);
1432 		ret = iio_trigger_register(data->motion_trig);
1433 		if (ret) {
1434 			data->motion_trig = NULL;
1435 			goto err_trigger_unregister;
1436 		}
1437 	}
1438 
1439 	ret = iio_triggered_buffer_setup(indio_dev,
1440 					 &iio_pollfunc_store_time,
1441 					 kxcjk1013_trigger_handler,
1442 					 &kxcjk1013_buffer_setup_ops);
1443 	if (ret < 0) {
1444 		dev_err(&client->dev, "iio triggered buffer setup failed\n");
1445 		goto err_trigger_unregister;
1446 	}
1447 
1448 	ret = pm_runtime_set_active(&client->dev);
1449 	if (ret)
1450 		goto err_buffer_cleanup;
1451 
1452 	pm_runtime_enable(&client->dev);
1453 	pm_runtime_set_autosuspend_delay(&client->dev,
1454 					 KXCJK1013_SLEEP_DELAY_MS);
1455 	pm_runtime_use_autosuspend(&client->dev);
1456 
1457 	ret = iio_device_register(indio_dev);
1458 	if (ret < 0) {
1459 		dev_err(&client->dev, "unable to register iio device\n");
1460 		goto err_buffer_cleanup;
1461 	}
1462 
1463 	return 0;
1464 
1465 err_buffer_cleanup:
1466 	if (data->dready_trig)
1467 		iio_triggered_buffer_cleanup(indio_dev);
1468 err_trigger_unregister:
1469 	if (data->dready_trig)
1470 		iio_trigger_unregister(data->dready_trig);
1471 	if (data->motion_trig)
1472 		iio_trigger_unregister(data->motion_trig);
1473 err_poweroff:
1474 	kxcjk1013_set_mode(data, STANDBY);
1475 
1476 	return ret;
1477 }
1478 
1479 static int kxcjk1013_remove(struct i2c_client *client)
1480 {
1481 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
1482 	struct kxcjk1013_data *data = iio_priv(indio_dev);
1483 
1484 	iio_device_unregister(indio_dev);
1485 
1486 	pm_runtime_disable(&client->dev);
1487 	pm_runtime_set_suspended(&client->dev);
1488 	pm_runtime_put_noidle(&client->dev);
1489 
1490 	if (data->dready_trig) {
1491 		iio_triggered_buffer_cleanup(indio_dev);
1492 		iio_trigger_unregister(data->dready_trig);
1493 		iio_trigger_unregister(data->motion_trig);
1494 	}
1495 
1496 	mutex_lock(&data->mutex);
1497 	kxcjk1013_set_mode(data, STANDBY);
1498 	mutex_unlock(&data->mutex);
1499 
1500 	return 0;
1501 }
1502 
1503 #ifdef CONFIG_PM_SLEEP
1504 static int kxcjk1013_suspend(struct device *dev)
1505 {
1506 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1507 	struct kxcjk1013_data *data = iio_priv(indio_dev);
1508 	int ret;
1509 
1510 	mutex_lock(&data->mutex);
1511 	ret = kxcjk1013_set_mode(data, STANDBY);
1512 	mutex_unlock(&data->mutex);
1513 
1514 	return ret;
1515 }
1516 
1517 static int kxcjk1013_resume(struct device *dev)
1518 {
1519 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1520 	struct kxcjk1013_data *data = iio_priv(indio_dev);
1521 	int ret = 0;
1522 
1523 	mutex_lock(&data->mutex);
1524 	ret = kxcjk1013_set_mode(data, OPERATION);
1525 	if (ret == 0)
1526 		ret = kxcjk1013_set_range(data, data->range);
1527 	mutex_unlock(&data->mutex);
1528 
1529 	return ret;
1530 }
1531 #endif
1532 
1533 #ifdef CONFIG_PM
1534 static int kxcjk1013_runtime_suspend(struct device *dev)
1535 {
1536 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1537 	struct kxcjk1013_data *data = iio_priv(indio_dev);
1538 	int ret;
1539 
1540 	ret = kxcjk1013_set_mode(data, STANDBY);
1541 	if (ret < 0) {
1542 		dev_err(&data->client->dev, "powering off device failed\n");
1543 		return -EAGAIN;
1544 	}
1545 	return 0;
1546 }
1547 
1548 static int kxcjk1013_runtime_resume(struct device *dev)
1549 {
1550 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1551 	struct kxcjk1013_data *data = iio_priv(indio_dev);
1552 	int ret;
1553 	int sleep_val;
1554 
1555 	ret = kxcjk1013_set_mode(data, OPERATION);
1556 	if (ret < 0)
1557 		return ret;
1558 
1559 	sleep_val = kxcjk1013_get_startup_times(data);
1560 	if (sleep_val < 20000)
1561 		usleep_range(sleep_val, 20000);
1562 	else
1563 		msleep_interruptible(sleep_val/1000);
1564 
1565 	return 0;
1566 }
1567 #endif
1568 
1569 static const struct dev_pm_ops kxcjk1013_pm_ops = {
1570 	SET_SYSTEM_SLEEP_PM_OPS(kxcjk1013_suspend, kxcjk1013_resume)
1571 	SET_RUNTIME_PM_OPS(kxcjk1013_runtime_suspend,
1572 			   kxcjk1013_runtime_resume, NULL)
1573 };
1574 
1575 static const struct acpi_device_id kx_acpi_match[] = {
1576 	{"KXCJ1013", KXCJK1013},
1577 	{"KXCJ1008", KXCJ91008},
1578 	{"KXCJ9000", KXCJ91008},
1579 	{"KIOX0008", KXCJ91008},
1580 	{"KIOX0009", KXTJ21009},
1581 	{"KIOX000A", KXCJ91008},
1582 	{"KIOX010A", KXCJ91008}, /* KXCJ91008 in the display of a yoga 2-in-1 */
1583 	{"KIOX020A", KXCJ91008}, /* KXCJ91008 in the base of a yoga 2-in-1 */
1584 	{"KXTJ1009", KXTJ21009},
1585 	{"KXJ2109",  KXTJ21009},
1586 	{"SMO8500",  KXCJ91008},
1587 	{ },
1588 };
1589 MODULE_DEVICE_TABLE(acpi, kx_acpi_match);
1590 
1591 static const struct i2c_device_id kxcjk1013_id[] = {
1592 	{"kxcjk1013", KXCJK1013},
1593 	{"kxcj91008", KXCJ91008},
1594 	{"kxtj21009", KXTJ21009},
1595 	{"kxtf9",     KXTF9},
1596 	{"SMO8500",   KXCJ91008},
1597 	{}
1598 };
1599 
1600 MODULE_DEVICE_TABLE(i2c, kxcjk1013_id);
1601 
1602 static const struct of_device_id kxcjk1013_of_match[] = {
1603 	{ .compatible = "kionix,kxcjk1013", },
1604 	{ .compatible = "kionix,kxcj91008", },
1605 	{ .compatible = "kionix,kxtj21009", },
1606 	{ .compatible = "kionix,kxtf9", },
1607 	{ }
1608 };
1609 MODULE_DEVICE_TABLE(of, kxcjk1013_of_match);
1610 
1611 static struct i2c_driver kxcjk1013_driver = {
1612 	.driver = {
1613 		.name	= KXCJK1013_DRV_NAME,
1614 		.acpi_match_table = ACPI_PTR(kx_acpi_match),
1615 		.of_match_table = kxcjk1013_of_match,
1616 		.pm	= &kxcjk1013_pm_ops,
1617 	},
1618 	.probe		= kxcjk1013_probe,
1619 	.remove		= kxcjk1013_remove,
1620 	.id_table	= kxcjk1013_id,
1621 };
1622 module_i2c_driver(kxcjk1013_driver);
1623 
1624 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
1625 MODULE_LICENSE("GPL v2");
1626 MODULE_DESCRIPTION("KXCJK1013 accelerometer driver");
1627