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