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