mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "H. Nikolaus Schaller" <hns@goldelico.com>
To: "Linus Walleij" <linus.walleij@linaro.org>,
	"Bartosz Golaszewski" <brgl@kernel.org>,
	"Rob Herring" <robh@kernel.org>,
	"Krzysztof Kozlowski" <krzk+dt@kernel.org>,
	"Conor Dooley" <conor+dt@kernel.org>,
	"Lee Jones" <lee@kernel.org>,
	"Daniel Thompson" <danielt@kernel.org>,
	"Jingoo Han" <jingoohan1@gmail.com>,
	"Pavel Machek" <pavel@kernel.org>,
	"Liam Girdwood" <lgirdwood@gmail.com>,
	"Mark Brown" <broonie@kernel.org>,
	"Aaro Koskinen" <aaro.koskinen@iki.fi>,
	"Andreas Kemnade" <andreas@kemnade.info>,
	"Kevin Hilman" <khilman@baylibre.com>,
	"Roger Quadros" <rogerq@kernel.org>,
	"Tony Lindgren" <tony@atomide.com>,
	"Russell King" <linux@armlinux.org.uk>,
	"Neil Armstrong" <neil.armstrong@linaro.org>,
	"Maarten Lankhorst" <maarten.lankhorst@linux.intel.com>,
	"Maxime Ripard" <mripard@kernel.org>,
	"Thomas Zimmermann" <tzimmermann@suse.de>,
	"David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>,
	"Uwe Kleine-König" <ukleinek@kernel.org>,
	"Helge Deller" <deller@gmx.de>,
	"Jaroslav Kysela" <perex@perex.cz>,
	"Takashi Iwai" <tiwai@suse.com>,
	"Peter Ujfalusi" <peter.ujfalusi@gmail.com>,
	"Jarkko Nikula" <jarkko.nikula@bitmer.com>
Cc: Jessica Zhang <jessica.zhang@oss.qualcomm.com>,
	linux-gpio@vger.kernel.org, devicetree@vger.kernel.org,
	linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org,
	linux-leds@vger.kernel.org, linux-sound@vger.kernel.org,
	linux-omap@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
	linux-pwm@vger.kernel.org, linux-fbdev@vger.kernel.org,
	letux-kernel@openphoenux.org, kernel@pyra-handheld.com,
	"H. Nikolaus Schaller" <hns@goldelico.com>,
	Grond <grond66@riseup.net>
