mbox series

[v3,00/10] ASoC: mediatek: mt8183-mt6358-ts3a227-max98357: support WoV

Message ID 20191014102022.236013-1-tzungbi@google.com
Headers show
Series ASoC: mediatek: mt8183-mt6358-ts3a227-max98357: support WoV | expand

Message

Tzung-Bi Shih Oct. 14, 2019, 10:20 a.m. UTC
This series makes mt6358, cros_ec_codec, and mt8183-mt6358-ts3a227-max98357
support WoV (wake on voice).

The first 3 commits are some cleanups and refactors.  It looks like
breaking the existing interface.  But please be noticed that, the
cros_ec_codec has not used by any real device yet.  The refactor is
very necessary to keep the style consistent and for easier to further
extend and maintain.
  platform/chrome: cros_ec: remove unused EC feature
  ASoC: cros_ec_codec: refactor I2S RX
  ASoC: cros_ec_codec: extract DMIC EC command from I2S RX

The 4th commit extends the feature offered from EC codec.
  platform/chrome: cros_ec: add common commands for EC codec

The 5th commit changes the behavior of setting and getting DMIC gains.
  ASoC: cros_ec_codec: read max DMIC gain from EC codec

The 6th and 7th commit make cros_ec_codec support WoV.
  ASoC: dt-bindings: cros_ec_codec: add SHM bindings
  ASoC: cros_ec_codec: support WoV

The 8th commit sets necessary registers on mt6358 to support WoV.
  ASoC: mediatek: mt6358: support WoV

The last 2 commit make machine driver mt8183-mt6358-ts3a227-max98357
support WoV if ec-codec is in DTS.
  ASoC: dt-bindings: mt8183: add ec-codec
  ASoC: mediatek: mt8183: support WoV

Changes from v1:
- fix a compile error and make kbuild bot happy
https://mailman.alsa-project.org/pipermail/alsa-devel/2019-October/156315.html
Changes from v2:
- rebase upon to "don't use snd_pcm_ops" series
https://mailman.alsa-project.org/pipermail/alsa-devel/2019-October/156170.html
- fix sparse errors
https://mailman.alsa-project.org/pipermail/alsa-devel/2019-October/156328.html
- use "reg" for SHM binding
https://mailman.alsa-project.org/pipermail/alsa-devel/2019-October/156657.html

Tzung-Bi Shih (10):
  WIP: platform/chrome: cros_ec: remove unused EC feature
  WIP: ASoC: cros_ec_codec: refactor I2S RX
  WIP: ASoC: cros_ec_codec: extract DMIC EC command from I2S RX
  WIP: platform/chrome: cros_ec: add common commands for EC codec
  WIP: ASoC: cros_ec_codec: read max DMIC gain from EC codec
  WIP: ASoC: dt-bindings: cros_ec_codec: add SHM bindings
  WIP: ASoC: cros_ec_codec: support WoV
  WIP: ASoC: mediatek: mt6358: support WoV
  WIP: ASoC: dt-bindings: mt8183: add ec-codec
  WIP: ASoC: mediatek: mt8183: support WoV

 .../bindings/sound/google,cros-ec-codec.txt   |   24 +-
 .../sound/mt8183-mt6358-ts3a227-max98357.txt  |    3 +
 drivers/platform/chrome/cros_ec_trace.c       |    5 +-
 .../linux/platform_data/cros_ec_commands.h    |  285 ++++-
 sound/soc/codecs/cros_ec_codec.c              | 1128 +++++++++++++----
 sound/soc/codecs/mt6358.c                     |  105 ++
 sound/soc/mediatek/Kconfig                    |    1 +
 .../mt8183/mt8183-mt6358-ts3a227-max98357.c   |   70 +-
 8 files changed, 1296 insertions(+), 325 deletions(-)

Comments

Tzung-Bi Shih Oct. 14, 2019, 12:34 p.m. UTC | #1
On Mon, Oct 14, 2019 at 6:20 PM Tzung-Bi Shih <tzungbi@google.com> wrote:
>
> This series makes mt6358, cros_ec_codec, and mt8183-mt6358-ts3a227-max98357
> support WoV (wake on voice).
>
> The first 3 commits are some cleanups and refactors.  It looks like
> breaking the existing interface.  But please be noticed that, the
> cros_ec_codec has not used by any real device yet.  The refactor is
> very necessary to keep the style consistent and for easier to further
> extend and maintain.
>   platform/chrome: cros_ec: remove unused EC feature
>   ASoC: cros_ec_codec: refactor I2S RX
>   ASoC: cros_ec_codec: extract DMIC EC command from I2S RX
>
> The 4th commit extends the feature offered from EC codec.
>   platform/chrome: cros_ec: add common commands for EC codec
>
> The 5th commit changes the behavior of setting and getting DMIC gains.
>   ASoC: cros_ec_codec: read max DMIC gain from EC codec
>
> The 6th and 7th commit make cros_ec_codec support WoV.
>   ASoC: dt-bindings: cros_ec_codec: add SHM bindings
>   ASoC: cros_ec_codec: support WoV
>
> The 8th commit sets necessary registers on mt6358 to support WoV.
>   ASoC: mediatek: mt6358: support WoV
>
> The last 2 commit make machine driver mt8183-mt6358-ts3a227-max98357
> support WoV if ec-codec is in DTS.
>   ASoC: dt-bindings: mt8183: add ec-codec
>   ASoC: mediatek: mt8183: support WoV
>
> Changes from v1:
> - fix a compile error and make kbuild bot happy
> https://mailman.alsa-project.org/pipermail/alsa-devel/2019-October/156315.html
> Changes from v2:
> - rebase upon to "don't use snd_pcm_ops" series
> https://mailman.alsa-project.org/pipermail/alsa-devel/2019-October/156170.html
> - fix sparse errors
> https://mailman.alsa-project.org/pipermail/alsa-devel/2019-October/156328.html
> - use "reg" for SHM binding
> https://mailman.alsa-project.org/pipermail/alsa-devel/2019-October/156657.html
>
> Tzung-Bi Shih (10):
>   WIP: platform/chrome: cros_ec: remove unused EC feature
>   WIP: ASoC: cros_ec_codec: refactor I2S RX
>   WIP: ASoC: cros_ec_codec: extract DMIC EC command from I2S RX
>   WIP: platform/chrome: cros_ec: add common commands for EC codec
>   WIP: ASoC: cros_ec_codec: read max DMIC gain from EC codec
>   WIP: ASoC: dt-bindings: cros_ec_codec: add SHM bindings
>   WIP: ASoC: cros_ec_codec: support WoV
>   WIP: ASoC: mediatek: mt6358: support WoV
>   WIP: ASoC: dt-bindings: mt8183: add ec-codec
>   WIP: ASoC: mediatek: mt8183: support WoV

Ha..I noticed here are some "WIP" prefixes forgot to remove.  To not
generate too much flood, will fix in later versions.

>
>  .../bindings/sound/google,cros-ec-codec.txt   |   24 +-
>  .../sound/mt8183-mt6358-ts3a227-max98357.txt  |    3 +
>  drivers/platform/chrome/cros_ec_trace.c       |    5 +-
>  .../linux/platform_data/cros_ec_commands.h    |  285 ++++-
>  sound/soc/codecs/cros_ec_codec.c              | 1128 +++++++++++++----
>  sound/soc/codecs/mt6358.c                     |  105 ++
>  sound/soc/mediatek/Kconfig                    |    1 +
>  .../mt8183/mt8183-mt6358-ts3a227-max98357.c   |   70 +-
>  8 files changed, 1296 insertions(+), 325 deletions(-)
>
> --
> 2.23.0.700.g56cf767bdb-goog
>
Mark Brown Oct. 15, 2019, 11:48 a.m. UTC | #2
On Mon, Oct 14, 2019 at 06:20:13PM +0800, Tzung-Bi Shih wrote:
> Remove unused EC_FEATURE_AUDIO_CODEC.

