xref: /linux/sound/soc/sof/intel/hda-codec.c (revision 67140b64b68395b79177efcd16a7530ed5311e49)
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2 //
3 // This file is provided under a dual BSD/GPLv2 license.  When using or
4 // redistributing this file, you may do so under either license.
5 //
6 // Copyright(c) 2018 Intel Corporation. All rights reserved.
7 //
8 // Authors: Keyon Jie <yang.jie@linux.intel.com>
9 //
10 
11 #include <linux/module.h>
12 #include <sound/hdaudio_ext.h>
13 #include <sound/hda_register.h>
14 #include <sound/hda_codec.h>
15 #include <sound/hda_i915.h>
16 #include <sound/sof.h>
17 #include "../ops.h"
18 #include "hda.h"
19 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
20 #include "../../codecs/hdac_hda.h"
21 #endif /* CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC */
22 
23 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
24 #define IDISP_VID_INTEL	0x80860000
25 #define CODEC_PROBE_RETRIES 3
26 
27 /* load the legacy HDA codec driver */
28 static int request_codec_module(struct hda_codec *codec)
29 {
30 #ifdef MODULE
31 	char alias[MODULE_NAME_LEN];
32 	const char *mod = NULL;
33 
34 	switch (codec->probe_id) {
35 	case HDA_CODEC_ID_GENERIC:
36 #if IS_MODULE(CONFIG_SND_HDA_GENERIC)
37 		mod = "snd-hda-codec-generic";
38 #endif
39 		break;
40 	default:
41 		snd_hdac_codec_modalias(&codec->core, alias, sizeof(alias));
42 		mod = alias;
43 		break;
44 	}
45 
46 	if (mod) {
47 		dev_dbg(&codec->core.dev, "loading codec module: %s\n", mod);
48 		request_module(mod);
49 	}
50 #endif /* MODULE */
51 	return device_attach(hda_codec_dev(codec));
52 }
53 
54 static int hda_codec_load_module(struct hda_codec *codec)
55 {
56 	int ret = request_codec_module(codec);
57 
58 	if (ret <= 0) {
59 		codec->probe_id = HDA_CODEC_ID_GENERIC;
60 		ret = request_codec_module(codec);
61 	}
62 
63 	return ret;
64 }
65 
66 /* enable controller wake up event for all codecs with jack connectors */
67 void hda_codec_jack_wake_enable(struct snd_sof_dev *sdev, bool enable)
68 {
69 	struct hda_bus *hbus = sof_to_hbus(sdev);
70 	struct hdac_bus *bus = sof_to_bus(sdev);
71 	struct hda_codec *codec;
72 	unsigned int mask = 0;
73 
74 	if (enable) {
75 		list_for_each_codec(codec, hbus)
76 			if (codec->jacktbl.used)
77 				mask |= BIT(codec->core.addr);
78 	}
79 
80 	snd_hdac_chip_updatew(bus, WAKEEN, STATESTS_INT_MASK, mask);
81 }
82 
83 /* check jack status after resuming from suspend mode */
84 void hda_codec_jack_check(struct snd_sof_dev *sdev)
85 {
86 	struct hda_bus *hbus = sof_to_hbus(sdev);
87 	struct hda_codec *codec;
88 
89 	list_for_each_codec(codec, hbus)
90 		/*
91 		 * Wake up all jack-detecting codecs regardless whether an event
92 		 * has been recorded in STATESTS
93 		 */
94 		if (codec->jacktbl.used)
95 			pm_request_resume(&codec->core.dev);
96 }
97 #else
98 void hda_codec_jack_wake_enable(struct snd_sof_dev *sdev, bool enable) {}
99 void hda_codec_jack_check(struct snd_sof_dev *sdev) {}
100 #endif /* CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC */
101 EXPORT_SYMBOL_NS(hda_codec_jack_wake_enable, SND_SOC_SOF_HDA_AUDIO_CODEC);
102 EXPORT_SYMBOL_NS(hda_codec_jack_check, SND_SOC_SOF_HDA_AUDIO_CODEC);
103 
104 #if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
105 #define is_generic_config(bus) \
106 	((bus)->modelname && !strcmp((bus)->modelname, "generic"))
107 #else
108 #define is_generic_config(x)	0
109 #endif
110 
111 /* probe individual codec */
112 static int hda_codec_probe(struct snd_sof_dev *sdev, int address,
113 			   bool hda_codec_use_common_hdmi)
114 {
115 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
116 	struct hdac_hda_priv *hda_priv;
117 	struct hda_codec *codec;
118 	int type = HDA_DEV_LEGACY;
119 #endif
120 	struct hda_bus *hbus = sof_to_hbus(sdev);
121 	struct hdac_device *hdev;
122 	u32 hda_cmd = (address << 28) | (AC_NODE_ROOT << 20) |
123 		(AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID;
124 	u32 resp = -1;
125 	int ret, retry = 0;
126 
127 	do {
128 		mutex_lock(&hbus->core.cmd_mutex);
129 		snd_hdac_bus_send_cmd(&hbus->core, hda_cmd);
130 		snd_hdac_bus_get_response(&hbus->core, address, &resp);
131 		mutex_unlock(&hbus->core.cmd_mutex);
132 	} while (resp == -1 && retry++ < CODEC_PROBE_RETRIES);
133 
134 	if (resp == -1)
135 		return -EIO;
136 	dev_dbg(sdev->dev, "HDA codec #%d probed OK: response: %x\n",
137 		address, resp);
138 
139 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
140 	hda_priv = devm_kzalloc(sdev->dev, sizeof(*hda_priv), GFP_KERNEL);
141 	if (!hda_priv)
142 		return -ENOMEM;
143 
144 	hda_priv->codec.bus = hbus;
145 	hdev = &hda_priv->codec.core;
146 	codec = &hda_priv->codec;
147 
148 	/* only probe ASoC codec drivers for HDAC-HDMI */
149 	if (!hda_codec_use_common_hdmi && (resp & 0xFFFF0000) == IDISP_VID_INTEL)
150 		type = HDA_DEV_ASOC;
151 
152 	ret = snd_hdac_ext_bus_device_init(&hbus->core, address, hdev, type);
153 	if (ret < 0)
154 		return ret;
155 
156 	if ((resp & 0xFFFF0000) == IDISP_VID_INTEL) {
157 		if (!hdev->bus->audio_component) {
158 			dev_dbg(sdev->dev,
159 				"iDisp hw present but no driver\n");
160 			ret = -ENOENT;
161 			goto out;
162 		}
163 		hda_priv->need_display_power = true;
164 	}
165 
166 	if (is_generic_config(hbus))
167 		codec->probe_id = HDA_CODEC_ID_GENERIC;
168 	else
169 		codec->probe_id = 0;
170 
171 	if (type == HDA_DEV_LEGACY) {
172 		ret = hda_codec_load_module(codec);
173 		/*
174 		 * handle ret==0 (no driver bound) as an error, but pass
175 		 * other return codes without modification
176 		 */
177 		if (ret == 0)
178 			ret = -ENOENT;
179 	}
180 
181 out:
182 	if (ret < 0) {
183 		snd_hdac_device_unregister(hdev);
184 		put_device(&hdev->dev);
185 	}
186 #else
187 	hdev = devm_kzalloc(sdev->dev, sizeof(*hdev), GFP_KERNEL);
188 	if (!hdev)
189 		return -ENOMEM;
190 
191 	ret = snd_hdac_ext_bus_device_init(&hbus->core, address, hdev, HDA_DEV_ASOC);
192 #endif
193 
194 	return ret;
195 }
196 
197 /* Codec initialization */
198 void hda_codec_probe_bus(struct snd_sof_dev *sdev,
199 			 bool hda_codec_use_common_hdmi)
200 {
201 	struct hdac_bus *bus = sof_to_bus(sdev);
202 	int i, ret;
203 
204 	/* probe codecs in avail slots */
205 	for (i = 0; i < HDA_MAX_CODECS; i++) {
206 
207 		if (!(bus->codec_mask & (1 << i)))
208 			continue;
209 
210 		ret = hda_codec_probe(sdev, i, hda_codec_use_common_hdmi);
211 		if (ret < 0) {
212 			dev_warn(bus->dev, "codec #%d probe error, ret: %d\n",
213 				 i, ret);
214 			bus->codec_mask &= ~BIT(i);
215 		}
216 	}
217 }
218 EXPORT_SYMBOL_NS(hda_codec_probe_bus, SND_SOC_SOF_HDA_AUDIO_CODEC);
219 
220 #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI) || \
221 	IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI)
222 
223 void hda_codec_i915_display_power(struct snd_sof_dev *sdev, bool enable)
224 {
225 	struct hdac_bus *bus = sof_to_bus(sdev);
226 
227 	if (HDA_IDISP_CODEC(bus->codec_mask)) {
228 		dev_dbg(bus->dev, "Turning i915 HDAC power %d\n", enable);
229 		snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, enable);
230 	}
231 }
232 EXPORT_SYMBOL_NS(hda_codec_i915_display_power, SND_SOC_SOF_HDA_AUDIO_CODEC_I915);
233 
234 int hda_codec_i915_init(struct snd_sof_dev *sdev)
235 {
236 	struct hdac_bus *bus = sof_to_bus(sdev);
237 	int ret;
238 
239 	/* i915 exposes a HDA codec for HDMI audio */
240 	ret = snd_hdac_i915_init(bus);
241 	if (ret < 0)
242 		return ret;
243 
244 	/* codec_mask not yet known, power up for probe */
245 	snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, true);
246 
247 	return 0;
248 }
249 EXPORT_SYMBOL_NS(hda_codec_i915_init, SND_SOC_SOF_HDA_AUDIO_CODEC_I915);
250 
251 int hda_codec_i915_exit(struct snd_sof_dev *sdev)
252 {
253 	struct hdac_bus *bus = sof_to_bus(sdev);
254 
255 	if (!bus->audio_component)
256 		return 0;
257 
258 	/* power down unconditionally */
259 	snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, false);
260 
261 	return snd_hdac_i915_exit(bus);
262 }
263 EXPORT_SYMBOL_NS(hda_codec_i915_exit, SND_SOC_SOF_HDA_AUDIO_CODEC_I915);
264 
265 #endif
266 
267 MODULE_LICENSE("Dual BSD/GPL");
268