@@ -47,6 +47,7 @@ struct cs42xx8_priv {
4747 unsigned long sysclk ;
4848 u32 tx_channels ;
4949 struct gpio_desc * gpiod_reset ;
50+ u32 rate [2 ];
5051};
5152
5253/* -127.5dB to 0dB with step of 0.5dB */
@@ -176,21 +177,27 @@ static const struct snd_soc_dapm_route cs42xx8_adc3_dapm_routes[] = {
176177};
177178
178179struct cs42xx8_ratios {
179- unsigned int ratio ;
180- unsigned char speed ;
181- unsigned char mclk ;
180+ unsigned int mfreq ;
181+ unsigned int min_mclk ;
182+ unsigned int max_mclk ;
183+ unsigned int ratio [3 ];
182184};
183185
186+ /*
187+ * According to reference mannual, define the cs42xx8_ratio struct
188+ * MFreq2 | MFreq1 | MFreq0 | Description | SSM | DSM | QSM |
189+ * 0 | 0 | 0 |1.029MHz to 12.8MHz | 256 | 128 | 64 |
190+ * 0 | 0 | 1 |1.536MHz to 19.2MHz | 384 | 192 | 96 |
191+ * 0 | 1 | 0 |2.048MHz to 25.6MHz | 512 | 256 | 128 |
192+ * 0 | 1 | 1 |3.072MHz to 38.4MHz | 768 | 384 | 192 |
193+ * 1 | x | x |4.096MHz to 51.2MHz |1024 | 512 | 256 |
194+ */
184195static const struct cs42xx8_ratios cs42xx8_ratios [] = {
185- { 64 , CS42XX8_FM_QUAD , CS42XX8_FUNCMOD_MFREQ_256 (4 ) },
186- { 96 , CS42XX8_FM_QUAD , CS42XX8_FUNCMOD_MFREQ_384 (4 ) },
187- { 128 , CS42XX8_FM_QUAD , CS42XX8_FUNCMOD_MFREQ_512 (4 ) },
188- { 192 , CS42XX8_FM_QUAD , CS42XX8_FUNCMOD_MFREQ_768 (4 ) },
189- { 256 , CS42XX8_FM_SINGLE , CS42XX8_FUNCMOD_MFREQ_256 (1 ) },
190- { 384 , CS42XX8_FM_SINGLE , CS42XX8_FUNCMOD_MFREQ_384 (1 ) },
191- { 512 , CS42XX8_FM_SINGLE , CS42XX8_FUNCMOD_MFREQ_512 (1 ) },
192- { 768 , CS42XX8_FM_SINGLE , CS42XX8_FUNCMOD_MFREQ_768 (1 ) },
193- { 1024 , CS42XX8_FM_SINGLE , CS42XX8_FUNCMOD_MFREQ_1024 (1 ) }
196+ { 0 , 1029000 , 12800000 , {256 , 128 , 64 } },
197+ { 2 , 1536000 , 19200000 , {384 , 192 , 96 } },
198+ { 4 , 2048000 , 25600000 , {512 , 256 , 128 } },
199+ { 6 , 3072000 , 38400000 , {768 , 384 , 192 } },
200+ { 8 , 4096000 , 51200000 , {1024 , 512 , 256 } },
194201};
195202
196203static int cs42xx8_set_dai_sysclk (struct snd_soc_dai * codec_dai ,
@@ -257,14 +264,68 @@ static int cs42xx8_hw_params(struct snd_pcm_substream *substream,
257264 struct snd_soc_component * component = dai -> component ;
258265 struct cs42xx8_priv * cs42xx8 = snd_soc_component_get_drvdata (component );
259266 bool tx = substream -> stream == SNDRV_PCM_STREAM_PLAYBACK ;
260- u32 ratio = cs42xx8 -> sysclk / params_rate (params );
261- u32 i , fm , val , mask ;
267+ u32 ratio [2 ];
268+ u32 rate [2 ];
269+ u32 fm [2 ];
270+ u32 i , val , mask ;
271+ bool condition1 , condition2 ;
262272
263273 if (tx )
264274 cs42xx8 -> tx_channels = params_channels (params );
265275
276+ rate [tx ] = params_rate (params );
277+ rate [!tx ] = cs42xx8 -> rate [!tx ];
278+
279+ ratio [tx ] = rate [tx ] > 0 ? cs42xx8 -> sysclk / rate [tx ] : 0 ;
280+ ratio [!tx ] = rate [!tx ] > 0 ? cs42xx8 -> sysclk / rate [!tx ] : 0 ;
281+
282+ /* Get functional mode for tx and rx according to rate */
283+ for (i = 0 ; i < 2 ; i ++ ) {
284+ if (cs42xx8 -> slave_mode ) {
285+ fm [i ] = CS42XX8_FM_AUTO ;
286+ } else {
287+ if (rate [i ] < 50000 ) {
288+ fm [i ] = CS42XX8_FM_SINGLE ;
289+ } else if (rate [i ] > 50000 && rate [i ] < 100000 ) {
290+ fm [i ] = CS42XX8_FM_DOUBLE ;
291+ } else if (rate [i ] > 100000 && rate [i ] < 200000 ) {
292+ fm [i ] = CS42XX8_FM_QUAD ;
293+ } else {
294+ dev_err (component -> dev ,
295+ "unsupported sample rate\n" );
296+ return - EINVAL ;
297+ }
298+ }
299+ }
300+
266301 for (i = 0 ; i < ARRAY_SIZE (cs42xx8_ratios ); i ++ ) {
267- if (cs42xx8_ratios [i ].ratio == ratio )
302+ /* Is the ratio[tx] valid ? */
303+ condition1 = ((fm [tx ] == CS42XX8_FM_AUTO ) ?
304+ (cs42xx8_ratios [i ].ratio [0 ] == ratio [tx ] ||
305+ cs42xx8_ratios [i ].ratio [1 ] == ratio [tx ] ||
306+ cs42xx8_ratios [i ].ratio [2 ] == ratio [tx ]) :
307+ (cs42xx8_ratios [i ].ratio [fm [tx ]] == ratio [tx ])) &&
308+ cs42xx8 -> sysclk >= cs42xx8_ratios [i ].min_mclk &&
309+ cs42xx8 -> sysclk <= cs42xx8_ratios [i ].max_mclk ;
310+
311+ if (!ratio [tx ])
312+ condition1 = true;
313+
314+ /* Is the ratio[!tx] valid ? */
315+ condition2 = ((fm [!tx ] == CS42XX8_FM_AUTO ) ?
316+ (cs42xx8_ratios [i ].ratio [0 ] == ratio [!tx ] ||
317+ cs42xx8_ratios [i ].ratio [1 ] == ratio [!tx ] ||
318+ cs42xx8_ratios [i ].ratio [2 ] == ratio [!tx ]) :
319+ (cs42xx8_ratios [i ].ratio [fm [!tx ]] == ratio [!tx ]));
320+
321+ if (!ratio [!tx ])
322+ condition2 = true;
323+
324+ /*
325+ * Both ratio[tx] and ratio[!tx] is valid, then we get
326+ * a proper MFreq.
327+ */
328+ if (condition1 && condition2 )
268329 break ;
269330 }
270331
@@ -273,15 +334,31 @@ static int cs42xx8_hw_params(struct snd_pcm_substream *substream,
273334 return - EINVAL ;
274335 }
275336
276- mask = CS42XX8_FUNCMOD_MFREQ_MASK ;
277- val = cs42xx8_ratios [i ].mclk ;
337+ cs42xx8 -> rate [tx ] = params_rate (params );
278338
279- fm = cs42xx8 -> slave_mode ? CS42XX8_FM_AUTO : cs42xx8_ratios [i ].speed ;
339+ mask = CS42XX8_FUNCMOD_MFREQ_MASK ;
340+ val = cs42xx8_ratios [i ].mfreq ;
280341
281342 regmap_update_bits (cs42xx8 -> regmap , CS42XX8_FUNCMOD ,
282343 CS42XX8_FUNCMOD_xC_FM_MASK (tx ) | mask ,
283- CS42XX8_FUNCMOD_xC_FM (tx , fm ) | val );
344+ CS42XX8_FUNCMOD_xC_FM (tx , fm [tx ]) | val );
345+
346+ return 0 ;
347+ }
348+
349+ static int cs42xx8_hw_free (struct snd_pcm_substream * substream ,
350+ struct snd_soc_dai * dai )
351+ {
352+ struct snd_soc_component * component = dai -> component ;
353+ struct cs42xx8_priv * cs42xx8 = snd_soc_component_get_drvdata (component );
354+ bool tx = substream -> stream == SNDRV_PCM_STREAM_PLAYBACK ;
284355
356+ /* Clear stored rate */
357+ cs42xx8 -> rate [tx ] = 0 ;
358+
359+ regmap_update_bits (cs42xx8 -> regmap , CS42XX8_FUNCMOD ,
360+ CS42XX8_FUNCMOD_xC_FM_MASK (tx ),
361+ CS42XX8_FUNCMOD_xC_FM (tx , CS42XX8_FM_AUTO ));
285362 return 0 ;
286363}
287364
@@ -302,6 +379,7 @@ static const struct snd_soc_dai_ops cs42xx8_dai_ops = {
302379 .set_fmt = cs42xx8_set_dai_fmt ,
303380 .set_sysclk = cs42xx8_set_dai_sysclk ,
304381 .hw_params = cs42xx8_hw_params ,
382+ .hw_free = cs42xx8_hw_free ,
305383 .digital_mute = cs42xx8_digital_mute ,
306384};
307385
0 commit comments