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