19e3ecb5bSAjit Pandey // SPDX-License-Identifier: GPL-2.0-only 29e3ecb5bSAjit Pandey // 39e3ecb5bSAjit Pandey // Copyright (c) 2020, The Linux Foundation. All rights reserved. 49e3ecb5bSAjit Pandey // 59e3ecb5bSAjit Pandey // sc7180.c -- ALSA SoC Machine driver for SC7180 69e3ecb5bSAjit Pandey 79e3ecb5bSAjit Pandey #include <dt-bindings/sound/sc7180-lpass.h> 84531f512SNikita Travkin #include <dt-bindings/sound/qcom,q6afe.h> 93cfbf07cSAjye Huang #include <linux/gpio/consumer.h> 109e3ecb5bSAjit Pandey #include <linux/module.h> 11340d79a1SRob Herring #include <linux/of.h> 129e3ecb5bSAjit Pandey #include <linux/platform_device.h> 139e3ecb5bSAjit Pandey #include <sound/core.h> 149e3ecb5bSAjit Pandey #include <sound/jack.h> 159e3ecb5bSAjit Pandey #include <sound/pcm.h> 169e3ecb5bSAjit Pandey #include <sound/soc.h> 179e3ecb5bSAjit Pandey #include <uapi/linux/input-event-codes.h> 189e3ecb5bSAjit Pandey 199e3ecb5bSAjit Pandey #include "../codecs/rt5682.h" 20425c5fceSlvzhaoxiong #include "../codecs/rt5682s.h" 219e3ecb5bSAjit Pandey #include "common.h" 224531f512SNikita Travkin #include "qdsp6/q6afe.h" 239e3ecb5bSAjit Pandey 249e3ecb5bSAjit Pandey #define DEFAULT_MCLK_RATE 19200000 254531f512SNikita Travkin #define MI2S_BCLK_RATE 1536000 269e3ecb5bSAjit Pandey #define RT5682_PLL1_FREQ (48000 * 512) 279e3ecb5bSAjit Pandey 289e3ecb5bSAjit Pandey #define DRIVER_NAME "SC7180" 299e3ecb5bSAjit Pandey 309e3ecb5bSAjit Pandey struct sc7180_snd_data { 319e3ecb5bSAjit Pandey struct snd_soc_card card; 329e3ecb5bSAjit Pandey u32 pri_mi2s_clk_count; 339e3ecb5bSAjit Pandey struct snd_soc_jack hs_jack; 349e3ecb5bSAjit Pandey struct snd_soc_jack hdmi_jack; 353cfbf07cSAjye Huang struct gpio_desc *dmic_sel; 363cfbf07cSAjye Huang int dmic_switch; 379e3ecb5bSAjit Pandey }; 389e3ecb5bSAjit Pandey 399e3ecb5bSAjit Pandey static void sc7180_jack_free(struct snd_jack *jack) 409e3ecb5bSAjit Pandey { 419e3ecb5bSAjit Pandey struct snd_soc_component *component = jack->private_data; 429e3ecb5bSAjit Pandey 439e3ecb5bSAjit Pandey snd_soc_component_set_jack(component, NULL, NULL); 449e3ecb5bSAjit Pandey } 459e3ecb5bSAjit Pandey 46883bfefcSAlper Nebi Yasak static struct snd_soc_jack_pin sc7180_jack_pins[] = { 47883bfefcSAlper Nebi Yasak { 48883bfefcSAlper Nebi Yasak .pin = "Headphone Jack", 49883bfefcSAlper Nebi Yasak .mask = SND_JACK_HEADPHONE, 50883bfefcSAlper Nebi Yasak }, 51883bfefcSAlper Nebi Yasak { 52883bfefcSAlper Nebi Yasak .pin = "Headset Mic", 53883bfefcSAlper Nebi Yasak .mask = SND_JACK_MICROPHONE, 54883bfefcSAlper Nebi Yasak }, 55883bfefcSAlper Nebi Yasak }; 56883bfefcSAlper Nebi Yasak 579e3ecb5bSAjit Pandey static int sc7180_headset_init(struct snd_soc_pcm_runtime *rtd) 589e3ecb5bSAjit Pandey { 599e3ecb5bSAjit Pandey struct snd_soc_card *card = rtd->card; 609e3ecb5bSAjit Pandey struct sc7180_snd_data *pdata = snd_soc_card_get_drvdata(card); 619b1a2dfaSKuninori Morimoto struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0); 629e3ecb5bSAjit Pandey struct snd_soc_component *component = codec_dai->component; 639e3ecb5bSAjit Pandey struct snd_jack *jack; 649e3ecb5bSAjit Pandey int rval; 659e3ecb5bSAjit Pandey 66883bfefcSAlper Nebi Yasak rval = snd_soc_card_jack_new_pins(card, "Headset Jack", 679e3ecb5bSAjit Pandey SND_JACK_HEADSET | 689e3ecb5bSAjit Pandey SND_JACK_HEADPHONE | 699e3ecb5bSAjit Pandey SND_JACK_BTN_0 | SND_JACK_BTN_1 | 709e3ecb5bSAjit Pandey SND_JACK_BTN_2 | SND_JACK_BTN_3, 71883bfefcSAlper Nebi Yasak &pdata->hs_jack, 72883bfefcSAlper Nebi Yasak sc7180_jack_pins, 73883bfefcSAlper Nebi Yasak ARRAY_SIZE(sc7180_jack_pins)); 749e3ecb5bSAjit Pandey 759e3ecb5bSAjit Pandey if (rval < 0) { 769e3ecb5bSAjit Pandey dev_err(card->dev, "Unable to add Headset Jack\n"); 779e3ecb5bSAjit Pandey return rval; 789e3ecb5bSAjit Pandey } 799e3ecb5bSAjit Pandey 809e3ecb5bSAjit Pandey jack = pdata->hs_jack.jack; 819e3ecb5bSAjit Pandey 829e3ecb5bSAjit Pandey snd_jack_set_key(jack, SND_JACK_BTN_0, KEY_PLAYPAUSE); 839e3ecb5bSAjit Pandey snd_jack_set_key(jack, SND_JACK_BTN_1, KEY_VOICECOMMAND); 849e3ecb5bSAjit Pandey snd_jack_set_key(jack, SND_JACK_BTN_2, KEY_VOLUMEUP); 859e3ecb5bSAjit Pandey snd_jack_set_key(jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN); 869e3ecb5bSAjit Pandey 879e3ecb5bSAjit Pandey jack->private_data = component; 889e3ecb5bSAjit Pandey jack->private_free = sc7180_jack_free; 899e3ecb5bSAjit Pandey 909e3ecb5bSAjit Pandey return snd_soc_component_set_jack(component, &pdata->hs_jack, NULL); 919e3ecb5bSAjit Pandey } 929e3ecb5bSAjit Pandey 939e3ecb5bSAjit Pandey static int sc7180_hdmi_init(struct snd_soc_pcm_runtime *rtd) 949e3ecb5bSAjit Pandey { 959e3ecb5bSAjit Pandey struct snd_soc_card *card = rtd->card; 969e3ecb5bSAjit Pandey struct sc7180_snd_data *pdata = snd_soc_card_get_drvdata(card); 979b1a2dfaSKuninori Morimoto struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0); 989e3ecb5bSAjit Pandey struct snd_soc_component *component = codec_dai->component; 999e3ecb5bSAjit Pandey struct snd_jack *jack; 1009e3ecb5bSAjit Pandey int rval; 1019e3ecb5bSAjit Pandey 1029e3ecb5bSAjit Pandey rval = snd_soc_card_jack_new( 1039e3ecb5bSAjit Pandey card, "HDMI Jack", 1049e3ecb5bSAjit Pandey SND_JACK_LINEOUT, 10519aed2d6SAkihiko Odaki &pdata->hdmi_jack); 1069e3ecb5bSAjit Pandey 1079e3ecb5bSAjit Pandey if (rval < 0) { 1089e3ecb5bSAjit Pandey dev_err(card->dev, "Unable to add HDMI Jack\n"); 1099e3ecb5bSAjit Pandey return rval; 1109e3ecb5bSAjit Pandey } 1119e3ecb5bSAjit Pandey 1129e3ecb5bSAjit Pandey jack = pdata->hdmi_jack.jack; 1139e3ecb5bSAjit Pandey jack->private_data = component; 1149e3ecb5bSAjit Pandey jack->private_free = sc7180_jack_free; 1159e3ecb5bSAjit Pandey 1169e3ecb5bSAjit Pandey return snd_soc_component_set_jack(component, &pdata->hdmi_jack, NULL); 1179e3ecb5bSAjit Pandey } 1189e3ecb5bSAjit Pandey 1199e3ecb5bSAjit Pandey static int sc7180_init(struct snd_soc_pcm_runtime *rtd) 1209e3ecb5bSAjit Pandey { 1219b1a2dfaSKuninori Morimoto struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0); 1229e3ecb5bSAjit Pandey 1239e3ecb5bSAjit Pandey switch (cpu_dai->id) { 1249e3ecb5bSAjit Pandey case MI2S_PRIMARY: 1259e3ecb5bSAjit Pandey return sc7180_headset_init(rtd); 1269e3ecb5bSAjit Pandey case MI2S_SECONDARY: 1279e3ecb5bSAjit Pandey return 0; 1289e3ecb5bSAjit Pandey case LPASS_DP_RX: 1299e3ecb5bSAjit Pandey return sc7180_hdmi_init(rtd); 1309e3ecb5bSAjit Pandey default: 1319e3ecb5bSAjit Pandey dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__, 1329e3ecb5bSAjit Pandey cpu_dai->id); 1339e3ecb5bSAjit Pandey return -EINVAL; 1349e3ecb5bSAjit Pandey } 1359e3ecb5bSAjit Pandey return 0; 1369e3ecb5bSAjit Pandey } 1379e3ecb5bSAjit Pandey 1384531f512SNikita Travkin static int sc7180_qdsp_init(struct snd_soc_pcm_runtime *rtd) 1399e3ecb5bSAjit Pandey { 1409b1a2dfaSKuninori Morimoto struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0); 1414531f512SNikita Travkin 1424531f512SNikita Travkin switch (cpu_dai->id) { 1434531f512SNikita Travkin case PRIMARY_MI2S_RX: 1444531f512SNikita Travkin return sc7180_headset_init(rtd); 1454531f512SNikita Travkin case PRIMARY_MI2S_TX: 1464531f512SNikita Travkin case TERTIARY_MI2S_RX: 1474531f512SNikita Travkin return 0; 1484531f512SNikita Travkin case DISPLAY_PORT_RX: 1494531f512SNikita Travkin return sc7180_hdmi_init(rtd); 1504531f512SNikita Travkin default: 1514531f512SNikita Travkin dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__, 1524531f512SNikita Travkin cpu_dai->id); 1534531f512SNikita Travkin return -EINVAL; 1544531f512SNikita Travkin } 1554531f512SNikita Travkin return 0; 1564531f512SNikita Travkin } 1574531f512SNikita Travkin 1584531f512SNikita Travkin static int sc7180_startup_realtek_codec(struct snd_soc_pcm_runtime *rtd) 1594531f512SNikita Travkin { 1609b1a2dfaSKuninori Morimoto struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0); 161425c5fceSlvzhaoxiong int pll_id, pll_source, pll_in, pll_out, clk_id, ret; 162425c5fceSlvzhaoxiong 163833a94aaSJudy Hsiao if (!strcmp(codec_dai->name, "rt5682-aif1")) { 164425c5fceSlvzhaoxiong pll_source = RT5682_PLL1_S_MCLK; 165425c5fceSlvzhaoxiong pll_id = 0; 166425c5fceSlvzhaoxiong clk_id = RT5682_SCLK_S_PLL1; 167425c5fceSlvzhaoxiong pll_out = RT5682_PLL1_FREQ; 168425c5fceSlvzhaoxiong pll_in = DEFAULT_MCLK_RATE; 169833a94aaSJudy Hsiao } else if (!strcmp(codec_dai->name, "rt5682s-aif1")) { 170425c5fceSlvzhaoxiong pll_source = RT5682S_PLL_S_MCLK; 171425c5fceSlvzhaoxiong pll_id = RT5682S_PLL2; 172425c5fceSlvzhaoxiong clk_id = RT5682S_SCLK_S_PLL2; 173425c5fceSlvzhaoxiong pll_out = RT5682_PLL1_FREQ; 174425c5fceSlvzhaoxiong pll_in = DEFAULT_MCLK_RATE; 1754531f512SNikita Travkin } else { 1764531f512SNikita Travkin return 0; 177425c5fceSlvzhaoxiong } 1789e3ecb5bSAjit Pandey snd_soc_dai_set_fmt(codec_dai, 1791148e16bSCharles Keepax SND_SOC_DAIFMT_BC_FC | 1809e3ecb5bSAjit Pandey SND_SOC_DAIFMT_NB_NF | 1819e3ecb5bSAjit Pandey SND_SOC_DAIFMT_I2S); 1829e3ecb5bSAjit Pandey 1839e3ecb5bSAjit Pandey /* Configure PLL1 for codec */ 184425c5fceSlvzhaoxiong ret = snd_soc_dai_set_pll(codec_dai, pll_id, pll_source, 185425c5fceSlvzhaoxiong pll_in, pll_out); 1869e3ecb5bSAjit Pandey if (ret) { 1879e3ecb5bSAjit Pandey dev_err(rtd->dev, "can't set codec pll: %d\n", ret); 1889e3ecb5bSAjit Pandey return ret; 1899e3ecb5bSAjit Pandey } 1909e3ecb5bSAjit Pandey 1919e3ecb5bSAjit Pandey /* Configure sysclk for codec */ 192425c5fceSlvzhaoxiong ret = snd_soc_dai_set_sysclk(codec_dai, clk_id, pll_out, 1939e3ecb5bSAjit Pandey SND_SOC_CLOCK_IN); 1949e3ecb5bSAjit Pandey if (ret) 1959e3ecb5bSAjit Pandey dev_err(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n", 1969e3ecb5bSAjit Pandey ret); 1979e3ecb5bSAjit Pandey 1984531f512SNikita Travkin return ret; 1994531f512SNikita Travkin } 2004531f512SNikita Travkin 2014531f512SNikita Travkin static int sc7180_snd_startup(struct snd_pcm_substream *substream) 2024531f512SNikita Travkin { 203*77678a25SKrzysztof Kozlowski struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 2044531f512SNikita Travkin struct snd_soc_card *card = rtd->card; 2054531f512SNikita Travkin struct sc7180_snd_data *data = snd_soc_card_get_drvdata(card); 2064531f512SNikita Travkin struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0); 2074531f512SNikita Travkin int ret; 2084531f512SNikita Travkin 2094531f512SNikita Travkin switch (cpu_dai->id) { 2104531f512SNikita Travkin case MI2S_PRIMARY: 2114531f512SNikita Travkin if (++data->pri_mi2s_clk_count == 1) { 2124531f512SNikita Travkin snd_soc_dai_set_sysclk(cpu_dai, 2134531f512SNikita Travkin LPASS_MCLK0, 2144531f512SNikita Travkin DEFAULT_MCLK_RATE, 2154531f512SNikita Travkin SNDRV_PCM_STREAM_PLAYBACK); 2164531f512SNikita Travkin } 2174531f512SNikita Travkin 2184531f512SNikita Travkin ret = sc7180_startup_realtek_codec(rtd); 2194531f512SNikita Travkin if (ret) 2204531f512SNikita Travkin return ret; 2214531f512SNikita Travkin 2229e3ecb5bSAjit Pandey break; 2239e3ecb5bSAjit Pandey case MI2S_SECONDARY: 2249e3ecb5bSAjit Pandey break; 2259e3ecb5bSAjit Pandey case LPASS_DP_RX: 2269e3ecb5bSAjit Pandey break; 2279e3ecb5bSAjit Pandey default: 2289e3ecb5bSAjit Pandey dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__, 2299e3ecb5bSAjit Pandey cpu_dai->id); 2309e3ecb5bSAjit Pandey return -EINVAL; 2319e3ecb5bSAjit Pandey } 2329e3ecb5bSAjit Pandey return 0; 2339e3ecb5bSAjit Pandey } 2349e3ecb5bSAjit Pandey 2354531f512SNikita Travkin static int sc7180_qdsp_snd_startup(struct snd_pcm_substream *substream) 2364531f512SNikita Travkin { 237*77678a25SKrzysztof Kozlowski struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 2384531f512SNikita Travkin struct snd_soc_card *card = rtd->card; 2394531f512SNikita Travkin struct sc7180_snd_data *data = snd_soc_card_get_drvdata(card); 2404531f512SNikita Travkin struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0); 2414531f512SNikita Travkin struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0); 2424531f512SNikita Travkin int ret; 2434531f512SNikita Travkin 2444531f512SNikita Travkin switch (cpu_dai->id) { 2454531f512SNikita Travkin case PRIMARY_MI2S_RX: 2464531f512SNikita Travkin case PRIMARY_MI2S_TX: 2474531f512SNikita Travkin if (++data->pri_mi2s_clk_count == 1) { 2484531f512SNikita Travkin snd_soc_dai_set_sysclk(cpu_dai, 2494531f512SNikita Travkin Q6AFE_LPASS_CLK_ID_MCLK_1, 2504531f512SNikita Travkin DEFAULT_MCLK_RATE, 2514531f512SNikita Travkin SNDRV_PCM_STREAM_PLAYBACK); 2524531f512SNikita Travkin snd_soc_dai_set_sysclk(cpu_dai, 2534531f512SNikita Travkin Q6AFE_LPASS_CLK_ID_PRI_MI2S_IBIT, 2544531f512SNikita Travkin MI2S_BCLK_RATE, 2554531f512SNikita Travkin SNDRV_PCM_STREAM_PLAYBACK); 2564531f512SNikita Travkin } 2574531f512SNikita Travkin 2584531f512SNikita Travkin snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_BP_FP); 2594531f512SNikita Travkin 2604531f512SNikita Travkin ret = sc7180_startup_realtek_codec(rtd); 2614531f512SNikita Travkin if (ret) 2624531f512SNikita Travkin return ret; 2634531f512SNikita Travkin 2644531f512SNikita Travkin break; 2654531f512SNikita Travkin case TERTIARY_MI2S_RX: 2664531f512SNikita Travkin snd_soc_dai_set_sysclk(cpu_dai, 2674531f512SNikita Travkin Q6AFE_LPASS_CLK_ID_TER_MI2S_IBIT, 2684531f512SNikita Travkin MI2S_BCLK_RATE, 2694531f512SNikita Travkin SNDRV_PCM_STREAM_PLAYBACK); 2704531f512SNikita Travkin 2714531f512SNikita Travkin snd_soc_dai_set_fmt(codec_dai, 2724531f512SNikita Travkin SND_SOC_DAIFMT_BC_FC | 2734531f512SNikita Travkin SND_SOC_DAIFMT_NB_NF | 2744531f512SNikita Travkin SND_SOC_DAIFMT_I2S); 2754531f512SNikita Travkin snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_BP_FP); 2764531f512SNikita Travkin break; 2774531f512SNikita Travkin case DISPLAY_PORT_RX: 2784531f512SNikita Travkin break; 2794531f512SNikita Travkin default: 2804531f512SNikita Travkin dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__, 2814531f512SNikita Travkin cpu_dai->id); 2824531f512SNikita Travkin return -EINVAL; 2834531f512SNikita Travkin } 2844531f512SNikita Travkin return 0; 2854531f512SNikita Travkin } 2864531f512SNikita Travkin 2873cfbf07cSAjye Huang static int dmic_get(struct snd_kcontrol *kcontrol, 2883cfbf07cSAjye Huang struct snd_ctl_elem_value *ucontrol) 2893cfbf07cSAjye Huang { 2903cfbf07cSAjye Huang struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol); 2913cfbf07cSAjye Huang struct sc7180_snd_data *data = snd_soc_card_get_drvdata(dapm->card); 2923cfbf07cSAjye Huang 2933cfbf07cSAjye Huang ucontrol->value.integer.value[0] = data->dmic_switch; 2943cfbf07cSAjye Huang return 0; 2953cfbf07cSAjye Huang } 2963cfbf07cSAjye Huang 2973cfbf07cSAjye Huang static int dmic_set(struct snd_kcontrol *kcontrol, 2983cfbf07cSAjye Huang struct snd_ctl_elem_value *ucontrol) 2993cfbf07cSAjye Huang { 3003cfbf07cSAjye Huang struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol); 3013cfbf07cSAjye Huang struct sc7180_snd_data *data = snd_soc_card_get_drvdata(dapm->card); 3023cfbf07cSAjye Huang 3033cfbf07cSAjye Huang data->dmic_switch = ucontrol->value.integer.value[0]; 3043cfbf07cSAjye Huang gpiod_set_value(data->dmic_sel, data->dmic_switch); 3053cfbf07cSAjye Huang return 0; 3063cfbf07cSAjye Huang } 3073cfbf07cSAjye Huang 3089e3ecb5bSAjit Pandey static void sc7180_snd_shutdown(struct snd_pcm_substream *substream) 3099e3ecb5bSAjit Pandey { 310*77678a25SKrzysztof Kozlowski struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 3119e3ecb5bSAjit Pandey struct snd_soc_card *card = rtd->card; 3129e3ecb5bSAjit Pandey struct sc7180_snd_data *data = snd_soc_card_get_drvdata(card); 3139b1a2dfaSKuninori Morimoto struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0); 3149e3ecb5bSAjit Pandey 3159e3ecb5bSAjit Pandey switch (cpu_dai->id) { 3169e3ecb5bSAjit Pandey case MI2S_PRIMARY: 3179e3ecb5bSAjit Pandey if (--data->pri_mi2s_clk_count == 0) { 3189e3ecb5bSAjit Pandey snd_soc_dai_set_sysclk(cpu_dai, 3199e3ecb5bSAjit Pandey LPASS_MCLK0, 3209e3ecb5bSAjit Pandey 0, 3219e3ecb5bSAjit Pandey SNDRV_PCM_STREAM_PLAYBACK); 3229e3ecb5bSAjit Pandey } 3239e3ecb5bSAjit Pandey break; 3249e3ecb5bSAjit Pandey case MI2S_SECONDARY: 3259e3ecb5bSAjit Pandey break; 3269e3ecb5bSAjit Pandey case LPASS_DP_RX: 3279e3ecb5bSAjit Pandey break; 3289e3ecb5bSAjit Pandey default: 3299e3ecb5bSAjit Pandey dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__, 3309e3ecb5bSAjit Pandey cpu_dai->id); 3319e3ecb5bSAjit Pandey break; 3329e3ecb5bSAjit Pandey } 3339e3ecb5bSAjit Pandey } 3349e3ecb5bSAjit Pandey 3354531f512SNikita Travkin static void sc7180_qdsp_snd_shutdown(struct snd_pcm_substream *substream) 3364531f512SNikita Travkin { 337*77678a25SKrzysztof Kozlowski struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 3384531f512SNikita Travkin struct snd_soc_card *card = rtd->card; 3394531f512SNikita Travkin struct sc7180_snd_data *data = snd_soc_card_get_drvdata(card); 3404531f512SNikita Travkin struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0); 3414531f512SNikita Travkin 3424531f512SNikita Travkin switch (cpu_dai->id) { 3434531f512SNikita Travkin case PRIMARY_MI2S_RX: 3444531f512SNikita Travkin case PRIMARY_MI2S_TX: 3454531f512SNikita Travkin if (--data->pri_mi2s_clk_count == 0) { 3464531f512SNikita Travkin snd_soc_dai_set_sysclk(cpu_dai, 3474531f512SNikita Travkin Q6AFE_LPASS_CLK_ID_MCLK_1, 3484531f512SNikita Travkin 0, 3494531f512SNikita Travkin SNDRV_PCM_STREAM_PLAYBACK); 3504531f512SNikita Travkin snd_soc_dai_set_sysclk(cpu_dai, 3514531f512SNikita Travkin Q6AFE_LPASS_CLK_ID_PRI_MI2S_IBIT, 3524531f512SNikita Travkin 0, 3534531f512SNikita Travkin SNDRV_PCM_STREAM_PLAYBACK); 3544531f512SNikita Travkin } 3554531f512SNikita Travkin break; 3564531f512SNikita Travkin case TERTIARY_MI2S_RX: 3574531f512SNikita Travkin snd_soc_dai_set_sysclk(cpu_dai, 3584531f512SNikita Travkin Q6AFE_LPASS_CLK_ID_TER_MI2S_IBIT, 3594531f512SNikita Travkin 0, 3604531f512SNikita Travkin SNDRV_PCM_STREAM_PLAYBACK); 3614531f512SNikita Travkin break; 3624531f512SNikita Travkin case DISPLAY_PORT_RX: 3634531f512SNikita Travkin break; 3644531f512SNikita Travkin default: 3654531f512SNikita Travkin dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__, 3664531f512SNikita Travkin cpu_dai->id); 3674531f512SNikita Travkin break; 3684531f512SNikita Travkin } 3694531f512SNikita Travkin } 3704531f512SNikita Travkin 371e936619bSxuyuqing static int sc7180_adau7002_init(struct snd_soc_pcm_runtime *rtd) 372e936619bSxuyuqing { 3739b1a2dfaSKuninori Morimoto struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0); 374e936619bSxuyuqing 375e936619bSxuyuqing switch (cpu_dai->id) { 376e936619bSxuyuqing case MI2S_PRIMARY: 377e936619bSxuyuqing return 0; 378e936619bSxuyuqing case MI2S_SECONDARY: 379e936619bSxuyuqing return 0; 380e936619bSxuyuqing case LPASS_DP_RX: 381e936619bSxuyuqing return sc7180_hdmi_init(rtd); 382e936619bSxuyuqing default: 383e936619bSxuyuqing dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__, 384e936619bSxuyuqing cpu_dai->id); 385e936619bSxuyuqing return -EINVAL; 386e936619bSxuyuqing } 387e936619bSxuyuqing return 0; 388e936619bSxuyuqing } 389e936619bSxuyuqing 390e936619bSxuyuqing static int sc7180_adau7002_snd_startup(struct snd_pcm_substream *substream) 391e936619bSxuyuqing { 392*77678a25SKrzysztof Kozlowski struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 3939b1a2dfaSKuninori Morimoto struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0); 3949b1a2dfaSKuninori Morimoto struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0); 3957f2c63d6Sxuyuqing struct snd_pcm_runtime *runtime = substream->runtime; 396e936619bSxuyuqing 397e936619bSxuyuqing switch (cpu_dai->id) { 398e936619bSxuyuqing case MI2S_PRIMARY: 399e936619bSxuyuqing snd_soc_dai_set_fmt(codec_dai, 400e936619bSxuyuqing SND_SOC_DAIFMT_CBS_CFS | 401e936619bSxuyuqing SND_SOC_DAIFMT_NB_NF | 402e936619bSxuyuqing SND_SOC_DAIFMT_I2S); 4037f2c63d6Sxuyuqing runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE; 4047f2c63d6Sxuyuqing snd_pcm_hw_constraint_msbits(runtime, 0, 32, 32); 405e936619bSxuyuqing 406e936619bSxuyuqing break; 407e936619bSxuyuqing case MI2S_SECONDARY: 408e936619bSxuyuqing break; 409e936619bSxuyuqing case LPASS_DP_RX: 410e936619bSxuyuqing break; 411e936619bSxuyuqing default: 412e936619bSxuyuqing dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__, 413e936619bSxuyuqing cpu_dai->id); 414e936619bSxuyuqing return -EINVAL; 415e936619bSxuyuqing } 416e936619bSxuyuqing return 0; 417e936619bSxuyuqing } 418e936619bSxuyuqing 4194531f512SNikita Travkin static int sc7180_qdsp_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd, 4204531f512SNikita Travkin struct snd_pcm_hw_params *params) 4214531f512SNikita Travkin { 4224531f512SNikita Travkin struct snd_interval *rate = hw_param_interval(params, 4234531f512SNikita Travkin SNDRV_PCM_HW_PARAM_RATE); 4244531f512SNikita Travkin struct snd_interval *channels = hw_param_interval(params, 4254531f512SNikita Travkin SNDRV_PCM_HW_PARAM_CHANNELS); 4264531f512SNikita Travkin 4274531f512SNikita Travkin rate->min = rate->max = 48000; 4284531f512SNikita Travkin channels->min = channels->max = 2; 4294531f512SNikita Travkin 4304531f512SNikita Travkin return 0; 4314531f512SNikita Travkin } 4324531f512SNikita Travkin 4339e3ecb5bSAjit Pandey static const struct snd_soc_ops sc7180_ops = { 4349e3ecb5bSAjit Pandey .startup = sc7180_snd_startup, 4359e3ecb5bSAjit Pandey .shutdown = sc7180_snd_shutdown, 4369e3ecb5bSAjit Pandey }; 4379e3ecb5bSAjit Pandey 4384531f512SNikita Travkin static const struct snd_soc_ops sc7180_qdsp_ops = { 4394531f512SNikita Travkin .startup = sc7180_qdsp_snd_startup, 4404531f512SNikita Travkin .shutdown = sc7180_qdsp_snd_shutdown, 4414531f512SNikita Travkin }; 4424531f512SNikita Travkin 443e936619bSxuyuqing static const struct snd_soc_ops sc7180_adau7002_ops = { 444e936619bSxuyuqing .startup = sc7180_adau7002_snd_startup, 445e936619bSxuyuqing }; 446e936619bSxuyuqing 4479e3ecb5bSAjit Pandey static const struct snd_soc_dapm_widget sc7180_snd_widgets[] = { 4489e3ecb5bSAjit Pandey SND_SOC_DAPM_HP("Headphone Jack", NULL), 4499e3ecb5bSAjit Pandey SND_SOC_DAPM_MIC("Headset Mic", NULL), 4509e3ecb5bSAjit Pandey }; 4519e3ecb5bSAjit Pandey 452883bfefcSAlper Nebi Yasak static const struct snd_kcontrol_new sc7180_snd_controls[] = { 453883bfefcSAlper Nebi Yasak SOC_DAPM_PIN_SWITCH("Headphone Jack"), 454883bfefcSAlper Nebi Yasak SOC_DAPM_PIN_SWITCH("Headset Mic"), 455883bfefcSAlper Nebi Yasak }; 456883bfefcSAlper Nebi Yasak 457e936619bSxuyuqing static const struct snd_soc_dapm_widget sc7180_adau7002_snd_widgets[] = { 458e936619bSxuyuqing SND_SOC_DAPM_MIC("DMIC", NULL), 459e936619bSxuyuqing }; 460e936619bSxuyuqing 4613cfbf07cSAjye Huang static const char * const dmic_mux_text[] = { 4623cfbf07cSAjye Huang "Front Mic", 4633cfbf07cSAjye Huang "Rear Mic", 4643cfbf07cSAjye Huang }; 4653cfbf07cSAjye Huang 4663cfbf07cSAjye Huang static SOC_ENUM_SINGLE_DECL(sc7180_dmic_enum, 4673cfbf07cSAjye Huang SND_SOC_NOPM, 0, dmic_mux_text); 4683cfbf07cSAjye Huang 4693cfbf07cSAjye Huang static const struct snd_kcontrol_new sc7180_dmic_mux_control = 4703cfbf07cSAjye Huang SOC_DAPM_ENUM_EXT("DMIC Select Mux", sc7180_dmic_enum, 4713cfbf07cSAjye Huang dmic_get, dmic_set); 4723cfbf07cSAjye Huang 4733cfbf07cSAjye Huang static const struct snd_soc_dapm_widget sc7180_snd_dual_mic_widgets[] = { 4743cfbf07cSAjye Huang SND_SOC_DAPM_HP("Headphone Jack", NULL), 4753cfbf07cSAjye Huang SND_SOC_DAPM_MIC("Headset Mic", NULL), 4763cfbf07cSAjye Huang SND_SOC_DAPM_MIC("DMIC", NULL), 4773cfbf07cSAjye Huang SND_SOC_DAPM_MUX("Dmic Mux", SND_SOC_NOPM, 0, 0, &sc7180_dmic_mux_control), 4783cfbf07cSAjye Huang }; 4793cfbf07cSAjye Huang 480883bfefcSAlper Nebi Yasak static const struct snd_kcontrol_new sc7180_snd_dual_mic_controls[] = { 481883bfefcSAlper Nebi Yasak SOC_DAPM_PIN_SWITCH("Headphone Jack"), 482883bfefcSAlper Nebi Yasak SOC_DAPM_PIN_SWITCH("Headset Mic"), 483883bfefcSAlper Nebi Yasak }; 484883bfefcSAlper Nebi Yasak 4853cfbf07cSAjye Huang static const struct snd_soc_dapm_route sc7180_snd_dual_mic_audio_route[] = { 4863cfbf07cSAjye Huang {"Dmic Mux", "Front Mic", "DMIC"}, 4873cfbf07cSAjye Huang {"Dmic Mux", "Rear Mic", "DMIC"}, 4883cfbf07cSAjye Huang }; 4893cfbf07cSAjye Huang 4909e3ecb5bSAjit Pandey static int sc7180_snd_platform_probe(struct platform_device *pdev) 4919e3ecb5bSAjit Pandey { 4929e3ecb5bSAjit Pandey struct snd_soc_card *card; 4939e3ecb5bSAjit Pandey struct sc7180_snd_data *data; 4949e3ecb5bSAjit Pandey struct device *dev = &pdev->dev; 495e936619bSxuyuqing struct snd_soc_dai_link *link; 4969e3ecb5bSAjit Pandey int ret; 497e936619bSxuyuqing int i; 4984531f512SNikita Travkin bool qdsp = false, no_headphone = false; 4999e3ecb5bSAjit Pandey 5009e3ecb5bSAjit Pandey /* Allocate the private data */ 5019e3ecb5bSAjit Pandey data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 5029e3ecb5bSAjit Pandey if (!data) 5039e3ecb5bSAjit Pandey return -ENOMEM; 5049e3ecb5bSAjit Pandey 5059e3ecb5bSAjit Pandey card = &data->card; 5069e3ecb5bSAjit Pandey snd_soc_card_set_drvdata(card, data); 5079e3ecb5bSAjit Pandey 5089e3ecb5bSAjit Pandey card->owner = THIS_MODULE; 5099e3ecb5bSAjit Pandey card->driver_name = DRIVER_NAME; 5109e3ecb5bSAjit Pandey card->dev = dev; 5119e3ecb5bSAjit Pandey card->dapm_widgets = sc7180_snd_widgets; 5129e3ecb5bSAjit Pandey card->num_dapm_widgets = ARRAY_SIZE(sc7180_snd_widgets); 513883bfefcSAlper Nebi Yasak card->controls = sc7180_snd_controls; 514883bfefcSAlper Nebi Yasak card->num_controls = ARRAY_SIZE(sc7180_snd_controls); 5159e3ecb5bSAjit Pandey 5163cfbf07cSAjye Huang if (of_property_read_bool(dev->of_node, "dmic-gpios")) { 5173cfbf07cSAjye Huang card->dapm_widgets = sc7180_snd_dual_mic_widgets, 5183cfbf07cSAjye Huang card->num_dapm_widgets = ARRAY_SIZE(sc7180_snd_dual_mic_widgets), 519883bfefcSAlper Nebi Yasak card->controls = sc7180_snd_dual_mic_controls, 520883bfefcSAlper Nebi Yasak card->num_controls = ARRAY_SIZE(sc7180_snd_dual_mic_controls), 5213cfbf07cSAjye Huang card->dapm_routes = sc7180_snd_dual_mic_audio_route, 5223cfbf07cSAjye Huang card->num_dapm_routes = ARRAY_SIZE(sc7180_snd_dual_mic_audio_route), 5233cfbf07cSAjye Huang data->dmic_sel = devm_gpiod_get(&pdev->dev, "dmic", GPIOD_OUT_LOW); 5243cfbf07cSAjye Huang if (IS_ERR(data->dmic_sel)) { 5253cfbf07cSAjye Huang dev_err(&pdev->dev, "DMIC gpio failed err=%ld\n", PTR_ERR(data->dmic_sel)); 5263cfbf07cSAjye Huang return PTR_ERR(data->dmic_sel); 5273cfbf07cSAjye Huang } 5283cfbf07cSAjye Huang } 5293cfbf07cSAjye Huang 530e936619bSxuyuqing if (of_device_is_compatible(dev->of_node, "google,sc7180-coachz")) { 531e936619bSxuyuqing no_headphone = true; 532e936619bSxuyuqing card->dapm_widgets = sc7180_adau7002_snd_widgets; 533e936619bSxuyuqing card->num_dapm_widgets = ARRAY_SIZE(sc7180_adau7002_snd_widgets); 5344531f512SNikita Travkin } else if (of_device_is_compatible(dev->of_node, "qcom,sc7180-qdsp6-sndcard")) { 5354531f512SNikita Travkin qdsp = true; 536e936619bSxuyuqing } 537e936619bSxuyuqing 5389e3ecb5bSAjit Pandey ret = qcom_snd_parse_of(card); 5399e3ecb5bSAjit Pandey if (ret) 5409e3ecb5bSAjit Pandey return ret; 5419e3ecb5bSAjit Pandey 542e936619bSxuyuqing for_each_card_prelinks(card, i, link) { 543e936619bSxuyuqing if (no_headphone) { 544e936619bSxuyuqing link->ops = &sc7180_adau7002_ops; 545e936619bSxuyuqing link->init = sc7180_adau7002_init; 5464531f512SNikita Travkin } else if (qdsp) { 5474531f512SNikita Travkin if (link->no_pcm == 1) { 5484531f512SNikita Travkin link->ops = &sc7180_qdsp_ops; 5494531f512SNikita Travkin link->be_hw_params_fixup = sc7180_qdsp_be_hw_params_fixup; 5504531f512SNikita Travkin link->init = sc7180_qdsp_init; 5514531f512SNikita Travkin } 552e936619bSxuyuqing } else { 553e936619bSxuyuqing link->ops = &sc7180_ops; 554e936619bSxuyuqing link->init = sc7180_init; 555e936619bSxuyuqing } 556e936619bSxuyuqing } 5579e3ecb5bSAjit Pandey 5589e3ecb5bSAjit Pandey return devm_snd_soc_register_card(dev, card); 5599e3ecb5bSAjit Pandey } 5609e3ecb5bSAjit Pandey 5619e3ecb5bSAjit Pandey static const struct of_device_id sc7180_snd_device_id[] = { 5629e3ecb5bSAjit Pandey {.compatible = "google,sc7180-trogdor"}, 563e936619bSxuyuqing {.compatible = "google,sc7180-coachz"}, 5644531f512SNikita Travkin {.compatible = "qcom,sc7180-qdsp6-sndcard"}, 5659e3ecb5bSAjit Pandey {}, 5669e3ecb5bSAjit Pandey }; 5679e3ecb5bSAjit Pandey MODULE_DEVICE_TABLE(of, sc7180_snd_device_id); 5689e3ecb5bSAjit Pandey 5699e3ecb5bSAjit Pandey static struct platform_driver sc7180_snd_driver = { 5709e3ecb5bSAjit Pandey .probe = sc7180_snd_platform_probe, 5719e3ecb5bSAjit Pandey .driver = { 5729e3ecb5bSAjit Pandey .name = "msm-snd-sc7180", 5739e3ecb5bSAjit Pandey .of_match_table = sc7180_snd_device_id, 574b2fc3029SCheng-Yi Chiang .pm = &snd_soc_pm_ops, 5759e3ecb5bSAjit Pandey }, 5769e3ecb5bSAjit Pandey }; 5779e3ecb5bSAjit Pandey module_platform_driver(sc7180_snd_driver); 5789e3ecb5bSAjit Pandey 5799e3ecb5bSAjit Pandey MODULE_DESCRIPTION("sc7180 ASoC Machine Driver"); 580bb339245SKrzysztof Kozlowski MODULE_LICENSE("GPL"); 581