hda_codec.c 148 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Universal Interface for Intel High Definition Audio Codec
 *
 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
 *
 *
 *  This driver is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This driver is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

22
#include <linux/mm.h>
Linus Torvalds's avatar
Linus Torvalds committed
23 24 25 26
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/pci.h>
27
#include <linux/mutex.h>
28
#include <linux/module.h>
Linus Torvalds's avatar
Linus Torvalds committed
29 30 31
#include <sound/core.h>
#include "hda_codec.h"
#include <sound/asoundef.h>
32
#include <sound/tlv.h>
Linus Torvalds's avatar
Linus Torvalds committed
33
#include <sound/initval.h>
34
#include <sound/jack.h>
Linus Torvalds's avatar
Linus Torvalds committed
35
#include "hda_local.h"
36
#include "hda_beep.h"
37
#include "hda_jack.h"
38
#include <sound/hda_hwdep.h>
Linus Torvalds's avatar
Linus Torvalds committed
39

40 41 42
#define CREATE_TRACE_POINTS
#include "hda_trace.h"

Linus Torvalds's avatar
Linus Torvalds committed
43 44 45 46 47 48 49 50 51 52 53
/*
 * vendor / preset table
 */

struct hda_vendor_id {
	unsigned int id;
	const char *name;
};

/* codec vendor labels */
static struct hda_vendor_id hda_vendor_ids[] = {
54
	{ 0x1002, "ATI" },
55
	{ 0x1013, "Cirrus Logic" },
56
	{ 0x1057, "Motorola" },
57
	{ 0x1095, "Silicon Image" },
58
	{ 0x10de, "Nvidia" },
59
	{ 0x10ec, "Realtek" },
60
	{ 0x1102, "Creative" },
61
	{ 0x1106, "VIA" },
62
	{ 0x111d, "IDT" },
63
	{ 0x11c1, "LSI" },
64
	{ 0x11d4, "Analog Devices" },
Linus Torvalds's avatar
Linus Torvalds committed
65
	{ 0x13f6, "C-Media" },
66
	{ 0x14f1, "Conexant" },
67 68
	{ 0x17e8, "Chrontel" },
	{ 0x1854, "LG" },
69
	{ 0x1aec, "Wolfson Microelectronics" },
Linus Torvalds's avatar
Linus Torvalds committed
70
	{ 0x434d, "C-Media" },
71
	{ 0x8086, "Intel" },
Matt's avatar
Matt committed
72
	{ 0x8384, "SigmaTel" },
Linus Torvalds's avatar
Linus Torvalds committed
73 74 75
	{} /* terminator */
};

76 77 78 79 80 81 82 83 84 85
static DEFINE_MUTEX(preset_mutex);
static LIST_HEAD(hda_preset_tables);

int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
{
	mutex_lock(&preset_mutex);
	list_add_tail(&preset->list, &hda_preset_tables);
	mutex_unlock(&preset_mutex);
	return 0;
}
86
EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
87 88 89 90 91 92 93 94

int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
{
	mutex_lock(&preset_mutex);
	list_del(&preset->list);
	mutex_unlock(&preset_mutex);
	return 0;
}
95
EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
Linus Torvalds's avatar
Linus Torvalds committed
96

97
#ifdef CONFIG_PM
98
#define codec_in_pm(codec)	((codec)->in_pm)
99 100
static void hda_power_work(struct work_struct *work);
static void hda_keep_power_on(struct hda_codec *codec);
101
#define hda_codec_is_power_on(codec)	((codec)->power_on)
102 103 104 105 106
static inline void hda_call_pm_notify(struct hda_bus *bus, bool power_up)
{
	if (bus->ops.pm_notify)
		bus->ops.pm_notify(bus, power_up);
}
107
#else
108
#define codec_in_pm(codec)	0
109
static inline void hda_keep_power_on(struct hda_codec *codec) {}
110
#define hda_codec_is_power_on(codec)	1
111
#define hda_call_pm_notify(bus, state) {}
112 113
#endif

114 115 116 117 118 119 120
/**
 * snd_hda_get_jack_location - Give a location string of the jack
 * @cfg: pin default config value
 *
 * Parse the pin default config value and returns the string of the
 * jack location, e.g. "Rear", "Front", etc.
 */
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
const char *snd_hda_get_jack_location(u32 cfg)
{
	static char *bases[7] = {
		"N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
	};
	static unsigned char specials_idx[] = {
		0x07, 0x08,
		0x17, 0x18, 0x19,
		0x37, 0x38
	};
	static char *specials[] = {
		"Rear Panel", "Drive Bar",
		"Riser", "HDMI", "ATAPI",
		"Mobile-In", "Mobile-Out"
	};
	int i;
	cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
	if ((cfg & 0x0f) < 7)
		return bases[cfg & 0x0f];
	for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
		if (cfg == specials_idx[i])
			return specials[i];
	}
	return "UNKNOWN";
}
146
EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
147

148 149 150 151 152 153 154
/**
 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
 * @cfg: pin default config value
 *
 * Parse the pin default config value and returns the string of the
 * jack connectivity, i.e. external or internal connection.
 */
155 156 157 158 159 160
const char *snd_hda_get_jack_connectivity(u32 cfg)
{
	static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };

	return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
}
161
EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
162

