1 // SPDX-License-Identifier: GPL-2.0+
5 // Copyright (C) 2018 Simon Shields <simon@lineageos.org>
6 // Copyright (C) 2020 Samsung Electronics Co., Ltd.
9 #include <linux/gpio/consumer.h>
10 #include <linux/mfd/wm8994/registers.h>
11 #include <linux/module.h>
13 #include <linux/of_device.h>
14 #include <linux/of_gpio.h>
15 #include <linux/regulator/consumer.h>
16 #include <sound/jack.h>
17 #include <sound/soc.h>
18 #include <sound/soc-dapm.h>
21 #include "../codecs/wm8994.h"
24 * The MCLK1 clock source is XCLKOUT with its mux set to the external fixed rate
27 #define MCLK1_RATE 24000000U
28 #define MCLK2_RATE 32768U
29 #define DEFAULT_FLL1_RATE 11289600U
32 struct regulator *reg_mic_bias;
33 struct regulator *reg_submic_bias;
34 struct gpio_desc *gpio_fm_sel;
35 struct gpio_desc *gpio_lineout_sel;
36 unsigned int fll1_rate;
38 struct snd_soc_jack headset_jack;
41 static int midas_start_fll1(struct snd_soc_pcm_runtime *rtd, unsigned int rate)
43 struct snd_soc_card *card = rtd->card;
44 struct midas_priv *priv = snd_soc_card_get_drvdata(card);
45 struct snd_soc_dai *aif1_dai = asoc_rtd_to_codec(rtd, 0);
46 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
50 rate = priv->fll1_rate;
52 * If no new rate is requested, set FLL1 to a sane default for jack
56 rate = DEFAULT_FLL1_RATE;
58 if (rate != priv->fll1_rate && priv->fll1_rate) {
59 /* while reconfiguring, switch to MCLK2 for SYSCLK */
60 ret = snd_soc_dai_set_sysclk(aif1_dai, WM8994_SYSCLK_MCLK2,
61 MCLK2_RATE, SND_SOC_CLOCK_IN);
63 dev_err(card->dev, "Unable to switch to MCLK2: %d\n", ret);
68 ret = snd_soc_dai_set_pll(aif1_dai, WM8994_FLL1, WM8994_FLL_SRC_MCLK1,
71 dev_err(card->dev, "Failed to set FLL1 rate: %d\n", ret);
74 priv->fll1_rate = rate;
76 ret = snd_soc_dai_set_sysclk(aif1_dai, WM8994_SYSCLK_FLL1,
77 priv->fll1_rate, SND_SOC_CLOCK_IN);
79 dev_err(card->dev, "Failed to set SYSCLK source: %d\n", ret);
83 ret = snd_soc_dai_set_sysclk(cpu_dai, SAMSUNG_I2S_OPCLK, 0,
84 SAMSUNG_I2S_OPCLK_PCLK);
86 dev_err(card->dev, "Failed to set OPCLK source: %d\n", ret);
93 static int midas_stop_fll1(struct snd_soc_pcm_runtime *rtd)
95 struct snd_soc_card *card = rtd->card;
96 struct midas_priv *priv = snd_soc_card_get_drvdata(card);
97 struct snd_soc_dai *aif1_dai = asoc_rtd_to_codec(rtd, 0);
100 ret = snd_soc_dai_set_sysclk(aif1_dai, WM8994_SYSCLK_MCLK2,
101 MCLK2_RATE, SND_SOC_CLOCK_IN);
103 dev_err(card->dev, "Unable to switch to MCLK2: %d\n", ret);
107 ret = snd_soc_dai_set_pll(aif1_dai, WM8994_FLL1, 0, 0, 0);
109 dev_err(card->dev, "Unable to stop FLL1: %d\n", ret);
118 static int midas_aif1_hw_params(struct snd_pcm_substream *substream,
119 struct snd_pcm_hw_params *params)
121 struct snd_soc_pcm_runtime *rtd = substream->private_data;
122 unsigned int pll_out;
124 /* AIF1CLK should be at least 3MHz for "optimal performance" */
125 if (params_rate(params) == 8000 || params_rate(params) == 11025)
126 pll_out = params_rate(params) * 512;
128 pll_out = params_rate(params) * 256;
130 return midas_start_fll1(rtd, pll_out);
133 static const struct snd_soc_ops midas_aif1_ops = {
134 .hw_params = midas_aif1_hw_params,
138 * We only have a single external speaker, so mix stereo data
139 * to a single mono stream.
141 static int midas_ext_spkmode(struct snd_soc_dapm_widget *w,
142 struct snd_kcontrol *kcontrol, int event)
144 struct snd_soc_component *codec = snd_soc_dapm_to_component(w->dapm);
148 case SND_SOC_DAPM_PRE_PMU:
149 ret = snd_soc_component_update_bits(codec, WM8994_SPKOUT_MIXERS,
150 WM8994_SPKMIXR_TO_SPKOUTL_MASK,
151 WM8994_SPKMIXR_TO_SPKOUTL);
153 case SND_SOC_DAPM_POST_PMD:
154 ret = snd_soc_component_update_bits(codec, WM8994_SPKOUT_MIXERS,
155 WM8994_SPKMIXR_TO_SPKOUTL_MASK,
163 static int midas_mic_bias(struct snd_soc_dapm_widget *w,
164 struct snd_kcontrol *kcontrol, int event)
166 struct snd_soc_card *card = w->dapm->card;
167 struct midas_priv *priv = snd_soc_card_get_drvdata(card);
170 case SND_SOC_DAPM_PRE_PMU:
171 return regulator_enable(priv->reg_mic_bias);
172 case SND_SOC_DAPM_POST_PMD:
173 return regulator_disable(priv->reg_mic_bias);
179 static int midas_submic_bias(struct snd_soc_dapm_widget *w,
180 struct snd_kcontrol *kcontrol, int event)
182 struct snd_soc_card *card = w->dapm->card;
183 struct midas_priv *priv = snd_soc_card_get_drvdata(card);
186 case SND_SOC_DAPM_PRE_PMU:
187 return regulator_enable(priv->reg_submic_bias);
188 case SND_SOC_DAPM_POST_PMD:
189 return regulator_disable(priv->reg_submic_bias);
195 static int midas_fm_set(struct snd_soc_dapm_widget *w,
196 struct snd_kcontrol *kcontrol, int event)
198 struct snd_soc_card *card = w->dapm->card;
199 struct midas_priv *priv = snd_soc_card_get_drvdata(card);
201 if (!priv->gpio_fm_sel)
205 case SND_SOC_DAPM_PRE_PMU:
206 gpiod_set_value_cansleep(priv->gpio_fm_sel, 1);
208 case SND_SOC_DAPM_POST_PMD:
209 gpiod_set_value_cansleep(priv->gpio_fm_sel, 0);
216 static int midas_line_set(struct snd_soc_dapm_widget *w,
217 struct snd_kcontrol *kcontrol, int event)
219 struct snd_soc_card *card = w->dapm->card;
220 struct midas_priv *priv = snd_soc_card_get_drvdata(card);
222 if (!priv->gpio_lineout_sel)
226 case SND_SOC_DAPM_PRE_PMU:
227 gpiod_set_value_cansleep(priv->gpio_lineout_sel, 1);
229 case SND_SOC_DAPM_POST_PMD:
230 gpiod_set_value_cansleep(priv->gpio_lineout_sel, 0);
237 static const struct snd_kcontrol_new midas_controls[] = {
238 SOC_DAPM_PIN_SWITCH("HP"),
240 SOC_DAPM_PIN_SWITCH("SPK"),
241 SOC_DAPM_PIN_SWITCH("RCV"),
243 SOC_DAPM_PIN_SWITCH("LINE"),
244 SOC_DAPM_PIN_SWITCH("HDMI"),
246 SOC_DAPM_PIN_SWITCH("Main Mic"),
247 SOC_DAPM_PIN_SWITCH("Sub Mic"),
248 SOC_DAPM_PIN_SWITCH("Headset Mic"),
250 SOC_DAPM_PIN_SWITCH("FM In"),
253 static const struct snd_soc_dapm_widget midas_dapm_widgets[] = {
254 SND_SOC_DAPM_HP("HP", NULL),
256 SND_SOC_DAPM_SPK("SPK", midas_ext_spkmode),
257 SND_SOC_DAPM_SPK("RCV", NULL),
259 /* FIXME: toggle MAX77693 on i9300/i9305 */
260 SND_SOC_DAPM_LINE("LINE", midas_line_set),
261 SND_SOC_DAPM_LINE("HDMI", NULL),
262 SND_SOC_DAPM_LINE("FM In", midas_fm_set),
264 SND_SOC_DAPM_MIC("Headset Mic", NULL),
265 SND_SOC_DAPM_MIC("Main Mic", midas_mic_bias),
266 SND_SOC_DAPM_MIC("Sub Mic", midas_submic_bias),
269 static int midas_set_bias_level(struct snd_soc_card *card,
270 struct snd_soc_dapm_context *dapm,
271 enum snd_soc_bias_level level)
273 struct snd_soc_pcm_runtime *rtd = snd_soc_get_pcm_runtime(card,
275 struct snd_soc_dai *aif1_dai = asoc_rtd_to_codec(rtd, 0);
277 if (dapm->dev != aif1_dai->dev)
281 case SND_SOC_BIAS_STANDBY:
282 return midas_stop_fll1(rtd);
283 case SND_SOC_BIAS_PREPARE:
284 return midas_start_fll1(rtd, 0);
292 static int midas_late_probe(struct snd_soc_card *card)
294 struct snd_soc_pcm_runtime *rtd = snd_soc_get_pcm_runtime(card,
296 struct snd_soc_dai *aif1_dai = asoc_rtd_to_codec(rtd, 0);
297 struct midas_priv *priv = snd_soc_card_get_drvdata(card);
300 /* Use MCLK2 as SYSCLK for boot */
301 ret = snd_soc_dai_set_sysclk(aif1_dai, WM8994_SYSCLK_MCLK2, MCLK2_RATE,
304 dev_err(aif1_dai->dev, "Failed to switch to MCLK2: %d\n", ret);
308 ret = snd_soc_card_jack_new(card, "Headset",
309 SND_JACK_HEADSET | SND_JACK_MECHANICAL |
310 SND_JACK_BTN_0 | SND_JACK_BTN_1 | SND_JACK_BTN_2 |
311 SND_JACK_BTN_3 | SND_JACK_BTN_4 | SND_JACK_BTN_5,
312 &priv->headset_jack);
316 wm8958_mic_detect(aif1_dai->component, &priv->headset_jack,
317 NULL, NULL, NULL, NULL);
321 static struct snd_soc_dai_driver midas_ext_dai[] = {
323 .name = "Voice call",
329 .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000),
330 .formats = SNDRV_PCM_FMTBIT_S16_LE,
337 .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000),
338 .formats = SNDRV_PCM_FMTBIT_S16_LE,
348 .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000),
349 .formats = SNDRV_PCM_FMTBIT_S16_LE,
356 .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000),
357 .formats = SNDRV_PCM_FMTBIT_S16_LE,
362 static const struct snd_soc_component_driver midas_component = {
363 .name = "midas-audio",
366 SND_SOC_DAILINK_DEFS(wm1811_hifi,
367 DAILINK_COMP_ARRAY(COMP_EMPTY()),
368 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "wm8994-aif1")),
369 DAILINK_COMP_ARRAY(COMP_EMPTY()));
371 SND_SOC_DAILINK_DEFS(wm1811_voice,
372 DAILINK_COMP_ARRAY(COMP_EMPTY()),
373 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "wm8994-aif2")),
374 DAILINK_COMP_ARRAY(COMP_EMPTY()));
376 SND_SOC_DAILINK_DEFS(wm1811_bt,
377 DAILINK_COMP_ARRAY(COMP_EMPTY()),
378 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "wm8994-aif3")),
379 DAILINK_COMP_ARRAY(COMP_EMPTY()));
381 static struct snd_soc_dai_link midas_dai[] = {
383 .name = "WM8994 AIF1",
384 .stream_name = "HiFi Primary",
385 .ops = &midas_aif1_ops,
386 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
387 SND_SOC_DAIFMT_CBM_CFM,
388 SND_SOC_DAILINK_REG(wm1811_hifi),
390 .name = "WM1811 Voice",
391 .stream_name = "Voice call",
393 SND_SOC_DAILINK_REG(wm1811_voice),
396 .stream_name = "Bluetooth",
398 SND_SOC_DAILINK_REG(wm1811_bt),
402 static struct snd_soc_card midas_card = {
403 .name = "Midas WM1811",
404 .owner = THIS_MODULE,
406 .dai_link = midas_dai,
407 .num_links = ARRAY_SIZE(midas_dai),
408 .controls = midas_controls,
409 .num_controls = ARRAY_SIZE(midas_controls),
410 .dapm_widgets = midas_dapm_widgets,
411 .num_dapm_widgets = ARRAY_SIZE(midas_dapm_widgets),
413 .set_bias_level = midas_set_bias_level,
414 .late_probe = midas_late_probe,
417 static int midas_probe(struct platform_device *pdev)
419 struct device_node *cpu_dai_node = NULL, *codec_dai_node = NULL;
420 struct device_node *cpu = NULL, *codec = NULL;
421 struct snd_soc_card *card = &midas_card;
422 struct device *dev = &pdev->dev;
423 static struct snd_soc_dai_link *dai_link;
424 struct midas_priv *priv;
427 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
431 snd_soc_card_set_drvdata(card, priv);
434 priv->reg_mic_bias = devm_regulator_get(dev, "mic-bias");
435 if (IS_ERR(priv->reg_mic_bias)) {
436 dev_err(dev, "Failed to get mic bias regulator\n");
437 return PTR_ERR(priv->reg_mic_bias);
440 priv->reg_submic_bias = devm_regulator_get(dev, "submic-bias");
441 if (IS_ERR(priv->reg_submic_bias)) {
442 dev_err(dev, "Failed to get submic bias regulator\n");
443 return PTR_ERR(priv->reg_submic_bias);
446 priv->gpio_fm_sel = devm_gpiod_get_optional(dev, "fm-sel", GPIOD_OUT_HIGH);
447 if (IS_ERR(priv->gpio_fm_sel)) {
448 dev_err(dev, "Failed to get FM selection GPIO\n");
449 return PTR_ERR(priv->gpio_fm_sel);
452 priv->gpio_lineout_sel = devm_gpiod_get_optional(dev, "lineout-sel",
454 if (IS_ERR(priv->gpio_lineout_sel)) {
455 dev_err(dev, "Failed to get line out selection GPIO\n");
456 return PTR_ERR(priv->gpio_lineout_sel);
459 ret = snd_soc_of_parse_card_name(card, "model");
461 dev_err(dev, "Card name is not specified\n");
465 ret = snd_soc_of_parse_audio_routing(card, "samsung,audio-routing");
467 dev_err(dev, "Audio routing invalid/unspecified\n");
471 cpu = of_get_child_by_name(dev->of_node, "cpu");
475 codec = of_get_child_by_name(dev->of_node, "codec");
481 cpu_dai_node = of_parse_phandle(cpu, "sound-dai", 0);
484 dev_err(dev, "parsing cpu/sound-dai failed\n");
489 codec_dai_node = of_parse_phandle(codec, "sound-dai", 0);
491 if (!codec_dai_node) {
492 dev_err(dev, "audio-codec property invalid/missing\n");
494 goto put_cpu_dai_node;
497 for_each_card_prelinks(card, i, dai_link) {
498 dai_link->codecs->of_node = codec_dai_node;
499 dai_link->cpus->of_node = cpu_dai_node;
500 dai_link->platforms->of_node = cpu_dai_node;
503 ret = devm_snd_soc_register_component(dev, &midas_component,
504 midas_ext_dai, ARRAY_SIZE(midas_ext_dai));
506 dev_err(dev, "Failed to register component: %d\n", ret);
507 goto put_codec_dai_node;
510 ret = devm_snd_soc_register_card(dev, card);
512 dev_err(dev, "Failed to register card: %d\n", ret);
513 goto put_codec_dai_node;
519 of_node_put(codec_dai_node);
521 of_node_put(cpu_dai_node);
525 static const struct of_device_id midas_of_match[] = {
526 { .compatible = "samsung,midas-audio" },
529 MODULE_DEVICE_TABLE(of, midas_of_match);
531 static struct platform_driver midas_driver = {
533 .name = "midas-audio",
534 .of_match_table = midas_of_match,
535 .pm = &snd_soc_pm_ops,
537 .probe = midas_probe,
539 module_platform_driver(midas_driver);
541 MODULE_AUTHOR("Simon Shields <simon@lineageos.org>");
542 MODULE_DESCRIPTION("ASoC support for Midas");
543 MODULE_LICENSE("GPL v2");