xref: /openbmc/linux/sound/soc/sof/intel/hda-codec.c (revision 1c0036e03edd5d97fc0af94dd3ab7e8c58b8191d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright(c) 2018 Intel Corporation. All rights reserved.
4 //
5 // Authors: Keyon Jie <yang.jie@linux.intel.com>
6 //
7 
8 #include <linux/module.h>
9 #include <sound/hdaudio_ext.h>
10 #include <sound/hda_register.h>
11 #include <sound/hda_codec.h>
12 #include <sound/hda_i915.h>
13 #include <sound/sof.h>
14 #include "../ops.h"
15 #include "hda.h"
16 
17 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
18 #include "../../codecs/hdac_hda.h"
19 
20 #define CODEC_PROBE_RETRIES	3
21 
22 #define IDISP_VID_INTEL	0x80860000
23 
24 static int hda_codec_mask = -1;
25 module_param_named(codec_mask, hda_codec_mask, int, 0444);
26 MODULE_PARM_DESC(codec_mask, "SOF HDA codec mask for probing");
27 
28 /* load the legacy HDA codec driver */
29 static int request_codec_module(struct hda_codec *codec)
30 {
31 #ifdef MODULE
32 	char alias[MODULE_NAME_LEN];
33 	const char *mod = NULL;
34 
35 	switch (codec->probe_id) {
36 	case HDA_CODEC_ID_GENERIC:
37 #if IS_MODULE(CONFIG_SND_HDA_GENERIC)
38 		mod = "snd-hda-codec-generic";
39 #endif
40 		break;
41 	default:
42 		snd_hdac_codec_modalias(&codec->core, alias, sizeof(alias));
43 		mod = alias;
44 		break;
45 	}
46 
47 	if (mod) {
48 		dev_dbg(&codec->core.dev, "loading codec module: %s\n", mod);
49 		request_module(mod);
50 	}
51 #endif /* MODULE */
52 	return device_attach(hda_codec_dev(codec));
53 }
54 
55 static int hda_codec_load_module(struct hda_codec *codec)
56 {
57 	int ret = request_codec_module(codec);
58 
59 	if (ret <= 0) {
60 		codec->probe_id = HDA_CODEC_ID_GENERIC;
61 		ret = request_codec_module(codec);
62 	}
63 
64 	return ret;
65 }
66 
67 /* enable controller wake up event for all codecs with jack connectors */
68 void hda_codec_jack_wake_enable(struct snd_sof_dev *sdev, bool enable)
69 {
70 	struct hda_bus *hbus = sof_to_hbus(sdev);
71 	struct hdac_bus *bus = sof_to_bus(sdev);
72 	struct hda_codec *codec;
73 	unsigned int mask = 0;
74 
75 	if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
76 	    sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
77 		return;
78 
79 	if (enable) {
80 		list_for_each_codec(codec, hbus)
81 			if (codec->jacktbl.used)
82 				mask |= BIT(codec->core.addr);
83 	}
84 
85 	snd_hdac_chip_updatew(bus, WAKEEN, STATESTS_INT_MASK, mask);
86 }
87 EXPORT_SYMBOL_NS_GPL(hda_codec_jack_wake_enable, SND_SOC_SOF_HDA_AUDIO_CODEC);
88 
89 /* check jack status after resuming from suspend mode */
90 void hda_codec_jack_check(struct snd_sof_dev *sdev)
91 {
92 	struct hda_bus *hbus = sof_to_hbus(sdev);
93 	struct hda_codec *codec;
94 
95 	if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
96 	    sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
97 		return;
98 
99 	list_for_each_codec(codec, hbus)
100 		/*
101 		 * Wake up all jack-detecting codecs regardless whether an event
102 		 * has been recorded in STATESTS
103 		 */
104 		if (codec->jacktbl.used)
105 			pm_request_resume(&codec->core.dev);
106 }
107 EXPORT_SYMBOL_NS_GPL(hda_codec_jack_check, SND_SOC_SOF_HDA_AUDIO_CODEC);
108 
109 #if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
110 #define is_generic_config(bus) \
111 	((bus)->modelname && !strcmp((bus)->modelname, "generic"))
112 #else
113 #define is_generic_config(x)	0
114 #endif
115 
116 static void hda_codec_device_exit(struct device *dev)
117 {
118 	snd_hdac_device_exit(dev_to_hdac_dev(dev));
119 }
120 
121 static struct hda_codec *hda_codec_device_init(struct hdac_bus *bus, int addr, int type)
122 {
123 	struct hda_codec *codec;
124 	int ret;
125 
126 	codec = snd_hda_codec_device_init(to_hda_bus(bus), addr, "ehdaudio%dD%d", bus->idx, addr);
127 	if (IS_ERR(codec)) {
128 		dev_err(bus->dev, "device init failed for hdac device\n");
129 		return codec;
130 	}
131 
132 	codec->core.type = type;
133 	codec->core.dev.release = hda_codec_device_exit;
134 
135 	ret = snd_hdac_device_register(&codec->core);
136 	if (ret) {
137 		dev_err(bus->dev, "failed to register hdac device\n");
138 		snd_hdac_device_exit(&codec->core);
139 		return ERR_PTR(ret);
140 	}
141 
142 	return codec;
143 }
144 
145 /* probe individual codec */
146 static int hda_codec_probe(struct snd_sof_dev *sdev, int address)
147 {
148 	struct hdac_hda_priv *hda_priv;
149 	struct hda_bus *hbus = sof_to_hbus(sdev);
150 	struct hda_codec *codec;
151 	u32 hda_cmd = (address << 28) | (AC_NODE_ROOT << 20) |
152 		(AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID;
153 	u32 resp = -1;
154 	int ret, retry = 0;
155 
156 	do {
157 		mutex_lock(&hbus->core.cmd_mutex);
158 		snd_hdac_bus_send_cmd(&hbus->core, hda_cmd);
159 		snd_hdac_bus_get_response(&hbus->core, address, &resp);
160 		mutex_unlock(&hbus->core.cmd_mutex);
161 	} while (resp == -1 && retry++ < CODEC_PROBE_RETRIES);
162 
163 	if (resp == -1)
164 		return -EIO;
165 	dev_dbg(sdev->dev, "HDA codec #%d probed OK: response: %x\n",
166 		address, resp);
167 
168 	hda_priv = devm_kzalloc(sdev->dev, sizeof(*hda_priv), GFP_KERNEL);
169 	if (!hda_priv)
170 		return -ENOMEM;
171 
172 	codec = hda_codec_device_init(&hbus->core, address, HDA_DEV_LEGACY);
173 	ret = PTR_ERR_OR_ZERO(codec);
174 	if (ret < 0)
175 		return ret;
176 
177 	hda_priv->codec = codec;
178 	dev_set_drvdata(&codec->core.dev, hda_priv);
179 
180 	if ((resp & 0xFFFF0000) == IDISP_VID_INTEL) {
181 		if (!hbus->core.audio_component) {
182 			dev_dbg(sdev->dev,
183 				"iDisp hw present but no driver\n");
184 			ret = -ENOENT;
185 			goto out;
186 		}
187 		hda_priv->need_display_power = true;
188 	}
189 
190 	if (is_generic_config(hbus))
191 		codec->probe_id = HDA_CODEC_ID_GENERIC;
192 	else
193 		codec->probe_id = 0;
194 
195 	ret = hda_codec_load_module(codec);
196 	/*
197 	 * handle ret==0 (no driver bound) as an error, but pass
198 	 * other return codes without modification
199 	 */
200 	if (ret == 0)
201 		ret = -ENOENT;
202 
203 out:
204 	if (ret < 0) {
205 		snd_hdac_device_unregister(&codec->core);
206 		put_device(&codec->core.dev);
207 	}
208 
209 	return ret;
210 }
211 
212 /* Codec initialization */
213 void hda_codec_probe_bus(struct snd_sof_dev *sdev)
214 {
215 	struct hdac_bus *bus = sof_to_bus(sdev);
216 	int i, ret;
217 
218 	if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
219 	    sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
220 		return;
221 
222 	/* probe codecs in avail slots */
223 	for (i = 0; i < HDA_MAX_CODECS; i++) {
224 
225 		if (!(bus->codec_mask & (1 << i)))
226 			continue;
227 
228 		ret = hda_codec_probe(sdev, i);
229 		if (ret < 0) {
230 			dev_warn(bus->dev, "codec #%d probe error, ret: %d\n",
231 				 i, ret);
232 			bus->codec_mask &= ~BIT(i);
233 		}
234 	}
235 }
236 EXPORT_SYMBOL_NS_GPL(hda_codec_probe_bus, SND_SOC_SOF_HDA_AUDIO_CODEC);
237 
238 void hda_codec_check_for_state_change(struct snd_sof_dev *sdev)
239 {
240 	struct hdac_bus *bus = sof_to_bus(sdev);
241 	unsigned int codec_mask;
242 
243 	codec_mask = snd_hdac_chip_readw(bus, STATESTS);
244 	if (codec_mask) {
245 		hda_codec_jack_check(sdev);
246 		snd_hdac_chip_writew(bus, STATESTS, codec_mask);
247 	}
248 }
249 EXPORT_SYMBOL_NS_GPL(hda_codec_check_for_state_change, SND_SOC_SOF_HDA_AUDIO_CODEC);
250 
251 void hda_codec_detect_mask(struct snd_sof_dev *sdev)
252 {
253 	struct hdac_bus *bus = sof_to_bus(sdev);
254 
255 	if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
256 	    sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
257 		return;
258 
259 	/* Accept unsolicited responses */
260 	snd_hdac_chip_updatel(bus, GCTL, AZX_GCTL_UNSOL, AZX_GCTL_UNSOL);
261 
262 	/* detect codecs */
263 	if (!bus->codec_mask) {
264 		bus->codec_mask = snd_hdac_chip_readw(bus, STATESTS);
265 		dev_dbg(bus->dev, "codec_mask = 0x%lx\n", bus->codec_mask);
266 	}
267 
268 	if (hda_codec_mask != -1) {
269 		bus->codec_mask &= hda_codec_mask;
270 		dev_dbg(bus->dev, "filtered codec_mask = 0x%lx\n",
271 			bus->codec_mask);
272 	}
273 }
274 EXPORT_SYMBOL_NS_GPL(hda_codec_detect_mask, SND_SOC_SOF_HDA_AUDIO_CODEC);
275 
276 void hda_codec_init_cmd_io(struct snd_sof_dev *sdev)
277 {
278 	struct hdac_bus *bus = sof_to_bus(sdev);
279 
280 	if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
281 	    sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
282 		return;
283 
284 	/* initialize the codec command I/O */
285 	snd_hdac_bus_init_cmd_io(bus);
286 }
287 EXPORT_SYMBOL_NS_GPL(hda_codec_init_cmd_io, SND_SOC_SOF_HDA_AUDIO_CODEC);
288 
289 void hda_codec_resume_cmd_io(struct snd_sof_dev *sdev)
290 {
291 	struct hdac_bus *bus = sof_to_bus(sdev);
292 
293 	if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
294 	    sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
295 		return;
296 
297 	/* set up CORB/RIRB buffers if was on before suspend */
298 	if (bus->cmd_dma_state)
299 		snd_hdac_bus_init_cmd_io(bus);
300 }
301 EXPORT_SYMBOL_NS_GPL(hda_codec_resume_cmd_io, SND_SOC_SOF_HDA_AUDIO_CODEC);
302 
303 void hda_codec_stop_cmd_io(struct snd_sof_dev *sdev)
304 {
305 	struct hdac_bus *bus = sof_to_bus(sdev);
306 
307 	if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
308 	    sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
309 		return;
310 
311 	/* initialize the codec command I/O */
312 	snd_hdac_bus_stop_cmd_io(bus);
313 }
314 EXPORT_SYMBOL_NS_GPL(hda_codec_stop_cmd_io, SND_SOC_SOF_HDA_AUDIO_CODEC);
315 
316 void hda_codec_suspend_cmd_io(struct snd_sof_dev *sdev)
317 {
318 	struct hdac_bus *bus = sof_to_bus(sdev);
319 
320 	if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
321 	    sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
322 		return;
323 
324 	/* stop the CORB/RIRB DMA if it is On */
325 	if (bus->cmd_dma_state)
326 		snd_hdac_bus_stop_cmd_io(bus);
327 
328 }
329 EXPORT_SYMBOL_NS_GPL(hda_codec_suspend_cmd_io, SND_SOC_SOF_HDA_AUDIO_CODEC);
330 
331 void hda_codec_rirb_status_clear(struct snd_sof_dev *sdev)
332 {
333 	struct hdac_bus *bus = sof_to_bus(sdev);
334 
335 	if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
336 	    sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
337 		return;
338 
339 	/* clear rirb status */
340 	snd_hdac_chip_writeb(bus, RIRBSTS, RIRB_INT_MASK);
341 }
342 EXPORT_SYMBOL_NS_GPL(hda_codec_rirb_status_clear, SND_SOC_SOF_HDA_AUDIO_CODEC);
343 
344 void hda_codec_set_codec_wakeup(struct snd_sof_dev *sdev, bool status)
345 {
346 	struct hdac_bus *bus = sof_to_bus(sdev);
347 
348 	if (sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
349 		return;
350 
351 	snd_hdac_set_codec_wakeup(bus, status);
352 }
353 EXPORT_SYMBOL_NS_GPL(hda_codec_set_codec_wakeup, SND_SOC_SOF_HDA_AUDIO_CODEC);
354 
355 bool hda_codec_check_rirb_status(struct snd_sof_dev *sdev)
356 {
357 	struct hdac_bus *bus = sof_to_bus(sdev);
358 	bool active = false;
359 	u32 rirb_status;
360 
361 	if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
362 	    sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
363 		return false;
364 
365 	rirb_status = snd_hdac_chip_readb(bus, RIRBSTS);
366 	if (rirb_status & RIRB_INT_MASK) {
367 		/*
368 		 * Clearing the interrupt status here ensures
369 		 * that no interrupt gets masked after the RIRB
370 		 * wp is read in snd_hdac_bus_update_rirb.
371 		 */
372 		snd_hdac_chip_writeb(bus, RIRBSTS,
373 				     RIRB_INT_MASK);
374 		active = true;
375 		if (rirb_status & RIRB_INT_RESPONSE)
376 			snd_hdac_bus_update_rirb(bus);
377 	}
378 	return active;
379 }
380 EXPORT_SYMBOL_NS_GPL(hda_codec_check_rirb_status, SND_SOC_SOF_HDA_AUDIO_CODEC);
381 
382 void hda_codec_device_remove(struct snd_sof_dev *sdev)
383 {
384 	struct hdac_bus *bus = sof_to_bus(sdev);
385 
386 	if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
387 	    sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
388 		return;
389 
390 	/* codec removal, invoke bus_device_remove */
391 	snd_hdac_ext_bus_device_remove(bus);
392 }
393 EXPORT_SYMBOL_NS_GPL(hda_codec_device_remove, SND_SOC_SOF_HDA_AUDIO_CODEC);
394 
395 #endif /* CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC */
396 
397 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC) && IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
398 
399 void hda_codec_i915_display_power(struct snd_sof_dev *sdev, bool enable)
400 {
401 	struct hdac_bus *bus = sof_to_bus(sdev);
402 
403 	if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
404 	    sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
405 		return;
406 
407 	if (HDA_IDISP_CODEC(bus->codec_mask)) {
408 		dev_dbg(bus->dev, "Turning i915 HDAC power %d\n", enable);
409 		snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, enable);
410 	}
411 }
412 EXPORT_SYMBOL_NS_GPL(hda_codec_i915_display_power, SND_SOC_SOF_HDA_AUDIO_CODEC_I915);
413 
414 int hda_codec_i915_init(struct snd_sof_dev *sdev)
415 {
416 	struct hdac_bus *bus = sof_to_bus(sdev);
417 	int ret;
418 
419 	if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
420 	    sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
421 		return 0;
422 
423 	/* i915 exposes a HDA codec for HDMI audio */
424 	ret = snd_hdac_i915_init(bus);
425 	if (ret < 0)
426 		return ret;
427 
428 	/* codec_mask not yet known, power up for probe */
429 	snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, true);
430 
431 	return 0;
432 }
433 EXPORT_SYMBOL_NS_GPL(hda_codec_i915_init, SND_SOC_SOF_HDA_AUDIO_CODEC_I915);
434 
435 int hda_codec_i915_exit(struct snd_sof_dev *sdev)
436 {
437 	struct hdac_bus *bus = sof_to_bus(sdev);
438 
439 	if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
440 	    sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
441 		return 0;
442 
443 	if (!bus->audio_component)
444 		return 0;
445 
446 	/* power down unconditionally */
447 	snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, false);
448 
449 	return snd_hdac_i915_exit(bus);
450 }
451 EXPORT_SYMBOL_NS_GPL(hda_codec_i915_exit, SND_SOC_SOF_HDA_AUDIO_CODEC_I915);
452 
453 #endif
454 
455 MODULE_LICENSE("Dual BSD/GPL");
456