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