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/iio/events.h>
31 #include <linux/iio/iio.h>
32 #include <linux/iio/sysfs.h>
36 #define PROX_STAT_CAL 0
37 #define PROX_STAT_SAMP 1
38 #define MAX_SAMPLES_CAL 200
40 /* TSL2X7X Device ID */
41 #define TRITON_ID 0x00
42 #define SWORDFISH_ID 0x30
43 #define HALIBUT_ID 0x20
45 /* Lux calculation constants */
46 #define TSL2X7X_LUX_CALC_OVER_FLOW 65535
48 /* TAOS Register definitions - note:
49 * depending on device, some of these register are not used and the
50 * register address is benign.
52 /* 2X7X register offsets */
53 #define TSL2X7X_MAX_CONFIG_REG 16
55 /* Device Registers and Masks */
56 #define TSL2X7X_CNTRL 0x00
57 #define TSL2X7X_ALS_TIME 0X01
58 #define TSL2X7X_PRX_TIME 0x02
59 #define TSL2X7X_WAIT_TIME 0x03
60 #define TSL2X7X_ALS_MINTHRESHLO 0X04
61 #define TSL2X7X_ALS_MINTHRESHHI 0X05
62 #define TSL2X7X_ALS_MAXTHRESHLO 0X06
63 #define TSL2X7X_ALS_MAXTHRESHHI 0X07
64 #define TSL2X7X_PRX_MINTHRESHLO 0X08
65 #define TSL2X7X_PRX_MINTHRESHHI 0X09
66 #define TSL2X7X_PRX_MAXTHRESHLO 0X0A
67 #define TSL2X7X_PRX_MAXTHRESHHI 0X0B
68 #define TSL2X7X_PERSISTENCE 0x0C
69 #define TSL2X7X_PRX_CONFIG 0x0D
70 #define TSL2X7X_PRX_COUNT 0x0E
71 #define TSL2X7X_GAIN 0x0F
72 #define TSL2X7X_NOTUSED 0x10
73 #define TSL2X7X_REVID 0x11
74 #define TSL2X7X_CHIPID 0x12
75 #define TSL2X7X_STATUS 0x13
76 #define TSL2X7X_ALS_CHAN0LO 0x14
77 #define TSL2X7X_ALS_CHAN0HI 0x15
78 #define TSL2X7X_ALS_CHAN1LO 0x16
79 #define TSL2X7X_ALS_CHAN1HI 0x17
80 #define TSL2X7X_PRX_LO 0x18
81 #define TSL2X7X_PRX_HI 0x19
83 /* tsl2X7X cmd reg masks */
84 #define TSL2X7X_CMD_REG 0x80
85 #define TSL2X7X_CMD_SPL_FN 0x60
87 #define TSL2X7X_CMD_PROX_INT_CLR 0X05
88 #define TSL2X7X_CMD_ALS_INT_CLR 0x06
89 #define TSL2X7X_CMD_PROXALS_INT_CLR 0X07
91 /* tsl2X7X cntrl reg masks */
92 #define TSL2X7X_CNTL_ADC_ENBL 0x02
93 #define TSL2X7X_CNTL_PWR_ON 0x01
95 /* tsl2X7X status reg masks */
96 #define TSL2X7X_STA_ADC_VALID 0x01
97 #define TSL2X7X_STA_PRX_VALID 0x02
98 #define TSL2X7X_STA_ADC_PRX_VALID (TSL2X7X_STA_ADC_VALID |\
99 TSL2X7X_STA_PRX_VALID)
100 #define TSL2X7X_STA_ALS_INTR 0x10
101 #define TSL2X7X_STA_PRX_INTR 0x20
103 /* tsl2X7X cntrl reg masks */
104 #define TSL2X7X_CNTL_REG_CLEAR 0x00
105 #define TSL2X7X_CNTL_PROX_INT_ENBL 0X20
106 #define TSL2X7X_CNTL_ALS_INT_ENBL 0X10
107 #define TSL2X7X_CNTL_WAIT_TMR_ENBL 0X08
108 #define TSL2X7X_CNTL_PROX_DET_ENBL 0X04
109 #define TSL2X7X_CNTL_PWRON 0x01
110 #define TSL2X7X_CNTL_ALSPON_ENBL 0x03
111 #define TSL2X7X_CNTL_INTALSPON_ENBL 0x13
112 #define TSL2X7X_CNTL_PROXPON_ENBL 0x0F
113 #define TSL2X7X_CNTL_INTPROXPON_ENBL 0x2F
115 /*Prox diode to use */
116 #define TSL2X7X_DIODE0 0x10
117 #define TSL2X7X_DIODE1 0x20
118 #define TSL2X7X_DIODE_BOTH 0x30
121 #define TSL2X7X_mA100 0x00
122 #define TSL2X7X_mA50 0x40
123 #define TSL2X7X_mA25 0x80
124 #define TSL2X7X_mA13 0xD0
125 #define TSL2X7X_MAX_TIMER_CNT (0xFF)
127 /*Common device IIO EventMask */
128 #define TSL2X7X_EVENT_MASK \
129 (IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING) | \
130 IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING)),
132 #define TSL2X7X_MIN_ITIME 3
134 /* TAOS txx2x7x Device family members */
149 TSL2X7X_CHIP_UNKNOWN
= 0,
150 TSL2X7X_CHIP_WORKING
= 1,
151 TSL2X7X_CHIP_SUSPENDED
= 2
154 struct tsl2x7x_parse_result
{
159 /* Per-device data */
160 struct tsl2x7x_als_info
{
166 struct tsl2x7x_prox_stat
{
170 unsigned long stddev
;
173 struct tsl2x7x_chip_info
{
174 int chan_table_elements
;
175 struct iio_chan_spec channel
[4];
176 const struct iio_info
*info
;
179 struct tsl2X7X_chip
{
181 struct mutex prox_mutex
;
182 struct mutex als_mutex
;
183 struct i2c_client
*client
;
185 struct tsl2x7x_als_info als_cur_info
;
186 struct tsl2x7x_settings tsl2x7x_settings
;
187 struct tsl2X7X_platform_data
*pdata
;
190 int tsl2x7x_chip_status
;
191 u8 tsl2x7x_config
[TSL2X7X_MAX_CONFIG_REG
];
192 const struct tsl2x7x_chip_info
*chip_info
;
193 const struct iio_info
*info
;
195 /* This structure is intentionally large to accommodate
196 * updates via sysfs. */
197 /* Sized to 9 = max 8 segments + 1 termination segment */
198 struct tsl2x7x_lux tsl2x7x_device_lux
[TSL2X7X_MAX_LUX_TABLE_SIZE
];
201 /* Different devices require different coefficents */
202 static const struct tsl2x7x_lux tsl2x71_lux_table
[] = {
203 { 14461, 611, 1211 },
208 static const struct tsl2x7x_lux tmd2x71_lux_table
[] = {
214 static const struct tsl2x7x_lux tsl2x72_lux_table
[] = {
220 static const struct tsl2x7x_lux tmd2x72_lux_table
[] = {
226 static const struct tsl2x7x_lux
*tsl2x7x_default_lux_table_group
[] = {
227 [tsl2571
] = tsl2x71_lux_table
,
228 [tsl2671
] = tsl2x71_lux_table
,
229 [tmd2671
] = tmd2x71_lux_table
,
230 [tsl2771
] = tsl2x71_lux_table
,
231 [tmd2771
] = tmd2x71_lux_table
,
232 [tsl2572
] = tsl2x72_lux_table
,
233 [tsl2672
] = tsl2x72_lux_table
,
234 [tmd2672
] = tmd2x72_lux_table
,
235 [tsl2772
] = tsl2x72_lux_table
,
236 [tmd2772
] = tmd2x72_lux_table
,
239 static const struct tsl2x7x_settings tsl2x7x_default_settings
= {
240 .als_time
= 219, /* 101 ms */
242 .prx_time
= 254, /* 5.4 ms */
246 .als_gain_trim
= 1000,
247 .als_cal_target
= 150,
248 .als_thresh_low
= 200,
249 .als_thresh_high
= 256,
253 .prox_thres_high
= 512,
254 .prox_max_samples_cal
= 30,
255 .prox_pulse_count
= 8
258 static const s16 tsl2X7X_als_gainadj
[] = {
265 static const s16 tsl2X7X_prx_gainadj
[] = {
272 /* Channel variations */
281 static const u8 device_channel_config
[] = {
295 * tsl2x7x_i2c_read() - Read a byte from a register.
296 * @client: i2c client
297 * @reg: device register to read from
298 * @*val: pointer to location to store register contents.
302 tsl2x7x_i2c_read(struct i2c_client
*client
, u8 reg
, u8
*val
)
306 /* select register to write */
307 ret
= i2c_smbus_write_byte(client
, (TSL2X7X_CMD_REG
| reg
));
309 dev_err(&client
->dev
, "%s: failed to write register %x\n"
315 ret
= i2c_smbus_read_byte(client
);
319 dev_err(&client
->dev
, "%s: failed to read register %x\n"
326 * tsl2x7x_get_lux() - Reads and calculates current lux value.
327 * @indio_dev: pointer to IIO device
329 * The raw ch0 and ch1 values of the ambient light sensed in the last
330 * integration cycle are read from the device.
331 * Time scale factor array values are adjusted based on the integration time.
332 * The raw values are multiplied by a scale factor, and device gain is obtained
333 * using gain index. Limit checks are done next, then the ratio of a multiple
334 * of ch1 value, to the ch0 value, is calculated. Array tsl2x7x_device_lux[]
335 * is then scanned to find the first ratio value that is just above the ratio
336 * we just calculated. The ch0 and ch1 multiplier constants in the array are
337 * then used along with the time scale factor array values, to calculate the
340 static int tsl2x7x_get_lux(struct iio_dev
*indio_dev
)
342 u16 ch0
, ch1
; /* separated ch0/ch1 data from device */
343 u32 lux
; /* raw lux calculated from device data */
347 struct tsl2x7x_lux
*p
;
348 struct tsl2X7X_chip
*chip
= iio_priv(indio_dev
);
353 if (mutex_trylock(&chip
->als_mutex
) == 0)
354 return chip
->als_cur_info
.lux
; /* busy, so return LAST VALUE */
356 if (chip
->tsl2x7x_chip_status
!= TSL2X7X_CHIP_WORKING
) {
357 /* device is not enabled */
358 dev_err(&chip
->client
->dev
, "%s: device is not enabled\n",
364 ret
= tsl2x7x_i2c_read(chip
->client
,
365 (TSL2X7X_CMD_REG
| TSL2X7X_STATUS
), &buf
[0]);
367 dev_err(&chip
->client
->dev
,
368 "%s: Failed to read STATUS Reg\n", __func__
);
371 /* is data new & valid */
372 if (!(buf
[0] & TSL2X7X_STA_ADC_VALID
)) {
373 dev_err(&chip
->client
->dev
,
374 "%s: data not valid yet\n", __func__
);
375 ret
= chip
->als_cur_info
.lux
; /* return LAST VALUE */
379 for (i
= 0; i
< 4; i
++) {
380 ret
= tsl2x7x_i2c_read(chip
->client
,
381 (TSL2X7X_CMD_REG
| (TSL2X7X_ALS_CHAN0LO
+ i
)),
384 dev_err(&chip
->client
->dev
,
385 "%s: failed to read. err=%x\n", __func__
, ret
);
390 /* clear any existing interrupt status */
391 ret
= i2c_smbus_write_byte(chip
->client
,
394 TSL2X7X_CMD_ALS_INT_CLR
));
396 dev_err(&chip
->client
->dev
,
397 "%s: i2c_write_command failed - err = %d\n",
399 goto out_unlock
; /* have no data, so return failure */
402 /* extract ALS/lux data */
403 ch0
= le16_to_cpup((const __le16
*)&buf
[0]);
404 ch1
= le16_to_cpup((const __le16
*)&buf
[2]);
406 chip
->als_cur_info
.als_ch0
= ch0
;
407 chip
->als_cur_info
.als_ch1
= ch1
;
409 if ((ch0
>= chip
->als_saturation
) || (ch1
>= chip
->als_saturation
)) {
410 lux
= TSL2X7X_LUX_CALC_OVER_FLOW
;
415 /* have no data, so return LAST VALUE */
416 ret
= chip
->als_cur_info
.lux
;
419 /* calculate ratio */
420 ratio
= (ch1
<< 15) / ch0
;
421 /* convert to unscaled lux using the pointer to the table */
422 p
= (struct tsl2x7x_lux
*) chip
->tsl2x7x_device_lux
;
423 while (p
->ratio
!= 0 && p
->ratio
< ratio
)
429 ch0lux
= DIV_ROUND_UP((ch0
* p
->ch0
),
430 tsl2X7X_als_gainadj
[chip
->tsl2x7x_settings
.als_gain
]);
431 ch1lux
= DIV_ROUND_UP((ch1
* p
->ch1
),
432 tsl2X7X_als_gainadj
[chip
->tsl2x7x_settings
.als_gain
]);
433 lux
= ch0lux
- ch1lux
;
436 /* note: lux is 31 bit max at this point */
437 if (ch1lux
> ch0lux
) {
438 dev_dbg(&chip
->client
->dev
, "ch1lux > ch0lux-return last value\n");
439 ret
= chip
->als_cur_info
.lux
;
443 /* adjust for active time scale */
444 if (chip
->als_time_scale
== 0)
447 lux
= (lux
+ (chip
->als_time_scale
>> 1)) /
448 chip
->als_time_scale
;
450 /* adjust for active gain scale
451 * The tsl2x7x_device_lux tables have a factor of 256 built-in.
452 * User-specified gain provides a multiplier.
453 * Apply user-specified gain before shifting right to retain precision.
454 * Use 64 bits to avoid overflow on multiplication.
455 * Then go back to 32 bits before division to avoid using div_u64().
459 lux64
= lux64
* chip
->tsl2x7x_settings
.als_gain_trim
;
462 lux
= (lux
+ 500) / 1000;
464 if (lux
> TSL2X7X_LUX_CALC_OVER_FLOW
) /* check for overflow */
465 lux
= TSL2X7X_LUX_CALC_OVER_FLOW
;
467 /* Update the structure with the latest lux. */
469 chip
->als_cur_info
.lux
= lux
;
473 mutex_unlock(&chip
->als_mutex
);
479 * tsl2x7x_get_prox() - Reads proximity data registers and updates
482 * @indio_dev: pointer to IIO device
484 static int tsl2x7x_get_prox(struct iio_dev
*indio_dev
)
490 struct tsl2X7X_chip
*chip
= iio_priv(indio_dev
);
492 if (mutex_trylock(&chip
->prox_mutex
) == 0) {
493 dev_err(&chip
->client
->dev
,
494 "%s: Can't get prox mutex\n", __func__
);
498 ret
= tsl2x7x_i2c_read(chip
->client
,
499 (TSL2X7X_CMD_REG
| TSL2X7X_STATUS
), &status
);
501 dev_err(&chip
->client
->dev
,
502 "%s: i2c err=%d\n", __func__
, ret
);
512 if (!(status
& TSL2X7X_STA_ADC_VALID
))
520 if (!(status
& TSL2X7X_STA_PRX_VALID
))
525 for (i
= 0; i
< 2; i
++) {
526 ret
= tsl2x7x_i2c_read(chip
->client
,
528 (TSL2X7X_PRX_LO
+ i
)), &chdata
[i
]);
534 le16_to_cpup((const __le16
*)&chdata
[0]);
538 mutex_unlock(&chip
->prox_mutex
);
540 return chip
->prox_data
;
544 * tsl2x7x_defaults() - Populates the device nominal operating parameters
545 * with those provided by a 'platform' data struct or
546 * with prefined defaults.
548 * @chip: pointer to device structure.
550 static void tsl2x7x_defaults(struct tsl2X7X_chip
*chip
)
552 /* If Operational settings defined elsewhere.. */
553 if (chip
->pdata
&& chip
->pdata
->platform_default_settings
!= 0)
554 memcpy(&(chip
->tsl2x7x_settings
),
555 chip
->pdata
->platform_default_settings
,
556 sizeof(tsl2x7x_default_settings
));
558 memcpy(&(chip
->tsl2x7x_settings
),
559 &tsl2x7x_default_settings
,
560 sizeof(tsl2x7x_default_settings
));
562 /* Load up the proper lux table. */
563 if (chip
->pdata
&& chip
->pdata
->platform_lux_table
[0].ratio
!= 0)
564 memcpy(chip
->tsl2x7x_device_lux
,
565 chip
->pdata
->platform_lux_table
,
566 sizeof(chip
->pdata
->platform_lux_table
));
568 memcpy(chip
->tsl2x7x_device_lux
,
569 (struct tsl2x7x_lux
*)tsl2x7x_default_lux_table_group
[chip
->id
],
570 MAX_DEFAULT_TABLE_BYTES
);
574 * tsl2x7x_als_calibrate() - Obtain single reading and calculate
577 * @indio_dev: pointer to IIO device
579 static int tsl2x7x_als_calibrate(struct iio_dev
*indio_dev
)
581 struct tsl2X7X_chip
*chip
= iio_priv(indio_dev
);
587 ret
= i2c_smbus_write_byte(chip
->client
,
588 (TSL2X7X_CMD_REG
| TSL2X7X_CNTRL
));
590 dev_err(&chip
->client
->dev
,
591 "%s: failed to write CNTRL register, ret=%d\n",
596 reg_val
= i2c_smbus_read_byte(chip
->client
);
597 if ((reg_val
& (TSL2X7X_CNTL_ADC_ENBL
| TSL2X7X_CNTL_PWR_ON
))
598 != (TSL2X7X_CNTL_ADC_ENBL
| TSL2X7X_CNTL_PWR_ON
)) {
599 dev_err(&chip
->client
->dev
,
600 "%s: failed: ADC not enabled\n", __func__
);
604 ret
= i2c_smbus_write_byte(chip
->client
,
605 (TSL2X7X_CMD_REG
| TSL2X7X_CNTRL
));
607 dev_err(&chip
->client
->dev
,
608 "%s: failed to write ctrl reg: ret=%d\n",
613 reg_val
= i2c_smbus_read_byte(chip
->client
);
614 if ((reg_val
& TSL2X7X_STA_ADC_VALID
) != TSL2X7X_STA_ADC_VALID
) {
615 dev_err(&chip
->client
->dev
,
616 "%s: failed: STATUS - ADC not valid.\n", __func__
);
620 lux_val
= tsl2x7x_get_lux(indio_dev
);
622 dev_err(&chip
->client
->dev
,
623 "%s: failed to get lux\n", __func__
);
627 gain_trim_val
= (((chip
->tsl2x7x_settings
.als_cal_target
)
628 * chip
->tsl2x7x_settings
.als_gain_trim
) / lux_val
);
629 if ((gain_trim_val
< 250) || (gain_trim_val
> 4000))
632 chip
->tsl2x7x_settings
.als_gain_trim
= gain_trim_val
;
633 dev_info(&chip
->client
->dev
,
634 "%s als_calibrate completed\n", chip
->client
->name
);
636 return (int) gain_trim_val
;
639 static int tsl2x7x_chip_on(struct iio_dev
*indio_dev
)
647 struct tsl2X7X_chip
*chip
= iio_priv(indio_dev
);
650 if (chip
->pdata
&& chip
->pdata
->power_on
)
651 chip
->pdata
->power_on(indio_dev
);
653 /* Non calculated parameters */
654 chip
->tsl2x7x_config
[TSL2X7X_PRX_TIME
] =
655 chip
->tsl2x7x_settings
.prx_time
;
656 chip
->tsl2x7x_config
[TSL2X7X_WAIT_TIME
] =
657 chip
->tsl2x7x_settings
.wait_time
;
658 chip
->tsl2x7x_config
[TSL2X7X_PRX_CONFIG
] =
659 chip
->tsl2x7x_settings
.prox_config
;
661 chip
->tsl2x7x_config
[TSL2X7X_ALS_MINTHRESHLO
] =
662 (chip
->tsl2x7x_settings
.als_thresh_low
) & 0xFF;
663 chip
->tsl2x7x_config
[TSL2X7X_ALS_MINTHRESHHI
] =
664 (chip
->tsl2x7x_settings
.als_thresh_low
>> 8) & 0xFF;
665 chip
->tsl2x7x_config
[TSL2X7X_ALS_MAXTHRESHLO
] =
666 (chip
->tsl2x7x_settings
.als_thresh_high
) & 0xFF;
667 chip
->tsl2x7x_config
[TSL2X7X_ALS_MAXTHRESHHI
] =
668 (chip
->tsl2x7x_settings
.als_thresh_high
>> 8) & 0xFF;
669 chip
->tsl2x7x_config
[TSL2X7X_PERSISTENCE
] =
670 chip
->tsl2x7x_settings
.persistence
;
672 chip
->tsl2x7x_config
[TSL2X7X_PRX_COUNT
] =
673 chip
->tsl2x7x_settings
.prox_pulse_count
;
674 chip
->tsl2x7x_config
[TSL2X7X_PRX_MINTHRESHLO
] =
675 chip
->tsl2x7x_settings
.prox_thres_low
;
676 chip
->tsl2x7x_config
[TSL2X7X_PRX_MAXTHRESHLO
] =
677 chip
->tsl2x7x_settings
.prox_thres_high
;
679 /* and make sure we're not already on */
680 if (chip
->tsl2x7x_chip_status
== TSL2X7X_CHIP_WORKING
) {
681 /* if forcing a register update - turn off, then on */
682 dev_info(&chip
->client
->dev
, "device is already enabled\n");
686 /* determine als integration register */
687 als_count
= (chip
->tsl2x7x_settings
.als_time
* 100 + 135) / 270;
689 als_count
= 1; /* ensure at least one cycle */
691 /* convert back to time (encompasses overrides) */
692 als_time
= (als_count
* 27 + 5) / 10;
693 chip
->tsl2x7x_config
[TSL2X7X_ALS_TIME
] = 256 - als_count
;
695 /* Set the gain based on tsl2x7x_settings struct */
696 chip
->tsl2x7x_config
[TSL2X7X_GAIN
] =
697 (chip
->tsl2x7x_settings
.als_gain
|
698 (TSL2X7X_mA100
| TSL2X7X_DIODE1
)
699 | ((chip
->tsl2x7x_settings
.prox_gain
) << 2));
701 /* set chip struct re scaling and saturation */
702 chip
->als_saturation
= als_count
* 922; /* 90% of full scale */
703 chip
->als_time_scale
= (als_time
+ 25) / 50;
705 /* TSL2X7X Specific power-on / adc enable sequence
706 * Power on the device 1st. */
707 utmp
= TSL2X7X_CNTL_PWR_ON
;
708 ret
= i2c_smbus_write_byte_data(chip
->client
,
709 TSL2X7X_CMD_REG
| TSL2X7X_CNTRL
, utmp
);
711 dev_err(&chip
->client
->dev
,
712 "%s: failed on CNTRL reg.\n", __func__
);
716 /* Use the following shadow copy for our delay before enabling ADC.
717 * Write all the registers. */
718 for (i
= 0, dev_reg
= chip
->tsl2x7x_config
;
719 i
< TSL2X7X_MAX_CONFIG_REG
; i
++) {
720 ret
= i2c_smbus_write_byte_data(chip
->client
,
721 TSL2X7X_CMD_REG
+ i
, *dev_reg
++);
723 dev_err(&chip
->client
->dev
,
724 "%s: failed on write to reg %d.\n", __func__
, i
);
729 mdelay(3); /* Power-on settling time */
731 /* NOW enable the ADC
732 * initialize the desired mode of operation */
733 utmp
= TSL2X7X_CNTL_PWR_ON
|
734 TSL2X7X_CNTL_ADC_ENBL
|
735 TSL2X7X_CNTL_PROX_DET_ENBL
;
736 ret
= i2c_smbus_write_byte_data(chip
->client
,
737 TSL2X7X_CMD_REG
| TSL2X7X_CNTRL
, utmp
);
739 dev_err(&chip
->client
->dev
,
740 "%s: failed on 2nd CTRL reg.\n", __func__
);
744 chip
->tsl2x7x_chip_status
= TSL2X7X_CHIP_WORKING
;
746 if (chip
->tsl2x7x_settings
.interrupts_en
!= 0) {
747 dev_info(&chip
->client
->dev
, "Setting Up Interrupt(s)\n");
749 reg_val
= TSL2X7X_CNTL_PWR_ON
| TSL2X7X_CNTL_ADC_ENBL
;
750 if ((chip
->tsl2x7x_settings
.interrupts_en
== 0x20) ||
751 (chip
->tsl2x7x_settings
.interrupts_en
== 0x30))
752 reg_val
|= TSL2X7X_CNTL_PROX_DET_ENBL
;
754 reg_val
|= chip
->tsl2x7x_settings
.interrupts_en
;
755 ret
= i2c_smbus_write_byte_data(chip
->client
,
756 (TSL2X7X_CMD_REG
| TSL2X7X_CNTRL
), reg_val
);
758 dev_err(&chip
->client
->dev
,
759 "%s: failed in tsl2x7x_IOCTL_INT_SET.\n",
762 /* Clear out any initial interrupts */
763 ret
= i2c_smbus_write_byte(chip
->client
,
764 TSL2X7X_CMD_REG
| TSL2X7X_CMD_SPL_FN
|
765 TSL2X7X_CMD_PROXALS_INT_CLR
);
767 dev_err(&chip
->client
->dev
,
768 "%s: Failed to clear Int status\n",
777 static int tsl2x7x_chip_off(struct iio_dev
*indio_dev
)
780 struct tsl2X7X_chip
*chip
= iio_priv(indio_dev
);
782 /* turn device off */
783 chip
->tsl2x7x_chip_status
= TSL2X7X_CHIP_SUSPENDED
;
785 ret
= i2c_smbus_write_byte_data(chip
->client
,
786 TSL2X7X_CMD_REG
| TSL2X7X_CNTRL
, 0x00);
788 if (chip
->pdata
&& chip
->pdata
->power_off
)
789 chip
->pdata
->power_off(chip
->client
);
795 * tsl2x7x_invoke_change
796 * @indio_dev: pointer to IIO device
798 * Obtain and lock both ALS and PROX resources,
799 * determine and save device state (On/Off),
800 * cycle device to implement updated parameter,
801 * put device back into proper state, and unlock
805 int tsl2x7x_invoke_change(struct iio_dev
*indio_dev
)
807 struct tsl2X7X_chip
*chip
= iio_priv(indio_dev
);
808 int device_status
= chip
->tsl2x7x_chip_status
;
810 mutex_lock(&chip
->als_mutex
);
811 mutex_lock(&chip
->prox_mutex
);
813 if (device_status
== TSL2X7X_CHIP_WORKING
)
814 tsl2x7x_chip_off(indio_dev
);
816 tsl2x7x_chip_on(indio_dev
);
818 if (device_status
!= TSL2X7X_CHIP_WORKING
)
819 tsl2x7x_chip_off(indio_dev
);
821 mutex_unlock(&chip
->prox_mutex
);
822 mutex_unlock(&chip
->als_mutex
);
828 void tsl2x7x_prox_calculate(int *data
, int length
,
829 struct tsl2x7x_prox_stat
*statP
)
839 statP
->min
= INT_MAX
;
840 statP
->max
= INT_MIN
;
841 for (i
= 0; i
< length
; i
++) {
842 sample_sum
+= data
[i
];
843 statP
->min
= min(statP
->min
, data
[i
]);
844 statP
->max
= max(statP
->max
, data
[i
]);
847 statP
->mean
= sample_sum
/ length
;
849 for (i
= 0; i
< length
; i
++) {
850 tmp
= data
[i
] - statP
->mean
;
851 sample_sum
+= tmp
* tmp
;
853 statP
->stddev
= int_sqrt((long)sample_sum
)/length
;
857 * tsl2x7x_prox_cal() - Calculates std. and sets thresholds.
858 * @indio_dev: pointer to IIO device
860 * Calculates a standard deviation based on the samples,
861 * and sets the threshold accordingly.
863 static void tsl2x7x_prox_cal(struct iio_dev
*indio_dev
)
865 int prox_history
[MAX_SAMPLES_CAL
+ 1];
867 struct tsl2x7x_prox_stat prox_stat_data
[2];
868 struct tsl2x7x_prox_stat
*calP
;
869 struct tsl2X7X_chip
*chip
= iio_priv(indio_dev
);
871 u8 current_state
= chip
->tsl2x7x_chip_status
;
873 if (chip
->tsl2x7x_settings
.prox_max_samples_cal
> MAX_SAMPLES_CAL
) {
874 dev_err(&chip
->client
->dev
,
875 "%s: max prox samples cal is too big: %d\n",
876 __func__
, chip
->tsl2x7x_settings
.prox_max_samples_cal
);
877 chip
->tsl2x7x_settings
.prox_max_samples_cal
= MAX_SAMPLES_CAL
;
880 /* have to stop to change settings */
881 tsl2x7x_chip_off(indio_dev
);
883 /* Enable proximity detection save just in case prox not wanted yet*/
884 tmp_irq_settings
= chip
->tsl2x7x_settings
.interrupts_en
;
885 chip
->tsl2x7x_settings
.interrupts_en
|= TSL2X7X_CNTL_PROX_INT_ENBL
;
887 /*turn on device if not already on*/
888 tsl2x7x_chip_on(indio_dev
);
890 /*gather the samples*/
891 for (i
= 0; i
< chip
->tsl2x7x_settings
.prox_max_samples_cal
; i
++) {
893 tsl2x7x_get_prox(indio_dev
);
894 prox_history
[i
] = chip
->prox_data
;
895 dev_info(&chip
->client
->dev
, "2 i=%d prox data= %d\n",
899 tsl2x7x_chip_off(indio_dev
);
900 calP
= &prox_stat_data
[PROX_STAT_CAL
];
901 tsl2x7x_prox_calculate(prox_history
,
902 chip
->tsl2x7x_settings
.prox_max_samples_cal
, calP
);
903 chip
->tsl2x7x_settings
.prox_thres_high
= (calP
->max
<< 1) - calP
->mean
;
905 dev_info(&chip
->client
->dev
, " cal min=%d mean=%d max=%d\n",
906 calP
->min
, calP
->mean
, calP
->max
);
907 dev_info(&chip
->client
->dev
,
908 "%s proximity threshold set to %d\n",
909 chip
->client
->name
, chip
->tsl2x7x_settings
.prox_thres_high
);
911 /* back to the way they were */
912 chip
->tsl2x7x_settings
.interrupts_en
= tmp_irq_settings
;
913 if (current_state
== TSL2X7X_CHIP_WORKING
)
914 tsl2x7x_chip_on(indio_dev
);
917 static ssize_t
tsl2x7x_power_state_show(struct device
*dev
,
918 struct device_attribute
*attr
, char *buf
)
920 struct tsl2X7X_chip
*chip
= iio_priv(dev_to_iio_dev(dev
));
922 return snprintf(buf
, PAGE_SIZE
, "%d\n", chip
->tsl2x7x_chip_status
);
925 static ssize_t
tsl2x7x_power_state_store(struct device
*dev
,
926 struct device_attribute
*attr
, const char *buf
, size_t len
)
928 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
931 if (strtobool(buf
, &value
))
935 tsl2x7x_chip_on(indio_dev
);
937 tsl2x7x_chip_off(indio_dev
);
942 static ssize_t
tsl2x7x_gain_available_show(struct device
*dev
,
943 struct device_attribute
*attr
, char *buf
)
945 struct tsl2X7X_chip
*chip
= iio_priv(dev_to_iio_dev(dev
));
953 return snprintf(buf
, PAGE_SIZE
, "%s\n", "1 8 16 128");
957 return snprintf(buf
, PAGE_SIZE
, "%s\n", "1 8 16 120");
960 static ssize_t
tsl2x7x_prox_gain_available_show(struct device
*dev
,
961 struct device_attribute
*attr
, char *buf
)
963 return snprintf(buf
, PAGE_SIZE
, "%s\n", "1 2 4 8");
966 static ssize_t
tsl2x7x_als_time_show(struct device
*dev
,
967 struct device_attribute
*attr
, char *buf
)
969 struct tsl2X7X_chip
*chip
= iio_priv(dev_to_iio_dev(dev
));
972 y
= (TSL2X7X_MAX_TIMER_CNT
- (u8
)chip
->tsl2x7x_settings
.als_time
) + 1;
973 z
= y
* TSL2X7X_MIN_ITIME
;
977 return snprintf(buf
, PAGE_SIZE
, "%d.%03d\n", y
, z
);
980 static ssize_t
tsl2x7x_als_time_store(struct device
*dev
,
981 struct device_attribute
*attr
, const char *buf
, size_t len
)
983 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
984 struct tsl2X7X_chip
*chip
= iio_priv(indio_dev
);
985 struct tsl2x7x_parse_result result
;
988 ret
= iio_str_to_fixpoint(buf
, 100, &result
.integer
, &result
.fract
);
993 chip
->tsl2x7x_settings
.als_time
=
994 (TSL2X7X_MAX_TIMER_CNT
- (u8
)result
.fract
);
996 dev_info(&chip
->client
->dev
, "%s: als time = %d",
997 __func__
, chip
->tsl2x7x_settings
.als_time
);
999 tsl2x7x_invoke_change(indio_dev
);
1001 return IIO_VAL_INT_PLUS_MICRO
;
1004 static IIO_CONST_ATTR(in_illuminance0_integration_time_available
,
1007 static ssize_t
tsl2x7x_als_cal_target_show(struct device
*dev
,
1008 struct device_attribute
*attr
, char *buf
)
1010 struct tsl2X7X_chip
*chip
= iio_priv(dev_to_iio_dev(dev
));
1012 return snprintf(buf
, PAGE_SIZE
, "%d\n",
1013 chip
->tsl2x7x_settings
.als_cal_target
);
1016 static ssize_t
tsl2x7x_als_cal_target_store(struct device
*dev
,
1017 struct device_attribute
*attr
, const char *buf
, size_t len
)
1019 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
1020 struct tsl2X7X_chip
*chip
= iio_priv(indio_dev
);
1021 unsigned long value
;
1023 if (kstrtoul(buf
, 0, &value
))
1027 chip
->tsl2x7x_settings
.als_cal_target
= value
;
1029 tsl2x7x_invoke_change(indio_dev
);
1034 /* persistence settings */
1035 static ssize_t
tsl2x7x_als_persistence_show(struct device
*dev
,
1036 struct device_attribute
*attr
, char *buf
)
1038 struct tsl2X7X_chip
*chip
= iio_priv(dev_to_iio_dev(dev
));
1039 int y
, z
, filter_delay
;
1041 /* Determine integration time */
1042 y
= (TSL2X7X_MAX_TIMER_CNT
- (u8
)chip
->tsl2x7x_settings
.als_time
) + 1;
1043 z
= y
* TSL2X7X_MIN_ITIME
;
1044 filter_delay
= z
* (chip
->tsl2x7x_settings
.persistence
& 0x0F);
1045 y
= (filter_delay
/ 1000);
1046 z
= (filter_delay
% 1000);
1048 return snprintf(buf
, PAGE_SIZE
, "%d.%03d\n", y
, z
);
1051 static ssize_t
tsl2x7x_als_persistence_store(struct device
*dev
,
1052 struct device_attribute
*attr
, const char *buf
, size_t len
)
1054 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
1055 struct tsl2X7X_chip
*chip
= iio_priv(indio_dev
);
1056 struct tsl2x7x_parse_result result
;
1057 int y
, z
, filter_delay
;
1060 ret
= iio_str_to_fixpoint(buf
, 100, &result
.integer
, &result
.fract
);
1064 y
= (TSL2X7X_MAX_TIMER_CNT
- (u8
)chip
->tsl2x7x_settings
.als_time
) + 1;
1065 z
= y
* TSL2X7X_MIN_ITIME
;
1068 DIV_ROUND_UP(((result
.integer
* 1000) + result
.fract
), z
);
1070 chip
->tsl2x7x_settings
.persistence
&= 0xF0;
1071 chip
->tsl2x7x_settings
.persistence
|= (filter_delay
& 0x0F);
1073 dev_info(&chip
->client
->dev
, "%s: als persistence = %d",
1074 __func__
, filter_delay
);
1076 tsl2x7x_invoke_change(indio_dev
);
1078 return IIO_VAL_INT_PLUS_MICRO
;
1081 static ssize_t
tsl2x7x_prox_persistence_show(struct device
*dev
,
1082 struct device_attribute
*attr
, char *buf
)
1084 struct tsl2X7X_chip
*chip
= iio_priv(dev_to_iio_dev(dev
));
1085 int y
, z
, filter_delay
;
1087 /* Determine integration time */
1088 y
= (TSL2X7X_MAX_TIMER_CNT
- (u8
)chip
->tsl2x7x_settings
.prx_time
) + 1;
1089 z
= y
* TSL2X7X_MIN_ITIME
;
1090 filter_delay
= z
* ((chip
->tsl2x7x_settings
.persistence
& 0xF0) >> 4);
1091 y
= (filter_delay
/ 1000);
1092 z
= (filter_delay
% 1000);
1094 return snprintf(buf
, PAGE_SIZE
, "%d.%03d\n", y
, z
);
1097 static ssize_t
tsl2x7x_prox_persistence_store(struct device
*dev
,
1098 struct device_attribute
*attr
, const char *buf
, size_t len
)
1100 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
1101 struct tsl2X7X_chip
*chip
= iio_priv(indio_dev
);
1102 struct tsl2x7x_parse_result result
;
1103 int y
, z
, filter_delay
;
1106 ret
= iio_str_to_fixpoint(buf
, 100, &result
.integer
, &result
.fract
);
1110 y
= (TSL2X7X_MAX_TIMER_CNT
- (u8
)chip
->tsl2x7x_settings
.prx_time
) + 1;
1111 z
= y
* TSL2X7X_MIN_ITIME
;
1114 DIV_ROUND_UP(((result
.integer
* 1000) + result
.fract
), z
);
1116 chip
->tsl2x7x_settings
.persistence
&= 0x0F;
1117 chip
->tsl2x7x_settings
.persistence
|= ((filter_delay
<< 4) & 0xF0);
1119 dev_info(&chip
->client
->dev
, "%s: prox persistence = %d",
1120 __func__
, filter_delay
);
1122 tsl2x7x_invoke_change(indio_dev
);
1124 return IIO_VAL_INT_PLUS_MICRO
;
1127 static ssize_t
tsl2x7x_do_calibrate(struct device
*dev
,
1128 struct device_attribute
*attr
, const char *buf
, size_t len
)
1130 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
1133 if (strtobool(buf
, &value
))
1137 tsl2x7x_als_calibrate(indio_dev
);
1139 tsl2x7x_invoke_change(indio_dev
);
1144 static ssize_t
tsl2x7x_luxtable_show(struct device
*dev
,
1145 struct device_attribute
*attr
, char *buf
)
1147 struct tsl2X7X_chip
*chip
= iio_priv(dev_to_iio_dev(dev
));
1151 while (i
< (TSL2X7X_MAX_LUX_TABLE_SIZE
* 3)) {
1152 offset
+= snprintf(buf
+ offset
, PAGE_SIZE
, "%d,%d,%d,",
1153 chip
->tsl2x7x_device_lux
[i
].ratio
,
1154 chip
->tsl2x7x_device_lux
[i
].ch0
,
1155 chip
->tsl2x7x_device_lux
[i
].ch1
);
1156 if (chip
->tsl2x7x_device_lux
[i
].ratio
== 0) {
1157 /* We just printed the first "0" entry.
1158 * Now get rid of the extra "," and break. */
1165 offset
+= snprintf(buf
+ offset
, PAGE_SIZE
, "\n");
1169 static ssize_t
tsl2x7x_luxtable_store(struct device
*dev
,
1170 struct device_attribute
*attr
, const char *buf
, size_t len
)
1172 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
1173 struct tsl2X7X_chip
*chip
= iio_priv(indio_dev
);
1174 int value
[ARRAY_SIZE(chip
->tsl2x7x_device_lux
)*3 + 1];
1177 get_options(buf
, ARRAY_SIZE(value
), value
);
1179 /* We now have an array of ints starting at value[1], and
1180 * enumerated by value[0].
1181 * We expect each group of three ints is one table entry,
1182 * and the last table entry is all 0.
1185 if ((n
% 3) || n
< 6 ||
1186 n
> ((ARRAY_SIZE(chip
->tsl2x7x_device_lux
) - 1) * 3)) {
1187 dev_info(dev
, "LUX TABLE INPUT ERROR 1 Value[0]=%d\n", n
);
1191 if ((value
[(n
- 2)] | value
[(n
- 1)] | value
[n
]) != 0) {
1192 dev_info(dev
, "LUX TABLE INPUT ERROR 2 Value[0]=%d\n", n
);
1196 if (chip
->tsl2x7x_chip_status
== TSL2X7X_CHIP_WORKING
)
1197 tsl2x7x_chip_off(indio_dev
);
1199 /* Zero out the table */
1200 memset(chip
->tsl2x7x_device_lux
, 0, sizeof(chip
->tsl2x7x_device_lux
));
1201 memcpy(chip
->tsl2x7x_device_lux
, &value
[1], (value
[0] * 4));
1203 tsl2x7x_invoke_change(indio_dev
);
1208 static ssize_t
tsl2x7x_do_prox_calibrate(struct device
*dev
,
1209 struct device_attribute
*attr
, const char *buf
, size_t len
)
1211 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
1214 if (strtobool(buf
, &value
))
1218 tsl2x7x_prox_cal(indio_dev
);
1220 tsl2x7x_invoke_change(indio_dev
);
1225 static int tsl2x7x_read_interrupt_config(struct iio_dev
*indio_dev
,
1228 struct tsl2X7X_chip
*chip
= iio_priv(indio_dev
);
1231 if (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code
) == IIO_INTENSITY
)
1232 ret
= !!(chip
->tsl2x7x_settings
.interrupts_en
& 0x10);
1234 ret
= !!(chip
->tsl2x7x_settings
.interrupts_en
& 0x20);
1239 static int tsl2x7x_write_interrupt_config(struct iio_dev
*indio_dev
,
1243 struct tsl2X7X_chip
*chip
= iio_priv(indio_dev
);
1245 if (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code
) == IIO_INTENSITY
) {
1247 chip
->tsl2x7x_settings
.interrupts_en
|= 0x10;
1249 chip
->tsl2x7x_settings
.interrupts_en
&= 0x20;
1252 chip
->tsl2x7x_settings
.interrupts_en
|= 0x20;
1254 chip
->tsl2x7x_settings
.interrupts_en
&= 0x10;
1257 tsl2x7x_invoke_change(indio_dev
);
1262 static int tsl2x7x_write_thresh(struct iio_dev
*indio_dev
,
1266 struct tsl2X7X_chip
*chip
= iio_priv(indio_dev
);
1268 if (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code
) == IIO_INTENSITY
) {
1269 switch (IIO_EVENT_CODE_EXTRACT_DIR(event_code
)) {
1270 case IIO_EV_DIR_RISING
:
1271 chip
->tsl2x7x_settings
.als_thresh_high
= val
;
1273 case IIO_EV_DIR_FALLING
:
1274 chip
->tsl2x7x_settings
.als_thresh_low
= val
;
1280 switch (IIO_EVENT_CODE_EXTRACT_DIR(event_code
)) {
1281 case IIO_EV_DIR_RISING
:
1282 chip
->tsl2x7x_settings
.prox_thres_high
= val
;
1284 case IIO_EV_DIR_FALLING
:
1285 chip
->tsl2x7x_settings
.prox_thres_low
= val
;
1292 tsl2x7x_invoke_change(indio_dev
);
1297 static int tsl2x7x_read_thresh(struct iio_dev
*indio_dev
,
1301 struct tsl2X7X_chip
*chip
= iio_priv(indio_dev
);
1303 if (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code
) == IIO_INTENSITY
) {
1304 switch (IIO_EVENT_CODE_EXTRACT_DIR(event_code
)) {
1305 case IIO_EV_DIR_RISING
:
1306 *val
= chip
->tsl2x7x_settings
.als_thresh_high
;
1308 case IIO_EV_DIR_FALLING
:
1309 *val
= chip
->tsl2x7x_settings
.als_thresh_low
;
1315 switch (IIO_EVENT_CODE_EXTRACT_DIR(event_code
)) {
1316 case IIO_EV_DIR_RISING
:
1317 *val
= chip
->tsl2x7x_settings
.prox_thres_high
;
1319 case IIO_EV_DIR_FALLING
:
1320 *val
= chip
->tsl2x7x_settings
.prox_thres_low
;
1330 static int tsl2x7x_read_raw(struct iio_dev
*indio_dev
,
1331 struct iio_chan_spec
const *chan
,
1337 struct tsl2X7X_chip
*chip
= iio_priv(indio_dev
);
1340 case IIO_CHAN_INFO_PROCESSED
:
1341 switch (chan
->type
) {
1343 tsl2x7x_get_lux(indio_dev
);
1344 *val
= chip
->als_cur_info
.lux
;
1352 case IIO_CHAN_INFO_RAW
:
1353 switch (chan
->type
) {
1355 tsl2x7x_get_lux(indio_dev
);
1356 if (chan
->channel
== 0)
1357 *val
= chip
->als_cur_info
.als_ch0
;
1359 *val
= chip
->als_cur_info
.als_ch1
;
1363 tsl2x7x_get_prox(indio_dev
);
1364 *val
= chip
->prox_data
;
1372 case IIO_CHAN_INFO_CALIBSCALE
:
1373 if (chan
->type
== IIO_LIGHT
)
1375 tsl2X7X_als_gainadj
[chip
->tsl2x7x_settings
.als_gain
];
1378 tsl2X7X_prx_gainadj
[chip
->tsl2x7x_settings
.prox_gain
];
1381 case IIO_CHAN_INFO_CALIBBIAS
:
1382 *val
= chip
->tsl2x7x_settings
.als_gain_trim
;
1393 static int tsl2x7x_write_raw(struct iio_dev
*indio_dev
,
1394 struct iio_chan_spec
const *chan
,
1399 struct tsl2X7X_chip
*chip
= iio_priv(indio_dev
);
1402 case IIO_CHAN_INFO_CALIBSCALE
:
1403 if (chan
->type
== IIO_INTENSITY
) {
1406 chip
->tsl2x7x_settings
.als_gain
= 0;
1409 chip
->tsl2x7x_settings
.als_gain
= 1;
1412 chip
->tsl2x7x_settings
.als_gain
= 2;
1424 chip
->tsl2x7x_settings
.als_gain
= 3;
1436 chip
->tsl2x7x_settings
.als_gain
= 3;
1444 chip
->tsl2x7x_settings
.prox_gain
= 0;
1447 chip
->tsl2x7x_settings
.prox_gain
= 1;
1450 chip
->tsl2x7x_settings
.prox_gain
= 2;
1453 chip
->tsl2x7x_settings
.prox_gain
= 3;
1460 case IIO_CHAN_INFO_CALIBBIAS
:
1461 chip
->tsl2x7x_settings
.als_gain_trim
= val
;
1468 tsl2x7x_invoke_change(indio_dev
);
1473 static DEVICE_ATTR(power_state
, S_IRUGO
| S_IWUSR
,
1474 tsl2x7x_power_state_show
, tsl2x7x_power_state_store
);
1476 static DEVICE_ATTR(in_proximity0_calibscale_available
, S_IRUGO
,
1477 tsl2x7x_prox_gain_available_show
, NULL
);
1479 static DEVICE_ATTR(in_illuminance0_calibscale_available
, S_IRUGO
,
1480 tsl2x7x_gain_available_show
, NULL
);
1482 static DEVICE_ATTR(in_illuminance0_integration_time
, S_IRUGO
| S_IWUSR
,
1483 tsl2x7x_als_time_show
, tsl2x7x_als_time_store
);
1485 static DEVICE_ATTR(in_illuminance0_target_input
, S_IRUGO
| S_IWUSR
,
1486 tsl2x7x_als_cal_target_show
, tsl2x7x_als_cal_target_store
);
1488 static DEVICE_ATTR(in_illuminance0_calibrate
, S_IWUSR
, NULL
,
1489 tsl2x7x_do_calibrate
);
1491 static DEVICE_ATTR(in_proximity0_calibrate
, S_IWUSR
, NULL
,
1492 tsl2x7x_do_prox_calibrate
);
1494 static DEVICE_ATTR(in_illuminance0_lux_table
, S_IRUGO
| S_IWUSR
,
1495 tsl2x7x_luxtable_show
, tsl2x7x_luxtable_store
);
1497 static DEVICE_ATTR(in_intensity0_thresh_period
, S_IRUGO
| S_IWUSR
,
1498 tsl2x7x_als_persistence_show
, tsl2x7x_als_persistence_store
);
1500 static DEVICE_ATTR(in_proximity0_thresh_period
, S_IRUGO
| S_IWUSR
,
1501 tsl2x7x_prox_persistence_show
, tsl2x7x_prox_persistence_store
);
1503 /* Use the default register values to identify the Taos device */
1504 static int tsl2x7x_device_id(unsigned char *id
, int target
)
1510 return ((*id
& 0xf0) == TRITON_ID
);
1514 return ((*id
& 0xf0) == HALIBUT_ID
);
1521 return ((*id
& 0xf0) == SWORDFISH_ID
);
1528 static irqreturn_t
tsl2x7x_event_handler(int irq
, void *private)
1530 struct iio_dev
*indio_dev
= private;
1531 struct tsl2X7X_chip
*chip
= iio_priv(indio_dev
);
1532 s64 timestamp
= iio_get_time_ns();
1536 value
= i2c_smbus_read_byte_data(chip
->client
,
1537 TSL2X7X_CMD_REG
| TSL2X7X_STATUS
);
1539 /* What type of interrupt do we need to process */
1540 if (value
& TSL2X7X_STA_PRX_INTR
) {
1541 tsl2x7x_get_prox(indio_dev
); /* freshen data for ABI */
1542 iio_push_event(indio_dev
,
1543 IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY
,
1550 if (value
& TSL2X7X_STA_ALS_INTR
) {
1551 tsl2x7x_get_lux(indio_dev
); /* freshen data for ABI */
1552 iio_push_event(indio_dev
,
1553 IIO_UNMOD_EVENT_CODE(IIO_LIGHT
,
1559 /* Clear interrupt now that we have handled it. */
1560 ret
= i2c_smbus_write_byte(chip
->client
,
1561 TSL2X7X_CMD_REG
| TSL2X7X_CMD_SPL_FN
|
1562 TSL2X7X_CMD_PROXALS_INT_CLR
);
1564 dev_err(&chip
->client
->dev
,
1565 "%s: Failed to clear irq from event handler. err = %d\n",
1571 static struct attribute
*tsl2x7x_ALS_device_attrs
[] = {
1572 &dev_attr_power_state
.attr
,
1573 &dev_attr_in_illuminance0_calibscale_available
.attr
,
1574 &dev_attr_in_illuminance0_integration_time
.attr
,
1575 &iio_const_attr_in_illuminance0_integration_time_available\
1577 &dev_attr_in_illuminance0_target_input
.attr
,
1578 &dev_attr_in_illuminance0_calibrate
.attr
,
1579 &dev_attr_in_illuminance0_lux_table
.attr
,
1583 static struct attribute
*tsl2x7x_PRX_device_attrs
[] = {
1584 &dev_attr_power_state
.attr
,
1585 &dev_attr_in_proximity0_calibrate
.attr
,
1589 static struct attribute
*tsl2x7x_ALSPRX_device_attrs
[] = {
1590 &dev_attr_power_state
.attr
,
1591 &dev_attr_in_illuminance0_calibscale_available
.attr
,
1592 &dev_attr_in_illuminance0_integration_time
.attr
,
1593 &iio_const_attr_in_illuminance0_integration_time_available\
1595 &dev_attr_in_illuminance0_target_input
.attr
,
1596 &dev_attr_in_illuminance0_calibrate
.attr
,
1597 &dev_attr_in_illuminance0_lux_table
.attr
,
1598 &dev_attr_in_proximity0_calibrate
.attr
,
1602 static struct attribute
*tsl2x7x_PRX2_device_attrs
[] = {
1603 &dev_attr_power_state
.attr
,
1604 &dev_attr_in_proximity0_calibrate
.attr
,
1605 &dev_attr_in_proximity0_calibscale_available
.attr
,
1609 static struct attribute
*tsl2x7x_ALSPRX2_device_attrs
[] = {
1610 &dev_attr_power_state
.attr
,
1611 &dev_attr_in_illuminance0_calibscale_available
.attr
,
1612 &dev_attr_in_illuminance0_integration_time
.attr
,
1613 &iio_const_attr_in_illuminance0_integration_time_available\
1615 &dev_attr_in_illuminance0_target_input
.attr
,
1616 &dev_attr_in_illuminance0_calibrate
.attr
,
1617 &dev_attr_in_illuminance0_lux_table
.attr
,
1618 &dev_attr_in_proximity0_calibrate
.attr
,
1619 &dev_attr_in_proximity0_calibscale_available
.attr
,
1623 static struct attribute
*tsl2X7X_ALS_event_attrs
[] = {
1624 &dev_attr_in_intensity0_thresh_period
.attr
,
1627 static struct attribute
*tsl2X7X_PRX_event_attrs
[] = {
1628 &dev_attr_in_proximity0_thresh_period
.attr
,
1632 static struct attribute
*tsl2X7X_ALSPRX_event_attrs
[] = {
1633 &dev_attr_in_intensity0_thresh_period
.attr
,
1634 &dev_attr_in_proximity0_thresh_period
.attr
,
1638 static const struct attribute_group tsl2X7X_device_attr_group_tbl
[] = {
1640 .attrs
= tsl2x7x_ALS_device_attrs
,
1643 .attrs
= tsl2x7x_PRX_device_attrs
,
1646 .attrs
= tsl2x7x_ALSPRX_device_attrs
,
1649 .attrs
= tsl2x7x_PRX2_device_attrs
,
1652 .attrs
= tsl2x7x_ALSPRX2_device_attrs
,
1656 static struct attribute_group tsl2X7X_event_attr_group_tbl
[] = {
1658 .attrs
= tsl2X7X_ALS_event_attrs
,
1662 .attrs
= tsl2X7X_PRX_event_attrs
,
1666 .attrs
= tsl2X7X_ALSPRX_event_attrs
,
1671 static const struct iio_info tsl2X7X_device_info
[] = {
1673 .attrs
= &tsl2X7X_device_attr_group_tbl
[ALS
],
1674 .event_attrs
= &tsl2X7X_event_attr_group_tbl
[ALS
],
1675 .driver_module
= THIS_MODULE
,
1676 .read_raw
= &tsl2x7x_read_raw
,
1677 .write_raw
= &tsl2x7x_write_raw
,
1678 .read_event_value
= &tsl2x7x_read_thresh
,
1679 .write_event_value
= &tsl2x7x_write_thresh
,
1680 .read_event_config
= &tsl2x7x_read_interrupt_config
,
1681 .write_event_config
= &tsl2x7x_write_interrupt_config
,
1684 .attrs
= &tsl2X7X_device_attr_group_tbl
[PRX
],
1685 .event_attrs
= &tsl2X7X_event_attr_group_tbl
[PRX
],
1686 .driver_module
= THIS_MODULE
,
1687 .read_raw
= &tsl2x7x_read_raw
,
1688 .write_raw
= &tsl2x7x_write_raw
,
1689 .read_event_value
= &tsl2x7x_read_thresh
,
1690 .write_event_value
= &tsl2x7x_write_thresh
,
1691 .read_event_config
= &tsl2x7x_read_interrupt_config
,
1692 .write_event_config
= &tsl2x7x_write_interrupt_config
,
1695 .attrs
= &tsl2X7X_device_attr_group_tbl
[ALSPRX
],
1696 .event_attrs
= &tsl2X7X_event_attr_group_tbl
[ALSPRX
],
1697 .driver_module
= THIS_MODULE
,
1698 .read_raw
= &tsl2x7x_read_raw
,
1699 .write_raw
= &tsl2x7x_write_raw
,
1700 .read_event_value
= &tsl2x7x_read_thresh
,
1701 .write_event_value
= &tsl2x7x_write_thresh
,
1702 .read_event_config
= &tsl2x7x_read_interrupt_config
,
1703 .write_event_config
= &tsl2x7x_write_interrupt_config
,
1706 .attrs
= &tsl2X7X_device_attr_group_tbl
[PRX2
],
1707 .event_attrs
= &tsl2X7X_event_attr_group_tbl
[PRX
],
1708 .driver_module
= THIS_MODULE
,
1709 .read_raw
= &tsl2x7x_read_raw
,
1710 .write_raw
= &tsl2x7x_write_raw
,
1711 .read_event_value
= &tsl2x7x_read_thresh
,
1712 .write_event_value
= &tsl2x7x_write_thresh
,
1713 .read_event_config
= &tsl2x7x_read_interrupt_config
,
1714 .write_event_config
= &tsl2x7x_write_interrupt_config
,
1717 .attrs
= &tsl2X7X_device_attr_group_tbl
[ALSPRX2
],
1718 .event_attrs
= &tsl2X7X_event_attr_group_tbl
[ALSPRX
],
1719 .driver_module
= THIS_MODULE
,
1720 .read_raw
= &tsl2x7x_read_raw
,
1721 .write_raw
= &tsl2x7x_write_raw
,
1722 .read_event_value
= &tsl2x7x_read_thresh
,
1723 .write_event_value
= &tsl2x7x_write_thresh
,
1724 .read_event_config
= &tsl2x7x_read_interrupt_config
,
1725 .write_event_config
= &tsl2x7x_write_interrupt_config
,
1729 static const struct tsl2x7x_chip_info tsl2x7x_chip_info_tbl
[] = {
1736 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
1738 .type
= IIO_INTENSITY
,
1741 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1742 BIT(IIO_CHAN_INFO_CALIBSCALE
) |
1743 BIT(IIO_CHAN_INFO_CALIBBIAS
),
1744 .event_mask
= TSL2X7X_EVENT_MASK
1746 .type
= IIO_INTENSITY
,
1751 .chan_table_elements
= 3,
1752 .info
= &tsl2X7X_device_info
[ALS
],
1757 .type
= IIO_PROXIMITY
,
1760 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
1761 .event_mask
= TSL2X7X_EVENT_MASK
1764 .chan_table_elements
= 1,
1765 .info
= &tsl2X7X_device_info
[PRX
],
1773 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
)
1775 .type
= IIO_INTENSITY
,
1778 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1779 BIT(IIO_CHAN_INFO_CALIBSCALE
) |
1780 BIT(IIO_CHAN_INFO_CALIBBIAS
),
1781 .event_mask
= TSL2X7X_EVENT_MASK
1783 .type
= IIO_INTENSITY
,
1786 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
1788 .type
= IIO_PROXIMITY
,
1791 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
1792 .event_mask
= TSL2X7X_EVENT_MASK
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_mask
= TSL2X7X_EVENT_MASK
1809 .chan_table_elements
= 1,
1810 .info
= &tsl2X7X_device_info
[PRX2
],
1818 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
1820 .type
= IIO_INTENSITY
,
1823 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1824 BIT(IIO_CHAN_INFO_CALIBSCALE
) |
1825 BIT(IIO_CHAN_INFO_CALIBBIAS
),
1826 .event_mask
= TSL2X7X_EVENT_MASK
1828 .type
= IIO_INTENSITY
,
1831 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
1833 .type
= IIO_PROXIMITY
,
1836 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1837 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1838 .event_mask
= TSL2X7X_EVENT_MASK
1841 .chan_table_elements
= 4,
1842 .info
= &tsl2X7X_device_info
[ALSPRX2
],
1846 static int tsl2x7x_probe(struct i2c_client
*clientp
,
1847 const struct i2c_device_id
*id
)
1850 unsigned char device_id
;
1851 struct iio_dev
*indio_dev
;
1852 struct tsl2X7X_chip
*chip
;
1854 indio_dev
= iio_device_alloc(sizeof(*chip
));
1858 chip
= iio_priv(indio_dev
);
1859 chip
->client
= clientp
;
1860 i2c_set_clientdata(clientp
, indio_dev
);
1862 ret
= tsl2x7x_i2c_read(chip
->client
,
1863 TSL2X7X_CHIPID
, &device_id
);
1867 if ((!tsl2x7x_device_id(&device_id
, id
->driver_data
)) ||
1868 (tsl2x7x_device_id(&device_id
, id
->driver_data
) == -EINVAL
)) {
1869 dev_info(&chip
->client
->dev
,
1870 "%s: i2c device found does not match expected id\n",
1875 ret
= i2c_smbus_write_byte(clientp
, (TSL2X7X_CMD_REG
| TSL2X7X_CNTRL
));
1877 dev_err(&clientp
->dev
, "%s: write to cmd reg failed. err = %d\n",
1882 /* ALS and PROX functions can be invoked via user space poll
1883 * or H/W interrupt. If busy return last sample. */
1884 mutex_init(&chip
->als_mutex
);
1885 mutex_init(&chip
->prox_mutex
);
1887 chip
->tsl2x7x_chip_status
= TSL2X7X_CHIP_UNKNOWN
;
1888 chip
->pdata
= clientp
->dev
.platform_data
;
1889 chip
->id
= id
->driver_data
;
1891 &tsl2x7x_chip_info_tbl
[device_channel_config
[id
->driver_data
]];
1893 indio_dev
->info
= chip
->chip_info
->info
;
1894 indio_dev
->dev
.parent
= &clientp
->dev
;
1895 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1896 indio_dev
->name
= chip
->client
->name
;
1897 indio_dev
->channels
= chip
->chip_info
->channel
;
1898 indio_dev
->num_channels
= chip
->chip_info
->chan_table_elements
;
1901 ret
= request_threaded_irq(clientp
->irq
,
1903 &tsl2x7x_event_handler
,
1904 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
1908 dev_err(&clientp
->dev
,
1909 "%s: irq request failed", __func__
);
1914 /* Load up the defaults */
1915 tsl2x7x_defaults(chip
);
1916 /* Make sure the chip is on */
1917 tsl2x7x_chip_on(indio_dev
);
1919 ret
= iio_device_register(indio_dev
);
1921 dev_err(&clientp
->dev
,
1922 "%s: iio registration failed\n", __func__
);
1926 dev_info(&clientp
->dev
, "%s Light sensor found.\n", id
->name
);
1932 free_irq(clientp
->irq
, indio_dev
);
1934 iio_device_free(indio_dev
);
1939 static int tsl2x7x_suspend(struct device
*dev
)
1941 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1942 struct tsl2X7X_chip
*chip
= iio_priv(indio_dev
);
1945 if (chip
->tsl2x7x_chip_status
== TSL2X7X_CHIP_WORKING
) {
1946 ret
= tsl2x7x_chip_off(indio_dev
);
1947 chip
->tsl2x7x_chip_status
= TSL2X7X_CHIP_SUSPENDED
;
1950 if (chip
->pdata
&& chip
->pdata
->platform_power
) {
1951 pm_message_t pmm
= {PM_EVENT_SUSPEND
};
1952 chip
->pdata
->platform_power(dev
, pmm
);
1958 static int tsl2x7x_resume(struct device
*dev
)
1960 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1961 struct tsl2X7X_chip
*chip
= iio_priv(indio_dev
);
1964 if (chip
->pdata
&& chip
->pdata
->platform_power
) {
1965 pm_message_t pmm
= {PM_EVENT_RESUME
};
1966 chip
->pdata
->platform_power(dev
, pmm
);
1969 if (chip
->tsl2x7x_chip_status
== TSL2X7X_CHIP_SUSPENDED
)
1970 ret
= tsl2x7x_chip_on(indio_dev
);
1975 static int tsl2x7x_remove(struct i2c_client
*client
)
1977 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
1979 tsl2x7x_chip_off(indio_dev
);
1981 iio_device_unregister(indio_dev
);
1983 free_irq(client
->irq
, indio_dev
);
1985 iio_device_free(indio_dev
);
1990 static struct i2c_device_id tsl2x7x_idtable
[] = {
1991 { "tsl2571", tsl2571
},
1992 { "tsl2671", tsl2671
},
1993 { "tmd2671", tmd2671
},
1994 { "tsl2771", tsl2771
},
1995 { "tmd2771", tmd2771
},
1996 { "tsl2572", tsl2572
},
1997 { "tsl2672", tsl2672
},
1998 { "tmd2672", tmd2672
},
1999 { "tsl2772", tsl2772
},
2000 { "tmd2772", tmd2772
},
2004 MODULE_DEVICE_TABLE(i2c
, tsl2x7x_idtable
);
2006 static const struct dev_pm_ops tsl2x7x_pm_ops
= {
2007 .suspend
= tsl2x7x_suspend
,
2008 .resume
= tsl2x7x_resume
,
2011 /* Driver definition */
2012 static struct i2c_driver tsl2x7x_driver
= {
2015 .pm
= &tsl2x7x_pm_ops
,
2017 .id_table
= tsl2x7x_idtable
,
2018 .probe
= tsl2x7x_probe
,
2019 .remove
= tsl2x7x_remove
,
2022 module_i2c_driver(tsl2x7x_driver
);
2024 MODULE_AUTHOR("J. August Brenner<jbrenner@taosinc.com>");
2025 MODULE_DESCRIPTION("TAOS tsl2x7x ambient and proximity light sensor driver");
2026 MODULE_LICENSE("GPL");