1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * vl6180.c - Support for STMicroelectronics VL6180 ALS, range and proximity 4 * sensor 5 * 6 * Copyright 2017 Peter Meerwald-Stadler <pmeerw@pmeerw.net> 7 * Copyright 2017 Manivannan Sadhasivam <manivannanece23@gmail.com> 8 * 9 * IIO driver for VL6180 (7-bit I2C slave address 0x29) 10 * 11 * Range: 0 to 100mm 12 * ALS: < 1 Lux up to 100 kLux 13 * IR: 850nm 14 * 15 * TODO: irq, threshold events, continuous mode, hardware buffer 16 */ 17 18 #include <linux/module.h> 19 #include <linux/i2c.h> 20 #include <linux/mutex.h> 21 #include <linux/err.h> 22 #include <linux/delay.h> 23 #include <linux/util_macros.h> 24 25 #include <linux/iio/iio.h> 26 #include <linux/iio/sysfs.h> 27 #include <linux/iio/buffer.h> 28 #include <linux/iio/trigger.h> 29 #include <linux/iio/trigger_consumer.h> 30 #include <linux/iio/triggered_buffer.h> 31 32 #define VL6180_DRV_NAME "vl6180" 33 34 /* Device identification register and value */ 35 #define VL6180_MODEL_ID 0x000 36 #define VL6180_MODEL_ID_VAL 0xb4 37 38 /* Configuration registers */ 39 #define VL6180_INTR_CONFIG 0x014 40 #define VL6180_INTR_CLEAR 0x015 41 #define VL6180_OUT_OF_RESET 0x016 42 #define VL6180_HOLD 0x017 43 #define VL6180_RANGE_START 0x018 44 #define VL6180_RANGE_INTER_MEAS_TIME 0x01b 45 #define VL6180_ALS_START 0x038 46 #define VL6180_ALS_INTER_MEAS_TIME 0x03e 47 #define VL6180_ALS_GAIN 0x03f 48 #define VL6180_ALS_IT 0x040 49 50 /* Status registers */ 51 #define VL6180_RANGE_STATUS 0x04d 52 #define VL6180_ALS_STATUS 0x04e 53 #define VL6180_INTR_STATUS 0x04f 54 55 /* Result value registers */ 56 #define VL6180_ALS_VALUE 0x050 57 #define VL6180_RANGE_VALUE 0x062 58 #define VL6180_RANGE_RATE 0x066 59 60 /* bits of the RANGE_START and ALS_START register */ 61 #define VL6180_MODE_CONT BIT(1) /* continuous mode */ 62 #define VL6180_STARTSTOP BIT(0) /* start measurement, auto-reset */ 63 64 /* bits of the INTR_STATUS and INTR_CONFIG register */ 65 #define VL6180_ALS_READY BIT(5) 66 #define VL6180_RANGE_READY BIT(2) 67 68 /* bits of the INTR_CLEAR register */ 69 #define VL6180_CLEAR_ERROR BIT(2) 70 #define VL6180_CLEAR_ALS BIT(1) 71 #define VL6180_CLEAR_RANGE BIT(0) 72 73 /* bits of the HOLD register */ 74 #define VL6180_HOLD_ON BIT(0) 75 76 /* default value for the ALS_IT register */ 77 #define VL6180_ALS_IT_100 0x63 /* 100 ms */ 78 79 /* values for the ALS_GAIN register */ 80 #define VL6180_ALS_GAIN_1 0x46 81 #define VL6180_ALS_GAIN_1_25 0x45 82 #define VL6180_ALS_GAIN_1_67 0x44 83 #define VL6180_ALS_GAIN_2_5 0x43 84 #define VL6180_ALS_GAIN_5 0x42 85 #define VL6180_ALS_GAIN_10 0x41 86 #define VL6180_ALS_GAIN_20 0x40 87 #define VL6180_ALS_GAIN_40 0x47 88 89 struct vl6180_data { 90 struct i2c_client *client; 91 struct mutex lock; 92 struct completion completion; 93 struct iio_trigger *trig; 94 unsigned int als_gain_milli; 95 unsigned int als_it_ms; 96 unsigned int als_meas_rate; 97 unsigned int range_meas_rate; 98 }; 99 100 enum { VL6180_ALS, VL6180_RANGE, VL6180_PROX }; 101 102 /** 103 * struct vl6180_chan_regs - Registers for accessing channels 104 * @drdy_mask: Data ready bit in status register 105 * @start_reg: Conversion start register 106 * @value_reg: Result value register 107 * @word: Register word length 108 */ 109 struct vl6180_chan_regs { 110 u8 drdy_mask; 111 u16 start_reg, value_reg; 112 bool word; 113 }; 114 115 static const struct vl6180_chan_regs vl6180_chan_regs_table[] = { 116 [VL6180_ALS] = { 117 .drdy_mask = VL6180_ALS_READY, 118 .start_reg = VL6180_ALS_START, 119 .value_reg = VL6180_ALS_VALUE, 120 .word = true, 121 }, 122 [VL6180_RANGE] = { 123 .drdy_mask = VL6180_RANGE_READY, 124 .start_reg = VL6180_RANGE_START, 125 .value_reg = VL6180_RANGE_VALUE, 126 .word = false, 127 }, 128 [VL6180_PROX] = { 129 .drdy_mask = VL6180_RANGE_READY, 130 .start_reg = VL6180_RANGE_START, 131 .value_reg = VL6180_RANGE_RATE, 132 .word = true, 133 }, 134 }; 135 136 static int vl6180_read(struct i2c_client *client, u16 cmd, void *databuf, 137 u8 len) 138 { 139 __be16 cmdbuf = cpu_to_be16(cmd); 140 struct i2c_msg msgs[2] = { 141 { .addr = client->addr, .len = sizeof(cmdbuf), .buf = (u8 *) &cmdbuf }, 142 { .addr = client->addr, .len = len, .buf = databuf, 143 .flags = I2C_M_RD } }; 144 int ret; 145 146 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 147 if (ret < 0) 148 dev_err(&client->dev, "failed reading register 0x%04x\n", cmd); 149 150 return ret; 151 } 152 153 static int vl6180_read_byte(struct i2c_client *client, u16 cmd) 154 { 155 u8 data; 156 int ret; 157 158 ret = vl6180_read(client, cmd, &data, sizeof(data)); 159 if (ret < 0) 160 return ret; 161 162 return data; 163 } 164 165 static int vl6180_read_word(struct i2c_client *client, u16 cmd) 166 { 167 __be16 data; 168 int ret; 169 170 ret = vl6180_read(client, cmd, &data, sizeof(data)); 171 if (ret < 0) 172 return ret; 173 174 return be16_to_cpu(data); 175 } 176 177 static int vl6180_write_byte(struct i2c_client *client, u16 cmd, u8 val) 178 { 179 u8 buf[3]; 180 struct i2c_msg msgs[1] = { 181 { .addr = client->addr, .len = sizeof(buf), .buf = (u8 *) &buf } }; 182 int ret; 183 184 buf[0] = cmd >> 8; 185 buf[1] = cmd & 0xff; 186 buf[2] = val; 187 188 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 189 if (ret < 0) { 190 dev_err(&client->dev, "failed writing register 0x%04x\n", cmd); 191 return ret; 192 } 193 194 return 0; 195 } 196 197 static int vl6180_write_word(struct i2c_client *client, u16 cmd, u16 val) 198 { 199 __be16 buf[2]; 200 struct i2c_msg msgs[1] = { 201 { .addr = client->addr, .len = sizeof(buf), .buf = (u8 *) &buf } }; 202 int ret; 203 204 buf[0] = cpu_to_be16(cmd); 205 buf[1] = cpu_to_be16(val); 206 207 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 208 if (ret < 0) { 209 dev_err(&client->dev, "failed writing register 0x%04x\n", cmd); 210 return ret; 211 } 212 213 return 0; 214 } 215 216 static int vl6180_measure(struct vl6180_data *data, int addr) 217 { 218 struct i2c_client *client = data->client; 219 unsigned long time_left; 220 int tries = 20, ret; 221 u16 value; 222 223 mutex_lock(&data->lock); 224 reinit_completion(&data->completion); 225 226 /* Start single shot measurement */ 227 ret = vl6180_write_byte(client, 228 vl6180_chan_regs_table[addr].start_reg, VL6180_STARTSTOP); 229 if (ret < 0) 230 goto fail; 231 232 if (client->irq) { 233 time_left = wait_for_completion_timeout(&data->completion, HZ / 10); 234 if (time_left == 0) { 235 ret = -ETIMEDOUT; 236 goto fail; 237 } 238 } else { 239 while (tries--) { 240 ret = vl6180_read_byte(client, VL6180_INTR_STATUS); 241 if (ret < 0) 242 goto fail; 243 244 if (ret & vl6180_chan_regs_table[addr].drdy_mask) 245 break; 246 msleep(20); 247 } 248 249 if (tries < 0) { 250 ret = -EIO; 251 goto fail; 252 } 253 } 254 255 /* Read result value from appropriate registers */ 256 ret = vl6180_chan_regs_table[addr].word ? 257 vl6180_read_word(client, vl6180_chan_regs_table[addr].value_reg) : 258 vl6180_read_byte(client, vl6180_chan_regs_table[addr].value_reg); 259 if (ret < 0) 260 goto fail; 261 value = ret; 262 263 /* Clear the interrupt flag after data read */ 264 ret = vl6180_write_byte(client, VL6180_INTR_CLEAR, 265 VL6180_CLEAR_ERROR | VL6180_CLEAR_ALS | VL6180_CLEAR_RANGE); 266 if (ret < 0) 267 goto fail; 268 269 ret = value; 270 271 fail: 272 mutex_unlock(&data->lock); 273 274 return ret; 275 } 276 277 static const struct iio_chan_spec vl6180_channels[] = { 278 { 279 .type = IIO_LIGHT, 280 .address = VL6180_ALS, 281 .scan_index = VL6180_ALS, 282 .scan_type = { 283 .sign = 'u', 284 .realbits = 16, 285 .storagebits = 16, 286 }, 287 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 288 BIT(IIO_CHAN_INFO_INT_TIME) | 289 BIT(IIO_CHAN_INFO_SCALE) | 290 BIT(IIO_CHAN_INFO_HARDWAREGAIN) | 291 BIT(IIO_CHAN_INFO_SAMP_FREQ), 292 }, { 293 .type = IIO_DISTANCE, 294 .address = VL6180_RANGE, 295 .scan_index = VL6180_RANGE, 296 .scan_type = { 297 .sign = 'u', 298 .realbits = 8, 299 .storagebits = 8, 300 }, 301 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 302 BIT(IIO_CHAN_INFO_SCALE) | 303 BIT(IIO_CHAN_INFO_SAMP_FREQ), 304 }, { 305 .type = IIO_PROXIMITY, 306 .address = VL6180_PROX, 307 .scan_index = VL6180_PROX, 308 .scan_type = { 309 .sign = 'u', 310 .realbits = 16, 311 .storagebits = 16, 312 }, 313 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 314 }, 315 IIO_CHAN_SOFT_TIMESTAMP(3), 316 }; 317 318 /* 319 * Available Ambient Light Sensor gain settings, 1/1000th, and 320 * corresponding setting for the VL6180_ALS_GAIN register 321 */ 322 static const int vl6180_als_gain_tab[8] = { 323 1000, 1250, 1670, 2500, 5000, 10000, 20000, 40000 324 }; 325 static const u8 vl6180_als_gain_tab_bits[8] = { 326 VL6180_ALS_GAIN_1, VL6180_ALS_GAIN_1_25, 327 VL6180_ALS_GAIN_1_67, VL6180_ALS_GAIN_2_5, 328 VL6180_ALS_GAIN_5, VL6180_ALS_GAIN_10, 329 VL6180_ALS_GAIN_20, VL6180_ALS_GAIN_40 330 }; 331 332 static int vl6180_read_raw(struct iio_dev *indio_dev, 333 struct iio_chan_spec const *chan, 334 int *val, int *val2, long mask) 335 { 336 struct vl6180_data *data = iio_priv(indio_dev); 337 int ret; 338 339 switch (mask) { 340 case IIO_CHAN_INFO_RAW: 341 ret = vl6180_measure(data, chan->address); 342 if (ret < 0) 343 return ret; 344 *val = ret; 345 346 return IIO_VAL_INT; 347 case IIO_CHAN_INFO_INT_TIME: 348 *val = data->als_it_ms; 349 *val2 = 1000; 350 351 return IIO_VAL_FRACTIONAL; 352 353 case IIO_CHAN_INFO_SCALE: 354 switch (chan->type) { 355 case IIO_LIGHT: 356 /* one ALS count is 0.32 Lux @ gain 1, IT 100 ms */ 357 *val = 32000; /* 0.32 * 1000 * 100 */ 358 *val2 = data->als_gain_milli * data->als_it_ms; 359 360 return IIO_VAL_FRACTIONAL; 361 362 case IIO_DISTANCE: 363 *val = 0; /* sensor reports mm, scale to meter */ 364 *val2 = 1000; 365 break; 366 default: 367 return -EINVAL; 368 } 369 370 return IIO_VAL_INT_PLUS_MICRO; 371 case IIO_CHAN_INFO_HARDWAREGAIN: 372 *val = data->als_gain_milli; 373 *val2 = 1000; 374 375 return IIO_VAL_FRACTIONAL; 376 377 case IIO_CHAN_INFO_SAMP_FREQ: 378 switch (chan->type) { 379 case IIO_DISTANCE: 380 *val = data->range_meas_rate; 381 return IIO_VAL_INT; 382 case IIO_LIGHT: 383 *val = data->als_meas_rate; 384 return IIO_VAL_INT; 385 default: 386 return -EINVAL; 387 } 388 389 default: 390 return -EINVAL; 391 } 392 } 393 394 static IIO_CONST_ATTR(als_gain_available, "1 1.25 1.67 2.5 5 10 20 40"); 395 396 static struct attribute *vl6180_attributes[] = { 397 &iio_const_attr_als_gain_available.dev_attr.attr, 398 NULL 399 }; 400 401 static const struct attribute_group vl6180_attribute_group = { 402 .attrs = vl6180_attributes, 403 }; 404 405 /* HOLD is needed before updating any config registers */ 406 static int vl6180_hold(struct vl6180_data *data, bool hold) 407 { 408 return vl6180_write_byte(data->client, VL6180_HOLD, 409 hold ? VL6180_HOLD_ON : 0); 410 } 411 412 static int vl6180_set_als_gain(struct vl6180_data *data, int val, int val2) 413 { 414 int i, ret, gain; 415 416 if (val < 1 || val > 40) 417 return -EINVAL; 418 419 gain = (val * 1000000 + val2) / 1000; 420 if (gain < 1 || gain > 40000) 421 return -EINVAL; 422 423 i = find_closest(gain, vl6180_als_gain_tab, 424 ARRAY_SIZE(vl6180_als_gain_tab)); 425 426 mutex_lock(&data->lock); 427 ret = vl6180_hold(data, true); 428 if (ret < 0) 429 goto fail; 430 431 ret = vl6180_write_byte(data->client, VL6180_ALS_GAIN, 432 vl6180_als_gain_tab_bits[i]); 433 434 if (ret >= 0) 435 data->als_gain_milli = vl6180_als_gain_tab[i]; 436 437 fail: 438 vl6180_hold(data, false); 439 mutex_unlock(&data->lock); 440 return ret; 441 } 442 443 static int vl6180_set_it(struct vl6180_data *data, int val, int val2) 444 { 445 int ret, it_ms; 446 447 it_ms = DIV_ROUND_CLOSEST(val2, 1000); /* round to ms */ 448 if (val != 0 || it_ms < 1 || it_ms > 512) 449 return -EINVAL; 450 451 mutex_lock(&data->lock); 452 ret = vl6180_hold(data, true); 453 if (ret < 0) 454 goto fail; 455 456 ret = vl6180_write_word(data->client, VL6180_ALS_IT, it_ms - 1); 457 458 if (ret >= 0) 459 data->als_it_ms = it_ms; 460 461 fail: 462 vl6180_hold(data, false); 463 mutex_unlock(&data->lock); 464 465 return ret; 466 } 467 468 static int vl6180_meas_reg_val_from_mhz(unsigned int mhz) 469 { 470 unsigned int period = DIV_ROUND_CLOSEST(1000 * 1000, mhz); 471 unsigned int reg_val = 0; 472 473 if (period > 10) 474 reg_val = period < 2550 ? (DIV_ROUND_CLOSEST(period, 10) - 1) : 254; 475 476 return reg_val; 477 } 478 479 static int vl6180_write_raw(struct iio_dev *indio_dev, 480 struct iio_chan_spec const *chan, 481 int val, int val2, long mask) 482 { 483 struct vl6180_data *data = iio_priv(indio_dev); 484 unsigned int reg_val; 485 486 switch (mask) { 487 case IIO_CHAN_INFO_INT_TIME: 488 return vl6180_set_it(data, val, val2); 489 490 case IIO_CHAN_INFO_HARDWAREGAIN: 491 if (chan->type != IIO_LIGHT) 492 return -EINVAL; 493 494 return vl6180_set_als_gain(data, val, val2); 495 496 case IIO_CHAN_INFO_SAMP_FREQ: 497 { 498 guard(mutex)(&data->lock); 499 switch (chan->type) { 500 case IIO_DISTANCE: 501 data->range_meas_rate = val; 502 reg_val = vl6180_meas_reg_val_from_mhz(val); 503 return vl6180_write_byte(data->client, 504 VL6180_RANGE_INTER_MEAS_TIME, reg_val); 505 506 case IIO_LIGHT: 507 data->als_meas_rate = val; 508 reg_val = vl6180_meas_reg_val_from_mhz(val); 509 return vl6180_write_byte(data->client, 510 VL6180_ALS_INTER_MEAS_TIME, reg_val); 511 512 default: 513 return -EINVAL; 514 } 515 } 516 517 default: 518 return -EINVAL; 519 } 520 } 521 522 static irqreturn_t vl6180_threaded_irq(int irq, void *priv) 523 { 524 struct iio_dev *indio_dev = priv; 525 struct vl6180_data *data = iio_priv(indio_dev); 526 527 if (iio_buffer_enabled(indio_dev)) 528 iio_trigger_poll_nested(indio_dev->trig); 529 else 530 complete(&data->completion); 531 532 return IRQ_HANDLED; 533 } 534 535 static irqreturn_t vl6180_trigger_handler(int irq, void *priv) 536 { 537 struct iio_poll_func *pf = priv; 538 struct iio_dev *indio_dev = pf->indio_dev; 539 struct vl6180_data *data = iio_priv(indio_dev); 540 s64 time_ns = iio_get_time_ns(indio_dev); 541 int ret, bit, i = 0; 542 struct { 543 u16 chan[2]; 544 aligned_s64 timestamp; 545 } scan = { }; 546 547 548 iio_for_each_active_channel(indio_dev, bit) { 549 if (vl6180_chan_regs_table[bit].word) 550 ret = vl6180_read_word(data->client, 551 vl6180_chan_regs_table[bit].value_reg); 552 else 553 ret = vl6180_read_byte(data->client, 554 vl6180_chan_regs_table[bit].value_reg); 555 556 if (ret < 0) { 557 dev_err(&data->client->dev, 558 "failed to read from value regs: %d\n", ret); 559 return IRQ_HANDLED; 560 } 561 562 scan.chan[i++] = ret; 563 } 564 565 iio_push_to_buffers_with_ts(indio_dev, &scan, sizeof(scan), time_ns); 566 iio_trigger_notify_done(indio_dev->trig); 567 568 /* Clear the interrupt flag after data read */ 569 ret = vl6180_write_byte(data->client, VL6180_INTR_CLEAR, 570 VL6180_CLEAR_ERROR | VL6180_CLEAR_ALS | VL6180_CLEAR_RANGE); 571 if (ret < 0) 572 dev_err(&data->client->dev, "failed to clear irq: %d\n", ret); 573 574 return IRQ_HANDLED; 575 } 576 577 static const struct iio_info vl6180_info = { 578 .read_raw = vl6180_read_raw, 579 .write_raw = vl6180_write_raw, 580 .attrs = &vl6180_attribute_group, 581 .validate_trigger = iio_validate_own_trigger, 582 }; 583 584 static int vl6180_buffer_postenable(struct iio_dev *indio_dev) 585 { 586 struct vl6180_data *data = iio_priv(indio_dev); 587 int bit; 588 589 iio_for_each_active_channel(indio_dev, bit) 590 return vl6180_write_byte(data->client, 591 vl6180_chan_regs_table[bit].start_reg, 592 VL6180_MODE_CONT | VL6180_STARTSTOP); 593 594 return -EINVAL; 595 } 596 597 static int vl6180_buffer_postdisable(struct iio_dev *indio_dev) 598 { 599 struct vl6180_data *data = iio_priv(indio_dev); 600 int bit; 601 602 iio_for_each_active_channel(indio_dev, bit) 603 return vl6180_write_byte(data->client, 604 vl6180_chan_regs_table[bit].start_reg, 605 VL6180_STARTSTOP); 606 607 return -EINVAL; 608 } 609 610 static const struct iio_buffer_setup_ops iio_triggered_buffer_setup_ops = { 611 .postenable = &vl6180_buffer_postenable, 612 .postdisable = &vl6180_buffer_postdisable, 613 }; 614 615 static const struct iio_trigger_ops vl6180_trigger_ops = { 616 .validate_device = iio_trigger_validate_own_device, 617 }; 618 619 static int vl6180_init(struct vl6180_data *data, struct iio_dev *indio_dev) 620 { 621 struct i2c_client *client = data->client; 622 int ret; 623 624 ret = vl6180_read_byte(client, VL6180_MODEL_ID); 625 if (ret < 0) 626 return ret; 627 628 if (ret != VL6180_MODEL_ID_VAL) { 629 dev_err(&client->dev, "invalid model ID %02x\n", ret); 630 return -ENODEV; 631 } 632 633 ret = vl6180_hold(data, true); 634 if (ret < 0) 635 return ret; 636 637 ret = vl6180_read_byte(client, VL6180_OUT_OF_RESET); 638 if (ret < 0) 639 return ret; 640 641 /* 642 * Detect false reset condition here. This bit is always set when the 643 * system comes out of reset. 644 */ 645 if (ret != 0x01) 646 dev_info(&client->dev, "device is not fresh out of reset\n"); 647 648 /* Enable ALS and Range ready interrupts */ 649 ret = vl6180_write_byte(client, VL6180_INTR_CONFIG, 650 VL6180_ALS_READY | VL6180_RANGE_READY); 651 if (ret < 0) 652 return ret; 653 654 ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev, NULL, 655 &vl6180_trigger_handler, 656 &iio_triggered_buffer_setup_ops); 657 if (ret) 658 return ret; 659 660 /* Default Range inter-measurement time: 50ms or 20000 mHz */ 661 ret = vl6180_write_byte(client, VL6180_RANGE_INTER_MEAS_TIME, 662 vl6180_meas_reg_val_from_mhz(20000)); 663 if (ret < 0) 664 return ret; 665 data->range_meas_rate = 20000; 666 667 /* Default ALS inter-measurement time: 10ms or 100000 mHz */ 668 ret = vl6180_write_byte(client, VL6180_ALS_INTER_MEAS_TIME, 669 vl6180_meas_reg_val_from_mhz(100000)); 670 if (ret < 0) 671 return ret; 672 data->als_meas_rate = 100000; 673 674 /* ALS integration time: 100ms */ 675 data->als_it_ms = 100; 676 ret = vl6180_write_word(client, VL6180_ALS_IT, VL6180_ALS_IT_100); 677 if (ret < 0) 678 return ret; 679 680 /* ALS gain: 1 */ 681 data->als_gain_milli = 1000; 682 ret = vl6180_write_byte(client, VL6180_ALS_GAIN, VL6180_ALS_GAIN_1); 683 if (ret < 0) 684 return ret; 685 686 ret = vl6180_write_byte(client, VL6180_OUT_OF_RESET, 0x00); 687 if (ret < 0) 688 return ret; 689 690 return vl6180_hold(data, false); 691 } 692 693 static int vl6180_probe(struct i2c_client *client) 694 { 695 struct vl6180_data *data; 696 struct iio_dev *indio_dev; 697 int ret; 698 699 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 700 if (!indio_dev) 701 return -ENOMEM; 702 703 data = iio_priv(indio_dev); 704 i2c_set_clientdata(client, indio_dev); 705 data->client = client; 706 mutex_init(&data->lock); 707 708 indio_dev->info = &vl6180_info; 709 indio_dev->channels = vl6180_channels; 710 indio_dev->num_channels = ARRAY_SIZE(vl6180_channels); 711 indio_dev->name = VL6180_DRV_NAME; 712 indio_dev->modes = INDIO_DIRECT_MODE; 713 714 ret = vl6180_init(data, indio_dev); 715 if (ret < 0) 716 return ret; 717 718 if (client->irq) { 719 ret = devm_request_threaded_irq(&client->dev, client->irq, 720 NULL, vl6180_threaded_irq, 721 IRQF_ONESHOT, 722 indio_dev->name, indio_dev); 723 if (ret) 724 return ret; 725 726 init_completion(&data->completion); 727 728 data->trig = devm_iio_trigger_alloc(&client->dev, "%s-dev%d", 729 indio_dev->name, iio_device_id(indio_dev)); 730 if (!data->trig) 731 return -ENOMEM; 732 733 data->trig->ops = &vl6180_trigger_ops; 734 iio_trigger_set_drvdata(data->trig, indio_dev); 735 ret = devm_iio_trigger_register(&client->dev, data->trig); 736 if (ret) 737 return ret; 738 739 indio_dev->trig = iio_trigger_get(data->trig); 740 } 741 742 return devm_iio_device_register(&client->dev, indio_dev); 743 } 744 745 static const struct of_device_id vl6180_of_match[] = { 746 { .compatible = "st,vl6180", }, 747 { } 748 }; 749 MODULE_DEVICE_TABLE(of, vl6180_of_match); 750 751 static const struct i2c_device_id vl6180_id[] = { 752 { .name = "vl6180" }, 753 { } 754 }; 755 MODULE_DEVICE_TABLE(i2c, vl6180_id); 756 757 static struct i2c_driver vl6180_driver = { 758 .driver = { 759 .name = VL6180_DRV_NAME, 760 .of_match_table = vl6180_of_match, 761 }, 762 .probe = vl6180_probe, 763 .id_table = vl6180_id, 764 }; 765 766 module_i2c_driver(vl6180_driver); 767 768 MODULE_AUTHOR("Peter Meerwald-Stadler <pmeerw@pmeerw.net>"); 769 MODULE_AUTHOR("Manivannan Sadhasivam <manivannanece23@gmail.com>"); 770 MODULE_DESCRIPTION("STMicro VL6180 ALS, range and proximity sensor driver"); 771 MODULE_LICENSE("GPL"); 772