ASoC: pcm3168a: refactor hw_params routine
authorNikita Yushchenko <nikita.yoush@cogentembedded.com>
Tue, 8 Feb 2022 08:42:18 +0000 (11:42 +0300)
committerMark Brown <broonie@kernel.org>
Mon, 14 Feb 2022 12:52:53 +0000 (12:52 +0000)
- group together code lines that calculate value for msad/msda field

- rename variables to better match their meaning:
    val -> ms,
    max_ratio -> num_scki_ratios

- update variable types to match exactly parameters or return types
  of the calls where those variables are used

- write two fields of the same register in a single regmap call

Signed-off-by: Nikita Yushchenko <nikita.yoush@cogentembedded.com>
Link: https://lore.kernel.org/r/20220208084220.1289836-3-nikita.yoush@cogentembedded.com
Signed-off-by: Mark Brown <broonie@kernel.org>
sound/soc/codecs/pcm3168a.c

index 987c584..526e456 100644 (file)
@@ -462,40 +462,45 @@ static int pcm3168a_hw_params(struct snd_pcm_substream *substream,
        struct pcm3168a_priv *pcm3168a = snd_soc_component_get_drvdata(component);
        struct pcm3168a_io_params *io_params = &pcm3168a->io_params[dai->id];
        bool master_mode;
-       u32 val, mask, shift, reg;
-       unsigned int rate, fmt, ratio, max_ratio;
-       unsigned int tdm_slots;
-       int i, slot_width;
-
-       rate = params_rate(params);
-
-       ratio = pcm3168a->sysclk / rate;
+       unsigned int reg, mask, ms, ms_shift, fmt, fmt_shift, ratio, tdm_slots;
+       int i, num_scki_ratios, slot_width;
 
        if (dai->id == PCM3168A_DAI_DAC) {
-               max_ratio = PCM3168A_NUM_SCKI_RATIOS_DAC;
+               num_scki_ratios = PCM3168A_NUM_SCKI_RATIOS_DAC;
                reg = PCM3168A_DAC_PWR_MST_FMT;
-               mask = PCM3168A_DAC_MSDA_MASK;
-               shift = PCM3168A_DAC_MSDA_SHIFT;
+               mask = PCM3168A_DAC_MSDA_MASK | PCM3168A_DAC_FMT_MASK;
+               ms_shift = PCM3168A_DAC_MSDA_SHIFT;
+               fmt_shift = PCM3168A_DAC_FMT_SHIFT;
        } else {
-               max_ratio = PCM3168A_NUM_SCKI_RATIOS_ADC;
+               num_scki_ratios = PCM3168A_NUM_SCKI_RATIOS_ADC;
                reg = PCM3168A_ADC_MST_FMT;
-               mask = PCM3168A_ADC_MSAD_MASK;
-               shift = PCM3168A_ADC_MSAD_SHIFT;
+               mask = PCM3168A_ADC_MSAD_MASK | PCM3168A_ADC_FMTAD_MASK;
+               ms_shift = PCM3168A_ADC_MSAD_SHIFT;
+               fmt_shift = PCM3168A_ADC_FMTAD_SHIFT;
        }
 
        master_mode = io_params->master_mode;
-       fmt = io_params->fmt;
 
-       for (i = 0; i < max_ratio; i++) {
-               if (pcm3168a_scki_ratios[i] == ratio)
-                       break;
-       }
+       if (master_mode) {
+               ratio = pcm3168a->sysclk / params_rate(params);
 
-       if (i == max_ratio) {
-               dev_err(component->dev, "unsupported sysclk ratio\n");
-               return -EINVAL;
+               for (i = 0; i < num_scki_ratios; i++) {
+                       if (pcm3168a_scki_ratios[i] == ratio)
+                               break;
+               }
+
+               if (i == num_scki_ratios) {
+                       dev_err(component->dev, "unsupported sysclk ratio\n");
+                       return -EINVAL;
+               }
+
+               ms = (i + 1);
+       } else {
+               ms = 0;
        }
 
+       fmt = io_params->fmt;
+
        if (io_params->slot_width)
                slot_width = io_params->slot_width;
        else
@@ -553,22 +558,8 @@ static int pcm3168a_hw_params(struct snd_pcm_substream *substream,
                }
        }
 
-       if (master_mode)
-               val = ((i + 1) << shift);
-       else
-               val = 0;
-
-       regmap_update_bits(pcm3168a->regmap, reg, mask, val);
-
-       if (dai->id == PCM3168A_DAI_DAC) {
-               mask = PCM3168A_DAC_FMT_MASK;
-               shift = PCM3168A_DAC_FMT_SHIFT;
-       } else {
-               mask = PCM3168A_ADC_FMTAD_MASK;
-               shift = PCM3168A_ADC_FMTAD_SHIFT;
-       }
-
-       regmap_update_bits(pcm3168a->regmap, reg, mask, fmt << shift);
+       regmap_update_bits(pcm3168a->regmap, reg, mask,
+                       (ms << ms_shift) | (fmt << fmt_shift));
 
        return 0;
 }