rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * vcnl4000.c - Support for Vishay VCNL4000/4010/4020 combined ambient |
| 3 | * light and 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 | * periodic ALS/proximity measurement (VCNL4010/20) |
| 17 | * interrupts (VCNL4010/20) |
| 18 | */ |
| 19 | |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/i2c.h> |
| 22 | #include <linux/err.h> |
| 23 | #include <linux/delay.h> |
| 24 | |
| 25 | #include <linux/iio/iio.h> |
| 26 | #include <linux/iio/sysfs.h> |
| 27 | |
| 28 | #define VCNL4000_DRV_NAME "vcnl4000" |
| 29 | #define VCNL4000_ID 0x01 |
| 30 | #define VCNL4010_ID 0x02 /* for VCNL4020, VCNL4010 */ |
| 31 | |
| 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 */ |
| 42 | |
| 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 */ |
| 48 | |
| 49 | struct vcnl4000_data { |
| 50 | struct i2c_client *client; |
| 51 | struct mutex lock; |
| 52 | }; |
| 53 | |
| 54 | static const struct i2c_device_id vcnl4000_id[] = { |
| 55 | { "vcnl4000", 0 }, |
| 56 | { } |
| 57 | }; |
| 58 | MODULE_DEVICE_TABLE(i2c, vcnl4000_id); |
| 59 | |
| 60 | static int vcnl4000_measure(struct vcnl4000_data *data, u8 req_mask, |
| 61 | u8 rdy_mask, u8 data_reg, int *val) |
| 62 | { |
| 63 | int tries = 20; |
| 64 | int ret; |
| 65 | |
| 66 | mutex_lock(&data->lock); |
| 67 | |
| 68 | ret = i2c_smbus_write_byte_data(data->client, VCNL4000_COMMAND, |
| 69 | req_mask); |
| 70 | if (ret < 0) |
| 71 | goto fail; |
| 72 | |
| 73 | /* wait for data to become ready */ |
| 74 | while (tries--) { |
| 75 | ret = i2c_smbus_read_byte_data(data->client, VCNL4000_COMMAND); |
| 76 | if (ret < 0) |
| 77 | goto fail; |
| 78 | if (ret & rdy_mask) |
| 79 | break; |
| 80 | msleep(20); /* measurement takes up to 100 ms */ |
| 81 | } |
| 82 | |
| 83 | if (tries < 0) { |
| 84 | dev_err(&data->client->dev, |
| 85 | "vcnl4000_measure() failed, data not ready\n"); |
| 86 | ret = -EIO; |
| 87 | goto fail; |
| 88 | } |
| 89 | |
| 90 | ret = i2c_smbus_read_word_swapped(data->client, data_reg); |
| 91 | if (ret < 0) |
| 92 | goto fail; |
| 93 | |
| 94 | mutex_unlock(&data->lock); |
| 95 | *val = ret; |
| 96 | |
| 97 | return 0; |
| 98 | |
| 99 | fail: |
| 100 | mutex_unlock(&data->lock); |
| 101 | return ret; |
| 102 | } |
| 103 | |
| 104 | static const struct iio_chan_spec vcnl4000_channels[] = { |
| 105 | { |
| 106 | .type = IIO_LIGHT, |
| 107 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
| 108 | BIT(IIO_CHAN_INFO_SCALE), |
| 109 | }, { |
| 110 | .type = IIO_PROXIMITY, |
| 111 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
| 112 | } |
| 113 | }; |
| 114 | |
| 115 | static int vcnl4000_read_raw(struct iio_dev *indio_dev, |
| 116 | struct iio_chan_spec const *chan, |
| 117 | int *val, int *val2, long mask) |
| 118 | { |
| 119 | int ret; |
| 120 | struct vcnl4000_data *data = iio_priv(indio_dev); |
| 121 | |
| 122 | switch (mask) { |
| 123 | case IIO_CHAN_INFO_RAW: |
| 124 | switch (chan->type) { |
| 125 | case IIO_LIGHT: |
| 126 | ret = vcnl4000_measure(data, |
| 127 | VCNL4000_AL_OD, VCNL4000_AL_RDY, |
| 128 | VCNL4000_AL_RESULT_HI, val); |
| 129 | if (ret < 0) |
| 130 | return ret; |
| 131 | return IIO_VAL_INT; |
| 132 | case IIO_PROXIMITY: |
| 133 | ret = vcnl4000_measure(data, |
| 134 | VCNL4000_PS_OD, VCNL4000_PS_RDY, |
| 135 | VCNL4000_PS_RESULT_HI, val); |
| 136 | if (ret < 0) |
| 137 | return ret; |
| 138 | return IIO_VAL_INT; |
| 139 | default: |
| 140 | return -EINVAL; |
| 141 | } |
| 142 | case IIO_CHAN_INFO_SCALE: |
| 143 | if (chan->type != IIO_LIGHT) |
| 144 | return -EINVAL; |
| 145 | |
| 146 | *val = 0; |
| 147 | *val2 = 250000; |
| 148 | return IIO_VAL_INT_PLUS_MICRO; |
| 149 | default: |
| 150 | return -EINVAL; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | static const struct iio_info vcnl4000_info = { |
| 155 | .read_raw = vcnl4000_read_raw, |
| 156 | .driver_module = THIS_MODULE, |
| 157 | }; |
| 158 | |
| 159 | static int vcnl4000_probe(struct i2c_client *client, |
| 160 | const struct i2c_device_id *id) |
| 161 | { |
| 162 | struct vcnl4000_data *data; |
| 163 | struct iio_dev *indio_dev; |
| 164 | int ret, prod_id; |
| 165 | |
| 166 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
| 167 | if (!indio_dev) |
| 168 | return -ENOMEM; |
| 169 | |
| 170 | data = iio_priv(indio_dev); |
| 171 | i2c_set_clientdata(client, indio_dev); |
| 172 | data->client = client; |
| 173 | mutex_init(&data->lock); |
| 174 | |
| 175 | ret = i2c_smbus_read_byte_data(data->client, VCNL4000_PROD_REV); |
| 176 | if (ret < 0) |
| 177 | return ret; |
| 178 | |
| 179 | prod_id = ret >> 4; |
| 180 | if (prod_id != VCNL4010_ID && prod_id != VCNL4000_ID) |
| 181 | return -ENODEV; |
| 182 | |
| 183 | dev_dbg(&client->dev, "%s Ambient light/proximity sensor, Rev: %02x\n", |
| 184 | (prod_id == VCNL4010_ID) ? "VCNL4010/4020" : "VCNL4000", |
| 185 | ret & 0xf); |
| 186 | |
| 187 | indio_dev->dev.parent = &client->dev; |
| 188 | indio_dev->info = &vcnl4000_info; |
| 189 | indio_dev->channels = vcnl4000_channels; |
| 190 | indio_dev->num_channels = ARRAY_SIZE(vcnl4000_channels); |
| 191 | indio_dev->name = VCNL4000_DRV_NAME; |
| 192 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 193 | |
| 194 | return devm_iio_device_register(&client->dev, indio_dev); |
| 195 | } |
| 196 | |
| 197 | static struct i2c_driver vcnl4000_driver = { |
| 198 | .driver = { |
| 199 | .name = VCNL4000_DRV_NAME, |
| 200 | }, |
| 201 | .probe = vcnl4000_probe, |
| 202 | .id_table = vcnl4000_id, |
| 203 | }; |
| 204 | |
| 205 | module_i2c_driver(vcnl4000_driver); |
| 206 | |
| 207 | MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>"); |
| 208 | MODULE_DESCRIPTION("Vishay VCNL4000 proximity/ambient light sensor driver"); |
| 209 | MODULE_LICENSE("GPL"); |