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
18 #include <linux/kernel.h>
19 #include <linux/i2c.h>
20 #include <linux/errno.h>
21 #include <linux/delay.h>
22 #include <linux/mutex.h>
23 #include <linux/interrupt.h>
24 #include <linux/slab.h>
25 #include <linux/module.h>
26 #include <linux/iio/events.h>
27 #include <linux/iio/iio.h>
28 #include <linux/iio/sysfs.h>
32 #define PROX_STAT_CAL 0
33 #define PROX_STAT_SAMP 1
34 #define MAX_SAMPLES_CAL 200
36 /* TSL2X7X Device ID */
37 #define TRITON_ID 0x00
38 #define SWORDFISH_ID 0x30
39 #define HALIBUT_ID 0x20
41 /* Lux calculation constants */
42 #define TSL2X7X_LUX_CALC_OVER_FLOW 65535
44 /* TAOS Register definitions - note:
45 * depending on device, some of these register are not used and the
46 * register address is benign.
48 /* 2X7X register offsets */
49 #define TSL2X7X_MAX_CONFIG_REG 16
51 /* Device Registers and Masks */
52 #define TSL2X7X_CNTRL 0x00
53 #define TSL2X7X_ALS_TIME 0X01
54 #define TSL2X7X_PRX_TIME 0x02
55 #define TSL2X7X_WAIT_TIME 0x03
56 #define TSL2X7X_ALS_MINTHRESHLO 0X04
57 #define TSL2X7X_ALS_MINTHRESHHI 0X05
58 #define TSL2X7X_ALS_MAXTHRESHLO 0X06
59 #define TSL2X7X_ALS_MAXTHRESHHI 0X07
60 #define TSL2X7X_PRX_MINTHRESHLO 0X08
61 #define TSL2X7X_PRX_MINTHRESHHI 0X09
62 #define TSL2X7X_PRX_MAXTHRESHLO 0X0A
63 #define TSL2X7X_PRX_MAXTHRESHHI 0X0B
64 #define TSL2X7X_PERSISTENCE 0x0C
65 #define TSL2X7X_PRX_CONFIG 0x0D
66 #define TSL2X7X_PRX_COUNT 0x0E
67 #define TSL2X7X_GAIN 0x0F
68 #define TSL2X7X_NOTUSED 0x10
69 #define TSL2X7X_REVID 0x11
70 #define TSL2X7X_CHIPID 0x12
71 #define TSL2X7X_STATUS 0x13
72 #define TSL2X7X_ALS_CHAN0LO 0x14
73 #define TSL2X7X_ALS_CHAN0HI 0x15
74 #define TSL2X7X_ALS_CHAN1LO 0x16
75 #define TSL2X7X_ALS_CHAN1HI 0x17
76 #define TSL2X7X_PRX_LO 0x18
77 #define TSL2X7X_PRX_HI 0x19
79 /* tsl2X7X cmd reg masks */
80 #define TSL2X7X_CMD_REG 0x80
81 #define TSL2X7X_CMD_SPL_FN 0x60
83 #define TSL2X7X_CMD_PROX_INT_CLR 0X05
84 #define TSL2X7X_CMD_ALS_INT_CLR 0x06
85 #define TSL2X7X_CMD_PROXALS_INT_CLR 0X07
87 /* tsl2X7X cntrl reg masks */
88 #define TSL2X7X_CNTL_ADC_ENBL 0x02
89 #define TSL2X7X_CNTL_PWR_ON 0x01
91 /* tsl2X7X status reg masks */
92 #define TSL2X7X_STA_ADC_VALID 0x01
93 #define TSL2X7X_STA_PRX_VALID 0x02
94 #define TSL2X7X_STA_ADC_PRX_VALID (TSL2X7X_STA_ADC_VALID |\
95 TSL2X7X_STA_PRX_VALID)
96 #define TSL2X7X_STA_ALS_INTR 0x10
97 #define TSL2X7X_STA_PRX_INTR 0x20
99 /* tsl2X7X cntrl reg masks */
100 #define TSL2X7X_CNTL_REG_CLEAR 0x00
101 #define TSL2X7X_CNTL_PROX_INT_ENBL 0X20
102 #define TSL2X7X_CNTL_ALS_INT_ENBL 0X10
103 #define TSL2X7X_CNTL_WAIT_TMR_ENBL 0X08
104 #define TSL2X7X_CNTL_PROX_DET_ENBL 0X04
105 #define TSL2X7X_CNTL_PWRON 0x01
106 #define TSL2X7X_CNTL_ALSPON_ENBL 0x03
107 #define TSL2X7X_CNTL_INTALSPON_ENBL 0x13
108 #define TSL2X7X_CNTL_PROXPON_ENBL 0x0F
109 #define TSL2X7X_CNTL_INTPROXPON_ENBL 0x2F
111 /*Prox diode to use */
112 #define TSL2X7X_DIODE0 0x10
113 #define TSL2X7X_DIODE1 0x20
114 #define TSL2X7X_DIODE_BOTH 0x30
117 #define TSL2X7X_mA100 0x00
118 #define TSL2X7X_mA50 0x40
119 #define TSL2X7X_mA25 0x80
120 #define TSL2X7X_mA13 0xD0
121 #define TSL2X7X_MAX_TIMER_CNT (0xFF)
123 #define TSL2X7X_MIN_ITIME 3
125 /* TAOS txx2x7x Device family members */
140 TSL2X7X_CHIP_UNKNOWN = 0,
141 TSL2X7X_CHIP_WORKING = 1,
142 TSL2X7X_CHIP_SUSPENDED = 2
145 struct tsl2x7x_parse_result {
150 /* Per-device data */
151 struct tsl2x7x_als_info {
157 struct tsl2x7x_prox_stat {
161 unsigned long stddev;
164 struct tsl2x7x_chip_info {
165 int chan_table_elements;
166 struct iio_chan_spec channel[4];
167 const struct iio_info *info;
170 struct tsl2X7X_chip {
172 struct mutex prox_mutex;
173 struct mutex als_mutex;
174 struct i2c_client *client;
176 struct tsl2x7x_als_info als_cur_info;
177 struct tsl2x7x_settings tsl2x7x_settings;
178 struct tsl2X7X_platform_data *pdata;
181 int tsl2x7x_chip_status;
182 u8 tsl2x7x_config[TSL2X7X_MAX_CONFIG_REG];
183 const struct tsl2x7x_chip_info *chip_info;
184 const struct iio_info *info;
187 * This structure is intentionally large to accommodate
189 * Sized to 9 = max 8 segments + 1 termination segment
191 struct tsl2x7x_lux tsl2x7x_device_lux[TSL2X7X_MAX_LUX_TABLE_SIZE];
194 /* Different devices require different coefficents */
195 static const struct tsl2x7x_lux tsl2x71_lux_table[] = {
196 { 14461, 611, 1211 },
201 static const struct tsl2x7x_lux tmd2x71_lux_table[] = {
207 static const struct tsl2x7x_lux tsl2x72_lux_table[] = {
213 static const struct tsl2x7x_lux tmd2x72_lux_table[] = {
219 static const struct tsl2x7x_lux *tsl2x7x_default_lux_table_group[] = {
220 [tsl2571] = tsl2x71_lux_table,
221 [tsl2671] = tsl2x71_lux_table,
222 [tmd2671] = tmd2x71_lux_table,
223 [tsl2771] = tsl2x71_lux_table,
224 [tmd2771] = tmd2x71_lux_table,
225 [tsl2572] = tsl2x72_lux_table,
226 [tsl2672] = tsl2x72_lux_table,
227 [tmd2672] = tmd2x72_lux_table,
228 [tsl2772] = tsl2x72_lux_table,
229 [tmd2772] = tmd2x72_lux_table,
232 static const struct tsl2x7x_settings tsl2x7x_default_settings = {
233 .als_time = 219, /* 101 ms */
235 .prx_time = 254, /* 5.4 ms */
239 .als_gain_trim = 1000,
240 .als_cal_target = 150,
241 .als_thresh_low = 200,
242 .als_thresh_high = 256,
246 .prox_thres_high = 512,
247 .prox_max_samples_cal = 30,
248 .prox_pulse_count = 8
251 static const s16 tsl2X7X_als_gainadj[] = {
258 static const s16 tsl2X7X_prx_gainadj[] = {
265 /* Channel variations */
274 static const u8 device_channel_config[] = {
288 * tsl2x7x_get_lux() - Reads and calculates current lux value.
289 * @indio_dev: pointer to IIO device
291 * The raw ch0 and ch1 values of the ambient light sensed in the last
292 * integration cycle are read from the device.
293 * Time scale factor array values are adjusted based on the integration time.
294 * The raw values are multiplied by a scale factor, and device gain is obtained
295 * using gain index. Limit checks are done next, then the ratio of a multiple
296 * of ch1 value, to the ch0 value, is calculated. Array tsl2x7x_device_lux[]
297 * is then scanned to find the first ratio value that is just above the ratio
298 * we just calculated. The ch0 and ch1 multiplier constants in the array are
299 * then used along with the time scale factor array values, to calculate the
302 static int tsl2x7x_get_lux(struct iio_dev *indio_dev)
304 u16 ch0, ch1; /* separated ch0/ch1 data from device */
305 u32 lux; /* raw lux calculated from device data */
309 struct tsl2x7x_lux *p;
310 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
315 if (mutex_trylock(&chip->als_mutex) == 0)
316 return chip->als_cur_info.lux; /* busy, so return LAST VALUE */
318 if (chip->tsl2x7x_chip_status != TSL2X7X_CHIP_WORKING) {
319 /* device is not enabled */
320 dev_err(&chip->client->dev, "%s: device is not enabled\n",
326 ret = i2c_smbus_read_byte_data(chip->client,
327 TSL2X7X_CMD_REG | TSL2X7X_STATUS);
329 dev_err(&chip->client->dev,
330 "%s: Failed to read STATUS Reg\n", __func__);
333 /* is data new & valid */
334 if (!(ret & TSL2X7X_STA_ADC_VALID)) {
335 dev_err(&chip->client->dev,
336 "%s: data not valid yet\n", __func__);
337 ret = chip->als_cur_info.lux; /* return LAST VALUE */
341 for (i = 0; i < 4; i++) {
342 int reg = TSL2X7X_CMD_REG | (TSL2X7X_ALS_CHAN0LO + i);
344 ret = i2c_smbus_read_byte_data(chip->client, reg);
346 dev_err(&chip->client->dev,
347 "failed to read. err=%x\n", ret);
354 /* clear any existing interrupt status */
355 ret = i2c_smbus_write_byte(chip->client,
358 TSL2X7X_CMD_ALS_INT_CLR));
360 dev_err(&chip->client->dev,
361 "i2c_write_command failed - err = %d\n", ret);
362 goto out_unlock; /* have no data, so return failure */
365 /* extract ALS/lux data */
366 ch0 = le16_to_cpup((const __le16 *)&buf[0]);
367 ch1 = le16_to_cpup((const __le16 *)&buf[2]);
369 chip->als_cur_info.als_ch0 = ch0;
370 chip->als_cur_info.als_ch1 = ch1;
372 if ((ch0 >= chip->als_saturation) || (ch1 >= chip->als_saturation)) {
373 lux = TSL2X7X_LUX_CALC_OVER_FLOW;
378 /* have no data, so return LAST VALUE */
379 ret = chip->als_cur_info.lux;
382 /* calculate ratio */
383 ratio = (ch1 << 15) / ch0;
384 /* convert to unscaled lux using the pointer to the table */
385 p = (struct tsl2x7x_lux *)chip->tsl2x7x_device_lux;
386 while (p->ratio != 0 && p->ratio < ratio)
392 ch0lux = DIV_ROUND_UP(ch0 * p->ch0,
393 tsl2X7X_als_gainadj[chip->tsl2x7x_settings.als_gain]);
394 ch1lux = DIV_ROUND_UP(ch1 * p->ch1,
395 tsl2X7X_als_gainadj[chip->tsl2x7x_settings.als_gain]);
396 lux = ch0lux - ch1lux;
399 /* note: lux is 31 bit max at this point */
400 if (ch1lux > ch0lux) {
401 dev_dbg(&chip->client->dev, "ch1lux > ch0lux-return last value\n");
402 ret = chip->als_cur_info.lux;
406 /* adjust for active time scale */
407 if (chip->als_time_scale == 0)
410 lux = (lux + (chip->als_time_scale >> 1)) /
411 chip->als_time_scale;
413 /* adjust for active gain scale
414 * The tsl2x7x_device_lux tables have a factor of 256 built-in.
415 * User-specified gain provides a multiplier.
416 * Apply user-specified gain before shifting right to retain precision.
417 * Use 64 bits to avoid overflow on multiplication.
418 * Then go back to 32 bits before division to avoid using div_u64().
422 lux64 = lux64 * chip->tsl2x7x_settings.als_gain_trim;
425 lux = (lux + 500) / 1000;
427 if (lux > TSL2X7X_LUX_CALC_OVER_FLOW) /* check for overflow */
428 lux = TSL2X7X_LUX_CALC_OVER_FLOW;
430 /* Update the structure with the latest lux. */
432 chip->als_cur_info.lux = lux;
436 mutex_unlock(&chip->als_mutex);
442 * tsl2x7x_get_prox() - Reads proximity data registers and updates
445 * @indio_dev: pointer to IIO device
447 static int tsl2x7x_get_prox(struct iio_dev *indio_dev)
452 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
454 if (mutex_trylock(&chip->prox_mutex) == 0) {
455 dev_err(&chip->client->dev,
456 "%s: Can't get prox mutex\n", __func__);
460 ret = i2c_smbus_read_byte_data(chip->client,
461 TSL2X7X_CMD_REG | TSL2X7X_STATUS);
463 dev_err(&chip->client->dev, "i2c err=%d\n", ret);
473 if (!(ret & TSL2X7X_STA_ADC_VALID))
481 if (!(ret & TSL2X7X_STA_PRX_VALID))
486 for (i = 0; i < 2; i++) {
487 int reg = TSL2X7X_CMD_REG | (TSL2X7X_PRX_LO + i);
489 ret = i2c_smbus_read_byte_data(chip->client, reg);
497 le16_to_cpup((const __le16 *)&chdata[0]);
501 mutex_unlock(&chip->prox_mutex);
503 return chip->prox_data;
507 * tsl2x7x_defaults() - Populates the device nominal operating parameters
508 * with those provided by a 'platform' data struct or
509 * with prefined defaults.
511 * @chip: pointer to device structure.
513 static void tsl2x7x_defaults(struct tsl2X7X_chip *chip)
515 /* If Operational settings defined elsewhere.. */
516 if (chip->pdata && chip->pdata->platform_default_settings)
517 memcpy(&chip->tsl2x7x_settings,
518 chip->pdata->platform_default_settings,
519 sizeof(tsl2x7x_default_settings));
521 memcpy(&chip->tsl2x7x_settings,
522 &tsl2x7x_default_settings,
523 sizeof(tsl2x7x_default_settings));
525 /* Load up the proper lux table. */
526 if (chip->pdata && chip->pdata->platform_lux_table[0].ratio != 0)
527 memcpy(chip->tsl2x7x_device_lux,
528 chip->pdata->platform_lux_table,
529 sizeof(chip->pdata->platform_lux_table));
531 memcpy(chip->tsl2x7x_device_lux,
532 (struct tsl2x7x_lux *)tsl2x7x_default_lux_table_group[chip->id],
533 MAX_DEFAULT_TABLE_BYTES);
537 * tsl2x7x_als_calibrate() - Obtain single reading and calculate
540 * @indio_dev: pointer to IIO device
542 static int tsl2x7x_als_calibrate(struct iio_dev *indio_dev)
544 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
549 ret = i2c_smbus_read_byte_data(chip->client,
550 TSL2X7X_CMD_REG | TSL2X7X_CNTRL);
552 dev_err(&chip->client->dev,
553 "%s: failed to read from the CNTRL register\n",
558 if ((ret & (TSL2X7X_CNTL_ADC_ENBL | TSL2X7X_CNTL_PWR_ON))
559 != (TSL2X7X_CNTL_ADC_ENBL | TSL2X7X_CNTL_PWR_ON)) {
560 dev_err(&chip->client->dev,
561 "%s: Device is not powered on and/or ADC is not enabled\n",
564 } else if ((ret & TSL2X7X_STA_ADC_VALID) != TSL2X7X_STA_ADC_VALID) {
565 dev_err(&chip->client->dev,
566 "%s: The two ADC channels have not completed an integration cycle\n",
571 lux_val = tsl2x7x_get_lux(indio_dev);
573 dev_err(&chip->client->dev,
574 "%s: failed to get lux\n", __func__);
578 gain_trim_val = ((chip->tsl2x7x_settings.als_cal_target)
579 * chip->tsl2x7x_settings.als_gain_trim) / lux_val;
580 if ((gain_trim_val < 250) || (gain_trim_val > 4000))
583 chip->tsl2x7x_settings.als_gain_trim = gain_trim_val;
584 dev_info(&chip->client->dev,
585 "%s als_calibrate completed\n", chip->client->name);
587 return (int)gain_trim_val;
590 static int tsl2x7x_chip_on(struct iio_dev *indio_dev)
598 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
601 if (chip->pdata && chip->pdata->power_on)
602 chip->pdata->power_on(indio_dev);
604 /* Non calculated parameters */
605 chip->tsl2x7x_config[TSL2X7X_PRX_TIME] =
606 chip->tsl2x7x_settings.prx_time;
607 chip->tsl2x7x_config[TSL2X7X_WAIT_TIME] =
608 chip->tsl2x7x_settings.wait_time;
609 chip->tsl2x7x_config[TSL2X7X_PRX_CONFIG] =
610 chip->tsl2x7x_settings.prox_config;
612 chip->tsl2x7x_config[TSL2X7X_ALS_MINTHRESHLO] =
613 (chip->tsl2x7x_settings.als_thresh_low) & 0xFF;
614 chip->tsl2x7x_config[TSL2X7X_ALS_MINTHRESHHI] =
615 (chip->tsl2x7x_settings.als_thresh_low >> 8) & 0xFF;
616 chip->tsl2x7x_config[TSL2X7X_ALS_MAXTHRESHLO] =
617 (chip->tsl2x7x_settings.als_thresh_high) & 0xFF;
618 chip->tsl2x7x_config[TSL2X7X_ALS_MAXTHRESHHI] =
619 (chip->tsl2x7x_settings.als_thresh_high >> 8) & 0xFF;
620 chip->tsl2x7x_config[TSL2X7X_PERSISTENCE] =
621 chip->tsl2x7x_settings.persistence;
623 chip->tsl2x7x_config[TSL2X7X_PRX_COUNT] =
624 chip->tsl2x7x_settings.prox_pulse_count;
625 chip->tsl2x7x_config[TSL2X7X_PRX_MINTHRESHLO] =
626 (chip->tsl2x7x_settings.prox_thres_low) & 0xFF;
627 chip->tsl2x7x_config[TSL2X7X_PRX_MINTHRESHHI] =
628 (chip->tsl2x7x_settings.prox_thres_low >> 8) & 0xFF;
629 chip->tsl2x7x_config[TSL2X7X_PRX_MAXTHRESHLO] =
630 (chip->tsl2x7x_settings.prox_thres_high) & 0xFF;
631 chip->tsl2x7x_config[TSL2X7X_PRX_MAXTHRESHHI] =
632 (chip->tsl2x7x_settings.prox_thres_high >> 8) & 0xFF;
634 /* and make sure we're not already on */
635 if (chip->tsl2x7x_chip_status == TSL2X7X_CHIP_WORKING) {
636 /* if forcing a register update - turn off, then on */
637 dev_info(&chip->client->dev, "device is already enabled\n");
641 /* determine als integration register */
642 als_count = (chip->tsl2x7x_settings.als_time * 100 + 135) / 270;
644 als_count = 1; /* ensure at least one cycle */
646 /* convert back to time (encompasses overrides) */
647 als_time = (als_count * 27 + 5) / 10;
648 chip->tsl2x7x_config[TSL2X7X_ALS_TIME] = 256 - als_count;
650 /* Set the gain based on tsl2x7x_settings struct */
651 chip->tsl2x7x_config[TSL2X7X_GAIN] =
652 chip->tsl2x7x_settings.als_gain |
653 (TSL2X7X_mA100 | TSL2X7X_DIODE1)
654 | ((chip->tsl2x7x_settings.prox_gain) << 2);
656 /* set chip struct re scaling and saturation */
657 chip->als_saturation = als_count * 922; /* 90% of full scale */
658 chip->als_time_scale = (als_time + 25) / 50;
661 * TSL2X7X Specific power-on / adc enable sequence
662 * Power on the device 1st.
664 utmp = TSL2X7X_CNTL_PWR_ON;
665 ret = i2c_smbus_write_byte_data(chip->client,
666 TSL2X7X_CMD_REG | TSL2X7X_CNTRL, utmp);
668 dev_err(&chip->client->dev,
669 "%s: failed on CNTRL reg.\n", __func__);
674 * Use the following shadow copy for our delay before enabling ADC.
675 * Write all the registers.
677 for (i = 0, dev_reg = chip->tsl2x7x_config;
678 i < TSL2X7X_MAX_CONFIG_REG; i++) {
679 ret = i2c_smbus_write_byte_data(chip->client,
683 dev_err(&chip->client->dev,
684 "failed on write to reg %d.\n", i);
689 /* Power-on settling time */
690 usleep_range(3000, 3500);
694 * initialize the desired mode of operation
696 utmp = TSL2X7X_CNTL_PWR_ON |
697 TSL2X7X_CNTL_ADC_ENBL |
698 TSL2X7X_CNTL_PROX_DET_ENBL;
699 ret = i2c_smbus_write_byte_data(chip->client,
700 TSL2X7X_CMD_REG | TSL2X7X_CNTRL, utmp);
702 dev_err(&chip->client->dev,
703 "%s: failed on 2nd CTRL reg.\n", __func__);
707 chip->tsl2x7x_chip_status = TSL2X7X_CHIP_WORKING;
709 if (chip->tsl2x7x_settings.interrupts_en != 0) {
710 dev_info(&chip->client->dev, "Setting Up Interrupt(s)\n");
712 reg_val = TSL2X7X_CNTL_PWR_ON | TSL2X7X_CNTL_ADC_ENBL;
713 if ((chip->tsl2x7x_settings.interrupts_en == 0x20) ||
714 (chip->tsl2x7x_settings.interrupts_en == 0x30))
715 reg_val |= TSL2X7X_CNTL_PROX_DET_ENBL;
717 reg_val |= chip->tsl2x7x_settings.interrupts_en;
718 ret = i2c_smbus_write_byte_data(chip->client,
720 TSL2X7X_CNTRL), reg_val);
722 dev_err(&chip->client->dev,
723 "%s: failed in tsl2x7x_IOCTL_INT_SET.\n",
726 /* Clear out any initial interrupts */
727 ret = i2c_smbus_write_byte(chip->client,
730 TSL2X7X_CMD_PROXALS_INT_CLR);
732 dev_err(&chip->client->dev,
733 "%s: Failed to clear Int status\n",
742 static int tsl2x7x_chip_off(struct iio_dev *indio_dev)
745 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
747 /* turn device off */
748 chip->tsl2x7x_chip_status = TSL2X7X_CHIP_SUSPENDED;
750 ret = i2c_smbus_write_byte_data(chip->client,
751 TSL2X7X_CMD_REG | TSL2X7X_CNTRL, 0x00);
753 if (chip->pdata && chip->pdata->power_off)
754 chip->pdata->power_off(chip->client);
760 * tsl2x7x_invoke_change
761 * @indio_dev: pointer to IIO device
763 * Obtain and lock both ALS and PROX resources,
764 * determine and save device state (On/Off),
765 * cycle device to implement updated parameter,
766 * put device back into proper state, and unlock
770 int tsl2x7x_invoke_change(struct iio_dev *indio_dev)
772 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
773 int device_status = chip->tsl2x7x_chip_status;
776 mutex_lock(&chip->als_mutex);
777 mutex_lock(&chip->prox_mutex);
779 if (device_status == TSL2X7X_CHIP_WORKING) {
780 ret = tsl2x7x_chip_off(indio_dev);
785 ret = tsl2x7x_chip_on(indio_dev);
788 mutex_unlock(&chip->prox_mutex);
789 mutex_unlock(&chip->als_mutex);
795 void tsl2x7x_prox_calculate(int *data, int length,
796 struct tsl2x7x_prox_stat *statP)
806 statP->min = INT_MAX;
807 statP->max = INT_MIN;
808 for (i = 0; i < length; i++) {
809 sample_sum += data[i];
810 statP->min = min(statP->min, data[i]);
811 statP->max = max(statP->max, data[i]);
814 statP->mean = sample_sum / length;
816 for (i = 0; i < length; i++) {
817 tmp = data[i] - statP->mean;
818 sample_sum += tmp * tmp;
820 statP->stddev = int_sqrt((long)sample_sum / length);
824 * tsl2x7x_prox_cal() - Calculates std. and sets thresholds.
825 * @indio_dev: pointer to IIO device
827 * Calculates a standard deviation based on the samples,
828 * and sets the threshold accordingly.
830 static void tsl2x7x_prox_cal(struct iio_dev *indio_dev)
832 int prox_history[MAX_SAMPLES_CAL + 1];
834 struct tsl2x7x_prox_stat prox_stat_data[2];
835 struct tsl2x7x_prox_stat *calP;
836 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
838 u8 current_state = chip->tsl2x7x_chip_status;
840 if (chip->tsl2x7x_settings.prox_max_samples_cal > MAX_SAMPLES_CAL) {
841 dev_err(&chip->client->dev,
842 "max prox samples cal is too big: %d\n",
843 chip->tsl2x7x_settings.prox_max_samples_cal);
844 chip->tsl2x7x_settings.prox_max_samples_cal = MAX_SAMPLES_CAL;
847 /* have to stop to change settings */
848 tsl2x7x_chip_off(indio_dev);
850 /* Enable proximity detection save just in case prox not wanted yet*/
851 tmp_irq_settings = chip->tsl2x7x_settings.interrupts_en;
852 chip->tsl2x7x_settings.interrupts_en |= TSL2X7X_CNTL_PROX_INT_ENBL;
854 /*turn on device if not already on*/
855 tsl2x7x_chip_on(indio_dev);
857 /*gather the samples*/
858 for (i = 0; i < chip->tsl2x7x_settings.prox_max_samples_cal; i++) {
859 usleep_range(15000, 17500);
860 tsl2x7x_get_prox(indio_dev);
861 prox_history[i] = chip->prox_data;
862 dev_info(&chip->client->dev, "2 i=%d prox data= %d\n",
866 tsl2x7x_chip_off(indio_dev);
867 calP = &prox_stat_data[PROX_STAT_CAL];
868 tsl2x7x_prox_calculate(prox_history,
869 chip->tsl2x7x_settings.prox_max_samples_cal,
871 chip->tsl2x7x_settings.prox_thres_high = (calP->max << 1) - calP->mean;
873 dev_info(&chip->client->dev, " cal min=%d mean=%d max=%d\n",
874 calP->min, calP->mean, calP->max);
875 dev_info(&chip->client->dev,
876 "%s proximity threshold set to %d\n",
877 chip->client->name, chip->tsl2x7x_settings.prox_thres_high);
879 /* back to the way they were */
880 chip->tsl2x7x_settings.interrupts_en = tmp_irq_settings;
881 if (current_state == TSL2X7X_CHIP_WORKING)
882 tsl2x7x_chip_on(indio_dev);
885 static ssize_t in_illuminance0_calibscale_available_show(struct device *dev,
886 struct device_attribute *attr,
889 struct tsl2X7X_chip *chip = iio_priv(dev_to_iio_dev(dev));
897 return snprintf(buf, PAGE_SIZE, "%s\n", "1 8 16 128");
900 return snprintf(buf, PAGE_SIZE, "%s\n", "1 8 16 120");
903 static ssize_t in_proximity0_calibscale_available_show(struct device *dev,
904 struct device_attribute *attr,
907 return snprintf(buf, PAGE_SIZE, "%s\n", "1 2 4 8");
910 static ssize_t in_illuminance0_integration_time_show(struct device *dev,
911 struct device_attribute *attr,
914 struct tsl2X7X_chip *chip = iio_priv(dev_to_iio_dev(dev));
917 y = (TSL2X7X_MAX_TIMER_CNT - (u8)chip->tsl2x7x_settings.als_time) + 1;
918 z = y * TSL2X7X_MIN_ITIME;
922 return snprintf(buf, PAGE_SIZE, "%d.%03d\n", y, z);
925 static ssize_t in_illuminance0_integration_time_store(struct device *dev,
926 struct device_attribute *attr,
927 const char *buf, size_t len)
929 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
930 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
931 struct tsl2x7x_parse_result result;
934 ret = iio_str_to_fixpoint(buf, 100, &result.integer, &result.fract);
939 chip->tsl2x7x_settings.als_time =
940 TSL2X7X_MAX_TIMER_CNT - (u8)result.fract;
942 dev_info(&chip->client->dev, "%s: als time = %d",
943 __func__, chip->tsl2x7x_settings.als_time);
945 tsl2x7x_invoke_change(indio_dev);
947 return IIO_VAL_INT_PLUS_MICRO;
950 static IIO_CONST_ATTR(in_illuminance0_integration_time_available,
953 static ssize_t in_illuminance0_target_input_show(struct device *dev,
954 struct device_attribute *attr,
957 struct tsl2X7X_chip *chip = iio_priv(dev_to_iio_dev(dev));
959 return snprintf(buf, PAGE_SIZE, "%d\n",
960 chip->tsl2x7x_settings.als_cal_target);
963 static ssize_t in_illuminance0_target_input_store(struct device *dev,
964 struct device_attribute *attr,
965 const char *buf, size_t len)
967 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
968 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
972 if (kstrtoul(buf, 0, &value))
976 chip->tsl2x7x_settings.als_cal_target = value;
978 ret = tsl2x7x_invoke_change(indio_dev);
985 /* persistence settings */
986 static ssize_t in_intensity0_thresh_period_show(struct device *dev,
987 struct device_attribute *attr,
990 struct tsl2X7X_chip *chip = iio_priv(dev_to_iio_dev(dev));
991 int y, z, filter_delay;
993 /* Determine integration time */
994 y = (TSL2X7X_MAX_TIMER_CNT - (u8)chip->tsl2x7x_settings.als_time) + 1;
995 z = y * TSL2X7X_MIN_ITIME;
996 filter_delay = z * (chip->tsl2x7x_settings.persistence & 0x0F);
997 y = filter_delay / 1000;
998 z = filter_delay % 1000;
1000 return snprintf(buf, PAGE_SIZE, "%d.%03d\n", y, z);
1003 static ssize_t in_intensity0_thresh_period_store(struct device *dev,
1004 struct device_attribute *attr,
1005 const char *buf, size_t len)
1007 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1008 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1009 struct tsl2x7x_parse_result result;
1010 int y, z, filter_delay;
1013 ret = iio_str_to_fixpoint(buf, 100, &result.integer, &result.fract);
1017 y = (TSL2X7X_MAX_TIMER_CNT - (u8)chip->tsl2x7x_settings.als_time) + 1;
1018 z = y * TSL2X7X_MIN_ITIME;
1021 DIV_ROUND_UP((result.integer * 1000) + result.fract, z);
1023 chip->tsl2x7x_settings.persistence &= 0xF0;
1024 chip->tsl2x7x_settings.persistence |= (filter_delay & 0x0F);
1026 dev_info(&chip->client->dev, "%s: als persistence = %d",
1027 __func__, filter_delay);
1029 ret = tsl2x7x_invoke_change(indio_dev);
1033 return IIO_VAL_INT_PLUS_MICRO;
1036 static ssize_t in_proximity0_thresh_period_show(struct device *dev,
1037 struct device_attribute *attr,
1040 struct tsl2X7X_chip *chip = iio_priv(dev_to_iio_dev(dev));
1041 int y, z, filter_delay;
1043 /* Determine integration time */
1044 y = (TSL2X7X_MAX_TIMER_CNT - (u8)chip->tsl2x7x_settings.prx_time) + 1;
1045 z = y * TSL2X7X_MIN_ITIME;
1046 filter_delay = z * ((chip->tsl2x7x_settings.persistence & 0xF0) >> 4);
1047 y = filter_delay / 1000;
1048 z = filter_delay % 1000;
1050 return snprintf(buf, PAGE_SIZE, "%d.%03d\n", y, z);
1053 static ssize_t in_proximity0_thresh_period_store(struct device *dev,
1054 struct device_attribute *attr,
1055 const char *buf, size_t len)
1057 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1058 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1059 struct tsl2x7x_parse_result result;
1060 int y, z, filter_delay;
1063 ret = iio_str_to_fixpoint(buf, 100, &result.integer, &result.fract);
1067 y = (TSL2X7X_MAX_TIMER_CNT - (u8)chip->tsl2x7x_settings.prx_time) + 1;
1068 z = y * TSL2X7X_MIN_ITIME;
1071 DIV_ROUND_UP((result.integer * 1000) + result.fract, z);
1073 chip->tsl2x7x_settings.persistence &= 0x0F;
1074 chip->tsl2x7x_settings.persistence |= ((filter_delay << 4) & 0xF0);
1076 dev_info(&chip->client->dev, "%s: prox persistence = %d",
1077 __func__, filter_delay);
1079 ret = tsl2x7x_invoke_change(indio_dev);
1084 return IIO_VAL_INT_PLUS_MICRO;
1087 static ssize_t in_illuminance0_calibrate_store(struct device *dev,
1088 struct device_attribute *attr,
1089 const char *buf, size_t len)
1091 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1095 if (strtobool(buf, &value))
1099 tsl2x7x_als_calibrate(indio_dev);
1101 ret = tsl2x7x_invoke_change(indio_dev);
1108 static ssize_t in_illuminance0_lux_table_show(struct device *dev,
1109 struct device_attribute *attr,
1112 struct tsl2X7X_chip *chip = iio_priv(dev_to_iio_dev(dev));
1116 while (i < (TSL2X7X_MAX_LUX_TABLE_SIZE * 3)) {
1117 offset += snprintf(buf + offset, PAGE_SIZE, "%u,%u,%u,",
1118 chip->tsl2x7x_device_lux[i].ratio,
1119 chip->tsl2x7x_device_lux[i].ch0,
1120 chip->tsl2x7x_device_lux[i].ch1);
1121 if (chip->tsl2x7x_device_lux[i].ratio == 0) {
1123 * We just printed the first "0" entry.
1124 * Now get rid of the extra "," and break.
1132 offset += snprintf(buf + offset, PAGE_SIZE, "\n");
1136 static ssize_t in_illuminance0_lux_table_store(struct device *dev,
1137 struct device_attribute *attr,
1138 const char *buf, size_t len)
1140 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1141 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1142 int value[ARRAY_SIZE(chip->tsl2x7x_device_lux) * 3 + 1];
1145 get_options(buf, ARRAY_SIZE(value), value);
1147 /* We now have an array of ints starting at value[1], and
1148 * enumerated by value[0].
1149 * We expect each group of three ints is one table entry,
1150 * and the last table entry is all 0.
1153 if ((n % 3) || n < 6 ||
1154 n > ((ARRAY_SIZE(chip->tsl2x7x_device_lux) - 1) * 3)) {
1155 dev_info(dev, "LUX TABLE INPUT ERROR 1 Value[0]=%d\n", n);
1159 if ((value[(n - 2)] | value[(n - 1)] | value[n]) != 0) {
1160 dev_info(dev, "LUX TABLE INPUT ERROR 2 Value[0]=%d\n", n);
1164 if (chip->tsl2x7x_chip_status == TSL2X7X_CHIP_WORKING)
1165 tsl2x7x_chip_off(indio_dev);
1167 /* Zero out the table */
1168 memset(chip->tsl2x7x_device_lux, 0, sizeof(chip->tsl2x7x_device_lux));
1169 memcpy(chip->tsl2x7x_device_lux, &value[1], (value[0] * 4));
1171 ret = tsl2x7x_invoke_change(indio_dev);
1178 static ssize_t in_proximity0_calibrate_store(struct device *dev,
1179 struct device_attribute *attr,
1180 const char *buf, size_t len)
1182 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1186 if (strtobool(buf, &value))
1190 tsl2x7x_prox_cal(indio_dev);
1192 ret = tsl2x7x_invoke_change(indio_dev);
1199 static int tsl2x7x_read_interrupt_config(struct iio_dev *indio_dev,
1200 const struct iio_chan_spec *chan,
1201 enum iio_event_type type,
1202 enum iio_event_direction dir)
1204 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1207 if (chan->type == IIO_INTENSITY)
1208 ret = !!(chip->tsl2x7x_settings.interrupts_en & 0x10);
1210 ret = !!(chip->tsl2x7x_settings.interrupts_en & 0x20);
1215 static int tsl2x7x_write_interrupt_config(struct iio_dev *indio_dev,
1216 const struct iio_chan_spec *chan,
1217 enum iio_event_type type,
1218 enum iio_event_direction dir,
1221 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1224 if (chan->type == IIO_INTENSITY) {
1226 chip->tsl2x7x_settings.interrupts_en |= 0x10;
1228 chip->tsl2x7x_settings.interrupts_en &= 0x20;
1231 chip->tsl2x7x_settings.interrupts_en |= 0x20;
1233 chip->tsl2x7x_settings.interrupts_en &= 0x10;
1236 ret = tsl2x7x_invoke_change(indio_dev);
1243 static int tsl2x7x_write_event_value(struct iio_dev *indio_dev,
1244 const struct iio_chan_spec *chan,
1245 enum iio_event_type type,
1246 enum iio_event_direction dir,
1247 enum iio_event_info info,
1250 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1254 case IIO_EV_INFO_VALUE:
1255 if (chan->type == IIO_INTENSITY) {
1257 case IIO_EV_DIR_RISING:
1258 chip->tsl2x7x_settings.als_thresh_high = val;
1261 case IIO_EV_DIR_FALLING:
1262 chip->tsl2x7x_settings.als_thresh_low = val;
1270 case IIO_EV_DIR_RISING:
1271 chip->tsl2x7x_settings.prox_thres_high = val;
1274 case IIO_EV_DIR_FALLING:
1275 chip->tsl2x7x_settings.prox_thres_low = val;
1290 return tsl2x7x_invoke_change(indio_dev);
1293 static int tsl2x7x_read_event_value(struct iio_dev *indio_dev,
1294 const struct iio_chan_spec *chan,
1295 enum iio_event_type type,
1296 enum iio_event_direction dir,
1297 enum iio_event_info info,
1298 int *val, int *val2)
1300 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1304 case IIO_EV_INFO_VALUE:
1305 if (chan->type == IIO_INTENSITY) {
1307 case IIO_EV_DIR_RISING:
1308 *val = chip->tsl2x7x_settings.als_thresh_high;
1311 case IIO_EV_DIR_FALLING:
1312 *val = chip->tsl2x7x_settings.als_thresh_low;
1320 case IIO_EV_DIR_RISING:
1321 *val = chip->tsl2x7x_settings.prox_thres_high;
1324 case IIO_EV_DIR_FALLING:
1325 *val = chip->tsl2x7x_settings.prox_thres_low;
1340 static int tsl2x7x_read_raw(struct iio_dev *indio_dev,
1341 struct iio_chan_spec const *chan,
1347 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1350 case IIO_CHAN_INFO_PROCESSED:
1351 switch (chan->type) {
1353 tsl2x7x_get_lux(indio_dev);
1354 *val = chip->als_cur_info.lux;
1361 case IIO_CHAN_INFO_RAW:
1362 switch (chan->type) {
1364 tsl2x7x_get_lux(indio_dev);
1365 if (chan->channel == 0)
1366 *val = chip->als_cur_info.als_ch0;
1368 *val = chip->als_cur_info.als_ch1;
1372 tsl2x7x_get_prox(indio_dev);
1373 *val = chip->prox_data;
1380 case IIO_CHAN_INFO_CALIBSCALE:
1381 if (chan->type == IIO_LIGHT)
1383 tsl2X7X_als_gainadj[chip->tsl2x7x_settings.als_gain];
1386 tsl2X7X_prx_gainadj[chip->tsl2x7x_settings.prox_gain];
1389 case IIO_CHAN_INFO_CALIBBIAS:
1390 *val = chip->tsl2x7x_settings.als_gain_trim;
1401 static int tsl2x7x_write_raw(struct iio_dev *indio_dev,
1402 struct iio_chan_spec const *chan,
1407 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1410 case IIO_CHAN_INFO_CALIBSCALE:
1411 if (chan->type == IIO_INTENSITY) {
1414 chip->tsl2x7x_settings.als_gain = 0;
1417 chip->tsl2x7x_settings.als_gain = 1;
1420 chip->tsl2x7x_settings.als_gain = 2;
1431 chip->tsl2x7x_settings.als_gain = 3;
1442 chip->tsl2x7x_settings.als_gain = 3;
1450 chip->tsl2x7x_settings.prox_gain = 0;
1453 chip->tsl2x7x_settings.prox_gain = 1;
1456 chip->tsl2x7x_settings.prox_gain = 2;
1459 chip->tsl2x7x_settings.prox_gain = 3;
1466 case IIO_CHAN_INFO_CALIBBIAS:
1467 chip->tsl2x7x_settings.als_gain_trim = val;
1474 return tsl2x7x_invoke_change(indio_dev);
1477 static DEVICE_ATTR_RO(in_proximity0_calibscale_available);
1479 static DEVICE_ATTR_RO(in_illuminance0_calibscale_available);
1481 static DEVICE_ATTR_RW(in_illuminance0_integration_time);
1483 static DEVICE_ATTR_RW(in_illuminance0_target_input);
1485 static DEVICE_ATTR_WO(in_illuminance0_calibrate);
1487 static DEVICE_ATTR_WO(in_proximity0_calibrate);
1489 static DEVICE_ATTR_RW(in_illuminance0_lux_table);
1491 static DEVICE_ATTR_RW(in_intensity0_thresh_period);
1493 static DEVICE_ATTR_RW(in_proximity0_thresh_period);
1495 /* Use the default register values to identify the Taos device */
1496 static int tsl2x7x_device_id(int *id, int target)
1502 return (*id & 0xf0) == TRITON_ID;
1505 return (*id & 0xf0) == HALIBUT_ID;
1511 return (*id & 0xf0) == SWORDFISH_ID;
1517 static irqreturn_t tsl2x7x_event_handler(int irq, void *private)
1519 struct iio_dev *indio_dev = private;
1520 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1521 s64 timestamp = iio_get_time_ns(indio_dev);
1525 value = i2c_smbus_read_byte_data(chip->client,
1526 TSL2X7X_CMD_REG | TSL2X7X_STATUS);
1528 /* What type of interrupt do we need to process */
1529 if (value & TSL2X7X_STA_PRX_INTR) {
1530 tsl2x7x_get_prox(indio_dev); /* freshen data for ABI */
1531 iio_push_event(indio_dev,
1532 IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY,
1539 if (value & TSL2X7X_STA_ALS_INTR) {
1540 tsl2x7x_get_lux(indio_dev); /* freshen data for ABI */
1541 iio_push_event(indio_dev,
1542 IIO_UNMOD_EVENT_CODE(IIO_LIGHT,
1548 /* Clear interrupt now that we have handled it. */
1549 ret = i2c_smbus_write_byte(chip->client,
1550 TSL2X7X_CMD_REG | TSL2X7X_CMD_SPL_FN |
1551 TSL2X7X_CMD_PROXALS_INT_CLR);
1553 dev_err(&chip->client->dev,
1554 "Failed to clear irq from event handler. err = %d\n",
1560 static struct attribute *tsl2x7x_ALS_device_attrs[] = {
1561 &dev_attr_in_illuminance0_calibscale_available.attr,
1562 &dev_attr_in_illuminance0_integration_time.attr,
1563 &iio_const_attr_in_illuminance0_integration_time_available.dev_attr.attr,
1564 &dev_attr_in_illuminance0_target_input.attr,
1565 &dev_attr_in_illuminance0_calibrate.attr,
1566 &dev_attr_in_illuminance0_lux_table.attr,
1570 static struct attribute *tsl2x7x_PRX_device_attrs[] = {
1571 &dev_attr_in_proximity0_calibrate.attr,
1575 static struct attribute *tsl2x7x_ALSPRX_device_attrs[] = {
1576 &dev_attr_in_illuminance0_calibscale_available.attr,
1577 &dev_attr_in_illuminance0_integration_time.attr,
1578 &iio_const_attr_in_illuminance0_integration_time_available.dev_attr.attr,
1579 &dev_attr_in_illuminance0_target_input.attr,
1580 &dev_attr_in_illuminance0_calibrate.attr,
1581 &dev_attr_in_illuminance0_lux_table.attr,
1582 &dev_attr_in_proximity0_calibrate.attr,
1586 static struct attribute *tsl2x7x_PRX2_device_attrs[] = {
1587 &dev_attr_in_proximity0_calibrate.attr,
1588 &dev_attr_in_proximity0_calibscale_available.attr,
1592 static struct attribute *tsl2x7x_ALSPRX2_device_attrs[] = {
1593 &dev_attr_in_illuminance0_calibscale_available.attr,
1594 &dev_attr_in_illuminance0_integration_time.attr,
1595 &iio_const_attr_in_illuminance0_integration_time_available.dev_attr.attr,
1596 &dev_attr_in_illuminance0_target_input.attr,
1597 &dev_attr_in_illuminance0_calibrate.attr,
1598 &dev_attr_in_illuminance0_lux_table.attr,
1599 &dev_attr_in_proximity0_calibrate.attr,
1600 &dev_attr_in_proximity0_calibscale_available.attr,
1604 static struct attribute *tsl2X7X_ALS_event_attrs[] = {
1605 &dev_attr_in_intensity0_thresh_period.attr,
1609 static struct attribute *tsl2X7X_PRX_event_attrs[] = {
1610 &dev_attr_in_proximity0_thresh_period.attr,
1614 static struct attribute *tsl2X7X_ALSPRX_event_attrs[] = {
1615 &dev_attr_in_intensity0_thresh_period.attr,
1616 &dev_attr_in_proximity0_thresh_period.attr,
1620 static const struct attribute_group tsl2X7X_device_attr_group_tbl[] = {
1622 .attrs = tsl2x7x_ALS_device_attrs,
1625 .attrs = tsl2x7x_PRX_device_attrs,
1628 .attrs = tsl2x7x_ALSPRX_device_attrs,
1631 .attrs = tsl2x7x_PRX2_device_attrs,
1634 .attrs = tsl2x7x_ALSPRX2_device_attrs,
1638 static const struct attribute_group tsl2X7X_event_attr_group_tbl[] = {
1640 .attrs = tsl2X7X_ALS_event_attrs,
1644 .attrs = tsl2X7X_PRX_event_attrs,
1648 .attrs = tsl2X7X_ALSPRX_event_attrs,
1653 static const struct iio_info tsl2X7X_device_info[] = {
1655 .attrs = &tsl2X7X_device_attr_group_tbl[ALS],
1656 .event_attrs = &tsl2X7X_event_attr_group_tbl[ALS],
1657 .driver_module = THIS_MODULE,
1658 .read_raw = &tsl2x7x_read_raw,
1659 .write_raw = &tsl2x7x_write_raw,
1660 .read_event_value = &tsl2x7x_read_event_value,
1661 .write_event_value = &tsl2x7x_write_event_value,
1662 .read_event_config = &tsl2x7x_read_interrupt_config,
1663 .write_event_config = &tsl2x7x_write_interrupt_config,
1666 .attrs = &tsl2X7X_device_attr_group_tbl[PRX],
1667 .event_attrs = &tsl2X7X_event_attr_group_tbl[PRX],
1668 .driver_module = THIS_MODULE,
1669 .read_raw = &tsl2x7x_read_raw,
1670 .write_raw = &tsl2x7x_write_raw,
1671 .read_event_value = &tsl2x7x_read_event_value,
1672 .write_event_value = &tsl2x7x_write_event_value,
1673 .read_event_config = &tsl2x7x_read_interrupt_config,
1674 .write_event_config = &tsl2x7x_write_interrupt_config,
1677 .attrs = &tsl2X7X_device_attr_group_tbl[ALSPRX],
1678 .event_attrs = &tsl2X7X_event_attr_group_tbl[ALSPRX],
1679 .driver_module = THIS_MODULE,
1680 .read_raw = &tsl2x7x_read_raw,
1681 .write_raw = &tsl2x7x_write_raw,
1682 .read_event_value = &tsl2x7x_read_event_value,
1683 .write_event_value = &tsl2x7x_write_event_value,
1684 .read_event_config = &tsl2x7x_read_interrupt_config,
1685 .write_event_config = &tsl2x7x_write_interrupt_config,
1688 .attrs = &tsl2X7X_device_attr_group_tbl[PRX2],
1689 .event_attrs = &tsl2X7X_event_attr_group_tbl[PRX],
1690 .driver_module = THIS_MODULE,
1691 .read_raw = &tsl2x7x_read_raw,
1692 .write_raw = &tsl2x7x_write_raw,
1693 .read_event_value = &tsl2x7x_read_event_value,
1694 .write_event_value = &tsl2x7x_write_event_value,
1695 .read_event_config = &tsl2x7x_read_interrupt_config,
1696 .write_event_config = &tsl2x7x_write_interrupt_config,
1699 .attrs = &tsl2X7X_device_attr_group_tbl[ALSPRX2],
1700 .event_attrs = &tsl2X7X_event_attr_group_tbl[ALSPRX],
1701 .driver_module = THIS_MODULE,
1702 .read_raw = &tsl2x7x_read_raw,
1703 .write_raw = &tsl2x7x_write_raw,
1704 .read_event_value = &tsl2x7x_read_event_value,
1705 .write_event_value = &tsl2x7x_write_event_value,
1706 .read_event_config = &tsl2x7x_read_interrupt_config,
1707 .write_event_config = &tsl2x7x_write_interrupt_config,
1711 static const struct iio_event_spec tsl2x7x_events[] = {
1713 .type = IIO_EV_TYPE_THRESH,
1714 .dir = IIO_EV_DIR_RISING,
1715 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
1716 BIT(IIO_EV_INFO_ENABLE),
1718 .type = IIO_EV_TYPE_THRESH,
1719 .dir = IIO_EV_DIR_FALLING,
1720 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
1721 BIT(IIO_EV_INFO_ENABLE),
1725 static const struct tsl2x7x_chip_info tsl2x7x_chip_info_tbl[] = {
1732 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1734 .type = IIO_INTENSITY,
1737 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1738 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1739 BIT(IIO_CHAN_INFO_CALIBBIAS),
1740 .event_spec = tsl2x7x_events,
1741 .num_event_specs = ARRAY_SIZE(tsl2x7x_events),
1743 .type = IIO_INTENSITY,
1748 .chan_table_elements = 3,
1749 .info = &tsl2X7X_device_info[ALS],
1754 .type = IIO_PROXIMITY,
1757 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1758 .event_spec = tsl2x7x_events,
1759 .num_event_specs = ARRAY_SIZE(tsl2x7x_events),
1762 .chan_table_elements = 1,
1763 .info = &tsl2X7X_device_info[PRX],
1771 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED)
1773 .type = IIO_INTENSITY,
1776 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1777 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1778 BIT(IIO_CHAN_INFO_CALIBBIAS),
1779 .event_spec = tsl2x7x_events,
1780 .num_event_specs = ARRAY_SIZE(tsl2x7x_events),
1782 .type = IIO_INTENSITY,
1785 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1787 .type = IIO_PROXIMITY,
1790 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1791 .event_spec = tsl2x7x_events,
1792 .num_event_specs = ARRAY_SIZE(tsl2x7x_events),
1795 .chan_table_elements = 4,
1796 .info = &tsl2X7X_device_info[ALSPRX],
1801 .type = IIO_PROXIMITY,
1804 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1805 BIT(IIO_CHAN_INFO_CALIBSCALE),
1806 .event_spec = tsl2x7x_events,
1807 .num_event_specs = ARRAY_SIZE(tsl2x7x_events),
1810 .chan_table_elements = 1,
1811 .info = &tsl2X7X_device_info[PRX2],
1819 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1821 .type = IIO_INTENSITY,
1824 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1825 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1826 BIT(IIO_CHAN_INFO_CALIBBIAS),
1827 .event_spec = tsl2x7x_events,
1828 .num_event_specs = ARRAY_SIZE(tsl2x7x_events),
1830 .type = IIO_INTENSITY,
1833 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1835 .type = IIO_PROXIMITY,
1838 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1839 BIT(IIO_CHAN_INFO_CALIBSCALE),
1840 .event_spec = tsl2x7x_events,
1841 .num_event_specs = ARRAY_SIZE(tsl2x7x_events),
1844 .chan_table_elements = 4,
1845 .info = &tsl2X7X_device_info[ALSPRX2],
1849 static int tsl2x7x_probe(struct i2c_client *clientp,
1850 const struct i2c_device_id *id)
1853 struct iio_dev *indio_dev;
1854 struct tsl2X7X_chip *chip;
1856 indio_dev = devm_iio_device_alloc(&clientp->dev, sizeof(*chip));
1860 chip = iio_priv(indio_dev);
1861 chip->client = clientp;
1862 i2c_set_clientdata(clientp, indio_dev);
1864 ret = i2c_smbus_read_byte_data(chip->client,
1865 TSL2X7X_CMD_REG | TSL2X7X_CHIPID);
1869 if ((!tsl2x7x_device_id(&ret, id->driver_data)) ||
1870 (tsl2x7x_device_id(&ret, id->driver_data) == -EINVAL)) {
1871 dev_info(&chip->client->dev,
1872 "%s: i2c device found does not match expected id\n",
1877 ret = i2c_smbus_write_byte(clientp, (TSL2X7X_CMD_REG | TSL2X7X_CNTRL));
1879 dev_err(&clientp->dev, "write to cmd reg failed. err = %d\n",
1885 * ALS and PROX functions can be invoked via user space poll
1886 * or H/W interrupt. If busy return last sample.
1888 mutex_init(&chip->als_mutex);
1889 mutex_init(&chip->prox_mutex);
1891 chip->tsl2x7x_chip_status = TSL2X7X_CHIP_UNKNOWN;
1892 chip->pdata = dev_get_platdata(&clientp->dev);
1893 chip->id = id->driver_data;
1895 &tsl2x7x_chip_info_tbl[device_channel_config[id->driver_data]];
1897 indio_dev->info = chip->chip_info->info;
1898 indio_dev->dev.parent = &clientp->dev;
1899 indio_dev->modes = INDIO_DIRECT_MODE;
1900 indio_dev->name = chip->client->name;
1901 indio_dev->channels = chip->chip_info->channel;
1902 indio_dev->num_channels = chip->chip_info->chan_table_elements;
1905 ret = devm_request_threaded_irq(&clientp->dev, clientp->irq,
1907 &tsl2x7x_event_handler,
1908 IRQF_TRIGGER_RISING |
1913 dev_err(&clientp->dev,
1914 "%s: irq request failed", __func__);
1919 /* Load up the defaults */
1920 tsl2x7x_defaults(chip);
1921 /* Make sure the chip is on */
1922 tsl2x7x_chip_on(indio_dev);
1924 ret = iio_device_register(indio_dev);
1926 dev_err(&clientp->dev,
1927 "%s: iio registration failed\n", __func__);
1931 dev_info(&clientp->dev, "%s Light sensor found.\n", id->name);
1936 static int tsl2x7x_suspend(struct device *dev)
1938 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1939 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1942 if (chip->tsl2x7x_chip_status == TSL2X7X_CHIP_WORKING) {
1943 ret = tsl2x7x_chip_off(indio_dev);
1944 chip->tsl2x7x_chip_status = TSL2X7X_CHIP_SUSPENDED;
1947 if (chip->pdata && chip->pdata->platform_power) {
1948 pm_message_t pmm = {PM_EVENT_SUSPEND};
1950 chip->pdata->platform_power(dev, pmm);
1956 static int tsl2x7x_resume(struct device *dev)
1958 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1959 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1962 if (chip->pdata && chip->pdata->platform_power) {
1963 pm_message_t pmm = {PM_EVENT_RESUME};
1965 chip->pdata->platform_power(dev, pmm);
1968 if (chip->tsl2x7x_chip_status == TSL2X7X_CHIP_SUSPENDED)
1969 ret = tsl2x7x_chip_on(indio_dev);
1974 static int tsl2x7x_remove(struct i2c_client *client)
1976 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1978 tsl2x7x_chip_off(indio_dev);
1980 iio_device_unregister(indio_dev);
1985 static struct i2c_device_id tsl2x7x_idtable[] = {
1986 { "tsl2571", tsl2571 },
1987 { "tsl2671", tsl2671 },
1988 { "tmd2671", tmd2671 },
1989 { "tsl2771", tsl2771 },
1990 { "tmd2771", tmd2771 },
1991 { "tsl2572", tsl2572 },
1992 { "tsl2672", tsl2672 },
1993 { "tmd2672", tmd2672 },
1994 { "tsl2772", tsl2772 },
1995 { "tmd2772", tmd2772 },
1999 MODULE_DEVICE_TABLE(i2c, tsl2x7x_idtable);
2001 static const struct of_device_id tsl2x7x_of_match[] = {
2002 { .compatible = "amstaos,tsl2571" },
2003 { .compatible = "amstaos,tsl2671" },
2004 { .compatible = "amstaos,tmd2671" },
2005 { .compatible = "amstaos,tsl2771" },
2006 { .compatible = "amstaos,tmd2771" },
2007 { .compatible = "amstaos,tsl2572" },
2008 { .compatible = "amstaos,tsl2672" },
2009 { .compatible = "amstaos,tmd2672" },
2010 { .compatible = "amstaos,tsl2772" },
2011 { .compatible = "amstaos,tmd2772" },
2014 MODULE_DEVICE_TABLE(of, tsl2x7x_of_match);
2016 static const struct dev_pm_ops tsl2x7x_pm_ops = {
2017 .suspend = tsl2x7x_suspend,
2018 .resume = tsl2x7x_resume,
2021 /* Driver definition */
2022 static struct i2c_driver tsl2x7x_driver = {
2025 .of_match_table = tsl2x7x_of_match,
2026 .pm = &tsl2x7x_pm_ops,
2028 .id_table = tsl2x7x_idtable,
2029 .probe = tsl2x7x_probe,
2030 .remove = tsl2x7x_remove,
2033 module_i2c_driver(tsl2x7x_driver);
2035 MODULE_AUTHOR("J. August Brenner<jbrenner@taosinc.com>");
2036 MODULE_DESCRIPTION("TAOS tsl2x7x ambient and proximity light sensor driver");
2037 MODULE_LICENSE("GPL");