ALSA: hda - Add snd_hda_override_conn_list() helper function
[deliverable/linux.git] / sound / pci / hda / hda_codec.c
CommitLineData
1da177e4
LT
1/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
5 *
6 *
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
1da177e4
LT
22#include <linux/init.h>
23#include <linux/delay.h>
24#include <linux/slab.h>
25#include <linux/pci.h>
62932df8 26#include <linux/mutex.h>
1da177e4
LT
27#include <sound/core.h>
28#include "hda_codec.h"
29#include <sound/asoundef.h>
302e9c5a 30#include <sound/tlv.h>
1da177e4 31#include <sound/initval.h>
cd372fb3 32#include <sound/jack.h>
1da177e4 33#include "hda_local.h"
123c07ae 34#include "hda_beep.h"
2807314d 35#include <sound/hda_hwdep.h>
1da177e4 36
1da177e4
LT
37/*
38 * vendor / preset table
39 */
40
41struct hda_vendor_id {
42 unsigned int id;
43 const char *name;
44};
45
46/* codec vendor labels */
47static struct hda_vendor_id hda_vendor_ids[] = {
c8cd1281 48 { 0x1002, "ATI" },
e5f14248 49 { 0x1013, "Cirrus Logic" },
a9226251 50 { 0x1057, "Motorola" },
c8cd1281 51 { 0x1095, "Silicon Image" },
31117b78 52 { 0x10de, "Nvidia" },
c8cd1281 53 { 0x10ec, "Realtek" },
4e01f54b 54 { 0x1102, "Creative" },
c577b8a1 55 { 0x1106, "VIA" },
7f16859a 56 { 0x111d, "IDT" },
c8cd1281 57 { 0x11c1, "LSI" },
54b903ec 58 { 0x11d4, "Analog Devices" },
1da177e4 59 { 0x13f6, "C-Media" },
a9226251 60 { 0x14f1, "Conexant" },
c8cd1281
TI
61 { 0x17e8, "Chrontel" },
62 { 0x1854, "LG" },
8199de3b 63 { 0x1aec, "Wolfson Microelectronics" },
1da177e4 64 { 0x434d, "C-Media" },
74c61133 65 { 0x8086, "Intel" },
2f2f4251 66 { 0x8384, "SigmaTel" },
1da177e4
LT
67 {} /* terminator */
68};
69
1289e9e8
TI
70static DEFINE_MUTEX(preset_mutex);
71static LIST_HEAD(hda_preset_tables);
72
73int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
74{
75 mutex_lock(&preset_mutex);
76 list_add_tail(&preset->list, &hda_preset_tables);
77 mutex_unlock(&preset_mutex);
78 return 0;
79}
ff7a3267 80EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
1289e9e8
TI
81
82int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
83{
84 mutex_lock(&preset_mutex);
85 list_del(&preset->list);
86 mutex_unlock(&preset_mutex);
87 return 0;
88}
ff7a3267 89EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
1da177e4 90
cb53c626
TI
91#ifdef CONFIG_SND_HDA_POWER_SAVE
92static void hda_power_work(struct work_struct *work);
93static void hda_keep_power_on(struct hda_codec *codec);
94#else
95static inline void hda_keep_power_on(struct hda_codec *codec) {}
96#endif
97
d5191e50
TI
98/**
99 * snd_hda_get_jack_location - Give a location string of the jack
100 * @cfg: pin default config value
101 *
102 * Parse the pin default config value and returns the string of the
103 * jack location, e.g. "Rear", "Front", etc.
104 */
50a9f790
MR
105const char *snd_hda_get_jack_location(u32 cfg)
106{
107 static char *bases[7] = {
108 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
109 };
110 static unsigned char specials_idx[] = {
111 0x07, 0x08,
112 0x17, 0x18, 0x19,
113 0x37, 0x38
114 };
115 static char *specials[] = {
116 "Rear Panel", "Drive Bar",
117 "Riser", "HDMI", "ATAPI",
118 "Mobile-In", "Mobile-Out"
119 };
120 int i;
121 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
122 if ((cfg & 0x0f) < 7)
123 return bases[cfg & 0x0f];
124 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
125 if (cfg == specials_idx[i])
126 return specials[i];
127 }
128 return "UNKNOWN";
129}
ff7a3267 130EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
50a9f790 131
d5191e50
TI
132/**
133 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
134 * @cfg: pin default config value
135 *
136 * Parse the pin default config value and returns the string of the
137 * jack connectivity, i.e. external or internal connection.
138 */
50a9f790
MR
139const char *snd_hda_get_jack_connectivity(u32 cfg)
140{
141 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
142
143 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
144}
ff7a3267 145EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
50a9f790 146
d5191e50
TI
147/**
148 * snd_hda_get_jack_type - Give a type string of the jack
149 * @cfg: pin default config value
150 *
151 * Parse the pin default config value and returns the string of the
152 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
153 */
50a9f790
MR
154const char *snd_hda_get_jack_type(u32 cfg)
155{
156 static char *jack_types[16] = {
157 "Line Out", "Speaker", "HP Out", "CD",
158 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
159 "Line In", "Aux", "Mic", "Telephony",
160 "SPDIF In", "Digitial In", "Reserved", "Other"
161 };
162
163 return jack_types[(cfg & AC_DEFCFG_DEVICE)
164 >> AC_DEFCFG_DEVICE_SHIFT];
165}
ff7a3267 166EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
50a9f790 167
33fa35ed
TI
168/*
169 * Compose a 32bit command word to be sent to the HD-audio controller
170 */
171static inline unsigned int
172make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
173 unsigned int verb, unsigned int parm)
174{
175 u32 val;
176
82e1b804
TI
177 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
178 (verb & ~0xfff) || (parm & ~0xffff)) {
6430aeeb
WF
179 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
180 codec->addr, direct, nid, verb, parm);
181 return ~0;
182 }
183
184 val = (u32)codec->addr << 28;
33fa35ed
TI
185 val |= (u32)direct << 27;
186 val |= (u32)nid << 20;
187 val |= verb << 8;
188 val |= parm;
189 return val;
190}
191
aa2936f5
TI
192/*
193 * Send and receive a verb
194 */
195static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
196 unsigned int *res)
197{
198 struct hda_bus *bus = codec->bus;
8dd78330 199 int err;
aa2936f5 200
6430aeeb
WF
201 if (cmd == ~0)
202 return -1;
203
aa2936f5
TI
204 if (res)
205 *res = -1;
8dd78330 206 again:
aa2936f5
TI
207 snd_hda_power_up(codec);
208 mutex_lock(&bus->cmd_mutex);
aa2936f5 209 err = bus->ops.command(bus, cmd);
8dd78330 210 if (!err && res)
deadff16 211 *res = bus->ops.get_response(bus, codec->addr);
aa2936f5
TI
212 mutex_unlock(&bus->cmd_mutex);
213 snd_hda_power_down(codec);
8dd78330
TI
214 if (res && *res == -1 && bus->rirb_error) {
215 if (bus->response_reset) {
216 snd_printd("hda_codec: resetting BUS due to "
217 "fatal communication error\n");
218 bus->ops.bus_reset(bus);
219 }
220 goto again;
221 }
222 /* clear reset-flag when the communication gets recovered */
223 if (!err)
224 bus->response_reset = 0;
aa2936f5
TI
225 return err;
226}
227
1da177e4
LT
228/**
229 * snd_hda_codec_read - send a command and get the response
230 * @codec: the HDA codec
231 * @nid: NID to send the command
232 * @direct: direct flag
233 * @verb: the verb to send
234 * @parm: the parameter for the verb
235 *
236 * Send a single command and read the corresponding response.
237 *
238 * Returns the obtained response value, or -1 for an error.
239 */
0ba21762
TI
240unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
241 int direct,
1da177e4
LT
242 unsigned int verb, unsigned int parm)
243{
aa2936f5
TI
244 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
245 unsigned int res;
9857edfd
GT
246 if (codec_exec_verb(codec, cmd, &res))
247 return -1;
1da177e4
LT
248 return res;
249}
ff7a3267 250EXPORT_SYMBOL_HDA(snd_hda_codec_read);
1da177e4
LT
251
252/**
253 * snd_hda_codec_write - send a single command without waiting for response
254 * @codec: the HDA codec
255 * @nid: NID to send the command
256 * @direct: direct flag
257 * @verb: the verb to send
258 * @parm: the parameter for the verb
259 *
260 * Send a single command without waiting for response.
261 *
262 * Returns 0 if successful, or a negative error code.
263 */
264int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
265 unsigned int verb, unsigned int parm)
266{
aa2936f5 267 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
33fa35ed 268 unsigned int res;
b20f3b83
TI
269 return codec_exec_verb(codec, cmd,
270 codec->bus->sync_write ? &res : NULL);
1da177e4 271}
ff7a3267 272EXPORT_SYMBOL_HDA(snd_hda_codec_write);
1da177e4
LT
273
274/**
275 * snd_hda_sequence_write - sequence writes
276 * @codec: the HDA codec
277 * @seq: VERB array to send
278 *
279 * Send the commands sequentially from the given array.
280 * The array must be terminated with NID=0.
281 */
282void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
283{
284 for (; seq->nid; seq++)
285 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
286}
ff7a3267 287EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
1da177e4
LT
288
289/**
290 * snd_hda_get_sub_nodes - get the range of sub nodes
291 * @codec: the HDA codec
292 * @nid: NID to parse
293 * @start_id: the pointer to store the start NID
294 *
295 * Parse the NID and store the start NID of its sub-nodes.
296 * Returns the number of sub-nodes.
297 */
0ba21762
TI
298int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
299 hda_nid_t *start_id)
1da177e4
LT
300{
301 unsigned int parm;
302
303 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
e8a7f136
DT
304 if (parm == -1)
305 return 0;
1da177e4
LT
306 *start_id = (parm >> 16) & 0x7fff;
307 return (int)(parm & 0x7fff);
308}
ff7a3267 309EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
1da177e4 310
a12d3e1e
TI
311static int _hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
312 hda_nid_t *conn_list, int max_conns);
b2f934a0
TI
313
314/* look up the cached results */
315static hda_nid_t *lookup_conn_list(struct snd_array *array, hda_nid_t nid)
316{
317 int i, len;
318 for (i = 0; i < array->used; ) {
319 hda_nid_t *p = snd_array_elem(array, i);
320 if (nid == *p)
321 return p;
322 len = p[1];
323 i += len + 2;
324 }
325 return NULL;
326}
a12d3e1e 327
1da177e4 328/**
b2f934a0 329 * snd_hda_get_conn_list - get connection list
1da177e4
LT
330 * @codec: the HDA codec
331 * @nid: NID to parse
dce2079b 332 * @listp: the pointer to store NID list
1da177e4
LT
333 *
334 * Parses the connection list of the given widget and stores the list
335 * of NIDs.
336 *
337 * Returns the number of connections, or a negative error code.
338 */
dce2079b
TI
339int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
340 const hda_nid_t **listp)
a12d3e1e
TI
341{
342 struct snd_array *array = &codec->conn_lists;
b2f934a0 343 int len, err;
a12d3e1e 344 hda_nid_t list[HDA_MAX_CONNECTIONS];
dce2079b 345 hda_nid_t *p;
b2f934a0 346 bool added = false;
a12d3e1e 347
b2f934a0
TI
348 again:
349 /* if the connection-list is already cached, read it */
350 p = lookup_conn_list(array, nid);
351 if (p) {
352 if (listp)
353 *listp = p + 2;
354 return p[1];
a12d3e1e 355 }
b2f934a0
TI
356 if (snd_BUG_ON(added))
357 return -EINVAL;
a12d3e1e 358
b2f934a0 359 /* read the connection and add to the cache */
a12d3e1e
TI
360 len = _hda_get_connections(codec, nid, list, HDA_MAX_CONNECTIONS);
361 if (len < 0)
362 return len;
b2f934a0
TI
363 err = snd_hda_override_conn_list(codec, nid, len, list);
364 if (err < 0)
365 return err;
366 added = true;
367 goto again;
a12d3e1e 368}
dce2079b
TI
369EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
370
371/**
372 * snd_hda_get_connections - copy connection list
373 * @codec: the HDA codec
374 * @nid: NID to parse
375 * @conn_list: connection list array
376 * @max_conns: max. number of connections to store
377 *
378 * Parses the connection list of the given widget and stores the list
379 * of NIDs.
380 *
381 * Returns the number of connections, or a negative error code.
382 */
383int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
384 hda_nid_t *conn_list, int max_conns)
385{
386 const hda_nid_t *list;
387 int len = snd_hda_get_conn_list(codec, nid, &list);
388
389 if (len <= 0)
390 return len;
391 if (len > max_conns) {
392 snd_printk(KERN_ERR "hda_codec: "
393 "Too many connections %d for NID 0x%x\n",
394 len, nid);
395 return -EINVAL;
396 }
397 memcpy(conn_list, list, len * sizeof(hda_nid_t));
398 return len;
399}
a12d3e1e
TI
400EXPORT_SYMBOL_HDA(snd_hda_get_connections);
401
402static int _hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
403 hda_nid_t *conn_list, int max_conns)
1da177e4
LT
404{
405 unsigned int parm;
54d17403 406 int i, conn_len, conns;
1da177e4 407 unsigned int shift, num_elems, mask;
1ba7a7c6 408 unsigned int wcaps;
54d17403 409 hda_nid_t prev_nid;
1da177e4 410
da3cec35
TI
411 if (snd_BUG_ON(!conn_list || max_conns <= 0))
412 return -EINVAL;
1da177e4 413
1ba7a7c6
TI
414 wcaps = get_wcaps(codec, nid);
415 if (!(wcaps & AC_WCAP_CONN_LIST) &&
8d087c76
TI
416 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
417 return 0;
16a433d8 418
1da177e4
LT
419 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
420 if (parm & AC_CLIST_LONG) {
421 /* long form */
422 shift = 16;
423 num_elems = 2;
424 } else {
425 /* short form */
426 shift = 8;
427 num_elems = 4;
428 }
429 conn_len = parm & AC_CLIST_LENGTH;
1da177e4
LT
430 mask = (1 << (shift-1)) - 1;
431
0ba21762 432 if (!conn_len)
1da177e4
LT
433 return 0; /* no connection */
434
435 if (conn_len == 1) {
436 /* single connection */
0ba21762
TI
437 parm = snd_hda_codec_read(codec, nid, 0,
438 AC_VERB_GET_CONNECT_LIST, 0);
3c6aae44
TI
439 if (parm == -1 && codec->bus->rirb_error)
440 return -EIO;
1da177e4
LT
441 conn_list[0] = parm & mask;
442 return 1;
443 }
444
445 /* multi connection */
446 conns = 0;
54d17403
TI
447 prev_nid = 0;
448 for (i = 0; i < conn_len; i++) {
449 int range_val;
450 hda_nid_t val, n;
451
3c6aae44 452 if (i % num_elems == 0) {
54d17403
TI
453 parm = snd_hda_codec_read(codec, nid, 0,
454 AC_VERB_GET_CONNECT_LIST, i);
3c6aae44
TI
455 if (parm == -1 && codec->bus->rirb_error)
456 return -EIO;
457 }
0ba21762 458 range_val = !!(parm & (1 << (shift-1))); /* ranges */
54d17403 459 val = parm & mask;
2e9bf247
JK
460 if (val == 0) {
461 snd_printk(KERN_WARNING "hda_codec: "
462 "invalid CONNECT_LIST verb %x[%i]:%x\n",
463 nid, i, parm);
464 return 0;
465 }
54d17403
TI
466 parm >>= shift;
467 if (range_val) {
468 /* ranges between the previous and this one */
0ba21762
TI
469 if (!prev_nid || prev_nid >= val) {
470 snd_printk(KERN_WARNING "hda_codec: "
471 "invalid dep_range_val %x:%x\n",
472 prev_nid, val);
54d17403
TI
473 continue;
474 }
475 for (n = prev_nid + 1; n <= val; n++) {
476 if (conns >= max_conns) {
5aacc218
TI
477 snd_printk(KERN_ERR "hda_codec: "
478 "Too many connections %d for NID 0x%x\n",
479 conns, nid);
1da177e4 480 return -EINVAL;
54d17403
TI
481 }
482 conn_list[conns++] = n;
1da177e4 483 }
54d17403
TI
484 } else {
485 if (conns >= max_conns) {
5aacc218
TI
486 snd_printk(KERN_ERR "hda_codec: "
487 "Too many connections %d for NID 0x%x\n",
488 conns, nid);
54d17403
TI
489 return -EINVAL;
490 }
491 conn_list[conns++] = val;
1da177e4 492 }
54d17403 493 prev_nid = val;
1da177e4
LT
494 }
495 return conns;
496}
497
a12d3e1e
TI
498static bool add_conn_list(struct snd_array *array, hda_nid_t nid)
499{
500 hda_nid_t *p = snd_array_new(array);
501 if (!p)
502 return false;
503 *p = nid;
504 return true;
505}
506
b2f934a0
TI
507/**
508 * snd_hda_override_conn_list - add/modify the connection-list to cache
509 * @codec: the HDA codec
510 * @nid: NID to parse
511 * @len: number of connection list entries
512 * @list: the list of connection entries
513 *
514 * Add or modify the given connection-list to the cache. If the corresponding
515 * cache already exists, invalidate it and append a new one.
516 *
517 * Returns zero or a negative error code.
518 */
519int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
520 const hda_nid_t *list)
521{
522 struct snd_array *array = &codec->conn_lists;
523 hda_nid_t *p;
524 int i, old_used;
525
526 p = lookup_conn_list(array, nid);
527 if (p)
528 *p = -1; /* invalidate the old entry */
529
530 old_used = array->used;
531 if (!add_conn_list(array, nid) || !add_conn_list(array, len))
532 goto error_add;
533 for (i = 0; i < len; i++)
534 if (!add_conn_list(array, list[i]))
535 goto error_add;
536 return 0;
537
538 error_add:
539 array->used = old_used;
540 return -ENOMEM;
541}
542EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
543
8d087c76
TI
544/**
545 * snd_hda_get_conn_index - get the connection index of the given NID
546 * @codec: the HDA codec
547 * @mux: NID containing the list
548 * @nid: NID to select
549 * @recursive: 1 when searching NID recursively, otherwise 0
550 *
551 * Parses the connection list of the widget @mux and checks whether the
552 * widget @nid is present. If it is, return the connection index.
553 * Otherwise it returns -1.
554 */
555int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
556 hda_nid_t nid, int recursive)
557{
558 hda_nid_t conn[HDA_MAX_NUM_INPUTS];
559 int i, nums;
560
561 nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn));
562 for (i = 0; i < nums; i++)
563 if (conn[i] == nid)
564 return i;
565 if (!recursive)
566 return -1;
567 if (recursive > 5) {
568 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
569 return -1;
570 }
571 recursive++;
572 for (i = 0; i < nums; i++)
573 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
574 return i;
575 return -1;
576}
577EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
578
1da177e4
LT
579/**
580 * snd_hda_queue_unsol_event - add an unsolicited event to queue
581 * @bus: the BUS
582 * @res: unsolicited event (lower 32bit of RIRB entry)
583 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
584 *
585 * Adds the given event to the queue. The events are processed in
586 * the workqueue asynchronously. Call this function in the interrupt
587 * hanlder when RIRB receives an unsolicited event.
588 *
589 * Returns 0 if successful, or a negative error code.
590 */
591int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
592{
593 struct hda_bus_unsolicited *unsol;
594 unsigned int wp;
595
0ba21762
TI
596 unsol = bus->unsol;
597 if (!unsol)
1da177e4
LT
598 return 0;
599
600 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
601 unsol->wp = wp;
602
603 wp <<= 1;
604 unsol->queue[wp] = res;
605 unsol->queue[wp + 1] = res_ex;
606
6acaed38 607 queue_work(bus->workq, &unsol->work);
1da177e4
LT
608
609 return 0;
610}
ff7a3267 611EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
1da177e4
LT
612
613/*
5c1d1a98 614 * process queued unsolicited events
1da177e4 615 */
c4028958 616static void process_unsol_events(struct work_struct *work)
1da177e4 617{
c4028958
DH
618 struct hda_bus_unsolicited *unsol =
619 container_of(work, struct hda_bus_unsolicited, work);
620 struct hda_bus *bus = unsol->bus;
1da177e4
LT
621 struct hda_codec *codec;
622 unsigned int rp, caddr, res;
623
624 while (unsol->rp != unsol->wp) {
625 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
626 unsol->rp = rp;
627 rp <<= 1;
628 res = unsol->queue[rp];
629 caddr = unsol->queue[rp + 1];
0ba21762 630 if (!(caddr & (1 << 4))) /* no unsolicited event? */
1da177e4
LT
631 continue;
632 codec = bus->caddr_tbl[caddr & 0x0f];
633 if (codec && codec->patch_ops.unsol_event)
634 codec->patch_ops.unsol_event(codec, res);
635 }
636}
637
638/*
639 * initialize unsolicited queue
640 */
6c1f45ea 641static int init_unsol_queue(struct hda_bus *bus)
1da177e4
LT
642{
643 struct hda_bus_unsolicited *unsol;
644
9f146bb6
TI
645 if (bus->unsol) /* already initialized */
646 return 0;
647
e560d8d8 648 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
0ba21762
TI
649 if (!unsol) {
650 snd_printk(KERN_ERR "hda_codec: "
651 "can't allocate unsolicited queue\n");
1da177e4
LT
652 return -ENOMEM;
653 }
c4028958
DH
654 INIT_WORK(&unsol->work, process_unsol_events);
655 unsol->bus = bus;
1da177e4
LT
656 bus->unsol = unsol;
657 return 0;
658}
659
660/*
661 * destructor
662 */
663static void snd_hda_codec_free(struct hda_codec *codec);
664
665static int snd_hda_bus_free(struct hda_bus *bus)
666{
0ba21762 667 struct hda_codec *codec, *n;
1da177e4 668
0ba21762 669 if (!bus)
1da177e4 670 return 0;
6acaed38
TI
671 if (bus->workq)
672 flush_workqueue(bus->workq);
673 if (bus->unsol)
1da177e4 674 kfree(bus->unsol);
0ba21762 675 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
1da177e4
LT
676 snd_hda_codec_free(codec);
677 }
678 if (bus->ops.private_free)
679 bus->ops.private_free(bus);
6acaed38
TI
680 if (bus->workq)
681 destroy_workqueue(bus->workq);
1da177e4
LT
682 kfree(bus);
683 return 0;
684}
685
c8b6bf9b 686static int snd_hda_bus_dev_free(struct snd_device *device)
1da177e4
LT
687{
688 struct hda_bus *bus = device->device_data;
b94d3539 689 bus->shutdown = 1;
1da177e4
LT
690 return snd_hda_bus_free(bus);
691}
692
d7ffba19
TI
693#ifdef CONFIG_SND_HDA_HWDEP
694static int snd_hda_bus_dev_register(struct snd_device *device)
695{
696 struct hda_bus *bus = device->device_data;
697 struct hda_codec *codec;
698 list_for_each_entry(codec, &bus->codec_list, list) {
699 snd_hda_hwdep_add_sysfs(codec);
a2f6309e 700 snd_hda_hwdep_add_power_sysfs(codec);
d7ffba19
TI
701 }
702 return 0;
703}
704#else
705#define snd_hda_bus_dev_register NULL
706#endif
707
1da177e4
LT
708/**
709 * snd_hda_bus_new - create a HDA bus
710 * @card: the card entry
711 * @temp: the template for hda_bus information
712 * @busp: the pointer to store the created bus instance
713 *
714 * Returns 0 if successful, or a negative error code.
715 */
1289e9e8 716int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
756e2b01
TI
717 const struct hda_bus_template *temp,
718 struct hda_bus **busp)
1da177e4
LT
719{
720 struct hda_bus *bus;
721 int err;
c8b6bf9b 722 static struct snd_device_ops dev_ops = {
d7ffba19 723 .dev_register = snd_hda_bus_dev_register,
1da177e4
LT
724 .dev_free = snd_hda_bus_dev_free,
725 };
726
da3cec35
TI
727 if (snd_BUG_ON(!temp))
728 return -EINVAL;
729 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
730 return -EINVAL;
1da177e4
LT
731
732 if (busp)
733 *busp = NULL;
734
e560d8d8 735 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
1da177e4
LT
736 if (bus == NULL) {
737 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
738 return -ENOMEM;
739 }
740
741 bus->card = card;
742 bus->private_data = temp->private_data;
743 bus->pci = temp->pci;
744 bus->modelname = temp->modelname;
fee2fba3 745 bus->power_save = temp->power_save;
1da177e4
LT
746 bus->ops = temp->ops;
747
62932df8 748 mutex_init(&bus->cmd_mutex);
3f50ac6a 749 mutex_init(&bus->prepare_mutex);
1da177e4
LT
750 INIT_LIST_HEAD(&bus->codec_list);
751
e8c0ee5d
TI
752 snprintf(bus->workq_name, sizeof(bus->workq_name),
753 "hd-audio%d", card->number);
754 bus->workq = create_singlethread_workqueue(bus->workq_name);
6acaed38 755 if (!bus->workq) {
e8c0ee5d
TI
756 snd_printk(KERN_ERR "cannot create workqueue %s\n",
757 bus->workq_name);
6acaed38
TI
758 kfree(bus);
759 return -ENOMEM;
760 }
761
0ba21762
TI
762 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
763 if (err < 0) {
1da177e4
LT
764 snd_hda_bus_free(bus);
765 return err;
766 }
767 if (busp)
768 *busp = bus;
769 return 0;
770}
ff7a3267 771EXPORT_SYMBOL_HDA(snd_hda_bus_new);
1da177e4 772
82467611
TI
773#ifdef CONFIG_SND_HDA_GENERIC
774#define is_generic_config(codec) \
f44ac837 775 (codec->modelname && !strcmp(codec->modelname, "generic"))
82467611
TI
776#else
777#define is_generic_config(codec) 0
778#endif
779
645f10c1 780#ifdef MODULE
1289e9e8
TI
781#define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
782#else
645f10c1 783#define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
1289e9e8
TI
784#endif
785
1da177e4
LT
786/*
787 * find a matching codec preset
788 */
6c1f45ea 789static const struct hda_codec_preset *
756e2b01 790find_codec_preset(struct hda_codec *codec)
1da177e4 791{
1289e9e8
TI
792 struct hda_codec_preset_list *tbl;
793 const struct hda_codec_preset *preset;
794 int mod_requested = 0;
1da177e4 795
82467611 796 if (is_generic_config(codec))
d5ad630b
TI
797 return NULL; /* use the generic parser */
798
1289e9e8
TI
799 again:
800 mutex_lock(&preset_mutex);
801 list_for_each_entry(tbl, &hda_preset_tables, list) {
802 if (!try_module_get(tbl->owner)) {
803 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
804 continue;
805 }
806 for (preset = tbl->preset; preset->id; preset++) {
1da177e4 807 u32 mask = preset->mask;
ca7cfae9
MB
808 if (preset->afg && preset->afg != codec->afg)
809 continue;
810 if (preset->mfg && preset->mfg != codec->mfg)
811 continue;
0ba21762 812 if (!mask)
1da177e4 813 mask = ~0;
9c7f852e 814 if (preset->id == (codec->vendor_id & mask) &&
0ba21762 815 (!preset->rev ||
1289e9e8
TI
816 preset->rev == codec->revision_id)) {
817 mutex_unlock(&preset_mutex);
818 codec->owner = tbl->owner;
1da177e4 819 return preset;
1289e9e8 820 }
1da177e4 821 }
1289e9e8
TI
822 module_put(tbl->owner);
823 }
824 mutex_unlock(&preset_mutex);
825
826 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
827 char name[32];
828 if (!mod_requested)
829 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
830 codec->vendor_id);
831 else
832 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
833 (codec->vendor_id >> 16) & 0xffff);
834 request_module(name);
835 mod_requested++;
836 goto again;
1da177e4
LT
837 }
838 return NULL;
839}
840
841/*
f44ac837 842 * get_codec_name - store the codec name
1da177e4 843 */
f44ac837 844static int get_codec_name(struct hda_codec *codec)
1da177e4
LT
845{
846 const struct hda_vendor_id *c;
847 const char *vendor = NULL;
848 u16 vendor_id = codec->vendor_id >> 16;
812a2cca
TI
849 char tmp[16];
850
851 if (codec->vendor_name)
852 goto get_chip_name;
1da177e4
LT
853
854 for (c = hda_vendor_ids; c->id; c++) {
855 if (c->id == vendor_id) {
856 vendor = c->name;
857 break;
858 }
859 }
0ba21762 860 if (!vendor) {
1da177e4
LT
861 sprintf(tmp, "Generic %04x", vendor_id);
862 vendor = tmp;
863 }
812a2cca
TI
864 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
865 if (!codec->vendor_name)
866 return -ENOMEM;
867
868 get_chip_name:
869 if (codec->chip_name)
870 return 0;
871
1da177e4 872 if (codec->preset && codec->preset->name)
812a2cca
TI
873 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
874 else {
875 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
876 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
877 }
878 if (!codec->chip_name)
f44ac837
TI
879 return -ENOMEM;
880 return 0;
1da177e4
LT
881}
882
883/*
673b683a 884 * look for an AFG and MFG nodes
1da177e4 885 */
1289e9e8 886static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
1da177e4 887{
93e82ae7 888 int i, total_nodes, function_id;
1da177e4
LT
889 hda_nid_t nid;
890
891 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
892 for (i = 0; i < total_nodes; i++, nid++) {
93e82ae7 893 function_id = snd_hda_param_read(codec, nid,
79c944ad 894 AC_PAR_FUNCTION_TYPE);
cd7643bf 895 switch (function_id & 0xff) {
673b683a
SK
896 case AC_GRP_AUDIO_FUNCTION:
897 codec->afg = nid;
79c944ad
JK
898 codec->afg_function_id = function_id & 0xff;
899 codec->afg_unsol = (function_id >> 8) & 1;
673b683a
SK
900 break;
901 case AC_GRP_MODEM_FUNCTION:
902 codec->mfg = nid;
79c944ad
JK
903 codec->mfg_function_id = function_id & 0xff;
904 codec->mfg_unsol = (function_id >> 8) & 1;
673b683a
SK
905 break;
906 default:
907 break;
908 }
1da177e4 909 }
1da177e4
LT
910}
911
54d17403
TI
912/*
913 * read widget caps for each widget and store in cache
914 */
915static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
916{
917 int i;
918 hda_nid_t nid;
919
920 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
921 &codec->start_nid);
922 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
0ba21762 923 if (!codec->wcaps)
54d17403
TI
924 return -ENOMEM;
925 nid = codec->start_nid;
926 for (i = 0; i < codec->num_nodes; i++, nid++)
927 codec->wcaps[i] = snd_hda_param_read(codec, nid,
928 AC_PAR_AUDIO_WIDGET_CAP);
929 return 0;
930}
931
3be14149
TI
932/* read all pin default configurations and save codec->init_pins */
933static int read_pin_defaults(struct hda_codec *codec)
934{
935 int i;
936 hda_nid_t nid = codec->start_nid;
937
938 for (i = 0; i < codec->num_nodes; i++, nid++) {
939 struct hda_pincfg *pin;
940 unsigned int wcaps = get_wcaps(codec, nid);
a22d543a 941 unsigned int wid_type = get_wcaps_type(wcaps);
3be14149
TI
942 if (wid_type != AC_WID_PIN)
943 continue;
944 pin = snd_array_new(&codec->init_pins);
945 if (!pin)
946 return -ENOMEM;
947 pin->nid = nid;
948 pin->cfg = snd_hda_codec_read(codec, nid, 0,
949 AC_VERB_GET_CONFIG_DEFAULT, 0);
ac0547dc
TI
950 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
951 AC_VERB_GET_PIN_WIDGET_CONTROL,
952 0);
3be14149
TI
953 }
954 return 0;
955}
956
957/* look up the given pin config list and return the item matching with NID */
958static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
959 struct snd_array *array,
960 hda_nid_t nid)
961{
962 int i;
963 for (i = 0; i < array->used; i++) {
964 struct hda_pincfg *pin = snd_array_elem(array, i);
965 if (pin->nid == nid)
966 return pin;
967 }
968 return NULL;
969}
970
971/* write a config value for the given NID */
972static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
973 unsigned int cfg)
974{
975 int i;
976 for (i = 0; i < 4; i++) {
977 snd_hda_codec_write(codec, nid, 0,
978 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
979 cfg & 0xff);
980 cfg >>= 8;
981 }
982}
983
984/* set the current pin config value for the given NID.
985 * the value is cached, and read via snd_hda_codec_get_pincfg()
986 */
987int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
988 hda_nid_t nid, unsigned int cfg)
989{
990 struct hda_pincfg *pin;
5e7b8e0d 991 unsigned int oldcfg;
3be14149 992
b82855a0
TI
993 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
994 return -EINVAL;
995
5e7b8e0d 996 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
3be14149
TI
997 pin = look_up_pincfg(codec, list, nid);
998 if (!pin) {
999 pin = snd_array_new(list);
1000 if (!pin)
1001 return -ENOMEM;
1002 pin->nid = nid;
1003 }
1004 pin->cfg = cfg;
5e7b8e0d
TI
1005
1006 /* change only when needed; e.g. if the pincfg is already present
1007 * in user_pins[], don't write it
1008 */
1009 cfg = snd_hda_codec_get_pincfg(codec, nid);
1010 if (oldcfg != cfg)
1011 set_pincfg(codec, nid, cfg);
3be14149
TI
1012 return 0;
1013}
1014
d5191e50
TI
1015/**
1016 * snd_hda_codec_set_pincfg - Override a pin default configuration
1017 * @codec: the HDA codec
1018 * @nid: NID to set the pin config
1019 * @cfg: the pin default config value
1020 *
1021 * Override a pin default configuration value in the cache.
1022 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1023 * priority than the real hardware value.
1024 */
3be14149
TI
1025int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1026 hda_nid_t nid, unsigned int cfg)
1027{
346ff70f 1028 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
3be14149
TI
1029}
1030EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1031
d5191e50
TI
1032/**
1033 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1034 * @codec: the HDA codec
1035 * @nid: NID to get the pin config
1036 *
1037 * Get the current pin config value of the given pin NID.
1038 * If the pincfg value is cached or overridden via sysfs or driver,
1039 * returns the cached value.
1040 */
3be14149
TI
1041unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1042{
1043 struct hda_pincfg *pin;
1044
3be14149 1045#ifdef CONFIG_SND_HDA_HWDEP
346ff70f 1046 pin = look_up_pincfg(codec, &codec->user_pins, nid);
3be14149
TI
1047 if (pin)
1048 return pin->cfg;
1049#endif
5e7b8e0d
TI
1050 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1051 if (pin)
1052 return pin->cfg;
3be14149
TI
1053 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1054 if (pin)
1055 return pin->cfg;
1056 return 0;
1057}
1058EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1059
1060/* restore all current pin configs */
1061static void restore_pincfgs(struct hda_codec *codec)
1062{
1063 int i;
1064 for (i = 0; i < codec->init_pins.used; i++) {
1065 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1066 set_pincfg(codec, pin->nid,
1067 snd_hda_codec_get_pincfg(codec, pin->nid));
1068 }
1069}
54d17403 1070
92ee6162
TI
1071/**
1072 * snd_hda_shutup_pins - Shut up all pins
1073 * @codec: the HDA codec
1074 *
1075 * Clear all pin controls to shup up before suspend for avoiding click noise.
1076 * The controls aren't cached so that they can be resumed properly.
1077 */
1078void snd_hda_shutup_pins(struct hda_codec *codec)
1079{
1080 int i;
ac0547dc
TI
1081 /* don't shut up pins when unloading the driver; otherwise it breaks
1082 * the default pin setup at the next load of the driver
1083 */
1084 if (codec->bus->shutdown)
1085 return;
92ee6162
TI
1086 for (i = 0; i < codec->init_pins.used; i++) {
1087 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1088 /* use read here for syncing after issuing each verb */
1089 snd_hda_codec_read(codec, pin->nid, 0,
1090 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1091 }
ac0547dc 1092 codec->pins_shutup = 1;
92ee6162
TI
1093}
1094EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1095
1c7276cf 1096#ifdef SND_HDA_NEEDS_RESUME
ac0547dc
TI
1097/* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1098static void restore_shutup_pins(struct hda_codec *codec)
1099{
1100 int i;
1101 if (!codec->pins_shutup)
1102 return;
1103 if (codec->bus->shutdown)
1104 return;
1105 for (i = 0; i < codec->init_pins.used; i++) {
1106 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1107 snd_hda_codec_write(codec, pin->nid, 0,
1108 AC_VERB_SET_PIN_WIDGET_CONTROL,
1109 pin->ctrl);
1110 }
1111 codec->pins_shutup = 0;
1112}
1c7276cf 1113#endif
ac0547dc 1114
01751f54
TI
1115static void init_hda_cache(struct hda_cache_rec *cache,
1116 unsigned int record_size);
1fcaee6e 1117static void free_hda_cache(struct hda_cache_rec *cache);
01751f54 1118
3be14149
TI
1119/* restore the initial pin cfgs and release all pincfg lists */
1120static void restore_init_pincfgs(struct hda_codec *codec)
1121{
346ff70f 1122 /* first free driver_pins and user_pins, then call restore_pincfg
3be14149
TI
1123 * so that only the values in init_pins are restored
1124 */
346ff70f 1125 snd_array_free(&codec->driver_pins);
3be14149 1126#ifdef CONFIG_SND_HDA_HWDEP
346ff70f 1127 snd_array_free(&codec->user_pins);
3be14149
TI
1128#endif
1129 restore_pincfgs(codec);
1130 snd_array_free(&codec->init_pins);
1131}
1132
eb541337
TI
1133/*
1134 * audio-converter setup caches
1135 */
1136struct hda_cvt_setup {
1137 hda_nid_t nid;
1138 u8 stream_tag;
1139 u8 channel_id;
1140 u16 format_id;
1141 unsigned char active; /* cvt is currently used */
1142 unsigned char dirty; /* setups should be cleared */
1143};
1144
1145/* get or create a cache entry for the given audio converter NID */
1146static struct hda_cvt_setup *
1147get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1148{
1149 struct hda_cvt_setup *p;
1150 int i;
1151
1152 for (i = 0; i < codec->cvt_setups.used; i++) {
1153 p = snd_array_elem(&codec->cvt_setups, i);
1154 if (p->nid == nid)
1155 return p;
1156 }
1157 p = snd_array_new(&codec->cvt_setups);
1158 if (p)
1159 p->nid = nid;
1160 return p;
1161}
1162
1da177e4
LT
1163/*
1164 * codec destructor
1165 */
1166static void snd_hda_codec_free(struct hda_codec *codec)
1167{
0ba21762 1168 if (!codec)
1da177e4 1169 return;
3be14149 1170 restore_init_pincfgs(codec);
cb53c626
TI
1171#ifdef CONFIG_SND_HDA_POWER_SAVE
1172 cancel_delayed_work(&codec->power_work);
6acaed38 1173 flush_workqueue(codec->bus->workq);
cb53c626 1174#endif
1da177e4 1175 list_del(&codec->list);
d13bd412 1176 snd_array_free(&codec->mixers);
5b0cb1d8 1177 snd_array_free(&codec->nids);
a12d3e1e 1178 snd_array_free(&codec->conn_lists);
7c935976 1179 snd_array_free(&codec->spdif_out);
1da177e4
LT
1180 codec->bus->caddr_tbl[codec->addr] = NULL;
1181 if (codec->patch_ops.free)
1182 codec->patch_ops.free(codec);
1289e9e8 1183 module_put(codec->owner);
01751f54 1184 free_hda_cache(&codec->amp_cache);
b3ac5636 1185 free_hda_cache(&codec->cmd_cache);
812a2cca
TI
1186 kfree(codec->vendor_name);
1187 kfree(codec->chip_name);
f44ac837 1188 kfree(codec->modelname);
54d17403 1189 kfree(codec->wcaps);
1da177e4
LT
1190 kfree(codec);
1191}
1192
bb6ac72f
TI
1193static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1194 unsigned int power_state);
1195
1da177e4
LT
1196/**
1197 * snd_hda_codec_new - create a HDA codec
1198 * @bus: the bus to assign
1199 * @codec_addr: the codec address
1200 * @codecp: the pointer to store the generated codec
1201 *
1202 * Returns 0 if successful, or a negative error code.
1203 */
28aedaf7
NL
1204int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1205 unsigned int codec_addr,
1206 struct hda_codec **codecp)
1da177e4
LT
1207{
1208 struct hda_codec *codec;
ba443687 1209 char component[31];
1da177e4
LT
1210 int err;
1211
da3cec35
TI
1212 if (snd_BUG_ON(!bus))
1213 return -EINVAL;
1214 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1215 return -EINVAL;
1da177e4
LT
1216
1217 if (bus->caddr_tbl[codec_addr]) {
0ba21762
TI
1218 snd_printk(KERN_ERR "hda_codec: "
1219 "address 0x%x is already occupied\n", codec_addr);
1da177e4
LT
1220 return -EBUSY;
1221 }
1222
e560d8d8 1223 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
1da177e4
LT
1224 if (codec == NULL) {
1225 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1226 return -ENOMEM;
1227 }
1228
1229 codec->bus = bus;
1230 codec->addr = codec_addr;
62932df8 1231 mutex_init(&codec->spdif_mutex);
5a9e02e9 1232 mutex_init(&codec->control_mutex);
01751f54 1233 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
b3ac5636 1234 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
5b0cb1d8
JK
1235 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1236 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
3be14149 1237 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
346ff70f 1238 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
eb541337 1239 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
a12d3e1e 1240 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
7c935976 1241 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
6c1f45ea
TI
1242 if (codec->bus->modelname) {
1243 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1244 if (!codec->modelname) {
1245 snd_hda_codec_free(codec);
1246 return -ENODEV;
1247 }
1248 }
1da177e4 1249
cb53c626
TI
1250#ifdef CONFIG_SND_HDA_POWER_SAVE
1251 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1252 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1253 * the caller has to power down appropriatley after initialization
1254 * phase.
1255 */
1256 hda_keep_power_on(codec);
1257#endif
1258
1da177e4
LT
1259 list_add_tail(&codec->list, &bus->codec_list);
1260 bus->caddr_tbl[codec_addr] = codec;
1261
0ba21762
TI
1262 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1263 AC_PAR_VENDOR_ID);
111d3af5
TI
1264 if (codec->vendor_id == -1)
1265 /* read again, hopefully the access method was corrected
1266 * in the last read...
1267 */
1268 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1269 AC_PAR_VENDOR_ID);
0ba21762
TI
1270 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1271 AC_PAR_SUBSYSTEM_ID);
1272 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1273 AC_PAR_REV_ID);
1da177e4 1274
673b683a 1275 setup_fg_nodes(codec);
0ba21762 1276 if (!codec->afg && !codec->mfg) {
673b683a 1277 snd_printdd("hda_codec: no AFG or MFG node found\n");
3be14149
TI
1278 err = -ENODEV;
1279 goto error;
1da177e4
LT
1280 }
1281
3be14149
TI
1282 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1283 if (err < 0) {
54d17403 1284 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
3be14149 1285 goto error;
54d17403 1286 }
3be14149
TI
1287 err = read_pin_defaults(codec);
1288 if (err < 0)
1289 goto error;
54d17403 1290
0ba21762 1291 if (!codec->subsystem_id) {
86284e45 1292 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
0ba21762
TI
1293 codec->subsystem_id =
1294 snd_hda_codec_read(codec, nid, 0,
1295 AC_VERB_GET_SUBSYSTEM_ID, 0);
86284e45
TI
1296 }
1297
bb6ac72f
TI
1298 /* power-up all before initialization */
1299 hda_set_power_state(codec,
1300 codec->afg ? codec->afg : codec->mfg,
1301 AC_PWRST_D0);
1302
6c1f45ea
TI
1303 snd_hda_codec_proc_new(codec);
1304
6c1f45ea 1305 snd_hda_create_hwdep(codec);
6c1f45ea
TI
1306
1307 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1308 codec->subsystem_id, codec->revision_id);
1309 snd_component_add(codec->bus->card, component);
1310
1311 if (codecp)
1312 *codecp = codec;
1313 return 0;
3be14149
TI
1314
1315 error:
1316 snd_hda_codec_free(codec);
1317 return err;
6c1f45ea 1318}
ff7a3267 1319EXPORT_SYMBOL_HDA(snd_hda_codec_new);
6c1f45ea 1320
d5191e50
TI
1321/**
1322 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1323 * @codec: the HDA codec
1324 *
1325 * Start parsing of the given codec tree and (re-)initialize the whole
1326 * patch instance.
1327 *
1328 * Returns 0 if successful or a negative error code.
1329 */
6c1f45ea
TI
1330int snd_hda_codec_configure(struct hda_codec *codec)
1331{
1332 int err;
1333
d5ad630b 1334 codec->preset = find_codec_preset(codec);
812a2cca 1335 if (!codec->vendor_name || !codec->chip_name) {
f44ac837
TI
1336 err = get_codec_name(codec);
1337 if (err < 0)
1338 return err;
1339 }
1da177e4 1340
82467611 1341 if (is_generic_config(codec)) {
1da177e4 1342 err = snd_hda_parse_generic_codec(codec);
82467611
TI
1343 goto patched;
1344 }
82467611
TI
1345 if (codec->preset && codec->preset->patch) {
1346 err = codec->preset->patch(codec);
1347 goto patched;
1348 }
1349
1350 /* call the default parser */
82467611 1351 err = snd_hda_parse_generic_codec(codec);
35a1e0cc
TI
1352 if (err < 0)
1353 printk(KERN_ERR "hda-codec: No codec parser is available\n");
82467611
TI
1354
1355 patched:
6c1f45ea
TI
1356 if (!err && codec->patch_ops.unsol_event)
1357 err = init_unsol_queue(codec->bus);
f62faedb
TI
1358 /* audio codec should override the mixer name */
1359 if (!err && (codec->afg || !*codec->bus->card->mixername))
1360 snprintf(codec->bus->card->mixername,
1361 sizeof(codec->bus->card->mixername),
1362 "%s %s", codec->vendor_name, codec->chip_name);
6c1f45ea 1363 return err;
1da177e4 1364}
a1e21c90 1365EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
1da177e4
LT
1366
1367/**
1368 * snd_hda_codec_setup_stream - set up the codec for streaming
1369 * @codec: the CODEC to set up
1370 * @nid: the NID to set up
1371 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1372 * @channel_id: channel id to pass, zero based.
1373 * @format: stream format.
1374 */
0ba21762
TI
1375void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1376 u32 stream_tag,
1da177e4
LT
1377 int channel_id, int format)
1378{
3f50ac6a 1379 struct hda_codec *c;
eb541337
TI
1380 struct hda_cvt_setup *p;
1381 unsigned int oldval, newval;
62b7e5e0 1382 int type;
eb541337
TI
1383 int i;
1384
0ba21762 1385 if (!nid)
d21b37ea
TI
1386 return;
1387
0ba21762
TI
1388 snd_printdd("hda_codec_setup_stream: "
1389 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1da177e4 1390 nid, stream_tag, channel_id, format);
eb541337
TI
1391 p = get_hda_cvt_setup(codec, nid);
1392 if (!p)
1393 return;
1394 /* update the stream-id if changed */
1395 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1396 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1397 newval = (stream_tag << 4) | channel_id;
1398 if (oldval != newval)
1399 snd_hda_codec_write(codec, nid, 0,
1400 AC_VERB_SET_CHANNEL_STREAMID,
1401 newval);
1402 p->stream_tag = stream_tag;
1403 p->channel_id = channel_id;
1404 }
1405 /* update the format-id if changed */
1406 if (p->format_id != format) {
1407 oldval = snd_hda_codec_read(codec, nid, 0,
1408 AC_VERB_GET_STREAM_FORMAT, 0);
1409 if (oldval != format) {
1410 msleep(1);
1411 snd_hda_codec_write(codec, nid, 0,
1412 AC_VERB_SET_STREAM_FORMAT,
1413 format);
1414 }
1415 p->format_id = format;
1416 }
1417 p->active = 1;
1418 p->dirty = 0;
1419
1420 /* make other inactive cvts with the same stream-tag dirty */
62b7e5e0 1421 type = get_wcaps_type(get_wcaps(codec, nid));
3f50ac6a
TI
1422 list_for_each_entry(c, &codec->bus->codec_list, list) {
1423 for (i = 0; i < c->cvt_setups.used; i++) {
1424 p = snd_array_elem(&c->cvt_setups, i);
62b7e5e0
TI
1425 if (!p->active && p->stream_tag == stream_tag &&
1426 get_wcaps_type(get_wcaps(codec, p->nid)) == type)
3f50ac6a
TI
1427 p->dirty = 1;
1428 }
eb541337 1429 }
1da177e4 1430}
ff7a3267 1431EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
1da177e4 1432
f0cea797
TI
1433static void really_cleanup_stream(struct hda_codec *codec,
1434 struct hda_cvt_setup *q);
1435
d5191e50 1436/**
f0cea797 1437 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
d5191e50
TI
1438 * @codec: the CODEC to clean up
1439 * @nid: the NID to clean up
f0cea797 1440 * @do_now: really clean up the stream instead of clearing the active flag
d5191e50 1441 */
f0cea797
TI
1442void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1443 int do_now)
888afa15 1444{
eb541337
TI
1445 struct hda_cvt_setup *p;
1446
888afa15
TI
1447 if (!nid)
1448 return;
1449
0e7adbe2
TI
1450 if (codec->no_sticky_stream)
1451 do_now = 1;
1452
888afa15 1453 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
eb541337 1454 p = get_hda_cvt_setup(codec, nid);
f0cea797
TI
1455 if (p) {
1456 /* here we just clear the active flag when do_now isn't set;
1457 * actual clean-ups will be done later in
1458 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1459 */
1460 if (do_now)
1461 really_cleanup_stream(codec, p);
1462 else
1463 p->active = 0;
1464 }
eb541337 1465}
f0cea797 1466EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
eb541337
TI
1467
1468static void really_cleanup_stream(struct hda_codec *codec,
1469 struct hda_cvt_setup *q)
1470{
1471 hda_nid_t nid = q->nid;
888afa15 1472 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
888afa15 1473 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
eb541337
TI
1474 memset(q, 0, sizeof(*q));
1475 q->nid = nid;
1476}
1477
1478/* clean up the all conflicting obsolete streams */
1479static void purify_inactive_streams(struct hda_codec *codec)
1480{
3f50ac6a 1481 struct hda_codec *c;
eb541337
TI
1482 int i;
1483
3f50ac6a
TI
1484 list_for_each_entry(c, &codec->bus->codec_list, list) {
1485 for (i = 0; i < c->cvt_setups.used; i++) {
1486 struct hda_cvt_setup *p;
1487 p = snd_array_elem(&c->cvt_setups, i);
1488 if (p->dirty)
1489 really_cleanup_stream(c, p);
1490 }
eb541337
TI
1491 }
1492}
1493
1c7276cf 1494#ifdef SND_HDA_NEEDS_RESUME
eb541337
TI
1495/* clean up all streams; called from suspend */
1496static void hda_cleanup_all_streams(struct hda_codec *codec)
1497{
1498 int i;
1499
1500 for (i = 0; i < codec->cvt_setups.used; i++) {
1501 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1502 if (p->stream_tag)
1503 really_cleanup_stream(codec, p);
1504 }
888afa15 1505}
1c7276cf 1506#endif
888afa15 1507
1da177e4
LT
1508/*
1509 * amp access functions
1510 */
1511
4a19faee 1512/* FIXME: more better hash key? */
28aedaf7 1513#define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1327a32b 1514#define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
92c7c8a7
TI
1515#define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1516#define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1da177e4 1517#define INFO_AMP_CAPS (1<<0)
4a19faee 1518#define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1da177e4
LT
1519
1520/* initialize the hash table */
1289e9e8 1521static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
01751f54
TI
1522 unsigned int record_size)
1523{
1524 memset(cache, 0, sizeof(*cache));
1525 memset(cache->hash, 0xff, sizeof(cache->hash));
603c4019 1526 snd_array_init(&cache->buf, record_size, 64);
01751f54
TI
1527}
1528
1fcaee6e 1529static void free_hda_cache(struct hda_cache_rec *cache)
1da177e4 1530{
603c4019 1531 snd_array_free(&cache->buf);
1da177e4
LT
1532}
1533
1534/* query the hash. allocate an entry if not found. */
a68d5a54 1535static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
1da177e4 1536{
01751f54
TI
1537 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1538 u16 cur = cache->hash[idx];
1539 struct hda_cache_head *info;
1da177e4
LT
1540
1541 while (cur != 0xffff) {
f43aa025 1542 info = snd_array_elem(&cache->buf, cur);
1da177e4
LT
1543 if (info->key == key)
1544 return info;
1545 cur = info->next;
1546 }
a68d5a54
TI
1547 return NULL;
1548}
1da177e4 1549
a68d5a54
TI
1550/* query the hash. allocate an entry if not found. */
1551static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1552 u32 key)
1553{
1554 struct hda_cache_head *info = get_hash(cache, key);
1555 if (!info) {
1556 u16 idx, cur;
1557 /* add a new hash entry */
1558 info = snd_array_new(&cache->buf);
1559 if (!info)
1560 return NULL;
1561 cur = snd_array_index(&cache->buf, info);
1562 info->key = key;
1563 info->val = 0;
1564 idx = key % (u16)ARRAY_SIZE(cache->hash);
1565 info->next = cache->hash[idx];
1566 cache->hash[idx] = cur;
1567 }
1da177e4
LT
1568 return info;
1569}
1570
01751f54
TI
1571/* query and allocate an amp hash entry */
1572static inline struct hda_amp_info *
1573get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1574{
1575 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1576}
1577
d5191e50
TI
1578/**
1579 * query_amp_caps - query AMP capabilities
1580 * @codec: the HD-auio codec
1581 * @nid: the NID to query
1582 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1583 *
1584 * Query AMP capabilities for the given widget and direction.
1585 * Returns the obtained capability bits.
1586 *
1587 * When cap bits have been already read, this doesn't read again but
1588 * returns the cached value.
1da177e4 1589 */
09a99959 1590u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1da177e4 1591{
0ba21762 1592 struct hda_amp_info *info;
1da177e4 1593
0ba21762
TI
1594 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1595 if (!info)
1da177e4 1596 return 0;
01751f54 1597 if (!(info->head.val & INFO_AMP_CAPS)) {
0ba21762 1598 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1da177e4 1599 nid = codec->afg;
0ba21762
TI
1600 info->amp_caps = snd_hda_param_read(codec, nid,
1601 direction == HDA_OUTPUT ?
1602 AC_PAR_AMP_OUT_CAP :
1603 AC_PAR_AMP_IN_CAP);
b75e53f0 1604 if (info->amp_caps)
01751f54 1605 info->head.val |= INFO_AMP_CAPS;
1da177e4
LT
1606 }
1607 return info->amp_caps;
1608}
ff7a3267 1609EXPORT_SYMBOL_HDA(query_amp_caps);
1da177e4 1610
d5191e50
TI
1611/**
1612 * snd_hda_override_amp_caps - Override the AMP capabilities
1613 * @codec: the CODEC to clean up
1614 * @nid: the NID to clean up
1615 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1616 * @caps: the capability bits to set
1617 *
1618 * Override the cached AMP caps bits value by the given one.
1619 * This function is useful if the driver needs to adjust the AMP ranges,
1620 * e.g. limit to 0dB, etc.
1621 *
1622 * Returns zero if successful or a negative error code.
1623 */
897cc188
TI
1624int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1625 unsigned int caps)
1626{
1627 struct hda_amp_info *info;
1628
1629 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1630 if (!info)
1631 return -EINVAL;
1632 info->amp_caps = caps;
01751f54 1633 info->head.val |= INFO_AMP_CAPS;
897cc188
TI
1634 return 0;
1635}
ff7a3267 1636EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
1327a32b 1637
92c7c8a7
TI
1638static unsigned int
1639query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1640 unsigned int (*func)(struct hda_codec *, hda_nid_t))
1327a32b
TI
1641{
1642 struct hda_amp_info *info;
1643
92c7c8a7 1644 info = get_alloc_amp_hash(codec, key);
1327a32b
TI
1645 if (!info)
1646 return 0;
1647 if (!info->head.val) {
1327a32b 1648 info->head.val |= INFO_AMP_CAPS;
92c7c8a7 1649 info->amp_caps = func(codec, nid);
1327a32b
TI
1650 }
1651 return info->amp_caps;
1652}
92c7c8a7
TI
1653
1654static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1655{
1656 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1657}
1658
d5191e50
TI
1659/**
1660 * snd_hda_query_pin_caps - Query PIN capabilities
1661 * @codec: the HD-auio codec
1662 * @nid: the NID to query
1663 *
1664 * Query PIN capabilities for the given widget.
1665 * Returns the obtained capability bits.
1666 *
1667 * When cap bits have been already read, this doesn't read again but
1668 * returns the cached value.
1669 */
92c7c8a7
TI
1670u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1671{
1672 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1673 read_pin_cap);
1674}
1327a32b 1675EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
897cc188 1676
864f92be
WF
1677/**
1678 * snd_hda_pin_sense - execute pin sense measurement
1679 * @codec: the CODEC to sense
1680 * @nid: the pin NID to sense
1681 *
1682 * Execute necessary pin sense measurement and return its Presence Detect,
1683 * Impedance, ELD Valid etc. status bits.
1684 */
1685u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
1686{
729d55ba 1687 u32 pincap;
864f92be 1688
729d55ba
TI
1689 if (!codec->no_trigger_sense) {
1690 pincap = snd_hda_query_pin_caps(codec, nid);
1691 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
28aedaf7
NL
1692 snd_hda_codec_read(codec, nid, 0,
1693 AC_VERB_SET_PIN_SENSE, 0);
729d55ba 1694 }
864f92be
WF
1695 return snd_hda_codec_read(codec, nid, 0,
1696 AC_VERB_GET_PIN_SENSE, 0);
1697}
1698EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
1699
1700/**
1701 * snd_hda_jack_detect - query pin Presence Detect status
1702 * @codec: the CODEC to sense
1703 * @nid: the pin NID to sense
1704 *
1705 * Query and return the pin's Presence Detect status.
1706 */
1707int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
1708{
28aedaf7
NL
1709 u32 sense = snd_hda_pin_sense(codec, nid);
1710 return !!(sense & AC_PINSENSE_PRESENCE);
864f92be
WF
1711}
1712EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
1713
1da177e4
LT
1714/*
1715 * read the current volume to info
4a19faee 1716 * if the cache exists, read the cache value.
1da177e4 1717 */
0ba21762
TI
1718static unsigned int get_vol_mute(struct hda_codec *codec,
1719 struct hda_amp_info *info, hda_nid_t nid,
1720 int ch, int direction, int index)
1da177e4
LT
1721{
1722 u32 val, parm;
1723
01751f54 1724 if (info->head.val & INFO_AMP_VOL(ch))
4a19faee 1725 return info->vol[ch];
1da177e4
LT
1726
1727 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1728 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1729 parm |= index;
0ba21762
TI
1730 val = snd_hda_codec_read(codec, nid, 0,
1731 AC_VERB_GET_AMP_GAIN_MUTE, parm);
1da177e4 1732 info->vol[ch] = val & 0xff;
01751f54 1733 info->head.val |= INFO_AMP_VOL(ch);
4a19faee 1734 return info->vol[ch];
1da177e4
LT
1735}
1736
1737/*
4a19faee 1738 * write the current volume in info to the h/w and update the cache
1da177e4 1739 */
4a19faee 1740static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
0ba21762
TI
1741 hda_nid_t nid, int ch, int direction, int index,
1742 int val)
1da177e4
LT
1743{
1744 u32 parm;
1745
1746 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1747 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1748 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1749 parm |= val;
1750 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
4a19faee 1751 info->vol[ch] = val;
1da177e4
LT
1752}
1753
d5191e50
TI
1754/**
1755 * snd_hda_codec_amp_read - Read AMP value
1756 * @codec: HD-audio codec
1757 * @nid: NID to read the AMP value
1758 * @ch: channel (left=0 or right=1)
1759 * @direction: #HDA_INPUT or #HDA_OUTPUT
1760 * @index: the index value (only for input direction)
1761 *
1762 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
1da177e4 1763 */
834be88d
TI
1764int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1765 int direction, int index)
1da177e4 1766{
0ba21762
TI
1767 struct hda_amp_info *info;
1768 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1769 if (!info)
1da177e4 1770 return 0;
4a19faee 1771 return get_vol_mute(codec, info, nid, ch, direction, index);
1da177e4 1772}
ff7a3267 1773EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
1da177e4 1774
d5191e50
TI
1775/**
1776 * snd_hda_codec_amp_update - update the AMP value
1777 * @codec: HD-audio codec
1778 * @nid: NID to read the AMP value
1779 * @ch: channel (left=0 or right=1)
1780 * @direction: #HDA_INPUT or #HDA_OUTPUT
1781 * @idx: the index value (only for input direction)
1782 * @mask: bit mask to set
1783 * @val: the bits value to set
1784 *
1785 * Update the AMP value with a bit mask.
1786 * Returns 0 if the value is unchanged, 1 if changed.
4a19faee 1787 */
834be88d
TI
1788int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1789 int direction, int idx, int mask, int val)
1da177e4 1790{
0ba21762 1791 struct hda_amp_info *info;
4a19faee 1792
0ba21762
TI
1793 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1794 if (!info)
1da177e4 1795 return 0;
46712646
TI
1796 if (snd_BUG_ON(mask & ~0xff))
1797 mask &= 0xff;
4a19faee
TI
1798 val &= mask;
1799 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
82beb8fd 1800 if (info->vol[ch] == val)
1da177e4 1801 return 0;
4a19faee 1802 put_vol_mute(codec, info, nid, ch, direction, idx, val);
1da177e4
LT
1803 return 1;
1804}
ff7a3267 1805EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
1da177e4 1806
d5191e50
TI
1807/**
1808 * snd_hda_codec_amp_stereo - update the AMP stereo values
1809 * @codec: HD-audio codec
1810 * @nid: NID to read the AMP value
1811 * @direction: #HDA_INPUT or #HDA_OUTPUT
1812 * @idx: the index value (only for input direction)
1813 * @mask: bit mask to set
1814 * @val: the bits value to set
1815 *
1816 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1817 * stereo widget with the same mask and value.
47fd830a
TI
1818 */
1819int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1820 int direction, int idx, int mask, int val)
1821{
1822 int ch, ret = 0;
46712646
TI
1823
1824 if (snd_BUG_ON(mask & ~0xff))
1825 mask &= 0xff;
47fd830a
TI
1826 for (ch = 0; ch < 2; ch++)
1827 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1828 idx, mask, val);
1829 return ret;
1830}
ff7a3267 1831EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
47fd830a 1832
cb53c626 1833#ifdef SND_HDA_NEEDS_RESUME
d5191e50
TI
1834/**
1835 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1836 * @codec: HD-audio codec
1837 *
1838 * Resume the all amp commands from the cache.
1839 */
b3ac5636
TI
1840void snd_hda_codec_resume_amp(struct hda_codec *codec)
1841{
603c4019 1842 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
b3ac5636
TI
1843 int i;
1844
603c4019 1845 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
b3ac5636
TI
1846 u32 key = buffer->head.key;
1847 hda_nid_t nid;
1848 unsigned int idx, dir, ch;
1849 if (!key)
1850 continue;
1851 nid = key & 0xff;
1852 idx = (key >> 16) & 0xff;
1853 dir = (key >> 24) & 0xff;
1854 for (ch = 0; ch < 2; ch++) {
1855 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1856 continue;
1857 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1858 buffer->vol[ch]);
1859 }
1860 }
1861}
ff7a3267 1862EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
cb53c626 1863#endif /* SND_HDA_NEEDS_RESUME */
1da177e4 1864
afbd9b84
TI
1865static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1866 unsigned int ofs)
1867{
1868 u32 caps = query_amp_caps(codec, nid, dir);
1869 /* get num steps */
1870 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1871 if (ofs < caps)
1872 caps -= ofs;
1873 return caps;
1874}
1875
d5191e50
TI
1876/**
1877 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1878 *
1879 * The control element is supposed to have the private_value field
1880 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1881 */
0ba21762
TI
1882int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1883 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
1884{
1885 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1886 u16 nid = get_amp_nid(kcontrol);
1887 u8 chs = get_amp_channels(kcontrol);
1888 int dir = get_amp_direction(kcontrol);
29fdbec2 1889 unsigned int ofs = get_amp_offset(kcontrol);
1da177e4 1890
afbd9b84
TI
1891 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1892 uinfo->count = chs == 3 ? 2 : 1;
1893 uinfo->value.integer.min = 0;
1894 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1895 if (!uinfo->value.integer.max) {
0ba21762 1896 printk(KERN_WARNING "hda_codec: "
9c8f2abd
TI
1897 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1898 kcontrol->id.name);
1da177e4
LT
1899 return -EINVAL;
1900 }
1da177e4
LT
1901 return 0;
1902}
ff7a3267 1903EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
1da177e4 1904
29fdbec2
TI
1905
1906static inline unsigned int
1907read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1908 int ch, int dir, int idx, unsigned int ofs)
1909{
1910 unsigned int val;
1911 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1912 val &= HDA_AMP_VOLMASK;
1913 if (val >= ofs)
1914 val -= ofs;
1915 else
1916 val = 0;
1917 return val;
1918}
1919
1920static inline int
1921update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1922 int ch, int dir, int idx, unsigned int ofs,
1923 unsigned int val)
1924{
afbd9b84
TI
1925 unsigned int maxval;
1926
29fdbec2
TI
1927 if (val > 0)
1928 val += ofs;
7ccc3efa
TI
1929 /* ofs = 0: raw max value */
1930 maxval = get_amp_max_value(codec, nid, dir, 0);
afbd9b84
TI
1931 if (val > maxval)
1932 val = maxval;
29fdbec2
TI
1933 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1934 HDA_AMP_VOLMASK, val);
1935}
1936
d5191e50
TI
1937/**
1938 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1939 *
1940 * The control element is supposed to have the private_value field
1941 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1942 */
0ba21762
TI
1943int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1944 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
1945{
1946 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1947 hda_nid_t nid = get_amp_nid(kcontrol);
1948 int chs = get_amp_channels(kcontrol);
1949 int dir = get_amp_direction(kcontrol);
1950 int idx = get_amp_index(kcontrol);
29fdbec2 1951 unsigned int ofs = get_amp_offset(kcontrol);
1da177e4
LT
1952 long *valp = ucontrol->value.integer.value;
1953
1954 if (chs & 1)
29fdbec2 1955 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
1da177e4 1956 if (chs & 2)
29fdbec2 1957 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
1da177e4
LT
1958 return 0;
1959}
ff7a3267 1960EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
1da177e4 1961
d5191e50
TI
1962/**
1963 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1964 *
1965 * The control element is supposed to have the private_value field
1966 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1967 */
0ba21762
TI
1968int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1969 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
1970{
1971 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1972 hda_nid_t nid = get_amp_nid(kcontrol);
1973 int chs = get_amp_channels(kcontrol);
1974 int dir = get_amp_direction(kcontrol);
1975 int idx = get_amp_index(kcontrol);
29fdbec2 1976 unsigned int ofs = get_amp_offset(kcontrol);
1da177e4
LT
1977 long *valp = ucontrol->value.integer.value;
1978 int change = 0;
1979
cb53c626 1980 snd_hda_power_up(codec);
b9f5a89c 1981 if (chs & 1) {
29fdbec2 1982 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
b9f5a89c
NG
1983 valp++;
1984 }
4a19faee 1985 if (chs & 2)
29fdbec2 1986 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
cb53c626 1987 snd_hda_power_down(codec);
1da177e4
LT
1988 return change;
1989}
ff7a3267 1990EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
1da177e4 1991
d5191e50
TI
1992/**
1993 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
1994 *
1995 * The control element is supposed to have the private_value field
1996 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1997 */
302e9c5a
JK
1998int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1999 unsigned int size, unsigned int __user *_tlv)
2000{
2001 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2002 hda_nid_t nid = get_amp_nid(kcontrol);
2003 int dir = get_amp_direction(kcontrol);
29fdbec2 2004 unsigned int ofs = get_amp_offset(kcontrol);
de8c85f7 2005 bool min_mute = get_amp_min_mute(kcontrol);
302e9c5a
JK
2006 u32 caps, val1, val2;
2007
2008 if (size < 4 * sizeof(unsigned int))
2009 return -ENOMEM;
2010 caps = query_amp_caps(codec, nid, dir);
0ba21762
TI
2011 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2012 val2 = (val2 + 1) * 25;
302e9c5a 2013 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
29fdbec2 2014 val1 += ofs;
302e9c5a 2015 val1 = ((int)val1) * ((int)val2);
de8c85f7 2016 if (min_mute)
c08d9169 2017 val2 |= TLV_DB_SCALE_MUTE;
302e9c5a
JK
2018 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2019 return -EFAULT;
2020 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2021 return -EFAULT;
2022 if (put_user(val1, _tlv + 2))
2023 return -EFAULT;
2024 if (put_user(val2, _tlv + 3))
2025 return -EFAULT;
2026 return 0;
2027}
ff7a3267 2028EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
302e9c5a 2029
d5191e50
TI
2030/**
2031 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2032 * @codec: HD-audio codec
2033 * @nid: NID of a reference widget
2034 * @dir: #HDA_INPUT or #HDA_OUTPUT
2035 * @tlv: TLV data to be stored, at least 4 elements
2036 *
2037 * Set (static) TLV data for a virtual master volume using the AMP caps
2038 * obtained from the reference NID.
2039 * The volume range is recalculated as if the max volume is 0dB.
2134ea4f
TI
2040 */
2041void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2042 unsigned int *tlv)
2043{
2044 u32 caps;
2045 int nums, step;
2046
2047 caps = query_amp_caps(codec, nid, dir);
2048 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2049 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2050 step = (step + 1) * 25;
2051 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2052 tlv[1] = 2 * sizeof(unsigned int);
2053 tlv[2] = -nums * step;
2054 tlv[3] = step;
2055}
ff7a3267 2056EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
2134ea4f
TI
2057
2058/* find a mixer control element with the given name */
09f99701
TI
2059static struct snd_kcontrol *
2060_snd_hda_find_mixer_ctl(struct hda_codec *codec,
2061 const char *name, int idx)
2134ea4f
TI
2062{
2063 struct snd_ctl_elem_id id;
2064 memset(&id, 0, sizeof(id));
2065 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
09f99701 2066 id.index = idx;
18cb7109
TI
2067 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2068 return NULL;
2134ea4f
TI
2069 strcpy(id.name, name);
2070 return snd_ctl_find_id(codec->bus->card, &id);
2071}
2072
d5191e50
TI
2073/**
2074 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2075 * @codec: HD-audio codec
2076 * @name: ctl id name string
2077 *
2078 * Get the control element with the given id string and IFACE_MIXER.
2079 */
09f99701
TI
2080struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2081 const char *name)
2082{
2083 return _snd_hda_find_mixer_ctl(codec, name, 0);
2084}
ff7a3267 2085EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
09f99701 2086
1afe206a
TI
2087static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name)
2088{
2089 int idx;
2090 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
2091 if (!_snd_hda_find_mixer_ctl(codec, name, idx))
2092 return idx;
2093 }
2094 return -EBUSY;
2095}
2096
d5191e50 2097/**
5b0cb1d8 2098 * snd_hda_ctl_add - Add a control element and assign to the codec
d5191e50
TI
2099 * @codec: HD-audio codec
2100 * @nid: corresponding NID (optional)
2101 * @kctl: the control element to assign
2102 *
2103 * Add the given control element to an array inside the codec instance.
2104 * All control elements belonging to a codec are supposed to be added
2105 * by this function so that a proper clean-up works at the free or
2106 * reconfiguration time.
2107 *
2108 * If non-zero @nid is passed, the NID is assigned to the control element.
2109 * The assignment is shown in the codec proc file.
2110 *
2111 * snd_hda_ctl_add() checks the control subdev id field whether
2112 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
9e3fd871
JK
2113 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2114 * specifies if kctl->private_value is a HDA amplifier value.
d5191e50 2115 */
3911a4c1
JK
2116int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2117 struct snd_kcontrol *kctl)
d13bd412
TI
2118{
2119 int err;
9e3fd871 2120 unsigned short flags = 0;
3911a4c1 2121 struct hda_nid_item *item;
d13bd412 2122
5e26dfd0 2123 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
9e3fd871 2124 flags |= HDA_NID_ITEM_AMP;
5e26dfd0
JK
2125 if (nid == 0)
2126 nid = get_amp_nid_(kctl->private_value);
2127 }
9e3fd871
JK
2128 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2129 nid = kctl->id.subdevice & 0xffff;
5e26dfd0 2130 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
4d02d1b6 2131 kctl->id.subdevice = 0;
d13bd412
TI
2132 err = snd_ctl_add(codec->bus->card, kctl);
2133 if (err < 0)
2134 return err;
3911a4c1
JK
2135 item = snd_array_new(&codec->mixers);
2136 if (!item)
d13bd412 2137 return -ENOMEM;
3911a4c1
JK
2138 item->kctl = kctl;
2139 item->nid = nid;
9e3fd871 2140 item->flags = flags;
d13bd412
TI
2141 return 0;
2142}
ff7a3267 2143EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
d13bd412 2144
5b0cb1d8
JK
2145/**
2146 * snd_hda_add_nid - Assign a NID to a control element
2147 * @codec: HD-audio codec
2148 * @nid: corresponding NID (optional)
2149 * @kctl: the control element to assign
2150 * @index: index to kctl
2151 *
2152 * Add the given control element to an array inside the codec instance.
2153 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2154 * NID:KCTL mapping - for example "Capture Source" selector.
2155 */
2156int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2157 unsigned int index, hda_nid_t nid)
2158{
2159 struct hda_nid_item *item;
2160
2161 if (nid > 0) {
2162 item = snd_array_new(&codec->nids);
2163 if (!item)
2164 return -ENOMEM;
2165 item->kctl = kctl;
2166 item->index = index;
2167 item->nid = nid;
2168 return 0;
2169 }
28d1a85e
TI
2170 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2171 kctl->id.name, kctl->id.index, index);
5b0cb1d8
JK
2172 return -EINVAL;
2173}
2174EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2175
d5191e50
TI
2176/**
2177 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2178 * @codec: HD-audio codec
2179 */
d13bd412
TI
2180void snd_hda_ctls_clear(struct hda_codec *codec)
2181{
2182 int i;
3911a4c1 2183 struct hda_nid_item *items = codec->mixers.list;
d13bd412 2184 for (i = 0; i < codec->mixers.used; i++)
3911a4c1 2185 snd_ctl_remove(codec->bus->card, items[i].kctl);
d13bd412 2186 snd_array_free(&codec->mixers);
5b0cb1d8 2187 snd_array_free(&codec->nids);
d13bd412
TI
2188}
2189
a65d629c
TI
2190/* pseudo device locking
2191 * toggle card->shutdown to allow/disallow the device access (as a hack)
2192 */
2193static int hda_lock_devices(struct snd_card *card)
6c1f45ea 2194{
a65d629c
TI
2195 spin_lock(&card->files_lock);
2196 if (card->shutdown) {
2197 spin_unlock(&card->files_lock);
2198 return -EINVAL;
2199 }
2200 card->shutdown = 1;
2201 spin_unlock(&card->files_lock);
2202 return 0;
2203}
2204
2205static void hda_unlock_devices(struct snd_card *card)
2206{
2207 spin_lock(&card->files_lock);
2208 card->shutdown = 0;
2209 spin_unlock(&card->files_lock);
2210}
2211
d5191e50
TI
2212/**
2213 * snd_hda_codec_reset - Clear all objects assigned to the codec
2214 * @codec: HD-audio codec
2215 *
2216 * This frees the all PCM and control elements assigned to the codec, and
2217 * clears the caches and restores the pin default configurations.
2218 *
2219 * When a device is being used, it returns -EBSY. If successfully freed,
2220 * returns zero.
2221 */
a65d629c
TI
2222int snd_hda_codec_reset(struct hda_codec *codec)
2223{
2224 struct snd_card *card = codec->bus->card;
2225 int i, pcm;
2226
2227 if (hda_lock_devices(card) < 0)
2228 return -EBUSY;
2229 /* check whether the codec isn't used by any mixer or PCM streams */
2230 if (!list_empty(&card->ctl_files)) {
2231 hda_unlock_devices(card);
2232 return -EBUSY;
2233 }
2234 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2235 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2236 if (!cpcm->pcm)
2237 continue;
2238 if (cpcm->pcm->streams[0].substream_opened ||
2239 cpcm->pcm->streams[1].substream_opened) {
2240 hda_unlock_devices(card);
2241 return -EBUSY;
2242 }
2243 }
2244
2245 /* OK, let it free */
6c1f45ea
TI
2246
2247#ifdef CONFIG_SND_HDA_POWER_SAVE
2248 cancel_delayed_work(&codec->power_work);
6acaed38 2249 flush_workqueue(codec->bus->workq);
6c1f45ea
TI
2250#endif
2251 snd_hda_ctls_clear(codec);
2252 /* relase PCMs */
2253 for (i = 0; i < codec->num_pcms; i++) {
529bd6c4 2254 if (codec->pcm_info[i].pcm) {
a65d629c 2255 snd_device_free(card, codec->pcm_info[i].pcm);
529bd6c4
TI
2256 clear_bit(codec->pcm_info[i].device,
2257 codec->bus->pcm_dev_bits);
2258 }
6c1f45ea
TI
2259 }
2260 if (codec->patch_ops.free)
2261 codec->patch_ops.free(codec);
56d17712 2262 codec->proc_widget_hook = NULL;
6c1f45ea
TI
2263 codec->spec = NULL;
2264 free_hda_cache(&codec->amp_cache);
2265 free_hda_cache(&codec->cmd_cache);
827057f5
TI
2266 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2267 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
346ff70f
TI
2268 /* free only driver_pins so that init_pins + user_pins are restored */
2269 snd_array_free(&codec->driver_pins);
3be14149 2270 restore_pincfgs(codec);
6c1f45ea
TI
2271 codec->num_pcms = 0;
2272 codec->pcm_info = NULL;
2273 codec->preset = NULL;
d1f1af2d
TI
2274 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2275 codec->slave_dig_outs = NULL;
2276 codec->spdif_status_reset = 0;
1289e9e8
TI
2277 module_put(codec->owner);
2278 codec->owner = NULL;
a65d629c
TI
2279
2280 /* allow device access again */
2281 hda_unlock_devices(card);
2282 return 0;
6c1f45ea
TI
2283}
2284
d5191e50
TI
2285/**
2286 * snd_hda_add_vmaster - create a virtual master control and add slaves
2287 * @codec: HD-audio codec
2288 * @name: vmaster control name
2289 * @tlv: TLV data (optional)
2290 * @slaves: slave control names (optional)
2291 *
2292 * Create a virtual master control with the given name. The TLV data
2293 * must be either NULL or a valid data.
2294 *
2295 * @slaves is a NULL-terminated array of strings, each of which is a
2296 * slave control name. All controls with these names are assigned to
2297 * the new virtual master control.
2298 *
2299 * This function returns zero if successful or a negative error code.
2300 */
2134ea4f 2301int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
ea734963 2302 unsigned int *tlv, const char * const *slaves)
2134ea4f
TI
2303{
2304 struct snd_kcontrol *kctl;
ea734963 2305 const char * const *s;
2134ea4f
TI
2306 int err;
2307
2f085549
TI
2308 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
2309 ;
2310 if (!*s) {
2311 snd_printdd("No slave found for %s\n", name);
2312 return 0;
2313 }
2134ea4f
TI
2314 kctl = snd_ctl_make_virtual_master(name, tlv);
2315 if (!kctl)
2316 return -ENOMEM;
3911a4c1 2317 err = snd_hda_ctl_add(codec, 0, kctl);
2134ea4f
TI
2318 if (err < 0)
2319 return err;
28aedaf7 2320
2134ea4f
TI
2321 for (s = slaves; *s; s++) {
2322 struct snd_kcontrol *sctl;
7a411ee0
TI
2323 int i = 0;
2324 for (;;) {
2325 sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
2326 if (!sctl) {
2327 if (!i)
2328 snd_printdd("Cannot find slave %s, "
2329 "skipped\n", *s);
2330 break;
2331 }
2332 err = snd_ctl_add_slave(kctl, sctl);
2333 if (err < 0)
2334 return err;
2335 i++;
2134ea4f 2336 }
2134ea4f
TI
2337 }
2338 return 0;
2339}
ff7a3267 2340EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
2134ea4f 2341
d5191e50
TI
2342/**
2343 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2344 *
2345 * The control element is supposed to have the private_value field
2346 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2347 */
0ba21762
TI
2348int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2349 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
2350{
2351 int chs = get_amp_channels(kcontrol);
2352
2353 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2354 uinfo->count = chs == 3 ? 2 : 1;
2355 uinfo->value.integer.min = 0;
2356 uinfo->value.integer.max = 1;
2357 return 0;
2358}
ff7a3267 2359EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
1da177e4 2360
d5191e50
TI
2361/**
2362 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2363 *
2364 * The control element is supposed to have the private_value field
2365 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2366 */
0ba21762
TI
2367int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2368 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
2369{
2370 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2371 hda_nid_t nid = get_amp_nid(kcontrol);
2372 int chs = get_amp_channels(kcontrol);
2373 int dir = get_amp_direction(kcontrol);
2374 int idx = get_amp_index(kcontrol);
2375 long *valp = ucontrol->value.integer.value;
2376
2377 if (chs & 1)
0ba21762 2378 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
47fd830a 2379 HDA_AMP_MUTE) ? 0 : 1;
1da177e4 2380 if (chs & 2)
0ba21762 2381 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
47fd830a 2382 HDA_AMP_MUTE) ? 0 : 1;
1da177e4
LT
2383 return 0;
2384}
ff7a3267 2385EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
1da177e4 2386
d5191e50
TI
2387/**
2388 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2389 *
2390 * The control element is supposed to have the private_value field
2391 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2392 */
0ba21762
TI
2393int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2394 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
2395{
2396 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2397 hda_nid_t nid = get_amp_nid(kcontrol);
2398 int chs = get_amp_channels(kcontrol);
2399 int dir = get_amp_direction(kcontrol);
2400 int idx = get_amp_index(kcontrol);
1da177e4
LT
2401 long *valp = ucontrol->value.integer.value;
2402 int change = 0;
2403
cb53c626 2404 snd_hda_power_up(codec);
b9f5a89c 2405 if (chs & 1) {
4a19faee 2406 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
47fd830a
TI
2407 HDA_AMP_MUTE,
2408 *valp ? 0 : HDA_AMP_MUTE);
b9f5a89c
NG
2409 valp++;
2410 }
4a19faee
TI
2411 if (chs & 2)
2412 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
47fd830a
TI
2413 HDA_AMP_MUTE,
2414 *valp ? 0 : HDA_AMP_MUTE);
9e5341b9 2415 hda_call_check_power_status(codec, nid);
cb53c626 2416 snd_hda_power_down(codec);
1da177e4
LT
2417 return change;
2418}
ff7a3267 2419EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
1da177e4 2420
67d634c0 2421#ifdef CONFIG_SND_HDA_INPUT_BEEP
d5191e50
TI
2422/**
2423 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2424 *
2425 * This function calls snd_hda_enable_beep_device(), which behaves differently
2426 * depending on beep_mode option.
2427 */
123c07ae
JK
2428int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2429 struct snd_ctl_elem_value *ucontrol)
2430{
2431 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2432 long *valp = ucontrol->value.integer.value;
2433
2434 snd_hda_enable_beep_device(codec, *valp);
2435 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2436}
2437EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
67d634c0 2438#endif /* CONFIG_SND_HDA_INPUT_BEEP */
123c07ae 2439
985be54b
TI
2440/*
2441 * bound volume controls
2442 *
2443 * bind multiple volumes (# indices, from 0)
2444 */
2445
2446#define AMP_VAL_IDX_SHIFT 19
2447#define AMP_VAL_IDX_MASK (0x0f<<19)
2448
d5191e50
TI
2449/**
2450 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2451 *
2452 * The control element is supposed to have the private_value field
2453 * set up via HDA_BIND_MUTE*() macros.
2454 */
0ba21762
TI
2455int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2456 struct snd_ctl_elem_value *ucontrol)
985be54b
TI
2457{
2458 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2459 unsigned long pval;
2460 int err;
2461
5a9e02e9 2462 mutex_lock(&codec->control_mutex);
985be54b
TI
2463 pval = kcontrol->private_value;
2464 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2465 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2466 kcontrol->private_value = pval;
5a9e02e9 2467 mutex_unlock(&codec->control_mutex);
985be54b
TI
2468 return err;
2469}
ff7a3267 2470EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
985be54b 2471
d5191e50
TI
2472/**
2473 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2474 *
2475 * The control element is supposed to have the private_value field
2476 * set up via HDA_BIND_MUTE*() macros.
2477 */
0ba21762
TI
2478int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2479 struct snd_ctl_elem_value *ucontrol)
985be54b
TI
2480{
2481 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2482 unsigned long pval;
2483 int i, indices, err = 0, change = 0;
2484
5a9e02e9 2485 mutex_lock(&codec->control_mutex);
985be54b
TI
2486 pval = kcontrol->private_value;
2487 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2488 for (i = 0; i < indices; i++) {
0ba21762
TI
2489 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2490 (i << AMP_VAL_IDX_SHIFT);
985be54b
TI
2491 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2492 if (err < 0)
2493 break;
2494 change |= err;
2495 }
2496 kcontrol->private_value = pval;
5a9e02e9 2497 mutex_unlock(&codec->control_mutex);
985be54b
TI
2498 return err < 0 ? err : change;
2499}
ff7a3267 2500EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
985be54b 2501
d5191e50
TI
2502/**
2503 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2504 *
2505 * The control element is supposed to have the private_value field
2506 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
532d5381
TI
2507 */
2508int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2509 struct snd_ctl_elem_info *uinfo)
2510{
2511 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2512 struct hda_bind_ctls *c;
2513 int err;
2514
5a9e02e9 2515 mutex_lock(&codec->control_mutex);
14c65f98 2516 c = (struct hda_bind_ctls *)kcontrol->private_value;
532d5381
TI
2517 kcontrol->private_value = *c->values;
2518 err = c->ops->info(kcontrol, uinfo);
2519 kcontrol->private_value = (long)c;
5a9e02e9 2520 mutex_unlock(&codec->control_mutex);
532d5381
TI
2521 return err;
2522}
ff7a3267 2523EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
532d5381 2524
d5191e50
TI
2525/**
2526 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2527 *
2528 * The control element is supposed to have the private_value field
2529 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2530 */
532d5381
TI
2531int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2532 struct snd_ctl_elem_value *ucontrol)
2533{
2534 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2535 struct hda_bind_ctls *c;
2536 int err;
2537
5a9e02e9 2538 mutex_lock(&codec->control_mutex);
14c65f98 2539 c = (struct hda_bind_ctls *)kcontrol->private_value;
532d5381
TI
2540 kcontrol->private_value = *c->values;
2541 err = c->ops->get(kcontrol, ucontrol);
2542 kcontrol->private_value = (long)c;
5a9e02e9 2543 mutex_unlock(&codec->control_mutex);
532d5381
TI
2544 return err;
2545}
ff7a3267 2546EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
532d5381 2547
d5191e50
TI
2548/**
2549 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2550 *
2551 * The control element is supposed to have the private_value field
2552 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2553 */
532d5381
TI
2554int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2555 struct snd_ctl_elem_value *ucontrol)
2556{
2557 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2558 struct hda_bind_ctls *c;
2559 unsigned long *vals;
2560 int err = 0, change = 0;
2561
5a9e02e9 2562 mutex_lock(&codec->control_mutex);
14c65f98 2563 c = (struct hda_bind_ctls *)kcontrol->private_value;
532d5381
TI
2564 for (vals = c->values; *vals; vals++) {
2565 kcontrol->private_value = *vals;
2566 err = c->ops->put(kcontrol, ucontrol);
2567 if (err < 0)
2568 break;
2569 change |= err;
2570 }
2571 kcontrol->private_value = (long)c;
5a9e02e9 2572 mutex_unlock(&codec->control_mutex);
532d5381
TI
2573 return err < 0 ? err : change;
2574}
ff7a3267 2575EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
532d5381 2576
d5191e50
TI
2577/**
2578 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2579 *
2580 * The control element is supposed to have the private_value field
2581 * set up via HDA_BIND_VOL() macro.
2582 */
532d5381
TI
2583int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2584 unsigned int size, unsigned int __user *tlv)
2585{
2586 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2587 struct hda_bind_ctls *c;
2588 int err;
2589
5a9e02e9 2590 mutex_lock(&codec->control_mutex);
14c65f98 2591 c = (struct hda_bind_ctls *)kcontrol->private_value;
532d5381
TI
2592 kcontrol->private_value = *c->values;
2593 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2594 kcontrol->private_value = (long)c;
5a9e02e9 2595 mutex_unlock(&codec->control_mutex);
532d5381
TI
2596 return err;
2597}
ff7a3267 2598EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
532d5381
TI
2599
2600struct hda_ctl_ops snd_hda_bind_vol = {
2601 .info = snd_hda_mixer_amp_volume_info,
2602 .get = snd_hda_mixer_amp_volume_get,
2603 .put = snd_hda_mixer_amp_volume_put,
2604 .tlv = snd_hda_mixer_amp_tlv
2605};
ff7a3267 2606EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
532d5381
TI
2607
2608struct hda_ctl_ops snd_hda_bind_sw = {
2609 .info = snd_hda_mixer_amp_switch_info,
2610 .get = snd_hda_mixer_amp_switch_get,
2611 .put = snd_hda_mixer_amp_switch_put,
2612 .tlv = snd_hda_mixer_amp_tlv
2613};
ff7a3267 2614EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
532d5381 2615
1da177e4
LT
2616/*
2617 * SPDIF out controls
2618 */
2619
0ba21762
TI
2620static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2621 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
2622{
2623 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2624 uinfo->count = 1;
2625 return 0;
2626}
2627
0ba21762
TI
2628static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2629 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
2630{
2631 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2632 IEC958_AES0_NONAUDIO |
2633 IEC958_AES0_CON_EMPHASIS_5015 |
2634 IEC958_AES0_CON_NOT_COPYRIGHT;
2635 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2636 IEC958_AES1_CON_ORIGINAL;
2637 return 0;
2638}
2639
0ba21762
TI
2640static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2641 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
2642{
2643 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2644 IEC958_AES0_NONAUDIO |
2645 IEC958_AES0_PRO_EMPHASIS_5015;
2646 return 0;
2647}
2648
0ba21762
TI
2649static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2650 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
2651{
2652 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
7c935976
SW
2653 int idx = kcontrol->private_value;
2654 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
1da177e4 2655
7c935976
SW
2656 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2657 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2658 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2659 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
1da177e4
LT
2660
2661 return 0;
2662}
2663
2664/* convert from SPDIF status bits to HDA SPDIF bits
2665 * bit 0 (DigEn) is always set zero (to be filled later)
2666 */
2667static unsigned short convert_from_spdif_status(unsigned int sbits)
2668{
2669 unsigned short val = 0;
2670
2671 if (sbits & IEC958_AES0_PROFESSIONAL)
0ba21762 2672 val |= AC_DIG1_PROFESSIONAL;
1da177e4 2673 if (sbits & IEC958_AES0_NONAUDIO)
0ba21762 2674 val |= AC_DIG1_NONAUDIO;
1da177e4 2675 if (sbits & IEC958_AES0_PROFESSIONAL) {
0ba21762
TI
2676 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2677 IEC958_AES0_PRO_EMPHASIS_5015)
2678 val |= AC_DIG1_EMPHASIS;
1da177e4 2679 } else {
0ba21762
TI
2680 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2681 IEC958_AES0_CON_EMPHASIS_5015)
2682 val |= AC_DIG1_EMPHASIS;
2683 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2684 val |= AC_DIG1_COPYRIGHT;
1da177e4 2685 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
0ba21762 2686 val |= AC_DIG1_LEVEL;
1da177e4
LT
2687 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2688 }
2689 return val;
2690}
2691
2692/* convert to SPDIF status bits from HDA SPDIF bits
2693 */
2694static unsigned int convert_to_spdif_status(unsigned short val)
2695{
2696 unsigned int sbits = 0;
2697
0ba21762 2698 if (val & AC_DIG1_NONAUDIO)
1da177e4 2699 sbits |= IEC958_AES0_NONAUDIO;
0ba21762 2700 if (val & AC_DIG1_PROFESSIONAL)
1da177e4
LT
2701 sbits |= IEC958_AES0_PROFESSIONAL;
2702 if (sbits & IEC958_AES0_PROFESSIONAL) {
0ba21762 2703 if (sbits & AC_DIG1_EMPHASIS)
1da177e4
LT
2704 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2705 } else {
0ba21762 2706 if (val & AC_DIG1_EMPHASIS)
1da177e4 2707 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
0ba21762 2708 if (!(val & AC_DIG1_COPYRIGHT))
1da177e4 2709 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
0ba21762 2710 if (val & AC_DIG1_LEVEL)
1da177e4
LT
2711 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2712 sbits |= val & (0x7f << 8);
2713 }
2714 return sbits;
2715}
2716
2f72853c
TI
2717/* set digital convert verbs both for the given NID and its slaves */
2718static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2719 int verb, int val)
2720{
dda14410 2721 const hda_nid_t *d;
2f72853c 2722
9e976976 2723 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
2f72853c
TI
2724 d = codec->slave_dig_outs;
2725 if (!d)
2726 return;
2727 for (; *d; d++)
9e976976 2728 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
2f72853c
TI
2729}
2730
2731static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2732 int dig1, int dig2)
2733{
2734 if (dig1 != -1)
2735 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2736 if (dig2 != -1)
2737 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2738}
2739
0ba21762
TI
2740static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2741 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
2742{
2743 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
7c935976
SW
2744 int idx = kcontrol->private_value;
2745 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2746 hda_nid_t nid = spdif->nid;
1da177e4
LT
2747 unsigned short val;
2748 int change;
2749
62932df8 2750 mutex_lock(&codec->spdif_mutex);
7c935976 2751 spdif->status = ucontrol->value.iec958.status[0] |
1da177e4
LT
2752 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2753 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2754 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
7c935976
SW
2755 val = convert_from_spdif_status(spdif->status);
2756 val |= spdif->ctls & 1;
2757 change = spdif->ctls != val;
2758 spdif->ctls = val;
74b654c9 2759 if (change && nid != (u16)-1)
2f72853c 2760 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
62932df8 2761 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
2762 return change;
2763}
2764
a5ce8890 2765#define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
1da177e4 2766
0ba21762
TI
2767static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2768 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
2769{
2770 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
7c935976
SW
2771 int idx = kcontrol->private_value;
2772 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
1da177e4 2773
7c935976 2774 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
1da177e4
LT
2775 return 0;
2776}
2777
74b654c9
SW
2778static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2779 int dig1, int dig2)
2780{
2781 set_dig_out_convert(codec, nid, dig1, dig2);
2782 /* unmute amp switch (if any) */
2783 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2784 (dig1 & AC_DIG1_ENABLE))
2785 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2786 HDA_AMP_MUTE, 0);
2787}
2788
0ba21762
TI
2789static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2790 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
2791{
2792 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
7c935976
SW
2793 int idx = kcontrol->private_value;
2794 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2795 hda_nid_t nid = spdif->nid;
1da177e4
LT
2796 unsigned short val;
2797 int change;
2798
62932df8 2799 mutex_lock(&codec->spdif_mutex);
7c935976 2800 val = spdif->ctls & ~AC_DIG1_ENABLE;
1da177e4 2801 if (ucontrol->value.integer.value[0])
0ba21762 2802 val |= AC_DIG1_ENABLE;
7c935976 2803 change = spdif->ctls != val;
74b654c9
SW
2804 spdif->ctls = val;
2805 if (change && nid != (u16)-1)
2806 set_spdif_ctls(codec, nid, val & 0xff, -1);
62932df8 2807 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
2808 return change;
2809}
2810
c8b6bf9b 2811static struct snd_kcontrol_new dig_mixes[] = {
1da177e4
LT
2812 {
2813 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2814 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
28aedaf7 2815 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
1da177e4
LT
2816 .info = snd_hda_spdif_mask_info,
2817 .get = snd_hda_spdif_cmask_get,
2818 },
2819 {
2820 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2821 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
28aedaf7 2822 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
1da177e4
LT
2823 .info = snd_hda_spdif_mask_info,
2824 .get = snd_hda_spdif_pmask_get,
2825 },
2826 {
2827 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
28aedaf7 2828 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
1da177e4
LT
2829 .info = snd_hda_spdif_mask_info,
2830 .get = snd_hda_spdif_default_get,
2831 .put = snd_hda_spdif_default_put,
2832 },
2833 {
2834 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
28aedaf7 2835 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
1da177e4
LT
2836 .info = snd_hda_spdif_out_switch_info,
2837 .get = snd_hda_spdif_out_switch_get,
2838 .put = snd_hda_spdif_out_switch_put,
2839 },
2840 { } /* end */
2841};
2842
2843/**
2844 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2845 * @codec: the HDA codec
2846 * @nid: audio out widget NID
2847 *
2848 * Creates controls related with the SPDIF output.
2849 * Called from each patch supporting the SPDIF out.
2850 *
2851 * Returns 0 if successful, or a negative error code.
2852 */
74b654c9
SW
2853int snd_hda_create_spdif_out_ctls(struct hda_codec *codec,
2854 hda_nid_t associated_nid,
2855 hda_nid_t cvt_nid)
1da177e4
LT
2856{
2857 int err;
c8b6bf9b
TI
2858 struct snd_kcontrol *kctl;
2859 struct snd_kcontrol_new *dig_mix;
09f99701 2860 int idx;
7c935976 2861 struct hda_spdif_out *spdif;
1da177e4 2862
1afe206a
TI
2863 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch");
2864 if (idx < 0) {
09f99701
TI
2865 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2866 return -EBUSY;
2867 }
7c935976 2868 spdif = snd_array_new(&codec->spdif_out);
1da177e4
LT
2869 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2870 kctl = snd_ctl_new1(dig_mix, codec);
b91f080f
TI
2871 if (!kctl)
2872 return -ENOMEM;
09f99701 2873 kctl->id.index = idx;
7c935976 2874 kctl->private_value = codec->spdif_out.used - 1;
74b654c9 2875 err = snd_hda_ctl_add(codec, associated_nid, kctl);
0ba21762 2876 if (err < 0)
1da177e4
LT
2877 return err;
2878 }
74b654c9
SW
2879 spdif->nid = cvt_nid;
2880 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
7c935976
SW
2881 AC_VERB_GET_DIGI_CONVERT_1, 0);
2882 spdif->status = convert_to_spdif_status(spdif->ctls);
1da177e4
LT
2883 return 0;
2884}
ff7a3267 2885EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
1da177e4 2886
7c935976
SW
2887struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
2888 hda_nid_t nid)
2889{
2890 int i;
2891 for (i = 0; i < codec->spdif_out.used; i++) {
2892 struct hda_spdif_out *spdif =
2893 snd_array_elem(&codec->spdif_out, i);
2894 if (spdif->nid == nid)
2895 return spdif;
2896 }
2897 return NULL;
2898}
2899EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
2900
74b654c9
SW
2901void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
2902{
2903 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2904
2905 mutex_lock(&codec->spdif_mutex);
2906 spdif->nid = (u16)-1;
2907 mutex_unlock(&codec->spdif_mutex);
2908}
2909EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
2910
2911void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
2912{
2913 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2914 unsigned short val;
2915
2916 mutex_lock(&codec->spdif_mutex);
2917 if (spdif->nid != nid) {
2918 spdif->nid = nid;
2919 val = spdif->ctls;
2920 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
2921 }
2922 mutex_unlock(&codec->spdif_mutex);
2923}
2924EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
2925
9a08160b
TI
2926/*
2927 * SPDIF sharing with analog output
2928 */
2929static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2930 struct snd_ctl_elem_value *ucontrol)
2931{
2932 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2933 ucontrol->value.integer.value[0] = mout->share_spdif;
2934 return 0;
2935}
2936
2937static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2938 struct snd_ctl_elem_value *ucontrol)
2939{
2940 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2941 mout->share_spdif = !!ucontrol->value.integer.value[0];
2942 return 0;
2943}
2944
2945static struct snd_kcontrol_new spdif_share_sw = {
2946 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2947 .name = "IEC958 Default PCM Playback Switch",
2948 .info = snd_ctl_boolean_mono_info,
2949 .get = spdif_share_sw_get,
2950 .put = spdif_share_sw_put,
2951};
2952
d5191e50
TI
2953/**
2954 * snd_hda_create_spdif_share_sw - create Default PCM switch
2955 * @codec: the HDA codec
2956 * @mout: multi-out instance
2957 */
9a08160b
TI
2958int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2959 struct hda_multi_out *mout)
2960{
2961 if (!mout->dig_out_nid)
2962 return 0;
2963 /* ATTENTION: here mout is passed as private_data, instead of codec */
3911a4c1
JK
2964 return snd_hda_ctl_add(codec, mout->dig_out_nid,
2965 snd_ctl_new1(&spdif_share_sw, mout));
9a08160b 2966}
ff7a3267 2967EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
9a08160b 2968
1da177e4
LT
2969/*
2970 * SPDIF input
2971 */
2972
2973#define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2974
0ba21762
TI
2975static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2976 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
2977{
2978 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2979
2980 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2981 return 0;
2982}
2983
0ba21762
TI
2984static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2985 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
2986{
2987 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2988 hda_nid_t nid = kcontrol->private_value;
2989 unsigned int val = !!ucontrol->value.integer.value[0];
2990 int change;
2991
62932df8 2992 mutex_lock(&codec->spdif_mutex);
1da177e4 2993 change = codec->spdif_in_enable != val;
82beb8fd 2994 if (change) {
1da177e4 2995 codec->spdif_in_enable = val;
82beb8fd
TI
2996 snd_hda_codec_write_cache(codec, nid, 0,
2997 AC_VERB_SET_DIGI_CONVERT_1, val);
1da177e4 2998 }
62932df8 2999 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
3000 return change;
3001}
3002
0ba21762
TI
3003static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3004 struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
3005{
3006 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3007 hda_nid_t nid = kcontrol->private_value;
3008 unsigned short val;
3009 unsigned int sbits;
3010
3982d17e 3011 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
1da177e4
LT
3012 sbits = convert_to_spdif_status(val);
3013 ucontrol->value.iec958.status[0] = sbits;
3014 ucontrol->value.iec958.status[1] = sbits >> 8;
3015 ucontrol->value.iec958.status[2] = sbits >> 16;
3016 ucontrol->value.iec958.status[3] = sbits >> 24;
3017 return 0;
3018}
3019
c8b6bf9b 3020static struct snd_kcontrol_new dig_in_ctls[] = {
1da177e4
LT
3021 {
3022 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
28aedaf7 3023 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
1da177e4
LT
3024 .info = snd_hda_spdif_in_switch_info,
3025 .get = snd_hda_spdif_in_switch_get,
3026 .put = snd_hda_spdif_in_switch_put,
3027 },
3028 {
3029 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3030 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
28aedaf7 3031 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
1da177e4
LT
3032 .info = snd_hda_spdif_mask_info,
3033 .get = snd_hda_spdif_in_status_get,
3034 },
3035 { } /* end */
3036};
3037
3038/**
3039 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3040 * @codec: the HDA codec
3041 * @nid: audio in widget NID
3042 *
3043 * Creates controls related with the SPDIF input.
3044 * Called from each patch supporting the SPDIF in.
3045 *
3046 * Returns 0 if successful, or a negative error code.
3047 */
12f288bf 3048int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
1da177e4
LT
3049{
3050 int err;
c8b6bf9b
TI
3051 struct snd_kcontrol *kctl;
3052 struct snd_kcontrol_new *dig_mix;
09f99701 3053 int idx;
1da177e4 3054
1afe206a
TI
3055 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch");
3056 if (idx < 0) {
09f99701
TI
3057 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3058 return -EBUSY;
3059 }
1da177e4
LT
3060 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3061 kctl = snd_ctl_new1(dig_mix, codec);
c8dcdf82
TI
3062 if (!kctl)
3063 return -ENOMEM;
1da177e4 3064 kctl->private_value = nid;
3911a4c1 3065 err = snd_hda_ctl_add(codec, nid, kctl);
0ba21762 3066 if (err < 0)
1da177e4
LT
3067 return err;
3068 }
0ba21762 3069 codec->spdif_in_enable =
3982d17e
AP
3070 snd_hda_codec_read(codec, nid, 0,
3071 AC_VERB_GET_DIGI_CONVERT_1, 0) &
0ba21762 3072 AC_DIG1_ENABLE;
1da177e4
LT
3073 return 0;
3074}
ff7a3267 3075EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
1da177e4 3076
cb53c626 3077#ifdef SND_HDA_NEEDS_RESUME
82beb8fd
TI
3078/*
3079 * command cache
3080 */
1da177e4 3081
b3ac5636
TI
3082/* build a 32bit cache key with the widget id and the command parameter */
3083#define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3084#define get_cmd_cache_nid(key) ((key) & 0xff)
3085#define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3086
3087/**
3088 * snd_hda_codec_write_cache - send a single command with caching
3089 * @codec: the HDA codec
3090 * @nid: NID to send the command
3091 * @direct: direct flag
3092 * @verb: the verb to send
3093 * @parm: the parameter for the verb
3094 *
3095 * Send a single command without waiting for response.
3096 *
3097 * Returns 0 if successful, or a negative error code.
3098 */
3099int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3100 int direct, unsigned int verb, unsigned int parm)
3101{
aa2936f5
TI
3102 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3103 struct hda_cache_head *c;
3104 u32 key;
33fa35ed 3105
aa2936f5
TI
3106 if (err < 0)
3107 return err;
3108 /* parm may contain the verb stuff for get/set amp */
3109 verb = verb | (parm >> 8);
3110 parm &= 0xff;
3111 key = build_cmd_cache_key(nid, verb);
3112 mutex_lock(&codec->bus->cmd_mutex);
3113 c = get_alloc_hash(&codec->cmd_cache, key);
3114 if (c)
3115 c->val = parm;
3116 mutex_unlock(&codec->bus->cmd_mutex);
3117 return 0;
b3ac5636 3118}
ff7a3267 3119EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
b3ac5636 3120
a68d5a54
TI
3121/**
3122 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3123 * @codec: the HDA codec
3124 * @nid: NID to send the command
3125 * @direct: direct flag
3126 * @verb: the verb to send
3127 * @parm: the parameter for the verb
3128 *
3129 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3130 * command if the parameter is already identical with the cached value.
3131 * If not, it sends the command and refreshes the cache.
3132 *
3133 * Returns 0 if successful, or a negative error code.
3134 */
3135int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3136 int direct, unsigned int verb, unsigned int parm)
3137{
3138 struct hda_cache_head *c;
3139 u32 key;
3140
3141 /* parm may contain the verb stuff for get/set amp */
3142 verb = verb | (parm >> 8);
3143 parm &= 0xff;
3144 key = build_cmd_cache_key(nid, verb);
3145 mutex_lock(&codec->bus->cmd_mutex);
3146 c = get_hash(&codec->cmd_cache, key);
3147 if (c && c->val == parm) {
3148 mutex_unlock(&codec->bus->cmd_mutex);
3149 return 0;
3150 }
3151 mutex_unlock(&codec->bus->cmd_mutex);
3152 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3153}
3154EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3155
d5191e50
TI
3156/**
3157 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3158 * @codec: HD-audio codec
3159 *
3160 * Execute all verbs recorded in the command caches to resume.
3161 */
b3ac5636
TI
3162void snd_hda_codec_resume_cache(struct hda_codec *codec)
3163{
603c4019 3164 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
b3ac5636
TI
3165 int i;
3166
603c4019 3167 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
b3ac5636
TI
3168 u32 key = buffer->key;
3169 if (!key)
3170 continue;
3171 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3172 get_cmd_cache_cmd(key), buffer->val);
3173 }
3174}
ff7a3267 3175EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
b3ac5636
TI
3176
3177/**
3178 * snd_hda_sequence_write_cache - sequence writes with caching
3179 * @codec: the HDA codec
3180 * @seq: VERB array to send
3181 *
3182 * Send the commands sequentially from the given array.
3183 * Thte commands are recorded on cache for power-save and resume.
3184 * The array must be terminated with NID=0.
3185 */
3186void snd_hda_sequence_write_cache(struct hda_codec *codec,
3187 const struct hda_verb *seq)
3188{
3189 for (; seq->nid; seq++)
3190 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3191 seq->param);
3192}
ff7a3267 3193EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
cb53c626 3194#endif /* SND_HDA_NEEDS_RESUME */
b3ac5636 3195
54d17403
TI
3196/*
3197 * set power state of the codec
3198 */
3199static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3200 unsigned int power_state)
3201{
cb53c626
TI
3202 hda_nid_t nid;
3203 int i;
54d17403 3204
05ff7e11
TI
3205 /* this delay seems necessary to avoid click noise at power-down */
3206 if (power_state == AC_PWRST_D3)
3207 msleep(100);
3208 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
54d17403 3209 power_state);
05ff7e11 3210 /* partial workaround for "azx_get_response timeout" */
dd2b4a7a
ZR
3211 if (power_state == AC_PWRST_D0 &&
3212 (codec->vendor_id & 0xffff0000) == 0x14f10000)
05ff7e11 3213 msleep(10);
54d17403 3214
cb53c626
TI
3215 nid = codec->start_nid;
3216 for (i = 0; i < codec->num_nodes; i++, nid++) {
7eba5c9d
TI
3217 unsigned int wcaps = get_wcaps(codec, nid);
3218 if (wcaps & AC_WCAP_POWER) {
a22d543a 3219 unsigned int wid_type = get_wcaps_type(wcaps);
a3b48c88
TI
3220 if (power_state == AC_PWRST_D3 &&
3221 wid_type == AC_WID_PIN) {
7eba5c9d
TI
3222 unsigned int pincap;
3223 /*
3224 * don't power down the widget if it controls
3225 * eapd and EAPD_BTLENABLE is set.
3226 */
14bafe32 3227 pincap = snd_hda_query_pin_caps(codec, nid);
7eba5c9d
TI
3228 if (pincap & AC_PINCAP_EAPD) {
3229 int eapd = snd_hda_codec_read(codec,
3230 nid, 0,
3231 AC_VERB_GET_EAPD_BTLENABLE, 0);
3232 eapd &= 0x02;
a3b48c88 3233 if (eapd)
7eba5c9d
TI
3234 continue;
3235 }
1194b5b7 3236 }
54d17403
TI
3237 snd_hda_codec_write(codec, nid, 0,
3238 AC_VERB_SET_POWER_STATE,
3239 power_state);
1194b5b7 3240 }
54d17403
TI
3241 }
3242
cb53c626
TI
3243 if (power_state == AC_PWRST_D0) {
3244 unsigned long end_time;
3245 int state;
cb53c626
TI
3246 /* wait until the codec reachs to D0 */
3247 end_time = jiffies + msecs_to_jiffies(500);
3248 do {
3249 state = snd_hda_codec_read(codec, fg, 0,
3250 AC_VERB_GET_POWER_STATE, 0);
3251 if (state == power_state)
3252 break;
3253 msleep(1);
3254 } while (time_after_eq(end_time, jiffies));
3255 }
3256}
3257
11aeff08
TI
3258#ifdef CONFIG_SND_HDA_HWDEP
3259/* execute additional init verbs */
3260static void hda_exec_init_verbs(struct hda_codec *codec)
3261{
3262 if (codec->init_verbs.list)
3263 snd_hda_sequence_write(codec, codec->init_verbs.list);
3264}
3265#else
3266static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3267#endif
3268
cb53c626
TI
3269#ifdef SND_HDA_NEEDS_RESUME
3270/*
3271 * call suspend and power-down; used both from PM and power-save
3272 */
3273static void hda_call_codec_suspend(struct hda_codec *codec)
3274{
3275 if (codec->patch_ops.suspend)
3276 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
eb541337 3277 hda_cleanup_all_streams(codec);
cb53c626
TI
3278 hda_set_power_state(codec,
3279 codec->afg ? codec->afg : codec->mfg,
3280 AC_PWRST_D3);
3281#ifdef CONFIG_SND_HDA_POWER_SAVE
a2f6309e 3282 snd_hda_update_power_acct(codec);
cb53c626 3283 cancel_delayed_work(&codec->power_work);
95e99fda 3284 codec->power_on = 0;
a221e287 3285 codec->power_transition = 0;
a2f6309e 3286 codec->power_jiffies = jiffies;
cb53c626 3287#endif
54d17403
TI
3288}
3289
cb53c626
TI
3290/*
3291 * kick up codec; used both from PM and power-save
3292 */
3293static void hda_call_codec_resume(struct hda_codec *codec)
3294{
3295 hda_set_power_state(codec,
3296 codec->afg ? codec->afg : codec->mfg,
3297 AC_PWRST_D0);
3be14149 3298 restore_pincfgs(codec); /* restore all current pin configs */
ac0547dc 3299 restore_shutup_pins(codec);
11aeff08 3300 hda_exec_init_verbs(codec);
cb53c626
TI
3301 if (codec->patch_ops.resume)
3302 codec->patch_ops.resume(codec);
3303 else {
9d99f312
TI
3304 if (codec->patch_ops.init)
3305 codec->patch_ops.init(codec);
cb53c626
TI
3306 snd_hda_codec_resume_amp(codec);
3307 snd_hda_codec_resume_cache(codec);
3308 }
3309}
3310#endif /* SND_HDA_NEEDS_RESUME */
3311
54d17403 3312
1da177e4
LT
3313/**
3314 * snd_hda_build_controls - build mixer controls
3315 * @bus: the BUS
3316 *
3317 * Creates mixer controls for each codec included in the bus.
3318 *
3319 * Returns 0 if successful, otherwise a negative error code.
3320 */
1289e9e8 3321int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
1da177e4 3322{
0ba21762 3323 struct hda_codec *codec;
1da177e4 3324
0ba21762 3325 list_for_each_entry(codec, &bus->codec_list, list) {
6c1f45ea 3326 int err = snd_hda_codec_build_controls(codec);
f93d461b 3327 if (err < 0) {
28d1a85e 3328 printk(KERN_ERR "hda_codec: cannot build controls "
28aedaf7 3329 "for #%d (error %d)\n", codec->addr, err);
f93d461b
TI
3330 err = snd_hda_codec_reset(codec);
3331 if (err < 0) {
3332 printk(KERN_ERR
3333 "hda_codec: cannot revert codec\n");
3334 return err;
3335 }
3336 }
1da177e4 3337 }
6c1f45ea
TI
3338 return 0;
3339}
ff7a3267 3340EXPORT_SYMBOL_HDA(snd_hda_build_controls);
cb53c626 3341
6c1f45ea
TI
3342int snd_hda_codec_build_controls(struct hda_codec *codec)
3343{
3344 int err = 0;
11aeff08 3345 hda_exec_init_verbs(codec);
6c1f45ea
TI
3346 /* continue to initialize... */
3347 if (codec->patch_ops.init)
3348 err = codec->patch_ops.init(codec);
3349 if (!err && codec->patch_ops.build_controls)
3350 err = codec->patch_ops.build_controls(codec);
6c1f45ea
TI
3351 if (err < 0)
3352 return err;
1da177e4
LT
3353 return 0;
3354}
3355
1da177e4
LT
3356/*
3357 * stream formats
3358 */
befdf316
TI
3359struct hda_rate_tbl {
3360 unsigned int hz;
3361 unsigned int alsa_bits;
3362 unsigned int hda_fmt;
3363};
3364
92f10b3f
TI
3365/* rate = base * mult / div */
3366#define HDA_RATE(base, mult, div) \
3367 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3368 (((div) - 1) << AC_FMT_DIV_SHIFT))
3369
befdf316 3370static struct hda_rate_tbl rate_bits[] = {
1da177e4 3371 /* rate in Hz, ALSA rate bitmask, HDA format value */
9d8f53f2
NG
3372
3373 /* autodetected value used in snd_hda_query_supported_pcm */
92f10b3f
TI
3374 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3375 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3376 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3377 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3378 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3379 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3380 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3381 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3382 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3383 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3384 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
a961f9fe
TI
3385#define AC_PAR_PCM_RATE_BITS 11
3386 /* up to bits 10, 384kHZ isn't supported properly */
3387
3388 /* not autodetected value */
92f10b3f 3389 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
9d8f53f2 3390
befdf316 3391 { 0 } /* terminator */
1da177e4
LT
3392};
3393
3394/**
3395 * snd_hda_calc_stream_format - calculate format bitset
3396 * @rate: the sample rate
3397 * @channels: the number of channels
3398 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3399 * @maxbps: the max. bps
3400 *
3401 * Calculate the format bitset from the given rate, channels and th PCM format.
3402 *
3403 * Return zero if invalid.
3404 */
3405unsigned int snd_hda_calc_stream_format(unsigned int rate,
3406 unsigned int channels,
3407 unsigned int format,
32c168c8
AH
3408 unsigned int maxbps,
3409 unsigned short spdif_ctls)
1da177e4
LT
3410{
3411 int i;
3412 unsigned int val = 0;
3413
befdf316
TI
3414 for (i = 0; rate_bits[i].hz; i++)
3415 if (rate_bits[i].hz == rate) {
3416 val = rate_bits[i].hda_fmt;
1da177e4
LT
3417 break;
3418 }
0ba21762 3419 if (!rate_bits[i].hz) {
1da177e4
LT
3420 snd_printdd("invalid rate %d\n", rate);
3421 return 0;
3422 }
3423
3424 if (channels == 0 || channels > 8) {
3425 snd_printdd("invalid channels %d\n", channels);
3426 return 0;
3427 }
3428 val |= channels - 1;
3429
3430 switch (snd_pcm_format_width(format)) {
28aedaf7 3431 case 8:
92f10b3f 3432 val |= AC_FMT_BITS_8;
28aedaf7
NL
3433 break;
3434 case 16:
92f10b3f 3435 val |= AC_FMT_BITS_16;
28aedaf7 3436 break;
1da177e4
LT
3437 case 20:
3438 case 24:
3439 case 32:
b0bb3aa6 3440 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
92f10b3f 3441 val |= AC_FMT_BITS_32;
1da177e4 3442 else if (maxbps >= 24)
92f10b3f 3443 val |= AC_FMT_BITS_24;
1da177e4 3444 else
92f10b3f 3445 val |= AC_FMT_BITS_20;
1da177e4
LT
3446 break;
3447 default:
0ba21762
TI
3448 snd_printdd("invalid format width %d\n",
3449 snd_pcm_format_width(format));
1da177e4
LT
3450 return 0;
3451 }
3452
32c168c8 3453 if (spdif_ctls & AC_DIG1_NONAUDIO)
92f10b3f 3454 val |= AC_FMT_TYPE_NON_PCM;
32c168c8 3455
1da177e4
LT
3456 return val;
3457}
ff7a3267 3458EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
1da177e4 3459
92c7c8a7
TI
3460static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3461{
3462 unsigned int val = 0;
3463 if (nid != codec->afg &&
3464 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3465 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3466 if (!val || val == -1)
3467 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3468 if (!val || val == -1)
3469 return 0;
3470 return val;
3471}
3472
3473static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3474{
3475 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3476 get_pcm_param);
3477}
3478
3479static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3480{
3481 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3482 if (!streams || streams == -1)
3483 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3484 if (!streams || streams == -1)
3485 return 0;
3486 return streams;
3487}
3488
3489static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3490{
3491 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3492 get_stream_param);
3493}
3494
1da177e4
LT
3495/**
3496 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3497 * @codec: the HDA codec
3498 * @nid: NID to query
3499 * @ratesp: the pointer to store the detected rate bitflags
3500 * @formatsp: the pointer to store the detected formats
3501 * @bpsp: the pointer to store the detected format widths
3502 *
3503 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3504 * or @bsps argument is ignored.
3505 *
3506 * Returns 0 if successful, otherwise a negative error code.
3507 */
384a48d7 3508int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
1da177e4
LT
3509 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3510{
ee504710 3511 unsigned int i, val, wcaps;
1da177e4 3512
ee504710 3513 wcaps = get_wcaps(codec, nid);
92c7c8a7 3514 val = query_pcm_param(codec, nid);
1da177e4
LT
3515
3516 if (ratesp) {
3517 u32 rates = 0;
a961f9fe 3518 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
1da177e4 3519 if (val & (1 << i))
befdf316 3520 rates |= rate_bits[i].alsa_bits;
1da177e4 3521 }
ee504710
JK
3522 if (rates == 0) {
3523 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3524 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3525 nid, val,
3526 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3527 return -EIO;
3528 }
1da177e4
LT
3529 *ratesp = rates;
3530 }
3531
3532 if (formatsp || bpsp) {
3533 u64 formats = 0;
ee504710 3534 unsigned int streams, bps;
1da177e4 3535
92c7c8a7
TI
3536 streams = query_stream_param(codec, nid);
3537 if (!streams)
1da177e4 3538 return -EIO;
1da177e4
LT
3539
3540 bps = 0;
3541 if (streams & AC_SUPFMT_PCM) {
3542 if (val & AC_SUPPCM_BITS_8) {
3543 formats |= SNDRV_PCM_FMTBIT_U8;
3544 bps = 8;
3545 }
3546 if (val & AC_SUPPCM_BITS_16) {
3547 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3548 bps = 16;
3549 }
3550 if (wcaps & AC_WCAP_DIGITAL) {
3551 if (val & AC_SUPPCM_BITS_32)
3552 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3553 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3554 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3555 if (val & AC_SUPPCM_BITS_24)
3556 bps = 24;
3557 else if (val & AC_SUPPCM_BITS_20)
3558 bps = 20;
0ba21762
TI
3559 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3560 AC_SUPPCM_BITS_32)) {
1da177e4
LT
3561 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3562 if (val & AC_SUPPCM_BITS_32)
3563 bps = 32;
1da177e4
LT
3564 else if (val & AC_SUPPCM_BITS_24)
3565 bps = 24;
33ef7651
NG
3566 else if (val & AC_SUPPCM_BITS_20)
3567 bps = 20;
1da177e4
LT
3568 }
3569 }
b5025c50 3570 if (streams & AC_SUPFMT_FLOAT32) {
1da177e4 3571 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
b0bb3aa6
TI
3572 if (!bps)
3573 bps = 32;
b5025c50
TI
3574 }
3575 if (streams == AC_SUPFMT_AC3) {
0ba21762 3576 /* should be exclusive */
1da177e4
LT
3577 /* temporary hack: we have still no proper support
3578 * for the direct AC3 stream...
3579 */
3580 formats |= SNDRV_PCM_FMTBIT_U8;
3581 bps = 8;
3582 }
ee504710
JK
3583 if (formats == 0) {
3584 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3585 "(nid=0x%x, val=0x%x, ovrd=%i, "
3586 "streams=0x%x)\n",
3587 nid, val,
3588 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3589 streams);
3590 return -EIO;
3591 }
1da177e4
LT
3592 if (formatsp)
3593 *formatsp = formats;
3594 if (bpsp)
3595 *bpsp = bps;
3596 }
3597
3598 return 0;
3599}
384a48d7 3600EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
1da177e4
LT
3601
3602/**
d5191e50
TI
3603 * snd_hda_is_supported_format - Check the validity of the format
3604 * @codec: HD-audio codec
3605 * @nid: NID to check
3606 * @format: the HD-audio format value to check
3607 *
3608 * Check whether the given node supports the format value.
1da177e4
LT
3609 *
3610 * Returns 1 if supported, 0 if not.
3611 */
3612int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3613 unsigned int format)
3614{
3615 int i;
3616 unsigned int val = 0, rate, stream;
3617
92c7c8a7
TI
3618 val = query_pcm_param(codec, nid);
3619 if (!val)
3620 return 0;
1da177e4
LT
3621
3622 rate = format & 0xff00;
a961f9fe 3623 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
befdf316 3624 if (rate_bits[i].hda_fmt == rate) {
1da177e4
LT
3625 if (val & (1 << i))
3626 break;
3627 return 0;
3628 }
a961f9fe 3629 if (i >= AC_PAR_PCM_RATE_BITS)
1da177e4
LT
3630 return 0;
3631
92c7c8a7
TI
3632 stream = query_stream_param(codec, nid);
3633 if (!stream)
1da177e4
LT
3634 return 0;
3635
3636 if (stream & AC_SUPFMT_PCM) {
3637 switch (format & 0xf0) {
3638 case 0x00:
0ba21762 3639 if (!(val & AC_SUPPCM_BITS_8))
1da177e4
LT
3640 return 0;
3641 break;
3642 case 0x10:
0ba21762 3643 if (!(val & AC_SUPPCM_BITS_16))
1da177e4
LT
3644 return 0;
3645 break;
3646 case 0x20:
0ba21762 3647 if (!(val & AC_SUPPCM_BITS_20))
1da177e4
LT
3648 return 0;
3649 break;
3650 case 0x30:
0ba21762 3651 if (!(val & AC_SUPPCM_BITS_24))
1da177e4
LT
3652 return 0;
3653 break;
3654 case 0x40:
0ba21762 3655 if (!(val & AC_SUPPCM_BITS_32))
1da177e4
LT
3656 return 0;
3657 break;
3658 default:
3659 return 0;
3660 }
3661 } else {
3662 /* FIXME: check for float32 and AC3? */
3663 }
3664
3665 return 1;
3666}
ff7a3267 3667EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
1da177e4
LT
3668
3669/*
3670 * PCM stuff
3671 */
3672static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3673 struct hda_codec *codec,
c8b6bf9b 3674 struct snd_pcm_substream *substream)
1da177e4
LT
3675{
3676 return 0;
3677}
3678
3679static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3680 struct hda_codec *codec,
3681 unsigned int stream_tag,
3682 unsigned int format,
c8b6bf9b 3683 struct snd_pcm_substream *substream)
1da177e4
LT
3684{
3685 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3686 return 0;
3687}
3688
3689static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3690 struct hda_codec *codec,
c8b6bf9b 3691 struct snd_pcm_substream *substream)
1da177e4 3692{
888afa15 3693 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1da177e4
LT
3694 return 0;
3695}
3696
6c1f45ea
TI
3697static int set_pcm_default_values(struct hda_codec *codec,
3698 struct hda_pcm_stream *info)
1da177e4 3699{
ee504710
JK
3700 int err;
3701
0ba21762
TI
3702 /* query support PCM information from the given NID */
3703 if (info->nid && (!info->rates || !info->formats)) {
ee504710 3704 err = snd_hda_query_supported_pcm(codec, info->nid,
0ba21762
TI
3705 info->rates ? NULL : &info->rates,
3706 info->formats ? NULL : &info->formats,
3707 info->maxbps ? NULL : &info->maxbps);
ee504710
JK
3708 if (err < 0)
3709 return err;
1da177e4
LT
3710 }
3711 if (info->ops.open == NULL)
3712 info->ops.open = hda_pcm_default_open_close;
3713 if (info->ops.close == NULL)
3714 info->ops.close = hda_pcm_default_open_close;
3715 if (info->ops.prepare == NULL) {
da3cec35
TI
3716 if (snd_BUG_ON(!info->nid))
3717 return -EINVAL;
1da177e4
LT
3718 info->ops.prepare = hda_pcm_default_prepare;
3719 }
1da177e4 3720 if (info->ops.cleanup == NULL) {
da3cec35
TI
3721 if (snd_BUG_ON(!info->nid))
3722 return -EINVAL;
1da177e4
LT
3723 info->ops.cleanup = hda_pcm_default_cleanup;
3724 }
3725 return 0;
3726}
3727
eb541337
TI
3728/*
3729 * codec prepare/cleanup entries
3730 */
3731int snd_hda_codec_prepare(struct hda_codec *codec,
3732 struct hda_pcm_stream *hinfo,
3733 unsigned int stream,
3734 unsigned int format,
3735 struct snd_pcm_substream *substream)
3736{
3737 int ret;
3f50ac6a 3738 mutex_lock(&codec->bus->prepare_mutex);
eb541337
TI
3739 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
3740 if (ret >= 0)
3741 purify_inactive_streams(codec);
3f50ac6a 3742 mutex_unlock(&codec->bus->prepare_mutex);
eb541337
TI
3743 return ret;
3744}
3745EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
3746
3747void snd_hda_codec_cleanup(struct hda_codec *codec,
3748 struct hda_pcm_stream *hinfo,
3749 struct snd_pcm_substream *substream)
3750{
3f50ac6a 3751 mutex_lock(&codec->bus->prepare_mutex);
eb541337 3752 hinfo->ops.cleanup(hinfo, codec, substream);
3f50ac6a 3753 mutex_unlock(&codec->bus->prepare_mutex);
eb541337
TI
3754}
3755EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
3756
d5191e50 3757/* global */
e3303235
JK
3758const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3759 "Audio", "SPDIF", "HDMI", "Modem"
3760};
3761
529bd6c4
TI
3762/*
3763 * get the empty PCM device number to assign
c8936222
TI
3764 *
3765 * note the max device number is limited by HDA_MAX_PCMS, currently 10
529bd6c4
TI
3766 */
3767static int get_empty_pcm_device(struct hda_bus *bus, int type)
3768{
f5d6def5
WF
3769 /* audio device indices; not linear to keep compatibility */
3770 static int audio_idx[HDA_PCM_NTYPES][5] = {
3771 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3772 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
92608bad 3773 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
f5d6def5 3774 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
529bd6c4 3775 };
f5d6def5
WF
3776 int i;
3777
3778 if (type >= HDA_PCM_NTYPES) {
529bd6c4
TI
3779 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3780 return -EINVAL;
3781 }
f5d6def5
WF
3782
3783 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3784 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3785 return audio_idx[type][i];
3786
28aedaf7
NL
3787 snd_printk(KERN_WARNING "Too many %s devices\n",
3788 snd_hda_pcm_type_name[type]);
f5d6def5 3789 return -EAGAIN;
529bd6c4
TI
3790}
3791
176d5335
TI
3792/*
3793 * attach a new PCM stream
3794 */
529bd6c4 3795static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
176d5335 3796{
33fa35ed 3797 struct hda_bus *bus = codec->bus;
176d5335
TI
3798 struct hda_pcm_stream *info;
3799 int stream, err;
3800
b91f080f 3801 if (snd_BUG_ON(!pcm->name))
176d5335
TI
3802 return -EINVAL;
3803 for (stream = 0; stream < 2; stream++) {
3804 info = &pcm->stream[stream];
3805 if (info->substreams) {
3806 err = set_pcm_default_values(codec, info);
3807 if (err < 0)
3808 return err;
3809 }
3810 }
33fa35ed 3811 return bus->ops.attach_pcm(bus, codec, pcm);
176d5335
TI
3812}
3813
529bd6c4
TI
3814/* assign all PCMs of the given codec */
3815int snd_hda_codec_build_pcms(struct hda_codec *codec)
3816{
3817 unsigned int pcm;
3818 int err;
3819
3820 if (!codec->num_pcms) {
3821 if (!codec->patch_ops.build_pcms)
3822 return 0;
3823 err = codec->patch_ops.build_pcms(codec);
6e655bf2
TI
3824 if (err < 0) {
3825 printk(KERN_ERR "hda_codec: cannot build PCMs"
28aedaf7 3826 "for #%d (error %d)\n", codec->addr, err);
6e655bf2
TI
3827 err = snd_hda_codec_reset(codec);
3828 if (err < 0) {
3829 printk(KERN_ERR
3830 "hda_codec: cannot revert codec\n");
3831 return err;
3832 }
3833 }
529bd6c4
TI
3834 }
3835 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
3836 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
3837 int dev;
3838
3839 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
41b5b01a 3840 continue; /* no substreams assigned */
529bd6c4
TI
3841
3842 if (!cpcm->pcm) {
3843 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
3844 if (dev < 0)
6e655bf2 3845 continue; /* no fatal error */
529bd6c4
TI
3846 cpcm->device = dev;
3847 err = snd_hda_attach_pcm(codec, cpcm);
6e655bf2
TI
3848 if (err < 0) {
3849 printk(KERN_ERR "hda_codec: cannot attach "
3850 "PCM stream %d for codec #%d\n",
3851 dev, codec->addr);
3852 continue; /* no fatal error */
3853 }
529bd6c4
TI
3854 }
3855 }
3856 return 0;
3857}
3858
1da177e4
LT
3859/**
3860 * snd_hda_build_pcms - build PCM information
3861 * @bus: the BUS
3862 *
3863 * Create PCM information for each codec included in the bus.
3864 *
3865 * The build_pcms codec patch is requested to set up codec->num_pcms and
3866 * codec->pcm_info properly. The array is referred by the top-level driver
3867 * to create its PCM instances.
3868 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3869 * callback.
3870 *
3871 * At least, substreams, channels_min and channels_max must be filled for
3872 * each stream. substreams = 0 indicates that the stream doesn't exist.
3873 * When rates and/or formats are zero, the supported values are queried
3874 * from the given nid. The nid is used also by the default ops.prepare
3875 * and ops.cleanup callbacks.
3876 *
3877 * The driver needs to call ops.open in its open callback. Similarly,
3878 * ops.close is supposed to be called in the close callback.
3879 * ops.prepare should be called in the prepare or hw_params callback
3880 * with the proper parameters for set up.
3881 * ops.cleanup should be called in hw_free for clean up of streams.
3882 *
25985edc 3883 * This function returns 0 if successful, or a negative error code.
1da177e4 3884 */
529bd6c4 3885int __devinit snd_hda_build_pcms(struct hda_bus *bus)
1da177e4 3886{
0ba21762 3887 struct hda_codec *codec;
1da177e4 3888
0ba21762 3889 list_for_each_entry(codec, &bus->codec_list, list) {
529bd6c4
TI
3890 int err = snd_hda_codec_build_pcms(codec);
3891 if (err < 0)
3892 return err;
1da177e4
LT
3893 }
3894 return 0;
3895}
ff7a3267 3896EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
1da177e4 3897
1da177e4
LT
3898/**
3899 * snd_hda_check_board_config - compare the current codec with the config table
3900 * @codec: the HDA codec
f5fcc13c
TI
3901 * @num_configs: number of config enums
3902 * @models: array of model name strings
1da177e4
LT
3903 * @tbl: configuration table, terminated by null entries
3904 *
3905 * Compares the modelname or PCI subsystem id of the current codec with the
3906 * given configuration table. If a matching entry is found, returns its
3907 * config value (supposed to be 0 or positive).
3908 *
3909 * If no entries are matching, the function returns a negative value.
3910 */
12f288bf 3911int snd_hda_check_board_config(struct hda_codec *codec,
ea734963 3912 int num_configs, const char * const *models,
12f288bf 3913 const struct snd_pci_quirk *tbl)
1da177e4 3914{
f44ac837 3915 if (codec->modelname && models) {
f5fcc13c
TI
3916 int i;
3917 for (i = 0; i < num_configs; i++) {
3918 if (models[i] &&
f44ac837 3919 !strcmp(codec->modelname, models[i])) {
f5fcc13c
TI
3920 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3921 "selected\n", models[i]);
3922 return i;
1da177e4
LT
3923 }
3924 }
3925 }
3926
f5fcc13c
TI
3927 if (!codec->bus->pci || !tbl)
3928 return -1;
3929
3930 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3931 if (!tbl)
3932 return -1;
3933 if (tbl->value >= 0 && tbl->value < num_configs) {
62cf872a 3934#ifdef CONFIG_SND_DEBUG_VERBOSE
f5fcc13c
TI
3935 char tmp[10];
3936 const char *model = NULL;
3937 if (models)
3938 model = models[tbl->value];
3939 if (!model) {
3940 sprintf(tmp, "#%d", tbl->value);
3941 model = tmp;
1da177e4 3942 }
f5fcc13c
TI
3943 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3944 "for config %x:%x (%s)\n",
3945 model, tbl->subvendor, tbl->subdevice,
3946 (tbl->name ? tbl->name : "Unknown device"));
3947#endif
3948 return tbl->value;
1da177e4
LT
3949 }
3950 return -1;
3951}
ff7a3267 3952EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
1da177e4 3953
2eda3445
MCC
3954/**
3955 * snd_hda_check_board_codec_sid_config - compare the current codec
28aedaf7
NL
3956 subsystem ID with the
3957 config table
2eda3445
MCC
3958
3959 This is important for Gateway notebooks with SB450 HDA Audio
3960 where the vendor ID of the PCI device is:
3961 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3962 and the vendor/subvendor are found only at the codec.
3963
3964 * @codec: the HDA codec
3965 * @num_configs: number of config enums
3966 * @models: array of model name strings
3967 * @tbl: configuration table, terminated by null entries
3968 *
3969 * Compares the modelname or PCI subsystem id of the current codec with the
3970 * given configuration table. If a matching entry is found, returns its
3971 * config value (supposed to be 0 or positive).
3972 *
3973 * If no entries are matching, the function returns a negative value.
3974 */
3975int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
ea734963 3976 int num_configs, const char * const *models,
2eda3445
MCC
3977 const struct snd_pci_quirk *tbl)
3978{
3979 const struct snd_pci_quirk *q;
3980
3981 /* Search for codec ID */
3982 for (q = tbl; q->subvendor; q++) {
3983 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
3984
3985 if (vendorid == codec->subsystem_id)
3986 break;
3987 }
3988
3989 if (!q->subvendor)
3990 return -1;
3991
3992 tbl = q;
3993
3994 if (tbl->value >= 0 && tbl->value < num_configs) {
d94ff6b7 3995#ifdef CONFIG_SND_DEBUG_VERBOSE
2eda3445
MCC
3996 char tmp[10];
3997 const char *model = NULL;
3998 if (models)
3999 model = models[tbl->value];
4000 if (!model) {
4001 sprintf(tmp, "#%d", tbl->value);
4002 model = tmp;
4003 }
4004 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4005 "for config %x:%x (%s)\n",
4006 model, tbl->subvendor, tbl->subdevice,
4007 (tbl->name ? tbl->name : "Unknown device"));
4008#endif
4009 return tbl->value;
4010 }
4011 return -1;
4012}
4013EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4014
1da177e4
LT
4015/**
4016 * snd_hda_add_new_ctls - create controls from the array
4017 * @codec: the HDA codec
c8b6bf9b 4018 * @knew: the array of struct snd_kcontrol_new
1da177e4
LT
4019 *
4020 * This helper function creates and add new controls in the given array.
4021 * The array must be terminated with an empty entry as terminator.
4022 *
4023 * Returns 0 if successful, or a negative error code.
4024 */
031024ee
TI
4025int snd_hda_add_new_ctls(struct hda_codec *codec,
4026 const struct snd_kcontrol_new *knew)
1da177e4 4027{
4d02d1b6 4028 int err;
1da177e4
LT
4029
4030 for (; knew->name; knew++) {
54d17403 4031 struct snd_kcontrol *kctl;
1afe206a 4032 int addr = 0, idx = 0;
5b0cb1d8
JK
4033 if (knew->iface == -1) /* skip this codec private value */
4034 continue;
1afe206a 4035 for (;;) {
54d17403 4036 kctl = snd_ctl_new1(knew, codec);
0ba21762 4037 if (!kctl)
54d17403 4038 return -ENOMEM;
1afe206a
TI
4039 if (addr > 0)
4040 kctl->id.device = addr;
4041 if (idx > 0)
4042 kctl->id.index = idx;
3911a4c1 4043 err = snd_hda_ctl_add(codec, 0, kctl);
1afe206a
TI
4044 if (!err)
4045 break;
4046 /* try first with another device index corresponding to
4047 * the codec addr; if it still fails (or it's the
4048 * primary codec), then try another control index
4049 */
4050 if (!addr && codec->addr)
4051 addr = codec->addr;
4052 else if (!idx && !knew->index) {
4053 idx = find_empty_mixer_ctl_idx(codec,
4054 knew->name);
4055 if (idx <= 0)
4056 return err;
4057 } else
54d17403
TI
4058 return err;
4059 }
1da177e4
LT
4060 }
4061 return 0;
4062}
ff7a3267 4063EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
1da177e4 4064
cb53c626
TI
4065#ifdef CONFIG_SND_HDA_POWER_SAVE
4066static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
4067 unsigned int power_state);
4068
4069static void hda_power_work(struct work_struct *work)
4070{
4071 struct hda_codec *codec =
4072 container_of(work, struct hda_codec, power_work.work);
33fa35ed 4073 struct hda_bus *bus = codec->bus;
cb53c626 4074
2e492462
ML
4075 if (!codec->power_on || codec->power_count) {
4076 codec->power_transition = 0;
cb53c626 4077 return;
2e492462 4078 }
cb53c626
TI
4079
4080 hda_call_codec_suspend(codec);
33fa35ed
TI
4081 if (bus->ops.pm_notify)
4082 bus->ops.pm_notify(bus);
cb53c626
TI
4083}
4084
4085static void hda_keep_power_on(struct hda_codec *codec)
4086{
4087 codec->power_count++;
4088 codec->power_on = 1;
a2f6309e
TI
4089 codec->power_jiffies = jiffies;
4090}
4091
d5191e50 4092/* update the power on/off account with the current jiffies */
a2f6309e
TI
4093void snd_hda_update_power_acct(struct hda_codec *codec)
4094{
4095 unsigned long delta = jiffies - codec->power_jiffies;
4096 if (codec->power_on)
4097 codec->power_on_acct += delta;
4098 else
4099 codec->power_off_acct += delta;
4100 codec->power_jiffies += delta;
cb53c626
TI
4101}
4102
d5191e50
TI
4103/**
4104 * snd_hda_power_up - Power-up the codec
4105 * @codec: HD-audio codec
4106 *
4107 * Increment the power-up counter and power up the hardware really when
4108 * not turned on yet.
28aedaf7 4109 */
cb53c626
TI
4110void snd_hda_power_up(struct hda_codec *codec)
4111{
33fa35ed
TI
4112 struct hda_bus *bus = codec->bus;
4113
cb53c626 4114 codec->power_count++;
a221e287 4115 if (codec->power_on || codec->power_transition)
cb53c626
TI
4116 return;
4117
a2f6309e 4118 snd_hda_update_power_acct(codec);
cb53c626 4119 codec->power_on = 1;
a2f6309e 4120 codec->power_jiffies = jiffies;
33fa35ed
TI
4121 if (bus->ops.pm_notify)
4122 bus->ops.pm_notify(bus);
cb53c626
TI
4123 hda_call_codec_resume(codec);
4124 cancel_delayed_work(&codec->power_work);
a221e287 4125 codec->power_transition = 0;
cb53c626 4126}
ff7a3267 4127EXPORT_SYMBOL_HDA(snd_hda_power_up);
1289e9e8
TI
4128
4129#define power_save(codec) \
4130 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
cb53c626 4131
d5191e50
TI
4132/**
4133 * snd_hda_power_down - Power-down the codec
4134 * @codec: HD-audio codec
4135 *
4136 * Decrement the power-up counter and schedules the power-off work if
4137 * the counter rearches to zero.
28aedaf7 4138 */
cb53c626
TI
4139void snd_hda_power_down(struct hda_codec *codec)
4140{
4141 --codec->power_count;
a221e287 4142 if (!codec->power_on || codec->power_count || codec->power_transition)
cb53c626 4143 return;
fee2fba3 4144 if (power_save(codec)) {
a221e287 4145 codec->power_transition = 1; /* avoid reentrance */
c107b41c 4146 queue_delayed_work(codec->bus->workq, &codec->power_work,
fee2fba3 4147 msecs_to_jiffies(power_save(codec) * 1000));
a221e287 4148 }
cb53c626 4149}
ff7a3267 4150EXPORT_SYMBOL_HDA(snd_hda_power_down);
cb53c626 4151
d5191e50
TI
4152/**
4153 * snd_hda_check_amp_list_power - Check the amp list and update the power
4154 * @codec: HD-audio codec
4155 * @check: the object containing an AMP list and the status
4156 * @nid: NID to check / update
4157 *
4158 * Check whether the given NID is in the amp list. If it's in the list,
4159 * check the current AMP status, and update the the power-status according
4160 * to the mute status.
4161 *
4162 * This function is supposed to be set or called from the check_power_status
4163 * patch ops.
28aedaf7 4164 */
cb53c626
TI
4165int snd_hda_check_amp_list_power(struct hda_codec *codec,
4166 struct hda_loopback_check *check,
4167 hda_nid_t nid)
4168{
031024ee 4169 const struct hda_amp_list *p;
cb53c626
TI
4170 int ch, v;
4171
4172 if (!check->amplist)
4173 return 0;
4174 for (p = check->amplist; p->nid; p++) {
4175 if (p->nid == nid)
4176 break;
4177 }
4178 if (!p->nid)
4179 return 0; /* nothing changed */
4180
4181 for (p = check->amplist; p->nid; p++) {
4182 for (ch = 0; ch < 2; ch++) {
4183 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4184 p->idx);
4185 if (!(v & HDA_AMP_MUTE) && v > 0) {
4186 if (!check->power_on) {
4187 check->power_on = 1;
4188 snd_hda_power_up(codec);
4189 }
4190 return 1;
4191 }
4192 }
4193 }
4194 if (check->power_on) {
4195 check->power_on = 0;
4196 snd_hda_power_down(codec);
4197 }
4198 return 0;
4199}
ff7a3267 4200EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
cb53c626 4201#endif
1da177e4 4202
c8b6bf9b 4203/*
d2a6d7dc
TI
4204 * Channel mode helper
4205 */
d5191e50
TI
4206
4207/**
4208 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4209 */
0ba21762
TI
4210int snd_hda_ch_mode_info(struct hda_codec *codec,
4211 struct snd_ctl_elem_info *uinfo,
4212 const struct hda_channel_mode *chmode,
4213 int num_chmodes)
d2a6d7dc
TI
4214{
4215 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4216 uinfo->count = 1;
4217 uinfo->value.enumerated.items = num_chmodes;
4218 if (uinfo->value.enumerated.item >= num_chmodes)
4219 uinfo->value.enumerated.item = num_chmodes - 1;
4220 sprintf(uinfo->value.enumerated.name, "%dch",
4221 chmode[uinfo->value.enumerated.item].channels);
4222 return 0;
4223}
ff7a3267 4224EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
d2a6d7dc 4225
d5191e50
TI
4226/**
4227 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4228 */
0ba21762
TI
4229int snd_hda_ch_mode_get(struct hda_codec *codec,
4230 struct snd_ctl_elem_value *ucontrol,
4231 const struct hda_channel_mode *chmode,
4232 int num_chmodes,
d2a6d7dc
TI
4233 int max_channels)
4234{
4235 int i;
4236
4237 for (i = 0; i < num_chmodes; i++) {
4238 if (max_channels == chmode[i].channels) {
4239 ucontrol->value.enumerated.item[0] = i;
4240 break;
4241 }
4242 }
4243 return 0;
4244}
ff7a3267 4245EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
d2a6d7dc 4246
d5191e50
TI
4247/**
4248 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4249 */
0ba21762
TI
4250int snd_hda_ch_mode_put(struct hda_codec *codec,
4251 struct snd_ctl_elem_value *ucontrol,
4252 const struct hda_channel_mode *chmode,
4253 int num_chmodes,
d2a6d7dc
TI
4254 int *max_channelsp)
4255{
4256 unsigned int mode;
4257
4258 mode = ucontrol->value.enumerated.item[0];
68ea7b2f
TI
4259 if (mode >= num_chmodes)
4260 return -EINVAL;
82beb8fd 4261 if (*max_channelsp == chmode[mode].channels)
d2a6d7dc
TI
4262 return 0;
4263 /* change the current channel setting */
4264 *max_channelsp = chmode[mode].channels;
4265 if (chmode[mode].sequence)
82beb8fd 4266 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
d2a6d7dc
TI
4267 return 1;
4268}
ff7a3267 4269EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
d2a6d7dc 4270
1da177e4
LT
4271/*
4272 * input MUX helper
4273 */
d5191e50
TI
4274
4275/**
4276 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4277 */
0ba21762
TI
4278int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4279 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
4280{
4281 unsigned int index;
4282
4283 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4284 uinfo->count = 1;
4285 uinfo->value.enumerated.items = imux->num_items;
5513b0c5
TI
4286 if (!imux->num_items)
4287 return 0;
1da177e4
LT
4288 index = uinfo->value.enumerated.item;
4289 if (index >= imux->num_items)
4290 index = imux->num_items - 1;
4291 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4292 return 0;
4293}
ff7a3267 4294EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
1da177e4 4295
d5191e50
TI
4296/**
4297 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4298 */
0ba21762
TI
4299int snd_hda_input_mux_put(struct hda_codec *codec,
4300 const struct hda_input_mux *imux,
4301 struct snd_ctl_elem_value *ucontrol,
4302 hda_nid_t nid,
1da177e4
LT
4303 unsigned int *cur_val)
4304{
4305 unsigned int idx;
4306
5513b0c5
TI
4307 if (!imux->num_items)
4308 return 0;
1da177e4
LT
4309 idx = ucontrol->value.enumerated.item[0];
4310 if (idx >= imux->num_items)
4311 idx = imux->num_items - 1;
82beb8fd 4312 if (*cur_val == idx)
1da177e4 4313 return 0;
82beb8fd
TI
4314 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4315 imux->items[idx].index);
1da177e4
LT
4316 *cur_val = idx;
4317 return 1;
4318}
ff7a3267 4319EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
1da177e4
LT
4320
4321
4322/*
4323 * Multi-channel / digital-out PCM helper functions
4324 */
4325
6b97eb45
TI
4326/* setup SPDIF output stream */
4327static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4328 unsigned int stream_tag, unsigned int format)
4329{
7c935976
SW
4330 struct hda_spdif_out *spdif = snd_hda_spdif_out_of_nid(codec, nid);
4331
6b97eb45 4332 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
7c935976 4333 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
28aedaf7 4334 set_dig_out_convert(codec, nid,
7c935976 4335 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
2f72853c 4336 -1);
6b97eb45 4337 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
2f72853c 4338 if (codec->slave_dig_outs) {
dda14410 4339 const hda_nid_t *d;
2f72853c
TI
4340 for (d = codec->slave_dig_outs; *d; d++)
4341 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4342 format);
4343 }
6b97eb45 4344 /* turn on again (if needed) */
7c935976 4345 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
2f72853c 4346 set_dig_out_convert(codec, nid,
7c935976 4347 spdif->ctls & 0xff, -1);
2f72853c 4348}
de51ca12 4349
2f72853c
TI
4350static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4351{
4352 snd_hda_codec_cleanup_stream(codec, nid);
4353 if (codec->slave_dig_outs) {
dda14410 4354 const hda_nid_t *d;
2f72853c
TI
4355 for (d = codec->slave_dig_outs; *d; d++)
4356 snd_hda_codec_cleanup_stream(codec, *d);
de51ca12 4357 }
6b97eb45
TI
4358}
4359
d5191e50
TI
4360/**
4361 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4362 * @bus: HD-audio bus
4363 */
fb8d1a34
TI
4364void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4365{
4366 struct hda_codec *codec;
4367
4368 if (!bus)
4369 return;
4370 list_for_each_entry(codec, &bus->codec_list, list) {
4371#ifdef CONFIG_SND_HDA_POWER_SAVE
4372 if (!codec->power_on)
4373 continue;
4374#endif
4375 if (codec->patch_ops.reboot_notify)
4376 codec->patch_ops.reboot_notify(codec);
4377 }
4378}
8f217a22 4379EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
fb8d1a34 4380
d5191e50
TI
4381/**
4382 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
1da177e4 4383 */
0ba21762
TI
4384int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4385 struct hda_multi_out *mout)
1da177e4 4386{
62932df8 4387 mutex_lock(&codec->spdif_mutex);
5930ca41
TI
4388 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4389 /* already opened as analog dup; reset it once */
2f72853c 4390 cleanup_dig_out_stream(codec, mout->dig_out_nid);
1da177e4 4391 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
62932df8 4392 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
4393 return 0;
4394}
ff7a3267 4395EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
1da177e4 4396
d5191e50
TI
4397/**
4398 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4399 */
6b97eb45
TI
4400int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4401 struct hda_multi_out *mout,
4402 unsigned int stream_tag,
4403 unsigned int format,
4404 struct snd_pcm_substream *substream)
4405{
4406 mutex_lock(&codec->spdif_mutex);
4407 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4408 mutex_unlock(&codec->spdif_mutex);
4409 return 0;
4410}
ff7a3267 4411EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
6b97eb45 4412
d5191e50
TI
4413/**
4414 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4415 */
9411e21c
TI
4416int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4417 struct hda_multi_out *mout)
4418{
4419 mutex_lock(&codec->spdif_mutex);
4420 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4421 mutex_unlock(&codec->spdif_mutex);
4422 return 0;
4423}
4424EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4425
d5191e50
TI
4426/**
4427 * snd_hda_multi_out_dig_close - release the digital out stream
1da177e4 4428 */
0ba21762
TI
4429int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4430 struct hda_multi_out *mout)
1da177e4 4431{
62932df8 4432 mutex_lock(&codec->spdif_mutex);
1da177e4 4433 mout->dig_out_used = 0;
62932df8 4434 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
4435 return 0;
4436}
ff7a3267 4437EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
1da177e4 4438
d5191e50
TI
4439/**
4440 * snd_hda_multi_out_analog_open - open analog outputs
4441 *
4442 * Open analog outputs and set up the hw-constraints.
4443 * If the digital outputs can be opened as slave, open the digital
4444 * outputs, too.
1da177e4 4445 */
0ba21762
TI
4446int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4447 struct hda_multi_out *mout,
9a08160b
TI
4448 struct snd_pcm_substream *substream,
4449 struct hda_pcm_stream *hinfo)
4450{
4451 struct snd_pcm_runtime *runtime = substream->runtime;
4452 runtime->hw.channels_max = mout->max_channels;
4453 if (mout->dig_out_nid) {
4454 if (!mout->analog_rates) {
4455 mout->analog_rates = hinfo->rates;
4456 mout->analog_formats = hinfo->formats;
4457 mout->analog_maxbps = hinfo->maxbps;
4458 } else {
4459 runtime->hw.rates = mout->analog_rates;
4460 runtime->hw.formats = mout->analog_formats;
4461 hinfo->maxbps = mout->analog_maxbps;
4462 }
4463 if (!mout->spdif_rates) {
4464 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4465 &mout->spdif_rates,
4466 &mout->spdif_formats,
4467 &mout->spdif_maxbps);
4468 }
4469 mutex_lock(&codec->spdif_mutex);
4470 if (mout->share_spdif) {
022b466f
TI
4471 if ((runtime->hw.rates & mout->spdif_rates) &&
4472 (runtime->hw.formats & mout->spdif_formats)) {
4473 runtime->hw.rates &= mout->spdif_rates;
4474 runtime->hw.formats &= mout->spdif_formats;
4475 if (mout->spdif_maxbps < hinfo->maxbps)
4476 hinfo->maxbps = mout->spdif_maxbps;
4477 } else {
4478 mout->share_spdif = 0;
4479 /* FIXME: need notify? */
4480 }
9a08160b 4481 }
eaa9985b 4482 mutex_unlock(&codec->spdif_mutex);
9a08160b 4483 }
1da177e4
LT
4484 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4485 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4486}
ff7a3267 4487EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
1da177e4 4488
d5191e50
TI
4489/**
4490 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4491 *
4492 * Set up the i/o for analog out.
4493 * When the digital out is available, copy the front out to digital out, too.
1da177e4 4494 */
0ba21762
TI
4495int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4496 struct hda_multi_out *mout,
1da177e4
LT
4497 unsigned int stream_tag,
4498 unsigned int format,
c8b6bf9b 4499 struct snd_pcm_substream *substream)
1da177e4 4500{
dda14410 4501 const hda_nid_t *nids = mout->dac_nids;
1da177e4 4502 int chs = substream->runtime->channels;
7c935976
SW
4503 struct hda_spdif_out *spdif =
4504 snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
1da177e4
LT
4505 int i;
4506
62932df8 4507 mutex_lock(&codec->spdif_mutex);
9a08160b
TI
4508 if (mout->dig_out_nid && mout->share_spdif &&
4509 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
1da177e4 4510 if (chs == 2 &&
0ba21762
TI
4511 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4512 format) &&
7c935976 4513 !(spdif->status & IEC958_AES0_NONAUDIO)) {
1da177e4 4514 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
6b97eb45
TI
4515 setup_dig_out_stream(codec, mout->dig_out_nid,
4516 stream_tag, format);
1da177e4
LT
4517 } else {
4518 mout->dig_out_used = 0;
2f72853c 4519 cleanup_dig_out_stream(codec, mout->dig_out_nid);
1da177e4
LT
4520 }
4521 }
62932df8 4522 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
4523
4524 /* front */
0ba21762
TI
4525 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4526 0, format);
d29240ce
TI
4527 if (!mout->no_share_stream &&
4528 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
1da177e4 4529 /* headphone out will just decode front left/right (stereo) */
0ba21762
TI
4530 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4531 0, format);
82bc955f
TI
4532 /* extra outputs copied from front */
4533 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
d29240ce 4534 if (!mout->no_share_stream && mout->extra_out_nid[i])
82bc955f
TI
4535 snd_hda_codec_setup_stream(codec,
4536 mout->extra_out_nid[i],
4537 stream_tag, 0, format);
4538
1da177e4
LT
4539 /* surrounds */
4540 for (i = 1; i < mout->num_dacs; i++) {
4b3acaf5 4541 if (chs >= (i + 1) * 2) /* independent out */
0ba21762
TI
4542 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4543 i * 2, format);
d29240ce 4544 else if (!mout->no_share_stream) /* copy front */
0ba21762
TI
4545 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4546 0, format);
1da177e4
LT
4547 }
4548 return 0;
4549}
ff7a3267 4550EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
1da177e4 4551
d5191e50
TI
4552/**
4553 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
1da177e4 4554 */
0ba21762
TI
4555int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4556 struct hda_multi_out *mout)
1da177e4 4557{
dda14410 4558 const hda_nid_t *nids = mout->dac_nids;
1da177e4
LT
4559 int i;
4560
4561 for (i = 0; i < mout->num_dacs; i++)
888afa15 4562 snd_hda_codec_cleanup_stream(codec, nids[i]);
1da177e4 4563 if (mout->hp_nid)
888afa15 4564 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
82bc955f
TI
4565 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4566 if (mout->extra_out_nid[i])
888afa15
TI
4567 snd_hda_codec_cleanup_stream(codec,
4568 mout->extra_out_nid[i]);
62932df8 4569 mutex_lock(&codec->spdif_mutex);
1da177e4 4570 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
2f72853c 4571 cleanup_dig_out_stream(codec, mout->dig_out_nid);
1da177e4
LT
4572 mout->dig_out_used = 0;
4573 }
62932df8 4574 mutex_unlock(&codec->spdif_mutex);
1da177e4
LT
4575 return 0;
4576}
ff7a3267 4577EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
1da177e4 4578
e9edcee0 4579/*
6b34500c 4580 * Helper for automatic pin configuration
e9edcee0 4581 */
df694daa 4582
dda14410 4583static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list)
df694daa
KY
4584{
4585 for (; *list; list++)
4586 if (*list == nid)
4587 return 1;
4588 return 0;
4589}
4590
81937d3b
SL
4591
4592/*
4593 * Sort an associated group of pins according to their sequence numbers.
4594 */
28aedaf7 4595static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
81937d3b
SL
4596 int num_pins)
4597{
4598 int i, j;
4599 short seq;
4600 hda_nid_t nid;
28aedaf7 4601
81937d3b
SL
4602 for (i = 0; i < num_pins; i++) {
4603 for (j = i + 1; j < num_pins; j++) {
4604 if (sequences[i] > sequences[j]) {
4605 seq = sequences[i];
4606 sequences[i] = sequences[j];
4607 sequences[j] = seq;
4608 nid = pins[i];
4609 pins[i] = pins[j];
4610 pins[j] = nid;
4611 }
4612 }
4613 }
4614}
4615
4616
75e0eb24
TI
4617/* add the found input-pin to the cfg->inputs[] table */
4618static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid,
4619 int type)
4620{
4621 if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
4622 cfg->inputs[cfg->num_inputs].pin = nid;
4623 cfg->inputs[cfg->num_inputs].type = type;
4624 cfg->num_inputs++;
4625 }
4626}
4627
4a4d4a69
TI
4628/* sort inputs in the order of AUTO_PIN_* type */
4629static void sort_autocfg_input_pins(struct auto_pin_cfg *cfg)
4630{
4631 int i, j;
4632
4633 for (i = 0; i < cfg->num_inputs; i++) {
4634 for (j = i + 1; j < cfg->num_inputs; j++) {
4635 if (cfg->inputs[i].type > cfg->inputs[j].type) {
4636 struct auto_pin_cfg_item tmp;
4637 tmp = cfg->inputs[i];
4638 cfg->inputs[i] = cfg->inputs[j];
4639 cfg->inputs[j] = tmp;
4640 }
4641 }
4642 }
4643}
4644
82bc955f
TI
4645/*
4646 * Parse all pin widgets and store the useful pin nids to cfg
4647 *
4648 * The number of line-outs or any primary output is stored in line_outs,
4649 * and the corresponding output pins are assigned to line_out_pins[],
4650 * in the order of front, rear, CLFE, side, ...
4651 *
4652 * If more extra outputs (speaker and headphone) are found, the pins are
eb06ed8f 4653 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
82bc955f
TI
4654 * is detected, one of speaker of HP pins is assigned as the primary
4655 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4656 * if any analog output exists.
28aedaf7 4657 *
86e2959a 4658 * The analog input pins are assigned to inputs array.
82bc955f
TI
4659 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4660 * respectively.
4661 */
12f288bf
TI
4662int snd_hda_parse_pin_def_config(struct hda_codec *codec,
4663 struct auto_pin_cfg *cfg,
dda14410 4664 const hda_nid_t *ignore_nids)
e9edcee0 4665{
0ef6ce7b 4666 hda_nid_t nid, end_nid;
81937d3b
SL
4667 short seq, assoc_line_out, assoc_speaker;
4668 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4669 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
f889fa91 4670 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
75e0eb24 4671 int i;
e9edcee0
TI
4672
4673 memset(cfg, 0, sizeof(*cfg));
4674
81937d3b
SL
4675 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4676 memset(sequences_speaker, 0, sizeof(sequences_speaker));
f889fa91 4677 memset(sequences_hp, 0, sizeof(sequences_hp));
81937d3b 4678 assoc_line_out = assoc_speaker = 0;
e9edcee0 4679
0ef6ce7b
TI
4680 end_nid = codec->start_nid + codec->num_nodes;
4681 for (nid = codec->start_nid; nid < end_nid; nid++) {
54d17403 4682 unsigned int wid_caps = get_wcaps(codec, nid);
a22d543a 4683 unsigned int wid_type = get_wcaps_type(wid_caps);
e9edcee0 4684 unsigned int def_conf;
1af7c5f0 4685 short assoc, loc, conn, dev;
e9edcee0
TI
4686
4687 /* read all default configuration for pin complex */
4688 if (wid_type != AC_WID_PIN)
4689 continue;
df694daa
KY
4690 /* ignore the given nids (e.g. pc-beep returns error) */
4691 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4692 continue;
4693
c17a1aba 4694 def_conf = snd_hda_codec_get_pincfg(codec, nid);
1af7c5f0
TI
4695 conn = get_defcfg_connect(def_conf);
4696 if (conn == AC_JACK_PORT_NONE)
e9edcee0
TI
4697 continue;
4698 loc = get_defcfg_location(def_conf);
1af7c5f0
TI
4699 dev = get_defcfg_device(def_conf);
4700
4701 /* workaround for buggy BIOS setups */
4702 if (dev == AC_JACK_LINE_OUT) {
4703 if (conn == AC_JACK_PORT_FIXED)
4704 dev = AC_JACK_SPEAKER;
4705 }
4706
4707 switch (dev) {
e9edcee0 4708 case AC_JACK_LINE_OUT:
e9edcee0
TI
4709 seq = get_defcfg_sequence(def_conf);
4710 assoc = get_defcfg_association(def_conf);
90da78bf
MR
4711
4712 if (!(wid_caps & AC_WCAP_STEREO))
4713 if (!cfg->mono_out_pin)
4714 cfg->mono_out_pin = nid;
0ba21762 4715 if (!assoc)
e9edcee0 4716 continue;
0ba21762 4717 if (!assoc_line_out)
e9edcee0
TI
4718 assoc_line_out = assoc;
4719 else if (assoc_line_out != assoc)
4720 continue;
4721 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4722 continue;
4723 cfg->line_out_pins[cfg->line_outs] = nid;
81937d3b 4724 sequences_line_out[cfg->line_outs] = seq;
e9edcee0
TI
4725 cfg->line_outs++;
4726 break;
8d88bc3d 4727 case AC_JACK_SPEAKER:
81937d3b
SL
4728 seq = get_defcfg_sequence(def_conf);
4729 assoc = get_defcfg_association(def_conf);
28aedaf7 4730 if (!assoc)
81937d3b 4731 continue;
28aedaf7 4732 if (!assoc_speaker)
81937d3b
SL
4733 assoc_speaker = assoc;
4734 else if (assoc_speaker != assoc)
4735 continue;
82bc955f
TI
4736 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4737 continue;
4738 cfg->speaker_pins[cfg->speaker_outs] = nid;
81937d3b 4739 sequences_speaker[cfg->speaker_outs] = seq;
82bc955f 4740 cfg->speaker_outs++;
8d88bc3d 4741 break;
e9edcee0 4742 case AC_JACK_HP_OUT:
f889fa91
TI
4743 seq = get_defcfg_sequence(def_conf);
4744 assoc = get_defcfg_association(def_conf);
eb06ed8f
TI
4745 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4746 continue;
4747 cfg->hp_pins[cfg->hp_outs] = nid;
f889fa91 4748 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
eb06ed8f 4749 cfg->hp_outs++;
e9edcee0 4750 break;
86e2959a
TI
4751 case AC_JACK_MIC_IN:
4752 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
e9edcee0 4753 break;
86e2959a
TI
4754 case AC_JACK_LINE_IN:
4755 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
e9edcee0
TI
4756 break;
4757 case AC_JACK_CD:
75e0eb24 4758 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
e9edcee0
TI
4759 break;
4760 case AC_JACK_AUX:
75e0eb24 4761 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
e9edcee0
TI
4762 break;
4763 case AC_JACK_SPDIF_OUT:
1b52ae70 4764 case AC_JACK_DIG_OTHER_OUT:
0852d7a6
TI
4765 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
4766 continue;
4767 cfg->dig_out_pins[cfg->dig_outs] = nid;
4768 cfg->dig_out_type[cfg->dig_outs] =
4769 (loc == AC_JACK_LOC_HDMI) ?
4770 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
4771 cfg->dig_outs++;
e9edcee0
TI
4772 break;
4773 case AC_JACK_SPDIF_IN:
1b52ae70 4774 case AC_JACK_DIG_OTHER_IN:
e9edcee0 4775 cfg->dig_in_pin = nid;
2297bd6e
TI
4776 if (loc == AC_JACK_LOC_HDMI)
4777 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
4778 else
4779 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
e9edcee0
TI
4780 break;
4781 }
4782 }
4783
5832fcf8
TI
4784 /* FIX-UP:
4785 * If no line-out is defined but multiple HPs are found,
4786 * some of them might be the real line-outs.
4787 */
4788 if (!cfg->line_outs && cfg->hp_outs > 1) {
4789 int i = 0;
4790 while (i < cfg->hp_outs) {
4791 /* The real HPs should have the sequence 0x0f */
4792 if ((sequences_hp[i] & 0x0f) == 0x0f) {
4793 i++;
4794 continue;
4795 }
4796 /* Move it to the line-out table */
4797 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
4798 sequences_line_out[cfg->line_outs] = sequences_hp[i];
4799 cfg->line_outs++;
4800 cfg->hp_outs--;
4801 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
4802 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
122661b6 4803 memmove(sequences_hp + i, sequences_hp + i + 1,
5832fcf8
TI
4804 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
4805 }
03642c9a
TI
4806 memset(cfg->hp_pins + cfg->hp_outs, 0,
4807 sizeof(hda_nid_t) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
b2d05760
TI
4808 if (!cfg->hp_outs)
4809 cfg->line_out_type = AUTO_PIN_HP_OUT;
4810
5832fcf8
TI
4811 }
4812
e9edcee0 4813 /* sort by sequence */
81937d3b
SL
4814 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
4815 cfg->line_outs);
4816 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
4817 cfg->speaker_outs);
f889fa91
TI
4818 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
4819 cfg->hp_outs);
28aedaf7 4820
81937d3b
SL
4821 /*
4822 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
4823 * as a primary output
4824 */
4825 if (!cfg->line_outs) {
4826 if (cfg->speaker_outs) {
4827 cfg->line_outs = cfg->speaker_outs;
4828 memcpy(cfg->line_out_pins, cfg->speaker_pins,
4829 sizeof(cfg->speaker_pins));
4830 cfg->speaker_outs = 0;
4831 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
4832 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
4833 } else if (cfg->hp_outs) {
4834 cfg->line_outs = cfg->hp_outs;
4835 memcpy(cfg->line_out_pins, cfg->hp_pins,
4836 sizeof(cfg->hp_pins));
4837 cfg->hp_outs = 0;
4838 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4839 cfg->line_out_type = AUTO_PIN_HP_OUT;
4840 }
4841 }
e9edcee0 4842
cb8e2f83
TI
4843 /* Reorder the surround channels
4844 * ALSA sequence is front/surr/clfe/side
4845 * HDA sequence is:
4846 * 4-ch: front/surr => OK as it is
4847 * 6-ch: front/clfe/surr
9422db40 4848 * 8-ch: front/clfe/rear/side|fc
cb8e2f83
TI
4849 */
4850 switch (cfg->line_outs) {
4851 case 3:
cb8e2f83
TI
4852 case 4:
4853 nid = cfg->line_out_pins[1];
9422db40 4854 cfg->line_out_pins[1] = cfg->line_out_pins[2];
cb8e2f83
TI
4855 cfg->line_out_pins[2] = nid;
4856 break;
e9edcee0
TI
4857 }
4858
4a4d4a69
TI
4859 sort_autocfg_input_pins(cfg);
4860
82bc955f
TI
4861 /*
4862 * debug prints of the parsed results
4863 */
c2de187e 4864 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x) type:%s\n",
82bc955f
TI
4865 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
4866 cfg->line_out_pins[2], cfg->line_out_pins[3],
c2de187e
TI
4867 cfg->line_out_pins[4],
4868 cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
4869 (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
4870 "speaker" : "line"));
82bc955f
TI
4871 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4872 cfg->speaker_outs, cfg->speaker_pins[0],
4873 cfg->speaker_pins[1], cfg->speaker_pins[2],
4874 cfg->speaker_pins[3], cfg->speaker_pins[4]);
eb06ed8f
TI
4875 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4876 cfg->hp_outs, cfg->hp_pins[0],
4877 cfg->hp_pins[1], cfg->hp_pins[2],
4878 cfg->hp_pins[3], cfg->hp_pins[4]);
90da78bf 4879 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
0852d7a6
TI
4880 if (cfg->dig_outs)
4881 snd_printd(" dig-out=0x%x/0x%x\n",
4882 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
75e0eb24
TI
4883 snd_printd(" inputs:");
4884 for (i = 0; i < cfg->num_inputs; i++) {
0b626737 4885 snd_printd(" %s=0x%x",
10a20af7 4886 hda_get_autocfg_input_label(codec, cfg, i),
75e0eb24
TI
4887 cfg->inputs[i].pin);
4888 }
4889 snd_printd("\n");
32d2c7fa 4890 if (cfg->dig_in_pin)
89ce9e87 4891 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
82bc955f 4892
e9edcee0
TI
4893 return 0;
4894}
ff7a3267 4895EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
e9edcee0 4896
99ae28be 4897int snd_hda_get_input_pin_attr(unsigned int def_conf)
a1c98515
TI
4898{
4899 unsigned int loc = get_defcfg_location(def_conf);
41c89ef3 4900 unsigned int conn = get_defcfg_connect(def_conf);
99ae28be
TI
4901 if (conn == AC_JACK_PORT_NONE)
4902 return INPUT_PIN_ATTR_UNUSED;
41c89ef3
TI
4903 /* Windows may claim the internal mic to be BOTH, too */
4904 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
99ae28be 4905 return INPUT_PIN_ATTR_INT;
41c89ef3 4906 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
99ae28be 4907 return INPUT_PIN_ATTR_INT;
a1c98515 4908 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
99ae28be 4909 return INPUT_PIN_ATTR_DOCK;
a1c98515 4910 if (loc == AC_JACK_LOC_REAR)
99ae28be 4911 return INPUT_PIN_ATTR_REAR;
a1c98515 4912 if (loc == AC_JACK_LOC_FRONT)
99ae28be
TI
4913 return INPUT_PIN_ATTR_FRONT;
4914 return INPUT_PIN_ATTR_NORMAL;
a1c98515 4915}
99ae28be 4916EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
a1c98515 4917
990061c2
TI
4918/**
4919 * hda_get_input_pin_label - Give a label for the given input pin
4920 *
4921 * When check_location is true, the function checks the pin location
4922 * for mic and line-in pins, and set an appropriate prefix like "Front",
4923 * "Rear", "Internal".
4924 */
4925
10a20af7
TI
4926const char *hda_get_input_pin_label(struct hda_codec *codec, hda_nid_t pin,
4927 int check_location)
4928{
a1c98515 4929 unsigned int def_conf;
ea734963 4930 static const char * const mic_names[] = {
a1c98515
TI
4931 "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
4932 };
99ae28be 4933 int attr;
10a20af7
TI
4934
4935 def_conf = snd_hda_codec_get_pincfg(codec, pin);
10a20af7
TI
4936
4937 switch (get_defcfg_device(def_conf)) {
4938 case AC_JACK_MIC_IN:
4939 if (!check_location)
4940 return "Mic";
99ae28be
TI
4941 attr = snd_hda_get_input_pin_attr(def_conf);
4942 if (!attr)
4943 return "None";
4944 return mic_names[attr - 1];
10a20af7
TI
4945 case AC_JACK_LINE_IN:
4946 if (!check_location)
4947 return "Line";
99ae28be
TI
4948 attr = snd_hda_get_input_pin_attr(def_conf);
4949 if (!attr)
4950 return "None";
4951 if (attr == INPUT_PIN_ATTR_DOCK)
4952 return "Dock Line";
4953 return "Line";
10a20af7
TI
4954 case AC_JACK_AUX:
4955 return "Aux";
4956 case AC_JACK_CD:
4957 return "CD";
4958 case AC_JACK_SPDIF_IN:
4959 return "SPDIF In";
4960 case AC_JACK_DIG_OTHER_IN:
4961 return "Digital In";
4962 default:
4963 return "Misc";
4964 }
4965}
4966EXPORT_SYMBOL_HDA(hda_get_input_pin_label);
4a471b7d 4967
a1c98515
TI
4968/* Check whether the location prefix needs to be added to the label.
4969 * If all mic-jacks are in the same location (e.g. rear panel), we don't
4970 * have to put "Front" prefix to each label. In such a case, returns false.
4971 */
4972static int check_mic_location_need(struct hda_codec *codec,
4973 const struct auto_pin_cfg *cfg,
4974 int input)
4975{
4976 unsigned int defc;
4977 int i, attr, attr2;
4978
4979 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
99ae28be 4980 attr = snd_hda_get_input_pin_attr(defc);
a1c98515 4981 /* for internal or docking mics, we need locations */
99ae28be 4982 if (attr <= INPUT_PIN_ATTR_NORMAL)
a1c98515
TI
4983 return 1;
4984
4985 attr = 0;
4986 for (i = 0; i < cfg->num_inputs; i++) {
4987 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
99ae28be
TI
4988 attr2 = snd_hda_get_input_pin_attr(defc);
4989 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
a1c98515
TI
4990 if (attr && attr != attr2)
4991 return 1; /* different locations found */
4992 attr = attr2;
4993 }
4994 }
4995 return 0;
4996}
4997
990061c2
TI
4998/**
4999 * hda_get_autocfg_input_label - Get a label for the given input
5000 *
5001 * Get a label for the given input pin defined by the autocfg item.
5002 * Unlike hda_get_input_pin_label(), this function checks all inputs
5003 * defined in autocfg and avoids the redundant mic/line prefix as much as
5004 * possible.
5005 */
10a20af7
TI
5006const char *hda_get_autocfg_input_label(struct hda_codec *codec,
5007 const struct auto_pin_cfg *cfg,
5008 int input)
d7b1ae9d
TI
5009{
5010 int type = cfg->inputs[input].type;
10a20af7 5011 int has_multiple_pins = 0;
d7b1ae9d 5012
10a20af7
TI
5013 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
5014 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
5015 has_multiple_pins = 1;
a1c98515
TI
5016 if (has_multiple_pins && type == AUTO_PIN_MIC)
5017 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
10a20af7
TI
5018 return hda_get_input_pin_label(codec, cfg->inputs[input].pin,
5019 has_multiple_pins);
5020}
5021EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
5022
990061c2
TI
5023/**
5024 * snd_hda_add_imux_item - Add an item to input_mux
5025 *
5026 * When the same label is used already in the existing items, the number
5027 * suffix is appended to the label. This label index number is stored
5028 * to type_idx when non-NULL pointer is given.
5029 */
10a20af7
TI
5030int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5031 int index, int *type_idx)
5032{
5033 int i, label_idx = 0;
5034 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5035 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5036 return -EINVAL;
5037 }
5038 for (i = 0; i < imux->num_items; i++) {
5039 if (!strncmp(label, imux->items[i].label, strlen(label)))
5040 label_idx++;
d7b1ae9d 5041 }
10a20af7
TI
5042 if (type_idx)
5043 *type_idx = label_idx;
5044 if (label_idx > 0)
5045 snprintf(imux->items[imux->num_items].label,
5046 sizeof(imux->items[imux->num_items].label),
5047 "%s %d", label, label_idx);
b5786e85 5048 else
10a20af7
TI
5049 strlcpy(imux->items[imux->num_items].label, label,
5050 sizeof(imux->items[imux->num_items].label));
5051 imux->items[imux->num_items].index = index;
5052 imux->num_items++;
5053 return 0;
d7b1ae9d 5054}
10a20af7 5055EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
d7b1ae9d 5056
4a471b7d 5057
1da177e4
LT
5058#ifdef CONFIG_PM
5059/*
5060 * power management
5061 */
5062
5063/**
5064 * snd_hda_suspend - suspend the codecs
5065 * @bus: the HDA bus
1da177e4
LT
5066 *
5067 * Returns 0 if successful.
5068 */
8dd78330 5069int snd_hda_suspend(struct hda_bus *bus)
1da177e4 5070{
0ba21762 5071 struct hda_codec *codec;
1da177e4 5072
0ba21762 5073 list_for_each_entry(codec, &bus->codec_list, list) {
0b7a2e9c
TI
5074#ifdef CONFIG_SND_HDA_POWER_SAVE
5075 if (!codec->power_on)
5076 continue;
5077#endif
cb53c626 5078 hda_call_codec_suspend(codec);
1da177e4
LT
5079 }
5080 return 0;
5081}
ff7a3267 5082EXPORT_SYMBOL_HDA(snd_hda_suspend);
1da177e4
LT
5083
5084/**
5085 * snd_hda_resume - resume the codecs
5086 * @bus: the HDA bus
1da177e4
LT
5087 *
5088 * Returns 0 if successful.
cb53c626 5089 *
25985edc 5090 * This function is defined only when POWER_SAVE isn't set.
cb53c626 5091 * In the power-save mode, the codec is resumed dynamically.
1da177e4
LT
5092 */
5093int snd_hda_resume(struct hda_bus *bus)
5094{
0ba21762 5095 struct hda_codec *codec;
1da177e4 5096
0ba21762 5097 list_for_each_entry(codec, &bus->codec_list, list) {
d804ad92
ML
5098 if (snd_hda_codec_needs_resume(codec))
5099 hda_call_codec_resume(codec);
1da177e4 5100 }
1da177e4
LT
5101 return 0;
5102}
ff7a3267 5103EXPORT_SYMBOL_HDA(snd_hda_resume);
1289e9e8 5104#endif /* CONFIG_PM */
b2e18597
TI
5105
5106/*
5107 * generic arrays
5108 */
5109
d5191e50
TI
5110/**
5111 * snd_array_new - get a new element from the given array
5112 * @array: the array object
28aedaf7 5113 *
d5191e50
TI
5114 * Get a new element from the given array. If it exceeds the
5115 * pre-allocated array size, re-allocate the array.
5116 *
5117 * Returns NULL if allocation failed.
b2e18597
TI
5118 */
5119void *snd_array_new(struct snd_array *array)
5120{
5121 if (array->used >= array->alloced) {
5122 int num = array->alloced + array->alloc_align;
b910d9ae
TI
5123 void *nlist;
5124 if (snd_BUG_ON(num >= 4096))
5125 return NULL;
5126 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
b2e18597
TI
5127 if (!nlist)
5128 return NULL;
5129 if (array->list) {
5130 memcpy(nlist, array->list,
5131 array->elem_size * array->alloced);
5132 kfree(array->list);
5133 }
5134 array->list = nlist;
5135 array->alloced = num;
5136 }
f43aa025 5137 return snd_array_elem(array, array->used++);
b2e18597 5138}
ff7a3267 5139EXPORT_SYMBOL_HDA(snd_array_new);
b2e18597 5140
d5191e50
TI
5141/**
5142 * snd_array_free - free the given array elements
5143 * @array: the array object
5144 */
b2e18597
TI
5145void snd_array_free(struct snd_array *array)
5146{
5147 kfree(array->list);
5148 array->used = 0;
5149 array->alloced = 0;
5150 array->list = NULL;
5151}
ff7a3267 5152EXPORT_SYMBOL_HDA(snd_array_free);
b2022266 5153
d5191e50
TI
5154/**
5155 * snd_print_pcm_rates - Print the supported PCM rates to the string buffer
5156 * @pcm: PCM caps bits
5157 * @buf: the string buffer to write
5158 * @buflen: the max buffer length
5159 *
b2022266
TI
5160 * used by hda_proc.c and hda_eld.c
5161 */
5162void snd_print_pcm_rates(int pcm, char *buf, int buflen)
5163{
5164 static unsigned int rates[] = {
5165 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
5166 96000, 176400, 192000, 384000
5167 };
5168 int i, j;
5169
5170 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
5171 if (pcm & (1 << i))
5172 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
5173
5174 buf[j] = '\0'; /* necessary when j == 0 */
5175}
ff7a3267 5176EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
b2022266 5177
d5191e50
TI
5178/**
5179 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5180 * @pcm: PCM caps bits
5181 * @buf: the string buffer to write
5182 * @buflen: the max buffer length
5183 *
5184 * used by hda_proc.c and hda_eld.c
5185 */
b2022266
TI
5186void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5187{
5188 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5189 int i, j;
5190
5191 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5192 if (pcm & (AC_SUPPCM_BITS_8 << i))
5193 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5194
5195 buf[j] = '\0'; /* necessary when j == 0 */
5196}
ff7a3267 5197EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
1289e9e8 5198
cd372fb3
TI
5199#ifdef CONFIG_SND_HDA_INPUT_JACK
5200/*
5201 * Input-jack notification support
5202 */
5203struct hda_jack_item {
5204 hda_nid_t nid;
5205 int type;
5206 struct snd_jack *jack;
5207};
5208
5209static const char *get_jack_default_name(struct hda_codec *codec, hda_nid_t nid,
5210 int type)
5211{
5212 switch (type) {
5213 case SND_JACK_HEADPHONE:
5214 return "Headphone";
5215 case SND_JACK_MICROPHONE:
5216 return "Mic";
5217 case SND_JACK_LINEOUT:
5218 return "Line-out";
5219 case SND_JACK_HEADSET:
5220 return "Headset";
07acecc1
DH
5221 case SND_JACK_VIDEOOUT:
5222 return "HDMI/DP";
cd372fb3
TI
5223 default:
5224 return "Misc";
5225 }
5226}
5227
5228static void hda_free_jack_priv(struct snd_jack *jack)
5229{
5230 struct hda_jack_item *jacks = jack->private_data;
5231 jacks->nid = 0;
5232 jacks->jack = NULL;
5233}
5234
5235int snd_hda_input_jack_add(struct hda_codec *codec, hda_nid_t nid, int type,
5236 const char *name)
5237{
5238 struct hda_jack_item *jack;
5239 int err;
5240
5241 snd_array_init(&codec->jacks, sizeof(*jack), 32);
5242 jack = snd_array_new(&codec->jacks);
5243 if (!jack)
5244 return -ENOMEM;
5245
5246 jack->nid = nid;
5247 jack->type = type;
5248 if (!name)
5249 name = get_jack_default_name(codec, nid, type);
5250 err = snd_jack_new(codec->bus->card, name, type, &jack->jack);
5251 if (err < 0) {
5252 jack->nid = 0;
5253 return err;
5254 }
5255 jack->jack->private_data = jack;
5256 jack->jack->private_free = hda_free_jack_priv;
5257 return 0;
5258}
5259EXPORT_SYMBOL_HDA(snd_hda_input_jack_add);
5260
5261void snd_hda_input_jack_report(struct hda_codec *codec, hda_nid_t nid)
5262{
5263 struct hda_jack_item *jacks = codec->jacks.list;
5264 int i;
5265
5266 if (!jacks)
5267 return;
5268
5269 for (i = 0; i < codec->jacks.used; i++, jacks++) {
5270 unsigned int pin_ctl;
5271 unsigned int present;
5272 int type;
5273
5274 if (jacks->nid != nid)
5275 continue;
5276 present = snd_hda_jack_detect(codec, nid);
5277 type = jacks->type;
5278 if (type == (SND_JACK_HEADPHONE | SND_JACK_LINEOUT)) {
5279 pin_ctl = snd_hda_codec_read(codec, nid, 0,
5280 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5281 type = (pin_ctl & AC_PINCTL_HP_EN) ?
5282 SND_JACK_HEADPHONE : SND_JACK_LINEOUT;
5283 }
5284 snd_jack_report(jacks->jack, present ? type : 0);
5285 }
5286}
5287EXPORT_SYMBOL_HDA(snd_hda_input_jack_report);
5288
5289/* free jack instances manually when clearing/reconfiguring */
5290void snd_hda_input_jack_free(struct hda_codec *codec)
5291{
5292 if (!codec->bus->shutdown && codec->jacks.list) {
5293 struct hda_jack_item *jacks = codec->jacks.list;
5294 int i;
5295 for (i = 0; i < codec->jacks.used; i++, jacks++) {
5296 if (jacks->jack)
5297 snd_device_free(codec->bus->card, jacks->jack);
5298 }
5299 }
5300 snd_array_free(&codec->jacks);
5301}
5302EXPORT_SYMBOL_HDA(snd_hda_input_jack_free);
5303#endif /* CONFIG_SND_HDA_INPUT_JACK */
5304
1289e9e8
TI
5305MODULE_DESCRIPTION("HDA codec core");
5306MODULE_LICENSE("GPL");
This page took 0.978421 seconds and 5 git commands to generate.