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/iio/iio.h>
24 #include <linux/iio/sysfs.h>
26 #define VZ89X_REG_MEASUREMENT 0x09
27 #define VZ89X_REG_MEASUREMENT_SIZE 6
29 #define VZ89X_VOC_CO2_IDX 0
30 #define VZ89X_VOC_SHORT_IDX 1
31 #define VZ89X_VOC_TVOC_IDX 2
32 #define VZ89X_VOC_RESISTANCE_IDX 3
35 struct i2c_client
*client
;
37 int (*xfer
)(struct vz89x_data
*data
, u8 cmd
);
39 unsigned long last_update
;
40 u8 buffer
[VZ89X_REG_MEASUREMENT_SIZE
];
43 static const struct iio_chan_spec vz89x_channels
[] = {
45 .type
= IIO_CONCENTRATION
,
46 .channel2
= IIO_MOD_CO2
,
49 BIT(IIO_CHAN_INFO_OFFSET
) | BIT(IIO_CHAN_INFO_RAW
),
50 .address
= VZ89X_VOC_CO2_IDX
,
53 .type
= IIO_CONCENTRATION
,
54 .channel2
= IIO_MOD_VOC
,
56 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
57 .address
= VZ89X_VOC_SHORT_IDX
,
58 .extend_name
= "short",
61 .type
= IIO_CONCENTRATION
,
62 .channel2
= IIO_MOD_VOC
,
65 BIT(IIO_CHAN_INFO_OFFSET
) | BIT(IIO_CHAN_INFO_RAW
),
66 .address
= VZ89X_VOC_TVOC_IDX
,
69 .type
= IIO_RESISTANCE
,
71 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
72 .address
= VZ89X_VOC_RESISTANCE_IDX
,
76 static IIO_CONST_ATTR(in_concentration_co2_scale
, "0.00000698689");
77 static IIO_CONST_ATTR(in_concentration_voc_scale
, "0.00000000436681223");
79 static struct attribute
*vz89x_attributes
[] = {
80 &iio_const_attr_in_concentration_co2_scale
.dev_attr
.attr
,
81 &iio_const_attr_in_concentration_voc_scale
.dev_attr
.attr
,
85 static const struct attribute_group vz89x_attrs_group
= {
86 .attrs
= vz89x_attributes
,
90 * Chipset sometime updates in the middle of a reading causing it to reset the
91 * data pointer, and causing invalid reading of previous data.
92 * We can check for this by reading MSB of the resistance reading that is
93 * always zero, and by also confirming the VOC_short isn't zero.
96 static int vz89x_measurement_is_valid(struct vz89x_data
*data
)
98 if (data
->buffer
[VZ89X_VOC_SHORT_IDX
] == 0)
101 return !!(data
->buffer
[VZ89X_REG_MEASUREMENT_SIZE
- 1] > 0);
104 static int vz89x_i2c_xfer(struct vz89x_data
*data
, u8 cmd
)
106 struct i2c_client
*client
= data
->client
;
107 struct i2c_msg msg
[2];
109 u8 buf
[3] = { cmd
, 0, 0};
111 msg
[0].addr
= client
->addr
;
112 msg
[0].flags
= client
->flags
;
114 msg
[0].buf
= (char *) &buf
;
116 msg
[1].addr
= client
->addr
;
117 msg
[1].flags
= client
->flags
| I2C_M_RD
;
118 msg
[1].len
= VZ89X_REG_MEASUREMENT_SIZE
;
119 msg
[1].buf
= (char *) &data
->buffer
;
121 ret
= i2c_transfer(client
->adapter
, msg
, 2);
123 return (ret
== 2) ? 0 : ret
;
126 static int vz89x_smbus_xfer(struct vz89x_data
*data
, u8 cmd
)
128 struct i2c_client
*client
= data
->client
;
132 ret
= i2c_smbus_write_word_data(client
, cmd
, 0);
136 for (i
= 0; i
< VZ89X_REG_MEASUREMENT_SIZE
; i
++) {
137 ret
= i2c_smbus_read_byte(client
);
140 data
->buffer
[i
] = ret
;
146 static int vz89x_get_measurement(struct vz89x_data
*data
)
150 /* sensor can only be polled once a second max per datasheet */
151 if (!time_after(jiffies
, data
->last_update
+ HZ
))
154 ret
= data
->xfer(data
, VZ89X_REG_MEASUREMENT
);
158 ret
= vz89x_measurement_is_valid(data
);
162 data
->last_update
= jiffies
;
167 static int vz89x_get_resistance_reading(struct vz89x_data
*data
)
169 u8
*buf
= &data
->buffer
[VZ89X_VOC_RESISTANCE_IDX
];
171 return buf
[0] | (buf
[1] << 8);
174 static int vz89x_read_raw(struct iio_dev
*indio_dev
,
175 struct iio_chan_spec
const *chan
, int *val
,
176 int *val2
, long mask
)
178 struct vz89x_data
*data
= iio_priv(indio_dev
);
182 case IIO_CHAN_INFO_RAW
:
183 mutex_lock(&data
->lock
);
184 ret
= vz89x_get_measurement(data
);
185 mutex_unlock(&data
->lock
);
190 switch (chan
->address
) {
191 case VZ89X_VOC_CO2_IDX
:
192 case VZ89X_VOC_SHORT_IDX
:
193 case VZ89X_VOC_TVOC_IDX
:
194 *val
= data
->buffer
[chan
->address
];
196 case VZ89X_VOC_RESISTANCE_IDX
:
197 *val
= vz89x_get_resistance_reading(data
);
203 case IIO_CHAN_INFO_SCALE
:
204 switch (chan
->type
) {
212 case IIO_CHAN_INFO_OFFSET
:
213 switch (chan
->address
) {
214 case VZ89X_VOC_CO2_IDX
:
217 return IIO_VAL_INT_PLUS_MICRO
;
218 case VZ89X_VOC_TVOC_IDX
:
229 static const struct iio_info vz89x_info
= {
230 .attrs
= &vz89x_attrs_group
,
231 .read_raw
= vz89x_read_raw
,
232 .driver_module
= THIS_MODULE
,
235 static int vz89x_probe(struct i2c_client
*client
,
236 const struct i2c_device_id
*id
)
238 struct iio_dev
*indio_dev
;
239 struct vz89x_data
*data
;
241 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
244 data
= iio_priv(indio_dev
);
246 if (i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
))
247 data
->xfer
= vz89x_i2c_xfer
;
248 else if (i2c_check_functionality(client
->adapter
,
249 I2C_FUNC_SMBUS_WORD_DATA
| I2C_FUNC_SMBUS_BYTE
))
250 data
->xfer
= vz89x_smbus_xfer
;
254 i2c_set_clientdata(client
, indio_dev
);
255 data
->client
= client
;
256 data
->last_update
= jiffies
- HZ
;
257 mutex_init(&data
->lock
);
259 indio_dev
->dev
.parent
= &client
->dev
;
260 indio_dev
->info
= &vz89x_info
,
261 indio_dev
->name
= dev_name(&client
->dev
);
262 indio_dev
->modes
= INDIO_DIRECT_MODE
;
264 indio_dev
->channels
= vz89x_channels
;
265 indio_dev
->num_channels
= ARRAY_SIZE(vz89x_channels
);
267 return devm_iio_device_register(&client
->dev
, indio_dev
);
270 static const struct i2c_device_id vz89x_id
[] = {
274 MODULE_DEVICE_TABLE(i2c
, vz89x_id
);
276 static const struct of_device_id vz89x_dt_ids
[] = {
277 { .compatible
= "sgx,vz89x" },
280 MODULE_DEVICE_TABLE(of
, vz89x_dt_ids
);
282 static struct i2c_driver vz89x_driver
= {
285 .of_match_table
= of_match_ptr(vz89x_dt_ids
),
287 .probe
= vz89x_probe
,
288 .id_table
= vz89x_id
,
290 module_i2c_driver(vz89x_driver
);
292 MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
293 MODULE_DESCRIPTION("SGX Sensortech MiCS VZ89X VOC sensors");
294 MODULE_LICENSE("GPL v2");