163 164 165 166 167 168 169
/**
 * snd_hda_get_jack_type - Give a type string of the jack
 * @cfg: pin default config value
 *
 * Parse the pin default config value and returns the string of the
 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
 */
170 171 172 173 174 175
const char *snd_hda_get_jack_type(u32 cfg)
{
	static char *jack_types[16] = {
		"Line Out", "Speaker", "HP Out", "CD",
		"SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
		"Line In", "Aux", "Mic", "Telephony",
176
		"SPDIF In", "Digital In", "Reserved", "Other"
177 178 179 180 181
	};

	return jack_types[(cfg & AC_DEFCFG_DEVICE)
				>> AC_DEFCFG_DEVICE_SHIFT];
}
182
EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
183

184 185 186 187 188 189 190 191 192
/*
 * Compose a 32bit command word to be sent to the HD-audio controller
 */
static inline unsigned int
make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
	       unsigned int verb, unsigned int parm)
{
	u32 val;

193 194
	if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
	    (verb & ~0xfff) || (parm & ~0xffff)) {
195 196 197 198 199 200
		printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
		       codec->addr, direct, nid, verb, parm);
		return ~0;
	}

	val = (u32)codec->addr << 28;
201 202 203 204 205 206 207
	val |= (u32)direct << 27;
	val |= (u32)nid << 20;
	val |= verb << 8;
	val |= parm;
	return val;
}

208 209 210 211 212 213 214
/*
 * Send and receive a verb
 */
static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
			   unsigned int *res)
{
	struct hda_bus *bus = codec->bus;
215
	int err;
216

217 218 219
	if (cmd == ~0)
		return -1;

220 221
	if (res)
		*res = -1;
222
 again:
223 224
	snd_hda_power_up(codec);
	mutex_lock(&bus->cmd_mutex);
Takashi Iwai's avatar
Takashi Iwai committed
225 226 227 228 229 230 231 232
	for (;;) {
		trace_hda_send_cmd(codec, cmd);
		err = bus->ops.command(bus, cmd);
		if (err != -EAGAIN)
			break;
		/* process pending verbs */
		bus->ops.get_response(bus, codec->addr);
	}
233
	if (!err && res) {
234
		*res = bus->ops.get_response(bus, codec->addr);
235 236
		trace_hda_get_response(codec, *res);
	}
237 238
	mutex_unlock(&bus->cmd_mutex);
	snd_hda_power_down(codec);
239
	if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) {
240 241 242
		if (bus->response_reset) {
			snd_printd("hda_codec: resetting BUS due to "
				   "fatal communication error\n");
243
			trace_hda_bus_reset(bus);
244 245 246 247 248
			bus->ops.bus_reset(bus);
		}
		goto again;
	}
	/* clear reset-flag when the communication gets recovered */
249
	if (!err || codec_in_pm(codec))
250
		bus->response_reset = 0;
251 252 253
	return err;
}

Linus Torvalds's avatar
Linus Torvalds committed
254 255 256 257 258 259 260 261 262 263 264 265
/**
 * snd_hda_codec_read - send a command and get the response
 * @codec: the HDA codec
 * @nid: NID to send the command
 * @direct: direct flag
 * @verb: the verb to send
 * @parm: the parameter for the verb
 *
 * Send a single command and read the corresponding response.
 *
 * Returns the obtained response value, or -1 for an error.
 */
266 267
unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
				int direct,
Linus Torvalds's avatar
Linus Torvalds committed
268 269
				unsigned int verb, unsigned int parm)
{
270 271
	unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
	unsigned int res;
272 273
	if (codec_exec_verb(codec, cmd, &res))
		return -1;
Linus Torvalds's avatar
Linus Torvalds committed
274 275
	return res;
}
276
EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds's avatar
Linus Torvalds committed
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292

/**
 * snd_hda_codec_write - send a single command without waiting for response
 * @codec: the HDA codec
 * @nid: NID to send the command
 * @direct: direct flag
 * @verb: the verb to send
 * @parm: the parameter for the verb
 *
 * Send a single command without waiting for response.
 *
 * Returns 0 if successful, or a negative error code.
 */
int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
			 unsigned int verb, unsigned int parm)
{
293
	unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
294
	unsigned int res;
295 296
	return codec_exec_verb(codec, cmd,
			       codec->bus->sync_write ? &res : NULL);
Linus Torvalds's avatar
Linus Torvalds committed
297
}
298
EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds's avatar
Linus Torvalds committed
299 300 301 302 303 304 305 306 307 308 309 310 311 312

/**
 * snd_hda_sequence_write - sequence writes
 * @codec: the HDA codec
 * @seq: VERB array to send
 *
 * Send the commands sequentially from the given array.
 * The array must be terminated with NID=0.
 */
void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
{
	for (; seq->nid; seq++)
		snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
}
313
EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds's avatar
Linus Torvalds committed
314 315 316 317 318 319 320 321 322 323

/**
 * snd_hda_get_sub_nodes - get the range of sub nodes
 * @codec: the HDA codec
 * @nid: NID to parse
 * @start_id: the pointer to store the start NID
 *
 * Parse the NID and store the start NID of its sub-nodes.
 * Returns the number of sub-nodes.
 */
