iio: tsl2772: Use devm_add_action_or_reset for tsl2772_chip_off
[platform/kernel/linux-rpi.git] / drivers / iio / light / tsl2772.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
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.
6  *
7  * Copyright (c) 2012, TAOS Corporation.
8  * Copyright (c) 2017-2018 Brian Masney <masneyb@onstation.org>
9  */
10
11 #include <linux/delay.h>
12 #include <linux/errno.h>
13 #include <linux/i2c.h>
14 #include <linux/interrupt.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/slab.h>
19 #include <linux/iio/events.h>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/sysfs.h>
22 #include <linux/platform_data/tsl2772.h>
23 #include <linux/regulator/consumer.h>
24
25 /* Cal defs */
26 #define PROX_STAT_CAL                   0
27 #define PROX_STAT_SAMP                  1
28 #define MAX_SAMPLES_CAL                 200
29
30 /* TSL2772 Device ID */
31 #define TRITON_ID                       0x00
32 #define SWORDFISH_ID                    0x30
33 #define HALIBUT_ID                      0x20
34
35 /* Lux calculation constants */
36 #define TSL2772_LUX_CALC_OVER_FLOW      65535
37
38 /*
39  * TAOS Register definitions - Note: depending on device, some of these register
40  * are not used and the register address is benign.
41  */
42
43 /* Register offsets */
44 #define TSL2772_MAX_CONFIG_REG          16
45
46 /* Device Registers and Masks */
47 #define TSL2772_CNTRL                   0x00
48 #define TSL2772_ALS_TIME                0X01
49 #define TSL2772_PRX_TIME                0x02
50 #define TSL2772_WAIT_TIME               0x03
51 #define TSL2772_ALS_MINTHRESHLO         0X04
52 #define TSL2772_ALS_MINTHRESHHI         0X05
53 #define TSL2772_ALS_MAXTHRESHLO         0X06
54 #define TSL2772_ALS_MAXTHRESHHI         0X07
55 #define TSL2772_PRX_MINTHRESHLO         0X08
56 #define TSL2772_PRX_MINTHRESHHI         0X09
57 #define TSL2772_PRX_MAXTHRESHLO         0X0A
58 #define TSL2772_PRX_MAXTHRESHHI         0X0B
59 #define TSL2772_PERSISTENCE             0x0C
60 #define TSL2772_ALS_PRX_CONFIG          0x0D
61 #define TSL2772_PRX_COUNT               0x0E
62 #define TSL2772_GAIN                    0x0F
63 #define TSL2772_NOTUSED                 0x10
64 #define TSL2772_REVID                   0x11
65 #define TSL2772_CHIPID                  0x12
66 #define TSL2772_STATUS                  0x13
67 #define TSL2772_ALS_CHAN0LO             0x14
68 #define TSL2772_ALS_CHAN0HI             0x15
69 #define TSL2772_ALS_CHAN1LO             0x16
70 #define TSL2772_ALS_CHAN1HI             0x17
71 #define TSL2772_PRX_LO                  0x18
72 #define TSL2772_PRX_HI                  0x19
73
74 /* tsl2772 cmd reg masks */
75 #define TSL2772_CMD_REG                 0x80
76 #define TSL2772_CMD_SPL_FN              0x60
77 #define TSL2772_CMD_REPEAT_PROTO        0x00
78 #define TSL2772_CMD_AUTOINC_PROTO       0x20
79
80 #define TSL2772_CMD_PROX_INT_CLR        0X05
81 #define TSL2772_CMD_ALS_INT_CLR         0x06
82 #define TSL2772_CMD_PROXALS_INT_CLR     0X07
83
84 /* tsl2772 cntrl reg masks */
85 #define TSL2772_CNTL_ADC_ENBL           0x02
86 #define TSL2772_CNTL_PWR_ON             0x01
87
88 /* tsl2772 status reg masks */
89 #define TSL2772_STA_ADC_VALID           0x01
90 #define TSL2772_STA_PRX_VALID           0x02
91 #define TSL2772_STA_ADC_PRX_VALID       (TSL2772_STA_ADC_VALID | \
92                                          TSL2772_STA_PRX_VALID)
93 #define TSL2772_STA_ALS_INTR            0x10
94 #define TSL2772_STA_PRX_INTR            0x20
95
96 /* tsl2772 cntrl reg masks */
97 #define TSL2772_CNTL_REG_CLEAR          0x00
98 #define TSL2772_CNTL_PROX_INT_ENBL      0X20
99 #define TSL2772_CNTL_ALS_INT_ENBL       0X10
100 #define TSL2772_CNTL_WAIT_TMR_ENBL      0X08
101 #define TSL2772_CNTL_PROX_DET_ENBL      0X04
102 #define TSL2772_CNTL_PWRON              0x01
103 #define TSL2772_CNTL_ALSPON_ENBL        0x03
104 #define TSL2772_CNTL_INTALSPON_ENBL     0x13
105 #define TSL2772_CNTL_PROXPON_ENBL       0x0F
106 #define TSL2772_CNTL_INTPROXPON_ENBL    0x2F
107
108 #define TSL2772_ALS_GAIN_TRIM_MIN       250
109 #define TSL2772_ALS_GAIN_TRIM_MAX       4000
110
111 #define TSL2772_MAX_PROX_LEDS           2
112
113 #define TSL2772_BOOT_MIN_SLEEP_TIME     10000
114 #define TSL2772_BOOT_MAX_SLEEP_TIME     28000
115
116 /* Device family members */
117 enum {
118         tsl2571,
119         tsl2671,
120         tmd2671,
121         tsl2771,
122         tmd2771,
123         tsl2572,
124         tsl2672,
125         tmd2672,
126         tsl2772,
127         tmd2772,
128         apds9930,
129 };
130
131 enum {
132         TSL2772_CHIP_UNKNOWN = 0,
133         TSL2772_CHIP_WORKING = 1,
134         TSL2772_CHIP_SUSPENDED = 2
135 };
136
137 /* Per-device data */
138 struct tsl2772_als_info {
139         u16 als_ch0;
140         u16 als_ch1;
141         u16 lux;
142 };
143
144 struct tsl2772_chip_info {
145         int chan_table_elements;
146         struct iio_chan_spec channel_with_events[4];
147         struct iio_chan_spec channel_without_events[4];
148         const struct iio_info *info;
149 };
150
151 static const int tsl2772_led_currents[][2] = {
152         { 100000, TSL2772_100_mA },
153         {  50000, TSL2772_50_mA },
154         {  25000, TSL2772_25_mA },
155         {  13000, TSL2772_13_mA },
156         {      0, 0 }
157 };
158
159 struct tsl2772_chip {
160         kernel_ulong_t id;
161         struct mutex prox_mutex;
162         struct mutex als_mutex;
163         struct i2c_client *client;
164         struct regulator *vdd_supply;
165         struct regulator *vddio_supply;
166         u16 prox_data;
167         struct tsl2772_als_info als_cur_info;
168         struct tsl2772_settings settings;
169         struct tsl2772_platform_data *pdata;
170         int als_gain_time_scale;
171         int als_saturation;
172         int tsl2772_chip_status;
173         u8 tsl2772_config[TSL2772_MAX_CONFIG_REG];
174         const struct tsl2772_chip_info  *chip_info;
175         const struct iio_info *info;
176         s64 event_timestamp;
177         /*
178          * This structure is intentionally large to accommodate
179          * updates via sysfs.
180          * Sized to 9 = max 8 segments + 1 termination segment
181          */
182         struct tsl2772_lux tsl2772_device_lux[TSL2772_MAX_LUX_TABLE_SIZE];
183 };
184
185 /*
186  * Different devices require different coefficents, and these numbers were
187  * derived from the 'Lux Equation' section of the various device datasheets.
188  * All of these coefficients assume a Glass Attenuation (GA) factor of 1.
189  * The coefficients are multiplied by 1000 to avoid floating point operations.
190  * The two rows in each table correspond to the Lux1 and Lux2 equations from
191  * the datasheets.
192  */
193 static const struct tsl2772_lux tsl2x71_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
194         { 53000, 106000 },
195         { 31800,  53000 },
196         { 0,          0 },
197 };
198
199 static const struct tsl2772_lux tmd2x71_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
200         { 24000,  48000 },
201         { 14400,  24000 },
202         { 0,          0 },
203 };
204
205 static const struct tsl2772_lux tsl2x72_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
206         { 60000, 112200 },
207         { 37800,  60000 },
208         {     0,      0 },
209 };
210
211 static const struct tsl2772_lux tmd2x72_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
212         { 20000,  35000 },
213         { 12600,  20000 },
214         {     0,      0 },
215 };
216
217 static const struct tsl2772_lux apds9930_lux_table[TSL2772_DEF_LUX_TABLE_SZ] = {
218         { 52000,  96824 },
219         { 38792,  67132 },
220         {     0,      0 },
221 };
222
223 static const struct tsl2772_lux *tsl2772_default_lux_table_group[] = {
224         [tsl2571] = tsl2x71_lux_table,
225         [tsl2671] = tsl2x71_lux_table,
226         [tmd2671] = tmd2x71_lux_table,
227         [tsl2771] = tsl2x71_lux_table,
228         [tmd2771] = tmd2x71_lux_table,
229         [tsl2572] = tsl2x72_lux_table,
230         [tsl2672] = tsl2x72_lux_table,
231         [tmd2672] = tmd2x72_lux_table,
232         [tsl2772] = tsl2x72_lux_table,
233         [tmd2772] = tmd2x72_lux_table,
234         [apds9930] = apds9930_lux_table,
235 };
236
237 static const struct tsl2772_settings tsl2772_default_settings = {
238         .als_time = 255, /* 2.72 / 2.73 ms */
239         .als_gain = 0,
240         .prox_time = 255, /* 2.72 / 2.73 ms */
241         .prox_gain = 0,
242         .wait_time = 255,
243         .als_prox_config = 0,
244         .als_gain_trim = 1000,
245         .als_cal_target = 150,
246         .als_persistence = 1,
247         .als_interrupt_en = false,
248         .als_thresh_low = 200,
249         .als_thresh_high = 256,
250         .prox_persistence = 1,
251         .prox_interrupt_en = false,
252         .prox_thres_low  = 0,
253         .prox_thres_high = 512,
254         .prox_max_samples_cal = 30,
255         .prox_pulse_count = 8,
256         .prox_diode = TSL2772_DIODE1,
257         .prox_power = TSL2772_100_mA
258 };
259
260 static const s16 tsl2772_als_gain[] = {
261         1,
262         8,
263         16,
264         120
265 };
266
267 static const s16 tsl2772_prox_gain[] = {
268         1,
269         2,
270         4,
271         8
272 };
273
274 static const int tsl2772_int_time_avail[][6] = {
275         [tsl2571] = { 0, 2720, 0, 2720, 0, 696000 },
276         [tsl2671] = { 0, 2720, 0, 2720, 0, 696000 },
277         [tmd2671] = { 0, 2720, 0, 2720, 0, 696000 },
278         [tsl2771] = { 0, 2720, 0, 2720, 0, 696000 },
279         [tmd2771] = { 0, 2720, 0, 2720, 0, 696000 },
280         [tsl2572] = { 0, 2730, 0, 2730, 0, 699000 },
281         [tsl2672] = { 0, 2730, 0, 2730, 0, 699000 },
282         [tmd2672] = { 0, 2730, 0, 2730, 0, 699000 },
283         [tsl2772] = { 0, 2730, 0, 2730, 0, 699000 },
284         [tmd2772] = { 0, 2730, 0, 2730, 0, 699000 },
285         [apds9930] = { 0, 2730, 0, 2730, 0, 699000 },
286 };
287
288 static int tsl2772_int_calibscale_avail[] = { 1, 8, 16, 120 };
289
290 static int tsl2772_prox_calibscale_avail[] = { 1, 2, 4, 8 };
291
292 /* Channel variations */
293 enum {
294         ALS,
295         PRX,
296         ALSPRX,
297         PRX2,
298         ALSPRX2,
299 };
300
301 static const u8 device_channel_config[] = {
302         [tsl2571] = ALS,
303         [tsl2671] = PRX,
304         [tmd2671] = PRX,
305         [tsl2771] = ALSPRX,
306         [tmd2771] = ALSPRX,
307         [tsl2572] = ALS,
308         [tsl2672] = PRX2,
309         [tmd2672] = PRX2,
310         [tsl2772] = ALSPRX2,
311         [tmd2772] = ALSPRX2,
312         [apds9930] = ALSPRX2,
313 };
314
315 static int tsl2772_read_status(struct tsl2772_chip *chip)
316 {
317         int ret;
318
319         ret = i2c_smbus_read_byte_data(chip->client,
320                                        TSL2772_CMD_REG | TSL2772_STATUS);
321         if (ret < 0)
322                 dev_err(&chip->client->dev,
323                         "%s: failed to read STATUS register: %d\n", __func__,
324                         ret);
325
326         return ret;
327 }
328
329 static int tsl2772_write_control_reg(struct tsl2772_chip *chip, u8 data)
330 {
331         int ret;
332
333         ret = i2c_smbus_write_byte_data(chip->client,
334                                         TSL2772_CMD_REG | TSL2772_CNTRL, data);
335         if (ret < 0) {
336                 dev_err(&chip->client->dev,
337                         "%s: failed to write to control register %x: %d\n",
338                         __func__, data, ret);
339         }
340
341         return ret;
342 }
343
344 static int tsl2772_read_autoinc_regs(struct tsl2772_chip *chip, int lower_reg,
345                                      int upper_reg)
346 {
347         u8 buf[2];
348         int ret;
349
350         ret = i2c_smbus_write_byte(chip->client,
351                                    TSL2772_CMD_REG | TSL2772_CMD_AUTOINC_PROTO |
352                                    lower_reg);
353         if (ret < 0) {
354                 dev_err(&chip->client->dev,
355                         "%s: failed to enable auto increment protocol: %d\n",
356                         __func__, ret);
357                 return ret;
358         }
359
360         ret = i2c_smbus_read_byte_data(chip->client,
361                                        TSL2772_CMD_REG | lower_reg);
362         if (ret < 0) {
363                 dev_err(&chip->client->dev,
364                         "%s: failed to read from register %x: %d\n", __func__,
365                         lower_reg, ret);
366                 return ret;
367         }
368         buf[0] = ret;
369
370         ret = i2c_smbus_read_byte_data(chip->client,
371                                        TSL2772_CMD_REG | upper_reg);
372         if (ret < 0) {
373                 dev_err(&chip->client->dev,
374                         "%s: failed to read from register %x: %d\n", __func__,
375                         upper_reg, ret);
376                 return ret;
377         }
378         buf[1] = ret;
379
380         ret = i2c_smbus_write_byte(chip->client,
381                                    TSL2772_CMD_REG | TSL2772_CMD_REPEAT_PROTO |
382                                    lower_reg);
383         if (ret < 0) {
384                 dev_err(&chip->client->dev,
385                         "%s: failed to enable repeated byte protocol: %d\n",
386                         __func__, ret);
387                 return ret;
388         }
389
390         return le16_to_cpup((const __le16 *)&buf[0]);
391 }
392
393 /**
394  * tsl2772_get_lux() - Reads and calculates current lux value.
395  * @indio_dev:  pointer to IIO device
396  *
397  * The raw ch0 and ch1 values of the ambient light sensed in the last
398  * integration cycle are read from the device. The raw values are multiplied
399  * by a device-specific scale factor, and divided by the integration time and
400  * device gain. The code supports multiple lux equations through the lux table
401  * coefficients. A lux gain trim is applied to each lux equation, and then the
402  * maximum lux within the interval 0..65535 is selected.
403  */
404 static int tsl2772_get_lux(struct iio_dev *indio_dev)
405 {
406         struct tsl2772_chip *chip = iio_priv(indio_dev);
407         struct tsl2772_lux *p;
408         int max_lux, ret;
409         bool overflow;
410
411         mutex_lock(&chip->als_mutex);
412
413         if (chip->tsl2772_chip_status != TSL2772_CHIP_WORKING) {
414                 dev_err(&chip->client->dev, "%s: device is not enabled\n",
415                         __func__);
416                 ret = -EBUSY;
417                 goto out_unlock;
418         }
419
420         ret = tsl2772_read_status(chip);
421         if (ret < 0)
422                 goto out_unlock;
423
424         if (!(ret & TSL2772_STA_ADC_VALID)) {
425                 dev_err(&chip->client->dev,
426                         "%s: data not valid yet\n", __func__);
427                 ret = chip->als_cur_info.lux; /* return LAST VALUE */
428                 goto out_unlock;
429         }
430
431         ret = tsl2772_read_autoinc_regs(chip, TSL2772_ALS_CHAN0LO,
432                                         TSL2772_ALS_CHAN0HI);
433         if (ret < 0)
434                 goto out_unlock;
435         chip->als_cur_info.als_ch0 = ret;
436
437         ret = tsl2772_read_autoinc_regs(chip, TSL2772_ALS_CHAN1LO,
438                                         TSL2772_ALS_CHAN1HI);
439         if (ret < 0)
440                 goto out_unlock;
441         chip->als_cur_info.als_ch1 = ret;
442
443         if (chip->als_cur_info.als_ch0 >= chip->als_saturation) {
444                 max_lux = TSL2772_LUX_CALC_OVER_FLOW;
445                 goto update_struct_with_max_lux;
446         }
447
448         if (!chip->als_cur_info.als_ch0) {
449                 /* have no data, so return LAST VALUE */
450                 ret = chip->als_cur_info.lux;
451                 goto out_unlock;
452         }
453
454         max_lux = 0;
455         overflow = false;
456         for (p = (struct tsl2772_lux *)chip->tsl2772_device_lux; p->ch0 != 0;
457              p++) {
458                 int lux;
459
460                 lux = ((chip->als_cur_info.als_ch0 * p->ch0) -
461                        (chip->als_cur_info.als_ch1 * p->ch1)) /
462                         chip->als_gain_time_scale;
463
464                 /*
465                  * The als_gain_trim can have a value within the range 250..4000
466                  * and is a multiplier for the lux. A trim of 1000 makes no
467                  * changes to the lux, less than 1000 scales it down, and
468                  * greater than 1000 scales it up.
469                  */
470                 lux = (lux * chip->settings.als_gain_trim) / 1000;
471
472                 if (lux > TSL2772_LUX_CALC_OVER_FLOW) {
473                         overflow = true;
474                         continue;
475                 }
476
477                 max_lux = max(max_lux, lux);
478         }
479
480         if (overflow && max_lux == 0)
481                 max_lux = TSL2772_LUX_CALC_OVER_FLOW;
482
483 update_struct_with_max_lux:
484         chip->als_cur_info.lux = max_lux;
485         ret = max_lux;
486
487 out_unlock:
488         mutex_unlock(&chip->als_mutex);
489
490         return ret;
491 }
492
493 /**
494  * tsl2772_get_prox() - Reads proximity data registers and updates
495  *                      chip->prox_data.
496  *
497  * @indio_dev:  pointer to IIO device
498  */
499 static int tsl2772_get_prox(struct iio_dev *indio_dev)
500 {
501         struct tsl2772_chip *chip = iio_priv(indio_dev);
502         int ret;
503
504         mutex_lock(&chip->prox_mutex);
505
506         ret = tsl2772_read_status(chip);
507         if (ret < 0)
508                 goto prox_poll_err;
509
510         switch (chip->id) {
511         case tsl2571:
512         case tsl2671:
513         case tmd2671:
514         case tsl2771:
515         case tmd2771:
516                 if (!(ret & TSL2772_STA_ADC_VALID)) {
517                         ret = -EINVAL;
518                         goto prox_poll_err;
519                 }
520                 break;
521         case tsl2572:
522         case tsl2672:
523         case tmd2672:
524         case tsl2772:
525         case tmd2772:
526         case apds9930:
527                 if (!(ret & TSL2772_STA_PRX_VALID)) {
528                         ret = -EINVAL;
529                         goto prox_poll_err;
530                 }
531                 break;
532         }
533
534         ret = tsl2772_read_autoinc_regs(chip, TSL2772_PRX_LO, TSL2772_PRX_HI);
535         if (ret < 0)
536                 goto prox_poll_err;
537         chip->prox_data = ret;
538
539 prox_poll_err:
540         mutex_unlock(&chip->prox_mutex);
541
542         return ret;
543 }
544
545 static int tsl2772_read_prox_led_current(struct tsl2772_chip *chip)
546 {
547         struct device_node *of_node = chip->client->dev.of_node;
548         int ret, tmp, i;
549
550         ret = of_property_read_u32(of_node, "led-max-microamp", &tmp);
551         if (ret < 0)
552                 return ret;
553
554         for (i = 0; tsl2772_led_currents[i][0] != 0; i++) {
555                 if (tmp == tsl2772_led_currents[i][0]) {
556                         chip->settings.prox_power = tsl2772_led_currents[i][1];
557                         return 0;
558                 }
559         }
560
561         dev_err(&chip->client->dev, "Invalid value %d for led-max-microamp\n",
562                 tmp);
563
564         return -EINVAL;
565
566 }
567
568 static int tsl2772_read_prox_diodes(struct tsl2772_chip *chip)
569 {
570         struct device_node *of_node = chip->client->dev.of_node;
571         int i, ret, num_leds, prox_diode_mask;
572         u32 leds[TSL2772_MAX_PROX_LEDS];
573
574         ret = of_property_count_u32_elems(of_node, "amstaos,proximity-diodes");
575         if (ret < 0)
576                 return ret;
577
578         num_leds = ret;
579         if (num_leds > TSL2772_MAX_PROX_LEDS)
580                 num_leds = TSL2772_MAX_PROX_LEDS;
581
582         ret = of_property_read_u32_array(of_node, "amstaos,proximity-diodes",
583                                          leds, num_leds);
584         if (ret < 0) {
585                 dev_err(&chip->client->dev,
586                         "Invalid value for amstaos,proximity-diodes: %d.\n",
587                         ret);
588                 return ret;
589         }
590
591         prox_diode_mask = 0;
592         for (i = 0; i < num_leds; i++) {
593                 if (leds[i] == 0)
594                         prox_diode_mask |= TSL2772_DIODE0;
595                 else if (leds[i] == 1)
596                         prox_diode_mask |= TSL2772_DIODE1;
597                 else {
598                         dev_err(&chip->client->dev,
599                                 "Invalid value %d in amstaos,proximity-diodes.\n",
600                                 leds[i]);
601                         return -EINVAL;
602                 }
603         }
604
605         return 0;
606 }
607
608 static void tsl2772_parse_dt(struct tsl2772_chip *chip)
609 {
610         tsl2772_read_prox_led_current(chip);
611         tsl2772_read_prox_diodes(chip);
612 }
613
614 /**
615  * tsl2772_defaults() - Populates the device nominal operating parameters
616  *                      with those provided by a 'platform' data struct or
617  *                      with prefined defaults.
618  *
619  * @chip:               pointer to device structure.
620  */
621 static void tsl2772_defaults(struct tsl2772_chip *chip)
622 {
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));
627         else
628                 memcpy(&chip->settings, &tsl2772_default_settings,
629                        sizeof(tsl2772_default_settings));
630
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));
636         else
637                 memcpy(chip->tsl2772_device_lux,
638                        tsl2772_default_lux_table_group[chip->id],
639                        TSL2772_DEFAULT_TABLE_BYTES);
640
641         tsl2772_parse_dt(chip);
642 }
643
644 /**
645  * tsl2772_als_calibrate() -    Obtain single reading and calculate
646  *                              the als_gain_trim.
647  *
648  * @indio_dev:  pointer to IIO device
649  */
650 static int tsl2772_als_calibrate(struct iio_dev *indio_dev)
651 {
652         struct tsl2772_chip *chip = iio_priv(indio_dev);
653         int ret, lux_val;
654
655         ret = i2c_smbus_read_byte_data(chip->client,
656                                        TSL2772_CMD_REG | TSL2772_CNTRL);
657         if (ret < 0) {
658                 dev_err(&chip->client->dev,
659                         "%s: failed to read from the CNTRL register\n",
660                         __func__);
661                 return ret;
662         }
663
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",
668                         __func__);
669                 return -EINVAL;
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",
673                         __func__);
674                 return -ENODATA;
675         }
676
677         lux_val = tsl2772_get_lux(indio_dev);
678         if (lux_val < 0) {
679                 dev_err(&chip->client->dev,
680                         "%s: failed to get lux\n", __func__);
681                 return lux_val;
682         }
683         if (lux_val == 0)
684                 return -ERANGE;
685
686         ret = (chip->settings.als_cal_target * chip->settings.als_gain_trim) /
687                         lux_val;
688         if (ret < TSL2772_ALS_GAIN_TRIM_MIN || ret > TSL2772_ALS_GAIN_TRIM_MAX)
689                 return -ERANGE;
690
691         chip->settings.als_gain_trim = ret;
692
693         return ret;
694 }
695
696 static void tsl2772_disable_regulators_action(void *_data)
697 {
698         struct tsl2772_chip *chip = _data;
699
700         regulator_disable(chip->vdd_supply);
701         regulator_disable(chip->vddio_supply);
702 }
703
704 static int tsl2772_enable_regulator(struct tsl2772_chip *chip,
705                                     struct regulator *regulator)
706 {
707         int ret;
708
709         ret = regulator_enable(regulator);
710         if (ret < 0) {
711                 dev_err(&chip->client->dev, "Failed to enable regulator: %d\n",
712                         ret);
713                 return ret;
714         }
715
716         return 0;
717 }
718
719 static struct regulator *tsl2772_get_regulator(struct tsl2772_chip *chip,
720                                                char *name)
721 {
722         struct regulator *regulator;
723         int ret;
724
725         regulator = devm_regulator_get(&chip->client->dev, name);
726         if (IS_ERR(regulator)) {
727                 if (PTR_ERR(regulator) != -EPROBE_DEFER)
728                         dev_err(&chip->client->dev,
729                                 "Failed to get %s regulator %d\n",
730                                 name, (int)PTR_ERR(regulator));
731
732                 return regulator;
733         }
734
735         ret = tsl2772_enable_regulator(chip, regulator);
736         if (ret < 0)
737                 return ERR_PTR(ret);
738
739         return regulator;
740 }
741
742 static int tsl2772_chip_on(struct iio_dev *indio_dev)
743 {
744         struct tsl2772_chip *chip = iio_priv(indio_dev);
745         int ret, i, als_count, als_time_us;
746         u8 *dev_reg, reg_val;
747
748         /* Non calculated parameters */
749         chip->tsl2772_config[TSL2772_ALS_TIME] = chip->settings.als_time;
750         chip->tsl2772_config[TSL2772_PRX_TIME] = chip->settings.prox_time;
751         chip->tsl2772_config[TSL2772_WAIT_TIME] = chip->settings.wait_time;
752         chip->tsl2772_config[TSL2772_ALS_PRX_CONFIG] =
753                 chip->settings.als_prox_config;
754
755         chip->tsl2772_config[TSL2772_ALS_MINTHRESHLO] =
756                 (chip->settings.als_thresh_low) & 0xFF;
757         chip->tsl2772_config[TSL2772_ALS_MINTHRESHHI] =
758                 (chip->settings.als_thresh_low >> 8) & 0xFF;
759         chip->tsl2772_config[TSL2772_ALS_MAXTHRESHLO] =
760                 (chip->settings.als_thresh_high) & 0xFF;
761         chip->tsl2772_config[TSL2772_ALS_MAXTHRESHHI] =
762                 (chip->settings.als_thresh_high >> 8) & 0xFF;
763         chip->tsl2772_config[TSL2772_PERSISTENCE] =
764                 (chip->settings.prox_persistence & 0xFF) << 4 |
765                 (chip->settings.als_persistence & 0xFF);
766
767         chip->tsl2772_config[TSL2772_PRX_COUNT] =
768                         chip->settings.prox_pulse_count;
769         chip->tsl2772_config[TSL2772_PRX_MINTHRESHLO] =
770                         (chip->settings.prox_thres_low) & 0xFF;
771         chip->tsl2772_config[TSL2772_PRX_MINTHRESHHI] =
772                         (chip->settings.prox_thres_low >> 8) & 0xFF;
773         chip->tsl2772_config[TSL2772_PRX_MAXTHRESHLO] =
774                         (chip->settings.prox_thres_high) & 0xFF;
775         chip->tsl2772_config[TSL2772_PRX_MAXTHRESHHI] =
776                         (chip->settings.prox_thres_high >> 8) & 0xFF;
777
778         /* and make sure we're not already on */
779         if (chip->tsl2772_chip_status == TSL2772_CHIP_WORKING) {
780                 /* if forcing a register update - turn off, then on */
781                 dev_info(&chip->client->dev, "device is already enabled\n");
782                 return -EINVAL;
783         }
784
785         /* Set the gain based on tsl2772_settings struct */
786         chip->tsl2772_config[TSL2772_GAIN] =
787                 (chip->settings.als_gain & 0xFF) |
788                 ((chip->settings.prox_gain & 0xFF) << 2) |
789                 (chip->settings.prox_diode << 4) |
790                 (chip->settings.prox_power << 6);
791
792         /* set chip time scaling and saturation */
793         als_count = 256 - chip->settings.als_time;
794         als_time_us = als_count * tsl2772_int_time_avail[chip->id][3];
795         chip->als_saturation = als_count * 768; /* 75% of full scale */
796         chip->als_gain_time_scale = als_time_us *
797                 tsl2772_als_gain[chip->settings.als_gain];
798
799         /*
800          * TSL2772 Specific power-on / adc enable sequence
801          * Power on the device 1st.
802          */
803         ret = tsl2772_write_control_reg(chip, TSL2772_CNTL_PWR_ON);
804         if (ret < 0)
805                 return ret;
806
807         /*
808          * Use the following shadow copy for our delay before enabling ADC.
809          * Write all the registers.
810          */
811         for (i = 0, dev_reg = chip->tsl2772_config;
812                         i < TSL2772_MAX_CONFIG_REG; i++) {
813                 int reg = TSL2772_CMD_REG + i;
814
815                 ret = i2c_smbus_write_byte_data(chip->client, reg,
816                                                 *dev_reg++);
817                 if (ret < 0) {
818                         dev_err(&chip->client->dev,
819                                 "%s: failed to write to register %x: %d\n",
820                                 __func__, reg, ret);
821                         return ret;
822                 }
823         }
824
825         /* Power-on settling time */
826         usleep_range(3000, 3500);
827
828         reg_val = TSL2772_CNTL_PWR_ON | TSL2772_CNTL_ADC_ENBL |
829                   TSL2772_CNTL_PROX_DET_ENBL;
830         if (chip->settings.als_interrupt_en)
831                 reg_val |= TSL2772_CNTL_ALS_INT_ENBL;
832         if (chip->settings.prox_interrupt_en)
833                 reg_val |= TSL2772_CNTL_PROX_INT_ENBL;
834
835         ret = tsl2772_write_control_reg(chip, reg_val);
836         if (ret < 0)
837                 return ret;
838
839         ret = i2c_smbus_write_byte(chip->client,
840                                    TSL2772_CMD_REG | TSL2772_CMD_SPL_FN |
841                                    TSL2772_CMD_PROXALS_INT_CLR);
842         if (ret < 0) {
843                 dev_err(&chip->client->dev,
844                         "%s: failed to clear interrupt status: %d\n",
845                         __func__, ret);
846                 return ret;
847         }
848
849         chip->tsl2772_chip_status = TSL2772_CHIP_WORKING;
850
851         return ret;
852 }
853
854 static int tsl2772_chip_off(struct iio_dev *indio_dev)
855 {
856         struct tsl2772_chip *chip = iio_priv(indio_dev);
857
858         /* turn device off */
859         chip->tsl2772_chip_status = TSL2772_CHIP_SUSPENDED;
860         return tsl2772_write_control_reg(chip, 0x00);
861 }
862
863 static void tsl2772_chip_off_action(void *data)
864 {
865         struct iio_dev *indio_dev = data;
866
867         tsl2772_chip_off(indio_dev);
868 }
869
870 /**
871  * tsl2772_invoke_change - power cycle the device to implement the user
872  *                         parameters
873  * @indio_dev:  pointer to IIO device
874  *
875  * Obtain and lock both ALS and PROX resources, determine and save device state
876  * (On/Off), cycle device to implement updated parameter, put device back into
877  * proper state, and unlock resource.
878  */
879 static int tsl2772_invoke_change(struct iio_dev *indio_dev)
880 {
881         struct tsl2772_chip *chip = iio_priv(indio_dev);
882         int device_status = chip->tsl2772_chip_status;
883         int ret;
884
885         mutex_lock(&chip->als_mutex);
886         mutex_lock(&chip->prox_mutex);
887
888         if (device_status == TSL2772_CHIP_WORKING) {
889                 ret = tsl2772_chip_off(indio_dev);
890                 if (ret < 0)
891                         goto unlock;
892         }
893
894         ret = tsl2772_chip_on(indio_dev);
895
896 unlock:
897         mutex_unlock(&chip->prox_mutex);
898         mutex_unlock(&chip->als_mutex);
899
900         return ret;
901 }
902
903 static int tsl2772_prox_cal(struct iio_dev *indio_dev)
904 {
905         struct tsl2772_chip *chip = iio_priv(indio_dev);
906         int prox_history[MAX_SAMPLES_CAL + 1];
907         int i, ret, mean, max, sample_sum;
908
909         if (chip->settings.prox_max_samples_cal < 1 ||
910             chip->settings.prox_max_samples_cal > MAX_SAMPLES_CAL)
911                 return -EINVAL;
912
913         for (i = 0; i < chip->settings.prox_max_samples_cal; i++) {
914                 usleep_range(15000, 17500);
915                 ret = tsl2772_get_prox(indio_dev);
916                 if (ret < 0)
917                         return ret;
918
919                 prox_history[i] = chip->prox_data;
920         }
921
922         sample_sum = 0;
923         max = INT_MIN;
924         for (i = 0; i < chip->settings.prox_max_samples_cal; i++) {
925                 sample_sum += prox_history[i];
926                 max = max(max, prox_history[i]);
927         }
928         mean = sample_sum / chip->settings.prox_max_samples_cal;
929
930         chip->settings.prox_thres_high = (max << 1) - mean;
931
932         return tsl2772_invoke_change(indio_dev);
933 }
934
935 static int tsl2772_read_avail(struct iio_dev *indio_dev,
936                               struct iio_chan_spec const *chan,
937                               const int **vals, int *type, int *length,
938                               long mask)
939 {
940         struct tsl2772_chip *chip = iio_priv(indio_dev);
941
942         switch (mask) {
943         case IIO_CHAN_INFO_CALIBSCALE:
944                 if (chan->type == IIO_INTENSITY) {
945                         *length = ARRAY_SIZE(tsl2772_int_calibscale_avail);
946                         *vals = tsl2772_int_calibscale_avail;
947                 } else {
948                         *length = ARRAY_SIZE(tsl2772_prox_calibscale_avail);
949                         *vals = tsl2772_prox_calibscale_avail;
950                 }
951                 *type = IIO_VAL_INT;
952                 return IIO_AVAIL_LIST;
953         case IIO_CHAN_INFO_INT_TIME:
954                 *length = ARRAY_SIZE(tsl2772_int_time_avail[chip->id]);
955                 *vals = tsl2772_int_time_avail[chip->id];
956                 *type = IIO_VAL_INT_PLUS_MICRO;
957                 return IIO_AVAIL_RANGE;
958         }
959
960         return -EINVAL;
961 }
962
963 static ssize_t in_illuminance0_target_input_show(struct device *dev,
964                                                  struct device_attribute *attr,
965                                                  char *buf)
966 {
967         struct tsl2772_chip *chip = iio_priv(dev_to_iio_dev(dev));
968
969         return snprintf(buf, PAGE_SIZE, "%d\n", chip->settings.als_cal_target);
970 }
971
972 static ssize_t in_illuminance0_target_input_store(struct device *dev,
973                                                   struct device_attribute *attr,
974                                                   const char *buf, size_t len)
975 {
976         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
977         struct tsl2772_chip *chip = iio_priv(indio_dev);
978         u16 value;
979         int ret;
980
981         if (kstrtou16(buf, 0, &value))
982                 return -EINVAL;
983
984         chip->settings.als_cal_target = value;
985         ret = tsl2772_invoke_change(indio_dev);
986         if (ret < 0)
987                 return ret;
988
989         return len;
990 }
991
992 static ssize_t in_illuminance0_calibrate_store(struct device *dev,
993                                                struct device_attribute *attr,
994                                                const char *buf, size_t len)
995 {
996         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
997         bool value;
998         int ret;
999
1000         if (kstrtobool(buf, &value) || !value)
1001                 return -EINVAL;
1002
1003         ret = tsl2772_als_calibrate(indio_dev);
1004         if (ret < 0)
1005                 return ret;
1006
1007         ret = tsl2772_invoke_change(indio_dev);
1008         if (ret < 0)
1009                 return ret;
1010
1011         return len;
1012 }
1013
1014 static ssize_t in_illuminance0_lux_table_show(struct device *dev,
1015                                               struct device_attribute *attr,
1016                                               char *buf)
1017 {
1018         struct tsl2772_chip *chip = iio_priv(dev_to_iio_dev(dev));
1019         int i = 0;
1020         int offset = 0;
1021
1022         while (i < TSL2772_MAX_LUX_TABLE_SIZE) {
1023                 offset += snprintf(buf + offset, PAGE_SIZE, "%u,%u,",
1024                         chip->tsl2772_device_lux[i].ch0,
1025                         chip->tsl2772_device_lux[i].ch1);
1026                 if (chip->tsl2772_device_lux[i].ch0 == 0) {
1027                         /*
1028                          * We just printed the first "0" entry.
1029                          * Now get rid of the extra "," and break.
1030                          */
1031                         offset--;
1032                         break;
1033                 }
1034                 i++;
1035         }
1036
1037         offset += snprintf(buf + offset, PAGE_SIZE, "\n");
1038         return offset;
1039 }
1040
1041 static ssize_t in_illuminance0_lux_table_store(struct device *dev,
1042                                                struct device_attribute *attr,
1043                                                const char *buf, size_t len)
1044 {
1045         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1046         struct tsl2772_chip *chip = iio_priv(indio_dev);
1047         int value[ARRAY_SIZE(chip->tsl2772_device_lux) * 2 + 1];
1048         int n, ret;
1049
1050         get_options(buf, ARRAY_SIZE(value), value);
1051
1052         /*
1053          * We now have an array of ints starting at value[1], and
1054          * enumerated by value[0].
1055          * We expect each group of two ints to be one table entry,
1056          * and the last table entry is all 0.
1057          */
1058         n = value[0];
1059         if ((n % 2) || n < 4 ||
1060             n > ((ARRAY_SIZE(chip->tsl2772_device_lux) - 1) * 2))
1061                 return -EINVAL;
1062
1063         if ((value[(n - 1)] | value[n]) != 0)
1064                 return -EINVAL;
1065
1066         if (chip->tsl2772_chip_status == TSL2772_CHIP_WORKING) {
1067                 ret = tsl2772_chip_off(indio_dev);
1068                 if (ret < 0)
1069                         return ret;
1070         }
1071
1072         /* Zero out the table */
1073         memset(chip->tsl2772_device_lux, 0, sizeof(chip->tsl2772_device_lux));
1074         memcpy(chip->tsl2772_device_lux, &value[1], (value[0] * 4));
1075
1076         ret = tsl2772_invoke_change(indio_dev);
1077         if (ret < 0)
1078                 return ret;
1079
1080         return len;
1081 }
1082
1083 static ssize_t in_proximity0_calibrate_store(struct device *dev,
1084                                              struct device_attribute *attr,
1085                                              const char *buf, size_t len)
1086 {
1087         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1088         bool value;
1089         int ret;
1090
1091         if (kstrtobool(buf, &value) || !value)
1092                 return -EINVAL;
1093
1094         ret = tsl2772_prox_cal(indio_dev);
1095         if (ret < 0)
1096                 return ret;
1097
1098         ret = tsl2772_invoke_change(indio_dev);
1099         if (ret < 0)
1100                 return ret;
1101
1102         return len;
1103 }
1104
1105 static int tsl2772_read_interrupt_config(struct iio_dev *indio_dev,
1106                                          const struct iio_chan_spec *chan,
1107                                          enum iio_event_type type,
1108                                          enum iio_event_direction dir)
1109 {
1110         struct tsl2772_chip *chip = iio_priv(indio_dev);
1111
1112         if (chan->type == IIO_INTENSITY)
1113                 return chip->settings.als_interrupt_en;
1114         else
1115                 return chip->settings.prox_interrupt_en;
1116 }
1117
1118 static int tsl2772_write_interrupt_config(struct iio_dev *indio_dev,
1119                                           const struct iio_chan_spec *chan,
1120                                           enum iio_event_type type,
1121                                           enum iio_event_direction dir,
1122                                           int val)
1123 {
1124         struct tsl2772_chip *chip = iio_priv(indio_dev);
1125
1126         if (chan->type == IIO_INTENSITY)
1127                 chip->settings.als_interrupt_en = val ? true : false;
1128         else
1129                 chip->settings.prox_interrupt_en = val ? true : false;
1130
1131         return tsl2772_invoke_change(indio_dev);
1132 }
1133
1134 static int tsl2772_write_event_value(struct iio_dev *indio_dev,
1135                                      const struct iio_chan_spec *chan,
1136                                      enum iio_event_type type,
1137                                      enum iio_event_direction dir,
1138                                      enum iio_event_info info,
1139                                      int val, int val2)
1140 {
1141         struct tsl2772_chip *chip = iio_priv(indio_dev);
1142         int ret = -EINVAL, count, persistence;
1143         u8 time;
1144
1145         switch (info) {
1146         case IIO_EV_INFO_VALUE:
1147                 if (chan->type == IIO_INTENSITY) {
1148                         switch (dir) {
1149                         case IIO_EV_DIR_RISING:
1150                                 chip->settings.als_thresh_high = val;
1151                                 ret = 0;
1152                                 break;
1153                         case IIO_EV_DIR_FALLING:
1154                                 chip->settings.als_thresh_low = val;
1155                                 ret = 0;
1156                                 break;
1157                         default:
1158                                 break;
1159                         }
1160                 } else {
1161                         switch (dir) {
1162                         case IIO_EV_DIR_RISING:
1163                                 chip->settings.prox_thres_high = val;
1164                                 ret = 0;
1165                                 break;
1166                         case IIO_EV_DIR_FALLING:
1167                                 chip->settings.prox_thres_low = val;
1168                                 ret = 0;
1169                                 break;
1170                         default:
1171                                 break;
1172                         }
1173                 }
1174                 break;
1175         case IIO_EV_INFO_PERIOD:
1176                 if (chan->type == IIO_INTENSITY)
1177                         time = chip->settings.als_time;
1178                 else
1179                         time = chip->settings.prox_time;
1180
1181                 count = 256 - time;
1182                 persistence = ((val * 1000000) + val2) /
1183                         (count * tsl2772_int_time_avail[chip->id][3]);
1184
1185                 if (chan->type == IIO_INTENSITY) {
1186                         /* ALS filter values are 1, 2, 3, 5, 10, 15, ..., 60 */
1187                         if (persistence > 3)
1188                                 persistence = (persistence / 5) + 3;
1189
1190                         chip->settings.als_persistence = persistence;
1191                 } else {
1192                         chip->settings.prox_persistence = persistence;
1193                 }
1194
1195                 ret = 0;
1196                 break;
1197         default:
1198                 break;
1199         }
1200
1201         if (ret < 0)
1202                 return ret;
1203
1204         return tsl2772_invoke_change(indio_dev);
1205 }
1206
1207 static int tsl2772_read_event_value(struct iio_dev *indio_dev,
1208                                     const struct iio_chan_spec *chan,
1209                                     enum iio_event_type type,
1210                                     enum iio_event_direction dir,
1211                                     enum iio_event_info info,
1212                                     int *val, int *val2)
1213 {
1214         struct tsl2772_chip *chip = iio_priv(indio_dev);
1215         int filter_delay, persistence;
1216         u8 time;
1217
1218         switch (info) {
1219         case IIO_EV_INFO_VALUE:
1220                 if (chan->type == IIO_INTENSITY) {
1221                         switch (dir) {
1222                         case IIO_EV_DIR_RISING:
1223                                 *val = chip->settings.als_thresh_high;
1224                                 return IIO_VAL_INT;
1225                         case IIO_EV_DIR_FALLING:
1226                                 *val = chip->settings.als_thresh_low;
1227                                 return IIO_VAL_INT;
1228                         default:
1229                                 return -EINVAL;
1230                         }
1231                 } else {
1232                         switch (dir) {
1233                         case IIO_EV_DIR_RISING:
1234                                 *val = chip->settings.prox_thres_high;
1235                                 return IIO_VAL_INT;
1236                         case IIO_EV_DIR_FALLING:
1237                                 *val = chip->settings.prox_thres_low;
1238                                 return IIO_VAL_INT;
1239                         default:
1240                                 return -EINVAL;
1241                         }
1242                 }
1243                 break;
1244         case IIO_EV_INFO_PERIOD:
1245                 if (chan->type == IIO_INTENSITY) {
1246                         time = chip->settings.als_time;
1247                         persistence = chip->settings.als_persistence;
1248
1249                         /* ALS filter values are 1, 2, 3, 5, 10, 15, ..., 60 */
1250                         if (persistence > 3)
1251                                 persistence = (persistence - 3) * 5;
1252                 } else {
1253                         time = chip->settings.prox_time;
1254                         persistence = chip->settings.prox_persistence;
1255                 }
1256
1257                 filter_delay = persistence * (256 - time) *
1258                         tsl2772_int_time_avail[chip->id][3];
1259
1260                 *val = filter_delay / 1000000;
1261                 *val2 = filter_delay % 1000000;
1262                 return IIO_VAL_INT_PLUS_MICRO;
1263         default:
1264                 return -EINVAL;
1265         }
1266 }
1267
1268 static int tsl2772_read_raw(struct iio_dev *indio_dev,
1269                             struct iio_chan_spec const *chan,
1270                             int *val,
1271                             int *val2,
1272                             long mask)
1273 {
1274         struct tsl2772_chip *chip = iio_priv(indio_dev);
1275
1276         switch (mask) {
1277         case IIO_CHAN_INFO_PROCESSED:
1278                 switch (chan->type) {
1279                 case IIO_LIGHT:
1280                         tsl2772_get_lux(indio_dev);
1281                         *val = chip->als_cur_info.lux;
1282                         return IIO_VAL_INT;
1283                 default:
1284                         return -EINVAL;
1285                 }
1286         case IIO_CHAN_INFO_RAW:
1287                 switch (chan->type) {
1288                 case IIO_INTENSITY:
1289                         tsl2772_get_lux(indio_dev);
1290                         if (chan->channel == 0)
1291                                 *val = chip->als_cur_info.als_ch0;
1292                         else
1293                                 *val = chip->als_cur_info.als_ch1;
1294                         return IIO_VAL_INT;
1295                 case IIO_PROXIMITY:
1296                         tsl2772_get_prox(indio_dev);
1297                         *val = chip->prox_data;
1298                         return IIO_VAL_INT;
1299                 default:
1300                         return -EINVAL;
1301                 }
1302                 break;
1303         case IIO_CHAN_INFO_CALIBSCALE:
1304                 if (chan->type == IIO_LIGHT)
1305                         *val = tsl2772_als_gain[chip->settings.als_gain];
1306                 else
1307                         *val = tsl2772_prox_gain[chip->settings.prox_gain];
1308                 return IIO_VAL_INT;
1309         case IIO_CHAN_INFO_CALIBBIAS:
1310                 *val = chip->settings.als_gain_trim;
1311                 return IIO_VAL_INT;
1312         case IIO_CHAN_INFO_INT_TIME:
1313                 *val = 0;
1314                 *val2 = (256 - chip->settings.als_time) *
1315                         tsl2772_int_time_avail[chip->id][3];
1316                 return IIO_VAL_INT_PLUS_MICRO;
1317         default:
1318                 return -EINVAL;
1319         }
1320 }
1321
1322 static int tsl2772_write_raw(struct iio_dev *indio_dev,
1323                              struct iio_chan_spec const *chan,
1324                              int val,
1325                              int val2,
1326                              long mask)
1327 {
1328         struct tsl2772_chip *chip = iio_priv(indio_dev);
1329
1330         switch (mask) {
1331         case IIO_CHAN_INFO_CALIBSCALE:
1332                 if (chan->type == IIO_INTENSITY) {
1333                         switch (val) {
1334                         case 1:
1335                                 chip->settings.als_gain = 0;
1336                                 break;
1337                         case 8:
1338                                 chip->settings.als_gain = 1;
1339                                 break;
1340                         case 16:
1341                                 chip->settings.als_gain = 2;
1342                                 break;
1343                         case 120:
1344                                 chip->settings.als_gain = 3;
1345                                 break;
1346                         default:
1347                                 return -EINVAL;
1348                         }
1349                 } else {
1350                         switch (val) {
1351                         case 1:
1352                                 chip->settings.prox_gain = 0;
1353                                 break;
1354                         case 2:
1355                                 chip->settings.prox_gain = 1;
1356                                 break;
1357                         case 4:
1358                                 chip->settings.prox_gain = 2;
1359                                 break;
1360                         case 8:
1361                                 chip->settings.prox_gain = 3;
1362                                 break;
1363                         default:
1364                                 return -EINVAL;
1365                         }
1366                 }
1367                 break;
1368         case IIO_CHAN_INFO_CALIBBIAS:
1369                 if (val < TSL2772_ALS_GAIN_TRIM_MIN ||
1370                     val > TSL2772_ALS_GAIN_TRIM_MAX)
1371                         return -EINVAL;
1372
1373                 chip->settings.als_gain_trim = val;
1374                 break;
1375         case IIO_CHAN_INFO_INT_TIME:
1376                 if (val != 0 || val2 < tsl2772_int_time_avail[chip->id][1] ||
1377                     val2 > tsl2772_int_time_avail[chip->id][5])
1378                         return -EINVAL;
1379
1380                 chip->settings.als_time = 256 -
1381                         (val2 / tsl2772_int_time_avail[chip->id][3]);
1382                 break;
1383         default:
1384                 return -EINVAL;
1385         }
1386
1387         return tsl2772_invoke_change(indio_dev);
1388 }
1389
1390 static DEVICE_ATTR_RW(in_illuminance0_target_input);
1391
1392 static DEVICE_ATTR_WO(in_illuminance0_calibrate);
1393
1394 static DEVICE_ATTR_WO(in_proximity0_calibrate);
1395
1396 static DEVICE_ATTR_RW(in_illuminance0_lux_table);
1397
1398 /* Use the default register values to identify the Taos device */
1399 static int tsl2772_device_id_verif(int id, int target)
1400 {
1401         switch (target) {
1402         case tsl2571:
1403         case tsl2671:
1404         case tsl2771:
1405                 return (id & 0xf0) == TRITON_ID;
1406         case tmd2671:
1407         case tmd2771:
1408                 return (id & 0xf0) == HALIBUT_ID;
1409         case tsl2572:
1410         case tsl2672:
1411         case tmd2672:
1412         case tsl2772:
1413         case tmd2772:
1414         case apds9930:
1415                 return (id & 0xf0) == SWORDFISH_ID;
1416         }
1417
1418         return -EINVAL;
1419 }
1420
1421 static irqreturn_t tsl2772_event_handler(int irq, void *private)
1422 {
1423         struct iio_dev *indio_dev = private;
1424         struct tsl2772_chip *chip = iio_priv(indio_dev);
1425         s64 timestamp = iio_get_time_ns(indio_dev);
1426         int ret;
1427
1428         ret = tsl2772_read_status(chip);
1429         if (ret < 0)
1430                 return IRQ_HANDLED;
1431
1432         /* What type of interrupt do we need to process */
1433         if (ret & TSL2772_STA_PRX_INTR) {
1434                 iio_push_event(indio_dev,
1435                                IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY,
1436                                                     0,
1437                                                     IIO_EV_TYPE_THRESH,
1438                                                     IIO_EV_DIR_EITHER),
1439                                timestamp);
1440         }
1441
1442         if (ret & TSL2772_STA_ALS_INTR) {
1443                 iio_push_event(indio_dev,
1444                                IIO_UNMOD_EVENT_CODE(IIO_LIGHT,
1445                                                     0,
1446                                                     IIO_EV_TYPE_THRESH,
1447                                                     IIO_EV_DIR_EITHER),
1448                                timestamp);
1449         }
1450
1451         ret = i2c_smbus_write_byte(chip->client,
1452                                    TSL2772_CMD_REG | TSL2772_CMD_SPL_FN |
1453                                    TSL2772_CMD_PROXALS_INT_CLR);
1454         if (ret < 0)
1455                 dev_err(&chip->client->dev,
1456                         "%s: failed to clear interrupt status: %d\n",
1457                         __func__, ret);
1458
1459         return IRQ_HANDLED;
1460 }
1461
1462 static struct attribute *tsl2772_ALS_device_attrs[] = {
1463         &dev_attr_in_illuminance0_target_input.attr,
1464         &dev_attr_in_illuminance0_calibrate.attr,
1465         &dev_attr_in_illuminance0_lux_table.attr,
1466         NULL
1467 };
1468
1469 static struct attribute *tsl2772_PRX_device_attrs[] = {
1470         &dev_attr_in_proximity0_calibrate.attr,
1471         NULL
1472 };
1473
1474 static struct attribute *tsl2772_ALSPRX_device_attrs[] = {
1475         &dev_attr_in_illuminance0_target_input.attr,
1476         &dev_attr_in_illuminance0_calibrate.attr,
1477         &dev_attr_in_illuminance0_lux_table.attr,
1478         NULL
1479 };
1480
1481 static struct attribute *tsl2772_PRX2_device_attrs[] = {
1482         &dev_attr_in_proximity0_calibrate.attr,
1483         NULL
1484 };
1485
1486 static struct attribute *tsl2772_ALSPRX2_device_attrs[] = {
1487         &dev_attr_in_illuminance0_target_input.attr,
1488         &dev_attr_in_illuminance0_calibrate.attr,
1489         &dev_attr_in_illuminance0_lux_table.attr,
1490         &dev_attr_in_proximity0_calibrate.attr,
1491         NULL
1492 };
1493
1494 static const struct attribute_group tsl2772_device_attr_group_tbl[] = {
1495         [ALS] = {
1496                 .attrs = tsl2772_ALS_device_attrs,
1497         },
1498         [PRX] = {
1499                 .attrs = tsl2772_PRX_device_attrs,
1500         },
1501         [ALSPRX] = {
1502                 .attrs = tsl2772_ALSPRX_device_attrs,
1503         },
1504         [PRX2] = {
1505                 .attrs = tsl2772_PRX2_device_attrs,
1506         },
1507         [ALSPRX2] = {
1508                 .attrs = tsl2772_ALSPRX2_device_attrs,
1509         },
1510 };
1511
1512 #define TSL2772_DEVICE_INFO(type)[type] = \
1513         { \
1514                 .attrs = &tsl2772_device_attr_group_tbl[type], \
1515                 .read_raw = &tsl2772_read_raw, \
1516                 .read_avail = &tsl2772_read_avail, \
1517                 .write_raw = &tsl2772_write_raw, \
1518                 .read_event_value = &tsl2772_read_event_value, \
1519                 .write_event_value = &tsl2772_write_event_value, \
1520                 .read_event_config = &tsl2772_read_interrupt_config, \
1521                 .write_event_config = &tsl2772_write_interrupt_config, \
1522         }
1523
1524 static const struct iio_info tsl2772_device_info[] = {
1525         TSL2772_DEVICE_INFO(ALS),
1526         TSL2772_DEVICE_INFO(PRX),
1527         TSL2772_DEVICE_INFO(ALSPRX),
1528         TSL2772_DEVICE_INFO(PRX2),
1529         TSL2772_DEVICE_INFO(ALSPRX2),
1530 };
1531
1532 static const struct iio_event_spec tsl2772_events[] = {
1533         {
1534                 .type = IIO_EV_TYPE_THRESH,
1535                 .dir = IIO_EV_DIR_RISING,
1536                 .mask_separate = BIT(IIO_EV_INFO_VALUE),
1537         }, {
1538                 .type = IIO_EV_TYPE_THRESH,
1539                 .dir = IIO_EV_DIR_FALLING,
1540                 .mask_separate = BIT(IIO_EV_INFO_VALUE),
1541         }, {
1542                 .type = IIO_EV_TYPE_THRESH,
1543                 .dir = IIO_EV_DIR_EITHER,
1544                 .mask_separate = BIT(IIO_EV_INFO_PERIOD) |
1545                         BIT(IIO_EV_INFO_ENABLE),
1546         },
1547 };
1548
1549 static const struct tsl2772_chip_info tsl2772_chip_info_tbl[] = {
1550         [ALS] = {
1551                 .channel_with_events = {
1552                         {
1553                         .type = IIO_LIGHT,
1554                         .indexed = 1,
1555                         .channel = 0,
1556                         .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1557                         }, {
1558                         .type = IIO_INTENSITY,
1559                         .indexed = 1,
1560                         .channel = 0,
1561                         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1562                                 BIT(IIO_CHAN_INFO_INT_TIME) |
1563                                 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1564                                 BIT(IIO_CHAN_INFO_CALIBBIAS),
1565                         .info_mask_separate_available =
1566                                 BIT(IIO_CHAN_INFO_INT_TIME) |
1567                                 BIT(IIO_CHAN_INFO_CALIBSCALE),
1568                         .event_spec = tsl2772_events,
1569                         .num_event_specs = ARRAY_SIZE(tsl2772_events),
1570                         }, {
1571                         .type = IIO_INTENSITY,
1572                         .indexed = 1,
1573                         .channel = 1,
1574                         },
1575                 },
1576                 .channel_without_events = {
1577                         {
1578                         .type = IIO_LIGHT,
1579                         .indexed = 1,
1580                         .channel = 0,
1581                         .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1582                         }, {
1583                         .type = IIO_INTENSITY,
1584                         .indexed = 1,
1585                         .channel = 0,
1586                         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1587                                 BIT(IIO_CHAN_INFO_INT_TIME) |
1588                                 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1589                                 BIT(IIO_CHAN_INFO_CALIBBIAS),
1590                         .info_mask_separate_available =
1591                                 BIT(IIO_CHAN_INFO_INT_TIME) |
1592                                 BIT(IIO_CHAN_INFO_CALIBSCALE),
1593                         }, {
1594                         .type = IIO_INTENSITY,
1595                         .indexed = 1,
1596                         .channel = 1,
1597                         },
1598                 },
1599                 .chan_table_elements = 3,
1600                 .info = &tsl2772_device_info[ALS],
1601         },
1602         [PRX] = {
1603                 .channel_with_events = {
1604                         {
1605                         .type = IIO_PROXIMITY,
1606                         .indexed = 1,
1607                         .channel = 0,
1608                         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1609                         .event_spec = tsl2772_events,
1610                         .num_event_specs = ARRAY_SIZE(tsl2772_events),
1611                         },
1612                 },
1613                 .channel_without_events = {
1614                         {
1615                         .type = IIO_PROXIMITY,
1616                         .indexed = 1,
1617                         .channel = 0,
1618                         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1619                         },
1620                 },
1621                 .chan_table_elements = 1,
1622                 .info = &tsl2772_device_info[PRX],
1623         },
1624         [ALSPRX] = {
1625                 .channel_with_events = {
1626                         {
1627                         .type = IIO_LIGHT,
1628                         .indexed = 1,
1629                         .channel = 0,
1630                         .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1631                         }, {
1632                         .type = IIO_INTENSITY,
1633                         .indexed = 1,
1634                         .channel = 0,
1635                         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1636                                 BIT(IIO_CHAN_INFO_INT_TIME) |
1637                                 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1638                                 BIT(IIO_CHAN_INFO_CALIBBIAS),
1639                         .info_mask_separate_available =
1640                                 BIT(IIO_CHAN_INFO_INT_TIME) |
1641                                 BIT(IIO_CHAN_INFO_CALIBSCALE),
1642                         .event_spec = tsl2772_events,
1643                         .num_event_specs = ARRAY_SIZE(tsl2772_events),
1644                         }, {
1645                         .type = IIO_INTENSITY,
1646                         .indexed = 1,
1647                         .channel = 1,
1648                         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1649                         }, {
1650                         .type = IIO_PROXIMITY,
1651                         .indexed = 1,
1652                         .channel = 0,
1653                         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1654                         .event_spec = tsl2772_events,
1655                         .num_event_specs = ARRAY_SIZE(tsl2772_events),
1656                         },
1657                 },
1658                 .channel_without_events = {
1659                         {
1660                         .type = IIO_LIGHT,
1661                         .indexed = 1,
1662                         .channel = 0,
1663                         .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1664                         }, {
1665                         .type = IIO_INTENSITY,
1666                         .indexed = 1,
1667                         .channel = 0,
1668                         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1669                                 BIT(IIO_CHAN_INFO_INT_TIME) |
1670                                 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1671                                 BIT(IIO_CHAN_INFO_CALIBBIAS),
1672                         .info_mask_separate_available =
1673                                 BIT(IIO_CHAN_INFO_INT_TIME) |
1674                                 BIT(IIO_CHAN_INFO_CALIBSCALE),
1675                         }, {
1676                         .type = IIO_INTENSITY,
1677                         .indexed = 1,
1678                         .channel = 1,
1679                         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1680                         }, {
1681                         .type = IIO_PROXIMITY,
1682                         .indexed = 1,
1683                         .channel = 0,
1684                         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1685                         },
1686                 },
1687                 .chan_table_elements = 4,
1688                 .info = &tsl2772_device_info[ALSPRX],
1689         },
1690         [PRX2] = {
1691                 .channel_with_events = {
1692                         {
1693                         .type = IIO_PROXIMITY,
1694                         .indexed = 1,
1695                         .channel = 0,
1696                         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1697                                 BIT(IIO_CHAN_INFO_CALIBSCALE),
1698                         .info_mask_separate_available =
1699                                 BIT(IIO_CHAN_INFO_CALIBSCALE),
1700                         .event_spec = tsl2772_events,
1701                         .num_event_specs = ARRAY_SIZE(tsl2772_events),
1702                         },
1703                 },
1704                 .channel_without_events = {
1705                         {
1706                         .type = IIO_PROXIMITY,
1707                         .indexed = 1,
1708                         .channel = 0,
1709                         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1710                                 BIT(IIO_CHAN_INFO_CALIBSCALE),
1711                         .info_mask_separate_available =
1712                                 BIT(IIO_CHAN_INFO_CALIBSCALE),
1713                         },
1714                 },
1715                 .chan_table_elements = 1,
1716                 .info = &tsl2772_device_info[PRX2],
1717         },
1718         [ALSPRX2] = {
1719                 .channel_with_events = {
1720                         {
1721                         .type = IIO_LIGHT,
1722                         .indexed = 1,
1723                         .channel = 0,
1724                         .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1725                         }, {
1726                         .type = IIO_INTENSITY,
1727                         .indexed = 1,
1728                         .channel = 0,
1729                         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1730                                 BIT(IIO_CHAN_INFO_INT_TIME) |
1731                                 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1732                                 BIT(IIO_CHAN_INFO_CALIBBIAS),
1733                         .info_mask_separate_available =
1734                                 BIT(IIO_CHAN_INFO_INT_TIME) |
1735                                 BIT(IIO_CHAN_INFO_CALIBSCALE),
1736                         .event_spec = tsl2772_events,
1737                         .num_event_specs = ARRAY_SIZE(tsl2772_events),
1738                         }, {
1739                         .type = IIO_INTENSITY,
1740                         .indexed = 1,
1741                         .channel = 1,
1742                         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1743                         }, {
1744                         .type = IIO_PROXIMITY,
1745                         .indexed = 1,
1746                         .channel = 0,
1747                         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1748                                 BIT(IIO_CHAN_INFO_CALIBSCALE),
1749                         .info_mask_separate_available =
1750                                 BIT(IIO_CHAN_INFO_CALIBSCALE),
1751                         .event_spec = tsl2772_events,
1752                         .num_event_specs = ARRAY_SIZE(tsl2772_events),
1753                         },
1754                 },
1755                 .channel_without_events = {
1756                         {
1757                         .type = IIO_LIGHT,
1758                         .indexed = 1,
1759                         .channel = 0,
1760                         .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
1761                         }, {
1762                         .type = IIO_INTENSITY,
1763                         .indexed = 1,
1764                         .channel = 0,
1765                         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1766                                 BIT(IIO_CHAN_INFO_INT_TIME) |
1767                                 BIT(IIO_CHAN_INFO_CALIBSCALE) |
1768                                 BIT(IIO_CHAN_INFO_CALIBBIAS),
1769                         .info_mask_separate_available =
1770                                 BIT(IIO_CHAN_INFO_INT_TIME) |
1771                                 BIT(IIO_CHAN_INFO_CALIBSCALE),
1772                         }, {
1773                         .type = IIO_INTENSITY,
1774                         .indexed = 1,
1775                         .channel = 1,
1776                         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1777                         }, {
1778                         .type = IIO_PROXIMITY,
1779                         .indexed = 1,
1780                         .channel = 0,
1781                         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1782                                 BIT(IIO_CHAN_INFO_CALIBSCALE),
1783                         .info_mask_separate_available =
1784                                 BIT(IIO_CHAN_INFO_CALIBSCALE),
1785                         },
1786                 },
1787                 .chan_table_elements = 4,
1788                 .info = &tsl2772_device_info[ALSPRX2],
1789         },
1790 };
1791
1792 static int tsl2772_probe(struct i2c_client *clientp,
1793                          const struct i2c_device_id *id)
1794 {
1795         struct iio_dev *indio_dev;
1796         struct tsl2772_chip *chip;
1797         int ret;
1798
1799         indio_dev = devm_iio_device_alloc(&clientp->dev, sizeof(*chip));
1800         if (!indio_dev)
1801                 return -ENOMEM;
1802
1803         chip = iio_priv(indio_dev);
1804         chip->client = clientp;
1805         i2c_set_clientdata(clientp, indio_dev);
1806
1807         chip->vddio_supply = tsl2772_get_regulator(chip, "vddio");
1808         if (IS_ERR(chip->vddio_supply))
1809                 return PTR_ERR(chip->vddio_supply);
1810
1811         chip->vdd_supply = tsl2772_get_regulator(chip, "vdd");
1812         if (IS_ERR(chip->vdd_supply)) {
1813                 regulator_disable(chip->vddio_supply);
1814                 return PTR_ERR(chip->vdd_supply);
1815         }
1816
1817         ret = devm_add_action_or_reset(&clientp->dev,
1818                                         tsl2772_disable_regulators_action,
1819                                         chip);
1820         if (ret < 0) {
1821                 dev_err(&clientp->dev, "Failed to setup regulator cleanup action %d\n",
1822                         ret);
1823                 return ret;
1824         }
1825
1826         usleep_range(TSL2772_BOOT_MIN_SLEEP_TIME, TSL2772_BOOT_MAX_SLEEP_TIME);
1827
1828         ret = i2c_smbus_read_byte_data(chip->client,
1829                                        TSL2772_CMD_REG | TSL2772_CHIPID);
1830         if (ret < 0)
1831                 return ret;
1832
1833         if (tsl2772_device_id_verif(ret, id->driver_data) <= 0) {
1834                 dev_info(&chip->client->dev,
1835                          "%s: i2c device found does not match expected id\n",
1836                                 __func__);
1837                 return -EINVAL;
1838         }
1839
1840         ret = i2c_smbus_write_byte(clientp, TSL2772_CMD_REG | TSL2772_CNTRL);
1841         if (ret < 0) {
1842                 dev_err(&clientp->dev,
1843                         "%s: Failed to write to CMD register: %d\n",
1844                         __func__, ret);
1845                 return ret;
1846         }
1847
1848         mutex_init(&chip->als_mutex);
1849         mutex_init(&chip->prox_mutex);
1850
1851         chip->tsl2772_chip_status = TSL2772_CHIP_UNKNOWN;
1852         chip->pdata = dev_get_platdata(&clientp->dev);
1853         chip->id = id->driver_data;
1854         chip->chip_info =
1855                 &tsl2772_chip_info_tbl[device_channel_config[id->driver_data]];
1856
1857         indio_dev->info = chip->chip_info->info;
1858         indio_dev->dev.parent = &clientp->dev;
1859         indio_dev->modes = INDIO_DIRECT_MODE;
1860         indio_dev->name = chip->client->name;
1861         indio_dev->num_channels = chip->chip_info->chan_table_elements;
1862
1863         if (clientp->irq) {
1864                 indio_dev->channels = chip->chip_info->channel_with_events;
1865
1866                 ret = devm_request_threaded_irq(&clientp->dev, clientp->irq,
1867                                                 NULL,
1868                                                 &tsl2772_event_handler,
1869                                                 IRQF_TRIGGER_FALLING |
1870                                                 IRQF_ONESHOT,
1871                                                 "TSL2772_event",
1872                                                 indio_dev);
1873                 if (ret) {
1874                         dev_err(&clientp->dev,
1875                                 "%s: irq request failed\n", __func__);
1876                         return ret;
1877                 }
1878         } else {
1879                 indio_dev->channels = chip->chip_info->channel_without_events;
1880         }
1881
1882         tsl2772_defaults(chip);
1883         ret = tsl2772_chip_on(indio_dev);
1884         if (ret < 0)
1885                 return ret;
1886
1887         ret = devm_add_action_or_reset(&clientp->dev,
1888                                         tsl2772_chip_off_action,
1889                                         indio_dev);
1890         if (ret < 0)
1891                 return ret;
1892
1893         ret = iio_device_register(indio_dev);
1894         if (ret) {
1895                 dev_err(&clientp->dev,
1896                         "%s: iio registration failed\n", __func__);
1897                 return ret;
1898         }
1899
1900         return 0;
1901 }
1902
1903 static int tsl2772_suspend(struct device *dev)
1904 {
1905         struct iio_dev *indio_dev = dev_get_drvdata(dev);
1906         struct tsl2772_chip *chip = iio_priv(indio_dev);
1907         int ret;
1908
1909         ret = tsl2772_chip_off(indio_dev);
1910         regulator_disable(chip->vdd_supply);
1911         regulator_disable(chip->vddio_supply);
1912
1913         return ret;
1914 }
1915
1916 static int tsl2772_resume(struct device *dev)
1917 {
1918         struct iio_dev *indio_dev = dev_get_drvdata(dev);
1919         struct tsl2772_chip *chip = iio_priv(indio_dev);
1920         int ret;
1921
1922         ret = tsl2772_enable_regulator(chip, chip->vddio_supply);
1923         if (ret < 0)
1924                 return ret;
1925
1926         ret = tsl2772_enable_regulator(chip, chip->vdd_supply);
1927         if (ret < 0) {
1928                 regulator_disable(chip->vddio_supply);
1929                 return ret;
1930         }
1931
1932         usleep_range(TSL2772_BOOT_MIN_SLEEP_TIME, TSL2772_BOOT_MAX_SLEEP_TIME);
1933
1934         return tsl2772_chip_on(indio_dev);
1935 }
1936
1937 static int tsl2772_remove(struct i2c_client *client)
1938 {
1939         struct iio_dev *indio_dev = i2c_get_clientdata(client);
1940
1941         iio_device_unregister(indio_dev);
1942
1943         return 0;
1944 }
1945
1946 static const struct i2c_device_id tsl2772_idtable[] = {
1947         { "tsl2571", tsl2571 },
1948         { "tsl2671", tsl2671 },
1949         { "tmd2671", tmd2671 },
1950         { "tsl2771", tsl2771 },
1951         { "tmd2771", tmd2771 },
1952         { "tsl2572", tsl2572 },
1953         { "tsl2672", tsl2672 },
1954         { "tmd2672", tmd2672 },
1955         { "tsl2772", tsl2772 },
1956         { "tmd2772", tmd2772 },
1957         { "apds9930", apds9930},
1958         {}
1959 };
1960
1961 MODULE_DEVICE_TABLE(i2c, tsl2772_idtable);
1962
1963 static const struct of_device_id tsl2772_of_match[] = {
1964         { .compatible = "amstaos,tsl2571" },
1965         { .compatible = "amstaos,tsl2671" },
1966         { .compatible = "amstaos,tmd2671" },
1967         { .compatible = "amstaos,tsl2771" },
1968         { .compatible = "amstaos,tmd2771" },
1969         { .compatible = "amstaos,tsl2572" },
1970         { .compatible = "amstaos,tsl2672" },
1971         { .compatible = "amstaos,tmd2672" },
1972         { .compatible = "amstaos,tsl2772" },
1973         { .compatible = "amstaos,tmd2772" },
1974         { .compatible = "avago,apds9930" },
1975         {}
1976 };
1977 MODULE_DEVICE_TABLE(of, tsl2772_of_match);
1978
1979 static const struct dev_pm_ops tsl2772_pm_ops = {
1980         .suspend = tsl2772_suspend,
1981         .resume  = tsl2772_resume,
1982 };
1983
1984 static struct i2c_driver tsl2772_driver = {
1985         .driver = {
1986                 .name = "tsl2772",
1987                 .of_match_table = tsl2772_of_match,
1988                 .pm = &tsl2772_pm_ops,
1989         },
1990         .id_table = tsl2772_idtable,
1991         .probe = tsl2772_probe,
1992         .remove = tsl2772_remove,
1993 };
1994
1995 module_i2c_driver(tsl2772_driver);
1996
1997 MODULE_AUTHOR("J. August Brenner <Jon.Brenner@ams.com>");
1998 MODULE_AUTHOR("Brian Masney <masneyb@onstation.org>");
1999 MODULE_DESCRIPTION("TAOS tsl2772 ambient and proximity light sensor driver");
2000 MODULE_LICENSE("GPL");