xref: /openbmc/linux/drivers/iio/light/vcnl4000.c (revision 71de0a05)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * vcnl4000.c - Support for Vishay VCNL4000/4010/4020/4040/4200 combined ambient
4  * light and proximity sensor
5  *
6  * Copyright 2012 Peter Meerwald <pmeerw@pmeerw.net>
7  * Copyright 2019 Pursim SPC
8  * Copyright 2020 Mathieu Othacehe <m.othacehe@gmail.com>
9  *
10  * IIO driver for:
11  *   VCNL4000/10/20 (7-bit I2C slave address 0x13)
12  *   VCNL4040 (7-bit I2C slave address 0x60)
13  *   VCNL4200 (7-bit I2C slave address 0x51)
14  *
15  * TODO:
16  *   allow to adjust IR current
17  *   interrupts (VCNL4040, VCNL4200)
18  */
19 
20 #include <linux/bitfield.h>
21 #include <linux/module.h>
22 #include <linux/i2c.h>
23 #include <linux/err.h>
24 #include <linux/delay.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/interrupt.h>
27 
28 #include <linux/iio/buffer.h>
29 #include <linux/iio/events.h>
30 #include <linux/iio/iio.h>
31 #include <linux/iio/sysfs.h>
32 #include <linux/iio/trigger.h>
33 #include <linux/iio/trigger_consumer.h>
34 #include <linux/iio/triggered_buffer.h>
35 
36 #define VCNL4000_DRV_NAME "vcnl4000"
37 #define VCNL4000_PROD_ID	0x01
38 #define VCNL4010_PROD_ID	0x02 /* for VCNL4020, VCNL4010 */
39 #define VCNL4040_PROD_ID	0x86
40 #define VCNL4200_PROD_ID	0x58
41 
42 #define VCNL4000_COMMAND	0x80 /* Command register */
43 #define VCNL4000_PROD_REV	0x81 /* Product ID and Revision ID */
44 #define VCNL4010_PROX_RATE      0x82 /* Proximity rate */
45 #define VCNL4000_LED_CURRENT	0x83 /* IR LED current for proximity mode */
46 #define VCNL4000_AL_PARAM	0x84 /* Ambient light parameter register */
47 #define VCNL4010_ALS_PARAM      0x84 /* ALS rate */
48 #define VCNL4000_AL_RESULT_HI	0x85 /* Ambient light result register, MSB */
49 #define VCNL4000_AL_RESULT_LO	0x86 /* Ambient light result register, LSB */
50 #define VCNL4000_PS_RESULT_HI	0x87 /* Proximity result register, MSB */
51 #define VCNL4000_PS_RESULT_LO	0x88 /* Proximity result register, LSB */
52 #define VCNL4000_PS_MEAS_FREQ	0x89 /* Proximity test signal frequency */
53 #define VCNL4010_INT_CTRL	0x89 /* Interrupt control */
54 #define VCNL4000_PS_MOD_ADJ	0x8a /* Proximity modulator timing adjustment */
55 #define VCNL4010_LOW_THR_HI     0x8a /* Low threshold, MSB */
56 #define VCNL4010_LOW_THR_LO     0x8b /* Low threshold, LSB */
57 #define VCNL4010_HIGH_THR_HI    0x8c /* High threshold, MSB */
58 #define VCNL4010_HIGH_THR_LO    0x8d /* High threshold, LSB */
59 #define VCNL4010_ISR		0x8e /* Interrupt status */
60 
61 #define VCNL4200_AL_CONF	0x00 /* Ambient light configuration */
62 #define VCNL4200_PS_CONF1	0x03 /* Proximity configuration */
63 #define VCNL4040_PS_THDL_LM	0x06 /* Proximity threshold low */
64 #define VCNL4040_PS_THDH_LM	0x07 /* Proximity threshold high */
65 #define VCNL4200_PS_DATA	0x08 /* Proximity data */
66 #define VCNL4200_AL_DATA	0x09 /* Ambient light data */
67 #define VCNL4040_INT_FLAGS	0x0b /* Interrupt register */
68 #define VCNL4200_DEV_ID		0x0e /* Device ID, slave address and version */
69 
70 #define VCNL4040_DEV_ID		0x0c /* Device ID and version */
71 
72 /* Bit masks for COMMAND register */
73 #define VCNL4000_AL_RDY		BIT(6) /* ALS data ready? */
74 #define VCNL4000_PS_RDY		BIT(5) /* proximity data ready? */
75 #define VCNL4000_AL_OD		BIT(4) /* start on-demand ALS measurement */
76 #define VCNL4000_PS_OD		BIT(3) /* start on-demand proximity measurement */
77 #define VCNL4000_ALS_EN		BIT(2) /* start ALS measurement */
78 #define VCNL4000_PROX_EN	BIT(1) /* start proximity measurement */
79 #define VCNL4000_SELF_TIMED_EN	BIT(0) /* start self-timed measurement */
80 
81 #define VCNL4040_ALS_CONF_ALS_SHUTDOWN	BIT(0)
82 #define VCNL4040_PS_CONF1_PS_SHUTDOWN	BIT(0)
83 #define VCNL4040_PS_CONF2_PS_IT	GENMASK(3, 1) /* Proximity integration time */
84 #define VCNL4040_PS_CONF2_PS_INT	GENMASK(9, 8) /* Proximity interrupt mode */
85 #define VCNL4040_PS_IF_AWAY		BIT(8) /* Proximity event cross low threshold */
86 #define VCNL4040_PS_IF_CLOSE		BIT(9) /* Proximity event cross high threshold */
87 
88 /* Bit masks for interrupt registers. */
89 #define VCNL4010_INT_THR_SEL	BIT(0) /* Select threshold interrupt source */
90 #define VCNL4010_INT_THR_EN	BIT(1) /* Threshold interrupt type */
91 #define VCNL4010_INT_ALS_EN	BIT(2) /* Enable on ALS data ready */
92 #define VCNL4010_INT_PROX_EN	BIT(3) /* Enable on proximity data ready */
93 
94 #define VCNL4010_INT_THR_HIGH	0 /* High threshold exceeded */
95 #define VCNL4010_INT_THR_LOW	1 /* Low threshold exceeded */
96 #define VCNL4010_INT_ALS	2 /* ALS data ready */
97 #define VCNL4010_INT_PROXIMITY	3 /* Proximity data ready */
98 
99 #define VCNL4010_INT_THR \
100 	(BIT(VCNL4010_INT_THR_LOW) | BIT(VCNL4010_INT_THR_HIGH))
101 #define VCNL4010_INT_DRDY \
102 	(BIT(VCNL4010_INT_PROXIMITY) | BIT(VCNL4010_INT_ALS))
103 
104 static const int vcnl4010_prox_sampling_frequency[][2] = {
105 	{1, 950000},
106 	{3, 906250},
107 	{7, 812500},
108 	{16, 625000},
109 	{31, 250000},
110 	{62, 500000},
111 	{125, 0},
112 	{250, 0},
113 };
114 
115 static const int vcnl4040_ps_it_times[][2] = {
116 	{0, 100},
117 	{0, 150},
118 	{0, 200},
119 	{0, 250},
120 	{0, 300},
121 	{0, 350},
122 	{0, 400},
123 	{0, 800},
124 };
125 
126 #define VCNL4000_SLEEP_DELAY_MS	2000 /* before we enter pm_runtime_suspend */
127 
128 enum vcnl4000_device_ids {
129 	VCNL4000,
130 	VCNL4010,
131 	VCNL4040,
132 	VCNL4200,
133 };
134 
135 struct vcnl4200_channel {
136 	u8 reg;
137 	ktime_t last_measurement;
138 	ktime_t sampling_rate;
139 	struct mutex lock;
140 };
141 
142 struct vcnl4000_data {
143 	struct i2c_client *client;
144 	enum vcnl4000_device_ids id;
145 	int rev;
146 	int al_scale;
147 	u8 ps_int;		/* proximity interrupt mode */
148 	const struct vcnl4000_chip_spec *chip_spec;
149 	struct mutex vcnl4000_lock;
150 	struct vcnl4200_channel vcnl4200_al;
151 	struct vcnl4200_channel vcnl4200_ps;
152 	uint32_t near_level;
153 };
154 
155 struct vcnl4000_chip_spec {
156 	const char *prod;
157 	struct iio_chan_spec const *channels;
158 	const int num_channels;
159 	const struct iio_info *info;
160 	const struct iio_buffer_setup_ops *buffer_setup_ops;
161 	int (*init)(struct vcnl4000_data *data);
162 	int (*measure_light)(struct vcnl4000_data *data, int *val);
163 	int (*measure_proximity)(struct vcnl4000_data *data, int *val);
164 	int (*set_power_state)(struct vcnl4000_data *data, bool on);
165 	irqreturn_t (*irq_thread)(int irq, void *priv);
166 	irqreturn_t (*trig_buffer_func)(int irq, void *priv);
167 };
168 
169 static const struct i2c_device_id vcnl4000_id[] = {
170 	{ "vcnl4000", VCNL4000 },
171 	{ "vcnl4010", VCNL4010 },
172 	{ "vcnl4020", VCNL4010 },
173 	{ "vcnl4040", VCNL4040 },
174 	{ "vcnl4200", VCNL4200 },
175 	{ }
176 };
177 MODULE_DEVICE_TABLE(i2c, vcnl4000_id);
178 
179 static int vcnl4000_set_power_state(struct vcnl4000_data *data, bool on)
180 {
181 	/* no suspend op */
182 	return 0;
183 }
184 
185 static int vcnl4000_init(struct vcnl4000_data *data)
186 {
187 	int ret, prod_id;
188 
189 	ret = i2c_smbus_read_byte_data(data->client, VCNL4000_PROD_REV);
190 	if (ret < 0)
191 		return ret;
192 
193 	prod_id = ret >> 4;
194 	switch (prod_id) {
195 	case VCNL4000_PROD_ID:
196 		if (data->id != VCNL4000)
197 			dev_warn(&data->client->dev,
198 					"wrong device id, use vcnl4000");
199 		break;
200 	case VCNL4010_PROD_ID:
201 		if (data->id != VCNL4010)
202 			dev_warn(&data->client->dev,
203 					"wrong device id, use vcnl4010/4020");
204 		break;
205 	default:
206 		return -ENODEV;
207 	}
208 
209 	data->rev = ret & 0xf;
210 	data->al_scale = 250000;
211 	mutex_init(&data->vcnl4000_lock);
212 
213 	return data->chip_spec->set_power_state(data, true);
214 };
215 
216 static ssize_t vcnl4000_write_als_enable(struct vcnl4000_data *data, bool en)
217 {
218 	int ret;
219 
220 	mutex_lock(&data->vcnl4000_lock);
221 
222 	ret = i2c_smbus_read_word_data(data->client, VCNL4200_AL_CONF);
223 	if (ret < 0)
224 		goto out;
225 
226 	if (en)
227 		ret &= ~VCNL4040_ALS_CONF_ALS_SHUTDOWN;
228 	else
229 		ret |= VCNL4040_ALS_CONF_ALS_SHUTDOWN;
230 
231 	ret = i2c_smbus_write_word_data(data->client, VCNL4200_AL_CONF, ret);
232 
233 out:
234 	mutex_unlock(&data->vcnl4000_lock);
235 
236 	return ret;
237 }
238 
239 static ssize_t vcnl4000_write_ps_enable(struct vcnl4000_data *data, bool en)
240 {
241 	int ret;
242 
243 	mutex_lock(&data->vcnl4000_lock);
244 
245 	ret = i2c_smbus_read_word_data(data->client, VCNL4200_PS_CONF1);
246 	if (ret < 0)
247 		goto out;
248 
249 	if (en)
250 		ret &= ~VCNL4040_PS_CONF1_PS_SHUTDOWN;
251 	else
252 		ret |= VCNL4040_PS_CONF1_PS_SHUTDOWN;
253 
254 	ret = i2c_smbus_write_word_data(data->client, VCNL4200_PS_CONF1, ret);
255 
256 out:
257 	mutex_unlock(&data->vcnl4000_lock);
258 
259 	return ret;
260 }
261 
262 static int vcnl4200_set_power_state(struct vcnl4000_data *data, bool on)
263 {
264 	int ret;
265 
266 	/* Do not power down if interrupts are enabled */
267 	if (!on && data->ps_int)
268 		return 0;
269 
270 	ret = vcnl4000_write_als_enable(data, on);
271 	if (ret < 0)
272 		return ret;
273 
274 	ret = vcnl4000_write_ps_enable(data, on);
275 	if (ret < 0)
276 		return ret;
277 
278 	if (on) {
279 		/* Wait at least one integration cycle before fetching data */
280 		data->vcnl4200_al.last_measurement = ktime_get();
281 		data->vcnl4200_ps.last_measurement = ktime_get();
282 	}
283 
284 	return 0;
285 }
286 
287 static int vcnl4200_init(struct vcnl4000_data *data)
288 {
289 	int ret, id;
290 
291 	ret = i2c_smbus_read_word_data(data->client, VCNL4200_DEV_ID);
292 	if (ret < 0)
293 		return ret;
294 
295 	id = ret & 0xff;
296 
297 	if (id != VCNL4200_PROD_ID) {
298 		ret = i2c_smbus_read_word_data(data->client, VCNL4040_DEV_ID);
299 		if (ret < 0)
300 			return ret;
301 
302 		id = ret & 0xff;
303 
304 		if (id != VCNL4040_PROD_ID)
305 			return -ENODEV;
306 	}
307 
308 	dev_dbg(&data->client->dev, "device id 0x%x", id);
309 
310 	data->rev = (ret >> 8) & 0xf;
311 	data->ps_int = 0;
312 
313 	data->vcnl4200_al.reg = VCNL4200_AL_DATA;
314 	data->vcnl4200_ps.reg = VCNL4200_PS_DATA;
315 	switch (id) {
316 	case VCNL4200_PROD_ID:
317 		/* Default wait time is 50ms, add 20% tolerance. */
318 		data->vcnl4200_al.sampling_rate = ktime_set(0, 60000 * 1000);
319 		/* Default wait time is 4.8ms, add 20% tolerance. */
320 		data->vcnl4200_ps.sampling_rate = ktime_set(0, 5760 * 1000);
321 		data->al_scale = 24000;
322 		break;
323 	case VCNL4040_PROD_ID:
324 		/* Default wait time is 80ms, add 20% tolerance. */
325 		data->vcnl4200_al.sampling_rate = ktime_set(0, 96000 * 1000);
326 		/* Default wait time is 5ms, add 20% tolerance. */
327 		data->vcnl4200_ps.sampling_rate = ktime_set(0, 6000 * 1000);
328 		data->al_scale = 120000;
329 		break;
330 	}
331 	mutex_init(&data->vcnl4200_al.lock);
332 	mutex_init(&data->vcnl4200_ps.lock);
333 
334 	ret = data->chip_spec->set_power_state(data, true);
335 	if (ret < 0)
336 		return ret;
337 
338 	return 0;
339 };
340 
341 static int vcnl4000_read_data(struct vcnl4000_data *data, u8 data_reg, int *val)
342 {
343 	s32 ret;
344 
345 	ret = i2c_smbus_read_word_swapped(data->client, data_reg);
346 	if (ret < 0)
347 		return ret;
348 
349 	*val = ret;
350 	return 0;
351 }
352 
353 static int vcnl4000_write_data(struct vcnl4000_data *data, u8 data_reg, int val)
354 {
355 	if (val > U16_MAX)
356 		return -ERANGE;
357 
358 	return i2c_smbus_write_word_swapped(data->client, data_reg, val);
359 }
360 
361 
362 static int vcnl4000_measure(struct vcnl4000_data *data, u8 req_mask,
363 				u8 rdy_mask, u8 data_reg, int *val)
364 {
365 	int tries = 20;
366 	int ret;
367 
368 	mutex_lock(&data->vcnl4000_lock);
369 
370 	ret = i2c_smbus_write_byte_data(data->client, VCNL4000_COMMAND,
371 					req_mask);
372 	if (ret < 0)
373 		goto fail;
374 
375 	/* wait for data to become ready */
376 	while (tries--) {
377 		ret = i2c_smbus_read_byte_data(data->client, VCNL4000_COMMAND);
378 		if (ret < 0)
379 			goto fail;
380 		if (ret & rdy_mask)
381 			break;
382 		msleep(20); /* measurement takes up to 100 ms */
383 	}
384 
385 	if (tries < 0) {
386 		dev_err(&data->client->dev,
387 			"vcnl4000_measure() failed, data not ready\n");
388 		ret = -EIO;
389 		goto fail;
390 	}
391 
392 	ret = vcnl4000_read_data(data, data_reg, val);
393 	if (ret < 0)
394 		goto fail;
395 
396 	mutex_unlock(&data->vcnl4000_lock);
397 
398 	return 0;
399 
400 fail:
401 	mutex_unlock(&data->vcnl4000_lock);
402 	return ret;
403 }
404 
405 static int vcnl4200_measure(struct vcnl4000_data *data,
406 		struct vcnl4200_channel *chan, int *val)
407 {
408 	int ret;
409 	s64 delta;
410 	ktime_t next_measurement;
411 
412 	mutex_lock(&chan->lock);
413 
414 	next_measurement = ktime_add(chan->last_measurement,
415 			chan->sampling_rate);
416 	delta = ktime_us_delta(next_measurement, ktime_get());
417 	if (delta > 0)
418 		usleep_range(delta, delta + 500);
419 	chan->last_measurement = ktime_get();
420 
421 	mutex_unlock(&chan->lock);
422 
423 	ret = i2c_smbus_read_word_data(data->client, chan->reg);
424 	if (ret < 0)
425 		return ret;
426 
427 	*val = ret;
428 
429 	return 0;
430 }
431 
432 static int vcnl4000_measure_light(struct vcnl4000_data *data, int *val)
433 {
434 	return vcnl4000_measure(data,
435 			VCNL4000_AL_OD, VCNL4000_AL_RDY,
436 			VCNL4000_AL_RESULT_HI, val);
437 }
438 
439 static int vcnl4200_measure_light(struct vcnl4000_data *data, int *val)
440 {
441 	return vcnl4200_measure(data, &data->vcnl4200_al, val);
442 }
443 
444 static int vcnl4000_measure_proximity(struct vcnl4000_data *data, int *val)
445 {
446 	return vcnl4000_measure(data,
447 			VCNL4000_PS_OD, VCNL4000_PS_RDY,
448 			VCNL4000_PS_RESULT_HI, val);
449 }
450 
451 static int vcnl4200_measure_proximity(struct vcnl4000_data *data, int *val)
452 {
453 	return vcnl4200_measure(data, &data->vcnl4200_ps, val);
454 }
455 
456 static int vcnl4010_read_proxy_samp_freq(struct vcnl4000_data *data, int *val,
457 					 int *val2)
458 {
459 	int ret;
460 
461 	ret = i2c_smbus_read_byte_data(data->client, VCNL4010_PROX_RATE);
462 	if (ret < 0)
463 		return ret;
464 
465 	if (ret >= ARRAY_SIZE(vcnl4010_prox_sampling_frequency))
466 		return -EINVAL;
467 
468 	*val = vcnl4010_prox_sampling_frequency[ret][0];
469 	*val2 = vcnl4010_prox_sampling_frequency[ret][1];
470 
471 	return 0;
472 }
473 
474 static bool vcnl4010_is_in_periodic_mode(struct vcnl4000_data *data)
475 {
476 	int ret;
477 
478 	ret = i2c_smbus_read_byte_data(data->client, VCNL4000_COMMAND);
479 	if (ret < 0)
480 		return false;
481 
482 	return !!(ret & VCNL4000_SELF_TIMED_EN);
483 }
484 
485 static int vcnl4000_set_pm_runtime_state(struct vcnl4000_data *data, bool on)
486 {
487 	struct device *dev = &data->client->dev;
488 	int ret;
489 
490 	if (on) {
491 		ret = pm_runtime_resume_and_get(dev);
492 	} else {
493 		pm_runtime_mark_last_busy(dev);
494 		ret = pm_runtime_put_autosuspend(dev);
495 	}
496 
497 	return ret;
498 }
499 
500 static int vcnl4040_read_ps_it(struct vcnl4000_data *data, int *val, int *val2)
501 {
502 	int ret;
503 
504 	ret = i2c_smbus_read_word_data(data->client, VCNL4200_PS_CONF1);
505 	if (ret < 0)
506 		return ret;
507 
508 	ret = FIELD_GET(VCNL4040_PS_CONF2_PS_IT, ret);
509 
510 	if (ret >= ARRAY_SIZE(vcnl4040_ps_it_times))
511 		return -EINVAL;
512 
513 	*val = vcnl4040_ps_it_times[ret][0];
514 	*val2 = vcnl4040_ps_it_times[ret][1];
515 
516 	return 0;
517 }
518 
519 static ssize_t vcnl4040_write_ps_it(struct vcnl4000_data *data, int val)
520 {
521 	unsigned int i;
522 	int ret, index = -1;
523 	u16 regval;
524 
525 	for (i = 0; i < ARRAY_SIZE(vcnl4040_ps_it_times); i++) {
526 		if (val == vcnl4040_ps_it_times[i][1]) {
527 			index = i;
528 			break;
529 		}
530 	}
531 
532 	if (index < 0)
533 		return -EINVAL;
534 
535 	mutex_lock(&data->vcnl4000_lock);
536 
537 	ret = i2c_smbus_read_word_data(data->client, VCNL4200_PS_CONF1);
538 	if (ret < 0)
539 		goto out;
540 
541 	regval = (ret & ~VCNL4040_PS_CONF2_PS_IT) |
542 	    FIELD_PREP(VCNL4040_PS_CONF2_PS_IT, index);
543 	ret = i2c_smbus_write_word_data(data->client, VCNL4200_PS_CONF1,
544 					regval);
545 
546 out:
547 	mutex_unlock(&data->vcnl4000_lock);
548 	return ret;
549 }
550 
551 static int vcnl4000_read_raw(struct iio_dev *indio_dev,
552 				struct iio_chan_spec const *chan,
553 				int *val, int *val2, long mask)
554 {
555 	int ret;
556 	struct vcnl4000_data *data = iio_priv(indio_dev);
557 
558 	switch (mask) {
559 	case IIO_CHAN_INFO_RAW:
560 		ret = vcnl4000_set_pm_runtime_state(data, true);
561 		if  (ret < 0)
562 			return ret;
563 
564 		switch (chan->type) {
565 		case IIO_LIGHT:
566 			ret = data->chip_spec->measure_light(data, val);
567 			if (!ret)
568 				ret = IIO_VAL_INT;
569 			break;
570 		case IIO_PROXIMITY:
571 			ret = data->chip_spec->measure_proximity(data, val);
572 			if (!ret)
573 				ret = IIO_VAL_INT;
574 			break;
575 		default:
576 			ret = -EINVAL;
577 		}
578 		vcnl4000_set_pm_runtime_state(data, false);
579 		return ret;
580 	case IIO_CHAN_INFO_SCALE:
581 		if (chan->type != IIO_LIGHT)
582 			return -EINVAL;
583 
584 		*val = 0;
585 		*val2 = data->al_scale;
586 		return IIO_VAL_INT_PLUS_MICRO;
587 	case IIO_CHAN_INFO_INT_TIME:
588 		if (chan->type != IIO_PROXIMITY)
589 			return -EINVAL;
590 		ret = vcnl4040_read_ps_it(data, val, val2);
591 		if (ret < 0)
592 			return ret;
593 		return IIO_VAL_INT_PLUS_MICRO;
594 	default:
595 		return -EINVAL;
596 	}
597 }
598 
599 static int vcnl4040_write_raw(struct iio_dev *indio_dev,
600 			      struct iio_chan_spec const *chan,
601 			      int val, int val2, long mask)
602 {
603 	struct vcnl4000_data *data = iio_priv(indio_dev);
604 
605 	switch (mask) {
606 	case IIO_CHAN_INFO_INT_TIME:
607 		if (val != 0)
608 			return -EINVAL;
609 		if (chan->type != IIO_PROXIMITY)
610 			return -EINVAL;
611 		return vcnl4040_write_ps_it(data, val2);
612 	default:
613 		return -EINVAL;
614 	}
615 }
616 
617 static int vcnl4040_read_avail(struct iio_dev *indio_dev,
618 			       struct iio_chan_spec const *chan,
619 			       const int **vals, int *type, int *length,
620 			       long mask)
621 {
622 	switch (mask) {
623 	case IIO_CHAN_INFO_INT_TIME:
624 		*vals = (int *)vcnl4040_ps_it_times;
625 		*type = IIO_VAL_INT_PLUS_MICRO;
626 		*length = 2 * ARRAY_SIZE(vcnl4040_ps_it_times);
627 		return IIO_AVAIL_LIST;
628 	default:
629 		return -EINVAL;
630 	}
631 }
632 
633 static int vcnl4010_read_raw(struct iio_dev *indio_dev,
634 			     struct iio_chan_spec const *chan,
635 			     int *val, int *val2, long mask)
636 {
637 	int ret;
638 	struct vcnl4000_data *data = iio_priv(indio_dev);
639 
640 	switch (mask) {
641 	case IIO_CHAN_INFO_RAW:
642 	case IIO_CHAN_INFO_SCALE:
643 		ret = iio_device_claim_direct_mode(indio_dev);
644 		if (ret)
645 			return ret;
646 
647 		/* Protect against event capture. */
648 		if (vcnl4010_is_in_periodic_mode(data)) {
649 			ret = -EBUSY;
650 		} else {
651 			ret = vcnl4000_read_raw(indio_dev, chan, val, val2,
652 						mask);
653 		}
654 
655 		iio_device_release_direct_mode(indio_dev);
656 		return ret;
657 	case IIO_CHAN_INFO_SAMP_FREQ:
658 		switch (chan->type) {
659 		case IIO_PROXIMITY:
660 			ret = vcnl4010_read_proxy_samp_freq(data, val, val2);
661 			if (ret < 0)
662 				return ret;
663 			return IIO_VAL_INT_PLUS_MICRO;
664 		default:
665 			return -EINVAL;
666 		}
667 	default:
668 		return -EINVAL;
669 	}
670 }
671 
672 static int vcnl4010_read_avail(struct iio_dev *indio_dev,
673 			       struct iio_chan_spec const *chan,
674 			       const int **vals, int *type, int *length,
675 			       long mask)
676 {
677 	switch (mask) {
678 	case IIO_CHAN_INFO_SAMP_FREQ:
679 		*vals = (int *)vcnl4010_prox_sampling_frequency;
680 		*type = IIO_VAL_INT_PLUS_MICRO;
681 		*length = 2 * ARRAY_SIZE(vcnl4010_prox_sampling_frequency);
682 		return IIO_AVAIL_LIST;
683 	default:
684 		return -EINVAL;
685 	}
686 }
687 
688 static int vcnl4010_write_proxy_samp_freq(struct vcnl4000_data *data, int val,
689 					  int val2)
690 {
691 	unsigned int i;
692 	int index = -1;
693 
694 	for (i = 0; i < ARRAY_SIZE(vcnl4010_prox_sampling_frequency); i++) {
695 		if (val == vcnl4010_prox_sampling_frequency[i][0] &&
696 		    val2 == vcnl4010_prox_sampling_frequency[i][1]) {
697 			index = i;
698 			break;
699 		}
700 	}
701 
702 	if (index < 0)
703 		return -EINVAL;
704 
705 	return i2c_smbus_write_byte_data(data->client, VCNL4010_PROX_RATE,
706 					 index);
707 }
708 
709 static int vcnl4010_write_raw(struct iio_dev *indio_dev,
710 			      struct iio_chan_spec const *chan,
711 			      int val, int val2, long mask)
712 {
713 	int ret;
714 	struct vcnl4000_data *data = iio_priv(indio_dev);
715 
716 	ret = iio_device_claim_direct_mode(indio_dev);
717 	if (ret)
718 		return ret;
719 
720 	/* Protect against event capture. */
721 	if (vcnl4010_is_in_periodic_mode(data)) {
722 		ret = -EBUSY;
723 		goto end;
724 	}
725 
726 	switch (mask) {
727 	case IIO_CHAN_INFO_SAMP_FREQ:
728 		switch (chan->type) {
729 		case IIO_PROXIMITY:
730 			ret = vcnl4010_write_proxy_samp_freq(data, val, val2);
731 			goto end;
732 		default:
733 			ret = -EINVAL;
734 			goto end;
735 		}
736 	default:
737 		ret = -EINVAL;
738 		goto end;
739 	}
740 
741 end:
742 	iio_device_release_direct_mode(indio_dev);
743 	return ret;
744 }
745 
746 static int vcnl4010_read_event(struct iio_dev *indio_dev,
747 			       const struct iio_chan_spec *chan,
748 			       enum iio_event_type type,
749 			       enum iio_event_direction dir,
750 			       enum iio_event_info info,
751 			       int *val, int *val2)
752 {
753 	int ret;
754 	struct vcnl4000_data *data = iio_priv(indio_dev);
755 
756 	switch (info) {
757 	case IIO_EV_INFO_VALUE:
758 		switch (dir) {
759 		case IIO_EV_DIR_RISING:
760 			ret = vcnl4000_read_data(data, VCNL4010_HIGH_THR_HI,
761 						 val);
762 			if (ret < 0)
763 				return ret;
764 			return IIO_VAL_INT;
765 		case IIO_EV_DIR_FALLING:
766 			ret = vcnl4000_read_data(data, VCNL4010_LOW_THR_HI,
767 						 val);
768 			if (ret < 0)
769 				return ret;
770 			return IIO_VAL_INT;
771 		default:
772 			return -EINVAL;
773 		}
774 	default:
775 		return -EINVAL;
776 	}
777 }
778 
779 static int vcnl4010_write_event(struct iio_dev *indio_dev,
780 				const struct iio_chan_spec *chan,
781 				enum iio_event_type type,
782 				enum iio_event_direction dir,
783 				enum iio_event_info info,
784 				int val, int val2)
785 {
786 	int ret;
787 	struct vcnl4000_data *data = iio_priv(indio_dev);
788 
789 	switch (info) {
790 	case IIO_EV_INFO_VALUE:
791 		switch (dir) {
792 		case IIO_EV_DIR_RISING:
793 			ret = vcnl4000_write_data(data, VCNL4010_HIGH_THR_HI,
794 						  val);
795 			if (ret < 0)
796 				return ret;
797 			return IIO_VAL_INT;
798 		case IIO_EV_DIR_FALLING:
799 			ret = vcnl4000_write_data(data, VCNL4010_LOW_THR_HI,
800 						  val);
801 			if (ret < 0)
802 				return ret;
803 			return IIO_VAL_INT;
804 		default:
805 			return -EINVAL;
806 		}
807 	default:
808 		return -EINVAL;
809 	}
810 }
811 
812 static int vcnl4040_read_event(struct iio_dev *indio_dev,
813 			       const struct iio_chan_spec *chan,
814 			       enum iio_event_type type,
815 			       enum iio_event_direction dir,
816 			       enum iio_event_info info,
817 			       int *val, int *val2)
818 {
819 	int ret;
820 	struct vcnl4000_data *data = iio_priv(indio_dev);
821 
822 	switch (dir) {
823 	case IIO_EV_DIR_RISING:
824 		ret = i2c_smbus_read_word_data(data->client,
825 					       VCNL4040_PS_THDH_LM);
826 		if (ret < 0)
827 			return ret;
828 		*val = ret;
829 		return IIO_VAL_INT;
830 	case IIO_EV_DIR_FALLING:
831 		ret = i2c_smbus_read_word_data(data->client,
832 					       VCNL4040_PS_THDL_LM);
833 		if (ret < 0)
834 			return ret;
835 		*val = ret;
836 		return IIO_VAL_INT;
837 	default:
838 		return -EINVAL;
839 	}
840 }
841 
842 static int vcnl4040_write_event(struct iio_dev *indio_dev,
843 				const struct iio_chan_spec *chan,
844 				enum iio_event_type type,
845 				enum iio_event_direction dir,
846 				enum iio_event_info info,
847 				int val, int val2)
848 {
849 	int ret;
850 	struct vcnl4000_data *data = iio_priv(indio_dev);
851 
852 	switch (dir) {
853 	case IIO_EV_DIR_RISING:
854 		ret = i2c_smbus_write_word_data(data->client,
855 						VCNL4040_PS_THDH_LM, val);
856 		if (ret < 0)
857 			return ret;
858 		return IIO_VAL_INT;
859 	case IIO_EV_DIR_FALLING:
860 		ret = i2c_smbus_write_word_data(data->client,
861 						VCNL4040_PS_THDL_LM, val);
862 		if (ret < 0)
863 			return ret;
864 		return IIO_VAL_INT;
865 	default:
866 		return -EINVAL;
867 	}
868 }
869 
870 static bool vcnl4010_is_thr_enabled(struct vcnl4000_data *data)
871 {
872 	int ret;
873 
874 	ret = i2c_smbus_read_byte_data(data->client, VCNL4010_INT_CTRL);
875 	if (ret < 0)
876 		return false;
877 
878 	return !!(ret & VCNL4010_INT_THR_EN);
879 }
880 
881 static int vcnl4010_read_event_config(struct iio_dev *indio_dev,
882 				      const struct iio_chan_spec *chan,
883 				      enum iio_event_type type,
884 				      enum iio_event_direction dir)
885 {
886 	struct vcnl4000_data *data = iio_priv(indio_dev);
887 
888 	switch (chan->type) {
889 	case IIO_PROXIMITY:
890 		return vcnl4010_is_thr_enabled(data);
891 	default:
892 		return -EINVAL;
893 	}
894 }
895 
896 static int vcnl4010_config_threshold(struct iio_dev *indio_dev, bool state)
897 {
898 	struct vcnl4000_data *data = iio_priv(indio_dev);
899 	int ret;
900 	int icr;
901 	int command;
902 
903 	if (state) {
904 		ret = iio_device_claim_direct_mode(indio_dev);
905 		if (ret)
906 			return ret;
907 
908 		/* Enable periodic measurement of proximity data. */
909 		command = VCNL4000_SELF_TIMED_EN | VCNL4000_PROX_EN;
910 
911 		/*
912 		 * Enable interrupts on threshold, for proximity data by
913 		 * default.
914 		 */
915 		icr = VCNL4010_INT_THR_EN;
916 	} else {
917 		if (!vcnl4010_is_thr_enabled(data))
918 			return 0;
919 
920 		command = 0;
921 		icr = 0;
922 	}
923 
924 	ret = i2c_smbus_write_byte_data(data->client, VCNL4000_COMMAND,
925 					command);
926 	if (ret < 0)
927 		goto end;
928 
929 	ret = i2c_smbus_write_byte_data(data->client, VCNL4010_INT_CTRL, icr);
930 
931 end:
932 	if (state)
933 		iio_device_release_direct_mode(indio_dev);
934 
935 	return ret;
936 }
937 
938 static int vcnl4010_write_event_config(struct iio_dev *indio_dev,
939 				       const struct iio_chan_spec *chan,
940 				       enum iio_event_type type,
941 				       enum iio_event_direction dir,
942 				       int state)
943 {
944 	switch (chan->type) {
945 	case IIO_PROXIMITY:
946 		return vcnl4010_config_threshold(indio_dev, state);
947 	default:
948 		return -EINVAL;
949 	}
950 }
951 
952 static int vcnl4040_read_event_config(struct iio_dev *indio_dev,
953 				      const struct iio_chan_spec *chan,
954 				      enum iio_event_type type,
955 				      enum iio_event_direction dir)
956 {
957 	int ret;
958 	struct vcnl4000_data *data = iio_priv(indio_dev);
959 
960 	ret = i2c_smbus_read_word_data(data->client, VCNL4200_PS_CONF1);
961 	if (ret < 0)
962 		return ret;
963 
964 	data->ps_int = FIELD_GET(VCNL4040_PS_CONF2_PS_INT, ret);
965 
966 	return (dir == IIO_EV_DIR_RISING) ?
967 		FIELD_GET(VCNL4040_PS_IF_AWAY, ret) :
968 		FIELD_GET(VCNL4040_PS_IF_CLOSE, ret);
969 }
970 
971 static int vcnl4040_write_event_config(struct iio_dev *indio_dev,
972 				       const struct iio_chan_spec *chan,
973 				       enum iio_event_type type,
974 				       enum iio_event_direction dir, int state)
975 {
976 	int ret;
977 	u16 val, mask;
978 	struct vcnl4000_data *data = iio_priv(indio_dev);
979 
980 	mutex_lock(&data->vcnl4000_lock);
981 
982 	ret = i2c_smbus_read_word_data(data->client, VCNL4200_PS_CONF1);
983 	if (ret < 0)
984 		goto out;
985 
986 	if (dir == IIO_EV_DIR_RISING)
987 		mask = VCNL4040_PS_IF_AWAY;
988 	else
989 		mask = VCNL4040_PS_IF_CLOSE;
990 
991 	val = state ? (ret | mask) : (ret & ~mask);
992 
993 	data->ps_int = FIELD_GET(VCNL4040_PS_CONF2_PS_INT, val);
994 	ret = i2c_smbus_write_word_data(data->client, VCNL4200_PS_CONF1, val);
995 
996 out:
997 	mutex_unlock(&data->vcnl4000_lock);
998 	data->chip_spec->set_power_state(data, data->ps_int != 0);
999 
1000 	return ret;
1001 }
1002 
1003 static irqreturn_t vcnl4040_irq_thread(int irq, void *p)
1004 {
1005 	struct iio_dev *indio_dev = p;
1006 	struct vcnl4000_data *data = iio_priv(indio_dev);
1007 	int ret;
1008 
1009 	ret = i2c_smbus_read_word_data(data->client, VCNL4040_INT_FLAGS);
1010 	if (ret < 0)
1011 		return IRQ_HANDLED;
1012 
1013 	if (ret & VCNL4040_PS_IF_CLOSE) {
1014 		iio_push_event(indio_dev,
1015 			       IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 0,
1016 						    IIO_EV_TYPE_THRESH,
1017 						    IIO_EV_DIR_RISING),
1018 			       iio_get_time_ns(indio_dev));
1019 	}
1020 
1021 	if (ret & VCNL4040_PS_IF_AWAY) {
1022 		iio_push_event(indio_dev,
1023 			       IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 0,
1024 						    IIO_EV_TYPE_THRESH,
1025 						    IIO_EV_DIR_FALLING),
1026 			       iio_get_time_ns(indio_dev));
1027 	}
1028 
1029 	return IRQ_HANDLED;
1030 }
1031 
1032 static ssize_t vcnl4000_read_near_level(struct iio_dev *indio_dev,
1033 					uintptr_t priv,
1034 					const struct iio_chan_spec *chan,
1035 					char *buf)
1036 {
1037 	struct vcnl4000_data *data = iio_priv(indio_dev);
1038 
1039 	return sprintf(buf, "%u\n", data->near_level);
1040 }
1041 
1042 static irqreturn_t vcnl4010_irq_thread(int irq, void *p)
1043 {
1044 	struct iio_dev *indio_dev = p;
1045 	struct vcnl4000_data *data = iio_priv(indio_dev);
1046 	unsigned long isr;
1047 	int ret;
1048 
1049 	ret = i2c_smbus_read_byte_data(data->client, VCNL4010_ISR);
1050 	if (ret < 0)
1051 		goto end;
1052 
1053 	isr = ret;
1054 
1055 	if (isr & VCNL4010_INT_THR) {
1056 		if (test_bit(VCNL4010_INT_THR_LOW, &isr)) {
1057 			iio_push_event(indio_dev,
1058 				       IIO_UNMOD_EVENT_CODE(
1059 					       IIO_PROXIMITY,
1060 					       1,
1061 					       IIO_EV_TYPE_THRESH,
1062 					       IIO_EV_DIR_FALLING),
1063 				       iio_get_time_ns(indio_dev));
1064 		}
1065 
1066 		if (test_bit(VCNL4010_INT_THR_HIGH, &isr)) {
1067 			iio_push_event(indio_dev,
1068 				       IIO_UNMOD_EVENT_CODE(
1069 					       IIO_PROXIMITY,
1070 					       1,
1071 					       IIO_EV_TYPE_THRESH,
1072 					       IIO_EV_DIR_RISING),
1073 				       iio_get_time_ns(indio_dev));
1074 		}
1075 
1076 		i2c_smbus_write_byte_data(data->client, VCNL4010_ISR,
1077 					  isr & VCNL4010_INT_THR);
1078 	}
1079 
1080 	if (isr & VCNL4010_INT_DRDY && iio_buffer_enabled(indio_dev))
1081 		iio_trigger_poll_chained(indio_dev->trig);
1082 
1083 end:
1084 	return IRQ_HANDLED;
1085 }
1086 
1087 static irqreturn_t vcnl4010_trigger_handler(int irq, void *p)
1088 {
1089 	struct iio_poll_func *pf = p;
1090 	struct iio_dev *indio_dev = pf->indio_dev;
1091 	struct vcnl4000_data *data = iio_priv(indio_dev);
1092 	const unsigned long *active_scan_mask = indio_dev->active_scan_mask;
1093 	u16 buffer[8] __aligned(8) = {0}; /* 1x16-bit + naturally aligned ts */
1094 	bool data_read = false;
1095 	unsigned long isr;
1096 	int val = 0;
1097 	int ret;
1098 
1099 	ret = i2c_smbus_read_byte_data(data->client, VCNL4010_ISR);
1100 	if (ret < 0)
1101 		goto end;
1102 
1103 	isr = ret;
1104 
1105 	if (test_bit(0, active_scan_mask)) {
1106 		if (test_bit(VCNL4010_INT_PROXIMITY, &isr)) {
1107 			ret = vcnl4000_read_data(data,
1108 						 VCNL4000_PS_RESULT_HI,
1109 						 &val);
1110 			if (ret < 0)
1111 				goto end;
1112 
1113 			buffer[0] = val;
1114 			data_read = true;
1115 		}
1116 	}
1117 
1118 	ret = i2c_smbus_write_byte_data(data->client, VCNL4010_ISR,
1119 					isr & VCNL4010_INT_DRDY);
1120 	if (ret < 0)
1121 		goto end;
1122 
1123 	if (!data_read)
1124 		goto end;
1125 
1126 	iio_push_to_buffers_with_timestamp(indio_dev, buffer,
1127 					   iio_get_time_ns(indio_dev));
1128 
1129 end:
1130 	iio_trigger_notify_done(indio_dev->trig);
1131 	return IRQ_HANDLED;
1132 }
1133 
1134 static int vcnl4010_buffer_postenable(struct iio_dev *indio_dev)
1135 {
1136 	struct vcnl4000_data *data = iio_priv(indio_dev);
1137 	int ret;
1138 	int cmd;
1139 
1140 	/* Do not enable the buffer if we are already capturing events. */
1141 	if (vcnl4010_is_in_periodic_mode(data))
1142 		return -EBUSY;
1143 
1144 	ret = i2c_smbus_write_byte_data(data->client, VCNL4010_INT_CTRL,
1145 					VCNL4010_INT_PROX_EN);
1146 	if (ret < 0)
1147 		return ret;
1148 
1149 	cmd = VCNL4000_SELF_TIMED_EN | VCNL4000_PROX_EN;
1150 	return i2c_smbus_write_byte_data(data->client, VCNL4000_COMMAND, cmd);
1151 }
1152 
1153 static int vcnl4010_buffer_predisable(struct iio_dev *indio_dev)
1154 {
1155 	struct vcnl4000_data *data = iio_priv(indio_dev);
1156 	int ret;
1157 
1158 	ret = i2c_smbus_write_byte_data(data->client, VCNL4010_INT_CTRL, 0);
1159 	if (ret < 0)
1160 		return ret;
1161 
1162 	return i2c_smbus_write_byte_data(data->client, VCNL4000_COMMAND, 0);
1163 }
1164 
1165 static const struct iio_buffer_setup_ops vcnl4010_buffer_ops = {
1166 	.postenable = &vcnl4010_buffer_postenable,
1167 	.predisable = &vcnl4010_buffer_predisable,
1168 };
1169 
1170 static const struct iio_chan_spec_ext_info vcnl4000_ext_info[] = {
1171 	{
1172 		.name = "nearlevel",
1173 		.shared = IIO_SEPARATE,
1174 		.read = vcnl4000_read_near_level,
1175 	},
1176 	{ /* sentinel */ }
1177 };
1178 
1179 static const struct iio_event_spec vcnl4000_event_spec[] = {
1180 	{
1181 		.type = IIO_EV_TYPE_THRESH,
1182 		.dir = IIO_EV_DIR_RISING,
1183 		.mask_separate = BIT(IIO_EV_INFO_VALUE),
1184 	}, {
1185 		.type = IIO_EV_TYPE_THRESH,
1186 		.dir = IIO_EV_DIR_FALLING,
1187 		.mask_separate = BIT(IIO_EV_INFO_VALUE),
1188 	}, {
1189 		.type = IIO_EV_TYPE_THRESH,
1190 		.dir = IIO_EV_DIR_EITHER,
1191 		.mask_separate = BIT(IIO_EV_INFO_ENABLE),
1192 	}
1193 };
1194 
1195 static const struct iio_event_spec vcnl4040_event_spec[] = {
1196 	{
1197 		.type = IIO_EV_TYPE_THRESH,
1198 		.dir = IIO_EV_DIR_RISING,
1199 		.mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE),
1200 	}, {
1201 		.type = IIO_EV_TYPE_THRESH,
1202 		.dir = IIO_EV_DIR_FALLING,
1203 		.mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE),
1204 	},
1205 };
1206 
1207 static const struct iio_chan_spec vcnl4000_channels[] = {
1208 	{
1209 		.type = IIO_LIGHT,
1210 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1211 			BIT(IIO_CHAN_INFO_SCALE),
1212 	}, {
1213 		.type = IIO_PROXIMITY,
1214 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1215 		.ext_info = vcnl4000_ext_info,
1216 	}
1217 };
1218 
1219 static const struct iio_chan_spec vcnl4010_channels[] = {
1220 	{
1221 		.type = IIO_LIGHT,
1222 		.scan_index = -1,
1223 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1224 			BIT(IIO_CHAN_INFO_SCALE),
1225 	}, {
1226 		.type = IIO_PROXIMITY,
1227 		.scan_index = 0,
1228 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1229 			BIT(IIO_CHAN_INFO_SAMP_FREQ),
1230 		.info_mask_separate_available = BIT(IIO_CHAN_INFO_SAMP_FREQ),
1231 		.event_spec = vcnl4000_event_spec,
1232 		.num_event_specs = ARRAY_SIZE(vcnl4000_event_spec),
1233 		.ext_info = vcnl4000_ext_info,
1234 		.scan_type = {
1235 			.sign = 'u',
1236 			.realbits = 16,
1237 			.storagebits = 16,
1238 			.endianness = IIO_CPU,
1239 		},
1240 	},
1241 	IIO_CHAN_SOFT_TIMESTAMP(1),
1242 };
1243 
1244 static const struct iio_chan_spec vcnl4040_channels[] = {
1245 	{
1246 		.type = IIO_LIGHT,
1247 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1248 			BIT(IIO_CHAN_INFO_SCALE),
1249 	}, {
1250 		.type = IIO_PROXIMITY,
1251 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1252 			BIT(IIO_CHAN_INFO_INT_TIME),
1253 		.info_mask_separate_available = BIT(IIO_CHAN_INFO_INT_TIME),
1254 		.ext_info = vcnl4000_ext_info,
1255 		.event_spec = vcnl4040_event_spec,
1256 		.num_event_specs = ARRAY_SIZE(vcnl4040_event_spec),
1257 	}
1258 };
1259 
1260 static const struct iio_info vcnl4000_info = {
1261 	.read_raw = vcnl4000_read_raw,
1262 };
1263 
1264 static const struct iio_info vcnl4010_info = {
1265 	.read_raw = vcnl4010_read_raw,
1266 	.read_avail = vcnl4010_read_avail,
1267 	.write_raw = vcnl4010_write_raw,
1268 	.read_event_value = vcnl4010_read_event,
1269 	.write_event_value = vcnl4010_write_event,
1270 	.read_event_config = vcnl4010_read_event_config,
1271 	.write_event_config = vcnl4010_write_event_config,
1272 };
1273 
1274 static const struct iio_info vcnl4040_info = {
1275 	.read_raw = vcnl4000_read_raw,
1276 	.write_raw = vcnl4040_write_raw,
1277 	.read_event_value = vcnl4040_read_event,
1278 	.write_event_value = vcnl4040_write_event,
1279 	.read_event_config = vcnl4040_read_event_config,
1280 	.write_event_config = vcnl4040_write_event_config,
1281 	.read_avail = vcnl4040_read_avail,
1282 };
1283 
1284 static const struct vcnl4000_chip_spec vcnl4000_chip_spec_cfg[] = {
1285 	[VCNL4000] = {
1286 		.prod = "VCNL4000",
1287 		.init = vcnl4000_init,
1288 		.measure_light = vcnl4000_measure_light,
1289 		.measure_proximity = vcnl4000_measure_proximity,
1290 		.set_power_state = vcnl4000_set_power_state,
1291 		.channels = vcnl4000_channels,
1292 		.num_channels = ARRAY_SIZE(vcnl4000_channels),
1293 		.info = &vcnl4000_info,
1294 	},
1295 	[VCNL4010] = {
1296 		.prod = "VCNL4010/4020",
1297 		.init = vcnl4000_init,
1298 		.measure_light = vcnl4000_measure_light,
1299 		.measure_proximity = vcnl4000_measure_proximity,
1300 		.set_power_state = vcnl4000_set_power_state,
1301 		.channels = vcnl4010_channels,
1302 		.num_channels = ARRAY_SIZE(vcnl4010_channels),
1303 		.info = &vcnl4010_info,
1304 		.irq_thread = vcnl4010_irq_thread,
1305 		.trig_buffer_func = vcnl4010_trigger_handler,
1306 		.buffer_setup_ops = &vcnl4010_buffer_ops,
1307 	},
1308 	[VCNL4040] = {
1309 		.prod = "VCNL4040",
1310 		.init = vcnl4200_init,
1311 		.measure_light = vcnl4200_measure_light,
1312 		.measure_proximity = vcnl4200_measure_proximity,
1313 		.set_power_state = vcnl4200_set_power_state,
1314 		.channels = vcnl4040_channels,
1315 		.num_channels = ARRAY_SIZE(vcnl4040_channels),
1316 		.info = &vcnl4040_info,
1317 		.irq_thread = vcnl4040_irq_thread,
1318 	},
1319 	[VCNL4200] = {
1320 		.prod = "VCNL4200",
1321 		.init = vcnl4200_init,
1322 		.measure_light = vcnl4200_measure_light,
1323 		.measure_proximity = vcnl4200_measure_proximity,
1324 		.set_power_state = vcnl4200_set_power_state,
1325 		.channels = vcnl4000_channels,
1326 		.num_channels = ARRAY_SIZE(vcnl4000_channels),
1327 		.info = &vcnl4000_info,
1328 	},
1329 };
1330 
1331 static const struct iio_trigger_ops vcnl4010_trigger_ops = {
1332 	.validate_device = iio_trigger_validate_own_device,
1333 };
1334 
1335 static int vcnl4010_probe_trigger(struct iio_dev *indio_dev)
1336 {
1337 	struct vcnl4000_data *data = iio_priv(indio_dev);
1338 	struct i2c_client *client = data->client;
1339 	struct iio_trigger *trigger;
1340 
1341 	trigger = devm_iio_trigger_alloc(&client->dev, "%s-dev%d",
1342 					 indio_dev->name,
1343 					 iio_device_id(indio_dev));
1344 	if (!trigger)
1345 		return -ENOMEM;
1346 
1347 	trigger->ops = &vcnl4010_trigger_ops;
1348 	iio_trigger_set_drvdata(trigger, indio_dev);
1349 
1350 	return devm_iio_trigger_register(&client->dev, trigger);
1351 }
1352 
1353 static int vcnl4000_probe(struct i2c_client *client)
1354 {
1355 	const struct i2c_device_id *id = i2c_client_get_device_id(client);
1356 	struct vcnl4000_data *data;
1357 	struct iio_dev *indio_dev;
1358 	int ret;
1359 
1360 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
1361 	if (!indio_dev)
1362 		return -ENOMEM;
1363 
1364 	data = iio_priv(indio_dev);
1365 	i2c_set_clientdata(client, indio_dev);
1366 	data->client = client;
1367 	data->id = id->driver_data;
1368 	data->chip_spec = &vcnl4000_chip_spec_cfg[data->id];
1369 
1370 	ret = data->chip_spec->init(data);
1371 	if (ret < 0)
1372 		return ret;
1373 
1374 	dev_dbg(&client->dev, "%s Ambient light/proximity sensor, Rev: %02x\n",
1375 		data->chip_spec->prod, data->rev);
1376 
1377 	if (device_property_read_u32(&client->dev, "proximity-near-level",
1378 				     &data->near_level))
1379 		data->near_level = 0;
1380 
1381 	indio_dev->info = data->chip_spec->info;
1382 	indio_dev->channels = data->chip_spec->channels;
1383 	indio_dev->num_channels = data->chip_spec->num_channels;
1384 	indio_dev->name = VCNL4000_DRV_NAME;
1385 	indio_dev->modes = INDIO_DIRECT_MODE;
1386 
1387 	if (data->chip_spec->trig_buffer_func &&
1388 	    data->chip_spec->buffer_setup_ops) {
1389 		ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev,
1390 						      NULL,
1391 						      data->chip_spec->trig_buffer_func,
1392 						      data->chip_spec->buffer_setup_ops);
1393 		if (ret < 0) {
1394 			dev_err(&client->dev,
1395 				"unable to setup iio triggered buffer\n");
1396 			return ret;
1397 		}
1398 	}
1399 
1400 	if (client->irq && data->chip_spec->irq_thread) {
1401 		ret = devm_request_threaded_irq(&client->dev, client->irq,
1402 						NULL, data->chip_spec->irq_thread,
1403 						IRQF_TRIGGER_FALLING |
1404 						IRQF_ONESHOT,
1405 						"vcnl4000_irq",
1406 						indio_dev);
1407 		if (ret < 0) {
1408 			dev_err(&client->dev, "irq request failed\n");
1409 			return ret;
1410 		}
1411 
1412 		ret = vcnl4010_probe_trigger(indio_dev);
1413 		if (ret < 0)
1414 			return ret;
1415 	}
1416 
1417 	ret = pm_runtime_set_active(&client->dev);
1418 	if (ret < 0)
1419 		goto fail_poweroff;
1420 
1421 	ret = iio_device_register(indio_dev);
1422 	if (ret < 0)
1423 		goto fail_poweroff;
1424 
1425 	pm_runtime_enable(&client->dev);
1426 	pm_runtime_set_autosuspend_delay(&client->dev, VCNL4000_SLEEP_DELAY_MS);
1427 	pm_runtime_use_autosuspend(&client->dev);
1428 
1429 	return 0;
1430 fail_poweroff:
1431 	data->chip_spec->set_power_state(data, false);
1432 	return ret;
1433 }
1434 
1435 static const struct of_device_id vcnl_4000_of_match[] = {
1436 	{
1437 		.compatible = "vishay,vcnl4000",
1438 		.data = (void *)VCNL4000,
1439 	},
1440 	{
1441 		.compatible = "vishay,vcnl4010",
1442 		.data = (void *)VCNL4010,
1443 	},
1444 	{
1445 		.compatible = "vishay,vcnl4020",
1446 		.data = (void *)VCNL4010,
1447 	},
1448 	{
1449 		.compatible = "vishay,vcnl4040",
1450 		.data = (void *)VCNL4040,
1451 	},
1452 	{
1453 		.compatible = "vishay,vcnl4200",
1454 		.data = (void *)VCNL4200,
1455 	},
1456 	{},
1457 };
1458 MODULE_DEVICE_TABLE(of, vcnl_4000_of_match);
1459 
1460 static void vcnl4000_remove(struct i2c_client *client)
1461 {
1462 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
1463 	struct vcnl4000_data *data = iio_priv(indio_dev);
1464 	int ret;
1465 
1466 	pm_runtime_dont_use_autosuspend(&client->dev);
1467 	pm_runtime_disable(&client->dev);
1468 	iio_device_unregister(indio_dev);
1469 	pm_runtime_set_suspended(&client->dev);
1470 
1471 	ret = data->chip_spec->set_power_state(data, false);
1472 	if (ret)
1473 		dev_warn(&client->dev, "Failed to power down (%pe)\n",
1474 			 ERR_PTR(ret));
1475 }
1476 
1477 static int vcnl4000_runtime_suspend(struct device *dev)
1478 {
1479 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1480 	struct vcnl4000_data *data = iio_priv(indio_dev);
1481 
1482 	return data->chip_spec->set_power_state(data, false);
1483 }
1484 
1485 static int vcnl4000_runtime_resume(struct device *dev)
1486 {
1487 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1488 	struct vcnl4000_data *data = iio_priv(indio_dev);
1489 
1490 	return data->chip_spec->set_power_state(data, true);
1491 }
1492 
1493 static DEFINE_RUNTIME_DEV_PM_OPS(vcnl4000_pm_ops, vcnl4000_runtime_suspend,
1494 				 vcnl4000_runtime_resume, NULL);
1495 
1496 static struct i2c_driver vcnl4000_driver = {
1497 	.driver = {
1498 		.name   = VCNL4000_DRV_NAME,
1499 		.pm	= pm_ptr(&vcnl4000_pm_ops),
1500 		.of_match_table = vcnl_4000_of_match,
1501 	},
1502 	.probe_new = vcnl4000_probe,
1503 	.id_table = vcnl4000_id,
1504 	.remove	= vcnl4000_remove,
1505 };
1506 
1507 module_i2c_driver(vcnl4000_driver);
1508 
1509 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
1510 MODULE_AUTHOR("Mathieu Othacehe <m.othacehe@gmail.com>");
1511 MODULE_DESCRIPTION("Vishay VCNL4000 proximity/ambient light sensor driver");
1512 MODULE_LICENSE("GPL");
1513