11
11
#include <linux/delay.h>
12
12
#include <linux/dev_printk.h>
13
13
#include <linux/device.h>
14
+ #include <linux/minmax.h>
14
15
#include <linux/module.h>
16
+ #include <linux/overflow.h>
15
17
#include <linux/soundwire/sdw_registers.h>
16
18
#include <linux/string_helpers.h>
17
19
#include <sound/control.h>
21
23
#include <sound/soc.h>
22
24
#include <sound/soc-component.h>
23
25
#include <sound/soc-dapm.h>
26
+ #include <sound/tlv.h>
24
27
25
28
static struct sdca_control * selector_find_control (struct device * dev ,
26
29
struct sdca_entity * entity ,
@@ -69,13 +72,33 @@ static struct sdca_control_range *selector_find_range(struct device *dev,
69
72
return control_find_range (dev , entity , control , cols , rows );
70
73
}
71
74
75
+ static bool exported_control (struct sdca_entity * entity , struct sdca_control * control )
76
+ {
77
+ switch (SDCA_CTL_TYPE (entity -> type , control -> sel )) {
78
+ case SDCA_CTL_TYPE_S (GE , DETECTED_MODE ):
79
+ return true;
80
+ default :
81
+ break ;
82
+ }
83
+
84
+ return control -> layers & (SDCA_ACCESS_LAYER_USER |
85
+ SDCA_ACCESS_LAYER_APPLICATION );
86
+ }
87
+
88
+ static bool readonly_control (struct sdca_control * control )
89
+ {
90
+ return control -> has_fixed || control -> mode == SDCA_ACCESS_MODE_RO ;
91
+ }
92
+
72
93
/**
73
94
* sdca_asoc_count_component - count the various component parts
74
95
* @function: Pointer to the Function information.
75
96
* @num_widgets: Output integer pointer, will be filled with the
76
97
* required number of DAPM widgets for the Function.
77
98
* @num_routes: Output integer pointer, will be filled with the
78
99
* required number of DAPM routes for the Function.
100
+ * @num_controls: Output integer pointer, will be filled with the
101
+ * required number of ALSA controls for the Function.
79
102
*
80
103
* This function counts various things within the SDCA Function such
81
104
* that the calling driver can allocate appropriate space before
@@ -84,12 +107,13 @@ static struct sdca_control_range *selector_find_range(struct device *dev,
84
107
* Return: Returns zero on success, and a negative error code on failure.
85
108
*/
86
109
int sdca_asoc_count_component (struct device * dev , struct sdca_function_data * function ,
87
- int * num_widgets , int * num_routes )
110
+ int * num_widgets , int * num_routes , int * num_controls )
88
111
{
89
- int i ;
112
+ int i , j ;
90
113
91
114
* num_widgets = function -> num_entities - 1 ;
92
115
* num_routes = 0 ;
116
+ * num_controls = 0 ;
93
117
94
118
for (i = 0 ; i < function -> num_entities - 1 ; i ++ ) {
95
119
struct sdca_entity * entity = & function -> entities [i ];
@@ -100,6 +124,7 @@ int sdca_asoc_count_component(struct device *dev, struct sdca_function_data *fun
100
124
case SDCA_ENTITY_TYPE_OT :
101
125
* num_routes += !!entity -> iot .clock ;
102
126
* num_routes += !!entity -> iot .is_dataport ;
127
+ * num_controls += !entity -> iot .is_dataport ;
103
128
break ;
104
129
case SDCA_ENTITY_TYPE_PDE :
105
130
* num_routes += entity -> pde .num_managed ;
@@ -113,6 +138,11 @@ int sdca_asoc_count_component(struct device *dev, struct sdca_function_data *fun
113
138
114
139
/* Add primary entity connections from DisCo */
115
140
* num_routes += entity -> num_sources ;
141
+
142
+ for (j = 0 ; j < entity -> num_controls ; j ++ ) {
143
+ if (exported_control (entity , & entity -> controls [j ]))
144
+ (* num_controls )++ ;
145
+ }
116
146
}
117
147
118
148
return 0 ;
@@ -797,6 +827,212 @@ int sdca_asoc_populate_dapm(struct device *dev, struct sdca_function_data *funct
797
827
}
798
828
EXPORT_SYMBOL_NS (sdca_asoc_populate_dapm , "SND_SOC_SDCA" );
799
829
830
+ static int control_limit_kctl (struct device * dev ,
831
+ struct sdca_entity * entity ,
832
+ struct sdca_control * control ,
833
+ struct snd_kcontrol_new * kctl )
834
+ {
835
+ struct soc_mixer_control * mc = (struct soc_mixer_control * )kctl -> private_value ;
836
+ struct sdca_control_range * range ;
837
+ int min , max , step ;
838
+ unsigned int * tlv ;
839
+ int shift ;
840
+
841
+ if (control -> type != SDCA_CTL_DATATYPE_Q7P8DB )
842
+ return 0 ;
843
+
844
+ /*
845
+ * FIXME: For now only handle the simple case of a single linear range
846
+ */
847
+ range = control_find_range (dev , entity , control , SDCA_VOLUME_LINEAR_NCOLS , 1 );
848
+ if (!range )
849
+ return - EINVAL ;
850
+
851
+ min = sdca_range (range , SDCA_VOLUME_LINEAR_MIN , 0 );
852
+ max = sdca_range (range , SDCA_VOLUME_LINEAR_MAX , 0 );
853
+ step = sdca_range (range , SDCA_VOLUME_LINEAR_STEP , 0 );
854
+
855
+ min = sign_extend32 (min , control -> nbits - 1 );
856
+ max = sign_extend32 (max , control -> nbits - 1 );
857
+
858
+ /*
859
+ * FIXME: Only support power of 2 step sizes as this can be supported
860
+ * by a simple shift.
861
+ */
862
+ if (hweight32 (step ) != 1 ) {
863
+ dev_err (dev , "%s: %s: currently unsupported step size\n" ,
864
+ entity -> label , control -> label );
865
+ return - EINVAL ;
866
+ }
867
+
868
+ /*
869
+ * The SDCA volumes are in steps of 1/256th of a dB, a step down of
870
+ * 64 (shift of 6) gives 1/4dB. 1/4dB is the smallest unit that is also
871
+ * representable in the ALSA TLVs which are in 1/100ths of a dB.
872
+ */
873
+ shift = max (ffs (step ) - 1 , 6 );
874
+
875
+ tlv = devm_kcalloc (dev , 4 , sizeof (* tlv ), GFP_KERNEL );
876
+ if (!tlv )
877
+ return - ENOMEM ;
878
+
879
+ tlv [0 ] = SNDRV_CTL_TLVT_DB_SCALE ;
880
+ tlv [1 ] = 2 * sizeof (* tlv );
881
+ tlv [2 ] = (min * 100 ) >> 8 ;
882
+ tlv [3 ] = ((1 << shift ) * 100 ) >> 8 ;
883
+
884
+ mc -> min = min >> shift ;
885
+ mc -> max = max >> shift ;
886
+ mc -> shift = shift ;
887
+ mc -> rshift = shift ;
888
+ mc -> sign_bit = 15 - shift ;
889
+
890
+ kctl -> tlv .p = tlv ;
891
+ kctl -> access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ ;
892
+
893
+ return 0 ;
894
+ }
895
+
896
+ static int populate_control (struct device * dev ,
897
+ struct sdca_function_data * function ,
898
+ struct sdca_entity * entity ,
899
+ struct sdca_control * control ,
900
+ struct snd_kcontrol_new * * kctl )
901
+ {
902
+ const char * control_suffix = "" ;
903
+ const char * control_name ;
904
+ struct soc_mixer_control * mc ;
905
+ int index = 0 ;
906
+ int ret ;
907
+ int cn ;
908
+
909
+ if (!exported_control (entity , control ))
910
+ return 0 ;
911
+
912
+ if (control -> type == SDCA_CTL_DATATYPE_ONEBIT )
913
+ control_suffix = " Switch" ;
914
+
915
+ control_name = devm_kasprintf (dev , GFP_KERNEL , "%s %s%s" , entity -> label ,
916
+ control -> label , control_suffix );
917
+ if (!control_name )
918
+ return - ENOMEM ;
919
+
920
+ mc = devm_kmalloc (dev , sizeof (* mc ), GFP_KERNEL );
921
+ if (!mc )
922
+ return - ENOMEM ;
923
+
924
+ for_each_set_bit (cn , (unsigned long * )& control -> cn_list ,
925
+ BITS_PER_TYPE (control -> cn_list )) {
926
+ switch (index ++ ) {
927
+ case 0 :
928
+ mc -> reg = SDW_SDCA_CTL (function -> desc -> adr , entity -> id ,
929
+ control -> sel , cn );
930
+ mc -> rreg = mc -> reg ;
931
+ break ;
932
+ case 1 :
933
+ mc -> rreg = SDW_SDCA_CTL (function -> desc -> adr , entity -> id ,
934
+ control -> sel , cn );
935
+ break ;
936
+ default :
937
+ dev_err (dev , "%s: %s: only mono/stereo controls supported\n" ,
938
+ entity -> label , control -> label );
939
+ return - EINVAL ;
940
+ }
941
+ }
942
+
943
+ mc -> min = 0 ;
944
+ mc -> max = clamp ((0x1ull << control -> nbits ) - 1 , 0 , type_max (mc -> max ));
945
+
946
+ (* kctl )-> name = control_name ;
947
+ (* kctl )-> private_value = (unsigned long )mc ;
948
+ (* kctl )-> iface = SNDRV_CTL_ELEM_IFACE_MIXER ;
949
+ (* kctl )-> info = snd_soc_info_volsw ;
950
+ (* kctl )-> get = snd_soc_get_volsw ;
951
+ (* kctl )-> put = snd_soc_put_volsw ;
952
+
953
+ if (readonly_control (control ))
954
+ (* kctl )-> access = SNDRV_CTL_ELEM_ACCESS_READ ;
955
+ else
956
+ (* kctl )-> access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
957
+
958
+ ret = control_limit_kctl (dev , entity , control , * kctl );
959
+ if (ret )
960
+ return ret ;
961
+
962
+ (* kctl )++ ;
963
+
964
+ return 0 ;
965
+ }
966
+
967
+ static int populate_pin_switch (struct device * dev ,
968
+ struct sdca_entity * entity ,
969
+ struct snd_kcontrol_new * * kctl )
970
+ {
971
+ const char * control_name ;
972
+
973
+ control_name = devm_kasprintf (dev , GFP_KERNEL , "%s Switch" , entity -> label );
974
+ if (!control_name )
975
+ return - ENOMEM ;
976
+
977
+ (* kctl )-> name = control_name ;
978
+ (* kctl )-> private_value = (unsigned long )entity -> label ;
979
+ (* kctl )-> iface = SNDRV_CTL_ELEM_IFACE_MIXER ;
980
+ (* kctl )-> info = snd_soc_dapm_info_pin_switch ;
981
+ (* kctl )-> get = snd_soc_dapm_get_component_pin_switch ;
982
+ (* kctl )-> put = snd_soc_dapm_put_component_pin_switch ;
983
+ (* kctl )++ ;
984
+
985
+ return 0 ;
986
+ }
987
+
988
+ /**
989
+ * sdca_asoc_populate_controls - fill in an array of ALSA controls for a Function
990
+ * @dev: Pointer to the device against which allocations will be done.
991
+ * @function: Pointer to the Function information.
992
+ * @route: Array of ALSA controls to be populated.
993
+ *
994
+ * This function populates an array of ALSA controls from the DisCo
995
+ * information for a particular SDCA Function. Typically,
996
+ * snd_soc_asoc_count_component will be used to allocate an
997
+ * appropriately sized array before calling this function.
998
+ *
999
+ * Return: Returns zero on success, and a negative error code on failure.
1000
+ */
1001
+ int sdca_asoc_populate_controls (struct device * dev ,
1002
+ struct sdca_function_data * function ,
1003
+ struct snd_kcontrol_new * kctl )
1004
+ {
1005
+ int i , j ;
1006
+ int ret ;
1007
+
1008
+ for (i = 0 ; i < function -> num_entities ; i ++ ) {
1009
+ struct sdca_entity * entity = & function -> entities [i ];
1010
+
1011
+ switch (entity -> type ) {
1012
+ case SDCA_ENTITY_TYPE_IT :
1013
+ case SDCA_ENTITY_TYPE_OT :
1014
+ if (!entity -> iot .is_dataport ) {
1015
+ ret = populate_pin_switch (dev , entity , & kctl );
1016
+ if (ret )
1017
+ return ret ;
1018
+ }
1019
+ break ;
1020
+ default :
1021
+ break ;
1022
+ }
1023
+
1024
+ for (j = 0 ; j < entity -> num_controls ; j ++ ) {
1025
+ ret = populate_control (dev , function , entity ,
1026
+ & entity -> controls [j ], & kctl );
1027
+ if (ret )
1028
+ return ret ;
1029
+ }
1030
+ }
1031
+
1032
+ return 0 ;
1033
+ }
1034
+ EXPORT_SYMBOL_NS (sdca_asoc_populate_controls , "SND_SOC_SDCA" );
1035
+
800
1036
/**
801
1037
* sdca_asoc_populate_component - fill in a component driver for a Function
802
1038
* @dev: Pointer to the device against which allocations will be done.
@@ -815,10 +1051,12 @@ int sdca_asoc_populate_component(struct device *dev,
815
1051
{
816
1052
struct snd_soc_dapm_widget * widgets ;
817
1053
struct snd_soc_dapm_route * routes ;
818
- int num_widgets , num_routes ;
1054
+ struct snd_kcontrol_new * controls ;
1055
+ int num_widgets , num_routes , num_controls ;
819
1056
int ret ;
820
1057
821
- ret = sdca_asoc_count_component (dev , function , & num_widgets , & num_routes );
1058
+ ret = sdca_asoc_count_component (dev , function , & num_widgets , & num_routes ,
1059
+ & num_controls );
822
1060
if (ret )
823
1061
return ret ;
824
1062
@@ -830,14 +1068,24 @@ int sdca_asoc_populate_component(struct device *dev,
830
1068
if (!routes )
831
1069
return - ENOMEM ;
832
1070
1071
+ controls = devm_kcalloc (dev , num_controls , sizeof (* controls ), GFP_KERNEL );
1072
+ if (!controls )
1073
+ return - ENOMEM ;
1074
+
833
1075
ret = sdca_asoc_populate_dapm (dev , function , widgets , routes );
834
1076
if (ret )
835
1077
return ret ;
836
1078
1079
+ ret = sdca_asoc_populate_controls (dev , function , controls );
1080
+ if (ret )
1081
+ return ret ;
1082
+
837
1083
component_drv -> dapm_widgets = widgets ;
838
1084
component_drv -> num_dapm_widgets = num_widgets ;
839
1085
component_drv -> dapm_routes = routes ;
840
1086
component_drv -> num_dapm_routes = num_routes ;
1087
+ component_drv -> controls = controls ;
1088
+ component_drv -> num_controls = num_controls ;
841
1089
842
1090
return 0 ;
843
1091
}
0 commit comments