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_device.h>
9 #include <linux/of_gpio.h>
10 #include <linux/regulator/consumer.h>
11 #include <sound/jack.h>
12 #include <sound/pcm_params.h>
13 #include <sound/soc.h>
16 #include "../codecs/wm8994.h"
18 #define ARIES_MCLK1_FREQ 24000000
20 struct aries_wm8994_variant {
21 unsigned int modem_dai_fmt;
25 struct aries_wm8994_data {
26 struct extcon_dev *usb_extcon;
27 struct regulator *reg_main_micbias;
28 struct regulator *reg_headset_micbias;
29 struct gpio_desc *gpio_headset_detect;
30 struct gpio_desc *gpio_headset_key;
31 struct gpio_desc *gpio_earpath_sel;
32 struct iio_channel *adc;
33 const struct aries_wm8994_variant *variant;
37 static struct snd_soc_jack aries_dock;
39 static struct snd_soc_jack_pin dock_pins[] = {
42 .mask = SND_JACK_LINEOUT,
46 static int aries_extcon_notifier(struct notifier_block *this,
47 unsigned long connected, void *_cmd)
50 snd_soc_jack_report(&aries_dock, SND_JACK_LINEOUT,
53 snd_soc_jack_report(&aries_dock, 0, SND_JACK_LINEOUT);
58 static struct notifier_block aries_extcon_notifier_block = {
59 .notifier_call = aries_extcon_notifier,
63 static struct snd_soc_jack aries_headset;
65 static struct snd_soc_jack_pin jack_pins[] = {
68 .mask = SND_JACK_HEADPHONE,
71 .mask = SND_JACK_MICROPHONE,
75 static struct snd_soc_jack_zone headset_zones[] = {
79 .jack_type = SND_JACK_HEADPHONE,
83 .jack_type = SND_JACK_HEADSET,
87 .jack_type = SND_JACK_HEADPHONE,
91 static irqreturn_t headset_det_irq_thread(int irq, void *data)
93 struct aries_wm8994_data *priv = (struct aries_wm8994_data *) data;
95 int time_left_ms = 300;
98 while (time_left_ms > 0) {
99 if (!gpiod_get_value(priv->gpio_headset_detect)) {
100 snd_soc_jack_report(&aries_headset, 0,
102 gpiod_set_value(priv->gpio_earpath_sel, 0);
109 /* Temporarily enable micbias and earpath selector */
110 ret = regulator_enable(priv->reg_headset_micbias);
112 pr_err("%s failed to enable micbias: %d", __func__, ret);
114 gpiod_set_value(priv->gpio_earpath_sel, 1);
116 ret = iio_read_channel_processed(priv->adc, &adc);
118 /* failed to read ADC, so assume headphone */
119 pr_err("%s failed to read ADC, assuming headphones", __func__);
120 snd_soc_jack_report(&aries_headset, SND_JACK_HEADPHONE,
123 snd_soc_jack_report(&aries_headset,
124 snd_soc_jack_get_type(&aries_headset, adc),
128 ret = regulator_disable(priv->reg_headset_micbias);
130 pr_err("%s failed disable micbias: %d", __func__, ret);
132 /* Disable earpath selector when no mic connected */
133 if (!(aries_headset.status & SND_JACK_MICROPHONE))
134 gpiod_set_value(priv->gpio_earpath_sel, 0);
139 static int headset_button_check(void *data)
141 struct aries_wm8994_data *priv = (struct aries_wm8994_data *) data;
143 /* Filter out keypresses when 4 pole jack not detected */
144 if (gpiod_get_value_cansleep(priv->gpio_headset_key) &&
145 aries_headset.status & SND_JACK_MICROPHONE)
146 return SND_JACK_BTN_0;
151 static struct snd_soc_jack_gpio headset_button_gpio[] = {
153 .name = "Media Button",
154 .report = SND_JACK_BTN_0,
156 .jack_status_check = headset_button_check,
160 static int aries_spk_cfg(struct snd_soc_dapm_widget *w,
161 struct snd_kcontrol *kcontrol, int event)
163 struct snd_soc_card *card = w->dapm->card;
164 struct snd_soc_pcm_runtime *rtd;
165 struct snd_soc_component *component;
168 rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]);
169 component = asoc_rtd_to_codec(rtd, 0)->component;
172 * We have an odd setup - the SPKMODE pin is pulled up so
173 * we only have access to the left side SPK configs,
174 * but SPKOUTR isn't bridged so when playing back in
175 * stereo, we only get the left hand channel. The only
176 * option we're left with is to force the AIF into mono
180 case SND_SOC_DAPM_POST_PMU:
181 ret = snd_soc_component_update_bits(component,
182 WM8994_AIF1_DAC1_FILTERS_1,
183 WM8994_AIF1DAC1_MONO, WM8994_AIF1DAC1_MONO);
185 case SND_SOC_DAPM_PRE_PMD:
186 ret = snd_soc_component_update_bits(component,
187 WM8994_AIF1_DAC1_FILTERS_1,
188 WM8994_AIF1DAC1_MONO, 0);
195 static int aries_main_bias(struct snd_soc_dapm_widget *w,
196 struct snd_kcontrol *kcontrol, int event)
198 struct snd_soc_card *card = w->dapm->card;
199 struct aries_wm8994_data *priv = snd_soc_card_get_drvdata(card);
203 case SND_SOC_DAPM_PRE_PMU:
204 ret = regulator_enable(priv->reg_main_micbias);
206 case SND_SOC_DAPM_POST_PMD:
207 ret = regulator_disable(priv->reg_main_micbias);
214 static int aries_headset_bias(struct snd_soc_dapm_widget *w,
215 struct snd_kcontrol *kcontrol, int event)
217 struct snd_soc_card *card = w->dapm->card;
218 struct aries_wm8994_data *priv = snd_soc_card_get_drvdata(card);
222 case SND_SOC_DAPM_PRE_PMU:
223 ret = regulator_enable(priv->reg_headset_micbias);
225 case SND_SOC_DAPM_POST_PMD:
226 ret = regulator_disable(priv->reg_headset_micbias);
233 static const struct snd_kcontrol_new aries_controls[] = {
234 SOC_DAPM_PIN_SWITCH("Modem In"),
235 SOC_DAPM_PIN_SWITCH("Modem Out"),
238 static const struct snd_soc_dapm_widget aries_dapm_widgets[] = {
239 SND_SOC_DAPM_HP("HP", NULL),
241 SND_SOC_DAPM_SPK("SPK", aries_spk_cfg),
242 SND_SOC_DAPM_SPK("RCV", NULL),
244 SND_SOC_DAPM_LINE("LINE", NULL),
246 SND_SOC_DAPM_MIC("Main Mic", aries_main_bias),
247 SND_SOC_DAPM_MIC("Headset Mic", aries_headset_bias),
249 SND_SOC_DAPM_MIC("Bluetooth Mic", NULL),
250 SND_SOC_DAPM_SPK("Bluetooth SPK", NULL),
252 SND_SOC_DAPM_LINE("Modem In", NULL),
253 SND_SOC_DAPM_LINE("Modem Out", NULL),
255 /* This must be last as it is conditionally not used */
256 SND_SOC_DAPM_LINE("FM In", NULL),
259 static int aries_hw_params(struct snd_pcm_substream *substream,
260 struct snd_pcm_hw_params *params)
262 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
263 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
264 unsigned int pll_out;
267 /* AIF1CLK should be >=3MHz for optimal performance */
268 if (params_width(params) == 24)
269 pll_out = params_rate(params) * 384;
270 else if (params_rate(params) == 8000 || params_rate(params) == 11025)
271 pll_out = params_rate(params) * 512;
273 pll_out = params_rate(params) * 256;
275 ret = snd_soc_dai_set_pll(codec_dai, WM8994_FLL1, WM8994_FLL_SRC_MCLK1,
276 ARIES_MCLK1_FREQ, pll_out);
280 ret = snd_soc_dai_set_sysclk(codec_dai, WM8994_SYSCLK_FLL1,
281 pll_out, SND_SOC_CLOCK_IN);
288 static int aries_hw_free(struct snd_pcm_substream *substream)
290 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
291 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
294 /* Switch sysclk to MCLK1 */
295 ret = snd_soc_dai_set_sysclk(codec_dai, WM8994_SYSCLK_MCLK1,
296 ARIES_MCLK1_FREQ, SND_SOC_CLOCK_IN);
301 ret = snd_soc_dai_set_pll(codec_dai, WM8994_FLL1, WM8994_FLL_SRC_MCLK1,
302 ARIES_MCLK1_FREQ, 0);
310 * Main DAI operations
312 static const struct snd_soc_ops aries_ops = {
313 .hw_params = aries_hw_params,
314 .hw_free = aries_hw_free,
317 static int aries_baseband_init(struct snd_soc_pcm_runtime *rtd)
319 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
320 unsigned int pll_out;
323 pll_out = 8000 * 512;
325 /* Set the codec FLL */
326 ret = snd_soc_dai_set_pll(codec_dai, WM8994_FLL2, WM8994_FLL_SRC_MCLK1,
327 ARIES_MCLK1_FREQ, pll_out);
331 /* Set the codec system clock */
332 ret = snd_soc_dai_set_sysclk(codec_dai, WM8994_SYSCLK_FLL2,
333 pll_out, SND_SOC_CLOCK_IN);
340 static int aries_late_probe(struct snd_soc_card *card)
342 struct aries_wm8994_data *priv = snd_soc_card_get_drvdata(card);
345 ret = snd_soc_card_jack_new_pins(card, "Dock", SND_JACK_LINEOUT,
346 &aries_dock, dock_pins, ARRAY_SIZE(dock_pins));
350 ret = devm_extcon_register_notifier(card->dev,
351 priv->usb_extcon, EXTCON_JACK_LINE_OUT,
352 &aries_extcon_notifier_block);
356 if (extcon_get_state(priv->usb_extcon,
357 EXTCON_JACK_LINE_OUT) > 0)
358 snd_soc_jack_report(&aries_dock, SND_JACK_LINEOUT,
361 snd_soc_jack_report(&aries_dock, 0, SND_JACK_LINEOUT);
363 ret = snd_soc_card_jack_new_pins(card, "Headset",
364 SND_JACK_HEADSET | SND_JACK_BTN_0,
366 jack_pins, ARRAY_SIZE(jack_pins));
370 ret = snd_soc_jack_add_zones(&aries_headset, ARRAY_SIZE(headset_zones),
375 irq = gpiod_to_irq(priv->gpio_headset_detect);
377 dev_err(card->dev, "Failed to map headset detect gpio to irq");
381 ret = devm_request_threaded_irq(card->dev, irq, NULL,
382 headset_det_irq_thread,
383 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
384 IRQF_ONESHOT, "headset_detect", priv);
386 dev_err(card->dev, "Failed to request headset detect irq");
390 headset_button_gpio[0].data = priv;
391 headset_button_gpio[0].desc = priv->gpio_headset_key;
393 snd_jack_set_key(aries_headset.jack, SND_JACK_BTN_0, KEY_MEDIA);
395 return snd_soc_jack_add_gpios(&aries_headset,
396 ARRAY_SIZE(headset_button_gpio), headset_button_gpio);
399 static const struct snd_soc_pcm_stream baseband_params = {
400 .formats = SNDRV_PCM_FMTBIT_S16_LE,
407 static const struct snd_soc_pcm_stream bluetooth_params = {
408 .formats = SNDRV_PCM_FMTBIT_S16_LE,
415 static const struct snd_soc_dapm_widget aries_modem_widgets[] = {
416 SND_SOC_DAPM_INPUT("Modem RX"),
417 SND_SOC_DAPM_OUTPUT("Modem TX"),
420 static const struct snd_soc_dapm_route aries_modem_routes[] = {
421 { "Modem Capture", NULL, "Modem RX" },
422 { "Modem TX", NULL, "Modem Playback" },
425 static const struct snd_soc_component_driver aries_component = {
426 .name = "aries-audio",
427 .dapm_widgets = aries_modem_widgets,
428 .num_dapm_widgets = ARRAY_SIZE(aries_modem_widgets),
429 .dapm_routes = aries_modem_routes,
430 .num_dapm_routes = ARRAY_SIZE(aries_modem_routes),
432 .use_pmdown_time = 1,
436 static struct snd_soc_dai_driver aries_ext_dai[] = {
438 .name = "Voice call",
440 .stream_name = "Modem Playback",
445 .rates = SNDRV_PCM_RATE_8000,
446 .formats = SNDRV_PCM_FMTBIT_S16_LE,
449 .stream_name = "Modem Capture",
454 .rates = SNDRV_PCM_RATE_8000,
455 .formats = SNDRV_PCM_FMTBIT_S16_LE,
460 SND_SOC_DAILINK_DEFS(aif1,
461 DAILINK_COMP_ARRAY(COMP_CPU(SAMSUNG_I2S_DAI)),
462 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "wm8994-aif1")),
463 DAILINK_COMP_ARRAY(COMP_EMPTY()));
465 SND_SOC_DAILINK_DEFS(baseband,
466 DAILINK_COMP_ARRAY(COMP_CPU("Voice call")),
467 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "wm8994-aif2")));
469 SND_SOC_DAILINK_DEFS(bluetooth,
470 DAILINK_COMP_ARRAY(COMP_CPU("bt-sco-pcm")),
471 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "wm8994-aif3")));
473 static struct snd_soc_dai_link aries_dai[] = {
475 .name = "WM8994 AIF1",
476 .stream_name = "HiFi",
477 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
478 SND_SOC_DAIFMT_CBM_CFM,
480 SND_SOC_DAILINK_REG(aif1),
483 .name = "WM8994 AIF2",
484 .stream_name = "Baseband",
485 .init = &aries_baseband_init,
486 .c2c_params = &baseband_params,
489 SND_SOC_DAILINK_REG(baseband),
492 .name = "WM8994 AIF3",
493 .stream_name = "Bluetooth",
494 .c2c_params = &bluetooth_params,
497 SND_SOC_DAILINK_REG(bluetooth),
501 static struct snd_soc_card aries_card = {
503 .owner = THIS_MODULE,
504 .dai_link = aries_dai,
505 .num_links = ARRAY_SIZE(aries_dai),
506 .controls = aries_controls,
507 .num_controls = ARRAY_SIZE(aries_controls),
508 .dapm_widgets = aries_dapm_widgets,
509 .num_dapm_widgets = ARRAY_SIZE(aries_dapm_widgets),
510 .late_probe = aries_late_probe,
513 static const struct aries_wm8994_variant fascinate4g_variant = {
514 .modem_dai_fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_CBS_CFS
515 | SND_SOC_DAIFMT_IB_NF,
516 .has_fm_radio = false,
519 static const struct aries_wm8994_variant aries_variant = {
520 .modem_dai_fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_CBM_CFM
521 | SND_SOC_DAIFMT_IB_NF,
522 .has_fm_radio = true,
525 static const struct of_device_id samsung_wm8994_of_match[] = {
527 .compatible = "samsung,fascinate4g-wm8994",
528 .data = &fascinate4g_variant,
531 .compatible = "samsung,aries-wm8994",
532 .data = &aries_variant,
536 MODULE_DEVICE_TABLE(of, samsung_wm8994_of_match);
538 static int aries_audio_probe(struct platform_device *pdev)
540 struct device_node *np = pdev->dev.of_node;
541 struct device_node *cpu, *codec, *extcon_np;
542 struct device *dev = &pdev->dev;
543 struct snd_soc_card *card = &aries_card;
544 struct aries_wm8994_data *priv;
545 struct snd_soc_dai_link *dai_link;
546 const struct of_device_id *match;
547 enum iio_chan_type channel_type;
555 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
559 snd_soc_card_set_drvdata(card, priv);
561 match = of_match_node(samsung_wm8994_of_match, np);
562 priv->variant = match->data;
564 /* Remove FM widget if not present */
565 if (!priv->variant->has_fm_radio)
566 card->num_dapm_widgets--;
568 priv->reg_main_micbias = devm_regulator_get(dev, "main-micbias");
569 if (IS_ERR(priv->reg_main_micbias)) {
570 dev_err(dev, "Failed to get main micbias regulator\n");
571 return PTR_ERR(priv->reg_main_micbias);
574 priv->reg_headset_micbias = devm_regulator_get(dev, "headset-micbias");
575 if (IS_ERR(priv->reg_headset_micbias)) {
576 dev_err(dev, "Failed to get headset micbias regulator\n");
577 return PTR_ERR(priv->reg_headset_micbias);
580 priv->gpio_earpath_sel = devm_gpiod_get(dev, "earpath-sel",
582 if (IS_ERR(priv->gpio_earpath_sel)) {
583 dev_err(dev, "Failed to get earpath selector gpio");
584 return PTR_ERR(priv->gpio_earpath_sel);
587 extcon_np = of_parse_phandle(np, "extcon", 0);
588 priv->usb_extcon = extcon_find_edev_by_node(extcon_np);
589 of_node_put(extcon_np);
590 if (IS_ERR(priv->usb_extcon))
591 return dev_err_probe(dev, PTR_ERR(priv->usb_extcon),
592 "Failed to get extcon device");
594 priv->adc = devm_iio_channel_get(dev, "headset-detect");
595 if (IS_ERR(priv->adc))
596 return dev_err_probe(dev, PTR_ERR(priv->adc),
597 "Failed to get ADC channel");
599 ret = iio_get_channel_type(priv->adc, &channel_type);
601 return dev_err_probe(dev, ret,
602 "Failed to get ADC channel type");
603 if (channel_type != IIO_VOLTAGE)
606 priv->gpio_headset_key = devm_gpiod_get(dev, "headset-key",
608 if (IS_ERR(priv->gpio_headset_key)) {
609 dev_err(dev, "Failed to get headset key gpio");
610 return PTR_ERR(priv->gpio_headset_key);
613 priv->gpio_headset_detect = devm_gpiod_get(dev,
614 "headset-detect", GPIOD_IN);
615 if (IS_ERR(priv->gpio_headset_detect)) {
616 dev_err(dev, "Failed to get headset detect gpio");
617 return PTR_ERR(priv->gpio_headset_detect);
620 /* Update card-name if provided through DT, else use default name */
621 snd_soc_of_parse_card_name(card, "model");
623 ret = snd_soc_of_parse_audio_routing(card, "audio-routing");
625 /* Backwards compatible way */
626 ret = snd_soc_of_parse_audio_routing(card, "samsung,audio-routing");
628 dev_err(dev, "Audio routing invalid/unspecified\n");
633 aries_dai[1].dai_fmt = priv->variant->modem_dai_fmt;
635 cpu = of_get_child_by_name(dev->of_node, "cpu");
639 codec = of_get_child_by_name(dev->of_node, "codec");
645 for_each_card_prelinks(card, i, dai_link) {
646 dai_link->codecs->of_node = of_parse_phandle(codec,
648 if (!dai_link->codecs->of_node) {
654 /* Set CPU and platform of_node for main DAI */
655 aries_dai[0].cpus->of_node = of_parse_phandle(cpu,
657 if (!aries_dai[0].cpus->of_node) {
662 aries_dai[0].platforms->of_node = aries_dai[0].cpus->of_node;
664 /* Set CPU of_node for BT DAI */
665 aries_dai[2].cpus->of_node = of_parse_phandle(cpu,
667 if (!aries_dai[2].cpus->of_node) {
672 ret = devm_snd_soc_register_component(dev, &aries_component,
673 aries_ext_dai, ARRAY_SIZE(aries_ext_dai));
675 dev_err(dev, "Failed to register component: %d\n", ret);
679 ret = devm_snd_soc_register_card(dev, card);
681 dev_err(dev, "snd_soc_register_card() failed:%d\n", ret);
690 static struct platform_driver aries_audio_driver = {
692 .name = "aries-audio-wm8994",
693 .of_match_table = of_match_ptr(samsung_wm8994_of_match),
694 .pm = &snd_soc_pm_ops,
696 .probe = aries_audio_probe,
699 module_platform_driver(aries_audio_driver);
701 MODULE_DESCRIPTION("ALSA SoC ARIES WM8994");
702 MODULE_LICENSE("GPL");
703 MODULE_ALIAS("platform:aries-audio-wm8994");