Subject: [PATCH v2 03/20] ASoC: twl4030: Allow APLL rate selection through set_sysclk()
Date: Tue, 24 Nov 2026 08:25:43 +0100	[thread overview]
Message-ID: <0cddcdff2c759301cbf2b11fe902e5df28ecca52.1795505160.git.hns@goldelico.com> (raw)
In-Reply-To: <cover.1795505160.git.hns@goldelico.com>

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 <grond66@riseup.net>
Signed-off-by: H. Nikolaus Schaller <hns@goldelico.com>
---

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


  parent reply	other threads:[~2026-11-24  7:27 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24  7:25 [PATCH v2 00/20] ARM: omap3: pandora: Modernize audio and backlight to use Device Tree to finally remove pdata-quirks H. Nikolaus Schaller
2026-11-24  7:25 ` [PATCH v2 01/20] dt-bindings: gpio: twl4030: Document REGEN GPIO H. Nikolaus Schaller
2026-11-24  7:25 ` [PATCH v2 02/20] gpio: twl4030: Support " H. Nikolaus Schaller
2026-11-24  7:25 ` H. Nikolaus Schaller [this message]
2026-11-24  7:25 ` [PATCH v2 04/20] dt-bindings: sound: pcm1754: Describe PCM1773 H. Nikolaus Schaller
2026-11-24  7:25 ` [PATCH v2 05/20] ASoC: codecs: pcm1754: Add PCM1773 support H. Nikolaus Schaller
2026-11-24  7:25 ` [PATCH v2 06/20] dt-bindings: sound: Document OpenPandora sound card H. Nikolaus Schaller
2026-11-24  7:25 ` [PATCH v2 07/20] ASoC: ti: omap3pandora: Convert sound card to DT H. Nikolaus Schaller
2026-11-24  7:25 ` [PATCH v2 08/20] dt-bindings: mfd: twl4030: Document 64-cycle PWM H. Nikolaus Schaller
2026-11-24  7:25 ` [PATCH v2 09/20] pwm: twl: Support 64-cycle PWM frame H. Nikolaus Schaller
2026-11-24  7:25 ` [PATCH v2 10/20] dt-bindings: backlight: pwm: Document enable guard H. Nikolaus Schaller
2026-11-24  7:25 ` [PATCH v2 11/20] backlight: pwm: Support enable guard period H. Nikolaus Schaller
2026-11-24  7:25 ` [PATCH v2 12/20] drm/panel: td043mtea1: Attach DT backlight H. Nikolaus Schaller
2026-11-24  7:25 ` [PATCH v2 13/20] ARM: dts: omap3pandora: Disable unused DMA channels H. Nikolaus Schaller
2026-11-24  7:25 ` [PATCH v2 14/20] ARM: dts: omap3-pandora: Model LCD power enable dependencies H. Nikolaus Schaller
2026-11-24  7:25 ` [PATCH v2 15/20] ARM: dts: omap3pandora: Enable audio interfaces H. Nikolaus Schaller
2026-11-24  7:25 ` [PATCH v2 16/20] ARM: dts: omap3pandora: Describe PCM1773 H. Nikolaus Schaller
2026-11-24  7:25 ` [PATCH v2 17/20] ARM: dts: omap3pandora: Describe sound card H. Nikolaus Schaller
2026-11-24  7:25 ` [PATCH v2 18/20] ARM: dts: omap3pandora: Switch backlight to PWM H. Nikolaus Schaller
2026-11-24  7:25 ` [PATCH v2 19/20] backlight: Remove legacy Pandora driver H. Nikolaus Schaller
2026-11-24  7:26 ` [PATCH v2 20/20] ARM: OMAP2+: Remove Pandora platform-data quirks H. Nikolaus Schaller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0cddcdff2c759301cbf2b11fe902e5df28ecca52.1795505160.git.hns@goldelico.com \
    --to=hns@goldelico.com \
    --cc=aaro.koskinen@iki.fi \
    --cc=airlied@gmail.com \
    --cc=andreas@kemnade.info \
    --cc=brgl@kernel.org \
    --cc=broonie@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=danielt@kernel.org \
    --cc=deller@gmx.de \
    --cc=devicetree@vger.kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=grond66@riseup.net \
    --cc=jarkko.nikula@bitmer.com \
    --cc=jessica.zhang@oss.qualcomm.com \
    --cc=jingoohan1@gmail.com \
    --cc=kernel@pyra-handheld.com \
    --cc=khilman@baylibre.com \
    --cc=krzk+dt@kernel.org \
    --cc=lee@kernel.org \
    --cc=letux-kernel@openphoenux.org \
    --cc=lgirdwood@gmail.com \
    --cc=linus.walleij@linaro.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-fbdev@vger.kernel.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-leds@vger.kernel.org \
    --cc=linux-omap@vger.kernel.org \
    --cc=linux-pwm@vger.kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=mripard@kernel.org \
    --cc=neil.armstrong@linaro.org \
    --cc=pavel@kernel.org \
    --cc=perex@perex.cz \
    --cc=peter.ujfalusi@gmail.com \
    --cc=robh@kernel.org \
    --cc=rogerq@kernel.org \
    --cc=simona@ffwll.ch \
    --cc=tiwai@suse.com \
    --cc=tony@atomide.com \
    --cc=tzimmermann@suse.de \
    --cc=ukleinek@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®