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