Skip to content

Commit 4ab4d84

Browse files
committed
iio: adc: at91-sama5d2_adc: adjust osr based on specific platform data
ADC captures data on 12 bits (if oversampling is not enabled). When using oversampling captured data could go up to 14 bits for SAMA5D2 or up to 16 bits for SAMA7G5 (depending on oversampling settings). All the channels that are subject of oversampling are registered as 14 or 16 real bits. Depending on the oversampling settings the ADC converted value need to be shifted up to 14 or 16 to cope with realbits value registered to IIO subsystem. Commit adds platform specific information to know if we run on a system with up to 14 or 16 bits ADC converted data. Signed-off-by: Claudiu Beznea <claudiu.beznea@microchip.com>
1 parent 328bc61 commit 4ab4d84

1 file changed

Lines changed: 19 additions & 13 deletions

File tree

drivers/iio/adc/at91-sama5d2_adc.c

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ static const struct at91_adc_reg_layout sama7g5_layout = {
404404
* @hw_trig_cnt: number of possible hardware triggers
405405
* @osr_mask: oversampling ratio bitmask on EMR register
406406
* @osr_vals: available oversampling rates
407+
* @chan_realbits: realbits for registered channels
407408
*/
408409
struct at91_adc_platform {
409410
const struct at91_adc_reg_layout *layout;
@@ -417,6 +418,7 @@ struct at91_adc_platform {
417418
unsigned int hw_trig_cnt;
418419
unsigned int osr_mask;
419420
unsigned int osr_vals;
421+
unsigned int chan_realbits;
420422
};
421423

422424
/**
@@ -619,6 +621,7 @@ static const struct at91_adc_platform sama5d2_platform = {
619621
.osr_vals = BIT(AT91_SAMA5D2_EMR_OSR_1SAMPLES) |
620622
BIT(AT91_SAMA5D2_EMR_OSR_4SAMPLES) |
621623
BIT(AT91_SAMA5D2_EMR_OSR_16SAMPLES),
624+
.chan_realbits = 14,
622625
};
623626

624627
static const struct at91_adc_platform sama7g5_platform = {
@@ -638,6 +641,7 @@ static const struct at91_adc_platform sama7g5_platform = {
638641
.osr_vals = BIT(AT91_SAMA5D2_EMR_OSR_1SAMPLES) |
639642
BIT(AT91_SAMA5D2_EMR_OSR_4SAMPLES) |
640643
BIT(AT91_SAMA5D2_EMR_OSR_16SAMPLES),
644+
.chan_realbits = 16,
641645
};
642646

643647
static int at91_adc_chan_xlate(struct iio_dev *indio_dev, int chan)
@@ -779,19 +783,21 @@ static int at91_adc_config_emr(struct at91_adc_state *st,
779783

780784
static int at91_adc_adjust_val_osr(struct at91_adc_state *st, int *val)
781785
{
782-
if (st->oversampling_ratio == AT91_OSR_1SAMPLES) {
783-
/*
784-
* in this case we only have 12 bits of real data, but channel
785-
* is registered as 14 bits, so shift left two bits
786-
*/
787-
*val <<= 2;
788-
} else if (st->oversampling_ratio == AT91_OSR_4SAMPLES) {
789-
/*
790-
* in this case we have 13 bits of real data, but channel
791-
* is registered as 14 bits, so left shift one bit
792-
*/
793-
*val <<= 1;
794-
}
786+
int nbits, diff;
787+
788+
if (st->oversampling_ratio == AT91_OSR_1SAMPLES)
789+
nbits = 12;
790+
else if (st->oversampling_ratio == AT91_OSR_4SAMPLES)
791+
nbits = 13;
792+
else if (st->oversampling_ratio == AT91_OSR_16SAMPLES)
793+
nbits = 14;
794+
795+
/*
796+
* We have nbits of real data and channel is registered as
797+
* st->soc_info.platform->chan_realbits, so shift left diff bits.
798+
*/
799+
diff = st->soc_info.platform->chan_realbits - nbits;
800+
*val <<= diff;
795801

796802
return IIO_VAL_INT;
797803
}

0 commit comments

Comments
 (0)