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