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