blob: 0fe570316848276c9b1f8612618737c6e5f305a5 [file] [log] [blame]
Thomas Gleixner2025cf92019-05-29 07:18:02 -07001// SPDX-License-Identifier: GPL-2.0-only
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00002/*
3 * KXCJK-1013 3-axis accelerometer driver
4 * Copyright (c) 2014, Intel Corporation.
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00005 */
6
7#include <linux/module.h>
8#include <linux/i2c.h>
9#include <linux/interrupt.h>
10#include <linux/delay.h>
11#include <linux/bitops.h>
12#include <linux/slab.h>
13#include <linux/string.h>
14#include <linux/acpi.h>
Srinivas Pandruvada124e1b1d2014-05-08 22:58:00 +010015#include <linux/pm.h>
16#include <linux/pm_runtime.h>
Devajith V S1d2e91a2020-12-13 22:54:36 +053017#include <linux/regulator/consumer.h>
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +000018#include <linux/iio/iio.h>
19#include <linux/iio/sysfs.h>
20#include <linux/iio/buffer.h>
21#include <linux/iio/trigger.h>
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +010022#include <linux/iio/events.h>
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +000023#include <linux/iio/trigger_consumer.h>
24#include <linux/iio/triggered_buffer.h>
25#include <linux/iio/accel/kxcjk_1013.h>
26
27#define KXCJK1013_DRV_NAME "kxcjk1013"
28#define KXCJK1013_IRQ_NAME "kxcjk1013_event"
29
Michał Mirosław1540d012017-09-17 17:01:04 +020030#define KXTF9_REG_HP_XOUT_L 0x00
31#define KXTF9_REG_HP_XOUT_H 0x01
32#define KXTF9_REG_HP_YOUT_L 0x02
33#define KXTF9_REG_HP_YOUT_H 0x03
34#define KXTF9_REG_HP_ZOUT_L 0x04
35#define KXTF9_REG_HP_ZOUT_H 0x05
36
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +000037#define KXCJK1013_REG_XOUT_L 0x06
38/*
39 * From low byte X axis register, all the other addresses of Y and Z can be
40 * obtained by just applying axis offset. The following axis defines are just
41 * provide clarity, but not used.
42 */
43#define KXCJK1013_REG_XOUT_H 0x07
44#define KXCJK1013_REG_YOUT_L 0x08
45#define KXCJK1013_REG_YOUT_H 0x09
46#define KXCJK1013_REG_ZOUT_L 0x0A
47#define KXCJK1013_REG_ZOUT_H 0x0B
48
49#define KXCJK1013_REG_DCST_RESP 0x0C
50#define KXCJK1013_REG_WHO_AM_I 0x0F
Michał Mirosław1540d012017-09-17 17:01:04 +020051#define KXTF9_REG_TILT_POS_CUR 0x10
52#define KXTF9_REG_TILT_POS_PREV 0x11
53#define KXTF9_REG_INT_SRC1 0x15
Stephan Gerholdae4467f2021-05-11 16:24:01 +020054#define KXTF9_REG_INT_SRC2 0x16
55#define KXCJK1013_REG_INT_SRC1 0x16
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +000056#define KXCJK1013_REG_INT_SRC2 0x17
57#define KXCJK1013_REG_STATUS_REG 0x18
58#define KXCJK1013_REG_INT_REL 0x1A
59#define KXCJK1013_REG_CTRL1 0x1B
Michał Mirosław1540d012017-09-17 17:01:04 +020060#define KXTF9_REG_CTRL2 0x1C
Stephan Gerholdae4467f2021-05-11 16:24:01 +020061#define KXTF9_REG_CTRL3 0x1D
62#define KXCJK1013_REG_CTRL2 0x1D
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +000063#define KXCJK1013_REG_INT_CTRL1 0x1E
64#define KXCJK1013_REG_INT_CTRL2 0x1F
Michał Mirosław1540d012017-09-17 17:01:04 +020065#define KXTF9_REG_INT_CTRL3 0x20
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +000066#define KXCJK1013_REG_DATA_CTRL 0x21
Michał Mirosław1540d012017-09-17 17:01:04 +020067#define KXTF9_REG_TILT_TIMER 0x28
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +000068#define KXCJK1013_REG_WAKE_TIMER 0x29
Michał Mirosław1540d012017-09-17 17:01:04 +020069#define KXTF9_REG_TDT_TIMER 0x2B
70#define KXTF9_REG_TDT_THRESH_H 0x2C
71#define KXTF9_REG_TDT_THRESH_L 0x2D
72#define KXTF9_REG_TDT_TAP_TIMER 0x2E
73#define KXTF9_REG_TDT_TOTAL_TIMER 0x2F
74#define KXTF9_REG_TDT_LATENCY_TIMER 0x30
75#define KXTF9_REG_TDT_WINDOW_TIMER 0x31
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +000076#define KXCJK1013_REG_SELF_TEST 0x3A
Michał Mirosław1540d012017-09-17 17:01:04 +020077#define KXTF9_REG_WAKE_THRESH 0x5A
78#define KXTF9_REG_TILT_ANGLE 0x5C
79#define KXTF9_REG_HYST_SET 0x5F
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +000080#define KXCJK1013_REG_WAKE_THRES 0x6A
81
Stephan Gerholdc630c172021-05-11 16:24:02 +020082/* Everything up to 0x11 is equal to KXCJK1013/KXTF9 above */
83#define KX023_REG_INS1 0x12
84#define KX023_REG_INS2 0x13
85#define KX023_REG_INS3 0x14
86#define KX023_REG_STAT 0x15
87#define KX023_REG_INT_REL 0x17
88#define KX023_REG_CNTL1 0x18
89#define KX023_REG_CNTL2 0x19
90#define KX023_REG_CNTL3 0x1A
91#define KX023_REG_ODCNTL 0x1B
92#define KX023_REG_INC1 0x1C
93#define KX023_REG_INC2 0x1D
94#define KX023_REG_INC3 0x1E
95#define KX023_REG_INC4 0x1F
96#define KX023_REG_INC5 0x20
97#define KX023_REG_INC6 0x21
98#define KX023_REG_TILT_TIMER 0x22
99#define KX023_REG_WUFC 0x23
100#define KX023_REG_TDTRC 0x24
101#define KX023_REG_TDTC 0x25
102#define KX023_REG_TTH 0x26
103#define KX023_REG_TTL 0x27
104#define KX023_REG_FTD 0x28
105#define KX023_REG_STD 0x29
106#define KX023_REG_TLT 0x2A
107#define KX023_REG_TWS 0x2B
108#define KX023_REG_ATH 0x30
109#define KX023_REG_TILT_ANGLE_LL 0x32
110#define KX023_REG_TILT_ANGLE_HL 0x33
111#define KX023_REG_HYST_SET 0x34
112#define KX023_REG_LP_CNTL 0x35
113#define KX023_REG_BUF_CNTL1 0x3A
114#define KX023_REG_BUF_CNTL2 0x3B
115#define KX023_REG_BUF_STATUS_1 0x3C
116#define KX023_REG_BUF_STATUS_2 0x3D
117#define KX023_REG_BUF_CLEAR 0x3E
118#define KX023_REG_BUF_READ 0x3F
119#define KX023_REG_SELF_TEST 0x60
120
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000121#define KXCJK1013_REG_CTRL1_BIT_PC1 BIT(7)
122#define KXCJK1013_REG_CTRL1_BIT_RES BIT(6)
123#define KXCJK1013_REG_CTRL1_BIT_DRDY BIT(5)
124#define KXCJK1013_REG_CTRL1_BIT_GSEL1 BIT(4)
125#define KXCJK1013_REG_CTRL1_BIT_GSEL0 BIT(3)
126#define KXCJK1013_REG_CTRL1_BIT_WUFE BIT(1)
Michał Mirosławf74e4402017-09-17 17:01:02 +0200127
Michał Mirosław1540d012017-09-17 17:01:04 +0200128#define KXCJK1013_REG_INT_CTRL1_BIT_IEU BIT(2) /* KXTF9 */
Michał Mirosławf74e4402017-09-17 17:01:02 +0200129#define KXCJK1013_REG_INT_CTRL1_BIT_IEL BIT(3)
130#define KXCJK1013_REG_INT_CTRL1_BIT_IEA BIT(4)
131#define KXCJK1013_REG_INT_CTRL1_BIT_IEN BIT(5)
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000132
Michał Mirosław1540d012017-09-17 17:01:04 +0200133#define KXTF9_REG_TILT_BIT_LEFT_EDGE BIT(5)
134#define KXTF9_REG_TILT_BIT_RIGHT_EDGE BIT(4)
135#define KXTF9_REG_TILT_BIT_LOWER_EDGE BIT(3)
136#define KXTF9_REG_TILT_BIT_UPPER_EDGE BIT(2)
137#define KXTF9_REG_TILT_BIT_FACE_DOWN BIT(1)
138#define KXTF9_REG_TILT_BIT_FACE_UP BIT(0)
139
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000140#define KXCJK1013_DATA_MASK_12_BIT 0x0FFF
141#define KXCJK1013_MAX_STARTUP_TIME_US 100000
142
Srinivas Pandruvada124e1b1d2014-05-08 22:58:00 +0100143#define KXCJK1013_SLEEP_DELAY_MS 2000
144
Michał Mirosław1540d012017-09-17 17:01:04 +0200145#define KXCJK1013_REG_INT_SRC1_BIT_TPS BIT(0) /* KXTF9 */
Michał Mirosławf74e4402017-09-17 17:01:02 +0200146#define KXCJK1013_REG_INT_SRC1_BIT_WUFS BIT(1)
Michał Mirosław1540d012017-09-17 17:01:04 +0200147#define KXCJK1013_REG_INT_SRC1_MASK_TDTS (BIT(2) | BIT(3)) /* KXTF9 */
148#define KXCJK1013_REG_INT_SRC1_TAP_NONE 0
149#define KXCJK1013_REG_INT_SRC1_TAP_SINGLE BIT(2)
150#define KXCJK1013_REG_INT_SRC1_TAP_DOUBLE BIT(3)
Michał Mirosławf74e4402017-09-17 17:01:02 +0200151#define KXCJK1013_REG_INT_SRC1_BIT_DRDY BIT(4)
152
Michał Mirosław1540d012017-09-17 17:01:04 +0200153/* KXCJK: INT_SOURCE2: motion detect, KXTF9: INT_SRC_REG1: tap detect */
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +0100154#define KXCJK1013_REG_INT_SRC2_BIT_ZP BIT(0)
155#define KXCJK1013_REG_INT_SRC2_BIT_ZN BIT(1)
156#define KXCJK1013_REG_INT_SRC2_BIT_YP BIT(2)
157#define KXCJK1013_REG_INT_SRC2_BIT_YN BIT(3)
158#define KXCJK1013_REG_INT_SRC2_BIT_XP BIT(4)
159#define KXCJK1013_REG_INT_SRC2_BIT_XN BIT(5)
160
Stephan Gerholdc630c172021-05-11 16:24:02 +0200161/* KX023 interrupt routing to INT1. INT2 can be configured with INC6 */
162#define KX023_REG_INC4_BFI1 BIT(6)
163#define KX023_REG_INC4_WMI1 BIT(5)
164#define KX023_REG_INC4_DRDY1 BIT(4)
165#define KX023_REG_INC4_TDTI1 BIT(2)
166#define KX023_REG_INC4_WUFI1 BIT(1)
167#define KX023_REG_INC4_TPI1 BIT(0)
168
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +0100169#define KXCJK1013_DEFAULT_WAKE_THRES 1
170
Daniel Balutac6861372014-03-09 08:33:00 +0000171enum kx_chipset {
172 KXCJK1013,
173 KXCJ91008,
174 KXTJ21009,
Michał Mirosław1540d012017-09-17 17:01:04 +0200175 KXTF9,
Stephan Gerholdc630c172021-05-11 16:24:02 +0200176 KX0231025,
Daniel Balutac6861372014-03-09 08:33:00 +0000177 KX_MAX_CHIPS /* this must be last */
178};
179
Hans de Goede11e94f22020-11-10 14:38:34 +0100180enum kx_acpi_type {
181 ACPI_GENERIC,
182 ACPI_SMO8500,
Hans de Goedee5b10322020-11-10 14:38:35 +0100183 ACPI_KIOX010A,
Hans de Goede11e94f22020-11-10 14:38:34 +0100184};
185
Stephan Gerholdae4467f2021-05-11 16:24:01 +0200186struct kx_chipset_regs {
187 u8 int_src1;
188 u8 int_src2;
189 u8 int_rel;
190 u8 ctrl1;
191 u8 wuf_ctrl;
192 u8 int_ctrl1;
193 u8 data_ctrl;
194 u8 wake_timer;
195 u8 wake_thres;
196};
197
198static const struct kx_chipset_regs kxcjk1013_regs = {
199 .int_src1 = KXCJK1013_REG_INT_SRC1,
200 .int_src2 = KXCJK1013_REG_INT_SRC2,
201 .int_rel = KXCJK1013_REG_INT_REL,
202 .ctrl1 = KXCJK1013_REG_CTRL1,
203 .wuf_ctrl = KXCJK1013_REG_CTRL2,
204 .int_ctrl1 = KXCJK1013_REG_INT_CTRL1,
205 .data_ctrl = KXCJK1013_REG_DATA_CTRL,
206 .wake_timer = KXCJK1013_REG_WAKE_TIMER,
207 .wake_thres = KXCJK1013_REG_WAKE_THRES,
208};
209
210static const struct kx_chipset_regs kxtf9_regs = {
211 /* .int_src1 was moved to INT_SRC2 on KXTF9 */
212 .int_src1 = KXTF9_REG_INT_SRC2,
213 /* .int_src2 is not available */
214 .int_rel = KXCJK1013_REG_INT_REL,
215 .ctrl1 = KXCJK1013_REG_CTRL1,
216 .wuf_ctrl = KXTF9_REG_CTRL3,
217 .int_ctrl1 = KXCJK1013_REG_INT_CTRL1,
218 .data_ctrl = KXCJK1013_REG_DATA_CTRL,
219 .wake_timer = KXCJK1013_REG_WAKE_TIMER,
220 .wake_thres = KXTF9_REG_WAKE_THRESH,
221};
222
Stephan Gerholdc630c172021-05-11 16:24:02 +0200223/* The registers have totally different names but the bits are compatible */
224static const struct kx_chipset_regs kx0231025_regs = {
225 .int_src1 = KX023_REG_INS2,
226 .int_src2 = KX023_REG_INS3,
227 .int_rel = KX023_REG_INT_REL,
228 .ctrl1 = KX023_REG_CNTL1,
229 .wuf_ctrl = KX023_REG_CNTL3,
230 .int_ctrl1 = KX023_REG_INC1,
231 .data_ctrl = KX023_REG_ODCNTL,
232 .wake_timer = KX023_REG_WUFC,
233 .wake_thres = KX023_REG_ATH,
234};
235
Jonathan Cameron3ab3aa22021-05-01 18:01:06 +0100236enum kxcjk1013_axis {
237 AXIS_X,
238 AXIS_Y,
239 AXIS_Z,
240 AXIS_MAX
241};
242
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000243struct kxcjk1013_data {
Devajith V S1d2e91a2020-12-13 22:54:36 +0530244 struct regulator_bulk_data regulators[2];
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000245 struct i2c_client *client;
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +0100246 struct iio_trigger *dready_trig;
247 struct iio_trigger *motion_trig;
Dmitry Osipenko1bde3302020-01-12 23:33:00 +0300248 struct iio_mount_matrix orientation;
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000249 struct mutex mutex;
Jonathan Cameron3ab3aa22021-05-01 18:01:06 +0100250 /* Ensure timestamp naturally aligned */
251 struct {
252 s16 chans[AXIS_MAX];
253 s64 timestamp __aligned(8);
254 } scan;
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000255 u8 odr_bits;
Srinivas Pandruvadaa735e3d2014-05-08 22:58:00 +0100256 u8 range;
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +0100257 int wake_thres;
258 int wake_dur;
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000259 bool active_high_intr;
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +0100260 bool dready_trigger_on;
261 int ev_enable_state;
262 bool motion_trigger_on;
263 int64_t timestamp;
Daniel Balutac6861372014-03-09 08:33:00 +0000264 enum kx_chipset chipset;
Hans de Goede11e94f22020-11-10 14:38:34 +0100265 enum kx_acpi_type acpi_type;
Stephan Gerholdae4467f2021-05-11 16:24:01 +0200266 const struct kx_chipset_regs *regs;
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000267};
268
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000269enum kxcjk1013_mode {
270 STANDBY,
271 OPERATION,
272};
273
Srinivas Pandruvadaa735e3d2014-05-08 22:58:00 +0100274enum kxcjk1013_range {
275 KXCJK1013_RANGE_2G,
276 KXCJK1013_RANGE_4G,
277 KXCJK1013_RANGE_8G,
278};
279
Michał Mirosław7469ac62017-09-17 17:01:01 +0200280struct kx_odr_map {
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000281 int val;
282 int val2;
283 int odr_bits;
Michał Mirosław7469ac62017-09-17 17:01:01 +0200284 int wuf_bits;
285};
286
287static const struct kx_odr_map samp_freq_table[] = {
288 { 0, 781000, 0x08, 0x00 },
289 { 1, 563000, 0x09, 0x01 },
290 { 3, 125000, 0x0A, 0x02 },
291 { 6, 250000, 0x0B, 0x03 },
292 { 12, 500000, 0x00, 0x04 },
293 { 25, 0, 0x01, 0x05 },
294 { 50, 0, 0x02, 0x06 },
295 { 100, 0, 0x03, 0x06 },
296 { 200, 0, 0x04, 0x06 },
297 { 400, 0, 0x05, 0x06 },
298 { 800, 0, 0x06, 0x06 },
299 { 1600, 0, 0x07, 0x06 },
300};
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000301
Michał Mirosław81fa8cb2017-09-17 17:01:03 +0200302static const char *const kxcjk1013_samp_freq_avail =
303 "0.781000 1.563000 3.125000 6.250000 12.500000 25 50 100 200 400 800 1600";
304
Michał Mirosław1540d012017-09-17 17:01:04 +0200305static const struct kx_odr_map kxtf9_samp_freq_table[] = {
306 { 25, 0, 0x01, 0x00 },
307 { 50, 0, 0x02, 0x01 },
308 { 100, 0, 0x03, 0x01 },
309 { 200, 0, 0x04, 0x01 },
310 { 400, 0, 0x05, 0x01 },
311 { 800, 0, 0x06, 0x01 },
312};
313
314static const char *const kxtf9_samp_freq_avail =
315 "25 50 100 200 400 800";
316
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000317/* Refer to section 4 of the specification */
Jonathan Cameron8ebbfb92021-11-28 17:24:45 +0000318static __maybe_unused const struct {
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000319 int odr_bits;
320 int usec;
Daniel Balutac6861372014-03-09 08:33:00 +0000321} odr_start_up_times[KX_MAX_CHIPS][12] = {
322 /* KXCJK-1013 */
323 {
324 {0x08, 100000},
325 {0x09, 100000},
326 {0x0A, 100000},
327 {0x0B, 100000},
328 {0, 80000},
329 {0x01, 41000},
330 {0x02, 21000},
331 {0x03, 11000},
332 {0x04, 6400},
333 {0x05, 3900},
334 {0x06, 2700},
335 {0x07, 2100},
336 },
337 /* KXCJ9-1008 */
338 {
339 {0x08, 100000},
340 {0x09, 100000},
341 {0x0A, 100000},
342 {0x0B, 100000},
343 {0, 80000},
344 {0x01, 41000},
345 {0x02, 21000},
346 {0x03, 11000},
347 {0x04, 6400},
348 {0x05, 3900},
349 {0x06, 2700},
350 {0x07, 2100},
351 },
352 /* KXCTJ2-1009 */
353 {
354 {0x08, 1240000},
355 {0x09, 621000},
356 {0x0A, 309000},
357 {0x0B, 151000},
358 {0, 80000},
359 {0x01, 41000},
360 {0x02, 21000},
361 {0x03, 11000},
362 {0x04, 6000},
363 {0x05, 4000},
364 {0x06, 3000},
365 {0x07, 2000},
366 },
Michał Mirosław1540d012017-09-17 17:01:04 +0200367 /* KXTF9 */
368 {
369 {0x01, 81000},
370 {0x02, 41000},
371 {0x03, 21000},
372 {0x04, 11000},
373 {0x05, 5100},
374 {0x06, 2700},
375 },
Stephan Gerholdc630c172021-05-11 16:24:02 +0200376 /* KX023-1025 */
377 {
378 /* First 4 are not in datasheet, taken from KXCTJ2-1009 */
379 {0x08, 1240000},
380 {0x09, 621000},
381 {0x0A, 309000},
382 {0x0B, 151000},
383 {0, 81000},
384 {0x01, 40000},
385 {0x02, 22000},
386 {0x03, 12000},
387 {0x04, 7000},
388 {0x05, 4400},
389 {0x06, 3000},
390 {0x07, 3000},
391 },
Daniel Balutac6861372014-03-09 08:33:00 +0000392};
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000393
Srinivas Pandruvadaa735e3d2014-05-08 22:58:00 +0100394static const struct {
395 u16 scale;
396 u8 gsel_0;
397 u8 gsel_1;
398} KXCJK1013_scale_table[] = { {9582, 0, 0},
399 {19163, 1, 0},
400 {38326, 0, 1} };
401
Hans de Goedee5b10322020-11-10 14:38:35 +0100402#ifdef CONFIG_ACPI
403enum kiox010a_fn_index {
404 KIOX010A_SET_LAPTOP_MODE = 1,
405 KIOX010A_SET_TABLET_MODE = 2,
406};
407
408static int kiox010a_dsm(struct device *dev, int fn_index)
409{
410 acpi_handle handle = ACPI_HANDLE(dev);
411 guid_t kiox010a_dsm_guid;
412 union acpi_object *obj;
413
414 if (!handle)
415 return -ENODEV;
416
417 guid_parse("1f339696-d475-4e26-8cad-2e9f8e6d7a91", &kiox010a_dsm_guid);
418
419 obj = acpi_evaluate_dsm(handle, &kiox010a_dsm_guid, 1, fn_index, NULL);
420 if (!obj)
421 return -EIO;
422
423 ACPI_FREE(obj);
424 return 0;
425}
426#endif
427
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000428static int kxcjk1013_set_mode(struct kxcjk1013_data *data,
429 enum kxcjk1013_mode mode)
430{
431 int ret;
432
Stephan Gerholdae4467f2021-05-11 16:24:01 +0200433 ret = i2c_smbus_read_byte_data(data->client, data->regs->ctrl1);
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000434 if (ret < 0) {
435 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
436 return ret;
437 }
438
439 if (mode == STANDBY)
440 ret &= ~KXCJK1013_REG_CTRL1_BIT_PC1;
441 else
442 ret |= KXCJK1013_REG_CTRL1_BIT_PC1;
443
Stephan Gerholdae4467f2021-05-11 16:24:01 +0200444 ret = i2c_smbus_write_byte_data(data->client, data->regs->ctrl1, ret);
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000445 if (ret < 0) {
446 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
447 return ret;
448 }
449
450 return 0;
451}
452
Srinivas Pandruvada124e1b1d2014-05-08 22:58:00 +0100453static int kxcjk1013_get_mode(struct kxcjk1013_data *data,
454 enum kxcjk1013_mode *mode)
455{
456 int ret;
457
Stephan Gerholdae4467f2021-05-11 16:24:01 +0200458 ret = i2c_smbus_read_byte_data(data->client, data->regs->ctrl1);
Srinivas Pandruvada124e1b1d2014-05-08 22:58:00 +0100459 if (ret < 0) {
460 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
461 return ret;
462 }
463
464 if (ret & KXCJK1013_REG_CTRL1_BIT_PC1)
465 *mode = OPERATION;
466 else
467 *mode = STANDBY;
468
469 return 0;
470}
471
Srinivas Pandruvadaa735e3d2014-05-08 22:58:00 +0100472static int kxcjk1013_set_range(struct kxcjk1013_data *data, int range_index)
473{
474 int ret;
475
Stephan Gerholdae4467f2021-05-11 16:24:01 +0200476 ret = i2c_smbus_read_byte_data(data->client, data->regs->ctrl1);
Srinivas Pandruvadaa735e3d2014-05-08 22:58:00 +0100477 if (ret < 0) {
478 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
479 return ret;
480 }
481
Daniel Balutae90dea62014-11-10 10:20:16 +0200482 ret &= ~(KXCJK1013_REG_CTRL1_BIT_GSEL0 |
483 KXCJK1013_REG_CTRL1_BIT_GSEL1);
Srinivas Pandruvadaa735e3d2014-05-08 22:58:00 +0100484 ret |= (KXCJK1013_scale_table[range_index].gsel_0 << 3);
485 ret |= (KXCJK1013_scale_table[range_index].gsel_1 << 4);
486
Stephan Gerholdae4467f2021-05-11 16:24:01 +0200487 ret = i2c_smbus_write_byte_data(data->client, data->regs->ctrl1, ret);
Srinivas Pandruvadaa735e3d2014-05-08 22:58:00 +0100488 if (ret < 0) {
489 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
490 return ret;
491 }
492
493 data->range = range_index;
494
495 return 0;
496}
497
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000498static int kxcjk1013_chip_init(struct kxcjk1013_data *data)
499{
500 int ret;
501
Hans de Goedee5b10322020-11-10 14:38:35 +0100502#ifdef CONFIG_ACPI
503 if (data->acpi_type == ACPI_KIOX010A) {
504 /* Make sure the kbd and touchpad on 2-in-1s using 2 KXCJ91008-s work */
505 kiox010a_dsm(&data->client->dev, KIOX010A_SET_LAPTOP_MODE);
506 }
507#endif
508
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000509 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_WHO_AM_I);
510 if (ret < 0) {
511 dev_err(&data->client->dev, "Error reading who_am_i\n");
512 return ret;
513 }
514
515 dev_dbg(&data->client->dev, "KXCJK1013 Chip Id %x\n", ret);
516
517 ret = kxcjk1013_set_mode(data, STANDBY);
518 if (ret < 0)
519 return ret;
520
Stephan Gerholdae4467f2021-05-11 16:24:01 +0200521 ret = i2c_smbus_read_byte_data(data->client, data->regs->ctrl1);
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000522 if (ret < 0) {
523 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
524 return ret;
525 }
526
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000527 /* Set 12 bit mode */
528 ret |= KXCJK1013_REG_CTRL1_BIT_RES;
529
Stephan Gerholdae4467f2021-05-11 16:24:01 +0200530 ret = i2c_smbus_write_byte_data(data->client, data->regs->ctrl1, ret);
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000531 if (ret < 0) {
532 dev_err(&data->client->dev, "Error reading reg_ctrl\n");
533 return ret;
534 }
535
Srinivas Pandruvadaa735e3d2014-05-08 22:58:00 +0100536 /* Setting range to 4G */
537 ret = kxcjk1013_set_range(data, KXCJK1013_RANGE_4G);
538 if (ret < 0)
539 return ret;
540
Stephan Gerholdae4467f2021-05-11 16:24:01 +0200541 ret = i2c_smbus_read_byte_data(data->client, data->regs->data_ctrl);
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000542 if (ret < 0) {
543 dev_err(&data->client->dev, "Error reading reg_data_ctrl\n");
544 return ret;
545 }
546
547 data->odr_bits = ret;
548
549 /* Set up INT polarity */
Stephan Gerholdae4467f2021-05-11 16:24:01 +0200550 ret = i2c_smbus_read_byte_data(data->client, data->regs->int_ctrl1);
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000551 if (ret < 0) {
552 dev_err(&data->client->dev, "Error reading reg_int_ctrl1\n");
553 return ret;
554 }
555
556 if (data->active_high_intr)
Michał Mirosławf74e4402017-09-17 17:01:02 +0200557 ret |= KXCJK1013_REG_INT_CTRL1_BIT_IEA;
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000558 else
Michał Mirosławf74e4402017-09-17 17:01:02 +0200559 ret &= ~KXCJK1013_REG_INT_CTRL1_BIT_IEA;
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000560
Stephan Gerholdae4467f2021-05-11 16:24:01 +0200561 ret = i2c_smbus_write_byte_data(data->client, data->regs->int_ctrl1, ret);
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000562 if (ret < 0) {
563 dev_err(&data->client->dev, "Error writing reg_int_ctrl1\n");
564 return ret;
565 }
566
Stephan Gerholdc630c172021-05-11 16:24:02 +0200567 /* On KX023, route all used interrupts to INT1 for now */
568 if (data->chipset == KX0231025 && data->client->irq > 0) {
569 ret = i2c_smbus_write_byte_data(data->client, KX023_REG_INC4,
570 KX023_REG_INC4_DRDY1 |
571 KX023_REG_INC4_WUFI1);
572 if (ret < 0) {
573 dev_err(&data->client->dev, "Error writing reg_inc4\n");
574 return ret;
575 }
576 }
577
Srinivas Pandruvada124e1b1d2014-05-08 22:58:00 +0100578 ret = kxcjk1013_set_mode(data, OPERATION);
579 if (ret < 0)
580 return ret;
581
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +0100582 data->wake_thres = KXCJK1013_DEFAULT_WAKE_THRES;
583
Srinivas Pandruvada124e1b1d2014-05-08 22:58:00 +0100584 return 0;
585}
586
Rafael J. Wysocki6f0a13f2014-12-04 01:08:13 +0100587#ifdef CONFIG_PM
Srinivas Pandruvada124e1b1d2014-05-08 22:58:00 +0100588static int kxcjk1013_get_startup_times(struct kxcjk1013_data *data)
589{
590 int i;
Daniel Balutac6861372014-03-09 08:33:00 +0000591 int idx = data->chipset;
Srinivas Pandruvada124e1b1d2014-05-08 22:58:00 +0100592
Daniel Balutac6861372014-03-09 08:33:00 +0000593 for (i = 0; i < ARRAY_SIZE(odr_start_up_times[idx]); ++i) {
594 if (odr_start_up_times[idx][i].odr_bits == data->odr_bits)
595 return odr_start_up_times[idx][i].usec;
Srinivas Pandruvada124e1b1d2014-05-08 22:58:00 +0100596 }
597
598 return KXCJK1013_MAX_STARTUP_TIME_US;
599}
Daniel Balutac9bf2372014-03-09 16:13:00 +0000600#endif
Srinivas Pandruvada124e1b1d2014-05-08 22:58:00 +0100601
602static int kxcjk1013_set_power_state(struct kxcjk1013_data *data, bool on)
603{
Irina Tirdeac75b8dc2014-12-06 00:18:08 +0200604#ifdef CONFIG_PM
Srinivas Pandruvada124e1b1d2014-05-08 22:58:00 +0100605 int ret;
606
607 if (on)
Jonathan Cameron19611ae2021-05-09 12:33:33 +0100608 ret = pm_runtime_resume_and_get(&data->client->dev);
Srinivas Pandruvada124e1b1d2014-05-08 22:58:00 +0100609 else {
610 pm_runtime_mark_last_busy(&data->client->dev);
611 ret = pm_runtime_put_autosuspend(&data->client->dev);
612 }
613 if (ret < 0) {
614 dev_err(&data->client->dev,
Mohan Kumarce7afa52019-04-19 11:36:51 +0300615 "Failed: %s for %d\n", __func__, on);
Srinivas Pandruvada124e1b1d2014-05-08 22:58:00 +0100616 return ret;
617 }
Irina Tirdeac75b8dc2014-12-06 00:18:08 +0200618#endif
Srinivas Pandruvada124e1b1d2014-05-08 22:58:00 +0100619
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000620 return 0;
621}
622
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +0100623static int kxcjk1013_chip_update_thresholds(struct kxcjk1013_data *data)
624{
Stephan Gerholdae4467f2021-05-11 16:24:01 +0200625 int ret;
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +0100626
Stephan Gerholdae4467f2021-05-11 16:24:01 +0200627 ret = i2c_smbus_write_byte_data(data->client, data->regs->wake_timer,
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +0100628 data->wake_dur);
629 if (ret < 0) {
630 dev_err(&data->client->dev,
631 "Error writing reg_wake_timer\n");
632 return ret;
633 }
634
Stephan Gerholdae4467f2021-05-11 16:24:01 +0200635 ret = i2c_smbus_write_byte_data(data->client, data->regs->wake_thres,
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +0100636 data->wake_thres);
637 if (ret < 0) {
638 dev_err(&data->client->dev, "Error writing reg_wake_thres\n");
639 return ret;
640 }
641
642 return 0;
643}
644
645static int kxcjk1013_setup_any_motion_interrupt(struct kxcjk1013_data *data,
646 bool status)
647{
648 int ret;
649 enum kxcjk1013_mode store_mode;
650
651 ret = kxcjk1013_get_mode(data, &store_mode);
652 if (ret < 0)
653 return ret;
654
655 /* This is requirement by spec to change state to STANDBY */
656 ret = kxcjk1013_set_mode(data, STANDBY);
657 if (ret < 0)
658 return ret;
659
660 ret = kxcjk1013_chip_update_thresholds(data);
661 if (ret < 0)
662 return ret;
663
Stephan Gerholdae4467f2021-05-11 16:24:01 +0200664 ret = i2c_smbus_read_byte_data(data->client, data->regs->int_ctrl1);
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +0100665 if (ret < 0) {
666 dev_err(&data->client->dev, "Error reading reg_int_ctrl1\n");
667 return ret;
668 }
669
670 if (status)
Michał Mirosławf74e4402017-09-17 17:01:02 +0200671 ret |= KXCJK1013_REG_INT_CTRL1_BIT_IEN;
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +0100672 else
Michał Mirosławf74e4402017-09-17 17:01:02 +0200673 ret &= ~KXCJK1013_REG_INT_CTRL1_BIT_IEN;
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +0100674
Stephan Gerholdae4467f2021-05-11 16:24:01 +0200675 ret = i2c_smbus_write_byte_data(data->client, data->regs->int_ctrl1, ret);
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +0100676 if (ret < 0) {
677 dev_err(&data->client->dev, "Error writing reg_int_ctrl1\n");
678 return ret;
679 }
680
Stephan Gerholdae4467f2021-05-11 16:24:01 +0200681 ret = i2c_smbus_read_byte_data(data->client, data->regs->ctrl1);
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +0100682 if (ret < 0) {
683 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
684 return ret;
685 }
686
687 if (status)
688 ret |= KXCJK1013_REG_CTRL1_BIT_WUFE;
689 else
690 ret &= ~KXCJK1013_REG_CTRL1_BIT_WUFE;
691
Stephan Gerholdae4467f2021-05-11 16:24:01 +0200692 ret = i2c_smbus_write_byte_data(data->client, data->regs->ctrl1, ret);
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +0100693 if (ret < 0) {
694 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
695 return ret;
696 }
697
698 if (store_mode == OPERATION) {
699 ret = kxcjk1013_set_mode(data, OPERATION);
700 if (ret < 0)
701 return ret;
702 }
703
704 return 0;
705}
706
707static int kxcjk1013_setup_new_data_interrupt(struct kxcjk1013_data *data,
708 bool status)
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000709{
710 int ret;
Srinivas Pandruvada124e1b1d2014-05-08 22:58:00 +0100711 enum kxcjk1013_mode store_mode;
712
713 ret = kxcjk1013_get_mode(data, &store_mode);
714 if (ret < 0)
715 return ret;
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000716
717 /* This is requirement by spec to change state to STANDBY */
718 ret = kxcjk1013_set_mode(data, STANDBY);
719 if (ret < 0)
720 return ret;
721
Stephan Gerholdae4467f2021-05-11 16:24:01 +0200722 ret = i2c_smbus_read_byte_data(data->client, data->regs->int_ctrl1);
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000723 if (ret < 0) {
724 dev_err(&data->client->dev, "Error reading reg_int_ctrl1\n");
725 return ret;
726 }
727
728 if (status)
Michał Mirosławf74e4402017-09-17 17:01:02 +0200729 ret |= KXCJK1013_REG_INT_CTRL1_BIT_IEN;
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000730 else
Michał Mirosławf74e4402017-09-17 17:01:02 +0200731 ret &= ~KXCJK1013_REG_INT_CTRL1_BIT_IEN;
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000732
Stephan Gerholdae4467f2021-05-11 16:24:01 +0200733 ret = i2c_smbus_write_byte_data(data->client, data->regs->int_ctrl1, ret);
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000734 if (ret < 0) {
735 dev_err(&data->client->dev, "Error writing reg_int_ctrl1\n");
736 return ret;
737 }
738
Stephan Gerholdae4467f2021-05-11 16:24:01 +0200739 ret = i2c_smbus_read_byte_data(data->client, data->regs->ctrl1);
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000740 if (ret < 0) {
741 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
742 return ret;
743 }
744
745 if (status)
746 ret |= KXCJK1013_REG_CTRL1_BIT_DRDY;
747 else
748 ret &= ~KXCJK1013_REG_CTRL1_BIT_DRDY;
749
Stephan Gerholdae4467f2021-05-11 16:24:01 +0200750 ret = i2c_smbus_write_byte_data(data->client, data->regs->ctrl1, ret);
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000751 if (ret < 0) {
752 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
753 return ret;
754 }
755
Srinivas Pandruvada124e1b1d2014-05-08 22:58:00 +0100756 if (store_mode == OPERATION) {
757 ret = kxcjk1013_set_mode(data, OPERATION);
758 if (ret < 0)
759 return ret;
760 }
761
762 return 0;
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000763}
764
Michał Mirosław7469ac62017-09-17 17:01:01 +0200765static const struct kx_odr_map *kxcjk1013_find_odr_value(
766 const struct kx_odr_map *map, size_t map_size, int val, int val2)
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000767{
768 int i;
769
Michał Mirosław7469ac62017-09-17 17:01:01 +0200770 for (i = 0; i < map_size; ++i) {
771 if (map[i].val == val && map[i].val2 == val2)
772 return &map[i];
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000773 }
774
Michał Mirosław7469ac62017-09-17 17:01:01 +0200775 return ERR_PTR(-EINVAL);
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000776}
777
Michał Mirosław7469ac62017-09-17 17:01:01 +0200778static int kxcjk1013_convert_odr_value(const struct kx_odr_map *map,
779 size_t map_size, int odr_bits,
780 int *val, int *val2)
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +0100781{
782 int i;
783
Michał Mirosław7469ac62017-09-17 17:01:01 +0200784 for (i = 0; i < map_size; ++i) {
785 if (map[i].odr_bits == odr_bits) {
786 *val = map[i].val;
787 *val2 = map[i].val2;
788 return IIO_VAL_INT_PLUS_MICRO;
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +0100789 }
790 }
791
792 return -EINVAL;
793}
794
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000795static int kxcjk1013_set_odr(struct kxcjk1013_data *data, int val, int val2)
796{
797 int ret;
Srinivas Pandruvada124e1b1d2014-05-08 22:58:00 +0100798 enum kxcjk1013_mode store_mode;
Michał Mirosław7469ac62017-09-17 17:01:01 +0200799 const struct kx_odr_map *odr_setting;
Srinivas Pandruvada124e1b1d2014-05-08 22:58:00 +0100800
801 ret = kxcjk1013_get_mode(data, &store_mode);
802 if (ret < 0)
803 return ret;
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000804
Michał Mirosław1540d012017-09-17 17:01:04 +0200805 if (data->chipset == KXTF9)
806 odr_setting = kxcjk1013_find_odr_value(kxtf9_samp_freq_table,
807 ARRAY_SIZE(kxtf9_samp_freq_table),
808 val, val2);
809 else
810 odr_setting = kxcjk1013_find_odr_value(samp_freq_table,
811 ARRAY_SIZE(samp_freq_table),
812 val, val2);
Michał Mirosław7469ac62017-09-17 17:01:01 +0200813
814 if (IS_ERR(odr_setting))
815 return PTR_ERR(odr_setting);
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000816
817 /* To change ODR, the chip must be set to STANDBY as per spec */
818 ret = kxcjk1013_set_mode(data, STANDBY);
819 if (ret < 0)
820 return ret;
821
Stephan Gerholdae4467f2021-05-11 16:24:01 +0200822 ret = i2c_smbus_write_byte_data(data->client, data->regs->data_ctrl,
Michał Mirosław7469ac62017-09-17 17:01:01 +0200823 odr_setting->odr_bits);
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000824 if (ret < 0) {
825 dev_err(&data->client->dev, "Error writing data_ctrl\n");
826 return ret;
827 }
828
Michał Mirosław7469ac62017-09-17 17:01:01 +0200829 data->odr_bits = odr_setting->odr_bits;
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +0100830
Stephan Gerholdae4467f2021-05-11 16:24:01 +0200831 ret = i2c_smbus_write_byte_data(data->client, data->regs->wuf_ctrl,
Michał Mirosław7469ac62017-09-17 17:01:01 +0200832 odr_setting->wuf_bits);
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +0100833 if (ret < 0) {
834 dev_err(&data->client->dev, "Error writing reg_ctrl2\n");
835 return ret;
836 }
837
Srinivas Pandruvada124e1b1d2014-05-08 22:58:00 +0100838 if (store_mode == OPERATION) {
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000839 ret = kxcjk1013_set_mode(data, OPERATION);
840 if (ret < 0)
841 return ret;
842 }
843
844 return 0;
845}
846
847static int kxcjk1013_get_odr(struct kxcjk1013_data *data, int *val, int *val2)
848{
Michał Mirosław1540d012017-09-17 17:01:04 +0200849 if (data->chipset == KXTF9)
850 return kxcjk1013_convert_odr_value(kxtf9_samp_freq_table,
851 ARRAY_SIZE(kxtf9_samp_freq_table),
852 data->odr_bits, val, val2);
853 else
854 return kxcjk1013_convert_odr_value(samp_freq_table,
855 ARRAY_SIZE(samp_freq_table),
856 data->odr_bits, val, val2);
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000857}
858
859static int kxcjk1013_get_acc_reg(struct kxcjk1013_data *data, int axis)
860{
861 u8 reg = KXCJK1013_REG_XOUT_L + axis * 2;
862 int ret;
863
864 ret = i2c_smbus_read_word_data(data->client, reg);
865 if (ret < 0) {
866 dev_err(&data->client->dev,
867 "failed to read accel_%c registers\n", 'x' + axis);
868 return ret;
869 }
870
871 return ret;
872}
873
Srinivas Pandruvadaa735e3d2014-05-08 22:58:00 +0100874static int kxcjk1013_set_scale(struct kxcjk1013_data *data, int val)
875{
876 int ret, i;
877 enum kxcjk1013_mode store_mode;
878
Srinivas Pandruvadaa735e3d2014-05-08 22:58:00 +0100879 for (i = 0; i < ARRAY_SIZE(KXCJK1013_scale_table); ++i) {
880 if (KXCJK1013_scale_table[i].scale == val) {
Srinivas Pandruvadaa735e3d2014-05-08 22:58:00 +0100881 ret = kxcjk1013_get_mode(data, &store_mode);
882 if (ret < 0)
883 return ret;
884
885 ret = kxcjk1013_set_mode(data, STANDBY);
886 if (ret < 0)
887 return ret;
888
889 ret = kxcjk1013_set_range(data, i);
890 if (ret < 0)
891 return ret;
892
893 if (store_mode == OPERATION) {
894 ret = kxcjk1013_set_mode(data, OPERATION);
895 if (ret)
896 return ret;
897 }
898
899 return 0;
900 }
901 }
902
903 return -EINVAL;
904}
905
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000906static int kxcjk1013_read_raw(struct iio_dev *indio_dev,
907 struct iio_chan_spec const *chan, int *val,
908 int *val2, long mask)
909{
910 struct kxcjk1013_data *data = iio_priv(indio_dev);
911 int ret;
912
913 switch (mask) {
914 case IIO_CHAN_INFO_RAW:
915 mutex_lock(&data->mutex);
916 if (iio_buffer_enabled(indio_dev))
917 ret = -EBUSY;
918 else {
Srinivas Pandruvada124e1b1d2014-05-08 22:58:00 +0100919 ret = kxcjk1013_set_power_state(data, true);
Srinivas Pandruvada88f6da72014-06-16 20:00:00 +0100920 if (ret < 0) {
921 mutex_unlock(&data->mutex);
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000922 return ret;
Srinivas Pandruvada88f6da72014-06-16 20:00:00 +0100923 }
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000924 ret = kxcjk1013_get_acc_reg(data, chan->scan_index);
Srinivas Pandruvada124e1b1d2014-05-08 22:58:00 +0100925 if (ret < 0) {
926 kxcjk1013_set_power_state(data, false);
927 mutex_unlock(&data->mutex);
928 return ret;
929 }
Gwendal Grignou91050792021-11-04 01:24:02 -0700930 *val = sign_extend32(ret >> chan->scan_type.shift,
931 chan->scan_type.realbits - 1);
Srinivas Pandruvada124e1b1d2014-05-08 22:58:00 +0100932 ret = kxcjk1013_set_power_state(data, false);
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000933 }
934 mutex_unlock(&data->mutex);
935
936 if (ret < 0)
937 return ret;
938
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000939 return IIO_VAL_INT;
940
941 case IIO_CHAN_INFO_SCALE:
942 *val = 0;
Srinivas Pandruvadaa735e3d2014-05-08 22:58:00 +0100943 *val2 = KXCJK1013_scale_table[data->range].scale;
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000944 return IIO_VAL_INT_PLUS_MICRO;
945
946 case IIO_CHAN_INFO_SAMP_FREQ:
947 mutex_lock(&data->mutex);
948 ret = kxcjk1013_get_odr(data, val, val2);
949 mutex_unlock(&data->mutex);
950 return ret;
951
952 default:
953 return -EINVAL;
954 }
955}
956
957static int kxcjk1013_write_raw(struct iio_dev *indio_dev,
958 struct iio_chan_spec const *chan, int val,
959 int val2, long mask)
960{
961 struct kxcjk1013_data *data = iio_priv(indio_dev);
962 int ret;
963
964 switch (mask) {
965 case IIO_CHAN_INFO_SAMP_FREQ:
966 mutex_lock(&data->mutex);
967 ret = kxcjk1013_set_odr(data, val, val2);
968 mutex_unlock(&data->mutex);
969 break;
Srinivas Pandruvadaa735e3d2014-05-08 22:58:00 +0100970 case IIO_CHAN_INFO_SCALE:
971 if (val)
972 return -EINVAL;
973
974 mutex_lock(&data->mutex);
975 ret = kxcjk1013_set_scale(data, val2);
976 mutex_unlock(&data->mutex);
977 break;
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +0000978 default:
979 ret = -EINVAL;
980 }
981
982 return ret;
983}
984
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +0100985static int kxcjk1013_read_event(struct iio_dev *indio_dev,
986 const struct iio_chan_spec *chan,
987 enum iio_event_type type,
988 enum iio_event_direction dir,
989 enum iio_event_info info,
990 int *val, int *val2)
991{
992 struct kxcjk1013_data *data = iio_priv(indio_dev);
993
994 *val2 = 0;
995 switch (info) {
996 case IIO_EV_INFO_VALUE:
997 *val = data->wake_thres;
998 break;
999 case IIO_EV_INFO_PERIOD:
1000 *val = data->wake_dur;
1001 break;
1002 default:
1003 return -EINVAL;
1004 }
1005
1006 return IIO_VAL_INT;
1007}
1008
1009static int kxcjk1013_write_event(struct iio_dev *indio_dev,
1010 const struct iio_chan_spec *chan,
1011 enum iio_event_type type,
1012 enum iio_event_direction dir,
1013 enum iio_event_info info,
1014 int val, int val2)
1015{
1016 struct kxcjk1013_data *data = iio_priv(indio_dev);
1017
1018 if (data->ev_enable_state)
1019 return -EBUSY;
1020
1021 switch (info) {
1022 case IIO_EV_INFO_VALUE:
1023 data->wake_thres = val;
1024 break;
1025 case IIO_EV_INFO_PERIOD:
1026 data->wake_dur = val;
1027 break;
1028 default:
1029 return -EINVAL;
1030 }
1031
1032 return 0;
1033}
1034
1035static int kxcjk1013_read_event_config(struct iio_dev *indio_dev,
1036 const struct iio_chan_spec *chan,
1037 enum iio_event_type type,
1038 enum iio_event_direction dir)
1039{
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +01001040 struct kxcjk1013_data *data = iio_priv(indio_dev);
1041
1042 return data->ev_enable_state;
1043}
1044
1045static int kxcjk1013_write_event_config(struct iio_dev *indio_dev,
1046 const struct iio_chan_spec *chan,
1047 enum iio_event_type type,
1048 enum iio_event_direction dir,
1049 int state)
1050{
1051 struct kxcjk1013_data *data = iio_priv(indio_dev);
1052 int ret;
1053
1054 if (state && data->ev_enable_state)
1055 return 0;
1056
1057 mutex_lock(&data->mutex);
1058
1059 if (!state && data->motion_trigger_on) {
1060 data->ev_enable_state = 0;
1061 mutex_unlock(&data->mutex);
1062 return 0;
1063 }
1064
1065 /*
1066 * We will expect the enable and disable to do operation in
1067 * in reverse order. This will happen here anyway as our
1068 * resume operation uses sync mode runtime pm calls, the
1069 * suspend operation will be delayed by autosuspend delay
1070 * So the disable operation will still happen in reverse of
1071 * enable operation. When runtime pm is disabled the mode
1072 * is always on so sequence doesn't matter
1073 */
1074 ret = kxcjk1013_set_power_state(data, state);
1075 if (ret < 0) {
1076 mutex_unlock(&data->mutex);
1077 return ret;
1078 }
1079
1080 ret = kxcjk1013_setup_any_motion_interrupt(data, state);
1081 if (ret < 0) {
Irina Tirdeafbd123e2014-12-10 18:23:53 +02001082 kxcjk1013_set_power_state(data, false);
1083 data->ev_enable_state = 0;
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +01001084 mutex_unlock(&data->mutex);
1085 return ret;
1086 }
1087
1088 data->ev_enable_state = state;
1089 mutex_unlock(&data->mutex);
1090
1091 return 0;
1092}
1093
Vlad Dogarua25691c2015-04-03 15:03:02 +03001094static int kxcjk1013_buffer_preenable(struct iio_dev *indio_dev)
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001095{
1096 struct kxcjk1013_data *data = iio_priv(indio_dev);
1097
Vlad Dogarua25691c2015-04-03 15:03:02 +03001098 return kxcjk1013_set_power_state(data, true);
1099}
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001100
Vlad Dogarua25691c2015-04-03 15:03:02 +03001101static int kxcjk1013_buffer_postdisable(struct iio_dev *indio_dev)
1102{
1103 struct kxcjk1013_data *data = iio_priv(indio_dev);
1104
1105 return kxcjk1013_set_power_state(data, false);
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001106}
1107
Michał Mirosław81fa8cb2017-09-17 17:01:03 +02001108static ssize_t kxcjk1013_get_samp_freq_avail(struct device *dev,
1109 struct device_attribute *attr,
1110 char *buf)
1111{
Michał Mirosław1540d012017-09-17 17:01:04 +02001112 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1113 struct kxcjk1013_data *data = iio_priv(indio_dev);
1114 const char *str;
1115
1116 if (data->chipset == KXTF9)
1117 str = kxtf9_samp_freq_avail;
1118 else
1119 str = kxcjk1013_samp_freq_avail;
1120
1121 return sprintf(buf, "%s\n", str);
Michał Mirosław81fa8cb2017-09-17 17:01:03 +02001122}
1123
Michał Mirosławd2ff1952017-09-17 17:01:03 +02001124static IIO_DEVICE_ATTR(in_accel_sampling_frequency_available, S_IRUGO,
1125 kxcjk1013_get_samp_freq_avail, NULL, 0);
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001126
Srinivas Pandruvadaa735e3d2014-05-08 22:58:00 +01001127static IIO_CONST_ATTR(in_accel_scale_available, "0.009582 0.019163 0.038326");
1128
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001129static struct attribute *kxcjk1013_attributes[] = {
Michał Mirosławd2ff1952017-09-17 17:01:03 +02001130 &iio_dev_attr_in_accel_sampling_frequency_available.dev_attr.attr,
Srinivas Pandruvadaa735e3d2014-05-08 22:58:00 +01001131 &iio_const_attr_in_accel_scale_available.dev_attr.attr,
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001132 NULL,
1133};
1134
1135static const struct attribute_group kxcjk1013_attrs_group = {
1136 .attrs = kxcjk1013_attributes,
1137};
1138
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +01001139static const struct iio_event_spec kxcjk1013_event = {
1140 .type = IIO_EV_TYPE_THRESH,
Daniel Baluta25afffe2014-10-10 15:53:00 +01001141 .dir = IIO_EV_DIR_EITHER,
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +01001142 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
1143 BIT(IIO_EV_INFO_ENABLE) |
1144 BIT(IIO_EV_INFO_PERIOD)
1145};
1146
Dmitry Osipenko1bde3302020-01-12 23:33:00 +03001147static const struct iio_mount_matrix *
1148kxcjk1013_get_mount_matrix(const struct iio_dev *indio_dev,
1149 const struct iio_chan_spec *chan)
1150{
1151 struct kxcjk1013_data *data = iio_priv(indio_dev);
1152
1153 return &data->orientation;
1154}
1155
1156static const struct iio_chan_spec_ext_info kxcjk1013_ext_info[] = {
1157 IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE, kxcjk1013_get_mount_matrix),
1158 { }
1159};
1160
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001161#define KXCJK1013_CHANNEL(_axis) { \
1162 .type = IIO_ACCEL, \
1163 .modified = 1, \
1164 .channel2 = IIO_MOD_##_axis, \
1165 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
1166 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
1167 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
1168 .scan_index = AXIS_##_axis, \
1169 .scan_type = { \
1170 .sign = 's', \
1171 .realbits = 12, \
1172 .storagebits = 16, \
1173 .shift = 4, \
Adriana Reus65ae47b2016-03-24 11:29:31 +02001174 .endianness = IIO_LE, \
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001175 }, \
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +01001176 .event_spec = &kxcjk1013_event, \
Dmitry Osipenko1bde3302020-01-12 23:33:00 +03001177 .ext_info = kxcjk1013_ext_info, \
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +01001178 .num_event_specs = 1 \
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001179}
1180
1181static const struct iio_chan_spec kxcjk1013_channels[] = {
1182 KXCJK1013_CHANNEL(X),
1183 KXCJK1013_CHANNEL(Y),
1184 KXCJK1013_CHANNEL(Z),
1185 IIO_CHAN_SOFT_TIMESTAMP(3),
1186};
1187
Vlad Dogarua25691c2015-04-03 15:03:02 +03001188static const struct iio_buffer_setup_ops kxcjk1013_buffer_setup_ops = {
1189 .preenable = kxcjk1013_buffer_preenable,
Vlad Dogarua25691c2015-04-03 15:03:02 +03001190 .postdisable = kxcjk1013_buffer_postdisable,
Vlad Dogarua25691c2015-04-03 15:03:02 +03001191};
1192
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001193static const struct iio_info kxcjk1013_info = {
1194 .attrs = &kxcjk1013_attrs_group,
1195 .read_raw = kxcjk1013_read_raw,
1196 .write_raw = kxcjk1013_write_raw,
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +01001197 .read_event_value = kxcjk1013_read_event,
1198 .write_event_value = kxcjk1013_write_event,
1199 .write_event_config = kxcjk1013_write_event_config,
1200 .read_event_config = kxcjk1013_read_event_config,
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001201};
1202
Adriana Reus09cf1b32016-03-24 11:29:30 +02001203static const unsigned long kxcjk1013_scan_masks[] = {0x7, 0};
1204
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001205static irqreturn_t kxcjk1013_trigger_handler(int irq, void *p)
1206{
1207 struct iio_poll_func *pf = p;
1208 struct iio_dev *indio_dev = pf->indio_dev;
1209 struct kxcjk1013_data *data = iio_priv(indio_dev);
Adriana Reus65ae47b2016-03-24 11:29:31 +02001210 int ret;
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001211
1212 mutex_lock(&data->mutex);
Adriana Reus65ae47b2016-03-24 11:29:31 +02001213 ret = i2c_smbus_read_i2c_block_data_or_emulated(data->client,
1214 KXCJK1013_REG_XOUT_L,
1215 AXIS_MAX * 2,
Jonathan Cameron3ab3aa22021-05-01 18:01:06 +01001216 (u8 *)data->scan.chans);
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001217 mutex_unlock(&data->mutex);
Adriana Reus65ae47b2016-03-24 11:29:31 +02001218 if (ret < 0)
1219 goto err;
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001220
Jonathan Cameron3ab3aa22021-05-01 18:01:06 +01001221 iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +01001222 data->timestamp);
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001223err:
1224 iio_trigger_notify_done(indio_dev->trig);
1225
1226 return IRQ_HANDLED;
1227}
1228
Jonathan Cameroneca85232020-09-20 14:25:48 +01001229static void kxcjk1013_trig_reen(struct iio_trigger *trig)
Srinivas Pandruvada59bfeab2014-07-17 01:42:00 +01001230{
1231 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
1232 struct kxcjk1013_data *data = iio_priv(indio_dev);
1233 int ret;
1234
Stephan Gerholdae4467f2021-05-11 16:24:01 +02001235 ret = i2c_smbus_read_byte_data(data->client, data->regs->int_rel);
Jonathan Cameroneca85232020-09-20 14:25:48 +01001236 if (ret < 0)
Srinivas Pandruvada59bfeab2014-07-17 01:42:00 +01001237 dev_err(&data->client->dev, "Error reading reg_int_rel\n");
Srinivas Pandruvada59bfeab2014-07-17 01:42:00 +01001238}
1239
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001240static int kxcjk1013_data_rdy_trigger_set_state(struct iio_trigger *trig,
1241 bool state)
1242{
1243 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
1244 struct kxcjk1013_data *data = iio_priv(indio_dev);
Srinivas Pandruvada124e1b1d2014-05-08 22:58:00 +01001245 int ret;
1246
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001247 mutex_lock(&data->mutex);
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +01001248
1249 if (!state && data->ev_enable_state && data->motion_trigger_on) {
1250 data->motion_trigger_on = false;
1251 mutex_unlock(&data->mutex);
1252 return 0;
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001253 }
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +01001254
1255 ret = kxcjk1013_set_power_state(data, state);
1256 if (ret < 0) {
1257 mutex_unlock(&data->mutex);
1258 return ret;
1259 }
1260 if (data->motion_trig == trig)
1261 ret = kxcjk1013_setup_any_motion_interrupt(data, state);
1262 else
1263 ret = kxcjk1013_setup_new_data_interrupt(data, state);
1264 if (ret < 0) {
Irina Tirdeafbd123e2014-12-10 18:23:53 +02001265 kxcjk1013_set_power_state(data, false);
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +01001266 mutex_unlock(&data->mutex);
1267 return ret;
1268 }
1269 if (data->motion_trig == trig)
1270 data->motion_trigger_on = state;
1271 else
1272 data->dready_trigger_on = state;
1273
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001274 mutex_unlock(&data->mutex);
1275
1276 return 0;
1277}
1278
1279static const struct iio_trigger_ops kxcjk1013_trigger_ops = {
1280 .set_trigger_state = kxcjk1013_data_rdy_trigger_set_state,
Jonathan Cameroneca85232020-09-20 14:25:48 +01001281 .reenable = kxcjk1013_trig_reen,
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001282};
1283
Michał Mirosławd8408e92017-09-17 17:01:02 +02001284static void kxcjk1013_report_motion_event(struct iio_dev *indio_dev)
1285{
1286 struct kxcjk1013_data *data = iio_priv(indio_dev);
1287
Stephan Gerholdae4467f2021-05-11 16:24:01 +02001288 int ret = i2c_smbus_read_byte_data(data->client, data->regs->int_src2);
Michał Mirosławd8408e92017-09-17 17:01:02 +02001289 if (ret < 0) {
1290 dev_err(&data->client->dev, "Error reading reg_int_src2\n");
1291 return;
1292 }
1293
1294 if (ret & KXCJK1013_REG_INT_SRC2_BIT_XN)
1295 iio_push_event(indio_dev,
1296 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1297 0,
1298 IIO_MOD_X,
1299 IIO_EV_TYPE_THRESH,
1300 IIO_EV_DIR_FALLING),
1301 data->timestamp);
1302
1303 if (ret & KXCJK1013_REG_INT_SRC2_BIT_XP)
1304 iio_push_event(indio_dev,
1305 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1306 0,
1307 IIO_MOD_X,
1308 IIO_EV_TYPE_THRESH,
1309 IIO_EV_DIR_RISING),
1310 data->timestamp);
1311
1312 if (ret & KXCJK1013_REG_INT_SRC2_BIT_YN)
1313 iio_push_event(indio_dev,
1314 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1315 0,
1316 IIO_MOD_Y,
1317 IIO_EV_TYPE_THRESH,
1318 IIO_EV_DIR_FALLING),
1319 data->timestamp);
1320
1321 if (ret & KXCJK1013_REG_INT_SRC2_BIT_YP)
1322 iio_push_event(indio_dev,
1323 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1324 0,
1325 IIO_MOD_Y,
1326 IIO_EV_TYPE_THRESH,
1327 IIO_EV_DIR_RISING),
1328 data->timestamp);
1329
1330 if (ret & KXCJK1013_REG_INT_SRC2_BIT_ZN)
1331 iio_push_event(indio_dev,
1332 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1333 0,
1334 IIO_MOD_Z,
1335 IIO_EV_TYPE_THRESH,
1336 IIO_EV_DIR_FALLING),
1337 data->timestamp);
1338
1339 if (ret & KXCJK1013_REG_INT_SRC2_BIT_ZP)
1340 iio_push_event(indio_dev,
1341 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1342 0,
1343 IIO_MOD_Z,
1344 IIO_EV_TYPE_THRESH,
1345 IIO_EV_DIR_RISING),
1346 data->timestamp);
1347}
1348
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +01001349static irqreturn_t kxcjk1013_event_handler(int irq, void *private)
1350{
1351 struct iio_dev *indio_dev = private;
1352 struct kxcjk1013_data *data = iio_priv(indio_dev);
1353 int ret;
1354
Stephan Gerholdae4467f2021-05-11 16:24:01 +02001355 ret = i2c_smbus_read_byte_data(data->client, data->regs->int_src1);
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +01001356 if (ret < 0) {
1357 dev_err(&data->client->dev, "Error reading reg_int_src1\n");
1358 goto ack_intr;
1359 }
1360
Michał Mirosławf74e4402017-09-17 17:01:02 +02001361 if (ret & KXCJK1013_REG_INT_SRC1_BIT_WUFS) {
Michał Mirosław1540d012017-09-17 17:01:04 +02001362 if (data->chipset == KXTF9)
1363 iio_push_event(indio_dev,
1364 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1365 0,
1366 IIO_MOD_X_AND_Y_AND_Z,
1367 IIO_EV_TYPE_THRESH,
1368 IIO_EV_DIR_RISING),
1369 data->timestamp);
1370 else
1371 kxcjk1013_report_motion_event(indio_dev);
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +01001372 }
1373
1374ack_intr:
1375 if (data->dready_trigger_on)
1376 return IRQ_HANDLED;
1377
Stephan Gerholdae4467f2021-05-11 16:24:01 +02001378 ret = i2c_smbus_read_byte_data(data->client, data->regs->int_rel);
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +01001379 if (ret < 0)
1380 dev_err(&data->client->dev, "Error reading reg_int_rel\n");
1381
1382 return IRQ_HANDLED;
1383}
1384
1385static irqreturn_t kxcjk1013_data_rdy_trig_poll(int irq, void *private)
1386{
1387 struct iio_dev *indio_dev = private;
1388 struct kxcjk1013_data *data = iio_priv(indio_dev);
1389
Gregor Boiriebc2b7da2016-03-09 19:05:49 +01001390 data->timestamp = iio_get_time_ns(indio_dev);
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +01001391
1392 if (data->dready_trigger_on)
1393 iio_trigger_poll(data->dready_trig);
1394 else if (data->motion_trigger_on)
1395 iio_trigger_poll(data->motion_trig);
1396
1397 if (data->ev_enable_state)
1398 return IRQ_WAKE_THREAD;
1399 else
1400 return IRQ_HANDLED;
1401}
1402
Daniel Balutac6861372014-03-09 08:33:00 +00001403static const char *kxcjk1013_match_acpi_device(struct device *dev,
Bastien Nocera3bfa74f2014-05-11 22:09:00 +01001404 enum kx_chipset *chipset,
Hans de Goede30132fe2021-02-07 17:09:01 +01001405 enum kx_acpi_type *acpi_type,
1406 const char **label)
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001407{
1408 const struct acpi_device_id *id;
Roberta Dobrescub0868df2014-12-30 20:57:53 +02001409
Daniel Balutac6861372014-03-09 08:33:00 +00001410 id = acpi_match_device(dev->driver->acpi_match_table, dev);
1411 if (!id)
1412 return NULL;
Antonio Ospitee693e152015-05-04 11:13:04 +02001413
Hans de Goede30132fe2021-02-07 17:09:01 +01001414 if (strcmp(id->id, "SMO8500") == 0) {
Hans de Goede11e94f22020-11-10 14:38:34 +01001415 *acpi_type = ACPI_SMO8500;
Hans de Goede30132fe2021-02-07 17:09:01 +01001416 } else if (strcmp(id->id, "KIOX010A") == 0) {
Hans de Goedee5b10322020-11-10 14:38:35 +01001417 *acpi_type = ACPI_KIOX010A;
Hans de Goede30132fe2021-02-07 17:09:01 +01001418 *label = "accel-display";
1419 } else if (strcmp(id->id, "KIOX020A") == 0) {
1420 *label = "accel-base";
1421 }
Antonio Ospitee693e152015-05-04 11:13:04 +02001422
Daniel Balutac6861372014-03-09 08:33:00 +00001423 *chipset = (enum kx_chipset)id->driver_data;
1424
1425 return dev_name(dev);
1426}
1427
Devajith V S1d2e91a2020-12-13 22:54:36 +05301428static void kxcjk1013_disable_regulators(void *d)
1429{
1430 struct kxcjk1013_data *data = d;
1431
1432 regulator_bulk_disable(ARRAY_SIZE(data->regulators), data->regulators);
1433}
1434
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001435static int kxcjk1013_probe(struct i2c_client *client,
1436 const struct i2c_device_id *id)
1437{
1438 struct kxcjk1013_data *data;
1439 struct iio_dev *indio_dev;
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001440 struct kxcjk_1013_platform_data *pdata;
Daniel Balutac6861372014-03-09 08:33:00 +00001441 const char *name;
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001442 int ret;
1443
1444 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
1445 if (!indio_dev)
1446 return -ENOMEM;
1447
1448 data = iio_priv(indio_dev);
1449 i2c_set_clientdata(client, indio_dev);
1450 data->client = client;
1451
1452 pdata = dev_get_platdata(&client->dev);
Dmitry Osipenko1bde3302020-01-12 23:33:00 +03001453 if (pdata) {
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001454 data->active_high_intr = pdata->active_high_intr;
Dmitry Osipenko1bde3302020-01-12 23:33:00 +03001455 data->orientation = pdata->orientation;
1456 } else {
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001457 data->active_high_intr = true; /* default polarity */
1458
Andy Shevchenkob8927702021-05-18 14:25:46 +03001459 ret = iio_read_mount_matrix(&client->dev, &data->orientation);
Dmitry Osipenko1bde3302020-01-12 23:33:00 +03001460 if (ret)
1461 return ret;
1462 }
1463
Devajith V S1d2e91a2020-12-13 22:54:36 +05301464 data->regulators[0].supply = "vdd";
1465 data->regulators[1].supply = "vddio";
1466 ret = devm_regulator_bulk_get(&client->dev, ARRAY_SIZE(data->regulators),
1467 data->regulators);
1468 if (ret)
1469 return dev_err_probe(&client->dev, ret, "Failed to get regulators\n");
1470
1471 ret = regulator_bulk_enable(ARRAY_SIZE(data->regulators),
1472 data->regulators);
1473 if (ret)
1474 return ret;
1475
1476 ret = devm_add_action_or_reset(&client->dev, kxcjk1013_disable_regulators, data);
1477 if (ret)
1478 return ret;
1479
1480 /*
1481 * A typical delay of 10ms is required for powering up
1482 * according to the data sheets of supported chips.
1483 * Hence double that to play safe.
1484 */
1485 msleep(20);
1486
Daniel Balutac6861372014-03-09 08:33:00 +00001487 if (id) {
1488 data->chipset = (enum kx_chipset)(id->driver_data);
1489 name = id->name;
1490 } else if (ACPI_HANDLE(&client->dev)) {
1491 name = kxcjk1013_match_acpi_device(&client->dev,
Bastien Nocera3bfa74f2014-05-11 22:09:00 +01001492 &data->chipset,
Hans de Goede30132fe2021-02-07 17:09:01 +01001493 &data->acpi_type,
1494 &indio_dev->label);
Daniel Balutac6861372014-03-09 08:33:00 +00001495 } else
1496 return -ENODEV;
1497
Stephan Gerholdae4467f2021-05-11 16:24:01 +02001498 switch (data->chipset) {
1499 case KXCJK1013:
1500 case KXCJ91008:
1501 case KXTJ21009:
1502 data->regs = &kxcjk1013_regs;
1503 break;
1504 case KXTF9:
1505 data->regs = &kxtf9_regs;
1506 break;
Stephan Gerholdc630c172021-05-11 16:24:02 +02001507 case KX0231025:
1508 data->regs = &kx0231025_regs;
1509 break;
Stephan Gerholdae4467f2021-05-11 16:24:01 +02001510 default:
1511 return -EINVAL;
1512 }
1513
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001514 ret = kxcjk1013_chip_init(data);
1515 if (ret < 0)
1516 return ret;
1517
1518 mutex_init(&data->mutex);
1519
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001520 indio_dev->channels = kxcjk1013_channels;
1521 indio_dev->num_channels = ARRAY_SIZE(kxcjk1013_channels);
Adriana Reus09cf1b32016-03-24 11:29:30 +02001522 indio_dev->available_scan_masks = kxcjk1013_scan_masks;
Daniel Balutac6861372014-03-09 08:33:00 +00001523 indio_dev->name = name;
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001524 indio_dev->modes = INDIO_DIRECT_MODE;
1525 indio_dev->info = &kxcjk1013_info;
1526
Hans de Goede11e94f22020-11-10 14:38:34 +01001527 if (client->irq > 0 && data->acpi_type != ACPI_SMO8500) {
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +01001528 ret = devm_request_threaded_irq(&client->dev, client->irq,
1529 kxcjk1013_data_rdy_trig_poll,
1530 kxcjk1013_event_handler,
1531 IRQF_TRIGGER_RISING,
1532 KXCJK1013_IRQ_NAME,
1533 indio_dev);
1534 if (ret)
Irina Tirdea9d02daf732014-12-10 18:23:54 +02001535 goto err_poweroff;
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +01001536
1537 data->dready_trig = devm_iio_trigger_alloc(&client->dev,
1538 "%s-dev%d",
1539 indio_dev->name,
Jonathan Cameron15ea28782021-04-26 18:49:03 +01001540 iio_device_id(indio_dev));
Irina Tirdea9d02daf732014-12-10 18:23:54 +02001541 if (!data->dready_trig) {
1542 ret = -ENOMEM;
1543 goto err_poweroff;
1544 }
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001545
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +01001546 data->motion_trig = devm_iio_trigger_alloc(&client->dev,
1547 "%s-any-motion-dev%d",
1548 indio_dev->name,
Jonathan Cameron15ea28782021-04-26 18:49:03 +01001549 iio_device_id(indio_dev));
Irina Tirdea9d02daf732014-12-10 18:23:54 +02001550 if (!data->motion_trig) {
1551 ret = -ENOMEM;
1552 goto err_poweroff;
1553 }
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001554
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +01001555 data->dready_trig->ops = &kxcjk1013_trigger_ops;
1556 iio_trigger_set_drvdata(data->dready_trig, indio_dev);
1557 indio_dev->trig = data->dready_trig;
Srinivas Pandruvadac1288b82014-07-17 01:42:00 +01001558 iio_trigger_get(indio_dev->trig);
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +01001559 ret = iio_trigger_register(data->dready_trig);
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001560 if (ret)
Irina Tirdea9d02daf732014-12-10 18:23:54 +02001561 goto err_poweroff;
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +01001562
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +01001563 data->motion_trig->ops = &kxcjk1013_trigger_ops;
1564 iio_trigger_set_drvdata(data->motion_trig, indio_dev);
1565 ret = iio_trigger_register(data->motion_trig);
1566 if (ret) {
1567 data->motion_trig = NULL;
1568 goto err_trigger_unregister;
1569 }
Vlad Dogarua25691c2015-04-03 15:03:02 +03001570 }
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001571
Vlad Dogarua25691c2015-04-03 15:03:02 +03001572 ret = iio_triggered_buffer_setup(indio_dev,
1573 &iio_pollfunc_store_time,
1574 kxcjk1013_trigger_handler,
1575 &kxcjk1013_buffer_setup_ops);
1576 if (ret < 0) {
1577 dev_err(&client->dev, "iio triggered buffer setup failed\n");
1578 goto err_trigger_unregister;
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001579 }
1580
Srinivas Pandruvada124e1b1d2014-05-08 22:58:00 +01001581 ret = pm_runtime_set_active(&client->dev);
1582 if (ret)
Adriana Reus7d0ead52015-11-05 16:25:29 +02001583 goto err_buffer_cleanup;
Srinivas Pandruvada124e1b1d2014-05-08 22:58:00 +01001584
1585 pm_runtime_enable(&client->dev);
1586 pm_runtime_set_autosuspend_delay(&client->dev,
1587 KXCJK1013_SLEEP_DELAY_MS);
1588 pm_runtime_use_autosuspend(&client->dev);
1589
Adriana Reus7d0ead52015-11-05 16:25:29 +02001590 ret = iio_device_register(indio_dev);
1591 if (ret < 0) {
1592 dev_err(&client->dev, "unable to register iio device\n");
1593 goto err_buffer_cleanup;
1594 }
1595
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001596 return 0;
1597
1598err_buffer_cleanup:
Yang Yingliang70c97742021-10-25 20:41:59 +08001599 iio_triggered_buffer_cleanup(indio_dev);
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001600err_trigger_unregister:
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +01001601 if (data->dready_trig)
1602 iio_trigger_unregister(data->dready_trig);
1603 if (data->motion_trig)
1604 iio_trigger_unregister(data->motion_trig);
Irina Tirdea9d02daf732014-12-10 18:23:54 +02001605err_poweroff:
1606 kxcjk1013_set_mode(data, STANDBY);
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001607
1608 return ret;
1609}
1610
1611static int kxcjk1013_remove(struct i2c_client *client)
1612{
1613 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1614 struct kxcjk1013_data *data = iio_priv(indio_dev);
1615
Adriana Reus7d0ead52015-11-05 16:25:29 +02001616 iio_device_unregister(indio_dev);
1617
Srinivas Pandruvada124e1b1d2014-05-08 22:58:00 +01001618 pm_runtime_disable(&client->dev);
1619 pm_runtime_set_suspended(&client->dev);
Srinivas Pandruvada124e1b1d2014-05-08 22:58:00 +01001620
Yang Yingliang70c97742021-10-25 20:41:59 +08001621 iio_triggered_buffer_cleanup(indio_dev);
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +01001622 if (data->dready_trig) {
Srinivas Pandruvadab4b491c2014-08-22 20:01:00 +01001623 iio_trigger_unregister(data->dready_trig);
1624 iio_trigger_unregister(data->motion_trig);
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001625 }
1626
1627 mutex_lock(&data->mutex);
1628 kxcjk1013_set_mode(data, STANDBY);
1629 mutex_unlock(&data->mutex);
1630
1631 return 0;
1632}
1633
1634#ifdef CONFIG_PM_SLEEP
1635static int kxcjk1013_suspend(struct device *dev)
1636{
1637 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1638 struct kxcjk1013_data *data = iio_priv(indio_dev);
Srinivas Pandruvada124e1b1d2014-05-08 22:58:00 +01001639 int ret;
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001640
1641 mutex_lock(&data->mutex);
Srinivas Pandruvada124e1b1d2014-05-08 22:58:00 +01001642 ret = kxcjk1013_set_mode(data, STANDBY);
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001643 mutex_unlock(&data->mutex);
1644
Srinivas Pandruvada124e1b1d2014-05-08 22:58:00 +01001645 return ret;
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001646}
1647
1648static int kxcjk1013_resume(struct device *dev)
1649{
1650 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1651 struct kxcjk1013_data *data = iio_priv(indio_dev);
Srinivas Pandruvada124e1b1d2014-05-08 22:58:00 +01001652 int ret = 0;
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001653
1654 mutex_lock(&data->mutex);
Irina Tirdead3653d02014-12-06 00:18:07 +02001655 ret = kxcjk1013_set_mode(data, OPERATION);
he, bofe2d3df2019-03-06 10:32:20 +08001656 if (ret == 0)
1657 ret = kxcjk1013_set_range(data, data->range);
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001658 mutex_unlock(&data->mutex);
1659
Srinivas Pandruvada124e1b1d2014-05-08 22:58:00 +01001660 return ret;
1661}
1662#endif
1663
Rafael J. Wysocki6f0a13f2014-12-04 01:08:13 +01001664#ifdef CONFIG_PM
Srinivas Pandruvada124e1b1d2014-05-08 22:58:00 +01001665static int kxcjk1013_runtime_suspend(struct device *dev)
1666{
1667 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1668 struct kxcjk1013_data *data = iio_priv(indio_dev);
Irina Tirdeafbd123e2014-12-10 18:23:53 +02001669 int ret;
Srinivas Pandruvada124e1b1d2014-05-08 22:58:00 +01001670
Irina Tirdeafbd123e2014-12-10 18:23:53 +02001671 ret = kxcjk1013_set_mode(data, STANDBY);
1672 if (ret < 0) {
1673 dev_err(&data->client->dev, "powering off device failed\n");
1674 return -EAGAIN;
1675 }
1676 return 0;
Srinivas Pandruvada124e1b1d2014-05-08 22:58:00 +01001677}
1678
1679static int kxcjk1013_runtime_resume(struct device *dev)
1680{
1681 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1682 struct kxcjk1013_data *data = iio_priv(indio_dev);
1683 int ret;
1684 int sleep_val;
1685
1686 ret = kxcjk1013_set_mode(data, OPERATION);
1687 if (ret < 0)
1688 return ret;
1689
1690 sleep_val = kxcjk1013_get_startup_times(data);
1691 if (sleep_val < 20000)
1692 usleep_range(sleep_val, 20000);
1693 else
1694 msleep_interruptible(sleep_val/1000);
1695
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001696 return 0;
1697}
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001698#endif
1699
Srinivas Pandruvada124e1b1d2014-05-08 22:58:00 +01001700static const struct dev_pm_ops kxcjk1013_pm_ops = {
1701 SET_SYSTEM_SLEEP_PM_OPS(kxcjk1013_suspend, kxcjk1013_resume)
1702 SET_RUNTIME_PM_OPS(kxcjk1013_runtime_suspend,
1703 kxcjk1013_runtime_resume, NULL)
1704};
1705
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001706static const struct acpi_device_id kx_acpi_match[] = {
Daniel Balutac6861372014-03-09 08:33:00 +00001707 {"KXCJ1013", KXCJK1013},
1708 {"KXCJ1008", KXCJ91008},
Antonio Ospite61e2c702015-05-04 11:13:03 +02001709 {"KXCJ9000", KXCJ91008},
Valentin Manea45e119e2019-03-31 14:54:08 +00001710 {"KIOX0008", KXCJ91008},
Hans de Goede047a6f72018-10-12 12:17:50 +02001711 {"KIOX0009", KXTJ21009},
Christophe Chapuis4d19c482016-07-17 10:15:15 +02001712 {"KIOX000A", KXCJ91008},
Hans de Goedec0af3b62019-08-18 13:23:06 +02001713 {"KIOX010A", KXCJ91008}, /* KXCJ91008 in the display of a yoga 2-in-1 */
1714 {"KIOX020A", KXCJ91008}, /* KXCJ91008 in the base of a yoga 2-in-1 */
Daniel Balutac6861372014-03-09 08:33:00 +00001715 {"KXTJ1009", KXTJ21009},
Stephan Gerholdb911b0a2018-11-24 17:22:20 +01001716 {"KXJ2109", KXTJ21009},
Bastien Nocera3bfa74f2014-05-11 22:09:00 +01001717 {"SMO8500", KXCJ91008},
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001718 { },
1719};
1720MODULE_DEVICE_TABLE(acpi, kx_acpi_match);
1721
1722static const struct i2c_device_id kxcjk1013_id[] = {
Daniel Balutac6861372014-03-09 08:33:00 +00001723 {"kxcjk1013", KXCJK1013},
1724 {"kxcj91008", KXCJ91008},
1725 {"kxtj21009", KXTJ21009},
Michał Mirosław1540d012017-09-17 17:01:04 +02001726 {"kxtf9", KXTF9},
Stephan Gerholdc630c172021-05-11 16:24:02 +02001727 {"kx023-1025", KX0231025},
Bastien Nocera3bfa74f2014-05-11 22:09:00 +01001728 {"SMO8500", KXCJ91008},
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001729 {}
1730};
1731
1732MODULE_DEVICE_TABLE(i2c, kxcjk1013_id);
1733
Robert Yang3b866fd2019-03-04 10:19:12 -05001734static const struct of_device_id kxcjk1013_of_match[] = {
1735 { .compatible = "kionix,kxcjk1013", },
1736 { .compatible = "kionix,kxcj91008", },
1737 { .compatible = "kionix,kxtj21009", },
1738 { .compatible = "kionix,kxtf9", },
Stephan Gerholdc630c172021-05-11 16:24:02 +02001739 { .compatible = "kionix,kx023-1025", },
Robert Yang3b866fd2019-03-04 10:19:12 -05001740 { }
1741};
1742MODULE_DEVICE_TABLE(of, kxcjk1013_of_match);
1743
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001744static struct i2c_driver kxcjk1013_driver = {
1745 .driver = {
1746 .name = KXCJK1013_DRV_NAME,
1747 .acpi_match_table = ACPI_PTR(kx_acpi_match),
Robert Yang3b866fd2019-03-04 10:19:12 -05001748 .of_match_table = kxcjk1013_of_match,
Srinivas Pandruvada124e1b1d2014-05-08 22:58:00 +01001749 .pm = &kxcjk1013_pm_ops,
Srinivas Pandruvada1a4fbf62014-11-06 23:07:00 +00001750 },
1751 .probe = kxcjk1013_probe,
1752 .remove = kxcjk1013_remove,
1753 .id_table = kxcjk1013_id,
1754};
1755module_i2c_driver(kxcjk1013_driver);
1756
1757MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
1758MODULE_LICENSE("GPL v2");
1759MODULE_DESCRIPTION("KXCJK1013 accelerometer driver");