linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: Brian Norris <briannorris@chromium.org>
To: Judy Hsiao <judyhsiao@chromium.org>
Cc: Heiko Stuebner <heiko@sntech.de>,
	Liam Girdwood <lgirdwood@gmail.com>,
	Rob Herring <robh+dt@kernel.org>, Mark Brown <broonie@kernel.org>,
	Jaroslav Kysela <perex@perex.cz>,
	Chen-Yu Tsai <wenst@chromium.org>,
	alsa-devel@alsa-project.org,
	linux-arm-kernel@lists.infradead.org,
	linux-rockchip@lists.infradead.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 1/3] ASoC: rockchip: i2s: switch BCLK to GPIO
Date: Thu, 16 Jun 2022 13:48:18 -0700	[thread overview]
Message-ID: <YquXEgHkw08TkNuG@google.com> (raw)
In-Reply-To: <20220616155836.3401420-2-judyhsiao@chromium.org>

Hi,

On Thu, Jun 16, 2022 at 03:58:34PM +0000, Judy Hsiao wrote:
> We discoverd that the state of BCLK on, LRCLK off and SD_MODE on
> may cause the speaker melting issue. Removing LRCLK while BCLK
> is present can cause unexpected output behavior including a large
> DC output voltage as described in the Max98357a datasheet.
> 
> In order to:
>   1. prevent BCLK from turning on by other component.
>   2. keep BCLK and LRCLK being present at the same time
> 
> This patch switches BCLK to GPIO func before LRCLK output, and
> configures BCLK func back during LRCLK is output.
> 
> Without this fix, BCLK is turned on 11 ms earlier than LRCK by the
> da7219.
> With this fix, BCLK is turned on only 0.4 ms earlier than LRCK by
> the rockchip codec.
> 
> Signed-off-by: Judy Hsiao <judyhsiao@chromium.org>

It feels like a lot of the noise in this patch is due to adding
additional error handling, unrelated to the real change you're making.
Maybe that deserves a separate patch?

