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