]>
Commit | Line | Data |
---|---|---|
5e7f47e4 MS |
1 | /* |
2 | * vl6180.c - Support for STMicroelectronics VL6180 ALS, range and proximity | |
3 | * sensor | |
4 | * | |
5 | * Copyright 2017 Peter Meerwald-Stadler <pmeerw@pmeerw.net> | |
6 | * Copyright 2017 Manivannan Sadhasivam <manivannanece23@gmail.com> | |
7 | * | |
8 | * This file is subject to the terms and conditions of version 2 of | |
9 | * the GNU General Public License. See the file COPYING in the main | |
10 | * directory of this archive for more details. | |
11 | * | |
12 | * IIO driver for VL6180 (7-bit I2C slave address 0x29) | |
13 | * | |
14 | * Range: 0 to 100mm | |
15 | * ALS: < 1 Lux up to 100 kLux | |
16 | * IR: 850nm | |
17 | * | |
18 | * TODO: irq, threshold events, continuous mode, hardware buffer | |
19 | */ | |
20 | ||
21 | #include <linux/module.h> | |
22 | #include <linux/i2c.h> | |
23 | #include <linux/mutex.h> | |
24 | #include <linux/err.h> | |
25 | #include <linux/of.h> | |
26 | #include <linux/delay.h> | |
27 | ||
28 | #include <linux/iio/iio.h> | |
29 | #include <linux/iio/sysfs.h> | |
30 | ||
31 | #define VL6180_DRV_NAME "vl6180" | |
32 | ||
33 | /* Device identification register and value */ | |
34 | #define VL6180_MODEL_ID 0x000 | |
35 | #define VL6180_MODEL_ID_VAL 0xb4 | |
36 | ||
37 | /* Configuration registers */ | |
38 | #define VL6180_INTR_CONFIG 0x014 | |
39 | #define VL6180_INTR_CLEAR 0x015 | |
40 | #define VL6180_OUT_OF_RESET 0x016 | |
41 | #define VL6180_HOLD 0x017 | |
42 | #define VL6180_RANGE_START 0x018 | |
43 | #define VL6180_ALS_START 0x038 | |
44 | #define VL6180_ALS_GAIN 0x03f | |
45 | #define VL6180_ALS_IT 0x040 | |
46 | ||
47 | /* Status registers */ | |
48 | #define VL6180_RANGE_STATUS 0x04d | |
49 | #define VL6180_ALS_STATUS 0x04e | |
50 | #define VL6180_INTR_STATUS 0x04f | |
51 | ||
52 | /* Result value registers */ | |
53 | #define VL6180_ALS_VALUE 0x050 | |
54 | #define VL6180_RANGE_VALUE 0x062 | |
55 | #define VL6180_RANGE_RATE 0x066 | |
56 | ||
57 | /* bits of the RANGE_START and ALS_START register */ | |
58 | #define VL6180_MODE_CONT BIT(1) /* continuous mode */ | |
59 | #define VL6180_STARTSTOP BIT(0) /* start measurement, auto-reset */ | |
60 | ||
61 | /* bits of the INTR_STATUS and INTR_CONFIG register */ | |
62 | #define VL6180_ALS_READY BIT(5) | |
63 | #define VL6180_RANGE_READY BIT(2) | |
64 | ||
65 | /* bits of the INTR_CLEAR register */ | |
66 | #define VL6180_CLEAR_ERROR BIT(2) | |
67 | #define VL6180_CLEAR_ALS BIT(1) | |
68 | #define VL6180_CLEAR_RANGE BIT(0) | |
69 | ||
70 | /* bits of the HOLD register */ | |
71 | #define VL6180_HOLD_ON BIT(0) | |
72 | ||
73 | /* default value for the ALS_IT register */ | |
74 | #define VL6180_ALS_IT_100 0x63 /* 100 ms */ | |
75 | ||
76 | /* values for the ALS_GAIN register */ | |
77 | #define VL6180_ALS_GAIN_1 0x46 | |
78 | #define VL6180_ALS_GAIN_1_25 0x45 | |
79 | #define VL6180_ALS_GAIN_1_67 0x44 | |
80 | #define VL6180_ALS_GAIN_2_5 0x43 | |
81 | #define VL6180_ALS_GAIN_5 0x42 | |
82 | #define VL6180_ALS_GAIN_10 0x41 | |
83 | #define VL6180_ALS_GAIN_20 0x40 | |
84 | #define VL6180_ALS_GAIN_40 0x47 | |
85 | ||
86 | struct vl6180_data { | |
87 | struct i2c_client *client; | |
88 | struct mutex lock; | |
89 | }; | |
90 | ||
91 | enum { VL6180_ALS, VL6180_RANGE, VL6180_PROX }; | |
92 | ||
93 | /** | |
94 | * struct vl6180_chan_regs - Registers for accessing channels | |
95 | * @drdy_mask: Data ready bit in status register | |
96 | * @start_reg: Conversion start register | |
97 | * @value_reg: Result value register | |
98 | * @word: Register word length | |
99 | */ | |
100 | struct vl6180_chan_regs { | |
101 | u8 drdy_mask; | |
102 | u16 start_reg, value_reg; | |
103 | bool word; | |
104 | }; | |
105 | ||
106 | static const struct vl6180_chan_regs vl6180_chan_regs_table[] = { | |
107 | [VL6180_ALS] = { | |
108 | .drdy_mask = VL6180_ALS_READY, | |
109 | .start_reg = VL6180_ALS_START, | |
110 | .value_reg = VL6180_ALS_VALUE, | |
111 | .word = true, | |
112 | }, | |
113 | [VL6180_RANGE] = { | |
114 | .drdy_mask = VL6180_RANGE_READY, | |
115 | .start_reg = VL6180_RANGE_START, | |
116 | .value_reg = VL6180_RANGE_VALUE, | |
117 | .word = false, | |
118 | }, | |
119 | [VL6180_PROX] = { | |
120 | .drdy_mask = VL6180_RANGE_READY, | |
121 | .start_reg = VL6180_RANGE_START, | |
122 | .value_reg = VL6180_RANGE_RATE, | |
123 | .word = true, | |
124 | }, | |
125 | }; | |
126 | ||
127 | static int vl6180_read(struct i2c_client *client, u16 cmd, void *databuf, | |
128 | u8 len) | |
129 | { | |
130 | __be16 cmdbuf = cpu_to_be16(cmd); | |
131 | struct i2c_msg msgs[2] = { | |
132 | { .addr = client->addr, .len = sizeof(cmdbuf), .buf = (u8 *) &cmdbuf }, | |
133 | { .addr = client->addr, .len = len, .buf = databuf, | |
134 | .flags = I2C_M_RD } }; | |
135 | int ret; | |
136 | ||
137 | ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); | |
138 | if (ret < 0) | |
139 | dev_err(&client->dev, "failed reading register 0x%04x\n", cmd); | |
140 | ||
141 | return ret; | |
142 | } | |
143 | ||
144 | static int vl6180_read_byte(struct i2c_client *client, u16 cmd) | |
145 | { | |
146 | u8 data; | |
147 | int ret; | |
148 | ||
149 | ret = vl6180_read(client, cmd, &data, sizeof(data)); | |
150 | if (ret < 0) | |
151 | return ret; | |
152 | ||
153 | return data; | |
154 | } | |
155 | ||
156 | static int vl6180_read_word(struct i2c_client *client, u16 cmd) | |
157 | { | |
158 | __be16 data; | |
159 | int ret; | |
160 | ||
161 | ret = vl6180_read(client, cmd, &data, sizeof(data)); | |
162 | if (ret < 0) | |
163 | return ret; | |
164 | ||
165 | return be16_to_cpu(data); | |
166 | } | |
167 | ||
168 | static int vl6180_write_byte(struct i2c_client *client, u16 cmd, u8 val) | |
169 | { | |
170 | u8 buf[3]; | |
171 | struct i2c_msg msgs[1] = { | |
172 | { .addr = client->addr, .len = sizeof(buf), .buf = (u8 *) &buf } }; | |
173 | int ret; | |
174 | ||
175 | buf[0] = cmd >> 8; | |
176 | buf[1] = cmd & 0xff; | |
177 | buf[2] = val; | |
178 | ||
179 | ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); | |
180 | if (ret < 0) { | |
181 | dev_err(&client->dev, "failed writing register 0x%04x\n", cmd); | |
182 | return ret; | |
183 | } | |
184 | ||
185 | return 0; | |
186 | } | |
187 | ||
188 | static int vl6180_write_word(struct i2c_client *client, u16 cmd, u16 val) | |
189 | { | |
190 | __be16 buf[2]; | |
191 | struct i2c_msg msgs[1] = { | |
192 | { .addr = client->addr, .len = sizeof(buf), .buf = (u8 *) &buf } }; | |
193 | int ret; | |
194 | ||
195 | buf[0] = cpu_to_be16(cmd); | |
196 | buf[1] = cpu_to_be16(val); | |
197 | ||
198 | ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); | |
199 | if (ret < 0) { | |
200 | dev_err(&client->dev, "failed writing register 0x%04x\n", cmd); | |
201 | return ret; | |
202 | } | |
203 | ||
204 | return 0; | |
205 | } | |
206 | ||
207 | static int vl6180_measure(struct vl6180_data *data, int addr) | |
208 | { | |
209 | struct i2c_client *client = data->client; | |
210 | int tries = 20, ret; | |
211 | u16 value; | |
212 | ||
213 | mutex_lock(&data->lock); | |
214 | /* Start single shot measurement */ | |
215 | ret = vl6180_write_byte(client, | |
216 | vl6180_chan_regs_table[addr].start_reg, VL6180_STARTSTOP); | |
217 | if (ret < 0) | |
218 | goto fail; | |
219 | ||
220 | while (tries--) { | |
221 | ret = vl6180_read_byte(client, VL6180_INTR_STATUS); | |
222 | if (ret < 0) | |
223 | goto fail; | |
224 | ||
225 | if (ret & vl6180_chan_regs_table[addr].drdy_mask) | |
226 | break; | |
227 | msleep(20); | |
228 | } | |
229 | ||
230 | if (tries < 0) { | |
231 | ret = -EIO; | |
232 | goto fail; | |
233 | } | |
234 | ||
235 | /* Read result value from appropriate registers */ | |
236 | ret = vl6180_chan_regs_table[addr].word ? | |
237 | vl6180_read_word(client, vl6180_chan_regs_table[addr].value_reg) : | |
238 | vl6180_read_byte(client, vl6180_chan_regs_table[addr].value_reg); | |
239 | if (ret < 0) | |
240 | goto fail; | |
241 | value = ret; | |
242 | ||
243 | /* Clear the interrupt flag after data read */ | |
244 | ret = vl6180_write_byte(client, VL6180_INTR_CLEAR, | |
245 | VL6180_CLEAR_ERROR | VL6180_CLEAR_ALS | VL6180_CLEAR_RANGE); | |
246 | if (ret < 0) | |
247 | goto fail; | |
248 | ||
249 | ret = value; | |
250 | ||
251 | fail: | |
252 | mutex_unlock(&data->lock); | |
253 | ||
254 | return ret; | |
255 | } | |
256 | ||
257 | static const struct iio_chan_spec vl6180_channels[] = { | |
258 | { | |
259 | .type = IIO_LIGHT, | |
260 | .address = VL6180_ALS, | |
261 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | | |
262 | BIT(IIO_CHAN_INFO_INT_TIME) | | |
263 | BIT(IIO_CHAN_INFO_SCALE) | | |
264 | BIT(IIO_CHAN_INFO_HARDWAREGAIN), | |
265 | }, { | |
266 | .type = IIO_DISTANCE, | |
267 | .address = VL6180_RANGE, | |
268 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | | |
269 | BIT(IIO_CHAN_INFO_SCALE), | |
270 | }, { | |
271 | .type = IIO_PROXIMITY, | |
272 | .address = VL6180_PROX, | |
273 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), | |
274 | } | |
275 | }; | |
276 | ||
277 | /* | |
278 | * Columns 3 & 4 represent the same value in decimal and hex notations. | |
279 | * Kept in order to avoid the datatype conversion while reading the | |
280 | * hardware_gain. | |
281 | */ | |
282 | static const int vl6180_als_gain[8][4] = { | |
283 | { 1, 0, 70, VL6180_ALS_GAIN_1 }, | |
284 | { 1, 250000, 69, VL6180_ALS_GAIN_1_25 }, | |
285 | { 1, 670000, 68, VL6180_ALS_GAIN_1_67 }, | |
286 | { 2, 500000, 67, VL6180_ALS_GAIN_2_5 }, | |
287 | { 5, 0, 66, VL6180_ALS_GAIN_5 }, | |
288 | { 10, 0, 65, VL6180_ALS_GAIN_10 }, | |
289 | { 20, 0, 64, VL6180_ALS_GAIN_20 }, | |
290 | { 40, 0, 71, VL6180_ALS_GAIN_40 } | |
291 | }; | |
292 | ||
293 | static int vl6180_read_raw(struct iio_dev *indio_dev, | |
294 | struct iio_chan_spec const *chan, | |
295 | int *val, int *val2, long mask) | |
296 | { | |
297 | struct vl6180_data *data = iio_priv(indio_dev); | |
298 | int ret, i; | |
299 | ||
300 | switch (mask) { | |
301 | case IIO_CHAN_INFO_RAW: | |
302 | ret = vl6180_measure(data, chan->address); | |
303 | if (ret < 0) | |
304 | return ret; | |
305 | *val = ret; | |
306 | ||
307 | return IIO_VAL_INT; | |
308 | case IIO_CHAN_INFO_INT_TIME: | |
309 | ret = vl6180_read_word(data->client, VL6180_ALS_IT); | |
310 | if (ret < 0) | |
311 | return ret; | |
312 | *val = 0; /* 1 count = 1ms (0 = 1ms) */ | |
313 | *val2 = (ret + 1) * 1000; /* convert to seconds */ | |
314 | ||
315 | return IIO_VAL_INT_PLUS_MICRO; | |
316 | case IIO_CHAN_INFO_SCALE: | |
317 | switch (chan->type) { | |
318 | case IIO_LIGHT: | |
319 | *val = 0; /* one ALS count is 0.32 Lux */ | |
320 | *val2 = 320000; | |
321 | break; | |
322 | case IIO_DISTANCE: | |
323 | *val = 0; /* sensor reports mm, scale to meter */ | |
324 | *val2 = 1000; | |
325 | break; | |
326 | default: | |
327 | return -EINVAL; | |
328 | } | |
329 | ||
330 | return IIO_VAL_INT_PLUS_MICRO; | |
331 | case IIO_CHAN_INFO_HARDWAREGAIN: | |
332 | ret = vl6180_read_byte(data->client, VL6180_ALS_GAIN); | |
333 | if (ret < 0) | |
334 | return -EINVAL; | |
335 | for (i = 0; i < ARRAY_SIZE(vl6180_als_gain); i++) { | |
336 | if (ret == vl6180_als_gain[i][2]) { | |
337 | *val = vl6180_als_gain[i][0]; | |
338 | *val2 = vl6180_als_gain[i][1]; | |
339 | } | |
340 | } | |
341 | ||
342 | return IIO_VAL_INT_PLUS_MICRO; | |
343 | default: | |
344 | return -EINVAL; | |
345 | } | |
346 | } | |
347 | ||
348 | static IIO_CONST_ATTR(als_gain_available, "1 1.25 1.67 2.5 5 10 20 40"); | |
349 | ||
350 | static struct attribute *vl6180_attributes[] = { | |
351 | &iio_const_attr_als_gain_available.dev_attr.attr, | |
352 | NULL | |
353 | }; | |
354 | ||
355 | static const struct attribute_group vl6180_attribute_group = { | |
356 | .attrs = vl6180_attributes, | |
357 | }; | |
358 | ||
359 | /* HOLD is needed before updating any config registers */ | |
360 | static int vl6180_hold(struct vl6180_data *data, bool hold) | |
361 | { | |
362 | return vl6180_write_byte(data->client, VL6180_HOLD, | |
363 | hold ? VL6180_HOLD_ON : 0); | |
364 | } | |
365 | ||
366 | static int vl6180_set_als_gain(struct vl6180_data *data, int val, int val2) | |
367 | { | |
368 | int i, ret; | |
369 | ||
370 | for (i = 0; i < ARRAY_SIZE(vl6180_als_gain); i++) { | |
371 | if (val == vl6180_als_gain[i][0] && | |
372 | val2 == vl6180_als_gain[i][1]) { | |
373 | mutex_lock(&data->lock); | |
374 | ret = vl6180_hold(data, true); | |
375 | if (ret < 0) | |
376 | goto fail; | |
377 | ret = vl6180_write_byte(data->client, VL6180_ALS_GAIN, | |
378 | vl6180_als_gain[i][3]); | |
379 | fail: | |
380 | vl6180_hold(data, false); | |
381 | mutex_unlock(&data->lock); | |
382 | return ret; | |
383 | } | |
384 | } | |
385 | ||
386 | return -EINVAL; | |
387 | } | |
388 | ||
1e2ed3d0 | 389 | static int vl6180_set_it(struct vl6180_data *data, int val, int val2) |
5e7f47e4 | 390 | { |
1e2ed3d0 SB |
391 | int ret, it_ms; |
392 | ||
393 | it_ms = (val2 + 500) / 1000; /* round to ms */ | |
394 | if (val != 0 || it_ms < 1 || it_ms > 512) | |
395 | return -EINVAL; | |
5e7f47e4 MS |
396 | |
397 | mutex_lock(&data->lock); | |
398 | ret = vl6180_hold(data, true); | |
399 | if (ret < 0) | |
400 | goto fail; | |
1e2ed3d0 SB |
401 | |
402 | ret = vl6180_write_word(data->client, VL6180_ALS_IT, it_ms - 1); | |
403 | ||
5e7f47e4 MS |
404 | fail: |
405 | vl6180_hold(data, false); | |
406 | mutex_unlock(&data->lock); | |
407 | ||
408 | return ret; | |
409 | } | |
410 | ||
411 | static int vl6180_write_raw(struct iio_dev *indio_dev, | |
412 | struct iio_chan_spec const *chan, | |
413 | int val, int val2, long mask) | |
414 | { | |
415 | struct vl6180_data *data = iio_priv(indio_dev); | |
416 | ||
417 | switch (mask) { | |
418 | case IIO_CHAN_INFO_INT_TIME: | |
1e2ed3d0 | 419 | return vl6180_set_it(data, val, val2); |
5e7f47e4 | 420 | |
5e7f47e4 MS |
421 | case IIO_CHAN_INFO_HARDWAREGAIN: |
422 | if (chan->type != IIO_LIGHT) | |
423 | return -EINVAL; | |
424 | ||
425 | return vl6180_set_als_gain(data, val, val2); | |
426 | default: | |
427 | return -EINVAL; | |
428 | } | |
429 | } | |
430 | ||
431 | static const struct iio_info vl6180_info = { | |
432 | .read_raw = vl6180_read_raw, | |
433 | .write_raw = vl6180_write_raw, | |
434 | .attrs = &vl6180_attribute_group, | |
5e7f47e4 MS |
435 | }; |
436 | ||
437 | static int vl6180_init(struct vl6180_data *data) | |
438 | { | |
439 | struct i2c_client *client = data->client; | |
440 | int ret; | |
441 | ||
442 | ret = vl6180_read_byte(client, VL6180_MODEL_ID); | |
443 | if (ret < 0) | |
444 | return ret; | |
445 | ||
446 | if (ret != VL6180_MODEL_ID_VAL) { | |
447 | dev_err(&client->dev, "invalid model ID %02x\n", ret); | |
448 | return -ENODEV; | |
449 | } | |
450 | ||
451 | ret = vl6180_hold(data, true); | |
452 | if (ret < 0) | |
453 | return ret; | |
454 | ||
455 | ret = vl6180_read_byte(client, VL6180_OUT_OF_RESET); | |
456 | if (ret < 0) | |
457 | return ret; | |
458 | ||
459 | /* | |
460 | * Detect false reset condition here. This bit is always set when the | |
461 | * system comes out of reset. | |
462 | */ | |
463 | if (ret != 0x01) | |
464 | dev_info(&client->dev, "device is not fresh out of reset\n"); | |
465 | ||
466 | /* Enable ALS and Range ready interrupts */ | |
467 | ret = vl6180_write_byte(client, VL6180_INTR_CONFIG, | |
468 | VL6180_ALS_READY | VL6180_RANGE_READY); | |
469 | if (ret < 0) | |
470 | return ret; | |
471 | ||
472 | /* ALS integration time: 100ms */ | |
473 | ret = vl6180_write_word(client, VL6180_ALS_IT, VL6180_ALS_IT_100); | |
474 | if (ret < 0) | |
475 | return ret; | |
476 | ||
477 | /* ALS gain: 1 */ | |
478 | ret = vl6180_write_byte(client, VL6180_ALS_GAIN, VL6180_ALS_GAIN_1); | |
479 | if (ret < 0) | |
480 | return ret; | |
481 | ||
482 | ret = vl6180_write_byte(client, VL6180_OUT_OF_RESET, 0x00); | |
483 | if (ret < 0) | |
484 | return ret; | |
485 | ||
486 | return vl6180_hold(data, false); | |
487 | } | |
488 | ||
489 | static int vl6180_probe(struct i2c_client *client, | |
490 | const struct i2c_device_id *id) | |
491 | { | |
492 | struct vl6180_data *data; | |
493 | struct iio_dev *indio_dev; | |
494 | int ret; | |
495 | ||
496 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); | |
497 | if (!indio_dev) | |
498 | return -ENOMEM; | |
499 | ||
500 | data = iio_priv(indio_dev); | |
501 | i2c_set_clientdata(client, indio_dev); | |
502 | data->client = client; | |
503 | mutex_init(&data->lock); | |
504 | ||
505 | indio_dev->dev.parent = &client->dev; | |
506 | indio_dev->info = &vl6180_info; | |
507 | indio_dev->channels = vl6180_channels; | |
508 | indio_dev->num_channels = ARRAY_SIZE(vl6180_channels); | |
509 | indio_dev->name = VL6180_DRV_NAME; | |
510 | indio_dev->modes = INDIO_DIRECT_MODE; | |
511 | ||
512 | ret = vl6180_init(data); | |
513 | if (ret < 0) | |
514 | return ret; | |
515 | ||
516 | return devm_iio_device_register(&client->dev, indio_dev); | |
517 | } | |
518 | ||
519 | static const struct of_device_id vl6180_of_match[] = { | |
520 | { .compatible = "st,vl6180", }, | |
521 | { }, | |
522 | }; | |
523 | MODULE_DEVICE_TABLE(of, vl6180_of_match); | |
524 | ||
525 | static const struct i2c_device_id vl6180_id[] = { | |
526 | { "vl6180", 0 }, | |
527 | { } | |
528 | }; | |
529 | MODULE_DEVICE_TABLE(i2c, vl6180_id); | |
530 | ||
531 | static struct i2c_driver vl6180_driver = { | |
532 | .driver = { | |
533 | .name = VL6180_DRV_NAME, | |
534 | .of_match_table = of_match_ptr(vl6180_of_match), | |
535 | }, | |
536 | .probe = vl6180_probe, | |
537 | .id_table = vl6180_id, | |
538 | }; | |
539 | ||
540 | module_i2c_driver(vl6180_driver); | |
541 | ||
542 | MODULE_AUTHOR("Peter Meerwald-Stadler <pmeerw@pmeerw.net>"); | |
543 | MODULE_AUTHOR("Manivannan Sadhasivam <manivannanece23@gmail.com>"); | |
544 | MODULE_DESCRIPTION("STMicro VL6180 ALS, range and proximity sensor driver"); | |
545 | MODULE_LICENSE("GPL"); |