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