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