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>
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
40 struct vz89x_chip_data
;
43 struct i2c_client
*client
;
44 const struct vz89x_chip_data
*chip
;
46 int (*xfer
)(struct vz89x_data
*data
, u8 cmd
);
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
;
63 static const struct iio_chan_spec vz89x_channels
[] = {
65 .type
= IIO_CONCENTRATION
,
66 .channel2
= IIO_MOD_CO2
,
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
,
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
,
85 BIT(IIO_CHAN_INFO_OFFSET
) | BIT(IIO_CHAN_INFO_RAW
),
86 .address
= VZ89X_VOC_TVOC_IDX
,
89 .type
= IIO_RESISTANCE
,
91 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
92 .address
= VZ89X_VOC_RESISTANCE_IDX
,
100 static const struct iio_chan_spec vz89te_channels
[] = {
102 .type
= IIO_CONCENTRATION
,
103 .channel2
= IIO_MOD_VOC
,
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
,
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
,
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
,
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)
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
)
164 for (i
= 0; i
< (data
->chip
->read_size
- 1); i
++) {
165 sum
= crc
+ data
->buffer
[i
];
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];
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
;
202 ret
= i2c_smbus_write_word_data(client
, cmd
, 0);
206 for (i
= 0; i
< data
->chip
->read_size
; i
++) {
207 ret
= i2c_smbus_read_byte(client
);
210 data
->buffer
[i
] = ret
;
216 static int vz89x_get_measurement(struct vz89x_data
*data
)
218 const struct vz89x_chip_data
*chip
= data
->chip
;
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
);
232 ret
= chip
->valid(data
);
236 data
->is_valid
= true;
241 static int vz89x_get_resistance_reading(struct vz89x_data
*data
,
242 struct iio_chan_spec
const *chan
,
245 u8
*tmp
= (u8
*) &data
->buffer
[chan
->address
];
247 switch (chan
->scan_type
.endianness
) {
249 *val
= le32_to_cpup((__le32
*) tmp
) & GENMASK(23, 0);
252 *val
= be32_to_cpup((__be32
*) tmp
) >> 8;
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
);
269 case IIO_CHAN_INFO_RAW
:
270 mutex_lock(&data
->lock
);
271 ret
= vz89x_get_measurement(data
);
272 mutex_unlock(&data
->lock
);
277 switch (chan
->type
) {
278 case IIO_CONCENTRATION
:
279 *val
= data
->buffer
[chan
->address
];
282 ret
= vz89x_get_resistance_reading(data
, chan
, val
);
290 case IIO_CHAN_INFO_SCALE
:
291 switch (chan
->type
) {
299 case IIO_CHAN_INFO_OFFSET
:
300 switch (chan
->channel2
) {
304 return IIO_VAL_INT_PLUS_MICRO
;
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
;
359 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
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
;
372 if (!dev_fwnode(dev
))
373 chip_id
= id
->driver_data
;
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
[] = {
395 { "vz89te", VZ89TE
},
398 MODULE_DEVICE_TABLE(i2c
, vz89x_id
);
400 static struct i2c_driver vz89x_driver
= {
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");