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__);
588 ret = (chip->settings.als_cal_target * chip->settings.als_gain_trim) /
590 if (ret < TSL2772_ALS_GAIN_TRIM_MIN || ret > TSL2772_ALS_GAIN_TRIM_MAX)
593 chip->settings.als_gain_trim = ret;
598 static int tsl2772_chip_on(struct iio_dev *indio_dev)
600 struct tsl2772_chip *chip = iio_priv(indio_dev);
601 int ret, i, als_count, als_time_us;
602 u8 *dev_reg, reg_val;
604 /* Non calculated parameters */
605 chip->tsl2772_config[TSL2772_ALS_TIME] = chip->settings.als_time;
606 chip->tsl2772_config[TSL2772_PRX_TIME] = chip->settings.prox_time;
607 chip->tsl2772_config[TSL2772_WAIT_TIME] = chip->settings.wait_time;
608 chip->tsl2772_config[TSL2772_ALS_PRX_CONFIG] =
609 chip->settings.als_prox_config;
611 chip->tsl2772_config[TSL2772_ALS_MINTHRESHLO] =
612 (chip->settings.als_thresh_low) & 0xFF;
613 chip->tsl2772_config[TSL2772_ALS_MINTHRESHHI] =
614 (chip->settings.als_thresh_low >> 8) & 0xFF;
615 chip->tsl2772_config[TSL2772_ALS_MAXTHRESHLO] =
616 (chip->settings.als_thresh_high) & 0xFF;
617 chip->tsl2772_config[TSL2772_ALS_MAXTHRESHHI] =
618 (chip->settings.als_thresh_high >> 8) & 0xFF;
619 chip->tsl2772_config[TSL2772_PERSISTENCE] =
620 (chip->settings.prox_persistence & 0xFF) << 4 |
621 (chip->settings.als_persistence & 0xFF);
623 chip->tsl2772_config[TSL2772_PRX_COUNT] =
624 chip->settings.prox_pulse_count;
625 chip->tsl2772_config[TSL2772_PRX_MINTHRESHLO] =
626 (chip->settings.prox_thres_low) & 0xFF;
627 chip->tsl2772_config[TSL2772_PRX_MINTHRESHHI] =
628 (chip->settings.prox_thres_low >> 8) & 0xFF;
629 chip->tsl2772_config[TSL2772_PRX_MAXTHRESHLO] =
630 (chip->settings.prox_thres_high) & 0xFF;
631 chip->tsl2772_config[TSL2772_PRX_MAXTHRESHHI] =
632 (chip->settings.prox_thres_high >> 8) & 0xFF;
634 /* and make sure we're not already on */
635 if (chip->tsl2772_chip_status == TSL2772_CHIP_WORKING) {
636 /* if forcing a register update - turn off, then on */
637 dev_info(&chip->client->dev, "device is already enabled\n");
641 /* Set the gain based on tsl2772_settings struct */
642 chip->tsl2772_config[TSL2772_GAIN] =
643 (chip->settings.als_gain & 0xFF) |
644 ((chip->settings.prox_gain & 0xFF) << 2) |
645 (chip->settings.prox_diode << 4) |
646 (chip->settings.prox_power << 6);
648 /* set chip time scaling and saturation */
649 als_count = 256 - chip->settings.als_time;
650 als_time_us = als_count * tsl2772_int_time_avail[chip->id][3];
651 chip->als_saturation = als_count * 768; /* 75% of full scale */
652 chip->als_gain_time_scale = als_time_us *
653 tsl2772_als_gain[chip->settings.als_gain];
656 * TSL2772 Specific power-on / adc enable sequence
657 * Power on the device 1st.
659 ret = tsl2772_write_control_reg(chip, TSL2772_CNTL_PWR_ON);
664 * Use the following shadow copy for our delay before enabling ADC.
665 * Write all the registers.
667 for (i = 0, dev_reg = chip->tsl2772_config;
668 i < TSL2772_MAX_CONFIG_REG; i++) {
669 int reg = TSL2772_CMD_REG + i;
671 ret = i2c_smbus_write_byte_data(chip->client, reg,
674 dev_err(&chip->client->dev,
675 "%s: failed to write to register %x: %d\n",
681 /* Power-on settling time */
682 usleep_range(3000, 3500);
684 reg_val = TSL2772_CNTL_PWR_ON | TSL2772_CNTL_ADC_ENBL |
685 TSL2772_CNTL_PROX_DET_ENBL;
686 if (chip->settings.als_interrupt_en)
687 reg_val |= TSL2772_CNTL_ALS_INT_ENBL;
688 if (chip->settings.prox_interrupt_en)
689 reg_val |= TSL2772_CNTL_PROX_INT_ENBL;
691 ret = tsl2772_write_control_reg(chip, reg_val);
695 ret = i2c_smbus_write_byte(chip->client,
696 TSL2772_CMD_REG | TSL2772_CMD_SPL_FN |
697 TSL2772_CMD_PROXALS_INT_CLR);
699 dev_err(&chip->client->dev,
700 "%s: failed to clear interrupt status: %d\n",
705 chip->tsl2772_chip_status = TSL2772_CHIP_WORKING;
710 static int tsl2772_chip_off(struct iio_dev *indio_dev)
712 struct tsl2772_chip *chip = iio_priv(indio_dev);
714 /* turn device off */
715 chip->tsl2772_chip_status = TSL2772_CHIP_SUSPENDED;
716 return tsl2772_write_control_reg(chip, 0x00);
719 static void tsl2772_chip_off_action(void *data)
721 struct iio_dev *indio_dev = data;
723 tsl2772_chip_off(indio_dev);
727 * tsl2772_invoke_change - power cycle the device to implement the user
729 * @indio_dev: pointer to IIO device
731 * Obtain and lock both ALS and PROX resources, determine and save device state
732 * (On/Off), cycle device to implement updated parameter, put device back into
733 * proper state, and unlock resource.
735 static int tsl2772_invoke_change(struct iio_dev *indio_dev)
737 struct tsl2772_chip *chip = iio_priv(indio_dev);
738 int device_status = chip->tsl2772_chip_status;
741 mutex_lock(&chip->als_mutex);
742 mutex_lock(&chip->prox_mutex);
744 if (device_status == TSL2772_CHIP_WORKING) {
745 ret = tsl2772_chip_off(indio_dev);
750 ret = tsl2772_chip_on(indio_dev);
753 mutex_unlock(&chip->prox_mutex);
754 mutex_unlock(&chip->als_mutex);
759 static int tsl2772_prox_cal(struct iio_dev *indio_dev)
761 struct tsl2772_chip *chip = iio_priv(indio_dev);
762 int prox_history[MAX_SAMPLES_CAL + 1];
763 int i, ret, mean, max, sample_sum;
765 if (chip->settings.prox_max_samples_cal < 1 ||
766 chip->settings.prox_max_samples_cal > MAX_SAMPLES_CAL)
769 for (i = 0; i < chip->settings.prox_max_samples_cal; i++) {
770 usleep_range(15000, 17500);
771 ret = tsl2772_get_prox(indio_dev);
775 prox_history[i] = chip->prox_data;
780 for (i = 0; i < chip->settings.prox_max_samples_cal; i++) {
781 sample_sum += prox_history[i];
782 max = max(max, prox_history[i]);
784 mean = sample_sum / chip->settings.prox_max_samples_cal;
786 chip->settings.prox_thres_high = (max << 1) - mean;
788 return tsl2772_invoke_change(indio_dev);
791 static int tsl2772_read_avail(struct iio_dev *indio_dev,
792 struct iio_chan_spec const *chan,
793 const int **vals, int *type, int *length,
796 struct tsl2772_chip *chip = iio_priv(indio_dev);
799 case IIO_CHAN_INFO_CALIBSCALE:
800 if (chan->type == IIO_INTENSITY) {
801 *length = ARRAY_SIZE(tsl2772_int_calibscale_avail);
802 *vals = tsl2772_int_calibscale_avail;
804 *length = ARRAY_SIZE(tsl2772_prox_calibscale_avail);
805 *vals = tsl2772_prox_calibscale_avail;
808 return IIO_AVAIL_LIST;
809 case IIO_CHAN_INFO_INT_TIME:
810 *length = ARRAY_SIZE(tsl2772_int_time_avail[chip->id]);
811 *vals = tsl2772_int_time_avail[chip->id];
812 *type = IIO_VAL_INT_PLUS_MICRO;
813 return IIO_AVAIL_RANGE;
819 static ssize_t in_illuminance0_target_input_show(struct device *dev,
820 struct device_attribute *attr,
823 struct tsl2772_chip *chip = iio_priv(dev_to_iio_dev(dev));
825 return snprintf(buf, PAGE_SIZE, "%d\n", chip->settings.als_cal_target);
828 static ssize_t in_illuminance0_target_input_store(struct device *dev,
829 struct device_attribute *attr,
830 const char *buf, size_t len)
832 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
833 struct tsl2772_chip *chip = iio_priv(indio_dev);
837 if (kstrtou16(buf, 0, &value))
840 chip->settings.als_cal_target = value;
841 ret = tsl2772_invoke_change(indio_dev);
848 static ssize_t in_illuminance0_calibrate_store(struct device *dev,
849 struct device_attribute *attr,
850 const char *buf, size_t len)
852 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
856 if (kstrtobool(buf, &value) || !value)
859 ret = tsl2772_als_calibrate(indio_dev);
863 ret = tsl2772_invoke_change(indio_dev);
870 static ssize_t in_illuminance0_lux_table_show(struct device *dev,
871 struct device_attribute *attr,
874 struct tsl2772_chip *chip = iio_priv(dev_to_iio_dev(dev));
878 while (i < TSL2772_MAX_LUX_TABLE_SIZE) {
879 offset += snprintf(buf + offset, PAGE_SIZE, "%u,%u,",
880 chip->tsl2772_device_lux[i].ch0,
881 chip->tsl2772_device_lux[i].ch1);
882 if (chip->tsl2772_device_lux[i].ch0 == 0) {
884 * We just printed the first "0" entry.
885 * Now get rid of the extra "," and break.
893 offset += snprintf(buf + offset, PAGE_SIZE, "\n");
897 static ssize_t in_illuminance0_lux_table_store(struct device *dev,
898 struct device_attribute *attr,
899 const char *buf, size_t len)
901 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
902 struct tsl2772_chip *chip = iio_priv(indio_dev);
903 int value[ARRAY_SIZE(chip->tsl2772_device_lux) * 2 + 1];
906 get_options(buf, ARRAY_SIZE(value), value);
909 * We now have an array of ints starting at value[1], and
910 * enumerated by value[0].
911 * We expect each group of two ints to be one table entry,
912 * and the last table entry is all 0.
915 if ((n % 2) || n < 4 ||
916 n > ((ARRAY_SIZE(chip->tsl2772_device_lux) - 1) * 2))
919 if ((value[(n - 1)] | value[n]) != 0)
922 if (chip->tsl2772_chip_status == TSL2772_CHIP_WORKING) {
923 ret = tsl2772_chip_off(indio_dev);
928 /* Zero out the table */
929 memset(chip->tsl2772_device_lux, 0, sizeof(chip->tsl2772_device_lux));
930 memcpy(chip->tsl2772_device_lux, &value[1], (value[0] * 4));
932 ret = tsl2772_invoke_change(indio_dev);
939 static ssize_t in_proximity0_calibrate_store(struct device *dev,
940 struct device_attribute *attr,
941 const char *buf, size_t len)
943 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
947 if (kstrtobool(buf, &value) || !value)
950 ret = tsl2772_prox_cal(indio_dev);
954 ret = tsl2772_invoke_change(indio_dev);
961 static int tsl2772_read_interrupt_config(struct iio_dev *indio_dev,
962 const struct iio_chan_spec *chan,
963 enum iio_event_type type,
964 enum iio_event_direction dir)
966 struct tsl2772_chip *chip = iio_priv(indio_dev);
968 if (chan->type == IIO_INTENSITY)
969 return chip->settings.als_interrupt_en;
971 return chip->settings.prox_interrupt_en;
974 static int tsl2772_write_interrupt_config(struct iio_dev *indio_dev,
975 const struct iio_chan_spec *chan,
976 enum iio_event_type type,
977 enum iio_event_direction dir,
980 struct tsl2772_chip *chip = iio_priv(indio_dev);
982 if (chan->type == IIO_INTENSITY)
983 chip->settings.als_interrupt_en = val ? true : false;
985 chip->settings.prox_interrupt_en = val ? true : false;
987 return tsl2772_invoke_change(indio_dev);
990 static int tsl2772_write_event_value(struct iio_dev *indio_dev,
991 const struct iio_chan_spec *chan,
992 enum iio_event_type type,
993 enum iio_event_direction dir,
994 enum iio_event_info info,
997 struct tsl2772_chip *chip = iio_priv(indio_dev);
998 int ret = -EINVAL, count, persistence;
1002 case IIO_EV_INFO_VALUE:
1003 if (chan->type == IIO_INTENSITY) {
1005 case IIO_EV_DIR_RISING:
1006 chip->settings.als_thresh_high = val;
1009 case IIO_EV_DIR_FALLING:
1010 chip->settings.als_thresh_low = val;
1018 case IIO_EV_DIR_RISING:
1019 chip->settings.prox_thres_high = val;
1022 case IIO_EV_DIR_FALLING:
1023 chip->settings.prox_thres_low = val;
1031 case IIO_EV_INFO_PERIOD:
1032 if (chan->type == IIO_INTENSITY)
1033 time = chip->settings.als_time;
1035 time = chip->settings.prox_time;
1038 persistence = ((val * 1000000) + val2) /
1039 (count * tsl2772_int_time_avail[chip->id][3]);
1041 if (chan->type == IIO_INTENSITY) {
1042 /* ALS filter values are 1, 2, 3, 5, 10, 15, ..., 60 */
1043 if (persistence > 3)
1044 persistence = (persistence / 5) + 3;
1046 chip->settings.als_persistence = persistence;
1048 chip->settings.prox_persistence = persistence;
1060 return tsl2772_invoke_change(indio_dev);
1063 static int tsl2772_read_event_value(struct iio_dev *indio_dev,
1064 const struct iio_chan_spec *chan,
1065 enum iio_event_type type,
1066 enum iio_event_direction dir,
1067 enum iio_event_info info,
1068 int *val, int *val2)
1070 struct tsl2772_chip *chip = iio_priv(indio_dev);
1071 int filter_delay, persistence;
1075 case IIO_EV_INFO_VALUE:
1076 if (chan->type == IIO_INTENSITY) {
1078 case IIO_EV_DIR_RISING:
1079 *val = chip->settings.als_thresh_high;
1081 case IIO_EV_DIR_FALLING:
1082 *val = chip->settings.als_thresh_low;
1089 case IIO_EV_DIR_RISING:
1090 *val = chip->settings.prox_thres_high;
1092 case IIO_EV_DIR_FALLING:
1093 *val = chip->settings.prox_thres_low;
1100 case IIO_EV_INFO_PERIOD:
1101 if (chan->type == IIO_INTENSITY) {
1102 time = chip->settings.als_time;
1103 persistence = chip->settings.als_persistence;
1105 /* ALS filter values are 1, 2, 3, 5, 10, 15, ..., 60 */
1106 if (persistence > 3)
1107 persistence = (persistence - 3) * 5;
1109 time = chip->settings.prox_time;
1110 persistence = chip->settings.prox_persistence;
1113 filter_delay = persistence * (256 - time) *
1114 tsl2772_int_time_avail[chip->id][3];
1116 *val = filter_delay / 1000000;
1117 *val2 = filter_delay % 1000000;
1118 return IIO_VAL_INT_PLUS_MICRO;
1124 static int tsl2772_read_raw(struct iio_dev *indio_dev,
1125 struct iio_chan_spec const *chan,
1130 struct tsl2772_chip *chip = iio_priv(indio_dev);
1133 case IIO_CHAN_INFO_PROCESSED:
1134 switch (chan->type) {
1136 tsl2772_get_lux(indio_dev);
1137 *val = chip->als_cur_info.lux;
1142 case IIO_CHAN_INFO_RAW:
1143 switch (chan->type) {
1145 tsl2772_get_lux(indio_dev);
1146 if (chan->channel == 0)
1147 *val = chip->als_cur_info.als_ch0;
1149 *val = chip->als_cur_info.als_ch1;
1152 tsl2772_get_prox(indio_dev);
1153 *val = chip->prox_data;
1159 case IIO_CHAN_INFO_CALIBSCALE:
1160 if (chan->type == IIO_LIGHT)
1161 *val = tsl2772_als_gain[chip->settings.als_gain];
1163 *val = tsl2772_prox_gain[chip->settings.prox_gain];
1165 case IIO_CHAN_INFO_CALIBBIAS:
1166 *val = chip->settings.als_gain_trim;
1168 case IIO_CHAN_INFO_INT_TIME:
1170 *val2 = (256 - chip->settings.als_time) *
1171 tsl2772_int_time_avail[chip->id][3];
1172 return IIO_VAL_INT_PLUS_MICRO;
1178 static int tsl2772_write_raw(struct iio_dev *indio_dev,
1179 struct iio_chan_spec const *chan,
1184 struct tsl2772_chip *chip = iio_priv(indio_dev);
1187 case IIO_CHAN_INFO_CALIBSCALE:
1188 if (chan->type == IIO_INTENSITY) {
1191 chip->settings.als_gain = 0;
1194 chip->settings.als_gain = 1;
1197 chip->settings.als_gain = 2;
1200 chip->settings.als_gain = 3;
1208 chip->settings.prox_gain = 0;
1211 chip->settings.prox_gain = 1;
1214 chip->settings.prox_gain = 2;
1217 chip->settings.prox_gain = 3;
1224 case IIO_CHAN_INFO_CALIBBIAS:
1225 if (val < TSL2772_ALS_GAIN_TRIM_MIN ||
1226 val > TSL2772_ALS_GAIN_TRIM_MAX)
1229 chip->settings.als_gain_trim = val;
1231 case IIO_CHAN_INFO_INT_TIME:
1232 if (val != 0 || val2 < tsl2772_int_time_avail[chip->id][1] ||
1233 val2 > tsl2772_int_time_avail[chip->id][5])
1236 chip->settings.als_time = 256 -
1237 (val2 / tsl2772_int_time_avail[chip->id][3]);
1243 return tsl2772_invoke_change(indio_dev);
1246 static DEVICE_ATTR_RW(in_illuminance0_target_input);
1248 static DEVICE_ATTR_WO(in_illuminance0_calibrate);
1250 static DEVICE_ATTR_WO(in_proximity0_calibrate);
1252 static DEVICE_ATTR_RW(in_illuminance0_lux_table);
1254 /* Use the default register values to identify the Taos device */
1255 static int tsl2772_device_id_verif(int id, int target)
1261 return (id & 0xf0) == TRITON_ID;
1264 return (id & 0xf0) == HALIBUT_ID;
1270 return (id & 0xf0) == SWORDFISH_ID;
1276 static irqreturn_t tsl2772_event_handler(int irq, void *private)
1278 struct iio_dev *indio_dev = private;
1279 struct tsl2772_chip *chip = iio_priv(indio_dev);
1280 s64 timestamp = iio_get_time_ns(indio_dev);
1283 ret = tsl2772_read_status(chip);
1287 /* What type of interrupt do we need to process */
1288 if (ret & TSL2772_STA_PRX_INTR) {
1289 iio_push_event(indio_dev,
1290 IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY,
1297 if (ret & TSL2772_STA_ALS_INTR) {
1298 iio_push_event(indio_dev,
1299 IIO_UNMOD_EVENT_CODE(IIO_LIGHT,
1306 ret = i2c_smbus_write_byte(chip->client,
1307 TSL2772_CMD_REG | TSL2772_CMD_SPL_FN |
1308 TSL2772_CMD_PROXALS_INT_CLR);
1310 dev_err(&chip->client->dev,
1311 "%s: failed to clear interrupt status: %d\n",
1317 static struct attribute *tsl2772_ALS_device_attrs[] = {
1318 &dev_attr_in_illuminance0_target_input.attr,
1319 &dev_attr_in_illuminance0_calibrate.attr,
1320 &dev_attr_in_illuminance0_lux_table.attr,
1324 static struct attribute *tsl2772_PRX_device_attrs[] = {
1325 &dev_attr_in_proximity0_calibrate.attr,
1329 static struct attribute *tsl2772_ALSPRX_device_attrs[] = {
1330 &dev_attr_in_illuminance0_target_input.attr,
1331 &dev_attr_in_illuminance0_calibrate.attr,
1332 &dev_attr_in_illuminance0_lux_table.attr,
1336 static struct attribute *tsl2772_PRX2_device_attrs[] = {
1337 &dev_attr_in_proximity0_calibrate.attr,
1341 static struct attribute *tsl2772_ALSPRX2_device_attrs[] = {
1342 &dev_attr_in_illuminance0_target_input.attr,
1343 &dev_attr_in_illuminance0_calibrate.attr,
1344 &dev_attr_in_illuminance0_lux_table.attr,
1345 &dev_attr_in_proximity0_calibrate.attr,
1349 static const struct attribute_group tsl2772_device_attr_group_tbl[] = {
1351 .attrs = tsl2772_ALS_device_attrs,
1354 .attrs = tsl2772_PRX_device_attrs,
1357 .attrs = tsl2772_ALSPRX_device_attrs,
1360 .attrs = tsl2772_PRX2_device_attrs,
1363 .attrs = tsl2772_ALSPRX2_device_attrs,
1367 #define TSL2772_DEVICE_INFO(type)[type] = \
1369 .attrs = &tsl2772_device_attr_group_tbl[type], \
1370 .read_raw = &tsl2772_read_raw, \
1371 .read_avail = &tsl2772_read_avail, \
1372 .write_raw = &tsl2772_write_raw, \
1373 .read_event_value = &tsl2772_read_event_value, \
1374 .write_event_value = &tsl2772_write_event_value, \
1375 .read_event_config = &tsl2772_read_interrupt_config, \
1376 .write_event_config = &tsl2772_write_interrupt_config, \
1379 static const struct iio_info tsl2772_device_info[] = {
1380 TSL2772_DEVICE_INFO(ALS),
1381 TSL2772_DEVICE_INFO(PRX),
1382 TSL2772_DEVICE_INFO(ALSPRX),
1383 TSL2772_DEVICE_INFO(PRX2),
1384 TSL2772_DEVICE_INFO(ALSPRX2),
1387 static const struct iio_event_spec tsl2772_events[] = {
1389 .type = IIO_EV_TYPE_THRESH,
1390 .dir = IIO_EV_DIR_RISING,
1391 .mask_separate = BIT(IIO_EV_INFO_VALUE),
1393 .type = IIO_EV_TYPE_THRESH,
1394 .dir = IIO_EV_DIR_FALLING,
1395 .mask_separate = BIT(IIO_EV_INFO_VALUE),
1397 .type = IIO_EV_TYPE_THRESH,
1398 .dir = IIO_EV_DIR_EITHER,
1399 .mask_separate = BIT(IIO_EV_INFO_PERIOD) |
1400 BIT(IIO_EV_INFO_ENABLE),
1404 static const struct tsl2772_chip_info tsl2772_chip_info_tbl[] = {
1406 .channel_with_events = {
1411 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1413 .type = IIO_INTENSITY,
1416 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1417 BIT(IIO_CHAN_INFO_INT_TIME) |
1418 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1419 BIT(IIO_CHAN_INFO_CALIBBIAS),
1420 .info_mask_separate_available =
1421 BIT(IIO_CHAN_INFO_INT_TIME) |
1422 BIT(IIO_CHAN_INFO_CALIBSCALE),
1423 .event_spec = tsl2772_events,
1424 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1426 .type = IIO_INTENSITY,
1431 .channel_without_events = {
1436 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1438 .type = IIO_INTENSITY,
1441 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1442 BIT(IIO_CHAN_INFO_INT_TIME) |
1443 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1444 BIT(IIO_CHAN_INFO_CALIBBIAS),
1445 .info_mask_separate_available =
1446 BIT(IIO_CHAN_INFO_INT_TIME) |
1447 BIT(IIO_CHAN_INFO_CALIBSCALE),
1449 .type = IIO_INTENSITY,
1454 .chan_table_elements = 3,
1455 .info = &tsl2772_device_info[ALS],
1458 .channel_with_events = {
1460 .type = IIO_PROXIMITY,
1463 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1464 .event_spec = tsl2772_events,
1465 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1468 .channel_without_events = {
1470 .type = IIO_PROXIMITY,
1473 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1476 .chan_table_elements = 1,
1477 .info = &tsl2772_device_info[PRX],
1480 .channel_with_events = {
1485 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1487 .type = IIO_INTENSITY,
1490 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1491 BIT(IIO_CHAN_INFO_INT_TIME) |
1492 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1493 BIT(IIO_CHAN_INFO_CALIBBIAS),
1494 .info_mask_separate_available =
1495 BIT(IIO_CHAN_INFO_INT_TIME) |
1496 BIT(IIO_CHAN_INFO_CALIBSCALE),
1497 .event_spec = tsl2772_events,
1498 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1500 .type = IIO_INTENSITY,
1503 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1505 .type = IIO_PROXIMITY,
1508 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1509 .event_spec = tsl2772_events,
1510 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1513 .channel_without_events = {
1518 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1520 .type = IIO_INTENSITY,
1523 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1524 BIT(IIO_CHAN_INFO_INT_TIME) |
1525 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1526 BIT(IIO_CHAN_INFO_CALIBBIAS),
1527 .info_mask_separate_available =
1528 BIT(IIO_CHAN_INFO_INT_TIME) |
1529 BIT(IIO_CHAN_INFO_CALIBSCALE),
1531 .type = IIO_INTENSITY,
1534 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1536 .type = IIO_PROXIMITY,
1539 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1542 .chan_table_elements = 4,
1543 .info = &tsl2772_device_info[ALSPRX],
1546 .channel_with_events = {
1548 .type = IIO_PROXIMITY,
1551 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1552 BIT(IIO_CHAN_INFO_CALIBSCALE),
1553 .info_mask_separate_available =
1554 BIT(IIO_CHAN_INFO_CALIBSCALE),
1555 .event_spec = tsl2772_events,
1556 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1559 .channel_without_events = {
1561 .type = IIO_PROXIMITY,
1564 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1565 BIT(IIO_CHAN_INFO_CALIBSCALE),
1566 .info_mask_separate_available =
1567 BIT(IIO_CHAN_INFO_CALIBSCALE),
1570 .chan_table_elements = 1,
1571 .info = &tsl2772_device_info[PRX2],
1574 .channel_with_events = {
1579 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1581 .type = IIO_INTENSITY,
1584 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1585 BIT(IIO_CHAN_INFO_INT_TIME) |
1586 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1587 BIT(IIO_CHAN_INFO_CALIBBIAS),
1588 .info_mask_separate_available =
1589 BIT(IIO_CHAN_INFO_INT_TIME) |
1590 BIT(IIO_CHAN_INFO_CALIBSCALE),
1591 .event_spec = tsl2772_events,
1592 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1594 .type = IIO_INTENSITY,
1597 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1599 .type = IIO_PROXIMITY,
1602 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1603 BIT(IIO_CHAN_INFO_CALIBSCALE),
1604 .info_mask_separate_available =
1605 BIT(IIO_CHAN_INFO_CALIBSCALE),
1606 .event_spec = tsl2772_events,
1607 .num_event_specs = ARRAY_SIZE(tsl2772_events),
1610 .channel_without_events = {
1615 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1617 .type = IIO_INTENSITY,
1620 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1621 BIT(IIO_CHAN_INFO_INT_TIME) |
1622 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1623 BIT(IIO_CHAN_INFO_CALIBBIAS),
1624 .info_mask_separate_available =
1625 BIT(IIO_CHAN_INFO_INT_TIME) |
1626 BIT(IIO_CHAN_INFO_CALIBSCALE),
1628 .type = IIO_INTENSITY,
1631 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1633 .type = IIO_PROXIMITY,
1636 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1637 BIT(IIO_CHAN_INFO_CALIBSCALE),
1638 .info_mask_separate_available =
1639 BIT(IIO_CHAN_INFO_CALIBSCALE),
1642 .chan_table_elements = 4,
1643 .info = &tsl2772_device_info[ALSPRX2],
1647 static int tsl2772_probe(struct i2c_client *clientp,
1648 const struct i2c_device_id *id)
1650 struct iio_dev *indio_dev;
1651 struct tsl2772_chip *chip;
1654 indio_dev = devm_iio_device_alloc(&clientp->dev, sizeof(*chip));
1658 chip = iio_priv(indio_dev);
1659 chip->client = clientp;
1660 i2c_set_clientdata(clientp, indio_dev);
1662 ret = i2c_smbus_read_byte_data(chip->client,
1663 TSL2772_CMD_REG | TSL2772_CHIPID);
1667 if (tsl2772_device_id_verif(ret, id->driver_data) <= 0) {
1668 dev_info(&chip->client->dev,
1669 "%s: i2c device found does not match expected id\n",
1674 ret = i2c_smbus_write_byte(clientp, TSL2772_CMD_REG | TSL2772_CNTRL);
1676 dev_err(&clientp->dev,
1677 "%s: Failed to write to CMD register: %d\n",
1682 mutex_init(&chip->als_mutex);
1683 mutex_init(&chip->prox_mutex);
1685 chip->tsl2772_chip_status = TSL2772_CHIP_UNKNOWN;
1686 chip->pdata = dev_get_platdata(&clientp->dev);
1687 chip->id = id->driver_data;
1689 &tsl2772_chip_info_tbl[device_channel_config[id->driver_data]];
1691 indio_dev->info = chip->chip_info->info;
1692 indio_dev->dev.parent = &clientp->dev;
1693 indio_dev->modes = INDIO_DIRECT_MODE;
1694 indio_dev->name = chip->client->name;
1695 indio_dev->num_channels = chip->chip_info->chan_table_elements;
1698 indio_dev->channels = chip->chip_info->channel_with_events;
1700 ret = devm_request_threaded_irq(&clientp->dev, clientp->irq,
1702 &tsl2772_event_handler,
1703 IRQF_TRIGGER_FALLING |
1708 dev_err(&clientp->dev,
1709 "%s: irq request failed\n", __func__);
1713 indio_dev->channels = chip->chip_info->channel_without_events;
1716 tsl2772_defaults(chip);
1717 ret = tsl2772_chip_on(indio_dev);
1721 ret = devm_add_action_or_reset(&clientp->dev,
1722 tsl2772_chip_off_action,
1727 ret = iio_device_register(indio_dev);
1729 dev_err(&clientp->dev,
1730 "%s: iio registration failed\n", __func__);
1737 static int tsl2772_suspend(struct device *dev)
1739 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1741 return tsl2772_chip_off(indio_dev);
1744 static int tsl2772_resume(struct device *dev)
1746 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1748 return tsl2772_chip_on(indio_dev);
1751 static int tsl2772_remove(struct i2c_client *client)
1753 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1755 iio_device_unregister(indio_dev);
1760 static const struct i2c_device_id tsl2772_idtable[] = {
1761 { "tsl2571", tsl2571 },
1762 { "tsl2671", tsl2671 },
1763 { "tmd2671", tmd2671 },
1764 { "tsl2771", tsl2771 },
1765 { "tmd2771", tmd2771 },
1766 { "tsl2572", tsl2572 },
1767 { "tsl2672", tsl2672 },
1768 { "tmd2672", tmd2672 },
1769 { "tsl2772", tsl2772 },
1770 { "tmd2772", tmd2772 },
1774 MODULE_DEVICE_TABLE(i2c, tsl2772_idtable);
1776 static const struct of_device_id tsl2772_of_match[] = {
1777 { .compatible = "amstaos,tsl2571" },
1778 { .compatible = "amstaos,tsl2671" },
1779 { .compatible = "amstaos,tmd2671" },
1780 { .compatible = "amstaos,tsl2771" },
1781 { .compatible = "amstaos,tmd2771" },
1782 { .compatible = "amstaos,tsl2572" },
1783 { .compatible = "amstaos,tsl2672" },
1784 { .compatible = "amstaos,tmd2672" },
1785 { .compatible = "amstaos,tsl2772" },
1786 { .compatible = "amstaos,tmd2772" },
1789 MODULE_DEVICE_TABLE(of, tsl2772_of_match);
1791 static const struct dev_pm_ops tsl2772_pm_ops = {
1792 .suspend = tsl2772_suspend,
1793 .resume = tsl2772_resume,
1796 static struct i2c_driver tsl2772_driver = {
1799 .of_match_table = tsl2772_of_match,
1800 .pm = &tsl2772_pm_ops,
1802 .id_table = tsl2772_idtable,
1803 .probe = tsl2772_probe,
1804 .remove = tsl2772_remove,
1807 module_i2c_driver(tsl2772_driver);
1809 MODULE_AUTHOR("J. August Brenner <Jon.Brenner@ams.com>");
1810 MODULE_AUTHOR("Brian Masney <masneyb@onstation.org>");
1811 MODULE_DESCRIPTION("TAOS tsl2772 ambient and proximity light sensor driver");
1812 MODULE_LICENSE("GPL");