The TWL4030 APLL supplies the audio clocks used by the Pandora sound
card, including playback through an external DAC. Playback does not
necessarily execute the TWL4030 codec's hw_params() callback, which
previously provided the APLL rate selection.

Allow the machine driver to select the APLL rate through set_sysclk()
so that the clock can be configured for playback-only streams.

Suggested-by: Grond <[email protected]>
Signed-off-by: H. Nikolaus Schaller <[email protected]>
---

Notes:
    Changes against V1:
    - Andreas Kemnade suggested modeling the APLL as a clock provider
      to enable a generic audio card; V2 retains the ASoC set_sysclk()
      interface instead. This alternative was not implemented.
    - Clarify in the commit message why playback-only streams require an
      explicit APLL clock-selection interface.
    - Take over patch authorship because Grond cannot be reached to
      confirm a full name or Signed-off-by. Credit the historical
      contribution with Suggested-by rather than asserting code authorship
      or a DCO sign-off.
    - Historical source: commit 5497d5a29f53 (Grond, 2021-12-15),
      "ASoC: twl4030-codec: Allow setting APLL rate through the
      .set_sysclk() interface". The present patch is derived from
      this implementation, with subsequent revisions.
    - Code and commit-message review assisted by ChatGPT.

 include/linux/mfd/twl4030-audio.h |   3 +
 sound/soc/codecs/twl4030.c        | 142 +++++++++++++++++++++---------
 2 files changed, 103 insertions(+), 42 deletions(-)

diff --git a/include/linux/mfd/twl4030-audio.h 
b/include/linux/mfd/twl4030-audio.h
index 1c28605dfda80..4378eef97b0fc 100644
--- a/include/linux/mfd/twl4030-audio.h
+++ b/include/linux/mfd/twl4030-audio.h
@@ -244,6 +244,9 @@
 #define TWL4030_VIBRA_SEL              0x10
 #define TWL4030_VIBRA_DIR_SEL          0x20
 
