Re: [PATCH v3 3/7] iio: chemical: bme680: Add triggered buffer support

Jonathan Cameron
2024-11-02T15:25:00Z

On Sat, 2 Nov 2024 14:13:07 +0100
Vasileios Amoiridis <vassilisamir@gmail.com> wrote:

> Add triggered buffer and soft timestamp support. The available scan mask
> enables all the channels of the sensor in order to follow the operation of
> the sensor. The sensor basically starts to capture from all channels
> as long as it enters into FORCED mode.
>
> The bulk read, reads a total of 15 registers from the sensor, 0x1D..0x2B.
> Even though some of those registers are not reported in the register map
> of the device, this is how the BME680 Sensor API [1] proposes to do it.
> This allows to have one bulk read instead of multiple ones.
>
> Link: https://github.com/boschsensortec/BME68x_SensorAPI/blob/v4.4.8/bme68x.c#L1200
> Signed-off-by: Vasileios Amoiridis <vassilisamir@gmail.com>
Applied with a couple of (to me) superfluous comments dropped.
> +static irqreturn_t bme680_trigger_handler(int irq, void *p)
> +{
> + struct iio_poll_func *pf = p;
> + struct iio_dev *indio_dev = pf->indio_dev;
> + struct bme680_data *data = iio_priv(indio_dev);
> + struct device *dev = regmap_get_device(data->regmap);
> + u32 adc_temp, adc_press, adc_humid;
> + u16 adc_gas_res, gas_regs_val;
> + u8 gas_range;
> + s32 t_fine;
> + int ret;
> +
> + guard(mutex)(&data->lock);
> +
> + ret = bme680_set_mode(data, BME680_MODE_FORCED);
> + if (ret < 0)
> + goto out;
> +
> + ret = bme680_wait_for_eoc(data);
> + if (ret)
> + goto out;
> +
> + /* Burst read data regs */
This one dropped as kind of obvious from the code.
> + ret = regmap_bulk_read(data->regmap, BME680_REG_MEAS_STAT_0,
> + data->buf, sizeof(data->buf));
> + if (ret) {
> + dev_err(dev, "failed to burst read sensor data\n");
> + goto out;
> + }
> + if (data->buf[0] & BME680_GAS_MEAS_BIT) {
> + dev_err(dev, "gas measurement incomplete\n");
> + goto out;
> + }
> +
> + /* Temperature calculations */
> + adc_temp = FIELD_GET(BME680_MEAS_TRIM_MASK, get_unaligned_be24(&data->buf[5]));
> + if (adc_temp == BME680_MEAS_SKIPPED) {
> + dev_err(dev, "reading temperature skipped\n");
> + goto out;
> + }
> + data->scan.chan[0] = bme680_compensate_temp(data, adc_temp);
> + t_fine = bme680_calc_t_fine(data, adc_temp);
> +
> + /* Pressure calculations */
> + adc_press = FIELD_GET(BME680_MEAS_TRIM_MASK, get_unaligned_be24(&data->buf[2]));
> + if (adc_press == BME680_MEAS_SKIPPED) {
> + dev_err(dev, "reading pressure skipped\n");
> + goto out;
> + }
> + data->scan.chan[1] = bme680_compensate_press(data, adc_press, t_fine);
> +
> + /* Humidity calculations */
> + adc_humid = get_unaligned_be16(&data->buf[8]);
> + if (adc_humid == BME680_MEAS_SKIPPED) {
> + dev_err(dev, "reading humidity skipped\n");
> + goto out;
> + }
> + data->scan.chan[2] = bme680_compensate_humid(data, adc_humid, t_fine);
> +
> + /* Gas calculations */
> + gas_regs_val = get_unaligned_be16(&data->buf[13]);
> + adc_gas_res = FIELD_GET(BME680_ADC_GAS_RES, gas_regs_val);
> + if ((gas_regs_val & BME680_GAS_STAB_BIT) == 0) {
> + dev_err(dev, "heater failed to reach the target temperature\n");
> + goto out;
> + }
> + gas_range = FIELD_GET(BME680_GAS_RANGE_MASK, gas_regs_val);
> + data->scan.chan[3] = bme680_compensate_gas(data, adc_gas_res, gas_range);
> +
> + /* Push to buffer */
This one is extremely obvious so dropped.

> + iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
> + iio_get_time_ns(indio_dev));
> +out:
> + iio_trigger_notify_done(indio_dev->trig);
> + return IRQ_HANDLED;
> +}
> +