Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * sgp40.c - Support for Sensirion SGP40 Gas Sensor
0004  *
0005  * Copyright (C) 2021 Andreas Klinger <ak@it-klinger.de>
0006  *
0007  * I2C slave address: 0x59
0008  *
0009  * Datasheet can be found here:
0010  * https://www.sensirion.com/file/datasheet_sgp40
0011  *
0012  * There are two functionalities supported:
0013  *
0014  * 1) read raw logarithmic resistance value from sensor
0015  *    --> useful to pass it to the algorithm of the sensor vendor for
0016  *    measuring deteriorations and improvements of air quality.
0017  *
0018  * 2) calculate an estimated absolute voc index (0 - 500 index points) for
0019  *    measuring the air quality.
0020  *    For this purpose the value of the resistance for which the voc index
0021  *    will be 250 can be set up using calibbias.
0022  *
0023  * Compensation values of relative humidity and temperature can be set up
0024  * by writing to the out values of temp and humidityrelative.
0025  */
0026 
0027 #include <linux/delay.h>
0028 #include <linux/crc8.h>
0029 #include <linux/module.h>
0030 #include <linux/mutex.h>
0031 #include <linux/i2c.h>
0032 #include <linux/iio/iio.h>
0033 
0034 /*
0035  * floating point calculation of voc is done as integer
0036  * where numbers are multiplied by 1 << SGP40_CALC_POWER
0037  */
0038 #define SGP40_CALC_POWER    14
0039 
0040 #define SGP40_CRC8_POLYNOMIAL   0x31
0041 #define SGP40_CRC8_INIT     0xff
0042 
0043 DECLARE_CRC8_TABLE(sgp40_crc8_table);
0044 
0045 struct sgp40_data {
0046     struct device       *dev;
0047     struct i2c_client   *client;
0048     int         rht;
0049     int         temp;
0050     int         res_calibbias;
0051     /* Prevent concurrent access to rht, tmp, calibbias */
0052     struct mutex        lock;
0053 };
0054 
0055 struct sgp40_tg_measure {
0056     u8  command[2];
0057     __be16  rht_ticks;
0058     u8  rht_crc;
0059     __be16  temp_ticks;
0060     u8  temp_crc;
0061 } __packed;
0062 
0063 struct sgp40_tg_result {
0064     __be16  res_ticks;
0065     u8  res_crc;
0066 } __packed;
0067 
0068 static const struct iio_chan_spec sgp40_channels[] = {
0069     {
0070         .type = IIO_CONCENTRATION,
0071         .channel2 = IIO_MOD_VOC,
0072         .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
0073     },
0074     {
0075         .type = IIO_RESISTANCE,
0076         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
0077             BIT(IIO_CHAN_INFO_CALIBBIAS),
0078     },
0079     {
0080         .type = IIO_TEMP,
0081         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
0082         .output = 1,
0083     },
0084     {
0085         .type = IIO_HUMIDITYRELATIVE,
0086         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
0087         .output = 1,
0088     },
0089 };
0090 
0091 /*
0092  * taylor approximation of e^x:
0093  * y = 1 + x + x^2 / 2 + x^3 / 6 + x^4 / 24 + ... + x^n / n!
0094  *
0095  * Because we are calculating x real value multiplied by 2^power we get
0096  * an additional 2^power^n to divide for every element. For a reasonable
0097  * precision this would overflow after a few iterations. Therefore we
0098  * divide the x^n part whenever its about to overflow (xmax).
0099  */
0100 
0101 static u32 sgp40_exp(int exp, u32 power, u32 rounds)
0102 {
0103         u32 x, y, xp;
0104         u32 factorial, divider, xmax;
0105         int sign = 1;
0106     int i;
0107 
0108         if (exp == 0)
0109                 return 1 << power;
0110         else if (exp < 0) {
0111                 sign = -1;
0112                 exp *= -1;
0113         }
0114 
0115         xmax = 0x7FFFFFFF / exp;
0116         x = exp;
0117         xp = 1;
0118         factorial = 1;
0119         y = 1 << power;
0120         divider = 0;
0121 
0122         for (i = 1; i <= rounds; i++) {
0123                 xp *= x;
0124                 factorial *= i;
0125                 y += (xp >> divider) / factorial;
0126                 divider += power;
0127                 /* divide when next multiplication would overflow */
0128                 if (xp >= xmax) {
0129                         xp >>= power;
0130                         divider -= power;
0131                 }
0132         }
0133 
0134         if (sign == -1)
0135                 return (1 << (power * 2)) / y;
0136         else
0137                 return y;
0138 }
0139 
0140 static int sgp40_calc_voc(struct sgp40_data *data, u16 resistance_raw, int *voc)
0141 {
0142     int x;
0143     u32 exp = 0;
0144 
0145     /* we calculate as a multiple of 16384 (2^14) */
0146     mutex_lock(&data->lock);
0147     x = ((int)resistance_raw - data->res_calibbias) * 106;
0148     mutex_unlock(&data->lock);
0149 
0150     /* voc = 500 / (1 + e^x) */
0151     exp = sgp40_exp(x, SGP40_CALC_POWER, 18);
0152     *voc = 500 * ((1 << (SGP40_CALC_POWER * 2)) / ((1<<SGP40_CALC_POWER) + exp));
0153 
0154     dev_dbg(data->dev, "raw: %d res_calibbias: %d x: %d exp: %d voc: %d\n",
0155                 resistance_raw, data->res_calibbias, x, exp, *voc);
0156 
0157     return 0;
0158 }
0159 
0160 static int sgp40_measure_resistance_raw(struct sgp40_data *data, u16 *resistance_raw)
0161 {
0162     int ret;
0163     struct i2c_client *client = data->client;
0164     u32 ticks;
0165     u16 ticks16;
0166     u8 crc;
0167     struct sgp40_tg_measure tg = {.command = {0x26, 0x0F}};
0168     struct sgp40_tg_result tgres;
0169 
0170     mutex_lock(&data->lock);
0171 
0172     ticks = (data->rht / 10) * 65535 / 10000;
0173     ticks16 = (u16)clamp(ticks, 0u, 65535u); /* clamp between 0 .. 100 %rH */
0174     tg.rht_ticks = cpu_to_be16(ticks16);
0175     tg.rht_crc = crc8(sgp40_crc8_table, (u8 *)&tg.rht_ticks, 2, SGP40_CRC8_INIT);
0176 
0177     ticks = ((data->temp + 45000) / 10 ) * 65535 / 17500;
0178     ticks16 = (u16)clamp(ticks, 0u, 65535u); /* clamp between -45 .. +130 °C */
0179     tg.temp_ticks = cpu_to_be16(ticks16);
0180     tg.temp_crc = crc8(sgp40_crc8_table, (u8 *)&tg.temp_ticks, 2, SGP40_CRC8_INIT);
0181 
0182     mutex_unlock(&data->lock);
0183 
0184     ret = i2c_master_send(client, (const char *)&tg, sizeof(tg));
0185     if (ret != sizeof(tg)) {
0186         dev_warn(data->dev, "i2c_master_send ret: %d sizeof: %zu\n", ret, sizeof(tg));
0187         return -EIO;
0188     }
0189     msleep(30);
0190 
0191     ret = i2c_master_recv(client, (u8 *)&tgres, sizeof(tgres));
0192     if (ret < 0)
0193         return ret;
0194     if (ret != sizeof(tgres)) {
0195         dev_warn(data->dev, "i2c_master_recv ret: %d sizeof: %zu\n", ret, sizeof(tgres));
0196         return -EIO;
0197     }
0198 
0199     crc = crc8(sgp40_crc8_table, (u8 *)&tgres.res_ticks, 2, SGP40_CRC8_INIT);
0200     if (crc != tgres.res_crc) {
0201         dev_err(data->dev, "CRC error while measure-raw\n");
0202         return -EIO;
0203     }
0204 
0205     *resistance_raw = be16_to_cpu(tgres.res_ticks);
0206 
0207     return 0;
0208 }
0209 
0210 static int sgp40_read_raw(struct iio_dev *indio_dev,
0211             struct iio_chan_spec const *chan, int *val,
0212             int *val2, long mask)
0213 {
0214     struct sgp40_data *data = iio_priv(indio_dev);
0215     int ret, voc;
0216     u16 resistance_raw;
0217 
0218     switch (mask) {
0219     case IIO_CHAN_INFO_RAW:
0220         switch (chan->type) {
0221         case IIO_RESISTANCE:
0222             ret = sgp40_measure_resistance_raw(data, &resistance_raw);
0223             if (ret)
0224                 return ret;
0225 
0226             *val = resistance_raw;
0227             return IIO_VAL_INT;
0228         case IIO_TEMP:
0229             mutex_lock(&data->lock);
0230             *val = data->temp;
0231             mutex_unlock(&data->lock);
0232             return IIO_VAL_INT;
0233         case IIO_HUMIDITYRELATIVE:
0234             mutex_lock(&data->lock);
0235             *val = data->rht;
0236             mutex_unlock(&data->lock);
0237             return IIO_VAL_INT;
0238         default:
0239             return -EINVAL;
0240         }
0241     case IIO_CHAN_INFO_PROCESSED:
0242         ret = sgp40_measure_resistance_raw(data, &resistance_raw);
0243         if (ret)
0244             return ret;
0245 
0246         ret = sgp40_calc_voc(data, resistance_raw, &voc);
0247         if (ret)
0248             return ret;
0249 
0250         *val = voc / (1 << SGP40_CALC_POWER);
0251         /*
0252          * calculation should fit into integer, where:
0253          * voc <= (500 * 2^SGP40_CALC_POWER) = 8192000
0254          * (with SGP40_CALC_POWER = 14)
0255          */
0256         *val2 = ((voc % (1 << SGP40_CALC_POWER)) * 244) / (1 << (SGP40_CALC_POWER - 12));
0257         dev_dbg(data->dev, "voc: %d val: %d.%06d\n", voc, *val, *val2);
0258         return IIO_VAL_INT_PLUS_MICRO;
0259     case IIO_CHAN_INFO_CALIBBIAS:
0260         mutex_lock(&data->lock);
0261         *val = data->res_calibbias;
0262         mutex_unlock(&data->lock);
0263         return IIO_VAL_INT;
0264     default:
0265         return -EINVAL;
0266     }
0267 }
0268 
0269 static int sgp40_write_raw(struct iio_dev *indio_dev,
0270             struct iio_chan_spec const *chan, int val,
0271             int val2, long mask)
0272 {
0273     struct sgp40_data *data = iio_priv(indio_dev);
0274 
0275     switch (mask) {
0276     case IIO_CHAN_INFO_RAW:
0277         switch (chan->type) {
0278         case IIO_TEMP:
0279             if ((val < -45000) || (val > 130000))
0280                 return -EINVAL;
0281 
0282             mutex_lock(&data->lock);
0283             data->temp = val;
0284             mutex_unlock(&data->lock);
0285             return 0;
0286         case IIO_HUMIDITYRELATIVE:
0287             if ((val < 0) || (val > 100000))
0288                 return -EINVAL;
0289 
0290             mutex_lock(&data->lock);
0291             data->rht = val;
0292             mutex_unlock(&data->lock);
0293             return 0;
0294         default:
0295             return -EINVAL;
0296         }
0297     case IIO_CHAN_INFO_CALIBBIAS:
0298         if ((val < 20000) || (val > 52768))
0299             return -EINVAL;
0300 
0301         mutex_lock(&data->lock);
0302         data->res_calibbias = val;
0303         mutex_unlock(&data->lock);
0304         return 0;
0305     }
0306     return -EINVAL;
0307 }
0308 
0309 static const struct iio_info sgp40_info = {
0310     .read_raw   = sgp40_read_raw,
0311     .write_raw  = sgp40_write_raw,
0312 };
0313 
0314 static int sgp40_probe(struct i2c_client *client,
0315              const struct i2c_device_id *id)
0316 {
0317     struct device *dev = &client->dev;
0318     struct iio_dev *indio_dev;
0319     struct sgp40_data *data;
0320     int ret;
0321 
0322     indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
0323     if (!indio_dev)
0324         return -ENOMEM;
0325 
0326     data = iio_priv(indio_dev);
0327     data->client = client;
0328     data->dev = dev;
0329 
0330     crc8_populate_msb(sgp40_crc8_table, SGP40_CRC8_POLYNOMIAL);
0331 
0332     mutex_init(&data->lock);
0333 
0334     /* set default values */
0335     data->rht = 50000;      /* 50 % */
0336     data->temp = 25000;     /* 25 °C */
0337     data->res_calibbias = 30000;    /* resistance raw value for voc index of 250 */
0338 
0339     indio_dev->info = &sgp40_info;
0340     indio_dev->name = id->name;
0341     indio_dev->modes = INDIO_DIRECT_MODE;
0342     indio_dev->channels = sgp40_channels;
0343     indio_dev->num_channels = ARRAY_SIZE(sgp40_channels);
0344 
0345     ret = devm_iio_device_register(dev, indio_dev);
0346     if (ret)
0347         dev_err(dev, "failed to register iio device\n");
0348 
0349     return ret;
0350 }
0351 
0352 static const struct i2c_device_id sgp40_id[] = {
0353     { "sgp40" },
0354     { }
0355 };
0356 
0357 MODULE_DEVICE_TABLE(i2c, sgp40_id);
0358 
0359 static const struct of_device_id sgp40_dt_ids[] = {
0360     { .compatible = "sensirion,sgp40" },
0361     { }
0362 };
0363 
0364 MODULE_DEVICE_TABLE(of, sgp40_dt_ids);
0365 
0366 static struct i2c_driver sgp40_driver = {
0367     .driver = {
0368         .name = "sgp40",
0369         .of_match_table = sgp40_dt_ids,
0370     },
0371     .probe = sgp40_probe,
0372     .id_table = sgp40_id,
0373 };
0374 module_i2c_driver(sgp40_driver);
0375 
0376 MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>");
0377 MODULE_DESCRIPTION("Sensirion SGP40 gas sensor");
0378 MODULE_LICENSE("GPL v2");