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