324 325
int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
			  hda_nid_t *start_id)
Linus Torvalds's avatar
Linus Torvalds committed
326 327 328 329
{
	unsigned int parm;

	parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
330 331
	if (parm == -1)
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
332 333 334
	*start_id = (parm >> 16) & 0x7fff;
	return (int)(parm & 0x7fff);
}
335
EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds's avatar
Linus Torvalds committed
336

337 338 339 340 341 342 343 344
/* connection list element */
struct hda_conn_list {
	struct list_head list;
	int len;
	hda_nid_t nid;
	hda_nid_t conns[0];
};

345
/* look up the cached results */
346 347
static struct hda_conn_list *
lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
348
{
349 350 351
	struct hda_conn_list *p;
	list_for_each_entry(p, &codec->conn_list, list) {
		if (p->nid == nid)
352 353 354 355
			return p;
	}
	return NULL;
}
356

357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
			 const hda_nid_t *list)
{
	struct hda_conn_list *p;

	p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
	if (!p)
		return -ENOMEM;
	p->len = len;
	p->nid = nid;
	memcpy(p->conns, list, len * sizeof(hda_nid_t));
	list_add(&p->list, &codec->conn_list);
	return 0;
}

static void remove_conn_list(struct hda_codec *codec)
{
	while (!list_empty(&codec->conn_list)) {
		struct hda_conn_list *p;
		p = list_first_entry(&codec->conn_list, typeof(*p), list);
		list_del(&p->list);
		kfree(p);
	}
}

382 383 384
/* read the connection and add to the cache */
static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
{
385 386
	hda_nid_t list[32];
	hda_nid_t *result = list;
387 388 389
	int len;

	len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
390 391 392 393 394 395 396 397 398 399 400 401
	if (len == -ENOSPC) {
		len = snd_hda_get_num_raw_conns(codec, nid);
		result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
		if (!result)
			return -ENOMEM;
		len = snd_hda_get_raw_connections(codec, nid, result, len);
	}
	if (len >= 0)
		len = snd_hda_override_conn_list(codec, nid, len, result);
	if (result != list)
		kfree(result);
	return len;
402 403
}

404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
/**
 * snd_hda_get_conn_list - get connection list
 * @codec: the HDA codec
 * @nid: NID to parse
 * @len: number of connection list entries
 * @listp: the pointer to store NID list
 *
 * Parses the connection list of the given widget and stores the pointer
 * to the list of NIDs.
 *
 * Returns the number of connections, or a negative error code.
 *
 * Note that the returned pointer isn't protected against the list
 * modification.  If snd_hda_override_conn_list() might be called
 * concurrently, protect with a mutex appropriately.
 */
int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
			  const hda_nid_t **listp)
{
	bool added = false;

	for (;;) {
		int err;
		const struct hda_conn_list *p;

		/* if the connection-list is already cached, read it */
		p = lookup_conn_list(codec, nid);
		if (p) {
			if (listp)
				*listp = p->conns;
			return p->len;
		}
		if (snd_BUG_ON(added))
			return -EINVAL;

		err = read_and_add_raw_conns(codec, nid);
		if (err < 0)
			return err;
		added = true;
	}
}
EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);

Linus Torvalds's avatar
Linus Torvalds committed
447
/**
448
 * snd_hda_get_connections - copy connection list
Linus Torvalds's avatar
Linus Torvalds committed
449 450
 * @codec: the HDA codec
 * @nid: NID to parse
451 452
 * @conn_list: connection list array; when NULL, checks only the size
 * @max_conns: max. number of connections to store
Linus Torvalds's avatar
Linus Torvalds committed
453 454 455 456 457 458
 *
 * Parses the connection list of the given widget and stores the list
 * of NIDs.
 *
 * Returns the number of connections, or a negative error code.
 */
459 460
int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
			    hda_nid_t *conn_list, int max_conns)
461
{
462 463
	const hda_nid_t *list;
	int len = snd_hda_get_conn_list(codec, nid, &list);
464

465 466
	if (len > 0 && conn_list) {
		if (len > max_conns) {
467 468 469 470 471
			snd_printk(KERN_ERR "hda_codec: "
				   "Too many connections %d for NID 0x%x\n",
				   len, nid);
			return -EINVAL;
		}
472
		memcpy(conn_list, list, len * sizeof(hda_nid_t));
473 474
	}

475
	return len;
476 477 478
}
EXPORT_SYMBOL_HDA(snd_hda_get_connections);

479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
/* return CONNLIST_LEN parameter of the given widget */
static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid)
{
	unsigned int wcaps = get_wcaps(codec, nid);
	unsigned int parm;

	if (!(wcaps & AC_WCAP_CONN_LIST) &&
	    get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
		return 0;

	parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
	if (parm == -1)
		parm = 0;
	return parm;
}

int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid)
{
497
	return snd_hda_get_raw_connections(codec, nid, NULL, 0);
498 499
}

500 501 502 503 504 505 506 507 508 509 510 511 512
/**
 * snd_hda_get_raw_connections - copy connection list without cache
 * @codec: the HDA codec
 * @nid: NID to parse
 * @conn_list: connection list array
 * @max_conns: max. number of connections to store
 *
 * Like snd_hda_get_connections(), copy the connection list but without
 * checking through the connection-list cache.
 * Currently called only from hda_proc.c, so not exported.
 */
