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