1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * KXCJK-1013 3-axis accelerometer driver 4 * Copyright (c) 2014, Intel Corporation. 5 */ 6 7 #include <linux/module.h> 8 #include <linux/i2c.h> 9 #include <linux/interrupt.h> 10 #include <linux/delay.h> 11 #include <linux/bitops.h> 12 #include <linux/slab.h> 13 #include <linux/string.h> 14 #include <linux/acpi.h> 15 #include <linux/pm.h> 16 #include <linux/pm_runtime.h> 17 #include <linux/iio/iio.h> 18 #include <linux/iio/sysfs.h> 19 #include <linux/iio/buffer.h> 20 #include <linux/iio/trigger.h> 21 #include <linux/iio/events.h> 22 #include <linux/iio/trigger_consumer.h> 23 #include <linux/iio/triggered_buffer.h> 24 #include <linux/iio/accel/kxcjk_1013.h> 25 26 #define KXCJK1013_DRV_NAME "kxcjk1013" 27 #define KXCJK1013_IRQ_NAME "kxcjk1013_event" 28 29 #define KXTF9_REG_HP_XOUT_L 0x00 30 #define KXTF9_REG_HP_XOUT_H 0x01 31 #define KXTF9_REG_HP_YOUT_L 0x02 32 #define KXTF9_REG_HP_YOUT_H 0x03 33 #define KXTF9_REG_HP_ZOUT_L 0x04 34 #define KXTF9_REG_HP_ZOUT_H 0x05 35 36 #define KXCJK1013_REG_XOUT_L 0x06 37 /* 38 * From low byte X axis register, all the other addresses of Y and Z can be 39 * obtained by just applying axis offset. The following axis defines are just 40 * provide clarity, but not used. 41 */ 42 #define KXCJK1013_REG_XOUT_H 0x07 43 #define KXCJK1013_REG_YOUT_L 0x08 44 #define KXCJK1013_REG_YOUT_H 0x09 45 #define KXCJK1013_REG_ZOUT_L 0x0A 46 #define KXCJK1013_REG_ZOUT_H 0x0B 47 48 #define KXCJK1013_REG_DCST_RESP 0x0C 49 #define KXCJK1013_REG_WHO_AM_I 0x0F 50 #define KXTF9_REG_TILT_POS_CUR 0x10 51 #define KXTF9_REG_TILT_POS_PREV 0x11 52 #define KXTF9_REG_INT_SRC1 0x15 53 #define KXCJK1013_REG_INT_SRC1 0x16 /* compatible, but called INT_SRC2 in KXTF9 ds */ 54 #define KXCJK1013_REG_INT_SRC2 0x17 55 #define KXCJK1013_REG_STATUS_REG 0x18 56 #define KXCJK1013_REG_INT_REL 0x1A 57 #define KXCJK1013_REG_CTRL1 0x1B 58 #define KXTF9_REG_CTRL2 0x1C 59 #define KXCJK1013_REG_CTRL2 0x1D /* mostly compatible, CTRL_REG3 in KTXF9 ds */ 60 #define KXCJK1013_REG_INT_CTRL1 0x1E 61 #define KXCJK1013_REG_INT_CTRL2 0x1F 62 #define KXTF9_REG_INT_CTRL3 0x20 63 #define KXCJK1013_REG_DATA_CTRL 0x21 64 #define KXTF9_REG_TILT_TIMER 0x28 65 #define KXCJK1013_REG_WAKE_TIMER 0x29 66 #define KXTF9_REG_TDT_TIMER 0x2B 67 #define KXTF9_REG_TDT_THRESH_H 0x2C 68 #define KXTF9_REG_TDT_THRESH_L 0x2D 69 #define KXTF9_REG_TDT_TAP_TIMER 0x2E 70 #define KXTF9_REG_TDT_TOTAL_TIMER 0x2F 71 #define KXTF9_REG_TDT_LATENCY_TIMER 0x30 72 #define KXTF9_REG_TDT_WINDOW_TIMER 0x31 73 #define KXCJK1013_REG_SELF_TEST 0x3A 74 #define KXTF9_REG_WAKE_THRESH 0x5A 75 #define KXTF9_REG_TILT_ANGLE 0x5C 76 #define KXTF9_REG_HYST_SET 0x5F 77 #define KXCJK1013_REG_WAKE_THRES 0x6A 78 79 #define KXCJK1013_REG_CTRL1_BIT_PC1 BIT(7) 80 #define KXCJK1013_REG_CTRL1_BIT_RES BIT(6) 81 #define KXCJK1013_REG_CTRL1_BIT_DRDY BIT(5) 82 #define KXCJK1013_REG_CTRL1_BIT_GSEL1 BIT(4) 83 #define KXCJK1013_REG_CTRL1_BIT_GSEL0 BIT(3) 84 #define KXCJK1013_REG_CTRL1_BIT_WUFE BIT(1) 85 86 #define KXCJK1013_REG_INT_CTRL1_BIT_IEU BIT(2) /* KXTF9 */ 87 #define KXCJK1013_REG_INT_CTRL1_BIT_IEL BIT(3) 88 #define KXCJK1013_REG_INT_CTRL1_BIT_IEA BIT(4) 89 #define KXCJK1013_REG_INT_CTRL1_BIT_IEN BIT(5) 90 91 #define KXTF9_REG_TILT_BIT_LEFT_EDGE BIT(5) 92 #define KXTF9_REG_TILT_BIT_RIGHT_EDGE BIT(4) 93 #define KXTF9_REG_TILT_BIT_LOWER_EDGE BIT(3) 94 #define KXTF9_REG_TILT_BIT_UPPER_EDGE BIT(2) 95 #define KXTF9_REG_TILT_BIT_FACE_DOWN BIT(1) 96 #define KXTF9_REG_TILT_BIT_FACE_UP BIT(0) 97 98 #define KXCJK1013_DATA_MASK_12_BIT 0x0FFF 99 #define KXCJK1013_MAX_STARTUP_TIME_US 100000 100 101 #define KXCJK1013_SLEEP_DELAY_MS 2000 102 103 #define KXCJK1013_REG_INT_SRC1_BIT_TPS BIT(0) /* KXTF9 */ 104 #define KXCJK1013_REG_INT_SRC1_BIT_WUFS BIT(1) 105 #define KXCJK1013_REG_INT_SRC1_MASK_TDTS (BIT(2) | BIT(3)) /* KXTF9 */ 106 #define KXCJK1013_REG_INT_SRC1_TAP_NONE 0 107 #define KXCJK1013_REG_INT_SRC1_TAP_SINGLE BIT(2) 108 #define KXCJK1013_REG_INT_SRC1_TAP_DOUBLE BIT(3) 109 #define KXCJK1013_REG_INT_SRC1_BIT_DRDY BIT(4) 110 111 /* KXCJK: INT_SOURCE2: motion detect, KXTF9: INT_SRC_REG1: tap detect */ 112 #define KXCJK1013_REG_INT_SRC2_BIT_ZP BIT(0) 113 #define KXCJK1013_REG_INT_SRC2_BIT_ZN BIT(1) 114 #define KXCJK1013_REG_INT_SRC2_BIT_YP BIT(2) 115 #define KXCJK1013_REG_INT_SRC2_BIT_YN BIT(3) 116 #define KXCJK1013_REG_INT_SRC2_BIT_XP BIT(4) 117 #define KXCJK1013_REG_INT_SRC2_BIT_XN BIT(5) 118 119 #define KXCJK1013_DEFAULT_WAKE_THRES 1 120 121 enum kx_chipset { 122 KXCJK1013, 123 KXCJ91008, 124 KXTJ21009, 125 KXTF9, 126 KX_MAX_CHIPS /* this must be last */ 127 }; 128 129 enum kx_acpi_type { 130 ACPI_GENERIC, 131 ACPI_SMO8500, 132 ACPI_KIOX010A, 133 }; 134 135 struct kxcjk1013_data { 136 struct i2c_client *client; 137 struct iio_trigger *dready_trig; 138 struct iio_trigger *motion_trig; 139 struct iio_mount_matrix orientation; 140 struct mutex mutex; 141 s16 buffer[8]; 142 u8 odr_bits; 143 u8 range; 144 int wake_thres; 145 int wake_dur; 146 bool active_high_intr; 147 bool dready_trigger_on; 148 int ev_enable_state; 149 bool motion_trigger_on; 150 int64_t timestamp; 151 enum kx_chipset chipset; 152 enum kx_acpi_type acpi_type; 153 }; 154 155 enum kxcjk1013_axis { 156 AXIS_X, 157 AXIS_Y, 158 AXIS_Z, 159 AXIS_MAX, 160 }; 161 162 enum kxcjk1013_mode { 163 STANDBY, 164 OPERATION, 165 }; 166 167 enum kxcjk1013_range { 168 KXCJK1013_RANGE_2G, 169 KXCJK1013_RANGE_4G, 170 KXCJK1013_RANGE_8G, 171 }; 172 173 struct kx_odr_map { 174 int val; 175 int val2; 176 int odr_bits; 177 int wuf_bits; 178 }; 179 180 static const struct kx_odr_map samp_freq_table[] = { 181 { 0, 781000, 0x08, 0x00 }, 182 { 1, 563000, 0x09, 0x01 }, 183 { 3, 125000, 0x0A, 0x02 }, 184 { 6, 250000, 0x0B, 0x03 }, 185 { 12, 500000, 0x00, 0x04 }, 186 { 25, 0, 0x01, 0x05 }, 187 { 50, 0, 0x02, 0x06 }, 188 { 100, 0, 0x03, 0x06 }, 189 { 200, 0, 0x04, 0x06 }, 190 { 400, 0, 0x05, 0x06 }, 191 { 800, 0, 0x06, 0x06 }, 192 { 1600, 0, 0x07, 0x06 }, 193 }; 194 195 static const char *const kxcjk1013_samp_freq_avail = 196 "0.781000 1.563000 3.125000 6.250000 12.500000 25 50 100 200 400 800 1600"; 197 198 static const struct kx_odr_map kxtf9_samp_freq_table[] = { 199 { 25, 0, 0x01, 0x00 }, 200 { 50, 0, 0x02, 0x01 }, 201 { 100, 0, 0x03, 0x01 }, 202 { 200, 0, 0x04, 0x01 }, 203 { 400, 0, 0x05, 0x01 }, 204 { 800, 0, 0x06, 0x01 }, 205 }; 206 207 static const char *const kxtf9_samp_freq_avail = 208 "25 50 100 200 400 800"; 209 210 /* Refer to section 4 of the specification */ 211 static const struct { 212 int odr_bits; 213 int usec; 214 } odr_start_up_times[KX_MAX_CHIPS][12] = { 215 /* KXCJK-1013 */ 216 { 217 {0x08, 100000}, 218 {0x09, 100000}, 219 {0x0A, 100000}, 220 {0x0B, 100000}, 221 {0, 80000}, 222 {0x01, 41000}, 223 {0x02, 21000}, 224 {0x03, 11000}, 225 {0x04, 6400}, 226 {0x05, 3900}, 227 {0x06, 2700}, 228 {0x07, 2100}, 229 }, 230 /* KXCJ9-1008 */ 231 { 232 {0x08, 100000}, 233 {0x09, 100000}, 234 {0x0A, 100000}, 235 {0x0B, 100000}, 236 {0, 80000}, 237 {0x01, 41000}, 238 {0x02, 21000}, 239 {0x03, 11000}, 240 {0x04, 6400}, 241 {0x05, 3900}, 242 {0x06, 2700}, 243 {0x07, 2100}, 244 }, 245 /* KXCTJ2-1009 */ 246 { 247 {0x08, 1240000}, 248 {0x09, 621000}, 249 {0x0A, 309000}, 250 {0x0B, 151000}, 251 {0, 80000}, 252 {0x01, 41000}, 253 {0x02, 21000}, 254 {0x03, 11000}, 255 {0x04, 6000}, 256 {0x05, 4000}, 257 {0x06, 3000}, 258 {0x07, 2000}, 259 }, 260 /* KXTF9 */ 261 { 262 {0x01, 81000}, 263 {0x02, 41000}, 264 {0x03, 21000}, 265 {0x04, 11000}, 266 {0x05, 5100}, 267 {0x06, 2700}, 268 }, 269 }; 270 271 static const struct { 272 u16 scale; 273 u8 gsel_0; 274 u8 gsel_1; 275 } KXCJK1013_scale_table[] = { {9582, 0, 0}, 276 {19163, 1, 0}, 277 {38326, 0, 1} }; 278 279 #ifdef CONFIG_ACPI 280 enum kiox010a_fn_index { 281 KIOX010A_SET_LAPTOP_MODE = 1, 282 KIOX010A_SET_TABLET_MODE = 2, 283 }; 284 285 static int kiox010a_dsm(struct device *dev, int fn_index) 286 { 287 acpi_handle handle = ACPI_HANDLE(dev); 288 guid_t kiox010a_dsm_guid; 289 union acpi_object *obj; 290 291 if (!handle) 292 return -ENODEV; 293 294 guid_parse("1f339696-d475-4e26-8cad-2e9f8e6d7a91", &kiox010a_dsm_guid); 295 296 obj = acpi_evaluate_dsm(handle, &kiox010a_dsm_guid, 1, fn_index, NULL); 297 if (!obj) 298 return -EIO; 299 300 ACPI_FREE(obj); 301 return 0; 302 } 303 #endif 304 305 static int kxcjk1013_set_mode(struct kxcjk1013_data *data, 306 enum kxcjk1013_mode mode) 307 { 308 int ret; 309 310 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_CTRL1); 311 if (ret < 0) { 312 dev_err(&data->client->dev, "Error reading reg_ctrl1\n"); 313 return ret; 314 } 315 316 if (mode == STANDBY) 317 ret &= ~KXCJK1013_REG_CTRL1_BIT_PC1; 318 else 319 ret |= KXCJK1013_REG_CTRL1_BIT_PC1; 320 321 ret = i2c_smbus_write_byte_data(data->client, 322 KXCJK1013_REG_CTRL1, ret); 323 if (ret < 0) { 324 dev_err(&data->client->dev, "Error writing reg_ctrl1\n"); 325 return ret; 326 } 327 328 return 0; 329 } 330 331 static int kxcjk1013_get_mode(struct kxcjk1013_data *data, 332 enum kxcjk1013_mode *mode) 333 { 334 int ret; 335 336 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_CTRL1); 337 if (ret < 0) { 338 dev_err(&data->client->dev, "Error reading reg_ctrl1\n"); 339 return ret; 340 } 341 342 if (ret & KXCJK1013_REG_CTRL1_BIT_PC1) 343 *mode = OPERATION; 344 else 345 *mode = STANDBY; 346 347 return 0; 348 } 349 350 static int kxcjk1013_set_range(struct kxcjk1013_data *data, int range_index) 351 { 352 int ret; 353 354 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_CTRL1); 355 if (ret < 0) { 356 dev_err(&data->client->dev, "Error reading reg_ctrl1\n"); 357 return ret; 358 } 359 360 ret &= ~(KXCJK1013_REG_CTRL1_BIT_GSEL0 | 361 KXCJK1013_REG_CTRL1_BIT_GSEL1); 362 ret |= (KXCJK1013_scale_table[range_index].gsel_0 << 3); 363 ret |= (KXCJK1013_scale_table[range_index].gsel_1 << 4); 364 365 ret = i2c_smbus_write_byte_data(data->client, 366 KXCJK1013_REG_CTRL1, 367 ret); 368 if (ret < 0) { 369 dev_err(&data->client->dev, "Error writing reg_ctrl1\n"); 370 return ret; 371 } 372 373 data->range = range_index; 374 375 return 0; 376 } 377 378 static int kxcjk1013_chip_init(struct kxcjk1013_data *data) 379 { 380 int ret; 381 382 #ifdef CONFIG_ACPI 383 if (data->acpi_type == ACPI_KIOX010A) { 384 /* Make sure the kbd and touchpad on 2-in-1s using 2 KXCJ91008-s work */ 385 kiox010a_dsm(&data->client->dev, KIOX010A_SET_LAPTOP_MODE); 386 } 387 #endif 388 389 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_WHO_AM_I); 390 if (ret < 0) { 391 dev_err(&data->client->dev, "Error reading who_am_i\n"); 392 return ret; 393 } 394 395 dev_dbg(&data->client->dev, "KXCJK1013 Chip Id %x\n", ret); 396 397 ret = kxcjk1013_set_mode(data, STANDBY); 398 if (ret < 0) 399 return ret; 400 401 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_CTRL1); 402 if (ret < 0) { 403 dev_err(&data->client->dev, "Error reading reg_ctrl1\n"); 404 return ret; 405 } 406 407 /* Set 12 bit mode */ 408 ret |= KXCJK1013_REG_CTRL1_BIT_RES; 409 410 ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_CTRL1, 411 ret); 412 if (ret < 0) { 413 dev_err(&data->client->dev, "Error reading reg_ctrl\n"); 414 return ret; 415 } 416 417 /* Setting range to 4G */ 418 ret = kxcjk1013_set_range(data, KXCJK1013_RANGE_4G); 419 if (ret < 0) 420 return ret; 421 422 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_DATA_CTRL); 423 if (ret < 0) { 424 dev_err(&data->client->dev, "Error reading reg_data_ctrl\n"); 425 return ret; 426 } 427 428 data->odr_bits = ret; 429 430 /* Set up INT polarity */ 431 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_INT_CTRL1); 432 if (ret < 0) { 433 dev_err(&data->client->dev, "Error reading reg_int_ctrl1\n"); 434 return ret; 435 } 436 437 if (data->active_high_intr) 438 ret |= KXCJK1013_REG_INT_CTRL1_BIT_IEA; 439 else 440 ret &= ~KXCJK1013_REG_INT_CTRL1_BIT_IEA; 441 442 ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_INT_CTRL1, 443 ret); 444 if (ret < 0) { 445 dev_err(&data->client->dev, "Error writing reg_int_ctrl1\n"); 446 return ret; 447 } 448 449 ret = kxcjk1013_set_mode(data, OPERATION); 450 if (ret < 0) 451 return ret; 452 453 data->wake_thres = KXCJK1013_DEFAULT_WAKE_THRES; 454 455 return 0; 456 } 457 458 #ifdef CONFIG_PM 459 static int kxcjk1013_get_startup_times(struct kxcjk1013_data *data) 460 { 461 int i; 462 int idx = data->chipset; 463 464 for (i = 0; i < ARRAY_SIZE(odr_start_up_times[idx]); ++i) { 465 if (odr_start_up_times[idx][i].odr_bits == data->odr_bits) 466 return odr_start_up_times[idx][i].usec; 467 } 468 469 return KXCJK1013_MAX_STARTUP_TIME_US; 470 } 471 #endif 472 473 static int kxcjk1013_set_power_state(struct kxcjk1013_data *data, bool on) 474 { 475 #ifdef CONFIG_PM 476 int ret; 477 478 if (on) 479 ret = pm_runtime_get_sync(&data->client->dev); 480 else { 481 pm_runtime_mark_last_busy(&data->client->dev); 482 ret = pm_runtime_put_autosuspend(&data->client->dev); 483 } 484 if (ret < 0) { 485 dev_err(&data->client->dev, 486 "Failed: %s for %d\n", __func__, on); 487 if (on) 488 pm_runtime_put_noidle(&data->client->dev); 489 return ret; 490 } 491 #endif 492 493 return 0; 494 } 495 496 static int kxcjk1013_chip_update_thresholds(struct kxcjk1013_data *data) 497 { 498 int waketh_reg, ret; 499 500 ret = i2c_smbus_write_byte_data(data->client, 501 KXCJK1013_REG_WAKE_TIMER, 502 data->wake_dur); 503 if (ret < 0) { 504 dev_err(&data->client->dev, 505 "Error writing reg_wake_timer\n"); 506 return ret; 507 } 508 509 waketh_reg = data->chipset == KXTF9 ? 510 KXTF9_REG_WAKE_THRESH : KXCJK1013_REG_WAKE_THRES; 511 ret = i2c_smbus_write_byte_data(data->client, waketh_reg, 512 data->wake_thres); 513 if (ret < 0) { 514 dev_err(&data->client->dev, "Error writing reg_wake_thres\n"); 515 return ret; 516 } 517 518 return 0; 519 } 520 521 static int kxcjk1013_setup_any_motion_interrupt(struct kxcjk1013_data *data, 522 bool status) 523 { 524 int ret; 525 enum kxcjk1013_mode store_mode; 526 527 ret = kxcjk1013_get_mode(data, &store_mode); 528 if (ret < 0) 529 return ret; 530 531 /* This is requirement by spec to change state to STANDBY */ 532 ret = kxcjk1013_set_mode(data, STANDBY); 533 if (ret < 0) 534 return ret; 535 536 ret = kxcjk1013_chip_update_thresholds(data); 537 if (ret < 0) 538 return ret; 539 540 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_INT_CTRL1); 541 if (ret < 0) { 542 dev_err(&data->client->dev, "Error reading reg_int_ctrl1\n"); 543 return ret; 544 } 545 546 if (status) 547 ret |= KXCJK1013_REG_INT_CTRL1_BIT_IEN; 548 else 549 ret &= ~KXCJK1013_REG_INT_CTRL1_BIT_IEN; 550 551 ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_INT_CTRL1, 552 ret); 553 if (ret < 0) { 554 dev_err(&data->client->dev, "Error writing reg_int_ctrl1\n"); 555 return ret; 556 } 557 558 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_CTRL1); 559 if (ret < 0) { 560 dev_err(&data->client->dev, "Error reading reg_ctrl1\n"); 561 return ret; 562 } 563 564 if (status) 565 ret |= KXCJK1013_REG_CTRL1_BIT_WUFE; 566 else 567 ret &= ~KXCJK1013_REG_CTRL1_BIT_WUFE; 568 569 ret = i2c_smbus_write_byte_data(data->client, 570 KXCJK1013_REG_CTRL1, ret); 571 if (ret < 0) { 572 dev_err(&data->client->dev, "Error writing reg_ctrl1\n"); 573 return ret; 574 } 575 576 if (store_mode == OPERATION) { 577 ret = kxcjk1013_set_mode(data, OPERATION); 578 if (ret < 0) 579 return ret; 580 } 581 582 return 0; 583 } 584 585 static int kxcjk1013_setup_new_data_interrupt(struct kxcjk1013_data *data, 586 bool status) 587 { 588 int ret; 589 enum kxcjk1013_mode store_mode; 590 591 ret = kxcjk1013_get_mode(data, &store_mode); 592 if (ret < 0) 593 return ret; 594 595 /* This is requirement by spec to change state to STANDBY */ 596 ret = kxcjk1013_set_mode(data, STANDBY); 597 if (ret < 0) 598 return ret; 599 600 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_INT_CTRL1); 601 if (ret < 0) { 602 dev_err(&data->client->dev, "Error reading reg_int_ctrl1\n"); 603 return ret; 604 } 605 606 if (status) 607 ret |= KXCJK1013_REG_INT_CTRL1_BIT_IEN; 608 else 609 ret &= ~KXCJK1013_REG_INT_CTRL1_BIT_IEN; 610 611 ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_INT_CTRL1, 612 ret); 613 if (ret < 0) { 614 dev_err(&data->client->dev, "Error writing reg_int_ctrl1\n"); 615 return ret; 616 } 617 618 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_CTRL1); 619 if (ret < 0) { 620 dev_err(&data->client->dev, "Error reading reg_ctrl1\n"); 621 return ret; 622 } 623 624 if (status) 625 ret |= KXCJK1013_REG_CTRL1_BIT_DRDY; 626 else 627 ret &= ~KXCJK1013_REG_CTRL1_BIT_DRDY; 628 629 ret = i2c_smbus_write_byte_data(data->client, 630 KXCJK1013_REG_CTRL1, ret); 631 if (ret < 0) { 632 dev_err(&data->client->dev, "Error writing reg_ctrl1\n"); 633 return ret; 634 } 635 636 if (store_mode == OPERATION) { 637 ret = kxcjk1013_set_mode(data, OPERATION); 638 if (ret < 0) 639 return ret; 640 } 641 642 return 0; 643 } 644 645 static const struct kx_odr_map *kxcjk1013_find_odr_value( 646 const struct kx_odr_map *map, size_t map_size, int val, int val2) 647 { 648 int i; 649 650 for (i = 0; i < map_size; ++i) { 651 if (map[i].val == val && map[i].val2 == val2) 652 return &map[i]; 653 } 654 655 return ERR_PTR(-EINVAL); 656 } 657 658 static int kxcjk1013_convert_odr_value(const struct kx_odr_map *map, 659 size_t map_size, int odr_bits, 660 int *val, int *val2) 661 { 662 int i; 663 664 for (i = 0; i < map_size; ++i) { 665 if (map[i].odr_bits == odr_bits) { 666 *val = map[i].val; 667 *val2 = map[i].val2; 668 return IIO_VAL_INT_PLUS_MICRO; 669 } 670 } 671 672 return -EINVAL; 673 } 674 675 static int kxcjk1013_set_odr(struct kxcjk1013_data *data, int val, int val2) 676 { 677 int ret; 678 enum kxcjk1013_mode store_mode; 679 const struct kx_odr_map *odr_setting; 680 681 ret = kxcjk1013_get_mode(data, &store_mode); 682 if (ret < 0) 683 return ret; 684 685 if (data->chipset == KXTF9) 686 odr_setting = kxcjk1013_find_odr_value(kxtf9_samp_freq_table, 687 ARRAY_SIZE(kxtf9_samp_freq_table), 688 val, val2); 689 else 690 odr_setting = kxcjk1013_find_odr_value(samp_freq_table, 691 ARRAY_SIZE(samp_freq_table), 692 val, val2); 693 694 if (IS_ERR(odr_setting)) 695 return PTR_ERR(odr_setting); 696 697 /* To change ODR, the chip must be set to STANDBY as per spec */ 698 ret = kxcjk1013_set_mode(data, STANDBY); 699 if (ret < 0) 700 return ret; 701 702 ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_DATA_CTRL, 703 odr_setting->odr_bits); 704 if (ret < 0) { 705 dev_err(&data->client->dev, "Error writing data_ctrl\n"); 706 return ret; 707 } 708 709 data->odr_bits = odr_setting->odr_bits; 710 711 ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_CTRL2, 712 odr_setting->wuf_bits); 713 if (ret < 0) { 714 dev_err(&data->client->dev, "Error writing reg_ctrl2\n"); 715 return ret; 716 } 717 718 if (store_mode == OPERATION) { 719 ret = kxcjk1013_set_mode(data, OPERATION); 720 if (ret < 0) 721 return ret; 722 } 723 724 return 0; 725 } 726 727 static int kxcjk1013_get_odr(struct kxcjk1013_data *data, int *val, int *val2) 728 { 729 if (data->chipset == KXTF9) 730 return kxcjk1013_convert_odr_value(kxtf9_samp_freq_table, 731 ARRAY_SIZE(kxtf9_samp_freq_table), 732 data->odr_bits, val, val2); 733 else 734 return kxcjk1013_convert_odr_value(samp_freq_table, 735 ARRAY_SIZE(samp_freq_table), 736 data->odr_bits, val, val2); 737 } 738 739 static int kxcjk1013_get_acc_reg(struct kxcjk1013_data *data, int axis) 740 { 741 u8 reg = KXCJK1013_REG_XOUT_L + axis * 2; 742 int ret; 743 744 ret = i2c_smbus_read_word_data(data->client, reg); 745 if (ret < 0) { 746 dev_err(&data->client->dev, 747 "failed to read accel_%c registers\n", 'x' + axis); 748 return ret; 749 } 750 751 return ret; 752 } 753 754 static int kxcjk1013_set_scale(struct kxcjk1013_data *data, int val) 755 { 756 int ret, i; 757 enum kxcjk1013_mode store_mode; 758 759 for (i = 0; i < ARRAY_SIZE(KXCJK1013_scale_table); ++i) { 760 if (KXCJK1013_scale_table[i].scale == val) { 761 ret = kxcjk1013_get_mode(data, &store_mode); 762 if (ret < 0) 763 return ret; 764 765 ret = kxcjk1013_set_mode(data, STANDBY); 766 if (ret < 0) 767 return ret; 768 769 ret = kxcjk1013_set_range(data, i); 770 if (ret < 0) 771 return ret; 772 773 if (store_mode == OPERATION) { 774 ret = kxcjk1013_set_mode(data, OPERATION); 775 if (ret) 776 return ret; 777 } 778 779 return 0; 780 } 781 } 782 783 return -EINVAL; 784 } 785 786 static int kxcjk1013_read_raw(struct iio_dev *indio_dev, 787 struct iio_chan_spec const *chan, int *val, 788 int *val2, long mask) 789 { 790 struct kxcjk1013_data *data = iio_priv(indio_dev); 791 int ret; 792 793 switch (mask) { 794 case IIO_CHAN_INFO_RAW: 795 mutex_lock(&data->mutex); 796 if (iio_buffer_enabled(indio_dev)) 797 ret = -EBUSY; 798 else { 799 ret = kxcjk1013_set_power_state(data, true); 800 if (ret < 0) { 801 mutex_unlock(&data->mutex); 802 return ret; 803 } 804 ret = kxcjk1013_get_acc_reg(data, chan->scan_index); 805 if (ret < 0) { 806 kxcjk1013_set_power_state(data, false); 807 mutex_unlock(&data->mutex); 808 return ret; 809 } 810 *val = sign_extend32(ret >> 4, 11); 811 ret = kxcjk1013_set_power_state(data, false); 812 } 813 mutex_unlock(&data->mutex); 814 815 if (ret < 0) 816 return ret; 817 818 return IIO_VAL_INT; 819 820 case IIO_CHAN_INFO_SCALE: 821 *val = 0; 822 *val2 = KXCJK1013_scale_table[data->range].scale; 823 return IIO_VAL_INT_PLUS_MICRO; 824 825 case IIO_CHAN_INFO_SAMP_FREQ: 826 mutex_lock(&data->mutex); 827 ret = kxcjk1013_get_odr(data, val, val2); 828 mutex_unlock(&data->mutex); 829 return ret; 830 831 default: 832 return -EINVAL; 833 } 834 } 835 836 static int kxcjk1013_write_raw(struct iio_dev *indio_dev, 837 struct iio_chan_spec const *chan, int val, 838 int val2, long mask) 839 { 840 struct kxcjk1013_data *data = iio_priv(indio_dev); 841 int ret; 842 843 switch (mask) { 844 case IIO_CHAN_INFO_SAMP_FREQ: 845 mutex_lock(&data->mutex); 846 ret = kxcjk1013_set_odr(data, val, val2); 847 mutex_unlock(&data->mutex); 848 break; 849 case IIO_CHAN_INFO_SCALE: 850 if (val) 851 return -EINVAL; 852 853 mutex_lock(&data->mutex); 854 ret = kxcjk1013_set_scale(data, val2); 855 mutex_unlock(&data->mutex); 856 break; 857 default: 858 ret = -EINVAL; 859 } 860 861 return ret; 862 } 863 864 static int kxcjk1013_read_event(struct iio_dev *indio_dev, 865 const struct iio_chan_spec *chan, 866 enum iio_event_type type, 867 enum iio_event_direction dir, 868 enum iio_event_info info, 869 int *val, int *val2) 870 { 871 struct kxcjk1013_data *data = iio_priv(indio_dev); 872 873 *val2 = 0; 874 switch (info) { 875 case IIO_EV_INFO_VALUE: 876 *val = data->wake_thres; 877 break; 878 case IIO_EV_INFO_PERIOD: 879 *val = data->wake_dur; 880 break; 881 default: 882 return -EINVAL; 883 } 884 885 return IIO_VAL_INT; 886 } 887 888 static int kxcjk1013_write_event(struct iio_dev *indio_dev, 889 const struct iio_chan_spec *chan, 890 enum iio_event_type type, 891 enum iio_event_direction dir, 892 enum iio_event_info info, 893 int val, int val2) 894 { 895 struct kxcjk1013_data *data = iio_priv(indio_dev); 896 897 if (data->ev_enable_state) 898 return -EBUSY; 899 900 switch (info) { 901 case IIO_EV_INFO_VALUE: 902 data->wake_thres = val; 903 break; 904 case IIO_EV_INFO_PERIOD: 905 data->wake_dur = val; 906 break; 907 default: 908 return -EINVAL; 909 } 910 911 return 0; 912 } 913 914 static int kxcjk1013_read_event_config(struct iio_dev *indio_dev, 915 const struct iio_chan_spec *chan, 916 enum iio_event_type type, 917 enum iio_event_direction dir) 918 { 919 struct kxcjk1013_data *data = iio_priv(indio_dev); 920 921 return data->ev_enable_state; 922 } 923 924 static int kxcjk1013_write_event_config(struct iio_dev *indio_dev, 925 const struct iio_chan_spec *chan, 926 enum iio_event_type type, 927 enum iio_event_direction dir, 928 int state) 929 { 930 struct kxcjk1013_data *data = iio_priv(indio_dev); 931 int ret; 932 933 if (state && data->ev_enable_state) 934 return 0; 935 936 mutex_lock(&data->mutex); 937 938 if (!state && data->motion_trigger_on) { 939 data->ev_enable_state = 0; 940 mutex_unlock(&data->mutex); 941 return 0; 942 } 943 944 /* 945 * We will expect the enable and disable to do operation in 946 * in reverse order. This will happen here anyway as our 947 * resume operation uses sync mode runtime pm calls, the 948 * suspend operation will be delayed by autosuspend delay 949 * So the disable operation will still happen in reverse of 950 * enable operation. When runtime pm is disabled the mode 951 * is always on so sequence doesn't matter 952 */ 953 ret = kxcjk1013_set_power_state(data, state); 954 if (ret < 0) { 955 mutex_unlock(&data->mutex); 956 return ret; 957 } 958 959 ret = kxcjk1013_setup_any_motion_interrupt(data, state); 960 if (ret < 0) { 961 kxcjk1013_set_power_state(data, false); 962 data->ev_enable_state = 0; 963 mutex_unlock(&data->mutex); 964 return ret; 965 } 966 967 data->ev_enable_state = state; 968 mutex_unlock(&data->mutex); 969 970 return 0; 971 } 972 973 static int kxcjk1013_buffer_preenable(struct iio_dev *indio_dev) 974 { 975 struct kxcjk1013_data *data = iio_priv(indio_dev); 976 977 return kxcjk1013_set_power_state(data, true); 978 } 979 980 static int kxcjk1013_buffer_postdisable(struct iio_dev *indio_dev) 981 { 982 struct kxcjk1013_data *data = iio_priv(indio_dev); 983 984 return kxcjk1013_set_power_state(data, false); 985 } 986 987 static ssize_t kxcjk1013_get_samp_freq_avail(struct device *dev, 988 struct device_attribute *attr, 989 char *buf) 990 { 991 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 992 struct kxcjk1013_data *data = iio_priv(indio_dev); 993 const char *str; 994 995 if (data->chipset == KXTF9) 996 str = kxtf9_samp_freq_avail; 997 else 998 str = kxcjk1013_samp_freq_avail; 999 1000 return sprintf(buf, "%s\n", str); 1001 } 1002 1003 static IIO_DEVICE_ATTR(in_accel_sampling_frequency_available, S_IRUGO, 1004 kxcjk1013_get_samp_freq_avail, NULL, 0); 1005 1006 static IIO_CONST_ATTR(in_accel_scale_available, "0.009582 0.019163 0.038326"); 1007 1008 static struct attribute *kxcjk1013_attributes[] = { 1009 &iio_dev_attr_in_accel_sampling_frequency_available.dev_attr.attr, 1010 &iio_const_attr_in_accel_scale_available.dev_attr.attr, 1011 NULL, 1012 }; 1013 1014 static const struct attribute_group kxcjk1013_attrs_group = { 1015 .attrs = kxcjk1013_attributes, 1016 }; 1017 1018 static const struct iio_event_spec kxcjk1013_event = { 1019 .type = IIO_EV_TYPE_THRESH, 1020 .dir = IIO_EV_DIR_EITHER, 1021 .mask_separate = BIT(IIO_EV_INFO_VALUE) | 1022 BIT(IIO_EV_INFO_ENABLE) | 1023 BIT(IIO_EV_INFO_PERIOD) 1024 }; 1025 1026 static const struct iio_mount_matrix * 1027 kxcjk1013_get_mount_matrix(const struct iio_dev *indio_dev, 1028 const struct iio_chan_spec *chan) 1029 { 1030 struct kxcjk1013_data *data = iio_priv(indio_dev); 1031 1032 return &data->orientation; 1033 } 1034 1035 static const struct iio_chan_spec_ext_info kxcjk1013_ext_info[] = { 1036 IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE, kxcjk1013_get_mount_matrix), 1037 { } 1038 }; 1039 1040 #define KXCJK1013_CHANNEL(_axis) { \ 1041 .type = IIO_ACCEL, \ 1042 .modified = 1, \ 1043 .channel2 = IIO_MOD_##_axis, \ 1044 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 1045 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ 1046 BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 1047 .scan_index = AXIS_##_axis, \ 1048 .scan_type = { \ 1049 .sign = 's', \ 1050 .realbits = 12, \ 1051 .storagebits = 16, \ 1052 .shift = 4, \ 1053 .endianness = IIO_LE, \ 1054 }, \ 1055 .event_spec = &kxcjk1013_event, \ 1056 .ext_info = kxcjk1013_ext_info, \ 1057 .num_event_specs = 1 \ 1058 } 1059 1060 static const struct iio_chan_spec kxcjk1013_channels[] = { 1061 KXCJK1013_CHANNEL(X), 1062 KXCJK1013_CHANNEL(Y), 1063 KXCJK1013_CHANNEL(Z), 1064 IIO_CHAN_SOFT_TIMESTAMP(3), 1065 }; 1066 1067 static const struct iio_buffer_setup_ops kxcjk1013_buffer_setup_ops = { 1068 .preenable = kxcjk1013_buffer_preenable, 1069 .postdisable = kxcjk1013_buffer_postdisable, 1070 }; 1071 1072 static const struct iio_info kxcjk1013_info = { 1073 .attrs = &kxcjk1013_attrs_group, 1074 .read_raw = kxcjk1013_read_raw, 1075 .write_raw = kxcjk1013_write_raw, 1076 .read_event_value = kxcjk1013_read_event, 1077 .write_event_value = kxcjk1013_write_event, 1078 .write_event_config = kxcjk1013_write_event_config, 1079 .read_event_config = kxcjk1013_read_event_config, 1080 }; 1081 1082 static const unsigned long kxcjk1013_scan_masks[] = {0x7, 0}; 1083 1084 static irqreturn_t kxcjk1013_trigger_handler(int irq, void *p) 1085 { 1086 struct iio_poll_func *pf = p; 1087 struct iio_dev *indio_dev = pf->indio_dev; 1088 struct kxcjk1013_data *data = iio_priv(indio_dev); 1089 int ret; 1090 1091 mutex_lock(&data->mutex); 1092 ret = i2c_smbus_read_i2c_block_data_or_emulated(data->client, 1093 KXCJK1013_REG_XOUT_L, 1094 AXIS_MAX * 2, 1095 (u8 *)data->buffer); 1096 mutex_unlock(&data->mutex); 1097 if (ret < 0) 1098 goto err; 1099 1100 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, 1101 data->timestamp); 1102 err: 1103 iio_trigger_notify_done(indio_dev->trig); 1104 1105 return IRQ_HANDLED; 1106 } 1107 1108 static void kxcjk1013_trig_reen(struct iio_trigger *trig) 1109 { 1110 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); 1111 struct kxcjk1013_data *data = iio_priv(indio_dev); 1112 int ret; 1113 1114 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_INT_REL); 1115 if (ret < 0) 1116 dev_err(&data->client->dev, "Error reading reg_int_rel\n"); 1117 } 1118 1119 static int kxcjk1013_data_rdy_trigger_set_state(struct iio_trigger *trig, 1120 bool state) 1121 { 1122 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); 1123 struct kxcjk1013_data *data = iio_priv(indio_dev); 1124 int ret; 1125 1126 mutex_lock(&data->mutex); 1127 1128 if (!state && data->ev_enable_state && data->motion_trigger_on) { 1129 data->motion_trigger_on = false; 1130 mutex_unlock(&data->mutex); 1131 return 0; 1132 } 1133 1134 ret = kxcjk1013_set_power_state(data, state); 1135 if (ret < 0) { 1136 mutex_unlock(&data->mutex); 1137 return ret; 1138 } 1139 if (data->motion_trig == trig) 1140 ret = kxcjk1013_setup_any_motion_interrupt(data, state); 1141 else 1142 ret = kxcjk1013_setup_new_data_interrupt(data, state); 1143 if (ret < 0) { 1144 kxcjk1013_set_power_state(data, false); 1145 mutex_unlock(&data->mutex); 1146 return ret; 1147 } 1148 if (data->motion_trig == trig) 1149 data->motion_trigger_on = state; 1150 else 1151 data->dready_trigger_on = state; 1152 1153 mutex_unlock(&data->mutex); 1154 1155 return 0; 1156 } 1157 1158 static const struct iio_trigger_ops kxcjk1013_trigger_ops = { 1159 .set_trigger_state = kxcjk1013_data_rdy_trigger_set_state, 1160 .reenable = kxcjk1013_trig_reen, 1161 }; 1162 1163 static void kxcjk1013_report_motion_event(struct iio_dev *indio_dev) 1164 { 1165 struct kxcjk1013_data *data = iio_priv(indio_dev); 1166 1167 int ret = i2c_smbus_read_byte_data(data->client, 1168 KXCJK1013_REG_INT_SRC2); 1169 if (ret < 0) { 1170 dev_err(&data->client->dev, "Error reading reg_int_src2\n"); 1171 return; 1172 } 1173 1174 if (ret & KXCJK1013_REG_INT_SRC2_BIT_XN) 1175 iio_push_event(indio_dev, 1176 IIO_MOD_EVENT_CODE(IIO_ACCEL, 1177 0, 1178 IIO_MOD_X, 1179 IIO_EV_TYPE_THRESH, 1180 IIO_EV_DIR_FALLING), 1181 data->timestamp); 1182 1183 if (ret & KXCJK1013_REG_INT_SRC2_BIT_XP) 1184 iio_push_event(indio_dev, 1185 IIO_MOD_EVENT_CODE(IIO_ACCEL, 1186 0, 1187 IIO_MOD_X, 1188 IIO_EV_TYPE_THRESH, 1189 IIO_EV_DIR_RISING), 1190 data->timestamp); 1191 1192 if (ret & KXCJK1013_REG_INT_SRC2_BIT_YN) 1193 iio_push_event(indio_dev, 1194 IIO_MOD_EVENT_CODE(IIO_ACCEL, 1195 0, 1196 IIO_MOD_Y, 1197 IIO_EV_TYPE_THRESH, 1198 IIO_EV_DIR_FALLING), 1199 data->timestamp); 1200 1201 if (ret & KXCJK1013_REG_INT_SRC2_BIT_YP) 1202 iio_push_event(indio_dev, 1203 IIO_MOD_EVENT_CODE(IIO_ACCEL, 1204 0, 1205 IIO_MOD_Y, 1206 IIO_EV_TYPE_THRESH, 1207 IIO_EV_DIR_RISING), 1208 data->timestamp); 1209 1210 if (ret & KXCJK1013_REG_INT_SRC2_BIT_ZN) 1211 iio_push_event(indio_dev, 1212 IIO_MOD_EVENT_CODE(IIO_ACCEL, 1213 0, 1214 IIO_MOD_Z, 1215 IIO_EV_TYPE_THRESH, 1216 IIO_EV_DIR_FALLING), 1217 data->timestamp); 1218 1219 if (ret & KXCJK1013_REG_INT_SRC2_BIT_ZP) 1220 iio_push_event(indio_dev, 1221 IIO_MOD_EVENT_CODE(IIO_ACCEL, 1222 0, 1223 IIO_MOD_Z, 1224 IIO_EV_TYPE_THRESH, 1225 IIO_EV_DIR_RISING), 1226 data->timestamp); 1227 } 1228 1229 static irqreturn_t kxcjk1013_event_handler(int irq, void *private) 1230 { 1231 struct iio_dev *indio_dev = private; 1232 struct kxcjk1013_data *data = iio_priv(indio_dev); 1233 int ret; 1234 1235 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_INT_SRC1); 1236 if (ret < 0) { 1237 dev_err(&data->client->dev, "Error reading reg_int_src1\n"); 1238 goto ack_intr; 1239 } 1240 1241 if (ret & KXCJK1013_REG_INT_SRC1_BIT_WUFS) { 1242 if (data->chipset == KXTF9) 1243 iio_push_event(indio_dev, 1244 IIO_MOD_EVENT_CODE(IIO_ACCEL, 1245 0, 1246 IIO_MOD_X_AND_Y_AND_Z, 1247 IIO_EV_TYPE_THRESH, 1248 IIO_EV_DIR_RISING), 1249 data->timestamp); 1250 else 1251 kxcjk1013_report_motion_event(indio_dev); 1252 } 1253 1254 ack_intr: 1255 if (data->dready_trigger_on) 1256 return IRQ_HANDLED; 1257 1258 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_INT_REL); 1259 if (ret < 0) 1260 dev_err(&data->client->dev, "Error reading reg_int_rel\n"); 1261 1262 return IRQ_HANDLED; 1263 } 1264 1265 static irqreturn_t kxcjk1013_data_rdy_trig_poll(int irq, void *private) 1266 { 1267 struct iio_dev *indio_dev = private; 1268 struct kxcjk1013_data *data = iio_priv(indio_dev); 1269 1270 data->timestamp = iio_get_time_ns(indio_dev); 1271 1272 if (data->dready_trigger_on) 1273 iio_trigger_poll(data->dready_trig); 1274 else if (data->motion_trigger_on) 1275 iio_trigger_poll(data->motion_trig); 1276 1277 if (data->ev_enable_state) 1278 return IRQ_WAKE_THREAD; 1279 else 1280 return IRQ_HANDLED; 1281 } 1282 1283 static const char *kxcjk1013_match_acpi_device(struct device *dev, 1284 enum kx_chipset *chipset, 1285 enum kx_acpi_type *acpi_type) 1286 { 1287 const struct acpi_device_id *id; 1288 1289 id = acpi_match_device(dev->driver->acpi_match_table, dev); 1290 if (!id) 1291 return NULL; 1292 1293 if (strcmp(id->id, "SMO8500") == 0) 1294 *acpi_type = ACPI_SMO8500; 1295 else if (strcmp(id->id, "KIOX010A") == 0) 1296 *acpi_type = ACPI_KIOX010A; 1297 1298 *chipset = (enum kx_chipset)id->driver_data; 1299 1300 return dev_name(dev); 1301 } 1302 1303 static int kxcjk1013_probe(struct i2c_client *client, 1304 const struct i2c_device_id *id) 1305 { 1306 struct kxcjk1013_data *data; 1307 struct iio_dev *indio_dev; 1308 struct kxcjk_1013_platform_data *pdata; 1309 const char *name; 1310 int ret; 1311 1312 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 1313 if (!indio_dev) 1314 return -ENOMEM; 1315 1316 data = iio_priv(indio_dev); 1317 i2c_set_clientdata(client, indio_dev); 1318 data->client = client; 1319 1320 pdata = dev_get_platdata(&client->dev); 1321 if (pdata) { 1322 data->active_high_intr = pdata->active_high_intr; 1323 data->orientation = pdata->orientation; 1324 } else { 1325 data->active_high_intr = true; /* default polarity */ 1326 1327 ret = iio_read_mount_matrix(&client->dev, "mount-matrix", 1328 &data->orientation); 1329 if (ret) 1330 return ret; 1331 } 1332 1333 if (id) { 1334 data->chipset = (enum kx_chipset)(id->driver_data); 1335 name = id->name; 1336 } else if (ACPI_HANDLE(&client->dev)) { 1337 name = kxcjk1013_match_acpi_device(&client->dev, 1338 &data->chipset, 1339 &data->acpi_type); 1340 } else 1341 return -ENODEV; 1342 1343 ret = kxcjk1013_chip_init(data); 1344 if (ret < 0) 1345 return ret; 1346 1347 mutex_init(&data->mutex); 1348 1349 indio_dev->channels = kxcjk1013_channels; 1350 indio_dev->num_channels = ARRAY_SIZE(kxcjk1013_channels); 1351 indio_dev->available_scan_masks = kxcjk1013_scan_masks; 1352 indio_dev->name = name; 1353 indio_dev->modes = INDIO_DIRECT_MODE; 1354 indio_dev->info = &kxcjk1013_info; 1355 1356 if (client->irq > 0 && data->acpi_type != ACPI_SMO8500) { 1357 ret = devm_request_threaded_irq(&client->dev, client->irq, 1358 kxcjk1013_data_rdy_trig_poll, 1359 kxcjk1013_event_handler, 1360 IRQF_TRIGGER_RISING, 1361 KXCJK1013_IRQ_NAME, 1362 indio_dev); 1363 if (ret) 1364 goto err_poweroff; 1365 1366 data->dready_trig = devm_iio_trigger_alloc(&client->dev, 1367 "%s-dev%d", 1368 indio_dev->name, 1369 indio_dev->id); 1370 if (!data->dready_trig) { 1371 ret = -ENOMEM; 1372 goto err_poweroff; 1373 } 1374 1375 data->motion_trig = devm_iio_trigger_alloc(&client->dev, 1376 "%s-any-motion-dev%d", 1377 indio_dev->name, 1378 indio_dev->id); 1379 if (!data->motion_trig) { 1380 ret = -ENOMEM; 1381 goto err_poweroff; 1382 } 1383 1384 data->dready_trig->dev.parent = &client->dev; 1385 data->dready_trig->ops = &kxcjk1013_trigger_ops; 1386 iio_trigger_set_drvdata(data->dready_trig, indio_dev); 1387 indio_dev->trig = data->dready_trig; 1388 iio_trigger_get(indio_dev->trig); 1389 ret = iio_trigger_register(data->dready_trig); 1390 if (ret) 1391 goto err_poweroff; 1392 1393 data->motion_trig->dev.parent = &client->dev; 1394 data->motion_trig->ops = &kxcjk1013_trigger_ops; 1395 iio_trigger_set_drvdata(data->motion_trig, indio_dev); 1396 ret = iio_trigger_register(data->motion_trig); 1397 if (ret) { 1398 data->motion_trig = NULL; 1399 goto err_trigger_unregister; 1400 } 1401 } 1402 1403 ret = iio_triggered_buffer_setup(indio_dev, 1404 &iio_pollfunc_store_time, 1405 kxcjk1013_trigger_handler, 1406 &kxcjk1013_buffer_setup_ops); 1407 if (ret < 0) { 1408 dev_err(&client->dev, "iio triggered buffer setup failed\n"); 1409 goto err_trigger_unregister; 1410 } 1411 1412 ret = pm_runtime_set_active(&client->dev); 1413 if (ret) 1414 goto err_buffer_cleanup; 1415 1416 pm_runtime_enable(&client->dev); 1417 pm_runtime_set_autosuspend_delay(&client->dev, 1418 KXCJK1013_SLEEP_DELAY_MS); 1419 pm_runtime_use_autosuspend(&client->dev); 1420 1421 ret = iio_device_register(indio_dev); 1422 if (ret < 0) { 1423 dev_err(&client->dev, "unable to register iio device\n"); 1424 goto err_buffer_cleanup; 1425 } 1426 1427 return 0; 1428 1429 err_buffer_cleanup: 1430 if (data->dready_trig) 1431 iio_triggered_buffer_cleanup(indio_dev); 1432 err_trigger_unregister: 1433 if (data->dready_trig) 1434 iio_trigger_unregister(data->dready_trig); 1435 if (data->motion_trig) 1436 iio_trigger_unregister(data->motion_trig); 1437 err_poweroff: 1438 kxcjk1013_set_mode(data, STANDBY); 1439 1440 return ret; 1441 } 1442 1443 static int kxcjk1013_remove(struct i2c_client *client) 1444 { 1445 struct iio_dev *indio_dev = i2c_get_clientdata(client); 1446 struct kxcjk1013_data *data = iio_priv(indio_dev); 1447 1448 iio_device_unregister(indio_dev); 1449 1450 pm_runtime_disable(&client->dev); 1451 pm_runtime_set_suspended(&client->dev); 1452 pm_runtime_put_noidle(&client->dev); 1453 1454 if (data->dready_trig) { 1455 iio_triggered_buffer_cleanup(indio_dev); 1456 iio_trigger_unregister(data->dready_trig); 1457 iio_trigger_unregister(data->motion_trig); 1458 } 1459 1460 mutex_lock(&data->mutex); 1461 kxcjk1013_set_mode(data, STANDBY); 1462 mutex_unlock(&data->mutex); 1463 1464 return 0; 1465 } 1466 1467 #ifdef CONFIG_PM_SLEEP 1468 static int kxcjk1013_suspend(struct device *dev) 1469 { 1470 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 1471 struct kxcjk1013_data *data = iio_priv(indio_dev); 1472 int ret; 1473 1474 mutex_lock(&data->mutex); 1475 ret = kxcjk1013_set_mode(data, STANDBY); 1476 mutex_unlock(&data->mutex); 1477 1478 return ret; 1479 } 1480 1481 static int kxcjk1013_resume(struct device *dev) 1482 { 1483 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 1484 struct kxcjk1013_data *data = iio_priv(indio_dev); 1485 int ret = 0; 1486 1487 mutex_lock(&data->mutex); 1488 ret = kxcjk1013_set_mode(data, OPERATION); 1489 if (ret == 0) 1490 ret = kxcjk1013_set_range(data, data->range); 1491 mutex_unlock(&data->mutex); 1492 1493 return ret; 1494 } 1495 #endif 1496 1497 #ifdef CONFIG_PM 1498 static int kxcjk1013_runtime_suspend(struct device *dev) 1499 { 1500 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 1501 struct kxcjk1013_data *data = iio_priv(indio_dev); 1502 int ret; 1503 1504 ret = kxcjk1013_set_mode(data, STANDBY); 1505 if (ret < 0) { 1506 dev_err(&data->client->dev, "powering off device failed\n"); 1507 return -EAGAIN; 1508 } 1509 return 0; 1510 } 1511 1512 static int kxcjk1013_runtime_resume(struct device *dev) 1513 { 1514 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 1515 struct kxcjk1013_data *data = iio_priv(indio_dev); 1516 int ret; 1517 int sleep_val; 1518 1519 ret = kxcjk1013_set_mode(data, OPERATION); 1520 if (ret < 0) 1521 return ret; 1522 1523 sleep_val = kxcjk1013_get_startup_times(data); 1524 if (sleep_val < 20000) 1525 usleep_range(sleep_val, 20000); 1526 else 1527 msleep_interruptible(sleep_val/1000); 1528 1529 return 0; 1530 } 1531 #endif 1532 1533 static const struct dev_pm_ops kxcjk1013_pm_ops = { 1534 SET_SYSTEM_SLEEP_PM_OPS(kxcjk1013_suspend, kxcjk1013_resume) 1535 SET_RUNTIME_PM_OPS(kxcjk1013_runtime_suspend, 1536 kxcjk1013_runtime_resume, NULL) 1537 }; 1538 1539 static const struct acpi_device_id kx_acpi_match[] = { 1540 {"KXCJ1013", KXCJK1013}, 1541 {"KXCJ1008", KXCJ91008}, 1542 {"KXCJ9000", KXCJ91008}, 1543 {"KIOX0008", KXCJ91008}, 1544 {"KIOX0009", KXTJ21009}, 1545 {"KIOX000A", KXCJ91008}, 1546 {"KIOX010A", KXCJ91008}, /* KXCJ91008 in the display of a yoga 2-in-1 */ 1547 {"KIOX020A", KXCJ91008}, /* KXCJ91008 in the base of a yoga 2-in-1 */ 1548 {"KXTJ1009", KXTJ21009}, 1549 {"KXJ2109", KXTJ21009}, 1550 {"SMO8500", KXCJ91008}, 1551 { }, 1552 }; 1553 MODULE_DEVICE_TABLE(acpi, kx_acpi_match); 1554 1555 static const struct i2c_device_id kxcjk1013_id[] = { 1556 {"kxcjk1013", KXCJK1013}, 1557 {"kxcj91008", KXCJ91008}, 1558 {"kxtj21009", KXTJ21009}, 1559 {"kxtf9", KXTF9}, 1560 {"SMO8500", KXCJ91008}, 1561 {} 1562 }; 1563 1564 MODULE_DEVICE_TABLE(i2c, kxcjk1013_id); 1565 1566 static const struct of_device_id kxcjk1013_of_match[] = { 1567 { .compatible = "kionix,kxcjk1013", }, 1568 { .compatible = "kionix,kxcj91008", }, 1569 { .compatible = "kionix,kxtj21009", }, 1570 { .compatible = "kionix,kxtf9", }, 1571 { } 1572 }; 1573 MODULE_DEVICE_TABLE(of, kxcjk1013_of_match); 1574 1575 static struct i2c_driver kxcjk1013_driver = { 1576 .driver = { 1577 .name = KXCJK1013_DRV_NAME, 1578 .acpi_match_table = ACPI_PTR(kx_acpi_match), 1579 .of_match_table = kxcjk1013_of_match, 1580 .pm = &kxcjk1013_pm_ops, 1581 }, 1582 .probe = kxcjk1013_probe, 1583 .remove = kxcjk1013_remove, 1584 .id_table = kxcjk1013_id, 1585 }; 1586 module_i2c_driver(kxcjk1013_driver); 1587 1588 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>"); 1589 MODULE_LICENSE("GPL v2"); 1590 MODULE_DESCRIPTION("KXCJK1013 accelerometer driver"); 1591