int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
				hda_nid_t *conn_list, int max_conns)
Linus Torvalds's avatar
Linus Torvalds committed
513 514
{
	unsigned int parm;
515
	int i, conn_len, conns;
Linus Torvalds's avatar
Linus Torvalds committed
516
	unsigned int shift, num_elems, mask;
517
	hda_nid_t prev_nid;
518
	int null_count = 0;
Linus Torvalds's avatar
Linus Torvalds committed
519

520 521
	parm = get_num_conns(codec, nid);
	if (!parm)
522
		return 0;
523

Linus Torvalds's avatar
Linus Torvalds committed
524 525 526 527 528 529 530 531 532 533 534 535
	if (parm & AC_CLIST_LONG) {
		/* long form */
		shift = 16;
		num_elems = 2;
	} else {
		/* short form */
		shift = 8;
		num_elems = 4;
	}
	conn_len = parm & AC_CLIST_LENGTH;
	mask = (1 << (shift-1)) - 1;

536
	if (!conn_len)
Linus Torvalds's avatar
Linus Torvalds committed
537 538 539 540
		return 0; /* no connection */

	if (conn_len == 1) {
		/* single connection */
541 542
		parm = snd_hda_codec_read(codec, nid, 0,
					  AC_VERB_GET_CONNECT_LIST, 0);
543 544
		if (parm == -1 && codec->bus->rirb_error)
			return -EIO;
545 546
		if (conn_list)
			conn_list[0] = parm & mask;
Linus Torvalds's avatar
Linus Torvalds committed
547 548 549 550 551
		return 1;
	}

	/* multi connection */
	conns = 0;
552 553 554 555 556
	prev_nid = 0;
	for (i = 0; i < conn_len; i++) {
		int range_val;
		hda_nid_t val, n;

557
		if (i % num_elems == 0) {
558 559
			parm = snd_hda_codec_read(codec, nid, 0,
						  AC_VERB_GET_CONNECT_LIST, i);
560 561 562
			if (parm == -1 && codec->bus->rirb_error)
				return -EIO;
		}
563
		range_val = !!(parm & (1 << (shift-1))); /* ranges */
564
		val = parm & mask;
565
		if (val == 0 && null_count++) {  /* no second chance */
566 567 568 569 570
			snd_printk(KERN_WARNING "hda_codec: "
				   "invalid CONNECT_LIST verb %x[%i]:%x\n",
				    nid, i, parm);
			return 0;
		}
571 572 573
		parm >>= shift;
		if (range_val) {
			/* ranges between the previous and this one */
574 575 576 577
			if (!prev_nid || prev_nid >= val) {
				snd_printk(KERN_WARNING "hda_codec: "
					   "invalid dep_range_val %x:%x\n",
					   prev_nid, val);
578 579 580
				continue;
			}
			for (n = prev_nid + 1; n <= val; n++) {
581 582 583 584 585 586 587 588 589
				if (conn_list) {
					if (conns >= max_conns)
						return -ENOSPC;
					conn_list[conns] = n;
				}
				conns++;
			}
		} else {
			if (conn_list) {
590 591
				if (conns >= max_conns)
					return -ENOSPC;
592
				conn_list[conns] = val;
Linus Torvalds's avatar
Linus Torvalds committed
593
			}
594
			conns++;
Linus Torvalds's avatar
Linus Torvalds committed
595
		}
596
		prev_nid = val;
Linus Torvalds's avatar
Linus Torvalds committed
597 598 599 600
	}
	return conns;
}

601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
/**
 * snd_hda_override_conn_list - add/modify the connection-list to cache
 * @codec: the HDA codec
 * @nid: NID to parse
 * @len: number of connection list entries
 * @list: the list of connection entries
 *
 * Add or modify the given connection-list to the cache.  If the corresponding
 * cache already exists, invalidate it and append a new one.
 *
 * Returns zero or a negative error code.
 */
int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
			       const hda_nid_t *list)
{
616
	struct hda_conn_list *p;
617

618 619 620 621 622
	p = lookup_conn_list(codec, nid);
	if (p) {
		list_del(&p->list);
		kfree(p);
	}
623

624
	return add_conn_list(codec, nid, len, list);
625 626 627
}
EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);

628 629 630 631 632 633 634 635 636 637 638 639 640
/**
 * snd_hda_get_conn_index - get the connection index of the given NID
 * @codec: the HDA codec
 * @mux: NID containing the list
 * @nid: NID to select
 * @recursive: 1 when searching NID recursively, otherwise 0
 *
 * Parses the connection list of the widget @mux and checks whether the
 * widget @nid is present.  If it is, return the connection index.
 * Otherwise it returns -1.
 */
int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
			   hda_nid_t nid, int recursive)
