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