blob: 7b269c3237e3cdc1da0cf7efd68d3ea9378ade7f [file] [log] [blame]
Takashi Iwaid8a766a2015-02-17 15:25:37 +01001/*
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 Iwai59ed1ea2015-02-18 15:39:59 +010011#include <linux/pm.h>
Takashi Iwaib2a0baf2015-03-05 17:21:32 +010012#include <linux/pm_runtime.h>
Takashi Iwaid8a766a2015-02-17 15:25:37 +010013#include <sound/core.h>
14#include "hda_codec.h"
15#include "hda_local.h"
16
Takashi Iwaid8a766a2015-02-17 15:25:37 +010017/*
18 * find a matching codec preset
19 */
Takashi Iwaie3d280f2015-02-17 21:46:37 +010020static int hda_codec_match(struct hdac_device *dev, struct hdac_driver *drv)
Takashi Iwaid8a766a2015-02-17 15:25:37 +010021{
Takashi Iwaie3d280f2015-02-17 21:46:37 +010022 struct hda_codec *codec = container_of(dev, struct hda_codec, core);
Takashi Iwaid8a766a2015-02-17 15:25:37 +010023 struct hda_codec_driver *driver =
Takashi Iwaie3d280f2015-02-17 21:46:37 +010024 container_of(drv, struct hda_codec_driver, core);
Takashi Iwaid8a766a2015-02-17 15:25:37 +010025 const struct hda_codec_preset *preset;
26 /* check probe_id instead of vendor_id if set */
Takashi Iwai7639a062015-03-03 10:07:24 +010027 u32 id = codec->probe_id ? codec->probe_id : codec->core.vendor_id;
Takashi Iwaid8a766a2015-02-17 15:25:37 +010028
29 for (preset = driver->preset; preset->id; preset++) {
30 u32 mask = preset->mask;
31
Takashi Iwai7639a062015-03-03 10:07:24 +010032 if (preset->afg && preset->afg != codec->core.afg)
Takashi Iwaid8a766a2015-02-17 15:25:37 +010033 continue;
Takashi Iwai7639a062015-03-03 10:07:24 +010034 if (preset->mfg && preset->mfg != codec->core.mfg)
Takashi Iwaid8a766a2015-02-17 15:25:37 +010035 continue;
36 if (!mask)
37 mask = ~0;
38 if (preset->id == (id & mask) &&
Takashi Iwai7639a062015-03-03 10:07:24 +010039 (!preset->rev || preset->rev == codec->core.revision_id)) {
Takashi Iwaid8a766a2015-02-17 15:25:37 +010040 codec->preset = preset;
41 return 1;
42 }
43 }
44 return 0;
45}
46
Takashi Iwaid068ebc2015-03-02 23:22:59 +010047/* process an unsolicited event */
48static 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 Iwaid8a766a2015-02-17 15:25:37 +010056/* reset the codec name from the preset */
57static int codec_refresh_name(struct hda_codec *codec, const char *name)
58{
Takashi Iwai7639a062015-03-03 10:07:24 +010059 if (name) {
60 kfree(codec->core.chip_name);
61 codec->core.chip_name = kstrdup(name, GFP_KERNEL);
Takashi Iwaid8a766a2015-02-17 15:25:37 +010062 }
Takashi Iwai7639a062015-03-03 10:07:24 +010063 return codec->core.chip_name ? 0 : -ENOMEM;
Takashi Iwaid8a766a2015-02-17 15:25:37 +010064}
65
66static 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;
78
79 if (!try_module_get(owner)) {
80 err = -EINVAL;
81 goto error;
82 }
83
84 err = codec->preset->patch(codec);
Takashi Iwaibcd96552015-02-27 20:44:54 +010085 if (err < 0)
86 goto error_module;
87
88 err = snd_hda_codec_build_pcms(codec);
89 if (err < 0)
90 goto error_module;
91 err = snd_hda_codec_build_controls(codec);
92 if (err < 0)
93 goto error_module;
94 if (codec->card->registered) {
95 err = snd_card_register(codec->card);
96 if (err < 0)
97 goto error_module;
Takashi Iwaic4c25332015-03-03 17:22:12 +010098 snd_hda_codec_register(codec);
Takashi Iwaid8a766a2015-02-17 15:25:37 +010099 }
100
101 return 0;
102
Takashi Iwaibcd96552015-02-27 20:44:54 +0100103 error_module:
104 module_put(owner);
105
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100106 error:
Takashi Iwaibcd96552015-02-27 20:44:54 +0100107 snd_hda_codec_cleanup_for_unbind(codec);
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100108 return err;
109}
110
111static int hda_codec_driver_remove(struct device *dev)
112{
113 struct hda_codec *codec = dev_to_hda_codec(dev);
114
115 if (codec->patch_ops.free)
116 codec->patch_ops.free(codec);
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100117 snd_hda_codec_cleanup_for_unbind(codec);
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100118 module_put(dev->driver->owner);
119 return 0;
120}
121
Takashi Iwaib2a0baf2015-03-05 17:21:32 +0100122static void hda_codec_driver_shutdown(struct device *dev)
123{
124 struct hda_codec *codec = dev_to_hda_codec(dev);
125
126 if (!pm_runtime_suspended(dev) && codec->patch_ops.reboot_notify)
127 codec->patch_ops.reboot_notify(codec);
128}
129
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100130int __hda_codec_driver_register(struct hda_codec_driver *drv, const char *name,
131 struct module *owner)
132{
Takashi Iwaie3d280f2015-02-17 21:46:37 +0100133 drv->core.driver.name = name;
134 drv->core.driver.owner = owner;
135 drv->core.driver.bus = &snd_hda_bus_type;
136 drv->core.driver.probe = hda_codec_driver_probe;
137 drv->core.driver.remove = hda_codec_driver_remove;
138 drv->core.driver.shutdown = hda_codec_driver_shutdown;
139 drv->core.driver.pm = &hda_codec_driver_pm;
140 drv->core.type = HDA_DEV_LEGACY;
141 drv->core.match = hda_codec_match;
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100142 drv->core.unsol_event = hda_codec_unsol_event;
Takashi Iwaie3d280f2015-02-17 21:46:37 +0100143 return driver_register(&drv->core.driver);
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100144}
145EXPORT_SYMBOL_GPL(__hda_codec_driver_register);
146
147void hda_codec_driver_unregister(struct hda_codec_driver *drv)
148{
Takashi Iwaie3d280f2015-02-17 21:46:37 +0100149 driver_unregister(&drv->core.driver);
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100150}
151EXPORT_SYMBOL_GPL(hda_codec_driver_unregister);
152
153static inline bool codec_probed(struct hda_codec *codec)
154{
155 return device_attach(hda_codec_dev(codec)) > 0 && codec->preset;
156}
157
158/* try to auto-load and bind the codec module */
159static void codec_bind_module(struct hda_codec *codec)
160{
161#ifdef MODULE
Takashi Iwai7639a062015-03-03 10:07:24 +0100162 request_module("snd-hda-codec-id:%08x", codec->core.vendor_id);
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100163 if (codec_probed(codec))
164 return;
165 request_module("snd-hda-codec-id:%04x*",
Takashi Iwai7639a062015-03-03 10:07:24 +0100166 (codec->core.vendor_id >> 16) & 0xffff);
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100167 if (codec_probed(codec))
168 return;
169#endif
170}
171
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100172#if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
173/* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
174static bool is_likely_hdmi_codec(struct hda_codec *codec)
175{
Takashi Iwai7639a062015-03-03 10:07:24 +0100176 hda_nid_t nid;
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100177
Takashi Iwai7639a062015-03-03 10:07:24 +0100178 for_each_hda_codec_node(nid, codec) {
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100179 unsigned int wcaps = get_wcaps(codec, nid);
180 switch (get_wcaps_type(wcaps)) {
181 case AC_WID_AUD_IN:
182 return false; /* HDMI parser supports only HDMI out */
183 case AC_WID_AUD_OUT:
184 if (!(wcaps & AC_WCAP_DIGITAL))
185 return false;
186 break;
187 }
188 }
189 return true;
190}
191#else
192/* no HDMI codec parser support */
193#define is_likely_hdmi_codec(codec) false
194#endif /* CONFIG_SND_HDA_CODEC_HDMI */
195
196static int codec_bind_generic(struct hda_codec *codec)
197{
198 if (codec->probe_id)
199 return -ENODEV;
200
201 if (is_likely_hdmi_codec(codec)) {
202 codec->probe_id = HDA_CODEC_ID_GENERIC_HDMI;
203#if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI)
204 request_module("snd-hda-codec-hdmi");
205#endif
206 if (codec_probed(codec))
207 return 0;
208 }
209
210 codec->probe_id = HDA_CODEC_ID_GENERIC;
211#if IS_MODULE(CONFIG_SND_HDA_GENERIC)
212 request_module("snd-hda-codec-generic");
213#endif
214 if (codec_probed(codec))
215 return 0;
216 return -ENODEV;
217}
218
219#if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
220#define is_generic_config(codec) \
221 (codec->modelname && !strcmp(codec->modelname, "generic"))
222#else
223#define is_generic_config(codec) 0
224#endif
225
226/**
227 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
228 * @codec: the HDA codec
229 *
230 * Start parsing of the given codec tree and (re-)initialize the whole
231 * patch instance.
232 *
233 * Returns 0 if successful or a negative error code.
234 */
235int snd_hda_codec_configure(struct hda_codec *codec)
236{
237 int err;
238
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100239 if (is_generic_config(codec))
240 codec->probe_id = HDA_CODEC_ID_GENERIC;
241 else
242 codec->probe_id = 0;
243
Takashi Iwai3256be62015-02-24 14:59:42 +0100244 err = snd_hdac_device_register(&codec->core);
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100245 if (err < 0)
246 return err;
247
248 if (!codec->preset)
249 codec_bind_module(codec);
250 if (!codec->preset) {
251 err = codec_bind_generic(codec);
252 if (err < 0) {
253 codec_err(codec, "Unable to bind the codec\n");
254 goto error;
255 }
256 }
257
258 /* audio codec should override the mixer name */
Takashi Iwai7639a062015-03-03 10:07:24 +0100259 if (codec->core.afg || !*codec->card->mixername)
Takashi Iwai6efdd852015-02-27 16:09:22 +0100260 snprintf(codec->card->mixername,
Takashi Iwai7639a062015-03-03 10:07:24 +0100261 sizeof(codec->card->mixername), "%s %s",
262 codec->core.vendor_name, codec->core.chip_name);
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100263 return 0;
264
265 error:
Takashi Iwai3256be62015-02-24 14:59:42 +0100266 snd_hdac_device_unregister(&codec->core);
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100267 return err;
268}
269EXPORT_SYMBOL_GPL(snd_hda_codec_configure);