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