linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Cezary Rojewski <cezary.rojewski@intel.com>
To: Thomas Preston <thomas.preston@codethink.co.uk>
Cc: Liam Girdwood <lgirdwood@gmail.com>,
	Mark Brown <broonie@kernel.org>, Rob Herring <robh+dt@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.com>,
	Charles Keepax <ckeepax@opensource.cirrus.com>,
	Jerome Brunet <jbrunet@baylibre.com>,
	Srinivas Kandagatla <srinivas.kandagatla@linaro.org>,
	Marco Felsch <m.felsch@pengutronix.de>,
	Paul Cercueil <paul@crapouillou.net>,
	Kirill Marinushkin <kmarinushkin@birdec.tech>,
	Cheng-Yi Chiang <cychiang@chromium.org>,
	Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>,
	Vinod Koul <vkoul@kernel.org>,
	Annaliese McDermond <nh6z@nh6z.net>,
	alsa-devel@alsa-project.org, devicetree@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v1 4/4] ASoC: tda7802: Add speaker-test sysfs
Date: Wed, 12 Jun 2019 19:56:16 +0200	[thread overview]
Message-ID: <607e83fb-3d37-cf05-ded5-4f582352bdb0@intel.com> (raw)
In-Reply-To: <20190611174909.12162-5-thomas.preston@codethink.co.uk>

