1 /* 2 * Copyright (C) 2011 Kionix, Inc. 3 * Written by Chris Hudson <chudson@kionix.com> 4 * 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. 8 * 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. 13 * 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 17 * 02111-1307, USA 18 */ 19 20 #include <linux/delay.h> 21 #include <linux/i2c.h> 22 #include <linux/input.h> 23 #include <linux/interrupt.h> 24 #include <linux/module.h> 25 #include <linux/slab.h> 26 #include <linux/input/kxtj9.h> 27 #include <linux/input-polldev.h> 28 29 #define NAME "kxtj9" 30 #define G_MAX 8000 31 /* OUTPUT REGISTERS */ 32 #define XOUT_L 0x06 33 #define WHO_AM_I 0x0F 34 /* CONTROL REGISTERS */ 35 #define INT_REL 0x1A 36 #define CTRL_REG1 0x1B 37 #define INT_CTRL1 0x1E 38 #define DATA_CTRL 0x21 39 /* CONTROL REGISTER 1 BITS */ 40 #define PC1_OFF 0x7F 41 #define PC1_ON (1 << 7) 42 /* Data ready funtion enable bit: set during probe if using irq mode */ 43 #define DRDYE (1 << 5) 44 /* INTERRUPT CONTROL REGISTER 1 BITS */ 45 /* Set these during probe if using irq mode */ 46 #define KXTJ9_IEL (1 << 3) 47 #define KXTJ9_IEA (1 << 4) 48 #define KXTJ9_IEN (1 << 5) 49 /* INPUT_ABS CONSTANTS */ 50 #define FUZZ 3 51 #define FLAT 3 52 /* RESUME STATE INDICES */ 53 #define RES_DATA_CTRL 0 54 #define RES_CTRL_REG1 1 55 #define RES_INT_CTRL1 2 56 #define RESUME_ENTRIES 3 57 58 /* 59 * The following table lists the maximum appropriate poll interval for each 60 * available output data rate. 61 */ 62 static const struct { 63 unsigned int cutoff; 64 u8 mask; 65 } kxtj9_odr_table[] = { 66 { 3, ODR800F }, 67 { 5, ODR400F }, 68 { 10, ODR200F }, 69 { 20, ODR100F }, 70 { 40, ODR50F }, 71 { 80, ODR25F }, 72 { 0, ODR12_5F}, 73 }; 74 75 struct kxtj9_data { 76 struct i2c_client *client; 77 struct kxtj9_platform_data pdata; 78 struct input_dev *input_dev; 79 #ifdef CONFIG_INPUT_KXTJ9_POLLED_MODE 80 struct input_polled_dev *poll_dev; 81 #endif 82 unsigned int last_poll_interval; 83 u8 shift; 84 u8 ctrl_reg1; 85 u8 data_ctrl; 86 u8 int_ctrl; 87 }; 88 89 static int kxtj9_i2c_read(struct kxtj9_data *tj9, u8 addr, u8 *data, int len) 90 { 91 struct i2c_msg msgs[] = { 92 { 93 .addr = tj9->client->addr, 94 .flags = tj9->client->flags, 95 .len = 1, 96 .buf = &addr, 97 }, 98 { 99 .addr = tj9->client->addr, 100 .flags = tj9->client->flags | I2C_M_RD, 101 .len = len, 102 .buf = data, 103 }, 104 }; 105 106 return i2c_transfer(tj9->client->adapter, msgs, 2); 107 } 108 109 static void kxtj9_report_acceleration_data(struct kxtj9_data *tj9) 110 { 111 s16 acc_data[3]; /* Data bytes from hardware xL, xH, yL, yH, zL, zH */ 112 s16 x, y, z; 113 int err; 114 115 err = kxtj9_i2c_read(tj9, XOUT_L, (u8 *)acc_data, 6); 116 if (err < 0) 117 dev_err(&tj9->client->dev, "accelerometer data read failed\n"); 118 119 x = le16_to_cpu(acc_data[tj9->pdata.axis_map_x]) >> tj9->shift; 120 y = le16_to_cpu(acc_data[tj9->pdata.axis_map_y]) >> tj9->shift; 121 z = le16_to_cpu(acc_data[tj9->pdata.axis_map_z]) >> tj9->shift; 122 123 input_report_abs(tj9->input_dev, ABS_X, tj9->pdata.negate_x ? -x : x); 124 input_report_abs(tj9->input_dev, ABS_Y, tj9->pdata.negate_y ? -y : y); 125 input_report_abs(tj9->input_dev, ABS_Z, tj9->pdata.negate_z ? -z : z); 126 input_sync(tj9->input_dev); 127 } 128 129 static irqreturn_t kxtj9_isr(int irq, void *dev) 130 { 131 struct kxtj9_data *tj9 = dev; 132 int err; 133 134 /* data ready is the only possible interrupt type */ 135 kxtj9_report_acceleration_data(tj9); 136 137 err = i2c_smbus_read_byte_data(tj9->client, INT_REL); 138 if (err < 0) 139 dev_err(&tj9->client->dev, 140 "error clearing interrupt status: %d\n", err); 141 142 return IRQ_HANDLED; 143 } 144 145 static int kxtj9_update_g_range(struct kxtj9_data *tj9, u8 new_g_range) 146 { 147 switch (new_g_range) { 148 case KXTJ9_G_2G: 149 tj9->shift = 4; 150 break; 151 case KXTJ9_G_4G: 152 tj9->shift = 3; 153 break; 154 case KXTJ9_G_8G: 155 tj9->shift = 2; 156 break; 157 default: 158 return -EINVAL; 159 } 160 161 tj9->ctrl_reg1 &= 0xe7; 162 tj9->ctrl_reg1 |= new_g_range; 163 164 return 0; 165 } 166 167 static int kxtj9_update_odr(struct kxtj9_data *tj9, unsigned int poll_interval) 168 { 169 int err; 170 int i; 171 172 /* Use the lowest ODR that can support the requested poll interval */ 173 for (i = 0; i < ARRAY_SIZE(kxtj9_odr_table); i++) { 174 tj9->data_ctrl = kxtj9_odr_table[i].mask; 175 if (poll_interval < kxtj9_odr_table[i].cutoff) 176 break; 177 } 178 179 err = i2c_smbus_write_byte_data(tj9->client, CTRL_REG1, 0); 180 if (err < 0) 181 return err; 182 183 err = i2c_smbus_write_byte_data(tj9->client, DATA_CTRL, tj9->data_ctrl); 184 if (err < 0) 185 return err; 186 187 err = i2c_smbus_write_byte_data(tj9->client, CTRL_REG1, tj9->ctrl_reg1); 188 if (err < 0) 189 return err; 190 191 return 0; 192 } 193 194 static int kxtj9_device_power_on(struct kxtj9_data *tj9) 195 { 196 if (tj9->pdata.power_on) 197 return tj9->pdata.power_on(); 198 199 return 0; 200 } 201 202 static void kxtj9_device_power_off(struct kxtj9_data *tj9) 203 { 204 int err; 205 206 tj9->ctrl_reg1 &= PC1_OFF; 207 err = i2c_smbus_write_byte_data(tj9->client, CTRL_REG1, tj9->ctrl_reg1); 208 if (err < 0) 209 dev_err(&tj9->client->dev, "soft power off failed\n"); 210 211 if (tj9->pdata.power_off) 212 tj9->pdata.power_off(); 213 } 214 215 static int kxtj9_enable(struct kxtj9_data *tj9) 216 { 217 int err; 218 219 err = kxtj9_device_power_on(tj9); 220 if (err < 0) 221 return err; 222 223 /* ensure that PC1 is cleared before updating control registers */ 224 err = i2c_smbus_write_byte_data(tj9->client, CTRL_REG1, 0); 225 if (err < 0) 226 return err; 227 228 /* only write INT_CTRL_REG1 if in irq mode */ 229 if (tj9->client->irq) { 230 err = i2c_smbus_write_byte_data(tj9->client, 231 INT_CTRL1, tj9->int_ctrl); 232 if (err < 0) 233 return err; 234 } 235 236 err = kxtj9_update_g_range(tj9, tj9->pdata.g_range); 237 if (err < 0) 238 return err; 239 240 /* turn on outputs */ 241 tj9->ctrl_reg1 |= PC1_ON; 242 err = i2c_smbus_write_byte_data(tj9->client, CTRL_REG1, tj9->ctrl_reg1); 243 if (err < 0) 244 return err; 245 246 err = kxtj9_update_odr(tj9, tj9->last_poll_interval); 247 if (err < 0) 248 return err; 249 250 /* clear initial interrupt if in irq mode */ 251 if (tj9->client->irq) { 252 err = i2c_smbus_read_byte_data(tj9->client, INT_REL); 253 if (err < 0) { 254 dev_err(&tj9->client->dev, 255 "error clearing interrupt: %d\n", err); 256 goto fail; 257 } 258 } 259 260 return 0; 261 262 fail: 263 kxtj9_device_power_off(tj9); 264 return err; 265 } 266 267 static void kxtj9_disable(struct kxtj9_data *tj9) 268 { 269 kxtj9_device_power_off(tj9); 270 } 271 272 static int kxtj9_input_open(struct input_dev *input) 273 { 274 struct kxtj9_data *tj9 = input_get_drvdata(input); 275 276 return kxtj9_enable(tj9); 277 } 278 279 static void kxtj9_input_close(struct input_dev *dev) 280 { 281 struct kxtj9_data *tj9 = input_get_drvdata(dev); 282 283 kxtj9_disable(tj9); 284 } 285 286 static void __devinit kxtj9_init_input_device(struct kxtj9_data *tj9, 287 struct input_dev *input_dev) 288 { 289 __set_bit(EV_ABS, input_dev->evbit); 290 input_set_abs_params(input_dev, ABS_X, -G_MAX, G_MAX, FUZZ, FLAT); 291 input_set_abs_params(input_dev, ABS_Y, -G_MAX, G_MAX, FUZZ, FLAT); 292 input_set_abs_params(input_dev, ABS_Z, -G_MAX, G_MAX, FUZZ, FLAT); 293 294 input_dev->name = "kxtj9_accel"; 295 input_dev->id.bustype = BUS_I2C; 296 input_dev->dev.parent = &tj9->client->dev; 297 } 298 299 static int __devinit kxtj9_setup_input_device(struct kxtj9_data *tj9) 300 { 301 struct input_dev *input_dev; 302 int err; 303 304 input_dev = input_allocate_device(); 305 if (!input_dev) { 306 dev_err(&tj9->client->dev, "input device allocate failed\n"); 307 return -ENOMEM; 308 } 309 310 tj9->input_dev = input_dev; 311 312 input_dev->open = kxtj9_input_open; 313 input_dev->close = kxtj9_input_close; 314 input_set_drvdata(input_dev, tj9); 315 316 kxtj9_init_input_device(tj9, input_dev); 317 318 err = input_register_device(tj9->input_dev); 319 if (err) { 320 dev_err(&tj9->client->dev, 321 "unable to register input polled device %s: %d\n", 322 tj9->input_dev->name, err); 323 input_free_device(tj9->input_dev); 324 return err; 325 } 326 327 return 0; 328 } 329 330 /* 331 * When IRQ mode is selected, we need to provide an interface to allow the user 332 * to change the output data rate of the part. For consistency, we are using 333 * the set_poll method, which accepts a poll interval in milliseconds, and then 334 * calls update_odr() while passing this value as an argument. In IRQ mode, the 335 * data outputs will not be read AT the requested poll interval, rather, the 336 * lowest ODR that can support the requested interval. The client application 337 * will be responsible for retrieving data from the input node at the desired 338 * interval. 339 */ 340 341 /* Returns currently selected poll interval (in ms) */ 342 static ssize_t kxtj9_get_poll(struct device *dev, 343 struct device_attribute *attr, char *buf) 344 { 345 struct i2c_client *client = to_i2c_client(dev); 346 struct kxtj9_data *tj9 = i2c_get_clientdata(client); 347 348 return sprintf(buf, "%d\n", tj9->last_poll_interval); 349 } 350 351 /* Allow users to select a new poll interval (in ms) */ 352 static ssize_t kxtj9_set_poll(struct device *dev, struct device_attribute *attr, 353 const char *buf, size_t count) 354 { 355 struct i2c_client *client = to_i2c_client(dev); 356 struct kxtj9_data *tj9 = i2c_get_clientdata(client); 357 struct input_dev *input_dev = tj9->input_dev; 358 unsigned int interval; 359 int error; 360 361 error = kstrtouint(buf, 10, &interval); 362 if (error < 0) 363 return error; 364 365 /* Lock the device to prevent races with open/close (and itself) */ 366 mutex_lock(&input_dev->mutex); 367 368 disable_irq(client->irq); 369 370 /* 371 * Set current interval to the greater of the minimum interval or 372 * the requested interval 373 */ 374 tj9->last_poll_interval = max(interval, tj9->pdata.min_interval); 375 376 kxtj9_update_odr(tj9, tj9->last_poll_interval); 377 378 enable_irq(client->irq); 379 mutex_unlock(&input_dev->mutex); 380 381 return count; 382 } 383 384 static DEVICE_ATTR(poll, S_IRUGO|S_IWUSR, kxtj9_get_poll, kxtj9_set_poll); 385 386 static struct attribute *kxtj9_attributes[] = { 387 &dev_attr_poll.attr, 388 NULL 389 }; 390 391 static struct attribute_group kxtj9_attribute_group = { 392 .attrs = kxtj9_attributes 393 }; 394 395 396 #ifdef CONFIG_INPUT_KXTJ9_POLLED_MODE 397 static void kxtj9_poll(struct input_polled_dev *dev) 398 { 399 struct kxtj9_data *tj9 = dev->private; 400 unsigned int poll_interval = dev->poll_interval; 401 402 kxtj9_report_acceleration_data(tj9); 403 404 if (poll_interval != tj9->last_poll_interval) { 405 kxtj9_update_odr(tj9, poll_interval); 406 tj9->last_poll_interval = poll_interval; 407 } 408 } 409 410 static void kxtj9_polled_input_open(struct input_polled_dev *dev) 411 { 412 struct kxtj9_data *tj9 = dev->private; 413 414 kxtj9_enable(tj9); 415 } 416 417 static void kxtj9_polled_input_close(struct input_polled_dev *dev) 418 { 419 struct kxtj9_data *tj9 = dev->private; 420 421 kxtj9_disable(tj9); 422 } 423 424 static int __devinit kxtj9_setup_polled_device(struct kxtj9_data *tj9) 425 { 426 int err; 427 struct input_polled_dev *poll_dev; 428 poll_dev = input_allocate_polled_device(); 429 430 if (!poll_dev) { 431 dev_err(&tj9->client->dev, 432 "Failed to allocate polled device\n"); 433 return -ENOMEM; 434 } 435 436 tj9->poll_dev = poll_dev; 437 tj9->input_dev = poll_dev->input; 438 439 poll_dev->private = tj9; 440 poll_dev->poll = kxtj9_poll; 441 poll_dev->open = kxtj9_polled_input_open; 442 poll_dev->close = kxtj9_polled_input_close; 443 444 kxtj9_init_input_device(tj9, poll_dev->input); 445 446 err = input_register_polled_device(poll_dev); 447 if (err) { 448 dev_err(&tj9->client->dev, 449 "Unable to register polled device, err=%d\n", err); 450 input_free_polled_device(poll_dev); 451 return err; 452 } 453 454 return 0; 455 } 456 457 static void __devexit kxtj9_teardown_polled_device(struct kxtj9_data *tj9) 458 { 459 input_unregister_polled_device(tj9->poll_dev); 460 input_free_polled_device(tj9->poll_dev); 461 } 462 463 #else 464 465 static inline int kxtj9_setup_polled_device(struct kxtj9_data *tj9) 466 { 467 return -ENOSYS; 468 } 469 470 static inline void kxtj9_teardown_polled_device(struct kxtj9_data *tj9) 471 { 472 } 473 474 #endif 475 476 static int __devinit kxtj9_verify(struct kxtj9_data *tj9) 477 { 478 int retval; 479 480 retval = kxtj9_device_power_on(tj9); 481 if (retval < 0) 482 return retval; 483 484 retval = i2c_smbus_read_byte_data(tj9->client, WHO_AM_I); 485 if (retval < 0) { 486 dev_err(&tj9->client->dev, "read err int source\n"); 487 goto out; 488 } 489 490 retval = retval != 0x06 ? -EIO : 0; 491 492 out: 493 kxtj9_device_power_off(tj9); 494 return retval; 495 } 496 497 static int __devinit kxtj9_probe(struct i2c_client *client, 498 const struct i2c_device_id *id) 499 { 500 const struct kxtj9_platform_data *pdata = client->dev.platform_data; 501 struct kxtj9_data *tj9; 502 int err; 503 504 if (!i2c_check_functionality(client->adapter, 505 I2C_FUNC_I2C | I2C_FUNC_SMBUS_BYTE_DATA)) { 506 dev_err(&client->dev, "client is not i2c capable\n"); 507 return -ENXIO; 508 } 509 510 if (!pdata) { 511 dev_err(&client->dev, "platform data is NULL; exiting\n"); 512 return -EINVAL; 513 } 514 515 tj9 = kzalloc(sizeof(*tj9), GFP_KERNEL); 516 if (!tj9) { 517 dev_err(&client->dev, 518 "failed to allocate memory for module data\n"); 519 return -ENOMEM; 520 } 521 522 tj9->client = client; 523 tj9->pdata = *pdata; 524 525 if (pdata->init) { 526 err = pdata->init(); 527 if (err < 0) 528 goto err_free_mem; 529 } 530 531 err = kxtj9_verify(tj9); 532 if (err < 0) { 533 dev_err(&client->dev, "device not recognized\n"); 534 goto err_pdata_exit; 535 } 536 537 i2c_set_clientdata(client, tj9); 538 539 tj9->ctrl_reg1 = tj9->pdata.res_12bit | tj9->pdata.g_range; 540 tj9->data_ctrl = tj9->pdata.data_odr_init; 541 542 if (client->irq) { 543 /* If in irq mode, populate INT_CTRL_REG1 and enable DRDY. */ 544 tj9->int_ctrl |= KXTJ9_IEN | KXTJ9_IEA | KXTJ9_IEL; 545 tj9->ctrl_reg1 |= DRDYE; 546 547 err = kxtj9_setup_input_device(tj9); 548 if (err) 549 goto err_pdata_exit; 550 551 err = request_threaded_irq(client->irq, NULL, kxtj9_isr, 552 IRQF_TRIGGER_RISING | IRQF_ONESHOT, 553 "kxtj9-irq", tj9); 554 if (err) { 555 dev_err(&client->dev, "request irq failed: %d\n", err); 556 goto err_destroy_input; 557 } 558 559 err = sysfs_create_group(&client->dev.kobj, &kxtj9_attribute_group); 560 if (err) { 561 dev_err(&client->dev, "sysfs create failed: %d\n", err); 562 goto err_free_irq; 563 } 564 565 } else { 566 err = kxtj9_setup_polled_device(tj9); 567 if (err) 568 goto err_pdata_exit; 569 } 570 571 return 0; 572 573 err_free_irq: 574 free_irq(client->irq, tj9); 575 err_destroy_input: 576 input_unregister_device(tj9->input_dev); 577 err_pdata_exit: 578 if (tj9->pdata.exit) 579 tj9->pdata.exit(); 580 err_free_mem: 581 kfree(tj9); 582 return err; 583 } 584 585 static int __devexit kxtj9_remove(struct i2c_client *client) 586 { 587 struct kxtj9_data *tj9 = i2c_get_clientdata(client); 588 589 if (client->irq) { 590 sysfs_remove_group(&client->dev.kobj, &kxtj9_attribute_group); 591 free_irq(client->irq, tj9); 592 input_unregister_device(tj9->input_dev); 593 } else { 594 kxtj9_teardown_polled_device(tj9); 595 } 596 597 if (tj9->pdata.exit) 598 tj9->pdata.exit(); 599 600 kfree(tj9); 601 602 return 0; 603 } 604 605 #ifdef CONFIG_PM_SLEEP 606 static int kxtj9_suspend(struct device *dev) 607 { 608 struct i2c_client *client = to_i2c_client(dev); 609 struct kxtj9_data *tj9 = i2c_get_clientdata(client); 610 struct input_dev *input_dev = tj9->input_dev; 611 612 mutex_lock(&input_dev->mutex); 613 614 if (input_dev->users) 615 kxtj9_disable(tj9); 616 617 mutex_unlock(&input_dev->mutex); 618 return 0; 619 } 620 621 static int kxtj9_resume(struct device *dev) 622 { 623 struct i2c_client *client = to_i2c_client(dev); 624 struct kxtj9_data *tj9 = i2c_get_clientdata(client); 625 struct input_dev *input_dev = tj9->input_dev; 626 int retval = 0; 627 628 mutex_lock(&input_dev->mutex); 629 630 if (input_dev->users) 631 kxtj9_enable(tj9); 632 633 mutex_unlock(&input_dev->mutex); 634 return retval; 635 } 636 #endif 637 638 static SIMPLE_DEV_PM_OPS(kxtj9_pm_ops, kxtj9_suspend, kxtj9_resume); 639 640 static const struct i2c_device_id kxtj9_id[] = { 641 { NAME, 0 }, 642 { }, 643 }; 644 645 MODULE_DEVICE_TABLE(i2c, kxtj9_id); 646 647 static struct i2c_driver kxtj9_driver = { 648 .driver = { 649 .name = NAME, 650 .owner = THIS_MODULE, 651 .pm = &kxtj9_pm_ops, 652 }, 653 .probe = kxtj9_probe, 654 .remove = __devexit_p(kxtj9_remove), 655 .id_table = kxtj9_id, 656 }; 657 658 static int __init kxtj9_init(void) 659 { 660 return i2c_add_driver(&kxtj9_driver); 661 } 662 module_init(kxtj9_init); 663 664 static void __exit kxtj9_exit(void) 665 { 666 i2c_del_driver(&kxtj9_driver); 667 } 668 module_exit(kxtj9_exit); 669 670 MODULE_DESCRIPTION("KXTJ9 accelerometer driver"); 671 MODULE_AUTHOR("Chris Hudson <chudson@kionix.com>"); 672 MODULE_LICENSE("GPL"); 673