641
{
642
	const hda_nid_t *conn;
643 644
	int i, nums;

645
	nums = snd_hda_get_conn_list(codec, mux, &conn);
646 647 648 649 650
	for (i = 0; i < nums; i++)
		if (conn[i] == nid)
			return i;
	if (!recursive)
		return -1;
651
	if (recursive > 10) {
652 653
		snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
		return -1;
654
	}
655
	recursive++;
656 657 658 659
	for (i = 0; i < nums; i++) {
		unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
		if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
			continue;
660 661
		if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
			return i;
662
	}
663
	return -1;
664
}
665
EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
Linus Torvalds's avatar
Linus Torvalds committed
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683

/**
 * snd_hda_queue_unsol_event - add an unsolicited event to queue
 * @bus: the BUS
 * @res: unsolicited event (lower 32bit of RIRB entry)
 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
 *
 * Adds the given event to the queue.  The events are processed in
 * the workqueue asynchronously.  Call this function in the interrupt
 * hanlder when RIRB receives an unsolicited event.
 *
 * Returns 0 if successful, or a negative error code.
 */
int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
{
	struct hda_bus_unsolicited *unsol;
	unsigned int wp;

684 685 686
	if (!bus || !bus->workq)
		return 0;

687
	trace_hda_unsol_event(bus, res, res_ex);
688 689
	unsol = bus->unsol;
	if (!unsol)
Linus Torvalds's avatar
Linus Torvalds committed
690 691 692 693 694 695 696 697 698
		return 0;

	wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
	unsol->wp = wp;

	wp <<= 1;
	unsol->queue[wp] = res;
	unsol->queue[wp + 1] = res_ex;

Takashi Iwai's avatar
Takashi Iwai committed
699
	queue_work(bus->workq, &unsol->work);
Linus Torvalds's avatar
Linus Torvalds committed
700 701 702

	return 0;
}
703
EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds's avatar
Linus Torvalds committed
704 705

/*
Wu Fengguang's avatar
Wu Fengguang committed
706
 * process queued unsolicited events
Linus Torvalds's avatar
Linus Torvalds committed
707
 */
David Howells's avatar
David Howells committed
708
static void process_unsol_events(struct work_struct *work)
Linus Torvalds's avatar
Linus Torvalds committed
709
{
David Howells's avatar
David Howells committed
710 711 712
	struct hda_bus_unsolicited *unsol =
		container_of(work, struct hda_bus_unsolicited, work);
	struct hda_bus *bus = unsol->bus;
Linus Torvalds's avatar
Linus Torvalds committed
713 714 715 716 717 718 719 720 721
	struct hda_codec *codec;
	unsigned int rp, caddr, res;

	while (unsol->rp != unsol->wp) {
		rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
		unsol->rp = rp;
		rp <<= 1;
		res = unsol->queue[rp];
		caddr = unsol->queue[rp + 1];
722
		if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds's avatar
Linus Torvalds committed
723 724 725 726 727 728 729 730 731 732
			continue;
		codec = bus->caddr_tbl[caddr & 0x0f];
		if (codec && codec->patch_ops.unsol_event)
			codec->patch_ops.unsol_event(codec, res);
	}
}

/*
 * initialize unsolicited queue
 */
733
static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds's avatar
Linus Torvalds committed
734 735 736
{
	struct hda_bus_unsolicited *unsol;

737 738 739
	if (bus->unsol) /* already initialized */
		return 0;

740
	unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
741 742 743
	if (!unsol) {
		snd_printk(KERN_ERR "hda_codec: "
			   "can't allocate unsolicited queue\n");
Linus Torvalds's avatar
Linus Torvalds committed
744 745
		return -ENOMEM;
	}
David Howells's avatar
David Howells committed
746 747
	INIT_WORK(&unsol->work, process_unsol_events);
	unsol->bus = bus;
Linus Torvalds's avatar
Linus Torvalds committed
748 749 750 751 752 753 754 755 756 757 758
	bus->unsol = unsol;
	return 0;
}

/*
 * destructor
 */
static void snd_hda_codec_free(struct hda_codec *codec);

static int snd_hda_bus_free(struct hda_bus *bus)
{
759
	struct hda_codec *codec, *n;
Linus Torvalds's avatar
Linus Torvalds committed
760

761
	if (!bus)
Linus Torvalds's avatar
Linus Torvalds committed
762
		return 0;
Takashi Iwai's avatar
Takashi Iwai committed
763 764 765
	if (bus->workq)
		flush_workqueue(bus->workq);
	if (bus->unsol)
Linus Torvalds's avatar
Linus Torvalds committed
766
		kfree(bus->unsol);
767
	list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds's avatar
Linus Torvalds committed
768 769 770 771
		snd_hda_codec_free(codec);
	}
	if (bus->ops.private_free)
		bus->ops.private_free(bus);
Takashi Iwai's avatar
Takashi Iwai committed
772 773
	if (bus->workq)
		destroy_workqueue(bus->workq);
Linus Torvalds's avatar
Linus Torvalds committed
774 775 776 777
	kfree(bus);
	return 0;
}

778
static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds's avatar
Linus Torvalds committed
779 780
{
	struct hda_bus *bus = device->device_data;
781
	bus->shutdown = 1;
Linus Torvalds's avatar
Linus Torvalds committed
782 783 784
	return snd_hda_bus_free(bus);
}

