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);
863 static void tsl2772_chip_off_action(void *data)
865 struct iio_dev *indio_dev = data;
867 tsl2772_chip_off(indio_dev);
871 * tsl2772_invoke_change - power cycle the device to implement the user
873 * @indio_dev: pointer to IIO device
875 * Obtain and lock both ALS and PROX resources, determine and save device state
876 * (On/Off), cycle device to implement updated parameter, put device back into
877 * proper state, and unlock resource.
879 static int tsl2772_invoke_change(struct iio_dev *indio_dev)
881 struct tsl2772_chip *chip = iio_priv(indio_dev);
882 int device_status = chip->tsl2772_chip_status;
885 mutex_lock(&chip->als_mutex);
886 mutex_lock(&chip->prox_mutex);
888 if (device_status == TSL2772_CHIP_WORKING) {
889 ret = tsl2772_chip_off(indio_dev);
894 ret = tsl2772_chip_on(indio_dev);
897 mutex_unlock(&chip->prox_mutex);
898 mutex_unlock(&chip->als_mutex);
903 static int tsl2772_prox_cal(struct iio_dev *indio_dev)
905 struct tsl2772_chip *chip = iio_priv(indio_dev);
906 int prox_history[MAX_SAMPLES_CAL + 1];
907 int i, ret, mean, max, sample_sum;
909 if (chip->settings.prox_max_samples_cal < 1 ||
910 chip->settings.prox_max_samples_cal > MAX_SAMPLES_CAL)
913 for (i = 0; i < chip->settings.prox_max_samples_cal; i++) {
914 usleep_range(15000, 17500);
915 ret = tsl2772_get_prox(indio_dev);
919 prox_history[i] = chip->prox_data;
924 for (i = 0; i < chip->settings.prox_max_samples_cal; i++) {
925 sample_sum += prox_history[i];
926 max = max(max, prox_history[i]);
928 mean = sample_sum / chip->settings.prox_max_samples_cal;
930 chip->settings.prox_thres_high = (max << 1) - mean;
932 return tsl2772_invoke_change(indio_dev);
935 static int tsl2772_read_avail(struct iio_dev *indio_dev,
936 struct iio_chan_spec const *chan,
937 const int **vals, int *type, int *length,
940 struct tsl2772_chip *chip = iio_priv(indio_dev);
943 case IIO_CHAN_INFO_CALIBSCALE:
944 if (chan->type == IIO_INTENSITY) {
945 *length = ARRAY_SIZE(tsl2772_int_calibscale_avail);
946 *vals = tsl2772_int_calibscale_avail;
948 *length = ARRAY_SIZE(tsl2772_prox_calibscale_avail);
949 *vals = tsl2772_prox_calibscale_avail;
952 return IIO_AVAIL_LIST;
953 case IIO_CHAN_INFO_INT_TIME:
954 *length = ARRAY_SIZE(tsl2772_int_time_avail[chip->id]);
955 *vals = tsl2772_int_time_avail[chip->id];
956 *type = IIO_VAL_INT_PLUS_MICRO;
957 return IIO_AVAIL_RANGE;
963 static ssize_t in_illuminance0_target_input_show(struct device *dev,
964 struct device_attribute *attr,
967 struct tsl2772_chip *chip = iio_priv(dev_to_iio_dev(dev));
969 return snprintf(buf, PAGE_SIZE, "%d\n", chip->settings.als_cal_target);
972 static ssize_t in_illuminance0_target_input_store(struct device *dev,
973 struct device_attribute *attr,
974 const char *buf, size_t len)
976 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
977 struct tsl2772_chip *chip = iio_priv(indio_dev);
981 if (kstrtou16(buf, 0, &value))
984 chip->settings.als_cal_target = value;
985 ret = tsl2772_invoke_change(indio_dev);
992 static ssize_t in_illuminance0_calibrate_store(struct device *dev,
993 struct device_attribute *attr,
994 const char *buf, size_t len)
996 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1000 if (kstrtobool(buf, &value) || !value)
1003 ret = tsl2772_als_calibrate(indio_dev);
1007 ret = tsl2772_invoke_change(indio_dev);
1014 static ssize_t in_illuminance0_lux_table_show(struct device *dev,
1015 struct device_attribute *attr,
1018 struct tsl2772_chip *chip = iio_priv(dev_to_iio_dev(dev));
1022 while (i < TSL2772_MAX_LUX_TABLE_SIZE) {
1023 offset += snprintf(buf + offset, PAGE_SIZE, "%u,%u,",
1024 chip->tsl2772_device_lux[i].ch0,
1025 chip->tsl2772_device_lux[i].ch1);
1026 if (chip->tsl2772_device_lux[i].ch0 == 0) {
1028 * We just printed the first "0" entry.
1029 * Now get rid of the extra "," and break.
1037 offset += snprintf(buf + offset, PAGE_SIZE, "\n");
1041 static ssize_t in_illuminance0_lux_table_store(struct device *dev,
1042 struct device_attribute *attr,
1043 const char *buf, size_t len)
1045 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1046 struct tsl2772_chip *chip = iio_priv(indio_dev);
1047 int value[ARRAY_SIZE(chip->tsl2772_device_lux) * 2 + 1];
1050 get_options(buf, ARRAY_SIZE(value), value);
1053 * We now have an array of ints starting at value[1], and
1054 * enumerated by value[0].
1055 * We expect each group of two ints to be one table entry,
1056 * and the last table entry is all 0.
1059 if ((n % 2) || n < 4 ||
1060 n > ((ARRAY_SIZE(chip->tsl2772_device_lux) - 1) * 2))
1063 if ((value[(n - 1)] | value[n]) != 0)
1066 if (chip->tsl2772_chip_status == TSL2772_CHIP_WORKING) {
1067 ret = tsl2772_chip_off(indio_dev);
1072 /* Zero out the table */
1073 memset(chip->tsl2772_device_lux, 0, sizeof(chip->tsl2772_device_lux));
1074 memcpy(chip->tsl2772_device_lux, &value[1], (value[0] * 4));
1076 ret = tsl2772_invoke_change(indio_dev);
1083 static ssize_t in_proximity0_calibrate_store(struct device *dev,
1084 struct device_attribute *attr,
1085 const char *buf, size_t len)
1087 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1091 if (kstrtobool(buf, &value) || !value)
1094 ret = tsl2772_prox_cal(indio_dev);
1098 ret = tsl2772_invoke_change(indio_dev);
1105 static int tsl2772_read_interrupt_config(struct iio_dev *indio_dev,
1106 const struct iio_chan_spec *chan,
1107 enum iio_event_type type,
1108 enum iio_event_direction dir)
1110 struct tsl2772_chip *chip = iio_priv(indio_dev);
1112 if (chan->type == IIO_INTENSITY)
1113 return chip->settings.als_interrupt_en;
1115 return chip->settings.prox_interrupt_en;
1118 static int tsl2772_write_interrupt_config(struct iio_dev *indio_dev,
1119 const struct iio_chan_spec *chan,
1120 enum iio_event_type type,
1121 enum iio_event_direction dir,
1124 struct tsl2772_chip *chip = iio_priv(indio_dev);
1126 if (chan->type == IIO_INTENSITY)
1127 chip->settings.als_interrupt_en = val ? true : false;
1129 chip->settings.prox_interrupt_en = val ? true : false;
1131 return tsl2772_invoke_change(indio_dev);
1134 static int tsl2772_write_event_value(struct iio_dev *indio_dev,
1135 const struct iio_chan_spec *chan,
1136 enum iio_event_type type,
1137 enum iio_event_direction dir,
1138 enum iio_event_info info,
1141 struct tsl2772_chip *chip = iio_priv(indio_dev);
1142 int ret = -EINVAL, count, persistence;
1146 case IIO_EV_INFO_VALUE:
1147 if (chan->type == IIO_INTENSITY) {
1149 case IIO_EV_DIR_RISING:
1150 chip->settings.als_thresh_high = val;
1153 case IIO_EV_DIR_FALLING:
1154 chip->settings.als_thresh_low = val;
1162 case IIO_EV_DIR_RISING:
1163 chip->settings.prox_thres_high = val;
1166 case IIO_EV_DIR_FALLING:
1167 chip->settings.prox_thres_low = val;
1175 case IIO_EV_INFO_PERIOD:
1176 if (chan->type == IIO_INTENSITY)
1177 time = chip->settings.als_time;
1179 time = chip->settings.prox_time;
1182 persistence = ((val * 1000000) + val2) /
1183 (count * tsl2772_int_time_avail[chip->id][3]);
1185 if (chan->type == IIO_INTENSITY) {
1186 /* ALS filter values are 1, 2, 3, 5, 10, 15, ..., 60 */
1187 if (persistence > 3)
1188 persistence = (persistence / 5) + 3;
1190 chip->settings.als_persistence = persistence;
1192 chip->settings.prox_persistence = persistence;
1204 return tsl2772_invoke_change(indio_dev);
1207 static int tsl2772_read_event_value(struct iio_dev *indio_dev,
1208 const struct iio_chan_spec *chan,
1209 enum iio_event_type type,
1210 enum iio_event_direction dir,
1211 enum iio_event_info info,
1212 int *val, int *val2)
1214 struct tsl2772_chip *chip = iio_priv(indio_dev);
1215 int filter_delay, persistence;
1219 case IIO_EV_INFO_VALUE:
1220 if (chan->type == IIO_INTENSITY) {
1222 case IIO_EV_DIR_RISING:
1223 *val = chip->settings.als_thresh_high;
1225 case IIO_EV_DIR_FALLING:
1226 *val = chip->settings.als_thresh_low;
1233 case IIO_EV_DIR_RISING:
1234 *val = chip->settings.prox_thres_high;
1236 case IIO_EV_DIR_FALLING:
1237 *val = chip->settings.prox_thres_low;
1244 case IIO_EV_INFO_PERIOD:
1245 if (chan->type == IIO_INTENSITY) {
1246 time = chip->settings.als_time;
1247 persistence = chip->settings.als_persistence;
1249 /* ALS filter values are 1, 2, 3, 5, 10, 15, ..., 60 */
1250 if (persistence > 3)
1251 persistence = (persistence - 3) * 5;
1253 time = chip->settings.prox_time;
1254 persistence = chip->settings.prox_persistence;
1257 filter_delay = persistence * (256 - time) *
1258 tsl2772_int_time_avail[chip->id][3];
1260 *val = filter_delay / 1000000;
1261 *val2 = filter_delay % 1000000;
1262 return IIO_VAL_INT_PLUS_MICRO;
1268 static int tsl2772_read_raw(struct iio_dev *indio_dev,
1269 struct iio_chan_spec const *chan,
1274 struct tsl2772_chip *chip = iio_priv(indio_dev);
1277 case IIO_CHAN_INFO_PROCESSED:
1278 switch (chan->type) {
1280 tsl2772_get_lux(indio_dev);
1281 *val = chip->als_cur_info.lux;
1286 case IIO_CHAN_INFO_RAW:
1287 switch (chan->type) {
1289 tsl2772_get_lux(indio_dev);
1290 if (chan->channel == 0)
1291 *val = chip->als_cur_info.als_ch0;
1293 *val = chip->als_cur_info.als_ch1;
1296 tsl2772_get_prox(indio_dev);
1297 *val = chip->prox_data;
1303 case IIO_CHAN_INFO_CALIBSCALE:
1304 if (chan->type == IIO_LIGHT)
1305 *val = tsl2772_als_gain[chip->settings.als_gain];
1307 *val = tsl2772_prox_gain[chip->settings.prox_gain];
1309 case IIO_CHAN_INFO_CALIBBIAS:
1310 *val = chip->settings.als_gain_trim;
1312 case IIO_CHAN_INFO_INT_TIME:
1314 *val2 = (256 - chip->settings.als_time) *
1315 tsl2772_int_time_avail[chip->id][3];
1316 return IIO_VAL_INT_PLUS_MICRO;
1322 static int tsl2772_write_raw(struct iio_dev *indio_dev,
1323 struct iio_chan_spec const *chan,
1328 struct tsl2772_chip *chip = iio_priv(indio_dev);
1331 case IIO_CHAN_INFO_CALIBSCALE:
1332 if (chan->type == IIO_INTENSITY) {
1335 chip->settings.als_gain = 0;
1338 chip->settings.als_gain = 1;
1341 chip->settings.als_gain = 2;
1344 chip->settings.als_gain = 3;
1352 chip->settings.prox_gain = 0;
1355 chip->settings.prox_gain = 1;
1358 chip->settings.prox_gain = 2;
1361 chip->settings.prox_gain = 3;
1368 case IIO_CHAN_INFO_CALIBBIAS:
1369 if (val < TSL2772_ALS_GAIN_TRIM_MIN ||
1370 val > TSL2772_ALS_GAIN_TRIM_MAX)
1373 chip->settings.als_gain_trim = val;
1375 case IIO_CHAN_INFO_INT_TIME:
1376 if (val != 0 || val2 < tsl2772_int_time_avail[chip->id][1] ||
1377 val2 > tsl2772_int_time_avail[chip->id][5])
1380 chip->settings.als_time = 256 -
1381 (val2 / tsl2772_int_time_avail[chip->id][3]);
1387 return tsl2772_invoke_change(indio_dev);
1390 static DEVICE_ATTR_RW(in_illuminance0_target_input);
1392 static DEVICE_ATTR_WO(in_illuminance0_calibrate);
1394 static DEVICE_ATTR_WO(in_proximity0_calibrate);
1396 static DEVICE_ATTR_RW(in_illuminance0_lux_table);
1398 /* Use the default register values to identify the Taos device */
1399 static int tsl2772_device_id_verif(int id, int target)
1405 return (id & 0xf0) == TRITON_ID;
1408 return (id & 0xf0) == HALIBUT_ID;
1415 return (id & 0xf0) == SWORDFISH_ID;
1421 static irqreturn_t tsl2772_event_handler(int irq, void *private)
1423 struct iio_dev *indio_dev = private;
1424 struct tsl2772_chip *chip = iio_priv(indio_dev);
1425 s64 timestamp = iio_get_time_ns(indio_dev);
1428 ret = tsl2772_read_status(chip);
1432 /* What type of interrupt do we need to process */
1433 if (ret & TSL2772_STA_PRX_INTR) {
1434 iio_push_event(indio_dev,
1435 IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY,
1442 if (ret & TSL2772_STA_ALS_INTR) {
1443 iio_push_event(indio_dev,
1444 IIO_UNMOD_EVENT_CODE(IIO_LIGHT,
1451 ret = i2c_smbus_write_byte(chip->client,
1452 TSL2772_CMD_REG | TSL2772_CMD_SPL_FN |
1453 TSL2772_CMD_PROXALS_INT_CLR);
1455 dev_err(&chip->client->dev,
1456 "%s: failed to clear interrupt status: %d\n",
1462 static struct attribute *tsl2772_ALS_device_attrs[] = {
1463 &dev_attr_in_illuminance0_target_input.attr,
1464 &dev_attr_in_illuminance0_calibrate.attr,
1465 &dev_attr_in_illuminance0_lux_table.attr,
1469 static struct attribute *tsl2772_PRX_device_attrs[] = {
1470 &dev_attr_in_proximity0_calibrate.attr,
1474 static struct attribute *tsl2772_ALSPRX_device_attrs[] = {
1475 &dev_attr_in_illuminance0_target_input.attr,
1476 &dev_attr_in_illuminance0_calibrate.attr,
1477 &dev_attr_in_illuminance0_lux_table.attr,
1481 static struct attribute *tsl2772_PRX2_device_attrs[] = {
1482 &dev_attr_in_proximity0_calibrate.attr,
1486 static struct attribute *tsl2772_ALSPRX2_device_attrs[] = {
1487 &dev_attr_in_illuminance0_target_input.attr,
1488 &dev_attr_in_illuminance0_calibrate.attr,
1489 &dev_attr_in_illuminance0_lux_table.attr,
1490 &dev_attr_in_proximity0_calibrate.attr,
1494 static const struct attribute_group tsl2772_device_attr_group_tbl[] = {
1496 .attrs = tsl2772_ALS_device_attrs,
1499 .attrs = tsl2772_PRX_device_attrs,
1502 .attrs = tsl2772_ALSPRX_device_attrs,
1505 .attrs = tsl2772_PRX2_device_attrs,
1508 .attrs = tsl2772_ALSPRX2_device_attrs,
1512 #define TSL2772_DEVICE_INFO(type)[type] = \
1514 .attrs = &tsl2772_device_attr_group_tbl[type], \
1515 .read_raw = &tsl2772_read_raw, \
1516 .read_avail = &tsl2772_read_avail, \
1517 .write_raw = &tsl2772_write_raw, \
1518 .read_event_value = &tsl2772_read_event_value, \
1519 .write_event_value = &tsl2772_write_event_value, \
1520 .read_event_config = &tsl2772_read_interrupt_config, \
1521 .write_event_config = &tsl2772_write_interrupt_config, \
1524 static const struct iio_info tsl2772_device_info[] = {
1525 TSL2772_DEVICE_INFO(ALS),
1526 TSL2772_DEVICE_INFO(PRX),
1527 TSL2772_DEVICE_INFO(ALSPRX),
1528 TSL2772_DEVICE_INFO(PRX2),
1529 TSL2772_DEVICE_INFO(ALSPRX2),
1532 static const struct iio_event_spec tsl2772_events[] = {
1534 .type = IIO_EV_TYPE_THRESH,
1535 .dir = IIO_EV_DIR_RISING,
1536 .mask_separate = BIT(IIO_EV_INFO_VALUE),
1538 .type = IIO_EV_TYPE_THRESH,
1539 .dir = IIO_EV_DIR_FALLING,
1540 .mask_separate = BIT(IIO_EV_INFO_VALUE),
1542 .type = IIO_EV_TYPE_THRESH,
1543 .dir = IIO_EV_DIR_EITHER,
1544 .mask_separate = BIT(IIO_EV_INFO_PERIOD) |
1545 BIT(IIO_EV_INFO_ENABLE),
1549 static const struct tsl2772_chip_info tsl2772_chip_info_tbl[] = {
1551 .channel_with_events = {
1556 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1558 .type = IIO_INTENSITY,
1561 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1562 BIT(IIO_CHAN_INFO_INT_TIME) |
1563 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1564 BIT(IIO_CHAN_INFO_CALIBBIAS),
1565 .info_mask_separate_available =
1566 BIT(IIO_CHAN_INFO_INT_TIME) |
1567 BIT(IIO_CHAN_INFO_CALIBSCALE),
1568 .event_spec = tsl2772_events,
1569 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1571 .type = IIO_INTENSITY,
1576 .channel_without_events = {
1581 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1583 .type = IIO_INTENSITY,
1586 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1587 BIT(IIO_CHAN_INFO_INT_TIME) |
1588 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1589 BIT(IIO_CHAN_INFO_CALIBBIAS),
1590 .info_mask_separate_available =
1591 BIT(IIO_CHAN_INFO_INT_TIME) |
1592 BIT(IIO_CHAN_INFO_CALIBSCALE),
1594 .type = IIO_INTENSITY,
1599 .chan_table_elements = 3,
1600 .info = &tsl2772_device_info[ALS],
1603 .channel_with_events = {
1605 .type = IIO_PROXIMITY,
1608 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1609 .event_spec = tsl2772_events,
1610 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1613 .channel_without_events = {
1615 .type = IIO_PROXIMITY,
1618 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1621 .chan_table_elements = 1,
1622 .info = &tsl2772_device_info[PRX],
1625 .channel_with_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),
1642 .event_spec = tsl2772_events,
1643 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1645 .type = IIO_INTENSITY,
1648 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1650 .type = IIO_PROXIMITY,
1653 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1654 .event_spec = tsl2772_events,
1655 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1658 .channel_without_events = {
1663 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1665 .type = IIO_INTENSITY,
1668 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1669 BIT(IIO_CHAN_INFO_INT_TIME) |
1670 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1671 BIT(IIO_CHAN_INFO_CALIBBIAS),
1672 .info_mask_separate_available =
1673 BIT(IIO_CHAN_INFO_INT_TIME) |
1674 BIT(IIO_CHAN_INFO_CALIBSCALE),
1676 .type = IIO_INTENSITY,
1679 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1681 .type = IIO_PROXIMITY,
1684 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1687 .chan_table_elements = 4,
1688 .info = &tsl2772_device_info[ALSPRX],
1691 .channel_with_events = {
1693 .type = IIO_PROXIMITY,
1696 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1697 BIT(IIO_CHAN_INFO_CALIBSCALE),
1698 .info_mask_separate_available =
1699 BIT(IIO_CHAN_INFO_CALIBSCALE),
1700 .event_spec = tsl2772_events,
1701 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1704 .channel_without_events = {
1706 .type = IIO_PROXIMITY,
1709 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1710 BIT(IIO_CHAN_INFO_CALIBSCALE),
1711 .info_mask_separate_available =
1712 BIT(IIO_CHAN_INFO_CALIBSCALE),
1715 .chan_table_elements = 1,
1716 .info = &tsl2772_device_info[PRX2],
1719 .channel_with_events = {
1724 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1726 .type = IIO_INTENSITY,
1729 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1730 BIT(IIO_CHAN_INFO_INT_TIME) |
1731 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1732 BIT(IIO_CHAN_INFO_CALIBBIAS),
1733 .info_mask_separate_available =
1734 BIT(IIO_CHAN_INFO_INT_TIME) |
1735 BIT(IIO_CHAN_INFO_CALIBSCALE),
1736 .event_spec = tsl2772_events,
1737 .num_event_specs = ARRAY_SIZE(tsl2772_events),
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),
1751 .event_spec = tsl2772_events,
1752 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1755 .channel_without_events = {
1760 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1762 .type = IIO_INTENSITY,
1765 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1766 BIT(IIO_CHAN_INFO_INT_TIME) |
1767 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1768 BIT(IIO_CHAN_INFO_CALIBBIAS),
1769 .info_mask_separate_available =
1770 BIT(IIO_CHAN_INFO_INT_TIME) |
1771 BIT(IIO_CHAN_INFO_CALIBSCALE),
1773 .type = IIO_INTENSITY,
1776 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1778 .type = IIO_PROXIMITY,
1781 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1782 BIT(IIO_CHAN_INFO_CALIBSCALE),
1783 .info_mask_separate_available =
1784 BIT(IIO_CHAN_INFO_CALIBSCALE),
1787 .chan_table_elements = 4,
1788 .info = &tsl2772_device_info[ALSPRX2],
1792 static int tsl2772_probe(struct i2c_client *clientp,
1793 const struct i2c_device_id *id)
1795 struct iio_dev *indio_dev;
1796 struct tsl2772_chip *chip;
1799 indio_dev = devm_iio_device_alloc(&clientp->dev, sizeof(*chip));
1803 chip = iio_priv(indio_dev);
1804 chip->client = clientp;
1805 i2c_set_clientdata(clientp, indio_dev);
1807 chip->vddio_supply = tsl2772_get_regulator(chip, "vddio");
1808 if (IS_ERR(chip->vddio_supply))
1809 return PTR_ERR(chip->vddio_supply);
1811 chip->vdd_supply = tsl2772_get_regulator(chip, "vdd");
1812 if (IS_ERR(chip->vdd_supply)) {
1813 regulator_disable(chip->vddio_supply);
1814 return PTR_ERR(chip->vdd_supply);
1817 ret = devm_add_action_or_reset(&clientp->dev,
1818 tsl2772_disable_regulators_action,
1821 dev_err(&clientp->dev, "Failed to setup regulator cleanup action %d\n",
1826 usleep_range(TSL2772_BOOT_MIN_SLEEP_TIME, TSL2772_BOOT_MAX_SLEEP_TIME);
1828 ret = i2c_smbus_read_byte_data(chip->client,
1829 TSL2772_CMD_REG | TSL2772_CHIPID);
1833 if (tsl2772_device_id_verif(ret, id->driver_data) <= 0) {
1834 dev_info(&chip->client->dev,
1835 "%s: i2c device found does not match expected id\n",
1840 ret = i2c_smbus_write_byte(clientp, TSL2772_CMD_REG | TSL2772_CNTRL);
1842 dev_err(&clientp->dev,
1843 "%s: Failed to write to CMD register: %d\n",
1848 mutex_init(&chip->als_mutex);
1849 mutex_init(&chip->prox_mutex);
1851 chip->tsl2772_chip_status = TSL2772_CHIP_UNKNOWN;
1852 chip->pdata = dev_get_platdata(&clientp->dev);
1853 chip->id = id->driver_data;
1855 &tsl2772_chip_info_tbl[device_channel_config[id->driver_data]];
1857 indio_dev->info = chip->chip_info->info;
1858 indio_dev->dev.parent = &clientp->dev;
1859 indio_dev->modes = INDIO_DIRECT_MODE;
1860 indio_dev->name = chip->client->name;
1861 indio_dev->num_channels = chip->chip_info->chan_table_elements;
1864 indio_dev->channels = chip->chip_info->channel_with_events;
1866 ret = devm_request_threaded_irq(&clientp->dev, clientp->irq,
1868 &tsl2772_event_handler,
1869 IRQF_TRIGGER_FALLING |
1874 dev_err(&clientp->dev,
1875 "%s: irq request failed\n", __func__);
1879 indio_dev->channels = chip->chip_info->channel_without_events;
1882 tsl2772_defaults(chip);
1883 ret = tsl2772_chip_on(indio_dev);
1887 ret = devm_add_action_or_reset(&clientp->dev,
1888 tsl2772_chip_off_action,
1893 ret = iio_device_register(indio_dev);
1895 dev_err(&clientp->dev,
1896 "%s: iio registration failed\n", __func__);
1903 static int tsl2772_suspend(struct device *dev)
1905 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1906 struct tsl2772_chip *chip = iio_priv(indio_dev);
1909 ret = tsl2772_chip_off(indio_dev);
1910 regulator_disable(chip->vdd_supply);
1911 regulator_disable(chip->vddio_supply);
1916 static int tsl2772_resume(struct device *dev)
1918 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1919 struct tsl2772_chip *chip = iio_priv(indio_dev);
1922 ret = tsl2772_enable_regulator(chip, chip->vddio_supply);
1926 ret = tsl2772_enable_regulator(chip, chip->vdd_supply);
1928 regulator_disable(chip->vddio_supply);
1932 usleep_range(TSL2772_BOOT_MIN_SLEEP_TIME, TSL2772_BOOT_MAX_SLEEP_TIME);
1934 return tsl2772_chip_on(indio_dev);
1937 static int tsl2772_remove(struct i2c_client *client)
1939 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1941 iio_device_unregister(indio_dev);
1946 static const struct i2c_device_id tsl2772_idtable[] = {
1947 { "tsl2571", tsl2571 },
1948 { "tsl2671", tsl2671 },
1949 { "tmd2671", tmd2671 },
1950 { "tsl2771", tsl2771 },
1951 { "tmd2771", tmd2771 },
1952 { "tsl2572", tsl2572 },
1953 { "tsl2672", tsl2672 },
1954 { "tmd2672", tmd2672 },
1955 { "tsl2772", tsl2772 },
1956 { "tmd2772", tmd2772 },
1957 { "apds9930", apds9930},
1961 MODULE_DEVICE_TABLE(i2c, tsl2772_idtable);
1963 static const struct of_device_id tsl2772_of_match[] = {
1964 { .compatible = "amstaos,tsl2571" },
1965 { .compatible = "amstaos,tsl2671" },
1966 { .compatible = "amstaos,tmd2671" },
1967 { .compatible = "amstaos,tsl2771" },
1968 { .compatible = "amstaos,tmd2771" },
1969 { .compatible = "amstaos,tsl2572" },
1970 { .compatible = "amstaos,tsl2672" },
1971 { .compatible = "amstaos,tmd2672" },
1972 { .compatible = "amstaos,tsl2772" },
1973 { .compatible = "amstaos,tmd2772" },
1974 { .compatible = "avago,apds9930" },
1977 MODULE_DEVICE_TABLE(of, tsl2772_of_match);
1979 static const struct dev_pm_ops tsl2772_pm_ops = {
1980 .suspend = tsl2772_suspend,
1981 .resume = tsl2772_resume,
1984 static struct i2c_driver tsl2772_driver = {
1987 .of_match_table = tsl2772_of_match,
1988 .pm = &tsl2772_pm_ops,
1990 .id_table = tsl2772_idtable,
1991 .probe = tsl2772_probe,
1992 .remove = tsl2772_remove,
1995 module_i2c_driver(tsl2772_driver);
1997 MODULE_AUTHOR("J. August Brenner <Jon.Brenner@ams.com>");
1998 MODULE_AUTHOR("Brian Masney <masneyb@onstation.org>");
1999 MODULE_DESCRIPTION("TAOS tsl2772 ambient and proximity light sensor driver");
2000 MODULE_LICENSE("GPL");