1 // SPDX-License-Identifier: GPL-2.0+
3 * sgp40.c - Support for Sensirion SGP40 Gas Sensor
5 * Copyright (C) 2021 Andreas Klinger <ak@it-klinger.de>
7 * I2C slave address: 0x59
9 * Datasheet can be found here:
10 * https://www.sensirion.com/file/datasheet_sgp40
12 * There are two functionalities supported:
14 * 1) read raw logarithmic resistance value from sensor
15 * --> useful to pass it to the algorithm of the sensor vendor for
16 * measuring deteriorations and improvements of air quality.
18 * 2) calculate an estimated absolute voc index (0 - 500 index points) for
19 * measuring the air quality.
20 * For this purpose the value of the resistance for which the voc index
21 * will be 250 can be set up using calibbias.
23 * Compensation values of relative humidity and temperature can be set up
24 * by writing to the out values of temp and humidityrelative.
27 #include <linux/delay.h>
28 #include <linux/crc8.h>
29 #include <linux/module.h>
30 #include <linux/mutex.h>
31 #include <linux/i2c.h>
32 #include <linux/iio/iio.h>
35 * floating point calculation of voc is done as integer
36 * where numbers are multiplied by 1 << SGP40_CALC_POWER
38 #define SGP40_CALC_POWER 14
40 #define SGP40_CRC8_POLYNOMIAL 0x31
41 #define SGP40_CRC8_INIT 0xff
43 DECLARE_CRC8_TABLE(sgp40_crc8_table);
47 struct i2c_client *client;
51 /* Prevent concurrent access to rht, tmp, calibbias */
55 struct sgp40_tg_measure {
63 struct sgp40_tg_result {
68 static const struct iio_chan_spec sgp40_channels[] = {
70 .type = IIO_CONCENTRATION,
71 .channel2 = IIO_MOD_VOC,
72 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
75 .type = IIO_RESISTANCE,
76 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
77 BIT(IIO_CHAN_INFO_CALIBBIAS),
81 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
85 .type = IIO_HUMIDITYRELATIVE,
86 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
92 * taylor approximation of e^x:
93 * y = 1 + x + x^2 / 2 + x^3 / 6 + x^4 / 24 + ... + x^n / n!
95 * Because we are calculating x real value multiplied by 2^power we get
96 * an additional 2^power^n to divide for every element. For a reasonable
97 * precision this would overflow after a few iterations. Therefore we
98 * divide the x^n part whenever its about to overflow (xmax).
101 static u32 sgp40_exp(int exp, u32 power, u32 rounds)
104 u32 factorial, divider, xmax;
115 xmax = 0x7FFFFFFF / exp;
122 for (i = 1; i <= rounds; i++) {
125 y += (xp >> divider) / factorial;
127 /* divide when next multiplication would overflow */
135 return (1 << (power * 2)) / y;
140 static int sgp40_calc_voc(struct sgp40_data *data, u16 resistance_raw, int *voc)
145 /* we calculate as a multiple of 16384 (2^14) */
146 mutex_lock(&data->lock);
147 x = ((int)resistance_raw - data->res_calibbias) * 106;
148 mutex_unlock(&data->lock);
150 /* voc = 500 / (1 + e^x) */
151 exp = sgp40_exp(x, SGP40_CALC_POWER, 18);
152 *voc = 500 * ((1 << (SGP40_CALC_POWER * 2)) / ((1<<SGP40_CALC_POWER) + exp));
154 dev_dbg(data->dev, "raw: %d res_calibbias: %d x: %d exp: %d voc: %d\n",
155 resistance_raw, data->res_calibbias, x, exp, *voc);
160 static int sgp40_measure_resistance_raw(struct sgp40_data *data, u16 *resistance_raw)
163 struct i2c_client *client = data->client;
167 struct sgp40_tg_measure tg = {.command = {0x26, 0x0F}};
168 struct sgp40_tg_result tgres;
170 mutex_lock(&data->lock);
172 ticks = (data->rht / 10) * 65535 / 10000;
173 ticks16 = (u16)clamp(ticks, 0u, 65535u); /* clamp between 0 .. 100 %rH */
174 tg.rht_ticks = cpu_to_be16(ticks16);
175 tg.rht_crc = crc8(sgp40_crc8_table, (u8 *)&tg.rht_ticks, 2, SGP40_CRC8_INIT);
177 ticks = ((data->temp + 45000) / 10 ) * 65535 / 17500;
178 ticks16 = (u16)clamp(ticks, 0u, 65535u); /* clamp between -45 .. +130 °C */
179 tg.temp_ticks = cpu_to_be16(ticks16);
180 tg.temp_crc = crc8(sgp40_crc8_table, (u8 *)&tg.temp_ticks, 2, SGP40_CRC8_INIT);
182 mutex_unlock(&data->lock);
184 ret = i2c_master_send(client, (const char *)&tg, sizeof(tg));
185 if (ret != sizeof(tg)) {
186 dev_warn(data->dev, "i2c_master_send ret: %d sizeof: %zu\n", ret, sizeof(tg));
191 ret = i2c_master_recv(client, (u8 *)&tgres, sizeof(tgres));
194 if (ret != sizeof(tgres)) {
195 dev_warn(data->dev, "i2c_master_recv ret: %d sizeof: %zu\n", ret, sizeof(tgres));
199 crc = crc8(sgp40_crc8_table, (u8 *)&tgres.res_ticks, 2, SGP40_CRC8_INIT);
200 if (crc != tgres.res_crc) {
201 dev_err(data->dev, "CRC error while measure-raw\n");
205 *resistance_raw = be16_to_cpu(tgres.res_ticks);
210 static int sgp40_read_raw(struct iio_dev *indio_dev,
211 struct iio_chan_spec const *chan, int *val,
212 int *val2, long mask)
214 struct sgp40_data *data = iio_priv(indio_dev);
219 case IIO_CHAN_INFO_RAW:
220 switch (chan->type) {
222 ret = sgp40_measure_resistance_raw(data, &resistance_raw);
226 *val = resistance_raw;
229 mutex_lock(&data->lock);
231 mutex_unlock(&data->lock);
233 case IIO_HUMIDITYRELATIVE:
234 mutex_lock(&data->lock);
236 mutex_unlock(&data->lock);
241 case IIO_CHAN_INFO_PROCESSED:
242 ret = sgp40_measure_resistance_raw(data, &resistance_raw);
246 ret = sgp40_calc_voc(data, resistance_raw, &voc);
250 *val = voc / (1 << SGP40_CALC_POWER);
252 * calculation should fit into integer, where:
253 * voc <= (500 * 2^SGP40_CALC_POWER) = 8192000
254 * (with SGP40_CALC_POWER = 14)
256 *val2 = ((voc % (1 << SGP40_CALC_POWER)) * 244) / (1 << (SGP40_CALC_POWER - 12));
257 dev_dbg(data->dev, "voc: %d val: %d.%06d\n", voc, *val, *val2);
258 return IIO_VAL_INT_PLUS_MICRO;
259 case IIO_CHAN_INFO_CALIBBIAS:
260 mutex_lock(&data->lock);
261 *val = data->res_calibbias;
262 mutex_unlock(&data->lock);
269 static int sgp40_write_raw(struct iio_dev *indio_dev,
270 struct iio_chan_spec const *chan, int val,
273 struct sgp40_data *data = iio_priv(indio_dev);
276 case IIO_CHAN_INFO_RAW:
277 switch (chan->type) {
279 if ((val < -45000) || (val > 130000))
282 mutex_lock(&data->lock);
284 mutex_unlock(&data->lock);
286 case IIO_HUMIDITYRELATIVE:
287 if ((val < 0) || (val > 100000))
290 mutex_lock(&data->lock);
292 mutex_unlock(&data->lock);
297 case IIO_CHAN_INFO_CALIBBIAS:
298 if ((val < 20000) || (val > 52768))
301 mutex_lock(&data->lock);
302 data->res_calibbias = val;
303 mutex_unlock(&data->lock);
309 static const struct iio_info sgp40_info = {
310 .read_raw = sgp40_read_raw,
311 .write_raw = sgp40_write_raw,
314 static int sgp40_probe(struct i2c_client *client)
316 const struct i2c_device_id *id = i2c_client_get_device_id(client);
317 struct device *dev = &client->dev;
318 struct iio_dev *indio_dev;
319 struct sgp40_data *data;
322 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
326 data = iio_priv(indio_dev);
327 data->client = client;
330 crc8_populate_msb(sgp40_crc8_table, SGP40_CRC8_POLYNOMIAL);
332 mutex_init(&data->lock);
334 /* set default values */
335 data->rht = 50000; /* 50 % */
336 data->temp = 25000; /* 25 °C */
337 data->res_calibbias = 30000; /* resistance raw value for voc index of 250 */
339 indio_dev->info = &sgp40_info;
340 indio_dev->name = id->name;
341 indio_dev->modes = INDIO_DIRECT_MODE;
342 indio_dev->channels = sgp40_channels;
343 indio_dev->num_channels = ARRAY_SIZE(sgp40_channels);
345 ret = devm_iio_device_register(dev, indio_dev);
347 dev_err(dev, "failed to register iio device\n");
352 static const struct i2c_device_id sgp40_id[] = {
357 MODULE_DEVICE_TABLE(i2c, sgp40_id);
359 static const struct of_device_id sgp40_dt_ids[] = {
360 { .compatible = "sensirion,sgp40" },
364 MODULE_DEVICE_TABLE(of, sgp40_dt_ids);
366 static struct i2c_driver sgp40_driver = {
369 .of_match_table = sgp40_dt_ids,
371 .probe = sgp40_probe,
372 .id_table = sgp40_id,
374 module_i2c_driver(sgp40_driver);
376 MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>");
377 MODULE_DESCRIPTION("Sensirion SGP40 gas sensor");
378 MODULE_LICENSE("GPL v2");