1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * STMicroelectronics accelerometers driver 4 * 5 * Copyright 2012-2013 STMicroelectronics Inc. 6 * 7 * Denis Ciocca <denis.ciocca@st.com> 8 */ 9 10 #include <linux/kernel.h> 11 #include <linux/module.h> 12 #include <linux/slab.h> 13 #include <linux/acpi.h> 14 #include <linux/errno.h> 15 #include <linux/types.h> 16 #include <linux/interrupt.h> 17 #include <linux/i2c.h> 18 #include <linux/irq.h> 19 #include <linux/iio/iio.h> 20 #include <linux/iio/sysfs.h> 21 #include <linux/iio/trigger.h> 22 #include <linux/iio/buffer.h> 23 24 #include <linux/iio/common/st_sensors.h> 25 #include "st_accel.h" 26 27 #define ST_ACCEL_NUMBER_DATA_CHANNELS 3 28 29 /* DEFAULT VALUE FOR SENSORS */ 30 #define ST_ACCEL_DEFAULT_OUT_X_L_ADDR 0x28 31 #define ST_ACCEL_DEFAULT_OUT_Y_L_ADDR 0x2a 32 #define ST_ACCEL_DEFAULT_OUT_Z_L_ADDR 0x2c 33 34 /* FULLSCALE */ 35 #define ST_ACCEL_FS_AVL_2G 2 36 #define ST_ACCEL_FS_AVL_4G 4 37 #define ST_ACCEL_FS_AVL_6G 6 38 #define ST_ACCEL_FS_AVL_8G 8 39 #define ST_ACCEL_FS_AVL_16G 16 40 #define ST_ACCEL_FS_AVL_100G 100 41 #define ST_ACCEL_FS_AVL_200G 200 42 #define ST_ACCEL_FS_AVL_400G 400 43 44 static const struct iio_chan_spec st_accel_8bit_channels[] = { 45 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL, 46 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), 47 ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_LE, 8, 8, 48 ST_ACCEL_DEFAULT_OUT_X_L_ADDR+1), 49 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL, 50 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), 51 ST_SENSORS_SCAN_Y, 1, IIO_MOD_Y, 's', IIO_LE, 8, 8, 52 ST_ACCEL_DEFAULT_OUT_Y_L_ADDR+1), 53 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL, 54 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), 55 ST_SENSORS_SCAN_Z, 1, IIO_MOD_Z, 's', IIO_LE, 8, 8, 56 ST_ACCEL_DEFAULT_OUT_Z_L_ADDR+1), 57 IIO_CHAN_SOFT_TIMESTAMP(3) 58 }; 59 60 static const struct iio_chan_spec st_accel_12bit_channels[] = { 61 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL, 62 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), 63 ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_LE, 12, 16, 64 ST_ACCEL_DEFAULT_OUT_X_L_ADDR), 65 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL, 66 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), 67 ST_SENSORS_SCAN_Y, 1, IIO_MOD_Y, 's', IIO_LE, 12, 16, 68 ST_ACCEL_DEFAULT_OUT_Y_L_ADDR), 69 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL, 70 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), 71 ST_SENSORS_SCAN_Z, 1, IIO_MOD_Z, 's', IIO_LE, 12, 16, 72 ST_ACCEL_DEFAULT_OUT_Z_L_ADDR), 73 IIO_CHAN_SOFT_TIMESTAMP(3) 74 }; 75 76 static const struct iio_chan_spec st_accel_16bit_channels[] = { 77 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL, 78 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), 79 ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_LE, 16, 16, 80 ST_ACCEL_DEFAULT_OUT_X_L_ADDR), 81 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL, 82 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), 83 ST_SENSORS_SCAN_Y, 1, IIO_MOD_Y, 's', IIO_LE, 16, 16, 84 ST_ACCEL_DEFAULT_OUT_Y_L_ADDR), 85 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL, 86 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), 87 ST_SENSORS_SCAN_Z, 1, IIO_MOD_Z, 's', IIO_LE, 16, 16, 88 ST_ACCEL_DEFAULT_OUT_Z_L_ADDR), 89 IIO_CHAN_SOFT_TIMESTAMP(3) 90 }; 91 92 static const struct st_sensor_settings st_accel_sensors_settings[] = { 93 { 94 .wai = 0x33, 95 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS, 96 .sensors_supported = { 97 [0] = LIS3DH_ACCEL_DEV_NAME, 98 [1] = LSM303DLHC_ACCEL_DEV_NAME, 99 [2] = LSM330D_ACCEL_DEV_NAME, 100 [3] = LSM330DL_ACCEL_DEV_NAME, 101 [4] = LSM330DLC_ACCEL_DEV_NAME, 102 [5] = LSM303AGR_ACCEL_DEV_NAME, 103 [6] = LIS2DH12_ACCEL_DEV_NAME, 104 [7] = LIS3DE_ACCEL_DEV_NAME, 105 }, 106 .ch = (struct iio_chan_spec *)st_accel_12bit_channels, 107 .odr = { 108 .addr = 0x20, 109 .mask = 0xf0, 110 .odr_avl = { 111 { .hz = 1, .value = 0x01, }, 112 { .hz = 10, .value = 0x02, }, 113 { .hz = 25, .value = 0x03, }, 114 { .hz = 50, .value = 0x04, }, 115 { .hz = 100, .value = 0x05, }, 116 { .hz = 200, .value = 0x06, }, 117 { .hz = 400, .value = 0x07, }, 118 { .hz = 1600, .value = 0x08, }, 119 }, 120 }, 121 .pw = { 122 .addr = 0x20, 123 .mask = 0xf0, 124 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, 125 }, 126 .enable_axis = { 127 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR, 128 .mask = ST_SENSORS_DEFAULT_AXIS_MASK, 129 }, 130 .fs = { 131 .addr = 0x23, 132 .mask = 0x30, 133 .fs_avl = { 134 [0] = { 135 .num = ST_ACCEL_FS_AVL_2G, 136 .value = 0x00, 137 .gain = IIO_G_TO_M_S_2(1000), 138 }, 139 [1] = { 140 .num = ST_ACCEL_FS_AVL_4G, 141 .value = 0x01, 142 .gain = IIO_G_TO_M_S_2(2000), 143 }, 144 [2] = { 145 .num = ST_ACCEL_FS_AVL_8G, 146 .value = 0x02, 147 .gain = IIO_G_TO_M_S_2(4000), 148 }, 149 [3] = { 150 .num = ST_ACCEL_FS_AVL_16G, 151 .value = 0x03, 152 .gain = IIO_G_TO_M_S_2(12000), 153 }, 154 }, 155 }, 156 .bdu = { 157 .addr = 0x23, 158 .mask = 0x80, 159 }, 160 .drdy_irq = { 161 .int1 = { 162 .addr = 0x22, 163 .mask = 0x10, 164 }, 165 .addr_ihl = 0x25, 166 .mask_ihl = 0x02, 167 .stat_drdy = { 168 .addr = ST_SENSORS_DEFAULT_STAT_ADDR, 169 .mask = 0x07, 170 }, 171 }, 172 .sim = { 173 .addr = 0x23, 174 .value = BIT(0), 175 }, 176 .multi_read_bit = true, 177 .bootime = 2, 178 }, 179 { 180 .wai = 0x32, 181 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS, 182 .sensors_supported = { 183 [0] = LIS331DLH_ACCEL_DEV_NAME, 184 [1] = LSM303DL_ACCEL_DEV_NAME, 185 [2] = LSM303DLH_ACCEL_DEV_NAME, 186 [3] = LSM303DLM_ACCEL_DEV_NAME, 187 }, 188 .ch = (struct iio_chan_spec *)st_accel_12bit_channels, 189 .odr = { 190 .addr = 0x20, 191 .mask = 0x18, 192 .odr_avl = { 193 { .hz = 50, .value = 0x00, }, 194 { .hz = 100, .value = 0x01, }, 195 { .hz = 400, .value = 0x02, }, 196 { .hz = 1000, .value = 0x03, }, 197 }, 198 }, 199 .pw = { 200 .addr = 0x20, 201 .mask = 0xe0, 202 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE, 203 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, 204 }, 205 .enable_axis = { 206 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR, 207 .mask = ST_SENSORS_DEFAULT_AXIS_MASK, 208 }, 209 .fs = { 210 .addr = 0x23, 211 .mask = 0x30, 212 .fs_avl = { 213 [0] = { 214 .num = ST_ACCEL_FS_AVL_2G, 215 .value = 0x00, 216 .gain = IIO_G_TO_M_S_2(1000), 217 }, 218 [1] = { 219 .num = ST_ACCEL_FS_AVL_4G, 220 .value = 0x01, 221 .gain = IIO_G_TO_M_S_2(2000), 222 }, 223 [2] = { 224 .num = ST_ACCEL_FS_AVL_8G, 225 .value = 0x03, 226 .gain = IIO_G_TO_M_S_2(3900), 227 }, 228 }, 229 }, 230 .bdu = { 231 .addr = 0x23, 232 .mask = 0x80, 233 }, 234 .drdy_irq = { 235 .int1 = { 236 .addr = 0x22, 237 .mask = 0x02, 238 .addr_od = 0x22, 239 .mask_od = 0x40, 240 }, 241 .int2 = { 242 .addr = 0x22, 243 .mask = 0x10, 244 .addr_od = 0x22, 245 .mask_od = 0x40, 246 }, 247 .addr_ihl = 0x22, 248 .mask_ihl = 0x80, 249 .stat_drdy = { 250 .addr = ST_SENSORS_DEFAULT_STAT_ADDR, 251 .mask = 0x07, 252 }, 253 }, 254 .sim = { 255 .addr = 0x23, 256 .value = BIT(0), 257 }, 258 .multi_read_bit = true, 259 .bootime = 2, 260 }, 261 { 262 .wai = 0x40, 263 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS, 264 .sensors_supported = { 265 [0] = LSM330_ACCEL_DEV_NAME, 266 }, 267 .ch = (struct iio_chan_spec *)st_accel_16bit_channels, 268 .odr = { 269 .addr = 0x20, 270 .mask = 0xf0, 271 .odr_avl = { 272 { .hz = 3, .value = 0x01, }, 273 { .hz = 6, .value = 0x02, }, 274 { .hz = 12, .value = 0x03, }, 275 { .hz = 25, .value = 0x04, }, 276 { .hz = 50, .value = 0x05, }, 277 { .hz = 100, .value = 0x06, }, 278 { .hz = 200, .value = 0x07, }, 279 { .hz = 400, .value = 0x08, }, 280 { .hz = 800, .value = 0x09, }, 281 { .hz = 1600, .value = 0x0a, }, 282 }, 283 }, 284 .pw = { 285 .addr = 0x20, 286 .mask = 0xf0, 287 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, 288 }, 289 .enable_axis = { 290 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR, 291 .mask = ST_SENSORS_DEFAULT_AXIS_MASK, 292 }, 293 .fs = { 294 .addr = 0x24, 295 .mask = 0x38, 296 .fs_avl = { 297 [0] = { 298 .num = ST_ACCEL_FS_AVL_2G, 299 .value = 0x00, 300 .gain = IIO_G_TO_M_S_2(61), 301 }, 302 [1] = { 303 .num = ST_ACCEL_FS_AVL_4G, 304 .value = 0x01, 305 .gain = IIO_G_TO_M_S_2(122), 306 }, 307 [2] = { 308 .num = ST_ACCEL_FS_AVL_6G, 309 .value = 0x02, 310 .gain = IIO_G_TO_M_S_2(183), 311 }, 312 [3] = { 313 .num = ST_ACCEL_FS_AVL_8G, 314 .value = 0x03, 315 .gain = IIO_G_TO_M_S_2(244), 316 }, 317 [4] = { 318 .num = ST_ACCEL_FS_AVL_16G, 319 .value = 0x04, 320 .gain = IIO_G_TO_M_S_2(732), 321 }, 322 }, 323 }, 324 .bdu = { 325 .addr = 0x20, 326 .mask = 0x08, 327 }, 328 .drdy_irq = { 329 .int1 = { 330 .addr = 0x23, 331 .mask = 0x80, 332 }, 333 .addr_ihl = 0x23, 334 .mask_ihl = 0x40, 335 .stat_drdy = { 336 .addr = ST_SENSORS_DEFAULT_STAT_ADDR, 337 .mask = 0x07, 338 }, 339 .ig1 = { 340 .en_addr = 0x23, 341 .en_mask = 0x08, 342 }, 343 }, 344 .sim = { 345 .addr = 0x24, 346 .value = BIT(0), 347 }, 348 .multi_read_bit = false, 349 .bootime = 2, 350 }, 351 { 352 .wai = 0x3a, 353 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS, 354 .sensors_supported = { 355 [0] = LIS3LV02DL_ACCEL_DEV_NAME, 356 }, 357 .ch = (struct iio_chan_spec *)st_accel_12bit_channels, 358 .odr = { 359 .addr = 0x20, 360 .mask = 0x30, /* DF1 and DF0 */ 361 .odr_avl = { 362 { .hz = 40, .value = 0x00, }, 363 { .hz = 160, .value = 0x01, }, 364 { .hz = 640, .value = 0x02, }, 365 { .hz = 2560, .value = 0x03, }, 366 }, 367 }, 368 .pw = { 369 .addr = 0x20, 370 .mask = 0xc0, 371 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE, 372 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, 373 }, 374 .enable_axis = { 375 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR, 376 .mask = ST_SENSORS_DEFAULT_AXIS_MASK, 377 }, 378 .fs = { 379 .addr = 0x21, 380 .mask = 0x80, 381 .fs_avl = { 382 [0] = { 383 .num = ST_ACCEL_FS_AVL_2G, 384 .value = 0x00, 385 .gain = IIO_G_TO_M_S_2(1000), 386 }, 387 [1] = { 388 .num = ST_ACCEL_FS_AVL_6G, 389 .value = 0x01, 390 .gain = IIO_G_TO_M_S_2(3000), 391 }, 392 }, 393 }, 394 .bdu = { 395 .addr = 0x21, 396 .mask = 0x40, 397 }, 398 /* 399 * Data Alignment Setting - needs to be set to get 400 * left-justified data like all other sensors. 401 */ 402 .das = { 403 .addr = 0x21, 404 .mask = 0x01, 405 }, 406 .drdy_irq = { 407 .int1 = { 408 .addr = 0x21, 409 .mask = 0x04, 410 }, 411 .stat_drdy = { 412 .addr = ST_SENSORS_DEFAULT_STAT_ADDR, 413 .mask = 0x07, 414 }, 415 }, 416 .sim = { 417 .addr = 0x21, 418 .value = BIT(1), 419 }, 420 .multi_read_bit = true, 421 .bootime = 2, /* guess */ 422 }, 423 { 424 .wai = 0x3b, 425 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS, 426 .sensors_supported = { 427 [0] = LIS331DL_ACCEL_DEV_NAME, 428 }, 429 .ch = (struct iio_chan_spec *)st_accel_8bit_channels, 430 .odr = { 431 .addr = 0x20, 432 .mask = 0x80, 433 .odr_avl = { 434 { .hz = 100, .value = 0x00, }, 435 { .hz = 400, .value = 0x01, }, 436 }, 437 }, 438 .pw = { 439 .addr = 0x20, 440 .mask = 0x40, 441 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE, 442 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, 443 }, 444 .enable_axis = { 445 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR, 446 .mask = ST_SENSORS_DEFAULT_AXIS_MASK, 447 }, 448 .fs = { 449 .addr = 0x20, 450 .mask = 0x20, 451 /* 452 * TODO: check these resulting gain settings, these are 453 * not in the datsheet 454 */ 455 .fs_avl = { 456 [0] = { 457 .num = ST_ACCEL_FS_AVL_2G, 458 .value = 0x00, 459 .gain = IIO_G_TO_M_S_2(18000), 460 }, 461 [1] = { 462 .num = ST_ACCEL_FS_AVL_8G, 463 .value = 0x01, 464 .gain = IIO_G_TO_M_S_2(72000), 465 }, 466 }, 467 }, 468 .drdy_irq = { 469 .int1 = { 470 .addr = 0x22, 471 .mask = 0x04, 472 .addr_od = 0x22, 473 .mask_od = 0x40, 474 }, 475 .int2 = { 476 .addr = 0x22, 477 .mask = 0x20, 478 .addr_od = 0x22, 479 .mask_od = 0x40, 480 }, 481 .addr_ihl = 0x22, 482 .mask_ihl = 0x80, 483 .stat_drdy = { 484 .addr = ST_SENSORS_DEFAULT_STAT_ADDR, 485 .mask = 0x07, 486 }, 487 }, 488 .sim = { 489 .addr = 0x21, 490 .value = BIT(7), 491 }, 492 .multi_read_bit = false, 493 .bootime = 2, /* guess */ 494 }, 495 { 496 .wai = 0x32, 497 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS, 498 .sensors_supported = { 499 [0] = H3LIS331DL_ACCEL_DEV_NAME, 500 }, 501 .ch = (struct iio_chan_spec *)st_accel_12bit_channels, 502 .odr = { 503 .addr = 0x20, 504 .mask = 0x18, 505 .odr_avl = { 506 { .hz = 50, .value = 0x00, }, 507 { .hz = 100, .value = 0x01, }, 508 { .hz = 400, .value = 0x02, }, 509 { .hz = 1000, .value = 0x03, }, 510 }, 511 }, 512 .pw = { 513 .addr = 0x20, 514 .mask = 0x20, 515 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE, 516 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, 517 }, 518 .enable_axis = { 519 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR, 520 .mask = ST_SENSORS_DEFAULT_AXIS_MASK, 521 }, 522 .fs = { 523 .addr = 0x23, 524 .mask = 0x30, 525 .fs_avl = { 526 [0] = { 527 .num = ST_ACCEL_FS_AVL_100G, 528 .value = 0x00, 529 .gain = IIO_G_TO_M_S_2(49000), 530 }, 531 [1] = { 532 .num = ST_ACCEL_FS_AVL_200G, 533 .value = 0x01, 534 .gain = IIO_G_TO_M_S_2(98000), 535 }, 536 [2] = { 537 .num = ST_ACCEL_FS_AVL_400G, 538 .value = 0x03, 539 .gain = IIO_G_TO_M_S_2(195000), 540 }, 541 }, 542 }, 543 .bdu = { 544 .addr = 0x23, 545 .mask = 0x80, 546 }, 547 .drdy_irq = { 548 .int1 = { 549 .addr = 0x22, 550 .mask = 0x02, 551 }, 552 .int2 = { 553 .addr = 0x22, 554 .mask = 0x10, 555 }, 556 .addr_ihl = 0x22, 557 .mask_ihl = 0x80, 558 }, 559 .sim = { 560 .addr = 0x23, 561 .value = BIT(0), 562 }, 563 .multi_read_bit = true, 564 .bootime = 2, 565 }, 566 { 567 /* No WAI register present */ 568 .sensors_supported = { 569 [0] = LIS3L02DQ_ACCEL_DEV_NAME, 570 }, 571 .ch = (struct iio_chan_spec *)st_accel_12bit_channels, 572 .odr = { 573 .addr = 0x20, 574 .mask = 0x30, 575 .odr_avl = { 576 { .hz = 280, .value = 0x00, }, 577 { .hz = 560, .value = 0x01, }, 578 { .hz = 1120, .value = 0x02, }, 579 { .hz = 4480, .value = 0x03, }, 580 }, 581 }, 582 .pw = { 583 .addr = 0x20, 584 .mask = 0xc0, 585 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE, 586 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, 587 }, 588 .enable_axis = { 589 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR, 590 .mask = ST_SENSORS_DEFAULT_AXIS_MASK, 591 }, 592 .fs = { 593 .fs_avl = { 594 [0] = { 595 .num = ST_ACCEL_FS_AVL_2G, 596 .gain = IIO_G_TO_M_S_2(488), 597 }, 598 }, 599 }, 600 /* 601 * The part has a BDU bit but if set the data is never 602 * updated so don't set it. 603 */ 604 .bdu = { 605 }, 606 .drdy_irq = { 607 .int1 = { 608 .addr = 0x21, 609 .mask = 0x04, 610 }, 611 .stat_drdy = { 612 .addr = ST_SENSORS_DEFAULT_STAT_ADDR, 613 .mask = 0x07, 614 }, 615 }, 616 .sim = { 617 .addr = 0x21, 618 .value = BIT(1), 619 }, 620 .multi_read_bit = false, 621 .bootime = 2, 622 }, 623 { 624 .wai = 0x33, 625 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS, 626 .sensors_supported = { 627 [0] = LNG2DM_ACCEL_DEV_NAME, 628 }, 629 .ch = (struct iio_chan_spec *)st_accel_8bit_channels, 630 .odr = { 631 .addr = 0x20, 632 .mask = 0xf0, 633 .odr_avl = { 634 { .hz = 1, .value = 0x01, }, 635 { .hz = 10, .value = 0x02, }, 636 { .hz = 25, .value = 0x03, }, 637 { .hz = 50, .value = 0x04, }, 638 { .hz = 100, .value = 0x05, }, 639 { .hz = 200, .value = 0x06, }, 640 { .hz = 400, .value = 0x07, }, 641 { .hz = 1600, .value = 0x08, }, 642 }, 643 }, 644 .pw = { 645 .addr = 0x20, 646 .mask = 0xf0, 647 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, 648 }, 649 .enable_axis = { 650 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR, 651 .mask = ST_SENSORS_DEFAULT_AXIS_MASK, 652 }, 653 .fs = { 654 .addr = 0x23, 655 .mask = 0x30, 656 .fs_avl = { 657 [0] = { 658 .num = ST_ACCEL_FS_AVL_2G, 659 .value = 0x00, 660 .gain = IIO_G_TO_M_S_2(15600), 661 }, 662 [1] = { 663 .num = ST_ACCEL_FS_AVL_4G, 664 .value = 0x01, 665 .gain = IIO_G_TO_M_S_2(31200), 666 }, 667 [2] = { 668 .num = ST_ACCEL_FS_AVL_8G, 669 .value = 0x02, 670 .gain = IIO_G_TO_M_S_2(62500), 671 }, 672 [3] = { 673 .num = ST_ACCEL_FS_AVL_16G, 674 .value = 0x03, 675 .gain = IIO_G_TO_M_S_2(187500), 676 }, 677 }, 678 }, 679 .drdy_irq = { 680 .int1 = { 681 .addr = 0x22, 682 .mask = 0x10, 683 }, 684 .addr_ihl = 0x25, 685 .mask_ihl = 0x02, 686 .stat_drdy = { 687 .addr = ST_SENSORS_DEFAULT_STAT_ADDR, 688 .mask = 0x07, 689 }, 690 }, 691 .sim = { 692 .addr = 0x23, 693 .value = BIT(0), 694 }, 695 .multi_read_bit = true, 696 .bootime = 2, 697 }, 698 { 699 .wai = 0x44, 700 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS, 701 .sensors_supported = { 702 [0] = LIS2DW12_ACCEL_DEV_NAME, 703 }, 704 .ch = (struct iio_chan_spec *)st_accel_12bit_channels, 705 .odr = { 706 .addr = 0x20, 707 .mask = 0xf0, 708 .odr_avl = { 709 { .hz = 1, .value = 0x01, }, 710 { .hz = 12, .value = 0x02, }, 711 { .hz = 25, .value = 0x03, }, 712 { .hz = 50, .value = 0x04, }, 713 { .hz = 100, .value = 0x05, }, 714 { .hz = 200, .value = 0x06, }, 715 }, 716 }, 717 .pw = { 718 .addr = 0x20, 719 .mask = 0xf0, 720 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, 721 }, 722 .fs = { 723 .addr = 0x25, 724 .mask = 0x30, 725 .fs_avl = { 726 [0] = { 727 .num = ST_ACCEL_FS_AVL_2G, 728 .value = 0x00, 729 .gain = IIO_G_TO_M_S_2(976), 730 }, 731 [1] = { 732 .num = ST_ACCEL_FS_AVL_4G, 733 .value = 0x01, 734 .gain = IIO_G_TO_M_S_2(1952), 735 }, 736 [2] = { 737 .num = ST_ACCEL_FS_AVL_8G, 738 .value = 0x02, 739 .gain = IIO_G_TO_M_S_2(3904), 740 }, 741 [3] = { 742 .num = ST_ACCEL_FS_AVL_16G, 743 .value = 0x03, 744 .gain = IIO_G_TO_M_S_2(7808), 745 }, 746 }, 747 }, 748 .bdu = { 749 .addr = 0x21, 750 .mask = 0x08, 751 }, 752 .drdy_irq = { 753 .int1 = { 754 .addr = 0x23, 755 .mask = 0x01, 756 .addr_od = 0x22, 757 .mask_od = 0x20, 758 }, 759 .int2 = { 760 .addr = 0x24, 761 .mask = 0x01, 762 .addr_od = 0x22, 763 .mask_od = 0x20, 764 }, 765 .addr_ihl = 0x22, 766 .mask_ihl = 0x08, 767 .stat_drdy = { 768 .addr = ST_SENSORS_DEFAULT_STAT_ADDR, 769 .mask = 0x01, 770 }, 771 }, 772 .sim = { 773 .addr = 0x21, 774 .value = BIT(0), 775 }, 776 .multi_read_bit = false, 777 .bootime = 2, 778 }, 779 { 780 .wai = 0x11, 781 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS, 782 .sensors_supported = { 783 [0] = LIS3DHH_ACCEL_DEV_NAME, 784 }, 785 .ch = (struct iio_chan_spec *)st_accel_16bit_channels, 786 .odr = { 787 /* just ODR = 1100Hz available */ 788 .odr_avl = { 789 { .hz = 1100, .value = 0x00, }, 790 }, 791 }, 792 .pw = { 793 .addr = 0x20, 794 .mask = 0x80, 795 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE, 796 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, 797 }, 798 .fs = { 799 .fs_avl = { 800 [0] = { 801 .num = ST_ACCEL_FS_AVL_2G, 802 .gain = IIO_G_TO_M_S_2(76), 803 }, 804 }, 805 }, 806 .bdu = { 807 .addr = 0x20, 808 .mask = 0x01, 809 }, 810 .drdy_irq = { 811 .int1 = { 812 .addr = 0x21, 813 .mask = 0x80, 814 .addr_od = 0x23, 815 .mask_od = 0x04, 816 }, 817 .int2 = { 818 .addr = 0x22, 819 .mask = 0x80, 820 .addr_od = 0x23, 821 .mask_od = 0x08, 822 }, 823 .stat_drdy = { 824 .addr = ST_SENSORS_DEFAULT_STAT_ADDR, 825 .mask = 0x07, 826 }, 827 }, 828 .multi_read_bit = false, 829 .bootime = 2, 830 }, 831 { 832 .wai = 0x33, 833 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS, 834 .sensors_supported = { 835 [0] = LIS2DE12_ACCEL_DEV_NAME, 836 }, 837 .ch = (struct iio_chan_spec *)st_accel_8bit_channels, 838 .odr = { 839 .addr = 0x20, 840 .mask = 0xf0, 841 .odr_avl = { 842 { .hz = 1, .value = 0x01, }, 843 { .hz = 10, .value = 0x02, }, 844 { .hz = 25, .value = 0x03, }, 845 { .hz = 50, .value = 0x04, }, 846 { .hz = 100, .value = 0x05, }, 847 { .hz = 200, .value = 0x06, }, 848 { .hz = 400, .value = 0x07, }, 849 { .hz = 1620, .value = 0x08, }, 850 { .hz = 5376, .value = 0x09, }, 851 }, 852 }, 853 .pw = { 854 .addr = 0x20, 855 .mask = 0xf0, 856 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, 857 }, 858 .enable_axis = { 859 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR, 860 .mask = ST_SENSORS_DEFAULT_AXIS_MASK, 861 }, 862 .fs = { 863 .addr = 0x23, 864 .mask = 0x30, 865 .fs_avl = { 866 [0] = { 867 .num = ST_ACCEL_FS_AVL_2G, 868 .value = 0x00, 869 .gain = IIO_G_TO_M_S_2(15600), 870 }, 871 [1] = { 872 .num = ST_ACCEL_FS_AVL_4G, 873 .value = 0x01, 874 .gain = IIO_G_TO_M_S_2(31200), 875 }, 876 [2] = { 877 .num = ST_ACCEL_FS_AVL_8G, 878 .value = 0x02, 879 .gain = IIO_G_TO_M_S_2(62500), 880 }, 881 [3] = { 882 .num = ST_ACCEL_FS_AVL_16G, 883 .value = 0x03, 884 .gain = IIO_G_TO_M_S_2(187500), 885 }, 886 }, 887 }, 888 .drdy_irq = { 889 .int1 = { 890 .addr = 0x22, 891 .mask = 0x10, 892 }, 893 .addr_ihl = 0x25, 894 .mask_ihl = 0x02, 895 .stat_drdy = { 896 .addr = ST_SENSORS_DEFAULT_STAT_ADDR, 897 .mask = 0x07, 898 }, 899 }, 900 .sim = { 901 .addr = 0x23, 902 .value = BIT(0), 903 }, 904 .multi_read_bit = true, 905 .bootime = 2, 906 }, 907 }; 908 909 static int st_accel_read_raw(struct iio_dev *indio_dev, 910 struct iio_chan_spec const *ch, int *val, 911 int *val2, long mask) 912 { 913 int err; 914 struct st_sensor_data *adata = iio_priv(indio_dev); 915 916 switch (mask) { 917 case IIO_CHAN_INFO_RAW: 918 err = st_sensors_read_info_raw(indio_dev, ch, val); 919 if (err < 0) 920 goto read_error; 921 922 return IIO_VAL_INT; 923 case IIO_CHAN_INFO_SCALE: 924 *val = adata->current_fullscale->gain / 1000000; 925 *val2 = adata->current_fullscale->gain % 1000000; 926 return IIO_VAL_INT_PLUS_MICRO; 927 case IIO_CHAN_INFO_SAMP_FREQ: 928 *val = adata->odr; 929 return IIO_VAL_INT; 930 default: 931 return -EINVAL; 932 } 933 934 read_error: 935 return err; 936 } 937 938 static int st_accel_write_raw(struct iio_dev *indio_dev, 939 struct iio_chan_spec const *chan, int val, int val2, long mask) 940 { 941 int err; 942 943 switch (mask) { 944 case IIO_CHAN_INFO_SCALE: { 945 int gain; 946 947 gain = val * 1000000 + val2; 948 err = st_sensors_set_fullscale_by_gain(indio_dev, gain); 949 break; 950 } 951 case IIO_CHAN_INFO_SAMP_FREQ: 952 if (val2) 953 return -EINVAL; 954 mutex_lock(&indio_dev->mlock); 955 err = st_sensors_set_odr(indio_dev, val); 956 mutex_unlock(&indio_dev->mlock); 957 return err; 958 default: 959 return -EINVAL; 960 } 961 962 return err; 963 } 964 965 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL(); 966 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_accel_scale_available); 967 968 static struct attribute *st_accel_attributes[] = { 969 &iio_dev_attr_sampling_frequency_available.dev_attr.attr, 970 &iio_dev_attr_in_accel_scale_available.dev_attr.attr, 971 NULL, 972 }; 973 974 static const struct attribute_group st_accel_attribute_group = { 975 .attrs = st_accel_attributes, 976 }; 977 978 static const struct iio_info accel_info = { 979 .attrs = &st_accel_attribute_group, 980 .read_raw = &st_accel_read_raw, 981 .write_raw = &st_accel_write_raw, 982 .debugfs_reg_access = &st_sensors_debugfs_reg_access, 983 }; 984 985 #ifdef CONFIG_IIO_TRIGGER 986 static const struct iio_trigger_ops st_accel_trigger_ops = { 987 .set_trigger_state = ST_ACCEL_TRIGGER_SET_STATE, 988 .validate_device = st_sensors_validate_device, 989 }; 990 #define ST_ACCEL_TRIGGER_OPS (&st_accel_trigger_ops) 991 #else 992 #define ST_ACCEL_TRIGGER_OPS NULL 993 #endif 994 995 static const struct iio_mount_matrix * 996 get_mount_matrix(const struct iio_dev *indio_dev, 997 const struct iio_chan_spec *chan) 998 { 999 struct st_sensor_data *adata = iio_priv(indio_dev); 1000 1001 return adata->mount_matrix; 1002 } 1003 1004 static const struct iio_chan_spec_ext_info mount_matrix_ext_info[] = { 1005 IIO_MOUNT_MATRIX(IIO_SHARED_BY_ALL, get_mount_matrix), 1006 { }, 1007 }; 1008 1009 /* Read ST-specific _ONT orientation data from ACPI and generate an 1010 * appropriate mount matrix. 1011 */ 1012 static int apply_acpi_orientation(struct iio_dev *indio_dev, 1013 struct iio_chan_spec *channels) 1014 { 1015 #ifdef CONFIG_ACPI 1016 struct st_sensor_data *adata = iio_priv(indio_dev); 1017 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; 1018 struct acpi_device *adev; 1019 union acpi_object *ont; 1020 union acpi_object *elements; 1021 acpi_status status; 1022 int ret = -EINVAL; 1023 unsigned int val; 1024 int i, j; 1025 int final_ont[3][3] = { { 0 }, }; 1026 1027 /* For some reason, ST's _ONT translation does not apply directly 1028 * to the data read from the sensor. Another translation must be 1029 * performed first, as described by the matrix below. Perhaps 1030 * ST required this specific translation for the first product 1031 * where the device was mounted? 1032 */ 1033 const int default_ont[3][3] = { 1034 { 0, 1, 0 }, 1035 { -1, 0, 0 }, 1036 { 0, 0, -1 }, 1037 }; 1038 1039 1040 adev = ACPI_COMPANION(adata->dev); 1041 if (!adev) 1042 return 0; 1043 1044 /* Read _ONT data, which should be a package of 6 integers. */ 1045 status = acpi_evaluate_object(adev->handle, "_ONT", NULL, &buffer); 1046 if (status == AE_NOT_FOUND) { 1047 return 0; 1048 } else if (ACPI_FAILURE(status)) { 1049 dev_warn(&indio_dev->dev, "failed to execute _ONT: %d\n", 1050 status); 1051 return status; 1052 } 1053 1054 ont = buffer.pointer; 1055 if (ont->type != ACPI_TYPE_PACKAGE || ont->package.count != 6) 1056 goto out; 1057 1058 /* The first 3 integers provide axis order information. 1059 * e.g. 0 1 2 would indicate normal X,Y,Z ordering. 1060 * e.g. 1 0 2 indicates that data arrives in order Y,X,Z. 1061 */ 1062 elements = ont->package.elements; 1063 for (i = 0; i < 3; i++) { 1064 if (elements[i].type != ACPI_TYPE_INTEGER) 1065 goto out; 1066 1067 val = elements[i].integer.value; 1068 if (val > 2) 1069 goto out; 1070 1071 /* Avoiding full matrix multiplication, we simply reorder the 1072 * columns in the default_ont matrix according to the 1073 * ordering provided by _ONT. 1074 */ 1075 final_ont[0][i] = default_ont[0][val]; 1076 final_ont[1][i] = default_ont[1][val]; 1077 final_ont[2][i] = default_ont[2][val]; 1078 } 1079 1080 /* The final 3 integers provide sign flip information. 1081 * 0 means no change, 1 means flip. 1082 * e.g. 0 0 1 means that Z data should be sign-flipped. 1083 * This is applied after the axis reordering from above. 1084 */ 1085 elements += 3; 1086 for (i = 0; i < 3; i++) { 1087 if (elements[i].type != ACPI_TYPE_INTEGER) 1088 goto out; 1089 1090 val = elements[i].integer.value; 1091 if (val != 0 && val != 1) 1092 goto out; 1093 if (!val) 1094 continue; 1095 1096 /* Flip the values in the indicated column */ 1097 final_ont[0][i] *= -1; 1098 final_ont[1][i] *= -1; 1099 final_ont[2][i] *= -1; 1100 } 1101 1102 /* Convert our integer matrix to a string-based iio_mount_matrix */ 1103 adata->mount_matrix = devm_kmalloc(&indio_dev->dev, 1104 sizeof(*adata->mount_matrix), 1105 GFP_KERNEL); 1106 if (!adata->mount_matrix) { 1107 ret = -ENOMEM; 1108 goto out; 1109 } 1110 1111 for (i = 0; i < 3; i++) { 1112 for (j = 0; j < 3; j++) { 1113 int matrix_val = final_ont[i][j]; 1114 char *str_value; 1115 1116 switch (matrix_val) { 1117 case -1: 1118 str_value = "-1"; 1119 break; 1120 case 0: 1121 str_value = "0"; 1122 break; 1123 case 1: 1124 str_value = "1"; 1125 break; 1126 default: 1127 goto out; 1128 } 1129 adata->mount_matrix->rotation[i * 3 + j] = str_value; 1130 } 1131 } 1132 1133 /* Expose the mount matrix via ext_info */ 1134 for (i = 0; i < indio_dev->num_channels; i++) 1135 channels[i].ext_info = mount_matrix_ext_info; 1136 1137 ret = 0; 1138 dev_info(&indio_dev->dev, "computed mount matrix from ACPI\n"); 1139 1140 out: 1141 kfree(buffer.pointer); 1142 return ret; 1143 #else /* !CONFIG_ACPI */ 1144 return 0; 1145 #endif 1146 } 1147 1148 /* 1149 * st_accel_get_settings() - get sensor settings from device name 1150 * @name: device name buffer reference. 1151 * 1152 * Return: valid reference on success, NULL otherwise. 1153 */ 1154 const struct st_sensor_settings *st_accel_get_settings(const char *name) 1155 { 1156 int index = st_sensors_get_settings_index(name, 1157 st_accel_sensors_settings, 1158 ARRAY_SIZE(st_accel_sensors_settings)); 1159 if (index < 0) 1160 return NULL; 1161 1162 return &st_accel_sensors_settings[index]; 1163 } 1164 EXPORT_SYMBOL(st_accel_get_settings); 1165 1166 int st_accel_common_probe(struct iio_dev *indio_dev) 1167 { 1168 struct st_sensor_data *adata = iio_priv(indio_dev); 1169 struct st_sensors_platform_data *pdata = 1170 (struct st_sensors_platform_data *)adata->dev->platform_data; 1171 struct iio_chan_spec *channels; 1172 size_t channels_size; 1173 int err; 1174 1175 indio_dev->modes = INDIO_DIRECT_MODE; 1176 indio_dev->info = &accel_info; 1177 1178 err = st_sensors_power_enable(indio_dev); 1179 if (err) 1180 return err; 1181 1182 err = st_sensors_verify_id(indio_dev); 1183 if (err < 0) 1184 goto st_accel_power_off; 1185 1186 adata->num_data_channels = ST_ACCEL_NUMBER_DATA_CHANNELS; 1187 indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS; 1188 1189 channels_size = indio_dev->num_channels * sizeof(struct iio_chan_spec); 1190 channels = devm_kmemdup(&indio_dev->dev, 1191 adata->sensor_settings->ch, 1192 channels_size, GFP_KERNEL); 1193 if (!channels) { 1194 err = -ENOMEM; 1195 goto st_accel_power_off; 1196 } 1197 1198 if (apply_acpi_orientation(indio_dev, channels)) 1199 dev_warn(&indio_dev->dev, 1200 "failed to apply ACPI orientation data: %d\n", err); 1201 1202 indio_dev->channels = channels; 1203 adata->current_fullscale = (struct st_sensor_fullscale_avl *) 1204 &adata->sensor_settings->fs.fs_avl[0]; 1205 adata->odr = adata->sensor_settings->odr.odr_avl[0].hz; 1206 1207 if (!pdata) 1208 pdata = (struct st_sensors_platform_data *)&default_accel_pdata; 1209 1210 err = st_sensors_init_sensor(indio_dev, pdata); 1211 if (err < 0) 1212 goto st_accel_power_off; 1213 1214 err = st_accel_allocate_ring(indio_dev); 1215 if (err < 0) 1216 goto st_accel_power_off; 1217 1218 if (adata->irq > 0) { 1219 err = st_sensors_allocate_trigger(indio_dev, 1220 ST_ACCEL_TRIGGER_OPS); 1221 if (err < 0) 1222 goto st_accel_probe_trigger_error; 1223 } 1224 1225 err = iio_device_register(indio_dev); 1226 if (err) 1227 goto st_accel_device_register_error; 1228 1229 dev_info(&indio_dev->dev, "registered accelerometer %s\n", 1230 indio_dev->name); 1231 1232 return 0; 1233 1234 st_accel_device_register_error: 1235 if (adata->irq > 0) 1236 st_sensors_deallocate_trigger(indio_dev); 1237 st_accel_probe_trigger_error: 1238 st_accel_deallocate_ring(indio_dev); 1239 st_accel_power_off: 1240 st_sensors_power_disable(indio_dev); 1241 1242 return err; 1243 } 1244 EXPORT_SYMBOL(st_accel_common_probe); 1245 1246 void st_accel_common_remove(struct iio_dev *indio_dev) 1247 { 1248 struct st_sensor_data *adata = iio_priv(indio_dev); 1249 1250 st_sensors_power_disable(indio_dev); 1251 1252 iio_device_unregister(indio_dev); 1253 if (adata->irq > 0) 1254 st_sensors_deallocate_trigger(indio_dev); 1255 1256 st_accel_deallocate_ring(indio_dev); 1257 } 1258 EXPORT_SYMBOL(st_accel_common_remove); 1259 1260 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>"); 1261 MODULE_DESCRIPTION("STMicroelectronics accelerometers driver"); 1262 MODULE_LICENSE("GPL v2"); 1263