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