> ---
>  sound/soc/rockchip/rockchip_i2s.c | 134 ++++++++++++++++++++++++------
>  1 file changed, 108 insertions(+), 26 deletions(-)
> 
> diff --git a/sound/soc/rockchip/rockchip_i2s.c b/sound/soc/rockchip/rockchip_i2s.c
> index 47a3971a9ce1..574d3d0900c4 100644
> --- a/sound/soc/rockchip/rockchip_i2s.c
> +++ b/sound/soc/rockchip/rockchip_i2s.c
> @@ -54,8 +54,40 @@ struct rk_i2s_dev {
>  	const struct rk_i2s_pins *pins;
>  	unsigned int bclk_ratio;
>  	spinlock_t lock; /* tx/rx lock */
> +	struct pinctrl *pinctrl;
> +	struct pinctrl_state *bclk_on;
> +	struct pinctrl_state *bclk_off;
>  };
>  
> +static int i2s_pinctrl_select_bclk_on(struct rk_i2s_dev *i2s)
> +{
> +	int ret = 0;
> +
> +	if (!IS_ERR(i2s->pinctrl) && !IS_ERR_OR_NULL(i2s->bclk_on))
> +		ret = pinctrl_select_state(i2s->pinctrl,
> +				     i2s->bclk_on);
> +
> +	if (ret)
> +		dev_err(i2s->dev, "bclk enable failed %d\n", ret);
> +
> +	return ret;
> +}
> +
> +static int i2s_pinctrl_select_bclk_off(struct rk_i2s_dev *i2s)
> +{
> +
> +	int ret = 0;
> +
> +	if (!IS_ERR(i2s->pinctrl) && !IS_ERR_OR_NULL(i2s->bclk_off))
> +		ret = pinctrl_select_state(i2s->pinctrl,
> +				     i2s->bclk_off);
> +
> +	if (ret)
> +		dev_err(i2s->dev, "bclk disable failed %d\n", ret);
> +
> +	return ret;
> +}
> +
>  static int i2s_runtime_suspend(struct device *dev)
>  {
>  	struct rk_i2s_dev *i2s = dev_get_drvdata(dev);
> @@ -92,39 +124,46 @@ static inline struct rk_i2s_dev *to_info(struct snd_soc_dai *dai)
>  	return snd_soc_dai_get_drvdata(dai);
>  }
>  
> -static void rockchip_snd_txctrl(struct rk_i2s_dev *i2s, int on)
> +static int rockchip_snd_txctrl(struct rk_i2s_dev *i2s, int on)
>  {
>  	unsigned int val = 0;
>  	int retry = 10;
> -
> +	int ret = 0;
> +
>  	spin_lock(&i2s->lock);
>  	if (on) {
> -		regmap_update_bits(i2s->regmap, I2S_DMACR,
> +		ret = regmap_update_bits(i2s->regmap, I2S_DMACR,
>  				   I2S_DMACR_TDE_ENABLE, I2S_DMACR_TDE_ENABLE);

I mentioned this off-list, but the 2nd-line indentation alignment *used*
to be in a very particular style, and you've moved that around a lot. To
match the previous style, it should be:

		ret = regmap_update_bits(i2s->regmap, I2S_DMACR,
					 I2S_DMACR_TDE_ENABLE,
					 I2S_DMACR_TDE_ENABLE);

(BTW, if you're using Gmail to view this, you're going to have no idea
what I'm talking about, since it doesn't do whitespace or monospace font
correctly...)

The same applies throughout; most of the 2nd-line, 3rd-line, ...
indentation is a little weird.

> -
> -		regmap_update_bits(i2s->regmap, I2S_XFER,
> +		if (ret < 0)
> +			goto end;
> +		ret = regmap_update_bits(i2s->regmap, I2S_XFER,
>  				   I2S_XFER_TXS_START | I2S_XFER_RXS_START,
>  				   I2S_XFER_TXS_START | I2S_XFER_RXS_START);
> -
> +		if (ret < 0)
> +			goto end;
>  		i2s->tx_start = true;
>  	} else {
>  		i2s->tx_start = false;
>  
> -		regmap_update_bits(i2s->regmap, I2S_DMACR,
> +		ret = regmap_update_bits(i2s->regmap, I2S_DMACR,
>  				   I2S_DMACR_TDE_ENABLE, I2S_DMACR_TDE_DISABLE);
> +		if (ret < 0)
> +			goto end;
>  
>  		if (!i2s->rx_start) {
> -			regmap_update_bits(i2s->regmap, I2S_XFER,
> +			ret = regmap_update_bits(i2s->regmap, I2S_XFER,
>  					   I2S_XFER_TXS_START |
>  					   I2S_XFER_RXS_START,
>  					   I2S_XFER_TXS_STOP |
>  					   I2S_XFER_RXS_STOP);
> -
> +			if (ret < 0)
> +				goto end;
>  			udelay(150);
> -			regmap_update_bits(i2s->regmap, I2S_CLR,
> +			ret = regmap_update_bits(i2s->regmap, I2S_CLR,
>  					   I2S_CLR_TXC | I2S_CLR_RXC,
>  					   I2S_CLR_TXC | I2S_CLR_RXC);
> -
> +			if (ret < 0)
> +				goto end;
>  			regmap_read(i2s->regmap, I2S_CLR, &val);
>  
>  			/* Should wait for clear operation to finish */
> @@ -138,42 +177,55 @@ static void rockchip_snd_txctrl(struct rk_i2s_dev *i2s, int on)
>  			}
>  		}
>  	}
> +end:
>  	spin_unlock(&i2s->lock);
> +	if (ret < 0)
> +		dev_err(i2s->dev, "lrclk update failed\n");
> +
> +	return ret;
>  }
>  
>  static void rockchip_snd_rxctrl(struct rk_i2s_dev *i2s, int on)
>  {
>  	unsigned int val = 0;
>  	int retry = 10;
> +	int ret = 0;
>  
>  	spin_lock(&i2s->lock);
>  	if (on) {
> -		regmap_update_bits(i2s->regmap, I2S_DMACR,
> +		ret = regmap_update_bits(i2s->regmap, I2S_DMACR,
>  				   I2S_DMACR_RDE_ENABLE, I2S_DMACR_RDE_ENABLE);
> +		if (ret < 0)
> +			goto end;
>  
> -		regmap_update_bits(i2s->regmap, I2S_XFER,
> +		ret = regmap_update_bits(i2s->regmap, I2S_XFER,
>  				   I2S_XFER_TXS_START | I2S_XFER_RXS_START,
>  				   I2S_XFER_TXS_START | I2S_XFER_RXS_START);
> -
> +		if (ret < 0)
> +			goto end;
>  		i2s->rx_start = true;
>  	} else {
>  		i2s->rx_start = false;
>  
> -		regmap_update_bits(i2s->regmap, I2S_DMACR,
> +		ret = regmap_update_bits(i2s->regmap, I2S_DMACR,
>  				   I2S_DMACR_RDE_ENABLE, I2S_DMACR_RDE_DISABLE);
> +		if (ret < 0)
> +			goto end;
>  
>  		if (!i2s->tx_start) {
> -			regmap_update_bits(i2s->regmap, I2S_XFER,
> +			ret = regmap_update_bits(i2s->regmap, I2S_XFER,
>  					   I2S_XFER_TXS_START |
> -					   I2S_XFER_RXS_START,
> +						   I2S_XFER_RXS_START,
>  					   I2S_XFER_TXS_STOP |
>  					   I2S_XFER_RXS_STOP);
> -
> +			if (ret < 0)
> +				goto end;
>  			udelay(150);
> -			regmap_update_bits(i2s->regmap, I2S_CLR,
> +			ret = regmap_update_bits(i2s->regmap, I2S_CLR,
>  					   I2S_CLR_TXC | I2S_CLR_RXC,
>  					   I2S_CLR_TXC | I2S_CLR_RXC);
> -
> +			if (ret < 0)
> +				goto end;
>  			regmap_read(i2s->regmap, I2S_CLR, &val);
>  
>  			/* Should wait for clear operation to finish */
> @@ -187,7 +239,12 @@ static void rockchip_snd_rxctrl(struct rk_i2s_dev *i2s, int on)
>  			}
>  		}
>  	}
> +end:
>  	spin_unlock(&i2s->lock);
> +	if (ret < 0)
> +		dev_err(i2s->dev, "lrclk update failed\n");
> +
> +	return ret;
>  }
>  
>  static int rockchip_i2s_set_fmt(struct snd_soc_dai *cpu_dai,
> @@ -425,17 +482,25 @@ static int rockchip_i2s_trigger(struct snd_pcm_substream *substream,
>  	case SNDRV_PCM_TRIGGER_RESUME:
>  	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
>  		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
> -			rockchip_snd_rxctrl(i2s, 1);
> +			ret = rockchip_snd_rxctrl(i2s, 1);
>  		else
> -			rockchip_snd_txctrl(i2s, 1);
> +			ret = rockchip_snd_txctrl(i2s, 1);
> +		if (ret < 0)
> +			return ret;
> +		i2s_pinctrl_select_bclk_on(i2s);
>  		break;
>  	case SNDRV_PCM_TRIGGER_SUSPEND:
>  	case SNDRV_PCM_TRIGGER_STOP:
>  	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
> -		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
> -			rockchip_snd_rxctrl(i2s, 0);
> -		else
> -			rockchip_snd_txctrl(i2s, 0);
> +		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
> +			if (!i2s->tx_start)
> +				i2s_pinctrl_select_bclk_off(i2s);
> +			ret = rockchip_snd_rxctrl(i2s, 0);
> +		} else {
> +			if (!i2s->rx_start)
> +				i2s_pinctrl_select_bclk_off(i2s);
> +			ret = rockchip_snd_txctrl(i2s, 0);
> +		}
>  		break;
>  	default:
>  		ret = -EINVAL;
> @@ -736,6 +801,23 @@ static int rockchip_i2s_probe(struct platform_device *pdev)
>  	}
>  
>  	i2s->bclk_ratio = 64;
> +	i2s->pinctrl = devm_pinctrl_get(&pdev->dev);
> +	if (IS_ERR(i2s->pinctrl))
> +		dev_err(&pdev->dev, "failed to find i2s pinctrl\n");
> +
> +	i2s->bclk_on = pinctrl_lookup_state(i2s->pinctrl,
> +				   "bclk_on");
> +	if (!IS_ERR_OR_NULL(i2s->bclk_on)) {
> +		dev_info(&pdev->dev, "switch bclk to GPIO func\n");

I don't think we need this printed at the KERN_INFO level. Either drop
it, or maybe dev_dbg().

Brian

> +		i2s->bclk_off = pinctrl_lookup_state(i2s->pinctrl,
> +				  "bclk_off");
> +		if (IS_ERR_OR_NULL(i2s->bclk_off)) {
> +			dev_err(&pdev->dev, "failed to find i2s bclk_off\n");
> +			goto err_clk;
> +		}
> +	}
> +
> +	i2s_pinctrl_select_bclk_off(i2s);
>  
>  	dev_set_drvdata(&pdev->dev, i2s);
>  
> -- 
> 2.36.1.476.g0c4daa206d-goog
> 

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

  parent reply	other threads:[~2022-06-16 20:53 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-16 15:58 [PATCH v2 0/3] ASoC: rockchip: i2s: switch BCLK to GPIO Judy Hsiao
2022-06-16 15:58 ` [PATCH v2 1/3] " Judy Hsiao
2022-06-16 19:03   ` kernel test robot
2022-06-16 20:48   ` Brian Norris [this message]
2022-06-17  4:21     ` Judy Hsiao
2022-06-16 15:58 ` [PATCH v2 2/3] arm64: dts: rk3399: " Judy Hsiao
2022-06-16 20:39   ` Brian Norris
2022-06-16 15:58 ` [PATCH v2 3/3] ASoC: dt-bindings: rockchip: Document pinctrl-names for i2s Judy Hsiao
2022-06-16 20:39   ` Brian Norris
2022-06-17  4:42 ` [PATCH v3 0/3] ASoC: rockchip: i2s: switch BCLK to GPIO Judy Hsiao
2022-06-17  4:42   ` [PATCH v3 1/3] " Judy Hsiao
2022-06-17 20:46     ` Brian Norris
2022-06-17  4:42   ` [PATCH v3 2/3] arm64: dts: rk3399: " Judy Hsiao
2022-06-17  4:42   ` [PATCH v3 3/3] ASoC: dt-bindings: rockchip: Document pinctrl-names for i2s Judy Hsiao
2022-06-17  8:45   ` [PATCH v3 0/3] ASoC: rockchip: i2s: switch BCLK to GPIO Mark Brown

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=YquXEgHkw08TkNuG@google.com \
    --to=briannorris@chromium.org \
    --cc=alsa-devel@alsa-project.org \
    --cc=broonie@kernel.org \
    --cc=heiko@sntech.de \
    --cc=judyhsiao@chromium.org \
    --cc=lgirdwood@gmail.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rockchip@lists.infradead.org \
    --cc=perex@perex.cz \
    --cc=robh+dt@kernel.org \
    --cc=wenst@chromium.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;
as well as URLs for NNTP newsgroup(s).