summaryrefslogtreecommitdiffstats
path: root/sound/soc/codecs/wm8904.c
diff options
context:
space:
mode:
Diffstat (limited to 'sound/soc/codecs/wm8904.c')
-rw-r--r--sound/soc/codecs/wm8904.c325
1 files changed, 314 insertions, 11 deletions
diff --git a/sound/soc/codecs/wm8904.c b/sound/soc/codecs/wm8904.c
index aef82532f8cf..1de09ea646cf 100644
--- a/sound/soc/codecs/wm8904.c
+++ b/sound/soc/codecs/wm8904.c
@@ -844,6 +844,26 @@ static int out_pga_event(struct snd_soc_dapm_widget *w,
return 0;
}
+static const char * const dmic_text[] = {
+ "DMIC1", "DMIC2"
+};
+
+static SOC_ENUM_SINGLE_DECL(dmic_enum, WM8904_DIGITAL_MICROPHONE_0,
+ WM8904_DMIC_SRC_SHIFT, dmic_text);
+
+static const struct snd_kcontrol_new dmic_mux =
+ SOC_DAPM_ENUM("DMIC Mux", dmic_enum);
+
+static const char * const cin_text[] = {
+ "ADC", "DMIC"
+};
+
+static SOC_ENUM_SINGLE_DECL(cin_enum, WM8904_DIGITAL_MICROPHONE_0,
+ WM8904_DMIC_ENA_SHIFT, cin_text);
+
+static const struct snd_kcontrol_new cin_mux =
+ SOC_DAPM_ENUM("Capture Input", cin_enum);
+
static const char *input_mode_text[] = {
"Single-Ended", "Differential Line", "Differential Mic"
};
@@ -963,6 +983,15 @@ SND_SOC_DAPM_AIF_OUT("AIFOUTL", "Capture", 0, SND_SOC_NOPM, 0, 0),
SND_SOC_DAPM_AIF_OUT("AIFOUTR", "Capture", 1, SND_SOC_NOPM, 0, 0),
};
+static const struct snd_soc_dapm_widget wm8904_dmic_dapm_widgets[] = {
+SND_SOC_DAPM_MUX("DMIC Mux", SND_SOC_NOPM, 0, 0, &dmic_mux),
+};
+
+static const struct snd_soc_dapm_widget wm8904_cin_dapm_widgets[] = {
+SND_SOC_DAPM_MUX("Left Capture Input", SND_SOC_NOPM, 0, 0, &cin_mux),
+SND_SOC_DAPM_MUX("Right Capture Input", SND_SOC_NOPM, 0, 0, &cin_mux),
+};
+
static const struct snd_soc_dapm_widget wm8904_dac_dapm_widgets[] = {
SND_SOC_DAPM_AIF_IN("AIFINL", "Playback", 0, SND_SOC_NOPM, 0, 0),
SND_SOC_DAPM_AIF_IN("AIFINR", "Playback", 1, SND_SOC_NOPM, 0, 0),
@@ -1101,12 +1130,45 @@ static const struct snd_soc_dapm_route adc_intercon[] = {
{ "AIFOUTR", NULL, "AIFOUTR Mux" },
{ "ADCL", NULL, "CLK_DSP" },
- { "ADCL", NULL, "Left Capture PGA" },
-
{ "ADCR", NULL, "CLK_DSP" },
+};
+
+/* No DMICs, always connect PGAs */
+static const struct snd_soc_dapm_route cin_nodmic_con[] = {
+ { "ADCL", NULL, "Left Capture PGA" },
{ "ADCR", NULL, "Right Capture PGA" },
};
+/* DMIC system in use: mux between ADC and DMICDAT1, 2 or both */
+static const struct snd_soc_dapm_route cin_adc_dmic_con[] = {
+ { "Left Capture Input", "ADC", "Left Capture PGA" },
+ { "Right Capture Input", "ADC", "Right Capture PGA" },
+
+ { "ADCL", NULL, "Left Capture Input" },
+ { "ADCR", NULL, "Right Capture Input" },
+};
+
+/* IN1L as DMICDAT1 */
+static const struct snd_soc_dapm_route cin_dmic1_con[] = {
+ { "Left Capture Input", "DMIC", "IN1L" },
+ { "Right Capture Input", "DMIC", "IN1L" },
+};
+
+/* IN1R as DMICDAT2 */
+static const struct snd_soc_dapm_route cin_dmic2_con[] = {
+ { "Left Capture Input", "DMIC", "IN1R" },
+ { "Right Capture Input", "DMIC", "IN1R" },
+};
+
+/* DMICDAT1 and DMICDAT2: mux between them, ADC still used for IN2 and IN3 */
+static const struct snd_soc_dapm_route cin_2dmics_con[] = {
+ { "DMIC Mux", "DMIC1", "IN1L" },
+ { "DMIC Mux", "DMIC2", "IN1R" },
+
+ { "Left Capture Input", "DMIC", "DMIC Mux" },
+ { "Right Capture Input", "DMIC", "DMIC Mux" },
+};
+
static const struct snd_soc_dapm_route dac_intercon[] = {
{ "DACL Mux", "Left", "AIFINL" },
{ "DACL Mux", "Right", "AIFINR" },
@@ -1424,15 +1486,15 @@ static int wm8904_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
unsigned int aif3 = 0;
switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
- case SND_SOC_DAIFMT_CBS_CFS:
+ case SND_SOC_DAIFMT_CBC_CFC:
break;
- case SND_SOC_DAIFMT_CBS_CFM:
+ case SND_SOC_DAIFMT_CBC_CFP:
aif3 |= WM8904_LRCLK_DIR;
break;
- case SND_SOC_DAIFMT_CBM_CFS:
+ case SND_SOC_DAIFMT_CBP_CFC:
aif1 |= WM8904_BCLK_DIR;
break;
- case SND_SOC_DAIFMT_CBM_CFM:
+ case SND_SOC_DAIFMT_CBP_CFP:
aif1 |= WM8904_BCLK_DIR;
aif3 |= WM8904_LRCLK_DIR;
break;
@@ -2050,18 +2112,70 @@ static void wm8904_handle_retune_mobile_pdata(struct snd_soc_component *componen
"Failed to add ReTune Mobile control: %d\n", ret);
}
+static void wm8904_handle_dmic_pdata(struct snd_soc_component *component)
+{
+ struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
+ struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
+ struct wm8904_pdata *pdata = wm8904->pdata;
+ unsigned int dmic_src;
+
+ if (!pdata->in1l_as_dmicdat1 && !pdata->in1r_as_dmicdat2) {
+ snd_soc_dapm_add_routes(dapm, cin_nodmic_con,
+ ARRAY_SIZE(cin_nodmic_con));
+ snd_soc_component_update_bits(component, WM8904_DIGITAL_MICROPHONE_0,
+ WM8904_DMIC_ENA_MASK, 0);
+ return;
+ }
+
+ /* Need a control and routing to switch between DMIC and ADC */
+ snd_soc_dapm_new_controls(dapm, wm8904_cin_dapm_widgets,
+ ARRAY_SIZE(wm8904_cin_dapm_widgets));
+ snd_soc_dapm_add_routes(dapm, cin_adc_dmic_con,
+ ARRAY_SIZE(cin_adc_dmic_con));
+
+ if (pdata->in1l_as_dmicdat1 && pdata->in1r_as_dmicdat2) {
+ /* Need a control and routing to mux between DMICDAT1 and 2 */
+ dev_dbg(component->dev, "DMICDAT1 and DMICDAT2 in use\n");
+ snd_soc_dapm_new_controls(dapm, wm8904_dmic_dapm_widgets,
+ ARRAY_SIZE(wm8904_dmic_dapm_widgets));
+ snd_soc_dapm_add_routes(dapm, cin_2dmics_con,
+ ARRAY_SIZE(cin_2dmics_con));
+ return;
+ }
+
+ /* Either DMICDAT1 or DMICDAT2 is in use, not both */
+ if (pdata->in1l_as_dmicdat1) {
+ dmic_src = 0;
+ snd_soc_dapm_add_routes(dapm, cin_dmic1_con,
+ ARRAY_SIZE(cin_dmic1_con));
+ } else {
+ dmic_src = 1;
+ snd_soc_dapm_add_routes(dapm, cin_dmic2_con,
+ ARRAY_SIZE(cin_dmic2_con));
+ }
+ dev_dbg(component->dev, "DMIC_SRC (0 or 1): %d\n", dmic_src);
+ snd_soc_component_update_bits(component, WM8904_DIGITAL_MICROPHONE_0,
+ WM8904_DMIC_SRC_MASK,
+ dmic_src << WM8904_DMIC_SRC_SHIFT);
+}
+
static void wm8904_handle_pdata(struct snd_soc_component *component)
{
+ struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
struct wm8904_pdata *pdata = wm8904->pdata;
int ret, i;
if (!pdata) {
+ snd_soc_dapm_add_routes(dapm, cin_nodmic_con,
+ ARRAY_SIZE(cin_nodmic_con));
snd_soc_add_component_controls(component, wm8904_eq_controls,
- ARRAY_SIZE(wm8904_eq_controls));
+ ARRAY_SIZE(wm8904_eq_controls));
return;
}
+ wm8904_handle_dmic_pdata(component);
+
dev_dbg(component->dev, "%d DRC configurations\n", pdata->num_drc_cfgs);
if (pdata->num_drc_cfgs) {
@@ -2117,10 +2231,11 @@ static int wm8904_probe(struct snd_soc_component *component)
return -EINVAL;
}
- wm8904_handle_pdata(component);
-
wm8904_add_widgets(component);
+ /* This can add dependent widgets, so it is done after add_widgets */
+ wm8904_handle_pdata(component);
+
return 0;
}
@@ -2168,6 +2283,184 @@ static const struct of_device_id wm8904_of_match[] = {
MODULE_DEVICE_TABLE(of, wm8904_of_match);
#endif
+/**
+ * wm8904_read_cfg_reg_arr() - Reads a subarray from a DT u16 array
+ *
+ * @np: pointer to the device_node struct
+ * @regs_property: DT property of interest
+ * @size: size of subarrays within the array
+ * @idx: index of the subarray of interest
+ * @out: output
+ *
+ * Helper to read a subarray from a DT uint16-array,
+ * divided into equally sized arrays of size `size`
+ *
+ * Subset starts at `idx * size` and is of size `size`
+ *
+ * Return: 0 on success, negative error code otherwise
+ */
+static int wm8904_read_cfg_reg_arr(const struct device_node *np,
+ const char * const regs_property,
+ int size, int idx,
+ u16 * const out)
+{
+ int i, offset, ret;
+
+ offset = idx * size;
+
+ for (i = 0; i < size; i++) {
+ ret = of_property_read_u16_index(np, regs_property, i + offset, &out[i]);
+ if (ret)
+ return ret;
+ }
+ return 0;
+}
+
+static int wm8904_parse_retune_cfg_regs(const struct device_node *np,
+ struct wm8904_pdata *pdata, int cfg_idx)
+{
+ return wm8904_read_cfg_reg_arr(np, "wlf,retune-mobile-cfg-regs",
+ WM8904_EQ_REGS, cfg_idx,
+ &pdata->retune_mobile_cfgs[cfg_idx].regs[0]);
+}
+
+static int wm8904_parse_drc_cfg_regs(const struct device_node *np,
+ struct wm8904_pdata *pdata, int cfg_idx)
+{
+ return wm8904_read_cfg_reg_arr(np, "wlf,drc-cfg-regs",
+ WM8904_DRC_REGS, cfg_idx,
+ &pdata->drc_cfgs[cfg_idx].regs[0]);
+}
+
+static int wm8904_parse_drc_cfg_from_of(struct i2c_client *i2c,
+ struct wm8904_pdata *pdata)
+{
+ const struct device_node *np = i2c->dev.of_node;
+ int i, n_cfgs;
+
+ n_cfgs = of_property_count_strings(np, "wlf,drc-cfg-names");
+ if (n_cfgs == -EINVAL)
+ return 0;
+
+ if (n_cfgs <= 0) {
+ dev_err(&i2c->dev, "Could not get wlf,drc-cfg-names length: %d",
+ n_cfgs);
+ return n_cfgs;
+ }
+
+ pdata->drc_cfgs = devm_kzalloc(&i2c->dev,
+ n_cfgs * sizeof(struct wm8904_drc_cfg),
+ GFP_KERNEL);
+ if (!pdata->drc_cfgs)
+ return -ENOMEM;
+
+ for (i = 0; i < n_cfgs; i++) {
+ if (wm8904_parse_drc_cfg_regs(np, pdata, i)) {
+ dev_err(&i2c->dev,
+ "Invalid 'wlf,drc-cfg-regs[%i,:]'\n", i);
+ return -EINVAL;
+ }
+
+ if (of_property_read_string_index(np, "wlf,drc-cfg-names", i,
+ &pdata->drc_cfgs[i].name)) {
+ dev_err(&i2c->dev,
+ "Invalid 'wlf,drc-cfg-names[%i]'\n", i);
+ return -EINVAL;
+ }
+ }
+
+ pdata->num_drc_cfgs = n_cfgs;
+ return 0;
+}
+
+static int wm8904_parse_retune_cfg_from_of(struct i2c_client *i2c,
+ struct wm8904_pdata *pdata)
+{
+ const struct device_node *np = i2c->dev.of_node;
+ int i, n_cfgs;
+
+ n_cfgs = of_property_count_strings(np, "wlf,retune-mobile-cfg-names");
+ if (n_cfgs == -EINVAL)
+ return 0;
+
+ if (n_cfgs <= 0) {
+ dev_err(&i2c->dev,
+ "Could not get wlf,retune-mobile-cfg-names length: %d",
+ n_cfgs);
+ return n_cfgs;
+ }
+
+ pdata->retune_mobile_cfgs = devm_kzalloc(&i2c->dev,
+ n_cfgs * sizeof(struct wm8904_retune_mobile_cfg),
+ GFP_KERNEL);
+ if (!pdata->retune_mobile_cfgs)
+ return -ENOMEM;
+
+ for (i = 0; i < n_cfgs; i++) {
+ if (wm8904_parse_retune_cfg_regs(np, pdata, i)) {
+ dev_err(&i2c->dev,
+ "Invalid 'wlf,retune-mobile-cfg-regs[%i,:]'\n", i);
+ return -EINVAL;
+ }
+
+ if (of_property_read_u32_index(np, "wlf,retune-mobile-cfg-hz", i,
+ &pdata->retune_mobile_cfgs[i].rate)) {
+ dev_err(&i2c->dev,
+ "Invalid 'wlf,retune-mobile-cfg-hz[%i]'\n", i);
+ return -EINVAL;
+ }
+
+ if (of_property_read_string_index(np, "wlf,retune-mobile-cfg-names", i,
+ &pdata->retune_mobile_cfgs[i].name)) {
+ dev_err(&i2c->dev,
+ "Invalid 'wlf,retune-mobile-cfg-names[%i]'\n", i);
+ return -EINVAL;
+ }
+ }
+
+ pdata->num_retune_mobile_cfgs = n_cfgs;
+ return 0;
+}
+
+static int wm8904_set_pdata_from_of(struct i2c_client *i2c,
+ struct wm8904_priv *wm8904)
+{
+ const struct device_node *np = i2c->dev.of_node;
+ struct wm8904_pdata *pdata;
+ int ret, i;
+
+ pdata = devm_kzalloc(&i2c->dev, sizeof(*pdata), GFP_KERNEL);
+ if (!pdata)
+ return -ENOMEM;
+
+ pdata->in1l_as_dmicdat1 =
+ of_property_read_bool(np, "wlf,in1l-as-dmicdat1");
+
+ pdata->in1r_as_dmicdat2 =
+ of_property_read_bool(np, "wlf,in1r-as-dmicdat2");
+
+ /* If absent, default to 0xFFFF for GPIO config (i.e.: don't set) */
+ for (i = 0; i < WM8904_GPIO_REGS; i++)
+ pdata->gpio_cfg[i] = 0xFFFF;
+
+ of_property_read_u32_array(np, "wlf,gpio-cfg", pdata->gpio_cfg,
+ ARRAY_SIZE(pdata->gpio_cfg));
+
+ of_property_read_u32_array(np, "wlf,micbias-cfg", pdata->mic_cfg,
+ ARRAY_SIZE(pdata->mic_cfg));
+
+ ret = wm8904_parse_drc_cfg_from_of(i2c, pdata);
+ if (ret)
+ return ret;
+
+ ret = wm8904_parse_retune_cfg_from_of(i2c, pdata);
+ if (ret)
+ return ret;
+
+ wm8904->pdata = pdata;
+ return 0;
+}
+
static const struct i2c_device_id wm8904_i2c_id[];
static int wm8904_i2c_probe(struct i2c_client *i2c)
@@ -2199,7 +2492,16 @@ static int wm8904_i2c_probe(struct i2c_client *i2c)
wm8904->devtype = (uintptr_t)i2c_get_match_data(i2c);
i2c_set_clientdata(i2c, wm8904);
- wm8904->pdata = i2c->dev.platform_data;
+
+ if (i2c->dev.of_node) {
+ ret = wm8904_set_pdata_from_of(i2c, wm8904);
+ if (ret) {
+ dev_err(&i2c->dev, "Failed to set platform data from of: %d\n", ret);
+ return ret;
+ }
+ } else {
+ wm8904->pdata = i2c->dev.platform_data;
+ }
for (i = 0; i < ARRAY_SIZE(wm8904->supplies); i++)
wm8904->supplies[i].supply = wm8904_supply_names[i];
@@ -2270,7 +2572,8 @@ static int wm8904_i2c_probe(struct i2c_client *i2c)
/* Apply configuration from the platform data. */
if (wm8904->pdata) {
for (i = 0; i < WM8904_GPIO_REGS; i++) {
- if (!wm8904->pdata->gpio_cfg[i])
+ /* 0xFFFF in this config means "don't touch" */
+ if (wm8904->pdata->gpio_cfg[i] == 0xffff)
continue;
regmap_update_bits(wm8904->regmap,