[PATCH v2 8/8] iio: adc: stm32-dfsdm: add scaling support to dfsdm

Olivier MOYSAN olivier.moysan at foss.st.com
Wed Jun 26 09:15:15 PDT 2024


Hi Nuno,

On 6/26/24 10:59, Nuno Sá wrote:
> Hi Olivier,
> 
> One thing that I just noticed...
> 
> On Tue, 2024-06-25 at 17:07 +0200, Olivier Moysan wrote:
>> Add scaling support to STM32 DFSDM.
>>
>> When used in an analog context, a DFSDM filter typically converts the data
>> from a sigma delta modulator. The IIO device associated to the DFSDM
>> filter provides these data as raw data.
>> The IIO device can provide scaling information (voltage and offset) to
>> allow conversion of raw data into physical values.
>>
>> With the new binding based on IIO backend framework, the sigma delta
>> modulators are defined as backends providing scaling information.
>>
>> The scaling is not supported with legacy binding.
>>
>> Signed-off-by: Olivier Moysan <olivier.moysan at foss.st.com>
>> Acked-by: Nuno Sa <nuno.sa at analog.com>
>> ---
>>
> 
> ...
> 
>> +
>> +	case IIO_CHAN_INFO_SCALE:
>> +		/*
>> +		 * Scale is expressed in mV.
>> +		 * When fast mode is disabled, actual resolution may be lower
>> +		 * than 2^n, where n = realbits - 1.
>> +		 * This leads to underestimating the input voltage.
>> +		 * To compensate this deviation, the voltage reference can be
>> +		 * corrected with a factor = realbits resolution / actual max
>> +		 */
>> +		if (adc->backend[idx]) {
>> +			iio_backend_read_raw(adc->backend[idx], chan, val,
>> val2, mask);
> 
> Eve if it does not matter for your usecase, you should still do error handling
> as iio_backend_read_raw() can return an error.
> 

Ack. On the same occasion, I will switch to a dedicated API.

Olivier

>> +			*val = div_u64((u64)*val * (u64)BIT(DFSDM_DATA_RES -
>> 1), max);
>> +			*val2 = chan->scan_type.realbits;
>> +			if (chan->differential)
>> +				*val *= 2;
>> +		}
>> +		return IIO_VAL_FRACTIONAL_LOG2;
>> +
>> +	case IIO_CHAN_INFO_OFFSET:
>> +		/*
>> +		 * DFSDM output data are in the range [-2^n, 2^n],
>> +		 * with n = realbits - 1.
>> +		 * - Differential modulator:
>> +		 * Offset correspond to SD modulator offset.
>> +		 * - Single ended modulator:
>> +		 * Input is in [0V, Vref] range,
>> +		 * where 0V corresponds to -2^n, and Vref to 2^n.
>> +		 * Add 2^n to offset. (i.e. middle of input range)
>> +		 * offset = offset(sd) * vref / res(sd) * max / vref.
>> +		 */
>> +		if (adc->backend[idx]) {
>> +			iio_backend_read_raw(adc->backend[idx], chan, val,
>> val2, mask);
> 
> Same...
> 
> - Nuno Sá



More information about the linux-arm-kernel mailing list