1 // SPDX-License-Identifier: GPL-2.0+
3 * Device driver for monitoring ambient light intensity in (lux) and proximity
4 * detection (prox) within the TAOS TSL2571, TSL2671, TMD2671, TSL2771, TMD2771,
5 * TSL2572, TSL2672, TMD2672, TSL2772, and TMD2772 devices.
7 * Copyright (c) 2012, TAOS Corporation.
8 * Copyright (c) 2017-2018 Brian Masney <masneyb@onstation.org>
11 #include <linux/delay.h>
12 #include <linux/errno.h>
13 #include <linux/i2c.h>
14 #include <linux/interrupt.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/slab.h>
19 #include <linux/iio/events.h>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/sysfs.h>
22 #include <linux/platform_data/tsl2772.h>
25 #define PROX_STAT_CAL 0
26 #define PROX_STAT_SAMP 1
27 #define MAX_SAMPLES_CAL 200
29 /* TSL2772 Device ID */
30 #define TRITON_ID 0x00
31 #define SWORDFISH_ID 0x30
32 #define HALIBUT_ID 0x20
34 /* Lux calculation constants */
35 #define TSL2772_LUX_CALC_OVER_FLOW 65535
38 * TAOS Register definitions - Note: depending on device, some of these register
39 * are not used and the register address is benign.
42 /* Register offsets */
43 #define TSL2772_MAX_CONFIG_REG 16
45 /* Device Registers and Masks */
46 #define TSL2772_CNTRL 0x00
47 #define TSL2772_ALS_TIME 0X01
48 #define TSL2772_PRX_TIME 0x02
49 #define TSL2772_WAIT_TIME 0x03
50 #define TSL2772_ALS_MINTHRESHLO 0X04
51 #define TSL2772_ALS_MINTHRESHHI 0X05
52 #define TSL2772_ALS_MAXTHRESHLO 0X06
53 #define TSL2772_ALS_MAXTHRESHHI 0X07
54 #define TSL2772_PRX_MINTHRESHLO 0X08
55 #define TSL2772_PRX_MINTHRESHHI 0X09
56 #define TSL2772_PRX_MAXTHRESHLO 0X0A
57 #define TSL2772_PRX_MAXTHRESHHI 0X0B
58 #define TSL2772_PERSISTENCE 0x0C
59 #define TSL2772_ALS_PRX_CONFIG 0x0D
60 #define TSL2772_PRX_COUNT 0x0E
61 #define TSL2772_GAIN 0x0F
62 #define TSL2772_NOTUSED 0x10
63 #define TSL2772_REVID 0x11
64 #define TSL2772_CHIPID 0x12
65 #define TSL2772_STATUS 0x13
66 #define TSL2772_ALS_CHAN0LO 0x14
67 #define TSL2772_ALS_CHAN0HI 0x15
68 #define TSL2772_ALS_CHAN1LO 0x16
69 #define TSL2772_ALS_CHAN1HI 0x17
70 #define TSL2772_PRX_LO 0x18
71 #define TSL2772_PRX_HI 0x19
73 /* tsl2772 cmd reg masks */
74 #define TSL2772_CMD_REG 0x80
75 #define TSL2772_CMD_SPL_FN 0x60
76 #define TSL2772_CMD_REPEAT_PROTO 0x00
77 #define TSL2772_CMD_AUTOINC_PROTO 0x20
79 #define TSL2772_CMD_PROX_INT_CLR 0X05
80 #define TSL2772_CMD_ALS_INT_CLR 0x06
81 #define TSL2772_CMD_PROXALS_INT_CLR 0X07
83 /* tsl2772 cntrl reg masks */
84 #define TSL2772_CNTL_ADC_ENBL 0x02
85 #define TSL2772_CNTL_PWR_ON 0x01
87 /* tsl2772 status reg masks */
88 #define TSL2772_STA_ADC_VALID 0x01
89 #define TSL2772_STA_PRX_VALID 0x02
90 #define TSL2772_STA_ADC_PRX_VALID (TSL2772_STA_ADC_VALID | \
91 TSL2772_STA_PRX_VALID)
92 #define TSL2772_STA_ALS_INTR 0x10
93 #define TSL2772_STA_PRX_INTR 0x20
95 /* tsl2772 cntrl reg masks */
96 #define TSL2772_CNTL_REG_CLEAR 0x00
97 #define TSL2772_CNTL_PROX_INT_ENBL 0X20
98 #define TSL2772_CNTL_ALS_INT_ENBL 0X10
99 #define TSL2772_CNTL_WAIT_TMR_ENBL 0X08
100 #define TSL2772_CNTL_PROX_DET_ENBL 0X04
101 #define TSL2772_CNTL_PWRON 0x01
102 #define TSL2772_CNTL_ALSPON_ENBL 0x03
103 #define TSL2772_CNTL_INTALSPON_ENBL 0x13
104 #define TSL2772_CNTL_PROXPON_ENBL 0x0F
105 #define TSL2772_CNTL_INTPROXPON_ENBL 0x2F
107 #define TSL2772_ALS_GAIN_TRIM_MIN 250
108 #define TSL2772_ALS_GAIN_TRIM_MAX 4000
110 /* Device family members */
125 TSL2772_CHIP_UNKNOWN
= 0,
126 TSL2772_CHIP_WORKING
= 1,
127 TSL2772_CHIP_SUSPENDED
= 2
130 /* Per-device data */
131 struct tsl2772_als_info
{
137 struct tsl2772_chip_info
{
138 int chan_table_elements
;
139 struct iio_chan_spec channel_with_events
[4];
140 struct iio_chan_spec channel_without_events
[4];
141 const struct iio_info
*info
;
144 struct tsl2772_chip
{
146 struct mutex prox_mutex
;
147 struct mutex als_mutex
;
148 struct i2c_client
*client
;
150 struct tsl2772_als_info als_cur_info
;
151 struct tsl2772_settings settings
;
152 struct tsl2772_platform_data
*pdata
;
153 int als_gain_time_scale
;
155 int tsl2772_chip_status
;
156 u8 tsl2772_config
[TSL2772_MAX_CONFIG_REG
];
157 const struct tsl2772_chip_info
*chip_info
;
158 const struct iio_info
*info
;
161 * This structure is intentionally large to accommodate
163 * Sized to 9 = max 8 segments + 1 termination segment
165 struct tsl2772_lux tsl2772_device_lux
[TSL2772_MAX_LUX_TABLE_SIZE
];
169 * Different devices require different coefficents, and these numbers were
170 * derived from the 'Lux Equation' section of the various device datasheets.
171 * All of these coefficients assume a Glass Attenuation (GA) factor of 1.
172 * The coefficients are multiplied by 1000 to avoid floating point operations.
173 * The two rows in each table correspond to the Lux1 and Lux2 equations from
176 static const struct tsl2772_lux tsl2x71_lux_table
[TSL2772_DEF_LUX_TABLE_SZ
] = {
182 static const struct tsl2772_lux tmd2x71_lux_table
[TSL2772_DEF_LUX_TABLE_SZ
] = {
188 static const struct tsl2772_lux tsl2x72_lux_table
[TSL2772_DEF_LUX_TABLE_SZ
] = {
194 static const struct tsl2772_lux tmd2x72_lux_table
[TSL2772_DEF_LUX_TABLE_SZ
] = {
200 static const struct tsl2772_lux
*tsl2772_default_lux_table_group
[] = {
201 [tsl2571
] = tsl2x71_lux_table
,
202 [tsl2671
] = tsl2x71_lux_table
,
203 [tmd2671
] = tmd2x71_lux_table
,
204 [tsl2771
] = tsl2x71_lux_table
,
205 [tmd2771
] = tmd2x71_lux_table
,
206 [tsl2572
] = tsl2x72_lux_table
,
207 [tsl2672
] = tsl2x72_lux_table
,
208 [tmd2672
] = tmd2x72_lux_table
,
209 [tsl2772
] = tsl2x72_lux_table
,
210 [tmd2772
] = tmd2x72_lux_table
,
213 static const struct tsl2772_settings tsl2772_default_settings
= {
214 .als_time
= 255, /* 2.72 / 2.73 ms */
216 .prox_time
= 255, /* 2.72 / 2.73 ms */
219 .als_prox_config
= 0,
220 .als_gain_trim
= 1000,
221 .als_cal_target
= 150,
222 .als_persistence
= 1,
223 .als_interrupt_en
= false,
224 .als_thresh_low
= 200,
225 .als_thresh_high
= 256,
226 .prox_persistence
= 1,
227 .prox_interrupt_en
= false,
229 .prox_thres_high
= 512,
230 .prox_max_samples_cal
= 30,
231 .prox_pulse_count
= 8,
232 .prox_diode
= TSL2772_DIODE1
,
233 .prox_power
= TSL2772_100_mA
236 static const s16 tsl2772_als_gain
[] = {
243 static const s16 tsl2772_prox_gain
[] = {
250 static const int tsl2772_int_time_avail
[][6] = {
251 [tsl2571
] = { 0, 2720, 0, 2720, 0, 696000 },
252 [tsl2671
] = { 0, 2720, 0, 2720, 0, 696000 },
253 [tmd2671
] = { 0, 2720, 0, 2720, 0, 696000 },
254 [tsl2771
] = { 0, 2720, 0, 2720, 0, 696000 },
255 [tmd2771
] = { 0, 2720, 0, 2720, 0, 696000 },
256 [tsl2572
] = { 0, 2730, 0, 2730, 0, 699000 },
257 [tsl2672
] = { 0, 2730, 0, 2730, 0, 699000 },
258 [tmd2672
] = { 0, 2730, 0, 2730, 0, 699000 },
259 [tsl2772
] = { 0, 2730, 0, 2730, 0, 699000 },
260 [tmd2772
] = { 0, 2730, 0, 2730, 0, 699000 },
263 static int tsl2772_int_calibscale_avail
[] = { 1, 8, 16, 120 };
265 static int tsl2772_prox_calibscale_avail
[] = { 1, 2, 4, 8 };
267 /* Channel variations */
276 static const u8 device_channel_config
[] = {
289 static int tsl2772_read_status(struct tsl2772_chip
*chip
)
293 ret
= i2c_smbus_read_byte_data(chip
->client
,
294 TSL2772_CMD_REG
| TSL2772_STATUS
);
296 dev_err(&chip
->client
->dev
,
297 "%s: failed to read STATUS register: %d\n", __func__
,
303 static int tsl2772_write_control_reg(struct tsl2772_chip
*chip
, u8 data
)
307 ret
= i2c_smbus_write_byte_data(chip
->client
,
308 TSL2772_CMD_REG
| TSL2772_CNTRL
, data
);
310 dev_err(&chip
->client
->dev
,
311 "%s: failed to write to control register %x: %d\n",
312 __func__
, data
, ret
);
318 static int tsl2772_read_autoinc_regs(struct tsl2772_chip
*chip
, int lower_reg
,
324 ret
= i2c_smbus_write_byte(chip
->client
,
325 TSL2772_CMD_REG
| TSL2772_CMD_AUTOINC_PROTO
|
328 dev_err(&chip
->client
->dev
,
329 "%s: failed to enable auto increment protocol: %d\n",
334 ret
= i2c_smbus_read_byte_data(chip
->client
,
335 TSL2772_CMD_REG
| lower_reg
);
337 dev_err(&chip
->client
->dev
,
338 "%s: failed to read from register %x: %d\n", __func__
,
344 ret
= i2c_smbus_read_byte_data(chip
->client
,
345 TSL2772_CMD_REG
| upper_reg
);
347 dev_err(&chip
->client
->dev
,
348 "%s: failed to read from register %x: %d\n", __func__
,
354 ret
= i2c_smbus_write_byte(chip
->client
,
355 TSL2772_CMD_REG
| TSL2772_CMD_REPEAT_PROTO
|
358 dev_err(&chip
->client
->dev
,
359 "%s: failed to enable repeated byte protocol: %d\n",
364 return le16_to_cpup((const __le16
*)&buf
[0]);
368 * tsl2772_get_lux() - Reads and calculates current lux value.
369 * @indio_dev: pointer to IIO device
371 * The raw ch0 and ch1 values of the ambient light sensed in the last
372 * integration cycle are read from the device. The raw values are multiplied
373 * by a device-specific scale factor, and divided by the integration time and
374 * device gain. The code supports multiple lux equations through the lux table
375 * coefficients. A lux gain trim is applied to each lux equation, and then the
376 * maximum lux within the interval 0..65535 is selected.
378 static int tsl2772_get_lux(struct iio_dev
*indio_dev
)
380 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
381 struct tsl2772_lux
*p
;
385 mutex_lock(&chip
->als_mutex
);
387 if (chip
->tsl2772_chip_status
!= TSL2772_CHIP_WORKING
) {
388 dev_err(&chip
->client
->dev
, "%s: device is not enabled\n",
394 ret
= tsl2772_read_status(chip
);
398 if (!(ret
& TSL2772_STA_ADC_VALID
)) {
399 dev_err(&chip
->client
->dev
,
400 "%s: data not valid yet\n", __func__
);
401 ret
= chip
->als_cur_info
.lux
; /* return LAST VALUE */
405 ret
= tsl2772_read_autoinc_regs(chip
, TSL2772_ALS_CHAN0LO
,
406 TSL2772_ALS_CHAN0HI
);
409 chip
->als_cur_info
.als_ch0
= ret
;
411 ret
= tsl2772_read_autoinc_regs(chip
, TSL2772_ALS_CHAN1LO
,
412 TSL2772_ALS_CHAN1HI
);
415 chip
->als_cur_info
.als_ch1
= ret
;
417 if (chip
->als_cur_info
.als_ch0
>= chip
->als_saturation
) {
418 max_lux
= TSL2772_LUX_CALC_OVER_FLOW
;
419 goto update_struct_with_max_lux
;
422 if (!chip
->als_cur_info
.als_ch0
) {
423 /* have no data, so return LAST VALUE */
424 ret
= chip
->als_cur_info
.lux
;
430 for (p
= (struct tsl2772_lux
*)chip
->tsl2772_device_lux
; p
->ch0
!= 0;
434 lux
= ((chip
->als_cur_info
.als_ch0
* p
->ch0
) -
435 (chip
->als_cur_info
.als_ch1
* p
->ch1
)) /
436 chip
->als_gain_time_scale
;
439 * The als_gain_trim can have a value within the range 250..4000
440 * and is a multiplier for the lux. A trim of 1000 makes no
441 * changes to the lux, less than 1000 scales it down, and
442 * greater than 1000 scales it up.
444 lux
= (lux
* chip
->settings
.als_gain_trim
) / 1000;
446 if (lux
> TSL2772_LUX_CALC_OVER_FLOW
) {
451 max_lux
= max(max_lux
, lux
);
454 if (overflow
&& max_lux
== 0)
455 max_lux
= TSL2772_LUX_CALC_OVER_FLOW
;
457 update_struct_with_max_lux
:
458 chip
->als_cur_info
.lux
= max_lux
;
462 mutex_unlock(&chip
->als_mutex
);
468 * tsl2772_get_prox() - Reads proximity data registers and updates
471 * @indio_dev: pointer to IIO device
473 static int tsl2772_get_prox(struct iio_dev
*indio_dev
)
475 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
478 mutex_lock(&chip
->prox_mutex
);
480 ret
= tsl2772_read_status(chip
);
490 if (!(ret
& TSL2772_STA_ADC_VALID
)) {
500 if (!(ret
& TSL2772_STA_PRX_VALID
)) {
507 ret
= tsl2772_read_autoinc_regs(chip
, TSL2772_PRX_LO
, TSL2772_PRX_HI
);
510 chip
->prox_data
= ret
;
513 mutex_unlock(&chip
->prox_mutex
);
519 * tsl2772_defaults() - Populates the device nominal operating parameters
520 * with those provided by a 'platform' data struct or
521 * with prefined defaults.
523 * @chip: pointer to device structure.
525 static void tsl2772_defaults(struct tsl2772_chip
*chip
)
527 /* If Operational settings defined elsewhere.. */
528 if (chip
->pdata
&& chip
->pdata
->platform_default_settings
)
529 memcpy(&chip
->settings
, chip
->pdata
->platform_default_settings
,
530 sizeof(tsl2772_default_settings
));
532 memcpy(&chip
->settings
, &tsl2772_default_settings
,
533 sizeof(tsl2772_default_settings
));
535 /* Load up the proper lux table. */
536 if (chip
->pdata
&& chip
->pdata
->platform_lux_table
[0].ch0
!= 0)
537 memcpy(chip
->tsl2772_device_lux
,
538 chip
->pdata
->platform_lux_table
,
539 sizeof(chip
->pdata
->platform_lux_table
));
541 memcpy(chip
->tsl2772_device_lux
,
542 tsl2772_default_lux_table_group
[chip
->id
],
543 TSL2772_DEFAULT_TABLE_BYTES
);
547 * tsl2772_als_calibrate() - Obtain single reading and calculate
550 * @indio_dev: pointer to IIO device
552 static int tsl2772_als_calibrate(struct iio_dev
*indio_dev
)
554 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
557 ret
= i2c_smbus_read_byte_data(chip
->client
,
558 TSL2772_CMD_REG
| TSL2772_CNTRL
);
560 dev_err(&chip
->client
->dev
,
561 "%s: failed to read from the CNTRL register\n",
566 if ((ret
& (TSL2772_CNTL_ADC_ENBL
| TSL2772_CNTL_PWR_ON
))
567 != (TSL2772_CNTL_ADC_ENBL
| TSL2772_CNTL_PWR_ON
)) {
568 dev_err(&chip
->client
->dev
,
569 "%s: Device is not powered on and/or ADC is not enabled\n",
572 } else if ((ret
& TSL2772_STA_ADC_VALID
) != TSL2772_STA_ADC_VALID
) {
573 dev_err(&chip
->client
->dev
,
574 "%s: The two ADC channels have not completed an integration cycle\n",
579 lux_val
= tsl2772_get_lux(indio_dev
);
581 dev_err(&chip
->client
->dev
,
582 "%s: failed to get lux\n", __func__
);
588 ret
= (chip
->settings
.als_cal_target
* chip
->settings
.als_gain_trim
) /
590 if (ret
< TSL2772_ALS_GAIN_TRIM_MIN
|| ret
> TSL2772_ALS_GAIN_TRIM_MAX
)
593 chip
->settings
.als_gain_trim
= ret
;
598 static int tsl2772_chip_on(struct iio_dev
*indio_dev
)
600 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
601 int ret
, i
, als_count
, als_time_us
;
602 u8
*dev_reg
, reg_val
;
604 /* Non calculated parameters */
605 chip
->tsl2772_config
[TSL2772_ALS_TIME
] = chip
->settings
.als_time
;
606 chip
->tsl2772_config
[TSL2772_PRX_TIME
] = chip
->settings
.prox_time
;
607 chip
->tsl2772_config
[TSL2772_WAIT_TIME
] = chip
->settings
.wait_time
;
608 chip
->tsl2772_config
[TSL2772_ALS_PRX_CONFIG
] =
609 chip
->settings
.als_prox_config
;
611 chip
->tsl2772_config
[TSL2772_ALS_MINTHRESHLO
] =
612 (chip
->settings
.als_thresh_low
) & 0xFF;
613 chip
->tsl2772_config
[TSL2772_ALS_MINTHRESHHI
] =
614 (chip
->settings
.als_thresh_low
>> 8) & 0xFF;
615 chip
->tsl2772_config
[TSL2772_ALS_MAXTHRESHLO
] =
616 (chip
->settings
.als_thresh_high
) & 0xFF;
617 chip
->tsl2772_config
[TSL2772_ALS_MAXTHRESHHI
] =
618 (chip
->settings
.als_thresh_high
>> 8) & 0xFF;
619 chip
->tsl2772_config
[TSL2772_PERSISTENCE
] =
620 (chip
->settings
.prox_persistence
& 0xFF) << 4 |
621 (chip
->settings
.als_persistence
& 0xFF);
623 chip
->tsl2772_config
[TSL2772_PRX_COUNT
] =
624 chip
->settings
.prox_pulse_count
;
625 chip
->tsl2772_config
[TSL2772_PRX_MINTHRESHLO
] =
626 (chip
->settings
.prox_thres_low
) & 0xFF;
627 chip
->tsl2772_config
[TSL2772_PRX_MINTHRESHHI
] =
628 (chip
->settings
.prox_thres_low
>> 8) & 0xFF;
629 chip
->tsl2772_config
[TSL2772_PRX_MAXTHRESHLO
] =
630 (chip
->settings
.prox_thres_high
) & 0xFF;
631 chip
->tsl2772_config
[TSL2772_PRX_MAXTHRESHHI
] =
632 (chip
->settings
.prox_thres_high
>> 8) & 0xFF;
634 /* and make sure we're not already on */
635 if (chip
->tsl2772_chip_status
== TSL2772_CHIP_WORKING
) {
636 /* if forcing a register update - turn off, then on */
637 dev_info(&chip
->client
->dev
, "device is already enabled\n");
641 /* Set the gain based on tsl2772_settings struct */
642 chip
->tsl2772_config
[TSL2772_GAIN
] =
643 (chip
->settings
.als_gain
& 0xFF) |
644 ((chip
->settings
.prox_gain
& 0xFF) << 2) |
645 (chip
->settings
.prox_diode
<< 4) |
646 (chip
->settings
.prox_power
<< 6);
648 /* set chip time scaling and saturation */
649 als_count
= 256 - chip
->settings
.als_time
;
650 als_time_us
= als_count
* tsl2772_int_time_avail
[chip
->id
][3];
651 chip
->als_saturation
= als_count
* 768; /* 75% of full scale */
652 chip
->als_gain_time_scale
= als_time_us
*
653 tsl2772_als_gain
[chip
->settings
.als_gain
];
656 * TSL2772 Specific power-on / adc enable sequence
657 * Power on the device 1st.
659 ret
= tsl2772_write_control_reg(chip
, TSL2772_CNTL_PWR_ON
);
664 * Use the following shadow copy for our delay before enabling ADC.
665 * Write all the registers.
667 for (i
= 0, dev_reg
= chip
->tsl2772_config
;
668 i
< TSL2772_MAX_CONFIG_REG
; i
++) {
669 int reg
= TSL2772_CMD_REG
+ i
;
671 ret
= i2c_smbus_write_byte_data(chip
->client
, reg
,
674 dev_err(&chip
->client
->dev
,
675 "%s: failed to write to register %x: %d\n",
681 /* Power-on settling time */
682 usleep_range(3000, 3500);
684 reg_val
= TSL2772_CNTL_PWR_ON
| TSL2772_CNTL_ADC_ENBL
|
685 TSL2772_CNTL_PROX_DET_ENBL
;
686 if (chip
->settings
.als_interrupt_en
)
687 reg_val
|= TSL2772_CNTL_ALS_INT_ENBL
;
688 if (chip
->settings
.prox_interrupt_en
)
689 reg_val
|= TSL2772_CNTL_PROX_INT_ENBL
;
691 ret
= tsl2772_write_control_reg(chip
, reg_val
);
695 ret
= i2c_smbus_write_byte(chip
->client
,
696 TSL2772_CMD_REG
| TSL2772_CMD_SPL_FN
|
697 TSL2772_CMD_PROXALS_INT_CLR
);
699 dev_err(&chip
->client
->dev
,
700 "%s: failed to clear interrupt status: %d\n",
705 chip
->tsl2772_chip_status
= TSL2772_CHIP_WORKING
;
710 static int tsl2772_chip_off(struct iio_dev
*indio_dev
)
712 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
714 /* turn device off */
715 chip
->tsl2772_chip_status
= TSL2772_CHIP_SUSPENDED
;
716 return tsl2772_write_control_reg(chip
, 0x00);
720 * tsl2772_invoke_change - power cycle the device to implement the user
722 * @indio_dev: pointer to IIO device
724 * Obtain and lock both ALS and PROX resources, determine and save device state
725 * (On/Off), cycle device to implement updated parameter, put device back into
726 * proper state, and unlock resource.
728 static int tsl2772_invoke_change(struct iio_dev
*indio_dev
)
730 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
731 int device_status
= chip
->tsl2772_chip_status
;
734 mutex_lock(&chip
->als_mutex
);
735 mutex_lock(&chip
->prox_mutex
);
737 if (device_status
== TSL2772_CHIP_WORKING
) {
738 ret
= tsl2772_chip_off(indio_dev
);
743 ret
= tsl2772_chip_on(indio_dev
);
746 mutex_unlock(&chip
->prox_mutex
);
747 mutex_unlock(&chip
->als_mutex
);
752 static int tsl2772_prox_cal(struct iio_dev
*indio_dev
)
754 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
755 int prox_history
[MAX_SAMPLES_CAL
+ 1];
756 int i
, ret
, mean
, max
, sample_sum
;
758 if (chip
->settings
.prox_max_samples_cal
< 1 ||
759 chip
->settings
.prox_max_samples_cal
> MAX_SAMPLES_CAL
)
762 for (i
= 0; i
< chip
->settings
.prox_max_samples_cal
; i
++) {
763 usleep_range(15000, 17500);
764 ret
= tsl2772_get_prox(indio_dev
);
768 prox_history
[i
] = chip
->prox_data
;
773 for (i
= 0; i
< chip
->settings
.prox_max_samples_cal
; i
++) {
774 sample_sum
+= prox_history
[i
];
775 max
= max(max
, prox_history
[i
]);
777 mean
= sample_sum
/ chip
->settings
.prox_max_samples_cal
;
779 chip
->settings
.prox_thres_high
= (max
<< 1) - mean
;
781 return tsl2772_invoke_change(indio_dev
);
784 static int tsl2772_read_avail(struct iio_dev
*indio_dev
,
785 struct iio_chan_spec
const *chan
,
786 const int **vals
, int *type
, int *length
,
789 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
792 case IIO_CHAN_INFO_CALIBSCALE
:
793 if (chan
->type
== IIO_INTENSITY
) {
794 *length
= ARRAY_SIZE(tsl2772_int_calibscale_avail
);
795 *vals
= tsl2772_int_calibscale_avail
;
797 *length
= ARRAY_SIZE(tsl2772_prox_calibscale_avail
);
798 *vals
= tsl2772_prox_calibscale_avail
;
801 return IIO_AVAIL_LIST
;
802 case IIO_CHAN_INFO_INT_TIME
:
803 *length
= ARRAY_SIZE(tsl2772_int_time_avail
[chip
->id
]);
804 *vals
= tsl2772_int_time_avail
[chip
->id
];
805 *type
= IIO_VAL_INT_PLUS_MICRO
;
806 return IIO_AVAIL_RANGE
;
812 static ssize_t
in_illuminance0_target_input_show(struct device
*dev
,
813 struct device_attribute
*attr
,
816 struct tsl2772_chip
*chip
= iio_priv(dev_to_iio_dev(dev
));
818 return snprintf(buf
, PAGE_SIZE
, "%d\n", chip
->settings
.als_cal_target
);
821 static ssize_t
in_illuminance0_target_input_store(struct device
*dev
,
822 struct device_attribute
*attr
,
823 const char *buf
, size_t len
)
825 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
826 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
830 if (kstrtou16(buf
, 0, &value
))
833 chip
->settings
.als_cal_target
= value
;
834 ret
= tsl2772_invoke_change(indio_dev
);
841 static ssize_t
in_illuminance0_calibrate_store(struct device
*dev
,
842 struct device_attribute
*attr
,
843 const char *buf
, size_t len
)
845 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
849 if (kstrtobool(buf
, &value
) || !value
)
852 ret
= tsl2772_als_calibrate(indio_dev
);
856 ret
= tsl2772_invoke_change(indio_dev
);
863 static ssize_t
in_illuminance0_lux_table_show(struct device
*dev
,
864 struct device_attribute
*attr
,
867 struct tsl2772_chip
*chip
= iio_priv(dev_to_iio_dev(dev
));
871 while (i
< TSL2772_MAX_LUX_TABLE_SIZE
) {
872 offset
+= snprintf(buf
+ offset
, PAGE_SIZE
, "%u,%u,",
873 chip
->tsl2772_device_lux
[i
].ch0
,
874 chip
->tsl2772_device_lux
[i
].ch1
);
875 if (chip
->tsl2772_device_lux
[i
].ch0
== 0) {
877 * We just printed the first "0" entry.
878 * Now get rid of the extra "," and break.
886 offset
+= snprintf(buf
+ offset
, PAGE_SIZE
, "\n");
890 static ssize_t
in_illuminance0_lux_table_store(struct device
*dev
,
891 struct device_attribute
*attr
,
892 const char *buf
, size_t len
)
894 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
895 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
896 int value
[ARRAY_SIZE(chip
->tsl2772_device_lux
) * 2 + 1];
899 get_options(buf
, ARRAY_SIZE(value
), value
);
902 * We now have an array of ints starting at value[1], and
903 * enumerated by value[0].
904 * We expect each group of two ints to be one table entry,
905 * and the last table entry is all 0.
908 if ((n
% 2) || n
< 4 ||
909 n
> ((ARRAY_SIZE(chip
->tsl2772_device_lux
) - 1) * 2))
912 if ((value
[(n
- 1)] | value
[n
]) != 0)
915 if (chip
->tsl2772_chip_status
== TSL2772_CHIP_WORKING
) {
916 ret
= tsl2772_chip_off(indio_dev
);
921 /* Zero out the table */
922 memset(chip
->tsl2772_device_lux
, 0, sizeof(chip
->tsl2772_device_lux
));
923 memcpy(chip
->tsl2772_device_lux
, &value
[1], (value
[0] * 4));
925 ret
= tsl2772_invoke_change(indio_dev
);
932 static ssize_t
in_proximity0_calibrate_store(struct device
*dev
,
933 struct device_attribute
*attr
,
934 const char *buf
, size_t len
)
936 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
940 if (kstrtobool(buf
, &value
) || !value
)
943 ret
= tsl2772_prox_cal(indio_dev
);
947 ret
= tsl2772_invoke_change(indio_dev
);
954 static int tsl2772_read_interrupt_config(struct iio_dev
*indio_dev
,
955 const struct iio_chan_spec
*chan
,
956 enum iio_event_type type
,
957 enum iio_event_direction dir
)
959 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
961 if (chan
->type
== IIO_INTENSITY
)
962 return chip
->settings
.als_interrupt_en
;
964 return chip
->settings
.prox_interrupt_en
;
967 static int tsl2772_write_interrupt_config(struct iio_dev
*indio_dev
,
968 const struct iio_chan_spec
*chan
,
969 enum iio_event_type type
,
970 enum iio_event_direction dir
,
973 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
975 if (chan
->type
== IIO_INTENSITY
)
976 chip
->settings
.als_interrupt_en
= val
? true : false;
978 chip
->settings
.prox_interrupt_en
= val
? true : false;
980 return tsl2772_invoke_change(indio_dev
);
983 static int tsl2772_write_event_value(struct iio_dev
*indio_dev
,
984 const struct iio_chan_spec
*chan
,
985 enum iio_event_type type
,
986 enum iio_event_direction dir
,
987 enum iio_event_info info
,
990 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
991 int ret
= -EINVAL
, count
, persistence
;
995 case IIO_EV_INFO_VALUE
:
996 if (chan
->type
== IIO_INTENSITY
) {
998 case IIO_EV_DIR_RISING
:
999 chip
->settings
.als_thresh_high
= val
;
1002 case IIO_EV_DIR_FALLING
:
1003 chip
->settings
.als_thresh_low
= val
;
1011 case IIO_EV_DIR_RISING
:
1012 chip
->settings
.prox_thres_high
= val
;
1015 case IIO_EV_DIR_FALLING
:
1016 chip
->settings
.prox_thres_low
= val
;
1024 case IIO_EV_INFO_PERIOD
:
1025 if (chan
->type
== IIO_INTENSITY
)
1026 time
= chip
->settings
.als_time
;
1028 time
= chip
->settings
.prox_time
;
1031 persistence
= ((val
* 1000000) + val2
) /
1032 (count
* tsl2772_int_time_avail
[chip
->id
][3]);
1034 if (chan
->type
== IIO_INTENSITY
) {
1035 /* ALS filter values are 1, 2, 3, 5, 10, 15, ..., 60 */
1036 if (persistence
> 3)
1037 persistence
= (persistence
/ 5) + 3;
1039 chip
->settings
.als_persistence
= persistence
;
1041 chip
->settings
.prox_persistence
= persistence
;
1053 return tsl2772_invoke_change(indio_dev
);
1056 static int tsl2772_read_event_value(struct iio_dev
*indio_dev
,
1057 const struct iio_chan_spec
*chan
,
1058 enum iio_event_type type
,
1059 enum iio_event_direction dir
,
1060 enum iio_event_info info
,
1061 int *val
, int *val2
)
1063 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
1064 int filter_delay
, persistence
;
1068 case IIO_EV_INFO_VALUE
:
1069 if (chan
->type
== IIO_INTENSITY
) {
1071 case IIO_EV_DIR_RISING
:
1072 *val
= chip
->settings
.als_thresh_high
;
1074 case IIO_EV_DIR_FALLING
:
1075 *val
= chip
->settings
.als_thresh_low
;
1082 case IIO_EV_DIR_RISING
:
1083 *val
= chip
->settings
.prox_thres_high
;
1085 case IIO_EV_DIR_FALLING
:
1086 *val
= chip
->settings
.prox_thres_low
;
1093 case IIO_EV_INFO_PERIOD
:
1094 if (chan
->type
== IIO_INTENSITY
) {
1095 time
= chip
->settings
.als_time
;
1096 persistence
= chip
->settings
.als_persistence
;
1098 /* ALS filter values are 1, 2, 3, 5, 10, 15, ..., 60 */
1099 if (persistence
> 3)
1100 persistence
= (persistence
- 3) * 5;
1102 time
= chip
->settings
.prox_time
;
1103 persistence
= chip
->settings
.prox_persistence
;
1106 filter_delay
= persistence
* (256 - time
) *
1107 tsl2772_int_time_avail
[chip
->id
][3];
1109 *val
= filter_delay
/ 1000000;
1110 *val2
= filter_delay
% 1000000;
1111 return IIO_VAL_INT_PLUS_MICRO
;
1117 static int tsl2772_read_raw(struct iio_dev
*indio_dev
,
1118 struct iio_chan_spec
const *chan
,
1123 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
1126 case IIO_CHAN_INFO_PROCESSED
:
1127 switch (chan
->type
) {
1129 tsl2772_get_lux(indio_dev
);
1130 *val
= chip
->als_cur_info
.lux
;
1135 case IIO_CHAN_INFO_RAW
:
1136 switch (chan
->type
) {
1138 tsl2772_get_lux(indio_dev
);
1139 if (chan
->channel
== 0)
1140 *val
= chip
->als_cur_info
.als_ch0
;
1142 *val
= chip
->als_cur_info
.als_ch1
;
1145 tsl2772_get_prox(indio_dev
);
1146 *val
= chip
->prox_data
;
1152 case IIO_CHAN_INFO_CALIBSCALE
:
1153 if (chan
->type
== IIO_LIGHT
)
1154 *val
= tsl2772_als_gain
[chip
->settings
.als_gain
];
1156 *val
= tsl2772_prox_gain
[chip
->settings
.prox_gain
];
1158 case IIO_CHAN_INFO_CALIBBIAS
:
1159 *val
= chip
->settings
.als_gain_trim
;
1161 case IIO_CHAN_INFO_INT_TIME
:
1163 *val2
= (256 - chip
->settings
.als_time
) *
1164 tsl2772_int_time_avail
[chip
->id
][3];
1165 return IIO_VAL_INT_PLUS_MICRO
;
1171 static int tsl2772_write_raw(struct iio_dev
*indio_dev
,
1172 struct iio_chan_spec
const *chan
,
1177 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
1180 case IIO_CHAN_INFO_CALIBSCALE
:
1181 if (chan
->type
== IIO_INTENSITY
) {
1184 chip
->settings
.als_gain
= 0;
1187 chip
->settings
.als_gain
= 1;
1190 chip
->settings
.als_gain
= 2;
1193 chip
->settings
.als_gain
= 3;
1201 chip
->settings
.prox_gain
= 0;
1204 chip
->settings
.prox_gain
= 1;
1207 chip
->settings
.prox_gain
= 2;
1210 chip
->settings
.prox_gain
= 3;
1217 case IIO_CHAN_INFO_CALIBBIAS
:
1218 if (val
< TSL2772_ALS_GAIN_TRIM_MIN
||
1219 val
> TSL2772_ALS_GAIN_TRIM_MAX
)
1222 chip
->settings
.als_gain_trim
= val
;
1224 case IIO_CHAN_INFO_INT_TIME
:
1225 if (val
!= 0 || val2
< tsl2772_int_time_avail
[chip
->id
][1] ||
1226 val2
> tsl2772_int_time_avail
[chip
->id
][5])
1229 chip
->settings
.als_time
= 256 -
1230 (val2
/ tsl2772_int_time_avail
[chip
->id
][3]);
1236 return tsl2772_invoke_change(indio_dev
);
1239 static DEVICE_ATTR_RW(in_illuminance0_target_input
);
1241 static DEVICE_ATTR_WO(in_illuminance0_calibrate
);
1243 static DEVICE_ATTR_WO(in_proximity0_calibrate
);
1245 static DEVICE_ATTR_RW(in_illuminance0_lux_table
);
1247 /* Use the default register values to identify the Taos device */
1248 static int tsl2772_device_id_verif(int id
, int target
)
1254 return (id
& 0xf0) == TRITON_ID
;
1257 return (id
& 0xf0) == HALIBUT_ID
;
1263 return (id
& 0xf0) == SWORDFISH_ID
;
1269 static irqreturn_t
tsl2772_event_handler(int irq
, void *private)
1271 struct iio_dev
*indio_dev
= private;
1272 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
1273 s64 timestamp
= iio_get_time_ns(indio_dev
);
1276 ret
= tsl2772_read_status(chip
);
1280 /* What type of interrupt do we need to process */
1281 if (ret
& TSL2772_STA_PRX_INTR
) {
1282 iio_push_event(indio_dev
,
1283 IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY
,
1290 if (ret
& TSL2772_STA_ALS_INTR
) {
1291 iio_push_event(indio_dev
,
1292 IIO_UNMOD_EVENT_CODE(IIO_LIGHT
,
1299 ret
= i2c_smbus_write_byte(chip
->client
,
1300 TSL2772_CMD_REG
| TSL2772_CMD_SPL_FN
|
1301 TSL2772_CMD_PROXALS_INT_CLR
);
1303 dev_err(&chip
->client
->dev
,
1304 "%s: failed to clear interrupt status: %d\n",
1310 static struct attribute
*tsl2772_ALS_device_attrs
[] = {
1311 &dev_attr_in_illuminance0_target_input
.attr
,
1312 &dev_attr_in_illuminance0_calibrate
.attr
,
1313 &dev_attr_in_illuminance0_lux_table
.attr
,
1317 static struct attribute
*tsl2772_PRX_device_attrs
[] = {
1318 &dev_attr_in_proximity0_calibrate
.attr
,
1322 static struct attribute
*tsl2772_ALSPRX_device_attrs
[] = {
1323 &dev_attr_in_illuminance0_target_input
.attr
,
1324 &dev_attr_in_illuminance0_calibrate
.attr
,
1325 &dev_attr_in_illuminance0_lux_table
.attr
,
1329 static struct attribute
*tsl2772_PRX2_device_attrs
[] = {
1330 &dev_attr_in_proximity0_calibrate
.attr
,
1334 static struct attribute
*tsl2772_ALSPRX2_device_attrs
[] = {
1335 &dev_attr_in_illuminance0_target_input
.attr
,
1336 &dev_attr_in_illuminance0_calibrate
.attr
,
1337 &dev_attr_in_illuminance0_lux_table
.attr
,
1338 &dev_attr_in_proximity0_calibrate
.attr
,
1342 static const struct attribute_group tsl2772_device_attr_group_tbl
[] = {
1344 .attrs
= tsl2772_ALS_device_attrs
,
1347 .attrs
= tsl2772_PRX_device_attrs
,
1350 .attrs
= tsl2772_ALSPRX_device_attrs
,
1353 .attrs
= tsl2772_PRX2_device_attrs
,
1356 .attrs
= tsl2772_ALSPRX2_device_attrs
,
1360 #define TSL2772_DEVICE_INFO(type)[type] = \
1362 .attrs = &tsl2772_device_attr_group_tbl[type], \
1363 .read_raw = &tsl2772_read_raw, \
1364 .read_avail = &tsl2772_read_avail, \
1365 .write_raw = &tsl2772_write_raw, \
1366 .read_event_value = &tsl2772_read_event_value, \
1367 .write_event_value = &tsl2772_write_event_value, \
1368 .read_event_config = &tsl2772_read_interrupt_config, \
1369 .write_event_config = &tsl2772_write_interrupt_config, \
1372 static const struct iio_info tsl2772_device_info
[] = {
1373 TSL2772_DEVICE_INFO(ALS
),
1374 TSL2772_DEVICE_INFO(PRX
),
1375 TSL2772_DEVICE_INFO(ALSPRX
),
1376 TSL2772_DEVICE_INFO(PRX2
),
1377 TSL2772_DEVICE_INFO(ALSPRX2
),
1380 static const struct iio_event_spec tsl2772_events
[] = {
1382 .type
= IIO_EV_TYPE_THRESH
,
1383 .dir
= IIO_EV_DIR_RISING
,
1384 .mask_separate
= BIT(IIO_EV_INFO_VALUE
),
1386 .type
= IIO_EV_TYPE_THRESH
,
1387 .dir
= IIO_EV_DIR_FALLING
,
1388 .mask_separate
= BIT(IIO_EV_INFO_VALUE
),
1390 .type
= IIO_EV_TYPE_THRESH
,
1391 .dir
= IIO_EV_DIR_EITHER
,
1392 .mask_separate
= BIT(IIO_EV_INFO_PERIOD
) |
1393 BIT(IIO_EV_INFO_ENABLE
),
1397 static const struct tsl2772_chip_info tsl2772_chip_info_tbl
[] = {
1399 .channel_with_events
= {
1404 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
1406 .type
= IIO_INTENSITY
,
1409 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1410 BIT(IIO_CHAN_INFO_INT_TIME
) |
1411 BIT(IIO_CHAN_INFO_CALIBSCALE
) |
1412 BIT(IIO_CHAN_INFO_CALIBBIAS
),
1413 .info_mask_separate_available
=
1414 BIT(IIO_CHAN_INFO_INT_TIME
) |
1415 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1416 .event_spec
= tsl2772_events
,
1417 .num_event_specs
= ARRAY_SIZE(tsl2772_events
),
1419 .type
= IIO_INTENSITY
,
1424 .channel_without_events
= {
1429 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
1431 .type
= IIO_INTENSITY
,
1434 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1435 BIT(IIO_CHAN_INFO_INT_TIME
) |
1436 BIT(IIO_CHAN_INFO_CALIBSCALE
) |
1437 BIT(IIO_CHAN_INFO_CALIBBIAS
),
1438 .info_mask_separate_available
=
1439 BIT(IIO_CHAN_INFO_INT_TIME
) |
1440 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1442 .type
= IIO_INTENSITY
,
1447 .chan_table_elements
= 3,
1448 .info
= &tsl2772_device_info
[ALS
],
1451 .channel_with_events
= {
1453 .type
= IIO_PROXIMITY
,
1456 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
1457 .event_spec
= tsl2772_events
,
1458 .num_event_specs
= ARRAY_SIZE(tsl2772_events
),
1461 .channel_without_events
= {
1463 .type
= IIO_PROXIMITY
,
1466 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
1469 .chan_table_elements
= 1,
1470 .info
= &tsl2772_device_info
[PRX
],
1473 .channel_with_events
= {
1478 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
1480 .type
= IIO_INTENSITY
,
1483 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1484 BIT(IIO_CHAN_INFO_INT_TIME
) |
1485 BIT(IIO_CHAN_INFO_CALIBSCALE
) |
1486 BIT(IIO_CHAN_INFO_CALIBBIAS
),
1487 .info_mask_separate_available
=
1488 BIT(IIO_CHAN_INFO_INT_TIME
) |
1489 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1490 .event_spec
= tsl2772_events
,
1491 .num_event_specs
= ARRAY_SIZE(tsl2772_events
),
1493 .type
= IIO_INTENSITY
,
1496 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
1498 .type
= IIO_PROXIMITY
,
1501 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
1502 .event_spec
= tsl2772_events
,
1503 .num_event_specs
= ARRAY_SIZE(tsl2772_events
),
1506 .channel_without_events
= {
1511 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
1513 .type
= IIO_INTENSITY
,
1516 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1517 BIT(IIO_CHAN_INFO_INT_TIME
) |
1518 BIT(IIO_CHAN_INFO_CALIBSCALE
) |
1519 BIT(IIO_CHAN_INFO_CALIBBIAS
),
1520 .info_mask_separate_available
=
1521 BIT(IIO_CHAN_INFO_INT_TIME
) |
1522 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1524 .type
= IIO_INTENSITY
,
1527 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
1529 .type
= IIO_PROXIMITY
,
1532 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
1535 .chan_table_elements
= 4,
1536 .info
= &tsl2772_device_info
[ALSPRX
],
1539 .channel_with_events
= {
1541 .type
= IIO_PROXIMITY
,
1544 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1545 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1546 .info_mask_separate_available
=
1547 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1548 .event_spec
= tsl2772_events
,
1549 .num_event_specs
= ARRAY_SIZE(tsl2772_events
),
1552 .channel_without_events
= {
1554 .type
= IIO_PROXIMITY
,
1557 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1558 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1559 .info_mask_separate_available
=
1560 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1563 .chan_table_elements
= 1,
1564 .info
= &tsl2772_device_info
[PRX2
],
1567 .channel_with_events
= {
1572 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
1574 .type
= IIO_INTENSITY
,
1577 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1578 BIT(IIO_CHAN_INFO_INT_TIME
) |
1579 BIT(IIO_CHAN_INFO_CALIBSCALE
) |
1580 BIT(IIO_CHAN_INFO_CALIBBIAS
),
1581 .info_mask_separate_available
=
1582 BIT(IIO_CHAN_INFO_INT_TIME
) |
1583 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1584 .event_spec
= tsl2772_events
,
1585 .num_event_specs
= ARRAY_SIZE(tsl2772_events
),
1587 .type
= IIO_INTENSITY
,
1590 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
1592 .type
= IIO_PROXIMITY
,
1595 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1596 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1597 .info_mask_separate_available
=
1598 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1599 .event_spec
= tsl2772_events
,
1600 .num_event_specs
= ARRAY_SIZE(tsl2772_events
),
1603 .channel_without_events
= {
1608 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
1610 .type
= IIO_INTENSITY
,
1613 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1614 BIT(IIO_CHAN_INFO_INT_TIME
) |
1615 BIT(IIO_CHAN_INFO_CALIBSCALE
) |
1616 BIT(IIO_CHAN_INFO_CALIBBIAS
),
1617 .info_mask_separate_available
=
1618 BIT(IIO_CHAN_INFO_INT_TIME
) |
1619 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1621 .type
= IIO_INTENSITY
,
1624 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
1626 .type
= IIO_PROXIMITY
,
1629 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1630 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1631 .info_mask_separate_available
=
1632 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1635 .chan_table_elements
= 4,
1636 .info
= &tsl2772_device_info
[ALSPRX2
],
1640 static int tsl2772_probe(struct i2c_client
*clientp
,
1641 const struct i2c_device_id
*id
)
1643 struct iio_dev
*indio_dev
;
1644 struct tsl2772_chip
*chip
;
1647 indio_dev
= devm_iio_device_alloc(&clientp
->dev
, sizeof(*chip
));
1651 chip
= iio_priv(indio_dev
);
1652 chip
->client
= clientp
;
1653 i2c_set_clientdata(clientp
, indio_dev
);
1655 ret
= i2c_smbus_read_byte_data(chip
->client
,
1656 TSL2772_CMD_REG
| TSL2772_CHIPID
);
1660 if (tsl2772_device_id_verif(ret
, id
->driver_data
) <= 0) {
1661 dev_info(&chip
->client
->dev
,
1662 "%s: i2c device found does not match expected id\n",
1667 ret
= i2c_smbus_write_byte(clientp
, TSL2772_CMD_REG
| TSL2772_CNTRL
);
1669 dev_err(&clientp
->dev
,
1670 "%s: Failed to write to CMD register: %d\n",
1675 mutex_init(&chip
->als_mutex
);
1676 mutex_init(&chip
->prox_mutex
);
1678 chip
->tsl2772_chip_status
= TSL2772_CHIP_UNKNOWN
;
1679 chip
->pdata
= dev_get_platdata(&clientp
->dev
);
1680 chip
->id
= id
->driver_data
;
1682 &tsl2772_chip_info_tbl
[device_channel_config
[id
->driver_data
]];
1684 indio_dev
->info
= chip
->chip_info
->info
;
1685 indio_dev
->dev
.parent
= &clientp
->dev
;
1686 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1687 indio_dev
->name
= chip
->client
->name
;
1688 indio_dev
->num_channels
= chip
->chip_info
->chan_table_elements
;
1691 indio_dev
->channels
= chip
->chip_info
->channel_with_events
;
1693 ret
= devm_request_threaded_irq(&clientp
->dev
, clientp
->irq
,
1695 &tsl2772_event_handler
,
1696 IRQF_TRIGGER_FALLING
|
1701 dev_err(&clientp
->dev
,
1702 "%s: irq request failed\n", __func__
);
1706 indio_dev
->channels
= chip
->chip_info
->channel_without_events
;
1709 tsl2772_defaults(chip
);
1710 ret
= tsl2772_chip_on(indio_dev
);
1714 ret
= iio_device_register(indio_dev
);
1716 tsl2772_chip_off(indio_dev
);
1717 dev_err(&clientp
->dev
,
1718 "%s: iio registration failed\n", __func__
);
1725 static int tsl2772_suspend(struct device
*dev
)
1727 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1729 return tsl2772_chip_off(indio_dev
);
1732 static int tsl2772_resume(struct device
*dev
)
1734 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1736 return tsl2772_chip_on(indio_dev
);
1739 static int tsl2772_remove(struct i2c_client
*client
)
1741 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
1743 tsl2772_chip_off(indio_dev
);
1745 iio_device_unregister(indio_dev
);
1750 static const struct i2c_device_id tsl2772_idtable
[] = {
1751 { "tsl2571", tsl2571
},
1752 { "tsl2671", tsl2671
},
1753 { "tmd2671", tmd2671
},
1754 { "tsl2771", tsl2771
},
1755 { "tmd2771", tmd2771
},
1756 { "tsl2572", tsl2572
},
1757 { "tsl2672", tsl2672
},
1758 { "tmd2672", tmd2672
},
1759 { "tsl2772", tsl2772
},
1760 { "tmd2772", tmd2772
},
1764 MODULE_DEVICE_TABLE(i2c
, tsl2772_idtable
);
1766 static const struct of_device_id tsl2772_of_match
[] = {
1767 { .compatible
= "amstaos,tsl2571" },
1768 { .compatible
= "amstaos,tsl2671" },
1769 { .compatible
= "amstaos,tmd2671" },
1770 { .compatible
= "amstaos,tsl2771" },
1771 { .compatible
= "amstaos,tmd2771" },
1772 { .compatible
= "amstaos,tsl2572" },
1773 { .compatible
= "amstaos,tsl2672" },
1774 { .compatible
= "amstaos,tmd2672" },
1775 { .compatible
= "amstaos,tsl2772" },
1776 { .compatible
= "amstaos,tmd2772" },
1779 MODULE_DEVICE_TABLE(of
, tsl2772_of_match
);
1781 static const struct dev_pm_ops tsl2772_pm_ops
= {
1782 .suspend
= tsl2772_suspend
,
1783 .resume
= tsl2772_resume
,
1786 static struct i2c_driver tsl2772_driver
= {
1789 .of_match_table
= tsl2772_of_match
,
1790 .pm
= &tsl2772_pm_ops
,
1792 .id_table
= tsl2772_idtable
,
1793 .probe
= tsl2772_probe
,
1794 .remove
= tsl2772_remove
,
1797 module_i2c_driver(tsl2772_driver
);
1799 MODULE_AUTHOR("J. August Brenner <Jon.Brenner@ams.com>");
1800 MODULE_AUTHOR("Brian Masney <masneyb@onstation.org>");
1801 MODULE_DESCRIPTION("TAOS tsl2772 ambient and proximity light sensor driver");
1802 MODULE_LICENSE("GPL");