1 // SPDX-License-Identifier: GPL-2.0-only
3 * Support for Vishay VCNL3020 proximity sensor on i2c bus.
4 * Based on Vishay VCNL4000 driver code.
7 #include <linux/module.h>
10 #include <linux/delay.h>
11 #include <linux/regmap.h>
12 #include <linux/interrupt.h>
14 #include <linux/iio/iio.h>
15 #include <linux/iio/events.h>
17 #define VCNL3020_PROD_ID 0x21
19 #define VCNL_COMMAND 0x80 /* Command register */
20 #define VCNL_PROD_REV 0x81 /* Product ID and Revision ID */
21 #define VCNL_PROXIMITY_RATE 0x82 /* Rate of Proximity Measurement */
22 #define VCNL_LED_CURRENT 0x83 /* IR LED current for proximity mode */
23 #define VCNL_PS_RESULT_HI 0x87 /* Proximity result register, MSB */
24 #define VCNL_PS_RESULT_LO 0x88 /* Proximity result register, LSB */
25 #define VCNL_PS_ICR 0x89 /* Interrupt Control Register */
26 #define VCNL_PS_LO_THR_HI 0x8a /* High byte of low threshold value */
27 #define VCNL_PS_LO_THR_LO 0x8b /* Low byte of low threshold value */
28 #define VCNL_PS_HI_THR_HI 0x8c /* High byte of high threshold value */
29 #define VCNL_PS_HI_THR_LO 0x8d /* Low byte of high threshold value */
30 #define VCNL_ISR 0x8e /* Interrupt Status Register */
31 #define VCNL_PS_MOD_ADJ 0x8f /* Proximity Modulator Timing Adjustment */
33 /* Bit masks for COMMAND register */
34 #define VCNL_PS_RDY BIT(5) /* proximity data ready? */
35 #define VCNL_PS_OD BIT(3) /* start on-demand proximity
39 /* Enables periodic proximity measurement */
40 #define VCNL_PS_EN BIT(1)
42 /* Enables state machine and LP oscillator for self timed measurements */
43 #define VCNL_PS_SELFTIMED_EN BIT(0)
45 /* Bit masks for ICR */
47 /* Enable interrupts on low or high thresholds */
48 #define VCNL_ICR_THRES_EN BIT(1)
50 /* Bit masks for ISR */
51 #define VCNL_INT_TH_HI BIT(0) /* High threshold hit */
52 #define VCNL_INT_TH_LOW BIT(1) /* Low threshold hit */
54 #define VCNL_ON_DEMAND_TIMEOUT_US 100000
55 #define VCNL_POLL_US 20000
57 static const int vcnl3020_prox_sampling_frequency[][2] = {
69 * struct vcnl3020_data - vcnl3020 specific data.
70 * @regmap: device register map.
71 * @dev: vcnl3020 device.
73 * @lock: lock for protecting access to device hardware registers.
74 * @buf: __be16 buffer.
76 struct vcnl3020_data {
77 struct regmap *regmap;
85 * struct vcnl3020_property - vcnl3020 property.
86 * @name: property name.
87 * @reg: i2c register offset.
88 * @conversion_func: conversion function.
90 struct vcnl3020_property {
93 u32 (*conversion_func)(u32 *val);
96 static u32 microamp_to_reg(u32 *val)
99 * An example of conversion from uA to reg val:
100 * 200000 uA == 200 mA == 20
102 return *val /= 10000;
105 static struct vcnl3020_property vcnl3020_led_current_property = {
106 .name = "vishay,led-current-microamp",
107 .reg = VCNL_LED_CURRENT,
108 .conversion_func = microamp_to_reg,
111 static int vcnl3020_get_and_apply_property(struct vcnl3020_data *data,
112 struct vcnl3020_property prop)
117 rc = device_property_read_u32(data->dev, prop.name, &val);
121 if (prop.conversion_func)
122 prop.conversion_func(&val);
124 rc = regmap_write(data->regmap, prop.reg, val);
126 dev_err(data->dev, "Error (%d) setting property (%s)\n",
133 static int vcnl3020_init(struct vcnl3020_data *data)
138 rc = regmap_read(data->regmap, VCNL_PROD_REV, ®);
141 "Error (%d) reading product revision\n", rc);
145 if (reg != VCNL3020_PROD_ID) {
147 "Product id (%x) did not match vcnl3020 (%x)\n", reg,
153 mutex_init(&data->lock);
155 return vcnl3020_get_and_apply_property(data,
156 vcnl3020_led_current_property);
159 static bool vcnl3020_is_in_periodic_mode(struct vcnl3020_data *data)
164 rc = regmap_read(data->regmap, VCNL_COMMAND, &cmd);
167 "Error (%d) reading command register\n", rc);
171 return !!(cmd & VCNL_PS_SELFTIMED_EN);
174 static int vcnl3020_measure_proximity(struct vcnl3020_data *data, int *val)
179 mutex_lock(&data->lock);
181 /* Protect against event capture. */
182 if (vcnl3020_is_in_periodic_mode(data)) {
187 rc = regmap_write(data->regmap, VCNL_COMMAND, VCNL_PS_OD);
191 /* wait for data to become ready */
192 rc = regmap_read_poll_timeout(data->regmap, VCNL_COMMAND, reg,
193 reg & VCNL_PS_RDY, VCNL_POLL_US,
194 VCNL_ON_DEMAND_TIMEOUT_US);
197 "Error (%d) reading vcnl3020 command register\n", rc);
201 /* high & low result bytes read */
202 rc = regmap_bulk_read(data->regmap, VCNL_PS_RESULT_HI, &data->buf,
207 *val = be16_to_cpu(data->buf);
210 mutex_unlock(&data->lock);
215 static int vcnl3020_read_proxy_samp_freq(struct vcnl3020_data *data, int *val,
219 unsigned int prox_rate;
221 rc = regmap_read(data->regmap, VCNL_PROXIMITY_RATE, &prox_rate);
225 if (prox_rate >= ARRAY_SIZE(vcnl3020_prox_sampling_frequency))
228 *val = vcnl3020_prox_sampling_frequency[prox_rate][0];
229 *val2 = vcnl3020_prox_sampling_frequency[prox_rate][1];
234 static int vcnl3020_write_proxy_samp_freq(struct vcnl3020_data *data, int val,
241 mutex_lock(&data->lock);
243 /* Protect against event capture. */
244 if (vcnl3020_is_in_periodic_mode(data)) {
249 for (i = 0; i < ARRAY_SIZE(vcnl3020_prox_sampling_frequency); i++) {
250 if (val == vcnl3020_prox_sampling_frequency[i][0] &&
251 val2 == vcnl3020_prox_sampling_frequency[i][1]) {
262 rc = regmap_write(data->regmap, VCNL_PROXIMITY_RATE, index);
265 "Error (%d) writing proximity rate register\n", rc);
268 mutex_unlock(&data->lock);
273 static bool vcnl3020_is_thr_enabled(struct vcnl3020_data *data)
278 rc = regmap_read(data->regmap, VCNL_PS_ICR, &icr);
281 "Error (%d) reading ICR register\n", rc);
285 return !!(icr & VCNL_ICR_THRES_EN);
288 static int vcnl3020_read_event(struct iio_dev *indio_dev,
289 const struct iio_chan_spec *chan,
290 enum iio_event_type type,
291 enum iio_event_direction dir,
292 enum iio_event_info info,
296 struct vcnl3020_data *data = iio_priv(indio_dev);
299 case IIO_EV_INFO_VALUE:
301 case IIO_EV_DIR_RISING:
302 rc = regmap_bulk_read(data->regmap, VCNL_PS_HI_THR_HI,
303 &data->buf, sizeof(data->buf));
306 *val = be16_to_cpu(data->buf);
308 case IIO_EV_DIR_FALLING:
309 rc = regmap_bulk_read(data->regmap, VCNL_PS_LO_THR_HI,
310 &data->buf, sizeof(data->buf));
313 *val = be16_to_cpu(data->buf);
323 static int vcnl3020_write_event(struct iio_dev *indio_dev,
324 const struct iio_chan_spec *chan,
325 enum iio_event_type type,
326 enum iio_event_direction dir,
327 enum iio_event_info info,
331 struct vcnl3020_data *data = iio_priv(indio_dev);
333 mutex_lock(&data->lock);
336 case IIO_EV_INFO_VALUE:
338 case IIO_EV_DIR_RISING:
339 /* 16 bit word/ low * high */
340 data->buf = cpu_to_be16(val);
341 rc = regmap_bulk_write(data->regmap, VCNL_PS_HI_THR_HI,
342 &data->buf, sizeof(data->buf));
347 case IIO_EV_DIR_FALLING:
348 data->buf = cpu_to_be16(val);
349 rc = regmap_bulk_write(data->regmap, VCNL_PS_LO_THR_HI,
350 &data->buf, sizeof(data->buf));
364 mutex_unlock(&data->lock);
369 static int vcnl3020_enable_periodic(struct iio_dev *indio_dev,
370 struct vcnl3020_data *data)
375 mutex_lock(&data->lock);
377 /* Enable periodic measurement of proximity data. */
378 cmd = VCNL_PS_EN | VCNL_PS_SELFTIMED_EN;
380 rc = regmap_write(data->regmap, VCNL_COMMAND, cmd);
383 "Error (%d) writing command register\n", rc);
388 * Enable interrupts on threshold, for proximity data by
391 rc = regmap_write(data->regmap, VCNL_PS_ICR, VCNL_ICR_THRES_EN);
394 "Error (%d) reading ICR register\n", rc);
397 mutex_unlock(&data->lock);
402 static int vcnl3020_disable_periodic(struct iio_dev *indio_dev,
403 struct vcnl3020_data *data)
407 mutex_lock(&data->lock);
409 rc = regmap_write(data->regmap, VCNL_COMMAND, 0);
412 "Error (%d) writing command register\n", rc);
416 rc = regmap_write(data->regmap, VCNL_PS_ICR, 0);
419 "Error (%d) writing ICR register\n", rc);
423 /* Clear interrupt flag bit */
424 rc = regmap_write(data->regmap, VCNL_ISR, 0);
427 "Error (%d) writing ISR register\n", rc);
430 mutex_unlock(&data->lock);
435 static int vcnl3020_config_threshold(struct iio_dev *indio_dev, bool state)
437 struct vcnl3020_data *data = iio_priv(indio_dev);
440 return vcnl3020_enable_periodic(indio_dev, data);
442 if (!vcnl3020_is_thr_enabled(data))
444 return vcnl3020_disable_periodic(indio_dev, data);
448 static int vcnl3020_write_event_config(struct iio_dev *indio_dev,
449 const struct iio_chan_spec *chan,
450 enum iio_event_type type,
451 enum iio_event_direction dir,
454 switch (chan->type) {
456 return vcnl3020_config_threshold(indio_dev, state);
462 static int vcnl3020_read_event_config(struct iio_dev *indio_dev,
463 const struct iio_chan_spec *chan,
464 enum iio_event_type type,
465 enum iio_event_direction dir)
467 struct vcnl3020_data *data = iio_priv(indio_dev);
469 switch (chan->type) {
471 return vcnl3020_is_thr_enabled(data);
477 static const struct iio_event_spec vcnl3020_event_spec[] = {
479 .type = IIO_EV_TYPE_THRESH,
480 .dir = IIO_EV_DIR_RISING,
481 .mask_separate = BIT(IIO_EV_INFO_VALUE),
483 .type = IIO_EV_TYPE_THRESH,
484 .dir = IIO_EV_DIR_FALLING,
485 .mask_separate = BIT(IIO_EV_INFO_VALUE),
487 .type = IIO_EV_TYPE_THRESH,
488 .dir = IIO_EV_DIR_EITHER,
489 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
493 static const struct iio_chan_spec vcnl3020_channels[] = {
495 .type = IIO_PROXIMITY,
496 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
497 BIT(IIO_CHAN_INFO_SAMP_FREQ),
498 .info_mask_separate_available = BIT(IIO_CHAN_INFO_SAMP_FREQ),
499 .event_spec = vcnl3020_event_spec,
500 .num_event_specs = ARRAY_SIZE(vcnl3020_event_spec),
504 static int vcnl3020_read_raw(struct iio_dev *indio_dev,
505 struct iio_chan_spec const *chan, int *val,
506 int *val2, long mask)
509 struct vcnl3020_data *data = iio_priv(indio_dev);
512 case IIO_CHAN_INFO_RAW:
513 rc = vcnl3020_measure_proximity(data, val);
517 case IIO_CHAN_INFO_SAMP_FREQ:
518 rc = vcnl3020_read_proxy_samp_freq(data, val, val2);
521 return IIO_VAL_INT_PLUS_MICRO;
527 static int vcnl3020_write_raw(struct iio_dev *indio_dev,
528 struct iio_chan_spec const *chan,
529 int val, int val2, long mask)
531 struct vcnl3020_data *data = iio_priv(indio_dev);
534 case IIO_CHAN_INFO_SAMP_FREQ:
535 return vcnl3020_write_proxy_samp_freq(data, val, val2);
541 static int vcnl3020_read_avail(struct iio_dev *indio_dev,
542 struct iio_chan_spec const *chan,
543 const int **vals, int *type, int *length,
547 case IIO_CHAN_INFO_SAMP_FREQ:
548 *vals = (int *)vcnl3020_prox_sampling_frequency;
549 *type = IIO_VAL_INT_PLUS_MICRO;
550 *length = 2 * ARRAY_SIZE(vcnl3020_prox_sampling_frequency);
551 return IIO_AVAIL_LIST;
557 static const struct iio_info vcnl3020_info = {
558 .read_raw = vcnl3020_read_raw,
559 .write_raw = vcnl3020_write_raw,
560 .read_avail = vcnl3020_read_avail,
561 .read_event_value = vcnl3020_read_event,
562 .write_event_value = vcnl3020_write_event,
563 .read_event_config = vcnl3020_read_event_config,
564 .write_event_config = vcnl3020_write_event_config,
567 static const struct regmap_config vcnl3020_regmap_config = {
570 .max_register = VCNL_PS_MOD_ADJ,
573 static irqreturn_t vcnl3020_handle_irq_thread(int irq, void *p)
575 struct iio_dev *indio_dev = p;
576 struct vcnl3020_data *data = iio_priv(indio_dev);
580 rc = regmap_read(data->regmap, VCNL_ISR, &isr);
582 dev_err(data->dev, "Error (%d) reading reg (0x%x)\n",
587 if (!(isr & VCNL_ICR_THRES_EN))
590 iio_push_event(indio_dev,
591 IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 1,
594 iio_get_time_ns(indio_dev));
596 rc = regmap_write(data->regmap, VCNL_ISR, isr & VCNL_ICR_THRES_EN);
598 dev_err(data->dev, "Error (%d) writing in reg (0x%x)\n",
604 static int vcnl3020_probe(struct i2c_client *client)
606 struct vcnl3020_data *data;
607 struct iio_dev *indio_dev;
608 struct regmap *regmap;
611 regmap = devm_regmap_init_i2c(client, &vcnl3020_regmap_config);
612 if (IS_ERR(regmap)) {
613 dev_err(&client->dev, "regmap_init failed\n");
614 return PTR_ERR(regmap);
617 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
621 data = iio_priv(indio_dev);
622 i2c_set_clientdata(client, indio_dev);
623 data->regmap = regmap;
624 data->dev = &client->dev;
626 rc = vcnl3020_init(data);
630 indio_dev->info = &vcnl3020_info;
631 indio_dev->channels = vcnl3020_channels;
632 indio_dev->num_channels = ARRAY_SIZE(vcnl3020_channels);
633 indio_dev->name = "vcnl3020";
634 indio_dev->modes = INDIO_DIRECT_MODE;
637 rc = devm_request_threaded_irq(&client->dev, client->irq,
638 NULL, vcnl3020_handle_irq_thread,
639 IRQF_ONESHOT, indio_dev->name,
642 dev_err(&client->dev,
643 "Error (%d) irq request failed (%u)\n", rc,
649 return devm_iio_device_register(&client->dev, indio_dev);
652 static const struct of_device_id vcnl3020_of_match[] = {
654 .compatible = "vishay,vcnl3020",
658 MODULE_DEVICE_TABLE(of, vcnl3020_of_match);
660 static struct i2c_driver vcnl3020_driver = {
663 .of_match_table = vcnl3020_of_match,
665 .probe = vcnl3020_probe,
667 module_i2c_driver(vcnl3020_driver);
669 MODULE_AUTHOR("Ivan Mikhaylov <i.mikhaylov@yadro.com>");
670 MODULE_DESCRIPTION("Vishay VCNL3020 proximity sensor driver");
671 MODULE_LICENSE("GPL");