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