1// SPDX-License-Identifier: GPL-2.0+ 2/* 3 * Device driver for monitoring ambient light intensity in (lux) and proximity 4 * detection (prox) within the TAOS TSL2571, TSL2671, TMD2671, TSL2771, TMD2771, 5 * TSL2572, TSL2672, TMD2672, TSL2772, and TMD2772 devices. 6 * 7 * Copyright (c) 2012, TAOS Corporation. 8 * Copyright (c) 2017-2018 Brian Masney <masneyb@onstation.org> 9 */ 10 11#include <linux/delay.h> 12#include <linux/errno.h> 13#include <linux/i2c.h> 14#include <linux/interrupt.h> 15#include <linux/kernel.h> 16#include <linux/module.h> 17#include <linux/mutex.h> 18#include <linux/slab.h> 19#include <linux/iio/events.h> 20#include <linux/iio/iio.h> 21#include <linux/iio/sysfs.h> 22#include <linux/platform_data/tsl2772.h> 23#include <linux/regulator/consumer.h> 24 25/* Cal defs */ 26#define PROX_STAT_CAL 0 27#define PROX_STAT_SAMP 1 28#define MAX_SAMPLES_CAL 200 29 30/* TSL2772 Device ID */ 31#define TRITON_ID 0x00 32#define SWORDFISH_ID 0x30 33#define HALIBUT_ID 0x20 34 35/* Lux calculation constants */ 36#define TSL2772_LUX_CALC_OVER_FLOW 65535 37 38/* 39 * TAOS Register definitions - Note: depending on device, some of these register 40 * are not used and the register address is benign. 41 */ 42 43/* Register offsets */ 44#define TSL2772_MAX_CONFIG_REG 16 45 46/* Device Registers and Masks */ 47#define TSL2772_CNTRL 0x00 48#define TSL2772_ALS_TIME 0X01 49#define TSL2772_PRX_TIME 0x02 50#define TSL2772_WAIT_TIME 0x03 51#define TSL2772_ALS_MINTHRESHLO 0X04 52#define TSL2772_ALS_MINTHRESHHI 0X05 53#define TSL2772_ALS_MAXTHRESHLO 0X06 54#define TSL2772_ALS_MAXTHRESHHI 0X07 55#define TSL2772_PRX_MINTHRESHLO 0X08 56#define TSL2772_PRX_MINTHRESHHI 0X09 57#define TSL2772_PRX_MAXTHRESHLO 0X0A 58#define TSL2772_PRX_MAXTHRESHHI 0X0B 59#define TSL2772_PERSISTENCE 0x0C 60#define TSL2772_ALS_PRX_CONFIG 0x0D 61#define TSL2772_PRX_COUNT 0x0E 62#define TSL2772_GAIN 0x0F 63#define TSL2772_NOTUSED 0x10 64#define TSL2772_REVID 0x11 65#define TSL2772_CHIPID 0x12 66#define TSL2772_STATUS 0x13 67#define TSL2772_ALS_CHAN0LO 0x14 68#define TSL2772_ALS_CHAN0HI 0x15 69#define TSL2772_ALS_CHAN1LO 0x16 70#define TSL2772_ALS_CHAN1HI 0x17 71#define TSL2772_PRX_LO 0x18 72#define TSL2772_PRX_HI 0x19 73 74/* tsl2772 cmd reg masks */ 75#define TSL2772_CMD_REG 0x80 76#define TSL2772_CMD_SPL_FN 0x60 77#define TSL2772_CMD_REPEAT_PROTO 0x00 78#define TSL2772_CMD_AUTOINC_PROTO 0x20 79 80#define TSL2772_CMD_PROX_INT_CLR 0X05 81#define TSL2772_CMD_ALS_INT_CLR 0x06 82#define TSL2772_CMD_PROXALS_INT_CLR 0X07 83 84/* tsl2772 cntrl reg masks */ 85#define TSL2772_CNTL_ADC_ENBL 0x02 86#define TSL2772_CNTL_PWR_ON 0x01 87 88/* tsl2772 status reg masks */ 89#define TSL2772_STA_ADC_VALID 0x01 90#define TSL2772_STA_PRX_VALID 0x02 91#define TSL2772_STA_ADC_PRX_VALID (TSL2772_STA_ADC_VALID | \ 92 TSL2772_STA_PRX_VALID) 93#define TSL2772_STA_ALS_INTR 0x10 94#define TSL2772_STA_PRX_INTR 0x20 95 96/* tsl2772 cntrl reg masks */ 97#define TSL2772_CNTL_REG_CLEAR 0x00 98#define TSL2772_CNTL_PROX_INT_ENBL 0X20 99#define TSL2772_CNTL_ALS_INT_ENBL 0X10 100#define TSL2772_CNTL_WAIT_TMR_ENBL 0X08 101#define TSL2772_CNTL_PROX_DET_ENBL 0X04 102#define TSL2772_CNTL_PWRON 0x01 103#define TSL2772_CNTL_ALSPON_ENBL 0x03 104#define TSL2772_CNTL_INTALSPON_ENBL 0x13 105#define TSL2772_CNTL_PROXPON_ENBL 0x0F 106#define TSL2772_CNTL_INTPROXPON_ENBL 0x2F 107 108#define TSL2772_ALS_GAIN_TRIM_MIN 250 109#define TSL2772_ALS_GAIN_TRIM_MAX 4000 110 111#define TSL2772_MAX_PROX_LEDS 2 112 113#define TSL2772_BOOT_MIN_SLEEP_TIME 10000 114#define TSL2772_BOOT_MAX_SLEEP_TIME 28000 115 116/* Device family members */ 117enum { 118 tsl2571, 119 tsl2671, 120 tmd2671, 121 tsl2771, 122 tmd2771, 123 tsl2572, 124 tsl2672, 125 tmd2672, 126 tsl2772, 127 tmd2772, 128 apds9930, 129}; 130 131enum { 132 TSL2772_CHIP_UNKNOWN = 0, 133 TSL2772_CHIP_WORKING = 1, 134 TSL2772_CHIP_SUSPENDED = 2 135}; 136 137enum { 138 TSL2772_SUPPLY_VDD = 0, 139 TSL2772_SUPPLY_VDDIO = 1, 140 TSL2772_NUM_SUPPLIES = 2 141}; 142 143/* Per-device data */ 144struct tsl2772_als_info { 145 u16 als_ch0; 146 u16 als_ch1; 147 u16 lux; 148}; 149 150struct tsl2772_chip_info { 151 int chan_table_elements; 152 struct iio_chan_spec channel_with_events[4]; 153 struct iio_chan_spec channel_without_events[4]; 154 const struct iio_info *info; 155}; 156 157static const int tsl2772_led_currents[][2] = { 158 { 100000, TSL2772_100_mA }, 159 { 50000, TSL2772_50_mA }, 160 { 25000, TSL2772_25_mA }, 161 { 13000, TSL2772_13_mA }, 162 { 0, 0 } 163}; 164 165struct tsl2772_chip { 166 kernel_ulong_t id; 167 struct mutex prox_mutex; 168 struct mutex als_mutex; 169 struct i2c_client *client; 170 struct regulator_bulk_data supplies[TSL2772_NUM_SUPPLIES]; 171 u16 prox_data; 172 struct tsl2772_als_info als_cur_info; 173 struct tsl2772_settings settings; 174 struct tsl2772_platform_data *pdata; 175 int als_gain_time_scale; 176 int als_saturation; 177 int tsl2772_chip_status; 178 u8 tsl2772_config[TSL2772_MAX_CONFIG_REG]; 179 const struct tsl2772_chip_info *chip_info; 180 const struct iio_info *info; 181 s64 event_timestamp; 182 /* 183 * This structure is intentionally large to accommodate 184 * updates via sysfs. 185 * Sized to 9 = max 8 segments + 1 termination segment 186 */ 187 struct tsl2772_lux tsl2772_device_lux[TSL2772_MAX_LUX_TABLE_SIZE]; 188}; 189 190/* 191 * Different devices require different coefficents, and these numbers were 192 * derived from the 'Lux Equation' section of the various device datasheets. 193 * All of these coefficients assume a Glass Attenuation (GA) factor of 1. 194 * The coefficients are multiplied by 1000 to avoid floating point operations. 195 * The two rows in each table correspond to the Lux1 and Lux2 equations from 196 * the datasheets. 197 */ 198static const struct tsl2772_lux tsl2x71_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = { 199 { 53000, 106000 }, 200 { 31800, 53000 }, 201 { 0, 0 }, 202}; 203 204static const struct tsl2772_lux tmd2x71_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = { 205 { 24000, 48000 }, 206 { 14400, 24000 }, 207 { 0, 0 }, 208}; 209 210static const struct tsl2772_lux tsl2x72_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = { 211 { 60000, 112200 }, 212 { 37800, 60000 }, 213 { 0, 0 }, 214}; 215 216static const struct tsl2772_lux tmd2x72_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = { 217 { 20000, 35000 }, 218 { 12600, 20000 }, 219 { 0, 0 }, 220}; 221 222static const struct tsl2772_lux apds9930_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = { 223 { 52000, 96824 }, 224 { 38792, 67132 }, 225 { 0, 0 }, 226}; 227 228static const struct tsl2772_lux *tsl2772_default_lux_table_group[] = { 229 [tsl2571] = tsl2x71_lux_table, 230 [tsl2671] = tsl2x71_lux_table, 231 [tmd2671] = tmd2x71_lux_table, 232 [tsl2771] = tsl2x71_lux_table, 233 [tmd2771] = tmd2x71_lux_table, 234 [tsl2572] = tsl2x72_lux_table, 235 [tsl2672] = tsl2x72_lux_table, 236 [tmd2672] = tmd2x72_lux_table, 237 [tsl2772] = tsl2x72_lux_table, 238 [tmd2772] = tmd2x72_lux_table, 239 [apds9930] = apds9930_lux_table, 240}; 241 242static const struct tsl2772_settings tsl2772_default_settings = { 243 .als_time = 255, /* 2.72 / 2.73 ms */ 244 .als_gain = 0, 245 .prox_time = 255, /* 2.72 / 2.73 ms */ 246 .prox_gain = 0, 247 .wait_time = 255, 248 .als_prox_config = 0, 249 .als_gain_trim = 1000, 250 .als_cal_target = 150, 251 .als_persistence = 1, 252 .als_interrupt_en = false, 253 .als_thresh_low = 200, 254 .als_thresh_high = 256, 255 .prox_persistence = 1, 256 .prox_interrupt_en = false, 257 .prox_thres_low = 0, 258 .prox_thres_high = 512, 259 .prox_max_samples_cal = 30, 260 .prox_pulse_count = 8, 261 .prox_diode = TSL2772_DIODE1, 262 .prox_power = TSL2772_100_mA 263}; 264 265static const s16 tsl2772_als_gain[] = { 266 1, 267 8, 268 16, 269 120 270}; 271 272static const s16 tsl2772_prox_gain[] = { 273 1, 274 2, 275 4, 276 8 277}; 278 279static const int tsl2772_int_time_avail[][6] = { 280 [tsl2571] = { 0, 2720, 0, 2720, 0, 696000 }, 281 [tsl2671] = { 0, 2720, 0, 2720, 0, 696000 }, 282 [tmd2671] = { 0, 2720, 0, 2720, 0, 696000 }, 283 [tsl2771] = { 0, 2720, 0, 2720, 0, 696000 }, 284 [tmd2771] = { 0, 2720, 0, 2720, 0, 696000 }, 285 [tsl2572] = { 0, 2730, 0, 2730, 0, 699000 }, 286 [tsl2672] = { 0, 2730, 0, 2730, 0, 699000 }, 287 [tmd2672] = { 0, 2730, 0, 2730, 0, 699000 }, 288 [tsl2772] = { 0, 2730, 0, 2730, 0, 699000 }, 289 [tmd2772] = { 0, 2730, 0, 2730, 0, 699000 }, 290 [apds9930] = { 0, 2730, 0, 2730, 0, 699000 }, 291}; 292 293static int tsl2772_int_calibscale_avail[] = { 1, 8, 16, 120 }; 294 295static int tsl2772_prox_calibscale_avail[] = { 1, 2, 4, 8 }; 296 297/* Channel variations */ 298enum { 299 ALS, 300 PRX, 301 ALSPRX, 302 PRX2, 303 ALSPRX2, 304}; 305 306static const u8 device_channel_config[] = { 307 [tsl2571] = ALS, 308 [tsl2671] = PRX, 309 [tmd2671] = PRX, 310 [tsl2771] = ALSPRX, 311 [tmd2771] = ALSPRX, 312 [tsl2572] = ALS, 313 [tsl2672] = PRX2, 314 [tmd2672] = PRX2, 315 [tsl2772] = ALSPRX2, 316 [tmd2772] = ALSPRX2, 317 [apds9930] = ALSPRX2, 318}; 319 320static int tsl2772_read_status(struct tsl2772_chip *chip) 321{ 322 int ret; 323 324 ret = i2c_smbus_read_byte_data(chip->client, 325 TSL2772_CMD_REG | TSL2772_STATUS); 326 if (ret < 0) 327 dev_err(&chip->client->dev, 328 "%s: failed to read STATUS register: %d\n", __func__, 329 ret); 330 331 return ret; 332} 333 334static int tsl2772_write_control_reg(struct tsl2772_chip *chip, u8 data) 335{ 336 int ret; 337 338 ret = i2c_smbus_write_byte_data(chip->client, 339 TSL2772_CMD_REG | TSL2772_CNTRL, data); 340 if (ret < 0) { 341 dev_err(&chip->client->dev, 342 "%s: failed to write to control register %x: %d\n", 343 __func__, data, ret); 344 } 345 346 return ret; 347} 348 349static int tsl2772_read_autoinc_regs(struct tsl2772_chip *chip, int lower_reg, 350 int upper_reg) 351{ 352 u8 buf[2]; 353 int ret; 354 355 ret = i2c_smbus_write_byte(chip->client, 356 TSL2772_CMD_REG | TSL2772_CMD_AUTOINC_PROTO | 357 lower_reg); 358 if (ret < 0) { 359 dev_err(&chip->client->dev, 360 "%s: failed to enable auto increment protocol: %d\n", 361 __func__, ret); 362 return ret; 363 } 364 365 ret = i2c_smbus_read_byte_data(chip->client, 366 TSL2772_CMD_REG | lower_reg); 367 if (ret < 0) { 368 dev_err(&chip->client->dev, 369 "%s: failed to read from register %x: %d\n", __func__, 370 lower_reg, ret); 371 return ret; 372 } 373 buf[0] = ret; 374 375 ret = i2c_smbus_read_byte_data(chip->client, 376 TSL2772_CMD_REG | upper_reg); 377 if (ret < 0) { 378 dev_err(&chip->client->dev, 379 "%s: failed to read from register %x: %d\n", __func__, 380 upper_reg, ret); 381 return ret; 382 } 383 buf[1] = ret; 384 385 ret = i2c_smbus_write_byte(chip->client, 386 TSL2772_CMD_REG | TSL2772_CMD_REPEAT_PROTO | 387 lower_reg); 388 if (ret < 0) { 389 dev_err(&chip->client->dev, 390 "%s: failed to enable repeated byte protocol: %d\n", 391 __func__, ret); 392 return ret; 393 } 394 395 return le16_to_cpup((const __le16 *)&buf[0]); 396} 397 398/** 399 * tsl2772_get_lux() - Reads and calculates current lux value. 400 * @indio_dev: pointer to IIO device 401 * 402 * The raw ch0 and ch1 values of the ambient light sensed in the last 403 * integration cycle are read from the device. The raw values are multiplied 404 * by a device-specific scale factor, and divided by the integration time and 405 * device gain. The code supports multiple lux equations through the lux table 406 * coefficients. A lux gain trim is applied to each lux equation, and then the 407 * maximum lux within the interval 0..65535 is selected. 408 */ 409static int tsl2772_get_lux(struct iio_dev *indio_dev) 410{ 411 struct tsl2772_chip *chip = iio_priv(indio_dev); 412 struct tsl2772_lux *p; 413 int max_lux, ret; 414 bool overflow; 415 416 mutex_lock(&chip->als_mutex); 417 418 if (chip->tsl2772_chip_status != TSL2772_CHIP_WORKING) { 419 dev_err(&chip->client->dev, "%s: device is not enabled\n", 420 __func__); 421 ret = -EBUSY; 422 goto out_unlock; 423 } 424 425 ret = tsl2772_read_status(chip); 426 if (ret < 0) 427 goto out_unlock; 428 429 if (!(ret & TSL2772_STA_ADC_VALID)) { 430 dev_err(&chip->client->dev, 431 "%s: data not valid yet\n", __func__); 432 ret = chip->als_cur_info.lux; /* return LAST VALUE */ 433 goto out_unlock; 434 } 435 436 ret = tsl2772_read_autoinc_regs(chip, TSL2772_ALS_CHAN0LO, 437 TSL2772_ALS_CHAN0HI); 438 if (ret < 0) 439 goto out_unlock; 440 chip->als_cur_info.als_ch0 = ret; 441 442 ret = tsl2772_read_autoinc_regs(chip, TSL2772_ALS_CHAN1LO, 443 TSL2772_ALS_CHAN1HI); 444 if (ret < 0) 445 goto out_unlock; 446 chip->als_cur_info.als_ch1 = ret; 447 448 if (chip->als_cur_info.als_ch0 >= chip->als_saturation) { 449 max_lux = TSL2772_LUX_CALC_OVER_FLOW; 450 goto update_struct_with_max_lux; 451 } 452 453 if (!chip->als_cur_info.als_ch0) { 454 /* have no data, so return LAST VALUE */ 455 ret = chip->als_cur_info.lux; 456 goto out_unlock; 457 } 458 459 max_lux = 0; 460 overflow = false; 461 for (p = (struct tsl2772_lux *)chip->tsl2772_device_lux; p->ch0 != 0; 462 p++) { 463 int lux; 464 465 lux = ((chip->als_cur_info.als_ch0 * p->ch0) - 466 (chip->als_cur_info.als_ch1 * p->ch1)) / 467 chip->als_gain_time_scale; 468 469 /* 470 * The als_gain_trim can have a value within the range 250..4000 471 * and is a multiplier for the lux. A trim of 1000 makes no 472 * changes to the lux, less than 1000 scales it down, and 473 * greater than 1000 scales it up. 474 */ 475 lux = (lux * chip->settings.als_gain_trim) / 1000; 476 477 if (lux > TSL2772_LUX_CALC_OVER_FLOW) { 478 overflow = true; 479 continue; 480 } 481 482 max_lux = max(max_lux, lux); 483 } 484 485 if (overflow && max_lux == 0) 486 max_lux = TSL2772_LUX_CALC_OVER_FLOW; 487 488update_struct_with_max_lux: 489 chip->als_cur_info.lux = max_lux; 490 ret = max_lux; 491 492out_unlock: 493 mutex_unlock(&chip->als_mutex); 494 495 return ret; 496} 497 498/** 499 * tsl2772_get_prox() - Reads proximity data registers and updates 500 * chip->prox_data. 501 * 502 * @indio_dev: pointer to IIO device 503 */ 504static int tsl2772_get_prox(struct iio_dev *indio_dev) 505{ 506 struct tsl2772_chip *chip = iio_priv(indio_dev); 507 int ret; 508 509 mutex_lock(&chip->prox_mutex); 510 511 ret = tsl2772_read_status(chip); 512 if (ret < 0) 513 goto prox_poll_err; 514 515 switch (chip->id) { 516 case tsl2571: 517 case tsl2671: 518 case tmd2671: 519 case tsl2771: 520 case tmd2771: 521 if (!(ret & TSL2772_STA_ADC_VALID)) { 522 ret = -EINVAL; 523 goto prox_poll_err; 524 } 525 break; 526 case tsl2572: 527 case tsl2672: 528 case tmd2672: 529 case tsl2772: 530 case tmd2772: 531 case apds9930: 532 if (!(ret & TSL2772_STA_PRX_VALID)) { 533 ret = -EINVAL; 534 goto prox_poll_err; 535 } 536 break; 537 } 538 539 ret = tsl2772_read_autoinc_regs(chip, TSL2772_PRX_LO, TSL2772_PRX_HI); 540 if (ret < 0) 541 goto prox_poll_err; 542 chip->prox_data = ret; 543 544prox_poll_err: 545 mutex_unlock(&chip->prox_mutex); 546 547 return ret; 548} 549 550static int tsl2772_read_prox_led_current(struct tsl2772_chip *chip) 551{ 552 struct device_node *of_node = chip->client->dev.of_node; 553 int ret, tmp, i; 554 555 ret = of_property_read_u32(of_node, "led-max-microamp", &tmp); 556 if (ret < 0) 557 return ret; 558 559 for (i = 0; tsl2772_led_currents[i][0] != 0; i++) { 560 if (tmp == tsl2772_led_currents[i][0]) { 561 chip->settings.prox_power = tsl2772_led_currents[i][1]; 562 return 0; 563 } 564 } 565 566 dev_err(&chip->client->dev, "Invalid value %d for led-max-microamp\n", 567 tmp); 568 569 return -EINVAL; 570 571} 572 573static int tsl2772_read_prox_diodes(struct tsl2772_chip *chip) 574{ 575 struct device_node *of_node = chip->client->dev.of_node; 576 int i, ret, num_leds, prox_diode_mask; 577 u32 leds[TSL2772_MAX_PROX_LEDS]; 578 579 ret = of_property_count_u32_elems(of_node, "amstaos,proximity-diodes"); 580 if (ret < 0) 581 return ret; 582 583 num_leds = ret; 584 if (num_leds > TSL2772_MAX_PROX_LEDS) 585 num_leds = TSL2772_MAX_PROX_LEDS; 586 587 ret = of_property_read_u32_array(of_node, "amstaos,proximity-diodes", 588 leds, num_leds); 589 if (ret < 0) { 590 dev_err(&chip->client->dev, 591 "Invalid value for amstaos,proximity-diodes: %d.\n", 592 ret); 593 return ret; 594 } 595 596 prox_diode_mask = 0; 597 for (i = 0; i < num_leds; i++) { 598 if (leds[i] == 0) 599 prox_diode_mask |= TSL2772_DIODE0; 600 else if (leds[i] == 1) 601 prox_diode_mask |= TSL2772_DIODE1; 602 else { 603 dev_err(&chip->client->dev, 604 "Invalid value %d in amstaos,proximity-diodes.\n", 605 leds[i]); 606 return -EINVAL; 607 } 608 } 609 chip->settings.prox_diode = prox_diode_mask; 610 611 return 0; 612} 613 614static void tsl2772_parse_dt(struct tsl2772_chip *chip) 615{ 616 tsl2772_read_prox_led_current(chip); 617 tsl2772_read_prox_diodes(chip); 618} 619 620/** 621 * tsl2772_defaults() - Populates the device nominal operating parameters 622 * with those provided by a 'platform' data struct or 623 * with prefined defaults. 624 * 625 * @chip: pointer to device structure. 626 */ 627static void tsl2772_defaults(struct tsl2772_chip *chip) 628{ 629 /* If Operational settings defined elsewhere.. */ 630 if (chip->pdata && chip->pdata->platform_default_settings) 631 memcpy(&chip->settings, chip->pdata->platform_default_settings, 632 sizeof(tsl2772_default_settings)); 633 else 634 memcpy(&chip->settings, &tsl2772_default_settings, 635 sizeof(tsl2772_default_settings)); 636 637 /* Load up the proper lux table. */ 638 if (chip->pdata && chip->pdata->platform_lux_table[0].ch0 != 0) 639 memcpy(chip->tsl2772_device_lux, 640 chip->pdata->platform_lux_table, 641 sizeof(chip->pdata->platform_lux_table)); 642 else 643 memcpy(chip->tsl2772_device_lux, 644 tsl2772_default_lux_table_group[chip->id], 645 TSL2772_DEFAULT_TABLE_BYTES); 646 647 tsl2772_parse_dt(chip); 648} 649 650/** 651 * tsl2772_als_calibrate() - Obtain single reading and calculate 652 * the als_gain_trim. 653 * 654 * @indio_dev: pointer to IIO device 655 */ 656static int tsl2772_als_calibrate(struct iio_dev *indio_dev) 657{ 658 struct tsl2772_chip *chip = iio_priv(indio_dev); 659 int ret, lux_val; 660 661 ret = i2c_smbus_read_byte_data(chip->client, 662 TSL2772_CMD_REG | TSL2772_CNTRL); 663 if (ret < 0) { 664 dev_err(&chip->client->dev, 665 "%s: failed to read from the CNTRL register\n", 666 __func__); 667 return ret; 668 } 669 670 if ((ret & (TSL2772_CNTL_ADC_ENBL | TSL2772_CNTL_PWR_ON)) 671 != (TSL2772_CNTL_ADC_ENBL | TSL2772_CNTL_PWR_ON)) { 672 dev_err(&chip->client->dev, 673 "%s: Device is not powered on and/or ADC is not enabled\n", 674 __func__); 675 return -EINVAL; 676 } else if ((ret & TSL2772_STA_ADC_VALID) != TSL2772_STA_ADC_VALID) { 677 dev_err(&chip->client->dev, 678 "%s: The two ADC channels have not completed an integration cycle\n", 679 __func__); 680 return -ENODATA; 681 } 682 683 lux_val = tsl2772_get_lux(indio_dev); 684 if (lux_val < 0) { 685 dev_err(&chip->client->dev, 686 "%s: failed to get lux\n", __func__); 687 return lux_val; 688 } 689 if (lux_val == 0) 690 return -ERANGE; 691 692 ret = (chip->settings.als_cal_target * chip->settings.als_gain_trim) / 693 lux_val; 694 if (ret < TSL2772_ALS_GAIN_TRIM_MIN || ret > TSL2772_ALS_GAIN_TRIM_MAX) 695 return -ERANGE; 696 697 chip->settings.als_gain_trim = ret; 698 699 return ret; 700} 701 702static void tsl2772_disable_regulators_action(void *_data) 703{ 704 struct tsl2772_chip *chip = _data; 705 706 regulator_bulk_disable(ARRAY_SIZE(chip->supplies), chip->supplies); 707} 708 709static int tsl2772_chip_on(struct iio_dev *indio_dev) 710{ 711 struct tsl2772_chip *chip = iio_priv(indio_dev); 712 int ret, i, als_count, als_time_us; 713 u8 *dev_reg, reg_val; 714 715 /* Non calculated parameters */ 716 chip->tsl2772_config[TSL2772_ALS_TIME] = chip->settings.als_time; 717 chip->tsl2772_config[TSL2772_PRX_TIME] = chip->settings.prox_time; 718 chip->tsl2772_config[TSL2772_WAIT_TIME] = chip->settings.wait_time; 719 chip->tsl2772_config[TSL2772_ALS_PRX_CONFIG] = 720 chip->settings.als_prox_config; 721 722 chip->tsl2772_config[TSL2772_ALS_MINTHRESHLO] = 723 (chip->settings.als_thresh_low) & 0xFF; 724 chip->tsl2772_config[TSL2772_ALS_MINTHRESHHI] = 725 (chip->settings.als_thresh_low >> 8) & 0xFF; 726 chip->tsl2772_config[TSL2772_ALS_MAXTHRESHLO] = 727 (chip->settings.als_thresh_high) & 0xFF; 728 chip->tsl2772_config[TSL2772_ALS_MAXTHRESHHI] = 729 (chip->settings.als_thresh_high >> 8) & 0xFF; 730 chip->tsl2772_config[TSL2772_PERSISTENCE] = 731 (chip->settings.prox_persistence & 0xFF) << 4 | 732 (chip->settings.als_persistence & 0xFF); 733 734 chip->tsl2772_config[TSL2772_PRX_COUNT] = 735 chip->settings.prox_pulse_count; 736 chip->tsl2772_config[TSL2772_PRX_MINTHRESHLO] = 737 (chip->settings.prox_thres_low) & 0xFF; 738 chip->tsl2772_config[TSL2772_PRX_MINTHRESHHI] = 739 (chip->settings.prox_thres_low >> 8) & 0xFF; 740 chip->tsl2772_config[TSL2772_PRX_MAXTHRESHLO] = 741 (chip->settings.prox_thres_high) & 0xFF; 742 chip->tsl2772_config[TSL2772_PRX_MAXTHRESHHI] = 743 (chip->settings.prox_thres_high >> 8) & 0xFF; 744 745 /* and make sure we're not already on */ 746 if (chip->tsl2772_chip_status == TSL2772_CHIP_WORKING) { 747 /* if forcing a register update - turn off, then on */ 748 dev_info(&chip->client->dev, "device is already enabled\n"); 749 return -EINVAL; 750 } 751 752 /* Set the gain based on tsl2772_settings struct */ 753 chip->tsl2772_config[TSL2772_GAIN] = 754 (chip->settings.als_gain & 0xFF) | 755 ((chip->settings.prox_gain & 0xFF) << 2) | 756 (chip->settings.prox_diode << 4) | 757 (chip->settings.prox_power << 6); 758 759 /* set chip time scaling and saturation */ 760 als_count = 256 - chip->settings.als_time; 761 als_time_us = als_count * tsl2772_int_time_avail[chip->id][3]; 762 chip->als_saturation = als_count * 768; /* 75% of full scale */ 763 chip->als_gain_time_scale = als_time_us * 764 tsl2772_als_gain[chip->settings.als_gain]; 765 766 /* 767 * TSL2772 Specific power-on / adc enable sequence 768 * Power on the device 1st. 769 */ 770 ret = tsl2772_write_control_reg(chip, TSL2772_CNTL_PWR_ON); 771 if (ret < 0) 772 return ret; 773 774 /* 775 * Use the following shadow copy for our delay before enabling ADC. 776 * Write all the registers. 777 */ 778 for (i = 0, dev_reg = chip->tsl2772_config; 779 i < TSL2772_MAX_CONFIG_REG; i++) { 780 int reg = TSL2772_CMD_REG + i; 781 782 ret = i2c_smbus_write_byte_data(chip->client, reg, 783 *dev_reg++); 784 if (ret < 0) { 785 dev_err(&chip->client->dev, 786 "%s: failed to write to register %x: %d\n", 787 __func__, reg, ret); 788 return ret; 789 } 790 } 791 792 /* Power-on settling time */ 793 usleep_range(3000, 3500); 794 795 reg_val = TSL2772_CNTL_PWR_ON | TSL2772_CNTL_ADC_ENBL | 796 TSL2772_CNTL_PROX_DET_ENBL; 797 if (chip->settings.als_interrupt_en) 798 reg_val |= TSL2772_CNTL_ALS_INT_ENBL; 799 if (chip->settings.prox_interrupt_en) 800 reg_val |= TSL2772_CNTL_PROX_INT_ENBL; 801 802 ret = tsl2772_write_control_reg(chip, reg_val); 803 if (ret < 0) 804 return ret; 805 806 ret = i2c_smbus_write_byte(chip->client, 807 TSL2772_CMD_REG | TSL2772_CMD_SPL_FN | 808 TSL2772_CMD_PROXALS_INT_CLR); 809 if (ret < 0) { 810 dev_err(&chip->client->dev, 811 "%s: failed to clear interrupt status: %d\n", 812 __func__, ret); 813 return ret; 814 } 815 816 chip->tsl2772_chip_status = TSL2772_CHIP_WORKING; 817 818 return ret; 819} 820 821static int tsl2772_chip_off(struct iio_dev *indio_dev) 822{ 823 struct tsl2772_chip *chip = iio_priv(indio_dev); 824 825 /* turn device off */ 826 chip->tsl2772_chip_status = TSL2772_CHIP_SUSPENDED; 827 return tsl2772_write_control_reg(chip, 0x00); 828} 829 830static void tsl2772_chip_off_action(void *data) 831{ 832 struct iio_dev *indio_dev = data; 833 834 tsl2772_chip_off(indio_dev); 835} 836 837/** 838 * tsl2772_invoke_change - power cycle the device to implement the user 839 * parameters 840 * @indio_dev: pointer to IIO device 841 * 842 * Obtain and lock both ALS and PROX resources, determine and save device state 843 * (On/Off), cycle device to implement updated parameter, put device back into 844 * proper state, and unlock resource. 845 */ 846static int tsl2772_invoke_change(struct iio_dev *indio_dev) 847{ 848 struct tsl2772_chip *chip = iio_priv(indio_dev); 849 int device_status = chip->tsl2772_chip_status; 850 int ret; 851 852 mutex_lock(&chip->als_mutex); 853 mutex_lock(&chip->prox_mutex); 854 855 if (device_status == TSL2772_CHIP_WORKING) { 856 ret = tsl2772_chip_off(indio_dev); 857 if (ret < 0) 858 goto unlock; 859 } 860 861 ret = tsl2772_chip_on(indio_dev); 862 863unlock: 864 mutex_unlock(&chip->prox_mutex); 865 mutex_unlock(&chip->als_mutex); 866 867 return ret; 868} 869 870static int tsl2772_prox_cal(struct iio_dev *indio_dev) 871{ 872 struct tsl2772_chip *chip = iio_priv(indio_dev); 873 int prox_history[MAX_SAMPLES_CAL + 1]; 874 int i, ret, mean, max, sample_sum; 875 876 if (chip->settings.prox_max_samples_cal < 1 || 877 chip->settings.prox_max_samples_cal > MAX_SAMPLES_CAL) 878 return -EINVAL; 879 880 for (i = 0; i < chip->settings.prox_max_samples_cal; i++) { 881 usleep_range(15000, 17500); 882 ret = tsl2772_get_prox(indio_dev); 883 if (ret < 0) 884 return ret; 885 886 prox_history[i] = chip->prox_data; 887 } 888 889 sample_sum = 0; 890 max = INT_MIN; 891 for (i = 0; i < chip->settings.prox_max_samples_cal; i++) { 892 sample_sum += prox_history[i]; 893 max = max(max, prox_history[i]); 894 } 895 mean = sample_sum / chip->settings.prox_max_samples_cal; 896 897 chip->settings.prox_thres_high = (max << 1) - mean; 898 899 return tsl2772_invoke_change(indio_dev); 900} 901 902static int tsl2772_read_avail(struct iio_dev *indio_dev, 903 struct iio_chan_spec const *chan, 904 const int **vals, int *type, int *length, 905 long mask) 906{ 907 struct tsl2772_chip *chip = iio_priv(indio_dev); 908 909 switch (mask) { 910 case IIO_CHAN_INFO_CALIBSCALE: 911 if (chan->type == IIO_INTENSITY) { 912 *length = ARRAY_SIZE(tsl2772_int_calibscale_avail); 913 *vals = tsl2772_int_calibscale_avail; 914 } else { 915 *length = ARRAY_SIZE(tsl2772_prox_calibscale_avail); 916 *vals = tsl2772_prox_calibscale_avail; 917 } 918 *type = IIO_VAL_INT; 919 return IIO_AVAIL_LIST; 920 case IIO_CHAN_INFO_INT_TIME: 921 *length = ARRAY_SIZE(tsl2772_int_time_avail[chip->id]); 922 *vals = tsl2772_int_time_avail[chip->id]; 923 *type = IIO_VAL_INT_PLUS_MICRO; 924 return IIO_AVAIL_RANGE; 925 } 926 927 return -EINVAL; 928} 929 930static ssize_t in_illuminance0_target_input_show(struct device *dev, 931 struct device_attribute *attr, 932 char *buf) 933{ 934 struct tsl2772_chip *chip = iio_priv(dev_to_iio_dev(dev)); 935 936 return scnprintf(buf, PAGE_SIZE, "%d\n", chip->settings.als_cal_target); 937} 938 939static ssize_t in_illuminance0_target_input_store(struct device *dev, 940 struct device_attribute *attr, 941 const char *buf, size_t len) 942{ 943 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 944 struct tsl2772_chip *chip = iio_priv(indio_dev); 945 u16 value; 946 int ret; 947 948 if (kstrtou16(buf, 0, &value)) 949 return -EINVAL; 950 951 chip->settings.als_cal_target = value; 952 ret = tsl2772_invoke_change(indio_dev); 953 if (ret < 0) 954 return ret; 955 956 return len; 957} 958 959static ssize_t in_illuminance0_calibrate_store(struct device *dev, 960 struct device_attribute *attr, 961 const char *buf, size_t len) 962{ 963 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 964 bool value; 965 int ret; 966 967 if (kstrtobool(buf, &value) || !value) 968 return -EINVAL; 969 970 ret = tsl2772_als_calibrate(indio_dev); 971 if (ret < 0) 972 return ret; 973 974 ret = tsl2772_invoke_change(indio_dev); 975 if (ret < 0) 976 return ret; 977 978 return len; 979} 980 981static ssize_t in_illuminance0_lux_table_show(struct device *dev, 982 struct device_attribute *attr, 983 char *buf) 984{ 985 struct tsl2772_chip *chip = iio_priv(dev_to_iio_dev(dev)); 986 int i = 0; 987 int offset = 0; 988 989 while (i < TSL2772_MAX_LUX_TABLE_SIZE) { 990 offset += scnprintf(buf + offset, PAGE_SIZE - offset, "%u,%u,", 991 chip->tsl2772_device_lux[i].ch0, 992 chip->tsl2772_device_lux[i].ch1); 993 if (chip->tsl2772_device_lux[i].ch0 == 0) { 994 /* 995 * We just printed the first "0" entry. 996 * Now get rid of the extra "," and break. 997 */ 998 offset--; 999 break; 1000 } 1001 i++; 1002 } 1003 1004 offset += scnprintf(buf + offset, PAGE_SIZE - offset, "\n"); 1005 return offset; 1006} 1007 1008static ssize_t in_illuminance0_lux_table_store(struct device *dev, 1009 struct device_attribute *attr, 1010 const char *buf, size_t len) 1011{ 1012 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 1013 struct tsl2772_chip *chip = iio_priv(indio_dev); 1014 int value[ARRAY_SIZE(chip->tsl2772_device_lux) * 2 + 1]; 1015 int n, ret; 1016 1017 get_options(buf, ARRAY_SIZE(value), value); 1018 1019 /* 1020 * We now have an array of ints starting at value[1], and 1021 * enumerated by value[0]. 1022 * We expect each group of two ints to be one table entry, 1023 * and the last table entry is all 0. 1024 */ 1025 n = value[0]; 1026 if ((n % 2) || n < 4 || 1027 n > ((ARRAY_SIZE(chip->tsl2772_device_lux) - 1) * 2)) 1028 return -EINVAL; 1029 1030 if ((value[(n - 1)] | value[n]) != 0) 1031 return -EINVAL; 1032 1033 if (chip->tsl2772_chip_status == TSL2772_CHIP_WORKING) { 1034 ret = tsl2772_chip_off(indio_dev); 1035 if (ret < 0) 1036 return ret; 1037 } 1038 1039 /* Zero out the table */ 1040 memset(chip->tsl2772_device_lux, 0, sizeof(chip->tsl2772_device_lux)); 1041 memcpy(chip->tsl2772_device_lux, &value[1], (value[0] * 4)); 1042 1043 ret = tsl2772_invoke_change(indio_dev); 1044 if (ret < 0) 1045 return ret; 1046 1047 return len; 1048} 1049 1050static ssize_t in_proximity0_calibrate_store(struct device *dev, 1051 struct device_attribute *attr, 1052 const char *buf, size_t len) 1053{ 1054 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 1055 bool value; 1056 int ret; 1057 1058 if (kstrtobool(buf, &value) || !value) 1059 return -EINVAL; 1060 1061 ret = tsl2772_prox_cal(indio_dev); 1062 if (ret < 0) 1063 return ret; 1064 1065 ret = tsl2772_invoke_change(indio_dev); 1066 if (ret < 0) 1067 return ret; 1068 1069 return len; 1070} 1071 1072static int tsl2772_read_interrupt_config(struct iio_dev *indio_dev, 1073 const struct iio_chan_spec *chan, 1074 enum iio_event_type type, 1075 enum iio_event_direction dir) 1076{ 1077 struct tsl2772_chip *chip = iio_priv(indio_dev); 1078 1079 if (chan->type == IIO_INTENSITY) 1080 return chip->settings.als_interrupt_en; 1081 else 1082 return chip->settings.prox_interrupt_en; 1083} 1084 1085static int tsl2772_write_interrupt_config(struct iio_dev *indio_dev, 1086 const struct iio_chan_spec *chan, 1087 enum iio_event_type type, 1088 enum iio_event_direction dir, 1089 int val) 1090{ 1091 struct tsl2772_chip *chip = iio_priv(indio_dev); 1092 1093 if (chan->type == IIO_INTENSITY) 1094 chip->settings.als_interrupt_en = val ? true : false; 1095 else 1096 chip->settings.prox_interrupt_en = val ? true : false; 1097 1098 return tsl2772_invoke_change(indio_dev); 1099} 1100 1101static int tsl2772_write_event_value(struct iio_dev *indio_dev, 1102 const struct iio_chan_spec *chan, 1103 enum iio_event_type type, 1104 enum iio_event_direction dir, 1105 enum iio_event_info info, 1106 int val, int val2) 1107{ 1108 struct tsl2772_chip *chip = iio_priv(indio_dev); 1109 int ret = -EINVAL, count, persistence; 1110 u8 time; 1111 1112 switch (info) { 1113 case IIO_EV_INFO_VALUE: 1114 if (chan->type == IIO_INTENSITY) { 1115 switch (dir) { 1116 case IIO_EV_DIR_RISING: 1117 chip->settings.als_thresh_high = val; 1118 ret = 0; 1119 break; 1120 case IIO_EV_DIR_FALLING: 1121 chip->settings.als_thresh_low = val; 1122 ret = 0; 1123 break; 1124 default: 1125 break; 1126 } 1127 } else { 1128 switch (dir) { 1129 case IIO_EV_DIR_RISING: 1130 chip->settings.prox_thres_high = val; 1131 ret = 0; 1132 break; 1133 case IIO_EV_DIR_FALLING: 1134 chip->settings.prox_thres_low = val; 1135 ret = 0; 1136 break; 1137 default: 1138 break; 1139 } 1140 } 1141 break; 1142 case IIO_EV_INFO_PERIOD: 1143 if (chan->type == IIO_INTENSITY) 1144 time = chip->settings.als_time; 1145 else 1146 time = chip->settings.prox_time; 1147 1148 count = 256 - time; 1149 persistence = ((val * 1000000) + val2) / 1150 (count * tsl2772_int_time_avail[chip->id][3]); 1151 1152 if (chan->type == IIO_INTENSITY) { 1153 /* ALS filter values are 1, 2, 3, 5, 10, 15, ..., 60 */ 1154 if (persistence > 3) 1155 persistence = (persistence / 5) + 3; 1156 1157 chip->settings.als_persistence = persistence; 1158 } else { 1159 chip->settings.prox_persistence = persistence; 1160 } 1161 1162 ret = 0; 1163 break; 1164 default: 1165 break; 1166 } 1167 1168 if (ret < 0) 1169 return ret; 1170 1171 return tsl2772_invoke_change(indio_dev); 1172} 1173 1174static int tsl2772_read_event_value(struct iio_dev *indio_dev, 1175 const struct iio_chan_spec *chan, 1176 enum iio_event_type type, 1177 enum iio_event_direction dir, 1178 enum iio_event_info info, 1179 int *val, int *val2) 1180{ 1181 struct tsl2772_chip *chip = iio_priv(indio_dev); 1182 int filter_delay, persistence; 1183 u8 time; 1184 1185 switch (info) { 1186 case IIO_EV_INFO_VALUE: 1187 if (chan->type == IIO_INTENSITY) { 1188 switch (dir) { 1189 case IIO_EV_DIR_RISING: 1190 *val = chip->settings.als_thresh_high; 1191 return IIO_VAL_INT; 1192 case IIO_EV_DIR_FALLING: 1193 *val = chip->settings.als_thresh_low; 1194 return IIO_VAL_INT; 1195 default: 1196 return -EINVAL; 1197 } 1198 } else { 1199 switch (dir) { 1200 case IIO_EV_DIR_RISING: 1201 *val = chip->settings.prox_thres_high; 1202 return IIO_VAL_INT; 1203 case IIO_EV_DIR_FALLING: 1204 *val = chip->settings.prox_thres_low; 1205 return IIO_VAL_INT; 1206 default: 1207 return -EINVAL; 1208 } 1209 } 1210 break; 1211 case IIO_EV_INFO_PERIOD: 1212 if (chan->type == IIO_INTENSITY) { 1213 time = chip->settings.als_time; 1214 persistence = chip->settings.als_persistence; 1215 1216 /* ALS filter values are 1, 2, 3, 5, 10, 15, ..., 60 */ 1217 if (persistence > 3) 1218 persistence = (persistence - 3) * 5; 1219 } else { 1220 time = chip->settings.prox_time; 1221 persistence = chip->settings.prox_persistence; 1222 } 1223 1224 filter_delay = persistence * (256 - time) * 1225 tsl2772_int_time_avail[chip->id][3]; 1226 1227 *val = filter_delay / 1000000; 1228 *val2 = filter_delay % 1000000; 1229 return IIO_VAL_INT_PLUS_MICRO; 1230 default: 1231 return -EINVAL; 1232 } 1233} 1234 1235static int tsl2772_read_raw(struct iio_dev *indio_dev, 1236 struct iio_chan_spec const *chan, 1237 int *val, 1238 int *val2, 1239 long mask) 1240{ 1241 struct tsl2772_chip *chip = iio_priv(indio_dev); 1242 1243 switch (mask) { 1244 case IIO_CHAN_INFO_PROCESSED: 1245 switch (chan->type) { 1246 case IIO_LIGHT: 1247 tsl2772_get_lux(indio_dev); 1248 *val = chip->als_cur_info.lux; 1249 return IIO_VAL_INT; 1250 default: 1251 return -EINVAL; 1252 } 1253 case IIO_CHAN_INFO_RAW: 1254 switch (chan->type) { 1255 case IIO_INTENSITY: 1256 tsl2772_get_lux(indio_dev); 1257 if (chan->channel == 0) 1258 *val = chip->als_cur_info.als_ch0; 1259 else 1260 *val = chip->als_cur_info.als_ch1; 1261 return IIO_VAL_INT; 1262 case IIO_PROXIMITY: 1263 tsl2772_get_prox(indio_dev); 1264 *val = chip->prox_data; 1265 return IIO_VAL_INT; 1266 default: 1267 return -EINVAL; 1268 } 1269 break; 1270 case IIO_CHAN_INFO_CALIBSCALE: 1271 if (chan->type == IIO_LIGHT) 1272 *val = tsl2772_als_gain[chip->settings.als_gain]; 1273 else 1274 *val = tsl2772_prox_gain[chip->settings.prox_gain]; 1275 return IIO_VAL_INT; 1276 case IIO_CHAN_INFO_CALIBBIAS: 1277 *val = chip->settings.als_gain_trim; 1278 return IIO_VAL_INT; 1279 case IIO_CHAN_INFO_INT_TIME: 1280 *val = 0; 1281 *val2 = (256 - chip->settings.als_time) * 1282 tsl2772_int_time_avail[chip->id][3]; 1283 return IIO_VAL_INT_PLUS_MICRO; 1284 default: 1285 return -EINVAL; 1286 } 1287} 1288 1289static int tsl2772_write_raw(struct iio_dev *indio_dev, 1290 struct iio_chan_spec const *chan, 1291 int val, 1292 int val2, 1293 long mask) 1294{ 1295 struct tsl2772_chip *chip = iio_priv(indio_dev); 1296 1297 switch (mask) { 1298 case IIO_CHAN_INFO_CALIBSCALE: 1299 if (chan->type == IIO_INTENSITY) { 1300 switch (val) { 1301 case 1: 1302 chip->settings.als_gain = 0; 1303 break; 1304 case 8: 1305 chip->settings.als_gain = 1; 1306 break; 1307 case 16: 1308 chip->settings.als_gain = 2; 1309 break; 1310 case 120: 1311 chip->settings.als_gain = 3; 1312 break; 1313 default: 1314 return -EINVAL; 1315 } 1316 } else { 1317 switch (val) { 1318 case 1: 1319 chip->settings.prox_gain = 0; 1320 break; 1321 case 2: 1322 chip->settings.prox_gain = 1; 1323 break; 1324 case 4: 1325 chip->settings.prox_gain = 2; 1326 break; 1327 case 8: 1328 chip->settings.prox_gain = 3; 1329 break; 1330 default: 1331 return -EINVAL; 1332 } 1333 } 1334 break; 1335 case IIO_CHAN_INFO_CALIBBIAS: 1336 if (val < TSL2772_ALS_GAIN_TRIM_MIN || 1337 val > TSL2772_ALS_GAIN_TRIM_MAX) 1338 return -EINVAL; 1339 1340 chip->settings.als_gain_trim = val; 1341 break; 1342 case IIO_CHAN_INFO_INT_TIME: 1343 if (val != 0 || val2 < tsl2772_int_time_avail[chip->id][1] || 1344 val2 > tsl2772_int_time_avail[chip->id][5]) 1345 return -EINVAL; 1346 1347 chip->settings.als_time = 256 - 1348 (val2 / tsl2772_int_time_avail[chip->id][3]); 1349 break; 1350 default: 1351 return -EINVAL; 1352 } 1353 1354 return tsl2772_invoke_change(indio_dev); 1355} 1356 1357static DEVICE_ATTR_RW(in_illuminance0_target_input); 1358 1359static DEVICE_ATTR_WO(in_illuminance0_calibrate); 1360 1361static DEVICE_ATTR_WO(in_proximity0_calibrate); 1362 1363static DEVICE_ATTR_RW(in_illuminance0_lux_table); 1364 1365/* Use the default register values to identify the Taos device */ 1366static int tsl2772_device_id_verif(int id, int target) 1367{ 1368 switch (target) { 1369 case tsl2571: 1370 case tsl2671: 1371 case tsl2771: 1372 return (id & 0xf0) == TRITON_ID; 1373 case tmd2671: 1374 case tmd2771: 1375 return (id & 0xf0) == HALIBUT_ID; 1376 case tsl2572: 1377 case tsl2672: 1378 case tmd2672: 1379 case tsl2772: 1380 case tmd2772: 1381 case apds9930: 1382 return (id & 0xf0) == SWORDFISH_ID; 1383 } 1384 1385 return -EINVAL; 1386} 1387 1388static irqreturn_t tsl2772_event_handler(int irq, void *private) 1389{ 1390 struct iio_dev *indio_dev = private; 1391 struct tsl2772_chip *chip = iio_priv(indio_dev); 1392 s64 timestamp = iio_get_time_ns(indio_dev); 1393 int ret; 1394 1395 ret = tsl2772_read_status(chip); 1396 if (ret < 0) 1397 return IRQ_HANDLED; 1398 1399 /* What type of interrupt do we need to process */ 1400 if (ret & TSL2772_STA_PRX_INTR) { 1401 iio_push_event(indio_dev, 1402 IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 1403 0, 1404 IIO_EV_TYPE_THRESH, 1405 IIO_EV_DIR_EITHER), 1406 timestamp); 1407 } 1408 1409 if (ret & TSL2772_STA_ALS_INTR) { 1410 iio_push_event(indio_dev, 1411 IIO_UNMOD_EVENT_CODE(IIO_LIGHT, 1412 0, 1413 IIO_EV_TYPE_THRESH, 1414 IIO_EV_DIR_EITHER), 1415 timestamp); 1416 } 1417 1418 ret = i2c_smbus_write_byte(chip->client, 1419 TSL2772_CMD_REG | TSL2772_CMD_SPL_FN | 1420 TSL2772_CMD_PROXALS_INT_CLR); 1421 if (ret < 0) 1422 dev_err(&chip->client->dev, 1423 "%s: failed to clear interrupt status: %d\n", 1424 __func__, ret); 1425 1426 return IRQ_HANDLED; 1427} 1428 1429static struct attribute *tsl2772_ALS_device_attrs[] = { 1430 &dev_attr_in_illuminance0_target_input.attr, 1431 &dev_attr_in_illuminance0_calibrate.attr, 1432 &dev_attr_in_illuminance0_lux_table.attr, 1433 NULL 1434}; 1435 1436static struct attribute *tsl2772_PRX_device_attrs[] = { 1437 &dev_attr_in_proximity0_calibrate.attr, 1438 NULL 1439}; 1440 1441static struct attribute *tsl2772_ALSPRX_device_attrs[] = { 1442 &dev_attr_in_illuminance0_target_input.attr, 1443 &dev_attr_in_illuminance0_calibrate.attr, 1444 &dev_attr_in_illuminance0_lux_table.attr, 1445 NULL 1446}; 1447 1448static struct attribute *tsl2772_PRX2_device_attrs[] = { 1449 &dev_attr_in_proximity0_calibrate.attr, 1450 NULL 1451}; 1452 1453static struct attribute *tsl2772_ALSPRX2_device_attrs[] = { 1454 &dev_attr_in_illuminance0_target_input.attr, 1455 &dev_attr_in_illuminance0_calibrate.attr, 1456 &dev_attr_in_illuminance0_lux_table.attr, 1457 &dev_attr_in_proximity0_calibrate.attr, 1458 NULL 1459}; 1460 1461static const struct attribute_group tsl2772_device_attr_group_tbl[] = { 1462 [ALS] = { 1463 .attrs = tsl2772_ALS_device_attrs, 1464 }, 1465 [PRX] = { 1466 .attrs = tsl2772_PRX_device_attrs, 1467 }, 1468 [ALSPRX] = { 1469 .attrs = tsl2772_ALSPRX_device_attrs, 1470 }, 1471 [PRX2] = { 1472 .attrs = tsl2772_PRX2_device_attrs, 1473 }, 1474 [ALSPRX2] = { 1475 .attrs = tsl2772_ALSPRX2_device_attrs, 1476 }, 1477}; 1478 1479#define TSL2772_DEVICE_INFO(type)[type] = \ 1480 { \ 1481 .attrs = &tsl2772_device_attr_group_tbl[type], \ 1482 .read_raw = &tsl2772_read_raw, \ 1483 .read_avail = &tsl2772_read_avail, \ 1484 .write_raw = &tsl2772_write_raw, \ 1485 .read_event_value = &tsl2772_read_event_value, \ 1486 .write_event_value = &tsl2772_write_event_value, \ 1487 .read_event_config = &tsl2772_read_interrupt_config, \ 1488 .write_event_config = &tsl2772_write_interrupt_config, \ 1489 } 1490 1491static const struct iio_info tsl2772_device_info[] = { 1492 TSL2772_DEVICE_INFO(ALS), 1493 TSL2772_DEVICE_INFO(PRX), 1494 TSL2772_DEVICE_INFO(ALSPRX), 1495 TSL2772_DEVICE_INFO(PRX2), 1496 TSL2772_DEVICE_INFO(ALSPRX2), 1497}; 1498 1499static const struct iio_event_spec tsl2772_events[] = { 1500 { 1501 .type = IIO_EV_TYPE_THRESH, 1502 .dir = IIO_EV_DIR_RISING, 1503 .mask_separate = BIT(IIO_EV_INFO_VALUE), 1504 }, { 1505 .type = IIO_EV_TYPE_THRESH, 1506 .dir = IIO_EV_DIR_FALLING, 1507 .mask_separate = BIT(IIO_EV_INFO_VALUE), 1508 }, { 1509 .type = IIO_EV_TYPE_THRESH, 1510 .dir = IIO_EV_DIR_EITHER, 1511 .mask_separate = BIT(IIO_EV_INFO_PERIOD) | 1512 BIT(IIO_EV_INFO_ENABLE), 1513 }, 1514}; 1515 1516static const struct tsl2772_chip_info tsl2772_chip_info_tbl[] = { 1517 [ALS] = { 1518 .channel_with_events = { 1519 { 1520 .type = IIO_LIGHT, 1521 .indexed = 1, 1522 .channel = 0, 1523 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), 1524 }, { 1525 .type = IIO_INTENSITY, 1526 .indexed = 1, 1527 .channel = 0, 1528 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 1529 BIT(IIO_CHAN_INFO_INT_TIME) | 1530 BIT(IIO_CHAN_INFO_CALIBSCALE) | 1531 BIT(IIO_CHAN_INFO_CALIBBIAS), 1532 .info_mask_separate_available = 1533 BIT(IIO_CHAN_INFO_INT_TIME) | 1534 BIT(IIO_CHAN_INFO_CALIBSCALE), 1535 .event_spec = tsl2772_events, 1536 .num_event_specs = ARRAY_SIZE(tsl2772_events), 1537 }, { 1538 .type = IIO_INTENSITY, 1539 .indexed = 1, 1540 .channel = 1, 1541 }, 1542 }, 1543 .channel_without_events = { 1544 { 1545 .type = IIO_LIGHT, 1546 .indexed = 1, 1547 .channel = 0, 1548 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), 1549 }, { 1550 .type = IIO_INTENSITY, 1551 .indexed = 1, 1552 .channel = 0, 1553 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 1554 BIT(IIO_CHAN_INFO_INT_TIME) | 1555 BIT(IIO_CHAN_INFO_CALIBSCALE) | 1556 BIT(IIO_CHAN_INFO_CALIBBIAS), 1557 .info_mask_separate_available = 1558 BIT(IIO_CHAN_INFO_INT_TIME) | 1559 BIT(IIO_CHAN_INFO_CALIBSCALE), 1560 }, { 1561 .type = IIO_INTENSITY, 1562 .indexed = 1, 1563 .channel = 1, 1564 }, 1565 }, 1566 .chan_table_elements = 3, 1567 .info = &tsl2772_device_info[ALS], 1568 }, 1569 [PRX] = { 1570 .channel_with_events = { 1571 { 1572 .type = IIO_PROXIMITY, 1573 .indexed = 1, 1574 .channel = 0, 1575 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 1576 .event_spec = tsl2772_events, 1577 .num_event_specs = ARRAY_SIZE(tsl2772_events), 1578 }, 1579 }, 1580 .channel_without_events = { 1581 { 1582 .type = IIO_PROXIMITY, 1583 .indexed = 1, 1584 .channel = 0, 1585 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 1586 }, 1587 }, 1588 .chan_table_elements = 1, 1589 .info = &tsl2772_device_info[PRX], 1590 }, 1591 [ALSPRX] = { 1592 .channel_with_events = { 1593 { 1594 .type = IIO_LIGHT, 1595 .indexed = 1, 1596 .channel = 0, 1597 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), 1598 }, { 1599 .type = IIO_INTENSITY, 1600 .indexed = 1, 1601 .channel = 0, 1602 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 1603 BIT(IIO_CHAN_INFO_INT_TIME) | 1604 BIT(IIO_CHAN_INFO_CALIBSCALE) | 1605 BIT(IIO_CHAN_INFO_CALIBBIAS), 1606 .info_mask_separate_available = 1607 BIT(IIO_CHAN_INFO_INT_TIME) | 1608 BIT(IIO_CHAN_INFO_CALIBSCALE), 1609 .event_spec = tsl2772_events, 1610 .num_event_specs = ARRAY_SIZE(tsl2772_events), 1611 }, { 1612 .type = IIO_INTENSITY, 1613 .indexed = 1, 1614 .channel = 1, 1615 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 1616 }, { 1617 .type = IIO_PROXIMITY, 1618 .indexed = 1, 1619 .channel = 0, 1620 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 1621 .event_spec = tsl2772_events, 1622 .num_event_specs = ARRAY_SIZE(tsl2772_events), 1623 }, 1624 }, 1625 .channel_without_events = { 1626 { 1627 .type = IIO_LIGHT, 1628 .indexed = 1, 1629 .channel = 0, 1630 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), 1631 }, { 1632 .type = IIO_INTENSITY, 1633 .indexed = 1, 1634 .channel = 0, 1635 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 1636 BIT(IIO_CHAN_INFO_INT_TIME) | 1637 BIT(IIO_CHAN_INFO_CALIBSCALE) | 1638 BIT(IIO_CHAN_INFO_CALIBBIAS), 1639 .info_mask_separate_available = 1640 BIT(IIO_CHAN_INFO_INT_TIME) | 1641 BIT(IIO_CHAN_INFO_CALIBSCALE), 1642 }, { 1643 .type = IIO_INTENSITY, 1644 .indexed = 1, 1645 .channel = 1, 1646 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 1647 }, { 1648 .type = IIO_PROXIMITY, 1649 .indexed = 1, 1650 .channel = 0, 1651 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 1652 }, 1653 }, 1654 .chan_table_elements = 4, 1655 .info = &tsl2772_device_info[ALSPRX], 1656 }, 1657 [PRX2] = { 1658 .channel_with_events = { 1659 { 1660 .type = IIO_PROXIMITY, 1661 .indexed = 1, 1662 .channel = 0, 1663 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 1664 BIT(IIO_CHAN_INFO_CALIBSCALE), 1665 .info_mask_separate_available = 1666 BIT(IIO_CHAN_INFO_CALIBSCALE), 1667 .event_spec = tsl2772_events, 1668 .num_event_specs = ARRAY_SIZE(tsl2772_events), 1669 }, 1670 }, 1671 .channel_without_events = { 1672 { 1673 .type = IIO_PROXIMITY, 1674 .indexed = 1, 1675 .channel = 0, 1676 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 1677 BIT(IIO_CHAN_INFO_CALIBSCALE), 1678 .info_mask_separate_available = 1679 BIT(IIO_CHAN_INFO_CALIBSCALE), 1680 }, 1681 }, 1682 .chan_table_elements = 1, 1683 .info = &tsl2772_device_info[PRX2], 1684 }, 1685 [ALSPRX2] = { 1686 .channel_with_events = { 1687 { 1688 .type = IIO_LIGHT, 1689 .indexed = 1, 1690 .channel = 0, 1691 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), 1692 }, { 1693 .type = IIO_INTENSITY, 1694 .indexed = 1, 1695 .channel = 0, 1696 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 1697 BIT(IIO_CHAN_INFO_INT_TIME) | 1698 BIT(IIO_CHAN_INFO_CALIBSCALE) | 1699 BIT(IIO_CHAN_INFO_CALIBBIAS), 1700 .info_mask_separate_available = 1701 BIT(IIO_CHAN_INFO_INT_TIME) | 1702 BIT(IIO_CHAN_INFO_CALIBSCALE), 1703 .event_spec = tsl2772_events, 1704 .num_event_specs = ARRAY_SIZE(tsl2772_events), 1705 }, { 1706 .type = IIO_INTENSITY, 1707 .indexed = 1, 1708 .channel = 1, 1709 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 1710 }, { 1711 .type = IIO_PROXIMITY, 1712 .indexed = 1, 1713 .channel = 0, 1714 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 1715 BIT(IIO_CHAN_INFO_CALIBSCALE), 1716 .info_mask_separate_available = 1717 BIT(IIO_CHAN_INFO_CALIBSCALE), 1718 .event_spec = tsl2772_events, 1719 .num_event_specs = ARRAY_SIZE(tsl2772_events), 1720 }, 1721 }, 1722 .channel_without_events = { 1723 { 1724 .type = IIO_LIGHT, 1725 .indexed = 1, 1726 .channel = 0, 1727 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), 1728 }, { 1729 .type = IIO_INTENSITY, 1730 .indexed = 1, 1731 .channel = 0, 1732 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 1733 BIT(IIO_CHAN_INFO_INT_TIME) | 1734 BIT(IIO_CHAN_INFO_CALIBSCALE) | 1735 BIT(IIO_CHAN_INFO_CALIBBIAS), 1736 .info_mask_separate_available = 1737 BIT(IIO_CHAN_INFO_INT_TIME) | 1738 BIT(IIO_CHAN_INFO_CALIBSCALE), 1739 }, { 1740 .type = IIO_INTENSITY, 1741 .indexed = 1, 1742 .channel = 1, 1743 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 1744 }, { 1745 .type = IIO_PROXIMITY, 1746 .indexed = 1, 1747 .channel = 0, 1748 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 1749 BIT(IIO_CHAN_INFO_CALIBSCALE), 1750 .info_mask_separate_available = 1751 BIT(IIO_CHAN_INFO_CALIBSCALE), 1752 }, 1753 }, 1754 .chan_table_elements = 4, 1755 .info = &tsl2772_device_info[ALSPRX2], 1756 }, 1757}; 1758 1759static int tsl2772_probe(struct i2c_client *clientp, 1760 const struct i2c_device_id *id) 1761{ 1762 struct iio_dev *indio_dev; 1763 struct tsl2772_chip *chip; 1764 int ret; 1765 1766 indio_dev = devm_iio_device_alloc(&clientp->dev, sizeof(*chip)); 1767 if (!indio_dev) 1768 return -ENOMEM; 1769 1770 chip = iio_priv(indio_dev); 1771 chip->client = clientp; 1772 i2c_set_clientdata(clientp, indio_dev); 1773 1774 chip->supplies[TSL2772_SUPPLY_VDD].supply = "vdd"; 1775 chip->supplies[TSL2772_SUPPLY_VDDIO].supply = "vddio"; 1776 1777 ret = devm_regulator_bulk_get(&clientp->dev, 1778 ARRAY_SIZE(chip->supplies), 1779 chip->supplies); 1780 if (ret < 0) 1781 return dev_err_probe(&clientp->dev, ret, "Failed to get regulators\n"); 1782 1783 ret = regulator_bulk_enable(ARRAY_SIZE(chip->supplies), chip->supplies); 1784 if (ret < 0) { 1785 dev_err(&clientp->dev, "Failed to enable regulators: %d\n", 1786 ret); 1787 return ret; 1788 } 1789 1790 ret = devm_add_action_or_reset(&clientp->dev, 1791 tsl2772_disable_regulators_action, 1792 chip); 1793 if (ret < 0) { 1794 dev_err(&clientp->dev, "Failed to setup regulator cleanup action %d\n", 1795 ret); 1796 return ret; 1797 } 1798 1799 usleep_range(TSL2772_BOOT_MIN_SLEEP_TIME, TSL2772_BOOT_MAX_SLEEP_TIME); 1800 1801 ret = i2c_smbus_read_byte_data(chip->client, 1802 TSL2772_CMD_REG | TSL2772_CHIPID); 1803 if (ret < 0) 1804 return ret; 1805 1806 if (tsl2772_device_id_verif(ret, id->driver_data) <= 0) { 1807 dev_info(&chip->client->dev, 1808 "%s: i2c device found does not match expected id\n", 1809 __func__); 1810 return -EINVAL; 1811 } 1812 1813 ret = i2c_smbus_write_byte(clientp, TSL2772_CMD_REG | TSL2772_CNTRL); 1814 if (ret < 0) { 1815 dev_err(&clientp->dev, 1816 "%s: Failed to write to CMD register: %d\n", 1817 __func__, ret); 1818 return ret; 1819 } 1820 1821 mutex_init(&chip->als_mutex); 1822 mutex_init(&chip->prox_mutex); 1823 1824 chip->tsl2772_chip_status = TSL2772_CHIP_UNKNOWN; 1825 chip->pdata = dev_get_platdata(&clientp->dev); 1826 chip->id = id->driver_data; 1827 chip->chip_info = 1828 &tsl2772_chip_info_tbl[device_channel_config[id->driver_data]]; 1829 1830 indio_dev->info = chip->chip_info->info; 1831 indio_dev->modes = INDIO_DIRECT_MODE; 1832 indio_dev->name = chip->client->name; 1833 indio_dev->num_channels = chip->chip_info->chan_table_elements; 1834 1835 if (clientp->irq) { 1836 indio_dev->channels = chip->chip_info->channel_with_events; 1837 1838 ret = devm_request_threaded_irq(&clientp->dev, clientp->irq, 1839 NULL, 1840 &tsl2772_event_handler, 1841 IRQF_TRIGGER_FALLING | 1842 IRQF_ONESHOT, 1843 "TSL2772_event", 1844 indio_dev); 1845 if (ret) { 1846 dev_err(&clientp->dev, 1847 "%s: irq request failed\n", __func__); 1848 return ret; 1849 } 1850 } else { 1851 indio_dev->channels = chip->chip_info->channel_without_events; 1852 } 1853 1854 tsl2772_defaults(chip); 1855 ret = tsl2772_chip_on(indio_dev); 1856 if (ret < 0) 1857 return ret; 1858 1859 ret = devm_add_action_or_reset(&clientp->dev, 1860 tsl2772_chip_off_action, 1861 indio_dev); 1862 if (ret < 0) 1863 return ret; 1864 1865 return devm_iio_device_register(&clientp->dev, indio_dev); 1866} 1867 1868static int tsl2772_suspend(struct device *dev) 1869{ 1870 struct iio_dev *indio_dev = dev_get_drvdata(dev); 1871 struct tsl2772_chip *chip = iio_priv(indio_dev); 1872 int ret; 1873 1874 ret = tsl2772_chip_off(indio_dev); 1875 regulator_bulk_disable(ARRAY_SIZE(chip->supplies), chip->supplies); 1876 1877 return ret; 1878} 1879 1880static int tsl2772_resume(struct device *dev) 1881{ 1882 struct iio_dev *indio_dev = dev_get_drvdata(dev); 1883 struct tsl2772_chip *chip = iio_priv(indio_dev); 1884 int ret; 1885 1886 ret = regulator_bulk_enable(ARRAY_SIZE(chip->supplies), chip->supplies); 1887 if (ret < 0) 1888 return ret; 1889 1890 usleep_range(TSL2772_BOOT_MIN_SLEEP_TIME, TSL2772_BOOT_MAX_SLEEP_TIME); 1891 1892 return tsl2772_chip_on(indio_dev); 1893} 1894 1895static const struct i2c_device_id tsl2772_idtable[] = { 1896 { "tsl2571", tsl2571 }, 1897 { "tsl2671", tsl2671 }, 1898 { "tmd2671", tmd2671 }, 1899 { "tsl2771", tsl2771 }, 1900 { "tmd2771", tmd2771 }, 1901 { "tsl2572", tsl2572 }, 1902 { "tsl2672", tsl2672 }, 1903 { "tmd2672", tmd2672 }, 1904 { "tsl2772", tsl2772 }, 1905 { "tmd2772", tmd2772 }, 1906 { "apds9930", apds9930}, 1907 {} 1908}; 1909 1910MODULE_DEVICE_TABLE(i2c, tsl2772_idtable); 1911 1912static const struct of_device_id tsl2772_of_match[] = { 1913 { .compatible = "amstaos,tsl2571" }, 1914 { .compatible = "amstaos,tsl2671" }, 1915 { .compatible = "amstaos,tmd2671" }, 1916 { .compatible = "amstaos,tsl2771" }, 1917 { .compatible = "amstaos,tmd2771" }, 1918 { .compatible = "amstaos,tsl2572" }, 1919 { .compatible = "amstaos,tsl2672" }, 1920 { .compatible = "amstaos,tmd2672" }, 1921 { .compatible = "amstaos,tsl2772" }, 1922 { .compatible = "amstaos,tmd2772" }, 1923 { .compatible = "avago,apds9930" }, 1924 {} 1925}; 1926MODULE_DEVICE_TABLE(of, tsl2772_of_match); 1927 1928static const struct dev_pm_ops tsl2772_pm_ops = { 1929 .suspend = tsl2772_suspend, 1930 .resume = tsl2772_resume, 1931}; 1932 1933static struct i2c_driver tsl2772_driver = { 1934 .driver = { 1935 .name = "tsl2772", 1936 .of_match_table = tsl2772_of_match, 1937 .pm = &tsl2772_pm_ops, 1938 }, 1939 .id_table = tsl2772_idtable, 1940 .probe = tsl2772_probe, 1941}; 1942 1943module_i2c_driver(tsl2772_driver); 1944 1945MODULE_AUTHOR("J. August Brenner <Jon.Brenner@ams.com>"); 1946MODULE_AUTHOR("Brian Masney <masneyb@onstation.org>"); 1947MODULE_DESCRIPTION("TAOS tsl2772 ambient and proximity light sensor driver"); 1948MODULE_LICENSE("GPL"); 1949