2 * Device driver for monitoring ambient light intensity in (lux)
3 * and proximity detection (prox) within the TAOS TSL2X7X family of devices.
5 * Copyright (c) 2012, TAOS Corporation.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <linux/kernel.h>
23 #include <linux/i2c.h>
24 #include <linux/errno.h>
25 #include <linux/delay.h>
26 #include <linux/mutex.h>
27 #include <linux/interrupt.h>
28 #include <linux/slab.h>
29 #include <linux/module.h>
30 #include <linux/version.h>
31 #include <linux/iio/events.h>
32 #include <linux/iio/iio.h>
33 #include <linux/iio/sysfs.h>
37 #define PROX_STAT_CAL 0
38 #define PROX_STAT_SAMP 1
39 #define MAX_SAMPLES_CAL 200
41 /* TSL2X7X Device ID */
42 #define TRITON_ID 0x00
43 #define SWORDFISH_ID 0x30
44 #define HALIBUT_ID 0x20
46 /* Lux calculation constants */
47 #define TSL2X7X_LUX_CALC_OVER_FLOW 65535
49 /* TAOS Register definitions - note:
50 * depending on device, some of these register are not used and the
51 * register address is benign.
53 /* 2X7X register offsets */
54 #define TSL2X7X_MAX_CONFIG_REG 16
56 /* Device Registers and Masks */
57 #define TSL2X7X_CNTRL 0x00
58 #define TSL2X7X_ALS_TIME 0X01
59 #define TSL2X7X_PRX_TIME 0x02
60 #define TSL2X7X_WAIT_TIME 0x03
61 #define TSL2X7X_ALS_MINTHRESHLO 0X04
62 #define TSL2X7X_ALS_MINTHRESHHI 0X05
63 #define TSL2X7X_ALS_MAXTHRESHLO 0X06
64 #define TSL2X7X_ALS_MAXTHRESHHI 0X07
65 #define TSL2X7X_PRX_MINTHRESHLO 0X08
66 #define TSL2X7X_PRX_MINTHRESHHI 0X09
67 #define TSL2X7X_PRX_MAXTHRESHLO 0X0A
68 #define TSL2X7X_PRX_MAXTHRESHHI 0X0B
69 #define TSL2X7X_PERSISTENCE 0x0C
70 #define TSL2X7X_PRX_CONFIG 0x0D
71 #define TSL2X7X_PRX_COUNT 0x0E
72 #define TSL2X7X_GAIN 0x0F
73 #define TSL2X7X_NOTUSED 0x10
74 #define TSL2X7X_REVID 0x11
75 #define TSL2X7X_CHIPID 0x12
76 #define TSL2X7X_STATUS 0x13
77 #define TSL2X7X_ALS_CHAN0LO 0x14
78 #define TSL2X7X_ALS_CHAN0HI 0x15
79 #define TSL2X7X_ALS_CHAN1LO 0x16
80 #define TSL2X7X_ALS_CHAN1HI 0x17
81 #define TSL2X7X_PRX_LO 0x18
82 #define TSL2X7X_PRX_HI 0x19
84 /* tsl2X7X cmd reg masks */
85 #define TSL2X7X_CMD_REG 0x80
86 #define TSL2X7X_CMD_SPL_FN 0x60
88 #define TSL2X7X_CMD_PROX_INT_CLR 0X05
89 #define TSL2X7X_CMD_ALS_INT_CLR 0x06
90 #define TSL2X7X_CMD_PROXALS_INT_CLR 0X07
92 /* tsl2X7X cntrl reg masks */
93 #define TSL2X7X_CNTL_ADC_ENBL 0x02
94 #define TSL2X7X_CNTL_PWR_ON 0x01
96 /* tsl2X7X status reg masks */
97 #define TSL2X7X_STA_ADC_VALID 0x01
98 #define TSL2X7X_STA_PRX_VALID 0x02
99 #define TSL2X7X_STA_ADC_PRX_VALID (TSL2X7X_STA_ADC_VALID |\
100 TSL2X7X_STA_PRX_VALID)
101 #define TSL2X7X_STA_ALS_INTR 0x10
102 #define TSL2X7X_STA_PRX_INTR 0x20
104 /* tsl2X7X cntrl reg masks */
105 #define TSL2X7X_CNTL_REG_CLEAR 0x00
106 #define TSL2X7X_CNTL_PROX_INT_ENBL 0X20
107 #define TSL2X7X_CNTL_ALS_INT_ENBL 0X10
108 #define TSL2X7X_CNTL_WAIT_TMR_ENBL 0X08
109 #define TSL2X7X_CNTL_PROX_DET_ENBL 0X04
110 #define TSL2X7X_CNTL_PWRON 0x01
111 #define TSL2X7X_CNTL_ALSPON_ENBL 0x03
112 #define TSL2X7X_CNTL_INTALSPON_ENBL 0x13
113 #define TSL2X7X_CNTL_PROXPON_ENBL 0x0F
114 #define TSL2X7X_CNTL_INTPROXPON_ENBL 0x2F
116 /*Prox diode to use */
117 #define TSL2X7X_DIODE0 0x10
118 #define TSL2X7X_DIODE1 0x20
119 #define TSL2X7X_DIODE_BOTH 0x30
122 #define TSL2X7X_mA100 0x00
123 #define TSL2X7X_mA50 0x40
124 #define TSL2X7X_mA25 0x80
125 #define TSL2X7X_mA13 0xD0
126 #define TSL2X7X_MAX_TIMER_CNT (0xFF)
128 /*Common device IIO EventMask */
129 #define TSL2X7X_EVENT_MASK \
130 (IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING) | \
131 IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING)),
133 #define TSL2X7X_MIN_ITIME 3
135 /* TAOS txx2x7x Device family members */
150 TSL2X7X_CHIP_UNKNOWN = 0,
151 TSL2X7X_CHIP_WORKING = 1,
152 TSL2X7X_CHIP_SUSPENDED = 2
155 struct tsl2x7x_parse_result {
160 /* Per-device data */
161 struct tsl2x7x_als_info {
167 struct tsl2x7x_prox_stat {
171 unsigned long stddev;
174 struct tsl2x7x_chip_info {
175 int chan_table_elements;
176 struct iio_chan_spec channel[4];
177 const struct iio_info *info;
180 struct tsl2X7X_chip {
182 struct mutex prox_mutex;
183 struct mutex als_mutex;
184 struct i2c_client *client;
186 struct tsl2x7x_als_info als_cur_info;
187 struct tsl2x7x_settings tsl2x7x_settings;
188 struct tsl2X7X_platform_data *pdata;
191 int tsl2x7x_chip_status;
192 u8 tsl2x7x_config[TSL2X7X_MAX_CONFIG_REG];
193 const struct tsl2x7x_chip_info *chip_info;
194 const struct iio_info *info;
196 /* This structure is intentionally large to accommodate
197 * updates via sysfs. */
198 /* Sized to 9 = max 8 segments + 1 termination segment */
199 struct tsl2x7x_lux tsl2x7x_device_lux[TSL2X7X_MAX_LUX_TABLE_SIZE];
202 /* Different devices require different coefficents */
203 static const struct tsl2x7x_lux tsl2x71_lux_table[] = {
204 { 14461, 611, 1211 },
209 static const struct tsl2x7x_lux tmd2x71_lux_table[] = {
215 static const struct tsl2x7x_lux tsl2x72_lux_table[] = {
221 static const struct tsl2x7x_lux tmd2x72_lux_table[] = {
227 static const struct tsl2x7x_lux *tsl2x7x_default_lux_table_group[] = {
228 [tsl2571] = tsl2x71_lux_table,
229 [tsl2671] = tsl2x71_lux_table,
230 [tmd2671] = tmd2x71_lux_table,
231 [tsl2771] = tsl2x71_lux_table,
232 [tmd2771] = tmd2x71_lux_table,
233 [tsl2572] = tsl2x72_lux_table,
234 [tsl2672] = tsl2x72_lux_table,
235 [tmd2672] = tmd2x72_lux_table,
236 [tsl2772] = tsl2x72_lux_table,
237 [tmd2772] = tmd2x72_lux_table,
240 static const struct tsl2x7x_settings tsl2x7x_default_settings = {
241 .als_time = 219, /* 101 ms */
243 .prx_time = 254, /* 5.4 ms */
247 .als_gain_trim = 1000,
248 .als_cal_target = 150,
249 .als_thresh_low = 200,
250 .als_thresh_high = 256,
254 .prox_thres_high = 512,
255 .prox_max_samples_cal = 30,
256 .prox_pulse_count = 8
259 static const s16 tsl2X7X_als_gainadj[] = {
266 static const s16 tsl2X7X_prx_gainadj[] = {
273 /* Channel variations */
282 static const u8 device_channel_config[] = {
296 * tsl2x7x_parse_buffer() - parse a decimal result from a buffer.
297 * @*buf: pointer to char buffer to parse
298 * @*result: pointer to buffer to contain
299 * resulting interger / decimal as ints.
303 tsl2x7x_parse_buffer(const char *buf, struct tsl2x7x_parse_result *result)
305 int integer = 0, fract = 0, fract_mult = 100000;
306 bool integer_part = true, negative = false;
314 if ('0' <= *buf && *buf <= '9') {
316 integer = integer*10 + *buf - '0';
318 fract += fract_mult*(*buf - '0');
323 } else if (*buf == '\n') {
324 if (*(buf + 1) == '\0')
328 } else if (*buf == '.') {
329 integer_part = false;
342 result->integer = integer;
343 result->fract = fract;
349 * tsl2x7x_i2c_read() - Read a byte from a register.
350 * @client: i2c client
351 * @reg: device register to read from
352 * @*val: pointer to location to store register contents.
356 tsl2x7x_i2c_read(struct i2c_client *client, u8 reg, u8 *val)
360 /* select register to write */
361 ret = i2c_smbus_write_byte(client, (TSL2X7X_CMD_REG | reg));
363 dev_err(&client->dev, "%s: failed to write register %x\n"
369 ret = i2c_smbus_read_byte(client);
373 dev_err(&client->dev, "%s: failed to read register %x\n"
380 * tsl2x7x_get_lux() - Reads and calculates current lux value.
381 * @indio_dev: pointer to IIO device
383 * The raw ch0 and ch1 values of the ambient light sensed in the last
384 * integration cycle are read from the device.
385 * Time scale factor array values are adjusted based on the integration time.
386 * The raw values are multiplied by a scale factor, and device gain is obtained
387 * using gain index. Limit checks are done next, then the ratio of a multiple
388 * of ch1 value, to the ch0 value, is calculated. Array tsl2x7x_device_lux[]
389 * is then scanned to find the first ratio value that is just above the ratio
390 * we just calculated. The ch0 and ch1 multiplier constants in the array are
391 * then used along with the time scale factor array values, to calculate the
394 static int tsl2x7x_get_lux(struct iio_dev *indio_dev)
396 u16 ch0, ch1; /* separated ch0/ch1 data from device */
397 u32 lux; /* raw lux calculated from device data */
401 struct tsl2x7x_lux *p;
402 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
407 if (mutex_trylock(&chip->als_mutex) == 0)
408 return chip->als_cur_info.lux; /* busy, so return LAST VALUE */
410 if (chip->tsl2x7x_chip_status != TSL2X7X_CHIP_WORKING) {
411 /* device is not enabled */
412 dev_err(&chip->client->dev, "%s: device is not enabled\n",
418 ret = tsl2x7x_i2c_read(chip->client,
419 (TSL2X7X_CMD_REG | TSL2X7X_STATUS), &buf[0]);
421 dev_err(&chip->client->dev,
422 "%s: Failed to read STATUS Reg\n", __func__);
425 /* is data new & valid */
426 if (!(buf[0] & TSL2X7X_STA_ADC_VALID)) {
427 dev_err(&chip->client->dev,
428 "%s: data not valid yet\n", __func__);
429 ret = chip->als_cur_info.lux; /* return LAST VALUE */
433 for (i = 0; i < 4; i++) {
434 ret = tsl2x7x_i2c_read(chip->client,
435 (TSL2X7X_CMD_REG | (TSL2X7X_ALS_CHAN0LO + i)),
438 dev_err(&chip->client->dev,
439 "%s: failed to read. err=%x\n", __func__, ret);
444 /* clear any existing interrupt status */
445 ret = i2c_smbus_write_byte(chip->client,
448 TSL2X7X_CMD_ALS_INT_CLR));
450 dev_err(&chip->client->dev,
451 "%s: i2c_write_command failed - err = %d\n",
453 goto out_unlock; /* have no data, so return failure */
456 /* extract ALS/lux data */
457 ch0 = le16_to_cpup((const __le16 *)&buf[0]);
458 ch1 = le16_to_cpup((const __le16 *)&buf[2]);
460 chip->als_cur_info.als_ch0 = ch0;
461 chip->als_cur_info.als_ch1 = ch1;
463 if ((ch0 >= chip->als_saturation) || (ch1 >= chip->als_saturation)) {
464 lux = TSL2X7X_LUX_CALC_OVER_FLOW;
469 /* have no data, so return LAST VALUE */
470 ret = chip->als_cur_info.lux;
473 /* calculate ratio */
474 ratio = (ch1 << 15) / ch0;
475 /* convert to unscaled lux using the pointer to the table */
476 p = (struct tsl2x7x_lux *) chip->tsl2x7x_device_lux;
477 while (p->ratio != 0 && p->ratio < ratio)
483 ch0lux = DIV_ROUND_UP((ch0 * p->ch0),
484 tsl2X7X_als_gainadj[chip->tsl2x7x_settings.als_gain]);
485 ch1lux = DIV_ROUND_UP((ch1 * p->ch1),
486 tsl2X7X_als_gainadj[chip->tsl2x7x_settings.als_gain]);
487 lux = ch0lux - ch1lux;
490 /* note: lux is 31 bit max at this point */
491 if (ch1lux > ch0lux) {
492 dev_dbg(&chip->client->dev, "ch1lux > ch0lux-return last value\n");
493 ret = chip->als_cur_info.lux;
497 /* adjust for active time scale */
498 if (chip->als_time_scale == 0)
501 lux = (lux + (chip->als_time_scale >> 1)) /
502 chip->als_time_scale;
504 /* adjust for active gain scale
505 * The tsl2x7x_device_lux tables have a factor of 256 built-in.
506 * User-specified gain provides a multiplier.
507 * Apply user-specified gain before shifting right to retain precision.
508 * Use 64 bits to avoid overflow on multiplication.
509 * Then go back to 32 bits before division to avoid using div_u64().
513 lux64 = lux64 * chip->tsl2x7x_settings.als_gain_trim;
516 lux = (lux + 500) / 1000;
518 if (lux > TSL2X7X_LUX_CALC_OVER_FLOW) /* check for overflow */
519 lux = TSL2X7X_LUX_CALC_OVER_FLOW;
521 /* Update the structure with the latest lux. */
523 chip->als_cur_info.lux = lux;
527 mutex_unlock(&chip->als_mutex);
533 * tsl2x7x_get_prox() - Reads proximity data registers and updates
536 * @indio_dev: pointer to IIO device
538 static int tsl2x7x_get_prox(struct iio_dev *indio_dev)
544 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
546 if (mutex_trylock(&chip->prox_mutex) == 0) {
547 dev_err(&chip->client->dev,
548 "%s: Can't get prox mutex\n", __func__);
552 ret = tsl2x7x_i2c_read(chip->client,
553 (TSL2X7X_CMD_REG | TSL2X7X_STATUS), &status);
555 dev_err(&chip->client->dev,
556 "%s: i2c err=%d\n", __func__, ret);
566 if (!(status & TSL2X7X_STA_ADC_VALID))
574 if (!(status & TSL2X7X_STA_PRX_VALID))
579 for (i = 0; i < 2; i++) {
580 ret = tsl2x7x_i2c_read(chip->client,
582 (TSL2X7X_PRX_LO + i)), &chdata[i]);
588 le16_to_cpup((const __le16 *)&chdata[0]);
592 mutex_unlock(&chip->prox_mutex);
594 return chip->prox_data;
598 * tsl2x7x_defaults() - Populates the device nominal operating parameters
599 * with those provided by a 'platform' data struct or
600 * with prefined defaults.
602 * @chip: pointer to device structure.
604 static void tsl2x7x_defaults(struct tsl2X7X_chip *chip)
606 /* If Operational settings defined elsewhere.. */
607 if (chip->pdata && chip->pdata->platform_default_settings != 0)
608 memcpy(&(chip->tsl2x7x_settings),
609 chip->pdata->platform_default_settings,
610 sizeof(tsl2x7x_default_settings));
612 memcpy(&(chip->tsl2x7x_settings),
613 &tsl2x7x_default_settings,
614 sizeof(tsl2x7x_default_settings));
616 /* Load up the proper lux table. */
617 if (chip->pdata && chip->pdata->platform_lux_table[0].ratio != 0)
618 memcpy(chip->tsl2x7x_device_lux,
619 chip->pdata->platform_lux_table,
620 sizeof(chip->pdata->platform_lux_table));
622 memcpy(chip->tsl2x7x_device_lux,
623 (struct tsl2x7x_lux *)tsl2x7x_default_lux_table_group[chip->id],
624 MAX_DEFAULT_TABLE_BYTES);
628 * tsl2x7x_als_calibrate() - Obtain single reading and calculate
631 * @indio_dev: pointer to IIO device
633 static int tsl2x7x_als_calibrate(struct iio_dev *indio_dev)
635 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
641 ret = i2c_smbus_write_byte(chip->client,
642 (TSL2X7X_CMD_REG | TSL2X7X_CNTRL));
644 dev_err(&chip->client->dev,
645 "%s: failed to write CNTRL register, ret=%d\n",
650 reg_val = i2c_smbus_read_byte(chip->client);
651 if ((reg_val & (TSL2X7X_CNTL_ADC_ENBL | TSL2X7X_CNTL_PWR_ON))
652 != (TSL2X7X_CNTL_ADC_ENBL | TSL2X7X_CNTL_PWR_ON)) {
653 dev_err(&chip->client->dev,
654 "%s: failed: ADC not enabled\n", __func__);
658 ret = i2c_smbus_write_byte(chip->client,
659 (TSL2X7X_CMD_REG | TSL2X7X_CNTRL));
661 dev_err(&chip->client->dev,
662 "%s: failed to write ctrl reg: ret=%d\n",
667 reg_val = i2c_smbus_read_byte(chip->client);
668 if ((reg_val & TSL2X7X_STA_ADC_VALID) != TSL2X7X_STA_ADC_VALID) {
669 dev_err(&chip->client->dev,
670 "%s: failed: STATUS - ADC not valid.\n", __func__);
674 lux_val = tsl2x7x_get_lux(indio_dev);
676 dev_err(&chip->client->dev,
677 "%s: failed to get lux\n", __func__);
681 gain_trim_val = (((chip->tsl2x7x_settings.als_cal_target)
682 * chip->tsl2x7x_settings.als_gain_trim) / lux_val);
683 if ((gain_trim_val < 250) || (gain_trim_val > 4000))
686 chip->tsl2x7x_settings.als_gain_trim = gain_trim_val;
687 dev_info(&chip->client->dev,
688 "%s als_calibrate completed\n", chip->client->name);
690 return (int) gain_trim_val;
693 static int tsl2x7x_chip_on(struct iio_dev *indio_dev)
701 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
704 if (chip->pdata && chip->pdata->power_on)
705 chip->pdata->power_on(indio_dev);
707 /* Non calculated parameters */
708 chip->tsl2x7x_config[TSL2X7X_PRX_TIME] =
709 chip->tsl2x7x_settings.prx_time;
710 chip->tsl2x7x_config[TSL2X7X_WAIT_TIME] =
711 chip->tsl2x7x_settings.wait_time;
712 chip->tsl2x7x_config[TSL2X7X_PRX_CONFIG] =
713 chip->tsl2x7x_settings.prox_config;
715 chip->tsl2x7x_config[TSL2X7X_ALS_MINTHRESHLO] =
716 (chip->tsl2x7x_settings.als_thresh_low) & 0xFF;
717 chip->tsl2x7x_config[TSL2X7X_ALS_MINTHRESHHI] =
718 (chip->tsl2x7x_settings.als_thresh_low >> 8) & 0xFF;
719 chip->tsl2x7x_config[TSL2X7X_ALS_MAXTHRESHLO] =
720 (chip->tsl2x7x_settings.als_thresh_high) & 0xFF;
721 chip->tsl2x7x_config[TSL2X7X_ALS_MAXTHRESHHI] =
722 (chip->tsl2x7x_settings.als_thresh_high >> 8) & 0xFF;
723 chip->tsl2x7x_config[TSL2X7X_PERSISTENCE] =
724 chip->tsl2x7x_settings.persistence;
726 chip->tsl2x7x_config[TSL2X7X_PRX_COUNT] =
727 chip->tsl2x7x_settings.prox_pulse_count;
728 chip->tsl2x7x_config[TSL2X7X_PRX_MINTHRESHLO] =
729 chip->tsl2x7x_settings.prox_thres_low;
730 chip->tsl2x7x_config[TSL2X7X_PRX_MAXTHRESHLO] =
731 chip->tsl2x7x_settings.prox_thres_high;
733 /* and make sure we're not already on */
734 if (chip->tsl2x7x_chip_status == TSL2X7X_CHIP_WORKING) {
735 /* if forcing a register update - turn off, then on */
736 dev_info(&chip->client->dev, "device is already enabled\n");
740 /* determine als integration regster */
741 als_count = (chip->tsl2x7x_settings.als_time * 100 + 135) / 270;
743 als_count = 1; /* ensure at least one cycle */
745 /* convert back to time (encompasses overrides) */
746 als_time = (als_count * 27 + 5) / 10;
747 chip->tsl2x7x_config[TSL2X7X_ALS_TIME] = 256 - als_count;
749 /* Set the gain based on tsl2x7x_settings struct */
750 chip->tsl2x7x_config[TSL2X7X_GAIN] =
751 (chip->tsl2x7x_settings.als_gain |
752 (TSL2X7X_mA100 | TSL2X7X_DIODE1)
753 | ((chip->tsl2x7x_settings.prox_gain) << 2));
755 /* set chip struct re scaling and saturation */
756 chip->als_saturation = als_count * 922; /* 90% of full scale */
757 chip->als_time_scale = (als_time + 25) / 50;
759 /* TSL2X7X Specific power-on / adc enable sequence
760 * Power on the device 1st. */
761 utmp = TSL2X7X_CNTL_PWR_ON;
762 ret = i2c_smbus_write_byte_data(chip->client,
763 TSL2X7X_CMD_REG | TSL2X7X_CNTRL, utmp);
765 dev_err(&chip->client->dev,
766 "%s: failed on CNTRL reg.\n", __func__);
770 /* Use the following shadow copy for our delay before enabling ADC.
771 * Write all the registers. */
772 for (i = 0, dev_reg = chip->tsl2x7x_config;
773 i < TSL2X7X_MAX_CONFIG_REG; i++) {
774 ret = i2c_smbus_write_byte_data(chip->client,
775 TSL2X7X_CMD_REG + i, *dev_reg++);
777 dev_err(&chip->client->dev,
778 "%s: failed on write to reg %d.\n", __func__, i);
783 mdelay(3); /* Power-on settling time */
785 /* NOW enable the ADC
786 * initialize the desired mode of operation */
787 utmp = TSL2X7X_CNTL_PWR_ON |
788 TSL2X7X_CNTL_ADC_ENBL |
789 TSL2X7X_CNTL_PROX_DET_ENBL;
790 ret = i2c_smbus_write_byte_data(chip->client,
791 TSL2X7X_CMD_REG | TSL2X7X_CNTRL, utmp);
793 dev_err(&chip->client->dev,
794 "%s: failed on 2nd CTRL reg.\n", __func__);
798 chip->tsl2x7x_chip_status = TSL2X7X_CHIP_WORKING;
800 if (chip->tsl2x7x_settings.interrupts_en != 0) {
801 dev_info(&chip->client->dev, "Setting Up Interrupt(s)\n");
803 reg_val = TSL2X7X_CNTL_PWR_ON | TSL2X7X_CNTL_ADC_ENBL;
804 if ((chip->tsl2x7x_settings.interrupts_en == 0x20) ||
805 (chip->tsl2x7x_settings.interrupts_en == 0x30))
806 reg_val |= TSL2X7X_CNTL_PROX_DET_ENBL;
808 reg_val |= chip->tsl2x7x_settings.interrupts_en;
809 ret = i2c_smbus_write_byte_data(chip->client,
810 (TSL2X7X_CMD_REG | TSL2X7X_CNTRL), reg_val);
812 dev_err(&chip->client->dev,
813 "%s: failed in tsl2x7x_IOCTL_INT_SET.\n",
816 /* Clear out any initial interrupts */
817 ret = i2c_smbus_write_byte(chip->client,
818 TSL2X7X_CMD_REG | TSL2X7X_CMD_SPL_FN |
819 TSL2X7X_CMD_PROXALS_INT_CLR);
821 dev_err(&chip->client->dev,
822 "%s: Failed to clear Int status\n",
831 static int tsl2x7x_chip_off(struct iio_dev *indio_dev)
834 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
836 /* turn device off */
837 chip->tsl2x7x_chip_status = TSL2X7X_CHIP_SUSPENDED;
839 ret = i2c_smbus_write_byte_data(chip->client,
840 TSL2X7X_CMD_REG | TSL2X7X_CNTRL, 0x00);
842 if (chip->pdata && chip->pdata->power_off)
843 chip->pdata->power_off(chip->client);
849 * tsl2x7x_invoke_change
850 * @indio_dev: pointer to IIO device
852 * Obtain and lock both ALS and PROX resources,
853 * determine and save device state (On/Off),
854 * cycle device to implement updated parameter,
855 * put device back into proper state, and unlock
859 int tsl2x7x_invoke_change(struct iio_dev *indio_dev)
861 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
862 int device_status = chip->tsl2x7x_chip_status;
864 mutex_lock(&chip->als_mutex);
865 mutex_lock(&chip->prox_mutex);
867 if (device_status == TSL2X7X_CHIP_WORKING)
868 tsl2x7x_chip_off(indio_dev);
870 tsl2x7x_chip_on(indio_dev);
872 if (device_status != TSL2X7X_CHIP_WORKING)
873 tsl2x7x_chip_off(indio_dev);
875 mutex_unlock(&chip->prox_mutex);
876 mutex_unlock(&chip->als_mutex);
882 void tsl2x7x_prox_calculate(int *data, int length,
883 struct tsl2x7x_prox_stat *statP)
893 statP->min = INT_MAX;
894 statP->max = INT_MIN;
895 for (i = 0; i < length; i++) {
896 sample_sum += data[i];
897 statP->min = min(statP->min, data[i]);
898 statP->max = max(statP->max, data[i]);
901 statP->mean = sample_sum / length;
903 for (i = 0; i < length; i++) {
904 tmp = data[i] - statP->mean;
905 sample_sum += tmp * tmp;
907 statP->stddev = int_sqrt((long)sample_sum)/length;
911 * tsl2x7x_prox_cal() - Calculates std. and sets thresholds.
912 * @indio_dev: pointer to IIO device
914 * Calculates a standard deviation based on the samples,
915 * and sets the threshold accordingly.
917 static void tsl2x7x_prox_cal(struct iio_dev *indio_dev)
919 int prox_history[MAX_SAMPLES_CAL + 1];
921 struct tsl2x7x_prox_stat prox_stat_data[2];
922 struct tsl2x7x_prox_stat *calP;
923 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
925 u8 current_state = chip->tsl2x7x_chip_status;
927 if (chip->tsl2x7x_settings.prox_max_samples_cal > MAX_SAMPLES_CAL) {
928 dev_err(&chip->client->dev,
929 "%s: max prox samples cal is too big: %d\n",
930 __func__, chip->tsl2x7x_settings.prox_max_samples_cal);
931 chip->tsl2x7x_settings.prox_max_samples_cal = MAX_SAMPLES_CAL;
934 /* have to stop to change settings */
935 tsl2x7x_chip_off(indio_dev);
937 /* Enable proximity detection save just in case prox not wanted yet*/
938 tmp_irq_settings = chip->tsl2x7x_settings.interrupts_en;
939 chip->tsl2x7x_settings.interrupts_en |= TSL2X7X_CNTL_PROX_INT_ENBL;
941 /*turn on device if not already on*/
942 tsl2x7x_chip_on(indio_dev);
944 /*gather the samples*/
945 for (i = 0; i < chip->tsl2x7x_settings.prox_max_samples_cal; i++) {
947 tsl2x7x_get_prox(indio_dev);
948 prox_history[i] = chip->prox_data;
949 dev_info(&chip->client->dev, "2 i=%d prox data= %d\n",
953 tsl2x7x_chip_off(indio_dev);
954 calP = &prox_stat_data[PROX_STAT_CAL];
955 tsl2x7x_prox_calculate(prox_history,
956 chip->tsl2x7x_settings.prox_max_samples_cal, calP);
957 chip->tsl2x7x_settings.prox_thres_high = (calP->max << 1) - calP->mean;
959 dev_info(&chip->client->dev, " cal min=%d mean=%d max=%d\n",
960 calP->min, calP->mean, calP->max);
961 dev_info(&chip->client->dev,
962 "%s proximity threshold set to %d\n",
963 chip->client->name, chip->tsl2x7x_settings.prox_thres_high);
965 /* back to the way they were */
966 chip->tsl2x7x_settings.interrupts_en = tmp_irq_settings;
967 if (current_state == TSL2X7X_CHIP_WORKING)
968 tsl2x7x_chip_on(indio_dev);
971 static ssize_t tsl2x7x_power_state_show(struct device *dev,
972 struct device_attribute *attr, char *buf)
974 struct tsl2X7X_chip *chip = iio_priv(dev_get_drvdata(dev));
976 return snprintf(buf, PAGE_SIZE, "%d\n", chip->tsl2x7x_chip_status);
979 static ssize_t tsl2x7x_power_state_store(struct device *dev,
980 struct device_attribute *attr, const char *buf, size_t len)
982 struct iio_dev *indio_dev = dev_get_drvdata(dev);
985 if (strtobool(buf, &value))
989 tsl2x7x_chip_on(indio_dev);
991 tsl2x7x_chip_off(indio_dev);
996 static ssize_t tsl2x7x_gain_available_show(struct device *dev,
997 struct device_attribute *attr, char *buf)
999 struct tsl2X7X_chip *chip = iio_priv(dev_get_drvdata(dev));
1007 return snprintf(buf, PAGE_SIZE, "%s\n", "1 8 16 128");
1011 return snprintf(buf, PAGE_SIZE, "%s\n", "1 8 16 120");
1014 static ssize_t tsl2x7x_prox_gain_available_show(struct device *dev,
1015 struct device_attribute *attr, char *buf)
1017 return snprintf(buf, PAGE_SIZE, "%s\n", "1 2 4 8");
1020 static ssize_t tsl2x7x_als_time_show(struct device *dev,
1021 struct device_attribute *attr, char *buf)
1023 struct tsl2X7X_chip *chip = iio_priv(dev_get_drvdata(dev));
1026 y = (TSL2X7X_MAX_TIMER_CNT - (u8)chip->tsl2x7x_settings.als_time) + 1;
1027 z = y * TSL2X7X_MIN_ITIME;
1031 return snprintf(buf, PAGE_SIZE, "%d.%03d\n", y, z);
1034 static ssize_t tsl2x7x_als_time_store(struct device *dev,
1035 struct device_attribute *attr, const char *buf, size_t len)
1037 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1038 struct tsl2X7X_chip *chip = iio_priv(dev_get_drvdata(dev));
1039 struct tsl2x7x_parse_result result;
1044 tsl2x7x_parse_buffer(buf, &result);
1046 result.fract /= 1000;
1048 chip->tsl2x7x_settings.als_time =
1049 (TSL2X7X_MAX_TIMER_CNT - (u8)result.fract);
1051 dev_info(&chip->client->dev, "%s: als time = %d",
1052 __func__, chip->tsl2x7x_settings.als_time);
1054 tsl2x7x_invoke_change(indio_dev);
1056 return IIO_VAL_INT_PLUS_MICRO;
1059 static IIO_CONST_ATTR(in_illuminance0_integration_time_available,
1062 static ssize_t tsl2x7x_als_cal_target_show(struct device *dev,
1063 struct device_attribute *attr, char *buf)
1065 struct tsl2X7X_chip *chip = iio_priv(dev_get_drvdata(dev));
1067 return snprintf(buf, PAGE_SIZE, "%d\n",
1068 chip->tsl2x7x_settings.als_cal_target);
1071 static ssize_t tsl2x7x_als_cal_target_store(struct device *dev,
1072 struct device_attribute *attr, const char *buf, size_t len)
1074 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1075 struct tsl2X7X_chip *chip = iio_priv(dev_get_drvdata(dev));
1076 unsigned long value;
1078 if (kstrtoul(buf, 0, &value))
1082 chip->tsl2x7x_settings.als_cal_target = value;
1084 tsl2x7x_invoke_change(indio_dev);
1089 /* persistence settings */
1090 static ssize_t tsl2x7x_als_persistence_show(struct device *dev,
1091 struct device_attribute *attr, char *buf)
1093 struct tsl2X7X_chip *chip = iio_priv(dev_get_drvdata(dev));
1094 int y, z, filter_delay;
1096 /* Determine integration time */
1097 y = (TSL2X7X_MAX_TIMER_CNT - (u8)chip->tsl2x7x_settings.als_time) + 1;
1098 z = y * TSL2X7X_MIN_ITIME;
1099 filter_delay = z * (chip->tsl2x7x_settings.persistence & 0x0F);
1100 y = (filter_delay / 1000);
1101 z = (filter_delay % 1000);
1103 return snprintf(buf, PAGE_SIZE, "%d.%03d\n", y, z);
1106 static ssize_t tsl2x7x_als_persistence_store(struct device *dev,
1107 struct device_attribute *attr, const char *buf, size_t len)
1109 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1110 struct tsl2X7X_chip *chip = iio_priv(dev_get_drvdata(dev));
1111 struct tsl2x7x_parse_result result;
1112 int y, z, filter_delay;
1116 tsl2x7x_parse_buffer(buf, &result);
1118 result.fract /= 1000;
1119 y = (TSL2X7X_MAX_TIMER_CNT - (u8)chip->tsl2x7x_settings.als_time) + 1;
1120 z = y * TSL2X7X_MIN_ITIME;
1123 DIV_ROUND_UP(((result.integer * 1000) + result.fract), z);
1125 chip->tsl2x7x_settings.persistence &= 0xF0;
1126 chip->tsl2x7x_settings.persistence |= (filter_delay & 0x0F);
1128 dev_info(&chip->client->dev, "%s: als persistence = %d",
1129 __func__, filter_delay);
1131 tsl2x7x_invoke_change(indio_dev);
1133 return IIO_VAL_INT_PLUS_MICRO;
1136 static ssize_t tsl2x7x_prox_persistence_show(struct device *dev,
1137 struct device_attribute *attr, char *buf)
1139 struct tsl2X7X_chip *chip = iio_priv(dev_get_drvdata(dev));
1140 int y, z, filter_delay;
1142 /* Determine integration time */
1143 y = (TSL2X7X_MAX_TIMER_CNT - (u8)chip->tsl2x7x_settings.prx_time) + 1;
1144 z = y * TSL2X7X_MIN_ITIME;
1145 filter_delay = z * ((chip->tsl2x7x_settings.persistence & 0xF0) >> 4);
1146 y = (filter_delay / 1000);
1147 z = (filter_delay % 1000);
1149 return snprintf(buf, PAGE_SIZE, "%d.%03d\n", y, z);
1152 static ssize_t tsl2x7x_prox_persistence_store(struct device *dev,
1153 struct device_attribute *attr, const char *buf, size_t len)
1155 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1156 struct tsl2X7X_chip *chip = iio_priv(dev_get_drvdata(dev));
1157 struct tsl2x7x_parse_result result;
1158 int y, z, filter_delay;
1162 tsl2x7x_parse_buffer(buf, &result);
1164 result.fract /= 1000;
1165 y = (TSL2X7X_MAX_TIMER_CNT - (u8)chip->tsl2x7x_settings.prx_time) + 1;
1166 z = y * TSL2X7X_MIN_ITIME;
1169 DIV_ROUND_UP(((result.integer * 1000) + result.fract), z);
1171 chip->tsl2x7x_settings.persistence &= 0x0F;
1172 chip->tsl2x7x_settings.persistence |= ((filter_delay << 4) & 0xF0);
1174 dev_info(&chip->client->dev, "%s: prox persistence = %d",
1175 __func__, filter_delay);
1177 tsl2x7x_invoke_change(indio_dev);
1179 return IIO_VAL_INT_PLUS_MICRO;
1182 static ssize_t tsl2x7x_do_calibrate(struct device *dev,
1183 struct device_attribute *attr, const char *buf, size_t len)
1185 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1188 if (strtobool(buf, &value))
1192 tsl2x7x_als_calibrate(indio_dev);
1194 tsl2x7x_invoke_change(indio_dev);
1199 static ssize_t tsl2x7x_luxtable_show(struct device *dev,
1200 struct device_attribute *attr, char *buf)
1202 struct tsl2X7X_chip *chip = iio_priv(dev_get_drvdata(dev));
1206 while (i < (TSL2X7X_MAX_LUX_TABLE_SIZE * 3)) {
1207 offset += snprintf(buf + offset, PAGE_SIZE, "%d,%d,%d,",
1208 chip->tsl2x7x_device_lux[i].ratio,
1209 chip->tsl2x7x_device_lux[i].ch0,
1210 chip->tsl2x7x_device_lux[i].ch1);
1211 if (chip->tsl2x7x_device_lux[i].ratio == 0) {
1212 /* We just printed the first "0" entry.
1213 * Now get rid of the extra "," and break. */
1220 offset += snprintf(buf + offset, PAGE_SIZE, "\n");
1224 static ssize_t tsl2x7x_luxtable_store(struct device *dev,
1225 struct device_attribute *attr, const char *buf, size_t len)
1227 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1228 struct tsl2X7X_chip *chip = iio_priv(dev_get_drvdata(dev));
1229 int value[ARRAY_SIZE(chip->tsl2x7x_device_lux)*3 + 1];
1232 get_options(buf, ARRAY_SIZE(value), value);
1234 /* We now have an array of ints starting at value[1], and
1235 * enumerated by value[0].
1236 * We expect each group of three ints is one table entry,
1237 * and the last table entry is all 0.
1240 if ((n % 3) || n < 6 ||
1241 n > ((ARRAY_SIZE(chip->tsl2x7x_device_lux) - 1) * 3)) {
1242 dev_info(dev, "LUX TABLE INPUT ERROR 1 Value[0]=%d\n", n);
1246 if ((value[(n - 2)] | value[(n - 1)] | value[n]) != 0) {
1247 dev_info(dev, "LUX TABLE INPUT ERROR 2 Value[0]=%d\n", n);
1251 if (chip->tsl2x7x_chip_status == TSL2X7X_CHIP_WORKING)
1252 tsl2x7x_chip_off(indio_dev);
1254 /* Zero out the table */
1255 memset(chip->tsl2x7x_device_lux, 0, sizeof(chip->tsl2x7x_device_lux));
1256 memcpy(chip->tsl2x7x_device_lux, &value[1], (value[0] * 4));
1258 tsl2x7x_invoke_change(indio_dev);
1263 static ssize_t tsl2x7x_do_prox_calibrate(struct device *dev,
1264 struct device_attribute *attr, const char *buf, size_t len)
1266 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1269 if (strtobool(buf, &value))
1273 tsl2x7x_prox_cal(indio_dev);
1275 tsl2x7x_invoke_change(indio_dev);
1280 static int tsl2x7x_read_interrupt_config(struct iio_dev *indio_dev,
1283 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1286 if (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code) == IIO_INTENSITY)
1287 ret = !!(chip->tsl2x7x_settings.interrupts_en & 0x10);
1289 ret = !!(chip->tsl2x7x_settings.interrupts_en & 0x20);
1294 static int tsl2x7x_write_interrupt_config(struct iio_dev *indio_dev,
1298 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1300 if (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code) == IIO_INTENSITY) {
1302 chip->tsl2x7x_settings.interrupts_en |= 0x10;
1304 chip->tsl2x7x_settings.interrupts_en &= 0x20;
1307 chip->tsl2x7x_settings.interrupts_en |= 0x20;
1309 chip->tsl2x7x_settings.interrupts_en &= 0x10;
1312 tsl2x7x_invoke_change(indio_dev);
1317 static int tsl2x7x_write_thresh(struct iio_dev *indio_dev,
1321 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1323 if (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code) == IIO_INTENSITY) {
1324 switch (IIO_EVENT_CODE_EXTRACT_DIR(event_code)) {
1325 case IIO_EV_DIR_RISING:
1326 chip->tsl2x7x_settings.als_thresh_high = val;
1328 case IIO_EV_DIR_FALLING:
1329 chip->tsl2x7x_settings.als_thresh_low = val;
1335 switch (IIO_EVENT_CODE_EXTRACT_DIR(event_code)) {
1336 case IIO_EV_DIR_RISING:
1337 chip->tsl2x7x_settings.prox_thres_high = val;
1339 case IIO_EV_DIR_FALLING:
1340 chip->tsl2x7x_settings.prox_thres_low = val;
1347 tsl2x7x_invoke_change(indio_dev);
1352 static int tsl2x7x_read_thresh(struct iio_dev *indio_dev,
1356 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1358 if (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code) == IIO_INTENSITY) {
1359 switch (IIO_EVENT_CODE_EXTRACT_DIR(event_code)) {
1360 case IIO_EV_DIR_RISING:
1361 *val = chip->tsl2x7x_settings.als_thresh_high;
1363 case IIO_EV_DIR_FALLING:
1364 *val = chip->tsl2x7x_settings.als_thresh_low;
1370 switch (IIO_EVENT_CODE_EXTRACT_DIR(event_code)) {
1371 case IIO_EV_DIR_RISING:
1372 *val = chip->tsl2x7x_settings.prox_thres_high;
1374 case IIO_EV_DIR_FALLING:
1375 *val = chip->tsl2x7x_settings.prox_thres_low;
1385 static int tsl2x7x_read_raw(struct iio_dev *indio_dev,
1386 struct iio_chan_spec const *chan,
1392 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1395 case IIO_CHAN_INFO_PROCESSED:
1396 switch (chan->type) {
1398 tsl2x7x_get_lux(indio_dev);
1399 *val = chip->als_cur_info.lux;
1407 case IIO_CHAN_INFO_RAW:
1408 switch (chan->type) {
1410 tsl2x7x_get_lux(indio_dev);
1411 if (chan->channel == 0)
1412 *val = chip->als_cur_info.als_ch0;
1414 *val = chip->als_cur_info.als_ch1;
1418 tsl2x7x_get_prox(indio_dev);
1419 *val = chip->prox_data;
1427 case IIO_CHAN_INFO_CALIBSCALE:
1428 if (chan->type == IIO_LIGHT)
1430 tsl2X7X_als_gainadj[chip->tsl2x7x_settings.als_gain];
1433 tsl2X7X_prx_gainadj[chip->tsl2x7x_settings.prox_gain];
1436 case IIO_CHAN_INFO_CALIBBIAS:
1437 *val = chip->tsl2x7x_settings.als_gain_trim;
1448 static int tsl2x7x_write_raw(struct iio_dev *indio_dev,
1449 struct iio_chan_spec const *chan,
1454 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1457 case IIO_CHAN_INFO_CALIBSCALE:
1458 if (chan->type == IIO_INTENSITY) {
1461 chip->tsl2x7x_settings.als_gain = 0;
1464 chip->tsl2x7x_settings.als_gain = 1;
1467 chip->tsl2x7x_settings.als_gain = 2;
1479 chip->tsl2x7x_settings.als_gain = 3;
1491 chip->tsl2x7x_settings.als_gain = 3;
1499 chip->tsl2x7x_settings.prox_gain = 0;
1502 chip->tsl2x7x_settings.prox_gain = 1;
1505 chip->tsl2x7x_settings.prox_gain = 2;
1508 chip->tsl2x7x_settings.prox_gain = 3;
1515 case IIO_CHAN_INFO_CALIBBIAS:
1516 chip->tsl2x7x_settings.als_gain_trim = val;
1523 tsl2x7x_invoke_change(indio_dev);
1528 static DEVICE_ATTR(power_state, S_IRUGO | S_IWUSR,
1529 tsl2x7x_power_state_show, tsl2x7x_power_state_store);
1531 static DEVICE_ATTR(in_proximity0_calibscale_available, S_IRUGO,
1532 tsl2x7x_prox_gain_available_show, NULL);
1534 static DEVICE_ATTR(in_illuminance0_calibscale_available, S_IRUGO,
1535 tsl2x7x_gain_available_show, NULL);
1537 static DEVICE_ATTR(in_illuminance0_integration_time, S_IRUGO | S_IWUSR,
1538 tsl2x7x_als_time_show, tsl2x7x_als_time_store);
1540 static DEVICE_ATTR(in_illuminance0_target_input, S_IRUGO | S_IWUSR,
1541 tsl2x7x_als_cal_target_show, tsl2x7x_als_cal_target_store);
1543 static DEVICE_ATTR(in_illuminance0_calibrate, S_IWUSR, NULL,
1544 tsl2x7x_do_calibrate);
1546 static DEVICE_ATTR(in_proximity0_calibrate, S_IWUSR, NULL,
1547 tsl2x7x_do_prox_calibrate);
1549 static DEVICE_ATTR(in_illuminance0_lux_table, S_IRUGO | S_IWUSR,
1550 tsl2x7x_luxtable_show, tsl2x7x_luxtable_store);
1552 static DEVICE_ATTR(in_intensity0_thresh_period, S_IRUGO | S_IWUSR,
1553 tsl2x7x_als_persistence_show, tsl2x7x_als_persistence_store);
1555 static DEVICE_ATTR(in_proximity0_thresh_period, S_IRUGO | S_IWUSR,
1556 tsl2x7x_prox_persistence_show, tsl2x7x_prox_persistence_store);
1558 /* Use the default register values to identify the Taos device */
1559 static int tsl2x7x_device_id(unsigned char *id, int target)
1565 return ((*id & 0xf0) == TRITON_ID);
1569 return ((*id & 0xf0) == HALIBUT_ID);
1576 return ((*id & 0xf0) == SWORDFISH_ID);
1583 static irqreturn_t tsl2x7x_event_handler(int irq, void *private)
1585 struct iio_dev *indio_dev = private;
1586 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1587 s64 timestamp = iio_get_time_ns();
1591 value = i2c_smbus_read_byte_data(chip->client,
1592 TSL2X7X_CMD_REG | TSL2X7X_STATUS);
1594 /* What type of interrupt do we need to process */
1595 if (value & TSL2X7X_STA_PRX_INTR) {
1596 tsl2x7x_get_prox(indio_dev); /* freshen data for ABI */
1597 iio_push_event(indio_dev,
1598 IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY,
1605 if (value & TSL2X7X_STA_ALS_INTR) {
1606 tsl2x7x_get_lux(indio_dev); /* freshen data for ABI */
1607 iio_push_event(indio_dev,
1608 IIO_UNMOD_EVENT_CODE(IIO_LIGHT,
1614 /* Clear interrupt now that we have handled it. */
1615 ret = i2c_smbus_write_byte(chip->client,
1616 TSL2X7X_CMD_REG | TSL2X7X_CMD_SPL_FN |
1617 TSL2X7X_CMD_PROXALS_INT_CLR);
1619 dev_err(&chip->client->dev,
1620 "%s: Failed to clear irq from event handler. err = %d\n",
1626 static struct attribute *tsl2x7x_ALS_device_attrs[] = {
1627 &dev_attr_power_state.attr,
1628 &dev_attr_in_illuminance0_calibscale_available.attr,
1629 &dev_attr_in_illuminance0_integration_time.attr,
1630 &iio_const_attr_in_illuminance0_integration_time_available\
1632 &dev_attr_in_illuminance0_target_input.attr,
1633 &dev_attr_in_illuminance0_calibrate.attr,
1634 &dev_attr_in_illuminance0_lux_table.attr,
1638 static struct attribute *tsl2x7x_PRX_device_attrs[] = {
1639 &dev_attr_power_state.attr,
1640 &dev_attr_in_proximity0_calibrate.attr,
1644 static struct attribute *tsl2x7x_ALSPRX_device_attrs[] = {
1645 &dev_attr_power_state.attr,
1646 &dev_attr_in_illuminance0_calibscale_available.attr,
1647 &dev_attr_in_illuminance0_integration_time.attr,
1648 &iio_const_attr_in_illuminance0_integration_time_available\
1650 &dev_attr_in_illuminance0_target_input.attr,
1651 &dev_attr_in_illuminance0_calibrate.attr,
1652 &dev_attr_in_illuminance0_lux_table.attr,
1653 &dev_attr_in_proximity0_calibrate.attr,
1657 static struct attribute *tsl2x7x_PRX2_device_attrs[] = {
1658 &dev_attr_power_state.attr,
1659 &dev_attr_in_proximity0_calibrate.attr,
1660 &dev_attr_in_proximity0_calibscale_available.attr,
1664 static struct attribute *tsl2x7x_ALSPRX2_device_attrs[] = {
1665 &dev_attr_power_state.attr,
1666 &dev_attr_in_illuminance0_calibscale_available.attr,
1667 &dev_attr_in_illuminance0_integration_time.attr,
1668 &iio_const_attr_in_illuminance0_integration_time_available\
1670 &dev_attr_in_illuminance0_target_input.attr,
1671 &dev_attr_in_illuminance0_calibrate.attr,
1672 &dev_attr_in_illuminance0_lux_table.attr,
1673 &dev_attr_in_proximity0_calibrate.attr,
1674 &dev_attr_in_proximity0_calibscale_available.attr,
1678 static struct attribute *tsl2X7X_ALS_event_attrs[] = {
1679 &dev_attr_in_intensity0_thresh_period.attr,
1682 static struct attribute *tsl2X7X_PRX_event_attrs[] = {
1683 &dev_attr_in_proximity0_thresh_period.attr,
1687 static struct attribute *tsl2X7X_ALSPRX_event_attrs[] = {
1688 &dev_attr_in_intensity0_thresh_period.attr,
1689 &dev_attr_in_proximity0_thresh_period.attr,
1693 static const struct attribute_group tsl2X7X_device_attr_group_tbl[] = {
1695 .attrs = tsl2x7x_ALS_device_attrs,
1698 .attrs = tsl2x7x_PRX_device_attrs,
1701 .attrs = tsl2x7x_ALSPRX_device_attrs,
1704 .attrs = tsl2x7x_PRX2_device_attrs,
1707 .attrs = tsl2x7x_ALSPRX2_device_attrs,
1711 static struct attribute_group tsl2X7X_event_attr_group_tbl[] = {
1713 .attrs = tsl2X7X_ALS_event_attrs,
1717 .attrs = tsl2X7X_PRX_event_attrs,
1721 .attrs = tsl2X7X_ALSPRX_event_attrs,
1726 static const struct iio_info tsl2X7X_device_info[] = {
1728 .attrs = &tsl2X7X_device_attr_group_tbl[ALS],
1729 .event_attrs = &tsl2X7X_event_attr_group_tbl[ALS],
1730 .driver_module = THIS_MODULE,
1731 .read_raw = &tsl2x7x_read_raw,
1732 .write_raw = &tsl2x7x_write_raw,
1733 .read_event_value = &tsl2x7x_read_thresh,
1734 .write_event_value = &tsl2x7x_write_thresh,
1735 .read_event_config = &tsl2x7x_read_interrupt_config,
1736 .write_event_config = &tsl2x7x_write_interrupt_config,
1739 .attrs = &tsl2X7X_device_attr_group_tbl[PRX],
1740 .event_attrs = &tsl2X7X_event_attr_group_tbl[PRX],
1741 .driver_module = THIS_MODULE,
1742 .read_raw = &tsl2x7x_read_raw,
1743 .write_raw = &tsl2x7x_write_raw,
1744 .read_event_value = &tsl2x7x_read_thresh,
1745 .write_event_value = &tsl2x7x_write_thresh,
1746 .read_event_config = &tsl2x7x_read_interrupt_config,
1747 .write_event_config = &tsl2x7x_write_interrupt_config,
1750 .attrs = &tsl2X7X_device_attr_group_tbl[ALSPRX],
1751 .event_attrs = &tsl2X7X_event_attr_group_tbl[ALSPRX],
1752 .driver_module = THIS_MODULE,
1753 .read_raw = &tsl2x7x_read_raw,
1754 .write_raw = &tsl2x7x_write_raw,
1755 .read_event_value = &tsl2x7x_read_thresh,
1756 .write_event_value = &tsl2x7x_write_thresh,
1757 .read_event_config = &tsl2x7x_read_interrupt_config,
1758 .write_event_config = &tsl2x7x_write_interrupt_config,
1761 .attrs = &tsl2X7X_device_attr_group_tbl[PRX2],
1762 .event_attrs = &tsl2X7X_event_attr_group_tbl[PRX],
1763 .driver_module = THIS_MODULE,
1764 .read_raw = &tsl2x7x_read_raw,
1765 .write_raw = &tsl2x7x_write_raw,
1766 .read_event_value = &tsl2x7x_read_thresh,
1767 .write_event_value = &tsl2x7x_write_thresh,
1768 .read_event_config = &tsl2x7x_read_interrupt_config,
1769 .write_event_config = &tsl2x7x_write_interrupt_config,
1772 .attrs = &tsl2X7X_device_attr_group_tbl[ALSPRX2],
1773 .event_attrs = &tsl2X7X_event_attr_group_tbl[ALSPRX],
1774 .driver_module = THIS_MODULE,
1775 .read_raw = &tsl2x7x_read_raw,
1776 .write_raw = &tsl2x7x_write_raw,
1777 .read_event_value = &tsl2x7x_read_thresh,
1778 .write_event_value = &tsl2x7x_write_thresh,
1779 .read_event_config = &tsl2x7x_read_interrupt_config,
1780 .write_event_config = &tsl2x7x_write_interrupt_config,
1784 static const struct tsl2x7x_chip_info tsl2x7x_chip_info_tbl[] = {
1791 .info_mask = IIO_CHAN_INFO_PROCESSED_SEPARATE_BIT,
1793 .type = IIO_INTENSITY,
1796 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
1797 IIO_CHAN_INFO_CALIBSCALE_SEPARATE_BIT |
1798 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT,
1799 .event_mask = TSL2X7X_EVENT_MASK
1801 .type = IIO_INTENSITY,
1806 .chan_table_elements = 3,
1807 .info = &tsl2X7X_device_info[ALS],
1812 .type = IIO_PROXIMITY,
1815 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
1816 .event_mask = TSL2X7X_EVENT_MASK
1819 .chan_table_elements = 1,
1820 .info = &tsl2X7X_device_info[PRX],
1828 .info_mask = IIO_CHAN_INFO_PROCESSED_SEPARATE_BIT
1830 .type = IIO_INTENSITY,
1833 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
1834 IIO_CHAN_INFO_CALIBSCALE_SEPARATE_BIT |
1835 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT,
1836 .event_mask = TSL2X7X_EVENT_MASK
1838 .type = IIO_INTENSITY,
1841 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
1843 .type = IIO_PROXIMITY,
1846 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
1847 .event_mask = TSL2X7X_EVENT_MASK
1850 .chan_table_elements = 4,
1851 .info = &tsl2X7X_device_info[ALSPRX],
1856 .type = IIO_PROXIMITY,
1859 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
1860 IIO_CHAN_INFO_CALIBSCALE_SEPARATE_BIT,
1861 .event_mask = TSL2X7X_EVENT_MASK
1864 .chan_table_elements = 1,
1865 .info = &tsl2X7X_device_info[PRX2],
1873 .info_mask = IIO_CHAN_INFO_PROCESSED_SEPARATE_BIT,
1875 .type = IIO_INTENSITY,
1878 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
1879 IIO_CHAN_INFO_CALIBSCALE_SEPARATE_BIT |
1880 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT,
1881 .event_mask = TSL2X7X_EVENT_MASK
1883 .type = IIO_INTENSITY,
1886 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
1888 .type = IIO_PROXIMITY,
1891 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
1892 IIO_CHAN_INFO_CALIBSCALE_SEPARATE_BIT,
1893 .event_mask = TSL2X7X_EVENT_MASK
1896 .chan_table_elements = 4,
1897 .info = &tsl2X7X_device_info[ALSPRX2],
1901 static int __devinit tsl2x7x_probe(struct i2c_client *clientp,
1902 const struct i2c_device_id *id)
1905 unsigned char device_id;
1906 struct iio_dev *indio_dev;
1907 struct tsl2X7X_chip *chip;
1909 indio_dev = iio_device_alloc(sizeof(*chip));
1913 chip = iio_priv(indio_dev);
1914 chip->client = clientp;
1915 i2c_set_clientdata(clientp, indio_dev);
1917 ret = tsl2x7x_i2c_read(chip->client,
1918 TSL2X7X_CHIPID, &device_id);
1922 if ((!tsl2x7x_device_id(&device_id, id->driver_data)) ||
1923 (tsl2x7x_device_id(&device_id, id->driver_data) == -EINVAL)) {
1924 dev_info(&chip->client->dev,
1925 "%s: i2c device found does not match expected id\n",
1930 ret = i2c_smbus_write_byte(clientp, (TSL2X7X_CMD_REG | TSL2X7X_CNTRL));
1932 dev_err(&clientp->dev, "%s: write to cmd reg failed. err = %d\n",
1937 /* ALS and PROX functions can be invoked via user space poll
1938 * or H/W interrupt. If busy return last sample. */
1939 mutex_init(&chip->als_mutex);
1940 mutex_init(&chip->prox_mutex);
1942 chip->tsl2x7x_chip_status = TSL2X7X_CHIP_UNKNOWN;
1943 chip->pdata = clientp->dev.platform_data;
1944 chip->id = id->driver_data;
1946 &tsl2x7x_chip_info_tbl[device_channel_config[id->driver_data]];
1948 indio_dev->info = chip->chip_info->info;
1949 indio_dev->dev.parent = &clientp->dev;
1950 indio_dev->modes = INDIO_DIRECT_MODE;
1951 indio_dev->name = chip->client->name;
1952 indio_dev->channels = chip->chip_info->channel;
1953 indio_dev->num_channels = chip->chip_info->chan_table_elements;
1956 ret = request_threaded_irq(clientp->irq,
1958 &tsl2x7x_event_handler,
1959 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
1963 dev_err(&clientp->dev,
1964 "%s: irq request failed", __func__);
1969 /* Load up the defaults */
1970 tsl2x7x_defaults(chip);
1971 /* Make sure the chip is on */
1972 tsl2x7x_chip_on(indio_dev);
1974 ret = iio_device_register(indio_dev);
1976 dev_err(&clientp->dev,
1977 "%s: iio registration failed\n", __func__);
1981 dev_info(&clientp->dev, "%s Light sensor found.\n", id->name);
1987 free_irq(clientp->irq, indio_dev);
1989 iio_device_free(indio_dev);
1994 static int tsl2x7x_suspend(struct device *dev)
1996 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1997 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
2000 if (chip->tsl2x7x_chip_status == TSL2X7X_CHIP_WORKING) {
2001 ret = tsl2x7x_chip_off(indio_dev);
2002 chip->tsl2x7x_chip_status = TSL2X7X_CHIP_SUSPENDED;
2005 if (chip->pdata && chip->pdata->platform_power) {
2006 pm_message_t pmm = {PM_EVENT_SUSPEND};
2007 chip->pdata->platform_power(dev, pmm);
2013 static int tsl2x7x_resume(struct device *dev)
2015 struct iio_dev *indio_dev = dev_get_drvdata(dev);
2016 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
2019 if (chip->pdata && chip->pdata->platform_power) {
2020 pm_message_t pmm = {PM_EVENT_RESUME};
2021 chip->pdata->platform_power(dev, pmm);
2024 if (chip->tsl2x7x_chip_status == TSL2X7X_CHIP_SUSPENDED)
2025 ret = tsl2x7x_chip_on(indio_dev);
2030 static int __devexit tsl2x7x_remove(struct i2c_client *client)
2032 struct tsl2X7X_chip *chip = i2c_get_clientdata(client);
2033 struct iio_dev *indio_dev = iio_priv_to_dev(chip);
2035 tsl2x7x_chip_off(indio_dev);
2037 iio_device_unregister(indio_dev);
2039 free_irq(client->irq, chip->client->name);
2041 iio_device_free(indio_dev);
2046 static struct i2c_device_id tsl2x7x_idtable[] = {
2047 { "tsl2571", tsl2571 },
2048 { "tsl2671", tsl2671 },
2049 { "tmd2671", tmd2671 },
2050 { "tsl2771", tsl2771 },
2051 { "tmd2771", tmd2771 },
2052 { "tsl2572", tsl2572 },
2053 { "tsl2672", tsl2672 },
2054 { "tmd2672", tmd2672 },
2055 { "tsl2772", tsl2772 },
2056 { "tmd2772", tmd2772 },
2060 MODULE_DEVICE_TABLE(i2c, tsl2x7x_idtable);
2062 static const struct dev_pm_ops tsl2x7x_pm_ops = {
2063 .suspend = tsl2x7x_suspend,
2064 .resume = tsl2x7x_resume,
2067 /* Driver definition */
2068 static struct i2c_driver tsl2x7x_driver = {
2071 .pm = &tsl2x7x_pm_ops,
2073 .id_table = tsl2x7x_idtable,
2074 .probe = tsl2x7x_probe,
2075 .remove = __devexit_p(tsl2x7x_remove),
2078 module_i2c_driver(tsl2x7x_driver);
2080 MODULE_AUTHOR("J. August Brenner<jbrenner@taosinc.com>");
2081 MODULE_DESCRIPTION("TAOS tsl2x7x ambient and proximity light sensor driver");
2082 MODULE_LICENSE("GPL");