File Index
Files and directories
Sign in
FILE

hda-codec.c

File preview with syntax highlighting.

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