xref: /openbmc/linux/sound/soc/sof/intel/hda-codec.c (revision 4d1356ac)
1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2 //
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.
5 //
6 // Copyright(c) 2018 Intel Corporation. All rights reserved.
7 //
8 // Authors: Keyon Jie <yang.jie@linux.intel.com>
9 //
10 
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>
17 #include "../ops.h"
18 #include "hda.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 */
22 
23 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
24 #define IDISP_VID_INTEL	0x80860000
25 
26 /* load the legacy HDA codec driver */
27 static int hda_codec_load_module(struct hda_codec *codec)
28 {
29 #ifdef MODULE
30 	char alias[MODULE_NAME_LEN];
31 	const char *module = alias;
32 
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);
36 #endif
37 	return device_attach(hda_codec_dev(codec));
38 }
39 
40 /* enable controller wake up event for all codecs with jack connectors */
41 void hda_codec_jack_wake_enable(struct snd_sof_dev *sdev)
42 {
43 	struct hda_bus *hbus = sof_to_hbus(sdev);
44 	struct hdac_bus *bus = sof_to_bus(sdev);
45 	struct hda_codec *codec;
46 	unsigned int mask = 0;
47 
48 	list_for_each_codec(codec, hbus)
49 		if (codec->jacktbl.used)
50 			mask |= BIT(codec->core.addr);
51 
52 	snd_hdac_chip_updatew(bus, WAKEEN, STATESTS_INT_MASK, mask);
53 }
54 
55 /* check jack status after resuming from suspend mode */
56 void hda_codec_jack_check(struct snd_sof_dev *sdev)
57 {
58 	struct hda_bus *hbus = sof_to_hbus(sdev);
59 	struct hdac_bus *bus = sof_to_bus(sdev);
60 	struct hda_codec *codec;
61 
62 	/* disable controller Wake Up event*/
63 	snd_hdac_chip_updatew(bus, WAKEEN, STATESTS_INT_MASK, 0);
64 
65 	list_for_each_codec(codec, hbus)
66 		/*
67 		 * Wake up all jack-detecting codecs regardless whether an event
68 		 * has been recorded in STATESTS
69 		 */
70 		if (codec->jacktbl.used)
71 			schedule_delayed_work(&codec->jackpoll_work,
72 					      codec->jackpoll_interval);
73 }
74 #else
75 void hda_codec_jack_wake_enable(struct snd_sof_dev *sdev) {}
76 void hda_codec_jack_check(struct snd_sof_dev *sdev) {}
77 #endif /* CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC */
78 EXPORT_SYMBOL(hda_codec_jack_wake_enable);
79 EXPORT_SYMBOL(hda_codec_jack_check);
80 
81 /* probe individual codec */
82 static int hda_codec_probe(struct snd_sof_dev *sdev, int address)
83 {
84 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
85 	struct hdac_hda_priv *hda_priv;
86 	struct snd_soc_acpi_mach_params *mach_params = NULL;
87 	struct snd_sof_pdata *pdata = sdev->pdata;
88 #endif
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;
93 	u32 resp = -1;
94 	int ret;
95 
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);
100 	if (resp == -1)
101 		return -EIO;
102 	dev_dbg(sdev->dev, "HDA codec #%d probed OK: response: %x\n",
103 		address, resp);
104 
105 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
106 	hda_priv = devm_kzalloc(sdev->dev, sizeof(*hda_priv), GFP_KERNEL);
107 	if (!hda_priv)
108 		return -ENOMEM;
109 
110 	hda_priv->codec.bus = hbus;
111 	hdev = &hda_priv->codec.core;
112 
113 	ret = snd_hdac_ext_bus_device_init(&hbus->core, address, hdev);
114 	if (ret < 0)
115 		return ret;
116 
117 	if (pdata->machine)
118 		mach_params = (struct snd_soc_acpi_mach_params *)
119 			&pdata->machine->mach_params;
120 
121 	if ((resp & 0xFFFF0000) == IDISP_VID_INTEL)
122 		hda_priv->need_display_power = true;
123 
124 	/*
125 	 * if common HDMI codec driver is not used, codec load
126 	 * is skipped here and hdac_hdmi is used instead
127 	 */
128 	if ((mach_params && mach_params->common_hdmi_codec_drv) ||
129 	    (resp & 0xFFFF0000) != IDISP_VID_INTEL) {
130 		hdev->type = HDA_DEV_LEGACY;
131 		ret = hda_codec_load_module(&hda_priv->codec);
132 		/*
133 		 * handle ret==0 (no driver bound) as an error, but pass
134 		 * other return codes without modification
135 		 */
136 		if (ret == 0)
137 			ret = -ENOENT;
138 	}
139 
140 	return ret;
141 #else
142 	hdev = devm_kzalloc(sdev->dev, sizeof(*hdev), GFP_KERNEL);
143 	if (!hdev)
144 		return -ENOMEM;
145 
146 	ret = snd_hdac_ext_bus_device_init(&hbus->core, address, hdev);
147 
148 	return ret;
149 #endif
150 }
151 
152 /* Codec initialization */
153 int hda_codec_probe_bus(struct snd_sof_dev *sdev)
154 {
155 	struct hdac_bus *bus = sof_to_bus(sdev);
156 	int i, ret;
157 
158 	/* probe codecs in avail slots */
159 	for (i = 0; i < HDA_MAX_CODECS; i++) {
160 
161 		if (!(bus->codec_mask & (1 << i)))
162 			continue;
163 
164 		ret = hda_codec_probe(sdev, i);
165 		if (ret < 0) {
166 			dev_err(bus->dev, "error: codec #%d probe error, ret: %d\n",
167 				i, ret);
168 			return ret;
169 		}
170 	}
171 
172 	return 0;
173 }
174 EXPORT_SYMBOL(hda_codec_probe_bus);
175 
176 #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI) || \
177 	IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI)
178 
179 void hda_codec_i915_get(struct snd_sof_dev *sdev)
180 {
181 	struct hdac_bus *bus = sof_to_bus(sdev);
182 
183 	dev_dbg(bus->dev, "Turning i915 HDAC power on\n");
184 	snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, true);
185 }
186 EXPORT_SYMBOL(hda_codec_i915_get);
187 
188 void hda_codec_i915_put(struct snd_sof_dev *sdev)
189 {
190 	struct hdac_bus *bus = sof_to_bus(sdev);
191 
192 	dev_dbg(bus->dev, "Turning i915 HDAC power off\n");
193 	snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, false);
194 }
195 EXPORT_SYMBOL(hda_codec_i915_put);
196 
197 int hda_codec_i915_init(struct snd_sof_dev *sdev)
198 {
199 	struct hdac_bus *bus = sof_to_bus(sdev);
200 	int ret;
201 
202 	/* i915 exposes a HDA codec for HDMI audio */
203 	ret = snd_hdac_i915_init(bus);
204 	if (ret < 0)
205 		return ret;
206 
207 	hda_codec_i915_get(sdev);
208 
209 	return 0;
210 }
211 EXPORT_SYMBOL(hda_codec_i915_init);
212 
213 int hda_codec_i915_exit(struct snd_sof_dev *sdev)
214 {
215 	struct hdac_bus *bus = sof_to_bus(sdev);
216 	int ret;
217 
218 	hda_codec_i915_put(sdev);
219 
220 	ret = snd_hdac_i915_exit(bus);
221 
222 	return ret;
223 }
224 EXPORT_SYMBOL(hda_codec_i915_exit);
225 
226 #endif
227 
228 MODULE_LICENSE("Dual BSD/GPL");
229