@@ -107,7 +107,10 @@ struct cf_axi_dds_state {
107107 struct gpio_desc * plddrbypass_gpio ;
108108 struct gpio_desc * interpolation_gpio ;
109109 struct jesd204_dev * jdev ;
110-
110+ /*
111+ * Lock used when in standalone mode.
112+ */
113+ struct mutex lock ;
111114 bool standalone ;
112115 bool dp_disable ;
113116 bool enable ;
@@ -133,6 +136,30 @@ struct cf_axi_dds_state {
133136 struct cf_axi_dds_chip_info chip_info_generated ;
134137};
135138
139+ #define cf_axi_dds_lock (st ) { \
140+ struct cf_axi_dds_state *__st = st; \
141+ \
142+ if (__st->standalone) { \
143+ mutex_lock(&__st->lock); \
144+ } else { \
145+ struct cf_axi_converter *conv = to_converter(__st->dev_spi); \
146+ \
147+ mutex_lock(&conv->lock); \
148+ } \
149+ }
150+
151+ #define cf_axi_dds_unlock (st ) { \
152+ struct cf_axi_dds_state *__st = st; \
153+ \
154+ if (__st->standalone) { \
155+ mutex_unlock(&__st->lock); \
156+ } else { \
157+ struct cf_axi_converter *conv = to_converter(__st->dev_spi); \
158+ \
159+ mutex_unlock(&conv->lock); \
160+ } \
161+ }
162+
136163bool cf_axi_dds_dma_fifo_en (struct cf_axi_dds_state * st )
137164{
138165 return st -> pl_dma_fifo_en ;
@@ -537,10 +564,10 @@ static ssize_t cf_axi_sampling_frequency_available(struct device *dev,
537564 if (!st -> interpolation_factor )
538565 return - ENODEV ;
539566
540- mutex_lock ( & indio_dev -> mlock );
567+ cf_axi_dds_lock ( st );
541568 ret = cf_axi_get_parent_sampling_frequency (st , & freq );
542569 if (ret < 0 ) {
543- mutex_unlock ( & indio_dev -> mlock );
570+ cf_axi_dds_unlock ( st );
544571 return ret ;
545572 }
546573
@@ -550,7 +577,7 @@ static ssize_t cf_axi_sampling_frequency_available(struct device *dev,
550577
551578 ret += snprintf (& buf [ret ], PAGE_SIZE - ret , "\n" );
552579
553- mutex_unlock ( & indio_dev -> mlock );
580+ cf_axi_dds_unlock ( st );
554581
555582 return ret ;
556583}
@@ -572,7 +599,7 @@ static ssize_t axidds_sync_start_store(struct device *dev,
572599 if (ret < 0 )
573600 return ret ;
574601
575- mutex_lock ( & indio_dev -> mlock );
602+ cf_axi_dds_lock ( st );
576603 if (st -> ext_sync_avail ) {
577604 switch (ret ) {
578605 case 0 :
@@ -590,7 +617,7 @@ static ssize_t axidds_sync_start_store(struct device *dev,
590617 } else if (ret == 0 ) {
591618 cf_axi_dds_start_sync (st , 0 );
592619 }
593- mutex_unlock ( & indio_dev -> mlock );
620+ cf_axi_dds_unlock ( st );
594621
595622 return ret < 0 ? ret : len ;
596623}
@@ -675,7 +702,7 @@ static int cf_axi_dds_read_raw(struct iio_dev *indio_dev,
675702 unsigned int reg , channel , phase = 0 ;
676703 int ret ;
677704
678- mutex_lock ( & indio_dev -> mlock );
705+ cf_axi_dds_lock ( st );
679706
680707 switch (m ) {
681708 case 0 :
@@ -685,7 +712,7 @@ static int cf_axi_dds_read_raw(struct iio_dev *indio_dev,
685712 }
686713 * val = st -> enable ;
687714
688- mutex_unlock ( & indio_dev -> mlock );
715+ cf_axi_dds_unlock ( st );
689716 return IIO_VAL_INT ;
690717 case IIO_CHAN_INFO_SCALE :
691718 if (chan -> type == IIO_VOLTAGE ) {
@@ -697,29 +724,29 @@ static int cf_axi_dds_read_raw(struct iio_dev *indio_dev,
697724 ret = conv -> read_raw (indio_dev , chan ,
698725 val , val2 , m );
699726 }
700- mutex_unlock ( & indio_dev -> mlock );
727+ cf_axi_dds_unlock ( st );
701728 return ret ;
702729 }
703730 }
704731
705732 reg = ADI_TO_DDS_SCALE (dds_read (st ,
706733 ADI_REG_CHAN_CNTRL_1_IIOCHAN (chan -> channel )));
707734 cf_axi_dds_signed_mag_fmt_to_iio (reg , val , val2 );
708- mutex_unlock ( & indio_dev -> mlock );
735+ cf_axi_dds_unlock ( st );
709736 return IIO_VAL_INT_PLUS_MICRO ;
710737 case IIO_CHAN_INFO_FREQUENCY :
711738 reg = dds_read (st , ADI_REG_CHAN_CNTRL_2_IIOCHAN (chan -> channel ));
712739 val64 = (u64 )ADI_TO_DDS_INCR (reg ) * (u64 )st -> dac_clk ;
713740 do_div (val64 , 0xFFFF );
714741 * val = val64 ;
715- mutex_unlock ( & indio_dev -> mlock );
742+ cf_axi_dds_unlock ( st );
716743 return IIO_VAL_INT ;
717744 case IIO_CHAN_INFO_PHASE :
718745 reg = dds_read (st , ADI_REG_CHAN_CNTRL_2_IIOCHAN (chan -> channel ));
719746 val64 = (u64 )ADI_TO_DDS_INIT (reg ) * 360000ULL + (0x10000 / 2 );
720747 do_div (val64 , 0x10000 );
721748 * val = val64 ;
722- mutex_unlock ( & indio_dev -> mlock );
749+ cf_axi_dds_unlock ( st );
723750 return IIO_VAL_INT ;
724751 case IIO_CHAN_INFO_SAMP_FREQ :
725752 ret = cf_axi_get_parent_sampling_frequency (st , & freq );
@@ -732,7 +759,7 @@ static int cf_axi_dds_read_raw(struct iio_dev *indio_dev,
732759 * val = lower_32_bits (freq );
733760 * val2 = upper_32_bits (freq );
734761
735- mutex_unlock ( & indio_dev -> mlock );
762+ cf_axi_dds_unlock ( st );
736763 return IIO_VAL_INT_64 ;
737764 case IIO_CHAN_INFO_CALIBPHASE :
738765 phase = 1 ;
@@ -748,7 +775,7 @@ static int cf_axi_dds_read_raw(struct iio_dev *indio_dev,
748775 else
749776 reg = ADI_TO_IQCOR_COEFF_2 (reg );
750777
751- mutex_unlock ( & indio_dev -> mlock );
778+ cf_axi_dds_unlock ( st );
752779 return cf_axi_dds_signed_mag_fmt_to_iio (reg , val , val2 );
753780 default :
754781 if (!st -> standalone ) {
@@ -764,7 +791,7 @@ static int cf_axi_dds_read_raw(struct iio_dev *indio_dev,
764791 }
765792 }
766793
767- mutex_unlock ( & indio_dev -> mlock );
794+ cf_axi_dds_unlock ( st );
768795
769796 return ret ;
770797}
@@ -786,7 +813,7 @@ static int cf_axi_dds_write_raw(struct iio_dev *indio_dev,
786813 else
787814 conv = ERR_PTR (- ENODEV );
788815
789- mutex_lock ( & indio_dev -> mlock );
816+ cf_axi_dds_lock ( st );
790817
791818 switch (mask ) {
792819 case IIO_CHAN_INFO_RAW :
@@ -812,7 +839,7 @@ static int cf_axi_dds_write_raw(struct iio_dev *indio_dev,
812839 ret = conv -> write_raw (indio_dev ,
813840 chan , val , val2 , mask );
814841 }
815- mutex_unlock ( & indio_dev -> mlock );
842+ cf_axi_dds_unlock ( st );
816843 return ret ;
817844 }
818845 }
@@ -944,7 +971,7 @@ static int cf_axi_dds_write_raw(struct iio_dev *indio_dev,
944971 }
945972
946973err_unlock :
947- mutex_unlock ( & indio_dev -> mlock );
974+ cf_axi_dds_unlock ( st );
948975
949976 return ret ;
950977}
@@ -968,7 +995,7 @@ static int cf_axi_dds_reg_access(struct iio_dev *indio_dev,
968995 if (st -> dev_spi )
969996 conv = to_converter (st -> dev_spi );
970997
971- mutex_lock ( & indio_dev -> mlock );
998+ cf_axi_dds_lock ( st );
972999 if (readval == NULL ) {
9731000 if ((reg & DEBUGFS_DRA_PCORE_REG_MAGIC ) ||
9741001 st -> standalone ) {
@@ -1003,7 +1030,7 @@ static int cf_axi_dds_reg_access(struct iio_dev *indio_dev,
10031030 }
10041031
10051032out_unlock :
1006- mutex_unlock ( & indio_dev -> mlock );
1033+ cf_axi_dds_unlock ( st );
10071034
10081035 return ret ;
10091036}
@@ -1657,7 +1684,7 @@ static ssize_t cf_axi_dds_ext_info_read(struct iio_dev *indio_dev,
16571684 unsigned int index ;
16581685 int ret = 0 ;
16591686
1660- mutex_lock ( & indio_dev -> mlock );
1687+ cf_axi_dds_lock ( st );
16611688
16621689 switch (private ) {
16631690 case CHANNEL_XBAR :
@@ -1673,7 +1700,7 @@ static ssize_t cf_axi_dds_ext_info_read(struct iio_dev *indio_dev,
16731700 ret = - EINVAL ;
16741701 }
16751702
1676- mutex_unlock ( & indio_dev -> mlock );
1703+ cf_axi_dds_unlock ( st );
16771704
16781705 if (ret == 0 )
16791706 ret = sprintf (buf , "%lld\n" , val );
@@ -1691,7 +1718,7 @@ static ssize_t cf_axi_dds_ext_info_write(struct iio_dev *indio_dev,
16911718 unsigned int index , val ;
16921719 int ret ;
16931720
1694- mutex_lock ( & indio_dev -> mlock );
1721+ cf_axi_dds_lock ( st );
16951722
16961723 switch (private ) {
16971724 case CHANNEL_XBAR :
@@ -1722,7 +1749,7 @@ static ssize_t cf_axi_dds_ext_info_write(struct iio_dev *indio_dev,
17221749 }
17231750
17241751out :
1725- mutex_unlock ( & indio_dev -> mlock );
1752+ cf_axi_dds_unlock ( st );
17261753
17271754 return ret ? ret : len ;
17281755}
@@ -2292,6 +2319,8 @@ static int cf_axi_dds_probe(struct platform_device *pdev)
22922319
22932320 st -> chip_info = & st -> chip_info_generated ;
22942321 }
2322+
2323+ mutex_init (& st -> lock );
22952324 } else {
22962325 st -> dev_spi = dds_converter_find (& pdev -> dev );
22972326 if (IS_ERR (st -> dev_spi ))
@@ -2309,6 +2338,7 @@ static int cf_axi_dds_probe(struct platform_device *pdev)
23092338 conv -> indio_dev = indio_dev ;
23102339 conv -> pcore_sync = cf_axi_dds_sync_frame ;
23112340 conv -> pcore_set_sed_pattern = cf_axi_dds_set_sed_pattern ;
2341+ mutex_init (& conv -> lock );
23122342
23132343 st -> dac_clk = conv -> get_data_clk (conv );
23142344
0 commit comments