1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * vcnl4000.c - Support for Vishay VCNL4000/4010/4020/4040/4200 combined ambient 4 * light and proximity sensor 5 * 6 * Copyright 2012 Peter Meerwald <pmeerw@pmeerw.net> 7 * Copyright 2019 Pursim SPC 8 * Copyright 2020 Mathieu Othacehe <m.othacehe@gmail.com> 9 * 10 * IIO driver for: 11 * VCNL4000/10/20 (7-bit I2C slave address 0x13) 12 * VCNL4040 (7-bit I2C slave address 0x60) 13 * VCNL4200 (7-bit I2C slave address 0x51) 14 * 15 * TODO: 16 * allow to adjust IR current 17 * interrupts (VCNL4040, VCNL4200) 18 */ 19 20 #include <linux/bitfield.h> 21 #include <linux/module.h> 22 #include <linux/i2c.h> 23 #include <linux/err.h> 24 #include <linux/delay.h> 25 #include <linux/pm_runtime.h> 26 #include <linux/interrupt.h> 27 28 #include <linux/iio/buffer.h> 29 #include <linux/iio/events.h> 30 #include <linux/iio/iio.h> 31 #include <linux/iio/sysfs.h> 32 #include <linux/iio/trigger.h> 33 #include <linux/iio/trigger_consumer.h> 34 #include <linux/iio/triggered_buffer.h> 35 36 #define VCNL4000_DRV_NAME "vcnl4000" 37 #define VCNL4000_PROD_ID 0x01 38 #define VCNL4010_PROD_ID 0x02 /* for VCNL4020, VCNL4010 */ 39 #define VCNL4040_PROD_ID 0x86 40 #define VCNL4200_PROD_ID 0x58 41 42 #define VCNL4000_COMMAND 0x80 /* Command register */ 43 #define VCNL4000_PROD_REV 0x81 /* Product ID and Revision ID */ 44 #define VCNL4010_PROX_RATE 0x82 /* Proximity rate */ 45 #define VCNL4000_LED_CURRENT 0x83 /* IR LED current for proximity mode */ 46 #define VCNL4000_AL_PARAM 0x84 /* Ambient light parameter register */ 47 #define VCNL4010_ALS_PARAM 0x84 /* ALS rate */ 48 #define VCNL4000_AL_RESULT_HI 0x85 /* Ambient light result register, MSB */ 49 #define VCNL4000_AL_RESULT_LO 0x86 /* Ambient light result register, LSB */ 50 #define VCNL4000_PS_RESULT_HI 0x87 /* Proximity result register, MSB */ 51 #define VCNL4000_PS_RESULT_LO 0x88 /* Proximity result register, LSB */ 52 #define VCNL4000_PS_MEAS_FREQ 0x89 /* Proximity test signal frequency */ 53 #define VCNL4010_INT_CTRL 0x89 /* Interrupt control */ 54 #define VCNL4000_PS_MOD_ADJ 0x8a /* Proximity modulator timing adjustment */ 55 #define VCNL4010_LOW_THR_HI 0x8a /* Low threshold, MSB */ 56 #define VCNL4010_LOW_THR_LO 0x8b /* Low threshold, LSB */ 57 #define VCNL4010_HIGH_THR_HI 0x8c /* High threshold, MSB */ 58 #define VCNL4010_HIGH_THR_LO 0x8d /* High threshold, LSB */ 59 #define VCNL4010_ISR 0x8e /* Interrupt status */ 60 61 #define VCNL4200_AL_CONF 0x00 /* Ambient light configuration */ 62 #define VCNL4200_PS_CONF1 0x03 /* Proximity configuration */ 63 #define VCNL4200_PS_DATA 0x08 /* Proximity data */ 64 #define VCNL4200_AL_DATA 0x09 /* Ambient light data */ 65 #define VCNL4200_DEV_ID 0x0e /* Device ID, slave address and version */ 66 67 #define VCNL4040_DEV_ID 0x0c /* Device ID and version */ 68 69 /* Bit masks for COMMAND register */ 70 #define VCNL4000_AL_RDY BIT(6) /* ALS data ready? */ 71 #define VCNL4000_PS_RDY BIT(5) /* proximity data ready? */ 72 #define VCNL4000_AL_OD BIT(4) /* start on-demand ALS measurement */ 73 #define VCNL4000_PS_OD BIT(3) /* start on-demand proximity measurement */ 74 #define VCNL4000_ALS_EN BIT(2) /* start ALS measurement */ 75 #define VCNL4000_PROX_EN BIT(1) /* start proximity measurement */ 76 #define VCNL4000_SELF_TIMED_EN BIT(0) /* start self-timed measurement */ 77 78 #define VCNL4040_ALS_CONF_ALS_SHUTDOWN BIT(0) 79 #define VCNL4040_PS_CONF1_PS_SHUTDOWN BIT(0) 80 #define VCNL4040_PS_CONF2_PS_IT GENMASK(3, 1) /* Proximity integration time */ 81 82 /* Bit masks for interrupt registers. */ 83 #define VCNL4010_INT_THR_SEL BIT(0) /* Select threshold interrupt source */ 84 #define VCNL4010_INT_THR_EN BIT(1) /* Threshold interrupt type */ 85 #define VCNL4010_INT_ALS_EN BIT(2) /* Enable on ALS data ready */ 86 #define VCNL4010_INT_PROX_EN BIT(3) /* Enable on proximity data ready */ 87 88 #define VCNL4010_INT_THR_HIGH 0 /* High threshold exceeded */ 89 #define VCNL4010_INT_THR_LOW 1 /* Low threshold exceeded */ 90 #define VCNL4010_INT_ALS 2 /* ALS data ready */ 91 #define VCNL4010_INT_PROXIMITY 3 /* Proximity data ready */ 92 93 #define VCNL4010_INT_THR \ 94 (BIT(VCNL4010_INT_THR_LOW) | BIT(VCNL4010_INT_THR_HIGH)) 95 #define VCNL4010_INT_DRDY \ 96 (BIT(VCNL4010_INT_PROXIMITY) | BIT(VCNL4010_INT_ALS)) 97 98 static const int vcnl4010_prox_sampling_frequency[][2] = { 99 {1, 950000}, 100 {3, 906250}, 101 {7, 812500}, 102 {16, 625000}, 103 {31, 250000}, 104 {62, 500000}, 105 {125, 0}, 106 {250, 0}, 107 }; 108 109 static const int vcnl4040_ps_it_times[][2] = { 110 {0, 100}, 111 {0, 150}, 112 {0, 200}, 113 {0, 250}, 114 {0, 300}, 115 {0, 350}, 116 {0, 400}, 117 {0, 800}, 118 }; 119 120 #define VCNL4000_SLEEP_DELAY_MS 2000 /* before we enter pm_runtime_suspend */ 121 122 enum vcnl4000_device_ids { 123 VCNL4000, 124 VCNL4010, 125 VCNL4040, 126 VCNL4200, 127 }; 128 129 struct vcnl4200_channel { 130 u8 reg; 131 ktime_t last_measurement; 132 ktime_t sampling_rate; 133 struct mutex lock; 134 }; 135 136 struct vcnl4000_data { 137 struct i2c_client *client; 138 enum vcnl4000_device_ids id; 139 int rev; 140 int al_scale; 141 const struct vcnl4000_chip_spec *chip_spec; 142 struct mutex vcnl4000_lock; 143 struct vcnl4200_channel vcnl4200_al; 144 struct vcnl4200_channel vcnl4200_ps; 145 uint32_t near_level; 146 }; 147 148 struct vcnl4000_chip_spec { 149 const char *prod; 150 struct iio_chan_spec const *channels; 151 const int num_channels; 152 const struct iio_info *info; 153 bool irq_support; 154 int (*init)(struct vcnl4000_data *data); 155 int (*measure_light)(struct vcnl4000_data *data, int *val); 156 int (*measure_proximity)(struct vcnl4000_data *data, int *val); 157 int (*set_power_state)(struct vcnl4000_data *data, bool on); 158 }; 159 160 static const struct i2c_device_id vcnl4000_id[] = { 161 { "vcnl4000", VCNL4000 }, 162 { "vcnl4010", VCNL4010 }, 163 { "vcnl4020", VCNL4010 }, 164 { "vcnl4040", VCNL4040 }, 165 { "vcnl4200", VCNL4200 }, 166 { } 167 }; 168 MODULE_DEVICE_TABLE(i2c, vcnl4000_id); 169 170 static int vcnl4000_set_power_state(struct vcnl4000_data *data, bool on) 171 { 172 /* no suspend op */ 173 return 0; 174 } 175 176 static int vcnl4000_init(struct vcnl4000_data *data) 177 { 178 int ret, prod_id; 179 180 ret = i2c_smbus_read_byte_data(data->client, VCNL4000_PROD_REV); 181 if (ret < 0) 182 return ret; 183 184 prod_id = ret >> 4; 185 switch (prod_id) { 186 case VCNL4000_PROD_ID: 187 if (data->id != VCNL4000) 188 dev_warn(&data->client->dev, 189 "wrong device id, use vcnl4000"); 190 break; 191 case VCNL4010_PROD_ID: 192 if (data->id != VCNL4010) 193 dev_warn(&data->client->dev, 194 "wrong device id, use vcnl4010/4020"); 195 break; 196 default: 197 return -ENODEV; 198 } 199 200 data->rev = ret & 0xf; 201 data->al_scale = 250000; 202 mutex_init(&data->vcnl4000_lock); 203 204 return data->chip_spec->set_power_state(data, true); 205 }; 206 207 static ssize_t vcnl4000_write_als_enable(struct vcnl4000_data *data, bool en) 208 { 209 int ret; 210 211 mutex_lock(&data->vcnl4000_lock); 212 213 ret = i2c_smbus_read_word_data(data->client, VCNL4200_AL_CONF); 214 if (ret < 0) 215 goto out; 216 217 if (en) 218 ret &= ~VCNL4040_ALS_CONF_ALS_SHUTDOWN; 219 else 220 ret |= VCNL4040_ALS_CONF_ALS_SHUTDOWN; 221 222 ret = i2c_smbus_write_word_data(data->client, VCNL4200_AL_CONF, ret); 223 224 out: 225 mutex_unlock(&data->vcnl4000_lock); 226 227 return ret; 228 } 229 230 static ssize_t vcnl4000_write_ps_enable(struct vcnl4000_data *data, bool en) 231 { 232 int ret; 233 234 mutex_lock(&data->vcnl4000_lock); 235 236 ret = i2c_smbus_read_word_data(data->client, VCNL4200_PS_CONF1); 237 if (ret < 0) 238 goto out; 239 240 if (en) 241 ret &= ~VCNL4040_PS_CONF1_PS_SHUTDOWN; 242 else 243 ret |= VCNL4040_PS_CONF1_PS_SHUTDOWN; 244 245 ret = i2c_smbus_write_word_data(data->client, VCNL4200_PS_CONF1, ret); 246 247 out: 248 mutex_unlock(&data->vcnl4000_lock); 249 250 return ret; 251 } 252 253 static int vcnl4200_set_power_state(struct vcnl4000_data *data, bool on) 254 { 255 int ret; 256 257 ret = vcnl4000_write_als_enable(data, on); 258 if (ret < 0) 259 return ret; 260 261 ret = vcnl4000_write_ps_enable(data, on); 262 if (ret < 0) 263 return ret; 264 265 if (on) { 266 /* Wait at least one integration cycle before fetching data */ 267 data->vcnl4200_al.last_measurement = ktime_get(); 268 data->vcnl4200_ps.last_measurement = ktime_get(); 269 } 270 271 return 0; 272 } 273 274 static int vcnl4200_init(struct vcnl4000_data *data) 275 { 276 int ret, id; 277 278 ret = i2c_smbus_read_word_data(data->client, VCNL4200_DEV_ID); 279 if (ret < 0) 280 return ret; 281 282 id = ret & 0xff; 283 284 if (id != VCNL4200_PROD_ID) { 285 ret = i2c_smbus_read_word_data(data->client, VCNL4040_DEV_ID); 286 if (ret < 0) 287 return ret; 288 289 id = ret & 0xff; 290 291 if (id != VCNL4040_PROD_ID) 292 return -ENODEV; 293 } 294 295 dev_dbg(&data->client->dev, "device id 0x%x", id); 296 297 data->rev = (ret >> 8) & 0xf; 298 299 data->vcnl4200_al.reg = VCNL4200_AL_DATA; 300 data->vcnl4200_ps.reg = VCNL4200_PS_DATA; 301 switch (id) { 302 case VCNL4200_PROD_ID: 303 /* Default wait time is 50ms, add 20% tolerance. */ 304 data->vcnl4200_al.sampling_rate = ktime_set(0, 60000 * 1000); 305 /* Default wait time is 4.8ms, add 20% tolerance. */ 306 data->vcnl4200_ps.sampling_rate = ktime_set(0, 5760 * 1000); 307 data->al_scale = 24000; 308 break; 309 case VCNL4040_PROD_ID: 310 /* Default wait time is 80ms, add 20% tolerance. */ 311 data->vcnl4200_al.sampling_rate = ktime_set(0, 96000 * 1000); 312 /* Default wait time is 5ms, add 20% tolerance. */ 313 data->vcnl4200_ps.sampling_rate = ktime_set(0, 6000 * 1000); 314 data->al_scale = 120000; 315 break; 316 } 317 mutex_init(&data->vcnl4200_al.lock); 318 mutex_init(&data->vcnl4200_ps.lock); 319 320 ret = data->chip_spec->set_power_state(data, true); 321 if (ret < 0) 322 return ret; 323 324 return 0; 325 }; 326 327 static int vcnl4000_read_data(struct vcnl4000_data *data, u8 data_reg, int *val) 328 { 329 s32 ret; 330 331 ret = i2c_smbus_read_word_swapped(data->client, data_reg); 332 if (ret < 0) 333 return ret; 334 335 *val = ret; 336 return 0; 337 } 338 339 static int vcnl4000_write_data(struct vcnl4000_data *data, u8 data_reg, int val) 340 { 341 if (val > U16_MAX) 342 return -ERANGE; 343 344 return i2c_smbus_write_word_swapped(data->client, data_reg, val); 345 } 346 347 348 static int vcnl4000_measure(struct vcnl4000_data *data, u8 req_mask, 349 u8 rdy_mask, u8 data_reg, int *val) 350 { 351 int tries = 20; 352 int ret; 353 354 mutex_lock(&data->vcnl4000_lock); 355 356 ret = i2c_smbus_write_byte_data(data->client, VCNL4000_COMMAND, 357 req_mask); 358 if (ret < 0) 359 goto fail; 360 361 /* wait for data to become ready */ 362 while (tries--) { 363 ret = i2c_smbus_read_byte_data(data->client, VCNL4000_COMMAND); 364 if (ret < 0) 365 goto fail; 366 if (ret & rdy_mask) 367 break; 368 msleep(20); /* measurement takes up to 100 ms */ 369 } 370 371 if (tries < 0) { 372 dev_err(&data->client->dev, 373 "vcnl4000_measure() failed, data not ready\n"); 374 ret = -EIO; 375 goto fail; 376 } 377 378 ret = vcnl4000_read_data(data, data_reg, val); 379 if (ret < 0) 380 goto fail; 381 382 mutex_unlock(&data->vcnl4000_lock); 383 384 return 0; 385 386 fail: 387 mutex_unlock(&data->vcnl4000_lock); 388 return ret; 389 } 390 391 static int vcnl4200_measure(struct vcnl4000_data *data, 392 struct vcnl4200_channel *chan, int *val) 393 { 394 int ret; 395 s64 delta; 396 ktime_t next_measurement; 397 398 mutex_lock(&chan->lock); 399 400 next_measurement = ktime_add(chan->last_measurement, 401 chan->sampling_rate); 402 delta = ktime_us_delta(next_measurement, ktime_get()); 403 if (delta > 0) 404 usleep_range(delta, delta + 500); 405 chan->last_measurement = ktime_get(); 406 407 mutex_unlock(&chan->lock); 408 409 ret = i2c_smbus_read_word_data(data->client, chan->reg); 410 if (ret < 0) 411 return ret; 412 413 *val = ret; 414 415 return 0; 416 } 417 418 static int vcnl4000_measure_light(struct vcnl4000_data *data, int *val) 419 { 420 return vcnl4000_measure(data, 421 VCNL4000_AL_OD, VCNL4000_AL_RDY, 422 VCNL4000_AL_RESULT_HI, val); 423 } 424 425 static int vcnl4200_measure_light(struct vcnl4000_data *data, int *val) 426 { 427 return vcnl4200_measure(data, &data->vcnl4200_al, val); 428 } 429 430 static int vcnl4000_measure_proximity(struct vcnl4000_data *data, int *val) 431 { 432 return vcnl4000_measure(data, 433 VCNL4000_PS_OD, VCNL4000_PS_RDY, 434 VCNL4000_PS_RESULT_HI, val); 435 } 436 437 static int vcnl4200_measure_proximity(struct vcnl4000_data *data, int *val) 438 { 439 return vcnl4200_measure(data, &data->vcnl4200_ps, val); 440 } 441 442 static int vcnl4010_read_proxy_samp_freq(struct vcnl4000_data *data, int *val, 443 int *val2) 444 { 445 int ret; 446 447 ret = i2c_smbus_read_byte_data(data->client, VCNL4010_PROX_RATE); 448 if (ret < 0) 449 return ret; 450 451 if (ret >= ARRAY_SIZE(vcnl4010_prox_sampling_frequency)) 452 return -EINVAL; 453 454 *val = vcnl4010_prox_sampling_frequency[ret][0]; 455 *val2 = vcnl4010_prox_sampling_frequency[ret][1]; 456 457 return 0; 458 } 459 460 static bool vcnl4010_is_in_periodic_mode(struct vcnl4000_data *data) 461 { 462 int ret; 463 464 ret = i2c_smbus_read_byte_data(data->client, VCNL4000_COMMAND); 465 if (ret < 0) 466 return false; 467 468 return !!(ret & VCNL4000_SELF_TIMED_EN); 469 } 470 471 static int vcnl4000_set_pm_runtime_state(struct vcnl4000_data *data, bool on) 472 { 473 struct device *dev = &data->client->dev; 474 int ret; 475 476 if (on) { 477 ret = pm_runtime_resume_and_get(dev); 478 } else { 479 pm_runtime_mark_last_busy(dev); 480 ret = pm_runtime_put_autosuspend(dev); 481 } 482 483 return ret; 484 } 485 486 static int vcnl4040_read_ps_it(struct vcnl4000_data *data, int *val, int *val2) 487 { 488 int ret; 489 490 ret = i2c_smbus_read_word_data(data->client, VCNL4200_PS_CONF1); 491 if (ret < 0) 492 return ret; 493 494 ret = FIELD_GET(VCNL4040_PS_CONF2_PS_IT, ret); 495 496 if (ret >= ARRAY_SIZE(vcnl4040_ps_it_times)) 497 return -EINVAL; 498 499 *val = vcnl4040_ps_it_times[ret][0]; 500 *val2 = vcnl4040_ps_it_times[ret][1]; 501 502 return 0; 503 } 504 505 static ssize_t vcnl4040_write_ps_it(struct vcnl4000_data *data, int val) 506 { 507 unsigned int i; 508 int ret, index = -1; 509 u16 regval; 510 511 for (i = 0; i < ARRAY_SIZE(vcnl4040_ps_it_times); i++) { 512 if (val == vcnl4040_ps_it_times[i][1]) { 513 index = i; 514 break; 515 } 516 } 517 518 if (index < 0) 519 return -EINVAL; 520 521 mutex_lock(&data->vcnl4000_lock); 522 523 ret = i2c_smbus_read_word_data(data->client, VCNL4200_PS_CONF1); 524 if (ret < 0) 525 goto out; 526 527 regval = (ret & ~VCNL4040_PS_CONF2_PS_IT) | 528 FIELD_PREP(VCNL4040_PS_CONF2_PS_IT, index); 529 ret = i2c_smbus_write_word_data(data->client, VCNL4200_PS_CONF1, 530 regval); 531 532 out: 533 mutex_unlock(&data->vcnl4000_lock); 534 return ret; 535 } 536 537 static int vcnl4000_read_raw(struct iio_dev *indio_dev, 538 struct iio_chan_spec const *chan, 539 int *val, int *val2, long mask) 540 { 541 int ret; 542 struct vcnl4000_data *data = iio_priv(indio_dev); 543 544 switch (mask) { 545 case IIO_CHAN_INFO_RAW: 546 ret = vcnl4000_set_pm_runtime_state(data, true); 547 if (ret < 0) 548 return ret; 549 550 switch (chan->type) { 551 case IIO_LIGHT: 552 ret = data->chip_spec->measure_light(data, val); 553 if (!ret) 554 ret = IIO_VAL_INT; 555 break; 556 case IIO_PROXIMITY: 557 ret = data->chip_spec->measure_proximity(data, val); 558 if (!ret) 559 ret = IIO_VAL_INT; 560 break; 561 default: 562 ret = -EINVAL; 563 } 564 vcnl4000_set_pm_runtime_state(data, false); 565 return ret; 566 case IIO_CHAN_INFO_SCALE: 567 if (chan->type != IIO_LIGHT) 568 return -EINVAL; 569 570 *val = 0; 571 *val2 = data->al_scale; 572 return IIO_VAL_INT_PLUS_MICRO; 573 case IIO_CHAN_INFO_INT_TIME: 574 if (chan->type != IIO_PROXIMITY) 575 return -EINVAL; 576 ret = vcnl4040_read_ps_it(data, val, val2); 577 if (ret < 0) 578 return ret; 579 return IIO_VAL_INT_PLUS_MICRO; 580 default: 581 return -EINVAL; 582 } 583 } 584 585 static int vcnl4040_write_raw(struct iio_dev *indio_dev, 586 struct iio_chan_spec const *chan, 587 int val, int val2, long mask) 588 { 589 struct vcnl4000_data *data = iio_priv(indio_dev); 590 591 switch (mask) { 592 case IIO_CHAN_INFO_INT_TIME: 593 if (val != 0) 594 return -EINVAL; 595 if (chan->type != IIO_PROXIMITY) 596 return -EINVAL; 597 return vcnl4040_write_ps_it(data, val2); 598 default: 599 return -EINVAL; 600 } 601 } 602 603 static int vcnl4040_read_avail(struct iio_dev *indio_dev, 604 struct iio_chan_spec const *chan, 605 const int **vals, int *type, int *length, 606 long mask) 607 { 608 switch (mask) { 609 case IIO_CHAN_INFO_INT_TIME: 610 *vals = (int *)vcnl4040_ps_it_times; 611 *type = IIO_VAL_INT_PLUS_MICRO; 612 *length = 2 * ARRAY_SIZE(vcnl4040_ps_it_times); 613 return IIO_AVAIL_LIST; 614 default: 615 return -EINVAL; 616 } 617 } 618 619 static int vcnl4010_read_raw(struct iio_dev *indio_dev, 620 struct iio_chan_spec const *chan, 621 int *val, int *val2, long mask) 622 { 623 int ret; 624 struct vcnl4000_data *data = iio_priv(indio_dev); 625 626 switch (mask) { 627 case IIO_CHAN_INFO_RAW: 628 case IIO_CHAN_INFO_SCALE: 629 ret = iio_device_claim_direct_mode(indio_dev); 630 if (ret) 631 return ret; 632 633 /* Protect against event capture. */ 634 if (vcnl4010_is_in_periodic_mode(data)) { 635 ret = -EBUSY; 636 } else { 637 ret = vcnl4000_read_raw(indio_dev, chan, val, val2, 638 mask); 639 } 640 641 iio_device_release_direct_mode(indio_dev); 642 return ret; 643 case IIO_CHAN_INFO_SAMP_FREQ: 644 switch (chan->type) { 645 case IIO_PROXIMITY: 646 ret = vcnl4010_read_proxy_samp_freq(data, val, val2); 647 if (ret < 0) 648 return ret; 649 return IIO_VAL_INT_PLUS_MICRO; 650 default: 651 return -EINVAL; 652 } 653 default: 654 return -EINVAL; 655 } 656 } 657 658 static int vcnl4010_read_avail(struct iio_dev *indio_dev, 659 struct iio_chan_spec const *chan, 660 const int **vals, int *type, int *length, 661 long mask) 662 { 663 switch (mask) { 664 case IIO_CHAN_INFO_SAMP_FREQ: 665 *vals = (int *)vcnl4010_prox_sampling_frequency; 666 *type = IIO_VAL_INT_PLUS_MICRO; 667 *length = 2 * ARRAY_SIZE(vcnl4010_prox_sampling_frequency); 668 return IIO_AVAIL_LIST; 669 default: 670 return -EINVAL; 671 } 672 } 673 674 static int vcnl4010_write_proxy_samp_freq(struct vcnl4000_data *data, int val, 675 int val2) 676 { 677 unsigned int i; 678 int index = -1; 679 680 for (i = 0; i < ARRAY_SIZE(vcnl4010_prox_sampling_frequency); i++) { 681 if (val == vcnl4010_prox_sampling_frequency[i][0] && 682 val2 == vcnl4010_prox_sampling_frequency[i][1]) { 683 index = i; 684 break; 685 } 686 } 687 688 if (index < 0) 689 return -EINVAL; 690 691 return i2c_smbus_write_byte_data(data->client, VCNL4010_PROX_RATE, 692 index); 693 } 694 695 static int vcnl4010_write_raw(struct iio_dev *indio_dev, 696 struct iio_chan_spec const *chan, 697 int val, int val2, long mask) 698 { 699 int ret; 700 struct vcnl4000_data *data = iio_priv(indio_dev); 701 702 ret = iio_device_claim_direct_mode(indio_dev); 703 if (ret) 704 return ret; 705 706 /* Protect against event capture. */ 707 if (vcnl4010_is_in_periodic_mode(data)) { 708 ret = -EBUSY; 709 goto end; 710 } 711 712 switch (mask) { 713 case IIO_CHAN_INFO_SAMP_FREQ: 714 switch (chan->type) { 715 case IIO_PROXIMITY: 716 ret = vcnl4010_write_proxy_samp_freq(data, val, val2); 717 goto end; 718 default: 719 ret = -EINVAL; 720 goto end; 721 } 722 default: 723 ret = -EINVAL; 724 goto end; 725 } 726 727 end: 728 iio_device_release_direct_mode(indio_dev); 729 return ret; 730 } 731 732 static int vcnl4010_read_event(struct iio_dev *indio_dev, 733 const struct iio_chan_spec *chan, 734 enum iio_event_type type, 735 enum iio_event_direction dir, 736 enum iio_event_info info, 737 int *val, int *val2) 738 { 739 int ret; 740 struct vcnl4000_data *data = iio_priv(indio_dev); 741 742 switch (info) { 743 case IIO_EV_INFO_VALUE: 744 switch (dir) { 745 case IIO_EV_DIR_RISING: 746 ret = vcnl4000_read_data(data, VCNL4010_HIGH_THR_HI, 747 val); 748 if (ret < 0) 749 return ret; 750 return IIO_VAL_INT; 751 case IIO_EV_DIR_FALLING: 752 ret = vcnl4000_read_data(data, VCNL4010_LOW_THR_HI, 753 val); 754 if (ret < 0) 755 return ret; 756 return IIO_VAL_INT; 757 default: 758 return -EINVAL; 759 } 760 default: 761 return -EINVAL; 762 } 763 } 764 765 static int vcnl4010_write_event(struct iio_dev *indio_dev, 766 const struct iio_chan_spec *chan, 767 enum iio_event_type type, 768 enum iio_event_direction dir, 769 enum iio_event_info info, 770 int val, int val2) 771 { 772 int ret; 773 struct vcnl4000_data *data = iio_priv(indio_dev); 774 775 switch (info) { 776 case IIO_EV_INFO_VALUE: 777 switch (dir) { 778 case IIO_EV_DIR_RISING: 779 ret = vcnl4000_write_data(data, VCNL4010_HIGH_THR_HI, 780 val); 781 if (ret < 0) 782 return ret; 783 return IIO_VAL_INT; 784 case IIO_EV_DIR_FALLING: 785 ret = vcnl4000_write_data(data, VCNL4010_LOW_THR_HI, 786 val); 787 if (ret < 0) 788 return ret; 789 return IIO_VAL_INT; 790 default: 791 return -EINVAL; 792 } 793 default: 794 return -EINVAL; 795 } 796 } 797 798 static bool vcnl4010_is_thr_enabled(struct vcnl4000_data *data) 799 { 800 int ret; 801 802 ret = i2c_smbus_read_byte_data(data->client, VCNL4010_INT_CTRL); 803 if (ret < 0) 804 return false; 805 806 return !!(ret & VCNL4010_INT_THR_EN); 807 } 808 809 static int vcnl4010_read_event_config(struct iio_dev *indio_dev, 810 const struct iio_chan_spec *chan, 811 enum iio_event_type type, 812 enum iio_event_direction dir) 813 { 814 struct vcnl4000_data *data = iio_priv(indio_dev); 815 816 switch (chan->type) { 817 case IIO_PROXIMITY: 818 return vcnl4010_is_thr_enabled(data); 819 default: 820 return -EINVAL; 821 } 822 } 823 824 static int vcnl4010_config_threshold(struct iio_dev *indio_dev, bool state) 825 { 826 struct vcnl4000_data *data = iio_priv(indio_dev); 827 int ret; 828 int icr; 829 int command; 830 831 if (state) { 832 ret = iio_device_claim_direct_mode(indio_dev); 833 if (ret) 834 return ret; 835 836 /* Enable periodic measurement of proximity data. */ 837 command = VCNL4000_SELF_TIMED_EN | VCNL4000_PROX_EN; 838 839 /* 840 * Enable interrupts on threshold, for proximity data by 841 * default. 842 */ 843 icr = VCNL4010_INT_THR_EN; 844 } else { 845 if (!vcnl4010_is_thr_enabled(data)) 846 return 0; 847 848 command = 0; 849 icr = 0; 850 } 851 852 ret = i2c_smbus_write_byte_data(data->client, VCNL4000_COMMAND, 853 command); 854 if (ret < 0) 855 goto end; 856 857 ret = i2c_smbus_write_byte_data(data->client, VCNL4010_INT_CTRL, icr); 858 859 end: 860 if (state) 861 iio_device_release_direct_mode(indio_dev); 862 863 return ret; 864 } 865 866 static int vcnl4010_write_event_config(struct iio_dev *indio_dev, 867 const struct iio_chan_spec *chan, 868 enum iio_event_type type, 869 enum iio_event_direction dir, 870 int state) 871 { 872 switch (chan->type) { 873 case IIO_PROXIMITY: 874 return vcnl4010_config_threshold(indio_dev, state); 875 default: 876 return -EINVAL; 877 } 878 } 879 880 static ssize_t vcnl4000_read_near_level(struct iio_dev *indio_dev, 881 uintptr_t priv, 882 const struct iio_chan_spec *chan, 883 char *buf) 884 { 885 struct vcnl4000_data *data = iio_priv(indio_dev); 886 887 return sprintf(buf, "%u\n", data->near_level); 888 } 889 890 static const struct iio_chan_spec_ext_info vcnl4000_ext_info[] = { 891 { 892 .name = "nearlevel", 893 .shared = IIO_SEPARATE, 894 .read = vcnl4000_read_near_level, 895 }, 896 { /* sentinel */ } 897 }; 898 899 static const struct iio_event_spec vcnl4000_event_spec[] = { 900 { 901 .type = IIO_EV_TYPE_THRESH, 902 .dir = IIO_EV_DIR_RISING, 903 .mask_separate = BIT(IIO_EV_INFO_VALUE), 904 }, { 905 .type = IIO_EV_TYPE_THRESH, 906 .dir = IIO_EV_DIR_FALLING, 907 .mask_separate = BIT(IIO_EV_INFO_VALUE), 908 }, { 909 .type = IIO_EV_TYPE_THRESH, 910 .dir = IIO_EV_DIR_EITHER, 911 .mask_separate = BIT(IIO_EV_INFO_ENABLE), 912 } 913 }; 914 915 static const struct iio_chan_spec vcnl4000_channels[] = { 916 { 917 .type = IIO_LIGHT, 918 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 919 BIT(IIO_CHAN_INFO_SCALE), 920 }, { 921 .type = IIO_PROXIMITY, 922 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 923 .ext_info = vcnl4000_ext_info, 924 } 925 }; 926 927 static const struct iio_chan_spec vcnl4010_channels[] = { 928 { 929 .type = IIO_LIGHT, 930 .scan_index = -1, 931 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 932 BIT(IIO_CHAN_INFO_SCALE), 933 }, { 934 .type = IIO_PROXIMITY, 935 .scan_index = 0, 936 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 937 BIT(IIO_CHAN_INFO_SAMP_FREQ), 938 .info_mask_separate_available = BIT(IIO_CHAN_INFO_SAMP_FREQ), 939 .event_spec = vcnl4000_event_spec, 940 .num_event_specs = ARRAY_SIZE(vcnl4000_event_spec), 941 .ext_info = vcnl4000_ext_info, 942 .scan_type = { 943 .sign = 'u', 944 .realbits = 16, 945 .storagebits = 16, 946 .endianness = IIO_CPU, 947 }, 948 }, 949 IIO_CHAN_SOFT_TIMESTAMP(1), 950 }; 951 952 static const struct iio_chan_spec vcnl4040_channels[] = { 953 { 954 .type = IIO_LIGHT, 955 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 956 BIT(IIO_CHAN_INFO_SCALE), 957 }, { 958 .type = IIO_PROXIMITY, 959 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 960 BIT(IIO_CHAN_INFO_INT_TIME), 961 .info_mask_separate_available = BIT(IIO_CHAN_INFO_INT_TIME), 962 .ext_info = vcnl4000_ext_info, 963 } 964 }; 965 966 static const struct iio_info vcnl4000_info = { 967 .read_raw = vcnl4000_read_raw, 968 }; 969 970 static const struct iio_info vcnl4010_info = { 971 .read_raw = vcnl4010_read_raw, 972 .read_avail = vcnl4010_read_avail, 973 .write_raw = vcnl4010_write_raw, 974 .read_event_value = vcnl4010_read_event, 975 .write_event_value = vcnl4010_write_event, 976 .read_event_config = vcnl4010_read_event_config, 977 .write_event_config = vcnl4010_write_event_config, 978 }; 979 980 static const struct iio_info vcnl4040_info = { 981 .read_raw = vcnl4000_read_raw, 982 .write_raw = vcnl4040_write_raw, 983 .read_avail = vcnl4040_read_avail, 984 }; 985 986 static const struct vcnl4000_chip_spec vcnl4000_chip_spec_cfg[] = { 987 [VCNL4000] = { 988 .prod = "VCNL4000", 989 .init = vcnl4000_init, 990 .measure_light = vcnl4000_measure_light, 991 .measure_proximity = vcnl4000_measure_proximity, 992 .set_power_state = vcnl4000_set_power_state, 993 .channels = vcnl4000_channels, 994 .num_channels = ARRAY_SIZE(vcnl4000_channels), 995 .info = &vcnl4000_info, 996 .irq_support = false, 997 }, 998 [VCNL4010] = { 999 .prod = "VCNL4010/4020", 1000 .init = vcnl4000_init, 1001 .measure_light = vcnl4000_measure_light, 1002 .measure_proximity = vcnl4000_measure_proximity, 1003 .set_power_state = vcnl4000_set_power_state, 1004 .channels = vcnl4010_channels, 1005 .num_channels = ARRAY_SIZE(vcnl4010_channels), 1006 .info = &vcnl4010_info, 1007 .irq_support = true, 1008 }, 1009 [VCNL4040] = { 1010 .prod = "VCNL4040", 1011 .init = vcnl4200_init, 1012 .measure_light = vcnl4200_measure_light, 1013 .measure_proximity = vcnl4200_measure_proximity, 1014 .set_power_state = vcnl4200_set_power_state, 1015 .channels = vcnl4040_channels, 1016 .num_channels = ARRAY_SIZE(vcnl4040_channels), 1017 .info = &vcnl4040_info, 1018 .irq_support = false, 1019 }, 1020 [VCNL4200] = { 1021 .prod = "VCNL4200", 1022 .init = vcnl4200_init, 1023 .measure_light = vcnl4200_measure_light, 1024 .measure_proximity = vcnl4200_measure_proximity, 1025 .set_power_state = vcnl4200_set_power_state, 1026 .channels = vcnl4000_channels, 1027 .num_channels = ARRAY_SIZE(vcnl4000_channels), 1028 .info = &vcnl4000_info, 1029 .irq_support = false, 1030 }, 1031 }; 1032 1033 static irqreturn_t vcnl4010_irq_thread(int irq, void *p) 1034 { 1035 struct iio_dev *indio_dev = p; 1036 struct vcnl4000_data *data = iio_priv(indio_dev); 1037 unsigned long isr; 1038 int ret; 1039 1040 ret = i2c_smbus_read_byte_data(data->client, VCNL4010_ISR); 1041 if (ret < 0) 1042 goto end; 1043 1044 isr = ret; 1045 1046 if (isr & VCNL4010_INT_THR) { 1047 if (test_bit(VCNL4010_INT_THR_LOW, &isr)) { 1048 iio_push_event(indio_dev, 1049 IIO_UNMOD_EVENT_CODE( 1050 IIO_PROXIMITY, 1051 1, 1052 IIO_EV_TYPE_THRESH, 1053 IIO_EV_DIR_FALLING), 1054 iio_get_time_ns(indio_dev)); 1055 } 1056 1057 if (test_bit(VCNL4010_INT_THR_HIGH, &isr)) { 1058 iio_push_event(indio_dev, 1059 IIO_UNMOD_EVENT_CODE( 1060 IIO_PROXIMITY, 1061 1, 1062 IIO_EV_TYPE_THRESH, 1063 IIO_EV_DIR_RISING), 1064 iio_get_time_ns(indio_dev)); 1065 } 1066 1067 i2c_smbus_write_byte_data(data->client, VCNL4010_ISR, 1068 isr & VCNL4010_INT_THR); 1069 } 1070 1071 if (isr & VCNL4010_INT_DRDY && iio_buffer_enabled(indio_dev)) 1072 iio_trigger_poll_chained(indio_dev->trig); 1073 1074 end: 1075 return IRQ_HANDLED; 1076 } 1077 1078 static irqreturn_t vcnl4010_trigger_handler(int irq, void *p) 1079 { 1080 struct iio_poll_func *pf = p; 1081 struct iio_dev *indio_dev = pf->indio_dev; 1082 struct vcnl4000_data *data = iio_priv(indio_dev); 1083 const unsigned long *active_scan_mask = indio_dev->active_scan_mask; 1084 u16 buffer[8] __aligned(8) = {0}; /* 1x16-bit + naturally aligned ts */ 1085 bool data_read = false; 1086 unsigned long isr; 1087 int val = 0; 1088 int ret; 1089 1090 ret = i2c_smbus_read_byte_data(data->client, VCNL4010_ISR); 1091 if (ret < 0) 1092 goto end; 1093 1094 isr = ret; 1095 1096 if (test_bit(0, active_scan_mask)) { 1097 if (test_bit(VCNL4010_INT_PROXIMITY, &isr)) { 1098 ret = vcnl4000_read_data(data, 1099 VCNL4000_PS_RESULT_HI, 1100 &val); 1101 if (ret < 0) 1102 goto end; 1103 1104 buffer[0] = val; 1105 data_read = true; 1106 } 1107 } 1108 1109 ret = i2c_smbus_write_byte_data(data->client, VCNL4010_ISR, 1110 isr & VCNL4010_INT_DRDY); 1111 if (ret < 0) 1112 goto end; 1113 1114 if (!data_read) 1115 goto end; 1116 1117 iio_push_to_buffers_with_timestamp(indio_dev, buffer, 1118 iio_get_time_ns(indio_dev)); 1119 1120 end: 1121 iio_trigger_notify_done(indio_dev->trig); 1122 return IRQ_HANDLED; 1123 } 1124 1125 static int vcnl4010_buffer_postenable(struct iio_dev *indio_dev) 1126 { 1127 struct vcnl4000_data *data = iio_priv(indio_dev); 1128 int ret; 1129 int cmd; 1130 1131 /* Do not enable the buffer if we are already capturing events. */ 1132 if (vcnl4010_is_in_periodic_mode(data)) 1133 return -EBUSY; 1134 1135 ret = i2c_smbus_write_byte_data(data->client, VCNL4010_INT_CTRL, 1136 VCNL4010_INT_PROX_EN); 1137 if (ret < 0) 1138 return ret; 1139 1140 cmd = VCNL4000_SELF_TIMED_EN | VCNL4000_PROX_EN; 1141 return i2c_smbus_write_byte_data(data->client, VCNL4000_COMMAND, cmd); 1142 } 1143 1144 static int vcnl4010_buffer_predisable(struct iio_dev *indio_dev) 1145 { 1146 struct vcnl4000_data *data = iio_priv(indio_dev); 1147 int ret; 1148 1149 ret = i2c_smbus_write_byte_data(data->client, VCNL4010_INT_CTRL, 0); 1150 if (ret < 0) 1151 return ret; 1152 1153 return i2c_smbus_write_byte_data(data->client, VCNL4000_COMMAND, 0); 1154 } 1155 1156 static const struct iio_buffer_setup_ops vcnl4010_buffer_ops = { 1157 .postenable = &vcnl4010_buffer_postenable, 1158 .predisable = &vcnl4010_buffer_predisable, 1159 }; 1160 1161 static const struct iio_trigger_ops vcnl4010_trigger_ops = { 1162 .validate_device = iio_trigger_validate_own_device, 1163 }; 1164 1165 static int vcnl4010_probe_trigger(struct iio_dev *indio_dev) 1166 { 1167 struct vcnl4000_data *data = iio_priv(indio_dev); 1168 struct i2c_client *client = data->client; 1169 struct iio_trigger *trigger; 1170 1171 trigger = devm_iio_trigger_alloc(&client->dev, "%s-dev%d", 1172 indio_dev->name, 1173 iio_device_id(indio_dev)); 1174 if (!trigger) 1175 return -ENOMEM; 1176 1177 trigger->ops = &vcnl4010_trigger_ops; 1178 iio_trigger_set_drvdata(trigger, indio_dev); 1179 1180 return devm_iio_trigger_register(&client->dev, trigger); 1181 } 1182 1183 static int vcnl4000_probe(struct i2c_client *client) 1184 { 1185 const struct i2c_device_id *id = i2c_client_get_device_id(client); 1186 struct vcnl4000_data *data; 1187 struct iio_dev *indio_dev; 1188 int ret; 1189 1190 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 1191 if (!indio_dev) 1192 return -ENOMEM; 1193 1194 data = iio_priv(indio_dev); 1195 i2c_set_clientdata(client, indio_dev); 1196 data->client = client; 1197 data->id = id->driver_data; 1198 data->chip_spec = &vcnl4000_chip_spec_cfg[data->id]; 1199 1200 ret = data->chip_spec->init(data); 1201 if (ret < 0) 1202 return ret; 1203 1204 dev_dbg(&client->dev, "%s Ambient light/proximity sensor, Rev: %02x\n", 1205 data->chip_spec->prod, data->rev); 1206 1207 if (device_property_read_u32(&client->dev, "proximity-near-level", 1208 &data->near_level)) 1209 data->near_level = 0; 1210 1211 indio_dev->info = data->chip_spec->info; 1212 indio_dev->channels = data->chip_spec->channels; 1213 indio_dev->num_channels = data->chip_spec->num_channels; 1214 indio_dev->name = VCNL4000_DRV_NAME; 1215 indio_dev->modes = INDIO_DIRECT_MODE; 1216 1217 if (client->irq && data->chip_spec->irq_support) { 1218 ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev, 1219 NULL, 1220 vcnl4010_trigger_handler, 1221 &vcnl4010_buffer_ops); 1222 if (ret < 0) { 1223 dev_err(&client->dev, 1224 "unable to setup iio triggered buffer\n"); 1225 return ret; 1226 } 1227 1228 ret = devm_request_threaded_irq(&client->dev, client->irq, 1229 NULL, vcnl4010_irq_thread, 1230 IRQF_TRIGGER_FALLING | 1231 IRQF_ONESHOT, 1232 "vcnl4010_irq", 1233 indio_dev); 1234 if (ret < 0) { 1235 dev_err(&client->dev, "irq request failed\n"); 1236 return ret; 1237 } 1238 1239 ret = vcnl4010_probe_trigger(indio_dev); 1240 if (ret < 0) 1241 return ret; 1242 } 1243 1244 ret = pm_runtime_set_active(&client->dev); 1245 if (ret < 0) 1246 goto fail_poweroff; 1247 1248 ret = iio_device_register(indio_dev); 1249 if (ret < 0) 1250 goto fail_poweroff; 1251 1252 pm_runtime_enable(&client->dev); 1253 pm_runtime_set_autosuspend_delay(&client->dev, VCNL4000_SLEEP_DELAY_MS); 1254 pm_runtime_use_autosuspend(&client->dev); 1255 1256 return 0; 1257 fail_poweroff: 1258 data->chip_spec->set_power_state(data, false); 1259 return ret; 1260 } 1261 1262 static const struct of_device_id vcnl_4000_of_match[] = { 1263 { 1264 .compatible = "vishay,vcnl4000", 1265 .data = (void *)VCNL4000, 1266 }, 1267 { 1268 .compatible = "vishay,vcnl4010", 1269 .data = (void *)VCNL4010, 1270 }, 1271 { 1272 .compatible = "vishay,vcnl4020", 1273 .data = (void *)VCNL4010, 1274 }, 1275 { 1276 .compatible = "vishay,vcnl4040", 1277 .data = (void *)VCNL4040, 1278 }, 1279 { 1280 .compatible = "vishay,vcnl4200", 1281 .data = (void *)VCNL4200, 1282 }, 1283 {}, 1284 }; 1285 MODULE_DEVICE_TABLE(of, vcnl_4000_of_match); 1286 1287 static void vcnl4000_remove(struct i2c_client *client) 1288 { 1289 struct iio_dev *indio_dev = i2c_get_clientdata(client); 1290 struct vcnl4000_data *data = iio_priv(indio_dev); 1291 int ret; 1292 1293 pm_runtime_dont_use_autosuspend(&client->dev); 1294 pm_runtime_disable(&client->dev); 1295 iio_device_unregister(indio_dev); 1296 pm_runtime_set_suspended(&client->dev); 1297 1298 ret = data->chip_spec->set_power_state(data, false); 1299 if (ret) 1300 dev_warn(&client->dev, "Failed to power down (%pe)\n", 1301 ERR_PTR(ret)); 1302 } 1303 1304 static int vcnl4000_runtime_suspend(struct device *dev) 1305 { 1306 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 1307 struct vcnl4000_data *data = iio_priv(indio_dev); 1308 1309 return data->chip_spec->set_power_state(data, false); 1310 } 1311 1312 static int vcnl4000_runtime_resume(struct device *dev) 1313 { 1314 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 1315 struct vcnl4000_data *data = iio_priv(indio_dev); 1316 1317 return data->chip_spec->set_power_state(data, true); 1318 } 1319 1320 static DEFINE_RUNTIME_DEV_PM_OPS(vcnl4000_pm_ops, vcnl4000_runtime_suspend, 1321 vcnl4000_runtime_resume, NULL); 1322 1323 static struct i2c_driver vcnl4000_driver = { 1324 .driver = { 1325 .name = VCNL4000_DRV_NAME, 1326 .pm = pm_ptr(&vcnl4000_pm_ops), 1327 .of_match_table = vcnl_4000_of_match, 1328 }, 1329 .probe_new = vcnl4000_probe, 1330 .id_table = vcnl4000_id, 1331 .remove = vcnl4000_remove, 1332 }; 1333 1334 module_i2c_driver(vcnl4000_driver); 1335 1336 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>"); 1337 MODULE_AUTHOR("Mathieu Othacehe <m.othacehe@gmail.com>"); 1338 MODULE_DESCRIPTION("Vishay VCNL4000 proximity/ambient light sensor driver"); 1339 MODULE_LICENSE("GPL"); 1340