hda_bind.c 7.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 * HD-audio codec driver binding
 * Copyright (c) Takashi Iwai <tiwai@suse.de>
 */

#include <linux/init.h>
#include <linux/slab.h>
#include <linux/mutex.h>
#include <linux/module.h>
#include <linux/export.h>
11
#include <linux/pm.h>
12
#include <linux/pm_runtime.h>
13 14 15 16 17
#include <sound/core.h>
#include "hda_codec.h"
#include "hda_local.h"

/*
18
 * find a matching codec id
19
 */
20
static int hda_codec_match(struct hdac_device *dev, struct hdac_driver *drv)
21
{
22
	struct hda_codec *codec = container_of(dev, struct hda_codec, core);
23
	struct hda_codec_driver *driver =
24
		container_of(drv, struct hda_codec_driver, core);
25
	const struct hda_device_id *list;
26
	/* check probe_id instead of vendor_id if set */
27
	u32 id = codec->probe_id ? codec->probe_id : codec->core.vendor_id;
28
	u32 rev_id = codec->core.revision_id;
29

30 31 32 33
	for (list = driver->id; list->vendor_id; list++) {
		if (list->vendor_id == id &&
		    (!list->rev_id || list->rev_id == rev_id)) {
			codec->preset = list;
34 35 36 37 38 39
			return 1;
		}
	}
	return 0;
}

40 41 42 43 44
/* process an unsolicited event */
static void hda_codec_unsol_event(struct hdac_device *dev, unsigned int ev)
{
	struct hda_codec *codec = container_of(dev, struct hda_codec, core);

45 46 47 48
	/* ignore unsol events during shutdown */
	if (codec->bus->shutdown)
		return;

49 50 51 52
	if (codec->patch_ops.unsol_event)
		codec->patch_ops.unsol_event(codec, ev);
}

53 54 55 56 57 58
/**
 * snd_hda_codec_set_name - set the codec name
 * @codec: the HDA codec
 * @name: name string to set
 */
int snd_hda_codec_set_name(struct hda_codec *codec, const char *name)
59
{
60 61 62 63 64 65 66 67 68
	int err;

	if (!name)
		return 0;
	err = snd_hdac_device_set_chip_name(&codec->core, name);
	if (err < 0)
		return err;

	/* update the mixer name */
69
	if (!*codec->card->mixername ||
70
	    codec->bus->mixer_assigned >= codec->core.addr) {
71 72 73
		snprintf(codec->card->mixername,
			 sizeof(codec->card->mixername), "%s %s",
			 codec->core.vendor_name, codec->core.chip_name);
74
		codec->bus->mixer_assigned = codec->core.addr;
75
	}
76 77

	return 0;
78
}
79
EXPORT_SYMBOL_GPL(snd_hda_codec_set_name);
80 81 82 83 84

static int hda_codec_driver_probe(struct device *dev)
{
	struct hda_codec *codec = dev_to_hda_codec(dev);
	struct module *owner = dev->driver->owner;
85
	hda_codec_patch_t patch;
86 87 88 89 90
	int err;

	if (WARN_ON(!codec->preset))
		return -EINVAL;

91
	err = snd_hda_codec_set_name(codec, codec->preset->name);
T
Takashi Iwai 已提交
92 93 94
	if (err < 0)
		goto error;
	err = snd_hdac_regmap_init(&codec->core);
95 96 97 98 99 100 101 102
	if (err < 0)
		goto error;

	if (!try_module_get(owner)) {
		err = -EINVAL;
		goto error;
	}

103 104 105 106
	patch = (hda_codec_patch_t)codec->preset->driver_data;
	if (patch) {
		err = patch(codec);
		if (err < 0)
107
			goto error_module_put;
108
	}
109 110 111 112 113 114 115

	err = snd_hda_codec_build_pcms(codec);
	if (err < 0)
		goto error_module;
	err = snd_hda_codec_build_controls(codec);
	if (err < 0)
		goto error_module;
116 117
	/* only register after the bus probe finished; otherwise it's racy */
	if (!codec->bus->bus_probing && codec->card->registered) {
118 119 120
		err = snd_card_register(codec->card);
		if (err < 0)
			goto error_module;
121
		snd_hda_codec_register(codec);
122 123
	}

T
Takashi Iwai 已提交
124
	codec->core.lazy_cache = true;
125 126
	return 0;

127
 error_module:
128 129 130
	if (codec->patch_ops.free)
		codec->patch_ops.free(codec);
 error_module_put:
131 132
	module_put(owner);

133
 error:
134
	snd_hda_codec_cleanup_for_unbind(codec);
135 136 137 138 139 140 141 142 143
	return err;
}

static int hda_codec_driver_remove(struct device *dev)
{
	struct hda_codec *codec = dev_to_hda_codec(dev);

	if (codec->patch_ops.free)
		codec->patch_ops.free(codec);
144
	snd_hda_codec_cleanup_for_unbind(codec);
145 146 147 148
	module_put(dev->driver->owner);
	return 0;
}

