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/property.h>
19 #include <linux/slab.h>
21 #include <linux/iio/events.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
24 #include <linux/platform_data/tsl2772.h>
25 #include <linux/regulator/consumer.h>
28 #define PROX_STAT_CAL 0
29 #define PROX_STAT_SAMP 1
30 #define MAX_SAMPLES_CAL 200
32 /* TSL2772 Device ID */
33 #define TRITON_ID 0x00
34 #define SWORDFISH_ID 0x30
35 #define HALIBUT_ID 0x20
37 /* Lux calculation constants */
38 #define TSL2772_LUX_CALC_OVER_FLOW 65535
41 * TAOS Register definitions - Note: depending on device, some of these register
42 * are not used and the register address is benign.
45 /* Register offsets */
46 #define TSL2772_MAX_CONFIG_REG 16
48 /* Device Registers and Masks */
49 #define TSL2772_CNTRL 0x00
50 #define TSL2772_ALS_TIME 0X01
51 #define TSL2772_PRX_TIME 0x02
52 #define TSL2772_WAIT_TIME 0x03
53 #define TSL2772_ALS_MINTHRESHLO 0X04
54 #define TSL2772_ALS_MINTHRESHHI 0X05
55 #define TSL2772_ALS_MAXTHRESHLO 0X06
56 #define TSL2772_ALS_MAXTHRESHHI 0X07
57 #define TSL2772_PRX_MINTHRESHLO 0X08
58 #define TSL2772_PRX_MINTHRESHHI 0X09
59 #define TSL2772_PRX_MAXTHRESHLO 0X0A
60 #define TSL2772_PRX_MAXTHRESHHI 0X0B
61 #define TSL2772_PERSISTENCE 0x0C
62 #define TSL2772_ALS_PRX_CONFIG 0x0D
63 #define TSL2772_PRX_COUNT 0x0E
64 #define TSL2772_GAIN 0x0F
65 #define TSL2772_NOTUSED 0x10
66 #define TSL2772_REVID 0x11
67 #define TSL2772_CHIPID 0x12
68 #define TSL2772_STATUS 0x13
69 #define TSL2772_ALS_CHAN0LO 0x14
70 #define TSL2772_ALS_CHAN0HI 0x15
71 #define TSL2772_ALS_CHAN1LO 0x16
72 #define TSL2772_ALS_CHAN1HI 0x17
73 #define TSL2772_PRX_LO 0x18
74 #define TSL2772_PRX_HI 0x19
76 /* tsl2772 cmd reg masks */
77 #define TSL2772_CMD_REG 0x80
78 #define TSL2772_CMD_SPL_FN 0x60
79 #define TSL2772_CMD_REPEAT_PROTO 0x00
80 #define TSL2772_CMD_AUTOINC_PROTO 0x20
82 #define TSL2772_CMD_PROX_INT_CLR 0X05
83 #define TSL2772_CMD_ALS_INT_CLR 0x06
84 #define TSL2772_CMD_PROXALS_INT_CLR 0X07
86 /* tsl2772 cntrl reg masks */
87 #define TSL2772_CNTL_ADC_ENBL 0x02
88 #define TSL2772_CNTL_PWR_ON 0x01
90 /* tsl2772 status reg masks */
91 #define TSL2772_STA_ADC_VALID 0x01
92 #define TSL2772_STA_PRX_VALID 0x02
93 #define TSL2772_STA_ADC_PRX_VALID (TSL2772_STA_ADC_VALID | \
94 TSL2772_STA_PRX_VALID)
95 #define TSL2772_STA_ALS_INTR 0x10
96 #define TSL2772_STA_PRX_INTR 0x20
98 /* tsl2772 cntrl reg masks */
99 #define TSL2772_CNTL_REG_CLEAR 0x00
100 #define TSL2772_CNTL_PROX_INT_ENBL 0X20
101 #define TSL2772_CNTL_ALS_INT_ENBL 0X10
102 #define TSL2772_CNTL_WAIT_TMR_ENBL 0X08
103 #define TSL2772_CNTL_PROX_DET_ENBL 0X04
104 #define TSL2772_CNTL_PWRON 0x01
105 #define TSL2772_CNTL_ALSPON_ENBL 0x03
106 #define TSL2772_CNTL_INTALSPON_ENBL 0x13
107 #define TSL2772_CNTL_PROXPON_ENBL 0x0F
108 #define TSL2772_CNTL_INTPROXPON_ENBL 0x2F
110 #define TSL2772_ALS_GAIN_TRIM_MIN 250
111 #define TSL2772_ALS_GAIN_TRIM_MAX 4000
113 #define TSL2772_MAX_PROX_LEDS 2
115 #define TSL2772_BOOT_MIN_SLEEP_TIME 10000
116 #define TSL2772_BOOT_MAX_SLEEP_TIME 28000
118 /* Device family members */
134 TSL2772_CHIP_UNKNOWN
= 0,
135 TSL2772_CHIP_WORKING
= 1,
136 TSL2772_CHIP_SUSPENDED
= 2
140 TSL2772_SUPPLY_VDD
= 0,
141 TSL2772_SUPPLY_VDDIO
= 1,
142 TSL2772_NUM_SUPPLIES
= 2
145 /* Per-device data */
146 struct tsl2772_als_info
{
152 struct tsl2772_chip_info
{
153 int chan_table_elements
;
154 struct iio_chan_spec channel_with_events
[4];
155 struct iio_chan_spec channel_without_events
[4];
156 const struct iio_info
*info
;
159 static const int tsl2772_led_currents
[][2] = {
160 { 100000, TSL2772_100_mA
},
161 { 50000, TSL2772_50_mA
},
162 { 25000, TSL2772_25_mA
},
163 { 13000, TSL2772_13_mA
},
167 struct tsl2772_chip
{
169 struct mutex prox_mutex
;
170 struct mutex als_mutex
;
171 struct i2c_client
*client
;
172 struct regulator_bulk_data supplies
[TSL2772_NUM_SUPPLIES
];
174 struct tsl2772_als_info als_cur_info
;
175 struct tsl2772_settings settings
;
176 struct tsl2772_platform_data
*pdata
;
177 int als_gain_time_scale
;
179 int tsl2772_chip_status
;
180 u8 tsl2772_config
[TSL2772_MAX_CONFIG_REG
];
181 const struct tsl2772_chip_info
*chip_info
;
182 const struct iio_info
*info
;
185 * This structure is intentionally large to accommodate
187 * Sized to 9 = max 8 segments + 1 termination segment
189 struct tsl2772_lux tsl2772_device_lux
[TSL2772_MAX_LUX_TABLE_SIZE
];
193 * Different devices require different coefficents, and these numbers were
194 * derived from the 'Lux Equation' section of the various device datasheets.
195 * All of these coefficients assume a Glass Attenuation (GA) factor of 1.
196 * The coefficients are multiplied by 1000 to avoid floating point operations.
197 * The two rows in each table correspond to the Lux1 and Lux2 equations from
200 static const struct tsl2772_lux tsl2x71_lux_table
[TSL2772_DEF_LUX_TABLE_SZ
] = {
206 static const struct tsl2772_lux tmd2x71_lux_table
[TSL2772_DEF_LUX_TABLE_SZ
] = {
212 static const struct tsl2772_lux tsl2x72_lux_table
[TSL2772_DEF_LUX_TABLE_SZ
] = {
218 static const struct tsl2772_lux tmd2x72_lux_table
[TSL2772_DEF_LUX_TABLE_SZ
] = {
224 static const struct tsl2772_lux apds9930_lux_table
[TSL2772_DEF_LUX_TABLE_SZ
] = {
230 static const struct tsl2772_lux
*tsl2772_default_lux_table_group
[] = {
231 [tsl2571
] = tsl2x71_lux_table
,
232 [tsl2671
] = tsl2x71_lux_table
,
233 [tmd2671
] = tmd2x71_lux_table
,
234 [tsl2771
] = tsl2x71_lux_table
,
235 [tmd2771
] = tmd2x71_lux_table
,
236 [tsl2572
] = tsl2x72_lux_table
,
237 [tsl2672
] = tsl2x72_lux_table
,
238 [tmd2672
] = tmd2x72_lux_table
,
239 [tsl2772
] = tsl2x72_lux_table
,
240 [tmd2772
] = tmd2x72_lux_table
,
241 [apds9930
] = apds9930_lux_table
,
244 static const struct tsl2772_settings tsl2772_default_settings
= {
245 .als_time
= 255, /* 2.72 / 2.73 ms */
247 .prox_time
= 255, /* 2.72 / 2.73 ms */
250 .als_prox_config
= 0,
251 .als_gain_trim
= 1000,
252 .als_cal_target
= 150,
253 .als_persistence
= 1,
254 .als_interrupt_en
= false,
255 .als_thresh_low
= 200,
256 .als_thresh_high
= 256,
257 .prox_persistence
= 1,
258 .prox_interrupt_en
= false,
260 .prox_thres_high
= 512,
261 .prox_max_samples_cal
= 30,
262 .prox_pulse_count
= 8,
263 .prox_diode
= TSL2772_DIODE1
,
264 .prox_power
= TSL2772_100_mA
267 static const s16 tsl2772_als_gain
[] = {
274 static const s16 tsl2772_prox_gain
[] = {
281 static const int tsl2772_int_time_avail
[][6] = {
282 [tsl2571
] = { 0, 2720, 0, 2720, 0, 696000 },
283 [tsl2671
] = { 0, 2720, 0, 2720, 0, 696000 },
284 [tmd2671
] = { 0, 2720, 0, 2720, 0, 696000 },
285 [tsl2771
] = { 0, 2720, 0, 2720, 0, 696000 },
286 [tmd2771
] = { 0, 2720, 0, 2720, 0, 696000 },
287 [tsl2572
] = { 0, 2730, 0, 2730, 0, 699000 },
288 [tsl2672
] = { 0, 2730, 0, 2730, 0, 699000 },
289 [tmd2672
] = { 0, 2730, 0, 2730, 0, 699000 },
290 [tsl2772
] = { 0, 2730, 0, 2730, 0, 699000 },
291 [tmd2772
] = { 0, 2730, 0, 2730, 0, 699000 },
292 [apds9930
] = { 0, 2730, 0, 2730, 0, 699000 },
295 static int tsl2772_int_calibscale_avail
[] = { 1, 8, 16, 120 };
297 static int tsl2772_prox_calibscale_avail
[] = { 1, 2, 4, 8 };
299 /* Channel variations */
308 static const u8 device_channel_config
[] = {
319 [apds9930
] = ALSPRX2
,
322 static int tsl2772_read_status(struct tsl2772_chip
*chip
)
326 ret
= i2c_smbus_read_byte_data(chip
->client
,
327 TSL2772_CMD_REG
| TSL2772_STATUS
);
329 dev_err(&chip
->client
->dev
,
330 "%s: failed to read STATUS register: %d\n", __func__
,
336 static int tsl2772_write_control_reg(struct tsl2772_chip
*chip
, u8 data
)
340 ret
= i2c_smbus_write_byte_data(chip
->client
,
341 TSL2772_CMD_REG
| TSL2772_CNTRL
, data
);
343 dev_err(&chip
->client
->dev
,
344 "%s: failed to write to control register %x: %d\n",
345 __func__
, data
, ret
);
351 static int tsl2772_read_autoinc_regs(struct tsl2772_chip
*chip
, int lower_reg
,
357 ret
= i2c_smbus_write_byte(chip
->client
,
358 TSL2772_CMD_REG
| TSL2772_CMD_AUTOINC_PROTO
|
361 dev_err(&chip
->client
->dev
,
362 "%s: failed to enable auto increment protocol: %d\n",
367 ret
= i2c_smbus_read_byte_data(chip
->client
,
368 TSL2772_CMD_REG
| lower_reg
);
370 dev_err(&chip
->client
->dev
,
371 "%s: failed to read from register %x: %d\n", __func__
,
377 ret
= i2c_smbus_read_byte_data(chip
->client
,
378 TSL2772_CMD_REG
| upper_reg
);
380 dev_err(&chip
->client
->dev
,
381 "%s: failed to read from register %x: %d\n", __func__
,
387 ret
= i2c_smbus_write_byte(chip
->client
,
388 TSL2772_CMD_REG
| TSL2772_CMD_REPEAT_PROTO
|
391 dev_err(&chip
->client
->dev
,
392 "%s: failed to enable repeated byte protocol: %d\n",
397 return le16_to_cpup((const __le16
*)&buf
[0]);
401 * tsl2772_get_lux() - Reads and calculates current lux value.
402 * @indio_dev: pointer to IIO device
404 * The raw ch0 and ch1 values of the ambient light sensed in the last
405 * integration cycle are read from the device. The raw values are multiplied
406 * by a device-specific scale factor, and divided by the integration time and
407 * device gain. The code supports multiple lux equations through the lux table
408 * coefficients. A lux gain trim is applied to each lux equation, and then the
409 * maximum lux within the interval 0..65535 is selected.
411 static int tsl2772_get_lux(struct iio_dev
*indio_dev
)
413 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
414 struct tsl2772_lux
*p
;
418 mutex_lock(&chip
->als_mutex
);
420 if (chip
->tsl2772_chip_status
!= TSL2772_CHIP_WORKING
) {
421 dev_err(&chip
->client
->dev
, "%s: device is not enabled\n",
427 ret
= tsl2772_read_status(chip
);
431 if (!(ret
& TSL2772_STA_ADC_VALID
)) {
432 dev_err(&chip
->client
->dev
,
433 "%s: data not valid yet\n", __func__
);
434 ret
= chip
->als_cur_info
.lux
; /* return LAST VALUE */
438 ret
= tsl2772_read_autoinc_regs(chip
, TSL2772_ALS_CHAN0LO
,
439 TSL2772_ALS_CHAN0HI
);
442 chip
->als_cur_info
.als_ch0
= ret
;
444 ret
= tsl2772_read_autoinc_regs(chip
, TSL2772_ALS_CHAN1LO
,
445 TSL2772_ALS_CHAN1HI
);
448 chip
->als_cur_info
.als_ch1
= ret
;
450 if (chip
->als_cur_info
.als_ch0
>= chip
->als_saturation
) {
451 max_lux
= TSL2772_LUX_CALC_OVER_FLOW
;
452 goto update_struct_with_max_lux
;
455 if (!chip
->als_cur_info
.als_ch0
) {
456 /* have no data, so return LAST VALUE */
457 ret
= chip
->als_cur_info
.lux
;
463 for (p
= (struct tsl2772_lux
*)chip
->tsl2772_device_lux
; p
->ch0
!= 0;
467 lux
= ((chip
->als_cur_info
.als_ch0
* p
->ch0
) -
468 (chip
->als_cur_info
.als_ch1
* p
->ch1
)) /
469 chip
->als_gain_time_scale
;
472 * The als_gain_trim can have a value within the range 250..4000
473 * and is a multiplier for the lux. A trim of 1000 makes no
474 * changes to the lux, less than 1000 scales it down, and
475 * greater than 1000 scales it up.
477 lux
= (lux
* chip
->settings
.als_gain_trim
) / 1000;
479 if (lux
> TSL2772_LUX_CALC_OVER_FLOW
) {
484 max_lux
= max(max_lux
, lux
);
487 if (overflow
&& max_lux
== 0)
488 max_lux
= TSL2772_LUX_CALC_OVER_FLOW
;
490 update_struct_with_max_lux
:
491 chip
->als_cur_info
.lux
= max_lux
;
495 mutex_unlock(&chip
->als_mutex
);
501 * tsl2772_get_prox() - Reads proximity data registers and updates
504 * @indio_dev: pointer to IIO device
506 static int tsl2772_get_prox(struct iio_dev
*indio_dev
)
508 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
511 mutex_lock(&chip
->prox_mutex
);
513 ret
= tsl2772_read_status(chip
);
523 if (!(ret
& TSL2772_STA_ADC_VALID
)) {
534 if (!(ret
& TSL2772_STA_PRX_VALID
)) {
541 ret
= tsl2772_read_autoinc_regs(chip
, TSL2772_PRX_LO
, TSL2772_PRX_HI
);
544 chip
->prox_data
= ret
;
547 mutex_unlock(&chip
->prox_mutex
);
552 static int tsl2772_read_prox_led_current(struct tsl2772_chip
*chip
)
554 struct device
*dev
= &chip
->client
->dev
;
557 ret
= device_property_read_u32(dev
, "led-max-microamp", &tmp
);
561 for (i
= 0; tsl2772_led_currents
[i
][0] != 0; i
++) {
562 if (tmp
== tsl2772_led_currents
[i
][0]) {
563 chip
->settings
.prox_power
= tsl2772_led_currents
[i
][1];
568 dev_err(dev
, "Invalid value %d for led-max-microamp\n", tmp
);
573 static int tsl2772_read_prox_diodes(struct tsl2772_chip
*chip
)
575 struct device
*dev
= &chip
->client
->dev
;
576 int i
, ret
, num_leds
, prox_diode_mask
;
577 u32 leds
[TSL2772_MAX_PROX_LEDS
];
579 ret
= device_property_count_u32(dev
, "amstaos,proximity-diodes");
584 if (num_leds
> TSL2772_MAX_PROX_LEDS
)
585 num_leds
= TSL2772_MAX_PROX_LEDS
;
587 ret
= device_property_read_u32_array(dev
, "amstaos,proximity-diodes", leds
, num_leds
);
589 dev_err(dev
, "Invalid value for amstaos,proximity-diodes: %d.\n", ret
);
594 for (i
= 0; i
< num_leds
; i
++) {
596 prox_diode_mask
|= TSL2772_DIODE0
;
597 else if (leds
[i
] == 1)
598 prox_diode_mask
|= TSL2772_DIODE1
;
600 dev_err(dev
, "Invalid value %d in amstaos,proximity-diodes.\n", leds
[i
]);
604 chip
->settings
.prox_diode
= prox_diode_mask
;
609 static void tsl2772_parse_dt(struct tsl2772_chip
*chip
)
611 tsl2772_read_prox_led_current(chip
);
612 tsl2772_read_prox_diodes(chip
);
616 * tsl2772_defaults() - Populates the device nominal operating parameters
617 * with those provided by a 'platform' data struct or
618 * with prefined defaults.
620 * @chip: pointer to device structure.
622 static void tsl2772_defaults(struct tsl2772_chip
*chip
)
624 /* If Operational settings defined elsewhere.. */
625 if (chip
->pdata
&& chip
->pdata
->platform_default_settings
)
626 memcpy(&chip
->settings
, chip
->pdata
->platform_default_settings
,
627 sizeof(tsl2772_default_settings
));
629 memcpy(&chip
->settings
, &tsl2772_default_settings
,
630 sizeof(tsl2772_default_settings
));
632 /* Load up the proper lux table. */
633 if (chip
->pdata
&& chip
->pdata
->platform_lux_table
[0].ch0
!= 0)
634 memcpy(chip
->tsl2772_device_lux
,
635 chip
->pdata
->platform_lux_table
,
636 sizeof(chip
->pdata
->platform_lux_table
));
638 memcpy(chip
->tsl2772_device_lux
,
639 tsl2772_default_lux_table_group
[chip
->id
],
640 TSL2772_DEFAULT_TABLE_BYTES
);
642 tsl2772_parse_dt(chip
);
646 * tsl2772_als_calibrate() - Obtain single reading and calculate
649 * @indio_dev: pointer to IIO device
651 static int tsl2772_als_calibrate(struct iio_dev
*indio_dev
)
653 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
656 ret
= i2c_smbus_read_byte_data(chip
->client
,
657 TSL2772_CMD_REG
| TSL2772_CNTRL
);
659 dev_err(&chip
->client
->dev
,
660 "%s: failed to read from the CNTRL register\n",
665 if ((ret
& (TSL2772_CNTL_ADC_ENBL
| TSL2772_CNTL_PWR_ON
))
666 != (TSL2772_CNTL_ADC_ENBL
| TSL2772_CNTL_PWR_ON
)) {
667 dev_err(&chip
->client
->dev
,
668 "%s: Device is not powered on and/or ADC is not enabled\n",
671 } else if ((ret
& TSL2772_STA_ADC_VALID
) != TSL2772_STA_ADC_VALID
) {
672 dev_err(&chip
->client
->dev
,
673 "%s: The two ADC channels have not completed an integration cycle\n",
678 lux_val
= tsl2772_get_lux(indio_dev
);
680 dev_err(&chip
->client
->dev
,
681 "%s: failed to get lux\n", __func__
);
687 ret
= (chip
->settings
.als_cal_target
* chip
->settings
.als_gain_trim
) /
689 if (ret
< TSL2772_ALS_GAIN_TRIM_MIN
|| ret
> TSL2772_ALS_GAIN_TRIM_MAX
)
692 chip
->settings
.als_gain_trim
= ret
;
697 static void tsl2772_disable_regulators_action(void *_data
)
699 struct tsl2772_chip
*chip
= _data
;
701 regulator_bulk_disable(ARRAY_SIZE(chip
->supplies
), chip
->supplies
);
704 static int tsl2772_chip_on(struct iio_dev
*indio_dev
)
706 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
707 int ret
, i
, als_count
, als_time_us
;
708 u8
*dev_reg
, reg_val
;
710 /* Non calculated parameters */
711 chip
->tsl2772_config
[TSL2772_ALS_TIME
] = chip
->settings
.als_time
;
712 chip
->tsl2772_config
[TSL2772_PRX_TIME
] = chip
->settings
.prox_time
;
713 chip
->tsl2772_config
[TSL2772_WAIT_TIME
] = chip
->settings
.wait_time
;
714 chip
->tsl2772_config
[TSL2772_ALS_PRX_CONFIG
] =
715 chip
->settings
.als_prox_config
;
717 chip
->tsl2772_config
[TSL2772_ALS_MINTHRESHLO
] =
718 (chip
->settings
.als_thresh_low
) & 0xFF;
719 chip
->tsl2772_config
[TSL2772_ALS_MINTHRESHHI
] =
720 (chip
->settings
.als_thresh_low
>> 8) & 0xFF;
721 chip
->tsl2772_config
[TSL2772_ALS_MAXTHRESHLO
] =
722 (chip
->settings
.als_thresh_high
) & 0xFF;
723 chip
->tsl2772_config
[TSL2772_ALS_MAXTHRESHHI
] =
724 (chip
->settings
.als_thresh_high
>> 8) & 0xFF;
725 chip
->tsl2772_config
[TSL2772_PERSISTENCE
] =
726 (chip
->settings
.prox_persistence
& 0xFF) << 4 |
727 (chip
->settings
.als_persistence
& 0xFF);
729 chip
->tsl2772_config
[TSL2772_PRX_COUNT
] =
730 chip
->settings
.prox_pulse_count
;
731 chip
->tsl2772_config
[TSL2772_PRX_MINTHRESHLO
] =
732 (chip
->settings
.prox_thres_low
) & 0xFF;
733 chip
->tsl2772_config
[TSL2772_PRX_MINTHRESHHI
] =
734 (chip
->settings
.prox_thres_low
>> 8) & 0xFF;
735 chip
->tsl2772_config
[TSL2772_PRX_MAXTHRESHLO
] =
736 (chip
->settings
.prox_thres_high
) & 0xFF;
737 chip
->tsl2772_config
[TSL2772_PRX_MAXTHRESHHI
] =
738 (chip
->settings
.prox_thres_high
>> 8) & 0xFF;
740 /* and make sure we're not already on */
741 if (chip
->tsl2772_chip_status
== TSL2772_CHIP_WORKING
) {
742 /* if forcing a register update - turn off, then on */
743 dev_info(&chip
->client
->dev
, "device is already enabled\n");
747 /* Set the gain based on tsl2772_settings struct */
748 chip
->tsl2772_config
[TSL2772_GAIN
] =
749 (chip
->settings
.als_gain
& 0xFF) |
750 ((chip
->settings
.prox_gain
& 0xFF) << 2) |
751 (chip
->settings
.prox_diode
<< 4) |
752 (chip
->settings
.prox_power
<< 6);
754 /* set chip time scaling and saturation */
755 als_count
= 256 - chip
->settings
.als_time
;
756 als_time_us
= als_count
* tsl2772_int_time_avail
[chip
->id
][3];
757 chip
->als_saturation
= als_count
* 768; /* 75% of full scale */
758 chip
->als_gain_time_scale
= als_time_us
*
759 tsl2772_als_gain
[chip
->settings
.als_gain
];
762 * TSL2772 Specific power-on / adc enable sequence
763 * Power on the device 1st.
765 ret
= tsl2772_write_control_reg(chip
, TSL2772_CNTL_PWR_ON
);
770 * Use the following shadow copy for our delay before enabling ADC.
771 * Write all the registers.
773 for (i
= 0, dev_reg
= chip
->tsl2772_config
;
774 i
< TSL2772_MAX_CONFIG_REG
; i
++) {
775 int reg
= TSL2772_CMD_REG
+ i
;
777 ret
= i2c_smbus_write_byte_data(chip
->client
, reg
,
780 dev_err(&chip
->client
->dev
,
781 "%s: failed to write to register %x: %d\n",
787 /* Power-on settling time */
788 usleep_range(3000, 3500);
790 reg_val
= TSL2772_CNTL_PWR_ON
| TSL2772_CNTL_ADC_ENBL
|
791 TSL2772_CNTL_PROX_DET_ENBL
;
792 if (chip
->settings
.als_interrupt_en
)
793 reg_val
|= TSL2772_CNTL_ALS_INT_ENBL
;
794 if (chip
->settings
.prox_interrupt_en
)
795 reg_val
|= TSL2772_CNTL_PROX_INT_ENBL
;
797 ret
= tsl2772_write_control_reg(chip
, reg_val
);
801 ret
= i2c_smbus_write_byte(chip
->client
,
802 TSL2772_CMD_REG
| TSL2772_CMD_SPL_FN
|
803 TSL2772_CMD_PROXALS_INT_CLR
);
805 dev_err(&chip
->client
->dev
,
806 "%s: failed to clear interrupt status: %d\n",
811 chip
->tsl2772_chip_status
= TSL2772_CHIP_WORKING
;
816 static int tsl2772_chip_off(struct iio_dev
*indio_dev
)
818 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
820 /* turn device off */
821 chip
->tsl2772_chip_status
= TSL2772_CHIP_SUSPENDED
;
822 return tsl2772_write_control_reg(chip
, 0x00);
825 static void tsl2772_chip_off_action(void *data
)
827 struct iio_dev
*indio_dev
= data
;
829 tsl2772_chip_off(indio_dev
);
833 * tsl2772_invoke_change - power cycle the device to implement the user
835 * @indio_dev: pointer to IIO device
837 * Obtain and lock both ALS and PROX resources, determine and save device state
838 * (On/Off), cycle device to implement updated parameter, put device back into
839 * proper state, and unlock resource.
841 static int tsl2772_invoke_change(struct iio_dev
*indio_dev
)
843 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
844 int device_status
= chip
->tsl2772_chip_status
;
847 mutex_lock(&chip
->als_mutex
);
848 mutex_lock(&chip
->prox_mutex
);
850 if (device_status
== TSL2772_CHIP_WORKING
) {
851 ret
= tsl2772_chip_off(indio_dev
);
856 ret
= tsl2772_chip_on(indio_dev
);
859 mutex_unlock(&chip
->prox_mutex
);
860 mutex_unlock(&chip
->als_mutex
);
865 static int tsl2772_prox_cal(struct iio_dev
*indio_dev
)
867 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
868 int prox_history
[MAX_SAMPLES_CAL
+ 1];
869 int i
, ret
, mean
, max
, sample_sum
;
871 if (chip
->settings
.prox_max_samples_cal
< 1 ||
872 chip
->settings
.prox_max_samples_cal
> MAX_SAMPLES_CAL
)
875 for (i
= 0; i
< chip
->settings
.prox_max_samples_cal
; i
++) {
876 usleep_range(15000, 17500);
877 ret
= tsl2772_get_prox(indio_dev
);
881 prox_history
[i
] = chip
->prox_data
;
886 for (i
= 0; i
< chip
->settings
.prox_max_samples_cal
; i
++) {
887 sample_sum
+= prox_history
[i
];
888 max
= max(max
, prox_history
[i
]);
890 mean
= sample_sum
/ chip
->settings
.prox_max_samples_cal
;
892 chip
->settings
.prox_thres_high
= (max
<< 1) - mean
;
894 return tsl2772_invoke_change(indio_dev
);
897 static int tsl2772_read_avail(struct iio_dev
*indio_dev
,
898 struct iio_chan_spec
const *chan
,
899 const int **vals
, int *type
, int *length
,
902 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
905 case IIO_CHAN_INFO_CALIBSCALE
:
906 if (chan
->type
== IIO_INTENSITY
) {
907 *length
= ARRAY_SIZE(tsl2772_int_calibscale_avail
);
908 *vals
= tsl2772_int_calibscale_avail
;
910 *length
= ARRAY_SIZE(tsl2772_prox_calibscale_avail
);
911 *vals
= tsl2772_prox_calibscale_avail
;
914 return IIO_AVAIL_LIST
;
915 case IIO_CHAN_INFO_INT_TIME
:
916 *length
= ARRAY_SIZE(tsl2772_int_time_avail
[chip
->id
]);
917 *vals
= tsl2772_int_time_avail
[chip
->id
];
918 *type
= IIO_VAL_INT_PLUS_MICRO
;
919 return IIO_AVAIL_RANGE
;
925 static ssize_t
in_illuminance0_target_input_show(struct device
*dev
,
926 struct device_attribute
*attr
,
929 struct tsl2772_chip
*chip
= iio_priv(dev_to_iio_dev(dev
));
931 return scnprintf(buf
, PAGE_SIZE
, "%d\n", chip
->settings
.als_cal_target
);
934 static ssize_t
in_illuminance0_target_input_store(struct device
*dev
,
935 struct device_attribute
*attr
,
936 const char *buf
, size_t len
)
938 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
939 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
943 if (kstrtou16(buf
, 0, &value
))
946 chip
->settings
.als_cal_target
= value
;
947 ret
= tsl2772_invoke_change(indio_dev
);
954 static ssize_t
in_illuminance0_calibrate_store(struct device
*dev
,
955 struct device_attribute
*attr
,
956 const char *buf
, size_t len
)
958 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
962 if (kstrtobool(buf
, &value
) || !value
)
965 ret
= tsl2772_als_calibrate(indio_dev
);
969 ret
= tsl2772_invoke_change(indio_dev
);
976 static ssize_t
in_illuminance0_lux_table_show(struct device
*dev
,
977 struct device_attribute
*attr
,
980 struct tsl2772_chip
*chip
= iio_priv(dev_to_iio_dev(dev
));
984 while (i
< TSL2772_MAX_LUX_TABLE_SIZE
) {
985 offset
+= scnprintf(buf
+ offset
, PAGE_SIZE
- offset
, "%u,%u,",
986 chip
->tsl2772_device_lux
[i
].ch0
,
987 chip
->tsl2772_device_lux
[i
].ch1
);
988 if (chip
->tsl2772_device_lux
[i
].ch0
== 0) {
990 * We just printed the first "0" entry.
991 * Now get rid of the extra "," and break.
999 offset
+= scnprintf(buf
+ offset
, PAGE_SIZE
- offset
, "\n");
1003 static ssize_t
in_illuminance0_lux_table_store(struct device
*dev
,
1004 struct device_attribute
*attr
,
1005 const char *buf
, size_t len
)
1007 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
1008 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
1009 int value
[ARRAY_SIZE(chip
->tsl2772_device_lux
) * 2 + 1];
1012 get_options(buf
, ARRAY_SIZE(value
), value
);
1015 * We now have an array of ints starting at value[1], and
1016 * enumerated by value[0].
1017 * We expect each group of two ints to be one table entry,
1018 * and the last table entry is all 0.
1021 if ((n
% 2) || n
< 4 ||
1022 n
> ((ARRAY_SIZE(chip
->tsl2772_device_lux
) - 1) * 2))
1025 if ((value
[(n
- 1)] | value
[n
]) != 0)
1028 if (chip
->tsl2772_chip_status
== TSL2772_CHIP_WORKING
) {
1029 ret
= tsl2772_chip_off(indio_dev
);
1034 /* Zero out the table */
1035 memset(chip
->tsl2772_device_lux
, 0, sizeof(chip
->tsl2772_device_lux
));
1036 memcpy(chip
->tsl2772_device_lux
, &value
[1], (value
[0] * 4));
1038 ret
= tsl2772_invoke_change(indio_dev
);
1045 static ssize_t
in_proximity0_calibrate_store(struct device
*dev
,
1046 struct device_attribute
*attr
,
1047 const char *buf
, size_t len
)
1049 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
1053 if (kstrtobool(buf
, &value
) || !value
)
1056 ret
= tsl2772_prox_cal(indio_dev
);
1060 ret
= tsl2772_invoke_change(indio_dev
);
1067 static int tsl2772_read_interrupt_config(struct iio_dev
*indio_dev
,
1068 const struct iio_chan_spec
*chan
,
1069 enum iio_event_type type
,
1070 enum iio_event_direction dir
)
1072 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
1074 if (chan
->type
== IIO_INTENSITY
)
1075 return chip
->settings
.als_interrupt_en
;
1077 return chip
->settings
.prox_interrupt_en
;
1080 static int tsl2772_write_interrupt_config(struct iio_dev
*indio_dev
,
1081 const struct iio_chan_spec
*chan
,
1082 enum iio_event_type type
,
1083 enum iio_event_direction dir
,
1086 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
1088 if (chan
->type
== IIO_INTENSITY
)
1089 chip
->settings
.als_interrupt_en
= val
;
1091 chip
->settings
.prox_interrupt_en
= val
;
1093 return tsl2772_invoke_change(indio_dev
);
1096 static int tsl2772_write_event_value(struct iio_dev
*indio_dev
,
1097 const struct iio_chan_spec
*chan
,
1098 enum iio_event_type type
,
1099 enum iio_event_direction dir
,
1100 enum iio_event_info info
,
1103 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
1104 int ret
= -EINVAL
, count
, persistence
;
1108 case IIO_EV_INFO_VALUE
:
1109 if (chan
->type
== IIO_INTENSITY
) {
1111 case IIO_EV_DIR_RISING
:
1112 chip
->settings
.als_thresh_high
= val
;
1115 case IIO_EV_DIR_FALLING
:
1116 chip
->settings
.als_thresh_low
= val
;
1124 case IIO_EV_DIR_RISING
:
1125 chip
->settings
.prox_thres_high
= val
;
1128 case IIO_EV_DIR_FALLING
:
1129 chip
->settings
.prox_thres_low
= val
;
1137 case IIO_EV_INFO_PERIOD
:
1138 if (chan
->type
== IIO_INTENSITY
)
1139 time
= chip
->settings
.als_time
;
1141 time
= chip
->settings
.prox_time
;
1144 persistence
= ((val
* 1000000) + val2
) /
1145 (count
* tsl2772_int_time_avail
[chip
->id
][3]);
1147 if (chan
->type
== IIO_INTENSITY
) {
1148 /* ALS filter values are 1, 2, 3, 5, 10, 15, ..., 60 */
1149 if (persistence
> 3)
1150 persistence
= (persistence
/ 5) + 3;
1152 chip
->settings
.als_persistence
= persistence
;
1154 chip
->settings
.prox_persistence
= persistence
;
1166 return tsl2772_invoke_change(indio_dev
);
1169 static int tsl2772_read_event_value(struct iio_dev
*indio_dev
,
1170 const struct iio_chan_spec
*chan
,
1171 enum iio_event_type type
,
1172 enum iio_event_direction dir
,
1173 enum iio_event_info info
,
1174 int *val
, int *val2
)
1176 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
1177 int filter_delay
, persistence
;
1181 case IIO_EV_INFO_VALUE
:
1182 if (chan
->type
== IIO_INTENSITY
) {
1184 case IIO_EV_DIR_RISING
:
1185 *val
= chip
->settings
.als_thresh_high
;
1187 case IIO_EV_DIR_FALLING
:
1188 *val
= chip
->settings
.als_thresh_low
;
1195 case IIO_EV_DIR_RISING
:
1196 *val
= chip
->settings
.prox_thres_high
;
1198 case IIO_EV_DIR_FALLING
:
1199 *val
= chip
->settings
.prox_thres_low
;
1206 case IIO_EV_INFO_PERIOD
:
1207 if (chan
->type
== IIO_INTENSITY
) {
1208 time
= chip
->settings
.als_time
;
1209 persistence
= chip
->settings
.als_persistence
;
1211 /* ALS filter values are 1, 2, 3, 5, 10, 15, ..., 60 */
1212 if (persistence
> 3)
1213 persistence
= (persistence
- 3) * 5;
1215 time
= chip
->settings
.prox_time
;
1216 persistence
= chip
->settings
.prox_persistence
;
1219 filter_delay
= persistence
* (256 - time
) *
1220 tsl2772_int_time_avail
[chip
->id
][3];
1222 *val
= filter_delay
/ 1000000;
1223 *val2
= filter_delay
% 1000000;
1224 return IIO_VAL_INT_PLUS_MICRO
;
1230 static int tsl2772_read_raw(struct iio_dev
*indio_dev
,
1231 struct iio_chan_spec
const *chan
,
1236 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
1239 case IIO_CHAN_INFO_PROCESSED
:
1240 switch (chan
->type
) {
1242 tsl2772_get_lux(indio_dev
);
1243 *val
= chip
->als_cur_info
.lux
;
1248 case IIO_CHAN_INFO_RAW
:
1249 switch (chan
->type
) {
1251 tsl2772_get_lux(indio_dev
);
1252 if (chan
->channel
== 0)
1253 *val
= chip
->als_cur_info
.als_ch0
;
1255 *val
= chip
->als_cur_info
.als_ch1
;
1258 tsl2772_get_prox(indio_dev
);
1259 *val
= chip
->prox_data
;
1265 case IIO_CHAN_INFO_CALIBSCALE
:
1266 if (chan
->type
== IIO_LIGHT
)
1267 *val
= tsl2772_als_gain
[chip
->settings
.als_gain
];
1269 *val
= tsl2772_prox_gain
[chip
->settings
.prox_gain
];
1271 case IIO_CHAN_INFO_CALIBBIAS
:
1272 *val
= chip
->settings
.als_gain_trim
;
1274 case IIO_CHAN_INFO_INT_TIME
:
1276 *val2
= (256 - chip
->settings
.als_time
) *
1277 tsl2772_int_time_avail
[chip
->id
][3];
1278 return IIO_VAL_INT_PLUS_MICRO
;
1284 static int tsl2772_write_raw(struct iio_dev
*indio_dev
,
1285 struct iio_chan_spec
const *chan
,
1290 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
1293 case IIO_CHAN_INFO_CALIBSCALE
:
1294 if (chan
->type
== IIO_INTENSITY
) {
1297 chip
->settings
.als_gain
= 0;
1300 chip
->settings
.als_gain
= 1;
1303 chip
->settings
.als_gain
= 2;
1306 chip
->settings
.als_gain
= 3;
1314 chip
->settings
.prox_gain
= 0;
1317 chip
->settings
.prox_gain
= 1;
1320 chip
->settings
.prox_gain
= 2;
1323 chip
->settings
.prox_gain
= 3;
1330 case IIO_CHAN_INFO_CALIBBIAS
:
1331 if (val
< TSL2772_ALS_GAIN_TRIM_MIN
||
1332 val
> TSL2772_ALS_GAIN_TRIM_MAX
)
1335 chip
->settings
.als_gain_trim
= val
;
1337 case IIO_CHAN_INFO_INT_TIME
:
1338 if (val
!= 0 || val2
< tsl2772_int_time_avail
[chip
->id
][1] ||
1339 val2
> tsl2772_int_time_avail
[chip
->id
][5])
1342 chip
->settings
.als_time
= 256 -
1343 (val2
/ tsl2772_int_time_avail
[chip
->id
][3]);
1349 return tsl2772_invoke_change(indio_dev
);
1352 static DEVICE_ATTR_RW(in_illuminance0_target_input
);
1354 static DEVICE_ATTR_WO(in_illuminance0_calibrate
);
1356 static DEVICE_ATTR_WO(in_proximity0_calibrate
);
1358 static DEVICE_ATTR_RW(in_illuminance0_lux_table
);
1360 /* Use the default register values to identify the Taos device */
1361 static int tsl2772_device_id_verif(int id
, int target
)
1367 return (id
& 0xf0) == TRITON_ID
;
1370 return (id
& 0xf0) == HALIBUT_ID
;
1377 return (id
& 0xf0) == SWORDFISH_ID
;
1383 static irqreturn_t
tsl2772_event_handler(int irq
, void *private)
1385 struct iio_dev
*indio_dev
= private;
1386 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
1387 s64 timestamp
= iio_get_time_ns(indio_dev
);
1390 ret
= tsl2772_read_status(chip
);
1394 /* What type of interrupt do we need to process */
1395 if (ret
& TSL2772_STA_PRX_INTR
) {
1396 iio_push_event(indio_dev
,
1397 IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY
,
1404 if (ret
& TSL2772_STA_ALS_INTR
) {
1405 iio_push_event(indio_dev
,
1406 IIO_UNMOD_EVENT_CODE(IIO_LIGHT
,
1413 ret
= i2c_smbus_write_byte(chip
->client
,
1414 TSL2772_CMD_REG
| TSL2772_CMD_SPL_FN
|
1415 TSL2772_CMD_PROXALS_INT_CLR
);
1417 dev_err(&chip
->client
->dev
,
1418 "%s: failed to clear interrupt status: %d\n",
1424 static struct attribute
*tsl2772_ALS_device_attrs
[] = {
1425 &dev_attr_in_illuminance0_target_input
.attr
,
1426 &dev_attr_in_illuminance0_calibrate
.attr
,
1427 &dev_attr_in_illuminance0_lux_table
.attr
,
1431 static struct attribute
*tsl2772_PRX_device_attrs
[] = {
1432 &dev_attr_in_proximity0_calibrate
.attr
,
1436 static struct attribute
*tsl2772_ALSPRX_device_attrs
[] = {
1437 &dev_attr_in_illuminance0_target_input
.attr
,
1438 &dev_attr_in_illuminance0_calibrate
.attr
,
1439 &dev_attr_in_illuminance0_lux_table
.attr
,
1443 static struct attribute
*tsl2772_PRX2_device_attrs
[] = {
1444 &dev_attr_in_proximity0_calibrate
.attr
,
1448 static struct attribute
*tsl2772_ALSPRX2_device_attrs
[] = {
1449 &dev_attr_in_illuminance0_target_input
.attr
,
1450 &dev_attr_in_illuminance0_calibrate
.attr
,
1451 &dev_attr_in_illuminance0_lux_table
.attr
,
1452 &dev_attr_in_proximity0_calibrate
.attr
,
1456 static const struct attribute_group tsl2772_device_attr_group_tbl
[] = {
1458 .attrs
= tsl2772_ALS_device_attrs
,
1461 .attrs
= tsl2772_PRX_device_attrs
,
1464 .attrs
= tsl2772_ALSPRX_device_attrs
,
1467 .attrs
= tsl2772_PRX2_device_attrs
,
1470 .attrs
= tsl2772_ALSPRX2_device_attrs
,
1474 #define TSL2772_DEVICE_INFO(type)[type] = \
1476 .attrs = &tsl2772_device_attr_group_tbl[type], \
1477 .read_raw = &tsl2772_read_raw, \
1478 .read_avail = &tsl2772_read_avail, \
1479 .write_raw = &tsl2772_write_raw, \
1480 .read_event_value = &tsl2772_read_event_value, \
1481 .write_event_value = &tsl2772_write_event_value, \
1482 .read_event_config = &tsl2772_read_interrupt_config, \
1483 .write_event_config = &tsl2772_write_interrupt_config, \
1486 static const struct iio_info tsl2772_device_info
[] = {
1487 TSL2772_DEVICE_INFO(ALS
),
1488 TSL2772_DEVICE_INFO(PRX
),
1489 TSL2772_DEVICE_INFO(ALSPRX
),
1490 TSL2772_DEVICE_INFO(PRX2
),
1491 TSL2772_DEVICE_INFO(ALSPRX2
),
1494 static const struct iio_event_spec tsl2772_events
[] = {
1496 .type
= IIO_EV_TYPE_THRESH
,
1497 .dir
= IIO_EV_DIR_RISING
,
1498 .mask_separate
= BIT(IIO_EV_INFO_VALUE
),
1500 .type
= IIO_EV_TYPE_THRESH
,
1501 .dir
= IIO_EV_DIR_FALLING
,
1502 .mask_separate
= BIT(IIO_EV_INFO_VALUE
),
1504 .type
= IIO_EV_TYPE_THRESH
,
1505 .dir
= IIO_EV_DIR_EITHER
,
1506 .mask_separate
= BIT(IIO_EV_INFO_PERIOD
) |
1507 BIT(IIO_EV_INFO_ENABLE
),
1511 static const struct tsl2772_chip_info tsl2772_chip_info_tbl
[] = {
1513 .channel_with_events
= {
1518 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
1520 .type
= IIO_INTENSITY
,
1523 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1524 BIT(IIO_CHAN_INFO_INT_TIME
) |
1525 BIT(IIO_CHAN_INFO_CALIBSCALE
) |
1526 BIT(IIO_CHAN_INFO_CALIBBIAS
),
1527 .info_mask_separate_available
=
1528 BIT(IIO_CHAN_INFO_INT_TIME
) |
1529 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1530 .event_spec
= tsl2772_events
,
1531 .num_event_specs
= ARRAY_SIZE(tsl2772_events
),
1533 .type
= IIO_INTENSITY
,
1538 .channel_without_events
= {
1543 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
1545 .type
= IIO_INTENSITY
,
1548 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1549 BIT(IIO_CHAN_INFO_INT_TIME
) |
1550 BIT(IIO_CHAN_INFO_CALIBSCALE
) |
1551 BIT(IIO_CHAN_INFO_CALIBBIAS
),
1552 .info_mask_separate_available
=
1553 BIT(IIO_CHAN_INFO_INT_TIME
) |
1554 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1556 .type
= IIO_INTENSITY
,
1561 .chan_table_elements
= 3,
1562 .info
= &tsl2772_device_info
[ALS
],
1565 .channel_with_events
= {
1567 .type
= IIO_PROXIMITY
,
1570 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
1571 .event_spec
= tsl2772_events
,
1572 .num_event_specs
= ARRAY_SIZE(tsl2772_events
),
1575 .channel_without_events
= {
1577 .type
= IIO_PROXIMITY
,
1580 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
1583 .chan_table_elements
= 1,
1584 .info
= &tsl2772_device_info
[PRX
],
1587 .channel_with_events
= {
1592 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
1594 .type
= IIO_INTENSITY
,
1597 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1598 BIT(IIO_CHAN_INFO_INT_TIME
) |
1599 BIT(IIO_CHAN_INFO_CALIBSCALE
) |
1600 BIT(IIO_CHAN_INFO_CALIBBIAS
),
1601 .info_mask_separate_available
=
1602 BIT(IIO_CHAN_INFO_INT_TIME
) |
1603 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1604 .event_spec
= tsl2772_events
,
1605 .num_event_specs
= ARRAY_SIZE(tsl2772_events
),
1607 .type
= IIO_INTENSITY
,
1610 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
1612 .type
= IIO_PROXIMITY
,
1615 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
1616 .event_spec
= tsl2772_events
,
1617 .num_event_specs
= ARRAY_SIZE(tsl2772_events
),
1620 .channel_without_events
= {
1625 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
1627 .type
= IIO_INTENSITY
,
1630 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1631 BIT(IIO_CHAN_INFO_INT_TIME
) |
1632 BIT(IIO_CHAN_INFO_CALIBSCALE
) |
1633 BIT(IIO_CHAN_INFO_CALIBBIAS
),
1634 .info_mask_separate_available
=
1635 BIT(IIO_CHAN_INFO_INT_TIME
) |
1636 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1638 .type
= IIO_INTENSITY
,
1641 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
1643 .type
= IIO_PROXIMITY
,
1646 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
1649 .chan_table_elements
= 4,
1650 .info
= &tsl2772_device_info
[ALSPRX
],
1653 .channel_with_events
= {
1655 .type
= IIO_PROXIMITY
,
1658 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1659 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1660 .info_mask_separate_available
=
1661 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1662 .event_spec
= tsl2772_events
,
1663 .num_event_specs
= ARRAY_SIZE(tsl2772_events
),
1666 .channel_without_events
= {
1668 .type
= IIO_PROXIMITY
,
1671 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1672 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1673 .info_mask_separate_available
=
1674 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1677 .chan_table_elements
= 1,
1678 .info
= &tsl2772_device_info
[PRX2
],
1681 .channel_with_events
= {
1686 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
1688 .type
= IIO_INTENSITY
,
1691 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1692 BIT(IIO_CHAN_INFO_INT_TIME
) |
1693 BIT(IIO_CHAN_INFO_CALIBSCALE
) |
1694 BIT(IIO_CHAN_INFO_CALIBBIAS
),
1695 .info_mask_separate_available
=
1696 BIT(IIO_CHAN_INFO_INT_TIME
) |
1697 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1698 .event_spec
= tsl2772_events
,
1699 .num_event_specs
= ARRAY_SIZE(tsl2772_events
),
1701 .type
= IIO_INTENSITY
,
1704 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
1706 .type
= IIO_PROXIMITY
,
1709 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1710 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1711 .info_mask_separate_available
=
1712 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1713 .event_spec
= tsl2772_events
,
1714 .num_event_specs
= ARRAY_SIZE(tsl2772_events
),
1717 .channel_without_events
= {
1722 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
1724 .type
= IIO_INTENSITY
,
1727 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1728 BIT(IIO_CHAN_INFO_INT_TIME
) |
1729 BIT(IIO_CHAN_INFO_CALIBSCALE
) |
1730 BIT(IIO_CHAN_INFO_CALIBBIAS
),
1731 .info_mask_separate_available
=
1732 BIT(IIO_CHAN_INFO_INT_TIME
) |
1733 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1735 .type
= IIO_INTENSITY
,
1738 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
1740 .type
= IIO_PROXIMITY
,
1743 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1744 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1745 .info_mask_separate_available
=
1746 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1749 .chan_table_elements
= 4,
1750 .info
= &tsl2772_device_info
[ALSPRX2
],
1754 static int tsl2772_probe(struct i2c_client
*clientp
)
1756 const struct i2c_device_id
*id
= i2c_client_get_device_id(clientp
);
1757 struct iio_dev
*indio_dev
;
1758 struct tsl2772_chip
*chip
;
1761 indio_dev
= devm_iio_device_alloc(&clientp
->dev
, sizeof(*chip
));
1765 chip
= iio_priv(indio_dev
);
1766 chip
->client
= clientp
;
1767 i2c_set_clientdata(clientp
, indio_dev
);
1769 chip
->supplies
[TSL2772_SUPPLY_VDD
].supply
= "vdd";
1770 chip
->supplies
[TSL2772_SUPPLY_VDDIO
].supply
= "vddio";
1772 ret
= devm_regulator_bulk_get(&clientp
->dev
,
1773 ARRAY_SIZE(chip
->supplies
),
1776 return dev_err_probe(&clientp
->dev
, ret
, "Failed to get regulators\n");
1778 ret
= regulator_bulk_enable(ARRAY_SIZE(chip
->supplies
), chip
->supplies
);
1780 dev_err(&clientp
->dev
, "Failed to enable regulators: %d\n",
1785 ret
= devm_add_action_or_reset(&clientp
->dev
,
1786 tsl2772_disable_regulators_action
,
1789 dev_err(&clientp
->dev
, "Failed to setup regulator cleanup action %d\n",
1794 usleep_range(TSL2772_BOOT_MIN_SLEEP_TIME
, TSL2772_BOOT_MAX_SLEEP_TIME
);
1796 ret
= i2c_smbus_read_byte_data(chip
->client
,
1797 TSL2772_CMD_REG
| TSL2772_CHIPID
);
1801 if (tsl2772_device_id_verif(ret
, id
->driver_data
) <= 0) {
1802 dev_info(&chip
->client
->dev
,
1803 "%s: i2c device found does not match expected id\n",
1808 ret
= i2c_smbus_write_byte(clientp
, TSL2772_CMD_REG
| TSL2772_CNTRL
);
1810 dev_err(&clientp
->dev
,
1811 "%s: Failed to write to CMD register: %d\n",
1816 mutex_init(&chip
->als_mutex
);
1817 mutex_init(&chip
->prox_mutex
);
1819 chip
->tsl2772_chip_status
= TSL2772_CHIP_UNKNOWN
;
1820 chip
->pdata
= dev_get_platdata(&clientp
->dev
);
1821 chip
->id
= id
->driver_data
;
1823 &tsl2772_chip_info_tbl
[device_channel_config
[id
->driver_data
]];
1825 indio_dev
->info
= chip
->chip_info
->info
;
1826 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1827 indio_dev
->name
= chip
->client
->name
;
1828 indio_dev
->num_channels
= chip
->chip_info
->chan_table_elements
;
1831 indio_dev
->channels
= chip
->chip_info
->channel_with_events
;
1833 ret
= devm_request_threaded_irq(&clientp
->dev
, clientp
->irq
,
1835 &tsl2772_event_handler
,
1836 IRQF_TRIGGER_FALLING
|
1841 dev_err(&clientp
->dev
,
1842 "%s: irq request failed\n", __func__
);
1846 indio_dev
->channels
= chip
->chip_info
->channel_without_events
;
1849 tsl2772_defaults(chip
);
1850 ret
= tsl2772_chip_on(indio_dev
);
1854 ret
= devm_add_action_or_reset(&clientp
->dev
,
1855 tsl2772_chip_off_action
,
1860 return devm_iio_device_register(&clientp
->dev
, indio_dev
);
1863 static int tsl2772_suspend(struct device
*dev
)
1865 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1866 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
1869 ret
= tsl2772_chip_off(indio_dev
);
1870 regulator_bulk_disable(ARRAY_SIZE(chip
->supplies
), chip
->supplies
);
1875 static int tsl2772_resume(struct device
*dev
)
1877 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1878 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
1881 ret
= regulator_bulk_enable(ARRAY_SIZE(chip
->supplies
), chip
->supplies
);
1885 usleep_range(TSL2772_BOOT_MIN_SLEEP_TIME
, TSL2772_BOOT_MAX_SLEEP_TIME
);
1887 return tsl2772_chip_on(indio_dev
);
1890 static const struct i2c_device_id tsl2772_idtable
[] = {
1891 { "tsl2571", tsl2571
},
1892 { "tsl2671", tsl2671
},
1893 { "tmd2671", tmd2671
},
1894 { "tsl2771", tsl2771
},
1895 { "tmd2771", tmd2771
},
1896 { "tsl2572", tsl2572
},
1897 { "tsl2672", tsl2672
},
1898 { "tmd2672", tmd2672
},
1899 { "tsl2772", tsl2772
},
1900 { "tmd2772", tmd2772
},
1901 { "apds9930", apds9930
},
1905 MODULE_DEVICE_TABLE(i2c
, tsl2772_idtable
);
1907 static const struct of_device_id tsl2772_of_match
[] = {
1908 { .compatible
= "amstaos,tsl2571" },
1909 { .compatible
= "amstaos,tsl2671" },
1910 { .compatible
= "amstaos,tmd2671" },
1911 { .compatible
= "amstaos,tsl2771" },
1912 { .compatible
= "amstaos,tmd2771" },
1913 { .compatible
= "amstaos,tsl2572" },
1914 { .compatible
= "amstaos,tsl2672" },
1915 { .compatible
= "amstaos,tmd2672" },
1916 { .compatible
= "amstaos,tsl2772" },
1917 { .compatible
= "amstaos,tmd2772" },
1918 { .compatible
= "avago,apds9930" },
1921 MODULE_DEVICE_TABLE(of
, tsl2772_of_match
);
1923 static const struct dev_pm_ops tsl2772_pm_ops
= {
1924 .suspend
= tsl2772_suspend
,
1925 .resume
= tsl2772_resume
,
1928 static struct i2c_driver tsl2772_driver
= {
1931 .of_match_table
= tsl2772_of_match
,
1932 .pm
= &tsl2772_pm_ops
,
1934 .id_table
= tsl2772_idtable
,
1935 .probe
= tsl2772_probe
,
1938 module_i2c_driver(tsl2772_driver
);
1940 MODULE_AUTHOR("J. August Brenner <Jon.Brenner@ams.com>");
1941 MODULE_AUTHOR("Brian Masney <masneyb@onstation.org>");
1942 MODULE_DESCRIPTION("TAOS tsl2772 ambient and proximity light sensor driver");
1943 MODULE_LICENSE("GPL");