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