Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 1 | /* |
| 2 | * HD-audio codec driver binding |
| 3 | * Copyright (c) Takashi Iwai <tiwai@suse.de> |
| 4 | */ |
| 5 | |
| 6 | #include <linux/init.h> |
| 7 | #include <linux/slab.h> |
| 8 | #include <linux/mutex.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/export.h> |
Takashi Iwai | 59ed1ea | 2015-02-18 15:39:59 +0100 | [diff] [blame] | 11 | #include <linux/pm.h> |
Takashi Iwai | b2a0baf | 2015-03-05 17:21:32 +0100 | [diff] [blame] | 12 | #include <linux/pm_runtime.h> |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 13 | #include <sound/core.h> |
| 14 | #include "hda_codec.h" |
| 15 | #include "hda_local.h" |
| 16 | |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 17 | /* |
| 18 | * find a matching codec preset |
| 19 | */ |
Takashi Iwai | e3d280f | 2015-02-17 21:46:37 +0100 | [diff] [blame] | 20 | static int hda_codec_match(struct hdac_device *dev, struct hdac_driver *drv) |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 21 | { |
Takashi Iwai | e3d280f | 2015-02-17 21:46:37 +0100 | [diff] [blame] | 22 | struct hda_codec *codec = container_of(dev, struct hda_codec, core); |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 23 | struct hda_codec_driver *driver = |
Takashi Iwai | e3d280f | 2015-02-17 21:46:37 +0100 | [diff] [blame] | 24 | container_of(drv, struct hda_codec_driver, core); |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 25 | const struct hda_codec_preset *preset; |
| 26 | /* check probe_id instead of vendor_id if set */ |
Takashi Iwai | 7639a06 | 2015-03-03 10:07:24 +0100 | [diff] [blame] | 27 | u32 id = codec->probe_id ? codec->probe_id : codec->core.vendor_id; |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 28 | |
| 29 | for (preset = driver->preset; preset->id; preset++) { |
| 30 | u32 mask = preset->mask; |
| 31 | |
Takashi Iwai | 7639a06 | 2015-03-03 10:07:24 +0100 | [diff] [blame] | 32 | if (preset->afg && preset->afg != codec->core.afg) |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 33 | continue; |
Takashi Iwai | 7639a06 | 2015-03-03 10:07:24 +0100 | [diff] [blame] | 34 | if (preset->mfg && preset->mfg != codec->core.mfg) |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 35 | continue; |
| 36 | if (!mask) |
| 37 | mask = ~0; |
| 38 | if (preset->id == (id & mask) && |
Takashi Iwai | 7639a06 | 2015-03-03 10:07:24 +0100 | [diff] [blame] | 39 | (!preset->rev || preset->rev == codec->core.revision_id)) { |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 40 | codec->preset = preset; |
| 41 | return 1; |
| 42 | } |
| 43 | } |
| 44 | return 0; |
| 45 | } |
| 46 | |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 47 | /* process an unsolicited event */ |
| 48 | static void hda_codec_unsol_event(struct hdac_device *dev, unsigned int ev) |
| 49 | { |
| 50 | struct hda_codec *codec = container_of(dev, struct hda_codec, core); |
| 51 | |
| 52 | if (codec->patch_ops.unsol_event) |
| 53 | codec->patch_ops.unsol_event(codec, ev); |
| 54 | } |
| 55 | |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 56 | /* reset the codec name from the preset */ |
| 57 | static int codec_refresh_name(struct hda_codec *codec, const char *name) |
| 58 | { |
Takashi Iwai | 7639a06 | 2015-03-03 10:07:24 +0100 | [diff] [blame] | 59 | if (name) { |
| 60 | kfree(codec->core.chip_name); |
| 61 | codec->core.chip_name = kstrdup(name, GFP_KERNEL); |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 62 | } |
Takashi Iwai | 7639a06 | 2015-03-03 10:07:24 +0100 | [diff] [blame] | 63 | return codec->core.chip_name ? 0 : -ENOMEM; |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | static int hda_codec_driver_probe(struct device *dev) |
| 67 | { |
| 68 | struct hda_codec *codec = dev_to_hda_codec(dev); |
| 69 | struct module *owner = dev->driver->owner; |
| 70 | int err; |
| 71 | |
| 72 | if (WARN_ON(!codec->preset)) |
| 73 | return -EINVAL; |
| 74 | |
| 75 | err = codec_refresh_name(codec, codec->preset->name); |
| 76 | if (err < 0) |
| 77 | goto error; |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame^] | 78 | err = snd_hdac_regmap_init(&codec->core); |
| 79 | if (err < 0) |
| 80 | goto error; |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 81 | |
| 82 | if (!try_module_get(owner)) { |
| 83 | err = -EINVAL; |
| 84 | goto error; |
| 85 | } |
| 86 | |
| 87 | err = codec->preset->patch(codec); |
Takashi Iwai | bcd9655 | 2015-02-27 20:44:54 +0100 | [diff] [blame] | 88 | if (err < 0) |
| 89 | goto error_module; |
| 90 | |
| 91 | err = snd_hda_codec_build_pcms(codec); |
| 92 | if (err < 0) |
| 93 | goto error_module; |
| 94 | err = snd_hda_codec_build_controls(codec); |
| 95 | if (err < 0) |
| 96 | goto error_module; |
| 97 | if (codec->card->registered) { |
| 98 | err = snd_card_register(codec->card); |
| 99 | if (err < 0) |
| 100 | goto error_module; |
Takashi Iwai | c4c2533 | 2015-03-03 17:22:12 +0100 | [diff] [blame] | 101 | snd_hda_codec_register(codec); |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 102 | } |
| 103 | |
Takashi Iwai | 4d75faa0 | 2015-02-25 14:42:38 +0100 | [diff] [blame^] | 104 | codec->core.lazy_cache = true; |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 105 | return 0; |
| 106 | |
Takashi Iwai | bcd9655 | 2015-02-27 20:44:54 +0100 | [diff] [blame] | 107 | error_module: |
| 108 | module_put(owner); |
| 109 | |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 110 | error: |
Takashi Iwai | bcd9655 | 2015-02-27 20:44:54 +0100 | [diff] [blame] | 111 | snd_hda_codec_cleanup_for_unbind(codec); |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 112 | return err; |
| 113 | } |
| 114 | |
| 115 | static int hda_codec_driver_remove(struct device *dev) |
| 116 | { |
| 117 | struct hda_codec *codec = dev_to_hda_codec(dev); |
| 118 | |
| 119 | if (codec->patch_ops.free) |
| 120 | codec->patch_ops.free(codec); |
Takashi Iwai | 9a6246f | 2015-02-27 18:17:28 +0100 | [diff] [blame] | 121 | snd_hda_codec_cleanup_for_unbind(codec); |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 122 | module_put(dev->driver->owner); |
| 123 | return 0; |
| 124 | } |
| 125 | |
Takashi Iwai | b2a0baf | 2015-03-05 17:21:32 +0100 | [diff] [blame] | 126 | static void hda_codec_driver_shutdown(struct device *dev) |
| 127 | { |
| 128 | struct hda_codec *codec = dev_to_hda_codec(dev); |
| 129 | |
| 130 | if (!pm_runtime_suspended(dev) && codec->patch_ops.reboot_notify) |
| 131 | codec->patch_ops.reboot_notify(codec); |
| 132 | } |
| 133 | |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 134 | int __hda_codec_driver_register(struct hda_codec_driver *drv, const char *name, |
| 135 | struct module *owner) |
| 136 | { |
Takashi Iwai | e3d280f | 2015-02-17 21:46:37 +0100 | [diff] [blame] | 137 | drv->core.driver.name = name; |
| 138 | drv->core.driver.owner = owner; |
| 139 | drv->core.driver.bus = &snd_hda_bus_type; |
| 140 | drv->core.driver.probe = hda_codec_driver_probe; |
| 141 | drv->core.driver.remove = hda_codec_driver_remove; |
| 142 | drv->core.driver.shutdown = hda_codec_driver_shutdown; |
| 143 | drv->core.driver.pm = &hda_codec_driver_pm; |
| 144 | drv->core.type = HDA_DEV_LEGACY; |
| 145 | drv->core.match = hda_codec_match; |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 146 | drv->core.unsol_event = hda_codec_unsol_event; |
Takashi Iwai | e3d280f | 2015-02-17 21:46:37 +0100 | [diff] [blame] | 147 | return driver_register(&drv->core.driver); |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 148 | } |
| 149 | EXPORT_SYMBOL_GPL(__hda_codec_driver_register); |
| 150 | |
| 151 | void hda_codec_driver_unregister(struct hda_codec_driver *drv) |
| 152 | { |
Takashi Iwai | e3d280f | 2015-02-17 21:46:37 +0100 | [diff] [blame] | 153 | driver_unregister(&drv->core.driver); |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 154 | } |
| 155 | EXPORT_SYMBOL_GPL(hda_codec_driver_unregister); |
| 156 | |
| 157 | static inline bool codec_probed(struct hda_codec *codec) |
| 158 | { |
| 159 | return device_attach(hda_codec_dev(codec)) > 0 && codec->preset; |
| 160 | } |
| 161 | |
| 162 | /* try to auto-load and bind the codec module */ |
| 163 | static void codec_bind_module(struct hda_codec *codec) |
| 164 | { |
| 165 | #ifdef MODULE |
Takashi Iwai | 7639a06 | 2015-03-03 10:07:24 +0100 | [diff] [blame] | 166 | request_module("snd-hda-codec-id:%08x", codec->core.vendor_id); |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 167 | if (codec_probed(codec)) |
| 168 | return; |
| 169 | request_module("snd-hda-codec-id:%04x*", |
Takashi Iwai | 7639a06 | 2015-03-03 10:07:24 +0100 | [diff] [blame] | 170 | (codec->core.vendor_id >> 16) & 0xffff); |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 171 | if (codec_probed(codec)) |
| 172 | return; |
| 173 | #endif |
| 174 | } |
| 175 | |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 176 | #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI) |
| 177 | /* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */ |
| 178 | static bool is_likely_hdmi_codec(struct hda_codec *codec) |
| 179 | { |
Takashi Iwai | 7639a06 | 2015-03-03 10:07:24 +0100 | [diff] [blame] | 180 | hda_nid_t nid; |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 181 | |
Takashi Iwai | 7639a06 | 2015-03-03 10:07:24 +0100 | [diff] [blame] | 182 | for_each_hda_codec_node(nid, codec) { |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 183 | unsigned int wcaps = get_wcaps(codec, nid); |
| 184 | switch (get_wcaps_type(wcaps)) { |
| 185 | case AC_WID_AUD_IN: |
| 186 | return false; /* HDMI parser supports only HDMI out */ |
| 187 | case AC_WID_AUD_OUT: |
| 188 | if (!(wcaps & AC_WCAP_DIGITAL)) |
| 189 | return false; |
| 190 | break; |
| 191 | } |
| 192 | } |
| 193 | return true; |
| 194 | } |
| 195 | #else |
| 196 | /* no HDMI codec parser support */ |
| 197 | #define is_likely_hdmi_codec(codec) false |
| 198 | #endif /* CONFIG_SND_HDA_CODEC_HDMI */ |
| 199 | |
| 200 | static int codec_bind_generic(struct hda_codec *codec) |
| 201 | { |
| 202 | if (codec->probe_id) |
| 203 | return -ENODEV; |
| 204 | |
| 205 | if (is_likely_hdmi_codec(codec)) { |
| 206 | codec->probe_id = HDA_CODEC_ID_GENERIC_HDMI; |
| 207 | #if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI) |
| 208 | request_module("snd-hda-codec-hdmi"); |
| 209 | #endif |
| 210 | if (codec_probed(codec)) |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | codec->probe_id = HDA_CODEC_ID_GENERIC; |
| 215 | #if IS_MODULE(CONFIG_SND_HDA_GENERIC) |
| 216 | request_module("snd-hda-codec-generic"); |
| 217 | #endif |
| 218 | if (codec_probed(codec)) |
| 219 | return 0; |
| 220 | return -ENODEV; |
| 221 | } |
| 222 | |
| 223 | #if IS_ENABLED(CONFIG_SND_HDA_GENERIC) |
| 224 | #define is_generic_config(codec) \ |
| 225 | (codec->modelname && !strcmp(codec->modelname, "generic")) |
| 226 | #else |
| 227 | #define is_generic_config(codec) 0 |
| 228 | #endif |
| 229 | |
| 230 | /** |
| 231 | * snd_hda_codec_configure - (Re-)configure the HD-audio codec |
| 232 | * @codec: the HDA codec |
| 233 | * |
| 234 | * Start parsing of the given codec tree and (re-)initialize the whole |
| 235 | * patch instance. |
| 236 | * |
| 237 | * Returns 0 if successful or a negative error code. |
| 238 | */ |
| 239 | int snd_hda_codec_configure(struct hda_codec *codec) |
| 240 | { |
| 241 | int err; |
| 242 | |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 243 | if (is_generic_config(codec)) |
| 244 | codec->probe_id = HDA_CODEC_ID_GENERIC; |
| 245 | else |
| 246 | codec->probe_id = 0; |
| 247 | |
Takashi Iwai | 3256be6 | 2015-02-24 14:59:42 +0100 | [diff] [blame] | 248 | err = snd_hdac_device_register(&codec->core); |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 249 | if (err < 0) |
| 250 | return err; |
| 251 | |
| 252 | if (!codec->preset) |
| 253 | codec_bind_module(codec); |
| 254 | if (!codec->preset) { |
| 255 | err = codec_bind_generic(codec); |
| 256 | if (err < 0) { |
| 257 | codec_err(codec, "Unable to bind the codec\n"); |
| 258 | goto error; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | /* audio codec should override the mixer name */ |
Takashi Iwai | 7639a06 | 2015-03-03 10:07:24 +0100 | [diff] [blame] | 263 | if (codec->core.afg || !*codec->card->mixername) |
Takashi Iwai | 6efdd85 | 2015-02-27 16:09:22 +0100 | [diff] [blame] | 264 | snprintf(codec->card->mixername, |
Takashi Iwai | 7639a06 | 2015-03-03 10:07:24 +0100 | [diff] [blame] | 265 | sizeof(codec->card->mixername), "%s %s", |
| 266 | codec->core.vendor_name, codec->core.chip_name); |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 267 | return 0; |
| 268 | |
| 269 | error: |
Takashi Iwai | 3256be6 | 2015-02-24 14:59:42 +0100 | [diff] [blame] | 270 | snd_hdac_device_unregister(&codec->core); |
Takashi Iwai | d8a766a | 2015-02-17 15:25:37 +0100 | [diff] [blame] | 271 | return err; |
| 272 | } |
| 273 | EXPORT_SYMBOL_GPL(snd_hda_codec_configure); |