1 /*
2 * vcnl4000.c - Support for Vishay VCNL4000 combined ambient light and
3 * proximity sensor
4 *
5 * Copyright 2012 Peter Meerwald <pmeerw@pmeerw.net>
6 *
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.
10 *
11 * IIO driver for VCNL4000 (7-bit I2C slave address 0x13)
12 *
13 * TODO:
14 * allow to adjust IR current
15 * proximity threshold and event handling
16 */
17
18 #include <linux/module.h>
19 #include <linux/i2c.h>
20 #include <linux/err.h>
21 #include <linux/delay.h>
22
23 #include <linux/iio/iio.h>
24 #include <linux/iio/sysfs.h>
25
26 #define VCNL4000_DRV_NAME "vcnl4000"
27
28 #define VCNL4000_COMMAND 0x80 /* Command register */
29 #define VCNL4000_PROD_REV 0x81 /* Product ID and Revision ID */
30 #define VCNL4000_LED_CURRENT 0x83 /* IR LED current for proximity mode */
31 #define VCNL4000_AL_PARAM 0x84 /* Ambient light parameter register */
32 #define VCNL4000_AL_RESULT_HI 0x85 /* Ambient light result register, MSB */
33 #define VCNL4000_AL_RESULT_LO 0x86 /* Ambient light result register, LSB */
34 #define VCNL4000_PS_RESULT_HI 0x87 /* Proximity result register, MSB */
35 #define VCNL4000_PS_RESULT_LO 0x88 /* Proximity result register, LSB */
36 #define VCNL4000_PS_MEAS_FREQ 0x89 /* Proximity test signal frequency */
37 #define VCNL4000_PS_MOD_ADJ 0x8a /* Proximity modulator timing adjustment */
38
39 /* Bit masks for COMMAND register */
40 #define VCNL4000_AL_RDY 0x40 /* ALS data ready? */
41 #define VCNL4000_PS_RDY 0x20 /* proximity data ready? */
42 #define VCNL4000_AL_OD 0x10 /* start on-demand ALS measurement */
43 #define VCNL4000_PS_OD 0x08 /* start on-demand proximity measurement */
44
45 struct vcnl4000_data {
46 struct i2c_client *client;
47 };
48
49 static const struct i2c_device_id vcnl4000_id[] = {
50 { "vcnl4000", 0 },
51 { }
52 };
53 MODULE_DEVICE_TABLE(i2c, vcnl4000_id);
54
vcnl4000_measure(struct vcnl4000_data * data,u8 req_mask,u8 rdy_mask,u8 data_reg,int * val)55 static int vcnl4000_measure(struct vcnl4000_data *data, u8 req_mask,
56 u8 rdy_mask, u8 data_reg, int *val)
57 {
58 int tries = 20;
59 int ret;
60
61 ret = i2c_smbus_write_byte_data(data->client, VCNL4000_COMMAND,
62 req_mask);
63 if (ret < 0)
64 return ret;
65
66 /* wait for data to become ready */
67 while (tries--) {
68 ret = i2c_smbus_read_byte_data(data->client, VCNL4000_COMMAND);
69 if (ret < 0)
70 return ret;
71 if (ret & rdy_mask)
72 break;
73 msleep(20); /* measurement takes up to 100 ms */
74 }
75
76 if (tries < 0) {
77 dev_err(&data->client->dev,
78 "vcnl4000_measure() failed, data not ready\n");
79 return -EIO;
80 }
81
82 ret = i2c_smbus_read_word_swapped(data->client, data_reg);
83 if (ret < 0)
84 return ret;
85
86 *val = ret;
87
88 return 0;
89 }
90
91 static const struct iio_chan_spec vcnl4000_channels[] = {
92 {
93 .type = IIO_LIGHT,
94 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
95 BIT(IIO_CHAN_INFO_SCALE),
96 }, {
97 .type = IIO_PROXIMITY,
98 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
99 }
100 };
101
vcnl4000_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)102 static int vcnl4000_read_raw(struct iio_dev *indio_dev,
103 struct iio_chan_spec const *chan,
104 int *val, int *val2, long mask)
105 {
106 int ret = -EINVAL;
107 struct vcnl4000_data *data = iio_priv(indio_dev);
108
109 switch (mask) {
110 case IIO_CHAN_INFO_RAW:
111 switch (chan->type) {
112 case IIO_LIGHT:
113 ret = vcnl4000_measure(data,
114 VCNL4000_AL_OD, VCNL4000_AL_RDY,
115 VCNL4000_AL_RESULT_HI, val);
116 if (ret < 0)
117 return ret;
118 ret = IIO_VAL_INT;
119 break;
120 case IIO_PROXIMITY:
121 ret = vcnl4000_measure(data,
122 VCNL4000_PS_OD, VCNL4000_PS_RDY,
123 VCNL4000_PS_RESULT_HI, val);
124 if (ret < 0)
125 return ret;
126 ret = IIO_VAL_INT;
127 break;
128 default:
129 break;
130 }
131 break;
132 case IIO_CHAN_INFO_SCALE:
133 if (chan->type == IIO_LIGHT) {
134 *val = 0;
135 *val2 = 250000;
136 ret = IIO_VAL_INT_PLUS_MICRO;
137 }
138 break;
139 default:
140 break;
141 }
142
143 return ret;
144 }
145
146 static const struct iio_info vcnl4000_info = {
147 .read_raw = vcnl4000_read_raw,
148 .driver_module = THIS_MODULE,
149 };
150
vcnl4000_probe(struct i2c_client * client,const struct i2c_device_id * id)151 static int vcnl4000_probe(struct i2c_client *client,
152 const struct i2c_device_id *id)
153 {
154 struct vcnl4000_data *data;
155 struct iio_dev *indio_dev;
156 int ret;
157
158 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
159 if (!indio_dev)
160 return -ENOMEM;
161
162 data = iio_priv(indio_dev);
163 i2c_set_clientdata(client, indio_dev);
164 data->client = client;
165
166 ret = i2c_smbus_read_byte_data(data->client, VCNL4000_PROD_REV);
167 if (ret < 0)
168 return ret;
169
170 dev_info(&client->dev, "VCNL4000 Ambient light/proximity sensor, Prod %02x, Rev: %02x\n",
171 ret >> 4, ret & 0xf);
172
173 indio_dev->dev.parent = &client->dev;
174 indio_dev->info = &vcnl4000_info;
175 indio_dev->channels = vcnl4000_channels;
176 indio_dev->num_channels = ARRAY_SIZE(vcnl4000_channels);
177 indio_dev->name = VCNL4000_DRV_NAME;
178 indio_dev->modes = INDIO_DIRECT_MODE;
179
180 return devm_iio_device_register(&client->dev, indio_dev);
181 }
182
183 static struct i2c_driver vcnl4000_driver = {
184 .driver = {
185 .name = VCNL4000_DRV_NAME,
186 },
187 .probe = vcnl4000_probe,
188 .id_table = vcnl4000_id,
189 };
190
191 module_i2c_driver(vcnl4000_driver);
192
193 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
194 MODULE_DESCRIPTION("Vishay VCNL4000 proximity/ambient light sensor driver");
195 MODULE_LICENSE("GPL");
196