1 // SPDX-License-Identifier: (GPL-2.0-only 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 */
27 static int request_codec_module(struct hda_codec
*codec
)
30 char alias
[MODULE_NAME_LEN
];
31 const char *mod
= NULL
;
33 switch (codec
->probe_id
) {
34 case HDA_CODEC_ID_GENERIC
:
35 #if IS_MODULE(CONFIG_SND_HDA_GENERIC)
36 mod
= "snd-hda-codec-generic";
40 snd_hdac_codec_modalias(&codec
->core
, alias
, sizeof(alias
));
46 dev_dbg(&codec
->core
.dev
, "loading codec module: %s\n", mod
);
50 return device_attach(hda_codec_dev(codec
));
53 static int hda_codec_load_module(struct hda_codec
*codec
)
55 int ret
= request_codec_module(codec
);
58 codec
->probe_id
= HDA_CODEC_ID_GENERIC
;
59 ret
= request_codec_module(codec
);
65 /* enable controller wake up event for all codecs with jack connectors */
66 void hda_codec_jack_wake_enable(struct snd_sof_dev
*sdev
)
68 struct hda_bus
*hbus
= sof_to_hbus(sdev
);
69 struct hdac_bus
*bus
= sof_to_bus(sdev
);
70 struct hda_codec
*codec
;
71 unsigned int mask
= 0;
73 list_for_each_codec(codec
, hbus
)
74 if (codec
->jacktbl
.used
)
75 mask
|= BIT(codec
->core
.addr
);
77 snd_hdac_chip_updatew(bus
, WAKEEN
, STATESTS_INT_MASK
, mask
);
80 /* check jack status after resuming from suspend mode */
81 void hda_codec_jack_check(struct snd_sof_dev
*sdev
)
83 struct hda_bus
*hbus
= sof_to_hbus(sdev
);
84 struct hdac_bus
*bus
= sof_to_bus(sdev
);
85 struct hda_codec
*codec
;
87 /* disable controller Wake Up event*/
88 snd_hdac_chip_updatew(bus
, WAKEEN
, STATESTS_INT_MASK
, 0);
90 list_for_each_codec(codec
, hbus
)
92 * Wake up all jack-detecting codecs regardless whether an event
93 * has been recorded in STATESTS
95 if (codec
->jacktbl
.used
)
96 schedule_delayed_work(&codec
->jackpoll_work
,
97 codec
->jackpoll_interval
);
100 void hda_codec_jack_wake_enable(struct snd_sof_dev
*sdev
) {}
101 void hda_codec_jack_check(struct snd_sof_dev
*sdev
) {}
102 #endif /* CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC */
103 EXPORT_SYMBOL_NS(hda_codec_jack_wake_enable
, SND_SOC_SOF_HDA_AUDIO_CODEC
);
104 EXPORT_SYMBOL_NS(hda_codec_jack_check
, SND_SOC_SOF_HDA_AUDIO_CODEC
);
106 #if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
107 #define is_generic_config(bus) \
108 ((bus)->modelname && !strcmp((bus)->modelname, "generic"))
110 #define is_generic_config(x) 0
113 /* probe individual codec */
114 static int hda_codec_probe(struct snd_sof_dev
*sdev
, int address
,
115 bool hda_codec_use_common_hdmi
)
117 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
118 struct hdac_hda_priv
*hda_priv
;
120 struct hda_bus
*hbus
= sof_to_hbus(sdev
);
121 struct hdac_device
*hdev
;
122 struct hda_codec
*codec
;
123 u32 hda_cmd
= (address
<< 28) | (AC_NODE_ROOT
<< 20) |
124 (AC_VERB_PARAMETERS
<< 8) | AC_PAR_VENDOR_ID
;
128 mutex_lock(&hbus
->core
.cmd_mutex
);
129 snd_hdac_bus_send_cmd(&hbus
->core
, hda_cmd
);
130 snd_hdac_bus_get_response(&hbus
->core
, address
, &resp
);
131 mutex_unlock(&hbus
->core
.cmd_mutex
);
134 dev_dbg(sdev
->dev
, "HDA codec #%d probed OK: response: %x\n",
137 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
138 hda_priv
= devm_kzalloc(sdev
->dev
, sizeof(*hda_priv
), GFP_KERNEL
);
142 hda_priv
->codec
.bus
= hbus
;
143 hdev
= &hda_priv
->codec
.core
;
144 codec
= &hda_priv
->codec
;
146 ret
= snd_hdac_ext_bus_device_init(&hbus
->core
, address
, hdev
);
150 if ((resp
& 0xFFFF0000) == IDISP_VID_INTEL
) {
151 if (!hdev
->bus
->audio_component
) {
153 "iDisp hw present but no driver\n");
156 hda_priv
->need_display_power
= true;
159 if (is_generic_config(hbus
))
160 codec
->probe_id
= HDA_CODEC_ID_GENERIC
;
165 * if common HDMI codec driver is not used, codec load
166 * is skipped here and hdac_hdmi is used instead
168 if (hda_codec_use_common_hdmi
||
169 (resp
& 0xFFFF0000) != IDISP_VID_INTEL
) {
170 hdev
->type
= HDA_DEV_LEGACY
;
171 ret
= hda_codec_load_module(codec
);
173 * handle ret==0 (no driver bound) as an error, but pass
174 * other return codes without modification
182 hdev
= devm_kzalloc(sdev
->dev
, sizeof(*hdev
), GFP_KERNEL
);
186 ret
= snd_hdac_ext_bus_device_init(&hbus
->core
, address
, hdev
);
192 /* Codec initialization */
193 void hda_codec_probe_bus(struct snd_sof_dev
*sdev
,
194 bool hda_codec_use_common_hdmi
)
196 struct hdac_bus
*bus
= sof_to_bus(sdev
);
199 /* probe codecs in avail slots */
200 for (i
= 0; i
< HDA_MAX_CODECS
; i
++) {
202 if (!(bus
->codec_mask
& (1 << i
)))
205 ret
= hda_codec_probe(sdev
, i
, hda_codec_use_common_hdmi
);
207 dev_warn(bus
->dev
, "codec #%d probe error, ret: %d\n",
209 bus
->codec_mask
&= ~BIT(i
);
213 EXPORT_SYMBOL_NS(hda_codec_probe_bus
, SND_SOC_SOF_HDA_AUDIO_CODEC
);
215 #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI) || \
216 IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI)
218 void hda_codec_i915_display_power(struct snd_sof_dev
*sdev
, bool enable
)
220 struct hdac_bus
*bus
= sof_to_bus(sdev
);
222 if (HDA_IDISP_CODEC(bus
->codec_mask
)) {
223 dev_dbg(bus
->dev
, "Turning i915 HDAC power %d\n", enable
);
224 snd_hdac_display_power(bus
, HDA_CODEC_IDX_CONTROLLER
, enable
);
227 EXPORT_SYMBOL_NS(hda_codec_i915_display_power
, SND_SOC_SOF_HDA_AUDIO_CODEC_I915
);
229 int hda_codec_i915_init(struct snd_sof_dev
*sdev
)
231 struct hdac_bus
*bus
= sof_to_bus(sdev
);
234 /* i915 exposes a HDA codec for HDMI audio */
235 ret
= snd_hdac_i915_init(bus
);
239 /* codec_mask not yet known, power up for probe */
240 snd_hdac_display_power(bus
, HDA_CODEC_IDX_CONTROLLER
, true);
244 EXPORT_SYMBOL_NS(hda_codec_i915_init
, SND_SOC_SOF_HDA_AUDIO_CODEC_I915
);
246 int hda_codec_i915_exit(struct snd_sof_dev
*sdev
)
248 struct hdac_bus
*bus
= sof_to_bus(sdev
);
250 if (!bus
->audio_component
)
253 /* power down unconditionally */
254 snd_hdac_display_power(bus
, HDA_CODEC_IDX_CONTROLLER
, false);
256 return snd_hdac_i915_exit(bus
);
258 EXPORT_SYMBOL_NS(hda_codec_i915_exit
, SND_SOC_SOF_HDA_AUDIO_CODEC_I915
);
262 MODULE_LICENSE("Dual BSD/GPL");