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",
609 chip->settings.prox_diode = prox_diode_mask;
614 static void tsl2772_parse_dt(struct tsl2772_chip *chip)
616 tsl2772_read_prox_led_current(chip);
617 tsl2772_read_prox_diodes(chip);
621 * tsl2772_defaults() - Populates the device nominal operating parameters
622 * with those provided by a 'platform' data struct or
623 * with prefined defaults.
625 * @chip: pointer to device structure.
627 static void tsl2772_defaults(struct tsl2772_chip *chip)
629 /* If Operational settings defined elsewhere.. */
630 if (chip->pdata && chip->pdata->platform_default_settings)
631 memcpy(&chip->settings, chip->pdata->platform_default_settings,
632 sizeof(tsl2772_default_settings));
634 memcpy(&chip->settings, &tsl2772_default_settings,
635 sizeof(tsl2772_default_settings));
637 /* Load up the proper lux table. */
638 if (chip->pdata && chip->pdata->platform_lux_table[0].ch0 != 0)
639 memcpy(chip->tsl2772_device_lux,
640 chip->pdata->platform_lux_table,
641 sizeof(chip->pdata->platform_lux_table));
643 memcpy(chip->tsl2772_device_lux,
644 tsl2772_default_lux_table_group[chip->id],
645 TSL2772_DEFAULT_TABLE_BYTES);
647 tsl2772_parse_dt(chip);
651 * tsl2772_als_calibrate() - Obtain single reading and calculate
654 * @indio_dev: pointer to IIO device
656 static int tsl2772_als_calibrate(struct iio_dev *indio_dev)
658 struct tsl2772_chip *chip = iio_priv(indio_dev);
661 ret = i2c_smbus_read_byte_data(chip->client,
662 TSL2772_CMD_REG | TSL2772_CNTRL);
664 dev_err(&chip->client->dev,
665 "%s: failed to read from the CNTRL register\n",
670 if ((ret & (TSL2772_CNTL_ADC_ENBL | TSL2772_CNTL_PWR_ON))
671 != (TSL2772_CNTL_ADC_ENBL | TSL2772_CNTL_PWR_ON)) {
672 dev_err(&chip->client->dev,
673 "%s: Device is not powered on and/or ADC is not enabled\n",
676 } else if ((ret & TSL2772_STA_ADC_VALID) != TSL2772_STA_ADC_VALID) {
677 dev_err(&chip->client->dev,
678 "%s: The two ADC channels have not completed an integration cycle\n",
683 lux_val = tsl2772_get_lux(indio_dev);
685 dev_err(&chip->client->dev,
686 "%s: failed to get lux\n", __func__);
692 ret = (chip->settings.als_cal_target * chip->settings.als_gain_trim) /
694 if (ret < TSL2772_ALS_GAIN_TRIM_MIN || ret > TSL2772_ALS_GAIN_TRIM_MAX)
697 chip->settings.als_gain_trim = ret;
702 static void tsl2772_disable_regulators_action(void *_data)
704 struct tsl2772_chip *chip = _data;
706 regulator_bulk_disable(ARRAY_SIZE(chip->supplies), chip->supplies);
709 static int tsl2772_chip_on(struct iio_dev *indio_dev)
711 struct tsl2772_chip *chip = iio_priv(indio_dev);
712 int ret, i, als_count, als_time_us;
713 u8 *dev_reg, reg_val;
715 /* Non calculated parameters */
716 chip->tsl2772_config[TSL2772_ALS_TIME] = chip->settings.als_time;
717 chip->tsl2772_config[TSL2772_PRX_TIME] = chip->settings.prox_time;
718 chip->tsl2772_config[TSL2772_WAIT_TIME] = chip->settings.wait_time;
719 chip->tsl2772_config[TSL2772_ALS_PRX_CONFIG] =
720 chip->settings.als_prox_config;
722 chip->tsl2772_config[TSL2772_ALS_MINTHRESHLO] =
723 (chip->settings.als_thresh_low) & 0xFF;
724 chip->tsl2772_config[TSL2772_ALS_MINTHRESHHI] =
725 (chip->settings.als_thresh_low >> 8) & 0xFF;
726 chip->tsl2772_config[TSL2772_ALS_MAXTHRESHLO] =
727 (chip->settings.als_thresh_high) & 0xFF;
728 chip->tsl2772_config[TSL2772_ALS_MAXTHRESHHI] =
729 (chip->settings.als_thresh_high >> 8) & 0xFF;
730 chip->tsl2772_config[TSL2772_PERSISTENCE] =
731 (chip->settings.prox_persistence & 0xFF) << 4 |
732 (chip->settings.als_persistence & 0xFF);
734 chip->tsl2772_config[TSL2772_PRX_COUNT] =
735 chip->settings.prox_pulse_count;
736 chip->tsl2772_config[TSL2772_PRX_MINTHRESHLO] =
737 (chip->settings.prox_thres_low) & 0xFF;
738 chip->tsl2772_config[TSL2772_PRX_MINTHRESHHI] =
739 (chip->settings.prox_thres_low >> 8) & 0xFF;
740 chip->tsl2772_config[TSL2772_PRX_MAXTHRESHLO] =
741 (chip->settings.prox_thres_high) & 0xFF;
742 chip->tsl2772_config[TSL2772_PRX_MAXTHRESHHI] =
743 (chip->settings.prox_thres_high >> 8) & 0xFF;
745 /* and make sure we're not already on */
746 if (chip->tsl2772_chip_status == TSL2772_CHIP_WORKING) {
747 /* if forcing a register update - turn off, then on */
748 dev_info(&chip->client->dev, "device is already enabled\n");
752 /* Set the gain based on tsl2772_settings struct */
753 chip->tsl2772_config[TSL2772_GAIN] =
754 (chip->settings.als_gain & 0xFF) |
755 ((chip->settings.prox_gain & 0xFF) << 2) |
756 (chip->settings.prox_diode << 4) |
757 (chip->settings.prox_power << 6);
759 /* set chip time scaling and saturation */
760 als_count = 256 - chip->settings.als_time;
761 als_time_us = als_count * tsl2772_int_time_avail[chip->id][3];
762 chip->als_saturation = als_count * 768; /* 75% of full scale */
763 chip->als_gain_time_scale = als_time_us *
764 tsl2772_als_gain[chip->settings.als_gain];
767 * TSL2772 Specific power-on / adc enable sequence
768 * Power on the device 1st.
770 ret = tsl2772_write_control_reg(chip, TSL2772_CNTL_PWR_ON);
775 * Use the following shadow copy for our delay before enabling ADC.
776 * Write all the registers.
778 for (i = 0, dev_reg = chip->tsl2772_config;
779 i < TSL2772_MAX_CONFIG_REG; i++) {
780 int reg = TSL2772_CMD_REG + i;
782 ret = i2c_smbus_write_byte_data(chip->client, reg,
785 dev_err(&chip->client->dev,
786 "%s: failed to write to register %x: %d\n",
792 /* Power-on settling time */
793 usleep_range(3000, 3500);
795 reg_val = TSL2772_CNTL_PWR_ON | TSL2772_CNTL_ADC_ENBL |
796 TSL2772_CNTL_PROX_DET_ENBL;
797 if (chip->settings.als_interrupt_en)
798 reg_val |= TSL2772_CNTL_ALS_INT_ENBL;
799 if (chip->settings.prox_interrupt_en)
800 reg_val |= TSL2772_CNTL_PROX_INT_ENBL;
802 ret = tsl2772_write_control_reg(chip, reg_val);
806 ret = i2c_smbus_write_byte(chip->client,
807 TSL2772_CMD_REG | TSL2772_CMD_SPL_FN |
808 TSL2772_CMD_PROXALS_INT_CLR);
810 dev_err(&chip->client->dev,
811 "%s: failed to clear interrupt status: %d\n",
816 chip->tsl2772_chip_status = TSL2772_CHIP_WORKING;
821 static int tsl2772_chip_off(struct iio_dev *indio_dev)
823 struct tsl2772_chip *chip = iio_priv(indio_dev);
825 /* turn device off */
826 chip->tsl2772_chip_status = TSL2772_CHIP_SUSPENDED;
827 return tsl2772_write_control_reg(chip, 0x00);
830 static void tsl2772_chip_off_action(void *data)
832 struct iio_dev *indio_dev = data;
834 tsl2772_chip_off(indio_dev);
838 * tsl2772_invoke_change - power cycle the device to implement the user
840 * @indio_dev: pointer to IIO device
842 * Obtain and lock both ALS and PROX resources, determine and save device state
843 * (On/Off), cycle device to implement updated parameter, put device back into
844 * proper state, and unlock resource.
846 static int tsl2772_invoke_change(struct iio_dev *indio_dev)
848 struct tsl2772_chip *chip = iio_priv(indio_dev);
849 int device_status = chip->tsl2772_chip_status;
852 mutex_lock(&chip->als_mutex);
853 mutex_lock(&chip->prox_mutex);
855 if (device_status == TSL2772_CHIP_WORKING) {
856 ret = tsl2772_chip_off(indio_dev);
861 ret = tsl2772_chip_on(indio_dev);
864 mutex_unlock(&chip->prox_mutex);
865 mutex_unlock(&chip->als_mutex);
870 static int tsl2772_prox_cal(struct iio_dev *indio_dev)
872 struct tsl2772_chip *chip = iio_priv(indio_dev);
873 int prox_history[MAX_SAMPLES_CAL + 1];
874 int i, ret, mean, max, sample_sum;
876 if (chip->settings.prox_max_samples_cal < 1 ||
877 chip->settings.prox_max_samples_cal > MAX_SAMPLES_CAL)
880 for (i = 0; i < chip->settings.prox_max_samples_cal; i++) {
881 usleep_range(15000, 17500);
882 ret = tsl2772_get_prox(indio_dev);
886 prox_history[i] = chip->prox_data;
891 for (i = 0; i < chip->settings.prox_max_samples_cal; i++) {
892 sample_sum += prox_history[i];
893 max = max(max, prox_history[i]);
895 mean = sample_sum / chip->settings.prox_max_samples_cal;
897 chip->settings.prox_thres_high = (max << 1) - mean;
899 return tsl2772_invoke_change(indio_dev);
902 static int tsl2772_read_avail(struct iio_dev *indio_dev,
903 struct iio_chan_spec const *chan,
904 const int **vals, int *type, int *length,
907 struct tsl2772_chip *chip = iio_priv(indio_dev);
910 case IIO_CHAN_INFO_CALIBSCALE:
911 if (chan->type == IIO_INTENSITY) {
912 *length = ARRAY_SIZE(tsl2772_int_calibscale_avail);
913 *vals = tsl2772_int_calibscale_avail;
915 *length = ARRAY_SIZE(tsl2772_prox_calibscale_avail);
916 *vals = tsl2772_prox_calibscale_avail;
919 return IIO_AVAIL_LIST;
920 case IIO_CHAN_INFO_INT_TIME:
921 *length = ARRAY_SIZE(tsl2772_int_time_avail[chip->id]);
922 *vals = tsl2772_int_time_avail[chip->id];
923 *type = IIO_VAL_INT_PLUS_MICRO;
924 return IIO_AVAIL_RANGE;
930 static ssize_t in_illuminance0_target_input_show(struct device *dev,
931 struct device_attribute *attr,
934 struct tsl2772_chip *chip = iio_priv(dev_to_iio_dev(dev));
936 return scnprintf(buf, PAGE_SIZE, "%d\n", chip->settings.als_cal_target);
939 static ssize_t in_illuminance0_target_input_store(struct device *dev,
940 struct device_attribute *attr,
941 const char *buf, size_t len)
943 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
944 struct tsl2772_chip *chip = iio_priv(indio_dev);
948 if (kstrtou16(buf, 0, &value))
951 chip->settings.als_cal_target = value;
952 ret = tsl2772_invoke_change(indio_dev);
959 static ssize_t in_illuminance0_calibrate_store(struct device *dev,
960 struct device_attribute *attr,
961 const char *buf, size_t len)
963 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
967 if (kstrtobool(buf, &value) || !value)
970 ret = tsl2772_als_calibrate(indio_dev);
974 ret = tsl2772_invoke_change(indio_dev);
981 static ssize_t in_illuminance0_lux_table_show(struct device *dev,
982 struct device_attribute *attr,
985 struct tsl2772_chip *chip = iio_priv(dev_to_iio_dev(dev));
989 while (i < TSL2772_MAX_LUX_TABLE_SIZE) {
990 offset += scnprintf(buf + offset, PAGE_SIZE - offset, "%u,%u,",
991 chip->tsl2772_device_lux[i].ch0,
992 chip->tsl2772_device_lux[i].ch1);
993 if (chip->tsl2772_device_lux[i].ch0 == 0) {
995 * We just printed the first "0" entry.
996 * Now get rid of the extra "," and break.
1004 offset += scnprintf(buf + offset, PAGE_SIZE - offset, "\n");
1008 static ssize_t in_illuminance0_lux_table_store(struct device *dev,
1009 struct device_attribute *attr,
1010 const char *buf, size_t len)
1012 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1013 struct tsl2772_chip *chip = iio_priv(indio_dev);
1014 int value[ARRAY_SIZE(chip->tsl2772_device_lux) * 2 + 1];
1017 get_options(buf, ARRAY_SIZE(value), value);
1020 * We now have an array of ints starting at value[1], and
1021 * enumerated by value[0].
1022 * We expect each group of two ints to be one table entry,
1023 * and the last table entry is all 0.
1026 if ((n % 2) || n < 4 ||
1027 n > ((ARRAY_SIZE(chip->tsl2772_device_lux) - 1) * 2))
1030 if ((value[(n - 1)] | value[n]) != 0)
1033 if (chip->tsl2772_chip_status == TSL2772_CHIP_WORKING) {
1034 ret = tsl2772_chip_off(indio_dev);
1039 /* Zero out the table */
1040 memset(chip->tsl2772_device_lux, 0, sizeof(chip->tsl2772_device_lux));
1041 memcpy(chip->tsl2772_device_lux, &value[1], (value[0] * 4));
1043 ret = tsl2772_invoke_change(indio_dev);
1050 static ssize_t in_proximity0_calibrate_store(struct device *dev,
1051 struct device_attribute *attr,
1052 const char *buf, size_t len)
1054 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1058 if (kstrtobool(buf, &value) || !value)
1061 ret = tsl2772_prox_cal(indio_dev);
1065 ret = tsl2772_invoke_change(indio_dev);
1072 static int tsl2772_read_interrupt_config(struct iio_dev *indio_dev,
1073 const struct iio_chan_spec *chan,
1074 enum iio_event_type type,
1075 enum iio_event_direction dir)
1077 struct tsl2772_chip *chip = iio_priv(indio_dev);
1079 if (chan->type == IIO_INTENSITY)
1080 return chip->settings.als_interrupt_en;
1082 return chip->settings.prox_interrupt_en;
1085 static int tsl2772_write_interrupt_config(struct iio_dev *indio_dev,
1086 const struct iio_chan_spec *chan,
1087 enum iio_event_type type,
1088 enum iio_event_direction dir,
1091 struct tsl2772_chip *chip = iio_priv(indio_dev);
1093 if (chan->type == IIO_INTENSITY)
1094 chip->settings.als_interrupt_en = val ? true : false;
1096 chip->settings.prox_interrupt_en = val ? true : false;
1098 return tsl2772_invoke_change(indio_dev);
1101 static int tsl2772_write_event_value(struct iio_dev *indio_dev,
1102 const struct iio_chan_spec *chan,
1103 enum iio_event_type type,
1104 enum iio_event_direction dir,
1105 enum iio_event_info info,
1108 struct tsl2772_chip *chip = iio_priv(indio_dev);
1109 int ret = -EINVAL, count, persistence;
1113 case IIO_EV_INFO_VALUE:
1114 if (chan->type == IIO_INTENSITY) {
1116 case IIO_EV_DIR_RISING:
1117 chip->settings.als_thresh_high = val;
1120 case IIO_EV_DIR_FALLING:
1121 chip->settings.als_thresh_low = val;
1129 case IIO_EV_DIR_RISING:
1130 chip->settings.prox_thres_high = val;
1133 case IIO_EV_DIR_FALLING:
1134 chip->settings.prox_thres_low = val;
1142 case IIO_EV_INFO_PERIOD:
1143 if (chan->type == IIO_INTENSITY)
1144 time = chip->settings.als_time;
1146 time = chip->settings.prox_time;
1149 persistence = ((val * 1000000) + val2) /
1150 (count * tsl2772_int_time_avail[chip->id][3]);
1152 if (chan->type == IIO_INTENSITY) {
1153 /* ALS filter values are 1, 2, 3, 5, 10, 15, ..., 60 */
1154 if (persistence > 3)
1155 persistence = (persistence / 5) + 3;
1157 chip->settings.als_persistence = persistence;
1159 chip->settings.prox_persistence = persistence;
1171 return tsl2772_invoke_change(indio_dev);
1174 static int tsl2772_read_event_value(struct iio_dev *indio_dev,
1175 const struct iio_chan_spec *chan,
1176 enum iio_event_type type,
1177 enum iio_event_direction dir,
1178 enum iio_event_info info,
1179 int *val, int *val2)
1181 struct tsl2772_chip *chip = iio_priv(indio_dev);
1182 int filter_delay, persistence;
1186 case IIO_EV_INFO_VALUE:
1187 if (chan->type == IIO_INTENSITY) {
1189 case IIO_EV_DIR_RISING:
1190 *val = chip->settings.als_thresh_high;
1192 case IIO_EV_DIR_FALLING:
1193 *val = chip->settings.als_thresh_low;
1200 case IIO_EV_DIR_RISING:
1201 *val = chip->settings.prox_thres_high;
1203 case IIO_EV_DIR_FALLING:
1204 *val = chip->settings.prox_thres_low;
1211 case IIO_EV_INFO_PERIOD:
1212 if (chan->type == IIO_INTENSITY) {
1213 time = chip->settings.als_time;
1214 persistence = chip->settings.als_persistence;
1216 /* ALS filter values are 1, 2, 3, 5, 10, 15, ..., 60 */
1217 if (persistence > 3)
1218 persistence = (persistence - 3) * 5;
1220 time = chip->settings.prox_time;
1221 persistence = chip->settings.prox_persistence;
1224 filter_delay = persistence * (256 - time) *
1225 tsl2772_int_time_avail[chip->id][3];
1227 *val = filter_delay / 1000000;
1228 *val2 = filter_delay % 1000000;
1229 return IIO_VAL_INT_PLUS_MICRO;
1235 static int tsl2772_read_raw(struct iio_dev *indio_dev,
1236 struct iio_chan_spec const *chan,
1241 struct tsl2772_chip *chip = iio_priv(indio_dev);
1244 case IIO_CHAN_INFO_PROCESSED:
1245 switch (chan->type) {
1247 tsl2772_get_lux(indio_dev);
1248 *val = chip->als_cur_info.lux;
1253 case IIO_CHAN_INFO_RAW:
1254 switch (chan->type) {
1256 tsl2772_get_lux(indio_dev);
1257 if (chan->channel == 0)
1258 *val = chip->als_cur_info.als_ch0;
1260 *val = chip->als_cur_info.als_ch1;
1263 tsl2772_get_prox(indio_dev);
1264 *val = chip->prox_data;
1270 case IIO_CHAN_INFO_CALIBSCALE:
1271 if (chan->type == IIO_LIGHT)
1272 *val = tsl2772_als_gain[chip->settings.als_gain];
1274 *val = tsl2772_prox_gain[chip->settings.prox_gain];
1276 case IIO_CHAN_INFO_CALIBBIAS:
1277 *val = chip->settings.als_gain_trim;
1279 case IIO_CHAN_INFO_INT_TIME:
1281 *val2 = (256 - chip->settings.als_time) *
1282 tsl2772_int_time_avail[chip->id][3];
1283 return IIO_VAL_INT_PLUS_MICRO;
1289 static int tsl2772_write_raw(struct iio_dev *indio_dev,
1290 struct iio_chan_spec const *chan,
1295 struct tsl2772_chip *chip = iio_priv(indio_dev);
1298 case IIO_CHAN_INFO_CALIBSCALE:
1299 if (chan->type == IIO_INTENSITY) {
1302 chip->settings.als_gain = 0;
1305 chip->settings.als_gain = 1;
1308 chip->settings.als_gain = 2;
1311 chip->settings.als_gain = 3;
1319 chip->settings.prox_gain = 0;
1322 chip->settings.prox_gain = 1;
1325 chip->settings.prox_gain = 2;
1328 chip->settings.prox_gain = 3;
1335 case IIO_CHAN_INFO_CALIBBIAS:
1336 if (val < TSL2772_ALS_GAIN_TRIM_MIN ||
1337 val > TSL2772_ALS_GAIN_TRIM_MAX)
1340 chip->settings.als_gain_trim = val;
1342 case IIO_CHAN_INFO_INT_TIME:
1343 if (val != 0 || val2 < tsl2772_int_time_avail[chip->id][1] ||
1344 val2 > tsl2772_int_time_avail[chip->id][5])
1347 chip->settings.als_time = 256 -
1348 (val2 / tsl2772_int_time_avail[chip->id][3]);
1354 return tsl2772_invoke_change(indio_dev);
1357 static DEVICE_ATTR_RW(in_illuminance0_target_input);
1359 static DEVICE_ATTR_WO(in_illuminance0_calibrate);
1361 static DEVICE_ATTR_WO(in_proximity0_calibrate);
1363 static DEVICE_ATTR_RW(in_illuminance0_lux_table);
1365 /* Use the default register values to identify the Taos device */
1366 static int tsl2772_device_id_verif(int id, int target)
1372 return (id & 0xf0) == TRITON_ID;
1375 return (id & 0xf0) == HALIBUT_ID;
1382 return (id & 0xf0) == SWORDFISH_ID;
1388 static irqreturn_t tsl2772_event_handler(int irq, void *private)
1390 struct iio_dev *indio_dev = private;
1391 struct tsl2772_chip *chip = iio_priv(indio_dev);
1392 s64 timestamp = iio_get_time_ns(indio_dev);
1395 ret = tsl2772_read_status(chip);
1399 /* What type of interrupt do we need to process */
1400 if (ret & TSL2772_STA_PRX_INTR) {
1401 iio_push_event(indio_dev,
1402 IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY,
1409 if (ret & TSL2772_STA_ALS_INTR) {
1410 iio_push_event(indio_dev,
1411 IIO_UNMOD_EVENT_CODE(IIO_LIGHT,
1418 ret = i2c_smbus_write_byte(chip->client,
1419 TSL2772_CMD_REG | TSL2772_CMD_SPL_FN |
1420 TSL2772_CMD_PROXALS_INT_CLR);
1422 dev_err(&chip->client->dev,
1423 "%s: failed to clear interrupt status: %d\n",
1429 static struct attribute *tsl2772_ALS_device_attrs[] = {
1430 &dev_attr_in_illuminance0_target_input.attr,
1431 &dev_attr_in_illuminance0_calibrate.attr,
1432 &dev_attr_in_illuminance0_lux_table.attr,
1436 static struct attribute *tsl2772_PRX_device_attrs[] = {
1437 &dev_attr_in_proximity0_calibrate.attr,
1441 static struct attribute *tsl2772_ALSPRX_device_attrs[] = {
1442 &dev_attr_in_illuminance0_target_input.attr,
1443 &dev_attr_in_illuminance0_calibrate.attr,
1444 &dev_attr_in_illuminance0_lux_table.attr,
1448 static struct attribute *tsl2772_PRX2_device_attrs[] = {
1449 &dev_attr_in_proximity0_calibrate.attr,
1453 static struct attribute *tsl2772_ALSPRX2_device_attrs[] = {
1454 &dev_attr_in_illuminance0_target_input.attr,
1455 &dev_attr_in_illuminance0_calibrate.attr,
1456 &dev_attr_in_illuminance0_lux_table.attr,
1457 &dev_attr_in_proximity0_calibrate.attr,
1461 static const struct attribute_group tsl2772_device_attr_group_tbl[] = {
1463 .attrs = tsl2772_ALS_device_attrs,
1466 .attrs = tsl2772_PRX_device_attrs,
1469 .attrs = tsl2772_ALSPRX_device_attrs,
1472 .attrs = tsl2772_PRX2_device_attrs,
1475 .attrs = tsl2772_ALSPRX2_device_attrs,
1479 #define TSL2772_DEVICE_INFO(type)[type] = \
1481 .attrs = &tsl2772_device_attr_group_tbl[type], \
1482 .read_raw = &tsl2772_read_raw, \
1483 .read_avail = &tsl2772_read_avail, \
1484 .write_raw = &tsl2772_write_raw, \
1485 .read_event_value = &tsl2772_read_event_value, \
1486 .write_event_value = &tsl2772_write_event_value, \
1487 .read_event_config = &tsl2772_read_interrupt_config, \
1488 .write_event_config = &tsl2772_write_interrupt_config, \
1491 static const struct iio_info tsl2772_device_info[] = {
1492 TSL2772_DEVICE_INFO(ALS),
1493 TSL2772_DEVICE_INFO(PRX),
1494 TSL2772_DEVICE_INFO(ALSPRX),
1495 TSL2772_DEVICE_INFO(PRX2),
1496 TSL2772_DEVICE_INFO(ALSPRX2),
1499 static const struct iio_event_spec tsl2772_events[] = {
1501 .type = IIO_EV_TYPE_THRESH,
1502 .dir = IIO_EV_DIR_RISING,
1503 .mask_separate = BIT(IIO_EV_INFO_VALUE),
1505 .type = IIO_EV_TYPE_THRESH,
1506 .dir = IIO_EV_DIR_FALLING,
1507 .mask_separate = BIT(IIO_EV_INFO_VALUE),
1509 .type = IIO_EV_TYPE_THRESH,
1510 .dir = IIO_EV_DIR_EITHER,
1511 .mask_separate = BIT(IIO_EV_INFO_PERIOD) |
1512 BIT(IIO_EV_INFO_ENABLE),
1516 static const struct tsl2772_chip_info tsl2772_chip_info_tbl[] = {
1518 .channel_with_events = {
1523 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1525 .type = IIO_INTENSITY,
1528 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1529 BIT(IIO_CHAN_INFO_INT_TIME) |
1530 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1531 BIT(IIO_CHAN_INFO_CALIBBIAS),
1532 .info_mask_separate_available =
1533 BIT(IIO_CHAN_INFO_INT_TIME) |
1534 BIT(IIO_CHAN_INFO_CALIBSCALE),
1535 .event_spec = tsl2772_events,
1536 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1538 .type = IIO_INTENSITY,
1543 .channel_without_events = {
1548 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1550 .type = IIO_INTENSITY,
1553 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1554 BIT(IIO_CHAN_INFO_INT_TIME) |
1555 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1556 BIT(IIO_CHAN_INFO_CALIBBIAS),
1557 .info_mask_separate_available =
1558 BIT(IIO_CHAN_INFO_INT_TIME) |
1559 BIT(IIO_CHAN_INFO_CALIBSCALE),
1561 .type = IIO_INTENSITY,
1566 .chan_table_elements = 3,
1567 .info = &tsl2772_device_info[ALS],
1570 .channel_with_events = {
1572 .type = IIO_PROXIMITY,
1575 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1576 .event_spec = tsl2772_events,
1577 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1580 .channel_without_events = {
1582 .type = IIO_PROXIMITY,
1585 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1588 .chan_table_elements = 1,
1589 .info = &tsl2772_device_info[PRX],
1592 .channel_with_events = {
1597 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1599 .type = IIO_INTENSITY,
1602 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1603 BIT(IIO_CHAN_INFO_INT_TIME) |
1604 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1605 BIT(IIO_CHAN_INFO_CALIBBIAS),
1606 .info_mask_separate_available =
1607 BIT(IIO_CHAN_INFO_INT_TIME) |
1608 BIT(IIO_CHAN_INFO_CALIBSCALE),
1609 .event_spec = tsl2772_events,
1610 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1612 .type = IIO_INTENSITY,
1615 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1617 .type = IIO_PROXIMITY,
1620 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1621 .event_spec = tsl2772_events,
1622 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1625 .channel_without_events = {
1630 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1632 .type = IIO_INTENSITY,
1635 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1636 BIT(IIO_CHAN_INFO_INT_TIME) |
1637 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1638 BIT(IIO_CHAN_INFO_CALIBBIAS),
1639 .info_mask_separate_available =
1640 BIT(IIO_CHAN_INFO_INT_TIME) |
1641 BIT(IIO_CHAN_INFO_CALIBSCALE),
1643 .type = IIO_INTENSITY,
1646 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1648 .type = IIO_PROXIMITY,
1651 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1654 .chan_table_elements = 4,
1655 .info = &tsl2772_device_info[ALSPRX],
1658 .channel_with_events = {
1660 .type = IIO_PROXIMITY,
1663 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1664 BIT(IIO_CHAN_INFO_CALIBSCALE),
1665 .info_mask_separate_available =
1666 BIT(IIO_CHAN_INFO_CALIBSCALE),
1667 .event_spec = tsl2772_events,
1668 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1671 .channel_without_events = {
1673 .type = IIO_PROXIMITY,
1676 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1677 BIT(IIO_CHAN_INFO_CALIBSCALE),
1678 .info_mask_separate_available =
1679 BIT(IIO_CHAN_INFO_CALIBSCALE),
1682 .chan_table_elements = 1,
1683 .info = &tsl2772_device_info[PRX2],
1686 .channel_with_events = {
1691 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1693 .type = IIO_INTENSITY,
1696 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1697 BIT(IIO_CHAN_INFO_INT_TIME) |
1698 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1699 BIT(IIO_CHAN_INFO_CALIBBIAS),
1700 .info_mask_separate_available =
1701 BIT(IIO_CHAN_INFO_INT_TIME) |
1702 BIT(IIO_CHAN_INFO_CALIBSCALE),
1703 .event_spec = tsl2772_events,
1704 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1706 .type = IIO_INTENSITY,
1709 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1711 .type = IIO_PROXIMITY,
1714 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1715 BIT(IIO_CHAN_INFO_CALIBSCALE),
1716 .info_mask_separate_available =
1717 BIT(IIO_CHAN_INFO_CALIBSCALE),
1718 .event_spec = tsl2772_events,
1719 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1722 .channel_without_events = {
1727 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1729 .type = IIO_INTENSITY,
1732 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1733 BIT(IIO_CHAN_INFO_INT_TIME) |
1734 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1735 BIT(IIO_CHAN_INFO_CALIBBIAS),
1736 .info_mask_separate_available =
1737 BIT(IIO_CHAN_INFO_INT_TIME) |
1738 BIT(IIO_CHAN_INFO_CALIBSCALE),
1740 .type = IIO_INTENSITY,
1743 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1745 .type = IIO_PROXIMITY,
1748 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1749 BIT(IIO_CHAN_INFO_CALIBSCALE),
1750 .info_mask_separate_available =
1751 BIT(IIO_CHAN_INFO_CALIBSCALE),
1754 .chan_table_elements = 4,
1755 .info = &tsl2772_device_info[ALSPRX2],
1759 static int tsl2772_probe(struct i2c_client *clientp,
1760 const struct i2c_device_id *id)
1762 struct iio_dev *indio_dev;
1763 struct tsl2772_chip *chip;
1766 indio_dev = devm_iio_device_alloc(&clientp->dev, sizeof(*chip));
1770 chip = iio_priv(indio_dev);
1771 chip->client = clientp;
1772 i2c_set_clientdata(clientp, indio_dev);
1774 chip->supplies[TSL2772_SUPPLY_VDD].supply = "vdd";
1775 chip->supplies[TSL2772_SUPPLY_VDDIO].supply = "vddio";
1777 ret = devm_regulator_bulk_get(&clientp->dev,
1778 ARRAY_SIZE(chip->supplies),
1781 return dev_err_probe(&clientp->dev, ret, "Failed to get regulators\n");
1783 ret = regulator_bulk_enable(ARRAY_SIZE(chip->supplies), chip->supplies);
1785 dev_err(&clientp->dev, "Failed to enable regulators: %d\n",
1790 ret = devm_add_action_or_reset(&clientp->dev,
1791 tsl2772_disable_regulators_action,
1794 dev_err(&clientp->dev, "Failed to setup regulator cleanup action %d\n",
1799 usleep_range(TSL2772_BOOT_MIN_SLEEP_TIME, TSL2772_BOOT_MAX_SLEEP_TIME);
1801 ret = i2c_smbus_read_byte_data(chip->client,
1802 TSL2772_CMD_REG | TSL2772_CHIPID);
1806 if (tsl2772_device_id_verif(ret, id->driver_data) <= 0) {
1807 dev_info(&chip->client->dev,
1808 "%s: i2c device found does not match expected id\n",
1813 ret = i2c_smbus_write_byte(clientp, TSL2772_CMD_REG | TSL2772_CNTRL);
1815 dev_err(&clientp->dev,
1816 "%s: Failed to write to CMD register: %d\n",
1821 mutex_init(&chip->als_mutex);
1822 mutex_init(&chip->prox_mutex);
1824 chip->tsl2772_chip_status = TSL2772_CHIP_UNKNOWN;
1825 chip->pdata = dev_get_platdata(&clientp->dev);
1826 chip->id = id->driver_data;
1828 &tsl2772_chip_info_tbl[device_channel_config[id->driver_data]];
1830 indio_dev->info = chip->chip_info->info;
1831 indio_dev->modes = INDIO_DIRECT_MODE;
1832 indio_dev->name = chip->client->name;
1833 indio_dev->num_channels = chip->chip_info->chan_table_elements;
1836 indio_dev->channels = chip->chip_info->channel_with_events;
1838 ret = devm_request_threaded_irq(&clientp->dev, clientp->irq,
1840 &tsl2772_event_handler,
1841 IRQF_TRIGGER_FALLING |
1846 dev_err(&clientp->dev,
1847 "%s: irq request failed\n", __func__);
1851 indio_dev->channels = chip->chip_info->channel_without_events;
1854 tsl2772_defaults(chip);
1855 ret = tsl2772_chip_on(indio_dev);
1859 ret = devm_add_action_or_reset(&clientp->dev,
1860 tsl2772_chip_off_action,
1865 return devm_iio_device_register(&clientp->dev, indio_dev);
1868 static int tsl2772_suspend(struct device *dev)
1870 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1871 struct tsl2772_chip *chip = iio_priv(indio_dev);
1874 ret = tsl2772_chip_off(indio_dev);
1875 regulator_bulk_disable(ARRAY_SIZE(chip->supplies), chip->supplies);
1880 static int tsl2772_resume(struct device *dev)
1882 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1883 struct tsl2772_chip *chip = iio_priv(indio_dev);
1886 ret = regulator_bulk_enable(ARRAY_SIZE(chip->supplies), chip->supplies);
1890 usleep_range(TSL2772_BOOT_MIN_SLEEP_TIME, TSL2772_BOOT_MAX_SLEEP_TIME);
1892 return tsl2772_chip_on(indio_dev);
1895 static const struct i2c_device_id tsl2772_idtable[] = {
1896 { "tsl2571", tsl2571 },
1897 { "tsl2671", tsl2671 },
1898 { "tmd2671", tmd2671 },
1899 { "tsl2771", tsl2771 },
1900 { "tmd2771", tmd2771 },
1901 { "tsl2572", tsl2572 },
1902 { "tsl2672", tsl2672 },
1903 { "tmd2672", tmd2672 },
1904 { "tsl2772", tsl2772 },
1905 { "tmd2772", tmd2772 },
1906 { "apds9930", apds9930},
1910 MODULE_DEVICE_TABLE(i2c, tsl2772_idtable);
1912 static const struct of_device_id tsl2772_of_match[] = {
1913 { .compatible = "amstaos,tsl2571" },
1914 { .compatible = "amstaos,tsl2671" },
1915 { .compatible = "amstaos,tmd2671" },
1916 { .compatible = "amstaos,tsl2771" },
1917 { .compatible = "amstaos,tmd2771" },
1918 { .compatible = "amstaos,tsl2572" },
1919 { .compatible = "amstaos,tsl2672" },
1920 { .compatible = "amstaos,tmd2672" },
1921 { .compatible = "amstaos,tsl2772" },
1922 { .compatible = "amstaos,tmd2772" },
1923 { .compatible = "avago,apds9930" },
1926 MODULE_DEVICE_TABLE(of, tsl2772_of_match);
1928 static const struct dev_pm_ops tsl2772_pm_ops = {
1929 .suspend = tsl2772_suspend,
1930 .resume = tsl2772_resume,
1933 static struct i2c_driver tsl2772_driver = {
1936 .of_match_table = tsl2772_of_match,
1937 .pm = &tsl2772_pm_ops,
1939 .id_table = tsl2772_idtable,
1940 .probe = tsl2772_probe,
1943 module_i2c_driver(tsl2772_driver);
1945 MODULE_AUTHOR("J. August Brenner <Jon.Brenner@ams.com>");
1946 MODULE_AUTHOR("Brian Masney <masneyb@onstation.org>");
1947 MODULE_DESCRIPTION("TAOS tsl2772 ambient and proximity light sensor driver");
1948 MODULE_LICENSE("GPL");