785 786 787 788 789 790 791
#ifdef CONFIG_SND_HDA_HWDEP
static int snd_hda_bus_dev_register(struct snd_device *device)
{
	struct hda_bus *bus = device->device_data;
	struct hda_codec *codec;
	list_for_each_entry(codec, &bus->codec_list, list) {
		snd_hda_hwdep_add_sysfs(codec);
792
		snd_hda_hwdep_add_power_sysfs(codec);
793 794 795 796 797 798 799
	}
	return 0;
}
#else
#define snd_hda_bus_dev_register	NULL
#endif

Linus Torvalds's avatar
Linus Torvalds committed
800 801 802 803 804 805 806 807
/**
 * snd_hda_bus_new - create a HDA bus
 * @card: the card entry
 * @temp: the template for hda_bus information
 * @busp: the pointer to store the created bus instance
 *
 * Returns 0 if successful, or a negative error code.
 */
808
int snd_hda_bus_new(struct snd_card *card,
809 810
			      const struct hda_bus_template *temp,
			      struct hda_bus **busp)
Linus Torvalds's avatar
Linus Torvalds committed
811 812 813
{
	struct hda_bus *bus;
	int err;
814
	static struct snd_device_ops dev_ops = {
815
		.dev_register = snd_hda_bus_dev_register,
Linus Torvalds's avatar
Linus Torvalds committed
816 817 818
		.dev_free = snd_hda_bus_dev_free,
	};

819 820 821 822
	if (snd_BUG_ON(!temp))
		return -EINVAL;
	if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
823 824 825 826

	if (busp)
		*busp = NULL;

827
	bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
828 829 830 831 832 833 834 835 836
	if (bus == NULL) {
		snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
		return -ENOMEM;
	}

	bus->card = card;
	bus->private_data = temp->private_data;
	bus->pci = temp->pci;
	bus->modelname = temp->modelname;
837
	bus->power_save = temp->power_save;
Linus Torvalds's avatar
Linus Torvalds committed
838 839
	bus->ops = temp->ops;

840
	mutex_init(&bus->cmd_mutex);
841
	mutex_init(&bus->prepare_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
842 843
	INIT_LIST_HEAD(&bus->codec_list);

844 845 846
	snprintf(bus->workq_name, sizeof(bus->workq_name),
		 "hd-audio%d", card->number);
	bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai's avatar
Takashi Iwai committed
847
	if (!bus->workq) {
848 849
		snd_printk(KERN_ERR "cannot create workqueue %s\n",
			   bus->workq_name);
Takashi Iwai's avatar
Takashi Iwai committed
850 851 852 853
		kfree(bus);
		return -ENOMEM;
	}

854 855
	err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
	if (err < 0) {
Linus Torvalds's avatar
Linus Torvalds committed
856 857 858 859 860 861 862
		snd_hda_bus_free(bus);
		return err;
	}
	if (busp)
		*busp = bus;
	return 0;
}
863
EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds's avatar
Linus Torvalds committed
864

865 866
#ifdef CONFIG_SND_HDA_GENERIC
#define is_generic_config(codec) \
867
	(codec->modelname && !strcmp(codec->modelname, "generic"))
868 869 870 871
#else
#define is_generic_config(codec)	0
#endif

872
#ifdef MODULE
873 874
#define HDA_MODREQ_MAX_COUNT	2	/* two request_modules()'s */
#else
875
#define HDA_MODREQ_MAX_COUNT	0	/* all presets are statically linked */
876 877
#endif

Linus Torvalds's avatar
Linus Torvalds committed
878 879 880
/*
 * find a matching codec preset
 */
881
static const struct hda_codec_preset *
882
find_codec_preset(struct hda_codec *codec)
Linus Torvalds's avatar
Linus Torvalds committed
883
{
884 885
	struct hda_codec_preset_list *tbl;
	const struct hda_codec_preset *preset;
886
	unsigned int mod_requested = 0;
Linus Torvalds's avatar
Linus Torvalds committed
887

888
	if (is_generic_config(codec))
889 890
		return NULL; /* use the generic parser */

891 892 893 894 895 896 897 898
 again:
	mutex_lock(&preset_mutex);
	list_for_each_entry(tbl, &hda_preset_tables, list) {
		if (!try_module_get(tbl->owner)) {
			snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
			continue;
		}
		for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds's avatar
Linus Torvalds committed
899
			u32 mask = preset->mask;
900 901 902 903
			if (preset->afg && preset->afg != codec->afg)
				continue;
			if (preset->mfg && preset->mfg != codec->mfg)
				continue;
904
			if (!mask)
Linus Torvalds's avatar
Linus Torvalds committed
905
				mask = ~0;
906
			if (preset->id == (codec->vendor_id & mask) &&
907
			    (!preset->rev ||
908 909 910
			     preset->rev == codec->revision_id)) {
				mutex_unlock(&preset_mutex);
				codec->owner = tbl->owner;
Linus Torvalds's avatar
Linus Torvalds committed
911
				return preset;
912
			}
Linus Torvalds's avatar
Linus Torvalds committed
913
		}
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
		module_put(tbl->owner);
	}
	mutex_unlock(&preset_mutex);

	if (mod_requested < HDA_MODREQ_MAX_COUNT) {
		char name[32];
		if (!mod_requested)
			snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
				 codec->vendor_id);
		else
			snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
				 (codec->vendor_id >> 16) & 0xffff);
		request_module(name);
		mod_requested++;
		goto again;
