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