21 #include <linux/i2c.h>
22 #include <linux/input.h>
24 #include <linux/module.h>
25 #include <linux/slab.h>
36 #define CTRL_REG1 0x1B
37 #define INT_CTRL1 0x1E
38 #define DATA_CTRL 0x21
41 #define PC1_ON (1 << 7)
43 #define DRDYE (1 << 5)
54 #define KXTJ9_IEL (1 << 3)
55 #define KXTJ9_IEA (1 << 4)
56 #define KXTJ9_IEN (1 << 5)
61 #define RES_DATA_CTRL 0
62 #define RES_CTRL_REG1 1
63 #define RES_INT_CTRL1 2
64 #define RESUME_ENTRIES 3
73 } kxtj9_odr_table[] = {
87 #ifdef CONFIG_INPUT_KXTJ9_POLLED_MODE
88 struct input_polled_dev *poll_dev;
102 .flags = tj9->
client->flags,
117 static void kxtj9_report_acceleration_data(
struct kxtj9_data *tj9)
123 err = kxtj9_i2c_read(tj9,
XOUT_L, (
u8 *)acc_data, 6);
125 dev_err(&tj9->
client->dev,
"accelerometer data read failed\n");
147 kxtj9_report_acceleration_data(tj9);
152 "error clearing interrupt status: %d\n", err);
157 static int kxtj9_update_g_range(
struct kxtj9_data *tj9,
u8 new_g_range)
159 switch (new_g_range) {
179 static int kxtj9_update_odr(
struct kxtj9_data *tj9,
unsigned int poll_interval)
185 for (i = 0; i <
ARRAY_SIZE(kxtj9_odr_table); i++) {
187 if (poll_interval < kxtj9_odr_table[i].
cutoff)
206 static int kxtj9_device_power_on(
struct kxtj9_data *tj9)
208 if (tj9->
pdata.power_on)
209 return tj9->
pdata.power_on();
214 static void kxtj9_device_power_off(
struct kxtj9_data *tj9)
223 if (tj9->
pdata.power_off)
224 tj9->
pdata.power_off();
227 static int kxtj9_enable(
struct kxtj9_data *tj9)
231 err = kxtj9_device_power_on(tj9);
248 err = kxtj9_update_g_range(tj9, tj9->
pdata.g_range);
267 "error clearing interrupt: %d\n", err);
275 kxtj9_device_power_off(tj9);
279 static void kxtj9_disable(
struct kxtj9_data *tj9)
281 kxtj9_device_power_off(tj9);
286 struct kxtj9_data *tj9 = input_get_drvdata(input);
288 return kxtj9_enable(tj9);
291 static void kxtj9_input_close(
struct input_dev *dev)
293 struct kxtj9_data *tj9 = input_get_drvdata(dev);
306 input_dev->name =
"kxtj9_accel";
307 input_dev->id.bustype =
BUS_I2C;
308 input_dev->dev.parent = &tj9->
client->dev;
313 struct input_dev *input_dev;
316 input_dev = input_allocate_device();
318 dev_err(&tj9->
client->dev,
"input device allocate failed\n");
324 input_dev->open = kxtj9_input_open;
325 input_dev->close = kxtj9_input_close;
326 input_set_drvdata(input_dev, tj9);
328 kxtj9_init_input_device(tj9, input_dev);
330 err = input_register_device(tj9->
input_dev);
333 "unable to register input polled device %s: %d\n",
358 struct kxtj9_data *tj9 = i2c_get_clientdata(client);
368 struct kxtj9_data *tj9 = i2c_get_clientdata(client);
369 struct input_dev *input_dev = tj9->
input_dev;
398 static struct attribute *kxtj9_attributes[] = {
404 .attrs = kxtj9_attributes
408 #ifdef CONFIG_INPUT_KXTJ9_POLLED_MODE
409 static void kxtj9_poll(
struct input_polled_dev *dev)
412 unsigned int poll_interval = dev->poll_interval;
414 kxtj9_report_acceleration_data(tj9);
417 kxtj9_update_odr(tj9, poll_interval);
422 static void kxtj9_polled_input_open(
struct input_polled_dev *dev)
429 static void kxtj9_polled_input_close(
struct input_polled_dev *dev)
439 struct input_polled_dev *poll_dev;
444 "Failed to allocate polled device\n");
448 tj9->poll_dev = poll_dev;
451 poll_dev->private = tj9;
452 poll_dev->poll = kxtj9_poll;
453 poll_dev->open = kxtj9_polled_input_open;
454 poll_dev->close = kxtj9_polled_input_close;
456 kxtj9_init_input_device(tj9, poll_dev->input);
461 "Unable to register polled device, err=%d\n", err);
477 static inline int kxtj9_setup_polled_device(
struct kxtj9_data *tj9)
482 static inline void kxtj9_teardown_polled_device(
struct kxtj9_data *tj9)
492 retval = kxtj9_device_power_on(tj9);
502 retval = (retval != 0x07 && retval != 0x08) ? -
EIO : 0;
505 kxtj9_device_power_off(tj9);
516 if (!i2c_check_functionality(client->
adapter,
518 dev_err(&client->
dev,
"client is not i2c capable\n");
523 dev_err(&client->
dev,
"platform data is NULL; exiting\n");
530 "failed to allocate memory for module data\n");
543 err = kxtj9_verify(tj9);
545 dev_err(&client->
dev,
"device not recognized\n");
549 i2c_set_clientdata(client, tj9);
559 err = kxtj9_setup_input_device(tj9);
567 dev_err(&client->
dev,
"request irq failed: %d\n", err);
568 goto err_destroy_input;
573 dev_err(&client->
dev,
"sysfs create failed: %d\n", err);
578 err = kxtj9_setup_polled_device(tj9);
599 struct kxtj9_data *tj9 = i2c_get_clientdata(client);
606 kxtj9_teardown_polled_device(tj9);
617 #ifdef CONFIG_PM_SLEEP
618 static int kxtj9_suspend(
struct device *dev)
621 struct kxtj9_data *tj9 = i2c_get_clientdata(client);
622 struct input_dev *input_dev = tj9->
input_dev;
626 if (input_dev->users)
633 static int kxtj9_resume(
struct device *dev)
636 struct kxtj9_data *tj9 = i2c_get_clientdata(client);
637 struct input_dev *input_dev = tj9->
input_dev;
642 if (input_dev->users)
665 .probe = kxtj9_probe,
667 .id_table = kxtj9_id,