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_MAX_SLV_NUM 3 34 #define ST_LSM6DSX_SLV_ADDR(n, base) ((base) + (n) * 3) 35 #define ST_LSM6DSX_SLV_SUB_ADDR(n, base) ((base) + 1 + (n) * 3) 36 #define ST_LSM6DSX_SLV_CONFIG(n, base) ((base) + 2 + (n) * 3) 37 38 #define ST_LS6DSX_READ_OP_MASK GENMASK(2, 0) 39 40 static const struct st_lsm6dsx_ext_dev_settings st_lsm6dsx_ext_dev_table[] = { 41 /* LIS2MDL */ 42 { 43 .i2c_addr = { 0x1e }, 44 .wai = { 45 .addr = 0x4f, 46 .val = 0x40, 47 }, 48 .id = ST_LSM6DSX_ID_MAGN, 49 .odr_table = { 50 .reg = { 51 .addr = 0x60, 52 .mask = GENMASK(3, 2), 53 }, 54 .odr_avl[0] = { 10, 0x0 }, 55 .odr_avl[1] = { 20, 0x1 }, 56 .odr_avl[2] = { 50, 0x2 }, 57 .odr_avl[3] = { 100, 0x3 }, 58 }, 59 .fs_table = { 60 .fs_avl[0] = { 61 .gain = 1500, 62 .val = 0x0, 63 }, /* 1500 uG/LSB */ 64 }, 65 .temp_comp = { 66 .addr = 0x60, 67 .mask = BIT(7), 68 }, 69 .pwr_table = { 70 .reg = { 71 .addr = 0x60, 72 .mask = GENMASK(1, 0), 73 }, 74 .off_val = 0x2, 75 .on_val = 0x0, 76 }, 77 .off_canc = { 78 .addr = 0x61, 79 .mask = BIT(1), 80 }, 81 .bdu = { 82 .addr = 0x62, 83 .mask = BIT(4), 84 }, 85 .out = { 86 .addr = 0x68, 87 .len = 6, 88 }, 89 }, 90 }; 91 92 static void st_lsm6dsx_shub_wait_complete(struct st_lsm6dsx_hw *hw) 93 { 94 struct st_lsm6dsx_sensor *sensor; 95 96 sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_ACC]); 97 msleep((2000U / sensor->odr) + 1); 98 } 99 100 /** 101 * st_lsm6dsx_shub_read_reg - read i2c controller register 102 * 103 * Read st_lsm6dsx i2c controller register 104 */ 105 static int st_lsm6dsx_shub_read_reg(struct st_lsm6dsx_hw *hw, u8 addr, 106 u8 *data, int len) 107 { 108 const struct st_lsm6dsx_shub_settings *hub_settings; 109 int err; 110 111 mutex_lock(&hw->page_lock); 112 113 hub_settings = &hw->settings->shub_settings; 114 err = st_lsm6dsx_set_page(hw, true); 115 if (err < 0) 116 goto out; 117 118 err = regmap_bulk_read(hw->regmap, addr, data, len); 119 120 st_lsm6dsx_set_page(hw, false); 121 out: 122 mutex_unlock(&hw->page_lock); 123 124 return err; 125 } 126 127 /** 128 * st_lsm6dsx_shub_write_reg - write i2c controller register 129 * 130 * Write st_lsm6dsx i2c controller register 131 */ 132 static int st_lsm6dsx_shub_write_reg(struct st_lsm6dsx_hw *hw, u8 addr, 133 u8 *data, int len) 134 { 135 int err; 136 137 mutex_lock(&hw->page_lock); 138 err = st_lsm6dsx_set_page(hw, true); 139 if (err < 0) 140 goto out; 141 142 err = regmap_bulk_write(hw->regmap, addr, data, len); 143 144 st_lsm6dsx_set_page(hw, false); 145 out: 146 mutex_unlock(&hw->page_lock); 147 148 return err; 149 } 150 151 static int 152 st_lsm6dsx_shub_write_reg_with_mask(struct st_lsm6dsx_hw *hw, u8 addr, 153 u8 mask, u8 val) 154 { 155 int err; 156 157 mutex_lock(&hw->page_lock); 158 err = st_lsm6dsx_set_page(hw, true); 159 if (err < 0) 160 goto out; 161 162 err = regmap_update_bits(hw->regmap, addr, mask, val); 163 164 st_lsm6dsx_set_page(hw, false); 165 out: 166 mutex_unlock(&hw->page_lock); 167 168 return err; 169 } 170 171 static int st_lsm6dsx_shub_master_enable(struct st_lsm6dsx_sensor *sensor, 172 bool enable) 173 { 174 const struct st_lsm6dsx_shub_settings *hub_settings; 175 struct st_lsm6dsx_hw *hw = sensor->hw; 176 unsigned int data; 177 int err; 178 179 /* enable acc sensor as trigger */ 180 err = st_lsm6dsx_sensor_set_enable(sensor, enable); 181 if (err < 0) 182 return err; 183 184 mutex_lock(&hw->page_lock); 185 186 hub_settings = &hw->settings->shub_settings; 187 err = st_lsm6dsx_set_page(hw, true); 188 if (err < 0) 189 goto out; 190 191 data = ST_LSM6DSX_SHIFT_VAL(enable, hub_settings->master_en.mask); 192 err = regmap_update_bits(hw->regmap, hub_settings->master_en.addr, 193 hub_settings->master_en.mask, data); 194 195 st_lsm6dsx_set_page(hw, false); 196 out: 197 mutex_unlock(&hw->page_lock); 198 199 return err; 200 } 201 202 /** 203 * st_lsm6dsx_shub_read - read data from slave device register 204 * 205 * Read data from slave device register. SLV0 is used for 206 * one-shot read operation 207 */ 208 static int 209 st_lsm6dsx_shub_read(struct st_lsm6dsx_sensor *sensor, u8 addr, 210 u8 *data, int len) 211 { 212 const struct st_lsm6dsx_shub_settings *hub_settings; 213 struct st_lsm6dsx_hw *hw = sensor->hw; 214 u8 config[3], slv_addr; 215 int err; 216 217 hub_settings = &hw->settings->shub_settings; 218 slv_addr = ST_LSM6DSX_SLV_ADDR(0, hub_settings->slv0_addr); 219 220 config[0] = (sensor->ext_info.addr << 1) | 1; 221 config[1] = addr; 222 config[2] = len & ST_LS6DSX_READ_OP_MASK; 223 224 err = st_lsm6dsx_shub_write_reg(hw, slv_addr, config, 225 sizeof(config)); 226 if (err < 0) 227 return err; 228 229 err = st_lsm6dsx_shub_master_enable(sensor, true); 230 if (err < 0) 231 return err; 232 233 st_lsm6dsx_shub_wait_complete(hw); 234 235 err = st_lsm6dsx_shub_read_reg(hw, hub_settings->shub_out, data, 236 len & ST_LS6DSX_READ_OP_MASK); 237 238 st_lsm6dsx_shub_master_enable(sensor, false); 239 240 memset(config, 0, sizeof(config)); 241 return st_lsm6dsx_shub_write_reg(hw, slv_addr, config, 242 sizeof(config)); 243 } 244 245 /** 246 * st_lsm6dsx_shub_write - write data to slave device register 247 * 248 * Write data from slave device register. SLV0 is used for 249 * one-shot write operation 250 */ 251 static int 252 st_lsm6dsx_shub_write(struct st_lsm6dsx_sensor *sensor, u8 addr, 253 u8 *data, int len) 254 { 255 const struct st_lsm6dsx_shub_settings *hub_settings; 256 struct st_lsm6dsx_hw *hw = sensor->hw; 257 u8 config[2], slv_addr; 258 int err, i; 259 260 hub_settings = &hw->settings->shub_settings; 261 if (hub_settings->wr_once.addr) { 262 unsigned int data; 263 264 data = ST_LSM6DSX_SHIFT_VAL(1, hub_settings->wr_once.mask); 265 err = st_lsm6dsx_shub_write_reg_with_mask(hw, 266 hub_settings->wr_once.addr, 267 hub_settings->wr_once.mask, 268 data); 269 if (err < 0) 270 return err; 271 } 272 273 slv_addr = ST_LSM6DSX_SLV_ADDR(0, hub_settings->slv0_addr); 274 config[0] = sensor->ext_info.addr << 1; 275 for (i = 0 ; i < len; i++) { 276 config[1] = addr + i; 277 278 err = st_lsm6dsx_shub_write_reg(hw, slv_addr, config, 279 sizeof(config)); 280 if (err < 0) 281 return err; 282 283 err = st_lsm6dsx_shub_write_reg(hw, hub_settings->dw_slv0_addr, 284 &data[i], 1); 285 if (err < 0) 286 return err; 287 288 err = st_lsm6dsx_shub_master_enable(sensor, true); 289 if (err < 0) 290 return err; 291 292 st_lsm6dsx_shub_wait_complete(hw); 293 294 st_lsm6dsx_shub_master_enable(sensor, false); 295 } 296 297 memset(config, 0, sizeof(config)); 298 return st_lsm6dsx_shub_write_reg(hw, slv_addr, config, sizeof(config)); 299 } 300 301 static int 302 st_lsm6dsx_shub_write_with_mask(struct st_lsm6dsx_sensor *sensor, 303 u8 addr, u8 mask, u8 val) 304 { 305 int err; 306 u8 data; 307 308 err = st_lsm6dsx_shub_read(sensor, addr, &data, sizeof(data)); 309 if (err < 0) 310 return err; 311 312 data = ((data & ~mask) | (val << __ffs(mask) & mask)); 313 314 return st_lsm6dsx_shub_write(sensor, addr, &data, sizeof(data)); 315 } 316 317 static int 318 st_lsm6dsx_shub_get_odr_val(struct st_lsm6dsx_sensor *sensor, 319 u16 odr, u16 *val) 320 { 321 const struct st_lsm6dsx_ext_dev_settings *settings; 322 int i; 323 324 settings = sensor->ext_info.settings; 325 for (i = 0; i < ST_LSM6DSX_ODR_LIST_SIZE; i++) 326 if (settings->odr_table.odr_avl[i].hz == odr) 327 break; 328 329 if (i == ST_LSM6DSX_ODR_LIST_SIZE) 330 return -EINVAL; 331 332 *val = settings->odr_table.odr_avl[i].val; 333 return 0; 334 } 335 336 static int 337 st_lsm6dsx_shub_set_odr(struct st_lsm6dsx_sensor *sensor, u16 odr) 338 { 339 const struct st_lsm6dsx_ext_dev_settings *settings; 340 u16 val; 341 int err; 342 343 err = st_lsm6dsx_shub_get_odr_val(sensor, odr, &val); 344 if (err < 0) 345 return err; 346 347 settings = sensor->ext_info.settings; 348 return st_lsm6dsx_shub_write_with_mask(sensor, 349 settings->odr_table.reg.addr, 350 settings->odr_table.reg.mask, 351 val); 352 } 353 354 /* use SLV{1,2,3} for FIFO read operations */ 355 static int 356 st_lsm6dsx_shub_config_channels(struct st_lsm6dsx_sensor *sensor, 357 bool enable) 358 { 359 const struct st_lsm6dsx_shub_settings *hub_settings; 360 const struct st_lsm6dsx_ext_dev_settings *settings; 361 u8 config[9] = {}, enable_mask, slv_addr; 362 struct st_lsm6dsx_hw *hw = sensor->hw; 363 struct st_lsm6dsx_sensor *cur_sensor; 364 int i, j = 0; 365 366 hub_settings = &hw->settings->shub_settings; 367 if (enable) 368 enable_mask = hw->enable_mask | BIT(sensor->id); 369 else 370 enable_mask = hw->enable_mask & ~BIT(sensor->id); 371 372 for (i = ST_LSM6DSX_ID_EXT0; i <= ST_LSM6DSX_ID_EXT2; i++) { 373 if (!hw->iio_devs[i]) 374 continue; 375 376 cur_sensor = iio_priv(hw->iio_devs[i]); 377 if (!(enable_mask & BIT(cur_sensor->id))) 378 continue; 379 380 settings = cur_sensor->ext_info.settings; 381 config[j] = (sensor->ext_info.addr << 1) | 1; 382 config[j + 1] = settings->out.addr; 383 config[j + 2] = (settings->out.len & ST_LS6DSX_READ_OP_MASK) | 384 hub_settings->batch_en; 385 j += 3; 386 } 387 388 slv_addr = ST_LSM6DSX_SLV_ADDR(1, hub_settings->slv0_addr); 389 return st_lsm6dsx_shub_write_reg(hw, slv_addr, config, 390 sizeof(config)); 391 } 392 393 int st_lsm6dsx_shub_set_enable(struct st_lsm6dsx_sensor *sensor, bool enable) 394 { 395 const struct st_lsm6dsx_ext_dev_settings *settings; 396 int err; 397 398 err = st_lsm6dsx_shub_config_channels(sensor, enable); 399 if (err < 0) 400 return err; 401 402 settings = sensor->ext_info.settings; 403 if (enable) { 404 err = st_lsm6dsx_shub_set_odr(sensor, sensor->odr); 405 if (err < 0) 406 return err; 407 } else { 408 err = st_lsm6dsx_shub_write_with_mask(sensor, 409 settings->odr_table.reg.addr, 410 settings->odr_table.reg.mask, 0); 411 if (err < 0) 412 return err; 413 } 414 415 if (settings->pwr_table.reg.addr) { 416 u8 val; 417 418 val = enable ? settings->pwr_table.on_val 419 : settings->pwr_table.off_val; 420 err = st_lsm6dsx_shub_write_with_mask(sensor, 421 settings->pwr_table.reg.addr, 422 settings->pwr_table.reg.mask, val); 423 if (err < 0) 424 return err; 425 } 426 427 return st_lsm6dsx_shub_master_enable(sensor, enable); 428 } 429 430 static int 431 st_lsm6dsx_shub_read_oneshot(struct st_lsm6dsx_sensor *sensor, 432 struct iio_chan_spec const *ch, 433 int *val) 434 { 435 int err, delay, len; 436 u8 data[4]; 437 438 err = st_lsm6dsx_shub_set_enable(sensor, true); 439 if (err < 0) 440 return err; 441 442 delay = 1000000 / sensor->odr; 443 usleep_range(delay, 2 * delay); 444 445 len = min_t(int, sizeof(data), ch->scan_type.realbits >> 3); 446 err = st_lsm6dsx_shub_read(sensor, ch->address, data, len); 447 448 st_lsm6dsx_shub_set_enable(sensor, false); 449 450 if (err < 0) 451 return err; 452 453 switch (len) { 454 case 2: 455 *val = (s16)le16_to_cpu(*((__le16 *)data)); 456 break; 457 default: 458 return -EINVAL; 459 } 460 461 return IIO_VAL_INT; 462 } 463 464 static int 465 st_lsm6dsx_shub_read_raw(struct iio_dev *iio_dev, 466 struct iio_chan_spec const *ch, 467 int *val, int *val2, long mask) 468 { 469 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev); 470 int ret; 471 472 switch (mask) { 473 case IIO_CHAN_INFO_RAW: 474 ret = iio_device_claim_direct_mode(iio_dev); 475 if (ret) 476 break; 477 478 ret = st_lsm6dsx_shub_read_oneshot(sensor, ch, val); 479 iio_device_release_direct_mode(iio_dev); 480 break; 481 case IIO_CHAN_INFO_SAMP_FREQ: 482 *val = sensor->odr; 483 ret = IIO_VAL_INT; 484 break; 485 case IIO_CHAN_INFO_SCALE: 486 *val = 0; 487 *val2 = sensor->gain; 488 ret = IIO_VAL_INT_PLUS_MICRO; 489 break; 490 default: 491 ret = -EINVAL; 492 break; 493 } 494 495 return ret; 496 } 497 498 static int 499 st_lsm6dsx_shub_write_raw(struct iio_dev *iio_dev, 500 struct iio_chan_spec const *chan, 501 int val, int val2, long mask) 502 { 503 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev); 504 int err; 505 506 err = iio_device_claim_direct_mode(iio_dev); 507 if (err) 508 return err; 509 510 switch (mask) { 511 case IIO_CHAN_INFO_SAMP_FREQ: { 512 u16 data; 513 514 err = st_lsm6dsx_shub_get_odr_val(sensor, val, &data); 515 if (!err) 516 sensor->odr = val; 517 break; 518 } 519 default: 520 err = -EINVAL; 521 break; 522 } 523 524 iio_device_release_direct_mode(iio_dev); 525 526 return err; 527 } 528 529 static ssize_t 530 st_lsm6dsx_shub_sampling_freq_avail(struct device *dev, 531 struct device_attribute *attr, 532 char *buf) 533 { 534 struct st_lsm6dsx_sensor *sensor = iio_priv(dev_get_drvdata(dev)); 535 const struct st_lsm6dsx_ext_dev_settings *settings; 536 int i, len = 0; 537 538 settings = sensor->ext_info.settings; 539 for (i = 0; i < ST_LSM6DSX_ODR_LIST_SIZE; i++) { 540 u16 val = settings->odr_table.odr_avl[i].hz; 541 542 if (val > 0) 543 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ", 544 val); 545 } 546 buf[len - 1] = '\n'; 547 548 return len; 549 } 550 551 static ssize_t st_lsm6dsx_shub_scale_avail(struct device *dev, 552 struct device_attribute *attr, 553 char *buf) 554 { 555 struct st_lsm6dsx_sensor *sensor = iio_priv(dev_get_drvdata(dev)); 556 const struct st_lsm6dsx_ext_dev_settings *settings; 557 int i, len = 0; 558 559 settings = sensor->ext_info.settings; 560 for (i = 0; i < ST_LSM6DSX_FS_LIST_SIZE; i++) { 561 u16 val = settings->fs_table.fs_avl[i].gain; 562 563 if (val > 0) 564 len += scnprintf(buf + len, PAGE_SIZE - len, "0.%06u ", 565 val); 566 } 567 buf[len - 1] = '\n'; 568 569 return len; 570 } 571 572 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(st_lsm6dsx_shub_sampling_freq_avail); 573 static IIO_DEVICE_ATTR(in_scale_available, 0444, 574 st_lsm6dsx_shub_scale_avail, NULL, 0); 575 static struct attribute *st_lsm6dsx_ext_attributes[] = { 576 &iio_dev_attr_sampling_frequency_available.dev_attr.attr, 577 &iio_dev_attr_in_scale_available.dev_attr.attr, 578 NULL, 579 }; 580 581 static const struct attribute_group st_lsm6dsx_ext_attribute_group = { 582 .attrs = st_lsm6dsx_ext_attributes, 583 }; 584 585 static const struct iio_info st_lsm6dsx_ext_info = { 586 .attrs = &st_lsm6dsx_ext_attribute_group, 587 .read_raw = st_lsm6dsx_shub_read_raw, 588 .write_raw = st_lsm6dsx_shub_write_raw, 589 .hwfifo_set_watermark = st_lsm6dsx_set_watermark, 590 }; 591 592 static struct iio_dev * 593 st_lsm6dsx_shub_alloc_iiodev(struct st_lsm6dsx_hw *hw, 594 enum st_lsm6dsx_sensor_id id, 595 const struct st_lsm6dsx_ext_dev_settings *info, 596 u8 i2c_addr, const char *name) 597 { 598 struct iio_chan_spec *ext_channels; 599 struct st_lsm6dsx_sensor *sensor; 600 struct iio_dev *iio_dev; 601 602 iio_dev = devm_iio_device_alloc(hw->dev, sizeof(*sensor)); 603 if (!iio_dev) 604 return NULL; 605 606 iio_dev->modes = INDIO_DIRECT_MODE; 607 iio_dev->dev.parent = hw->dev; 608 iio_dev->info = &st_lsm6dsx_ext_info; 609 610 sensor = iio_priv(iio_dev); 611 sensor->id = id; 612 sensor->hw = hw; 613 sensor->odr = info->odr_table.odr_avl[0].hz; 614 sensor->gain = info->fs_table.fs_avl[0].gain; 615 sensor->ext_info.settings = info; 616 sensor->ext_info.addr = i2c_addr; 617 sensor->watermark = 1; 618 619 switch (info->id) { 620 case ST_LSM6DSX_ID_MAGN: { 621 const struct iio_chan_spec magn_channels[] = { 622 ST_LSM6DSX_CHANNEL(IIO_MAGN, info->out.addr, 623 IIO_MOD_X, 0), 624 ST_LSM6DSX_CHANNEL(IIO_MAGN, info->out.addr + 2, 625 IIO_MOD_Y, 1), 626 ST_LSM6DSX_CHANNEL(IIO_MAGN, info->out.addr + 4, 627 IIO_MOD_Z, 2), 628 IIO_CHAN_SOFT_TIMESTAMP(3), 629 }; 630 631 ext_channels = devm_kzalloc(hw->dev, sizeof(magn_channels), 632 GFP_KERNEL); 633 if (!ext_channels) 634 return NULL; 635 636 memcpy(ext_channels, magn_channels, sizeof(magn_channels)); 637 iio_dev->available_scan_masks = st_lsm6dsx_available_scan_masks; 638 iio_dev->channels = ext_channels; 639 iio_dev->num_channels = ARRAY_SIZE(magn_channels); 640 641 scnprintf(sensor->name, sizeof(sensor->name), "%s_magn", 642 name); 643 break; 644 } 645 default: 646 return NULL; 647 } 648 iio_dev->name = sensor->name; 649 650 return iio_dev; 651 } 652 653 static int st_lsm6dsx_shub_init_device(struct st_lsm6dsx_sensor *sensor) 654 { 655 const struct st_lsm6dsx_ext_dev_settings *settings; 656 int err; 657 658 settings = sensor->ext_info.settings; 659 if (settings->bdu.addr) { 660 err = st_lsm6dsx_shub_write_with_mask(sensor, 661 settings->bdu.addr, 662 settings->bdu.mask, 1); 663 if (err < 0) 664 return err; 665 } 666 667 if (settings->temp_comp.addr) { 668 err = st_lsm6dsx_shub_write_with_mask(sensor, 669 settings->temp_comp.addr, 670 settings->temp_comp.mask, 1); 671 if (err < 0) 672 return err; 673 } 674 675 if (settings->off_canc.addr) { 676 err = st_lsm6dsx_shub_write_with_mask(sensor, 677 settings->off_canc.addr, 678 settings->off_canc.mask, 1); 679 if (err < 0) 680 return err; 681 } 682 683 return 0; 684 } 685 686 static int 687 st_lsm6dsx_shub_check_wai(struct st_lsm6dsx_hw *hw, u8 *i2c_addr, 688 const struct st_lsm6dsx_ext_dev_settings *settings) 689 { 690 const struct st_lsm6dsx_shub_settings *hub_settings; 691 struct st_lsm6dsx_sensor *sensor; 692 u8 config[3], data, slv_addr; 693 bool found = false; 694 int i, err; 695 696 hub_settings = &hw->settings->shub_settings; 697 slv_addr = ST_LSM6DSX_SLV_ADDR(0, hub_settings->slv0_addr); 698 sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_ACC]); 699 700 for (i = 0; i < ARRAY_SIZE(settings->i2c_addr); i++) { 701 if (!settings->i2c_addr[i]) 702 continue; 703 704 /* read wai slave register */ 705 config[0] = (settings->i2c_addr[i] << 1) | 0x1; 706 config[1] = settings->wai.addr; 707 config[2] = 0x1; 708 709 err = st_lsm6dsx_shub_write_reg(hw, slv_addr, config, 710 sizeof(config)); 711 if (err < 0) 712 return err; 713 714 err = st_lsm6dsx_shub_master_enable(sensor, true); 715 if (err < 0) 716 return err; 717 718 st_lsm6dsx_shub_wait_complete(hw); 719 720 err = st_lsm6dsx_shub_read_reg(hw, 721 hub_settings->shub_out, 722 &data, sizeof(data)); 723 724 st_lsm6dsx_shub_master_enable(sensor, false); 725 726 if (err < 0) 727 return err; 728 729 if (data != settings->wai.val) 730 continue; 731 732 *i2c_addr = settings->i2c_addr[i]; 733 found = true; 734 break; 735 } 736 737 /* reset SLV0 channel */ 738 memset(config, 0, sizeof(config)); 739 err = st_lsm6dsx_shub_write_reg(hw, slv_addr, config, 740 sizeof(config)); 741 if (err < 0) 742 return err; 743 744 return found ? 0 : -ENODEV; 745 } 746 747 int st_lsm6dsx_shub_probe(struct st_lsm6dsx_hw *hw, const char *name) 748 { 749 enum st_lsm6dsx_sensor_id id = ST_LSM6DSX_ID_EXT0; 750 struct st_lsm6dsx_sensor *sensor; 751 int err, i, num_ext_dev = 0; 752 u8 i2c_addr = 0; 753 754 for (i = 0; i < ARRAY_SIZE(st_lsm6dsx_ext_dev_table); i++) { 755 err = st_lsm6dsx_shub_check_wai(hw, &i2c_addr, 756 &st_lsm6dsx_ext_dev_table[i]); 757 if (err == -ENODEV) 758 continue; 759 else if (err < 0) 760 return err; 761 762 hw->iio_devs[id] = st_lsm6dsx_shub_alloc_iiodev(hw, id, 763 &st_lsm6dsx_ext_dev_table[i], 764 i2c_addr, name); 765 if (!hw->iio_devs[id]) 766 return -ENOMEM; 767 768 sensor = iio_priv(hw->iio_devs[id]); 769 err = st_lsm6dsx_shub_init_device(sensor); 770 if (err < 0) 771 return err; 772 773 if (++num_ext_dev >= ST_LSM6DSX_MAX_SLV_NUM) 774 break; 775 id++; 776 } 777 778 return 0; 779 } 780