2 * Copyright (C) 2011 Kionix, Inc.
3 * Written by Chris Hudson <chudson@kionix.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 #include <linux/delay.h>
21 #include <linux/i2c.h>
22 #include <linux/input.h>
23 #include <linux/interrupt.h>
24 #include <linux/slab.h>
25 #include <linux/input/kxtj9.h>
26 #include <linux/input-polldev.h>
30 /* OUTPUT REGISTERS */
33 /* CONTROL REGISTERS */
35 #define CTRL_REG1 0x1B
36 #define INT_CTRL1 0x1E
37 #define DATA_CTRL 0x21
38 /* CONTROL REGISTER 1 BITS */
40 #define PC1_ON (1 << 7)
41 /* Data ready funtion enable bit: set during probe if using irq mode */
42 #define DRDYE (1 << 5)
43 /* INTERRUPT CONTROL REGISTER 1 BITS */
44 /* Set these during probe if using irq mode */
45 #define KXTJ9_IEL (1 << 3)
46 #define KXTJ9_IEA (1 << 4)
47 #define KXTJ9_IEN (1 << 5)
48 /* INPUT_ABS CONSTANTS */
51 /* RESUME STATE INDICES */
52 #define RES_DATA_CTRL 0
53 #define RES_CTRL_REG1 1
54 #define RES_INT_CTRL1 2
55 #define RESUME_ENTRIES 3
58 * The following table lists the maximum appropriate poll interval for each
59 * available output data rate.
64 } kxtj9_odr_table
[] = {
75 struct i2c_client
*client
;
76 struct kxtj9_platform_data pdata
;
77 struct input_dev
*input_dev
;
78 #ifdef CONFIG_INPUT_KXTJ9_POLLED_MODE
79 struct input_polled_dev
*poll_dev
;
81 unsigned int last_poll_interval
;
88 static int kxtj9_i2c_read(struct kxtj9_data
*tj9
, u8 addr
, u8
*data
, int len
)
90 struct i2c_msg msgs
[] = {
92 .addr
= tj9
->client
->addr
,
93 .flags
= tj9
->client
->flags
,
98 .addr
= tj9
->client
->addr
,
99 .flags
= tj9
->client
->flags
| I2C_M_RD
,
105 return i2c_transfer(tj9
->client
->adapter
, msgs
, 2);
108 static void kxtj9_report_acceleration_data(struct kxtj9_data
*tj9
)
110 s16 acc_data
[3]; /* Data bytes from hardware xL, xH, yL, yH, zL, zH */
114 err
= kxtj9_i2c_read(tj9
, XOUT_L
, (u8
*)acc_data
, 6);
116 dev_err(&tj9
->client
->dev
, "accelerometer data read failed\n");
118 x
= le16_to_cpu(acc_data
[tj9
->pdata
.axis_map_x
]) >> tj9
->shift
;
119 y
= le16_to_cpu(acc_data
[tj9
->pdata
.axis_map_y
]) >> tj9
->shift
;
120 z
= le16_to_cpu(acc_data
[tj9
->pdata
.axis_map_z
]) >> tj9
->shift
;
122 input_report_abs(tj9
->input_dev
, ABS_X
, tj9
->pdata
.negate_x
? -x
: x
);
123 input_report_abs(tj9
->input_dev
, ABS_Y
, tj9
->pdata
.negate_y
? -y
: y
);
124 input_report_abs(tj9
->input_dev
, ABS_Z
, tj9
->pdata
.negate_z
? -z
: z
);
125 input_sync(tj9
->input_dev
);
128 static irqreturn_t
kxtj9_isr(int irq
, void *dev
)
130 struct kxtj9_data
*tj9
= dev
;
133 /* data ready is the only possible interrupt type */
134 kxtj9_report_acceleration_data(tj9
);
136 err
= i2c_smbus_read_byte_data(tj9
->client
, INT_REL
);
138 dev_err(&tj9
->client
->dev
,
139 "error clearing interrupt status: %d\n", err
);
144 static int kxtj9_update_g_range(struct kxtj9_data
*tj9
, u8 new_g_range
)
146 switch (new_g_range
) {
160 tj9
->ctrl_reg1
&= 0xe7;
161 tj9
->ctrl_reg1
|= new_g_range
;
166 static int kxtj9_update_odr(struct kxtj9_data
*tj9
, unsigned int poll_interval
)
171 /* Use the lowest ODR that can support the requested poll interval */
172 for (i
= 0; i
< ARRAY_SIZE(kxtj9_odr_table
); i
++) {
173 tj9
->data_ctrl
= kxtj9_odr_table
[i
].mask
;
174 if (poll_interval
< kxtj9_odr_table
[i
].cutoff
)
178 err
= i2c_smbus_write_byte_data(tj9
->client
, CTRL_REG1
, 0);
182 err
= i2c_smbus_write_byte_data(tj9
->client
, DATA_CTRL
, tj9
->data_ctrl
);
186 err
= i2c_smbus_write_byte_data(tj9
->client
, CTRL_REG1
, tj9
->ctrl_reg1
);
193 static int kxtj9_device_power_on(struct kxtj9_data
*tj9
)
195 if (tj9
->pdata
.power_on
)
196 return tj9
->pdata
.power_on();
201 static void kxtj9_device_power_off(struct kxtj9_data
*tj9
)
205 tj9
->ctrl_reg1
&= PC1_OFF
;
206 err
= i2c_smbus_write_byte_data(tj9
->client
, CTRL_REG1
, tj9
->ctrl_reg1
);
208 dev_err(&tj9
->client
->dev
, "soft power off failed\n");
210 if (tj9
->pdata
.power_off
)
211 tj9
->pdata
.power_off();
214 static int kxtj9_enable(struct kxtj9_data
*tj9
)
218 err
= kxtj9_device_power_on(tj9
);
222 /* ensure that PC1 is cleared before updating control registers */
223 err
= i2c_smbus_write_byte_data(tj9
->client
, CTRL_REG1
, 0);
227 /* only write INT_CTRL_REG1 if in irq mode */
228 if (tj9
->client
->irq
) {
229 err
= i2c_smbus_write_byte_data(tj9
->client
,
230 INT_CTRL1
, tj9
->int_ctrl
);
235 err
= kxtj9_update_g_range(tj9
, tj9
->pdata
.g_range
);
239 /* turn on outputs */
240 tj9
->ctrl_reg1
|= PC1_ON
;
241 err
= i2c_smbus_write_byte_data(tj9
->client
, CTRL_REG1
, tj9
->ctrl_reg1
);
245 err
= kxtj9_update_odr(tj9
, tj9
->last_poll_interval
);
249 /* clear initial interrupt if in irq mode */
250 if (tj9
->client
->irq
) {
251 err
= i2c_smbus_read_byte_data(tj9
->client
, INT_REL
);
253 dev_err(&tj9
->client
->dev
,
254 "error clearing interrupt: %d\n", err
);
262 kxtj9_device_power_off(tj9
);
266 static void kxtj9_disable(struct kxtj9_data
*tj9
)
268 kxtj9_device_power_off(tj9
);
271 static int kxtj9_input_open(struct input_dev
*input
)
273 struct kxtj9_data
*tj9
= input_get_drvdata(input
);
275 return kxtj9_enable(tj9
);
278 static void kxtj9_input_close(struct input_dev
*dev
)
280 struct kxtj9_data
*tj9
= input_get_drvdata(dev
);
285 static void __devinit
kxtj9_init_input_device(struct kxtj9_data
*tj9
,
286 struct input_dev
*input_dev
)
288 __set_bit(EV_ABS
, input_dev
->evbit
);
289 input_set_abs_params(input_dev
, ABS_X
, -G_MAX
, G_MAX
, FUZZ
, FLAT
);
290 input_set_abs_params(input_dev
, ABS_Y
, -G_MAX
, G_MAX
, FUZZ
, FLAT
);
291 input_set_abs_params(input_dev
, ABS_Z
, -G_MAX
, G_MAX
, FUZZ
, FLAT
);
293 input_dev
->name
= "kxtj9_accel";
294 input_dev
->id
.bustype
= BUS_I2C
;
295 input_dev
->dev
.parent
= &tj9
->client
->dev
;
298 static int __devinit
kxtj9_setup_input_device(struct kxtj9_data
*tj9
)
300 struct input_dev
*input_dev
;
303 input_dev
= input_allocate_device();
305 dev_err(&tj9
->client
->dev
, "input device allocate failed\n");
309 tj9
->input_dev
= input_dev
;
311 input_dev
->open
= kxtj9_input_open
;
312 input_dev
->close
= kxtj9_input_close
;
313 input_set_drvdata(input_dev
, tj9
);
315 kxtj9_init_input_device(tj9
, input_dev
);
317 err
= input_register_device(tj9
->input_dev
);
319 dev_err(&tj9
->client
->dev
,
320 "unable to register input polled device %s: %d\n",
321 tj9
->input_dev
->name
, err
);
322 input_free_device(tj9
->input_dev
);
330 * When IRQ mode is selected, we need to provide an interface to allow the user
331 * to change the output data rate of the part. For consistency, we are using
332 * the set_poll method, which accepts a poll interval in milliseconds, and then
333 * calls update_odr() while passing this value as an argument. In IRQ mode, the
334 * data outputs will not be read AT the requested poll interval, rather, the
335 * lowest ODR that can support the requested interval. The client application
336 * will be responsible for retrieving data from the input node at the desired
340 /* Returns currently selected poll interval (in ms) */
341 static ssize_t
kxtj9_get_poll(struct device
*dev
,
342 struct device_attribute
*attr
, char *buf
)
344 struct i2c_client
*client
= to_i2c_client(dev
);
345 struct kxtj9_data
*tj9
= i2c_get_clientdata(client
);
347 return sprintf(buf
, "%d\n", tj9
->last_poll_interval
);
350 /* Allow users to select a new poll interval (in ms) */
351 static ssize_t
kxtj9_set_poll(struct device
*dev
, struct device_attribute
*attr
,
352 const char *buf
, size_t count
)
354 struct i2c_client
*client
= to_i2c_client(dev
);
355 struct kxtj9_data
*tj9
= i2c_get_clientdata(client
);
356 struct input_dev
*input_dev
= tj9
->input_dev
;
357 unsigned int interval
;
360 error
= kstrtouint(buf
, 10, &interval
);
364 /* Lock the device to prevent races with open/close (and itself) */
365 mutex_lock(&input_dev
->mutex
);
367 disable_irq(client
->irq
);
370 * Set current interval to the greater of the minimum interval or
371 * the requested interval
373 tj9
->last_poll_interval
= max(interval
, tj9
->pdata
.min_interval
);
375 kxtj9_update_odr(tj9
, tj9
->last_poll_interval
);
377 enable_irq(client
->irq
);
378 mutex_unlock(&input_dev
->mutex
);
383 static DEVICE_ATTR(poll
, S_IRUGO
|S_IWUSR
, kxtj9_get_poll
, kxtj9_set_poll
);
385 static struct attribute
*kxtj9_attributes
[] = {
390 static struct attribute_group kxtj9_attribute_group
= {
391 .attrs
= kxtj9_attributes
395 #ifdef CONFIG_INPUT_KXTJ9_POLLED_MODE
396 static void kxtj9_poll(struct input_polled_dev
*dev
)
398 struct kxtj9_data
*tj9
= dev
->private;
399 unsigned int poll_interval
= dev
->poll_interval
;
401 kxtj9_report_acceleration_data(tj9
);
403 if (poll_interval
!= tj9
->last_poll_interval
) {
404 kxtj9_update_odr(tj9
, poll_interval
);
405 tj9
->last_poll_interval
= poll_interval
;
409 static void kxtj9_polled_input_open(struct input_polled_dev
*dev
)
411 struct kxtj9_data
*tj9
= dev
->private;
416 static void kxtj9_polled_input_close(struct input_polled_dev
*dev
)
418 struct kxtj9_data
*tj9
= dev
->private;
423 static int __devinit
kxtj9_setup_polled_device(struct kxtj9_data
*tj9
)
426 struct input_polled_dev
*poll_dev
;
427 poll_dev
= input_allocate_polled_device();
430 dev_err(&tj9
->client
->dev
,
431 "Failed to allocate polled device\n");
435 tj9
->poll_dev
= poll_dev
;
436 tj9
->input_dev
= poll_dev
->input
;
438 poll_dev
->private = tj9
;
439 poll_dev
->poll
= kxtj9_poll
;
440 poll_dev
->open
= kxtj9_polled_input_open
;
441 poll_dev
->close
= kxtj9_polled_input_close
;
443 kxtj9_init_input_device(tj9
, poll_dev
->input
);
445 err
= input_register_polled_device(poll_dev
);
447 dev_err(&tj9
->client
->dev
,
448 "Unable to register polled device, err=%d\n", err
);
449 input_free_polled_device(poll_dev
);
456 static void __devexit
kxtj9_teardown_polled_device(struct kxtj9_data
*tj9
)
458 input_unregister_polled_device(tj9
->poll_dev
);
459 input_free_polled_device(tj9
->poll_dev
);
464 static inline int kxtj9_setup_polled_device(struct kxtj9_data
*tj9
)
469 static inline void kxtj9_teardown_polled_device(struct kxtj9_data
*tj9
)
475 static int __devinit
kxtj9_verify(struct kxtj9_data
*tj9
)
479 retval
= kxtj9_device_power_on(tj9
);
483 retval
= i2c_smbus_read_byte_data(tj9
->client
, WHO_AM_I
);
485 dev_err(&tj9
->client
->dev
, "read err int source\n");
489 retval
= retval
!= 0x06 ? -EIO
: 0;
492 kxtj9_device_power_off(tj9
);
496 static int __devinit
kxtj9_probe(struct i2c_client
*client
,
497 const struct i2c_device_id
*id
)
499 const struct kxtj9_platform_data
*pdata
= client
->dev
.platform_data
;
500 struct kxtj9_data
*tj9
;
503 if (!i2c_check_functionality(client
->adapter
,
504 I2C_FUNC_I2C
| I2C_FUNC_SMBUS_BYTE_DATA
)) {
505 dev_err(&client
->dev
, "client is not i2c capable\n");
510 dev_err(&client
->dev
, "platform data is NULL; exiting\n");
514 tj9
= kzalloc(sizeof(*tj9
), GFP_KERNEL
);
516 dev_err(&client
->dev
,
517 "failed to allocate memory for module data\n");
521 tj9
->client
= client
;
530 err
= kxtj9_verify(tj9
);
532 dev_err(&client
->dev
, "device not recognized\n");
536 i2c_set_clientdata(client
, tj9
);
538 tj9
->ctrl_reg1
= tj9
->pdata
.res_12bit
| tj9
->pdata
.g_range
;
539 tj9
->data_ctrl
= tj9
->pdata
.data_odr_init
;
542 /* If in irq mode, populate INT_CTRL_REG1 and enable DRDY. */
543 tj9
->int_ctrl
|= KXTJ9_IEN
| KXTJ9_IEA
| KXTJ9_IEL
;
544 tj9
->ctrl_reg1
|= DRDYE
;
546 err
= kxtj9_setup_input_device(tj9
);
550 err
= request_threaded_irq(client
->irq
, NULL
, kxtj9_isr
,
551 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
554 dev_err(&client
->dev
, "request irq failed: %d\n", err
);
555 goto err_destroy_input
;
558 err
= sysfs_create_group(&client
->dev
.kobj
, &kxtj9_attribute_group
);
560 dev_err(&client
->dev
, "sysfs create failed: %d\n", err
);
565 err
= kxtj9_setup_polled_device(tj9
);
573 free_irq(client
->irq
, tj9
);
575 input_unregister_device(tj9
->input_dev
);
584 static int __devexit
kxtj9_remove(struct i2c_client
*client
)
586 struct kxtj9_data
*tj9
= i2c_get_clientdata(client
);
589 sysfs_remove_group(&client
->dev
.kobj
, &kxtj9_attribute_group
);
590 free_irq(client
->irq
, tj9
);
591 input_unregister_device(tj9
->input_dev
);
593 kxtj9_teardown_polled_device(tj9
);
604 #ifdef CONFIG_PM_SLEEP
605 static int kxtj9_suspend(struct device
*dev
)
607 struct i2c_client
*client
= to_i2c_client(dev
);
608 struct kxtj9_data
*tj9
= i2c_get_clientdata(client
);
609 struct input_dev
*input_dev
= tj9
->input_dev
;
611 mutex_lock(&input_dev
->mutex
);
613 if (input_dev
->users
)
616 mutex_unlock(&input_dev
->mutex
);
620 static int kxtj9_resume(struct device
*dev
)
622 struct i2c_client
*client
= to_i2c_client(dev
);
623 struct kxtj9_data
*tj9
= i2c_get_clientdata(client
);
624 struct input_dev
*input_dev
= tj9
->input_dev
;
627 mutex_lock(&input_dev
->mutex
);
629 if (input_dev
->users
)
632 mutex_unlock(&input_dev
->mutex
);
637 static SIMPLE_DEV_PM_OPS(kxtj9_pm_ops
, kxtj9_suspend
, kxtj9_resume
);
639 static const struct i2c_device_id kxtj9_id
[] = {
644 MODULE_DEVICE_TABLE(i2c
, kxtj9_id
);
646 static struct i2c_driver kxtj9_driver
= {
649 .owner
= THIS_MODULE
,
652 .probe
= kxtj9_probe
,
653 .remove
= __devexit_p(kxtj9_remove
),
654 .id_table
= kxtj9_id
,
657 static int __init
kxtj9_init(void)
659 return i2c_add_driver(&kxtj9_driver
);
661 module_init(kxtj9_init
);
663 static void __exit
kxtj9_exit(void)
665 i2c_del_driver(&kxtj9_driver
);
667 module_exit(kxtj9_exit
);
669 MODULE_DESCRIPTION("KXTJ9 accelerometer driver");
670 MODULE_AUTHOR("Chris Hudson <chudson@kionix.com>");
671 MODULE_LICENSE("GPL");