What's the route to getting these platform/chrome changes reviewed?
They don't seem to have got any attention thus far and this one is right
at the start of the series.
Tzung-Bi Shih Oct. 15, 2019, 3:37 p.m. UTC | #3
On Tue, Oct 15, 2019 at 7:49 PM Mark Brown <broonie@kernel.org> wrote:
>
> On Mon, Oct 14, 2019 at 06:20:13PM +0800, Tzung-Bi Shih wrote:
> > Remove unused EC_FEATURE_AUDIO_CODEC.
>
> What's the route to getting these platform/chrome changes reviewed?
> They don't seem to have got any attention thus far and this one is right
> at the start of the series.

Enric, could you help to review the "platform/chrome" changes in this
series?  All changes have merged in the EC firmware code
(https://chromium.googlesource.com/chromiumos/platform/ec/+/refs/heads/master/include/ec_commands.h).
Benson Leung Oct. 17, 2019, 2:25 a.m. UTC | #4
Hi Tzung-Bi,

On Mon, Oct 14, 2019 at 06:20:13PM +0800, Tzung-Bi Shih wrote:
> Remove unused EC_FEATURE_AUDIO_CODEC.
> 
> Signed-off-by: Tzung-Bi Shih <tzungbi@google.com>

Acked-By: Benson Leung <bleung@chromium.org>

Thanks,
Benson

> ---
>  include/linux/platform_data/cros_ec_commands.h | 2 --
>  1 file changed, 2 deletions(-)
> 
> diff --git a/include/linux/platform_data/cros_ec_commands.h b/include/linux/platform_data/cros_ec_commands.h
> index 98415686cbfa..43b8f7dae4cc 100644
> --- a/include/linux/platform_data/cros_ec_commands.h
> +++ b/include/linux/platform_data/cros_ec_commands.h
> @@ -1277,8 +1277,6 @@ enum ec_feature_code {
>  	 * MOTIONSENSE_CMD_TABLET_MODE_LID_ANGLE.
>  	 */
>  	EC_FEATURE_REFINED_TABLET_MODE_HYSTERESIS = 37,
> -	/* EC supports audio codec. */
> -	EC_FEATURE_AUDIO_CODEC = 38,
>  	/* The MCU is a System Companion Processor (SCP). */
>  	EC_FEATURE_SCP = 39,
>  	/* The MCU is an Integrated Sensor Hub */
> -- 
> 2.23.0.700.g56cf767bdb-goog
>
Benson Leung Oct. 17, 2019, 2:30 a.m. UTC | #5
Hi Tzung-Bi,

On Mon, Oct 14, 2019 at 06:20:14PM +0800, Tzung-Bi Shih wrote:
> Refactor by the following items:
> - reformat copyright declaration
> - use more specific name "i2s rx"
> - use verbose symbol names to separate namespaces
> - make some short functions inline
> - remove unused TDM-related code
> 
> Signed-off-by: Tzung-Bi Shih <tzungbi@google.com>

Acked-By: Benson Leung <bleung@chromium.org>

Thanks,
Benson

> ---
>  drivers/platform/chrome/cros_ec_trace.c       |   2 +-
>  .../linux/platform_data/cros_ec_commands.h    | 120 ++---
>  sound/soc/codecs/cros_ec_codec.c              | 502 +++++++-----------
>  3 files changed, 251 insertions(+), 373 deletions(-)
> 
> diff --git a/drivers/platform/chrome/cros_ec_trace.c b/drivers/platform/chrome/cros_ec_trace.c
> index 6f80ff4532ae..901850004b2b 100644
> --- a/drivers/platform/chrome/cros_ec_trace.c
> +++ b/drivers/platform/chrome/cros_ec_trace.c
> @@ -98,7 +98,7 @@
>  	TRACE_SYMBOL(EC_CMD_SB_READ_BLOCK), \
>  	TRACE_SYMBOL(EC_CMD_SB_WRITE_BLOCK), \
>  	TRACE_SYMBOL(EC_CMD_BATTERY_VENDOR_PARAM), \
> -	TRACE_SYMBOL(EC_CMD_CODEC_I2S), \
> +	TRACE_SYMBOL(EC_CMD_EC_CODEC_I2S_RX), \
>  	TRACE_SYMBOL(EC_CMD_REBOOT_EC), \
>  	TRACE_SYMBOL(EC_CMD_GET_PANIC_INFO), \
>  	TRACE_SYMBOL(EC_CMD_ACPI_READ), \
> diff --git a/include/linux/platform_data/cros_ec_commands.h b/include/linux/platform_data/cros_ec_commands.h
> index 43b8f7dae4cc..261ac83bd007 100644
> --- a/include/linux/platform_data/cros_ec_commands.h
> +++ b/include/linux/platform_data/cros_ec_commands.h
> @@ -4466,92 +4466,74 @@ enum mkbp_cec_event {
>  
>  /*****************************************************************************/
>  
> -/* Commands for I2S recording on audio codec. */
> -
> -#define EC_CMD_CODEC_I2S 0x00BC
> -#define EC_WOV_I2S_SAMPLE_RATE 48000
> -
> -enum ec_codec_i2s_subcmd {
> -	EC_CODEC_SET_SAMPLE_DEPTH = 0x0,
> -	EC_CODEC_SET_GAIN = 0x1,
> -	EC_CODEC_GET_GAIN = 0x2,
> -	EC_CODEC_I2S_ENABLE = 0x3,
> -	EC_CODEC_I2S_SET_CONFIG = 0x4,
> -	EC_CODEC_I2S_SET_TDM_CONFIG = 0x5,
> -	EC_CODEC_I2S_SET_BCLK = 0x6,
> -	EC_CODEC_I2S_SUBCMD_COUNT = 0x7,
> +/* Commands for I2S RX on audio codec. */
> +
> +#define EC_CMD_EC_CODEC_I2S_RX 0x00BC
> +
> +enum ec_codec_i2s_rx_subcmd {
> +	EC_CODEC_I2S_RX_ENABLE = 0x0,
> +	EC_CODEC_I2S_RX_DISABLE = 0x1,
> +	EC_CODEC_I2S_RX_SET_GAIN = 0x2,
> +	EC_CODEC_I2S_RX_GET_GAIN = 0x3,
> +	EC_CODEC_I2S_RX_SET_SAMPLE_DEPTH = 0x4,
> +	EC_CODEC_I2S_RX_SET_DAIFMT = 0x5,
> +	EC_CODEC_I2S_RX_SET_BCLK = 0x6,
> +	EC_CODEC_I2S_RX_SUBCMD_COUNT,
>  };
>  
> -enum ec_sample_depth_value {
> -	EC_CODEC_SAMPLE_DEPTH_16 = 0,
> -	EC_CODEC_SAMPLE_DEPTH_24 = 1,
> +enum ec_codec_i2s_rx_sample_depth {
> +	EC_CODEC_I2S_RX_SAMPLE_DEPTH_16 = 0x0,
> +	EC_CODEC_I2S_RX_SAMPLE_DEPTH_24 = 0x1,
> +	EC_CODEC_I2S_RX_SAMPLE_DEPTH_COUNT,
>  };
>  
> -enum ec_i2s_config {
> -	EC_DAI_FMT_I2S = 0,
> -	EC_DAI_FMT_RIGHT_J = 1,
> -	EC_DAI_FMT_LEFT_J = 2,
> -	EC_DAI_FMT_PCM_A = 3,
> -	EC_DAI_FMT_PCM_B = 4,
> -	EC_DAI_FMT_PCM_TDM = 5,
> +enum ec_codec_i2s_rx_daifmt {
> +	EC_CODEC_I2S_RX_DAIFMT_I2S = 0x0,
> +	EC_CODEC_I2S_RX_DAIFMT_RIGHT_J = 0x1,
> +	EC_CODEC_I2S_RX_DAIFMT_LEFT_J = 0x2,
> +	EC_CODEC_I2S_RX_DAIFMT_COUNT,
>  };
>  
> -/*
> - * For subcommand EC_CODEC_GET_GAIN.
> - */
> -struct __ec_align1 ec_codec_i2s_gain {
> +struct __ec_align1 ec_param_ec_codec_i2s_rx_set_sample_depth {
> +	uint8_t depth;
> +	uint8_t reserved[3];
> +};
> +
> +struct __ec_align1 ec_param_ec_codec_i2s_rx_set_gain {
>  	uint8_t left;
>  	uint8_t right;
> +	uint8_t reserved[2];
>  };
>  
> -struct __ec_todo_unpacked ec_param_codec_i2s_tdm {
> -	int16_t ch0_delay; /* 0 to 496 */
> -	int16_t ch1_delay; /* -1 to 496 */
> -	uint8_t adjacent_to_ch0;
> -	uint8_t adjacent_to_ch1;
> +struct __ec_align1 ec_param_ec_codec_i2s_rx_set_daifmt {
> +	uint8_t daifmt;
> +	uint8_t reserved[3];
>  };
>  
> -struct __ec_todo_packed ec_param_codec_i2s {
> -	/* enum ec_codec_i2s_subcmd */
> -	uint8_t cmd;
> -	union {
> -		/*
> -		 * EC_CODEC_SET_SAMPLE_DEPTH
> -		 * Value should be one of ec_sample_depth_value.
> -		 */
> -		uint8_t depth;
> -
> -		/*
> -		 * EC_CODEC_SET_GAIN
> -		 * Value should be 0~43 for both channels.
> -		 */
> -		struct ec_codec_i2s_gain gain;
> -
> -		/*
> -		 * EC_CODEC_I2S_ENABLE
> -		 * 1 to enable, 0 to disable.
> -		 */
> -		uint8_t i2s_enable;
> -
> -		/*
> -		 * EC_CODEC_I2S_SET_CONFIG
> -		 * Value should be one of ec_i2s_config.
> -		 */
> -		uint8_t i2s_config;
> +struct __ec_align4 ec_param_ec_codec_i2s_rx_set_bclk {
> +	uint32_t bclk;
> +};
>  
> -		/*
> -		 * EC_CODEC_I2S_SET_TDM_CONFIG
> -		 * Value should be one of ec_i2s_config.
> -		 */
> -		struct ec_param_codec_i2s_tdm tdm_param;
> +struct __ec_align4 ec_param_ec_codec_i2s_rx {
> +	uint8_t cmd; /* enum ec_codec_i2s_rx_subcmd */
> +	uint8_t reserved[3];
>  
> -		/*
> -		 * EC_CODEC_I2S_SET_BCLK
> -		 */
> -		uint32_t bclk;
> +	union {
> +		struct ec_param_ec_codec_i2s_rx_set_sample_depth
> +				set_sample_depth_param;
> +		struct ec_param_ec_codec_i2s_rx_set_gain
> +				set_gain_param;
> +		struct ec_param_ec_codec_i2s_rx_set_daifmt
> +				set_daifmt_param;
> +		struct ec_param_ec_codec_i2s_rx_set_bclk
> +				set_bclk_param;
>  	};
>  };
>  
> +struct __ec_align1 ec_response_ec_codec_i2s_rx_get_gain {
> +	uint8_t left;
> +	uint8_t right;
> +};
>  
>  /*****************************************************************************/
>  /* System commands */
> diff --git a/sound/soc/codecs/cros_ec_codec.c b/sound/soc/codecs/cros_ec_codec.c
> index 3c1bd24a1057..179fa77291cd 100644
> --- a/sound/soc/codecs/cros_ec_codec.c
> +++ b/sound/soc/codecs/cros_ec_codec.c
> @@ -1,6 +1,8 @@
>  // SPDX-License-Identifier: GPL-2.0
>  /*
> - * Driver for ChromeOS Embedded Controller codec.
> + * Copyright 2019 Google, Inc.
> + *
> + * ChromeOS Embedded Controller codec driver.
>   *
>   * This driver uses the cros-ec interface to communicate with the ChromeOS
>   * EC for audio function.
> @@ -18,403 +20,297 @@
>  #include <sound/soc.h>
>  #include <sound/tlv.h>
>  
> -#define DRV_NAME "cros-ec-codec"
> -
> -/**
> - * struct cros_ec_codec_data - ChromeOS EC codec driver data.
> - * @dev:		Device structure used in sysfs.
> - * @ec_device:		cros_ec_device structure to talk to the physical device.
> - * @component:		Pointer to the component.
> - * @max_dmic_gain:	Maximum gain in dB supported by EC codec.
> - */
> -struct cros_ec_codec_data {
> +struct cros_ec_codec_priv {
>  	struct device *dev;
>  	struct cros_ec_device *ec_device;
> -	struct snd_soc_component *component;
> -	unsigned int max_dmic_gain;
>  };
>  
> -static const DECLARE_TLV_DB_SCALE(ec_mic_gain_tlv, 0, 100, 0);
> -
> -static int ec_command_get_gain(struct snd_soc_component *component,
> -			       struct ec_param_codec_i2s *param,
> -			       struct ec_codec_i2s_gain *resp)
> +static int send_ec_host_command(struct cros_ec_device *ec_dev, uint32_t cmd,
> +				uint8_t *out, size_t outsize,
> +				uint8_t *in, size_t insize)
>  {
> -	struct cros_ec_codec_data *codec_data =
> -		snd_soc_component_get_drvdata(component);
> -	struct cros_ec_device *ec_device = codec_data->ec_device;
> -	u8 buffer[sizeof(struct cros_ec_command) +
> -		  max(sizeof(struct ec_param_codec_i2s),
> -		      sizeof(struct ec_codec_i2s_gain))];
> -	struct cros_ec_command *msg = (struct cros_ec_command *)&buffer;
>  	int ret;
> +	struct cros_ec_command *msg;
> +
> +	msg = kmalloc(sizeof(*msg) + max(outsize, insize), GFP_KERNEL);
> +	if (!msg)
> +		return -ENOMEM;
>  
>  	msg->version = 0;
> -	msg->command = EC_CMD_CODEC_I2S;
> -	msg->outsize = sizeof(struct ec_param_codec_i2s);
> -	msg->insize = sizeof(struct ec_codec_i2s_gain);
> +	msg->command = cmd;
> +	msg->outsize = outsize;
> +	msg->insize = insize;
> +
> +	if (outsize)
> +		memcpy(msg->data, out, outsize);
>  
> -	memcpy(msg->data, param, msg->outsize);
> +	ret = cros_ec_cmd_xfer_status(ec_dev, msg);
> +	if (ret < 0)
> +		goto error;
>  
> -	ret = cros_ec_cmd_xfer_status(ec_device, msg);
> -	if (ret > 0)
> -		memcpy(resp, msg->data, msg->insize);
> +	if (insize)
> +		memcpy(in, msg->data, insize);
>  
> +	ret = 0;
> +error:
> +	kfree(msg);
>  	return ret;
>  }
>  
> -/*
> - * Wrapper for EC command without response.
> - */
> -static int ec_command_no_resp(struct snd_soc_component *component,
> -			      struct ec_param_codec_i2s *param)
> +static int dmic_get_gain(struct snd_kcontrol *kcontrol,
> +			 struct snd_ctl_elem_value *ucontrol)
>  {
> -	struct cros_ec_codec_data *codec_data =
> +	struct snd_soc_component *component =
> +		snd_soc_kcontrol_component(kcontrol);
> +	struct cros_ec_codec_priv *priv =
>  		snd_soc_component_get_drvdata(component);
> -	struct cros_ec_device *ec_device = codec_data->ec_device;
> -	u8 buffer[sizeof(struct cros_ec_command) +
> -		  sizeof(struct ec_param_codec_i2s)];
> -	struct cros_ec_command *msg = (struct cros_ec_command *)&buffer;
> -
> -	msg->version = 0;
> -	msg->command = EC_CMD_CODEC_I2S;
> -	msg->outsize = sizeof(struct ec_param_codec_i2s);
> -	msg->insize = 0;
> -
> -	memcpy(msg->data, param, msg->outsize);
> -
> -	return cros_ec_cmd_xfer_status(ec_device, msg);
> -}
> -
> -static int set_i2s_config(struct snd_soc_component *component,
> -			  enum ec_i2s_config i2s_config)
> -{
> -	struct ec_param_codec_i2s param;
> +	struct ec_param_ec_codec_i2s_rx p;
> +	struct ec_response_ec_codec_i2s_rx_get_gain r;
> +	int ret;
>  
> -	dev_dbg(component->dev, "%s set I2S format to %u\n", __func__,
> -		i2s_config);
> +	p.cmd = EC_CODEC_I2S_RX_GET_GAIN;
> +	ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_I2S_RX,
> +				   (uint8_t *)&p, sizeof(p),
> +				   (uint8_t *)&r, sizeof(r));
> +	if (ret < 0)
> +		return ret;
>  
> -	param.cmd = EC_CODEC_I2S_SET_CONFIG;
> -	param.i2s_config = i2s_config;
> +	ucontrol->value.integer.value[0] = r.left;
> +	ucontrol->value.integer.value[1] = r.right;
>  
> -	return ec_command_no_resp(component, &param);
> +	return 0;
>  }
>  
> -static int cros_ec_i2s_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
> +static int dmic_put_gain(struct snd_kcontrol *kcontrol,
> +			 struct snd_ctl_elem_value *ucontrol)
>  {
> -	struct snd_soc_component *component = dai->component;
> -	enum ec_i2s_config i2s_config;
> -
> -	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
> -	case SND_SOC_DAIFMT_CBS_CFS:
> -		break;
> -	default:
> -		return -EINVAL;
> -	}
> -
> -	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
> -	case SND_SOC_DAIFMT_NB_NF:
> -		break;
> -	default:
> -		return -EINVAL;
> -	}
> -
> -	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
> -	case SND_SOC_DAIFMT_I2S:
> -		i2s_config = EC_DAI_FMT_I2S;
> -		break;
> -
> -	case SND_SOC_DAIFMT_RIGHT_J:
> -		i2s_config = EC_DAI_FMT_RIGHT_J;
> -		break;
> -
> -	case SND_SOC_DAIFMT_LEFT_J:
> -		i2s_config = EC_DAI_FMT_LEFT_J;
> -		break;
> -
> -	case SND_SOC_DAIFMT_DSP_A:
> -		i2s_config = EC_DAI_FMT_PCM_A;
> -		break;
> -
> -	case SND_SOC_DAIFMT_DSP_B:
> -		i2s_config = EC_DAI_FMT_PCM_B;
> -		break;
> +	struct snd_soc_component *component =
> +		snd_soc_kcontrol_component(kcontrol);
> +	struct cros_ec_codec_priv *priv =
> +		snd_soc_component_get_drvdata(component);
> +	struct soc_mixer_control *control =
> +		(struct soc_mixer_control *)kcontrol->private_value;
> +	int max_dmic_gain = control->max;
> +	int left = ucontrol->value.integer.value[0];
> +	int right = ucontrol->value.integer.value[1];
> +	struct ec_param_ec_codec_i2s_rx p;
>  
> -	default:
> +	if (left > max_dmic_gain || right > max_dmic_gain)
>  		return -EINVAL;
> -	}
>  
> -	return set_i2s_config(component, i2s_config);
> -}
> -
> -static int set_i2s_sample_depth(struct snd_soc_component *component,
> -				enum ec_sample_depth_value depth)
> -{
> -	struct ec_param_codec_i2s param;
> -
> -	dev_dbg(component->dev, "%s set depth to %u\n", __func__, depth);
> -
> -	param.cmd = EC_CODEC_SET_SAMPLE_DEPTH;
> -	param.depth = depth;
> +	dev_dbg(component->dev, "set mic gain to %u, %u\n", left, right);
>  
> -	return ec_command_no_resp(component, &param);
> +	p.cmd = EC_CODEC_I2S_RX_SET_GAIN;
> +	p.set_gain_param.left = left;
> +	p.set_gain_param.right = right;
> +	return send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_I2S_RX,
> +				    (uint8_t *)&p, sizeof(p), NULL, 0);
>  }
>  
> -static int set_i2s_bclk(struct snd_soc_component *component, uint32_t bclk)
> -{
> -	struct ec_param_codec_i2s param;
> -
> -	dev_dbg(component->dev, "%s set i2s bclk to %u\n", __func__, bclk);
> +static const DECLARE_TLV_DB_SCALE(dmic_gain_tlv, 0, 100, 0);
>  
> -	param.cmd = EC_CODEC_I2S_SET_BCLK;
> -	param.bclk = bclk;
> +enum {
> +	DMIC_CTL_GAIN = 0,
> +};
>  
> -	return ec_command_no_resp(component, &param);
> -}
> +static struct snd_kcontrol_new dmic_controls[] = {
> +	[DMIC_CTL_GAIN] =
> +		SOC_DOUBLE_EXT_TLV("EC Mic Gain", SND_SOC_NOPM, SND_SOC_NOPM,
> +				   0, 0, 0, dmic_get_gain, dmic_put_gain,
> +				   dmic_gain_tlv),
> +};
>  
> -static int cros_ec_i2s_hw_params(struct snd_pcm_substream *substream,
> -				 struct snd_pcm_hw_params *params,
> -				 struct snd_soc_dai *dai)
> +static int i2s_rx_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;
> -	unsigned int rate, bclk;
> +	struct cros_ec_codec_priv *priv =
> +		snd_soc_component_get_drvdata(component);
> +	struct ec_param_ec_codec_i2s_rx p;
> +	enum ec_codec_i2s_rx_sample_depth depth;
>  	int ret;
>  
> -	rate = params_rate(params);
> -	if (rate != 48000)
> +	if (params_rate(params) != 48000)
>  		return -EINVAL;
>  
>  	switch (params_format(params)) {
>  	case SNDRV_PCM_FORMAT_S16_LE:
> -		ret = set_i2s_sample_depth(component, EC_CODEC_SAMPLE_DEPTH_16);
> +		depth = EC_CODEC_I2S_RX_SAMPLE_DEPTH_16;
>  		break;
>  	case SNDRV_PCM_FORMAT_S24_LE:
> -		ret = set_i2s_sample_depth(component, EC_CODEC_SAMPLE_DEPTH_24);
> +		depth = EC_CODEC_I2S_RX_SAMPLE_DEPTH_24;
>  		break;
>  	default:
>  		return -EINVAL;
>  	}
> -	if (ret < 0)
> -		return ret;
> -
> -	bclk = snd_soc_params_to_bclk(params);
> -	return set_i2s_bclk(component, bclk);
> -}
>  
> -static const struct snd_soc_dai_ops cros_ec_i2s_dai_ops = {
> -	.hw_params = cros_ec_i2s_hw_params,
> -	.set_fmt = cros_ec_i2s_set_dai_fmt,
> -};
> +	dev_dbg(component->dev, "set depth to %u\n", depth);
>  
> -static struct snd_soc_dai_driver cros_ec_dai[] = {
> -	{
> -		.name = "cros_ec_codec I2S",
> -		.id = 0,
> -		.capture = {
> -			.stream_name = "I2S Capture",
> -			.channels_min = 2,
> -			.channels_max = 2,
> -			.rates = SNDRV_PCM_RATE_48000,
> -			.formats = SNDRV_PCM_FMTBIT_S16_LE |
> -				   SNDRV_PCM_FMTBIT_S24_LE,
> -		},
> -		.ops = &cros_ec_i2s_dai_ops,
> -	}
> -};
> -
> -static int get_ec_mic_gain(struct snd_soc_component *component,
> -			   u8 *left, u8 *right)
> -{
> -	struct ec_param_codec_i2s param;
> -	struct ec_codec_i2s_gain resp;
> -	int ret;
> -
> -	param.cmd = EC_CODEC_GET_GAIN;
> -
> -	ret = ec_command_get_gain(component, &param, &resp);
> +	p.cmd = EC_CODEC_I2S_RX_SET_SAMPLE_DEPTH;
> +	p.set_sample_depth_param.depth = depth;
> +	ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_I2S_RX,
> +				   (uint8_t *)&p, sizeof(p), NULL, 0);
>  	if (ret < 0)
>  		return ret;
>  
> -	*left = resp.left;
> -	*right = resp.right;
> -
> -	return 0;
> -}
> -
> -static int mic_gain_get(struct snd_kcontrol *kcontrol,
> -			struct snd_ctl_elem_value *ucontrol)
> -{
> -	struct snd_soc_component *component =
> -		snd_soc_kcontrol_component(kcontrol);
> -	u8 left, right;
> -	int ret;
> -
> -	ret = get_ec_mic_gain(component, &left, &right);
> -	if (ret)
> -		return ret;
> -
> -	ucontrol->value.integer.value[0] = left;
> -	ucontrol->value.integer.value[1] = right;
> -
> -	return 0;
> -}
> -
> -static int set_ec_mic_gain(struct snd_soc_component *component,
> -			   u8 left, u8 right)
> -{
> -	struct ec_param_codec_i2s param;
> -
> -	dev_dbg(component->dev, "%s set mic gain to %u, %u\n",
> -		__func__, left, right);
> +	dev_dbg(component->dev, "set bclk to %u\n",
> +		snd_soc_params_to_bclk(params));
>  
> -	param.cmd = EC_CODEC_SET_GAIN;
> -	param.gain.left = left;
> -	param.gain.right = right;
> -
> -	return ec_command_no_resp(component, &param);
> +	p.cmd = EC_CODEC_I2S_RX_SET_BCLK;
> +	p.set_bclk_param.bclk = snd_soc_params_to_bclk(params);
> +	return send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_I2S_RX,
> +				    (uint8_t *)&p, sizeof(p), NULL, 0);
>  }
>  
> -static int mic_gain_put(struct snd_kcontrol *kcontrol,
> -			struct snd_ctl_elem_value *ucontrol)
> +static int i2s_rx_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
>  {
> -	struct snd_soc_component *component =
> -		snd_soc_kcontrol_component(kcontrol);
> -	struct cros_ec_codec_data *codec_data =
> +	struct snd_soc_component *component = dai->component;
> +	struct cros_ec_codec_priv *priv =
>  		snd_soc_component_get_drvdata(component);
> -	int left = ucontrol->value.integer.value[0];
> -	int right = ucontrol->value.integer.value[1];
> -	unsigned int max_dmic_gain = codec_data->max_dmic_gain;
> +	struct ec_param_ec_codec_i2s_rx p;
> +	enum ec_codec_i2s_rx_daifmt daifmt;
>  
> -	if (left > max_dmic_gain || right > max_dmic_gain)
> +	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
> +	case SND_SOC_DAIFMT_CBS_CFS:
> +		break;
> +	default:
>  		return -EINVAL;
> +	}
>  
> -	return set_ec_mic_gain(component, (u8)left, (u8)right);
> -}
> -
> -static struct snd_kcontrol_new mic_gain_control =
> -	SOC_DOUBLE_EXT_TLV("EC Mic Gain", SND_SOC_NOPM, SND_SOC_NOPM, 0, 0, 0,
> -			   mic_gain_get, mic_gain_put, ec_mic_gain_tlv);
> -
> -static int enable_i2s(struct snd_soc_component *component, int enable)
> -{
> -	struct ec_param_codec_i2s param;
> +	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
> +	case SND_SOC_DAIFMT_NB_NF:
> +		break;
> +	default:
> +		return -EINVAL;
> +	}
>  
> -	dev_dbg(component->dev, "%s set i2s to %u\n", __func__, enable);
> +	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
> +	case SND_SOC_DAIFMT_I2S:
> +		daifmt = EC_CODEC_I2S_RX_DAIFMT_I2S;
> +		break;
> +	case SND_SOC_DAIFMT_RIGHT_J:
> +		daifmt = EC_CODEC_I2S_RX_DAIFMT_RIGHT_J;
> +		break;
> +	case SND_SOC_DAIFMT_LEFT_J:
> +		daifmt = EC_CODEC_I2S_RX_DAIFMT_LEFT_J;
> +		break;
> +	default:
> +		return -EINVAL;
> +	}
>  
> -	param.cmd = EC_CODEC_I2S_ENABLE;
> -	param.i2s_enable = enable;
> +	dev_dbg(component->dev, "set format to %u\n", daifmt);
>  
> -	return ec_command_no_resp(component, &param);
> +	p.cmd = EC_CODEC_I2S_RX_SET_DAIFMT;
> +	p.set_daifmt_param.daifmt = daifmt;
> +	return send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_I2S_RX,
> +				    (uint8_t *)&p, sizeof(p), NULL, 0);
>  }
>  
> -static int cros_ec_i2s_enable_event(struct snd_soc_dapm_widget *w,
> -				    struct snd_kcontrol *kcontrol, int event)
> +static const struct snd_soc_dai_ops i2s_rx_dai_ops = {
> +	.hw_params = i2s_rx_hw_params,
> +	.set_fmt = i2s_rx_set_fmt,
> +};
> +
> +static int i2s_rx_event(struct snd_soc_dapm_widget *w,
> +			struct snd_kcontrol *kcontrol, int event)
>  {
>  	struct snd_soc_component *component =
>  		snd_soc_dapm_to_component(w->dapm);
> +	struct cros_ec_codec_priv *priv =
> +		snd_soc_component_get_drvdata(component);
> +	struct ec_param_ec_codec_i2s_rx p;
>  
>  	switch (event) {
>  	case SND_SOC_DAPM_PRE_PMU:
> -		dev_dbg(component->dev,
> -			"%s got SND_SOC_DAPM_PRE_PMU event\n", __func__);
> -		return enable_i2s(component, 1);
> -
> +		dev_dbg(component->dev, "enable I2S RX\n");
> +		p.cmd = EC_CODEC_I2S_RX_ENABLE;
> +		break;
>  	case SND_SOC_DAPM_PRE_PMD:
> -		dev_dbg(component->dev,
> -			"%s got SND_SOC_DAPM_PRE_PMD event\n", __func__);
> -		return enable_i2s(component, 0);
> +		dev_dbg(component->dev, "disable I2S RX\n");
> +		p.cmd = EC_CODEC_I2S_RX_DISABLE;
> +		break;
> +	default:
> +		return 0;
>  	}
>  
> -	return 0;
> +	return send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_I2S_RX,
> +				    (uint8_t *)&p, sizeof(p), NULL, 0);
>  }
>  
> -/*
> - * The goal of this DAPM route is to turn on/off I2S using EC
> - * host command when capture stream is started/stopped.
> - */
> -static const struct snd_soc_dapm_widget cros_ec_codec_dapm_widgets[] = {
> +static struct snd_soc_dapm_widget i2s_rx_dapm_widgets[] = {
>  	SND_SOC_DAPM_INPUT("DMIC"),
> -
> -	/*
> -	 * Control EC to enable/disable I2S.
> -	 */
> -	SND_SOC_DAPM_SUPPLY("I2S Enable", SND_SOC_NOPM,
> -			    0, 0, cros_ec_i2s_enable_event,
> +	SND_SOC_DAPM_SUPPLY("I2S RX Enable", SND_SOC_NOPM, 0, 0, i2s_rx_event,
>  			    SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_PRE_PMD),
> +	SND_SOC_DAPM_AIF_OUT("I2S RX", "I2S Capture", 0, SND_SOC_NOPM, 0, 0),
> +};
>  
> -	SND_SOC_DAPM_AIF_OUT("I2STX", "I2S Capture", 0, SND_SOC_NOPM, 0, 0),
> +static struct snd_soc_dapm_route i2s_rx_dapm_routes[] = {
> +	{"I2S RX", NULL, "DMIC"},
> +	{"I2S RX", NULL, "I2S RX Enable"},
>  };
>  
> -static const struct snd_soc_dapm_route cros_ec_codec_dapm_routes[] = {
> -	{ "I2STX", NULL, "DMIC" },
> -	{ "I2STX", NULL, "I2S Enable" },
> +static struct snd_soc_dai_driver i2s_rx_dai_driver = {
> +	.name = "EC Codec I2S RX",
> +	.capture = {
> +		.stream_name = "I2S Capture",
> +		.channels_min = 2,
> +		.channels_max = 2,
> +		.rates = SNDRV_PCM_RATE_48000,
> +		.formats = SNDRV_PCM_FMTBIT_S16_LE |
> +			SNDRV_PCM_FMTBIT_S24_LE,
> +	},
> +	.ops = &i2s_rx_dai_ops,
>  };
>  
> -/*
> - * Read maximum gain from device property and set it to mixer control.
> - */
> -static int cros_ec_set_gain_range(struct device *dev)
> +static int i2s_rx_probe(struct snd_soc_component *component)
>  {
> +	struct cros_ec_codec_priv *priv =
> +		snd_soc_component_get_drvdata(component);
> +	struct device *dev = priv->dev;
> +	int ret, val;
>  	struct soc_mixer_control *control;
> -	struct cros_ec_codec_data *codec_data = dev_get_drvdata(dev);
> -	int rc;
>  
> -	rc = device_property_read_u32(dev, "max-dmic-gain",
> -				      &codec_data->max_dmic_gain);
> -	if (rc)
> -		return rc;
> +	ret = device_property_read_u32(dev, "max-dmic-gain", &val);
> +	if (ret) {
> +		dev_err(dev, "Failed to read 'max-dmic-gain'\n");
> +		return ret;
> +	}
>  
>  	control = (struct soc_mixer_control *)
> -				mic_gain_control.private_value;
> -	control->max = codec_data->max_dmic_gain;
> -	control->platform_max = codec_data->max_dmic_gain;
> +			dmic_controls[DMIC_CTL_GAIN].private_value;
> +	control->max = val;
> +	control->platform_max = val;
>  
> -	return 0;
> -}
> -
> -static int cros_ec_codec_probe(struct snd_soc_component *component)
> -{
> -	int rc;
> -
> -	struct cros_ec_codec_data *codec_data =
> -		snd_soc_component_get_drvdata(component);
> -
> -	rc = cros_ec_set_gain_range(codec_data->dev);
> -	if (rc)
> -		return rc;
> -
> -	return snd_soc_add_component_controls(component, &mic_gain_control, 1);
> +	return snd_soc_add_component_controls(component,
> +			&dmic_controls[DMIC_CTL_GAIN], 1);
>  }
>  
> -static const struct snd_soc_component_driver cros_ec_component_driver = {
> -	.probe			= cros_ec_codec_probe,
> -	.dapm_widgets		= cros_ec_codec_dapm_widgets,
> -	.num_dapm_widgets	= ARRAY_SIZE(cros_ec_codec_dapm_widgets),
> -	.dapm_routes		= cros_ec_codec_dapm_routes,
> -	.num_dapm_routes	= ARRAY_SIZE(cros_ec_codec_dapm_routes),
> +static const struct snd_soc_component_driver i2s_rx_component_driver = {
> +	.probe			= i2s_rx_probe,
> +	.dapm_widgets		= i2s_rx_dapm_widgets,
> +	.num_dapm_widgets	= ARRAY_SIZE(i2s_rx_dapm_widgets),
> +	.dapm_routes		= i2s_rx_dapm_routes,
> +	.num_dapm_routes	= ARRAY_SIZE(i2s_rx_dapm_routes),
>  };
>  
> -/*
> - * Platform device and platform driver fro cros-ec-codec.
> - */
> -static int cros_ec_codec_platform_probe(struct platform_device *pd)
> +static int cros_ec_codec_platform_probe(struct platform_device *pdev)
>  {
> -	struct device *dev = &pd->dev;
> -	struct cros_ec_device *ec_device = dev_get_drvdata(pd->dev.parent);
> -	struct cros_ec_codec_data *codec_data;
> +	struct device *dev = &pdev->dev;
> +	struct cros_ec_device *ec_device = dev_get_drvdata(pdev->dev.parent);
> +	struct cros_ec_codec_priv *priv;
>  
> -	codec_data = devm_kzalloc(dev, sizeof(struct cros_ec_codec_data),
> -				  GFP_KERNEL);
> -	if (!codec_data)
> +	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
> +	if (!priv)
>  		return -ENOMEM;
>  
> -	codec_data->dev = dev;
> -	codec_data->ec_device = ec_device;
> +	priv->dev = dev;
> +	priv->ec_device = ec_device;
>  
> -	platform_set_drvdata(pd, codec_data);
> +	platform_set_drvdata(pdev, priv);
>  
> -	return devm_snd_soc_register_component(dev, &cros_ec_component_driver,
> -					  cros_ec_dai, ARRAY_SIZE(cros_ec_dai));
> +	return devm_snd_soc_register_component(dev, &i2s_rx_component_driver,
> +					       &i2s_rx_dai_driver, 1);
>  }
>  
>  #ifdef CONFIG_OF
> @@ -427,7 +323,7 @@ MODULE_DEVICE_TABLE(of, cros_ec_codec_of_match);
>  
>  static struct platform_driver cros_ec_codec_platform_driver = {
>  	.driver = {
> -		.name = DRV_NAME,
> +		.name = "cros-ec-codec",
>  		.of_match_table = of_match_ptr(cros_ec_codec_of_match),
>  	},
>  	.probe = cros_ec_codec_platform_probe,
> @@ -438,4 +334,4 @@ module_platform_driver(cros_ec_codec_platform_driver);
>  MODULE_LICENSE("GPL v2");
>  MODULE_DESCRIPTION("ChromeOS EC codec driver");
>  MODULE_AUTHOR("Cheng-Yi Chiang <cychiang@chromium.org>");
> -MODULE_ALIAS("platform:" DRV_NAME);
> +MODULE_ALIAS("platform:cros-ec-codec");
> -- 
> 2.23.0.700.g56cf767bdb-goog
>
Benson Leung Oct. 17, 2019, 2:48 a.m. UTC | #6
Hi Tzung-Bi,

On Mon, Oct 14, 2019 at 06:20:15PM +0800, Tzung-Bi Shih wrote:
> Extract DMIC EC command from I2S RX.  Setting and getting
> microphone gains is common features.
> 
> Signed-off-by: Tzung-Bi Shih <tzungbi@google.com>

Acked-By: Benson Leung <bleung@chromium.org>

Thanks,
Benson

> ---
>  drivers/platform/chrome/cros_ec_trace.c       |  1 +
>  .../linux/platform_data/cros_ec_commands.h    | 49 +++++++++++-----
>  sound/soc/codecs/cros_ec_codec.c              | 57 ++++++++++---------
>  3 files changed, 68 insertions(+), 39 deletions(-)
> 
> diff --git a/drivers/platform/chrome/cros_ec_trace.c b/drivers/platform/chrome/cros_ec_trace.c
> index 901850004b2b..e73bb6a8b00e 100644
> --- a/drivers/platform/chrome/cros_ec_trace.c
> +++ b/drivers/platform/chrome/cros_ec_trace.c
> @@ -98,6 +98,7 @@
>  	TRACE_SYMBOL(EC_CMD_SB_READ_BLOCK), \
>  	TRACE_SYMBOL(EC_CMD_SB_WRITE_BLOCK), \
>  	TRACE_SYMBOL(EC_CMD_BATTERY_VENDOR_PARAM), \
> +	TRACE_SYMBOL(EC_CMD_EC_CODEC_DMIC), \
>  	TRACE_SYMBOL(EC_CMD_EC_CODEC_I2S_RX), \
>  	TRACE_SYMBOL(EC_CMD_REBOOT_EC), \
>  	TRACE_SYMBOL(EC_CMD_GET_PANIC_INFO), \
> diff --git a/include/linux/platform_data/cros_ec_commands.h b/include/linux/platform_data/cros_ec_commands.h
> index 261ac83bd007..58e460c015ef 100644
> --- a/include/linux/platform_data/cros_ec_commands.h
> +++ b/include/linux/platform_data/cros_ec_commands.h
> @@ -4466,18 +4466,48 @@ enum mkbp_cec_event {
>  
>  /*****************************************************************************/
>  
> +/* Commands for DMIC on audio codec. */
> +#define EC_CMD_EC_CODEC_DMIC 0x00BC
> +
> +enum ec_codec_dmic_subcmd {
> +	EC_CODEC_DMIC_SET_GAIN = 0x0,
> +	EC_CODEC_DMIC_GET_GAIN = 0x1,
> +	EC_CODEC_DMIC_SUBCMD_COUNT,
> +};
> +
> +struct __ec_align1 ec_param_ec_codec_dmic_set_gain {
> +	uint8_t left;
> +	uint8_t right;
> +	uint8_t reserved[2];
> +};
> +
> +struct __ec_align4 ec_param_ec_codec_dmic {
> +	uint8_t cmd; /* enum ec_codec_dmic_subcmd */
> +	uint8_t reserved[3];
> +
> +	union {
> +		struct ec_param_ec_codec_dmic_set_gain
> +				set_gain_param;
> +	};
> +};
> +
> +struct __ec_align1 ec_response_ec_codec_dmic_get_gain {
> +	uint8_t left;
> +	uint8_t right;
> +};
> +
> +/*****************************************************************************/
> +
>  /* Commands for I2S RX on audio codec. */
>  
> -#define EC_CMD_EC_CODEC_I2S_RX 0x00BC
> +#define EC_CMD_EC_CODEC_I2S_RX 0x00BD
>  
>  enum ec_codec_i2s_rx_subcmd {
>  	EC_CODEC_I2S_RX_ENABLE = 0x0,
>  	EC_CODEC_I2S_RX_DISABLE = 0x1,
> -	EC_CODEC_I2S_RX_SET_GAIN = 0x2,
> -	EC_CODEC_I2S_RX_GET_GAIN = 0x3,
> -	EC_CODEC_I2S_RX_SET_SAMPLE_DEPTH = 0x4,
> -	EC_CODEC_I2S_RX_SET_DAIFMT = 0x5,
> -	EC_CODEC_I2S_RX_SET_BCLK = 0x6,
> +	EC_CODEC_I2S_RX_SET_SAMPLE_DEPTH = 0x2,
> +	EC_CODEC_I2S_RX_SET_DAIFMT = 0x3,
> +	EC_CODEC_I2S_RX_SET_BCLK = 0x4,
>  	EC_CODEC_I2S_RX_SUBCMD_COUNT,
>  };
>  
> @@ -4521,8 +4551,6 @@ struct __ec_align4 ec_param_ec_codec_i2s_rx {
>  	union {
>  		struct ec_param_ec_codec_i2s_rx_set_sample_depth
>  				set_sample_depth_param;
> -		struct ec_param_ec_codec_i2s_rx_set_gain
> -				set_gain_param;
>  		struct ec_param_ec_codec_i2s_rx_set_daifmt
>  				set_daifmt_param;
>  		struct ec_param_ec_codec_i2s_rx_set_bclk
> @@ -4530,11 +4558,6 @@ struct __ec_align4 ec_param_ec_codec_i2s_rx {
>  	};
>  };
>  
> -struct __ec_align1 ec_response_ec_codec_i2s_rx_get_gain {
> -	uint8_t left;
> -	uint8_t right;
> -};
> -
>  /*****************************************************************************/
>  /* System commands */
>  
> diff --git a/sound/soc/codecs/cros_ec_codec.c b/sound/soc/codecs/cros_ec_codec.c
> index 179fa77291cd..c19c7fe42e2e 100644
> --- a/sound/soc/codecs/cros_ec_codec.c
> +++ b/sound/soc/codecs/cros_ec_codec.c
> @@ -64,12 +64,12 @@ static int dmic_get_gain(struct snd_kcontrol *kcontrol,
>  		snd_soc_kcontrol_component(kcontrol);
>  	struct cros_ec_codec_priv *priv =
>  		snd_soc_component_get_drvdata(component);
> -	struct ec_param_ec_codec_i2s_rx p;
> -	struct ec_response_ec_codec_i2s_rx_get_gain r;
> +	struct ec_param_ec_codec_dmic p;
> +	struct ec_response_ec_codec_dmic_get_gain r;
>  	int ret;
>  
> -	p.cmd = EC_CODEC_I2S_RX_GET_GAIN;
> -	ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_I2S_RX,
> +	p.cmd = EC_CODEC_DMIC_GET_GAIN;
> +	ret = send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_DMIC,
>  				   (uint8_t *)&p, sizeof(p),
>  				   (uint8_t *)&r, sizeof(r));
>  	if (ret < 0)
> @@ -93,17 +93,17 @@ static int dmic_put_gain(struct snd_kcontrol *kcontrol,
>  	int max_dmic_gain = control->max;
>  	int left = ucontrol->value.integer.value[0];
>  	int right = ucontrol->value.integer.value[1];
> -	struct ec_param_ec_codec_i2s_rx p;
> +	struct ec_param_ec_codec_dmic p;
>  
>  	if (left > max_dmic_gain || right > max_dmic_gain)
>  		return -EINVAL;
>  
>  	dev_dbg(component->dev, "set mic gain to %u, %u\n", left, right);
>  
> -	p.cmd = EC_CODEC_I2S_RX_SET_GAIN;
> +	p.cmd = EC_CODEC_DMIC_SET_GAIN;
>  	p.set_gain_param.left = left;
>  	p.set_gain_param.right = right;
> -	return send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_I2S_RX,
> +	return send_ec_host_command(priv->ec_device, EC_CMD_EC_CODEC_DMIC,
>  				    (uint8_t *)&p, sizeof(p), NULL, 0);
>  }
>  
> @@ -120,6 +120,29 @@ static struct snd_kcontrol_new dmic_controls[] = {
>  				   dmic_gain_tlv),
>  };
>  
> +static int dmic_probe(struct snd_soc_component *component)
> +{
> +	struct cros_ec_codec_priv *priv =
> +		snd_soc_component_get_drvdata(component);
> +	struct device *dev = priv->dev;
> +	int ret, val;
> +	struct soc_mixer_control *control;
> +
> +	ret = device_property_read_u32(dev, "max-dmic-gain", &val);
> +	if (ret) {
> +		dev_err(dev, "Failed to read 'max-dmic-gain'\n");
> +		return ret;
> +	}
> +
> +	control = (struct soc_mixer_control *)
> +		dmic_controls[DMIC_CTL_GAIN].private_value;
> +	control->max = val;
> +	control->platform_max = val;
> +
> +	return snd_soc_add_component_controls(component,
> +			&dmic_controls[DMIC_CTL_GAIN], 1);
> +}
> +
>  static int i2s_rx_hw_params(struct snd_pcm_substream *substream,
>  			    struct snd_pcm_hw_params *params,
>  			    struct snd_soc_dai *dai)
> @@ -265,25 +288,7 @@ static struct snd_soc_dai_driver i2s_rx_dai_driver = {
>  
>  static int i2s_rx_probe(struct snd_soc_component *component)
>  {
> -	struct cros_ec_codec_priv *priv =
> -		snd_soc_component_get_drvdata(component);
> -	struct device *dev = priv->dev;
> -	int ret, val;
> -	struct soc_mixer_control *control;
> -
> -	ret = device_property_read_u32(dev, "max-dmic-gain", &val);
> -	if (ret) {
> -		dev_err(dev, "Failed to read 'max-dmic-gain'\n");
> -		return ret;
> -	}
> -
> -	control = (struct soc_mixer_control *)
> -			dmic_controls[DMIC_CTL_GAIN].private_value;
> -	control->max = val;
> -	control->platform_max = val;
> -
> -	return snd_soc_add_component_controls(component,
> -			&dmic_controls[DMIC_CTL_GAIN], 1);
> +	return dmic_probe(component);
>  }
>  
>  static const struct snd_soc_component_driver i2s_rx_component_driver = {
> -- 
> 2.23.0.700.g56cf767bdb-goog
>
Benson Leung Oct. 17, 2019, 2:57 a.m. UTC | #7
Hi Tzung-Bi,

On Mon, Oct 14, 2019 at 06:20:16PM +0800, Tzung-Bi Shih wrote:
> Add the following common commands:
> - GET_CAPABILITIES
> - GET_SHM_ADDR
> - SET_SHM_ADDR
> 
> Signed-off-by: Tzung-Bi Shih <tzungbi@google.com>

Acked-By: Benson Leung <bleung@chromium.org>

Thanks,
Benson

> ---
>  drivers/platform/chrome/cros_ec_trace.c       |  1 +
>  .../linux/platform_data/cros_ec_commands.h    | 64 ++++++++++++++++++-
>  2 files changed, 63 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/platform/chrome/cros_ec_trace.c b/drivers/platform/chrome/cros_ec_trace.c
> index e73bb6a8b00e..2ea0d4e0d54d 100644
> --- a/drivers/platform/chrome/cros_ec_trace.c
> +++ b/drivers/platform/chrome/cros_ec_trace.c
> @@ -98,6 +98,7 @@
>  	TRACE_SYMBOL(EC_CMD_SB_READ_BLOCK), \
>  	TRACE_SYMBOL(EC_CMD_SB_WRITE_BLOCK), \
>  	TRACE_SYMBOL(EC_CMD_BATTERY_VENDOR_PARAM), \
> +	TRACE_SYMBOL(EC_CMD_EC_CODEC), \
>  	TRACE_SYMBOL(EC_CMD_EC_CODEC_DMIC), \
>  	TRACE_SYMBOL(EC_CMD_EC_CODEC_I2S_RX), \
>  	TRACE_SYMBOL(EC_CMD_REBOOT_EC), \
> diff --git a/include/linux/platform_data/cros_ec_commands.h b/include/linux/platform_data/cros_ec_commands.h
> index 58e460c015ef..3ca0fa9e92a7 100644
> --- a/include/linux/platform_data/cros_ec_commands.h
> +++ b/include/linux/platform_data/cros_ec_commands.h
> @@ -4466,8 +4466,68 @@ enum mkbp_cec_event {
>  
>  /*****************************************************************************/
>  
> +/* Commands for audio codec. */
> +#define EC_CMD_EC_CODEC 0x00BC
> +
> +enum ec_codec_subcmd {
> +	EC_CODEC_GET_CAPABILITIES = 0x0,
> +	EC_CODEC_GET_SHM_ADDR = 0x1,
> +	EC_CODEC_SET_SHM_ADDR = 0x2,
> +	EC_CODEC_SUBCMD_COUNT,
> +};
> +
> +enum ec_codec_cap {
> +	EC_CODEC_CAP_LAST = 32,
> +};
> +
> +enum ec_codec_shm_id {
> +	EC_CODEC_SHM_ID_LAST,
> +};
> +
> +enum ec_codec_shm_type {
> +	EC_CODEC_SHM_TYPE_EC_RAM = 0x0,
> +	EC_CODEC_SHM_TYPE_SYSTEM_RAM = 0x1,
> +};
> +
> +struct __ec_align1 ec_param_ec_codec_get_shm_addr {
> +	uint8_t shm_id;
> +	uint8_t reserved[3];
> +};
> +
> +struct __ec_align4 ec_param_ec_codec_set_shm_addr {
> +	uint64_t phys_addr;
> +	uint32_t len;
> +	uint8_t shm_id;
> +	uint8_t reserved[3];
> +};
> +
> +struct __ec_align4 ec_param_ec_codec {
> +	uint8_t cmd; /* enum ec_codec_subcmd */
> +	uint8_t reserved[3];
> +
> +	union {
> +		struct ec_param_ec_codec_get_shm_addr
> +				get_shm_addr_param;
> +		struct ec_param_ec_codec_set_shm_addr
> +				set_shm_addr_param;
> +	};
> +};
> +
> +struct __ec_align4 ec_response_ec_codec_get_capabilities {
> +	uint32_t capabilities;
> +};
> +
> +struct __ec_align4 ec_response_ec_codec_get_shm_addr {
> +	uint64_t phys_addr;
> +	uint32_t len;
> +	uint8_t type;
> +	uint8_t reserved[3];
> +};
> +
> +/*****************************************************************************/
> +
>  /* Commands for DMIC on audio codec. */
> -#define EC_CMD_EC_CODEC_DMIC 0x00BC
> +#define EC_CMD_EC_CODEC_DMIC 0x00BD
>  
>  enum ec_codec_dmic_subcmd {
>  	EC_CODEC_DMIC_SET_GAIN = 0x0,
> @@ -4500,7 +4560,7 @@ struct __ec_align1 ec_response_ec_codec_dmic_get_gain {
>  
>  /* Commands for I2S RX on audio codec. */
>  
> -#define EC_CMD_EC_CODEC_I2S_RX 0x00BD
> +#define EC_CMD_EC_CODEC_I2S_RX 0x00BE
>  
>  enum ec_codec_i2s_rx_subcmd {
>  	EC_CODEC_I2S_RX_ENABLE = 0x0,
> -- 
> 2.23.0.700.g56cf767bdb-goog
>