1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2011 Kionix, Inc.
4 * Written by Chris Hudson <chudson@kionix.com>
7 #include <linux/delay.h>
9 #include <linux/input.h>
10 #include <linux/interrupt.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/input/kxtj9.h>
17 /* OUTPUT REGISTERS */
20 /* CONTROL REGISTERS */
22 #define CTRL_REG1 0x1B
23 #define INT_CTRL1 0x1E
24 #define DATA_CTRL 0x21
25 /* CONTROL REGISTER 1 BITS */
27 #define PC1_ON (1 << 7)
28 /* Data ready function enable bit: set during probe if using irq mode */
29 #define DRDYE (1 << 5)
30 /* DATA CONTROL REGISTER BITS */
38 /* INTERRUPT CONTROL REGISTER 1 BITS */
39 /* Set these during probe if using irq mode */
40 #define KXTJ9_IEL (1 << 3)
41 #define KXTJ9_IEA (1 << 4)
42 #define KXTJ9_IEN (1 << 5)
43 /* INPUT_ABS CONSTANTS */
46 /* RESUME STATE INDICES */
47 #define RES_DATA_CTRL 0
48 #define RES_CTRL_REG1 1
49 #define RES_INT_CTRL1 2
50 #define RESUME_ENTRIES 3
53 * The following table lists the maximum appropriate poll interval for each
54 * available output data rate.
59 } kxtj9_odr_table
[] = {
70 struct i2c_client
*client
;
71 struct kxtj9_platform_data pdata
;
72 struct input_dev
*input_dev
;
73 unsigned int last_poll_interval
;
80 static int kxtj9_i2c_read(struct kxtj9_data
*tj9
, u8 addr
, u8
*data
, int len
)
82 struct i2c_msg msgs
[] = {
84 .addr
= tj9
->client
->addr
,
85 .flags
= tj9
->client
->flags
,
90 .addr
= tj9
->client
->addr
,
91 .flags
= tj9
->client
->flags
| I2C_M_RD
,
97 return i2c_transfer(tj9
->client
->adapter
, msgs
, 2);
100 static void kxtj9_report_acceleration_data(struct kxtj9_data
*tj9
)
102 s16 acc_data
[3]; /* Data bytes from hardware xL, xH, yL, yH, zL, zH */
106 err
= kxtj9_i2c_read(tj9
, XOUT_L
, (u8
*)acc_data
, 6);
108 dev_err(&tj9
->client
->dev
, "accelerometer data read failed\n");
110 x
= le16_to_cpu(acc_data
[tj9
->pdata
.axis_map_x
]);
111 y
= le16_to_cpu(acc_data
[tj9
->pdata
.axis_map_y
]);
112 z
= le16_to_cpu(acc_data
[tj9
->pdata
.axis_map_z
]);
118 input_report_abs(tj9
->input_dev
, ABS_X
, tj9
->pdata
.negate_x
? -x
: x
);
119 input_report_abs(tj9
->input_dev
, ABS_Y
, tj9
->pdata
.negate_y
? -y
: y
);
120 input_report_abs(tj9
->input_dev
, ABS_Z
, tj9
->pdata
.negate_z
? -z
: z
);
121 input_sync(tj9
->input_dev
);
124 static irqreturn_t
kxtj9_isr(int irq
, void *dev
)
126 struct kxtj9_data
*tj9
= dev
;
129 /* data ready is the only possible interrupt type */
130 kxtj9_report_acceleration_data(tj9
);
132 err
= i2c_smbus_read_byte_data(tj9
->client
, INT_REL
);
134 dev_err(&tj9
->client
->dev
,
135 "error clearing interrupt status: %d\n", err
);
140 static int kxtj9_update_g_range(struct kxtj9_data
*tj9
, u8 new_g_range
)
142 switch (new_g_range
) {
156 tj9
->ctrl_reg1
&= 0xe7;
157 tj9
->ctrl_reg1
|= new_g_range
;
162 static int kxtj9_update_odr(struct kxtj9_data
*tj9
, unsigned int poll_interval
)
167 /* Use the lowest ODR that can support the requested poll interval */
168 for (i
= 0; i
< ARRAY_SIZE(kxtj9_odr_table
); i
++) {
169 tj9
->data_ctrl
= kxtj9_odr_table
[i
].mask
;
170 if (poll_interval
< kxtj9_odr_table
[i
].cutoff
)
174 err
= i2c_smbus_write_byte_data(tj9
->client
, CTRL_REG1
, 0);
178 err
= i2c_smbus_write_byte_data(tj9
->client
, DATA_CTRL
, tj9
->data_ctrl
);
182 err
= i2c_smbus_write_byte_data(tj9
->client
, CTRL_REG1
, tj9
->ctrl_reg1
);
189 static int kxtj9_device_power_on(struct kxtj9_data
*tj9
)
191 if (tj9
->pdata
.power_on
)
192 return tj9
->pdata
.power_on();
197 static void kxtj9_device_power_off(struct kxtj9_data
*tj9
)
201 tj9
->ctrl_reg1
&= PC1_OFF
;
202 err
= i2c_smbus_write_byte_data(tj9
->client
, CTRL_REG1
, tj9
->ctrl_reg1
);
204 dev_err(&tj9
->client
->dev
, "soft power off failed\n");
206 if (tj9
->pdata
.power_off
)
207 tj9
->pdata
.power_off();
210 static int kxtj9_enable(struct kxtj9_data
*tj9
)
214 err
= kxtj9_device_power_on(tj9
);
218 /* ensure that PC1 is cleared before updating control registers */
219 err
= i2c_smbus_write_byte_data(tj9
->client
, CTRL_REG1
, 0);
223 /* only write INT_CTRL_REG1 if in irq mode */
224 if (tj9
->client
->irq
) {
225 err
= i2c_smbus_write_byte_data(tj9
->client
,
226 INT_CTRL1
, tj9
->int_ctrl
);
231 err
= kxtj9_update_g_range(tj9
, tj9
->pdata
.g_range
);
235 /* turn on outputs */
236 tj9
->ctrl_reg1
|= PC1_ON
;
237 err
= i2c_smbus_write_byte_data(tj9
->client
, CTRL_REG1
, tj9
->ctrl_reg1
);
241 err
= kxtj9_update_odr(tj9
, tj9
->last_poll_interval
);
245 /* clear initial interrupt if in irq mode */
246 if (tj9
->client
->irq
) {
247 err
= i2c_smbus_read_byte_data(tj9
->client
, INT_REL
);
249 dev_err(&tj9
->client
->dev
,
250 "error clearing interrupt: %d\n", err
);
258 kxtj9_device_power_off(tj9
);
262 static void kxtj9_disable(struct kxtj9_data
*tj9
)
264 kxtj9_device_power_off(tj9
);
267 static int kxtj9_input_open(struct input_dev
*input
)
269 struct kxtj9_data
*tj9
= input_get_drvdata(input
);
271 return kxtj9_enable(tj9
);
274 static void kxtj9_input_close(struct input_dev
*dev
)
276 struct kxtj9_data
*tj9
= input_get_drvdata(dev
);
282 * When IRQ mode is selected, we need to provide an interface to allow the user
283 * to change the output data rate of the part. For consistency, we are using
284 * the set_poll method, which accepts a poll interval in milliseconds, and then
285 * calls update_odr() while passing this value as an argument. In IRQ mode, the
286 * data outputs will not be read AT the requested poll interval, rather, the
287 * lowest ODR that can support the requested interval. The client application
288 * will be responsible for retrieving data from the input node at the desired
292 /* Returns currently selected poll interval (in ms) */
293 static ssize_t
kxtj9_get_poll(struct device
*dev
,
294 struct device_attribute
*attr
, char *buf
)
296 struct i2c_client
*client
= to_i2c_client(dev
);
297 struct kxtj9_data
*tj9
= i2c_get_clientdata(client
);
299 return sprintf(buf
, "%d\n", tj9
->last_poll_interval
);
302 /* Allow users to select a new poll interval (in ms) */
303 static ssize_t
kxtj9_set_poll(struct device
*dev
, struct device_attribute
*attr
,
304 const char *buf
, size_t count
)
306 struct i2c_client
*client
= to_i2c_client(dev
);
307 struct kxtj9_data
*tj9
= i2c_get_clientdata(client
);
308 struct input_dev
*input_dev
= tj9
->input_dev
;
309 unsigned int interval
;
312 error
= kstrtouint(buf
, 10, &interval
);
316 /* Lock the device to prevent races with open/close (and itself) */
317 guard(mutex
)(&input_dev
->mutex
);
318 guard(disable_irq
)(&client
->irq
);
321 * Set current interval to the greater of the minimum interval or
322 * the requested interval
324 tj9
->last_poll_interval
= max(interval
, tj9
->pdata
.min_interval
);
326 kxtj9_update_odr(tj9
, tj9
->last_poll_interval
);
331 static DEVICE_ATTR(poll
, S_IRUGO
|S_IWUSR
, kxtj9_get_poll
, kxtj9_set_poll
);
333 static struct attribute
*kxtj9_attrs
[] = {
338 static umode_t
kxtj9_attr_is_visible(struct kobject
*kobj
,
339 struct attribute
*attr
, int n
)
341 struct device
*dev
= kobj_to_dev(kobj
);
342 struct i2c_client
*client
= to_i2c_client(dev
);
344 return client
->irq
? attr
->mode
: 0;
347 static struct attribute_group kxtj9_group
= {
348 .attrs
= kxtj9_attrs
,
349 .is_visible
= kxtj9_attr_is_visible
,
351 __ATTRIBUTE_GROUPS(kxtj9
);
353 static void kxtj9_poll(struct input_dev
*input
)
355 struct kxtj9_data
*tj9
= input_get_drvdata(input
);
356 unsigned int poll_interval
= input_get_poll_interval(input
);
358 kxtj9_report_acceleration_data(tj9
);
360 if (poll_interval
!= tj9
->last_poll_interval
) {
361 kxtj9_update_odr(tj9
, poll_interval
);
362 tj9
->last_poll_interval
= poll_interval
;
366 static void kxtj9_platform_exit(void *data
)
368 struct kxtj9_data
*tj9
= data
;
374 static int kxtj9_verify(struct kxtj9_data
*tj9
)
378 retval
= kxtj9_device_power_on(tj9
);
382 retval
= i2c_smbus_read_byte_data(tj9
->client
, WHO_AM_I
);
384 dev_err(&tj9
->client
->dev
, "read err int source\n");
388 retval
= (retval
!= 0x07 && retval
!= 0x08) ? -EIO
: 0;
391 kxtj9_device_power_off(tj9
);
395 static int kxtj9_probe(struct i2c_client
*client
)
397 const struct kxtj9_platform_data
*pdata
=
398 dev_get_platdata(&client
->dev
);
399 struct kxtj9_data
*tj9
;
400 struct input_dev
*input_dev
;
403 if (!i2c_check_functionality(client
->adapter
,
404 I2C_FUNC_I2C
| I2C_FUNC_SMBUS_BYTE_DATA
)) {
405 dev_err(&client
->dev
, "client is not i2c capable\n");
410 dev_err(&client
->dev
, "platform data is NULL; exiting\n");
414 tj9
= devm_kzalloc(&client
->dev
, sizeof(*tj9
), GFP_KERNEL
);
416 dev_err(&client
->dev
,
417 "failed to allocate memory for module data\n");
421 tj9
->client
= client
;
430 err
= devm_add_action_or_reset(&client
->dev
, kxtj9_platform_exit
, tj9
);
434 err
= kxtj9_verify(tj9
);
436 dev_err(&client
->dev
, "device not recognized\n");
440 i2c_set_clientdata(client
, tj9
);
442 tj9
->ctrl_reg1
= tj9
->pdata
.res_12bit
| tj9
->pdata
.g_range
;
443 tj9
->last_poll_interval
= tj9
->pdata
.init_interval
;
445 input_dev
= devm_input_allocate_device(&client
->dev
);
447 dev_err(&client
->dev
, "input device allocate failed\n");
451 input_set_drvdata(input_dev
, tj9
);
452 tj9
->input_dev
= input_dev
;
454 input_dev
->name
= "kxtj9_accel";
455 input_dev
->id
.bustype
= BUS_I2C
;
457 input_dev
->open
= kxtj9_input_open
;
458 input_dev
->close
= kxtj9_input_close
;
460 input_set_abs_params(input_dev
, ABS_X
, -G_MAX
, G_MAX
, FUZZ
, FLAT
);
461 input_set_abs_params(input_dev
, ABS_Y
, -G_MAX
, G_MAX
, FUZZ
, FLAT
);
462 input_set_abs_params(input_dev
, ABS_Z
, -G_MAX
, G_MAX
, FUZZ
, FLAT
);
464 if (client
->irq
<= 0) {
465 err
= input_setup_polling(input_dev
, kxtj9_poll
);
470 err
= input_register_device(input_dev
);
472 dev_err(&client
->dev
,
473 "unable to register input polled device %s: %d\n",
474 input_dev
->name
, err
);
479 /* If in irq mode, populate INT_CTRL_REG1 and enable DRDY. */
480 tj9
->int_ctrl
|= KXTJ9_IEN
| KXTJ9_IEA
| KXTJ9_IEL
;
481 tj9
->ctrl_reg1
|= DRDYE
;
483 err
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
485 IRQF_TRIGGER_RISING
|
489 dev_err(&client
->dev
, "request irq failed: %d\n", err
);
497 static int kxtj9_suspend(struct device
*dev
)
499 struct i2c_client
*client
= to_i2c_client(dev
);
500 struct kxtj9_data
*tj9
= i2c_get_clientdata(client
);
501 struct input_dev
*input_dev
= tj9
->input_dev
;
503 guard(mutex
)(&input_dev
->mutex
);
505 if (input_device_enabled(input_dev
))
511 static int kxtj9_resume(struct device
*dev
)
513 struct i2c_client
*client
= to_i2c_client(dev
);
514 struct kxtj9_data
*tj9
= i2c_get_clientdata(client
);
515 struct input_dev
*input_dev
= tj9
->input_dev
;
517 guard(mutex
)(&input_dev
->mutex
);
519 if (input_device_enabled(input_dev
))
525 static DEFINE_SIMPLE_DEV_PM_OPS(kxtj9_pm_ops
, kxtj9_suspend
, kxtj9_resume
);
527 static const struct i2c_device_id kxtj9_id
[] = {
532 MODULE_DEVICE_TABLE(i2c
, kxtj9_id
);
534 static struct i2c_driver kxtj9_driver
= {
537 .dev_groups
= kxtj9_groups
,
538 .pm
= pm_sleep_ptr(&kxtj9_pm_ops
),
540 .probe
= kxtj9_probe
,
541 .id_table
= kxtj9_id
,
544 module_i2c_driver(kxtj9_driver
);
546 MODULE_DESCRIPTION("KXTJ9 accelerometer driver");
547 MODULE_AUTHOR("Chris Hudson <chudson@kionix.com>");
548 MODULE_LICENSE("GPL");