Linus Torvalds's avatar
Linus Torvalds committed
929 930 931 932 933
	}
	return NULL;
}

/*
934
 * get_codec_name - store the codec name
Linus Torvalds's avatar
Linus Torvalds committed
935
 */
936
static int get_codec_name(struct hda_codec *codec)
Linus Torvalds's avatar
Linus Torvalds committed
937 938 939 940
{
	const struct hda_vendor_id *c;
	const char *vendor = NULL;
	u16 vendor_id = codec->vendor_id >> 16;
941 942 943 944
	char tmp[16];

	if (codec->vendor_name)
		goto get_chip_name;
Linus Torvalds's avatar
Linus Torvalds committed
945 946 947 948 949 950 951

	for (c = hda_vendor_ids; c->id; c++) {
		if (c->id == vendor_id) {
			vendor = c->name;
			break;
		}
	}
952
	if (!vendor) {
Linus Torvalds's avatar
Linus Torvalds committed
953 954 955
		sprintf(tmp, "Generic %04x", vendor_id);
		vendor = tmp;
	}
956 957 958 959 960 961 962 963
	codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
	if (!codec->vendor_name)
		return -ENOMEM;

 get_chip_name:
	if (codec->chip_name)
		return 0;

Linus Torvalds's avatar
Linus Torvalds committed
964
	if (codec->preset && codec->preset->name)
965 966 967 968 969 970
		codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
	else {
		sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
		codec->chip_name = kstrdup(tmp, GFP_KERNEL);
	}
	if (!codec->chip_name)
971 972
		return -ENOMEM;
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
973 974 975
}

/*
Sasha Khapyorsky's avatar
Sasha Khapyorsky committed
976
 * look for an AFG and MFG nodes
Linus Torvalds's avatar
Linus Torvalds committed
977
 */
978
static void setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds's avatar
Linus Torvalds committed
979
{
980
	int i, total_nodes, function_id;
Linus Torvalds's avatar
Linus Torvalds committed
981 982 983 984
	hda_nid_t nid;

	total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
	for (i = 0; i < total_nodes; i++, nid++) {
985
		function_id = snd_hda_param_read(codec, nid,
986
						AC_PAR_FUNCTION_TYPE);
987
		switch (function_id & 0xff) {
Sasha Khapyorsky's avatar
Sasha Khapyorsky committed
988 989
		case AC_GRP_AUDIO_FUNCTION:
			codec->afg = nid;
990 991
			codec->afg_function_id = function_id & 0xff;
			codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky's avatar
Sasha Khapyorsky committed
992 993 994
			break;
		case AC_GRP_MODEM_FUNCTION:
			codec->mfg = nid;
995 996
			codec->mfg_function_id = function_id & 0xff;
			codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky's avatar
Sasha Khapyorsky committed
997 998 999 1000
			break;
		default:
			break;
		}
Linus Torvalds's avatar
Linus Torvalds committed
1001 1002 1003
	}
}

1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
/*
 * read widget caps for each widget and store in cache
 */
static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
{
	int i;
	hda_nid_t nid;

	codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
						 &codec->start_nid);
	codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
1015
	if (!codec->wcaps)
1016 1017 1018 1019 1020 1021 1022 1023
		return -ENOMEM;
	nid = codec->start_nid;
	for (i = 0; i < codec->num_nodes; i++, nid++)
		codec->wcaps[i] = snd_hda_param_read(codec, nid,
						     AC_PAR_AUDIO_WIDGET_CAP);
	return 0;
}

1024 1025 1026 1027 1028 1029 1030 1031 1032
/* read all pin default configurations and save codec->init_pins */
static int read_pin_defaults(struct hda_codec *codec)
{
	int i;
	hda_nid_t nid = codec->start_nid;

	for (i = 0; i < codec->num_nodes; i++, nid++) {
		struct hda_pincfg *pin;
		unsigned int wcaps = get_wcaps(codec, nid);
1033
		unsigned int wid_type = get_wcaps_type(wcaps);
1034 1035 1036 1037 1038 1039 1040 1041
		if (wid_type != AC_WID_PIN)
			continue;
		pin = snd_array_new(&codec->init_pins);
		if (!pin)
			return -ENOMEM;
		pin->nid = nid;
		pin->cfg = snd_hda_codec_read(codec, nid, 0,
					      AC_VERB_GET_CONFIG_DEFAULT, 0);
1042 1043 1044
		pin->ctrl = snd_hda_codec_read(codec, nid, 0,
					       AC_VERB_GET_PIN_WIDGET_CONTROL,
					       0);
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
	}
	return 0;
}

/* look up the given pin config list and return the item matching with NID */
static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
					 struct snd_array *array,
					 hda_nid_t nid)
{
	int i;
	for (i = 0; i < array->used; i++) {
		struct hda_pincfg *pin = snd_array_elem(array, i);
		if (pin->nid == nid)
			return pin;
	}
	return NULL;
}

/* set the current pin config value for the given NID.
 * the value is cached, and read via snd_hda_codec_get_pincfg()
 */
