1 // SPDX-License-Identifier: GPL-2.0-only
3 * KXCJK-1013 3-axis accelerometer driver
4 * Copyright (c) 2014, Intel Corporation.
7 #include <linux/module.h>
9 #include <linux/interrupt.h>
10 #include <linux/delay.h>
11 #include <linux/bitops.h>
12 #include <linux/slab.h>
13 #include <linux/string.h>
14 #include <linux/acpi.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/iio/iio.h>
18 #include <linux/iio/sysfs.h>
19 #include <linux/iio/buffer.h>
20 #include <linux/iio/trigger.h>
21 #include <linux/iio/events.h>
22 #include <linux/iio/trigger_consumer.h>
23 #include <linux/iio/triggered_buffer.h>
24 #include <linux/iio/accel/kxcjk_1013.h>
26 #define KXCJK1013_DRV_NAME "kxcjk1013"
27 #define KXCJK1013_IRQ_NAME "kxcjk1013_event"
29 #define KXTF9_REG_HP_XOUT_L 0x00
30 #define KXTF9_REG_HP_XOUT_H 0x01
31 #define KXTF9_REG_HP_YOUT_L 0x02
32 #define KXTF9_REG_HP_YOUT_H 0x03
33 #define KXTF9_REG_HP_ZOUT_L 0x04
34 #define KXTF9_REG_HP_ZOUT_H 0x05
36 #define KXCJK1013_REG_XOUT_L 0x06
38 * From low byte X axis register, all the other addresses of Y and Z can be
39 * obtained by just applying axis offset. The following axis defines are just
40 * provide clarity, but not used.
42 #define KXCJK1013_REG_XOUT_H 0x07
43 #define KXCJK1013_REG_YOUT_L 0x08
44 #define KXCJK1013_REG_YOUT_H 0x09
45 #define KXCJK1013_REG_ZOUT_L 0x0A
46 #define KXCJK1013_REG_ZOUT_H 0x0B
48 #define KXCJK1013_REG_DCST_RESP 0x0C
49 #define KXCJK1013_REG_WHO_AM_I 0x0F
50 #define KXTF9_REG_TILT_POS_CUR 0x10
51 #define KXTF9_REG_TILT_POS_PREV 0x11
52 #define KXTF9_REG_INT_SRC1 0x15
53 #define KXCJK1013_REG_INT_SRC1 0x16 /* compatible, but called INT_SRC2 in KXTF9 ds */
54 #define KXCJK1013_REG_INT_SRC2 0x17
55 #define KXCJK1013_REG_STATUS_REG 0x18
56 #define KXCJK1013_REG_INT_REL 0x1A
57 #define KXCJK1013_REG_CTRL1 0x1B
58 #define KXTF9_REG_CTRL2 0x1C
59 #define KXCJK1013_REG_CTRL2 0x1D /* mostly compatible, CTRL_REG3 in KTXF9 ds */
60 #define KXCJK1013_REG_INT_CTRL1 0x1E
61 #define KXCJK1013_REG_INT_CTRL2 0x1F
62 #define KXTF9_REG_INT_CTRL3 0x20
63 #define KXCJK1013_REG_DATA_CTRL 0x21
64 #define KXTF9_REG_TILT_TIMER 0x28
65 #define KXCJK1013_REG_WAKE_TIMER 0x29
66 #define KXTF9_REG_TDT_TIMER 0x2B
67 #define KXTF9_REG_TDT_THRESH_H 0x2C
68 #define KXTF9_REG_TDT_THRESH_L 0x2D
69 #define KXTF9_REG_TDT_TAP_TIMER 0x2E
70 #define KXTF9_REG_TDT_TOTAL_TIMER 0x2F
71 #define KXTF9_REG_TDT_LATENCY_TIMER 0x30
72 #define KXTF9_REG_TDT_WINDOW_TIMER 0x31
73 #define KXCJK1013_REG_SELF_TEST 0x3A
74 #define KXTF9_REG_WAKE_THRESH 0x5A
75 #define KXTF9_REG_TILT_ANGLE 0x5C
76 #define KXTF9_REG_HYST_SET 0x5F
77 #define KXCJK1013_REG_WAKE_THRES 0x6A
79 #define KXCJK1013_REG_CTRL1_BIT_PC1 BIT(7)
80 #define KXCJK1013_REG_CTRL1_BIT_RES BIT(6)
81 #define KXCJK1013_REG_CTRL1_BIT_DRDY BIT(5)
82 #define KXCJK1013_REG_CTRL1_BIT_GSEL1 BIT(4)
83 #define KXCJK1013_REG_CTRL1_BIT_GSEL0 BIT(3)
84 #define KXCJK1013_REG_CTRL1_BIT_WUFE BIT(1)
86 #define KXCJK1013_REG_INT_CTRL1_BIT_IEU BIT(2) /* KXTF9 */
87 #define KXCJK1013_REG_INT_CTRL1_BIT_IEL BIT(3)
88 #define KXCJK1013_REG_INT_CTRL1_BIT_IEA BIT(4)
89 #define KXCJK1013_REG_INT_CTRL1_BIT_IEN BIT(5)
91 #define KXTF9_REG_TILT_BIT_LEFT_EDGE BIT(5)
92 #define KXTF9_REG_TILT_BIT_RIGHT_EDGE BIT(4)
93 #define KXTF9_REG_TILT_BIT_LOWER_EDGE BIT(3)
94 #define KXTF9_REG_TILT_BIT_UPPER_EDGE BIT(2)
95 #define KXTF9_REG_TILT_BIT_FACE_DOWN BIT(1)
96 #define KXTF9_REG_TILT_BIT_FACE_UP BIT(0)
98 #define KXCJK1013_DATA_MASK_12_BIT 0x0FFF
99 #define KXCJK1013_MAX_STARTUP_TIME_US 100000
101 #define KXCJK1013_SLEEP_DELAY_MS 2000
103 #define KXCJK1013_REG_INT_SRC1_BIT_TPS BIT(0) /* KXTF9 */
104 #define KXCJK1013_REG_INT_SRC1_BIT_WUFS BIT(1)
105 #define KXCJK1013_REG_INT_SRC1_MASK_TDTS (BIT(2) | BIT(3)) /* KXTF9 */
106 #define KXCJK1013_REG_INT_SRC1_TAP_NONE 0
107 #define KXCJK1013_REG_INT_SRC1_TAP_SINGLE BIT(2)
108 #define KXCJK1013_REG_INT_SRC1_TAP_DOUBLE BIT(3)
109 #define KXCJK1013_REG_INT_SRC1_BIT_DRDY BIT(4)
111 /* KXCJK: INT_SOURCE2: motion detect, KXTF9: INT_SRC_REG1: tap detect */
112 #define KXCJK1013_REG_INT_SRC2_BIT_ZP BIT(0)
113 #define KXCJK1013_REG_INT_SRC2_BIT_ZN BIT(1)
114 #define KXCJK1013_REG_INT_SRC2_BIT_YP BIT(2)
115 #define KXCJK1013_REG_INT_SRC2_BIT_YN BIT(3)
116 #define KXCJK1013_REG_INT_SRC2_BIT_XP BIT(4)
117 #define KXCJK1013_REG_INT_SRC2_BIT_XN BIT(5)
119 #define KXCJK1013_DEFAULT_WAKE_THRES 1
126 KX_MAX_CHIPS
/* this must be last */
129 struct kxcjk1013_data
{
130 struct i2c_client
*client
;
131 struct iio_trigger
*dready_trig
;
132 struct iio_trigger
*motion_trig
;
133 struct iio_mount_matrix orientation
;
140 bool active_high_intr
;
141 bool dready_trigger_on
;
143 bool motion_trigger_on
;
145 enum kx_chipset chipset
;
146 bool is_smo8500_device
;
149 enum kxcjk1013_axis
{
156 enum kxcjk1013_mode
{
161 enum kxcjk1013_range
{
174 static const struct kx_odr_map samp_freq_table
[] = {
175 { 0, 781000, 0x08, 0x00 },
176 { 1, 563000, 0x09, 0x01 },
177 { 3, 125000, 0x0A, 0x02 },
178 { 6, 250000, 0x0B, 0x03 },
179 { 12, 500000, 0x00, 0x04 },
180 { 25, 0, 0x01, 0x05 },
181 { 50, 0, 0x02, 0x06 },
182 { 100, 0, 0x03, 0x06 },
183 { 200, 0, 0x04, 0x06 },
184 { 400, 0, 0x05, 0x06 },
185 { 800, 0, 0x06, 0x06 },
186 { 1600, 0, 0x07, 0x06 },
189 static const char *const kxcjk1013_samp_freq_avail
=
190 "0.781000 1.563000 3.125000 6.250000 12.500000 25 50 100 200 400 800 1600";
192 static const struct kx_odr_map kxtf9_samp_freq_table
[] = {
193 { 25, 0, 0x01, 0x00 },
194 { 50, 0, 0x02, 0x01 },
195 { 100, 0, 0x03, 0x01 },
196 { 200, 0, 0x04, 0x01 },
197 { 400, 0, 0x05, 0x01 },
198 { 800, 0, 0x06, 0x01 },
201 static const char *const kxtf9_samp_freq_avail
=
202 "25 50 100 200 400 800";
204 /* Refer to section 4 of the specification */
205 static const struct {
208 } odr_start_up_times
[KX_MAX_CHIPS
][12] = {
265 static const struct {
269 } KXCJK1013_scale_table
[] = { {9582, 0, 0},
273 static int kxcjk1013_set_mode(struct kxcjk1013_data
*data
,
274 enum kxcjk1013_mode mode
)
278 ret
= i2c_smbus_read_byte_data(data
->client
, KXCJK1013_REG_CTRL1
);
280 dev_err(&data
->client
->dev
, "Error reading reg_ctrl1\n");
285 ret
&= ~KXCJK1013_REG_CTRL1_BIT_PC1
;
287 ret
|= KXCJK1013_REG_CTRL1_BIT_PC1
;
289 ret
= i2c_smbus_write_byte_data(data
->client
,
290 KXCJK1013_REG_CTRL1
, ret
);
292 dev_err(&data
->client
->dev
, "Error writing reg_ctrl1\n");
299 static int kxcjk1013_get_mode(struct kxcjk1013_data
*data
,
300 enum kxcjk1013_mode
*mode
)
304 ret
= i2c_smbus_read_byte_data(data
->client
, KXCJK1013_REG_CTRL1
);
306 dev_err(&data
->client
->dev
, "Error reading reg_ctrl1\n");
310 if (ret
& KXCJK1013_REG_CTRL1_BIT_PC1
)
318 static int kxcjk1013_set_range(struct kxcjk1013_data
*data
, int range_index
)
322 ret
= i2c_smbus_read_byte_data(data
->client
, KXCJK1013_REG_CTRL1
);
324 dev_err(&data
->client
->dev
, "Error reading reg_ctrl1\n");
328 ret
&= ~(KXCJK1013_REG_CTRL1_BIT_GSEL0
|
329 KXCJK1013_REG_CTRL1_BIT_GSEL1
);
330 ret
|= (KXCJK1013_scale_table
[range_index
].gsel_0
<< 3);
331 ret
|= (KXCJK1013_scale_table
[range_index
].gsel_1
<< 4);
333 ret
= i2c_smbus_write_byte_data(data
->client
,
337 dev_err(&data
->client
->dev
, "Error writing reg_ctrl1\n");
341 data
->range
= range_index
;
346 static int kxcjk1013_chip_init(struct kxcjk1013_data
*data
)
350 ret
= i2c_smbus_read_byte_data(data
->client
, KXCJK1013_REG_WHO_AM_I
);
352 dev_err(&data
->client
->dev
, "Error reading who_am_i\n");
356 dev_dbg(&data
->client
->dev
, "KXCJK1013 Chip Id %x\n", ret
);
358 ret
= kxcjk1013_set_mode(data
, STANDBY
);
362 ret
= i2c_smbus_read_byte_data(data
->client
, KXCJK1013_REG_CTRL1
);
364 dev_err(&data
->client
->dev
, "Error reading reg_ctrl1\n");
368 /* Set 12 bit mode */
369 ret
|= KXCJK1013_REG_CTRL1_BIT_RES
;
371 ret
= i2c_smbus_write_byte_data(data
->client
, KXCJK1013_REG_CTRL1
,
374 dev_err(&data
->client
->dev
, "Error reading reg_ctrl\n");
378 /* Setting range to 4G */
379 ret
= kxcjk1013_set_range(data
, KXCJK1013_RANGE_4G
);
383 ret
= i2c_smbus_read_byte_data(data
->client
, KXCJK1013_REG_DATA_CTRL
);
385 dev_err(&data
->client
->dev
, "Error reading reg_data_ctrl\n");
389 data
->odr_bits
= ret
;
391 /* Set up INT polarity */
392 ret
= i2c_smbus_read_byte_data(data
->client
, KXCJK1013_REG_INT_CTRL1
);
394 dev_err(&data
->client
->dev
, "Error reading reg_int_ctrl1\n");
398 if (data
->active_high_intr
)
399 ret
|= KXCJK1013_REG_INT_CTRL1_BIT_IEA
;
401 ret
&= ~KXCJK1013_REG_INT_CTRL1_BIT_IEA
;
403 ret
= i2c_smbus_write_byte_data(data
->client
, KXCJK1013_REG_INT_CTRL1
,
406 dev_err(&data
->client
->dev
, "Error writing reg_int_ctrl1\n");
410 ret
= kxcjk1013_set_mode(data
, OPERATION
);
414 data
->wake_thres
= KXCJK1013_DEFAULT_WAKE_THRES
;
420 static int kxcjk1013_get_startup_times(struct kxcjk1013_data
*data
)
423 int idx
= data
->chipset
;
425 for (i
= 0; i
< ARRAY_SIZE(odr_start_up_times
[idx
]); ++i
) {
426 if (odr_start_up_times
[idx
][i
].odr_bits
== data
->odr_bits
)
427 return odr_start_up_times
[idx
][i
].usec
;
430 return KXCJK1013_MAX_STARTUP_TIME_US
;
434 static int kxcjk1013_set_power_state(struct kxcjk1013_data
*data
, bool on
)
440 ret
= pm_runtime_get_sync(&data
->client
->dev
);
442 pm_runtime_mark_last_busy(&data
->client
->dev
);
443 ret
= pm_runtime_put_autosuspend(&data
->client
->dev
);
446 dev_err(&data
->client
->dev
,
447 "Failed: %s for %d\n", __func__
, on
);
449 pm_runtime_put_noidle(&data
->client
->dev
);
457 static int kxcjk1013_chip_update_thresholds(struct kxcjk1013_data
*data
)
461 ret
= i2c_smbus_write_byte_data(data
->client
,
462 KXCJK1013_REG_WAKE_TIMER
,
465 dev_err(&data
->client
->dev
,
466 "Error writing reg_wake_timer\n");
470 waketh_reg
= data
->chipset
== KXTF9
?
471 KXTF9_REG_WAKE_THRESH
: KXCJK1013_REG_WAKE_THRES
;
472 ret
= i2c_smbus_write_byte_data(data
->client
, waketh_reg
,
475 dev_err(&data
->client
->dev
, "Error writing reg_wake_thres\n");
482 static int kxcjk1013_setup_any_motion_interrupt(struct kxcjk1013_data
*data
,
486 enum kxcjk1013_mode store_mode
;
488 ret
= kxcjk1013_get_mode(data
, &store_mode
);
492 /* This is requirement by spec to change state to STANDBY */
493 ret
= kxcjk1013_set_mode(data
, STANDBY
);
497 ret
= kxcjk1013_chip_update_thresholds(data
);
501 ret
= i2c_smbus_read_byte_data(data
->client
, KXCJK1013_REG_INT_CTRL1
);
503 dev_err(&data
->client
->dev
, "Error reading reg_int_ctrl1\n");
508 ret
|= KXCJK1013_REG_INT_CTRL1_BIT_IEN
;
510 ret
&= ~KXCJK1013_REG_INT_CTRL1_BIT_IEN
;
512 ret
= i2c_smbus_write_byte_data(data
->client
, KXCJK1013_REG_INT_CTRL1
,
515 dev_err(&data
->client
->dev
, "Error writing reg_int_ctrl1\n");
519 ret
= i2c_smbus_read_byte_data(data
->client
, KXCJK1013_REG_CTRL1
);
521 dev_err(&data
->client
->dev
, "Error reading reg_ctrl1\n");
526 ret
|= KXCJK1013_REG_CTRL1_BIT_WUFE
;
528 ret
&= ~KXCJK1013_REG_CTRL1_BIT_WUFE
;
530 ret
= i2c_smbus_write_byte_data(data
->client
,
531 KXCJK1013_REG_CTRL1
, ret
);
533 dev_err(&data
->client
->dev
, "Error writing reg_ctrl1\n");
537 if (store_mode
== OPERATION
) {
538 ret
= kxcjk1013_set_mode(data
, OPERATION
);
546 static int kxcjk1013_setup_new_data_interrupt(struct kxcjk1013_data
*data
,
550 enum kxcjk1013_mode store_mode
;
552 ret
= kxcjk1013_get_mode(data
, &store_mode
);
556 /* This is requirement by spec to change state to STANDBY */
557 ret
= kxcjk1013_set_mode(data
, STANDBY
);
561 ret
= i2c_smbus_read_byte_data(data
->client
, KXCJK1013_REG_INT_CTRL1
);
563 dev_err(&data
->client
->dev
, "Error reading reg_int_ctrl1\n");
568 ret
|= KXCJK1013_REG_INT_CTRL1_BIT_IEN
;
570 ret
&= ~KXCJK1013_REG_INT_CTRL1_BIT_IEN
;
572 ret
= i2c_smbus_write_byte_data(data
->client
, KXCJK1013_REG_INT_CTRL1
,
575 dev_err(&data
->client
->dev
, "Error writing reg_int_ctrl1\n");
579 ret
= i2c_smbus_read_byte_data(data
->client
, KXCJK1013_REG_CTRL1
);
581 dev_err(&data
->client
->dev
, "Error reading reg_ctrl1\n");
586 ret
|= KXCJK1013_REG_CTRL1_BIT_DRDY
;
588 ret
&= ~KXCJK1013_REG_CTRL1_BIT_DRDY
;
590 ret
= i2c_smbus_write_byte_data(data
->client
,
591 KXCJK1013_REG_CTRL1
, ret
);
593 dev_err(&data
->client
->dev
, "Error writing reg_ctrl1\n");
597 if (store_mode
== OPERATION
) {
598 ret
= kxcjk1013_set_mode(data
, OPERATION
);
606 static const struct kx_odr_map
*kxcjk1013_find_odr_value(
607 const struct kx_odr_map
*map
, size_t map_size
, int val
, int val2
)
611 for (i
= 0; i
< map_size
; ++i
) {
612 if (map
[i
].val
== val
&& map
[i
].val2
== val2
)
616 return ERR_PTR(-EINVAL
);
619 static int kxcjk1013_convert_odr_value(const struct kx_odr_map
*map
,
620 size_t map_size
, int odr_bits
,
625 for (i
= 0; i
< map_size
; ++i
) {
626 if (map
[i
].odr_bits
== odr_bits
) {
629 return IIO_VAL_INT_PLUS_MICRO
;
636 static int kxcjk1013_set_odr(struct kxcjk1013_data
*data
, int val
, int val2
)
639 enum kxcjk1013_mode store_mode
;
640 const struct kx_odr_map
*odr_setting
;
642 ret
= kxcjk1013_get_mode(data
, &store_mode
);
646 if (data
->chipset
== KXTF9
)
647 odr_setting
= kxcjk1013_find_odr_value(kxtf9_samp_freq_table
,
648 ARRAY_SIZE(kxtf9_samp_freq_table
),
651 odr_setting
= kxcjk1013_find_odr_value(samp_freq_table
,
652 ARRAY_SIZE(samp_freq_table
),
655 if (IS_ERR(odr_setting
))
656 return PTR_ERR(odr_setting
);
658 /* To change ODR, the chip must be set to STANDBY as per spec */
659 ret
= kxcjk1013_set_mode(data
, STANDBY
);
663 ret
= i2c_smbus_write_byte_data(data
->client
, KXCJK1013_REG_DATA_CTRL
,
664 odr_setting
->odr_bits
);
666 dev_err(&data
->client
->dev
, "Error writing data_ctrl\n");
670 data
->odr_bits
= odr_setting
->odr_bits
;
672 ret
= i2c_smbus_write_byte_data(data
->client
, KXCJK1013_REG_CTRL2
,
673 odr_setting
->wuf_bits
);
675 dev_err(&data
->client
->dev
, "Error writing reg_ctrl2\n");
679 if (store_mode
== OPERATION
) {
680 ret
= kxcjk1013_set_mode(data
, OPERATION
);
688 static int kxcjk1013_get_odr(struct kxcjk1013_data
*data
, int *val
, int *val2
)
690 if (data
->chipset
== KXTF9
)
691 return kxcjk1013_convert_odr_value(kxtf9_samp_freq_table
,
692 ARRAY_SIZE(kxtf9_samp_freq_table
),
693 data
->odr_bits
, val
, val2
);
695 return kxcjk1013_convert_odr_value(samp_freq_table
,
696 ARRAY_SIZE(samp_freq_table
),
697 data
->odr_bits
, val
, val2
);
700 static int kxcjk1013_get_acc_reg(struct kxcjk1013_data
*data
, int axis
)
702 u8 reg
= KXCJK1013_REG_XOUT_L
+ axis
* 2;
705 ret
= i2c_smbus_read_word_data(data
->client
, reg
);
707 dev_err(&data
->client
->dev
,
708 "failed to read accel_%c registers\n", 'x' + axis
);
715 static int kxcjk1013_set_scale(struct kxcjk1013_data
*data
, int val
)
718 enum kxcjk1013_mode store_mode
;
720 for (i
= 0; i
< ARRAY_SIZE(KXCJK1013_scale_table
); ++i
) {
721 if (KXCJK1013_scale_table
[i
].scale
== val
) {
722 ret
= kxcjk1013_get_mode(data
, &store_mode
);
726 ret
= kxcjk1013_set_mode(data
, STANDBY
);
730 ret
= kxcjk1013_set_range(data
, i
);
734 if (store_mode
== OPERATION
) {
735 ret
= kxcjk1013_set_mode(data
, OPERATION
);
747 static int kxcjk1013_read_raw(struct iio_dev
*indio_dev
,
748 struct iio_chan_spec
const *chan
, int *val
,
749 int *val2
, long mask
)
751 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
755 case IIO_CHAN_INFO_RAW
:
756 mutex_lock(&data
->mutex
);
757 if (iio_buffer_enabled(indio_dev
))
760 ret
= kxcjk1013_set_power_state(data
, true);
762 mutex_unlock(&data
->mutex
);
765 ret
= kxcjk1013_get_acc_reg(data
, chan
->scan_index
);
767 kxcjk1013_set_power_state(data
, false);
768 mutex_unlock(&data
->mutex
);
771 *val
= sign_extend32(ret
>> 4, 11);
772 ret
= kxcjk1013_set_power_state(data
, false);
774 mutex_unlock(&data
->mutex
);
781 case IIO_CHAN_INFO_SCALE
:
783 *val2
= KXCJK1013_scale_table
[data
->range
].scale
;
784 return IIO_VAL_INT_PLUS_MICRO
;
786 case IIO_CHAN_INFO_SAMP_FREQ
:
787 mutex_lock(&data
->mutex
);
788 ret
= kxcjk1013_get_odr(data
, val
, val2
);
789 mutex_unlock(&data
->mutex
);
797 static int kxcjk1013_write_raw(struct iio_dev
*indio_dev
,
798 struct iio_chan_spec
const *chan
, int val
,
801 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
805 case IIO_CHAN_INFO_SAMP_FREQ
:
806 mutex_lock(&data
->mutex
);
807 ret
= kxcjk1013_set_odr(data
, val
, val2
);
808 mutex_unlock(&data
->mutex
);
810 case IIO_CHAN_INFO_SCALE
:
814 mutex_lock(&data
->mutex
);
815 ret
= kxcjk1013_set_scale(data
, val2
);
816 mutex_unlock(&data
->mutex
);
825 static int kxcjk1013_read_event(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 enum iio_event_info info
,
832 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
836 case IIO_EV_INFO_VALUE
:
837 *val
= data
->wake_thres
;
839 case IIO_EV_INFO_PERIOD
:
840 *val
= data
->wake_dur
;
849 static int kxcjk1013_write_event(struct iio_dev
*indio_dev
,
850 const struct iio_chan_spec
*chan
,
851 enum iio_event_type type
,
852 enum iio_event_direction dir
,
853 enum iio_event_info info
,
856 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
858 if (data
->ev_enable_state
)
862 case IIO_EV_INFO_VALUE
:
863 data
->wake_thres
= val
;
865 case IIO_EV_INFO_PERIOD
:
866 data
->wake_dur
= val
;
875 static int kxcjk1013_read_event_config(struct iio_dev
*indio_dev
,
876 const struct iio_chan_spec
*chan
,
877 enum iio_event_type type
,
878 enum iio_event_direction dir
)
880 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
882 return data
->ev_enable_state
;
885 static int kxcjk1013_write_event_config(struct iio_dev
*indio_dev
,
886 const struct iio_chan_spec
*chan
,
887 enum iio_event_type type
,
888 enum iio_event_direction dir
,
891 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
894 if (state
&& data
->ev_enable_state
)
897 mutex_lock(&data
->mutex
);
899 if (!state
&& data
->motion_trigger_on
) {
900 data
->ev_enable_state
= 0;
901 mutex_unlock(&data
->mutex
);
906 * We will expect the enable and disable to do operation in
907 * in reverse order. This will happen here anyway as our
908 * resume operation uses sync mode runtime pm calls, the
909 * suspend operation will be delayed by autosuspend delay
910 * So the disable operation will still happen in reverse of
911 * enable operation. When runtime pm is disabled the mode
912 * is always on so sequence doesn't matter
914 ret
= kxcjk1013_set_power_state(data
, state
);
916 mutex_unlock(&data
->mutex
);
920 ret
= kxcjk1013_setup_any_motion_interrupt(data
, state
);
922 kxcjk1013_set_power_state(data
, false);
923 data
->ev_enable_state
= 0;
924 mutex_unlock(&data
->mutex
);
928 data
->ev_enable_state
= state
;
929 mutex_unlock(&data
->mutex
);
934 static int kxcjk1013_buffer_preenable(struct iio_dev
*indio_dev
)
936 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
938 return kxcjk1013_set_power_state(data
, true);
941 static int kxcjk1013_buffer_postdisable(struct iio_dev
*indio_dev
)
943 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
945 return kxcjk1013_set_power_state(data
, false);
948 static ssize_t
kxcjk1013_get_samp_freq_avail(struct device
*dev
,
949 struct device_attribute
*attr
,
952 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
953 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
956 if (data
->chipset
== KXTF9
)
957 str
= kxtf9_samp_freq_avail
;
959 str
= kxcjk1013_samp_freq_avail
;
961 return sprintf(buf
, "%s\n", str
);
964 static IIO_DEVICE_ATTR(in_accel_sampling_frequency_available
, S_IRUGO
,
965 kxcjk1013_get_samp_freq_avail
, NULL
, 0);
967 static IIO_CONST_ATTR(in_accel_scale_available
, "0.009582 0.019163 0.038326");
969 static struct attribute
*kxcjk1013_attributes
[] = {
970 &iio_dev_attr_in_accel_sampling_frequency_available
.dev_attr
.attr
,
971 &iio_const_attr_in_accel_scale_available
.dev_attr
.attr
,
975 static const struct attribute_group kxcjk1013_attrs_group
= {
976 .attrs
= kxcjk1013_attributes
,
979 static const struct iio_event_spec kxcjk1013_event
= {
980 .type
= IIO_EV_TYPE_THRESH
,
981 .dir
= IIO_EV_DIR_EITHER
,
982 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
983 BIT(IIO_EV_INFO_ENABLE
) |
984 BIT(IIO_EV_INFO_PERIOD
)
987 static const struct iio_mount_matrix
*
988 kxcjk1013_get_mount_matrix(const struct iio_dev
*indio_dev
,
989 const struct iio_chan_spec
*chan
)
991 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
993 return &data
->orientation
;
996 static const struct iio_chan_spec_ext_info kxcjk1013_ext_info
[] = {
997 IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE
, kxcjk1013_get_mount_matrix
),
1001 #define KXCJK1013_CHANNEL(_axis) { \
1002 .type = IIO_ACCEL, \
1004 .channel2 = IIO_MOD_##_axis, \
1005 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
1006 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
1007 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
1008 .scan_index = AXIS_##_axis, \
1012 .storagebits = 16, \
1014 .endianness = IIO_LE, \
1016 .event_spec = &kxcjk1013_event, \
1017 .ext_info = kxcjk1013_ext_info, \
1018 .num_event_specs = 1 \
1021 static const struct iio_chan_spec kxcjk1013_channels
[] = {
1022 KXCJK1013_CHANNEL(X
),
1023 KXCJK1013_CHANNEL(Y
),
1024 KXCJK1013_CHANNEL(Z
),
1025 IIO_CHAN_SOFT_TIMESTAMP(3),
1028 static const struct iio_buffer_setup_ops kxcjk1013_buffer_setup_ops
= {
1029 .preenable
= kxcjk1013_buffer_preenable
,
1030 .postenable
= iio_triggered_buffer_postenable
,
1031 .postdisable
= kxcjk1013_buffer_postdisable
,
1032 .predisable
= iio_triggered_buffer_predisable
,
1035 static const struct iio_info kxcjk1013_info
= {
1036 .attrs
= &kxcjk1013_attrs_group
,
1037 .read_raw
= kxcjk1013_read_raw
,
1038 .write_raw
= kxcjk1013_write_raw
,
1039 .read_event_value
= kxcjk1013_read_event
,
1040 .write_event_value
= kxcjk1013_write_event
,
1041 .write_event_config
= kxcjk1013_write_event_config
,
1042 .read_event_config
= kxcjk1013_read_event_config
,
1045 static const unsigned long kxcjk1013_scan_masks
[] = {0x7, 0};
1047 static irqreturn_t
kxcjk1013_trigger_handler(int irq
, void *p
)
1049 struct iio_poll_func
*pf
= p
;
1050 struct iio_dev
*indio_dev
= pf
->indio_dev
;
1051 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
1054 mutex_lock(&data
->mutex
);
1055 ret
= i2c_smbus_read_i2c_block_data_or_emulated(data
->client
,
1056 KXCJK1013_REG_XOUT_L
,
1058 (u8
*)data
->buffer
);
1059 mutex_unlock(&data
->mutex
);
1063 iio_push_to_buffers_with_timestamp(indio_dev
, data
->buffer
,
1066 iio_trigger_notify_done(indio_dev
->trig
);
1071 static int kxcjk1013_trig_try_reen(struct iio_trigger
*trig
)
1073 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
1074 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
1077 ret
= i2c_smbus_read_byte_data(data
->client
, KXCJK1013_REG_INT_REL
);
1079 dev_err(&data
->client
->dev
, "Error reading reg_int_rel\n");
1086 static int kxcjk1013_data_rdy_trigger_set_state(struct iio_trigger
*trig
,
1089 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
1090 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
1093 mutex_lock(&data
->mutex
);
1095 if (!state
&& data
->ev_enable_state
&& data
->motion_trigger_on
) {
1096 data
->motion_trigger_on
= false;
1097 mutex_unlock(&data
->mutex
);
1101 ret
= kxcjk1013_set_power_state(data
, state
);
1103 mutex_unlock(&data
->mutex
);
1106 if (data
->motion_trig
== trig
)
1107 ret
= kxcjk1013_setup_any_motion_interrupt(data
, state
);
1109 ret
= kxcjk1013_setup_new_data_interrupt(data
, state
);
1111 kxcjk1013_set_power_state(data
, false);
1112 mutex_unlock(&data
->mutex
);
1115 if (data
->motion_trig
== trig
)
1116 data
->motion_trigger_on
= state
;
1118 data
->dready_trigger_on
= state
;
1120 mutex_unlock(&data
->mutex
);
1125 static const struct iio_trigger_ops kxcjk1013_trigger_ops
= {
1126 .set_trigger_state
= kxcjk1013_data_rdy_trigger_set_state
,
1127 .try_reenable
= kxcjk1013_trig_try_reen
,
1130 static void kxcjk1013_report_motion_event(struct iio_dev
*indio_dev
)
1132 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
1134 int ret
= i2c_smbus_read_byte_data(data
->client
,
1135 KXCJK1013_REG_INT_SRC2
);
1137 dev_err(&data
->client
->dev
, "Error reading reg_int_src2\n");
1141 if (ret
& KXCJK1013_REG_INT_SRC2_BIT_XN
)
1142 iio_push_event(indio_dev
,
1143 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1147 IIO_EV_DIR_FALLING
),
1150 if (ret
& KXCJK1013_REG_INT_SRC2_BIT_XP
)
1151 iio_push_event(indio_dev
,
1152 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1159 if (ret
& KXCJK1013_REG_INT_SRC2_BIT_YN
)
1160 iio_push_event(indio_dev
,
1161 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1165 IIO_EV_DIR_FALLING
),
1168 if (ret
& KXCJK1013_REG_INT_SRC2_BIT_YP
)
1169 iio_push_event(indio_dev
,
1170 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1177 if (ret
& KXCJK1013_REG_INT_SRC2_BIT_ZN
)
1178 iio_push_event(indio_dev
,
1179 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1183 IIO_EV_DIR_FALLING
),
1186 if (ret
& KXCJK1013_REG_INT_SRC2_BIT_ZP
)
1187 iio_push_event(indio_dev
,
1188 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1196 static irqreturn_t
kxcjk1013_event_handler(int irq
, void *private)
1198 struct iio_dev
*indio_dev
= private;
1199 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
1202 ret
= i2c_smbus_read_byte_data(data
->client
, KXCJK1013_REG_INT_SRC1
);
1204 dev_err(&data
->client
->dev
, "Error reading reg_int_src1\n");
1208 if (ret
& KXCJK1013_REG_INT_SRC1_BIT_WUFS
) {
1209 if (data
->chipset
== KXTF9
)
1210 iio_push_event(indio_dev
,
1211 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1213 IIO_MOD_X_AND_Y_AND_Z
,
1218 kxcjk1013_report_motion_event(indio_dev
);
1222 if (data
->dready_trigger_on
)
1225 ret
= i2c_smbus_read_byte_data(data
->client
, KXCJK1013_REG_INT_REL
);
1227 dev_err(&data
->client
->dev
, "Error reading reg_int_rel\n");
1232 static irqreturn_t
kxcjk1013_data_rdy_trig_poll(int irq
, void *private)
1234 struct iio_dev
*indio_dev
= private;
1235 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
1237 data
->timestamp
= iio_get_time_ns(indio_dev
);
1239 if (data
->dready_trigger_on
)
1240 iio_trigger_poll(data
->dready_trig
);
1241 else if (data
->motion_trigger_on
)
1242 iio_trigger_poll(data
->motion_trig
);
1244 if (data
->ev_enable_state
)
1245 return IRQ_WAKE_THREAD
;
1250 static const char *kxcjk1013_match_acpi_device(struct device
*dev
,
1251 enum kx_chipset
*chipset
,
1252 bool *is_smo8500_device
)
1254 const struct acpi_device_id
*id
;
1256 id
= acpi_match_device(dev
->driver
->acpi_match_table
, dev
);
1260 if (strcmp(id
->id
, "SMO8500") == 0)
1261 *is_smo8500_device
= true;
1263 *chipset
= (enum kx_chipset
)id
->driver_data
;
1265 return dev_name(dev
);
1268 static int kxcjk1013_probe(struct i2c_client
*client
,
1269 const struct i2c_device_id
*id
)
1271 struct kxcjk1013_data
*data
;
1272 struct iio_dev
*indio_dev
;
1273 struct kxcjk_1013_platform_data
*pdata
;
1277 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
1281 data
= iio_priv(indio_dev
);
1282 i2c_set_clientdata(client
, indio_dev
);
1283 data
->client
= client
;
1285 pdata
= dev_get_platdata(&client
->dev
);
1287 data
->active_high_intr
= pdata
->active_high_intr
;
1288 data
->orientation
= pdata
->orientation
;
1290 data
->active_high_intr
= true; /* default polarity */
1292 ret
= iio_read_mount_matrix(&client
->dev
, "mount-matrix",
1293 &data
->orientation
);
1299 data
->chipset
= (enum kx_chipset
)(id
->driver_data
);
1301 } else if (ACPI_HANDLE(&client
->dev
)) {
1302 name
= kxcjk1013_match_acpi_device(&client
->dev
,
1304 &data
->is_smo8500_device
);
1308 ret
= kxcjk1013_chip_init(data
);
1312 mutex_init(&data
->mutex
);
1314 indio_dev
->dev
.parent
= &client
->dev
;
1315 indio_dev
->channels
= kxcjk1013_channels
;
1316 indio_dev
->num_channels
= ARRAY_SIZE(kxcjk1013_channels
);
1317 indio_dev
->available_scan_masks
= kxcjk1013_scan_masks
;
1318 indio_dev
->name
= name
;
1319 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1320 indio_dev
->info
= &kxcjk1013_info
;
1322 if (client
->irq
> 0 && !data
->is_smo8500_device
) {
1323 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
1324 kxcjk1013_data_rdy_trig_poll
,
1325 kxcjk1013_event_handler
,
1326 IRQF_TRIGGER_RISING
,
1332 data
->dready_trig
= devm_iio_trigger_alloc(&client
->dev
,
1336 if (!data
->dready_trig
) {
1341 data
->motion_trig
= devm_iio_trigger_alloc(&client
->dev
,
1342 "%s-any-motion-dev%d",
1345 if (!data
->motion_trig
) {
1350 data
->dready_trig
->dev
.parent
= &client
->dev
;
1351 data
->dready_trig
->ops
= &kxcjk1013_trigger_ops
;
1352 iio_trigger_set_drvdata(data
->dready_trig
, indio_dev
);
1353 indio_dev
->trig
= data
->dready_trig
;
1354 iio_trigger_get(indio_dev
->trig
);
1355 ret
= iio_trigger_register(data
->dready_trig
);
1359 data
->motion_trig
->dev
.parent
= &client
->dev
;
1360 data
->motion_trig
->ops
= &kxcjk1013_trigger_ops
;
1361 iio_trigger_set_drvdata(data
->motion_trig
, indio_dev
);
1362 ret
= iio_trigger_register(data
->motion_trig
);
1364 data
->motion_trig
= NULL
;
1365 goto err_trigger_unregister
;
1369 ret
= iio_triggered_buffer_setup(indio_dev
,
1370 &iio_pollfunc_store_time
,
1371 kxcjk1013_trigger_handler
,
1372 &kxcjk1013_buffer_setup_ops
);
1374 dev_err(&client
->dev
, "iio triggered buffer setup failed\n");
1375 goto err_trigger_unregister
;
1378 ret
= pm_runtime_set_active(&client
->dev
);
1380 goto err_buffer_cleanup
;
1382 pm_runtime_enable(&client
->dev
);
1383 pm_runtime_set_autosuspend_delay(&client
->dev
,
1384 KXCJK1013_SLEEP_DELAY_MS
);
1385 pm_runtime_use_autosuspend(&client
->dev
);
1387 ret
= iio_device_register(indio_dev
);
1389 dev_err(&client
->dev
, "unable to register iio device\n");
1390 goto err_buffer_cleanup
;
1396 if (data
->dready_trig
)
1397 iio_triggered_buffer_cleanup(indio_dev
);
1398 err_trigger_unregister
:
1399 if (data
->dready_trig
)
1400 iio_trigger_unregister(data
->dready_trig
);
1401 if (data
->motion_trig
)
1402 iio_trigger_unregister(data
->motion_trig
);
1404 kxcjk1013_set_mode(data
, STANDBY
);
1409 static int kxcjk1013_remove(struct i2c_client
*client
)
1411 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
1412 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
1414 iio_device_unregister(indio_dev
);
1416 pm_runtime_disable(&client
->dev
);
1417 pm_runtime_set_suspended(&client
->dev
);
1418 pm_runtime_put_noidle(&client
->dev
);
1420 if (data
->dready_trig
) {
1421 iio_triggered_buffer_cleanup(indio_dev
);
1422 iio_trigger_unregister(data
->dready_trig
);
1423 iio_trigger_unregister(data
->motion_trig
);
1426 mutex_lock(&data
->mutex
);
1427 kxcjk1013_set_mode(data
, STANDBY
);
1428 mutex_unlock(&data
->mutex
);
1433 #ifdef CONFIG_PM_SLEEP
1434 static int kxcjk1013_suspend(struct device
*dev
)
1436 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1437 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
1440 mutex_lock(&data
->mutex
);
1441 ret
= kxcjk1013_set_mode(data
, STANDBY
);
1442 mutex_unlock(&data
->mutex
);
1447 static int kxcjk1013_resume(struct device
*dev
)
1449 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1450 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
1453 mutex_lock(&data
->mutex
);
1454 ret
= kxcjk1013_set_mode(data
, OPERATION
);
1456 ret
= kxcjk1013_set_range(data
, data
->range
);
1457 mutex_unlock(&data
->mutex
);
1464 static int kxcjk1013_runtime_suspend(struct device
*dev
)
1466 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1467 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
1470 ret
= kxcjk1013_set_mode(data
, STANDBY
);
1472 dev_err(&data
->client
->dev
, "powering off device failed\n");
1478 static int kxcjk1013_runtime_resume(struct device
*dev
)
1480 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1481 struct kxcjk1013_data
*data
= iio_priv(indio_dev
);
1485 ret
= kxcjk1013_set_mode(data
, OPERATION
);
1489 sleep_val
= kxcjk1013_get_startup_times(data
);
1490 if (sleep_val
< 20000)
1491 usleep_range(sleep_val
, 20000);
1493 msleep_interruptible(sleep_val
/1000);
1499 static const struct dev_pm_ops kxcjk1013_pm_ops
= {
1500 SET_SYSTEM_SLEEP_PM_OPS(kxcjk1013_suspend
, kxcjk1013_resume
)
1501 SET_RUNTIME_PM_OPS(kxcjk1013_runtime_suspend
,
1502 kxcjk1013_runtime_resume
, NULL
)
1505 static const struct acpi_device_id kx_acpi_match
[] = {
1506 {"KXCJ1013", KXCJK1013
},
1507 {"KXCJ1008", KXCJ91008
},
1508 {"KXCJ9000", KXCJ91008
},
1509 {"KIOX0008", KXCJ91008
},
1510 {"KIOX0009", KXTJ21009
},
1511 {"KIOX000A", KXCJ91008
},
1512 {"KIOX010A", KXCJ91008
}, /* KXCJ91008 in the display of a yoga 2-in-1 */
1513 {"KIOX020A", KXCJ91008
}, /* KXCJ91008 in the base of a yoga 2-in-1 */
1514 {"KXTJ1009", KXTJ21009
},
1515 {"KXJ2109", KXTJ21009
},
1516 {"SMO8500", KXCJ91008
},
1519 MODULE_DEVICE_TABLE(acpi
, kx_acpi_match
);
1521 static const struct i2c_device_id kxcjk1013_id
[] = {
1522 {"kxcjk1013", KXCJK1013
},
1523 {"kxcj91008", KXCJ91008
},
1524 {"kxtj21009", KXTJ21009
},
1526 {"SMO8500", KXCJ91008
},
1530 MODULE_DEVICE_TABLE(i2c
, kxcjk1013_id
);
1532 static const struct of_device_id kxcjk1013_of_match
[] = {
1533 { .compatible
= "kionix,kxcjk1013", },
1534 { .compatible
= "kionix,kxcj91008", },
1535 { .compatible
= "kionix,kxtj21009", },
1536 { .compatible
= "kionix,kxtf9", },
1539 MODULE_DEVICE_TABLE(of
, kxcjk1013_of_match
);
1541 static struct i2c_driver kxcjk1013_driver
= {
1543 .name
= KXCJK1013_DRV_NAME
,
1544 .acpi_match_table
= ACPI_PTR(kx_acpi_match
),
1545 .of_match_table
= kxcjk1013_of_match
,
1546 .pm
= &kxcjk1013_pm_ops
,
1548 .probe
= kxcjk1013_probe
,
1549 .remove
= kxcjk1013_remove
,
1550 .id_table
= kxcjk1013_id
,
1552 module_i2c_driver(kxcjk1013_driver
);
1554 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
1555 MODULE_LICENSE("GPL v2");
1556 MODULE_DESCRIPTION("KXCJK1013 accelerometer driver");