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