xref: /linux/drivers/iio/light/vl6180.c (revision 79d2e1919a2728ef49d938eb20ebd5903c14dfb0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * vl6180.c - Support for STMicroelectronics VL6180 ALS, range and proximity
4  * sensor
5  *
6  * Copyright 2017 Peter Meerwald-Stadler <pmeerw@pmeerw.net>
7  * Copyright 2017 Manivannan Sadhasivam <manivannanece23@gmail.com>
8  *
9  * IIO driver for VL6180 (7-bit I2C slave address 0x29)
10  *
11  * Range: 0 to 100mm
12  * ALS: < 1 Lux up to 100 kLux
13  * IR: 850nm
14  *
15  * TODO: irq, threshold events, continuous mode, hardware buffer
16  */
17 
18 #include <linux/module.h>
19 #include <linux/mod_devicetable.h>
20 #include <linux/i2c.h>
21 #include <linux/mutex.h>
22 #include <linux/err.h>
23 #include <linux/delay.h>
24 #include <linux/util_macros.h>
25 
26 #include <linux/iio/iio.h>
27 #include <linux/iio/sysfs.h>
28 #include <linux/iio/buffer.h>
29 #include <linux/iio/trigger.h>
30 #include <linux/iio/trigger_consumer.h>
31 #include <linux/iio/triggered_buffer.h>
32 
33 #define VL6180_DRV_NAME "vl6180"
34 
35 /* Device identification register and value */
36 #define VL6180_MODEL_ID	0x000
37 #define VL6180_MODEL_ID_VAL 0xb4
38 
39 /* Configuration registers */
40 #define VL6180_INTR_CONFIG 0x014
41 #define VL6180_INTR_CLEAR 0x015
42 #define VL6180_OUT_OF_RESET 0x016
43 #define VL6180_HOLD 0x017
44 #define VL6180_RANGE_START 0x018
45 #define VL6180_RANGE_INTER_MEAS_TIME 0x01b
46 #define VL6180_ALS_START 0x038
47 #define VL6180_ALS_INTER_MEAS_TIME 0x03e
48 #define VL6180_ALS_GAIN 0x03f
49 #define VL6180_ALS_IT 0x040
50 
51 /* Status registers */
52 #define VL6180_RANGE_STATUS 0x04d
53 #define VL6180_ALS_STATUS 0x04e
54 #define VL6180_INTR_STATUS 0x04f
55 
56 /* Result value registers */
57 #define VL6180_ALS_VALUE 0x050
58 #define VL6180_RANGE_VALUE 0x062
59 #define VL6180_RANGE_RATE 0x066
60 
61 /* bits of the RANGE_START and ALS_START register */
62 #define VL6180_MODE_CONT BIT(1) /* continuous mode */
63 #define VL6180_STARTSTOP BIT(0) /* start measurement, auto-reset */
64 
65 /* bits of the INTR_STATUS and INTR_CONFIG register */
66 #define VL6180_ALS_READY BIT(5)
67 #define VL6180_RANGE_READY BIT(2)
68 
69 /* bits of the INTR_CLEAR register */
70 #define VL6180_CLEAR_ERROR BIT(2)
71 #define VL6180_CLEAR_ALS BIT(1)
72 #define VL6180_CLEAR_RANGE BIT(0)
73 
74 /* bits of the HOLD register */
75 #define VL6180_HOLD_ON BIT(0)
76 
77 /* default value for the ALS_IT register */
78 #define VL6180_ALS_IT_100 0x63 /* 100 ms */
79 
80 /* values for the ALS_GAIN register */
81 #define VL6180_ALS_GAIN_1 0x46
82 #define VL6180_ALS_GAIN_1_25 0x45
83 #define VL6180_ALS_GAIN_1_67 0x44
84 #define VL6180_ALS_GAIN_2_5 0x43
85 #define VL6180_ALS_GAIN_5 0x42
86 #define VL6180_ALS_GAIN_10 0x41
87 #define VL6180_ALS_GAIN_20 0x40
88 #define VL6180_ALS_GAIN_40 0x47
89 
90 struct vl6180_data {
91 	struct i2c_client *client;
92 	struct mutex lock;
93 	struct completion completion;
94 	struct iio_trigger *trig;
95 	unsigned int als_gain_milli;
96 	unsigned int als_it_ms;
97 	unsigned int als_meas_rate;
98 	unsigned int range_meas_rate;
99 
100 	struct {
101 		u16 chan[2];
102 		aligned_s64 timestamp;
103 	} scan;
104 };
105 
106 enum { VL6180_ALS, VL6180_RANGE, VL6180_PROX };
107 
108 /**
109  * struct vl6180_chan_regs - Registers for accessing channels
110  * @drdy_mask:			Data ready bit in status register
111  * @start_reg:			Conversion start register
112  * @value_reg:			Result value register
113  * @word:			Register word length
114  */
115 struct vl6180_chan_regs {
116 	u8 drdy_mask;
117 	u16 start_reg, value_reg;
118 	bool word;
119 };
120 
121 static const struct vl6180_chan_regs vl6180_chan_regs_table[] = {
122 	[VL6180_ALS] = {
123 		.drdy_mask = VL6180_ALS_READY,
124 		.start_reg = VL6180_ALS_START,
125 		.value_reg = VL6180_ALS_VALUE,
126 		.word = true,
127 	},
128 	[VL6180_RANGE] = {
129 		.drdy_mask = VL6180_RANGE_READY,
130 		.start_reg = VL6180_RANGE_START,
131 		.value_reg = VL6180_RANGE_VALUE,
132 		.word = false,
133 	},
134 	[VL6180_PROX] = {
135 		.drdy_mask = VL6180_RANGE_READY,
136 		.start_reg = VL6180_RANGE_START,
137 		.value_reg = VL6180_RANGE_RATE,
138 		.word = true,
139 	},
140 };
141 
142 static int vl6180_read(struct i2c_client *client, u16 cmd, void *databuf,
143 		       u8 len)
144 {
145 	__be16 cmdbuf = cpu_to_be16(cmd);
146 	struct i2c_msg msgs[2] = {
147 		{ .addr = client->addr, .len = sizeof(cmdbuf), .buf = (u8 *) &cmdbuf },
148 		{ .addr = client->addr, .len = len, .buf = databuf,
149 		  .flags = I2C_M_RD } };
150 	int ret;
151 
152 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
153 	if (ret < 0)
154 		dev_err(&client->dev, "failed reading register 0x%04x\n", cmd);
155 
156 	return ret;
157 }
158 
159 static int vl6180_read_byte(struct i2c_client *client, u16 cmd)
160 {
161 	u8 data;
162 	int ret;
163 
164 	ret = vl6180_read(client, cmd, &data, sizeof(data));
165 	if (ret < 0)
166 		return ret;
167 
168 	return data;
169 }
170 
171 static int vl6180_read_word(struct i2c_client *client, u16 cmd)
172 {
173 	__be16 data;
174 	int ret;
175 
176 	ret = vl6180_read(client, cmd, &data, sizeof(data));
177 	if (ret < 0)
178 		return ret;
179 
180 	return be16_to_cpu(data);
181 }
182 
183 static int vl6180_write_byte(struct i2c_client *client, u16 cmd, u8 val)
184 {
185 	u8 buf[3];
186 	struct i2c_msg msgs[1] = {
187 		{ .addr = client->addr, .len = sizeof(buf), .buf = (u8 *) &buf } };
188 	int ret;
189 
190 	buf[0] = cmd >> 8;
191 	buf[1] = cmd & 0xff;
192 	buf[2] = val;
193 
194 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
195 	if (ret < 0) {
196 		dev_err(&client->dev, "failed writing register 0x%04x\n", cmd);
197 		return ret;
198 	}
199 
200 	return 0;
201 }
202 
203 static int vl6180_write_word(struct i2c_client *client, u16 cmd, u16 val)
204 {
205 	__be16 buf[2];
206 	struct i2c_msg msgs[1] = {
207 		{ .addr = client->addr, .len = sizeof(buf), .buf = (u8 *) &buf } };
208 	int ret;
209 
210 	buf[0] = cpu_to_be16(cmd);
211 	buf[1] = cpu_to_be16(val);
212 
213 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
214 	if (ret < 0) {
215 		dev_err(&client->dev, "failed writing register 0x%04x\n", cmd);
216 		return ret;
217 	}
218 
219 	return 0;
220 }
221 
222 static int vl6180_measure(struct vl6180_data *data, int addr)
223 {
224 	struct i2c_client *client = data->client;
225 	unsigned long time_left;
226 	int tries = 20, ret;
227 	u16 value;
228 
229 	mutex_lock(&data->lock);
230 	reinit_completion(&data->completion);
231 
232 	/* Start single shot measurement */
233 	ret = vl6180_write_byte(client,
234 		vl6180_chan_regs_table[addr].start_reg, VL6180_STARTSTOP);
235 	if (ret < 0)
236 		goto fail;
237 
238 	if (client->irq) {
239 		time_left = wait_for_completion_timeout(&data->completion, HZ / 10);
240 		if (time_left == 0) {
241 			ret = -ETIMEDOUT;
242 			goto fail;
243 		}
244 	} else {
245 		while (tries--) {
246 			ret = vl6180_read_byte(client, VL6180_INTR_STATUS);
247 			if (ret < 0)
248 				goto fail;
249 
250 			if (ret & vl6180_chan_regs_table[addr].drdy_mask)
251 				break;
252 			msleep(20);
253 		}
254 
255 		if (tries < 0) {
256 			ret = -EIO;
257 			goto fail;
258 		}
259 	}
260 
261 	/* Read result value from appropriate registers */
262 	ret = vl6180_chan_regs_table[addr].word ?
263 		vl6180_read_word(client, vl6180_chan_regs_table[addr].value_reg) :
264 		vl6180_read_byte(client, vl6180_chan_regs_table[addr].value_reg);
265 	if (ret < 0)
266 		goto fail;
267 	value = ret;
268 
269 	/* Clear the interrupt flag after data read */
270 	ret = vl6180_write_byte(client, VL6180_INTR_CLEAR,
271 		VL6180_CLEAR_ERROR | VL6180_CLEAR_ALS | VL6180_CLEAR_RANGE);
272 	if (ret < 0)
273 		goto fail;
274 
275 	ret = value;
276 
277 fail:
278 	mutex_unlock(&data->lock);
279 
280 	return ret;
281 }
282 
283 static const struct iio_chan_spec vl6180_channels[] = {
284 	{
285 		.type = IIO_LIGHT,
286 		.address = VL6180_ALS,
287 		.scan_index = VL6180_ALS,
288 		.scan_type = {
289 			.sign = 'u',
290 			.realbits = 16,
291 			.storagebits = 16,
292 		},
293 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
294 			BIT(IIO_CHAN_INFO_INT_TIME) |
295 			BIT(IIO_CHAN_INFO_SCALE) |
296 			BIT(IIO_CHAN_INFO_HARDWAREGAIN) |
297 			BIT(IIO_CHAN_INFO_SAMP_FREQ),
298 	}, {
299 		.type = IIO_DISTANCE,
300 		.address = VL6180_RANGE,
301 		.scan_index = VL6180_RANGE,
302 		.scan_type = {
303 			.sign = 'u',
304 			.realbits = 8,
305 			.storagebits = 8,
306 		},
307 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
308 			BIT(IIO_CHAN_INFO_SCALE) |
309 			BIT(IIO_CHAN_INFO_SAMP_FREQ),
310 	}, {
311 		.type = IIO_PROXIMITY,
312 		.address = VL6180_PROX,
313 		.scan_index = VL6180_PROX,
314 		.scan_type = {
315 			.sign = 'u',
316 			.realbits = 16,
317 			.storagebits = 16,
318 		},
319 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
320 	},
321 	IIO_CHAN_SOFT_TIMESTAMP(3),
322 };
323 
324 /*
325  * Available Ambient Light Sensor gain settings, 1/1000th, and
326  * corresponding setting for the VL6180_ALS_GAIN register
327  */
328 static const int vl6180_als_gain_tab[8] = {
329 	1000, 1250, 1670, 2500, 5000, 10000, 20000, 40000
330 };
331 static const u8 vl6180_als_gain_tab_bits[8] = {
332 	VL6180_ALS_GAIN_1,    VL6180_ALS_GAIN_1_25,
333 	VL6180_ALS_GAIN_1_67, VL6180_ALS_GAIN_2_5,
334 	VL6180_ALS_GAIN_5,    VL6180_ALS_GAIN_10,
335 	VL6180_ALS_GAIN_20,   VL6180_ALS_GAIN_40
336 };
337 
338 static int vl6180_read_raw(struct iio_dev *indio_dev,
339 				struct iio_chan_spec const *chan,
340 				int *val, int *val2, long mask)
341 {
342 	struct vl6180_data *data = iio_priv(indio_dev);
343 	int ret;
344 
345 	switch (mask) {
346 	case IIO_CHAN_INFO_RAW:
347 		ret = vl6180_measure(data, chan->address);
348 		if (ret < 0)
349 			return ret;
350 		*val = ret;
351 
352 		return IIO_VAL_INT;
353 	case IIO_CHAN_INFO_INT_TIME:
354 		*val = data->als_it_ms;
355 		*val2 = 1000;
356 
357 		return IIO_VAL_FRACTIONAL;
358 
359 	case IIO_CHAN_INFO_SCALE:
360 		switch (chan->type) {
361 		case IIO_LIGHT:
362 			/* one ALS count is 0.32 Lux @ gain 1, IT 100 ms */
363 			*val = 32000; /* 0.32 * 1000 * 100 */
364 			*val2 = data->als_gain_milli * data->als_it_ms;
365 
366 			return IIO_VAL_FRACTIONAL;
367 
368 		case IIO_DISTANCE:
369 			*val = 0; /* sensor reports mm, scale to meter */
370 			*val2 = 1000;
371 			break;
372 		default:
373 			return -EINVAL;
374 		}
375 
376 		return IIO_VAL_INT_PLUS_MICRO;
377 	case IIO_CHAN_INFO_HARDWAREGAIN:
378 		*val = data->als_gain_milli;
379 		*val2 = 1000;
380 
381 		return IIO_VAL_FRACTIONAL;
382 
383 	case IIO_CHAN_INFO_SAMP_FREQ:
384 		switch (chan->type) {
385 		case IIO_DISTANCE:
386 			*val = data->range_meas_rate;
387 			return IIO_VAL_INT;
388 		case IIO_LIGHT:
389 			*val = data->als_meas_rate;
390 			return IIO_VAL_INT;
391 		default:
392 			return -EINVAL;
393 		}
394 
395 	default:
396 		return -EINVAL;
397 	}
398 }
399 
400 static IIO_CONST_ATTR(als_gain_available, "1 1.25 1.67 2.5 5 10 20 40");
401 
402 static struct attribute *vl6180_attributes[] = {
403 	&iio_const_attr_als_gain_available.dev_attr.attr,
404 	NULL
405 };
406 
407 static const struct attribute_group vl6180_attribute_group = {
408 	.attrs = vl6180_attributes,
409 };
410 
411 /* HOLD is needed before updating any config registers */
412 static int vl6180_hold(struct vl6180_data *data, bool hold)
413 {
414 	return vl6180_write_byte(data->client, VL6180_HOLD,
415 		hold ? VL6180_HOLD_ON : 0);
416 }
417 
418 static int vl6180_set_als_gain(struct vl6180_data *data, int val, int val2)
419 {
420 	int i, ret, gain;
421 
422 	if (val < 1 || val > 40)
423 		return -EINVAL;
424 
425 	gain = (val * 1000000 + val2) / 1000;
426 	if (gain < 1 || gain > 40000)
427 		return -EINVAL;
428 
429 	i = find_closest(gain, vl6180_als_gain_tab,
430 			 ARRAY_SIZE(vl6180_als_gain_tab));
431 
432 	mutex_lock(&data->lock);
433 	ret = vl6180_hold(data, true);
434 	if (ret < 0)
435 		goto fail;
436 
437 	ret = vl6180_write_byte(data->client, VL6180_ALS_GAIN,
438 				vl6180_als_gain_tab_bits[i]);
439 
440 	if (ret >= 0)
441 		data->als_gain_milli = vl6180_als_gain_tab[i];
442 
443 fail:
444 	vl6180_hold(data, false);
445 	mutex_unlock(&data->lock);
446 	return ret;
447 }
448 
449 static int vl6180_set_it(struct vl6180_data *data, int val, int val2)
450 {
451 	int ret, it_ms;
452 
453 	it_ms = DIV_ROUND_CLOSEST(val2, 1000); /* round to ms */
454 	if (val != 0 || it_ms < 1 || it_ms > 512)
455 		return -EINVAL;
456 
457 	mutex_lock(&data->lock);
458 	ret = vl6180_hold(data, true);
459 	if (ret < 0)
460 		goto fail;
461 
462 	ret = vl6180_write_word(data->client, VL6180_ALS_IT, it_ms - 1);
463 
464 	if (ret >= 0)
465 		data->als_it_ms = it_ms;
466 
467 fail:
468 	vl6180_hold(data, false);
469 	mutex_unlock(&data->lock);
470 
471 	return ret;
472 }
473 
474 static int vl6180_meas_reg_val_from_mhz(unsigned int mhz)
475 {
476 	unsigned int period = DIV_ROUND_CLOSEST(1000 * 1000, mhz);
477 	unsigned int reg_val = 0;
478 
479 	if (period > 10)
480 		reg_val = period < 2550 ? (DIV_ROUND_CLOSEST(period, 10) - 1) : 254;
481 
482 	return reg_val;
483 }
484 
485 static int vl6180_write_raw(struct iio_dev *indio_dev,
486 			     struct iio_chan_spec const *chan,
487 			     int val, int val2, long mask)
488 {
489 	struct vl6180_data *data = iio_priv(indio_dev);
490 	unsigned int reg_val;
491 
492 	switch (mask) {
493 	case IIO_CHAN_INFO_INT_TIME:
494 		return vl6180_set_it(data, val, val2);
495 
496 	case IIO_CHAN_INFO_HARDWAREGAIN:
497 		if (chan->type != IIO_LIGHT)
498 			return -EINVAL;
499 
500 		return vl6180_set_als_gain(data, val, val2);
501 
502 	case IIO_CHAN_INFO_SAMP_FREQ:
503 	{
504 		guard(mutex)(&data->lock);
505 		switch (chan->type) {
506 		case IIO_DISTANCE:
507 			data->range_meas_rate = val;
508 			reg_val = vl6180_meas_reg_val_from_mhz(val);
509 			return vl6180_write_byte(data->client,
510 				VL6180_RANGE_INTER_MEAS_TIME, reg_val);
511 
512 		case IIO_LIGHT:
513 			data->als_meas_rate = val;
514 			reg_val = vl6180_meas_reg_val_from_mhz(val);
515 			return vl6180_write_byte(data->client,
516 				VL6180_ALS_INTER_MEAS_TIME, reg_val);
517 
518 		default:
519 			return -EINVAL;
520 		}
521 	}
522 
523 	default:
524 		return -EINVAL;
525 	}
526 }
527 
528 static irqreturn_t vl6180_threaded_irq(int irq, void *priv)
529 {
530 	struct iio_dev *indio_dev = priv;
531 	struct vl6180_data *data = iio_priv(indio_dev);
532 
533 	if (iio_buffer_enabled(indio_dev))
534 		iio_trigger_poll_nested(indio_dev->trig);
535 	else
536 		complete(&data->completion);
537 
538 	return IRQ_HANDLED;
539 }
540 
541 static irqreturn_t vl6180_trigger_handler(int irq, void *priv)
542 {
543 	struct iio_poll_func *pf = priv;
544 	struct iio_dev *indio_dev = pf->indio_dev;
545 	struct vl6180_data *data = iio_priv(indio_dev);
546 	s64 time_ns = iio_get_time_ns(indio_dev);
547 	int ret, bit, i = 0;
548 
549 	iio_for_each_active_channel(indio_dev, bit) {
550 		if (vl6180_chan_regs_table[bit].word)
551 			ret = vl6180_read_word(data->client,
552 				vl6180_chan_regs_table[bit].value_reg);
553 		else
554 			ret = vl6180_read_byte(data->client,
555 				vl6180_chan_regs_table[bit].value_reg);
556 
557 		if (ret < 0) {
558 			dev_err(&data->client->dev,
559 				"failed to read from value regs: %d\n", ret);
560 			return IRQ_HANDLED;
561 		}
562 
563 		data->scan.chan[i++] = ret;
564 	}
565 
566 	iio_push_to_buffers_with_timestamp(indio_dev, &data->scan, time_ns);
567 	iio_trigger_notify_done(indio_dev->trig);
568 
569 	/* Clear the interrupt flag after data read */
570 	ret = vl6180_write_byte(data->client, VL6180_INTR_CLEAR,
571 		VL6180_CLEAR_ERROR | VL6180_CLEAR_ALS | VL6180_CLEAR_RANGE);
572 	if (ret < 0)
573 		dev_err(&data->client->dev, "failed to clear irq: %d\n", ret);
574 
575 	return IRQ_HANDLED;
576 }
577 
578 static const struct iio_info vl6180_info = {
579 	.read_raw = vl6180_read_raw,
580 	.write_raw = vl6180_write_raw,
581 	.attrs = &vl6180_attribute_group,
582 	.validate_trigger = iio_validate_own_trigger,
583 };
584 
585 static int vl6180_buffer_postenable(struct iio_dev *indio_dev)
586 {
587 	struct vl6180_data *data = iio_priv(indio_dev);
588 	int bit;
589 
590 	iio_for_each_active_channel(indio_dev, bit)
591 		return vl6180_write_byte(data->client,
592 			vl6180_chan_regs_table[bit].start_reg,
593 			VL6180_MODE_CONT | VL6180_STARTSTOP);
594 
595 	return -EINVAL;
596 }
597 
598 static int vl6180_buffer_postdisable(struct iio_dev *indio_dev)
599 {
600 	struct vl6180_data *data = iio_priv(indio_dev);
601 	int bit;
602 
603 	iio_for_each_active_channel(indio_dev, bit)
604 		return vl6180_write_byte(data->client,
605 			vl6180_chan_regs_table[bit].start_reg,
606 			VL6180_STARTSTOP);
607 
608 	return -EINVAL;
609 }
610 
611 static const struct iio_buffer_setup_ops iio_triggered_buffer_setup_ops = {
612 	.postenable = &vl6180_buffer_postenable,
613 	.postdisable = &vl6180_buffer_postdisable,
614 };
615 
616 static const struct iio_trigger_ops vl6180_trigger_ops = {
617 	.validate_device = iio_trigger_validate_own_device,
618 };
619 
620 static int vl6180_init(struct vl6180_data *data, struct iio_dev *indio_dev)
621 {
622 	struct i2c_client *client = data->client;
623 	int ret;
624 
625 	ret = vl6180_read_byte(client, VL6180_MODEL_ID);
626 	if (ret < 0)
627 		return ret;
628 
629 	if (ret != VL6180_MODEL_ID_VAL) {
630 		dev_err(&client->dev, "invalid model ID %02x\n", ret);
631 		return -ENODEV;
632 	}
633 
634 	ret = vl6180_hold(data, true);
635 	if (ret < 0)
636 		return ret;
637 
638 	ret = vl6180_read_byte(client, VL6180_OUT_OF_RESET);
639 	if (ret < 0)
640 		return ret;
641 
642 	/*
643 	 * Detect false reset condition here. This bit is always set when the
644 	 * system comes out of reset.
645 	 */
646 	if (ret != 0x01)
647 		dev_info(&client->dev, "device is not fresh out of reset\n");
648 
649 	/* Enable ALS and Range ready interrupts */
650 	ret = vl6180_write_byte(client, VL6180_INTR_CONFIG,
651 				VL6180_ALS_READY | VL6180_RANGE_READY);
652 	if (ret < 0)
653 		return ret;
654 
655 	ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev, NULL,
656 						&vl6180_trigger_handler,
657 						&iio_triggered_buffer_setup_ops);
658 	if (ret)
659 		return ret;
660 
661 	/* Default Range inter-measurement time: 50ms or 20000 mHz */
662 	ret = vl6180_write_byte(client, VL6180_RANGE_INTER_MEAS_TIME,
663 				vl6180_meas_reg_val_from_mhz(20000));
664 	if (ret < 0)
665 		return ret;
666 	data->range_meas_rate = 20000;
667 
668 	/* Default ALS inter-measurement time: 10ms or 100000 mHz */
669 	ret = vl6180_write_byte(client, VL6180_ALS_INTER_MEAS_TIME,
670 				vl6180_meas_reg_val_from_mhz(100000));
671 	if (ret < 0)
672 		return ret;
673 	data->als_meas_rate = 100000;
674 
675 	/* ALS integration time: 100ms */
676 	data->als_it_ms = 100;
677 	ret = vl6180_write_word(client, VL6180_ALS_IT, VL6180_ALS_IT_100);
678 	if (ret < 0)
679 		return ret;
680 
681 	/* ALS gain: 1 */
682 	data->als_gain_milli = 1000;
683 	ret = vl6180_write_byte(client, VL6180_ALS_GAIN, VL6180_ALS_GAIN_1);
684 	if (ret < 0)
685 		return ret;
686 
687 	ret = vl6180_write_byte(client, VL6180_OUT_OF_RESET, 0x00);
688 	if (ret < 0)
689 		return ret;
690 
691 	return vl6180_hold(data, false);
692 }
693 
694 static int vl6180_probe(struct i2c_client *client)
695 {
696 	struct vl6180_data *data;
697 	struct iio_dev *indio_dev;
698 	int ret;
699 
700 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
701 	if (!indio_dev)
702 		return -ENOMEM;
703 
704 	data = iio_priv(indio_dev);
705 	i2c_set_clientdata(client, indio_dev);
706 	data->client = client;
707 	mutex_init(&data->lock);
708 
709 	indio_dev->info = &vl6180_info;
710 	indio_dev->channels = vl6180_channels;
711 	indio_dev->num_channels = ARRAY_SIZE(vl6180_channels);
712 	indio_dev->name = VL6180_DRV_NAME;
713 	indio_dev->modes = INDIO_DIRECT_MODE;
714 
715 	ret = vl6180_init(data, indio_dev);
716 	if (ret < 0)
717 		return ret;
718 
719 	if (client->irq) {
720 		ret = devm_request_threaded_irq(&client->dev, client->irq,
721 						NULL, vl6180_threaded_irq,
722 						IRQF_ONESHOT,
723 						indio_dev->name, indio_dev);
724 		if (ret)
725 			return dev_err_probe(&client->dev, ret, "devm_request_irq error \n");
726 
727 		init_completion(&data->completion);
728 
729 		data->trig = devm_iio_trigger_alloc(&client->dev, "%s-dev%d",
730 						indio_dev->name, iio_device_id(indio_dev));
731 		if (!data->trig)
732 			return -ENOMEM;
733 
734 		data->trig->ops = &vl6180_trigger_ops;
735 		iio_trigger_set_drvdata(data->trig, indio_dev);
736 		ret = devm_iio_trigger_register(&client->dev, data->trig);
737 		if (ret)
738 			return ret;
739 
740 		indio_dev->trig = iio_trigger_get(data->trig);
741 	}
742 
743 	return devm_iio_device_register(&client->dev, indio_dev);
744 }
745 
746 static const struct of_device_id vl6180_of_match[] = {
747 	{ .compatible = "st,vl6180", },
748 	{ },
749 };
750 MODULE_DEVICE_TABLE(of, vl6180_of_match);
751 
752 static const struct i2c_device_id vl6180_id[] = {
753 	{ "vl6180" },
754 	{ }
755 };
756 MODULE_DEVICE_TABLE(i2c, vl6180_id);
757 
758 static struct i2c_driver vl6180_driver = {
759 	.driver = {
760 		.name   = VL6180_DRV_NAME,
761 		.of_match_table = vl6180_of_match,
762 	},
763 	.probe = vl6180_probe,
764 	.id_table = vl6180_id,
765 };
766 
767 module_i2c_driver(vl6180_driver);
768 
769 MODULE_AUTHOR("Peter Meerwald-Stadler <pmeerw@pmeerw.net>");
770 MODULE_AUTHOR("Manivannan Sadhasivam <manivannanece23@gmail.com>");
771 MODULE_DESCRIPTION("STMicro VL6180 ALS, range and proximity sensor driver");
772 MODULE_LICENSE("GPL");
773