1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * VEML6030, VMEL6035 and VEML7700 Ambient Light Sensors 4 * 5 * Copyright (c) 2019, Rishi Gupta <gupt21@gmail.com> 6 * 7 * VEML6030: 8 * Datasheet: https://www.vishay.com/docs/84366/veml6030.pdf 9 * Appnote-84367: https://www.vishay.com/docs/84367/designingveml6030.pdf 10 * 11 * VEML6035: 12 * Datasheet: https://www.vishay.com/docs/84889/veml6035.pdf 13 * Appnote-84944: https://www.vishay.com/docs/84944/designingveml6035.pdf 14 * 15 * VEML7700: 16 * Datasheet: https://www.vishay.com/docs/84286/veml7700.pdf 17 * Appnote-84323: https://www.vishay.com/docs/84323/designingveml7700.pdf 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/regmap.h> 25 #include <linux/interrupt.h> 26 #include <linux/pm_runtime.h> 27 #include <linux/regulator/consumer.h> 28 #include <linux/iio/iio.h> 29 #include <linux/iio/sysfs.h> 30 #include <linux/iio/events.h> 31 #include <linux/iio/trigger_consumer.h> 32 #include <linux/iio/triggered_buffer.h> 33 34 /* Device registers */ 35 #define VEML6030_REG_ALS_CONF 0x00 36 #define VEML6030_REG_ALS_WH 0x01 37 #define VEML6030_REG_ALS_WL 0x02 38 #define VEML6030_REG_ALS_PSM 0x03 39 #define VEML6030_REG_ALS_DATA 0x04 40 #define VEML6030_REG_WH_DATA 0x05 41 #define VEML6030_REG_ALS_INT 0x06 42 #define VEML6030_REG_DATA(ch) (VEML6030_REG_ALS_DATA + (ch)) 43 44 /* Bit masks for specific functionality */ 45 #define VEML6030_ALS_IT GENMASK(9, 6) 46 #define VEML6030_PSM GENMASK(2, 1) 47 #define VEML6030_ALS_PERS GENMASK(5, 4) 48 #define VEML6030_ALS_GAIN GENMASK(12, 11) 49 #define VEML6030_PSM_EN BIT(0) 50 #define VEML6030_INT_TH_LOW BIT(15) 51 #define VEML6030_INT_TH_HIGH BIT(14) 52 #define VEML6030_ALS_INT_EN BIT(1) 53 #define VEML6030_ALS_SD BIT(0) 54 55 #define VEML6035_GAIN_M GENMASK(12, 10) 56 #define VEML6035_GAIN BIT(10) 57 #define VEML6035_DG BIT(11) 58 #define VEML6035_SENS BIT(12) 59 #define VEML6035_INT_CHAN BIT(3) 60 #define VEML6035_CHAN_EN BIT(2) 61 62 enum veml6030_scan { 63 VEML6030_SCAN_ALS, 64 VEML6030_SCAN_WH, 65 VEML6030_SCAN_TIMESTAMP, 66 }; 67 68 struct veml603x_chip { 69 const char *name; 70 const int(*scale_vals)[][2]; 71 const int num_scale_vals; 72 const struct iio_chan_spec *channels; 73 const int num_channels; 74 int (*hw_init)(struct iio_dev *indio_dev, struct device *dev); 75 int (*set_info)(struct iio_dev *indio_dev); 76 int (*set_als_gain)(struct iio_dev *indio_dev, int val, int val2); 77 int (*get_als_gain)(struct iio_dev *indio_dev, int *val, int *val2); 78 }; 79 80 /* 81 * The resolution depends on both gain and integration time. The 82 * cur_resolution stores one of the resolution mentioned in the 83 * table during startup and gets updated whenever integration time 84 * or gain is changed. 85 * 86 * Table 'resolution and maximum detection range' in the appnotes 87 * is visualized as a 2D array. The cur_gain stores index of gain 88 * in this table (0-3 for VEML6030, 0-5 for VEML6035) while the 89 * cur_integration_time holds index of integration time (0-5). 90 */ 91 struct veml6030_data { 92 struct i2c_client *client; 93 struct regmap *regmap; 94 int cur_resolution; 95 int cur_gain; 96 int cur_integration_time; 97 const struct veml603x_chip *chip; 98 }; 99 100 static const int veml6030_it_times[][2] = { 101 { 0, 25000 }, 102 { 0, 50000 }, 103 { 0, 100000 }, 104 { 0, 200000 }, 105 { 0, 400000 }, 106 { 0, 800000 }, 107 }; 108 109 /* 110 * Scale is 1/gain. Value 0.125 is ALS gain x (1/8), 0.25 is 111 * ALS gain x (1/4), 0.5 is ALS gain x (1/2), 1.0 is ALS gain x 1, 112 * 2.0 is ALS gain x2, and 4.0 is ALS gain x 4. 113 */ 114 static const int veml6030_scale_vals[][2] = { 115 { 0, 125000 }, 116 { 0, 250000 }, 117 { 1, 0 }, 118 { 2, 0 }, 119 }; 120 121 static const int veml6035_scale_vals[][2] = { 122 { 0, 125000 }, 123 { 0, 250000 }, 124 { 0, 500000 }, 125 { 1, 0 }, 126 { 2, 0 }, 127 { 4, 0 }, 128 }; 129 130 /* 131 * Persistence = 1/2/4/8 x integration time 132 * Minimum time for which light readings must stay above configured 133 * threshold to assert the interrupt. 134 */ 135 static const char * const period_values[] = { 136 "0.1 0.2 0.4 0.8", 137 "0.2 0.4 0.8 1.6", 138 "0.4 0.8 1.6 3.2", 139 "0.8 1.6 3.2 6.4", 140 "0.05 0.1 0.2 0.4", 141 "0.025 0.050 0.1 0.2" 142 }; 143 144 /* 145 * Return list of valid period values in seconds corresponding to 146 * the currently active integration time. 147 */ 148 static ssize_t in_illuminance_period_available_show(struct device *dev, 149 struct device_attribute *attr, char *buf) 150 { 151 struct veml6030_data *data = iio_priv(dev_to_iio_dev(dev)); 152 int ret, reg, x; 153 154 ret = regmap_read(data->regmap, VEML6030_REG_ALS_CONF, ®); 155 if (ret) { 156 dev_err(&data->client->dev, 157 "can't read als conf register %d\n", ret); 158 return ret; 159 } 160 161 ret = ((reg >> 6) & 0xF); 162 switch (ret) { 163 case 0: 164 case 1: 165 case 2: 166 case 3: 167 x = ret; 168 break; 169 case 8: 170 x = 4; 171 break; 172 case 12: 173 x = 5; 174 break; 175 default: 176 return -EINVAL; 177 } 178 179 return sysfs_emit(buf, "%s\n", period_values[x]); 180 } 181 182 static IIO_DEVICE_ATTR_RO(in_illuminance_period_available, 0); 183 184 static struct attribute *veml6030_event_attributes[] = { 185 &iio_dev_attr_in_illuminance_period_available.dev_attr.attr, 186 NULL 187 }; 188 189 static const struct attribute_group veml6030_event_attr_group = { 190 .attrs = veml6030_event_attributes, 191 }; 192 193 static int veml6030_als_pwr_on(struct veml6030_data *data) 194 { 195 int ret; 196 197 ret = regmap_clear_bits(data->regmap, VEML6030_REG_ALS_CONF, 198 VEML6030_ALS_SD); 199 if (ret) 200 return ret; 201 202 /* Wait 4 ms to let processor & oscillator start correctly */ 203 fsleep(4000); 204 205 return 0; 206 } 207 208 static int veml6030_als_shut_down(struct veml6030_data *data) 209 { 210 return regmap_set_bits(data->regmap, VEML6030_REG_ALS_CONF, 211 VEML6030_ALS_SD); 212 } 213 214 static void veml6030_als_shut_down_action(void *data) 215 { 216 veml6030_als_shut_down(data); 217 } 218 219 static const struct iio_event_spec veml6030_event_spec[] = { 220 { 221 .type = IIO_EV_TYPE_THRESH, 222 .dir = IIO_EV_DIR_RISING, 223 .mask_separate = BIT(IIO_EV_INFO_VALUE), 224 }, { 225 .type = IIO_EV_TYPE_THRESH, 226 .dir = IIO_EV_DIR_FALLING, 227 .mask_separate = BIT(IIO_EV_INFO_VALUE), 228 }, { 229 .type = IIO_EV_TYPE_THRESH, 230 .dir = IIO_EV_DIR_EITHER, 231 .mask_separate = BIT(IIO_EV_INFO_PERIOD) | 232 BIT(IIO_EV_INFO_ENABLE), 233 }, 234 }; 235 236 /* Channel number */ 237 enum veml6030_chan { 238 CH_ALS, 239 CH_WHITE, 240 }; 241 242 static const struct iio_chan_spec veml6030_channels[] = { 243 { 244 .type = IIO_LIGHT, 245 .channel = CH_ALS, 246 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 247 BIT(IIO_CHAN_INFO_PROCESSED) | 248 BIT(IIO_CHAN_INFO_INT_TIME) | 249 BIT(IIO_CHAN_INFO_SCALE), 250 .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME) | 251 BIT(IIO_CHAN_INFO_SCALE), 252 .event_spec = veml6030_event_spec, 253 .num_event_specs = ARRAY_SIZE(veml6030_event_spec), 254 .scan_index = VEML6030_SCAN_ALS, 255 .scan_type = { 256 .sign = 'u', 257 .realbits = 16, 258 .storagebits = 16, 259 .endianness = IIO_CPU, 260 }, 261 }, 262 { 263 .type = IIO_INTENSITY, 264 .channel = CH_WHITE, 265 .modified = 1, 266 .channel2 = IIO_MOD_LIGHT_BOTH, 267 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 268 BIT(IIO_CHAN_INFO_INT_TIME) | 269 BIT(IIO_CHAN_INFO_SCALE), 270 .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME) | 271 BIT(IIO_CHAN_INFO_SCALE), 272 .scan_index = VEML6030_SCAN_WH, 273 .scan_type = { 274 .sign = 'u', 275 .realbits = 16, 276 .storagebits = 16, 277 .endianness = IIO_CPU, 278 }, 279 }, 280 IIO_CHAN_SOFT_TIMESTAMP(VEML6030_SCAN_TIMESTAMP), 281 }; 282 283 static const struct iio_chan_spec veml7700_channels[] = { 284 { 285 .type = IIO_LIGHT, 286 .channel = CH_ALS, 287 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 288 BIT(IIO_CHAN_INFO_PROCESSED) | 289 BIT(IIO_CHAN_INFO_INT_TIME) | 290 BIT(IIO_CHAN_INFO_SCALE), 291 .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME) | 292 BIT(IIO_CHAN_INFO_SCALE), 293 .scan_index = VEML6030_SCAN_ALS, 294 .scan_type = { 295 .sign = 'u', 296 .realbits = 16, 297 .storagebits = 16, 298 .endianness = IIO_CPU, 299 }, 300 }, 301 { 302 .type = IIO_INTENSITY, 303 .channel = CH_WHITE, 304 .modified = 1, 305 .channel2 = IIO_MOD_LIGHT_BOTH, 306 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 307 BIT(IIO_CHAN_INFO_INT_TIME) | 308 BIT(IIO_CHAN_INFO_SCALE), 309 .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME) | 310 BIT(IIO_CHAN_INFO_SCALE), 311 .scan_index = VEML6030_SCAN_WH, 312 .scan_type = { 313 .sign = 'u', 314 .realbits = 16, 315 .storagebits = 16, 316 .endianness = IIO_CPU, 317 }, 318 }, 319 IIO_CHAN_SOFT_TIMESTAMP(VEML6030_SCAN_TIMESTAMP), 320 }; 321 322 static const struct regmap_config veml6030_regmap_config = { 323 .name = "veml6030_regmap", 324 .reg_bits = 8, 325 .val_bits = 16, 326 .max_register = VEML6030_REG_ALS_INT, 327 .val_format_endian = REGMAP_ENDIAN_LITTLE, 328 }; 329 330 static int veml6030_get_intgrn_tm(struct iio_dev *indio_dev, 331 int *val, int *val2) 332 { 333 int ret, reg; 334 struct veml6030_data *data = iio_priv(indio_dev); 335 336 ret = regmap_read(data->regmap, VEML6030_REG_ALS_CONF, ®); 337 if (ret) { 338 dev_err(&data->client->dev, 339 "can't read als conf register %d\n", ret); 340 return ret; 341 } 342 343 switch ((reg >> 6) & 0xF) { 344 case 0: 345 *val2 = 100000; 346 break; 347 case 1: 348 *val2 = 200000; 349 break; 350 case 2: 351 *val2 = 400000; 352 break; 353 case 3: 354 *val2 = 800000; 355 break; 356 case 8: 357 *val2 = 50000; 358 break; 359 case 12: 360 *val2 = 25000; 361 break; 362 default: 363 return -EINVAL; 364 } 365 366 *val = 0; 367 return IIO_VAL_INT_PLUS_MICRO; 368 } 369 370 static int veml6030_set_intgrn_tm(struct iio_dev *indio_dev, 371 int val, int val2) 372 { 373 int ret, new_int_time, int_idx; 374 struct veml6030_data *data = iio_priv(indio_dev); 375 376 if (val) 377 return -EINVAL; 378 379 switch (val2) { 380 case 25000: 381 new_int_time = 0x300; 382 int_idx = 5; 383 break; 384 case 50000: 385 new_int_time = 0x200; 386 int_idx = 4; 387 break; 388 case 100000: 389 new_int_time = 0x00; 390 int_idx = 3; 391 break; 392 case 200000: 393 new_int_time = 0x40; 394 int_idx = 2; 395 break; 396 case 400000: 397 new_int_time = 0x80; 398 int_idx = 1; 399 break; 400 case 800000: 401 new_int_time = 0xC0; 402 int_idx = 0; 403 break; 404 default: 405 return -EINVAL; 406 } 407 408 ret = regmap_update_bits(data->regmap, VEML6030_REG_ALS_CONF, 409 VEML6030_ALS_IT, new_int_time); 410 if (ret) { 411 dev_err(&data->client->dev, 412 "can't update als integration time %d\n", ret); 413 return ret; 414 } 415 416 /* 417 * Cache current integration time and update resolution. For every 418 * increase in integration time to next level, resolution is halved 419 * and vice-versa. 420 */ 421 if (data->cur_integration_time < int_idx) 422 data->cur_resolution <<= int_idx - data->cur_integration_time; 423 else if (data->cur_integration_time > int_idx) 424 data->cur_resolution >>= data->cur_integration_time - int_idx; 425 426 data->cur_integration_time = int_idx; 427 428 return ret; 429 } 430 431 static int veml6030_read_persistence(struct iio_dev *indio_dev, 432 int *val, int *val2) 433 { 434 int ret, reg, period, x, y; 435 struct veml6030_data *data = iio_priv(indio_dev); 436 437 ret = veml6030_get_intgrn_tm(indio_dev, &x, &y); 438 if (ret < 0) 439 return ret; 440 441 ret = regmap_read(data->regmap, VEML6030_REG_ALS_CONF, ®); 442 if (ret) { 443 dev_err(&data->client->dev, 444 "can't read als conf register %d\n", ret); 445 } 446 447 /* integration time multiplied by 1/2/4/8 */ 448 period = y * (1 << ((reg >> 4) & 0x03)); 449 450 *val = period / 1000000; 451 *val2 = period % 1000000; 452 453 return IIO_VAL_INT_PLUS_MICRO; 454 } 455 456 static int veml6030_write_persistence(struct iio_dev *indio_dev, 457 int val, int val2) 458 { 459 int ret, period, x, y; 460 struct veml6030_data *data = iio_priv(indio_dev); 461 462 ret = veml6030_get_intgrn_tm(indio_dev, &x, &y); 463 if (ret < 0) 464 return ret; 465 466 if (!val) { 467 period = val2 / y; 468 } else { 469 if ((val == 1) && (val2 == 600000)) 470 period = 1600000 / y; 471 else if ((val == 3) && (val2 == 200000)) 472 period = 3200000 / y; 473 else if ((val == 6) && (val2 == 400000)) 474 period = 6400000 / y; 475 else 476 period = -1; 477 } 478 479 if (period <= 0 || period > 8 || hweight8(period) != 1) 480 return -EINVAL; 481 482 ret = regmap_update_bits(data->regmap, VEML6030_REG_ALS_CONF, 483 VEML6030_ALS_PERS, (ffs(period) - 1) << 4); 484 if (ret) 485 dev_err(&data->client->dev, 486 "can't set persistence value %d\n", ret); 487 488 return ret; 489 } 490 491 /* 492 * Cache currently set gain & update resolution. For every 493 * increase in the gain to next level, resolution is halved 494 * and vice-versa. 495 */ 496 static void veml6030_update_gain_res(struct veml6030_data *data, int gain_idx) 497 { 498 if (data->cur_gain < gain_idx) 499 data->cur_resolution <<= gain_idx - data->cur_gain; 500 else if (data->cur_gain > gain_idx) 501 data->cur_resolution >>= data->cur_gain - gain_idx; 502 503 data->cur_gain = gain_idx; 504 } 505 506 static int veml6030_set_als_gain(struct iio_dev *indio_dev, 507 int val, int val2) 508 { 509 int ret, new_gain, gain_idx; 510 struct veml6030_data *data = iio_priv(indio_dev); 511 512 if (val == 0 && val2 == 125000) { 513 new_gain = 0x1000; /* 0x02 << 11 */ 514 gain_idx = 3; 515 } else if (val == 0 && val2 == 250000) { 516 new_gain = 0x1800; 517 gain_idx = 2; 518 } else if (val == 1 && val2 == 0) { 519 new_gain = 0x00; 520 gain_idx = 1; 521 } else if (val == 2 && val2 == 0) { 522 new_gain = 0x800; 523 gain_idx = 0; 524 } else { 525 return -EINVAL; 526 } 527 528 ret = regmap_update_bits(data->regmap, VEML6030_REG_ALS_CONF, 529 VEML6030_ALS_GAIN, new_gain); 530 if (ret) { 531 dev_err(&data->client->dev, 532 "can't set als gain %d\n", ret); 533 return ret; 534 } 535 536 veml6030_update_gain_res(data, gain_idx); 537 538 return 0; 539 } 540 541 static int veml6035_set_als_gain(struct iio_dev *indio_dev, int val, int val2) 542 { 543 int ret, new_gain, gain_idx; 544 struct veml6030_data *data = iio_priv(indio_dev); 545 546 if (val == 0 && val2 == 125000) { 547 new_gain = VEML6035_SENS; 548 gain_idx = 5; 549 } else if (val == 0 && val2 == 250000) { 550 new_gain = VEML6035_SENS | VEML6035_GAIN; 551 gain_idx = 4; 552 } else if (val == 0 && val2 == 500000) { 553 new_gain = VEML6035_SENS | VEML6035_GAIN | 554 VEML6035_DG; 555 gain_idx = 3; 556 } else if (val == 1 && val2 == 0) { 557 new_gain = 0x0000; 558 gain_idx = 2; 559 } else if (val == 2 && val2 == 0) { 560 new_gain = VEML6035_GAIN; 561 gain_idx = 1; 562 } else if (val == 4 && val2 == 0) { 563 new_gain = VEML6035_GAIN | VEML6035_DG; 564 gain_idx = 0; 565 } else { 566 return -EINVAL; 567 } 568 569 ret = regmap_update_bits(data->regmap, VEML6030_REG_ALS_CONF, 570 VEML6035_GAIN_M, new_gain); 571 if (ret) { 572 dev_err(&data->client->dev, "can't set als gain %d\n", ret); 573 return ret; 574 } 575 576 veml6030_update_gain_res(data, gain_idx); 577 578 return 0; 579 } 580 581 static int veml6030_get_als_gain(struct iio_dev *indio_dev, 582 int *val, int *val2) 583 { 584 int ret, reg; 585 struct veml6030_data *data = iio_priv(indio_dev); 586 587 ret = regmap_read(data->regmap, VEML6030_REG_ALS_CONF, ®); 588 if (ret) { 589 dev_err(&data->client->dev, 590 "can't read als conf register %d\n", ret); 591 return ret; 592 } 593 594 switch ((reg >> 11) & 0x03) { 595 case 0: 596 *val = 1; 597 *val2 = 0; 598 break; 599 case 1: 600 *val = 2; 601 *val2 = 0; 602 break; 603 case 2: 604 *val = 0; 605 *val2 = 125000; 606 break; 607 case 3: 608 *val = 0; 609 *val2 = 250000; 610 break; 611 default: 612 return -EINVAL; 613 } 614 615 return IIO_VAL_INT_PLUS_MICRO; 616 } 617 618 static int veml6035_get_als_gain(struct iio_dev *indio_dev, int *val, int *val2) 619 { 620 int ret, reg; 621 struct veml6030_data *data = iio_priv(indio_dev); 622 623 ret = regmap_read(data->regmap, VEML6030_REG_ALS_CONF, ®); 624 if (ret) { 625 dev_err(&data->client->dev, 626 "can't read als conf register %d\n", ret); 627 return ret; 628 } 629 630 switch (FIELD_GET(VEML6035_GAIN_M, reg)) { 631 case 0: 632 *val = 1; 633 *val2 = 0; 634 break; 635 case 1: 636 case 2: 637 *val = 2; 638 *val2 = 0; 639 break; 640 case 3: 641 *val = 4; 642 *val2 = 0; 643 break; 644 case 4: 645 *val = 0; 646 *val2 = 125000; 647 break; 648 case 5: 649 case 6: 650 *val = 0; 651 *val2 = 250000; 652 break; 653 case 7: 654 *val = 0; 655 *val2 = 500000; 656 break; 657 default: 658 return -EINVAL; 659 } 660 661 return IIO_VAL_INT_PLUS_MICRO; 662 } 663 664 static int veml6030_read_thresh(struct iio_dev *indio_dev, 665 int *val, int *val2, int dir) 666 { 667 int ret, reg; 668 struct veml6030_data *data = iio_priv(indio_dev); 669 670 if (dir == IIO_EV_DIR_RISING) 671 ret = regmap_read(data->regmap, VEML6030_REG_ALS_WH, ®); 672 else 673 ret = regmap_read(data->regmap, VEML6030_REG_ALS_WL, ®); 674 if (ret) { 675 dev_err(&data->client->dev, 676 "can't read als threshold value %d\n", ret); 677 return ret; 678 } 679 680 *val = reg & 0xffff; 681 return IIO_VAL_INT; 682 } 683 684 static int veml6030_write_thresh(struct iio_dev *indio_dev, 685 int val, int val2, int dir) 686 { 687 int ret; 688 struct veml6030_data *data = iio_priv(indio_dev); 689 690 if (val > 0xFFFF || val < 0 || val2) 691 return -EINVAL; 692 693 if (dir == IIO_EV_DIR_RISING) { 694 ret = regmap_write(data->regmap, VEML6030_REG_ALS_WH, val); 695 if (ret) 696 dev_err(&data->client->dev, 697 "can't set high threshold %d\n", ret); 698 } else { 699 ret = regmap_write(data->regmap, VEML6030_REG_ALS_WL, val); 700 if (ret) 701 dev_err(&data->client->dev, 702 "can't set low threshold %d\n", ret); 703 } 704 705 return ret; 706 } 707 708 /* 709 * Provide both raw as well as light reading in lux. 710 * light (in lux) = resolution * raw reading 711 */ 712 static int veml6030_read_raw(struct iio_dev *indio_dev, 713 struct iio_chan_spec const *chan, int *val, 714 int *val2, long mask) 715 { 716 int ret, reg; 717 struct veml6030_data *data = iio_priv(indio_dev); 718 struct regmap *regmap = data->regmap; 719 struct device *dev = &data->client->dev; 720 721 switch (mask) { 722 case IIO_CHAN_INFO_RAW: 723 case IIO_CHAN_INFO_PROCESSED: 724 switch (chan->type) { 725 case IIO_LIGHT: 726 ret = regmap_read(regmap, VEML6030_REG_ALS_DATA, ®); 727 if (ret < 0) { 728 dev_err(dev, "can't read als data %d\n", ret); 729 return ret; 730 } 731 if (mask == IIO_CHAN_INFO_PROCESSED) { 732 *val = (reg * data->cur_resolution) / 10000; 733 *val2 = (reg * data->cur_resolution) % 10000 * 100; 734 return IIO_VAL_INT_PLUS_MICRO; 735 } 736 *val = reg; 737 return IIO_VAL_INT; 738 case IIO_INTENSITY: 739 ret = regmap_read(regmap, VEML6030_REG_WH_DATA, ®); 740 if (ret < 0) { 741 dev_err(dev, "can't read white data %d\n", ret); 742 return ret; 743 } 744 *val = reg; 745 return IIO_VAL_INT; 746 default: 747 return -EINVAL; 748 } 749 case IIO_CHAN_INFO_INT_TIME: 750 return veml6030_get_intgrn_tm(indio_dev, val, val2); 751 case IIO_CHAN_INFO_SCALE: 752 return data->chip->get_als_gain(indio_dev, val, val2); 753 default: 754 return -EINVAL; 755 } 756 } 757 758 static int veml6030_read_avail(struct iio_dev *indio_dev, 759 struct iio_chan_spec const *chan, 760 const int **vals, int *type, int *length, 761 long mask) 762 { 763 struct veml6030_data *data = iio_priv(indio_dev); 764 765 switch (mask) { 766 case IIO_CHAN_INFO_INT_TIME: 767 *vals = (int *)&veml6030_it_times; 768 *length = 2 * ARRAY_SIZE(veml6030_it_times); 769 *type = IIO_VAL_INT_PLUS_MICRO; 770 return IIO_AVAIL_LIST; 771 case IIO_CHAN_INFO_SCALE: 772 *vals = (int *)*data->chip->scale_vals; 773 *length = 2 * data->chip->num_scale_vals; 774 *type = IIO_VAL_INT_PLUS_MICRO; 775 return IIO_AVAIL_LIST; 776 } 777 778 return -EINVAL; 779 } 780 781 static int veml6030_write_raw(struct iio_dev *indio_dev, 782 struct iio_chan_spec const *chan, 783 int val, int val2, long mask) 784 { 785 struct veml6030_data *data = iio_priv(indio_dev); 786 787 switch (mask) { 788 case IIO_CHAN_INFO_INT_TIME: 789 return veml6030_set_intgrn_tm(indio_dev, val, val2); 790 case IIO_CHAN_INFO_SCALE: 791 return data->chip->set_als_gain(indio_dev, val, val2); 792 default: 793 return -EINVAL; 794 } 795 } 796 797 static int veml6030_read_event_val(struct iio_dev *indio_dev, 798 const struct iio_chan_spec *chan, enum iio_event_type type, 799 enum iio_event_direction dir, enum iio_event_info info, 800 int *val, int *val2) 801 { 802 switch (info) { 803 case IIO_EV_INFO_VALUE: 804 switch (dir) { 805 case IIO_EV_DIR_RISING: 806 case IIO_EV_DIR_FALLING: 807 return veml6030_read_thresh(indio_dev, val, val2, dir); 808 default: 809 return -EINVAL; 810 } 811 break; 812 case IIO_EV_INFO_PERIOD: 813 return veml6030_read_persistence(indio_dev, val, val2); 814 default: 815 return -EINVAL; 816 } 817 } 818 819 static int veml6030_write_event_val(struct iio_dev *indio_dev, 820 const struct iio_chan_spec *chan, enum iio_event_type type, 821 enum iio_event_direction dir, enum iio_event_info info, 822 int val, int val2) 823 { 824 switch (info) { 825 case IIO_EV_INFO_VALUE: 826 return veml6030_write_thresh(indio_dev, val, val2, dir); 827 case IIO_EV_INFO_PERIOD: 828 return veml6030_write_persistence(indio_dev, val, val2); 829 default: 830 return -EINVAL; 831 } 832 } 833 834 static int veml6030_read_interrupt_config(struct iio_dev *indio_dev, 835 const struct iio_chan_spec *chan, enum iio_event_type type, 836 enum iio_event_direction dir) 837 { 838 int ret, reg; 839 struct veml6030_data *data = iio_priv(indio_dev); 840 841 ret = regmap_read(data->regmap, VEML6030_REG_ALS_CONF, ®); 842 if (ret) { 843 dev_err(&data->client->dev, 844 "can't read als conf register %d\n", ret); 845 return ret; 846 } 847 848 if (reg & VEML6030_ALS_INT_EN) 849 return 1; 850 else 851 return 0; 852 } 853 854 /* 855 * Sensor should not be measuring light when interrupt is configured. 856 * Therefore correct sequence to configure interrupt functionality is: 857 * shut down -> enable/disable interrupt -> power on 858 * 859 * state = 1 enables interrupt, state = 0 disables interrupt 860 */ 861 static int veml6030_write_interrupt_config(struct iio_dev *indio_dev, 862 const struct iio_chan_spec *chan, enum iio_event_type type, 863 enum iio_event_direction dir, bool state) 864 { 865 int ret; 866 struct veml6030_data *data = iio_priv(indio_dev); 867 868 ret = veml6030_als_shut_down(data); 869 if (ret < 0) { 870 dev_err(&data->client->dev, 871 "can't disable als to configure interrupt %d\n", ret); 872 return ret; 873 } 874 875 /* enable interrupt + power on */ 876 ret = regmap_update_bits(data->regmap, VEML6030_REG_ALS_CONF, 877 VEML6030_ALS_INT_EN | VEML6030_ALS_SD, state << 1); 878 if (ret) 879 dev_err(&data->client->dev, 880 "can't enable interrupt & poweron als %d\n", ret); 881 882 return ret; 883 } 884 885 static const struct iio_info veml6030_info = { 886 .read_raw = veml6030_read_raw, 887 .read_avail = veml6030_read_avail, 888 .write_raw = veml6030_write_raw, 889 .read_event_value = veml6030_read_event_val, 890 .write_event_value = veml6030_write_event_val, 891 .read_event_config = veml6030_read_interrupt_config, 892 .write_event_config = veml6030_write_interrupt_config, 893 .event_attrs = &veml6030_event_attr_group, 894 }; 895 896 static const struct iio_info veml6030_info_no_irq = { 897 .read_raw = veml6030_read_raw, 898 .read_avail = veml6030_read_avail, 899 .write_raw = veml6030_write_raw, 900 }; 901 902 static irqreturn_t veml6030_event_handler(int irq, void *private) 903 { 904 int ret, reg, evtdir; 905 struct iio_dev *indio_dev = private; 906 struct veml6030_data *data = iio_priv(indio_dev); 907 908 ret = regmap_read(data->regmap, VEML6030_REG_ALS_INT, ®); 909 if (ret) { 910 dev_err(&data->client->dev, 911 "can't read als interrupt register %d\n", ret); 912 return IRQ_HANDLED; 913 } 914 915 /* Spurious interrupt handling */ 916 if (!(reg & (VEML6030_INT_TH_HIGH | VEML6030_INT_TH_LOW))) 917 return IRQ_NONE; 918 919 if (reg & VEML6030_INT_TH_HIGH) 920 evtdir = IIO_EV_DIR_RISING; 921 else 922 evtdir = IIO_EV_DIR_FALLING; 923 924 iio_push_event(indio_dev, IIO_UNMOD_EVENT_CODE(IIO_INTENSITY, 925 0, IIO_EV_TYPE_THRESH, evtdir), 926 iio_get_time_ns(indio_dev)); 927 928 return IRQ_HANDLED; 929 } 930 931 static irqreturn_t veml6030_trigger_handler(int irq, void *p) 932 { 933 struct iio_poll_func *pf = p; 934 struct iio_dev *iio = pf->indio_dev; 935 struct veml6030_data *data = iio_priv(iio); 936 unsigned int reg; 937 int ch, ret, i = 0; 938 struct { 939 u16 chans[2]; 940 aligned_s64 timestamp; 941 } scan; 942 943 memset(&scan, 0, sizeof(scan)); 944 945 iio_for_each_active_channel(iio, ch) { 946 ret = regmap_read(data->regmap, VEML6030_REG_DATA(ch), 947 ®); 948 if (ret) 949 goto done; 950 951 scan.chans[i++] = reg; 952 } 953 954 iio_push_to_buffers_with_timestamp(iio, &scan, pf->timestamp); 955 956 done: 957 iio_trigger_notify_done(iio->trig); 958 959 return IRQ_HANDLED; 960 } 961 962 static int veml6030_set_info(struct iio_dev *indio_dev) 963 { 964 struct veml6030_data *data = iio_priv(indio_dev); 965 struct i2c_client *client = data->client; 966 int ret; 967 968 if (client->irq) { 969 ret = devm_request_threaded_irq(&client->dev, client->irq, 970 NULL, veml6030_event_handler, 971 IRQF_TRIGGER_LOW | IRQF_ONESHOT, 972 indio_dev->name, indio_dev); 973 if (ret < 0) 974 return dev_err_probe(&client->dev, ret, 975 "irq %d request failed\n", 976 client->irq); 977 978 indio_dev->info = &veml6030_info; 979 } else { 980 indio_dev->info = &veml6030_info_no_irq; 981 } 982 983 return 0; 984 } 985 986 static int veml7700_set_info(struct iio_dev *indio_dev) 987 { 988 indio_dev->info = &veml6030_info_no_irq; 989 990 return 0; 991 } 992 993 /* 994 * Set ALS gain to 1/8, integration time to 100 ms, PSM to mode 2, 995 * persistence to 1 x integration time and the threshold 996 * interrupt disabled by default. First shutdown the sensor, 997 * update registers and then power on the sensor. 998 */ 999 static int veml6030_hw_init(struct iio_dev *indio_dev, struct device *dev) 1000 { 1001 int ret, val; 1002 struct veml6030_data *data = iio_priv(indio_dev); 1003 1004 ret = veml6030_als_shut_down(data); 1005 if (ret) 1006 return dev_err_probe(dev, ret, "can't shutdown als\n"); 1007 1008 ret = regmap_write(data->regmap, VEML6030_REG_ALS_CONF, 0x1001); 1009 if (ret) 1010 return dev_err_probe(dev, ret, "can't setup als configs\n"); 1011 1012 ret = regmap_update_bits(data->regmap, VEML6030_REG_ALS_PSM, 1013 VEML6030_PSM | VEML6030_PSM_EN, 0x03); 1014 if (ret) 1015 return dev_err_probe(dev, ret, "can't setup default PSM\n"); 1016 1017 ret = regmap_write(data->regmap, VEML6030_REG_ALS_WH, 0xFFFF); 1018 if (ret) 1019 return dev_err_probe(dev, ret, "can't setup high threshold\n"); 1020 1021 ret = regmap_write(data->regmap, VEML6030_REG_ALS_WL, 0x0000); 1022 if (ret) 1023 return dev_err_probe(dev, ret, "can't setup low threshold\n"); 1024 1025 ret = veml6030_als_pwr_on(data); 1026 if (ret) 1027 return dev_err_probe(dev, ret, "can't poweron als\n"); 1028 1029 ret = devm_add_action_or_reset(dev, veml6030_als_shut_down_action, data); 1030 if (ret < 0) 1031 return ret; 1032 1033 /* Clear stale interrupt status bits if any during start */ 1034 ret = regmap_read(data->regmap, VEML6030_REG_ALS_INT, &val); 1035 if (ret < 0) 1036 return dev_err_probe(dev, ret, 1037 "can't clear als interrupt status\n"); 1038 1039 /* Cache currently active measurement parameters */ 1040 data->cur_gain = 3; 1041 data->cur_resolution = 5376; 1042 data->cur_integration_time = 3; 1043 1044 return ret; 1045 } 1046 1047 /* 1048 * Set ALS gain to 1/8, integration time to 100 ms, ALS and WHITE 1049 * channel enabled, ALS channel interrupt, PSM enabled, 1050 * PSM_WAIT = 0.8 s, persistence to 1 x integration time and the 1051 * threshold interrupt disabled by default. First shutdown the sensor, 1052 * update registers and then power on the sensor. 1053 */ 1054 static int veml6035_hw_init(struct iio_dev *indio_dev, struct device *dev) 1055 { 1056 int ret, val; 1057 struct veml6030_data *data = iio_priv(indio_dev); 1058 1059 ret = veml6030_als_shut_down(data); 1060 if (ret) 1061 return dev_err_probe(dev, ret, "can't shutdown als\n"); 1062 1063 ret = regmap_write(data->regmap, VEML6030_REG_ALS_CONF, 1064 VEML6035_SENS | VEML6035_CHAN_EN | VEML6030_ALS_SD); 1065 if (ret) 1066 return dev_err_probe(dev, ret, "can't setup als configs\n"); 1067 1068 ret = regmap_update_bits(data->regmap, VEML6030_REG_ALS_PSM, 1069 VEML6030_PSM | VEML6030_PSM_EN, 0x03); 1070 if (ret) 1071 return dev_err_probe(dev, ret, "can't setup default PSM\n"); 1072 1073 ret = regmap_write(data->regmap, VEML6030_REG_ALS_WH, 0xFFFF); 1074 if (ret) 1075 return dev_err_probe(dev, ret, "can't setup high threshold\n"); 1076 1077 ret = regmap_write(data->regmap, VEML6030_REG_ALS_WL, 0x0000); 1078 if (ret) 1079 return dev_err_probe(dev, ret, "can't setup low threshold\n"); 1080 1081 ret = veml6030_als_pwr_on(data); 1082 if (ret) 1083 return dev_err_probe(dev, ret, "can't poweron als\n"); 1084 1085 ret = devm_add_action_or_reset(dev, veml6030_als_shut_down_action, data); 1086 if (ret < 0) 1087 return ret; 1088 1089 /* Clear stale interrupt status bits if any during start */ 1090 ret = regmap_read(data->regmap, VEML6030_REG_ALS_INT, &val); 1091 if (ret < 0) 1092 return dev_err_probe(dev, ret, 1093 "can't clear als interrupt status\n"); 1094 1095 /* Cache currently active measurement parameters */ 1096 data->cur_gain = 5; 1097 data->cur_resolution = 1024; 1098 data->cur_integration_time = 3; 1099 1100 return 0; 1101 } 1102 1103 static int veml6030_probe(struct i2c_client *client) 1104 { 1105 int ret; 1106 struct veml6030_data *data; 1107 struct iio_dev *indio_dev; 1108 struct regmap *regmap; 1109 1110 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) 1111 return dev_err_probe(&client->dev, -EOPNOTSUPP, 1112 "i2c adapter doesn't support plain i2c\n"); 1113 1114 regmap = devm_regmap_init_i2c(client, &veml6030_regmap_config); 1115 if (IS_ERR(regmap)) 1116 return dev_err_probe(&client->dev, PTR_ERR(regmap), 1117 "can't setup regmap\n"); 1118 1119 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 1120 if (!indio_dev) 1121 return -ENOMEM; 1122 1123 data = iio_priv(indio_dev); 1124 i2c_set_clientdata(client, indio_dev); 1125 data->client = client; 1126 data->regmap = regmap; 1127 1128 ret = devm_regulator_get_enable(&client->dev, "vdd"); 1129 if (ret) 1130 return dev_err_probe(&client->dev, ret, 1131 "failed to enable regulator\n"); 1132 1133 data->chip = i2c_get_match_data(client); 1134 if (!data->chip) 1135 return -EINVAL; 1136 1137 indio_dev->name = data->chip->name; 1138 indio_dev->channels = data->chip->channels; 1139 indio_dev->num_channels = data->chip->num_channels; 1140 indio_dev->modes = INDIO_DIRECT_MODE; 1141 1142 ret = data->chip->set_info(indio_dev); 1143 if (ret < 0) 1144 return ret; 1145 1146 ret = data->chip->hw_init(indio_dev, &client->dev); 1147 if (ret < 0) 1148 return ret; 1149 1150 ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev, NULL, 1151 veml6030_trigger_handler, NULL); 1152 if (ret) 1153 return dev_err_probe(&client->dev, ret, 1154 "Failed to register triggered buffer"); 1155 1156 return devm_iio_device_register(&client->dev, indio_dev); 1157 } 1158 1159 static int veml6030_runtime_suspend(struct device *dev) 1160 { 1161 int ret; 1162 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 1163 struct veml6030_data *data = iio_priv(indio_dev); 1164 1165 ret = veml6030_als_shut_down(data); 1166 if (ret < 0) 1167 dev_err(&data->client->dev, "can't suspend als %d\n", ret); 1168 1169 return ret; 1170 } 1171 1172 static int veml6030_runtime_resume(struct device *dev) 1173 { 1174 int ret; 1175 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 1176 struct veml6030_data *data = iio_priv(indio_dev); 1177 1178 ret = veml6030_als_pwr_on(data); 1179 if (ret < 0) 1180 dev_err(&data->client->dev, "can't resume als %d\n", ret); 1181 1182 return ret; 1183 } 1184 1185 static DEFINE_RUNTIME_DEV_PM_OPS(veml6030_pm_ops, veml6030_runtime_suspend, 1186 veml6030_runtime_resume, NULL); 1187 1188 static const struct veml603x_chip veml6030_chip = { 1189 .name = "veml6030", 1190 .scale_vals = &veml6030_scale_vals, 1191 .num_scale_vals = ARRAY_SIZE(veml6030_scale_vals), 1192 .channels = veml6030_channels, 1193 .num_channels = ARRAY_SIZE(veml6030_channels), 1194 .hw_init = veml6030_hw_init, 1195 .set_info = veml6030_set_info, 1196 .set_als_gain = veml6030_set_als_gain, 1197 .get_als_gain = veml6030_get_als_gain, 1198 }; 1199 1200 static const struct veml603x_chip veml6035_chip = { 1201 .name = "veml6035", 1202 .scale_vals = &veml6035_scale_vals, 1203 .num_scale_vals = ARRAY_SIZE(veml6035_scale_vals), 1204 .channels = veml6030_channels, 1205 .num_channels = ARRAY_SIZE(veml6030_channels), 1206 .hw_init = veml6035_hw_init, 1207 .set_info = veml6030_set_info, 1208 .set_als_gain = veml6035_set_als_gain, 1209 .get_als_gain = veml6035_get_als_gain, 1210 }; 1211 1212 static const struct veml603x_chip veml7700_chip = { 1213 .name = "veml7700", 1214 .scale_vals = &veml6030_scale_vals, 1215 .num_scale_vals = ARRAY_SIZE(veml6030_scale_vals), 1216 .channels = veml7700_channels, 1217 .num_channels = ARRAY_SIZE(veml7700_channels), 1218 .hw_init = veml6030_hw_init, 1219 .set_info = veml7700_set_info, 1220 .set_als_gain = veml6030_set_als_gain, 1221 .get_als_gain = veml6030_get_als_gain, 1222 }; 1223 1224 static const struct of_device_id veml6030_of_match[] = { 1225 { 1226 .compatible = "vishay,veml6030", 1227 .data = &veml6030_chip, 1228 }, 1229 { 1230 .compatible = "vishay,veml6035", 1231 .data = &veml6035_chip, 1232 }, 1233 { 1234 .compatible = "vishay,veml7700", 1235 .data = &veml7700_chip, 1236 }, 1237 { } 1238 }; 1239 MODULE_DEVICE_TABLE(of, veml6030_of_match); 1240 1241 static const struct i2c_device_id veml6030_id[] = { 1242 { "veml6030", (kernel_ulong_t)&veml6030_chip}, 1243 { "veml6035", (kernel_ulong_t)&veml6035_chip}, 1244 { "veml7700", (kernel_ulong_t)&veml7700_chip}, 1245 { } 1246 }; 1247 MODULE_DEVICE_TABLE(i2c, veml6030_id); 1248 1249 static struct i2c_driver veml6030_driver = { 1250 .driver = { 1251 .name = "veml6030", 1252 .of_match_table = veml6030_of_match, 1253 .pm = pm_ptr(&veml6030_pm_ops), 1254 }, 1255 .probe = veml6030_probe, 1256 .id_table = veml6030_id, 1257 }; 1258 module_i2c_driver(veml6030_driver); 1259 1260 MODULE_AUTHOR("Rishi Gupta <gupt21@gmail.com>"); 1261 MODULE_DESCRIPTION("VEML6030 Ambient Light Sensor"); 1262 MODULE_LICENSE("GPL v2"); 1263