1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * IIO driver for the light sensor ISL29028. 4 * ISL29028 is Concurrent Ambient Light and Proximity Sensor 5 * 6 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved. 7 * Copyright (c) 2016-2017 Brian Masney <masneyb@onstation.org> 8 * 9 * Datasheets: 10 * - http://www.intersil.com/content/dam/Intersil/documents/isl2/isl29028.pdf 11 * - http://www.intersil.com/content/dam/Intersil/documents/isl2/isl29030.pdf 12 */ 13 14 #include <linux/module.h> 15 #include <linux/i2c.h> 16 #include <linux/err.h> 17 #include <linux/mutex.h> 18 #include <linux/delay.h> 19 #include <linux/slab.h> 20 #include <linux/regmap.h> 21 #include <linux/iio/iio.h> 22 #include <linux/iio/sysfs.h> 23 #include <linux/pm_runtime.h> 24 25 #define ISL29028_CONV_TIME_MS 100 26 27 #define ISL29028_REG_CONFIGURE 0x01 28 29 #define ISL29028_CONF_ALS_IR_MODE_ALS 0 30 #define ISL29028_CONF_ALS_IR_MODE_IR BIT(0) 31 #define ISL29028_CONF_ALS_IR_MODE_MASK BIT(0) 32 33 #define ISL29028_CONF_ALS_RANGE_LOW_LUX 0 34 #define ISL29028_CONF_ALS_RANGE_HIGH_LUX BIT(1) 35 #define ISL29028_CONF_ALS_RANGE_MASK BIT(1) 36 37 #define ISL29028_CONF_ALS_DIS 0 38 #define ISL29028_CONF_ALS_EN BIT(2) 39 #define ISL29028_CONF_ALS_EN_MASK BIT(2) 40 41 #define ISL29028_CONF_PROX_SLP_SH 4 42 #define ISL29028_CONF_PROX_SLP_MASK (7 << ISL29028_CONF_PROX_SLP_SH) 43 44 #define ISL29028_CONF_PROX_EN BIT(7) 45 #define ISL29028_CONF_PROX_EN_MASK BIT(7) 46 47 #define ISL29028_REG_INTERRUPT 0x02 48 49 #define ISL29028_REG_PROX_DATA 0x08 50 #define ISL29028_REG_ALSIR_L 0x09 51 #define ISL29028_REG_ALSIR_U 0x0A 52 53 #define ISL29028_REG_TEST1_MODE 0x0E 54 #define ISL29028_REG_TEST2_MODE 0x0F 55 56 #define ISL29028_NUM_REGS (ISL29028_REG_TEST2_MODE + 1) 57 58 #define ISL29028_POWER_OFF_DELAY_MS 2000 59 60 struct isl29028_prox_data { 61 int sampling_int; 62 int sampling_fract; 63 int sleep_time; 64 }; 65 66 static const struct isl29028_prox_data isl29028_prox_data[] = { 67 { 1, 250000, 800 }, 68 { 2, 500000, 400 }, 69 { 5, 0, 200 }, 70 { 10, 0, 100 }, 71 { 13, 300000, 75 }, 72 { 20, 0, 50 }, 73 { 80, 0, 13 }, /* 74 * Note: Data sheet lists 12.5 ms sleep time. 75 * Round up a half millisecond for msleep(). 76 */ 77 { 100, 0, 0 } 78 }; 79 80 enum isl29028_als_ir_mode { 81 ISL29028_MODE_NONE = 0, 82 ISL29028_MODE_ALS, 83 ISL29028_MODE_IR, 84 }; 85 86 struct isl29028_chip { 87 struct mutex lock; 88 struct regmap *regmap; 89 int prox_sampling_int; 90 int prox_sampling_frac; 91 bool enable_prox; 92 int lux_scale; 93 enum isl29028_als_ir_mode als_ir_mode; 94 }; 95 96 static int isl29028_find_prox_sleep_index(int sampling_int, int sampling_fract) 97 { 98 int i; 99 100 for (i = 0; i < ARRAY_SIZE(isl29028_prox_data); ++i) { 101 if (isl29028_prox_data[i].sampling_int == sampling_int && 102 isl29028_prox_data[i].sampling_fract == sampling_fract) 103 return i; 104 } 105 106 return -EINVAL; 107 } 108 109 static int isl29028_set_proxim_sampling(struct isl29028_chip *chip, 110 int sampling_int, int sampling_fract) 111 { 112 struct device *dev = regmap_get_device(chip->regmap); 113 int sleep_index, ret; 114 115 sleep_index = isl29028_find_prox_sleep_index(sampling_int, 116 sampling_fract); 117 if (sleep_index < 0) 118 return sleep_index; 119 120 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE, 121 ISL29028_CONF_PROX_SLP_MASK, 122 sleep_index << ISL29028_CONF_PROX_SLP_SH); 123 124 if (ret < 0) { 125 dev_err(dev, "%s(): Error %d setting the proximity sampling\n", 126 __func__, ret); 127 return ret; 128 } 129 130 chip->prox_sampling_int = sampling_int; 131 chip->prox_sampling_frac = sampling_fract; 132 133 return ret; 134 } 135 136 static int isl29028_enable_proximity(struct isl29028_chip *chip) 137 { 138 int prox_index, ret; 139 140 ret = isl29028_set_proxim_sampling(chip, chip->prox_sampling_int, 141 chip->prox_sampling_frac); 142 if (ret < 0) 143 return ret; 144 145 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE, 146 ISL29028_CONF_PROX_EN_MASK, 147 ISL29028_CONF_PROX_EN); 148 if (ret < 0) 149 return ret; 150 151 /* Wait for conversion to be complete for first sample */ 152 prox_index = isl29028_find_prox_sleep_index(chip->prox_sampling_int, 153 chip->prox_sampling_frac); 154 if (prox_index < 0) 155 return prox_index; 156 157 msleep(isl29028_prox_data[prox_index].sleep_time); 158 159 return 0; 160 } 161 162 static int isl29028_set_als_scale(struct isl29028_chip *chip, int lux_scale) 163 { 164 struct device *dev = regmap_get_device(chip->regmap); 165 int val = (lux_scale == 2000) ? ISL29028_CONF_ALS_RANGE_HIGH_LUX : 166 ISL29028_CONF_ALS_RANGE_LOW_LUX; 167 int ret; 168 169 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE, 170 ISL29028_CONF_ALS_RANGE_MASK, val); 171 if (ret < 0) { 172 dev_err(dev, "%s(): Error %d setting the ALS scale\n", __func__, 173 ret); 174 return ret; 175 } 176 177 chip->lux_scale = lux_scale; 178 179 return ret; 180 } 181 182 static int isl29028_set_als_ir_mode(struct isl29028_chip *chip, 183 enum isl29028_als_ir_mode mode) 184 { 185 int ret; 186 187 if (chip->als_ir_mode == mode) 188 return 0; 189 190 ret = isl29028_set_als_scale(chip, chip->lux_scale); 191 if (ret < 0) 192 return ret; 193 194 switch (mode) { 195 case ISL29028_MODE_ALS: 196 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE, 197 ISL29028_CONF_ALS_IR_MODE_MASK, 198 ISL29028_CONF_ALS_IR_MODE_ALS); 199 if (ret < 0) 200 return ret; 201 202 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE, 203 ISL29028_CONF_ALS_RANGE_MASK, 204 ISL29028_CONF_ALS_RANGE_HIGH_LUX); 205 break; 206 case ISL29028_MODE_IR: 207 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE, 208 ISL29028_CONF_ALS_IR_MODE_MASK, 209 ISL29028_CONF_ALS_IR_MODE_IR); 210 break; 211 case ISL29028_MODE_NONE: 212 return regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE, 213 ISL29028_CONF_ALS_EN_MASK, 214 ISL29028_CONF_ALS_DIS); 215 } 216 217 if (ret < 0) 218 return ret; 219 220 /* Enable the ALS/IR */ 221 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE, 222 ISL29028_CONF_ALS_EN_MASK, 223 ISL29028_CONF_ALS_EN); 224 if (ret < 0) 225 return ret; 226 227 /* Need to wait for conversion time if ALS/IR mode enabled */ 228 msleep(ISL29028_CONV_TIME_MS); 229 230 chip->als_ir_mode = mode; 231 232 return 0; 233 } 234 235 static int isl29028_read_als_ir(struct isl29028_chip *chip, int *als_ir) 236 { 237 struct device *dev = regmap_get_device(chip->regmap); 238 unsigned int lsb; 239 unsigned int msb; 240 int ret; 241 242 ret = regmap_read(chip->regmap, ISL29028_REG_ALSIR_L, &lsb); 243 if (ret < 0) { 244 dev_err(dev, 245 "%s(): Error %d reading register ALSIR_L\n", 246 __func__, ret); 247 return ret; 248 } 249 250 ret = regmap_read(chip->regmap, ISL29028_REG_ALSIR_U, &msb); 251 if (ret < 0) { 252 dev_err(dev, 253 "%s(): Error %d reading register ALSIR_U\n", 254 __func__, ret); 255 return ret; 256 } 257 258 *als_ir = ((msb & 0xF) << 8) | (lsb & 0xFF); 259 260 return 0; 261 } 262 263 static int isl29028_read_proxim(struct isl29028_chip *chip, int *prox) 264 { 265 struct device *dev = regmap_get_device(chip->regmap); 266 unsigned int data; 267 int ret; 268 269 if (!chip->enable_prox) { 270 ret = isl29028_enable_proximity(chip); 271 if (ret < 0) 272 return ret; 273 274 chip->enable_prox = true; 275 } 276 277 ret = regmap_read(chip->regmap, ISL29028_REG_PROX_DATA, &data); 278 if (ret < 0) { 279 dev_err(dev, "%s(): Error %d reading register PROX_DATA\n", 280 __func__, ret); 281 return ret; 282 } 283 284 *prox = data; 285 286 return 0; 287 } 288 289 static int isl29028_als_get(struct isl29028_chip *chip, int *als_data) 290 { 291 struct device *dev = regmap_get_device(chip->regmap); 292 int ret; 293 int als_ir_data; 294 295 ret = isl29028_set_als_ir_mode(chip, ISL29028_MODE_ALS); 296 if (ret < 0) { 297 dev_err(dev, "%s(): Error %d enabling ALS mode\n", __func__, 298 ret); 299 return ret; 300 } 301 302 ret = isl29028_read_als_ir(chip, &als_ir_data); 303 if (ret < 0) 304 return ret; 305 306 /* 307 * convert als data count to lux. 308 * if lux_scale = 125, lux = count * 0.031 309 * if lux_scale = 2000, lux = count * 0.49 310 */ 311 if (chip->lux_scale == 125) 312 als_ir_data = (als_ir_data * 31) / 1000; 313 else 314 als_ir_data = (als_ir_data * 49) / 100; 315 316 *als_data = als_ir_data; 317 318 return 0; 319 } 320 321 static int isl29028_ir_get(struct isl29028_chip *chip, int *ir_data) 322 { 323 struct device *dev = regmap_get_device(chip->regmap); 324 int ret; 325 326 ret = isl29028_set_als_ir_mode(chip, ISL29028_MODE_IR); 327 if (ret < 0) { 328 dev_err(dev, "%s(): Error %d enabling IR mode\n", __func__, 329 ret); 330 return ret; 331 } 332 333 return isl29028_read_als_ir(chip, ir_data); 334 } 335 336 /* Channel IO */ 337 static int isl29028_write_raw(struct iio_dev *indio_dev, 338 struct iio_chan_spec const *chan, 339 int val, int val2, long mask) 340 { 341 struct isl29028_chip *chip = iio_priv(indio_dev); 342 struct device *dev = regmap_get_device(chip->regmap); 343 int ret; 344 345 ret = pm_runtime_resume_and_get(dev); 346 if (ret < 0) 347 return ret; 348 349 mutex_lock(&chip->lock); 350 351 ret = -EINVAL; 352 switch (chan->type) { 353 case IIO_PROXIMITY: 354 if (mask != IIO_CHAN_INFO_SAMP_FREQ) { 355 dev_err(dev, 356 "%s(): proximity: Mask value 0x%08lx is not supported\n", 357 __func__, mask); 358 break; 359 } 360 361 if (val < 1 || val > 100) { 362 dev_err(dev, 363 "%s(): proximity: Sampling frequency %d is not in the range [1:100]\n", 364 __func__, val); 365 break; 366 } 367 368 ret = isl29028_set_proxim_sampling(chip, val, val2); 369 break; 370 case IIO_LIGHT: 371 if (mask != IIO_CHAN_INFO_SCALE) { 372 dev_err(dev, 373 "%s(): light: Mask value 0x%08lx is not supported\n", 374 __func__, mask); 375 break; 376 } 377 378 if (val != 125 && val != 2000) { 379 dev_err(dev, 380 "%s(): light: Lux scale %d is not in the set {125, 2000}\n", 381 __func__, val); 382 break; 383 } 384 385 ret = isl29028_set_als_scale(chip, val); 386 break; 387 default: 388 dev_err(dev, "%s(): Unsupported channel type %x\n", 389 __func__, chan->type); 390 break; 391 } 392 393 mutex_unlock(&chip->lock); 394 395 if (ret < 0) 396 return ret; 397 398 ret = pm_runtime_put_autosuspend(dev); 399 if (ret < 0) 400 return ret; 401 402 return 0; 403 } 404 405 static int isl29028_read_raw(struct iio_dev *indio_dev, 406 struct iio_chan_spec const *chan, 407 int *val, int *val2, long mask) 408 { 409 struct isl29028_chip *chip = iio_priv(indio_dev); 410 struct device *dev = regmap_get_device(chip->regmap); 411 int ret, pm_ret; 412 413 ret = pm_runtime_resume_and_get(dev); 414 if (ret < 0) 415 return ret; 416 417 mutex_lock(&chip->lock); 418 419 ret = -EINVAL; 420 switch (mask) { 421 case IIO_CHAN_INFO_RAW: 422 case IIO_CHAN_INFO_PROCESSED: 423 switch (chan->type) { 424 case IIO_LIGHT: 425 ret = isl29028_als_get(chip, val); 426 break; 427 case IIO_INTENSITY: 428 ret = isl29028_ir_get(chip, val); 429 break; 430 case IIO_PROXIMITY: 431 ret = isl29028_read_proxim(chip, val); 432 break; 433 default: 434 break; 435 } 436 437 if (ret < 0) 438 break; 439 440 ret = IIO_VAL_INT; 441 break; 442 case IIO_CHAN_INFO_SAMP_FREQ: 443 if (chan->type != IIO_PROXIMITY) 444 break; 445 446 *val = chip->prox_sampling_int; 447 *val2 = chip->prox_sampling_frac; 448 ret = IIO_VAL_INT; 449 break; 450 case IIO_CHAN_INFO_SCALE: 451 if (chan->type != IIO_LIGHT) 452 break; 453 *val = chip->lux_scale; 454 ret = IIO_VAL_INT; 455 break; 456 default: 457 dev_err(dev, "%s(): mask value 0x%08lx is not supported\n", 458 __func__, mask); 459 break; 460 } 461 462 mutex_unlock(&chip->lock); 463 464 if (ret < 0) 465 return ret; 466 467 /** 468 * Preserve the ret variable if the call to 469 * pm_runtime_put_autosuspend() is successful so the reading 470 * (if applicable) is returned to user space. 471 */ 472 pm_ret = pm_runtime_put_autosuspend(dev); 473 if (pm_ret < 0) 474 return pm_ret; 475 476 return ret; 477 } 478 479 static IIO_CONST_ATTR(in_proximity_sampling_frequency_available, 480 "1.25 2.5 5 10 13.3 20 80 100"); 481 static IIO_CONST_ATTR(in_illuminance_scale_available, "125 2000"); 482 483 #define ISL29028_CONST_ATTR(name) (&iio_const_attr_##name.dev_attr.attr) 484 static struct attribute *isl29028_attributes[] = { 485 ISL29028_CONST_ATTR(in_proximity_sampling_frequency_available), 486 ISL29028_CONST_ATTR(in_illuminance_scale_available), 487 NULL, 488 }; 489 490 static const struct attribute_group isl29108_group = { 491 .attrs = isl29028_attributes, 492 }; 493 494 static const struct iio_chan_spec isl29028_channels[] = { 495 { 496 .type = IIO_LIGHT, 497 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | 498 BIT(IIO_CHAN_INFO_SCALE), 499 }, { 500 .type = IIO_INTENSITY, 501 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 502 }, { 503 .type = IIO_PROXIMITY, 504 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 505 BIT(IIO_CHAN_INFO_SAMP_FREQ), 506 } 507 }; 508 509 static const struct iio_info isl29028_info = { 510 .attrs = &isl29108_group, 511 .read_raw = isl29028_read_raw, 512 .write_raw = isl29028_write_raw, 513 }; 514 515 static int isl29028_clear_configure_reg(struct isl29028_chip *chip) 516 { 517 struct device *dev = regmap_get_device(chip->regmap); 518 int ret; 519 520 ret = regmap_write(chip->regmap, ISL29028_REG_CONFIGURE, 0x0); 521 if (ret < 0) 522 dev_err(dev, "%s(): Error %d clearing the CONFIGURE register\n", 523 __func__, ret); 524 525 chip->als_ir_mode = ISL29028_MODE_NONE; 526 chip->enable_prox = false; 527 528 return ret; 529 } 530 531 static bool isl29028_is_volatile_reg(struct device *dev, unsigned int reg) 532 { 533 switch (reg) { 534 case ISL29028_REG_INTERRUPT: 535 case ISL29028_REG_PROX_DATA: 536 case ISL29028_REG_ALSIR_L: 537 case ISL29028_REG_ALSIR_U: 538 return true; 539 default: 540 return false; 541 } 542 } 543 544 static const struct regmap_config isl29028_regmap_config = { 545 .reg_bits = 8, 546 .val_bits = 8, 547 .volatile_reg = isl29028_is_volatile_reg, 548 .max_register = ISL29028_NUM_REGS - 1, 549 .num_reg_defaults_raw = ISL29028_NUM_REGS, 550 .cache_type = REGCACHE_MAPLE, 551 }; 552 553 static int isl29028_probe(struct i2c_client *client) 554 { 555 const struct i2c_device_id *id = i2c_client_get_device_id(client); 556 struct isl29028_chip *chip; 557 struct iio_dev *indio_dev; 558 int ret; 559 560 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip)); 561 if (!indio_dev) 562 return -ENOMEM; 563 564 chip = iio_priv(indio_dev); 565 566 i2c_set_clientdata(client, indio_dev); 567 mutex_init(&chip->lock); 568 569 chip->regmap = devm_regmap_init_i2c(client, &isl29028_regmap_config); 570 if (IS_ERR(chip->regmap)) { 571 ret = PTR_ERR(chip->regmap); 572 dev_err(&client->dev, "%s: Error %d initializing regmap\n", 573 __func__, ret); 574 return ret; 575 } 576 577 chip->enable_prox = false; 578 chip->prox_sampling_int = 20; 579 chip->prox_sampling_frac = 0; 580 chip->lux_scale = 2000; 581 582 ret = regmap_write(chip->regmap, ISL29028_REG_TEST1_MODE, 0x0); 583 if (ret < 0) { 584 dev_err(&client->dev, 585 "%s(): Error %d writing to TEST1_MODE register\n", 586 __func__, ret); 587 return ret; 588 } 589 590 ret = regmap_write(chip->regmap, ISL29028_REG_TEST2_MODE, 0x0); 591 if (ret < 0) { 592 dev_err(&client->dev, 593 "%s(): Error %d writing to TEST2_MODE register\n", 594 __func__, ret); 595 return ret; 596 } 597 598 ret = isl29028_clear_configure_reg(chip); 599 if (ret < 0) 600 return ret; 601 602 indio_dev->info = &isl29028_info; 603 indio_dev->channels = isl29028_channels; 604 indio_dev->num_channels = ARRAY_SIZE(isl29028_channels); 605 indio_dev->name = id->name; 606 indio_dev->modes = INDIO_DIRECT_MODE; 607 608 pm_runtime_enable(&client->dev); 609 pm_runtime_set_autosuspend_delay(&client->dev, 610 ISL29028_POWER_OFF_DELAY_MS); 611 pm_runtime_use_autosuspend(&client->dev); 612 613 ret = iio_device_register(indio_dev); 614 if (ret < 0) { 615 dev_err(&client->dev, 616 "%s(): iio registration failed with error %d\n", 617 __func__, ret); 618 return ret; 619 } 620 621 return 0; 622 } 623 624 static void isl29028_remove(struct i2c_client *client) 625 { 626 struct iio_dev *indio_dev = i2c_get_clientdata(client); 627 struct isl29028_chip *chip = iio_priv(indio_dev); 628 629 iio_device_unregister(indio_dev); 630 631 pm_runtime_disable(&client->dev); 632 pm_runtime_set_suspended(&client->dev); 633 634 isl29028_clear_configure_reg(chip); 635 } 636 637 static int isl29028_suspend(struct device *dev) 638 { 639 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 640 struct isl29028_chip *chip = iio_priv(indio_dev); 641 int ret; 642 643 mutex_lock(&chip->lock); 644 645 ret = isl29028_clear_configure_reg(chip); 646 647 mutex_unlock(&chip->lock); 648 649 return ret; 650 } 651 652 static int isl29028_resume(struct device *dev) 653 { 654 /** 655 * The specific component (ALS/IR or proximity) will enable itself as 656 * needed the next time that the user requests a reading. This is done 657 * above in isl29028_set_als_ir_mode() and isl29028_enable_proximity(). 658 */ 659 return 0; 660 } 661 662 static DEFINE_RUNTIME_DEV_PM_OPS(isl29028_pm_ops, isl29028_suspend, 663 isl29028_resume, NULL); 664 665 static const struct i2c_device_id isl29028_id[] = { 666 { .name = "isl29028" }, 667 { .name = "isl29030" }, 668 { } 669 }; 670 MODULE_DEVICE_TABLE(i2c, isl29028_id); 671 672 static const struct of_device_id isl29028_of_match[] = { 673 { .compatible = "isl,isl29028", }, /* for backward compat., don't use */ 674 { .compatible = "isil,isl29028", }, 675 { .compatible = "isil,isl29030", }, 676 { } 677 }; 678 MODULE_DEVICE_TABLE(of, isl29028_of_match); 679 680 static struct i2c_driver isl29028_driver = { 681 .driver = { 682 .name = "isl29028", 683 .pm = pm_ptr(&isl29028_pm_ops), 684 .of_match_table = isl29028_of_match, 685 }, 686 .probe = isl29028_probe, 687 .remove = isl29028_remove, 688 .id_table = isl29028_id, 689 }; 690 691 module_i2c_driver(isl29028_driver); 692 693 MODULE_DESCRIPTION("ISL29028 Ambient Light and Proximity Sensor driver"); 694 MODULE_LICENSE("GPL v2"); 695 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>"); 696