16bae5ea9SRakesh Ughreja // SPDX-License-Identifier: GPL-2.0 26bae5ea9SRakesh Ughreja // Copyright(c) 2015-18 Intel Corporation. 36bae5ea9SRakesh Ughreja 46bae5ea9SRakesh Ughreja /* 56bae5ea9SRakesh Ughreja * hdac_hda.c - ASoC extensions to reuse the legacy HDA codec drivers 66bae5ea9SRakesh Ughreja * with ASoC platform drivers. These APIs are called by the legacy HDA 76bae5ea9SRakesh Ughreja * codec drivers using hdac_ext_bus_ops ops. 86bae5ea9SRakesh Ughreja */ 96bae5ea9SRakesh Ughreja 106bae5ea9SRakesh Ughreja #include <linux/init.h> 116bae5ea9SRakesh Ughreja #include <linux/delay.h> 126bae5ea9SRakesh Ughreja #include <linux/module.h> 136bae5ea9SRakesh Ughreja #include <linux/pm_runtime.h> 146bae5ea9SRakesh Ughreja #include <sound/pcm_params.h> 156bae5ea9SRakesh Ughreja #include <sound/soc.h> 166bae5ea9SRakesh Ughreja #include <sound/hdaudio_ext.h> 17608b8c36SKai Vehmanen #include <sound/hda_i915.h> 186bae5ea9SRakesh Ughreja #include <sound/hda_codec.h> 196bae5ea9SRakesh Ughreja #include <sound/hda_register.h> 206bae5ea9SRakesh Ughreja 21608b8c36SKai Vehmanen #include "hdac_hda.h" 226bae5ea9SRakesh Ughreja 236bae5ea9SRakesh Ughreja #define STUB_FORMATS (SNDRV_PCM_FMTBIT_S8 | \ 246bae5ea9SRakesh Ughreja SNDRV_PCM_FMTBIT_U8 | \ 256bae5ea9SRakesh Ughreja SNDRV_PCM_FMTBIT_S16_LE | \ 266bae5ea9SRakesh Ughreja SNDRV_PCM_FMTBIT_U16_LE | \ 276bae5ea9SRakesh Ughreja SNDRV_PCM_FMTBIT_S24_LE | \ 286bae5ea9SRakesh Ughreja SNDRV_PCM_FMTBIT_U24_LE | \ 296bae5ea9SRakesh Ughreja SNDRV_PCM_FMTBIT_S32_LE | \ 306bae5ea9SRakesh Ughreja SNDRV_PCM_FMTBIT_U32_LE | \ 316bae5ea9SRakesh Ughreja SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE) 326bae5ea9SRakesh Ughreja 33608b8c36SKai Vehmanen #define STUB_HDMI_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\ 34608b8c36SKai Vehmanen SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\ 35608b8c36SKai Vehmanen SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\ 36608b8c36SKai Vehmanen SNDRV_PCM_RATE_192000) 37608b8c36SKai Vehmanen 386bae5ea9SRakesh Ughreja static int hdac_hda_dai_open(struct snd_pcm_substream *substream, 396bae5ea9SRakesh Ughreja struct snd_soc_dai *dai); 406bae5ea9SRakesh Ughreja static void hdac_hda_dai_close(struct snd_pcm_substream *substream, 416bae5ea9SRakesh Ughreja struct snd_soc_dai *dai); 426bae5ea9SRakesh Ughreja static int hdac_hda_dai_prepare(struct snd_pcm_substream *substream, 436bae5ea9SRakesh Ughreja struct snd_soc_dai *dai); 4403d0aa4dSRander Wang static int hdac_hda_dai_hw_params(struct snd_pcm_substream *substream, 4503d0aa4dSRander Wang struct snd_pcm_hw_params *params, 4603d0aa4dSRander Wang struct snd_soc_dai *dai); 476bae5ea9SRakesh Ughreja static int hdac_hda_dai_hw_free(struct snd_pcm_substream *substream, 486bae5ea9SRakesh Ughreja struct snd_soc_dai *dai); 496bae5ea9SRakesh Ughreja static int hdac_hda_dai_set_tdm_slot(struct snd_soc_dai *dai, 506bae5ea9SRakesh Ughreja unsigned int tx_mask, unsigned int rx_mask, 516bae5ea9SRakesh Ughreja int slots, int slot_width); 526bae5ea9SRakesh Ughreja static struct hda_pcm *snd_soc_find_pcm_from_dai(struct hdac_hda_priv *hda_pvt, 536bae5ea9SRakesh Ughreja struct snd_soc_dai *dai); 546bae5ea9SRakesh Ughreja 55704a9fc2SJulia Lawall static const struct snd_soc_dai_ops hdac_hda_dai_ops = { 566bae5ea9SRakesh Ughreja .startup = hdac_hda_dai_open, 576bae5ea9SRakesh Ughreja .shutdown = hdac_hda_dai_close, 586bae5ea9SRakesh Ughreja .prepare = hdac_hda_dai_prepare, 5903d0aa4dSRander Wang .hw_params = hdac_hda_dai_hw_params, 606bae5ea9SRakesh Ughreja .hw_free = hdac_hda_dai_hw_free, 616bae5ea9SRakesh Ughreja .set_tdm_slot = hdac_hda_dai_set_tdm_slot, 626bae5ea9SRakesh Ughreja }; 636bae5ea9SRakesh Ughreja 646bae5ea9SRakesh Ughreja static struct snd_soc_dai_driver hdac_hda_dais[] = { 656bae5ea9SRakesh Ughreja { 666bae5ea9SRakesh Ughreja .id = HDAC_ANALOG_DAI_ID, 676bae5ea9SRakesh Ughreja .name = "Analog Codec DAI", 686bae5ea9SRakesh Ughreja .ops = &hdac_hda_dai_ops, 696bae5ea9SRakesh Ughreja .playback = { 706bae5ea9SRakesh Ughreja .stream_name = "Analog Codec Playback", 716bae5ea9SRakesh Ughreja .channels_min = 1, 726bae5ea9SRakesh Ughreja .channels_max = 16, 736bae5ea9SRakesh Ughreja .rates = SNDRV_PCM_RATE_8000_192000, 746bae5ea9SRakesh Ughreja .formats = STUB_FORMATS, 756bae5ea9SRakesh Ughreja .sig_bits = 24, 766bae5ea9SRakesh Ughreja }, 776bae5ea9SRakesh Ughreja .capture = { 786bae5ea9SRakesh Ughreja .stream_name = "Analog Codec Capture", 796bae5ea9SRakesh Ughreja .channels_min = 1, 806bae5ea9SRakesh Ughreja .channels_max = 16, 816bae5ea9SRakesh Ughreja .rates = SNDRV_PCM_RATE_8000_192000, 826bae5ea9SRakesh Ughreja .formats = STUB_FORMATS, 836bae5ea9SRakesh Ughreja .sig_bits = 24, 846bae5ea9SRakesh Ughreja }, 856bae5ea9SRakesh Ughreja }, 866bae5ea9SRakesh Ughreja { 876bae5ea9SRakesh Ughreja .id = HDAC_DIGITAL_DAI_ID, 886bae5ea9SRakesh Ughreja .name = "Digital Codec DAI", 896bae5ea9SRakesh Ughreja .ops = &hdac_hda_dai_ops, 906bae5ea9SRakesh Ughreja .playback = { 916bae5ea9SRakesh Ughreja .stream_name = "Digital Codec Playback", 926bae5ea9SRakesh Ughreja .channels_min = 1, 936bae5ea9SRakesh Ughreja .channels_max = 16, 946bae5ea9SRakesh Ughreja .rates = SNDRV_PCM_RATE_8000_192000, 956bae5ea9SRakesh Ughreja .formats = STUB_FORMATS, 966bae5ea9SRakesh Ughreja .sig_bits = 24, 976bae5ea9SRakesh Ughreja }, 986bae5ea9SRakesh Ughreja .capture = { 996bae5ea9SRakesh Ughreja .stream_name = "Digital Codec Capture", 1006bae5ea9SRakesh Ughreja .channels_min = 1, 1016bae5ea9SRakesh Ughreja .channels_max = 16, 1026bae5ea9SRakesh Ughreja .rates = SNDRV_PCM_RATE_8000_192000, 1036bae5ea9SRakesh Ughreja .formats = STUB_FORMATS, 1046bae5ea9SRakesh Ughreja .sig_bits = 24, 1056bae5ea9SRakesh Ughreja }, 1066bae5ea9SRakesh Ughreja }, 1076bae5ea9SRakesh Ughreja { 1086bae5ea9SRakesh Ughreja .id = HDAC_ALT_ANALOG_DAI_ID, 1096bae5ea9SRakesh Ughreja .name = "Alt Analog Codec DAI", 1106bae5ea9SRakesh Ughreja .ops = &hdac_hda_dai_ops, 1116bae5ea9SRakesh Ughreja .playback = { 1126bae5ea9SRakesh Ughreja .stream_name = "Alt Analog Codec Playback", 1136bae5ea9SRakesh Ughreja .channels_min = 1, 1146bae5ea9SRakesh Ughreja .channels_max = 16, 1156bae5ea9SRakesh Ughreja .rates = SNDRV_PCM_RATE_8000_192000, 1166bae5ea9SRakesh Ughreja .formats = STUB_FORMATS, 1176bae5ea9SRakesh Ughreja .sig_bits = 24, 1186bae5ea9SRakesh Ughreja }, 1196bae5ea9SRakesh Ughreja .capture = { 1206bae5ea9SRakesh Ughreja .stream_name = "Alt Analog Codec Capture", 1216bae5ea9SRakesh Ughreja .channels_min = 1, 1226bae5ea9SRakesh Ughreja .channels_max = 16, 1236bae5ea9SRakesh Ughreja .rates = SNDRV_PCM_RATE_8000_192000, 1246bae5ea9SRakesh Ughreja .formats = STUB_FORMATS, 1256bae5ea9SRakesh Ughreja .sig_bits = 24, 1266bae5ea9SRakesh Ughreja }, 127608b8c36SKai Vehmanen }, 128608b8c36SKai Vehmanen { 129608b8c36SKai Vehmanen .id = HDAC_HDMI_0_DAI_ID, 130608b8c36SKai Vehmanen .name = "intel-hdmi-hifi1", 131608b8c36SKai Vehmanen .ops = &hdac_hda_dai_ops, 132608b8c36SKai Vehmanen .playback = { 133608b8c36SKai Vehmanen .stream_name = "hifi1", 134608b8c36SKai Vehmanen .channels_min = 1, 135608b8c36SKai Vehmanen .channels_max = 32, 136608b8c36SKai Vehmanen .rates = STUB_HDMI_RATES, 137608b8c36SKai Vehmanen .formats = STUB_FORMATS, 138608b8c36SKai Vehmanen .sig_bits = 24, 139608b8c36SKai Vehmanen }, 140608b8c36SKai Vehmanen }, 141608b8c36SKai Vehmanen { 142608b8c36SKai Vehmanen .id = HDAC_HDMI_1_DAI_ID, 143608b8c36SKai Vehmanen .name = "intel-hdmi-hifi2", 144608b8c36SKai Vehmanen .ops = &hdac_hda_dai_ops, 145608b8c36SKai Vehmanen .playback = { 146608b8c36SKai Vehmanen .stream_name = "hifi2", 147608b8c36SKai Vehmanen .channels_min = 1, 148608b8c36SKai Vehmanen .channels_max = 32, 149608b8c36SKai Vehmanen .rates = STUB_HDMI_RATES, 150608b8c36SKai Vehmanen .formats = STUB_FORMATS, 151608b8c36SKai Vehmanen .sig_bits = 24, 152608b8c36SKai Vehmanen }, 153608b8c36SKai Vehmanen }, 154608b8c36SKai Vehmanen { 155608b8c36SKai Vehmanen .id = HDAC_HDMI_2_DAI_ID, 156608b8c36SKai Vehmanen .name = "intel-hdmi-hifi3", 157608b8c36SKai Vehmanen .ops = &hdac_hda_dai_ops, 158608b8c36SKai Vehmanen .playback = { 159608b8c36SKai Vehmanen .stream_name = "hifi3", 160608b8c36SKai Vehmanen .channels_min = 1, 161608b8c36SKai Vehmanen .channels_max = 32, 162608b8c36SKai Vehmanen .rates = STUB_HDMI_RATES, 163608b8c36SKai Vehmanen .formats = STUB_FORMATS, 164608b8c36SKai Vehmanen .sig_bits = 24, 165608b8c36SKai Vehmanen }, 166608b8c36SKai Vehmanen }, 1674bb16cd8SSathyanarayana Nujella { 1684bb16cd8SSathyanarayana Nujella .id = HDAC_HDMI_3_DAI_ID, 1694bb16cd8SSathyanarayana Nujella .name = "intel-hdmi-hifi4", 1704bb16cd8SSathyanarayana Nujella .ops = &hdac_hda_dai_ops, 1714bb16cd8SSathyanarayana Nujella .playback = { 1724bb16cd8SSathyanarayana Nujella .stream_name = "hifi4", 1734bb16cd8SSathyanarayana Nujella .channels_min = 1, 1744bb16cd8SSathyanarayana Nujella .channels_max = 32, 1754bb16cd8SSathyanarayana Nujella .rates = STUB_HDMI_RATES, 1764bb16cd8SSathyanarayana Nujella .formats = STUB_FORMATS, 1774bb16cd8SSathyanarayana Nujella .sig_bits = 24, 1784bb16cd8SSathyanarayana Nujella }, 1794bb16cd8SSathyanarayana Nujella }, 1806bae5ea9SRakesh Ughreja 1816bae5ea9SRakesh Ughreja }; 1826bae5ea9SRakesh Ughreja 1836bae5ea9SRakesh Ughreja static int hdac_hda_dai_set_tdm_slot(struct snd_soc_dai *dai, 1846bae5ea9SRakesh Ughreja unsigned int tx_mask, unsigned int rx_mask, 1856bae5ea9SRakesh Ughreja int slots, int slot_width) 1866bae5ea9SRakesh Ughreja { 1876bae5ea9SRakesh Ughreja struct snd_soc_component *component = dai->component; 1886bae5ea9SRakesh Ughreja struct hdac_hda_priv *hda_pvt; 1896bae5ea9SRakesh Ughreja struct hdac_hda_pcm *pcm; 1906bae5ea9SRakesh Ughreja 1916bae5ea9SRakesh Ughreja hda_pvt = snd_soc_component_get_drvdata(component); 1926bae5ea9SRakesh Ughreja pcm = &hda_pvt->pcm[dai->id]; 193608b8c36SKai Vehmanen 1946bae5ea9SRakesh Ughreja if (tx_mask) 195608b8c36SKai Vehmanen pcm->stream_tag[SNDRV_PCM_STREAM_PLAYBACK] = tx_mask; 1966bae5ea9SRakesh Ughreja else 197608b8c36SKai Vehmanen pcm->stream_tag[SNDRV_PCM_STREAM_CAPTURE] = rx_mask; 1986bae5ea9SRakesh Ughreja 1996bae5ea9SRakesh Ughreja return 0; 2006bae5ea9SRakesh Ughreja } 2016bae5ea9SRakesh Ughreja 20203d0aa4dSRander Wang static int hdac_hda_dai_hw_params(struct snd_pcm_substream *substream, 20303d0aa4dSRander Wang struct snd_pcm_hw_params *params, 20403d0aa4dSRander Wang struct snd_soc_dai *dai) 20503d0aa4dSRander Wang { 20603d0aa4dSRander Wang struct snd_soc_component *component = dai->component; 20703d0aa4dSRander Wang struct hdac_hda_priv *hda_pvt; 20803d0aa4dSRander Wang unsigned int format_val; 20903d0aa4dSRander Wang unsigned int maxbps; 21003d0aa4dSRander Wang 21103d0aa4dSRander Wang if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 21203d0aa4dSRander Wang maxbps = dai->driver->playback.sig_bits; 21303d0aa4dSRander Wang else 21403d0aa4dSRander Wang maxbps = dai->driver->capture.sig_bits; 21503d0aa4dSRander Wang 21603d0aa4dSRander Wang hda_pvt = snd_soc_component_get_drvdata(component); 21703d0aa4dSRander Wang format_val = snd_hdac_calc_stream_format(params_rate(params), 21803d0aa4dSRander Wang params_channels(params), 21903d0aa4dSRander Wang params_format(params), 22003d0aa4dSRander Wang maxbps, 22103d0aa4dSRander Wang 0); 22203d0aa4dSRander Wang if (!format_val) { 22303d0aa4dSRander Wang dev_err(dai->dev, 22403d0aa4dSRander Wang "invalid format_val, rate=%d, ch=%d, format=%d, maxbps=%d\n", 22503d0aa4dSRander Wang params_rate(params), params_channels(params), 22603d0aa4dSRander Wang params_format(params), maxbps); 22703d0aa4dSRander Wang 22803d0aa4dSRander Wang return -EINVAL; 22903d0aa4dSRander Wang } 23003d0aa4dSRander Wang 23103d0aa4dSRander Wang hda_pvt->pcm[dai->id].format_val[substream->stream] = format_val; 23203d0aa4dSRander Wang return 0; 23303d0aa4dSRander Wang } 23403d0aa4dSRander Wang 2356bae5ea9SRakesh Ughreja static int hdac_hda_dai_hw_free(struct snd_pcm_substream *substream, 2366bae5ea9SRakesh Ughreja struct snd_soc_dai *dai) 2376bae5ea9SRakesh Ughreja { 2386bae5ea9SRakesh Ughreja struct snd_soc_component *component = dai->component; 2396bae5ea9SRakesh Ughreja struct hdac_hda_priv *hda_pvt; 2406bae5ea9SRakesh Ughreja struct hda_pcm_stream *hda_stream; 2416bae5ea9SRakesh Ughreja struct hda_pcm *pcm; 2426bae5ea9SRakesh Ughreja 2436bae5ea9SRakesh Ughreja hda_pvt = snd_soc_component_get_drvdata(component); 2446bae5ea9SRakesh Ughreja pcm = snd_soc_find_pcm_from_dai(hda_pvt, dai); 2456bae5ea9SRakesh Ughreja if (!pcm) 2466bae5ea9SRakesh Ughreja return -EINVAL; 2476bae5ea9SRakesh Ughreja 2486bae5ea9SRakesh Ughreja hda_stream = &pcm->stream[substream->stream]; 2496bae5ea9SRakesh Ughreja snd_hda_codec_cleanup(&hda_pvt->codec, hda_stream, substream); 2506bae5ea9SRakesh Ughreja 2516bae5ea9SRakesh Ughreja return 0; 2526bae5ea9SRakesh Ughreja } 2536bae5ea9SRakesh Ughreja 2546bae5ea9SRakesh Ughreja static int hdac_hda_dai_prepare(struct snd_pcm_substream *substream, 2556bae5ea9SRakesh Ughreja struct snd_soc_dai *dai) 2566bae5ea9SRakesh Ughreja { 2576bae5ea9SRakesh Ughreja struct snd_soc_component *component = dai->component; 2586bae5ea9SRakesh Ughreja struct hda_pcm_stream *hda_stream; 25903d0aa4dSRander Wang struct hdac_hda_priv *hda_pvt; 26003d0aa4dSRander Wang struct hdac_device *hdev; 2616bae5ea9SRakesh Ughreja unsigned int format_val; 2626bae5ea9SRakesh Ughreja struct hda_pcm *pcm; 2636bae5ea9SRakesh Ughreja unsigned int stream; 2646bae5ea9SRakesh Ughreja int ret = 0; 2656bae5ea9SRakesh Ughreja 2666bae5ea9SRakesh Ughreja hda_pvt = snd_soc_component_get_drvdata(component); 2676bae5ea9SRakesh Ughreja hdev = &hda_pvt->codec.core; 2686bae5ea9SRakesh Ughreja pcm = snd_soc_find_pcm_from_dai(hda_pvt, dai); 2696bae5ea9SRakesh Ughreja if (!pcm) 2706bae5ea9SRakesh Ughreja return -EINVAL; 2716bae5ea9SRakesh Ughreja 2726bae5ea9SRakesh Ughreja hda_stream = &pcm->stream[substream->stream]; 2736bae5ea9SRakesh Ughreja 2746bae5ea9SRakesh Ughreja stream = hda_pvt->pcm[dai->id].stream_tag[substream->stream]; 27503d0aa4dSRander Wang format_val = hda_pvt->pcm[dai->id].format_val[substream->stream]; 2766bae5ea9SRakesh Ughreja 2776bae5ea9SRakesh Ughreja ret = snd_hda_codec_prepare(&hda_pvt->codec, hda_stream, 2786bae5ea9SRakesh Ughreja stream, format_val, substream); 2796bae5ea9SRakesh Ughreja if (ret < 0) 2806bae5ea9SRakesh Ughreja dev_err(&hdev->dev, "codec prepare failed %d\n", ret); 2816bae5ea9SRakesh Ughreja 2826bae5ea9SRakesh Ughreja return ret; 2836bae5ea9SRakesh Ughreja } 2846bae5ea9SRakesh Ughreja 2856bae5ea9SRakesh Ughreja static int hdac_hda_dai_open(struct snd_pcm_substream *substream, 2866bae5ea9SRakesh Ughreja struct snd_soc_dai *dai) 2876bae5ea9SRakesh Ughreja { 2886bae5ea9SRakesh Ughreja struct snd_soc_component *component = dai->component; 2896bae5ea9SRakesh Ughreja struct hdac_hda_priv *hda_pvt; 2906bae5ea9SRakesh Ughreja struct hda_pcm_stream *hda_stream; 2916bae5ea9SRakesh Ughreja struct hda_pcm *pcm; 2926bae5ea9SRakesh Ughreja 2936bae5ea9SRakesh Ughreja hda_pvt = snd_soc_component_get_drvdata(component); 2946bae5ea9SRakesh Ughreja pcm = snd_soc_find_pcm_from_dai(hda_pvt, dai); 2956bae5ea9SRakesh Ughreja if (!pcm) 2966bae5ea9SRakesh Ughreja return -EINVAL; 2976bae5ea9SRakesh Ughreja 2986bae5ea9SRakesh Ughreja snd_hda_codec_pcm_get(pcm); 2996bae5ea9SRakesh Ughreja 3006bae5ea9SRakesh Ughreja hda_stream = &pcm->stream[substream->stream]; 3016bae5ea9SRakesh Ughreja 30206f07e23SKai Vehmanen return hda_stream->ops.open(hda_stream, &hda_pvt->codec, substream); 3036bae5ea9SRakesh Ughreja } 3046bae5ea9SRakesh Ughreja 3056bae5ea9SRakesh Ughreja static void hdac_hda_dai_close(struct snd_pcm_substream *substream, 3066bae5ea9SRakesh Ughreja struct snd_soc_dai *dai) 3076bae5ea9SRakesh Ughreja { 3086bae5ea9SRakesh Ughreja struct snd_soc_component *component = dai->component; 3096bae5ea9SRakesh Ughreja struct hdac_hda_priv *hda_pvt; 3106bae5ea9SRakesh Ughreja struct hda_pcm_stream *hda_stream; 3116bae5ea9SRakesh Ughreja struct hda_pcm *pcm; 3126bae5ea9SRakesh Ughreja 3136bae5ea9SRakesh Ughreja hda_pvt = snd_soc_component_get_drvdata(component); 3146bae5ea9SRakesh Ughreja pcm = snd_soc_find_pcm_from_dai(hda_pvt, dai); 3156bae5ea9SRakesh Ughreja if (!pcm) 3166bae5ea9SRakesh Ughreja return; 3176bae5ea9SRakesh Ughreja 3186bae5ea9SRakesh Ughreja hda_stream = &pcm->stream[substream->stream]; 3196bae5ea9SRakesh Ughreja 3206bae5ea9SRakesh Ughreja hda_stream->ops.close(hda_stream, &hda_pvt->codec, substream); 3216bae5ea9SRakesh Ughreja 3226bae5ea9SRakesh Ughreja snd_hda_codec_pcm_put(pcm); 3236bae5ea9SRakesh Ughreja } 3246bae5ea9SRakesh Ughreja 3256bae5ea9SRakesh Ughreja static struct hda_pcm *snd_soc_find_pcm_from_dai(struct hdac_hda_priv *hda_pvt, 3266bae5ea9SRakesh Ughreja struct snd_soc_dai *dai) 3276bae5ea9SRakesh Ughreja { 3286bae5ea9SRakesh Ughreja struct hda_codec *hcodec = &hda_pvt->codec; 3296bae5ea9SRakesh Ughreja struct hda_pcm *cpcm; 3306bae5ea9SRakesh Ughreja const char *pcm_name; 3316bae5ea9SRakesh Ughreja 332608b8c36SKai Vehmanen /* 333608b8c36SKai Vehmanen * map DAI ID to the closest matching PCM name, using the naming 334608b8c36SKai Vehmanen * scheme used by hda-codec snd_hda_gen_build_pcms() and for 335608b8c36SKai Vehmanen * HDMI in hda_codec patch_hdmi.c) 336608b8c36SKai Vehmanen */ 337608b8c36SKai Vehmanen 3386bae5ea9SRakesh Ughreja switch (dai->id) { 3396bae5ea9SRakesh Ughreja case HDAC_ANALOG_DAI_ID: 3406bae5ea9SRakesh Ughreja pcm_name = "Analog"; 3416bae5ea9SRakesh Ughreja break; 3426bae5ea9SRakesh Ughreja case HDAC_DIGITAL_DAI_ID: 3436bae5ea9SRakesh Ughreja pcm_name = "Digital"; 3446bae5ea9SRakesh Ughreja break; 3456bae5ea9SRakesh Ughreja case HDAC_ALT_ANALOG_DAI_ID: 3466bae5ea9SRakesh Ughreja pcm_name = "Alt Analog"; 3476bae5ea9SRakesh Ughreja break; 348608b8c36SKai Vehmanen case HDAC_HDMI_0_DAI_ID: 349608b8c36SKai Vehmanen pcm_name = "HDMI 0"; 350608b8c36SKai Vehmanen break; 351608b8c36SKai Vehmanen case HDAC_HDMI_1_DAI_ID: 352608b8c36SKai Vehmanen pcm_name = "HDMI 1"; 353608b8c36SKai Vehmanen break; 354608b8c36SKai Vehmanen case HDAC_HDMI_2_DAI_ID: 355608b8c36SKai Vehmanen pcm_name = "HDMI 2"; 356608b8c36SKai Vehmanen break; 3574bb16cd8SSathyanarayana Nujella case HDAC_HDMI_3_DAI_ID: 3584bb16cd8SSathyanarayana Nujella pcm_name = "HDMI 3"; 3594bb16cd8SSathyanarayana Nujella break; 3606bae5ea9SRakesh Ughreja default: 3616bae5ea9SRakesh Ughreja dev_err(&hcodec->core.dev, "invalid dai id %d\n", dai->id); 3626bae5ea9SRakesh Ughreja return NULL; 3636bae5ea9SRakesh Ughreja } 3646bae5ea9SRakesh Ughreja 3656bae5ea9SRakesh Ughreja list_for_each_entry(cpcm, &hcodec->pcm_list_head, list) { 366608b8c36SKai Vehmanen if (strstr(cpcm->name, pcm_name)) 3676bae5ea9SRakesh Ughreja return cpcm; 3686bae5ea9SRakesh Ughreja } 3696bae5ea9SRakesh Ughreja 3706bae5ea9SRakesh Ughreja dev_err(&hcodec->core.dev, "didn't find PCM for DAI %s\n", dai->name); 3716bae5ea9SRakesh Ughreja return NULL; 3726bae5ea9SRakesh Ughreja } 3736bae5ea9SRakesh Ughreja 374608b8c36SKai Vehmanen static bool is_hdmi_codec(struct hda_codec *hcodec) 375608b8c36SKai Vehmanen { 376608b8c36SKai Vehmanen struct hda_pcm *cpcm; 377608b8c36SKai Vehmanen 378608b8c36SKai Vehmanen list_for_each_entry(cpcm, &hcodec->pcm_list_head, list) { 379608b8c36SKai Vehmanen if (cpcm->pcm_type == HDA_PCM_TYPE_HDMI) 380608b8c36SKai Vehmanen return true; 381608b8c36SKai Vehmanen } 382608b8c36SKai Vehmanen 383608b8c36SKai Vehmanen return false; 384608b8c36SKai Vehmanen } 385608b8c36SKai Vehmanen 3866bae5ea9SRakesh Ughreja static int hdac_hda_codec_probe(struct snd_soc_component *component) 3876bae5ea9SRakesh Ughreja { 3886bae5ea9SRakesh Ughreja struct hdac_hda_priv *hda_pvt = 3896bae5ea9SRakesh Ughreja snd_soc_component_get_drvdata(component); 3906bae5ea9SRakesh Ughreja struct snd_soc_dapm_context *dapm = 3916bae5ea9SRakesh Ughreja snd_soc_component_get_dapm(component); 3926bae5ea9SRakesh Ughreja struct hdac_device *hdev = &hda_pvt->codec.core; 3936bae5ea9SRakesh Ughreja struct hda_codec *hcodec = &hda_pvt->codec; 3946bae5ea9SRakesh Ughreja struct hdac_ext_link *hlink; 3956bae5ea9SRakesh Ughreja hda_codec_patch_t patch; 3966bae5ea9SRakesh Ughreja int ret; 3976bae5ea9SRakesh Ughreja 3986bae5ea9SRakesh Ughreja hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev)); 3996bae5ea9SRakesh Ughreja if (!hlink) { 4006bae5ea9SRakesh Ughreja dev_err(&hdev->dev, "hdac link not found\n"); 4016bae5ea9SRakesh Ughreja return -EIO; 4026bae5ea9SRakesh Ughreja } 4036bae5ea9SRakesh Ughreja 4046bae5ea9SRakesh Ughreja snd_hdac_ext_bus_link_get(hdev->bus, hlink); 4056bae5ea9SRakesh Ughreja 406608b8c36SKai Vehmanen /* 407608b8c36SKai Vehmanen * Ensure any HDA display is powered at codec probe. 408608b8c36SKai Vehmanen * After snd_hda_codec_device_new(), display power is 409608b8c36SKai Vehmanen * managed by runtime PM. 410608b8c36SKai Vehmanen */ 411608b8c36SKai Vehmanen if (hda_pvt->need_display_power) 412608b8c36SKai Vehmanen snd_hdac_display_power(hdev->bus, 413608b8c36SKai Vehmanen HDA_CODEC_IDX_CONTROLLER, true); 414608b8c36SKai Vehmanen 4156bae5ea9SRakesh Ughreja ret = snd_hda_codec_device_new(hcodec->bus, component->card->snd_card, 4166bae5ea9SRakesh Ughreja hdev->addr, hcodec); 4176bae5ea9SRakesh Ughreja if (ret < 0) { 4186bae5ea9SRakesh Ughreja dev_err(&hdev->dev, "failed to create hda codec %d\n", ret); 4196bae5ea9SRakesh Ughreja goto error_no_pm; 4206bae5ea9SRakesh Ughreja } 421b60ee2e2SBard liao /* 422b60ee2e2SBard liao * Overwrite type to HDA_DEV_ASOC since it is a ASoC driver 423b60ee2e2SBard liao * hda_codec.c will check this flag to determine if unregister 424b60ee2e2SBard liao * device is needed. 425b60ee2e2SBard liao */ 426b60ee2e2SBard liao hdev->type = HDA_DEV_ASOC; 4276bae5ea9SRakesh Ughreja 4286bae5ea9SRakesh Ughreja /* 4296bae5ea9SRakesh Ughreja * snd_hda_codec_device_new decrements the usage count so call get pm 4306bae5ea9SRakesh Ughreja * else the device will be powered off 4316bae5ea9SRakesh Ughreja */ 4326bae5ea9SRakesh Ughreja pm_runtime_get_noresume(&hdev->dev); 4336bae5ea9SRakesh Ughreja 4346bae5ea9SRakesh Ughreja hcodec->bus->card = dapm->card->snd_card; 4356bae5ea9SRakesh Ughreja 4366bae5ea9SRakesh Ughreja ret = snd_hda_codec_set_name(hcodec, hcodec->preset->name); 4376bae5ea9SRakesh Ughreja if (ret < 0) { 4386bae5ea9SRakesh Ughreja dev_err(&hdev->dev, "name failed %s\n", hcodec->preset->name); 439a94eacceSPierre-Louis Bossart goto error_pm; 4406bae5ea9SRakesh Ughreja } 4416bae5ea9SRakesh Ughreja 4426bae5ea9SRakesh Ughreja ret = snd_hdac_regmap_init(&hcodec->core); 4436bae5ea9SRakesh Ughreja if (ret < 0) { 4446bae5ea9SRakesh Ughreja dev_err(&hdev->dev, "regmap init failed\n"); 445a94eacceSPierre-Louis Bossart goto error_pm; 4466bae5ea9SRakesh Ughreja } 4476bae5ea9SRakesh Ughreja 4486bae5ea9SRakesh Ughreja patch = (hda_codec_patch_t)hcodec->preset->driver_data; 4496bae5ea9SRakesh Ughreja if (patch) { 4506bae5ea9SRakesh Ughreja ret = patch(hcodec); 4516bae5ea9SRakesh Ughreja if (ret < 0) { 4526bae5ea9SRakesh Ughreja dev_err(&hdev->dev, "patch failed %d\n", ret); 453a94eacceSPierre-Louis Bossart goto error_regmap; 4546bae5ea9SRakesh Ughreja } 4556bae5ea9SRakesh Ughreja } else { 4566bae5ea9SRakesh Ughreja dev_dbg(&hdev->dev, "no patch file found\n"); 4576bae5ea9SRakesh Ughreja } 4586bae5ea9SRakesh Ughreja 459608b8c36SKai Vehmanen /* configure codec for 1:1 PCM:DAI mapping */ 460608b8c36SKai Vehmanen hcodec->mst_no_extra_pcms = 1; 461608b8c36SKai Vehmanen 4626bae5ea9SRakesh Ughreja ret = snd_hda_codec_parse_pcms(hcodec); 4636bae5ea9SRakesh Ughreja if (ret < 0) { 4646bae5ea9SRakesh Ughreja dev_err(&hdev->dev, "unable to map pcms to dai %d\n", ret); 465640f835cSKai Vehmanen goto error_patch; 4666bae5ea9SRakesh Ughreja } 4676bae5ea9SRakesh Ughreja 468608b8c36SKai Vehmanen /* HDMI controls need to be created in machine drivers */ 469608b8c36SKai Vehmanen if (!is_hdmi_codec(hcodec)) { 4706bae5ea9SRakesh Ughreja ret = snd_hda_codec_build_controls(hcodec); 4716bae5ea9SRakesh Ughreja if (ret < 0) { 472608b8c36SKai Vehmanen dev_err(&hdev->dev, "unable to create controls %d\n", 473608b8c36SKai Vehmanen ret); 474640f835cSKai Vehmanen goto error_patch; 4756bae5ea9SRakesh Ughreja } 476608b8c36SKai Vehmanen } 4776bae5ea9SRakesh Ughreja 4786bae5ea9SRakesh Ughreja hcodec->core.lazy_cache = true; 4796bae5ea9SRakesh Ughreja 480608b8c36SKai Vehmanen if (hda_pvt->need_display_power) 481608b8c36SKai Vehmanen snd_hdac_display_power(hdev->bus, 482608b8c36SKai Vehmanen HDA_CODEC_IDX_CONTROLLER, false); 483608b8c36SKai Vehmanen 4847a2ba46fSKai Vehmanen /* match for forbid call in snd_hda_codec_device_new() */ 4857a2ba46fSKai Vehmanen pm_runtime_allow(&hdev->dev); 4867a2ba46fSKai Vehmanen 4876bae5ea9SRakesh Ughreja /* 4886bae5ea9SRakesh Ughreja * hdac_device core already sets the state to active and calls 4896bae5ea9SRakesh Ughreja * get_noresume. So enable runtime and set the device to suspend. 4906bae5ea9SRakesh Ughreja * pm_runtime_enable is also called during codec registeration 4916bae5ea9SRakesh Ughreja */ 4926bae5ea9SRakesh Ughreja pm_runtime_put(&hdev->dev); 4936bae5ea9SRakesh Ughreja pm_runtime_suspend(&hdev->dev); 4946bae5ea9SRakesh Ughreja 4956bae5ea9SRakesh Ughreja return 0; 4966bae5ea9SRakesh Ughreja 497640f835cSKai Vehmanen error_patch: 498640f835cSKai Vehmanen if (hcodec->patch_ops.free) 499640f835cSKai Vehmanen hcodec->patch_ops.free(hcodec); 500a94eacceSPierre-Louis Bossart error_regmap: 501a94eacceSPierre-Louis Bossart snd_hdac_regmap_exit(hdev); 502a94eacceSPierre-Louis Bossart error_pm: 5036bae5ea9SRakesh Ughreja pm_runtime_put(&hdev->dev); 5046bae5ea9SRakesh Ughreja error_no_pm: 5056bae5ea9SRakesh Ughreja snd_hdac_ext_bus_link_put(hdev->bus, hlink); 5066bae5ea9SRakesh Ughreja return ret; 5076bae5ea9SRakesh Ughreja } 5086bae5ea9SRakesh Ughreja 5096bae5ea9SRakesh Ughreja static void hdac_hda_codec_remove(struct snd_soc_component *component) 5106bae5ea9SRakesh Ughreja { 5116bae5ea9SRakesh Ughreja struct hdac_hda_priv *hda_pvt = 5126bae5ea9SRakesh Ughreja snd_soc_component_get_drvdata(component); 5136bae5ea9SRakesh Ughreja struct hdac_device *hdev = &hda_pvt->codec.core; 514c3ec8ac8SKai Vehmanen struct hda_codec *codec = &hda_pvt->codec; 5156bae5ea9SRakesh Ughreja struct hdac_ext_link *hlink = NULL; 5166bae5ea9SRakesh Ughreja 5176bae5ea9SRakesh Ughreja hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev)); 5186bae5ea9SRakesh Ughreja if (!hlink) { 5196bae5ea9SRakesh Ughreja dev_err(&hdev->dev, "hdac link not found\n"); 5206bae5ea9SRakesh Ughreja return; 5216bae5ea9SRakesh Ughreja } 5226bae5ea9SRakesh Ughreja 5236bae5ea9SRakesh Ughreja pm_runtime_disable(&hdev->dev); 5245dc7d5bcSKai Vehmanen snd_hdac_ext_bus_link_put(hdev->bus, hlink); 525a94eacceSPierre-Louis Bossart 526c3ec8ac8SKai Vehmanen if (codec->patch_ops.free) 527c3ec8ac8SKai Vehmanen codec->patch_ops.free(codec); 528c3ec8ac8SKai Vehmanen 529c3ec8ac8SKai Vehmanen snd_hda_codec_cleanup_for_unbind(codec); 5306bae5ea9SRakesh Ughreja } 5316bae5ea9SRakesh Ughreja 5326bae5ea9SRakesh Ughreja static const struct snd_soc_dapm_route hdac_hda_dapm_routes[] = { 5336bae5ea9SRakesh Ughreja {"AIF1TX", NULL, "Codec Input Pin1"}, 5346bae5ea9SRakesh Ughreja {"AIF2TX", NULL, "Codec Input Pin2"}, 5356bae5ea9SRakesh Ughreja {"AIF3TX", NULL, "Codec Input Pin3"}, 5366bae5ea9SRakesh Ughreja 5376bae5ea9SRakesh Ughreja {"Codec Output Pin1", NULL, "AIF1RX"}, 5386bae5ea9SRakesh Ughreja {"Codec Output Pin2", NULL, "AIF2RX"}, 5396bae5ea9SRakesh Ughreja {"Codec Output Pin3", NULL, "AIF3RX"}, 5406bae5ea9SRakesh Ughreja }; 5416bae5ea9SRakesh Ughreja 5426bae5ea9SRakesh Ughreja static const struct snd_soc_dapm_widget hdac_hda_dapm_widgets[] = { 5436bae5ea9SRakesh Ughreja /* Audio Interface */ 5446bae5ea9SRakesh Ughreja SND_SOC_DAPM_AIF_IN("AIF1RX", "Analog Codec Playback", 0, 5456bae5ea9SRakesh Ughreja SND_SOC_NOPM, 0, 0), 5466bae5ea9SRakesh Ughreja SND_SOC_DAPM_AIF_IN("AIF2RX", "Digital Codec Playback", 0, 5476bae5ea9SRakesh Ughreja SND_SOC_NOPM, 0, 0), 5486bae5ea9SRakesh Ughreja SND_SOC_DAPM_AIF_IN("AIF3RX", "Alt Analog Codec Playback", 0, 5496bae5ea9SRakesh Ughreja SND_SOC_NOPM, 0, 0), 5506bae5ea9SRakesh Ughreja SND_SOC_DAPM_AIF_OUT("AIF1TX", "Analog Codec Capture", 0, 5516bae5ea9SRakesh Ughreja SND_SOC_NOPM, 0, 0), 5526bae5ea9SRakesh Ughreja SND_SOC_DAPM_AIF_OUT("AIF2TX", "Digital Codec Capture", 0, 5536bae5ea9SRakesh Ughreja SND_SOC_NOPM, 0, 0), 5546bae5ea9SRakesh Ughreja SND_SOC_DAPM_AIF_OUT("AIF3TX", "Alt Analog Codec Capture", 0, 5556bae5ea9SRakesh Ughreja SND_SOC_NOPM, 0, 0), 5566bae5ea9SRakesh Ughreja 5576bae5ea9SRakesh Ughreja /* Input Pins */ 5586bae5ea9SRakesh Ughreja SND_SOC_DAPM_INPUT("Codec Input Pin1"), 5596bae5ea9SRakesh Ughreja SND_SOC_DAPM_INPUT("Codec Input Pin2"), 5606bae5ea9SRakesh Ughreja SND_SOC_DAPM_INPUT("Codec Input Pin3"), 5616bae5ea9SRakesh Ughreja 5626bae5ea9SRakesh Ughreja /* Output Pins */ 5636bae5ea9SRakesh Ughreja SND_SOC_DAPM_OUTPUT("Codec Output Pin1"), 5646bae5ea9SRakesh Ughreja SND_SOC_DAPM_OUTPUT("Codec Output Pin2"), 5656bae5ea9SRakesh Ughreja SND_SOC_DAPM_OUTPUT("Codec Output Pin3"), 5666bae5ea9SRakesh Ughreja }; 5676bae5ea9SRakesh Ughreja 5686bae5ea9SRakesh Ughreja static const struct snd_soc_component_driver hdac_hda_codec = { 5696bae5ea9SRakesh Ughreja .probe = hdac_hda_codec_probe, 5706bae5ea9SRakesh Ughreja .remove = hdac_hda_codec_remove, 5716bae5ea9SRakesh Ughreja .idle_bias_on = false, 5726bae5ea9SRakesh Ughreja .dapm_widgets = hdac_hda_dapm_widgets, 5736bae5ea9SRakesh Ughreja .num_dapm_widgets = ARRAY_SIZE(hdac_hda_dapm_widgets), 5746bae5ea9SRakesh Ughreja .dapm_routes = hdac_hda_dapm_routes, 5756bae5ea9SRakesh Ughreja .num_dapm_routes = ARRAY_SIZE(hdac_hda_dapm_routes), 5766bae5ea9SRakesh Ughreja }; 5776bae5ea9SRakesh Ughreja 5786bae5ea9SRakesh Ughreja static int hdac_hda_dev_probe(struct hdac_device *hdev) 5796bae5ea9SRakesh Ughreja { 5806bae5ea9SRakesh Ughreja struct hdac_ext_link *hlink; 5816bae5ea9SRakesh Ughreja struct hdac_hda_priv *hda_pvt; 5826bae5ea9SRakesh Ughreja int ret; 5836bae5ea9SRakesh Ughreja 5846bae5ea9SRakesh Ughreja /* hold the ref while we probe */ 5856bae5ea9SRakesh Ughreja hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev)); 5866bae5ea9SRakesh Ughreja if (!hlink) { 5876bae5ea9SRakesh Ughreja dev_err(&hdev->dev, "hdac link not found\n"); 5886bae5ea9SRakesh Ughreja return -EIO; 5896bae5ea9SRakesh Ughreja } 5906bae5ea9SRakesh Ughreja snd_hdac_ext_bus_link_get(hdev->bus, hlink); 5916bae5ea9SRakesh Ughreja 5926bae5ea9SRakesh Ughreja hda_pvt = hdac_to_hda_priv(hdev); 5936bae5ea9SRakesh Ughreja if (!hda_pvt) 5946bae5ea9SRakesh Ughreja return -ENOMEM; 5956bae5ea9SRakesh Ughreja 5966bae5ea9SRakesh Ughreja /* ASoC specific initialization */ 59710ccaa39SKuninori Morimoto ret = devm_snd_soc_register_component(&hdev->dev, 5986bae5ea9SRakesh Ughreja &hdac_hda_codec, hdac_hda_dais, 5996bae5ea9SRakesh Ughreja ARRAY_SIZE(hdac_hda_dais)); 6006bae5ea9SRakesh Ughreja if (ret < 0) { 6016bae5ea9SRakesh Ughreja dev_err(&hdev->dev, "failed to register HDA codec %d\n", ret); 6026bae5ea9SRakesh Ughreja return ret; 6036bae5ea9SRakesh Ughreja } 6046bae5ea9SRakesh Ughreja 6056bae5ea9SRakesh Ughreja dev_set_drvdata(&hdev->dev, hda_pvt); 6066bae5ea9SRakesh Ughreja snd_hdac_ext_bus_link_put(hdev->bus, hlink); 6076bae5ea9SRakesh Ughreja 6086bae5ea9SRakesh Ughreja return ret; 6096bae5ea9SRakesh Ughreja } 6106bae5ea9SRakesh Ughreja 6116bae5ea9SRakesh Ughreja static int hdac_hda_dev_remove(struct hdac_device *hdev) 6126bae5ea9SRakesh Ughreja { 613c3ec8ac8SKai Vehmanen /* 614c3ec8ac8SKai Vehmanen * Resources are freed in hdac_hda_codec_remove(). This 615c3ec8ac8SKai Vehmanen * function is kept to keep hda_codec_driver_remove() happy. 616c3ec8ac8SKai Vehmanen */ 6176bae5ea9SRakesh Ughreja return 0; 6186bae5ea9SRakesh Ughreja } 6196bae5ea9SRakesh Ughreja 6206bae5ea9SRakesh Ughreja static struct hdac_ext_bus_ops hdac_ops = { 6216bae5ea9SRakesh Ughreja .hdev_attach = hdac_hda_dev_probe, 6226bae5ea9SRakesh Ughreja .hdev_detach = hdac_hda_dev_remove, 6236bae5ea9SRakesh Ughreja }; 6246bae5ea9SRakesh Ughreja 6256bae5ea9SRakesh Ughreja struct hdac_ext_bus_ops *snd_soc_hdac_hda_get_ops(void) 6266bae5ea9SRakesh Ughreja { 6276bae5ea9SRakesh Ughreja return &hdac_ops; 6286bae5ea9SRakesh Ughreja } 6296bae5ea9SRakesh Ughreja EXPORT_SYMBOL_GPL(snd_soc_hdac_hda_get_ops); 6306bae5ea9SRakesh Ughreja 6316bae5ea9SRakesh Ughreja MODULE_LICENSE("GPL v2"); 6326bae5ea9SRakesh Ughreja MODULE_DESCRIPTION("ASoC Extensions for legacy HDA Drivers"); 6336bae5ea9SRakesh Ughreja MODULE_AUTHOR("Rakesh Ughreja<rakesh.a.ughreja@intel.com>"); 634