1 // SPDX-License-Identifier: GPL-2.0-only
3 // Copyright(c) 2018 Intel Corporation. All rights reserved.
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)
57 int ret = request_codec_module(codec);
60 codec->probe_id = HDA_CODEC_ID_GENERIC;
61 ret = request_codec_module(codec);
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)
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;
75 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
76 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
80 list_for_each_codec(codec, hbus)
81 if (codec->jacktbl.used)
82 mask |= BIT(codec->core.addr);
85 snd_hdac_chip_updatew(bus, WAKEEN, STATESTS_INT_MASK, mask);
87 EXPORT_SYMBOL_NS_GPL(hda_codec_jack_wake_enable, SND_SOC_SOF_HDA_AUDIO_CODEC);
89 /* check jack status after resuming from suspend mode */
90 void hda_codec_jack_check(struct snd_sof_dev *sdev)
92 struct hda_bus *hbus = sof_to_hbus(sdev);
93 struct hda_codec *codec;
95 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
96 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
99 list_for_each_codec(codec, hbus)
101 * Wake up all jack-detecting codecs regardless whether an event
102 * has been recorded in STATESTS
104 if (codec->jacktbl.used)
105 pm_request_resume(&codec->core.dev);
107 EXPORT_SYMBOL_NS_GPL(hda_codec_jack_check, SND_SOC_SOF_HDA_AUDIO_CODEC);
109 #if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
110 #define is_generic_config(bus) \
111 ((bus)->modelname && !strcmp((bus)->modelname, "generic"))
113 #define is_generic_config(x) 0
116 static struct hda_codec *hda_codec_device_init(struct hdac_bus *bus, int addr, int type)
118 struct hda_codec *codec;
121 codec = snd_hda_codec_device_init(to_hda_bus(bus), addr, "ehdaudio%dD%d", bus->idx, addr);
123 dev_err(bus->dev, "device init failed for hdac device\n");
127 codec->core.type = type;
129 ret = snd_hdac_device_register(&codec->core);
131 dev_err(bus->dev, "failed to register hdac device\n");
132 put_device(&codec->core.dev);
139 /* probe individual codec */
140 static int hda_codec_probe(struct snd_sof_dev *sdev, int address)
142 struct hdac_hda_priv *hda_priv;
143 struct hda_bus *hbus = sof_to_hbus(sdev);
144 struct hda_codec *codec;
145 u32 hda_cmd = (address << 28) | (AC_NODE_ROOT << 20) |
146 (AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID;
151 mutex_lock(&hbus->core.cmd_mutex);
152 snd_hdac_bus_send_cmd(&hbus->core, hda_cmd);
153 snd_hdac_bus_get_response(&hbus->core, address, &resp);
154 mutex_unlock(&hbus->core.cmd_mutex);
155 } while (resp == -1 && retry++ < CODEC_PROBE_RETRIES);
159 dev_dbg(sdev->dev, "HDA codec #%d probed OK: response: %x\n",
162 hda_priv = devm_kzalloc(sdev->dev, sizeof(*hda_priv), GFP_KERNEL);
166 codec = hda_codec_device_init(&hbus->core, address, HDA_DEV_LEGACY);
167 ret = PTR_ERR_OR_ZERO(codec);
171 hda_priv->codec = codec;
172 dev_set_drvdata(&codec->core.dev, hda_priv);
174 if ((resp & 0xFFFF0000) == IDISP_VID_INTEL) {
175 if (!hbus->core.audio_component) {
177 "iDisp hw present but no driver\n");
181 hda_priv->need_display_power = true;
184 if (is_generic_config(hbus))
185 codec->probe_id = HDA_CODEC_ID_GENERIC;
189 ret = hda_codec_load_module(codec);
191 * handle ret==0 (no driver bound) as an error, but pass
192 * other return codes without modification
199 snd_hdac_device_unregister(&codec->core);
200 put_device(&codec->core.dev);
206 /* Codec initialization */
207 void hda_codec_probe_bus(struct snd_sof_dev *sdev)
209 struct hdac_bus *bus = sof_to_bus(sdev);
212 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
213 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
216 /* probe codecs in avail slots */
217 for (i = 0; i < HDA_MAX_CODECS; i++) {
219 if (!(bus->codec_mask & (1 << i)))
222 ret = hda_codec_probe(sdev, i);
224 dev_warn(bus->dev, "codec #%d probe error, ret: %d\n",
226 bus->codec_mask &= ~BIT(i);
230 EXPORT_SYMBOL_NS_GPL(hda_codec_probe_bus, SND_SOC_SOF_HDA_AUDIO_CODEC);
232 void hda_codec_check_for_state_change(struct snd_sof_dev *sdev)
234 struct hdac_bus *bus = sof_to_bus(sdev);
235 unsigned int codec_mask;
237 codec_mask = snd_hdac_chip_readw(bus, STATESTS);
239 hda_codec_jack_check(sdev);
240 snd_hdac_chip_writew(bus, STATESTS, codec_mask);
243 EXPORT_SYMBOL_NS_GPL(hda_codec_check_for_state_change, SND_SOC_SOF_HDA_AUDIO_CODEC);
245 void hda_codec_detect_mask(struct snd_sof_dev *sdev)
247 struct hdac_bus *bus = sof_to_bus(sdev);
249 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
250 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
253 /* Accept unsolicited responses */
254 snd_hdac_chip_updatel(bus, GCTL, AZX_GCTL_UNSOL, AZX_GCTL_UNSOL);
257 if (!bus->codec_mask) {
258 bus->codec_mask = snd_hdac_chip_readw(bus, STATESTS);
259 dev_dbg(bus->dev, "codec_mask = 0x%lx\n", bus->codec_mask);
262 if (hda_codec_mask != -1) {
263 bus->codec_mask &= hda_codec_mask;
264 dev_dbg(bus->dev, "filtered codec_mask = 0x%lx\n",
268 EXPORT_SYMBOL_NS_GPL(hda_codec_detect_mask, SND_SOC_SOF_HDA_AUDIO_CODEC);
270 void hda_codec_init_cmd_io(struct snd_sof_dev *sdev)
272 struct hdac_bus *bus = sof_to_bus(sdev);
274 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
275 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
278 /* initialize the codec command I/O */
279 snd_hdac_bus_init_cmd_io(bus);
281 EXPORT_SYMBOL_NS_GPL(hda_codec_init_cmd_io, SND_SOC_SOF_HDA_AUDIO_CODEC);
283 void hda_codec_resume_cmd_io(struct snd_sof_dev *sdev)
285 struct hdac_bus *bus = sof_to_bus(sdev);
287 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
288 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
291 /* set up CORB/RIRB buffers if was on before suspend */
292 if (bus->cmd_dma_state)
293 snd_hdac_bus_init_cmd_io(bus);
295 EXPORT_SYMBOL_NS_GPL(hda_codec_resume_cmd_io, SND_SOC_SOF_HDA_AUDIO_CODEC);
297 void hda_codec_stop_cmd_io(struct snd_sof_dev *sdev)
299 struct hdac_bus *bus = sof_to_bus(sdev);
301 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
302 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
305 /* initialize the codec command I/O */
306 snd_hdac_bus_stop_cmd_io(bus);
308 EXPORT_SYMBOL_NS_GPL(hda_codec_stop_cmd_io, SND_SOC_SOF_HDA_AUDIO_CODEC);
310 void hda_codec_suspend_cmd_io(struct snd_sof_dev *sdev)
312 struct hdac_bus *bus = sof_to_bus(sdev);
314 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
315 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
318 /* stop the CORB/RIRB DMA if it is On */
319 if (bus->cmd_dma_state)
320 snd_hdac_bus_stop_cmd_io(bus);
323 EXPORT_SYMBOL_NS_GPL(hda_codec_suspend_cmd_io, SND_SOC_SOF_HDA_AUDIO_CODEC);
325 void hda_codec_rirb_status_clear(struct snd_sof_dev *sdev)
327 struct hdac_bus *bus = sof_to_bus(sdev);
329 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
330 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
333 /* clear rirb status */
334 snd_hdac_chip_writeb(bus, RIRBSTS, RIRB_INT_MASK);
336 EXPORT_SYMBOL_NS_GPL(hda_codec_rirb_status_clear, SND_SOC_SOF_HDA_AUDIO_CODEC);
338 void hda_codec_set_codec_wakeup(struct snd_sof_dev *sdev, bool status)
340 struct hdac_bus *bus = sof_to_bus(sdev);
342 if (sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
345 snd_hdac_set_codec_wakeup(bus, status);
347 EXPORT_SYMBOL_NS_GPL(hda_codec_set_codec_wakeup, SND_SOC_SOF_HDA_AUDIO_CODEC);
349 bool hda_codec_check_rirb_status(struct snd_sof_dev *sdev)
351 struct hdac_bus *bus = sof_to_bus(sdev);
355 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
356 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
359 rirb_status = snd_hdac_chip_readb(bus, RIRBSTS);
360 if (rirb_status & RIRB_INT_MASK) {
362 * Clearing the interrupt status here ensures
363 * that no interrupt gets masked after the RIRB
364 * wp is read in snd_hdac_bus_update_rirb.
366 snd_hdac_chip_writeb(bus, RIRBSTS,
369 if (rirb_status & RIRB_INT_RESPONSE)
370 snd_hdac_bus_update_rirb(bus);
374 EXPORT_SYMBOL_NS_GPL(hda_codec_check_rirb_status, SND_SOC_SOF_HDA_AUDIO_CODEC);
376 void hda_codec_device_remove(struct snd_sof_dev *sdev)
378 struct hdac_bus *bus = sof_to_bus(sdev);
380 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
381 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
384 /* codec removal, invoke bus_device_remove */
385 snd_hdac_ext_bus_device_remove(bus);
387 EXPORT_SYMBOL_NS_GPL(hda_codec_device_remove, SND_SOC_SOF_HDA_AUDIO_CODEC);
389 #endif /* CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC */
391 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC) && IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
393 void hda_codec_i915_display_power(struct snd_sof_dev *sdev, bool enable)
395 struct hdac_bus *bus = sof_to_bus(sdev);
397 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
398 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
401 if (HDA_IDISP_CODEC(bus->codec_mask)) {
402 dev_dbg(bus->dev, "Turning i915 HDAC power %d\n", enable);
403 snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, enable);
406 EXPORT_SYMBOL_NS_GPL(hda_codec_i915_display_power, SND_SOC_SOF_HDA_AUDIO_CODEC_I915);
408 int hda_codec_i915_init(struct snd_sof_dev *sdev)
410 struct hdac_bus *bus = sof_to_bus(sdev);
413 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
414 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
417 /* i915 exposes a HDA codec for HDMI audio */
418 ret = snd_hdac_i915_init(bus);
422 /* codec_mask not yet known, power up for probe */
423 snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, true);
427 EXPORT_SYMBOL_NS_GPL(hda_codec_i915_init, SND_SOC_SOF_HDA_AUDIO_CODEC_I915);
429 int hda_codec_i915_exit(struct snd_sof_dev *sdev)
431 struct hdac_bus *bus = sof_to_bus(sdev);
433 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
434 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
437 if (!bus->audio_component)
440 /* power down unconditionally */
441 snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, false);
443 return snd_hdac_i915_exit(bus);
445 EXPORT_SYMBOL_NS_GPL(hda_codec_i915_exit, SND_SOC_SOF_HDA_AUDIO_CODEC_I915);
449 MODULE_LICENSE("Dual BSD/GPL");