1 // SPDX-License-Identifier: GPL-2.0-only
3 * vcnl4000.c - Support for Vishay VCNL4000/4010/4020/4040/4200 combined ambient
4 * light and proximity sensor
6 * Copyright 2012 Peter Meerwald <pmeerw@pmeerw.net>
7 * Copyright 2019 Pursim SPC
10 * VCNL4000/10/20 (7-bit I2C slave address 0x13)
11 * VCNL4040 (7-bit I2C slave address 0x60)
12 * VCNL4200 (7-bit I2C slave address 0x51)
15 * allow to adjust IR current
16 * proximity threshold and event handling
17 * periodic ALS/proximity measurement (VCNL4010/20)
18 * interrupts (VCNL4010/20/40, VCNL4200)
21 #include <linux/module.h>
22 #include <linux/i2c.h>
23 #include <linux/err.h>
24 #include <linux/delay.h>
26 #include <linux/iio/iio.h>
27 #include <linux/iio/sysfs.h>
29 #define VCNL4000_DRV_NAME "vcnl4000"
30 #define VCNL4000_PROD_ID 0x01
31 #define VCNL4010_PROD_ID 0x02 /* for VCNL4020, VCNL4010 */
32 #define VCNL4040_PROD_ID 0x86
33 #define VCNL4200_PROD_ID 0x58
35 #define VCNL4000_COMMAND 0x80 /* Command register */
36 #define VCNL4000_PROD_REV 0x81 /* Product ID and Revision ID */
37 #define VCNL4000_LED_CURRENT 0x83 /* IR LED current for proximity mode */
38 #define VCNL4000_AL_PARAM 0x84 /* Ambient light parameter register */
39 #define VCNL4000_AL_RESULT_HI 0x85 /* Ambient light result register, MSB */
40 #define VCNL4000_AL_RESULT_LO 0x86 /* Ambient light result register, LSB */
41 #define VCNL4000_PS_RESULT_HI 0x87 /* Proximity result register, MSB */
42 #define VCNL4000_PS_RESULT_LO 0x88 /* Proximity result register, LSB */
43 #define VCNL4000_PS_MEAS_FREQ 0x89 /* Proximity test signal frequency */
44 #define VCNL4000_PS_MOD_ADJ 0x8a /* Proximity modulator timing adjustment */
46 #define VCNL4200_AL_CONF 0x00 /* Ambient light configuration */
47 #define VCNL4200_PS_CONF1 0x03 /* Proximity configuration */
48 #define VCNL4200_PS_DATA 0x08 /* Proximity data */
49 #define VCNL4200_AL_DATA 0x09 /* Ambient light data */
50 #define VCNL4200_DEV_ID 0x0e /* Device ID, slave address and version */
52 #define VCNL4040_DEV_ID 0x0c /* Device ID and version */
54 /* Bit masks for COMMAND register */
55 #define VCNL4000_AL_RDY BIT(6) /* ALS data ready? */
56 #define VCNL4000_PS_RDY BIT(5) /* proximity data ready? */
57 #define VCNL4000_AL_OD BIT(4) /* start on-demand ALS measurement */
58 #define VCNL4000_PS_OD BIT(3) /* start on-demand proximity measurement */
60 enum vcnl4000_device_ids
{
67 struct vcnl4200_channel
{
69 ktime_t last_measurement
;
70 ktime_t sampling_rate
;
74 struct vcnl4000_data
{
75 struct i2c_client
*client
;
76 enum vcnl4000_device_ids id
;
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
;
85 struct vcnl4000_chip_spec
{
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
);
92 static const struct i2c_device_id vcnl4000_id
[] = {
93 { "vcnl4000", VCNL4000
},
94 { "vcnl4010", VCNL4010
},
95 { "vcnl4020", VCNL4010
},
96 { "vcnl4040", VCNL4040
},
97 { "vcnl4200", VCNL4200
},
100 MODULE_DEVICE_TABLE(i2c
, vcnl4000_id
);
102 static int vcnl4000_init(struct vcnl4000_data
*data
)
106 ret
= i2c_smbus_read_byte_data(data
->client
, VCNL4000_PROD_REV
);
112 case VCNL4000_PROD_ID
:
113 if (data
->id
!= VCNL4000
)
114 dev_warn(&data
->client
->dev
,
115 "wrong device id, use vcnl4000");
117 case VCNL4010_PROD_ID
:
118 if (data
->id
!= VCNL4010
)
119 dev_warn(&data
->client
->dev
,
120 "wrong device id, use vcnl4010/4020");
126 data
->rev
= ret
& 0xf;
127 data
->al_scale
= 250000;
128 mutex_init(&data
->vcnl4000_lock
);
133 static int vcnl4200_init(struct vcnl4000_data
*data
)
137 ret
= i2c_smbus_read_word_data(data
->client
, VCNL4200_DEV_ID
);
143 if (id
!= VCNL4200_PROD_ID
) {
144 ret
= i2c_smbus_read_word_data(data
->client
, VCNL4040_DEV_ID
);
150 if (id
!= VCNL4040_PROD_ID
)
154 dev_dbg(&data
->client
->dev
, "device id 0x%x", id
);
156 data
->rev
= (ret
>> 8) & 0xf;
158 /* Set defaults and enable both channels */
159 ret
= i2c_smbus_write_word_data(data
->client
, VCNL4200_AL_CONF
, 0);
162 ret
= i2c_smbus_write_word_data(data
->client
, VCNL4200_PS_CONF1
, 0);
166 data
->vcnl4200_al
.reg
= VCNL4200_AL_DATA
;
167 data
->vcnl4200_ps
.reg
= VCNL4200_PS_DATA
;
169 case VCNL4200_PROD_ID
:
170 /* Integration time is 50ms, but the experiments */
171 /* show 54ms in total. */
172 data
->vcnl4200_al
.sampling_rate
= ktime_set(0, 54000 * 1000);
173 data
->vcnl4200_ps
.sampling_rate
= ktime_set(0, 4200 * 1000);
174 data
->al_scale
= 24000;
176 case VCNL4040_PROD_ID
:
177 /* Integration time is 80ms, add 10ms. */
178 data
->vcnl4200_al
.sampling_rate
= ktime_set(0, 100000 * 1000);
179 data
->vcnl4200_ps
.sampling_rate
= ktime_set(0, 100000 * 1000);
180 data
->al_scale
= 120000;
183 data
->vcnl4200_al
.last_measurement
= ktime_set(0, 0);
184 data
->vcnl4200_ps
.last_measurement
= ktime_set(0, 0);
185 mutex_init(&data
->vcnl4200_al
.lock
);
186 mutex_init(&data
->vcnl4200_ps
.lock
);
191 static int vcnl4000_measure(struct vcnl4000_data
*data
, u8 req_mask
,
192 u8 rdy_mask
, u8 data_reg
, int *val
)
198 mutex_lock(&data
->vcnl4000_lock
);
200 ret
= i2c_smbus_write_byte_data(data
->client
, VCNL4000_COMMAND
,
205 /* wait for data to become ready */
207 ret
= i2c_smbus_read_byte_data(data
->client
, VCNL4000_COMMAND
);
212 msleep(20); /* measurement takes up to 100 ms */
216 dev_err(&data
->client
->dev
,
217 "vcnl4000_measure() failed, data not ready\n");
222 ret
= i2c_smbus_read_i2c_block_data(data
->client
,
223 data_reg
, sizeof(buf
), (u8
*) &buf
);
227 mutex_unlock(&data
->vcnl4000_lock
);
228 *val
= be16_to_cpu(buf
);
233 mutex_unlock(&data
->vcnl4000_lock
);
237 static int vcnl4200_measure(struct vcnl4000_data
*data
,
238 struct vcnl4200_channel
*chan
, int *val
)
242 ktime_t next_measurement
;
244 mutex_lock(&chan
->lock
);
246 next_measurement
= ktime_add(chan
->last_measurement
,
247 chan
->sampling_rate
);
248 delta
= ktime_us_delta(next_measurement
, ktime_get());
250 usleep_range(delta
, delta
+ 500);
251 chan
->last_measurement
= ktime_get();
253 mutex_unlock(&chan
->lock
);
255 ret
= i2c_smbus_read_word_data(data
->client
, chan
->reg
);
264 static int vcnl4000_measure_light(struct vcnl4000_data
*data
, int *val
)
266 return vcnl4000_measure(data
,
267 VCNL4000_AL_OD
, VCNL4000_AL_RDY
,
268 VCNL4000_AL_RESULT_HI
, val
);
271 static int vcnl4200_measure_light(struct vcnl4000_data
*data
, int *val
)
273 return vcnl4200_measure(data
, &data
->vcnl4200_al
, val
);
276 static int vcnl4000_measure_proximity(struct vcnl4000_data
*data
, int *val
)
278 return vcnl4000_measure(data
,
279 VCNL4000_PS_OD
, VCNL4000_PS_RDY
,
280 VCNL4000_PS_RESULT_HI
, val
);
283 static int vcnl4200_measure_proximity(struct vcnl4000_data
*data
, int *val
)
285 return vcnl4200_measure(data
, &data
->vcnl4200_ps
, val
);
288 static const struct vcnl4000_chip_spec vcnl4000_chip_spec_cfg
[] = {
291 .init
= vcnl4000_init
,
292 .measure_light
= vcnl4000_measure_light
,
293 .measure_proximity
= vcnl4000_measure_proximity
,
296 .prod
= "VCNL4010/4020",
297 .init
= vcnl4000_init
,
298 .measure_light
= vcnl4000_measure_light
,
299 .measure_proximity
= vcnl4000_measure_proximity
,
303 .init
= vcnl4200_init
,
304 .measure_light
= vcnl4200_measure_light
,
305 .measure_proximity
= vcnl4200_measure_proximity
,
309 .init
= vcnl4200_init
,
310 .measure_light
= vcnl4200_measure_light
,
311 .measure_proximity
= vcnl4200_measure_proximity
,
315 static const struct iio_chan_spec vcnl4000_channels
[] = {
318 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
319 BIT(IIO_CHAN_INFO_SCALE
),
321 .type
= IIO_PROXIMITY
,
322 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
326 static int vcnl4000_read_raw(struct iio_dev
*indio_dev
,
327 struct iio_chan_spec
const *chan
,
328 int *val
, int *val2
, long mask
)
331 struct vcnl4000_data
*data
= iio_priv(indio_dev
);
334 case IIO_CHAN_INFO_RAW
:
335 switch (chan
->type
) {
337 ret
= data
->chip_spec
->measure_light(data
, val
);
342 ret
= data
->chip_spec
->measure_proximity(data
, val
);
349 case IIO_CHAN_INFO_SCALE
:
350 if (chan
->type
!= IIO_LIGHT
)
354 *val2
= data
->al_scale
;
355 return IIO_VAL_INT_PLUS_MICRO
;
361 static const struct iio_info vcnl4000_info
= {
362 .read_raw
= vcnl4000_read_raw
,
365 static int vcnl4000_probe(struct i2c_client
*client
,
366 const struct i2c_device_id
*id
)
368 struct vcnl4000_data
*data
;
369 struct iio_dev
*indio_dev
;
372 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
376 data
= iio_priv(indio_dev
);
377 i2c_set_clientdata(client
, indio_dev
);
378 data
->client
= client
;
379 data
->id
= id
->driver_data
;
380 data
->chip_spec
= &vcnl4000_chip_spec_cfg
[data
->id
];
382 ret
= data
->chip_spec
->init(data
);
386 dev_dbg(&client
->dev
, "%s Ambient light/proximity sensor, Rev: %02x\n",
387 data
->chip_spec
->prod
, data
->rev
);
389 indio_dev
->dev
.parent
= &client
->dev
;
390 indio_dev
->info
= &vcnl4000_info
;
391 indio_dev
->channels
= vcnl4000_channels
;
392 indio_dev
->num_channels
= ARRAY_SIZE(vcnl4000_channels
);
393 indio_dev
->name
= VCNL4000_DRV_NAME
;
394 indio_dev
->modes
= INDIO_DIRECT_MODE
;
396 return devm_iio_device_register(&client
->dev
, indio_dev
);
399 static const struct of_device_id vcnl_4000_of_match
[] = {
401 .compatible
= "vishay,vcnl4000",
402 .data
= (void *)VCNL4000
,
405 .compatible
= "vishay,vcnl4010",
406 .data
= (void *)VCNL4010
,
409 .compatible
= "vishay,vcnl4020",
410 .data
= (void *)VCNL4010
,
413 .compatible
= "vishay,vcnl4040",
414 .data
= (void *)VCNL4040
,
417 .compatible
= "vishay,vcnl4200",
418 .data
= (void *)VCNL4200
,
422 MODULE_DEVICE_TABLE(of
, vcnl_4000_of_match
);
424 static struct i2c_driver vcnl4000_driver
= {
426 .name
= VCNL4000_DRV_NAME
,
427 .of_match_table
= vcnl_4000_of_match
,
429 .probe
= vcnl4000_probe
,
430 .id_table
= vcnl4000_id
,
433 module_i2c_driver(vcnl4000_driver
);
435 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
436 MODULE_DESCRIPTION("Vishay VCNL4000 proximity/ambient light sensor driver");
437 MODULE_LICENSE("GPL");