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