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
) & 0xFF;
676 chip
->tsl2x7x_config
[TSL2X7X_PRX_MINTHRESHHI
] =
677 (chip
->tsl2x7x_settings
.prox_thres_low
>> 8) & 0xFF;
678 chip
->tsl2x7x_config
[TSL2X7X_PRX_MAXTHRESHLO
] =
679 (chip
->tsl2x7x_settings
.prox_thres_high
) & 0xFF;
680 chip
->tsl2x7x_config
[TSL2X7X_PRX_MAXTHRESHHI
] =
681 (chip
->tsl2x7x_settings
.prox_thres_high
>> 8) & 0xFF;
683 /* and make sure we're not already on */
684 if (chip
->tsl2x7x_chip_status
== TSL2X7X_CHIP_WORKING
) {
685 /* if forcing a register update - turn off, then on */
686 dev_info(&chip
->client
->dev
, "device is already enabled\n");
690 /* determine als integration register */
691 als_count
= (chip
->tsl2x7x_settings
.als_time
* 100 + 135) / 270;
693 als_count
= 1; /* ensure at least one cycle */
695 /* convert back to time (encompasses overrides) */
696 als_time
= (als_count
* 27 + 5) / 10;
697 chip
->tsl2x7x_config
[TSL2X7X_ALS_TIME
] = 256 - als_count
;
699 /* Set the gain based on tsl2x7x_settings struct */
700 chip
->tsl2x7x_config
[TSL2X7X_GAIN
] =
701 (chip
->tsl2x7x_settings
.als_gain
|
702 (TSL2X7X_mA100
| TSL2X7X_DIODE1
)
703 | ((chip
->tsl2x7x_settings
.prox_gain
) << 2));
705 /* set chip struct re scaling and saturation */
706 chip
->als_saturation
= als_count
* 922; /* 90% of full scale */
707 chip
->als_time_scale
= (als_time
+ 25) / 50;
709 /* TSL2X7X Specific power-on / adc enable sequence
710 * Power on the device 1st. */
711 utmp
= TSL2X7X_CNTL_PWR_ON
;
712 ret
= i2c_smbus_write_byte_data(chip
->client
,
713 TSL2X7X_CMD_REG
| TSL2X7X_CNTRL
, utmp
);
715 dev_err(&chip
->client
->dev
,
716 "%s: failed on CNTRL reg.\n", __func__
);
720 /* Use the following shadow copy for our delay before enabling ADC.
721 * Write all the registers. */
722 for (i
= 0, dev_reg
= chip
->tsl2x7x_config
;
723 i
< TSL2X7X_MAX_CONFIG_REG
; i
++) {
724 ret
= i2c_smbus_write_byte_data(chip
->client
,
725 TSL2X7X_CMD_REG
+ i
, *dev_reg
++);
727 dev_err(&chip
->client
->dev
,
728 "%s: failed on write to reg %d.\n", __func__
, i
);
733 mdelay(3); /* Power-on settling time */
735 /* NOW enable the ADC
736 * initialize the desired mode of operation */
737 utmp
= TSL2X7X_CNTL_PWR_ON
|
738 TSL2X7X_CNTL_ADC_ENBL
|
739 TSL2X7X_CNTL_PROX_DET_ENBL
;
740 ret
= i2c_smbus_write_byte_data(chip
->client
,
741 TSL2X7X_CMD_REG
| TSL2X7X_CNTRL
, utmp
);
743 dev_err(&chip
->client
->dev
,
744 "%s: failed on 2nd CTRL reg.\n", __func__
);
748 chip
->tsl2x7x_chip_status
= TSL2X7X_CHIP_WORKING
;
750 if (chip
->tsl2x7x_settings
.interrupts_en
!= 0) {
751 dev_info(&chip
->client
->dev
, "Setting Up Interrupt(s)\n");
753 reg_val
= TSL2X7X_CNTL_PWR_ON
| TSL2X7X_CNTL_ADC_ENBL
;
754 if ((chip
->tsl2x7x_settings
.interrupts_en
== 0x20) ||
755 (chip
->tsl2x7x_settings
.interrupts_en
== 0x30))
756 reg_val
|= TSL2X7X_CNTL_PROX_DET_ENBL
;
758 reg_val
|= chip
->tsl2x7x_settings
.interrupts_en
;
759 ret
= i2c_smbus_write_byte_data(chip
->client
,
760 (TSL2X7X_CMD_REG
| TSL2X7X_CNTRL
), reg_val
);
762 dev_err(&chip
->client
->dev
,
763 "%s: failed in tsl2x7x_IOCTL_INT_SET.\n",
766 /* Clear out any initial interrupts */
767 ret
= i2c_smbus_write_byte(chip
->client
,
768 TSL2X7X_CMD_REG
| TSL2X7X_CMD_SPL_FN
|
769 TSL2X7X_CMD_PROXALS_INT_CLR
);
771 dev_err(&chip
->client
->dev
,
772 "%s: Failed to clear Int status\n",
781 static int tsl2x7x_chip_off(struct iio_dev
*indio_dev
)
784 struct tsl2X7X_chip
*chip
= iio_priv(indio_dev
);
786 /* turn device off */
787 chip
->tsl2x7x_chip_status
= TSL2X7X_CHIP_SUSPENDED
;
789 ret
= i2c_smbus_write_byte_data(chip
->client
,
790 TSL2X7X_CMD_REG
| TSL2X7X_CNTRL
, 0x00);
792 if (chip
->pdata
&& chip
->pdata
->power_off
)
793 chip
->pdata
->power_off(chip
->client
);
799 * tsl2x7x_invoke_change
800 * @indio_dev: pointer to IIO device
802 * Obtain and lock both ALS and PROX resources,
803 * determine and save device state (On/Off),
804 * cycle device to implement updated parameter,
805 * put device back into proper state, and unlock
809 int tsl2x7x_invoke_change(struct iio_dev
*indio_dev
)
811 struct tsl2X7X_chip
*chip
= iio_priv(indio_dev
);
812 int device_status
= chip
->tsl2x7x_chip_status
;
814 mutex_lock(&chip
->als_mutex
);
815 mutex_lock(&chip
->prox_mutex
);
817 if (device_status
== TSL2X7X_CHIP_WORKING
)
818 tsl2x7x_chip_off(indio_dev
);
820 tsl2x7x_chip_on(indio_dev
);
822 if (device_status
!= TSL2X7X_CHIP_WORKING
)
823 tsl2x7x_chip_off(indio_dev
);
825 mutex_unlock(&chip
->prox_mutex
);
826 mutex_unlock(&chip
->als_mutex
);
832 void tsl2x7x_prox_calculate(int *data
, int length
,
833 struct tsl2x7x_prox_stat
*statP
)
843 statP
->min
= INT_MAX
;
844 statP
->max
= INT_MIN
;
845 for (i
= 0; i
< length
; i
++) {
846 sample_sum
+= data
[i
];
847 statP
->min
= min(statP
->min
, data
[i
]);
848 statP
->max
= max(statP
->max
, data
[i
]);
851 statP
->mean
= sample_sum
/ length
;
853 for (i
= 0; i
< length
; i
++) {
854 tmp
= data
[i
] - statP
->mean
;
855 sample_sum
+= tmp
* tmp
;
857 statP
->stddev
= int_sqrt((long)sample_sum
)/length
;
861 * tsl2x7x_prox_cal() - Calculates std. and sets thresholds.
862 * @indio_dev: pointer to IIO device
864 * Calculates a standard deviation based on the samples,
865 * and sets the threshold accordingly.
867 static void tsl2x7x_prox_cal(struct iio_dev
*indio_dev
)
869 int prox_history
[MAX_SAMPLES_CAL
+ 1];
871 struct tsl2x7x_prox_stat prox_stat_data
[2];
872 struct tsl2x7x_prox_stat
*calP
;
873 struct tsl2X7X_chip
*chip
= iio_priv(indio_dev
);
875 u8 current_state
= chip
->tsl2x7x_chip_status
;
877 if (chip
->tsl2x7x_settings
.prox_max_samples_cal
> MAX_SAMPLES_CAL
) {
878 dev_err(&chip
->client
->dev
,
879 "%s: max prox samples cal is too big: %d\n",
880 __func__
, chip
->tsl2x7x_settings
.prox_max_samples_cal
);
881 chip
->tsl2x7x_settings
.prox_max_samples_cal
= MAX_SAMPLES_CAL
;
884 /* have to stop to change settings */
885 tsl2x7x_chip_off(indio_dev
);
887 /* Enable proximity detection save just in case prox not wanted yet*/
888 tmp_irq_settings
= chip
->tsl2x7x_settings
.interrupts_en
;
889 chip
->tsl2x7x_settings
.interrupts_en
|= TSL2X7X_CNTL_PROX_INT_ENBL
;
891 /*turn on device if not already on*/
892 tsl2x7x_chip_on(indio_dev
);
894 /*gather the samples*/
895 for (i
= 0; i
< chip
->tsl2x7x_settings
.prox_max_samples_cal
; i
++) {
897 tsl2x7x_get_prox(indio_dev
);
898 prox_history
[i
] = chip
->prox_data
;
899 dev_info(&chip
->client
->dev
, "2 i=%d prox data= %d\n",
903 tsl2x7x_chip_off(indio_dev
);
904 calP
= &prox_stat_data
[PROX_STAT_CAL
];
905 tsl2x7x_prox_calculate(prox_history
,
906 chip
->tsl2x7x_settings
.prox_max_samples_cal
, calP
);
907 chip
->tsl2x7x_settings
.prox_thres_high
= (calP
->max
<< 1) - calP
->mean
;
909 dev_info(&chip
->client
->dev
, " cal min=%d mean=%d max=%d\n",
910 calP
->min
, calP
->mean
, calP
->max
);
911 dev_info(&chip
->client
->dev
,
912 "%s proximity threshold set to %d\n",
913 chip
->client
->name
, chip
->tsl2x7x_settings
.prox_thres_high
);
915 /* back to the way they were */
916 chip
->tsl2x7x_settings
.interrupts_en
= tmp_irq_settings
;
917 if (current_state
== TSL2X7X_CHIP_WORKING
)
918 tsl2x7x_chip_on(indio_dev
);
921 static ssize_t
tsl2x7x_power_state_show(struct device
*dev
,
922 struct device_attribute
*attr
, char *buf
)
924 struct tsl2X7X_chip
*chip
= iio_priv(dev_to_iio_dev(dev
));
926 return snprintf(buf
, PAGE_SIZE
, "%d\n", chip
->tsl2x7x_chip_status
);
929 static ssize_t
tsl2x7x_power_state_store(struct device
*dev
,
930 struct device_attribute
*attr
, const char *buf
, size_t len
)
932 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
935 if (strtobool(buf
, &value
))
939 tsl2x7x_chip_on(indio_dev
);
941 tsl2x7x_chip_off(indio_dev
);
946 static ssize_t
tsl2x7x_gain_available_show(struct device
*dev
,
947 struct device_attribute
*attr
, char *buf
)
949 struct tsl2X7X_chip
*chip
= iio_priv(dev_to_iio_dev(dev
));
957 return snprintf(buf
, PAGE_SIZE
, "%s\n", "1 8 16 128");
961 return snprintf(buf
, PAGE_SIZE
, "%s\n", "1 8 16 120");
964 static ssize_t
tsl2x7x_prox_gain_available_show(struct device
*dev
,
965 struct device_attribute
*attr
, char *buf
)
967 return snprintf(buf
, PAGE_SIZE
, "%s\n", "1 2 4 8");
970 static ssize_t
tsl2x7x_als_time_show(struct device
*dev
,
971 struct device_attribute
*attr
, char *buf
)
973 struct tsl2X7X_chip
*chip
= iio_priv(dev_to_iio_dev(dev
));
976 y
= (TSL2X7X_MAX_TIMER_CNT
- (u8
)chip
->tsl2x7x_settings
.als_time
) + 1;
977 z
= y
* TSL2X7X_MIN_ITIME
;
981 return snprintf(buf
, PAGE_SIZE
, "%d.%03d\n", y
, z
);
984 static ssize_t
tsl2x7x_als_time_store(struct device
*dev
,
985 struct device_attribute
*attr
, const char *buf
, size_t len
)
987 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
988 struct tsl2X7X_chip
*chip
= iio_priv(indio_dev
);
989 struct tsl2x7x_parse_result result
;
992 ret
= iio_str_to_fixpoint(buf
, 100, &result
.integer
, &result
.fract
);
997 chip
->tsl2x7x_settings
.als_time
=
998 (TSL2X7X_MAX_TIMER_CNT
- (u8
)result
.fract
);
1000 dev_info(&chip
->client
->dev
, "%s: als time = %d",
1001 __func__
, chip
->tsl2x7x_settings
.als_time
);
1003 tsl2x7x_invoke_change(indio_dev
);
1005 return IIO_VAL_INT_PLUS_MICRO
;
1008 static IIO_CONST_ATTR(in_illuminance0_integration_time_available
,
1011 static ssize_t
tsl2x7x_als_cal_target_show(struct device
*dev
,
1012 struct device_attribute
*attr
, char *buf
)
1014 struct tsl2X7X_chip
*chip
= iio_priv(dev_to_iio_dev(dev
));
1016 return snprintf(buf
, PAGE_SIZE
, "%d\n",
1017 chip
->tsl2x7x_settings
.als_cal_target
);
1020 static ssize_t
tsl2x7x_als_cal_target_store(struct device
*dev
,
1021 struct device_attribute
*attr
, const char *buf
, size_t len
)
1023 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
1024 struct tsl2X7X_chip
*chip
= iio_priv(indio_dev
);
1025 unsigned long value
;
1027 if (kstrtoul(buf
, 0, &value
))
1031 chip
->tsl2x7x_settings
.als_cal_target
= value
;
1033 tsl2x7x_invoke_change(indio_dev
);
1038 /* persistence settings */
1039 static ssize_t
tsl2x7x_als_persistence_show(struct device
*dev
,
1040 struct device_attribute
*attr
, char *buf
)
1042 struct tsl2X7X_chip
*chip
= iio_priv(dev_to_iio_dev(dev
));
1043 int y
, z
, filter_delay
;
1045 /* Determine integration time */
1046 y
= (TSL2X7X_MAX_TIMER_CNT
- (u8
)chip
->tsl2x7x_settings
.als_time
) + 1;
1047 z
= y
* TSL2X7X_MIN_ITIME
;
1048 filter_delay
= z
* (chip
->tsl2x7x_settings
.persistence
& 0x0F);
1049 y
= (filter_delay
/ 1000);
1050 z
= (filter_delay
% 1000);
1052 return snprintf(buf
, PAGE_SIZE
, "%d.%03d\n", y
, z
);
1055 static ssize_t
tsl2x7x_als_persistence_store(struct device
*dev
,
1056 struct device_attribute
*attr
, const char *buf
, size_t len
)
1058 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
1059 struct tsl2X7X_chip
*chip
= iio_priv(indio_dev
);
1060 struct tsl2x7x_parse_result result
;
1061 int y
, z
, filter_delay
;
1064 ret
= iio_str_to_fixpoint(buf
, 100, &result
.integer
, &result
.fract
);
1068 y
= (TSL2X7X_MAX_TIMER_CNT
- (u8
)chip
->tsl2x7x_settings
.als_time
) + 1;
1069 z
= y
* TSL2X7X_MIN_ITIME
;
1072 DIV_ROUND_UP(((result
.integer
* 1000) + result
.fract
), z
);
1074 chip
->tsl2x7x_settings
.persistence
&= 0xF0;
1075 chip
->tsl2x7x_settings
.persistence
|= (filter_delay
& 0x0F);
1077 dev_info(&chip
->client
->dev
, "%s: als persistence = %d",
1078 __func__
, filter_delay
);
1080 tsl2x7x_invoke_change(indio_dev
);
1082 return IIO_VAL_INT_PLUS_MICRO
;
1085 static ssize_t
tsl2x7x_prox_persistence_show(struct device
*dev
,
1086 struct device_attribute
*attr
, char *buf
)
1088 struct tsl2X7X_chip
*chip
= iio_priv(dev_to_iio_dev(dev
));
1089 int y
, z
, filter_delay
;
1091 /* Determine integration time */
1092 y
= (TSL2X7X_MAX_TIMER_CNT
- (u8
)chip
->tsl2x7x_settings
.prx_time
) + 1;
1093 z
= y
* TSL2X7X_MIN_ITIME
;
1094 filter_delay
= z
* ((chip
->tsl2x7x_settings
.persistence
& 0xF0) >> 4);
1095 y
= (filter_delay
/ 1000);
1096 z
= (filter_delay
% 1000);
1098 return snprintf(buf
, PAGE_SIZE
, "%d.%03d\n", y
, z
);
1101 static ssize_t
tsl2x7x_prox_persistence_store(struct device
*dev
,
1102 struct device_attribute
*attr
, const char *buf
, size_t len
)
1104 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
1105 struct tsl2X7X_chip
*chip
= iio_priv(indio_dev
);
1106 struct tsl2x7x_parse_result result
;
1107 int y
, z
, filter_delay
;
1110 ret
= iio_str_to_fixpoint(buf
, 100, &result
.integer
, &result
.fract
);
1114 y
= (TSL2X7X_MAX_TIMER_CNT
- (u8
)chip
->tsl2x7x_settings
.prx_time
) + 1;
1115 z
= y
* TSL2X7X_MIN_ITIME
;
1118 DIV_ROUND_UP(((result
.integer
* 1000) + result
.fract
), z
);
1120 chip
->tsl2x7x_settings
.persistence
&= 0x0F;
1121 chip
->tsl2x7x_settings
.persistence
|= ((filter_delay
<< 4) & 0xF0);
1123 dev_info(&chip
->client
->dev
, "%s: prox persistence = %d",
1124 __func__
, filter_delay
);
1126 tsl2x7x_invoke_change(indio_dev
);
1128 return IIO_VAL_INT_PLUS_MICRO
;
1131 static ssize_t
tsl2x7x_do_calibrate(struct device
*dev
,
1132 struct device_attribute
*attr
, const char *buf
, size_t len
)
1134 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
1137 if (strtobool(buf
, &value
))
1141 tsl2x7x_als_calibrate(indio_dev
);
1143 tsl2x7x_invoke_change(indio_dev
);
1148 static ssize_t
tsl2x7x_luxtable_show(struct device
*dev
,
1149 struct device_attribute
*attr
, char *buf
)
1151 struct tsl2X7X_chip
*chip
= iio_priv(dev_to_iio_dev(dev
));
1155 while (i
< (TSL2X7X_MAX_LUX_TABLE_SIZE
* 3)) {
1156 offset
+= snprintf(buf
+ offset
, PAGE_SIZE
, "%d,%d,%d,",
1157 chip
->tsl2x7x_device_lux
[i
].ratio
,
1158 chip
->tsl2x7x_device_lux
[i
].ch0
,
1159 chip
->tsl2x7x_device_lux
[i
].ch1
);
1160 if (chip
->tsl2x7x_device_lux
[i
].ratio
== 0) {
1161 /* We just printed the first "0" entry.
1162 * Now get rid of the extra "," and break. */
1169 offset
+= snprintf(buf
+ offset
, PAGE_SIZE
, "\n");
1173 static ssize_t
tsl2x7x_luxtable_store(struct device
*dev
,
1174 struct device_attribute
*attr
, const char *buf
, size_t len
)
1176 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
1177 struct tsl2X7X_chip
*chip
= iio_priv(indio_dev
);
1178 int value
[ARRAY_SIZE(chip
->tsl2x7x_device_lux
)*3 + 1];
1181 get_options(buf
, ARRAY_SIZE(value
), value
);
1183 /* We now have an array of ints starting at value[1], and
1184 * enumerated by value[0].
1185 * We expect each group of three ints is one table entry,
1186 * and the last table entry is all 0.
1189 if ((n
% 3) || n
< 6 ||
1190 n
> ((ARRAY_SIZE(chip
->tsl2x7x_device_lux
) - 1) * 3)) {
1191 dev_info(dev
, "LUX TABLE INPUT ERROR 1 Value[0]=%d\n", n
);
1195 if ((value
[(n
- 2)] | value
[(n
- 1)] | value
[n
]) != 0) {
1196 dev_info(dev
, "LUX TABLE INPUT ERROR 2 Value[0]=%d\n", n
);
1200 if (chip
->tsl2x7x_chip_status
== TSL2X7X_CHIP_WORKING
)
1201 tsl2x7x_chip_off(indio_dev
);
1203 /* Zero out the table */
1204 memset(chip
->tsl2x7x_device_lux
, 0, sizeof(chip
->tsl2x7x_device_lux
));
1205 memcpy(chip
->tsl2x7x_device_lux
, &value
[1], (value
[0] * 4));
1207 tsl2x7x_invoke_change(indio_dev
);
1212 static ssize_t
tsl2x7x_do_prox_calibrate(struct device
*dev
,
1213 struct device_attribute
*attr
, const char *buf
, size_t len
)
1215 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
1218 if (strtobool(buf
, &value
))
1222 tsl2x7x_prox_cal(indio_dev
);
1224 tsl2x7x_invoke_change(indio_dev
);
1229 static int tsl2x7x_read_interrupt_config(struct iio_dev
*indio_dev
,
1232 struct tsl2X7X_chip
*chip
= iio_priv(indio_dev
);
1235 if (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code
) == IIO_INTENSITY
)
1236 ret
= !!(chip
->tsl2x7x_settings
.interrupts_en
& 0x10);
1238 ret
= !!(chip
->tsl2x7x_settings
.interrupts_en
& 0x20);
1243 static int tsl2x7x_write_interrupt_config(struct iio_dev
*indio_dev
,
1247 struct tsl2X7X_chip
*chip
= iio_priv(indio_dev
);
1249 if (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code
) == IIO_INTENSITY
) {
1251 chip
->tsl2x7x_settings
.interrupts_en
|= 0x10;
1253 chip
->tsl2x7x_settings
.interrupts_en
&= 0x20;
1256 chip
->tsl2x7x_settings
.interrupts_en
|= 0x20;
1258 chip
->tsl2x7x_settings
.interrupts_en
&= 0x10;
1261 tsl2x7x_invoke_change(indio_dev
);
1266 static int tsl2x7x_write_thresh(struct iio_dev
*indio_dev
,
1270 struct tsl2X7X_chip
*chip
= iio_priv(indio_dev
);
1272 if (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code
) == IIO_INTENSITY
) {
1273 switch (IIO_EVENT_CODE_EXTRACT_DIR(event_code
)) {
1274 case IIO_EV_DIR_RISING
:
1275 chip
->tsl2x7x_settings
.als_thresh_high
= val
;
1277 case IIO_EV_DIR_FALLING
:
1278 chip
->tsl2x7x_settings
.als_thresh_low
= val
;
1284 switch (IIO_EVENT_CODE_EXTRACT_DIR(event_code
)) {
1285 case IIO_EV_DIR_RISING
:
1286 chip
->tsl2x7x_settings
.prox_thres_high
= val
;
1288 case IIO_EV_DIR_FALLING
:
1289 chip
->tsl2x7x_settings
.prox_thres_low
= val
;
1296 tsl2x7x_invoke_change(indio_dev
);
1301 static int tsl2x7x_read_thresh(struct iio_dev
*indio_dev
,
1305 struct tsl2X7X_chip
*chip
= iio_priv(indio_dev
);
1307 if (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code
) == IIO_INTENSITY
) {
1308 switch (IIO_EVENT_CODE_EXTRACT_DIR(event_code
)) {
1309 case IIO_EV_DIR_RISING
:
1310 *val
= chip
->tsl2x7x_settings
.als_thresh_high
;
1312 case IIO_EV_DIR_FALLING
:
1313 *val
= chip
->tsl2x7x_settings
.als_thresh_low
;
1319 switch (IIO_EVENT_CODE_EXTRACT_DIR(event_code
)) {
1320 case IIO_EV_DIR_RISING
:
1321 *val
= chip
->tsl2x7x_settings
.prox_thres_high
;
1323 case IIO_EV_DIR_FALLING
:
1324 *val
= chip
->tsl2x7x_settings
.prox_thres_low
;
1334 static int tsl2x7x_read_raw(struct iio_dev
*indio_dev
,
1335 struct iio_chan_spec
const *chan
,
1341 struct tsl2X7X_chip
*chip
= iio_priv(indio_dev
);
1344 case IIO_CHAN_INFO_PROCESSED
:
1345 switch (chan
->type
) {
1347 tsl2x7x_get_lux(indio_dev
);
1348 *val
= chip
->als_cur_info
.lux
;
1356 case IIO_CHAN_INFO_RAW
:
1357 switch (chan
->type
) {
1359 tsl2x7x_get_lux(indio_dev
);
1360 if (chan
->channel
== 0)
1361 *val
= chip
->als_cur_info
.als_ch0
;
1363 *val
= chip
->als_cur_info
.als_ch1
;
1367 tsl2x7x_get_prox(indio_dev
);
1368 *val
= chip
->prox_data
;
1376 case IIO_CHAN_INFO_CALIBSCALE
:
1377 if (chan
->type
== IIO_LIGHT
)
1379 tsl2X7X_als_gainadj
[chip
->tsl2x7x_settings
.als_gain
];
1382 tsl2X7X_prx_gainadj
[chip
->tsl2x7x_settings
.prox_gain
];
1385 case IIO_CHAN_INFO_CALIBBIAS
:
1386 *val
= chip
->tsl2x7x_settings
.als_gain_trim
;
1397 static int tsl2x7x_write_raw(struct iio_dev
*indio_dev
,
1398 struct iio_chan_spec
const *chan
,
1403 struct tsl2X7X_chip
*chip
= iio_priv(indio_dev
);
1406 case IIO_CHAN_INFO_CALIBSCALE
:
1407 if (chan
->type
== IIO_INTENSITY
) {
1410 chip
->tsl2x7x_settings
.als_gain
= 0;
1413 chip
->tsl2x7x_settings
.als_gain
= 1;
1416 chip
->tsl2x7x_settings
.als_gain
= 2;
1428 chip
->tsl2x7x_settings
.als_gain
= 3;
1440 chip
->tsl2x7x_settings
.als_gain
= 3;
1448 chip
->tsl2x7x_settings
.prox_gain
= 0;
1451 chip
->tsl2x7x_settings
.prox_gain
= 1;
1454 chip
->tsl2x7x_settings
.prox_gain
= 2;
1457 chip
->tsl2x7x_settings
.prox_gain
= 3;
1464 case IIO_CHAN_INFO_CALIBBIAS
:
1465 chip
->tsl2x7x_settings
.als_gain_trim
= val
;
1472 tsl2x7x_invoke_change(indio_dev
);
1477 static DEVICE_ATTR(power_state
, S_IRUGO
| S_IWUSR
,
1478 tsl2x7x_power_state_show
, tsl2x7x_power_state_store
);
1480 static DEVICE_ATTR(in_proximity0_calibscale_available
, S_IRUGO
,
1481 tsl2x7x_prox_gain_available_show
, NULL
);
1483 static DEVICE_ATTR(in_illuminance0_calibscale_available
, S_IRUGO
,
1484 tsl2x7x_gain_available_show
, NULL
);
1486 static DEVICE_ATTR(in_illuminance0_integration_time
, S_IRUGO
| S_IWUSR
,
1487 tsl2x7x_als_time_show
, tsl2x7x_als_time_store
);
1489 static DEVICE_ATTR(in_illuminance0_target_input
, S_IRUGO
| S_IWUSR
,
1490 tsl2x7x_als_cal_target_show
, tsl2x7x_als_cal_target_store
);
1492 static DEVICE_ATTR(in_illuminance0_calibrate
, S_IWUSR
, NULL
,
1493 tsl2x7x_do_calibrate
);
1495 static DEVICE_ATTR(in_proximity0_calibrate
, S_IWUSR
, NULL
,
1496 tsl2x7x_do_prox_calibrate
);
1498 static DEVICE_ATTR(in_illuminance0_lux_table
, S_IRUGO
| S_IWUSR
,
1499 tsl2x7x_luxtable_show
, tsl2x7x_luxtable_store
);
1501 static DEVICE_ATTR(in_intensity0_thresh_period
, S_IRUGO
| S_IWUSR
,
1502 tsl2x7x_als_persistence_show
, tsl2x7x_als_persistence_store
);
1504 static DEVICE_ATTR(in_proximity0_thresh_period
, S_IRUGO
| S_IWUSR
,
1505 tsl2x7x_prox_persistence_show
, tsl2x7x_prox_persistence_store
);
1507 /* Use the default register values to identify the Taos device */
1508 static int tsl2x7x_device_id(unsigned char *id
, int target
)
1514 return ((*id
& 0xf0) == TRITON_ID
);
1518 return ((*id
& 0xf0) == HALIBUT_ID
);
1525 return ((*id
& 0xf0) == SWORDFISH_ID
);
1532 static irqreturn_t
tsl2x7x_event_handler(int irq
, void *private)
1534 struct iio_dev
*indio_dev
= private;
1535 struct tsl2X7X_chip
*chip
= iio_priv(indio_dev
);
1536 s64 timestamp
= iio_get_time_ns();
1540 value
= i2c_smbus_read_byte_data(chip
->client
,
1541 TSL2X7X_CMD_REG
| TSL2X7X_STATUS
);
1543 /* What type of interrupt do we need to process */
1544 if (value
& TSL2X7X_STA_PRX_INTR
) {
1545 tsl2x7x_get_prox(indio_dev
); /* freshen data for ABI */
1546 iio_push_event(indio_dev
,
1547 IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY
,
1554 if (value
& TSL2X7X_STA_ALS_INTR
) {
1555 tsl2x7x_get_lux(indio_dev
); /* freshen data for ABI */
1556 iio_push_event(indio_dev
,
1557 IIO_UNMOD_EVENT_CODE(IIO_LIGHT
,
1563 /* Clear interrupt now that we have handled it. */
1564 ret
= i2c_smbus_write_byte(chip
->client
,
1565 TSL2X7X_CMD_REG
| TSL2X7X_CMD_SPL_FN
|
1566 TSL2X7X_CMD_PROXALS_INT_CLR
);
1568 dev_err(&chip
->client
->dev
,
1569 "%s: Failed to clear irq from event handler. err = %d\n",
1575 static struct attribute
*tsl2x7x_ALS_device_attrs
[] = {
1576 &dev_attr_power_state
.attr
,
1577 &dev_attr_in_illuminance0_calibscale_available
.attr
,
1578 &dev_attr_in_illuminance0_integration_time
.attr
,
1579 &iio_const_attr_in_illuminance0_integration_time_available\
1581 &dev_attr_in_illuminance0_target_input
.attr
,
1582 &dev_attr_in_illuminance0_calibrate
.attr
,
1583 &dev_attr_in_illuminance0_lux_table
.attr
,
1587 static struct attribute
*tsl2x7x_PRX_device_attrs
[] = {
1588 &dev_attr_power_state
.attr
,
1589 &dev_attr_in_proximity0_calibrate
.attr
,
1593 static struct attribute
*tsl2x7x_ALSPRX_device_attrs
[] = {
1594 &dev_attr_power_state
.attr
,
1595 &dev_attr_in_illuminance0_calibscale_available
.attr
,
1596 &dev_attr_in_illuminance0_integration_time
.attr
,
1597 &iio_const_attr_in_illuminance0_integration_time_available\
1599 &dev_attr_in_illuminance0_target_input
.attr
,
1600 &dev_attr_in_illuminance0_calibrate
.attr
,
1601 &dev_attr_in_illuminance0_lux_table
.attr
,
1602 &dev_attr_in_proximity0_calibrate
.attr
,
1606 static struct attribute
*tsl2x7x_PRX2_device_attrs
[] = {
1607 &dev_attr_power_state
.attr
,
1608 &dev_attr_in_proximity0_calibrate
.attr
,
1609 &dev_attr_in_proximity0_calibscale_available
.attr
,
1613 static struct attribute
*tsl2x7x_ALSPRX2_device_attrs
[] = {
1614 &dev_attr_power_state
.attr
,
1615 &dev_attr_in_illuminance0_calibscale_available
.attr
,
1616 &dev_attr_in_illuminance0_integration_time
.attr
,
1617 &iio_const_attr_in_illuminance0_integration_time_available\
1619 &dev_attr_in_illuminance0_target_input
.attr
,
1620 &dev_attr_in_illuminance0_calibrate
.attr
,
1621 &dev_attr_in_illuminance0_lux_table
.attr
,
1622 &dev_attr_in_proximity0_calibrate
.attr
,
1623 &dev_attr_in_proximity0_calibscale_available
.attr
,
1627 static struct attribute
*tsl2X7X_ALS_event_attrs
[] = {
1628 &dev_attr_in_intensity0_thresh_period
.attr
,
1631 static struct attribute
*tsl2X7X_PRX_event_attrs
[] = {
1632 &dev_attr_in_proximity0_thresh_period
.attr
,
1636 static struct attribute
*tsl2X7X_ALSPRX_event_attrs
[] = {
1637 &dev_attr_in_intensity0_thresh_period
.attr
,
1638 &dev_attr_in_proximity0_thresh_period
.attr
,
1642 static const struct attribute_group tsl2X7X_device_attr_group_tbl
[] = {
1644 .attrs
= tsl2x7x_ALS_device_attrs
,
1647 .attrs
= tsl2x7x_PRX_device_attrs
,
1650 .attrs
= tsl2x7x_ALSPRX_device_attrs
,
1653 .attrs
= tsl2x7x_PRX2_device_attrs
,
1656 .attrs
= tsl2x7x_ALSPRX2_device_attrs
,
1660 static struct attribute_group tsl2X7X_event_attr_group_tbl
[] = {
1662 .attrs
= tsl2X7X_ALS_event_attrs
,
1666 .attrs
= tsl2X7X_PRX_event_attrs
,
1670 .attrs
= tsl2X7X_ALSPRX_event_attrs
,
1675 static const struct iio_info tsl2X7X_device_info
[] = {
1677 .attrs
= &tsl2X7X_device_attr_group_tbl
[ALS
],
1678 .event_attrs
= &tsl2X7X_event_attr_group_tbl
[ALS
],
1679 .driver_module
= THIS_MODULE
,
1680 .read_raw
= &tsl2x7x_read_raw
,
1681 .write_raw
= &tsl2x7x_write_raw
,
1682 .read_event_value
= &tsl2x7x_read_thresh
,
1683 .write_event_value
= &tsl2x7x_write_thresh
,
1684 .read_event_config
= &tsl2x7x_read_interrupt_config
,
1685 .write_event_config
= &tsl2x7x_write_interrupt_config
,
1688 .attrs
= &tsl2X7X_device_attr_group_tbl
[PRX
],
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_thresh
,
1694 .write_event_value
= &tsl2x7x_write_thresh
,
1695 .read_event_config
= &tsl2x7x_read_interrupt_config
,
1696 .write_event_config
= &tsl2x7x_write_interrupt_config
,
1699 .attrs
= &tsl2X7X_device_attr_group_tbl
[ALSPRX
],
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_thresh
,
1705 .write_event_value
= &tsl2x7x_write_thresh
,
1706 .read_event_config
= &tsl2x7x_read_interrupt_config
,
1707 .write_event_config
= &tsl2x7x_write_interrupt_config
,
1710 .attrs
= &tsl2X7X_device_attr_group_tbl
[PRX2
],
1711 .event_attrs
= &tsl2X7X_event_attr_group_tbl
[PRX
],
1712 .driver_module
= THIS_MODULE
,
1713 .read_raw
= &tsl2x7x_read_raw
,
1714 .write_raw
= &tsl2x7x_write_raw
,
1715 .read_event_value
= &tsl2x7x_read_thresh
,
1716 .write_event_value
= &tsl2x7x_write_thresh
,
1717 .read_event_config
= &tsl2x7x_read_interrupt_config
,
1718 .write_event_config
= &tsl2x7x_write_interrupt_config
,
1721 .attrs
= &tsl2X7X_device_attr_group_tbl
[ALSPRX2
],
1722 .event_attrs
= &tsl2X7X_event_attr_group_tbl
[ALSPRX
],
1723 .driver_module
= THIS_MODULE
,
1724 .read_raw
= &tsl2x7x_read_raw
,
1725 .write_raw
= &tsl2x7x_write_raw
,
1726 .read_event_value
= &tsl2x7x_read_thresh
,
1727 .write_event_value
= &tsl2x7x_write_thresh
,
1728 .read_event_config
= &tsl2x7x_read_interrupt_config
,
1729 .write_event_config
= &tsl2x7x_write_interrupt_config
,
1733 static const struct tsl2x7x_chip_info tsl2x7x_chip_info_tbl
[] = {
1740 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
1742 .type
= IIO_INTENSITY
,
1745 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1746 BIT(IIO_CHAN_INFO_CALIBSCALE
) |
1747 BIT(IIO_CHAN_INFO_CALIBBIAS
),
1748 .event_mask
= TSL2X7X_EVENT_MASK
1750 .type
= IIO_INTENSITY
,
1755 .chan_table_elements
= 3,
1756 .info
= &tsl2X7X_device_info
[ALS
],
1761 .type
= IIO_PROXIMITY
,
1764 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
1765 .event_mask
= TSL2X7X_EVENT_MASK
1768 .chan_table_elements
= 1,
1769 .info
= &tsl2X7X_device_info
[PRX
],
1777 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
)
1779 .type
= IIO_INTENSITY
,
1782 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1783 BIT(IIO_CHAN_INFO_CALIBSCALE
) |
1784 BIT(IIO_CHAN_INFO_CALIBBIAS
),
1785 .event_mask
= TSL2X7X_EVENT_MASK
1787 .type
= IIO_INTENSITY
,
1790 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
1792 .type
= IIO_PROXIMITY
,
1795 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
1796 .event_mask
= TSL2X7X_EVENT_MASK
1799 .chan_table_elements
= 4,
1800 .info
= &tsl2X7X_device_info
[ALSPRX
],
1805 .type
= IIO_PROXIMITY
,
1808 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1809 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1810 .event_mask
= TSL2X7X_EVENT_MASK
1813 .chan_table_elements
= 1,
1814 .info
= &tsl2X7X_device_info
[PRX2
],
1822 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
1824 .type
= IIO_INTENSITY
,
1827 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1828 BIT(IIO_CHAN_INFO_CALIBSCALE
) |
1829 BIT(IIO_CHAN_INFO_CALIBBIAS
),
1830 .event_mask
= TSL2X7X_EVENT_MASK
1832 .type
= IIO_INTENSITY
,
1835 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
1837 .type
= IIO_PROXIMITY
,
1840 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1841 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1842 .event_mask
= TSL2X7X_EVENT_MASK
1845 .chan_table_elements
= 4,
1846 .info
= &tsl2X7X_device_info
[ALSPRX2
],
1850 static int tsl2x7x_probe(struct i2c_client
*clientp
,
1851 const struct i2c_device_id
*id
)
1854 unsigned char device_id
;
1855 struct iio_dev
*indio_dev
;
1856 struct tsl2X7X_chip
*chip
;
1858 indio_dev
= iio_device_alloc(sizeof(*chip
));
1862 chip
= iio_priv(indio_dev
);
1863 chip
->client
= clientp
;
1864 i2c_set_clientdata(clientp
, indio_dev
);
1866 ret
= tsl2x7x_i2c_read(chip
->client
,
1867 TSL2X7X_CHIPID
, &device_id
);
1871 if ((!tsl2x7x_device_id(&device_id
, id
->driver_data
)) ||
1872 (tsl2x7x_device_id(&device_id
, id
->driver_data
) == -EINVAL
)) {
1873 dev_info(&chip
->client
->dev
,
1874 "%s: i2c device found does not match expected id\n",
1880 ret
= i2c_smbus_write_byte(clientp
, (TSL2X7X_CMD_REG
| TSL2X7X_CNTRL
));
1882 dev_err(&clientp
->dev
, "%s: write to cmd reg failed. err = %d\n",
1887 /* ALS and PROX functions can be invoked via user space poll
1888 * or H/W interrupt. If busy return last sample. */
1889 mutex_init(&chip
->als_mutex
);
1890 mutex_init(&chip
->prox_mutex
);
1892 chip
->tsl2x7x_chip_status
= TSL2X7X_CHIP_UNKNOWN
;
1893 chip
->pdata
= clientp
->dev
.platform_data
;
1894 chip
->id
= id
->driver_data
;
1896 &tsl2x7x_chip_info_tbl
[device_channel_config
[id
->driver_data
]];
1898 indio_dev
->info
= chip
->chip_info
->info
;
1899 indio_dev
->dev
.parent
= &clientp
->dev
;
1900 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1901 indio_dev
->name
= chip
->client
->name
;
1902 indio_dev
->channels
= chip
->chip_info
->channel
;
1903 indio_dev
->num_channels
= chip
->chip_info
->chan_table_elements
;
1906 ret
= request_threaded_irq(clientp
->irq
,
1908 &tsl2x7x_event_handler
,
1909 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
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
);
1937 free_irq(clientp
->irq
, indio_dev
);
1939 iio_device_free(indio_dev
);
1944 static int tsl2x7x_suspend(struct device
*dev
)
1946 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1947 struct tsl2X7X_chip
*chip
= iio_priv(indio_dev
);
1950 if (chip
->tsl2x7x_chip_status
== TSL2X7X_CHIP_WORKING
) {
1951 ret
= tsl2x7x_chip_off(indio_dev
);
1952 chip
->tsl2x7x_chip_status
= TSL2X7X_CHIP_SUSPENDED
;
1955 if (chip
->pdata
&& chip
->pdata
->platform_power
) {
1956 pm_message_t pmm
= {PM_EVENT_SUSPEND
};
1957 chip
->pdata
->platform_power(dev
, pmm
);
1963 static int tsl2x7x_resume(struct device
*dev
)
1965 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1966 struct tsl2X7X_chip
*chip
= iio_priv(indio_dev
);
1969 if (chip
->pdata
&& chip
->pdata
->platform_power
) {
1970 pm_message_t pmm
= {PM_EVENT_RESUME
};
1971 chip
->pdata
->platform_power(dev
, pmm
);
1974 if (chip
->tsl2x7x_chip_status
== TSL2X7X_CHIP_SUSPENDED
)
1975 ret
= tsl2x7x_chip_on(indio_dev
);
1980 static int tsl2x7x_remove(struct i2c_client
*client
)
1982 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
1984 tsl2x7x_chip_off(indio_dev
);
1986 iio_device_unregister(indio_dev
);
1988 free_irq(client
->irq
, indio_dev
);
1990 iio_device_free(indio_dev
);
1995 static struct i2c_device_id tsl2x7x_idtable
[] = {
1996 { "tsl2571", tsl2571
},
1997 { "tsl2671", tsl2671
},
1998 { "tmd2671", tmd2671
},
1999 { "tsl2771", tsl2771
},
2000 { "tmd2771", tmd2771
},
2001 { "tsl2572", tsl2572
},
2002 { "tsl2672", tsl2672
},
2003 { "tmd2672", tmd2672
},
2004 { "tsl2772", tsl2772
},
2005 { "tmd2772", tmd2772
},
2009 MODULE_DEVICE_TABLE(i2c
, tsl2x7x_idtable
);
2011 static const struct dev_pm_ops tsl2x7x_pm_ops
= {
2012 .suspend
= tsl2x7x_suspend
,
2013 .resume
= tsl2x7x_resume
,
2016 /* Driver definition */
2017 static struct i2c_driver tsl2x7x_driver
= {
2020 .pm
= &tsl2x7x_pm_ops
,
2022 .id_table
= tsl2x7x_idtable
,
2023 .probe
= tsl2x7x_probe
,
2024 .remove
= tsl2x7x_remove
,
2027 module_i2c_driver(tsl2x7x_driver
);
2029 MODULE_AUTHOR("J. August Brenner<jbrenner@taosinc.com>");
2030 MODULE_DESCRIPTION("TAOS tsl2x7x ambient and proximity light sensor driver");
2031 MODULE_LICENSE("GPL");