2 * vcnl4000.c - Support for Vishay VCNL4000/4010/4020/4040/4200 combined ambient
3 * light and proximity sensor
5 * Copyright 2012 Peter Meerwald <pmeerw@pmeerw.net>
6 * Copyright 2019 Pursim SPC
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.
13 * VCNL4000/10/20 (7-bit I2C slave address 0x13)
14 * VCNL4040 (7-bit I2C slave address 0x60)
15 * VCNL4200 (7-bit I2C slave address 0x51)
18 * allow to adjust IR current
19 * proximity threshold and event handling
20 * periodic ALS/proximity measurement (VCNL4010/20)
21 * interrupts (VCNL4010/20/40, VCNL4200)
24 #include <linux/module.h>
25 #include <linux/i2c.h>
26 #include <linux/err.h>
27 #include <linux/delay.h>
29 #include <linux/iio/iio.h>
30 #include <linux/iio/sysfs.h>
32 #define VCNL4000_DRV_NAME "vcnl4000"
33 #define VCNL4000_PROD_ID 0x01
34 #define VCNL4010_PROD_ID 0x02 /* for VCNL4020, VCNL4010 */
35 #define VCNL4040_PROD_ID 0x86
36 #define VCNL4200_PROD_ID 0x58
38 #define VCNL4000_COMMAND 0x80 /* Command register */
39 #define VCNL4000_PROD_REV 0x81 /* Product ID and Revision ID */
40 #define VCNL4000_LED_CURRENT 0x83 /* IR LED current for proximity mode */
41 #define VCNL4000_AL_PARAM 0x84 /* Ambient light parameter register */
42 #define VCNL4000_AL_RESULT_HI 0x85 /* Ambient light result register, MSB */
43 #define VCNL4000_AL_RESULT_LO 0x86 /* Ambient light result register, LSB */
44 #define VCNL4000_PS_RESULT_HI 0x87 /* Proximity result register, MSB */
45 #define VCNL4000_PS_RESULT_LO 0x88 /* Proximity result register, LSB */
46 #define VCNL4000_PS_MEAS_FREQ 0x89 /* Proximity test signal frequency */
47 #define VCNL4000_PS_MOD_ADJ 0x8a /* Proximity modulator timing adjustment */
49 #define VCNL4200_AL_CONF 0x00 /* Ambient light configuration */
50 #define VCNL4200_PS_CONF1 0x03 /* Proximity configuration */
51 #define VCNL4200_PS_DATA 0x08 /* Proximity data */
52 #define VCNL4200_AL_DATA 0x09 /* Ambient light data */
53 #define VCNL4200_DEV_ID 0x0e /* Device ID, slave address and version */
55 #define VCNL4040_DEV_ID 0x0c /* Device ID and version */
57 /* Bit masks for COMMAND register */
58 #define VCNL4000_AL_RDY BIT(6) /* ALS data ready? */
59 #define VCNL4000_PS_RDY BIT(5) /* proximity data ready? */
60 #define VCNL4000_AL_OD BIT(4) /* start on-demand ALS measurement */
61 #define VCNL4000_PS_OD BIT(3) /* start on-demand proximity measurement */
63 enum vcnl4000_device_ids
{
70 struct vcnl4200_channel
{
72 ktime_t last_measurement
;
73 ktime_t sampling_rate
;
77 struct vcnl4000_data
{
78 struct i2c_client
*client
;
79 enum vcnl4000_device_ids id
;
82 const struct vcnl4000_chip_spec
*chip_spec
;
83 struct mutex vcnl4000_lock
;
84 struct vcnl4200_channel vcnl4200_al
;
85 struct vcnl4200_channel vcnl4200_ps
;
88 struct vcnl4000_chip_spec
{
90 int (*init
)(struct vcnl4000_data
*data
);
91 int (*measure_light
)(struct vcnl4000_data
*data
, int *val
);
92 int (*measure_proximity
)(struct vcnl4000_data
*data
, int *val
);
95 static const struct i2c_device_id vcnl4000_id
[] = {
96 { "vcnl4000", VCNL4000
},
97 { "vcnl4010", VCNL4010
},
98 { "vcnl4020", VCNL4010
},
99 { "vcnl4040", VCNL4040
},
100 { "vcnl4200", VCNL4200
},
103 MODULE_DEVICE_TABLE(i2c
, vcnl4000_id
);
105 static int vcnl4000_init(struct vcnl4000_data
*data
)
109 ret
= i2c_smbus_read_byte_data(data
->client
, VCNL4000_PROD_REV
);
115 case VCNL4000_PROD_ID
:
116 if (data
->id
!= VCNL4000
)
117 dev_warn(&data
->client
->dev
,
118 "wrong device id, use vcnl4000");
120 case VCNL4010_PROD_ID
:
121 if (data
->id
!= VCNL4010
)
122 dev_warn(&data
->client
->dev
,
123 "wrong device id, use vcnl4010/4020");
129 data
->rev
= ret
& 0xf;
130 data
->al_scale
= 250000;
131 mutex_init(&data
->vcnl4000_lock
);
136 static int vcnl4200_init(struct vcnl4000_data
*data
)
140 ret
= i2c_smbus_read_word_data(data
->client
, VCNL4200_DEV_ID
);
146 if (id
!= VCNL4200_PROD_ID
) {
147 ret
= i2c_smbus_read_word_data(data
->client
, VCNL4040_DEV_ID
);
153 if (id
!= VCNL4040_PROD_ID
)
157 dev_dbg(&data
->client
->dev
, "device id 0x%x", id
);
159 data
->rev
= (ret
>> 8) & 0xf;
161 /* Set defaults and enable both channels */
162 ret
= i2c_smbus_write_word_data(data
->client
, VCNL4200_AL_CONF
, 0);
165 ret
= i2c_smbus_write_word_data(data
->client
, VCNL4200_PS_CONF1
, 0);
169 data
->al_scale
= 24000;
170 data
->vcnl4200_al
.reg
= VCNL4200_AL_DATA
;
171 data
->vcnl4200_ps
.reg
= VCNL4200_PS_DATA
;
173 case VCNL4200_PROD_ID
:
174 /* Integration time is 50ms, but the experiments */
175 /* show 54ms in total. */
176 data
->vcnl4200_al
.sampling_rate
= ktime_set(0, 54000 * 1000);
177 data
->vcnl4200_ps
.sampling_rate
= ktime_set(0, 4200 * 1000);
179 case VCNL4040_PROD_ID
:
180 /* Integration time is 80ms, add 10ms. */
181 data
->vcnl4200_al
.sampling_rate
= ktime_set(0, 100000 * 1000);
182 data
->vcnl4200_ps
.sampling_rate
= ktime_set(0, 100000 * 1000);
185 data
->vcnl4200_al
.last_measurement
= ktime_set(0, 0);
186 data
->vcnl4200_ps
.last_measurement
= ktime_set(0, 0);
187 mutex_init(&data
->vcnl4200_al
.lock
);
188 mutex_init(&data
->vcnl4200_ps
.lock
);
193 static int vcnl4000_measure(struct vcnl4000_data
*data
, u8 req_mask
,
194 u8 rdy_mask
, u8 data_reg
, int *val
)
200 mutex_lock(&data
->vcnl4000_lock
);
202 ret
= i2c_smbus_write_byte_data(data
->client
, VCNL4000_COMMAND
,
207 /* wait for data to become ready */
209 ret
= i2c_smbus_read_byte_data(data
->client
, VCNL4000_COMMAND
);
214 msleep(20); /* measurement takes up to 100 ms */
218 dev_err(&data
->client
->dev
,
219 "vcnl4000_measure() failed, data not ready\n");
224 ret
= i2c_smbus_read_i2c_block_data(data
->client
,
225 data_reg
, sizeof(buf
), (u8
*) &buf
);
229 mutex_unlock(&data
->vcnl4000_lock
);
230 *val
= be16_to_cpu(buf
);
235 mutex_unlock(&data
->vcnl4000_lock
);
239 static int vcnl4200_measure(struct vcnl4000_data
*data
,
240 struct vcnl4200_channel
*chan
, int *val
)
244 ktime_t next_measurement
;
246 mutex_lock(&chan
->lock
);
248 next_measurement
= ktime_add(chan
->last_measurement
,
249 chan
->sampling_rate
);
250 delta
= ktime_us_delta(next_measurement
, ktime_get());
252 usleep_range(delta
, delta
+ 500);
253 chan
->last_measurement
= ktime_get();
255 mutex_unlock(&chan
->lock
);
257 ret
= i2c_smbus_read_word_data(data
->client
, chan
->reg
);
266 static int vcnl4000_measure_light(struct vcnl4000_data
*data
, int *val
)
268 return vcnl4000_measure(data
,
269 VCNL4000_AL_OD
, VCNL4000_AL_RDY
,
270 VCNL4000_AL_RESULT_HI
, val
);
273 static int vcnl4200_measure_light(struct vcnl4000_data
*data
, int *val
)
275 return vcnl4200_measure(data
, &data
->vcnl4200_al
, val
);
278 static int vcnl4000_measure_proximity(struct vcnl4000_data
*data
, int *val
)
280 return vcnl4000_measure(data
,
281 VCNL4000_PS_OD
, VCNL4000_PS_RDY
,
282 VCNL4000_PS_RESULT_HI
, val
);
285 static int vcnl4200_measure_proximity(struct vcnl4000_data
*data
, int *val
)
287 return vcnl4200_measure(data
, &data
->vcnl4200_ps
, val
);
290 static const struct vcnl4000_chip_spec vcnl4000_chip_spec_cfg
[] = {
293 .init
= vcnl4000_init
,
294 .measure_light
= vcnl4000_measure_light
,
295 .measure_proximity
= vcnl4000_measure_proximity
,
298 .prod
= "VCNL4010/4020",
299 .init
= vcnl4000_init
,
300 .measure_light
= vcnl4000_measure_light
,
301 .measure_proximity
= vcnl4000_measure_proximity
,
305 .init
= vcnl4200_init
,
306 .measure_light
= vcnl4200_measure_light
,
307 .measure_proximity
= vcnl4200_measure_proximity
,
311 .init
= vcnl4200_init
,
312 .measure_light
= vcnl4200_measure_light
,
313 .measure_proximity
= vcnl4200_measure_proximity
,
317 static const struct iio_chan_spec vcnl4000_channels
[] = {
320 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
321 BIT(IIO_CHAN_INFO_SCALE
),
323 .type
= IIO_PROXIMITY
,
324 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
328 static int vcnl4000_read_raw(struct iio_dev
*indio_dev
,
329 struct iio_chan_spec
const *chan
,
330 int *val
, int *val2
, long mask
)
333 struct vcnl4000_data
*data
= iio_priv(indio_dev
);
336 case IIO_CHAN_INFO_RAW
:
337 switch (chan
->type
) {
339 ret
= data
->chip_spec
->measure_light(data
, val
);
344 ret
= data
->chip_spec
->measure_proximity(data
, val
);
351 case IIO_CHAN_INFO_SCALE
:
352 if (chan
->type
!= IIO_LIGHT
)
356 *val2
= data
->al_scale
;
357 return IIO_VAL_INT_PLUS_MICRO
;
363 static const struct iio_info vcnl4000_info
= {
364 .read_raw
= vcnl4000_read_raw
,
367 static int vcnl4000_probe(struct i2c_client
*client
,
368 const struct i2c_device_id
*id
)
370 struct vcnl4000_data
*data
;
371 struct iio_dev
*indio_dev
;
374 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
378 data
= iio_priv(indio_dev
);
379 i2c_set_clientdata(client
, indio_dev
);
380 data
->client
= client
;
381 data
->id
= id
->driver_data
;
382 data
->chip_spec
= &vcnl4000_chip_spec_cfg
[data
->id
];
384 ret
= data
->chip_spec
->init(data
);
388 dev_dbg(&client
->dev
, "%s Ambient light/proximity sensor, Rev: %02x\n",
389 data
->chip_spec
->prod
, data
->rev
);
391 indio_dev
->dev
.parent
= &client
->dev
;
392 indio_dev
->info
= &vcnl4000_info
;
393 indio_dev
->channels
= vcnl4000_channels
;
394 indio_dev
->num_channels
= ARRAY_SIZE(vcnl4000_channels
);
395 indio_dev
->name
= VCNL4000_DRV_NAME
;
396 indio_dev
->modes
= INDIO_DIRECT_MODE
;
398 return devm_iio_device_register(&client
->dev
, indio_dev
);
401 static const struct of_device_id vcnl_4000_of_match
[] = {
403 .compatible
= "vishay,vcnl4000",
407 .compatible
= "vishay,vcnl4010",
411 .compatible
= "vishay,vcnl4010",
415 .compatible
= "vishay,vcnl4200",
420 MODULE_DEVICE_TABLE(of
, vcnl_4000_of_match
);
422 static struct i2c_driver vcnl4000_driver
= {
424 .name
= VCNL4000_DRV_NAME
,
425 .of_match_table
= vcnl_4000_of_match
,
427 .probe
= vcnl4000_probe
,
428 .id_table
= vcnl4000_id
,
431 module_i2c_driver(vcnl4000_driver
);
433 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
434 MODULE_DESCRIPTION("Vishay VCNL4000 proximity/ambient light sensor driver");
435 MODULE_LICENSE("GPL");