1 // SPDX-License-Identifier: (GPL-2.0-only 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 #define CODEC_PROBE_RETRIES 3 24 25 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC) 26 #define IDISP_VID_INTEL 0x80860000 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 84 /* check jack status after resuming from suspend mode */ 85 void hda_codec_jack_check(struct snd_sof_dev *sdev) 86 { 87 struct hda_bus *hbus = sof_to_hbus(sdev); 88 struct hda_codec *codec; 89 90 list_for_each_codec(codec, hbus) 91 /* 92 * Wake up all jack-detecting codecs regardless whether an event 93 * has been recorded in STATESTS 94 */ 95 if (codec->jacktbl.used) 96 pm_request_resume(&codec->core.dev); 97 } 98 #else 99 void hda_codec_jack_wake_enable(struct snd_sof_dev *sdev, bool enable) {} 100 void hda_codec_jack_check(struct snd_sof_dev *sdev) {} 101 #endif /* CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC */ 102 EXPORT_SYMBOL_NS(hda_codec_jack_wake_enable, SND_SOC_SOF_HDA_AUDIO_CODEC); 103 EXPORT_SYMBOL_NS(hda_codec_jack_check, SND_SOC_SOF_HDA_AUDIO_CODEC); 104 105 #if IS_ENABLED(CONFIG_SND_HDA_GENERIC) 106 #define is_generic_config(bus) \ 107 ((bus)->modelname && !strcmp((bus)->modelname, "generic")) 108 #else 109 #define is_generic_config(x) 0 110 #endif 111 112 static void hda_codec_device_exit(struct device *dev) 113 { 114 snd_hdac_device_exit(dev_to_hdac_dev(dev)); 115 } 116 117 static struct hda_codec *hda_codec_device_init(struct hdac_bus *bus, int addr, int type) 118 { 119 struct hda_codec *codec; 120 int ret; 121 122 codec = snd_hda_codec_device_init(to_hda_bus(bus), addr, "ehdaudio%dD%d", bus->idx, addr); 123 if (IS_ERR(codec)) { 124 dev_err(bus->dev, "device init failed for hdac device\n"); 125 return codec; 126 } 127 128 codec->core.type = type; 129 codec->core.dev.release = hda_codec_device_exit; 130 131 ret = snd_hdac_device_register(&codec->core); 132 if (ret) { 133 dev_err(bus->dev, "failed to register hdac device\n"); 134 snd_hdac_device_exit(&codec->core); 135 return ERR_PTR(ret); 136 } 137 138 return codec; 139 } 140 141 /* probe individual codec */ 142 static int hda_codec_probe(struct snd_sof_dev *sdev, int address, 143 bool hda_codec_use_common_hdmi) 144 { 145 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC) 146 struct hdac_hda_priv *hda_priv; 147 int type = HDA_DEV_LEGACY; 148 #endif 149 struct hda_bus *hbus = sof_to_hbus(sdev); 150 struct hda_codec *codec; 151 u32 hda_cmd = (address << 28) | (AC_NODE_ROOT << 20) | 152 (AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID; 153 u32 resp = -1; 154 int ret, retry = 0; 155 156 do { 157 mutex_lock(&hbus->core.cmd_mutex); 158 snd_hdac_bus_send_cmd(&hbus->core, hda_cmd); 159 snd_hdac_bus_get_response(&hbus->core, address, &resp); 160 mutex_unlock(&hbus->core.cmd_mutex); 161 } while (resp == -1 && retry++ < CODEC_PROBE_RETRIES); 162 163 if (resp == -1) 164 return -EIO; 165 dev_dbg(sdev->dev, "HDA codec #%d probed OK: response: %x\n", 166 address, resp); 167 168 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC) 169 hda_priv = devm_kzalloc(sdev->dev, sizeof(*hda_priv), GFP_KERNEL); 170 if (!hda_priv) 171 return -ENOMEM; 172 173 /* only probe ASoC codec drivers for HDAC-HDMI */ 174 if (!hda_codec_use_common_hdmi && (resp & 0xFFFF0000) == IDISP_VID_INTEL) 175 type = HDA_DEV_ASOC; 176 177 codec = hda_codec_device_init(&hbus->core, address, type); 178 ret = PTR_ERR_OR_ZERO(codec); 179 if (ret < 0) 180 return ret; 181 182 hda_priv->codec = codec; 183 dev_set_drvdata(&codec->core.dev, hda_priv); 184 185 if ((resp & 0xFFFF0000) == IDISP_VID_INTEL) { 186 if (!hbus->core.audio_component) { 187 dev_dbg(sdev->dev, 188 "iDisp hw present but no driver\n"); 189 ret = -ENOENT; 190 goto out; 191 } 192 hda_priv->need_display_power = true; 193 } 194 195 if (is_generic_config(hbus)) 196 codec->probe_id = HDA_CODEC_ID_GENERIC; 197 else 198 codec->probe_id = 0; 199 200 if (type == HDA_DEV_LEGACY) { 201 ret = hda_codec_load_module(codec); 202 /* 203 * handle ret==0 (no driver bound) as an error, but pass 204 * other return codes without modification 205 */ 206 if (ret == 0) 207 ret = -ENOENT; 208 } 209 210 out: 211 if (ret < 0) { 212 snd_hdac_device_unregister(&codec->core); 213 put_device(&codec->core.dev); 214 } 215 #else 216 codec = hda_codec_device_init(&hbus->core, address, HDA_DEV_ASOC); 217 ret = PTR_ERR_OR_ZERO(codec); 218 #endif 219 220 return ret; 221 } 222 223 /* Codec initialization */ 224 void hda_codec_probe_bus(struct snd_sof_dev *sdev, 225 bool hda_codec_use_common_hdmi) 226 { 227 struct hdac_bus *bus = sof_to_bus(sdev); 228 int i, ret; 229 230 /* probe codecs in avail slots */ 231 for (i = 0; i < HDA_MAX_CODECS; i++) { 232 233 if (!(bus->codec_mask & (1 << i))) 234 continue; 235 236 ret = hda_codec_probe(sdev, i, hda_codec_use_common_hdmi); 237 if (ret < 0) { 238 dev_warn(bus->dev, "codec #%d probe error, ret: %d\n", 239 i, ret); 240 bus->codec_mask &= ~BIT(i); 241 } 242 } 243 } 244 EXPORT_SYMBOL_NS(hda_codec_probe_bus, SND_SOC_SOF_HDA_AUDIO_CODEC); 245 246 #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI) || \ 247 IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI) 248 249 void hda_codec_i915_display_power(struct snd_sof_dev *sdev, bool enable) 250 { 251 struct hdac_bus *bus = sof_to_bus(sdev); 252 253 if (HDA_IDISP_CODEC(bus->codec_mask)) { 254 dev_dbg(bus->dev, "Turning i915 HDAC power %d\n", enable); 255 snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, enable); 256 } 257 } 258 EXPORT_SYMBOL_NS(hda_codec_i915_display_power, SND_SOC_SOF_HDA_AUDIO_CODEC_I915); 259 260 int hda_codec_i915_init(struct snd_sof_dev *sdev) 261 { 262 struct hdac_bus *bus = sof_to_bus(sdev); 263 int ret; 264 265 /* i915 exposes a HDA codec for HDMI audio */ 266 ret = snd_hdac_i915_init(bus); 267 if (ret < 0) 268 return ret; 269 270 /* codec_mask not yet known, power up for probe */ 271 snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, true); 272 273 return 0; 274 } 275 EXPORT_SYMBOL_NS(hda_codec_i915_init, SND_SOC_SOF_HDA_AUDIO_CODEC_I915); 276 277 int hda_codec_i915_exit(struct snd_sof_dev *sdev) 278 { 279 struct hdac_bus *bus = sof_to_bus(sdev); 280 281 if (!bus->audio_component) 282 return 0; 283 284 /* power down unconditionally */ 285 snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, false); 286 287 return snd_hdac_i915_exit(bus); 288 } 289 EXPORT_SYMBOL_NS(hda_codec_i915_exit, SND_SOC_SOF_HDA_AUDIO_CODEC_I915); 290 291 #endif 292 293 MODULE_LICENSE("Dual BSD/GPL"); 294