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
137 /* Per-device data */
138 struct tsl2772_als_info {
144 struct tsl2772_chip_info {
145 int chan_table_elements;
146 struct iio_chan_spec channel_with_events[4];
147 struct iio_chan_spec channel_without_events[4];
148 const struct iio_info *info;
151 static const int tsl2772_led_currents[][2] = {
152 { 100000, TSL2772_100_mA },
153 { 50000, TSL2772_50_mA },
154 { 25000, TSL2772_25_mA },
155 { 13000, TSL2772_13_mA },
159 struct tsl2772_chip {
161 struct mutex prox_mutex;
162 struct mutex als_mutex;
163 struct i2c_client *client;
164 struct regulator *vdd_supply;
165 struct regulator *vddio_supply;
167 struct tsl2772_als_info als_cur_info;
168 struct tsl2772_settings settings;
169 struct tsl2772_platform_data *pdata;
170 int als_gain_time_scale;
172 int tsl2772_chip_status;
173 u8 tsl2772_config[TSL2772_MAX_CONFIG_REG];
174 const struct tsl2772_chip_info *chip_info;
175 const struct iio_info *info;
178 * This structure is intentionally large to accommodate
180 * Sized to 9 = max 8 segments + 1 termination segment
182 struct tsl2772_lux tsl2772_device_lux[TSL2772_MAX_LUX_TABLE_SIZE];
186 * Different devices require different coefficents, and these numbers were
187 * derived from the 'Lux Equation' section of the various device datasheets.
188 * All of these coefficients assume a Glass Attenuation (GA) factor of 1.
189 * The coefficients are multiplied by 1000 to avoid floating point operations.
190 * The two rows in each table correspond to the Lux1 and Lux2 equations from
193 static const struct tsl2772_lux tsl2x71_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
199 static const struct tsl2772_lux tmd2x71_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
205 static const struct tsl2772_lux tsl2x72_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
211 static const struct tsl2772_lux tmd2x72_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
217 static const struct tsl2772_lux apds9930_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
223 static const struct tsl2772_lux *tsl2772_default_lux_table_group[] = {
224 [tsl2571] = tsl2x71_lux_table,
225 [tsl2671] = tsl2x71_lux_table,
226 [tmd2671] = tmd2x71_lux_table,
227 [tsl2771] = tsl2x71_lux_table,
228 [tmd2771] = tmd2x71_lux_table,
229 [tsl2572] = tsl2x72_lux_table,
230 [tsl2672] = tsl2x72_lux_table,
231 [tmd2672] = tmd2x72_lux_table,
232 [tsl2772] = tsl2x72_lux_table,
233 [tmd2772] = tmd2x72_lux_table,
234 [apds9930] = apds9930_lux_table,
237 static const struct tsl2772_settings tsl2772_default_settings = {
238 .als_time = 255, /* 2.72 / 2.73 ms */
240 .prox_time = 255, /* 2.72 / 2.73 ms */
243 .als_prox_config = 0,
244 .als_gain_trim = 1000,
245 .als_cal_target = 150,
246 .als_persistence = 1,
247 .als_interrupt_en = false,
248 .als_thresh_low = 200,
249 .als_thresh_high = 256,
250 .prox_persistence = 1,
251 .prox_interrupt_en = false,
253 .prox_thres_high = 512,
254 .prox_max_samples_cal = 30,
255 .prox_pulse_count = 8,
256 .prox_diode = TSL2772_DIODE1,
257 .prox_power = TSL2772_100_mA
260 static const s16 tsl2772_als_gain[] = {
267 static const s16 tsl2772_prox_gain[] = {
274 static const int tsl2772_int_time_avail[][6] = {
275 [tsl2571] = { 0, 2720, 0, 2720, 0, 696000 },
276 [tsl2671] = { 0, 2720, 0, 2720, 0, 696000 },
277 [tmd2671] = { 0, 2720, 0, 2720, 0, 696000 },
278 [tsl2771] = { 0, 2720, 0, 2720, 0, 696000 },
279 [tmd2771] = { 0, 2720, 0, 2720, 0, 696000 },
280 [tsl2572] = { 0, 2730, 0, 2730, 0, 699000 },
281 [tsl2672] = { 0, 2730, 0, 2730, 0, 699000 },
282 [tmd2672] = { 0, 2730, 0, 2730, 0, 699000 },
283 [tsl2772] = { 0, 2730, 0, 2730, 0, 699000 },
284 [tmd2772] = { 0, 2730, 0, 2730, 0, 699000 },
285 [apds9930] = { 0, 2730, 0, 2730, 0, 699000 },
288 static int tsl2772_int_calibscale_avail[] = { 1, 8, 16, 120 };
290 static int tsl2772_prox_calibscale_avail[] = { 1, 2, 4, 8 };
292 /* Channel variations */
301 static const u8 device_channel_config[] = {
312 [apds9930] = ALSPRX2,
315 static int tsl2772_read_status(struct tsl2772_chip *chip)
319 ret = i2c_smbus_read_byte_data(chip->client,
320 TSL2772_CMD_REG | TSL2772_STATUS);
322 dev_err(&chip->client->dev,
323 "%s: failed to read STATUS register: %d\n", __func__,
329 static int tsl2772_write_control_reg(struct tsl2772_chip *chip, u8 data)
333 ret = i2c_smbus_write_byte_data(chip->client,
334 TSL2772_CMD_REG | TSL2772_CNTRL, data);
336 dev_err(&chip->client->dev,
337 "%s: failed to write to control register %x: %d\n",
338 __func__, data, ret);
344 static int tsl2772_read_autoinc_regs(struct tsl2772_chip *chip, int lower_reg,
350 ret = i2c_smbus_write_byte(chip->client,
351 TSL2772_CMD_REG | TSL2772_CMD_AUTOINC_PROTO |
354 dev_err(&chip->client->dev,
355 "%s: failed to enable auto increment protocol: %d\n",
360 ret = i2c_smbus_read_byte_data(chip->client,
361 TSL2772_CMD_REG | lower_reg);
363 dev_err(&chip->client->dev,
364 "%s: failed to read from register %x: %d\n", __func__,
370 ret = i2c_smbus_read_byte_data(chip->client,
371 TSL2772_CMD_REG | upper_reg);
373 dev_err(&chip->client->dev,
374 "%s: failed to read from register %x: %d\n", __func__,
380 ret = i2c_smbus_write_byte(chip->client,
381 TSL2772_CMD_REG | TSL2772_CMD_REPEAT_PROTO |
384 dev_err(&chip->client->dev,
385 "%s: failed to enable repeated byte protocol: %d\n",
390 return le16_to_cpup((const __le16 *)&buf[0]);
394 * tsl2772_get_lux() - Reads and calculates current lux value.
395 * @indio_dev: pointer to IIO device
397 * The raw ch0 and ch1 values of the ambient light sensed in the last
398 * integration cycle are read from the device. The raw values are multiplied
399 * by a device-specific scale factor, and divided by the integration time and
400 * device gain. The code supports multiple lux equations through the lux table
401 * coefficients. A lux gain trim is applied to each lux equation, and then the
402 * maximum lux within the interval 0..65535 is selected.
404 static int tsl2772_get_lux(struct iio_dev *indio_dev)
406 struct tsl2772_chip *chip = iio_priv(indio_dev);
407 struct tsl2772_lux *p;
411 mutex_lock(&chip->als_mutex);
413 if (chip->tsl2772_chip_status != TSL2772_CHIP_WORKING) {
414 dev_err(&chip->client->dev, "%s: device is not enabled\n",
420 ret = tsl2772_read_status(chip);
424 if (!(ret & TSL2772_STA_ADC_VALID)) {
425 dev_err(&chip->client->dev,
426 "%s: data not valid yet\n", __func__);
427 ret = chip->als_cur_info.lux; /* return LAST VALUE */
431 ret = tsl2772_read_autoinc_regs(chip, TSL2772_ALS_CHAN0LO,
432 TSL2772_ALS_CHAN0HI);
435 chip->als_cur_info.als_ch0 = ret;
437 ret = tsl2772_read_autoinc_regs(chip, TSL2772_ALS_CHAN1LO,
438 TSL2772_ALS_CHAN1HI);
441 chip->als_cur_info.als_ch1 = ret;
443 if (chip->als_cur_info.als_ch0 >= chip->als_saturation) {
444 max_lux = TSL2772_LUX_CALC_OVER_FLOW;
445 goto update_struct_with_max_lux;
448 if (!chip->als_cur_info.als_ch0) {
449 /* have no data, so return LAST VALUE */
450 ret = chip->als_cur_info.lux;
456 for (p = (struct tsl2772_lux *)chip->tsl2772_device_lux; p->ch0 != 0;
460 lux = ((chip->als_cur_info.als_ch0 * p->ch0) -
461 (chip->als_cur_info.als_ch1 * p->ch1)) /
462 chip->als_gain_time_scale;
465 * The als_gain_trim can have a value within the range 250..4000
466 * and is a multiplier for the lux. A trim of 1000 makes no
467 * changes to the lux, less than 1000 scales it down, and
468 * greater than 1000 scales it up.
470 lux = (lux * chip->settings.als_gain_trim) / 1000;
472 if (lux > TSL2772_LUX_CALC_OVER_FLOW) {
477 max_lux = max(max_lux, lux);
480 if (overflow && max_lux == 0)
481 max_lux = TSL2772_LUX_CALC_OVER_FLOW;
483 update_struct_with_max_lux:
484 chip->als_cur_info.lux = max_lux;
488 mutex_unlock(&chip->als_mutex);
494 * tsl2772_get_prox() - Reads proximity data registers and updates
497 * @indio_dev: pointer to IIO device
499 static int tsl2772_get_prox(struct iio_dev *indio_dev)
501 struct tsl2772_chip *chip = iio_priv(indio_dev);
504 mutex_lock(&chip->prox_mutex);
506 ret = tsl2772_read_status(chip);
516 if (!(ret & TSL2772_STA_ADC_VALID)) {
527 if (!(ret & TSL2772_STA_PRX_VALID)) {
534 ret = tsl2772_read_autoinc_regs(chip, TSL2772_PRX_LO, TSL2772_PRX_HI);
537 chip->prox_data = ret;
540 mutex_unlock(&chip->prox_mutex);
545 static int tsl2772_read_prox_led_current(struct tsl2772_chip *chip)
547 struct device_node *of_node = chip->client->dev.of_node;
550 ret = of_property_read_u32(of_node, "led-max-microamp", &tmp);
554 for (i = 0; tsl2772_led_currents[i][0] != 0; i++) {
555 if (tmp == tsl2772_led_currents[i][0]) {
556 chip->settings.prox_power = tsl2772_led_currents[i][1];
561 dev_err(&chip->client->dev, "Invalid value %d for led-max-microamp\n",
568 static int tsl2772_read_prox_diodes(struct tsl2772_chip *chip)
570 struct device_node *of_node = chip->client->dev.of_node;
571 int i, ret, num_leds, prox_diode_mask;
572 u32 leds[TSL2772_MAX_PROX_LEDS];
574 ret = of_property_count_u32_elems(of_node, "amstaos,proximity-diodes");
579 if (num_leds > TSL2772_MAX_PROX_LEDS)
580 num_leds = TSL2772_MAX_PROX_LEDS;
582 ret = of_property_read_u32_array(of_node, "amstaos,proximity-diodes",
585 dev_err(&chip->client->dev,
586 "Invalid value for amstaos,proximity-diodes: %d.\n",
592 for (i = 0; i < num_leds; i++) {
594 prox_diode_mask |= TSL2772_DIODE0;
595 else if (leds[i] == 1)
596 prox_diode_mask |= TSL2772_DIODE1;
598 dev_err(&chip->client->dev,
599 "Invalid value %d in amstaos,proximity-diodes.\n",
608 static void tsl2772_parse_dt(struct tsl2772_chip *chip)
610 tsl2772_read_prox_led_current(chip);
611 tsl2772_read_prox_diodes(chip);
615 * tsl2772_defaults() - Populates the device nominal operating parameters
616 * with those provided by a 'platform' data struct or
617 * with prefined defaults.
619 * @chip: pointer to device structure.
621 static void tsl2772_defaults(struct tsl2772_chip *chip)
623 /* If Operational settings defined elsewhere.. */
624 if (chip->pdata && chip->pdata->platform_default_settings)
625 memcpy(&chip->settings, chip->pdata->platform_default_settings,
626 sizeof(tsl2772_default_settings));
628 memcpy(&chip->settings, &tsl2772_default_settings,
629 sizeof(tsl2772_default_settings));
631 /* Load up the proper lux table. */
632 if (chip->pdata && chip->pdata->platform_lux_table[0].ch0 != 0)
633 memcpy(chip->tsl2772_device_lux,
634 chip->pdata->platform_lux_table,
635 sizeof(chip->pdata->platform_lux_table));
637 memcpy(chip->tsl2772_device_lux,
638 tsl2772_default_lux_table_group[chip->id],
639 TSL2772_DEFAULT_TABLE_BYTES);
641 tsl2772_parse_dt(chip);
645 * tsl2772_als_calibrate() - Obtain single reading and calculate
648 * @indio_dev: pointer to IIO device
650 static int tsl2772_als_calibrate(struct iio_dev *indio_dev)
652 struct tsl2772_chip *chip = iio_priv(indio_dev);
655 ret = i2c_smbus_read_byte_data(chip->client,
656 TSL2772_CMD_REG | TSL2772_CNTRL);
658 dev_err(&chip->client->dev,
659 "%s: failed to read from the CNTRL register\n",
664 if ((ret & (TSL2772_CNTL_ADC_ENBL | TSL2772_CNTL_PWR_ON))
665 != (TSL2772_CNTL_ADC_ENBL | TSL2772_CNTL_PWR_ON)) {
666 dev_err(&chip->client->dev,
667 "%s: Device is not powered on and/or ADC is not enabled\n",
670 } else if ((ret & TSL2772_STA_ADC_VALID) != TSL2772_STA_ADC_VALID) {
671 dev_err(&chip->client->dev,
672 "%s: The two ADC channels have not completed an integration cycle\n",
677 lux_val = tsl2772_get_lux(indio_dev);
679 dev_err(&chip->client->dev,
680 "%s: failed to get lux\n", __func__);
686 ret = (chip->settings.als_cal_target * chip->settings.als_gain_trim) /
688 if (ret < TSL2772_ALS_GAIN_TRIM_MIN || ret > TSL2772_ALS_GAIN_TRIM_MAX)
691 chip->settings.als_gain_trim = ret;
696 static void tsl2772_disable_regulators_action(void *_data)
698 struct tsl2772_chip *chip = _data;
700 regulator_disable(chip->vdd_supply);
701 regulator_disable(chip->vddio_supply);
704 static int tsl2772_enable_regulator(struct tsl2772_chip *chip,
705 struct regulator *regulator)
709 ret = regulator_enable(regulator);
711 dev_err(&chip->client->dev, "Failed to enable regulator: %d\n",
719 static struct regulator *tsl2772_get_regulator(struct tsl2772_chip *chip,
722 struct regulator *regulator;
725 regulator = devm_regulator_get(&chip->client->dev, name);
726 if (IS_ERR(regulator)) {
727 if (PTR_ERR(regulator) != -EPROBE_DEFER)
728 dev_err(&chip->client->dev,
729 "Failed to get %s regulator %d\n",
730 name, (int)PTR_ERR(regulator));
735 ret = tsl2772_enable_regulator(chip, regulator);
742 static int tsl2772_chip_on(struct iio_dev *indio_dev)
744 struct tsl2772_chip *chip = iio_priv(indio_dev);
745 int ret, i, als_count, als_time_us;
746 u8 *dev_reg, reg_val;
748 /* Non calculated parameters */
749 chip->tsl2772_config[TSL2772_ALS_TIME] = chip->settings.als_time;
750 chip->tsl2772_config[TSL2772_PRX_TIME] = chip->settings.prox_time;
751 chip->tsl2772_config[TSL2772_WAIT_TIME] = chip->settings.wait_time;
752 chip->tsl2772_config[TSL2772_ALS_PRX_CONFIG] =
753 chip->settings.als_prox_config;
755 chip->tsl2772_config[TSL2772_ALS_MINTHRESHLO] =
756 (chip->settings.als_thresh_low) & 0xFF;
757 chip->tsl2772_config[TSL2772_ALS_MINTHRESHHI] =
758 (chip->settings.als_thresh_low >> 8) & 0xFF;
759 chip->tsl2772_config[TSL2772_ALS_MAXTHRESHLO] =
760 (chip->settings.als_thresh_high) & 0xFF;
761 chip->tsl2772_config[TSL2772_ALS_MAXTHRESHHI] =
762 (chip->settings.als_thresh_high >> 8) & 0xFF;
763 chip->tsl2772_config[TSL2772_PERSISTENCE] =
764 (chip->settings.prox_persistence & 0xFF) << 4 |
765 (chip->settings.als_persistence & 0xFF);
767 chip->tsl2772_config[TSL2772_PRX_COUNT] =
768 chip->settings.prox_pulse_count;
769 chip->tsl2772_config[TSL2772_PRX_MINTHRESHLO] =
770 (chip->settings.prox_thres_low) & 0xFF;
771 chip->tsl2772_config[TSL2772_PRX_MINTHRESHHI] =
772 (chip->settings.prox_thres_low >> 8) & 0xFF;
773 chip->tsl2772_config[TSL2772_PRX_MAXTHRESHLO] =
774 (chip->settings.prox_thres_high) & 0xFF;
775 chip->tsl2772_config[TSL2772_PRX_MAXTHRESHHI] =
776 (chip->settings.prox_thres_high >> 8) & 0xFF;
778 /* and make sure we're not already on */
779 if (chip->tsl2772_chip_status == TSL2772_CHIP_WORKING) {
780 /* if forcing a register update - turn off, then on */
781 dev_info(&chip->client->dev, "device is already enabled\n");
785 /* Set the gain based on tsl2772_settings struct */
786 chip->tsl2772_config[TSL2772_GAIN] =
787 (chip->settings.als_gain & 0xFF) |
788 ((chip->settings.prox_gain & 0xFF) << 2) |
789 (chip->settings.prox_diode << 4) |
790 (chip->settings.prox_power << 6);
792 /* set chip time scaling and saturation */
793 als_count = 256 - chip->settings.als_time;
794 als_time_us = als_count * tsl2772_int_time_avail[chip->id][3];
795 chip->als_saturation = als_count * 768; /* 75% of full scale */
796 chip->als_gain_time_scale = als_time_us *
797 tsl2772_als_gain[chip->settings.als_gain];
800 * TSL2772 Specific power-on / adc enable sequence
801 * Power on the device 1st.
803 ret = tsl2772_write_control_reg(chip, TSL2772_CNTL_PWR_ON);
808 * Use the following shadow copy for our delay before enabling ADC.
809 * Write all the registers.
811 for (i = 0, dev_reg = chip->tsl2772_config;
812 i < TSL2772_MAX_CONFIG_REG; i++) {
813 int reg = TSL2772_CMD_REG + i;
815 ret = i2c_smbus_write_byte_data(chip->client, reg,
818 dev_err(&chip->client->dev,
819 "%s: failed to write to register %x: %d\n",
825 /* Power-on settling time */
826 usleep_range(3000, 3500);
828 reg_val = TSL2772_CNTL_PWR_ON | TSL2772_CNTL_ADC_ENBL |
829 TSL2772_CNTL_PROX_DET_ENBL;
830 if (chip->settings.als_interrupt_en)
831 reg_val |= TSL2772_CNTL_ALS_INT_ENBL;
832 if (chip->settings.prox_interrupt_en)
833 reg_val |= TSL2772_CNTL_PROX_INT_ENBL;
835 ret = tsl2772_write_control_reg(chip, reg_val);
839 ret = i2c_smbus_write_byte(chip->client,
840 TSL2772_CMD_REG | TSL2772_CMD_SPL_FN |
841 TSL2772_CMD_PROXALS_INT_CLR);
843 dev_err(&chip->client->dev,
844 "%s: failed to clear interrupt status: %d\n",
849 chip->tsl2772_chip_status = TSL2772_CHIP_WORKING;
854 static int tsl2772_chip_off(struct iio_dev *indio_dev)
856 struct tsl2772_chip *chip = iio_priv(indio_dev);
858 /* turn device off */
859 chip->tsl2772_chip_status = TSL2772_CHIP_SUSPENDED;
860 return tsl2772_write_control_reg(chip, 0x00);
864 * tsl2772_invoke_change - power cycle the device to implement the user
866 * @indio_dev: pointer to IIO device
868 * Obtain and lock both ALS and PROX resources, determine and save device state
869 * (On/Off), cycle device to implement updated parameter, put device back into
870 * proper state, and unlock resource.
872 static int tsl2772_invoke_change(struct iio_dev *indio_dev)
874 struct tsl2772_chip *chip = iio_priv(indio_dev);
875 int device_status = chip->tsl2772_chip_status;
878 mutex_lock(&chip->als_mutex);
879 mutex_lock(&chip->prox_mutex);
881 if (device_status == TSL2772_CHIP_WORKING) {
882 ret = tsl2772_chip_off(indio_dev);
887 ret = tsl2772_chip_on(indio_dev);
890 mutex_unlock(&chip->prox_mutex);
891 mutex_unlock(&chip->als_mutex);
896 static int tsl2772_prox_cal(struct iio_dev *indio_dev)
898 struct tsl2772_chip *chip = iio_priv(indio_dev);
899 int prox_history[MAX_SAMPLES_CAL + 1];
900 int i, ret, mean, max, sample_sum;
902 if (chip->settings.prox_max_samples_cal < 1 ||
903 chip->settings.prox_max_samples_cal > MAX_SAMPLES_CAL)
906 for (i = 0; i < chip->settings.prox_max_samples_cal; i++) {
907 usleep_range(15000, 17500);
908 ret = tsl2772_get_prox(indio_dev);
912 prox_history[i] = chip->prox_data;
917 for (i = 0; i < chip->settings.prox_max_samples_cal; i++) {
918 sample_sum += prox_history[i];
919 max = max(max, prox_history[i]);
921 mean = sample_sum / chip->settings.prox_max_samples_cal;
923 chip->settings.prox_thres_high = (max << 1) - mean;
925 return tsl2772_invoke_change(indio_dev);
928 static int tsl2772_read_avail(struct iio_dev *indio_dev,
929 struct iio_chan_spec const *chan,
930 const int **vals, int *type, int *length,
933 struct tsl2772_chip *chip = iio_priv(indio_dev);
936 case IIO_CHAN_INFO_CALIBSCALE:
937 if (chan->type == IIO_INTENSITY) {
938 *length = ARRAY_SIZE(tsl2772_int_calibscale_avail);
939 *vals = tsl2772_int_calibscale_avail;
941 *length = ARRAY_SIZE(tsl2772_prox_calibscale_avail);
942 *vals = tsl2772_prox_calibscale_avail;
945 return IIO_AVAIL_LIST;
946 case IIO_CHAN_INFO_INT_TIME:
947 *length = ARRAY_SIZE(tsl2772_int_time_avail[chip->id]);
948 *vals = tsl2772_int_time_avail[chip->id];
949 *type = IIO_VAL_INT_PLUS_MICRO;
950 return IIO_AVAIL_RANGE;
956 static ssize_t in_illuminance0_target_input_show(struct device *dev,
957 struct device_attribute *attr,
960 struct tsl2772_chip *chip = iio_priv(dev_to_iio_dev(dev));
962 return snprintf(buf, PAGE_SIZE, "%d\n", chip->settings.als_cal_target);
965 static ssize_t in_illuminance0_target_input_store(struct device *dev,
966 struct device_attribute *attr,
967 const char *buf, size_t len)
969 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
970 struct tsl2772_chip *chip = iio_priv(indio_dev);
974 if (kstrtou16(buf, 0, &value))
977 chip->settings.als_cal_target = value;
978 ret = tsl2772_invoke_change(indio_dev);
985 static ssize_t in_illuminance0_calibrate_store(struct device *dev,
986 struct device_attribute *attr,
987 const char *buf, size_t len)
989 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
993 if (kstrtobool(buf, &value) || !value)
996 ret = tsl2772_als_calibrate(indio_dev);
1000 ret = tsl2772_invoke_change(indio_dev);
1007 static ssize_t in_illuminance0_lux_table_show(struct device *dev,
1008 struct device_attribute *attr,
1011 struct tsl2772_chip *chip = iio_priv(dev_to_iio_dev(dev));
1015 while (i < TSL2772_MAX_LUX_TABLE_SIZE) {
1016 offset += snprintf(buf + offset, PAGE_SIZE, "%u,%u,",
1017 chip->tsl2772_device_lux[i].ch0,
1018 chip->tsl2772_device_lux[i].ch1);
1019 if (chip->tsl2772_device_lux[i].ch0 == 0) {
1021 * We just printed the first "0" entry.
1022 * Now get rid of the extra "," and break.
1030 offset += snprintf(buf + offset, PAGE_SIZE, "\n");
1034 static ssize_t in_illuminance0_lux_table_store(struct device *dev,
1035 struct device_attribute *attr,
1036 const char *buf, size_t len)
1038 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1039 struct tsl2772_chip *chip = iio_priv(indio_dev);
1040 int value[ARRAY_SIZE(chip->tsl2772_device_lux) * 2 + 1];
1043 get_options(buf, ARRAY_SIZE(value), value);
1046 * We now have an array of ints starting at value[1], and
1047 * enumerated by value[0].
1048 * We expect each group of two ints to be one table entry,
1049 * and the last table entry is all 0.
1052 if ((n % 2) || n < 4 ||
1053 n > ((ARRAY_SIZE(chip->tsl2772_device_lux) - 1) * 2))
1056 if ((value[(n - 1)] | value[n]) != 0)
1059 if (chip->tsl2772_chip_status == TSL2772_CHIP_WORKING) {
1060 ret = tsl2772_chip_off(indio_dev);
1065 /* Zero out the table */
1066 memset(chip->tsl2772_device_lux, 0, sizeof(chip->tsl2772_device_lux));
1067 memcpy(chip->tsl2772_device_lux, &value[1], (value[0] * 4));
1069 ret = tsl2772_invoke_change(indio_dev);
1076 static ssize_t in_proximity0_calibrate_store(struct device *dev,
1077 struct device_attribute *attr,
1078 const char *buf, size_t len)
1080 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1084 if (kstrtobool(buf, &value) || !value)
1087 ret = tsl2772_prox_cal(indio_dev);
1091 ret = tsl2772_invoke_change(indio_dev);
1098 static int tsl2772_read_interrupt_config(struct iio_dev *indio_dev,
1099 const struct iio_chan_spec *chan,
1100 enum iio_event_type type,
1101 enum iio_event_direction dir)
1103 struct tsl2772_chip *chip = iio_priv(indio_dev);
1105 if (chan->type == IIO_INTENSITY)
1106 return chip->settings.als_interrupt_en;
1108 return chip->settings.prox_interrupt_en;
1111 static int tsl2772_write_interrupt_config(struct iio_dev *indio_dev,
1112 const struct iio_chan_spec *chan,
1113 enum iio_event_type type,
1114 enum iio_event_direction dir,
1117 struct tsl2772_chip *chip = iio_priv(indio_dev);
1119 if (chan->type == IIO_INTENSITY)
1120 chip->settings.als_interrupt_en = val ? true : false;
1122 chip->settings.prox_interrupt_en = val ? true : false;
1124 return tsl2772_invoke_change(indio_dev);
1127 static int tsl2772_write_event_value(struct iio_dev *indio_dev,
1128 const struct iio_chan_spec *chan,
1129 enum iio_event_type type,
1130 enum iio_event_direction dir,
1131 enum iio_event_info info,
1134 struct tsl2772_chip *chip = iio_priv(indio_dev);
1135 int ret = -EINVAL, count, persistence;
1139 case IIO_EV_INFO_VALUE:
1140 if (chan->type == IIO_INTENSITY) {
1142 case IIO_EV_DIR_RISING:
1143 chip->settings.als_thresh_high = val;
1146 case IIO_EV_DIR_FALLING:
1147 chip->settings.als_thresh_low = val;
1155 case IIO_EV_DIR_RISING:
1156 chip->settings.prox_thres_high = val;
1159 case IIO_EV_DIR_FALLING:
1160 chip->settings.prox_thres_low = val;
1168 case IIO_EV_INFO_PERIOD:
1169 if (chan->type == IIO_INTENSITY)
1170 time = chip->settings.als_time;
1172 time = chip->settings.prox_time;
1175 persistence = ((val * 1000000) + val2) /
1176 (count * tsl2772_int_time_avail[chip->id][3]);
1178 if (chan->type == IIO_INTENSITY) {
1179 /* ALS filter values are 1, 2, 3, 5, 10, 15, ..., 60 */
1180 if (persistence > 3)
1181 persistence = (persistence / 5) + 3;
1183 chip->settings.als_persistence = persistence;
1185 chip->settings.prox_persistence = persistence;
1197 return tsl2772_invoke_change(indio_dev);
1200 static int tsl2772_read_event_value(struct iio_dev *indio_dev,
1201 const struct iio_chan_spec *chan,
1202 enum iio_event_type type,
1203 enum iio_event_direction dir,
1204 enum iio_event_info info,
1205 int *val, int *val2)
1207 struct tsl2772_chip *chip = iio_priv(indio_dev);
1208 int filter_delay, persistence;
1212 case IIO_EV_INFO_VALUE:
1213 if (chan->type == IIO_INTENSITY) {
1215 case IIO_EV_DIR_RISING:
1216 *val = chip->settings.als_thresh_high;
1218 case IIO_EV_DIR_FALLING:
1219 *val = chip->settings.als_thresh_low;
1226 case IIO_EV_DIR_RISING:
1227 *val = chip->settings.prox_thres_high;
1229 case IIO_EV_DIR_FALLING:
1230 *val = chip->settings.prox_thres_low;
1237 case IIO_EV_INFO_PERIOD:
1238 if (chan->type == IIO_INTENSITY) {
1239 time = chip->settings.als_time;
1240 persistence = chip->settings.als_persistence;
1242 /* ALS filter values are 1, 2, 3, 5, 10, 15, ..., 60 */
1243 if (persistence > 3)
1244 persistence = (persistence - 3) * 5;
1246 time = chip->settings.prox_time;
1247 persistence = chip->settings.prox_persistence;
1250 filter_delay = persistence * (256 - time) *
1251 tsl2772_int_time_avail[chip->id][3];
1253 *val = filter_delay / 1000000;
1254 *val2 = filter_delay % 1000000;
1255 return IIO_VAL_INT_PLUS_MICRO;
1261 static int tsl2772_read_raw(struct iio_dev *indio_dev,
1262 struct iio_chan_spec const *chan,
1267 struct tsl2772_chip *chip = iio_priv(indio_dev);
1270 case IIO_CHAN_INFO_PROCESSED:
1271 switch (chan->type) {
1273 tsl2772_get_lux(indio_dev);
1274 *val = chip->als_cur_info.lux;
1279 case IIO_CHAN_INFO_RAW:
1280 switch (chan->type) {
1282 tsl2772_get_lux(indio_dev);
1283 if (chan->channel == 0)
1284 *val = chip->als_cur_info.als_ch0;
1286 *val = chip->als_cur_info.als_ch1;
1289 tsl2772_get_prox(indio_dev);
1290 *val = chip->prox_data;
1296 case IIO_CHAN_INFO_CALIBSCALE:
1297 if (chan->type == IIO_LIGHT)
1298 *val = tsl2772_als_gain[chip->settings.als_gain];
1300 *val = tsl2772_prox_gain[chip->settings.prox_gain];
1302 case IIO_CHAN_INFO_CALIBBIAS:
1303 *val = chip->settings.als_gain_trim;
1305 case IIO_CHAN_INFO_INT_TIME:
1307 *val2 = (256 - chip->settings.als_time) *
1308 tsl2772_int_time_avail[chip->id][3];
1309 return IIO_VAL_INT_PLUS_MICRO;
1315 static int tsl2772_write_raw(struct iio_dev *indio_dev,
1316 struct iio_chan_spec const *chan,
1321 struct tsl2772_chip *chip = iio_priv(indio_dev);
1324 case IIO_CHAN_INFO_CALIBSCALE:
1325 if (chan->type == IIO_INTENSITY) {
1328 chip->settings.als_gain = 0;
1331 chip->settings.als_gain = 1;
1334 chip->settings.als_gain = 2;
1337 chip->settings.als_gain = 3;
1345 chip->settings.prox_gain = 0;
1348 chip->settings.prox_gain = 1;
1351 chip->settings.prox_gain = 2;
1354 chip->settings.prox_gain = 3;
1361 case IIO_CHAN_INFO_CALIBBIAS:
1362 if (val < TSL2772_ALS_GAIN_TRIM_MIN ||
1363 val > TSL2772_ALS_GAIN_TRIM_MAX)
1366 chip->settings.als_gain_trim = val;
1368 case IIO_CHAN_INFO_INT_TIME:
1369 if (val != 0 || val2 < tsl2772_int_time_avail[chip->id][1] ||
1370 val2 > tsl2772_int_time_avail[chip->id][5])
1373 chip->settings.als_time = 256 -
1374 (val2 / tsl2772_int_time_avail[chip->id][3]);
1380 return tsl2772_invoke_change(indio_dev);
1383 static DEVICE_ATTR_RW(in_illuminance0_target_input);
1385 static DEVICE_ATTR_WO(in_illuminance0_calibrate);
1387 static DEVICE_ATTR_WO(in_proximity0_calibrate);
1389 static DEVICE_ATTR_RW(in_illuminance0_lux_table);
1391 /* Use the default register values to identify the Taos device */
1392 static int tsl2772_device_id_verif(int id, int target)
1398 return (id & 0xf0) == TRITON_ID;
1401 return (id & 0xf0) == HALIBUT_ID;
1408 return (id & 0xf0) == SWORDFISH_ID;
1414 static irqreturn_t tsl2772_event_handler(int irq, void *private)
1416 struct iio_dev *indio_dev = private;
1417 struct tsl2772_chip *chip = iio_priv(indio_dev);
1418 s64 timestamp = iio_get_time_ns(indio_dev);
1421 ret = tsl2772_read_status(chip);
1425 /* What type of interrupt do we need to process */
1426 if (ret & TSL2772_STA_PRX_INTR) {
1427 iio_push_event(indio_dev,
1428 IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY,
1435 if (ret & TSL2772_STA_ALS_INTR) {
1436 iio_push_event(indio_dev,
1437 IIO_UNMOD_EVENT_CODE(IIO_LIGHT,
1444 ret = i2c_smbus_write_byte(chip->client,
1445 TSL2772_CMD_REG | TSL2772_CMD_SPL_FN |
1446 TSL2772_CMD_PROXALS_INT_CLR);
1448 dev_err(&chip->client->dev,
1449 "%s: failed to clear interrupt status: %d\n",
1455 static struct attribute *tsl2772_ALS_device_attrs[] = {
1456 &dev_attr_in_illuminance0_target_input.attr,
1457 &dev_attr_in_illuminance0_calibrate.attr,
1458 &dev_attr_in_illuminance0_lux_table.attr,
1462 static struct attribute *tsl2772_PRX_device_attrs[] = {
1463 &dev_attr_in_proximity0_calibrate.attr,
1467 static struct attribute *tsl2772_ALSPRX_device_attrs[] = {
1468 &dev_attr_in_illuminance0_target_input.attr,
1469 &dev_attr_in_illuminance0_calibrate.attr,
1470 &dev_attr_in_illuminance0_lux_table.attr,
1474 static struct attribute *tsl2772_PRX2_device_attrs[] = {
1475 &dev_attr_in_proximity0_calibrate.attr,
1479 static struct attribute *tsl2772_ALSPRX2_device_attrs[] = {
1480 &dev_attr_in_illuminance0_target_input.attr,
1481 &dev_attr_in_illuminance0_calibrate.attr,
1482 &dev_attr_in_illuminance0_lux_table.attr,
1483 &dev_attr_in_proximity0_calibrate.attr,
1487 static const struct attribute_group tsl2772_device_attr_group_tbl[] = {
1489 .attrs = tsl2772_ALS_device_attrs,
1492 .attrs = tsl2772_PRX_device_attrs,
1495 .attrs = tsl2772_ALSPRX_device_attrs,
1498 .attrs = tsl2772_PRX2_device_attrs,
1501 .attrs = tsl2772_ALSPRX2_device_attrs,
1505 #define TSL2772_DEVICE_INFO(type)[type] = \
1507 .attrs = &tsl2772_device_attr_group_tbl[type], \
1508 .read_raw = &tsl2772_read_raw, \
1509 .read_avail = &tsl2772_read_avail, \
1510 .write_raw = &tsl2772_write_raw, \
1511 .read_event_value = &tsl2772_read_event_value, \
1512 .write_event_value = &tsl2772_write_event_value, \
1513 .read_event_config = &tsl2772_read_interrupt_config, \
1514 .write_event_config = &tsl2772_write_interrupt_config, \
1517 static const struct iio_info tsl2772_device_info[] = {
1518 TSL2772_DEVICE_INFO(ALS),
1519 TSL2772_DEVICE_INFO(PRX),
1520 TSL2772_DEVICE_INFO(ALSPRX),
1521 TSL2772_DEVICE_INFO(PRX2),
1522 TSL2772_DEVICE_INFO(ALSPRX2),
1525 static const struct iio_event_spec tsl2772_events[] = {
1527 .type = IIO_EV_TYPE_THRESH,
1528 .dir = IIO_EV_DIR_RISING,
1529 .mask_separate = BIT(IIO_EV_INFO_VALUE),
1531 .type = IIO_EV_TYPE_THRESH,
1532 .dir = IIO_EV_DIR_FALLING,
1533 .mask_separate = BIT(IIO_EV_INFO_VALUE),
1535 .type = IIO_EV_TYPE_THRESH,
1536 .dir = IIO_EV_DIR_EITHER,
1537 .mask_separate = BIT(IIO_EV_INFO_PERIOD) |
1538 BIT(IIO_EV_INFO_ENABLE),
1542 static const struct tsl2772_chip_info tsl2772_chip_info_tbl[] = {
1544 .channel_with_events = {
1549 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1551 .type = IIO_INTENSITY,
1554 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1555 BIT(IIO_CHAN_INFO_INT_TIME) |
1556 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1557 BIT(IIO_CHAN_INFO_CALIBBIAS),
1558 .info_mask_separate_available =
1559 BIT(IIO_CHAN_INFO_INT_TIME) |
1560 BIT(IIO_CHAN_INFO_CALIBSCALE),
1561 .event_spec = tsl2772_events,
1562 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1564 .type = IIO_INTENSITY,
1569 .channel_without_events = {
1574 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1576 .type = IIO_INTENSITY,
1579 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1580 BIT(IIO_CHAN_INFO_INT_TIME) |
1581 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1582 BIT(IIO_CHAN_INFO_CALIBBIAS),
1583 .info_mask_separate_available =
1584 BIT(IIO_CHAN_INFO_INT_TIME) |
1585 BIT(IIO_CHAN_INFO_CALIBSCALE),
1587 .type = IIO_INTENSITY,
1592 .chan_table_elements = 3,
1593 .info = &tsl2772_device_info[ALS],
1596 .channel_with_events = {
1598 .type = IIO_PROXIMITY,
1601 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1602 .event_spec = tsl2772_events,
1603 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1606 .channel_without_events = {
1608 .type = IIO_PROXIMITY,
1611 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1614 .chan_table_elements = 1,
1615 .info = &tsl2772_device_info[PRX],
1618 .channel_with_events = {
1623 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1625 .type = IIO_INTENSITY,
1628 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1629 BIT(IIO_CHAN_INFO_INT_TIME) |
1630 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1631 BIT(IIO_CHAN_INFO_CALIBBIAS),
1632 .info_mask_separate_available =
1633 BIT(IIO_CHAN_INFO_INT_TIME) |
1634 BIT(IIO_CHAN_INFO_CALIBSCALE),
1635 .event_spec = tsl2772_events,
1636 .num_event_specs = ARRAY_SIZE(tsl2772_events),
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),
1647 .event_spec = tsl2772_events,
1648 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1651 .channel_without_events = {
1656 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1658 .type = IIO_INTENSITY,
1661 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1662 BIT(IIO_CHAN_INFO_INT_TIME) |
1663 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1664 BIT(IIO_CHAN_INFO_CALIBBIAS),
1665 .info_mask_separate_available =
1666 BIT(IIO_CHAN_INFO_INT_TIME) |
1667 BIT(IIO_CHAN_INFO_CALIBSCALE),
1669 .type = IIO_INTENSITY,
1672 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1674 .type = IIO_PROXIMITY,
1677 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1680 .chan_table_elements = 4,
1681 .info = &tsl2772_device_info[ALSPRX],
1684 .channel_with_events = {
1686 .type = IIO_PROXIMITY,
1689 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1690 BIT(IIO_CHAN_INFO_CALIBSCALE),
1691 .info_mask_separate_available =
1692 BIT(IIO_CHAN_INFO_CALIBSCALE),
1693 .event_spec = tsl2772_events,
1694 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1697 .channel_without_events = {
1699 .type = IIO_PROXIMITY,
1702 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1703 BIT(IIO_CHAN_INFO_CALIBSCALE),
1704 .info_mask_separate_available =
1705 BIT(IIO_CHAN_INFO_CALIBSCALE),
1708 .chan_table_elements = 1,
1709 .info = &tsl2772_device_info[PRX2],
1712 .channel_with_events = {
1717 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1719 .type = IIO_INTENSITY,
1722 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1723 BIT(IIO_CHAN_INFO_INT_TIME) |
1724 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1725 BIT(IIO_CHAN_INFO_CALIBBIAS),
1726 .info_mask_separate_available =
1727 BIT(IIO_CHAN_INFO_INT_TIME) |
1728 BIT(IIO_CHAN_INFO_CALIBSCALE),
1729 .event_spec = tsl2772_events,
1730 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1732 .type = IIO_INTENSITY,
1735 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1737 .type = IIO_PROXIMITY,
1740 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1741 BIT(IIO_CHAN_INFO_CALIBSCALE),
1742 .info_mask_separate_available =
1743 BIT(IIO_CHAN_INFO_CALIBSCALE),
1744 .event_spec = tsl2772_events,
1745 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1748 .channel_without_events = {
1753 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1755 .type = IIO_INTENSITY,
1758 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1759 BIT(IIO_CHAN_INFO_INT_TIME) |
1760 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1761 BIT(IIO_CHAN_INFO_CALIBBIAS),
1762 .info_mask_separate_available =
1763 BIT(IIO_CHAN_INFO_INT_TIME) |
1764 BIT(IIO_CHAN_INFO_CALIBSCALE),
1766 .type = IIO_INTENSITY,
1769 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1771 .type = IIO_PROXIMITY,
1774 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1775 BIT(IIO_CHAN_INFO_CALIBSCALE),
1776 .info_mask_separate_available =
1777 BIT(IIO_CHAN_INFO_CALIBSCALE),
1780 .chan_table_elements = 4,
1781 .info = &tsl2772_device_info[ALSPRX2],
1785 static int tsl2772_probe(struct i2c_client *clientp,
1786 const struct i2c_device_id *id)
1788 struct iio_dev *indio_dev;
1789 struct tsl2772_chip *chip;
1792 indio_dev = devm_iio_device_alloc(&clientp->dev, sizeof(*chip));
1796 chip = iio_priv(indio_dev);
1797 chip->client = clientp;
1798 i2c_set_clientdata(clientp, indio_dev);
1800 chip->vddio_supply = tsl2772_get_regulator(chip, "vddio");
1801 if (IS_ERR(chip->vddio_supply))
1802 return PTR_ERR(chip->vddio_supply);
1804 chip->vdd_supply = tsl2772_get_regulator(chip, "vdd");
1805 if (IS_ERR(chip->vdd_supply)) {
1806 regulator_disable(chip->vddio_supply);
1807 return PTR_ERR(chip->vdd_supply);
1810 ret = devm_add_action(&clientp->dev, tsl2772_disable_regulators_action,
1813 tsl2772_disable_regulators_action(chip);
1814 dev_err(&clientp->dev, "Failed to setup regulator cleanup action %d\n",
1819 usleep_range(TSL2772_BOOT_MIN_SLEEP_TIME, TSL2772_BOOT_MAX_SLEEP_TIME);
1821 ret = i2c_smbus_read_byte_data(chip->client,
1822 TSL2772_CMD_REG | TSL2772_CHIPID);
1826 if (tsl2772_device_id_verif(ret, id->driver_data) <= 0) {
1827 dev_info(&chip->client->dev,
1828 "%s: i2c device found does not match expected id\n",
1833 ret = i2c_smbus_write_byte(clientp, TSL2772_CMD_REG | TSL2772_CNTRL);
1835 dev_err(&clientp->dev,
1836 "%s: Failed to write to CMD register: %d\n",
1841 mutex_init(&chip->als_mutex);
1842 mutex_init(&chip->prox_mutex);
1844 chip->tsl2772_chip_status = TSL2772_CHIP_UNKNOWN;
1845 chip->pdata = dev_get_platdata(&clientp->dev);
1846 chip->id = id->driver_data;
1848 &tsl2772_chip_info_tbl[device_channel_config[id->driver_data]];
1850 indio_dev->info = chip->chip_info->info;
1851 indio_dev->dev.parent = &clientp->dev;
1852 indio_dev->modes = INDIO_DIRECT_MODE;
1853 indio_dev->name = chip->client->name;
1854 indio_dev->num_channels = chip->chip_info->chan_table_elements;
1857 indio_dev->channels = chip->chip_info->channel_with_events;
1859 ret = devm_request_threaded_irq(&clientp->dev, clientp->irq,
1861 &tsl2772_event_handler,
1862 IRQF_TRIGGER_FALLING |
1867 dev_err(&clientp->dev,
1868 "%s: irq request failed\n", __func__);
1872 indio_dev->channels = chip->chip_info->channel_without_events;
1875 tsl2772_defaults(chip);
1876 ret = tsl2772_chip_on(indio_dev);
1880 ret = iio_device_register(indio_dev);
1882 tsl2772_chip_off(indio_dev);
1883 dev_err(&clientp->dev,
1884 "%s: iio registration failed\n", __func__);
1891 static int tsl2772_suspend(struct device *dev)
1893 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1894 struct tsl2772_chip *chip = iio_priv(indio_dev);
1897 ret = tsl2772_chip_off(indio_dev);
1898 regulator_disable(chip->vdd_supply);
1899 regulator_disable(chip->vddio_supply);
1904 static int tsl2772_resume(struct device *dev)
1906 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1907 struct tsl2772_chip *chip = iio_priv(indio_dev);
1910 ret = tsl2772_enable_regulator(chip, chip->vddio_supply);
1914 ret = tsl2772_enable_regulator(chip, chip->vdd_supply);
1916 regulator_disable(chip->vddio_supply);
1920 usleep_range(TSL2772_BOOT_MIN_SLEEP_TIME, TSL2772_BOOT_MAX_SLEEP_TIME);
1922 return tsl2772_chip_on(indio_dev);
1925 static int tsl2772_remove(struct i2c_client *client)
1927 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1929 tsl2772_chip_off(indio_dev);
1931 iio_device_unregister(indio_dev);
1936 static const struct i2c_device_id tsl2772_idtable[] = {
1937 { "tsl2571", tsl2571 },
1938 { "tsl2671", tsl2671 },
1939 { "tmd2671", tmd2671 },
1940 { "tsl2771", tsl2771 },
1941 { "tmd2771", tmd2771 },
1942 { "tsl2572", tsl2572 },
1943 { "tsl2672", tsl2672 },
1944 { "tmd2672", tmd2672 },
1945 { "tsl2772", tsl2772 },
1946 { "tmd2772", tmd2772 },
1947 { "apds9930", apds9930},
1951 MODULE_DEVICE_TABLE(i2c, tsl2772_idtable);
1953 static const struct of_device_id tsl2772_of_match[] = {
1954 { .compatible = "amstaos,tsl2571" },
1955 { .compatible = "amstaos,tsl2671" },
1956 { .compatible = "amstaos,tmd2671" },
1957 { .compatible = "amstaos,tsl2771" },
1958 { .compatible = "amstaos,tmd2771" },
1959 { .compatible = "amstaos,tsl2572" },
1960 { .compatible = "amstaos,tsl2672" },
1961 { .compatible = "amstaos,tmd2672" },
1962 { .compatible = "amstaos,tsl2772" },
1963 { .compatible = "amstaos,tmd2772" },
1964 { .compatible = "avago,apds9930" },
1967 MODULE_DEVICE_TABLE(of, tsl2772_of_match);
1969 static const struct dev_pm_ops tsl2772_pm_ops = {
1970 .suspend = tsl2772_suspend,
1971 .resume = tsl2772_resume,
1974 static struct i2c_driver tsl2772_driver = {
1977 .of_match_table = tsl2772_of_match,
1978 .pm = &tsl2772_pm_ops,
1980 .id_table = tsl2772_idtable,
1981 .probe = tsl2772_probe,
1982 .remove = tsl2772_remove,
1985 module_i2c_driver(tsl2772_driver);
1987 MODULE_AUTHOR("J. August Brenner <Jon.Brenner@ams.com>");
1988 MODULE_AUTHOR("Brian Masney <masneyb@onstation.org>");
1989 MODULE_DESCRIPTION("TAOS tsl2772 ambient and proximity light sensor driver");
1990 MODULE_LICENSE("GPL");