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 }, 107 .ch = (struct iio_chan_spec *)st_accel_12bit_channels, 108 .odr = { 109 .addr = 0x20, 110 .mask = 0xf0, 111 .odr_avl = { 112 { .hz = 1, .value = 0x01, }, 113 { .hz = 10, .value = 0x02, }, 114 { .hz = 25, .value = 0x03, }, 115 { .hz = 50, .value = 0x04, }, 116 { .hz = 100, .value = 0x05, }, 117 { .hz = 200, .value = 0x06, }, 118 { .hz = 400, .value = 0x07, }, 119 { .hz = 1600, .value = 0x08, }, 120 }, 121 }, 122 .pw = { 123 .addr = 0x20, 124 .mask = 0xf0, 125 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, 126 }, 127 .enable_axis = { 128 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR, 129 .mask = ST_SENSORS_DEFAULT_AXIS_MASK, 130 }, 131 .fs = { 132 .addr = 0x23, 133 .mask = 0x30, 134 .fs_avl = { 135 [0] = { 136 .num = ST_ACCEL_FS_AVL_2G, 137 .value = 0x00, 138 .gain = IIO_G_TO_M_S_2(1000), 139 }, 140 [1] = { 141 .num = ST_ACCEL_FS_AVL_4G, 142 .value = 0x01, 143 .gain = IIO_G_TO_M_S_2(2000), 144 }, 145 [2] = { 146 .num = ST_ACCEL_FS_AVL_8G, 147 .value = 0x02, 148 .gain = IIO_G_TO_M_S_2(4000), 149 }, 150 [3] = { 151 .num = ST_ACCEL_FS_AVL_16G, 152 .value = 0x03, 153 .gain = IIO_G_TO_M_S_2(12000), 154 }, 155 }, 156 }, 157 .bdu = { 158 .addr = 0x23, 159 .mask = 0x80, 160 }, 161 .drdy_irq = { 162 .addr = 0x22, 163 .mask_int1 = 0x10, 164 .mask_int2 = 0x08, 165 .addr_ihl = 0x25, 166 .mask_ihl = 0x02, 167 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR, 168 }, 169 .sim = { 170 .addr = 0x23, 171 .value = BIT(0), 172 }, 173 .multi_read_bit = true, 174 .bootime = 2, 175 }, 176 { 177 .wai = 0x32, 178 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS, 179 .sensors_supported = { 180 [0] = LIS331DLH_ACCEL_DEV_NAME, 181 [1] = LSM303DL_ACCEL_DEV_NAME, 182 [2] = LSM303DLH_ACCEL_DEV_NAME, 183 [3] = LSM303DLM_ACCEL_DEV_NAME, 184 }, 185 .ch = (struct iio_chan_spec *)st_accel_12bit_channels, 186 .odr = { 187 .addr = 0x20, 188 .mask = 0x18, 189 .odr_avl = { 190 { .hz = 50, .value = 0x00, }, 191 { .hz = 100, .value = 0x01, }, 192 { .hz = 400, .value = 0x02, }, 193 { .hz = 1000, .value = 0x03, }, 194 }, 195 }, 196 .pw = { 197 .addr = 0x20, 198 .mask = 0xe0, 199 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE, 200 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, 201 }, 202 .enable_axis = { 203 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR, 204 .mask = ST_SENSORS_DEFAULT_AXIS_MASK, 205 }, 206 .fs = { 207 .addr = 0x23, 208 .mask = 0x30, 209 .fs_avl = { 210 [0] = { 211 .num = ST_ACCEL_FS_AVL_2G, 212 .value = 0x00, 213 .gain = IIO_G_TO_M_S_2(1000), 214 }, 215 [1] = { 216 .num = ST_ACCEL_FS_AVL_4G, 217 .value = 0x01, 218 .gain = IIO_G_TO_M_S_2(2000), 219 }, 220 [2] = { 221 .num = ST_ACCEL_FS_AVL_8G, 222 .value = 0x03, 223 .gain = IIO_G_TO_M_S_2(3900), 224 }, 225 }, 226 }, 227 .bdu = { 228 .addr = 0x23, 229 .mask = 0x80, 230 }, 231 .drdy_irq = { 232 .addr = 0x22, 233 .mask_int1 = 0x02, 234 .mask_int2 = 0x10, 235 .addr_ihl = 0x22, 236 .mask_ihl = 0x80, 237 .addr_od = 0x22, 238 .mask_od = 0x40, 239 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR, 240 }, 241 .sim = { 242 .addr = 0x23, 243 .value = BIT(0), 244 }, 245 .multi_read_bit = true, 246 .bootime = 2, 247 }, 248 { 249 .wai = 0x40, 250 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS, 251 .sensors_supported = { 252 [0] = LSM330_ACCEL_DEV_NAME, 253 }, 254 .ch = (struct iio_chan_spec *)st_accel_16bit_channels, 255 .odr = { 256 .addr = 0x20, 257 .mask = 0xf0, 258 .odr_avl = { 259 { .hz = 3, .value = 0x01, }, 260 { .hz = 6, .value = 0x02, }, 261 { .hz = 12, .value = 0x03, }, 262 { .hz = 25, .value = 0x04, }, 263 { .hz = 50, .value = 0x05, }, 264 { .hz = 100, .value = 0x06, }, 265 { .hz = 200, .value = 0x07, }, 266 { .hz = 400, .value = 0x08, }, 267 { .hz = 800, .value = 0x09, }, 268 { .hz = 1600, .value = 0x0a, }, 269 }, 270 }, 271 .pw = { 272 .addr = 0x20, 273 .mask = 0xf0, 274 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, 275 }, 276 .enable_axis = { 277 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR, 278 .mask = ST_SENSORS_DEFAULT_AXIS_MASK, 279 }, 280 .fs = { 281 .addr = 0x24, 282 .mask = 0x38, 283 .fs_avl = { 284 [0] = { 285 .num = ST_ACCEL_FS_AVL_2G, 286 .value = 0x00, 287 .gain = IIO_G_TO_M_S_2(61), 288 }, 289 [1] = { 290 .num = ST_ACCEL_FS_AVL_4G, 291 .value = 0x01, 292 .gain = IIO_G_TO_M_S_2(122), 293 }, 294 [2] = { 295 .num = ST_ACCEL_FS_AVL_6G, 296 .value = 0x02, 297 .gain = IIO_G_TO_M_S_2(183), 298 }, 299 [3] = { 300 .num = ST_ACCEL_FS_AVL_8G, 301 .value = 0x03, 302 .gain = IIO_G_TO_M_S_2(244), 303 }, 304 [4] = { 305 .num = ST_ACCEL_FS_AVL_16G, 306 .value = 0x04, 307 .gain = IIO_G_TO_M_S_2(732), 308 }, 309 }, 310 }, 311 .bdu = { 312 .addr = 0x20, 313 .mask = 0x08, 314 }, 315 .drdy_irq = { 316 .addr = 0x23, 317 .mask_int1 = 0x80, 318 .mask_int2 = 0x00, 319 .addr_ihl = 0x23, 320 .mask_ihl = 0x40, 321 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR, 322 .ig1 = { 323 .en_addr = 0x23, 324 .en_mask = 0x08, 325 }, 326 }, 327 .sim = { 328 .addr = 0x24, 329 .value = BIT(0), 330 }, 331 .multi_read_bit = false, 332 .bootime = 2, 333 }, 334 { 335 .wai = 0x3a, 336 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS, 337 .sensors_supported = { 338 [0] = LIS3LV02DL_ACCEL_DEV_NAME, 339 }, 340 .ch = (struct iio_chan_spec *)st_accel_12bit_channels, 341 .odr = { 342 .addr = 0x20, 343 .mask = 0x30, /* DF1 and DF0 */ 344 .odr_avl = { 345 { .hz = 40, .value = 0x00, }, 346 { .hz = 160, .value = 0x01, }, 347 { .hz = 640, .value = 0x02, }, 348 { .hz = 2560, .value = 0x03, }, 349 }, 350 }, 351 .pw = { 352 .addr = 0x20, 353 .mask = 0xc0, 354 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE, 355 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, 356 }, 357 .enable_axis = { 358 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR, 359 .mask = ST_SENSORS_DEFAULT_AXIS_MASK, 360 }, 361 .fs = { 362 .addr = 0x21, 363 .mask = 0x80, 364 .fs_avl = { 365 [0] = { 366 .num = ST_ACCEL_FS_AVL_2G, 367 .value = 0x00, 368 .gain = IIO_G_TO_M_S_2(1000), 369 }, 370 [1] = { 371 .num = ST_ACCEL_FS_AVL_6G, 372 .value = 0x01, 373 .gain = IIO_G_TO_M_S_2(3000), 374 }, 375 }, 376 }, 377 .bdu = { 378 .addr = 0x21, 379 .mask = 0x40, 380 }, 381 /* 382 * Data Alignment Setting - needs to be set to get 383 * left-justified data like all other sensors. 384 */ 385 .das = { 386 .addr = 0x21, 387 .mask = 0x01, 388 }, 389 .drdy_irq = { 390 .addr = 0x21, 391 .mask_int1 = 0x04, 392 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR, 393 }, 394 .sim = { 395 .addr = 0x21, 396 .value = BIT(1), 397 }, 398 .multi_read_bit = true, 399 .bootime = 2, /* guess */ 400 }, 401 { 402 .wai = 0x3b, 403 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS, 404 .sensors_supported = { 405 [0] = LIS331DL_ACCEL_DEV_NAME, 406 }, 407 .ch = (struct iio_chan_spec *)st_accel_8bit_channels, 408 .odr = { 409 .addr = 0x20, 410 .mask = 0x80, 411 .odr_avl = { 412 { .hz = 100, .value = 0x00, }, 413 { .hz = 400, .value = 0x01, }, 414 }, 415 }, 416 .pw = { 417 .addr = 0x20, 418 .mask = 0x40, 419 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE, 420 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, 421 }, 422 .enable_axis = { 423 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR, 424 .mask = ST_SENSORS_DEFAULT_AXIS_MASK, 425 }, 426 .fs = { 427 .addr = 0x20, 428 .mask = 0x20, 429 /* 430 * TODO: check these resulting gain settings, these are 431 * not in the datsheet 432 */ 433 .fs_avl = { 434 [0] = { 435 .num = ST_ACCEL_FS_AVL_2G, 436 .value = 0x00, 437 .gain = IIO_G_TO_M_S_2(18000), 438 }, 439 [1] = { 440 .num = ST_ACCEL_FS_AVL_8G, 441 .value = 0x01, 442 .gain = IIO_G_TO_M_S_2(72000), 443 }, 444 }, 445 }, 446 .drdy_irq = { 447 .addr = 0x22, 448 .mask_int1 = 0x04, 449 .mask_int2 = 0x20, 450 .addr_ihl = 0x22, 451 .mask_ihl = 0x80, 452 .addr_od = 0x22, 453 .mask_od = 0x40, 454 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR, 455 }, 456 .sim = { 457 .addr = 0x21, 458 .value = BIT(7), 459 }, 460 .multi_read_bit = false, 461 .bootime = 2, /* guess */ 462 }, 463 { 464 .wai = 0x32, 465 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS, 466 .sensors_supported = { 467 [0] = H3LIS331DL_DRIVER_NAME, 468 }, 469 .ch = (struct iio_chan_spec *)st_accel_12bit_channels, 470 .odr = { 471 .addr = 0x20, 472 .mask = 0x18, 473 .odr_avl = { 474 { .hz = 50, .value = 0x00, }, 475 { .hz = 100, .value = 0x01, }, 476 { .hz = 400, .value = 0x02, }, 477 { .hz = 1000, .value = 0x03, }, 478 }, 479 }, 480 .pw = { 481 .addr = 0x20, 482 .mask = 0x20, 483 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE, 484 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, 485 }, 486 .enable_axis = { 487 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR, 488 .mask = ST_SENSORS_DEFAULT_AXIS_MASK, 489 }, 490 .fs = { 491 .addr = 0x23, 492 .mask = 0x30, 493 .fs_avl = { 494 [0] = { 495 .num = ST_ACCEL_FS_AVL_100G, 496 .value = 0x00, 497 .gain = IIO_G_TO_M_S_2(49000), 498 }, 499 [1] = { 500 .num = ST_ACCEL_FS_AVL_200G, 501 .value = 0x01, 502 .gain = IIO_G_TO_M_S_2(98000), 503 }, 504 [2] = { 505 .num = ST_ACCEL_FS_AVL_400G, 506 .value = 0x03, 507 .gain = IIO_G_TO_M_S_2(195000), 508 }, 509 }, 510 }, 511 .bdu = { 512 .addr = 0x23, 513 .mask = 0x80, 514 }, 515 .drdy_irq = { 516 .addr = 0x22, 517 .mask_int1 = 0x02, 518 .mask_int2 = 0x10, 519 .addr_ihl = 0x22, 520 .mask_ihl = 0x80, 521 }, 522 .sim = { 523 .addr = 0x23, 524 .value = BIT(0), 525 }, 526 .multi_read_bit = true, 527 .bootime = 2, 528 }, 529 { 530 /* No WAI register present */ 531 .sensors_supported = { 532 [0] = LIS3L02DQ_ACCEL_DEV_NAME, 533 }, 534 .ch = (struct iio_chan_spec *)st_accel_12bit_channels, 535 .odr = { 536 .addr = 0x20, 537 .mask = 0x30, 538 .odr_avl = { 539 { .hz = 280, .value = 0x00, }, 540 { .hz = 560, .value = 0x01, }, 541 { .hz = 1120, .value = 0x02, }, 542 { .hz = 4480, .value = 0x03, }, 543 }, 544 }, 545 .pw = { 546 .addr = 0x20, 547 .mask = 0xc0, 548 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE, 549 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, 550 }, 551 .enable_axis = { 552 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR, 553 .mask = ST_SENSORS_DEFAULT_AXIS_MASK, 554 }, 555 .fs = { 556 .fs_avl = { 557 [0] = { 558 .num = ST_ACCEL_FS_AVL_2G, 559 .gain = IIO_G_TO_M_S_2(488), 560 }, 561 }, 562 }, 563 /* 564 * The part has a BDU bit but if set the data is never 565 * updated so don't set it. 566 */ 567 .bdu = { 568 }, 569 .drdy_irq = { 570 .addr = 0x21, 571 .mask_int1 = 0x04, 572 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR, 573 }, 574 .sim = { 575 .addr = 0x21, 576 .value = BIT(1), 577 }, 578 .multi_read_bit = false, 579 .bootime = 2, 580 }, 581 { 582 .wai = 0x33, 583 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS, 584 .sensors_supported = { 585 [0] = LNG2DM_ACCEL_DEV_NAME, 586 }, 587 .ch = (struct iio_chan_spec *)st_accel_8bit_channels, 588 .odr = { 589 .addr = 0x20, 590 .mask = 0xf0, 591 .odr_avl = { 592 { .hz = 1, .value = 0x01, }, 593 { .hz = 10, .value = 0x02, }, 594 { .hz = 25, .value = 0x03, }, 595 { .hz = 50, .value = 0x04, }, 596 { .hz = 100, .value = 0x05, }, 597 { .hz = 200, .value = 0x06, }, 598 { .hz = 400, .value = 0x07, }, 599 { .hz = 1600, .value = 0x08, }, 600 }, 601 }, 602 .pw = { 603 .addr = 0x20, 604 .mask = 0xf0, 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 .addr = 0x23, 613 .mask = 0x30, 614 .fs_avl = { 615 [0] = { 616 .num = ST_ACCEL_FS_AVL_2G, 617 .value = 0x00, 618 .gain = IIO_G_TO_M_S_2(15600), 619 }, 620 [1] = { 621 .num = ST_ACCEL_FS_AVL_4G, 622 .value = 0x01, 623 .gain = IIO_G_TO_M_S_2(31200), 624 }, 625 [2] = { 626 .num = ST_ACCEL_FS_AVL_8G, 627 .value = 0x02, 628 .gain = IIO_G_TO_M_S_2(62500), 629 }, 630 [3] = { 631 .num = ST_ACCEL_FS_AVL_16G, 632 .value = 0x03, 633 .gain = IIO_G_TO_M_S_2(187500), 634 }, 635 }, 636 }, 637 .drdy_irq = { 638 .addr = 0x22, 639 .mask_int1 = 0x10, 640 .mask_int2 = 0x08, 641 .addr_ihl = 0x25, 642 .mask_ihl = 0x02, 643 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR, 644 }, 645 .sim = { 646 .addr = 0x23, 647 .value = BIT(0), 648 }, 649 .multi_read_bit = true, 650 .bootime = 2, 651 }, 652 }; 653 654 static int st_accel_read_raw(struct iio_dev *indio_dev, 655 struct iio_chan_spec const *ch, int *val, 656 int *val2, long mask) 657 { 658 int err; 659 struct st_sensor_data *adata = iio_priv(indio_dev); 660 661 switch (mask) { 662 case IIO_CHAN_INFO_RAW: 663 err = st_sensors_read_info_raw(indio_dev, ch, val); 664 if (err < 0) 665 goto read_error; 666 667 return IIO_VAL_INT; 668 case IIO_CHAN_INFO_SCALE: 669 *val = adata->current_fullscale->gain / 1000000; 670 *val2 = adata->current_fullscale->gain % 1000000; 671 return IIO_VAL_INT_PLUS_MICRO; 672 case IIO_CHAN_INFO_SAMP_FREQ: 673 *val = adata->odr; 674 return IIO_VAL_INT; 675 default: 676 return -EINVAL; 677 } 678 679 read_error: 680 return err; 681 } 682 683 static int st_accel_write_raw(struct iio_dev *indio_dev, 684 struct iio_chan_spec const *chan, int val, int val2, long mask) 685 { 686 int err; 687 688 switch (mask) { 689 case IIO_CHAN_INFO_SCALE: { 690 int gain; 691 692 gain = val * 1000000 + val2; 693 err = st_sensors_set_fullscale_by_gain(indio_dev, gain); 694 break; 695 } 696 case IIO_CHAN_INFO_SAMP_FREQ: 697 if (val2) 698 return -EINVAL; 699 mutex_lock(&indio_dev->mlock); 700 err = st_sensors_set_odr(indio_dev, val); 701 mutex_unlock(&indio_dev->mlock); 702 return err; 703 default: 704 return -EINVAL; 705 } 706 707 return err; 708 } 709 710 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL(); 711 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_accel_scale_available); 712 713 static struct attribute *st_accel_attributes[] = { 714 &iio_dev_attr_sampling_frequency_available.dev_attr.attr, 715 &iio_dev_attr_in_accel_scale_available.dev_attr.attr, 716 NULL, 717 }; 718 719 static const struct attribute_group st_accel_attribute_group = { 720 .attrs = st_accel_attributes, 721 }; 722 723 static const struct iio_info accel_info = { 724 .driver_module = THIS_MODULE, 725 .attrs = &st_accel_attribute_group, 726 .read_raw = &st_accel_read_raw, 727 .write_raw = &st_accel_write_raw, 728 .debugfs_reg_access = &st_sensors_debugfs_reg_access, 729 }; 730 731 #ifdef CONFIG_IIO_TRIGGER 732 static const struct iio_trigger_ops st_accel_trigger_ops = { 733 .owner = THIS_MODULE, 734 .set_trigger_state = ST_ACCEL_TRIGGER_SET_STATE, 735 .validate_device = st_sensors_validate_device, 736 }; 737 #define ST_ACCEL_TRIGGER_OPS (&st_accel_trigger_ops) 738 #else 739 #define ST_ACCEL_TRIGGER_OPS NULL 740 #endif 741 742 int st_accel_common_probe(struct iio_dev *indio_dev) 743 { 744 struct st_sensor_data *adata = iio_priv(indio_dev); 745 struct st_sensors_platform_data *pdata = 746 (struct st_sensors_platform_data *)adata->dev->platform_data; 747 int irq = adata->get_irq_data_ready(indio_dev); 748 int err; 749 750 indio_dev->modes = INDIO_DIRECT_MODE; 751 indio_dev->info = &accel_info; 752 mutex_init(&adata->tb.buf_lock); 753 754 err = st_sensors_power_enable(indio_dev); 755 if (err) 756 return err; 757 758 err = st_sensors_check_device_support(indio_dev, 759 ARRAY_SIZE(st_accel_sensors_settings), 760 st_accel_sensors_settings); 761 if (err < 0) 762 goto st_accel_power_off; 763 764 adata->num_data_channels = ST_ACCEL_NUMBER_DATA_CHANNELS; 765 adata->multiread_bit = adata->sensor_settings->multi_read_bit; 766 indio_dev->channels = adata->sensor_settings->ch; 767 indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS; 768 769 adata->current_fullscale = (struct st_sensor_fullscale_avl *) 770 &adata->sensor_settings->fs.fs_avl[0]; 771 adata->odr = adata->sensor_settings->odr.odr_avl[0].hz; 772 773 if (!pdata) 774 pdata = (struct st_sensors_platform_data *)&default_accel_pdata; 775 776 err = st_sensors_init_sensor(indio_dev, adata->dev->platform_data); 777 if (err < 0) 778 goto st_accel_power_off; 779 780 err = st_accel_allocate_ring(indio_dev); 781 if (err < 0) 782 goto st_accel_power_off; 783 784 if (irq > 0) { 785 err = st_sensors_allocate_trigger(indio_dev, 786 ST_ACCEL_TRIGGER_OPS); 787 if (err < 0) 788 goto st_accel_probe_trigger_error; 789 } 790 791 err = iio_device_register(indio_dev); 792 if (err) 793 goto st_accel_device_register_error; 794 795 dev_info(&indio_dev->dev, "registered accelerometer %s\n", 796 indio_dev->name); 797 798 return 0; 799 800 st_accel_device_register_error: 801 if (irq > 0) 802 st_sensors_deallocate_trigger(indio_dev); 803 st_accel_probe_trigger_error: 804 st_accel_deallocate_ring(indio_dev); 805 st_accel_power_off: 806 st_sensors_power_disable(indio_dev); 807 808 return err; 809 } 810 EXPORT_SYMBOL(st_accel_common_probe); 811 812 void st_accel_common_remove(struct iio_dev *indio_dev) 813 { 814 struct st_sensor_data *adata = iio_priv(indio_dev); 815 816 st_sensors_power_disable(indio_dev); 817 818 iio_device_unregister(indio_dev); 819 if (adata->get_irq_data_ready(indio_dev) > 0) 820 st_sensors_deallocate_trigger(indio_dev); 821 822 st_accel_deallocate_ring(indio_dev); 823 } 824 EXPORT_SYMBOL(st_accel_common_remove); 825 826 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>"); 827 MODULE_DESCRIPTION("STMicroelectronics accelerometers driver"); 828 MODULE_LICENSE("GPL v2"); 829