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>
23 #include <linux/regulator/consumer.h>
26 #define PROX_STAT_CAL 0
27 #define PROX_STAT_SAMP 1
28 #define MAX_SAMPLES_CAL 200
30 /* TSL2772 Device ID */
31 #define TRITON_ID 0x00
32 #define SWORDFISH_ID 0x30
33 #define HALIBUT_ID 0x20
35 /* Lux calculation constants */
36 #define TSL2772_LUX_CALC_OVER_FLOW 65535
39 * TAOS Register definitions - Note: depending on device, some of these register
40 * are not used and the register address is benign.
43 /* Register offsets */
44 #define TSL2772_MAX_CONFIG_REG 16
46 /* Device Registers and Masks */
47 #define TSL2772_CNTRL 0x00
48 #define TSL2772_ALS_TIME 0X01
49 #define TSL2772_PRX_TIME 0x02
50 #define TSL2772_WAIT_TIME 0x03
51 #define TSL2772_ALS_MINTHRESHLO 0X04
52 #define TSL2772_ALS_MINTHRESHHI 0X05
53 #define TSL2772_ALS_MAXTHRESHLO 0X06
54 #define TSL2772_ALS_MAXTHRESHHI 0X07
55 #define TSL2772_PRX_MINTHRESHLO 0X08
56 #define TSL2772_PRX_MINTHRESHHI 0X09
57 #define TSL2772_PRX_MAXTHRESHLO 0X0A
58 #define TSL2772_PRX_MAXTHRESHHI 0X0B
59 #define TSL2772_PERSISTENCE 0x0C
60 #define TSL2772_ALS_PRX_CONFIG 0x0D
61 #define TSL2772_PRX_COUNT 0x0E
62 #define TSL2772_GAIN 0x0F
63 #define TSL2772_NOTUSED 0x10
64 #define TSL2772_REVID 0x11
65 #define TSL2772_CHIPID 0x12
66 #define TSL2772_STATUS 0x13
67 #define TSL2772_ALS_CHAN0LO 0x14
68 #define TSL2772_ALS_CHAN0HI 0x15
69 #define TSL2772_ALS_CHAN1LO 0x16
70 #define TSL2772_ALS_CHAN1HI 0x17
71 #define TSL2772_PRX_LO 0x18
72 #define TSL2772_PRX_HI 0x19
74 /* tsl2772 cmd reg masks */
75 #define TSL2772_CMD_REG 0x80
76 #define TSL2772_CMD_SPL_FN 0x60
77 #define TSL2772_CMD_REPEAT_PROTO 0x00
78 #define TSL2772_CMD_AUTOINC_PROTO 0x20
80 #define TSL2772_CMD_PROX_INT_CLR 0X05
81 #define TSL2772_CMD_ALS_INT_CLR 0x06
82 #define TSL2772_CMD_PROXALS_INT_CLR 0X07
84 /* tsl2772 cntrl reg masks */
85 #define TSL2772_CNTL_ADC_ENBL 0x02
86 #define TSL2772_CNTL_PWR_ON 0x01
88 /* tsl2772 status reg masks */
89 #define TSL2772_STA_ADC_VALID 0x01
90 #define TSL2772_STA_PRX_VALID 0x02
91 #define TSL2772_STA_ADC_PRX_VALID (TSL2772_STA_ADC_VALID | \
92 TSL2772_STA_PRX_VALID)
93 #define TSL2772_STA_ALS_INTR 0x10
94 #define TSL2772_STA_PRX_INTR 0x20
96 /* tsl2772 cntrl reg masks */
97 #define TSL2772_CNTL_REG_CLEAR 0x00
98 #define TSL2772_CNTL_PROX_INT_ENBL 0X20
99 #define TSL2772_CNTL_ALS_INT_ENBL 0X10
100 #define TSL2772_CNTL_WAIT_TMR_ENBL 0X08
101 #define TSL2772_CNTL_PROX_DET_ENBL 0X04
102 #define TSL2772_CNTL_PWRON 0x01
103 #define TSL2772_CNTL_ALSPON_ENBL 0x03
104 #define TSL2772_CNTL_INTALSPON_ENBL 0x13
105 #define TSL2772_CNTL_PROXPON_ENBL 0x0F
106 #define TSL2772_CNTL_INTPROXPON_ENBL 0x2F
108 #define TSL2772_ALS_GAIN_TRIM_MIN 250
109 #define TSL2772_ALS_GAIN_TRIM_MAX 4000
111 #define TSL2772_MAX_PROX_LEDS 2
113 #define TSL2772_BOOT_MIN_SLEEP_TIME 10000
114 #define TSL2772_BOOT_MAX_SLEEP_TIME 28000
116 /* Device family members */
132 TSL2772_CHIP_UNKNOWN
= 0,
133 TSL2772_CHIP_WORKING
= 1,
134 TSL2772_CHIP_SUSPENDED
= 2
138 TSL2772_SUPPLY_VDD
= 0,
139 TSL2772_SUPPLY_VDDIO
= 1,
140 TSL2772_NUM_SUPPLIES
= 2
143 /* Per-device data */
144 struct tsl2772_als_info
{
150 struct tsl2772_chip_info
{
151 int chan_table_elements
;
152 struct iio_chan_spec channel_with_events
[4];
153 struct iio_chan_spec channel_without_events
[4];
154 const struct iio_info
*info
;
157 static const int tsl2772_led_currents
[][2] = {
158 { 100000, TSL2772_100_mA
},
159 { 50000, TSL2772_50_mA
},
160 { 25000, TSL2772_25_mA
},
161 { 13000, TSL2772_13_mA
},
165 struct tsl2772_chip
{
167 struct mutex prox_mutex
;
168 struct mutex als_mutex
;
169 struct i2c_client
*client
;
170 struct regulator_bulk_data supplies
[TSL2772_NUM_SUPPLIES
];
172 struct tsl2772_als_info als_cur_info
;
173 struct tsl2772_settings settings
;
174 struct tsl2772_platform_data
*pdata
;
175 int als_gain_time_scale
;
177 int tsl2772_chip_status
;
178 u8 tsl2772_config
[TSL2772_MAX_CONFIG_REG
];
179 const struct tsl2772_chip_info
*chip_info
;
180 const struct iio_info
*info
;
183 * This structure is intentionally large to accommodate
185 * Sized to 9 = max 8 segments + 1 termination segment
187 struct tsl2772_lux tsl2772_device_lux
[TSL2772_MAX_LUX_TABLE_SIZE
];
191 * Different devices require different coefficents, and these numbers were
192 * derived from the 'Lux Equation' section of the various device datasheets.
193 * All of these coefficients assume a Glass Attenuation (GA) factor of 1.
194 * The coefficients are multiplied by 1000 to avoid floating point operations.
195 * The two rows in each table correspond to the Lux1 and Lux2 equations from
198 static const struct tsl2772_lux tsl2x71_lux_table
[TSL2772_DEF_LUX_TABLE_SZ
] = {
204 static const struct tsl2772_lux tmd2x71_lux_table
[TSL2772_DEF_LUX_TABLE_SZ
] = {
210 static const struct tsl2772_lux tsl2x72_lux_table
[TSL2772_DEF_LUX_TABLE_SZ
] = {
216 static const struct tsl2772_lux tmd2x72_lux_table
[TSL2772_DEF_LUX_TABLE_SZ
] = {
222 static const struct tsl2772_lux apds9930_lux_table
[TSL2772_DEF_LUX_TABLE_SZ
] = {
228 static const struct tsl2772_lux
*tsl2772_default_lux_table_group
[] = {
229 [tsl2571
] = tsl2x71_lux_table
,
230 [tsl2671
] = tsl2x71_lux_table
,
231 [tmd2671
] = tmd2x71_lux_table
,
232 [tsl2771
] = tsl2x71_lux_table
,
233 [tmd2771
] = tmd2x71_lux_table
,
234 [tsl2572
] = tsl2x72_lux_table
,
235 [tsl2672
] = tsl2x72_lux_table
,
236 [tmd2672
] = tmd2x72_lux_table
,
237 [tsl2772
] = tsl2x72_lux_table
,
238 [tmd2772
] = tmd2x72_lux_table
,
239 [apds9930
] = apds9930_lux_table
,
242 static const struct tsl2772_settings tsl2772_default_settings
= {
243 .als_time
= 255, /* 2.72 / 2.73 ms */
245 .prox_time
= 255, /* 2.72 / 2.73 ms */
248 .als_prox_config
= 0,
249 .als_gain_trim
= 1000,
250 .als_cal_target
= 150,
251 .als_persistence
= 1,
252 .als_interrupt_en
= false,
253 .als_thresh_low
= 200,
254 .als_thresh_high
= 256,
255 .prox_persistence
= 1,
256 .prox_interrupt_en
= false,
258 .prox_thres_high
= 512,
259 .prox_max_samples_cal
= 30,
260 .prox_pulse_count
= 8,
261 .prox_diode
= TSL2772_DIODE1
,
262 .prox_power
= TSL2772_100_mA
265 static const s16 tsl2772_als_gain
[] = {
272 static const s16 tsl2772_prox_gain
[] = {
279 static const int tsl2772_int_time_avail
[][6] = {
280 [tsl2571
] = { 0, 2720, 0, 2720, 0, 696000 },
281 [tsl2671
] = { 0, 2720, 0, 2720, 0, 696000 },
282 [tmd2671
] = { 0, 2720, 0, 2720, 0, 696000 },
283 [tsl2771
] = { 0, 2720, 0, 2720, 0, 696000 },
284 [tmd2771
] = { 0, 2720, 0, 2720, 0, 696000 },
285 [tsl2572
] = { 0, 2730, 0, 2730, 0, 699000 },
286 [tsl2672
] = { 0, 2730, 0, 2730, 0, 699000 },
287 [tmd2672
] = { 0, 2730, 0, 2730, 0, 699000 },
288 [tsl2772
] = { 0, 2730, 0, 2730, 0, 699000 },
289 [tmd2772
] = { 0, 2730, 0, 2730, 0, 699000 },
290 [apds9930
] = { 0, 2730, 0, 2730, 0, 699000 },
293 static int tsl2772_int_calibscale_avail
[] = { 1, 8, 16, 120 };
295 static int tsl2772_prox_calibscale_avail
[] = { 1, 2, 4, 8 };
297 /* Channel variations */
306 static const u8 device_channel_config
[] = {
317 [apds9930
] = ALSPRX2
,
320 static int tsl2772_read_status(struct tsl2772_chip
*chip
)
324 ret
= i2c_smbus_read_byte_data(chip
->client
,
325 TSL2772_CMD_REG
| TSL2772_STATUS
);
327 dev_err(&chip
->client
->dev
,
328 "%s: failed to read STATUS register: %d\n", __func__
,
334 static int tsl2772_write_control_reg(struct tsl2772_chip
*chip
, u8 data
)
338 ret
= i2c_smbus_write_byte_data(chip
->client
,
339 TSL2772_CMD_REG
| TSL2772_CNTRL
, data
);
341 dev_err(&chip
->client
->dev
,
342 "%s: failed to write to control register %x: %d\n",
343 __func__
, data
, ret
);
349 static int tsl2772_read_autoinc_regs(struct tsl2772_chip
*chip
, int lower_reg
,
355 ret
= i2c_smbus_write_byte(chip
->client
,
356 TSL2772_CMD_REG
| TSL2772_CMD_AUTOINC_PROTO
|
359 dev_err(&chip
->client
->dev
,
360 "%s: failed to enable auto increment protocol: %d\n",
365 ret
= i2c_smbus_read_byte_data(chip
->client
,
366 TSL2772_CMD_REG
| lower_reg
);
368 dev_err(&chip
->client
->dev
,
369 "%s: failed to read from register %x: %d\n", __func__
,
375 ret
= i2c_smbus_read_byte_data(chip
->client
,
376 TSL2772_CMD_REG
| upper_reg
);
378 dev_err(&chip
->client
->dev
,
379 "%s: failed to read from register %x: %d\n", __func__
,
385 ret
= i2c_smbus_write_byte(chip
->client
,
386 TSL2772_CMD_REG
| TSL2772_CMD_REPEAT_PROTO
|
389 dev_err(&chip
->client
->dev
,
390 "%s: failed to enable repeated byte protocol: %d\n",
395 return le16_to_cpup((const __le16
*)&buf
[0]);
399 * tsl2772_get_lux() - Reads and calculates current lux value.
400 * @indio_dev: pointer to IIO device
402 * The raw ch0 and ch1 values of the ambient light sensed in the last
403 * integration cycle are read from the device. The raw values are multiplied
404 * by a device-specific scale factor, and divided by the integration time and
405 * device gain. The code supports multiple lux equations through the lux table
406 * coefficients. A lux gain trim is applied to each lux equation, and then the
407 * maximum lux within the interval 0..65535 is selected.
409 static int tsl2772_get_lux(struct iio_dev
*indio_dev
)
411 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
412 struct tsl2772_lux
*p
;
416 mutex_lock(&chip
->als_mutex
);
418 if (chip
->tsl2772_chip_status
!= TSL2772_CHIP_WORKING
) {
419 dev_err(&chip
->client
->dev
, "%s: device is not enabled\n",
425 ret
= tsl2772_read_status(chip
);
429 if (!(ret
& TSL2772_STA_ADC_VALID
)) {
430 dev_err(&chip
->client
->dev
,
431 "%s: data not valid yet\n", __func__
);
432 ret
= chip
->als_cur_info
.lux
; /* return LAST VALUE */
436 ret
= tsl2772_read_autoinc_regs(chip
, TSL2772_ALS_CHAN0LO
,
437 TSL2772_ALS_CHAN0HI
);
440 chip
->als_cur_info
.als_ch0
= ret
;
442 ret
= tsl2772_read_autoinc_regs(chip
, TSL2772_ALS_CHAN1LO
,
443 TSL2772_ALS_CHAN1HI
);
446 chip
->als_cur_info
.als_ch1
= ret
;
448 if (chip
->als_cur_info
.als_ch0
>= chip
->als_saturation
) {
449 max_lux
= TSL2772_LUX_CALC_OVER_FLOW
;
450 goto update_struct_with_max_lux
;
453 if (!chip
->als_cur_info
.als_ch0
) {
454 /* have no data, so return LAST VALUE */
455 ret
= chip
->als_cur_info
.lux
;
461 for (p
= (struct tsl2772_lux
*)chip
->tsl2772_device_lux
; p
->ch0
!= 0;
465 lux
= ((chip
->als_cur_info
.als_ch0
* p
->ch0
) -
466 (chip
->als_cur_info
.als_ch1
* p
->ch1
)) /
467 chip
->als_gain_time_scale
;
470 * The als_gain_trim can have a value within the range 250..4000
471 * and is a multiplier for the lux. A trim of 1000 makes no
472 * changes to the lux, less than 1000 scales it down, and
473 * greater than 1000 scales it up.
475 lux
= (lux
* chip
->settings
.als_gain_trim
) / 1000;
477 if (lux
> TSL2772_LUX_CALC_OVER_FLOW
) {
482 max_lux
= max(max_lux
, lux
);
485 if (overflow
&& max_lux
== 0)
486 max_lux
= TSL2772_LUX_CALC_OVER_FLOW
;
488 update_struct_with_max_lux
:
489 chip
->als_cur_info
.lux
= max_lux
;
493 mutex_unlock(&chip
->als_mutex
);
499 * tsl2772_get_prox() - Reads proximity data registers and updates
502 * @indio_dev: pointer to IIO device
504 static int tsl2772_get_prox(struct iio_dev
*indio_dev
)
506 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
509 mutex_lock(&chip
->prox_mutex
);
511 ret
= tsl2772_read_status(chip
);
521 if (!(ret
& TSL2772_STA_ADC_VALID
)) {
532 if (!(ret
& TSL2772_STA_PRX_VALID
)) {
539 ret
= tsl2772_read_autoinc_regs(chip
, TSL2772_PRX_LO
, TSL2772_PRX_HI
);
542 chip
->prox_data
= ret
;
545 mutex_unlock(&chip
->prox_mutex
);
550 static int tsl2772_read_prox_led_current(struct tsl2772_chip
*chip
)
552 struct device_node
*of_node
= chip
->client
->dev
.of_node
;
555 ret
= of_property_read_u32(of_node
, "led-max-microamp", &tmp
);
559 for (i
= 0; tsl2772_led_currents
[i
][0] != 0; i
++) {
560 if (tmp
== tsl2772_led_currents
[i
][0]) {
561 chip
->settings
.prox_power
= tsl2772_led_currents
[i
][1];
566 dev_err(&chip
->client
->dev
, "Invalid value %d for led-max-microamp\n",
573 static int tsl2772_read_prox_diodes(struct tsl2772_chip
*chip
)
575 struct device_node
*of_node
= chip
->client
->dev
.of_node
;
576 int i
, ret
, num_leds
, prox_diode_mask
;
577 u32 leds
[TSL2772_MAX_PROX_LEDS
];
579 ret
= of_property_count_u32_elems(of_node
, "amstaos,proximity-diodes");
584 if (num_leds
> TSL2772_MAX_PROX_LEDS
)
585 num_leds
= TSL2772_MAX_PROX_LEDS
;
587 ret
= of_property_read_u32_array(of_node
, "amstaos,proximity-diodes",
590 dev_err(&chip
->client
->dev
,
591 "Invalid value for amstaos,proximity-diodes: %d.\n",
597 for (i
= 0; i
< num_leds
; i
++) {
599 prox_diode_mask
|= TSL2772_DIODE0
;
600 else if (leds
[i
] == 1)
601 prox_diode_mask
|= TSL2772_DIODE1
;
603 dev_err(&chip
->client
->dev
,
604 "Invalid value %d in amstaos,proximity-diodes.\n",
613 static void tsl2772_parse_dt(struct tsl2772_chip
*chip
)
615 tsl2772_read_prox_led_current(chip
);
616 tsl2772_read_prox_diodes(chip
);
620 * tsl2772_defaults() - Populates the device nominal operating parameters
621 * with those provided by a 'platform' data struct or
622 * with prefined defaults.
624 * @chip: pointer to device structure.
626 static void tsl2772_defaults(struct tsl2772_chip
*chip
)
628 /* If Operational settings defined elsewhere.. */
629 if (chip
->pdata
&& chip
->pdata
->platform_default_settings
)
630 memcpy(&chip
->settings
, chip
->pdata
->platform_default_settings
,
631 sizeof(tsl2772_default_settings
));
633 memcpy(&chip
->settings
, &tsl2772_default_settings
,
634 sizeof(tsl2772_default_settings
));
636 /* Load up the proper lux table. */
637 if (chip
->pdata
&& chip
->pdata
->platform_lux_table
[0].ch0
!= 0)
638 memcpy(chip
->tsl2772_device_lux
,
639 chip
->pdata
->platform_lux_table
,
640 sizeof(chip
->pdata
->platform_lux_table
));
642 memcpy(chip
->tsl2772_device_lux
,
643 tsl2772_default_lux_table_group
[chip
->id
],
644 TSL2772_DEFAULT_TABLE_BYTES
);
646 tsl2772_parse_dt(chip
);
650 * tsl2772_als_calibrate() - Obtain single reading and calculate
653 * @indio_dev: pointer to IIO device
655 static int tsl2772_als_calibrate(struct iio_dev
*indio_dev
)
657 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
660 ret
= i2c_smbus_read_byte_data(chip
->client
,
661 TSL2772_CMD_REG
| TSL2772_CNTRL
);
663 dev_err(&chip
->client
->dev
,
664 "%s: failed to read from the CNTRL register\n",
669 if ((ret
& (TSL2772_CNTL_ADC_ENBL
| TSL2772_CNTL_PWR_ON
))
670 != (TSL2772_CNTL_ADC_ENBL
| TSL2772_CNTL_PWR_ON
)) {
671 dev_err(&chip
->client
->dev
,
672 "%s: Device is not powered on and/or ADC is not enabled\n",
675 } else if ((ret
& TSL2772_STA_ADC_VALID
) != TSL2772_STA_ADC_VALID
) {
676 dev_err(&chip
->client
->dev
,
677 "%s: The two ADC channels have not completed an integration cycle\n",
682 lux_val
= tsl2772_get_lux(indio_dev
);
684 dev_err(&chip
->client
->dev
,
685 "%s: failed to get lux\n", __func__
);
691 ret
= (chip
->settings
.als_cal_target
* chip
->settings
.als_gain_trim
) /
693 if (ret
< TSL2772_ALS_GAIN_TRIM_MIN
|| ret
> TSL2772_ALS_GAIN_TRIM_MAX
)
696 chip
->settings
.als_gain_trim
= ret
;
701 static void tsl2772_disable_regulators_action(void *_data
)
703 struct tsl2772_chip
*chip
= _data
;
705 regulator_bulk_disable(ARRAY_SIZE(chip
->supplies
), chip
->supplies
);
708 static int tsl2772_chip_on(struct iio_dev
*indio_dev
)
710 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
711 int ret
, i
, als_count
, als_time_us
;
712 u8
*dev_reg
, reg_val
;
714 /* Non calculated parameters */
715 chip
->tsl2772_config
[TSL2772_ALS_TIME
] = chip
->settings
.als_time
;
716 chip
->tsl2772_config
[TSL2772_PRX_TIME
] = chip
->settings
.prox_time
;
717 chip
->tsl2772_config
[TSL2772_WAIT_TIME
] = chip
->settings
.wait_time
;
718 chip
->tsl2772_config
[TSL2772_ALS_PRX_CONFIG
] =
719 chip
->settings
.als_prox_config
;
721 chip
->tsl2772_config
[TSL2772_ALS_MINTHRESHLO
] =
722 (chip
->settings
.als_thresh_low
) & 0xFF;
723 chip
->tsl2772_config
[TSL2772_ALS_MINTHRESHHI
] =
724 (chip
->settings
.als_thresh_low
>> 8) & 0xFF;
725 chip
->tsl2772_config
[TSL2772_ALS_MAXTHRESHLO
] =
726 (chip
->settings
.als_thresh_high
) & 0xFF;
727 chip
->tsl2772_config
[TSL2772_ALS_MAXTHRESHHI
] =
728 (chip
->settings
.als_thresh_high
>> 8) & 0xFF;
729 chip
->tsl2772_config
[TSL2772_PERSISTENCE
] =
730 (chip
->settings
.prox_persistence
& 0xFF) << 4 |
731 (chip
->settings
.als_persistence
& 0xFF);
733 chip
->tsl2772_config
[TSL2772_PRX_COUNT
] =
734 chip
->settings
.prox_pulse_count
;
735 chip
->tsl2772_config
[TSL2772_PRX_MINTHRESHLO
] =
736 (chip
->settings
.prox_thres_low
) & 0xFF;
737 chip
->tsl2772_config
[TSL2772_PRX_MINTHRESHHI
] =
738 (chip
->settings
.prox_thres_low
>> 8) & 0xFF;
739 chip
->tsl2772_config
[TSL2772_PRX_MAXTHRESHLO
] =
740 (chip
->settings
.prox_thres_high
) & 0xFF;
741 chip
->tsl2772_config
[TSL2772_PRX_MAXTHRESHHI
] =
742 (chip
->settings
.prox_thres_high
>> 8) & 0xFF;
744 /* and make sure we're not already on */
745 if (chip
->tsl2772_chip_status
== TSL2772_CHIP_WORKING
) {
746 /* if forcing a register update - turn off, then on */
747 dev_info(&chip
->client
->dev
, "device is already enabled\n");
751 /* Set the gain based on tsl2772_settings struct */
752 chip
->tsl2772_config
[TSL2772_GAIN
] =
753 (chip
->settings
.als_gain
& 0xFF) |
754 ((chip
->settings
.prox_gain
& 0xFF) << 2) |
755 (chip
->settings
.prox_diode
<< 4) |
756 (chip
->settings
.prox_power
<< 6);
758 /* set chip time scaling and saturation */
759 als_count
= 256 - chip
->settings
.als_time
;
760 als_time_us
= als_count
* tsl2772_int_time_avail
[chip
->id
][3];
761 chip
->als_saturation
= als_count
* 768; /* 75% of full scale */
762 chip
->als_gain_time_scale
= als_time_us
*
763 tsl2772_als_gain
[chip
->settings
.als_gain
];
766 * TSL2772 Specific power-on / adc enable sequence
767 * Power on the device 1st.
769 ret
= tsl2772_write_control_reg(chip
, TSL2772_CNTL_PWR_ON
);
774 * Use the following shadow copy for our delay before enabling ADC.
775 * Write all the registers.
777 for (i
= 0, dev_reg
= chip
->tsl2772_config
;
778 i
< TSL2772_MAX_CONFIG_REG
; i
++) {
779 int reg
= TSL2772_CMD_REG
+ i
;
781 ret
= i2c_smbus_write_byte_data(chip
->client
, reg
,
784 dev_err(&chip
->client
->dev
,
785 "%s: failed to write to register %x: %d\n",
791 /* Power-on settling time */
792 usleep_range(3000, 3500);
794 reg_val
= TSL2772_CNTL_PWR_ON
| TSL2772_CNTL_ADC_ENBL
|
795 TSL2772_CNTL_PROX_DET_ENBL
;
796 if (chip
->settings
.als_interrupt_en
)
797 reg_val
|= TSL2772_CNTL_ALS_INT_ENBL
;
798 if (chip
->settings
.prox_interrupt_en
)
799 reg_val
|= TSL2772_CNTL_PROX_INT_ENBL
;
801 ret
= tsl2772_write_control_reg(chip
, reg_val
);
805 ret
= i2c_smbus_write_byte(chip
->client
,
806 TSL2772_CMD_REG
| TSL2772_CMD_SPL_FN
|
807 TSL2772_CMD_PROXALS_INT_CLR
);
809 dev_err(&chip
->client
->dev
,
810 "%s: failed to clear interrupt status: %d\n",
815 chip
->tsl2772_chip_status
= TSL2772_CHIP_WORKING
;
820 static int tsl2772_chip_off(struct iio_dev
*indio_dev
)
822 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
824 /* turn device off */
825 chip
->tsl2772_chip_status
= TSL2772_CHIP_SUSPENDED
;
826 return tsl2772_write_control_reg(chip
, 0x00);
829 static void tsl2772_chip_off_action(void *data
)
831 struct iio_dev
*indio_dev
= data
;
833 tsl2772_chip_off(indio_dev
);
837 * tsl2772_invoke_change - power cycle the device to implement the user
839 * @indio_dev: pointer to IIO device
841 * Obtain and lock both ALS and PROX resources, determine and save device state
842 * (On/Off), cycle device to implement updated parameter, put device back into
843 * proper state, and unlock resource.
845 static int tsl2772_invoke_change(struct iio_dev
*indio_dev
)
847 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
848 int device_status
= chip
->tsl2772_chip_status
;
851 mutex_lock(&chip
->als_mutex
);
852 mutex_lock(&chip
->prox_mutex
);
854 if (device_status
== TSL2772_CHIP_WORKING
) {
855 ret
= tsl2772_chip_off(indio_dev
);
860 ret
= tsl2772_chip_on(indio_dev
);
863 mutex_unlock(&chip
->prox_mutex
);
864 mutex_unlock(&chip
->als_mutex
);
869 static int tsl2772_prox_cal(struct iio_dev
*indio_dev
)
871 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
872 int prox_history
[MAX_SAMPLES_CAL
+ 1];
873 int i
, ret
, mean
, max
, sample_sum
;
875 if (chip
->settings
.prox_max_samples_cal
< 1 ||
876 chip
->settings
.prox_max_samples_cal
> MAX_SAMPLES_CAL
)
879 for (i
= 0; i
< chip
->settings
.prox_max_samples_cal
; i
++) {
880 usleep_range(15000, 17500);
881 ret
= tsl2772_get_prox(indio_dev
);
885 prox_history
[i
] = chip
->prox_data
;
890 for (i
= 0; i
< chip
->settings
.prox_max_samples_cal
; i
++) {
891 sample_sum
+= prox_history
[i
];
892 max
= max(max
, prox_history
[i
]);
894 mean
= sample_sum
/ chip
->settings
.prox_max_samples_cal
;
896 chip
->settings
.prox_thres_high
= (max
<< 1) - mean
;
898 return tsl2772_invoke_change(indio_dev
);
901 static int tsl2772_read_avail(struct iio_dev
*indio_dev
,
902 struct iio_chan_spec
const *chan
,
903 const int **vals
, int *type
, int *length
,
906 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
909 case IIO_CHAN_INFO_CALIBSCALE
:
910 if (chan
->type
== IIO_INTENSITY
) {
911 *length
= ARRAY_SIZE(tsl2772_int_calibscale_avail
);
912 *vals
= tsl2772_int_calibscale_avail
;
914 *length
= ARRAY_SIZE(tsl2772_prox_calibscale_avail
);
915 *vals
= tsl2772_prox_calibscale_avail
;
918 return IIO_AVAIL_LIST
;
919 case IIO_CHAN_INFO_INT_TIME
:
920 *length
= ARRAY_SIZE(tsl2772_int_time_avail
[chip
->id
]);
921 *vals
= tsl2772_int_time_avail
[chip
->id
];
922 *type
= IIO_VAL_INT_PLUS_MICRO
;
923 return IIO_AVAIL_RANGE
;
929 static ssize_t
in_illuminance0_target_input_show(struct device
*dev
,
930 struct device_attribute
*attr
,
933 struct tsl2772_chip
*chip
= iio_priv(dev_to_iio_dev(dev
));
935 return snprintf(buf
, PAGE_SIZE
, "%d\n", chip
->settings
.als_cal_target
);
938 static ssize_t
in_illuminance0_target_input_store(struct device
*dev
,
939 struct device_attribute
*attr
,
940 const char *buf
, size_t len
)
942 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
943 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
947 if (kstrtou16(buf
, 0, &value
))
950 chip
->settings
.als_cal_target
= value
;
951 ret
= tsl2772_invoke_change(indio_dev
);
958 static ssize_t
in_illuminance0_calibrate_store(struct device
*dev
,
959 struct device_attribute
*attr
,
960 const char *buf
, size_t len
)
962 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
966 if (kstrtobool(buf
, &value
) || !value
)
969 ret
= tsl2772_als_calibrate(indio_dev
);
973 ret
= tsl2772_invoke_change(indio_dev
);
980 static ssize_t
in_illuminance0_lux_table_show(struct device
*dev
,
981 struct device_attribute
*attr
,
984 struct tsl2772_chip
*chip
= iio_priv(dev_to_iio_dev(dev
));
988 while (i
< TSL2772_MAX_LUX_TABLE_SIZE
) {
989 offset
+= snprintf(buf
+ offset
, PAGE_SIZE
, "%u,%u,",
990 chip
->tsl2772_device_lux
[i
].ch0
,
991 chip
->tsl2772_device_lux
[i
].ch1
);
992 if (chip
->tsl2772_device_lux
[i
].ch0
== 0) {
994 * We just printed the first "0" entry.
995 * Now get rid of the extra "," and break.
1003 offset
+= snprintf(buf
+ offset
, PAGE_SIZE
, "\n");
1007 static ssize_t
in_illuminance0_lux_table_store(struct device
*dev
,
1008 struct device_attribute
*attr
,
1009 const char *buf
, size_t len
)
1011 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
1012 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
1013 int value
[ARRAY_SIZE(chip
->tsl2772_device_lux
) * 2 + 1];
1016 get_options(buf
, ARRAY_SIZE(value
), value
);
1019 * We now have an array of ints starting at value[1], and
1020 * enumerated by value[0].
1021 * We expect each group of two ints to be one table entry,
1022 * and the last table entry is all 0.
1025 if ((n
% 2) || n
< 4 ||
1026 n
> ((ARRAY_SIZE(chip
->tsl2772_device_lux
) - 1) * 2))
1029 if ((value
[(n
- 1)] | value
[n
]) != 0)
1032 if (chip
->tsl2772_chip_status
== TSL2772_CHIP_WORKING
) {
1033 ret
= tsl2772_chip_off(indio_dev
);
1038 /* Zero out the table */
1039 memset(chip
->tsl2772_device_lux
, 0, sizeof(chip
->tsl2772_device_lux
));
1040 memcpy(chip
->tsl2772_device_lux
, &value
[1], (value
[0] * 4));
1042 ret
= tsl2772_invoke_change(indio_dev
);
1049 static ssize_t
in_proximity0_calibrate_store(struct device
*dev
,
1050 struct device_attribute
*attr
,
1051 const char *buf
, size_t len
)
1053 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
1057 if (kstrtobool(buf
, &value
) || !value
)
1060 ret
= tsl2772_prox_cal(indio_dev
);
1064 ret
= tsl2772_invoke_change(indio_dev
);
1071 static int tsl2772_read_interrupt_config(struct iio_dev
*indio_dev
,
1072 const struct iio_chan_spec
*chan
,
1073 enum iio_event_type type
,
1074 enum iio_event_direction dir
)
1076 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
1078 if (chan
->type
== IIO_INTENSITY
)
1079 return chip
->settings
.als_interrupt_en
;
1081 return chip
->settings
.prox_interrupt_en
;
1084 static int tsl2772_write_interrupt_config(struct iio_dev
*indio_dev
,
1085 const struct iio_chan_spec
*chan
,
1086 enum iio_event_type type
,
1087 enum iio_event_direction dir
,
1090 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
1092 if (chan
->type
== IIO_INTENSITY
)
1093 chip
->settings
.als_interrupt_en
= val
? true : false;
1095 chip
->settings
.prox_interrupt_en
= val
? true : false;
1097 return tsl2772_invoke_change(indio_dev
);
1100 static int tsl2772_write_event_value(struct iio_dev
*indio_dev
,
1101 const struct iio_chan_spec
*chan
,
1102 enum iio_event_type type
,
1103 enum iio_event_direction dir
,
1104 enum iio_event_info info
,
1107 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
1108 int ret
= -EINVAL
, count
, persistence
;
1112 case IIO_EV_INFO_VALUE
:
1113 if (chan
->type
== IIO_INTENSITY
) {
1115 case IIO_EV_DIR_RISING
:
1116 chip
->settings
.als_thresh_high
= val
;
1119 case IIO_EV_DIR_FALLING
:
1120 chip
->settings
.als_thresh_low
= val
;
1128 case IIO_EV_DIR_RISING
:
1129 chip
->settings
.prox_thres_high
= val
;
1132 case IIO_EV_DIR_FALLING
:
1133 chip
->settings
.prox_thres_low
= val
;
1141 case IIO_EV_INFO_PERIOD
:
1142 if (chan
->type
== IIO_INTENSITY
)
1143 time
= chip
->settings
.als_time
;
1145 time
= chip
->settings
.prox_time
;
1148 persistence
= ((val
* 1000000) + val2
) /
1149 (count
* tsl2772_int_time_avail
[chip
->id
][3]);
1151 if (chan
->type
== IIO_INTENSITY
) {
1152 /* ALS filter values are 1, 2, 3, 5, 10, 15, ..., 60 */
1153 if (persistence
> 3)
1154 persistence
= (persistence
/ 5) + 3;
1156 chip
->settings
.als_persistence
= persistence
;
1158 chip
->settings
.prox_persistence
= persistence
;
1170 return tsl2772_invoke_change(indio_dev
);
1173 static int tsl2772_read_event_value(struct iio_dev
*indio_dev
,
1174 const struct iio_chan_spec
*chan
,
1175 enum iio_event_type type
,
1176 enum iio_event_direction dir
,
1177 enum iio_event_info info
,
1178 int *val
, int *val2
)
1180 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
1181 int filter_delay
, persistence
;
1185 case IIO_EV_INFO_VALUE
:
1186 if (chan
->type
== IIO_INTENSITY
) {
1188 case IIO_EV_DIR_RISING
:
1189 *val
= chip
->settings
.als_thresh_high
;
1191 case IIO_EV_DIR_FALLING
:
1192 *val
= chip
->settings
.als_thresh_low
;
1199 case IIO_EV_DIR_RISING
:
1200 *val
= chip
->settings
.prox_thres_high
;
1202 case IIO_EV_DIR_FALLING
:
1203 *val
= chip
->settings
.prox_thres_low
;
1210 case IIO_EV_INFO_PERIOD
:
1211 if (chan
->type
== IIO_INTENSITY
) {
1212 time
= chip
->settings
.als_time
;
1213 persistence
= chip
->settings
.als_persistence
;
1215 /* ALS filter values are 1, 2, 3, 5, 10, 15, ..., 60 */
1216 if (persistence
> 3)
1217 persistence
= (persistence
- 3) * 5;
1219 time
= chip
->settings
.prox_time
;
1220 persistence
= chip
->settings
.prox_persistence
;
1223 filter_delay
= persistence
* (256 - time
) *
1224 tsl2772_int_time_avail
[chip
->id
][3];
1226 *val
= filter_delay
/ 1000000;
1227 *val2
= filter_delay
% 1000000;
1228 return IIO_VAL_INT_PLUS_MICRO
;
1234 static int tsl2772_read_raw(struct iio_dev
*indio_dev
,
1235 struct iio_chan_spec
const *chan
,
1240 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
1243 case IIO_CHAN_INFO_PROCESSED
:
1244 switch (chan
->type
) {
1246 tsl2772_get_lux(indio_dev
);
1247 *val
= chip
->als_cur_info
.lux
;
1252 case IIO_CHAN_INFO_RAW
:
1253 switch (chan
->type
) {
1255 tsl2772_get_lux(indio_dev
);
1256 if (chan
->channel
== 0)
1257 *val
= chip
->als_cur_info
.als_ch0
;
1259 *val
= chip
->als_cur_info
.als_ch1
;
1262 tsl2772_get_prox(indio_dev
);
1263 *val
= chip
->prox_data
;
1269 case IIO_CHAN_INFO_CALIBSCALE
:
1270 if (chan
->type
== IIO_LIGHT
)
1271 *val
= tsl2772_als_gain
[chip
->settings
.als_gain
];
1273 *val
= tsl2772_prox_gain
[chip
->settings
.prox_gain
];
1275 case IIO_CHAN_INFO_CALIBBIAS
:
1276 *val
= chip
->settings
.als_gain_trim
;
1278 case IIO_CHAN_INFO_INT_TIME
:
1280 *val2
= (256 - chip
->settings
.als_time
) *
1281 tsl2772_int_time_avail
[chip
->id
][3];
1282 return IIO_VAL_INT_PLUS_MICRO
;
1288 static int tsl2772_write_raw(struct iio_dev
*indio_dev
,
1289 struct iio_chan_spec
const *chan
,
1294 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
1297 case IIO_CHAN_INFO_CALIBSCALE
:
1298 if (chan
->type
== IIO_INTENSITY
) {
1301 chip
->settings
.als_gain
= 0;
1304 chip
->settings
.als_gain
= 1;
1307 chip
->settings
.als_gain
= 2;
1310 chip
->settings
.als_gain
= 3;
1318 chip
->settings
.prox_gain
= 0;
1321 chip
->settings
.prox_gain
= 1;
1324 chip
->settings
.prox_gain
= 2;
1327 chip
->settings
.prox_gain
= 3;
1334 case IIO_CHAN_INFO_CALIBBIAS
:
1335 if (val
< TSL2772_ALS_GAIN_TRIM_MIN
||
1336 val
> TSL2772_ALS_GAIN_TRIM_MAX
)
1339 chip
->settings
.als_gain_trim
= val
;
1341 case IIO_CHAN_INFO_INT_TIME
:
1342 if (val
!= 0 || val2
< tsl2772_int_time_avail
[chip
->id
][1] ||
1343 val2
> tsl2772_int_time_avail
[chip
->id
][5])
1346 chip
->settings
.als_time
= 256 -
1347 (val2
/ tsl2772_int_time_avail
[chip
->id
][3]);
1353 return tsl2772_invoke_change(indio_dev
);
1356 static DEVICE_ATTR_RW(in_illuminance0_target_input
);
1358 static DEVICE_ATTR_WO(in_illuminance0_calibrate
);
1360 static DEVICE_ATTR_WO(in_proximity0_calibrate
);
1362 static DEVICE_ATTR_RW(in_illuminance0_lux_table
);
1364 /* Use the default register values to identify the Taos device */
1365 static int tsl2772_device_id_verif(int id
, int target
)
1371 return (id
& 0xf0) == TRITON_ID
;
1374 return (id
& 0xf0) == HALIBUT_ID
;
1381 return (id
& 0xf0) == SWORDFISH_ID
;
1387 static irqreturn_t
tsl2772_event_handler(int irq
, void *private)
1389 struct iio_dev
*indio_dev
= private;
1390 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
1391 s64 timestamp
= iio_get_time_ns(indio_dev
);
1394 ret
= tsl2772_read_status(chip
);
1398 /* What type of interrupt do we need to process */
1399 if (ret
& TSL2772_STA_PRX_INTR
) {
1400 iio_push_event(indio_dev
,
1401 IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY
,
1408 if (ret
& TSL2772_STA_ALS_INTR
) {
1409 iio_push_event(indio_dev
,
1410 IIO_UNMOD_EVENT_CODE(IIO_LIGHT
,
1417 ret
= i2c_smbus_write_byte(chip
->client
,
1418 TSL2772_CMD_REG
| TSL2772_CMD_SPL_FN
|
1419 TSL2772_CMD_PROXALS_INT_CLR
);
1421 dev_err(&chip
->client
->dev
,
1422 "%s: failed to clear interrupt status: %d\n",
1428 static struct attribute
*tsl2772_ALS_device_attrs
[] = {
1429 &dev_attr_in_illuminance0_target_input
.attr
,
1430 &dev_attr_in_illuminance0_calibrate
.attr
,
1431 &dev_attr_in_illuminance0_lux_table
.attr
,
1435 static struct attribute
*tsl2772_PRX_device_attrs
[] = {
1436 &dev_attr_in_proximity0_calibrate
.attr
,
1440 static struct attribute
*tsl2772_ALSPRX_device_attrs
[] = {
1441 &dev_attr_in_illuminance0_target_input
.attr
,
1442 &dev_attr_in_illuminance0_calibrate
.attr
,
1443 &dev_attr_in_illuminance0_lux_table
.attr
,
1447 static struct attribute
*tsl2772_PRX2_device_attrs
[] = {
1448 &dev_attr_in_proximity0_calibrate
.attr
,
1452 static struct attribute
*tsl2772_ALSPRX2_device_attrs
[] = {
1453 &dev_attr_in_illuminance0_target_input
.attr
,
1454 &dev_attr_in_illuminance0_calibrate
.attr
,
1455 &dev_attr_in_illuminance0_lux_table
.attr
,
1456 &dev_attr_in_proximity0_calibrate
.attr
,
1460 static const struct attribute_group tsl2772_device_attr_group_tbl
[] = {
1462 .attrs
= tsl2772_ALS_device_attrs
,
1465 .attrs
= tsl2772_PRX_device_attrs
,
1468 .attrs
= tsl2772_ALSPRX_device_attrs
,
1471 .attrs
= tsl2772_PRX2_device_attrs
,
1474 .attrs
= tsl2772_ALSPRX2_device_attrs
,
1478 #define TSL2772_DEVICE_INFO(type)[type] = \
1480 .attrs = &tsl2772_device_attr_group_tbl[type], \
1481 .read_raw = &tsl2772_read_raw, \
1482 .read_avail = &tsl2772_read_avail, \
1483 .write_raw = &tsl2772_write_raw, \
1484 .read_event_value = &tsl2772_read_event_value, \
1485 .write_event_value = &tsl2772_write_event_value, \
1486 .read_event_config = &tsl2772_read_interrupt_config, \
1487 .write_event_config = &tsl2772_write_interrupt_config, \
1490 static const struct iio_info tsl2772_device_info
[] = {
1491 TSL2772_DEVICE_INFO(ALS
),
1492 TSL2772_DEVICE_INFO(PRX
),
1493 TSL2772_DEVICE_INFO(ALSPRX
),
1494 TSL2772_DEVICE_INFO(PRX2
),
1495 TSL2772_DEVICE_INFO(ALSPRX2
),
1498 static const struct iio_event_spec tsl2772_events
[] = {
1500 .type
= IIO_EV_TYPE_THRESH
,
1501 .dir
= IIO_EV_DIR_RISING
,
1502 .mask_separate
= BIT(IIO_EV_INFO_VALUE
),
1504 .type
= IIO_EV_TYPE_THRESH
,
1505 .dir
= IIO_EV_DIR_FALLING
,
1506 .mask_separate
= BIT(IIO_EV_INFO_VALUE
),
1508 .type
= IIO_EV_TYPE_THRESH
,
1509 .dir
= IIO_EV_DIR_EITHER
,
1510 .mask_separate
= BIT(IIO_EV_INFO_PERIOD
) |
1511 BIT(IIO_EV_INFO_ENABLE
),
1515 static const struct tsl2772_chip_info tsl2772_chip_info_tbl
[] = {
1517 .channel_with_events
= {
1522 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
1524 .type
= IIO_INTENSITY
,
1527 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1528 BIT(IIO_CHAN_INFO_INT_TIME
) |
1529 BIT(IIO_CHAN_INFO_CALIBSCALE
) |
1530 BIT(IIO_CHAN_INFO_CALIBBIAS
),
1531 .info_mask_separate_available
=
1532 BIT(IIO_CHAN_INFO_INT_TIME
) |
1533 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1534 .event_spec
= tsl2772_events
,
1535 .num_event_specs
= ARRAY_SIZE(tsl2772_events
),
1537 .type
= IIO_INTENSITY
,
1542 .channel_without_events
= {
1547 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
1549 .type
= IIO_INTENSITY
,
1552 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1553 BIT(IIO_CHAN_INFO_INT_TIME
) |
1554 BIT(IIO_CHAN_INFO_CALIBSCALE
) |
1555 BIT(IIO_CHAN_INFO_CALIBBIAS
),
1556 .info_mask_separate_available
=
1557 BIT(IIO_CHAN_INFO_INT_TIME
) |
1558 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1560 .type
= IIO_INTENSITY
,
1565 .chan_table_elements
= 3,
1566 .info
= &tsl2772_device_info
[ALS
],
1569 .channel_with_events
= {
1571 .type
= IIO_PROXIMITY
,
1574 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
1575 .event_spec
= tsl2772_events
,
1576 .num_event_specs
= ARRAY_SIZE(tsl2772_events
),
1579 .channel_without_events
= {
1581 .type
= IIO_PROXIMITY
,
1584 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
1587 .chan_table_elements
= 1,
1588 .info
= &tsl2772_device_info
[PRX
],
1591 .channel_with_events
= {
1596 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
1598 .type
= IIO_INTENSITY
,
1601 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1602 BIT(IIO_CHAN_INFO_INT_TIME
) |
1603 BIT(IIO_CHAN_INFO_CALIBSCALE
) |
1604 BIT(IIO_CHAN_INFO_CALIBBIAS
),
1605 .info_mask_separate_available
=
1606 BIT(IIO_CHAN_INFO_INT_TIME
) |
1607 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1608 .event_spec
= tsl2772_events
,
1609 .num_event_specs
= ARRAY_SIZE(tsl2772_events
),
1611 .type
= IIO_INTENSITY
,
1614 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
1616 .type
= IIO_PROXIMITY
,
1619 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
1620 .event_spec
= tsl2772_events
,
1621 .num_event_specs
= ARRAY_SIZE(tsl2772_events
),
1624 .channel_without_events
= {
1629 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
1631 .type
= IIO_INTENSITY
,
1634 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1635 BIT(IIO_CHAN_INFO_INT_TIME
) |
1636 BIT(IIO_CHAN_INFO_CALIBSCALE
) |
1637 BIT(IIO_CHAN_INFO_CALIBBIAS
),
1638 .info_mask_separate_available
=
1639 BIT(IIO_CHAN_INFO_INT_TIME
) |
1640 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1642 .type
= IIO_INTENSITY
,
1645 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
1647 .type
= IIO_PROXIMITY
,
1650 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
1653 .chan_table_elements
= 4,
1654 .info
= &tsl2772_device_info
[ALSPRX
],
1657 .channel_with_events
= {
1659 .type
= IIO_PROXIMITY
,
1662 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1663 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1664 .info_mask_separate_available
=
1665 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1666 .event_spec
= tsl2772_events
,
1667 .num_event_specs
= ARRAY_SIZE(tsl2772_events
),
1670 .channel_without_events
= {
1672 .type
= IIO_PROXIMITY
,
1675 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1676 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1677 .info_mask_separate_available
=
1678 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1681 .chan_table_elements
= 1,
1682 .info
= &tsl2772_device_info
[PRX2
],
1685 .channel_with_events
= {
1690 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
1692 .type
= IIO_INTENSITY
,
1695 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1696 BIT(IIO_CHAN_INFO_INT_TIME
) |
1697 BIT(IIO_CHAN_INFO_CALIBSCALE
) |
1698 BIT(IIO_CHAN_INFO_CALIBBIAS
),
1699 .info_mask_separate_available
=
1700 BIT(IIO_CHAN_INFO_INT_TIME
) |
1701 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1702 .event_spec
= tsl2772_events
,
1703 .num_event_specs
= ARRAY_SIZE(tsl2772_events
),
1705 .type
= IIO_INTENSITY
,
1708 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
1710 .type
= IIO_PROXIMITY
,
1713 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1714 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1715 .info_mask_separate_available
=
1716 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1717 .event_spec
= tsl2772_events
,
1718 .num_event_specs
= ARRAY_SIZE(tsl2772_events
),
1721 .channel_without_events
= {
1726 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
1728 .type
= IIO_INTENSITY
,
1731 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1732 BIT(IIO_CHAN_INFO_INT_TIME
) |
1733 BIT(IIO_CHAN_INFO_CALIBSCALE
) |
1734 BIT(IIO_CHAN_INFO_CALIBBIAS
),
1735 .info_mask_separate_available
=
1736 BIT(IIO_CHAN_INFO_INT_TIME
) |
1737 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1739 .type
= IIO_INTENSITY
,
1742 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
1744 .type
= IIO_PROXIMITY
,
1747 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1748 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1749 .info_mask_separate_available
=
1750 BIT(IIO_CHAN_INFO_CALIBSCALE
),
1753 .chan_table_elements
= 4,
1754 .info
= &tsl2772_device_info
[ALSPRX2
],
1758 static int tsl2772_probe(struct i2c_client
*clientp
,
1759 const struct i2c_device_id
*id
)
1761 struct iio_dev
*indio_dev
;
1762 struct tsl2772_chip
*chip
;
1765 indio_dev
= devm_iio_device_alloc(&clientp
->dev
, sizeof(*chip
));
1769 chip
= iio_priv(indio_dev
);
1770 chip
->client
= clientp
;
1771 i2c_set_clientdata(clientp
, indio_dev
);
1773 chip
->supplies
[TSL2772_SUPPLY_VDD
].supply
= "vdd";
1774 chip
->supplies
[TSL2772_SUPPLY_VDDIO
].supply
= "vddio";
1776 ret
= devm_regulator_bulk_get(&clientp
->dev
,
1777 ARRAY_SIZE(chip
->supplies
),
1780 if (ret
!= -EPROBE_DEFER
)
1781 dev_err(&clientp
->dev
,
1782 "Failed to get regulators: %d\n",
1788 ret
= regulator_bulk_enable(ARRAY_SIZE(chip
->supplies
), chip
->supplies
);
1790 dev_err(&clientp
->dev
, "Failed to enable regulators: %d\n",
1795 ret
= devm_add_action_or_reset(&clientp
->dev
,
1796 tsl2772_disable_regulators_action
,
1799 dev_err(&clientp
->dev
, "Failed to setup regulator cleanup action %d\n",
1804 usleep_range(TSL2772_BOOT_MIN_SLEEP_TIME
, TSL2772_BOOT_MAX_SLEEP_TIME
);
1806 ret
= i2c_smbus_read_byte_data(chip
->client
,
1807 TSL2772_CMD_REG
| TSL2772_CHIPID
);
1811 if (tsl2772_device_id_verif(ret
, id
->driver_data
) <= 0) {
1812 dev_info(&chip
->client
->dev
,
1813 "%s: i2c device found does not match expected id\n",
1818 ret
= i2c_smbus_write_byte(clientp
, TSL2772_CMD_REG
| TSL2772_CNTRL
);
1820 dev_err(&clientp
->dev
,
1821 "%s: Failed to write to CMD register: %d\n",
1826 mutex_init(&chip
->als_mutex
);
1827 mutex_init(&chip
->prox_mutex
);
1829 chip
->tsl2772_chip_status
= TSL2772_CHIP_UNKNOWN
;
1830 chip
->pdata
= dev_get_platdata(&clientp
->dev
);
1831 chip
->id
= id
->driver_data
;
1833 &tsl2772_chip_info_tbl
[device_channel_config
[id
->driver_data
]];
1835 indio_dev
->info
= chip
->chip_info
->info
;
1836 indio_dev
->dev
.parent
= &clientp
->dev
;
1837 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1838 indio_dev
->name
= chip
->client
->name
;
1839 indio_dev
->num_channels
= chip
->chip_info
->chan_table_elements
;
1842 indio_dev
->channels
= chip
->chip_info
->channel_with_events
;
1844 ret
= devm_request_threaded_irq(&clientp
->dev
, clientp
->irq
,
1846 &tsl2772_event_handler
,
1847 IRQF_TRIGGER_FALLING
|
1852 dev_err(&clientp
->dev
,
1853 "%s: irq request failed\n", __func__
);
1857 indio_dev
->channels
= chip
->chip_info
->channel_without_events
;
1860 tsl2772_defaults(chip
);
1861 ret
= tsl2772_chip_on(indio_dev
);
1865 ret
= devm_add_action_or_reset(&clientp
->dev
,
1866 tsl2772_chip_off_action
,
1871 return devm_iio_device_register(&clientp
->dev
, indio_dev
);
1874 static int tsl2772_suspend(struct device
*dev
)
1876 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1877 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
1880 ret
= tsl2772_chip_off(indio_dev
);
1881 regulator_bulk_disable(ARRAY_SIZE(chip
->supplies
), chip
->supplies
);
1886 static int tsl2772_resume(struct device
*dev
)
1888 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1889 struct tsl2772_chip
*chip
= iio_priv(indio_dev
);
1892 ret
= regulator_bulk_enable(ARRAY_SIZE(chip
->supplies
), chip
->supplies
);
1896 usleep_range(TSL2772_BOOT_MIN_SLEEP_TIME
, TSL2772_BOOT_MAX_SLEEP_TIME
);
1898 return tsl2772_chip_on(indio_dev
);
1901 static const struct i2c_device_id tsl2772_idtable
[] = {
1902 { "tsl2571", tsl2571
},
1903 { "tsl2671", tsl2671
},
1904 { "tmd2671", tmd2671
},
1905 { "tsl2771", tsl2771
},
1906 { "tmd2771", tmd2771
},
1907 { "tsl2572", tsl2572
},
1908 { "tsl2672", tsl2672
},
1909 { "tmd2672", tmd2672
},
1910 { "tsl2772", tsl2772
},
1911 { "tmd2772", tmd2772
},
1912 { "apds9930", apds9930
},
1916 MODULE_DEVICE_TABLE(i2c
, tsl2772_idtable
);
1918 static const struct of_device_id tsl2772_of_match
[] = {
1919 { .compatible
= "amstaos,tsl2571" },
1920 { .compatible
= "amstaos,tsl2671" },
1921 { .compatible
= "amstaos,tmd2671" },
1922 { .compatible
= "amstaos,tsl2771" },
1923 { .compatible
= "amstaos,tmd2771" },
1924 { .compatible
= "amstaos,tsl2572" },
1925 { .compatible
= "amstaos,tsl2672" },
1926 { .compatible
= "amstaos,tmd2672" },
1927 { .compatible
= "amstaos,tsl2772" },
1928 { .compatible
= "amstaos,tmd2772" },
1929 { .compatible
= "avago,apds9930" },
1932 MODULE_DEVICE_TABLE(of
, tsl2772_of_match
);
1934 static const struct dev_pm_ops tsl2772_pm_ops
= {
1935 .suspend
= tsl2772_suspend
,
1936 .resume
= tsl2772_resume
,
1939 static struct i2c_driver tsl2772_driver
= {
1942 .of_match_table
= tsl2772_of_match
,
1943 .pm
= &tsl2772_pm_ops
,
1945 .id_table
= tsl2772_idtable
,
1946 .probe
= tsl2772_probe
,
1949 module_i2c_driver(tsl2772_driver
);
1951 MODULE_AUTHOR("J. August Brenner <Jon.Brenner@ams.com>");
1952 MODULE_AUTHOR("Brian Masney <masneyb@onstation.org>");
1953 MODULE_DESCRIPTION("TAOS tsl2772 ambient and proximity light sensor driver");
1954 MODULE_LICENSE("GPL");