1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * drivers/iio/light/tsl2563.c 4 * 5 * Copyright (C) 2008 Nokia Corporation 6 * 7 * Written by Timo O. Karjalainen <timo.o.karjalainen@nokia.com> 8 * Contact: Amit Kucheria <amit.kucheria@verdurent.com> 9 * 10 * Converted to IIO driver 11 * Amit Kucheria <amit.kucheria@verdurent.com> 12 */ 13 14 #include <linux/module.h> 15 #include <linux/mod_devicetable.h> 16 #include <linux/property.h> 17 #include <linux/i2c.h> 18 #include <linux/interrupt.h> 19 #include <linux/irq.h> 20 #include <linux/sched.h> 21 #include <linux/mutex.h> 22 #include <linux/delay.h> 23 #include <linux/pm.h> 24 #include <linux/err.h> 25 #include <linux/slab.h> 26 27 #include <linux/iio/iio.h> 28 #include <linux/iio/sysfs.h> 29 #include <linux/iio/events.h> 30 #include <linux/platform_data/tsl2563.h> 31 32 /* Use this many bits for fraction part. */ 33 #define ADC_FRAC_BITS 14 34 35 /* Given number of 1/10000's in ADC_FRAC_BITS precision. */ 36 #define FRAC10K(f) (((f) * (1L << (ADC_FRAC_BITS))) / (10000)) 37 38 /* Bits used for fraction in calibration coefficients.*/ 39 #define CALIB_FRAC_BITS 10 40 /* 0.5 in CALIB_FRAC_BITS precision */ 41 #define CALIB_FRAC_HALF (1 << (CALIB_FRAC_BITS - 1)) 42 /* Make a fraction from a number n that was multiplied with b. */ 43 #define CALIB_FRAC(n, b) (((n) << CALIB_FRAC_BITS) / (b)) 44 /* Decimal 10^(digits in sysfs presentation) */ 45 #define CALIB_BASE_SYSFS 1000 46 47 #define TSL2563_CMD 0x80 48 #define TSL2563_CLEARINT 0x40 49 50 #define TSL2563_REG_CTRL 0x00 51 #define TSL2563_REG_TIMING 0x01 52 #define TSL2563_REG_LOWLOW 0x02 /* data0 low threshold, 2 bytes */ 53 #define TSL2563_REG_LOWHIGH 0x03 54 #define TSL2563_REG_HIGHLOW 0x04 /* data0 high threshold, 2 bytes */ 55 #define TSL2563_REG_HIGHHIGH 0x05 56 #define TSL2563_REG_INT 0x06 57 #define TSL2563_REG_ID 0x0a 58 #define TSL2563_REG_DATA0LOW 0x0c /* broadband sensor value, 2 bytes */ 59 #define TSL2563_REG_DATA0HIGH 0x0d 60 #define TSL2563_REG_DATA1LOW 0x0e /* infrared sensor value, 2 bytes */ 61 #define TSL2563_REG_DATA1HIGH 0x0f 62 63 #define TSL2563_CMD_POWER_ON 0x03 64 #define TSL2563_CMD_POWER_OFF 0x00 65 #define TSL2563_CTRL_POWER_MASK 0x03 66 67 #define TSL2563_TIMING_13MS 0x00 68 #define TSL2563_TIMING_100MS 0x01 69 #define TSL2563_TIMING_400MS 0x02 70 #define TSL2563_TIMING_MASK 0x03 71 #define TSL2563_TIMING_GAIN16 0x10 72 #define TSL2563_TIMING_GAIN1 0x00 73 74 #define TSL2563_INT_DISABLED 0x00 75 #define TSL2563_INT_LEVEL 0x10 76 #define TSL2563_INT_PERSIST(n) ((n) & 0x0F) 77 78 struct tsl2563_gainlevel_coeff { 79 u8 gaintime; 80 u16 min; 81 u16 max; 82 }; 83 84 static const struct tsl2563_gainlevel_coeff tsl2563_gainlevel_table[] = { 85 { 86 .gaintime = TSL2563_TIMING_400MS | TSL2563_TIMING_GAIN16, 87 .min = 0, 88 .max = 65534, 89 }, { 90 .gaintime = TSL2563_TIMING_400MS | TSL2563_TIMING_GAIN1, 91 .min = 2048, 92 .max = 65534, 93 }, { 94 .gaintime = TSL2563_TIMING_100MS | TSL2563_TIMING_GAIN1, 95 .min = 4095, 96 .max = 37177, 97 }, { 98 .gaintime = TSL2563_TIMING_13MS | TSL2563_TIMING_GAIN1, 99 .min = 3000, 100 .max = 65535, 101 }, 102 }; 103 104 struct tsl2563_chip { 105 struct mutex lock; 106 struct i2c_client *client; 107 struct delayed_work poweroff_work; 108 109 /* Remember state for suspend and resume functions */ 110 bool suspended; 111 112 struct tsl2563_gainlevel_coeff const *gainlevel; 113 114 u16 low_thres; 115 u16 high_thres; 116 u8 intr; 117 bool int_enabled; 118 119 /* Calibration coefficients */ 120 u32 calib0; 121 u32 calib1; 122 int cover_comp_gain; 123 124 /* Cache current values, to be returned while suspended */ 125 u32 data0; 126 u32 data1; 127 }; 128 129 static int tsl2563_set_power(struct tsl2563_chip *chip, int on) 130 { 131 struct i2c_client *client = chip->client; 132 u8 cmd; 133 134 cmd = on ? TSL2563_CMD_POWER_ON : TSL2563_CMD_POWER_OFF; 135 return i2c_smbus_write_byte_data(client, 136 TSL2563_CMD | TSL2563_REG_CTRL, cmd); 137 } 138 139 /* 140 * Return value is 0 for off, 1 for on, or a negative error 141 * code if reading failed. 142 */ 143 static int tsl2563_get_power(struct tsl2563_chip *chip) 144 { 145 struct i2c_client *client = chip->client; 146 int ret; 147 148 ret = i2c_smbus_read_byte_data(client, TSL2563_CMD | TSL2563_REG_CTRL); 149 if (ret < 0) 150 return ret; 151 152 return (ret & TSL2563_CTRL_POWER_MASK) == TSL2563_CMD_POWER_ON; 153 } 154 155 static int tsl2563_configure(struct tsl2563_chip *chip) 156 { 157 int ret; 158 159 ret = i2c_smbus_write_byte_data(chip->client, 160 TSL2563_CMD | TSL2563_REG_TIMING, 161 chip->gainlevel->gaintime); 162 if (ret) 163 goto error_ret; 164 ret = i2c_smbus_write_byte_data(chip->client, 165 TSL2563_CMD | TSL2563_REG_HIGHLOW, 166 chip->high_thres & 0xFF); 167 if (ret) 168 goto error_ret; 169 ret = i2c_smbus_write_byte_data(chip->client, 170 TSL2563_CMD | TSL2563_REG_HIGHHIGH, 171 (chip->high_thres >> 8) & 0xFF); 172 if (ret) 173 goto error_ret; 174 ret = i2c_smbus_write_byte_data(chip->client, 175 TSL2563_CMD | TSL2563_REG_LOWLOW, 176 chip->low_thres & 0xFF); 177 if (ret) 178 goto error_ret; 179 ret = i2c_smbus_write_byte_data(chip->client, 180 TSL2563_CMD | TSL2563_REG_LOWHIGH, 181 (chip->low_thres >> 8) & 0xFF); 182 /* 183 * Interrupt register is automatically written anyway if it is relevant 184 * so is not here. 185 */ 186 error_ret: 187 return ret; 188 } 189 190 static void tsl2563_poweroff_work(struct work_struct *work) 191 { 192 struct tsl2563_chip *chip = 193 container_of(work, struct tsl2563_chip, poweroff_work.work); 194 tsl2563_set_power(chip, 0); 195 } 196 197 static int tsl2563_detect(struct tsl2563_chip *chip) 198 { 199 int ret; 200 201 ret = tsl2563_set_power(chip, 1); 202 if (ret) 203 return ret; 204 205 ret = tsl2563_get_power(chip); 206 if (ret < 0) 207 return ret; 208 209 return ret ? 0 : -ENODEV; 210 } 211 212 static int tsl2563_read_id(struct tsl2563_chip *chip, u8 *id) 213 { 214 struct i2c_client *client = chip->client; 215 int ret; 216 217 ret = i2c_smbus_read_byte_data(client, TSL2563_CMD | TSL2563_REG_ID); 218 if (ret < 0) 219 return ret; 220 221 *id = ret; 222 223 return 0; 224 } 225 226 /* 227 * "Normalized" ADC value is one obtained with 400ms of integration time and 228 * 16x gain. This function returns the number of bits of shift needed to 229 * convert between normalized values and HW values obtained using given 230 * timing and gain settings. 231 */ 232 static int tsl2563_adc_shiftbits(u8 timing) 233 { 234 int shift = 0; 235 236 switch (timing & TSL2563_TIMING_MASK) { 237 case TSL2563_TIMING_13MS: 238 shift += 5; 239 break; 240 case TSL2563_TIMING_100MS: 241 shift += 2; 242 break; 243 case TSL2563_TIMING_400MS: 244 /* no-op */ 245 break; 246 } 247 248 if (!(timing & TSL2563_TIMING_GAIN16)) 249 shift += 4; 250 251 return shift; 252 } 253 254 /* Convert a HW ADC value to normalized scale. */ 255 static u32 tsl2563_normalize_adc(u16 adc, u8 timing) 256 { 257 return adc << tsl2563_adc_shiftbits(timing); 258 } 259 260 static void tsl2563_wait_adc(struct tsl2563_chip *chip) 261 { 262 unsigned int delay; 263 264 switch (chip->gainlevel->gaintime & TSL2563_TIMING_MASK) { 265 case TSL2563_TIMING_13MS: 266 delay = 14; 267 break; 268 case TSL2563_TIMING_100MS: 269 delay = 101; 270 break; 271 default: 272 delay = 402; 273 } 274 /* 275 * TODO: Make sure that we wait at least required delay but why we 276 * have to extend it one tick more? 277 */ 278 schedule_timeout_interruptible(msecs_to_jiffies(delay) + 2); 279 } 280 281 static int tsl2563_adjust_gainlevel(struct tsl2563_chip *chip, u16 adc) 282 { 283 struct i2c_client *client = chip->client; 284 285 if (adc > chip->gainlevel->max || adc < chip->gainlevel->min) { 286 287 (adc > chip->gainlevel->max) ? 288 chip->gainlevel++ : chip->gainlevel--; 289 290 i2c_smbus_write_byte_data(client, 291 TSL2563_CMD | TSL2563_REG_TIMING, 292 chip->gainlevel->gaintime); 293 294 tsl2563_wait_adc(chip); 295 tsl2563_wait_adc(chip); 296 297 return 1; 298 } else 299 return 0; 300 } 301 302 static int tsl2563_get_adc(struct tsl2563_chip *chip) 303 { 304 struct i2c_client *client = chip->client; 305 u16 adc0, adc1; 306 int retry = 1; 307 int ret = 0; 308 309 if (chip->suspended) 310 goto out; 311 312 if (!chip->int_enabled) { 313 cancel_delayed_work_sync(&chip->poweroff_work); 314 315 if (!tsl2563_get_power(chip)) { 316 ret = tsl2563_set_power(chip, 1); 317 if (ret) 318 goto out; 319 ret = tsl2563_configure(chip); 320 if (ret) 321 goto out; 322 tsl2563_wait_adc(chip); 323 } 324 } 325 326 while (retry) { 327 ret = i2c_smbus_read_word_data(client, 328 TSL2563_CMD | TSL2563_REG_DATA0LOW); 329 if (ret < 0) 330 goto out; 331 adc0 = ret; 332 333 ret = i2c_smbus_read_word_data(client, 334 TSL2563_CMD | TSL2563_REG_DATA1LOW); 335 if (ret < 0) 336 goto out; 337 adc1 = ret; 338 339 retry = tsl2563_adjust_gainlevel(chip, adc0); 340 } 341 342 chip->data0 = tsl2563_normalize_adc(adc0, chip->gainlevel->gaintime); 343 chip->data1 = tsl2563_normalize_adc(adc1, chip->gainlevel->gaintime); 344 345 if (!chip->int_enabled) 346 schedule_delayed_work(&chip->poweroff_work, 5 * HZ); 347 348 ret = 0; 349 out: 350 return ret; 351 } 352 353 static inline int tsl2563_calib_to_sysfs(u32 calib) 354 { 355 return (int) (((calib * CALIB_BASE_SYSFS) + 356 CALIB_FRAC_HALF) >> CALIB_FRAC_BITS); 357 } 358 359 static inline u32 tsl2563_calib_from_sysfs(int value) 360 { 361 return (((u32) value) << CALIB_FRAC_BITS) / CALIB_BASE_SYSFS; 362 } 363 364 /* 365 * Conversions between lux and ADC values. 366 * 367 * The basic formula is lux = c0 * adc0 - c1 * adc1, where c0 and c1 are 368 * appropriate constants. Different constants are needed for different 369 * kinds of light, determined by the ratio adc1/adc0 (basically the ratio 370 * of the intensities in infrared and visible wavelengths). lux_table below 371 * lists the upper threshold of the adc1/adc0 ratio and the corresponding 372 * constants. 373 */ 374 375 struct tsl2563_lux_coeff { 376 unsigned long ch_ratio; 377 unsigned long ch0_coeff; 378 unsigned long ch1_coeff; 379 }; 380 381 static const struct tsl2563_lux_coeff lux_table[] = { 382 { 383 .ch_ratio = FRAC10K(1300), 384 .ch0_coeff = FRAC10K(315), 385 .ch1_coeff = FRAC10K(262), 386 }, { 387 .ch_ratio = FRAC10K(2600), 388 .ch0_coeff = FRAC10K(337), 389 .ch1_coeff = FRAC10K(430), 390 }, { 391 .ch_ratio = FRAC10K(3900), 392 .ch0_coeff = FRAC10K(363), 393 .ch1_coeff = FRAC10K(529), 394 }, { 395 .ch_ratio = FRAC10K(5200), 396 .ch0_coeff = FRAC10K(392), 397 .ch1_coeff = FRAC10K(605), 398 }, { 399 .ch_ratio = FRAC10K(6500), 400 .ch0_coeff = FRAC10K(229), 401 .ch1_coeff = FRAC10K(291), 402 }, { 403 .ch_ratio = FRAC10K(8000), 404 .ch0_coeff = FRAC10K(157), 405 .ch1_coeff = FRAC10K(180), 406 }, { 407 .ch_ratio = FRAC10K(13000), 408 .ch0_coeff = FRAC10K(34), 409 .ch1_coeff = FRAC10K(26), 410 }, { 411 .ch_ratio = ULONG_MAX, 412 .ch0_coeff = 0, 413 .ch1_coeff = 0, 414 }, 415 }; 416 417 /* Convert normalized, scaled ADC values to lux. */ 418 static unsigned int tsl2563_adc_to_lux(u32 adc0, u32 adc1) 419 { 420 const struct tsl2563_lux_coeff *lp = lux_table; 421 unsigned long ratio, lux, ch0 = adc0, ch1 = adc1; 422 423 ratio = ch0 ? ((ch1 << ADC_FRAC_BITS) / ch0) : ULONG_MAX; 424 425 while (lp->ch_ratio < ratio) 426 lp++; 427 428 lux = ch0 * lp->ch0_coeff - ch1 * lp->ch1_coeff; 429 430 return (unsigned int) (lux >> ADC_FRAC_BITS); 431 } 432 433 /* Apply calibration coefficient to ADC count. */ 434 static u32 tsl2563_calib_adc(u32 adc, u32 calib) 435 { 436 unsigned long scaled = adc; 437 438 scaled *= calib; 439 scaled >>= CALIB_FRAC_BITS; 440 441 return (u32) scaled; 442 } 443 444 static int tsl2563_write_raw(struct iio_dev *indio_dev, 445 struct iio_chan_spec const *chan, 446 int val, 447 int val2, 448 long mask) 449 { 450 struct tsl2563_chip *chip = iio_priv(indio_dev); 451 452 if (mask != IIO_CHAN_INFO_CALIBSCALE) 453 return -EINVAL; 454 if (chan->channel2 == IIO_MOD_LIGHT_BOTH) 455 chip->calib0 = tsl2563_calib_from_sysfs(val); 456 else if (chan->channel2 == IIO_MOD_LIGHT_IR) 457 chip->calib1 = tsl2563_calib_from_sysfs(val); 458 else 459 return -EINVAL; 460 461 return 0; 462 } 463 464 static int tsl2563_read_raw(struct iio_dev *indio_dev, 465 struct iio_chan_spec const *chan, 466 int *val, 467 int *val2, 468 long mask) 469 { 470 int ret = -EINVAL; 471 u32 calib0, calib1; 472 struct tsl2563_chip *chip = iio_priv(indio_dev); 473 474 mutex_lock(&chip->lock); 475 switch (mask) { 476 case IIO_CHAN_INFO_RAW: 477 case IIO_CHAN_INFO_PROCESSED: 478 switch (chan->type) { 479 case IIO_LIGHT: 480 ret = tsl2563_get_adc(chip); 481 if (ret) 482 goto error_ret; 483 calib0 = tsl2563_calib_adc(chip->data0, chip->calib0) * 484 chip->cover_comp_gain; 485 calib1 = tsl2563_calib_adc(chip->data1, chip->calib1) * 486 chip->cover_comp_gain; 487 *val = tsl2563_adc_to_lux(calib0, calib1); 488 ret = IIO_VAL_INT; 489 break; 490 case IIO_INTENSITY: 491 ret = tsl2563_get_adc(chip); 492 if (ret) 493 goto error_ret; 494 if (chan->channel2 == IIO_MOD_LIGHT_BOTH) 495 *val = chip->data0; 496 else 497 *val = chip->data1; 498 ret = IIO_VAL_INT; 499 break; 500 default: 501 break; 502 } 503 break; 504 505 case IIO_CHAN_INFO_CALIBSCALE: 506 if (chan->channel2 == IIO_MOD_LIGHT_BOTH) 507 *val = tsl2563_calib_to_sysfs(chip->calib0); 508 else 509 *val = tsl2563_calib_to_sysfs(chip->calib1); 510 ret = IIO_VAL_INT; 511 break; 512 default: 513 ret = -EINVAL; 514 goto error_ret; 515 } 516 517 error_ret: 518 mutex_unlock(&chip->lock); 519 return ret; 520 } 521 522 static const struct iio_event_spec tsl2563_events[] = { 523 { 524 .type = IIO_EV_TYPE_THRESH, 525 .dir = IIO_EV_DIR_RISING, 526 .mask_separate = BIT(IIO_EV_INFO_VALUE) | 527 BIT(IIO_EV_INFO_ENABLE), 528 }, { 529 .type = IIO_EV_TYPE_THRESH, 530 .dir = IIO_EV_DIR_FALLING, 531 .mask_separate = BIT(IIO_EV_INFO_VALUE) | 532 BIT(IIO_EV_INFO_ENABLE), 533 }, 534 }; 535 536 static const struct iio_chan_spec tsl2563_channels[] = { 537 { 538 .type = IIO_LIGHT, 539 .indexed = 1, 540 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), 541 .channel = 0, 542 }, { 543 .type = IIO_INTENSITY, 544 .modified = 1, 545 .channel2 = IIO_MOD_LIGHT_BOTH, 546 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 547 BIT(IIO_CHAN_INFO_CALIBSCALE), 548 .event_spec = tsl2563_events, 549 .num_event_specs = ARRAY_SIZE(tsl2563_events), 550 }, { 551 .type = IIO_INTENSITY, 552 .modified = 1, 553 .channel2 = IIO_MOD_LIGHT_IR, 554 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 555 BIT(IIO_CHAN_INFO_CALIBSCALE), 556 } 557 }; 558 559 static int tsl2563_read_thresh(struct iio_dev *indio_dev, 560 const struct iio_chan_spec *chan, enum iio_event_type type, 561 enum iio_event_direction dir, enum iio_event_info info, int *val, 562 int *val2) 563 { 564 struct tsl2563_chip *chip = iio_priv(indio_dev); 565 566 switch (dir) { 567 case IIO_EV_DIR_RISING: 568 *val = chip->high_thres; 569 break; 570 case IIO_EV_DIR_FALLING: 571 *val = chip->low_thres; 572 break; 573 default: 574 return -EINVAL; 575 } 576 577 return IIO_VAL_INT; 578 } 579 580 static int tsl2563_write_thresh(struct iio_dev *indio_dev, 581 const struct iio_chan_spec *chan, enum iio_event_type type, 582 enum iio_event_direction dir, enum iio_event_info info, int val, 583 int val2) 584 { 585 struct tsl2563_chip *chip = iio_priv(indio_dev); 586 int ret; 587 u8 address; 588 589 if (dir == IIO_EV_DIR_RISING) 590 address = TSL2563_REG_HIGHLOW; 591 else 592 address = TSL2563_REG_LOWLOW; 593 mutex_lock(&chip->lock); 594 ret = i2c_smbus_write_byte_data(chip->client, TSL2563_CMD | address, 595 val & 0xFF); 596 if (ret) 597 goto error_ret; 598 ret = i2c_smbus_write_byte_data(chip->client, 599 TSL2563_CMD | (address + 1), 600 (val >> 8) & 0xFF); 601 if (dir == IIO_EV_DIR_RISING) 602 chip->high_thres = val; 603 else 604 chip->low_thres = val; 605 606 error_ret: 607 mutex_unlock(&chip->lock); 608 609 return ret; 610 } 611 612 static irqreturn_t tsl2563_event_handler(int irq, void *private) 613 { 614 struct iio_dev *dev_info = private; 615 struct tsl2563_chip *chip = iio_priv(dev_info); 616 617 iio_push_event(dev_info, 618 IIO_UNMOD_EVENT_CODE(IIO_INTENSITY, 619 0, 620 IIO_EV_TYPE_THRESH, 621 IIO_EV_DIR_EITHER), 622 iio_get_time_ns(dev_info)); 623 624 /* clear the interrupt and push the event */ 625 i2c_smbus_write_byte(chip->client, TSL2563_CMD | TSL2563_CLEARINT); 626 return IRQ_HANDLED; 627 } 628 629 static int tsl2563_write_interrupt_config(struct iio_dev *indio_dev, 630 const struct iio_chan_spec *chan, enum iio_event_type type, 631 enum iio_event_direction dir, int state) 632 { 633 struct tsl2563_chip *chip = iio_priv(indio_dev); 634 int ret = 0; 635 636 mutex_lock(&chip->lock); 637 if (state && !(chip->intr & 0x30)) { 638 chip->intr &= ~0x30; 639 chip->intr |= 0x10; 640 /* ensure the chip is actually on */ 641 cancel_delayed_work_sync(&chip->poweroff_work); 642 if (!tsl2563_get_power(chip)) { 643 ret = tsl2563_set_power(chip, 1); 644 if (ret) 645 goto out; 646 ret = tsl2563_configure(chip); 647 if (ret) 648 goto out; 649 } 650 ret = i2c_smbus_write_byte_data(chip->client, 651 TSL2563_CMD | TSL2563_REG_INT, 652 chip->intr); 653 chip->int_enabled = true; 654 } 655 656 if (!state && (chip->intr & 0x30)) { 657 chip->intr &= ~0x30; 658 ret = i2c_smbus_write_byte_data(chip->client, 659 TSL2563_CMD | TSL2563_REG_INT, 660 chip->intr); 661 chip->int_enabled = false; 662 /* now the interrupt is not enabled, we can go to sleep */ 663 schedule_delayed_work(&chip->poweroff_work, 5 * HZ); 664 } 665 out: 666 mutex_unlock(&chip->lock); 667 668 return ret; 669 } 670 671 static int tsl2563_read_interrupt_config(struct iio_dev *indio_dev, 672 const struct iio_chan_spec *chan, enum iio_event_type type, 673 enum iio_event_direction dir) 674 { 675 struct tsl2563_chip *chip = iio_priv(indio_dev); 676 int ret; 677 678 mutex_lock(&chip->lock); 679 ret = i2c_smbus_read_byte_data(chip->client, 680 TSL2563_CMD | TSL2563_REG_INT); 681 mutex_unlock(&chip->lock); 682 if (ret < 0) 683 return ret; 684 685 return !!(ret & 0x30); 686 } 687 688 static const struct iio_info tsl2563_info_no_irq = { 689 .read_raw = &tsl2563_read_raw, 690 .write_raw = &tsl2563_write_raw, 691 }; 692 693 static const struct iio_info tsl2563_info = { 694 .read_raw = &tsl2563_read_raw, 695 .write_raw = &tsl2563_write_raw, 696 .read_event_value = &tsl2563_read_thresh, 697 .write_event_value = &tsl2563_write_thresh, 698 .read_event_config = &tsl2563_read_interrupt_config, 699 .write_event_config = &tsl2563_write_interrupt_config, 700 }; 701 702 static int tsl2563_probe(struct i2c_client *client, 703 const struct i2c_device_id *device_id) 704 { 705 struct iio_dev *indio_dev; 706 struct tsl2563_chip *chip; 707 struct tsl2563_platform_data *pdata = client->dev.platform_data; 708 int err = 0; 709 u8 id = 0; 710 711 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip)); 712 if (!indio_dev) 713 return -ENOMEM; 714 715 chip = iio_priv(indio_dev); 716 717 i2c_set_clientdata(client, indio_dev); 718 chip->client = client; 719 720 err = tsl2563_detect(chip); 721 if (err) { 722 dev_err(&client->dev, "detect error %d\n", -err); 723 return err; 724 } 725 726 err = tsl2563_read_id(chip, &id); 727 if (err) { 728 dev_err(&client->dev, "read id error %d\n", -err); 729 return err; 730 } 731 732 mutex_init(&chip->lock); 733 734 /* Default values used until userspace says otherwise */ 735 chip->low_thres = 0x0; 736 chip->high_thres = 0xffff; 737 chip->gainlevel = tsl2563_gainlevel_table; 738 chip->intr = TSL2563_INT_PERSIST(4); 739 chip->calib0 = tsl2563_calib_from_sysfs(CALIB_BASE_SYSFS); 740 chip->calib1 = tsl2563_calib_from_sysfs(CALIB_BASE_SYSFS); 741 742 if (pdata) { 743 chip->cover_comp_gain = pdata->cover_comp_gain; 744 } else { 745 err = device_property_read_u32(&client->dev, "amstaos,cover-comp-gain", 746 &chip->cover_comp_gain); 747 if (err) 748 chip->cover_comp_gain = 1; 749 } 750 751 dev_info(&client->dev, "model %d, rev. %d\n", id >> 4, id & 0x0f); 752 indio_dev->name = client->name; 753 indio_dev->channels = tsl2563_channels; 754 indio_dev->num_channels = ARRAY_SIZE(tsl2563_channels); 755 indio_dev->modes = INDIO_DIRECT_MODE; 756 757 if (client->irq) 758 indio_dev->info = &tsl2563_info; 759 else 760 indio_dev->info = &tsl2563_info_no_irq; 761 762 if (client->irq) { 763 err = devm_request_threaded_irq(&client->dev, client->irq, 764 NULL, 765 &tsl2563_event_handler, 766 IRQF_TRIGGER_RISING | IRQF_ONESHOT, 767 "tsl2563_event", 768 indio_dev); 769 if (err) { 770 dev_err(&client->dev, "irq request error %d\n", -err); 771 return err; 772 } 773 } 774 775 err = tsl2563_configure(chip); 776 if (err) { 777 dev_err(&client->dev, "configure error %d\n", -err); 778 return err; 779 } 780 781 INIT_DELAYED_WORK(&chip->poweroff_work, tsl2563_poweroff_work); 782 783 /* The interrupt cannot yet be enabled so this is fine without lock */ 784 schedule_delayed_work(&chip->poweroff_work, 5 * HZ); 785 786 err = iio_device_register(indio_dev); 787 if (err) { 788 dev_err(&client->dev, "iio registration error %d\n", -err); 789 goto fail; 790 } 791 792 return 0; 793 794 fail: 795 cancel_delayed_work_sync(&chip->poweroff_work); 796 return err; 797 } 798 799 static void tsl2563_remove(struct i2c_client *client) 800 { 801 struct iio_dev *indio_dev = i2c_get_clientdata(client); 802 struct tsl2563_chip *chip = iio_priv(indio_dev); 803 804 iio_device_unregister(indio_dev); 805 if (!chip->int_enabled) 806 cancel_delayed_work_sync(&chip->poweroff_work); 807 /* Ensure that interrupts are disabled - then flush any bottom halves */ 808 chip->intr &= ~0x30; 809 i2c_smbus_write_byte_data(chip->client, TSL2563_CMD | TSL2563_REG_INT, 810 chip->intr); 811 tsl2563_set_power(chip, 0); 812 } 813 814 static int tsl2563_suspend(struct device *dev) 815 { 816 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 817 struct tsl2563_chip *chip = iio_priv(indio_dev); 818 int ret; 819 820 mutex_lock(&chip->lock); 821 822 ret = tsl2563_set_power(chip, 0); 823 if (ret) 824 goto out; 825 826 chip->suspended = true; 827 828 out: 829 mutex_unlock(&chip->lock); 830 return ret; 831 } 832 833 static int tsl2563_resume(struct device *dev) 834 { 835 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 836 struct tsl2563_chip *chip = iio_priv(indio_dev); 837 int ret; 838 839 mutex_lock(&chip->lock); 840 841 ret = tsl2563_set_power(chip, 1); 842 if (ret) 843 goto out; 844 845 ret = tsl2563_configure(chip); 846 if (ret) 847 goto out; 848 849 chip->suspended = false; 850 851 out: 852 mutex_unlock(&chip->lock); 853 return ret; 854 } 855 856 static DEFINE_SIMPLE_DEV_PM_OPS(tsl2563_pm_ops, tsl2563_suspend, 857 tsl2563_resume); 858 859 static const struct i2c_device_id tsl2563_id[] = { 860 { "tsl2560", 0 }, 861 { "tsl2561", 1 }, 862 { "tsl2562", 2 }, 863 { "tsl2563", 3 }, 864 {} 865 }; 866 MODULE_DEVICE_TABLE(i2c, tsl2563_id); 867 868 static const struct of_device_id tsl2563_of_match[] = { 869 { .compatible = "amstaos,tsl2560" }, 870 { .compatible = "amstaos,tsl2561" }, 871 { .compatible = "amstaos,tsl2562" }, 872 { .compatible = "amstaos,tsl2563" }, 873 {} 874 }; 875 MODULE_DEVICE_TABLE(of, tsl2563_of_match); 876 877 static struct i2c_driver tsl2563_i2c_driver = { 878 .driver = { 879 .name = "tsl2563", 880 .of_match_table = tsl2563_of_match, 881 .pm = pm_sleep_ptr(&tsl2563_pm_ops), 882 }, 883 .probe = tsl2563_probe, 884 .remove = tsl2563_remove, 885 .id_table = tsl2563_id, 886 }; 887 module_i2c_driver(tsl2563_i2c_driver); 888 889 MODULE_AUTHOR("Nokia Corporation"); 890 MODULE_DESCRIPTION("tsl2563 light sensor driver"); 891 MODULE_LICENSE("GPL"); 892