xref: /OK3568_Linux_fs/kernel/drivers/iio/light/vl6180.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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: threshold events, continuous mode
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/of.h>
24 #include <linux/delay.h>
25 #include <linux/util_macros.h>
26 
27 #include <linux/iio/iio.h>
28 #include <linux/iio/sysfs.h>
29 #include <linux/gpio.h>
30 #include <linux/of_gpio.h>
31 #include <linux/interrupt.h>
32 #include <linux/iio/triggered_buffer.h>
33 #include <linux/iio/kfifo_buf.h>
34 #include <linux/iio/buffer.h>
35 
36 #define VL6180_DRV_NAME "vl6180"
37 
38 /* Device identification register and value */
39 #define VL6180_MODEL_ID	0x000
40 #define VL6180_MODEL_ID_VAL 0xb4
41 
42 /* Configuration registers */
43 #define VL6180_SYS_MODE_GPIO1 0x011
44 #define VL6180_INTR_CONFIG 0x014
45 #define VL6180_INTR_CLEAR 0x015
46 #define VL6180_OUT_OF_RESET 0x016
47 #define VL6180_HOLD 0x017
48 #define VL6180_RANGE_START 0x018
49 #define VL6180_RANGE_INTER_MES_PERIOD 0x01b
50 #define VL6180_ALS_START 0x038
51 #define VL6180_ALS_THRESH_HIGH 0x03a
52 #define VL6180_ALS_THRESH_LOW 0x03c
53 #define VL6180_ALS_INTER_MES_PERIOD 0x03e
54 #define VL6180_ALS_GAIN 0x03f
55 #define VL6180_ALS_IT 0x040
56 
57 /* Status registers */
58 #define VL6180_RANGE_STATUS 0x04d
59 #define VL6180_ALS_STATUS 0x04e
60 #define VL6180_INTR_STATUS 0x04f
61 
62 /* Result value registers */
63 #define VL6180_ALS_VALUE 0x050
64 #define VL6180_RANGE_VALUE 0x062
65 #define VL6180_RANGE_RATE 0x066
66 
67 #define VL6180_RANGE_THRESH_HIGH 0x019
68 #define VL6180_RANGE_THRESH_LOW 0x01a
69 #define VL6180_RANGE_MAX_CONVERGENCE_TIME 0x01c
70 #define VL6180_RANGE_CROSSTALK_COMPENSATION_RATE 0x01e
71 #define VL6180_RANGE_PART_TO_PART_RANGE_OFFSET 0x024
72 #define VL6180_RANGE_RANGE_IGNORE_VALID_HEIGHT 0x025
73 #define VL6180_RANGE_RANGE_IGNORE_THRESHOLD 0x026
74 #define VL6180_RANGE_MAX_AMBIENT_LEVEL_MULT 0x02c
75 #define VL6180_RANGE_RANGE_CHECK_ENABLES 0x02d
76 #define VL6180_RANGE_VHV_RECALIBRATE 0x02e
77 #define VL6180_RANGE_VHV_REPEAT_RATE 0x031
78 #define VL6180_READOUT_AVERAGING_SAMPLE_PERIOD 0x10a
79 
80 /* bits of the SYS_MODE_GPIO1 register */
81 #define VL6180_SYS_GPIO1_POLARITY BIT(5) /* active high */
82 #define VL6180_SYS_GPIO1_SELECT BIT(4) /* configure GPIO interrupt output */
83 
84 /* bits of the RANGE_START and ALS_START register */
85 #define VL6180_MODE_CONT BIT(1) /* continuous mode */
86 #define VL6180_STARTSTOP BIT(0) /* start measurement, auto-reset */
87 
88 /* bits of the INTR_STATUS and INTR_CONFIG register */
89 #define VL6180_ALS_LEVEL_LOW BIT(3)
90 #define VL6180_ALS_LEVEL_HIGH BIT(4)
91 #define VL6180_ALS_OUT_OF_WINDOW (BIT(3) | BIT(4))
92 #define VL6180_ALS_READY BIT(5)
93 #define VL6180_RANGE_LEVEL_LOW BIT(0)
94 #define VL6180_RANGE_LEVEL_HIGH BIT(1)
95 #define VL6180_RANGE_OUT_OF_WINDOW (BIT(0) | BIT(1))
96 #define VL6180_RANGE_READY BIT(2)
97 #define VL6180_INT_RANGE_GPIO_MASK GENMASK(2, 0)
98 #define VL6180_INT_ALS_GPIO_MASK GENMASK(5, 3)
99 #define VL6180_INT_ERR_GPIO_MASK GENMASK(7, 6)
100 
101 /* bits of the INTR_CLEAR register */
102 #define VL6180_CLEAR_ERROR BIT(2)
103 #define VL6180_CLEAR_ALS BIT(1)
104 #define VL6180_CLEAR_RANGE BIT(0)
105 
106 /* bits of the HOLD register */
107 #define VL6180_HOLD_ON BIT(0)
108 
109 /* default value for the ALS_IT register */
110 #define VL6180_ALS_IT_100 0x63 /* 100 ms */
111 
112 /* values for the ALS_GAIN register */
113 #define VL6180_ALS_GAIN_1 0x46
114 #define VL6180_ALS_GAIN_1_25 0x45
115 #define VL6180_ALS_GAIN_1_67 0x44
116 #define VL6180_ALS_GAIN_2_5 0x43
117 #define VL6180_ALS_GAIN_5 0x42
118 #define VL6180_ALS_GAIN_10 0x41
119 #define VL6180_ALS_GAIN_20 0x40
120 #define VL6180_ALS_GAIN_40 0x47
121 
122 struct vl6180_data {
123 	struct i2c_client *client;
124 	struct mutex lock;
125 	unsigned int als_gain_milli;
126 	unsigned int als_it_ms;
127 	struct gpio_desc *avdd;
128 	struct gpio_desc *chip_enable;
129 
130 	/* Ensure natural alignment of timestamp */
131 	struct {
132 		u16 channels[3];
133 		u16 reserved;
134 		s64 ts;
135 	} scan;
136 };
137 
138 enum { VL6180_ALS, VL6180_RANGE, VL6180_PROX };
139 
140 /**
141  * struct vl6180_chan_regs - Registers for accessing channels
142  * @drdy_mask:			Data ready bit in status register
143  * @start_reg:			Conversion start register
144  * @value_reg:			Result value register
145  * @word:			Register word length
146  */
147 struct vl6180_chan_regs {
148 	u8 drdy_mask;
149 	u16 start_reg, value_reg;
150 	bool word;
151 };
152 
153 static const struct vl6180_chan_regs vl6180_chan_regs_table[] = {
154 	[VL6180_ALS] = {
155 		.drdy_mask = VL6180_ALS_READY,
156 		.start_reg = VL6180_ALS_START,
157 		.value_reg = VL6180_ALS_VALUE,
158 		.word = true,
159 	},
160 	[VL6180_RANGE] = {
161 		.drdy_mask = VL6180_RANGE_READY,
162 		.start_reg = VL6180_RANGE_START,
163 		.value_reg = VL6180_RANGE_VALUE,
164 		.word = false,
165 	},
166 	[VL6180_PROX] = {
167 		.drdy_mask = VL6180_RANGE_READY,
168 		.start_reg = VL6180_RANGE_START,
169 		.value_reg = VL6180_RANGE_RATE,
170 		.word = true,
171 	},
172 };
173 
174 /**
175  * struct vl6180_custom_data - Data for custom initialization
176  * @reg:			Register
177  * @val:			Value
178  */
179 struct vl6180_custom_data {
180 	u16 reg;
181 	u8 val;
182 };
183 
184 static const struct vl6180_custom_data vl6180_custom_data_table[] = {
185 	{ .reg = 0x207, .val = 0x01, },
186 	{ .reg = 0x208, .val = 0x01, },
187 	{ .reg = 0x096, .val = 0x00, },
188 	{ .reg = 0x097, .val = 0xfd, },
189 	{ .reg = 0x0e3, .val = 0x00, },
190 	{ .reg = 0x0e4, .val = 0x04, },
191 	{ .reg = 0x0e5, .val = 0x02, },
192 	{ .reg = 0x0e6, .val = 0x01, },
193 	{ .reg = 0x0e7, .val = 0x03, },
194 	{ .reg = 0x0f5, .val = 0x02, },
195 	{ .reg = 0x0d9, .val = 0x05, },
196 	{ .reg = 0x0db, .val = 0xce, },
197 	{ .reg = 0x0dc, .val = 0x03, },
198 	{ .reg = 0x0dd, .val = 0xf8, },
199 	{ .reg = 0x09f, .val = 0x00, },
200 	{ .reg = 0x0a3, .val = 0x3c, },
201 	{ .reg = 0x0b7, .val = 0x00, },
202 	{ .reg = 0x0bb, .val = 0x3c, },
203 	{ .reg = 0x0b2, .val = 0x09, },
204 	{ .reg = 0x0ca, .val = 0x09, },
205 	{ .reg = 0x198, .val = 0x01, },
206 	{ .reg = 0x1b0, .val = 0x17, },
207 	{ .reg = 0x1ad, .val = 0x00, },
208 	{ .reg = 0x0ff, .val = 0x05, },
209 	{ .reg = 0x100, .val = 0x05, },
210 	{ .reg = 0x199, .val = 0x05, },
211 	{ .reg = 0x1a6, .val = 0x1b, },
212 	{ .reg = 0x1ac, .val = 0x3e, },
213 	{ .reg = 0x1a7, .val = 0x1f, },
214 	{ .reg = 0x030, .val = 0x00, },
215 };
216 
vl6180_read(struct i2c_client * client,u16 cmd,void * databuf,u8 len)217 static int vl6180_read(struct i2c_client *client, u16 cmd, void *databuf,
218 		       u8 len)
219 {
220 	__be16 cmdbuf = cpu_to_be16(cmd);
221 	struct i2c_msg msgs[2] = {
222 		{ .addr = client->addr, .len = sizeof(cmdbuf), .buf = (u8 *) &cmdbuf },
223 		{ .addr = client->addr, .len = len, .buf = databuf,
224 		  .flags = I2C_M_RD } };
225 	int ret;
226 
227 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
228 	if (ret < 0)
229 		dev_err(&client->dev, "failed reading register 0x%04x\n", cmd);
230 
231 	return ret;
232 }
233 
vl6180_read_byte(struct i2c_client * client,u16 cmd)234 static int vl6180_read_byte(struct i2c_client *client, u16 cmd)
235 {
236 	u8 data;
237 	int ret;
238 
239 	ret = vl6180_read(client, cmd, &data, sizeof(data));
240 	if (ret < 0)
241 		return ret;
242 
243 	return data;
244 }
245 
vl6180_read_word(struct i2c_client * client,u16 cmd)246 static int vl6180_read_word(struct i2c_client *client, u16 cmd)
247 {
248 	__be16 data;
249 	int ret;
250 
251 	ret = vl6180_read(client, cmd, &data, sizeof(data));
252 	if (ret < 0)
253 		return ret;
254 
255 	return be16_to_cpu(data);
256 }
257 
vl6180_write_byte(struct i2c_client * client,u16 cmd,u8 val)258 static int vl6180_write_byte(struct i2c_client *client, u16 cmd, u8 val)
259 {
260 	u8 buf[3];
261 	struct i2c_msg msgs[1] = {
262 		{ .addr = client->addr, .len = sizeof(buf), .buf = (u8 *) &buf } };
263 	int ret;
264 
265 	buf[0] = cmd >> 8;
266 	buf[1] = cmd & 0xff;
267 	buf[2] = val;
268 
269 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
270 	if (ret < 0) {
271 		dev_err(&client->dev, "failed writing register 0x%04x\n", cmd);
272 		return ret;
273 	}
274 
275 	return 0;
276 }
277 
vl6180_write_word(struct i2c_client * client,u16 cmd,u16 val)278 static int vl6180_write_word(struct i2c_client *client, u16 cmd, u16 val)
279 {
280 	__be16 buf[2];
281 	struct i2c_msg msgs[1] = {
282 		{ .addr = client->addr, .len = sizeof(buf), .buf = (u8 *) &buf } };
283 	int ret;
284 
285 	buf[0] = cpu_to_be16(cmd);
286 	buf[1] = cpu_to_be16(val);
287 
288 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
289 	if (ret < 0) {
290 		dev_err(&client->dev, "failed writing register 0x%04x\n", cmd);
291 		return ret;
292 	}
293 
294 	return 0;
295 }
296 
vl6180_measure(struct vl6180_data * data,int addr)297 static int vl6180_measure(struct vl6180_data *data, int addr)
298 {
299 	struct i2c_client *client = data->client;
300 	int tries = 20, ret;
301 	u16 value;
302 
303 	mutex_lock(&data->lock);
304 	/* Start single shot measurement */
305 	ret = vl6180_write_byte(client,
306 		vl6180_chan_regs_table[addr].start_reg, VL6180_STARTSTOP);
307 	if (ret < 0)
308 		goto fail;
309 
310 	while (tries--) {
311 		ret = vl6180_read_byte(client, VL6180_INTR_STATUS);
312 		if (ret < 0)
313 			goto fail;
314 
315 		if (ret & vl6180_chan_regs_table[addr].drdy_mask)
316 			break;
317 		msleep(20);
318 	}
319 
320 	if (tries < 0) {
321 		ret = -EIO;
322 		goto fail;
323 	}
324 
325 	/* Read result value from appropriate registers */
326 	ret = vl6180_chan_regs_table[addr].word ?
327 		vl6180_read_word(client, vl6180_chan_regs_table[addr].value_reg) :
328 		vl6180_read_byte(client, vl6180_chan_regs_table[addr].value_reg);
329 	if (ret < 0)
330 		goto fail;
331 	value = ret;
332 
333 	/* Clear the interrupt flag after data read */
334 	ret = vl6180_write_byte(client, VL6180_INTR_CLEAR,
335 		VL6180_CLEAR_ERROR | VL6180_CLEAR_ALS | VL6180_CLEAR_RANGE);
336 	if (ret < 0)
337 		goto fail;
338 
339 	ret = value;
340 
341 fail:
342 	mutex_unlock(&data->lock);
343 
344 	return ret;
345 }
346 
347 static const struct iio_chan_spec vl6180_channels[] = {
348 	{
349 		.type = IIO_LIGHT,
350 		.address = VL6180_ALS,
351 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
352 			BIT(IIO_CHAN_INFO_INT_TIME) |
353 			BIT(IIO_CHAN_INFO_SCALE) |
354 			BIT(IIO_CHAN_INFO_HARDWAREGAIN),
355 		.scan_index = 0,
356 		.scan_type = {
357 			.sign = 'u',
358 			.realbits = 16,
359 			.storagebits = 16,
360 		}
361 	}, {
362 		.type = IIO_DISTANCE,
363 		.address = VL6180_RANGE,
364 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
365 			BIT(IIO_CHAN_INFO_SCALE),
366 		.scan_index = 1,
367 		.scan_type = {
368 			.sign = 'u',
369 			.realbits = 16,
370 			.storagebits = 16,
371 		}
372 	}, {
373 		.type = IIO_PROXIMITY,
374 		.address = VL6180_PROX,
375 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
376 		.scan_index = 2,
377 		.scan_type = {
378 			.sign = 'u',
379 			.realbits = 16,
380 			.storagebits = 16,
381 		}
382 	},
383 	IIO_CHAN_SOFT_TIMESTAMP(3),
384 };
385 
386 /*
387  * Available Ambient Light Sensor gain settings, 1/1000th, and
388  * corresponding setting for the VL6180_ALS_GAIN register
389  */
390 static const int vl6180_als_gain_tab[8] = {
391 	1000, 1250, 1670, 2500, 5000, 10000, 20000, 40000
392 };
393 static const u8 vl6180_als_gain_tab_bits[8] = {
394 	VL6180_ALS_GAIN_1,    VL6180_ALS_GAIN_1_25,
395 	VL6180_ALS_GAIN_1_67, VL6180_ALS_GAIN_2_5,
396 	VL6180_ALS_GAIN_5,    VL6180_ALS_GAIN_10,
397 	VL6180_ALS_GAIN_20,   VL6180_ALS_GAIN_40
398 };
399 
vl6180_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)400 static int vl6180_read_raw(struct iio_dev *indio_dev,
401 				struct iio_chan_spec const *chan,
402 				int *val, int *val2, long mask)
403 {
404 	struct vl6180_data *data = iio_priv(indio_dev);
405 	int ret;
406 
407 	switch (mask) {
408 	case IIO_CHAN_INFO_RAW:
409 		ret = vl6180_measure(data, chan->address);
410 		if (ret < 0)
411 			return ret;
412 		*val = ret;
413 
414 		return IIO_VAL_INT;
415 	case IIO_CHAN_INFO_INT_TIME:
416 		*val = data->als_it_ms;
417 		*val2 = 1000;
418 
419 		return IIO_VAL_FRACTIONAL;
420 
421 	case IIO_CHAN_INFO_SCALE:
422 		switch (chan->type) {
423 		case IIO_LIGHT:
424 			/* one ALS count is 0.32 Lux @ gain 1, IT 100 ms */
425 			*val = 32000; /* 0.32 * 1000 * 100 */
426 			*val2 = data->als_gain_milli * data->als_it_ms;
427 
428 			return IIO_VAL_FRACTIONAL;
429 
430 		case IIO_DISTANCE:
431 			*val = 0; /* sensor reports mm, scale to meter */
432 			*val2 = 1000;
433 			break;
434 		default:
435 			return -EINVAL;
436 		}
437 
438 		return IIO_VAL_INT_PLUS_MICRO;
439 	case IIO_CHAN_INFO_HARDWAREGAIN:
440 		*val = data->als_gain_milli;
441 		*val2 = 1000;
442 
443 		return IIO_VAL_FRACTIONAL;
444 
445 	default:
446 		return -EINVAL;
447 	}
448 }
449 
450 static IIO_CONST_ATTR(als_gain_available, "1 1.25 1.67 2.5 5 10 20 40");
451 
452 static struct attribute *vl6180_attributes[] = {
453 	&iio_const_attr_als_gain_available.dev_attr.attr,
454 	NULL
455 };
456 
457 static const struct attribute_group vl6180_attribute_group = {
458 	.attrs = vl6180_attributes,
459 };
460 
461 /* HOLD is needed before updating any config registers */
vl6180_hold(struct vl6180_data * data,bool hold)462 static int vl6180_hold(struct vl6180_data *data, bool hold)
463 {
464 	return vl6180_write_byte(data->client, VL6180_HOLD,
465 		hold ? VL6180_HOLD_ON : 0);
466 }
467 
vl6180_set_als_gain(struct vl6180_data * data,int val,int val2)468 static int vl6180_set_als_gain(struct vl6180_data *data, int val, int val2)
469 {
470 	int i, ret, gain;
471 
472 	if (val < 1 || val > 40)
473 		return -EINVAL;
474 
475 	gain = (val * 1000000 + val2) / 1000;
476 	if (gain < 1 || gain > 40000)
477 		return -EINVAL;
478 
479 	i = find_closest(gain, vl6180_als_gain_tab,
480 			 ARRAY_SIZE(vl6180_als_gain_tab));
481 
482 	mutex_lock(&data->lock);
483 	ret = vl6180_hold(data, true);
484 	if (ret < 0)
485 		goto fail;
486 
487 	ret = vl6180_write_byte(data->client, VL6180_ALS_GAIN,
488 				vl6180_als_gain_tab_bits[i]);
489 
490 	if (ret >= 0)
491 		data->als_gain_milli = vl6180_als_gain_tab[i];
492 
493 fail:
494 	vl6180_hold(data, false);
495 	mutex_unlock(&data->lock);
496 	return ret;
497 }
498 
vl6180_set_it(struct vl6180_data * data,int val,int val2)499 static int vl6180_set_it(struct vl6180_data *data, int val, int val2)
500 {
501 	int ret, it_ms;
502 
503 	it_ms = (val2 + 500) / 1000; /* round to ms */
504 	if (val != 0 || it_ms < 1 || it_ms > 512)
505 		return -EINVAL;
506 
507 	mutex_lock(&data->lock);
508 	ret = vl6180_hold(data, true);
509 	if (ret < 0)
510 		goto fail;
511 
512 	ret = vl6180_write_word(data->client, VL6180_ALS_IT, it_ms - 1);
513 
514 	if (ret >= 0)
515 		data->als_it_ms = it_ms;
516 
517 fail:
518 	vl6180_hold(data, false);
519 	mutex_unlock(&data->lock);
520 
521 	return ret;
522 }
523 
vl6180_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)524 static int vl6180_write_raw(struct iio_dev *indio_dev,
525 			     struct iio_chan_spec const *chan,
526 			     int val, int val2, long mask)
527 {
528 	struct vl6180_data *data = iio_priv(indio_dev);
529 
530 	switch (mask) {
531 	case IIO_CHAN_INFO_INT_TIME:
532 		return vl6180_set_it(data, val, val2);
533 
534 	case IIO_CHAN_INFO_HARDWAREGAIN:
535 		if (chan->type != IIO_LIGHT)
536 			return -EINVAL;
537 
538 		return vl6180_set_als_gain(data, val, val2);
539 	default:
540 		return -EINVAL;
541 	}
542 }
543 
544 static const struct iio_info vl6180_info = {
545 	.read_raw = vl6180_read_raw,
546 	.write_raw = vl6180_write_raw,
547 	.attrs = &vl6180_attribute_group,
548 };
549 
vl6180_power_enable(struct vl6180_data * data)550 static int vl6180_power_enable(struct vl6180_data *data)
551 {
552 	/* Enable power supply. */
553 	if (!IS_ERR_OR_NULL(data->avdd))
554 		gpiod_set_value_cansleep(data->avdd, 1);
555 
556 	/* Power-up default is chip enable (CE). */
557 	if (!IS_ERR_OR_NULL(data->chip_enable)) {
558 		gpiod_set_value_cansleep(data->chip_enable, 0);
559 		usleep_range(500, 1000);
560 		gpiod_set_value_cansleep(data->chip_enable, 1);
561 	}
562 
563 	return 0;
564 }
565 
vl6180_custom_init(struct vl6180_data * data)566 static int vl6180_custom_init(struct vl6180_data *data)
567 {
568 	struct i2c_client *client = data->client;
569 	int ret;
570 	int i;
571 
572 	/* REGISTER_TUNING_SR03_270514_CustomerView.txt */
573 	for (i = 0; i < ARRAY_SIZE(vl6180_custom_data_table); ++i) {
574 		ret = vl6180_write_byte(client,
575 					vl6180_custom_data_table[i].reg,
576 					vl6180_custom_data_table[i].val);
577 
578 		if (ret < 0)
579 			break;
580 	}
581 
582 	return ret;
583 }
584 
vl6180_range_init(struct vl6180_data * data)585 static int vl6180_range_init(struct vl6180_data *data)
586 {
587 	struct i2c_client *client = data->client;
588 	int ret;
589 	u8 enables;
590 	u8 offset;
591 	u8 xtalk = 3;
592 
593 	/* Enables polling for ‘New Sample ready’ when measurement completes */
594 	ret = vl6180_write_byte(client, VL6180_SYS_MODE_GPIO1,
595 				(VL6180_SYS_GPIO1_POLARITY |
596 				 VL6180_SYS_GPIO1_SELECT));
597 	if (ret < 0)
598 		goto out;
599 
600 	/* Set the averaging sample period (compromise between lower noise and
601 	 * increased execution time), 0x30 equals to 4.3 ms.
602 	 */
603 	ret = vl6180_write_byte(client, VL6180_READOUT_AVERAGING_SAMPLE_PERIOD,
604 				0x30);
605 	if (ret < 0)
606 		goto out;
607 
608 	/* Sets the # of range measurements after which auto calibration of
609 	 * system is performed
610 	 */
611 	ret = vl6180_write_byte(client, VL6180_RANGE_VHV_REPEAT_RATE, 0xff);
612 	if (ret < 0)
613 		goto out;
614 
615 	/* Perform a single temperature calibration of the ranging sensor */
616 	ret = vl6180_write_byte(client, VL6180_RANGE_VHV_RECALIBRATE, 0x01);
617 	if (ret < 0)
618 		goto out;
619 
620 	/* Set SNR limit to 0.06 */
621 	ret = vl6180_write_byte(client, VL6180_RANGE_MAX_AMBIENT_LEVEL_MULT,
622 				0xff);
623 	if (ret < 0)
624 		goto out;
625 
626 	/* Set default ranging inter-measurement period to 100ms */
627 	ret = vl6180_write_byte(client, VL6180_RANGE_INTER_MES_PERIOD, 0x09);
628 	if (ret < 0)
629 		goto out;
630 
631 	/* Copy registers */
632 	/* NOTE: 0x0da, 0x027, 0x0db, 0x028, 0x0dc, 0x029 and 0x0dd are
633 	 * unavailable on the datasheet.
634 	 */
635 	ret = vl6180_read_byte(client, VL6180_RANGE_RANGE_IGNORE_THRESHOLD);
636 	if (ret < 0)
637 		goto out;
638 
639 	ret = vl6180_write_byte(client, 0x0da, ret);
640 	if (ret < 0)
641 		goto out;
642 
643 	ret = vl6180_read_byte(client, 0x027);
644 	if (ret < 0)
645 		goto out;
646 
647 	ret = vl6180_write_byte(client, 0x0db, ret);
648 	if (ret < 0)
649 		goto out;
650 
651 	ret = vl6180_read_byte(client, 0x028);
652 	if (ret < 0)
653 		goto out;
654 
655 	ret = vl6180_write_byte(client, 0x0dc, ret);
656 	if (ret < 0)
657 		goto out;
658 
659 	ret = vl6180_read_byte(client, 0x029);
660 	if (ret < 0)
661 		goto out;
662 
663 	ret = vl6180_write_byte(client, 0x0dd, ret);
664 	if (ret < 0)
665 		goto out;
666 
667 	ret = vl6180_write_byte(client, VL6180_RANGE_MAX_CONVERGENCE_TIME, 0x32);
668 	if (ret < 0)
669 		goto out;
670 
671 	ret = vl6180_read_byte(client, VL6180_RANGE_RANGE_CHECK_ENABLES);
672 	if (ret < 0)
673 		goto out;
674 
675 	/* Disable early convergence */
676 	enables = ret & 0xfe;
677 	ret = vl6180_write_byte(client, VL6180_RANGE_RANGE_CHECK_ENABLES, enables);
678 	if (ret < 0)
679 		goto out;
680 
681 	ret = vl6180_write_byte(client, VL6180_RANGE_THRESH_HIGH, 0xc8);
682 	if (ret < 0)
683 		goto out;
684 
685 	ret = vl6180_write_byte(client, VL6180_RANGE_THRESH_LOW, 0x00);
686 	if (ret < 0)
687 		goto out;
688 
689 	ret = vl6180_write_byte(client, VL6180_ALS_IT, VL6180_ALS_IT_100);
690 	if (ret < 0)
691 		goto out;
692 
693 	ret = vl6180_write_byte(client, VL6180_ALS_INTER_MES_PERIOD, 0x13);
694 	if (ret < 0)
695 		goto out;
696 
697 	ret = vl6180_write_byte(client, VL6180_ALS_GAIN, VL6180_ALS_GAIN_1);
698 	if (ret < 0)
699 		goto out;
700 
701 	ret = vl6180_write_byte(client, VL6180_ALS_THRESH_LOW, 0x00);
702 	if (ret < 0)
703 		goto out;
704 
705 	ret = vl6180_write_byte(client, VL6180_ALS_THRESH_HIGH, 0xff);
706 	if (ret < 0)
707 		goto out;
708 
709 	/* Cover glass ignore */
710 	ret = vl6180_write_byte(client,
711 				VL6180_RANGE_RANGE_IGNORE_VALID_HEIGHT, 0xff);
712 	if (ret < 0)
713 		goto out;
714 
715 	ret = vl6180_read_byte(client, VL6180_RANGE_PART_TO_PART_RANGE_OFFSET);
716 	if (ret < 0)
717 		goto out;
718 
719 	/* Apply default calibration on part to part offset */
720 	offset = ret / 4;
721 	ret = vl6180_write_byte(client, VL6180_RANGE_PART_TO_PART_RANGE_OFFSET,
722 				offset);
723 	if (ret < 0)
724 		goto out;
725 
726 	ret = vl6180_write_byte(client,
727 				VL6180_RANGE_CROSSTALK_COMPENSATION_RATE,
728 				0x00);
729 	if (ret < 0)
730 		goto out;
731 
732 	ret = vl6180_write_byte(client, 0x01f, xtalk);
733 
734 out:
735 	return ret;
736 }
737 
vl6180_init(struct vl6180_data * data)738 static int vl6180_init(struct vl6180_data *data)
739 {
740 	struct i2c_client *client = data->client;
741 	int ret;
742 
743 	ret = vl6180_power_enable(data);
744 	if (ret) {
745 		dev_err(&client->dev, "failed to configure power\n");
746 		return ret;
747 	}
748 
749 	/*
750 	 * After the MCU boot sequence the device enters software standby,
751 	 * host initialization can commence immediately after entering
752 	 * software standby.
753 	 */
754 	usleep_range(500, 1000);
755 
756 	ret = vl6180_read_byte(client, VL6180_MODEL_ID);
757 	if (ret < 0)
758 		return ret;
759 
760 	if (ret != VL6180_MODEL_ID_VAL) {
761 		dev_err(&client->dev, "invalid model ID %02x\n", ret);
762 		return -ENODEV;
763 	}
764 
765 	ret = vl6180_hold(data, true);
766 	if (ret < 0)
767 		return ret;
768 
769 	ret = vl6180_read_byte(client, VL6180_OUT_OF_RESET);
770 	if (ret < 0)
771 		return ret;
772 
773 	/*
774 	 * Detect false reset condition here. This bit is always set when the
775 	 * system comes out of reset.
776 	 */
777 	if (ret != 0x01)
778 		dev_info(&client->dev, "device is not fresh out of reset\n");
779 
780 	/* ALS integration time: 100ms */
781 	data->als_it_ms = 100;
782 	ret = vl6180_write_word(client, VL6180_ALS_IT, VL6180_ALS_IT_100);
783 	if (ret < 0)
784 		return ret;
785 
786 	/* ALS gain: 1 */
787 	data->als_gain_milli = 1000;
788 	ret = vl6180_write_byte(client, VL6180_ALS_GAIN, VL6180_ALS_GAIN_1);
789 	if (ret < 0)
790 		return ret;
791 
792 	ret = vl6180_custom_init(data);
793 	if (ret < 0)
794 		return ret;
795 
796 	ret = vl6180_range_init(data);
797 	if (ret < 0)
798 		return ret;
799 
800 	ret = vl6180_write_byte(client, VL6180_RANGE_START,
801 				(VL6180_STARTSTOP | VL6180_MODE_CONT));
802 	if (ret < 0)
803 		return ret;
804 
805 	ret = vl6180_write_byte(client, VL6180_OUT_OF_RESET, 0x00);
806 	if (ret < 0)
807 		return ret;
808 
809 	return vl6180_hold(data, false);
810 }
811 
vl6180_irq_thread(int irq,void * priv)812 static irqreturn_t vl6180_irq_thread(int irq, void *priv)
813 {
814 	struct vl6180_data *data = priv;
815 	struct i2c_client *client = data->client;
816 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
817 	int ret;
818 	u8 val = 0;
819 
820 	ret = vl6180_read_byte(client, VL6180_INTR_STATUS);
821 	if (ret < 0)
822 		goto out;
823 
824 	if (ret & VL6180_INT_ALS_GPIO_MASK)
825 		val |= VL6180_CLEAR_ALS;
826 
827 	if (ret & VL6180_INT_RANGE_GPIO_MASK)
828 		val |= VL6180_CLEAR_RANGE;
829 
830 	if (ret & VL6180_INT_ERR_GPIO_MASK)
831 		val |= VL6180_CLEAR_ERROR;
832 
833 	vl6180_write_byte(client, VL6180_INTR_CLEAR, val);
834 
835 	ret = vl6180_read_word(client, VL6180_ALS_VALUE);
836 	if (ret < 0)
837 		goto out;
838 	data->scan.channels[VL6180_ALS] = ret;
839 
840 	ret = vl6180_read_byte(client, VL6180_RANGE_VALUE);
841 	if (ret < 0)
842 		goto out;
843 	data->scan.channels[VL6180_RANGE] = ret;
844 
845 	ret = vl6180_read_word(client, VL6180_RANGE_RATE);
846 	if (ret < 0)
847 		goto out;
848 	data->scan.channels[VL6180_PROX] = ret;
849 
850 	iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
851 					   ktime_get_boottime_ns());
852 
853 out:
854 	return IRQ_HANDLED;
855 }
856 
vl6180_buffer_preenable(struct iio_dev * indio_dev)857 static int vl6180_buffer_preenable(struct iio_dev *indio_dev)
858 {
859 	struct vl6180_data *data = iio_priv(indio_dev);
860 	u8 val;
861 	int ret;
862 
863 	ret = vl6180_read_byte(data->client, VL6180_INTR_CONFIG);
864 	if (ret < 0)
865 		return ret;
866 
867 	/* Enable ALS and Range ready interrupts */
868 	val = ret | VL6180_ALS_READY | VL6180_RANGE_READY;
869 	ret = vl6180_write_byte(data->client, VL6180_INTR_CONFIG, val);
870 
871 	return ret;
872 }
873 
vl6180_buffer_postdisable(struct iio_dev * indio_dev)874 static int vl6180_buffer_postdisable(struct iio_dev *indio_dev)
875 {
876 	struct vl6180_data *data = iio_priv(indio_dev);
877 	u8 val;
878 	int ret;
879 
880 	ret = vl6180_read_byte(data->client, VL6180_INTR_CONFIG);
881 	if (ret < 0)
882 		return ret;
883 
884 	/* Disable ALS and Range ready interrupts */
885 	val = ret & ~(VL6180_ALS_READY | VL6180_RANGE_READY);
886 	ret = vl6180_write_byte(data->client, VL6180_INTR_CONFIG, val);
887 
888 	return ret;
889 }
890 
891 static const struct iio_buffer_setup_ops vl6180_buffer_setup_ops = {
892 	.preenable = vl6180_buffer_preenable,
893 	.postdisable = vl6180_buffer_postdisable,
894 };
895 
vl6180_probe(struct i2c_client * client,const struct i2c_device_id * id)896 static int vl6180_probe(struct i2c_client *client,
897 			  const struct i2c_device_id *id)
898 {
899 	struct vl6180_data *data;
900 	struct iio_dev *indio_dev;
901 	struct iio_buffer *buffer;
902 	u32 type;
903 	int ret;
904 
905 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
906 	if (!indio_dev)
907 		return -ENOMEM;
908 
909 	data = iio_priv(indio_dev);
910 	i2c_set_clientdata(client, indio_dev);
911 	data->client = client;
912 	mutex_init(&data->lock);
913 
914 	indio_dev->info = &vl6180_info;
915 	indio_dev->channels = vl6180_channels;
916 	indio_dev->num_channels = ARRAY_SIZE(vl6180_channels);
917 	indio_dev->name = VL6180_DRV_NAME;
918 	indio_dev->modes = INDIO_DIRECT_MODE;
919 
920 	/*
921 	 * NOTE: If the power is controlled by gpio, the power
922 	 * configuration should match the power-up timing.
923 	 */
924 	data->avdd = devm_gpiod_get_optional(&client->dev, "avdd",
925 					     GPIOD_OUT_HIGH);
926 	data->chip_enable = devm_gpiod_get_optional(&client->dev, "chip-enable",
927 						    GPIOD_OUT_HIGH);
928 
929 	ret = vl6180_init(data);
930 	if (ret < 0)
931 		return ret;
932 
933 	if (client->irq) {
934 		buffer = devm_iio_kfifo_allocate(&client->dev);
935 		if (!buffer)
936 			return -ENOMEM;
937 
938 		iio_device_attach_buffer(indio_dev, buffer);
939 		indio_dev->modes |= INDIO_BUFFER_SOFTWARE;
940 		indio_dev->setup_ops = &vl6180_buffer_setup_ops;
941 
942 		type = irqd_get_trigger_type(irq_get_irq_data(client->irq));
943 		ret = devm_request_threaded_irq(&client->dev, client->irq,
944 						NULL, vl6180_irq_thread,
945 						type | IRQF_ONESHOT, "vl6180",
946 						data);
947 		if (ret) {
948 			dev_err(&client->dev,
949 				"failed to request vl6180 IRQ\n");
950 			return ret;
951 		}
952 	}
953 
954 	return devm_iio_device_register(&client->dev, indio_dev);
955 }
956 
957 static const struct of_device_id vl6180_of_match[] = {
958 	{ .compatible = "st,vl6180", },
959 	{ },
960 };
961 MODULE_DEVICE_TABLE(of, vl6180_of_match);
962 
963 static const struct i2c_device_id vl6180_id[] = {
964 	{ "vl6180", 0 },
965 	{ }
966 };
967 MODULE_DEVICE_TABLE(i2c, vl6180_id);
968 
969 static struct i2c_driver vl6180_driver = {
970 	.driver = {
971 		.name   = VL6180_DRV_NAME,
972 		.of_match_table = vl6180_of_match,
973 	},
974 	.probe  = vl6180_probe,
975 	.id_table = vl6180_id,
976 };
977 
978 module_i2c_driver(vl6180_driver);
979 
980 MODULE_AUTHOR("Peter Meerwald-Stadler <pmeerw@pmeerw.net>");
981 MODULE_AUTHOR("Manivannan Sadhasivam <manivannanece23@gmail.com>");
982 MODULE_DESCRIPTION("STMicro VL6180 ALS, range and proximity sensor driver");
983 MODULE_LICENSE("GPL");
984