xref: /openbmc/linux/sound/soc/codecs/hdac_hda.c (revision 552b1a85)
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 },
1676bae5ea9SRakesh Ughreja 
1686bae5ea9SRakesh Ughreja };
1696bae5ea9SRakesh Ughreja 
1706bae5ea9SRakesh Ughreja static int hdac_hda_dai_set_tdm_slot(struct snd_soc_dai *dai,
1716bae5ea9SRakesh Ughreja 				     unsigned int tx_mask, unsigned int rx_mask,
1726bae5ea9SRakesh Ughreja 				     int slots, int slot_width)
1736bae5ea9SRakesh Ughreja {
1746bae5ea9SRakesh Ughreja 	struct snd_soc_component *component = dai->component;
1756bae5ea9SRakesh Ughreja 	struct hdac_hda_priv *hda_pvt;
1766bae5ea9SRakesh Ughreja 	struct hdac_hda_pcm *pcm;
1776bae5ea9SRakesh Ughreja 
1786bae5ea9SRakesh Ughreja 	hda_pvt = snd_soc_component_get_drvdata(component);
1796bae5ea9SRakesh Ughreja 	pcm = &hda_pvt->pcm[dai->id];
180608b8c36SKai Vehmanen 
1816bae5ea9SRakesh Ughreja 	if (tx_mask)
182608b8c36SKai Vehmanen 		pcm->stream_tag[SNDRV_PCM_STREAM_PLAYBACK] = tx_mask;
1836bae5ea9SRakesh Ughreja 	else
184608b8c36SKai Vehmanen 		pcm->stream_tag[SNDRV_PCM_STREAM_CAPTURE] = rx_mask;
1856bae5ea9SRakesh Ughreja 
1866bae5ea9SRakesh Ughreja 	return 0;
1876bae5ea9SRakesh Ughreja }
1886bae5ea9SRakesh Ughreja 
18903d0aa4dSRander Wang static int hdac_hda_dai_hw_params(struct snd_pcm_substream *substream,
19003d0aa4dSRander Wang 				  struct snd_pcm_hw_params *params,
19103d0aa4dSRander Wang 				  struct snd_soc_dai *dai)
19203d0aa4dSRander Wang {
19303d0aa4dSRander Wang 	struct snd_soc_component *component = dai->component;
19403d0aa4dSRander Wang 	struct hdac_hda_priv *hda_pvt;
19503d0aa4dSRander Wang 	unsigned int format_val;
19603d0aa4dSRander Wang 	unsigned int maxbps;
19703d0aa4dSRander Wang 
19803d0aa4dSRander Wang 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
19903d0aa4dSRander Wang 		maxbps = dai->driver->playback.sig_bits;
20003d0aa4dSRander Wang 	else
20103d0aa4dSRander Wang 		maxbps = dai->driver->capture.sig_bits;
20203d0aa4dSRander Wang 
20303d0aa4dSRander Wang 	hda_pvt = snd_soc_component_get_drvdata(component);
20403d0aa4dSRander Wang 	format_val = snd_hdac_calc_stream_format(params_rate(params),
20503d0aa4dSRander Wang 						 params_channels(params),
20603d0aa4dSRander Wang 						 params_format(params),
20703d0aa4dSRander Wang 						 maxbps,
20803d0aa4dSRander Wang 						 0);
20903d0aa4dSRander Wang 	if (!format_val) {
21003d0aa4dSRander Wang 		dev_err(dai->dev,
21103d0aa4dSRander Wang 			"invalid format_val, rate=%d, ch=%d, format=%d, maxbps=%d\n",
21203d0aa4dSRander Wang 			params_rate(params), params_channels(params),
21303d0aa4dSRander Wang 			params_format(params), maxbps);
21403d0aa4dSRander Wang 
21503d0aa4dSRander Wang 		return -EINVAL;
21603d0aa4dSRander Wang 	}
21703d0aa4dSRander Wang 
21803d0aa4dSRander Wang 	hda_pvt->pcm[dai->id].format_val[substream->stream] = format_val;
21903d0aa4dSRander Wang 	return 0;
22003d0aa4dSRander Wang }
22103d0aa4dSRander Wang 
2226bae5ea9SRakesh Ughreja static int hdac_hda_dai_hw_free(struct snd_pcm_substream *substream,
2236bae5ea9SRakesh Ughreja 				struct snd_soc_dai *dai)
2246bae5ea9SRakesh Ughreja {
2256bae5ea9SRakesh Ughreja 	struct snd_soc_component *component = dai->component;
2266bae5ea9SRakesh Ughreja 	struct hdac_hda_priv *hda_pvt;
2276bae5ea9SRakesh Ughreja 	struct hda_pcm_stream *hda_stream;
2286bae5ea9SRakesh Ughreja 	struct hda_pcm *pcm;
2296bae5ea9SRakesh Ughreja 
2306bae5ea9SRakesh Ughreja 	hda_pvt = snd_soc_component_get_drvdata(component);
2316bae5ea9SRakesh Ughreja 	pcm = snd_soc_find_pcm_from_dai(hda_pvt, dai);
2326bae5ea9SRakesh Ughreja 	if (!pcm)
2336bae5ea9SRakesh Ughreja 		return -EINVAL;
2346bae5ea9SRakesh Ughreja 
2356bae5ea9SRakesh Ughreja 	hda_stream = &pcm->stream[substream->stream];
2366bae5ea9SRakesh Ughreja 	snd_hda_codec_cleanup(&hda_pvt->codec, hda_stream, substream);
2376bae5ea9SRakesh Ughreja 
2386bae5ea9SRakesh Ughreja 	return 0;
2396bae5ea9SRakesh Ughreja }
2406bae5ea9SRakesh Ughreja 
2416bae5ea9SRakesh Ughreja static int hdac_hda_dai_prepare(struct snd_pcm_substream *substream,
2426bae5ea9SRakesh Ughreja 				struct snd_soc_dai *dai)
2436bae5ea9SRakesh Ughreja {
2446bae5ea9SRakesh Ughreja 	struct snd_soc_component *component = dai->component;
2456bae5ea9SRakesh Ughreja 	struct hda_pcm_stream *hda_stream;
24603d0aa4dSRander Wang 	struct hdac_hda_priv *hda_pvt;
24703d0aa4dSRander Wang 	struct hdac_device *hdev;
2486bae5ea9SRakesh Ughreja 	unsigned int format_val;
2496bae5ea9SRakesh Ughreja 	struct hda_pcm *pcm;
2506bae5ea9SRakesh Ughreja 	unsigned int stream;
2516bae5ea9SRakesh Ughreja 	int ret = 0;
2526bae5ea9SRakesh Ughreja 
2536bae5ea9SRakesh Ughreja 	hda_pvt = snd_soc_component_get_drvdata(component);
2546bae5ea9SRakesh Ughreja 	hdev = &hda_pvt->codec.core;
2556bae5ea9SRakesh Ughreja 	pcm = snd_soc_find_pcm_from_dai(hda_pvt, dai);
2566bae5ea9SRakesh Ughreja 	if (!pcm)
2576bae5ea9SRakesh Ughreja 		return -EINVAL;
2586bae5ea9SRakesh Ughreja 
2596bae5ea9SRakesh Ughreja 	hda_stream = &pcm->stream[substream->stream];
2606bae5ea9SRakesh Ughreja 
2616bae5ea9SRakesh Ughreja 	stream = hda_pvt->pcm[dai->id].stream_tag[substream->stream];
26203d0aa4dSRander Wang 	format_val = hda_pvt->pcm[dai->id].format_val[substream->stream];
2636bae5ea9SRakesh Ughreja 
2646bae5ea9SRakesh Ughreja 	ret = snd_hda_codec_prepare(&hda_pvt->codec, hda_stream,
2656bae5ea9SRakesh Ughreja 				    stream, format_val, substream);
2666bae5ea9SRakesh Ughreja 	if (ret < 0)
2676bae5ea9SRakesh Ughreja 		dev_err(&hdev->dev, "codec prepare failed %d\n", ret);
2686bae5ea9SRakesh Ughreja 
2696bae5ea9SRakesh Ughreja 	return ret;
2706bae5ea9SRakesh Ughreja }
2716bae5ea9SRakesh Ughreja 
2726bae5ea9SRakesh Ughreja static int hdac_hda_dai_open(struct snd_pcm_substream *substream,
2736bae5ea9SRakesh Ughreja 			     struct snd_soc_dai *dai)
2746bae5ea9SRakesh Ughreja {
2756bae5ea9SRakesh Ughreja 	struct snd_soc_component *component = dai->component;
2766bae5ea9SRakesh Ughreja 	struct hdac_hda_priv *hda_pvt;
2776bae5ea9SRakesh Ughreja 	struct hda_pcm_stream *hda_stream;
2786bae5ea9SRakesh Ughreja 	struct hda_pcm *pcm;
2796bae5ea9SRakesh Ughreja 	int ret;
2806bae5ea9SRakesh Ughreja 
2816bae5ea9SRakesh Ughreja 	hda_pvt = snd_soc_component_get_drvdata(component);
2826bae5ea9SRakesh Ughreja 	pcm = snd_soc_find_pcm_from_dai(hda_pvt, dai);
2836bae5ea9SRakesh Ughreja 	if (!pcm)
2846bae5ea9SRakesh Ughreja 		return -EINVAL;
2856bae5ea9SRakesh Ughreja 
2866bae5ea9SRakesh Ughreja 	snd_hda_codec_pcm_get(pcm);
2876bae5ea9SRakesh Ughreja 
2886bae5ea9SRakesh Ughreja 	hda_stream = &pcm->stream[substream->stream];
2896bae5ea9SRakesh Ughreja 
2906bae5ea9SRakesh Ughreja 	ret = hda_stream->ops.open(hda_stream, &hda_pvt->codec, substream);
2916bae5ea9SRakesh Ughreja 	if (ret < 0)
2926bae5ea9SRakesh Ughreja 		snd_hda_codec_pcm_put(pcm);
2936bae5ea9SRakesh Ughreja 
2946bae5ea9SRakesh Ughreja 	return ret;
2956bae5ea9SRakesh Ughreja }
2966bae5ea9SRakesh Ughreja 
2976bae5ea9SRakesh Ughreja static void hdac_hda_dai_close(struct snd_pcm_substream *substream,
2986bae5ea9SRakesh Ughreja 			       struct snd_soc_dai *dai)
2996bae5ea9SRakesh Ughreja {
3006bae5ea9SRakesh Ughreja 	struct snd_soc_component *component = dai->component;
3016bae5ea9SRakesh Ughreja 	struct hdac_hda_priv *hda_pvt;
3026bae5ea9SRakesh Ughreja 	struct hda_pcm_stream *hda_stream;
3036bae5ea9SRakesh Ughreja 	struct hda_pcm *pcm;
3046bae5ea9SRakesh Ughreja 
3056bae5ea9SRakesh Ughreja 	hda_pvt = snd_soc_component_get_drvdata(component);
3066bae5ea9SRakesh Ughreja 	pcm = snd_soc_find_pcm_from_dai(hda_pvt, dai);
3076bae5ea9SRakesh Ughreja 	if (!pcm)
3086bae5ea9SRakesh Ughreja 		return;
3096bae5ea9SRakesh Ughreja 
3106bae5ea9SRakesh Ughreja 	hda_stream = &pcm->stream[substream->stream];
3116bae5ea9SRakesh Ughreja 
3126bae5ea9SRakesh Ughreja 	hda_stream->ops.close(hda_stream, &hda_pvt->codec, substream);
3136bae5ea9SRakesh Ughreja 
3146bae5ea9SRakesh Ughreja 	snd_hda_codec_pcm_put(pcm);
3156bae5ea9SRakesh Ughreja }
3166bae5ea9SRakesh Ughreja 
3176bae5ea9SRakesh Ughreja static struct hda_pcm *snd_soc_find_pcm_from_dai(struct hdac_hda_priv *hda_pvt,
3186bae5ea9SRakesh Ughreja 						 struct snd_soc_dai *dai)
3196bae5ea9SRakesh Ughreja {
3206bae5ea9SRakesh Ughreja 	struct hda_codec *hcodec = &hda_pvt->codec;
3216bae5ea9SRakesh Ughreja 	struct hda_pcm *cpcm;
3226bae5ea9SRakesh Ughreja 	const char *pcm_name;
3236bae5ea9SRakesh Ughreja 
324608b8c36SKai Vehmanen 	/*
325608b8c36SKai Vehmanen 	 * map DAI ID to the closest matching PCM name, using the naming
326608b8c36SKai Vehmanen 	 * scheme used by hda-codec snd_hda_gen_build_pcms() and for
327608b8c36SKai Vehmanen 	 * HDMI in hda_codec patch_hdmi.c)
328608b8c36SKai Vehmanen 	 */
329608b8c36SKai Vehmanen 
3306bae5ea9SRakesh Ughreja 	switch (dai->id) {
3316bae5ea9SRakesh Ughreja 	case HDAC_ANALOG_DAI_ID:
3326bae5ea9SRakesh Ughreja 		pcm_name = "Analog";
3336bae5ea9SRakesh Ughreja 		break;
3346bae5ea9SRakesh Ughreja 	case HDAC_DIGITAL_DAI_ID:
3356bae5ea9SRakesh Ughreja 		pcm_name = "Digital";
3366bae5ea9SRakesh Ughreja 		break;
3376bae5ea9SRakesh Ughreja 	case HDAC_ALT_ANALOG_DAI_ID:
3386bae5ea9SRakesh Ughreja 		pcm_name = "Alt Analog";
3396bae5ea9SRakesh Ughreja 		break;
340608b8c36SKai Vehmanen 	case HDAC_HDMI_0_DAI_ID:
341608b8c36SKai Vehmanen 		pcm_name = "HDMI 0";
342608b8c36SKai Vehmanen 		break;
343608b8c36SKai Vehmanen 	case HDAC_HDMI_1_DAI_ID:
344608b8c36SKai Vehmanen 		pcm_name = "HDMI 1";
345608b8c36SKai Vehmanen 		break;
346608b8c36SKai Vehmanen 	case HDAC_HDMI_2_DAI_ID:
347608b8c36SKai Vehmanen 		pcm_name = "HDMI 2";
348608b8c36SKai Vehmanen 		break;
3496bae5ea9SRakesh Ughreja 	default:
3506bae5ea9SRakesh Ughreja 		dev_err(&hcodec->core.dev, "invalid dai id %d\n", dai->id);
3516bae5ea9SRakesh Ughreja 		return NULL;
3526bae5ea9SRakesh Ughreja 	}
3536bae5ea9SRakesh Ughreja 
3546bae5ea9SRakesh Ughreja 	list_for_each_entry(cpcm, &hcodec->pcm_list_head, list) {
355608b8c36SKai Vehmanen 		if (strstr(cpcm->name, pcm_name))
3566bae5ea9SRakesh Ughreja 			return cpcm;
3576bae5ea9SRakesh Ughreja 	}
3586bae5ea9SRakesh Ughreja 
3596bae5ea9SRakesh Ughreja 	dev_err(&hcodec->core.dev, "didn't find PCM for DAI %s\n", dai->name);
3606bae5ea9SRakesh Ughreja 	return NULL;
3616bae5ea9SRakesh Ughreja }
3626bae5ea9SRakesh Ughreja 
363608b8c36SKai Vehmanen static bool is_hdmi_codec(struct hda_codec *hcodec)
364608b8c36SKai Vehmanen {
365608b8c36SKai Vehmanen 	struct hda_pcm *cpcm;
366608b8c36SKai Vehmanen 
367608b8c36SKai Vehmanen 	list_for_each_entry(cpcm, &hcodec->pcm_list_head, list) {
368608b8c36SKai Vehmanen 		if (cpcm->pcm_type == HDA_PCM_TYPE_HDMI)
369608b8c36SKai Vehmanen 			return true;
370608b8c36SKai Vehmanen 	}
371608b8c36SKai Vehmanen 
372608b8c36SKai Vehmanen 	return false;
373608b8c36SKai Vehmanen }
374608b8c36SKai Vehmanen 
3756bae5ea9SRakesh Ughreja static int hdac_hda_codec_probe(struct snd_soc_component *component)
3766bae5ea9SRakesh Ughreja {
3776bae5ea9SRakesh Ughreja 	struct hdac_hda_priv *hda_pvt =
3786bae5ea9SRakesh Ughreja 			snd_soc_component_get_drvdata(component);
3796bae5ea9SRakesh Ughreja 	struct snd_soc_dapm_context *dapm =
3806bae5ea9SRakesh Ughreja 			snd_soc_component_get_dapm(component);
3816bae5ea9SRakesh Ughreja 	struct hdac_device *hdev = &hda_pvt->codec.core;
3826bae5ea9SRakesh Ughreja 	struct hda_codec *hcodec = &hda_pvt->codec;
3836bae5ea9SRakesh Ughreja 	struct hdac_ext_link *hlink;
3846bae5ea9SRakesh Ughreja 	hda_codec_patch_t patch;
3856bae5ea9SRakesh Ughreja 	int ret;
3866bae5ea9SRakesh Ughreja 
3876bae5ea9SRakesh Ughreja 	hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev));
3886bae5ea9SRakesh Ughreja 	if (!hlink) {
3896bae5ea9SRakesh Ughreja 		dev_err(&hdev->dev, "hdac link not found\n");
3906bae5ea9SRakesh Ughreja 		return -EIO;
3916bae5ea9SRakesh Ughreja 	}
3926bae5ea9SRakesh Ughreja 
3936bae5ea9SRakesh Ughreja 	snd_hdac_ext_bus_link_get(hdev->bus, hlink);
3946bae5ea9SRakesh Ughreja 
395608b8c36SKai Vehmanen 	/*
396608b8c36SKai Vehmanen 	 * Ensure any HDA display is powered at codec probe.
397608b8c36SKai Vehmanen 	 * After snd_hda_codec_device_new(), display power is
398608b8c36SKai Vehmanen 	 * managed by runtime PM.
399608b8c36SKai Vehmanen 	 */
400608b8c36SKai Vehmanen 	if (hda_pvt->need_display_power)
401608b8c36SKai Vehmanen 		snd_hdac_display_power(hdev->bus,
402608b8c36SKai Vehmanen 				       HDA_CODEC_IDX_CONTROLLER, true);
403608b8c36SKai Vehmanen 
4046bae5ea9SRakesh Ughreja 	ret = snd_hda_codec_device_new(hcodec->bus, component->card->snd_card,
4056bae5ea9SRakesh Ughreja 				       hdev->addr, hcodec);
4066bae5ea9SRakesh Ughreja 	if (ret < 0) {
4076bae5ea9SRakesh Ughreja 		dev_err(&hdev->dev, "failed to create hda codec %d\n", ret);
4086bae5ea9SRakesh Ughreja 		goto error_no_pm;
4096bae5ea9SRakesh Ughreja 	}
410b60ee2e2SBard liao 	/*
411b60ee2e2SBard liao 	 * Overwrite type to HDA_DEV_ASOC since it is a ASoC driver
412b60ee2e2SBard liao 	 * hda_codec.c will check this flag to determine if unregister
413b60ee2e2SBard liao 	 * device is needed.
414b60ee2e2SBard liao 	 */
415b60ee2e2SBard liao 	hdev->type = HDA_DEV_ASOC;
4166bae5ea9SRakesh Ughreja 
4176bae5ea9SRakesh Ughreja 	/*
4186bae5ea9SRakesh Ughreja 	 * snd_hda_codec_device_new decrements the usage count so call get pm
4196bae5ea9SRakesh Ughreja 	 * else the device will be powered off
4206bae5ea9SRakesh Ughreja 	 */
4216bae5ea9SRakesh Ughreja 	pm_runtime_get_noresume(&hdev->dev);
4226bae5ea9SRakesh Ughreja 
4236bae5ea9SRakesh Ughreja 	hcodec->bus->card = dapm->card->snd_card;
4246bae5ea9SRakesh Ughreja 
4256bae5ea9SRakesh Ughreja 	ret = snd_hda_codec_set_name(hcodec, hcodec->preset->name);
4266bae5ea9SRakesh Ughreja 	if (ret < 0) {
4276bae5ea9SRakesh Ughreja 		dev_err(&hdev->dev, "name failed %s\n", hcodec->preset->name);
4286bae5ea9SRakesh Ughreja 		goto error;
4296bae5ea9SRakesh Ughreja 	}
4306bae5ea9SRakesh Ughreja 
4316bae5ea9SRakesh Ughreja 	ret = snd_hdac_regmap_init(&hcodec->core);
4326bae5ea9SRakesh Ughreja 	if (ret < 0) {
4336bae5ea9SRakesh Ughreja 		dev_err(&hdev->dev, "regmap init failed\n");
4346bae5ea9SRakesh Ughreja 		goto error;
4356bae5ea9SRakesh Ughreja 	}
4366bae5ea9SRakesh Ughreja 
4376bae5ea9SRakesh Ughreja 	patch = (hda_codec_patch_t)hcodec->preset->driver_data;
4386bae5ea9SRakesh Ughreja 	if (patch) {
4396bae5ea9SRakesh Ughreja 		ret = patch(hcodec);
4406bae5ea9SRakesh Ughreja 		if (ret < 0) {
4416bae5ea9SRakesh Ughreja 			dev_err(&hdev->dev, "patch failed %d\n", ret);
4426bae5ea9SRakesh Ughreja 			goto error;
4436bae5ea9SRakesh Ughreja 		}
4446bae5ea9SRakesh Ughreja 	} else {
4456bae5ea9SRakesh Ughreja 		dev_dbg(&hdev->dev, "no patch file found\n");
4466bae5ea9SRakesh Ughreja 	}
4476bae5ea9SRakesh Ughreja 
448608b8c36SKai Vehmanen 	/* configure codec for 1:1 PCM:DAI mapping */
449608b8c36SKai Vehmanen 	hcodec->mst_no_extra_pcms = 1;
450608b8c36SKai Vehmanen 
4516bae5ea9SRakesh Ughreja 	ret = snd_hda_codec_parse_pcms(hcodec);
4526bae5ea9SRakesh Ughreja 	if (ret < 0) {
4536bae5ea9SRakesh Ughreja 		dev_err(&hdev->dev, "unable to map pcms to dai %d\n", ret);
4546bae5ea9SRakesh Ughreja 		goto error;
4556bae5ea9SRakesh Ughreja 	}
4566bae5ea9SRakesh Ughreja 
457608b8c36SKai Vehmanen 	/* HDMI controls need to be created in machine drivers */
458608b8c36SKai Vehmanen 	if (!is_hdmi_codec(hcodec)) {
4596bae5ea9SRakesh Ughreja 		ret = snd_hda_codec_build_controls(hcodec);
4606bae5ea9SRakesh Ughreja 		if (ret < 0) {
461608b8c36SKai Vehmanen 			dev_err(&hdev->dev, "unable to create controls %d\n",
462608b8c36SKai Vehmanen 				ret);
4636bae5ea9SRakesh Ughreja 			goto error;
4646bae5ea9SRakesh Ughreja 		}
465608b8c36SKai Vehmanen 	}
4666bae5ea9SRakesh Ughreja 
4676bae5ea9SRakesh Ughreja 	hcodec->core.lazy_cache = true;
4686bae5ea9SRakesh Ughreja 
469608b8c36SKai Vehmanen 	if (hda_pvt->need_display_power)
470608b8c36SKai Vehmanen 		snd_hdac_display_power(hdev->bus,
471608b8c36SKai Vehmanen 				       HDA_CODEC_IDX_CONTROLLER, false);
472608b8c36SKai Vehmanen 
4736bae5ea9SRakesh Ughreja 	/*
4746bae5ea9SRakesh Ughreja 	 * hdac_device core already sets the state to active and calls
4756bae5ea9SRakesh Ughreja 	 * get_noresume. So enable runtime and set the device to suspend.
4766bae5ea9SRakesh Ughreja 	 * pm_runtime_enable is also called during codec registeration
4776bae5ea9SRakesh Ughreja 	 */
4786bae5ea9SRakesh Ughreja 	pm_runtime_put(&hdev->dev);
4796bae5ea9SRakesh Ughreja 	pm_runtime_suspend(&hdev->dev);
4806bae5ea9SRakesh Ughreja 
4816bae5ea9SRakesh Ughreja 	return 0;
4826bae5ea9SRakesh Ughreja 
4836bae5ea9SRakesh Ughreja error:
4846bae5ea9SRakesh Ughreja 	pm_runtime_put(&hdev->dev);
4856bae5ea9SRakesh Ughreja error_no_pm:
4866bae5ea9SRakesh Ughreja 	snd_hdac_ext_bus_link_put(hdev->bus, hlink);
4876bae5ea9SRakesh Ughreja 	return ret;
4886bae5ea9SRakesh Ughreja }
4896bae5ea9SRakesh Ughreja 
4906bae5ea9SRakesh Ughreja static void hdac_hda_codec_remove(struct snd_soc_component *component)
4916bae5ea9SRakesh Ughreja {
4926bae5ea9SRakesh Ughreja 	struct hdac_hda_priv *hda_pvt =
4936bae5ea9SRakesh Ughreja 		      snd_soc_component_get_drvdata(component);
4946bae5ea9SRakesh Ughreja 	struct hdac_device *hdev = &hda_pvt->codec.core;
4956bae5ea9SRakesh Ughreja 	struct hdac_ext_link *hlink = NULL;
4966bae5ea9SRakesh Ughreja 
4976bae5ea9SRakesh Ughreja 	hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev));
4986bae5ea9SRakesh Ughreja 	if (!hlink) {
4996bae5ea9SRakesh Ughreja 		dev_err(&hdev->dev, "hdac link not found\n");
5006bae5ea9SRakesh Ughreja 		return;
5016bae5ea9SRakesh Ughreja 	}
5026bae5ea9SRakesh Ughreja 
5036bae5ea9SRakesh Ughreja 	pm_runtime_disable(&hdev->dev);
5045dc7d5bcSKai Vehmanen 	snd_hdac_ext_bus_link_put(hdev->bus, hlink);
5056bae5ea9SRakesh Ughreja }
5066bae5ea9SRakesh Ughreja 
5076bae5ea9SRakesh Ughreja static const struct snd_soc_dapm_route hdac_hda_dapm_routes[] = {
5086bae5ea9SRakesh Ughreja 	{"AIF1TX", NULL, "Codec Input Pin1"},
5096bae5ea9SRakesh Ughreja 	{"AIF2TX", NULL, "Codec Input Pin2"},
5106bae5ea9SRakesh Ughreja 	{"AIF3TX", NULL, "Codec Input Pin3"},
5116bae5ea9SRakesh Ughreja 
5126bae5ea9SRakesh Ughreja 	{"Codec Output Pin1", NULL, "AIF1RX"},
5136bae5ea9SRakesh Ughreja 	{"Codec Output Pin2", NULL, "AIF2RX"},
5146bae5ea9SRakesh Ughreja 	{"Codec Output Pin3", NULL, "AIF3RX"},
5156bae5ea9SRakesh Ughreja };
5166bae5ea9SRakesh Ughreja 
5176bae5ea9SRakesh Ughreja static const struct snd_soc_dapm_widget hdac_hda_dapm_widgets[] = {
5186bae5ea9SRakesh Ughreja 	/* Audio Interface */
5196bae5ea9SRakesh Ughreja 	SND_SOC_DAPM_AIF_IN("AIF1RX", "Analog Codec Playback", 0,
5206bae5ea9SRakesh Ughreja 			    SND_SOC_NOPM, 0, 0),
5216bae5ea9SRakesh Ughreja 	SND_SOC_DAPM_AIF_IN("AIF2RX", "Digital Codec Playback", 0,
5226bae5ea9SRakesh Ughreja 			    SND_SOC_NOPM, 0, 0),
5236bae5ea9SRakesh Ughreja 	SND_SOC_DAPM_AIF_IN("AIF3RX", "Alt Analog Codec Playback", 0,
5246bae5ea9SRakesh Ughreja 			    SND_SOC_NOPM, 0, 0),
5256bae5ea9SRakesh Ughreja 	SND_SOC_DAPM_AIF_OUT("AIF1TX", "Analog Codec Capture", 0,
5266bae5ea9SRakesh Ughreja 			     SND_SOC_NOPM, 0, 0),
5276bae5ea9SRakesh Ughreja 	SND_SOC_DAPM_AIF_OUT("AIF2TX", "Digital Codec Capture", 0,
5286bae5ea9SRakesh Ughreja 			     SND_SOC_NOPM, 0, 0),
5296bae5ea9SRakesh Ughreja 	SND_SOC_DAPM_AIF_OUT("AIF3TX", "Alt Analog Codec Capture", 0,
5306bae5ea9SRakesh Ughreja 			     SND_SOC_NOPM, 0, 0),
5316bae5ea9SRakesh Ughreja 
5326bae5ea9SRakesh Ughreja 	/* Input Pins */
5336bae5ea9SRakesh Ughreja 	SND_SOC_DAPM_INPUT("Codec Input Pin1"),
5346bae5ea9SRakesh Ughreja 	SND_SOC_DAPM_INPUT("Codec Input Pin2"),
5356bae5ea9SRakesh Ughreja 	SND_SOC_DAPM_INPUT("Codec Input Pin3"),
5366bae5ea9SRakesh Ughreja 
5376bae5ea9SRakesh Ughreja 	/* Output Pins */
5386bae5ea9SRakesh Ughreja 	SND_SOC_DAPM_OUTPUT("Codec Output Pin1"),
5396bae5ea9SRakesh Ughreja 	SND_SOC_DAPM_OUTPUT("Codec Output Pin2"),
5406bae5ea9SRakesh Ughreja 	SND_SOC_DAPM_OUTPUT("Codec Output Pin3"),
5416bae5ea9SRakesh Ughreja };
5426bae5ea9SRakesh Ughreja 
5436bae5ea9SRakesh Ughreja static const struct snd_soc_component_driver hdac_hda_codec = {
5446bae5ea9SRakesh Ughreja 	.probe		= hdac_hda_codec_probe,
5456bae5ea9SRakesh Ughreja 	.remove		= hdac_hda_codec_remove,
5466bae5ea9SRakesh Ughreja 	.idle_bias_on	= false,
5476bae5ea9SRakesh Ughreja 	.dapm_widgets           = hdac_hda_dapm_widgets,
5486bae5ea9SRakesh Ughreja 	.num_dapm_widgets       = ARRAY_SIZE(hdac_hda_dapm_widgets),
5496bae5ea9SRakesh Ughreja 	.dapm_routes            = hdac_hda_dapm_routes,
5506bae5ea9SRakesh Ughreja 	.num_dapm_routes        = ARRAY_SIZE(hdac_hda_dapm_routes),
5516bae5ea9SRakesh Ughreja };
5526bae5ea9SRakesh Ughreja 
5536bae5ea9SRakesh Ughreja static int hdac_hda_dev_probe(struct hdac_device *hdev)
5546bae5ea9SRakesh Ughreja {
5556bae5ea9SRakesh Ughreja 	struct hdac_ext_link *hlink;
5566bae5ea9SRakesh Ughreja 	struct hdac_hda_priv *hda_pvt;
5576bae5ea9SRakesh Ughreja 	int ret;
5586bae5ea9SRakesh Ughreja 
5596bae5ea9SRakesh Ughreja 	/* hold the ref while we probe */
5606bae5ea9SRakesh Ughreja 	hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev));
5616bae5ea9SRakesh Ughreja 	if (!hlink) {
5626bae5ea9SRakesh Ughreja 		dev_err(&hdev->dev, "hdac link not found\n");
5636bae5ea9SRakesh Ughreja 		return -EIO;
5646bae5ea9SRakesh Ughreja 	}
5656bae5ea9SRakesh Ughreja 	snd_hdac_ext_bus_link_get(hdev->bus, hlink);
5666bae5ea9SRakesh Ughreja 
5676bae5ea9SRakesh Ughreja 	hda_pvt = hdac_to_hda_priv(hdev);
5686bae5ea9SRakesh Ughreja 	if (!hda_pvt)
5696bae5ea9SRakesh Ughreja 		return -ENOMEM;
5706bae5ea9SRakesh Ughreja 
5716bae5ea9SRakesh Ughreja 	/* ASoC specific initialization */
57210ccaa39SKuninori Morimoto 	ret = devm_snd_soc_register_component(&hdev->dev,
5736bae5ea9SRakesh Ughreja 					 &hdac_hda_codec, hdac_hda_dais,
5746bae5ea9SRakesh Ughreja 					 ARRAY_SIZE(hdac_hda_dais));
5756bae5ea9SRakesh Ughreja 	if (ret < 0) {
5766bae5ea9SRakesh Ughreja 		dev_err(&hdev->dev, "failed to register HDA codec %d\n", ret);
5776bae5ea9SRakesh Ughreja 		return ret;
5786bae5ea9SRakesh Ughreja 	}
5796bae5ea9SRakesh Ughreja 
5806bae5ea9SRakesh Ughreja 	dev_set_drvdata(&hdev->dev, hda_pvt);
5816bae5ea9SRakesh Ughreja 	snd_hdac_ext_bus_link_put(hdev->bus, hlink);
5826bae5ea9SRakesh Ughreja 
5836bae5ea9SRakesh Ughreja 	return ret;
5846bae5ea9SRakesh Ughreja }
5856bae5ea9SRakesh Ughreja 
5866bae5ea9SRakesh Ughreja static int hdac_hda_dev_remove(struct hdac_device *hdev)
5876bae5ea9SRakesh Ughreja {
588804cbf4bSKeyon Jie 	struct hdac_hda_priv *hda_pvt;
589804cbf4bSKeyon Jie 
590804cbf4bSKeyon Jie 	hda_pvt = dev_get_drvdata(&hdev->dev);
591552b1a85SKai Vehmanen 	if (hda_pvt && hda_pvt->codec.registered)
592804cbf4bSKeyon Jie 		cancel_delayed_work_sync(&hda_pvt->codec.jackpoll_work);
593552b1a85SKai Vehmanen 
5946bae5ea9SRakesh Ughreja 	return 0;
5956bae5ea9SRakesh Ughreja }
5966bae5ea9SRakesh Ughreja 
5976bae5ea9SRakesh Ughreja static struct hdac_ext_bus_ops hdac_ops = {
5986bae5ea9SRakesh Ughreja 	.hdev_attach = hdac_hda_dev_probe,
5996bae5ea9SRakesh Ughreja 	.hdev_detach = hdac_hda_dev_remove,
6006bae5ea9SRakesh Ughreja };
6016bae5ea9SRakesh Ughreja 
6026bae5ea9SRakesh Ughreja struct hdac_ext_bus_ops *snd_soc_hdac_hda_get_ops(void)
6036bae5ea9SRakesh Ughreja {
6046bae5ea9SRakesh Ughreja 	return &hdac_ops;
6056bae5ea9SRakesh Ughreja }
6066bae5ea9SRakesh Ughreja EXPORT_SYMBOL_GPL(snd_soc_hdac_hda_get_ops);
6076bae5ea9SRakesh Ughreja 
6086bae5ea9SRakesh Ughreja MODULE_LICENSE("GPL v2");
6096bae5ea9SRakesh Ughreja MODULE_DESCRIPTION("ASoC Extensions for legacy HDA Drivers");
6106bae5ea9SRakesh Ughreja MODULE_AUTHOR("Rakesh Ughreja<rakesh.a.ughreja@intel.com>");
611