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