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