2 * vcnl4000.c - Support for Vishay VCNL4000/4010/4020 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.
11 * IIO driver for VCNL4000 (7-bit I2C slave address 0x13)
14 * allow to adjust IR current
15 * proximity threshold and event handling
16 * periodic ALS/proximity measurement (VCNL4010/20)
17 * interrupts (VCNL4010/20)
20 #include <linux/module.h>
21 #include <linux/i2c.h>
22 #include <linux/err.h>
23 #include <linux/delay.h>
25 #include <linux/iio/iio.h>
26 #include <linux/iio/sysfs.h>
28 #define VCNL4000_DRV_NAME "vcnl4000"
29 #define VCNL4000_ID 0x01
30 #define VCNL4010_ID 0x02 /* for VCNL4020, VCNL4010 */
32 #define VCNL4000_COMMAND 0x80 /* Command register */
33 #define VCNL4000_PROD_REV 0x81 /* Product ID and Revision ID */
34 #define VCNL4000_LED_CURRENT 0x83 /* IR LED current for proximity mode */
35 #define VCNL4000_AL_PARAM 0x84 /* Ambient light parameter register */
36 #define VCNL4000_AL_RESULT_HI 0x85 /* Ambient light result register, MSB */
37 #define VCNL4000_AL_RESULT_LO 0x86 /* Ambient light result register, LSB */
38 #define VCNL4000_PS_RESULT_HI 0x87 /* Proximity result register, MSB */
39 #define VCNL4000_PS_RESULT_LO 0x88 /* Proximity result register, LSB */
40 #define VCNL4000_PS_MEAS_FREQ 0x89 /* Proximity test signal frequency */
41 #define VCNL4000_PS_MOD_ADJ 0x8a /* Proximity modulator timing adjustment */
43 /* Bit masks for COMMAND register */
44 #define VCNL4000_AL_RDY BIT(6) /* ALS data ready? */
45 #define VCNL4000_PS_RDY BIT(5) /* proximity data ready? */
46 #define VCNL4000_AL_OD BIT(4) /* start on-demand ALS measurement */
47 #define VCNL4000_PS_OD BIT(3) /* start on-demand proximity measurement */
49 struct vcnl4000_data
{
50 struct i2c_client
*client
;
54 static const struct i2c_device_id vcnl4000_id
[] = {
58 MODULE_DEVICE_TABLE(i2c
, vcnl4000_id
);
60 static int vcnl4000_measure(struct vcnl4000_data
*data
, u8 req_mask
,
61 u8 rdy_mask
, u8 data_reg
, int *val
)
67 mutex_lock(&data
->lock
);
69 ret
= i2c_smbus_write_byte_data(data
->client
, VCNL4000_COMMAND
,
74 /* wait for data to become ready */
76 ret
= i2c_smbus_read_byte_data(data
->client
, VCNL4000_COMMAND
);
81 msleep(20); /* measurement takes up to 100 ms */
85 dev_err(&data
->client
->dev
,
86 "vcnl4000_measure() failed, data not ready\n");
91 ret
= i2c_smbus_read_i2c_block_data(data
->client
,
92 data_reg
, sizeof(buf
), (u8
*) &buf
);
96 mutex_unlock(&data
->lock
);
97 *val
= be16_to_cpu(buf
);
102 mutex_unlock(&data
->lock
);
106 static const struct iio_chan_spec vcnl4000_channels
[] = {
109 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
110 BIT(IIO_CHAN_INFO_SCALE
),
112 .type
= IIO_PROXIMITY
,
113 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
117 static int vcnl4000_read_raw(struct iio_dev
*indio_dev
,
118 struct iio_chan_spec
const *chan
,
119 int *val
, int *val2
, long mask
)
122 struct vcnl4000_data
*data
= iio_priv(indio_dev
);
125 case IIO_CHAN_INFO_RAW
:
126 switch (chan
->type
) {
128 ret
= vcnl4000_measure(data
,
129 VCNL4000_AL_OD
, VCNL4000_AL_RDY
,
130 VCNL4000_AL_RESULT_HI
, val
);
135 ret
= vcnl4000_measure(data
,
136 VCNL4000_PS_OD
, VCNL4000_PS_RDY
,
137 VCNL4000_PS_RESULT_HI
, val
);
144 case IIO_CHAN_INFO_SCALE
:
145 if (chan
->type
!= IIO_LIGHT
)
150 return IIO_VAL_INT_PLUS_MICRO
;
156 static const struct iio_info vcnl4000_info
= {
157 .read_raw
= vcnl4000_read_raw
,
158 .driver_module
= THIS_MODULE
,
161 static int vcnl4000_probe(struct i2c_client
*client
,
162 const struct i2c_device_id
*id
)
164 struct vcnl4000_data
*data
;
165 struct iio_dev
*indio_dev
;
168 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
172 data
= iio_priv(indio_dev
);
173 i2c_set_clientdata(client
, indio_dev
);
174 data
->client
= client
;
175 mutex_init(&data
->lock
);
177 ret
= i2c_smbus_read_byte_data(data
->client
, VCNL4000_PROD_REV
);
182 if (prod_id
!= VCNL4010_ID
&& prod_id
!= VCNL4000_ID
)
185 dev_dbg(&client
->dev
, "%s Ambient light/proximity sensor, Rev: %02x\n",
186 (prod_id
== VCNL4010_ID
) ? "VCNL4010/4020" : "VCNL4000",
189 indio_dev
->dev
.parent
= &client
->dev
;
190 indio_dev
->info
= &vcnl4000_info
;
191 indio_dev
->channels
= vcnl4000_channels
;
192 indio_dev
->num_channels
= ARRAY_SIZE(vcnl4000_channels
);
193 indio_dev
->name
= VCNL4000_DRV_NAME
;
194 indio_dev
->modes
= INDIO_DIRECT_MODE
;
196 return devm_iio_device_register(&client
->dev
, indio_dev
);
199 static struct i2c_driver vcnl4000_driver
= {
201 .name
= VCNL4000_DRV_NAME
,
203 .probe
= vcnl4000_probe
,
204 .id_table
= vcnl4000_id
,
207 module_i2c_driver(vcnl4000_driver
);
209 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
210 MODULE_DESCRIPTION("Vishay VCNL4000 proximity/ambient light sensor driver");
211 MODULE_LICENSE("GPL");