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