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