1 // SPDX-License-Identifier: GPL-2.0+
2 #include <linux/extcon.h>
3 #include <linux/iio/consumer.h>
4 #include <linux/input-event-codes.h>
5 #include <linux/mfd/wm8994/registers.h>
6 #include <linux/module.h>
8 #include <linux/of_gpio.h>
9 #include <linux/regulator/consumer.h>
10 #include <sound/jack.h>
11 #include <sound/pcm_params.h>
12 #include <sound/soc.h>
15 #include "../codecs/wm8994.h"
17 #define ARIES_MCLK1_FREQ 24000000
19 struct aries_wm8994_variant {
20 unsigned int modem_dai_fmt;
24 struct aries_wm8994_data {
25 struct extcon_dev *usb_extcon;
26 struct regulator *reg_main_micbias;
27 struct regulator *reg_headset_micbias;
28 struct gpio_desc *gpio_headset_detect;
29 struct gpio_desc *gpio_headset_key;
30 struct gpio_desc *gpio_earpath_sel;
31 struct iio_channel *adc;
32 const struct aries_wm8994_variant *variant;
36 static struct snd_soc_jack aries_dock;
38 static struct snd_soc_jack_pin dock_pins[] = {
41 .mask = SND_JACK_LINEOUT,
45 static int aries_extcon_notifier(struct notifier_block *this,
46 unsigned long connected, void *_cmd)
49 snd_soc_jack_report(&aries_dock, SND_JACK_LINEOUT,
52 snd_soc_jack_report(&aries_dock, 0, SND_JACK_LINEOUT);
57 static struct notifier_block aries_extcon_notifier_block = {
58 .notifier_call = aries_extcon_notifier,
62 static struct snd_soc_jack aries_headset;
64 static struct snd_soc_jack_pin jack_pins[] = {
67 .mask = SND_JACK_HEADPHONE,
70 .mask = SND_JACK_MICROPHONE,
74 static struct snd_soc_jack_zone headset_zones[] = {
78 .jack_type = SND_JACK_HEADPHONE,
82 .jack_type = SND_JACK_HEADSET,
86 .jack_type = SND_JACK_HEADPHONE,
90 static irqreturn_t headset_det_irq_thread(int irq, void *data)
92 struct aries_wm8994_data *priv = (struct aries_wm8994_data *) data;
94 int time_left_ms = 300;
97 while (time_left_ms > 0) {
98 if (!gpiod_get_value(priv->gpio_headset_detect)) {
99 snd_soc_jack_report(&aries_headset, 0,
101 gpiod_set_value(priv->gpio_earpath_sel, 0);
108 /* Temporarily enable micbias and earpath selector */
109 ret = regulator_enable(priv->reg_headset_micbias);
111 pr_err("%s failed to enable micbias: %d", __func__, ret);
113 gpiod_set_value(priv->gpio_earpath_sel, 1);
115 ret = iio_read_channel_processed(priv->adc, &adc);
117 /* failed to read ADC, so assume headphone */
118 pr_err("%s failed to read ADC, assuming headphones", __func__);
119 snd_soc_jack_report(&aries_headset, SND_JACK_HEADPHONE,
122 snd_soc_jack_report(&aries_headset,
123 snd_soc_jack_get_type(&aries_headset, adc),
127 ret = regulator_disable(priv->reg_headset_micbias);
129 pr_err("%s failed disable micbias: %d", __func__, ret);
131 /* Disable earpath selector when no mic connected */
132 if (!(aries_headset.status & SND_JACK_MICROPHONE))
133 gpiod_set_value(priv->gpio_earpath_sel, 0);
138 static int headset_button_check(void *data)
140 struct aries_wm8994_data *priv = (struct aries_wm8994_data *) data;
142 /* Filter out keypresses when 4 pole jack not detected */
143 if (gpiod_get_value_cansleep(priv->gpio_headset_key) &&
144 aries_headset.status & SND_JACK_MICROPHONE)
145 return SND_JACK_BTN_0;
150 static struct snd_soc_jack_gpio headset_button_gpio[] = {
152 .name = "Media Button",
153 .report = SND_JACK_BTN_0,
155 .jack_status_check = headset_button_check,
159 static int aries_spk_cfg(struct snd_soc_dapm_widget *w,
160 struct snd_kcontrol *kcontrol, int event)
162 struct snd_soc_card *card = w->dapm->card;
163 struct snd_soc_pcm_runtime *rtd;
164 struct snd_soc_component *component;
167 rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]);
168 component = snd_soc_rtd_to_codec(rtd, 0)->component;
171 * We have an odd setup - the SPKMODE pin is pulled up so
172 * we only have access to the left side SPK configs,
173 * but SPKOUTR isn't bridged so when playing back in
174 * stereo, we only get the left hand channel. The only
175 * option we're left with is to force the AIF into mono
179 case SND_SOC_DAPM_POST_PMU:
180 ret = snd_soc_component_update_bits(component,
181 WM8994_AIF1_DAC1_FILTERS_1,
182 WM8994_AIF1DAC1_MONO, WM8994_AIF1DAC1_MONO);
184 case SND_SOC_DAPM_PRE_PMD:
185 ret = snd_soc_component_update_bits(component,
186 WM8994_AIF1_DAC1_FILTERS_1,
187 WM8994_AIF1DAC1_MONO, 0);
194 static int aries_main_bias(struct snd_soc_dapm_widget *w,
195 struct snd_kcontrol *kcontrol, int event)
197 struct snd_soc_card *card = w->dapm->card;
198 struct aries_wm8994_data *priv = snd_soc_card_get_drvdata(card);
202 case SND_SOC_DAPM_PRE_PMU:
203 ret = regulator_enable(priv->reg_main_micbias);
205 case SND_SOC_DAPM_POST_PMD:
206 ret = regulator_disable(priv->reg_main_micbias);
213 static int aries_headset_bias(struct snd_soc_dapm_widget *w,
214 struct snd_kcontrol *kcontrol, int event)
216 struct snd_soc_card *card = w->dapm->card;
217 struct aries_wm8994_data *priv = snd_soc_card_get_drvdata(card);
221 case SND_SOC_DAPM_PRE_PMU:
222 ret = regulator_enable(priv->reg_headset_micbias);
224 case SND_SOC_DAPM_POST_PMD:
225 ret = regulator_disable(priv->reg_headset_micbias);
232 static const struct snd_kcontrol_new aries_controls[] = {
233 SOC_DAPM_PIN_SWITCH("Modem In"),
234 SOC_DAPM_PIN_SWITCH("Modem Out"),
237 static const struct snd_soc_dapm_widget aries_dapm_widgets[] = {
238 SND_SOC_DAPM_HP("HP", NULL),
240 SND_SOC_DAPM_SPK("SPK", aries_spk_cfg),
241 SND_SOC_DAPM_SPK("RCV", NULL),
243 SND_SOC_DAPM_LINE("LINE", NULL),
245 SND_SOC_DAPM_MIC("Main Mic", aries_main_bias),
246 SND_SOC_DAPM_MIC("Headset Mic", aries_headset_bias),
248 SND_SOC_DAPM_MIC("Bluetooth Mic", NULL),
249 SND_SOC_DAPM_SPK("Bluetooth SPK", NULL),
251 SND_SOC_DAPM_LINE("Modem In", NULL),
252 SND_SOC_DAPM_LINE("Modem Out", NULL),
254 /* This must be last as it is conditionally not used */
255 SND_SOC_DAPM_LINE("FM In", NULL),
258 static int aries_hw_params(struct snd_pcm_substream *substream,
259 struct snd_pcm_hw_params *params)
261 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
262 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
263 unsigned int pll_out;
266 /* AIF1CLK should be >=3MHz for optimal performance */
267 if (params_width(params) == 24)
268 pll_out = params_rate(params) * 384;
269 else if (params_rate(params) == 8000 || params_rate(params) == 11025)
270 pll_out = params_rate(params) * 512;
272 pll_out = params_rate(params) * 256;
274 ret = snd_soc_dai_set_pll(codec_dai, WM8994_FLL1, WM8994_FLL_SRC_MCLK1,
275 ARIES_MCLK1_FREQ, pll_out);
279 ret = snd_soc_dai_set_sysclk(codec_dai, WM8994_SYSCLK_FLL1,
280 pll_out, SND_SOC_CLOCK_IN);
287 static int aries_hw_free(struct snd_pcm_substream *substream)
289 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
290 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
293 /* Switch sysclk to MCLK1 */
294 ret = snd_soc_dai_set_sysclk(codec_dai, WM8994_SYSCLK_MCLK1,
295 ARIES_MCLK1_FREQ, SND_SOC_CLOCK_IN);
300 ret = snd_soc_dai_set_pll(codec_dai, WM8994_FLL1, WM8994_FLL_SRC_MCLK1,
301 ARIES_MCLK1_FREQ, 0);
309 * Main DAI operations
311 static const struct snd_soc_ops aries_ops = {
312 .hw_params = aries_hw_params,
313 .hw_free = aries_hw_free,
316 static int aries_baseband_init(struct snd_soc_pcm_runtime *rtd)
318 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
319 unsigned int pll_out;
322 pll_out = 8000 * 512;
324 /* Set the codec FLL */
325 ret = snd_soc_dai_set_pll(codec_dai, WM8994_FLL2, WM8994_FLL_SRC_MCLK1,
326 ARIES_MCLK1_FREQ, pll_out);
330 /* Set the codec system clock */
331 ret = snd_soc_dai_set_sysclk(codec_dai, WM8994_SYSCLK_FLL2,
332 pll_out, SND_SOC_CLOCK_IN);
339 static int aries_late_probe(struct snd_soc_card *card)
341 struct aries_wm8994_data *priv = snd_soc_card_get_drvdata(card);
344 ret = snd_soc_card_jack_new_pins(card, "Dock", SND_JACK_LINEOUT,
345 &aries_dock, dock_pins, ARRAY_SIZE(dock_pins));
349 ret = devm_extcon_register_notifier(card->dev,
350 priv->usb_extcon, EXTCON_JACK_LINE_OUT,
351 &aries_extcon_notifier_block);
355 if (extcon_get_state(priv->usb_extcon,
356 EXTCON_JACK_LINE_OUT) > 0)
357 snd_soc_jack_report(&aries_dock, SND_JACK_LINEOUT,
360 snd_soc_jack_report(&aries_dock, 0, SND_JACK_LINEOUT);
362 ret = snd_soc_card_jack_new_pins(card, "Headset",
363 SND_JACK_HEADSET | SND_JACK_BTN_0,
365 jack_pins, ARRAY_SIZE(jack_pins));
369 ret = snd_soc_jack_add_zones(&aries_headset, ARRAY_SIZE(headset_zones),
374 irq = gpiod_to_irq(priv->gpio_headset_detect);
376 dev_err(card->dev, "Failed to map headset detect gpio to irq");
380 ret = devm_request_threaded_irq(card->dev, irq, NULL,
381 headset_det_irq_thread,
382 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
383 IRQF_ONESHOT, "headset_detect", priv);
385 dev_err(card->dev, "Failed to request headset detect irq");
389 headset_button_gpio[0].data = priv;
390 headset_button_gpio[0].desc = priv->gpio_headset_key;
392 snd_jack_set_key(aries_headset.jack, SND_JACK_BTN_0, KEY_MEDIA);
394 return snd_soc_jack_add_gpios(&aries_headset,
395 ARRAY_SIZE(headset_button_gpio), headset_button_gpio);
398 static const struct snd_soc_pcm_stream baseband_params = {
399 .formats = SNDRV_PCM_FMTBIT_S16_LE,
406 static const struct snd_soc_pcm_stream bluetooth_params = {
407 .formats = SNDRV_PCM_FMTBIT_S16_LE,
414 static const struct snd_soc_dapm_widget aries_modem_widgets[] = {
415 SND_SOC_DAPM_INPUT("Modem RX"),
416 SND_SOC_DAPM_OUTPUT("Modem TX"),
419 static const struct snd_soc_dapm_route aries_modem_routes[] = {
420 { "Modem Capture", NULL, "Modem RX" },
421 { "Modem TX", NULL, "Modem Playback" },
424 static const struct snd_soc_component_driver aries_component = {
425 .name = "aries-audio",
426 .dapm_widgets = aries_modem_widgets,
427 .num_dapm_widgets = ARRAY_SIZE(aries_modem_widgets),
428 .dapm_routes = aries_modem_routes,
429 .num_dapm_routes = ARRAY_SIZE(aries_modem_routes),
431 .use_pmdown_time = 1,
435 static struct snd_soc_dai_driver aries_ext_dai[] = {
437 .name = "Voice call",
439 .stream_name = "Modem Playback",
444 .rates = SNDRV_PCM_RATE_8000,
445 .formats = SNDRV_PCM_FMTBIT_S16_LE,
448 .stream_name = "Modem Capture",
453 .rates = SNDRV_PCM_RATE_8000,
454 .formats = SNDRV_PCM_FMTBIT_S16_LE,
459 SND_SOC_DAILINK_DEFS(aif1,
460 DAILINK_COMP_ARRAY(COMP_CPU(SAMSUNG_I2S_DAI)),
461 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "wm8994-aif1")),
462 DAILINK_COMP_ARRAY(COMP_EMPTY()));
464 SND_SOC_DAILINK_DEFS(baseband,
465 DAILINK_COMP_ARRAY(COMP_CPU("Voice call")),
466 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "wm8994-aif2")));
468 SND_SOC_DAILINK_DEFS(bluetooth,
469 DAILINK_COMP_ARRAY(COMP_CPU("bt-sco-pcm")),
470 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "wm8994-aif3")));
472 static struct snd_soc_dai_link aries_dai[] = {
474 .name = "WM8994 AIF1",
475 .stream_name = "HiFi",
476 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
477 SND_SOC_DAIFMT_CBM_CFM,
479 SND_SOC_DAILINK_REG(aif1),
482 .name = "WM8994 AIF2",
483 .stream_name = "Baseband",
484 .init = &aries_baseband_init,
485 .c2c_params = &baseband_params,
488 SND_SOC_DAILINK_REG(baseband),
491 .name = "WM8994 AIF3",
492 .stream_name = "Bluetooth",
493 .c2c_params = &bluetooth_params,
496 SND_SOC_DAILINK_REG(bluetooth),
500 static struct snd_soc_card aries_card = {
502 .owner = THIS_MODULE,
503 .dai_link = aries_dai,
504 .num_links = ARRAY_SIZE(aries_dai),
505 .controls = aries_controls,
506 .num_controls = ARRAY_SIZE(aries_controls),
507 .dapm_widgets = aries_dapm_widgets,
508 .num_dapm_widgets = ARRAY_SIZE(aries_dapm_widgets),
509 .late_probe = aries_late_probe,
512 static const struct aries_wm8994_variant fascinate4g_variant = {
513 .modem_dai_fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_CBS_CFS
514 | SND_SOC_DAIFMT_IB_NF,
515 .has_fm_radio = false,
518 static const struct aries_wm8994_variant aries_variant = {
519 .modem_dai_fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_CBM_CFM
520 | SND_SOC_DAIFMT_IB_NF,
521 .has_fm_radio = true,
524 static const struct of_device_id samsung_wm8994_of_match[] = {
526 .compatible = "samsung,fascinate4g-wm8994",
527 .data = &fascinate4g_variant,
530 .compatible = "samsung,aries-wm8994",
531 .data = &aries_variant,
535 MODULE_DEVICE_TABLE(of, samsung_wm8994_of_match);
537 static int aries_audio_probe(struct platform_device *pdev)
539 struct device_node *np = pdev->dev.of_node;
540 struct device_node *cpu, *codec, *extcon_np;
541 struct device *dev = &pdev->dev;
542 struct snd_soc_card *card = &aries_card;
543 struct aries_wm8994_data *priv;
544 struct snd_soc_dai_link *dai_link;
545 const struct of_device_id *match;
546 enum iio_chan_type channel_type;
554 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
558 snd_soc_card_set_drvdata(card, priv);
560 match = of_match_node(samsung_wm8994_of_match, np);
561 priv->variant = match->data;
563 /* Remove FM widget if not present */
564 if (!priv->variant->has_fm_radio)
565 card->num_dapm_widgets--;
567 priv->reg_main_micbias = devm_regulator_get(dev, "main-micbias");
568 if (IS_ERR(priv->reg_main_micbias)) {
569 dev_err(dev, "Failed to get main micbias regulator\n");
570 return PTR_ERR(priv->reg_main_micbias);
573 priv->reg_headset_micbias = devm_regulator_get(dev, "headset-micbias");
574 if (IS_ERR(priv->reg_headset_micbias)) {
575 dev_err(dev, "Failed to get headset micbias regulator\n");
576 return PTR_ERR(priv->reg_headset_micbias);
579 priv->gpio_earpath_sel = devm_gpiod_get(dev, "earpath-sel",
581 if (IS_ERR(priv->gpio_earpath_sel)) {
582 dev_err(dev, "Failed to get earpath selector gpio");
583 return PTR_ERR(priv->gpio_earpath_sel);
586 extcon_np = of_parse_phandle(np, "extcon", 0);
587 priv->usb_extcon = extcon_find_edev_by_node(extcon_np);
588 of_node_put(extcon_np);
589 if (IS_ERR(priv->usb_extcon))
590 return dev_err_probe(dev, PTR_ERR(priv->usb_extcon),
591 "Failed to get extcon device");
593 priv->adc = devm_iio_channel_get(dev, "headset-detect");
594 if (IS_ERR(priv->adc))
595 return dev_err_probe(dev, PTR_ERR(priv->adc),
596 "Failed to get ADC channel");
598 ret = iio_get_channel_type(priv->adc, &channel_type);
600 return dev_err_probe(dev, ret,
601 "Failed to get ADC channel type");
602 if (channel_type != IIO_VOLTAGE)
605 priv->gpio_headset_key = devm_gpiod_get(dev, "headset-key",
607 if (IS_ERR(priv->gpio_headset_key)) {
608 dev_err(dev, "Failed to get headset key gpio");
609 return PTR_ERR(priv->gpio_headset_key);
612 priv->gpio_headset_detect = devm_gpiod_get(dev,
613 "headset-detect", GPIOD_IN);
614 if (IS_ERR(priv->gpio_headset_detect)) {
615 dev_err(dev, "Failed to get headset detect gpio");
616 return PTR_ERR(priv->gpio_headset_detect);
619 /* Update card-name if provided through DT, else use default name */
620 snd_soc_of_parse_card_name(card, "model");
622 ret = snd_soc_of_parse_audio_routing(card, "audio-routing");
624 /* Backwards compatible way */
625 ret = snd_soc_of_parse_audio_routing(card, "samsung,audio-routing");
627 dev_err(dev, "Audio routing invalid/unspecified\n");
632 aries_dai[1].dai_fmt = priv->variant->modem_dai_fmt;
634 cpu = of_get_child_by_name(dev->of_node, "cpu");
638 codec = of_get_child_by_name(dev->of_node, "codec");
644 for_each_card_prelinks(card, i, dai_link) {
645 dai_link->codecs->of_node = of_parse_phandle(codec,
647 if (!dai_link->codecs->of_node) {
653 /* Set CPU and platform of_node for main DAI */
654 aries_dai[0].cpus->of_node = of_parse_phandle(cpu,
656 if (!aries_dai[0].cpus->of_node) {
661 aries_dai[0].platforms->of_node = aries_dai[0].cpus->of_node;
663 /* Set CPU of_node for BT DAI */
664 aries_dai[2].cpus->of_node = of_parse_phandle(cpu,
666 if (!aries_dai[2].cpus->of_node) {
671 ret = devm_snd_soc_register_component(dev, &aries_component,
672 aries_ext_dai, ARRAY_SIZE(aries_ext_dai));
674 dev_err(dev, "Failed to register component: %d\n", ret);
678 ret = devm_snd_soc_register_card(dev, card);
680 dev_err(dev, "snd_soc_register_card() failed:%d\n", ret);
689 static struct platform_driver aries_audio_driver = {
691 .name = "aries-audio-wm8994",
692 .of_match_table = of_match_ptr(samsung_wm8994_of_match),
693 .pm = &snd_soc_pm_ops,
695 .probe = aries_audio_probe,
698 module_platform_driver(aries_audio_driver);
700 MODULE_DESCRIPTION("ALSA SoC ARIES WM8994");
701 MODULE_LICENSE("GPL");
702 MODULE_ALIAS("platform:aries-audio-wm8994");