int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
		       hda_nid_t nid, unsigned int cfg)
{
	struct hda_pincfg *pin;

1071 1072 1073 1074 1075
	/* the check below may be invalid when pins are added by a fixup
	 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
	 * for now
	 */
	/*
1076 1077
	if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
		return -EINVAL;
1078
	*/
1079

1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
	pin = look_up_pincfg(codec, list, nid);
	if (!pin) {
		pin = snd_array_new(list);
		if (!pin)
			return -ENOMEM;
		pin->nid = nid;
	}
	pin->cfg = cfg;
	return 0;
}

1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
/**
 * snd_hda_codec_set_pincfg - Override a pin default configuration
 * @codec: the HDA codec
 * @nid: NID to set the pin config
 * @cfg: the pin default config value
 *
 * Override a pin default configuration value in the cache.
 * This value can be read by snd_hda_codec_get_pincfg() in a higher
 * priority than the real hardware value.
 */
1101 1102 1103
int snd_hda_codec_set_pincfg(struct hda_codec *codec,
			     hda_nid_t nid, unsigned int cfg)
{
1104
	return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
1105 1106 1107
}
EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);

1108 1109 1110 1111 1112 1113 1114 1115 1116
/**
 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
 * @codec: the HDA codec
 * @nid: NID to get the pin config
 *
 * Get the current pin config value of the given pin NID.
 * If the pincfg value is cached or overridden via sysfs or driver,
 * returns the cached value.
 */
1117 1118 1119 1120 1121
unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
{
	struct hda_pincfg *pin;

#ifdef CONFIG_SND_HDA_HWDEP
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
	{
		unsigned int cfg = 0;
		mutex_lock(&codec->user_mutex);
		pin = look_up_pincfg(codec, &codec->user_pins, nid);
		if (pin)
			cfg = pin->cfg;
		mutex_unlock(&codec->user_mutex);
		if (cfg)
			return cfg;
	}
1132
#endif
1133 1134 1135
	pin = look_up_pincfg(codec, &codec->driver_pins, nid);
	if (pin)
		return pin->cfg;
1136 1137 1138 1139 1140 1141 1142
	pin = look_up_pincfg(codec, &codec->init_pins, nid);
	if (pin)
		return pin->cfg;
	return 0;
}
EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);

1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
/* remember the current pinctl target value */
int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
				 unsigned int val)
{
	struct hda_pincfg *pin;

	pin = look_up_pincfg(codec, &codec->init_pins, nid);
	if (!pin)
		return -EINVAL;
	pin->target = val;
	return 0;
}
EXPORT_SYMBOL_HDA(snd_hda_codec_set_pin_target);

/* return the current pinctl target value */
int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
{
	struct hda_pincfg *pin;

	pin = look_up_pincfg(codec, &codec->init_pins, nid);
	if (!pin)
		return 0;
	return pin->target;
}
EXPORT_SYMBOL_HDA(snd_hda_codec_get_pin_target);

1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
/**
 * snd_hda_shutup_pins - Shut up all pins
 * @codec: the HDA codec
 *
 * Clear all pin controls to shup up before suspend for avoiding click noise.
 * The controls aren't cached so that they can be resumed properly.
 */
void snd_hda_shutup_pins(struct hda_codec *codec)
{
	int i;
1179 1180 1181 1182 1183
	/* don't shut up pins when unloading the driver; otherwise it breaks
	 * the default pin setup at the next load of the driver
	 */
	if (codec->bus->shutdown)
		return;
1184 1185 1186 1187 1188 1189
	for (i = 0; i < codec->init_pins.used; i++) {
		struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
		/* use read here for syncing after issuing each verb */
		snd_hda_codec_read(codec, pin->nid, 0,
				   AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
	}
1190
	codec->pins_shutup = 1;
1191 1192 1193
}
EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);

1194
#ifdef CONFIG_PM
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
static void restore_shutup_pins(struct hda_codec *codec)
{
	int i;
	if (!codec->pins_shutup)
		return;
	if (codec->bus->shutdown)
		return;
	for (i = 0; i < codec->init_pins.used; i++) {
		struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
		snd_hda_codec_write(codec, pin->nid, 0,
				    AC_VERB_SET_PIN_WIDGET_CONTROL,
				    pin->ctrl);
	}
	codec->pins_shutup = 0;
}
1211
#endif
1212

1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
static void hda_jackpoll_work(struct work_struct *work)
{
	struct hda_codec *codec =
		container_of(work, struct hda_codec, jackpoll_work.work);
	if (!codec->jackpoll_interval)
		return;

	snd_hda_jack_set_dirty_all(codec);
	snd_hda_jack_poll_all(codec);
	queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
			   codec->jackpoll_interval);
}

1226 1227
static void init_hda_cache(struct hda_cache_rec *cache,
			   unsigned int record_size);
1228
static void free_hda_cache(struct hda_cache_rec *cache);
1229

1230 1231
/* release all pincfg lists */
static void free_init_pincfgs(struct hda_codec *codec)
1232
{
1233
	snd_array_free(&codec->driver_pins);
1234
#ifdef CONFIG_SND_HDA_HWDEP
1235
	snd_array_free(&codec->user_pins);
1236 1237 1238 1239
#endif