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