2 * vz89x.c - Support for SGX Sensortech MiCS VZ89X VOC sensors
4 * Copyright (C) 2015 Matt Ranostay <mranostay@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
18 #include <linux/module.h>
19 #include <linux/mutex.h>
20 #include <linux/init.h>
21 #include <linux/i2c.h>
23 #include <linux/of_device.h>
25 #include <linux/iio/iio.h>
26 #include <linux/iio/sysfs.h>
28 #define VZ89X_REG_MEASUREMENT 0x09
29 #define VZ89X_REG_MEASUREMENT_RD_SIZE 6
30 #define VZ89X_REG_MEASUREMENT_WR_SIZE 3
32 #define VZ89X_VOC_CO2_IDX 0
33 #define VZ89X_VOC_SHORT_IDX 1
34 #define VZ89X_VOC_TVOC_IDX 2
35 #define VZ89X_VOC_RESISTANCE_IDX 3
37 #define VZ89TE_REG_MEASUREMENT 0x0c
38 #define VZ89TE_REG_MEASUREMENT_RD_SIZE 7
39 #define VZ89TE_REG_MEASUREMENT_WR_SIZE 6
41 #define VZ89TE_VOC_TVOC_IDX 0
42 #define VZ89TE_VOC_CO2_IDX 1
43 #define VZ89TE_VOC_RESISTANCE_IDX 2
50 struct vz89x_chip_data;
53 struct i2c_client *client;
54 const struct vz89x_chip_data *chip;
56 int (*xfer)(struct vz89x_data *data, u8 cmd);
59 unsigned long last_update;
60 u8 buffer[VZ89TE_REG_MEASUREMENT_RD_SIZE];
63 struct vz89x_chip_data {
64 bool (*valid)(struct vz89x_data *data);
65 const struct iio_chan_spec *channels;
73 static const struct iio_chan_spec vz89x_channels[] = {
75 .type = IIO_CONCENTRATION,
76 .channel2 = IIO_MOD_CO2,
79 BIT(IIO_CHAN_INFO_OFFSET) | BIT(IIO_CHAN_INFO_RAW),
80 .address = VZ89X_VOC_CO2_IDX,
83 .type = IIO_CONCENTRATION,
84 .channel2 = IIO_MOD_VOC,
86 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
87 .address = VZ89X_VOC_SHORT_IDX,
88 .extend_name = "short",
91 .type = IIO_CONCENTRATION,
92 .channel2 = IIO_MOD_VOC,
95 BIT(IIO_CHAN_INFO_OFFSET) | BIT(IIO_CHAN_INFO_RAW),
96 .address = VZ89X_VOC_TVOC_IDX,
99 .type = IIO_RESISTANCE,
100 .info_mask_separate =
101 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
102 .address = VZ89X_VOC_RESISTANCE_IDX,
105 .endianness = IIO_LE,
110 static const struct iio_chan_spec vz89te_channels[] = {
112 .type = IIO_CONCENTRATION,
113 .channel2 = IIO_MOD_VOC,
115 .info_mask_separate =
116 BIT(IIO_CHAN_INFO_OFFSET) | BIT(IIO_CHAN_INFO_RAW),
117 .address = VZ89TE_VOC_TVOC_IDX,
121 .type = IIO_CONCENTRATION,
122 .channel2 = IIO_MOD_CO2,
124 .info_mask_separate =
125 BIT(IIO_CHAN_INFO_OFFSET) | BIT(IIO_CHAN_INFO_RAW),
126 .address = VZ89TE_VOC_CO2_IDX,
129 .type = IIO_RESISTANCE,
130 .info_mask_separate =
131 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
132 .address = VZ89TE_VOC_RESISTANCE_IDX,
135 .endianness = IIO_BE,
140 static IIO_CONST_ATTR(in_concentration_co2_scale, "0.00000698689");
141 static IIO_CONST_ATTR(in_concentration_voc_scale, "0.00000000436681223");
143 static struct attribute *vz89x_attributes[] = {
144 &iio_const_attr_in_concentration_co2_scale.dev_attr.attr,
145 &iio_const_attr_in_concentration_voc_scale.dev_attr.attr,
149 static const struct attribute_group vz89x_attrs_group = {
150 .attrs = vz89x_attributes,
154 * Chipset sometime updates in the middle of a reading causing it to reset the
155 * data pointer, and causing invalid reading of previous data.
156 * We can check for this by reading MSB of the resistance reading that is
157 * always zero, and by also confirming the VOC_short isn't zero.
160 static bool vz89x_measurement_is_valid(struct vz89x_data *data)
162 if (data->buffer[VZ89X_VOC_SHORT_IDX] == 0)
165 return !!(data->buffer[data->chip->read_size - 1] > 0);
168 /* VZ89TE device has a modified CRC-8 two complement check */
169 static bool vz89te_measurement_is_valid(struct vz89x_data *data)
174 for (i = 0; i < (data->chip->read_size - 1); i++) {
175 sum = crc + data->buffer[i];
180 return !((0xff - crc) == data->buffer[data->chip->read_size - 1]);
183 static int vz89x_i2c_xfer(struct vz89x_data *data, u8 cmd)
185 const struct vz89x_chip_data *chip = data->chip;
186 struct i2c_client *client = data->client;
187 struct i2c_msg msg[2];
189 u8 buf[6] = { cmd, 0, 0, 0, 0, 0xf3 };
191 msg[0].addr = client->addr;
192 msg[0].flags = client->flags;
193 msg[0].len = chip->write_size;
194 msg[0].buf = (char *) &buf;
196 msg[1].addr = client->addr;
197 msg[1].flags = client->flags | I2C_M_RD;
198 msg[1].len = chip->read_size;
199 msg[1].buf = (char *) &data->buffer;
201 ret = i2c_transfer(client->adapter, msg, 2);
203 return (ret == 2) ? 0 : ret;
206 static int vz89x_smbus_xfer(struct vz89x_data *data, u8 cmd)
208 struct i2c_client *client = data->client;
212 ret = i2c_smbus_write_word_data(client, cmd, 0);
216 for (i = 0; i < data->chip->read_size; i++) {
217 ret = i2c_smbus_read_byte(client);
220 data->buffer[i] = ret;
226 static int vz89x_get_measurement(struct vz89x_data *data)
228 const struct vz89x_chip_data *chip = data->chip;
231 /* sensor can only be polled once a second max per datasheet */
232 if (!time_after(jiffies, data->last_update + HZ))
233 return data->is_valid ? 0 : -EAGAIN;
235 data->is_valid = false;
236 data->last_update = jiffies;
238 ret = data->xfer(data, chip->cmd);
242 ret = chip->valid(data);
246 data->is_valid = true;
251 static int vz89x_get_resistance_reading(struct vz89x_data *data,
252 struct iio_chan_spec const *chan,
255 u8 *tmp = (u8 *) &data->buffer[chan->address];
257 switch (chan->scan_type.endianness) {
259 *val = le32_to_cpup((__le32 *) tmp) & GENMASK(23, 0);
262 *val = be32_to_cpup((__be32 *) tmp) >> 8;
271 static int vz89x_read_raw(struct iio_dev *indio_dev,
272 struct iio_chan_spec const *chan, int *val,
273 int *val2, long mask)
275 struct vz89x_data *data = iio_priv(indio_dev);
279 case IIO_CHAN_INFO_RAW:
280 mutex_lock(&data->lock);
281 ret = vz89x_get_measurement(data);
282 mutex_unlock(&data->lock);
287 switch (chan->type) {
288 case IIO_CONCENTRATION:
289 *val = data->buffer[chan->address];
292 ret = vz89x_get_resistance_reading(data, chan, val);
300 case IIO_CHAN_INFO_SCALE:
301 switch (chan->type) {
309 case IIO_CHAN_INFO_OFFSET:
310 switch (chan->channel2) {
314 return IIO_VAL_INT_PLUS_MICRO;
326 static const struct iio_info vz89x_info = {
327 .attrs = &vz89x_attrs_group,
328 .read_raw = vz89x_read_raw,
329 .driver_module = THIS_MODULE,
332 static const struct vz89x_chip_data vz89x_chips[] = {
334 .valid = vz89x_measurement_is_valid,
336 .cmd = VZ89X_REG_MEASUREMENT,
337 .read_size = VZ89X_REG_MEASUREMENT_RD_SIZE,
338 .write_size = VZ89X_REG_MEASUREMENT_WR_SIZE,
340 .channels = vz89x_channels,
341 .num_channels = ARRAY_SIZE(vz89x_channels),
344 .valid = vz89te_measurement_is_valid,
346 .cmd = VZ89TE_REG_MEASUREMENT,
347 .read_size = VZ89TE_REG_MEASUREMENT_RD_SIZE,
348 .write_size = VZ89TE_REG_MEASUREMENT_WR_SIZE,
350 .channels = vz89te_channels,
351 .num_channels = ARRAY_SIZE(vz89te_channels),
355 static const struct of_device_id vz89x_dt_ids[] = {
356 { .compatible = "sgx,vz89x", .data = (void *) VZ89X },
357 { .compatible = "sgx,vz89te", .data = (void *) VZ89TE },
360 MODULE_DEVICE_TABLE(of, vz89x_dt_ids);
362 static int vz89x_probe(struct i2c_client *client,
363 const struct i2c_device_id *id)
365 struct iio_dev *indio_dev;
366 struct vz89x_data *data;
367 const struct of_device_id *of_id;
370 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
373 data = iio_priv(indio_dev);
375 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
376 data->xfer = vz89x_i2c_xfer;
377 else if (i2c_check_functionality(client->adapter,
378 I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_BYTE))
379 data->xfer = vz89x_smbus_xfer;
383 of_id = of_match_device(vz89x_dt_ids, &client->dev);
385 chip_id = id->driver_data;
387 chip_id = (unsigned long)of_id->data;
389 i2c_set_clientdata(client, indio_dev);
390 data->client = client;
391 data->chip = &vz89x_chips[chip_id];
392 data->last_update = jiffies - HZ;
393 mutex_init(&data->lock);
395 indio_dev->dev.parent = &client->dev;
396 indio_dev->info = &vz89x_info,
397 indio_dev->name = dev_name(&client->dev);
398 indio_dev->modes = INDIO_DIRECT_MODE;
400 indio_dev->channels = data->chip->channels;
401 indio_dev->num_channels = data->chip->num_channels;
403 return devm_iio_device_register(&client->dev, indio_dev);
406 static const struct i2c_device_id vz89x_id[] = {
408 { "vz89te", VZ89TE },
411 MODULE_DEVICE_TABLE(i2c, vz89x_id);
413 static struct i2c_driver vz89x_driver = {
416 .of_match_table = of_match_ptr(vz89x_dt_ids),
418 .probe = vz89x_probe,
419 .id_table = vz89x_id,
421 module_i2c_driver(vz89x_driver);
423 MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
424 MODULE_DESCRIPTION("SGX Sensortech MiCS VZ89X VOC sensors");
425 MODULE_LICENSE("GPL v2");