149 150 151 152 153 154 155 156
static void hda_codec_driver_shutdown(struct device *dev)
{
	struct hda_codec *codec = dev_to_hda_codec(dev);

	if (!pm_runtime_suspended(dev) && codec->patch_ops.reboot_notify)
		codec->patch_ops.reboot_notify(codec);
}

157 158 159
int __hda_codec_driver_register(struct hda_codec_driver *drv, const char *name,
			       struct module *owner)
{
160 161 162 163 164 165 166 167 168
	drv->core.driver.name = name;
	drv->core.driver.owner = owner;
	drv->core.driver.bus = &snd_hda_bus_type;
	drv->core.driver.probe = hda_codec_driver_probe;
	drv->core.driver.remove = hda_codec_driver_remove;
	drv->core.driver.shutdown = hda_codec_driver_shutdown;
	drv->core.driver.pm = &hda_codec_driver_pm;
	drv->core.type = HDA_DEV_LEGACY;
	drv->core.match = hda_codec_match;
169
	drv->core.unsol_event = hda_codec_unsol_event;
170
	return driver_register(&drv->core.driver);
171 172 173 174 175
}
EXPORT_SYMBOL_GPL(__hda_codec_driver_register);

void hda_codec_driver_unregister(struct hda_codec_driver *drv)
{
176
	driver_unregister(&drv->core.driver);
177 178 179 180 181 182 183 184
}
EXPORT_SYMBOL_GPL(hda_codec_driver_unregister);

static inline bool codec_probed(struct hda_codec *codec)
{
	return device_attach(hda_codec_dev(codec)) > 0 && codec->preset;
}

185 186
/* try to auto-load codec module */
static void request_codec_module(struct hda_codec *codec)
187 188
{
#ifdef MODULE
189
	char modalias[32];
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
	const char *mod = NULL;

	switch (codec->probe_id) {
	case HDA_CODEC_ID_GENERIC_HDMI:
#if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI)
		mod = "snd-hda-codec-hdmi";
#endif
		break;
	case HDA_CODEC_ID_GENERIC:
#if IS_MODULE(CONFIG_SND_HDA_GENERIC)
		mod = "snd-hda-codec-generic";
#endif
		break;
	default:
		snd_hdac_codec_modalias(&codec->core, modalias, sizeof(modalias));
		mod = modalias;
		break;
	}

	if (mod)
		request_module(mod);
#endif /* MODULE */
}
213

214 215 216 217 218
/* try to auto-load and bind the codec module */
static void codec_bind_module(struct hda_codec *codec)
{
#ifdef MODULE
	request_codec_module(codec);
219 220 221 222 223 224 225 226 227
	if (codec_probed(codec))
		return;
#endif
}

#if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
/* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
static bool is_likely_hdmi_codec(struct hda_codec *codec)
{
228
	hda_nid_t nid;
229

230
	for_each_hda_codec_node(nid, codec) {
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
		unsigned int wcaps = get_wcaps(codec, nid);
		switch (get_wcaps_type(wcaps)) {
		case AC_WID_AUD_IN:
			return false; /* HDMI parser supports only HDMI out */
		case AC_WID_AUD_OUT:
			if (!(wcaps & AC_WCAP_DIGITAL))
				return false;
			break;
		}
	}
	return true;
}
#else
/* no HDMI codec parser support */
#define is_likely_hdmi_codec(codec)	false
#endif /* CONFIG_SND_HDA_CODEC_HDMI */

static int codec_bind_generic(struct hda_codec *codec)
{
	if (codec->probe_id)
		return -ENODEV;

	if (is_likely_hdmi_codec(codec)) {
		codec->probe_id = HDA_CODEC_ID_GENERIC_HDMI;
255
		request_codec_module(codec);
256 257 258 259 260
		if (codec_probed(codec))
			return 0;
	}

	codec->probe_id = HDA_CODEC_ID_GENERIC;
261
	request_codec_module(codec);
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
	if (codec_probed(codec))
		return 0;
	return -ENODEV;
}

#if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
#define is_generic_config(codec) \
	(codec->modelname && !strcmp(codec->modelname, "generic"))
#else
#define is_generic_config(codec)	0
#endif

/**
 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
 * @codec: the HDA codec
 *
 * Start parsing of the given codec tree and (re-)initialize the whole
 * patch instance.
 *
 * Returns 0 if successful or a negative error code.
 */
int snd_hda_codec_configure(struct hda_codec *codec)
{
	int err;

	if (is_generic_config(codec))
		codec->probe_id = HDA_CODEC_ID_GENERIC;
	else
		codec->probe_id = 0;

T
Takashi Iwai 已提交
292
	err = snd_hdac_device_register(&codec->core);
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
	if (err < 0)
		return err;

	if (!codec->preset)
		codec_bind_module(codec);
	if (!codec->preset) {
		err = codec_bind_generic(codec);
		if (err < 0) {
			codec_err(codec, "Unable to bind the codec\n");
			goto error;
		}
	}

	return 0;

 error:
T
Takashi Iwai 已提交
309
	snd_hdac_device_unregister(&codec->core);
310 311 312
	return err;
}
EXPORT_SYMBOL_GPL(snd_hda_codec_configure);