On 2019-06-11 19:49, Thomas Preston wrote:
> Add speaker_test device attribute. When the speaker-test node is read
> the hardware speaker test is started.
> 
> 	$ cat /sys/devices/.../device:00/speaker_test
> 	04 04 04 04
> 
> Signed-off-by: Thomas Preston <thomas.preston@codethink.co.uk>
> Cc: Patrick Glaser <pglaser@tesla.com>
> Cc: Rob Duncan <rduncan@tesla.com>
> Cc: Nate Case <ncase@tesla.com>
> ---
>   sound/soc/codecs/tda7802.c | 172 +++++++++++++++++++++++++++++++++++++++++++++
>   1 file changed, 172 insertions(+)
> 
> diff --git a/sound/soc/codecs/tda7802.c b/sound/soc/codecs/tda7802.c
> index 62aae011d9f1..edfa752c0c9f 100644
> --- a/sound/soc/codecs/tda7802.c
> +++ b/sound/soc/codecs/tda7802.c
> @@ -157,6 +157,7 @@ static const u8 IB3_FORMAT[4][4] = {
>   #define DUMP_NUM_REGS			   (DUMP_NUM_BLOCK * 2)
>   
>   struct tda7802_priv {
> +	struct mutex mutex;
>   	struct i2c_client *i2c;
>   	struct regmap *regmap;
>   	struct regulator *enable_reg;
> @@ -458,6 +459,159 @@ static struct snd_soc_dai_driver tda7802_dai_driver = {
>   	.ops = &tda7802_dai_ops,
>   };
>   
> +/* The speaker test is triggered by reading a sysfs attribute file attached to
> + * the I2C device. The user space thread that's doing the reading is blocked
> + * until the test completes (or times out). We only permit one test to be in
> + * progress at a time.
> + */
> +
> +static int speaker_test_start(struct regmap *regmap)
> +{
> +	int err;
> +
> +	err = regmap_update_bits(regmap, TDA7802_IB5, IB5_AMPLIFIER_ON, 0);
> +	if (err < 0) {
> +		dev_err(regmap_get_device(regmap),
> +			"Cannot disable amp for speaker test (%d)\n", err);
> +		return err;
> +	}
> +
> +	err = regmap_update_bits(regmap, TDA7802_IB4, IB4_DIAG_MODE_ENABLE, 0);
> +	if (err < 0) {
> +		dev_err(regmap_get_device(regmap),
> +			"Cannot disable diag mode before speaker test (%d)\n",
> +			err);
> +		return err;
> +	}
> +
> +	/* Seem to need at least a 15 ms delay here before the rising
> +	 * edge. Otherwise the diagnostics never complete (perhaps
> +	 * they never start).
> +	 */
> +	msleep(20);
> +
> +	err = regmap_update_bits(regmap, TDA7802_IB4,
> +				 IB4_DIAG_MODE_ENABLE, IB4_DIAG_MODE_ENABLE);
> +	if (err < 0) {
> +		dev_err(regmap_get_device(regmap),
> +			"Cannot enable diag mode for speaker test (%d)\n", err);
> +		return err;
> +	}
> +
> +	return 0;

You may want to follow path set by speaker_test_stop: replace "return 0" 
with "return err" and have only error msg found within preceding 
if-statement.

> +}
> +
> +static int speaker_test_stop(struct regmap *regmap)
> +{
> +	int err;
> +
> +	err = regmap_update_bits(regmap, TDA7802_IB4, IB4_DIAG_MODE_ENABLE, 0);
> +	if (err < 0)
> +		dev_err(regmap_get_device(regmap),
> +			"Cannot disable diag mode after speaker test (%d)\n",
> +			err);
> +	return err;
> +}
> +
> +/* We poll the test completion bit, letting the current thread sleep
> + * until we're done. These values are not critical.
> + */
> +#define SPEAKER_TEST_DONE_POLL_PERIOD_US 5000
> +#define SPEAKER_TEST_DONE_TIMEOUT_US	 5000000
> +
> +static int speaker_test_check(struct tda7802_priv *tda7802,
> +		unsigned int *status)
> +{
> +	struct regmap *regmap = tda7802->regmap;
> +	struct device *dev = &tda7802->i2c->dev;
> +	int reg_err = 0, err = 0, i, amp_on;

Both reg_err and err are initialized unnecessarily.

> +	unsigned int val;
> +
> +	reg_err = regulator_enable(tda7802->enable_reg);
> +	if (reg_err < 0) {
> +		dev_err(dev, "Could not enable (speaker-test).\n");
> +		return reg_err;
> +	}
> +	msleep(ENABLE_DELAY_MS);
> +
> +	/* we should return amp on state when speaker-test is done */
> +	err = regmap_read(regmap, TDA7802_IB5, &amp_on);
> +	if (err < 0) {
> +		dev_err(dev, "Could not read amp on state (speaker-test)\n");
> +		goto speaker_test_disable;
> +	}
> +
> +	for (i = 0; i < CHANNELS_PER_AMP; ++i)
> +		status[i] = 0xff;
> +
> +	err = speaker_test_start(regmap);
> +	if (err < 0)
> +		goto speaker_test_restore;
> +
> +	/* Wait until DB0_STARTUP_DIAG_STATUS is set */
> +	err = regmap_read_poll_timeout(regmap, TDA7802_DB0, val,
> +			val & DB0_STARTUP_DIAG_STATUS,
> +			SPEAKER_TEST_DONE_POLL_PERIOD_US,
> +			SPEAKER_TEST_DONE_TIMEOUT_US);
> +	if (err < 0) {
> +		if (err == -ENODATA)
> +			dev_err(dev,
> +				"Speaker diagnostic test did not complete\n");
> +		speaker_test_stop(regmap);
> +		goto speaker_test_restore;
> +	}
> +
> +	err = speaker_test_stop(regmap);
> +	if (err < 0)
> +		goto speaker_test_restore;

This sequence looks weird and as if it could be simplified but I might 
be just plain wrong. speaker_test_stop gets invoked whether or not 
regmap_read_poll_timeout fails, though only in "happy" path its return 
is account for. If it's not too much to ask, could you clarify?

> +
> +	for (i = 0; i < CHANNELS_PER_AMP; ++i) {
> +		err = regmap_read(regmap, TDA7802_DB1 + i, status + i);
> +		if (err < 0) {
> +			dev_err(dev,
> +				"Cannot get speaker %d status (%d)\n", i, err);
> +			goto speaker_test_restore;
> +		}
> +	}
> +
> +speaker_test_restore:
> +	err = regmap_update_bits(regmap, TDA7802_IB5, IB5_AMPLIFIER_ON,
> +			amp_on);
> +	if (err < 0)
> +		dev_err(dev, "Could not restore amp on state (speaker-test)\n");
> +
> +speaker_test_disable:
> +	reg_err = regulator_disable(tda7802->enable_reg);
> +	if (reg_err < 0) {
> +		dev_err(dev, "Could not disable (speaker-test).\n");
> +		return reg_err;
> +	}
> +	return err;
> +}
> +
> +static ssize_t speaker_test_show(struct device *dev,
> +		struct device_attribute *attr, char *buf)
> +{
> +	struct tda7802_priv *tda7802 = dev_get_drvdata(dev);
> +	unsigned int channel_status[CHANNELS_PER_AMP];
> +	char *str = buf;
> +	int ret, i;
> +
> +	mutex_lock(&tda7802->mutex);
> +	ret = speaker_test_check(tda7802, channel_status);
> +	mutex_unlock(&tda7802->mutex);
> +	if (ret < 0)
> +		return ret;
> +
> +	for (i = 0; i < CHANNELS_PER_AMP; i++)
> +		str += sprintf(str, "%02x ", channel_status[i]);
> +	str += sprintf(str, "\n");
> +
> +	return str - buf;
> +}
> +
> +static DEVICE_ATTR_RO(speaker_test);
> +
>   static ssize_t enable_show(struct device *dev, struct device_attribute *attr,
>   		char *buf)
>   {
> @@ -537,12 +691,28 @@ static int tda7802_probe(struct snd_soc_component *component)
>   
>   	dev_dbg(dev, "%s\n", __func__);
>   
> +	/* Device is ready, now we should create sysfs attributes.
> +	 * Rememer to cleanup
> +	 */
>   	err = device_create_file(dev, &dev_attr_enable);
>   	if (err < 0) {
>   		dev_err(dev, "Could not create enable attr\n");
>   		return err;
>   	}
>   
> +	mutex_init(&tda7802->mutex);
> +	err = device_create_file(dev, &dev_attr_speaker_test);
> +	if (err < 0) {
> +		dev_err(dev, "Could not create speaker_test attr\n");
> +		goto cleanup_speaker_test;
> +	}
> +
> +	return 0;
> +
> +cleanup_speaker_test:
> +	mutex_destroy(&tda7802->mutex);
> +	device_remove_file(&tda7802->i2c->dev, &dev_attr_enable);
> +
>   	return err;
>   }
>   
> @@ -550,6 +720,8 @@ static void tda7802_remove(struct snd_soc_component *component)
>   {
>   	struct tda7802_priv *tda7802 = snd_soc_component_get_drvdata(component);
>   
> +	device_remove_file(&tda7802->i2c->dev, &dev_attr_speaker_test);
> +	mutex_destroy(&tda7802->mutex);
>   	device_remove_file(&tda7802->i2c->dev, &dev_attr_enable);
>   }
>   
> 

      reply	other threads:[~2019-06-12 17:56 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-11 17:49 [PATCH v1 0/4] ASoC: Codecs: Add TDA7802 codec Thomas Preston
2019-06-11 17:49 ` [PATCH v1 1/4] dt-bindings: ASoC: Add TDA7802 amplifier Thomas Preston
2019-06-12  8:10   ` Charles Keepax
2019-06-11 17:49 ` [PATCH v1 2/4] ASoC: Add codec driver for ST TDA7802 Thomas Preston
2019-06-13 18:34   ` Mark Brown
2019-06-14 21:17   ` Kirill Marinushkin
2019-06-11 17:49 ` [PATCH v1 3/4] ASoC: tda7802: Add enable device attribute Thomas Preston
2019-06-12 17:14   ` Cezary Rojewski
2019-06-13 18:14   ` Mark Brown
2019-06-11 17:49 ` [PATCH v1 4/4] ASoC: tda7802: Add speaker-test sysfs Thomas Preston
2019-06-12 17:56   ` Cezary Rojewski [this message]

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=607e83fb-3d37-cf05-ded5-4f582352bdb0@intel.com \
    --to=cezary.rojewski@intel.com \
    --cc=alsa-devel@alsa-project.org \
    --cc=broonie@kernel.org \
    --cc=ckeepax@opensource.cirrus.com \
    --cc=cychiang@chromium.org \
    --cc=devicetree@vger.kernel.org \
    --cc=jbrunet@baylibre.com \
    --cc=kmarinushkin@birdec.tech \
    --cc=kuninori.morimoto.gx@renesas.com \
    --cc=lgirdwood@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=m.felsch@pengutronix.de \
    --cc=mark.rutland@arm.com \
    --cc=nh6z@nh6z.net \
    --cc=paul@crapouillou.net \
    --cc=perex@perex.cz \
    --cc=robh+dt@kernel.org \
    --cc=srinivas.kandagatla@linaro.org \
    --cc=thomas.preston@codethink.co.uk \
    --cc=tiwai@suse.com \
    --cc=vkoul@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;
as well as URLs for NNTP newsgroup(s).