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