blob: d0d6dfbfcfdf8731a76aeac7664dbd007ee970e4 [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/*
Takashi Iwaib9a94a92015-10-01 16:20:04 +020018 * find a matching codec id
Takashi Iwaid8a766a2015-02-17 15:25:37 +010019 */
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 Iwaib9a94a92015-10-01 16:20:04 +020025 const struct hda_device_id *list;
Takashi Iwaid8a766a2015-02-17 15:25:37 +010026 /* 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 Iwaib9a94a92015-10-01 16:20:04 +020028 u32 rev_id = codec->core.revision_id;
Takashi Iwaid8a766a2015-02-17 15:25:37 +010029
Takashi Iwaib9a94a92015-10-01 16:20:04 +020030 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 Iwaid8a766a2015-02-17 15:25:37 +010034 return 1;
35 }
36 }
37 return 0;
38}
39
Takashi Iwaid068ebc2015-03-02 23:22:59 +010040/* process an unsolicited event */
41static 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
Takashi Iwai6693e852019-10-28 11:58:03 +010045 /* ignore unsol events during shutdown */
46 if (codec->bus->shutdown)
47 return;
48
Takashi Iwaid068ebc2015-03-02 23:22:59 +010049 if (codec->patch_ops.unsol_event)
50 codec->patch_ops.unsol_event(codec, ev);
51}
52
Takashi Iwaided255b2015-10-01 17:59:43 +020053/**
54 * snd_hda_codec_set_name - set the codec name
55 * @codec: the HDA codec
56 * @name: name string to set
57 */
58int snd_hda_codec_set_name(struct hda_codec *codec, const char *name)
Takashi Iwaid8a766a2015-02-17 15:25:37 +010059{
Takashi Iwaided255b2015-10-01 17:59:43 +020060 int err;
61
62 if (!name)
63 return 0;
64 err = snd_hdac_device_set_chip_name(&codec->core, name);
65 if (err < 0)
66 return err;
67
68 /* update the mixer name */
Takashi Iwai7fbe8242015-10-15 14:06:14 +020069 if (!*codec->card->mixername ||
Takashi Iwai2f0eaad2015-10-17 18:25:38 +020070 codec->bus->mixer_assigned >= codec->core.addr) {
Takashi Iwaided255b2015-10-01 17:59:43 +020071 snprintf(codec->card->mixername,
72 sizeof(codec->card->mixername), "%s %s",
73 codec->core.vendor_name, codec->core.chip_name);
Takashi Iwai2f0eaad2015-10-17 18:25:38 +020074 codec->bus->mixer_assigned = codec->core.addr;
Takashi Iwaid8a766a2015-02-17 15:25:37 +010075 }
Takashi Iwaided255b2015-10-01 17:59:43 +020076
77 return 0;
Takashi Iwaid8a766a2015-02-17 15:25:37 +010078}
Takashi Iwaided255b2015-10-01 17:59:43 +020079EXPORT_SYMBOL_GPL(snd_hda_codec_set_name);
Takashi Iwaid8a766a2015-02-17 15:25:37 +010080
81static int hda_codec_driver_probe(struct device *dev)
82{
83 struct hda_codec *codec = dev_to_hda_codec(dev);
84 struct module *owner = dev->driver->owner;
Takashi Iwaib9a94a92015-10-01 16:20:04 +020085 hda_codec_patch_t patch;
Takashi Iwaid8a766a2015-02-17 15:25:37 +010086 int err;
87
88 if (WARN_ON(!codec->preset))
89 return -EINVAL;
90
Takashi Iwaided255b2015-10-01 17:59:43 +020091 err = snd_hda_codec_set_name(codec, codec->preset->name);
Takashi Iwaid8a766a2015-02-17 15:25:37 +010092 if (err < 0)
93 goto error;
Takashi Iwai4d75faa02015-02-25 14:42:38 +010094 err = snd_hdac_regmap_init(&codec->core);
95 if (err < 0)
96 goto error;
Takashi Iwaid8a766a2015-02-17 15:25:37 +010097
98 if (!try_module_get(owner)) {
99 err = -EINVAL;
100 goto error;
101 }
102
Takashi Iwaib9a94a92015-10-01 16:20:04 +0200103 patch = (hda_codec_patch_t)codec->preset->driver_data;
104 if (patch) {
105 err = patch(codec);
106 if (err < 0)
107 goto error_module;
108 }
Takashi Iwaibcd96552015-02-27 20:44:54 +0100109
110 err = snd_hda_codec_build_pcms(codec);
111 if (err < 0)
112 goto error_module;
113 err = snd_hda_codec_build_controls(codec);
114 if (err < 0)
115 goto error_module;
Takashi Iwai18c071d2019-01-30 17:46:03 +0100116 /* only register after the bus probe finished; otherwise it's racy */
117 if (!codec->bus->bus_probing && codec->card->registered) {
Takashi Iwaibcd96552015-02-27 20:44:54 +0100118 err = snd_card_register(codec->card);
119 if (err < 0)
120 goto error_module;
Takashi Iwaic4c25332015-03-03 17:22:12 +0100121 snd_hda_codec_register(codec);
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100122 }
123
Takashi Iwai4d75faa02015-02-25 14:42:38 +0100124 codec->core.lazy_cache = true;
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100125 return 0;
126
Takashi Iwaibcd96552015-02-27 20:44:54 +0100127 error_module:
128 module_put(owner);
129
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100130 error:
Takashi Iwaibcd96552015-02-27 20:44:54 +0100131 snd_hda_codec_cleanup_for_unbind(codec);
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100132 return err;
133}
134
135static int hda_codec_driver_remove(struct device *dev)
136{
137 struct hda_codec *codec = dev_to_hda_codec(dev);
138
139 if (codec->patch_ops.free)
140 codec->patch_ops.free(codec);
Takashi Iwai9a6246f2015-02-27 18:17:28 +0100141 snd_hda_codec_cleanup_for_unbind(codec);
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100142 module_put(dev->driver->owner);
143 return 0;
144}
145
Takashi Iwaib2a0baf2015-03-05 17:21:32 +0100146static void hda_codec_driver_shutdown(struct device *dev)
147{
148 struct hda_codec *codec = dev_to_hda_codec(dev);
149
150 if (!pm_runtime_suspended(dev) && codec->patch_ops.reboot_notify)
151 codec->patch_ops.reboot_notify(codec);
152}
153
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100154int __hda_codec_driver_register(struct hda_codec_driver *drv, const char *name,
155 struct module *owner)
156{
Takashi Iwaie3d280f2015-02-17 21:46:37 +0100157 drv->core.driver.name = name;
158 drv->core.driver.owner = owner;
159 drv->core.driver.bus = &snd_hda_bus_type;
160 drv->core.driver.probe = hda_codec_driver_probe;
161 drv->core.driver.remove = hda_codec_driver_remove;
162 drv->core.driver.shutdown = hda_codec_driver_shutdown;
163 drv->core.driver.pm = &hda_codec_driver_pm;
164 drv->core.type = HDA_DEV_LEGACY;
165 drv->core.match = hda_codec_match;
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100166 drv->core.unsol_event = hda_codec_unsol_event;
Takashi Iwaie3d280f2015-02-17 21:46:37 +0100167 return driver_register(&drv->core.driver);
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100168}
169EXPORT_SYMBOL_GPL(__hda_codec_driver_register);
170
171void hda_codec_driver_unregister(struct hda_codec_driver *drv)
172{
Takashi Iwaie3d280f2015-02-17 21:46:37 +0100173 driver_unregister(&drv->core.driver);
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100174}
175EXPORT_SYMBOL_GPL(hda_codec_driver_unregister);
176
177static inline bool codec_probed(struct hda_codec *codec)
178{
179 return device_attach(hda_codec_dev(codec)) > 0 && codec->preset;
180}
181
Takashi Iwaibca8e982016-01-20 15:46:44 +0100182/* try to auto-load codec module */
183static void request_codec_module(struct hda_codec *codec)
184{
185#ifdef MODULE
186 char modalias[32];
187 const char *mod = NULL;
188
189 switch (codec->probe_id) {
190 case HDA_CODEC_ID_GENERIC_HDMI:
191#if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI)
192 mod = "snd-hda-codec-hdmi";
193#endif
194 break;
195 case HDA_CODEC_ID_GENERIC:
196#if IS_MODULE(CONFIG_SND_HDA_GENERIC)
197 mod = "snd-hda-codec-generic";
198#endif
199 break;
200 default:
201 snd_hdac_codec_modalias(&codec->core, modalias, sizeof(modalias));
202 mod = modalias;
203 break;
204 }
205
206 if (mod)
207 request_module(mod);
208#endif /* MODULE */
209}
210
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100211/* try to auto-load and bind the codec module */
212static void codec_bind_module(struct hda_codec *codec)
213{
214#ifdef MODULE
Takashi Iwaibca8e982016-01-20 15:46:44 +0100215 request_codec_module(codec);
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100216 if (codec_probed(codec))
217 return;
218#endif
219}
220
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100221#if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
222/* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
223static bool is_likely_hdmi_codec(struct hda_codec *codec)
224{
Takashi Iwai7639a062015-03-03 10:07:24 +0100225 hda_nid_t nid;
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100226
Takashi Iwai7639a062015-03-03 10:07:24 +0100227 for_each_hda_codec_node(nid, codec) {
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100228 unsigned int wcaps = get_wcaps(codec, nid);
229 switch (get_wcaps_type(wcaps)) {
230 case AC_WID_AUD_IN:
231 return false; /* HDMI parser supports only HDMI out */
232 case AC_WID_AUD_OUT:
233 if (!(wcaps & AC_WCAP_DIGITAL))
234 return false;
235 break;
236 }
237 }
238 return true;
239}
240#else
241/* no HDMI codec parser support */
242#define is_likely_hdmi_codec(codec) false
243#endif /* CONFIG_SND_HDA_CODEC_HDMI */
244
245static int codec_bind_generic(struct hda_codec *codec)
246{
247 if (codec->probe_id)
248 return -ENODEV;
249
250 if (is_likely_hdmi_codec(codec)) {
251 codec->probe_id = HDA_CODEC_ID_GENERIC_HDMI;
Takashi Iwaibca8e982016-01-20 15:46:44 +0100252 request_codec_module(codec);
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100253 if (codec_probed(codec))
254 return 0;
255 }
256
257 codec->probe_id = HDA_CODEC_ID_GENERIC;
Takashi Iwaibca8e982016-01-20 15:46:44 +0100258 request_codec_module(codec);
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100259 if (codec_probed(codec))
260 return 0;
261 return -ENODEV;
262}
263
264#if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
265#define is_generic_config(codec) \
266 (codec->modelname && !strcmp(codec->modelname, "generic"))
267#else
268#define is_generic_config(codec) 0
269#endif
270
271/**
272 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
273 * @codec: the HDA codec
274 *
275 * Start parsing of the given codec tree and (re-)initialize the whole
276 * patch instance.
277 *
278 * Returns 0 if successful or a negative error code.
279 */
280int snd_hda_codec_configure(struct hda_codec *codec)
281{
282 int err;
283
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100284 if (is_generic_config(codec))
285 codec->probe_id = HDA_CODEC_ID_GENERIC;
286 else
287 codec->probe_id = 0;
288
Takashi Iwai3256be62015-02-24 14:59:42 +0100289 err = snd_hdac_device_register(&codec->core);
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100290 if (err < 0)
291 return err;
292
293 if (!codec->preset)
294 codec_bind_module(codec);
295 if (!codec->preset) {
296 err = codec_bind_generic(codec);
297 if (err < 0) {
298 codec_err(codec, "Unable to bind the codec\n");
299 goto error;
300 }
301 }
302
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100303 return 0;
304
305 error:
Takashi Iwai3256be62015-02-24 14:59:42 +0100306 snd_hdac_device_unregister(&codec->core);
Takashi Iwaid8a766a2015-02-17 15:25:37 +0100307 return err;
308}
309EXPORT_SYMBOL_GPL(snd_hda_codec_configure);