1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
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.
6 // Copyright(c) 2018 Intel Corporation. All rights reserved.
8 // Authors: Keyon Jie <yang.jie@linux.intel.com>
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>
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 */
23 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
24 #define IDISP_VID_INTEL 0x80860000
26 /* load the legacy HDA codec driver */
28 static void hda_codec_load_module(struct hda_codec *codec)
30 char alias[MODULE_NAME_LEN];
31 const char *module = alias;
33 snd_hdac_codec_modalias(&codec->core, alias, sizeof(alias));
34 dev_dbg(&codec->core.dev, "loading codec module: %s\n", module);
35 request_module(module);
38 static void hda_codec_load_module(struct hda_codec *codec) {}
41 /* enable controller wake up event for all codecs with jack connectors */
42 void hda_codec_jack_wake_enable(struct snd_sof_dev *sdev)
44 struct hda_bus *hbus = sof_to_hbus(sdev);
45 struct hdac_bus *bus = sof_to_bus(sdev);
46 struct hda_codec *codec;
47 unsigned int mask = 0;
49 list_for_each_codec(codec, hbus)
50 if (codec->jacktbl.used)
51 mask |= BIT(codec->core.addr);
53 snd_hdac_chip_updatew(bus, WAKEEN, STATESTS_INT_MASK, mask);
56 /* check jack status after resuming from suspend mode */
57 void hda_codec_jack_check(struct snd_sof_dev *sdev)
59 struct hda_bus *hbus = sof_to_hbus(sdev);
60 struct hdac_bus *bus = sof_to_bus(sdev);
61 struct hda_codec *codec;
63 /* disable controller Wake Up event*/
64 snd_hdac_chip_updatew(bus, WAKEEN, STATESTS_INT_MASK, 0);
66 list_for_each_codec(codec, hbus)
68 * Wake up all jack-detecting codecs regardless whether an event
69 * has been recorded in STATESTS
71 if (codec->jacktbl.used)
72 schedule_delayed_work(&codec->jackpoll_work,
73 codec->jackpoll_interval);
76 void hda_codec_jack_wake_enable(struct snd_sof_dev *sdev) {}
77 void hda_codec_jack_check(struct snd_sof_dev *sdev) {}
78 #endif /* CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC */
79 EXPORT_SYMBOL(hda_codec_jack_wake_enable);
80 EXPORT_SYMBOL(hda_codec_jack_check);
82 /* probe individual codec */
83 static int hda_codec_probe(struct snd_sof_dev *sdev, int address,
84 bool hda_codec_use_common_hdmi)
86 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
87 struct hdac_hda_priv *hda_priv;
89 struct hda_bus *hbus = sof_to_hbus(sdev);
90 struct hdac_device *hdev;
91 u32 hda_cmd = (address << 28) | (AC_NODE_ROOT << 20) |
92 (AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID;
96 mutex_lock(&hbus->core.cmd_mutex);
97 snd_hdac_bus_send_cmd(&hbus->core, hda_cmd);
98 snd_hdac_bus_get_response(&hbus->core, address, &resp);
99 mutex_unlock(&hbus->core.cmd_mutex);
102 dev_dbg(sdev->dev, "HDA codec #%d probed OK: response: %x\n",
105 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
106 hda_priv = devm_kzalloc(sdev->dev, sizeof(*hda_priv), GFP_KERNEL);
110 hda_priv->codec.bus = hbus;
111 hdev = &hda_priv->codec.core;
113 ret = snd_hdac_ext_bus_device_init(&hbus->core, address, hdev);
117 if ((resp & 0xFFFF0000) == IDISP_VID_INTEL)
118 hda_priv->need_display_power = true;
121 * if common HDMI codec driver is not used, codec load
122 * is skipped here and hdac_hdmi is used instead
124 if (hda_codec_use_common_hdmi ||
125 (resp & 0xFFFF0000) != IDISP_VID_INTEL) {
126 hdev->type = HDA_DEV_LEGACY;
127 hda_codec_load_module(&hda_priv->codec);
132 hdev = devm_kzalloc(sdev->dev, sizeof(*hdev), GFP_KERNEL);
136 ret = snd_hdac_ext_bus_device_init(&hbus->core, address, hdev);
142 /* Codec initialization */
143 int hda_codec_probe_bus(struct snd_sof_dev *sdev,
144 bool hda_codec_use_common_hdmi)
146 struct hdac_bus *bus = sof_to_bus(sdev);
149 /* probe codecs in avail slots */
150 for (i = 0; i < HDA_MAX_CODECS; i++) {
152 if (!(bus->codec_mask & (1 << i)))
155 ret = hda_codec_probe(sdev, i, hda_codec_use_common_hdmi);
157 dev_err(bus->dev, "error: codec #%d probe error, ret: %d\n",
165 EXPORT_SYMBOL(hda_codec_probe_bus);
167 #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI) || \
168 IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI)
170 void hda_codec_i915_get(struct snd_sof_dev *sdev)
172 struct hdac_bus *bus = sof_to_bus(sdev);
174 dev_dbg(bus->dev, "Turning i915 HDAC power on\n");
175 snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, true);
177 EXPORT_SYMBOL(hda_codec_i915_get);
179 void hda_codec_i915_put(struct snd_sof_dev *sdev)
181 struct hdac_bus *bus = sof_to_bus(sdev);
183 dev_dbg(bus->dev, "Turning i915 HDAC power off\n");
184 snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, false);
186 EXPORT_SYMBOL(hda_codec_i915_put);
188 int hda_codec_i915_init(struct snd_sof_dev *sdev)
190 struct hdac_bus *bus = sof_to_bus(sdev);
193 /* i915 exposes a HDA codec for HDMI audio */
194 ret = snd_hdac_i915_init(bus);
198 hda_codec_i915_get(sdev);
202 EXPORT_SYMBOL(hda_codec_i915_init);
204 int hda_codec_i915_exit(struct snd_sof_dev *sdev)
206 struct hdac_bus *bus = sof_to_bus(sdev);
209 hda_codec_i915_put(sdev);
211 ret = snd_hdac_i915_exit(bus);
215 EXPORT_SYMBOL(hda_codec_i915_exit);
219 MODULE_LICENSE("Dual BSD/GPL");