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