+/* clock ID used for setting APLL rate */
+#define TWL4030_CLOCK_APLL             0xffff
+
 /* TWL4030 codec resource IDs */
 enum twl4030_audio_res {
        TWL4030_AUDIO_RES_POWER = 0,
diff --git a/sound/soc/codecs/twl4030.c b/sound/soc/codecs/twl4030.c
index e207721b99414..3c9ed38004783 100644
--- a/sound/soc/codecs/twl4030.c
+++ b/sound/soc/codecs/twl4030.c
@@ -198,6 +198,38 @@ static void twl4030_codec_enable(struct snd_soc_component 
*component, int enable
        udelay(10);
 }
 
+static int twl4030_perform_writes(struct snd_soc_component *component,
+                                 const unsigned int *regs,
+                                 const unsigned int *vals,
+                                 unsigned int n)
+{
+       struct twl4030_priv *twl4030 = snd_soc_component_get_drvdata(component);
+       bool reboot_codec = twl4030->codec_powered;
+       unsigned int i;
+       int ret = 0;
+
+       if (reboot_codec)
+               twl4030_codec_enable(component, 0);
+
+       for (i = 0; i < n; ++i) {
+               ret = twl4030_write(component, regs[i], vals[i]);
+               if (ret)
+                       break;
+       }
+
+       if (reboot_codec)
+               twl4030_codec_enable(component, 1);
+
+       return ret;
+}
+
+static inline int
+twl4030_perform_single_write(struct snd_soc_component *component,
+                            unsigned int reg, unsigned int val)
+{
+       return twl4030_perform_writes(component, &reg, &val, 1);
+}
+
 static void
 twl4030_get_board_param_values(struct twl4030_board_params *board_params,
                               struct device_node *node)
@@ -1697,38 +1729,16 @@ static void twl4030_shutdown(struct snd_pcm_substream 
*substream,
                twl4030_tdm_enable(component, substream->stream, 0);
 }
 
-static int twl4030_hw_params(struct snd_pcm_substream *substream,
-                            struct snd_pcm_hw_params *params,
-                            struct snd_soc_dai *dai)
+static int twl4030_get_codec_mode_for_apll_rate(struct snd_soc_component 
*component,
+                                               unsigned int rate, u8 *res)
 {
-       struct snd_soc_component *component = dai->component;
-       struct twl4030_priv *twl4030 = snd_soc_component_get_drvdata(component);
-       u8 mode, old_mode, format, old_format;
-
-        /* If the substream has 4 channel, do the necessary setup */
-       if (params_channels(params) == 4) {
-               format = twl4030_read(component, TWL4030_REG_AUDIO_IF);
-               mode = twl4030_read(component, TWL4030_REG_CODEC_MODE);
-
-               /* Safety check: are we in the correct operating mode and
-                * the interface is in TDM mode? */
-               if ((mode & TWL4030_OPTION_1) &&
-                   ((format & TWL4030_AIF_FORMAT) == TWL4030_AIF_FORMAT_TDM))
-                       twl4030_tdm_enable(component, substream->stream, 1);
-               else
-                       return -EINVAL;
-       }
-
-       if (twl4030->configured)
-               /* Ignoring hw_params for already configured DAI */
-               return 0;
+       u8 old_mode, mode;
 
-       /* bit rate */
        old_mode = twl4030_read(component,
                                TWL4030_REG_CODEC_MODE) & ~TWL4030_CODECPDZ;
        mode = old_mode & ~TWL4030_APLL_RATE;
 
-       switch (params_rate(params)) {
+       switch (rate) {
        case 8000:
                mode |= TWL4030_APLL_RATE_8000;
                break;
@@ -1760,11 +1770,50 @@ static int twl4030_hw_params(struct snd_pcm_substream 
*substream,
                mode |= TWL4030_APLL_RATE_96000;
                break;
        default:
-               dev_err(component->dev, "%s: unknown rate %d\n", __func__,
-                       params_rate(params));
+               dev_err(component->dev, "%s: unknown rate %u\n", __func__,
+                       rate);
                return -EINVAL;
        }
 
+       *res = mode;
+
+       return mode != old_mode ? 1 : 0;
+}
+
+static int twl4030_hw_params(struct snd_pcm_substream *substream,
+                            struct snd_pcm_hw_params *params,
+                            struct snd_soc_dai *dai)
+{
+       struct snd_soc_component *component = dai->component;
+       struct twl4030_priv *twl4030 = snd_soc_component_get_drvdata(component);
+       u8 mode, format, old_format;
+       int s, ret;
+       unsigned int regs[2], vals[2];
+
+        /* If the substream has 4 channel, do the necessary setup */
+       if (params_channels(params) == 4) {
+               format = twl4030_read(component, TWL4030_REG_AUDIO_IF);
+               mode = twl4030_read(component, TWL4030_REG_CODEC_MODE);
+
+               /* Safety check: are we in the correct operating mode and
+                * the interface is in TDM mode?
+                */
+               if ((mode & TWL4030_OPTION_1) &&
+                   ((format & TWL4030_AIF_FORMAT) == TWL4030_AIF_FORMAT_TDM))
+                       twl4030_tdm_enable(component, substream->stream, 1);
+               else
+                       return -EINVAL;
+       }
+
+       if (twl4030->configured)
+               /* Ignoring hw_params for already configured DAI */
+               return 0;
+
+       s = twl4030_get_codec_mode_for_apll_rate(component,
+                                                params_rate(params), &mode);
+       if (s < 0)
+               return s;
+
        /* sample size */
        old_format = twl4030_read(component, TWL4030_REG_AUDIO_IF);
        format = old_format;
@@ -1782,20 +1831,14 @@ static int twl4030_hw_params(struct snd_pcm_substream 
*substream,
                return -EINVAL;
        }
 
-       if (format != old_format || mode != old_mode) {
-               if (twl4030->codec_powered) {
-                       /*
-                        * If the codec is powered, than we need to toggle the
-                        * codec power.
-                        */
-                       twl4030_codec_enable(component, 0);
-                       twl4030_write(component, TWL4030_REG_CODEC_MODE, mode);
-                       twl4030_write(component, TWL4030_REG_AUDIO_IF, format);
-                       twl4030_codec_enable(component, 1);
-               } else {
-                       twl4030_write(component, TWL4030_REG_CODEC_MODE, mode);
-                       twl4030_write(component, TWL4030_REG_AUDIO_IF, format);
-               }
+       if (format != old_format || s) {
+               regs[0] = TWL4030_REG_CODEC_MODE;
+               vals[0] = mode;
+               regs[1] = TWL4030_REG_AUDIO_IF;
+               vals[1] = format;
+               ret = twl4030_perform_writes(component, regs, vals, 
ARRAY_SIZE(regs));
+               if (ret)
+                       return ret;
        }
 
        /* Store the important parameters for the DAI configuration and set
@@ -1820,6 +1863,21 @@ static int twl4030_set_dai_sysclk(struct snd_soc_dai 
*codec_dai, int clk_id,
 {
        struct snd_soc_component *component = codec_dai->component;
        struct twl4030_priv *twl4030 = snd_soc_component_get_drvdata(component);
+       int s;
+       u8 mode;
+
+       if (clk_id == TWL4030_CLOCK_APLL) {
+               s = twl4030_get_codec_mode_for_apll_rate(component,
+                                                        freq, &mode);
+               if (s < 0)
+                       return s;
+
+               if (s)
+                       return twl4030_perform_single_write(component,
+                                       TWL4030_REG_CODEC_MODE, mode);
+
+               return 0;
+       }
 
        switch (freq) {
        case 19200000:
-- 
2.56.0

Reply via email to