@@ -138,8 +138,7 @@ struct at91_adc_reg_layout {
138138/* Extended Mode Register */
139139 u16 EMR ;
140140/* Extended Mode Register - Oversampling rate */
141- #define AT91_SAMA5D2_EMR_OSR (V ) ((V) << 16)
142- #define AT91_SAMA5D2_EMR_OSR_MASK GENMASK(17, 16)
141+ #define AT91_SAMA5D2_EMR_OSR (V , M ) (((V) << 16) & (M))
143142#define AT91_SAMA5D2_EMR_OSR_1SAMPLES 0
144143#define AT91_SAMA5D2_EMR_OSR_4SAMPLES 1
145144#define AT91_SAMA5D2_EMR_OSR_16SAMPLES 2
@@ -403,6 +402,8 @@ static const struct at91_adc_reg_layout sama7g5_layout = {
403402 * @max_index: highest channel index (highest index may be higher
404403 * than the total channel number)
405404 * @hw_trig_cnt: number of possible hardware triggers
405+ * @osr_mask: oversampling ratio bitmask on EMR register
406+ * @osr_vals: available oversampling rates
406407 */
407408struct at91_adc_platform {
408409 const struct at91_adc_reg_layout * layout ;
@@ -414,6 +415,8 @@ struct at91_adc_platform {
414415 unsigned int max_channels ;
415416 unsigned int max_index ;
416417 unsigned int hw_trig_cnt ;
418+ unsigned int osr_mask ;
419+ unsigned int osr_vals ;
417420};
418421
419422/**
@@ -612,6 +615,10 @@ static const struct at91_adc_platform sama5d2_platform = {
612615 .max_index = AT91_SAMA5D2_MAX_CHAN_IDX ,
613616#define AT91_SAMA5D2_HW_TRIG_CNT 3
614617 .hw_trig_cnt = AT91_SAMA5D2_HW_TRIG_CNT ,
618+ .osr_mask = GENMASK (17 , 16 ),
619+ .osr_vals = BIT (AT91_SAMA5D2_EMR_OSR_1SAMPLES ) |
620+ BIT (AT91_SAMA5D2_EMR_OSR_4SAMPLES ) |
621+ BIT (AT91_SAMA5D2_EMR_OSR_16SAMPLES ),
615622};
616623
617624static const struct at91_adc_platform sama7g5_platform = {
@@ -627,6 +634,10 @@ static const struct at91_adc_platform sama7g5_platform = {
627634 .max_index = AT91_SAMA7G5_MAX_CHAN_IDX ,
628635#define AT91_SAMA7G5_HW_TRIG_CNT 3
629636 .hw_trig_cnt = AT91_SAMA7G5_HW_TRIG_CNT ,
637+ .osr_mask = GENMASK (18 , 16 ),
638+ .osr_vals = BIT (AT91_SAMA5D2_EMR_OSR_1SAMPLES ) |
639+ BIT (AT91_SAMA5D2_EMR_OSR_4SAMPLES ) |
640+ BIT (AT91_SAMA5D2_EMR_OSR_16SAMPLES ),
630641};
631642
632643static int at91_adc_chan_xlate (struct iio_dev * indio_dev , int chan )
@@ -725,34 +736,45 @@ static void at91_adc_eoc_ena(struct at91_adc_state *st, unsigned int channel)
725736 at91_adc_writel (st , EOC_IER , BIT (channel ));
726737}
727738
728- static void at91_adc_config_emr (struct at91_adc_state * st )
739+ static int at91_adc_config_emr (struct at91_adc_state * st ,
740+ u32 oversampling_ratio )
729741{
730742 /* configure the extended mode register */
731743 unsigned int emr = at91_adc_readl (st , EMR );
744+ unsigned int osr_mask = st -> soc_info .platform -> osr_mask ;
745+ unsigned int osr_vals = st -> soc_info .platform -> osr_vals ;
732746
733747 /* select oversampling per single trigger event */
734748 emr |= AT91_SAMA5D2_EMR_ASTE (1 );
735749
736750 /* delete leftover content if it's the case */
737- emr &= ~AT91_SAMA5D2_EMR_OSR_MASK ;
751+ emr &= ~osr_mask ;
738752
739753 /* select oversampling ratio from configuration */
740- switch (st -> oversampling_ratio ) {
754+ switch (oversampling_ratio ) {
741755 case AT91_OSR_1SAMPLES :
742- emr |= AT91_SAMA5D2_EMR_OSR (AT91_SAMA5D2_EMR_OSR_1SAMPLES ) &
743- AT91_SAMA5D2_EMR_OSR_MASK ;
756+ if (!(osr_vals & BIT (AT91_SAMA5D2_EMR_OSR_1SAMPLES )))
757+ return - EINVAL ;
758+ emr |= AT91_SAMA5D2_EMR_OSR (AT91_SAMA5D2_EMR_OSR_1SAMPLES ,
759+ osr_mask );
744760 break ;
745761 case AT91_OSR_4SAMPLES :
746- emr |= AT91_SAMA5D2_EMR_OSR (AT91_SAMA5D2_EMR_OSR_4SAMPLES ) &
747- AT91_SAMA5D2_EMR_OSR_MASK ;
762+ if (!(osr_vals & BIT (AT91_SAMA5D2_EMR_OSR_4SAMPLES )))
763+ return - EINVAL ;
764+ emr |= AT91_SAMA5D2_EMR_OSR (AT91_SAMA5D2_EMR_OSR_4SAMPLES ,
765+ osr_mask );
748766 break ;
749767 case AT91_OSR_16SAMPLES :
750- emr |= AT91_SAMA5D2_EMR_OSR (AT91_SAMA5D2_EMR_OSR_16SAMPLES ) &
751- AT91_SAMA5D2_EMR_OSR_MASK ;
768+ if (!(osr_vals & BIT (AT91_SAMA5D2_EMR_OSR_16SAMPLES )))
769+ return - EINVAL ;
770+ emr |= AT91_SAMA5D2_EMR_OSR (AT91_SAMA5D2_EMR_OSR_16SAMPLES ,
771+ osr_mask );
752772 break ;
753773 }
754774
755775 at91_adc_writel (st , EMR , emr );
776+
777+ return 0 ;
756778}
757779
758780static int at91_adc_adjust_val_osr (struct at91_adc_state * st , int * val )
@@ -1637,6 +1659,7 @@ static int at91_adc_write_raw(struct iio_dev *indio_dev,
16371659 int val , int val2 , long mask )
16381660{
16391661 struct at91_adc_state * st = iio_priv (indio_dev );
1662+ int ret ;
16401663
16411664 switch (mask ) {
16421665 case IIO_CHAN_INFO_OVERSAMPLING_RATIO :
@@ -1646,9 +1669,11 @@ static int at91_adc_write_raw(struct iio_dev *indio_dev,
16461669 /* if no change, optimize out */
16471670 if (val == st -> oversampling_ratio )
16481671 return 0 ;
1649- st -> oversampling_ratio = val ;
16501672 /* update ratio */
1651- at91_adc_config_emr (st );
1673+ ret = at91_adc_config_emr (st , val );
1674+ if (ret )
1675+ return ret ;
1676+ st -> oversampling_ratio = val ;
16521677 return 0 ;
16531678 case IIO_CHAN_INFO_SAMP_FREQ :
16541679 if (val < st -> soc_info .min_sample_rate ||
@@ -1820,7 +1845,7 @@ static void at91_adc_hw_init(struct iio_dev *indio_dev)
18201845 at91_adc_setup_samp_freq (indio_dev , st -> soc_info .min_sample_rate );
18211846
18221847 /* configure extended mode register */
1823- at91_adc_config_emr (st );
1848+ at91_adc_config_emr (st , st -> oversampling_ratio );
18241849}
18251850
18261851static ssize_t at91_adc_get_fifo_state (struct device * dev ,
0 commit comments