@@ -728,7 +728,6 @@ static int at91_adc_configure_trigger(struct iio_trigger *trig, bool state)
728728 struct iio_dev * indio = iio_trigger_get_drvdata (trig );
729729 struct at91_adc_state * st = iio_priv (indio );
730730 u32 status = at91_adc_readl (st , AT91_SAMA5D2_TRGR );
731- u8 bit ;
732731
733732 /* clear TRGMOD */
734733 status &= ~AT91_SAMA5D2_TRGR_TRGMOD_MASK ;
@@ -739,48 +738,6 @@ static int at91_adc_configure_trigger(struct iio_trigger *trig, bool state)
739738 /* set/unset hw trigger */
740739 at91_adc_writel (st , AT91_SAMA5D2_TRGR , status );
741740
742- for_each_set_bit (bit , indio -> active_scan_mask , indio -> num_channels ) {
743- struct iio_chan_spec const * chan = at91_adc_chan_get (indio , bit );
744- u32 cor ;
745-
746- if (!chan )
747- continue ;
748- /* these channel types cannot be handled by this trigger */
749- if (chan -> type == IIO_POSITIONRELATIVE ||
750- chan -> type == IIO_PRESSURE )
751- continue ;
752-
753- if (state ) {
754- cor = at91_adc_readl (st , AT91_SAMA5D2_COR );
755-
756- if (chan -> differential )
757- cor |= (BIT (chan -> channel ) |
758- BIT (chan -> channel2 )) <<
759- AT91_SAMA5D2_COR_DIFF_OFFSET ;
760- else
761- cor &= ~(BIT (chan -> channel ) <<
762- AT91_SAMA5D2_COR_DIFF_OFFSET );
763-
764- at91_adc_writel (st , AT91_SAMA5D2_COR , cor );
765- }
766-
767- if (state )
768- at91_adc_writel (st , AT91_SAMA5D2_CHER ,
769- BIT (chan -> channel ));
770- else
771- at91_adc_writel (st , AT91_SAMA5D2_CHDR ,
772- BIT (chan -> channel ));
773- }
774-
775- /* Nothing to do if using DMA */
776- if (st -> dma_st .dma_chan )
777- return 0 ;
778-
779- if (state )
780- at91_adc_writel (st , AT91_SAMA5D2_IER , AT91_SAMA5D2_IER_DRDY );
781- else
782- at91_adc_writel (st , AT91_SAMA5D2_IDR , AT91_SAMA5D2_IER_DRDY );
783-
784741 return 0 ;
785742}
786743
@@ -905,9 +862,22 @@ static int at91_adc_dma_start(struct iio_dev *indio_dev)
905862 return 0 ;
906863}
907864
865+ static bool at91_adc_buffer_check_use_irq (struct iio_dev * indio ,
866+ struct at91_adc_state * st )
867+ {
868+ /* if using DMA, we do not use our own IRQ (we use DMA-controller) */
869+ if (st -> dma_st .dma_chan )
870+ return false;
871+ /* if the trigger is not ours, then it has its own IRQ */
872+ if (iio_trigger_validate_own_device (indio -> trig , indio ))
873+ return false;
874+ return true;
875+ }
876+
908877static int at91_adc_buffer_postenable (struct iio_dev * indio_dev )
909878{
910879 int ret ;
880+ u8 bit ;
911881 struct at91_adc_state * st = iio_priv (indio_dev );
912882
913883 /* check if we are enabling triggered buffer or the touchscreen */
@@ -928,6 +898,36 @@ static int at91_adc_buffer_postenable(struct iio_dev *indio_dev)
928898 return ret ;
929899 }
930900
901+ for_each_set_bit (bit , indio_dev -> active_scan_mask ,
902+ indio_dev -> num_channels ) {
903+ struct iio_chan_spec const * chan =
904+ at91_adc_chan_get (indio_dev , bit );
905+ u32 cor ;
906+
907+ if (!chan )
908+ continue ;
909+ /* these channel types cannot be handled by this trigger */
910+ if (chan -> type == IIO_POSITIONRELATIVE ||
911+ chan -> type == IIO_PRESSURE )
912+ continue ;
913+
914+ cor = at91_adc_readl (st , AT91_SAMA5D2_COR );
915+
916+ if (chan -> differential )
917+ cor |= (BIT (chan -> channel ) | BIT (chan -> channel2 )) <<
918+ AT91_SAMA5D2_COR_DIFF_OFFSET ;
919+ else
920+ cor &= ~(BIT (chan -> channel ) <<
921+ AT91_SAMA5D2_COR_DIFF_OFFSET );
922+
923+ at91_adc_writel (st , AT91_SAMA5D2_COR , cor );
924+
925+ at91_adc_writel (st , AT91_SAMA5D2_CHER , BIT (chan -> channel ));
926+ }
927+
928+ if (at91_adc_buffer_check_use_irq (indio_dev , st ))
929+ at91_adc_writel (st , AT91_SAMA5D2_IER , AT91_SAMA5D2_IER_DRDY );
930+
931931 return iio_triggered_buffer_postenable (indio_dev );
932932}
933933
@@ -948,21 +948,11 @@ static int at91_adc_buffer_predisable(struct iio_dev *indio_dev)
948948 if (!(indio_dev -> currentmode & INDIO_ALL_TRIGGERED_MODES ))
949949 return - EINVAL ;
950950
951- /* continue with the triggered buffer */
952- ret = iio_triggered_buffer_predisable (indio_dev );
953- if (ret < 0 )
954- dev_err (& indio_dev -> dev , "buffer predisable failed\n" );
955-
956- if (!st -> dma_st .dma_chan )
957- return ret ;
958-
959- /* if we are using DMA we must clear registers and end DMA */
960- dmaengine_terminate_sync (st -> dma_st .dma_chan );
961-
962951 /*
963- * For each enabled channel we must read the last converted value
952+ * For each enable channel we must disable it in hardware.
953+ * In the case of DMA, we must read the last converted value
964954 * to clear EOC status and not get a possible interrupt later.
965- * This value is being read by DMA from LCDR anyway
955+ * This value is being read by DMA from LCDR anyway, so it's not lost.
966956 */
967957 for_each_set_bit (bit , indio_dev -> active_scan_mask ,
968958 indio_dev -> num_channels ) {
@@ -975,12 +965,28 @@ static int at91_adc_buffer_predisable(struct iio_dev *indio_dev)
975965 if (chan -> type == IIO_POSITIONRELATIVE ||
976966 chan -> type == IIO_PRESSURE )
977967 continue ;
968+
969+ at91_adc_writel (st , AT91_SAMA5D2_CHDR , BIT (chan -> channel ));
970+
978971 if (st -> dma_st .dma_chan )
979972 at91_adc_readl (st , chan -> address );
980973 }
981974
975+ if (at91_adc_buffer_check_use_irq (indio_dev , st ))
976+ at91_adc_writel (st , AT91_SAMA5D2_IDR , AT91_SAMA5D2_IER_DRDY );
977+
982978 /* read overflow register to clear possible overflow status */
983979 at91_adc_readl (st , AT91_SAMA5D2_OVER );
980+
981+ /* continue with the triggered buffer */
982+ ret = iio_triggered_buffer_predisable (indio_dev );
983+ if (ret < 0 )
984+ dev_err (& indio_dev -> dev , "buffer predisable failed\n" );
985+
986+ /* if we are using DMA we must clear registers and end DMA */
987+ if (st -> dma_st .dma_chan )
988+ dmaengine_terminate_sync (st -> dma_st .dma_chan );
989+
984990 return ret ;
985991}
986992
@@ -1135,6 +1141,13 @@ static irqreturn_t at91_adc_trigger_handler(int irq, void *p)
11351141 struct iio_dev * indio_dev = pf -> indio_dev ;
11361142 struct at91_adc_state * st = iio_priv (indio_dev );
11371143
1144+ /*
1145+ * If it's not our trigger, start a conversion now, as we are
1146+ * actually polling the trigger now.
1147+ */
1148+ if (iio_trigger_validate_own_device (indio_dev -> trig , indio_dev ))
1149+ at91_adc_writel (st , AT91_SAMA5D2_CR , AT91_SAMA5D2_CR_START );
1150+
11381151 if (st -> dma_st .dma_chan )
11391152 at91_adc_trigger_handler_dma (indio_dev );
11401153 else
@@ -1147,20 +1160,9 @@ static irqreturn_t at91_adc_trigger_handler(int irq, void *p)
11471160
11481161static int at91_adc_buffer_init (struct iio_dev * indio )
11491162{
1150- struct at91_adc_state * st = iio_priv (indio );
1151-
1152- if (st -> selected_trig -> hw_trig ) {
1153- return devm_iio_triggered_buffer_setup (& indio -> dev , indio ,
1154- & iio_pollfunc_store_time ,
1155- & at91_adc_trigger_handler , & at91_buffer_setup_ops );
1156- }
1157- /*
1158- * we need to prepare the buffer ops in case we will get
1159- * another buffer attached (like a callback buffer for the touchscreen)
1160- */
1161- indio -> setup_ops = & at91_buffer_setup_ops ;
1162-
1163- return 0 ;
1163+ return devm_iio_triggered_buffer_setup (& indio -> dev , indio ,
1164+ & iio_pollfunc_store_time ,
1165+ & at91_adc_trigger_handler , & at91_buffer_setup_ops );
11641166}
11651167
11661168static unsigned at91_adc_startup_time (unsigned startup_time_min ,
0 commit comments