12025cf9eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21a4fbf6aSSrinivas Pandruvada /*
31a4fbf6aSSrinivas Pandruvada * KXCJK-1013 3-axis accelerometer driver
41a4fbf6aSSrinivas Pandruvada * Copyright (c) 2014, Intel Corporation.
51a4fbf6aSSrinivas Pandruvada */
61a4fbf6aSSrinivas Pandruvada
71a4fbf6aSSrinivas Pandruvada #include <linux/module.h>
81a4fbf6aSSrinivas Pandruvada #include <linux/i2c.h>
91a4fbf6aSSrinivas Pandruvada #include <linux/interrupt.h>
101a4fbf6aSSrinivas Pandruvada #include <linux/delay.h>
111a4fbf6aSSrinivas Pandruvada #include <linux/bitops.h>
121a4fbf6aSSrinivas Pandruvada #include <linux/slab.h>
131a4fbf6aSSrinivas Pandruvada #include <linux/string.h>
141a4fbf6aSSrinivas Pandruvada #include <linux/acpi.h>
15124e1b1dSSrinivas Pandruvada #include <linux/pm.h>
16124e1b1dSSrinivas Pandruvada #include <linux/pm_runtime.h>
171d2e91a2SDevajith V S #include <linux/regulator/consumer.h>
181a4fbf6aSSrinivas Pandruvada #include <linux/iio/iio.h>
191a4fbf6aSSrinivas Pandruvada #include <linux/iio/sysfs.h>
201a4fbf6aSSrinivas Pandruvada #include <linux/iio/buffer.h>
211a4fbf6aSSrinivas Pandruvada #include <linux/iio/trigger.h>
22b4b491c0SSrinivas Pandruvada #include <linux/iio/events.h>
231a4fbf6aSSrinivas Pandruvada #include <linux/iio/trigger_consumer.h>
241a4fbf6aSSrinivas Pandruvada #include <linux/iio/triggered_buffer.h>
251a4fbf6aSSrinivas Pandruvada #include <linux/iio/accel/kxcjk_1013.h>
261a4fbf6aSSrinivas Pandruvada
271a4fbf6aSSrinivas Pandruvada #define KXCJK1013_DRV_NAME "kxcjk1013"
281a4fbf6aSSrinivas Pandruvada #define KXCJK1013_IRQ_NAME "kxcjk1013_event"
291a4fbf6aSSrinivas Pandruvada
301540d010SMichał Mirosław #define KXTF9_REG_HP_XOUT_L 0x00
311540d010SMichał Mirosław #define KXTF9_REG_HP_XOUT_H 0x01
321540d010SMichał Mirosław #define KXTF9_REG_HP_YOUT_L 0x02
331540d010SMichał Mirosław #define KXTF9_REG_HP_YOUT_H 0x03
341540d010SMichał Mirosław #define KXTF9_REG_HP_ZOUT_L 0x04
351540d010SMichał Mirosław #define KXTF9_REG_HP_ZOUT_H 0x05
361540d010SMichał Mirosław
371a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_XOUT_L 0x06
381a4fbf6aSSrinivas Pandruvada /*
391a4fbf6aSSrinivas Pandruvada * From low byte X axis register, all the other addresses of Y and Z can be
401a4fbf6aSSrinivas Pandruvada * obtained by just applying axis offset. The following axis defines are just
411a4fbf6aSSrinivas Pandruvada * provide clarity, but not used.
421a4fbf6aSSrinivas Pandruvada */
431a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_XOUT_H 0x07
441a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_YOUT_L 0x08
451a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_YOUT_H 0x09
461a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_ZOUT_L 0x0A
471a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_ZOUT_H 0x0B
481a4fbf6aSSrinivas Pandruvada
491a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_DCST_RESP 0x0C
501a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_WHO_AM_I 0x0F
511540d010SMichał Mirosław #define KXTF9_REG_TILT_POS_CUR 0x10
521540d010SMichał Mirosław #define KXTF9_REG_TILT_POS_PREV 0x11
531540d010SMichał Mirosław #define KXTF9_REG_INT_SRC1 0x15
54ae4467f6SStephan Gerhold #define KXTF9_REG_INT_SRC2 0x16
55ae4467f6SStephan Gerhold #define KXCJK1013_REG_INT_SRC1 0x16
561a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_INT_SRC2 0x17
571a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_STATUS_REG 0x18
581a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_INT_REL 0x1A
591a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_CTRL1 0x1B
601540d010SMichał Mirosław #define KXTF9_REG_CTRL2 0x1C
61ae4467f6SStephan Gerhold #define KXTF9_REG_CTRL3 0x1D
62ae4467f6SStephan Gerhold #define KXCJK1013_REG_CTRL2 0x1D
631a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_INT_CTRL1 0x1E
641a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_INT_CTRL2 0x1F
651540d010SMichał Mirosław #define KXTF9_REG_INT_CTRL3 0x20
661a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_DATA_CTRL 0x21
671540d010SMichał Mirosław #define KXTF9_REG_TILT_TIMER 0x28
681a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_WAKE_TIMER 0x29
691540d010SMichał Mirosław #define KXTF9_REG_TDT_TIMER 0x2B
701540d010SMichał Mirosław #define KXTF9_REG_TDT_THRESH_H 0x2C
711540d010SMichał Mirosław #define KXTF9_REG_TDT_THRESH_L 0x2D
721540d010SMichał Mirosław #define KXTF9_REG_TDT_TAP_TIMER 0x2E
731540d010SMichał Mirosław #define KXTF9_REG_TDT_TOTAL_TIMER 0x2F
741540d010SMichał Mirosław #define KXTF9_REG_TDT_LATENCY_TIMER 0x30
751540d010SMichał Mirosław #define KXTF9_REG_TDT_WINDOW_TIMER 0x31
761a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_SELF_TEST 0x3A
771540d010SMichał Mirosław #define KXTF9_REG_WAKE_THRESH 0x5A
781540d010SMichał Mirosław #define KXTF9_REG_TILT_ANGLE 0x5C
791540d010SMichał Mirosław #define KXTF9_REG_HYST_SET 0x5F
801a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_WAKE_THRES 0x6A
811a4fbf6aSSrinivas Pandruvada
82c630c176SStephan Gerhold /* Everything up to 0x11 is equal to KXCJK1013/KXTF9 above */
83c630c176SStephan Gerhold #define KX023_REG_INS1 0x12
84c630c176SStephan Gerhold #define KX023_REG_INS2 0x13
85c630c176SStephan Gerhold #define KX023_REG_INS3 0x14
86c630c176SStephan Gerhold #define KX023_REG_STAT 0x15
87c630c176SStephan Gerhold #define KX023_REG_INT_REL 0x17
88c630c176SStephan Gerhold #define KX023_REG_CNTL1 0x18
89c630c176SStephan Gerhold #define KX023_REG_CNTL2 0x19
90c630c176SStephan Gerhold #define KX023_REG_CNTL3 0x1A
91c630c176SStephan Gerhold #define KX023_REG_ODCNTL 0x1B
92c630c176SStephan Gerhold #define KX023_REG_INC1 0x1C
93c630c176SStephan Gerhold #define KX023_REG_INC2 0x1D
94c630c176SStephan Gerhold #define KX023_REG_INC3 0x1E
95c630c176SStephan Gerhold #define KX023_REG_INC4 0x1F
96c630c176SStephan Gerhold #define KX023_REG_INC5 0x20
97c630c176SStephan Gerhold #define KX023_REG_INC6 0x21
98c630c176SStephan Gerhold #define KX023_REG_TILT_TIMER 0x22
99c630c176SStephan Gerhold #define KX023_REG_WUFC 0x23
100c630c176SStephan Gerhold #define KX023_REG_TDTRC 0x24
101c630c176SStephan Gerhold #define KX023_REG_TDTC 0x25
102c630c176SStephan Gerhold #define KX023_REG_TTH 0x26
103c630c176SStephan Gerhold #define KX023_REG_TTL 0x27
104c630c176SStephan Gerhold #define KX023_REG_FTD 0x28
105c630c176SStephan Gerhold #define KX023_REG_STD 0x29
106c630c176SStephan Gerhold #define KX023_REG_TLT 0x2A
107c630c176SStephan Gerhold #define KX023_REG_TWS 0x2B
108c630c176SStephan Gerhold #define KX023_REG_ATH 0x30
109c630c176SStephan Gerhold #define KX023_REG_TILT_ANGLE_LL 0x32
110c630c176SStephan Gerhold #define KX023_REG_TILT_ANGLE_HL 0x33
111c630c176SStephan Gerhold #define KX023_REG_HYST_SET 0x34
112c630c176SStephan Gerhold #define KX023_REG_LP_CNTL 0x35
113c630c176SStephan Gerhold #define KX023_REG_BUF_CNTL1 0x3A
114c630c176SStephan Gerhold #define KX023_REG_BUF_CNTL2 0x3B
115c630c176SStephan Gerhold #define KX023_REG_BUF_STATUS_1 0x3C
116c630c176SStephan Gerhold #define KX023_REG_BUF_STATUS_2 0x3D
117c630c176SStephan Gerhold #define KX023_REG_BUF_CLEAR 0x3E
118c630c176SStephan Gerhold #define KX023_REG_BUF_READ 0x3F
119c630c176SStephan Gerhold #define KX023_REG_SELF_TEST 0x60
120c630c176SStephan Gerhold
1211a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_CTRL1_BIT_PC1 BIT(7)
1221a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_CTRL1_BIT_RES BIT(6)
1231a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_CTRL1_BIT_DRDY BIT(5)
1241a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_CTRL1_BIT_GSEL1 BIT(4)
1251a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_CTRL1_BIT_GSEL0 BIT(3)
1261a4fbf6aSSrinivas Pandruvada #define KXCJK1013_REG_CTRL1_BIT_WUFE BIT(1)
127f74e440cSMichał Mirosław
1281540d010SMichał Mirosław #define KXCJK1013_REG_INT_CTRL1_BIT_IEU BIT(2) /* KXTF9 */
129f74e440cSMichał Mirosław #define KXCJK1013_REG_INT_CTRL1_BIT_IEL BIT(3)
130f74e440cSMichał Mirosław #define KXCJK1013_REG_INT_CTRL1_BIT_IEA BIT(4)
131f74e440cSMichał Mirosław #define KXCJK1013_REG_INT_CTRL1_BIT_IEN BIT(5)
1321a4fbf6aSSrinivas Pandruvada
1331540d010SMichał Mirosław #define KXTF9_REG_TILT_BIT_LEFT_EDGE BIT(5)
1341540d010SMichał Mirosław #define KXTF9_REG_TILT_BIT_RIGHT_EDGE BIT(4)
1351540d010SMichał Mirosław #define KXTF9_REG_TILT_BIT_LOWER_EDGE BIT(3)
1361540d010SMichał Mirosław #define KXTF9_REG_TILT_BIT_UPPER_EDGE BIT(2)
1371540d010SMichał Mirosław #define KXTF9_REG_TILT_BIT_FACE_DOWN BIT(1)
1381540d010SMichał Mirosław #define KXTF9_REG_TILT_BIT_FACE_UP BIT(0)
1391540d010SMichał Mirosław
1401a4fbf6aSSrinivas Pandruvada #define KXCJK1013_DATA_MASK_12_BIT 0x0FFF
1411a4fbf6aSSrinivas Pandruvada #define KXCJK1013_MAX_STARTUP_TIME_US 100000
1421a4fbf6aSSrinivas Pandruvada
143124e1b1dSSrinivas Pandruvada #define KXCJK1013_SLEEP_DELAY_MS 2000
144124e1b1dSSrinivas Pandruvada
1451540d010SMichał Mirosław #define KXCJK1013_REG_INT_SRC1_BIT_TPS BIT(0) /* KXTF9 */
146f74e440cSMichał Mirosław #define KXCJK1013_REG_INT_SRC1_BIT_WUFS BIT(1)
1471540d010SMichał Mirosław #define KXCJK1013_REG_INT_SRC1_MASK_TDTS (BIT(2) | BIT(3)) /* KXTF9 */
1481540d010SMichał Mirosław #define KXCJK1013_REG_INT_SRC1_TAP_NONE 0
1491540d010SMichał Mirosław #define KXCJK1013_REG_INT_SRC1_TAP_SINGLE BIT(2)
1501540d010SMichał Mirosław #define KXCJK1013_REG_INT_SRC1_TAP_DOUBLE BIT(3)
151f74e440cSMichał Mirosław #define KXCJK1013_REG_INT_SRC1_BIT_DRDY BIT(4)
152f74e440cSMichał Mirosław
1531540d010SMichał Mirosław /* KXCJK: INT_SOURCE2: motion detect, KXTF9: INT_SRC_REG1: tap detect */
154b4b491c0SSrinivas Pandruvada #define KXCJK1013_REG_INT_SRC2_BIT_ZP BIT(0)
155b4b491c0SSrinivas Pandruvada #define KXCJK1013_REG_INT_SRC2_BIT_ZN BIT(1)
156b4b491c0SSrinivas Pandruvada #define KXCJK1013_REG_INT_SRC2_BIT_YP BIT(2)
157b4b491c0SSrinivas Pandruvada #define KXCJK1013_REG_INT_SRC2_BIT_YN BIT(3)
158b4b491c0SSrinivas Pandruvada #define KXCJK1013_REG_INT_SRC2_BIT_XP BIT(4)
159b4b491c0SSrinivas Pandruvada #define KXCJK1013_REG_INT_SRC2_BIT_XN BIT(5)
160b4b491c0SSrinivas Pandruvada
161c630c176SStephan Gerhold /* KX023 interrupt routing to INT1. INT2 can be configured with INC6 */
162c630c176SStephan Gerhold #define KX023_REG_INC4_BFI1 BIT(6)
163c630c176SStephan Gerhold #define KX023_REG_INC4_WMI1 BIT(5)
164c630c176SStephan Gerhold #define KX023_REG_INC4_DRDY1 BIT(4)
165c630c176SStephan Gerhold #define KX023_REG_INC4_TDTI1 BIT(2)
166c630c176SStephan Gerhold #define KX023_REG_INC4_WUFI1 BIT(1)
167c630c176SStephan Gerhold #define KX023_REG_INC4_TPI1 BIT(0)
168c630c176SStephan Gerhold
169b4b491c0SSrinivas Pandruvada #define KXCJK1013_DEFAULT_WAKE_THRES 1
170b4b491c0SSrinivas Pandruvada
171c6861377SDaniel Baluta enum kx_chipset {
172c6861377SDaniel Baluta KXCJK1013,
173c6861377SDaniel Baluta KXCJ91008,
174c6861377SDaniel Baluta KXTJ21009,
1751540d010SMichał Mirosław KXTF9,
176*d5cbe150SRayyan Ansari KX0221020,
177c630c176SStephan Gerhold KX0231025,
178c6861377SDaniel Baluta KX_MAX_CHIPS /* this must be last */
179c6861377SDaniel Baluta };
180c6861377SDaniel Baluta
18111e94f28SHans de Goede enum kx_acpi_type {
18211e94f28SHans de Goede ACPI_GENERIC,
18311e94f28SHans de Goede ACPI_SMO8500,
184e5b1032aSHans de Goede ACPI_KIOX010A,
18511e94f28SHans de Goede };
18611e94f28SHans de Goede
187ae4467f6SStephan Gerhold struct kx_chipset_regs {
188ae4467f6SStephan Gerhold u8 int_src1;
189ae4467f6SStephan Gerhold u8 int_src2;
190ae4467f6SStephan Gerhold u8 int_rel;
191ae4467f6SStephan Gerhold u8 ctrl1;
192ae4467f6SStephan Gerhold u8 wuf_ctrl;
193ae4467f6SStephan Gerhold u8 int_ctrl1;
194ae4467f6SStephan Gerhold u8 data_ctrl;
195ae4467f6SStephan Gerhold u8 wake_timer;
196ae4467f6SStephan Gerhold u8 wake_thres;
197ae4467f6SStephan Gerhold };
198ae4467f6SStephan Gerhold
199ae4467f6SStephan Gerhold static const struct kx_chipset_regs kxcjk1013_regs = {
200ae4467f6SStephan Gerhold .int_src1 = KXCJK1013_REG_INT_SRC1,
201ae4467f6SStephan Gerhold .int_src2 = KXCJK1013_REG_INT_SRC2,
202ae4467f6SStephan Gerhold .int_rel = KXCJK1013_REG_INT_REL,
203ae4467f6SStephan Gerhold .ctrl1 = KXCJK1013_REG_CTRL1,
204ae4467f6SStephan Gerhold .wuf_ctrl = KXCJK1013_REG_CTRL2,
205ae4467f6SStephan Gerhold .int_ctrl1 = KXCJK1013_REG_INT_CTRL1,
206ae4467f6SStephan Gerhold .data_ctrl = KXCJK1013_REG_DATA_CTRL,
207ae4467f6SStephan Gerhold .wake_timer = KXCJK1013_REG_WAKE_TIMER,
208ae4467f6SStephan Gerhold .wake_thres = KXCJK1013_REG_WAKE_THRES,
209ae4467f6SStephan Gerhold };
210ae4467f6SStephan Gerhold
211ae4467f6SStephan Gerhold static const struct kx_chipset_regs kxtf9_regs = {
212ae4467f6SStephan Gerhold /* .int_src1 was moved to INT_SRC2 on KXTF9 */
213ae4467f6SStephan Gerhold .int_src1 = KXTF9_REG_INT_SRC2,
214ae4467f6SStephan Gerhold /* .int_src2 is not available */
215ae4467f6SStephan Gerhold .int_rel = KXCJK1013_REG_INT_REL,
216ae4467f6SStephan Gerhold .ctrl1 = KXCJK1013_REG_CTRL1,
217ae4467f6SStephan Gerhold .wuf_ctrl = KXTF9_REG_CTRL3,
218ae4467f6SStephan Gerhold .int_ctrl1 = KXCJK1013_REG_INT_CTRL1,
219ae4467f6SStephan Gerhold .data_ctrl = KXCJK1013_REG_DATA_CTRL,
220ae4467f6SStephan Gerhold .wake_timer = KXCJK1013_REG_WAKE_TIMER,
221ae4467f6SStephan Gerhold .wake_thres = KXTF9_REG_WAKE_THRESH,
222ae4467f6SStephan Gerhold };
223ae4467f6SStephan Gerhold
224c630c176SStephan Gerhold /* The registers have totally different names but the bits are compatible */
225c630c176SStephan Gerhold static const struct kx_chipset_regs kx0231025_regs = {
226c630c176SStephan Gerhold .int_src1 = KX023_REG_INS2,
227c630c176SStephan Gerhold .int_src2 = KX023_REG_INS3,
228c630c176SStephan Gerhold .int_rel = KX023_REG_INT_REL,
229c630c176SStephan Gerhold .ctrl1 = KX023_REG_CNTL1,
230c630c176SStephan Gerhold .wuf_ctrl = KX023_REG_CNTL3,
231c630c176SStephan Gerhold .int_ctrl1 = KX023_REG_INC1,
232c630c176SStephan Gerhold .data_ctrl = KX023_REG_ODCNTL,
233c630c176SStephan Gerhold .wake_timer = KX023_REG_WUFC,
234c630c176SStephan Gerhold .wake_thres = KX023_REG_ATH,
235c630c176SStephan Gerhold };
236c630c176SStephan Gerhold
2373ab3aa2eSJonathan Cameron enum kxcjk1013_axis {
2383ab3aa2eSJonathan Cameron AXIS_X,
2393ab3aa2eSJonathan Cameron AXIS_Y,
2403ab3aa2eSJonathan Cameron AXIS_Z,
2413ab3aa2eSJonathan Cameron AXIS_MAX
2423ab3aa2eSJonathan Cameron };
2433ab3aa2eSJonathan Cameron
2441a4fbf6aSSrinivas Pandruvada struct kxcjk1013_data {
2451a4fbf6aSSrinivas Pandruvada struct i2c_client *client;
246b4b491c0SSrinivas Pandruvada struct iio_trigger *dready_trig;
247b4b491c0SSrinivas Pandruvada struct iio_trigger *motion_trig;
2481bde330cSDmitry Osipenko struct iio_mount_matrix orientation;
2491a4fbf6aSSrinivas Pandruvada struct mutex mutex;
2503ab3aa2eSJonathan Cameron /* Ensure timestamp naturally aligned */
2513ab3aa2eSJonathan Cameron struct {
2523ab3aa2eSJonathan Cameron s16 chans[AXIS_MAX];
2533ab3aa2eSJonathan Cameron s64 timestamp __aligned(8);
2543ab3aa2eSJonathan Cameron } scan;
2551a4fbf6aSSrinivas Pandruvada u8 odr_bits;
256a735e3d7SSrinivas Pandruvada u8 range;
257b4b491c0SSrinivas Pandruvada int wake_thres;
258b4b491c0SSrinivas Pandruvada int wake_dur;
2591a4fbf6aSSrinivas Pandruvada bool active_high_intr;
260b4b491c0SSrinivas Pandruvada bool dready_trigger_on;
261b4b491c0SSrinivas Pandruvada int ev_enable_state;
262b4b491c0SSrinivas Pandruvada bool motion_trigger_on;
263b4b491c0SSrinivas Pandruvada int64_t timestamp;
264c6861377SDaniel Baluta enum kx_chipset chipset;
26511e94f28SHans de Goede enum kx_acpi_type acpi_type;
266ae4467f6SStephan Gerhold const struct kx_chipset_regs *regs;
2671a4fbf6aSSrinivas Pandruvada };
2681a4fbf6aSSrinivas Pandruvada
2691a4fbf6aSSrinivas Pandruvada enum kxcjk1013_mode {
2701a4fbf6aSSrinivas Pandruvada STANDBY,
2711a4fbf6aSSrinivas Pandruvada OPERATION,
2721a4fbf6aSSrinivas Pandruvada };
2731a4fbf6aSSrinivas Pandruvada
274a735e3d7SSrinivas Pandruvada enum kxcjk1013_range {
275a735e3d7SSrinivas Pandruvada KXCJK1013_RANGE_2G,
276a735e3d7SSrinivas Pandruvada KXCJK1013_RANGE_4G,
277a735e3d7SSrinivas Pandruvada KXCJK1013_RANGE_8G,
278a735e3d7SSrinivas Pandruvada };
279a735e3d7SSrinivas Pandruvada
2807469ac69SMichał Mirosław struct kx_odr_map {
2811a4fbf6aSSrinivas Pandruvada int val;
2821a4fbf6aSSrinivas Pandruvada int val2;
2831a4fbf6aSSrinivas Pandruvada int odr_bits;
2847469ac69SMichał Mirosław int wuf_bits;
2857469ac69SMichał Mirosław };
2867469ac69SMichał Mirosław
2877469ac69SMichał Mirosław static const struct kx_odr_map samp_freq_table[] = {
2887469ac69SMichał Mirosław { 0, 781000, 0x08, 0x00 },
2897469ac69SMichał Mirosław { 1, 563000, 0x09, 0x01 },
2907469ac69SMichał Mirosław { 3, 125000, 0x0A, 0x02 },
2917469ac69SMichał Mirosław { 6, 250000, 0x0B, 0x03 },
2927469ac69SMichał Mirosław { 12, 500000, 0x00, 0x04 },
2937469ac69SMichał Mirosław { 25, 0, 0x01, 0x05 },
2947469ac69SMichał Mirosław { 50, 0, 0x02, 0x06 },
2957469ac69SMichał Mirosław { 100, 0, 0x03, 0x06 },
2967469ac69SMichał Mirosław { 200, 0, 0x04, 0x06 },
2977469ac69SMichał Mirosław { 400, 0, 0x05, 0x06 },
2987469ac69SMichał Mirosław { 800, 0, 0x06, 0x06 },
2997469ac69SMichał Mirosław { 1600, 0, 0x07, 0x06 },
3007469ac69SMichał Mirosław };
3011a4fbf6aSSrinivas Pandruvada
30281fa8cb9SMichał Mirosław static const char *const kxcjk1013_samp_freq_avail =
30381fa8cb9SMichał Mirosław "0.781000 1.563000 3.125000 6.250000 12.500000 25 50 100 200 400 800 1600";
30481fa8cb9SMichał Mirosław
3051540d010SMichał Mirosław static const struct kx_odr_map kxtf9_samp_freq_table[] = {
3061540d010SMichał Mirosław { 25, 0, 0x01, 0x00 },
3071540d010SMichał Mirosław { 50, 0, 0x02, 0x01 },
3081540d010SMichał Mirosław { 100, 0, 0x03, 0x01 },
3091540d010SMichał Mirosław { 200, 0, 0x04, 0x01 },
3101540d010SMichał Mirosław { 400, 0, 0x05, 0x01 },
3111540d010SMichał Mirosław { 800, 0, 0x06, 0x01 },
3121540d010SMichał Mirosław };
3131540d010SMichał Mirosław
3141540d010SMichał Mirosław static const char *const kxtf9_samp_freq_avail =
3151540d010SMichał Mirosław "25 50 100 200 400 800";
3161540d010SMichał Mirosław
3171a4fbf6aSSrinivas Pandruvada /* Refer to section 4 of the specification */
3188ebbfb98SJonathan Cameron static __maybe_unused const struct {
3191a4fbf6aSSrinivas Pandruvada int odr_bits;
3201a4fbf6aSSrinivas Pandruvada int usec;
321c6861377SDaniel Baluta } odr_start_up_times[KX_MAX_CHIPS][12] = {
322c6861377SDaniel Baluta /* KXCJK-1013 */
323c6861377SDaniel Baluta {
324c6861377SDaniel Baluta {0x08, 100000},
325c6861377SDaniel Baluta {0x09, 100000},
326c6861377SDaniel Baluta {0x0A, 100000},
327c6861377SDaniel Baluta {0x0B, 100000},
328c6861377SDaniel Baluta {0, 80000},
329c6861377SDaniel Baluta {0x01, 41000},
330c6861377SDaniel Baluta {0x02, 21000},
331c6861377SDaniel Baluta {0x03, 11000},
332c6861377SDaniel Baluta {0x04, 6400},
333c6861377SDaniel Baluta {0x05, 3900},
334c6861377SDaniel Baluta {0x06, 2700},
335c6861377SDaniel Baluta {0x07, 2100},
336c6861377SDaniel Baluta },
337c6861377SDaniel Baluta /* KXCJ9-1008 */
338c6861377SDaniel Baluta {
339c6861377SDaniel Baluta {0x08, 100000},
340c6861377SDaniel Baluta {0x09, 100000},
341c6861377SDaniel Baluta {0x0A, 100000},
342c6861377SDaniel Baluta {0x0B, 100000},
343c6861377SDaniel Baluta {0, 80000},
344c6861377SDaniel Baluta {0x01, 41000},
345c6861377SDaniel Baluta {0x02, 21000},
346c6861377SDaniel Baluta {0x03, 11000},
347c6861377SDaniel Baluta {0x04, 6400},
348c6861377SDaniel Baluta {0x05, 3900},
349c6861377SDaniel Baluta {0x06, 2700},
350c6861377SDaniel Baluta {0x07, 2100},
351c6861377SDaniel Baluta },
352c6861377SDaniel Baluta /* KXCTJ2-1009 */
353c6861377SDaniel Baluta {
354c6861377SDaniel Baluta {0x08, 1240000},
355c6861377SDaniel Baluta {0x09, 621000},
356c6861377SDaniel Baluta {0x0A, 309000},
357c6861377SDaniel Baluta {0x0B, 151000},
358c6861377SDaniel Baluta {0, 80000},
359c6861377SDaniel Baluta {0x01, 41000},
360c6861377SDaniel Baluta {0x02, 21000},
361c6861377SDaniel Baluta {0x03, 11000},
362c6861377SDaniel Baluta {0x04, 6000},
363c6861377SDaniel Baluta {0x05, 4000},
364c6861377SDaniel Baluta {0x06, 3000},
365c6861377SDaniel Baluta {0x07, 2000},
366c6861377SDaniel Baluta },
3671540d010SMichał Mirosław /* KXTF9 */
3681540d010SMichał Mirosław {
3691540d010SMichał Mirosław {0x01, 81000},
3701540d010SMichał Mirosław {0x02, 41000},
3711540d010SMichał Mirosław {0x03, 21000},
3721540d010SMichał Mirosław {0x04, 11000},
3731540d010SMichał Mirosław {0x05, 5100},
3741540d010SMichał Mirosław {0x06, 2700},
3751540d010SMichał Mirosław },
376c630c176SStephan Gerhold /* KX023-1025 */
377c630c176SStephan Gerhold {
378c630c176SStephan Gerhold /* First 4 are not in datasheet, taken from KXCTJ2-1009 */
379c630c176SStephan Gerhold {0x08, 1240000},
380c630c176SStephan Gerhold {0x09, 621000},
381c630c176SStephan Gerhold {0x0A, 309000},
382c630c176SStephan Gerhold {0x0B, 151000},
383c630c176SStephan Gerhold {0, 81000},
384c630c176SStephan Gerhold {0x01, 40000},
385c630c176SStephan Gerhold {0x02, 22000},
386c630c176SStephan Gerhold {0x03, 12000},
387c630c176SStephan Gerhold {0x04, 7000},
388c630c176SStephan Gerhold {0x05, 4400},
389c630c176SStephan Gerhold {0x06, 3000},
390c630c176SStephan Gerhold {0x07, 3000},
391c630c176SStephan Gerhold },
392c6861377SDaniel Baluta };
3931a4fbf6aSSrinivas Pandruvada
394a735e3d7SSrinivas Pandruvada static const struct {
395a735e3d7SSrinivas Pandruvada u16 scale;
396a735e3d7SSrinivas Pandruvada u8 gsel_0;
397a735e3d7SSrinivas Pandruvada u8 gsel_1;
398a735e3d7SSrinivas Pandruvada } KXCJK1013_scale_table[] = { {9582, 0, 0},
399a735e3d7SSrinivas Pandruvada {19163, 1, 0},
400a735e3d7SSrinivas Pandruvada {38326, 0, 1} };
401a735e3d7SSrinivas Pandruvada
402e5b1032aSHans de Goede #ifdef CONFIG_ACPI
403e5b1032aSHans de Goede enum kiox010a_fn_index {
404e5b1032aSHans de Goede KIOX010A_SET_LAPTOP_MODE = 1,
405e5b1032aSHans de Goede KIOX010A_SET_TABLET_MODE = 2,
406e5b1032aSHans de Goede };
407e5b1032aSHans de Goede
kiox010a_dsm(struct device * dev,int fn_index)408e5b1032aSHans de Goede static int kiox010a_dsm(struct device *dev, int fn_index)
409e5b1032aSHans de Goede {
410e5b1032aSHans de Goede acpi_handle handle = ACPI_HANDLE(dev);
411e5b1032aSHans de Goede guid_t kiox010a_dsm_guid;
412e5b1032aSHans de Goede union acpi_object *obj;
413e5b1032aSHans de Goede
414e5b1032aSHans de Goede if (!handle)
415e5b1032aSHans de Goede return -ENODEV;
416e5b1032aSHans de Goede
417e5b1032aSHans de Goede guid_parse("1f339696-d475-4e26-8cad-2e9f8e6d7a91", &kiox010a_dsm_guid);
418e5b1032aSHans de Goede
419e5b1032aSHans de Goede obj = acpi_evaluate_dsm(handle, &kiox010a_dsm_guid, 1, fn_index, NULL);
420e5b1032aSHans de Goede if (!obj)
421e5b1032aSHans de Goede return -EIO;
422e5b1032aSHans de Goede
423e5b1032aSHans de Goede ACPI_FREE(obj);
424e5b1032aSHans de Goede return 0;
425e5b1032aSHans de Goede }
426ab3764c7SJonathan Cameron
427ab3764c7SJonathan Cameron static const struct acpi_device_id kx_acpi_match[] = {
428ab3764c7SJonathan Cameron {"KXCJ1013", KXCJK1013},
429ab3764c7SJonathan Cameron {"KXCJ1008", KXCJ91008},
430ab3764c7SJonathan Cameron {"KXCJ9000", KXCJ91008},
431ab3764c7SJonathan Cameron {"KIOX0008", KXCJ91008},
432ab3764c7SJonathan Cameron {"KIOX0009", KXTJ21009},
433ab3764c7SJonathan Cameron {"KIOX000A", KXCJ91008},
434ab3764c7SJonathan Cameron {"KIOX010A", KXCJ91008}, /* KXCJ91008 in the display of a yoga 2-in-1 */
435ab3764c7SJonathan Cameron {"KIOX020A", KXCJ91008}, /* KXCJ91008 in the base of a yoga 2-in-1 */
436ab3764c7SJonathan Cameron {"KXTJ1009", KXTJ21009},
437ab3764c7SJonathan Cameron {"KXJ2109", KXTJ21009},
438ab3764c7SJonathan Cameron {"SMO8500", KXCJ91008},
439ab3764c7SJonathan Cameron { }
440ab3764c7SJonathan Cameron };
441ab3764c7SJonathan Cameron MODULE_DEVICE_TABLE(acpi, kx_acpi_match);
442ab3764c7SJonathan Cameron
443e5b1032aSHans de Goede #endif
444e5b1032aSHans de Goede
kxcjk1013_set_mode(struct kxcjk1013_data * data,enum kxcjk1013_mode mode)4451a4fbf6aSSrinivas Pandruvada static int kxcjk1013_set_mode(struct kxcjk1013_data *data,
4461a4fbf6aSSrinivas Pandruvada enum kxcjk1013_mode mode)
4471a4fbf6aSSrinivas Pandruvada {
4481a4fbf6aSSrinivas Pandruvada int ret;
4491a4fbf6aSSrinivas Pandruvada
450ae4467f6SStephan Gerhold ret = i2c_smbus_read_byte_data(data->client, data->regs->ctrl1);
4511a4fbf6aSSrinivas Pandruvada if (ret < 0) {
4521a4fbf6aSSrinivas Pandruvada dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
4531a4fbf6aSSrinivas Pandruvada return ret;
4541a4fbf6aSSrinivas Pandruvada }
4551a4fbf6aSSrinivas Pandruvada
4561a4fbf6aSSrinivas Pandruvada if (mode == STANDBY)
4571a4fbf6aSSrinivas Pandruvada ret &= ~KXCJK1013_REG_CTRL1_BIT_PC1;
4581a4fbf6aSSrinivas Pandruvada else
4591a4fbf6aSSrinivas Pandruvada ret |= KXCJK1013_REG_CTRL1_BIT_PC1;
4601a4fbf6aSSrinivas Pandruvada
461ae4467f6SStephan Gerhold ret = i2c_smbus_write_byte_data(data->client, data->regs->ctrl1, ret);
4621a4fbf6aSSrinivas Pandruvada if (ret < 0) {
4631a4fbf6aSSrinivas Pandruvada dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
4641a4fbf6aSSrinivas Pandruvada return ret;
4651a4fbf6aSSrinivas Pandruvada }
4661a4fbf6aSSrinivas Pandruvada
4671a4fbf6aSSrinivas Pandruvada return 0;
4681a4fbf6aSSrinivas Pandruvada }
4691a4fbf6aSSrinivas Pandruvada
kxcjk1013_get_mode(struct kxcjk1013_data * data,enum kxcjk1013_mode * mode)470124e1b1dSSrinivas Pandruvada static int kxcjk1013_get_mode(struct kxcjk1013_data *data,
471124e1b1dSSrinivas Pandruvada enum kxcjk1013_mode *mode)
472124e1b1dSSrinivas Pandruvada {
473124e1b1dSSrinivas Pandruvada int ret;
474124e1b1dSSrinivas Pandruvada
475ae4467f6SStephan Gerhold ret = i2c_smbus_read_byte_data(data->client, data->regs->ctrl1);
476124e1b1dSSrinivas Pandruvada if (ret < 0) {
477124e1b1dSSrinivas Pandruvada dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
478124e1b1dSSrinivas Pandruvada return ret;
479124e1b1dSSrinivas Pandruvada }
480124e1b1dSSrinivas Pandruvada
481124e1b1dSSrinivas Pandruvada if (ret & KXCJK1013_REG_CTRL1_BIT_PC1)
482124e1b1dSSrinivas Pandruvada *mode = OPERATION;
483124e1b1dSSrinivas Pandruvada else
484124e1b1dSSrinivas Pandruvada *mode = STANDBY;
485124e1b1dSSrinivas Pandruvada
486124e1b1dSSrinivas Pandruvada return 0;
487124e1b1dSSrinivas Pandruvada }
488124e1b1dSSrinivas Pandruvada
kxcjk1013_set_range(struct kxcjk1013_data * data,int range_index)489a735e3d7SSrinivas Pandruvada static int kxcjk1013_set_range(struct kxcjk1013_data *data, int range_index)
490a735e3d7SSrinivas Pandruvada {
491a735e3d7SSrinivas Pandruvada int ret;
492a735e3d7SSrinivas Pandruvada
493ae4467f6SStephan Gerhold ret = i2c_smbus_read_byte_data(data->client, data->regs->ctrl1);
494a735e3d7SSrinivas Pandruvada if (ret < 0) {
495a735e3d7SSrinivas Pandruvada dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
496a735e3d7SSrinivas Pandruvada return ret;
497a735e3d7SSrinivas Pandruvada }
498a735e3d7SSrinivas Pandruvada
499e90dea6aSDaniel Baluta ret &= ~(KXCJK1013_REG_CTRL1_BIT_GSEL0 |
500e90dea6aSDaniel Baluta KXCJK1013_REG_CTRL1_BIT_GSEL1);
501a735e3d7SSrinivas Pandruvada ret |= (KXCJK1013_scale_table[range_index].gsel_0 << 3);
502a735e3d7SSrinivas Pandruvada ret |= (KXCJK1013_scale_table[range_index].gsel_1 << 4);
503a735e3d7SSrinivas Pandruvada
504ae4467f6SStephan Gerhold ret = i2c_smbus_write_byte_data(data->client, data->regs->ctrl1, ret);
505a735e3d7SSrinivas Pandruvada if (ret < 0) {
506a735e3d7SSrinivas Pandruvada dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
507a735e3d7SSrinivas Pandruvada return ret;
508a735e3d7SSrinivas Pandruvada }
509a735e3d7SSrinivas Pandruvada
510a735e3d7SSrinivas Pandruvada data->range = range_index;
511a735e3d7SSrinivas Pandruvada
512a735e3d7SSrinivas Pandruvada return 0;
513a735e3d7SSrinivas Pandruvada }
514a735e3d7SSrinivas Pandruvada
kxcjk1013_chip_init(struct kxcjk1013_data * data)5151a4fbf6aSSrinivas Pandruvada static int kxcjk1013_chip_init(struct kxcjk1013_data *data)
5161a4fbf6aSSrinivas Pandruvada {
5171a4fbf6aSSrinivas Pandruvada int ret;
5181a4fbf6aSSrinivas Pandruvada
519e5b1032aSHans de Goede #ifdef CONFIG_ACPI
520e5b1032aSHans de Goede if (data->acpi_type == ACPI_KIOX010A) {
521e5b1032aSHans de Goede /* Make sure the kbd and touchpad on 2-in-1s using 2 KXCJ91008-s work */
522e5b1032aSHans de Goede kiox010a_dsm(&data->client->dev, KIOX010A_SET_LAPTOP_MODE);
523e5b1032aSHans de Goede }
524e5b1032aSHans de Goede #endif
525e5b1032aSHans de Goede
5261a4fbf6aSSrinivas Pandruvada ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_WHO_AM_I);
5271a4fbf6aSSrinivas Pandruvada if (ret < 0) {
5281a4fbf6aSSrinivas Pandruvada dev_err(&data->client->dev, "Error reading who_am_i\n");
5291a4fbf6aSSrinivas Pandruvada return ret;
5301a4fbf6aSSrinivas Pandruvada }
5311a4fbf6aSSrinivas Pandruvada
5321a4fbf6aSSrinivas Pandruvada dev_dbg(&data->client->dev, "KXCJK1013 Chip Id %x\n", ret);
5331a4fbf6aSSrinivas Pandruvada
5341a4fbf6aSSrinivas Pandruvada ret = kxcjk1013_set_mode(data, STANDBY);
5351a4fbf6aSSrinivas Pandruvada if (ret < 0)
5361a4fbf6aSSrinivas Pandruvada return ret;
5371a4fbf6aSSrinivas Pandruvada
538ae4467f6SStephan Gerhold ret = i2c_smbus_read_byte_data(data->client, data->regs->ctrl1);
5391a4fbf6aSSrinivas Pandruvada if (ret < 0) {
5401a4fbf6aSSrinivas Pandruvada dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
5411a4fbf6aSSrinivas Pandruvada return ret;
5421a4fbf6aSSrinivas Pandruvada }
5431a4fbf6aSSrinivas Pandruvada
5441a4fbf6aSSrinivas Pandruvada /* Set 12 bit mode */
5451a4fbf6aSSrinivas Pandruvada ret |= KXCJK1013_REG_CTRL1_BIT_RES;
5461a4fbf6aSSrinivas Pandruvada
547ae4467f6SStephan Gerhold ret = i2c_smbus_write_byte_data(data->client, data->regs->ctrl1, ret);
5481a4fbf6aSSrinivas Pandruvada if (ret < 0) {
5491a4fbf6aSSrinivas Pandruvada dev_err(&data->client->dev, "Error reading reg_ctrl\n");
5501a4fbf6aSSrinivas Pandruvada return ret;
5511a4fbf6aSSrinivas Pandruvada }
5521a4fbf6aSSrinivas Pandruvada
553a735e3d7SSrinivas Pandruvada /* Setting range to 4G */
554a735e3d7SSrinivas Pandruvada ret = kxcjk1013_set_range(data, KXCJK1013_RANGE_4G);
555a735e3d7SSrinivas Pandruvada if (ret < 0)
556a735e3d7SSrinivas Pandruvada return ret;
557a735e3d7SSrinivas Pandruvada
558ae4467f6SStephan Gerhold ret = i2c_smbus_read_byte_data(data->client, data->regs->data_ctrl);
5591a4fbf6aSSrinivas Pandruvada if (ret < 0) {
5601a4fbf6aSSrinivas Pandruvada dev_err(&data->client->dev, "Error reading reg_data_ctrl\n");
5611a4fbf6aSSrinivas Pandruvada return ret;
5621a4fbf6aSSrinivas Pandruvada }
5631a4fbf6aSSrinivas Pandruvada
5641a4fbf6aSSrinivas Pandruvada data->odr_bits = ret;
5651a4fbf6aSSrinivas Pandruvada
5661a4fbf6aSSrinivas Pandruvada /* Set up INT polarity */
567ae4467f6SStephan Gerhold ret = i2c_smbus_read_byte_data(data->client, data->regs->int_ctrl1);
5681a4fbf6aSSrinivas Pandruvada if (ret < 0) {
5691a4fbf6aSSrinivas Pandruvada dev_err(&data->client->dev, "Error reading reg_int_ctrl1\n");
5701a4fbf6aSSrinivas Pandruvada return ret;
5711a4fbf6aSSrinivas Pandruvada }
5721a4fbf6aSSrinivas Pandruvada
5731a4fbf6aSSrinivas Pandruvada if (data->active_high_intr)
574f74e440cSMichał Mirosław ret |= KXCJK1013_REG_INT_CTRL1_BIT_IEA;
5751a4fbf6aSSrinivas Pandruvada else
576f74e440cSMichał Mirosław ret &= ~KXCJK1013_REG_INT_CTRL1_BIT_IEA;
5771a4fbf6aSSrinivas Pandruvada
578ae4467f6SStephan Gerhold ret = i2c_smbus_write_byte_data(data->client, data->regs->int_ctrl1, ret);
5791a4fbf6aSSrinivas Pandruvada if (ret < 0) {
5801a4fbf6aSSrinivas Pandruvada dev_err(&data->client->dev, "Error writing reg_int_ctrl1\n");
5811a4fbf6aSSrinivas Pandruvada return ret;
5821a4fbf6aSSrinivas Pandruvada }
5831a4fbf6aSSrinivas Pandruvada
584*d5cbe150SRayyan Ansari /* On KX023 and KX022, route all used interrupts to INT1 for now */
585*d5cbe150SRayyan Ansari if ((data->chipset == KX0231025 || data->chipset == KX0221020) && data->client->irq > 0) {
586c630c176SStephan Gerhold ret = i2c_smbus_write_byte_data(data->client, KX023_REG_INC4,
587c630c176SStephan Gerhold KX023_REG_INC4_DRDY1 |
588c630c176SStephan Gerhold KX023_REG_INC4_WUFI1);
589c630c176SStephan Gerhold if (ret < 0) {
590c630c176SStephan Gerhold dev_err(&data->client->dev, "Error writing reg_inc4\n");
591c630c176SStephan Gerhold return ret;
592c630c176SStephan Gerhold }
593c630c176SStephan Gerhold }
594c630c176SStephan Gerhold
595124e1b1dSSrinivas Pandruvada ret = kxcjk1013_set_mode(data, OPERATION);
596124e1b1dSSrinivas Pandruvada if (ret < 0)
597124e1b1dSSrinivas Pandruvada return ret;
598124e1b1dSSrinivas Pandruvada
599b4b491c0SSrinivas Pandruvada data->wake_thres = KXCJK1013_DEFAULT_WAKE_THRES;
600b4b491c0SSrinivas Pandruvada
601124e1b1dSSrinivas Pandruvada return 0;
602124e1b1dSSrinivas Pandruvada }
603124e1b1dSSrinivas Pandruvada
6046f0a13f2SRafael J. Wysocki #ifdef CONFIG_PM
kxcjk1013_get_startup_times(struct kxcjk1013_data * data)605124e1b1dSSrinivas Pandruvada static int kxcjk1013_get_startup_times(struct kxcjk1013_data *data)
606124e1b1dSSrinivas Pandruvada {
607124e1b1dSSrinivas Pandruvada int i;
608c6861377SDaniel Baluta int idx = data->chipset;
609124e1b1dSSrinivas Pandruvada
610c6861377SDaniel Baluta for (i = 0; i < ARRAY_SIZE(odr_start_up_times[idx]); ++i) {
611c6861377SDaniel Baluta if (odr_start_up_times[idx][i].odr_bits == data->odr_bits)
612c6861377SDaniel Baluta return odr_start_up_times[idx][i].usec;
613124e1b1dSSrinivas Pandruvada }
614124e1b1dSSrinivas Pandruvada
615124e1b1dSSrinivas Pandruvada return KXCJK1013_MAX_STARTUP_TIME_US;
616124e1b1dSSrinivas Pandruvada }
617c9bf2373SDaniel Baluta #endif
618124e1b1dSSrinivas Pandruvada
kxcjk1013_set_power_state(struct kxcjk1013_data * data,bool on)619124e1b1dSSrinivas Pandruvada static int kxcjk1013_set_power_state(struct kxcjk1013_data *data, bool on)
620124e1b1dSSrinivas Pandruvada {
621c75b8dc8SIrina Tirdea #ifdef CONFIG_PM
622124e1b1dSSrinivas Pandruvada int ret;
623124e1b1dSSrinivas Pandruvada
624124e1b1dSSrinivas Pandruvada if (on)
62519611aecSJonathan Cameron ret = pm_runtime_resume_and_get(&data->client->dev);
626124e1b1dSSrinivas Pandruvada else {
627124e1b1dSSrinivas Pandruvada pm_runtime_mark_last_busy(&data->client->dev);
628124e1b1dSSrinivas Pandruvada ret = pm_runtime_put_autosuspend(&data->client->dev);
629124e1b1dSSrinivas Pandruvada }
630124e1b1dSSrinivas Pandruvada if (ret < 0) {
631124e1b1dSSrinivas Pandruvada dev_err(&data->client->dev,
632ce7afa5cSMohan Kumar "Failed: %s for %d\n", __func__, on);
633124e1b1dSSrinivas Pandruvada return ret;
634124e1b1dSSrinivas Pandruvada }
635c75b8dc8SIrina Tirdea #endif
636124e1b1dSSrinivas Pandruvada
6371a4fbf6aSSrinivas Pandruvada return 0;
6381a4fbf6aSSrinivas Pandruvada }
6391a4fbf6aSSrinivas Pandruvada
kxcjk1013_chip_update_thresholds(struct kxcjk1013_data * data)640b4b491c0SSrinivas Pandruvada static int kxcjk1013_chip_update_thresholds(struct kxcjk1013_data *data)
641b4b491c0SSrinivas Pandruvada {
642ae4467f6SStephan Gerhold int ret;
643b4b491c0SSrinivas Pandruvada
644ae4467f6SStephan Gerhold ret = i2c_smbus_write_byte_data(data->client, data->regs->wake_timer,
645b4b491c0SSrinivas Pandruvada data->wake_dur);
646b4b491c0SSrinivas Pandruvada if (ret < 0) {
647b4b491c0SSrinivas Pandruvada dev_err(&data->client->dev,
648b4b491c0SSrinivas Pandruvada "Error writing reg_wake_timer\n");
649b4b491c0SSrinivas Pandruvada return ret;
650b4b491c0SSrinivas Pandruvada }
651b4b491c0SSrinivas Pandruvada
652ae4467f6SStephan Gerhold ret = i2c_smbus_write_byte_data(data->client, data->regs->wake_thres,
653b4b491c0SSrinivas Pandruvada data->wake_thres);
654b4b491c0SSrinivas Pandruvada if (ret < 0) {
655b4b491c0SSrinivas Pandruvada dev_err(&data->client->dev, "Error writing reg_wake_thres\n");
656b4b491c0SSrinivas Pandruvada return ret;
657b4b491c0SSrinivas Pandruvada }
658b4b491c0SSrinivas Pandruvada
659b4b491c0SSrinivas Pandruvada return 0;
660b4b491c0SSrinivas Pandruvada }
661b4b491c0SSrinivas Pandruvada
kxcjk1013_setup_any_motion_interrupt(struct kxcjk1013_data * data,bool status)662b4b491c0SSrinivas Pandruvada static int kxcjk1013_setup_any_motion_interrupt(struct kxcjk1013_data *data,
663b4b491c0SSrinivas Pandruvada bool status)
664b4b491c0SSrinivas Pandruvada {
665b4b491c0SSrinivas Pandruvada int ret;
666b4b491c0SSrinivas Pandruvada enum kxcjk1013_mode store_mode;
667b4b491c0SSrinivas Pandruvada
668b4b491c0SSrinivas Pandruvada ret = kxcjk1013_get_mode(data, &store_mode);
669b4b491c0SSrinivas Pandruvada if (ret < 0)
670b4b491c0SSrinivas Pandruvada return ret;
671b4b491c0SSrinivas Pandruvada
672b4b491c0SSrinivas Pandruvada /* This is requirement by spec to change state to STANDBY */
673b4b491c0SSrinivas Pandruvada ret = kxcjk1013_set_mode(data, STANDBY);
674b4b491c0SSrinivas Pandruvada if (ret < 0)
675b4b491c0SSrinivas Pandruvada return ret;
676b4b491c0SSrinivas Pandruvada
677b4b491c0SSrinivas Pandruvada ret = kxcjk1013_chip_update_thresholds(data);
678b4b491c0SSrinivas Pandruvada if (ret < 0)
679b4b491c0SSrinivas Pandruvada return ret;
680b4b491c0SSrinivas Pandruvada
681ae4467f6SStephan Gerhold ret = i2c_smbus_read_byte_data(data->client, data->regs->int_ctrl1);
682b4b491c0SSrinivas Pandruvada if (ret < 0) {
683b4b491c0SSrinivas Pandruvada dev_err(&data->client->dev, "Error reading reg_int_ctrl1\n");
684b4b491c0SSrinivas Pandruvada return ret;
685b4b491c0SSrinivas Pandruvada }
686b4b491c0SSrinivas Pandruvada
687b4b491c0SSrinivas Pandruvada if (status)
688f74e440cSMichał Mirosław ret |= KXCJK1013_REG_INT_CTRL1_BIT_IEN;
689b4b491c0SSrinivas Pandruvada else
690f74e440cSMichał Mirosław ret &= ~KXCJK1013_REG_INT_CTRL1_BIT_IEN;
691b4b491c0SSrinivas Pandruvada
692ae4467f6SStephan Gerhold ret = i2c_smbus_write_byte_data(data->client, data->regs->int_ctrl1, ret);
693b4b491c0SSrinivas Pandruvada if (ret < 0) {
694b4b491c0SSrinivas Pandruvada dev_err(&data->client->dev, "Error writing reg_int_ctrl1\n");
695b4b491c0SSrinivas Pandruvada return ret;
696b4b491c0SSrinivas Pandruvada }
697b4b491c0SSrinivas Pandruvada
698ae4467f6SStephan Gerhold ret = i2c_smbus_read_byte_data(data->client, data->regs->ctrl1);
699b4b491c0SSrinivas Pandruvada if (ret < 0) {
700b4b491c0SSrinivas Pandruvada dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
701b4b491c0SSrinivas Pandruvada return ret;
702b4b491c0SSrinivas Pandruvada }
703b4b491c0SSrinivas Pandruvada
704b4b491c0SSrinivas Pandruvada if (status)
705b4b491c0SSrinivas Pandruvada ret |= KXCJK1013_REG_CTRL1_BIT_WUFE;
706b4b491c0SSrinivas Pandruvada else
707b4b491c0SSrinivas Pandruvada ret &= ~KXCJK1013_REG_CTRL1_BIT_WUFE;
708b4b491c0SSrinivas Pandruvada
709ae4467f6SStephan Gerhold ret = i2c_smbus_write_byte_data(data->client, data->regs->ctrl1, ret);
710b4b491c0SSrinivas Pandruvada if (ret < 0) {
711b4b491c0SSrinivas Pandruvada dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
712b4b491c0SSrinivas Pandruvada return ret;
713b4b491c0SSrinivas Pandruvada }
714b4b491c0SSrinivas Pandruvada
715b4b491c0SSrinivas Pandruvada if (store_mode == OPERATION) {
716b4b491c0SSrinivas Pandruvada ret = kxcjk1013_set_mode(data, OPERATION);
717b4b491c0SSrinivas Pandruvada if (ret < 0)
718b4b491c0SSrinivas Pandruvada return ret;
719b4b491c0SSrinivas Pandruvada }
720b4b491c0SSrinivas Pandruvada
721b4b491c0SSrinivas Pandruvada return 0;
722b4b491c0SSrinivas Pandruvada }
723b4b491c0SSrinivas Pandruvada
kxcjk1013_setup_new_data_interrupt(struct kxcjk1013_data * data,bool status)724b4b491c0SSrinivas Pandruvada static int kxcjk1013_setup_new_data_interrupt(struct kxcjk1013_data *data,
7251a4fbf6aSSrinivas Pandruvada bool status)
7261a4fbf6aSSrinivas Pandruvada {
7271a4fbf6aSSrinivas Pandruvada int ret;
728124e1b1dSSrinivas Pandruvada enum kxcjk1013_mode store_mode;
729124e1b1dSSrinivas Pandruvada
730124e1b1dSSrinivas Pandruvada ret = kxcjk1013_get_mode(data, &store_mode);
731124e1b1dSSrinivas Pandruvada if (ret < 0)
732124e1b1dSSrinivas Pandruvada return ret;
7331a4fbf6aSSrinivas Pandruvada
7341a4fbf6aSSrinivas Pandruvada /* This is requirement by spec to change state to STANDBY */
7351a4fbf6aSSrinivas Pandruvada ret = kxcjk1013_set_mode(data, STANDBY);
7361a4fbf6aSSrinivas Pandruvada if (ret < 0)
7371a4fbf6aSSrinivas Pandruvada return ret;
7381a4fbf6aSSrinivas Pandruvada
739ae4467f6SStephan Gerhold ret = i2c_smbus_read_byte_data(data->client, data->regs->int_ctrl1);
7401a4fbf6aSSrinivas Pandruvada if (ret < 0) {
7411a4fbf6aSSrinivas Pandruvada dev_err(&data->client->dev, "Error reading reg_int_ctrl1\n");
7421a4fbf6aSSrinivas Pandruvada return ret;
7431a4fbf6aSSrinivas Pandruvada }
7441a4fbf6aSSrinivas Pandruvada
7451a4fbf6aSSrinivas Pandruvada if (status)
746f74e440cSMichał Mirosław ret |= KXCJK1013_REG_INT_CTRL1_BIT_IEN;
7471a4fbf6aSSrinivas Pandruvada else
748f74e440cSMichał Mirosław ret &= ~KXCJK1013_REG_INT_CTRL1_BIT_IEN;
7491a4fbf6aSSrinivas Pandruvada
750ae4467f6SStephan Gerhold ret = i2c_smbus_write_byte_data(data->client, data->regs->int_ctrl1, ret);
7511a4fbf6aSSrinivas Pandruvada if (ret < 0) {
7521a4fbf6aSSrinivas Pandruvada dev_err(&data->client->dev, "Error writing reg_int_ctrl1\n");
7531a4fbf6aSSrinivas Pandruvada return ret;
7541a4fbf6aSSrinivas Pandruvada }
7551a4fbf6aSSrinivas Pandruvada
756ae4467f6SStephan Gerhold ret = i2c_smbus_read_byte_data(data->client, data->regs->ctrl1);
7571a4fbf6aSSrinivas Pandruvada if (ret < 0) {
7581a4fbf6aSSrinivas Pandruvada dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
7591a4fbf6aSSrinivas Pandruvada return ret;
7601a4fbf6aSSrinivas Pandruvada }
7611a4fbf6aSSrinivas Pandruvada
7621a4fbf6aSSrinivas Pandruvada if (status)
7631a4fbf6aSSrinivas Pandruvada ret |= KXCJK1013_REG_CTRL1_BIT_DRDY;
7641a4fbf6aSSrinivas Pandruvada else
7651a4fbf6aSSrinivas Pandruvada ret &= ~KXCJK1013_REG_CTRL1_BIT_DRDY;
7661a4fbf6aSSrinivas Pandruvada
767ae4467f6SStephan Gerhold ret = i2c_smbus_write_byte_data(data->client, data->regs->ctrl1, ret);
7681a4fbf6aSSrinivas Pandruvada if (ret < 0) {
7691a4fbf6aSSrinivas Pandruvada dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
7701a4fbf6aSSrinivas Pandruvada return ret;
7711a4fbf6aSSrinivas Pandruvada }
7721a4fbf6aSSrinivas Pandruvada
773124e1b1dSSrinivas Pandruvada if (store_mode == OPERATION) {
774124e1b1dSSrinivas Pandruvada ret = kxcjk1013_set_mode(data, OPERATION);
775124e1b1dSSrinivas Pandruvada if (ret < 0)
7761a4fbf6aSSrinivas Pandruvada return ret;
7771a4fbf6aSSrinivas Pandruvada }
7781a4fbf6aSSrinivas Pandruvada
779124e1b1dSSrinivas Pandruvada return 0;
780124e1b1dSSrinivas Pandruvada }
781124e1b1dSSrinivas Pandruvada
kxcjk1013_find_odr_value(const struct kx_odr_map * map,size_t map_size,int val,int val2)7827469ac69SMichał Mirosław static const struct kx_odr_map *kxcjk1013_find_odr_value(
7837469ac69SMichał Mirosław const struct kx_odr_map *map, size_t map_size, int val, int val2)
7841a4fbf6aSSrinivas Pandruvada {
7851a4fbf6aSSrinivas Pandruvada int i;
7861a4fbf6aSSrinivas Pandruvada
7877469ac69SMichał Mirosław for (i = 0; i < map_size; ++i) {
7887469ac69SMichał Mirosław if (map[i].val == val && map[i].val2 == val2)
7897469ac69SMichał Mirosław return &map[i];
7901a4fbf6aSSrinivas Pandruvada }
7911a4fbf6aSSrinivas Pandruvada
7927469ac69SMichał Mirosław return ERR_PTR(-EINVAL);
7931a4fbf6aSSrinivas Pandruvada }
7941a4fbf6aSSrinivas Pandruvada
kxcjk1013_convert_odr_value(const struct kx_odr_map * map,size_t map_size,int odr_bits,int * val,int * val2)7957469ac69SMichał Mirosław static int kxcjk1013_convert_odr_value(const struct kx_odr_map *map,
7967469ac69SMichał Mirosław size_t map_size, int odr_bits,
7977469ac69SMichał Mirosław int *val, int *val2)
798b4b491c0SSrinivas Pandruvada {
799b4b491c0SSrinivas Pandruvada int i;
800b4b491c0SSrinivas Pandruvada
8017469ac69SMichał Mirosław for (i = 0; i < map_size; ++i) {
8027469ac69SMichał Mirosław if (map[i].odr_bits == odr_bits) {
8037469ac69SMichał Mirosław *val = map[i].val;
8047469ac69SMichał Mirosław *val2 = map[i].val2;
8057469ac69SMichał Mirosław return IIO_VAL_INT_PLUS_MICRO;
806b4b491c0SSrinivas Pandruvada }
807b4b491c0SSrinivas Pandruvada }
808b4b491c0SSrinivas Pandruvada
809b4b491c0SSrinivas Pandruvada return -EINVAL;
810b4b491c0SSrinivas Pandruvada }
811b4b491c0SSrinivas Pandruvada
kxcjk1013_set_odr(struct kxcjk1013_data * data,int val,int val2)8121a4fbf6aSSrinivas Pandruvada static int kxcjk1013_set_odr(struct kxcjk1013_data *data, int val, int val2)
8131a4fbf6aSSrinivas Pandruvada {
8141a4fbf6aSSrinivas Pandruvada int ret;
815124e1b1dSSrinivas Pandruvada enum kxcjk1013_mode store_mode;
8167469ac69SMichał Mirosław const struct kx_odr_map *odr_setting;
817124e1b1dSSrinivas Pandruvada
818124e1b1dSSrinivas Pandruvada ret = kxcjk1013_get_mode(data, &store_mode);
819124e1b1dSSrinivas Pandruvada if (ret < 0)
820124e1b1dSSrinivas Pandruvada return ret;
8211a4fbf6aSSrinivas Pandruvada
8221540d010SMichał Mirosław if (data->chipset == KXTF9)
8231540d010SMichał Mirosław odr_setting = kxcjk1013_find_odr_value(kxtf9_samp_freq_table,
8241540d010SMichał Mirosław ARRAY_SIZE(kxtf9_samp_freq_table),
8251540d010SMichał Mirosław val, val2);
8261540d010SMichał Mirosław else
8277469ac69SMichał Mirosław odr_setting = kxcjk1013_find_odr_value(samp_freq_table,
8287469ac69SMichał Mirosław ARRAY_SIZE(samp_freq_table),
8297469ac69SMichał Mirosław val, val2);
8307469ac69SMichał Mirosław
8317469ac69SMichał Mirosław if (IS_ERR(odr_setting))
8327469ac69SMichał Mirosław return PTR_ERR(odr_setting);
8331a4fbf6aSSrinivas Pandruvada
8341a4fbf6aSSrinivas Pandruvada /* To change ODR, the chip must be set to STANDBY as per spec */
8351a4fbf6aSSrinivas Pandruvada ret = kxcjk1013_set_mode(data, STANDBY);
8361a4fbf6aSSrinivas Pandruvada if (ret < 0)
8371a4fbf6aSSrinivas Pandruvada return ret;
8381a4fbf6aSSrinivas Pandruvada
839ae4467f6SStephan Gerhold ret = i2c_smbus_write_byte_data(data->client, data->regs->data_ctrl,
8407469ac69SMichał Mirosław odr_setting->odr_bits);
8411a4fbf6aSSrinivas Pandruvada if (ret < 0) {
8421a4fbf6aSSrinivas Pandruvada dev_err(&data->client->dev, "Error writing data_ctrl\n");
8431a4fbf6aSSrinivas Pandruvada return ret;
8441a4fbf6aSSrinivas Pandruvada }
8451a4fbf6aSSrinivas Pandruvada
8467469ac69SMichał Mirosław data->odr_bits = odr_setting->odr_bits;
847b4b491c0SSrinivas Pandruvada
848ae4467f6SStephan Gerhold ret = i2c_smbus_write_byte_data(data->client, data->regs->wuf_ctrl,
8497469ac69SMichał Mirosław odr_setting->wuf_bits);
850b4b491c0SSrinivas Pandruvada if (ret < 0) {
851b4b491c0SSrinivas Pandruvada dev_err(&data->client->dev, "Error writing reg_ctrl2\n");
852b4b491c0SSrinivas Pandruvada return ret;
853b4b491c0SSrinivas Pandruvada }
854b4b491c0SSrinivas Pandruvada
855124e1b1dSSrinivas Pandruvada if (store_mode == OPERATION) {
8561a4fbf6aSSrinivas Pandruvada ret = kxcjk1013_set_mode(data, OPERATION);
8571a4fbf6aSSrinivas Pandruvada if (ret < 0)
8581a4fbf6aSSrinivas Pandruvada return ret;
8591a4fbf6aSSrinivas Pandruvada }
8601a4fbf6aSSrinivas Pandruvada
8611a4fbf6aSSrinivas Pandruvada return 0;
8621a4fbf6aSSrinivas Pandruvada }
8631a4fbf6aSSrinivas Pandruvada
kxcjk1013_get_odr(struct kxcjk1013_data * data,int * val,int * val2)8641a4fbf6aSSrinivas Pandruvada static int kxcjk1013_get_odr(struct kxcjk1013_data *data, int *val, int *val2)
8651a4fbf6aSSrinivas Pandruvada {
8661540d010SMichał Mirosław if (data->chipset == KXTF9)
8671540d010SMichał Mirosław return kxcjk1013_convert_odr_value(kxtf9_samp_freq_table,
8681540d010SMichał Mirosław ARRAY_SIZE(kxtf9_samp_freq_table),
8691540d010SMichał Mirosław data->odr_bits, val, val2);
8701540d010SMichał Mirosław else
8717469ac69SMichał Mirosław return kxcjk1013_convert_odr_value(samp_freq_table,
8727469ac69SMichał Mirosław ARRAY_SIZE(samp_freq_table),
8737469ac69SMichał Mirosław data->odr_bits, val, val2);
8741a4fbf6aSSrinivas Pandruvada }
8751a4fbf6aSSrinivas Pandruvada
kxcjk1013_get_acc_reg(struct kxcjk1013_data * data,int axis)8761a4fbf6aSSrinivas Pandruvada static int kxcjk1013_get_acc_reg(struct kxcjk1013_data *data, int axis)
8771a4fbf6aSSrinivas Pandruvada {
8781a4fbf6aSSrinivas Pandruvada u8 reg = KXCJK1013_REG_XOUT_L + axis * 2;
8791a4fbf6aSSrinivas Pandruvada int ret;
8801a4fbf6aSSrinivas Pandruvada
8811a4fbf6aSSrinivas Pandruvada ret = i2c_smbus_read_word_data(data->client, reg);
8821a4fbf6aSSrinivas Pandruvada if (ret < 0) {
8831a4fbf6aSSrinivas Pandruvada dev_err(&data->client->dev,
8841a4fbf6aSSrinivas Pandruvada "failed to read accel_%c registers\n", 'x' + axis);
8851a4fbf6aSSrinivas Pandruvada return ret;
8861a4fbf6aSSrinivas Pandruvada }
8871a4fbf6aSSrinivas Pandruvada
8881a4fbf6aSSrinivas Pandruvada return ret;
8891a4fbf6aSSrinivas Pandruvada }
8901a4fbf6aSSrinivas Pandruvada
kxcjk1013_set_scale(struct kxcjk1013_data * data,int val)891a735e3d7SSrinivas Pandruvada static int kxcjk1013_set_scale(struct kxcjk1013_data *data, int val)
892a735e3d7SSrinivas Pandruvada {
893a735e3d7SSrinivas Pandruvada int ret, i;
894a735e3d7SSrinivas Pandruvada enum kxcjk1013_mode store_mode;
895a735e3d7SSrinivas Pandruvada
896a735e3d7SSrinivas Pandruvada for (i = 0; i < ARRAY_SIZE(KXCJK1013_scale_table); ++i) {
897a735e3d7SSrinivas Pandruvada if (KXCJK1013_scale_table[i].scale == val) {
898a735e3d7SSrinivas Pandruvada ret = kxcjk1013_get_mode(data, &store_mode);
899a735e3d7SSrinivas Pandruvada if (ret < 0)
900a735e3d7SSrinivas Pandruvada return ret;
901a735e3d7SSrinivas Pandruvada
902a735e3d7SSrinivas Pandruvada ret = kxcjk1013_set_mode(data, STANDBY);
903a735e3d7SSrinivas Pandruvada if (ret < 0)
904a735e3d7SSrinivas Pandruvada return ret;
905a735e3d7SSrinivas Pandruvada
906a735e3d7SSrinivas Pandruvada ret = kxcjk1013_set_range(data, i);
907a735e3d7SSrinivas Pandruvada if (ret < 0)
908a735e3d7SSrinivas Pandruvada return ret;
909a735e3d7SSrinivas Pandruvada
910a735e3d7SSrinivas Pandruvada if (store_mode == OPERATION) {
911a735e3d7SSrinivas Pandruvada ret = kxcjk1013_set_mode(data, OPERATION);
912a735e3d7SSrinivas Pandruvada if (ret)
913a735e3d7SSrinivas Pandruvada return ret;
914a735e3d7SSrinivas Pandruvada }
915a735e3d7SSrinivas Pandruvada
916a735e3d7SSrinivas Pandruvada return 0;
917a735e3d7SSrinivas Pandruvada }
918a735e3d7SSrinivas Pandruvada }
919a735e3d7SSrinivas Pandruvada
920a735e3d7SSrinivas Pandruvada return -EINVAL;
921a735e3d7SSrinivas Pandruvada }
922a735e3d7SSrinivas Pandruvada
kxcjk1013_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)9231a4fbf6aSSrinivas Pandruvada static int kxcjk1013_read_raw(struct iio_dev *indio_dev,
9241a4fbf6aSSrinivas Pandruvada struct iio_chan_spec const *chan, int *val,
9251a4fbf6aSSrinivas Pandruvada int *val2, long mask)
9261a4fbf6aSSrinivas Pandruvada {
9271a4fbf6aSSrinivas Pandruvada struct kxcjk1013_data *data = iio_priv(indio_dev);
9281a4fbf6aSSrinivas Pandruvada int ret;
9291a4fbf6aSSrinivas Pandruvada
9301a4fbf6aSSrinivas Pandruvada switch (mask) {
9311a4fbf6aSSrinivas Pandruvada case IIO_CHAN_INFO_RAW:
9321a4fbf6aSSrinivas Pandruvada mutex_lock(&data->mutex);
9331a4fbf6aSSrinivas Pandruvada if (iio_buffer_enabled(indio_dev))
9341a4fbf6aSSrinivas Pandruvada ret = -EBUSY;
9351a4fbf6aSSrinivas Pandruvada else {
936124e1b1dSSrinivas Pandruvada ret = kxcjk1013_set_power_state(data, true);
93788f6da77SSrinivas Pandruvada if (ret < 0) {
93888f6da77SSrinivas Pandruvada mutex_unlock(&data->mutex);
9391a4fbf6aSSrinivas Pandruvada return ret;
94088f6da77SSrinivas Pandruvada }
9411a4fbf6aSSrinivas Pandruvada ret = kxcjk1013_get_acc_reg(data, chan->scan_index);
942124e1b1dSSrinivas Pandruvada if (ret < 0) {
943124e1b1dSSrinivas Pandruvada kxcjk1013_set_power_state(data, false);
944124e1b1dSSrinivas Pandruvada mutex_unlock(&data->mutex);
945124e1b1dSSrinivas Pandruvada return ret;
946124e1b1dSSrinivas Pandruvada }
9479105079dSGwendal Grignou *val = sign_extend32(ret >> chan->scan_type.shift,
9489105079dSGwendal Grignou chan->scan_type.realbits - 1);
949124e1b1dSSrinivas Pandruvada ret = kxcjk1013_set_power_state(data, false);
9501a4fbf6aSSrinivas Pandruvada }
9511a4fbf6aSSrinivas Pandruvada mutex_unlock(&data->mutex);
9521a4fbf6aSSrinivas Pandruvada
9531a4fbf6aSSrinivas Pandruvada if (ret < 0)
9541a4fbf6aSSrinivas Pandruvada return ret;
9551a4fbf6aSSrinivas Pandruvada
9561a4fbf6aSSrinivas Pandruvada return IIO_VAL_INT;
9571a4fbf6aSSrinivas Pandruvada
9581a4fbf6aSSrinivas Pandruvada case IIO_CHAN_INFO_SCALE:
9591a4fbf6aSSrinivas Pandruvada *val = 0;
960a735e3d7SSrinivas Pandruvada *val2 = KXCJK1013_scale_table[data->range].scale;
9611a4fbf6aSSrinivas Pandruvada return IIO_VAL_INT_PLUS_MICRO;
9621a4fbf6aSSrinivas Pandruvada
9631a4fbf6aSSrinivas Pandruvada case IIO_CHAN_INFO_SAMP_FREQ:
9641a4fbf6aSSrinivas Pandruvada mutex_lock(&data->mutex);
9651a4fbf6aSSrinivas Pandruvada ret = kxcjk1013_get_odr(data, val, val2);
9661a4fbf6aSSrinivas Pandruvada mutex_unlock(&data->mutex);
9671a4fbf6aSSrinivas Pandruvada return ret;
9681a4fbf6aSSrinivas Pandruvada
9691a4fbf6aSSrinivas Pandruvada default:
9701a4fbf6aSSrinivas Pandruvada return -EINVAL;
9711a4fbf6aSSrinivas Pandruvada }
9721a4fbf6aSSrinivas Pandruvada }
9731a4fbf6aSSrinivas Pandruvada
kxcjk1013_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)9741a4fbf6aSSrinivas Pandruvada static int kxcjk1013_write_raw(struct iio_dev *indio_dev,
9751a4fbf6aSSrinivas Pandruvada struct iio_chan_spec const *chan, int val,
9761a4fbf6aSSrinivas Pandruvada int val2, long mask)
9771a4fbf6aSSrinivas Pandruvada {
9781a4fbf6aSSrinivas Pandruvada struct kxcjk1013_data *data = iio_priv(indio_dev);
9791a4fbf6aSSrinivas Pandruvada int ret;
9801a4fbf6aSSrinivas Pandruvada
9811a4fbf6aSSrinivas Pandruvada switch (mask) {
9821a4fbf6aSSrinivas Pandruvada case IIO_CHAN_INFO_SAMP_FREQ:
9831a4fbf6aSSrinivas Pandruvada mutex_lock(&data->mutex);
9841a4fbf6aSSrinivas Pandruvada ret = kxcjk1013_set_odr(data, val, val2);
9851a4fbf6aSSrinivas Pandruvada mutex_unlock(&data->mutex);
9861a4fbf6aSSrinivas Pandruvada break;
987a735e3d7SSrinivas Pandruvada case IIO_CHAN_INFO_SCALE:
988a735e3d7SSrinivas Pandruvada if (val)
989a735e3d7SSrinivas Pandruvada return -EINVAL;
990a735e3d7SSrinivas Pandruvada
991a735e3d7SSrinivas Pandruvada mutex_lock(&data->mutex);
992a735e3d7SSrinivas Pandruvada ret = kxcjk1013_set_scale(data, val2);
993a735e3d7SSrinivas Pandruvada mutex_unlock(&data->mutex);
994a735e3d7SSrinivas Pandruvada break;
9951a4fbf6aSSrinivas Pandruvada default:
9961a4fbf6aSSrinivas Pandruvada ret = -EINVAL;
9971a4fbf6aSSrinivas Pandruvada }
9981a4fbf6aSSrinivas Pandruvada
9991a4fbf6aSSrinivas Pandruvada return ret;
10001a4fbf6aSSrinivas Pandruvada }
10011a4fbf6aSSrinivas Pandruvada
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)1002b4b491c0SSrinivas Pandruvada static int kxcjk1013_read_event(struct iio_dev *indio_dev,
1003b4b491c0SSrinivas Pandruvada const struct iio_chan_spec *chan,
1004b4b491c0SSrinivas Pandruvada enum iio_event_type type,
1005b4b491c0SSrinivas Pandruvada enum iio_event_direction dir,
1006b4b491c0SSrinivas Pandruvada enum iio_event_info info,
1007b4b491c0SSrinivas Pandruvada int *val, int *val2)
1008b4b491c0SSrinivas Pandruvada {
1009b4b491c0SSrinivas Pandruvada struct kxcjk1013_data *data = iio_priv(indio_dev);
1010b4b491c0SSrinivas Pandruvada
1011b4b491c0SSrinivas Pandruvada *val2 = 0;
1012b4b491c0SSrinivas Pandruvada switch (info) {
1013b4b491c0SSrinivas Pandruvada case IIO_EV_INFO_VALUE:
1014b4b491c0SSrinivas Pandruvada *val = data->wake_thres;
1015b4b491c0SSrinivas Pandruvada break;
1016b4b491c0SSrinivas Pandruvada case IIO_EV_INFO_PERIOD:
1017b4b491c0SSrinivas Pandruvada *val = data->wake_dur;
1018b4b491c0SSrinivas Pandruvada break;
1019b4b491c0SSrinivas Pandruvada default:
1020b4b491c0SSrinivas Pandruvada return -EINVAL;
1021b4b491c0SSrinivas Pandruvada }
1022b4b491c0SSrinivas Pandruvada
1023b4b491c0SSrinivas Pandruvada return IIO_VAL_INT;
1024b4b491c0SSrinivas Pandruvada }
1025b4b491c0SSrinivas Pandruvada
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)1026b4b491c0SSrinivas Pandruvada static int kxcjk1013_write_event(struct iio_dev *indio_dev,
1027b4b491c0SSrinivas Pandruvada const struct iio_chan_spec *chan,
1028b4b491c0SSrinivas Pandruvada enum iio_event_type type,
1029b4b491c0SSrinivas Pandruvada enum iio_event_direction dir,
1030b4b491c0SSrinivas Pandruvada enum iio_event_info info,
1031b4b491c0SSrinivas Pandruvada int val, int val2)
1032b4b491c0SSrinivas Pandruvada {
1033b4b491c0SSrinivas Pandruvada struct kxcjk1013_data *data = iio_priv(indio_dev);
1034b4b491c0SSrinivas Pandruvada
1035b4b491c0SSrinivas Pandruvada if (data->ev_enable_state)
1036b4b491c0SSrinivas Pandruvada return -EBUSY;
1037b4b491c0SSrinivas Pandruvada
1038b4b491c0SSrinivas Pandruvada switch (info) {
1039b4b491c0SSrinivas Pandruvada case IIO_EV_INFO_VALUE:
1040b4b491c0SSrinivas Pandruvada data->wake_thres = val;
1041b4b491c0SSrinivas Pandruvada break;
1042b4b491c0SSrinivas Pandruvada case IIO_EV_INFO_PERIOD:
1043b4b491c0SSrinivas Pandruvada data->wake_dur = val;
1044b4b491c0SSrinivas Pandruvada break;
1045b4b491c0SSrinivas Pandruvada default:
1046b4b491c0SSrinivas Pandruvada return -EINVAL;
1047b4b491c0SSrinivas Pandruvada }
1048b4b491c0SSrinivas Pandruvada
1049b4b491c0SSrinivas Pandruvada return 0;
1050b4b491c0SSrinivas Pandruvada }
1051b4b491c0SSrinivas Pandruvada
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)1052b4b491c0SSrinivas Pandruvada static int kxcjk1013_read_event_config(struct iio_dev *indio_dev,
1053b4b491c0SSrinivas Pandruvada const struct iio_chan_spec *chan,
1054b4b491c0SSrinivas Pandruvada enum iio_event_type type,
1055b4b491c0SSrinivas Pandruvada enum iio_event_direction dir)
1056b4b491c0SSrinivas Pandruvada {
1057b4b491c0SSrinivas Pandruvada struct kxcjk1013_data *data = iio_priv(indio_dev);
1058b4b491c0SSrinivas Pandruvada
1059b4b491c0SSrinivas Pandruvada return data->ev_enable_state;
1060b4b491c0SSrinivas Pandruvada }
1061b4b491c0SSrinivas Pandruvada
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)1062b4b491c0SSrinivas Pandruvada static int kxcjk1013_write_event_config(struct iio_dev *indio_dev,
1063b4b491c0SSrinivas Pandruvada const struct iio_chan_spec *chan,
1064b4b491c0SSrinivas Pandruvada enum iio_event_type type,
1065b4b491c0SSrinivas Pandruvada enum iio_event_direction dir,
1066b4b491c0SSrinivas Pandruvada int state)
1067b4b491c0SSrinivas Pandruvada {
1068b4b491c0SSrinivas Pandruvada struct kxcjk1013_data *data = iio_priv(indio_dev);
1069b4b491c0SSrinivas Pandruvada int ret;
1070b4b491c0SSrinivas Pandruvada
1071b4b491c0SSrinivas Pandruvada if (state && data->ev_enable_state)
1072b4b491c0SSrinivas Pandruvada return 0;
1073b4b491c0SSrinivas Pandruvada
1074b4b491c0SSrinivas Pandruvada mutex_lock(&data->mutex);
1075b4b491c0SSrinivas Pandruvada
1076b4b491c0SSrinivas Pandruvada if (!state && data->motion_trigger_on) {
1077b4b491c0SSrinivas Pandruvada data->ev_enable_state = 0;
1078b4b491c0SSrinivas Pandruvada mutex_unlock(&data->mutex);
1079b4b491c0SSrinivas Pandruvada return 0;
1080b4b491c0SSrinivas Pandruvada }
1081b4b491c0SSrinivas Pandruvada
1082b4b491c0SSrinivas Pandruvada /*
1083b4b491c0SSrinivas Pandruvada * We will expect the enable and disable to do operation in
10841cf507cfSwangjianli * reverse order. This will happen here anyway as our
1085b4b491c0SSrinivas Pandruvada * resume operation uses sync mode runtime pm calls, the
1086b4b491c0SSrinivas Pandruvada * suspend operation will be delayed by autosuspend delay
1087b4b491c0SSrinivas Pandruvada * So the disable operation will still happen in reverse of
1088b4b491c0SSrinivas Pandruvada * enable operation. When runtime pm is disabled the mode
1089b4b491c0SSrinivas Pandruvada * is always on so sequence doesn't matter
1090b4b491c0SSrinivas Pandruvada */
1091b4b491c0SSrinivas Pandruvada ret = kxcjk1013_set_power_state(data, state);
1092b4b491c0SSrinivas Pandruvada if (ret < 0) {
1093b4b491c0SSrinivas Pandruvada mutex_unlock(&data->mutex);
1094b4b491c0SSrinivas Pandruvada return ret;
1095b4b491c0SSrinivas Pandruvada }
1096b4b491c0SSrinivas Pandruvada
1097b4b491c0SSrinivas Pandruvada ret = kxcjk1013_setup_any_motion_interrupt(data, state);
1098b4b491c0SSrinivas Pandruvada if (ret < 0) {
1099fbd123e9SIrina Tirdea kxcjk1013_set_power_state(data, false);
1100fbd123e9SIrina Tirdea data->ev_enable_state = 0;
1101b4b491c0SSrinivas Pandruvada mutex_unlock(&data->mutex);
1102b4b491c0SSrinivas Pandruvada return ret;
1103b4b491c0SSrinivas Pandruvada }
1104b4b491c0SSrinivas Pandruvada
1105b4b491c0SSrinivas Pandruvada data->ev_enable_state = state;
1106b4b491c0SSrinivas Pandruvada mutex_unlock(&data->mutex);
1107b4b491c0SSrinivas Pandruvada
1108b4b491c0SSrinivas Pandruvada return 0;
1109b4b491c0SSrinivas Pandruvada }
1110b4b491c0SSrinivas Pandruvada
kxcjk1013_buffer_preenable(struct iio_dev * indio_dev)1111a25691c1SVlad Dogaru static int kxcjk1013_buffer_preenable(struct iio_dev *indio_dev)
11121a4fbf6aSSrinivas Pandruvada {
11131a4fbf6aSSrinivas Pandruvada struct kxcjk1013_data *data = iio_priv(indio_dev);
11141a4fbf6aSSrinivas Pandruvada
1115a25691c1SVlad Dogaru return kxcjk1013_set_power_state(data, true);
1116a25691c1SVlad Dogaru }
11171a4fbf6aSSrinivas Pandruvada
kxcjk1013_buffer_postdisable(struct iio_dev * indio_dev)1118a25691c1SVlad Dogaru static int kxcjk1013_buffer_postdisable(struct iio_dev *indio_dev)
1119a25691c1SVlad Dogaru {
1120a25691c1SVlad Dogaru struct kxcjk1013_data *data = iio_priv(indio_dev);
1121a25691c1SVlad Dogaru
1122a25691c1SVlad Dogaru return kxcjk1013_set_power_state(data, false);
11231a4fbf6aSSrinivas Pandruvada }
11241a4fbf6aSSrinivas Pandruvada
kxcjk1013_get_samp_freq_avail(struct device * dev,struct device_attribute * attr,char * buf)112581fa8cb9SMichał Mirosław static ssize_t kxcjk1013_get_samp_freq_avail(struct device *dev,
112681fa8cb9SMichał Mirosław struct device_attribute *attr,
112781fa8cb9SMichał Mirosław char *buf)
112881fa8cb9SMichał Mirosław {
11291540d010SMichał Mirosław struct iio_dev *indio_dev = dev_to_iio_dev(dev);
11301540d010SMichał Mirosław struct kxcjk1013_data *data = iio_priv(indio_dev);
11311540d010SMichał Mirosław const char *str;
11321540d010SMichał Mirosław
11331540d010SMichał Mirosław if (data->chipset == KXTF9)
11341540d010SMichał Mirosław str = kxtf9_samp_freq_avail;
11351540d010SMichał Mirosław else
11361540d010SMichał Mirosław str = kxcjk1013_samp_freq_avail;
11371540d010SMichał Mirosław
11381540d010SMichał Mirosław return sprintf(buf, "%s\n", str);
113981fa8cb9SMichał Mirosław }
114081fa8cb9SMichał Mirosław
1141d2ff1956SMichał Mirosław static IIO_DEVICE_ATTR(in_accel_sampling_frequency_available, S_IRUGO,
1142d2ff1956SMichał Mirosław kxcjk1013_get_samp_freq_avail, NULL, 0);
11431a4fbf6aSSrinivas Pandruvada
1144a735e3d7SSrinivas Pandruvada static IIO_CONST_ATTR(in_accel_scale_available, "0.009582 0.019163 0.038326");
1145a735e3d7SSrinivas Pandruvada
11461a4fbf6aSSrinivas Pandruvada static struct attribute *kxcjk1013_attributes[] = {
1147d2ff1956SMichał Mirosław &iio_dev_attr_in_accel_sampling_frequency_available.dev_attr.attr,
1148a735e3d7SSrinivas Pandruvada &iio_const_attr_in_accel_scale_available.dev_attr.attr,
11491a4fbf6aSSrinivas Pandruvada NULL,
11501a4fbf6aSSrinivas Pandruvada };
11511a4fbf6aSSrinivas Pandruvada
11521a4fbf6aSSrinivas Pandruvada static const struct attribute_group kxcjk1013_attrs_group = {
11531a4fbf6aSSrinivas Pandruvada .attrs = kxcjk1013_attributes,
11541a4fbf6aSSrinivas Pandruvada };
11551a4fbf6aSSrinivas Pandruvada
1156b4b491c0SSrinivas Pandruvada static const struct iio_event_spec kxcjk1013_event = {
1157b4b491c0SSrinivas Pandruvada .type = IIO_EV_TYPE_THRESH,
115825afffe1SDaniel Baluta .dir = IIO_EV_DIR_EITHER,
1159b4b491c0SSrinivas Pandruvada .mask_separate = BIT(IIO_EV_INFO_VALUE) |
1160b4b491c0SSrinivas Pandruvada BIT(IIO_EV_INFO_ENABLE) |
1161b4b491c0SSrinivas Pandruvada BIT(IIO_EV_INFO_PERIOD)
1162b4b491c0SSrinivas Pandruvada };
1163b4b491c0SSrinivas Pandruvada
11641bde330cSDmitry Osipenko static const struct iio_mount_matrix *
kxcjk1013_get_mount_matrix(const struct iio_dev * indio_dev,const struct iio_chan_spec * chan)11651bde330cSDmitry Osipenko kxcjk1013_get_mount_matrix(const struct iio_dev *indio_dev,
11661bde330cSDmitry Osipenko const struct iio_chan_spec *chan)
11671bde330cSDmitry Osipenko {
11681bde330cSDmitry Osipenko struct kxcjk1013_data *data = iio_priv(indio_dev);
11691bde330cSDmitry Osipenko
11701bde330cSDmitry Osipenko return &data->orientation;
11711bde330cSDmitry Osipenko }
11721bde330cSDmitry Osipenko
11731bde330cSDmitry Osipenko static const struct iio_chan_spec_ext_info kxcjk1013_ext_info[] = {
11741bde330cSDmitry Osipenko IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE, kxcjk1013_get_mount_matrix),
11751bde330cSDmitry Osipenko { }
11761bde330cSDmitry Osipenko };
11771bde330cSDmitry Osipenko
11781a4fbf6aSSrinivas Pandruvada #define KXCJK1013_CHANNEL(_axis) { \
11791a4fbf6aSSrinivas Pandruvada .type = IIO_ACCEL, \
11801a4fbf6aSSrinivas Pandruvada .modified = 1, \
11811a4fbf6aSSrinivas Pandruvada .channel2 = IIO_MOD_##_axis, \
11821a4fbf6aSSrinivas Pandruvada .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
11831a4fbf6aSSrinivas Pandruvada .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
11841a4fbf6aSSrinivas Pandruvada BIT(IIO_CHAN_INFO_SAMP_FREQ), \
11851a4fbf6aSSrinivas Pandruvada .scan_index = AXIS_##_axis, \
11861a4fbf6aSSrinivas Pandruvada .scan_type = { \
11871a4fbf6aSSrinivas Pandruvada .sign = 's', \
11881a4fbf6aSSrinivas Pandruvada .realbits = 12, \
11891a4fbf6aSSrinivas Pandruvada .storagebits = 16, \
11901a4fbf6aSSrinivas Pandruvada .shift = 4, \
119165ae47b0SAdriana Reus .endianness = IIO_LE, \
11921a4fbf6aSSrinivas Pandruvada }, \
1193b4b491c0SSrinivas Pandruvada .event_spec = &kxcjk1013_event, \
11941bde330cSDmitry Osipenko .ext_info = kxcjk1013_ext_info, \
1195b4b491c0SSrinivas Pandruvada .num_event_specs = 1 \
11961a4fbf6aSSrinivas Pandruvada }
11971a4fbf6aSSrinivas Pandruvada
11981a4fbf6aSSrinivas Pandruvada static const struct iio_chan_spec kxcjk1013_channels[] = {
11991a4fbf6aSSrinivas Pandruvada KXCJK1013_CHANNEL(X),
12001a4fbf6aSSrinivas Pandruvada KXCJK1013_CHANNEL(Y),
12011a4fbf6aSSrinivas Pandruvada KXCJK1013_CHANNEL(Z),
12021a4fbf6aSSrinivas Pandruvada IIO_CHAN_SOFT_TIMESTAMP(3),
12031a4fbf6aSSrinivas Pandruvada };
12041a4fbf6aSSrinivas Pandruvada
1205a25691c1SVlad Dogaru static const struct iio_buffer_setup_ops kxcjk1013_buffer_setup_ops = {
1206a25691c1SVlad Dogaru .preenable = kxcjk1013_buffer_preenable,
1207a25691c1SVlad Dogaru .postdisable = kxcjk1013_buffer_postdisable,
1208a25691c1SVlad Dogaru };
1209a25691c1SVlad Dogaru
12101a4fbf6aSSrinivas Pandruvada static const struct iio_info kxcjk1013_info = {
12111a4fbf6aSSrinivas Pandruvada .attrs = &kxcjk1013_attrs_group,
12121a4fbf6aSSrinivas Pandruvada .read_raw = kxcjk1013_read_raw,
12131a4fbf6aSSrinivas Pandruvada .write_raw = kxcjk1013_write_raw,
1214b4b491c0SSrinivas Pandruvada .read_event_value = kxcjk1013_read_event,
1215b4b491c0SSrinivas Pandruvada .write_event_value = kxcjk1013_write_event,
1216b4b491c0SSrinivas Pandruvada .write_event_config = kxcjk1013_write_event_config,
1217b4b491c0SSrinivas Pandruvada .read_event_config = kxcjk1013_read_event_config,
12181a4fbf6aSSrinivas Pandruvada };
12191a4fbf6aSSrinivas Pandruvada
122009cf1b32SAdriana Reus static const unsigned long kxcjk1013_scan_masks[] = {0x7, 0};
122109cf1b32SAdriana Reus
kxcjk1013_trigger_handler(int irq,void * p)12221a4fbf6aSSrinivas Pandruvada static irqreturn_t kxcjk1013_trigger_handler(int irq, void *p)
12231a4fbf6aSSrinivas Pandruvada {
12241a4fbf6aSSrinivas Pandruvada struct iio_poll_func *pf = p;
12251a4fbf6aSSrinivas Pandruvada struct iio_dev *indio_dev = pf->indio_dev;
12261a4fbf6aSSrinivas Pandruvada struct kxcjk1013_data *data = iio_priv(indio_dev);
122765ae47b0SAdriana Reus int ret;
12281a4fbf6aSSrinivas Pandruvada
12291a4fbf6aSSrinivas Pandruvada mutex_lock(&data->mutex);
123065ae47b0SAdriana Reus ret = i2c_smbus_read_i2c_block_data_or_emulated(data->client,
123165ae47b0SAdriana Reus KXCJK1013_REG_XOUT_L,
123265ae47b0SAdriana Reus AXIS_MAX * 2,
12333ab3aa2eSJonathan Cameron (u8 *)data->scan.chans);
12341a4fbf6aSSrinivas Pandruvada mutex_unlock(&data->mutex);
123565ae47b0SAdriana Reus if (ret < 0)
12361a4fbf6aSSrinivas Pandruvada goto err;
12371a4fbf6aSSrinivas Pandruvada
12383ab3aa2eSJonathan Cameron iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
1239b4b491c0SSrinivas Pandruvada data->timestamp);
12401a4fbf6aSSrinivas Pandruvada err:
12411a4fbf6aSSrinivas Pandruvada iio_trigger_notify_done(indio_dev->trig);
12421a4fbf6aSSrinivas Pandruvada
12431a4fbf6aSSrinivas Pandruvada return IRQ_HANDLED;
12441a4fbf6aSSrinivas Pandruvada }
12451a4fbf6aSSrinivas Pandruvada
kxcjk1013_trig_reen(struct iio_trigger * trig)1246eca8523aSJonathan Cameron static void kxcjk1013_trig_reen(struct iio_trigger *trig)
124759bfeabaSSrinivas Pandruvada {
124859bfeabaSSrinivas Pandruvada struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
124959bfeabaSSrinivas Pandruvada struct kxcjk1013_data *data = iio_priv(indio_dev);
125059bfeabaSSrinivas Pandruvada int ret;
125159bfeabaSSrinivas Pandruvada
1252ae4467f6SStephan Gerhold ret = i2c_smbus_read_byte_data(data->client, data->regs->int_rel);
1253eca8523aSJonathan Cameron if (ret < 0)
125459bfeabaSSrinivas Pandruvada dev_err(&data->client->dev, "Error reading reg_int_rel\n");
125559bfeabaSSrinivas Pandruvada }
125659bfeabaSSrinivas Pandruvada
kxcjk1013_data_rdy_trigger_set_state(struct iio_trigger * trig,bool state)12571a4fbf6aSSrinivas Pandruvada static int kxcjk1013_data_rdy_trigger_set_state(struct iio_trigger *trig,
12581a4fbf6aSSrinivas Pandruvada bool state)
12591a4fbf6aSSrinivas Pandruvada {
12601a4fbf6aSSrinivas Pandruvada struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
12611a4fbf6aSSrinivas Pandruvada struct kxcjk1013_data *data = iio_priv(indio_dev);
1262124e1b1dSSrinivas Pandruvada int ret;
1263124e1b1dSSrinivas Pandruvada
12641a4fbf6aSSrinivas Pandruvada mutex_lock(&data->mutex);
1265b4b491c0SSrinivas Pandruvada
1266b4b491c0SSrinivas Pandruvada if (!state && data->ev_enable_state && data->motion_trigger_on) {
1267b4b491c0SSrinivas Pandruvada data->motion_trigger_on = false;
1268b4b491c0SSrinivas Pandruvada mutex_unlock(&data->mutex);
1269b4b491c0SSrinivas Pandruvada return 0;
1270b4b491c0SSrinivas Pandruvada }
1271b4b491c0SSrinivas Pandruvada
1272124e1b1dSSrinivas Pandruvada ret = kxcjk1013_set_power_state(data, state);
1273124e1b1dSSrinivas Pandruvada if (ret < 0) {
127488f6da77SSrinivas Pandruvada mutex_unlock(&data->mutex);
1275124e1b1dSSrinivas Pandruvada return ret;
127688f6da77SSrinivas Pandruvada }
1277b4b491c0SSrinivas Pandruvada if (data->motion_trig == trig)
1278b4b491c0SSrinivas Pandruvada ret = kxcjk1013_setup_any_motion_interrupt(data, state);
1279b4b491c0SSrinivas Pandruvada else
1280b4b491c0SSrinivas Pandruvada ret = kxcjk1013_setup_new_data_interrupt(data, state);
1281b4b491c0SSrinivas Pandruvada if (ret < 0) {
1282fbd123e9SIrina Tirdea kxcjk1013_set_power_state(data, false);
1283b4b491c0SSrinivas Pandruvada mutex_unlock(&data->mutex);
1284b4b491c0SSrinivas Pandruvada return ret;
12851a4fbf6aSSrinivas Pandruvada }
1286b4b491c0SSrinivas Pandruvada if (data->motion_trig == trig)
1287b4b491c0SSrinivas Pandruvada data->motion_trigger_on = state;
1288b4b491c0SSrinivas Pandruvada else
1289b4b491c0SSrinivas Pandruvada data->dready_trigger_on = state;
1290b4b491c0SSrinivas Pandruvada
12911a4fbf6aSSrinivas Pandruvada mutex_unlock(&data->mutex);
12921a4fbf6aSSrinivas Pandruvada
12931a4fbf6aSSrinivas Pandruvada return 0;
12941a4fbf6aSSrinivas Pandruvada }
12951a4fbf6aSSrinivas Pandruvada
12961a4fbf6aSSrinivas Pandruvada static const struct iio_trigger_ops kxcjk1013_trigger_ops = {
12971a4fbf6aSSrinivas Pandruvada .set_trigger_state = kxcjk1013_data_rdy_trigger_set_state,
1298eca8523aSJonathan Cameron .reenable = kxcjk1013_trig_reen,
12991a4fbf6aSSrinivas Pandruvada };
13001a4fbf6aSSrinivas Pandruvada
kxcjk1013_report_motion_event(struct iio_dev * indio_dev)1301d8408e98SMichał Mirosław static void kxcjk1013_report_motion_event(struct iio_dev *indio_dev)
1302b4b491c0SSrinivas Pandruvada {
1303b4b491c0SSrinivas Pandruvada struct kxcjk1013_data *data = iio_priv(indio_dev);
1304b4b491c0SSrinivas Pandruvada
1305ae4467f6SStephan Gerhold int ret = i2c_smbus_read_byte_data(data->client, data->regs->int_src2);
1306b4b491c0SSrinivas Pandruvada if (ret < 0) {
1307d8408e98SMichał Mirosław dev_err(&data->client->dev, "Error reading reg_int_src2\n");
1308d8408e98SMichał Mirosław return;
1309b4b491c0SSrinivas Pandruvada }
1310b4b491c0SSrinivas Pandruvada
1311b4b491c0SSrinivas Pandruvada if (ret & KXCJK1013_REG_INT_SRC2_BIT_XN)
1312b4b491c0SSrinivas Pandruvada iio_push_event(indio_dev,
1313b4b491c0SSrinivas Pandruvada IIO_MOD_EVENT_CODE(IIO_ACCEL,
1314b4b491c0SSrinivas Pandruvada 0,
1315b4b491c0SSrinivas Pandruvada IIO_MOD_X,
1316b4b491c0SSrinivas Pandruvada IIO_EV_TYPE_THRESH,
1317b4b491c0SSrinivas Pandruvada IIO_EV_DIR_FALLING),
1318b4b491c0SSrinivas Pandruvada data->timestamp);
1319d8408e98SMichał Mirosław
1320b4b491c0SSrinivas Pandruvada if (ret & KXCJK1013_REG_INT_SRC2_BIT_XP)
1321b4b491c0SSrinivas Pandruvada iio_push_event(indio_dev,
1322b4b491c0SSrinivas Pandruvada IIO_MOD_EVENT_CODE(IIO_ACCEL,
1323b4b491c0SSrinivas Pandruvada 0,
1324b4b491c0SSrinivas Pandruvada IIO_MOD_X,
1325b4b491c0SSrinivas Pandruvada IIO_EV_TYPE_THRESH,
1326b4b491c0SSrinivas Pandruvada IIO_EV_DIR_RISING),
1327b4b491c0SSrinivas Pandruvada data->timestamp);
1328b4b491c0SSrinivas Pandruvada
1329b4b491c0SSrinivas Pandruvada if (ret & KXCJK1013_REG_INT_SRC2_BIT_YN)
1330b4b491c0SSrinivas Pandruvada iio_push_event(indio_dev,
1331b4b491c0SSrinivas Pandruvada IIO_MOD_EVENT_CODE(IIO_ACCEL,
1332b4b491c0SSrinivas Pandruvada 0,
1333b4b491c0SSrinivas Pandruvada IIO_MOD_Y,
1334b4b491c0SSrinivas Pandruvada IIO_EV_TYPE_THRESH,
1335b4b491c0SSrinivas Pandruvada IIO_EV_DIR_FALLING),
1336b4b491c0SSrinivas Pandruvada data->timestamp);
1337d8408e98SMichał Mirosław
1338b4b491c0SSrinivas Pandruvada if (ret & KXCJK1013_REG_INT_SRC2_BIT_YP)
1339b4b491c0SSrinivas Pandruvada iio_push_event(indio_dev,
1340b4b491c0SSrinivas Pandruvada IIO_MOD_EVENT_CODE(IIO_ACCEL,
1341b4b491c0SSrinivas Pandruvada 0,
1342b4b491c0SSrinivas Pandruvada IIO_MOD_Y,
1343b4b491c0SSrinivas Pandruvada IIO_EV_TYPE_THRESH,
1344b4b491c0SSrinivas Pandruvada IIO_EV_DIR_RISING),
1345b4b491c0SSrinivas Pandruvada data->timestamp);
1346b4b491c0SSrinivas Pandruvada
1347b4b491c0SSrinivas Pandruvada if (ret & KXCJK1013_REG_INT_SRC2_BIT_ZN)
1348b4b491c0SSrinivas Pandruvada iio_push_event(indio_dev,
1349b4b491c0SSrinivas Pandruvada IIO_MOD_EVENT_CODE(IIO_ACCEL,
1350b4b491c0SSrinivas Pandruvada 0,
1351b4b491c0SSrinivas Pandruvada IIO_MOD_Z,
1352b4b491c0SSrinivas Pandruvada IIO_EV_TYPE_THRESH,
1353b4b491c0SSrinivas Pandruvada IIO_EV_DIR_FALLING),
1354b4b491c0SSrinivas Pandruvada data->timestamp);
1355d8408e98SMichał Mirosław
1356b4b491c0SSrinivas Pandruvada if (ret & KXCJK1013_REG_INT_SRC2_BIT_ZP)
1357b4b491c0SSrinivas Pandruvada iio_push_event(indio_dev,
1358b4b491c0SSrinivas Pandruvada IIO_MOD_EVENT_CODE(IIO_ACCEL,
1359b4b491c0SSrinivas Pandruvada 0,
1360b4b491c0SSrinivas Pandruvada IIO_MOD_Z,
1361b4b491c0SSrinivas Pandruvada IIO_EV_TYPE_THRESH,
1362b4b491c0SSrinivas Pandruvada IIO_EV_DIR_RISING),
1363b4b491c0SSrinivas Pandruvada data->timestamp);
1364b4b491c0SSrinivas Pandruvada }
1365b4b491c0SSrinivas Pandruvada
kxcjk1013_event_handler(int irq,void * private)1366d8408e98SMichał Mirosław static irqreturn_t kxcjk1013_event_handler(int irq, void *private)
1367d8408e98SMichał Mirosław {
1368d8408e98SMichał Mirosław struct iio_dev *indio_dev = private;
1369d8408e98SMichał Mirosław struct kxcjk1013_data *data = iio_priv(indio_dev);
1370d8408e98SMichał Mirosław int ret;
1371d8408e98SMichał Mirosław
1372ae4467f6SStephan Gerhold ret = i2c_smbus_read_byte_data(data->client, data->regs->int_src1);
1373d8408e98SMichał Mirosław if (ret < 0) {
1374d8408e98SMichał Mirosław dev_err(&data->client->dev, "Error reading reg_int_src1\n");
1375d8408e98SMichał Mirosław goto ack_intr;
1376d8408e98SMichał Mirosław }
1377d8408e98SMichał Mirosław
1378d8408e98SMichał Mirosław if (ret & KXCJK1013_REG_INT_SRC1_BIT_WUFS) {
13791540d010SMichał Mirosław if (data->chipset == KXTF9)
13801540d010SMichał Mirosław iio_push_event(indio_dev,
13811540d010SMichał Mirosław IIO_MOD_EVENT_CODE(IIO_ACCEL,
13821540d010SMichał Mirosław 0,
13831540d010SMichał Mirosław IIO_MOD_X_AND_Y_AND_Z,
13841540d010SMichał Mirosław IIO_EV_TYPE_THRESH,
13851540d010SMichał Mirosław IIO_EV_DIR_RISING),
13861540d010SMichał Mirosław data->timestamp);
13871540d010SMichał Mirosław else
1388d8408e98SMichał Mirosław kxcjk1013_report_motion_event(indio_dev);
1389d8408e98SMichał Mirosław }
1390d8408e98SMichał Mirosław
1391b4b491c0SSrinivas Pandruvada ack_intr:
1392b4b491c0SSrinivas Pandruvada if (data->dready_trigger_on)
1393b4b491c0SSrinivas Pandruvada return IRQ_HANDLED;
1394b4b491c0SSrinivas Pandruvada
1395ae4467f6SStephan Gerhold ret = i2c_smbus_read_byte_data(data->client, data->regs->int_rel);
1396b4b491c0SSrinivas Pandruvada if (ret < 0)
1397b4b491c0SSrinivas Pandruvada dev_err(&data->client->dev, "Error reading reg_int_rel\n");
1398b4b491c0SSrinivas Pandruvada
1399b4b491c0SSrinivas Pandruvada return IRQ_HANDLED;
1400b4b491c0SSrinivas Pandruvada }
1401b4b491c0SSrinivas Pandruvada
kxcjk1013_data_rdy_trig_poll(int irq,void * private)1402b4b491c0SSrinivas Pandruvada static irqreturn_t kxcjk1013_data_rdy_trig_poll(int irq, void *private)
1403b4b491c0SSrinivas Pandruvada {
1404b4b491c0SSrinivas Pandruvada struct iio_dev *indio_dev = private;
1405b4b491c0SSrinivas Pandruvada struct kxcjk1013_data *data = iio_priv(indio_dev);
1406b4b491c0SSrinivas Pandruvada
1407bc2b7dabSGregor Boirie data->timestamp = iio_get_time_ns(indio_dev);
1408b4b491c0SSrinivas Pandruvada
1409b4b491c0SSrinivas Pandruvada if (data->dready_trigger_on)
1410b4b491c0SSrinivas Pandruvada iio_trigger_poll(data->dready_trig);
1411b4b491c0SSrinivas Pandruvada else if (data->motion_trigger_on)
1412b4b491c0SSrinivas Pandruvada iio_trigger_poll(data->motion_trig);
1413b4b491c0SSrinivas Pandruvada
1414b4b491c0SSrinivas Pandruvada if (data->ev_enable_state)
1415b4b491c0SSrinivas Pandruvada return IRQ_WAKE_THREAD;
1416b4b491c0SSrinivas Pandruvada else
1417b4b491c0SSrinivas Pandruvada return IRQ_HANDLED;
1418b4b491c0SSrinivas Pandruvada }
1419b4b491c0SSrinivas Pandruvada
kxcjk1013_match_acpi_device(struct device * dev,enum kx_chipset * chipset,enum kx_acpi_type * acpi_type,const char ** label)1420c6861377SDaniel Baluta static const char *kxcjk1013_match_acpi_device(struct device *dev,
14213bfa74f8SBastien Nocera enum kx_chipset *chipset,
142230132fe4SHans de Goede enum kx_acpi_type *acpi_type,
142330132fe4SHans de Goede const char **label)
14241a4fbf6aSSrinivas Pandruvada {
14251a4fbf6aSSrinivas Pandruvada const struct acpi_device_id *id;
1426b0868df4SRoberta Dobrescu
1427c6861377SDaniel Baluta id = acpi_match_device(dev->driver->acpi_match_table, dev);
1428c6861377SDaniel Baluta if (!id)
1429c6861377SDaniel Baluta return NULL;
1430e693e15eSAntonio Ospite
143130132fe4SHans de Goede if (strcmp(id->id, "SMO8500") == 0) {
143211e94f28SHans de Goede *acpi_type = ACPI_SMO8500;
143330132fe4SHans de Goede } else if (strcmp(id->id, "KIOX010A") == 0) {
1434e5b1032aSHans de Goede *acpi_type = ACPI_KIOX010A;
143530132fe4SHans de Goede *label = "accel-display";
143630132fe4SHans de Goede } else if (strcmp(id->id, "KIOX020A") == 0) {
143730132fe4SHans de Goede *label = "accel-base";
143830132fe4SHans de Goede }
1439e693e15eSAntonio Ospite
1440c6861377SDaniel Baluta *chipset = (enum kx_chipset)id->driver_data;
1441c6861377SDaniel Baluta
1442c6861377SDaniel Baluta return dev_name(dev);
1443c6861377SDaniel Baluta }
1444c6861377SDaniel Baluta
kxcjk1013_probe(struct i2c_client * client)144509cec083SUwe Kleine-König static int kxcjk1013_probe(struct i2c_client *client)
14461a4fbf6aSSrinivas Pandruvada {
144709cec083SUwe Kleine-König const struct i2c_device_id *id = i2c_client_get_device_id(client);
14481fa4ff1fSJonathan Cameron static const char * const regulator_names[] = { "vdd", "vddio" };
14491a4fbf6aSSrinivas Pandruvada struct kxcjk1013_data *data;
14501a4fbf6aSSrinivas Pandruvada struct iio_dev *indio_dev;
14511a4fbf6aSSrinivas Pandruvada struct kxcjk_1013_platform_data *pdata;
1452c6861377SDaniel Baluta const char *name;
14531a4fbf6aSSrinivas Pandruvada int ret;
14541a4fbf6aSSrinivas Pandruvada
14551a4fbf6aSSrinivas Pandruvada indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
14561a4fbf6aSSrinivas Pandruvada if (!indio_dev)
14571a4fbf6aSSrinivas Pandruvada return -ENOMEM;
14581a4fbf6aSSrinivas Pandruvada
14591a4fbf6aSSrinivas Pandruvada data = iio_priv(indio_dev);
14601a4fbf6aSSrinivas Pandruvada i2c_set_clientdata(client, indio_dev);
14611a4fbf6aSSrinivas Pandruvada data->client = client;
14621a4fbf6aSSrinivas Pandruvada
14631a4fbf6aSSrinivas Pandruvada pdata = dev_get_platdata(&client->dev);
14641bde330cSDmitry Osipenko if (pdata) {
14651a4fbf6aSSrinivas Pandruvada data->active_high_intr = pdata->active_high_intr;
14661bde330cSDmitry Osipenko data->orientation = pdata->orientation;
14671bde330cSDmitry Osipenko } else {
14681a4fbf6aSSrinivas Pandruvada data->active_high_intr = true; /* default polarity */
14691a4fbf6aSSrinivas Pandruvada
1470e074cc30SHans de Goede if (!iio_read_acpi_mount_matrix(&client->dev, &data->orientation, "ROTM")) {
1471b892770aSAndy Shevchenko ret = iio_read_mount_matrix(&client->dev, &data->orientation);
14721bde330cSDmitry Osipenko if (ret)
14731bde330cSDmitry Osipenko return ret;
14741bde330cSDmitry Osipenko }
14751bde330cSDmitry Osipenko
14763b4ebff2SSean Rhodes }
14773b4ebff2SSean Rhodes
14781fa4ff1fSJonathan Cameron ret = devm_regulator_bulk_get_enable(&client->dev,
14791fa4ff1fSJonathan Cameron ARRAY_SIZE(regulator_names),
14801fa4ff1fSJonathan Cameron regulator_names);
14811d2e91a2SDevajith V S if (ret)
14821d2e91a2SDevajith V S return dev_err_probe(&client->dev, ret, "Failed to get regulators\n");
14831d2e91a2SDevajith V S
14841d2e91a2SDevajith V S /*
14851d2e91a2SDevajith V S * A typical delay of 10ms is required for powering up
14861d2e91a2SDevajith V S * according to the data sheets of supported chips.
14871d2e91a2SDevajith V S * Hence double that to play safe.
14881d2e91a2SDevajith V S */
14891d2e91a2SDevajith V S msleep(20);
14901d2e91a2SDevajith V S
1491c6861377SDaniel Baluta if (id) {
1492c6861377SDaniel Baluta data->chipset = (enum kx_chipset)(id->driver_data);
1493c6861377SDaniel Baluta name = id->name;
1494c6861377SDaniel Baluta } else if (ACPI_HANDLE(&client->dev)) {
1495c6861377SDaniel Baluta name = kxcjk1013_match_acpi_device(&client->dev,
14963bfa74f8SBastien Nocera &data->chipset,
149730132fe4SHans de Goede &data->acpi_type,
149830132fe4SHans de Goede &indio_dev->label);
1499c6861377SDaniel Baluta } else
1500c6861377SDaniel Baluta return -ENODEV;
1501c6861377SDaniel Baluta
1502ae4467f6SStephan Gerhold switch (data->chipset) {
1503ae4467f6SStephan Gerhold case KXCJK1013:
1504ae4467f6SStephan Gerhold case KXCJ91008:
1505ae4467f6SStephan Gerhold case KXTJ21009:
1506ae4467f6SStephan Gerhold data->regs = &kxcjk1013_regs;
1507ae4467f6SStephan Gerhold break;
1508ae4467f6SStephan Gerhold case KXTF9:
1509ae4467f6SStephan Gerhold data->regs = &kxtf9_regs;
1510ae4467f6SStephan Gerhold break;
1511*d5cbe150SRayyan Ansari case KX0221020:
1512c630c176SStephan Gerhold case KX0231025:
1513c630c176SStephan Gerhold data->regs = &kx0231025_regs;
1514c630c176SStephan Gerhold break;
1515ae4467f6SStephan Gerhold default:
1516ae4467f6SStephan Gerhold return -EINVAL;
1517ae4467f6SStephan Gerhold }
1518ae4467f6SStephan Gerhold
15191a4fbf6aSSrinivas Pandruvada ret = kxcjk1013_chip_init(data);
15201a4fbf6aSSrinivas Pandruvada if (ret < 0)
15211a4fbf6aSSrinivas Pandruvada return ret;
15221a4fbf6aSSrinivas Pandruvada
15231a4fbf6aSSrinivas Pandruvada mutex_init(&data->mutex);
15241a4fbf6aSSrinivas Pandruvada
15251a4fbf6aSSrinivas Pandruvada indio_dev->channels = kxcjk1013_channels;
15261a4fbf6aSSrinivas Pandruvada indio_dev->num_channels = ARRAY_SIZE(kxcjk1013_channels);
152709cf1b32SAdriana Reus indio_dev->available_scan_masks = kxcjk1013_scan_masks;
1528c6861377SDaniel Baluta indio_dev->name = name;
15291a4fbf6aSSrinivas Pandruvada indio_dev->modes = INDIO_DIRECT_MODE;
15301a4fbf6aSSrinivas Pandruvada indio_dev->info = &kxcjk1013_info;
15311a4fbf6aSSrinivas Pandruvada
153211e94f28SHans de Goede if (client->irq > 0 && data->acpi_type != ACPI_SMO8500) {
1533b4b491c0SSrinivas Pandruvada ret = devm_request_threaded_irq(&client->dev, client->irq,
1534b4b491c0SSrinivas Pandruvada kxcjk1013_data_rdy_trig_poll,
1535b4b491c0SSrinivas Pandruvada kxcjk1013_event_handler,
15361a4fbf6aSSrinivas Pandruvada IRQF_TRIGGER_RISING,
15371a4fbf6aSSrinivas Pandruvada KXCJK1013_IRQ_NAME,
1538b4b491c0SSrinivas Pandruvada indio_dev);
15391a4fbf6aSSrinivas Pandruvada if (ret)
15409d02daf7SIrina Tirdea goto err_poweroff;
1541b4b491c0SSrinivas Pandruvada
1542b4b491c0SSrinivas Pandruvada data->dready_trig = devm_iio_trigger_alloc(&client->dev,
1543b4b491c0SSrinivas Pandruvada "%s-dev%d",
1544b4b491c0SSrinivas Pandruvada indio_dev->name,
154515ea2878SJonathan Cameron iio_device_id(indio_dev));
15469d02daf7SIrina Tirdea if (!data->dready_trig) {
15479d02daf7SIrina Tirdea ret = -ENOMEM;
15489d02daf7SIrina Tirdea goto err_poweroff;
15499d02daf7SIrina Tirdea }
1550b4b491c0SSrinivas Pandruvada
1551b4b491c0SSrinivas Pandruvada data->motion_trig = devm_iio_trigger_alloc(&client->dev,
1552b4b491c0SSrinivas Pandruvada "%s-any-motion-dev%d",
1553b4b491c0SSrinivas Pandruvada indio_dev->name,
155415ea2878SJonathan Cameron iio_device_id(indio_dev));
15559d02daf7SIrina Tirdea if (!data->motion_trig) {
15569d02daf7SIrina Tirdea ret = -ENOMEM;
15579d02daf7SIrina Tirdea goto err_poweroff;
15589d02daf7SIrina Tirdea }
1559b4b491c0SSrinivas Pandruvada
1560b4b491c0SSrinivas Pandruvada data->dready_trig->ops = &kxcjk1013_trigger_ops;
1561b4b491c0SSrinivas Pandruvada iio_trigger_set_drvdata(data->dready_trig, indio_dev);
1562b4b491c0SSrinivas Pandruvada ret = iio_trigger_register(data->dready_trig);
1563b4b491c0SSrinivas Pandruvada if (ret)
15649d02daf7SIrina Tirdea goto err_poweroff;
1565b4b491c0SSrinivas Pandruvada
1566ed302925SDmitry Rokosov indio_dev->trig = iio_trigger_get(data->dready_trig);
1567ed302925SDmitry Rokosov
1568b4b491c0SSrinivas Pandruvada data->motion_trig->ops = &kxcjk1013_trigger_ops;
1569b4b491c0SSrinivas Pandruvada iio_trigger_set_drvdata(data->motion_trig, indio_dev);
1570b4b491c0SSrinivas Pandruvada ret = iio_trigger_register(data->motion_trig);
1571b4b491c0SSrinivas Pandruvada if (ret) {
1572b4b491c0SSrinivas Pandruvada data->motion_trig = NULL;
1573b4b491c0SSrinivas Pandruvada goto err_trigger_unregister;
1574b4b491c0SSrinivas Pandruvada }
1575a25691c1SVlad Dogaru }
15761a4fbf6aSSrinivas Pandruvada
15771a4fbf6aSSrinivas Pandruvada ret = iio_triggered_buffer_setup(indio_dev,
15781a4fbf6aSSrinivas Pandruvada &iio_pollfunc_store_time,
15791a4fbf6aSSrinivas Pandruvada kxcjk1013_trigger_handler,
1580a25691c1SVlad Dogaru &kxcjk1013_buffer_setup_ops);
15811a4fbf6aSSrinivas Pandruvada if (ret < 0) {
1582a25691c1SVlad Dogaru dev_err(&client->dev, "iio triggered buffer setup failed\n");
15831a4fbf6aSSrinivas Pandruvada goto err_trigger_unregister;
15841a4fbf6aSSrinivas Pandruvada }
15851a4fbf6aSSrinivas Pandruvada
1586124e1b1dSSrinivas Pandruvada ret = pm_runtime_set_active(&client->dev);
1587124e1b1dSSrinivas Pandruvada if (ret)
15887d0ead5cSAdriana Reus goto err_buffer_cleanup;
1589124e1b1dSSrinivas Pandruvada
1590124e1b1dSSrinivas Pandruvada pm_runtime_enable(&client->dev);
1591124e1b1dSSrinivas Pandruvada pm_runtime_set_autosuspend_delay(&client->dev,
1592124e1b1dSSrinivas Pandruvada KXCJK1013_SLEEP_DELAY_MS);
1593124e1b1dSSrinivas Pandruvada pm_runtime_use_autosuspend(&client->dev);
1594124e1b1dSSrinivas Pandruvada
15957d0ead5cSAdriana Reus ret = iio_device_register(indio_dev);
15967d0ead5cSAdriana Reus if (ret < 0) {
15977d0ead5cSAdriana Reus dev_err(&client->dev, "unable to register iio device\n");
1598632fe0bbSMiaoqian Lin goto err_pm_cleanup;
15997d0ead5cSAdriana Reus }
16007d0ead5cSAdriana Reus
16011a4fbf6aSSrinivas Pandruvada return 0;
16021a4fbf6aSSrinivas Pandruvada
1603632fe0bbSMiaoqian Lin err_pm_cleanup:
1604632fe0bbSMiaoqian Lin pm_runtime_dont_use_autosuspend(&client->dev);
1605632fe0bbSMiaoqian Lin pm_runtime_disable(&client->dev);
16061a4fbf6aSSrinivas Pandruvada err_buffer_cleanup:
16071a4fbf6aSSrinivas Pandruvada iio_triggered_buffer_cleanup(indio_dev);
16081a4fbf6aSSrinivas Pandruvada err_trigger_unregister:
1609b4b491c0SSrinivas Pandruvada if (data->dready_trig)
1610b4b491c0SSrinivas Pandruvada iio_trigger_unregister(data->dready_trig);
1611b4b491c0SSrinivas Pandruvada if (data->motion_trig)
1612b4b491c0SSrinivas Pandruvada iio_trigger_unregister(data->motion_trig);
16139d02daf7SIrina Tirdea err_poweroff:
16149d02daf7SIrina Tirdea kxcjk1013_set_mode(data, STANDBY);
16151a4fbf6aSSrinivas Pandruvada
16161a4fbf6aSSrinivas Pandruvada return ret;
16171a4fbf6aSSrinivas Pandruvada }
16181a4fbf6aSSrinivas Pandruvada
kxcjk1013_remove(struct i2c_client * client)1619ed5c2f5fSUwe Kleine-König static void kxcjk1013_remove(struct i2c_client *client)
16201a4fbf6aSSrinivas Pandruvada {
16211a4fbf6aSSrinivas Pandruvada struct iio_dev *indio_dev = i2c_get_clientdata(client);
16221a4fbf6aSSrinivas Pandruvada struct kxcjk1013_data *data = iio_priv(indio_dev);
16231a4fbf6aSSrinivas Pandruvada
16247d0ead5cSAdriana Reus iio_device_unregister(indio_dev);
16257d0ead5cSAdriana Reus
1626124e1b1dSSrinivas Pandruvada pm_runtime_disable(&client->dev);
1627124e1b1dSSrinivas Pandruvada pm_runtime_set_suspended(&client->dev);
1628124e1b1dSSrinivas Pandruvada
16291a4fbf6aSSrinivas Pandruvada iio_triggered_buffer_cleanup(indio_dev);
163070c9774eSYang Yingliang if (data->dready_trig) {
1631b4b491c0SSrinivas Pandruvada iio_trigger_unregister(data->dready_trig);
1632b4b491c0SSrinivas Pandruvada iio_trigger_unregister(data->motion_trig);
16331a4fbf6aSSrinivas Pandruvada }
16341a4fbf6aSSrinivas Pandruvada
16351a4fbf6aSSrinivas Pandruvada mutex_lock(&data->mutex);
16361a4fbf6aSSrinivas Pandruvada kxcjk1013_set_mode(data, STANDBY);
16371a4fbf6aSSrinivas Pandruvada mutex_unlock(&data->mutex);
16381a4fbf6aSSrinivas Pandruvada }
16391a4fbf6aSSrinivas Pandruvada
16401a4fbf6aSSrinivas Pandruvada #ifdef CONFIG_PM_SLEEP
kxcjk1013_suspend(struct device * dev)16411a4fbf6aSSrinivas Pandruvada static int kxcjk1013_suspend(struct device *dev)
16421a4fbf6aSSrinivas Pandruvada {
16431a4fbf6aSSrinivas Pandruvada struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
16441a4fbf6aSSrinivas Pandruvada struct kxcjk1013_data *data = iio_priv(indio_dev);
1645124e1b1dSSrinivas Pandruvada int ret;
16461a4fbf6aSSrinivas Pandruvada
16471a4fbf6aSSrinivas Pandruvada mutex_lock(&data->mutex);
1648124e1b1dSSrinivas Pandruvada ret = kxcjk1013_set_mode(data, STANDBY);
16491a4fbf6aSSrinivas Pandruvada mutex_unlock(&data->mutex);
16501a4fbf6aSSrinivas Pandruvada
1651124e1b1dSSrinivas Pandruvada return ret;
16521a4fbf6aSSrinivas Pandruvada }
16531a4fbf6aSSrinivas Pandruvada
kxcjk1013_resume(struct device * dev)16541a4fbf6aSSrinivas Pandruvada static int kxcjk1013_resume(struct device *dev)
16551a4fbf6aSSrinivas Pandruvada {
16561a4fbf6aSSrinivas Pandruvada struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
16571a4fbf6aSSrinivas Pandruvada struct kxcjk1013_data *data = iio_priv(indio_dev);
1658124e1b1dSSrinivas Pandruvada int ret = 0;
16591a4fbf6aSSrinivas Pandruvada
16601a4fbf6aSSrinivas Pandruvada mutex_lock(&data->mutex);
1661124e1b1dSSrinivas Pandruvada ret = kxcjk1013_set_mode(data, OPERATION);
1662fe2d3df6She, bo if (ret == 0)
1663fe2d3df6She, bo ret = kxcjk1013_set_range(data, data->range);
16641a4fbf6aSSrinivas Pandruvada mutex_unlock(&data->mutex);
16651a4fbf6aSSrinivas Pandruvada
1666124e1b1dSSrinivas Pandruvada return ret;
1667124e1b1dSSrinivas Pandruvada }
1668124e1b1dSSrinivas Pandruvada #endif
1669124e1b1dSSrinivas Pandruvada
16706f0a13f2SRafael J. Wysocki #ifdef CONFIG_PM
kxcjk1013_runtime_suspend(struct device * dev)1671124e1b1dSSrinivas Pandruvada static int kxcjk1013_runtime_suspend(struct device *dev)
1672124e1b1dSSrinivas Pandruvada {
1673124e1b1dSSrinivas Pandruvada struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1674124e1b1dSSrinivas Pandruvada struct kxcjk1013_data *data = iio_priv(indio_dev);
1675fbd123e9SIrina Tirdea int ret;
1676124e1b1dSSrinivas Pandruvada
1677fbd123e9SIrina Tirdea ret = kxcjk1013_set_mode(data, STANDBY);
1678fbd123e9SIrina Tirdea if (ret < 0) {
1679fbd123e9SIrina Tirdea dev_err(&data->client->dev, "powering off device failed\n");
1680fbd123e9SIrina Tirdea return -EAGAIN;
1681fbd123e9SIrina Tirdea }
1682fbd123e9SIrina Tirdea return 0;
1683124e1b1dSSrinivas Pandruvada }
1684124e1b1dSSrinivas Pandruvada
kxcjk1013_runtime_resume(struct device * dev)1685124e1b1dSSrinivas Pandruvada static int kxcjk1013_runtime_resume(struct device *dev)
1686124e1b1dSSrinivas Pandruvada {
1687124e1b1dSSrinivas Pandruvada struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1688124e1b1dSSrinivas Pandruvada struct kxcjk1013_data *data = iio_priv(indio_dev);
1689124e1b1dSSrinivas Pandruvada int ret;
1690124e1b1dSSrinivas Pandruvada int sleep_val;
1691124e1b1dSSrinivas Pandruvada
1692124e1b1dSSrinivas Pandruvada ret = kxcjk1013_set_mode(data, OPERATION);
1693124e1b1dSSrinivas Pandruvada if (ret < 0)
1694124e1b1dSSrinivas Pandruvada return ret;
1695124e1b1dSSrinivas Pandruvada
1696124e1b1dSSrinivas Pandruvada sleep_val = kxcjk1013_get_startup_times(data);
1697124e1b1dSSrinivas Pandruvada if (sleep_val < 20000)
1698124e1b1dSSrinivas Pandruvada usleep_range(sleep_val, 20000);
1699124e1b1dSSrinivas Pandruvada else
1700124e1b1dSSrinivas Pandruvada msleep_interruptible(sleep_val/1000);
1701124e1b1dSSrinivas Pandruvada
17021a4fbf6aSSrinivas Pandruvada return 0;
17031a4fbf6aSSrinivas Pandruvada }
17041a4fbf6aSSrinivas Pandruvada #endif
17051a4fbf6aSSrinivas Pandruvada
1706124e1b1dSSrinivas Pandruvada static const struct dev_pm_ops kxcjk1013_pm_ops = {
1707124e1b1dSSrinivas Pandruvada SET_SYSTEM_SLEEP_PM_OPS(kxcjk1013_suspend, kxcjk1013_resume)
1708124e1b1dSSrinivas Pandruvada SET_RUNTIME_PM_OPS(kxcjk1013_runtime_suspend,
1709124e1b1dSSrinivas Pandruvada kxcjk1013_runtime_resume, NULL)
1710124e1b1dSSrinivas Pandruvada };
1711124e1b1dSSrinivas Pandruvada
17121a4fbf6aSSrinivas Pandruvada static const struct i2c_device_id kxcjk1013_id[] = {
1713c6861377SDaniel Baluta {"kxcjk1013", KXCJK1013},
1714c6861377SDaniel Baluta {"kxcj91008", KXCJ91008},
1715c6861377SDaniel Baluta {"kxtj21009", KXTJ21009},
17161540d010SMichał Mirosław {"kxtf9", KXTF9},
1717*d5cbe150SRayyan Ansari {"kx022-1020", KX0221020},
1718c630c176SStephan Gerhold {"kx023-1025", KX0231025},
17193bfa74f8SBastien Nocera {"SMO8500", KXCJ91008},
17201a4fbf6aSSrinivas Pandruvada {}
17211a4fbf6aSSrinivas Pandruvada };
17221a4fbf6aSSrinivas Pandruvada
17231a4fbf6aSSrinivas Pandruvada MODULE_DEVICE_TABLE(i2c, kxcjk1013_id);
17241a4fbf6aSSrinivas Pandruvada
17253b866fd2SRobert Yang static const struct of_device_id kxcjk1013_of_match[] = {
17263b866fd2SRobert Yang { .compatible = "kionix,kxcjk1013", },
17273b866fd2SRobert Yang { .compatible = "kionix,kxcj91008", },
17283b866fd2SRobert Yang { .compatible = "kionix,kxtj21009", },
17293b866fd2SRobert Yang { .compatible = "kionix,kxtf9", },
1730*d5cbe150SRayyan Ansari { .compatible = "kionix,kx022-1020", },
1731c630c176SStephan Gerhold { .compatible = "kionix,kx023-1025", },
17323b866fd2SRobert Yang { }
17333b866fd2SRobert Yang };
17343b866fd2SRobert Yang MODULE_DEVICE_TABLE(of, kxcjk1013_of_match);
17353b866fd2SRobert Yang
17361a4fbf6aSSrinivas Pandruvada static struct i2c_driver kxcjk1013_driver = {
17371a4fbf6aSSrinivas Pandruvada .driver = {
17381a4fbf6aSSrinivas Pandruvada .name = KXCJK1013_DRV_NAME,
17391a4fbf6aSSrinivas Pandruvada .acpi_match_table = ACPI_PTR(kx_acpi_match),
17403b866fd2SRobert Yang .of_match_table = kxcjk1013_of_match,
1741124e1b1dSSrinivas Pandruvada .pm = &kxcjk1013_pm_ops,
17421a4fbf6aSSrinivas Pandruvada },
17437cf15f42SUwe Kleine-König .probe = kxcjk1013_probe,
17441a4fbf6aSSrinivas Pandruvada .remove = kxcjk1013_remove,
17451a4fbf6aSSrinivas Pandruvada .id_table = kxcjk1013_id,
17461a4fbf6aSSrinivas Pandruvada };
17471a4fbf6aSSrinivas Pandruvada module_i2c_driver(kxcjk1013_driver);
17481a4fbf6aSSrinivas Pandruvada
17491a4fbf6aSSrinivas Pandruvada MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
17501a4fbf6aSSrinivas Pandruvada MODULE_LICENSE("GPL v2");
17511a4fbf6aSSrinivas Pandruvada MODULE_DESCRIPTION("KXCJK1013 accelerometer driver");
1752