1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include <linux/module.h>
22#include <linux/i2c.h>
23#include <linux/err.h>
24#include <linux/delay.h>
25
26#include <linux/iio/iio.h>
27#include <linux/iio/sysfs.h>
28
29#define VCNL4000_DRV_NAME "vcnl4000"
30#define VCNL4000_PROD_ID 0x01
31#define VCNL4010_PROD_ID 0x02
32#define VCNL4040_PROD_ID 0x86
33#define VCNL4200_PROD_ID 0x58
34
35#define VCNL4000_COMMAND 0x80
36#define VCNL4000_PROD_REV 0x81
37#define VCNL4000_LED_CURRENT 0x83
38#define VCNL4000_AL_PARAM 0x84
39#define VCNL4000_AL_RESULT_HI 0x85
40#define VCNL4000_AL_RESULT_LO 0x86
41#define VCNL4000_PS_RESULT_HI 0x87
42#define VCNL4000_PS_RESULT_LO 0x88
43#define VCNL4000_PS_MEAS_FREQ 0x89
44#define VCNL4000_PS_MOD_ADJ 0x8a
45
46#define VCNL4200_AL_CONF 0x00
47#define VCNL4200_PS_CONF1 0x03
48#define VCNL4200_PS_DATA 0x08
49#define VCNL4200_AL_DATA 0x09
50#define VCNL4200_DEV_ID 0x0e
51
52#define VCNL4040_DEV_ID 0x0c
53
54
55#define VCNL4000_AL_RDY BIT(6)
56#define VCNL4000_PS_RDY BIT(5)
57#define VCNL4000_AL_OD BIT(4)
58#define VCNL4000_PS_OD BIT(3)
59
60enum vcnl4000_device_ids {
61 VCNL4000,
62 VCNL4010,
63 VCNL4040,
64 VCNL4200,
65};
66
67struct vcnl4200_channel {
68 u8 reg;
69 ktime_t last_measurement;
70 ktime_t sampling_rate;
71 struct mutex lock;
72};
73
74struct vcnl4000_data {
75 struct i2c_client *client;
76 enum vcnl4000_device_ids id;
77 int rev;
78 int al_scale;
79 const struct vcnl4000_chip_spec *chip_spec;
80 struct mutex vcnl4000_lock;
81 struct vcnl4200_channel vcnl4200_al;
82 struct vcnl4200_channel vcnl4200_ps;
83};
84
85struct vcnl4000_chip_spec {
86 const char *prod;
87 int (*init)(struct vcnl4000_data *data);
88 int (*measure_light)(struct vcnl4000_data *data, int *val);
89 int (*measure_proximity)(struct vcnl4000_data *data, int *val);
90};
91
92static const struct i2c_device_id vcnl4000_id[] = {
93 { "vcnl4000", VCNL4000 },
94 { "vcnl4010", VCNL4010 },
95 { "vcnl4020", VCNL4010 },
96 { "vcnl4040", VCNL4040 },
97 { "vcnl4200", VCNL4200 },
98 { }
99};
100MODULE_DEVICE_TABLE(i2c, vcnl4000_id);
101
102static int vcnl4000_init(struct vcnl4000_data *data)
103{
104 int ret, prod_id;
105
106 ret = i2c_smbus_read_byte_data(data->client, VCNL4000_PROD_REV);
107 if (ret < 0)
108 return ret;
109
110 prod_id = ret >> 4;
111 switch (prod_id) {
112 case VCNL4000_PROD_ID:
113 if (data->id != VCNL4000)
114 dev_warn(&data->client->dev,
115 "wrong device id, use vcnl4000");
116 break;
117 case VCNL4010_PROD_ID:
118 if (data->id != VCNL4010)
119 dev_warn(&data->client->dev,
120 "wrong device id, use vcnl4010/4020");
121 break;
122 default:
123 return -ENODEV;
124 }
125
126 data->rev = ret & 0xf;
127 data->al_scale = 250000;
128 mutex_init(&data->vcnl4000_lock);
129
130 return 0;
131};
132
133static int vcnl4200_init(struct vcnl4000_data *data)
134{
135 int ret, id;
136
137 ret = i2c_smbus_read_word_data(data->client, VCNL4200_DEV_ID);
138 if (ret < 0)
139 return ret;
140
141 id = ret & 0xff;
142
143 if (id != VCNL4200_PROD_ID) {
144 ret = i2c_smbus_read_word_data(data->client, VCNL4040_DEV_ID);
145 if (ret < 0)
146 return ret;
147
148 id = ret & 0xff;
149
150 if (id != VCNL4040_PROD_ID)
151 return -ENODEV;
152 }
153
154 dev_dbg(&data->client->dev, "device id 0x%x", id);
155
156 data->rev = (ret >> 8) & 0xf;
157
158
159 ret = i2c_smbus_write_word_data(data->client, VCNL4200_AL_CONF, 0);
160 if (ret < 0)
161 return ret;
162 ret = i2c_smbus_write_word_data(data->client, VCNL4200_PS_CONF1, 0);
163 if (ret < 0)
164 return ret;
165
166 data->vcnl4200_al.reg = VCNL4200_AL_DATA;
167 data->vcnl4200_ps.reg = VCNL4200_PS_DATA;
168 switch (id) {
169 case VCNL4200_PROD_ID:
170
171 data->vcnl4200_al.sampling_rate = ktime_set(0, 60000 * 1000);
172
173 data->vcnl4200_ps.sampling_rate = ktime_set(0, 5760 * 1000);
174 data->al_scale = 24000;
175 break;
176 case VCNL4040_PROD_ID:
177
178 data->vcnl4200_al.sampling_rate = ktime_set(0, 96000 * 1000);
179
180 data->vcnl4200_ps.sampling_rate = ktime_set(0, 6000 * 1000);
181 data->al_scale = 120000;
182 break;
183 }
184 data->vcnl4200_al.last_measurement = ktime_set(0, 0);
185 data->vcnl4200_ps.last_measurement = ktime_set(0, 0);
186 mutex_init(&data->vcnl4200_al.lock);
187 mutex_init(&data->vcnl4200_ps.lock);
188
189 return 0;
190};
191
192static int vcnl4000_measure(struct vcnl4000_data *data, u8 req_mask,
193 u8 rdy_mask, u8 data_reg, int *val)
194{
195 int tries = 20;
196 __be16 buf;
197 int ret;
198
199 mutex_lock(&data->vcnl4000_lock);
200
201 ret = i2c_smbus_write_byte_data(data->client, VCNL4000_COMMAND,
202 req_mask);
203 if (ret < 0)
204 goto fail;
205
206
207 while (tries--) {
208 ret = i2c_smbus_read_byte_data(data->client, VCNL4000_COMMAND);
209 if (ret < 0)
210 goto fail;
211 if (ret & rdy_mask)
212 break;
213 msleep(20);
214 }
215
216 if (tries < 0) {
217 dev_err(&data->client->dev,
218 "vcnl4000_measure() failed, data not ready\n");
219 ret = -EIO;
220 goto fail;
221 }
222
223 ret = i2c_smbus_read_i2c_block_data(data->client,
224 data_reg, sizeof(buf), (u8 *) &buf);
225 if (ret < 0)
226 goto fail;
227
228 mutex_unlock(&data->vcnl4000_lock);
229 *val = be16_to_cpu(buf);
230
231 return 0;
232
233fail:
234 mutex_unlock(&data->vcnl4000_lock);
235 return ret;
236}
237
238static int vcnl4200_measure(struct vcnl4000_data *data,
239 struct vcnl4200_channel *chan, int *val)
240{
241 int ret;
242 s64 delta;
243 ktime_t next_measurement;
244
245 mutex_lock(&chan->lock);
246
247 next_measurement = ktime_add(chan->last_measurement,
248 chan->sampling_rate);
249 delta = ktime_us_delta(next_measurement, ktime_get());
250 if (delta > 0)
251 usleep_range(delta, delta + 500);
252 chan->last_measurement = ktime_get();
253
254 mutex_unlock(&chan->lock);
255
256 ret = i2c_smbus_read_word_data(data->client, chan->reg);
257 if (ret < 0)
258 return ret;
259
260 *val = ret;
261
262 return 0;
263}
264
265static int vcnl4000_measure_light(struct vcnl4000_data *data, int *val)
266{
267 return vcnl4000_measure(data,
268 VCNL4000_AL_OD, VCNL4000_AL_RDY,
269 VCNL4000_AL_RESULT_HI, val);
270}
271
272static int vcnl4200_measure_light(struct vcnl4000_data *data, int *val)
273{
274 return vcnl4200_measure(data, &data->vcnl4200_al, val);
275}
276
277static int vcnl4000_measure_proximity(struct vcnl4000_data *data, int *val)
278{
279 return vcnl4000_measure(data,
280 VCNL4000_PS_OD, VCNL4000_PS_RDY,
281 VCNL4000_PS_RESULT_HI, val);
282}
283
284static int vcnl4200_measure_proximity(struct vcnl4000_data *data, int *val)
285{
286 return vcnl4200_measure(data, &data->vcnl4200_ps, val);
287}
288
289static const struct vcnl4000_chip_spec vcnl4000_chip_spec_cfg[] = {
290 [VCNL4000] = {
291 .prod = "VCNL4000",
292 .init = vcnl4000_init,
293 .measure_light = vcnl4000_measure_light,
294 .measure_proximity = vcnl4000_measure_proximity,
295 },
296 [VCNL4010] = {
297 .prod = "VCNL4010/4020",
298 .init = vcnl4000_init,
299 .measure_light = vcnl4000_measure_light,
300 .measure_proximity = vcnl4000_measure_proximity,
301 },
302 [VCNL4040] = {
303 .prod = "VCNL4040",
304 .init = vcnl4200_init,
305 .measure_light = vcnl4200_measure_light,
306 .measure_proximity = vcnl4200_measure_proximity,
307 },
308 [VCNL4200] = {
309 .prod = "VCNL4200",
310 .init = vcnl4200_init,
311 .measure_light = vcnl4200_measure_light,
312 .measure_proximity = vcnl4200_measure_proximity,
313 },
314};
315
316static const struct iio_chan_spec vcnl4000_channels[] = {
317 {
318 .type = IIO_LIGHT,
319 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
320 BIT(IIO_CHAN_INFO_SCALE),
321 }, {
322 .type = IIO_PROXIMITY,
323 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
324 }
325};
326
327static int vcnl4000_read_raw(struct iio_dev *indio_dev,
328 struct iio_chan_spec const *chan,
329 int *val, int *val2, long mask)
330{
331 int ret;
332 struct vcnl4000_data *data = iio_priv(indio_dev);
333
334 switch (mask) {
335 case IIO_CHAN_INFO_RAW:
336 switch (chan->type) {
337 case IIO_LIGHT:
338 ret = data->chip_spec->measure_light(data, val);
339 if (ret < 0)
340 return ret;
341 return IIO_VAL_INT;
342 case IIO_PROXIMITY:
343 ret = data->chip_spec->measure_proximity(data, val);
344 if (ret < 0)
345 return ret;
346 return IIO_VAL_INT;
347 default:
348 return -EINVAL;
349 }
350 case IIO_CHAN_INFO_SCALE:
351 if (chan->type != IIO_LIGHT)
352 return -EINVAL;
353
354 *val = 0;
355 *val2 = data->al_scale;
356 return IIO_VAL_INT_PLUS_MICRO;
357 default:
358 return -EINVAL;
359 }
360}
361
362static const struct iio_info vcnl4000_info = {
363 .read_raw = vcnl4000_read_raw,
364};
365
366static int vcnl4000_probe(struct i2c_client *client,
367 const struct i2c_device_id *id)
368{
369 struct vcnl4000_data *data;
370 struct iio_dev *indio_dev;
371 int ret;
372
373 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
374 if (!indio_dev)
375 return -ENOMEM;
376
377 data = iio_priv(indio_dev);
378 i2c_set_clientdata(client, indio_dev);
379 data->client = client;
380 data->id = id->driver_data;
381 data->chip_spec = &vcnl4000_chip_spec_cfg[data->id];
382
383 ret = data->chip_spec->init(data);
384 if (ret < 0)
385 return ret;
386
387 dev_dbg(&client->dev, "%s Ambient light/proximity sensor, Rev: %02x\n",
388 data->chip_spec->prod, data->rev);
389
390 indio_dev->dev.parent = &client->dev;
391 indio_dev->info = &vcnl4000_info;
392 indio_dev->channels = vcnl4000_channels;
393 indio_dev->num_channels = ARRAY_SIZE(vcnl4000_channels);
394 indio_dev->name = VCNL4000_DRV_NAME;
395 indio_dev->modes = INDIO_DIRECT_MODE;
396
397 return devm_iio_device_register(&client->dev, indio_dev);
398}
399
400static const struct of_device_id vcnl_4000_of_match[] = {
401 {
402 .compatible = "vishay,vcnl4000",
403 .data = (void *)VCNL4000,
404 },
405 {
406 .compatible = "vishay,vcnl4010",
407 .data = (void *)VCNL4010,
408 },
409 {
410 .compatible = "vishay,vcnl4020",
411 .data = (void *)VCNL4010,
412 },
413 {
414 .compatible = "vishay,vcnl4040",
415 .data = (void *)VCNL4040,
416 },
417 {
418 .compatible = "vishay,vcnl4200",
419 .data = (void *)VCNL4200,
420 },
421 {},
422};
423MODULE_DEVICE_TABLE(of, vcnl_4000_of_match);
424
425static struct i2c_driver vcnl4000_driver = {
426 .driver = {
427 .name = VCNL4000_DRV_NAME,
428 .of_match_table = vcnl_4000_of_match,
429 },
430 .probe = vcnl4000_probe,
431 .id_table = vcnl4000_id,
432};
433
434module_i2c_driver(vcnl4000_driver);
435
436MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
437MODULE_DESCRIPTION("Vishay VCNL4000 proximity/ambient light sensor driver");
438MODULE_LICENSE("GPL");
439