1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * A iio driver for the light sensor ISL 29018/29023/29035. 4 * 5 * IIO driver for monitoring ambient light intensity in luxi, proximity 6 * sensing and infrared sensing. 7 * 8 * Copyright (c) 2010, NVIDIA Corporation. 9 */ 10 11 #include <linux/i2c.h> 12 #include <linux/err.h> 13 #include <linux/kstrtox.h> 14 #include <linux/module.h> 15 #include <linux/mutex.h> 16 #include <linux/delay.h> 17 #include <linux/regmap.h> 18 #include <linux/regulator/consumer.h> 19 #include <linux/slab.h> 20 21 #include <linux/iio/iio.h> 22 #include <linux/iio/sysfs.h> 23 24 #define ISL29018_CONV_TIME_MS 100 25 26 #define ISL29018_REG_ADD_COMMAND1 0x00 27 #define ISL29018_CMD1_OPMODE_SHIFT 5 28 #define ISL29018_CMD1_OPMODE_MASK (7 << ISL29018_CMD1_OPMODE_SHIFT) 29 #define ISL29018_CMD1_OPMODE_POWER_DOWN 0 30 #define ISL29018_CMD1_OPMODE_ALS_ONCE 1 31 #define ISL29018_CMD1_OPMODE_IR_ONCE 2 32 #define ISL29018_CMD1_OPMODE_PROX_ONCE 3 33 34 #define ISL29018_REG_ADD_COMMAND2 0x01 35 #define ISL29018_CMD2_RESOLUTION_SHIFT 2 36 #define ISL29018_CMD2_RESOLUTION_MASK (0x3 << ISL29018_CMD2_RESOLUTION_SHIFT) 37 38 #define ISL29018_CMD2_RANGE_SHIFT 0 39 #define ISL29018_CMD2_RANGE_MASK (0x3 << ISL29018_CMD2_RANGE_SHIFT) 40 41 #define ISL29018_CMD2_SCHEME_SHIFT 7 42 #define ISL29018_CMD2_SCHEME_MASK (0x1 << ISL29018_CMD2_SCHEME_SHIFT) 43 44 #define ISL29018_REG_ADD_DATA_LSB 0x02 45 #define ISL29018_REG_ADD_DATA_MSB 0x03 46 47 #define ISL29018_REG_TEST 0x08 48 #define ISL29018_TEST_SHIFT 0 49 #define ISL29018_TEST_MASK (0xFF << ISL29018_TEST_SHIFT) 50 51 #define ISL29035_REG_DEVICE_ID 0x0F 52 #define ISL29035_DEVICE_ID_SHIFT 0x03 53 #define ISL29035_DEVICE_ID_MASK (0x7 << ISL29035_DEVICE_ID_SHIFT) 54 #define ISL29035_DEVICE_ID 0x5 55 #define ISL29035_BOUT_SHIFT 0x07 56 #define ISL29035_BOUT_MASK (0x01 << ISL29035_BOUT_SHIFT) 57 58 enum isl29018_int_time { 59 ISL29018_INT_TIME_16, 60 ISL29018_INT_TIME_12, 61 ISL29018_INT_TIME_8, 62 ISL29018_INT_TIME_4, 63 }; 64 65 static const unsigned int isl29018_int_utimes[3][4] = { 66 {90000, 5630, 351, 21}, 67 {90000, 5600, 352, 22}, 68 {105000, 6500, 410, 25}, 69 }; 70 71 static const struct isl29018_scale { 72 unsigned int scale; 73 unsigned int uscale; 74 } isl29018_scales[4][4] = { 75 { {0, 15258}, {0, 61035}, {0, 244140}, {0, 976562} }, 76 { {0, 244140}, {0, 976562}, {3, 906250}, {15, 625000} }, 77 { {3, 906250}, {15, 625000}, {62, 500000}, {250, 0} }, 78 { {62, 500000}, {250, 0}, {1000, 0}, {4000, 0} } 79 }; 80 81 struct isl29018_chip { 82 struct regmap *regmap; 83 struct mutex lock; 84 int type; 85 unsigned int calibscale; 86 unsigned int ucalibscale; 87 unsigned int int_time; 88 struct isl29018_scale scale; 89 int prox_scheme; 90 bool suspended; 91 struct regulator *vcc_reg; 92 }; 93 94 static int isl29018_set_integration_time(struct isl29018_chip *chip, 95 unsigned int utime) 96 { 97 unsigned int i; 98 int ret; 99 unsigned int int_time, new_int_time; 100 101 for (i = 0; i < ARRAY_SIZE(isl29018_int_utimes[chip->type]); ++i) { 102 if (utime == isl29018_int_utimes[chip->type][i]) { 103 new_int_time = i; 104 break; 105 } 106 } 107 108 if (i >= ARRAY_SIZE(isl29018_int_utimes[chip->type])) 109 return -EINVAL; 110 111 ret = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMAND2, 112 ISL29018_CMD2_RESOLUTION_MASK, 113 i << ISL29018_CMD2_RESOLUTION_SHIFT); 114 if (ret < 0) 115 return ret; 116 117 /* Keep the same range when integration time changes */ 118 int_time = chip->int_time; 119 for (i = 0; i < ARRAY_SIZE(isl29018_scales[int_time]); ++i) { 120 if (chip->scale.scale == isl29018_scales[int_time][i].scale && 121 chip->scale.uscale == isl29018_scales[int_time][i].uscale) { 122 chip->scale = isl29018_scales[new_int_time][i]; 123 break; 124 } 125 } 126 chip->int_time = new_int_time; 127 128 return 0; 129 } 130 131 static int isl29018_set_scale(struct isl29018_chip *chip, int scale, int uscale) 132 { 133 unsigned int i; 134 int ret; 135 struct isl29018_scale new_scale; 136 137 for (i = 0; i < ARRAY_SIZE(isl29018_scales[chip->int_time]); ++i) { 138 if (scale == isl29018_scales[chip->int_time][i].scale && 139 uscale == isl29018_scales[chip->int_time][i].uscale) { 140 new_scale = isl29018_scales[chip->int_time][i]; 141 break; 142 } 143 } 144 145 if (i >= ARRAY_SIZE(isl29018_scales[chip->int_time])) 146 return -EINVAL; 147 148 ret = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMAND2, 149 ISL29018_CMD2_RANGE_MASK, 150 i << ISL29018_CMD2_RANGE_SHIFT); 151 if (ret < 0) 152 return ret; 153 154 chip->scale = new_scale; 155 156 return 0; 157 } 158 159 static int isl29018_read_sensor_input(struct isl29018_chip *chip, int mode) 160 { 161 int status; 162 unsigned int lsb; 163 unsigned int msb; 164 struct device *dev = regmap_get_device(chip->regmap); 165 166 /* Set mode */ 167 status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1, 168 mode << ISL29018_CMD1_OPMODE_SHIFT); 169 if (status) { 170 dev_err(dev, 171 "Error in setting operating mode err %d\n", status); 172 return status; 173 } 174 msleep(ISL29018_CONV_TIME_MS); 175 status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_LSB, &lsb); 176 if (status < 0) { 177 dev_err(dev, 178 "Error in reading LSB DATA with err %d\n", status); 179 return status; 180 } 181 182 status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_MSB, &msb); 183 if (status < 0) { 184 dev_err(dev, 185 "Error in reading MSB DATA with error %d\n", status); 186 return status; 187 } 188 dev_vdbg(dev, "MSB 0x%x and LSB 0x%x\n", msb, lsb); 189 190 return (msb << 8) | lsb; 191 } 192 193 static int isl29018_read_lux(struct isl29018_chip *chip, int *lux) 194 { 195 int lux_data; 196 unsigned int data_x_range; 197 198 lux_data = isl29018_read_sensor_input(chip, 199 ISL29018_CMD1_OPMODE_ALS_ONCE); 200 if (lux_data < 0) 201 return lux_data; 202 203 data_x_range = lux_data * chip->scale.scale + 204 lux_data * chip->scale.uscale / 1000000; 205 *lux = data_x_range * chip->calibscale + 206 data_x_range * chip->ucalibscale / 1000000; 207 208 return 0; 209 } 210 211 static int isl29018_read_ir(struct isl29018_chip *chip, int *ir) 212 { 213 int ir_data; 214 215 ir_data = isl29018_read_sensor_input(chip, 216 ISL29018_CMD1_OPMODE_IR_ONCE); 217 if (ir_data < 0) 218 return ir_data; 219 220 *ir = ir_data; 221 222 return 0; 223 } 224 225 static int isl29018_read_proximity_ir(struct isl29018_chip *chip, int scheme, 226 int *near_ir) 227 { 228 int status; 229 int prox_data = -1; 230 int ir_data = -1; 231 struct device *dev = regmap_get_device(chip->regmap); 232 233 /* Do proximity sensing with required scheme */ 234 status = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMAND2, 235 ISL29018_CMD2_SCHEME_MASK, 236 scheme << ISL29018_CMD2_SCHEME_SHIFT); 237 if (status) { 238 dev_err(dev, "Error in setting operating mode\n"); 239 return status; 240 } 241 242 prox_data = isl29018_read_sensor_input(chip, 243 ISL29018_CMD1_OPMODE_PROX_ONCE); 244 if (prox_data < 0) 245 return prox_data; 246 247 if (scheme == 1) { 248 *near_ir = prox_data; 249 return 0; 250 } 251 252 ir_data = isl29018_read_sensor_input(chip, 253 ISL29018_CMD1_OPMODE_IR_ONCE); 254 if (ir_data < 0) 255 return ir_data; 256 257 if (prox_data >= ir_data) 258 *near_ir = prox_data - ir_data; 259 else 260 *near_ir = 0; 261 262 return 0; 263 } 264 265 static ssize_t in_illuminance_scale_available_show 266 (struct device *dev, struct device_attribute *attr, 267 char *buf) 268 { 269 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 270 struct isl29018_chip *chip = iio_priv(indio_dev); 271 unsigned int i; 272 int len = 0; 273 274 mutex_lock(&chip->lock); 275 for (i = 0; i < ARRAY_SIZE(isl29018_scales[chip->int_time]); ++i) 276 len += sysfs_emit_at(buf, len, "%d.%06d ", 277 isl29018_scales[chip->int_time][i].scale, 278 isl29018_scales[chip->int_time][i].uscale); 279 mutex_unlock(&chip->lock); 280 281 buf[len - 1] = '\n'; 282 283 return len; 284 } 285 286 static ssize_t in_illuminance_integration_time_available_show 287 (struct device *dev, struct device_attribute *attr, 288 char *buf) 289 { 290 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 291 struct isl29018_chip *chip = iio_priv(indio_dev); 292 unsigned int i; 293 int len = 0; 294 295 for (i = 0; i < ARRAY_SIZE(isl29018_int_utimes[chip->type]); ++i) 296 len += sysfs_emit_at(buf, len, "0.%06d ", 297 isl29018_int_utimes[chip->type][i]); 298 299 buf[len - 1] = '\n'; 300 301 return len; 302 } 303 304 /* 305 * From ISL29018 Data Sheet (FN6619.4, Oct 8, 2012) regarding the 306 * infrared suppression: 307 * 308 * Proximity Sensing Scheme: Bit 7. This bit programs the function 309 * of the proximity detection. Logic 0 of this bit, Scheme 0, makes 310 * full n (4, 8, 12, 16) bits (unsigned) proximity detection. The range 311 * of Scheme 0 proximity count is from 0 to 2^n. Logic 1 of this bit, 312 * Scheme 1, makes n-1 (3, 7, 11, 15) bits (2's complementary) 313 * proximity_less_ambient detection. The range of Scheme 1 314 * proximity count is from -2^(n-1) to 2^(n-1) . The sign bit is extended 315 * for resolutions less than 16. While Scheme 0 has wider dynamic 316 * range, Scheme 1 proximity detection is less affected by the 317 * ambient IR noise variation. 318 * 319 * 0 Sensing IR from LED and ambient 320 * 1 Sensing IR from LED with ambient IR rejection 321 */ 322 static ssize_t proximity_on_chip_ambient_infrared_suppression_show 323 (struct device *dev, struct device_attribute *attr, 324 char *buf) 325 { 326 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 327 struct isl29018_chip *chip = iio_priv(indio_dev); 328 329 /* 330 * Return the "proximity scheme" i.e. if the chip does on chip 331 * infrared suppression (1 means perform on chip suppression) 332 */ 333 return sysfs_emit(buf, "%d\n", chip->prox_scheme); 334 } 335 336 static ssize_t proximity_on_chip_ambient_infrared_suppression_store 337 (struct device *dev, struct device_attribute *attr, 338 const char *buf, size_t count) 339 { 340 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 341 struct isl29018_chip *chip = iio_priv(indio_dev); 342 int val; 343 int ret; 344 345 ret = kstrtoint(buf, 10, &val); 346 if (ret) 347 return ret; 348 if (!(val == 0 || val == 1)) 349 return -EINVAL; 350 351 /* 352 * Get the "proximity scheme" i.e. if the chip does on chip 353 * infrared suppression (1 means perform on chip suppression) 354 */ 355 mutex_lock(&chip->lock); 356 chip->prox_scheme = val; 357 mutex_unlock(&chip->lock); 358 359 return count; 360 } 361 362 static int isl29018_write_raw(struct iio_dev *indio_dev, 363 struct iio_chan_spec const *chan, 364 int val, 365 int val2, 366 long mask) 367 { 368 struct isl29018_chip *chip = iio_priv(indio_dev); 369 int ret = -EINVAL; 370 371 mutex_lock(&chip->lock); 372 if (chip->suspended) { 373 ret = -EBUSY; 374 goto write_done; 375 } 376 switch (mask) { 377 case IIO_CHAN_INFO_CALIBSCALE: 378 if (chan->type == IIO_LIGHT) { 379 chip->calibscale = val; 380 chip->ucalibscale = val2; 381 ret = 0; 382 } 383 break; 384 case IIO_CHAN_INFO_INT_TIME: 385 if (chan->type == IIO_LIGHT && !val) 386 ret = isl29018_set_integration_time(chip, val2); 387 break; 388 case IIO_CHAN_INFO_SCALE: 389 if (chan->type == IIO_LIGHT) 390 ret = isl29018_set_scale(chip, val, val2); 391 break; 392 default: 393 break; 394 } 395 396 write_done: 397 mutex_unlock(&chip->lock); 398 399 return ret; 400 } 401 402 static int isl29018_read_raw(struct iio_dev *indio_dev, 403 struct iio_chan_spec const *chan, 404 int *val, 405 int *val2, 406 long mask) 407 { 408 int ret = -EINVAL; 409 struct isl29018_chip *chip = iio_priv(indio_dev); 410 411 mutex_lock(&chip->lock); 412 if (chip->suspended) { 413 ret = -EBUSY; 414 goto read_done; 415 } 416 switch (mask) { 417 case IIO_CHAN_INFO_RAW: 418 case IIO_CHAN_INFO_PROCESSED: 419 switch (chan->type) { 420 case IIO_LIGHT: 421 ret = isl29018_read_lux(chip, val); 422 break; 423 case IIO_INTENSITY: 424 ret = isl29018_read_ir(chip, val); 425 break; 426 case IIO_PROXIMITY: 427 ret = isl29018_read_proximity_ir(chip, 428 chip->prox_scheme, 429 val); 430 break; 431 default: 432 break; 433 } 434 if (!ret) 435 ret = IIO_VAL_INT; 436 break; 437 case IIO_CHAN_INFO_INT_TIME: 438 if (chan->type == IIO_LIGHT) { 439 *val = 0; 440 *val2 = isl29018_int_utimes[chip->type][chip->int_time]; 441 ret = IIO_VAL_INT_PLUS_MICRO; 442 } 443 break; 444 case IIO_CHAN_INFO_SCALE: 445 if (chan->type == IIO_LIGHT) { 446 *val = chip->scale.scale; 447 *val2 = chip->scale.uscale; 448 ret = IIO_VAL_INT_PLUS_MICRO; 449 } 450 break; 451 case IIO_CHAN_INFO_CALIBSCALE: 452 if (chan->type == IIO_LIGHT) { 453 *val = chip->calibscale; 454 *val2 = chip->ucalibscale; 455 ret = IIO_VAL_INT_PLUS_MICRO; 456 } 457 break; 458 default: 459 break; 460 } 461 462 read_done: 463 mutex_unlock(&chip->lock); 464 465 return ret; 466 } 467 468 #define ISL29018_LIGHT_CHANNEL { \ 469 .type = IIO_LIGHT, \ 470 .indexed = 1, \ 471 .channel = 0, \ 472 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | \ 473 BIT(IIO_CHAN_INFO_CALIBSCALE) | \ 474 BIT(IIO_CHAN_INFO_SCALE) | \ 475 BIT(IIO_CHAN_INFO_INT_TIME), \ 476 } 477 478 #define ISL29018_IR_CHANNEL { \ 479 .type = IIO_INTENSITY, \ 480 .modified = 1, \ 481 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 482 .channel2 = IIO_MOD_LIGHT_IR, \ 483 } 484 485 #define ISL29018_PROXIMITY_CHANNEL { \ 486 .type = IIO_PROXIMITY, \ 487 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 488 } 489 490 static const struct iio_chan_spec isl29018_channels[] = { 491 ISL29018_LIGHT_CHANNEL, 492 ISL29018_IR_CHANNEL, 493 ISL29018_PROXIMITY_CHANNEL, 494 }; 495 496 static const struct iio_chan_spec isl29023_channels[] = { 497 ISL29018_LIGHT_CHANNEL, 498 ISL29018_IR_CHANNEL, 499 }; 500 501 static IIO_DEVICE_ATTR_RO(in_illuminance_integration_time_available, 0); 502 static IIO_DEVICE_ATTR_RO(in_illuminance_scale_available, 0); 503 static IIO_DEVICE_ATTR_RW(proximity_on_chip_ambient_infrared_suppression, 0); 504 505 #define ISL29018_DEV_ATTR(name) (&iio_dev_attr_##name.dev_attr.attr) 506 507 static struct attribute *isl29018_attributes[] = { 508 ISL29018_DEV_ATTR(in_illuminance_scale_available), 509 ISL29018_DEV_ATTR(in_illuminance_integration_time_available), 510 ISL29018_DEV_ATTR(proximity_on_chip_ambient_infrared_suppression), 511 NULL 512 }; 513 514 static struct attribute *isl29023_attributes[] = { 515 ISL29018_DEV_ATTR(in_illuminance_scale_available), 516 ISL29018_DEV_ATTR(in_illuminance_integration_time_available), 517 NULL 518 }; 519 520 static const struct attribute_group isl29018_group = { 521 .attrs = isl29018_attributes, 522 }; 523 524 static const struct attribute_group isl29023_group = { 525 .attrs = isl29023_attributes, 526 }; 527 528 enum { 529 isl29018, 530 isl29023, 531 isl29035, 532 }; 533 534 static int isl29018_chip_init(struct isl29018_chip *chip) 535 { 536 int status; 537 struct device *dev = regmap_get_device(chip->regmap); 538 539 if (chip->type == isl29035) { 540 unsigned int id; 541 542 status = regmap_read(chip->regmap, ISL29035_REG_DEVICE_ID, &id); 543 if (status < 0) { 544 dev_err(dev, 545 "Error reading ID register with error %d\n", 546 status); 547 return status; 548 } 549 550 id = (id & ISL29035_DEVICE_ID_MASK) >> ISL29035_DEVICE_ID_SHIFT; 551 552 if (id != ISL29035_DEVICE_ID) 553 return -ENODEV; 554 555 /* Clear brownout bit */ 556 status = regmap_clear_bits(chip->regmap, 557 ISL29035_REG_DEVICE_ID, 558 ISL29035_BOUT_MASK); 559 if (status < 0) 560 return status; 561 } 562 563 /* 564 * Code added per Intersil Application Note 1534: 565 * When VDD sinks to approximately 1.8V or below, some of 566 * the part's registers may change their state. When VDD 567 * recovers to 2.25V (or greater), the part may thus be in an 568 * unknown mode of operation. The user can return the part to 569 * a known mode of operation either by (a) setting VDD = 0V for 570 * 1 second or more and then powering back up with a slew rate 571 * of 0.5V/ms or greater, or (b) via I2C disable all ALS/PROX 572 * conversions, clear the test registers, and then rewrite all 573 * registers to the desired values. 574 * ... 575 * For ISL29011, ISL29018, ISL29021, ISL29023 576 * 1. Write 0x00 to register 0x08 (TEST) 577 * 2. Write 0x00 to register 0x00 (CMD1) 578 * 3. Rewrite all registers to the desired values 579 * 580 * ISL29018 Data Sheet (FN6619.1, Feb 11, 2010) essentially says 581 * the same thing EXCEPT the data sheet asks for a 1ms delay after 582 * writing the CMD1 register. 583 */ 584 status = regmap_write(chip->regmap, ISL29018_REG_TEST, 0x0); 585 if (status < 0) { 586 dev_err(dev, "Failed to clear isl29018 TEST reg.(%d)\n", 587 status); 588 return status; 589 } 590 591 /* 592 * See Intersil AN1534 comments above. 593 * "Operating Mode" (COMMAND1) register is reprogrammed when 594 * data is read from the device. 595 */ 596 status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1, 0); 597 if (status < 0) { 598 dev_err(dev, "Failed to clear isl29018 CMD1 reg.(%d)\n", 599 status); 600 return status; 601 } 602 603 usleep_range(1000, 2000); /* per data sheet, page 10 */ 604 605 /* Set defaults */ 606 status = isl29018_set_scale(chip, chip->scale.scale, 607 chip->scale.uscale); 608 if (status < 0) { 609 dev_err(dev, "Init of isl29018 fails\n"); 610 return status; 611 } 612 613 status = isl29018_set_integration_time(chip, 614 isl29018_int_utimes[chip->type][chip->int_time]); 615 if (status < 0) 616 dev_err(dev, "Init of isl29018 fails\n"); 617 618 return status; 619 } 620 621 static const struct iio_info isl29018_info = { 622 .attrs = &isl29018_group, 623 .read_raw = isl29018_read_raw, 624 .write_raw = isl29018_write_raw, 625 }; 626 627 static const struct iio_info isl29023_info = { 628 .attrs = &isl29023_group, 629 .read_raw = isl29018_read_raw, 630 .write_raw = isl29018_write_raw, 631 }; 632 633 static bool isl29018_is_volatile_reg(struct device *dev, unsigned int reg) 634 { 635 switch (reg) { 636 case ISL29018_REG_ADD_DATA_LSB: 637 case ISL29018_REG_ADD_DATA_MSB: 638 case ISL29018_REG_ADD_COMMAND1: 639 case ISL29018_REG_TEST: 640 case ISL29035_REG_DEVICE_ID: 641 return true; 642 default: 643 return false; 644 } 645 } 646 647 static const struct regmap_config isl29018_regmap_config = { 648 .reg_bits = 8, 649 .val_bits = 8, 650 .volatile_reg = isl29018_is_volatile_reg, 651 .max_register = ISL29018_REG_TEST, 652 .num_reg_defaults_raw = ISL29018_REG_TEST + 1, 653 .cache_type = REGCACHE_RBTREE, 654 }; 655 656 static const struct regmap_config isl29035_regmap_config = { 657 .reg_bits = 8, 658 .val_bits = 8, 659 .volatile_reg = isl29018_is_volatile_reg, 660 .max_register = ISL29035_REG_DEVICE_ID, 661 .num_reg_defaults_raw = ISL29035_REG_DEVICE_ID + 1, 662 .cache_type = REGCACHE_RBTREE, 663 }; 664 665 struct isl29018_chip_info { 666 const struct iio_chan_spec *channels; 667 int num_channels; 668 const struct iio_info *indio_info; 669 const struct regmap_config *regmap_cfg; 670 }; 671 672 static const struct isl29018_chip_info isl29018_chip_info_tbl[] = { 673 [isl29018] = { 674 .channels = isl29018_channels, 675 .num_channels = ARRAY_SIZE(isl29018_channels), 676 .indio_info = &isl29018_info, 677 .regmap_cfg = &isl29018_regmap_config, 678 }, 679 [isl29023] = { 680 .channels = isl29023_channels, 681 .num_channels = ARRAY_SIZE(isl29023_channels), 682 .indio_info = &isl29023_info, 683 .regmap_cfg = &isl29018_regmap_config, 684 }, 685 [isl29035] = { 686 .channels = isl29023_channels, 687 .num_channels = ARRAY_SIZE(isl29023_channels), 688 .indio_info = &isl29023_info, 689 .regmap_cfg = &isl29035_regmap_config, 690 }, 691 }; 692 693 static void isl29018_disable_regulator_action(void *_data) 694 { 695 struct isl29018_chip *chip = _data; 696 int err; 697 698 err = regulator_disable(chip->vcc_reg); 699 if (err) 700 pr_err("failed to disable isl29018's VCC regulator!\n"); 701 } 702 703 static int isl29018_probe(struct i2c_client *client) 704 { 705 const struct i2c_device_id *id = i2c_client_get_device_id(client); 706 struct isl29018_chip *chip; 707 struct iio_dev *indio_dev; 708 const void *ddata = NULL; 709 const char *name; 710 int dev_id; 711 int err; 712 713 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip)); 714 if (!indio_dev) 715 return -ENOMEM; 716 717 chip = iio_priv(indio_dev); 718 719 i2c_set_clientdata(client, indio_dev); 720 721 if (id) { 722 name = id->name; 723 dev_id = id->driver_data; 724 } else { 725 name = iio_get_acpi_device_name_and_data(&client->dev, &ddata); 726 dev_id = (intptr_t)ddata; 727 } 728 729 mutex_init(&chip->lock); 730 731 chip->type = dev_id; 732 chip->calibscale = 1; 733 chip->ucalibscale = 0; 734 chip->int_time = ISL29018_INT_TIME_16; 735 chip->scale = isl29018_scales[chip->int_time][0]; 736 chip->suspended = false; 737 738 chip->vcc_reg = devm_regulator_get(&client->dev, "vcc"); 739 if (IS_ERR(chip->vcc_reg)) 740 return dev_err_probe(&client->dev, PTR_ERR(chip->vcc_reg), 741 "failed to get VCC regulator!\n"); 742 743 err = regulator_enable(chip->vcc_reg); 744 if (err) { 745 dev_err(&client->dev, "failed to enable VCC regulator!\n"); 746 return err; 747 } 748 749 err = devm_add_action_or_reset(&client->dev, isl29018_disable_regulator_action, 750 chip); 751 if (err) { 752 dev_err(&client->dev, "failed to setup regulator cleanup action!\n"); 753 return err; 754 } 755 756 chip->regmap = devm_regmap_init_i2c(client, 757 isl29018_chip_info_tbl[dev_id].regmap_cfg); 758 if (IS_ERR(chip->regmap)) { 759 err = PTR_ERR(chip->regmap); 760 dev_err(&client->dev, "regmap initialization fails: %d\n", err); 761 return err; 762 } 763 764 err = isl29018_chip_init(chip); 765 if (err) 766 return err; 767 768 indio_dev->info = isl29018_chip_info_tbl[dev_id].indio_info; 769 indio_dev->channels = isl29018_chip_info_tbl[dev_id].channels; 770 indio_dev->num_channels = isl29018_chip_info_tbl[dev_id].num_channels; 771 indio_dev->name = name; 772 indio_dev->modes = INDIO_DIRECT_MODE; 773 774 return devm_iio_device_register(&client->dev, indio_dev); 775 } 776 777 static int isl29018_suspend(struct device *dev) 778 { 779 struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev)); 780 int ret; 781 782 mutex_lock(&chip->lock); 783 784 /* 785 * Since this driver uses only polling commands, we are by default in 786 * auto shutdown (ie, power-down) mode. 787 * So we do not have much to do here. 788 */ 789 chip->suspended = true; 790 ret = regulator_disable(chip->vcc_reg); 791 if (ret) 792 dev_err(dev, "failed to disable VCC regulator\n"); 793 794 mutex_unlock(&chip->lock); 795 796 return ret; 797 } 798 799 static int isl29018_resume(struct device *dev) 800 { 801 struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev)); 802 int err; 803 804 mutex_lock(&chip->lock); 805 806 err = regulator_enable(chip->vcc_reg); 807 if (err) { 808 dev_err(dev, "failed to enable VCC regulator\n"); 809 mutex_unlock(&chip->lock); 810 return err; 811 } 812 813 err = isl29018_chip_init(chip); 814 if (!err) 815 chip->suspended = false; 816 817 mutex_unlock(&chip->lock); 818 819 return err; 820 } 821 822 static DEFINE_SIMPLE_DEV_PM_OPS(isl29018_pm_ops, isl29018_suspend, 823 isl29018_resume); 824 825 static const struct acpi_device_id isl29018_acpi_match[] = { 826 {"ISL29018", isl29018}, 827 {"ISL29023", isl29023}, 828 {"ISL29035", isl29035}, 829 { } 830 }; 831 MODULE_DEVICE_TABLE(acpi, isl29018_acpi_match); 832 833 static const struct i2c_device_id isl29018_id[] = { 834 { .name = "isl29018", .driver_data = isl29018 }, 835 { .name = "isl29023", .driver_data = isl29023 }, 836 { .name = "isl29035", .driver_data = isl29035 }, 837 { } 838 }; 839 MODULE_DEVICE_TABLE(i2c, isl29018_id); 840 841 static const struct of_device_id isl29018_of_match[] = { 842 { .compatible = "isil,isl29018", }, 843 { .compatible = "isil,isl29023", }, 844 { .compatible = "isil,isl29035", }, 845 { } 846 }; 847 MODULE_DEVICE_TABLE(of, isl29018_of_match); 848 849 static struct i2c_driver isl29018_driver = { 850 .driver = { 851 .name = "isl29018", 852 .acpi_match_table = isl29018_acpi_match, 853 .pm = pm_sleep_ptr(&isl29018_pm_ops), 854 .of_match_table = isl29018_of_match, 855 }, 856 .probe = isl29018_probe, 857 .id_table = isl29018_id, 858 }; 859 module_i2c_driver(isl29018_driver); 860 861 MODULE_DESCRIPTION("ISL29018 Ambient Light Sensor driver"); 862 MODULE_LICENSE("GPL"); 863