1/* 2 * drivers/i2c/chips/tsl2563.c 3 * 4 * Copyright (C) 2008 Nokia Corporation 5 * 6 * Written by Timo O. Karjalainen <timo.o.karjalainen@nokia.com> 7 * Contact: Amit Kucheria <amit.kucheria@verdurent.com> 8 * 9 * Converted to IIO driver 10 * Amit Kucheria <amit.kucheria@verdurent.com> 11 * 12 * This program is free software; you can redistribute it and/or 13 * modify it under the terms of the GNU General Public License 14 * version 2 as published by the Free Software Foundation. 15 * 16 * This program is distributed in the hope that it will be useful, but 17 * WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 * General Public License for more details. 20 * 21 * You should have received a copy of the GNU General Public License 22 * along with this program; if not, write to the Free Software 23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 24 * 02110-1301 USA 25 */ 26 27#include <linux/module.h> 28#include <linux/i2c.h> 29#include <linux/interrupt.h> 30#include <linux/irq.h> 31#include <linux/sched.h> 32#include <linux/mutex.h> 33#include <linux/delay.h> 34#include <linux/platform_device.h> 35#include <linux/pm.h> 36#include <linux/hwmon.h> 37#include <linux/err.h> 38#include <linux/slab.h> 39 40#include "../iio.h" 41#include "tsl2563.h" 42 43/* Use this many bits for fraction part. */ 44#define ADC_FRAC_BITS (14) 45 46/* Given number of 1/10000's in ADC_FRAC_BITS precision. */ 47#define FRAC10K(f) (((f) * (1L << (ADC_FRAC_BITS))) / (10000)) 48 49/* Bits used for fraction in calibration coefficients.*/ 50#define CALIB_FRAC_BITS (10) 51/* 0.5 in CALIB_FRAC_BITS precision */ 52#define CALIB_FRAC_HALF (1 << (CALIB_FRAC_BITS - 1)) 53/* Make a fraction from a number n that was multiplied with b. */ 54#define CALIB_FRAC(n, b) (((n) << CALIB_FRAC_BITS) / (b)) 55/* Decimal 10^(digits in sysfs presentation) */ 56#define CALIB_BASE_SYSFS (1000) 57 58#define TSL2563_CMD (0x80) 59#define TSL2563_CLEARINT (0x40) 60 61#define TSL2563_REG_CTRL (0x00) 62#define TSL2563_REG_TIMING (0x01) 63#define TSL2563_REG_LOWLOW (0x02) /* data0 low threshold, 2 bytes */ 64#define TSL2563_REG_LOWHIGH (0x03) 65#define TSL2563_REG_HIGHLOW (0x04) /* data0 high threshold, 2 bytes */ 66#define TSL2563_REG_HIGHHIGH (0x05) 67#define TSL2563_REG_INT (0x06) 68#define TSL2563_REG_ID (0x0a) 69#define TSL2563_REG_DATA0LOW (0x0c) /* broadband sensor value, 2 bytes */ 70#define TSL2563_REG_DATA0HIGH (0x0d) 71#define TSL2563_REG_DATA1LOW (0x0e) /* infrared sensor value, 2 bytes */ 72#define TSL2563_REG_DATA1HIGH (0x0f) 73 74#define TSL2563_CMD_POWER_ON (0x03) 75#define TSL2563_CMD_POWER_OFF (0x00) 76#define TSL2563_CTRL_POWER_MASK (0x03) 77 78#define TSL2563_TIMING_13MS (0x00) 79#define TSL2563_TIMING_100MS (0x01) 80#define TSL2563_TIMING_400MS (0x02) 81#define TSL2563_TIMING_MASK (0x03) 82#define TSL2563_TIMING_GAIN16 (0x10) 83#define TSL2563_TIMING_GAIN1 (0x00) 84 85#define TSL2563_INT_DISBLED (0x00) 86#define TSL2563_INT_LEVEL (0x10) 87#define TSL2563_INT_PERSIST(n) ((n) & 0x0F) 88 89struct tsl2563_gainlevel_coeff { 90 u8 gaintime; 91 u16 min; 92 u16 max; 93}; 94 95static struct tsl2563_gainlevel_coeff tsl2563_gainlevel_table[] = { 96 { 97 .gaintime = TSL2563_TIMING_400MS | TSL2563_TIMING_GAIN16, 98 .min = 0, 99 .max = 65534, 100 }, { 101 .gaintime = TSL2563_TIMING_400MS | TSL2563_TIMING_GAIN1, 102 .min = 2048, 103 .max = 65534, 104 }, { 105 .gaintime = TSL2563_TIMING_100MS | TSL2563_TIMING_GAIN1, 106 .min = 4095, 107 .max = 37177, 108 }, { 109 .gaintime = TSL2563_TIMING_13MS | TSL2563_TIMING_GAIN1, 110 .min = 3000, 111 .max = 65535, 112 }, 113}; 114 115struct tsl2563_chip { 116 struct mutex lock; 117 struct i2c_client *client; 118 struct iio_dev *indio_dev; 119 struct delayed_work poweroff_work; 120 121 struct work_struct work_thresh; 122 s64 event_timestamp; 123 /* Remember state for suspend and resume functions */ 124 pm_message_t state; 125 126 struct tsl2563_gainlevel_coeff *gainlevel; 127 128 u16 low_thres; 129 u16 high_thres; 130 u8 intr; 131 bool int_enabled; 132 133 /* Calibration coefficients */ 134 u32 calib0; 135 u32 calib1; 136 int cover_comp_gain; 137 138 /* Cache current values, to be returned while suspended */ 139 u32 data0; 140 u32 data1; 141}; 142 143static int tsl2563_write(struct i2c_client *client, u8 reg, u8 value) 144{ 145 int ret; 146 u8 buf[2]; 147 148 buf[0] = TSL2563_CMD | reg; 149 buf[1] = value; 150 151 ret = i2c_master_send(client, buf, sizeof(buf)); 152 return (ret == sizeof(buf)) ? 0 : ret; 153} 154 155static int tsl2563_read(struct i2c_client *client, u8 reg, void *buf, int len) 156{ 157 int ret; 158 u8 cmd = TSL2563_CMD | reg; 159 160 ret = i2c_master_send(client, &cmd, sizeof(cmd)); 161 if (ret != sizeof(cmd)) 162 return ret; 163 164 return i2c_master_recv(client, buf, len); 165} 166 167static int tsl2563_set_power(struct tsl2563_chip *chip, int on) 168{ 169 struct i2c_client *client = chip->client; 170 u8 cmd; 171 172 cmd = on ? TSL2563_CMD_POWER_ON : TSL2563_CMD_POWER_OFF; 173 return tsl2563_write(client, TSL2563_REG_CTRL, cmd); 174} 175 176/* 177 * Return value is 0 for off, 1 for on, or a negative error 178 * code if reading failed. 179 */ 180static int tsl2563_get_power(struct tsl2563_chip *chip) 181{ 182 struct i2c_client *client = chip->client; 183 int ret; 184 u8 val; 185 186 ret = tsl2563_read(client, TSL2563_REG_CTRL, &val, sizeof(val)); 187 if (ret != sizeof(val)) 188 return ret; 189 190 return (val & TSL2563_CTRL_POWER_MASK) == TSL2563_CMD_POWER_ON; 191} 192 193static int tsl2563_configure(struct tsl2563_chip *chip) 194{ 195 int ret; 196 197 ret = tsl2563_write(chip->client, TSL2563_REG_TIMING, 198 chip->gainlevel->gaintime); 199 if (ret) 200 goto error_ret; 201 ret = tsl2563_write(chip->client, TSL2563_REG_HIGHLOW, 202 chip->high_thres & 0xFF); 203 if (ret) 204 goto error_ret; 205 ret = tsl2563_write(chip->client, TSL2563_REG_HIGHHIGH, 206 (chip->high_thres >> 8) & 0xFF); 207 if (ret) 208 goto error_ret; 209 ret = tsl2563_write(chip->client, TSL2563_REG_LOWLOW, 210 chip->low_thres & 0xFF); 211 if (ret) 212 goto error_ret; 213 ret = tsl2563_write(chip->client, TSL2563_REG_LOWHIGH, 214 (chip->low_thres >> 8) & 0xFF); 215/* Interrupt register is automatically written anyway if it is relevant 216 so is not here */ 217error_ret: 218 return ret; 219} 220 221static void tsl2563_poweroff_work(struct work_struct *work) 222{ 223 struct tsl2563_chip *chip = 224 container_of(work, struct tsl2563_chip, poweroff_work.work); 225 tsl2563_set_power(chip, 0); 226} 227 228static int tsl2563_detect(struct tsl2563_chip *chip) 229{ 230 int ret; 231 232 ret = tsl2563_set_power(chip, 1); 233 if (ret) 234 return ret; 235 236 ret = tsl2563_get_power(chip); 237 if (ret < 0) 238 return ret; 239 240 return ret ? 0 : -ENODEV; 241} 242 243static int tsl2563_read_id(struct tsl2563_chip *chip, u8 *id) 244{ 245 struct i2c_client *client = chip->client; 246 int ret; 247 248 ret = tsl2563_read(client, TSL2563_REG_ID, id, sizeof(*id)); 249 if (ret != sizeof(*id)) 250 return ret; 251 252 return 0; 253} 254 255/* 256 * "Normalized" ADC value is one obtained with 400ms of integration time and 257 * 16x gain. This function returns the number of bits of shift needed to 258 * convert between normalized values and HW values obtained using given 259 * timing and gain settings. 260 */ 261static int adc_shiftbits(u8 timing) 262{ 263 int shift = 0; 264 265 switch (timing & TSL2563_TIMING_MASK) { 266 case TSL2563_TIMING_13MS: 267 shift += 5; 268 break; 269 case TSL2563_TIMING_100MS: 270 shift += 2; 271 break; 272 case TSL2563_TIMING_400MS: 273 /* no-op */ 274 break; 275 } 276 277 if (!(timing & TSL2563_TIMING_GAIN16)) 278 shift += 4; 279 280 return shift; 281} 282 283/* Convert a HW ADC value to normalized scale. */ 284static u32 normalize_adc(u16 adc, u8 timing) 285{ 286 return adc << adc_shiftbits(timing); 287} 288 289static void tsl2563_wait_adc(struct tsl2563_chip *chip) 290{ 291 unsigned int delay; 292 293 switch (chip->gainlevel->gaintime & TSL2563_TIMING_MASK) { 294 case TSL2563_TIMING_13MS: 295 delay = 14; 296 break; 297 case TSL2563_TIMING_100MS: 298 delay = 101; 299 break; 300 default: 301 delay = 402; 302 } 303 /* 304 * TODO: Make sure that we wait at least required delay but why we 305 * have to extend it one tick more? 306 */ 307 schedule_timeout_interruptible(msecs_to_jiffies(delay) + 2); 308} 309 310static int tsl2563_adjust_gainlevel(struct tsl2563_chip *chip, u16 adc) 311{ 312 struct i2c_client *client = chip->client; 313 314 if (adc > chip->gainlevel->max || adc < chip->gainlevel->min) { 315 316 (adc > chip->gainlevel->max) ? 317 chip->gainlevel++ : chip->gainlevel--; 318 319 tsl2563_write(client, TSL2563_REG_TIMING, 320 chip->gainlevel->gaintime); 321 322 tsl2563_wait_adc(chip); 323 tsl2563_wait_adc(chip); 324 325 return 1; 326 } else 327 return 0; 328} 329 330static int tsl2563_get_adc(struct tsl2563_chip *chip) 331{ 332 struct i2c_client *client = chip->client; 333 u8 buf0[2], buf1[2]; 334 u16 adc0, adc1; 335 int retry = 1; 336 int ret = 0; 337 338 if (chip->state.event != PM_EVENT_ON) 339 goto out; 340 341 if (!chip->int_enabled) { 342 cancel_delayed_work(&chip->poweroff_work); 343 344 if (!tsl2563_get_power(chip)) { 345 ret = tsl2563_set_power(chip, 1); 346 if (ret) 347 goto out; 348 ret = tsl2563_configure(chip); 349 if (ret) 350 goto out; 351 tsl2563_wait_adc(chip); 352 } 353 } 354 355 while (retry) { 356 ret = tsl2563_read(client, 357 TSL2563_REG_DATA0LOW, 358 buf0, sizeof(buf0)); 359 if (ret != sizeof(buf0)) 360 goto out; 361 362 ret = tsl2563_read(client, TSL2563_REG_DATA1LOW, 363 buf1, sizeof(buf1)); 364 if (ret != sizeof(buf1)) 365 goto out; 366 367 adc0 = (buf0[1] << 8) + buf0[0]; 368 adc1 = (buf1[1] << 8) + buf1[0]; 369 370 retry = tsl2563_adjust_gainlevel(chip, adc0); 371 } 372 373 chip->data0 = normalize_adc(adc0, chip->gainlevel->gaintime); 374 chip->data1 = normalize_adc(adc1, chip->gainlevel->gaintime); 375 376 if (!chip->int_enabled) 377 schedule_delayed_work(&chip->poweroff_work, 5 * HZ); 378 379 ret = 0; 380out: 381 return ret; 382} 383 384static inline int calib_to_sysfs(u32 calib) 385{ 386 return (int) (((calib * CALIB_BASE_SYSFS) + 387 CALIB_FRAC_HALF) >> CALIB_FRAC_BITS); 388} 389 390static inline u32 calib_from_sysfs(int value) 391{ 392 return (((u32) value) << CALIB_FRAC_BITS) / CALIB_BASE_SYSFS; 393} 394 395/* 396 * Conversions between lux and ADC values. 397 * 398 * The basic formula is lux = c0 * adc0 - c1 * adc1, where c0 and c1 are 399 * appropriate constants. Different constants are needed for different 400 * kinds of light, determined by the ratio adc1/adc0 (basically the ratio 401 * of the intensities in infrared and visible wavelengths). lux_table below 402 * lists the upper threshold of the adc1/adc0 ratio and the corresponding 403 * constants. 404 */ 405 406struct tsl2563_lux_coeff { 407 unsigned long ch_ratio; 408 unsigned long ch0_coeff; 409 unsigned long ch1_coeff; 410}; 411 412static const struct tsl2563_lux_coeff lux_table[] = { 413 { 414 .ch_ratio = FRAC10K(1300), 415 .ch0_coeff = FRAC10K(315), 416 .ch1_coeff = FRAC10K(262), 417 }, { 418 .ch_ratio = FRAC10K(2600), 419 .ch0_coeff = FRAC10K(337), 420 .ch1_coeff = FRAC10K(430), 421 }, { 422 .ch_ratio = FRAC10K(3900), 423 .ch0_coeff = FRAC10K(363), 424 .ch1_coeff = FRAC10K(529), 425 }, { 426 .ch_ratio = FRAC10K(5200), 427 .ch0_coeff = FRAC10K(392), 428 .ch1_coeff = FRAC10K(605), 429 }, { 430 .ch_ratio = FRAC10K(6500), 431 .ch0_coeff = FRAC10K(229), 432 .ch1_coeff = FRAC10K(291), 433 }, { 434 .ch_ratio = FRAC10K(8000), 435 .ch0_coeff = FRAC10K(157), 436 .ch1_coeff = FRAC10K(180), 437 }, { 438 .ch_ratio = FRAC10K(13000), 439 .ch0_coeff = FRAC10K(34), 440 .ch1_coeff = FRAC10K(26), 441 }, { 442 .ch_ratio = ULONG_MAX, 443 .ch0_coeff = 0, 444 .ch1_coeff = 0, 445 }, 446}; 447 448/* 449 * Convert normalized, scaled ADC values to lux. 450 */ 451static unsigned int adc_to_lux(u32 adc0, u32 adc1) 452{ 453 const struct tsl2563_lux_coeff *lp = lux_table; 454 unsigned long ratio, lux, ch0 = adc0, ch1 = adc1; 455 456 ratio = ch0 ? ((ch1 << ADC_FRAC_BITS) / ch0) : ULONG_MAX; 457 458 while (lp->ch_ratio < ratio) 459 lp++; 460 461 lux = ch0 * lp->ch0_coeff - ch1 * lp->ch1_coeff; 462 463 return (unsigned int) (lux >> ADC_FRAC_BITS); 464} 465 466/*--------------------------------------------------------------*/ 467/* Sysfs interface */ 468/*--------------------------------------------------------------*/ 469 470static ssize_t tsl2563_adc_show(struct device *dev, 471 struct device_attribute *attr, char *buf) 472{ 473 struct iio_dev *indio_dev = dev_get_drvdata(dev); 474 struct tsl2563_chip *chip = indio_dev->dev_data; 475 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 476 int ret; 477 478 mutex_lock(&chip->lock); 479 480 ret = tsl2563_get_adc(chip); 481 if (ret) 482 goto out; 483 484 switch (this_attr->address) { 485 case 0: 486 ret = snprintf(buf, PAGE_SIZE, "%d\n", chip->data0); 487 break; 488 case 1: 489 ret = snprintf(buf, PAGE_SIZE, "%d\n", chip->data1); 490 break; 491 } 492out: 493 mutex_unlock(&chip->lock); 494 return ret; 495} 496 497/* Apply calibration coefficient to ADC count. */ 498static u32 calib_adc(u32 adc, u32 calib) 499{ 500 unsigned long scaled = adc; 501 502 scaled *= calib; 503 scaled >>= CALIB_FRAC_BITS; 504 505 return (u32) scaled; 506} 507 508static ssize_t tsl2563_lux_show(struct device *dev, 509 struct device_attribute *attr, char *buf) 510{ 511 struct iio_dev *indio_dev = dev_get_drvdata(dev); 512 struct tsl2563_chip *chip = indio_dev->dev_data; 513 u32 calib0, calib1; 514 int ret; 515 516 mutex_lock(&chip->lock); 517 518 ret = tsl2563_get_adc(chip); 519 if (ret) 520 goto out; 521 522 calib0 = calib_adc(chip->data0, chip->calib0) * chip->cover_comp_gain; 523 calib1 = calib_adc(chip->data1, chip->calib1) * chip->cover_comp_gain; 524 525 ret = snprintf(buf, PAGE_SIZE, "%d\n", adc_to_lux(calib0, calib1)); 526 527out: 528 mutex_unlock(&chip->lock); 529 return ret; 530} 531 532static ssize_t format_calib(char *buf, int len, u32 calib) 533{ 534 return snprintf(buf, PAGE_SIZE, "%d\n", calib_to_sysfs(calib)); 535} 536 537static ssize_t tsl2563_calib_show(struct device *dev, 538 struct device_attribute *attr, char *buf) 539{ 540 struct iio_dev *indio_dev = dev_get_drvdata(dev); 541 struct tsl2563_chip *chip = indio_dev->dev_data; 542 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 543 int ret; 544 545 mutex_lock(&chip->lock); 546 switch (this_attr->address) { 547 case 0: 548 ret = format_calib(buf, PAGE_SIZE, chip->calib0); 549 break; 550 case 1: 551 ret = format_calib(buf, PAGE_SIZE, chip->calib1); 552 break; 553 default: 554 ret = -ENODEV; 555 } 556 mutex_unlock(&chip->lock); 557 return ret; 558} 559 560static ssize_t tsl2563_calib_store(struct device *dev, 561 struct device_attribute *attr, 562 const char *buf, size_t len) 563{ 564 struct iio_dev *indio_dev = dev_get_drvdata(dev); 565 struct tsl2563_chip *chip = indio_dev->dev_data; 566 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 567 int value; 568 u32 calib; 569 570 if (1 != sscanf(buf, "%d", &value)) 571 return -EINVAL; 572 573 calib = calib_from_sysfs(value); 574 575 switch (this_attr->address) { 576 case 0: 577 chip->calib0 = calib; 578 break; 579 case 1: 580 chip->calib1 = calib; 581 break; 582 } 583 584 return len; 585} 586 587static IIO_DEVICE_ATTR(intensity_both_raw, S_IRUGO, 588 tsl2563_adc_show, NULL, 0); 589static IIO_DEVICE_ATTR(intensity_ir_raw, S_IRUGO, 590 tsl2563_adc_show, NULL, 1); 591static DEVICE_ATTR(illuminance0_input, S_IRUGO, tsl2563_lux_show, NULL); 592static IIO_DEVICE_ATTR(intensity_both_calibgain, S_IRUGO | S_IWUSR, 593 tsl2563_calib_show, tsl2563_calib_store, 0); 594static IIO_DEVICE_ATTR(intensity_ir_calibgain, S_IRUGO | S_IWUSR, 595 tsl2563_calib_show, tsl2563_calib_store, 1); 596 597static ssize_t tsl2563_show_name(struct device *dev, 598 struct device_attribute *attr, 599 char *buf) 600{ 601 struct iio_dev *indio_dev = dev_get_drvdata(dev); 602 struct tsl2563_chip *chip = indio_dev->dev_data; 603 return sprintf(buf, "%s\n", chip->client->name); 604} 605 606static DEVICE_ATTR(name, S_IRUGO, tsl2563_show_name, NULL); 607 608static struct attribute *tsl2563_attributes[] = { 609 &iio_dev_attr_intensity_both_raw.dev_attr.attr, 610 &iio_dev_attr_intensity_ir_raw.dev_attr.attr, 611 &dev_attr_illuminance0_input.attr, 612 &iio_dev_attr_intensity_both_calibgain.dev_attr.attr, 613 &iio_dev_attr_intensity_ir_calibgain.dev_attr.attr, 614 &dev_attr_name.attr, 615 NULL 616}; 617 618static const struct attribute_group tsl2563_group = { 619 .attrs = tsl2563_attributes, 620}; 621 622static ssize_t tsl2563_read_thresh(struct device *dev, 623 struct device_attribute *attr, 624 char *buf) 625{ 626 struct iio_dev *indio_dev = dev_get_drvdata(dev); 627 struct tsl2563_chip *chip = indio_dev->dev_data; 628 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 629 u16 val = 0; 630 switch (this_attr->address) { 631 case TSL2563_REG_HIGHLOW: 632 val = chip->high_thres; 633 break; 634 case TSL2563_REG_LOWLOW: 635 val = chip->low_thres; 636 break; 637 } 638 return snprintf(buf, PAGE_SIZE, "%d\n", val); 639} 640 641static ssize_t tsl2563_write_thresh(struct device *dev, 642 struct device_attribute *attr, 643 const char *buf, 644 size_t len) 645{ 646 struct iio_dev *indio_dev = dev_get_drvdata(dev); 647 struct tsl2563_chip *chip = indio_dev->dev_data; 648 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 649 unsigned long val; 650 int ret; 651 652 ret = strict_strtoul(buf, 10, &val); 653 if (ret) 654 return ret; 655 mutex_lock(&chip->lock); 656 ret = tsl2563_write(chip->client, this_attr->address, val & 0xFF); 657 if (ret) 658 goto error_ret; 659 ret = tsl2563_write(chip->client, this_attr->address + 1, 660 (val >> 8) & 0xFF); 661 switch (this_attr->address) { 662 case TSL2563_REG_HIGHLOW: 663 chip->high_thres = val; 664 break; 665 case TSL2563_REG_LOWLOW: 666 chip->low_thres = val; 667 break; 668 } 669 670error_ret: 671 mutex_unlock(&chip->lock); 672 673 return ret < 0 ? ret : len; 674} 675 676static IIO_DEVICE_ATTR(intensity_both_thresh_high_value, 677 S_IRUGO | S_IWUSR, 678 tsl2563_read_thresh, 679 tsl2563_write_thresh, 680 TSL2563_REG_HIGHLOW); 681 682static IIO_DEVICE_ATTR(intensity_both_thresh_low_value, 683 S_IRUGO | S_IWUSR, 684 tsl2563_read_thresh, 685 tsl2563_write_thresh, 686 TSL2563_REG_LOWLOW); 687 688static int tsl2563_int_th(struct iio_dev *dev_info, 689 int index, 690 s64 timestamp, 691 int not_test) 692{ 693 struct tsl2563_chip *chip = dev_info->dev_data; 694 695 chip->event_timestamp = timestamp; 696 schedule_work(&chip->work_thresh); 697 698 return 0; 699} 700 701static void tsl2563_int_bh(struct work_struct *work_s) 702{ 703 struct tsl2563_chip *chip 704 = container_of(work_s, 705 struct tsl2563_chip, work_thresh); 706 u8 cmd = TSL2563_CMD | TSL2563_CLEARINT; 707 708 iio_push_event(chip->indio_dev, 0, 709 IIO_EVENT_CODE_LIGHT_BASE, 710 chip->event_timestamp); 711 712 /* reenable_irq */ 713 enable_irq(chip->client->irq); 714 /* clear the interrupt and push the event */ 715 i2c_master_send(chip->client, &cmd, sizeof(cmd)); 716 717} 718 719static ssize_t tsl2563_write_interrupt_config(struct device *dev, 720 struct device_attribute *attr, 721 const char *buf, 722 size_t len) 723{ 724 struct iio_dev *indio_dev = dev_get_drvdata(dev); 725 struct tsl2563_chip *chip = indio_dev->dev_data; 726 struct iio_event_attr *this_attr = to_iio_event_attr(attr); 727 int input, ret = 0; 728 729 ret = sscanf(buf, "%d", &input); 730 if (ret != 1) 731 return -EINVAL; 732 mutex_lock(&chip->lock); 733 if (input && !(chip->intr & 0x30)) { 734 iio_add_event_to_list(this_attr->listel, 735 &indio_dev->interrupts[0]->ev_list); 736 chip->intr &= ~0x30; 737 chip->intr |= 0x10; 738 /* ensure the chip is actually on */ 739 cancel_delayed_work(&chip->poweroff_work); 740 if (!tsl2563_get_power(chip)) { 741 ret = tsl2563_set_power(chip, 1); 742 if (ret) 743 goto out; 744 ret = tsl2563_configure(chip); 745 if (ret) 746 goto out; 747 } 748 ret = tsl2563_write(chip->client, TSL2563_REG_INT, chip->intr); 749 chip->int_enabled = true; 750 } 751 752 if (!input && (chip->intr & 0x30)) { 753 chip->intr |= ~0x30; 754 ret = tsl2563_write(chip->client, TSL2563_REG_INT, chip->intr); 755 iio_remove_event_from_list(this_attr->listel, 756 &indio_dev->interrupts[0]->ev_list); 757 chip->int_enabled = false; 758 /* now the interrupt is not enabled, we can go to sleep */ 759 schedule_delayed_work(&chip->poweroff_work, 5 * HZ); 760 } 761out: 762 mutex_unlock(&chip->lock); 763 764 return (ret < 0) ? ret : len; 765} 766 767static ssize_t tsl2563_read_interrupt_config(struct device *dev, 768 struct device_attribute *attr, 769 char *buf) 770{ 771 struct iio_dev *indio_dev = dev_get_drvdata(dev); 772 struct tsl2563_chip *chip = indio_dev->dev_data; 773 int ret; 774 u8 rxbuf; 775 ssize_t len; 776 777 mutex_lock(&chip->lock); 778 ret = tsl2563_read(chip->client, 779 TSL2563_REG_INT, 780 &rxbuf, 781 sizeof(rxbuf)); 782 mutex_unlock(&chip->lock); 783 if (ret < 0) 784 goto error_ret; 785 len = snprintf(buf, PAGE_SIZE, "%d\n", !!(rxbuf & 0x30)); 786error_ret: 787 788 return (ret < 0) ? ret : len; 789} 790 791IIO_EVENT_ATTR(intensity_both_thresh_both_en, 792 tsl2563_read_interrupt_config, 793 tsl2563_write_interrupt_config, 794 0, 795 tsl2563_int_th); 796 797static struct attribute *tsl2563_event_attributes[] = { 798 &iio_event_attr_intensity_both_thresh_both_en.dev_attr.attr, 799 &iio_dev_attr_intensity_both_thresh_high_value.dev_attr.attr, 800 &iio_dev_attr_intensity_both_thresh_low_value.dev_attr.attr, 801 NULL, 802}; 803 804static struct attribute_group tsl2563_event_attribute_group = { 805 .attrs = tsl2563_event_attributes, 806}; 807 808/*--------------------------------------------------------------*/ 809/* Probe, Attach, Remove */ 810/*--------------------------------------------------------------*/ 811static struct i2c_driver tsl2563_i2c_driver; 812 813static int __devinit tsl2563_probe(struct i2c_client *client, 814 const struct i2c_device_id *device_id) 815{ 816 struct tsl2563_chip *chip; 817 struct tsl2563_platform_data *pdata = client->dev.platform_data; 818 int err = 0; 819 int ret; 820 u8 id; 821 822 chip = kzalloc(sizeof(*chip), GFP_KERNEL); 823 if (!chip) 824 return -ENOMEM; 825 826 INIT_WORK(&chip->work_thresh, tsl2563_int_bh); 827 i2c_set_clientdata(client, chip); 828 chip->client = client; 829 830 err = tsl2563_detect(chip); 831 if (err) { 832 dev_err(&client->dev, "device not found, error %d\n", -err); 833 goto fail1; 834 } 835 836 err = tsl2563_read_id(chip, &id); 837 if (err) 838 goto fail1; 839 840 mutex_init(&chip->lock); 841 842 /* Default values used until userspace says otherwise */ 843 chip->low_thres = 0x0; 844 chip->high_thres = 0xffff; 845 chip->gainlevel = tsl2563_gainlevel_table; 846 chip->intr = TSL2563_INT_PERSIST(4); 847 chip->calib0 = calib_from_sysfs(CALIB_BASE_SYSFS); 848 chip->calib1 = calib_from_sysfs(CALIB_BASE_SYSFS); 849 850 if (pdata) 851 chip->cover_comp_gain = pdata->cover_comp_gain; 852 else 853 chip->cover_comp_gain = 1; 854 855 dev_info(&client->dev, "model %d, rev. %d\n", id >> 4, id & 0x0f); 856 857 chip->indio_dev = iio_allocate_device(); 858 if (!chip->indio_dev) 859 goto fail1; 860 chip->indio_dev->attrs = &tsl2563_group; 861 chip->indio_dev->dev.parent = &client->dev; 862 chip->indio_dev->dev_data = (void *)(chip); 863 chip->indio_dev->driver_module = THIS_MODULE; 864 chip->indio_dev->modes = INDIO_DIRECT_MODE; 865 if (client->irq) { 866 chip->indio_dev->num_interrupt_lines = 1; 867 chip->indio_dev->event_attrs 868 = &tsl2563_event_attribute_group; 869 } 870 ret = iio_device_register(chip->indio_dev); 871 if (ret) 872 goto fail1; 873 874 if (client->irq) { 875 ret = iio_register_interrupt_line(client->irq, 876 chip->indio_dev, 877 0, 878 IRQF_TRIGGER_RISING, 879 client->name); 880 if (ret) 881 goto fail2; 882 } 883 err = tsl2563_configure(chip); 884 if (err) 885 goto fail3; 886 887 INIT_DELAYED_WORK(&chip->poweroff_work, tsl2563_poweroff_work); 888 /* The interrupt cannot yet be enabled so this is fine without lock */ 889 schedule_delayed_work(&chip->poweroff_work, 5 * HZ); 890 891 return 0; 892fail3: 893 if (client->irq) 894 iio_unregister_interrupt_line(chip->indio_dev, 0); 895fail2: 896 iio_device_unregister(chip->indio_dev); 897fail1: 898 kfree(chip); 899 return err; 900} 901 902static int tsl2563_remove(struct i2c_client *client) 903{ 904 struct tsl2563_chip *chip = i2c_get_clientdata(client); 905 if (!chip->int_enabled) 906 cancel_delayed_work(&chip->poweroff_work); 907 /* Ensure that interrupts are disabled - then flush any bottom halves */ 908 chip->intr |= ~0x30; 909 tsl2563_write(chip->client, TSL2563_REG_INT, chip->intr); 910 flush_scheduled_work(); 911 tsl2563_set_power(chip, 0); 912 if (client->irq) 913 iio_unregister_interrupt_line(chip->indio_dev, 0); 914 iio_device_unregister(chip->indio_dev); 915 916 kfree(chip); 917 return 0; 918} 919 920static int tsl2563_suspend(struct i2c_client *client, pm_message_t state) 921{ 922 struct tsl2563_chip *chip = i2c_get_clientdata(client); 923 int ret; 924 925 mutex_lock(&chip->lock); 926 927 ret = tsl2563_set_power(chip, 0); 928 if (ret) 929 goto out; 930 931 chip->state = state; 932 933out: 934 mutex_unlock(&chip->lock); 935 return ret; 936} 937 938static int tsl2563_resume(struct i2c_client *client) 939{ 940 struct tsl2563_chip *chip = i2c_get_clientdata(client); 941 int ret; 942 943 mutex_lock(&chip->lock); 944 945 ret = tsl2563_set_power(chip, 1); 946 if (ret) 947 goto out; 948 949 ret = tsl2563_configure(chip); 950 if (ret) 951 goto out; 952 953 chip->state.event = PM_EVENT_ON; 954 955out: 956 mutex_unlock(&chip->lock); 957 return ret; 958} 959 960static const struct i2c_device_id tsl2563_id[] = { 961 { "tsl2560", 0 }, 962 { "tsl2561", 1 }, 963 { "tsl2562", 2 }, 964 { "tsl2563", 3 }, 965 {} 966}; 967MODULE_DEVICE_TABLE(i2c, tsl2563_id); 968 969static struct i2c_driver tsl2563_i2c_driver = { 970 .driver = { 971 .name = "tsl2563", 972 }, 973 .suspend = tsl2563_suspend, 974 .resume = tsl2563_resume, 975 .probe = tsl2563_probe, 976 .remove = __devexit_p(tsl2563_remove), 977 .id_table = tsl2563_id, 978}; 979 980static int __init tsl2563_init(void) 981{ 982 return i2c_add_driver(&tsl2563_i2c_driver); 983} 984 985static void __exit tsl2563_exit(void) 986{ 987 i2c_del_driver(&tsl2563_i2c_driver); 988} 989 990MODULE_AUTHOR("Nokia Corporation"); 991MODULE_DESCRIPTION("tsl2563 light sensor driver"); 992MODULE_LICENSE("GPL"); 993 994module_init(tsl2563_init); 995module_exit(tsl2563_exit); 996