@@ -728,7 +728,6 @@ static int at91_adc_configure_trigger(struct iio_trigger *trig, bool state)
728
728
struct iio_dev * indio = iio_trigger_get_drvdata (trig );
729
729
struct at91_adc_state * st = iio_priv (indio );
730
730
u32 status = at91_adc_readl (st , AT91_SAMA5D2_TRGR );
731
- u8 bit ;
732
731
733
732
/* clear TRGMOD */
734
733
status &= ~AT91_SAMA5D2_TRGR_TRGMOD_MASK ;
@@ -739,48 +738,6 @@ static int at91_adc_configure_trigger(struct iio_trigger *trig, bool state)
739
738
/* set/unset hw trigger */
740
739
at91_adc_writel (st , AT91_SAMA5D2_TRGR , status );
741
740
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
-
784
741
return 0 ;
785
742
}
786
743
@@ -905,9 +862,22 @@ static int at91_adc_dma_start(struct iio_dev *indio_dev)
905
862
return 0 ;
906
863
}
907
864
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
+
908
877
static int at91_adc_buffer_postenable (struct iio_dev * indio_dev )
909
878
{
910
879
int ret ;
880
+ u8 bit ;
911
881
struct at91_adc_state * st = iio_priv (indio_dev );
912
882
913
883
/* 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)
928
898
return ret ;
929
899
}
930
900
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
+
931
931
return iio_triggered_buffer_postenable (indio_dev );
932
932
}
933
933
@@ -948,21 +948,11 @@ static int at91_adc_buffer_predisable(struct iio_dev *indio_dev)
948
948
if (!(indio_dev -> currentmode & INDIO_ALL_TRIGGERED_MODES ))
949
949
return - EINVAL ;
950
950
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
-
962
951
/*
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
964
954
* 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.
966
956
*/
967
957
for_each_set_bit (bit , indio_dev -> active_scan_mask ,
968
958
indio_dev -> num_channels ) {
@@ -975,12 +965,28 @@ static int at91_adc_buffer_predisable(struct iio_dev *indio_dev)
975
965
if (chan -> type == IIO_POSITIONRELATIVE ||
976
966
chan -> type == IIO_PRESSURE )
977
967
continue ;
968
+
969
+ at91_adc_writel (st , AT91_SAMA5D2_CHDR , BIT (chan -> channel ));
970
+
978
971
if (st -> dma_st .dma_chan )
979
972
at91_adc_readl (st , chan -> address );
980
973
}
981
974
975
+ if (at91_adc_buffer_check_use_irq (indio_dev , st ))
976
+ at91_adc_writel (st , AT91_SAMA5D2_IDR , AT91_SAMA5D2_IER_DRDY );
977
+
982
978
/* read overflow register to clear possible overflow status */
983
979
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
+
984
990
return ret ;
985
991
}
986
992
@@ -1135,6 +1141,13 @@ static irqreturn_t at91_adc_trigger_handler(int irq, void *p)
1135
1141
struct iio_dev * indio_dev = pf -> indio_dev ;
1136
1142
struct at91_adc_state * st = iio_priv (indio_dev );
1137
1143
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
+
1138
1151
if (st -> dma_st .dma_chan )
1139
1152
at91_adc_trigger_handler_dma (indio_dev );
1140
1153
else
@@ -1147,20 +1160,9 @@ static irqreturn_t at91_adc_trigger_handler(int irq, void *p)
1147
1160
1148
1161
static int at91_adc_buffer_init (struct iio_dev * indio )
1149
1162
{
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 );
1164
1166
}
1165
1167
1166
1168
static unsigned at91_adc_startup_time (unsigned startup_time_min ,
0 commit comments