1 // SPDX-License-Identifier: GPL-2.0+
3 * Device driver for monitoring ambient light intensity in (lux) and proximity
4 * detection (prox) within the TAOS TSL2571, TSL2671, TMD2671, TSL2771, TMD2771,
5 * TSL2572, TSL2672, TMD2672, TSL2772, and TMD2772 devices.
7 * Copyright (c) 2012, TAOS Corporation.
8 * Copyright (c) 2017-2018 Brian Masney <masneyb@onstation.org>
11 #include <linux/delay.h>
12 #include <linux/errno.h>
13 #include <linux/i2c.h>
14 #include <linux/interrupt.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/property.h>
19 #include <linux/slab.h>
21 #include <linux/iio/events.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
24 #include <linux/platform_data/tsl2772.h>
25 #include <linux/regulator/consumer.h>
28 #define PROX_STAT_CAL 0
29 #define PROX_STAT_SAMP 1
30 #define MAX_SAMPLES_CAL 200
32 /* TSL2772 Device ID */
33 #define TRITON_ID 0x00
34 #define SWORDFISH_ID 0x30
35 #define HALIBUT_ID 0x20
37 /* Lux calculation constants */
38 #define TSL2772_LUX_CALC_OVER_FLOW 65535
41 * TAOS Register definitions - Note: depending on device, some of these register
42 * are not used and the register address is benign.
45 /* Register offsets */
46 #define TSL2772_MAX_CONFIG_REG 16
48 /* Device Registers and Masks */
49 #define TSL2772_CNTRL 0x00
50 #define TSL2772_ALS_TIME 0X01
51 #define TSL2772_PRX_TIME 0x02
52 #define TSL2772_WAIT_TIME 0x03
53 #define TSL2772_ALS_MINTHRESHLO 0X04
54 #define TSL2772_ALS_MINTHRESHHI 0X05
55 #define TSL2772_ALS_MAXTHRESHLO 0X06
56 #define TSL2772_ALS_MAXTHRESHHI 0X07
57 #define TSL2772_PRX_MINTHRESHLO 0X08
58 #define TSL2772_PRX_MINTHRESHHI 0X09
59 #define TSL2772_PRX_MAXTHRESHLO 0X0A
60 #define TSL2772_PRX_MAXTHRESHHI 0X0B
61 #define TSL2772_PERSISTENCE 0x0C
62 #define TSL2772_ALS_PRX_CONFIG 0x0D
63 #define TSL2772_PRX_COUNT 0x0E
64 #define TSL2772_GAIN 0x0F
65 #define TSL2772_NOTUSED 0x10
66 #define TSL2772_REVID 0x11
67 #define TSL2772_CHIPID 0x12
68 #define TSL2772_STATUS 0x13
69 #define TSL2772_ALS_CHAN0LO 0x14
70 #define TSL2772_ALS_CHAN0HI 0x15
71 #define TSL2772_ALS_CHAN1LO 0x16
72 #define TSL2772_ALS_CHAN1HI 0x17
73 #define TSL2772_PRX_LO 0x18
74 #define TSL2772_PRX_HI 0x19
76 /* tsl2772 cmd reg masks */
77 #define TSL2772_CMD_REG 0x80
78 #define TSL2772_CMD_SPL_FN 0x60
79 #define TSL2772_CMD_REPEAT_PROTO 0x00
80 #define TSL2772_CMD_AUTOINC_PROTO 0x20
82 #define TSL2772_CMD_PROX_INT_CLR 0X05
83 #define TSL2772_CMD_ALS_INT_CLR 0x06
84 #define TSL2772_CMD_PROXALS_INT_CLR 0X07
86 /* tsl2772 cntrl reg masks */
87 #define TSL2772_CNTL_ADC_ENBL 0x02
88 #define TSL2772_CNTL_PWR_ON 0x01
90 /* tsl2772 status reg masks */
91 #define TSL2772_STA_ADC_VALID 0x01
92 #define TSL2772_STA_PRX_VALID 0x02
93 #define TSL2772_STA_ADC_PRX_VALID (TSL2772_STA_ADC_VALID | \
94 TSL2772_STA_PRX_VALID)
95 #define TSL2772_STA_ALS_INTR 0x10
96 #define TSL2772_STA_PRX_INTR 0x20
98 /* tsl2772 cntrl reg masks */
99 #define TSL2772_CNTL_REG_CLEAR 0x00
100 #define TSL2772_CNTL_PROX_INT_ENBL 0X20
101 #define TSL2772_CNTL_ALS_INT_ENBL 0X10
102 #define TSL2772_CNTL_WAIT_TMR_ENBL 0X08
103 #define TSL2772_CNTL_PROX_DET_ENBL 0X04
104 #define TSL2772_CNTL_PWRON 0x01
105 #define TSL2772_CNTL_ALSPON_ENBL 0x03
106 #define TSL2772_CNTL_INTALSPON_ENBL 0x13
107 #define TSL2772_CNTL_PROXPON_ENBL 0x0F
108 #define TSL2772_CNTL_INTPROXPON_ENBL 0x2F
110 #define TSL2772_ALS_GAIN_TRIM_MIN 250
111 #define TSL2772_ALS_GAIN_TRIM_MAX 4000
113 #define TSL2772_MAX_PROX_LEDS 2
115 #define TSL2772_BOOT_MIN_SLEEP_TIME 10000
116 #define TSL2772_BOOT_MAX_SLEEP_TIME 28000
118 /* Device family members */
134 TSL2772_CHIP_UNKNOWN = 0,
135 TSL2772_CHIP_WORKING = 1,
136 TSL2772_CHIP_SUSPENDED = 2
140 TSL2772_SUPPLY_VDD = 0,
141 TSL2772_SUPPLY_VDDIO = 1,
142 TSL2772_NUM_SUPPLIES = 2
145 /* Per-device data */
146 struct tsl2772_als_info {
152 struct tsl2772_chip_info {
153 int chan_table_elements;
154 struct iio_chan_spec channel_with_events[4];
155 struct iio_chan_spec channel_without_events[4];
156 const struct iio_info *info;
159 static const int tsl2772_led_currents[][2] = {
160 { 100000, TSL2772_100_mA },
161 { 50000, TSL2772_50_mA },
162 { 25000, TSL2772_25_mA },
163 { 13000, TSL2772_13_mA },
167 struct tsl2772_chip {
169 struct mutex prox_mutex;
170 struct mutex als_mutex;
171 struct i2c_client *client;
172 struct regulator_bulk_data supplies[TSL2772_NUM_SUPPLIES];
174 struct tsl2772_als_info als_cur_info;
175 struct tsl2772_settings settings;
176 struct tsl2772_platform_data *pdata;
177 int als_gain_time_scale;
179 int tsl2772_chip_status;
180 u8 tsl2772_config[TSL2772_MAX_CONFIG_REG];
181 const struct tsl2772_chip_info *chip_info;
182 const struct iio_info *info;
185 * This structure is intentionally large to accommodate
187 * Sized to 9 = max 8 segments + 1 termination segment
189 struct tsl2772_lux tsl2772_device_lux[TSL2772_MAX_LUX_TABLE_SIZE];
193 * Different devices require different coefficents, and these numbers were
194 * derived from the 'Lux Equation' section of the various device datasheets.
195 * All of these coefficients assume a Glass Attenuation (GA) factor of 1.
196 * The coefficients are multiplied by 1000 to avoid floating point operations.
197 * The two rows in each table correspond to the Lux1 and Lux2 equations from
200 static const struct tsl2772_lux tsl2x71_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
206 static const struct tsl2772_lux tmd2x71_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
212 static const struct tsl2772_lux tsl2x72_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
218 static const struct tsl2772_lux tmd2x72_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
224 static const struct tsl2772_lux apds9930_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
230 static const struct tsl2772_lux *tsl2772_default_lux_table_group[] = {
231 [tsl2571] = tsl2x71_lux_table,
232 [tsl2671] = tsl2x71_lux_table,
233 [tmd2671] = tmd2x71_lux_table,
234 [tsl2771] = tsl2x71_lux_table,
235 [tmd2771] = tmd2x71_lux_table,
236 [tsl2572] = tsl2x72_lux_table,
237 [tsl2672] = tsl2x72_lux_table,
238 [tmd2672] = tmd2x72_lux_table,
239 [tsl2772] = tsl2x72_lux_table,
240 [tmd2772] = tmd2x72_lux_table,
241 [apds9930] = apds9930_lux_table,
244 static const struct tsl2772_settings tsl2772_default_settings = {
245 .als_time = 255, /* 2.72 / 2.73 ms */
247 .prox_time = 255, /* 2.72 / 2.73 ms */
250 .als_prox_config = 0,
251 .als_gain_trim = 1000,
252 .als_cal_target = 150,
253 .als_persistence = 1,
254 .als_interrupt_en = false,
255 .als_thresh_low = 200,
256 .als_thresh_high = 256,
257 .prox_persistence = 1,
258 .prox_interrupt_en = false,
260 .prox_thres_high = 512,
261 .prox_max_samples_cal = 30,
262 .prox_pulse_count = 8,
263 .prox_diode = TSL2772_DIODE1,
264 .prox_power = TSL2772_100_mA
267 static const s16 tsl2772_als_gain[] = {
274 static const s16 tsl2772_prox_gain[] = {
281 static const int tsl2772_int_time_avail[][6] = {
282 [tsl2571] = { 0, 2720, 0, 2720, 0, 696000 },
283 [tsl2671] = { 0, 2720, 0, 2720, 0, 696000 },
284 [tmd2671] = { 0, 2720, 0, 2720, 0, 696000 },
285 [tsl2771] = { 0, 2720, 0, 2720, 0, 696000 },
286 [tmd2771] = { 0, 2720, 0, 2720, 0, 696000 },
287 [tsl2572] = { 0, 2730, 0, 2730, 0, 699000 },
288 [tsl2672] = { 0, 2730, 0, 2730, 0, 699000 },
289 [tmd2672] = { 0, 2730, 0, 2730, 0, 699000 },
290 [tsl2772] = { 0, 2730, 0, 2730, 0, 699000 },
291 [tmd2772] = { 0, 2730, 0, 2730, 0, 699000 },
292 [apds9930] = { 0, 2730, 0, 2730, 0, 699000 },
295 static int tsl2772_int_calibscale_avail[] = { 1, 8, 16, 120 };
297 static int tsl2772_prox_calibscale_avail[] = { 1, 2, 4, 8 };
299 /* Channel variations */
308 static const u8 device_channel_config[] = {
319 [apds9930] = ALSPRX2,
322 static int tsl2772_read_status(struct tsl2772_chip *chip)
326 ret = i2c_smbus_read_byte_data(chip->client,
327 TSL2772_CMD_REG | TSL2772_STATUS);
329 dev_err(&chip->client->dev,
330 "%s: failed to read STATUS register: %d\n", __func__,
336 static int tsl2772_write_control_reg(struct tsl2772_chip *chip, u8 data)
340 ret = i2c_smbus_write_byte_data(chip->client,
341 TSL2772_CMD_REG | TSL2772_CNTRL, data);
343 dev_err(&chip->client->dev,
344 "%s: failed to write to control register %x: %d\n",
345 __func__, data, ret);
351 static int tsl2772_read_autoinc_regs(struct tsl2772_chip *chip, int lower_reg,
357 ret = i2c_smbus_write_byte(chip->client,
358 TSL2772_CMD_REG | TSL2772_CMD_AUTOINC_PROTO |
361 dev_err(&chip->client->dev,
362 "%s: failed to enable auto increment protocol: %d\n",
367 ret = i2c_smbus_read_byte_data(chip->client,
368 TSL2772_CMD_REG | lower_reg);
370 dev_err(&chip->client->dev,
371 "%s: failed to read from register %x: %d\n", __func__,
377 ret = i2c_smbus_read_byte_data(chip->client,
378 TSL2772_CMD_REG | upper_reg);
380 dev_err(&chip->client->dev,
381 "%s: failed to read from register %x: %d\n", __func__,
387 ret = i2c_smbus_write_byte(chip->client,
388 TSL2772_CMD_REG | TSL2772_CMD_REPEAT_PROTO |
391 dev_err(&chip->client->dev,
392 "%s: failed to enable repeated byte protocol: %d\n",
397 return le16_to_cpup((const __le16 *)&buf[0]);
401 * tsl2772_get_lux() - Reads and calculates current lux value.
402 * @indio_dev: pointer to IIO device
404 * The raw ch0 and ch1 values of the ambient light sensed in the last
405 * integration cycle are read from the device. The raw values are multiplied
406 * by a device-specific scale factor, and divided by the integration time and
407 * device gain. The code supports multiple lux equations through the lux table
408 * coefficients. A lux gain trim is applied to each lux equation, and then the
409 * maximum lux within the interval 0..65535 is selected.
411 static int tsl2772_get_lux(struct iio_dev *indio_dev)
413 struct tsl2772_chip *chip = iio_priv(indio_dev);
414 struct tsl2772_lux *p;
418 mutex_lock(&chip->als_mutex);
420 if (chip->tsl2772_chip_status != TSL2772_CHIP_WORKING) {
421 dev_err(&chip->client->dev, "%s: device is not enabled\n",
427 ret = tsl2772_read_status(chip);
431 if (!(ret & TSL2772_STA_ADC_VALID)) {
432 dev_err(&chip->client->dev,
433 "%s: data not valid yet\n", __func__);
434 ret = chip->als_cur_info.lux; /* return LAST VALUE */
438 ret = tsl2772_read_autoinc_regs(chip, TSL2772_ALS_CHAN0LO,
439 TSL2772_ALS_CHAN0HI);
442 chip->als_cur_info.als_ch0 = ret;
444 ret = tsl2772_read_autoinc_regs(chip, TSL2772_ALS_CHAN1LO,
445 TSL2772_ALS_CHAN1HI);
448 chip->als_cur_info.als_ch1 = ret;
450 if (chip->als_cur_info.als_ch0 >= chip->als_saturation) {
451 max_lux = TSL2772_LUX_CALC_OVER_FLOW;
452 goto update_struct_with_max_lux;
455 if (!chip->als_cur_info.als_ch0) {
456 /* have no data, so return LAST VALUE */
457 ret = chip->als_cur_info.lux;
463 for (p = (struct tsl2772_lux *)chip->tsl2772_device_lux; p->ch0 != 0;
467 lux = ((chip->als_cur_info.als_ch0 * p->ch0) -
468 (chip->als_cur_info.als_ch1 * p->ch1)) /
469 chip->als_gain_time_scale;
472 * The als_gain_trim can have a value within the range 250..4000
473 * and is a multiplier for the lux. A trim of 1000 makes no
474 * changes to the lux, less than 1000 scales it down, and
475 * greater than 1000 scales it up.
477 lux = (lux * chip->settings.als_gain_trim) / 1000;
479 if (lux > TSL2772_LUX_CALC_OVER_FLOW) {
484 max_lux = max(max_lux, lux);
487 if (overflow && max_lux == 0)
488 max_lux = TSL2772_LUX_CALC_OVER_FLOW;
490 update_struct_with_max_lux:
491 chip->als_cur_info.lux = max_lux;
495 mutex_unlock(&chip->als_mutex);
501 * tsl2772_get_prox() - Reads proximity data registers and updates
504 * @indio_dev: pointer to IIO device
506 static int tsl2772_get_prox(struct iio_dev *indio_dev)
508 struct tsl2772_chip *chip = iio_priv(indio_dev);
511 mutex_lock(&chip->prox_mutex);
513 ret = tsl2772_read_status(chip);
523 if (!(ret & TSL2772_STA_ADC_VALID)) {
534 if (!(ret & TSL2772_STA_PRX_VALID)) {
541 ret = tsl2772_read_autoinc_regs(chip, TSL2772_PRX_LO, TSL2772_PRX_HI);
544 chip->prox_data = ret;
547 mutex_unlock(&chip->prox_mutex);
552 static int tsl2772_read_prox_led_current(struct tsl2772_chip *chip)
554 struct device *dev = &chip->client->dev;
557 ret = device_property_read_u32(dev, "led-max-microamp", &tmp);
561 for (i = 0; tsl2772_led_currents[i][0] != 0; i++) {
562 if (tmp == tsl2772_led_currents[i][0]) {
563 chip->settings.prox_power = tsl2772_led_currents[i][1];
568 dev_err(dev, "Invalid value %d for led-max-microamp\n", tmp);
573 static int tsl2772_read_prox_diodes(struct tsl2772_chip *chip)
575 struct device *dev = &chip->client->dev;
576 int i, ret, num_leds, prox_diode_mask;
577 u32 leds[TSL2772_MAX_PROX_LEDS];
579 ret = device_property_count_u32(dev, "amstaos,proximity-diodes");
584 if (num_leds > TSL2772_MAX_PROX_LEDS)
585 num_leds = TSL2772_MAX_PROX_LEDS;
587 ret = device_property_read_u32_array(dev, "amstaos,proximity-diodes", leds, num_leds);
589 dev_err(dev, "Invalid value for amstaos,proximity-diodes: %d.\n", ret);
594 for (i = 0; i < num_leds; i++) {
596 prox_diode_mask |= TSL2772_DIODE0;
597 else if (leds[i] == 1)
598 prox_diode_mask |= TSL2772_DIODE1;
600 dev_err(dev, "Invalid value %d in amstaos,proximity-diodes.\n", leds[i]);
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_bulk_disable(ARRAY_SIZE(chip->supplies), chip->supplies);
703 static int tsl2772_chip_on(struct iio_dev *indio_dev)
705 struct tsl2772_chip *chip = iio_priv(indio_dev);
706 int ret, i, als_count, als_time_us;
707 u8 *dev_reg, reg_val;
709 /* Non calculated parameters */
710 chip->tsl2772_config[TSL2772_ALS_TIME] = chip->settings.als_time;
711 chip->tsl2772_config[TSL2772_PRX_TIME] = chip->settings.prox_time;
712 chip->tsl2772_config[TSL2772_WAIT_TIME] = chip->settings.wait_time;
713 chip->tsl2772_config[TSL2772_ALS_PRX_CONFIG] =
714 chip->settings.als_prox_config;
716 chip->tsl2772_config[TSL2772_ALS_MINTHRESHLO] =
717 (chip->settings.als_thresh_low) & 0xFF;
718 chip->tsl2772_config[TSL2772_ALS_MINTHRESHHI] =
719 (chip->settings.als_thresh_low >> 8) & 0xFF;
720 chip->tsl2772_config[TSL2772_ALS_MAXTHRESHLO] =
721 (chip->settings.als_thresh_high) & 0xFF;
722 chip->tsl2772_config[TSL2772_ALS_MAXTHRESHHI] =
723 (chip->settings.als_thresh_high >> 8) & 0xFF;
724 chip->tsl2772_config[TSL2772_PERSISTENCE] =
725 (chip->settings.prox_persistence & 0xFF) << 4 |
726 (chip->settings.als_persistence & 0xFF);
728 chip->tsl2772_config[TSL2772_PRX_COUNT] =
729 chip->settings.prox_pulse_count;
730 chip->tsl2772_config[TSL2772_PRX_MINTHRESHLO] =
731 (chip->settings.prox_thres_low) & 0xFF;
732 chip->tsl2772_config[TSL2772_PRX_MINTHRESHHI] =
733 (chip->settings.prox_thres_low >> 8) & 0xFF;
734 chip->tsl2772_config[TSL2772_PRX_MAXTHRESHLO] =
735 (chip->settings.prox_thres_high) & 0xFF;
736 chip->tsl2772_config[TSL2772_PRX_MAXTHRESHHI] =
737 (chip->settings.prox_thres_high >> 8) & 0xFF;
739 /* and make sure we're not already on */
740 if (chip->tsl2772_chip_status == TSL2772_CHIP_WORKING) {
741 /* if forcing a register update - turn off, then on */
742 dev_info(&chip->client->dev, "device is already enabled\n");
746 /* Set the gain based on tsl2772_settings struct */
747 chip->tsl2772_config[TSL2772_GAIN] =
748 (chip->settings.als_gain & 0xFF) |
749 ((chip->settings.prox_gain & 0xFF) << 2) |
750 (chip->settings.prox_diode << 4) |
751 (chip->settings.prox_power << 6);
753 /* set chip time scaling and saturation */
754 als_count = 256 - chip->settings.als_time;
755 als_time_us = als_count * tsl2772_int_time_avail[chip->id][3];
756 chip->als_saturation = als_count * 768; /* 75% of full scale */
757 chip->als_gain_time_scale = als_time_us *
758 tsl2772_als_gain[chip->settings.als_gain];
761 * TSL2772 Specific power-on / adc enable sequence
762 * Power on the device 1st.
764 ret = tsl2772_write_control_reg(chip, TSL2772_CNTL_PWR_ON);
769 * Use the following shadow copy for our delay before enabling ADC.
770 * Write all the registers.
772 for (i = 0, dev_reg = chip->tsl2772_config;
773 i < TSL2772_MAX_CONFIG_REG; i++) {
774 int reg = TSL2772_CMD_REG + i;
776 ret = i2c_smbus_write_byte_data(chip->client, reg,
779 dev_err(&chip->client->dev,
780 "%s: failed to write to register %x: %d\n",
786 /* Power-on settling time */
787 usleep_range(3000, 3500);
789 reg_val = TSL2772_CNTL_PWR_ON | TSL2772_CNTL_ADC_ENBL |
790 TSL2772_CNTL_PROX_DET_ENBL;
791 if (chip->settings.als_interrupt_en)
792 reg_val |= TSL2772_CNTL_ALS_INT_ENBL;
793 if (chip->settings.prox_interrupt_en)
794 reg_val |= TSL2772_CNTL_PROX_INT_ENBL;
796 ret = tsl2772_write_control_reg(chip, reg_val);
800 ret = i2c_smbus_write_byte(chip->client,
801 TSL2772_CMD_REG | TSL2772_CMD_SPL_FN |
802 TSL2772_CMD_PROXALS_INT_CLR);
804 dev_err(&chip->client->dev,
805 "%s: failed to clear interrupt status: %d\n",
810 chip->tsl2772_chip_status = TSL2772_CHIP_WORKING;
815 static int tsl2772_chip_off(struct iio_dev *indio_dev)
817 struct tsl2772_chip *chip = iio_priv(indio_dev);
819 /* turn device off */
820 chip->tsl2772_chip_status = TSL2772_CHIP_SUSPENDED;
821 return tsl2772_write_control_reg(chip, 0x00);
824 static void tsl2772_chip_off_action(void *data)
826 struct iio_dev *indio_dev = data;
828 tsl2772_chip_off(indio_dev);
832 * tsl2772_invoke_change - power cycle the device to implement the user
834 * @indio_dev: pointer to IIO device
836 * Obtain and lock both ALS and PROX resources, determine and save device state
837 * (On/Off), cycle device to implement updated parameter, put device back into
838 * proper state, and unlock resource.
840 static int tsl2772_invoke_change(struct iio_dev *indio_dev)
842 struct tsl2772_chip *chip = iio_priv(indio_dev);
843 int device_status = chip->tsl2772_chip_status;
846 mutex_lock(&chip->als_mutex);
847 mutex_lock(&chip->prox_mutex);
849 if (device_status == TSL2772_CHIP_WORKING) {
850 ret = tsl2772_chip_off(indio_dev);
855 ret = tsl2772_chip_on(indio_dev);
858 mutex_unlock(&chip->prox_mutex);
859 mutex_unlock(&chip->als_mutex);
864 static int tsl2772_prox_cal(struct iio_dev *indio_dev)
866 struct tsl2772_chip *chip = iio_priv(indio_dev);
867 int prox_history[MAX_SAMPLES_CAL + 1];
868 int i, ret, mean, max, sample_sum;
870 if (chip->settings.prox_max_samples_cal < 1 ||
871 chip->settings.prox_max_samples_cal > MAX_SAMPLES_CAL)
874 for (i = 0; i < chip->settings.prox_max_samples_cal; i++) {
875 usleep_range(15000, 17500);
876 ret = tsl2772_get_prox(indio_dev);
880 prox_history[i] = chip->prox_data;
885 for (i = 0; i < chip->settings.prox_max_samples_cal; i++) {
886 sample_sum += prox_history[i];
887 max = max(max, prox_history[i]);
889 mean = sample_sum / chip->settings.prox_max_samples_cal;
891 chip->settings.prox_thres_high = (max << 1) - mean;
893 return tsl2772_invoke_change(indio_dev);
896 static int tsl2772_read_avail(struct iio_dev *indio_dev,
897 struct iio_chan_spec const *chan,
898 const int **vals, int *type, int *length,
901 struct tsl2772_chip *chip = iio_priv(indio_dev);
904 case IIO_CHAN_INFO_CALIBSCALE:
905 if (chan->type == IIO_INTENSITY) {
906 *length = ARRAY_SIZE(tsl2772_int_calibscale_avail);
907 *vals = tsl2772_int_calibscale_avail;
909 *length = ARRAY_SIZE(tsl2772_prox_calibscale_avail);
910 *vals = tsl2772_prox_calibscale_avail;
913 return IIO_AVAIL_LIST;
914 case IIO_CHAN_INFO_INT_TIME:
915 *length = ARRAY_SIZE(tsl2772_int_time_avail[chip->id]);
916 *vals = tsl2772_int_time_avail[chip->id];
917 *type = IIO_VAL_INT_PLUS_MICRO;
918 return IIO_AVAIL_RANGE;
924 static ssize_t in_illuminance0_target_input_show(struct device *dev,
925 struct device_attribute *attr,
928 struct tsl2772_chip *chip = iio_priv(dev_to_iio_dev(dev));
930 return scnprintf(buf, PAGE_SIZE, "%d\n", chip->settings.als_cal_target);
933 static ssize_t in_illuminance0_target_input_store(struct device *dev,
934 struct device_attribute *attr,
935 const char *buf, size_t len)
937 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
938 struct tsl2772_chip *chip = iio_priv(indio_dev);
942 if (kstrtou16(buf, 0, &value))
945 chip->settings.als_cal_target = value;
946 ret = tsl2772_invoke_change(indio_dev);
953 static ssize_t in_illuminance0_calibrate_store(struct device *dev,
954 struct device_attribute *attr,
955 const char *buf, size_t len)
957 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
961 if (kstrtobool(buf, &value) || !value)
964 ret = tsl2772_als_calibrate(indio_dev);
968 ret = tsl2772_invoke_change(indio_dev);
975 static ssize_t in_illuminance0_lux_table_show(struct device *dev,
976 struct device_attribute *attr,
979 struct tsl2772_chip *chip = iio_priv(dev_to_iio_dev(dev));
983 while (i < TSL2772_MAX_LUX_TABLE_SIZE) {
984 offset += scnprintf(buf + offset, PAGE_SIZE - offset, "%u,%u,",
985 chip->tsl2772_device_lux[i].ch0,
986 chip->tsl2772_device_lux[i].ch1);
987 if (chip->tsl2772_device_lux[i].ch0 == 0) {
989 * We just printed the first "0" entry.
990 * Now get rid of the extra "," and break.
998 offset += scnprintf(buf + offset, PAGE_SIZE - offset, "\n");
1002 static ssize_t in_illuminance0_lux_table_store(struct device *dev,
1003 struct device_attribute *attr,
1004 const char *buf, size_t len)
1006 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1007 struct tsl2772_chip *chip = iio_priv(indio_dev);
1008 int value[ARRAY_SIZE(chip->tsl2772_device_lux) * 2 + 1];
1011 get_options(buf, ARRAY_SIZE(value), value);
1014 * We now have an array of ints starting at value[1], and
1015 * enumerated by value[0].
1016 * We expect each group of two ints to be one table entry,
1017 * and the last table entry is all 0.
1020 if ((n % 2) || n < 4 ||
1021 n > ((ARRAY_SIZE(chip->tsl2772_device_lux) - 1) * 2))
1024 if ((value[(n - 1)] | value[n]) != 0)
1027 if (chip->tsl2772_chip_status == TSL2772_CHIP_WORKING) {
1028 ret = tsl2772_chip_off(indio_dev);
1033 /* Zero out the table */
1034 memset(chip->tsl2772_device_lux, 0, sizeof(chip->tsl2772_device_lux));
1035 memcpy(chip->tsl2772_device_lux, &value[1], (value[0] * 4));
1037 ret = tsl2772_invoke_change(indio_dev);
1044 static ssize_t in_proximity0_calibrate_store(struct device *dev,
1045 struct device_attribute *attr,
1046 const char *buf, size_t len)
1048 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1052 if (kstrtobool(buf, &value) || !value)
1055 ret = tsl2772_prox_cal(indio_dev);
1059 ret = tsl2772_invoke_change(indio_dev);
1066 static int tsl2772_read_interrupt_config(struct iio_dev *indio_dev,
1067 const struct iio_chan_spec *chan,
1068 enum iio_event_type type,
1069 enum iio_event_direction dir)
1071 struct tsl2772_chip *chip = iio_priv(indio_dev);
1073 if (chan->type == IIO_INTENSITY)
1074 return chip->settings.als_interrupt_en;
1076 return chip->settings.prox_interrupt_en;
1079 static int tsl2772_write_interrupt_config(struct iio_dev *indio_dev,
1080 const struct iio_chan_spec *chan,
1081 enum iio_event_type type,
1082 enum iio_event_direction dir,
1085 struct tsl2772_chip *chip = iio_priv(indio_dev);
1087 if (chan->type == IIO_INTENSITY)
1088 chip->settings.als_interrupt_en = val ? true : false;
1090 chip->settings.prox_interrupt_en = val ? true : false;
1092 return tsl2772_invoke_change(indio_dev);
1095 static int tsl2772_write_event_value(struct iio_dev *indio_dev,
1096 const struct iio_chan_spec *chan,
1097 enum iio_event_type type,
1098 enum iio_event_direction dir,
1099 enum iio_event_info info,
1102 struct tsl2772_chip *chip = iio_priv(indio_dev);
1103 int ret = -EINVAL, count, persistence;
1107 case IIO_EV_INFO_VALUE:
1108 if (chan->type == IIO_INTENSITY) {
1110 case IIO_EV_DIR_RISING:
1111 chip->settings.als_thresh_high = val;
1114 case IIO_EV_DIR_FALLING:
1115 chip->settings.als_thresh_low = val;
1123 case IIO_EV_DIR_RISING:
1124 chip->settings.prox_thres_high = val;
1127 case IIO_EV_DIR_FALLING:
1128 chip->settings.prox_thres_low = val;
1136 case IIO_EV_INFO_PERIOD:
1137 if (chan->type == IIO_INTENSITY)
1138 time = chip->settings.als_time;
1140 time = chip->settings.prox_time;
1143 persistence = ((val * 1000000) + val2) /
1144 (count * tsl2772_int_time_avail[chip->id][3]);
1146 if (chan->type == IIO_INTENSITY) {
1147 /* ALS filter values are 1, 2, 3, 5, 10, 15, ..., 60 */
1148 if (persistence > 3)
1149 persistence = (persistence / 5) + 3;
1151 chip->settings.als_persistence = persistence;
1153 chip->settings.prox_persistence = persistence;
1165 return tsl2772_invoke_change(indio_dev);
1168 static int tsl2772_read_event_value(struct iio_dev *indio_dev,
1169 const struct iio_chan_spec *chan,
1170 enum iio_event_type type,
1171 enum iio_event_direction dir,
1172 enum iio_event_info info,
1173 int *val, int *val2)
1175 struct tsl2772_chip *chip = iio_priv(indio_dev);
1176 int filter_delay, persistence;
1180 case IIO_EV_INFO_VALUE:
1181 if (chan->type == IIO_INTENSITY) {
1183 case IIO_EV_DIR_RISING:
1184 *val = chip->settings.als_thresh_high;
1186 case IIO_EV_DIR_FALLING:
1187 *val = chip->settings.als_thresh_low;
1194 case IIO_EV_DIR_RISING:
1195 *val = chip->settings.prox_thres_high;
1197 case IIO_EV_DIR_FALLING:
1198 *val = chip->settings.prox_thres_low;
1205 case IIO_EV_INFO_PERIOD:
1206 if (chan->type == IIO_INTENSITY) {
1207 time = chip->settings.als_time;
1208 persistence = chip->settings.als_persistence;
1210 /* ALS filter values are 1, 2, 3, 5, 10, 15, ..., 60 */
1211 if (persistence > 3)
1212 persistence = (persistence - 3) * 5;
1214 time = chip->settings.prox_time;
1215 persistence = chip->settings.prox_persistence;
1218 filter_delay = persistence * (256 - time) *
1219 tsl2772_int_time_avail[chip->id][3];
1221 *val = filter_delay / 1000000;
1222 *val2 = filter_delay % 1000000;
1223 return IIO_VAL_INT_PLUS_MICRO;
1229 static int tsl2772_read_raw(struct iio_dev *indio_dev,
1230 struct iio_chan_spec const *chan,
1235 struct tsl2772_chip *chip = iio_priv(indio_dev);
1238 case IIO_CHAN_INFO_PROCESSED:
1239 switch (chan->type) {
1241 tsl2772_get_lux(indio_dev);
1242 *val = chip->als_cur_info.lux;
1247 case IIO_CHAN_INFO_RAW:
1248 switch (chan->type) {
1250 tsl2772_get_lux(indio_dev);
1251 if (chan->channel == 0)
1252 *val = chip->als_cur_info.als_ch0;
1254 *val = chip->als_cur_info.als_ch1;
1257 tsl2772_get_prox(indio_dev);
1258 *val = chip->prox_data;
1264 case IIO_CHAN_INFO_CALIBSCALE:
1265 if (chan->type == IIO_LIGHT)
1266 *val = tsl2772_als_gain[chip->settings.als_gain];
1268 *val = tsl2772_prox_gain[chip->settings.prox_gain];
1270 case IIO_CHAN_INFO_CALIBBIAS:
1271 *val = chip->settings.als_gain_trim;
1273 case IIO_CHAN_INFO_INT_TIME:
1275 *val2 = (256 - chip->settings.als_time) *
1276 tsl2772_int_time_avail[chip->id][3];
1277 return IIO_VAL_INT_PLUS_MICRO;
1283 static int tsl2772_write_raw(struct iio_dev *indio_dev,
1284 struct iio_chan_spec const *chan,
1289 struct tsl2772_chip *chip = iio_priv(indio_dev);
1292 case IIO_CHAN_INFO_CALIBSCALE:
1293 if (chan->type == IIO_INTENSITY) {
1296 chip->settings.als_gain = 0;
1299 chip->settings.als_gain = 1;
1302 chip->settings.als_gain = 2;
1305 chip->settings.als_gain = 3;
1313 chip->settings.prox_gain = 0;
1316 chip->settings.prox_gain = 1;
1319 chip->settings.prox_gain = 2;
1322 chip->settings.prox_gain = 3;
1329 case IIO_CHAN_INFO_CALIBBIAS:
1330 if (val < TSL2772_ALS_GAIN_TRIM_MIN ||
1331 val > TSL2772_ALS_GAIN_TRIM_MAX)
1334 chip->settings.als_gain_trim = val;
1336 case IIO_CHAN_INFO_INT_TIME:
1337 if (val != 0 || val2 < tsl2772_int_time_avail[chip->id][1] ||
1338 val2 > tsl2772_int_time_avail[chip->id][5])
1341 chip->settings.als_time = 256 -
1342 (val2 / tsl2772_int_time_avail[chip->id][3]);
1348 return tsl2772_invoke_change(indio_dev);
1351 static DEVICE_ATTR_RW(in_illuminance0_target_input);
1353 static DEVICE_ATTR_WO(in_illuminance0_calibrate);
1355 static DEVICE_ATTR_WO(in_proximity0_calibrate);
1357 static DEVICE_ATTR_RW(in_illuminance0_lux_table);
1359 /* Use the default register values to identify the Taos device */
1360 static int tsl2772_device_id_verif(int id, int target)
1366 return (id & 0xf0) == TRITON_ID;
1369 return (id & 0xf0) == HALIBUT_ID;
1376 return (id & 0xf0) == SWORDFISH_ID;
1382 static irqreturn_t tsl2772_event_handler(int irq, void *private)
1384 struct iio_dev *indio_dev = private;
1385 struct tsl2772_chip *chip = iio_priv(indio_dev);
1386 s64 timestamp = iio_get_time_ns(indio_dev);
1389 ret = tsl2772_read_status(chip);
1393 /* What type of interrupt do we need to process */
1394 if (ret & TSL2772_STA_PRX_INTR) {
1395 iio_push_event(indio_dev,
1396 IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY,
1403 if (ret & TSL2772_STA_ALS_INTR) {
1404 iio_push_event(indio_dev,
1405 IIO_UNMOD_EVENT_CODE(IIO_LIGHT,
1412 ret = i2c_smbus_write_byte(chip->client,
1413 TSL2772_CMD_REG | TSL2772_CMD_SPL_FN |
1414 TSL2772_CMD_PROXALS_INT_CLR);
1416 dev_err(&chip->client->dev,
1417 "%s: failed to clear interrupt status: %d\n",
1423 static struct attribute *tsl2772_ALS_device_attrs[] = {
1424 &dev_attr_in_illuminance0_target_input.attr,
1425 &dev_attr_in_illuminance0_calibrate.attr,
1426 &dev_attr_in_illuminance0_lux_table.attr,
1430 static struct attribute *tsl2772_PRX_device_attrs[] = {
1431 &dev_attr_in_proximity0_calibrate.attr,
1435 static struct attribute *tsl2772_ALSPRX_device_attrs[] = {
1436 &dev_attr_in_illuminance0_target_input.attr,
1437 &dev_attr_in_illuminance0_calibrate.attr,
1438 &dev_attr_in_illuminance0_lux_table.attr,
1442 static struct attribute *tsl2772_PRX2_device_attrs[] = {
1443 &dev_attr_in_proximity0_calibrate.attr,
1447 static struct attribute *tsl2772_ALSPRX2_device_attrs[] = {
1448 &dev_attr_in_illuminance0_target_input.attr,
1449 &dev_attr_in_illuminance0_calibrate.attr,
1450 &dev_attr_in_illuminance0_lux_table.attr,
1451 &dev_attr_in_proximity0_calibrate.attr,
1455 static const struct attribute_group tsl2772_device_attr_group_tbl[] = {
1457 .attrs = tsl2772_ALS_device_attrs,
1460 .attrs = tsl2772_PRX_device_attrs,
1463 .attrs = tsl2772_ALSPRX_device_attrs,
1466 .attrs = tsl2772_PRX2_device_attrs,
1469 .attrs = tsl2772_ALSPRX2_device_attrs,
1473 #define TSL2772_DEVICE_INFO(type)[type] = \
1475 .attrs = &tsl2772_device_attr_group_tbl[type], \
1476 .read_raw = &tsl2772_read_raw, \
1477 .read_avail = &tsl2772_read_avail, \
1478 .write_raw = &tsl2772_write_raw, \
1479 .read_event_value = &tsl2772_read_event_value, \
1480 .write_event_value = &tsl2772_write_event_value, \
1481 .read_event_config = &tsl2772_read_interrupt_config, \
1482 .write_event_config = &tsl2772_write_interrupt_config, \
1485 static const struct iio_info tsl2772_device_info[] = {
1486 TSL2772_DEVICE_INFO(ALS),
1487 TSL2772_DEVICE_INFO(PRX),
1488 TSL2772_DEVICE_INFO(ALSPRX),
1489 TSL2772_DEVICE_INFO(PRX2),
1490 TSL2772_DEVICE_INFO(ALSPRX2),
1493 static const struct iio_event_spec tsl2772_events[] = {
1495 .type = IIO_EV_TYPE_THRESH,
1496 .dir = IIO_EV_DIR_RISING,
1497 .mask_separate = BIT(IIO_EV_INFO_VALUE),
1499 .type = IIO_EV_TYPE_THRESH,
1500 .dir = IIO_EV_DIR_FALLING,
1501 .mask_separate = BIT(IIO_EV_INFO_VALUE),
1503 .type = IIO_EV_TYPE_THRESH,
1504 .dir = IIO_EV_DIR_EITHER,
1505 .mask_separate = BIT(IIO_EV_INFO_PERIOD) |
1506 BIT(IIO_EV_INFO_ENABLE),
1510 static const struct tsl2772_chip_info tsl2772_chip_info_tbl[] = {
1512 .channel_with_events = {
1517 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1519 .type = IIO_INTENSITY,
1522 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1523 BIT(IIO_CHAN_INFO_INT_TIME) |
1524 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1525 BIT(IIO_CHAN_INFO_CALIBBIAS),
1526 .info_mask_separate_available =
1527 BIT(IIO_CHAN_INFO_INT_TIME) |
1528 BIT(IIO_CHAN_INFO_CALIBSCALE),
1529 .event_spec = tsl2772_events,
1530 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1532 .type = IIO_INTENSITY,
1537 .channel_without_events = {
1542 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1544 .type = IIO_INTENSITY,
1547 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1548 BIT(IIO_CHAN_INFO_INT_TIME) |
1549 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1550 BIT(IIO_CHAN_INFO_CALIBBIAS),
1551 .info_mask_separate_available =
1552 BIT(IIO_CHAN_INFO_INT_TIME) |
1553 BIT(IIO_CHAN_INFO_CALIBSCALE),
1555 .type = IIO_INTENSITY,
1560 .chan_table_elements = 3,
1561 .info = &tsl2772_device_info[ALS],
1564 .channel_with_events = {
1566 .type = IIO_PROXIMITY,
1569 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1570 .event_spec = tsl2772_events,
1571 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1574 .channel_without_events = {
1576 .type = IIO_PROXIMITY,
1579 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1582 .chan_table_elements = 1,
1583 .info = &tsl2772_device_info[PRX],
1586 .channel_with_events = {
1591 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1593 .type = IIO_INTENSITY,
1596 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1597 BIT(IIO_CHAN_INFO_INT_TIME) |
1598 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1599 BIT(IIO_CHAN_INFO_CALIBBIAS),
1600 .info_mask_separate_available =
1601 BIT(IIO_CHAN_INFO_INT_TIME) |
1602 BIT(IIO_CHAN_INFO_CALIBSCALE),
1603 .event_spec = tsl2772_events,
1604 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1606 .type = IIO_INTENSITY,
1609 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1611 .type = IIO_PROXIMITY,
1614 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1615 .event_spec = tsl2772_events,
1616 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1619 .channel_without_events = {
1624 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1626 .type = IIO_INTENSITY,
1629 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1630 BIT(IIO_CHAN_INFO_INT_TIME) |
1631 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1632 BIT(IIO_CHAN_INFO_CALIBBIAS),
1633 .info_mask_separate_available =
1634 BIT(IIO_CHAN_INFO_INT_TIME) |
1635 BIT(IIO_CHAN_INFO_CALIBSCALE),
1637 .type = IIO_INTENSITY,
1640 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1642 .type = IIO_PROXIMITY,
1645 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1648 .chan_table_elements = 4,
1649 .info = &tsl2772_device_info[ALSPRX],
1652 .channel_with_events = {
1654 .type = IIO_PROXIMITY,
1657 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1658 BIT(IIO_CHAN_INFO_CALIBSCALE),
1659 .info_mask_separate_available =
1660 BIT(IIO_CHAN_INFO_CALIBSCALE),
1661 .event_spec = tsl2772_events,
1662 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1665 .channel_without_events = {
1667 .type = IIO_PROXIMITY,
1670 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1671 BIT(IIO_CHAN_INFO_CALIBSCALE),
1672 .info_mask_separate_available =
1673 BIT(IIO_CHAN_INFO_CALIBSCALE),
1676 .chan_table_elements = 1,
1677 .info = &tsl2772_device_info[PRX2],
1680 .channel_with_events = {
1685 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1687 .type = IIO_INTENSITY,
1690 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1691 BIT(IIO_CHAN_INFO_INT_TIME) |
1692 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1693 BIT(IIO_CHAN_INFO_CALIBBIAS),
1694 .info_mask_separate_available =
1695 BIT(IIO_CHAN_INFO_INT_TIME) |
1696 BIT(IIO_CHAN_INFO_CALIBSCALE),
1697 .event_spec = tsl2772_events,
1698 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1700 .type = IIO_INTENSITY,
1703 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1705 .type = IIO_PROXIMITY,
1708 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1709 BIT(IIO_CHAN_INFO_CALIBSCALE),
1710 .info_mask_separate_available =
1711 BIT(IIO_CHAN_INFO_CALIBSCALE),
1712 .event_spec = tsl2772_events,
1713 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1716 .channel_without_events = {
1721 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1723 .type = IIO_INTENSITY,
1726 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1727 BIT(IIO_CHAN_INFO_INT_TIME) |
1728 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1729 BIT(IIO_CHAN_INFO_CALIBBIAS),
1730 .info_mask_separate_available =
1731 BIT(IIO_CHAN_INFO_INT_TIME) |
1732 BIT(IIO_CHAN_INFO_CALIBSCALE),
1734 .type = IIO_INTENSITY,
1737 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1739 .type = IIO_PROXIMITY,
1742 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1743 BIT(IIO_CHAN_INFO_CALIBSCALE),
1744 .info_mask_separate_available =
1745 BIT(IIO_CHAN_INFO_CALIBSCALE),
1748 .chan_table_elements = 4,
1749 .info = &tsl2772_device_info[ALSPRX2],
1753 static int tsl2772_probe(struct i2c_client *clientp,
1754 const struct i2c_device_id *id)
1756 struct iio_dev *indio_dev;
1757 struct tsl2772_chip *chip;
1760 indio_dev = devm_iio_device_alloc(&clientp->dev, sizeof(*chip));
1764 chip = iio_priv(indio_dev);
1765 chip->client = clientp;
1766 i2c_set_clientdata(clientp, indio_dev);
1768 chip->supplies[TSL2772_SUPPLY_VDD].supply = "vdd";
1769 chip->supplies[TSL2772_SUPPLY_VDDIO].supply = "vddio";
1771 ret = devm_regulator_bulk_get(&clientp->dev,
1772 ARRAY_SIZE(chip->supplies),
1775 return dev_err_probe(&clientp->dev, ret, "Failed to get regulators\n");
1777 ret = regulator_bulk_enable(ARRAY_SIZE(chip->supplies), chip->supplies);
1779 dev_err(&clientp->dev, "Failed to enable regulators: %d\n",
1784 ret = devm_add_action_or_reset(&clientp->dev,
1785 tsl2772_disable_regulators_action,
1788 dev_err(&clientp->dev, "Failed to setup regulator cleanup action %d\n",
1793 usleep_range(TSL2772_BOOT_MIN_SLEEP_TIME, TSL2772_BOOT_MAX_SLEEP_TIME);
1795 ret = i2c_smbus_read_byte_data(chip->client,
1796 TSL2772_CMD_REG | TSL2772_CHIPID);
1800 if (tsl2772_device_id_verif(ret, id->driver_data) <= 0) {
1801 dev_info(&chip->client->dev,
1802 "%s: i2c device found does not match expected id\n",
1807 ret = i2c_smbus_write_byte(clientp, TSL2772_CMD_REG | TSL2772_CNTRL);
1809 dev_err(&clientp->dev,
1810 "%s: Failed to write to CMD register: %d\n",
1815 mutex_init(&chip->als_mutex);
1816 mutex_init(&chip->prox_mutex);
1818 chip->tsl2772_chip_status = TSL2772_CHIP_UNKNOWN;
1819 chip->pdata = dev_get_platdata(&clientp->dev);
1820 chip->id = id->driver_data;
1822 &tsl2772_chip_info_tbl[device_channel_config[id->driver_data]];
1824 indio_dev->info = chip->chip_info->info;
1825 indio_dev->modes = INDIO_DIRECT_MODE;
1826 indio_dev->name = chip->client->name;
1827 indio_dev->num_channels = chip->chip_info->chan_table_elements;
1830 indio_dev->channels = chip->chip_info->channel_with_events;
1832 ret = devm_request_threaded_irq(&clientp->dev, clientp->irq,
1834 &tsl2772_event_handler,
1835 IRQF_TRIGGER_FALLING |
1840 dev_err(&clientp->dev,
1841 "%s: irq request failed\n", __func__);
1845 indio_dev->channels = chip->chip_info->channel_without_events;
1848 tsl2772_defaults(chip);
1849 ret = tsl2772_chip_on(indio_dev);
1853 ret = devm_add_action_or_reset(&clientp->dev,
1854 tsl2772_chip_off_action,
1859 return devm_iio_device_register(&clientp->dev, indio_dev);
1862 static int tsl2772_suspend(struct device *dev)
1864 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1865 struct tsl2772_chip *chip = iio_priv(indio_dev);
1868 ret = tsl2772_chip_off(indio_dev);
1869 regulator_bulk_disable(ARRAY_SIZE(chip->supplies), chip->supplies);
1874 static int tsl2772_resume(struct device *dev)
1876 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1877 struct tsl2772_chip *chip = iio_priv(indio_dev);
1880 ret = regulator_bulk_enable(ARRAY_SIZE(chip->supplies), chip->supplies);
1884 usleep_range(TSL2772_BOOT_MIN_SLEEP_TIME, TSL2772_BOOT_MAX_SLEEP_TIME);
1886 return tsl2772_chip_on(indio_dev);
1889 static const struct i2c_device_id tsl2772_idtable[] = {
1890 { "tsl2571", tsl2571 },
1891 { "tsl2671", tsl2671 },
1892 { "tmd2671", tmd2671 },
1893 { "tsl2771", tsl2771 },
1894 { "tmd2771", tmd2771 },
1895 { "tsl2572", tsl2572 },
1896 { "tsl2672", tsl2672 },
1897 { "tmd2672", tmd2672 },
1898 { "tsl2772", tsl2772 },
1899 { "tmd2772", tmd2772 },
1900 { "apds9930", apds9930 },
1904 MODULE_DEVICE_TABLE(i2c, tsl2772_idtable);
1906 static const struct of_device_id tsl2772_of_match[] = {
1907 { .compatible = "amstaos,tsl2571" },
1908 { .compatible = "amstaos,tsl2671" },
1909 { .compatible = "amstaos,tmd2671" },
1910 { .compatible = "amstaos,tsl2771" },
1911 { .compatible = "amstaos,tmd2771" },
1912 { .compatible = "amstaos,tsl2572" },
1913 { .compatible = "amstaos,tsl2672" },
1914 { .compatible = "amstaos,tmd2672" },
1915 { .compatible = "amstaos,tsl2772" },
1916 { .compatible = "amstaos,tmd2772" },
1917 { .compatible = "avago,apds9930" },
1920 MODULE_DEVICE_TABLE(of, tsl2772_of_match);
1922 static const struct dev_pm_ops tsl2772_pm_ops = {
1923 .suspend = tsl2772_suspend,
1924 .resume = tsl2772_resume,
1927 static struct i2c_driver tsl2772_driver = {
1930 .of_match_table = tsl2772_of_match,
1931 .pm = &tsl2772_pm_ops,
1933 .id_table = tsl2772_idtable,
1934 .probe = tsl2772_probe,
1937 module_i2c_driver(tsl2772_driver);
1939 MODULE_AUTHOR("J. August Brenner <Jon.Brenner@ams.com>");
1940 MODULE_AUTHOR("Brian Masney <masneyb@onstation.org>");
1941 MODULE_DESCRIPTION("TAOS tsl2772 ambient and proximity light sensor driver");
1942 MODULE_LICENSE("GPL");