2 * vcnl4000.c - Support for Vishay VCNL4000/4010/4020/4200 combined ambient
3 * light and proximity sensor
5 * Copyright 2012 Peter Meerwald <pmeerw@pmeerw.net>
7 * This file is subject to the terms and conditions of version 2 of
8 * the GNU General Public License. See the file COPYING in the main
9 * directory of this archive for more details.
12 * VCNL4000/10/20 (7-bit I2C slave address 0x13)
13 * VCNL4200 (7-bit I2C slave address 0x51)
16 * allow to adjust IR current
17 * proximity threshold and event handling
18 * periodic ALS/proximity measurement (VCNL4010/20)
19 * interrupts (VCNL4010/20, VCNL4200)
22 #include <linux/module.h>
23 #include <linux/i2c.h>
24 #include <linux/err.h>
25 #include <linux/delay.h>
27 #include <linux/iio/iio.h>
28 #include <linux/iio/sysfs.h>
30 #define VCNL4000_DRV_NAME "vcnl4000"
31 #define VCNL4000_PROD_ID 0x01
32 #define VCNL4010_PROD_ID 0x02 /* for VCNL4020, VCNL4010 */
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 /* Bit masks for COMMAND register */
53 #define VCNL4000_AL_RDY BIT(6) /* ALS data ready? */
54 #define VCNL4000_PS_RDY BIT(5) /* proximity data ready? */
55 #define VCNL4000_AL_OD BIT(4) /* start on-demand ALS measurement */
56 #define VCNL4000_PS_OD BIT(3) /* start on-demand proximity measurement */
58 enum vcnl4000_device_ids
{
64 struct vcnl4200_channel
{
66 ktime_t last_measurement
;
67 ktime_t sampling_rate
;
71 struct vcnl4000_data
{
72 struct i2c_client
*client
;
73 enum vcnl4000_device_ids id
;
76 const struct vcnl4000_chip_spec
*chip_spec
;
77 struct mutex vcnl4000_lock
;
78 struct vcnl4200_channel vcnl4200_al
;
79 struct vcnl4200_channel vcnl4200_ps
;
82 struct vcnl4000_chip_spec
{
84 int (*init
)(struct vcnl4000_data
*data
);
85 int (*measure_light
)(struct vcnl4000_data
*data
, int *val
);
86 int (*measure_proximity
)(struct vcnl4000_data
*data
, int *val
);
89 static const struct i2c_device_id vcnl4000_id
[] = {
90 { "vcnl4000", VCNL4000
},
91 { "vcnl4010", VCNL4010
},
92 { "vcnl4020", VCNL4010
},
93 { "vcnl4200", VCNL4200
},
96 MODULE_DEVICE_TABLE(i2c
, vcnl4000_id
);
98 static int vcnl4000_init(struct vcnl4000_data
*data
)
102 ret
= i2c_smbus_read_byte_data(data
->client
, VCNL4000_PROD_REV
);
108 case VCNL4000_PROD_ID
:
109 if (data
->id
!= VCNL4000
)
110 dev_warn(&data
->client
->dev
,
111 "wrong device id, use vcnl4000");
113 case VCNL4010_PROD_ID
:
114 if (data
->id
!= VCNL4010
)
115 dev_warn(&data
->client
->dev
,
116 "wrong device id, use vcnl4010/4020");
122 data
->rev
= ret
& 0xf;
123 data
->al_scale
= 250000;
124 mutex_init(&data
->vcnl4000_lock
);
129 static int vcnl4200_init(struct vcnl4000_data
*data
)
133 ret
= i2c_smbus_read_word_data(data
->client
, VCNL4200_DEV_ID
);
137 if ((ret
& 0xff) != VCNL4200_PROD_ID
)
140 data
->rev
= (ret
>> 8) & 0xf;
142 /* Set defaults and enable both channels */
143 ret
= i2c_smbus_write_byte_data(data
->client
, VCNL4200_AL_CONF
, 0x00);
146 ret
= i2c_smbus_write_byte_data(data
->client
, VCNL4200_PS_CONF1
, 0x00);
150 data
->al_scale
= 24000;
151 data
->vcnl4200_al
.reg
= VCNL4200_AL_DATA
;
152 data
->vcnl4200_ps
.reg
= VCNL4200_PS_DATA
;
153 /* Default wait time is 50ms, add 20% tolerance. */
154 data
->vcnl4200_al
.sampling_rate
= ktime_set(0, 60000 * 1000);
155 /* Default wait time is 4.8ms, add 20% tolerance. */
156 data
->vcnl4200_ps
.sampling_rate
= ktime_set(0, 5760 * 1000);
157 data
->vcnl4200_al
.last_measurement
= ktime_set(0, 0);
158 data
->vcnl4200_ps
.last_measurement
= ktime_set(0, 0);
159 mutex_init(&data
->vcnl4200_al
.lock
);
160 mutex_init(&data
->vcnl4200_ps
.lock
);
165 static int vcnl4000_measure(struct vcnl4000_data
*data
, u8 req_mask
,
166 u8 rdy_mask
, u8 data_reg
, int *val
)
171 mutex_lock(&data
->vcnl4000_lock
);
173 ret
= i2c_smbus_write_byte_data(data
->client
, VCNL4000_COMMAND
,
178 /* wait for data to become ready */
180 ret
= i2c_smbus_read_byte_data(data
->client
, VCNL4000_COMMAND
);
185 msleep(20); /* measurement takes up to 100 ms */
189 dev_err(&data
->client
->dev
,
190 "vcnl4000_measure() failed, data not ready\n");
195 ret
= i2c_smbus_read_word_swapped(data
->client
, data_reg
);
199 mutex_unlock(&data
->vcnl4000_lock
);
205 mutex_unlock(&data
->vcnl4000_lock
);
209 static int vcnl4200_measure(struct vcnl4000_data
*data
,
210 struct vcnl4200_channel
*chan
, int *val
)
214 ktime_t next_measurement
;
216 mutex_lock(&chan
->lock
);
218 next_measurement
= ktime_add(chan
->last_measurement
,
219 chan
->sampling_rate
);
220 delta
= ktime_us_delta(next_measurement
, ktime_get());
222 usleep_range(delta
, delta
+ 500);
223 chan
->last_measurement
= ktime_get();
225 mutex_unlock(&chan
->lock
);
227 ret
= i2c_smbus_read_word_data(data
->client
, chan
->reg
);
236 static int vcnl4000_measure_light(struct vcnl4000_data
*data
, int *val
)
238 return vcnl4000_measure(data
,
239 VCNL4000_AL_OD
, VCNL4000_AL_RDY
,
240 VCNL4000_AL_RESULT_HI
, val
);
243 static int vcnl4200_measure_light(struct vcnl4000_data
*data
, int *val
)
245 return vcnl4200_measure(data
, &data
->vcnl4200_al
, val
);
248 static int vcnl4000_measure_proximity(struct vcnl4000_data
*data
, int *val
)
250 return vcnl4000_measure(data
,
251 VCNL4000_PS_OD
, VCNL4000_PS_RDY
,
252 VCNL4000_PS_RESULT_HI
, val
);
255 static int vcnl4200_measure_proximity(struct vcnl4000_data
*data
, int *val
)
257 return vcnl4200_measure(data
, &data
->vcnl4200_ps
, val
);
260 static const struct vcnl4000_chip_spec vcnl4000_chip_spec_cfg
[] = {
263 .init
= vcnl4000_init
,
264 .measure_light
= vcnl4000_measure_light
,
265 .measure_proximity
= vcnl4000_measure_proximity
,
268 .prod
= "VCNL4010/4020",
269 .init
= vcnl4000_init
,
270 .measure_light
= vcnl4000_measure_light
,
271 .measure_proximity
= vcnl4000_measure_proximity
,
275 .init
= vcnl4200_init
,
276 .measure_light
= vcnl4200_measure_light
,
277 .measure_proximity
= vcnl4200_measure_proximity
,
281 static const struct iio_chan_spec vcnl4000_channels
[] = {
284 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
285 BIT(IIO_CHAN_INFO_SCALE
),
287 .type
= IIO_PROXIMITY
,
288 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
292 static int vcnl4000_read_raw(struct iio_dev
*indio_dev
,
293 struct iio_chan_spec
const *chan
,
294 int *val
, int *val2
, long mask
)
297 struct vcnl4000_data
*data
= iio_priv(indio_dev
);
300 case IIO_CHAN_INFO_RAW
:
301 switch (chan
->type
) {
303 ret
= data
->chip_spec
->measure_light(data
, val
);
308 ret
= data
->chip_spec
->measure_proximity(data
, val
);
315 case IIO_CHAN_INFO_SCALE
:
316 if (chan
->type
!= IIO_LIGHT
)
320 *val2
= data
->al_scale
;
321 return IIO_VAL_INT_PLUS_MICRO
;
327 static const struct iio_info vcnl4000_info
= {
328 .read_raw
= vcnl4000_read_raw
,
331 static int vcnl4000_probe(struct i2c_client
*client
,
332 const struct i2c_device_id
*id
)
334 struct vcnl4000_data
*data
;
335 struct iio_dev
*indio_dev
;
338 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
342 data
= iio_priv(indio_dev
);
343 i2c_set_clientdata(client
, indio_dev
);
344 data
->client
= client
;
345 data
->id
= id
->driver_data
;
346 data
->chip_spec
= &vcnl4000_chip_spec_cfg
[data
->id
];
348 ret
= data
->chip_spec
->init(data
);
352 dev_dbg(&client
->dev
, "%s Ambient light/proximity sensor, Rev: %02x\n",
353 data
->chip_spec
->prod
, data
->rev
);
355 indio_dev
->dev
.parent
= &client
->dev
;
356 indio_dev
->info
= &vcnl4000_info
;
357 indio_dev
->channels
= vcnl4000_channels
;
358 indio_dev
->num_channels
= ARRAY_SIZE(vcnl4000_channels
);
359 indio_dev
->name
= VCNL4000_DRV_NAME
;
360 indio_dev
->modes
= INDIO_DIRECT_MODE
;
362 return devm_iio_device_register(&client
->dev
, indio_dev
);
365 static struct i2c_driver vcnl4000_driver
= {
367 .name
= VCNL4000_DRV_NAME
,
369 .probe
= vcnl4000_probe
,
370 .id_table
= vcnl4000_id
,
373 module_i2c_driver(vcnl4000_driver
);
375 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
376 MODULE_DESCRIPTION("Vishay VCNL4000 proximity/ambient light sensor driver");
377 MODULE_LICENSE("GPL");