Linux 4.19.133
[linux/fpc-iii.git] / drivers / iio / light / vcnl4000.c
bloba3fdffa4ef847336c1a7e07a404cbf80a3ed7bf0
1 /*
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.
11 * IIO driver for:
12 * VCNL4000/10/20 (7-bit I2C slave address 0x13)
13 * VCNL4200 (7-bit I2C slave address 0x51)
15 * TODO:
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 {
59 VCNL4000,
60 VCNL4010,
61 VCNL4200,
64 struct vcnl4200_channel {
65 u8 reg;
66 ktime_t last_measurement;
67 ktime_t sampling_rate;
68 struct mutex lock;
71 struct vcnl4000_data {
72 struct i2c_client *client;
73 enum vcnl4000_device_ids id;
74 int rev;
75 int al_scale;
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 {
83 const char *prod;
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 },
94 { }
96 MODULE_DEVICE_TABLE(i2c, vcnl4000_id);
98 static int vcnl4000_init(struct vcnl4000_data *data)
100 int ret, prod_id;
102 ret = i2c_smbus_read_byte_data(data->client, VCNL4000_PROD_REV);
103 if (ret < 0)
104 return ret;
106 prod_id = ret >> 4;
107 switch (prod_id) {
108 case VCNL4000_PROD_ID:
109 if (data->id != VCNL4000)
110 dev_warn(&data->client->dev,
111 "wrong device id, use vcnl4000");
112 break;
113 case VCNL4010_PROD_ID:
114 if (data->id != VCNL4010)
115 dev_warn(&data->client->dev,
116 "wrong device id, use vcnl4010/4020");
117 break;
118 default:
119 return -ENODEV;
122 data->rev = ret & 0xf;
123 data->al_scale = 250000;
124 mutex_init(&data->vcnl4000_lock);
126 return 0;
129 static int vcnl4200_init(struct vcnl4000_data *data)
131 int ret;
133 ret = i2c_smbus_read_word_data(data->client, VCNL4200_DEV_ID);
134 if (ret < 0)
135 return ret;
137 if ((ret & 0xff) != VCNL4200_PROD_ID)
138 return -ENODEV;
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);
144 if (ret < 0)
145 return ret;
146 ret = i2c_smbus_write_byte_data(data->client, VCNL4200_PS_CONF1, 0x00);
147 if (ret < 0)
148 return ret;
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);
162 return 0;
165 static int vcnl4000_measure(struct vcnl4000_data *data, u8 req_mask,
166 u8 rdy_mask, u8 data_reg, int *val)
168 int tries = 20;
169 int ret;
171 mutex_lock(&data->vcnl4000_lock);
173 ret = i2c_smbus_write_byte_data(data->client, VCNL4000_COMMAND,
174 req_mask);
175 if (ret < 0)
176 goto fail;
178 /* wait for data to become ready */
179 while (tries--) {
180 ret = i2c_smbus_read_byte_data(data->client, VCNL4000_COMMAND);
181 if (ret < 0)
182 goto fail;
183 if (ret & rdy_mask)
184 break;
185 msleep(20); /* measurement takes up to 100 ms */
188 if (tries < 0) {
189 dev_err(&data->client->dev,
190 "vcnl4000_measure() failed, data not ready\n");
191 ret = -EIO;
192 goto fail;
195 ret = i2c_smbus_read_word_swapped(data->client, data_reg);
196 if (ret < 0)
197 goto fail;
199 mutex_unlock(&data->vcnl4000_lock);
200 *val = ret;
202 return 0;
204 fail:
205 mutex_unlock(&data->vcnl4000_lock);
206 return ret;
209 static int vcnl4200_measure(struct vcnl4000_data *data,
210 struct vcnl4200_channel *chan, int *val)
212 int ret;
213 s64 delta;
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());
221 if (delta > 0)
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);
228 if (ret < 0)
229 return ret;
231 *val = ret;
233 return 0;
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[] = {
261 [VCNL4000] = {
262 .prod = "VCNL4000",
263 .init = vcnl4000_init,
264 .measure_light = vcnl4000_measure_light,
265 .measure_proximity = vcnl4000_measure_proximity,
267 [VCNL4010] = {
268 .prod = "VCNL4010/4020",
269 .init = vcnl4000_init,
270 .measure_light = vcnl4000_measure_light,
271 .measure_proximity = vcnl4000_measure_proximity,
273 [VCNL4200] = {
274 .prod = "VCNL4200",
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[] = {
283 .type = IIO_LIGHT,
284 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
285 BIT(IIO_CHAN_INFO_SCALE),
286 }, {
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)
296 int ret;
297 struct vcnl4000_data *data = iio_priv(indio_dev);
299 switch (mask) {
300 case IIO_CHAN_INFO_RAW:
301 switch (chan->type) {
302 case IIO_LIGHT:
303 ret = data->chip_spec->measure_light(data, val);
304 if (ret < 0)
305 return ret;
306 return IIO_VAL_INT;
307 case IIO_PROXIMITY:
308 ret = data->chip_spec->measure_proximity(data, val);
309 if (ret < 0)
310 return ret;
311 return IIO_VAL_INT;
312 default:
313 return -EINVAL;
315 case IIO_CHAN_INFO_SCALE:
316 if (chan->type != IIO_LIGHT)
317 return -EINVAL;
319 *val = 0;
320 *val2 = data->al_scale;
321 return IIO_VAL_INT_PLUS_MICRO;
322 default:
323 return -EINVAL;
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;
336 int ret;
338 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
339 if (!indio_dev)
340 return -ENOMEM;
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);
349 if (ret < 0)
350 return ret;
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 = {
366 .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");