| 1 | // SPDX-License-Identifier: GPL-2.0-only
|
|---|
| 2 | /*
|
|---|
| 3 | * HD-audio codec driver binding
|
|---|
| 4 | * Copyright (c) Takashi Iwai <tiwai@suse.de>
|
|---|
| 5 | */
|
|---|
| 6 |
|
|---|
| 7 | #include <linux/init.h>
|
|---|
| 8 | #include <linux/slab.h>
|
|---|
| 9 | #include <linux/mutex.h>
|
|---|
| 10 | #include <linux/module.h>
|
|---|
| 11 | #include <linux/export.h>
|
|---|
| 12 | #include <linux/pm.h>
|
|---|
| 13 | #include <sound/core.h>
|
|---|
| 14 | #include <sound/hda_codec.h>
|
|---|
| 15 | #include "hda_local.h"
|
|---|
| 16 | #include "hda_jack.h"
|
|---|
| 17 |
|
|---|
| 18 | #pragma disable_message (201)
|
|---|
| 19 |
|
|---|
| 20 | /*
|
|---|
| 21 | * find a matching codec id
|
|---|
| 22 | */
|
|---|
| 23 | static int hda_codec_match(struct hdac_device *dev, struct hdac_driver *drv)
|
|---|
| 24 | {
|
|---|
| 25 | struct hda_codec *codec = container_of(dev, struct hda_codec, core);
|
|---|
| 26 | struct hda_codec_driver *driver =
|
|---|
| 27 | container_of(drv, struct hda_codec_driver, core);
|
|---|
| 28 | const struct hda_device_id *list;
|
|---|
| 29 | /* check probe_id instead of vendor_id if set */
|
|---|
| 30 | u32 id = codec->probe_id ? codec->probe_id : codec->core.vendor_id;
|
|---|
| 31 | u32 rev_id = codec->core.revision_id;
|
|---|
| 32 |
|
|---|
| 33 | for (list = driver->id; list->vendor_id; list++) {
|
|---|
| 34 | if (list->vendor_id == id &&
|
|---|
| 35 | (!list->rev_id || list->rev_id == rev_id)) {
|
|---|
| 36 | codec->preset = list;
|
|---|
| 37 | return 1;
|
|---|
| 38 | }
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | return 0;
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | /* process an unsolicited event */
|
|---|
| 45 | static void hda_codec_unsol_event(struct hdac_device *dev, unsigned int ev)
|
|---|
| 46 | {
|
|---|
| 47 | struct hda_codec *codec = container_of(dev, struct hda_codec, core);
|
|---|
| 48 |
|
|---|
| 49 | /* ignore unsol events during shutdown */
|
|---|
| 50 | if (codec->bus->shutdown)
|
|---|
| 51 | return;
|
|---|
| 52 | #ifndef TARGET_OS2
|
|---|
| 53 | /* ignore unsol events during system suspend/resume */
|
|---|
| 54 | if (codec->core.dev.power.power_state.event != PM_EVENT_ON)
|
|---|
| 55 | return;
|
|---|
| 56 | #endif
|
|---|
| 57 | if (codec->patch_ops.unsol_event)
|
|---|
| 58 | codec->patch_ops.unsol_event(codec, ev);
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | /**
|
|---|
| 62 | * snd_hda_codec_set_name - set the codec name
|
|---|
| 63 | * @codec: the HDA codec
|
|---|
| 64 | * @name: name string to set
|
|---|
| 65 | */
|
|---|
| 66 | int snd_hda_codec_set_name(struct hda_codec *codec, const char *name)
|
|---|
| 67 | {
|
|---|
| 68 | int err;
|
|---|
| 69 |
|
|---|
| 70 | if (!name)
|
|---|
| 71 | return 0;
|
|---|
| 72 | err = snd_hdac_device_set_chip_name(&codec->core, name);
|
|---|
| 73 | if (err < 0)
|
|---|
| 74 | return err;
|
|---|
| 75 |
|
|---|
| 76 | /* update the mixer name */
|
|---|
| 77 | if (!*codec->card->mixername ||
|
|---|
| 78 | codec->bus->mixer_assigned >= codec->core.addr) {
|
|---|
| 79 | snprintf(codec->card->mixername,
|
|---|
| 80 | sizeof(codec->card->mixername), "%s %s",
|
|---|
| 81 | codec->core.vendor_name, codec->core.chip_name);
|
|---|
| 82 | codec->bus->mixer_assigned = codec->core.addr;
|
|---|
| 83 | }
|
|---|
| 84 | return 0;
|
|---|
| 85 | }
|
|---|
| 86 | EXPORT_SYMBOL_GPL(snd_hda_codec_set_name);
|
|---|
| 87 |
|
|---|
| 88 | static int hda_codec_driver_probe(struct device *dev)
|
|---|
| 89 | {
|
|---|
| 90 | struct hda_codec *codec = dev_to_hda_codec(dev);
|
|---|
| 91 | struct module *owner = dev->driver->owner;
|
|---|
| 92 | hda_codec_patch_t patch;
|
|---|
| 93 | int err;
|
|---|
| 94 |
|
|---|
| 95 | if (codec->bus->core.ext_ops) {
|
|---|
| 96 | if (WARN_ON(!codec->bus->core.ext_ops->hdev_attach))
|
|---|
| 97 | return -EINVAL;
|
|---|
| 98 | return codec->bus->core.ext_ops->hdev_attach(&codec->core);
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | if (WARN_ON(!codec->preset))
|
|---|
| 102 | return -EINVAL;
|
|---|
| 103 |
|
|---|
| 104 | err = snd_hda_codec_set_name(codec, codec->preset->name);
|
|---|
| 105 | if (err < 0)
|
|---|
| 106 | goto error;
|
|---|
| 107 | err = snd_hdac_regmap_init(&codec->core);
|
|---|
| 108 | if (err < 0)
|
|---|
| 109 | goto error;
|
|---|
| 110 |
|
|---|
| 111 | if (!try_module_get(owner)) {
|
|---|
| 112 | err = -EINVAL;
|
|---|
| 113 | goto error;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | patch = (hda_codec_patch_t)codec->preset->driver_data;
|
|---|
| 117 | if (patch) {
|
|---|
| 118 | err = patch(codec);
|
|---|
| 119 | if (err < 0)
|
|---|
| 120 | goto error_module_put;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | err = snd_hda_codec_build_pcms(codec);
|
|---|
| 124 | if (err < 0)
|
|---|
| 125 | goto error_module;
|
|---|
| 126 | err = snd_hda_codec_build_controls(codec);
|
|---|
| 127 | if (err < 0)
|
|---|
| 128 | goto error_module;
|
|---|
| 129 | /* only register after the bus probe finished; otherwise it's racy */
|
|---|
| 130 | if (!codec->bus->bus_probing && codec->card->registered) {
|
|---|
| 131 | err = snd_card_register(codec->card);
|
|---|
| 132 | if (err < 0)
|
|---|
| 133 | goto error_module;
|
|---|
| 134 | snd_hda_codec_register(codec);
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | codec->core.lazy_cache = true;
|
|---|
| 138 | return 0;
|
|---|
| 139 |
|
|---|
| 140 | error_module:
|
|---|
| 141 | if (codec->patch_ops.free)
|
|---|
| 142 | codec->patch_ops.free(codec);
|
|---|
| 143 | error_module_put:
|
|---|
| 144 | module_put(owner);
|
|---|
| 145 |
|
|---|
| 146 | error:
|
|---|
| 147 | snd_hda_codec_cleanup_for_unbind(codec);
|
|---|
| 148 | codec->preset = NULL;
|
|---|
| 149 | return err;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | static int hda_codec_driver_remove(struct device *dev)
|
|---|
| 153 | {
|
|---|
| 154 | struct hda_codec *codec = dev_to_hda_codec(dev);
|
|---|
| 155 |
|
|---|
| 156 | if (codec->bus->core.ext_ops) {
|
|---|
| 157 | if (WARN_ON(!codec->bus->core.ext_ops->hdev_detach))
|
|---|
| 158 | return -EINVAL;
|
|---|
| 159 | return codec->bus->core.ext_ops->hdev_detach(&codec->core);
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | snd_hda_codec_disconnect_pcms(codec);
|
|---|
| 163 | snd_hda_jack_tbl_disconnect(codec);
|
|---|
| 164 | if (!refcount_dec_and_test(&codec->pcm_ref))
|
|---|
| 165 | wait_event(codec->remove_sleep, !refcount_read(&codec->pcm_ref));
|
|---|
| 166 | snd_power_sync_ref(codec->bus->card);
|
|---|
| 167 |
|
|---|
| 168 | if (codec->patch_ops.free)
|
|---|
| 169 | codec->patch_ops.free(codec);
|
|---|
| 170 | snd_hda_codec_cleanup_for_unbind(codec);
|
|---|
| 171 | codec->preset = NULL;
|
|---|
| 172 | module_put(dev->driver->owner);
|
|---|
| 173 | return 0;
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | static void hda_codec_driver_shutdown(struct device *dev)
|
|---|
| 177 | {
|
|---|
| 178 | snd_hda_codec_shutdown(dev_to_hda_codec(dev));
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | int __hda_codec_driver_register(struct hda_codec_driver *drv, const char *name,
|
|---|
| 182 | struct module *owner)
|
|---|
| 183 | {
|
|---|
| 184 | drv->core.driver.name = name;
|
|---|
| 185 | drv->core.driver.owner = owner;
|
|---|
| 186 | drv->core.driver.bus = &snd_hda_bus_type;
|
|---|
| 187 | drv->core.driver.probe = hda_codec_driver_probe;
|
|---|
| 188 | drv->core.driver.remove = hda_codec_driver_remove;
|
|---|
| 189 | drv->core.driver.shutdown = hda_codec_driver_shutdown;
|
|---|
| 190 | drv->core.driver.pm = &hda_codec_driver_pm;
|
|---|
| 191 | drv->core.type = HDA_DEV_LEGACY;
|
|---|
| 192 | drv->core.match = hda_codec_match;
|
|---|
| 193 | drv->core.unsol_event = hda_codec_unsol_event;
|
|---|
| 194 | return driver_register(&drv->core.driver);
|
|---|
| 195 | }
|
|---|
| 196 | EXPORT_SYMBOL_GPL(__hda_codec_driver_register);
|
|---|
| 197 |
|
|---|
| 198 | void hda_codec_driver_unregister(struct hda_codec_driver *drv)
|
|---|
| 199 | {
|
|---|
| 200 | driver_unregister(&drv->core.driver);
|
|---|
| 201 | }
|
|---|
| 202 | EXPORT_SYMBOL_GPL(hda_codec_driver_unregister);
|
|---|
| 203 |
|
|---|
| 204 | static inline bool codec_probed(struct hda_codec *codec)
|
|---|
| 205 | {
|
|---|
| 206 | return device_attach(hda_codec_dev(codec)) > 0 && codec->preset;
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | /* try to auto-load codec module */
|
|---|
| 210 | static void request_codec_module(struct hda_codec *codec)
|
|---|
| 211 | {
|
|---|
| 212 | #ifdef MODULE
|
|---|
| 213 | char modalias[32];
|
|---|
| 214 | const char *mod = NULL;
|
|---|
| 215 |
|
|---|
| 216 | switch (codec->probe_id) {
|
|---|
| 217 | case HDA_CODEC_ID_GENERIC_HDMI:
|
|---|
| 218 | #if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI) || defined(CONFIG_SND_HDA_CODEC_HDMI)
|
|---|
| 219 | mod = "snd-hda-codec-hdmi";
|
|---|
| 220 | #endif
|
|---|
| 221 | break;
|
|---|
| 222 | case HDA_CODEC_ID_GENERIC:
|
|---|
| 223 | #if IS_MODULE(CONFIG_SND_HDA_GENERIC) || defined(CONFIG_SND_HDA_GENERIC)
|
|---|
| 224 | mod = "snd-hda-codec-generic";
|
|---|
| 225 | #endif
|
|---|
| 226 | break;
|
|---|
| 227 | default:
|
|---|
| 228 | snd_hdac_codec_modalias(&codec->core, modalias, sizeof(modalias));
|
|---|
| 229 | mod = modalias;
|
|---|
| 230 | break;
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | #ifndef TARGET_OS2
|
|---|
| 234 | if (mod)
|
|---|
| 235 | request_module(mod);
|
|---|
| 236 | #endif
|
|---|
| 237 | #endif /* MODULE */
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | /* try to auto-load and bind the codec module */
|
|---|
| 241 | static void codec_bind_module(struct hda_codec *codec)
|
|---|
| 242 | {
|
|---|
| 243 | //#ifdef MODULE
|
|---|
| 244 | request_codec_module(codec);
|
|---|
| 245 | if (codec_probed(codec))
|
|---|
| 246 | return;
|
|---|
| 247 | //#endif
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI) || defined(CONFIG_SND_HDA_CODEC_HDMI)
|
|---|
| 251 | /* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
|
|---|
| 252 | static bool is_likely_hdmi_codec(struct hda_codec *codec)
|
|---|
| 253 | {
|
|---|
| 254 | hda_nid_t nid;
|
|---|
| 255 |
|
|---|
| 256 | /*
|
|---|
| 257 | * For ASoC users, if snd_hda_hdmi_codec module is denylisted and any
|
|---|
| 258 | * event causes i915 enumeration to fail, ->wcaps remains uninitialized.
|
|---|
| 259 | */
|
|---|
| 260 | if (!codec->wcaps)
|
|---|
| 261 | return true;
|
|---|
| 262 |
|
|---|
| 263 | for_each_hda_codec_node(nid, codec) {
|
|---|
| 264 | unsigned int wcaps = get_wcaps(codec, nid);
|
|---|
| 265 | switch (get_wcaps_type(wcaps)) {
|
|---|
| 266 | case AC_WID_AUD_IN:
|
|---|
| 267 | return false; /* HDMI parser supports only HDMI out */
|
|---|
| 268 | case AC_WID_AUD_OUT:
|
|---|
| 269 | if (!(wcaps & AC_WCAP_DIGITAL))
|
|---|
| 270 | return false;
|
|---|
| 271 | break;
|
|---|
| 272 | }
|
|---|
| 273 | }
|
|---|
| 274 | return true;
|
|---|
| 275 | }
|
|---|
| 276 | #else
|
|---|
| 277 | /* no HDMI codec parser support */
|
|---|
| 278 | #define is_likely_hdmi_codec(codec) false
|
|---|
| 279 | #endif /* CONFIG_SND_HDA_CODEC_HDMI */
|
|---|
| 280 |
|
|---|
| 281 | static int codec_bind_generic(struct hda_codec *codec)
|
|---|
| 282 | {
|
|---|
| 283 | if (codec->probe_id)
|
|---|
| 284 | return -ENODEV;
|
|---|
| 285 |
|
|---|
| 286 | if (is_likely_hdmi_codec(codec)) {
|
|---|
| 287 | codec->probe_id = HDA_CODEC_ID_GENERIC_HDMI;
|
|---|
| 288 | request_codec_module(codec);
|
|---|
| 289 | if (codec_probed(codec))
|
|---|
| 290 | return 0;
|
|---|
| 291 | }
|
|---|
| 292 |
|
|---|
| 293 | codec->probe_id = HDA_CODEC_ID_GENERIC;
|
|---|
| 294 | request_codec_module(codec);
|
|---|
| 295 | if (codec_probed(codec))
|
|---|
| 296 | return 0;
|
|---|
| 297 | return -ENODEV;
|
|---|
| 298 | }
|
|---|
| 299 |
|
|---|
| 300 | #if IS_ENABLED(CONFIG_SND_HDA_GENERIC) || defined(CONFIG_SND_HDA_GENERIC)
|
|---|
| 301 | #define is_generic_config(codec) \
|
|---|
| 302 | (codec->modelname && !strcmp(codec->modelname, "generic"))
|
|---|
| 303 | #else
|
|---|
| 304 | #define is_generic_config(codec) 0
|
|---|
| 305 | #endif
|
|---|
| 306 |
|
|---|
| 307 | /**
|
|---|
| 308 | * snd_hda_codec_configure - (Re-)configure the HD-audio codec
|
|---|
| 309 | * @codec: the HDA codec
|
|---|
| 310 | *
|
|---|
| 311 | * Start parsing of the given codec tree and (re-)initialize the whole
|
|---|
| 312 | * patch instance.
|
|---|
| 313 | *
|
|---|
| 314 | * Returns 0 if successful or a negative error code.
|
|---|
| 315 | */
|
|---|
| 316 | int snd_hda_codec_configure(struct hda_codec *codec)
|
|---|
| 317 | {
|
|---|
| 318 | int err;
|
|---|
| 319 |
|
|---|
| 320 | if (codec->configured)
|
|---|
| 321 | return 0;
|
|---|
| 322 |
|
|---|
| 323 | if (is_generic_config(codec))
|
|---|
| 324 | codec->probe_id = HDA_CODEC_ID_GENERIC;
|
|---|
| 325 | else
|
|---|
| 326 | codec->probe_id = 0;
|
|---|
| 327 |
|
|---|
| 328 | if (!device_is_registered(&codec->core.dev)) {
|
|---|
| 329 | err = snd_hdac_device_register(&codec->core);
|
|---|
| 330 | if (err < 0)
|
|---|
| 331 | return err;
|
|---|
| 332 | }
|
|---|
| 333 |
|
|---|
| 334 | if (!codec->preset)
|
|---|
| 335 | codec_bind_module(codec);
|
|---|
| 336 | if (!codec->preset) {
|
|---|
| 337 | err = codec_bind_generic(codec);
|
|---|
| 338 | if (err < 0) {
|
|---|
| 339 | codec_dbg(codec, "Unable to bind the codec - err = %d\n",err);
|
|---|
| 340 | return err;
|
|---|
| 341 | }
|
|---|
| 342 | }
|
|---|
| 343 |
|
|---|
| 344 | codec->configured = 1;
|
|---|
| 345 | return 0;
|
|---|
| 346 | }
|
|---|
| 347 | EXPORT_SYMBOL_GPL(snd_hda_codec_configure);
|
|---|