1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2011 Kionix, Inc. 4 * Written by Chris Hudson <chudson@kionix.com> 5 */ 6 7 #include <linux/delay.h> 8 #include <linux/i2c.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> 14 #include <linux/input-polldev.h> 15 16 #define NAME "kxtj9" 17 #define G_MAX 8000 18 /* OUTPUT REGISTERS */ 19 #define XOUT_L 0x06 20 #define WHO_AM_I 0x0F 21 /* CONTROL REGISTERS */ 22 #define INT_REL 0x1A 23 #define CTRL_REG1 0x1B 24 #define INT_CTRL1 0x1E 25 #define DATA_CTRL 0x21 26 /* CONTROL REGISTER 1 BITS */ 27 #define PC1_OFF 0x7F 28 #define PC1_ON (1 << 7) 29 /* Data ready funtion enable bit: set during probe if using irq mode */ 30 #define DRDYE (1 << 5) 31 /* DATA CONTROL REGISTER BITS */ 32 #define ODR12_5F 0 33 #define ODR25F 1 34 #define ODR50F 2 35 #define ODR100F 3 36 #define ODR200F 4 37 #define ODR400F 5 38 #define ODR800F 6 39 /* INTERRUPT CONTROL REGISTER 1 BITS */ 40 /* Set these during probe if using irq mode */ 41 #define KXTJ9_IEL (1 << 3) 42 #define KXTJ9_IEA (1 << 4) 43 #define KXTJ9_IEN (1 << 5) 44 /* INPUT_ABS CONSTANTS */ 45 #define FUZZ 3 46 #define FLAT 3 47 /* RESUME STATE INDICES */ 48 #define RES_DATA_CTRL 0 49 #define RES_CTRL_REG1 1 50 #define RES_INT_CTRL1 2 51 #define RESUME_ENTRIES 3 52 53 /* 54 * The following table lists the maximum appropriate poll interval for each 55 * available output data rate. 56 */ 57 static const struct { 58 unsigned int cutoff; 59 u8 mask; 60 } kxtj9_odr_table[] = { 61 { 3, ODR800F }, 62 { 5, ODR400F }, 63 { 10, ODR200F }, 64 { 20, ODR100F }, 65 { 40, ODR50F }, 66 { 80, ODR25F }, 67 { 0, ODR12_5F}, 68 }; 69 70 struct kxtj9_data { 71 struct i2c_client *client; 72 struct kxtj9_platform_data pdata; 73 struct input_dev *input_dev; 74 #ifdef CONFIG_INPUT_KXTJ9_POLLED_MODE 75 struct input_polled_dev *poll_dev; 76 #endif 77 unsigned int last_poll_interval; 78 u8 shift; 79 u8 ctrl_reg1; 80 u8 data_ctrl; 81 u8 int_ctrl; 82 }; 83 84 static int kxtj9_i2c_read(struct kxtj9_data *tj9, u8 addr, u8 *data, int len) 85 { 86 struct i2c_msg msgs[] = { 87 { 88 .addr = tj9->client->addr, 89 .flags = tj9->client->flags, 90 .len = 1, 91 .buf = &addr, 92 }, 93 { 94 .addr = tj9->client->addr, 95 .flags = tj9->client->flags | I2C_M_RD, 96 .len = len, 97 .buf = data, 98 }, 99 }; 100 101 return i2c_transfer(tj9->client->adapter, msgs, 2); 102 } 103 104 static void kxtj9_report_acceleration_data(struct kxtj9_data *tj9) 105 { 106 s16 acc_data[3]; /* Data bytes from hardware xL, xH, yL, yH, zL, zH */ 107 s16 x, y, z; 108 int err; 109 110 err = kxtj9_i2c_read(tj9, XOUT_L, (u8 *)acc_data, 6); 111 if (err < 0) 112 dev_err(&tj9->client->dev, "accelerometer data read failed\n"); 113 114 x = le16_to_cpu(acc_data[tj9->pdata.axis_map_x]); 115 y = le16_to_cpu(acc_data[tj9->pdata.axis_map_y]); 116 z = le16_to_cpu(acc_data[tj9->pdata.axis_map_z]); 117 118 x >>= tj9->shift; 119 y >>= tj9->shift; 120 z >>= tj9->shift; 121 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); 126 } 127 128 static irqreturn_t kxtj9_isr(int irq, void *dev) 129 { 130 struct kxtj9_data *tj9 = dev; 131 int err; 132 133 /* data ready is the only possible interrupt type */ 134 kxtj9_report_acceleration_data(tj9); 135 136 err = i2c_smbus_read_byte_data(tj9->client, INT_REL); 137 if (err < 0) 138 dev_err(&tj9->client->dev, 139 "error clearing interrupt status: %d\n", err); 140 141 return IRQ_HANDLED; 142 } 143 144 static int kxtj9_update_g_range(struct kxtj9_data *tj9, u8 new_g_range) 145 { 146 switch (new_g_range) { 147 case KXTJ9_G_2G: 148 tj9->shift = 4; 149 break; 150 case KXTJ9_G_4G: 151 tj9->shift = 3; 152 break; 153 case KXTJ9_G_8G: 154 tj9->shift = 2; 155 break; 156 default: 157 return -EINVAL; 158 } 159 160 tj9->ctrl_reg1 &= 0xe7; 161 tj9->ctrl_reg1 |= new_g_range; 162 163 return 0; 164 } 165 166 static int kxtj9_update_odr(struct kxtj9_data *tj9, unsigned int poll_interval) 167 { 168 int err; 169 int i; 170 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) 175 break; 176 } 177 178 err = i2c_smbus_write_byte_data(tj9->client, CTRL_REG1, 0); 179 if (err < 0) 180 return err; 181 182 err = i2c_smbus_write_byte_data(tj9->client, DATA_CTRL, tj9->data_ctrl); 183 if (err < 0) 184 return err; 185 186 err = i2c_smbus_write_byte_data(tj9->client, CTRL_REG1, tj9->ctrl_reg1); 187 if (err < 0) 188 return err; 189 190 return 0; 191 } 192 193 static int kxtj9_device_power_on(struct kxtj9_data *tj9) 194 { 195 if (tj9->pdata.power_on) 196 return tj9->pdata.power_on(); 197 198 return 0; 199 } 200 201 static void kxtj9_device_power_off(struct kxtj9_data *tj9) 202 { 203 int err; 204 205 tj9->ctrl_reg1 &= PC1_OFF; 206 err = i2c_smbus_write_byte_data(tj9->client, CTRL_REG1, tj9->ctrl_reg1); 207 if (err < 0) 208 dev_err(&tj9->client->dev, "soft power off failed\n"); 209 210 if (tj9->pdata.power_off) 211 tj9->pdata.power_off(); 212 } 213 214 static int kxtj9_enable(struct kxtj9_data *tj9) 215 { 216 int err; 217 218 err = kxtj9_device_power_on(tj9); 219 if (err < 0) 220 return err; 221 222 /* ensure that PC1 is cleared before updating control registers */ 223 err = i2c_smbus_write_byte_data(tj9->client, CTRL_REG1, 0); 224 if (err < 0) 225 return err; 226 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); 231 if (err < 0) 232 return err; 233 } 234 235 err = kxtj9_update_g_range(tj9, tj9->pdata.g_range); 236 if (err < 0) 237 return err; 238 239 /* turn on outputs */ 240 tj9->ctrl_reg1 |= PC1_ON; 241 err = i2c_smbus_write_byte_data(tj9->client, CTRL_REG1, tj9->ctrl_reg1); 242 if (err < 0) 243 return err; 244 245 err = kxtj9_update_odr(tj9, tj9->last_poll_interval); 246 if (err < 0) 247 return err; 248 249 /* clear initial interrupt if in irq mode */ 250 if (tj9->client->irq) { 251 err = i2c_smbus_read_byte_data(tj9->client, INT_REL); 252 if (err < 0) { 253 dev_err(&tj9->client->dev, 254 "error clearing interrupt: %d\n", err); 255 goto fail; 256 } 257 } 258 259 return 0; 260 261 fail: 262 kxtj9_device_power_off(tj9); 263 return err; 264 } 265 266 static void kxtj9_disable(struct kxtj9_data *tj9) 267 { 268 kxtj9_device_power_off(tj9); 269 } 270 271 static int kxtj9_input_open(struct input_dev *input) 272 { 273 struct kxtj9_data *tj9 = input_get_drvdata(input); 274 275 return kxtj9_enable(tj9); 276 } 277 278 static void kxtj9_input_close(struct input_dev *dev) 279 { 280 struct kxtj9_data *tj9 = input_get_drvdata(dev); 281 282 kxtj9_disable(tj9); 283 } 284 285 static void kxtj9_init_input_device(struct kxtj9_data *tj9, 286 struct input_dev *input_dev) 287 { 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); 292 293 input_dev->name = "kxtj9_accel"; 294 input_dev->id.bustype = BUS_I2C; 295 input_dev->dev.parent = &tj9->client->dev; 296 } 297 298 static int kxtj9_setup_input_device(struct kxtj9_data *tj9) 299 { 300 struct input_dev *input_dev; 301 int err; 302 303 input_dev = input_allocate_device(); 304 if (!input_dev) { 305 dev_err(&tj9->client->dev, "input device allocate failed\n"); 306 return -ENOMEM; 307 } 308 309 tj9->input_dev = input_dev; 310 311 input_dev->open = kxtj9_input_open; 312 input_dev->close = kxtj9_input_close; 313 input_set_drvdata(input_dev, tj9); 314 315 kxtj9_init_input_device(tj9, input_dev); 316 317 err = input_register_device(tj9->input_dev); 318 if (err) { 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); 323 return err; 324 } 325 326 return 0; 327 } 328 329 /* 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 337 * interval. 338 */ 339 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) 343 { 344 struct i2c_client *client = to_i2c_client(dev); 345 struct kxtj9_data *tj9 = i2c_get_clientdata(client); 346 347 return sprintf(buf, "%d\n", tj9->last_poll_interval); 348 } 349 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) 353 { 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; 358 int error; 359 360 error = kstrtouint(buf, 10, &interval); 361 if (error < 0) 362 return error; 363 364 /* Lock the device to prevent races with open/close (and itself) */ 365 mutex_lock(&input_dev->mutex); 366 367 disable_irq(client->irq); 368 369 /* 370 * Set current interval to the greater of the minimum interval or 371 * the requested interval 372 */ 373 tj9->last_poll_interval = max(interval, tj9->pdata.min_interval); 374 375 kxtj9_update_odr(tj9, tj9->last_poll_interval); 376 377 enable_irq(client->irq); 378 mutex_unlock(&input_dev->mutex); 379 380 return count; 381 } 382 383 static DEVICE_ATTR(poll, S_IRUGO|S_IWUSR, kxtj9_get_poll, kxtj9_set_poll); 384 385 static struct attribute *kxtj9_attributes[] = { 386 &dev_attr_poll.attr, 387 NULL 388 }; 389 390 static struct attribute_group kxtj9_attribute_group = { 391 .attrs = kxtj9_attributes 392 }; 393 394 395 #ifdef CONFIG_INPUT_KXTJ9_POLLED_MODE 396 static void kxtj9_poll(struct input_polled_dev *dev) 397 { 398 struct kxtj9_data *tj9 = dev->private; 399 unsigned int poll_interval = dev->poll_interval; 400 401 kxtj9_report_acceleration_data(tj9); 402 403 if (poll_interval != tj9->last_poll_interval) { 404 kxtj9_update_odr(tj9, poll_interval); 405 tj9->last_poll_interval = poll_interval; 406 } 407 } 408 409 static void kxtj9_polled_input_open(struct input_polled_dev *dev) 410 { 411 struct kxtj9_data *tj9 = dev->private; 412 413 kxtj9_enable(tj9); 414 } 415 416 static void kxtj9_polled_input_close(struct input_polled_dev *dev) 417 { 418 struct kxtj9_data *tj9 = dev->private; 419 420 kxtj9_disable(tj9); 421 } 422 423 static int kxtj9_setup_polled_device(struct kxtj9_data *tj9) 424 { 425 int err; 426 struct input_polled_dev *poll_dev; 427 poll_dev = input_allocate_polled_device(); 428 429 if (!poll_dev) { 430 dev_err(&tj9->client->dev, 431 "Failed to allocate polled device\n"); 432 return -ENOMEM; 433 } 434 435 tj9->poll_dev = poll_dev; 436 tj9->input_dev = poll_dev->input; 437 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; 442 443 kxtj9_init_input_device(tj9, poll_dev->input); 444 445 err = input_register_polled_device(poll_dev); 446 if (err) { 447 dev_err(&tj9->client->dev, 448 "Unable to register polled device, err=%d\n", err); 449 input_free_polled_device(poll_dev); 450 return err; 451 } 452 453 return 0; 454 } 455 456 static void kxtj9_teardown_polled_device(struct kxtj9_data *tj9) 457 { 458 input_unregister_polled_device(tj9->poll_dev); 459 input_free_polled_device(tj9->poll_dev); 460 } 461 462 #else 463 464 static inline int kxtj9_setup_polled_device(struct kxtj9_data *tj9) 465 { 466 return -ENOSYS; 467 } 468 469 static inline void kxtj9_teardown_polled_device(struct kxtj9_data *tj9) 470 { 471 } 472 473 #endif 474 475 static int kxtj9_verify(struct kxtj9_data *tj9) 476 { 477 int retval; 478 479 retval = kxtj9_device_power_on(tj9); 480 if (retval < 0) 481 return retval; 482 483 retval = i2c_smbus_read_byte_data(tj9->client, WHO_AM_I); 484 if (retval < 0) { 485 dev_err(&tj9->client->dev, "read err int source\n"); 486 goto out; 487 } 488 489 retval = (retval != 0x07 && retval != 0x08) ? -EIO : 0; 490 491 out: 492 kxtj9_device_power_off(tj9); 493 return retval; 494 } 495 496 static int kxtj9_probe(struct i2c_client *client, 497 const struct i2c_device_id *id) 498 { 499 const struct kxtj9_platform_data *pdata = 500 dev_get_platdata(&client->dev); 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->last_poll_interval = tj9->pdata.init_interval; 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 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 static int __maybe_unused kxtj9_suspend(struct device *dev) 606 { 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; 610 611 mutex_lock(&input_dev->mutex); 612 613 if (input_dev->users) 614 kxtj9_disable(tj9); 615 616 mutex_unlock(&input_dev->mutex); 617 return 0; 618 } 619 620 static int __maybe_unused kxtj9_resume(struct device *dev) 621 { 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; 625 626 mutex_lock(&input_dev->mutex); 627 628 if (input_dev->users) 629 kxtj9_enable(tj9); 630 631 mutex_unlock(&input_dev->mutex); 632 return 0; 633 } 634 635 static SIMPLE_DEV_PM_OPS(kxtj9_pm_ops, kxtj9_suspend, kxtj9_resume); 636 637 static const struct i2c_device_id kxtj9_id[] = { 638 { NAME, 0 }, 639 { }, 640 }; 641 642 MODULE_DEVICE_TABLE(i2c, kxtj9_id); 643 644 static struct i2c_driver kxtj9_driver = { 645 .driver = { 646 .name = NAME, 647 .pm = &kxtj9_pm_ops, 648 }, 649 .probe = kxtj9_probe, 650 .remove = kxtj9_remove, 651 .id_table = kxtj9_id, 652 }; 653 654 module_i2c_driver(kxtj9_driver); 655 656 MODULE_DESCRIPTION("KXTJ9 accelerometer driver"); 657 MODULE_AUTHOR("Chris Hudson <chudson@kionix.com>"); 658 MODULE_LICENSE("GPL"); 659