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>
25 #define PROX_STAT_CAL 0
26 #define PROX_STAT_SAMP 1
27 #define MAX_SAMPLES_CAL 200
29 /* TSL2772 Device ID */
30 #define TRITON_ID 0x00
31 #define SWORDFISH_ID 0x30
32 #define HALIBUT_ID 0x20
34 /* Lux calculation constants */
35 #define TSL2772_LUX_CALC_OVER_FLOW 65535
38 * TAOS Register definitions - Note: depending on device, some of these register
39 * are not used and the register address is benign.
42 /* Register offsets */
43 #define TSL2772_MAX_CONFIG_REG 16
45 /* Device Registers and Masks */
46 #define TSL2772_CNTRL 0x00
47 #define TSL2772_ALS_TIME 0X01
48 #define TSL2772_PRX_TIME 0x02
49 #define TSL2772_WAIT_TIME 0x03
50 #define TSL2772_ALS_MINTHRESHLO 0X04
51 #define TSL2772_ALS_MINTHRESHHI 0X05
52 #define TSL2772_ALS_MAXTHRESHLO 0X06
53 #define TSL2772_ALS_MAXTHRESHHI 0X07
54 #define TSL2772_PRX_MINTHRESHLO 0X08
55 #define TSL2772_PRX_MINTHRESHHI 0X09
56 #define TSL2772_PRX_MAXTHRESHLO 0X0A
57 #define TSL2772_PRX_MAXTHRESHHI 0X0B
58 #define TSL2772_PERSISTENCE 0x0C
59 #define TSL2772_ALS_PRX_CONFIG 0x0D
60 #define TSL2772_PRX_COUNT 0x0E
61 #define TSL2772_GAIN 0x0F
62 #define TSL2772_NOTUSED 0x10
63 #define TSL2772_REVID 0x11
64 #define TSL2772_CHIPID 0x12
65 #define TSL2772_STATUS 0x13
66 #define TSL2772_ALS_CHAN0LO 0x14
67 #define TSL2772_ALS_CHAN0HI 0x15
68 #define TSL2772_ALS_CHAN1LO 0x16
69 #define TSL2772_ALS_CHAN1HI 0x17
70 #define TSL2772_PRX_LO 0x18
71 #define TSL2772_PRX_HI 0x19
73 /* tsl2772 cmd reg masks */
74 #define TSL2772_CMD_REG 0x80
75 #define TSL2772_CMD_SPL_FN 0x60
76 #define TSL2772_CMD_REPEAT_PROTO 0x00
77 #define TSL2772_CMD_AUTOINC_PROTO 0x20
79 #define TSL2772_CMD_PROX_INT_CLR 0X05
80 #define TSL2772_CMD_ALS_INT_CLR 0x06
81 #define TSL2772_CMD_PROXALS_INT_CLR 0X07
83 /* tsl2772 cntrl reg masks */
84 #define TSL2772_CNTL_ADC_ENBL 0x02
85 #define TSL2772_CNTL_PWR_ON 0x01
87 /* tsl2772 status reg masks */
88 #define TSL2772_STA_ADC_VALID 0x01
89 #define TSL2772_STA_PRX_VALID 0x02
90 #define TSL2772_STA_ADC_PRX_VALID (TSL2772_STA_ADC_VALID | \
91 TSL2772_STA_PRX_VALID)
92 #define TSL2772_STA_ALS_INTR 0x10
93 #define TSL2772_STA_PRX_INTR 0x20
95 /* tsl2772 cntrl reg masks */
96 #define TSL2772_CNTL_REG_CLEAR 0x00
97 #define TSL2772_CNTL_PROX_INT_ENBL 0X20
98 #define TSL2772_CNTL_ALS_INT_ENBL 0X10
99 #define TSL2772_CNTL_WAIT_TMR_ENBL 0X08
100 #define TSL2772_CNTL_PROX_DET_ENBL 0X04
101 #define TSL2772_CNTL_PWRON 0x01
102 #define TSL2772_CNTL_ALSPON_ENBL 0x03
103 #define TSL2772_CNTL_INTALSPON_ENBL 0x13
104 #define TSL2772_CNTL_PROXPON_ENBL 0x0F
105 #define TSL2772_CNTL_INTPROXPON_ENBL 0x2F
107 #define TSL2772_ALS_GAIN_TRIM_MIN 250
108 #define TSL2772_ALS_GAIN_TRIM_MAX 4000
110 /* Device family members */
125 TSL2772_CHIP_UNKNOWN = 0,
126 TSL2772_CHIP_WORKING = 1,
127 TSL2772_CHIP_SUSPENDED = 2
130 /* Per-device data */
131 struct tsl2772_als_info {
137 struct tsl2772_chip_info {
138 int chan_table_elements;
139 struct iio_chan_spec channel_with_events[4];
140 struct iio_chan_spec channel_without_events[4];
141 const struct iio_info *info;
144 struct tsl2772_chip {
146 struct mutex prox_mutex;
147 struct mutex als_mutex;
148 struct i2c_client *client;
150 struct tsl2772_als_info als_cur_info;
151 struct tsl2772_settings settings;
152 struct tsl2772_platform_data *pdata;
153 int als_gain_time_scale;
155 int tsl2772_chip_status;
156 u8 tsl2772_config[TSL2772_MAX_CONFIG_REG];
157 const struct tsl2772_chip_info *chip_info;
158 const struct iio_info *info;
161 * This structure is intentionally large to accommodate
163 * Sized to 9 = max 8 segments + 1 termination segment
165 struct tsl2772_lux tsl2772_device_lux[TSL2772_MAX_LUX_TABLE_SIZE];
169 * Different devices require different coefficents, and these numbers were
170 * derived from the 'Lux Equation' section of the various device datasheets.
171 * All of these coefficients assume a Glass Attenuation (GA) factor of 1.
172 * The coefficients are multiplied by 1000 to avoid floating point operations.
173 * The two rows in each table correspond to the Lux1 and Lux2 equations from
176 static const struct tsl2772_lux tsl2x71_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
182 static const struct tsl2772_lux tmd2x71_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
188 static const struct tsl2772_lux tsl2x72_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
194 static const struct tsl2772_lux tmd2x72_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
200 static const struct tsl2772_lux *tsl2772_default_lux_table_group[] = {
201 [tsl2571] = tsl2x71_lux_table,
202 [tsl2671] = tsl2x71_lux_table,
203 [tmd2671] = tmd2x71_lux_table,
204 [tsl2771] = tsl2x71_lux_table,
205 [tmd2771] = tmd2x71_lux_table,
206 [tsl2572] = tsl2x72_lux_table,
207 [tsl2672] = tsl2x72_lux_table,
208 [tmd2672] = tmd2x72_lux_table,
209 [tsl2772] = tsl2x72_lux_table,
210 [tmd2772] = tmd2x72_lux_table,
213 static const struct tsl2772_settings tsl2772_default_settings = {
214 .als_time = 255, /* 2.72 / 2.73 ms */
216 .prox_time = 255, /* 2.72 / 2.73 ms */
219 .als_prox_config = 0,
220 .als_gain_trim = 1000,
221 .als_cal_target = 150,
222 .als_persistence = 1,
223 .als_interrupt_en = false,
224 .als_thresh_low = 200,
225 .als_thresh_high = 256,
226 .prox_persistence = 1,
227 .prox_interrupt_en = false,
229 .prox_thres_high = 512,
230 .prox_max_samples_cal = 30,
231 .prox_pulse_count = 8,
232 .prox_diode = TSL2772_DIODE1,
233 .prox_power = TSL2772_100_mA
236 static const s16 tsl2772_als_gain[] = {
243 static const s16 tsl2772_prox_gain[] = {
250 static const int tsl2772_int_time_avail[][6] = {
251 [tsl2571] = { 0, 2720, 0, 2720, 0, 696000 },
252 [tsl2671] = { 0, 2720, 0, 2720, 0, 696000 },
253 [tmd2671] = { 0, 2720, 0, 2720, 0, 696000 },
254 [tsl2771] = { 0, 2720, 0, 2720, 0, 696000 },
255 [tmd2771] = { 0, 2720, 0, 2720, 0, 696000 },
256 [tsl2572] = { 0, 2730, 0, 2730, 0, 699000 },
257 [tsl2672] = { 0, 2730, 0, 2730, 0, 699000 },
258 [tmd2672] = { 0, 2730, 0, 2730, 0, 699000 },
259 [tsl2772] = { 0, 2730, 0, 2730, 0, 699000 },
260 [tmd2772] = { 0, 2730, 0, 2730, 0, 699000 },
263 static int tsl2772_int_calibscale_avail[] = { 1, 8, 16, 120 };
265 static int tsl2772_prox_calibscale_avail[] = { 1, 2, 4, 8 };
267 /* Channel variations */
276 static const u8 device_channel_config[] = {
289 static int tsl2772_read_status(struct tsl2772_chip *chip)
293 ret = i2c_smbus_read_byte_data(chip->client,
294 TSL2772_CMD_REG | TSL2772_STATUS);
296 dev_err(&chip->client->dev,
297 "%s: failed to read STATUS register: %d\n", __func__,
303 static int tsl2772_write_control_reg(struct tsl2772_chip *chip, u8 data)
307 ret = i2c_smbus_write_byte_data(chip->client,
308 TSL2772_CMD_REG | TSL2772_CNTRL, data);
310 dev_err(&chip->client->dev,
311 "%s: failed to write to control register %x: %d\n",
312 __func__, data, ret);
318 static int tsl2772_read_autoinc_regs(struct tsl2772_chip *chip, int lower_reg,
324 ret = i2c_smbus_write_byte(chip->client,
325 TSL2772_CMD_REG | TSL2772_CMD_AUTOINC_PROTO |
328 dev_err(&chip->client->dev,
329 "%s: failed to enable auto increment protocol: %d\n",
334 ret = i2c_smbus_read_byte_data(chip->client,
335 TSL2772_CMD_REG | lower_reg);
337 dev_err(&chip->client->dev,
338 "%s: failed to read from register %x: %d\n", __func__,
344 ret = i2c_smbus_read_byte_data(chip->client,
345 TSL2772_CMD_REG | upper_reg);
347 dev_err(&chip->client->dev,
348 "%s: failed to read from register %x: %d\n", __func__,
354 ret = i2c_smbus_write_byte(chip->client,
355 TSL2772_CMD_REG | TSL2772_CMD_REPEAT_PROTO |
358 dev_err(&chip->client->dev,
359 "%s: failed to enable repeated byte protocol: %d\n",
364 return le16_to_cpup((const __le16 *)&buf[0]);
368 * tsl2772_get_lux() - Reads and calculates current lux value.
369 * @indio_dev: pointer to IIO device
371 * The raw ch0 and ch1 values of the ambient light sensed in the last
372 * integration cycle are read from the device. The raw values are multiplied
373 * by a device-specific scale factor, and divided by the integration time and
374 * device gain. The code supports multiple lux equations through the lux table
375 * coefficients. A lux gain trim is applied to each lux equation, and then the
376 * maximum lux within the interval 0..65535 is selected.
378 static int tsl2772_get_lux(struct iio_dev *indio_dev)
380 struct tsl2772_chip *chip = iio_priv(indio_dev);
381 struct tsl2772_lux *p;
385 mutex_lock(&chip->als_mutex);
387 if (chip->tsl2772_chip_status != TSL2772_CHIP_WORKING) {
388 dev_err(&chip->client->dev, "%s: device is not enabled\n",
394 ret = tsl2772_read_status(chip);
398 if (!(ret & TSL2772_STA_ADC_VALID)) {
399 dev_err(&chip->client->dev,
400 "%s: data not valid yet\n", __func__);
401 ret = chip->als_cur_info.lux; /* return LAST VALUE */
405 ret = tsl2772_read_autoinc_regs(chip, TSL2772_ALS_CHAN0LO,
406 TSL2772_ALS_CHAN0HI);
409 chip->als_cur_info.als_ch0 = ret;
411 ret = tsl2772_read_autoinc_regs(chip, TSL2772_ALS_CHAN1LO,
412 TSL2772_ALS_CHAN1HI);
415 chip->als_cur_info.als_ch1 = ret;
417 if (chip->als_cur_info.als_ch0 >= chip->als_saturation) {
418 max_lux = TSL2772_LUX_CALC_OVER_FLOW;
419 goto update_struct_with_max_lux;
422 if (!chip->als_cur_info.als_ch0) {
423 /* have no data, so return LAST VALUE */
424 ret = chip->als_cur_info.lux;
430 for (p = (struct tsl2772_lux *)chip->tsl2772_device_lux; p->ch0 != 0;
434 lux = ((chip->als_cur_info.als_ch0 * p->ch0) -
435 (chip->als_cur_info.als_ch1 * p->ch1)) /
436 chip->als_gain_time_scale;
439 * The als_gain_trim can have a value within the range 250..4000
440 * and is a multiplier for the lux. A trim of 1000 makes no
441 * changes to the lux, less than 1000 scales it down, and
442 * greater than 1000 scales it up.
444 lux = (lux * chip->settings.als_gain_trim) / 1000;
446 if (lux > TSL2772_LUX_CALC_OVER_FLOW) {
451 max_lux = max(max_lux, lux);
454 if (overflow && max_lux == 0)
455 max_lux = TSL2772_LUX_CALC_OVER_FLOW;
457 update_struct_with_max_lux:
458 chip->als_cur_info.lux = max_lux;
462 mutex_unlock(&chip->als_mutex);
468 * tsl2772_get_prox() - Reads proximity data registers and updates
471 * @indio_dev: pointer to IIO device
473 static int tsl2772_get_prox(struct iio_dev *indio_dev)
475 struct tsl2772_chip *chip = iio_priv(indio_dev);
478 mutex_lock(&chip->prox_mutex);
480 ret = tsl2772_read_status(chip);
490 if (!(ret & TSL2772_STA_ADC_VALID)) {
500 if (!(ret & TSL2772_STA_PRX_VALID)) {
507 ret = tsl2772_read_autoinc_regs(chip, TSL2772_PRX_LO, TSL2772_PRX_HI);
510 chip->prox_data = ret;
513 mutex_unlock(&chip->prox_mutex);
519 * tsl2772_defaults() - Populates the device nominal operating parameters
520 * with those provided by a 'platform' data struct or
521 * with prefined defaults.
523 * @chip: pointer to device structure.
525 static void tsl2772_defaults(struct tsl2772_chip *chip)
527 /* If Operational settings defined elsewhere.. */
528 if (chip->pdata && chip->pdata->platform_default_settings)
529 memcpy(&chip->settings, chip->pdata->platform_default_settings,
530 sizeof(tsl2772_default_settings));
532 memcpy(&chip->settings, &tsl2772_default_settings,
533 sizeof(tsl2772_default_settings));
535 /* Load up the proper lux table. */
536 if (chip->pdata && chip->pdata->platform_lux_table[0].ch0 != 0)
537 memcpy(chip->tsl2772_device_lux,
538 chip->pdata->platform_lux_table,
539 sizeof(chip->pdata->platform_lux_table));
541 memcpy(chip->tsl2772_device_lux,
542 tsl2772_default_lux_table_group[chip->id],
543 TSL2772_DEFAULT_TABLE_BYTES);
547 * tsl2772_als_calibrate() - Obtain single reading and calculate
550 * @indio_dev: pointer to IIO device
552 static int tsl2772_als_calibrate(struct iio_dev *indio_dev)
554 struct tsl2772_chip *chip = iio_priv(indio_dev);
557 ret = i2c_smbus_read_byte_data(chip->client,
558 TSL2772_CMD_REG | TSL2772_CNTRL);
560 dev_err(&chip->client->dev,
561 "%s: failed to read from the CNTRL register\n",
566 if ((ret & (TSL2772_CNTL_ADC_ENBL | TSL2772_CNTL_PWR_ON))
567 != (TSL2772_CNTL_ADC_ENBL | TSL2772_CNTL_PWR_ON)) {
568 dev_err(&chip->client->dev,
569 "%s: Device is not powered on and/or ADC is not enabled\n",
572 } else if ((ret & TSL2772_STA_ADC_VALID) != TSL2772_STA_ADC_VALID) {
573 dev_err(&chip->client->dev,
574 "%s: The two ADC channels have not completed an integration cycle\n",
579 lux_val = tsl2772_get_lux(indio_dev);
581 dev_err(&chip->client->dev,
582 "%s: failed to get lux\n", __func__);
586 ret = (chip->settings.als_cal_target * chip->settings.als_gain_trim) /
588 if (ret < TSL2772_ALS_GAIN_TRIM_MIN || ret > TSL2772_ALS_GAIN_TRIM_MAX)
591 chip->settings.als_gain_trim = ret;
596 static int tsl2772_chip_on(struct iio_dev *indio_dev)
598 struct tsl2772_chip *chip = iio_priv(indio_dev);
599 int ret, i, als_count, als_time_us;
600 u8 *dev_reg, reg_val;
602 /* Non calculated parameters */
603 chip->tsl2772_config[TSL2772_ALS_TIME] = chip->settings.als_time;
604 chip->tsl2772_config[TSL2772_PRX_TIME] = chip->settings.prox_time;
605 chip->tsl2772_config[TSL2772_WAIT_TIME] = chip->settings.wait_time;
606 chip->tsl2772_config[TSL2772_ALS_PRX_CONFIG] =
607 chip->settings.als_prox_config;
609 chip->tsl2772_config[TSL2772_ALS_MINTHRESHLO] =
610 (chip->settings.als_thresh_low) & 0xFF;
611 chip->tsl2772_config[TSL2772_ALS_MINTHRESHHI] =
612 (chip->settings.als_thresh_low >> 8) & 0xFF;
613 chip->tsl2772_config[TSL2772_ALS_MAXTHRESHLO] =
614 (chip->settings.als_thresh_high) & 0xFF;
615 chip->tsl2772_config[TSL2772_ALS_MAXTHRESHHI] =
616 (chip->settings.als_thresh_high >> 8) & 0xFF;
617 chip->tsl2772_config[TSL2772_PERSISTENCE] =
618 (chip->settings.prox_persistence & 0xFF) << 4 |
619 (chip->settings.als_persistence & 0xFF);
621 chip->tsl2772_config[TSL2772_PRX_COUNT] =
622 chip->settings.prox_pulse_count;
623 chip->tsl2772_config[TSL2772_PRX_MINTHRESHLO] =
624 (chip->settings.prox_thres_low) & 0xFF;
625 chip->tsl2772_config[TSL2772_PRX_MINTHRESHHI] =
626 (chip->settings.prox_thres_low >> 8) & 0xFF;
627 chip->tsl2772_config[TSL2772_PRX_MAXTHRESHLO] =
628 (chip->settings.prox_thres_high) & 0xFF;
629 chip->tsl2772_config[TSL2772_PRX_MAXTHRESHHI] =
630 (chip->settings.prox_thres_high >> 8) & 0xFF;
632 /* and make sure we're not already on */
633 if (chip->tsl2772_chip_status == TSL2772_CHIP_WORKING) {
634 /* if forcing a register update - turn off, then on */
635 dev_info(&chip->client->dev, "device is already enabled\n");
639 /* Set the gain based on tsl2772_settings struct */
640 chip->tsl2772_config[TSL2772_GAIN] =
641 (chip->settings.als_gain & 0xFF) |
642 ((chip->settings.prox_gain & 0xFF) << 2) |
643 (chip->settings.prox_diode << 4) |
644 (chip->settings.prox_power << 6);
646 /* set chip time scaling and saturation */
647 als_count = 256 - chip->settings.als_time;
648 als_time_us = als_count * tsl2772_int_time_avail[chip->id][3];
649 chip->als_saturation = als_count * 768; /* 75% of full scale */
650 chip->als_gain_time_scale = als_time_us *
651 tsl2772_als_gain[chip->settings.als_gain];
654 * TSL2772 Specific power-on / adc enable sequence
655 * Power on the device 1st.
657 ret = tsl2772_write_control_reg(chip, TSL2772_CNTL_PWR_ON);
662 * Use the following shadow copy for our delay before enabling ADC.
663 * Write all the registers.
665 for (i = 0, dev_reg = chip->tsl2772_config;
666 i < TSL2772_MAX_CONFIG_REG; i++) {
667 int reg = TSL2772_CMD_REG + i;
669 ret = i2c_smbus_write_byte_data(chip->client, reg,
672 dev_err(&chip->client->dev,
673 "%s: failed to write to register %x: %d\n",
679 /* Power-on settling time */
680 usleep_range(3000, 3500);
682 reg_val = TSL2772_CNTL_PWR_ON | TSL2772_CNTL_ADC_ENBL |
683 TSL2772_CNTL_PROX_DET_ENBL;
684 if (chip->settings.als_interrupt_en)
685 reg_val |= TSL2772_CNTL_ALS_INT_ENBL;
686 if (chip->settings.prox_interrupt_en)
687 reg_val |= TSL2772_CNTL_PROX_INT_ENBL;
689 ret = tsl2772_write_control_reg(chip, reg_val);
693 ret = i2c_smbus_write_byte(chip->client,
694 TSL2772_CMD_REG | TSL2772_CMD_SPL_FN |
695 TSL2772_CMD_PROXALS_INT_CLR);
697 dev_err(&chip->client->dev,
698 "%s: failed to clear interrupt status: %d\n",
703 chip->tsl2772_chip_status = TSL2772_CHIP_WORKING;
708 static int tsl2772_chip_off(struct iio_dev *indio_dev)
710 struct tsl2772_chip *chip = iio_priv(indio_dev);
712 /* turn device off */
713 chip->tsl2772_chip_status = TSL2772_CHIP_SUSPENDED;
714 return tsl2772_write_control_reg(chip, 0x00);
718 * tsl2772_invoke_change - power cycle the device to implement the user
720 * @indio_dev: pointer to IIO device
722 * Obtain and lock both ALS and PROX resources, determine and save device state
723 * (On/Off), cycle device to implement updated parameter, put device back into
724 * proper state, and unlock resource.
726 static int tsl2772_invoke_change(struct iio_dev *indio_dev)
728 struct tsl2772_chip *chip = iio_priv(indio_dev);
729 int device_status = chip->tsl2772_chip_status;
732 mutex_lock(&chip->als_mutex);
733 mutex_lock(&chip->prox_mutex);
735 if (device_status == TSL2772_CHIP_WORKING) {
736 ret = tsl2772_chip_off(indio_dev);
741 ret = tsl2772_chip_on(indio_dev);
744 mutex_unlock(&chip->prox_mutex);
745 mutex_unlock(&chip->als_mutex);
750 static int tsl2772_prox_cal(struct iio_dev *indio_dev)
752 struct tsl2772_chip *chip = iio_priv(indio_dev);
753 int prox_history[MAX_SAMPLES_CAL + 1];
754 int i, ret, mean, max, sample_sum;
756 if (chip->settings.prox_max_samples_cal < 1 ||
757 chip->settings.prox_max_samples_cal > MAX_SAMPLES_CAL)
760 for (i = 0; i < chip->settings.prox_max_samples_cal; i++) {
761 usleep_range(15000, 17500);
762 ret = tsl2772_get_prox(indio_dev);
766 prox_history[i] = chip->prox_data;
771 for (i = 0; i < chip->settings.prox_max_samples_cal; i++) {
772 sample_sum += prox_history[i];
773 max = max(max, prox_history[i]);
775 mean = sample_sum / chip->settings.prox_max_samples_cal;
777 chip->settings.prox_thres_high = (max << 1) - mean;
779 return tsl2772_invoke_change(indio_dev);
782 static int tsl2772_read_avail(struct iio_dev *indio_dev,
783 struct iio_chan_spec const *chan,
784 const int **vals, int *type, int *length,
787 struct tsl2772_chip *chip = iio_priv(indio_dev);
790 case IIO_CHAN_INFO_CALIBSCALE:
791 if (chan->type == IIO_INTENSITY) {
792 *length = ARRAY_SIZE(tsl2772_int_calibscale_avail);
793 *vals = tsl2772_int_calibscale_avail;
795 *length = ARRAY_SIZE(tsl2772_prox_calibscale_avail);
796 *vals = tsl2772_prox_calibscale_avail;
799 return IIO_AVAIL_LIST;
800 case IIO_CHAN_INFO_INT_TIME:
801 *length = ARRAY_SIZE(tsl2772_int_time_avail[chip->id]);
802 *vals = tsl2772_int_time_avail[chip->id];
803 *type = IIO_VAL_INT_PLUS_MICRO;
804 return IIO_AVAIL_RANGE;
810 static ssize_t in_illuminance0_target_input_show(struct device *dev,
811 struct device_attribute *attr,
814 struct tsl2772_chip *chip = iio_priv(dev_to_iio_dev(dev));
816 return snprintf(buf, PAGE_SIZE, "%d\n", chip->settings.als_cal_target);
819 static ssize_t in_illuminance0_target_input_store(struct device *dev,
820 struct device_attribute *attr,
821 const char *buf, size_t len)
823 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
824 struct tsl2772_chip *chip = iio_priv(indio_dev);
828 if (kstrtou16(buf, 0, &value))
831 chip->settings.als_cal_target = value;
832 ret = tsl2772_invoke_change(indio_dev);
839 static ssize_t in_illuminance0_calibrate_store(struct device *dev,
840 struct device_attribute *attr,
841 const char *buf, size_t len)
843 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
847 if (kstrtobool(buf, &value) || !value)
850 ret = tsl2772_als_calibrate(indio_dev);
854 ret = tsl2772_invoke_change(indio_dev);
861 static ssize_t in_illuminance0_lux_table_show(struct device *dev,
862 struct device_attribute *attr,
865 struct tsl2772_chip *chip = iio_priv(dev_to_iio_dev(dev));
869 while (i < TSL2772_MAX_LUX_TABLE_SIZE) {
870 offset += snprintf(buf + offset, PAGE_SIZE, "%u,%u,",
871 chip->tsl2772_device_lux[i].ch0,
872 chip->tsl2772_device_lux[i].ch1);
873 if (chip->tsl2772_device_lux[i].ch0 == 0) {
875 * We just printed the first "0" entry.
876 * Now get rid of the extra "," and break.
884 offset += snprintf(buf + offset, PAGE_SIZE, "\n");
888 static ssize_t in_illuminance0_lux_table_store(struct device *dev,
889 struct device_attribute *attr,
890 const char *buf, size_t len)
892 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
893 struct tsl2772_chip *chip = iio_priv(indio_dev);
894 int value[ARRAY_SIZE(chip->tsl2772_device_lux) * 2 + 1];
897 get_options(buf, ARRAY_SIZE(value), value);
900 * We now have an array of ints starting at value[1], and
901 * enumerated by value[0].
902 * We expect each group of two ints to be one table entry,
903 * and the last table entry is all 0.
906 if ((n % 2) || n < 4 ||
907 n > ((ARRAY_SIZE(chip->tsl2772_device_lux) - 1) * 2))
910 if ((value[(n - 1)] | value[n]) != 0)
913 if (chip->tsl2772_chip_status == TSL2772_CHIP_WORKING) {
914 ret = tsl2772_chip_off(indio_dev);
919 /* Zero out the table */
920 memset(chip->tsl2772_device_lux, 0, sizeof(chip->tsl2772_device_lux));
921 memcpy(chip->tsl2772_device_lux, &value[1], (value[0] * 4));
923 ret = tsl2772_invoke_change(indio_dev);
930 static ssize_t in_proximity0_calibrate_store(struct device *dev,
931 struct device_attribute *attr,
932 const char *buf, size_t len)
934 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
938 if (kstrtobool(buf, &value) || !value)
941 ret = tsl2772_prox_cal(indio_dev);
945 ret = tsl2772_invoke_change(indio_dev);
952 static int tsl2772_read_interrupt_config(struct iio_dev *indio_dev,
953 const struct iio_chan_spec *chan,
954 enum iio_event_type type,
955 enum iio_event_direction dir)
957 struct tsl2772_chip *chip = iio_priv(indio_dev);
959 if (chan->type == IIO_INTENSITY)
960 return chip->settings.als_interrupt_en;
962 return chip->settings.prox_interrupt_en;
965 static int tsl2772_write_interrupt_config(struct iio_dev *indio_dev,
966 const struct iio_chan_spec *chan,
967 enum iio_event_type type,
968 enum iio_event_direction dir,
971 struct tsl2772_chip *chip = iio_priv(indio_dev);
973 if (chan->type == IIO_INTENSITY)
974 chip->settings.als_interrupt_en = val ? true : false;
976 chip->settings.prox_interrupt_en = val ? true : false;
978 return tsl2772_invoke_change(indio_dev);
981 static int tsl2772_write_event_value(struct iio_dev *indio_dev,
982 const struct iio_chan_spec *chan,
983 enum iio_event_type type,
984 enum iio_event_direction dir,
985 enum iio_event_info info,
988 struct tsl2772_chip *chip = iio_priv(indio_dev);
989 int ret = -EINVAL, count, persistence;
993 case IIO_EV_INFO_VALUE:
994 if (chan->type == IIO_INTENSITY) {
996 case IIO_EV_DIR_RISING:
997 chip->settings.als_thresh_high = val;
1000 case IIO_EV_DIR_FALLING:
1001 chip->settings.als_thresh_low = val;
1009 case IIO_EV_DIR_RISING:
1010 chip->settings.prox_thres_high = val;
1013 case IIO_EV_DIR_FALLING:
1014 chip->settings.prox_thres_low = val;
1022 case IIO_EV_INFO_PERIOD:
1023 if (chan->type == IIO_INTENSITY)
1024 time = chip->settings.als_time;
1026 time = chip->settings.prox_time;
1029 persistence = ((val * 1000000) + val2) /
1030 (count * tsl2772_int_time_avail[chip->id][3]);
1032 if (chan->type == IIO_INTENSITY) {
1033 /* ALS filter values are 1, 2, 3, 5, 10, 15, ..., 60 */
1034 if (persistence > 3)
1035 persistence = (persistence / 5) + 3;
1037 chip->settings.als_persistence = persistence;
1039 chip->settings.prox_persistence = persistence;
1051 return tsl2772_invoke_change(indio_dev);
1054 static int tsl2772_read_event_value(struct iio_dev *indio_dev,
1055 const struct iio_chan_spec *chan,
1056 enum iio_event_type type,
1057 enum iio_event_direction dir,
1058 enum iio_event_info info,
1059 int *val, int *val2)
1061 struct tsl2772_chip *chip = iio_priv(indio_dev);
1062 int filter_delay, persistence;
1066 case IIO_EV_INFO_VALUE:
1067 if (chan->type == IIO_INTENSITY) {
1069 case IIO_EV_DIR_RISING:
1070 *val = chip->settings.als_thresh_high;
1072 case IIO_EV_DIR_FALLING:
1073 *val = chip->settings.als_thresh_low;
1080 case IIO_EV_DIR_RISING:
1081 *val = chip->settings.prox_thres_high;
1083 case IIO_EV_DIR_FALLING:
1084 *val = chip->settings.prox_thres_low;
1091 case IIO_EV_INFO_PERIOD:
1092 if (chan->type == IIO_INTENSITY) {
1093 time = chip->settings.als_time;
1094 persistence = chip->settings.als_persistence;
1096 /* ALS filter values are 1, 2, 3, 5, 10, 15, ..., 60 */
1097 if (persistence > 3)
1098 persistence = (persistence - 3) * 5;
1100 time = chip->settings.prox_time;
1101 persistence = chip->settings.prox_persistence;
1104 filter_delay = persistence * (256 - time) *
1105 tsl2772_int_time_avail[chip->id][3];
1107 *val = filter_delay / 1000000;
1108 *val2 = filter_delay % 1000000;
1109 return IIO_VAL_INT_PLUS_MICRO;
1115 static int tsl2772_read_raw(struct iio_dev *indio_dev,
1116 struct iio_chan_spec const *chan,
1121 struct tsl2772_chip *chip = iio_priv(indio_dev);
1124 case IIO_CHAN_INFO_PROCESSED:
1125 switch (chan->type) {
1127 tsl2772_get_lux(indio_dev);
1128 *val = chip->als_cur_info.lux;
1133 case IIO_CHAN_INFO_RAW:
1134 switch (chan->type) {
1136 tsl2772_get_lux(indio_dev);
1137 if (chan->channel == 0)
1138 *val = chip->als_cur_info.als_ch0;
1140 *val = chip->als_cur_info.als_ch1;
1143 tsl2772_get_prox(indio_dev);
1144 *val = chip->prox_data;
1150 case IIO_CHAN_INFO_CALIBSCALE:
1151 if (chan->type == IIO_LIGHT)
1152 *val = tsl2772_als_gain[chip->settings.als_gain];
1154 *val = tsl2772_prox_gain[chip->settings.prox_gain];
1156 case IIO_CHAN_INFO_CALIBBIAS:
1157 *val = chip->settings.als_gain_trim;
1159 case IIO_CHAN_INFO_INT_TIME:
1161 *val2 = (256 - chip->settings.als_time) *
1162 tsl2772_int_time_avail[chip->id][3];
1163 return IIO_VAL_INT_PLUS_MICRO;
1169 static int tsl2772_write_raw(struct iio_dev *indio_dev,
1170 struct iio_chan_spec const *chan,
1175 struct tsl2772_chip *chip = iio_priv(indio_dev);
1178 case IIO_CHAN_INFO_CALIBSCALE:
1179 if (chan->type == IIO_INTENSITY) {
1182 chip->settings.als_gain = 0;
1185 chip->settings.als_gain = 1;
1188 chip->settings.als_gain = 2;
1191 chip->settings.als_gain = 3;
1199 chip->settings.prox_gain = 0;
1202 chip->settings.prox_gain = 1;
1205 chip->settings.prox_gain = 2;
1208 chip->settings.prox_gain = 3;
1215 case IIO_CHAN_INFO_CALIBBIAS:
1216 if (val < TSL2772_ALS_GAIN_TRIM_MIN ||
1217 val > TSL2772_ALS_GAIN_TRIM_MAX)
1220 chip->settings.als_gain_trim = val;
1222 case IIO_CHAN_INFO_INT_TIME:
1223 if (val != 0 || val2 < tsl2772_int_time_avail[chip->id][1] ||
1224 val2 > tsl2772_int_time_avail[chip->id][5])
1227 chip->settings.als_time = 256 -
1228 (val2 / tsl2772_int_time_avail[chip->id][3]);
1234 return tsl2772_invoke_change(indio_dev);
1237 static DEVICE_ATTR_RW(in_illuminance0_target_input);
1239 static DEVICE_ATTR_WO(in_illuminance0_calibrate);
1241 static DEVICE_ATTR_WO(in_proximity0_calibrate);
1243 static DEVICE_ATTR_RW(in_illuminance0_lux_table);
1245 /* Use the default register values to identify the Taos device */
1246 static int tsl2772_device_id_verif(int id, int target)
1252 return (id & 0xf0) == TRITON_ID;
1255 return (id & 0xf0) == HALIBUT_ID;
1261 return (id & 0xf0) == SWORDFISH_ID;
1267 static irqreturn_t tsl2772_event_handler(int irq, void *private)
1269 struct iio_dev *indio_dev = private;
1270 struct tsl2772_chip *chip = iio_priv(indio_dev);
1271 s64 timestamp = iio_get_time_ns(indio_dev);
1274 ret = tsl2772_read_status(chip);
1278 /* What type of interrupt do we need to process */
1279 if (ret & TSL2772_STA_PRX_INTR) {
1280 iio_push_event(indio_dev,
1281 IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY,
1288 if (ret & TSL2772_STA_ALS_INTR) {
1289 iio_push_event(indio_dev,
1290 IIO_UNMOD_EVENT_CODE(IIO_LIGHT,
1297 ret = i2c_smbus_write_byte(chip->client,
1298 TSL2772_CMD_REG | TSL2772_CMD_SPL_FN |
1299 TSL2772_CMD_PROXALS_INT_CLR);
1301 dev_err(&chip->client->dev,
1302 "%s: failed to clear interrupt status: %d\n",
1308 static struct attribute *tsl2772_ALS_device_attrs[] = {
1309 &dev_attr_in_illuminance0_target_input.attr,
1310 &dev_attr_in_illuminance0_calibrate.attr,
1311 &dev_attr_in_illuminance0_lux_table.attr,
1315 static struct attribute *tsl2772_PRX_device_attrs[] = {
1316 &dev_attr_in_proximity0_calibrate.attr,
1320 static struct attribute *tsl2772_ALSPRX_device_attrs[] = {
1321 &dev_attr_in_illuminance0_target_input.attr,
1322 &dev_attr_in_illuminance0_calibrate.attr,
1323 &dev_attr_in_illuminance0_lux_table.attr,
1327 static struct attribute *tsl2772_PRX2_device_attrs[] = {
1328 &dev_attr_in_proximity0_calibrate.attr,
1332 static struct attribute *tsl2772_ALSPRX2_device_attrs[] = {
1333 &dev_attr_in_illuminance0_target_input.attr,
1334 &dev_attr_in_illuminance0_calibrate.attr,
1335 &dev_attr_in_illuminance0_lux_table.attr,
1336 &dev_attr_in_proximity0_calibrate.attr,
1340 static const struct attribute_group tsl2772_device_attr_group_tbl[] = {
1342 .attrs = tsl2772_ALS_device_attrs,
1345 .attrs = tsl2772_PRX_device_attrs,
1348 .attrs = tsl2772_ALSPRX_device_attrs,
1351 .attrs = tsl2772_PRX2_device_attrs,
1354 .attrs = tsl2772_ALSPRX2_device_attrs,
1358 #define TSL2772_DEVICE_INFO(type)[type] = \
1360 .attrs = &tsl2772_device_attr_group_tbl[type], \
1361 .read_raw = &tsl2772_read_raw, \
1362 .read_avail = &tsl2772_read_avail, \
1363 .write_raw = &tsl2772_write_raw, \
1364 .read_event_value = &tsl2772_read_event_value, \
1365 .write_event_value = &tsl2772_write_event_value, \
1366 .read_event_config = &tsl2772_read_interrupt_config, \
1367 .write_event_config = &tsl2772_write_interrupt_config, \
1370 static const struct iio_info tsl2772_device_info[] = {
1371 TSL2772_DEVICE_INFO(ALS),
1372 TSL2772_DEVICE_INFO(PRX),
1373 TSL2772_DEVICE_INFO(ALSPRX),
1374 TSL2772_DEVICE_INFO(PRX2),
1375 TSL2772_DEVICE_INFO(ALSPRX2),
1378 static const struct iio_event_spec tsl2772_events[] = {
1380 .type = IIO_EV_TYPE_THRESH,
1381 .dir = IIO_EV_DIR_RISING,
1382 .mask_separate = BIT(IIO_EV_INFO_VALUE),
1384 .type = IIO_EV_TYPE_THRESH,
1385 .dir = IIO_EV_DIR_FALLING,
1386 .mask_separate = BIT(IIO_EV_INFO_VALUE),
1388 .type = IIO_EV_TYPE_THRESH,
1389 .dir = IIO_EV_DIR_EITHER,
1390 .mask_separate = BIT(IIO_EV_INFO_PERIOD) |
1391 BIT(IIO_EV_INFO_ENABLE),
1395 static const struct tsl2772_chip_info tsl2772_chip_info_tbl[] = {
1397 .channel_with_events = {
1402 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1404 .type = IIO_INTENSITY,
1407 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1408 BIT(IIO_CHAN_INFO_INT_TIME) |
1409 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1410 BIT(IIO_CHAN_INFO_CALIBBIAS),
1411 .info_mask_separate_available =
1412 BIT(IIO_CHAN_INFO_INT_TIME) |
1413 BIT(IIO_CHAN_INFO_CALIBSCALE),
1414 .event_spec = tsl2772_events,
1415 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1417 .type = IIO_INTENSITY,
1422 .channel_without_events = {
1427 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1429 .type = IIO_INTENSITY,
1432 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1433 BIT(IIO_CHAN_INFO_INT_TIME) |
1434 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1435 BIT(IIO_CHAN_INFO_CALIBBIAS),
1436 .info_mask_separate_available =
1437 BIT(IIO_CHAN_INFO_INT_TIME) |
1438 BIT(IIO_CHAN_INFO_CALIBSCALE),
1440 .type = IIO_INTENSITY,
1445 .chan_table_elements = 3,
1446 .info = &tsl2772_device_info[ALS],
1449 .channel_with_events = {
1451 .type = IIO_PROXIMITY,
1454 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1455 .event_spec = tsl2772_events,
1456 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1459 .channel_without_events = {
1461 .type = IIO_PROXIMITY,
1464 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1467 .chan_table_elements = 1,
1468 .info = &tsl2772_device_info[PRX],
1471 .channel_with_events = {
1476 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1478 .type = IIO_INTENSITY,
1481 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1482 BIT(IIO_CHAN_INFO_INT_TIME) |
1483 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1484 BIT(IIO_CHAN_INFO_CALIBBIAS),
1485 .info_mask_separate_available =
1486 BIT(IIO_CHAN_INFO_INT_TIME) |
1487 BIT(IIO_CHAN_INFO_CALIBSCALE),
1488 .event_spec = tsl2772_events,
1489 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1491 .type = IIO_INTENSITY,
1494 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1496 .type = IIO_PROXIMITY,
1499 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1500 .event_spec = tsl2772_events,
1501 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1504 .channel_without_events = {
1509 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1511 .type = IIO_INTENSITY,
1514 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1515 BIT(IIO_CHAN_INFO_INT_TIME) |
1516 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1517 BIT(IIO_CHAN_INFO_CALIBBIAS),
1518 .info_mask_separate_available =
1519 BIT(IIO_CHAN_INFO_INT_TIME) |
1520 BIT(IIO_CHAN_INFO_CALIBSCALE),
1522 .type = IIO_INTENSITY,
1525 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1527 .type = IIO_PROXIMITY,
1530 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1533 .chan_table_elements = 4,
1534 .info = &tsl2772_device_info[ALSPRX],
1537 .channel_with_events = {
1539 .type = IIO_PROXIMITY,
1542 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1543 BIT(IIO_CHAN_INFO_CALIBSCALE),
1544 .info_mask_separate_available =
1545 BIT(IIO_CHAN_INFO_CALIBSCALE),
1546 .event_spec = tsl2772_events,
1547 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1550 .channel_without_events = {
1552 .type = IIO_PROXIMITY,
1555 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1556 BIT(IIO_CHAN_INFO_CALIBSCALE),
1557 .info_mask_separate_available =
1558 BIT(IIO_CHAN_INFO_CALIBSCALE),
1561 .chan_table_elements = 1,
1562 .info = &tsl2772_device_info[PRX2],
1565 .channel_with_events = {
1570 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1572 .type = IIO_INTENSITY,
1575 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1576 BIT(IIO_CHAN_INFO_INT_TIME) |
1577 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1578 BIT(IIO_CHAN_INFO_CALIBBIAS),
1579 .info_mask_separate_available =
1580 BIT(IIO_CHAN_INFO_INT_TIME) |
1581 BIT(IIO_CHAN_INFO_CALIBSCALE),
1582 .event_spec = tsl2772_events,
1583 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1585 .type = IIO_INTENSITY,
1588 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1590 .type = IIO_PROXIMITY,
1593 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1594 BIT(IIO_CHAN_INFO_CALIBSCALE),
1595 .info_mask_separate_available =
1596 BIT(IIO_CHAN_INFO_CALIBSCALE),
1597 .event_spec = tsl2772_events,
1598 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1601 .channel_without_events = {
1606 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1608 .type = IIO_INTENSITY,
1611 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1612 BIT(IIO_CHAN_INFO_INT_TIME) |
1613 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1614 BIT(IIO_CHAN_INFO_CALIBBIAS),
1615 .info_mask_separate_available =
1616 BIT(IIO_CHAN_INFO_INT_TIME) |
1617 BIT(IIO_CHAN_INFO_CALIBSCALE),
1619 .type = IIO_INTENSITY,
1622 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1624 .type = IIO_PROXIMITY,
1627 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1628 BIT(IIO_CHAN_INFO_CALIBSCALE),
1629 .info_mask_separate_available =
1630 BIT(IIO_CHAN_INFO_CALIBSCALE),
1633 .chan_table_elements = 4,
1634 .info = &tsl2772_device_info[ALSPRX2],
1638 static int tsl2772_probe(struct i2c_client *clientp,
1639 const struct i2c_device_id *id)
1641 struct iio_dev *indio_dev;
1642 struct tsl2772_chip *chip;
1645 indio_dev = devm_iio_device_alloc(&clientp->dev, sizeof(*chip));
1649 chip = iio_priv(indio_dev);
1650 chip->client = clientp;
1651 i2c_set_clientdata(clientp, indio_dev);
1653 ret = i2c_smbus_read_byte_data(chip->client,
1654 TSL2772_CMD_REG | TSL2772_CHIPID);
1658 if (tsl2772_device_id_verif(ret, id->driver_data) <= 0) {
1659 dev_info(&chip->client->dev,
1660 "%s: i2c device found does not match expected id\n",
1665 ret = i2c_smbus_write_byte(clientp, TSL2772_CMD_REG | TSL2772_CNTRL);
1667 dev_err(&clientp->dev,
1668 "%s: Failed to write to CMD register: %d\n",
1673 mutex_init(&chip->als_mutex);
1674 mutex_init(&chip->prox_mutex);
1676 chip->tsl2772_chip_status = TSL2772_CHIP_UNKNOWN;
1677 chip->pdata = dev_get_platdata(&clientp->dev);
1678 chip->id = id->driver_data;
1680 &tsl2772_chip_info_tbl[device_channel_config[id->driver_data]];
1682 indio_dev->info = chip->chip_info->info;
1683 indio_dev->dev.parent = &clientp->dev;
1684 indio_dev->modes = INDIO_DIRECT_MODE;
1685 indio_dev->name = chip->client->name;
1686 indio_dev->num_channels = chip->chip_info->chan_table_elements;
1689 indio_dev->channels = chip->chip_info->channel_with_events;
1691 ret = devm_request_threaded_irq(&clientp->dev, clientp->irq,
1693 &tsl2772_event_handler,
1694 IRQF_TRIGGER_FALLING |
1699 dev_err(&clientp->dev,
1700 "%s: irq request failed\n", __func__);
1704 indio_dev->channels = chip->chip_info->channel_without_events;
1707 tsl2772_defaults(chip);
1708 ret = tsl2772_chip_on(indio_dev);
1712 ret = iio_device_register(indio_dev);
1714 tsl2772_chip_off(indio_dev);
1715 dev_err(&clientp->dev,
1716 "%s: iio registration failed\n", __func__);
1723 static int tsl2772_suspend(struct device *dev)
1725 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1727 return tsl2772_chip_off(indio_dev);
1730 static int tsl2772_resume(struct device *dev)
1732 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1734 return tsl2772_chip_on(indio_dev);
1737 static int tsl2772_remove(struct i2c_client *client)
1739 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1741 tsl2772_chip_off(indio_dev);
1743 iio_device_unregister(indio_dev);
1748 static const struct i2c_device_id tsl2772_idtable[] = {
1749 { "tsl2571", tsl2571 },
1750 { "tsl2671", tsl2671 },
1751 { "tmd2671", tmd2671 },
1752 { "tsl2771", tsl2771 },
1753 { "tmd2771", tmd2771 },
1754 { "tsl2572", tsl2572 },
1755 { "tsl2672", tsl2672 },
1756 { "tmd2672", tmd2672 },
1757 { "tsl2772", tsl2772 },
1758 { "tmd2772", tmd2772 },
1762 MODULE_DEVICE_TABLE(i2c, tsl2772_idtable);
1764 static const struct of_device_id tsl2772_of_match[] = {
1765 { .compatible = "amstaos,tsl2571" },
1766 { .compatible = "amstaos,tsl2671" },
1767 { .compatible = "amstaos,tmd2671" },
1768 { .compatible = "amstaos,tsl2771" },
1769 { .compatible = "amstaos,tmd2771" },
1770 { .compatible = "amstaos,tsl2572" },
1771 { .compatible = "amstaos,tsl2672" },
1772 { .compatible = "amstaos,tmd2672" },
1773 { .compatible = "amstaos,tsl2772" },
1774 { .compatible = "amstaos,tmd2772" },
1777 MODULE_DEVICE_TABLE(of, tsl2772_of_match);
1779 static const struct dev_pm_ops tsl2772_pm_ops = {
1780 .suspend = tsl2772_suspend,
1781 .resume = tsl2772_resume,
1784 static struct i2c_driver tsl2772_driver = {
1787 .of_match_table = tsl2772_of_match,
1788 .pm = &tsl2772_pm_ops,
1790 .id_table = tsl2772_idtable,
1791 .probe = tsl2772_probe,
1792 .remove = tsl2772_remove,
1795 module_i2c_driver(tsl2772_driver);
1797 MODULE_AUTHOR("J. August Brenner <Jon.Brenner@ams.com>");
1798 MODULE_AUTHOR("Brian Masney <masneyb@onstation.org>");
1799 MODULE_DESCRIPTION("TAOS tsl2772 ambient and proximity light sensor driver");
1800 MODULE_LICENSE("GPL");