1 /* 2 * STMicroelectronics st_lsm6dsx i2c controller driver 3 * 4 * i2c controller embedded in lsm6dx series can connect up to four 5 * slave devices using accelerometer sensor as trigger for i2c 6 * read/write operations. Current implementation relies on SLV0 channel 7 * for slave configuration and SLV{1,2,3} to read data and push them into 8 * the hw FIFO 9 * 10 * Copyright (C) 2018 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com> 11 * 12 * Permission to use, copy, modify, and/or distribute this software for any 13 * purpose with or without fee is hereby granted, provided that the above 14 * copyright notice and this permission notice appear in all copies. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 17 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 18 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 19 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 20 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 21 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 22 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 23 * 24 */ 25 #include <linux/module.h> 26 #include <linux/regmap.h> 27 #include <linux/iio/iio.h> 28 #include <linux/iio/sysfs.h> 29 #include <linux/bitfield.h> 30 31 #include "st_lsm6dsx.h" 32 33 #define ST_LSM6DSX_SLV_ADDR(n, base) ((base) + (n) * 3) 34 #define ST_LSM6DSX_SLV_SUB_ADDR(n, base) ((base) + 1 + (n) * 3) 35 #define ST_LSM6DSX_SLV_CONFIG(n, base) ((base) + 2 + (n) * 3) 36 37 #define ST_LS6DSX_READ_OP_MASK GENMASK(2, 0) 38 39 static const struct st_lsm6dsx_ext_dev_settings st_lsm6dsx_ext_dev_table[] = { 40 /* LIS2MDL */ 41 { 42 .i2c_addr = { 0x1e }, 43 .wai = { 44 .addr = 0x4f, 45 .val = 0x40, 46 }, 47 .id = ST_LSM6DSX_ID_MAGN, 48 .odr_table = { 49 .reg = { 50 .addr = 0x60, 51 .mask = GENMASK(3, 2), 52 }, 53 .odr_avl[0] = { 10000, 0x0 }, 54 .odr_avl[1] = { 20000, 0x1 }, 55 .odr_avl[2] = { 50000, 0x2 }, 56 .odr_avl[3] = { 100000, 0x3 }, 57 .odr_len = 4, 58 }, 59 .fs_table = { 60 .fs_avl[0] = { 61 .gain = 1500, 62 .val = 0x0, 63 }, /* 1500 uG/LSB */ 64 .fs_len = 1, 65 }, 66 .temp_comp = { 67 .addr = 0x60, 68 .mask = BIT(7), 69 }, 70 .pwr_table = { 71 .reg = { 72 .addr = 0x60, 73 .mask = GENMASK(1, 0), 74 }, 75 .off_val = 0x2, 76 .on_val = 0x0, 77 }, 78 .off_canc = { 79 .addr = 0x61, 80 .mask = BIT(1), 81 }, 82 .bdu = { 83 .addr = 0x62, 84 .mask = BIT(4), 85 }, 86 .out = { 87 .addr = 0x68, 88 .len = 6, 89 }, 90 }, 91 /* LIS3MDL */ 92 { 93 .i2c_addr = { 0x1e }, 94 .wai = { 95 .addr = 0x0f, 96 .val = 0x3d, 97 }, 98 .id = ST_LSM6DSX_ID_MAGN, 99 .odr_table = { 100 .reg = { 101 .addr = 0x20, 102 .mask = GENMASK(4, 2), 103 }, 104 .odr_avl[0] = { 1000, 0x0 }, 105 .odr_avl[1] = { 2000, 0x1 }, 106 .odr_avl[2] = { 3000, 0x2 }, 107 .odr_avl[3] = { 5000, 0x3 }, 108 .odr_avl[4] = { 10000, 0x4 }, 109 .odr_avl[5] = { 20000, 0x5 }, 110 .odr_avl[6] = { 40000, 0x6 }, 111 .odr_avl[7] = { 80000, 0x7 }, 112 .odr_len = 8, 113 }, 114 .fs_table = { 115 .reg = { 116 .addr = 0x21, 117 .mask = GENMASK(6, 5), 118 }, 119 .fs_avl[0] = { 120 .gain = 146, 121 .val = 0x00, 122 }, /* 4000 uG/LSB */ 123 .fs_avl[1] = { 124 .gain = 292, 125 .val = 0x01, 126 }, /* 8000 uG/LSB */ 127 .fs_avl[2] = { 128 .gain = 438, 129 .val = 0x02, 130 }, /* 12000 uG/LSB */ 131 .fs_avl[3] = { 132 .gain = 584, 133 .val = 0x03, 134 }, /* 16000 uG/LSB */ 135 .fs_len = 4, 136 }, 137 .pwr_table = { 138 .reg = { 139 .addr = 0x22, 140 .mask = GENMASK(1, 0), 141 }, 142 .off_val = 0x2, 143 .on_val = 0x0, 144 }, 145 .bdu = { 146 .addr = 0x24, 147 .mask = BIT(6), 148 }, 149 .out = { 150 .addr = 0x28, 151 .len = 6, 152 }, 153 }, 154 }; 155 156 static void st_lsm6dsx_shub_wait_complete(struct st_lsm6dsx_hw *hw) 157 { 158 struct st_lsm6dsx_sensor *sensor; 159 u32 odr; 160 161 sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_ACC]); 162 odr = (hw->enable_mask & BIT(ST_LSM6DSX_ID_ACC)) ? sensor->odr : 12500; 163 msleep((2000000U / odr) + 1); 164 } 165 166 /* 167 * st_lsm6dsx_shub_read_output - read i2c controller register 168 * 169 * Read st_lsm6dsx i2c controller register 170 */ 171 static int 172 st_lsm6dsx_shub_read_output(struct st_lsm6dsx_hw *hw, u8 *data, 173 int len) 174 { 175 const struct st_lsm6dsx_shub_settings *hub_settings; 176 int err; 177 178 mutex_lock(&hw->page_lock); 179 180 hub_settings = &hw->settings->shub_settings; 181 if (hub_settings->shub_out.sec_page) { 182 err = st_lsm6dsx_set_page(hw, true); 183 if (err < 0) 184 goto out; 185 } 186 187 err = regmap_bulk_read(hw->regmap, hub_settings->shub_out.addr, 188 data, len); 189 190 if (hub_settings->shub_out.sec_page) 191 st_lsm6dsx_set_page(hw, false); 192 out: 193 mutex_unlock(&hw->page_lock); 194 195 return err; 196 } 197 198 /* 199 * st_lsm6dsx_shub_write_reg - write i2c controller register 200 * 201 * Write st_lsm6dsx i2c controller register 202 */ 203 static int st_lsm6dsx_shub_write_reg(struct st_lsm6dsx_hw *hw, u8 addr, 204 u8 *data, int len) 205 { 206 int err; 207 208 mutex_lock(&hw->page_lock); 209 err = st_lsm6dsx_set_page(hw, true); 210 if (err < 0) 211 goto out; 212 213 err = regmap_bulk_write(hw->regmap, addr, data, len); 214 215 st_lsm6dsx_set_page(hw, false); 216 out: 217 mutex_unlock(&hw->page_lock); 218 219 return err; 220 } 221 222 static int 223 st_lsm6dsx_shub_write_reg_with_mask(struct st_lsm6dsx_hw *hw, u8 addr, 224 u8 mask, u8 val) 225 { 226 int err; 227 228 mutex_lock(&hw->page_lock); 229 err = st_lsm6dsx_set_page(hw, true); 230 if (err < 0) 231 goto out; 232 233 err = regmap_update_bits(hw->regmap, addr, mask, val); 234 235 st_lsm6dsx_set_page(hw, false); 236 out: 237 mutex_unlock(&hw->page_lock); 238 239 return err; 240 } 241 242 static int st_lsm6dsx_shub_master_enable(struct st_lsm6dsx_sensor *sensor, 243 bool enable) 244 { 245 const struct st_lsm6dsx_shub_settings *hub_settings; 246 struct st_lsm6dsx_hw *hw = sensor->hw; 247 unsigned int data; 248 int err; 249 250 /* enable acc sensor as trigger */ 251 err = st_lsm6dsx_sensor_set_enable(sensor, enable); 252 if (err < 0) 253 return err; 254 255 mutex_lock(&hw->page_lock); 256 257 hub_settings = &hw->settings->shub_settings; 258 if (hub_settings->master_en.sec_page) { 259 err = st_lsm6dsx_set_page(hw, true); 260 if (err < 0) 261 goto out; 262 } 263 264 data = ST_LSM6DSX_SHIFT_VAL(enable, hub_settings->master_en.mask); 265 err = regmap_update_bits(hw->regmap, hub_settings->master_en.addr, 266 hub_settings->master_en.mask, data); 267 268 if (hub_settings->master_en.sec_page) 269 st_lsm6dsx_set_page(hw, false); 270 out: 271 mutex_unlock(&hw->page_lock); 272 273 return err; 274 } 275 276 /* 277 * st_lsm6dsx_shub_read - read data from slave device register 278 * 279 * Read data from slave device register. SLV0 is used for 280 * one-shot read operation 281 */ 282 static int 283 st_lsm6dsx_shub_read(struct st_lsm6dsx_sensor *sensor, u8 addr, 284 u8 *data, int len) 285 { 286 const struct st_lsm6dsx_shub_settings *hub_settings; 287 u8 config[3], slv_addr, slv_config = 0; 288 struct st_lsm6dsx_hw *hw = sensor->hw; 289 const struct st_lsm6dsx_reg *aux_sens; 290 int err; 291 292 hub_settings = &hw->settings->shub_settings; 293 slv_addr = ST_LSM6DSX_SLV_ADDR(0, hub_settings->slv0_addr); 294 aux_sens = &hw->settings->shub_settings.aux_sens; 295 /* do not overwrite aux_sens */ 296 if (slv_addr + 2 == aux_sens->addr) 297 slv_config = ST_LSM6DSX_SHIFT_VAL(3, aux_sens->mask); 298 299 config[0] = (sensor->ext_info.addr << 1) | 1; 300 config[1] = addr; 301 config[2] = (len & ST_LS6DSX_READ_OP_MASK) | slv_config; 302 303 err = st_lsm6dsx_shub_write_reg(hw, slv_addr, config, 304 sizeof(config)); 305 if (err < 0) 306 return err; 307 308 err = st_lsm6dsx_shub_master_enable(sensor, true); 309 if (err < 0) 310 return err; 311 312 st_lsm6dsx_shub_wait_complete(hw); 313 314 err = st_lsm6dsx_shub_read_output(hw, data, 315 len & ST_LS6DSX_READ_OP_MASK); 316 317 st_lsm6dsx_shub_master_enable(sensor, false); 318 319 config[0] = hub_settings->pause; 320 config[1] = 0; 321 config[2] = slv_config; 322 return st_lsm6dsx_shub_write_reg(hw, slv_addr, config, 323 sizeof(config)); 324 } 325 326 /* 327 * st_lsm6dsx_shub_write - write data to slave device register 328 * 329 * Write data from slave device register. SLV0 is used for 330 * one-shot write operation 331 */ 332 static int 333 st_lsm6dsx_shub_write(struct st_lsm6dsx_sensor *sensor, u8 addr, 334 u8 *data, int len) 335 { 336 const struct st_lsm6dsx_shub_settings *hub_settings; 337 struct st_lsm6dsx_hw *hw = sensor->hw; 338 u8 config[2], slv_addr; 339 int err, i; 340 341 hub_settings = &hw->settings->shub_settings; 342 if (hub_settings->wr_once.addr) { 343 unsigned int data; 344 345 data = ST_LSM6DSX_SHIFT_VAL(1, hub_settings->wr_once.mask); 346 err = st_lsm6dsx_shub_write_reg_with_mask(hw, 347 hub_settings->wr_once.addr, 348 hub_settings->wr_once.mask, 349 data); 350 if (err < 0) 351 return err; 352 } 353 354 slv_addr = ST_LSM6DSX_SLV_ADDR(0, hub_settings->slv0_addr); 355 config[0] = sensor->ext_info.addr << 1; 356 for (i = 0 ; i < len; i++) { 357 config[1] = addr + i; 358 359 err = st_lsm6dsx_shub_write_reg(hw, slv_addr, config, 360 sizeof(config)); 361 if (err < 0) 362 return err; 363 364 err = st_lsm6dsx_shub_write_reg(hw, hub_settings->dw_slv0_addr, 365 &data[i], 1); 366 if (err < 0) 367 return err; 368 369 err = st_lsm6dsx_shub_master_enable(sensor, true); 370 if (err < 0) 371 return err; 372 373 st_lsm6dsx_shub_wait_complete(hw); 374 375 st_lsm6dsx_shub_master_enable(sensor, false); 376 } 377 378 config[0] = hub_settings->pause; 379 config[1] = 0; 380 return st_lsm6dsx_shub_write_reg(hw, slv_addr, config, sizeof(config)); 381 } 382 383 static int 384 st_lsm6dsx_shub_write_with_mask(struct st_lsm6dsx_sensor *sensor, 385 u8 addr, u8 mask, u8 val) 386 { 387 int err; 388 u8 data; 389 390 err = st_lsm6dsx_shub_read(sensor, addr, &data, sizeof(data)); 391 if (err < 0) 392 return err; 393 394 data = ((data & ~mask) | (val << __ffs(mask) & mask)); 395 396 return st_lsm6dsx_shub_write(sensor, addr, &data, sizeof(data)); 397 } 398 399 static int 400 st_lsm6dsx_shub_get_odr_val(struct st_lsm6dsx_sensor *sensor, 401 u32 odr, u16 *val) 402 { 403 const struct st_lsm6dsx_ext_dev_settings *settings; 404 int i; 405 406 settings = sensor->ext_info.settings; 407 for (i = 0; i < settings->odr_table.odr_len; i++) { 408 if (settings->odr_table.odr_avl[i].milli_hz == odr) 409 break; 410 } 411 412 if (i == settings->odr_table.odr_len) 413 return -EINVAL; 414 415 *val = settings->odr_table.odr_avl[i].val; 416 return 0; 417 } 418 419 static int 420 st_lsm6dsx_shub_set_odr(struct st_lsm6dsx_sensor *sensor, u32 odr) 421 { 422 const struct st_lsm6dsx_ext_dev_settings *settings; 423 u16 val; 424 int err; 425 426 err = st_lsm6dsx_shub_get_odr_val(sensor, odr, &val); 427 if (err < 0) 428 return err; 429 430 settings = sensor->ext_info.settings; 431 return st_lsm6dsx_shub_write_with_mask(sensor, 432 settings->odr_table.reg.addr, 433 settings->odr_table.reg.mask, 434 val); 435 } 436 437 /* use SLV{1,2,3} for FIFO read operations */ 438 static int 439 st_lsm6dsx_shub_config_channels(struct st_lsm6dsx_sensor *sensor, 440 bool enable) 441 { 442 const struct st_lsm6dsx_shub_settings *hub_settings; 443 const struct st_lsm6dsx_ext_dev_settings *settings; 444 u8 config[9] = {}, enable_mask, slv_addr; 445 struct st_lsm6dsx_hw *hw = sensor->hw; 446 struct st_lsm6dsx_sensor *cur_sensor; 447 int i, j = 0; 448 449 hub_settings = &hw->settings->shub_settings; 450 if (enable) 451 enable_mask = hw->enable_mask | BIT(sensor->id); 452 else 453 enable_mask = hw->enable_mask & ~BIT(sensor->id); 454 455 for (i = ST_LSM6DSX_ID_EXT0; i <= ST_LSM6DSX_ID_EXT2; i++) { 456 if (!hw->iio_devs[i]) 457 continue; 458 459 cur_sensor = iio_priv(hw->iio_devs[i]); 460 if (!(enable_mask & BIT(cur_sensor->id))) 461 continue; 462 463 settings = cur_sensor->ext_info.settings; 464 config[j] = (sensor->ext_info.addr << 1) | 1; 465 config[j + 1] = settings->out.addr; 466 config[j + 2] = (settings->out.len & ST_LS6DSX_READ_OP_MASK) | 467 hub_settings->batch_en; 468 j += 3; 469 } 470 471 slv_addr = ST_LSM6DSX_SLV_ADDR(1, hub_settings->slv0_addr); 472 return st_lsm6dsx_shub_write_reg(hw, slv_addr, config, 473 sizeof(config)); 474 } 475 476 int st_lsm6dsx_shub_set_enable(struct st_lsm6dsx_sensor *sensor, bool enable) 477 { 478 const struct st_lsm6dsx_ext_dev_settings *settings; 479 int err; 480 481 err = st_lsm6dsx_shub_config_channels(sensor, enable); 482 if (err < 0) 483 return err; 484 485 settings = sensor->ext_info.settings; 486 if (enable) { 487 err = st_lsm6dsx_shub_set_odr(sensor, 488 sensor->ext_info.slv_odr); 489 if (err < 0) 490 return err; 491 } else { 492 err = st_lsm6dsx_shub_write_with_mask(sensor, 493 settings->odr_table.reg.addr, 494 settings->odr_table.reg.mask, 0); 495 if (err < 0) 496 return err; 497 } 498 499 if (settings->pwr_table.reg.addr) { 500 u8 val; 501 502 val = enable ? settings->pwr_table.on_val 503 : settings->pwr_table.off_val; 504 err = st_lsm6dsx_shub_write_with_mask(sensor, 505 settings->pwr_table.reg.addr, 506 settings->pwr_table.reg.mask, val); 507 if (err < 0) 508 return err; 509 } 510 511 return st_lsm6dsx_shub_master_enable(sensor, enable); 512 } 513 514 static int 515 st_lsm6dsx_shub_read_oneshot(struct st_lsm6dsx_sensor *sensor, 516 struct iio_chan_spec const *ch, 517 int *val) 518 { 519 int err, delay, len; 520 u8 data[4]; 521 522 err = st_lsm6dsx_shub_set_enable(sensor, true); 523 if (err < 0) 524 return err; 525 526 delay = 1000000000 / sensor->ext_info.slv_odr; 527 usleep_range(delay, 2 * delay); 528 529 len = min_t(int, sizeof(data), ch->scan_type.realbits >> 3); 530 err = st_lsm6dsx_shub_read(sensor, ch->address, data, len); 531 if (err < 0) 532 return err; 533 534 err = st_lsm6dsx_shub_set_enable(sensor, false); 535 if (err < 0) 536 return err; 537 538 switch (len) { 539 case 2: 540 *val = (s16)le16_to_cpu(*((__le16 *)data)); 541 break; 542 default: 543 return -EINVAL; 544 } 545 546 return IIO_VAL_INT; 547 } 548 549 static int 550 st_lsm6dsx_shub_read_raw(struct iio_dev *iio_dev, 551 struct iio_chan_spec const *ch, 552 int *val, int *val2, long mask) 553 { 554 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev); 555 int ret; 556 557 switch (mask) { 558 case IIO_CHAN_INFO_RAW: 559 ret = iio_device_claim_direct_mode(iio_dev); 560 if (ret) 561 break; 562 563 ret = st_lsm6dsx_shub_read_oneshot(sensor, ch, val); 564 iio_device_release_direct_mode(iio_dev); 565 break; 566 case IIO_CHAN_INFO_SAMP_FREQ: 567 *val = sensor->ext_info.slv_odr / 1000; 568 *val2 = (sensor->ext_info.slv_odr % 1000) * 1000; 569 ret = IIO_VAL_INT_PLUS_MICRO; 570 break; 571 case IIO_CHAN_INFO_SCALE: 572 *val = 0; 573 *val2 = sensor->gain; 574 ret = IIO_VAL_INT_PLUS_MICRO; 575 break; 576 default: 577 ret = -EINVAL; 578 break; 579 } 580 581 return ret; 582 } 583 584 static int 585 st_lsm6dsx_shub_set_full_scale(struct st_lsm6dsx_sensor *sensor, 586 u32 gain) 587 { 588 const struct st_lsm6dsx_fs_table_entry *fs_table; 589 int i, err; 590 591 fs_table = &sensor->ext_info.settings->fs_table; 592 if (!fs_table->reg.addr) 593 return -ENOTSUPP; 594 595 for (i = 0; i < fs_table->fs_len; i++) { 596 if (fs_table->fs_avl[i].gain == gain) 597 break; 598 } 599 600 if (i == fs_table->fs_len) 601 return -EINVAL; 602 603 err = st_lsm6dsx_shub_write_with_mask(sensor, fs_table->reg.addr, 604 fs_table->reg.mask, 605 fs_table->fs_avl[i].val); 606 if (err < 0) 607 return err; 608 609 sensor->gain = gain; 610 611 return 0; 612 } 613 614 static int 615 st_lsm6dsx_shub_write_raw(struct iio_dev *iio_dev, 616 struct iio_chan_spec const *chan, 617 int val, int val2, long mask) 618 { 619 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev); 620 int err; 621 622 err = iio_device_claim_direct_mode(iio_dev); 623 if (err) 624 return err; 625 626 switch (mask) { 627 case IIO_CHAN_INFO_SAMP_FREQ: { 628 u16 data; 629 630 val = val * 1000 + val2 / 1000; 631 err = st_lsm6dsx_shub_get_odr_val(sensor, val, &data); 632 if (!err) { 633 struct st_lsm6dsx_hw *hw = sensor->hw; 634 struct st_lsm6dsx_sensor *ref_sensor; 635 u8 odr_val; 636 int odr; 637 638 ref_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_ACC]); 639 odr = st_lsm6dsx_check_odr(ref_sensor, val, &odr_val); 640 if (odr < 0) { 641 err = odr; 642 goto release; 643 } 644 645 sensor->ext_info.slv_odr = val; 646 sensor->odr = odr; 647 } 648 break; 649 } 650 case IIO_CHAN_INFO_SCALE: 651 err = st_lsm6dsx_shub_set_full_scale(sensor, val2); 652 break; 653 default: 654 err = -EINVAL; 655 break; 656 } 657 658 release: 659 iio_device_release_direct_mode(iio_dev); 660 661 return err; 662 } 663 664 static ssize_t 665 st_lsm6dsx_shub_sampling_freq_avail(struct device *dev, 666 struct device_attribute *attr, 667 char *buf) 668 { 669 struct st_lsm6dsx_sensor *sensor = iio_priv(dev_get_drvdata(dev)); 670 const struct st_lsm6dsx_ext_dev_settings *settings; 671 int i, len = 0; 672 673 settings = sensor->ext_info.settings; 674 for (i = 0; i < settings->odr_table.odr_len; i++) { 675 u32 val = settings->odr_table.odr_avl[i].milli_hz; 676 677 len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%03d ", 678 val / 1000, val % 1000); 679 } 680 buf[len - 1] = '\n'; 681 682 return len; 683 } 684 685 static ssize_t st_lsm6dsx_shub_scale_avail(struct device *dev, 686 struct device_attribute *attr, 687 char *buf) 688 { 689 struct st_lsm6dsx_sensor *sensor = iio_priv(dev_get_drvdata(dev)); 690 const struct st_lsm6dsx_ext_dev_settings *settings; 691 int i, len = 0; 692 693 settings = sensor->ext_info.settings; 694 for (i = 0; i < settings->fs_table.fs_len; i++) 695 len += scnprintf(buf + len, PAGE_SIZE - len, "0.%06u ", 696 settings->fs_table.fs_avl[i].gain); 697 buf[len - 1] = '\n'; 698 699 return len; 700 } 701 702 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(st_lsm6dsx_shub_sampling_freq_avail); 703 static IIO_DEVICE_ATTR(in_scale_available, 0444, 704 st_lsm6dsx_shub_scale_avail, NULL, 0); 705 static struct attribute *st_lsm6dsx_ext_attributes[] = { 706 &iio_dev_attr_sampling_frequency_available.dev_attr.attr, 707 &iio_dev_attr_in_scale_available.dev_attr.attr, 708 NULL, 709 }; 710 711 static const struct attribute_group st_lsm6dsx_ext_attribute_group = { 712 .attrs = st_lsm6dsx_ext_attributes, 713 }; 714 715 static const struct iio_info st_lsm6dsx_ext_info = { 716 .attrs = &st_lsm6dsx_ext_attribute_group, 717 .read_raw = st_lsm6dsx_shub_read_raw, 718 .write_raw = st_lsm6dsx_shub_write_raw, 719 .hwfifo_set_watermark = st_lsm6dsx_set_watermark, 720 }; 721 722 static struct iio_dev * 723 st_lsm6dsx_shub_alloc_iiodev(struct st_lsm6dsx_hw *hw, 724 enum st_lsm6dsx_sensor_id id, 725 const struct st_lsm6dsx_ext_dev_settings *info, 726 u8 i2c_addr, const char *name) 727 { 728 enum st_lsm6dsx_sensor_id ref_id = ST_LSM6DSX_ID_ACC; 729 struct iio_chan_spec *ext_channels; 730 struct st_lsm6dsx_sensor *sensor; 731 struct iio_dev *iio_dev; 732 733 iio_dev = devm_iio_device_alloc(hw->dev, sizeof(*sensor)); 734 if (!iio_dev) 735 return NULL; 736 737 iio_dev->modes = INDIO_DIRECT_MODE; 738 iio_dev->info = &st_lsm6dsx_ext_info; 739 740 sensor = iio_priv(iio_dev); 741 sensor->id = id; 742 sensor->hw = hw; 743 sensor->odr = hw->settings->odr_table[ref_id].odr_avl[0].milli_hz; 744 sensor->ext_info.slv_odr = info->odr_table.odr_avl[0].milli_hz; 745 sensor->gain = info->fs_table.fs_avl[0].gain; 746 sensor->ext_info.settings = info; 747 sensor->ext_info.addr = i2c_addr; 748 sensor->watermark = 1; 749 750 switch (info->id) { 751 case ST_LSM6DSX_ID_MAGN: { 752 const struct iio_chan_spec magn_channels[] = { 753 ST_LSM6DSX_CHANNEL(IIO_MAGN, info->out.addr, 754 IIO_MOD_X, 0), 755 ST_LSM6DSX_CHANNEL(IIO_MAGN, info->out.addr + 2, 756 IIO_MOD_Y, 1), 757 ST_LSM6DSX_CHANNEL(IIO_MAGN, info->out.addr + 4, 758 IIO_MOD_Z, 2), 759 IIO_CHAN_SOFT_TIMESTAMP(3), 760 }; 761 762 ext_channels = devm_kzalloc(hw->dev, sizeof(magn_channels), 763 GFP_KERNEL); 764 if (!ext_channels) 765 return NULL; 766 767 memcpy(ext_channels, magn_channels, sizeof(magn_channels)); 768 iio_dev->available_scan_masks = st_lsm6dsx_available_scan_masks; 769 iio_dev->channels = ext_channels; 770 iio_dev->num_channels = ARRAY_SIZE(magn_channels); 771 772 scnprintf(sensor->name, sizeof(sensor->name), "%s_magn", 773 name); 774 break; 775 } 776 default: 777 return NULL; 778 } 779 iio_dev->name = sensor->name; 780 781 return iio_dev; 782 } 783 784 static int st_lsm6dsx_shub_init_device(struct st_lsm6dsx_sensor *sensor) 785 { 786 const struct st_lsm6dsx_ext_dev_settings *settings; 787 int err; 788 789 settings = sensor->ext_info.settings; 790 if (settings->bdu.addr) { 791 err = st_lsm6dsx_shub_write_with_mask(sensor, 792 settings->bdu.addr, 793 settings->bdu.mask, 1); 794 if (err < 0) 795 return err; 796 } 797 798 if (settings->temp_comp.addr) { 799 err = st_lsm6dsx_shub_write_with_mask(sensor, 800 settings->temp_comp.addr, 801 settings->temp_comp.mask, 1); 802 if (err < 0) 803 return err; 804 } 805 806 if (settings->off_canc.addr) { 807 err = st_lsm6dsx_shub_write_with_mask(sensor, 808 settings->off_canc.addr, 809 settings->off_canc.mask, 1); 810 if (err < 0) 811 return err; 812 } 813 814 return 0; 815 } 816 817 static int 818 st_lsm6dsx_shub_check_wai(struct st_lsm6dsx_hw *hw, u8 *i2c_addr, 819 const struct st_lsm6dsx_ext_dev_settings *settings) 820 { 821 const struct st_lsm6dsx_shub_settings *hub_settings; 822 u8 config[3], data, slv_addr, slv_config = 0; 823 const struct st_lsm6dsx_reg *aux_sens; 824 struct st_lsm6dsx_sensor *sensor; 825 bool found = false; 826 int i, err; 827 828 sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_ACC]); 829 hub_settings = &hw->settings->shub_settings; 830 aux_sens = &hw->settings->shub_settings.aux_sens; 831 slv_addr = ST_LSM6DSX_SLV_ADDR(0, hub_settings->slv0_addr); 832 /* do not overwrite aux_sens */ 833 if (slv_addr + 2 == aux_sens->addr) 834 slv_config = ST_LSM6DSX_SHIFT_VAL(3, aux_sens->mask); 835 836 for (i = 0; i < ARRAY_SIZE(settings->i2c_addr); i++) { 837 if (!settings->i2c_addr[i]) 838 continue; 839 840 /* read wai slave register */ 841 config[0] = (settings->i2c_addr[i] << 1) | 0x1; 842 config[1] = settings->wai.addr; 843 config[2] = 0x1 | slv_config; 844 845 err = st_lsm6dsx_shub_write_reg(hw, slv_addr, config, 846 sizeof(config)); 847 if (err < 0) 848 return err; 849 850 err = st_lsm6dsx_shub_master_enable(sensor, true); 851 if (err < 0) 852 return err; 853 854 st_lsm6dsx_shub_wait_complete(hw); 855 856 err = st_lsm6dsx_shub_read_output(hw, &data, sizeof(data)); 857 858 st_lsm6dsx_shub_master_enable(sensor, false); 859 860 if (err < 0) 861 return err; 862 863 if (data != settings->wai.val) 864 continue; 865 866 *i2c_addr = settings->i2c_addr[i]; 867 found = true; 868 break; 869 } 870 871 /* reset SLV0 channel */ 872 config[0] = hub_settings->pause; 873 config[1] = 0; 874 config[2] = slv_config; 875 err = st_lsm6dsx_shub_write_reg(hw, slv_addr, config, 876 sizeof(config)); 877 if (err < 0) 878 return err; 879 880 return found ? 0 : -ENODEV; 881 } 882 883 int st_lsm6dsx_shub_probe(struct st_lsm6dsx_hw *hw, const char *name) 884 { 885 enum st_lsm6dsx_sensor_id id = ST_LSM6DSX_ID_EXT0; 886 struct st_lsm6dsx_sensor *sensor; 887 int err, i, num_ext_dev = 0; 888 u8 i2c_addr = 0; 889 890 for (i = 0; i < ARRAY_SIZE(st_lsm6dsx_ext_dev_table); i++) { 891 err = st_lsm6dsx_shub_check_wai(hw, &i2c_addr, 892 &st_lsm6dsx_ext_dev_table[i]); 893 if (err == -ENODEV) 894 continue; 895 else if (err < 0) 896 return err; 897 898 hw->iio_devs[id] = st_lsm6dsx_shub_alloc_iiodev(hw, id, 899 &st_lsm6dsx_ext_dev_table[i], 900 i2c_addr, name); 901 if (!hw->iio_devs[id]) 902 return -ENOMEM; 903 904 sensor = iio_priv(hw->iio_devs[id]); 905 err = st_lsm6dsx_shub_init_device(sensor); 906 if (err < 0) 907 return err; 908 909 if (++num_ext_dev >= hw->settings->shub_settings.num_ext_dev) 910 break; 911 id++; 912 } 913 914 return 0; 915 } 916