16b1687bfSRohit kumar // SPDX-License-Identifier: GPL-2.0
26b1687bfSRohit kumar /*
36b1687bfSRohit kumar * Copyright (c) 2018, The Linux Foundation. All rights reserved.
46b1687bfSRohit kumar */
56b1687bfSRohit kumar
6528a4a0bSKrzysztof Kozlowski #include <dt-bindings/sound/qcom,q6afe.h>
76b1687bfSRohit kumar #include <linux/module.h>
86b1687bfSRohit kumar #include <linux/platform_device.h>
909b38200SCheng-Yi Chiang #include <sound/core.h>
106b1687bfSRohit kumar #include <sound/pcm.h>
116b1687bfSRohit kumar #include <sound/pcm_params.h>
1280e733fdSCheng-Yi Chiang #include <sound/jack.h>
1309b38200SCheng-Yi Chiang #include <sound/soc.h>
141b93a884SSrinivas Kandagatla #include <linux/soundwire/sdw.h>
1580e733fdSCheng-Yi Chiang #include <uapi/linux/input-event-codes.h>
166b1687bfSRohit kumar #include "common.h"
176b1687bfSRohit kumar #include "qdsp6/q6afe.h"
18*d0e806b0SAlexey Klimov #include "sdw.h"
19ce55698dSCheng-Yi Chiang #include "../codecs/rt5663.h"
206b1687bfSRohit kumar
213f48b6ebSSrinivas Kandagatla #define DRIVER_NAME "sdm845"
226b1687bfSRohit kumar #define DEFAULT_SAMPLE_RATE_48K 48000
236b1687bfSRohit kumar #define DEFAULT_MCLK_RATE 24576000
24a1849b7dSRohit kumar #define TDM_BCLK_RATE 6144000
25a1849b7dSRohit kumar #define MI2S_BCLK_RATE 1536000
2609b38200SCheng-Yi Chiang #define LEFT_SPK_TDM_TX_MASK 0x30
2709b38200SCheng-Yi Chiang #define RIGHT_SPK_TDM_TX_MASK 0xC0
2809b38200SCheng-Yi Chiang #define SPK_TDM_RX_MASK 0x03
2909b38200SCheng-Yi Chiang #define NUM_TDM_SLOTS 8
305caf64c6SSrinivas Kandagatla #define SLIM_MAX_TX_PORTS 16
314800fe6eSSrinivas Kandagatla #define SLIM_MAX_RX_PORTS 13
325caf64c6SSrinivas Kandagatla #define WCD934X_DEFAULT_MCLK_RATE 9600000
336b1687bfSRohit kumar
346b1687bfSRohit kumar struct sdm845_snd_data {
3580e733fdSCheng-Yi Chiang struct snd_soc_jack jack;
3680e733fdSCheng-Yi Chiang bool jack_setup;
375f55c969SSrinivas Kandagatla bool slim_port_setup;
381c668e1cSSrinivas Kandagatla bool stream_prepared[AFE_PORT_MAX];
396b1687bfSRohit kumar struct snd_soc_card *card;
406b1687bfSRohit kumar uint32_t pri_mi2s_clk_count;
41992e75b3SRohit kumar uint32_t sec_mi2s_clk_count;
426b1687bfSRohit kumar uint32_t quat_tdm_clk_count;
431c668e1cSSrinivas Kandagatla struct sdw_stream_runtime *sruntime[AFE_PORT_MAX];
446b1687bfSRohit kumar };
456b1687bfSRohit kumar
46242372d6SAlper Nebi Yasak static struct snd_soc_jack_pin sdm845_jack_pins[] = {
47242372d6SAlper Nebi Yasak {
48242372d6SAlper Nebi Yasak .pin = "Headphone Jack",
49242372d6SAlper Nebi Yasak .mask = SND_JACK_HEADPHONE,
50242372d6SAlper Nebi Yasak },
51242372d6SAlper Nebi Yasak {
52242372d6SAlper Nebi Yasak .pin = "Headset Mic",
53242372d6SAlper Nebi Yasak .mask = SND_JACK_MICROPHONE,
54242372d6SAlper Nebi Yasak },
55242372d6SAlper Nebi Yasak };
56242372d6SAlper Nebi Yasak
576b1687bfSRohit kumar static unsigned int tdm_slot_offset[8] = {0, 4, 8, 12, 16, 20, 24, 28};
586b1687bfSRohit kumar
sdm845_slim_snd_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)595caf64c6SSrinivas Kandagatla static int sdm845_slim_snd_hw_params(struct snd_pcm_substream *substream,
605caf64c6SSrinivas Kandagatla struct snd_pcm_hw_params *params)
615caf64c6SSrinivas Kandagatla {
629b1a2dfaSKuninori Morimoto struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
639b1a2dfaSKuninori Morimoto struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
64a4eb41eeSKuninori Morimoto struct snd_soc_dai *codec_dai;
651b93a884SSrinivas Kandagatla struct sdm845_snd_data *pdata = snd_soc_card_get_drvdata(rtd->card);
665caf64c6SSrinivas Kandagatla u32 rx_ch[SLIM_MAX_RX_PORTS], tx_ch[SLIM_MAX_TX_PORTS];
671b93a884SSrinivas Kandagatla struct sdw_stream_runtime *sruntime;
685caf64c6SSrinivas Kandagatla u32 rx_ch_cnt = 0, tx_ch_cnt = 0;
695caf64c6SSrinivas Kandagatla int ret = 0, i;
705caf64c6SSrinivas Kandagatla
71c998ee30SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, codec_dai) {
72e8444560SPierre-Louis Bossart sruntime = snd_soc_dai_get_stream(codec_dai,
731b93a884SSrinivas Kandagatla substream->stream);
741b93a884SSrinivas Kandagatla if (sruntime != ERR_PTR(-ENOTSUPP))
751b93a884SSrinivas Kandagatla pdata->sruntime[cpu_dai->id] = sruntime;
761b93a884SSrinivas Kandagatla
77a4eb41eeSKuninori Morimoto ret = snd_soc_dai_get_channel_map(codec_dai,
785caf64c6SSrinivas Kandagatla &tx_ch_cnt, tx_ch, &rx_ch_cnt, rx_ch);
795caf64c6SSrinivas Kandagatla
805caf64c6SSrinivas Kandagatla if (ret != 0 && ret != -ENOTSUPP) {
815caf64c6SSrinivas Kandagatla pr_err("failed to get codec chan map, err:%d\n", ret);
825caf64c6SSrinivas Kandagatla return ret;
835caf64c6SSrinivas Kandagatla } else if (ret == -ENOTSUPP) {
845caf64c6SSrinivas Kandagatla /* Ignore unsupported */
855caf64c6SSrinivas Kandagatla continue;
865caf64c6SSrinivas Kandagatla }
875caf64c6SSrinivas Kandagatla
885caf64c6SSrinivas Kandagatla if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
895caf64c6SSrinivas Kandagatla ret = snd_soc_dai_set_channel_map(cpu_dai, 0, NULL,
905caf64c6SSrinivas Kandagatla rx_ch_cnt, rx_ch);
915caf64c6SSrinivas Kandagatla else
925caf64c6SSrinivas Kandagatla ret = snd_soc_dai_set_channel_map(cpu_dai, tx_ch_cnt,
935caf64c6SSrinivas Kandagatla tx_ch, 0, NULL);
945caf64c6SSrinivas Kandagatla }
955caf64c6SSrinivas Kandagatla
965caf64c6SSrinivas Kandagatla return 0;
975caf64c6SSrinivas Kandagatla }
985caf64c6SSrinivas Kandagatla
sdm845_tdm_snd_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)996b1687bfSRohit kumar static int sdm845_tdm_snd_hw_params(struct snd_pcm_substream *substream,
1006b1687bfSRohit kumar struct snd_pcm_hw_params *params)
1016b1687bfSRohit kumar {
1029b1a2dfaSKuninori Morimoto struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
1039b1a2dfaSKuninori Morimoto struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
104a4eb41eeSKuninori Morimoto struct snd_soc_dai *codec_dai;
10509b38200SCheng-Yi Chiang int ret = 0, j;
1066b1687bfSRohit kumar int channels, slot_width;
1076b1687bfSRohit kumar
1086b1687bfSRohit kumar switch (params_format(params)) {
1096b1687bfSRohit kumar case SNDRV_PCM_FORMAT_S16_LE:
110a1849b7dSRohit kumar slot_width = 16;
1116b1687bfSRohit kumar break;
1126b1687bfSRohit kumar default:
1136b1687bfSRohit kumar dev_err(rtd->dev, "%s: invalid param format 0x%x\n",
1146b1687bfSRohit kumar __func__, params_format(params));
1156b1687bfSRohit kumar return -EINVAL;
1166b1687bfSRohit kumar }
1176b1687bfSRohit kumar
1186b1687bfSRohit kumar channels = params_channels(params);
1196b1687bfSRohit kumar if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1206b1687bfSRohit kumar ret = snd_soc_dai_set_tdm_slot(cpu_dai, 0, 0x3,
1216b1687bfSRohit kumar 8, slot_width);
1226b1687bfSRohit kumar if (ret < 0) {
1236b1687bfSRohit kumar dev_err(rtd->dev, "%s: failed to set tdm slot, err:%d\n",
1246b1687bfSRohit kumar __func__, ret);
1256b1687bfSRohit kumar goto end;
1266b1687bfSRohit kumar }
1276b1687bfSRohit kumar
1286b1687bfSRohit kumar ret = snd_soc_dai_set_channel_map(cpu_dai, 0, NULL,
1296b1687bfSRohit kumar channels, tdm_slot_offset);
1306b1687bfSRohit kumar if (ret < 0) {
1316b1687bfSRohit kumar dev_err(rtd->dev, "%s: failed to set channel map, err:%d\n",
1326b1687bfSRohit kumar __func__, ret);
1336b1687bfSRohit kumar goto end;
1346b1687bfSRohit kumar }
1356b1687bfSRohit kumar } else {
1366b1687bfSRohit kumar ret = snd_soc_dai_set_tdm_slot(cpu_dai, 0xf, 0,
1376b1687bfSRohit kumar 8, slot_width);
1386b1687bfSRohit kumar if (ret < 0) {
1396b1687bfSRohit kumar dev_err(rtd->dev, "%s: failed to set tdm slot, err:%d\n",
1406b1687bfSRohit kumar __func__, ret);
1416b1687bfSRohit kumar goto end;
1426b1687bfSRohit kumar }
1436b1687bfSRohit kumar
1446b1687bfSRohit kumar ret = snd_soc_dai_set_channel_map(cpu_dai, channels,
1456b1687bfSRohit kumar tdm_slot_offset, 0, NULL);
1466b1687bfSRohit kumar if (ret < 0) {
1476b1687bfSRohit kumar dev_err(rtd->dev, "%s: failed to set channel map, err:%d\n",
1486b1687bfSRohit kumar __func__, ret);
1496b1687bfSRohit kumar goto end;
1506b1687bfSRohit kumar }
1516b1687bfSRohit kumar }
15209b38200SCheng-Yi Chiang
153c998ee30SKuninori Morimoto for_each_rtd_codec_dais(rtd, j, codec_dai) {
15409b38200SCheng-Yi Chiang
15509b38200SCheng-Yi Chiang if (!strcmp(codec_dai->component->name_prefix, "Left")) {
15609b38200SCheng-Yi Chiang ret = snd_soc_dai_set_tdm_slot(
15709b38200SCheng-Yi Chiang codec_dai, LEFT_SPK_TDM_TX_MASK,
15809b38200SCheng-Yi Chiang SPK_TDM_RX_MASK, NUM_TDM_SLOTS,
15909b38200SCheng-Yi Chiang slot_width);
16009b38200SCheng-Yi Chiang if (ret < 0) {
16109b38200SCheng-Yi Chiang dev_err(rtd->dev,
16209b38200SCheng-Yi Chiang "DEV0 TDM slot err:%d\n", ret);
16309b38200SCheng-Yi Chiang return ret;
16409b38200SCheng-Yi Chiang }
16509b38200SCheng-Yi Chiang }
16609b38200SCheng-Yi Chiang
16709b38200SCheng-Yi Chiang if (!strcmp(codec_dai->component->name_prefix, "Right")) {
16809b38200SCheng-Yi Chiang ret = snd_soc_dai_set_tdm_slot(
16909b38200SCheng-Yi Chiang codec_dai, RIGHT_SPK_TDM_TX_MASK,
17009b38200SCheng-Yi Chiang SPK_TDM_RX_MASK, NUM_TDM_SLOTS,
17109b38200SCheng-Yi Chiang slot_width);
17209b38200SCheng-Yi Chiang if (ret < 0) {
17309b38200SCheng-Yi Chiang dev_err(rtd->dev,
17409b38200SCheng-Yi Chiang "DEV1 TDM slot err:%d\n", ret);
17509b38200SCheng-Yi Chiang return ret;
17609b38200SCheng-Yi Chiang }
17709b38200SCheng-Yi Chiang }
17809b38200SCheng-Yi Chiang }
17909b38200SCheng-Yi Chiang
1806b1687bfSRohit kumar end:
1816b1687bfSRohit kumar return ret;
1826b1687bfSRohit kumar }
1836b1687bfSRohit kumar
sdm845_snd_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)1846b1687bfSRohit kumar static int sdm845_snd_hw_params(struct snd_pcm_substream *substream,
1856b1687bfSRohit kumar struct snd_pcm_hw_params *params)
1866b1687bfSRohit kumar {
1879b1a2dfaSKuninori Morimoto struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
1889b1a2dfaSKuninori Morimoto struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
1899b1a2dfaSKuninori Morimoto struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
1906b1687bfSRohit kumar int ret = 0;
1916b1687bfSRohit kumar
1926b1687bfSRohit kumar switch (cpu_dai->id) {
193ce55698dSCheng-Yi Chiang case PRIMARY_MI2S_RX:
194ce55698dSCheng-Yi Chiang case PRIMARY_MI2S_TX:
195ce55698dSCheng-Yi Chiang /*
196ce55698dSCheng-Yi Chiang * Use ASRC for internal clocks, as PLL rate isn't multiple
197ce55698dSCheng-Yi Chiang * of BCLK.
198ce55698dSCheng-Yi Chiang */
199ce55698dSCheng-Yi Chiang rt5663_sel_asrc_clk_src(
200ce55698dSCheng-Yi Chiang codec_dai->component,
201ce55698dSCheng-Yi Chiang RT5663_DA_STEREO_FILTER | RT5663_AD_STEREO_FILTER,
202ce55698dSCheng-Yi Chiang RT5663_CLK_SEL_I2S1_ASRC);
203ce55698dSCheng-Yi Chiang ret = snd_soc_dai_set_sysclk(
204ce55698dSCheng-Yi Chiang codec_dai, RT5663_SCLK_S_MCLK, DEFAULT_MCLK_RATE,
205ce55698dSCheng-Yi Chiang SND_SOC_CLOCK_IN);
206ce55698dSCheng-Yi Chiang if (ret < 0)
207ce55698dSCheng-Yi Chiang dev_err(rtd->dev,
208ce55698dSCheng-Yi Chiang "snd_soc_dai_set_sysclk err = %d\n", ret);
209ce55698dSCheng-Yi Chiang break;
2106b1687bfSRohit kumar case QUATERNARY_TDM_RX_0:
2116b1687bfSRohit kumar case QUATERNARY_TDM_TX_0:
2126b1687bfSRohit kumar ret = sdm845_tdm_snd_hw_params(substream, params);
2136b1687bfSRohit kumar break;
2145caf64c6SSrinivas Kandagatla case SLIMBUS_0_RX...SLIMBUS_6_TX:
2155caf64c6SSrinivas Kandagatla ret = sdm845_slim_snd_hw_params(substream, params);
2165caf64c6SSrinivas Kandagatla break;
2175caf64c6SSrinivas Kandagatla case QUATERNARY_MI2S_RX:
2185caf64c6SSrinivas Kandagatla break;
2196b1687bfSRohit kumar default:
2206b1687bfSRohit kumar pr_err("%s: invalid dai id 0x%x\n", __func__, cpu_dai->id);
2216b1687bfSRohit kumar break;
2226b1687bfSRohit kumar }
2236b1687bfSRohit kumar return ret;
2246b1687bfSRohit kumar }
2256b1687bfSRohit kumar
sdm845_jack_free(struct snd_jack * jack)2262998369fSRohit kumar static void sdm845_jack_free(struct snd_jack *jack)
2272998369fSRohit kumar {
2282998369fSRohit kumar struct snd_soc_component *component = jack->private_data;
2292998369fSRohit kumar
2302998369fSRohit kumar snd_soc_component_set_jack(component, NULL, NULL);
2312998369fSRohit kumar }
2322998369fSRohit kumar
sdm845_dai_init(struct snd_soc_pcm_runtime * rtd)23380e733fdSCheng-Yi Chiang static int sdm845_dai_init(struct snd_soc_pcm_runtime *rtd)
23480e733fdSCheng-Yi Chiang {
23580e733fdSCheng-Yi Chiang struct snd_soc_component *component;
23680e733fdSCheng-Yi Chiang struct snd_soc_card *card = rtd->card;
2379b1a2dfaSKuninori Morimoto struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
2389b1a2dfaSKuninori Morimoto struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
23980e733fdSCheng-Yi Chiang struct sdm845_snd_data *pdata = snd_soc_card_get_drvdata(card);
2405f55c969SSrinivas Kandagatla struct snd_soc_dai_link *link = rtd->dai_link;
2412998369fSRohit kumar struct snd_jack *jack;
2425caf64c6SSrinivas Kandagatla /*
2435caf64c6SSrinivas Kandagatla * Codec SLIMBUS configuration
2445caf64c6SSrinivas Kandagatla * RX1, RX2, RX3, RX4, RX5, RX6, RX7, RX8, RX9, RX10, RX11, RX12, RX13
2455caf64c6SSrinivas Kandagatla * TX1, TX2, TX3, TX4, TX5, TX6, TX7, TX8, TX9, TX10, TX11, TX12, TX13
2465caf64c6SSrinivas Kandagatla * TX14, TX15, TX16
2475caf64c6SSrinivas Kandagatla */
2485caf64c6SSrinivas Kandagatla unsigned int rx_ch[SLIM_MAX_RX_PORTS] = {144, 145, 146, 147, 148, 149,
2495caf64c6SSrinivas Kandagatla 150, 151, 152, 153, 154, 155, 156};
2505caf64c6SSrinivas Kandagatla unsigned int tx_ch[SLIM_MAX_TX_PORTS] = {128, 129, 130, 131, 132, 133,
2515caf64c6SSrinivas Kandagatla 134, 135, 136, 137, 138, 139,
2525caf64c6SSrinivas Kandagatla 140, 141, 142, 143};
2535caf64c6SSrinivas Kandagatla int rval, i;
2545caf64c6SSrinivas Kandagatla
25580e733fdSCheng-Yi Chiang
25680e733fdSCheng-Yi Chiang if (!pdata->jack_setup) {
257242372d6SAlper Nebi Yasak rval = snd_soc_card_jack_new_pins(card, "Headset Jack",
25880e733fdSCheng-Yi Chiang SND_JACK_HEADSET |
25980e733fdSCheng-Yi Chiang SND_JACK_HEADPHONE |
26080e733fdSCheng-Yi Chiang SND_JACK_BTN_0 | SND_JACK_BTN_1 |
26180e733fdSCheng-Yi Chiang SND_JACK_BTN_2 | SND_JACK_BTN_3,
262242372d6SAlper Nebi Yasak &pdata->jack,
263242372d6SAlper Nebi Yasak sdm845_jack_pins,
264242372d6SAlper Nebi Yasak ARRAY_SIZE(sdm845_jack_pins));
26580e733fdSCheng-Yi Chiang
26680e733fdSCheng-Yi Chiang if (rval < 0) {
26780e733fdSCheng-Yi Chiang dev_err(card->dev, "Unable to add Headphone Jack\n");
26880e733fdSCheng-Yi Chiang return rval;
26980e733fdSCheng-Yi Chiang }
27080e733fdSCheng-Yi Chiang
27180e733fdSCheng-Yi Chiang jack = pdata->jack.jack;
27280e733fdSCheng-Yi Chiang
27380e733fdSCheng-Yi Chiang snd_jack_set_key(jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
27480e733fdSCheng-Yi Chiang snd_jack_set_key(jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
27580e733fdSCheng-Yi Chiang snd_jack_set_key(jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
27680e733fdSCheng-Yi Chiang snd_jack_set_key(jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
27780e733fdSCheng-Yi Chiang pdata->jack_setup = true;
27880e733fdSCheng-Yi Chiang }
27980e733fdSCheng-Yi Chiang
2802998369fSRohit kumar switch (cpu_dai->id) {
2812998369fSRohit kumar case PRIMARY_MI2S_RX:
2822998369fSRohit kumar jack = pdata->jack.jack;
2832998369fSRohit kumar component = codec_dai->component;
28480e733fdSCheng-Yi Chiang
2852998369fSRohit kumar jack->private_data = component;
2862998369fSRohit kumar jack->private_free = sdm845_jack_free;
2872998369fSRohit kumar rval = snd_soc_component_set_jack(component,
2882998369fSRohit kumar &pdata->jack, NULL);
28980e733fdSCheng-Yi Chiang if (rval != 0 && rval != -ENOTSUPP) {
29080e733fdSCheng-Yi Chiang dev_warn(card->dev, "Failed to set jack: %d\n", rval);
29180e733fdSCheng-Yi Chiang return rval;
29280e733fdSCheng-Yi Chiang }
2932998369fSRohit kumar break;
2945caf64c6SSrinivas Kandagatla case SLIMBUS_0_RX...SLIMBUS_6_TX:
2955f55c969SSrinivas Kandagatla /* setting up wcd multiple times for slim port is redundant */
2965f55c969SSrinivas Kandagatla if (pdata->slim_port_setup || !link->no_pcm)
2975f55c969SSrinivas Kandagatla return 0;
2985f55c969SSrinivas Kandagatla
299c998ee30SKuninori Morimoto for_each_rtd_codec_dais(rtd, i, codec_dai) {
300a4eb41eeSKuninori Morimoto rval = snd_soc_dai_set_channel_map(codec_dai,
3015caf64c6SSrinivas Kandagatla ARRAY_SIZE(tx_ch),
3025caf64c6SSrinivas Kandagatla tx_ch,
3035caf64c6SSrinivas Kandagatla ARRAY_SIZE(rx_ch),
3045caf64c6SSrinivas Kandagatla rx_ch);
3055caf64c6SSrinivas Kandagatla if (rval != 0 && rval != -ENOTSUPP)
3065caf64c6SSrinivas Kandagatla return rval;
3075caf64c6SSrinivas Kandagatla
308a4eb41eeSKuninori Morimoto snd_soc_dai_set_sysclk(codec_dai, 0,
3095caf64c6SSrinivas Kandagatla WCD934X_DEFAULT_MCLK_RATE,
3105caf64c6SSrinivas Kandagatla SNDRV_PCM_STREAM_PLAYBACK);
311c15d4b72SSrinivas Kandagatla
312c15d4b72SSrinivas Kandagatla rval = snd_soc_component_set_jack(codec_dai->component,
313c15d4b72SSrinivas Kandagatla &pdata->jack, NULL);
314c15d4b72SSrinivas Kandagatla if (rval != 0 && rval != -ENOTSUPP) {
315c15d4b72SSrinivas Kandagatla dev_warn(card->dev, "Failed to set jack: %d\n", rval);
316c15d4b72SSrinivas Kandagatla return rval;
317c15d4b72SSrinivas Kandagatla }
3185caf64c6SSrinivas Kandagatla }
3195f55c969SSrinivas Kandagatla
3205f55c969SSrinivas Kandagatla pdata->slim_port_setup = true;
3215f55c969SSrinivas Kandagatla
3225caf64c6SSrinivas Kandagatla break;
3232998369fSRohit kumar default:
3242998369fSRohit kumar break;
32580e733fdSCheng-Yi Chiang }
32680e733fdSCheng-Yi Chiang
32780e733fdSCheng-Yi Chiang return 0;
32880e733fdSCheng-Yi Chiang }
32980e733fdSCheng-Yi Chiang
33080e733fdSCheng-Yi Chiang
sdm845_snd_startup(struct snd_pcm_substream * substream)3316b1687bfSRohit kumar static int sdm845_snd_startup(struct snd_pcm_substream *substream)
3326b1687bfSRohit kumar {
3331148e16bSCharles Keepax unsigned int fmt = SND_SOC_DAIFMT_BP_FP;
3341148e16bSCharles Keepax unsigned int codec_dai_fmt = SND_SOC_DAIFMT_BC_FC;
3359b1a2dfaSKuninori Morimoto struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
3366b1687bfSRohit kumar struct snd_soc_card *card = rtd->card;
3376b1687bfSRohit kumar struct sdm845_snd_data *data = snd_soc_card_get_drvdata(card);
3389b1a2dfaSKuninori Morimoto struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
3399b1a2dfaSKuninori Morimoto struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
34009b38200SCheng-Yi Chiang int j;
34109b38200SCheng-Yi Chiang int ret;
3426b1687bfSRohit kumar
3436b1687bfSRohit kumar switch (cpu_dai->id) {
3446b1687bfSRohit kumar case PRIMARY_MI2S_RX:
3456b1687bfSRohit kumar case PRIMARY_MI2S_TX:
346ce55698dSCheng-Yi Chiang codec_dai_fmt |= SND_SOC_DAIFMT_NB_NF;
3476b1687bfSRohit kumar if (++(data->pri_mi2s_clk_count) == 1) {
3486b1687bfSRohit kumar snd_soc_dai_set_sysclk(cpu_dai,
3496b1687bfSRohit kumar Q6AFE_LPASS_CLK_ID_MCLK_1,
3506b1687bfSRohit kumar DEFAULT_MCLK_RATE, SNDRV_PCM_STREAM_PLAYBACK);
3516b1687bfSRohit kumar snd_soc_dai_set_sysclk(cpu_dai,
3526b1687bfSRohit kumar Q6AFE_LPASS_CLK_ID_PRI_MI2S_IBIT,
353a1849b7dSRohit kumar MI2S_BCLK_RATE, SNDRV_PCM_STREAM_PLAYBACK);
3546b1687bfSRohit kumar }
3556b1687bfSRohit kumar snd_soc_dai_set_fmt(cpu_dai, fmt);
356ce55698dSCheng-Yi Chiang snd_soc_dai_set_fmt(codec_dai, codec_dai_fmt);
3576b1687bfSRohit kumar break;
3586b1687bfSRohit kumar
359992e75b3SRohit kumar case SECONDARY_MI2S_TX:
3609019ab10SCheng-Yi Chiang codec_dai_fmt |= SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_I2S;
361992e75b3SRohit kumar if (++(data->sec_mi2s_clk_count) == 1) {
362992e75b3SRohit kumar snd_soc_dai_set_sysclk(cpu_dai,
363992e75b3SRohit kumar Q6AFE_LPASS_CLK_ID_SEC_MI2S_IBIT,
364992e75b3SRohit kumar MI2S_BCLK_RATE, SNDRV_PCM_STREAM_CAPTURE);
365992e75b3SRohit kumar }
366992e75b3SRohit kumar snd_soc_dai_set_fmt(cpu_dai, fmt);
3679019ab10SCheng-Yi Chiang snd_soc_dai_set_fmt(codec_dai, codec_dai_fmt);
368992e75b3SRohit kumar break;
3695caf64c6SSrinivas Kandagatla case QUATERNARY_MI2S_RX:
3705caf64c6SSrinivas Kandagatla snd_soc_dai_set_sysclk(cpu_dai,
3715caf64c6SSrinivas Kandagatla Q6AFE_LPASS_CLK_ID_QUAD_MI2S_IBIT,
3725caf64c6SSrinivas Kandagatla MI2S_BCLK_RATE, SNDRV_PCM_STREAM_PLAYBACK);
3731148e16bSCharles Keepax snd_soc_dai_set_fmt(cpu_dai, fmt);
3745caf64c6SSrinivas Kandagatla
3755caf64c6SSrinivas Kandagatla
3765caf64c6SSrinivas Kandagatla break;
377992e75b3SRohit kumar
3786b1687bfSRohit kumar case QUATERNARY_TDM_RX_0:
3796b1687bfSRohit kumar case QUATERNARY_TDM_TX_0:
3806b1687bfSRohit kumar if (++(data->quat_tdm_clk_count) == 1) {
3816b1687bfSRohit kumar snd_soc_dai_set_sysclk(cpu_dai,
3826b1687bfSRohit kumar Q6AFE_LPASS_CLK_ID_QUAD_TDM_IBIT,
383a1849b7dSRohit kumar TDM_BCLK_RATE, SNDRV_PCM_STREAM_PLAYBACK);
3846b1687bfSRohit kumar }
38509b38200SCheng-Yi Chiang
38609b38200SCheng-Yi Chiang codec_dai_fmt |= SND_SOC_DAIFMT_IB_NF | SND_SOC_DAIFMT_DSP_B;
38709b38200SCheng-Yi Chiang
388c998ee30SKuninori Morimoto for_each_rtd_codec_dais(rtd, j, codec_dai) {
38909b38200SCheng-Yi Chiang
39009b38200SCheng-Yi Chiang if (!strcmp(codec_dai->component->name_prefix,
39109b38200SCheng-Yi Chiang "Left")) {
39209b38200SCheng-Yi Chiang ret = snd_soc_dai_set_fmt(
39309b38200SCheng-Yi Chiang codec_dai, codec_dai_fmt);
39409b38200SCheng-Yi Chiang if (ret < 0) {
39509b38200SCheng-Yi Chiang dev_err(rtd->dev,
39609b38200SCheng-Yi Chiang "Left TDM fmt err:%d\n", ret);
39709b38200SCheng-Yi Chiang return ret;
39809b38200SCheng-Yi Chiang }
39909b38200SCheng-Yi Chiang }
40009b38200SCheng-Yi Chiang
40109b38200SCheng-Yi Chiang if (!strcmp(codec_dai->component->name_prefix,
40209b38200SCheng-Yi Chiang "Right")) {
40309b38200SCheng-Yi Chiang ret = snd_soc_dai_set_fmt(
40409b38200SCheng-Yi Chiang codec_dai, codec_dai_fmt);
40509b38200SCheng-Yi Chiang if (ret < 0) {
40609b38200SCheng-Yi Chiang dev_err(rtd->dev,
40709b38200SCheng-Yi Chiang "Right TDM slot err:%d\n", ret);
40809b38200SCheng-Yi Chiang return ret;
40909b38200SCheng-Yi Chiang }
41009b38200SCheng-Yi Chiang }
41109b38200SCheng-Yi Chiang }
4126b1687bfSRohit kumar break;
4135caf64c6SSrinivas Kandagatla case SLIMBUS_0_RX...SLIMBUS_6_TX:
4145caf64c6SSrinivas Kandagatla break;
4156b1687bfSRohit kumar
4166b1687bfSRohit kumar default:
4176b1687bfSRohit kumar pr_err("%s: invalid dai id 0x%x\n", __func__, cpu_dai->id);
4186b1687bfSRohit kumar break;
4196b1687bfSRohit kumar }
420*d0e806b0SAlexey Klimov return qcom_snd_sdw_startup(substream);
4216b1687bfSRohit kumar }
4226b1687bfSRohit kumar
sdm845_snd_shutdown(struct snd_pcm_substream * substream)4236b1687bfSRohit kumar static void sdm845_snd_shutdown(struct snd_pcm_substream *substream)
4246b1687bfSRohit kumar {
4259b1a2dfaSKuninori Morimoto struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
4266b1687bfSRohit kumar struct snd_soc_card *card = rtd->card;
4276b1687bfSRohit kumar struct sdm845_snd_data *data = snd_soc_card_get_drvdata(card);
4289b1a2dfaSKuninori Morimoto struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
429*d0e806b0SAlexey Klimov struct sdw_stream_runtime *sruntime = data->sruntime[cpu_dai->id];
4306b1687bfSRohit kumar
4316b1687bfSRohit kumar switch (cpu_dai->id) {
4326b1687bfSRohit kumar case PRIMARY_MI2S_RX:
4336b1687bfSRohit kumar case PRIMARY_MI2S_TX:
4346b1687bfSRohit kumar if (--(data->pri_mi2s_clk_count) == 0) {
4356b1687bfSRohit kumar snd_soc_dai_set_sysclk(cpu_dai,
4366b1687bfSRohit kumar Q6AFE_LPASS_CLK_ID_MCLK_1,
4376b1687bfSRohit kumar 0, SNDRV_PCM_STREAM_PLAYBACK);
4386b1687bfSRohit kumar snd_soc_dai_set_sysclk(cpu_dai,
4396b1687bfSRohit kumar Q6AFE_LPASS_CLK_ID_PRI_MI2S_IBIT,
4406b1687bfSRohit kumar 0, SNDRV_PCM_STREAM_PLAYBACK);
441fca11622SSaiyam Doshi }
4426b1687bfSRohit kumar break;
4436b1687bfSRohit kumar
444992e75b3SRohit kumar case SECONDARY_MI2S_TX:
445992e75b3SRohit kumar if (--(data->sec_mi2s_clk_count) == 0) {
446992e75b3SRohit kumar snd_soc_dai_set_sysclk(cpu_dai,
447992e75b3SRohit kumar Q6AFE_LPASS_CLK_ID_SEC_MI2S_IBIT,
448992e75b3SRohit kumar 0, SNDRV_PCM_STREAM_CAPTURE);
449992e75b3SRohit kumar }
450992e75b3SRohit kumar break;
451992e75b3SRohit kumar
4526b1687bfSRohit kumar case QUATERNARY_TDM_RX_0:
4536b1687bfSRohit kumar case QUATERNARY_TDM_TX_0:
4546b1687bfSRohit kumar if (--(data->quat_tdm_clk_count) == 0) {
4556b1687bfSRohit kumar snd_soc_dai_set_sysclk(cpu_dai,
4566b1687bfSRohit kumar Q6AFE_LPASS_CLK_ID_QUAD_TDM_IBIT,
4576b1687bfSRohit kumar 0, SNDRV_PCM_STREAM_PLAYBACK);
4586b1687bfSRohit kumar }
4596b1687bfSRohit kumar break;
4605caf64c6SSrinivas Kandagatla case SLIMBUS_0_RX...SLIMBUS_6_TX:
4615caf64c6SSrinivas Kandagatla case QUATERNARY_MI2S_RX:
4625caf64c6SSrinivas Kandagatla break;
4636b1687bfSRohit kumar
4646b1687bfSRohit kumar default:
4656b1687bfSRohit kumar pr_err("%s: invalid dai id 0x%x\n", __func__, cpu_dai->id);
4666b1687bfSRohit kumar break;
4676b1687bfSRohit kumar }
468*d0e806b0SAlexey Klimov
469*d0e806b0SAlexey Klimov data->sruntime[cpu_dai->id] = NULL;
470*d0e806b0SAlexey Klimov sdw_release_stream(sruntime);
4716b1687bfSRohit kumar }
4726b1687bfSRohit kumar
sdm845_snd_prepare(struct snd_pcm_substream * substream)4731b93a884SSrinivas Kandagatla static int sdm845_snd_prepare(struct snd_pcm_substream *substream)
4741b93a884SSrinivas Kandagatla {
4759b1a2dfaSKuninori Morimoto struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
4761b93a884SSrinivas Kandagatla struct sdm845_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
4779b1a2dfaSKuninori Morimoto struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
4781b93a884SSrinivas Kandagatla struct sdw_stream_runtime *sruntime = data->sruntime[cpu_dai->id];
4791b93a884SSrinivas Kandagatla int ret;
4801b93a884SSrinivas Kandagatla
4811b93a884SSrinivas Kandagatla if (!sruntime)
4821b93a884SSrinivas Kandagatla return 0;
4831b93a884SSrinivas Kandagatla
4841b93a884SSrinivas Kandagatla if (data->stream_prepared[cpu_dai->id]) {
4851b93a884SSrinivas Kandagatla sdw_disable_stream(sruntime);
4861b93a884SSrinivas Kandagatla sdw_deprepare_stream(sruntime);
4871b93a884SSrinivas Kandagatla data->stream_prepared[cpu_dai->id] = false;
4881b93a884SSrinivas Kandagatla }
4891b93a884SSrinivas Kandagatla
4901b93a884SSrinivas Kandagatla ret = sdw_prepare_stream(sruntime);
4911b93a884SSrinivas Kandagatla if (ret)
4921b93a884SSrinivas Kandagatla return ret;
4931b93a884SSrinivas Kandagatla
4941b93a884SSrinivas Kandagatla /**
4951b93a884SSrinivas Kandagatla * NOTE: there is a strict hw requirement about the ordering of port
4961b93a884SSrinivas Kandagatla * enables and actual WSA881x PA enable. PA enable should only happen
4971b93a884SSrinivas Kandagatla * after soundwire ports are enabled if not DC on the line is
4981b93a884SSrinivas Kandagatla * accumulated resulting in Click/Pop Noise
4991b93a884SSrinivas Kandagatla * PA enable/mute are handled as part of codec DAPM and digital mute.
5001b93a884SSrinivas Kandagatla */
5011b93a884SSrinivas Kandagatla
5021b93a884SSrinivas Kandagatla ret = sdw_enable_stream(sruntime);
5031b93a884SSrinivas Kandagatla if (ret) {
5041b93a884SSrinivas Kandagatla sdw_deprepare_stream(sruntime);
5051b93a884SSrinivas Kandagatla return ret;
5061b93a884SSrinivas Kandagatla }
5071b93a884SSrinivas Kandagatla data->stream_prepared[cpu_dai->id] = true;
5081b93a884SSrinivas Kandagatla
5091b93a884SSrinivas Kandagatla return ret;
5101b93a884SSrinivas Kandagatla }
5111b93a884SSrinivas Kandagatla
sdm845_snd_hw_free(struct snd_pcm_substream * substream)5121b93a884SSrinivas Kandagatla static int sdm845_snd_hw_free(struct snd_pcm_substream *substream)
5131b93a884SSrinivas Kandagatla {
5149b1a2dfaSKuninori Morimoto struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
5151b93a884SSrinivas Kandagatla struct sdm845_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
5169b1a2dfaSKuninori Morimoto struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
5171b93a884SSrinivas Kandagatla struct sdw_stream_runtime *sruntime = data->sruntime[cpu_dai->id];
5181b93a884SSrinivas Kandagatla
5191b93a884SSrinivas Kandagatla if (sruntime && data->stream_prepared[cpu_dai->id]) {
5201b93a884SSrinivas Kandagatla sdw_disable_stream(sruntime);
5211b93a884SSrinivas Kandagatla sdw_deprepare_stream(sruntime);
5221b93a884SSrinivas Kandagatla data->stream_prepared[cpu_dai->id] = false;
5231b93a884SSrinivas Kandagatla }
5241b93a884SSrinivas Kandagatla
5251b93a884SSrinivas Kandagatla return 0;
5261b93a884SSrinivas Kandagatla }
5271b93a884SSrinivas Kandagatla
528ff60005eSJulia Lawall static const struct snd_soc_ops sdm845_be_ops = {
5296b1687bfSRohit kumar .hw_params = sdm845_snd_hw_params,
5301b93a884SSrinivas Kandagatla .hw_free = sdm845_snd_hw_free,
5311b93a884SSrinivas Kandagatla .prepare = sdm845_snd_prepare,
5326b1687bfSRohit kumar .startup = sdm845_snd_startup,
5336b1687bfSRohit kumar .shutdown = sdm845_snd_shutdown,
5346b1687bfSRohit kumar };
5356b1687bfSRohit kumar
sdm845_be_hw_params_fixup(struct snd_soc_pcm_runtime * rtd,struct snd_pcm_hw_params * params)5366b1687bfSRohit kumar static int sdm845_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
5376b1687bfSRohit kumar struct snd_pcm_hw_params *params)
5386b1687bfSRohit kumar {
5396b1687bfSRohit kumar struct snd_interval *rate = hw_param_interval(params,
5406b1687bfSRohit kumar SNDRV_PCM_HW_PARAM_RATE);
5416b1687bfSRohit kumar struct snd_interval *channels = hw_param_interval(params,
5426b1687bfSRohit kumar SNDRV_PCM_HW_PARAM_CHANNELS);
5436b1687bfSRohit kumar struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
5446b1687bfSRohit kumar
5456b1687bfSRohit kumar rate->min = rate->max = DEFAULT_SAMPLE_RATE_48K;
5466b1687bfSRohit kumar channels->min = channels->max = 2;
5476b1687bfSRohit kumar snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
5486b1687bfSRohit kumar
5496b1687bfSRohit kumar return 0;
5506b1687bfSRohit kumar }
5516b1687bfSRohit kumar
5527f1806ebSCheng-Yi Chiang static const struct snd_soc_dapm_widget sdm845_snd_widgets[] = {
5537f1806ebSCheng-Yi Chiang SND_SOC_DAPM_HP("Headphone Jack", NULL),
5547f1806ebSCheng-Yi Chiang SND_SOC_DAPM_MIC("Headset Mic", NULL),
5557f1806ebSCheng-Yi Chiang SND_SOC_DAPM_SPK("Left Spk", NULL),
5567f1806ebSCheng-Yi Chiang SND_SOC_DAPM_SPK("Right Spk", NULL),
5577f1806ebSCheng-Yi Chiang SND_SOC_DAPM_MIC("Int Mic", NULL),
5587f1806ebSCheng-Yi Chiang };
5597f1806ebSCheng-Yi Chiang
560242372d6SAlper Nebi Yasak static const struct snd_kcontrol_new sdm845_snd_controls[] = {
561242372d6SAlper Nebi Yasak SOC_DAPM_PIN_SWITCH("Headphone Jack"),
562242372d6SAlper Nebi Yasak SOC_DAPM_PIN_SWITCH("Headset Mic"),
563242372d6SAlper Nebi Yasak };
564242372d6SAlper Nebi Yasak
sdm845_add_ops(struct snd_soc_card * card)56580e733fdSCheng-Yi Chiang static void sdm845_add_ops(struct snd_soc_card *card)
5666b1687bfSRohit kumar {
5677fe072b4SKuninori Morimoto struct snd_soc_dai_link *link;
5687fe072b4SKuninori Morimoto int i;
5696b1687bfSRohit kumar
5707fe072b4SKuninori Morimoto for_each_card_prelinks(card, i, link) {
5716b1687bfSRohit kumar if (link->no_pcm == 1) {
5726b1687bfSRohit kumar link->ops = &sdm845_be_ops;
5736b1687bfSRohit kumar link->be_hw_params_fixup = sdm845_be_hw_params_fixup;
5746b1687bfSRohit kumar }
57580e733fdSCheng-Yi Chiang link->init = sdm845_dai_init;
5766b1687bfSRohit kumar }
5776b1687bfSRohit kumar }
5786b1687bfSRohit kumar
sdm845_snd_platform_probe(struct platform_device * pdev)5796b1687bfSRohit kumar static int sdm845_snd_platform_probe(struct platform_device *pdev)
5806b1687bfSRohit kumar {
5816b1687bfSRohit kumar struct snd_soc_card *card;
5826b1687bfSRohit kumar struct sdm845_snd_data *data;
5836b1687bfSRohit kumar struct device *dev = &pdev->dev;
5846b1687bfSRohit kumar int ret;
5856b1687bfSRohit kumar
586ed3b53e7SStephan Gerhold card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
5876b1687bfSRohit kumar if (!card)
5886b1687bfSRohit kumar return -ENOMEM;
5896b1687bfSRohit kumar
5906b1687bfSRohit kumar /* Allocate the private data */
591ed3b53e7SStephan Gerhold data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
592ed3b53e7SStephan Gerhold if (!data)
593ed3b53e7SStephan Gerhold return -ENOMEM;
5946b1687bfSRohit kumar
5953f48b6ebSSrinivas Kandagatla card->driver_name = DRIVER_NAME;
5967f1806ebSCheng-Yi Chiang card->dapm_widgets = sdm845_snd_widgets;
5977f1806ebSCheng-Yi Chiang card->num_dapm_widgets = ARRAY_SIZE(sdm845_snd_widgets);
598242372d6SAlper Nebi Yasak card->controls = sdm845_snd_controls;
599242372d6SAlper Nebi Yasak card->num_controls = ARRAY_SIZE(sdm845_snd_controls);
6006b1687bfSRohit kumar card->dev = dev;
6013c27ea23SStephan Gerhold card->owner = THIS_MODULE;
6026b1687bfSRohit kumar dev_set_drvdata(dev, card);
6036b1687bfSRohit kumar ret = qcom_snd_parse_of(card);
604d0c56b30SBjorn Andersson if (ret)
605ed3b53e7SStephan Gerhold return ret;
6066b1687bfSRohit kumar
6076b1687bfSRohit kumar data->card = card;
6086b1687bfSRohit kumar snd_soc_card_set_drvdata(card, data);
6096b1687bfSRohit kumar
61080e733fdSCheng-Yi Chiang sdm845_add_ops(card);
611ed3b53e7SStephan Gerhold return devm_snd_soc_register_card(dev, card);
6126b1687bfSRohit kumar }
6136b1687bfSRohit kumar
6146b1687bfSRohit kumar static const struct of_device_id sdm845_snd_device_id[] = {
6156b1687bfSRohit kumar { .compatible = "qcom,sdm845-sndcard" },
6163a14adc5SKrzysztof Kozlowski /* Do not grow the list for compatible devices */
6175caf64c6SSrinivas Kandagatla { .compatible = "qcom,db845c-sndcard" },
6185caf64c6SSrinivas Kandagatla { .compatible = "lenovo,yoga-c630-sndcard" },
6196b1687bfSRohit kumar {},
6206b1687bfSRohit kumar };
6216b1687bfSRohit kumar MODULE_DEVICE_TABLE(of, sdm845_snd_device_id);
6226b1687bfSRohit kumar
6236b1687bfSRohit kumar static struct platform_driver sdm845_snd_driver = {
6246b1687bfSRohit kumar .probe = sdm845_snd_platform_probe,
6256b1687bfSRohit kumar .driver = {
6266b1687bfSRohit kumar .name = "msm-snd-sdm845",
6276b1687bfSRohit kumar .of_match_table = sdm845_snd_device_id,
6286b1687bfSRohit kumar },
6296b1687bfSRohit kumar };
6306b1687bfSRohit kumar module_platform_driver(sdm845_snd_driver);
6316b1687bfSRohit kumar
6326b1687bfSRohit kumar MODULE_DESCRIPTION("sdm845 ASoC Machine Driver");
633bb339245SKrzysztof Kozlowski MODULE_LICENSE("GPL");
634