WIP FPC-III support
[linux/fpc-iii.git] / drivers / iio / chemical / vz89x.c
blob23b22a5f5c1ce141045c34a99e3b0f6a8bcc3c68
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * vz89x.c - Support for SGX Sensortech MiCS VZ89X VOC sensors
5 * Copyright (C) 2015-2018
6 * Author: Matt Ranostay <matt.ranostay@konsulko.com>
7 */
9 #include <linux/module.h>
10 #include <linux/mutex.h>
11 #include <linux/init.h>
12 #include <linux/i2c.h>
13 #include <linux/mod_devicetable.h>
15 #include <linux/iio/iio.h>
16 #include <linux/iio/sysfs.h>
18 #define VZ89X_REG_MEASUREMENT 0x09
19 #define VZ89X_REG_MEASUREMENT_RD_SIZE 6
20 #define VZ89X_REG_MEASUREMENT_WR_SIZE 3
22 #define VZ89X_VOC_CO2_IDX 0
23 #define VZ89X_VOC_SHORT_IDX 1
24 #define VZ89X_VOC_TVOC_IDX 2
25 #define VZ89X_VOC_RESISTANCE_IDX 3
27 #define VZ89TE_REG_MEASUREMENT 0x0c
28 #define VZ89TE_REG_MEASUREMENT_RD_SIZE 7
29 #define VZ89TE_REG_MEASUREMENT_WR_SIZE 6
31 #define VZ89TE_VOC_TVOC_IDX 0
32 #define VZ89TE_VOC_CO2_IDX 1
33 #define VZ89TE_VOC_RESISTANCE_IDX 2
35 enum {
36 VZ89X,
37 VZ89TE,
40 struct vz89x_chip_data;
42 struct vz89x_data {
43 struct i2c_client *client;
44 const struct vz89x_chip_data *chip;
45 struct mutex lock;
46 int (*xfer)(struct vz89x_data *data, u8 cmd);
48 bool is_valid;
49 unsigned long last_update;
50 u8 buffer[VZ89TE_REG_MEASUREMENT_RD_SIZE];
53 struct vz89x_chip_data {
54 bool (*valid)(struct vz89x_data *data);
55 const struct iio_chan_spec *channels;
56 u8 num_channels;
58 u8 cmd;
59 u8 read_size;
60 u8 write_size;
63 static const struct iio_chan_spec vz89x_channels[] = {
65 .type = IIO_CONCENTRATION,
66 .channel2 = IIO_MOD_CO2,
67 .modified = 1,
68 .info_mask_separate =
69 BIT(IIO_CHAN_INFO_OFFSET) | BIT(IIO_CHAN_INFO_RAW),
70 .address = VZ89X_VOC_CO2_IDX,
73 .type = IIO_CONCENTRATION,
74 .channel2 = IIO_MOD_VOC,
75 .modified = 1,
76 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
77 .address = VZ89X_VOC_SHORT_IDX,
78 .extend_name = "short",
81 .type = IIO_CONCENTRATION,
82 .channel2 = IIO_MOD_VOC,
83 .modified = 1,
84 .info_mask_separate =
85 BIT(IIO_CHAN_INFO_OFFSET) | BIT(IIO_CHAN_INFO_RAW),
86 .address = VZ89X_VOC_TVOC_IDX,
89 .type = IIO_RESISTANCE,
90 .info_mask_separate =
91 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
92 .address = VZ89X_VOC_RESISTANCE_IDX,
93 .scan_index = -1,
94 .scan_type = {
95 .endianness = IIO_LE,
100 static const struct iio_chan_spec vz89te_channels[] = {
102 .type = IIO_CONCENTRATION,
103 .channel2 = IIO_MOD_VOC,
104 .modified = 1,
105 .info_mask_separate =
106 BIT(IIO_CHAN_INFO_OFFSET) | BIT(IIO_CHAN_INFO_RAW),
107 .address = VZ89TE_VOC_TVOC_IDX,
111 .type = IIO_CONCENTRATION,
112 .channel2 = IIO_MOD_CO2,
113 .modified = 1,
114 .info_mask_separate =
115 BIT(IIO_CHAN_INFO_OFFSET) | BIT(IIO_CHAN_INFO_RAW),
116 .address = VZ89TE_VOC_CO2_IDX,
119 .type = IIO_RESISTANCE,
120 .info_mask_separate =
121 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
122 .address = VZ89TE_VOC_RESISTANCE_IDX,
123 .scan_index = -1,
124 .scan_type = {
125 .endianness = IIO_BE,
130 static IIO_CONST_ATTR(in_concentration_co2_scale, "0.00000698689");
131 static IIO_CONST_ATTR(in_concentration_voc_scale, "0.00000000436681223");
133 static struct attribute *vz89x_attributes[] = {
134 &iio_const_attr_in_concentration_co2_scale.dev_attr.attr,
135 &iio_const_attr_in_concentration_voc_scale.dev_attr.attr,
136 NULL,
139 static const struct attribute_group vz89x_attrs_group = {
140 .attrs = vz89x_attributes,
144 * Chipset sometime updates in the middle of a reading causing it to reset the
145 * data pointer, and causing invalid reading of previous data.
146 * We can check for this by reading MSB of the resistance reading that is
147 * always zero, and by also confirming the VOC_short isn't zero.
150 static bool vz89x_measurement_is_valid(struct vz89x_data *data)
152 if (data->buffer[VZ89X_VOC_SHORT_IDX] == 0)
153 return true;
155 return !!(data->buffer[data->chip->read_size - 1] > 0);
158 /* VZ89TE device has a modified CRC-8 two complement check */
159 static bool vz89te_measurement_is_valid(struct vz89x_data *data)
161 u8 crc = 0;
162 int i, sum = 0;
164 for (i = 0; i < (data->chip->read_size - 1); i++) {
165 sum = crc + data->buffer[i];
166 crc = sum;
167 crc += sum / 256;
170 return !((0xff - crc) == data->buffer[data->chip->read_size - 1]);
173 static int vz89x_i2c_xfer(struct vz89x_data *data, u8 cmd)
175 const struct vz89x_chip_data *chip = data->chip;
176 struct i2c_client *client = data->client;
177 struct i2c_msg msg[2];
178 int ret;
179 u8 buf[6] = { cmd, 0, 0, 0, 0, 0xf3 };
181 msg[0].addr = client->addr;
182 msg[0].flags = client->flags;
183 msg[0].len = chip->write_size;
184 msg[0].buf = (char *) &buf;
186 msg[1].addr = client->addr;
187 msg[1].flags = client->flags | I2C_M_RD;
188 msg[1].len = chip->read_size;
189 msg[1].buf = (char *) &data->buffer;
191 ret = i2c_transfer(client->adapter, msg, 2);
193 return (ret == 2) ? 0 : ret;
196 static int vz89x_smbus_xfer(struct vz89x_data *data, u8 cmd)
198 struct i2c_client *client = data->client;
199 int ret;
200 int i;
202 ret = i2c_smbus_write_word_data(client, cmd, 0);
203 if (ret < 0)
204 return ret;
206 for (i = 0; i < data->chip->read_size; i++) {
207 ret = i2c_smbus_read_byte(client);
208 if (ret < 0)
209 return ret;
210 data->buffer[i] = ret;
213 return 0;
216 static int vz89x_get_measurement(struct vz89x_data *data)
218 const struct vz89x_chip_data *chip = data->chip;
219 int ret;
221 /* sensor can only be polled once a second max per datasheet */
222 if (!time_after(jiffies, data->last_update + HZ))
223 return data->is_valid ? 0 : -EAGAIN;
225 data->is_valid = false;
226 data->last_update = jiffies;
228 ret = data->xfer(data, chip->cmd);
229 if (ret < 0)
230 return ret;
232 ret = chip->valid(data);
233 if (ret)
234 return -EAGAIN;
236 data->is_valid = true;
238 return 0;
241 static int vz89x_get_resistance_reading(struct vz89x_data *data,
242 struct iio_chan_spec const *chan,
243 int *val)
245 u8 *tmp = (u8 *) &data->buffer[chan->address];
247 switch (chan->scan_type.endianness) {
248 case IIO_LE:
249 *val = le32_to_cpup((__le32 *) tmp) & GENMASK(23, 0);
250 break;
251 case IIO_BE:
252 *val = be32_to_cpup((__be32 *) tmp) >> 8;
253 break;
254 default:
255 return -EINVAL;
258 return 0;
261 static int vz89x_read_raw(struct iio_dev *indio_dev,
262 struct iio_chan_spec const *chan, int *val,
263 int *val2, long mask)
265 struct vz89x_data *data = iio_priv(indio_dev);
266 int ret = -EINVAL;
268 switch (mask) {
269 case IIO_CHAN_INFO_RAW:
270 mutex_lock(&data->lock);
271 ret = vz89x_get_measurement(data);
272 mutex_unlock(&data->lock);
274 if (ret)
275 return ret;
277 switch (chan->type) {
278 case IIO_CONCENTRATION:
279 *val = data->buffer[chan->address];
280 return IIO_VAL_INT;
281 case IIO_RESISTANCE:
282 ret = vz89x_get_resistance_reading(data, chan, val);
283 if (!ret)
284 return IIO_VAL_INT;
285 break;
286 default:
287 return -EINVAL;
289 break;
290 case IIO_CHAN_INFO_SCALE:
291 switch (chan->type) {
292 case IIO_RESISTANCE:
293 *val = 10;
294 return IIO_VAL_INT;
295 default:
296 return -EINVAL;
298 break;
299 case IIO_CHAN_INFO_OFFSET:
300 switch (chan->channel2) {
301 case IIO_MOD_CO2:
302 *val = 44;
303 *val2 = 250000;
304 return IIO_VAL_INT_PLUS_MICRO;
305 case IIO_MOD_VOC:
306 *val = -13;
307 return IIO_VAL_INT;
308 default:
309 return -EINVAL;
313 return ret;
316 static const struct iio_info vz89x_info = {
317 .attrs = &vz89x_attrs_group,
318 .read_raw = vz89x_read_raw,
321 static const struct vz89x_chip_data vz89x_chips[] = {
323 .valid = vz89x_measurement_is_valid,
325 .cmd = VZ89X_REG_MEASUREMENT,
326 .read_size = VZ89X_REG_MEASUREMENT_RD_SIZE,
327 .write_size = VZ89X_REG_MEASUREMENT_WR_SIZE,
329 .channels = vz89x_channels,
330 .num_channels = ARRAY_SIZE(vz89x_channels),
333 .valid = vz89te_measurement_is_valid,
335 .cmd = VZ89TE_REG_MEASUREMENT,
336 .read_size = VZ89TE_REG_MEASUREMENT_RD_SIZE,
337 .write_size = VZ89TE_REG_MEASUREMENT_WR_SIZE,
339 .channels = vz89te_channels,
340 .num_channels = ARRAY_SIZE(vz89te_channels),
344 static const struct of_device_id vz89x_dt_ids[] = {
345 { .compatible = "sgx,vz89x", .data = (void *) VZ89X },
346 { .compatible = "sgx,vz89te", .data = (void *) VZ89TE },
349 MODULE_DEVICE_TABLE(of, vz89x_dt_ids);
351 static int vz89x_probe(struct i2c_client *client,
352 const struct i2c_device_id *id)
354 struct device *dev = &client->dev;
355 struct iio_dev *indio_dev;
356 struct vz89x_data *data;
357 int chip_id;
359 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
360 if (!indio_dev)
361 return -ENOMEM;
362 data = iio_priv(indio_dev);
364 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
365 data->xfer = vz89x_i2c_xfer;
366 else if (i2c_check_functionality(client->adapter,
367 I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_BYTE))
368 data->xfer = vz89x_smbus_xfer;
369 else
370 return -EOPNOTSUPP;
372 if (!dev_fwnode(dev))
373 chip_id = id->driver_data;
374 else
375 chip_id = (unsigned long)device_get_match_data(dev);
377 i2c_set_clientdata(client, indio_dev);
378 data->client = client;
379 data->chip = &vz89x_chips[chip_id];
380 data->last_update = jiffies - HZ;
381 mutex_init(&data->lock);
383 indio_dev->info = &vz89x_info;
384 indio_dev->name = dev_name(dev);
385 indio_dev->modes = INDIO_DIRECT_MODE;
387 indio_dev->channels = data->chip->channels;
388 indio_dev->num_channels = data->chip->num_channels;
390 return devm_iio_device_register(dev, indio_dev);
393 static const struct i2c_device_id vz89x_id[] = {
394 { "vz89x", VZ89X },
395 { "vz89te", VZ89TE },
398 MODULE_DEVICE_TABLE(i2c, vz89x_id);
400 static struct i2c_driver vz89x_driver = {
401 .driver = {
402 .name = "vz89x",
403 .of_match_table = vz89x_dt_ids,
405 .probe = vz89x_probe,
406 .id_table = vz89x_id,
408 module_i2c_driver(vz89x_driver);
410 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
411 MODULE_DESCRIPTION("SGX Sensortech MiCS VZ89X VOC sensors");
412 MODULE_LICENSE("GPL v2");