1 // SPDX-License-Identifier: GPL-2.0+
3 * vz89x.c - Support for SGX Sensortech MiCS VZ89X VOC sensors
5 * Copyright (C) 2015-2018
6 * Author: Matt Ranostay <matt.ranostay@konsulko.com>
9 #include <linux/module.h>
10 #include <linux/mutex.h>
11 #include <linux/init.h>
12 #include <linux/i2c.h>
14 #include <linux/of_device.h>
16 #include <linux/iio/iio.h>
17 #include <linux/iio/sysfs.h>
19 #define VZ89X_REG_MEASUREMENT 0x09
20 #define VZ89X_REG_MEASUREMENT_RD_SIZE 6
21 #define VZ89X_REG_MEASUREMENT_WR_SIZE 3
23 #define VZ89X_VOC_CO2_IDX 0
24 #define VZ89X_VOC_SHORT_IDX 1
25 #define VZ89X_VOC_TVOC_IDX 2
26 #define VZ89X_VOC_RESISTANCE_IDX 3
28 #define VZ89TE_REG_MEASUREMENT 0x0c
29 #define VZ89TE_REG_MEASUREMENT_RD_SIZE 7
30 #define VZ89TE_REG_MEASUREMENT_WR_SIZE 6
32 #define VZ89TE_VOC_TVOC_IDX 0
33 #define VZ89TE_VOC_CO2_IDX 1
34 #define VZ89TE_VOC_RESISTANCE_IDX 2
41 struct vz89x_chip_data;
44 struct i2c_client *client;
45 const struct vz89x_chip_data *chip;
47 int (*xfer)(struct vz89x_data *data, u8 cmd);
50 unsigned long last_update;
51 u8 buffer[VZ89TE_REG_MEASUREMENT_RD_SIZE];
54 struct vz89x_chip_data {
55 bool (*valid)(struct vz89x_data *data);
56 const struct iio_chan_spec *channels;
64 static const struct iio_chan_spec vz89x_channels[] = {
66 .type = IIO_CONCENTRATION,
67 .channel2 = IIO_MOD_CO2,
70 BIT(IIO_CHAN_INFO_OFFSET) | BIT(IIO_CHAN_INFO_RAW),
71 .address = VZ89X_VOC_CO2_IDX,
74 .type = IIO_CONCENTRATION,
75 .channel2 = IIO_MOD_VOC,
77 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
78 .address = VZ89X_VOC_SHORT_IDX,
79 .extend_name = "short",
82 .type = IIO_CONCENTRATION,
83 .channel2 = IIO_MOD_VOC,
86 BIT(IIO_CHAN_INFO_OFFSET) | BIT(IIO_CHAN_INFO_RAW),
87 .address = VZ89X_VOC_TVOC_IDX,
90 .type = IIO_RESISTANCE,
92 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
93 .address = VZ89X_VOC_RESISTANCE_IDX,
101 static const struct iio_chan_spec vz89te_channels[] = {
103 .type = IIO_CONCENTRATION,
104 .channel2 = IIO_MOD_VOC,
106 .info_mask_separate =
107 BIT(IIO_CHAN_INFO_OFFSET) | BIT(IIO_CHAN_INFO_RAW),
108 .address = VZ89TE_VOC_TVOC_IDX,
112 .type = IIO_CONCENTRATION,
113 .channel2 = IIO_MOD_CO2,
115 .info_mask_separate =
116 BIT(IIO_CHAN_INFO_OFFSET) | BIT(IIO_CHAN_INFO_RAW),
117 .address = VZ89TE_VOC_CO2_IDX,
120 .type = IIO_RESISTANCE,
121 .info_mask_separate =
122 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
123 .address = VZ89TE_VOC_RESISTANCE_IDX,
126 .endianness = IIO_BE,
131 static IIO_CONST_ATTR(in_concentration_co2_scale, "0.00000698689");
132 static IIO_CONST_ATTR(in_concentration_voc_scale, "0.00000000436681223");
134 static struct attribute *vz89x_attributes[] = {
135 &iio_const_attr_in_concentration_co2_scale.dev_attr.attr,
136 &iio_const_attr_in_concentration_voc_scale.dev_attr.attr,
140 static const struct attribute_group vz89x_attrs_group = {
141 .attrs = vz89x_attributes,
145 * Chipset sometime updates in the middle of a reading causing it to reset the
146 * data pointer, and causing invalid reading of previous data.
147 * We can check for this by reading MSB of the resistance reading that is
148 * always zero, and by also confirming the VOC_short isn't zero.
151 static bool vz89x_measurement_is_valid(struct vz89x_data *data)
153 if (data->buffer[VZ89X_VOC_SHORT_IDX] == 0)
156 return !!(data->buffer[data->chip->read_size - 1] > 0);
159 /* VZ89TE device has a modified CRC-8 two complement check */
160 static bool vz89te_measurement_is_valid(struct vz89x_data *data)
165 for (i = 0; i < (data->chip->read_size - 1); i++) {
166 sum = crc + data->buffer[i];
171 return !((0xff - crc) == data->buffer[data->chip->read_size - 1]);
174 static int vz89x_i2c_xfer(struct vz89x_data *data, u8 cmd)
176 const struct vz89x_chip_data *chip = data->chip;
177 struct i2c_client *client = data->client;
178 struct i2c_msg msg[2];
180 u8 buf[6] = { cmd, 0, 0, 0, 0, 0xf3 };
182 msg[0].addr = client->addr;
183 msg[0].flags = client->flags;
184 msg[0].len = chip->write_size;
185 msg[0].buf = (char *) &buf;
187 msg[1].addr = client->addr;
188 msg[1].flags = client->flags | I2C_M_RD;
189 msg[1].len = chip->read_size;
190 msg[1].buf = (char *) &data->buffer;
192 ret = i2c_transfer(client->adapter, msg, 2);
194 return (ret == 2) ? 0 : ret;
197 static int vz89x_smbus_xfer(struct vz89x_data *data, u8 cmd)
199 struct i2c_client *client = data->client;
203 ret = i2c_smbus_write_word_data(client, cmd, 0);
207 for (i = 0; i < data->chip->read_size; i++) {
208 ret = i2c_smbus_read_byte(client);
211 data->buffer[i] = ret;
217 static int vz89x_get_measurement(struct vz89x_data *data)
219 const struct vz89x_chip_data *chip = data->chip;
222 /* sensor can only be polled once a second max per datasheet */
223 if (!time_after(jiffies, data->last_update + HZ))
224 return data->is_valid ? 0 : -EAGAIN;
226 data->is_valid = false;
227 data->last_update = jiffies;
229 ret = data->xfer(data, chip->cmd);
233 ret = chip->valid(data);
237 data->is_valid = true;
242 static int vz89x_get_resistance_reading(struct vz89x_data *data,
243 struct iio_chan_spec const *chan,
246 u8 *tmp = (u8 *) &data->buffer[chan->address];
248 switch (chan->scan_type.endianness) {
250 *val = le32_to_cpup((__le32 *) tmp) & GENMASK(23, 0);
253 *val = be32_to_cpup((__be32 *) tmp) >> 8;
262 static int vz89x_read_raw(struct iio_dev *indio_dev,
263 struct iio_chan_spec const *chan, int *val,
264 int *val2, long mask)
266 struct vz89x_data *data = iio_priv(indio_dev);
270 case IIO_CHAN_INFO_RAW:
271 mutex_lock(&data->lock);
272 ret = vz89x_get_measurement(data);
273 mutex_unlock(&data->lock);
278 switch (chan->type) {
279 case IIO_CONCENTRATION:
280 *val = data->buffer[chan->address];
283 ret = vz89x_get_resistance_reading(data, chan, val);
291 case IIO_CHAN_INFO_SCALE:
292 switch (chan->type) {
300 case IIO_CHAN_INFO_OFFSET:
301 switch (chan->channel2) {
305 return IIO_VAL_INT_PLUS_MICRO;
317 static const struct iio_info vz89x_info = {
318 .attrs = &vz89x_attrs_group,
319 .read_raw = vz89x_read_raw,
322 static const struct vz89x_chip_data vz89x_chips[] = {
324 .valid = vz89x_measurement_is_valid,
326 .cmd = VZ89X_REG_MEASUREMENT,
327 .read_size = VZ89X_REG_MEASUREMENT_RD_SIZE,
328 .write_size = VZ89X_REG_MEASUREMENT_WR_SIZE,
330 .channels = vz89x_channels,
331 .num_channels = ARRAY_SIZE(vz89x_channels),
334 .valid = vz89te_measurement_is_valid,
336 .cmd = VZ89TE_REG_MEASUREMENT,
337 .read_size = VZ89TE_REG_MEASUREMENT_RD_SIZE,
338 .write_size = VZ89TE_REG_MEASUREMENT_WR_SIZE,
340 .channels = vz89te_channels,
341 .num_channels = ARRAY_SIZE(vz89te_channels),
345 static const struct of_device_id vz89x_dt_ids[] = {
346 { .compatible = "sgx,vz89x", .data = (void *) VZ89X },
347 { .compatible = "sgx,vz89te", .data = (void *) VZ89TE },
350 MODULE_DEVICE_TABLE(of, vz89x_dt_ids);
352 static int vz89x_probe(struct i2c_client *client,
353 const struct i2c_device_id *id)
355 struct iio_dev *indio_dev;
356 struct vz89x_data *data;
357 const struct of_device_id *of_id;
360 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
363 data = iio_priv(indio_dev);
365 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
366 data->xfer = vz89x_i2c_xfer;
367 else if (i2c_check_functionality(client->adapter,
368 I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_BYTE))
369 data->xfer = vz89x_smbus_xfer;
373 of_id = of_match_device(vz89x_dt_ids, &client->dev);
375 chip_id = id->driver_data;
377 chip_id = (unsigned long)of_id->data;
379 i2c_set_clientdata(client, indio_dev);
380 data->client = client;
381 data->chip = &vz89x_chips[chip_id];
382 data->last_update = jiffies - HZ;
383 mutex_init(&data->lock);
385 indio_dev->dev.parent = &client->dev;
386 indio_dev->info = &vz89x_info;
387 indio_dev->name = dev_name(&client->dev);
388 indio_dev->modes = INDIO_DIRECT_MODE;
390 indio_dev->channels = data->chip->channels;
391 indio_dev->num_channels = data->chip->num_channels;
393 return devm_iio_device_register(&client->dev, indio_dev);
396 static const struct i2c_device_id vz89x_id[] = {
398 { "vz89te", VZ89TE },
401 MODULE_DEVICE_TABLE(i2c, vz89x_id);
403 static struct i2c_driver vz89x_driver = {
406 .of_match_table = of_match_ptr(vz89x_dt_ids),
408 .probe = vz89x_probe,
409 .id_table = vz89x_id,
411 module_i2c_driver(vz89x_driver);
413 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
414 MODULE_DESCRIPTION("SGX Sensortech MiCS VZ89X VOC sensors");
415 MODULE_LICENSE("GPL v2");