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