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>
32 #include "../events.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 /* TAOS txx2x7x Device family members */
148 TSL2X7X_CHIP_UNKNOWN = 0,
149 TSL2X7X_CHIP_WORKING = 1,
150 TSL2X7X_CHIP_SUSPENDED = 2
153 struct tsl2x7x_parse_result {
158 /* Per-device data */
159 struct tsl2x7x_als_info {
165 struct tsl2x7x_prox_stat {
169 unsigned long stddev;
172 struct tsl2x7x_chip_info {
173 int chan_table_elements;
174 struct iio_chan_spec channel[4];
175 const struct iio_info *info;
178 struct tsl2X7X_chip {
180 struct mutex prox_mutex;
181 struct mutex als_mutex;
182 struct i2c_client *client;
184 struct tsl2x7x_als_info als_cur_info;
185 struct tsl2x7x_settings tsl2x7x_settings;
186 struct tsl2X7X_platform_data *pdata;
189 int tsl2x7x_chip_status;
190 u8 tsl2x7x_config[TSL2X7X_MAX_CONFIG_REG];
191 const struct tsl2x7x_chip_info *chip_info;
192 const struct iio_info *info;
194 /* This structure is intentionally large to accommodate
195 * updates via sysfs. */
196 /* Sized to 9 = max 8 segments + 1 termination segment */
197 struct tsl2x7x_lux tsl2x7x_device_lux[TSL2X7X_MAX_LUX_TABLE_SIZE];
200 /* Different devices require different coefficents */
201 static const struct tsl2x7x_lux tsl2x71_lux_table[] = {
202 { 14461, 611, 1211 },
207 static const struct tsl2x7x_lux tmd2x71_lux_table[] = {
213 static const struct tsl2x7x_lux tsl2x72_lux_table[] = {
219 static const struct tsl2x7x_lux tmd2x72_lux_table[] = {
225 static const struct tsl2x7x_lux *tsl2x7x_default_lux_table_group[] = {
226 [tsl2571] = tsl2x71_lux_table,
227 [tsl2671] = tsl2x71_lux_table,
228 [tmd2671] = tmd2x71_lux_table,
229 [tsl2771] = tsl2x71_lux_table,
230 [tmd2771] = tmd2x71_lux_table,
231 [tsl2572] = tsl2x72_lux_table,
232 [tsl2672] = tsl2x72_lux_table,
233 [tmd2672] = tmd2x72_lux_table,
234 [tsl2772] = tsl2x72_lux_table,
235 [tmd2772] = tmd2x72_lux_table,
238 static const struct tsl2x7x_settings tsl2x7x_default_settings = {
239 .als_time = 219, /* 101 ms */
241 .prx_time = 254, /* 5.4 ms */
245 .als_gain_trim = 1000,
246 .als_cal_target = 150,
247 .als_thresh_low = 200,
248 .als_thresh_high = 256,
252 .prox_thres_high = 512,
253 .prox_max_samples_cal = 30,
254 .prox_pulse_count = 8
257 static const s16 tsl2X7X_als_gainadj[] = {
264 static const s16 tsl2X7X_prx_gainadj[] = {
271 /* Channel variations */
280 const u8 device_channel_config[] = {
294 * tsl2x7x_parse_buffer() - parse a decimal result from a buffer.
295 * @*buf: pointer to char buffer to parse
296 * @*result: pointer to buffer to contain
297 * resulting interger / decimal as ints.
301 tsl2x7x_parse_buffer(const char *buf, struct tsl2x7x_parse_result *result)
303 int integer = 0, fract = 0, fract_mult = 100000;
304 bool integer_part = true, negative = false;
312 if ('0' <= *buf && *buf <= '9') {
314 integer = integer*10 + *buf - '0';
316 fract += fract_mult*(*buf - '0');
321 } else if (*buf == '\n') {
322 if (*(buf + 1) == '\0')
326 } else if (*buf == '.') {
327 integer_part = false;
340 result->integer = integer;
341 result->fract = fract;
347 * tsl2x7x_i2c_read() - Read a byte from a register.
348 * @client: i2c client
349 * @reg: device register to read from
350 * @*val: pointer to location to store register contents.
354 tsl2x7x_i2c_read(struct i2c_client *client, u8 reg, u8 *val)
358 /* select register to write */
359 ret = i2c_smbus_write_byte(client, (TSL2X7X_CMD_REG | reg));
361 dev_err(&client->dev, "%s: failed to write register %x\n"
367 ret = i2c_smbus_read_byte(client);
371 dev_err(&client->dev, "%s: failed to read register %x\n"
378 * tsl2x7x_get_lux() - Reads and calculates current lux value.
379 * @indio_dev: pointer to IIO device
381 * The raw ch0 and ch1 values of the ambient light sensed in the last
382 * integration cycle are read from the device.
383 * Time scale factor array values are adjusted based on the integration time.
384 * The raw values are multiplied by a scale factor, and device gain is obtained
385 * using gain index. Limit checks are done next, then the ratio of a multiple
386 * of ch1 value, to the ch0 value, is calculated. Array tsl2x7x_device_lux[]
387 * is then scanned to find the first ratio value that is just above the ratio
388 * we just calculated. The ch0 and ch1 multiplier constants in the array are
389 * then used along with the time scale factor array values, to calculate the
392 static int tsl2x7x_get_lux(struct iio_dev *indio_dev)
394 u16 ch0, ch1; /* separated ch0/ch1 data from device */
395 u32 lux; /* raw lux calculated from device data */
399 struct tsl2x7x_lux *p;
400 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
405 if (mutex_trylock(&chip->als_mutex) == 0)
406 return chip->als_cur_info.lux; /* busy, so return LAST VALUE */
408 if (chip->tsl2x7x_chip_status != TSL2X7X_CHIP_WORKING) {
409 /* device is not enabled */
410 dev_err(&chip->client->dev, "%s: device is not enabled\n",
416 ret = tsl2x7x_i2c_read(chip->client,
417 (TSL2X7X_CMD_REG | TSL2X7X_STATUS), &buf[0]);
419 dev_err(&chip->client->dev,
420 "%s: Failed to read STATUS Reg\n", __func__);
423 /* is data new & valid */
424 if (!(buf[0] & TSL2X7X_STA_ADC_VALID)) {
425 dev_err(&chip->client->dev,
426 "%s: data not valid yet\n", __func__);
427 ret = chip->als_cur_info.lux; /* return LAST VALUE */
431 for (i = 0; i < 4; i++) {
432 ret = tsl2x7x_i2c_read(chip->client,
433 (TSL2X7X_CMD_REG | (TSL2X7X_ALS_CHAN0LO + i)),
436 dev_err(&chip->client->dev,
437 "%s: failed to read. err=%x\n", __func__, ret);
442 /* clear any existing interrupt status */
443 ret = i2c_smbus_write_byte(chip->client,
446 TSL2X7X_CMD_ALS_INT_CLR));
448 dev_err(&chip->client->dev,
449 "%s: i2c_write_command failed - err = %d\n",
451 goto out_unlock; /* have no data, so return failure */
454 /* extract ALS/lux data */
455 ch0 = le16_to_cpup((const __le16 *)&buf[0]);
456 ch1 = le16_to_cpup((const __le16 *)&buf[2]);
458 chip->als_cur_info.als_ch0 = ch0;
459 chip->als_cur_info.als_ch1 = ch1;
461 if ((ch0 >= chip->als_saturation) || (ch1 >= chip->als_saturation)) {
462 lux = TSL2X7X_LUX_CALC_OVER_FLOW;
467 /* have no data, so return LAST VALUE */
468 ret = chip->als_cur_info.lux;
471 /* calculate ratio */
472 ratio = (ch1 << 15) / ch0;
473 /* convert to unscaled lux using the pointer to the table */
474 p = (struct tsl2x7x_lux *) chip->tsl2x7x_device_lux;
475 while (p->ratio != 0 && p->ratio < ratio)
481 ch0lux = DIV_ROUND_UP((ch0 * p->ch0),
482 tsl2X7X_als_gainadj[chip->tsl2x7x_settings.als_gain]);
483 ch1lux = DIV_ROUND_UP((ch1 * p->ch1),
484 tsl2X7X_als_gainadj[chip->tsl2x7x_settings.als_gain]);
485 lux = ch0lux - ch1lux;
488 /* note: lux is 31 bit max at this point */
489 if (ch1lux > ch0lux) {
490 dev_dbg(&chip->client->dev, "ch1lux > ch0lux-return last value\n");
491 ret = chip->als_cur_info.lux;
495 /* adjust for active time scale */
496 if (chip->als_time_scale == 0)
499 lux = (lux + (chip->als_time_scale >> 1)) /
500 chip->als_time_scale;
502 /* adjust for active gain scale
503 * The tsl2x7x_device_lux tables have a factor of 256 built-in.
504 * User-specified gain provides a multiplier.
505 * Apply user-specified gain before shifting right to retain precision.
506 * Use 64 bits to avoid overflow on multiplication.
507 * Then go back to 32 bits before division to avoid using div_u64().
511 lux64 = lux64 * chip->tsl2x7x_settings.als_gain_trim;
514 lux = (lux + 500) / 1000;
516 if (lux > TSL2X7X_LUX_CALC_OVER_FLOW) /* check for overflow */
517 lux = TSL2X7X_LUX_CALC_OVER_FLOW;
519 /* Update the structure with the latest lux. */
521 chip->als_cur_info.lux = lux;
525 mutex_unlock(&chip->als_mutex);
531 * tsl2x7x_get_prox() - Reads proximity data registers and updates
534 * @indio_dev: pointer to IIO device
536 static int tsl2x7x_get_prox(struct iio_dev *indio_dev)
542 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
544 if (mutex_trylock(&chip->prox_mutex) == 0) {
545 dev_err(&chip->client->dev,
546 "%s: Can't get prox mutex\n", __func__);
550 ret = tsl2x7x_i2c_read(chip->client,
551 (TSL2X7X_CMD_REG | TSL2X7X_STATUS), &status);
553 dev_err(&chip->client->dev,
554 "%s: i2c err=%d\n", __func__, ret);
564 if (!(status & TSL2X7X_STA_ADC_VALID))
572 if (!(status & TSL2X7X_STA_PRX_VALID))
577 for (i = 0; i < 2; i++) {
578 ret = tsl2x7x_i2c_read(chip->client,
580 (TSL2X7X_PRX_LO + i)), &chdata[i]);
586 le16_to_cpup((const __le16 *)&chdata[0]);
590 mutex_unlock(&chip->prox_mutex);
592 return chip->prox_data;
596 * tsl2x7x_defaults() - Populates the device nominal operating parameters
597 * with those provided by a 'platform' data struct or
598 * with prefined defaults.
600 * @chip: pointer to device structure.
602 static void tsl2x7x_defaults(struct tsl2X7X_chip *chip)
604 /* If Operational settings defined elsewhere.. */
605 if (chip->pdata && chip->pdata->platform_default_settings != 0)
606 memcpy(&(chip->tsl2x7x_settings),
607 chip->pdata->platform_default_settings,
608 sizeof(tsl2x7x_default_settings));
610 memcpy(&(chip->tsl2x7x_settings),
611 &tsl2x7x_default_settings,
612 sizeof(tsl2x7x_default_settings));
614 /* Load up the proper lux table. */
615 if (chip->pdata && chip->pdata->platform_lux_table[0].ratio != 0)
616 memcpy(chip->tsl2x7x_device_lux,
617 chip->pdata->platform_lux_table,
618 sizeof(chip->pdata->platform_lux_table));
620 memcpy(chip->tsl2x7x_device_lux,
621 (struct tsl2x7x_lux *)tsl2x7x_default_lux_table_group[chip->id],
622 MAX_DEFAULT_TABLE_BYTES);
626 * tsl2x7x_als_calibrate() - Obtain single reading and calculate
629 * @indio_dev: pointer to IIO device
631 static int tsl2x7x_als_calibrate(struct iio_dev *indio_dev)
633 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
639 ret = i2c_smbus_write_byte(chip->client,
640 (TSL2X7X_CMD_REG | TSL2X7X_CNTRL));
642 dev_err(&chip->client->dev,
643 "%s: failed to write CNTRL register, ret=%d\n",
648 reg_val = i2c_smbus_read_byte(chip->client);
649 if ((reg_val & (TSL2X7X_CNTL_ADC_ENBL | TSL2X7X_CNTL_PWR_ON))
650 != (TSL2X7X_CNTL_ADC_ENBL | TSL2X7X_CNTL_PWR_ON)) {
651 dev_err(&chip->client->dev,
652 "%s: failed: ADC not enabled\n", __func__);
656 ret = i2c_smbus_write_byte(chip->client,
657 (TSL2X7X_CMD_REG | TSL2X7X_CNTRL));
659 dev_err(&chip->client->dev,
660 "%s: failed to write ctrl reg: ret=%d\n",
665 reg_val = i2c_smbus_read_byte(chip->client);
666 if ((reg_val & TSL2X7X_STA_ADC_VALID) != TSL2X7X_STA_ADC_VALID) {
667 dev_err(&chip->client->dev,
668 "%s: failed: STATUS - ADC not valid.\n", __func__);
672 lux_val = tsl2x7x_get_lux(indio_dev);
674 dev_err(&chip->client->dev,
675 "%s: failed to get lux\n", __func__);
679 gain_trim_val = (((chip->tsl2x7x_settings.als_cal_target)
680 * chip->tsl2x7x_settings.als_gain_trim) / lux_val);
681 if ((gain_trim_val < 250) || (gain_trim_val > 4000))
684 chip->tsl2x7x_settings.als_gain_trim = gain_trim_val;
685 dev_info(&chip->client->dev,
686 "%s als_calibrate completed\n", chip->client->name);
688 return (int) gain_trim_val;
691 static int tsl2x7x_chip_on(struct iio_dev *indio_dev)
699 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
702 if (chip->pdata && chip->pdata->power_on)
703 chip->pdata->power_on(indio_dev);
705 /* Non calculated parameters */
706 chip->tsl2x7x_config[TSL2X7X_PRX_TIME] =
707 chip->tsl2x7x_settings.prx_time;
708 chip->tsl2x7x_config[TSL2X7X_WAIT_TIME] =
709 chip->tsl2x7x_settings.wait_time;
710 chip->tsl2x7x_config[TSL2X7X_PRX_CONFIG] =
711 chip->tsl2x7x_settings.prox_config;
713 chip->tsl2x7x_config[TSL2X7X_ALS_MINTHRESHLO] =
714 (chip->tsl2x7x_settings.als_thresh_low) & 0xFF;
715 chip->tsl2x7x_config[TSL2X7X_ALS_MINTHRESHHI] =
716 (chip->tsl2x7x_settings.als_thresh_low >> 8) & 0xFF;
717 chip->tsl2x7x_config[TSL2X7X_ALS_MAXTHRESHLO] =
718 (chip->tsl2x7x_settings.als_thresh_high) & 0xFF;
719 chip->tsl2x7x_config[TSL2X7X_ALS_MAXTHRESHHI] =
720 (chip->tsl2x7x_settings.als_thresh_high >> 8) & 0xFF;
721 chip->tsl2x7x_config[TSL2X7X_PERSISTENCE] =
722 chip->tsl2x7x_settings.persistence;
724 chip->tsl2x7x_config[TSL2X7X_PRX_COUNT] =
725 chip->tsl2x7x_settings.prox_pulse_count;
726 chip->tsl2x7x_config[TSL2X7X_PRX_MINTHRESHLO] =
727 chip->tsl2x7x_settings.prox_thres_low;
728 chip->tsl2x7x_config[TSL2X7X_PRX_MAXTHRESHLO] =
729 chip->tsl2x7x_settings.prox_thres_high;
731 /* and make sure we're not already on */
732 if (chip->tsl2x7x_chip_status == TSL2X7X_CHIP_WORKING) {
733 /* if forcing a register update - turn off, then on */
734 dev_info(&chip->client->dev, "device is already enabled\n");
738 /* determine als integration regster */
739 als_count = (chip->tsl2x7x_settings.als_time * 100 + 135) / 270;
741 als_count = 1; /* ensure at least one cycle */
743 /* convert back to time (encompasses overrides) */
744 als_time = (als_count * 27 + 5) / 10;
745 chip->tsl2x7x_config[TSL2X7X_ALS_TIME] = 256 - als_count;
747 /* Set the gain based on tsl2x7x_settings struct */
748 chip->tsl2x7x_config[TSL2X7X_GAIN] =
749 (chip->tsl2x7x_settings.als_gain |
750 (TSL2X7X_mA100 | TSL2X7X_DIODE1)
751 | ((chip->tsl2x7x_settings.prox_gain) << 2));
753 /* set chip struct re scaling and saturation */
754 chip->als_saturation = als_count * 922; /* 90% of full scale */
755 chip->als_time_scale = (als_time + 25) / 50;
757 /* TSL2X7X Specific power-on / adc enable sequence
758 * Power on the device 1st. */
759 utmp = TSL2X7X_CNTL_PWR_ON;
760 ret = i2c_smbus_write_byte_data(chip->client,
761 TSL2X7X_CMD_REG | TSL2X7X_CNTRL, utmp);
763 dev_err(&chip->client->dev,
764 "%s: failed on CNTRL reg.\n", __func__);
768 /* Use the following shadow copy for our delay before enabling ADC.
769 * Write all the registers. */
770 for (i = 0, dev_reg = chip->tsl2x7x_config;
771 i < TSL2X7X_MAX_CONFIG_REG; i++) {
772 ret = i2c_smbus_write_byte_data(chip->client,
773 TSL2X7X_CMD_REG + i, *dev_reg++);
775 dev_err(&chip->client->dev,
776 "%s: failed on write to reg %d.\n", __func__, i);
781 udelay(3000); /* Power-on settling time */
783 /* NOW enable the ADC
784 * initialize the desired mode of operation */
785 utmp = TSL2X7X_CNTL_PWR_ON |
786 TSL2X7X_CNTL_ADC_ENBL |
787 TSL2X7X_CNTL_PROX_DET_ENBL;
788 ret = i2c_smbus_write_byte_data(chip->client,
789 TSL2X7X_CMD_REG | TSL2X7X_CNTRL, utmp);
791 dev_err(&chip->client->dev,
792 "%s: failed on 2nd CTRL reg.\n", __func__);
796 chip->tsl2x7x_chip_status = TSL2X7X_CHIP_WORKING;
798 if (chip->tsl2x7x_settings.interrupts_en != 0) {
799 dev_info(&chip->client->dev, "Setting Up Interrupt(s)\n");
801 reg_val = TSL2X7X_CNTL_PWR_ON | TSL2X7X_CNTL_ADC_ENBL;
802 if ((chip->tsl2x7x_settings.interrupts_en == 0x20) ||
803 (chip->tsl2x7x_settings.interrupts_en == 0x30))
804 reg_val |= TSL2X7X_CNTL_PROX_DET_ENBL;
806 reg_val |= chip->tsl2x7x_settings.interrupts_en;
807 ret = i2c_smbus_write_byte_data(chip->client,
808 (TSL2X7X_CMD_REG | TSL2X7X_CNTRL), reg_val);
810 dev_err(&chip->client->dev,
811 "%s: failed in tsl2x7x_IOCTL_INT_SET.\n",
814 /* Clear out any initial interrupts */
815 ret = i2c_smbus_write_byte(chip->client,
816 TSL2X7X_CMD_REG | TSL2X7X_CMD_SPL_FN |
817 TSL2X7X_CMD_PROXALS_INT_CLR);
819 dev_err(&chip->client->dev,
820 "%s: Failed to clear Int status\n",
829 static int tsl2x7x_chip_off(struct iio_dev *indio_dev)
832 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
834 /* turn device off */
835 chip->tsl2x7x_chip_status = TSL2X7X_CHIP_SUSPENDED;
837 ret = i2c_smbus_write_byte_data(chip->client,
838 TSL2X7X_CMD_REG | TSL2X7X_CNTRL, 0x00);
840 if (chip->pdata && chip->pdata->power_off)
841 chip->pdata->power_off(chip->client);
847 * tsl2x7x_invoke_change
848 * @indio_dev: pointer to IIO device
850 * Obtain and lock both ALS and PROX resources,
851 * determine and save device state (On/Off),
852 * cycle device to implement updated parameter,
853 * put device back into proper state, and unlock
856 int tsl2x7x_invoke_change(struct iio_dev *indio_dev)
858 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
859 int device_status = chip->tsl2x7x_chip_status;
861 mutex_lock(&chip->als_mutex);
862 mutex_lock(&chip->prox_mutex);
864 if (device_status == TSL2X7X_CHIP_WORKING)
865 tsl2x7x_chip_off(indio_dev);
867 tsl2x7x_chip_on(indio_dev);
869 if (device_status != TSL2X7X_CHIP_WORKING)
870 tsl2x7x_chip_off(indio_dev);
872 mutex_unlock(&chip->prox_mutex);
873 mutex_unlock(&chip->als_mutex);
879 void tsl2x7x_prox_calculate(int *data, int length,
880 struct tsl2x7x_prox_stat *statP)
890 statP->min = INT_MAX;
891 statP->max = INT_MIN;
892 for (i = 0; i < length; i++) {
893 sample_sum += data[i];
894 statP->min = min(statP->min, data[i]);
895 statP->max = max(statP->max, data[i]);
898 statP->mean = sample_sum / length;
900 for (i = 0; i < length; i++) {
901 tmp = data[i] - statP->mean;
902 sample_sum += tmp * tmp;
904 statP->stddev = int_sqrt((long)sample_sum)/length;
908 * tsl2x7x_prox_cal() - Calculates std. and sets thresholds.
909 * @indio_dev: pointer to IIO device
911 * Calculates a standard deviation based on the samples,
912 * and sets the threshold accordingly.
914 static void tsl2x7x_prox_cal(struct iio_dev *indio_dev)
916 int prox_history[MAX_SAMPLES_CAL + 1];
918 struct tsl2x7x_prox_stat prox_stat_data[2];
919 struct tsl2x7x_prox_stat *calP;
920 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
922 u8 current_state = chip->tsl2x7x_chip_status;
924 if (chip->tsl2x7x_settings.prox_max_samples_cal > MAX_SAMPLES_CAL) {
925 dev_err(&chip->client->dev,
926 "%s: max prox samples cal is too big: %d\n",
927 __func__, chip->tsl2x7x_settings.prox_max_samples_cal);
928 chip->tsl2x7x_settings.prox_max_samples_cal = MAX_SAMPLES_CAL;
931 /* have to stop to change settings */
932 tsl2x7x_chip_off(indio_dev);
934 /* Enable proximity detection save just in case prox not wanted yet*/
935 tmp_irq_settings = chip->tsl2x7x_settings.interrupts_en;
936 chip->tsl2x7x_settings.interrupts_en |= TSL2X7X_CNTL_PROX_INT_ENBL;
938 /*turn on device if not already on*/
939 tsl2x7x_chip_on(indio_dev);
941 /*gather the samples*/
942 for (i = 0; i < chip->tsl2x7x_settings.prox_max_samples_cal; i++) {
944 tsl2x7x_get_prox(indio_dev);
945 prox_history[i] = chip->prox_data;
946 dev_info(&chip->client->dev, "2 i=%d prox data= %d\n",
950 tsl2x7x_chip_off(indio_dev);
951 calP = &prox_stat_data[PROX_STAT_CAL];
952 tsl2x7x_prox_calculate(prox_history,
953 chip->tsl2x7x_settings.prox_max_samples_cal, calP);
954 chip->tsl2x7x_settings.prox_thres_high = (calP->max << 1) - calP->mean;
956 dev_info(&chip->client->dev, " cal min=%d mean=%d max=%d\n",
957 calP->min, calP->mean, calP->max);
958 dev_info(&chip->client->dev,
959 "%s proximity threshold set to %d\n",
960 chip->client->name, chip->tsl2x7x_settings.prox_thres_high);
962 /* back to the way they were */
963 chip->tsl2x7x_settings.interrupts_en = tmp_irq_settings;
964 if (current_state == TSL2X7X_CHIP_WORKING)
965 tsl2x7x_chip_on(indio_dev);
968 static ssize_t tsl2x7x_power_state_show(struct device *dev,
969 struct device_attribute *attr, char *buf)
971 struct tsl2X7X_chip *chip = iio_priv(dev_get_drvdata(dev));
973 return snprintf(buf, PAGE_SIZE, "%d\n", chip->tsl2x7x_chip_status);
976 static ssize_t tsl2x7x_power_state_store(struct device *dev,
977 struct device_attribute *attr, const char *buf, size_t len)
979 struct iio_dev *indio_dev = dev_get_drvdata(dev);
982 if (strtobool(buf, &value))
986 tsl2x7x_chip_on(indio_dev);
988 tsl2x7x_chip_off(indio_dev);
993 static ssize_t tsl2x7x_gain_available_show(struct device *dev,
994 struct device_attribute *attr, char *buf)
996 struct tsl2X7X_chip *chip = iio_priv(dev_get_drvdata(dev));
1004 return snprintf(buf, PAGE_SIZE, "%s\n", "1 8 16 128");
1008 return snprintf(buf, PAGE_SIZE, "%s\n", "1 8 16 120");
1011 static ssize_t tsl2x7x_prox_gain_available_show(struct device *dev,
1012 struct device_attribute *attr, char *buf)
1014 return snprintf(buf, PAGE_SIZE, "%s\n", "1 2 4 8");
1017 static ssize_t tsl2x7x_als_time_show(struct device *dev,
1018 struct device_attribute *attr, char *buf)
1020 struct tsl2X7X_chip *chip = iio_priv(dev_get_drvdata(dev));
1023 y = (TSL2X7X_MAX_TIMER_CNT - (u8)chip->tsl2x7x_settings.als_time) + 1;
1028 return snprintf(buf, PAGE_SIZE, "%d.%03d\n", y, z);
1031 static ssize_t tsl2x7x_als_time_store(struct device *dev,
1032 struct device_attribute *attr, const char *buf, size_t len)
1034 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1035 struct tsl2X7X_chip *chip = iio_priv(dev_get_drvdata(dev));
1036 struct tsl2x7x_parse_result result;
1041 tsl2x7x_parse_buffer(buf, &result);
1043 result.fract /= 1000;
1045 chip->tsl2x7x_settings.als_time =
1046 (TSL2X7X_MAX_TIMER_CNT - (u8)result.fract);
1048 dev_info(&chip->client->dev, "%s: als time = %d",
1049 __func__, chip->tsl2x7x_settings.als_time);
1051 tsl2x7x_invoke_change(indio_dev);
1053 return IIO_VAL_INT_PLUS_MICRO;
1056 static IIO_CONST_ATTR(in_illuminance0_integration_time_available,
1059 static ssize_t tsl2x7x_als_cal_target_show(struct device *dev,
1060 struct device_attribute *attr, char *buf)
1062 struct tsl2X7X_chip *chip = iio_priv(dev_get_drvdata(dev));
1064 return snprintf(buf, PAGE_SIZE, "%d\n",
1065 chip->tsl2x7x_settings.als_cal_target);
1068 static ssize_t tsl2x7x_als_cal_target_store(struct device *dev,
1069 struct device_attribute *attr, const char *buf, size_t len)
1071 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1072 struct tsl2X7X_chip *chip = iio_priv(dev_get_drvdata(dev));
1073 unsigned long value;
1075 if (kstrtoul(buf, 0, &value))
1079 chip->tsl2x7x_settings.als_cal_target = value;
1081 tsl2x7x_invoke_change(indio_dev);
1086 /* persistence settings */
1087 static ssize_t tsl2x7x_als_persistence_show(struct device *dev,
1088 struct device_attribute *attr, char *buf)
1090 struct tsl2X7X_chip *chip = iio_priv(dev_get_drvdata(dev));
1091 int y, z, filter_delay;
1093 /* Determine integration time */
1094 y = (TSL2X7X_MAX_TIMER_CNT - (u8)chip->tsl2x7x_settings.als_time) + 1;
1096 filter_delay = z * (chip->tsl2x7x_settings.persistence & 0x0F);
1097 y = (filter_delay / 1000);
1098 z = (filter_delay % 1000);
1100 return snprintf(buf, PAGE_SIZE, "%d.%03d\n", y, z);
1103 static ssize_t tsl2x7x_als_persistence_store(struct device *dev,
1104 struct device_attribute *attr, const char *buf, size_t len)
1106 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1107 struct tsl2X7X_chip *chip = iio_priv(dev_get_drvdata(dev));
1108 struct tsl2x7x_parse_result result;
1109 int y, z, filter_delay;
1113 tsl2x7x_parse_buffer(buf, &result);
1115 result.fract /= 1000;
1116 y = (TSL2X7X_MAX_TIMER_CNT - (u8)chip->tsl2x7x_settings.als_time) + 1;
1120 DIV_ROUND_UP(((result.integer * 1000) + result.fract), z);
1122 chip->tsl2x7x_settings.persistence &= 0xF0;
1123 chip->tsl2x7x_settings.persistence |= (filter_delay & 0x0F);
1125 dev_info(&chip->client->dev, "%s: als persistence = %d",
1126 __func__, filter_delay);
1128 tsl2x7x_invoke_change(indio_dev);
1130 return IIO_VAL_INT_PLUS_MICRO;
1133 static ssize_t tsl2x7x_prox_persistence_show(struct device *dev,
1134 struct device_attribute *attr, char *buf)
1136 struct tsl2X7X_chip *chip = iio_priv(dev_get_drvdata(dev));
1137 int y, z, filter_delay;
1139 /* Determine integration time */
1140 y = (TSL2X7X_MAX_TIMER_CNT - (u8)chip->tsl2x7x_settings.prx_time) + 1;
1142 filter_delay = z * ((chip->tsl2x7x_settings.persistence & 0xF0) >> 4);
1143 y = (filter_delay / 1000);
1144 z = (filter_delay % 1000);
1146 return snprintf(buf, PAGE_SIZE, "%d.%03d\n", y, z);
1149 static ssize_t tsl2x7x_prox_persistence_store(struct device *dev,
1150 struct device_attribute *attr, const char *buf, size_t len)
1152 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1153 struct tsl2X7X_chip *chip = iio_priv(dev_get_drvdata(dev));
1154 struct tsl2x7x_parse_result result;
1155 int y, z, filter_delay;
1159 tsl2x7x_parse_buffer(buf, &result);
1161 result.fract /= 1000;
1162 y = (TSL2X7X_MAX_TIMER_CNT - (u8)chip->tsl2x7x_settings.prx_time) + 1;
1166 DIV_ROUND_UP(((result.integer * 1000) + result.fract), z);
1168 chip->tsl2x7x_settings.persistence &= 0x0F;
1169 chip->tsl2x7x_settings.persistence |= ((filter_delay << 4) & 0xF0);
1171 dev_info(&chip->client->dev, "%s: prox persistence = %d",
1172 __func__, filter_delay);
1174 tsl2x7x_invoke_change(indio_dev);
1176 return IIO_VAL_INT_PLUS_MICRO;
1179 static ssize_t tsl2x7x_do_calibrate(struct device *dev,
1180 struct device_attribute *attr, const char *buf, size_t len)
1182 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1185 if (strtobool(buf, &value))
1189 tsl2x7x_als_calibrate(indio_dev);
1191 tsl2x7x_invoke_change(indio_dev);
1196 static ssize_t tsl2x7x_luxtable_show(struct device *dev,
1197 struct device_attribute *attr, char *buf)
1199 struct tsl2X7X_chip *chip = iio_priv(dev_get_drvdata(dev));
1203 while (i < (TSL2X7X_MAX_LUX_TABLE_SIZE * 3)) {
1204 offset += snprintf(buf + offset, PAGE_SIZE, "%d,%d,%d,",
1205 chip->tsl2x7x_device_lux[i].ratio,
1206 chip->tsl2x7x_device_lux[i].ch0,
1207 chip->tsl2x7x_device_lux[i].ch1);
1208 if (chip->tsl2x7x_device_lux[i].ratio == 0) {
1209 /* We just printed the first "0" entry.
1210 * Now get rid of the extra "," and break. */
1217 offset += snprintf(buf + offset, PAGE_SIZE, "\n");
1221 static ssize_t tsl2x7x_luxtable_store(struct device *dev,
1222 struct device_attribute *attr, const char *buf, size_t len)
1224 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1225 struct tsl2X7X_chip *chip = iio_priv(dev_get_drvdata(dev));
1226 int value[ARRAY_SIZE(chip->tsl2x7x_device_lux)*3 + 1];
1229 get_options(buf, ARRAY_SIZE(value), value);
1231 /* We now have an array of ints starting at value[1], and
1232 * enumerated by value[0].
1233 * We expect each group of three ints is one table entry,
1234 * and the last table entry is all 0.
1237 if ((n % 3) || n < 6 ||
1238 n > ((ARRAY_SIZE(chip->tsl2x7x_device_lux) - 1) * 3)) {
1239 dev_info(dev, "LUX TABLE INPUT ERROR 1 Value[0]=%d\n", n);
1243 if ((value[(n - 2)] | value[(n - 1)] | value[n]) != 0) {
1244 dev_info(dev, "LUX TABLE INPUT ERROR 2 Value[0]=%d\n", n);
1248 if (chip->tsl2x7x_chip_status == TSL2X7X_CHIP_WORKING)
1249 tsl2x7x_chip_off(indio_dev);
1251 /* Zero out the table */
1252 memset(chip->tsl2x7x_device_lux, 0, sizeof(chip->tsl2x7x_device_lux));
1253 memcpy(chip->tsl2x7x_device_lux, &value[1], (value[0] * 4));
1255 tsl2x7x_invoke_change(indio_dev);
1260 static ssize_t tsl2x7x_do_prox_calibrate(struct device *dev,
1261 struct device_attribute *attr, const char *buf, size_t len)
1263 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1266 if (strtobool(buf, &value))
1270 tsl2x7x_prox_cal(indio_dev);
1272 tsl2x7x_invoke_change(indio_dev);
1277 static int tsl2x7x_read_interrupt_config(struct iio_dev *indio_dev,
1280 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1283 if (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code) == IIO_INTENSITY)
1284 ret = !!(chip->tsl2x7x_settings.interrupts_en & 0x10);
1286 ret = !!(chip->tsl2x7x_settings.interrupts_en & 0x20);
1291 static int tsl2x7x_write_interrupt_config(struct iio_dev *indio_dev,
1295 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1297 if (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code) == IIO_INTENSITY) {
1299 chip->tsl2x7x_settings.interrupts_en |= 0x10;
1301 chip->tsl2x7x_settings.interrupts_en &= 0x20;
1304 chip->tsl2x7x_settings.interrupts_en |= 0x20;
1306 chip->tsl2x7x_settings.interrupts_en &= 0x10;
1309 tsl2x7x_invoke_change(indio_dev);
1314 static int tsl2x7x_write_thresh(struct iio_dev *indio_dev,
1318 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1320 if (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code) == IIO_INTENSITY) {
1321 switch (IIO_EVENT_CODE_EXTRACT_DIR(event_code)) {
1322 case IIO_EV_DIR_RISING:
1323 chip->tsl2x7x_settings.als_thresh_high = val;
1325 case IIO_EV_DIR_FALLING:
1326 chip->tsl2x7x_settings.als_thresh_low = val;
1332 switch (IIO_EVENT_CODE_EXTRACT_DIR(event_code)) {
1333 case IIO_EV_DIR_RISING:
1334 chip->tsl2x7x_settings.prox_thres_high = val;
1336 case IIO_EV_DIR_FALLING:
1337 chip->tsl2x7x_settings.prox_thres_low = val;
1344 tsl2x7x_invoke_change(indio_dev);
1349 static int tsl2x7x_read_thresh(struct iio_dev *indio_dev,
1353 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1355 if (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code) == IIO_INTENSITY) {
1356 switch (IIO_EVENT_CODE_EXTRACT_DIR(event_code)) {
1357 case IIO_EV_DIR_RISING:
1358 *val = chip->tsl2x7x_settings.als_thresh_high;
1360 case IIO_EV_DIR_FALLING:
1361 *val = chip->tsl2x7x_settings.als_thresh_low;
1367 switch (IIO_EVENT_CODE_EXTRACT_DIR(event_code)) {
1368 case IIO_EV_DIR_RISING:
1369 *val = chip->tsl2x7x_settings.prox_thres_high;
1371 case IIO_EV_DIR_FALLING:
1372 *val = chip->tsl2x7x_settings.prox_thres_low;
1382 static int tsl2x7x_read_raw(struct iio_dev *indio_dev,
1383 struct iio_chan_spec const *chan,
1389 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1392 case IIO_CHAN_INFO_RAW:
1393 switch (chan->type) {
1395 tsl2x7x_get_lux(indio_dev);
1396 *val = chip->als_cur_info.lux;
1400 tsl2x7x_get_lux(indio_dev);
1401 if (chan->channel == 0)
1402 *val = chip->als_cur_info.als_ch0;
1404 *val = chip->als_cur_info.als_ch1;
1408 tsl2x7x_get_prox(indio_dev);
1409 *val = chip->prox_data;
1417 case IIO_CHAN_INFO_CALIBSCALE:
1418 if (chan->type == IIO_LIGHT)
1420 tsl2X7X_als_gainadj[chip->tsl2x7x_settings.als_gain];
1423 tsl2X7X_prx_gainadj[chip->tsl2x7x_settings.prox_gain];
1426 case IIO_CHAN_INFO_CALIBBIAS:
1427 *val = chip->tsl2x7x_settings.als_gain_trim;
1438 static int tsl2x7x_write_raw(struct iio_dev *indio_dev,
1439 struct iio_chan_spec const *chan,
1444 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1447 case IIO_CHAN_INFO_CALIBSCALE:
1448 if (chan->type == IIO_INTENSITY) {
1451 chip->tsl2x7x_settings.als_gain = 0;
1454 chip->tsl2x7x_settings.als_gain = 1;
1457 chip->tsl2x7x_settings.als_gain = 2;
1469 chip->tsl2x7x_settings.als_gain = 3;
1481 chip->tsl2x7x_settings.als_gain = 3;
1489 chip->tsl2x7x_settings.prox_gain = 0;
1492 chip->tsl2x7x_settings.prox_gain = 1;
1495 chip->tsl2x7x_settings.prox_gain = 2;
1498 chip->tsl2x7x_settings.prox_gain = 3;
1505 case IIO_CHAN_INFO_CALIBBIAS:
1506 chip->tsl2x7x_settings.als_gain_trim = val;
1513 tsl2x7x_invoke_change(indio_dev);
1518 static DEVICE_ATTR(power_state, S_IRUGO | S_IWUSR,
1519 tsl2x7x_power_state_show, tsl2x7x_power_state_store);
1521 static DEVICE_ATTR(in_proximity0_calibscale_available, S_IRUGO,
1522 tsl2x7x_prox_gain_available_show, NULL);
1524 static DEVICE_ATTR(in_illuminance0_calibscale_available, S_IRUGO,
1525 tsl2x7x_gain_available_show, NULL);
1527 static DEVICE_ATTR(in_illuminance0_integration_time, S_IRUGO | S_IWUSR,
1528 tsl2x7x_als_time_show, tsl2x7x_als_time_store);
1530 static DEVICE_ATTR(in_illuminance0_target_input, S_IRUGO | S_IWUSR,
1531 tsl2x7x_als_cal_target_show, tsl2x7x_als_cal_target_store);
1533 static DEVICE_ATTR(in_illuminance0_calibrate, S_IWUSR, NULL,
1534 tsl2x7x_do_calibrate);
1536 static DEVICE_ATTR(in_proximity0_calibrate, S_IWUSR, NULL,
1537 tsl2x7x_do_prox_calibrate);
1539 static DEVICE_ATTR(in_illuminance0_lux_table, S_IRUGO | S_IWUSR,
1540 tsl2x7x_luxtable_show, tsl2x7x_luxtable_store);
1542 static DEVICE_ATTR(in_intensity0_thresh_period, S_IRUGO | S_IWUSR,
1543 tsl2x7x_als_persistence_show, tsl2x7x_als_persistence_store);
1545 static DEVICE_ATTR(in_proximity0_thresh_period, S_IRUGO | S_IWUSR,
1546 tsl2x7x_prox_persistence_show, tsl2x7x_prox_persistence_store);
1548 /* Use the default register values to identify the Taos device */
1549 static int tsl2x7x_device_id(unsigned char *id, int target)
1555 return ((*id & 0xf0) == TRITON_ID);
1559 return ((*id & 0xf0) == HALIBUT_ID);
1566 return ((*id & 0xf0) == SWORDFISH_ID);
1573 static irqreturn_t tsl2x7x_event_handler(int irq, void *private)
1575 struct iio_dev *indio_dev = private;
1576 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1577 s64 timestamp = iio_get_time_ns();
1581 value = i2c_smbus_read_byte_data(chip->client,
1582 TSL2X7X_CMD_REG | TSL2X7X_STATUS);
1584 /* What type of interrupt do we need to process */
1585 if (value & TSL2X7X_STA_PRX_INTR) {
1586 tsl2x7x_get_prox(indio_dev); /* freshen data for ABI */
1587 iio_push_event(indio_dev,
1588 IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY,
1595 if (value & TSL2X7X_STA_ALS_INTR) {
1596 tsl2x7x_get_lux(indio_dev); /* freshen data for ABI */
1597 iio_push_event(indio_dev,
1598 IIO_UNMOD_EVENT_CODE(IIO_LIGHT,
1604 /* Clear interrupt now that we have handled it. */
1605 ret = i2c_smbus_write_byte(chip->client,
1606 TSL2X7X_CMD_REG | TSL2X7X_CMD_SPL_FN |
1607 TSL2X7X_CMD_PROXALS_INT_CLR);
1609 dev_err(&chip->client->dev,
1610 "%s: Failed to clear irq from event handler. err = %d\n",
1616 static struct attribute *tsl2x7x_ALS_device_attrs[] = {
1617 &dev_attr_power_state.attr,
1618 &dev_attr_in_illuminance0_calibscale_available.attr,
1619 &dev_attr_in_illuminance0_integration_time.attr,
1620 &iio_const_attr_in_illuminance0_integration_time_available\
1622 &dev_attr_in_illuminance0_target_input.attr,
1623 &dev_attr_in_illuminance0_calibrate.attr,
1624 &dev_attr_in_illuminance0_lux_table.attr,
1628 static struct attribute *tsl2x7x_PRX_device_attrs[] = {
1629 &dev_attr_power_state.attr,
1630 &dev_attr_in_proximity0_calibrate.attr,
1634 static struct attribute *tsl2x7x_ALSPRX_device_attrs[] = {
1635 &dev_attr_power_state.attr,
1636 &dev_attr_in_illuminance0_calibscale_available.attr,
1637 &dev_attr_in_illuminance0_integration_time.attr,
1638 &iio_const_attr_in_illuminance0_integration_time_available\
1640 &dev_attr_in_illuminance0_target_input.attr,
1641 &dev_attr_in_illuminance0_calibrate.attr,
1642 &dev_attr_in_illuminance0_lux_table.attr,
1643 &dev_attr_in_proximity0_calibrate.attr,
1647 static struct attribute *tsl2x7x_PRX2_device_attrs[] = {
1648 &dev_attr_power_state.attr,
1649 &dev_attr_in_proximity0_calibrate.attr,
1650 &dev_attr_in_proximity0_calibscale_available.attr,
1654 static struct attribute *tsl2x7x_ALSPRX2_device_attrs[] = {
1655 &dev_attr_power_state.attr,
1656 &dev_attr_in_illuminance0_calibscale_available.attr,
1657 &dev_attr_in_illuminance0_integration_time.attr,
1658 &iio_const_attr_in_illuminance0_integration_time_available\
1660 &dev_attr_in_illuminance0_target_input.attr,
1661 &dev_attr_in_illuminance0_calibrate.attr,
1662 &dev_attr_in_illuminance0_lux_table.attr,
1663 &dev_attr_in_proximity0_calibrate.attr,
1664 &dev_attr_in_proximity0_calibscale_available.attr,
1668 static struct attribute *tsl2X7X_ALS_event_attrs[] = {
1669 &dev_attr_in_intensity0_thresh_period.attr,
1672 static struct attribute *tsl2X7X_PRX_event_attrs[] = {
1673 &dev_attr_in_proximity0_thresh_period.attr,
1677 static struct attribute *tsl2X7X_ALSPRX_event_attrs[] = {
1678 &dev_attr_in_intensity0_thresh_period.attr,
1679 &dev_attr_in_proximity0_thresh_period.attr,
1683 static const struct attribute_group tsl2X7X_device_attr_group_tbl[] = {
1685 .attrs = tsl2x7x_ALS_device_attrs,
1688 .attrs = tsl2x7x_PRX_device_attrs,
1691 .attrs = tsl2x7x_ALSPRX_device_attrs,
1694 .attrs = tsl2x7x_PRX2_device_attrs,
1697 .attrs = tsl2x7x_ALSPRX2_device_attrs,
1701 static struct attribute_group tsl2X7X_event_attr_group_tbl[] = {
1703 .attrs = tsl2X7X_ALS_event_attrs,
1707 .attrs = tsl2X7X_PRX_event_attrs,
1711 .attrs = tsl2X7X_ALSPRX_event_attrs,
1716 static const struct iio_info tsl2X7X_device_info[] = {
1718 .attrs = &tsl2X7X_device_attr_group_tbl[ALS],
1719 .event_attrs = &tsl2X7X_event_attr_group_tbl[ALS],
1720 .driver_module = THIS_MODULE,
1721 .read_raw = &tsl2x7x_read_raw,
1722 .write_raw = &tsl2x7x_write_raw,
1723 .read_event_value = &tsl2x7x_read_thresh,
1724 .write_event_value = &tsl2x7x_write_thresh,
1725 .read_event_config = &tsl2x7x_read_interrupt_config,
1726 .write_event_config = &tsl2x7x_write_interrupt_config,
1729 .attrs = &tsl2X7X_device_attr_group_tbl[PRX],
1730 .event_attrs = &tsl2X7X_event_attr_group_tbl[PRX],
1731 .driver_module = THIS_MODULE,
1732 .read_raw = &tsl2x7x_read_raw,
1733 .write_raw = &tsl2x7x_write_raw,
1734 .read_event_value = &tsl2x7x_read_thresh,
1735 .write_event_value = &tsl2x7x_write_thresh,
1736 .read_event_config = &tsl2x7x_read_interrupt_config,
1737 .write_event_config = &tsl2x7x_write_interrupt_config,
1740 .attrs = &tsl2X7X_device_attr_group_tbl[ALSPRX],
1741 .event_attrs = &tsl2X7X_event_attr_group_tbl[ALSPRX],
1742 .driver_module = THIS_MODULE,
1743 .read_raw = &tsl2x7x_read_raw,
1744 .write_raw = &tsl2x7x_write_raw,
1745 .read_event_value = &tsl2x7x_read_thresh,
1746 .write_event_value = &tsl2x7x_write_thresh,
1747 .read_event_config = &tsl2x7x_read_interrupt_config,
1748 .write_event_config = &tsl2x7x_write_interrupt_config,
1751 .attrs = &tsl2X7X_device_attr_group_tbl[PRX2],
1752 .event_attrs = &tsl2X7X_event_attr_group_tbl[PRX],
1753 .driver_module = THIS_MODULE,
1754 .read_raw = &tsl2x7x_read_raw,
1755 .write_raw = &tsl2x7x_write_raw,
1756 .read_event_value = &tsl2x7x_read_thresh,
1757 .write_event_value = &tsl2x7x_write_thresh,
1758 .read_event_config = &tsl2x7x_read_interrupt_config,
1759 .write_event_config = &tsl2x7x_write_interrupt_config,
1762 .attrs = &tsl2X7X_device_attr_group_tbl[ALSPRX2],
1763 .event_attrs = &tsl2X7X_event_attr_group_tbl[ALSPRX],
1764 .driver_module = THIS_MODULE,
1765 .read_raw = &tsl2x7x_read_raw,
1766 .write_raw = &tsl2x7x_write_raw,
1767 .read_event_value = &tsl2x7x_read_thresh,
1768 .write_event_value = &tsl2x7x_write_thresh,
1769 .read_event_config = &tsl2x7x_read_interrupt_config,
1770 .write_event_config = &tsl2x7x_write_interrupt_config,
1774 static const struct tsl2x7x_chip_info tsl2x7x_chip_info_tbl[] = {
1781 .info_mask = IIO_CHAN_INFO_PROCESSED_SEPARATE_BIT,
1783 .type = IIO_INTENSITY,
1786 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
1787 IIO_CHAN_INFO_CALIBSCALE_SEPARATE_BIT |
1788 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT,
1789 .event_mask = TSL2X7X_EVENT_MASK
1791 .type = IIO_INTENSITY,
1796 .chan_table_elements = 3,
1797 .info = &tsl2X7X_device_info[ALS],
1802 .type = IIO_PROXIMITY,
1805 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
1806 .event_mask = TSL2X7X_EVENT_MASK
1809 .chan_table_elements = 1,
1810 .info = &tsl2X7X_device_info[PRX],
1818 .info_mask = IIO_CHAN_INFO_PROCESSED_SEPARATE_BIT
1820 .type = IIO_INTENSITY,
1823 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
1824 IIO_CHAN_INFO_CALIBSCALE_SEPARATE_BIT |
1825 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT,
1826 .event_mask = TSL2X7X_EVENT_MASK
1828 .type = IIO_INTENSITY,
1831 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
1833 .type = IIO_PROXIMITY,
1836 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
1837 .event_mask = TSL2X7X_EVENT_MASK
1840 .chan_table_elements = 4,
1841 .info = &tsl2X7X_device_info[ALSPRX],
1846 .type = IIO_PROXIMITY,
1849 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
1850 IIO_CHAN_INFO_CALIBSCALE_SEPARATE_BIT,
1851 .event_mask = TSL2X7X_EVENT_MASK
1854 .chan_table_elements = 1,
1855 .info = &tsl2X7X_device_info[PRX2],
1863 .info_mask = IIO_CHAN_INFO_PROCESSED_SEPARATE_BIT,
1865 .type = IIO_INTENSITY,
1868 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
1869 IIO_CHAN_INFO_CALIBSCALE_SEPARATE_BIT |
1870 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT,
1871 .event_mask = TSL2X7X_EVENT_MASK
1873 .type = IIO_INTENSITY,
1876 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
1878 .type = IIO_PROXIMITY,
1881 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
1882 IIO_CHAN_INFO_CALIBSCALE_SEPARATE_BIT,
1883 .event_mask = TSL2X7X_EVENT_MASK
1886 .chan_table_elements = 4,
1887 .info = &tsl2X7X_device_info[ALSPRX2],
1891 static int __devinit tsl2x7x_probe(struct i2c_client *clientp,
1892 const struct i2c_device_id *id)
1895 unsigned char device_id;
1896 struct iio_dev *indio_dev;
1897 struct tsl2X7X_chip *chip;
1899 indio_dev = iio_allocate_device(sizeof(*chip));
1903 chip = iio_priv(indio_dev);
1904 chip->client = clientp;
1905 i2c_set_clientdata(clientp, indio_dev);
1907 ret = tsl2x7x_i2c_read(chip->client,
1908 TSL2X7X_CHIPID, &device_id);
1912 if ((!tsl2x7x_device_id(&device_id, id->driver_data)) ||
1913 (tsl2x7x_device_id(&device_id, id->driver_data) == -EINVAL)) {
1914 dev_info(&chip->client->dev,
1915 "%s: i2c device found does not match expected id\n",
1920 ret = i2c_smbus_write_byte(clientp, (TSL2X7X_CMD_REG | TSL2X7X_CNTRL));
1922 dev_err(&clientp->dev, "%s: write to cmd reg failed. err = %d\n",
1927 /* ALS and PROX functions can be invoked via user space poll
1928 * or H/W interrupt. If busy return last sample. */
1929 mutex_init(&chip->als_mutex);
1930 mutex_init(&chip->prox_mutex);
1932 chip->tsl2x7x_chip_status = TSL2X7X_CHIP_UNKNOWN;
1933 chip->pdata = clientp->dev.platform_data;
1934 chip->id = id->driver_data;
1936 &tsl2x7x_chip_info_tbl[device_channel_config[id->driver_data]];
1938 indio_dev->info = chip->chip_info->info;
1939 indio_dev->dev.parent = &clientp->dev;
1940 indio_dev->modes = INDIO_DIRECT_MODE;
1941 indio_dev->name = chip->client->name;
1942 indio_dev->channels = chip->chip_info->channel;
1943 indio_dev->num_channels = chip->chip_info->chan_table_elements;
1946 ret = request_threaded_irq(clientp->irq,
1948 &tsl2x7x_event_handler,
1949 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
1953 dev_err(&clientp->dev,
1954 "%s: irq request failed", __func__);
1959 /* Load up the defaults */
1960 tsl2x7x_defaults(chip);
1961 /* Make sure the chip is on */
1962 tsl2x7x_chip_on(indio_dev);
1964 ret = iio_device_register(indio_dev);
1966 dev_err(&clientp->dev,
1967 "%s: iio registration failed\n", __func__);
1971 dev_info(&clientp->dev, "%s Light sensor found.\n", id->name);
1977 free_irq(clientp->irq, indio_dev);
1979 iio_free_device(indio_dev);
1984 static int tsl2x7x_suspend(struct device *dev)
1986 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1987 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1990 if (chip->tsl2x7x_chip_status == TSL2X7X_CHIP_WORKING) {
1991 ret = tsl2x7x_chip_off(indio_dev);
1992 chip->tsl2x7x_chip_status = TSL2X7X_CHIP_SUSPENDED;
1995 if (chip->pdata && chip->pdata->platform_power) {
1996 pm_message_t pmm = {PM_EVENT_SUSPEND};
1997 chip->pdata->platform_power(dev, pmm);
2003 static int tsl2x7x_resume(struct device *dev)
2005 struct iio_dev *indio_dev = dev_get_drvdata(dev);
2006 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
2009 if (chip->pdata && chip->pdata->platform_power) {
2010 pm_message_t pmm = {PM_EVENT_RESUME};
2011 chip->pdata->platform_power(dev, pmm);
2014 if (chip->tsl2x7x_chip_status == TSL2X7X_CHIP_SUSPENDED)
2015 ret = tsl2x7x_chip_on(indio_dev);
2020 static int __devexit tsl2x7x_remove(struct i2c_client *client)
2022 struct tsl2X7X_chip *chip = i2c_get_clientdata(client);
2023 struct iio_dev *indio_dev = iio_priv_to_dev(chip);
2025 tsl2x7x_chip_off(indio_dev);
2027 iio_device_unregister(indio_dev);
2029 free_irq(client->irq, chip->client->name);
2031 iio_free_device(indio_dev);
2036 static struct i2c_device_id tsl2x7x_idtable[] = {
2037 { "tsl2571", tsl2571 },
2038 { "tsl2671", tsl2671 },
2039 { "tmd2671", tmd2671 },
2040 { "tsl2771", tsl2771 },
2041 { "tmd2771", tmd2771 },
2042 { "tsl2572", tsl2572 },
2043 { "tsl2672", tsl2672 },
2044 { "tmd2672", tmd2672 },
2045 { "tsl2772", tsl2772 },
2046 { "tmd2772", tmd2772 },
2050 MODULE_DEVICE_TABLE(i2c, tsl2x7x_idtable);
2052 static const struct dev_pm_ops tsl2x7x_pm_ops = {
2053 .suspend = tsl2x7x_suspend,
2054 .resume = tsl2x7x_resume,
2057 /* Driver definition */
2058 static struct i2c_driver tsl2x7x_driver = {
2061 .pm = &tsl2x7x_pm_ops,
2063 .id_table = tsl2x7x_idtable,
2064 .probe = tsl2x7x_probe,
2065 .remove = __devexit_p(tsl2x7x_remove),
2068 module_i2c_driver(tsl2x7x_driver);
2070 MODULE_AUTHOR("J. August Brenner<jbrenner@taosinc.com>");
2071 MODULE_DESCRIPTION("TAOS tsl2x7x ambient and proximity light sensor driver");
2072 MODULE_LICENSE("GPL");