1 // SPDX-License-Identifier: GPL-2.0-only
3 // Copyright(c) 2018 Intel Corporation
5 // Authors: Keyon Jie <yang.jie@linux.intel.com>
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>
17 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
18 #include "../../codecs/hdac_hda.h"
20 #define CODEC_PROBE_RETRIES 3
22 #define IDISP_VID_INTEL 0x80860000
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");
28 /* load the legacy HDA codec driver */
29 static int request_codec_module(struct hda_codec
*codec
)
32 char alias
[MODULE_NAME_LEN
];
33 const char *mod
= NULL
;
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";
42 snd_hdac_codec_modalias(&codec
->core
, alias
, sizeof(alias
));
48 dev_dbg(&codec
->core
.dev
, "loading codec module: %s\n", mod
);
52 return device_attach(hda_codec_dev(codec
));
55 static int hda_codec_load_module(struct hda_codec
*codec
)
59 ret
= snd_hdac_device_register(&codec
->core
);
61 dev_err(&codec
->core
.dev
, "failed to register hdac device\n");
62 put_device(&codec
->core
.dev
);
66 ret
= request_codec_module(codec
);
68 codec
->probe_id
= HDA_CODEC_ID_GENERIC
;
69 ret
= request_codec_module(codec
);
75 /* enable controller wake up event for all codecs with jack connectors */
76 void hda_codec_jack_wake_enable(struct snd_sof_dev
*sdev
, bool enable
)
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;
84 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT
) &&
85 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC
))
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
);
96 list_for_each_codec(codec
, hbus
) {
97 /* reset WAKEEN only HDaudio codecs */
98 mask
|= BIT(codec
->core
.addr
);
102 snd_hdac_chip_updatew(bus
, WAKEEN
, mask
& STATESTS_INT_MASK
, val
);
104 EXPORT_SYMBOL_NS_GPL(hda_codec_jack_wake_enable
, "SND_SOC_SOF_HDA_AUDIO_CODEC");
106 /* check jack status after resuming from suspend mode */
107 void hda_codec_jack_check(struct snd_sof_dev
*sdev
)
109 struct hda_bus
*hbus
= sof_to_hbus(sdev
);
110 struct hda_codec
*codec
;
112 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT
) &&
113 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC
))
116 list_for_each_codec(codec
, hbus
)
118 * Wake up all jack-detecting codecs regardless whether an event
119 * has been recorded in STATESTS
121 if (codec
->jacktbl
.used
)
122 pm_request_resume(&codec
->core
.dev
);
124 EXPORT_SYMBOL_NS_GPL(hda_codec_jack_check
, "SND_SOC_SOF_HDA_AUDIO_CODEC");
126 #if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
127 #define is_generic_config(bus) \
128 ((bus)->modelname && !strcmp((bus)->modelname, "generic"))
130 #define is_generic_config(x) 0
133 static struct hda_codec
*hda_codec_device_init(struct hdac_bus
*bus
, int addr
, int type
)
135 struct hda_codec
*codec
;
137 codec
= snd_hda_codec_device_init(to_hda_bus(bus
), addr
, "ehdaudio%dD%d", bus
->idx
, addr
);
139 dev_err(bus
->dev
, "device init failed for hdac device\n");
143 codec
->core
.type
= type
;
148 /* probe individual codec */
149 static int hda_codec_probe(struct snd_sof_dev
*sdev
, int address
)
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
;
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
);
168 dev_dbg(sdev
->dev
, "HDA codec #%d probed OK: response: %x\n",
171 hda_priv
= devm_kzalloc(sdev
->dev
, sizeof(*hda_priv
), GFP_KERNEL
);
175 codec
= hda_codec_device_init(&hbus
->core
, address
, HDA_DEV_LEGACY
);
176 ret
= PTR_ERR_OR_ZERO(codec
);
180 hda_priv
->codec
= codec
;
181 hda_priv
->dev_index
= address
;
182 dev_set_drvdata(&codec
->core
.dev
, hda_priv
);
184 if ((resp
& 0xFFFF0000) == IDISP_VID_INTEL
) {
185 if (!hbus
->core
.audio_component
) {
187 "iDisp hw present but no driver\n");
191 hda_priv
->need_display_power
= true;
194 if (is_generic_config(hbus
))
195 codec
->probe_id
= HDA_CODEC_ID_GENERIC
;
199 ret
= hda_codec_load_module(codec
);
201 * handle ret==0 (no driver bound) as an error, but pass
202 * other return codes without modification
209 snd_hdac_device_unregister(&codec
->core
);
210 put_device(&codec
->core
.dev
);
216 /* Codec initialization */
217 void hda_codec_probe_bus(struct snd_sof_dev
*sdev
)
219 struct hdac_bus
*bus
= sof_to_bus(sdev
);
222 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT
) &&
223 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC
))
226 /* probe codecs in avail slots */
227 for (i
= 0; i
< HDA_MAX_CODECS
; i
++) {
229 if (!(bus
->codec_mask
& (1 << i
)))
232 ret
= hda_codec_probe(sdev
, i
);
234 dev_warn(bus
->dev
, "codec #%d probe error, ret: %d\n",
236 bus
->codec_mask
&= ~BIT(i
);
240 EXPORT_SYMBOL_NS_GPL(hda_codec_probe_bus
, "SND_SOC_SOF_HDA_AUDIO_CODEC");
242 void hda_codec_check_for_state_change(struct snd_sof_dev
*sdev
)
244 struct hdac_bus
*bus
= sof_to_bus(sdev
);
245 unsigned int codec_mask
;
247 codec_mask
= snd_hdac_chip_readw(bus
, STATESTS
);
249 hda_codec_jack_check(sdev
);
250 snd_hdac_chip_writew(bus
, STATESTS
, codec_mask
);
253 EXPORT_SYMBOL_NS_GPL(hda_codec_check_for_state_change
, "SND_SOC_SOF_HDA_AUDIO_CODEC");
255 void hda_codec_detect_mask(struct snd_sof_dev
*sdev
)
257 struct hdac_bus
*bus
= sof_to_bus(sdev
);
259 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT
) &&
260 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC
))
263 /* Accept unsolicited responses */
264 snd_hdac_chip_updatel(bus
, GCTL
, AZX_GCTL_UNSOL
, AZX_GCTL_UNSOL
);
267 if (!bus
->codec_mask
) {
268 bus
->codec_mask
= snd_hdac_chip_readw(bus
, STATESTS
);
269 dev_dbg(bus
->dev
, "codec_mask = 0x%lx\n", bus
->codec_mask
);
272 if (hda_codec_mask
!= -1) {
273 bus
->codec_mask
&= hda_codec_mask
;
274 dev_dbg(bus
->dev
, "filtered codec_mask = 0x%lx\n",
278 EXPORT_SYMBOL_NS_GPL(hda_codec_detect_mask
, "SND_SOC_SOF_HDA_AUDIO_CODEC");
280 void hda_codec_init_cmd_io(struct snd_sof_dev
*sdev
)
282 struct hdac_bus
*bus
= sof_to_bus(sdev
);
284 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT
) &&
285 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC
))
288 /* initialize the codec command I/O */
289 snd_hdac_bus_init_cmd_io(bus
);
291 EXPORT_SYMBOL_NS_GPL(hda_codec_init_cmd_io
, "SND_SOC_SOF_HDA_AUDIO_CODEC");
293 void hda_codec_resume_cmd_io(struct snd_sof_dev
*sdev
)
295 struct hdac_bus
*bus
= sof_to_bus(sdev
);
297 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT
) &&
298 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC
))
301 /* set up CORB/RIRB buffers if was on before suspend */
302 if (bus
->cmd_dma_state
)
303 snd_hdac_bus_init_cmd_io(bus
);
305 EXPORT_SYMBOL_NS_GPL(hda_codec_resume_cmd_io
, "SND_SOC_SOF_HDA_AUDIO_CODEC");
307 void hda_codec_stop_cmd_io(struct snd_sof_dev
*sdev
)
309 struct hdac_bus
*bus
= sof_to_bus(sdev
);
311 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT
) &&
312 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC
))
315 /* initialize the codec command I/O */
316 snd_hdac_bus_stop_cmd_io(bus
);
318 EXPORT_SYMBOL_NS_GPL(hda_codec_stop_cmd_io
, "SND_SOC_SOF_HDA_AUDIO_CODEC");
320 void hda_codec_suspend_cmd_io(struct snd_sof_dev
*sdev
)
322 struct hdac_bus
*bus
= sof_to_bus(sdev
);
324 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT
) &&
325 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC
))
328 /* stop the CORB/RIRB DMA if it is On */
329 if (bus
->cmd_dma_state
)
330 snd_hdac_bus_stop_cmd_io(bus
);
333 EXPORT_SYMBOL_NS_GPL(hda_codec_suspend_cmd_io
, "SND_SOC_SOF_HDA_AUDIO_CODEC");
335 void hda_codec_rirb_status_clear(struct snd_sof_dev
*sdev
)
337 struct hdac_bus
*bus
= sof_to_bus(sdev
);
339 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT
) &&
340 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC
))
343 /* clear rirb status */
344 snd_hdac_chip_writeb(bus
, RIRBSTS
, RIRB_INT_MASK
);
346 EXPORT_SYMBOL_NS_GPL(hda_codec_rirb_status_clear
, "SND_SOC_SOF_HDA_AUDIO_CODEC");
348 void hda_codec_set_codec_wakeup(struct snd_sof_dev
*sdev
, bool status
)
350 struct hdac_bus
*bus
= sof_to_bus(sdev
);
352 if (sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC
))
355 snd_hdac_set_codec_wakeup(bus
, status
);
357 EXPORT_SYMBOL_NS_GPL(hda_codec_set_codec_wakeup
, "SND_SOC_SOF_HDA_AUDIO_CODEC");
359 bool hda_codec_check_rirb_status(struct snd_sof_dev
*sdev
)
361 struct hdac_bus
*bus
= sof_to_bus(sdev
);
365 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT
) &&
366 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC
))
369 rirb_status
= snd_hdac_chip_readb(bus
, RIRBSTS
);
370 if (rirb_status
& RIRB_INT_MASK
) {
372 * Clearing the interrupt status here ensures
373 * that no interrupt gets masked after the RIRB
374 * wp is read in snd_hdac_bus_update_rirb.
376 snd_hdac_chip_writeb(bus
, RIRBSTS
,
379 if (rirb_status
& RIRB_INT_RESPONSE
)
380 snd_hdac_bus_update_rirb(bus
);
384 EXPORT_SYMBOL_NS_GPL(hda_codec_check_rirb_status
, "SND_SOC_SOF_HDA_AUDIO_CODEC");
386 void hda_codec_device_remove(struct snd_sof_dev
*sdev
)
388 struct hdac_bus
*bus
= sof_to_bus(sdev
);
390 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT
) &&
391 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC
))
394 /* codec removal, invoke bus_device_remove */
395 snd_hdac_ext_bus_device_remove(bus
);
397 EXPORT_SYMBOL_NS_GPL(hda_codec_device_remove
, "SND_SOC_SOF_HDA_AUDIO_CODEC");
399 #endif /* CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC */
401 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC) && IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
403 void hda_codec_i915_display_power(struct snd_sof_dev
*sdev
, bool enable
)
405 struct hdac_bus
*bus
= sof_to_bus(sdev
);
407 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT
) &&
408 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC
))
411 if (HDA_IDISP_CODEC(bus
->codec_mask
)) {
412 dev_dbg(bus
->dev
, "Turning i915 HDAC power %d\n", enable
);
413 snd_hdac_display_power(bus
, HDA_CODEC_IDX_CONTROLLER
, enable
);
416 EXPORT_SYMBOL_NS_GPL(hda_codec_i915_display_power
, "SND_SOC_SOF_HDA_AUDIO_CODEC_I915");
418 int hda_codec_i915_init(struct snd_sof_dev
*sdev
)
420 struct hdac_bus
*bus
= sof_to_bus(sdev
);
423 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT
) &&
424 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC
))
427 /* i915 exposes a HDA codec for HDMI audio */
428 ret
= snd_hdac_i915_init(bus
);
432 /* codec_mask not yet known, power up for probe */
433 snd_hdac_display_power(bus
, HDA_CODEC_IDX_CONTROLLER
, true);
437 EXPORT_SYMBOL_NS_GPL(hda_codec_i915_init
, "SND_SOC_SOF_HDA_AUDIO_CODEC_I915");
439 int hda_codec_i915_exit(struct snd_sof_dev
*sdev
)
441 struct hdac_bus
*bus
= sof_to_bus(sdev
);
443 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT
) &&
444 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC
))
447 if (!bus
->audio_component
)
450 /* power down unconditionally */
451 snd_hdac_display_power(bus
, HDA_CODEC_IDX_CONTROLLER
, false);
453 return snd_hdac_i915_exit(bus
);
455 EXPORT_SYMBOL_NS_GPL(hda_codec_i915_exit
, "SND_SOC_SOF_HDA_AUDIO_CODEC_I915");
459 MODULE_LICENSE("Dual BSD/GPL");
460 MODULE_DESCRIPTION("SOF support for HDaudio codecs");